blob: 5a28ef571f022194f5e049b4a394909015985fad [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;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800226 u8 bssid[ETH_ALEN], *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
246 if (wpa_sm_get_bssid(sm, bssid) < 0) {
247 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
248 "Failed to read BSSID for EAPOL-Key request");
249 return;
250 }
251
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700252 mic_len = wpa_mic_len(sm->key_mgmt, sm->pmk_len);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800253 hdrlen = sizeof(*reply) + mic_len + 2;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700254 rbuf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_KEY, NULL,
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800255 hdrlen, &rlen, (void *) &reply);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700256 if (rbuf == NULL)
257 return;
258
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800259 reply->type = (sm->proto == WPA_PROTO_RSN ||
260 sm->proto == WPA_PROTO_OSEN) ?
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700261 EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA;
262 key_info = WPA_KEY_INFO_REQUEST | ver;
263 if (sm->ptk_set)
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800264 key_info |= WPA_KEY_INFO_SECURE;
265 if (sm->ptk_set && mic_len)
266 key_info |= WPA_KEY_INFO_MIC;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700267 if (error)
268 key_info |= WPA_KEY_INFO_ERROR;
269 if (pairwise)
270 key_info |= WPA_KEY_INFO_KEY_TYPE;
271 WPA_PUT_BE16(reply->key_info, key_info);
272 WPA_PUT_BE16(reply->key_length, 0);
273 os_memcpy(reply->replay_counter, sm->request_counter,
274 WPA_REPLAY_COUNTER_LEN);
275 inc_byte_array(sm->request_counter, WPA_REPLAY_COUNTER_LEN);
276
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800277 mic = (u8 *) (reply + 1);
278 WPA_PUT_BE16(mic + mic_len, 0);
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800279 if (!(key_info & WPA_KEY_INFO_MIC))
280 key_mic = NULL;
281 else
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800282 key_mic = mic;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700283
284 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
285 "WPA: Sending EAPOL-Key Request (error=%d "
286 "pairwise=%d ptk_set=%d len=%lu)",
287 error, pairwise, sm->ptk_set, (unsigned long) rlen);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800288 wpa_eapol_key_send(sm, &sm->ptk, ver, bssid, ETH_P_EAPOL, rbuf, rlen,
289 key_mic);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700290}
291
292
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800293static void wpa_supplicant_key_mgmt_set_pmk(struct wpa_sm *sm)
294{
295#ifdef CONFIG_IEEE80211R
296 if (sm->key_mgmt == WPA_KEY_MGMT_FT_IEEE8021X) {
297 if (wpa_sm_key_mgmt_set_pmk(sm, sm->xxkey, sm->xxkey_len))
298 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
299 "RSN: Cannot set low order 256 bits of MSK for key management offload");
300 } else {
301#endif /* CONFIG_IEEE80211R */
302 if (wpa_sm_key_mgmt_set_pmk(sm, sm->pmk, sm->pmk_len))
303 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
304 "RSN: Cannot set PMK for key management offload");
305#ifdef CONFIG_IEEE80211R
306 }
307#endif /* CONFIG_IEEE80211R */
308}
309
310
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700311static int wpa_supplicant_get_pmk(struct wpa_sm *sm,
312 const unsigned char *src_addr,
313 const u8 *pmkid)
314{
315 int abort_cached = 0;
316
317 if (pmkid && !sm->cur_pmksa) {
318 /* When using drivers that generate RSN IE, wpa_supplicant may
319 * not have enough time to get the association information
320 * event before receiving this 1/4 message, so try to find a
321 * matching PMKSA cache entry here. */
Sunil Ravi77d572f2023-01-17 23:58:31 +0000322 sm->cur_pmksa = pmksa_cache_get(sm->pmksa, src_addr,
323 sm->own_addr, pmkid,
Roshan Pius3a1667e2018-07-03 15:17:14 -0700324 NULL, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700325 if (sm->cur_pmksa) {
326 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
327 "RSN: found matching PMKID from PMKSA cache");
328 } else {
329 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
330 "RSN: no matching PMKID found");
331 abort_cached = 1;
332 }
333 }
334
335 if (pmkid && sm->cur_pmksa &&
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700336 os_memcmp_const(pmkid, sm->cur_pmksa->pmkid, PMKID_LEN) == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700337 wpa_hexdump(MSG_DEBUG, "RSN: matched PMKID", pmkid, PMKID_LEN);
338 wpa_sm_set_pmk_from_pmksa(sm);
339 wpa_hexdump_key(MSG_DEBUG, "RSN: PMK from PMKSA cache",
340 sm->pmk, sm->pmk_len);
341 eapol_sm_notify_cached(sm->eapol);
342#ifdef CONFIG_IEEE80211R
343 sm->xxkey_len = 0;
Roshan Pius3a1667e2018-07-03 15:17:14 -0700344#ifdef CONFIG_SAE
Sunil Ravi89eba102022-09-13 21:04:37 -0700345 if ((sm->key_mgmt == WPA_KEY_MGMT_FT_SAE ||
346 sm->key_mgmt == WPA_KEY_MGMT_FT_SAE_EXT_KEY) &&
Roshan Pius3a1667e2018-07-03 15:17:14 -0700347 sm->pmk_len == PMK_LEN) {
348 /* Need to allow FT key derivation to proceed with
349 * PMK from SAE being used as the XXKey in cases where
350 * the PMKID in msg 1/4 matches the PMKSA entry that was
351 * just added based on SAE authentication for the
352 * initial mobility domain association. */
353 os_memcpy(sm->xxkey, sm->pmk, sm->pmk_len);
354 sm->xxkey_len = sm->pmk_len;
355 }
356#endif /* CONFIG_SAE */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700357#endif /* CONFIG_IEEE80211R */
358 } else if (wpa_key_mgmt_wpa_ieee8021x(sm->key_mgmt) && sm->eapol) {
359 int res, pmk_len;
Hai Shalom81f62d82019-07-22 12:10:00 -0700360#ifdef CONFIG_IEEE80211R
361 u8 buf[2 * PMK_LEN];
362#endif /* CONFIG_IEEE80211R */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800363
Dmitry Shmidtebd93af2017-02-21 13:40:44 -0800364 if (wpa_key_mgmt_sha384(sm->key_mgmt))
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800365 pmk_len = PMK_LEN_SUITE_B_192;
366 else
367 pmk_len = PMK_LEN;
368 res = eapol_sm_get_key(sm->eapol, sm->pmk, pmk_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700369 if (res) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800370 if (pmk_len == PMK_LEN) {
371 /*
372 * EAP-LEAP is an exception from other EAP
373 * methods: it uses only 16-byte PMK.
374 */
375 res = eapol_sm_get_key(sm->eapol, sm->pmk, 16);
376 pmk_len = 16;
377 }
Hai Shalomf1c97642019-07-19 23:42:07 +0000378 }
Hai Shalom81f62d82019-07-22 12:10:00 -0700379#ifdef CONFIG_IEEE80211R
380 if (res == 0 &&
381 eapol_sm_get_key(sm->eapol, buf, 2 * PMK_LEN) == 0) {
382 if (wpa_key_mgmt_sha384(sm->key_mgmt)) {
383 os_memcpy(sm->xxkey, buf, SHA384_MAC_LEN);
384 sm->xxkey_len = SHA384_MAC_LEN;
385 } else {
386 os_memcpy(sm->xxkey, buf + PMK_LEN, PMK_LEN);
387 sm->xxkey_len = PMK_LEN;
388 }
389 forced_memzero(buf, sizeof(buf));
390 if (sm->proto == WPA_PROTO_RSN &&
391 wpa_key_mgmt_ft(sm->key_mgmt)) {
392 struct rsn_pmksa_cache_entry *sa = NULL;
393 const u8 *fils_cache_id = NULL;
394
395#ifdef CONFIG_FILS
396 if (sm->fils_cache_id_set)
397 fils_cache_id = sm->fils_cache_id;
398#endif /* CONFIG_FILS */
399 wpa_hexdump_key(MSG_DEBUG,
400 "FT: Cache XXKey/MPMK",
401 sm->xxkey, sm->xxkey_len);
402 sa = pmksa_cache_add(sm->pmksa,
403 sm->xxkey, sm->xxkey_len,
404 NULL, NULL, 0,
405 src_addr, sm->own_addr,
406 sm->network_ctx,
407 sm->key_mgmt,
408 fils_cache_id);
409 if (!sm->cur_pmksa)
410 sm->cur_pmksa = sa;
411 }
412 }
413#endif /* CONFIG_IEEE80211R */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700414 if (res == 0) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700415 struct rsn_pmksa_cache_entry *sa = NULL;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700416 const u8 *fils_cache_id = NULL;
417
418#ifdef CONFIG_FILS
419 if (sm->fils_cache_id_set)
420 fils_cache_id = sm->fils_cache_id;
421#endif /* CONFIG_FILS */
422
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700423 wpa_hexdump_key(MSG_DEBUG, "WPA: PMK from EAPOL state "
424 "machines", sm->pmk, pmk_len);
425 sm->pmk_len = pmk_len;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800426 wpa_supplicant_key_mgmt_set_pmk(sm);
Dmitry Shmidtc55524a2011-07-07 11:18:38 -0700427 if (sm->proto == WPA_PROTO_RSN &&
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800428 !wpa_key_mgmt_suite_b(sm->key_mgmt) &&
Dmitry Shmidtc55524a2011-07-07 11:18:38 -0700429 !wpa_key_mgmt_ft(sm->key_mgmt)) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700430 sa = pmksa_cache_add(sm->pmksa,
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800431 sm->pmk, pmk_len, NULL,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800432 NULL, 0,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700433 src_addr, sm->own_addr,
434 sm->network_ctx,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700435 sm->key_mgmt,
436 fils_cache_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700437 }
438 if (!sm->cur_pmksa && pmkid &&
Sunil Ravi77d572f2023-01-17 23:58:31 +0000439 pmksa_cache_get(sm->pmksa, src_addr, sm->own_addr,
440 pmkid, NULL, 0)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700441 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
442 "RSN: the new PMK matches with the "
443 "PMKID");
444 abort_cached = 0;
Jouni Malinen6ec30382015-07-08 20:48:18 +0300445 } else if (sa && !sm->cur_pmksa && pmkid) {
446 /*
447 * It looks like the authentication server
448 * derived mismatching MSK. This should not
449 * really happen, but bugs happen.. There is not
450 * much we can do here without knowing what
451 * exactly caused the server to misbehave.
452 */
Dmitry Shmidt014a3ff2015-12-28 13:27:49 -0800453 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
Jouni Malinen6ec30382015-07-08 20:48:18 +0300454 "RSN: PMKID mismatch - authentication server may have derived different MSK?!");
455 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700456 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700457
458 if (!sm->cur_pmksa)
459 sm->cur_pmksa = sa;
Hai Shalom81f62d82019-07-22 12:10:00 -0700460#ifdef CONFIG_IEEE80211R
461 } else if (wpa_key_mgmt_ft(sm->key_mgmt) && sm->ft_protocol) {
462 wpa_printf(MSG_DEBUG,
463 "FT: Continue 4-way handshake without PMK/PMKID for association using FT protocol");
464#endif /* CONFIG_IEEE80211R */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700465 } else {
466 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
467 "WPA: Failed to get master session key from "
468 "EAPOL state machines - key handshake "
469 "aborted");
470 if (sm->cur_pmksa) {
471 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
472 "RSN: Cancelled PMKSA caching "
473 "attempt");
474 sm->cur_pmksa = NULL;
475 abort_cached = 1;
476 } else if (!abort_cached) {
477 return -1;
478 }
479 }
480 }
481
Dmitry Shmidtc55524a2011-07-07 11:18:38 -0700482 if (abort_cached && wpa_key_mgmt_wpa_ieee8021x(sm->key_mgmt) &&
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800483 !wpa_key_mgmt_suite_b(sm->key_mgmt) &&
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800484 !wpa_key_mgmt_ft(sm->key_mgmt) && sm->key_mgmt != WPA_KEY_MGMT_OSEN)
485 {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700486 /* Send EAPOL-Start to trigger full EAP authentication. */
487 u8 *buf;
488 size_t buflen;
489
490 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
491 "RSN: no PMKSA entry found - trigger "
492 "full EAP authentication");
493 buf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_START,
494 NULL, 0, &buflen, NULL);
495 if (buf) {
Hai Shalomc1a21442022-02-04 13:43:00 -0800496 /* Set and reset eapFail to allow EAP state machine to
497 * proceed with new authentication. */
498 eapol_sm_notify_eap_fail(sm->eapol, true);
499 eapol_sm_notify_eap_fail(sm->eapol, false);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700500 wpa_sm_ether_send(sm, sm->bssid, ETH_P_EAPOL,
501 buf, buflen);
502 os_free(buf);
503 return -2;
504 }
505
506 return -1;
507 }
508
509 return 0;
510}
511
512
513/**
514 * wpa_supplicant_send_2_of_4 - Send message 2 of WPA/RSN 4-Way Handshake
515 * @sm: Pointer to WPA state machine data from wpa_sm_init()
516 * @dst: Destination address for the frame
517 * @key: Pointer to the EAPOL-Key frame header
518 * @ver: Version bits from EAPOL-Key Key Info
519 * @nonce: Nonce value for the EAPOL-Key frame
520 * @wpa_ie: WPA/RSN IE
521 * @wpa_ie_len: Length of the WPA/RSN IE
522 * @ptk: PTK to use for keyed hash and encryption
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800523 * Returns: >= 0 on success, < 0 on failure
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700524 */
525int wpa_supplicant_send_2_of_4(struct wpa_sm *sm, const unsigned char *dst,
526 const struct wpa_eapol_key *key,
527 int ver, const u8 *nonce,
528 const u8 *wpa_ie, size_t wpa_ie_len,
529 struct wpa_ptk *ptk)
530{
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800531 size_t mic_len, hdrlen, rlen;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700532 struct wpa_eapol_key *reply;
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800533 u8 *rbuf, *key_mic;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700534 u8 *rsn_ie_buf = NULL;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800535 u16 key_info;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700536
537 if (wpa_ie == NULL) {
538 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, "WPA: No wpa_ie set - "
539 "cannot generate msg 2/4");
540 return -1;
541 }
542
543#ifdef CONFIG_IEEE80211R
544 if (wpa_key_mgmt_ft(sm->key_mgmt)) {
545 int res;
546
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -0800547 wpa_hexdump(MSG_DEBUG, "WPA: WPA IE before FT processing",
548 wpa_ie, wpa_ie_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700549 /*
550 * Add PMKR1Name into RSN IE (PMKID-List) and add MDIE and
551 * FTIE from (Re)Association Response.
552 */
553 rsn_ie_buf = os_malloc(wpa_ie_len + 2 + 2 + PMKID_LEN +
554 sm->assoc_resp_ies_len);
555 if (rsn_ie_buf == NULL)
556 return -1;
557 os_memcpy(rsn_ie_buf, wpa_ie, wpa_ie_len);
Dmitry Shmidt55840ad2015-12-14 12:45:46 -0800558 res = wpa_insert_pmkid(rsn_ie_buf, &wpa_ie_len,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700559 sm->pmk_r1_name);
560 if (res < 0) {
561 os_free(rsn_ie_buf);
562 return -1;
563 }
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -0800564 wpa_hexdump(MSG_DEBUG,
565 "WPA: WPA IE after PMKID[PMKR1Name] addition into RSNE",
566 rsn_ie_buf, wpa_ie_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700567
568 if (sm->assoc_resp_ies) {
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -0800569 wpa_hexdump(MSG_DEBUG, "WPA: Add assoc_resp_ies",
570 sm->assoc_resp_ies,
571 sm->assoc_resp_ies_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700572 os_memcpy(rsn_ie_buf + wpa_ie_len, sm->assoc_resp_ies,
573 sm->assoc_resp_ies_len);
574 wpa_ie_len += sm->assoc_resp_ies_len;
575 }
576
577 wpa_ie = rsn_ie_buf;
578 }
579#endif /* CONFIG_IEEE80211R */
580
581 wpa_hexdump(MSG_DEBUG, "WPA: WPA IE for msg 2/4", wpa_ie, wpa_ie_len);
582
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700583 mic_len = wpa_mic_len(sm->key_mgmt, sm->pmk_len);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800584 hdrlen = sizeof(*reply) + mic_len + 2;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700585 rbuf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_KEY,
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800586 NULL, hdrlen + wpa_ie_len,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700587 &rlen, (void *) &reply);
588 if (rbuf == NULL) {
589 os_free(rsn_ie_buf);
590 return -1;
591 }
592
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800593 reply->type = (sm->proto == WPA_PROTO_RSN ||
594 sm->proto == WPA_PROTO_OSEN) ?
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700595 EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800596 key_info = ver | WPA_KEY_INFO_KEY_TYPE;
Sunil8cd6f4d2022-06-28 18:40:46 +0000597 if (sm->ptk_set && sm->proto != WPA_PROTO_WPA)
598 key_info |= WPA_KEY_INFO_SECURE;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800599 if (mic_len)
600 key_info |= WPA_KEY_INFO_MIC;
601 else
602 key_info |= WPA_KEY_INFO_ENCR_KEY_DATA;
603 WPA_PUT_BE16(reply->key_info, key_info);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800604 if (sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700605 WPA_PUT_BE16(reply->key_length, 0);
606 else
607 os_memcpy(reply->key_length, key->key_length, 2);
608 os_memcpy(reply->replay_counter, key->replay_counter,
609 WPA_REPLAY_COUNTER_LEN);
610 wpa_hexdump(MSG_DEBUG, "WPA: Replay Counter", reply->replay_counter,
611 WPA_REPLAY_COUNTER_LEN);
612
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800613 key_mic = (u8 *) (reply + 1);
614 WPA_PUT_BE16(key_mic + mic_len, wpa_ie_len); /* Key Data Length */
615 os_memcpy(key_mic + mic_len + 2, wpa_ie, wpa_ie_len); /* Key Data */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700616 os_free(rsn_ie_buf);
617
618 os_memcpy(reply->key_nonce, nonce, WPA_NONCE_LEN);
619
Roshan Pius5e7db942018-04-12 12:27:41 -0700620 wpa_dbg(sm->ctx->msg_ctx, MSG_INFO, "WPA: Sending EAPOL-Key 2/4");
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800621 return wpa_eapol_key_send(sm, ptk, ver, dst, ETH_P_EAPOL, rbuf, rlen,
622 key_mic);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700623}
624
625
626static int wpa_derive_ptk(struct wpa_sm *sm, const unsigned char *src_addr,
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800627 const struct wpa_eapol_key *key, struct wpa_ptk *ptk)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700628{
Sunil Ravi89eba102022-09-13 21:04:37 -0700629 int ret;
Hai Shalom021b0b52019-04-10 11:17:58 -0700630 const u8 *z = NULL;
Hai Shalom60840252021-02-19 19:02:11 -0800631 size_t z_len = 0, kdk_len;
Hai Shalomfdcde762020-04-02 11:19:20 -0700632 int akmp;
Hai Shalom021b0b52019-04-10 11:17:58 -0700633
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700634#ifdef CONFIG_IEEE80211R
635 if (wpa_key_mgmt_ft(sm->key_mgmt))
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800636 return wpa_derive_ptk_ft(sm, src_addr, key, ptk);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700637#endif /* CONFIG_IEEE80211R */
638
Hai Shalom021b0b52019-04-10 11:17:58 -0700639#ifdef CONFIG_DPP2
640 if (sm->key_mgmt == WPA_KEY_MGMT_DPP && sm->dpp_z) {
641 z = wpabuf_head(sm->dpp_z);
642 z_len = wpabuf_len(sm->dpp_z);
643 }
644#endif /* CONFIG_DPP2 */
645
Hai Shalomfdcde762020-04-02 11:19:20 -0700646 akmp = sm->key_mgmt;
647#ifdef CONFIG_OWE
648 if (sm->owe_ptk_workaround && akmp == WPA_KEY_MGMT_OWE &&
649 sm->pmk_len > 32) {
650 wpa_printf(MSG_DEBUG,
651 "OWE: Force SHA256 for PTK derivation");
652 akmp |= WPA_KEY_MGMT_PSK_SHA256;
653 }
654#endif /* CONFIG_OWE */
Hai Shalom60840252021-02-19 19:02:11 -0800655
656 if (sm->force_kdk_derivation ||
Hai Shalomc1a21442022-02-04 13:43:00 -0800657 (sm->secure_ltf &&
658 ieee802_11_rsnx_capab(sm->ap_rsnxe, WLAN_RSNX_CAPAB_SECURE_LTF)))
Hai Shalom60840252021-02-19 19:02:11 -0800659 kdk_len = WPA_KDK_MAX_LEN;
660 else
661 kdk_len = 0;
662
Sunil Ravi89eba102022-09-13 21:04:37 -0700663 ret = wpa_pmk_to_ptk(sm->pmk, sm->pmk_len, "Pairwise key expansion",
Sunil Ravi77d572f2023-01-17 23:58:31 +0000664 sm->own_addr, wpa_sm_get_auth_addr(sm), sm->snonce,
Sunil Ravi89eba102022-09-13 21:04:37 -0700665 key->key_nonce, ptk, akmp,
666 sm->pairwise_cipher, z, z_len,
667 kdk_len);
668 if (ret) {
669 wpa_printf(MSG_ERROR, "WPA: PTK derivation failed");
670 return ret;
671 }
672
673#ifdef CONFIG_PASN
674 if (sm->secure_ltf &&
675 ieee802_11_rsnx_capab(sm->ap_rsnxe, WLAN_RSNX_CAPAB_SECURE_LTF))
676 ret = wpa_ltf_keyseed(ptk, akmp, sm->pairwise_cipher);
677#endif /* CONFIG_PASN */
678
679 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700680}
681
682
Hai Shalomfdcde762020-04-02 11:19:20 -0700683static int wpa_handle_ext_key_id(struct wpa_sm *sm,
684 struct wpa_eapol_ie_parse *kde)
685{
686 if (sm->ext_key_id) {
687 u16 key_id;
688
689 if (!kde->key_id) {
690 wpa_msg(sm->ctx->msg_ctx,
691 sm->use_ext_key_id ? MSG_INFO : MSG_DEBUG,
692 "RSN: No Key ID in Extended Key ID handshake");
693 sm->keyidx_active = 0;
694 return sm->use_ext_key_id ? -1 : 0;
695 }
696
697 key_id = kde->key_id[0] & 0x03;
698 if (key_id > 1) {
699 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
700 "RSN: Invalid Extended Key ID: %d", key_id);
701 return -1;
702 }
703 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
704 "RSN: Using Extended Key ID %d", key_id);
705 sm->keyidx_active = key_id;
706 sm->use_ext_key_id = 1;
707 } else {
708 if (kde->key_id && (kde->key_id[0] & 0x03)) {
709 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
710 "RSN: Non-zero Extended Key ID Key ID in PTK0 handshake");
711 return -1;
712 }
713
714 if (kde->key_id) {
715 /* This is not supposed to be included here, but ignore
716 * the case of matching Key ID 0 just in case. */
717 wpa_msg(sm->ctx->msg_ctx, MSG_DEBUG,
718 "RSN: Extended Key ID Key ID 0 in PTK0 handshake");
719 }
720 sm->keyidx_active = 0;
721 sm->use_ext_key_id = 0;
722 }
723
724 return 0;
725}
726
727
Sunil Ravi77d572f2023-01-17 23:58:31 +0000728static u8 * rsn_add_kde(u8 *pos, u32 kde, const u8 *data, size_t data_len)
729{
730 *pos++ = WLAN_EID_VENDOR_SPECIFIC;
731 *pos++ = RSN_SELECTOR_LEN + data_len;
732 RSN_SELECTOR_PUT(pos, kde);
733 pos += RSN_SELECTOR_LEN;
734 os_memcpy(pos, data, data_len);
735 pos += data_len;
736
737 return pos;
738}
739
740
741static size_t wpa_mlo_link_kde_len(struct wpa_sm *sm)
742{
743 int i;
744 unsigned int num_links = 0;
745
746 for (i = 0; i < MAX_NUM_MLO_LINKS; i++) {
747 if (sm->mlo.assoc_link_id != i && (sm->mlo.req_links & BIT(i)))
748 num_links++;
749 }
750
751 return num_links * (RSN_SELECTOR_LEN + 1 + ETH_ALEN + 2);
752}
753
754
755static u8 * wpa_mlo_link_kde(struct wpa_sm *sm, u8 *pos)
756{
757 int i;
758 u8 hdr[1 + ETH_ALEN];
759
760 for (i = 0; i < MAX_NUM_MLO_LINKS; i++) {
761 if (sm->mlo.assoc_link_id == i || !(sm->mlo.req_links & BIT(i)))
762 continue;
763
764 wpa_printf(MSG_DEBUG,
765 "MLO: Add MLO Link %d KDE in EAPOL-Key 2/4", i);
766 hdr[0] = i & 0xF; /* LinkID; no RSNE or RSNXE */
767 os_memcpy(&hdr[1], sm->mlo.links[i].addr, ETH_ALEN);
768 pos = rsn_add_kde(pos, RSN_KEY_DATA_MLO_LINK, hdr, sizeof(hdr));
769 }
770
771 return pos;
772}
773
774
775static bool is_valid_ap_mld_mac_kde(struct wpa_sm *sm, const u8 *mac_kde)
776{
777 return mac_kde &&
778 os_memcmp(mac_kde, sm->mlo.ap_mld_addr, ETH_ALEN) == 0;
779}
780
781
782static void wpas_swap_tkip_mic_keys(struct wpa_ptk *ptk)
783{
784 u8 buf[8];
785
786 /* Supplicant: swap tx/rx Mic keys */
787 os_memcpy(buf, &ptk->tk[16], 8);
788 os_memcpy(&ptk->tk[16], &ptk->tk[24], 8);
789 os_memcpy(&ptk->tk[24], buf, 8);
790 forced_memzero(buf, sizeof(buf));
791}
792
793
794static void wpa_supplicant_process_1_of_4_wpa(struct wpa_sm *sm,
795 const unsigned char *src_addr,
796 const struct wpa_eapol_key *key,
797 u16 ver, const u8 *key_data,
798 size_t key_data_len,
799 enum frame_encryption encrypted)
800{
801 struct wpa_eapol_ie_parse ie;
802 struct wpa_ptk *ptk;
803 int res;
804
805 if (wpa_sm_get_network_ctx(sm) == NULL) {
806 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
807 "WPA: No SSID info found (msg 1 of 4)");
808 return;
809 }
810
811 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
812 "WPA: RX message 1 of 4-Way Handshake from " MACSTR
813 " (ver=%d)", MAC2STR(src_addr), ver);
814
815 os_memset(&ie, 0, sizeof(ie));
816
817 res = wpa_supplicant_get_pmk(sm, src_addr, ie.pmkid);
818 if (res == -2) {
819 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
820 "WPA: Do not reply to msg 1/4 - requesting full EAP authentication");
821 return;
822 }
823 if (res)
824 goto failed;
825
826 wpa_sm_set_state(sm, WPA_4WAY_HANDSHAKE);
827
828 if (sm->renew_snonce) {
829 if (random_get_bytes(sm->snonce, WPA_NONCE_LEN)) {
830 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
831 "WPA: Failed to get random data for SNonce");
832 goto failed;
833 }
834 sm->renew_snonce = 0;
835 wpa_hexdump(MSG_DEBUG, "WPA: Renewed SNonce",
836 sm->snonce, WPA_NONCE_LEN);
837 }
838
839 /* Calculate PTK which will be stored as a temporary PTK until it has
840 * been verified when processing message 3/4. */
841 ptk = &sm->tptk;
842 if (wpa_derive_ptk(sm, src_addr, key, ptk) < 0)
843 goto failed;
844 if (sm->pairwise_cipher == WPA_CIPHER_TKIP)
845 wpas_swap_tkip_mic_keys(ptk);
846 sm->tptk_set = 1;
847
848 if (wpa_supplicant_send_2_of_4(sm, wpa_sm_get_auth_addr(sm), key, ver,
849 sm->snonce, sm->assoc_wpa_ie,
850 sm->assoc_wpa_ie_len, ptk) < 0)
851 goto failed;
852
853 os_memcpy(sm->anonce, key->key_nonce, WPA_NONCE_LEN);
854 return;
855
856failed:
857 wpa_sm_deauthenticate(sm, WLAN_REASON_UNSPECIFIED);
858}
859
860
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700861static void wpa_supplicant_process_1_of_4(struct wpa_sm *sm,
862 const unsigned char *src_addr,
863 const struct wpa_eapol_key *key,
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700864 u16 ver, const u8 *key_data,
Sunil8cd6f4d2022-06-28 18:40:46 +0000865 size_t key_data_len,
866 enum frame_encryption encrypted)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700867{
868 struct wpa_eapol_ie_parse ie;
869 struct wpa_ptk *ptk;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700870 int res;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800871 u8 *kde, *kde_buf = NULL;
872 size_t kde_len;
Sunil Ravi77d572f2023-01-17 23:58:31 +0000873 size_t mlo_kde_len = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700874
Sunil8cd6f4d2022-06-28 18:40:46 +0000875 if (encrypted == FRAME_NOT_ENCRYPTED && sm->tk_set &&
876 wpa_sm_pmf_enabled(sm)) {
877 wpa_printf(MSG_DEBUG,
878 "RSN: Discard unencrypted EAPOL-Key msg 1/4 when TK is set and PMF is enabled");
879 return;
880 }
881
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700882 if (wpa_sm_get_network_ctx(sm) == NULL) {
883 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, "WPA: No SSID info "
884 "found (msg 1 of 4)");
885 return;
886 }
887
Hai Shalomfdcde762020-04-02 11:19:20 -0700888 if (sm->wpa_deny_ptk0_rekey && !sm->use_ext_key_id &&
889 wpa_sm_get_state(sm) == WPA_COMPLETED) {
890 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
891 "WPA: PTK0 rekey not allowed, reconnecting");
892 wpa_sm_reconnect(sm);
893 return;
894 }
895
Roshan Pius5e7db942018-04-12 12:27:41 -0700896 wpa_dbg(sm->ctx->msg_ctx, MSG_INFO, "WPA: RX message 1 of 4-Way "
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700897 "Handshake from " MACSTR " (ver=%d)", MAC2STR(src_addr), ver);
898
899 os_memset(&ie, 0, sizeof(ie));
900
Sunil Ravi77d572f2023-01-17 23:58:31 +0000901 /* RSN: msg 1/4 should contain PMKID for the selected PMK */
902 wpa_hexdump(MSG_DEBUG, "RSN: msg 1/4 key data", key_data, key_data_len);
903 if (wpa_supplicant_parse_ies(key_data, key_data_len, &ie) < 0) {
904 wpa_printf(MSG_DEBUG,
905 "RSN: Discard EAPOL-Key msg 1/4 with invalid IEs/KDEs");
906 return;
907 }
908 if (ie.pmkid) {
909 wpa_hexdump(MSG_DEBUG, "RSN: PMKID from Authenticator",
910 ie.pmkid, PMKID_LEN);
911 }
912
913 if (sm->mlo.valid_links && !is_valid_ap_mld_mac_kde(sm, ie.mac_addr)) {
914 wpa_printf(MSG_INFO,
915 "RSN: Discard EAPOL-Key msg 1/4 with invalid AP MLD MAC address KDE");
916 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700917 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700918
919 res = wpa_supplicant_get_pmk(sm, src_addr, ie.pmkid);
920 if (res == -2) {
921 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "RSN: Do not reply to "
922 "msg 1/4 - requesting full EAP authentication");
923 return;
924 }
925 if (res)
926 goto failed;
927
Sunil8cd6f4d2022-06-28 18:40:46 +0000928 wpa_sm_set_state(sm, WPA_4WAY_HANDSHAKE);
929
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700930 if (sm->renew_snonce) {
931 if (random_get_bytes(sm->snonce, WPA_NONCE_LEN)) {
932 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
933 "WPA: Failed to get random data for SNonce");
934 goto failed;
935 }
936 sm->renew_snonce = 0;
937 wpa_hexdump(MSG_DEBUG, "WPA: Renewed SNonce",
938 sm->snonce, WPA_NONCE_LEN);
939 }
940
941 /* Calculate PTK which will be stored as a temporary PTK until it has
942 * been verified when processing message 3/4. */
943 ptk = &sm->tptk;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700944 if (wpa_derive_ptk(sm, src_addr, key, ptk) < 0)
945 goto failed;
Sunil Ravi77d572f2023-01-17 23:58:31 +0000946 if (sm->pairwise_cipher == WPA_CIPHER_TKIP)
947 wpas_swap_tkip_mic_keys(ptk);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700948 sm->tptk_set = 1;
949
Sunil Ravi77d572f2023-01-17 23:58:31 +0000950 /* Add MLO Link KDE and MAC KDE in M2 for ML connection */
951 if (sm->mlo.valid_links)
952 mlo_kde_len = wpa_mlo_link_kde_len(sm) +
953 RSN_SELECTOR_LEN + ETH_ALEN + 2;
954
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800955 kde = sm->assoc_wpa_ie;
956 kde_len = sm->assoc_wpa_ie_len;
Hai Shalomc3565922019-10-28 11:58:20 -0700957 kde_buf = os_malloc(kde_len +
958 2 + RSN_SELECTOR_LEN + 3 +
959 sm->assoc_rsnxe_len +
Hai Shalom4fbc08f2020-05-18 12:37:00 -0700960 2 + RSN_SELECTOR_LEN + 1 +
Sunil Ravi77d572f2023-01-17 23:58:31 +0000961 2 + RSN_SELECTOR_LEN + 2 + mlo_kde_len);
962
Hai Shalomc3565922019-10-28 11:58:20 -0700963 if (!kde_buf)
964 goto failed;
965 os_memcpy(kde_buf, kde, kde_len);
966 kde = kde_buf;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800967
Hai Shalom74f70d42019-02-11 14:42:39 -0800968#ifdef CONFIG_OCV
969 if (wpa_sm_ocv_enabled(sm)) {
970 struct wpa_channel_info ci;
971 u8 *pos;
972
Hai Shalomc3565922019-10-28 11:58:20 -0700973 pos = kde + kde_len;
Hai Shalom74f70d42019-02-11 14:42:39 -0800974 if (wpa_sm_channel_info(sm, &ci) != 0) {
975 wpa_printf(MSG_WARNING,
976 "Failed to get channel info for OCI element in EAPOL-Key 2/4");
977 goto failed;
978 }
Hai Shalom899fcc72020-10-19 14:38:18 -0700979#ifdef CONFIG_TESTING_OPTIONS
980 if (sm->oci_freq_override_eapol) {
981 wpa_printf(MSG_INFO,
982 "TEST: Override OCI KDE frequency %d -> %d MHz",
983 ci.frequency, sm->oci_freq_override_eapol);
984 ci.frequency = sm->oci_freq_override_eapol;
985 }
986#endif /* CONFIG_TESTING_OPTIONS */
Hai Shalom74f70d42019-02-11 14:42:39 -0800987
Hai Shalom74f70d42019-02-11 14:42:39 -0800988 if (ocv_insert_oci_kde(&ci, &pos) < 0)
989 goto failed;
990 kde_len = pos - kde;
991 }
992#endif /* CONFIG_OCV */
993
Hai Shalomc3565922019-10-28 11:58:20 -0700994 if (sm->assoc_rsnxe && sm->assoc_rsnxe_len) {
995 os_memcpy(kde + kde_len, sm->assoc_rsnxe, sm->assoc_rsnxe_len);
996 kde_len += sm->assoc_rsnxe_len;
997 }
998
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800999#ifdef CONFIG_P2P
1000 if (sm->p2p) {
Hai Shalomc3565922019-10-28 11:58:20 -07001001 u8 *pos;
1002
1003 wpa_printf(MSG_DEBUG,
1004 "P2P: Add IP Address Request KDE into EAPOL-Key 2/4");
1005 pos = kde + kde_len;
1006 *pos++ = WLAN_EID_VENDOR_SPECIFIC;
1007 *pos++ = RSN_SELECTOR_LEN + 1;
1008 RSN_SELECTOR_PUT(pos, WFA_KEY_DATA_IP_ADDR_REQ);
1009 pos += RSN_SELECTOR_LEN;
1010 *pos++ = 0x01;
1011 kde_len = pos - kde;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001012 }
1013#endif /* CONFIG_P2P */
1014
Hai Shalom4fbc08f2020-05-18 12:37:00 -07001015#ifdef CONFIG_DPP2
1016 if (DPP_VERSION > 1 && sm->key_mgmt == WPA_KEY_MGMT_DPP) {
1017 u8 *pos;
1018
1019 wpa_printf(MSG_DEBUG, "DPP: Add DPP KDE into EAPOL-Key 2/4");
1020 pos = kde + kde_len;
1021 *pos++ = WLAN_EID_VENDOR_SPECIFIC;
1022 *pos++ = RSN_SELECTOR_LEN + 2;
1023 RSN_SELECTOR_PUT(pos, WFA_KEY_DATA_DPP);
1024 pos += RSN_SELECTOR_LEN;
1025 *pos++ = DPP_VERSION; /* Protocol Version */
1026 *pos = 0; /* Flags */
1027 if (sm->dpp_pfs == 0)
1028 *pos |= DPP_KDE_PFS_ALLOWED;
1029 else if (sm->dpp_pfs == 1)
1030 *pos |= DPP_KDE_PFS_ALLOWED | DPP_KDE_PFS_REQUIRED;
1031 pos++;
1032 kde_len = pos - kde;
1033 }
1034#endif /* CONFIG_DPP2 */
1035
Sunil Ravi77d572f2023-01-17 23:58:31 +00001036 if (sm->mlo.valid_links) {
1037 u8 *pos;
1038
1039 /* Add MAC KDE */
1040 wpa_printf(MSG_DEBUG, "MLO: Add MAC KDE into EAPOL-Key 2/4");
1041 pos = kde + kde_len;
1042 pos = rsn_add_kde(pos, RSN_KEY_DATA_MAC_ADDR, sm->own_addr,
1043 ETH_ALEN);
1044
1045 /* Add MLO Link KDE */
1046 wpa_printf(MSG_DEBUG, "Add MLO Link KDE(s) into EAPOL-Key 2/4");
1047 pos = wpa_mlo_link_kde(sm, pos);
1048 kde_len = pos - kde;
1049 }
1050
1051 if (wpa_supplicant_send_2_of_4(sm, wpa_sm_get_auth_addr(sm), key, ver,
1052 sm->snonce, kde, kde_len, ptk) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001053 goto failed;
1054
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001055 os_free(kde_buf);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001056 os_memcpy(sm->anonce, key->key_nonce, WPA_NONCE_LEN);
1057 return;
1058
1059failed:
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001060 os_free(kde_buf);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001061 wpa_sm_deauthenticate(sm, WLAN_REASON_UNSPECIFIED);
1062}
1063
1064
1065static void wpa_sm_start_preauth(void *eloop_ctx, void *timeout_ctx)
1066{
1067 struct wpa_sm *sm = eloop_ctx;
1068 rsn_preauth_candidate_process(sm);
1069}
1070
1071
1072static void wpa_supplicant_key_neg_complete(struct wpa_sm *sm,
1073 const u8 *addr, int secure)
1074{
1075 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1076 "WPA: Key negotiation completed with "
1077 MACSTR " [PTK=%s GTK=%s]", MAC2STR(addr),
1078 wpa_cipher_txt(sm->pairwise_cipher),
1079 wpa_cipher_txt(sm->group_cipher));
1080 wpa_sm_cancel_auth_timeout(sm);
1081 wpa_sm_set_state(sm, WPA_COMPLETED);
1082
1083 if (secure) {
1084 wpa_sm_mlme_setprotection(
1085 sm, addr, MLME_SETPROTECTION_PROTECT_TYPE_RX_TX,
1086 MLME_SETPROTECTION_KEY_TYPE_PAIRWISE);
Hai Shalome21d4e82020-04-29 16:34:06 -07001087 eapol_sm_notify_portValid(sm->eapol, true);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001088 if (wpa_key_mgmt_wpa_psk(sm->key_mgmt) ||
1089 sm->key_mgmt == WPA_KEY_MGMT_DPP ||
1090 sm->key_mgmt == WPA_KEY_MGMT_OWE)
Hai Shalome21d4e82020-04-29 16:34:06 -07001091 eapol_sm_notify_eap_success(sm->eapol, true);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001092 /*
1093 * Start preauthentication after a short wait to avoid a
1094 * possible race condition between the data receive and key
1095 * configuration after the 4-Way Handshake. This increases the
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001096 * likelihood of the first preauth EAPOL-Start frame getting to
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001097 * the target AP.
1098 */
Hai Shalom74f70d42019-02-11 14:42:39 -08001099 if (!dl_list_empty(&sm->pmksa_candidates))
1100 eloop_register_timeout(1, 0, wpa_sm_start_preauth,
1101 sm, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001102 }
1103
1104 if (sm->cur_pmksa && sm->cur_pmksa->opportunistic) {
1105 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1106 "RSN: Authenticator accepted "
1107 "opportunistic PMKSA entry - marking it valid");
1108 sm->cur_pmksa->opportunistic = 0;
1109 }
1110
1111#ifdef CONFIG_IEEE80211R
1112 if (wpa_key_mgmt_ft(sm->key_mgmt)) {
1113 /* Prepare for the next transition */
1114 wpa_ft_prepare_auth_request(sm, NULL);
1115 }
1116#endif /* CONFIG_IEEE80211R */
1117}
1118
1119
1120static void wpa_sm_rekey_ptk(void *eloop_ctx, void *timeout_ctx)
1121{
1122 struct wpa_sm *sm = eloop_ctx;
1123 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Request PTK rekeying");
1124 wpa_sm_key_request(sm, 0, 1);
1125}
1126
1127
1128static int wpa_supplicant_install_ptk(struct wpa_sm *sm,
Hai Shalomfdcde762020-04-02 11:19:20 -07001129 const struct wpa_eapol_key *key,
1130 enum key_flag key_flag)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001131{
1132 int keylen, rsclen;
1133 enum wpa_alg alg;
1134 const u8 *key_rsc;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001135
Mathy Vanhoefc66556c2017-09-29 04:22:51 +02001136 if (sm->ptk.installed) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001137 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1138 "WPA: Do not re-install same PTK to the driver");
1139 return 0;
1140 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001141
1142 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1143 "WPA: Installing PTK to the driver");
1144
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07001145 if (sm->pairwise_cipher == WPA_CIPHER_NONE) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001146 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Pairwise Cipher "
1147 "Suite: NONE - do not use pairwise keys");
1148 return 0;
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07001149 }
1150
1151 if (!wpa_cipher_valid_pairwise(sm->pairwise_cipher)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001152 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1153 "WPA: Unsupported pairwise cipher %d",
1154 sm->pairwise_cipher);
1155 return -1;
1156 }
1157
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07001158 alg = wpa_cipher_to_alg(sm->pairwise_cipher);
1159 keylen = wpa_cipher_key_len(sm->pairwise_cipher);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001160 if (keylen <= 0 || (unsigned int) keylen != sm->ptk.tk_len) {
1161 wpa_printf(MSG_DEBUG, "WPA: TK length mismatch: %d != %lu",
1162 keylen, (long unsigned int) sm->ptk.tk_len);
1163 return -1;
1164 }
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07001165 rsclen = wpa_cipher_rsc_len(sm->pairwise_cipher);
1166
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001167 if (sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001168 key_rsc = null_rsc;
1169 } else {
1170 key_rsc = key->key_rsc;
1171 wpa_hexdump(MSG_DEBUG, "WPA: RSC", key_rsc, rsclen);
1172 }
1173
Sunil Ravi77d572f2023-01-17 23:58:31 +00001174 if (wpa_sm_set_key(sm, -1, alg, wpa_sm_get_auth_addr(sm),
1175 sm->keyidx_active, 1, key_rsc, rsclen, sm->ptk.tk,
1176 keylen, KEY_FLAG_PAIRWISE | key_flag) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001177 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
Sunil Ravi77d572f2023-01-17 23:58:31 +00001178 "WPA: Failed to set PTK to the driver (alg=%d keylen=%d auth_addr="
Hai Shalomfdcde762020-04-02 11:19:20 -07001179 MACSTR " idx=%d key_flag=0x%x)",
Sunil Ravi77d572f2023-01-17 23:58:31 +00001180 alg, keylen, MAC2STR(wpa_sm_get_auth_addr(sm)),
Hai Shalomfdcde762020-04-02 11:19:20 -07001181 sm->keyidx_active, key_flag);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001182 return -1;
1183 }
1184
Sunil Ravi89eba102022-09-13 21:04:37 -07001185#ifdef CONFIG_PASN
1186 if (sm->secure_ltf &&
1187 ieee802_11_rsnx_capab(sm->ap_rsnxe, WLAN_RSNX_CAPAB_SECURE_LTF) &&
1188 wpa_sm_set_ltf_keyseed(sm, sm->own_addr, sm->bssid,
1189 sm->ptk.ltf_keyseed_len,
1190 sm->ptk.ltf_keyseed) < 0) {
1191 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1192 "WPA: Failed to set LTF keyseed to the driver (keylen=%zu bssid="
1193 MACSTR ")", sm->ptk.ltf_keyseed_len,
1194 MAC2STR(sm->bssid));
1195 return -1;
1196 }
1197#endif /* CONFIG_PASN */
1198
Hai Shalom60840252021-02-19 19:02:11 -08001199 wpa_sm_store_ptk(sm, sm->bssid, sm->pairwise_cipher,
1200 sm->dot11RSNAConfigPMKLifetime, &sm->ptk);
1201
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001202 /* TK is not needed anymore in supplicant */
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001203 os_memset(sm->ptk.tk, 0, WPA_TK_MAX_LEN);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001204 sm->ptk.tk_len = 0;
Mathy Vanhoefc66556c2017-09-29 04:22:51 +02001205 sm->ptk.installed = 1;
Sunil8cd6f4d2022-06-28 18:40:46 +00001206 sm->tk_set = true;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001207
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001208 if (sm->wpa_ptk_rekey) {
1209 eloop_cancel_timeout(wpa_sm_rekey_ptk, sm, NULL);
1210 eloop_register_timeout(sm->wpa_ptk_rekey, 0, wpa_sm_rekey_ptk,
1211 sm, NULL);
1212 }
Hai Shalomfdcde762020-04-02 11:19:20 -07001213 return 0;
1214}
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001215
Hai Shalomfdcde762020-04-02 11:19:20 -07001216
1217static int wpa_supplicant_activate_ptk(struct wpa_sm *sm)
1218{
1219 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
Sunil Ravi77d572f2023-01-17 23:58:31 +00001220 "WPA: Activate PTK (idx=%d auth_addr=" MACSTR ")",
1221 sm->keyidx_active, MAC2STR(wpa_sm_get_auth_addr(sm)));
Hai Shalomfdcde762020-04-02 11:19:20 -07001222
Sunil Ravi77d572f2023-01-17 23:58:31 +00001223 if (wpa_sm_set_key(sm, -1, 0, wpa_sm_get_auth_addr(sm),
1224 sm->keyidx_active, 0, NULL, 0, NULL, 0,
1225 KEY_FLAG_PAIRWISE_RX_TX_MODIFY) < 0) {
Hai Shalomfdcde762020-04-02 11:19:20 -07001226 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
Sunil Ravi77d572f2023-01-17 23:58:31 +00001227 "WPA: Failed to activate PTK for TX (idx=%d auth_addr="
1228 MACSTR ")", sm->keyidx_active,
1229 MAC2STR(wpa_sm_get_auth_addr(sm)));
Hai Shalomfdcde762020-04-02 11:19:20 -07001230 return -1;
1231 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001232 return 0;
1233}
1234
1235
1236static int wpa_supplicant_check_group_cipher(struct wpa_sm *sm,
1237 int group_cipher,
1238 int keylen, int maxkeylen,
1239 int *key_rsc_len,
1240 enum wpa_alg *alg)
1241{
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07001242 int klen;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001243
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07001244 *alg = wpa_cipher_to_alg(group_cipher);
1245 if (*alg == WPA_ALG_NONE) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001246 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1247 "WPA: Unsupported Group Cipher %d",
1248 group_cipher);
1249 return -1;
1250 }
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07001251 *key_rsc_len = wpa_cipher_rsc_len(group_cipher);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001252
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07001253 klen = wpa_cipher_key_len(group_cipher);
1254 if (keylen != klen || maxkeylen < klen) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001255 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1256 "WPA: Unsupported %s Group Cipher key length %d (%d)",
1257 wpa_cipher_txt(group_cipher), keylen, maxkeylen);
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07001258 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001259 }
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07001260 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001261}
1262
1263
1264struct wpa_gtk_data {
1265 enum wpa_alg alg;
1266 int tx, key_rsc_len, keyidx;
1267 u8 gtk[32];
1268 int gtk_len;
1269};
1270
1271
1272static int wpa_supplicant_install_gtk(struct wpa_sm *sm,
1273 const struct wpa_gtk_data *gd,
Jouni Malinen58c0e962017-10-01 12:12:24 +03001274 const u8 *key_rsc, int wnm_sleep)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001275{
1276 const u8 *_gtk = gd->gtk;
1277 u8 gtk_buf[32];
1278
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02001279 /* Detect possible key reinstallation */
Jouni Malinen58c0e962017-10-01 12:12:24 +03001280 if ((sm->gtk.gtk_len == (size_t) gd->gtk_len &&
1281 os_memcmp(sm->gtk.gtk, gd->gtk, sm->gtk.gtk_len) == 0) ||
1282 (sm->gtk_wnm_sleep.gtk_len == (size_t) gd->gtk_len &&
1283 os_memcmp(sm->gtk_wnm_sleep.gtk, gd->gtk,
1284 sm->gtk_wnm_sleep.gtk_len) == 0)) {
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02001285 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1286 "WPA: Not reinstalling already in-use GTK to the driver (keyidx=%d tx=%d len=%d)",
1287 gd->keyidx, gd->tx, gd->gtk_len);
1288 return 0;
1289 }
1290
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001291 wpa_hexdump_key(MSG_DEBUG, "WPA: Group Key", gd->gtk, gd->gtk_len);
1292 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1293 "WPA: Installing GTK to the driver (keyidx=%d tx=%d len=%d)",
1294 gd->keyidx, gd->tx, gd->gtk_len);
1295 wpa_hexdump(MSG_DEBUG, "WPA: RSC", key_rsc, gd->key_rsc_len);
1296 if (sm->group_cipher == WPA_CIPHER_TKIP) {
1297 /* Swap Tx/Rx keys for Michael MIC */
1298 os_memcpy(gtk_buf, gd->gtk, 16);
1299 os_memcpy(gtk_buf + 16, gd->gtk + 24, 8);
1300 os_memcpy(gtk_buf + 24, gd->gtk + 16, 8);
1301 _gtk = gtk_buf;
1302 }
1303 if (sm->pairwise_cipher == WPA_CIPHER_NONE) {
Sunil Ravi77d572f2023-01-17 23:58:31 +00001304 if (wpa_sm_set_key(sm, -1, gd->alg, NULL,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001305 gd->keyidx, 1, key_rsc, gd->key_rsc_len,
Hai Shalomfdcde762020-04-02 11:19:20 -07001306 _gtk, gd->gtk_len,
1307 KEY_FLAG_GROUP_RX_TX_DEFAULT) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001308 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1309 "WPA: Failed to set GTK to the driver "
1310 "(Group only)");
Hai Shalom81f62d82019-07-22 12:10:00 -07001311 forced_memzero(gtk_buf, sizeof(gtk_buf));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001312 return -1;
1313 }
Sunil Ravi77d572f2023-01-17 23:58:31 +00001314 } else if (wpa_sm_set_key(sm, -1, gd->alg, broadcast_ether_addr,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001315 gd->keyidx, gd->tx, key_rsc, gd->key_rsc_len,
Hai Shalomfdcde762020-04-02 11:19:20 -07001316 _gtk, gd->gtk_len, KEY_FLAG_GROUP_RX) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001317 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1318 "WPA: Failed to set GTK to "
1319 "the driver (alg=%d keylen=%d keyidx=%d)",
1320 gd->alg, gd->gtk_len, gd->keyidx);
Hai Shalom81f62d82019-07-22 12:10:00 -07001321 forced_memzero(gtk_buf, sizeof(gtk_buf));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001322 return -1;
1323 }
Hai Shalom81f62d82019-07-22 12:10:00 -07001324 forced_memzero(gtk_buf, sizeof(gtk_buf));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001325
Jouni Malinen58c0e962017-10-01 12:12:24 +03001326 if (wnm_sleep) {
1327 sm->gtk_wnm_sleep.gtk_len = gd->gtk_len;
1328 os_memcpy(sm->gtk_wnm_sleep.gtk, gd->gtk,
1329 sm->gtk_wnm_sleep.gtk_len);
1330 } else {
1331 sm->gtk.gtk_len = gd->gtk_len;
1332 os_memcpy(sm->gtk.gtk, gd->gtk, sm->gtk.gtk_len);
1333 }
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02001334
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001335 return 0;
1336}
1337
1338
Sunil Ravi77d572f2023-01-17 23:58:31 +00001339static int wpa_supplicant_install_mlo_gtk(struct wpa_sm *sm, u8 link_id,
1340 const struct wpa_gtk_data *gd,
1341 const u8 *key_rsc, int wnm_sleep)
1342{
1343 const u8 *gtk = gd->gtk;
1344
1345 /* Detect possible key reinstallation */
1346 if ((sm->mlo.links[link_id].gtk.gtk_len == (size_t) gd->gtk_len &&
1347 os_memcmp(sm->mlo.links[link_id].gtk.gtk, gd->gtk,
1348 sm->mlo.links[link_id].gtk.gtk_len) == 0) ||
1349 (sm->mlo.links[link_id].gtk_wnm_sleep.gtk_len ==
1350 (size_t) gd->gtk_len &&
1351 os_memcmp(sm->mlo.links[link_id].gtk_wnm_sleep.gtk, gd->gtk,
1352 sm->mlo.links[link_id].gtk_wnm_sleep.gtk_len) == 0)) {
1353 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1354 "RSN: Not reinstalling already in-use GTK to the driver (link_id=%d keyidx=%d tx=%d len=%d)",
1355 link_id, gd->keyidx, gd->tx, gd->gtk_len);
1356 return 0;
1357 }
1358
1359 wpa_hexdump_link_key(MSG_DEBUG, link_id, "RSN: Group Key", gd->gtk,
1360 gd->gtk_len);
1361 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1362 "RSN: Installing GTK to the driver (link_id=%d keyidx=%d tx=%d len=%d)",
1363 link_id, gd->keyidx, gd->tx, gd->gtk_len);
1364 wpa_hexdump_link(MSG_DEBUG, link_id, "RSN: RSC",
1365 key_rsc, gd->key_rsc_len);
1366 if (wpa_sm_set_key(sm, link_id, gd->alg, broadcast_ether_addr,
1367 gd->keyidx, gd->tx, key_rsc, gd->key_rsc_len, gtk,
1368 gd->gtk_len, KEY_FLAG_GROUP_RX) < 0) {
1369 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1370 "RSN: Failed to set GTK to the driver (link_id=%d alg=%d keylen=%d keyidx=%d)",
1371 link_id, gd->alg, gd->gtk_len, gd->keyidx);
1372 return -1;
1373 }
1374
1375 if (wnm_sleep) {
1376 sm->mlo.links[link_id].gtk_wnm_sleep.gtk_len = gd->gtk_len;
1377 os_memcpy(sm->mlo.links[link_id].gtk_wnm_sleep.gtk, gd->gtk,
1378 sm->mlo.links[link_id].gtk_wnm_sleep.gtk_len);
1379 } else {
1380 sm->mlo.links[link_id].gtk.gtk_len = gd->gtk_len;
1381 os_memcpy(sm->mlo.links[link_id].gtk.gtk, gd->gtk,
1382 sm->mlo.links[link_id].gtk.gtk_len);
1383 }
1384
1385 return 0;
1386}
1387
1388
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001389static int wpa_supplicant_gtk_tx_bit_workaround(const struct wpa_sm *sm,
1390 int tx)
1391{
1392 if (tx && sm->pairwise_cipher != WPA_CIPHER_NONE) {
1393 /* Ignore Tx bit for GTK if a pairwise key is used. One AP
1394 * seemed to set this bit (incorrectly, since Tx is only when
1395 * doing Group Key only APs) and without this workaround, the
1396 * data connection does not work because wpa_supplicant
1397 * configured non-zero keyidx to be used for unicast. */
1398 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1399 "WPA: Tx bit set for GTK, but pairwise "
1400 "keys are used - ignore Tx bit");
1401 return 0;
1402 }
1403 return tx;
1404}
1405
1406
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001407static int wpa_supplicant_rsc_relaxation(const struct wpa_sm *sm,
1408 const u8 *rsc)
1409{
1410 int rsclen;
1411
1412 if (!sm->wpa_rsc_relaxation)
1413 return 0;
1414
1415 rsclen = wpa_cipher_rsc_len(sm->group_cipher);
1416
1417 /*
1418 * Try to detect RSC (endian) corruption issue where the AP sends
1419 * the RSC bytes in EAPOL-Key message in the wrong order, both if
1420 * it's actually a 6-byte field (as it should be) and if it treats
1421 * it as an 8-byte field.
1422 * An AP model known to have this bug is the Sapido RB-1632.
1423 */
1424 if (rsclen == 6 && ((rsc[5] && !rsc[0]) || rsc[6] || rsc[7])) {
1425 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1426 "RSC %02x%02x%02x%02x%02x%02x%02x%02x is likely bogus, using 0",
1427 rsc[0], rsc[1], rsc[2], rsc[3],
1428 rsc[4], rsc[5], rsc[6], rsc[7]);
1429
1430 return 1;
1431 }
1432
1433 return 0;
1434}
1435
1436
Sunil Ravi77d572f2023-01-17 23:58:31 +00001437static int wpa_supplicant_mlo_gtk(struct wpa_sm *sm, u8 link_id, const u8 *gtk,
1438 size_t gtk_len, int key_info)
1439{
1440 struct wpa_gtk_data gd;
1441 const u8 *key_rsc;
1442 int ret;
1443
1444 /*
1445 * MLO GTK KDE format:
1446 * KeyID[bits 0-1], Tx [bit 2], Reserved [bit 3], link id [4-7]
1447 * PN
1448 * GTK
1449 */
1450 os_memset(&gd, 0, sizeof(gd));
1451 wpa_hexdump_link_key(MSG_DEBUG, link_id,
1452 "RSN: received GTK in pairwise handshake",
1453 gtk, gtk_len);
1454
1455 if (gtk_len < RSN_MLO_GTK_KDE_PREFIX_LENGTH ||
1456 gtk_len - RSN_MLO_GTK_KDE_PREFIX_LENGTH > sizeof(gd.gtk))
1457 return -1;
1458
1459 gd.keyidx = gtk[0] & 0x3;
1460 gtk += 1;
1461 gtk_len -= 1;
1462
1463 key_rsc = gtk;
1464
1465 gtk += 6;
1466 gtk_len -= 6;
1467
1468 os_memcpy(gd.gtk, gtk, gtk_len);
1469 gd.gtk_len = gtk_len;
1470
1471 ret = 0;
1472 if (wpa_supplicant_check_group_cipher(sm, sm->group_cipher, gtk_len,
1473 gtk_len, &gd.key_rsc_len,
1474 &gd.alg) ||
1475 wpa_supplicant_install_mlo_gtk(sm, link_id, &gd, key_rsc, 0)) {
1476 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1477 "RSN: Failed to install GTK for MLO Link ID %u",
1478 link_id);
1479 ret = -1;
1480 goto out;
1481 }
1482
1483out:
1484 forced_memzero(&gd, sizeof(gd));
1485 return ret;
1486}
1487
1488
1489static int wpa_supplicant_pairwise_mlo_gtk(struct wpa_sm *sm,
1490 const struct wpa_eapol_key *key,
1491 struct wpa_eapol_ie_parse *ie,
1492 int key_info)
1493{
1494 u8 i;
1495
1496 for (i = 0; i < MAX_NUM_MLO_LINKS; i++) {
1497 if (!(sm->mlo.valid_links & BIT(i)))
1498 continue;
1499
1500 if (!ie->mlo_gtk[i]) {
1501 wpa_msg(sm->ctx->msg_ctx, MSG_ERROR,
1502 "MLO RSN: GTK not found for link ID %u", i);
1503 return -1;
1504 }
1505
1506 if (wpa_supplicant_mlo_gtk(sm, i, ie->mlo_gtk[i],
1507 ie->mlo_gtk_len[i], key_info))
1508 return -1;
1509 }
1510
1511 return 0;
1512}
1513
1514
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001515static int wpa_supplicant_pairwise_gtk(struct wpa_sm *sm,
1516 const struct wpa_eapol_key *key,
1517 const u8 *gtk, size_t gtk_len,
1518 int key_info)
1519{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001520 struct wpa_gtk_data gd;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001521 const u8 *key_rsc;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001522
1523 /*
1524 * IEEE Std 802.11i-2004 - 8.5.2 EAPOL-Key frames - Figure 43x
1525 * GTK KDE format:
1526 * KeyID[bits 0-1], Tx [bit 2], Reserved [bits 3-7]
1527 * Reserved [bits 0-7]
1528 * GTK
1529 */
1530
1531 os_memset(&gd, 0, sizeof(gd));
1532 wpa_hexdump_key(MSG_DEBUG, "RSN: received GTK in pairwise handshake",
1533 gtk, gtk_len);
1534
1535 if (gtk_len < 2 || gtk_len - 2 > sizeof(gd.gtk))
1536 return -1;
1537
1538 gd.keyidx = gtk[0] & 0x3;
1539 gd.tx = wpa_supplicant_gtk_tx_bit_workaround(sm,
1540 !!(gtk[0] & BIT(2)));
1541 gtk += 2;
1542 gtk_len -= 2;
1543
1544 os_memcpy(gd.gtk, gtk, gtk_len);
1545 gd.gtk_len = gtk_len;
1546
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001547 key_rsc = key->key_rsc;
1548 if (wpa_supplicant_rsc_relaxation(sm, key->key_rsc))
1549 key_rsc = null_rsc;
1550
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001551 if (sm->group_cipher != WPA_CIPHER_GTK_NOT_USED &&
1552 (wpa_supplicant_check_group_cipher(sm, sm->group_cipher,
1553 gtk_len, gtk_len,
1554 &gd.key_rsc_len, &gd.alg) ||
Jouni Malinen58c0e962017-10-01 12:12:24 +03001555 wpa_supplicant_install_gtk(sm, &gd, key_rsc, 0))) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001556 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1557 "RSN: Failed to install GTK");
Hai Shalom81f62d82019-07-22 12:10:00 -07001558 forced_memzero(&gd, sizeof(gd));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001559 return -1;
1560 }
Hai Shalom81f62d82019-07-22 12:10:00 -07001561 forced_memzero(&gd, sizeof(gd));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001562
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001563 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001564}
1565
1566
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02001567static int wpa_supplicant_install_igtk(struct wpa_sm *sm,
Jouni Malinen58c0e962017-10-01 12:12:24 +03001568 const struct wpa_igtk_kde *igtk,
1569 int wnm_sleep)
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02001570{
1571 size_t len = wpa_cipher_key_len(sm->mgmt_group_cipher);
1572 u16 keyidx = WPA_GET_LE16(igtk->keyid);
1573
1574 /* Detect possible key reinstallation */
Jouni Malinen58c0e962017-10-01 12:12:24 +03001575 if ((sm->igtk.igtk_len == len &&
1576 os_memcmp(sm->igtk.igtk, igtk->igtk, sm->igtk.igtk_len) == 0) ||
1577 (sm->igtk_wnm_sleep.igtk_len == len &&
1578 os_memcmp(sm->igtk_wnm_sleep.igtk, igtk->igtk,
1579 sm->igtk_wnm_sleep.igtk_len) == 0)) {
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02001580 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1581 "WPA: Not reinstalling already in-use IGTK to the driver (keyidx=%d)",
1582 keyidx);
1583 return 0;
1584 }
1585
1586 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
Hai Shalom39ba6fc2019-01-22 12:40:38 -08001587 "WPA: IGTK keyid %d pn " COMPACT_MACSTR,
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02001588 keyidx, MAC2STR(igtk->pn));
1589 wpa_hexdump_key(MSG_DEBUG, "WPA: IGTK", igtk->igtk, len);
1590 if (keyidx > 4095) {
1591 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1592 "WPA: Invalid IGTK KeyID %d", keyidx);
1593 return -1;
1594 }
Sunil Ravi77d572f2023-01-17 23:58:31 +00001595 if (wpa_sm_set_key(sm, -1, wpa_cipher_to_alg(sm->mgmt_group_cipher),
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02001596 broadcast_ether_addr,
1597 keyidx, 0, igtk->pn, sizeof(igtk->pn),
Hai Shalomfdcde762020-04-02 11:19:20 -07001598 igtk->igtk, len, KEY_FLAG_GROUP_RX) < 0) {
Hai Shalom5f92bc92019-04-18 11:54:11 -07001599 if (keyidx == 0x0400 || keyidx == 0x0500) {
1600 /* Assume the AP has broken PMF implementation since it
1601 * seems to have swapped the KeyID bytes. The AP cannot
1602 * be trusted to implement BIP correctly or provide a
1603 * valid IGTK, so do not try to configure this key with
1604 * swapped KeyID bytes. Instead, continue without
1605 * configuring the IGTK so that the driver can drop any
1606 * received group-addressed robust management frames due
1607 * to missing keys.
1608 *
1609 * Normally, this error behavior would result in us
1610 * disconnecting, but there are number of deployed APs
1611 * with this broken behavior, so as an interoperability
1612 * workaround, allow the connection to proceed. */
1613 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1614 "WPA: Ignore IGTK configuration error due to invalid IGTK KeyID byte order");
1615 } else {
1616 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1617 "WPA: Failed to configure IGTK to the driver");
1618 return -1;
1619 }
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02001620 }
1621
Jouni Malinen58c0e962017-10-01 12:12:24 +03001622 if (wnm_sleep) {
1623 sm->igtk_wnm_sleep.igtk_len = len;
1624 os_memcpy(sm->igtk_wnm_sleep.igtk, igtk->igtk,
1625 sm->igtk_wnm_sleep.igtk_len);
1626 } else {
1627 sm->igtk.igtk_len = len;
1628 os_memcpy(sm->igtk.igtk, igtk->igtk, sm->igtk.igtk_len);
1629 }
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02001630
1631 return 0;
1632}
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02001633
1634
Hai Shalomfdcde762020-04-02 11:19:20 -07001635static int wpa_supplicant_install_bigtk(struct wpa_sm *sm,
1636 const struct wpa_bigtk_kde *bigtk,
1637 int wnm_sleep)
1638{
1639 size_t len = wpa_cipher_key_len(sm->mgmt_group_cipher);
1640 u16 keyidx = WPA_GET_LE16(bigtk->keyid);
1641
1642 /* Detect possible key reinstallation */
1643 if ((sm->bigtk.bigtk_len == len &&
1644 os_memcmp(sm->bigtk.bigtk, bigtk->bigtk,
1645 sm->bigtk.bigtk_len) == 0) ||
1646 (sm->bigtk_wnm_sleep.bigtk_len == len &&
1647 os_memcmp(sm->bigtk_wnm_sleep.bigtk, bigtk->bigtk,
1648 sm->bigtk_wnm_sleep.bigtk_len) == 0)) {
1649 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1650 "WPA: Not reinstalling already in-use BIGTK to the driver (keyidx=%d)",
1651 keyidx);
1652 return 0;
1653 }
1654
1655 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1656 "WPA: BIGTK keyid %d pn " COMPACT_MACSTR,
1657 keyidx, MAC2STR(bigtk->pn));
1658 wpa_hexdump_key(MSG_DEBUG, "WPA: BIGTK", bigtk->bigtk, len);
1659 if (keyidx < 6 || keyidx > 7) {
1660 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1661 "WPA: Invalid BIGTK KeyID %d", keyidx);
1662 return -1;
1663 }
Sunil Ravi77d572f2023-01-17 23:58:31 +00001664 if (wpa_sm_set_key(sm, -1, wpa_cipher_to_alg(sm->mgmt_group_cipher),
Hai Shalomfdcde762020-04-02 11:19:20 -07001665 broadcast_ether_addr,
1666 keyidx, 0, bigtk->pn, sizeof(bigtk->pn),
1667 bigtk->bigtk, len, KEY_FLAG_GROUP_RX) < 0) {
1668 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1669 "WPA: Failed to configure BIGTK to the driver");
1670 return -1;
1671 }
1672
1673 if (wnm_sleep) {
1674 sm->bigtk_wnm_sleep.bigtk_len = len;
1675 os_memcpy(sm->bigtk_wnm_sleep.bigtk, bigtk->bigtk,
1676 sm->bigtk_wnm_sleep.bigtk_len);
1677 } else {
1678 sm->bigtk.bigtk_len = len;
1679 os_memcpy(sm->bigtk.bigtk, bigtk->bigtk, sm->bigtk.bigtk_len);
1680 }
1681
1682 return 0;
1683}
1684
1685
Sunil Ravi77d572f2023-01-17 23:58:31 +00001686static int wpa_supplicant_install_mlo_igtk(struct wpa_sm *sm, u8 link_id,
1687 const struct rsn_mlo_igtk_kde *igtk,
1688 int wnm_sleep)
1689{
1690 size_t len = wpa_cipher_key_len(sm->mgmt_group_cipher);
1691 u16 keyidx = WPA_GET_LE16(igtk->keyid);
1692
1693 /* Detect possible key reinstallation */
1694 if ((sm->mlo.links[link_id].igtk.igtk_len == len &&
1695 os_memcmp(sm->mlo.links[link_id].igtk.igtk, igtk->igtk,
1696 sm->mlo.links[link_id].igtk.igtk_len) == 0) ||
1697 (sm->mlo.links[link_id].igtk_wnm_sleep.igtk_len == len &&
1698 os_memcmp(sm->mlo.links[link_id].igtk_wnm_sleep.igtk, igtk->igtk,
1699 sm->mlo.links[link_id].igtk_wnm_sleep.igtk_len) == 0)) {
1700 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1701 "RSN: Not reinstalling already in-use IGTK to the driver (link_id=%d keyidx=%d)",
1702 link_id, keyidx);
1703 return 0;
1704 }
1705
1706 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1707 "RSN: MLO Link %u IGTK keyid %d pn " COMPACT_MACSTR,
1708 link_id, keyidx, MAC2STR(igtk->pn));
1709 wpa_hexdump_link_key(MSG_DEBUG, link_id, "RSN: IGTK", igtk->igtk, len);
1710 if (keyidx > 4095) {
1711 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1712 "RSN: Invalid MLO Link %d IGTK KeyID %d", link_id,
1713 keyidx);
1714 return -1;
1715 }
1716 if (wpa_sm_set_key(sm, link_id,
1717 wpa_cipher_to_alg(sm->mgmt_group_cipher),
1718 broadcast_ether_addr, keyidx, 0, igtk->pn,
1719 sizeof(igtk->pn), igtk->igtk, len,
1720 KEY_FLAG_GROUP_RX) < 0) {
1721 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1722 "RSN: Failed to configure MLO Link %d IGTK to the driver",
1723 link_id);
1724 return -1;
1725 }
1726
1727 if (wnm_sleep) {
1728 sm->mlo.links[link_id].igtk_wnm_sleep.igtk_len = len;
1729 os_memcpy(sm->mlo.links[link_id].igtk_wnm_sleep.igtk,
1730 igtk->igtk,
1731 sm->mlo.links[link_id].igtk_wnm_sleep.igtk_len);
1732 } else {
1733 sm->mlo.links[link_id].igtk.igtk_len = len;
1734 os_memcpy(sm->mlo.links[link_id].igtk.igtk, igtk->igtk,
1735 sm->mlo.links[link_id].igtk.igtk_len);
1736 }
1737
1738 return 0;
1739}
1740
1741
1742static int
1743wpa_supplicant_install_mlo_bigtk(struct wpa_sm *sm, u8 link_id,
1744 const struct rsn_mlo_bigtk_kde *bigtk,
1745 int wnm_sleep)
1746{
1747 size_t len = wpa_cipher_key_len(sm->mgmt_group_cipher);
1748 u16 keyidx = WPA_GET_LE16(bigtk->keyid);
1749
1750 /* Detect possible key reinstallation */
1751 if ((sm->mlo.links[link_id].bigtk.bigtk_len == len &&
1752 os_memcmp(sm->mlo.links[link_id].bigtk.bigtk, bigtk->bigtk,
1753 sm->mlo.links[link_id].bigtk.bigtk_len) == 0) ||
1754 (sm->mlo.links[link_id].bigtk_wnm_sleep.bigtk_len == len &&
1755 os_memcmp(sm->mlo.links[link_id].bigtk_wnm_sleep.bigtk,
1756 bigtk->bigtk,
1757 sm->mlo.links[link_id].bigtk_wnm_sleep.bigtk_len) ==
1758 0)) {
1759 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1760 "RSN: Not reinstalling already in-use BIGTK to the driver (link_id=%d keyidx=%d)",
1761 link_id, keyidx);
1762 return 0;
1763 }
1764
1765 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1766 "RSN: MLO Link %u BIGTK keyid %d pn " COMPACT_MACSTR,
1767 link_id, keyidx, MAC2STR(bigtk->pn));
1768 wpa_hexdump_link_key(MSG_DEBUG, link_id, "RSN: BIGTK", bigtk->bigtk,
1769 len);
1770 if (keyidx < 6 || keyidx > 7) {
1771 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1772 "RSN: Invalid MLO Link %d BIGTK KeyID %d", link_id,
1773 keyidx);
1774 return -1;
1775 }
1776 if (wpa_sm_set_key(sm, link_id,
1777 wpa_cipher_to_alg(sm->mgmt_group_cipher),
1778 broadcast_ether_addr, keyidx, 0, bigtk->pn,
1779 sizeof(bigtk->pn), bigtk->bigtk, len,
1780 KEY_FLAG_GROUP_RX) < 0) {
1781 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1782 "RSN: Failed to configure MLO Link %d BIGTK to the driver",
1783 link_id);
1784 return -1;
1785 }
1786
1787 if (wnm_sleep) {
1788 sm->mlo.links[link_id].bigtk_wnm_sleep.bigtk_len = len;
1789 os_memcpy(sm->mlo.links[link_id].bigtk_wnm_sleep.bigtk,
1790 bigtk->bigtk,
1791 sm->mlo.links[link_id].bigtk_wnm_sleep.bigtk_len);
1792 } else {
1793 sm->mlo.links[link_id].bigtk.bigtk_len = len;
1794 os_memcpy(sm->mlo.links[link_id].bigtk.bigtk, bigtk->bigtk,
1795 sm->mlo.links[link_id].bigtk.bigtk_len);
1796 }
1797
1798 return 0;
1799}
1800
1801
1802static int _mlo_ieee80211w_set_keys(struct wpa_sm *sm, u8 link_id,
1803 struct wpa_eapol_ie_parse *ie)
1804{
1805 size_t len;
1806
1807 if (ie->mlo_igtk[link_id]) {
1808 len = wpa_cipher_key_len(sm->mgmt_group_cipher);
1809 if (ie->mlo_igtk_len[link_id] !=
1810 RSN_MLO_IGTK_KDE_PREFIX_LENGTH + len)
1811 return -1;
1812
1813 if (wpa_supplicant_install_mlo_igtk(
1814 sm, link_id,
1815 (const struct rsn_mlo_igtk_kde *)
1816 ie->mlo_igtk[link_id],
1817 0) < 0)
1818 return -1;
1819 }
1820
1821 if (ie->mlo_bigtk[link_id] && sm->beacon_prot) {
1822 len = wpa_cipher_key_len(sm->mgmt_group_cipher);
1823 if (ie->mlo_bigtk_len[link_id] !=
1824 RSN_MLO_BIGTK_KDE_PREFIX_LENGTH + len)
1825 return -1;
1826
1827 if (wpa_supplicant_install_mlo_bigtk(
1828 sm, link_id,
1829 (const struct rsn_mlo_bigtk_kde *)
1830 ie->mlo_bigtk[link_id],
1831 0) < 0)
1832 return -1;
1833 }
1834
1835 return 0;
1836}
1837
1838
1839static int mlo_ieee80211w_set_keys(struct wpa_sm *sm,
1840 struct wpa_eapol_ie_parse *ie)
1841{
1842 u8 i;
1843
1844 if (!wpa_cipher_valid_mgmt_group(sm->mgmt_group_cipher) ||
1845 sm->mgmt_group_cipher == WPA_CIPHER_GTK_NOT_USED)
1846 return 0;
1847
1848 for (i = 0; i < MAX_NUM_MLO_LINKS; i++) {
1849 if (!(sm->mlo.valid_links & BIT(i)))
1850 continue;
1851
1852 if (_mlo_ieee80211w_set_keys(sm, i, ie))
1853 return -1;
1854 }
1855
1856 return 0;
1857}
1858
1859
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001860static int ieee80211w_set_keys(struct wpa_sm *sm,
1861 struct wpa_eapol_ie_parse *ie)
1862{
Hai Shalomfdcde762020-04-02 11:19:20 -07001863 size_t len;
1864
Hai Shalom60840252021-02-19 19:02:11 -08001865 if (!wpa_cipher_valid_mgmt_group(sm->mgmt_group_cipher) ||
1866 sm->mgmt_group_cipher == WPA_CIPHER_GTK_NOT_USED)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001867 return 0;
1868
1869 if (ie->igtk) {
1870 const struct wpa_igtk_kde *igtk;
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02001871
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001872 len = wpa_cipher_key_len(sm->mgmt_group_cipher);
1873 if (ie->igtk_len != WPA_IGTK_KDE_PREFIX_LEN + len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001874 return -1;
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02001875
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001876 igtk = (const struct wpa_igtk_kde *) ie->igtk;
Jouni Malinen58c0e962017-10-01 12:12:24 +03001877 if (wpa_supplicant_install_igtk(sm, igtk, 0) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001878 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001879 }
1880
Hai Shalomfdcde762020-04-02 11:19:20 -07001881 if (ie->bigtk && sm->beacon_prot) {
1882 const struct wpa_bigtk_kde *bigtk;
1883
1884 len = wpa_cipher_key_len(sm->mgmt_group_cipher);
1885 if (ie->bigtk_len != WPA_BIGTK_KDE_PREFIX_LEN + len)
1886 return -1;
1887
1888 bigtk = (const struct wpa_bigtk_kde *) ie->bigtk;
1889 if (wpa_supplicant_install_bigtk(sm, bigtk, 0) < 0)
1890 return -1;
1891 }
1892
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001893 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001894}
1895
1896
1897static void wpa_report_ie_mismatch(struct wpa_sm *sm,
1898 const char *reason, const u8 *src_addr,
1899 const u8 *wpa_ie, size_t wpa_ie_len,
1900 const u8 *rsn_ie, size_t rsn_ie_len)
1901{
1902 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, "WPA: %s (src=" MACSTR ")",
1903 reason, MAC2STR(src_addr));
1904
1905 if (sm->ap_wpa_ie) {
1906 wpa_hexdump(MSG_INFO, "WPA: WPA IE in Beacon/ProbeResp",
1907 sm->ap_wpa_ie, sm->ap_wpa_ie_len);
1908 }
1909 if (wpa_ie) {
1910 if (!sm->ap_wpa_ie) {
1911 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1912 "WPA: No WPA IE in Beacon/ProbeResp");
1913 }
1914 wpa_hexdump(MSG_INFO, "WPA: WPA IE in 3/4 msg",
1915 wpa_ie, wpa_ie_len);
1916 }
1917
1918 if (sm->ap_rsn_ie) {
1919 wpa_hexdump(MSG_INFO, "WPA: RSN IE in Beacon/ProbeResp",
1920 sm->ap_rsn_ie, sm->ap_rsn_ie_len);
1921 }
1922 if (rsn_ie) {
1923 if (!sm->ap_rsn_ie) {
1924 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1925 "WPA: No RSN IE in Beacon/ProbeResp");
1926 }
1927 wpa_hexdump(MSG_INFO, "WPA: RSN IE in 3/4 msg",
1928 rsn_ie, rsn_ie_len);
1929 }
1930
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001931 wpa_sm_deauthenticate(sm, WLAN_REASON_IE_IN_4WAY_DIFFERS);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001932}
1933
1934
1935#ifdef CONFIG_IEEE80211R
1936
1937static int ft_validate_mdie(struct wpa_sm *sm,
1938 const unsigned char *src_addr,
1939 struct wpa_eapol_ie_parse *ie,
1940 const u8 *assoc_resp_mdie)
1941{
1942 struct rsn_mdie *mdie;
1943
1944 mdie = (struct rsn_mdie *) (ie->mdie + 2);
1945 if (ie->mdie == NULL || ie->mdie_len < 2 + sizeof(*mdie) ||
1946 os_memcmp(mdie->mobility_domain, sm->mobility_domain,
1947 MOBILITY_DOMAIN_ID_LEN) != 0) {
1948 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "FT: MDIE in msg 3/4 did "
1949 "not match with the current mobility domain");
1950 return -1;
1951 }
1952
1953 if (assoc_resp_mdie &&
1954 (assoc_resp_mdie[1] != ie->mdie[1] ||
1955 os_memcmp(assoc_resp_mdie, ie->mdie, 2 + ie->mdie[1]) != 0)) {
1956 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "FT: MDIE mismatch");
1957 wpa_hexdump(MSG_DEBUG, "FT: MDIE in EAPOL-Key msg 3/4",
1958 ie->mdie, 2 + ie->mdie[1]);
1959 wpa_hexdump(MSG_DEBUG, "FT: MDIE in (Re)Association Response",
1960 assoc_resp_mdie, 2 + assoc_resp_mdie[1]);
1961 return -1;
1962 }
1963
1964 return 0;
1965}
1966
1967
1968static int ft_validate_ftie(struct wpa_sm *sm,
1969 const unsigned char *src_addr,
1970 struct wpa_eapol_ie_parse *ie,
1971 const u8 *assoc_resp_ftie)
1972{
1973 if (ie->ftie == NULL) {
1974 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1975 "FT: No FTIE in EAPOL-Key msg 3/4");
1976 return -1;
1977 }
1978
1979 if (assoc_resp_ftie == NULL)
1980 return 0;
1981
1982 if (assoc_resp_ftie[1] != ie->ftie[1] ||
1983 os_memcmp(assoc_resp_ftie, ie->ftie, 2 + ie->ftie[1]) != 0) {
1984 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "FT: FTIE mismatch");
1985 wpa_hexdump(MSG_DEBUG, "FT: FTIE in EAPOL-Key msg 3/4",
1986 ie->ftie, 2 + ie->ftie[1]);
1987 wpa_hexdump(MSG_DEBUG, "FT: FTIE in (Re)Association Response",
1988 assoc_resp_ftie, 2 + assoc_resp_ftie[1]);
1989 return -1;
1990 }
1991
1992 return 0;
1993}
1994
1995
1996static int ft_validate_rsnie(struct wpa_sm *sm,
1997 const unsigned char *src_addr,
1998 struct wpa_eapol_ie_parse *ie)
1999{
2000 struct wpa_ie_data rsn;
2001
2002 if (!ie->rsn_ie)
2003 return 0;
2004
2005 /*
2006 * Verify that PMKR1Name from EAPOL-Key message 3/4
2007 * matches with the value we derived.
2008 */
2009 if (wpa_parse_wpa_ie_rsn(ie->rsn_ie, ie->rsn_ie_len, &rsn) < 0 ||
2010 rsn.num_pmkid != 1 || rsn.pmkid == NULL) {
2011 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "FT: No PMKR1Name in "
2012 "FT 4-way handshake message 3/4");
2013 return -1;
2014 }
2015
Dmitry Shmidtc2817022014-07-02 10:32:10 -07002016 if (os_memcmp_const(rsn.pmkid, sm->pmk_r1_name, WPA_PMK_NAME_LEN) != 0)
2017 {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002018 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2019 "FT: PMKR1Name mismatch in "
2020 "FT 4-way handshake message 3/4");
2021 wpa_hexdump(MSG_DEBUG, "FT: PMKR1Name from Authenticator",
2022 rsn.pmkid, WPA_PMK_NAME_LEN);
2023 wpa_hexdump(MSG_DEBUG, "FT: Derived PMKR1Name",
2024 sm->pmk_r1_name, WPA_PMK_NAME_LEN);
2025 return -1;
2026 }
2027
2028 return 0;
2029}
2030
2031
2032static int wpa_supplicant_validate_ie_ft(struct wpa_sm *sm,
2033 const unsigned char *src_addr,
2034 struct wpa_eapol_ie_parse *ie)
2035{
2036 const u8 *pos, *end, *mdie = NULL, *ftie = NULL;
2037
2038 if (sm->assoc_resp_ies) {
2039 pos = sm->assoc_resp_ies;
2040 end = pos + sm->assoc_resp_ies_len;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002041 while (end - pos > 2) {
2042 if (2 + pos[1] > end - pos)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002043 break;
2044 switch (*pos) {
2045 case WLAN_EID_MOBILITY_DOMAIN:
2046 mdie = pos;
2047 break;
2048 case WLAN_EID_FAST_BSS_TRANSITION:
2049 ftie = pos;
2050 break;
2051 }
2052 pos += 2 + pos[1];
2053 }
2054 }
2055
2056 if (ft_validate_mdie(sm, src_addr, ie, mdie) < 0 ||
2057 ft_validate_ftie(sm, src_addr, ie, ftie) < 0 ||
2058 ft_validate_rsnie(sm, src_addr, ie) < 0)
2059 return -1;
2060
2061 return 0;
2062}
2063
2064#endif /* CONFIG_IEEE80211R */
2065
2066
2067static int wpa_supplicant_validate_ie(struct wpa_sm *sm,
2068 const unsigned char *src_addr,
2069 struct wpa_eapol_ie_parse *ie)
2070{
2071 if (sm->ap_wpa_ie == NULL && sm->ap_rsn_ie == NULL) {
2072 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2073 "WPA: No WPA/RSN IE for this AP known. "
2074 "Trying to get from scan results");
2075 if (wpa_sm_get_beacon_ie(sm) < 0) {
2076 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
2077 "WPA: Could not find AP from "
2078 "the scan results");
Hai Shalomfdcde762020-04-02 11:19:20 -07002079 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002080 }
Hai Shalomfdcde762020-04-02 11:19:20 -07002081 wpa_msg(sm->ctx->msg_ctx, MSG_DEBUG,
2082 "WPA: Found the current AP from updated scan results");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002083 }
2084
2085 if (ie->wpa_ie == NULL && ie->rsn_ie == NULL &&
2086 (sm->ap_wpa_ie || sm->ap_rsn_ie)) {
2087 wpa_report_ie_mismatch(sm, "IE in 3/4 msg does not match "
2088 "with IE in Beacon/ProbeResp (no IE?)",
2089 src_addr, ie->wpa_ie, ie->wpa_ie_len,
2090 ie->rsn_ie, ie->rsn_ie_len);
2091 return -1;
2092 }
2093
2094 if ((ie->wpa_ie && sm->ap_wpa_ie &&
2095 (ie->wpa_ie_len != sm->ap_wpa_ie_len ||
2096 os_memcmp(ie->wpa_ie, sm->ap_wpa_ie, ie->wpa_ie_len) != 0)) ||
2097 (ie->rsn_ie && sm->ap_rsn_ie &&
2098 wpa_compare_rsn_ie(wpa_key_mgmt_ft(sm->key_mgmt),
2099 sm->ap_rsn_ie, sm->ap_rsn_ie_len,
2100 ie->rsn_ie, ie->rsn_ie_len))) {
2101 wpa_report_ie_mismatch(sm, "IE in 3/4 msg does not match "
2102 "with IE in Beacon/ProbeResp",
2103 src_addr, ie->wpa_ie, ie->wpa_ie_len,
2104 ie->rsn_ie, ie->rsn_ie_len);
2105 return -1;
2106 }
2107
2108 if (sm->proto == WPA_PROTO_WPA &&
2109 ie->rsn_ie && sm->ap_rsn_ie == NULL && sm->rsn_enabled) {
2110 wpa_report_ie_mismatch(sm, "Possible downgrade attack "
2111 "detected - RSN was enabled and RSN IE "
2112 "was in msg 3/4, but not in "
2113 "Beacon/ProbeResp",
2114 src_addr, ie->wpa_ie, ie->wpa_ie_len,
2115 ie->rsn_ie, ie->rsn_ie_len);
2116 return -1;
2117 }
2118
Hai Shalom60840252021-02-19 19:02:11 -08002119 if (sm->proto == WPA_PROTO_RSN &&
2120 ((sm->ap_rsnxe && !ie->rsnxe) ||
2121 (!sm->ap_rsnxe && ie->rsnxe) ||
2122 (sm->ap_rsnxe && ie->rsnxe &&
2123 (sm->ap_rsnxe_len != ie->rsnxe_len ||
2124 os_memcmp(sm->ap_rsnxe, ie->rsnxe, sm->ap_rsnxe_len) != 0)))) {
Hai Shalomc3565922019-10-28 11:58:20 -07002125 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2126 "WPA: RSNXE mismatch between Beacon/ProbeResp and EAPOL-Key msg 3/4");
Hai Shalomfdcde762020-04-02 11:19:20 -07002127 wpa_hexdump(MSG_INFO, "RSNXE in Beacon/ProbeResp",
2128 sm->ap_rsnxe, sm->ap_rsnxe_len);
2129 wpa_hexdump(MSG_INFO, "RSNXE in EAPOL-Key msg 3/4",
2130 ie->rsnxe, ie->rsnxe_len);
2131 wpa_sm_deauthenticate(sm, WLAN_REASON_IE_IN_4WAY_DIFFERS);
Hai Shalomc3565922019-10-28 11:58:20 -07002132 return -1;
2133 }
2134
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002135#ifdef CONFIG_IEEE80211R
2136 if (wpa_key_mgmt_ft(sm->key_mgmt) &&
2137 wpa_supplicant_validate_ie_ft(sm, src_addr, ie) < 0)
2138 return -1;
2139#endif /* CONFIG_IEEE80211R */
2140
2141 return 0;
2142}
2143
2144
2145/**
2146 * wpa_supplicant_send_4_of_4 - Send message 4 of WPA/RSN 4-Way Handshake
2147 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2148 * @dst: Destination address for the frame
2149 * @key: Pointer to the EAPOL-Key frame header
2150 * @ver: Version bits from EAPOL-Key Key Info
2151 * @key_info: Key Info
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002152 * @ptk: PTK to use for keyed hash and encryption
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002153 * Returns: >= 0 on success, < 0 on failure
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002154 */
2155int wpa_supplicant_send_4_of_4(struct wpa_sm *sm, const unsigned char *dst,
2156 const struct wpa_eapol_key *key,
2157 u16 ver, u16 key_info,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002158 struct wpa_ptk *ptk)
2159{
Dmitry Shmidt807291d2015-01-27 13:40:23 -08002160 size_t mic_len, hdrlen, rlen;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002161 struct wpa_eapol_key *reply;
Dmitry Shmidt807291d2015-01-27 13:40:23 -08002162 u8 *rbuf, *key_mic;
Sunil Ravi77d572f2023-01-17 23:58:31 +00002163 u8 *kde = NULL;
2164 size_t kde_len = 0;
2165
2166 if (sm->mlo.valid_links) {
2167 u8 *pos;
2168
2169 kde = os_malloc(RSN_SELECTOR_LEN + ETH_ALEN + 2);
2170 if (!kde)
2171 return -1;
2172
2173 /* Add MAC KDE */
2174 wpa_printf(MSG_DEBUG, "MLO: Add MAC KDE into EAPOL-Key 4/4");
2175 pos = kde;
2176 pos = rsn_add_kde(pos, RSN_KEY_DATA_MAC_ADDR, sm->own_addr,
2177 ETH_ALEN);
2178 kde_len = pos - kde;
2179 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002180
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002181 mic_len = wpa_mic_len(sm->key_mgmt, sm->pmk_len);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002182 hdrlen = sizeof(*reply) + mic_len + 2;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002183 rbuf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_KEY, NULL,
Sunil Ravi77d572f2023-01-17 23:58:31 +00002184 hdrlen + kde_len, &rlen, (void *) &reply);
2185 if (!rbuf) {
2186 os_free(kde);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002187 return -1;
Sunil Ravi77d572f2023-01-17 23:58:31 +00002188 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002189
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002190 reply->type = (sm->proto == WPA_PROTO_RSN ||
2191 sm->proto == WPA_PROTO_OSEN) ?
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002192 EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA;
2193 key_info &= WPA_KEY_INFO_SECURE;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002194 key_info |= ver | WPA_KEY_INFO_KEY_TYPE;
2195 if (mic_len)
2196 key_info |= WPA_KEY_INFO_MIC;
2197 else
2198 key_info |= WPA_KEY_INFO_ENCR_KEY_DATA;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002199 WPA_PUT_BE16(reply->key_info, key_info);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002200 if (sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002201 WPA_PUT_BE16(reply->key_length, 0);
2202 else
2203 os_memcpy(reply->key_length, key->key_length, 2);
2204 os_memcpy(reply->replay_counter, key->replay_counter,
2205 WPA_REPLAY_COUNTER_LEN);
2206
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002207 key_mic = (u8 *) (reply + 1);
Sunil Ravi77d572f2023-01-17 23:58:31 +00002208 WPA_PUT_BE16(key_mic + mic_len, kde_len); /* Key Data length */
2209 if (kde) {
2210 os_memcpy(key_mic + mic_len + 2, kde, kde_len); /* Key Data */
2211 os_free(kde);
2212 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002213
Roshan Pius5e7db942018-04-12 12:27:41 -07002214 wpa_dbg(sm->ctx->msg_ctx, MSG_INFO, "WPA: Sending EAPOL-Key 4/4");
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002215 return wpa_eapol_key_send(sm, ptk, ver, dst, ETH_P_EAPOL, rbuf, rlen,
2216 key_mic);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002217}
2218
2219
Sunil Ravi77d572f2023-01-17 23:58:31 +00002220static int wpa_supplicant_validate_link_kde(struct wpa_sm *sm, u8 link_id,
2221 const u8 *link_kde,
2222 size_t link_kde_len)
2223{
2224 size_t rsne_len = 0, rsnxe_len = 0;
2225 const u8 *rsne = NULL, *rsnxe = NULL;
2226
2227 if (!link_kde ||
2228 link_kde_len < RSN_MLO_LINK_KDE_LINK_MAC_INDEX + ETH_ALEN) {
2229 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2230 "RSN: MLO Link KDE is not found for link ID %d",
2231 link_id);
2232 return -1;
2233 }
2234
2235 if (os_memcmp(sm->mlo.links[link_id].bssid,
2236 &link_kde[RSN_MLO_LINK_KDE_LINK_MAC_INDEX],
2237 ETH_ALEN) != 0) {
2238 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2239 "RSN: MLO Link %u MAC address (" MACSTR
2240 ") not matching association response (" MACSTR ")",
2241 link_id,
2242 MAC2STR(&link_kde[RSN_MLO_LINK_KDE_LINK_MAC_INDEX]),
2243 MAC2STR(sm->mlo.links[link_id].bssid));
2244 return -1;
2245 }
2246
2247 if (link_kde[0] & RSN_MLO_LINK_KDE_LI_RSNE_INFO) {
2248 rsne = link_kde + RSN_MLO_LINK_KDE_FIXED_LENGTH;
2249 if (link_kde_len < RSN_MLO_LINK_KDE_FIXED_LENGTH + 2 ||
2250 link_kde_len <
2251 (size_t) (RSN_MLO_LINK_KDE_FIXED_LENGTH + 2 + rsne[1])) {
2252 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2253 "RSN: No room for link %u RSNE in MLO Link KDE",
2254 link_id);
2255 return -1;
2256 }
2257
2258 rsne_len = rsne[1] + 2;
2259 }
2260
2261 if (!rsne) {
2262 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2263 "RSN: RSNE not present in MLO Link %u KDE", link_id);
2264 return -1;
2265 }
2266
2267 if (link_kde[0] & RSN_MLO_LINK_KDE_LI_RSNXE_INFO) {
2268 rsnxe = link_kde + RSN_MLO_LINK_KDE_FIXED_LENGTH + rsne_len;
2269 if (link_kde_len <
2270 (RSN_MLO_LINK_KDE_FIXED_LENGTH + rsne_len + 2) ||
2271 link_kde_len <
2272 (RSN_MLO_LINK_KDE_FIXED_LENGTH + rsne_len + 2 + rsnxe[1])) {
2273 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2274 "RSN: No room for link %u RSNXE in MLO Link KDE",
2275 link_id);
2276 return -1;
2277 }
2278
2279 rsnxe_len = rsnxe[1] + 2;
2280 }
2281
2282 if (wpa_compare_rsn_ie(wpa_key_mgmt_ft(sm->key_mgmt),
2283 sm->mlo.links[link_id].ap_rsne,
2284 sm->mlo.links[link_id].ap_rsne_len,
2285 rsne, rsne_len)) {
2286 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2287 "RSN MLO: IE in 3/4 msg does not match with IE in Beacon/ProbeResp for link ID %u",
2288 link_id);
2289 wpa_hexdump(MSG_INFO, "RSNE in Beacon/ProbeResp",
2290 sm->mlo.links[link_id].ap_rsne,
2291 sm->mlo.links[link_id].ap_rsne_len);
2292 wpa_hexdump(MSG_INFO, "RSNE in EAPOL-Key msg 3/4",
2293 rsne, rsne_len);
2294 return -1;
2295 }
2296
2297 if ((sm->mlo.links[link_id].ap_rsnxe && !rsnxe) ||
2298 (!sm->mlo.links[link_id].ap_rsnxe && rsnxe) ||
2299 (sm->mlo.links[link_id].ap_rsnxe && rsnxe &&
2300 (sm->mlo.links[link_id].ap_rsnxe_len != rsnxe_len ||
2301 os_memcmp(sm->mlo.links[link_id].ap_rsnxe, rsnxe,
2302 sm->mlo.links[link_id].ap_rsnxe_len) != 0))) {
2303 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2304 "RSN MLO: RSNXE mismatch between Beacon/ProbeResp and EAPOL-Key msg 3/4 for link ID %u",
2305 link_id);
2306 wpa_hexdump(MSG_INFO, "RSNXE in Beacon/ProbeResp",
2307 sm->mlo.links[link_id].ap_rsnxe,
2308 sm->mlo.links[link_id].ap_rsnxe_len);
2309 wpa_hexdump(MSG_INFO, "RSNXE in EAPOL-Key msg 3/4",
2310 rsnxe, rsnxe_len);
2311 wpa_sm_deauthenticate(sm, WLAN_REASON_IE_IN_4WAY_DIFFERS);
2312 return -1;
2313 }
2314
2315 return 0;
2316}
2317
2318
2319static int wpa_validate_mlo_ieee80211w_kdes(struct wpa_sm *sm,
2320 u8 link_id,
2321 struct wpa_eapol_ie_parse *ie)
2322{
2323 if (ie->mlo_igtk[link_id] &&
2324 ie->mlo_igtk_len[link_id] != RSN_MLO_IGTK_KDE_PREFIX_LENGTH +
2325 (unsigned int) wpa_cipher_key_len(sm->mgmt_group_cipher)) {
2326 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2327 "RSN MLO: Invalid IGTK KDE length %lu for link ID %u",
2328 (unsigned long) ie->mlo_igtk_len, link_id);
2329 return -1;
2330 }
2331
2332 if (!sm->beacon_prot)
2333 return 0;
2334
2335 if (ie->mlo_bigtk[link_id] &&
2336 ie->mlo_bigtk_len[link_id] != RSN_MLO_BIGTK_KDE_PREFIX_LENGTH +
2337 (unsigned int) wpa_cipher_key_len(sm->mgmt_group_cipher)) {
2338 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
2339 "RSN MLO: Invalid BIGTK KDE length %lu for link ID %u",
2340 (unsigned long) ie->mlo_bigtk_len, link_id);
2341 return -1;
2342 }
2343
2344 return 0;
2345}
2346
2347
2348static void wpa_supplicant_process_3_of_4_wpa(struct wpa_sm *sm,
2349 const struct wpa_eapol_key *key,
2350 u16 ver, const u8 *key_data,
2351 size_t key_data_len)
2352{
2353 u16 key_info, keylen;
2354 struct wpa_eapol_ie_parse ie;
2355
2356 wpa_sm_set_state(sm, WPA_4WAY_HANDSHAKE);
2357 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2358 "WPA: RX message 3 of 4-Way Handshake from " MACSTR
2359 " (ver=%d)", MAC2STR(sm->bssid), ver);
2360
2361 key_info = WPA_GET_BE16(key->key_info);
2362
2363 wpa_hexdump(MSG_DEBUG, "WPA: IE KeyData", key_data, key_data_len);
2364 if (wpa_supplicant_parse_ies(key_data, key_data_len, &ie) < 0)
2365 goto failed;
2366
2367 if (wpa_supplicant_validate_ie(sm, sm->bssid, &ie) < 0)
2368 goto failed;
2369
2370 if (os_memcmp(sm->anonce, key->key_nonce, WPA_NONCE_LEN) != 0) {
2371 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
2372 "WPA: ANonce from message 1 of 4-Way Handshake differs from 3 of 4-Way Handshake - drop packet (src="
2373 MACSTR ")", MAC2STR(sm->bssid));
2374 goto failed;
2375 }
2376
2377 keylen = WPA_GET_BE16(key->key_length);
2378 if (keylen != wpa_cipher_key_len(sm->pairwise_cipher)) {
2379 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
2380 "WPA: Invalid %s key length %d (src=" MACSTR ")",
2381 wpa_cipher_txt(sm->pairwise_cipher), keylen,
2382 MAC2STR(sm->bssid));
2383 goto failed;
2384 }
2385
2386 if (wpa_supplicant_send_4_of_4(sm, wpa_sm_get_auth_addr(sm), key, ver,
2387 key_info, &sm->ptk) < 0)
2388 goto failed;
2389
2390 /* SNonce was successfully used in msg 3/4, so mark it to be renewed
2391 * for the next 4-Way Handshake. If msg 3 is received again, the old
2392 * SNonce will still be used to avoid changing PTK. */
2393 sm->renew_snonce = 1;
2394
2395 if ((key_info & WPA_KEY_INFO_INSTALL) &&
2396 wpa_supplicant_install_ptk(sm, key, KEY_FLAG_RX_TX))
2397 goto failed;
2398
2399 if (key_info & WPA_KEY_INFO_SECURE) {
2400 wpa_sm_mlme_setprotection(
2401 sm, sm->bssid, MLME_SETPROTECTION_PROTECT_TYPE_RX,
2402 MLME_SETPROTECTION_KEY_TYPE_PAIRWISE);
2403 eapol_sm_notify_portValid(sm->eapol, true);
2404 }
2405 wpa_sm_set_state(sm, WPA_GROUP_HANDSHAKE);
2406
2407 sm->msg_3_of_4_ok = 1;
2408 return;
2409
2410failed:
2411 wpa_sm_deauthenticate(sm, WLAN_REASON_UNSPECIFIED);
2412}
2413
2414
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002415static void wpa_supplicant_process_3_of_4(struct wpa_sm *sm,
2416 const struct wpa_eapol_key *key,
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002417 u16 ver, const u8 *key_data,
2418 size_t key_data_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002419{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002420 u16 key_info, keylen;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002421 struct wpa_eapol_ie_parse ie;
Sunil Ravi77d572f2023-01-17 23:58:31 +00002422 bool mlo = sm->mlo.valid_links;
2423 int i;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002424
2425 wpa_sm_set_state(sm, WPA_4WAY_HANDSHAKE);
Sunil Ravi77d572f2023-01-17 23:58:31 +00002426 wpa_dbg(sm->ctx->msg_ctx, MSG_INFO,
2427 "RSN: RX message 3 of 4-Way Handshake from " MACSTR
2428 " (ver=%d)%s", MAC2STR(sm->bssid), ver, mlo ? " (MLO)" : "");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002429
2430 key_info = WPA_GET_BE16(key->key_info);
2431
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002432 wpa_hexdump(MSG_DEBUG, "WPA: IE KeyData", key_data, key_data_len);
2433 if (wpa_supplicant_parse_ies(key_data, key_data_len, &ie) < 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002434 goto failed;
Sunil Ravi77d572f2023-01-17 23:58:31 +00002435
2436 if (mlo && !ie.valid_mlo_gtks) {
2437 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2438 "MLO RSN: No GTK KDE included in EAPOL-Key msg 3/4");
2439 goto failed;
2440 }
2441 if (mlo &&
2442 (key_info &
2443 (WPA_KEY_INFO_ENCR_KEY_DATA | WPA_KEY_INFO_INSTALL |
2444 WPA_KEY_INFO_SECURE)) !=
2445 (WPA_KEY_INFO_ENCR_KEY_DATA | WPA_KEY_INFO_INSTALL |
2446 WPA_KEY_INFO_SECURE)) {
2447 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
2448 "RSN MLO: Invalid key info (0x%x) in EAPOL-Key msg 3/4",
2449 key_info);
2450 goto failed;
2451 }
2452
2453 if (mlo && !is_valid_ap_mld_mac_kde(sm, ie.mac_addr)) {
2454 wpa_printf(MSG_DEBUG, "RSN: Invalid AP MLD MAC address KDE");
2455 goto failed;
2456 }
2457
2458 for (i = 0; mlo && i < MAX_NUM_MLD_LINKS; i++) {
2459 if (!(sm->mlo.req_links & BIT(i)))
2460 continue;
2461
2462 if (wpa_supplicant_validate_link_kde(sm, i, ie.mlo_link[i],
2463 ie.mlo_link_len[i]) < 0)
2464 goto failed;
2465
2466 if (!(sm->mlo.valid_links & BIT(i)))
2467 continue;
2468
2469 if (!ie.mlo_gtk[i]) {
2470 wpa_msg(sm->ctx->msg_ctx, MSG_ERROR,
2471 "RSN: GTK not found for link ID %u", i);
2472 goto failed;
2473 }
2474
2475 if (sm->mgmt_group_cipher != WPA_CIPHER_GTK_NOT_USED &&
2476 wpa_cipher_valid_mgmt_group(sm->mgmt_group_cipher) &&
2477 wpa_validate_mlo_ieee80211w_kdes(sm, i, &ie) < 0)
2478 goto failed;
2479 }
2480
2481#ifdef CONFIG_IEEE80211R
2482 if (mlo && wpa_key_mgmt_ft(sm->key_mgmt) &&
2483 wpa_supplicant_validate_ie_ft(sm, sm->bssid, &ie) < 0)
2484 goto failed;
2485#endif /* CONFIG_IEEE80211R */
2486
2487 if (!mlo && ie.gtk && !(key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002488 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
2489 "WPA: GTK IE in unencrypted key data");
2490 goto failed;
2491 }
Sunil Ravi77d572f2023-01-17 23:58:31 +00002492 if (!mlo && ie.igtk && !(key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002493 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
2494 "WPA: IGTK KDE in unencrypted key data");
2495 goto failed;
2496 }
2497
Sunil Ravi77d572f2023-01-17 23:58:31 +00002498 if (!mlo && ie.igtk &&
Hai Shalom60840252021-02-19 19:02:11 -08002499 sm->mgmt_group_cipher != WPA_CIPHER_GTK_NOT_USED &&
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002500 wpa_cipher_valid_mgmt_group(sm->mgmt_group_cipher) &&
2501 ie.igtk_len != WPA_IGTK_KDE_PREFIX_LEN +
2502 (unsigned int) wpa_cipher_key_len(sm->mgmt_group_cipher)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002503 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
2504 "WPA: Invalid IGTK KDE length %lu",
2505 (unsigned long) ie.igtk_len);
2506 goto failed;
2507 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002508
Sunil Ravi77d572f2023-01-17 23:58:31 +00002509 if (!mlo && wpa_supplicant_validate_ie(sm, sm->bssid, &ie) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002510 goto failed;
2511
Hai Shalomfdcde762020-04-02 11:19:20 -07002512 if (wpa_handle_ext_key_id(sm, &ie))
2513 goto failed;
2514
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002515 if (os_memcmp(sm->anonce, key->key_nonce, WPA_NONCE_LEN) != 0) {
2516 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
2517 "WPA: ANonce from message 1 of 4-Way Handshake "
2518 "differs from 3 of 4-Way Handshake - drop packet (src="
2519 MACSTR ")", MAC2STR(sm->bssid));
2520 goto failed;
2521 }
2522
2523 keylen = WPA_GET_BE16(key->key_length);
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07002524 if (keylen != wpa_cipher_key_len(sm->pairwise_cipher)) {
2525 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
2526 "WPA: Invalid %s key length %d (src=" MACSTR
2527 ")", wpa_cipher_txt(sm->pairwise_cipher), keylen,
2528 MAC2STR(sm->bssid));
2529 goto failed;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002530 }
2531
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002532#ifdef CONFIG_P2P
2533 if (ie.ip_addr_alloc) {
2534 os_memcpy(sm->p2p_ip_addr, ie.ip_addr_alloc, 3 * 4);
2535 wpa_hexdump(MSG_DEBUG, "P2P: IP address info",
2536 sm->p2p_ip_addr, sizeof(sm->p2p_ip_addr));
2537 }
2538#endif /* CONFIG_P2P */
2539
Hai Shalom74f70d42019-02-11 14:42:39 -08002540#ifdef CONFIG_OCV
2541 if (wpa_sm_ocv_enabled(sm)) {
2542 struct wpa_channel_info ci;
2543
2544 if (wpa_sm_channel_info(sm, &ci) != 0) {
2545 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
2546 "Failed to get channel info to validate received OCI in EAPOL-Key 3/4");
2547 return;
2548 }
2549
2550 if (ocv_verify_tx_params(ie.oci, ie.oci_len, &ci,
2551 channel_width_to_int(ci.chanwidth),
Hai Shalom899fcc72020-10-19 14:38:18 -07002552 ci.seg1_idx) != OCI_SUCCESS) {
2553 wpa_msg(sm->ctx->msg_ctx, MSG_INFO, OCV_FAILURE
2554 "addr=" MACSTR " frame=eapol-key-m3 error=%s",
2555 MAC2STR(sm->bssid), ocv_errorstr);
Hai Shalom74f70d42019-02-11 14:42:39 -08002556 return;
2557 }
2558 }
2559#endif /* CONFIG_OCV */
2560
Hai Shalom4fbc08f2020-05-18 12:37:00 -07002561#ifdef CONFIG_DPP2
2562 if (DPP_VERSION > 1 && ie.dpp_kde) {
2563 wpa_printf(MSG_DEBUG,
2564 "DPP: peer Protocol Version %u Flags 0x%x",
2565 ie.dpp_kde[0], ie.dpp_kde[1]);
2566 if (sm->key_mgmt == WPA_KEY_MGMT_DPP && sm->dpp_pfs != 2 &&
2567 (ie.dpp_kde[1] & DPP_KDE_PFS_ALLOWED) && !sm->dpp_z) {
2568 wpa_printf(MSG_INFO,
2569 "DPP: Peer indicated it supports PFS and local configuration allows this, but PFS was not negotiated for the association");
2570 goto failed;
2571 }
2572 }
2573#endif /* CONFIG_DPP2 */
2574
Hai Shalomfdcde762020-04-02 11:19:20 -07002575 if (sm->use_ext_key_id &&
2576 wpa_supplicant_install_ptk(sm, key, KEY_FLAG_RX))
2577 goto failed;
2578
Sunil Ravi77d572f2023-01-17 23:58:31 +00002579 if (wpa_supplicant_send_4_of_4(sm, wpa_sm_get_auth_addr(sm), key, ver,
2580 key_info, &sm->ptk) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002581 goto failed;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002582
2583 /* SNonce was successfully used in msg 3/4, so mark it to be renewed
2584 * for the next 4-Way Handshake. If msg 3 is received again, the old
2585 * SNonce will still be used to avoid changing PTK. */
2586 sm->renew_snonce = 1;
2587
2588 if (key_info & WPA_KEY_INFO_INSTALL) {
Hai Shalomfdcde762020-04-02 11:19:20 -07002589 int res;
2590
2591 if (sm->use_ext_key_id)
2592 res = wpa_supplicant_activate_ptk(sm);
2593 else
2594 res = wpa_supplicant_install_ptk(sm, key,
2595 KEY_FLAG_RX_TX);
2596 if (res)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002597 goto failed;
2598 }
2599
2600 if (key_info & WPA_KEY_INFO_SECURE) {
2601 wpa_sm_mlme_setprotection(
2602 sm, sm->bssid, MLME_SETPROTECTION_PROTECT_TYPE_RX,
2603 MLME_SETPROTECTION_KEY_TYPE_PAIRWISE);
Hai Shalome21d4e82020-04-29 16:34:06 -07002604 eapol_sm_notify_portValid(sm->eapol, true);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002605 }
2606 wpa_sm_set_state(sm, WPA_GROUP_HANDSHAKE);
2607
Sunil Ravi77d572f2023-01-17 23:58:31 +00002608 if (mlo) {
2609 if (wpa_supplicant_pairwise_mlo_gtk(sm, key, &ie,
2610 key_info) < 0) {
2611 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2612 "MLO RSN: Failed to configure MLO GTKs");
2613 goto failed;
2614 }
2615 } else if (sm->group_cipher == WPA_CIPHER_GTK_NOT_USED) {
Hai Shalom5f92bc92019-04-18 11:54:11 -07002616 /* No GTK to be set to the driver */
2617 } else if (!ie.gtk && sm->proto == WPA_PROTO_RSN) {
2618 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2619 "RSN: No GTK KDE included in EAPOL-Key msg 3/4");
2620 goto failed;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002621 } else if (ie.gtk &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002622 wpa_supplicant_pairwise_gtk(sm, key,
2623 ie.gtk, ie.gtk_len, key_info) < 0) {
2624 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2625 "RSN: Failed to configure GTK");
2626 goto failed;
2627 }
2628
Sunil Ravi77d572f2023-01-17 23:58:31 +00002629 if ((mlo && mlo_ieee80211w_set_keys(sm, &ie) < 0) ||
2630 (!mlo && ieee80211w_set_keys(sm, &ie) < 0)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002631 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2632 "RSN: Failed to configure IGTK");
2633 goto failed;
2634 }
2635
Sunil Ravi77d572f2023-01-17 23:58:31 +00002636 if (mlo || sm->group_cipher == WPA_CIPHER_GTK_NOT_USED || ie.gtk)
Hai Shalom5f92bc92019-04-18 11:54:11 -07002637 wpa_supplicant_key_neg_complete(sm, sm->bssid,
2638 key_info & WPA_KEY_INFO_SECURE);
2639
Sunil Ravi77d572f2023-01-17 23:58:31 +00002640 if (mlo || ie.gtk)
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002641 wpa_sm_set_rekey_offload(sm);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002642
Hai Shalom39ba6fc2019-01-22 12:40:38 -08002643 /* Add PMKSA cache entry for Suite B AKMs here since PMKID can be
2644 * calculated only after KCK has been derived. Though, do not replace an
2645 * existing PMKSA entry after each 4-way handshake (i.e., new KCK/PMKID)
2646 * to avoid unnecessary changes of PMKID while continuing to use the
2647 * same PMK. */
2648 if (sm->proto == WPA_PROTO_RSN && wpa_key_mgmt_suite_b(sm->key_mgmt) &&
2649 !sm->cur_pmksa) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002650 struct rsn_pmksa_cache_entry *sa;
2651
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08002652 sa = pmksa_cache_add(sm->pmksa, sm->pmk, sm->pmk_len, NULL,
Dmitry Shmidt807291d2015-01-27 13:40:23 -08002653 sm->ptk.kck, sm->ptk.kck_len,
Sunil Ravi77d572f2023-01-17 23:58:31 +00002654 wpa_sm_get_auth_addr(sm), sm->own_addr,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002655 sm->network_ctx, sm->key_mgmt, NULL);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002656 if (!sm->cur_pmksa)
2657 sm->cur_pmksa = sa;
2658 }
2659
Hai Shalomfdcde762020-04-02 11:19:20 -07002660 if (ie.transition_disable)
2661 wpa_sm_transition_disable(sm, ie.transition_disable[0]);
Dmitry Shmidt4dd28dc2015-03-10 11:21:43 -07002662 sm->msg_3_of_4_ok = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002663 return;
2664
2665failed:
2666 wpa_sm_deauthenticate(sm, WLAN_REASON_UNSPECIFIED);
2667}
2668
2669
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002670static int wpa_supplicant_send_2_of_2(struct wpa_sm *sm,
2671 const struct wpa_eapol_key *key,
2672 int ver, u16 key_info)
2673{
Dmitry Shmidt807291d2015-01-27 13:40:23 -08002674 size_t mic_len, hdrlen, rlen;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002675 struct wpa_eapol_key *reply;
Dmitry Shmidt807291d2015-01-27 13:40:23 -08002676 u8 *rbuf, *key_mic;
Hai Shalom74f70d42019-02-11 14:42:39 -08002677 size_t kde_len = 0;
2678
Sunil Ravia04bd252022-05-02 22:54:18 -07002679#ifdef CONFIG_TESTING_OPTIONS
2680 if (sm->disable_eapol_g2_tx) {
2681 wpa_printf(MSG_INFO, "TEST: Disable sending EAPOL-Key 2/2");
2682 return 0;
2683 }
2684#endif /* CONFIG_TESTING_OPTIONS */
2685
Hai Shalom74f70d42019-02-11 14:42:39 -08002686#ifdef CONFIG_OCV
2687 if (wpa_sm_ocv_enabled(sm))
2688 kde_len = OCV_OCI_KDE_LEN;
2689#endif /* CONFIG_OCV */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002690
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002691 mic_len = wpa_mic_len(sm->key_mgmt, sm->pmk_len);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002692 hdrlen = sizeof(*reply) + mic_len + 2;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002693 rbuf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_KEY, NULL,
Hai Shalom74f70d42019-02-11 14:42:39 -08002694 hdrlen + kde_len, &rlen, (void *) &reply);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002695 if (rbuf == NULL)
2696 return -1;
2697
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002698 reply->type = (sm->proto == WPA_PROTO_RSN ||
2699 sm->proto == WPA_PROTO_OSEN) ?
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002700 EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA;
2701 key_info &= WPA_KEY_INFO_KEY_INDEX_MASK;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002702 key_info |= ver | WPA_KEY_INFO_SECURE;
2703 if (mic_len)
2704 key_info |= WPA_KEY_INFO_MIC;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002705 else
2706 key_info |= WPA_KEY_INFO_ENCR_KEY_DATA;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002707 WPA_PUT_BE16(reply->key_info, key_info);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002708 if (sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002709 WPA_PUT_BE16(reply->key_length, 0);
2710 else
2711 os_memcpy(reply->key_length, key->key_length, 2);
2712 os_memcpy(reply->replay_counter, key->replay_counter,
2713 WPA_REPLAY_COUNTER_LEN);
2714
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002715 key_mic = (u8 *) (reply + 1);
Hai Shalom74f70d42019-02-11 14:42:39 -08002716 WPA_PUT_BE16(key_mic + mic_len, kde_len); /* Key Data Length */
2717
2718#ifdef CONFIG_OCV
2719 if (wpa_sm_ocv_enabled(sm)) {
2720 struct wpa_channel_info ci;
2721 u8 *pos;
2722
2723 if (wpa_sm_channel_info(sm, &ci) != 0) {
2724 wpa_printf(MSG_WARNING,
2725 "Failed to get channel info for OCI element in EAPOL-Key 2/2");
2726 os_free(rbuf);
2727 return -1;
2728 }
Hai Shalom899fcc72020-10-19 14:38:18 -07002729#ifdef CONFIG_TESTING_OPTIONS
2730 if (sm->oci_freq_override_eapol_g2) {
2731 wpa_printf(MSG_INFO,
2732 "TEST: Override OCI KDE frequency %d -> %d MHz",
2733 ci.frequency,
2734 sm->oci_freq_override_eapol_g2);
2735 ci.frequency = sm->oci_freq_override_eapol_g2;
2736 }
2737#endif /* CONFIG_TESTING_OPTIONS */
Hai Shalom74f70d42019-02-11 14:42:39 -08002738
2739 pos = key_mic + mic_len + 2; /* Key Data */
2740 if (ocv_insert_oci_kde(&ci, &pos) < 0) {
2741 os_free(rbuf);
2742 return -1;
2743 }
2744 }
2745#endif /* CONFIG_OCV */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002746
2747 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Sending EAPOL-Key 2/2");
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002748 return wpa_eapol_key_send(sm, &sm->ptk, ver, sm->bssid, ETH_P_EAPOL,
2749 rbuf, rlen, key_mic);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002750}
2751
2752
Sunil Ravi77d572f2023-01-17 23:58:31 +00002753static void wpa_supplicant_process_mlo_1_of_2(struct wpa_sm *sm,
2754 const unsigned char *src_addr,
2755 const struct wpa_eapol_key *key,
2756 const u8 *key_data,
2757 size_t key_data_len, u16 ver)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002758{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002759 u16 key_info;
Sunil Ravi77d572f2023-01-17 23:58:31 +00002760 u8 i;
2761 struct wpa_eapol_ie_parse ie;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002762
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002763 if (!sm->msg_3_of_4_ok && !wpa_fils_is_completed(sm)) {
Dmitry Shmidt4dd28dc2015-03-10 11:21:43 -07002764 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
Sunil Ravi77d572f2023-01-17 23:58:31 +00002765 "MLO RSN: Group Key Handshake started prior to completion of 4-way handshake");
2766 goto failed;
2767 }
2768
2769 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "MLO RSN: RX message 1 of Group "
2770 "Key Handshake from " MACSTR " (ver=%d)", MAC2STR(src_addr),
2771 ver);
2772
2773 key_info = WPA_GET_BE16(key->key_info);
2774
2775 wpa_sm_set_state(sm, WPA_GROUP_HANDSHAKE);
2776
2777 wpa_hexdump_key(MSG_DEBUG, "MLO RSN: msg 1/2 key data", key_data,
2778 key_data_len);
2779 if (wpa_supplicant_parse_ies(key_data, key_data_len, &ie) < 0)
2780 goto failed;
2781
2782 if (!ie.valid_mlo_gtks) {
2783 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2784 "MLO RSN: No MLO GTK KDE in Group Key msg 1/2");
2785 goto failed;
2786 }
2787
2788 if (!(key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
2789 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
2790 "MLO RSN: MLO GTK KDE in unencrypted key data");
2791 goto failed;
2792 }
2793
2794#ifdef CONFIG_OCV
2795 if (wpa_sm_ocv_enabled(sm)) {
2796 struct wpa_channel_info ci;
2797
2798 if (wpa_sm_channel_info(sm, &ci) != 0) {
2799 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
2800 "Failed to get channel info to validate received OCI in EAPOL-Key group msg 1/2");
2801 goto failed;
2802 }
2803
2804 if (ocv_verify_tx_params(ie.oci, ie.oci_len, &ci,
2805 channel_width_to_int(ci.chanwidth),
2806 ci.seg1_idx) != OCI_SUCCESS) {
2807 wpa_msg(sm->ctx->msg_ctx, MSG_INFO, OCV_FAILURE
2808 "addr=" MACSTR " frame=eapol-key-g1 error=%s",
2809 MAC2STR(sm->bssid), ocv_errorstr);
2810 goto failed;
2811 }
2812 }
2813#endif /* CONFIG_OCV */
2814
2815 if (mlo_ieee80211w_set_keys(sm, &ie) < 0)
2816 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2817 "MLO RSN: Failed to configure MLO IGTK");
2818
2819 for (i = 0; i < MAX_NUM_MLO_LINKS; i++) {
2820 if (!(sm->mlo.valid_links & BIT(i)))
2821 continue;
2822
2823 /*
2824 * AP may send group keys for subset of the all links during
2825 * rekey
2826 */
2827 if (!ie.mlo_gtk[i])
2828 continue;
2829
2830 if (wpa_supplicant_mlo_gtk(sm, i, ie.mlo_gtk[i],
2831 ie.mlo_gtk_len[i], key_info))
2832 goto failed;
2833 }
2834
2835 if (wpa_supplicant_send_2_of_2(sm, key, ver, key_info) < 0)
2836 goto failed;
2837
2838 wpa_msg(sm->ctx->msg_ctx, MSG_INFO, "MLO RSN: Group rekeying completed "
2839 "with " MACSTR " [GTK=%s]", MAC2STR(sm->mlo.ap_mld_addr),
2840 wpa_cipher_txt(sm->group_cipher));
2841 wpa_sm_cancel_auth_timeout(sm);
2842 wpa_sm_set_state(sm, WPA_COMPLETED);
2843
2844 wpa_sm_set_rekey_offload(sm);
2845
2846 return;
2847
2848failed:
2849 wpa_sm_deauthenticate(sm, WLAN_REASON_UNSPECIFIED);
2850}
2851
2852
2853static void wpa_supplicant_process_1_of_2_wpa(struct wpa_sm *sm,
2854 const unsigned char *src_addr,
2855 const struct wpa_eapol_key *key,
2856 const u8 *key_data,
2857 size_t key_data_len, u16 ver)
2858{
2859 u16 key_info;
2860 int rekey;
2861 struct wpa_gtk_data gd;
2862 const u8 *key_rsc;
2863 size_t maxkeylen;
2864 u16 gtk_len;
2865
2866 if (!sm->msg_3_of_4_ok) {
2867 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
Dmitry Shmidt4dd28dc2015-03-10 11:21:43 -07002868 "WPA: Group Key Handshake started prior to completion of 4-way handshake");
2869 goto failed;
2870 }
2871
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002872 os_memset(&gd, 0, sizeof(gd));
2873
2874 rekey = wpa_sm_get_state(sm) == WPA_COMPLETED;
Sunil Ravi77d572f2023-01-17 23:58:31 +00002875 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2876 "WPA: RX message 1 of Group Key Handshake from " MACSTR
2877 " (ver=%d)", MAC2STR(src_addr), ver);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002878
2879 key_info = WPA_GET_BE16(key->key_info);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002880
Sunil Ravi77d572f2023-01-17 23:58:31 +00002881 gtk_len = WPA_GET_BE16(key->key_length);
2882 maxkeylen = key_data_len;
2883 if (ver == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
2884 if (maxkeylen < 8) {
2885 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2886 "WPA: Too short maxkeylen (%lu)",
2887 (unsigned long) maxkeylen);
2888 goto failed;
2889 }
2890 maxkeylen -= 8;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002891 }
2892
Sunil Ravi77d572f2023-01-17 23:58:31 +00002893 if (gtk_len > maxkeylen ||
2894 wpa_supplicant_check_group_cipher(sm, sm->group_cipher,
2895 gtk_len, maxkeylen,
2896 &gd.key_rsc_len, &gd.alg))
2897 goto failed;
2898
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002899 wpa_sm_set_state(sm, WPA_GROUP_HANDSHAKE);
2900
Sunil Ravi77d572f2023-01-17 23:58:31 +00002901 gd.gtk_len = gtk_len;
2902 gd.keyidx = (key_info & WPA_KEY_INFO_KEY_INDEX_MASK) >>
2903 WPA_KEY_INFO_KEY_INDEX_SHIFT;
2904 if (ver == WPA_KEY_INFO_TYPE_HMAC_MD5_RC4 && sm->ptk.kek_len == 16) {
2905#if defined(CONFIG_NO_RC4) || defined(CONFIG_FIPS)
2906 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
2907 "WPA: RC4 not supported in the build");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002908 goto failed;
Sunil Ravi77d572f2023-01-17 23:58:31 +00002909#else /* CONFIG_NO_RC4 || CONFIG_FIPS */
2910 u8 ek[32];
2911 if (key_data_len > sizeof(gd.gtk)) {
2912 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
2913 "WPA: RC4 key data too long (%lu)",
2914 (unsigned long) key_data_len);
2915 goto failed;
2916 }
2917 os_memcpy(ek, key->key_iv, 16);
2918 os_memcpy(ek + 16, sm->ptk.kek, sm->ptk.kek_len);
2919 os_memcpy(gd.gtk, key_data, key_data_len);
2920 if (rc4_skip(ek, 32, 256, gd.gtk, key_data_len)) {
2921 forced_memzero(ek, sizeof(ek));
2922 wpa_msg(sm->ctx->msg_ctx, MSG_ERROR,
2923 "WPA: RC4 failed");
2924 goto failed;
2925 }
2926 forced_memzero(ek, sizeof(ek));
2927#endif /* CONFIG_NO_RC4 || CONFIG_FIPS */
2928 } else if (ver == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
2929 if (maxkeylen % 8) {
2930 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
2931 "WPA: Unsupported AES-WRAP len %lu",
2932 (unsigned long) maxkeylen);
2933 goto failed;
2934 }
2935 if (maxkeylen > sizeof(gd.gtk)) {
2936 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
2937 "WPA: AES-WRAP key data "
2938 "too long (keydatalen=%lu maxkeylen=%lu)",
2939 (unsigned long) key_data_len,
2940 (unsigned long) maxkeylen);
2941 goto failed;
2942 }
2943 if (aes_unwrap(sm->ptk.kek, sm->ptk.kek_len, maxkeylen / 8,
2944 key_data, gd.gtk)) {
2945 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
2946 "WPA: AES unwrap failed - could not decrypt "
2947 "GTK");
2948 goto failed;
2949 }
2950 } else {
2951 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
2952 "WPA: Unsupported key_info type %d", ver);
2953 goto failed;
2954 }
2955 gd.tx = wpa_supplicant_gtk_tx_bit_workaround(
2956 sm, !!(key_info & WPA_KEY_INFO_TXRX));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002957
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002958 key_rsc = key->key_rsc;
2959 if (wpa_supplicant_rsc_relaxation(sm, key->key_rsc))
2960 key_rsc = null_rsc;
2961
Jouni Malinen58c0e962017-10-01 12:12:24 +03002962 if (wpa_supplicant_install_gtk(sm, &gd, key_rsc, 0) ||
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002963 wpa_supplicant_send_2_of_2(sm, key, ver, key_info) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002964 goto failed;
Hai Shalom81f62d82019-07-22 12:10:00 -07002965 forced_memzero(&gd, sizeof(gd));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002966
2967 if (rekey) {
Sunil Ravi77d572f2023-01-17 23:58:31 +00002968 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2969 "WPA: Group rekeying completed with " MACSTR
2970 " [GTK=%s]",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002971 MAC2STR(sm->bssid), wpa_cipher_txt(sm->group_cipher));
2972 wpa_sm_cancel_auth_timeout(sm);
2973 wpa_sm_set_state(sm, WPA_COMPLETED);
2974 } else {
2975 wpa_supplicant_key_neg_complete(sm, sm->bssid,
Sunil Ravi77d572f2023-01-17 23:58:31 +00002976 key_info & WPA_KEY_INFO_SECURE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002977 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002978
2979 wpa_sm_set_rekey_offload(sm);
2980
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002981 return;
2982
2983failed:
Hai Shalom81f62d82019-07-22 12:10:00 -07002984 forced_memzero(&gd, sizeof(gd));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002985 wpa_sm_deauthenticate(sm, WLAN_REASON_UNSPECIFIED);
2986}
2987
2988
Sunil Ravi77d572f2023-01-17 23:58:31 +00002989static void wpa_supplicant_process_1_of_2(struct wpa_sm *sm,
2990 const unsigned char *src_addr,
2991 const struct wpa_eapol_key *key,
2992 const u8 *key_data,
2993 size_t key_data_len, u16 ver)
2994{
2995 u16 key_info;
2996 struct wpa_gtk_data gd;
2997 const u8 *key_rsc;
2998 int maxkeylen;
2999 struct wpa_eapol_ie_parse ie;
3000 u16 gtk_len;
3001
3002 if (!sm->msg_3_of_4_ok && !wpa_fils_is_completed(sm)) {
3003 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
3004 "RSN: Group Key Handshake started prior to completion of 4-way handshake");
3005 goto failed;
3006 }
3007
3008 os_memset(&gd, 0, sizeof(gd));
3009
3010 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
3011 "RSN: RX message 1 of Group Key Handshake from " MACSTR
3012 " (ver=%d)", MAC2STR(src_addr), ver);
3013
3014 key_info = WPA_GET_BE16(key->key_info);
3015
3016 wpa_hexdump_key(MSG_DEBUG, "RSN: msg 1/2 key data",
3017 key_data, key_data_len);
3018 if (wpa_supplicant_parse_ies(key_data, key_data_len, &ie) < 0)
3019 goto failed;
3020
3021 wpa_sm_set_state(sm, WPA_GROUP_HANDSHAKE);
3022
3023 if (ie.gtk && !(key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
3024 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
3025 "RSN: GTK KDE in unencrypted key data");
3026 goto failed;
3027 }
3028 if (!ie.gtk) {
3029 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
3030 "RSN: No GTK KDE in Group Key msg 1/2");
3031 goto failed;
3032 }
3033 gtk_len = ie.gtk_len;
3034 if (gtk_len < 2) {
3035 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
3036 "RSN: Invalid GTK KDE length (%u) in Group Key msg 1/2",
3037 gtk_len);
3038 goto failed;
3039 }
3040 gtk_len -= 2;
3041 if (gtk_len > sizeof(gd.gtk)) {
3042 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
3043 "RSN: Too long GTK in GTK KDE (len=%u)", gtk_len);
3044 goto failed;
3045 }
3046 maxkeylen = gd.gtk_len = gtk_len;
3047
3048#ifdef CONFIG_OCV
3049 if (wpa_sm_ocv_enabled(sm)) {
3050 struct wpa_channel_info ci;
3051
3052 if (wpa_sm_channel_info(sm, &ci) != 0) {
3053 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
3054 "Failed to get channel info to validate received OCI in EAPOL-Key group msg 1/2");
3055 goto failed;
3056 }
3057
3058 if (ocv_verify_tx_params(ie.oci, ie.oci_len, &ci,
3059 channel_width_to_int(ci.chanwidth),
3060 ci.seg1_idx) != OCI_SUCCESS) {
3061 wpa_msg(sm->ctx->msg_ctx, MSG_INFO, OCV_FAILURE
3062 "addr=" MACSTR " frame=eapol-key-g1 error=%s",
3063 MAC2STR(sm->bssid), ocv_errorstr);
3064 goto failed;
3065 }
3066 }
3067#endif /* CONFIG_OCV */
3068
3069 if (wpa_supplicant_check_group_cipher(sm, sm->group_cipher,
3070 gtk_len, maxkeylen,
3071 &gd.key_rsc_len, &gd.alg))
3072 goto failed;
3073
3074 wpa_hexdump_key(MSG_DEBUG, "RSN: received GTK in group key handshake",
3075 ie.gtk, 2 + gtk_len);
3076 gd.keyidx = ie.gtk[0] & 0x3;
3077 gd.tx = wpa_supplicant_gtk_tx_bit_workaround(sm,
3078 !!(ie.gtk[0] & BIT(2)));
3079 os_memcpy(gd.gtk, ie.gtk + 2, gtk_len);
3080
3081 if (ieee80211w_set_keys(sm, &ie) < 0)
3082 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
3083 "RSN: Failed to configure IGTK");
3084
3085 key_rsc = key->key_rsc;
3086 if (wpa_supplicant_rsc_relaxation(sm, key->key_rsc))
3087 key_rsc = null_rsc;
3088
3089 if (wpa_supplicant_install_gtk(sm, &gd, key_rsc, 0) ||
3090 wpa_supplicant_send_2_of_2(sm, key, ver, key_info) < 0)
3091 goto failed;
3092 forced_memzero(&gd, sizeof(gd));
3093
3094 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
3095 "RSN: Group rekeying completed with " MACSTR " [GTK=%s]",
3096 MAC2STR(sm->bssid), wpa_cipher_txt(sm->group_cipher));
3097 wpa_sm_cancel_auth_timeout(sm);
3098 wpa_sm_set_state(sm, WPA_COMPLETED);
3099
3100 wpa_sm_set_rekey_offload(sm);
3101
3102 return;
3103
3104failed:
3105 forced_memzero(&gd, sizeof(gd));
3106 wpa_sm_deauthenticate(sm, WLAN_REASON_UNSPECIFIED);
3107}
3108
3109
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003110static int wpa_supplicant_verify_eapol_key_mic(struct wpa_sm *sm,
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003111 struct wpa_eapol_key *key,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003112 u16 ver,
3113 const u8 *buf, size_t len)
3114{
Dmitry Shmidt807291d2015-01-27 13:40:23 -08003115 u8 mic[WPA_EAPOL_KEY_MIC_MAX_LEN];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003116 int ok = 0;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003117 size_t mic_len = wpa_mic_len(sm->key_mgmt, sm->pmk_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003118
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003119 os_memcpy(mic, key + 1, mic_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003120 if (sm->tptk_set) {
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003121 os_memset(key + 1, 0, mic_len);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003122 if (wpa_eapol_key_mic(sm->tptk.kck, sm->tptk.kck_len,
3123 sm->key_mgmt,
3124 ver, buf, len, (u8 *) (key + 1)) < 0 ||
3125 os_memcmp_const(mic, key + 1, mic_len) != 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003126 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
3127 "WPA: Invalid EAPOL-Key MIC "
3128 "when using TPTK - ignoring TPTK");
Hai Shalom74f70d42019-02-11 14:42:39 -08003129#ifdef TEST_FUZZ
3130 wpa_printf(MSG_INFO,
3131 "TEST: Ignore Key MIC failure for fuzz testing");
3132 goto continue_fuzz;
3133#endif /* TEST_FUZZ */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003134 } else {
Hai Shalom74f70d42019-02-11 14:42:39 -08003135#ifdef TEST_FUZZ
3136 continue_fuzz:
3137#endif /* TEST_FUZZ */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003138 ok = 1;
3139 sm->tptk_set = 0;
3140 sm->ptk_set = 1;
3141 os_memcpy(&sm->ptk, &sm->tptk, sizeof(sm->ptk));
Dmitry Shmidt61593f02014-04-21 16:27:35 -07003142 os_memset(&sm->tptk, 0, sizeof(sm->tptk));
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003143 /*
3144 * This assures the same TPTK in sm->tptk can never be
Roshan Pius3a1667e2018-07-03 15:17:14 -07003145 * copied twice to sm->ptk as the new PTK. In
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003146 * combination with the installed flag in the wpa_ptk
3147 * struct, this assures the same PTK is only installed
3148 * once.
3149 */
3150 sm->renew_snonce = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003151 }
3152 }
3153
3154 if (!ok && sm->ptk_set) {
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003155 os_memset(key + 1, 0, mic_len);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003156 if (wpa_eapol_key_mic(sm->ptk.kck, sm->ptk.kck_len,
3157 sm->key_mgmt,
3158 ver, buf, len, (u8 *) (key + 1)) < 0 ||
3159 os_memcmp_const(mic, key + 1, mic_len) != 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003160 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
3161 "WPA: Invalid EAPOL-Key MIC - "
3162 "dropping packet");
Hai Shalom74f70d42019-02-11 14:42:39 -08003163#ifdef TEST_FUZZ
3164 wpa_printf(MSG_INFO,
3165 "TEST: Ignore Key MIC failure for fuzz testing");
3166 goto continue_fuzz2;
3167#endif /* TEST_FUZZ */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003168 return -1;
3169 }
Hai Shalom74f70d42019-02-11 14:42:39 -08003170#ifdef TEST_FUZZ
3171 continue_fuzz2:
3172#endif /* TEST_FUZZ */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003173 ok = 1;
3174 }
3175
3176 if (!ok) {
3177 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
3178 "WPA: Could not verify EAPOL-Key MIC - "
3179 "dropping packet");
3180 return -1;
3181 }
3182
3183 os_memcpy(sm->rx_replay_counter, key->replay_counter,
3184 WPA_REPLAY_COUNTER_LEN);
3185 sm->rx_replay_counter_set = 1;
3186 return 0;
3187}
3188
3189
3190/* Decrypt RSN EAPOL-Key key data (RC4 or AES-WRAP) */
3191static int wpa_supplicant_decrypt_key_data(struct wpa_sm *sm,
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003192 struct wpa_eapol_key *key,
3193 size_t mic_len, u16 ver,
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003194 u8 *key_data, size_t *key_data_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003195{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003196 wpa_hexdump(MSG_DEBUG, "RSN: encrypted key data",
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003197 key_data, *key_data_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003198 if (!sm->ptk_set) {
3199 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
3200 "WPA: PTK not available, cannot decrypt EAPOL-Key Key "
3201 "Data");
3202 return -1;
3203 }
3204
3205 /* Decrypt key data here so that this operation does not need
3206 * to be implemented separately for each message type. */
Dmitry Shmidt807291d2015-01-27 13:40:23 -08003207 if (ver == WPA_KEY_INFO_TYPE_HMAC_MD5_RC4 && sm->ptk.kek_len == 16) {
Sunil Ravi77d572f2023-01-17 23:58:31 +00003208#if defined(CONFIG_NO_RC4) || defined(CONFIG_FIPS)
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003209 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
3210 "WPA: RC4 not supported in the build");
3211 return -1;
Sunil Ravi77d572f2023-01-17 23:58:31 +00003212#else /* CONFIG_NO_RC4 || CONFIG_FIPS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003213 u8 ek[32];
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003214
3215 wpa_printf(MSG_DEBUG, "WPA: Decrypt Key Data using RC4");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003216 os_memcpy(ek, key->key_iv, 16);
Dmitry Shmidt807291d2015-01-27 13:40:23 -08003217 os_memcpy(ek + 16, sm->ptk.kek, sm->ptk.kek_len);
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003218 if (rc4_skip(ek, 32, 256, key_data, *key_data_len)) {
Hai Shalom81f62d82019-07-22 12:10:00 -07003219 forced_memzero(ek, sizeof(ek));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003220 wpa_msg(sm->ctx->msg_ctx, MSG_ERROR,
3221 "WPA: RC4 failed");
3222 return -1;
3223 }
Hai Shalom81f62d82019-07-22 12:10:00 -07003224 forced_memzero(ek, sizeof(ek));
Sunil Ravi77d572f2023-01-17 23:58:31 +00003225#endif /* CONFIG_NO_RC4 || CONFIG_FIPS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003226 } else if (ver == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES ||
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08003227 ver == WPA_KEY_INFO_TYPE_AES_128_CMAC ||
Roshan Pius3a1667e2018-07-03 15:17:14 -07003228 wpa_use_aes_key_wrap(sm->key_mgmt)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003229 u8 *buf;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003230
3231 wpa_printf(MSG_DEBUG,
3232 "WPA: Decrypt Key Data using AES-UNWRAP (KEK length %u)",
3233 (unsigned int) sm->ptk.kek_len);
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003234 if (*key_data_len < 8 || *key_data_len % 8) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003235 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003236 "WPA: Unsupported AES-WRAP len %u",
3237 (unsigned int) *key_data_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003238 return -1;
3239 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003240 *key_data_len -= 8; /* AES-WRAP adds 8 bytes */
3241 buf = os_malloc(*key_data_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003242 if (buf == NULL) {
3243 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
3244 "WPA: No memory for AES-UNWRAP buffer");
3245 return -1;
3246 }
Hai Shalom74f70d42019-02-11 14:42:39 -08003247#ifdef TEST_FUZZ
3248 os_memset(buf, 0x11, *key_data_len);
3249#endif /* TEST_FUZZ */
Dmitry Shmidt807291d2015-01-27 13:40:23 -08003250 if (aes_unwrap(sm->ptk.kek, sm->ptk.kek_len, *key_data_len / 8,
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003251 key_data, buf)) {
Hai Shalom74f70d42019-02-11 14:42:39 -08003252#ifdef TEST_FUZZ
3253 wpa_printf(MSG_INFO,
3254 "TEST: Ignore AES unwrap failure for fuzz testing");
3255 goto continue_fuzz;
3256#endif /* TEST_FUZZ */
Dmitry Shmidt7d56b752015-12-22 10:59:44 -08003257 bin_clear_free(buf, *key_data_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003258 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
3259 "WPA: AES unwrap failed - "
3260 "could not decrypt EAPOL-Key key data");
3261 return -1;
3262 }
Hai Shalom74f70d42019-02-11 14:42:39 -08003263#ifdef TEST_FUZZ
3264 continue_fuzz:
3265#endif /* TEST_FUZZ */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003266 os_memcpy(key_data, buf, *key_data_len);
Dmitry Shmidt7d56b752015-12-22 10:59:44 -08003267 bin_clear_free(buf, *key_data_len);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003268 WPA_PUT_BE16(((u8 *) (key + 1)) + mic_len, *key_data_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003269 } else {
3270 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
3271 "WPA: Unsupported key_info type %d", ver);
3272 return -1;
3273 }
3274 wpa_hexdump_key(MSG_DEBUG, "WPA: decrypted EAPOL-Key key data",
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003275 key_data, *key_data_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003276 return 0;
3277}
3278
3279
3280/**
3281 * wpa_sm_aborted_cached - Notify WPA that PMKSA caching was aborted
3282 * @sm: Pointer to WPA state machine data from wpa_sm_init()
3283 */
3284void wpa_sm_aborted_cached(struct wpa_sm *sm)
3285{
3286 if (sm && sm->cur_pmksa) {
3287 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
3288 "RSN: Cancelling PMKSA caching attempt");
3289 sm->cur_pmksa = NULL;
3290 }
3291}
3292
3293
Hai Shalomc1a21442022-02-04 13:43:00 -08003294void wpa_sm_aborted_external_cached(struct wpa_sm *sm)
3295{
3296 if (sm && sm->cur_pmksa && sm->cur_pmksa->external) {
3297 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
3298 "RSN: Cancelling external PMKSA caching attempt");
3299 sm->cur_pmksa = NULL;
3300 }
3301}
3302
3303
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003304static void wpa_eapol_key_dump(struct wpa_sm *sm,
Dmitry Shmidt807291d2015-01-27 13:40:23 -08003305 const struct wpa_eapol_key *key,
3306 unsigned int key_data_len,
3307 const u8 *mic, unsigned int mic_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003308{
3309#ifndef CONFIG_NO_STDOUT_DEBUG
3310 u16 key_info = WPA_GET_BE16(key->key_info);
3311
3312 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, " EAPOL-Key type=%d", key->type);
3313 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
3314 " key_info 0x%x (ver=%d keyidx=%d rsvd=%d %s%s%s%s%s%s%s%s)",
3315 key_info, key_info & WPA_KEY_INFO_TYPE_MASK,
3316 (key_info & WPA_KEY_INFO_KEY_INDEX_MASK) >>
3317 WPA_KEY_INFO_KEY_INDEX_SHIFT,
3318 (key_info & (BIT(13) | BIT(14) | BIT(15))) >> 13,
3319 key_info & WPA_KEY_INFO_KEY_TYPE ? "Pairwise" : "Group",
3320 key_info & WPA_KEY_INFO_INSTALL ? " Install" : "",
3321 key_info & WPA_KEY_INFO_ACK ? " Ack" : "",
3322 key_info & WPA_KEY_INFO_MIC ? " MIC" : "",
3323 key_info & WPA_KEY_INFO_SECURE ? " Secure" : "",
3324 key_info & WPA_KEY_INFO_ERROR ? " Error" : "",
3325 key_info & WPA_KEY_INFO_REQUEST ? " Request" : "",
3326 key_info & WPA_KEY_INFO_ENCR_KEY_DATA ? " Encr" : "");
3327 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
3328 " key_length=%u key_data_length=%u",
Dmitry Shmidt807291d2015-01-27 13:40:23 -08003329 WPA_GET_BE16(key->key_length), key_data_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003330 wpa_hexdump(MSG_DEBUG, " replay_counter",
3331 key->replay_counter, WPA_REPLAY_COUNTER_LEN);
3332 wpa_hexdump(MSG_DEBUG, " key_nonce", key->key_nonce, WPA_NONCE_LEN);
3333 wpa_hexdump(MSG_DEBUG, " key_iv", key->key_iv, 16);
3334 wpa_hexdump(MSG_DEBUG, " key_rsc", key->key_rsc, 8);
3335 wpa_hexdump(MSG_DEBUG, " key_id (reserved)", key->key_id, 8);
Dmitry Shmidt807291d2015-01-27 13:40:23 -08003336 wpa_hexdump(MSG_DEBUG, " key_mic", mic, mic_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003337#endif /* CONFIG_NO_STDOUT_DEBUG */
3338}
3339
3340
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003341#ifdef CONFIG_FILS
3342static int wpa_supp_aead_decrypt(struct wpa_sm *sm, u8 *buf, size_t buf_len,
3343 size_t *key_data_len)
3344{
3345 struct wpa_ptk *ptk;
3346 struct ieee802_1x_hdr *hdr;
3347 struct wpa_eapol_key *key;
3348 u8 *pos, *tmp;
3349 const u8 *aad[1];
3350 size_t aad_len[1];
3351
3352 if (*key_data_len < AES_BLOCK_SIZE) {
3353 wpa_printf(MSG_INFO, "No room for AES-SIV data in the frame");
3354 return -1;
3355 }
3356
3357 if (sm->tptk_set)
3358 ptk = &sm->tptk;
3359 else if (sm->ptk_set)
3360 ptk = &sm->ptk;
3361 else
3362 return -1;
3363
3364 hdr = (struct ieee802_1x_hdr *) buf;
3365 key = (struct wpa_eapol_key *) (hdr + 1);
3366 pos = (u8 *) (key + 1);
3367 pos += 2; /* Pointing at the Encrypted Key Data field */
3368
3369 tmp = os_malloc(*key_data_len);
3370 if (!tmp)
3371 return -1;
3372
3373 /* AES-SIV AAD from EAPOL protocol version field (inclusive) to
3374 * to Key Data (exclusive). */
3375 aad[0] = buf;
3376 aad_len[0] = pos - buf;
3377 if (aes_siv_decrypt(ptk->kek, ptk->kek_len, pos, *key_data_len,
3378 1, aad, aad_len, tmp) < 0) {
3379 wpa_printf(MSG_INFO, "Invalid AES-SIV data in the frame");
3380 bin_clear_free(tmp, *key_data_len);
3381 return -1;
3382 }
3383
3384 /* AEAD decryption and validation completed successfully */
3385 (*key_data_len) -= AES_BLOCK_SIZE;
3386 wpa_hexdump_key(MSG_DEBUG, "WPA: Decrypted Key Data",
3387 tmp, *key_data_len);
3388
3389 /* Replace Key Data field with the decrypted version */
3390 os_memcpy(pos, tmp, *key_data_len);
3391 pos -= 2; /* Key Data Length field */
3392 WPA_PUT_BE16(pos, *key_data_len);
3393 bin_clear_free(tmp, *key_data_len);
3394
3395 if (sm->tptk_set) {
3396 sm->tptk_set = 0;
3397 sm->ptk_set = 1;
3398 os_memcpy(&sm->ptk, &sm->tptk, sizeof(sm->ptk));
3399 os_memset(&sm->tptk, 0, sizeof(sm->tptk));
3400 }
3401
3402 os_memcpy(sm->rx_replay_counter, key->replay_counter,
3403 WPA_REPLAY_COUNTER_LEN);
3404 sm->rx_replay_counter_set = 1;
3405
3406 return 0;
3407}
3408#endif /* CONFIG_FILS */
3409
3410
Sunil Ravi77d572f2023-01-17 23:58:31 +00003411static int wpa_sm_rx_eapol_wpa(struct wpa_sm *sm, const u8 *src_addr,
3412 struct wpa_eapol_key *key,
3413 enum frame_encryption encrypted,
3414 const u8 *tmp, size_t data_len,
3415 u8 *key_data, size_t key_data_len)
3416{
3417 u16 key_info, ver;
3418
3419 key_info = WPA_GET_BE16(key->key_info);
3420
3421 if (key->type != EAPOL_KEY_TYPE_WPA) {
3422 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
3423 "WPA: Unsupported EAPOL-Key type %d", key->type);
3424 return -1;
3425 }
3426
3427 ver = key_info & WPA_KEY_INFO_TYPE_MASK;
3428 if (ver != WPA_KEY_INFO_TYPE_HMAC_MD5_RC4 &&
3429 ver != WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
3430 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
3431 "WPA: Unsupported EAPOL-Key descriptor version %d",
3432 ver);
3433 return -1;
3434 }
3435
3436 if (sm->pairwise_cipher == WPA_CIPHER_CCMP &&
3437 ver != WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
3438 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
3439 "WPA: CCMP is used, but EAPOL-Key descriptor version (%d) is not 2",
3440 ver);
3441 if (sm->group_cipher != WPA_CIPHER_CCMP &&
3442 !(key_info & WPA_KEY_INFO_KEY_TYPE)) {
3443 /* Earlier versions of IEEE 802.11i did not explicitly
3444 * require version 2 descriptor for all EAPOL-Key
3445 * packets, so allow group keys to use version 1 if
3446 * CCMP is not used for them. */
3447 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
3448 "WPA: Backwards compatibility: allow invalid version for non-CCMP group keys");
3449 } else
3450 return -1;
3451 }
3452
3453 if ((key_info & WPA_KEY_INFO_MIC) &&
3454 wpa_supplicant_verify_eapol_key_mic(sm, key, ver, tmp, data_len))
3455 return -1;
3456
3457 if (key_info & WPA_KEY_INFO_KEY_TYPE) {
3458 if (key_info & WPA_KEY_INFO_KEY_INDEX_MASK) {
3459 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
3460 "WPA: Ignored EAPOL-Key (Pairwise) with non-zero key index");
3461 return -1;
3462 }
3463 if (key_info & (WPA_KEY_INFO_MIC |
3464 WPA_KEY_INFO_ENCR_KEY_DATA)) {
3465 /* 3/4 4-Way Handshake */
3466 wpa_supplicant_process_3_of_4_wpa(sm, key, ver,
3467 key_data,
3468 key_data_len);
3469 } else {
3470 /* 1/4 4-Way Handshake */
3471 wpa_supplicant_process_1_of_4_wpa(sm, src_addr, key,
3472 ver, key_data,
3473 key_data_len,
3474 encrypted);
3475 }
3476 } else {
3477 if (key_info & WPA_KEY_INFO_MIC) {
3478 /* 1/2 Group Key Handshake */
3479 wpa_supplicant_process_1_of_2_wpa(sm, src_addr, key,
3480 key_data,
3481 key_data_len,
3482 ver);
3483 } else {
3484 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
3485 "WPA: EAPOL-Key (Group) without Mic/Encr bit - dropped");
3486 }
3487 }
3488
3489 return 1;
3490}
3491
3492
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003493/**
3494 * wpa_sm_rx_eapol - Process received WPA EAPOL frames
3495 * @sm: Pointer to WPA state machine data from wpa_sm_init()
3496 * @src_addr: Source MAC address of the EAPOL packet
3497 * @buf: Pointer to the beginning of the EAPOL data (EAPOL header)
3498 * @len: Length of the EAPOL frame
Sunil8cd6f4d2022-06-28 18:40:46 +00003499 * @encrypted: Whether the frame was encrypted
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003500 * Returns: 1 = WPA EAPOL-Key processed, 0 = not a WPA EAPOL-Key, -1 failure
3501 *
3502 * This function is called for each received EAPOL frame. Other than EAPOL-Key
3503 * frames can be skipped if filtering is done elsewhere. wpa_sm_rx_eapol() is
3504 * only processing WPA and WPA2 EAPOL-Key frames.
3505 *
3506 * The received EAPOL-Key packets are validated and valid packets are replied
3507 * to. In addition, key material (PTK, GTK) is configured at the end of a
3508 * successful key handshake.
3509 */
3510int wpa_sm_rx_eapol(struct wpa_sm *sm, const u8 *src_addr,
Sunil8cd6f4d2022-06-28 18:40:46 +00003511 const u8 *buf, size_t len, enum frame_encryption encrypted)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003512{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003513 size_t plen, data_len, key_data_len;
3514 const struct ieee802_1x_hdr *hdr;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003515 struct wpa_eapol_key *key;
3516 u16 key_info, ver;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003517 u8 *tmp = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003518 int ret = -1;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003519 u8 *mic, *key_data;
Hai Shalom899fcc72020-10-19 14:38:18 -07003520 size_t mic_len, keyhdrlen, pmk_len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003521
3522#ifdef CONFIG_IEEE80211R
3523 sm->ft_completed = 0;
3524#endif /* CONFIG_IEEE80211R */
3525
Hai Shalom899fcc72020-10-19 14:38:18 -07003526 pmk_len = sm->pmk_len;
3527 if (!pmk_len && sm->cur_pmksa)
3528 pmk_len = sm->cur_pmksa->pmk_len;
3529 mic_len = wpa_mic_len(sm->key_mgmt, pmk_len);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003530 keyhdrlen = sizeof(*key) + mic_len + 2;
Dmitry Shmidt807291d2015-01-27 13:40:23 -08003531
3532 if (len < sizeof(*hdr) + keyhdrlen) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003533 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
3534 "WPA: EAPOL frame too short to be a WPA "
3535 "EAPOL-Key (len %lu, expecting at least %lu)",
3536 (unsigned long) len,
Dmitry Shmidt807291d2015-01-27 13:40:23 -08003537 (unsigned long) sizeof(*hdr) + keyhdrlen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003538 return 0;
3539 }
3540
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003541 hdr = (const struct ieee802_1x_hdr *) buf;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003542 plen = be_to_host16(hdr->length);
3543 data_len = plen + sizeof(*hdr);
3544 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
3545 "IEEE 802.1X RX: version=%d type=%d length=%lu",
3546 hdr->version, hdr->type, (unsigned long) plen);
3547
3548 if (hdr->version < EAPOL_VERSION) {
3549 /* TODO: backwards compatibility */
3550 }
3551 if (hdr->type != IEEE802_1X_TYPE_EAPOL_KEY) {
3552 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
3553 "WPA: EAPOL frame (type %u) discarded, "
3554 "not a Key frame", hdr->type);
3555 ret = 0;
3556 goto out;
3557 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003558 wpa_hexdump(MSG_MSGDUMP, "WPA: RX EAPOL-Key", buf, len);
Dmitry Shmidt807291d2015-01-27 13:40:23 -08003559 if (plen > len - sizeof(*hdr) || plen < keyhdrlen) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003560 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
3561 "WPA: EAPOL frame payload size %lu "
3562 "invalid (frame size %lu)",
3563 (unsigned long) plen, (unsigned long) len);
3564 ret = 0;
3565 goto out;
3566 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003567 if (data_len < len) {
3568 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
3569 "WPA: ignoring %lu bytes after the IEEE 802.1X data",
3570 (unsigned long) len - data_len);
3571 }
3572
3573 /*
3574 * Make a copy of the frame since we need to modify the buffer during
3575 * MAC validation and Key Data decryption.
3576 */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003577 tmp = os_memdup(buf, data_len);
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003578 if (tmp == NULL)
3579 goto out;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003580 key = (struct wpa_eapol_key *) (tmp + sizeof(struct ieee802_1x_hdr));
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003581 mic = (u8 *) (key + 1);
3582 key_data = mic + mic_len + 2;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003583
3584 if (key->type != EAPOL_KEY_TYPE_WPA && key->type != EAPOL_KEY_TYPE_RSN)
3585 {
3586 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
3587 "WPA: EAPOL-Key type (%d) unknown, discarded",
3588 key->type);
3589 ret = 0;
3590 goto out;
3591 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003592
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003593 key_data_len = WPA_GET_BE16(mic + mic_len);
3594 wpa_eapol_key_dump(sm, key, key_data_len, mic, mic_len);
Dmitry Shmidt807291d2015-01-27 13:40:23 -08003595
3596 if (key_data_len > plen - keyhdrlen) {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003597 wpa_msg(sm->ctx->msg_ctx, MSG_INFO, "WPA: Invalid EAPOL-Key "
3598 "frame - key_data overflow (%u > %u)",
3599 (unsigned int) key_data_len,
Dmitry Shmidt807291d2015-01-27 13:40:23 -08003600 (unsigned int) (plen - keyhdrlen));
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003601 goto out;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003602 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003603
Sunil Ravi77d572f2023-01-17 23:58:31 +00003604 if (sm->rx_replay_counter_set &&
3605 os_memcmp(key->replay_counter, sm->rx_replay_counter,
3606 WPA_REPLAY_COUNTER_LEN) <= 0) {
3607 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
3608 "WPA: EAPOL-Key Replay Counter did not increase - dropping packet");
3609 goto out;
3610 }
3611
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003612 eapol_sm_notify_lower_layer_success(sm->eapol, 0);
Sunil Ravi77d572f2023-01-17 23:58:31 +00003613
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003614 key_info = WPA_GET_BE16(key->key_info);
Sunil Ravi77d572f2023-01-17 23:58:31 +00003615
3616 if (key_info & WPA_KEY_INFO_SMK_MESSAGE) {
3617 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
3618 "WPA: Unsupported SMK bit in key_info");
3619 goto out;
3620 }
3621
3622 if (!(key_info & WPA_KEY_INFO_ACK)) {
3623 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
3624 "WPA: No Ack bit in key_info");
3625 goto out;
3626 }
3627
3628 if (key_info & WPA_KEY_INFO_REQUEST) {
3629 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
3630 "WPA: EAPOL-Key with Request bit - dropped");
3631 goto out;
3632 }
3633
3634 if (sm->proto == WPA_PROTO_WPA) {
3635 ret = wpa_sm_rx_eapol_wpa(sm, src_addr, key, encrypted,
3636 tmp, data_len,
3637 key_data, key_data_len);
3638 goto out;
3639 }
3640
3641 if (key->type != EAPOL_KEY_TYPE_RSN) {
3642 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
3643 "RSN: Unsupported EAPOL-Key type %d", key->type);
3644 goto out;
3645 }
3646
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003647 ver = key_info & WPA_KEY_INFO_TYPE_MASK;
3648 if (ver != WPA_KEY_INFO_TYPE_HMAC_MD5_RC4 &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003649 ver != WPA_KEY_INFO_TYPE_AES_128_CMAC &&
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08003650 ver != WPA_KEY_INFO_TYPE_HMAC_SHA1_AES &&
Roshan Pius3a1667e2018-07-03 15:17:14 -07003651 !wpa_use_akm_defined(sm->key_mgmt)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003652 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
Sunil Ravi77d572f2023-01-17 23:58:31 +00003653 "RSN: Unsupported EAPOL-Key descriptor version %d",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003654 ver);
3655 goto out;
3656 }
3657
Sunil Ravi77d572f2023-01-17 23:58:31 +00003658 if (ver == WPA_KEY_INFO_TYPE_HMAC_MD5_RC4 &&
3659 sm->pairwise_cipher != WPA_CIPHER_TKIP) {
3660 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
3661 "RSN: EAPOL-Key descriptor version %d not allowed without TKIP as the pairwise cipher",
3662 ver);
3663 goto out;
3664 }
3665
3666 if (ver == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES &&
3667 (sm->key_mgmt != WPA_KEY_MGMT_IEEE8021X &&
3668 sm->key_mgmt != WPA_KEY_MGMT_PSK)) {
3669 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
3670 "RSN: EAPOL-Key descriptor version %d not allowed due to negotiated AKM (0x%x)",
3671 ver, sm->key_mgmt);
3672 goto out;
3673 }
3674
Roshan Pius3a1667e2018-07-03 15:17:14 -07003675 if (wpa_use_akm_defined(sm->key_mgmt) &&
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003676 ver != WPA_KEY_INFO_TYPE_AKM_DEFINED) {
3677 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
3678 "RSN: Unsupported EAPOL-Key descriptor version %d (expected AKM defined = 0)",
3679 ver);
3680 goto out;
3681 }
3682
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003683#ifdef CONFIG_IEEE80211R
3684 if (wpa_key_mgmt_ft(sm->key_mgmt)) {
3685 /* IEEE 802.11r uses a new key_info type (AES-128-CMAC). */
Roshan Pius3a1667e2018-07-03 15:17:14 -07003686 if (ver != WPA_KEY_INFO_TYPE_AES_128_CMAC &&
3687 !wpa_use_akm_defined(sm->key_mgmt)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003688 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
3689 "FT: AP did not use AES-128-CMAC");
3690 goto out;
3691 }
3692 } else
3693#endif /* CONFIG_IEEE80211R */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003694 if (wpa_key_mgmt_sha256(sm->key_mgmt)) {
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08003695 if (ver != WPA_KEY_INFO_TYPE_AES_128_CMAC &&
Roshan Pius3a1667e2018-07-03 15:17:14 -07003696 !wpa_use_akm_defined(sm->key_mgmt)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003697 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
Sunil Ravi77d572f2023-01-17 23:58:31 +00003698 "RSN: AP did not use the negotiated AES-128-CMAC");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003699 goto out;
3700 }
Hai Shalomc3565922019-10-28 11:58:20 -07003701 } else if (sm->pairwise_cipher == WPA_CIPHER_CCMP &&
3702 !wpa_use_akm_defined(sm->key_mgmt) &&
3703 ver != WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003704 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
Sunil Ravi77d572f2023-01-17 23:58:31 +00003705 "RSN: CCMP is used, but EAPOL-Key descriptor version (%d) is not 2", ver);
3706 if (ver == WPA_KEY_INFO_TYPE_AES_128_CMAC) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003707 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
Sunil Ravi77d572f2023-01-17 23:58:31 +00003708 "RSN: Interoperability workaround: allow incorrect (should have been HMAC-SHA1), but stronger (is AES-128-CMAC), descriptor version to be used");
3709 } else {
Jouni Malinen658fb4a2014-11-14 20:57:05 +02003710 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
Sunil Ravi77d572f2023-01-17 23:58:31 +00003711 "RSN: Unexpected descriptor version %u", ver);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003712 goto out;
Sunil Ravi77d572f2023-01-17 23:58:31 +00003713 }
Dmitry Shmidt71757432014-06-02 13:50:35 -07003714 } else if (sm->pairwise_cipher == WPA_CIPHER_GCMP &&
Roshan Pius3a1667e2018-07-03 15:17:14 -07003715 !wpa_use_akm_defined(sm->key_mgmt) &&
Dmitry Shmidt71757432014-06-02 13:50:35 -07003716 ver != WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003717 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
Sunil Ravi77d572f2023-01-17 23:58:31 +00003718 "RSN: GCMP is used, but EAPOL-Key descriptor version (%d) is not 2",
3719 ver);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003720 goto out;
3721 }
3722
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003723 if ((key_info & WPA_KEY_INFO_MIC) &&
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003724 wpa_supplicant_verify_eapol_key_mic(sm, key, ver, tmp, data_len))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003725 goto out;
3726
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003727#ifdef CONFIG_FILS
3728 if (!mic_len && (key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
3729 if (wpa_supp_aead_decrypt(sm, tmp, data_len, &key_data_len))
3730 goto out;
3731 }
3732#endif /* CONFIG_FILS */
3733
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08003734 if ((sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN) &&
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003735 (key_info & WPA_KEY_INFO_ENCR_KEY_DATA) && mic_len) {
Hai Shalomce48b4a2018-09-05 11:41:35 -07003736 /*
3737 * Only decrypt the Key Data field if the frame's authenticity
3738 * was verified. When using AES-SIV (FILS), the MIC flag is not
3739 * set, so this check should only be performed if mic_len != 0
3740 * which is the case in this code branch.
3741 */
3742 if (!(key_info & WPA_KEY_INFO_MIC)) {
3743 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
3744 "WPA: Ignore EAPOL-Key with encrypted but unauthenticated data");
3745 goto out;
3746 }
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003747 if (wpa_supplicant_decrypt_key_data(sm, key, mic_len,
3748 ver, key_data,
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003749 &key_data_len))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003750 goto out;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003751 }
3752
3753 if (key_info & WPA_KEY_INFO_KEY_TYPE) {
3754 if (key_info & WPA_KEY_INFO_KEY_INDEX_MASK) {
3755 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
Sunil Ravi77d572f2023-01-17 23:58:31 +00003756 "RSN: Ignored EAPOL-Key (Pairwise) with non-zero key index");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003757 goto out;
3758 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003759 if (key_info & (WPA_KEY_INFO_MIC |
3760 WPA_KEY_INFO_ENCR_KEY_DATA)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003761 /* 3/4 4-Way Handshake */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003762 wpa_supplicant_process_3_of_4(sm, key, ver, key_data,
3763 key_data_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003764 } else {
3765 /* 1/4 4-Way Handshake */
3766 wpa_supplicant_process_1_of_4(sm, src_addr, key,
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07003767 ver, key_data,
Sunil8cd6f4d2022-06-28 18:40:46 +00003768 key_data_len,
3769 encrypted);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003770 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003771 } else {
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003772 if ((mic_len && (key_info & WPA_KEY_INFO_MIC)) ||
3773 (!mic_len && (key_info & WPA_KEY_INFO_ENCR_KEY_DATA))) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003774 /* 1/2 Group Key Handshake */
Sunil Ravi77d572f2023-01-17 23:58:31 +00003775 if (sm->mlo.valid_links)
3776 wpa_supplicant_process_mlo_1_of_2(sm, src_addr,
3777 key, key_data,
3778 key_data_len,
3779 ver);
3780 else
3781 wpa_supplicant_process_1_of_2(sm, src_addr, key,
3782 key_data,
3783 key_data_len,
3784 ver);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003785 } else {
3786 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
Sunil Ravi77d572f2023-01-17 23:58:31 +00003787 "RSN: EAPOL-Key (Group) without Mic/Encr bit - dropped");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003788 }
3789 }
3790
3791 ret = 1;
3792
3793out:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003794 bin_clear_free(tmp, data_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003795 return ret;
3796}
3797
3798
3799#ifdef CONFIG_CTRL_IFACE
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003800static u32 wpa_key_mgmt_suite(struct wpa_sm *sm)
3801{
3802 switch (sm->key_mgmt) {
3803 case WPA_KEY_MGMT_IEEE8021X:
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08003804 return ((sm->proto == WPA_PROTO_RSN ||
3805 sm->proto == WPA_PROTO_OSEN) ?
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003806 RSN_AUTH_KEY_MGMT_UNSPEC_802_1X :
3807 WPA_AUTH_KEY_MGMT_UNSPEC_802_1X);
3808 case WPA_KEY_MGMT_PSK:
3809 return (sm->proto == WPA_PROTO_RSN ?
3810 RSN_AUTH_KEY_MGMT_PSK_OVER_802_1X :
3811 WPA_AUTH_KEY_MGMT_PSK_OVER_802_1X);
3812#ifdef CONFIG_IEEE80211R
3813 case WPA_KEY_MGMT_FT_IEEE8021X:
3814 return RSN_AUTH_KEY_MGMT_FT_802_1X;
3815 case WPA_KEY_MGMT_FT_PSK:
3816 return RSN_AUTH_KEY_MGMT_FT_PSK;
3817#endif /* CONFIG_IEEE80211R */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003818 case WPA_KEY_MGMT_IEEE8021X_SHA256:
3819 return RSN_AUTH_KEY_MGMT_802_1X_SHA256;
3820 case WPA_KEY_MGMT_PSK_SHA256:
3821 return RSN_AUTH_KEY_MGMT_PSK_SHA256;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003822 case WPA_KEY_MGMT_CCKM:
3823 return (sm->proto == WPA_PROTO_RSN ?
3824 RSN_AUTH_KEY_MGMT_CCKM:
3825 WPA_AUTH_KEY_MGMT_CCKM);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003826 case WPA_KEY_MGMT_WPA_NONE:
3827 return WPA_AUTH_KEY_MGMT_NONE;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003828 case WPA_KEY_MGMT_IEEE8021X_SUITE_B:
3829 return RSN_AUTH_KEY_MGMT_802_1X_SUITE_B;
Dmitry Shmidt807291d2015-01-27 13:40:23 -08003830 case WPA_KEY_MGMT_IEEE8021X_SUITE_B_192:
3831 return RSN_AUTH_KEY_MGMT_802_1X_SUITE_B_192;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003832 default:
3833 return 0;
3834 }
3835}
3836
3837
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003838#define RSN_SUITE "%02x-%02x-%02x-%d"
3839#define RSN_SUITE_ARG(s) \
3840((s) >> 24) & 0xff, ((s) >> 16) & 0xff, ((s) >> 8) & 0xff, (s) & 0xff
3841
3842/**
3843 * wpa_sm_get_mib - Dump text list of MIB entries
3844 * @sm: Pointer to WPA state machine data from wpa_sm_init()
3845 * @buf: Buffer for the list
3846 * @buflen: Length of the buffer
3847 * Returns: Number of bytes written to buffer
3848 *
3849 * This function is used fetch dot11 MIB variables.
3850 */
3851int wpa_sm_get_mib(struct wpa_sm *sm, char *buf, size_t buflen)
3852{
3853 char pmkid_txt[PMKID_LEN * 2 + 1];
Hai Shalome21d4e82020-04-29 16:34:06 -07003854 bool rsna;
3855 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003856 size_t len;
3857
3858 if (sm->cur_pmksa) {
3859 wpa_snprintf_hex(pmkid_txt, sizeof(pmkid_txt),
3860 sm->cur_pmksa->pmkid, PMKID_LEN);
3861 } else
3862 pmkid_txt[0] = '\0';
3863
Hai Shalome21d4e82020-04-29 16:34:06 -07003864 rsna = (wpa_key_mgmt_wpa_psk(sm->key_mgmt) ||
3865 wpa_key_mgmt_wpa_ieee8021x(sm->key_mgmt)) &&
3866 sm->proto == WPA_PROTO_RSN;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003867
3868 ret = os_snprintf(buf, buflen,
3869 "dot11RSNAOptionImplemented=TRUE\n"
3870 "dot11RSNAPreauthenticationImplemented=TRUE\n"
3871 "dot11RSNAEnabled=%s\n"
3872 "dot11RSNAPreauthenticationEnabled=%s\n"
3873 "dot11RSNAConfigVersion=%d\n"
3874 "dot11RSNAConfigPairwiseKeysSupported=5\n"
3875 "dot11RSNAConfigGroupCipherSize=%d\n"
3876 "dot11RSNAConfigPMKLifetime=%d\n"
3877 "dot11RSNAConfigPMKReauthThreshold=%d\n"
3878 "dot11RSNAConfigNumberOfPTKSAReplayCounters=1\n"
3879 "dot11RSNAConfigSATimeout=%d\n",
3880 rsna ? "TRUE" : "FALSE",
3881 rsna ? "TRUE" : "FALSE",
3882 RSN_VERSION,
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07003883 wpa_cipher_key_len(sm->group_cipher) * 8,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003884 sm->dot11RSNAConfigPMKLifetime,
3885 sm->dot11RSNAConfigPMKReauthThreshold,
3886 sm->dot11RSNAConfigSATimeout);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003887 if (os_snprintf_error(buflen, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003888 return 0;
3889 len = ret;
3890
3891 ret = os_snprintf(
3892 buf + len, buflen - len,
3893 "dot11RSNAAuthenticationSuiteSelected=" RSN_SUITE "\n"
3894 "dot11RSNAPairwiseCipherSelected=" RSN_SUITE "\n"
3895 "dot11RSNAGroupCipherSelected=" RSN_SUITE "\n"
3896 "dot11RSNAPMKIDUsed=%s\n"
3897 "dot11RSNAAuthenticationSuiteRequested=" RSN_SUITE "\n"
3898 "dot11RSNAPairwiseCipherRequested=" RSN_SUITE "\n"
3899 "dot11RSNAGroupCipherRequested=" RSN_SUITE "\n"
3900 "dot11RSNAConfigNumberOfGTKSAReplayCounters=0\n"
3901 "dot11RSNA4WayHandshakeFailures=%u\n",
3902 RSN_SUITE_ARG(wpa_key_mgmt_suite(sm)),
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07003903 RSN_SUITE_ARG(wpa_cipher_to_suite(sm->proto,
3904 sm->pairwise_cipher)),
3905 RSN_SUITE_ARG(wpa_cipher_to_suite(sm->proto,
3906 sm->group_cipher)),
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003907 pmkid_txt,
3908 RSN_SUITE_ARG(wpa_key_mgmt_suite(sm)),
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07003909 RSN_SUITE_ARG(wpa_cipher_to_suite(sm->proto,
3910 sm->pairwise_cipher)),
3911 RSN_SUITE_ARG(wpa_cipher_to_suite(sm->proto,
3912 sm->group_cipher)),
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003913 sm->dot11RSNA4WayHandshakeFailures);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003914 if (!os_snprintf_error(buflen - len, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003915 len += ret;
3916
3917 return (int) len;
3918}
3919#endif /* CONFIG_CTRL_IFACE */
3920
3921
3922static void wpa_sm_pmksa_free_cb(struct rsn_pmksa_cache_entry *entry,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003923 void *ctx, enum pmksa_free_reason reason)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003924{
3925 struct wpa_sm *sm = ctx;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003926 int deauth = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003927
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003928 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "RSN: PMKSA cache entry free_cb: "
3929 MACSTR " reason=%d", MAC2STR(entry->aa), reason);
3930
3931 if (sm->cur_pmksa == entry) {
3932 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
3933 "RSN: %s current PMKSA entry",
3934 reason == PMKSA_REPLACE ? "replaced" : "removed");
3935 pmksa_cache_clear_current(sm);
3936
3937 /*
3938 * If an entry is simply being replaced, there's no need to
3939 * deauthenticate because it will be immediately re-added.
3940 * This happens when EAP authentication is completed again
3941 * (reauth or failed PMKSA caching attempt).
3942 */
3943 if (reason != PMKSA_REPLACE)
3944 deauth = 1;
3945 }
3946
3947 if (reason == PMKSA_EXPIRE &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003948 (sm->pmk_len == entry->pmk_len &&
3949 os_memcmp(sm->pmk, entry->pmk, sm->pmk_len) == 0)) {
3950 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003951 "RSN: deauthenticating due to expired PMK");
3952 pmksa_cache_clear_current(sm);
3953 deauth = 1;
3954 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003955
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003956 if (deauth) {
Roshan Pius3a1667e2018-07-03 15:17:14 -07003957 sm->pmk_len = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003958 os_memset(sm->pmk, 0, sizeof(sm->pmk));
3959 wpa_sm_deauthenticate(sm, WLAN_REASON_UNSPECIFIED);
3960 }
3961}
3962
3963
Hai Shalomc1a21442022-02-04 13:43:00 -08003964static bool wpa_sm_pmksa_is_current_cb(struct rsn_pmksa_cache_entry *entry,
3965 void *ctx)
3966{
3967 struct wpa_sm *sm = ctx;
3968
3969 return sm->cur_pmksa == entry;
3970}
3971
3972
Sunil Ravi77d572f2023-01-17 23:58:31 +00003973static void wpa_sm_pmksa_notify_cb(struct rsn_pmksa_cache_entry *entry,
3974 void *ctx)
3975{
3976 struct wpa_sm *sm = ctx;
3977
3978 wpa_sm_notify_pmksa_cache_entry(sm, entry);
3979}
3980
3981
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003982/**
3983 * wpa_sm_init - Initialize WPA state machine
3984 * @ctx: Context pointer for callbacks; this needs to be an allocated buffer
3985 * Returns: Pointer to the allocated WPA state machine data
3986 *
3987 * This function is used to allocate a new WPA state machine and the returned
3988 * value is passed to all WPA state machine calls.
3989 */
3990struct wpa_sm * wpa_sm_init(struct wpa_sm_ctx *ctx)
3991{
3992 struct wpa_sm *sm;
3993
3994 sm = os_zalloc(sizeof(*sm));
3995 if (sm == NULL)
3996 return NULL;
3997 dl_list_init(&sm->pmksa_candidates);
3998 sm->renew_snonce = 1;
3999 sm->ctx = ctx;
4000
4001 sm->dot11RSNAConfigPMKLifetime = 43200;
4002 sm->dot11RSNAConfigPMKReauthThreshold = 70;
4003 sm->dot11RSNAConfigSATimeout = 60;
4004
Hai Shalomc1a21442022-02-04 13:43:00 -08004005 sm->pmksa = pmksa_cache_init(wpa_sm_pmksa_free_cb,
Sunil Ravi77d572f2023-01-17 23:58:31 +00004006 wpa_sm_pmksa_is_current_cb,
4007 wpa_sm_pmksa_notify_cb, sm, sm);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004008 if (sm->pmksa == NULL) {
4009 wpa_msg(sm->ctx->msg_ctx, MSG_ERROR,
4010 "RSN: PMKSA cache initialization failed");
4011 os_free(sm);
4012 return NULL;
4013 }
4014
4015 return sm;
4016}
4017
4018
4019/**
4020 * wpa_sm_deinit - Deinitialize WPA state machine
4021 * @sm: Pointer to WPA state machine data from wpa_sm_init()
4022 */
4023void wpa_sm_deinit(struct wpa_sm *sm)
4024{
Sunil Ravi77d572f2023-01-17 23:58:31 +00004025 int i;
4026
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004027 if (sm == NULL)
4028 return;
4029 pmksa_cache_deinit(sm->pmksa);
4030 eloop_cancel_timeout(wpa_sm_start_preauth, sm, NULL);
4031 eloop_cancel_timeout(wpa_sm_rekey_ptk, sm, NULL);
4032 os_free(sm->assoc_wpa_ie);
Hai Shalomc3565922019-10-28 11:58:20 -07004033 os_free(sm->assoc_rsnxe);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004034 os_free(sm->ap_wpa_ie);
4035 os_free(sm->ap_rsn_ie);
Hai Shalomc3565922019-10-28 11:58:20 -07004036 os_free(sm->ap_rsnxe);
Sunil Ravi77d572f2023-01-17 23:58:31 +00004037 for (i = 0; i < MAX_NUM_MLD_LINKS; i++) {
4038 os_free(sm->mlo.links[i].ap_rsne);
4039 os_free(sm->mlo.links[i].ap_rsnxe);
4040 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004041 wpa_sm_drop_sa(sm);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004042 os_free(sm->ctx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004043#ifdef CONFIG_IEEE80211R
4044 os_free(sm->assoc_resp_ies);
4045#endif /* CONFIG_IEEE80211R */
Dmitry Shmidt55840ad2015-12-14 12:45:46 -08004046#ifdef CONFIG_TESTING_OPTIONS
4047 wpabuf_free(sm->test_assoc_ie);
4048#endif /* CONFIG_TESTING_OPTIONS */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004049#ifdef CONFIG_FILS_SK_PFS
4050 crypto_ecdh_deinit(sm->fils_ecdh);
4051#endif /* CONFIG_FILS_SK_PFS */
4052#ifdef CONFIG_FILS
4053 wpabuf_free(sm->fils_ft_ies);
4054#endif /* CONFIG_FILS */
4055#ifdef CONFIG_OWE
4056 crypto_ecdh_deinit(sm->owe_ecdh);
4057#endif /* CONFIG_OWE */
Hai Shalom021b0b52019-04-10 11:17:58 -07004058#ifdef CONFIG_DPP2
4059 wpabuf_clear_free(sm->dpp_z);
4060#endif /* CONFIG_DPP2 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004061 os_free(sm);
4062}
4063
4064
Sunil Ravi77d572f2023-01-17 23:58:31 +00004065static void wpa_sm_clear_ptk(struct wpa_sm *sm)
4066{
4067 int i;
4068
4069 sm->ptk_set = 0;
4070 os_memset(&sm->ptk, 0, sizeof(sm->ptk));
4071 sm->tptk_set = 0;
4072 os_memset(&sm->tptk, 0, sizeof(sm->tptk));
4073 os_memset(&sm->gtk, 0, sizeof(sm->gtk));
4074 os_memset(&sm->gtk_wnm_sleep, 0, sizeof(sm->gtk_wnm_sleep));
4075 os_memset(&sm->igtk, 0, sizeof(sm->igtk));
4076 os_memset(&sm->igtk_wnm_sleep, 0, sizeof(sm->igtk_wnm_sleep));
4077 sm->tk_set = false;
4078 for (i = 0; i < MAX_NUM_MLD_LINKS; i++) {
4079 os_memset(&sm->mlo.links[i].gtk, 0,
4080 sizeof(sm->mlo.links[i].gtk));
4081 os_memset(&sm->mlo.links[i].gtk_wnm_sleep, 0,
4082 sizeof(sm->mlo.links[i].gtk_wnm_sleep));
4083 os_memset(&sm->mlo.links[i].igtk, 0,
4084 sizeof(sm->mlo.links[i].igtk));
4085 os_memset(&sm->mlo.links[i].igtk_wnm_sleep, 0,
4086 sizeof(sm->mlo.links[i].igtk_wnm_sleep));
4087 }
4088}
4089
4090
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004091/**
4092 * wpa_sm_notify_assoc - Notify WPA state machine about association
4093 * @sm: Pointer to WPA state machine data from wpa_sm_init()
4094 * @bssid: The BSSID of the new association
4095 *
4096 * This function is called to let WPA state machine know that the connection
4097 * was established.
4098 */
4099void wpa_sm_notify_assoc(struct wpa_sm *sm, const u8 *bssid)
4100{
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02004101 int clear_keys = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004102
4103 if (sm == NULL)
4104 return;
4105
4106 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
4107 "WPA: Association event - clear replay counter");
4108 os_memcpy(sm->bssid, bssid, ETH_ALEN);
4109 os_memset(sm->rx_replay_counter, 0, WPA_REPLAY_COUNTER_LEN);
4110 sm->rx_replay_counter_set = 0;
4111 sm->renew_snonce = 1;
4112 if (os_memcmp(sm->preauth_bssid, bssid, ETH_ALEN) == 0)
4113 rsn_preauth_deinit(sm);
4114
4115#ifdef CONFIG_IEEE80211R
4116 if (wpa_ft_is_completed(sm)) {
4117 /*
4118 * Clear portValid to kick EAPOL state machine to re-enter
4119 * AUTHENTICATED state to get the EAPOL port Authorized.
4120 */
Hai Shalome21d4e82020-04-29 16:34:06 -07004121 eapol_sm_notify_portValid(sm->eapol, false);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004122 wpa_supplicant_key_neg_complete(sm, sm->bssid, 1);
4123
4124 /* Prepare for the next transition */
4125 wpa_ft_prepare_auth_request(sm, NULL);
4126
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02004127 clear_keys = 0;
Hai Shalom81f62d82019-07-22 12:10:00 -07004128 sm->ft_protocol = 1;
4129 } else {
4130 sm->ft_protocol = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004131 }
4132#endif /* CONFIG_IEEE80211R */
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004133#ifdef CONFIG_FILS
4134 if (sm->fils_completed) {
4135 /*
4136 * Clear portValid to kick EAPOL state machine to re-enter
4137 * AUTHENTICATED state to get the EAPOL port Authorized.
4138 */
4139 wpa_supplicant_key_neg_complete(sm, sm->bssid, 1);
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02004140 clear_keys = 0;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004141 }
4142#endif /* CONFIG_FILS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004143
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02004144 if (clear_keys) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004145 /*
4146 * IEEE 802.11, 8.4.10: Delete PTK SA on (re)association if
4147 * this is not part of a Fast BSS Transition.
4148 */
4149 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Clear old PTK");
Sunil Ravi77d572f2023-01-17 23:58:31 +00004150 wpa_sm_clear_ptk(sm);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004151 }
4152
4153#ifdef CONFIG_TDLS
4154 wpa_tdls_assoc(sm);
4155#endif /* CONFIG_TDLS */
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004156
4157#ifdef CONFIG_P2P
4158 os_memset(sm->p2p_ip_addr, 0, sizeof(sm->p2p_ip_addr));
4159#endif /* CONFIG_P2P */
Hai Shalomfdcde762020-04-02 11:19:20 -07004160
4161 sm->keyidx_active = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004162}
4163
4164
4165/**
4166 * wpa_sm_notify_disassoc - Notify WPA state machine about disassociation
4167 * @sm: Pointer to WPA state machine data from wpa_sm_init()
4168 *
4169 * This function is called to let WPA state machine know that the connection
4170 * was lost. This will abort any existing pre-authentication session.
4171 */
4172void wpa_sm_notify_disassoc(struct wpa_sm *sm)
4173{
Dmitry Shmidt4dd28dc2015-03-10 11:21:43 -07004174 eloop_cancel_timeout(wpa_sm_start_preauth, sm, NULL);
4175 eloop_cancel_timeout(wpa_sm_rekey_ptk, sm, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004176 rsn_preauth_deinit(sm);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004177 pmksa_cache_clear_current(sm);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004178 if (wpa_sm_get_state(sm) == WPA_4WAY_HANDSHAKE)
4179 sm->dot11RSNA4WayHandshakeFailures++;
4180#ifdef CONFIG_TDLS
4181 wpa_tdls_disassoc(sm);
4182#endif /* CONFIG_TDLS */
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004183#ifdef CONFIG_FILS
4184 sm->fils_completed = 0;
4185#endif /* CONFIG_FILS */
Jouni Malinen4283f9e2017-09-22 12:06:37 +03004186#ifdef CONFIG_IEEE80211R
4187 sm->ft_reassoc_completed = 0;
Hai Shalom81f62d82019-07-22 12:10:00 -07004188 sm->ft_protocol = 0;
Jouni Malinen4283f9e2017-09-22 12:06:37 +03004189#endif /* CONFIG_IEEE80211R */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004190
4191 /* Keys are not needed in the WPA state machine anymore */
4192 wpa_sm_drop_sa(sm);
Hai Shalomfdcde762020-04-02 11:19:20 -07004193 sm->keyidx_active = 0;
Dmitry Shmidt4dd28dc2015-03-10 11:21:43 -07004194
4195 sm->msg_3_of_4_ok = 0;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004196 os_memset(sm->bssid, 0, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004197}
4198
4199
4200/**
4201 * wpa_sm_set_pmk - Set PMK
4202 * @sm: Pointer to WPA state machine data from wpa_sm_init()
4203 * @pmk: The new PMK
4204 * @pmk_len: The length of the new PMK in bytes
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08004205 * @pmkid: Calculated PMKID
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004206 * @bssid: AA to add into PMKSA cache or %NULL to not cache the PMK
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004207 *
4208 * Configure the PMK for WPA state machine.
4209 */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004210void wpa_sm_set_pmk(struct wpa_sm *sm, const u8 *pmk, size_t pmk_len,
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08004211 const u8 *pmkid, const u8 *bssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004212{
4213 if (sm == NULL)
4214 return;
4215
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004216 wpa_hexdump_key(MSG_DEBUG, "WPA: Set PMK based on external data",
4217 pmk, pmk_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004218 sm->pmk_len = pmk_len;
4219 os_memcpy(sm->pmk, pmk, pmk_len);
4220
4221#ifdef CONFIG_IEEE80211R
4222 /* Set XXKey to be PSK for FT key derivation */
4223 sm->xxkey_len = pmk_len;
4224 os_memcpy(sm->xxkey, pmk, pmk_len);
4225#endif /* CONFIG_IEEE80211R */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004226
4227 if (bssid) {
Hai Shalomc1a21442022-02-04 13:43:00 -08004228 sm->cur_pmksa = pmksa_cache_add(sm->pmksa, pmk, pmk_len,
4229 pmkid, NULL, 0, bssid,
4230 sm->own_addr,
4231 sm->network_ctx, sm->key_mgmt,
4232 NULL);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004233 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004234}
4235
4236
4237/**
4238 * wpa_sm_set_pmk_from_pmksa - Set PMK based on the current PMKSA
4239 * @sm: Pointer to WPA state machine data from wpa_sm_init()
4240 *
4241 * Take the PMK from the current PMKSA into use. If no PMKSA is active, the PMK
4242 * will be cleared.
4243 */
4244void wpa_sm_set_pmk_from_pmksa(struct wpa_sm *sm)
4245{
4246 if (sm == NULL)
4247 return;
4248
4249 if (sm->cur_pmksa) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004250 wpa_hexdump_key(MSG_DEBUG,
4251 "WPA: Set PMK based on current PMKSA",
4252 sm->cur_pmksa->pmk, sm->cur_pmksa->pmk_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004253 sm->pmk_len = sm->cur_pmksa->pmk_len;
4254 os_memcpy(sm->pmk, sm->cur_pmksa->pmk, sm->pmk_len);
4255 } else {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004256 wpa_printf(MSG_DEBUG, "WPA: No current PMKSA - clear PMK");
4257 sm->pmk_len = 0;
4258 os_memset(sm->pmk, 0, PMK_LEN_MAX);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004259 }
4260}
4261
4262
4263/**
4264 * wpa_sm_set_fast_reauth - Set fast reauthentication (EAP) enabled/disabled
4265 * @sm: Pointer to WPA state machine data from wpa_sm_init()
4266 * @fast_reauth: Whether fast reauthentication (EAP) is allowed
4267 */
4268void wpa_sm_set_fast_reauth(struct wpa_sm *sm, int fast_reauth)
4269{
4270 if (sm)
4271 sm->fast_reauth = fast_reauth;
4272}
4273
4274
4275/**
4276 * wpa_sm_set_scard_ctx - Set context pointer for smartcard callbacks
4277 * @sm: Pointer to WPA state machine data from wpa_sm_init()
4278 * @scard_ctx: Context pointer for smartcard related callback functions
4279 */
4280void wpa_sm_set_scard_ctx(struct wpa_sm *sm, void *scard_ctx)
4281{
4282 if (sm == NULL)
4283 return;
4284 sm->scard_ctx = scard_ctx;
4285 if (sm->preauth_eapol)
4286 eapol_sm_register_scard_ctx(sm->preauth_eapol, scard_ctx);
4287}
4288
4289
4290/**
Hai Shalomfdcde762020-04-02 11:19:20 -07004291 * wpa_sm_set_config - Notification of current configuration change
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004292 * @sm: Pointer to WPA state machine data from wpa_sm_init()
4293 * @config: Pointer to current network configuration
4294 *
4295 * Notify WPA state machine that configuration has changed. config will be
4296 * stored as a backpointer to network configuration. This can be %NULL to clear
4297 * the stored pointed.
4298 */
4299void wpa_sm_set_config(struct wpa_sm *sm, struct rsn_supp_config *config)
4300{
4301 if (!sm)
4302 return;
4303
4304 if (config) {
4305 sm->network_ctx = config->network_ctx;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004306 sm->allowed_pairwise_cipher = config->allowed_pairwise_cipher;
4307 sm->proactive_key_caching = config->proactive_key_caching;
4308 sm->eap_workaround = config->eap_workaround;
4309 sm->eap_conf_ctx = config->eap_conf_ctx;
4310 if (config->ssid) {
4311 os_memcpy(sm->ssid, config->ssid, config->ssid_len);
4312 sm->ssid_len = config->ssid_len;
4313 } else
4314 sm->ssid_len = 0;
4315 sm->wpa_ptk_rekey = config->wpa_ptk_rekey;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004316 sm->p2p = config->p2p;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004317 sm->wpa_rsc_relaxation = config->wpa_rsc_relaxation;
Hai Shalomfdcde762020-04-02 11:19:20 -07004318 sm->owe_ptk_workaround = config->owe_ptk_workaround;
Hai Shalom60840252021-02-19 19:02:11 -08004319 sm->force_kdk_derivation = config->force_kdk_derivation;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004320#ifdef CONFIG_FILS
4321 if (config->fils_cache_id) {
4322 sm->fils_cache_id_set = 1;
4323 os_memcpy(sm->fils_cache_id, config->fils_cache_id,
4324 FILS_CACHE_ID_LEN);
4325 } else {
4326 sm->fils_cache_id_set = 0;
4327 }
4328#endif /* CONFIG_FILS */
Hai Shalomfdcde762020-04-02 11:19:20 -07004329 sm->beacon_prot = config->beacon_prot;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004330 } else {
4331 sm->network_ctx = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004332 sm->allowed_pairwise_cipher = 0;
4333 sm->proactive_key_caching = 0;
4334 sm->eap_workaround = 0;
4335 sm->eap_conf_ctx = NULL;
4336 sm->ssid_len = 0;
4337 sm->wpa_ptk_rekey = 0;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004338 sm->p2p = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004339 sm->wpa_rsc_relaxation = 0;
Hai Shalomfdcde762020-04-02 11:19:20 -07004340 sm->owe_ptk_workaround = 0;
4341 sm->beacon_prot = 0;
Hai Shalom60840252021-02-19 19:02:11 -08004342 sm->force_kdk_derivation = false;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004343 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004344}
4345
4346
Sunil Ravi77d572f2023-01-17 23:58:31 +00004347int wpa_sm_set_mlo_params(struct wpa_sm *sm, const struct wpa_sm_mlo *mlo)
4348{
4349 int i;
4350
4351 if (!sm)
4352 return -1;
4353
4354 os_memcpy(sm->mlo.ap_mld_addr, mlo->ap_mld_addr, ETH_ALEN);
4355 sm->mlo.assoc_link_id = mlo->assoc_link_id;
4356 sm->mlo.valid_links = mlo->valid_links;
4357 sm->mlo.req_links = mlo->req_links;
4358
4359 for (i = 0; i < MAX_NUM_MLD_LINKS; i++) {
4360 const u8 *ie;
4361 size_t len;
4362
4363 if (sm->mlo.req_links & BIT(i)) {
4364 if (!mlo->links[i].ap_rsne ||
4365 mlo->links[i].ap_rsne_len == 0) {
4366 wpa_dbg(sm->ctx->msg_ctx, MSG_INFO,
4367 "RSN: No RSNE for AP MLO link %d with BSSID "
4368 MACSTR,
4369 i, MAC2STR(mlo->links[i].bssid));
4370 return -1;
4371
4372 }
4373 os_memcpy(sm->mlo.links[i].addr, mlo->links[i].addr,
4374 ETH_ALEN);
4375 os_memcpy(sm->mlo.links[i].bssid, mlo->links[i].bssid,
4376 ETH_ALEN);
4377 }
4378
4379 ie = mlo->links[i].ap_rsne;
4380 len = mlo->links[i].ap_rsne_len;
4381 os_free(sm->mlo.links[i].ap_rsne);
4382 if (!ie || len == 0) {
4383 if (sm->mlo.links[i].ap_rsne)
4384 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
4385 "RSN: Clearing MLO link[%u] AP RSNE",
4386 i);
4387 sm->mlo.links[i].ap_rsne = NULL;
4388 sm->mlo.links[i].ap_rsne_len = 0;
4389 } else {
4390 wpa_hexdump_link(MSG_DEBUG, i, "RSN: Set AP RSNE",
4391 ie, len);
4392 sm->mlo.links[i].ap_rsne = os_memdup(ie, len);
4393 if (!sm->mlo.links[i].ap_rsne) {
4394 sm->mlo.links[i].ap_rsne_len = 0;
4395 return -1;
4396 }
4397 sm->mlo.links[i].ap_rsne_len = len;
4398 }
4399
4400 ie = mlo->links[i].ap_rsnxe;
4401 len = mlo->links[i].ap_rsnxe_len;
4402 os_free(sm->mlo.links[i].ap_rsnxe);
4403 if (!ie || len == 0) {
4404 if (sm->mlo.links[i].ap_rsnxe)
4405 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
4406 "RSN: Clearing MLO link[%u] AP RSNXE",
4407 i);
4408 sm->mlo.links[i].ap_rsnxe = NULL;
4409 sm->mlo.links[i].ap_rsnxe_len = 0;
4410 } else {
4411 wpa_hexdump_link(MSG_DEBUG, i, "RSN: Set AP RSNXE", ie,
4412 len);
4413 sm->mlo.links[i].ap_rsnxe = os_memdup(ie, len);
4414 if (!sm->mlo.links[i].ap_rsnxe) {
4415 sm->mlo.links[i].ap_rsnxe_len = 0;
4416 return -1;
4417 }
4418 sm->mlo.links[i].ap_rsnxe_len = len;
4419 }
4420 }
4421
4422 return 0;
4423}
4424
4425
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004426/**
4427 * wpa_sm_set_own_addr - Set own MAC address
4428 * @sm: Pointer to WPA state machine data from wpa_sm_init()
4429 * @addr: Own MAC address
4430 */
4431void wpa_sm_set_own_addr(struct wpa_sm *sm, const u8 *addr)
4432{
4433 if (sm)
4434 os_memcpy(sm->own_addr, addr, ETH_ALEN);
4435}
4436
4437
4438/**
4439 * wpa_sm_set_ifname - Set network interface name
4440 * @sm: Pointer to WPA state machine data from wpa_sm_init()
4441 * @ifname: Interface name
4442 * @bridge_ifname: Optional bridge interface name (for pre-auth)
4443 */
4444void wpa_sm_set_ifname(struct wpa_sm *sm, const char *ifname,
4445 const char *bridge_ifname)
4446{
4447 if (sm) {
4448 sm->ifname = ifname;
4449 sm->bridge_ifname = bridge_ifname;
4450 }
4451}
4452
4453
4454/**
4455 * wpa_sm_set_eapol - Set EAPOL state machine pointer
4456 * @sm: Pointer to WPA state machine data from wpa_sm_init()
4457 * @eapol: Pointer to EAPOL state machine allocated with eapol_sm_init()
4458 */
4459void wpa_sm_set_eapol(struct wpa_sm *sm, struct eapol_sm *eapol)
4460{
4461 if (sm)
4462 sm->eapol = eapol;
4463}
4464
4465
4466/**
4467 * wpa_sm_set_param - Set WPA state machine parameters
4468 * @sm: Pointer to WPA state machine data from wpa_sm_init()
4469 * @param: Parameter field
4470 * @value: Parameter value
4471 * Returns: 0 on success, -1 on failure
4472 */
4473int wpa_sm_set_param(struct wpa_sm *sm, enum wpa_sm_conf_params param,
4474 unsigned int value)
4475{
4476 int ret = 0;
4477
4478 if (sm == NULL)
4479 return -1;
4480
4481 switch (param) {
4482 case RSNA_PMK_LIFETIME:
4483 if (value > 0)
4484 sm->dot11RSNAConfigPMKLifetime = value;
4485 else
4486 ret = -1;
4487 break;
4488 case RSNA_PMK_REAUTH_THRESHOLD:
4489 if (value > 0 && value <= 100)
4490 sm->dot11RSNAConfigPMKReauthThreshold = value;
4491 else
4492 ret = -1;
4493 break;
4494 case RSNA_SA_TIMEOUT:
4495 if (value > 0)
4496 sm->dot11RSNAConfigSATimeout = value;
4497 else
4498 ret = -1;
4499 break;
4500 case WPA_PARAM_PROTO:
4501 sm->proto = value;
4502 break;
4503 case WPA_PARAM_PAIRWISE:
4504 sm->pairwise_cipher = value;
4505 break;
4506 case WPA_PARAM_GROUP:
4507 sm->group_cipher = value;
4508 break;
4509 case WPA_PARAM_KEY_MGMT:
4510 sm->key_mgmt = value;
4511 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004512 case WPA_PARAM_MGMT_GROUP:
4513 sm->mgmt_group_cipher = value;
4514 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004515 case WPA_PARAM_RSN_ENABLED:
4516 sm->rsn_enabled = value;
4517 break;
4518 case WPA_PARAM_MFP:
4519 sm->mfp = value;
4520 break;
Hai Shalom74f70d42019-02-11 14:42:39 -08004521 case WPA_PARAM_OCV:
4522 sm->ocv = value;
4523 break;
Hai Shalomc3565922019-10-28 11:58:20 -07004524 case WPA_PARAM_SAE_PWE:
4525 sm->sae_pwe = value;
4526 break;
Hai Shalom899fcc72020-10-19 14:38:18 -07004527 case WPA_PARAM_SAE_PK:
4528 sm->sae_pk = value;
4529 break;
Hai Shalomfdcde762020-04-02 11:19:20 -07004530 case WPA_PARAM_DENY_PTK0_REKEY:
4531 sm->wpa_deny_ptk0_rekey = value;
4532 break;
4533 case WPA_PARAM_EXT_KEY_ID:
4534 sm->ext_key_id = value;
4535 break;
4536 case WPA_PARAM_USE_EXT_KEY_ID:
4537 sm->use_ext_key_id = value;
4538 break;
Hai Shalomb755a2a2020-04-23 21:49:02 -07004539#ifdef CONFIG_TESTING_OPTIONS
4540 case WPA_PARAM_FT_RSNXE_USED:
4541 sm->ft_rsnxe_used = value;
4542 break;
Hai Shalom899fcc72020-10-19 14:38:18 -07004543 case WPA_PARAM_OCI_FREQ_EAPOL:
4544 sm->oci_freq_override_eapol = value;
4545 break;
4546 case WPA_PARAM_OCI_FREQ_EAPOL_G2:
4547 sm->oci_freq_override_eapol_g2 = value;
4548 break;
4549 case WPA_PARAM_OCI_FREQ_FT_ASSOC:
4550 sm->oci_freq_override_ft_assoc = value;
4551 break;
4552 case WPA_PARAM_OCI_FREQ_FILS_ASSOC:
4553 sm->oci_freq_override_fils_assoc = value;
4554 break;
Sunil Ravia04bd252022-05-02 22:54:18 -07004555 case WPA_PARAM_DISABLE_EAPOL_G2_TX:
4556 sm->disable_eapol_g2_tx = value;
4557 break;
Hai Shalomb755a2a2020-04-23 21:49:02 -07004558#endif /* CONFIG_TESTING_OPTIONS */
Hai Shalom4fbc08f2020-05-18 12:37:00 -07004559#ifdef CONFIG_DPP2
4560 case WPA_PARAM_DPP_PFS:
4561 sm->dpp_pfs = value;
4562 break;
4563#endif /* CONFIG_DPP2 */
Sunil Ravi640215c2023-06-28 23:08:09 +00004564 case WPA_PARAM_WMM_ENABLED:
4565 sm->wmm_enabled = value;
4566 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004567 default:
4568 break;
4569 }
4570
4571 return ret;
4572}
4573
4574
4575/**
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004576 * wpa_sm_get_status - Get WPA state machine
4577 * @sm: Pointer to WPA state machine data from wpa_sm_init()
4578 * @buf: Buffer for status information
4579 * @buflen: Maximum buffer length
4580 * @verbose: Whether to include verbose status information
4581 * Returns: Number of bytes written to buf.
4582 *
4583 * Query WPA state machine for status information. This function fills in
4584 * a text area with current status information. If the buffer (buf) is not
4585 * large enough, status information will be truncated to fit the buffer.
4586 */
4587int wpa_sm_get_status(struct wpa_sm *sm, char *buf, size_t buflen,
4588 int verbose)
4589{
4590 char *pos = buf, *end = buf + buflen;
4591 int ret;
4592
4593 ret = os_snprintf(pos, end - pos,
4594 "pairwise_cipher=%s\n"
4595 "group_cipher=%s\n"
4596 "key_mgmt=%s\n",
4597 wpa_cipher_txt(sm->pairwise_cipher),
4598 wpa_cipher_txt(sm->group_cipher),
4599 wpa_key_mgmt_txt(sm->key_mgmt, sm->proto));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004600 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004601 return pos - buf;
4602 pos += ret;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004603
Hai Shalom4fbc08f2020-05-18 12:37:00 -07004604#ifdef CONFIG_DPP2
4605 if (sm->key_mgmt == WPA_KEY_MGMT_DPP && sm->dpp_z) {
4606 ret = os_snprintf(pos, end - pos, "dpp_pfs=1\n");
4607 if (os_snprintf_error(end - pos, ret))
4608 return pos - buf;
4609 pos += ret;
4610 }
4611#endif /* CONFIG_DPP2 */
4612
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004613 if (sm->mfp != NO_MGMT_FRAME_PROTECTION && sm->ap_rsn_ie) {
4614 struct wpa_ie_data rsn;
4615 if (wpa_parse_wpa_ie_rsn(sm->ap_rsn_ie, sm->ap_rsn_ie_len, &rsn)
4616 >= 0 &&
4617 rsn.capabilities & (WPA_CAPABILITY_MFPR |
4618 WPA_CAPABILITY_MFPC)) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004619 ret = os_snprintf(pos, end - pos, "pmf=%d\n"
4620 "mgmt_group_cipher=%s\n",
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004621 (rsn.capabilities &
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004622 WPA_CAPABILITY_MFPR) ? 2 : 1,
4623 wpa_cipher_txt(
4624 sm->mgmt_group_cipher));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004625 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004626 return pos - buf;
4627 pos += ret;
4628 }
4629 }
4630
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004631 return pos - buf;
4632}
4633
4634
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07004635int wpa_sm_pmf_enabled(struct wpa_sm *sm)
4636{
4637 struct wpa_ie_data rsn;
4638
4639 if (sm->mfp == NO_MGMT_FRAME_PROTECTION || !sm->ap_rsn_ie)
4640 return 0;
4641
4642 if (wpa_parse_wpa_ie_rsn(sm->ap_rsn_ie, sm->ap_rsn_ie_len, &rsn) >= 0 &&
4643 rsn.capabilities & (WPA_CAPABILITY_MFPR | WPA_CAPABILITY_MFPC))
4644 return 1;
4645
4646 return 0;
4647}
4648
4649
Hai Shalomfdcde762020-04-02 11:19:20 -07004650int wpa_sm_ext_key_id(struct wpa_sm *sm)
4651{
4652 return sm ? sm->ext_key_id : 0;
4653}
4654
4655
4656int wpa_sm_ext_key_id_active(struct wpa_sm *sm)
4657{
4658 return sm ? sm->use_ext_key_id : 0;
4659}
4660
4661
Hai Shalom74f70d42019-02-11 14:42:39 -08004662int wpa_sm_ocv_enabled(struct wpa_sm *sm)
4663{
4664 struct wpa_ie_data rsn;
4665
4666 if (!sm->ocv || !sm->ap_rsn_ie)
4667 return 0;
4668
4669 return wpa_parse_wpa_ie_rsn(sm->ap_rsn_ie, sm->ap_rsn_ie_len,
4670 &rsn) >= 0 &&
4671 (rsn.capabilities & WPA_CAPABILITY_OCVC);
4672}
4673
4674
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004675/**
4676 * wpa_sm_set_assoc_wpa_ie_default - Generate own WPA/RSN IE from configuration
4677 * @sm: Pointer to WPA state machine data from wpa_sm_init()
4678 * @wpa_ie: Pointer to buffer for WPA/RSN IE
4679 * @wpa_ie_len: Pointer to the length of the wpa_ie buffer
4680 * Returns: 0 on success, -1 on failure
4681 */
4682int wpa_sm_set_assoc_wpa_ie_default(struct wpa_sm *sm, u8 *wpa_ie,
4683 size_t *wpa_ie_len)
4684{
4685 int res;
4686
4687 if (sm == NULL)
4688 return -1;
4689
Dmitry Shmidt55840ad2015-12-14 12:45:46 -08004690#ifdef CONFIG_TESTING_OPTIONS
4691 if (sm->test_assoc_ie) {
4692 wpa_printf(MSG_DEBUG,
4693 "TESTING: Replace association WPA/RSN IE");
4694 if (*wpa_ie_len < wpabuf_len(sm->test_assoc_ie))
4695 return -1;
4696 os_memcpy(wpa_ie, wpabuf_head(sm->test_assoc_ie),
4697 wpabuf_len(sm->test_assoc_ie));
4698 res = wpabuf_len(sm->test_assoc_ie);
4699 } else
4700#endif /* CONFIG_TESTING_OPTIONS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004701 res = wpa_gen_wpa_ie(sm, wpa_ie, *wpa_ie_len);
4702 if (res < 0)
4703 return -1;
4704 *wpa_ie_len = res;
4705
4706 wpa_hexdump(MSG_DEBUG, "WPA: Set own WPA IE default",
4707 wpa_ie, *wpa_ie_len);
4708
4709 if (sm->assoc_wpa_ie == NULL) {
4710 /*
4711 * Make a copy of the WPA/RSN IE so that 4-Way Handshake gets
4712 * the correct version of the IE even if PMKSA caching is
4713 * aborted (which would remove PMKID from IE generation).
4714 */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004715 sm->assoc_wpa_ie = os_memdup(wpa_ie, *wpa_ie_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004716 if (sm->assoc_wpa_ie == NULL)
4717 return -1;
4718
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004719 sm->assoc_wpa_ie_len = *wpa_ie_len;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004720 } else {
4721 wpa_hexdump(MSG_DEBUG,
4722 "WPA: Leave previously set WPA IE default",
4723 sm->assoc_wpa_ie, sm->assoc_wpa_ie_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004724 }
4725
4726 return 0;
4727}
4728
4729
4730/**
4731 * wpa_sm_set_assoc_wpa_ie - Set own WPA/RSN IE from (Re)AssocReq
4732 * @sm: Pointer to WPA state machine data from wpa_sm_init()
4733 * @ie: Pointer to IE data (starting from id)
4734 * @len: IE length
4735 * Returns: 0 on success, -1 on failure
4736 *
4737 * Inform WPA state machine about the WPA/RSN IE used in (Re)Association
4738 * Request frame. The IE will be used to override the default value generated
4739 * with wpa_sm_set_assoc_wpa_ie_default().
4740 */
4741int wpa_sm_set_assoc_wpa_ie(struct wpa_sm *sm, const u8 *ie, size_t len)
4742{
4743 if (sm == NULL)
4744 return -1;
4745
4746 os_free(sm->assoc_wpa_ie);
4747 if (ie == NULL || len == 0) {
4748 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
4749 "WPA: clearing own WPA/RSN IE");
4750 sm->assoc_wpa_ie = NULL;
4751 sm->assoc_wpa_ie_len = 0;
4752 } else {
4753 wpa_hexdump(MSG_DEBUG, "WPA: set own WPA/RSN IE", ie, len);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004754 sm->assoc_wpa_ie = os_memdup(ie, len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004755 if (sm->assoc_wpa_ie == NULL)
4756 return -1;
4757
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004758 sm->assoc_wpa_ie_len = len;
4759 }
4760
4761 return 0;
4762}
4763
4764
4765/**
Hai Shalomc3565922019-10-28 11:58:20 -07004766 * wpa_sm_set_assoc_rsnxe_default - Generate own RSNXE from configuration
4767 * @sm: Pointer to WPA state machine data from wpa_sm_init()
4768 * @rsnxe: Pointer to buffer for RSNXE
4769 * @rsnxe_len: Pointer to the length of the rsne buffer
4770 * Returns: 0 on success, -1 on failure
4771 */
4772int wpa_sm_set_assoc_rsnxe_default(struct wpa_sm *sm, u8 *rsnxe,
4773 size_t *rsnxe_len)
4774{
4775 int res;
4776
4777 if (!sm)
4778 return -1;
4779
4780 res = wpa_gen_rsnxe(sm, rsnxe, *rsnxe_len);
4781 if (res < 0)
4782 return -1;
4783 *rsnxe_len = res;
4784
4785 wpa_hexdump(MSG_DEBUG, "RSN: Set own RSNXE default", rsnxe, *rsnxe_len);
4786
4787 if (sm->assoc_rsnxe) {
4788 wpa_hexdump(MSG_DEBUG,
4789 "RSN: Leave previously set RSNXE default",
4790 sm->assoc_rsnxe, sm->assoc_rsnxe_len);
4791 } else if (*rsnxe_len > 0) {
4792 /*
4793 * Make a copy of the RSNXE so that 4-Way Handshake gets the
4794 * correct version of the IE even if it gets changed.
4795 */
4796 sm->assoc_rsnxe = os_memdup(rsnxe, *rsnxe_len);
4797 if (!sm->assoc_rsnxe)
4798 return -1;
4799
4800 sm->assoc_rsnxe_len = *rsnxe_len;
4801 }
4802
4803 return 0;
4804}
4805
4806
4807/**
4808 * wpa_sm_set_assoc_rsnxe - Set own RSNXE from (Re)AssocReq
4809 * @sm: Pointer to WPA state machine data from wpa_sm_init()
4810 * @ie: Pointer to IE data (starting from id)
4811 * @len: IE length
4812 * Returns: 0 on success, -1 on failure
4813 *
4814 * Inform WPA state machine about the RSNXE used in (Re)Association Request
4815 * frame. The IE will be used to override the default value generated
4816 * with wpa_sm_set_assoc_rsnxe_default().
4817 */
4818int wpa_sm_set_assoc_rsnxe(struct wpa_sm *sm, const u8 *ie, size_t len)
4819{
4820 if (!sm)
4821 return -1;
4822
4823 os_free(sm->assoc_rsnxe);
4824 if (!ie || len == 0) {
4825 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
4826 "RSN: clearing own RSNXE");
4827 sm->assoc_rsnxe = NULL;
4828 sm->assoc_rsnxe_len = 0;
4829 } else {
4830 wpa_hexdump(MSG_DEBUG, "RSN: set own RSNXE", ie, len);
4831 sm->assoc_rsnxe = os_memdup(ie, len);
4832 if (!sm->assoc_rsnxe)
4833 return -1;
4834
4835 sm->assoc_rsnxe_len = len;
4836 }
4837
4838 return 0;
4839}
4840
4841
4842/**
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004843 * wpa_sm_set_ap_wpa_ie - Set AP WPA IE from Beacon/ProbeResp
4844 * @sm: Pointer to WPA state machine data from wpa_sm_init()
4845 * @ie: Pointer to IE data (starting from id)
4846 * @len: IE length
4847 * Returns: 0 on success, -1 on failure
4848 *
4849 * Inform WPA state machine about the WPA IE used in Beacon / Probe Response
4850 * frame.
4851 */
4852int wpa_sm_set_ap_wpa_ie(struct wpa_sm *sm, const u8 *ie, size_t len)
4853{
4854 if (sm == NULL)
4855 return -1;
4856
4857 os_free(sm->ap_wpa_ie);
4858 if (ie == NULL || len == 0) {
4859 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
4860 "WPA: clearing AP WPA IE");
4861 sm->ap_wpa_ie = NULL;
4862 sm->ap_wpa_ie_len = 0;
4863 } else {
4864 wpa_hexdump(MSG_DEBUG, "WPA: set AP WPA IE", ie, len);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004865 sm->ap_wpa_ie = os_memdup(ie, len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004866 if (sm->ap_wpa_ie == NULL)
4867 return -1;
4868
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004869 sm->ap_wpa_ie_len = len;
4870 }
4871
4872 return 0;
4873}
4874
4875
4876/**
4877 * wpa_sm_set_ap_rsn_ie - Set AP RSN IE from Beacon/ProbeResp
4878 * @sm: Pointer to WPA state machine data from wpa_sm_init()
4879 * @ie: Pointer to IE data (starting from id)
4880 * @len: IE length
4881 * Returns: 0 on success, -1 on failure
4882 *
4883 * Inform WPA state machine about the RSN IE used in Beacon / Probe Response
4884 * frame.
4885 */
4886int wpa_sm_set_ap_rsn_ie(struct wpa_sm *sm, const u8 *ie, size_t len)
4887{
4888 if (sm == NULL)
4889 return -1;
4890
4891 os_free(sm->ap_rsn_ie);
4892 if (ie == NULL || len == 0) {
4893 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
4894 "WPA: clearing AP RSN IE");
4895 sm->ap_rsn_ie = NULL;
4896 sm->ap_rsn_ie_len = 0;
4897 } else {
4898 wpa_hexdump(MSG_DEBUG, "WPA: set AP RSN IE", ie, len);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004899 sm->ap_rsn_ie = os_memdup(ie, len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004900 if (sm->ap_rsn_ie == NULL)
4901 return -1;
4902
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004903 sm->ap_rsn_ie_len = len;
4904 }
4905
4906 return 0;
4907}
4908
4909
4910/**
Hai Shalomc3565922019-10-28 11:58:20 -07004911 * wpa_sm_set_ap_rsnxe - Set AP RSNXE from Beacon/ProbeResp
4912 * @sm: Pointer to WPA state machine data from wpa_sm_init()
4913 * @ie: Pointer to IE data (starting from id)
4914 * @len: IE length
4915 * Returns: 0 on success, -1 on failure
4916 *
4917 * Inform WPA state machine about the RSNXE used in Beacon / Probe Response
4918 * frame.
4919 */
4920int wpa_sm_set_ap_rsnxe(struct wpa_sm *sm, const u8 *ie, size_t len)
4921{
4922 if (!sm)
4923 return -1;
4924
4925 os_free(sm->ap_rsnxe);
4926 if (!ie || len == 0) {
4927 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: clearing AP RSNXE");
4928 sm->ap_rsnxe = NULL;
4929 sm->ap_rsnxe_len = 0;
4930 } else {
4931 wpa_hexdump(MSG_DEBUG, "WPA: set AP RSNXE", ie, len);
4932 sm->ap_rsnxe = os_memdup(ie, len);
4933 if (!sm->ap_rsnxe)
4934 return -1;
4935
4936 sm->ap_rsnxe_len = len;
4937 }
4938
4939 return 0;
4940}
4941
4942
4943/**
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004944 * wpa_sm_parse_own_wpa_ie - Parse own WPA/RSN IE
4945 * @sm: Pointer to WPA state machine data from wpa_sm_init()
4946 * @data: Pointer to data area for parsing results
4947 * Returns: 0 on success, -1 if IE is not known, or -2 on parsing failure
4948 *
4949 * Parse the contents of the own WPA or RSN IE from (Re)AssocReq and write the
4950 * parsed data into data.
4951 */
4952int wpa_sm_parse_own_wpa_ie(struct wpa_sm *sm, struct wpa_ie_data *data)
4953{
4954 if (sm == NULL)
4955 return -1;
4956
4957 if (sm->assoc_wpa_ie == NULL) {
4958 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
4959 "WPA: No WPA/RSN IE available from association info");
4960 return -1;
4961 }
4962 if (wpa_parse_wpa_ie(sm->assoc_wpa_ie, sm->assoc_wpa_ie_len, data))
4963 return -2;
4964 return 0;
4965}
4966
4967
4968int wpa_sm_pmksa_cache_list(struct wpa_sm *sm, char *buf, size_t len)
4969{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004970 return pmksa_cache_list(sm->pmksa, buf, len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004971}
4972
4973
Dmitry Shmidt29333592017-01-09 12:27:11 -08004974struct rsn_pmksa_cache_entry * wpa_sm_pmksa_cache_head(struct wpa_sm *sm)
4975{
4976 return pmksa_cache_head(sm->pmksa);
4977}
4978
4979
4980struct rsn_pmksa_cache_entry *
4981wpa_sm_pmksa_cache_add_entry(struct wpa_sm *sm,
4982 struct rsn_pmksa_cache_entry * entry)
4983{
4984 return pmksa_cache_add_entry(sm->pmksa, entry);
4985}
4986
4987
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004988void wpa_sm_pmksa_cache_add(struct wpa_sm *sm, const u8 *pmk, size_t pmk_len,
4989 const u8 *pmkid, const u8 *bssid,
4990 const u8 *fils_cache_id)
4991{
4992 sm->cur_pmksa = pmksa_cache_add(sm->pmksa, pmk, pmk_len, pmkid, NULL, 0,
4993 bssid, sm->own_addr, sm->network_ctx,
4994 sm->key_mgmt, fils_cache_id);
4995}
4996
4997
Sunil Ravi77d572f2023-01-17 23:58:31 +00004998int wpa_sm_pmksa_exists(struct wpa_sm *sm, const u8 *bssid, const u8 *own_addr,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004999 const void *network_ctx)
5000{
Sunil Ravi77d572f2023-01-17 23:58:31 +00005001 return pmksa_cache_get(sm->pmksa, bssid, own_addr, NULL, network_ctx,
5002 0) != NULL;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005003}
5004
5005
Hai Shalom60840252021-02-19 19:02:11 -08005006struct rsn_pmksa_cache_entry * wpa_sm_pmksa_cache_get(struct wpa_sm *sm,
5007 const u8 *aa,
5008 const u8 *pmkid,
5009 const void *network_ctx,
5010 int akmp)
5011{
Sunil Ravi77d572f2023-01-17 23:58:31 +00005012 return pmksa_cache_get(sm->pmksa, aa, sm->own_addr, pmkid, network_ctx,
5013 akmp);
5014}
5015
5016
5017void wpa_sm_pmksa_cache_remove(struct wpa_sm *sm,
5018 struct rsn_pmksa_cache_entry *entry)
5019{
5020 if (sm && sm->pmksa)
5021 pmksa_cache_remove(sm->pmksa, entry);
Hai Shalom60840252021-02-19 19:02:11 -08005022}
5023
5024
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005025void wpa_sm_drop_sa(struct wpa_sm *sm)
5026{
5027 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Clear old PMK and PTK");
Sunil Ravi77d572f2023-01-17 23:58:31 +00005028 wpa_sm_clear_ptk(sm);
Roshan Pius3a1667e2018-07-03 15:17:14 -07005029 sm->pmk_len = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005030 os_memset(sm->pmk, 0, sizeof(sm->pmk));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005031#ifdef CONFIG_IEEE80211R
5032 os_memset(sm->xxkey, 0, sizeof(sm->xxkey));
Roshan Pius3a1667e2018-07-03 15:17:14 -07005033 sm->xxkey_len = 0;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005034 os_memset(sm->pmk_r0, 0, sizeof(sm->pmk_r0));
Roshan Pius3a1667e2018-07-03 15:17:14 -07005035 sm->pmk_r0_len = 0;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005036 os_memset(sm->pmk_r1, 0, sizeof(sm->pmk_r1));
Roshan Pius3a1667e2018-07-03 15:17:14 -07005037 sm->pmk_r1_len = 0;
Hai Shalom60840252021-02-19 19:02:11 -08005038#ifdef CONFIG_PASN
5039 os_free(sm->pasn_r1kh);
5040 sm->pasn_r1kh = NULL;
5041 sm->n_pasn_r1kh = 0;
5042#endif /* CONFIG_PASN */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005043#endif /* CONFIG_IEEE80211R */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005044}
5045
5046
Sunil Ravi77d572f2023-01-17 23:58:31 +00005047#ifdef CONFIG_IEEE80211R
5048bool wpa_sm_has_ft_keys(struct wpa_sm *sm, const u8 *md)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005049{
Sunil Ravi77d572f2023-01-17 23:58:31 +00005050 if (!sm)
5051 return false;
5052 if (!wpa_key_mgmt_ft(sm->key_mgmt) ||
5053 os_memcmp(md, sm->key_mobility_domain,
5054 MOBILITY_DOMAIN_ID_LEN) != 0) {
5055 /* Do not allow FT protocol to be used even if we were to have
5056 * an PTK since the mobility domain has changed. */
5057 return false;
5058 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005059 return sm->ptk_set;
5060}
Sunil Ravi77d572f2023-01-17 23:58:31 +00005061#endif /* CONFIG_IEEE80211R */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005062
5063
Hai Shalomfdcde762020-04-02 11:19:20 -07005064int wpa_sm_has_ptk_installed(struct wpa_sm *sm)
5065{
5066 if (!sm)
5067 return 0;
Sunil8cd6f4d2022-06-28 18:40:46 +00005068 return sm->tk_set || sm->ptk.installed;
Hai Shalomfdcde762020-04-02 11:19:20 -07005069}
5070
5071
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005072void wpa_sm_update_replay_ctr(struct wpa_sm *sm, const u8 *replay_ctr)
5073{
5074 os_memcpy(sm->rx_replay_counter, replay_ctr, WPA_REPLAY_COUNTER_LEN);
5075}
5076
5077
5078void wpa_sm_pmksa_cache_flush(struct wpa_sm *sm, void *network_ctx)
5079{
Hai Shalomc1a21442022-02-04 13:43:00 -08005080 pmksa_cache_flush(sm->pmksa, network_ctx, NULL, 0, false);
5081}
5082
5083
5084void wpa_sm_external_pmksa_cache_flush(struct wpa_sm *sm, void *network_ctx)
5085{
5086 pmksa_cache_flush(sm->pmksa, network_ctx, NULL, 0, true);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08005087}
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005088
Andy Kuoaba17c12022-04-14 16:05:31 +08005089#if defined(CONFIG_DRIVER_NL80211_BRCM) || defined(CONFIG_DRIVER_NL80211_SYNA)
Mir Ali677e7482020-11-12 19:49:02 +05305090void wpa_sm_install_pmk(struct wpa_sm *sm)
5091{
5092 /* In case the driver wants to handle re-assocs, pass it down the PMK. */
Sunil Ravi77d572f2023-01-17 23:58:31 +00005093 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 +05305094 (u8*)sm->pmk, sm->pmk_len, KEY_FLAG_PMK) < 0) {
5095 wpa_hexdump(MSG_DEBUG, "PSK: Install PMK to the driver for driver reassociations",
5096 (u8*)sm->pmk, sm->pmk_len);
5097 /* No harm if the driver doesn't support. */
5098 wpa_msg(sm->ctx->msg_ctx, MSG_DEBUG,
5099 "WPA: Failed to set PMK to the driver");
5100 }
5101}
Mir Alieaaf04e2021-06-07 12:17:29 +05305102
5103void wpa_sm_notify_brcm_ft_reassoc(struct wpa_sm *sm, const u8 *bssid)
5104{
5105 u8 buf[256];
5106 struct wpa_supplicant *wpa_s = sm->ctx->ctx;
5107
5108 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
5109 "WPA: BRCM FT Reassociation event - clear replay counter");
5110 os_memcpy(sm->bssid, bssid, ETH_ALEN);
5111 os_memset(sm->rx_replay_counter, 0, WPA_REPLAY_COUNTER_LEN);
5112 sm->rx_replay_counter_set = 0;
5113
5114 if (wpa_drv_driver_cmd(wpa_s, "GET_FTKEY", (char *)buf, sizeof(buf)) < 0) {
5115 wpa_msg(sm->ctx->msg_ctx, MSG_ERROR,
5116 "WPA: Failed to get FT KEY information");
5117 wpa_supplicant_deauthenticate(
5118 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
5119
5120 } else {
5121 /* update kck and kek */
5122 os_memcpy(sm->ptk.kck, buf, 16);
5123 os_memcpy(sm->ptk.kek, buf + 16, 16);
5124 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
5125 "WPA: Updated KCK and KEK after FT reassoc");
5126 }
5127}
Andy Kuoaba17c12022-04-14 16:05:31 +08005128#endif /* CONFIG_DRIVER_NL80211_BRCM || CONFIG_DRIVER_NL80211_SYNA */
5129
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005130
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08005131#ifdef CONFIG_WNM
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005132int wpa_wnmsleep_install_key(struct wpa_sm *sm, u8 subelem_id, u8 *buf)
5133{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005134 u16 keyinfo;
5135 u8 keylen; /* plaintext key len */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005136 u8 *key_rsc;
5137
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005138 if (subelem_id == WNM_SLEEP_SUBELEM_GTK) {
Dmitry Shmidt61593f02014-04-21 16:27:35 -07005139 struct wpa_gtk_data gd;
5140
5141 os_memset(&gd, 0, sizeof(gd));
5142 keylen = wpa_cipher_key_len(sm->group_cipher);
5143 gd.key_rsc_len = wpa_cipher_rsc_len(sm->group_cipher);
5144 gd.alg = wpa_cipher_to_alg(sm->group_cipher);
5145 if (gd.alg == WPA_ALG_NONE) {
5146 wpa_printf(MSG_DEBUG, "Unsupported group cipher suite");
5147 return -1;
5148 }
5149
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005150 key_rsc = buf + 5;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08005151 keyinfo = WPA_GET_LE16(buf + 2);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005152 gd.gtk_len = keylen;
5153 if (gd.gtk_len != buf[4]) {
5154 wpa_printf(MSG_DEBUG, "GTK len mismatch len %d vs %d",
5155 gd.gtk_len, buf[4]);
5156 return -1;
5157 }
5158 gd.keyidx = keyinfo & 0x03; /* B0 - B1 */
5159 gd.tx = wpa_supplicant_gtk_tx_bit_workaround(
5160 sm, !!(keyinfo & WPA_KEY_INFO_TXRX));
5161
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08005162 os_memcpy(gd.gtk, buf + 13, gd.gtk_len);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005163
5164 wpa_hexdump_key(MSG_DEBUG, "Install GTK (WNM SLEEP)",
5165 gd.gtk, gd.gtk_len);
Jouni Malinen58c0e962017-10-01 12:12:24 +03005166 if (wpa_supplicant_install_gtk(sm, &gd, key_rsc, 1)) {
Hai Shalom81f62d82019-07-22 12:10:00 -07005167 forced_memzero(&gd, sizeof(gd));
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005168 wpa_printf(MSG_DEBUG, "Failed to install the GTK in "
5169 "WNM mode");
5170 return -1;
5171 }
Hai Shalom81f62d82019-07-22 12:10:00 -07005172 forced_memzero(&gd, sizeof(gd));
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005173 } else if (subelem_id == WNM_SLEEP_SUBELEM_IGTK) {
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02005174 const struct wpa_igtk_kde *igtk;
Dmitry Shmidt61593f02014-04-21 16:27:35 -07005175
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02005176 igtk = (const struct wpa_igtk_kde *) (buf + 2);
Jouni Malinen58c0e962017-10-01 12:12:24 +03005177 if (wpa_supplicant_install_igtk(sm, igtk, 1) < 0)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005178 return -1;
Hai Shalomfdcde762020-04-02 11:19:20 -07005179 } else if (subelem_id == WNM_SLEEP_SUBELEM_BIGTK) {
5180 const struct wpa_bigtk_kde *bigtk;
5181
5182 bigtk = (const struct wpa_bigtk_kde *) (buf + 2);
5183 if (sm->beacon_prot &&
5184 wpa_supplicant_install_bigtk(sm, bigtk, 1) < 0)
5185 return -1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07005186 } else {
5187 wpa_printf(MSG_DEBUG, "Unknown element id");
5188 return -1;
5189 }
5190
5191 return 0;
5192}
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08005193#endif /* CONFIG_WNM */
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08005194
5195
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08005196#ifdef CONFIG_P2P
5197
5198int wpa_sm_get_p2p_ip_addr(struct wpa_sm *sm, u8 *buf)
5199{
5200 if (sm == NULL || WPA_GET_BE32(sm->p2p_ip_addr) == 0)
5201 return -1;
5202 os_memcpy(buf, sm->p2p_ip_addr, 3 * 4);
5203 return 0;
5204}
5205
5206#endif /* CONFIG_P2P */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005207
5208
5209void wpa_sm_set_rx_replay_ctr(struct wpa_sm *sm, const u8 *rx_replay_counter)
5210{
5211 if (rx_replay_counter == NULL)
5212 return;
5213
5214 os_memcpy(sm->rx_replay_counter, rx_replay_counter,
5215 WPA_REPLAY_COUNTER_LEN);
5216 sm->rx_replay_counter_set = 1;
5217 wpa_printf(MSG_DEBUG, "Updated key replay counter");
5218}
5219
5220
Dmitry Shmidt807291d2015-01-27 13:40:23 -08005221void wpa_sm_set_ptk_kck_kek(struct wpa_sm *sm,
5222 const u8 *ptk_kck, size_t ptk_kck_len,
5223 const u8 *ptk_kek, size_t ptk_kek_len)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005224{
Dmitry Shmidt807291d2015-01-27 13:40:23 -08005225 if (ptk_kck && ptk_kck_len <= WPA_KCK_MAX_LEN) {
5226 os_memcpy(sm->ptk.kck, ptk_kck, ptk_kck_len);
5227 sm->ptk.kck_len = ptk_kck_len;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005228 wpa_printf(MSG_DEBUG, "Updated PTK KCK");
5229 }
Dmitry Shmidt807291d2015-01-27 13:40:23 -08005230 if (ptk_kek && ptk_kek_len <= WPA_KEK_MAX_LEN) {
5231 os_memcpy(sm->ptk.kek, ptk_kek, ptk_kek_len);
5232 sm->ptk.kek_len = ptk_kek_len;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08005233 wpa_printf(MSG_DEBUG, "Updated PTK KEK");
5234 }
5235 sm->ptk_set = 1;
5236}
Dmitry Shmidt55840ad2015-12-14 12:45:46 -08005237
5238
5239#ifdef CONFIG_TESTING_OPTIONS
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005240
Dmitry Shmidt55840ad2015-12-14 12:45:46 -08005241void wpa_sm_set_test_assoc_ie(struct wpa_sm *sm, struct wpabuf *buf)
5242{
5243 wpabuf_free(sm->test_assoc_ie);
5244 sm->test_assoc_ie = buf;
5245}
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005246
5247
5248const u8 * wpa_sm_get_anonce(struct wpa_sm *sm)
5249{
5250 return sm->anonce;
5251}
5252
Dmitry Shmidt55840ad2015-12-14 12:45:46 -08005253#endif /* CONFIG_TESTING_OPTIONS */
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08005254
5255
Roshan Pius3a1667e2018-07-03 15:17:14 -07005256unsigned int wpa_sm_get_key_mgmt(struct wpa_sm *sm)
5257{
5258 return sm->key_mgmt;
5259}
5260
5261
Sunil Ravi77d572f2023-01-17 23:58:31 +00005262const u8 * wpa_sm_get_auth_addr(struct wpa_sm *sm)
5263{
5264 return sm->mlo.valid_links ? sm->mlo.ap_mld_addr : sm->bssid;
5265}
5266
5267
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08005268#ifdef CONFIG_FILS
5269
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005270struct wpabuf * fils_build_auth(struct wpa_sm *sm, int dh_group, const u8 *md)
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08005271{
5272 struct wpabuf *buf = NULL;
5273 struct wpabuf *erp_msg;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005274 struct wpabuf *pub = NULL;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08005275
5276 erp_msg = eapol_sm_build_erp_reauth_start(sm->eapol);
5277 if (!erp_msg && !sm->cur_pmksa) {
5278 wpa_printf(MSG_DEBUG,
5279 "FILS: Neither ERP EAP-Initiate/Re-auth nor PMKSA cache entry is available - skip FILS");
5280 goto fail;
5281 }
5282
5283 wpa_printf(MSG_DEBUG, "FILS: Try to use FILS (erp=%d pmksa_cache=%d)",
5284 erp_msg != NULL, sm->cur_pmksa != NULL);
5285
5286 sm->fils_completed = 0;
5287
5288 if (!sm->assoc_wpa_ie) {
5289 wpa_printf(MSG_INFO, "FILS: No own RSN IE set for FILS");
5290 goto fail;
5291 }
5292
5293 if (random_get_bytes(sm->fils_nonce, FILS_NONCE_LEN) < 0 ||
5294 random_get_bytes(sm->fils_session, FILS_SESSION_LEN) < 0)
5295 goto fail;
5296
5297 wpa_hexdump(MSG_DEBUG, "FILS: Generated FILS Nonce",
5298 sm->fils_nonce, FILS_NONCE_LEN);
5299 wpa_hexdump(MSG_DEBUG, "FILS: Generated FILS Session",
5300 sm->fils_session, FILS_SESSION_LEN);
5301
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005302#ifdef CONFIG_FILS_SK_PFS
5303 sm->fils_dh_group = dh_group;
5304 if (dh_group) {
5305 crypto_ecdh_deinit(sm->fils_ecdh);
5306 sm->fils_ecdh = crypto_ecdh_init(dh_group);
5307 if (!sm->fils_ecdh) {
5308 wpa_printf(MSG_INFO,
5309 "FILS: Could not initialize ECDH with group %d",
5310 dh_group);
5311 goto fail;
5312 }
5313 pub = crypto_ecdh_get_pubkey(sm->fils_ecdh, 1);
5314 if (!pub)
5315 goto fail;
5316 wpa_hexdump_buf(MSG_DEBUG, "FILS: Element (DH public key)",
5317 pub);
5318 sm->fils_dh_elem_len = wpabuf_len(pub);
5319 }
5320#endif /* CONFIG_FILS_SK_PFS */
5321
5322 buf = wpabuf_alloc(1000 + sm->assoc_wpa_ie_len +
5323 (pub ? wpabuf_len(pub) : 0));
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08005324 if (!buf)
5325 goto fail;
5326
5327 /* Fields following the Authentication algorithm number field */
5328
5329 /* Authentication Transaction seq# */
5330 wpabuf_put_le16(buf, 1);
5331
5332 /* Status Code */
5333 wpabuf_put_le16(buf, WLAN_STATUS_SUCCESS);
5334
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005335 /* TODO: FILS PK */
5336#ifdef CONFIG_FILS_SK_PFS
5337 if (dh_group) {
5338 /* Finite Cyclic Group */
5339 wpabuf_put_le16(buf, dh_group);
5340 /* Element */
5341 wpabuf_put_buf(buf, pub);
5342 }
5343#endif /* CONFIG_FILS_SK_PFS */
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08005344
5345 /* RSNE */
5346 wpa_hexdump(MSG_DEBUG, "FILS: RSNE in FILS Authentication frame",
5347 sm->assoc_wpa_ie, sm->assoc_wpa_ie_len);
5348 wpabuf_put_data(buf, sm->assoc_wpa_ie, sm->assoc_wpa_ie_len);
5349
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005350 if (md) {
5351 /* MDE when using FILS for FT initial association */
5352 struct rsn_mdie *mdie;
5353
5354 wpabuf_put_u8(buf, WLAN_EID_MOBILITY_DOMAIN);
5355 wpabuf_put_u8(buf, sizeof(*mdie));
5356 mdie = wpabuf_put(buf, sizeof(*mdie));
5357 os_memcpy(mdie->mobility_domain, md, MOBILITY_DOMAIN_ID_LEN);
5358 mdie->ft_capab = 0;
5359 }
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08005360
5361 /* FILS Nonce */
5362 wpabuf_put_u8(buf, WLAN_EID_EXTENSION); /* Element ID */
5363 wpabuf_put_u8(buf, 1 + FILS_NONCE_LEN); /* Length */
5364 /* Element ID Extension */
5365 wpabuf_put_u8(buf, WLAN_EID_EXT_FILS_NONCE);
5366 wpabuf_put_data(buf, sm->fils_nonce, FILS_NONCE_LEN);
5367
5368 /* FILS Session */
5369 wpabuf_put_u8(buf, WLAN_EID_EXTENSION); /* Element ID */
5370 wpabuf_put_u8(buf, 1 + FILS_SESSION_LEN); /* Length */
5371 /* Element ID Extension */
5372 wpabuf_put_u8(buf, WLAN_EID_EXT_FILS_SESSION);
5373 wpabuf_put_data(buf, sm->fils_session, FILS_SESSION_LEN);
5374
Hai Shalomfdcde762020-04-02 11:19:20 -07005375 /* Wrapped Data */
Paul Stewart092955c2017-02-06 09:13:09 -08005376 sm->fils_erp_pmkid_set = 0;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08005377 if (erp_msg) {
5378 wpabuf_put_u8(buf, WLAN_EID_EXTENSION); /* Element ID */
5379 wpabuf_put_u8(buf, 1 + wpabuf_len(erp_msg)); /* Length */
5380 /* Element ID Extension */
Hai Shalomfdcde762020-04-02 11:19:20 -07005381 wpabuf_put_u8(buf, WLAN_EID_EXT_WRAPPED_DATA);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08005382 wpabuf_put_buf(buf, erp_msg);
Paul Stewart092955c2017-02-06 09:13:09 -08005383 /* Calculate pending PMKID here so that we do not need to
5384 * maintain a copy of the EAP-Initiate/Reauth message. */
5385 if (fils_pmkid_erp(sm->key_mgmt, wpabuf_head(erp_msg),
5386 wpabuf_len(erp_msg),
5387 sm->fils_erp_pmkid) == 0)
5388 sm->fils_erp_pmkid_set = 1;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08005389 }
5390
5391 wpa_hexdump_buf(MSG_DEBUG, "RSN: FILS fields for Authentication frame",
5392 buf);
5393
5394fail:
5395 wpabuf_free(erp_msg);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005396 wpabuf_free(pub);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08005397 return buf;
5398}
5399
5400
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005401int fils_process_auth(struct wpa_sm *sm, const u8 *bssid, const u8 *data,
5402 size_t len)
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08005403{
5404 const u8 *pos, *end;
5405 struct ieee802_11_elems elems;
5406 struct wpa_ie_data rsn;
5407 int pmkid_match = 0;
5408 u8 ick[FILS_ICK_MAX_LEN];
5409 size_t ick_len;
5410 int res;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005411 struct wpabuf *dh_ss = NULL;
5412 const u8 *g_sta = NULL;
5413 size_t g_sta_len = 0;
5414 const u8 *g_ap = NULL;
Hai Shalom60840252021-02-19 19:02:11 -08005415 size_t g_ap_len = 0, kdk_len;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005416 struct wpabuf *pub = NULL;
5417
5418 os_memcpy(sm->bssid, bssid, ETH_ALEN);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08005419
5420 wpa_hexdump(MSG_DEBUG, "FILS: Authentication frame fields",
5421 data, len);
5422 pos = data;
5423 end = data + len;
5424
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005425 /* TODO: FILS PK */
5426#ifdef CONFIG_FILS_SK_PFS
5427 if (sm->fils_dh_group) {
5428 u16 group;
5429
5430 /* Using FILS PFS */
5431
5432 /* Finite Cyclic Group */
5433 if (end - pos < 2) {
5434 wpa_printf(MSG_DEBUG,
5435 "FILS: No room for Finite Cyclic Group");
5436 goto fail;
5437 }
5438 group = WPA_GET_LE16(pos);
5439 pos += 2;
5440 if (group != sm->fils_dh_group) {
5441 wpa_printf(MSG_DEBUG,
5442 "FILS: Unexpected change in Finite Cyclic Group: %u (expected %u)",
5443 group, sm->fils_dh_group);
5444 goto fail;
5445 }
5446
5447 /* Element */
5448 if ((size_t) (end - pos) < sm->fils_dh_elem_len) {
5449 wpa_printf(MSG_DEBUG, "FILS: No room for Element");
5450 goto fail;
5451 }
5452
5453 if (!sm->fils_ecdh) {
5454 wpa_printf(MSG_DEBUG, "FILS: No ECDH state available");
5455 goto fail;
5456 }
5457 dh_ss = crypto_ecdh_set_peerkey(sm->fils_ecdh, 1, pos,
5458 sm->fils_dh_elem_len);
5459 if (!dh_ss) {
5460 wpa_printf(MSG_DEBUG, "FILS: ECDH operation failed");
5461 goto fail;
5462 }
5463 wpa_hexdump_buf_key(MSG_DEBUG, "FILS: DH_SS", dh_ss);
5464 g_ap = pos;
5465 g_ap_len = sm->fils_dh_elem_len;
5466 pos += sm->fils_dh_elem_len;
5467 }
5468#endif /* CONFIG_FILS_SK_PFS */
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08005469
5470 wpa_hexdump(MSG_DEBUG, "FILS: Remaining IEs", pos, end - pos);
5471 if (ieee802_11_parse_elems(pos, end - pos, &elems, 1) == ParseFailed) {
5472 wpa_printf(MSG_DEBUG, "FILS: Could not parse elements");
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005473 goto fail;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08005474 }
5475
5476 /* RSNE */
5477 wpa_hexdump(MSG_DEBUG, "FILS: RSN element", elems.rsn_ie,
5478 elems.rsn_ie_len);
5479 if (!elems.rsn_ie ||
5480 wpa_parse_wpa_ie_rsn(elems.rsn_ie - 2, elems.rsn_ie_len + 2,
5481 &rsn) < 0) {
5482 wpa_printf(MSG_DEBUG, "FILS: No RSN element");
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005483 goto fail;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08005484 }
5485
5486 if (!elems.fils_nonce) {
5487 wpa_printf(MSG_DEBUG, "FILS: No FILS Nonce field");
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005488 goto fail;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08005489 }
5490 os_memcpy(sm->fils_anonce, elems.fils_nonce, FILS_NONCE_LEN);
5491 wpa_hexdump(MSG_DEBUG, "FILS: ANonce", sm->fils_anonce, FILS_NONCE_LEN);
5492
Roshan Pius3a1667e2018-07-03 15:17:14 -07005493#ifdef CONFIG_IEEE80211R
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005494 if (wpa_key_mgmt_ft(sm->key_mgmt)) {
5495 struct wpa_ft_ies parse;
5496
5497 if (!elems.mdie || !elems.ftie) {
5498 wpa_printf(MSG_DEBUG, "FILS+FT: No MDE or FTE");
5499 goto fail;
5500 }
5501
Roshan Pius3a1667e2018-07-03 15:17:14 -07005502 if (wpa_ft_parse_ies(pos, end - pos, &parse,
Sunil Ravi77d572f2023-01-17 23:58:31 +00005503 sm->key_mgmt) < 0) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005504 wpa_printf(MSG_DEBUG, "FILS+FT: Failed to parse IEs");
5505 goto fail;
5506 }
5507
5508 if (!parse.r0kh_id) {
5509 wpa_printf(MSG_DEBUG,
5510 "FILS+FT: No R0KH-ID subelem in FTE");
5511 goto fail;
5512 }
5513 os_memcpy(sm->r0kh_id, parse.r0kh_id, parse.r0kh_id_len);
5514 sm->r0kh_id_len = parse.r0kh_id_len;
5515 wpa_hexdump_ascii(MSG_DEBUG, "FILS+FT: R0KH-ID",
5516 sm->r0kh_id, sm->r0kh_id_len);
5517
5518 if (!parse.r1kh_id) {
5519 wpa_printf(MSG_DEBUG,
5520 "FILS+FT: No R1KH-ID subelem in FTE");
5521 goto fail;
5522 }
5523 os_memcpy(sm->r1kh_id, parse.r1kh_id, FT_R1KH_ID_LEN);
5524 wpa_hexdump(MSG_DEBUG, "FILS+FT: R1KH-ID",
5525 sm->r1kh_id, FT_R1KH_ID_LEN);
5526
5527 /* TODO: Check MDE and FTE payload */
5528
5529 wpabuf_free(sm->fils_ft_ies);
5530 sm->fils_ft_ies = wpabuf_alloc(2 + elems.mdie_len +
5531 2 + elems.ftie_len);
5532 if (!sm->fils_ft_ies)
5533 goto fail;
5534 wpabuf_put_data(sm->fils_ft_ies, elems.mdie - 2,
5535 2 + elems.mdie_len);
5536 wpabuf_put_data(sm->fils_ft_ies, elems.ftie - 2,
5537 2 + elems.ftie_len);
5538 } else {
5539 wpabuf_free(sm->fils_ft_ies);
5540 sm->fils_ft_ies = NULL;
5541 }
Roshan Pius3a1667e2018-07-03 15:17:14 -07005542#endif /* CONFIG_IEEE80211R */
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08005543
5544 /* PMKID List */
5545 if (rsn.pmkid && rsn.num_pmkid > 0) {
5546 wpa_hexdump(MSG_DEBUG, "FILS: PMKID List",
5547 rsn.pmkid, rsn.num_pmkid * PMKID_LEN);
5548
5549 if (rsn.num_pmkid != 1) {
5550 wpa_printf(MSG_DEBUG, "FILS: Invalid PMKID selection");
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005551 goto fail;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08005552 }
5553 wpa_hexdump(MSG_DEBUG, "FILS: PMKID", rsn.pmkid, PMKID_LEN);
5554 if (os_memcmp(sm->cur_pmksa->pmkid, rsn.pmkid, PMKID_LEN) != 0)
5555 {
5556 wpa_printf(MSG_DEBUG, "FILS: PMKID mismatch");
5557 wpa_hexdump(MSG_DEBUG, "FILS: Expected PMKID",
5558 sm->cur_pmksa->pmkid, PMKID_LEN);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005559 goto fail;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08005560 }
5561 wpa_printf(MSG_DEBUG,
5562 "FILS: Matching PMKID - continue using PMKSA caching");
5563 pmkid_match = 1;
5564 }
5565 if (!pmkid_match && sm->cur_pmksa) {
5566 wpa_printf(MSG_DEBUG,
5567 "FILS: No PMKID match - cannot use cached PMKSA entry");
5568 sm->cur_pmksa = NULL;
5569 }
5570
5571 /* FILS Session */
5572 if (!elems.fils_session) {
5573 wpa_printf(MSG_DEBUG, "FILS: No FILS Session element");
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005574 goto fail;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08005575 }
5576 wpa_hexdump(MSG_DEBUG, "FILS: FILS Session", elems.fils_session,
5577 FILS_SESSION_LEN);
5578 if (os_memcmp(sm->fils_session, elems.fils_session, FILS_SESSION_LEN)
5579 != 0) {
5580 wpa_printf(MSG_DEBUG, "FILS: Session mismatch");
5581 wpa_hexdump(MSG_DEBUG, "FILS: Expected FILS Session",
5582 sm->fils_session, FILS_SESSION_LEN);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005583 goto fail;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08005584 }
5585
Hai Shalomfdcde762020-04-02 11:19:20 -07005586 /* Wrapped Data */
5587 if (!sm->cur_pmksa && elems.wrapped_data) {
Paul Stewart092955c2017-02-06 09:13:09 -08005588 u8 rmsk[ERP_MAX_KEY_LEN];
5589 size_t rmsk_len;
5590
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08005591 wpa_hexdump(MSG_DEBUG, "FILS: Wrapped Data",
Hai Shalomfdcde762020-04-02 11:19:20 -07005592 elems.wrapped_data,
5593 elems.wrapped_data_len);
5594 eapol_sm_process_erp_finish(sm->eapol, elems.wrapped_data,
5595 elems.wrapped_data_len);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08005596 if (eapol_sm_failed(sm->eapol))
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005597 goto fail;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08005598
Paul Stewart092955c2017-02-06 09:13:09 -08005599 rmsk_len = ERP_MAX_KEY_LEN;
5600 res = eapol_sm_get_key(sm->eapol, rmsk, rmsk_len);
5601 if (res == PMK_LEN) {
5602 rmsk_len = PMK_LEN;
5603 res = eapol_sm_get_key(sm->eapol, rmsk, rmsk_len);
5604 }
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08005605 if (res)
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005606 goto fail;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08005607
Paul Stewart092955c2017-02-06 09:13:09 -08005608 res = fils_rmsk_to_pmk(sm->key_mgmt, rmsk, rmsk_len,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005609 sm->fils_nonce, sm->fils_anonce,
5610 dh_ss ? wpabuf_head(dh_ss) : NULL,
5611 dh_ss ? wpabuf_len(dh_ss) : 0,
Paul Stewart092955c2017-02-06 09:13:09 -08005612 sm->pmk, &sm->pmk_len);
Hai Shalom81f62d82019-07-22 12:10:00 -07005613 forced_memzero(rmsk, sizeof(rmsk));
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005614
5615 /* Don't use DHss in PTK derivation if PMKSA caching is not
5616 * used. */
5617 wpabuf_clear_free(dh_ss);
5618 dh_ss = NULL;
5619
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08005620 if (res)
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005621 goto fail;
Paul Stewart092955c2017-02-06 09:13:09 -08005622
5623 if (!sm->fils_erp_pmkid_set) {
5624 wpa_printf(MSG_DEBUG, "FILS: PMKID not available");
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005625 goto fail;
Paul Stewart092955c2017-02-06 09:13:09 -08005626 }
5627 wpa_hexdump(MSG_DEBUG, "FILS: PMKID", sm->fils_erp_pmkid,
5628 PMKID_LEN);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08005629 wpa_printf(MSG_DEBUG, "FILS: ERP processing succeeded - add PMKSA cache entry for the result");
Paul Stewart092955c2017-02-06 09:13:09 -08005630 sm->cur_pmksa = pmksa_cache_add(sm->pmksa, sm->pmk, sm->pmk_len,
5631 sm->fils_erp_pmkid, NULL, 0,
5632 sm->bssid, sm->own_addr,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005633 sm->network_ctx, sm->key_mgmt,
5634 NULL);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08005635 }
5636
5637 if (!sm->cur_pmksa) {
5638 wpa_printf(MSG_DEBUG,
5639 "FILS: No remaining options to continue FILS authentication");
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005640 goto fail;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08005641 }
5642
Hai Shalom60840252021-02-19 19:02:11 -08005643 if (sm->force_kdk_derivation ||
Hai Shalomc1a21442022-02-04 13:43:00 -08005644 (sm->secure_ltf &&
5645 ieee802_11_rsnx_capab(sm->ap_rsnxe, WLAN_RSNX_CAPAB_SECURE_LTF)))
Hai Shalom60840252021-02-19 19:02:11 -08005646 kdk_len = WPA_KDK_MAX_LEN;
5647 else
5648 kdk_len = 0;
5649
Sunil Ravi77d572f2023-01-17 23:58:31 +00005650 if (fils_pmk_to_ptk(sm->pmk, sm->pmk_len, sm->own_addr,
5651 wpa_sm_get_auth_addr(sm),
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005652 sm->fils_nonce, sm->fils_anonce,
5653 dh_ss ? wpabuf_head(dh_ss) : NULL,
5654 dh_ss ? wpabuf_len(dh_ss) : 0,
5655 &sm->ptk, ick, &ick_len,
5656 sm->key_mgmt, sm->pairwise_cipher,
Hai Shalom60840252021-02-19 19:02:11 -08005657 sm->fils_ft, &sm->fils_ft_len,
5658 kdk_len) < 0) {
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08005659 wpa_printf(MSG_DEBUG, "FILS: Failed to derive PTK");
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005660 goto fail;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08005661 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005662
Sunil Ravi89eba102022-09-13 21:04:37 -07005663#ifdef CONFIG_PASN
5664 if (sm->secure_ltf &&
5665 ieee802_11_rsnx_capab(sm->ap_rsnxe, WLAN_RSNX_CAPAB_SECURE_LTF) &&
5666 wpa_ltf_keyseed(&sm->ptk, sm->key_mgmt, sm->pairwise_cipher)) {
5667 wpa_printf(MSG_DEBUG, "FILS: Failed to derive LTF keyseed");
5668 goto fail;
5669 }
5670#endif /* CONFIG_PASN */
5671
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005672 wpabuf_clear_free(dh_ss);
5673 dh_ss = NULL;
5674
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08005675 sm->ptk_set = 1;
5676 sm->tptk_set = 0;
5677 os_memset(&sm->tptk, 0, sizeof(sm->tptk));
5678
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005679#ifdef CONFIG_FILS_SK_PFS
5680 if (sm->fils_dh_group) {
5681 if (!sm->fils_ecdh) {
5682 wpa_printf(MSG_INFO, "FILS: ECDH not initialized");
5683 goto fail;
5684 }
5685 pub = crypto_ecdh_get_pubkey(sm->fils_ecdh, 1);
5686 if (!pub)
5687 goto fail;
5688 wpa_hexdump_buf(MSG_DEBUG, "FILS: gSTA", pub);
5689 g_sta = wpabuf_head(pub);
5690 g_sta_len = wpabuf_len(pub);
5691 if (!g_ap) {
5692 wpa_printf(MSG_INFO, "FILS: gAP not available");
5693 goto fail;
5694 }
5695 wpa_hexdump(MSG_DEBUG, "FILS: gAP", g_ap, g_ap_len);
5696 }
5697#endif /* CONFIG_FILS_SK_PFS */
5698
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08005699 res = fils_key_auth_sk(ick, ick_len, sm->fils_nonce,
5700 sm->fils_anonce, sm->own_addr, sm->bssid,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005701 g_sta, g_sta_len, g_ap, g_ap_len,
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08005702 sm->key_mgmt, sm->fils_key_auth_sta,
5703 sm->fils_key_auth_ap,
5704 &sm->fils_key_auth_len);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005705 wpabuf_free(pub);
Hai Shalom81f62d82019-07-22 12:10:00 -07005706 forced_memzero(ick, sizeof(ick));
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08005707 return res;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005708fail:
5709 wpabuf_free(pub);
5710 wpabuf_clear_free(dh_ss);
5711 return -1;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08005712}
5713
5714
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005715#ifdef CONFIG_IEEE80211R
5716static int fils_ft_build_assoc_req_rsne(struct wpa_sm *sm, struct wpabuf *buf)
5717{
5718 struct rsn_ie_hdr *rsnie;
5719 u16 capab;
5720 u8 *pos;
Roshan Pius3a1667e2018-07-03 15:17:14 -07005721 int use_sha384 = wpa_key_mgmt_sha384(sm->key_mgmt);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005722
5723 /* RSNIE[PMKR0Name/PMKR1Name] */
5724 rsnie = wpabuf_put(buf, sizeof(*rsnie));
5725 rsnie->elem_id = WLAN_EID_RSN;
5726 WPA_PUT_LE16(rsnie->version, RSN_VERSION);
5727
5728 /* Group Suite Selector */
5729 if (!wpa_cipher_valid_group(sm->group_cipher)) {
5730 wpa_printf(MSG_WARNING, "FT: Invalid group cipher (%d)",
5731 sm->group_cipher);
5732 return -1;
5733 }
5734 pos = wpabuf_put(buf, RSN_SELECTOR_LEN);
5735 RSN_SELECTOR_PUT(pos, wpa_cipher_to_suite(WPA_PROTO_RSN,
5736 sm->group_cipher));
5737
5738 /* Pairwise Suite Count */
5739 wpabuf_put_le16(buf, 1);
5740
5741 /* Pairwise Suite List */
5742 if (!wpa_cipher_valid_pairwise(sm->pairwise_cipher)) {
5743 wpa_printf(MSG_WARNING, "FT: Invalid pairwise cipher (%d)",
5744 sm->pairwise_cipher);
5745 return -1;
5746 }
5747 pos = wpabuf_put(buf, RSN_SELECTOR_LEN);
5748 RSN_SELECTOR_PUT(pos, wpa_cipher_to_suite(WPA_PROTO_RSN,
5749 sm->pairwise_cipher));
5750
5751 /* Authenticated Key Management Suite Count */
5752 wpabuf_put_le16(buf, 1);
5753
5754 /* Authenticated Key Management Suite List */
5755 pos = wpabuf_put(buf, RSN_SELECTOR_LEN);
5756 if (sm->key_mgmt == WPA_KEY_MGMT_FT_FILS_SHA256)
5757 RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_FT_FILS_SHA256);
5758 else if (sm->key_mgmt == WPA_KEY_MGMT_FT_FILS_SHA384)
5759 RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_FT_FILS_SHA384);
5760 else {
5761 wpa_printf(MSG_WARNING,
5762 "FILS+FT: Invalid key management type (%d)",
5763 sm->key_mgmt);
5764 return -1;
5765 }
5766
5767 /* RSN Capabilities */
5768 capab = 0;
Hai Shalomc3565922019-10-28 11:58:20 -07005769 if (sm->mfp)
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005770 capab |= WPA_CAPABILITY_MFPC;
Hai Shalomc3565922019-10-28 11:58:20 -07005771 if (sm->mfp == 2)
5772 capab |= WPA_CAPABILITY_MFPR;
Hai Shalom74f70d42019-02-11 14:42:39 -08005773 if (sm->ocv)
5774 capab |= WPA_CAPABILITY_OCVC;
Hai Shalomfdcde762020-04-02 11:19:20 -07005775 if (sm->ext_key_id)
5776 capab |= WPA_CAPABILITY_EXT_KEY_ID_FOR_UNICAST;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005777 wpabuf_put_le16(buf, capab);
5778
5779 /* PMKID Count */
5780 wpabuf_put_le16(buf, 1);
5781
5782 /* PMKID List [PMKR1Name] */
5783 wpa_hexdump_key(MSG_DEBUG, "FILS+FT: XXKey (FILS-FT)",
5784 sm->fils_ft, sm->fils_ft_len);
5785 wpa_hexdump_ascii(MSG_DEBUG, "FILS+FT: SSID", sm->ssid, sm->ssid_len);
5786 wpa_hexdump(MSG_DEBUG, "FILS+FT: MDID",
5787 sm->mobility_domain, MOBILITY_DOMAIN_ID_LEN);
5788 wpa_hexdump_ascii(MSG_DEBUG, "FILS+FT: R0KH-ID",
5789 sm->r0kh_id, sm->r0kh_id_len);
5790 if (wpa_derive_pmk_r0(sm->fils_ft, sm->fils_ft_len, sm->ssid,
5791 sm->ssid_len, sm->mobility_domain,
5792 sm->r0kh_id, sm->r0kh_id_len, sm->own_addr,
Sunil Ravi77d572f2023-01-17 23:58:31 +00005793 sm->pmk_r0, sm->pmk_r0_name, sm->key_mgmt) < 0) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005794 wpa_printf(MSG_WARNING, "FILS+FT: Could not derive PMK-R0");
5795 return -1;
5796 }
Sunil Ravi77d572f2023-01-17 23:58:31 +00005797 if (wpa_key_mgmt_sae_ext_key(sm->key_mgmt))
5798 sm->pmk_r0_len = sm->fils_ft_len;
5799 else
5800 sm->pmk_r0_len = use_sha384 ? SHA384_MAC_LEN : PMK_LEN;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005801 wpa_printf(MSG_DEBUG, "FILS+FT: R1KH-ID: " MACSTR,
5802 MAC2STR(sm->r1kh_id));
5803 pos = wpabuf_put(buf, WPA_PMK_NAME_LEN);
5804 if (wpa_derive_pmk_r1_name(sm->pmk_r0_name, sm->r1kh_id, sm->own_addr,
Sunil Ravi77d572f2023-01-17 23:58:31 +00005805 sm->pmk_r1_name, sm->fils_ft_len) < 0) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005806 wpa_printf(MSG_WARNING, "FILS+FT: Could not derive PMKR1Name");
5807 return -1;
5808 }
Hai Shalom021b0b52019-04-10 11:17:58 -07005809 os_memcpy(pos, sm->pmk_r1_name, WPA_PMK_NAME_LEN);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005810
Sunil Ravi77d572f2023-01-17 23:58:31 +00005811 os_memcpy(sm->key_mobility_domain, sm->mobility_domain,
5812 MOBILITY_DOMAIN_ID_LEN);
5813
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005814 if (sm->mgmt_group_cipher == WPA_CIPHER_AES_128_CMAC) {
5815 /* Management Group Cipher Suite */
5816 pos = wpabuf_put(buf, RSN_SELECTOR_LEN);
5817 RSN_SELECTOR_PUT(pos, RSN_CIPHER_SUITE_AES_128_CMAC);
5818 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005819
5820 rsnie->len = ((u8 *) wpabuf_put(buf, 0) - (u8 *) rsnie) - 2;
5821 return 0;
5822}
5823#endif /* CONFIG_IEEE80211R */
5824
5825
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08005826struct wpabuf * fils_build_assoc_req(struct wpa_sm *sm, const u8 **kek,
5827 size_t *kek_len, const u8 **snonce,
Paul Stewart092955c2017-02-06 09:13:09 -08005828 const u8 **anonce,
5829 const struct wpabuf **hlp,
5830 unsigned int num_hlp)
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08005831{
5832 struct wpabuf *buf;
Paul Stewart092955c2017-02-06 09:13:09 -08005833 size_t len;
5834 unsigned int i;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08005835
Paul Stewart092955c2017-02-06 09:13:09 -08005836 len = 1000;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005837#ifdef CONFIG_IEEE80211R
5838 if (sm->fils_ft_ies)
5839 len += wpabuf_len(sm->fils_ft_ies);
5840 if (wpa_key_mgmt_ft(sm->key_mgmt))
5841 len += 256;
5842#endif /* CONFIG_IEEE80211R */
Paul Stewart092955c2017-02-06 09:13:09 -08005843 for (i = 0; hlp && i < num_hlp; i++)
5844 len += 10 + wpabuf_len(hlp[i]);
5845 buf = wpabuf_alloc(len);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08005846 if (!buf)
5847 return NULL;
5848
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005849#ifdef CONFIG_IEEE80211R
5850 if (wpa_key_mgmt_ft(sm->key_mgmt) && sm->fils_ft_ies) {
5851 /* MDE and FTE when using FILS+FT */
5852 wpabuf_put_buf(buf, sm->fils_ft_ies);
5853 /* RSNE with PMKR1Name in PMKID field */
5854 if (fils_ft_build_assoc_req_rsne(sm, buf) < 0) {
5855 wpabuf_free(buf);
5856 return NULL;
5857 }
5858 }
5859#endif /* CONFIG_IEEE80211R */
5860
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08005861 /* FILS Session */
5862 wpabuf_put_u8(buf, WLAN_EID_EXTENSION); /* Element ID */
5863 wpabuf_put_u8(buf, 1 + FILS_SESSION_LEN); /* Length */
5864 /* Element ID Extension */
5865 wpabuf_put_u8(buf, WLAN_EID_EXT_FILS_SESSION);
5866 wpabuf_put_data(buf, sm->fils_session, FILS_SESSION_LEN);
5867
5868 /* Everything after FILS Session element gets encrypted in the driver
5869 * with KEK. The buffer returned from here is the plaintext version. */
5870
5871 /* TODO: FILS Public Key */
5872
5873 /* FILS Key Confirm */
5874 wpabuf_put_u8(buf, WLAN_EID_EXTENSION); /* Element ID */
5875 wpabuf_put_u8(buf, 1 + sm->fils_key_auth_len); /* Length */
5876 /* Element ID Extension */
5877 wpabuf_put_u8(buf, WLAN_EID_EXT_FILS_KEY_CONFIRM);
5878 wpabuf_put_data(buf, sm->fils_key_auth_sta, sm->fils_key_auth_len);
5879
Paul Stewart092955c2017-02-06 09:13:09 -08005880 /* FILS HLP Container */
5881 for (i = 0; hlp && i < num_hlp; i++) {
5882 const u8 *pos = wpabuf_head(hlp[i]);
5883 size_t left = wpabuf_len(hlp[i]);
5884
5885 wpabuf_put_u8(buf, WLAN_EID_EXTENSION); /* Element ID */
5886 if (left <= 254)
5887 len = 1 + left;
5888 else
5889 len = 255;
5890 wpabuf_put_u8(buf, len); /* Length */
5891 /* Element ID Extension */
5892 wpabuf_put_u8(buf, WLAN_EID_EXT_FILS_HLP_CONTAINER);
5893 /* Destination MAC Address, Source MAC Address, HLP Packet.
5894 * HLP Packet is in MSDU format (i.e., included the LLC/SNAP
5895 * header when LPD is used). */
5896 wpabuf_put_data(buf, pos, len - 1);
5897 pos += len - 1;
5898 left -= len - 1;
5899 while (left) {
5900 wpabuf_put_u8(buf, WLAN_EID_FRAGMENT);
5901 len = left > 255 ? 255 : left;
5902 wpabuf_put_u8(buf, len);
5903 wpabuf_put_data(buf, pos, len);
5904 pos += len;
5905 left -= len;
5906 }
5907 }
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08005908
5909 /* TODO: FILS IP Address Assignment */
5910
Hai Shalom74f70d42019-02-11 14:42:39 -08005911#ifdef CONFIG_OCV
5912 if (wpa_sm_ocv_enabled(sm)) {
5913 struct wpa_channel_info ci;
5914 u8 *pos;
5915
5916 if (wpa_sm_channel_info(sm, &ci) != 0) {
5917 wpa_printf(MSG_WARNING,
5918 "FILS: Failed to get channel info for OCI element");
5919 wpabuf_free(buf);
5920 return NULL;
5921 }
Hai Shalom899fcc72020-10-19 14:38:18 -07005922#ifdef CONFIG_TESTING_OPTIONS
5923 if (sm->oci_freq_override_fils_assoc) {
5924 wpa_printf(MSG_INFO,
5925 "TEST: Override OCI KDE frequency %d -> %d MHz",
5926 ci.frequency,
5927 sm->oci_freq_override_fils_assoc);
5928 ci.frequency = sm->oci_freq_override_fils_assoc;
5929 }
5930#endif /* CONFIG_TESTING_OPTIONS */
Hai Shalom74f70d42019-02-11 14:42:39 -08005931
5932 pos = wpabuf_put(buf, OCV_OCI_EXTENDED_LEN);
5933 if (ocv_insert_extended_oci(&ci, pos) < 0) {
5934 wpabuf_free(buf);
5935 return NULL;
5936 }
5937 }
5938#endif /* CONFIG_OCV */
5939
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08005940 wpa_hexdump_buf(MSG_DEBUG, "FILS: Association Request plaintext", buf);
5941
5942 *kek = sm->ptk.kek;
5943 *kek_len = sm->ptk.kek_len;
5944 wpa_hexdump_key(MSG_DEBUG, "FILS: KEK for AEAD", *kek, *kek_len);
5945 *snonce = sm->fils_nonce;
5946 wpa_hexdump(MSG_DEBUG, "FILS: SNonce for AEAD AAD",
5947 *snonce, FILS_NONCE_LEN);
5948 *anonce = sm->fils_anonce;
5949 wpa_hexdump(MSG_DEBUG, "FILS: ANonce for AEAD AAD",
5950 *anonce, FILS_NONCE_LEN);
5951
5952 return buf;
5953}
5954
5955
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08005956static void fils_process_hlp_resp(struct wpa_sm *sm, const u8 *resp, size_t len)
5957{
5958 const u8 *pos, *end;
5959
5960 wpa_hexdump(MSG_MSGDUMP, "FILS: HLP response", resp, len);
5961 if (len < 2 * ETH_ALEN)
5962 return;
5963 pos = resp + 2 * ETH_ALEN;
5964 end = resp + len;
5965 if (end - pos >= 6 &&
5966 os_memcmp(pos, "\xaa\xaa\x03\x00\x00\x00", 6) == 0)
5967 pos += 6; /* Remove SNAP/LLC header */
5968 wpa_sm_fils_hlp_rx(sm, resp, resp + ETH_ALEN, pos, end - pos);
5969}
5970
5971
5972static void fils_process_hlp_container(struct wpa_sm *sm, const u8 *pos,
5973 size_t len)
5974{
5975 const u8 *end = pos + len;
5976 u8 *tmp, *tmp_pos;
5977
5978 /* Check if there are any FILS HLP Container elements */
5979 while (end - pos >= 2) {
5980 if (2 + pos[1] > end - pos)
5981 return;
5982 if (pos[0] == WLAN_EID_EXTENSION &&
5983 pos[1] >= 1 + 2 * ETH_ALEN &&
5984 pos[2] == WLAN_EID_EXT_FILS_HLP_CONTAINER)
5985 break;
5986 pos += 2 + pos[1];
5987 }
5988 if (end - pos < 2)
5989 return; /* No FILS HLP Container elements */
5990
5991 tmp = os_malloc(end - pos);
5992 if (!tmp)
5993 return;
5994
5995 while (end - pos >= 2) {
5996 if (2 + pos[1] > end - pos ||
5997 pos[0] != WLAN_EID_EXTENSION ||
5998 pos[1] < 1 + 2 * ETH_ALEN ||
5999 pos[2] != WLAN_EID_EXT_FILS_HLP_CONTAINER)
6000 break;
6001 tmp_pos = tmp;
6002 os_memcpy(tmp_pos, pos + 3, pos[1] - 1);
6003 tmp_pos += pos[1] - 1;
6004 pos += 2 + pos[1];
6005
6006 /* Add possible fragments */
6007 while (end - pos >= 2 && pos[0] == WLAN_EID_FRAGMENT &&
6008 2 + pos[1] <= end - pos) {
6009 os_memcpy(tmp_pos, pos + 2, pos[1]);
6010 tmp_pos += pos[1];
6011 pos += 2 + pos[1];
6012 }
6013
6014 fils_process_hlp_resp(sm, tmp, tmp_pos - tmp);
6015 }
6016
6017 os_free(tmp);
6018}
6019
6020
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08006021int fils_process_assoc_resp(struct wpa_sm *sm, const u8 *resp, size_t len)
6022{
6023 const struct ieee80211_mgmt *mgmt;
6024 const u8 *end, *ie_start;
6025 struct ieee802_11_elems elems;
6026 int keylen, rsclen;
6027 enum wpa_alg alg;
6028 struct wpa_gtk_data gd;
6029 int maxkeylen;
6030 struct wpa_eapol_ie_parse kde;
6031
6032 if (!sm || !sm->ptk_set) {
6033 wpa_printf(MSG_DEBUG, "FILS: No KEK available");
6034 return -1;
6035 }
6036
6037 if (!wpa_key_mgmt_fils(sm->key_mgmt)) {
6038 wpa_printf(MSG_DEBUG, "FILS: Not a FILS AKM");
6039 return -1;
6040 }
6041
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006042 if (sm->fils_completed) {
6043 wpa_printf(MSG_DEBUG,
6044 "FILS: Association has already been completed for this FILS authentication - ignore unexpected retransmission");
6045 return -1;
6046 }
6047
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08006048 wpa_hexdump(MSG_DEBUG, "FILS: (Re)Association Response frame",
6049 resp, len);
6050
6051 mgmt = (const struct ieee80211_mgmt *) resp;
6052 if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.assoc_resp))
6053 return -1;
6054
6055 end = resp + len;
6056 /* Same offset for Association Response and Reassociation Response */
6057 ie_start = mgmt->u.assoc_resp.variable;
6058
6059 if (ieee802_11_parse_elems(ie_start, end - ie_start, &elems, 1) ==
6060 ParseFailed) {
6061 wpa_printf(MSG_DEBUG,
6062 "FILS: Failed to parse decrypted elements");
6063 goto fail;
6064 }
6065
6066 if (!elems.fils_session) {
6067 wpa_printf(MSG_DEBUG, "FILS: No FILS Session element");
6068 return -1;
6069 }
6070 if (os_memcmp(elems.fils_session, sm->fils_session,
6071 FILS_SESSION_LEN) != 0) {
6072 wpa_printf(MSG_DEBUG, "FILS: FILS Session mismatch");
6073 wpa_hexdump(MSG_DEBUG, "FILS: Received FILS Session",
6074 elems.fils_session, FILS_SESSION_LEN);
6075 wpa_hexdump(MSG_DEBUG, "FILS: Expected FILS Session",
6076 sm->fils_session, FILS_SESSION_LEN);
6077 }
6078
Hai Shalom81f62d82019-07-22 12:10:00 -07006079 if (!elems.rsn_ie) {
6080 wpa_printf(MSG_DEBUG,
6081 "FILS: No RSNE in (Re)Association Response");
6082 /* As an interop workaround, allow this for now since IEEE Std
6083 * 802.11ai-2016 did not include all the needed changes to make
6084 * a FILS AP include RSNE in the frame. This workaround might
6085 * eventually be removed and replaced with rejection (goto fail)
6086 * to follow a strict interpretation of the standard. */
6087 } else if (wpa_compare_rsn_ie(wpa_key_mgmt_ft(sm->key_mgmt),
6088 sm->ap_rsn_ie, sm->ap_rsn_ie_len,
6089 elems.rsn_ie - 2, elems.rsn_ie_len + 2)) {
6090 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
6091 "FILS: RSNE mismatch between Beacon/Probe Response and (Re)Association Response");
6092 wpa_hexdump(MSG_DEBUG, "FILS: RSNE in Beacon/Probe Response",
6093 sm->ap_rsn_ie, sm->ap_rsn_ie_len);
6094 wpa_hexdump(MSG_DEBUG, "FILS: RSNE in (Re)Association Response",
6095 elems.rsn_ie, elems.rsn_ie_len);
6096 goto fail;
6097 }
6098
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08006099 /* TODO: FILS Public Key */
6100
6101 if (!elems.fils_key_confirm) {
6102 wpa_printf(MSG_DEBUG, "FILS: No FILS Key Confirm element");
6103 goto fail;
6104 }
6105 if (elems.fils_key_confirm_len != sm->fils_key_auth_len) {
6106 wpa_printf(MSG_DEBUG,
6107 "FILS: Unexpected Key-Auth length %d (expected %d)",
6108 elems.fils_key_confirm_len,
6109 (int) sm->fils_key_auth_len);
6110 goto fail;
6111 }
6112 if (os_memcmp(elems.fils_key_confirm, sm->fils_key_auth_ap,
6113 sm->fils_key_auth_len) != 0) {
6114 wpa_printf(MSG_DEBUG, "FILS: Key-Auth mismatch");
6115 wpa_hexdump(MSG_DEBUG, "FILS: Received Key-Auth",
6116 elems.fils_key_confirm,
6117 elems.fils_key_confirm_len);
6118 wpa_hexdump(MSG_DEBUG, "FILS: Expected Key-Auth",
6119 sm->fils_key_auth_ap, sm->fils_key_auth_len);
6120 goto fail;
6121 }
6122
Hai Shalom74f70d42019-02-11 14:42:39 -08006123#ifdef CONFIG_OCV
6124 if (wpa_sm_ocv_enabled(sm)) {
6125 struct wpa_channel_info ci;
6126
6127 if (wpa_sm_channel_info(sm, &ci) != 0) {
6128 wpa_printf(MSG_WARNING,
6129 "Failed to get channel info to validate received OCI in FILS (Re)Association Response frame");
6130 goto fail;
6131 }
6132
6133 if (ocv_verify_tx_params(elems.oci, elems.oci_len, &ci,
6134 channel_width_to_int(ci.chanwidth),
Hai Shalom899fcc72020-10-19 14:38:18 -07006135 ci.seg1_idx) != OCI_SUCCESS) {
6136 wpa_msg(sm->ctx->msg_ctx, MSG_INFO, OCV_FAILURE
6137 "addr=" MACSTR " frame=fils-assoc error=%s",
6138 MAC2STR(sm->bssid), ocv_errorstr);
Hai Shalom74f70d42019-02-11 14:42:39 -08006139 goto fail;
6140 }
6141 }
6142#endif /* CONFIG_OCV */
6143
Hai Shalom021b0b52019-04-10 11:17:58 -07006144#ifdef CONFIG_IEEE80211R
6145 if (wpa_key_mgmt_ft(sm->key_mgmt) && sm->fils_ft_ies) {
6146 struct wpa_ie_data rsn;
6147
6148 /* Check that PMKR1Name derived by the AP matches */
6149 if (!elems.rsn_ie ||
6150 wpa_parse_wpa_ie_rsn(elems.rsn_ie - 2, elems.rsn_ie_len + 2,
6151 &rsn) < 0 ||
6152 !rsn.pmkid || rsn.num_pmkid != 1 ||
6153 os_memcmp(rsn.pmkid, sm->pmk_r1_name,
6154 WPA_PMK_NAME_LEN) != 0) {
6155 wpa_printf(MSG_DEBUG,
6156 "FILS+FT: No RSNE[PMKR1Name] match in AssocResp");
6157 goto fail;
6158 }
6159 }
6160#endif /* CONFIG_IEEE80211R */
6161
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08006162 /* Key Delivery */
6163 if (!elems.key_delivery) {
6164 wpa_printf(MSG_DEBUG, "FILS: No Key Delivery element");
6165 goto fail;
6166 }
6167
6168 /* Parse GTK and set the key to the driver */
6169 os_memset(&gd, 0, sizeof(gd));
6170 if (wpa_supplicant_parse_ies(elems.key_delivery + WPA_KEY_RSC_LEN,
6171 elems.key_delivery_len - WPA_KEY_RSC_LEN,
6172 &kde) < 0) {
6173 wpa_printf(MSG_DEBUG, "FILS: Failed to parse KDEs");
6174 goto fail;
6175 }
6176 if (!kde.gtk) {
6177 wpa_printf(MSG_DEBUG, "FILS: No GTK KDE");
6178 goto fail;
6179 }
6180 maxkeylen = gd.gtk_len = kde.gtk_len - 2;
6181 if (wpa_supplicant_check_group_cipher(sm, sm->group_cipher,
6182 gd.gtk_len, maxkeylen,
6183 &gd.key_rsc_len, &gd.alg))
6184 goto fail;
6185
6186 wpa_hexdump_key(MSG_DEBUG, "FILS: Received GTK", kde.gtk, kde.gtk_len);
6187 gd.keyidx = kde.gtk[0] & 0x3;
6188 gd.tx = wpa_supplicant_gtk_tx_bit_workaround(sm,
6189 !!(kde.gtk[0] & BIT(2)));
6190 if (kde.gtk_len - 2 > sizeof(gd.gtk)) {
6191 wpa_printf(MSG_DEBUG, "FILS: Too long GTK in GTK KDE (len=%lu)",
6192 (unsigned long) kde.gtk_len - 2);
6193 goto fail;
6194 }
6195 os_memcpy(gd.gtk, kde.gtk + 2, kde.gtk_len - 2);
6196
6197 wpa_printf(MSG_DEBUG, "FILS: Set GTK to driver");
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006198 if (wpa_supplicant_install_gtk(sm, &gd, elems.key_delivery, 0) < 0) {
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08006199 wpa_printf(MSG_DEBUG, "FILS: Failed to set GTK");
6200 goto fail;
6201 }
6202
6203 if (ieee80211w_set_keys(sm, &kde) < 0) {
6204 wpa_printf(MSG_DEBUG, "FILS: Failed to set IGTK");
6205 goto fail;
6206 }
6207
6208 alg = wpa_cipher_to_alg(sm->pairwise_cipher);
6209 keylen = wpa_cipher_key_len(sm->pairwise_cipher);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006210 if (keylen <= 0 || (unsigned int) keylen != sm->ptk.tk_len) {
6211 wpa_printf(MSG_DEBUG, "FILS: TK length mismatch: %u != %lu",
6212 keylen, (long unsigned int) sm->ptk.tk_len);
6213 goto fail;
6214 }
Hai Shalomfdcde762020-04-02 11:19:20 -07006215
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08006216 rsclen = wpa_cipher_rsc_len(sm->pairwise_cipher);
6217 wpa_hexdump_key(MSG_DEBUG, "FILS: Set TK to driver",
6218 sm->ptk.tk, keylen);
Sunil Ravi77d572f2023-01-17 23:58:31 +00006219 if (wpa_sm_set_key(sm, -1, alg, wpa_sm_get_auth_addr(sm), 0, 1,
6220 null_rsc, rsclen,
Hai Shalomfdcde762020-04-02 11:19:20 -07006221 sm->ptk.tk, keylen, KEY_FLAG_PAIRWISE_RX_TX) < 0) {
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08006222 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
Sunil Ravi77d572f2023-01-17 23:58:31 +00006223 "FILS: Failed to set PTK to the driver (alg=%d keylen=%d auth_addr="
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08006224 MACSTR ")",
Sunil Ravi77d572f2023-01-17 23:58:31 +00006225 alg, keylen, MAC2STR(wpa_sm_get_auth_addr(sm)));
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08006226 goto fail;
6227 }
6228
Hai Shalom60840252021-02-19 19:02:11 -08006229 wpa_sm_store_ptk(sm, sm->bssid, sm->pairwise_cipher,
6230 sm->dot11RSNAConfigPMKLifetime, &sm->ptk);
6231
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08006232 /* TODO: TK could be cleared after auth frame exchange now that driver
6233 * takes care of association frame encryption/decryption. */
6234 /* TK is not needed anymore in supplicant */
6235 os_memset(sm->ptk.tk, 0, WPA_TK_MAX_LEN);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006236 sm->ptk.tk_len = 0;
Mathy Vanhoefc66556c2017-09-29 04:22:51 +02006237 sm->ptk.installed = 1;
Sunil8cd6f4d2022-06-28 18:40:46 +00006238 sm->tk_set = true;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08006239
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08006240 /* FILS HLP Container */
6241 fils_process_hlp_container(sm, ie_start, end - ie_start);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08006242
6243 /* TODO: FILS IP Address Assignment */
6244
6245 wpa_printf(MSG_DEBUG, "FILS: Auth+Assoc completed successfully");
6246 sm->fils_completed = 1;
Hai Shalom81f62d82019-07-22 12:10:00 -07006247 forced_memzero(&gd, sizeof(gd));
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08006248
Hai Shalomfdcde762020-04-02 11:19:20 -07006249 if (kde.transition_disable)
6250 wpa_sm_transition_disable(sm, kde.transition_disable[0]);
6251
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08006252 return 0;
6253fail:
Hai Shalom81f62d82019-07-22 12:10:00 -07006254 forced_memzero(&gd, sizeof(gd));
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08006255 return -1;
6256}
6257
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006258
6259void wpa_sm_set_reset_fils_completed(struct wpa_sm *sm, int set)
6260{
6261 if (sm)
6262 sm->fils_completed = !!set;
6263}
6264
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08006265#endif /* CONFIG_FILS */
6266
6267
6268int wpa_fils_is_completed(struct wpa_sm *sm)
6269{
6270#ifdef CONFIG_FILS
6271 return sm && sm->fils_completed;
6272#else /* CONFIG_FILS */
6273 return 0;
6274#endif /* CONFIG_FILS */
6275}
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006276
6277
6278#ifdef CONFIG_OWE
6279
6280struct wpabuf * owe_build_assoc_req(struct wpa_sm *sm, u16 group)
6281{
6282 struct wpabuf *ie = NULL, *pub = NULL;
6283 size_t prime_len;
6284
6285 if (group == 19)
6286 prime_len = 32;
6287 else if (group == 20)
6288 prime_len = 48;
6289 else if (group == 21)
6290 prime_len = 66;
6291 else
6292 return NULL;
6293
6294 crypto_ecdh_deinit(sm->owe_ecdh);
6295 sm->owe_ecdh = crypto_ecdh_init(group);
6296 if (!sm->owe_ecdh)
6297 goto fail;
6298 sm->owe_group = group;
6299 pub = crypto_ecdh_get_pubkey(sm->owe_ecdh, 0);
6300 pub = wpabuf_zeropad(pub, prime_len);
6301 if (!pub)
6302 goto fail;
6303
6304 ie = wpabuf_alloc(5 + wpabuf_len(pub));
6305 if (!ie)
6306 goto fail;
6307 wpabuf_put_u8(ie, WLAN_EID_EXTENSION);
6308 wpabuf_put_u8(ie, 1 + 2 + wpabuf_len(pub));
6309 wpabuf_put_u8(ie, WLAN_EID_EXT_OWE_DH_PARAM);
6310 wpabuf_put_le16(ie, group);
6311 wpabuf_put_buf(ie, pub);
6312 wpabuf_free(pub);
6313 wpa_hexdump_buf(MSG_DEBUG, "OWE: Diffie-Hellman Parameter element",
6314 ie);
6315
6316 return ie;
6317fail:
6318 wpabuf_free(pub);
6319 crypto_ecdh_deinit(sm->owe_ecdh);
6320 sm->owe_ecdh = NULL;
6321 return NULL;
6322}
6323
6324
6325int owe_process_assoc_resp(struct wpa_sm *sm, const u8 *bssid,
6326 const u8 *resp_ies, size_t resp_ies_len)
6327{
6328 struct ieee802_11_elems elems;
6329 u16 group;
6330 struct wpabuf *secret, *pub, *hkey;
6331 int res;
6332 u8 prk[SHA512_MAC_LEN], pmkid[SHA512_MAC_LEN];
6333 const char *info = "OWE Key Generation";
6334 const u8 *addr[2];
6335 size_t len[2];
6336 size_t hash_len, prime_len;
6337 struct wpa_ie_data data;
6338
6339 if (!resp_ies ||
6340 ieee802_11_parse_elems(resp_ies, resp_ies_len, &elems, 1) ==
6341 ParseFailed) {
6342 wpa_printf(MSG_INFO,
6343 "OWE: Could not parse Association Response frame elements");
6344 return -1;
6345 }
6346
6347 if (sm->cur_pmksa && elems.rsn_ie &&
6348 wpa_parse_wpa_ie_rsn(elems.rsn_ie - 2, 2 + elems.rsn_ie_len,
6349 &data) == 0 &&
6350 data.num_pmkid == 1 && data.pmkid &&
6351 os_memcmp(sm->cur_pmksa->pmkid, data.pmkid, PMKID_LEN) == 0) {
6352 wpa_printf(MSG_DEBUG, "OWE: Use PMKSA caching");
6353 wpa_sm_set_pmk_from_pmksa(sm);
6354 return 0;
6355 }
6356
6357 if (!elems.owe_dh) {
6358 wpa_printf(MSG_INFO,
6359 "OWE: No Diffie-Hellman Parameter element found in Association Response frame");
6360 return -1;
6361 }
6362
6363 group = WPA_GET_LE16(elems.owe_dh);
6364 if (group != sm->owe_group) {
6365 wpa_printf(MSG_INFO,
6366 "OWE: Unexpected Diffie-Hellman group in response: %u",
6367 group);
6368 return -1;
6369 }
6370
6371 if (!sm->owe_ecdh) {
6372 wpa_printf(MSG_INFO, "OWE: No ECDH state available");
6373 return -1;
6374 }
6375
6376 if (group == 19)
6377 prime_len = 32;
6378 else if (group == 20)
6379 prime_len = 48;
6380 else if (group == 21)
6381 prime_len = 66;
6382 else
6383 return -1;
6384
6385 secret = crypto_ecdh_set_peerkey(sm->owe_ecdh, 0,
6386 elems.owe_dh + 2,
6387 elems.owe_dh_len - 2);
6388 secret = wpabuf_zeropad(secret, prime_len);
6389 if (!secret) {
6390 wpa_printf(MSG_DEBUG, "OWE: Invalid peer DH public key");
6391 return -1;
6392 }
6393 wpa_hexdump_buf_key(MSG_DEBUG, "OWE: DH shared secret", secret);
6394
6395 /* prk = HKDF-extract(C | A | group, z) */
6396
6397 pub = crypto_ecdh_get_pubkey(sm->owe_ecdh, 0);
6398 if (!pub) {
6399 wpabuf_clear_free(secret);
6400 return -1;
6401 }
6402
6403 /* PMKID = Truncate-128(Hash(C | A)) */
6404 addr[0] = wpabuf_head(pub);
6405 len[0] = wpabuf_len(pub);
6406 addr[1] = elems.owe_dh + 2;
6407 len[1] = elems.owe_dh_len - 2;
6408 if (group == 19) {
6409 res = sha256_vector(2, addr, len, pmkid);
6410 hash_len = SHA256_MAC_LEN;
6411 } else if (group == 20) {
6412 res = sha384_vector(2, addr, len, pmkid);
6413 hash_len = SHA384_MAC_LEN;
6414 } else if (group == 21) {
6415 res = sha512_vector(2, addr, len, pmkid);
6416 hash_len = SHA512_MAC_LEN;
6417 } else {
6418 res = -1;
6419 hash_len = 0;
6420 }
6421 pub = wpabuf_zeropad(pub, prime_len);
6422 if (res < 0 || !pub) {
6423 wpabuf_free(pub);
6424 wpabuf_clear_free(secret);
6425 return -1;
6426 }
6427
6428 hkey = wpabuf_alloc(wpabuf_len(pub) + elems.owe_dh_len - 2 + 2);
6429 if (!hkey) {
6430 wpabuf_free(pub);
6431 wpabuf_clear_free(secret);
6432 return -1;
6433 }
6434
6435 wpabuf_put_buf(hkey, pub); /* C */
6436 wpabuf_free(pub);
6437 wpabuf_put_data(hkey, elems.owe_dh + 2, elems.owe_dh_len - 2); /* A */
6438 wpabuf_put_le16(hkey, sm->owe_group); /* group */
6439 if (group == 19)
6440 res = hmac_sha256(wpabuf_head(hkey), wpabuf_len(hkey),
6441 wpabuf_head(secret), wpabuf_len(secret), prk);
6442 else if (group == 20)
6443 res = hmac_sha384(wpabuf_head(hkey), wpabuf_len(hkey),
6444 wpabuf_head(secret), wpabuf_len(secret), prk);
6445 else if (group == 21)
6446 res = hmac_sha512(wpabuf_head(hkey), wpabuf_len(hkey),
6447 wpabuf_head(secret), wpabuf_len(secret), prk);
6448 wpabuf_clear_free(hkey);
6449 wpabuf_clear_free(secret);
6450 if (res < 0)
6451 return -1;
6452
6453 wpa_hexdump_key(MSG_DEBUG, "OWE: prk", prk, hash_len);
6454
6455 /* PMK = HKDF-expand(prk, "OWE Key Generation", n) */
6456
6457 if (group == 19)
6458 res = hmac_sha256_kdf(prk, hash_len, NULL, (const u8 *) info,
6459 os_strlen(info), sm->pmk, hash_len);
6460 else if (group == 20)
6461 res = hmac_sha384_kdf(prk, hash_len, NULL, (const u8 *) info,
6462 os_strlen(info), sm->pmk, hash_len);
6463 else if (group == 21)
6464 res = hmac_sha512_kdf(prk, hash_len, NULL, (const u8 *) info,
6465 os_strlen(info), sm->pmk, hash_len);
Hai Shalom81f62d82019-07-22 12:10:00 -07006466 forced_memzero(prk, SHA512_MAC_LEN);
Roshan Pius3a1667e2018-07-03 15:17:14 -07006467 if (res < 0) {
6468 sm->pmk_len = 0;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006469 return -1;
Roshan Pius3a1667e2018-07-03 15:17:14 -07006470 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07006471 sm->pmk_len = hash_len;
6472
6473 wpa_hexdump_key(MSG_DEBUG, "OWE: PMK", sm->pmk, sm->pmk_len);
6474 wpa_hexdump(MSG_DEBUG, "OWE: PMKID", pmkid, PMKID_LEN);
6475 pmksa_cache_add(sm->pmksa, sm->pmk, sm->pmk_len, pmkid, NULL, 0,
6476 bssid, sm->own_addr, sm->network_ctx, sm->key_mgmt,
6477 NULL);
6478
6479 return 0;
6480}
6481
6482#endif /* CONFIG_OWE */
6483
6484
6485void wpa_sm_set_fils_cache_id(struct wpa_sm *sm, const u8 *fils_cache_id)
6486{
6487#ifdef CONFIG_FILS
6488 if (sm && fils_cache_id) {
6489 sm->fils_cache_id_set = 1;
6490 os_memcpy(sm->fils_cache_id, fils_cache_id, FILS_CACHE_ID_LEN);
6491 }
6492#endif /* CONFIG_FILS */
6493}
Hai Shalom021b0b52019-04-10 11:17:58 -07006494
6495
6496#ifdef CONFIG_DPP2
6497void wpa_sm_set_dpp_z(struct wpa_sm *sm, const struct wpabuf *z)
6498{
6499 if (sm) {
6500 wpabuf_clear_free(sm->dpp_z);
6501 sm->dpp_z = z ? wpabuf_dup(z) : NULL;
6502 }
6503}
6504#endif /* CONFIG_DPP2 */
Hai Shalom60840252021-02-19 19:02:11 -08006505
6506
6507#ifdef CONFIG_PASN
Sunil Ravi89eba102022-09-13 21:04:37 -07006508
Sunil Ravi89eba102022-09-13 21:04:37 -07006509void wpa_pasn_sm_set_caps(struct wpa_sm *sm, unsigned int flags2)
6510{
6511 if (flags2 & WPA_DRIVER_FLAGS2_SEC_LTF_STA)
6512 sm->secure_ltf = 1;
6513 if (flags2 & WPA_DRIVER_FLAGS2_SEC_RTT_STA)
6514 sm->secure_rtt = 1;
6515 if (flags2 & WPA_DRIVER_FLAGS2_PROT_RANGE_NEG_STA)
6516 sm->prot_range_neg = 1;
6517}
6518
Hai Shalom60840252021-02-19 19:02:11 -08006519#endif /* CONFIG_PASN */
Hai Shalomc1a21442022-02-04 13:43:00 -08006520
6521
6522void wpa_sm_pmksa_cache_reconfig(struct wpa_sm *sm)
6523{
6524 if (sm)
6525 pmksa_cache_reconfig(sm->pmksa);
6526}
Sunil Ravi77d572f2023-01-17 23:58:31 +00006527
6528
6529struct rsn_pmksa_cache * wpa_sm_get_pmksa_cache(struct wpa_sm *sm)
6530{
6531 return sm ? sm->pmksa : NULL;
6532}
6533
6534
6535void wpa_sm_set_cur_pmksa(struct wpa_sm *sm,
6536 struct rsn_pmksa_cache_entry *entry)
6537{
6538 if (sm)
6539 sm->cur_pmksa = entry;
6540}