blob: 21f8b5b9d5b8c41c11c82a78ea3162ac3eb810d4 [file] [log] [blame]
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001/*
2 * WPA Supplicant - WPA state machine and EAPOL-Key processing
Roshan Pius3a1667e2018-07-03 15:17:14 -07003 * Copyright (c) 2003-2018, Jouni Malinen <j@w1.fi>
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004 * Copyright(c) 2015 Intel Deutschland GmbH
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005 *
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006 * This software may be distributed under the terms of the BSD license.
7 * See README for more details.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008 */
9
10#include "includes.h"
11
12#include "common.h"
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -080013#include "crypto/aes.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070014#include "crypto/aes_wrap.h"
15#include "crypto/crypto.h"
16#include "crypto/random.h"
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -080017#include "crypto/aes_siv.h"
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070018#include "crypto/sha256.h"
19#include "crypto/sha384.h"
20#include "crypto/sha512.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070021#include "common/ieee802_11_defs.h"
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -080022#include "common/ieee802_11_common.h"
Hai Shalom74f70d42019-02-11 14:42:39 -080023#include "common/ocv.h"
Hai Shalom4fbc08f2020-05-18 12:37:00 -070024#include "common/dpp.h"
Hai Shalom899fcc72020-10-19 14:38:18 -070025#include "common/wpa_ctrl.h"
Paul Stewart092955c2017-02-06 09:13:09 -080026#include "eap_common/eap_defs.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070027#include "eapol_supp/eapol_supp_sm.h"
Hai Shalom74f70d42019-02-11 14:42:39 -080028#include "drivers/driver.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070029#include "wpa.h"
30#include "eloop.h"
31#include "preauth.h"
32#include "pmksa_cache.h"
33#include "wpa_i.h"
34#include "wpa_ie.h"
Mir Alieaaf04e2021-06-07 12:17:29 +053035#include "wpa_supplicant_i.h"
36#include "driver_i.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070037
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080038static const u8 null_rsc[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
39
40
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070041/**
42 * wpa_eapol_key_send - Send WPA/RSN EAPOL-Key message
43 * @sm: Pointer to WPA state machine data from wpa_sm_init()
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -080044 * @ptk: PTK for Key Confirmation/Encryption Key
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070045 * @ver: Version field from Key Info
46 * @dest: Destination address for the frame
47 * @proto: Ethertype (usually ETH_P_EAPOL)
48 * @msg: EAPOL-Key message
49 * @msg_len: Length of message
50 * @key_mic: Pointer to the buffer to which the EAPOL-Key MIC is written
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080051 * Returns: >= 0 on success, < 0 on failure
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070052 */
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -080053int wpa_eapol_key_send(struct wpa_sm *sm, struct wpa_ptk *ptk,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080054 int ver, const u8 *dest, u16 proto,
55 u8 *msg, size_t msg_len, u8 *key_mic)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070056{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080057 int ret = -1;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070058 size_t mic_len = wpa_mic_len(sm->key_mgmt, sm->pmk_len);
Dmitry Shmidt807291d2015-01-27 13:40:23 -080059
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070060 wpa_printf(MSG_DEBUG, "WPA: Send EAPOL-Key frame to " MACSTR
61 " ver=%d mic_len=%d key_mgmt=0x%x",
62 MAC2STR(dest), ver, (int) mic_len, sm->key_mgmt);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070063 if (is_zero_ether_addr(dest) && is_zero_ether_addr(sm->bssid)) {
64 /*
65 * Association event was not yet received; try to fetch
66 * BSSID from the driver.
67 */
68 if (wpa_sm_get_bssid(sm, sm->bssid) < 0) {
69 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
70 "WPA: Failed to read BSSID for "
71 "EAPOL-Key destination address");
72 } else {
73 dest = sm->bssid;
74 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
75 "WPA: Use BSSID (" MACSTR
76 ") as the destination for EAPOL-Key",
77 MAC2STR(dest));
78 }
79 }
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -080080
81 if (mic_len) {
82 if (key_mic && (!ptk || !ptk->kck_len))
83 goto out;
84
85 if (key_mic &&
86 wpa_eapol_key_mic(ptk->kck, ptk->kck_len, sm->key_mgmt, ver,
87 msg, msg_len, key_mic)) {
88 wpa_msg(sm->ctx->msg_ctx, MSG_ERROR,
89 "WPA: Failed to generate EAPOL-Key version %d key_mgmt 0x%x MIC",
90 ver, sm->key_mgmt);
91 goto out;
92 }
Dmitry Shmidt29333592017-01-09 12:27:11 -080093 if (ptk)
94 wpa_hexdump_key(MSG_DEBUG, "WPA: KCK",
95 ptk->kck, ptk->kck_len);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -080096 wpa_hexdump(MSG_DEBUG, "WPA: Derived Key MIC",
97 key_mic, mic_len);
98 } else {
99#ifdef CONFIG_FILS
100 /* AEAD cipher - Key MIC field not used */
101 struct ieee802_1x_hdr *s_hdr, *hdr;
102 struct wpa_eapol_key *s_key, *key;
103 u8 *buf, *s_key_data, *key_data;
104 size_t buf_len = msg_len + AES_BLOCK_SIZE;
105 size_t key_data_len;
106 u16 eapol_len;
107 const u8 *aad[1];
108 size_t aad_len[1];
109
110 if (!ptk || !ptk->kek_len)
111 goto out;
112
113 key_data_len = msg_len - sizeof(struct ieee802_1x_hdr) -
114 sizeof(struct wpa_eapol_key) - 2;
115
116 buf = os_malloc(buf_len);
117 if (!buf)
118 goto out;
119
120 os_memcpy(buf, msg, msg_len);
121 hdr = (struct ieee802_1x_hdr *) buf;
122 key = (struct wpa_eapol_key *) (hdr + 1);
123 key_data = ((u8 *) (key + 1)) + 2;
124
125 /* Update EAPOL header to include AES-SIV overhead */
126 eapol_len = be_to_host16(hdr->length);
127 eapol_len += AES_BLOCK_SIZE;
128 hdr->length = host_to_be16(eapol_len);
129
130 /* Update Key Data Length field to include AES-SIV overhead */
131 WPA_PUT_BE16((u8 *) (key + 1), AES_BLOCK_SIZE + key_data_len);
132
133 s_hdr = (struct ieee802_1x_hdr *) msg;
134 s_key = (struct wpa_eapol_key *) (s_hdr + 1);
135 s_key_data = ((u8 *) (s_key + 1)) + 2;
136
137 wpa_hexdump_key(MSG_DEBUG, "WPA: Plaintext Key Data",
138 s_key_data, key_data_len);
139
140 wpa_hexdump_key(MSG_DEBUG, "WPA: KEK", ptk->kek, ptk->kek_len);
141 /* AES-SIV AAD from EAPOL protocol version field (inclusive) to
142 * to Key Data (exclusive). */
143 aad[0] = buf;
144 aad_len[0] = key_data - buf;
145 if (aes_siv_encrypt(ptk->kek, ptk->kek_len,
146 s_key_data, key_data_len,
147 1, aad, aad_len, key_data) < 0) {
148 os_free(buf);
149 goto out;
150 }
151
152 wpa_hexdump(MSG_DEBUG, "WPA: Encrypted Key Data from SIV",
153 key_data, AES_BLOCK_SIZE + key_data_len);
154
155 os_free(msg);
156 msg = buf;
157 msg_len = buf_len;
158#else /* CONFIG_FILS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700159 goto out;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800160#endif /* CONFIG_FILS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700161 }
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800162
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700163 wpa_hexdump(MSG_MSGDUMP, "WPA: TX EAPOL-Key", msg, msg_len);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800164 ret = wpa_sm_ether_send(sm, dest, proto, msg, msg_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700165 eapol_sm_notify_tx_eapol_key(sm->eapol);
166out:
167 os_free(msg);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800168 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700169}
170
171
172/**
173 * wpa_sm_key_request - Send EAPOL-Key Request
174 * @sm: Pointer to WPA state machine data from wpa_sm_init()
175 * @error: Indicate whether this is an Michael MIC error report
176 * @pairwise: 1 = error report for pairwise packet, 0 = for group packet
177 *
178 * Send an EAPOL-Key Request to the current authenticator. This function is
179 * used to request rekeying and it is usually called when a local Michael MIC
180 * failure is detected.
181 */
182void wpa_sm_key_request(struct wpa_sm *sm, int error, int pairwise)
183{
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800184 size_t mic_len, hdrlen, rlen;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700185 struct wpa_eapol_key *reply;
186 int key_info, ver;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800187 u8 bssid[ETH_ALEN], *rbuf, *key_mic, *mic;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700188
Hai Shalomfdcde762020-04-02 11:19:20 -0700189 if (pairwise && sm->wpa_deny_ptk0_rekey && !sm->use_ext_key_id &&
Sunil8cd6f4d2022-06-28 18:40:46 +0000190 wpa_sm_get_state(sm) == WPA_COMPLETED && !error) {
Hai Shalomfdcde762020-04-02 11:19:20 -0700191 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
192 "WPA: PTK0 rekey not allowed, reconnecting");
193 wpa_sm_reconnect(sm);
194 return;
195 }
196
Roshan Pius3a1667e2018-07-03 15:17:14 -0700197 if (wpa_use_akm_defined(sm->key_mgmt))
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800198 ver = WPA_KEY_INFO_TYPE_AKM_DEFINED;
199 else if (wpa_key_mgmt_ft(sm->key_mgmt) ||
200 wpa_key_mgmt_sha256(sm->key_mgmt))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700201 ver = WPA_KEY_INFO_TYPE_AES_128_CMAC;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700202 else if (sm->pairwise_cipher != WPA_CIPHER_TKIP)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700203 ver = WPA_KEY_INFO_TYPE_HMAC_SHA1_AES;
204 else
205 ver = WPA_KEY_INFO_TYPE_HMAC_MD5_RC4;
206
207 if (wpa_sm_get_bssid(sm, bssid) < 0) {
208 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
209 "Failed to read BSSID for EAPOL-Key request");
210 return;
211 }
212
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700213 mic_len = wpa_mic_len(sm->key_mgmt, sm->pmk_len);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800214 hdrlen = sizeof(*reply) + mic_len + 2;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700215 rbuf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_KEY, NULL,
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800216 hdrlen, &rlen, (void *) &reply);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700217 if (rbuf == NULL)
218 return;
219
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800220 reply->type = (sm->proto == WPA_PROTO_RSN ||
221 sm->proto == WPA_PROTO_OSEN) ?
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700222 EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA;
223 key_info = WPA_KEY_INFO_REQUEST | ver;
224 if (sm->ptk_set)
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800225 key_info |= WPA_KEY_INFO_SECURE;
226 if (sm->ptk_set && mic_len)
227 key_info |= WPA_KEY_INFO_MIC;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700228 if (error)
229 key_info |= WPA_KEY_INFO_ERROR;
230 if (pairwise)
231 key_info |= WPA_KEY_INFO_KEY_TYPE;
232 WPA_PUT_BE16(reply->key_info, key_info);
233 WPA_PUT_BE16(reply->key_length, 0);
234 os_memcpy(reply->replay_counter, sm->request_counter,
235 WPA_REPLAY_COUNTER_LEN);
236 inc_byte_array(sm->request_counter, WPA_REPLAY_COUNTER_LEN);
237
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800238 mic = (u8 *) (reply + 1);
239 WPA_PUT_BE16(mic + mic_len, 0);
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800240 if (!(key_info & WPA_KEY_INFO_MIC))
241 key_mic = NULL;
242 else
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800243 key_mic = mic;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700244
245 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
246 "WPA: Sending EAPOL-Key Request (error=%d "
247 "pairwise=%d ptk_set=%d len=%lu)",
248 error, pairwise, sm->ptk_set, (unsigned long) rlen);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800249 wpa_eapol_key_send(sm, &sm->ptk, ver, bssid, ETH_P_EAPOL, rbuf, rlen,
250 key_mic);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700251}
252
253
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800254static void wpa_supplicant_key_mgmt_set_pmk(struct wpa_sm *sm)
255{
256#ifdef CONFIG_IEEE80211R
257 if (sm->key_mgmt == WPA_KEY_MGMT_FT_IEEE8021X) {
258 if (wpa_sm_key_mgmt_set_pmk(sm, sm->xxkey, sm->xxkey_len))
259 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
260 "RSN: Cannot set low order 256 bits of MSK for key management offload");
261 } else {
262#endif /* CONFIG_IEEE80211R */
263 if (wpa_sm_key_mgmt_set_pmk(sm, sm->pmk, sm->pmk_len))
264 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
265 "RSN: Cannot set PMK for key management offload");
266#ifdef CONFIG_IEEE80211R
267 }
268#endif /* CONFIG_IEEE80211R */
269}
270
271
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700272static int wpa_supplicant_get_pmk(struct wpa_sm *sm,
273 const unsigned char *src_addr,
274 const u8 *pmkid)
275{
276 int abort_cached = 0;
277
278 if (pmkid && !sm->cur_pmksa) {
279 /* When using drivers that generate RSN IE, wpa_supplicant may
280 * not have enough time to get the association information
281 * event before receiving this 1/4 message, so try to find a
282 * matching PMKSA cache entry here. */
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800283 sm->cur_pmksa = pmksa_cache_get(sm->pmksa, src_addr, pmkid,
Roshan Pius3a1667e2018-07-03 15:17:14 -0700284 NULL, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700285 if (sm->cur_pmksa) {
286 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
287 "RSN: found matching PMKID from PMKSA cache");
288 } else {
289 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
290 "RSN: no matching PMKID found");
291 abort_cached = 1;
292 }
293 }
294
295 if (pmkid && sm->cur_pmksa &&
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700296 os_memcmp_const(pmkid, sm->cur_pmksa->pmkid, PMKID_LEN) == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700297 wpa_hexdump(MSG_DEBUG, "RSN: matched PMKID", pmkid, PMKID_LEN);
298 wpa_sm_set_pmk_from_pmksa(sm);
299 wpa_hexdump_key(MSG_DEBUG, "RSN: PMK from PMKSA cache",
300 sm->pmk, sm->pmk_len);
301 eapol_sm_notify_cached(sm->eapol);
302#ifdef CONFIG_IEEE80211R
303 sm->xxkey_len = 0;
Roshan Pius3a1667e2018-07-03 15:17:14 -0700304#ifdef CONFIG_SAE
Sunil Ravi89eba102022-09-13 21:04:37 -0700305 if ((sm->key_mgmt == WPA_KEY_MGMT_FT_SAE ||
306 sm->key_mgmt == WPA_KEY_MGMT_FT_SAE_EXT_KEY) &&
Roshan Pius3a1667e2018-07-03 15:17:14 -0700307 sm->pmk_len == PMK_LEN) {
308 /* Need to allow FT key derivation to proceed with
309 * PMK from SAE being used as the XXKey in cases where
310 * the PMKID in msg 1/4 matches the PMKSA entry that was
311 * just added based on SAE authentication for the
312 * initial mobility domain association. */
313 os_memcpy(sm->xxkey, sm->pmk, sm->pmk_len);
314 sm->xxkey_len = sm->pmk_len;
315 }
316#endif /* CONFIG_SAE */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700317#endif /* CONFIG_IEEE80211R */
318 } else if (wpa_key_mgmt_wpa_ieee8021x(sm->key_mgmt) && sm->eapol) {
319 int res, pmk_len;
Hai Shalom81f62d82019-07-22 12:10:00 -0700320#ifdef CONFIG_IEEE80211R
321 u8 buf[2 * PMK_LEN];
322#endif /* CONFIG_IEEE80211R */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800323
Dmitry Shmidtebd93af2017-02-21 13:40:44 -0800324 if (wpa_key_mgmt_sha384(sm->key_mgmt))
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800325 pmk_len = PMK_LEN_SUITE_B_192;
326 else
327 pmk_len = PMK_LEN;
328 res = eapol_sm_get_key(sm->eapol, sm->pmk, pmk_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700329 if (res) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800330 if (pmk_len == PMK_LEN) {
331 /*
332 * EAP-LEAP is an exception from other EAP
333 * methods: it uses only 16-byte PMK.
334 */
335 res = eapol_sm_get_key(sm->eapol, sm->pmk, 16);
336 pmk_len = 16;
337 }
Hai Shalomf1c97642019-07-19 23:42:07 +0000338 }
Hai Shalom81f62d82019-07-22 12:10:00 -0700339#ifdef CONFIG_IEEE80211R
340 if (res == 0 &&
341 eapol_sm_get_key(sm->eapol, buf, 2 * PMK_LEN) == 0) {
342 if (wpa_key_mgmt_sha384(sm->key_mgmt)) {
343 os_memcpy(sm->xxkey, buf, SHA384_MAC_LEN);
344 sm->xxkey_len = SHA384_MAC_LEN;
345 } else {
346 os_memcpy(sm->xxkey, buf + PMK_LEN, PMK_LEN);
347 sm->xxkey_len = PMK_LEN;
348 }
349 forced_memzero(buf, sizeof(buf));
350 if (sm->proto == WPA_PROTO_RSN &&
351 wpa_key_mgmt_ft(sm->key_mgmt)) {
352 struct rsn_pmksa_cache_entry *sa = NULL;
353 const u8 *fils_cache_id = NULL;
354
355#ifdef CONFIG_FILS
356 if (sm->fils_cache_id_set)
357 fils_cache_id = sm->fils_cache_id;
358#endif /* CONFIG_FILS */
359 wpa_hexdump_key(MSG_DEBUG,
360 "FT: Cache XXKey/MPMK",
361 sm->xxkey, sm->xxkey_len);
362 sa = pmksa_cache_add(sm->pmksa,
363 sm->xxkey, sm->xxkey_len,
364 NULL, NULL, 0,
365 src_addr, sm->own_addr,
366 sm->network_ctx,
367 sm->key_mgmt,
368 fils_cache_id);
369 if (!sm->cur_pmksa)
370 sm->cur_pmksa = sa;
371 }
372 }
373#endif /* CONFIG_IEEE80211R */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700374 if (res == 0) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700375 struct rsn_pmksa_cache_entry *sa = NULL;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700376 const u8 *fils_cache_id = NULL;
377
378#ifdef CONFIG_FILS
379 if (sm->fils_cache_id_set)
380 fils_cache_id = sm->fils_cache_id;
381#endif /* CONFIG_FILS */
382
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700383 wpa_hexdump_key(MSG_DEBUG, "WPA: PMK from EAPOL state "
384 "machines", sm->pmk, pmk_len);
385 sm->pmk_len = pmk_len;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800386 wpa_supplicant_key_mgmt_set_pmk(sm);
Dmitry Shmidtc55524a2011-07-07 11:18:38 -0700387 if (sm->proto == WPA_PROTO_RSN &&
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800388 !wpa_key_mgmt_suite_b(sm->key_mgmt) &&
Dmitry Shmidtc55524a2011-07-07 11:18:38 -0700389 !wpa_key_mgmt_ft(sm->key_mgmt)) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700390 sa = pmksa_cache_add(sm->pmksa,
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800391 sm->pmk, pmk_len, NULL,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800392 NULL, 0,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700393 src_addr, sm->own_addr,
394 sm->network_ctx,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700395 sm->key_mgmt,
396 fils_cache_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700397 }
398 if (!sm->cur_pmksa && pmkid &&
Roshan Pius3a1667e2018-07-03 15:17:14 -0700399 pmksa_cache_get(sm->pmksa, src_addr, pmkid, NULL,
400 0)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700401 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
402 "RSN: the new PMK matches with the "
403 "PMKID");
404 abort_cached = 0;
Jouni Malinen6ec30382015-07-08 20:48:18 +0300405 } else if (sa && !sm->cur_pmksa && pmkid) {
406 /*
407 * It looks like the authentication server
408 * derived mismatching MSK. This should not
409 * really happen, but bugs happen.. There is not
410 * much we can do here without knowing what
411 * exactly caused the server to misbehave.
412 */
Dmitry Shmidt014a3ff2015-12-28 13:27:49 -0800413 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
Jouni Malinen6ec30382015-07-08 20:48:18 +0300414 "RSN: PMKID mismatch - authentication server may have derived different MSK?!");
415 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700416 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700417
418 if (!sm->cur_pmksa)
419 sm->cur_pmksa = sa;
Hai Shalom81f62d82019-07-22 12:10:00 -0700420#ifdef CONFIG_IEEE80211R
421 } else if (wpa_key_mgmt_ft(sm->key_mgmt) && sm->ft_protocol) {
422 wpa_printf(MSG_DEBUG,
423 "FT: Continue 4-way handshake without PMK/PMKID for association using FT protocol");
424#endif /* CONFIG_IEEE80211R */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700425 } else {
426 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
427 "WPA: Failed to get master session key from "
428 "EAPOL state machines - key handshake "
429 "aborted");
430 if (sm->cur_pmksa) {
431 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
432 "RSN: Cancelled PMKSA caching "
433 "attempt");
434 sm->cur_pmksa = NULL;
435 abort_cached = 1;
436 } else if (!abort_cached) {
437 return -1;
438 }
439 }
440 }
441
Dmitry Shmidtc55524a2011-07-07 11:18:38 -0700442 if (abort_cached && wpa_key_mgmt_wpa_ieee8021x(sm->key_mgmt) &&
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800443 !wpa_key_mgmt_suite_b(sm->key_mgmt) &&
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800444 !wpa_key_mgmt_ft(sm->key_mgmt) && sm->key_mgmt != WPA_KEY_MGMT_OSEN)
445 {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700446 /* Send EAPOL-Start to trigger full EAP authentication. */
447 u8 *buf;
448 size_t buflen;
449
450 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
451 "RSN: no PMKSA entry found - trigger "
452 "full EAP authentication");
453 buf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_START,
454 NULL, 0, &buflen, NULL);
455 if (buf) {
Hai Shalomc1a21442022-02-04 13:43:00 -0800456 /* Set and reset eapFail to allow EAP state machine to
457 * proceed with new authentication. */
458 eapol_sm_notify_eap_fail(sm->eapol, true);
459 eapol_sm_notify_eap_fail(sm->eapol, false);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700460 wpa_sm_ether_send(sm, sm->bssid, ETH_P_EAPOL,
461 buf, buflen);
462 os_free(buf);
463 return -2;
464 }
465
466 return -1;
467 }
468
469 return 0;
470}
471
472
473/**
474 * wpa_supplicant_send_2_of_4 - Send message 2 of WPA/RSN 4-Way Handshake
475 * @sm: Pointer to WPA state machine data from wpa_sm_init()
476 * @dst: Destination address for the frame
477 * @key: Pointer to the EAPOL-Key frame header
478 * @ver: Version bits from EAPOL-Key Key Info
479 * @nonce: Nonce value for the EAPOL-Key frame
480 * @wpa_ie: WPA/RSN IE
481 * @wpa_ie_len: Length of the WPA/RSN IE
482 * @ptk: PTK to use for keyed hash and encryption
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800483 * Returns: >= 0 on success, < 0 on failure
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700484 */
485int wpa_supplicant_send_2_of_4(struct wpa_sm *sm, const unsigned char *dst,
486 const struct wpa_eapol_key *key,
487 int ver, const u8 *nonce,
488 const u8 *wpa_ie, size_t wpa_ie_len,
489 struct wpa_ptk *ptk)
490{
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800491 size_t mic_len, hdrlen, rlen;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700492 struct wpa_eapol_key *reply;
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800493 u8 *rbuf, *key_mic;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700494 u8 *rsn_ie_buf = NULL;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800495 u16 key_info;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700496
497 if (wpa_ie == NULL) {
498 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, "WPA: No wpa_ie set - "
499 "cannot generate msg 2/4");
500 return -1;
501 }
502
503#ifdef CONFIG_IEEE80211R
504 if (wpa_key_mgmt_ft(sm->key_mgmt)) {
505 int res;
506
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -0800507 wpa_hexdump(MSG_DEBUG, "WPA: WPA IE before FT processing",
508 wpa_ie, wpa_ie_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700509 /*
510 * Add PMKR1Name into RSN IE (PMKID-List) and add MDIE and
511 * FTIE from (Re)Association Response.
512 */
513 rsn_ie_buf = os_malloc(wpa_ie_len + 2 + 2 + PMKID_LEN +
514 sm->assoc_resp_ies_len);
515 if (rsn_ie_buf == NULL)
516 return -1;
517 os_memcpy(rsn_ie_buf, wpa_ie, wpa_ie_len);
Dmitry Shmidt55840ad2015-12-14 12:45:46 -0800518 res = wpa_insert_pmkid(rsn_ie_buf, &wpa_ie_len,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700519 sm->pmk_r1_name);
520 if (res < 0) {
521 os_free(rsn_ie_buf);
522 return -1;
523 }
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -0800524 wpa_hexdump(MSG_DEBUG,
525 "WPA: WPA IE after PMKID[PMKR1Name] addition into RSNE",
526 rsn_ie_buf, wpa_ie_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700527
528 if (sm->assoc_resp_ies) {
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -0800529 wpa_hexdump(MSG_DEBUG, "WPA: Add assoc_resp_ies",
530 sm->assoc_resp_ies,
531 sm->assoc_resp_ies_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700532 os_memcpy(rsn_ie_buf + wpa_ie_len, sm->assoc_resp_ies,
533 sm->assoc_resp_ies_len);
534 wpa_ie_len += sm->assoc_resp_ies_len;
535 }
536
537 wpa_ie = rsn_ie_buf;
538 }
539#endif /* CONFIG_IEEE80211R */
540
541 wpa_hexdump(MSG_DEBUG, "WPA: WPA IE for msg 2/4", wpa_ie, wpa_ie_len);
542
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700543 mic_len = wpa_mic_len(sm->key_mgmt, sm->pmk_len);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800544 hdrlen = sizeof(*reply) + mic_len + 2;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700545 rbuf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_KEY,
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800546 NULL, hdrlen + wpa_ie_len,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700547 &rlen, (void *) &reply);
548 if (rbuf == NULL) {
549 os_free(rsn_ie_buf);
550 return -1;
551 }
552
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800553 reply->type = (sm->proto == WPA_PROTO_RSN ||
554 sm->proto == WPA_PROTO_OSEN) ?
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700555 EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800556 key_info = ver | WPA_KEY_INFO_KEY_TYPE;
Sunil8cd6f4d2022-06-28 18:40:46 +0000557 if (sm->ptk_set && sm->proto != WPA_PROTO_WPA)
558 key_info |= WPA_KEY_INFO_SECURE;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800559 if (mic_len)
560 key_info |= WPA_KEY_INFO_MIC;
561 else
562 key_info |= WPA_KEY_INFO_ENCR_KEY_DATA;
563 WPA_PUT_BE16(reply->key_info, key_info);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800564 if (sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700565 WPA_PUT_BE16(reply->key_length, 0);
566 else
567 os_memcpy(reply->key_length, key->key_length, 2);
568 os_memcpy(reply->replay_counter, key->replay_counter,
569 WPA_REPLAY_COUNTER_LEN);
570 wpa_hexdump(MSG_DEBUG, "WPA: Replay Counter", reply->replay_counter,
571 WPA_REPLAY_COUNTER_LEN);
572
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800573 key_mic = (u8 *) (reply + 1);
574 WPA_PUT_BE16(key_mic + mic_len, wpa_ie_len); /* Key Data Length */
575 os_memcpy(key_mic + mic_len + 2, wpa_ie, wpa_ie_len); /* Key Data */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700576 os_free(rsn_ie_buf);
577
578 os_memcpy(reply->key_nonce, nonce, WPA_NONCE_LEN);
579
Roshan Pius5e7db942018-04-12 12:27:41 -0700580 wpa_dbg(sm->ctx->msg_ctx, MSG_INFO, "WPA: Sending EAPOL-Key 2/4");
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800581 return wpa_eapol_key_send(sm, ptk, ver, dst, ETH_P_EAPOL, rbuf, rlen,
582 key_mic);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700583}
584
585
586static int wpa_derive_ptk(struct wpa_sm *sm, const unsigned char *src_addr,
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800587 const struct wpa_eapol_key *key, struct wpa_ptk *ptk)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700588{
Sunil Ravi89eba102022-09-13 21:04:37 -0700589 int ret;
Hai Shalom021b0b52019-04-10 11:17:58 -0700590 const u8 *z = NULL;
Hai Shalom60840252021-02-19 19:02:11 -0800591 size_t z_len = 0, kdk_len;
Hai Shalomfdcde762020-04-02 11:19:20 -0700592 int akmp;
Hai Shalom021b0b52019-04-10 11:17:58 -0700593
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700594#ifdef CONFIG_IEEE80211R
595 if (wpa_key_mgmt_ft(sm->key_mgmt))
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800596 return wpa_derive_ptk_ft(sm, src_addr, key, ptk);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700597#endif /* CONFIG_IEEE80211R */
598
Hai Shalom021b0b52019-04-10 11:17:58 -0700599#ifdef CONFIG_DPP2
600 if (sm->key_mgmt == WPA_KEY_MGMT_DPP && sm->dpp_z) {
601 z = wpabuf_head(sm->dpp_z);
602 z_len = wpabuf_len(sm->dpp_z);
603 }
604#endif /* CONFIG_DPP2 */
605
Hai Shalomfdcde762020-04-02 11:19:20 -0700606 akmp = sm->key_mgmt;
607#ifdef CONFIG_OWE
608 if (sm->owe_ptk_workaround && akmp == WPA_KEY_MGMT_OWE &&
609 sm->pmk_len > 32) {
610 wpa_printf(MSG_DEBUG,
611 "OWE: Force SHA256 for PTK derivation");
612 akmp |= WPA_KEY_MGMT_PSK_SHA256;
613 }
614#endif /* CONFIG_OWE */
Hai Shalom60840252021-02-19 19:02:11 -0800615
616 if (sm->force_kdk_derivation ||
Hai Shalomc1a21442022-02-04 13:43:00 -0800617 (sm->secure_ltf &&
618 ieee802_11_rsnx_capab(sm->ap_rsnxe, WLAN_RSNX_CAPAB_SECURE_LTF)))
Hai Shalom60840252021-02-19 19:02:11 -0800619 kdk_len = WPA_KDK_MAX_LEN;
620 else
621 kdk_len = 0;
622
Sunil Ravi89eba102022-09-13 21:04:37 -0700623 ret = wpa_pmk_to_ptk(sm->pmk, sm->pmk_len, "Pairwise key expansion",
624 sm->own_addr, sm->bssid, sm->snonce,
625 key->key_nonce, ptk, akmp,
626 sm->pairwise_cipher, z, z_len,
627 kdk_len);
628 if (ret) {
629 wpa_printf(MSG_ERROR, "WPA: PTK derivation failed");
630 return ret;
631 }
632
633#ifdef CONFIG_PASN
634 if (sm->secure_ltf &&
635 ieee802_11_rsnx_capab(sm->ap_rsnxe, WLAN_RSNX_CAPAB_SECURE_LTF))
636 ret = wpa_ltf_keyseed(ptk, akmp, sm->pairwise_cipher);
637#endif /* CONFIG_PASN */
638
639 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700640}
641
642
Hai Shalomfdcde762020-04-02 11:19:20 -0700643static int wpa_handle_ext_key_id(struct wpa_sm *sm,
644 struct wpa_eapol_ie_parse *kde)
645{
646 if (sm->ext_key_id) {
647 u16 key_id;
648
649 if (!kde->key_id) {
650 wpa_msg(sm->ctx->msg_ctx,
651 sm->use_ext_key_id ? MSG_INFO : MSG_DEBUG,
652 "RSN: No Key ID in Extended Key ID handshake");
653 sm->keyidx_active = 0;
654 return sm->use_ext_key_id ? -1 : 0;
655 }
656
657 key_id = kde->key_id[0] & 0x03;
658 if (key_id > 1) {
659 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
660 "RSN: Invalid Extended Key ID: %d", key_id);
661 return -1;
662 }
663 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
664 "RSN: Using Extended Key ID %d", key_id);
665 sm->keyidx_active = key_id;
666 sm->use_ext_key_id = 1;
667 } else {
668 if (kde->key_id && (kde->key_id[0] & 0x03)) {
669 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
670 "RSN: Non-zero Extended Key ID Key ID in PTK0 handshake");
671 return -1;
672 }
673
674 if (kde->key_id) {
675 /* This is not supposed to be included here, but ignore
676 * the case of matching Key ID 0 just in case. */
677 wpa_msg(sm->ctx->msg_ctx, MSG_DEBUG,
678 "RSN: Extended Key ID Key ID 0 in PTK0 handshake");
679 }
680 sm->keyidx_active = 0;
681 sm->use_ext_key_id = 0;
682 }
683
684 return 0;
685}
686
687
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700688static void wpa_supplicant_process_1_of_4(struct wpa_sm *sm,
689 const unsigned char *src_addr,
690 const struct wpa_eapol_key *key,
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700691 u16 ver, const u8 *key_data,
Sunil8cd6f4d2022-06-28 18:40:46 +0000692 size_t key_data_len,
693 enum frame_encryption encrypted)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700694{
695 struct wpa_eapol_ie_parse ie;
696 struct wpa_ptk *ptk;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700697 int res;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800698 u8 *kde, *kde_buf = NULL;
699 size_t kde_len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700700
Sunil8cd6f4d2022-06-28 18:40:46 +0000701 if (encrypted == FRAME_NOT_ENCRYPTED && sm->tk_set &&
702 wpa_sm_pmf_enabled(sm)) {
703 wpa_printf(MSG_DEBUG,
704 "RSN: Discard unencrypted EAPOL-Key msg 1/4 when TK is set and PMF is enabled");
705 return;
706 }
707
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700708 if (wpa_sm_get_network_ctx(sm) == NULL) {
709 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, "WPA: No SSID info "
710 "found (msg 1 of 4)");
711 return;
712 }
713
Hai Shalomfdcde762020-04-02 11:19:20 -0700714 if (sm->wpa_deny_ptk0_rekey && !sm->use_ext_key_id &&
715 wpa_sm_get_state(sm) == WPA_COMPLETED) {
716 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
717 "WPA: PTK0 rekey not allowed, reconnecting");
718 wpa_sm_reconnect(sm);
719 return;
720 }
721
Roshan Pius5e7db942018-04-12 12:27:41 -0700722 wpa_dbg(sm->ctx->msg_ctx, MSG_INFO, "WPA: RX message 1 of 4-Way "
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700723 "Handshake from " MACSTR " (ver=%d)", MAC2STR(src_addr), ver);
724
725 os_memset(&ie, 0, sizeof(ie));
726
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800727 if (sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700728 /* RSN: msg 1/4 should contain PMKID for the selected PMK */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700729 wpa_hexdump(MSG_DEBUG, "RSN: msg 1/4 key data",
730 key_data, key_data_len);
Sunil8cd6f4d2022-06-28 18:40:46 +0000731 if (wpa_supplicant_parse_ies(key_data, key_data_len, &ie) < 0) {
732 wpa_printf(MSG_DEBUG,
733 "RSN: Discard EAPOL-Key msg 1/4 with invalid IEs/KDEs");
734 return;
735 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700736 if (ie.pmkid) {
737 wpa_hexdump(MSG_DEBUG, "RSN: PMKID from "
738 "Authenticator", ie.pmkid, PMKID_LEN);
739 }
740 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700741
742 res = wpa_supplicant_get_pmk(sm, src_addr, ie.pmkid);
743 if (res == -2) {
744 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "RSN: Do not reply to "
745 "msg 1/4 - requesting full EAP authentication");
746 return;
747 }
748 if (res)
749 goto failed;
750
Sunil8cd6f4d2022-06-28 18:40:46 +0000751 wpa_sm_set_state(sm, WPA_4WAY_HANDSHAKE);
752
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700753 if (sm->renew_snonce) {
754 if (random_get_bytes(sm->snonce, WPA_NONCE_LEN)) {
755 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
756 "WPA: Failed to get random data for SNonce");
757 goto failed;
758 }
759 sm->renew_snonce = 0;
760 wpa_hexdump(MSG_DEBUG, "WPA: Renewed SNonce",
761 sm->snonce, WPA_NONCE_LEN);
762 }
763
764 /* Calculate PTK which will be stored as a temporary PTK until it has
765 * been verified when processing message 3/4. */
766 ptk = &sm->tptk;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700767 if (wpa_derive_ptk(sm, src_addr, key, ptk) < 0)
768 goto failed;
Dmitry Shmidt98660862014-03-11 17:26:21 -0700769 if (sm->pairwise_cipher == WPA_CIPHER_TKIP) {
Dmitry Shmidt61593f02014-04-21 16:27:35 -0700770 u8 buf[8];
Dmitry Shmidt98660862014-03-11 17:26:21 -0700771 /* Supplicant: swap tx/rx Mic keys */
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800772 os_memcpy(buf, &ptk->tk[16], 8);
773 os_memcpy(&ptk->tk[16], &ptk->tk[24], 8);
774 os_memcpy(&ptk->tk[24], buf, 8);
Hai Shalom81f62d82019-07-22 12:10:00 -0700775 forced_memzero(buf, sizeof(buf));
Dmitry Shmidt98660862014-03-11 17:26:21 -0700776 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700777 sm->tptk_set = 1;
778
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800779 kde = sm->assoc_wpa_ie;
780 kde_len = sm->assoc_wpa_ie_len;
Hai Shalomc3565922019-10-28 11:58:20 -0700781 kde_buf = os_malloc(kde_len +
782 2 + RSN_SELECTOR_LEN + 3 +
783 sm->assoc_rsnxe_len +
Hai Shalom4fbc08f2020-05-18 12:37:00 -0700784 2 + RSN_SELECTOR_LEN + 1 +
785 2 + RSN_SELECTOR_LEN + 2);
Hai Shalomc3565922019-10-28 11:58:20 -0700786 if (!kde_buf)
787 goto failed;
788 os_memcpy(kde_buf, kde, kde_len);
789 kde = kde_buf;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800790
Hai Shalom74f70d42019-02-11 14:42:39 -0800791#ifdef CONFIG_OCV
792 if (wpa_sm_ocv_enabled(sm)) {
793 struct wpa_channel_info ci;
794 u8 *pos;
795
Hai Shalomc3565922019-10-28 11:58:20 -0700796 pos = kde + kde_len;
Hai Shalom74f70d42019-02-11 14:42:39 -0800797 if (wpa_sm_channel_info(sm, &ci) != 0) {
798 wpa_printf(MSG_WARNING,
799 "Failed to get channel info for OCI element in EAPOL-Key 2/4");
800 goto failed;
801 }
Hai Shalom899fcc72020-10-19 14:38:18 -0700802#ifdef CONFIG_TESTING_OPTIONS
803 if (sm->oci_freq_override_eapol) {
804 wpa_printf(MSG_INFO,
805 "TEST: Override OCI KDE frequency %d -> %d MHz",
806 ci.frequency, sm->oci_freq_override_eapol);
807 ci.frequency = sm->oci_freq_override_eapol;
808 }
809#endif /* CONFIG_TESTING_OPTIONS */
Hai Shalom74f70d42019-02-11 14:42:39 -0800810
Hai Shalom74f70d42019-02-11 14:42:39 -0800811 if (ocv_insert_oci_kde(&ci, &pos) < 0)
812 goto failed;
813 kde_len = pos - kde;
814 }
815#endif /* CONFIG_OCV */
816
Hai Shalomc3565922019-10-28 11:58:20 -0700817 if (sm->assoc_rsnxe && sm->assoc_rsnxe_len) {
818 os_memcpy(kde + kde_len, sm->assoc_rsnxe, sm->assoc_rsnxe_len);
819 kde_len += sm->assoc_rsnxe_len;
820 }
821
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800822#ifdef CONFIG_P2P
823 if (sm->p2p) {
Hai Shalomc3565922019-10-28 11:58:20 -0700824 u8 *pos;
825
826 wpa_printf(MSG_DEBUG,
827 "P2P: Add IP Address Request KDE into EAPOL-Key 2/4");
828 pos = kde + kde_len;
829 *pos++ = WLAN_EID_VENDOR_SPECIFIC;
830 *pos++ = RSN_SELECTOR_LEN + 1;
831 RSN_SELECTOR_PUT(pos, WFA_KEY_DATA_IP_ADDR_REQ);
832 pos += RSN_SELECTOR_LEN;
833 *pos++ = 0x01;
834 kde_len = pos - kde;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800835 }
836#endif /* CONFIG_P2P */
837
Hai Shalom4fbc08f2020-05-18 12:37:00 -0700838#ifdef CONFIG_DPP2
839 if (DPP_VERSION > 1 && sm->key_mgmt == WPA_KEY_MGMT_DPP) {
840 u8 *pos;
841
842 wpa_printf(MSG_DEBUG, "DPP: Add DPP KDE into EAPOL-Key 2/4");
843 pos = kde + kde_len;
844 *pos++ = WLAN_EID_VENDOR_SPECIFIC;
845 *pos++ = RSN_SELECTOR_LEN + 2;
846 RSN_SELECTOR_PUT(pos, WFA_KEY_DATA_DPP);
847 pos += RSN_SELECTOR_LEN;
848 *pos++ = DPP_VERSION; /* Protocol Version */
849 *pos = 0; /* Flags */
850 if (sm->dpp_pfs == 0)
851 *pos |= DPP_KDE_PFS_ALLOWED;
852 else if (sm->dpp_pfs == 1)
853 *pos |= DPP_KDE_PFS_ALLOWED | DPP_KDE_PFS_REQUIRED;
854 pos++;
855 kde_len = pos - kde;
856 }
857#endif /* CONFIG_DPP2 */
858
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700859 if (wpa_supplicant_send_2_of_4(sm, sm->bssid, key, ver, sm->snonce,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800860 kde, kde_len, ptk) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700861 goto failed;
862
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800863 os_free(kde_buf);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700864 os_memcpy(sm->anonce, key->key_nonce, WPA_NONCE_LEN);
865 return;
866
867failed:
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800868 os_free(kde_buf);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700869 wpa_sm_deauthenticate(sm, WLAN_REASON_UNSPECIFIED);
870}
871
872
873static void wpa_sm_start_preauth(void *eloop_ctx, void *timeout_ctx)
874{
875 struct wpa_sm *sm = eloop_ctx;
876 rsn_preauth_candidate_process(sm);
877}
878
879
880static void wpa_supplicant_key_neg_complete(struct wpa_sm *sm,
881 const u8 *addr, int secure)
882{
883 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
884 "WPA: Key negotiation completed with "
885 MACSTR " [PTK=%s GTK=%s]", MAC2STR(addr),
886 wpa_cipher_txt(sm->pairwise_cipher),
887 wpa_cipher_txt(sm->group_cipher));
888 wpa_sm_cancel_auth_timeout(sm);
889 wpa_sm_set_state(sm, WPA_COMPLETED);
890
891 if (secure) {
892 wpa_sm_mlme_setprotection(
893 sm, addr, MLME_SETPROTECTION_PROTECT_TYPE_RX_TX,
894 MLME_SETPROTECTION_KEY_TYPE_PAIRWISE);
Hai Shalome21d4e82020-04-29 16:34:06 -0700895 eapol_sm_notify_portValid(sm->eapol, true);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700896 if (wpa_key_mgmt_wpa_psk(sm->key_mgmt) ||
897 sm->key_mgmt == WPA_KEY_MGMT_DPP ||
898 sm->key_mgmt == WPA_KEY_MGMT_OWE)
Hai Shalome21d4e82020-04-29 16:34:06 -0700899 eapol_sm_notify_eap_success(sm->eapol, true);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700900 /*
901 * Start preauthentication after a short wait to avoid a
902 * possible race condition between the data receive and key
903 * configuration after the 4-Way Handshake. This increases the
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800904 * likelihood of the first preauth EAPOL-Start frame getting to
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700905 * the target AP.
906 */
Hai Shalom74f70d42019-02-11 14:42:39 -0800907 if (!dl_list_empty(&sm->pmksa_candidates))
908 eloop_register_timeout(1, 0, wpa_sm_start_preauth,
909 sm, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700910 }
911
912 if (sm->cur_pmksa && sm->cur_pmksa->opportunistic) {
913 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
914 "RSN: Authenticator accepted "
915 "opportunistic PMKSA entry - marking it valid");
916 sm->cur_pmksa->opportunistic = 0;
917 }
918
919#ifdef CONFIG_IEEE80211R
920 if (wpa_key_mgmt_ft(sm->key_mgmt)) {
921 /* Prepare for the next transition */
922 wpa_ft_prepare_auth_request(sm, NULL);
923 }
924#endif /* CONFIG_IEEE80211R */
925}
926
927
928static void wpa_sm_rekey_ptk(void *eloop_ctx, void *timeout_ctx)
929{
930 struct wpa_sm *sm = eloop_ctx;
931 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Request PTK rekeying");
932 wpa_sm_key_request(sm, 0, 1);
933}
934
935
936static int wpa_supplicant_install_ptk(struct wpa_sm *sm,
Hai Shalomfdcde762020-04-02 11:19:20 -0700937 const struct wpa_eapol_key *key,
938 enum key_flag key_flag)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700939{
940 int keylen, rsclen;
941 enum wpa_alg alg;
942 const u8 *key_rsc;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800943
Mathy Vanhoefc66556c2017-09-29 04:22:51 +0200944 if (sm->ptk.installed) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800945 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
946 "WPA: Do not re-install same PTK to the driver");
947 return 0;
948 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700949
950 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
951 "WPA: Installing PTK to the driver");
952
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700953 if (sm->pairwise_cipher == WPA_CIPHER_NONE) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700954 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Pairwise Cipher "
955 "Suite: NONE - do not use pairwise keys");
956 return 0;
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700957 }
958
959 if (!wpa_cipher_valid_pairwise(sm->pairwise_cipher)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700960 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
961 "WPA: Unsupported pairwise cipher %d",
962 sm->pairwise_cipher);
963 return -1;
964 }
965
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700966 alg = wpa_cipher_to_alg(sm->pairwise_cipher);
967 keylen = wpa_cipher_key_len(sm->pairwise_cipher);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700968 if (keylen <= 0 || (unsigned int) keylen != sm->ptk.tk_len) {
969 wpa_printf(MSG_DEBUG, "WPA: TK length mismatch: %d != %lu",
970 keylen, (long unsigned int) sm->ptk.tk_len);
971 return -1;
972 }
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700973 rsclen = wpa_cipher_rsc_len(sm->pairwise_cipher);
974
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800975 if (sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700976 key_rsc = null_rsc;
977 } else {
978 key_rsc = key->key_rsc;
979 wpa_hexdump(MSG_DEBUG, "WPA: RSC", key_rsc, rsclen);
980 }
981
Hai Shalomfdcde762020-04-02 11:19:20 -0700982 if (wpa_sm_set_key(sm, alg, sm->bssid, sm->keyidx_active, 1, key_rsc,
983 rsclen, sm->ptk.tk, keylen,
984 KEY_FLAG_PAIRWISE | key_flag) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700985 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
Hai Shalomfdcde762020-04-02 11:19:20 -0700986 "WPA: Failed to set PTK to the driver (alg=%d keylen=%d bssid="
987 MACSTR " idx=%d key_flag=0x%x)",
988 alg, keylen, MAC2STR(sm->bssid),
989 sm->keyidx_active, key_flag);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700990 return -1;
991 }
992
Sunil Ravi89eba102022-09-13 21:04:37 -0700993#ifdef CONFIG_PASN
994 if (sm->secure_ltf &&
995 ieee802_11_rsnx_capab(sm->ap_rsnxe, WLAN_RSNX_CAPAB_SECURE_LTF) &&
996 wpa_sm_set_ltf_keyseed(sm, sm->own_addr, sm->bssid,
997 sm->ptk.ltf_keyseed_len,
998 sm->ptk.ltf_keyseed) < 0) {
999 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1000 "WPA: Failed to set LTF keyseed to the driver (keylen=%zu bssid="
1001 MACSTR ")", sm->ptk.ltf_keyseed_len,
1002 MAC2STR(sm->bssid));
1003 return -1;
1004 }
1005#endif /* CONFIG_PASN */
1006
Hai Shalom60840252021-02-19 19:02:11 -08001007 wpa_sm_store_ptk(sm, sm->bssid, sm->pairwise_cipher,
1008 sm->dot11RSNAConfigPMKLifetime, &sm->ptk);
1009
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001010 /* TK is not needed anymore in supplicant */
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001011 os_memset(sm->ptk.tk, 0, WPA_TK_MAX_LEN);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001012 sm->ptk.tk_len = 0;
Mathy Vanhoefc66556c2017-09-29 04:22:51 +02001013 sm->ptk.installed = 1;
Sunil8cd6f4d2022-06-28 18:40:46 +00001014 sm->tk_set = true;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001015
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001016 if (sm->wpa_ptk_rekey) {
1017 eloop_cancel_timeout(wpa_sm_rekey_ptk, sm, NULL);
1018 eloop_register_timeout(sm->wpa_ptk_rekey, 0, wpa_sm_rekey_ptk,
1019 sm, NULL);
1020 }
Hai Shalomfdcde762020-04-02 11:19:20 -07001021 return 0;
1022}
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001023
Hai Shalomfdcde762020-04-02 11:19:20 -07001024
1025static int wpa_supplicant_activate_ptk(struct wpa_sm *sm)
1026{
1027 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1028 "WPA: Activate PTK (idx=%d bssid=" MACSTR ")",
1029 sm->keyidx_active, MAC2STR(sm->bssid));
1030
1031 if (wpa_sm_set_key(sm, 0, sm->bssid, sm->keyidx_active, 0, NULL, 0,
1032 NULL, 0, KEY_FLAG_PAIRWISE_RX_TX_MODIFY) < 0) {
1033 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1034 "WPA: Failed to activate PTK for TX (idx=%d bssid="
1035 MACSTR ")", sm->keyidx_active, MAC2STR(sm->bssid));
1036 return -1;
1037 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001038 return 0;
1039}
1040
1041
1042static int wpa_supplicant_check_group_cipher(struct wpa_sm *sm,
1043 int group_cipher,
1044 int keylen, int maxkeylen,
1045 int *key_rsc_len,
1046 enum wpa_alg *alg)
1047{
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07001048 int klen;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001049
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07001050 *alg = wpa_cipher_to_alg(group_cipher);
1051 if (*alg == WPA_ALG_NONE) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001052 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1053 "WPA: Unsupported Group Cipher %d",
1054 group_cipher);
1055 return -1;
1056 }
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07001057 *key_rsc_len = wpa_cipher_rsc_len(group_cipher);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001058
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07001059 klen = wpa_cipher_key_len(group_cipher);
1060 if (keylen != klen || maxkeylen < klen) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001061 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1062 "WPA: Unsupported %s Group Cipher key length %d (%d)",
1063 wpa_cipher_txt(group_cipher), keylen, maxkeylen);
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07001064 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001065 }
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07001066 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001067}
1068
1069
1070struct wpa_gtk_data {
1071 enum wpa_alg alg;
1072 int tx, key_rsc_len, keyidx;
1073 u8 gtk[32];
1074 int gtk_len;
1075};
1076
1077
1078static int wpa_supplicant_install_gtk(struct wpa_sm *sm,
1079 const struct wpa_gtk_data *gd,
Jouni Malinen58c0e962017-10-01 12:12:24 +03001080 const u8 *key_rsc, int wnm_sleep)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001081{
1082 const u8 *_gtk = gd->gtk;
1083 u8 gtk_buf[32];
1084
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02001085 /* Detect possible key reinstallation */
Jouni Malinen58c0e962017-10-01 12:12:24 +03001086 if ((sm->gtk.gtk_len == (size_t) gd->gtk_len &&
1087 os_memcmp(sm->gtk.gtk, gd->gtk, sm->gtk.gtk_len) == 0) ||
1088 (sm->gtk_wnm_sleep.gtk_len == (size_t) gd->gtk_len &&
1089 os_memcmp(sm->gtk_wnm_sleep.gtk, gd->gtk,
1090 sm->gtk_wnm_sleep.gtk_len) == 0)) {
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02001091 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1092 "WPA: Not reinstalling already in-use GTK to the driver (keyidx=%d tx=%d len=%d)",
1093 gd->keyidx, gd->tx, gd->gtk_len);
1094 return 0;
1095 }
1096
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001097 wpa_hexdump_key(MSG_DEBUG, "WPA: Group Key", gd->gtk, gd->gtk_len);
1098 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1099 "WPA: Installing GTK to the driver (keyidx=%d tx=%d len=%d)",
1100 gd->keyidx, gd->tx, gd->gtk_len);
1101 wpa_hexdump(MSG_DEBUG, "WPA: RSC", key_rsc, gd->key_rsc_len);
1102 if (sm->group_cipher == WPA_CIPHER_TKIP) {
1103 /* Swap Tx/Rx keys for Michael MIC */
1104 os_memcpy(gtk_buf, gd->gtk, 16);
1105 os_memcpy(gtk_buf + 16, gd->gtk + 24, 8);
1106 os_memcpy(gtk_buf + 24, gd->gtk + 16, 8);
1107 _gtk = gtk_buf;
1108 }
1109 if (sm->pairwise_cipher == WPA_CIPHER_NONE) {
1110 if (wpa_sm_set_key(sm, gd->alg, NULL,
1111 gd->keyidx, 1, key_rsc, gd->key_rsc_len,
Hai Shalomfdcde762020-04-02 11:19:20 -07001112 _gtk, gd->gtk_len,
1113 KEY_FLAG_GROUP_RX_TX_DEFAULT) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001114 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1115 "WPA: Failed to set GTK to the driver "
1116 "(Group only)");
Hai Shalom81f62d82019-07-22 12:10:00 -07001117 forced_memzero(gtk_buf, sizeof(gtk_buf));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001118 return -1;
1119 }
1120 } else if (wpa_sm_set_key(sm, gd->alg, broadcast_ether_addr,
1121 gd->keyidx, gd->tx, key_rsc, gd->key_rsc_len,
Hai Shalomfdcde762020-04-02 11:19:20 -07001122 _gtk, gd->gtk_len, KEY_FLAG_GROUP_RX) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001123 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1124 "WPA: Failed to set GTK to "
1125 "the driver (alg=%d keylen=%d keyidx=%d)",
1126 gd->alg, gd->gtk_len, gd->keyidx);
Hai Shalom81f62d82019-07-22 12:10:00 -07001127 forced_memzero(gtk_buf, sizeof(gtk_buf));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001128 return -1;
1129 }
Hai Shalom81f62d82019-07-22 12:10:00 -07001130 forced_memzero(gtk_buf, sizeof(gtk_buf));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001131
Jouni Malinen58c0e962017-10-01 12:12:24 +03001132 if (wnm_sleep) {
1133 sm->gtk_wnm_sleep.gtk_len = gd->gtk_len;
1134 os_memcpy(sm->gtk_wnm_sleep.gtk, gd->gtk,
1135 sm->gtk_wnm_sleep.gtk_len);
1136 } else {
1137 sm->gtk.gtk_len = gd->gtk_len;
1138 os_memcpy(sm->gtk.gtk, gd->gtk, sm->gtk.gtk_len);
1139 }
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02001140
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001141 return 0;
1142}
1143
1144
1145static int wpa_supplicant_gtk_tx_bit_workaround(const struct wpa_sm *sm,
1146 int tx)
1147{
1148 if (tx && sm->pairwise_cipher != WPA_CIPHER_NONE) {
1149 /* Ignore Tx bit for GTK if a pairwise key is used. One AP
1150 * seemed to set this bit (incorrectly, since Tx is only when
1151 * doing Group Key only APs) and without this workaround, the
1152 * data connection does not work because wpa_supplicant
1153 * configured non-zero keyidx to be used for unicast. */
1154 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1155 "WPA: Tx bit set for GTK, but pairwise "
1156 "keys are used - ignore Tx bit");
1157 return 0;
1158 }
1159 return tx;
1160}
1161
1162
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001163static int wpa_supplicant_rsc_relaxation(const struct wpa_sm *sm,
1164 const u8 *rsc)
1165{
1166 int rsclen;
1167
1168 if (!sm->wpa_rsc_relaxation)
1169 return 0;
1170
1171 rsclen = wpa_cipher_rsc_len(sm->group_cipher);
1172
1173 /*
1174 * Try to detect RSC (endian) corruption issue where the AP sends
1175 * the RSC bytes in EAPOL-Key message in the wrong order, both if
1176 * it's actually a 6-byte field (as it should be) and if it treats
1177 * it as an 8-byte field.
1178 * An AP model known to have this bug is the Sapido RB-1632.
1179 */
1180 if (rsclen == 6 && ((rsc[5] && !rsc[0]) || rsc[6] || rsc[7])) {
1181 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1182 "RSC %02x%02x%02x%02x%02x%02x%02x%02x is likely bogus, using 0",
1183 rsc[0], rsc[1], rsc[2], rsc[3],
1184 rsc[4], rsc[5], rsc[6], rsc[7]);
1185
1186 return 1;
1187 }
1188
1189 return 0;
1190}
1191
1192
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001193static int wpa_supplicant_pairwise_gtk(struct wpa_sm *sm,
1194 const struct wpa_eapol_key *key,
1195 const u8 *gtk, size_t gtk_len,
1196 int key_info)
1197{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001198 struct wpa_gtk_data gd;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001199 const u8 *key_rsc;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001200
1201 /*
1202 * IEEE Std 802.11i-2004 - 8.5.2 EAPOL-Key frames - Figure 43x
1203 * GTK KDE format:
1204 * KeyID[bits 0-1], Tx [bit 2], Reserved [bits 3-7]
1205 * Reserved [bits 0-7]
1206 * GTK
1207 */
1208
1209 os_memset(&gd, 0, sizeof(gd));
1210 wpa_hexdump_key(MSG_DEBUG, "RSN: received GTK in pairwise handshake",
1211 gtk, gtk_len);
1212
1213 if (gtk_len < 2 || gtk_len - 2 > sizeof(gd.gtk))
1214 return -1;
1215
1216 gd.keyidx = gtk[0] & 0x3;
1217 gd.tx = wpa_supplicant_gtk_tx_bit_workaround(sm,
1218 !!(gtk[0] & BIT(2)));
1219 gtk += 2;
1220 gtk_len -= 2;
1221
1222 os_memcpy(gd.gtk, gtk, gtk_len);
1223 gd.gtk_len = gtk_len;
1224
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001225 key_rsc = key->key_rsc;
1226 if (wpa_supplicant_rsc_relaxation(sm, key->key_rsc))
1227 key_rsc = null_rsc;
1228
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001229 if (sm->group_cipher != WPA_CIPHER_GTK_NOT_USED &&
1230 (wpa_supplicant_check_group_cipher(sm, sm->group_cipher,
1231 gtk_len, gtk_len,
1232 &gd.key_rsc_len, &gd.alg) ||
Jouni Malinen58c0e962017-10-01 12:12:24 +03001233 wpa_supplicant_install_gtk(sm, &gd, key_rsc, 0))) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001234 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1235 "RSN: Failed to install GTK");
Hai Shalom81f62d82019-07-22 12:10:00 -07001236 forced_memzero(&gd, sizeof(gd));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001237 return -1;
1238 }
Hai Shalom81f62d82019-07-22 12:10:00 -07001239 forced_memzero(&gd, sizeof(gd));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001240
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001241 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001242}
1243
1244
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02001245static int wpa_supplicant_install_igtk(struct wpa_sm *sm,
Jouni Malinen58c0e962017-10-01 12:12:24 +03001246 const struct wpa_igtk_kde *igtk,
1247 int wnm_sleep)
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02001248{
1249 size_t len = wpa_cipher_key_len(sm->mgmt_group_cipher);
1250 u16 keyidx = WPA_GET_LE16(igtk->keyid);
1251
1252 /* Detect possible key reinstallation */
Jouni Malinen58c0e962017-10-01 12:12:24 +03001253 if ((sm->igtk.igtk_len == len &&
1254 os_memcmp(sm->igtk.igtk, igtk->igtk, sm->igtk.igtk_len) == 0) ||
1255 (sm->igtk_wnm_sleep.igtk_len == len &&
1256 os_memcmp(sm->igtk_wnm_sleep.igtk, igtk->igtk,
1257 sm->igtk_wnm_sleep.igtk_len) == 0)) {
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02001258 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1259 "WPA: Not reinstalling already in-use IGTK to the driver (keyidx=%d)",
1260 keyidx);
1261 return 0;
1262 }
1263
1264 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
Hai Shalom39ba6fc2019-01-22 12:40:38 -08001265 "WPA: IGTK keyid %d pn " COMPACT_MACSTR,
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02001266 keyidx, MAC2STR(igtk->pn));
1267 wpa_hexdump_key(MSG_DEBUG, "WPA: IGTK", igtk->igtk, len);
1268 if (keyidx > 4095) {
1269 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1270 "WPA: Invalid IGTK KeyID %d", keyidx);
1271 return -1;
1272 }
1273 if (wpa_sm_set_key(sm, wpa_cipher_to_alg(sm->mgmt_group_cipher),
1274 broadcast_ether_addr,
1275 keyidx, 0, igtk->pn, sizeof(igtk->pn),
Hai Shalomfdcde762020-04-02 11:19:20 -07001276 igtk->igtk, len, KEY_FLAG_GROUP_RX) < 0) {
Hai Shalom5f92bc92019-04-18 11:54:11 -07001277 if (keyidx == 0x0400 || keyidx == 0x0500) {
1278 /* Assume the AP has broken PMF implementation since it
1279 * seems to have swapped the KeyID bytes. The AP cannot
1280 * be trusted to implement BIP correctly or provide a
1281 * valid IGTK, so do not try to configure this key with
1282 * swapped KeyID bytes. Instead, continue without
1283 * configuring the IGTK so that the driver can drop any
1284 * received group-addressed robust management frames due
1285 * to missing keys.
1286 *
1287 * Normally, this error behavior would result in us
1288 * disconnecting, but there are number of deployed APs
1289 * with this broken behavior, so as an interoperability
1290 * workaround, allow the connection to proceed. */
1291 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1292 "WPA: Ignore IGTK configuration error due to invalid IGTK KeyID byte order");
1293 } else {
1294 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1295 "WPA: Failed to configure IGTK to the driver");
1296 return -1;
1297 }
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02001298 }
1299
Jouni Malinen58c0e962017-10-01 12:12:24 +03001300 if (wnm_sleep) {
1301 sm->igtk_wnm_sleep.igtk_len = len;
1302 os_memcpy(sm->igtk_wnm_sleep.igtk, igtk->igtk,
1303 sm->igtk_wnm_sleep.igtk_len);
1304 } else {
1305 sm->igtk.igtk_len = len;
1306 os_memcpy(sm->igtk.igtk, igtk->igtk, sm->igtk.igtk_len);
1307 }
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02001308
1309 return 0;
1310}
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02001311
1312
Hai Shalomfdcde762020-04-02 11:19:20 -07001313static int wpa_supplicant_install_bigtk(struct wpa_sm *sm,
1314 const struct wpa_bigtk_kde *bigtk,
1315 int wnm_sleep)
1316{
1317 size_t len = wpa_cipher_key_len(sm->mgmt_group_cipher);
1318 u16 keyidx = WPA_GET_LE16(bigtk->keyid);
1319
1320 /* Detect possible key reinstallation */
1321 if ((sm->bigtk.bigtk_len == len &&
1322 os_memcmp(sm->bigtk.bigtk, bigtk->bigtk,
1323 sm->bigtk.bigtk_len) == 0) ||
1324 (sm->bigtk_wnm_sleep.bigtk_len == len &&
1325 os_memcmp(sm->bigtk_wnm_sleep.bigtk, bigtk->bigtk,
1326 sm->bigtk_wnm_sleep.bigtk_len) == 0)) {
1327 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1328 "WPA: Not reinstalling already in-use BIGTK to the driver (keyidx=%d)",
1329 keyidx);
1330 return 0;
1331 }
1332
1333 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1334 "WPA: BIGTK keyid %d pn " COMPACT_MACSTR,
1335 keyidx, MAC2STR(bigtk->pn));
1336 wpa_hexdump_key(MSG_DEBUG, "WPA: BIGTK", bigtk->bigtk, len);
1337 if (keyidx < 6 || keyidx > 7) {
1338 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1339 "WPA: Invalid BIGTK KeyID %d", keyidx);
1340 return -1;
1341 }
1342 if (wpa_sm_set_key(sm, wpa_cipher_to_alg(sm->mgmt_group_cipher),
1343 broadcast_ether_addr,
1344 keyidx, 0, bigtk->pn, sizeof(bigtk->pn),
1345 bigtk->bigtk, len, KEY_FLAG_GROUP_RX) < 0) {
1346 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1347 "WPA: Failed to configure BIGTK to the driver");
1348 return -1;
1349 }
1350
1351 if (wnm_sleep) {
1352 sm->bigtk_wnm_sleep.bigtk_len = len;
1353 os_memcpy(sm->bigtk_wnm_sleep.bigtk, bigtk->bigtk,
1354 sm->bigtk_wnm_sleep.bigtk_len);
1355 } else {
1356 sm->bigtk.bigtk_len = len;
1357 os_memcpy(sm->bigtk.bigtk, bigtk->bigtk, sm->bigtk.bigtk_len);
1358 }
1359
1360 return 0;
1361}
1362
1363
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001364static int ieee80211w_set_keys(struct wpa_sm *sm,
1365 struct wpa_eapol_ie_parse *ie)
1366{
Hai Shalomfdcde762020-04-02 11:19:20 -07001367 size_t len;
1368
Hai Shalom60840252021-02-19 19:02:11 -08001369 if (!wpa_cipher_valid_mgmt_group(sm->mgmt_group_cipher) ||
1370 sm->mgmt_group_cipher == WPA_CIPHER_GTK_NOT_USED)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001371 return 0;
1372
1373 if (ie->igtk) {
1374 const struct wpa_igtk_kde *igtk;
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02001375
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001376 len = wpa_cipher_key_len(sm->mgmt_group_cipher);
1377 if (ie->igtk_len != WPA_IGTK_KDE_PREFIX_LEN + len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001378 return -1;
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02001379
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001380 igtk = (const struct wpa_igtk_kde *) ie->igtk;
Jouni Malinen58c0e962017-10-01 12:12:24 +03001381 if (wpa_supplicant_install_igtk(sm, igtk, 0) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001382 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001383 }
1384
Hai Shalomfdcde762020-04-02 11:19:20 -07001385 if (ie->bigtk && sm->beacon_prot) {
1386 const struct wpa_bigtk_kde *bigtk;
1387
1388 len = wpa_cipher_key_len(sm->mgmt_group_cipher);
1389 if (ie->bigtk_len != WPA_BIGTK_KDE_PREFIX_LEN + len)
1390 return -1;
1391
1392 bigtk = (const struct wpa_bigtk_kde *) ie->bigtk;
1393 if (wpa_supplicant_install_bigtk(sm, bigtk, 0) < 0)
1394 return -1;
1395 }
1396
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001397 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001398}
1399
1400
1401static void wpa_report_ie_mismatch(struct wpa_sm *sm,
1402 const char *reason, const u8 *src_addr,
1403 const u8 *wpa_ie, size_t wpa_ie_len,
1404 const u8 *rsn_ie, size_t rsn_ie_len)
1405{
1406 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, "WPA: %s (src=" MACSTR ")",
1407 reason, MAC2STR(src_addr));
1408
1409 if (sm->ap_wpa_ie) {
1410 wpa_hexdump(MSG_INFO, "WPA: WPA IE in Beacon/ProbeResp",
1411 sm->ap_wpa_ie, sm->ap_wpa_ie_len);
1412 }
1413 if (wpa_ie) {
1414 if (!sm->ap_wpa_ie) {
1415 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1416 "WPA: No WPA IE in Beacon/ProbeResp");
1417 }
1418 wpa_hexdump(MSG_INFO, "WPA: WPA IE in 3/4 msg",
1419 wpa_ie, wpa_ie_len);
1420 }
1421
1422 if (sm->ap_rsn_ie) {
1423 wpa_hexdump(MSG_INFO, "WPA: RSN IE in Beacon/ProbeResp",
1424 sm->ap_rsn_ie, sm->ap_rsn_ie_len);
1425 }
1426 if (rsn_ie) {
1427 if (!sm->ap_rsn_ie) {
1428 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1429 "WPA: No RSN IE in Beacon/ProbeResp");
1430 }
1431 wpa_hexdump(MSG_INFO, "WPA: RSN IE in 3/4 msg",
1432 rsn_ie, rsn_ie_len);
1433 }
1434
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001435 wpa_sm_deauthenticate(sm, WLAN_REASON_IE_IN_4WAY_DIFFERS);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001436}
1437
1438
1439#ifdef CONFIG_IEEE80211R
1440
1441static int ft_validate_mdie(struct wpa_sm *sm,
1442 const unsigned char *src_addr,
1443 struct wpa_eapol_ie_parse *ie,
1444 const u8 *assoc_resp_mdie)
1445{
1446 struct rsn_mdie *mdie;
1447
1448 mdie = (struct rsn_mdie *) (ie->mdie + 2);
1449 if (ie->mdie == NULL || ie->mdie_len < 2 + sizeof(*mdie) ||
1450 os_memcmp(mdie->mobility_domain, sm->mobility_domain,
1451 MOBILITY_DOMAIN_ID_LEN) != 0) {
1452 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "FT: MDIE in msg 3/4 did "
1453 "not match with the current mobility domain");
1454 return -1;
1455 }
1456
1457 if (assoc_resp_mdie &&
1458 (assoc_resp_mdie[1] != ie->mdie[1] ||
1459 os_memcmp(assoc_resp_mdie, ie->mdie, 2 + ie->mdie[1]) != 0)) {
1460 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "FT: MDIE mismatch");
1461 wpa_hexdump(MSG_DEBUG, "FT: MDIE in EAPOL-Key msg 3/4",
1462 ie->mdie, 2 + ie->mdie[1]);
1463 wpa_hexdump(MSG_DEBUG, "FT: MDIE in (Re)Association Response",
1464 assoc_resp_mdie, 2 + assoc_resp_mdie[1]);
1465 return -1;
1466 }
1467
1468 return 0;
1469}
1470
1471
1472static int ft_validate_ftie(struct wpa_sm *sm,
1473 const unsigned char *src_addr,
1474 struct wpa_eapol_ie_parse *ie,
1475 const u8 *assoc_resp_ftie)
1476{
1477 if (ie->ftie == NULL) {
1478 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1479 "FT: No FTIE in EAPOL-Key msg 3/4");
1480 return -1;
1481 }
1482
1483 if (assoc_resp_ftie == NULL)
1484 return 0;
1485
1486 if (assoc_resp_ftie[1] != ie->ftie[1] ||
1487 os_memcmp(assoc_resp_ftie, ie->ftie, 2 + ie->ftie[1]) != 0) {
1488 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "FT: FTIE mismatch");
1489 wpa_hexdump(MSG_DEBUG, "FT: FTIE in EAPOL-Key msg 3/4",
1490 ie->ftie, 2 + ie->ftie[1]);
1491 wpa_hexdump(MSG_DEBUG, "FT: FTIE in (Re)Association Response",
1492 assoc_resp_ftie, 2 + assoc_resp_ftie[1]);
1493 return -1;
1494 }
1495
1496 return 0;
1497}
1498
1499
1500static int ft_validate_rsnie(struct wpa_sm *sm,
1501 const unsigned char *src_addr,
1502 struct wpa_eapol_ie_parse *ie)
1503{
1504 struct wpa_ie_data rsn;
1505
1506 if (!ie->rsn_ie)
1507 return 0;
1508
1509 /*
1510 * Verify that PMKR1Name from EAPOL-Key message 3/4
1511 * matches with the value we derived.
1512 */
1513 if (wpa_parse_wpa_ie_rsn(ie->rsn_ie, ie->rsn_ie_len, &rsn) < 0 ||
1514 rsn.num_pmkid != 1 || rsn.pmkid == NULL) {
1515 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "FT: No PMKR1Name in "
1516 "FT 4-way handshake message 3/4");
1517 return -1;
1518 }
1519
Dmitry Shmidtc2817022014-07-02 10:32:10 -07001520 if (os_memcmp_const(rsn.pmkid, sm->pmk_r1_name, WPA_PMK_NAME_LEN) != 0)
1521 {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001522 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1523 "FT: PMKR1Name mismatch in "
1524 "FT 4-way handshake message 3/4");
1525 wpa_hexdump(MSG_DEBUG, "FT: PMKR1Name from Authenticator",
1526 rsn.pmkid, WPA_PMK_NAME_LEN);
1527 wpa_hexdump(MSG_DEBUG, "FT: Derived PMKR1Name",
1528 sm->pmk_r1_name, WPA_PMK_NAME_LEN);
1529 return -1;
1530 }
1531
1532 return 0;
1533}
1534
1535
1536static int wpa_supplicant_validate_ie_ft(struct wpa_sm *sm,
1537 const unsigned char *src_addr,
1538 struct wpa_eapol_ie_parse *ie)
1539{
1540 const u8 *pos, *end, *mdie = NULL, *ftie = NULL;
1541
1542 if (sm->assoc_resp_ies) {
1543 pos = sm->assoc_resp_ies;
1544 end = pos + sm->assoc_resp_ies_len;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001545 while (end - pos > 2) {
1546 if (2 + pos[1] > end - pos)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001547 break;
1548 switch (*pos) {
1549 case WLAN_EID_MOBILITY_DOMAIN:
1550 mdie = pos;
1551 break;
1552 case WLAN_EID_FAST_BSS_TRANSITION:
1553 ftie = pos;
1554 break;
1555 }
1556 pos += 2 + pos[1];
1557 }
1558 }
1559
1560 if (ft_validate_mdie(sm, src_addr, ie, mdie) < 0 ||
1561 ft_validate_ftie(sm, src_addr, ie, ftie) < 0 ||
1562 ft_validate_rsnie(sm, src_addr, ie) < 0)
1563 return -1;
1564
1565 return 0;
1566}
1567
1568#endif /* CONFIG_IEEE80211R */
1569
1570
1571static int wpa_supplicant_validate_ie(struct wpa_sm *sm,
1572 const unsigned char *src_addr,
1573 struct wpa_eapol_ie_parse *ie)
1574{
1575 if (sm->ap_wpa_ie == NULL && sm->ap_rsn_ie == NULL) {
1576 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1577 "WPA: No WPA/RSN IE for this AP known. "
1578 "Trying to get from scan results");
1579 if (wpa_sm_get_beacon_ie(sm) < 0) {
1580 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1581 "WPA: Could not find AP from "
1582 "the scan results");
Hai Shalomfdcde762020-04-02 11:19:20 -07001583 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001584 }
Hai Shalomfdcde762020-04-02 11:19:20 -07001585 wpa_msg(sm->ctx->msg_ctx, MSG_DEBUG,
1586 "WPA: Found the current AP from updated scan results");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001587 }
1588
1589 if (ie->wpa_ie == NULL && ie->rsn_ie == NULL &&
1590 (sm->ap_wpa_ie || sm->ap_rsn_ie)) {
1591 wpa_report_ie_mismatch(sm, "IE in 3/4 msg does not match "
1592 "with IE in Beacon/ProbeResp (no IE?)",
1593 src_addr, ie->wpa_ie, ie->wpa_ie_len,
1594 ie->rsn_ie, ie->rsn_ie_len);
1595 return -1;
1596 }
1597
1598 if ((ie->wpa_ie && sm->ap_wpa_ie &&
1599 (ie->wpa_ie_len != sm->ap_wpa_ie_len ||
1600 os_memcmp(ie->wpa_ie, sm->ap_wpa_ie, ie->wpa_ie_len) != 0)) ||
1601 (ie->rsn_ie && sm->ap_rsn_ie &&
1602 wpa_compare_rsn_ie(wpa_key_mgmt_ft(sm->key_mgmt),
1603 sm->ap_rsn_ie, sm->ap_rsn_ie_len,
1604 ie->rsn_ie, ie->rsn_ie_len))) {
1605 wpa_report_ie_mismatch(sm, "IE in 3/4 msg does not match "
1606 "with IE in Beacon/ProbeResp",
1607 src_addr, ie->wpa_ie, ie->wpa_ie_len,
1608 ie->rsn_ie, ie->rsn_ie_len);
1609 return -1;
1610 }
1611
1612 if (sm->proto == WPA_PROTO_WPA &&
1613 ie->rsn_ie && sm->ap_rsn_ie == NULL && sm->rsn_enabled) {
1614 wpa_report_ie_mismatch(sm, "Possible downgrade attack "
1615 "detected - RSN was enabled and RSN IE "
1616 "was in msg 3/4, but not in "
1617 "Beacon/ProbeResp",
1618 src_addr, ie->wpa_ie, ie->wpa_ie_len,
1619 ie->rsn_ie, ie->rsn_ie_len);
1620 return -1;
1621 }
1622
Hai Shalom60840252021-02-19 19:02:11 -08001623 if (sm->proto == WPA_PROTO_RSN &&
1624 ((sm->ap_rsnxe && !ie->rsnxe) ||
1625 (!sm->ap_rsnxe && ie->rsnxe) ||
1626 (sm->ap_rsnxe && ie->rsnxe &&
1627 (sm->ap_rsnxe_len != ie->rsnxe_len ||
1628 os_memcmp(sm->ap_rsnxe, ie->rsnxe, sm->ap_rsnxe_len) != 0)))) {
Hai Shalomc3565922019-10-28 11:58:20 -07001629 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1630 "WPA: RSNXE mismatch between Beacon/ProbeResp and EAPOL-Key msg 3/4");
Hai Shalomfdcde762020-04-02 11:19:20 -07001631 wpa_hexdump(MSG_INFO, "RSNXE in Beacon/ProbeResp",
1632 sm->ap_rsnxe, sm->ap_rsnxe_len);
1633 wpa_hexdump(MSG_INFO, "RSNXE in EAPOL-Key msg 3/4",
1634 ie->rsnxe, ie->rsnxe_len);
1635 wpa_sm_deauthenticate(sm, WLAN_REASON_IE_IN_4WAY_DIFFERS);
Hai Shalomc3565922019-10-28 11:58:20 -07001636 return -1;
1637 }
1638
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001639#ifdef CONFIG_IEEE80211R
1640 if (wpa_key_mgmt_ft(sm->key_mgmt) &&
1641 wpa_supplicant_validate_ie_ft(sm, src_addr, ie) < 0)
1642 return -1;
1643#endif /* CONFIG_IEEE80211R */
1644
1645 return 0;
1646}
1647
1648
1649/**
1650 * wpa_supplicant_send_4_of_4 - Send message 4 of WPA/RSN 4-Way Handshake
1651 * @sm: Pointer to WPA state machine data from wpa_sm_init()
1652 * @dst: Destination address for the frame
1653 * @key: Pointer to the EAPOL-Key frame header
1654 * @ver: Version bits from EAPOL-Key Key Info
1655 * @key_info: Key Info
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001656 * @ptk: PTK to use for keyed hash and encryption
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001657 * Returns: >= 0 on success, < 0 on failure
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001658 */
1659int wpa_supplicant_send_4_of_4(struct wpa_sm *sm, const unsigned char *dst,
1660 const struct wpa_eapol_key *key,
1661 u16 ver, u16 key_info,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001662 struct wpa_ptk *ptk)
1663{
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001664 size_t mic_len, hdrlen, rlen;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001665 struct wpa_eapol_key *reply;
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001666 u8 *rbuf, *key_mic;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001667
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001668 mic_len = wpa_mic_len(sm->key_mgmt, sm->pmk_len);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001669 hdrlen = sizeof(*reply) + mic_len + 2;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001670 rbuf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_KEY, NULL,
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001671 hdrlen, &rlen, (void *) &reply);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001672 if (rbuf == NULL)
1673 return -1;
1674
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001675 reply->type = (sm->proto == WPA_PROTO_RSN ||
1676 sm->proto == WPA_PROTO_OSEN) ?
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001677 EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA;
1678 key_info &= WPA_KEY_INFO_SECURE;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001679 key_info |= ver | WPA_KEY_INFO_KEY_TYPE;
1680 if (mic_len)
1681 key_info |= WPA_KEY_INFO_MIC;
1682 else
1683 key_info |= WPA_KEY_INFO_ENCR_KEY_DATA;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001684 WPA_PUT_BE16(reply->key_info, key_info);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001685 if (sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001686 WPA_PUT_BE16(reply->key_length, 0);
1687 else
1688 os_memcpy(reply->key_length, key->key_length, 2);
1689 os_memcpy(reply->replay_counter, key->replay_counter,
1690 WPA_REPLAY_COUNTER_LEN);
1691
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001692 key_mic = (u8 *) (reply + 1);
1693 WPA_PUT_BE16(key_mic + mic_len, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001694
Roshan Pius5e7db942018-04-12 12:27:41 -07001695 wpa_dbg(sm->ctx->msg_ctx, MSG_INFO, "WPA: Sending EAPOL-Key 4/4");
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001696 return wpa_eapol_key_send(sm, ptk, ver, dst, ETH_P_EAPOL, rbuf, rlen,
1697 key_mic);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001698}
1699
1700
1701static void wpa_supplicant_process_3_of_4(struct wpa_sm *sm,
1702 const struct wpa_eapol_key *key,
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001703 u16 ver, const u8 *key_data,
1704 size_t key_data_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001705{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001706 u16 key_info, keylen;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001707 struct wpa_eapol_ie_parse ie;
1708
1709 wpa_sm_set_state(sm, WPA_4WAY_HANDSHAKE);
Roshan Pius5e7db942018-04-12 12:27:41 -07001710 wpa_dbg(sm->ctx->msg_ctx, MSG_INFO, "WPA: RX message 3 of 4-Way "
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001711 "Handshake from " MACSTR " (ver=%d)", MAC2STR(sm->bssid), ver);
1712
1713 key_info = WPA_GET_BE16(key->key_info);
1714
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001715 wpa_hexdump(MSG_DEBUG, "WPA: IE KeyData", key_data, key_data_len);
1716 if (wpa_supplicant_parse_ies(key_data, key_data_len, &ie) < 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001717 goto failed;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001718 if (ie.gtk && !(key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
1719 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1720 "WPA: GTK IE in unencrypted key data");
1721 goto failed;
1722 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001723 if (ie.igtk && !(key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
1724 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1725 "WPA: IGTK KDE in unencrypted key data");
1726 goto failed;
1727 }
1728
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001729 if (ie.igtk &&
Hai Shalom60840252021-02-19 19:02:11 -08001730 sm->mgmt_group_cipher != WPA_CIPHER_GTK_NOT_USED &&
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001731 wpa_cipher_valid_mgmt_group(sm->mgmt_group_cipher) &&
1732 ie.igtk_len != WPA_IGTK_KDE_PREFIX_LEN +
1733 (unsigned int) wpa_cipher_key_len(sm->mgmt_group_cipher)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001734 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1735 "WPA: Invalid IGTK KDE length %lu",
1736 (unsigned long) ie.igtk_len);
1737 goto failed;
1738 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001739
1740 if (wpa_supplicant_validate_ie(sm, sm->bssid, &ie) < 0)
1741 goto failed;
1742
Hai Shalomfdcde762020-04-02 11:19:20 -07001743 if (wpa_handle_ext_key_id(sm, &ie))
1744 goto failed;
1745
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001746 if (os_memcmp(sm->anonce, key->key_nonce, WPA_NONCE_LEN) != 0) {
1747 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1748 "WPA: ANonce from message 1 of 4-Way Handshake "
1749 "differs from 3 of 4-Way Handshake - drop packet (src="
1750 MACSTR ")", MAC2STR(sm->bssid));
1751 goto failed;
1752 }
1753
1754 keylen = WPA_GET_BE16(key->key_length);
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07001755 if (keylen != wpa_cipher_key_len(sm->pairwise_cipher)) {
1756 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1757 "WPA: Invalid %s key length %d (src=" MACSTR
1758 ")", wpa_cipher_txt(sm->pairwise_cipher), keylen,
1759 MAC2STR(sm->bssid));
1760 goto failed;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001761 }
1762
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001763#ifdef CONFIG_P2P
1764 if (ie.ip_addr_alloc) {
1765 os_memcpy(sm->p2p_ip_addr, ie.ip_addr_alloc, 3 * 4);
1766 wpa_hexdump(MSG_DEBUG, "P2P: IP address info",
1767 sm->p2p_ip_addr, sizeof(sm->p2p_ip_addr));
1768 }
1769#endif /* CONFIG_P2P */
1770
Hai Shalom74f70d42019-02-11 14:42:39 -08001771#ifdef CONFIG_OCV
1772 if (wpa_sm_ocv_enabled(sm)) {
1773 struct wpa_channel_info ci;
1774
1775 if (wpa_sm_channel_info(sm, &ci) != 0) {
1776 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1777 "Failed to get channel info to validate received OCI in EAPOL-Key 3/4");
1778 return;
1779 }
1780
1781 if (ocv_verify_tx_params(ie.oci, ie.oci_len, &ci,
1782 channel_width_to_int(ci.chanwidth),
Hai Shalom899fcc72020-10-19 14:38:18 -07001783 ci.seg1_idx) != OCI_SUCCESS) {
1784 wpa_msg(sm->ctx->msg_ctx, MSG_INFO, OCV_FAILURE
1785 "addr=" MACSTR " frame=eapol-key-m3 error=%s",
1786 MAC2STR(sm->bssid), ocv_errorstr);
Hai Shalom74f70d42019-02-11 14:42:39 -08001787 return;
1788 }
1789 }
1790#endif /* CONFIG_OCV */
1791
Hai Shalom4fbc08f2020-05-18 12:37:00 -07001792#ifdef CONFIG_DPP2
1793 if (DPP_VERSION > 1 && ie.dpp_kde) {
1794 wpa_printf(MSG_DEBUG,
1795 "DPP: peer Protocol Version %u Flags 0x%x",
1796 ie.dpp_kde[0], ie.dpp_kde[1]);
1797 if (sm->key_mgmt == WPA_KEY_MGMT_DPP && sm->dpp_pfs != 2 &&
1798 (ie.dpp_kde[1] & DPP_KDE_PFS_ALLOWED) && !sm->dpp_z) {
1799 wpa_printf(MSG_INFO,
1800 "DPP: Peer indicated it supports PFS and local configuration allows this, but PFS was not negotiated for the association");
1801 goto failed;
1802 }
1803 }
1804#endif /* CONFIG_DPP2 */
1805
Hai Shalomfdcde762020-04-02 11:19:20 -07001806 if (sm->use_ext_key_id &&
1807 wpa_supplicant_install_ptk(sm, key, KEY_FLAG_RX))
1808 goto failed;
1809
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001810 if (wpa_supplicant_send_4_of_4(sm, sm->bssid, key, ver, key_info,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001811 &sm->ptk) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001812 goto failed;
1813 }
1814
1815 /* SNonce was successfully used in msg 3/4, so mark it to be renewed
1816 * for the next 4-Way Handshake. If msg 3 is received again, the old
1817 * SNonce will still be used to avoid changing PTK. */
1818 sm->renew_snonce = 1;
1819
1820 if (key_info & WPA_KEY_INFO_INSTALL) {
Hai Shalomfdcde762020-04-02 11:19:20 -07001821 int res;
1822
1823 if (sm->use_ext_key_id)
1824 res = wpa_supplicant_activate_ptk(sm);
1825 else
1826 res = wpa_supplicant_install_ptk(sm, key,
1827 KEY_FLAG_RX_TX);
1828 if (res)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001829 goto failed;
1830 }
1831
1832 if (key_info & WPA_KEY_INFO_SECURE) {
1833 wpa_sm_mlme_setprotection(
1834 sm, sm->bssid, MLME_SETPROTECTION_PROTECT_TYPE_RX,
1835 MLME_SETPROTECTION_KEY_TYPE_PAIRWISE);
Hai Shalome21d4e82020-04-29 16:34:06 -07001836 eapol_sm_notify_portValid(sm->eapol, true);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001837 }
1838 wpa_sm_set_state(sm, WPA_GROUP_HANDSHAKE);
1839
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001840 if (sm->group_cipher == WPA_CIPHER_GTK_NOT_USED) {
Hai Shalom5f92bc92019-04-18 11:54:11 -07001841 /* No GTK to be set to the driver */
1842 } else if (!ie.gtk && sm->proto == WPA_PROTO_RSN) {
1843 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1844 "RSN: No GTK KDE included in EAPOL-Key msg 3/4");
1845 goto failed;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001846 } else if (ie.gtk &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001847 wpa_supplicant_pairwise_gtk(sm, key,
1848 ie.gtk, ie.gtk_len, key_info) < 0) {
1849 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1850 "RSN: Failed to configure GTK");
1851 goto failed;
1852 }
1853
1854 if (ieee80211w_set_keys(sm, &ie) < 0) {
1855 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1856 "RSN: Failed to configure IGTK");
1857 goto failed;
1858 }
1859
Hai Shalom5f92bc92019-04-18 11:54:11 -07001860 if (sm->group_cipher == WPA_CIPHER_GTK_NOT_USED || ie.gtk)
1861 wpa_supplicant_key_neg_complete(sm, sm->bssid,
1862 key_info & WPA_KEY_INFO_SECURE);
1863
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001864 if (ie.gtk)
1865 wpa_sm_set_rekey_offload(sm);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001866
Hai Shalom39ba6fc2019-01-22 12:40:38 -08001867 /* Add PMKSA cache entry for Suite B AKMs here since PMKID can be
1868 * calculated only after KCK has been derived. Though, do not replace an
1869 * existing PMKSA entry after each 4-way handshake (i.e., new KCK/PMKID)
1870 * to avoid unnecessary changes of PMKID while continuing to use the
1871 * same PMK. */
1872 if (sm->proto == WPA_PROTO_RSN && wpa_key_mgmt_suite_b(sm->key_mgmt) &&
1873 !sm->cur_pmksa) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001874 struct rsn_pmksa_cache_entry *sa;
1875
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001876 sa = pmksa_cache_add(sm->pmksa, sm->pmk, sm->pmk_len, NULL,
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001877 sm->ptk.kck, sm->ptk.kck_len,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001878 sm->bssid, sm->own_addr,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001879 sm->network_ctx, sm->key_mgmt, NULL);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001880 if (!sm->cur_pmksa)
1881 sm->cur_pmksa = sa;
1882 }
1883
Hai Shalomfdcde762020-04-02 11:19:20 -07001884 if (ie.transition_disable)
1885 wpa_sm_transition_disable(sm, ie.transition_disable[0]);
Dmitry Shmidt4dd28dc2015-03-10 11:21:43 -07001886 sm->msg_3_of_4_ok = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001887 return;
1888
1889failed:
1890 wpa_sm_deauthenticate(sm, WLAN_REASON_UNSPECIFIED);
1891}
1892
1893
1894static int wpa_supplicant_process_1_of_2_rsn(struct wpa_sm *sm,
1895 const u8 *keydata,
1896 size_t keydatalen,
1897 u16 key_info,
1898 struct wpa_gtk_data *gd)
1899{
1900 int maxkeylen;
1901 struct wpa_eapol_ie_parse ie;
Hai Shalom60840252021-02-19 19:02:11 -08001902 u16 gtk_len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001903
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08001904 wpa_hexdump_key(MSG_DEBUG, "RSN: msg 1/2 key data",
1905 keydata, keydatalen);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001906 if (wpa_supplicant_parse_ies(keydata, keydatalen, &ie) < 0)
1907 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001908 if (ie.gtk && !(key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
1909 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1910 "WPA: GTK IE in unencrypted key data");
1911 return -1;
1912 }
1913 if (ie.gtk == NULL) {
1914 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1915 "WPA: No GTK IE in Group Key msg 1/2");
1916 return -1;
1917 }
Hai Shalom60840252021-02-19 19:02:11 -08001918 gtk_len = ie.gtk_len;
1919 if (gtk_len < 2) {
1920 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1921 "RSN: Invalid GTK KDE length (%u) in Group Key msg 1/2",
1922 gtk_len);
1923 return -1;
1924 }
1925 gtk_len -= 2;
1926 if (gtk_len > sizeof(gd->gtk)) {
1927 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1928 "RSN: Too long GTK in GTK KDE (len=%u)", gtk_len);
1929 return -1;
1930 }
1931 maxkeylen = gd->gtk_len = gtk_len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001932
Hai Shalom74f70d42019-02-11 14:42:39 -08001933#ifdef CONFIG_OCV
1934 if (wpa_sm_ocv_enabled(sm)) {
1935 struct wpa_channel_info ci;
1936
1937 if (wpa_sm_channel_info(sm, &ci) != 0) {
1938 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1939 "Failed to get channel info to validate received OCI in EAPOL-Key group msg 1/2");
1940 return -1;
1941 }
1942
1943 if (ocv_verify_tx_params(ie.oci, ie.oci_len, &ci,
1944 channel_width_to_int(ci.chanwidth),
Hai Shalom899fcc72020-10-19 14:38:18 -07001945 ci.seg1_idx) != OCI_SUCCESS) {
1946 wpa_msg(sm->ctx->msg_ctx, MSG_INFO, OCV_FAILURE
1947 "addr=" MACSTR " frame=eapol-key-g1 error=%s",
1948 MAC2STR(sm->bssid), ocv_errorstr);
Hai Shalom74f70d42019-02-11 14:42:39 -08001949 return -1;
1950 }
1951 }
1952#endif /* CONFIG_OCV */
1953
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001954 if (wpa_supplicant_check_group_cipher(sm, sm->group_cipher,
Hai Shalom60840252021-02-19 19:02:11 -08001955 gtk_len, maxkeylen,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001956 &gd->key_rsc_len, &gd->alg))
1957 return -1;
1958
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001959 wpa_hexdump_key(MSG_DEBUG, "RSN: received GTK in group key handshake",
Hai Shalom60840252021-02-19 19:02:11 -08001960 ie.gtk, 2 + gtk_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001961 gd->keyidx = ie.gtk[0] & 0x3;
1962 gd->tx = wpa_supplicant_gtk_tx_bit_workaround(sm,
1963 !!(ie.gtk[0] & BIT(2)));
Hai Shalom60840252021-02-19 19:02:11 -08001964 os_memcpy(gd->gtk, ie.gtk + 2, gtk_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001965
1966 if (ieee80211w_set_keys(sm, &ie) < 0)
1967 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1968 "RSN: Failed to configure IGTK");
1969
1970 return 0;
1971}
1972
1973
1974static int wpa_supplicant_process_1_of_2_wpa(struct wpa_sm *sm,
1975 const struct wpa_eapol_key *key,
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001976 const u8 *key_data,
1977 size_t key_data_len, u16 key_info,
1978 u16 ver, struct wpa_gtk_data *gd)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001979{
1980 size_t maxkeylen;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001981 u16 gtk_len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001982
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001983 gtk_len = WPA_GET_BE16(key->key_length);
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001984 maxkeylen = key_data_len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001985 if (ver == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
1986 if (maxkeylen < 8) {
1987 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1988 "WPA: Too short maxkeylen (%lu)",
1989 (unsigned long) maxkeylen);
1990 return -1;
1991 }
1992 maxkeylen -= 8;
1993 }
1994
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001995 if (gtk_len > maxkeylen ||
1996 wpa_supplicant_check_group_cipher(sm, sm->group_cipher,
1997 gtk_len, maxkeylen,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001998 &gd->key_rsc_len, &gd->alg))
1999 return -1;
2000
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002001 gd->gtk_len = gtk_len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002002 gd->keyidx = (key_info & WPA_KEY_INFO_KEY_INDEX_MASK) >>
2003 WPA_KEY_INFO_KEY_INDEX_SHIFT;
Dmitry Shmidt807291d2015-01-27 13:40:23 -08002004 if (ver == WPA_KEY_INFO_TYPE_HMAC_MD5_RC4 && sm->ptk.kek_len == 16) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002005#ifdef CONFIG_NO_RC4
2006 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
2007 "WPA: RC4 not supported in the build");
2008 return -1;
2009#else /* CONFIG_NO_RC4 */
Dmitry Shmidt61593f02014-04-21 16:27:35 -07002010 u8 ek[32];
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002011 if (key_data_len > sizeof(gd->gtk)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002012 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
2013 "WPA: RC4 key data too long (%lu)",
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002014 (unsigned long) key_data_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002015 return -1;
2016 }
Dmitry Shmidt61593f02014-04-21 16:27:35 -07002017 os_memcpy(ek, key->key_iv, 16);
Dmitry Shmidt807291d2015-01-27 13:40:23 -08002018 os_memcpy(ek + 16, sm->ptk.kek, sm->ptk.kek_len);
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002019 os_memcpy(gd->gtk, key_data, key_data_len);
2020 if (rc4_skip(ek, 32, 256, gd->gtk, key_data_len)) {
Hai Shalom81f62d82019-07-22 12:10:00 -07002021 forced_memzero(ek, sizeof(ek));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002022 wpa_msg(sm->ctx->msg_ctx, MSG_ERROR,
2023 "WPA: RC4 failed");
2024 return -1;
2025 }
Hai Shalom81f62d82019-07-22 12:10:00 -07002026 forced_memzero(ek, sizeof(ek));
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002027#endif /* CONFIG_NO_RC4 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002028 } else if (ver == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002029 if (maxkeylen % 8) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002030 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
2031 "WPA: Unsupported AES-WRAP len %lu",
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002032 (unsigned long) maxkeylen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002033 return -1;
2034 }
2035 if (maxkeylen > sizeof(gd->gtk)) {
2036 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
2037 "WPA: AES-WRAP key data "
2038 "too long (keydatalen=%lu maxkeylen=%lu)",
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002039 (unsigned long) key_data_len,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002040 (unsigned long) maxkeylen);
2041 return -1;
2042 }
Dmitry Shmidt807291d2015-01-27 13:40:23 -08002043 if (aes_unwrap(sm->ptk.kek, sm->ptk.kek_len, maxkeylen / 8,
2044 key_data, gd->gtk)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002045 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
2046 "WPA: AES unwrap failed - could not decrypt "
2047 "GTK");
2048 return -1;
2049 }
2050 } else {
2051 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
2052 "WPA: Unsupported key_info type %d", ver);
2053 return -1;
2054 }
2055 gd->tx = wpa_supplicant_gtk_tx_bit_workaround(
2056 sm, !!(key_info & WPA_KEY_INFO_TXRX));
2057 return 0;
2058}
2059
2060
2061static int wpa_supplicant_send_2_of_2(struct wpa_sm *sm,
2062 const struct wpa_eapol_key *key,
2063 int ver, u16 key_info)
2064{
Dmitry Shmidt807291d2015-01-27 13:40:23 -08002065 size_t mic_len, hdrlen, rlen;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002066 struct wpa_eapol_key *reply;
Dmitry Shmidt807291d2015-01-27 13:40:23 -08002067 u8 *rbuf, *key_mic;
Hai Shalom74f70d42019-02-11 14:42:39 -08002068 size_t kde_len = 0;
2069
Sunil Ravia04bd252022-05-02 22:54:18 -07002070#ifdef CONFIG_TESTING_OPTIONS
2071 if (sm->disable_eapol_g2_tx) {
2072 wpa_printf(MSG_INFO, "TEST: Disable sending EAPOL-Key 2/2");
2073 return 0;
2074 }
2075#endif /* CONFIG_TESTING_OPTIONS */
2076
Hai Shalom74f70d42019-02-11 14:42:39 -08002077#ifdef CONFIG_OCV
2078 if (wpa_sm_ocv_enabled(sm))
2079 kde_len = OCV_OCI_KDE_LEN;
2080#endif /* CONFIG_OCV */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002081
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002082 mic_len = wpa_mic_len(sm->key_mgmt, sm->pmk_len);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002083 hdrlen = sizeof(*reply) + mic_len + 2;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002084 rbuf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_KEY, NULL,
Hai Shalom74f70d42019-02-11 14:42:39 -08002085 hdrlen + kde_len, &rlen, (void *) &reply);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002086 if (rbuf == NULL)
2087 return -1;
2088
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002089 reply->type = (sm->proto == WPA_PROTO_RSN ||
2090 sm->proto == WPA_PROTO_OSEN) ?
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002091 EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA;
2092 key_info &= WPA_KEY_INFO_KEY_INDEX_MASK;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002093 key_info |= ver | WPA_KEY_INFO_SECURE;
2094 if (mic_len)
2095 key_info |= WPA_KEY_INFO_MIC;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002096 else
2097 key_info |= WPA_KEY_INFO_ENCR_KEY_DATA;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002098 WPA_PUT_BE16(reply->key_info, key_info);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002099 if (sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002100 WPA_PUT_BE16(reply->key_length, 0);
2101 else
2102 os_memcpy(reply->key_length, key->key_length, 2);
2103 os_memcpy(reply->replay_counter, key->replay_counter,
2104 WPA_REPLAY_COUNTER_LEN);
2105
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002106 key_mic = (u8 *) (reply + 1);
Hai Shalom74f70d42019-02-11 14:42:39 -08002107 WPA_PUT_BE16(key_mic + mic_len, kde_len); /* Key Data Length */
2108
2109#ifdef CONFIG_OCV
2110 if (wpa_sm_ocv_enabled(sm)) {
2111 struct wpa_channel_info ci;
2112 u8 *pos;
2113
2114 if (wpa_sm_channel_info(sm, &ci) != 0) {
2115 wpa_printf(MSG_WARNING,
2116 "Failed to get channel info for OCI element in EAPOL-Key 2/2");
2117 os_free(rbuf);
2118 return -1;
2119 }
Hai Shalom899fcc72020-10-19 14:38:18 -07002120#ifdef CONFIG_TESTING_OPTIONS
2121 if (sm->oci_freq_override_eapol_g2) {
2122 wpa_printf(MSG_INFO,
2123 "TEST: Override OCI KDE frequency %d -> %d MHz",
2124 ci.frequency,
2125 sm->oci_freq_override_eapol_g2);
2126 ci.frequency = sm->oci_freq_override_eapol_g2;
2127 }
2128#endif /* CONFIG_TESTING_OPTIONS */
Hai Shalom74f70d42019-02-11 14:42:39 -08002129
2130 pos = key_mic + mic_len + 2; /* Key Data */
2131 if (ocv_insert_oci_kde(&ci, &pos) < 0) {
2132 os_free(rbuf);
2133 return -1;
2134 }
2135 }
2136#endif /* CONFIG_OCV */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002137
2138 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Sending EAPOL-Key 2/2");
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002139 return wpa_eapol_key_send(sm, &sm->ptk, ver, sm->bssid, ETH_P_EAPOL,
2140 rbuf, rlen, key_mic);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002141}
2142
2143
2144static void wpa_supplicant_process_1_of_2(struct wpa_sm *sm,
2145 const unsigned char *src_addr,
2146 const struct wpa_eapol_key *key,
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002147 const u8 *key_data,
2148 size_t key_data_len, u16 ver)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002149{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002150 u16 key_info;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002151 int rekey, ret;
2152 struct wpa_gtk_data gd;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002153 const u8 *key_rsc;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002154
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002155 if (!sm->msg_3_of_4_ok && !wpa_fils_is_completed(sm)) {
Dmitry Shmidt4dd28dc2015-03-10 11:21:43 -07002156 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2157 "WPA: Group Key Handshake started prior to completion of 4-way handshake");
2158 goto failed;
2159 }
2160
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002161 os_memset(&gd, 0, sizeof(gd));
2162
2163 rekey = wpa_sm_get_state(sm) == WPA_COMPLETED;
2164 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: RX message 1 of Group Key "
2165 "Handshake from " MACSTR " (ver=%d)", MAC2STR(src_addr), ver);
2166
2167 key_info = WPA_GET_BE16(key->key_info);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002168
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002169 if (sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN) {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002170 ret = wpa_supplicant_process_1_of_2_rsn(sm, key_data,
2171 key_data_len, key_info,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002172 &gd);
2173 } else {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002174 ret = wpa_supplicant_process_1_of_2_wpa(sm, key, key_data,
2175 key_data_len,
2176 key_info, ver, &gd);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002177 }
2178
2179 wpa_sm_set_state(sm, WPA_GROUP_HANDSHAKE);
2180
2181 if (ret)
2182 goto failed;
2183
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002184 key_rsc = key->key_rsc;
2185 if (wpa_supplicant_rsc_relaxation(sm, key->key_rsc))
2186 key_rsc = null_rsc;
2187
Jouni Malinen58c0e962017-10-01 12:12:24 +03002188 if (wpa_supplicant_install_gtk(sm, &gd, key_rsc, 0) ||
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002189 wpa_supplicant_send_2_of_2(sm, key, ver, key_info) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002190 goto failed;
Hai Shalom81f62d82019-07-22 12:10:00 -07002191 forced_memzero(&gd, sizeof(gd));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002192
2193 if (rekey) {
2194 wpa_msg(sm->ctx->msg_ctx, MSG_INFO, "WPA: Group rekeying "
2195 "completed with " MACSTR " [GTK=%s]",
2196 MAC2STR(sm->bssid), wpa_cipher_txt(sm->group_cipher));
2197 wpa_sm_cancel_auth_timeout(sm);
2198 wpa_sm_set_state(sm, WPA_COMPLETED);
2199 } else {
2200 wpa_supplicant_key_neg_complete(sm, sm->bssid,
2201 key_info &
2202 WPA_KEY_INFO_SECURE);
2203 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002204
2205 wpa_sm_set_rekey_offload(sm);
2206
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002207 return;
2208
2209failed:
Hai Shalom81f62d82019-07-22 12:10:00 -07002210 forced_memzero(&gd, sizeof(gd));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002211 wpa_sm_deauthenticate(sm, WLAN_REASON_UNSPECIFIED);
2212}
2213
2214
2215static int wpa_supplicant_verify_eapol_key_mic(struct wpa_sm *sm,
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002216 struct wpa_eapol_key *key,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002217 u16 ver,
2218 const u8 *buf, size_t len)
2219{
Dmitry Shmidt807291d2015-01-27 13:40:23 -08002220 u8 mic[WPA_EAPOL_KEY_MIC_MAX_LEN];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002221 int ok = 0;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002222 size_t mic_len = wpa_mic_len(sm->key_mgmt, sm->pmk_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002223
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002224 os_memcpy(mic, key + 1, mic_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002225 if (sm->tptk_set) {
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002226 os_memset(key + 1, 0, mic_len);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002227 if (wpa_eapol_key_mic(sm->tptk.kck, sm->tptk.kck_len,
2228 sm->key_mgmt,
2229 ver, buf, len, (u8 *) (key + 1)) < 0 ||
2230 os_memcmp_const(mic, key + 1, mic_len) != 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002231 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
2232 "WPA: Invalid EAPOL-Key MIC "
2233 "when using TPTK - ignoring TPTK");
Hai Shalom74f70d42019-02-11 14:42:39 -08002234#ifdef TEST_FUZZ
2235 wpa_printf(MSG_INFO,
2236 "TEST: Ignore Key MIC failure for fuzz testing");
2237 goto continue_fuzz;
2238#endif /* TEST_FUZZ */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002239 } else {
Hai Shalom74f70d42019-02-11 14:42:39 -08002240#ifdef TEST_FUZZ
2241 continue_fuzz:
2242#endif /* TEST_FUZZ */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002243 ok = 1;
2244 sm->tptk_set = 0;
2245 sm->ptk_set = 1;
2246 os_memcpy(&sm->ptk, &sm->tptk, sizeof(sm->ptk));
Dmitry Shmidt61593f02014-04-21 16:27:35 -07002247 os_memset(&sm->tptk, 0, sizeof(sm->tptk));
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002248 /*
2249 * This assures the same TPTK in sm->tptk can never be
Roshan Pius3a1667e2018-07-03 15:17:14 -07002250 * copied twice to sm->ptk as the new PTK. In
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002251 * combination with the installed flag in the wpa_ptk
2252 * struct, this assures the same PTK is only installed
2253 * once.
2254 */
2255 sm->renew_snonce = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002256 }
2257 }
2258
2259 if (!ok && sm->ptk_set) {
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002260 os_memset(key + 1, 0, mic_len);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002261 if (wpa_eapol_key_mic(sm->ptk.kck, sm->ptk.kck_len,
2262 sm->key_mgmt,
2263 ver, buf, len, (u8 *) (key + 1)) < 0 ||
2264 os_memcmp_const(mic, key + 1, mic_len) != 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002265 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
2266 "WPA: Invalid EAPOL-Key MIC - "
2267 "dropping packet");
Hai Shalom74f70d42019-02-11 14:42:39 -08002268#ifdef TEST_FUZZ
2269 wpa_printf(MSG_INFO,
2270 "TEST: Ignore Key MIC failure for fuzz testing");
2271 goto continue_fuzz2;
2272#endif /* TEST_FUZZ */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002273 return -1;
2274 }
Hai Shalom74f70d42019-02-11 14:42:39 -08002275#ifdef TEST_FUZZ
2276 continue_fuzz2:
2277#endif /* TEST_FUZZ */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002278 ok = 1;
2279 }
2280
2281 if (!ok) {
2282 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
2283 "WPA: Could not verify EAPOL-Key MIC - "
2284 "dropping packet");
2285 return -1;
2286 }
2287
2288 os_memcpy(sm->rx_replay_counter, key->replay_counter,
2289 WPA_REPLAY_COUNTER_LEN);
2290 sm->rx_replay_counter_set = 1;
2291 return 0;
2292}
2293
2294
2295/* Decrypt RSN EAPOL-Key key data (RC4 or AES-WRAP) */
2296static int wpa_supplicant_decrypt_key_data(struct wpa_sm *sm,
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002297 struct wpa_eapol_key *key,
2298 size_t mic_len, u16 ver,
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002299 u8 *key_data, size_t *key_data_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002300{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002301 wpa_hexdump(MSG_DEBUG, "RSN: encrypted key data",
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002302 key_data, *key_data_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002303 if (!sm->ptk_set) {
2304 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
2305 "WPA: PTK not available, cannot decrypt EAPOL-Key Key "
2306 "Data");
2307 return -1;
2308 }
2309
2310 /* Decrypt key data here so that this operation does not need
2311 * to be implemented separately for each message type. */
Dmitry Shmidt807291d2015-01-27 13:40:23 -08002312 if (ver == WPA_KEY_INFO_TYPE_HMAC_MD5_RC4 && sm->ptk.kek_len == 16) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002313#ifdef CONFIG_NO_RC4
2314 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
2315 "WPA: RC4 not supported in the build");
2316 return -1;
2317#else /* CONFIG_NO_RC4 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002318 u8 ek[32];
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002319
2320 wpa_printf(MSG_DEBUG, "WPA: Decrypt Key Data using RC4");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002321 os_memcpy(ek, key->key_iv, 16);
Dmitry Shmidt807291d2015-01-27 13:40:23 -08002322 os_memcpy(ek + 16, sm->ptk.kek, sm->ptk.kek_len);
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002323 if (rc4_skip(ek, 32, 256, key_data, *key_data_len)) {
Hai Shalom81f62d82019-07-22 12:10:00 -07002324 forced_memzero(ek, sizeof(ek));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002325 wpa_msg(sm->ctx->msg_ctx, MSG_ERROR,
2326 "WPA: RC4 failed");
2327 return -1;
2328 }
Hai Shalom81f62d82019-07-22 12:10:00 -07002329 forced_memzero(ek, sizeof(ek));
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002330#endif /* CONFIG_NO_RC4 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002331 } else if (ver == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES ||
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002332 ver == WPA_KEY_INFO_TYPE_AES_128_CMAC ||
Roshan Pius3a1667e2018-07-03 15:17:14 -07002333 wpa_use_aes_key_wrap(sm->key_mgmt)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002334 u8 *buf;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002335
2336 wpa_printf(MSG_DEBUG,
2337 "WPA: Decrypt Key Data using AES-UNWRAP (KEK length %u)",
2338 (unsigned int) sm->ptk.kek_len);
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002339 if (*key_data_len < 8 || *key_data_len % 8) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002340 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002341 "WPA: Unsupported AES-WRAP len %u",
2342 (unsigned int) *key_data_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002343 return -1;
2344 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002345 *key_data_len -= 8; /* AES-WRAP adds 8 bytes */
2346 buf = os_malloc(*key_data_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002347 if (buf == NULL) {
2348 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
2349 "WPA: No memory for AES-UNWRAP buffer");
2350 return -1;
2351 }
Hai Shalom74f70d42019-02-11 14:42:39 -08002352#ifdef TEST_FUZZ
2353 os_memset(buf, 0x11, *key_data_len);
2354#endif /* TEST_FUZZ */
Dmitry Shmidt807291d2015-01-27 13:40:23 -08002355 if (aes_unwrap(sm->ptk.kek, sm->ptk.kek_len, *key_data_len / 8,
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002356 key_data, buf)) {
Hai Shalom74f70d42019-02-11 14:42:39 -08002357#ifdef TEST_FUZZ
2358 wpa_printf(MSG_INFO,
2359 "TEST: Ignore AES unwrap failure for fuzz testing");
2360 goto continue_fuzz;
2361#endif /* TEST_FUZZ */
Dmitry Shmidt7d56b752015-12-22 10:59:44 -08002362 bin_clear_free(buf, *key_data_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002363 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
2364 "WPA: AES unwrap failed - "
2365 "could not decrypt EAPOL-Key key data");
2366 return -1;
2367 }
Hai Shalom74f70d42019-02-11 14:42:39 -08002368#ifdef TEST_FUZZ
2369 continue_fuzz:
2370#endif /* TEST_FUZZ */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002371 os_memcpy(key_data, buf, *key_data_len);
Dmitry Shmidt7d56b752015-12-22 10:59:44 -08002372 bin_clear_free(buf, *key_data_len);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002373 WPA_PUT_BE16(((u8 *) (key + 1)) + mic_len, *key_data_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002374 } else {
2375 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
2376 "WPA: Unsupported key_info type %d", ver);
2377 return -1;
2378 }
2379 wpa_hexdump_key(MSG_DEBUG, "WPA: decrypted EAPOL-Key key data",
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002380 key_data, *key_data_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002381 return 0;
2382}
2383
2384
2385/**
2386 * wpa_sm_aborted_cached - Notify WPA that PMKSA caching was aborted
2387 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2388 */
2389void wpa_sm_aborted_cached(struct wpa_sm *sm)
2390{
2391 if (sm && sm->cur_pmksa) {
2392 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2393 "RSN: Cancelling PMKSA caching attempt");
2394 sm->cur_pmksa = NULL;
2395 }
2396}
2397
2398
Hai Shalomc1a21442022-02-04 13:43:00 -08002399void wpa_sm_aborted_external_cached(struct wpa_sm *sm)
2400{
2401 if (sm && sm->cur_pmksa && sm->cur_pmksa->external) {
2402 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2403 "RSN: Cancelling external PMKSA caching attempt");
2404 sm->cur_pmksa = NULL;
2405 }
2406}
2407
2408
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002409static void wpa_eapol_key_dump(struct wpa_sm *sm,
Dmitry Shmidt807291d2015-01-27 13:40:23 -08002410 const struct wpa_eapol_key *key,
2411 unsigned int key_data_len,
2412 const u8 *mic, unsigned int mic_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002413{
2414#ifndef CONFIG_NO_STDOUT_DEBUG
2415 u16 key_info = WPA_GET_BE16(key->key_info);
2416
2417 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, " EAPOL-Key type=%d", key->type);
2418 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2419 " key_info 0x%x (ver=%d keyidx=%d rsvd=%d %s%s%s%s%s%s%s%s)",
2420 key_info, key_info & WPA_KEY_INFO_TYPE_MASK,
2421 (key_info & WPA_KEY_INFO_KEY_INDEX_MASK) >>
2422 WPA_KEY_INFO_KEY_INDEX_SHIFT,
2423 (key_info & (BIT(13) | BIT(14) | BIT(15))) >> 13,
2424 key_info & WPA_KEY_INFO_KEY_TYPE ? "Pairwise" : "Group",
2425 key_info & WPA_KEY_INFO_INSTALL ? " Install" : "",
2426 key_info & WPA_KEY_INFO_ACK ? " Ack" : "",
2427 key_info & WPA_KEY_INFO_MIC ? " MIC" : "",
2428 key_info & WPA_KEY_INFO_SECURE ? " Secure" : "",
2429 key_info & WPA_KEY_INFO_ERROR ? " Error" : "",
2430 key_info & WPA_KEY_INFO_REQUEST ? " Request" : "",
2431 key_info & WPA_KEY_INFO_ENCR_KEY_DATA ? " Encr" : "");
2432 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2433 " key_length=%u key_data_length=%u",
Dmitry Shmidt807291d2015-01-27 13:40:23 -08002434 WPA_GET_BE16(key->key_length), key_data_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002435 wpa_hexdump(MSG_DEBUG, " replay_counter",
2436 key->replay_counter, WPA_REPLAY_COUNTER_LEN);
2437 wpa_hexdump(MSG_DEBUG, " key_nonce", key->key_nonce, WPA_NONCE_LEN);
2438 wpa_hexdump(MSG_DEBUG, " key_iv", key->key_iv, 16);
2439 wpa_hexdump(MSG_DEBUG, " key_rsc", key->key_rsc, 8);
2440 wpa_hexdump(MSG_DEBUG, " key_id (reserved)", key->key_id, 8);
Dmitry Shmidt807291d2015-01-27 13:40:23 -08002441 wpa_hexdump(MSG_DEBUG, " key_mic", mic, mic_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002442#endif /* CONFIG_NO_STDOUT_DEBUG */
2443}
2444
2445
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002446#ifdef CONFIG_FILS
2447static int wpa_supp_aead_decrypt(struct wpa_sm *sm, u8 *buf, size_t buf_len,
2448 size_t *key_data_len)
2449{
2450 struct wpa_ptk *ptk;
2451 struct ieee802_1x_hdr *hdr;
2452 struct wpa_eapol_key *key;
2453 u8 *pos, *tmp;
2454 const u8 *aad[1];
2455 size_t aad_len[1];
2456
2457 if (*key_data_len < AES_BLOCK_SIZE) {
2458 wpa_printf(MSG_INFO, "No room for AES-SIV data in the frame");
2459 return -1;
2460 }
2461
2462 if (sm->tptk_set)
2463 ptk = &sm->tptk;
2464 else if (sm->ptk_set)
2465 ptk = &sm->ptk;
2466 else
2467 return -1;
2468
2469 hdr = (struct ieee802_1x_hdr *) buf;
2470 key = (struct wpa_eapol_key *) (hdr + 1);
2471 pos = (u8 *) (key + 1);
2472 pos += 2; /* Pointing at the Encrypted Key Data field */
2473
2474 tmp = os_malloc(*key_data_len);
2475 if (!tmp)
2476 return -1;
2477
2478 /* AES-SIV AAD from EAPOL protocol version field (inclusive) to
2479 * to Key Data (exclusive). */
2480 aad[0] = buf;
2481 aad_len[0] = pos - buf;
2482 if (aes_siv_decrypt(ptk->kek, ptk->kek_len, pos, *key_data_len,
2483 1, aad, aad_len, tmp) < 0) {
2484 wpa_printf(MSG_INFO, "Invalid AES-SIV data in the frame");
2485 bin_clear_free(tmp, *key_data_len);
2486 return -1;
2487 }
2488
2489 /* AEAD decryption and validation completed successfully */
2490 (*key_data_len) -= AES_BLOCK_SIZE;
2491 wpa_hexdump_key(MSG_DEBUG, "WPA: Decrypted Key Data",
2492 tmp, *key_data_len);
2493
2494 /* Replace Key Data field with the decrypted version */
2495 os_memcpy(pos, tmp, *key_data_len);
2496 pos -= 2; /* Key Data Length field */
2497 WPA_PUT_BE16(pos, *key_data_len);
2498 bin_clear_free(tmp, *key_data_len);
2499
2500 if (sm->tptk_set) {
2501 sm->tptk_set = 0;
2502 sm->ptk_set = 1;
2503 os_memcpy(&sm->ptk, &sm->tptk, sizeof(sm->ptk));
2504 os_memset(&sm->tptk, 0, sizeof(sm->tptk));
2505 }
2506
2507 os_memcpy(sm->rx_replay_counter, key->replay_counter,
2508 WPA_REPLAY_COUNTER_LEN);
2509 sm->rx_replay_counter_set = 1;
2510
2511 return 0;
2512}
2513#endif /* CONFIG_FILS */
2514
2515
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002516/**
2517 * wpa_sm_rx_eapol - Process received WPA EAPOL frames
2518 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2519 * @src_addr: Source MAC address of the EAPOL packet
2520 * @buf: Pointer to the beginning of the EAPOL data (EAPOL header)
2521 * @len: Length of the EAPOL frame
Sunil8cd6f4d2022-06-28 18:40:46 +00002522 * @encrypted: Whether the frame was encrypted
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002523 * Returns: 1 = WPA EAPOL-Key processed, 0 = not a WPA EAPOL-Key, -1 failure
2524 *
2525 * This function is called for each received EAPOL frame. Other than EAPOL-Key
2526 * frames can be skipped if filtering is done elsewhere. wpa_sm_rx_eapol() is
2527 * only processing WPA and WPA2 EAPOL-Key frames.
2528 *
2529 * The received EAPOL-Key packets are validated and valid packets are replied
2530 * to. In addition, key material (PTK, GTK) is configured at the end of a
2531 * successful key handshake.
2532 */
2533int wpa_sm_rx_eapol(struct wpa_sm *sm, const u8 *src_addr,
Sunil8cd6f4d2022-06-28 18:40:46 +00002534 const u8 *buf, size_t len, enum frame_encryption encrypted)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002535{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002536 size_t plen, data_len, key_data_len;
2537 const struct ieee802_1x_hdr *hdr;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002538 struct wpa_eapol_key *key;
2539 u16 key_info, ver;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002540 u8 *tmp = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002541 int ret = -1;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002542 u8 *mic, *key_data;
Hai Shalom899fcc72020-10-19 14:38:18 -07002543 size_t mic_len, keyhdrlen, pmk_len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002544
2545#ifdef CONFIG_IEEE80211R
2546 sm->ft_completed = 0;
2547#endif /* CONFIG_IEEE80211R */
2548
Hai Shalom899fcc72020-10-19 14:38:18 -07002549 pmk_len = sm->pmk_len;
2550 if (!pmk_len && sm->cur_pmksa)
2551 pmk_len = sm->cur_pmksa->pmk_len;
2552 mic_len = wpa_mic_len(sm->key_mgmt, pmk_len);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002553 keyhdrlen = sizeof(*key) + mic_len + 2;
Dmitry Shmidt807291d2015-01-27 13:40:23 -08002554
2555 if (len < sizeof(*hdr) + keyhdrlen) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002556 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2557 "WPA: EAPOL frame too short to be a WPA "
2558 "EAPOL-Key (len %lu, expecting at least %lu)",
2559 (unsigned long) len,
Dmitry Shmidt807291d2015-01-27 13:40:23 -08002560 (unsigned long) sizeof(*hdr) + keyhdrlen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002561 return 0;
2562 }
2563
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002564 hdr = (const struct ieee802_1x_hdr *) buf;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002565 plen = be_to_host16(hdr->length);
2566 data_len = plen + sizeof(*hdr);
2567 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2568 "IEEE 802.1X RX: version=%d type=%d length=%lu",
2569 hdr->version, hdr->type, (unsigned long) plen);
2570
2571 if (hdr->version < EAPOL_VERSION) {
2572 /* TODO: backwards compatibility */
2573 }
2574 if (hdr->type != IEEE802_1X_TYPE_EAPOL_KEY) {
2575 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2576 "WPA: EAPOL frame (type %u) discarded, "
2577 "not a Key frame", hdr->type);
2578 ret = 0;
2579 goto out;
2580 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002581 wpa_hexdump(MSG_MSGDUMP, "WPA: RX EAPOL-Key", buf, len);
Dmitry Shmidt807291d2015-01-27 13:40:23 -08002582 if (plen > len - sizeof(*hdr) || plen < keyhdrlen) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002583 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2584 "WPA: EAPOL frame payload size %lu "
2585 "invalid (frame size %lu)",
2586 (unsigned long) plen, (unsigned long) len);
2587 ret = 0;
2588 goto out;
2589 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002590 if (data_len < len) {
2591 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2592 "WPA: ignoring %lu bytes after the IEEE 802.1X data",
2593 (unsigned long) len - data_len);
2594 }
2595
2596 /*
2597 * Make a copy of the frame since we need to modify the buffer during
2598 * MAC validation and Key Data decryption.
2599 */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002600 tmp = os_memdup(buf, data_len);
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002601 if (tmp == NULL)
2602 goto out;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002603 key = (struct wpa_eapol_key *) (tmp + sizeof(struct ieee802_1x_hdr));
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002604 mic = (u8 *) (key + 1);
2605 key_data = mic + mic_len + 2;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002606
2607 if (key->type != EAPOL_KEY_TYPE_WPA && key->type != EAPOL_KEY_TYPE_RSN)
2608 {
2609 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2610 "WPA: EAPOL-Key type (%d) unknown, discarded",
2611 key->type);
2612 ret = 0;
2613 goto out;
2614 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002615
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002616 key_data_len = WPA_GET_BE16(mic + mic_len);
2617 wpa_eapol_key_dump(sm, key, key_data_len, mic, mic_len);
Dmitry Shmidt807291d2015-01-27 13:40:23 -08002618
2619 if (key_data_len > plen - keyhdrlen) {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002620 wpa_msg(sm->ctx->msg_ctx, MSG_INFO, "WPA: Invalid EAPOL-Key "
2621 "frame - key_data overflow (%u > %u)",
2622 (unsigned int) key_data_len,
Dmitry Shmidt807291d2015-01-27 13:40:23 -08002623 (unsigned int) (plen - keyhdrlen));
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002624 goto out;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002625 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002626
2627 eapol_sm_notify_lower_layer_success(sm->eapol, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002628 key_info = WPA_GET_BE16(key->key_info);
2629 ver = key_info & WPA_KEY_INFO_TYPE_MASK;
2630 if (ver != WPA_KEY_INFO_TYPE_HMAC_MD5_RC4 &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002631 ver != WPA_KEY_INFO_TYPE_AES_128_CMAC &&
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002632 ver != WPA_KEY_INFO_TYPE_HMAC_SHA1_AES &&
Roshan Pius3a1667e2018-07-03 15:17:14 -07002633 !wpa_use_akm_defined(sm->key_mgmt)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002634 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2635 "WPA: Unsupported EAPOL-Key descriptor version %d",
2636 ver);
2637 goto out;
2638 }
2639
Roshan Pius3a1667e2018-07-03 15:17:14 -07002640 if (wpa_use_akm_defined(sm->key_mgmt) &&
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002641 ver != WPA_KEY_INFO_TYPE_AKM_DEFINED) {
2642 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2643 "RSN: Unsupported EAPOL-Key descriptor version %d (expected AKM defined = 0)",
2644 ver);
2645 goto out;
2646 }
2647
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002648#ifdef CONFIG_IEEE80211R
2649 if (wpa_key_mgmt_ft(sm->key_mgmt)) {
2650 /* IEEE 802.11r uses a new key_info type (AES-128-CMAC). */
Roshan Pius3a1667e2018-07-03 15:17:14 -07002651 if (ver != WPA_KEY_INFO_TYPE_AES_128_CMAC &&
2652 !wpa_use_akm_defined(sm->key_mgmt)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002653 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2654 "FT: AP did not use AES-128-CMAC");
2655 goto out;
2656 }
2657 } else
2658#endif /* CONFIG_IEEE80211R */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002659 if (wpa_key_mgmt_sha256(sm->key_mgmt)) {
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002660 if (ver != WPA_KEY_INFO_TYPE_AES_128_CMAC &&
Roshan Pius3a1667e2018-07-03 15:17:14 -07002661 !wpa_use_akm_defined(sm->key_mgmt)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002662 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2663 "WPA: AP did not use the "
2664 "negotiated AES-128-CMAC");
2665 goto out;
2666 }
Hai Shalomc3565922019-10-28 11:58:20 -07002667 } else if (sm->pairwise_cipher == WPA_CIPHER_CCMP &&
2668 !wpa_use_akm_defined(sm->key_mgmt) &&
2669 ver != WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002670 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2671 "WPA: CCMP is used, but EAPOL-Key "
2672 "descriptor version (%d) is not 2", ver);
2673 if (sm->group_cipher != WPA_CIPHER_CCMP &&
2674 !(key_info & WPA_KEY_INFO_KEY_TYPE)) {
2675 /* Earlier versions of IEEE 802.11i did not explicitly
2676 * require version 2 descriptor for all EAPOL-Key
2677 * packets, so allow group keys to use version 1 if
2678 * CCMP is not used for them. */
2679 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2680 "WPA: Backwards compatibility: allow invalid "
2681 "version for non-CCMP group keys");
Jouni Malinen658fb4a2014-11-14 20:57:05 +02002682 } else if (ver == WPA_KEY_INFO_TYPE_AES_128_CMAC) {
2683 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2684 "WPA: Interoperability workaround: allow incorrect (should have been HMAC-SHA1), but stronger (is AES-128-CMAC), descriptor version to be used");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002685 } else
2686 goto out;
Dmitry Shmidt71757432014-06-02 13:50:35 -07002687 } else if (sm->pairwise_cipher == WPA_CIPHER_GCMP &&
Roshan Pius3a1667e2018-07-03 15:17:14 -07002688 !wpa_use_akm_defined(sm->key_mgmt) &&
Dmitry Shmidt71757432014-06-02 13:50:35 -07002689 ver != WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002690 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2691 "WPA: GCMP is used, but EAPOL-Key "
2692 "descriptor version (%d) is not 2", ver);
2693 goto out;
2694 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002695
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002696 if (sm->rx_replay_counter_set &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002697 os_memcmp(key->replay_counter, sm->rx_replay_counter,
2698 WPA_REPLAY_COUNTER_LEN) <= 0) {
2699 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
2700 "WPA: EAPOL-Key Replay Counter did not increase - "
2701 "dropping packet");
2702 goto out;
2703 }
2704
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002705 if (key_info & WPA_KEY_INFO_SMK_MESSAGE) {
2706 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2707 "WPA: Unsupported SMK bit in key_info");
2708 goto out;
2709 }
2710
2711 if (!(key_info & WPA_KEY_INFO_ACK)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002712 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2713 "WPA: No Ack bit in key_info");
2714 goto out;
2715 }
2716
2717 if (key_info & WPA_KEY_INFO_REQUEST) {
2718 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2719 "WPA: EAPOL-Key with Request bit - dropped");
2720 goto out;
2721 }
2722
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002723 if ((key_info & WPA_KEY_INFO_MIC) &&
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002724 wpa_supplicant_verify_eapol_key_mic(sm, key, ver, tmp, data_len))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002725 goto out;
2726
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002727#ifdef CONFIG_FILS
2728 if (!mic_len && (key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
2729 if (wpa_supp_aead_decrypt(sm, tmp, data_len, &key_data_len))
2730 goto out;
2731 }
2732#endif /* CONFIG_FILS */
2733
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002734 if ((sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN) &&
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002735 (key_info & WPA_KEY_INFO_ENCR_KEY_DATA) && mic_len) {
Hai Shalomce48b4a2018-09-05 11:41:35 -07002736 /*
2737 * Only decrypt the Key Data field if the frame's authenticity
2738 * was verified. When using AES-SIV (FILS), the MIC flag is not
2739 * set, so this check should only be performed if mic_len != 0
2740 * which is the case in this code branch.
2741 */
2742 if (!(key_info & WPA_KEY_INFO_MIC)) {
2743 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
2744 "WPA: Ignore EAPOL-Key with encrypted but unauthenticated data");
2745 goto out;
2746 }
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002747 if (wpa_supplicant_decrypt_key_data(sm, key, mic_len,
2748 ver, key_data,
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002749 &key_data_len))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002750 goto out;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002751 }
2752
2753 if (key_info & WPA_KEY_INFO_KEY_TYPE) {
2754 if (key_info & WPA_KEY_INFO_KEY_INDEX_MASK) {
2755 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
2756 "WPA: Ignored EAPOL-Key (Pairwise) with "
2757 "non-zero key index");
2758 goto out;
2759 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002760 if (key_info & (WPA_KEY_INFO_MIC |
2761 WPA_KEY_INFO_ENCR_KEY_DATA)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002762 /* 3/4 4-Way Handshake */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002763 wpa_supplicant_process_3_of_4(sm, key, ver, key_data,
2764 key_data_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002765 } else {
2766 /* 1/4 4-Way Handshake */
2767 wpa_supplicant_process_1_of_4(sm, src_addr, key,
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002768 ver, key_data,
Sunil8cd6f4d2022-06-28 18:40:46 +00002769 key_data_len,
2770 encrypted);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002771 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002772 } else {
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002773 if ((mic_len && (key_info & WPA_KEY_INFO_MIC)) ||
2774 (!mic_len && (key_info & WPA_KEY_INFO_ENCR_KEY_DATA))) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002775 /* 1/2 Group Key Handshake */
2776 wpa_supplicant_process_1_of_2(sm, src_addr, key,
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002777 key_data, key_data_len,
2778 ver);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002779 } else {
2780 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002781 "WPA: EAPOL-Key (Group) without Mic/Encr bit - "
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002782 "dropped");
2783 }
2784 }
2785
2786 ret = 1;
2787
2788out:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002789 bin_clear_free(tmp, data_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002790 return ret;
2791}
2792
2793
2794#ifdef CONFIG_CTRL_IFACE
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002795static u32 wpa_key_mgmt_suite(struct wpa_sm *sm)
2796{
2797 switch (sm->key_mgmt) {
2798 case WPA_KEY_MGMT_IEEE8021X:
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002799 return ((sm->proto == WPA_PROTO_RSN ||
2800 sm->proto == WPA_PROTO_OSEN) ?
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002801 RSN_AUTH_KEY_MGMT_UNSPEC_802_1X :
2802 WPA_AUTH_KEY_MGMT_UNSPEC_802_1X);
2803 case WPA_KEY_MGMT_PSK:
2804 return (sm->proto == WPA_PROTO_RSN ?
2805 RSN_AUTH_KEY_MGMT_PSK_OVER_802_1X :
2806 WPA_AUTH_KEY_MGMT_PSK_OVER_802_1X);
2807#ifdef CONFIG_IEEE80211R
2808 case WPA_KEY_MGMT_FT_IEEE8021X:
2809 return RSN_AUTH_KEY_MGMT_FT_802_1X;
2810 case WPA_KEY_MGMT_FT_PSK:
2811 return RSN_AUTH_KEY_MGMT_FT_PSK;
2812#endif /* CONFIG_IEEE80211R */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002813 case WPA_KEY_MGMT_IEEE8021X_SHA256:
2814 return RSN_AUTH_KEY_MGMT_802_1X_SHA256;
2815 case WPA_KEY_MGMT_PSK_SHA256:
2816 return RSN_AUTH_KEY_MGMT_PSK_SHA256;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002817 case WPA_KEY_MGMT_CCKM:
2818 return (sm->proto == WPA_PROTO_RSN ?
2819 RSN_AUTH_KEY_MGMT_CCKM:
2820 WPA_AUTH_KEY_MGMT_CCKM);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002821 case WPA_KEY_MGMT_WPA_NONE:
2822 return WPA_AUTH_KEY_MGMT_NONE;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002823 case WPA_KEY_MGMT_IEEE8021X_SUITE_B:
2824 return RSN_AUTH_KEY_MGMT_802_1X_SUITE_B;
Dmitry Shmidt807291d2015-01-27 13:40:23 -08002825 case WPA_KEY_MGMT_IEEE8021X_SUITE_B_192:
2826 return RSN_AUTH_KEY_MGMT_802_1X_SUITE_B_192;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002827 default:
2828 return 0;
2829 }
2830}
2831
2832
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002833#define RSN_SUITE "%02x-%02x-%02x-%d"
2834#define RSN_SUITE_ARG(s) \
2835((s) >> 24) & 0xff, ((s) >> 16) & 0xff, ((s) >> 8) & 0xff, (s) & 0xff
2836
2837/**
2838 * wpa_sm_get_mib - Dump text list of MIB entries
2839 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2840 * @buf: Buffer for the list
2841 * @buflen: Length of the buffer
2842 * Returns: Number of bytes written to buffer
2843 *
2844 * This function is used fetch dot11 MIB variables.
2845 */
2846int wpa_sm_get_mib(struct wpa_sm *sm, char *buf, size_t buflen)
2847{
2848 char pmkid_txt[PMKID_LEN * 2 + 1];
Hai Shalome21d4e82020-04-29 16:34:06 -07002849 bool rsna;
2850 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002851 size_t len;
2852
2853 if (sm->cur_pmksa) {
2854 wpa_snprintf_hex(pmkid_txt, sizeof(pmkid_txt),
2855 sm->cur_pmksa->pmkid, PMKID_LEN);
2856 } else
2857 pmkid_txt[0] = '\0';
2858
Hai Shalome21d4e82020-04-29 16:34:06 -07002859 rsna = (wpa_key_mgmt_wpa_psk(sm->key_mgmt) ||
2860 wpa_key_mgmt_wpa_ieee8021x(sm->key_mgmt)) &&
2861 sm->proto == WPA_PROTO_RSN;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002862
2863 ret = os_snprintf(buf, buflen,
2864 "dot11RSNAOptionImplemented=TRUE\n"
2865 "dot11RSNAPreauthenticationImplemented=TRUE\n"
2866 "dot11RSNAEnabled=%s\n"
2867 "dot11RSNAPreauthenticationEnabled=%s\n"
2868 "dot11RSNAConfigVersion=%d\n"
2869 "dot11RSNAConfigPairwiseKeysSupported=5\n"
2870 "dot11RSNAConfigGroupCipherSize=%d\n"
2871 "dot11RSNAConfigPMKLifetime=%d\n"
2872 "dot11RSNAConfigPMKReauthThreshold=%d\n"
2873 "dot11RSNAConfigNumberOfPTKSAReplayCounters=1\n"
2874 "dot11RSNAConfigSATimeout=%d\n",
2875 rsna ? "TRUE" : "FALSE",
2876 rsna ? "TRUE" : "FALSE",
2877 RSN_VERSION,
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07002878 wpa_cipher_key_len(sm->group_cipher) * 8,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002879 sm->dot11RSNAConfigPMKLifetime,
2880 sm->dot11RSNAConfigPMKReauthThreshold,
2881 sm->dot11RSNAConfigSATimeout);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002882 if (os_snprintf_error(buflen, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002883 return 0;
2884 len = ret;
2885
2886 ret = os_snprintf(
2887 buf + len, buflen - len,
2888 "dot11RSNAAuthenticationSuiteSelected=" RSN_SUITE "\n"
2889 "dot11RSNAPairwiseCipherSelected=" RSN_SUITE "\n"
2890 "dot11RSNAGroupCipherSelected=" RSN_SUITE "\n"
2891 "dot11RSNAPMKIDUsed=%s\n"
2892 "dot11RSNAAuthenticationSuiteRequested=" RSN_SUITE "\n"
2893 "dot11RSNAPairwiseCipherRequested=" RSN_SUITE "\n"
2894 "dot11RSNAGroupCipherRequested=" RSN_SUITE "\n"
2895 "dot11RSNAConfigNumberOfGTKSAReplayCounters=0\n"
2896 "dot11RSNA4WayHandshakeFailures=%u\n",
2897 RSN_SUITE_ARG(wpa_key_mgmt_suite(sm)),
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07002898 RSN_SUITE_ARG(wpa_cipher_to_suite(sm->proto,
2899 sm->pairwise_cipher)),
2900 RSN_SUITE_ARG(wpa_cipher_to_suite(sm->proto,
2901 sm->group_cipher)),
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002902 pmkid_txt,
2903 RSN_SUITE_ARG(wpa_key_mgmt_suite(sm)),
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07002904 RSN_SUITE_ARG(wpa_cipher_to_suite(sm->proto,
2905 sm->pairwise_cipher)),
2906 RSN_SUITE_ARG(wpa_cipher_to_suite(sm->proto,
2907 sm->group_cipher)),
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002908 sm->dot11RSNA4WayHandshakeFailures);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002909 if (!os_snprintf_error(buflen - len, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002910 len += ret;
2911
2912 return (int) len;
2913}
2914#endif /* CONFIG_CTRL_IFACE */
2915
2916
2917static void wpa_sm_pmksa_free_cb(struct rsn_pmksa_cache_entry *entry,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002918 void *ctx, enum pmksa_free_reason reason)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002919{
2920 struct wpa_sm *sm = ctx;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002921 int deauth = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002922
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002923 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "RSN: PMKSA cache entry free_cb: "
2924 MACSTR " reason=%d", MAC2STR(entry->aa), reason);
2925
2926 if (sm->cur_pmksa == entry) {
2927 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2928 "RSN: %s current PMKSA entry",
2929 reason == PMKSA_REPLACE ? "replaced" : "removed");
2930 pmksa_cache_clear_current(sm);
2931
2932 /*
2933 * If an entry is simply being replaced, there's no need to
2934 * deauthenticate because it will be immediately re-added.
2935 * This happens when EAP authentication is completed again
2936 * (reauth or failed PMKSA caching attempt).
2937 */
2938 if (reason != PMKSA_REPLACE)
2939 deauth = 1;
2940 }
2941
2942 if (reason == PMKSA_EXPIRE &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002943 (sm->pmk_len == entry->pmk_len &&
2944 os_memcmp(sm->pmk, entry->pmk, sm->pmk_len) == 0)) {
2945 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002946 "RSN: deauthenticating due to expired PMK");
2947 pmksa_cache_clear_current(sm);
2948 deauth = 1;
2949 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002950
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002951 if (deauth) {
Roshan Pius3a1667e2018-07-03 15:17:14 -07002952 sm->pmk_len = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002953 os_memset(sm->pmk, 0, sizeof(sm->pmk));
2954 wpa_sm_deauthenticate(sm, WLAN_REASON_UNSPECIFIED);
2955 }
2956}
2957
2958
Hai Shalomc1a21442022-02-04 13:43:00 -08002959static bool wpa_sm_pmksa_is_current_cb(struct rsn_pmksa_cache_entry *entry,
2960 void *ctx)
2961{
2962 struct wpa_sm *sm = ctx;
2963
2964 return sm->cur_pmksa == entry;
2965}
2966
2967
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002968/**
2969 * wpa_sm_init - Initialize WPA state machine
2970 * @ctx: Context pointer for callbacks; this needs to be an allocated buffer
2971 * Returns: Pointer to the allocated WPA state machine data
2972 *
2973 * This function is used to allocate a new WPA state machine and the returned
2974 * value is passed to all WPA state machine calls.
2975 */
2976struct wpa_sm * wpa_sm_init(struct wpa_sm_ctx *ctx)
2977{
2978 struct wpa_sm *sm;
2979
2980 sm = os_zalloc(sizeof(*sm));
2981 if (sm == NULL)
2982 return NULL;
2983 dl_list_init(&sm->pmksa_candidates);
2984 sm->renew_snonce = 1;
2985 sm->ctx = ctx;
2986
2987 sm->dot11RSNAConfigPMKLifetime = 43200;
2988 sm->dot11RSNAConfigPMKReauthThreshold = 70;
2989 sm->dot11RSNAConfigSATimeout = 60;
2990
Hai Shalomc1a21442022-02-04 13:43:00 -08002991 sm->pmksa = pmksa_cache_init(wpa_sm_pmksa_free_cb,
2992 wpa_sm_pmksa_is_current_cb, sm, sm);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002993 if (sm->pmksa == NULL) {
2994 wpa_msg(sm->ctx->msg_ctx, MSG_ERROR,
2995 "RSN: PMKSA cache initialization failed");
2996 os_free(sm);
2997 return NULL;
2998 }
2999
3000 return sm;
3001}
3002
3003
3004/**
3005 * wpa_sm_deinit - Deinitialize WPA state machine
3006 * @sm: Pointer to WPA state machine data from wpa_sm_init()
3007 */
3008void wpa_sm_deinit(struct wpa_sm *sm)
3009{
3010 if (sm == NULL)
3011 return;
3012 pmksa_cache_deinit(sm->pmksa);
3013 eloop_cancel_timeout(wpa_sm_start_preauth, sm, NULL);
3014 eloop_cancel_timeout(wpa_sm_rekey_ptk, sm, NULL);
3015 os_free(sm->assoc_wpa_ie);
Hai Shalomc3565922019-10-28 11:58:20 -07003016 os_free(sm->assoc_rsnxe);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003017 os_free(sm->ap_wpa_ie);
3018 os_free(sm->ap_rsn_ie);
Hai Shalomc3565922019-10-28 11:58:20 -07003019 os_free(sm->ap_rsnxe);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003020 wpa_sm_drop_sa(sm);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003021 os_free(sm->ctx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003022#ifdef CONFIG_IEEE80211R
3023 os_free(sm->assoc_resp_ies);
3024#endif /* CONFIG_IEEE80211R */
Dmitry Shmidt55840ad2015-12-14 12:45:46 -08003025#ifdef CONFIG_TESTING_OPTIONS
3026 wpabuf_free(sm->test_assoc_ie);
3027#endif /* CONFIG_TESTING_OPTIONS */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003028#ifdef CONFIG_FILS_SK_PFS
3029 crypto_ecdh_deinit(sm->fils_ecdh);
3030#endif /* CONFIG_FILS_SK_PFS */
3031#ifdef CONFIG_FILS
3032 wpabuf_free(sm->fils_ft_ies);
3033#endif /* CONFIG_FILS */
3034#ifdef CONFIG_OWE
3035 crypto_ecdh_deinit(sm->owe_ecdh);
3036#endif /* CONFIG_OWE */
Hai Shalom021b0b52019-04-10 11:17:58 -07003037#ifdef CONFIG_DPP2
3038 wpabuf_clear_free(sm->dpp_z);
3039#endif /* CONFIG_DPP2 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003040 os_free(sm);
3041}
3042
3043
3044/**
3045 * wpa_sm_notify_assoc - Notify WPA state machine about association
3046 * @sm: Pointer to WPA state machine data from wpa_sm_init()
3047 * @bssid: The BSSID of the new association
3048 *
3049 * This function is called to let WPA state machine know that the connection
3050 * was established.
3051 */
3052void wpa_sm_notify_assoc(struct wpa_sm *sm, const u8 *bssid)
3053{
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02003054 int clear_keys = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003055
3056 if (sm == NULL)
3057 return;
3058
3059 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
3060 "WPA: Association event - clear replay counter");
3061 os_memcpy(sm->bssid, bssid, ETH_ALEN);
3062 os_memset(sm->rx_replay_counter, 0, WPA_REPLAY_COUNTER_LEN);
3063 sm->rx_replay_counter_set = 0;
3064 sm->renew_snonce = 1;
3065 if (os_memcmp(sm->preauth_bssid, bssid, ETH_ALEN) == 0)
3066 rsn_preauth_deinit(sm);
3067
3068#ifdef CONFIG_IEEE80211R
3069 if (wpa_ft_is_completed(sm)) {
3070 /*
3071 * Clear portValid to kick EAPOL state machine to re-enter
3072 * AUTHENTICATED state to get the EAPOL port Authorized.
3073 */
Hai Shalome21d4e82020-04-29 16:34:06 -07003074 eapol_sm_notify_portValid(sm->eapol, false);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003075 wpa_supplicant_key_neg_complete(sm, sm->bssid, 1);
3076
3077 /* Prepare for the next transition */
3078 wpa_ft_prepare_auth_request(sm, NULL);
3079
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02003080 clear_keys = 0;
Hai Shalom81f62d82019-07-22 12:10:00 -07003081 sm->ft_protocol = 1;
3082 } else {
3083 sm->ft_protocol = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003084 }
3085#endif /* CONFIG_IEEE80211R */
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003086#ifdef CONFIG_FILS
3087 if (sm->fils_completed) {
3088 /*
3089 * Clear portValid to kick EAPOL state machine to re-enter
3090 * AUTHENTICATED state to get the EAPOL port Authorized.
3091 */
3092 wpa_supplicant_key_neg_complete(sm, sm->bssid, 1);
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02003093 clear_keys = 0;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003094 }
3095#endif /* CONFIG_FILS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003096
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02003097 if (clear_keys) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003098 /*
3099 * IEEE 802.11, 8.4.10: Delete PTK SA on (re)association if
3100 * this is not part of a Fast BSS Transition.
3101 */
3102 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Clear old PTK");
3103 sm->ptk_set = 0;
Dmitry Shmidt61593f02014-04-21 16:27:35 -07003104 os_memset(&sm->ptk, 0, sizeof(sm->ptk));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003105 sm->tptk_set = 0;
Dmitry Shmidt61593f02014-04-21 16:27:35 -07003106 os_memset(&sm->tptk, 0, sizeof(sm->tptk));
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02003107 os_memset(&sm->gtk, 0, sizeof(sm->gtk));
Jouni Malinen58c0e962017-10-01 12:12:24 +03003108 os_memset(&sm->gtk_wnm_sleep, 0, sizeof(sm->gtk_wnm_sleep));
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02003109 os_memset(&sm->igtk, 0, sizeof(sm->igtk));
Jouni Malinen58c0e962017-10-01 12:12:24 +03003110 os_memset(&sm->igtk_wnm_sleep, 0, sizeof(sm->igtk_wnm_sleep));
Sunil8cd6f4d2022-06-28 18:40:46 +00003111 sm->tk_set = false;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003112 }
3113
3114#ifdef CONFIG_TDLS
3115 wpa_tdls_assoc(sm);
3116#endif /* CONFIG_TDLS */
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003117
3118#ifdef CONFIG_P2P
3119 os_memset(sm->p2p_ip_addr, 0, sizeof(sm->p2p_ip_addr));
3120#endif /* CONFIG_P2P */
Hai Shalomfdcde762020-04-02 11:19:20 -07003121
3122 sm->keyidx_active = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003123}
3124
3125
3126/**
3127 * wpa_sm_notify_disassoc - Notify WPA state machine about disassociation
3128 * @sm: Pointer to WPA state machine data from wpa_sm_init()
3129 *
3130 * This function is called to let WPA state machine know that the connection
3131 * was lost. This will abort any existing pre-authentication session.
3132 */
3133void wpa_sm_notify_disassoc(struct wpa_sm *sm)
3134{
Dmitry Shmidt4dd28dc2015-03-10 11:21:43 -07003135 eloop_cancel_timeout(wpa_sm_start_preauth, sm, NULL);
3136 eloop_cancel_timeout(wpa_sm_rekey_ptk, sm, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003137 rsn_preauth_deinit(sm);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003138 pmksa_cache_clear_current(sm);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003139 if (wpa_sm_get_state(sm) == WPA_4WAY_HANDSHAKE)
3140 sm->dot11RSNA4WayHandshakeFailures++;
3141#ifdef CONFIG_TDLS
3142 wpa_tdls_disassoc(sm);
3143#endif /* CONFIG_TDLS */
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003144#ifdef CONFIG_FILS
3145 sm->fils_completed = 0;
3146#endif /* CONFIG_FILS */
Jouni Malinen4283f9e2017-09-22 12:06:37 +03003147#ifdef CONFIG_IEEE80211R
3148 sm->ft_reassoc_completed = 0;
Hai Shalom81f62d82019-07-22 12:10:00 -07003149 sm->ft_protocol = 0;
Jouni Malinen4283f9e2017-09-22 12:06:37 +03003150#endif /* CONFIG_IEEE80211R */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003151
3152 /* Keys are not needed in the WPA state machine anymore */
3153 wpa_sm_drop_sa(sm);
Hai Shalomfdcde762020-04-02 11:19:20 -07003154 sm->keyidx_active = 0;
Dmitry Shmidt4dd28dc2015-03-10 11:21:43 -07003155
3156 sm->msg_3_of_4_ok = 0;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003157 os_memset(sm->bssid, 0, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003158}
3159
3160
3161/**
3162 * wpa_sm_set_pmk - Set PMK
3163 * @sm: Pointer to WPA state machine data from wpa_sm_init()
3164 * @pmk: The new PMK
3165 * @pmk_len: The length of the new PMK in bytes
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08003166 * @pmkid: Calculated PMKID
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003167 * @bssid: AA to add into PMKSA cache or %NULL to not cache the PMK
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003168 *
3169 * Configure the PMK for WPA state machine.
3170 */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003171void wpa_sm_set_pmk(struct wpa_sm *sm, const u8 *pmk, size_t pmk_len,
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08003172 const u8 *pmkid, const u8 *bssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003173{
3174 if (sm == NULL)
3175 return;
3176
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003177 wpa_hexdump_key(MSG_DEBUG, "WPA: Set PMK based on external data",
3178 pmk, pmk_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003179 sm->pmk_len = pmk_len;
3180 os_memcpy(sm->pmk, pmk, pmk_len);
3181
3182#ifdef CONFIG_IEEE80211R
3183 /* Set XXKey to be PSK for FT key derivation */
3184 sm->xxkey_len = pmk_len;
3185 os_memcpy(sm->xxkey, pmk, pmk_len);
3186#endif /* CONFIG_IEEE80211R */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003187
3188 if (bssid) {
Hai Shalomc1a21442022-02-04 13:43:00 -08003189 sm->cur_pmksa = pmksa_cache_add(sm->pmksa, pmk, pmk_len,
3190 pmkid, NULL, 0, bssid,
3191 sm->own_addr,
3192 sm->network_ctx, sm->key_mgmt,
3193 NULL);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003194 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003195}
3196
3197
3198/**
3199 * wpa_sm_set_pmk_from_pmksa - Set PMK based on the current PMKSA
3200 * @sm: Pointer to WPA state machine data from wpa_sm_init()
3201 *
3202 * Take the PMK from the current PMKSA into use. If no PMKSA is active, the PMK
3203 * will be cleared.
3204 */
3205void wpa_sm_set_pmk_from_pmksa(struct wpa_sm *sm)
3206{
3207 if (sm == NULL)
3208 return;
3209
3210 if (sm->cur_pmksa) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003211 wpa_hexdump_key(MSG_DEBUG,
3212 "WPA: Set PMK based on current PMKSA",
3213 sm->cur_pmksa->pmk, sm->cur_pmksa->pmk_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003214 sm->pmk_len = sm->cur_pmksa->pmk_len;
3215 os_memcpy(sm->pmk, sm->cur_pmksa->pmk, sm->pmk_len);
3216 } else {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003217 wpa_printf(MSG_DEBUG, "WPA: No current PMKSA - clear PMK");
3218 sm->pmk_len = 0;
3219 os_memset(sm->pmk, 0, PMK_LEN_MAX);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003220 }
3221}
3222
3223
3224/**
3225 * wpa_sm_set_fast_reauth - Set fast reauthentication (EAP) enabled/disabled
3226 * @sm: Pointer to WPA state machine data from wpa_sm_init()
3227 * @fast_reauth: Whether fast reauthentication (EAP) is allowed
3228 */
3229void wpa_sm_set_fast_reauth(struct wpa_sm *sm, int fast_reauth)
3230{
3231 if (sm)
3232 sm->fast_reauth = fast_reauth;
3233}
3234
3235
3236/**
3237 * wpa_sm_set_scard_ctx - Set context pointer for smartcard callbacks
3238 * @sm: Pointer to WPA state machine data from wpa_sm_init()
3239 * @scard_ctx: Context pointer for smartcard related callback functions
3240 */
3241void wpa_sm_set_scard_ctx(struct wpa_sm *sm, void *scard_ctx)
3242{
3243 if (sm == NULL)
3244 return;
3245 sm->scard_ctx = scard_ctx;
3246 if (sm->preauth_eapol)
3247 eapol_sm_register_scard_ctx(sm->preauth_eapol, scard_ctx);
3248}
3249
3250
3251/**
Hai Shalomfdcde762020-04-02 11:19:20 -07003252 * wpa_sm_set_config - Notification of current configuration change
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003253 * @sm: Pointer to WPA state machine data from wpa_sm_init()
3254 * @config: Pointer to current network configuration
3255 *
3256 * Notify WPA state machine that configuration has changed. config will be
3257 * stored as a backpointer to network configuration. This can be %NULL to clear
3258 * the stored pointed.
3259 */
3260void wpa_sm_set_config(struct wpa_sm *sm, struct rsn_supp_config *config)
3261{
3262 if (!sm)
3263 return;
3264
3265 if (config) {
3266 sm->network_ctx = config->network_ctx;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003267 sm->allowed_pairwise_cipher = config->allowed_pairwise_cipher;
3268 sm->proactive_key_caching = config->proactive_key_caching;
3269 sm->eap_workaround = config->eap_workaround;
3270 sm->eap_conf_ctx = config->eap_conf_ctx;
3271 if (config->ssid) {
3272 os_memcpy(sm->ssid, config->ssid, config->ssid_len);
3273 sm->ssid_len = config->ssid_len;
3274 } else
3275 sm->ssid_len = 0;
3276 sm->wpa_ptk_rekey = config->wpa_ptk_rekey;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003277 sm->p2p = config->p2p;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003278 sm->wpa_rsc_relaxation = config->wpa_rsc_relaxation;
Hai Shalomfdcde762020-04-02 11:19:20 -07003279 sm->owe_ptk_workaround = config->owe_ptk_workaround;
Hai Shalom60840252021-02-19 19:02:11 -08003280 sm->force_kdk_derivation = config->force_kdk_derivation;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003281#ifdef CONFIG_FILS
3282 if (config->fils_cache_id) {
3283 sm->fils_cache_id_set = 1;
3284 os_memcpy(sm->fils_cache_id, config->fils_cache_id,
3285 FILS_CACHE_ID_LEN);
3286 } else {
3287 sm->fils_cache_id_set = 0;
3288 }
3289#endif /* CONFIG_FILS */
Hai Shalomfdcde762020-04-02 11:19:20 -07003290 sm->beacon_prot = config->beacon_prot;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003291 } else {
3292 sm->network_ctx = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003293 sm->allowed_pairwise_cipher = 0;
3294 sm->proactive_key_caching = 0;
3295 sm->eap_workaround = 0;
3296 sm->eap_conf_ctx = NULL;
3297 sm->ssid_len = 0;
3298 sm->wpa_ptk_rekey = 0;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003299 sm->p2p = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003300 sm->wpa_rsc_relaxation = 0;
Hai Shalomfdcde762020-04-02 11:19:20 -07003301 sm->owe_ptk_workaround = 0;
3302 sm->beacon_prot = 0;
Hai Shalom60840252021-02-19 19:02:11 -08003303 sm->force_kdk_derivation = false;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003304 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003305}
3306
3307
3308/**
3309 * wpa_sm_set_own_addr - Set own MAC address
3310 * @sm: Pointer to WPA state machine data from wpa_sm_init()
3311 * @addr: Own MAC address
3312 */
3313void wpa_sm_set_own_addr(struct wpa_sm *sm, const u8 *addr)
3314{
3315 if (sm)
3316 os_memcpy(sm->own_addr, addr, ETH_ALEN);
3317}
3318
3319
3320/**
3321 * wpa_sm_set_ifname - Set network interface name
3322 * @sm: Pointer to WPA state machine data from wpa_sm_init()
3323 * @ifname: Interface name
3324 * @bridge_ifname: Optional bridge interface name (for pre-auth)
3325 */
3326void wpa_sm_set_ifname(struct wpa_sm *sm, const char *ifname,
3327 const char *bridge_ifname)
3328{
3329 if (sm) {
3330 sm->ifname = ifname;
3331 sm->bridge_ifname = bridge_ifname;
3332 }
3333}
3334
3335
3336/**
3337 * wpa_sm_set_eapol - Set EAPOL state machine pointer
3338 * @sm: Pointer to WPA state machine data from wpa_sm_init()
3339 * @eapol: Pointer to EAPOL state machine allocated with eapol_sm_init()
3340 */
3341void wpa_sm_set_eapol(struct wpa_sm *sm, struct eapol_sm *eapol)
3342{
3343 if (sm)
3344 sm->eapol = eapol;
3345}
3346
3347
3348/**
3349 * wpa_sm_set_param - Set WPA state machine parameters
3350 * @sm: Pointer to WPA state machine data from wpa_sm_init()
3351 * @param: Parameter field
3352 * @value: Parameter value
3353 * Returns: 0 on success, -1 on failure
3354 */
3355int wpa_sm_set_param(struct wpa_sm *sm, enum wpa_sm_conf_params param,
3356 unsigned int value)
3357{
3358 int ret = 0;
3359
3360 if (sm == NULL)
3361 return -1;
3362
3363 switch (param) {
3364 case RSNA_PMK_LIFETIME:
3365 if (value > 0)
3366 sm->dot11RSNAConfigPMKLifetime = value;
3367 else
3368 ret = -1;
3369 break;
3370 case RSNA_PMK_REAUTH_THRESHOLD:
3371 if (value > 0 && value <= 100)
3372 sm->dot11RSNAConfigPMKReauthThreshold = value;
3373 else
3374 ret = -1;
3375 break;
3376 case RSNA_SA_TIMEOUT:
3377 if (value > 0)
3378 sm->dot11RSNAConfigSATimeout = value;
3379 else
3380 ret = -1;
3381 break;
3382 case WPA_PARAM_PROTO:
3383 sm->proto = value;
3384 break;
3385 case WPA_PARAM_PAIRWISE:
3386 sm->pairwise_cipher = value;
3387 break;
3388 case WPA_PARAM_GROUP:
3389 sm->group_cipher = value;
3390 break;
3391 case WPA_PARAM_KEY_MGMT:
3392 sm->key_mgmt = value;
3393 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003394 case WPA_PARAM_MGMT_GROUP:
3395 sm->mgmt_group_cipher = value;
3396 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003397 case WPA_PARAM_RSN_ENABLED:
3398 sm->rsn_enabled = value;
3399 break;
3400 case WPA_PARAM_MFP:
3401 sm->mfp = value;
3402 break;
Hai Shalom74f70d42019-02-11 14:42:39 -08003403 case WPA_PARAM_OCV:
3404 sm->ocv = value;
3405 break;
Hai Shalomc3565922019-10-28 11:58:20 -07003406 case WPA_PARAM_SAE_PWE:
3407 sm->sae_pwe = value;
3408 break;
Hai Shalom899fcc72020-10-19 14:38:18 -07003409 case WPA_PARAM_SAE_PK:
3410 sm->sae_pk = value;
3411 break;
Hai Shalomfdcde762020-04-02 11:19:20 -07003412 case WPA_PARAM_DENY_PTK0_REKEY:
3413 sm->wpa_deny_ptk0_rekey = value;
3414 break;
3415 case WPA_PARAM_EXT_KEY_ID:
3416 sm->ext_key_id = value;
3417 break;
3418 case WPA_PARAM_USE_EXT_KEY_ID:
3419 sm->use_ext_key_id = value;
3420 break;
Hai Shalomb755a2a2020-04-23 21:49:02 -07003421#ifdef CONFIG_TESTING_OPTIONS
3422 case WPA_PARAM_FT_RSNXE_USED:
3423 sm->ft_rsnxe_used = value;
3424 break;
Hai Shalom899fcc72020-10-19 14:38:18 -07003425 case WPA_PARAM_OCI_FREQ_EAPOL:
3426 sm->oci_freq_override_eapol = value;
3427 break;
3428 case WPA_PARAM_OCI_FREQ_EAPOL_G2:
3429 sm->oci_freq_override_eapol_g2 = value;
3430 break;
3431 case WPA_PARAM_OCI_FREQ_FT_ASSOC:
3432 sm->oci_freq_override_ft_assoc = value;
3433 break;
3434 case WPA_PARAM_OCI_FREQ_FILS_ASSOC:
3435 sm->oci_freq_override_fils_assoc = value;
3436 break;
Sunil Ravia04bd252022-05-02 22:54:18 -07003437 case WPA_PARAM_DISABLE_EAPOL_G2_TX:
3438 sm->disable_eapol_g2_tx = value;
3439 break;
Hai Shalomb755a2a2020-04-23 21:49:02 -07003440#endif /* CONFIG_TESTING_OPTIONS */
Hai Shalom4fbc08f2020-05-18 12:37:00 -07003441#ifdef CONFIG_DPP2
3442 case WPA_PARAM_DPP_PFS:
3443 sm->dpp_pfs = value;
3444 break;
3445#endif /* CONFIG_DPP2 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003446 default:
3447 break;
3448 }
3449
3450 return ret;
3451}
3452
3453
3454/**
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003455 * wpa_sm_get_status - Get WPA state machine
3456 * @sm: Pointer to WPA state machine data from wpa_sm_init()
3457 * @buf: Buffer for status information
3458 * @buflen: Maximum buffer length
3459 * @verbose: Whether to include verbose status information
3460 * Returns: Number of bytes written to buf.
3461 *
3462 * Query WPA state machine for status information. This function fills in
3463 * a text area with current status information. If the buffer (buf) is not
3464 * large enough, status information will be truncated to fit the buffer.
3465 */
3466int wpa_sm_get_status(struct wpa_sm *sm, char *buf, size_t buflen,
3467 int verbose)
3468{
3469 char *pos = buf, *end = buf + buflen;
3470 int ret;
3471
3472 ret = os_snprintf(pos, end - pos,
3473 "pairwise_cipher=%s\n"
3474 "group_cipher=%s\n"
3475 "key_mgmt=%s\n",
3476 wpa_cipher_txt(sm->pairwise_cipher),
3477 wpa_cipher_txt(sm->group_cipher),
3478 wpa_key_mgmt_txt(sm->key_mgmt, sm->proto));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003479 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003480 return pos - buf;
3481 pos += ret;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003482
Hai Shalom4fbc08f2020-05-18 12:37:00 -07003483#ifdef CONFIG_DPP2
3484 if (sm->key_mgmt == WPA_KEY_MGMT_DPP && sm->dpp_z) {
3485 ret = os_snprintf(pos, end - pos, "dpp_pfs=1\n");
3486 if (os_snprintf_error(end - pos, ret))
3487 return pos - buf;
3488 pos += ret;
3489 }
3490#endif /* CONFIG_DPP2 */
3491
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003492 if (sm->mfp != NO_MGMT_FRAME_PROTECTION && sm->ap_rsn_ie) {
3493 struct wpa_ie_data rsn;
3494 if (wpa_parse_wpa_ie_rsn(sm->ap_rsn_ie, sm->ap_rsn_ie_len, &rsn)
3495 >= 0 &&
3496 rsn.capabilities & (WPA_CAPABILITY_MFPR |
3497 WPA_CAPABILITY_MFPC)) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003498 ret = os_snprintf(pos, end - pos, "pmf=%d\n"
3499 "mgmt_group_cipher=%s\n",
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003500 (rsn.capabilities &
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003501 WPA_CAPABILITY_MFPR) ? 2 : 1,
3502 wpa_cipher_txt(
3503 sm->mgmt_group_cipher));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003504 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003505 return pos - buf;
3506 pos += ret;
3507 }
3508 }
3509
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003510 return pos - buf;
3511}
3512
3513
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07003514int wpa_sm_pmf_enabled(struct wpa_sm *sm)
3515{
3516 struct wpa_ie_data rsn;
3517
3518 if (sm->mfp == NO_MGMT_FRAME_PROTECTION || !sm->ap_rsn_ie)
3519 return 0;
3520
3521 if (wpa_parse_wpa_ie_rsn(sm->ap_rsn_ie, sm->ap_rsn_ie_len, &rsn) >= 0 &&
3522 rsn.capabilities & (WPA_CAPABILITY_MFPR | WPA_CAPABILITY_MFPC))
3523 return 1;
3524
3525 return 0;
3526}
3527
3528
Hai Shalomfdcde762020-04-02 11:19:20 -07003529int wpa_sm_ext_key_id(struct wpa_sm *sm)
3530{
3531 return sm ? sm->ext_key_id : 0;
3532}
3533
3534
3535int wpa_sm_ext_key_id_active(struct wpa_sm *sm)
3536{
3537 return sm ? sm->use_ext_key_id : 0;
3538}
3539
3540
Hai Shalom74f70d42019-02-11 14:42:39 -08003541int wpa_sm_ocv_enabled(struct wpa_sm *sm)
3542{
3543 struct wpa_ie_data rsn;
3544
3545 if (!sm->ocv || !sm->ap_rsn_ie)
3546 return 0;
3547
3548 return wpa_parse_wpa_ie_rsn(sm->ap_rsn_ie, sm->ap_rsn_ie_len,
3549 &rsn) >= 0 &&
3550 (rsn.capabilities & WPA_CAPABILITY_OCVC);
3551}
3552
3553
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003554/**
3555 * wpa_sm_set_assoc_wpa_ie_default - Generate own WPA/RSN IE from configuration
3556 * @sm: Pointer to WPA state machine data from wpa_sm_init()
3557 * @wpa_ie: Pointer to buffer for WPA/RSN IE
3558 * @wpa_ie_len: Pointer to the length of the wpa_ie buffer
3559 * Returns: 0 on success, -1 on failure
3560 */
3561int wpa_sm_set_assoc_wpa_ie_default(struct wpa_sm *sm, u8 *wpa_ie,
3562 size_t *wpa_ie_len)
3563{
3564 int res;
3565
3566 if (sm == NULL)
3567 return -1;
3568
Dmitry Shmidt55840ad2015-12-14 12:45:46 -08003569#ifdef CONFIG_TESTING_OPTIONS
3570 if (sm->test_assoc_ie) {
3571 wpa_printf(MSG_DEBUG,
3572 "TESTING: Replace association WPA/RSN IE");
3573 if (*wpa_ie_len < wpabuf_len(sm->test_assoc_ie))
3574 return -1;
3575 os_memcpy(wpa_ie, wpabuf_head(sm->test_assoc_ie),
3576 wpabuf_len(sm->test_assoc_ie));
3577 res = wpabuf_len(sm->test_assoc_ie);
3578 } else
3579#endif /* CONFIG_TESTING_OPTIONS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003580 res = wpa_gen_wpa_ie(sm, wpa_ie, *wpa_ie_len);
3581 if (res < 0)
3582 return -1;
3583 *wpa_ie_len = res;
3584
3585 wpa_hexdump(MSG_DEBUG, "WPA: Set own WPA IE default",
3586 wpa_ie, *wpa_ie_len);
3587
3588 if (sm->assoc_wpa_ie == NULL) {
3589 /*
3590 * Make a copy of the WPA/RSN IE so that 4-Way Handshake gets
3591 * the correct version of the IE even if PMKSA caching is
3592 * aborted (which would remove PMKID from IE generation).
3593 */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003594 sm->assoc_wpa_ie = os_memdup(wpa_ie, *wpa_ie_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003595 if (sm->assoc_wpa_ie == NULL)
3596 return -1;
3597
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003598 sm->assoc_wpa_ie_len = *wpa_ie_len;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003599 } else {
3600 wpa_hexdump(MSG_DEBUG,
3601 "WPA: Leave previously set WPA IE default",
3602 sm->assoc_wpa_ie, sm->assoc_wpa_ie_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003603 }
3604
3605 return 0;
3606}
3607
3608
3609/**
3610 * wpa_sm_set_assoc_wpa_ie - Set own WPA/RSN IE from (Re)AssocReq
3611 * @sm: Pointer to WPA state machine data from wpa_sm_init()
3612 * @ie: Pointer to IE data (starting from id)
3613 * @len: IE length
3614 * Returns: 0 on success, -1 on failure
3615 *
3616 * Inform WPA state machine about the WPA/RSN IE used in (Re)Association
3617 * Request frame. The IE will be used to override the default value generated
3618 * with wpa_sm_set_assoc_wpa_ie_default().
3619 */
3620int wpa_sm_set_assoc_wpa_ie(struct wpa_sm *sm, const u8 *ie, size_t len)
3621{
3622 if (sm == NULL)
3623 return -1;
3624
3625 os_free(sm->assoc_wpa_ie);
3626 if (ie == NULL || len == 0) {
3627 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
3628 "WPA: clearing own WPA/RSN IE");
3629 sm->assoc_wpa_ie = NULL;
3630 sm->assoc_wpa_ie_len = 0;
3631 } else {
3632 wpa_hexdump(MSG_DEBUG, "WPA: set own WPA/RSN IE", ie, len);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003633 sm->assoc_wpa_ie = os_memdup(ie, len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003634 if (sm->assoc_wpa_ie == NULL)
3635 return -1;
3636
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003637 sm->assoc_wpa_ie_len = len;
3638 }
3639
3640 return 0;
3641}
3642
3643
3644/**
Hai Shalomc3565922019-10-28 11:58:20 -07003645 * wpa_sm_set_assoc_rsnxe_default - Generate own RSNXE from configuration
3646 * @sm: Pointer to WPA state machine data from wpa_sm_init()
3647 * @rsnxe: Pointer to buffer for RSNXE
3648 * @rsnxe_len: Pointer to the length of the rsne buffer
3649 * Returns: 0 on success, -1 on failure
3650 */
3651int wpa_sm_set_assoc_rsnxe_default(struct wpa_sm *sm, u8 *rsnxe,
3652 size_t *rsnxe_len)
3653{
3654 int res;
3655
3656 if (!sm)
3657 return -1;
3658
3659 res = wpa_gen_rsnxe(sm, rsnxe, *rsnxe_len);
3660 if (res < 0)
3661 return -1;
3662 *rsnxe_len = res;
3663
3664 wpa_hexdump(MSG_DEBUG, "RSN: Set own RSNXE default", rsnxe, *rsnxe_len);
3665
3666 if (sm->assoc_rsnxe) {
3667 wpa_hexdump(MSG_DEBUG,
3668 "RSN: Leave previously set RSNXE default",
3669 sm->assoc_rsnxe, sm->assoc_rsnxe_len);
3670 } else if (*rsnxe_len > 0) {
3671 /*
3672 * Make a copy of the RSNXE so that 4-Way Handshake gets the
3673 * correct version of the IE even if it gets changed.
3674 */
3675 sm->assoc_rsnxe = os_memdup(rsnxe, *rsnxe_len);
3676 if (!sm->assoc_rsnxe)
3677 return -1;
3678
3679 sm->assoc_rsnxe_len = *rsnxe_len;
3680 }
3681
3682 return 0;
3683}
3684
3685
3686/**
3687 * wpa_sm_set_assoc_rsnxe - Set own RSNXE from (Re)AssocReq
3688 * @sm: Pointer to WPA state machine data from wpa_sm_init()
3689 * @ie: Pointer to IE data (starting from id)
3690 * @len: IE length
3691 * Returns: 0 on success, -1 on failure
3692 *
3693 * Inform WPA state machine about the RSNXE used in (Re)Association Request
3694 * frame. The IE will be used to override the default value generated
3695 * with wpa_sm_set_assoc_rsnxe_default().
3696 */
3697int wpa_sm_set_assoc_rsnxe(struct wpa_sm *sm, const u8 *ie, size_t len)
3698{
3699 if (!sm)
3700 return -1;
3701
3702 os_free(sm->assoc_rsnxe);
3703 if (!ie || len == 0) {
3704 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
3705 "RSN: clearing own RSNXE");
3706 sm->assoc_rsnxe = NULL;
3707 sm->assoc_rsnxe_len = 0;
3708 } else {
3709 wpa_hexdump(MSG_DEBUG, "RSN: set own RSNXE", ie, len);
3710 sm->assoc_rsnxe = os_memdup(ie, len);
3711 if (!sm->assoc_rsnxe)
3712 return -1;
3713
3714 sm->assoc_rsnxe_len = len;
3715 }
3716
3717 return 0;
3718}
3719
3720
3721/**
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003722 * wpa_sm_set_ap_wpa_ie - Set AP WPA IE from Beacon/ProbeResp
3723 * @sm: Pointer to WPA state machine data from wpa_sm_init()
3724 * @ie: Pointer to IE data (starting from id)
3725 * @len: IE length
3726 * Returns: 0 on success, -1 on failure
3727 *
3728 * Inform WPA state machine about the WPA IE used in Beacon / Probe Response
3729 * frame.
3730 */
3731int wpa_sm_set_ap_wpa_ie(struct wpa_sm *sm, const u8 *ie, size_t len)
3732{
3733 if (sm == NULL)
3734 return -1;
3735
3736 os_free(sm->ap_wpa_ie);
3737 if (ie == NULL || len == 0) {
3738 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
3739 "WPA: clearing AP WPA IE");
3740 sm->ap_wpa_ie = NULL;
3741 sm->ap_wpa_ie_len = 0;
3742 } else {
3743 wpa_hexdump(MSG_DEBUG, "WPA: set AP WPA IE", ie, len);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003744 sm->ap_wpa_ie = os_memdup(ie, len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003745 if (sm->ap_wpa_ie == NULL)
3746 return -1;
3747
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003748 sm->ap_wpa_ie_len = len;
3749 }
3750
3751 return 0;
3752}
3753
3754
3755/**
3756 * wpa_sm_set_ap_rsn_ie - Set AP RSN IE from Beacon/ProbeResp
3757 * @sm: Pointer to WPA state machine data from wpa_sm_init()
3758 * @ie: Pointer to IE data (starting from id)
3759 * @len: IE length
3760 * Returns: 0 on success, -1 on failure
3761 *
3762 * Inform WPA state machine about the RSN IE used in Beacon / Probe Response
3763 * frame.
3764 */
3765int wpa_sm_set_ap_rsn_ie(struct wpa_sm *sm, const u8 *ie, size_t len)
3766{
3767 if (sm == NULL)
3768 return -1;
3769
3770 os_free(sm->ap_rsn_ie);
3771 if (ie == NULL || len == 0) {
3772 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
3773 "WPA: clearing AP RSN IE");
3774 sm->ap_rsn_ie = NULL;
3775 sm->ap_rsn_ie_len = 0;
3776 } else {
3777 wpa_hexdump(MSG_DEBUG, "WPA: set AP RSN IE", ie, len);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003778 sm->ap_rsn_ie = os_memdup(ie, len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003779 if (sm->ap_rsn_ie == NULL)
3780 return -1;
3781
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003782 sm->ap_rsn_ie_len = len;
3783 }
3784
3785 return 0;
3786}
3787
3788
3789/**
Hai Shalomc3565922019-10-28 11:58:20 -07003790 * wpa_sm_set_ap_rsnxe - Set AP RSNXE from Beacon/ProbeResp
3791 * @sm: Pointer to WPA state machine data from wpa_sm_init()
3792 * @ie: Pointer to IE data (starting from id)
3793 * @len: IE length
3794 * Returns: 0 on success, -1 on failure
3795 *
3796 * Inform WPA state machine about the RSNXE used in Beacon / Probe Response
3797 * frame.
3798 */
3799int wpa_sm_set_ap_rsnxe(struct wpa_sm *sm, const u8 *ie, size_t len)
3800{
3801 if (!sm)
3802 return -1;
3803
3804 os_free(sm->ap_rsnxe);
3805 if (!ie || len == 0) {
3806 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: clearing AP RSNXE");
3807 sm->ap_rsnxe = NULL;
3808 sm->ap_rsnxe_len = 0;
3809 } else {
3810 wpa_hexdump(MSG_DEBUG, "WPA: set AP RSNXE", ie, len);
3811 sm->ap_rsnxe = os_memdup(ie, len);
3812 if (!sm->ap_rsnxe)
3813 return -1;
3814
3815 sm->ap_rsnxe_len = len;
3816 }
3817
3818 return 0;
3819}
3820
3821
3822/**
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003823 * wpa_sm_parse_own_wpa_ie - Parse own WPA/RSN IE
3824 * @sm: Pointer to WPA state machine data from wpa_sm_init()
3825 * @data: Pointer to data area for parsing results
3826 * Returns: 0 on success, -1 if IE is not known, or -2 on parsing failure
3827 *
3828 * Parse the contents of the own WPA or RSN IE from (Re)AssocReq and write the
3829 * parsed data into data.
3830 */
3831int wpa_sm_parse_own_wpa_ie(struct wpa_sm *sm, struct wpa_ie_data *data)
3832{
3833 if (sm == NULL)
3834 return -1;
3835
3836 if (sm->assoc_wpa_ie == NULL) {
3837 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
3838 "WPA: No WPA/RSN IE available from association info");
3839 return -1;
3840 }
3841 if (wpa_parse_wpa_ie(sm->assoc_wpa_ie, sm->assoc_wpa_ie_len, data))
3842 return -2;
3843 return 0;
3844}
3845
3846
3847int wpa_sm_pmksa_cache_list(struct wpa_sm *sm, char *buf, size_t len)
3848{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003849 return pmksa_cache_list(sm->pmksa, buf, len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003850}
3851
3852
Dmitry Shmidt29333592017-01-09 12:27:11 -08003853struct rsn_pmksa_cache_entry * wpa_sm_pmksa_cache_head(struct wpa_sm *sm)
3854{
3855 return pmksa_cache_head(sm->pmksa);
3856}
3857
3858
3859struct rsn_pmksa_cache_entry *
3860wpa_sm_pmksa_cache_add_entry(struct wpa_sm *sm,
3861 struct rsn_pmksa_cache_entry * entry)
3862{
3863 return pmksa_cache_add_entry(sm->pmksa, entry);
3864}
3865
3866
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003867void wpa_sm_pmksa_cache_add(struct wpa_sm *sm, const u8 *pmk, size_t pmk_len,
3868 const u8 *pmkid, const u8 *bssid,
3869 const u8 *fils_cache_id)
3870{
3871 sm->cur_pmksa = pmksa_cache_add(sm->pmksa, pmk, pmk_len, pmkid, NULL, 0,
3872 bssid, sm->own_addr, sm->network_ctx,
3873 sm->key_mgmt, fils_cache_id);
3874}
3875
3876
3877int wpa_sm_pmksa_exists(struct wpa_sm *sm, const u8 *bssid,
3878 const void *network_ctx)
3879{
Roshan Pius3a1667e2018-07-03 15:17:14 -07003880 return pmksa_cache_get(sm->pmksa, bssid, NULL, network_ctx, 0) != NULL;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003881}
3882
3883
Hai Shalom60840252021-02-19 19:02:11 -08003884struct rsn_pmksa_cache_entry * wpa_sm_pmksa_cache_get(struct wpa_sm *sm,
3885 const u8 *aa,
3886 const u8 *pmkid,
3887 const void *network_ctx,
3888 int akmp)
3889{
3890 return pmksa_cache_get(sm->pmksa, aa, pmkid, network_ctx, akmp);
3891}
3892
3893
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003894void wpa_sm_drop_sa(struct wpa_sm *sm)
3895{
3896 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Clear old PMK and PTK");
3897 sm->ptk_set = 0;
3898 sm->tptk_set = 0;
Sunil8cd6f4d2022-06-28 18:40:46 +00003899 sm->tk_set = false;
Roshan Pius3a1667e2018-07-03 15:17:14 -07003900 sm->pmk_len = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003901 os_memset(sm->pmk, 0, sizeof(sm->pmk));
3902 os_memset(&sm->ptk, 0, sizeof(sm->ptk));
3903 os_memset(&sm->tptk, 0, sizeof(sm->tptk));
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02003904 os_memset(&sm->gtk, 0, sizeof(sm->gtk));
Jouni Malinen58c0e962017-10-01 12:12:24 +03003905 os_memset(&sm->gtk_wnm_sleep, 0, sizeof(sm->gtk_wnm_sleep));
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02003906 os_memset(&sm->igtk, 0, sizeof(sm->igtk));
Jouni Malinen58c0e962017-10-01 12:12:24 +03003907 os_memset(&sm->igtk_wnm_sleep, 0, sizeof(sm->igtk_wnm_sleep));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003908#ifdef CONFIG_IEEE80211R
3909 os_memset(sm->xxkey, 0, sizeof(sm->xxkey));
Roshan Pius3a1667e2018-07-03 15:17:14 -07003910 sm->xxkey_len = 0;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003911 os_memset(sm->pmk_r0, 0, sizeof(sm->pmk_r0));
Roshan Pius3a1667e2018-07-03 15:17:14 -07003912 sm->pmk_r0_len = 0;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003913 os_memset(sm->pmk_r1, 0, sizeof(sm->pmk_r1));
Roshan Pius3a1667e2018-07-03 15:17:14 -07003914 sm->pmk_r1_len = 0;
Hai Shalom60840252021-02-19 19:02:11 -08003915#ifdef CONFIG_PASN
3916 os_free(sm->pasn_r1kh);
3917 sm->pasn_r1kh = NULL;
3918 sm->n_pasn_r1kh = 0;
3919#endif /* CONFIG_PASN */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003920#endif /* CONFIG_IEEE80211R */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003921}
3922
3923
3924int wpa_sm_has_ptk(struct wpa_sm *sm)
3925{
3926 if (sm == NULL)
3927 return 0;
3928 return sm->ptk_set;
3929}
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003930
3931
Hai Shalomfdcde762020-04-02 11:19:20 -07003932int wpa_sm_has_ptk_installed(struct wpa_sm *sm)
3933{
3934 if (!sm)
3935 return 0;
Sunil8cd6f4d2022-06-28 18:40:46 +00003936 return sm->tk_set || sm->ptk.installed;
Hai Shalomfdcde762020-04-02 11:19:20 -07003937}
3938
3939
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003940void wpa_sm_update_replay_ctr(struct wpa_sm *sm, const u8 *replay_ctr)
3941{
3942 os_memcpy(sm->rx_replay_counter, replay_ctr, WPA_REPLAY_COUNTER_LEN);
3943}
3944
3945
3946void wpa_sm_pmksa_cache_flush(struct wpa_sm *sm, void *network_ctx)
3947{
Hai Shalomc1a21442022-02-04 13:43:00 -08003948 pmksa_cache_flush(sm->pmksa, network_ctx, NULL, 0, false);
3949}
3950
3951
3952void wpa_sm_external_pmksa_cache_flush(struct wpa_sm *sm, void *network_ctx)
3953{
3954 pmksa_cache_flush(sm->pmksa, network_ctx, NULL, 0, true);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003955}
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003956
Andy Kuoaba17c12022-04-14 16:05:31 +08003957#if defined(CONFIG_DRIVER_NL80211_BRCM) || defined(CONFIG_DRIVER_NL80211_SYNA)
Mir Ali677e7482020-11-12 19:49:02 +05303958void wpa_sm_install_pmk(struct wpa_sm *sm)
3959{
3960 /* In case the driver wants to handle re-assocs, pass it down the PMK. */
3961 if (wpa_sm_set_key(sm, wpa_cipher_to_alg(sm->pairwise_cipher), NULL, 0, 0, NULL, 0,
3962 (u8*)sm->pmk, sm->pmk_len, KEY_FLAG_PMK) < 0) {
3963 wpa_hexdump(MSG_DEBUG, "PSK: Install PMK to the driver for driver reassociations",
3964 (u8*)sm->pmk, sm->pmk_len);
3965 /* No harm if the driver doesn't support. */
3966 wpa_msg(sm->ctx->msg_ctx, MSG_DEBUG,
3967 "WPA: Failed to set PMK to the driver");
3968 }
3969}
Mir Alieaaf04e2021-06-07 12:17:29 +05303970
3971void wpa_sm_notify_brcm_ft_reassoc(struct wpa_sm *sm, const u8 *bssid)
3972{
3973 u8 buf[256];
3974 struct wpa_supplicant *wpa_s = sm->ctx->ctx;
3975
3976 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
3977 "WPA: BRCM FT Reassociation event - clear replay counter");
3978 os_memcpy(sm->bssid, bssid, ETH_ALEN);
3979 os_memset(sm->rx_replay_counter, 0, WPA_REPLAY_COUNTER_LEN);
3980 sm->rx_replay_counter_set = 0;
3981
3982 if (wpa_drv_driver_cmd(wpa_s, "GET_FTKEY", (char *)buf, sizeof(buf)) < 0) {
3983 wpa_msg(sm->ctx->msg_ctx, MSG_ERROR,
3984 "WPA: Failed to get FT KEY information");
3985 wpa_supplicant_deauthenticate(
3986 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
3987
3988 } else {
3989 /* update kck and kek */
3990 os_memcpy(sm->ptk.kck, buf, 16);
3991 os_memcpy(sm->ptk.kek, buf + 16, 16);
3992 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
3993 "WPA: Updated KCK and KEK after FT reassoc");
3994 }
3995}
Andy Kuoaba17c12022-04-14 16:05:31 +08003996#endif /* CONFIG_DRIVER_NL80211_BRCM || CONFIG_DRIVER_NL80211_SYNA */
3997
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003998
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003999#ifdef CONFIG_WNM
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004000int wpa_wnmsleep_install_key(struct wpa_sm *sm, u8 subelem_id, u8 *buf)
4001{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004002 u16 keyinfo;
4003 u8 keylen; /* plaintext key len */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004004 u8 *key_rsc;
4005
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004006 if (subelem_id == WNM_SLEEP_SUBELEM_GTK) {
Dmitry Shmidt61593f02014-04-21 16:27:35 -07004007 struct wpa_gtk_data gd;
4008
4009 os_memset(&gd, 0, sizeof(gd));
4010 keylen = wpa_cipher_key_len(sm->group_cipher);
4011 gd.key_rsc_len = wpa_cipher_rsc_len(sm->group_cipher);
4012 gd.alg = wpa_cipher_to_alg(sm->group_cipher);
4013 if (gd.alg == WPA_ALG_NONE) {
4014 wpa_printf(MSG_DEBUG, "Unsupported group cipher suite");
4015 return -1;
4016 }
4017
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004018 key_rsc = buf + 5;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004019 keyinfo = WPA_GET_LE16(buf + 2);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004020 gd.gtk_len = keylen;
4021 if (gd.gtk_len != buf[4]) {
4022 wpa_printf(MSG_DEBUG, "GTK len mismatch len %d vs %d",
4023 gd.gtk_len, buf[4]);
4024 return -1;
4025 }
4026 gd.keyidx = keyinfo & 0x03; /* B0 - B1 */
4027 gd.tx = wpa_supplicant_gtk_tx_bit_workaround(
4028 sm, !!(keyinfo & WPA_KEY_INFO_TXRX));
4029
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004030 os_memcpy(gd.gtk, buf + 13, gd.gtk_len);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004031
4032 wpa_hexdump_key(MSG_DEBUG, "Install GTK (WNM SLEEP)",
4033 gd.gtk, gd.gtk_len);
Jouni Malinen58c0e962017-10-01 12:12:24 +03004034 if (wpa_supplicant_install_gtk(sm, &gd, key_rsc, 1)) {
Hai Shalom81f62d82019-07-22 12:10:00 -07004035 forced_memzero(&gd, sizeof(gd));
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004036 wpa_printf(MSG_DEBUG, "Failed to install the GTK in "
4037 "WNM mode");
4038 return -1;
4039 }
Hai Shalom81f62d82019-07-22 12:10:00 -07004040 forced_memzero(&gd, sizeof(gd));
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004041 } else if (subelem_id == WNM_SLEEP_SUBELEM_IGTK) {
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02004042 const struct wpa_igtk_kde *igtk;
Dmitry Shmidt61593f02014-04-21 16:27:35 -07004043
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02004044 igtk = (const struct wpa_igtk_kde *) (buf + 2);
Jouni Malinen58c0e962017-10-01 12:12:24 +03004045 if (wpa_supplicant_install_igtk(sm, igtk, 1) < 0)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004046 return -1;
Hai Shalomfdcde762020-04-02 11:19:20 -07004047 } else if (subelem_id == WNM_SLEEP_SUBELEM_BIGTK) {
4048 const struct wpa_bigtk_kde *bigtk;
4049
4050 bigtk = (const struct wpa_bigtk_kde *) (buf + 2);
4051 if (sm->beacon_prot &&
4052 wpa_supplicant_install_bigtk(sm, bigtk, 1) < 0)
4053 return -1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004054 } else {
4055 wpa_printf(MSG_DEBUG, "Unknown element id");
4056 return -1;
4057 }
4058
4059 return 0;
4060}
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004061#endif /* CONFIG_WNM */
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004062
4063
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004064#ifdef CONFIG_P2P
4065
4066int wpa_sm_get_p2p_ip_addr(struct wpa_sm *sm, u8 *buf)
4067{
4068 if (sm == NULL || WPA_GET_BE32(sm->p2p_ip_addr) == 0)
4069 return -1;
4070 os_memcpy(buf, sm->p2p_ip_addr, 3 * 4);
4071 return 0;
4072}
4073
4074#endif /* CONFIG_P2P */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004075
4076
4077void wpa_sm_set_rx_replay_ctr(struct wpa_sm *sm, const u8 *rx_replay_counter)
4078{
4079 if (rx_replay_counter == NULL)
4080 return;
4081
4082 os_memcpy(sm->rx_replay_counter, rx_replay_counter,
4083 WPA_REPLAY_COUNTER_LEN);
4084 sm->rx_replay_counter_set = 1;
4085 wpa_printf(MSG_DEBUG, "Updated key replay counter");
4086}
4087
4088
Dmitry Shmidt807291d2015-01-27 13:40:23 -08004089void wpa_sm_set_ptk_kck_kek(struct wpa_sm *sm,
4090 const u8 *ptk_kck, size_t ptk_kck_len,
4091 const u8 *ptk_kek, size_t ptk_kek_len)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004092{
Dmitry Shmidt807291d2015-01-27 13:40:23 -08004093 if (ptk_kck && ptk_kck_len <= WPA_KCK_MAX_LEN) {
4094 os_memcpy(sm->ptk.kck, ptk_kck, ptk_kck_len);
4095 sm->ptk.kck_len = ptk_kck_len;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004096 wpa_printf(MSG_DEBUG, "Updated PTK KCK");
4097 }
Dmitry Shmidt807291d2015-01-27 13:40:23 -08004098 if (ptk_kek && ptk_kek_len <= WPA_KEK_MAX_LEN) {
4099 os_memcpy(sm->ptk.kek, ptk_kek, ptk_kek_len);
4100 sm->ptk.kek_len = ptk_kek_len;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004101 wpa_printf(MSG_DEBUG, "Updated PTK KEK");
4102 }
4103 sm->ptk_set = 1;
4104}
Dmitry Shmidt55840ad2015-12-14 12:45:46 -08004105
4106
4107#ifdef CONFIG_TESTING_OPTIONS
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004108
Dmitry Shmidt55840ad2015-12-14 12:45:46 -08004109void wpa_sm_set_test_assoc_ie(struct wpa_sm *sm, struct wpabuf *buf)
4110{
4111 wpabuf_free(sm->test_assoc_ie);
4112 sm->test_assoc_ie = buf;
4113}
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004114
4115
4116const u8 * wpa_sm_get_anonce(struct wpa_sm *sm)
4117{
4118 return sm->anonce;
4119}
4120
Dmitry Shmidt55840ad2015-12-14 12:45:46 -08004121#endif /* CONFIG_TESTING_OPTIONS */
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004122
4123
Roshan Pius3a1667e2018-07-03 15:17:14 -07004124unsigned int wpa_sm_get_key_mgmt(struct wpa_sm *sm)
4125{
4126 return sm->key_mgmt;
4127}
4128
4129
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004130#ifdef CONFIG_FILS
4131
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004132struct wpabuf * fils_build_auth(struct wpa_sm *sm, int dh_group, const u8 *md)
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004133{
4134 struct wpabuf *buf = NULL;
4135 struct wpabuf *erp_msg;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004136 struct wpabuf *pub = NULL;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004137
4138 erp_msg = eapol_sm_build_erp_reauth_start(sm->eapol);
4139 if (!erp_msg && !sm->cur_pmksa) {
4140 wpa_printf(MSG_DEBUG,
4141 "FILS: Neither ERP EAP-Initiate/Re-auth nor PMKSA cache entry is available - skip FILS");
4142 goto fail;
4143 }
4144
4145 wpa_printf(MSG_DEBUG, "FILS: Try to use FILS (erp=%d pmksa_cache=%d)",
4146 erp_msg != NULL, sm->cur_pmksa != NULL);
4147
4148 sm->fils_completed = 0;
4149
4150 if (!sm->assoc_wpa_ie) {
4151 wpa_printf(MSG_INFO, "FILS: No own RSN IE set for FILS");
4152 goto fail;
4153 }
4154
4155 if (random_get_bytes(sm->fils_nonce, FILS_NONCE_LEN) < 0 ||
4156 random_get_bytes(sm->fils_session, FILS_SESSION_LEN) < 0)
4157 goto fail;
4158
4159 wpa_hexdump(MSG_DEBUG, "FILS: Generated FILS Nonce",
4160 sm->fils_nonce, FILS_NONCE_LEN);
4161 wpa_hexdump(MSG_DEBUG, "FILS: Generated FILS Session",
4162 sm->fils_session, FILS_SESSION_LEN);
4163
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004164#ifdef CONFIG_FILS_SK_PFS
4165 sm->fils_dh_group = dh_group;
4166 if (dh_group) {
4167 crypto_ecdh_deinit(sm->fils_ecdh);
4168 sm->fils_ecdh = crypto_ecdh_init(dh_group);
4169 if (!sm->fils_ecdh) {
4170 wpa_printf(MSG_INFO,
4171 "FILS: Could not initialize ECDH with group %d",
4172 dh_group);
4173 goto fail;
4174 }
4175 pub = crypto_ecdh_get_pubkey(sm->fils_ecdh, 1);
4176 if (!pub)
4177 goto fail;
4178 wpa_hexdump_buf(MSG_DEBUG, "FILS: Element (DH public key)",
4179 pub);
4180 sm->fils_dh_elem_len = wpabuf_len(pub);
4181 }
4182#endif /* CONFIG_FILS_SK_PFS */
4183
4184 buf = wpabuf_alloc(1000 + sm->assoc_wpa_ie_len +
4185 (pub ? wpabuf_len(pub) : 0));
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004186 if (!buf)
4187 goto fail;
4188
4189 /* Fields following the Authentication algorithm number field */
4190
4191 /* Authentication Transaction seq# */
4192 wpabuf_put_le16(buf, 1);
4193
4194 /* Status Code */
4195 wpabuf_put_le16(buf, WLAN_STATUS_SUCCESS);
4196
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004197 /* TODO: FILS PK */
4198#ifdef CONFIG_FILS_SK_PFS
4199 if (dh_group) {
4200 /* Finite Cyclic Group */
4201 wpabuf_put_le16(buf, dh_group);
4202 /* Element */
4203 wpabuf_put_buf(buf, pub);
4204 }
4205#endif /* CONFIG_FILS_SK_PFS */
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004206
4207 /* RSNE */
4208 wpa_hexdump(MSG_DEBUG, "FILS: RSNE in FILS Authentication frame",
4209 sm->assoc_wpa_ie, sm->assoc_wpa_ie_len);
4210 wpabuf_put_data(buf, sm->assoc_wpa_ie, sm->assoc_wpa_ie_len);
4211
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004212 if (md) {
4213 /* MDE when using FILS for FT initial association */
4214 struct rsn_mdie *mdie;
4215
4216 wpabuf_put_u8(buf, WLAN_EID_MOBILITY_DOMAIN);
4217 wpabuf_put_u8(buf, sizeof(*mdie));
4218 mdie = wpabuf_put(buf, sizeof(*mdie));
4219 os_memcpy(mdie->mobility_domain, md, MOBILITY_DOMAIN_ID_LEN);
4220 mdie->ft_capab = 0;
4221 }
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004222
4223 /* FILS Nonce */
4224 wpabuf_put_u8(buf, WLAN_EID_EXTENSION); /* Element ID */
4225 wpabuf_put_u8(buf, 1 + FILS_NONCE_LEN); /* Length */
4226 /* Element ID Extension */
4227 wpabuf_put_u8(buf, WLAN_EID_EXT_FILS_NONCE);
4228 wpabuf_put_data(buf, sm->fils_nonce, FILS_NONCE_LEN);
4229
4230 /* FILS Session */
4231 wpabuf_put_u8(buf, WLAN_EID_EXTENSION); /* Element ID */
4232 wpabuf_put_u8(buf, 1 + FILS_SESSION_LEN); /* Length */
4233 /* Element ID Extension */
4234 wpabuf_put_u8(buf, WLAN_EID_EXT_FILS_SESSION);
4235 wpabuf_put_data(buf, sm->fils_session, FILS_SESSION_LEN);
4236
Hai Shalomfdcde762020-04-02 11:19:20 -07004237 /* Wrapped Data */
Paul Stewart092955c2017-02-06 09:13:09 -08004238 sm->fils_erp_pmkid_set = 0;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004239 if (erp_msg) {
4240 wpabuf_put_u8(buf, WLAN_EID_EXTENSION); /* Element ID */
4241 wpabuf_put_u8(buf, 1 + wpabuf_len(erp_msg)); /* Length */
4242 /* Element ID Extension */
Hai Shalomfdcde762020-04-02 11:19:20 -07004243 wpabuf_put_u8(buf, WLAN_EID_EXT_WRAPPED_DATA);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004244 wpabuf_put_buf(buf, erp_msg);
Paul Stewart092955c2017-02-06 09:13:09 -08004245 /* Calculate pending PMKID here so that we do not need to
4246 * maintain a copy of the EAP-Initiate/Reauth message. */
4247 if (fils_pmkid_erp(sm->key_mgmt, wpabuf_head(erp_msg),
4248 wpabuf_len(erp_msg),
4249 sm->fils_erp_pmkid) == 0)
4250 sm->fils_erp_pmkid_set = 1;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004251 }
4252
4253 wpa_hexdump_buf(MSG_DEBUG, "RSN: FILS fields for Authentication frame",
4254 buf);
4255
4256fail:
4257 wpabuf_free(erp_msg);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004258 wpabuf_free(pub);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004259 return buf;
4260}
4261
4262
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004263int fils_process_auth(struct wpa_sm *sm, const u8 *bssid, const u8 *data,
4264 size_t len)
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004265{
4266 const u8 *pos, *end;
4267 struct ieee802_11_elems elems;
4268 struct wpa_ie_data rsn;
4269 int pmkid_match = 0;
4270 u8 ick[FILS_ICK_MAX_LEN];
4271 size_t ick_len;
4272 int res;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004273 struct wpabuf *dh_ss = NULL;
4274 const u8 *g_sta = NULL;
4275 size_t g_sta_len = 0;
4276 const u8 *g_ap = NULL;
Hai Shalom60840252021-02-19 19:02:11 -08004277 size_t g_ap_len = 0, kdk_len;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004278 struct wpabuf *pub = NULL;
4279
4280 os_memcpy(sm->bssid, bssid, ETH_ALEN);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004281
4282 wpa_hexdump(MSG_DEBUG, "FILS: Authentication frame fields",
4283 data, len);
4284 pos = data;
4285 end = data + len;
4286
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004287 /* TODO: FILS PK */
4288#ifdef CONFIG_FILS_SK_PFS
4289 if (sm->fils_dh_group) {
4290 u16 group;
4291
4292 /* Using FILS PFS */
4293
4294 /* Finite Cyclic Group */
4295 if (end - pos < 2) {
4296 wpa_printf(MSG_DEBUG,
4297 "FILS: No room for Finite Cyclic Group");
4298 goto fail;
4299 }
4300 group = WPA_GET_LE16(pos);
4301 pos += 2;
4302 if (group != sm->fils_dh_group) {
4303 wpa_printf(MSG_DEBUG,
4304 "FILS: Unexpected change in Finite Cyclic Group: %u (expected %u)",
4305 group, sm->fils_dh_group);
4306 goto fail;
4307 }
4308
4309 /* Element */
4310 if ((size_t) (end - pos) < sm->fils_dh_elem_len) {
4311 wpa_printf(MSG_DEBUG, "FILS: No room for Element");
4312 goto fail;
4313 }
4314
4315 if (!sm->fils_ecdh) {
4316 wpa_printf(MSG_DEBUG, "FILS: No ECDH state available");
4317 goto fail;
4318 }
4319 dh_ss = crypto_ecdh_set_peerkey(sm->fils_ecdh, 1, pos,
4320 sm->fils_dh_elem_len);
4321 if (!dh_ss) {
4322 wpa_printf(MSG_DEBUG, "FILS: ECDH operation failed");
4323 goto fail;
4324 }
4325 wpa_hexdump_buf_key(MSG_DEBUG, "FILS: DH_SS", dh_ss);
4326 g_ap = pos;
4327 g_ap_len = sm->fils_dh_elem_len;
4328 pos += sm->fils_dh_elem_len;
4329 }
4330#endif /* CONFIG_FILS_SK_PFS */
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004331
4332 wpa_hexdump(MSG_DEBUG, "FILS: Remaining IEs", pos, end - pos);
4333 if (ieee802_11_parse_elems(pos, end - pos, &elems, 1) == ParseFailed) {
4334 wpa_printf(MSG_DEBUG, "FILS: Could not parse elements");
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004335 goto fail;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004336 }
4337
4338 /* RSNE */
4339 wpa_hexdump(MSG_DEBUG, "FILS: RSN element", elems.rsn_ie,
4340 elems.rsn_ie_len);
4341 if (!elems.rsn_ie ||
4342 wpa_parse_wpa_ie_rsn(elems.rsn_ie - 2, elems.rsn_ie_len + 2,
4343 &rsn) < 0) {
4344 wpa_printf(MSG_DEBUG, "FILS: No RSN element");
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004345 goto fail;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004346 }
4347
4348 if (!elems.fils_nonce) {
4349 wpa_printf(MSG_DEBUG, "FILS: No FILS Nonce field");
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004350 goto fail;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004351 }
4352 os_memcpy(sm->fils_anonce, elems.fils_nonce, FILS_NONCE_LEN);
4353 wpa_hexdump(MSG_DEBUG, "FILS: ANonce", sm->fils_anonce, FILS_NONCE_LEN);
4354
Roshan Pius3a1667e2018-07-03 15:17:14 -07004355#ifdef CONFIG_IEEE80211R
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004356 if (wpa_key_mgmt_ft(sm->key_mgmt)) {
4357 struct wpa_ft_ies parse;
4358
4359 if (!elems.mdie || !elems.ftie) {
4360 wpa_printf(MSG_DEBUG, "FILS+FT: No MDE or FTE");
4361 goto fail;
4362 }
4363
Roshan Pius3a1667e2018-07-03 15:17:14 -07004364 if (wpa_ft_parse_ies(pos, end - pos, &parse,
4365 wpa_key_mgmt_sha384(sm->key_mgmt)) < 0) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004366 wpa_printf(MSG_DEBUG, "FILS+FT: Failed to parse IEs");
4367 goto fail;
4368 }
4369
4370 if (!parse.r0kh_id) {
4371 wpa_printf(MSG_DEBUG,
4372 "FILS+FT: No R0KH-ID subelem in FTE");
4373 goto fail;
4374 }
4375 os_memcpy(sm->r0kh_id, parse.r0kh_id, parse.r0kh_id_len);
4376 sm->r0kh_id_len = parse.r0kh_id_len;
4377 wpa_hexdump_ascii(MSG_DEBUG, "FILS+FT: R0KH-ID",
4378 sm->r0kh_id, sm->r0kh_id_len);
4379
4380 if (!parse.r1kh_id) {
4381 wpa_printf(MSG_DEBUG,
4382 "FILS+FT: No R1KH-ID subelem in FTE");
4383 goto fail;
4384 }
4385 os_memcpy(sm->r1kh_id, parse.r1kh_id, FT_R1KH_ID_LEN);
4386 wpa_hexdump(MSG_DEBUG, "FILS+FT: R1KH-ID",
4387 sm->r1kh_id, FT_R1KH_ID_LEN);
4388
4389 /* TODO: Check MDE and FTE payload */
4390
4391 wpabuf_free(sm->fils_ft_ies);
4392 sm->fils_ft_ies = wpabuf_alloc(2 + elems.mdie_len +
4393 2 + elems.ftie_len);
4394 if (!sm->fils_ft_ies)
4395 goto fail;
4396 wpabuf_put_data(sm->fils_ft_ies, elems.mdie - 2,
4397 2 + elems.mdie_len);
4398 wpabuf_put_data(sm->fils_ft_ies, elems.ftie - 2,
4399 2 + elems.ftie_len);
4400 } else {
4401 wpabuf_free(sm->fils_ft_ies);
4402 sm->fils_ft_ies = NULL;
4403 }
Roshan Pius3a1667e2018-07-03 15:17:14 -07004404#endif /* CONFIG_IEEE80211R */
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004405
4406 /* PMKID List */
4407 if (rsn.pmkid && rsn.num_pmkid > 0) {
4408 wpa_hexdump(MSG_DEBUG, "FILS: PMKID List",
4409 rsn.pmkid, rsn.num_pmkid * PMKID_LEN);
4410
4411 if (rsn.num_pmkid != 1) {
4412 wpa_printf(MSG_DEBUG, "FILS: Invalid PMKID selection");
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004413 goto fail;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004414 }
4415 wpa_hexdump(MSG_DEBUG, "FILS: PMKID", rsn.pmkid, PMKID_LEN);
4416 if (os_memcmp(sm->cur_pmksa->pmkid, rsn.pmkid, PMKID_LEN) != 0)
4417 {
4418 wpa_printf(MSG_DEBUG, "FILS: PMKID mismatch");
4419 wpa_hexdump(MSG_DEBUG, "FILS: Expected PMKID",
4420 sm->cur_pmksa->pmkid, PMKID_LEN);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004421 goto fail;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004422 }
4423 wpa_printf(MSG_DEBUG,
4424 "FILS: Matching PMKID - continue using PMKSA caching");
4425 pmkid_match = 1;
4426 }
4427 if (!pmkid_match && sm->cur_pmksa) {
4428 wpa_printf(MSG_DEBUG,
4429 "FILS: No PMKID match - cannot use cached PMKSA entry");
4430 sm->cur_pmksa = NULL;
4431 }
4432
4433 /* FILS Session */
4434 if (!elems.fils_session) {
4435 wpa_printf(MSG_DEBUG, "FILS: No FILS Session element");
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004436 goto fail;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004437 }
4438 wpa_hexdump(MSG_DEBUG, "FILS: FILS Session", elems.fils_session,
4439 FILS_SESSION_LEN);
4440 if (os_memcmp(sm->fils_session, elems.fils_session, FILS_SESSION_LEN)
4441 != 0) {
4442 wpa_printf(MSG_DEBUG, "FILS: Session mismatch");
4443 wpa_hexdump(MSG_DEBUG, "FILS: Expected FILS Session",
4444 sm->fils_session, FILS_SESSION_LEN);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004445 goto fail;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004446 }
4447
Hai Shalomfdcde762020-04-02 11:19:20 -07004448 /* Wrapped Data */
4449 if (!sm->cur_pmksa && elems.wrapped_data) {
Paul Stewart092955c2017-02-06 09:13:09 -08004450 u8 rmsk[ERP_MAX_KEY_LEN];
4451 size_t rmsk_len;
4452
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004453 wpa_hexdump(MSG_DEBUG, "FILS: Wrapped Data",
Hai Shalomfdcde762020-04-02 11:19:20 -07004454 elems.wrapped_data,
4455 elems.wrapped_data_len);
4456 eapol_sm_process_erp_finish(sm->eapol, elems.wrapped_data,
4457 elems.wrapped_data_len);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004458 if (eapol_sm_failed(sm->eapol))
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004459 goto fail;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004460
Paul Stewart092955c2017-02-06 09:13:09 -08004461 rmsk_len = ERP_MAX_KEY_LEN;
4462 res = eapol_sm_get_key(sm->eapol, rmsk, rmsk_len);
4463 if (res == PMK_LEN) {
4464 rmsk_len = PMK_LEN;
4465 res = eapol_sm_get_key(sm->eapol, rmsk, rmsk_len);
4466 }
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004467 if (res)
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004468 goto fail;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004469
Paul Stewart092955c2017-02-06 09:13:09 -08004470 res = fils_rmsk_to_pmk(sm->key_mgmt, rmsk, rmsk_len,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004471 sm->fils_nonce, sm->fils_anonce,
4472 dh_ss ? wpabuf_head(dh_ss) : NULL,
4473 dh_ss ? wpabuf_len(dh_ss) : 0,
Paul Stewart092955c2017-02-06 09:13:09 -08004474 sm->pmk, &sm->pmk_len);
Hai Shalom81f62d82019-07-22 12:10:00 -07004475 forced_memzero(rmsk, sizeof(rmsk));
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004476
4477 /* Don't use DHss in PTK derivation if PMKSA caching is not
4478 * used. */
4479 wpabuf_clear_free(dh_ss);
4480 dh_ss = NULL;
4481
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08004482 if (res)
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004483 goto fail;
Paul Stewart092955c2017-02-06 09:13:09 -08004484
4485 if (!sm->fils_erp_pmkid_set) {
4486 wpa_printf(MSG_DEBUG, "FILS: PMKID not available");
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004487 goto fail;
Paul Stewart092955c2017-02-06 09:13:09 -08004488 }
4489 wpa_hexdump(MSG_DEBUG, "FILS: PMKID", sm->fils_erp_pmkid,
4490 PMKID_LEN);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004491 wpa_printf(MSG_DEBUG, "FILS: ERP processing succeeded - add PMKSA cache entry for the result");
Paul Stewart092955c2017-02-06 09:13:09 -08004492 sm->cur_pmksa = pmksa_cache_add(sm->pmksa, sm->pmk, sm->pmk_len,
4493 sm->fils_erp_pmkid, NULL, 0,
4494 sm->bssid, sm->own_addr,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004495 sm->network_ctx, sm->key_mgmt,
4496 NULL);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004497 }
4498
4499 if (!sm->cur_pmksa) {
4500 wpa_printf(MSG_DEBUG,
4501 "FILS: No remaining options to continue FILS authentication");
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004502 goto fail;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004503 }
4504
Hai Shalom60840252021-02-19 19:02:11 -08004505 if (sm->force_kdk_derivation ||
Hai Shalomc1a21442022-02-04 13:43:00 -08004506 (sm->secure_ltf &&
4507 ieee802_11_rsnx_capab(sm->ap_rsnxe, WLAN_RSNX_CAPAB_SECURE_LTF)))
Hai Shalom60840252021-02-19 19:02:11 -08004508 kdk_len = WPA_KDK_MAX_LEN;
4509 else
4510 kdk_len = 0;
4511
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004512 if (fils_pmk_to_ptk(sm->pmk, sm->pmk_len, sm->own_addr, sm->bssid,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004513 sm->fils_nonce, sm->fils_anonce,
4514 dh_ss ? wpabuf_head(dh_ss) : NULL,
4515 dh_ss ? wpabuf_len(dh_ss) : 0,
4516 &sm->ptk, ick, &ick_len,
4517 sm->key_mgmt, sm->pairwise_cipher,
Hai Shalom60840252021-02-19 19:02:11 -08004518 sm->fils_ft, &sm->fils_ft_len,
4519 kdk_len) < 0) {
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004520 wpa_printf(MSG_DEBUG, "FILS: Failed to derive PTK");
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004521 goto fail;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004522 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004523
Sunil Ravi89eba102022-09-13 21:04:37 -07004524#ifdef CONFIG_PASN
4525 if (sm->secure_ltf &&
4526 ieee802_11_rsnx_capab(sm->ap_rsnxe, WLAN_RSNX_CAPAB_SECURE_LTF) &&
4527 wpa_ltf_keyseed(&sm->ptk, sm->key_mgmt, sm->pairwise_cipher)) {
4528 wpa_printf(MSG_DEBUG, "FILS: Failed to derive LTF keyseed");
4529 goto fail;
4530 }
4531#endif /* CONFIG_PASN */
4532
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004533 wpabuf_clear_free(dh_ss);
4534 dh_ss = NULL;
4535
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004536 sm->ptk_set = 1;
4537 sm->tptk_set = 0;
4538 os_memset(&sm->tptk, 0, sizeof(sm->tptk));
4539
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004540#ifdef CONFIG_FILS_SK_PFS
4541 if (sm->fils_dh_group) {
4542 if (!sm->fils_ecdh) {
4543 wpa_printf(MSG_INFO, "FILS: ECDH not initialized");
4544 goto fail;
4545 }
4546 pub = crypto_ecdh_get_pubkey(sm->fils_ecdh, 1);
4547 if (!pub)
4548 goto fail;
4549 wpa_hexdump_buf(MSG_DEBUG, "FILS: gSTA", pub);
4550 g_sta = wpabuf_head(pub);
4551 g_sta_len = wpabuf_len(pub);
4552 if (!g_ap) {
4553 wpa_printf(MSG_INFO, "FILS: gAP not available");
4554 goto fail;
4555 }
4556 wpa_hexdump(MSG_DEBUG, "FILS: gAP", g_ap, g_ap_len);
4557 }
4558#endif /* CONFIG_FILS_SK_PFS */
4559
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004560 res = fils_key_auth_sk(ick, ick_len, sm->fils_nonce,
4561 sm->fils_anonce, sm->own_addr, sm->bssid,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004562 g_sta, g_sta_len, g_ap, g_ap_len,
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004563 sm->key_mgmt, sm->fils_key_auth_sta,
4564 sm->fils_key_auth_ap,
4565 &sm->fils_key_auth_len);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004566 wpabuf_free(pub);
Hai Shalom81f62d82019-07-22 12:10:00 -07004567 forced_memzero(ick, sizeof(ick));
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004568 return res;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004569fail:
4570 wpabuf_free(pub);
4571 wpabuf_clear_free(dh_ss);
4572 return -1;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004573}
4574
4575
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004576#ifdef CONFIG_IEEE80211R
4577static int fils_ft_build_assoc_req_rsne(struct wpa_sm *sm, struct wpabuf *buf)
4578{
4579 struct rsn_ie_hdr *rsnie;
4580 u16 capab;
4581 u8 *pos;
Roshan Pius3a1667e2018-07-03 15:17:14 -07004582 int use_sha384 = wpa_key_mgmt_sha384(sm->key_mgmt);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004583
4584 /* RSNIE[PMKR0Name/PMKR1Name] */
4585 rsnie = wpabuf_put(buf, sizeof(*rsnie));
4586 rsnie->elem_id = WLAN_EID_RSN;
4587 WPA_PUT_LE16(rsnie->version, RSN_VERSION);
4588
4589 /* Group Suite Selector */
4590 if (!wpa_cipher_valid_group(sm->group_cipher)) {
4591 wpa_printf(MSG_WARNING, "FT: Invalid group cipher (%d)",
4592 sm->group_cipher);
4593 return -1;
4594 }
4595 pos = wpabuf_put(buf, RSN_SELECTOR_LEN);
4596 RSN_SELECTOR_PUT(pos, wpa_cipher_to_suite(WPA_PROTO_RSN,
4597 sm->group_cipher));
4598
4599 /* Pairwise Suite Count */
4600 wpabuf_put_le16(buf, 1);
4601
4602 /* Pairwise Suite List */
4603 if (!wpa_cipher_valid_pairwise(sm->pairwise_cipher)) {
4604 wpa_printf(MSG_WARNING, "FT: Invalid pairwise cipher (%d)",
4605 sm->pairwise_cipher);
4606 return -1;
4607 }
4608 pos = wpabuf_put(buf, RSN_SELECTOR_LEN);
4609 RSN_SELECTOR_PUT(pos, wpa_cipher_to_suite(WPA_PROTO_RSN,
4610 sm->pairwise_cipher));
4611
4612 /* Authenticated Key Management Suite Count */
4613 wpabuf_put_le16(buf, 1);
4614
4615 /* Authenticated Key Management Suite List */
4616 pos = wpabuf_put(buf, RSN_SELECTOR_LEN);
4617 if (sm->key_mgmt == WPA_KEY_MGMT_FT_FILS_SHA256)
4618 RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_FT_FILS_SHA256);
4619 else if (sm->key_mgmt == WPA_KEY_MGMT_FT_FILS_SHA384)
4620 RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_FT_FILS_SHA384);
4621 else {
4622 wpa_printf(MSG_WARNING,
4623 "FILS+FT: Invalid key management type (%d)",
4624 sm->key_mgmt);
4625 return -1;
4626 }
4627
4628 /* RSN Capabilities */
4629 capab = 0;
Hai Shalomc3565922019-10-28 11:58:20 -07004630 if (sm->mfp)
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004631 capab |= WPA_CAPABILITY_MFPC;
Hai Shalomc3565922019-10-28 11:58:20 -07004632 if (sm->mfp == 2)
4633 capab |= WPA_CAPABILITY_MFPR;
Hai Shalom74f70d42019-02-11 14:42:39 -08004634 if (sm->ocv)
4635 capab |= WPA_CAPABILITY_OCVC;
Hai Shalomfdcde762020-04-02 11:19:20 -07004636 if (sm->ext_key_id)
4637 capab |= WPA_CAPABILITY_EXT_KEY_ID_FOR_UNICAST;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004638 wpabuf_put_le16(buf, capab);
4639
4640 /* PMKID Count */
4641 wpabuf_put_le16(buf, 1);
4642
4643 /* PMKID List [PMKR1Name] */
4644 wpa_hexdump_key(MSG_DEBUG, "FILS+FT: XXKey (FILS-FT)",
4645 sm->fils_ft, sm->fils_ft_len);
4646 wpa_hexdump_ascii(MSG_DEBUG, "FILS+FT: SSID", sm->ssid, sm->ssid_len);
4647 wpa_hexdump(MSG_DEBUG, "FILS+FT: MDID",
4648 sm->mobility_domain, MOBILITY_DOMAIN_ID_LEN);
4649 wpa_hexdump_ascii(MSG_DEBUG, "FILS+FT: R0KH-ID",
4650 sm->r0kh_id, sm->r0kh_id_len);
4651 if (wpa_derive_pmk_r0(sm->fils_ft, sm->fils_ft_len, sm->ssid,
4652 sm->ssid_len, sm->mobility_domain,
4653 sm->r0kh_id, sm->r0kh_id_len, sm->own_addr,
Roshan Pius3a1667e2018-07-03 15:17:14 -07004654 sm->pmk_r0, sm->pmk_r0_name, use_sha384) < 0) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004655 wpa_printf(MSG_WARNING, "FILS+FT: Could not derive PMK-R0");
4656 return -1;
4657 }
Roshan Pius3a1667e2018-07-03 15:17:14 -07004658 sm->pmk_r0_len = use_sha384 ? SHA384_MAC_LEN : PMK_LEN;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004659 wpa_printf(MSG_DEBUG, "FILS+FT: R1KH-ID: " MACSTR,
4660 MAC2STR(sm->r1kh_id));
4661 pos = wpabuf_put(buf, WPA_PMK_NAME_LEN);
4662 if (wpa_derive_pmk_r1_name(sm->pmk_r0_name, sm->r1kh_id, sm->own_addr,
Hai Shalom021b0b52019-04-10 11:17:58 -07004663 sm->pmk_r1_name, use_sha384) < 0) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004664 wpa_printf(MSG_WARNING, "FILS+FT: Could not derive PMKR1Name");
4665 return -1;
4666 }
Hai Shalom021b0b52019-04-10 11:17:58 -07004667 os_memcpy(pos, sm->pmk_r1_name, WPA_PMK_NAME_LEN);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004668
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004669 if (sm->mgmt_group_cipher == WPA_CIPHER_AES_128_CMAC) {
4670 /* Management Group Cipher Suite */
4671 pos = wpabuf_put(buf, RSN_SELECTOR_LEN);
4672 RSN_SELECTOR_PUT(pos, RSN_CIPHER_SUITE_AES_128_CMAC);
4673 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004674
4675 rsnie->len = ((u8 *) wpabuf_put(buf, 0) - (u8 *) rsnie) - 2;
4676 return 0;
4677}
4678#endif /* CONFIG_IEEE80211R */
4679
4680
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004681struct wpabuf * fils_build_assoc_req(struct wpa_sm *sm, const u8 **kek,
4682 size_t *kek_len, const u8 **snonce,
Paul Stewart092955c2017-02-06 09:13:09 -08004683 const u8 **anonce,
4684 const struct wpabuf **hlp,
4685 unsigned int num_hlp)
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004686{
4687 struct wpabuf *buf;
Paul Stewart092955c2017-02-06 09:13:09 -08004688 size_t len;
4689 unsigned int i;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004690
Paul Stewart092955c2017-02-06 09:13:09 -08004691 len = 1000;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004692#ifdef CONFIG_IEEE80211R
4693 if (sm->fils_ft_ies)
4694 len += wpabuf_len(sm->fils_ft_ies);
4695 if (wpa_key_mgmt_ft(sm->key_mgmt))
4696 len += 256;
4697#endif /* CONFIG_IEEE80211R */
Paul Stewart092955c2017-02-06 09:13:09 -08004698 for (i = 0; hlp && i < num_hlp; i++)
4699 len += 10 + wpabuf_len(hlp[i]);
4700 buf = wpabuf_alloc(len);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004701 if (!buf)
4702 return NULL;
4703
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004704#ifdef CONFIG_IEEE80211R
4705 if (wpa_key_mgmt_ft(sm->key_mgmt) && sm->fils_ft_ies) {
4706 /* MDE and FTE when using FILS+FT */
4707 wpabuf_put_buf(buf, sm->fils_ft_ies);
4708 /* RSNE with PMKR1Name in PMKID field */
4709 if (fils_ft_build_assoc_req_rsne(sm, buf) < 0) {
4710 wpabuf_free(buf);
4711 return NULL;
4712 }
4713 }
4714#endif /* CONFIG_IEEE80211R */
4715
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004716 /* FILS Session */
4717 wpabuf_put_u8(buf, WLAN_EID_EXTENSION); /* Element ID */
4718 wpabuf_put_u8(buf, 1 + FILS_SESSION_LEN); /* Length */
4719 /* Element ID Extension */
4720 wpabuf_put_u8(buf, WLAN_EID_EXT_FILS_SESSION);
4721 wpabuf_put_data(buf, sm->fils_session, FILS_SESSION_LEN);
4722
4723 /* Everything after FILS Session element gets encrypted in the driver
4724 * with KEK. The buffer returned from here is the plaintext version. */
4725
4726 /* TODO: FILS Public Key */
4727
4728 /* FILS Key Confirm */
4729 wpabuf_put_u8(buf, WLAN_EID_EXTENSION); /* Element ID */
4730 wpabuf_put_u8(buf, 1 + sm->fils_key_auth_len); /* Length */
4731 /* Element ID Extension */
4732 wpabuf_put_u8(buf, WLAN_EID_EXT_FILS_KEY_CONFIRM);
4733 wpabuf_put_data(buf, sm->fils_key_auth_sta, sm->fils_key_auth_len);
4734
Paul Stewart092955c2017-02-06 09:13:09 -08004735 /* FILS HLP Container */
4736 for (i = 0; hlp && i < num_hlp; i++) {
4737 const u8 *pos = wpabuf_head(hlp[i]);
4738 size_t left = wpabuf_len(hlp[i]);
4739
4740 wpabuf_put_u8(buf, WLAN_EID_EXTENSION); /* Element ID */
4741 if (left <= 254)
4742 len = 1 + left;
4743 else
4744 len = 255;
4745 wpabuf_put_u8(buf, len); /* Length */
4746 /* Element ID Extension */
4747 wpabuf_put_u8(buf, WLAN_EID_EXT_FILS_HLP_CONTAINER);
4748 /* Destination MAC Address, Source MAC Address, HLP Packet.
4749 * HLP Packet is in MSDU format (i.e., included the LLC/SNAP
4750 * header when LPD is used). */
4751 wpabuf_put_data(buf, pos, len - 1);
4752 pos += len - 1;
4753 left -= len - 1;
4754 while (left) {
4755 wpabuf_put_u8(buf, WLAN_EID_FRAGMENT);
4756 len = left > 255 ? 255 : left;
4757 wpabuf_put_u8(buf, len);
4758 wpabuf_put_data(buf, pos, len);
4759 pos += len;
4760 left -= len;
4761 }
4762 }
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004763
4764 /* TODO: FILS IP Address Assignment */
4765
Hai Shalom74f70d42019-02-11 14:42:39 -08004766#ifdef CONFIG_OCV
4767 if (wpa_sm_ocv_enabled(sm)) {
4768 struct wpa_channel_info ci;
4769 u8 *pos;
4770
4771 if (wpa_sm_channel_info(sm, &ci) != 0) {
4772 wpa_printf(MSG_WARNING,
4773 "FILS: Failed to get channel info for OCI element");
4774 wpabuf_free(buf);
4775 return NULL;
4776 }
Hai Shalom899fcc72020-10-19 14:38:18 -07004777#ifdef CONFIG_TESTING_OPTIONS
4778 if (sm->oci_freq_override_fils_assoc) {
4779 wpa_printf(MSG_INFO,
4780 "TEST: Override OCI KDE frequency %d -> %d MHz",
4781 ci.frequency,
4782 sm->oci_freq_override_fils_assoc);
4783 ci.frequency = sm->oci_freq_override_fils_assoc;
4784 }
4785#endif /* CONFIG_TESTING_OPTIONS */
Hai Shalom74f70d42019-02-11 14:42:39 -08004786
4787 pos = wpabuf_put(buf, OCV_OCI_EXTENDED_LEN);
4788 if (ocv_insert_extended_oci(&ci, pos) < 0) {
4789 wpabuf_free(buf);
4790 return NULL;
4791 }
4792 }
4793#endif /* CONFIG_OCV */
4794
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004795 wpa_hexdump_buf(MSG_DEBUG, "FILS: Association Request plaintext", buf);
4796
4797 *kek = sm->ptk.kek;
4798 *kek_len = sm->ptk.kek_len;
4799 wpa_hexdump_key(MSG_DEBUG, "FILS: KEK for AEAD", *kek, *kek_len);
4800 *snonce = sm->fils_nonce;
4801 wpa_hexdump(MSG_DEBUG, "FILS: SNonce for AEAD AAD",
4802 *snonce, FILS_NONCE_LEN);
4803 *anonce = sm->fils_anonce;
4804 wpa_hexdump(MSG_DEBUG, "FILS: ANonce for AEAD AAD",
4805 *anonce, FILS_NONCE_LEN);
4806
4807 return buf;
4808}
4809
4810
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08004811static void fils_process_hlp_resp(struct wpa_sm *sm, const u8 *resp, size_t len)
4812{
4813 const u8 *pos, *end;
4814
4815 wpa_hexdump(MSG_MSGDUMP, "FILS: HLP response", resp, len);
4816 if (len < 2 * ETH_ALEN)
4817 return;
4818 pos = resp + 2 * ETH_ALEN;
4819 end = resp + len;
4820 if (end - pos >= 6 &&
4821 os_memcmp(pos, "\xaa\xaa\x03\x00\x00\x00", 6) == 0)
4822 pos += 6; /* Remove SNAP/LLC header */
4823 wpa_sm_fils_hlp_rx(sm, resp, resp + ETH_ALEN, pos, end - pos);
4824}
4825
4826
4827static void fils_process_hlp_container(struct wpa_sm *sm, const u8 *pos,
4828 size_t len)
4829{
4830 const u8 *end = pos + len;
4831 u8 *tmp, *tmp_pos;
4832
4833 /* Check if there are any FILS HLP Container elements */
4834 while (end - pos >= 2) {
4835 if (2 + pos[1] > end - pos)
4836 return;
4837 if (pos[0] == WLAN_EID_EXTENSION &&
4838 pos[1] >= 1 + 2 * ETH_ALEN &&
4839 pos[2] == WLAN_EID_EXT_FILS_HLP_CONTAINER)
4840 break;
4841 pos += 2 + pos[1];
4842 }
4843 if (end - pos < 2)
4844 return; /* No FILS HLP Container elements */
4845
4846 tmp = os_malloc(end - pos);
4847 if (!tmp)
4848 return;
4849
4850 while (end - pos >= 2) {
4851 if (2 + pos[1] > end - pos ||
4852 pos[0] != WLAN_EID_EXTENSION ||
4853 pos[1] < 1 + 2 * ETH_ALEN ||
4854 pos[2] != WLAN_EID_EXT_FILS_HLP_CONTAINER)
4855 break;
4856 tmp_pos = tmp;
4857 os_memcpy(tmp_pos, pos + 3, pos[1] - 1);
4858 tmp_pos += pos[1] - 1;
4859 pos += 2 + pos[1];
4860
4861 /* Add possible fragments */
4862 while (end - pos >= 2 && pos[0] == WLAN_EID_FRAGMENT &&
4863 2 + pos[1] <= end - pos) {
4864 os_memcpy(tmp_pos, pos + 2, pos[1]);
4865 tmp_pos += pos[1];
4866 pos += 2 + pos[1];
4867 }
4868
4869 fils_process_hlp_resp(sm, tmp, tmp_pos - tmp);
4870 }
4871
4872 os_free(tmp);
4873}
4874
4875
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004876int fils_process_assoc_resp(struct wpa_sm *sm, const u8 *resp, size_t len)
4877{
4878 const struct ieee80211_mgmt *mgmt;
4879 const u8 *end, *ie_start;
4880 struct ieee802_11_elems elems;
4881 int keylen, rsclen;
4882 enum wpa_alg alg;
4883 struct wpa_gtk_data gd;
4884 int maxkeylen;
4885 struct wpa_eapol_ie_parse kde;
4886
4887 if (!sm || !sm->ptk_set) {
4888 wpa_printf(MSG_DEBUG, "FILS: No KEK available");
4889 return -1;
4890 }
4891
4892 if (!wpa_key_mgmt_fils(sm->key_mgmt)) {
4893 wpa_printf(MSG_DEBUG, "FILS: Not a FILS AKM");
4894 return -1;
4895 }
4896
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004897 if (sm->fils_completed) {
4898 wpa_printf(MSG_DEBUG,
4899 "FILS: Association has already been completed for this FILS authentication - ignore unexpected retransmission");
4900 return -1;
4901 }
4902
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004903 wpa_hexdump(MSG_DEBUG, "FILS: (Re)Association Response frame",
4904 resp, len);
4905
4906 mgmt = (const struct ieee80211_mgmt *) resp;
4907 if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.assoc_resp))
4908 return -1;
4909
4910 end = resp + len;
4911 /* Same offset for Association Response and Reassociation Response */
4912 ie_start = mgmt->u.assoc_resp.variable;
4913
4914 if (ieee802_11_parse_elems(ie_start, end - ie_start, &elems, 1) ==
4915 ParseFailed) {
4916 wpa_printf(MSG_DEBUG,
4917 "FILS: Failed to parse decrypted elements");
4918 goto fail;
4919 }
4920
4921 if (!elems.fils_session) {
4922 wpa_printf(MSG_DEBUG, "FILS: No FILS Session element");
4923 return -1;
4924 }
4925 if (os_memcmp(elems.fils_session, sm->fils_session,
4926 FILS_SESSION_LEN) != 0) {
4927 wpa_printf(MSG_DEBUG, "FILS: FILS Session mismatch");
4928 wpa_hexdump(MSG_DEBUG, "FILS: Received FILS Session",
4929 elems.fils_session, FILS_SESSION_LEN);
4930 wpa_hexdump(MSG_DEBUG, "FILS: Expected FILS Session",
4931 sm->fils_session, FILS_SESSION_LEN);
4932 }
4933
Hai Shalom81f62d82019-07-22 12:10:00 -07004934 if (!elems.rsn_ie) {
4935 wpa_printf(MSG_DEBUG,
4936 "FILS: No RSNE in (Re)Association Response");
4937 /* As an interop workaround, allow this for now since IEEE Std
4938 * 802.11ai-2016 did not include all the needed changes to make
4939 * a FILS AP include RSNE in the frame. This workaround might
4940 * eventually be removed and replaced with rejection (goto fail)
4941 * to follow a strict interpretation of the standard. */
4942 } else if (wpa_compare_rsn_ie(wpa_key_mgmt_ft(sm->key_mgmt),
4943 sm->ap_rsn_ie, sm->ap_rsn_ie_len,
4944 elems.rsn_ie - 2, elems.rsn_ie_len + 2)) {
4945 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
4946 "FILS: RSNE mismatch between Beacon/Probe Response and (Re)Association Response");
4947 wpa_hexdump(MSG_DEBUG, "FILS: RSNE in Beacon/Probe Response",
4948 sm->ap_rsn_ie, sm->ap_rsn_ie_len);
4949 wpa_hexdump(MSG_DEBUG, "FILS: RSNE in (Re)Association Response",
4950 elems.rsn_ie, elems.rsn_ie_len);
4951 goto fail;
4952 }
4953
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004954 /* TODO: FILS Public Key */
4955
4956 if (!elems.fils_key_confirm) {
4957 wpa_printf(MSG_DEBUG, "FILS: No FILS Key Confirm element");
4958 goto fail;
4959 }
4960 if (elems.fils_key_confirm_len != sm->fils_key_auth_len) {
4961 wpa_printf(MSG_DEBUG,
4962 "FILS: Unexpected Key-Auth length %d (expected %d)",
4963 elems.fils_key_confirm_len,
4964 (int) sm->fils_key_auth_len);
4965 goto fail;
4966 }
4967 if (os_memcmp(elems.fils_key_confirm, sm->fils_key_auth_ap,
4968 sm->fils_key_auth_len) != 0) {
4969 wpa_printf(MSG_DEBUG, "FILS: Key-Auth mismatch");
4970 wpa_hexdump(MSG_DEBUG, "FILS: Received Key-Auth",
4971 elems.fils_key_confirm,
4972 elems.fils_key_confirm_len);
4973 wpa_hexdump(MSG_DEBUG, "FILS: Expected Key-Auth",
4974 sm->fils_key_auth_ap, sm->fils_key_auth_len);
4975 goto fail;
4976 }
4977
Hai Shalom74f70d42019-02-11 14:42:39 -08004978#ifdef CONFIG_OCV
4979 if (wpa_sm_ocv_enabled(sm)) {
4980 struct wpa_channel_info ci;
4981
4982 if (wpa_sm_channel_info(sm, &ci) != 0) {
4983 wpa_printf(MSG_WARNING,
4984 "Failed to get channel info to validate received OCI in FILS (Re)Association Response frame");
4985 goto fail;
4986 }
4987
4988 if (ocv_verify_tx_params(elems.oci, elems.oci_len, &ci,
4989 channel_width_to_int(ci.chanwidth),
Hai Shalom899fcc72020-10-19 14:38:18 -07004990 ci.seg1_idx) != OCI_SUCCESS) {
4991 wpa_msg(sm->ctx->msg_ctx, MSG_INFO, OCV_FAILURE
4992 "addr=" MACSTR " frame=fils-assoc error=%s",
4993 MAC2STR(sm->bssid), ocv_errorstr);
Hai Shalom74f70d42019-02-11 14:42:39 -08004994 goto fail;
4995 }
4996 }
4997#endif /* CONFIG_OCV */
4998
Hai Shalom021b0b52019-04-10 11:17:58 -07004999#ifdef CONFIG_IEEE80211R
5000 if (wpa_key_mgmt_ft(sm->key_mgmt) && sm->fils_ft_ies) {
5001 struct wpa_ie_data rsn;
5002
5003 /* Check that PMKR1Name derived by the AP matches */
5004 if (!elems.rsn_ie ||
5005 wpa_parse_wpa_ie_rsn(elems.rsn_ie - 2, elems.rsn_ie_len + 2,
5006 &rsn) < 0 ||
5007 !rsn.pmkid || rsn.num_pmkid != 1 ||
5008 os_memcmp(rsn.pmkid, sm->pmk_r1_name,
5009 WPA_PMK_NAME_LEN) != 0) {
5010 wpa_printf(MSG_DEBUG,
5011 "FILS+FT: No RSNE[PMKR1Name] match in AssocResp");
5012 goto fail;
5013 }
5014 }
5015#endif /* CONFIG_IEEE80211R */
5016
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08005017 /* Key Delivery */
5018 if (!elems.key_delivery) {
5019 wpa_printf(MSG_DEBUG, "FILS: No Key Delivery element");
5020 goto fail;
5021 }
5022
5023 /* Parse GTK and set the key to the driver */
5024 os_memset(&gd, 0, sizeof(gd));
5025 if (wpa_supplicant_parse_ies(elems.key_delivery + WPA_KEY_RSC_LEN,
5026 elems.key_delivery_len - WPA_KEY_RSC_LEN,
5027 &kde) < 0) {
5028 wpa_printf(MSG_DEBUG, "FILS: Failed to parse KDEs");
5029 goto fail;
5030 }
5031 if (!kde.gtk) {
5032 wpa_printf(MSG_DEBUG, "FILS: No GTK KDE");
5033 goto fail;
5034 }
5035 maxkeylen = gd.gtk_len = kde.gtk_len - 2;
5036 if (wpa_supplicant_check_group_cipher(sm, sm->group_cipher,
5037 gd.gtk_len, maxkeylen,
5038 &gd.key_rsc_len, &gd.alg))
5039 goto fail;
5040
5041 wpa_hexdump_key(MSG_DEBUG, "FILS: Received GTK", kde.gtk, kde.gtk_len);
5042 gd.keyidx = kde.gtk[0] & 0x3;
5043 gd.tx = wpa_supplicant_gtk_tx_bit_workaround(sm,
5044 !!(kde.gtk[0] & BIT(2)));
5045 if (kde.gtk_len - 2 > sizeof(gd.gtk)) {
5046 wpa_printf(MSG_DEBUG, "FILS: Too long GTK in GTK KDE (len=%lu)",
5047 (unsigned long) kde.gtk_len - 2);
5048 goto fail;
5049 }
5050 os_memcpy(gd.gtk, kde.gtk + 2, kde.gtk_len - 2);
5051
5052 wpa_printf(MSG_DEBUG, "FILS: Set GTK to driver");
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005053 if (wpa_supplicant_install_gtk(sm, &gd, elems.key_delivery, 0) < 0) {
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08005054 wpa_printf(MSG_DEBUG, "FILS: Failed to set GTK");
5055 goto fail;
5056 }
5057
5058 if (ieee80211w_set_keys(sm, &kde) < 0) {
5059 wpa_printf(MSG_DEBUG, "FILS: Failed to set IGTK");
5060 goto fail;
5061 }
5062
5063 alg = wpa_cipher_to_alg(sm->pairwise_cipher);
5064 keylen = wpa_cipher_key_len(sm->pairwise_cipher);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005065 if (keylen <= 0 || (unsigned int) keylen != sm->ptk.tk_len) {
5066 wpa_printf(MSG_DEBUG, "FILS: TK length mismatch: %u != %lu",
5067 keylen, (long unsigned int) sm->ptk.tk_len);
5068 goto fail;
5069 }
Hai Shalomfdcde762020-04-02 11:19:20 -07005070
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08005071 rsclen = wpa_cipher_rsc_len(sm->pairwise_cipher);
5072 wpa_hexdump_key(MSG_DEBUG, "FILS: Set TK to driver",
5073 sm->ptk.tk, keylen);
5074 if (wpa_sm_set_key(sm, alg, sm->bssid, 0, 1, null_rsc, rsclen,
Hai Shalomfdcde762020-04-02 11:19:20 -07005075 sm->ptk.tk, keylen, KEY_FLAG_PAIRWISE_RX_TX) < 0) {
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08005076 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
5077 "FILS: Failed to set PTK to the driver (alg=%d keylen=%d bssid="
5078 MACSTR ")",
5079 alg, keylen, MAC2STR(sm->bssid));
5080 goto fail;
5081 }
5082
Hai Shalom60840252021-02-19 19:02:11 -08005083 wpa_sm_store_ptk(sm, sm->bssid, sm->pairwise_cipher,
5084 sm->dot11RSNAConfigPMKLifetime, &sm->ptk);
5085
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08005086 /* TODO: TK could be cleared after auth frame exchange now that driver
5087 * takes care of association frame encryption/decryption. */
5088 /* TK is not needed anymore in supplicant */
5089 os_memset(sm->ptk.tk, 0, WPA_TK_MAX_LEN);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005090 sm->ptk.tk_len = 0;
Mathy Vanhoefc66556c2017-09-29 04:22:51 +02005091 sm->ptk.installed = 1;
Sunil8cd6f4d2022-06-28 18:40:46 +00005092 sm->tk_set = true;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08005093
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08005094 /* FILS HLP Container */
5095 fils_process_hlp_container(sm, ie_start, end - ie_start);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08005096
5097 /* TODO: FILS IP Address Assignment */
5098
5099 wpa_printf(MSG_DEBUG, "FILS: Auth+Assoc completed successfully");
5100 sm->fils_completed = 1;
Hai Shalom81f62d82019-07-22 12:10:00 -07005101 forced_memzero(&gd, sizeof(gd));
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08005102
Hai Shalomfdcde762020-04-02 11:19:20 -07005103 if (kde.transition_disable)
5104 wpa_sm_transition_disable(sm, kde.transition_disable[0]);
5105
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08005106 return 0;
5107fail:
Hai Shalom81f62d82019-07-22 12:10:00 -07005108 forced_memzero(&gd, sizeof(gd));
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08005109 return -1;
5110}
5111
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005112
5113void wpa_sm_set_reset_fils_completed(struct wpa_sm *sm, int set)
5114{
5115 if (sm)
5116 sm->fils_completed = !!set;
5117}
5118
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08005119#endif /* CONFIG_FILS */
5120
5121
5122int wpa_fils_is_completed(struct wpa_sm *sm)
5123{
5124#ifdef CONFIG_FILS
5125 return sm && sm->fils_completed;
5126#else /* CONFIG_FILS */
5127 return 0;
5128#endif /* CONFIG_FILS */
5129}
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005130
5131
5132#ifdef CONFIG_OWE
5133
5134struct wpabuf * owe_build_assoc_req(struct wpa_sm *sm, u16 group)
5135{
5136 struct wpabuf *ie = NULL, *pub = NULL;
5137 size_t prime_len;
5138
5139 if (group == 19)
5140 prime_len = 32;
5141 else if (group == 20)
5142 prime_len = 48;
5143 else if (group == 21)
5144 prime_len = 66;
5145 else
5146 return NULL;
5147
5148 crypto_ecdh_deinit(sm->owe_ecdh);
5149 sm->owe_ecdh = crypto_ecdh_init(group);
5150 if (!sm->owe_ecdh)
5151 goto fail;
5152 sm->owe_group = group;
5153 pub = crypto_ecdh_get_pubkey(sm->owe_ecdh, 0);
5154 pub = wpabuf_zeropad(pub, prime_len);
5155 if (!pub)
5156 goto fail;
5157
5158 ie = wpabuf_alloc(5 + wpabuf_len(pub));
5159 if (!ie)
5160 goto fail;
5161 wpabuf_put_u8(ie, WLAN_EID_EXTENSION);
5162 wpabuf_put_u8(ie, 1 + 2 + wpabuf_len(pub));
5163 wpabuf_put_u8(ie, WLAN_EID_EXT_OWE_DH_PARAM);
5164 wpabuf_put_le16(ie, group);
5165 wpabuf_put_buf(ie, pub);
5166 wpabuf_free(pub);
5167 wpa_hexdump_buf(MSG_DEBUG, "OWE: Diffie-Hellman Parameter element",
5168 ie);
5169
5170 return ie;
5171fail:
5172 wpabuf_free(pub);
5173 crypto_ecdh_deinit(sm->owe_ecdh);
5174 sm->owe_ecdh = NULL;
5175 return NULL;
5176}
5177
5178
5179int owe_process_assoc_resp(struct wpa_sm *sm, const u8 *bssid,
5180 const u8 *resp_ies, size_t resp_ies_len)
5181{
5182 struct ieee802_11_elems elems;
5183 u16 group;
5184 struct wpabuf *secret, *pub, *hkey;
5185 int res;
5186 u8 prk[SHA512_MAC_LEN], pmkid[SHA512_MAC_LEN];
5187 const char *info = "OWE Key Generation";
5188 const u8 *addr[2];
5189 size_t len[2];
5190 size_t hash_len, prime_len;
5191 struct wpa_ie_data data;
5192
5193 if (!resp_ies ||
5194 ieee802_11_parse_elems(resp_ies, resp_ies_len, &elems, 1) ==
5195 ParseFailed) {
5196 wpa_printf(MSG_INFO,
5197 "OWE: Could not parse Association Response frame elements");
5198 return -1;
5199 }
5200
5201 if (sm->cur_pmksa && elems.rsn_ie &&
5202 wpa_parse_wpa_ie_rsn(elems.rsn_ie - 2, 2 + elems.rsn_ie_len,
5203 &data) == 0 &&
5204 data.num_pmkid == 1 && data.pmkid &&
5205 os_memcmp(sm->cur_pmksa->pmkid, data.pmkid, PMKID_LEN) == 0) {
5206 wpa_printf(MSG_DEBUG, "OWE: Use PMKSA caching");
5207 wpa_sm_set_pmk_from_pmksa(sm);
5208 return 0;
5209 }
5210
5211 if (!elems.owe_dh) {
5212 wpa_printf(MSG_INFO,
5213 "OWE: No Diffie-Hellman Parameter element found in Association Response frame");
5214 return -1;
5215 }
5216
5217 group = WPA_GET_LE16(elems.owe_dh);
5218 if (group != sm->owe_group) {
5219 wpa_printf(MSG_INFO,
5220 "OWE: Unexpected Diffie-Hellman group in response: %u",
5221 group);
5222 return -1;
5223 }
5224
5225 if (!sm->owe_ecdh) {
5226 wpa_printf(MSG_INFO, "OWE: No ECDH state available");
5227 return -1;
5228 }
5229
5230 if (group == 19)
5231 prime_len = 32;
5232 else if (group == 20)
5233 prime_len = 48;
5234 else if (group == 21)
5235 prime_len = 66;
5236 else
5237 return -1;
5238
5239 secret = crypto_ecdh_set_peerkey(sm->owe_ecdh, 0,
5240 elems.owe_dh + 2,
5241 elems.owe_dh_len - 2);
5242 secret = wpabuf_zeropad(secret, prime_len);
5243 if (!secret) {
5244 wpa_printf(MSG_DEBUG, "OWE: Invalid peer DH public key");
5245 return -1;
5246 }
5247 wpa_hexdump_buf_key(MSG_DEBUG, "OWE: DH shared secret", secret);
5248
5249 /* prk = HKDF-extract(C | A | group, z) */
5250
5251 pub = crypto_ecdh_get_pubkey(sm->owe_ecdh, 0);
5252 if (!pub) {
5253 wpabuf_clear_free(secret);
5254 return -1;
5255 }
5256
5257 /* PMKID = Truncate-128(Hash(C | A)) */
5258 addr[0] = wpabuf_head(pub);
5259 len[0] = wpabuf_len(pub);
5260 addr[1] = elems.owe_dh + 2;
5261 len[1] = elems.owe_dh_len - 2;
5262 if (group == 19) {
5263 res = sha256_vector(2, addr, len, pmkid);
5264 hash_len = SHA256_MAC_LEN;
5265 } else if (group == 20) {
5266 res = sha384_vector(2, addr, len, pmkid);
5267 hash_len = SHA384_MAC_LEN;
5268 } else if (group == 21) {
5269 res = sha512_vector(2, addr, len, pmkid);
5270 hash_len = SHA512_MAC_LEN;
5271 } else {
5272 res = -1;
5273 hash_len = 0;
5274 }
5275 pub = wpabuf_zeropad(pub, prime_len);
5276 if (res < 0 || !pub) {
5277 wpabuf_free(pub);
5278 wpabuf_clear_free(secret);
5279 return -1;
5280 }
5281
5282 hkey = wpabuf_alloc(wpabuf_len(pub) + elems.owe_dh_len - 2 + 2);
5283 if (!hkey) {
5284 wpabuf_free(pub);
5285 wpabuf_clear_free(secret);
5286 return -1;
5287 }
5288
5289 wpabuf_put_buf(hkey, pub); /* C */
5290 wpabuf_free(pub);
5291 wpabuf_put_data(hkey, elems.owe_dh + 2, elems.owe_dh_len - 2); /* A */
5292 wpabuf_put_le16(hkey, sm->owe_group); /* group */
5293 if (group == 19)
5294 res = hmac_sha256(wpabuf_head(hkey), wpabuf_len(hkey),
5295 wpabuf_head(secret), wpabuf_len(secret), prk);
5296 else if (group == 20)
5297 res = hmac_sha384(wpabuf_head(hkey), wpabuf_len(hkey),
5298 wpabuf_head(secret), wpabuf_len(secret), prk);
5299 else if (group == 21)
5300 res = hmac_sha512(wpabuf_head(hkey), wpabuf_len(hkey),
5301 wpabuf_head(secret), wpabuf_len(secret), prk);
5302 wpabuf_clear_free(hkey);
5303 wpabuf_clear_free(secret);
5304 if (res < 0)
5305 return -1;
5306
5307 wpa_hexdump_key(MSG_DEBUG, "OWE: prk", prk, hash_len);
5308
5309 /* PMK = HKDF-expand(prk, "OWE Key Generation", n) */
5310
5311 if (group == 19)
5312 res = hmac_sha256_kdf(prk, hash_len, NULL, (const u8 *) info,
5313 os_strlen(info), sm->pmk, hash_len);
5314 else if (group == 20)
5315 res = hmac_sha384_kdf(prk, hash_len, NULL, (const u8 *) info,
5316 os_strlen(info), sm->pmk, hash_len);
5317 else if (group == 21)
5318 res = hmac_sha512_kdf(prk, hash_len, NULL, (const u8 *) info,
5319 os_strlen(info), sm->pmk, hash_len);
Hai Shalom81f62d82019-07-22 12:10:00 -07005320 forced_memzero(prk, SHA512_MAC_LEN);
Roshan Pius3a1667e2018-07-03 15:17:14 -07005321 if (res < 0) {
5322 sm->pmk_len = 0;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005323 return -1;
Roshan Pius3a1667e2018-07-03 15:17:14 -07005324 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005325 sm->pmk_len = hash_len;
5326
5327 wpa_hexdump_key(MSG_DEBUG, "OWE: PMK", sm->pmk, sm->pmk_len);
5328 wpa_hexdump(MSG_DEBUG, "OWE: PMKID", pmkid, PMKID_LEN);
5329 pmksa_cache_add(sm->pmksa, sm->pmk, sm->pmk_len, pmkid, NULL, 0,
5330 bssid, sm->own_addr, sm->network_ctx, sm->key_mgmt,
5331 NULL);
5332
5333 return 0;
5334}
5335
5336#endif /* CONFIG_OWE */
5337
5338
5339void wpa_sm_set_fils_cache_id(struct wpa_sm *sm, const u8 *fils_cache_id)
5340{
5341#ifdef CONFIG_FILS
5342 if (sm && fils_cache_id) {
5343 sm->fils_cache_id_set = 1;
5344 os_memcpy(sm->fils_cache_id, fils_cache_id, FILS_CACHE_ID_LEN);
5345 }
5346#endif /* CONFIG_FILS */
5347}
Hai Shalom021b0b52019-04-10 11:17:58 -07005348
5349
5350#ifdef CONFIG_DPP2
5351void wpa_sm_set_dpp_z(struct wpa_sm *sm, const struct wpabuf *z)
5352{
5353 if (sm) {
5354 wpabuf_clear_free(sm->dpp_z);
5355 sm->dpp_z = z ? wpabuf_dup(z) : NULL;
5356 }
5357}
5358#endif /* CONFIG_DPP2 */
Hai Shalom60840252021-02-19 19:02:11 -08005359
5360
5361#ifdef CONFIG_PASN
Sunil Ravi89eba102022-09-13 21:04:37 -07005362
Hai Shalom60840252021-02-19 19:02:11 -08005363void wpa_pasn_pmksa_cache_add(struct wpa_sm *sm, const u8 *pmk, size_t pmk_len,
5364 const u8 *pmkid, const u8 *bssid, int key_mgmt)
5365{
5366 sm->cur_pmksa = pmksa_cache_add(sm->pmksa, pmk, pmk_len, pmkid, NULL, 0,
5367 bssid, sm->own_addr, NULL,
5368 key_mgmt, 0);
5369}
Sunil Ravi89eba102022-09-13 21:04:37 -07005370
5371
5372void wpa_pasn_sm_set_caps(struct wpa_sm *sm, unsigned int flags2)
5373{
5374 if (flags2 & WPA_DRIVER_FLAGS2_SEC_LTF_STA)
5375 sm->secure_ltf = 1;
5376 if (flags2 & WPA_DRIVER_FLAGS2_SEC_RTT_STA)
5377 sm->secure_rtt = 1;
5378 if (flags2 & WPA_DRIVER_FLAGS2_PROT_RANGE_NEG_STA)
5379 sm->prot_range_neg = 1;
5380}
5381
Hai Shalom60840252021-02-19 19:02:11 -08005382#endif /* CONFIG_PASN */
Hai Shalomc1a21442022-02-04 13:43:00 -08005383
5384
5385void wpa_sm_pmksa_cache_reconfig(struct wpa_sm *sm)
5386{
5387 if (sm)
5388 pmksa_cache_reconfig(sm->pmksa);
5389}