blob: 66a78066545f4383118527569b7bd88b3659c7b4 [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 &&
190 wpa_sm_get_state(sm) == WPA_COMPLETED) {
191 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
305 if (sm->key_mgmt == WPA_KEY_MGMT_FT_SAE &&
306 sm->pmk_len == PMK_LEN) {
307 /* Need to allow FT key derivation to proceed with
308 * PMK from SAE being used as the XXKey in cases where
309 * the PMKID in msg 1/4 matches the PMKSA entry that was
310 * just added based on SAE authentication for the
311 * initial mobility domain association. */
312 os_memcpy(sm->xxkey, sm->pmk, sm->pmk_len);
313 sm->xxkey_len = sm->pmk_len;
314 }
315#endif /* CONFIG_SAE */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700316#endif /* CONFIG_IEEE80211R */
317 } else if (wpa_key_mgmt_wpa_ieee8021x(sm->key_mgmt) && sm->eapol) {
318 int res, pmk_len;
Hai Shalom81f62d82019-07-22 12:10:00 -0700319#ifdef CONFIG_IEEE80211R
320 u8 buf[2 * PMK_LEN];
321#endif /* CONFIG_IEEE80211R */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800322
Dmitry Shmidtebd93af2017-02-21 13:40:44 -0800323 if (wpa_key_mgmt_sha384(sm->key_mgmt))
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800324 pmk_len = PMK_LEN_SUITE_B_192;
325 else
326 pmk_len = PMK_LEN;
327 res = eapol_sm_get_key(sm->eapol, sm->pmk, pmk_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700328 if (res) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800329 if (pmk_len == PMK_LEN) {
330 /*
331 * EAP-LEAP is an exception from other EAP
332 * methods: it uses only 16-byte PMK.
333 */
334 res = eapol_sm_get_key(sm->eapol, sm->pmk, 16);
335 pmk_len = 16;
336 }
Hai Shalomf1c97642019-07-19 23:42:07 +0000337 }
Hai Shalom81f62d82019-07-22 12:10:00 -0700338#ifdef CONFIG_IEEE80211R
339 if (res == 0 &&
340 eapol_sm_get_key(sm->eapol, buf, 2 * PMK_LEN) == 0) {
341 if (wpa_key_mgmt_sha384(sm->key_mgmt)) {
342 os_memcpy(sm->xxkey, buf, SHA384_MAC_LEN);
343 sm->xxkey_len = SHA384_MAC_LEN;
344 } else {
345 os_memcpy(sm->xxkey, buf + PMK_LEN, PMK_LEN);
346 sm->xxkey_len = PMK_LEN;
347 }
348 forced_memzero(buf, sizeof(buf));
349 if (sm->proto == WPA_PROTO_RSN &&
350 wpa_key_mgmt_ft(sm->key_mgmt)) {
351 struct rsn_pmksa_cache_entry *sa = NULL;
352 const u8 *fils_cache_id = NULL;
353
354#ifdef CONFIG_FILS
355 if (sm->fils_cache_id_set)
356 fils_cache_id = sm->fils_cache_id;
357#endif /* CONFIG_FILS */
358 wpa_hexdump_key(MSG_DEBUG,
359 "FT: Cache XXKey/MPMK",
360 sm->xxkey, sm->xxkey_len);
361 sa = pmksa_cache_add(sm->pmksa,
362 sm->xxkey, sm->xxkey_len,
363 NULL, NULL, 0,
364 src_addr, sm->own_addr,
365 sm->network_ctx,
366 sm->key_mgmt,
367 fils_cache_id);
368 if (!sm->cur_pmksa)
369 sm->cur_pmksa = sa;
370 }
371 }
372#endif /* CONFIG_IEEE80211R */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700373 if (res == 0) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700374 struct rsn_pmksa_cache_entry *sa = NULL;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700375 const u8 *fils_cache_id = NULL;
376
377#ifdef CONFIG_FILS
378 if (sm->fils_cache_id_set)
379 fils_cache_id = sm->fils_cache_id;
380#endif /* CONFIG_FILS */
381
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700382 wpa_hexdump_key(MSG_DEBUG, "WPA: PMK from EAPOL state "
383 "machines", sm->pmk, pmk_len);
384 sm->pmk_len = pmk_len;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800385 wpa_supplicant_key_mgmt_set_pmk(sm);
Dmitry Shmidtc55524a2011-07-07 11:18:38 -0700386 if (sm->proto == WPA_PROTO_RSN &&
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800387 !wpa_key_mgmt_suite_b(sm->key_mgmt) &&
Dmitry Shmidtc55524a2011-07-07 11:18:38 -0700388 !wpa_key_mgmt_ft(sm->key_mgmt)) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700389 sa = pmksa_cache_add(sm->pmksa,
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800390 sm->pmk, pmk_len, NULL,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800391 NULL, 0,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700392 src_addr, sm->own_addr,
393 sm->network_ctx,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700394 sm->key_mgmt,
395 fils_cache_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700396 }
397 if (!sm->cur_pmksa && pmkid &&
Roshan Pius3a1667e2018-07-03 15:17:14 -0700398 pmksa_cache_get(sm->pmksa, src_addr, pmkid, NULL,
399 0)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700400 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
401 "RSN: the new PMK matches with the "
402 "PMKID");
403 abort_cached = 0;
Jouni Malinen6ec30382015-07-08 20:48:18 +0300404 } else if (sa && !sm->cur_pmksa && pmkid) {
405 /*
406 * It looks like the authentication server
407 * derived mismatching MSK. This should not
408 * really happen, but bugs happen.. There is not
409 * much we can do here without knowing what
410 * exactly caused the server to misbehave.
411 */
Dmitry Shmidt014a3ff2015-12-28 13:27:49 -0800412 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
Jouni Malinen6ec30382015-07-08 20:48:18 +0300413 "RSN: PMKID mismatch - authentication server may have derived different MSK?!");
414 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700415 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700416
417 if (!sm->cur_pmksa)
418 sm->cur_pmksa = sa;
Hai Shalom81f62d82019-07-22 12:10:00 -0700419#ifdef CONFIG_IEEE80211R
420 } else if (wpa_key_mgmt_ft(sm->key_mgmt) && sm->ft_protocol) {
421 wpa_printf(MSG_DEBUG,
422 "FT: Continue 4-way handshake without PMK/PMKID for association using FT protocol");
423#endif /* CONFIG_IEEE80211R */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700424 } else {
425 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
426 "WPA: Failed to get master session key from "
427 "EAPOL state machines - key handshake "
428 "aborted");
429 if (sm->cur_pmksa) {
430 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
431 "RSN: Cancelled PMKSA caching "
432 "attempt");
433 sm->cur_pmksa = NULL;
434 abort_cached = 1;
435 } else if (!abort_cached) {
436 return -1;
437 }
438 }
439 }
440
Dmitry Shmidtc55524a2011-07-07 11:18:38 -0700441 if (abort_cached && wpa_key_mgmt_wpa_ieee8021x(sm->key_mgmt) &&
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800442 !wpa_key_mgmt_suite_b(sm->key_mgmt) &&
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800443 !wpa_key_mgmt_ft(sm->key_mgmt) && sm->key_mgmt != WPA_KEY_MGMT_OSEN)
444 {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700445 /* Send EAPOL-Start to trigger full EAP authentication. */
446 u8 *buf;
447 size_t buflen;
448
449 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
450 "RSN: no PMKSA entry found - trigger "
451 "full EAP authentication");
452 buf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_START,
453 NULL, 0, &buflen, NULL);
454 if (buf) {
Hai Shalomc1a21442022-02-04 13:43:00 -0800455 /* Set and reset eapFail to allow EAP state machine to
456 * proceed with new authentication. */
457 eapol_sm_notify_eap_fail(sm->eapol, true);
458 eapol_sm_notify_eap_fail(sm->eapol, false);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700459 wpa_sm_ether_send(sm, sm->bssid, ETH_P_EAPOL,
460 buf, buflen);
461 os_free(buf);
462 return -2;
463 }
464
465 return -1;
466 }
467
468 return 0;
469}
470
471
472/**
473 * wpa_supplicant_send_2_of_4 - Send message 2 of WPA/RSN 4-Way Handshake
474 * @sm: Pointer to WPA state machine data from wpa_sm_init()
475 * @dst: Destination address for the frame
476 * @key: Pointer to the EAPOL-Key frame header
477 * @ver: Version bits from EAPOL-Key Key Info
478 * @nonce: Nonce value for the EAPOL-Key frame
479 * @wpa_ie: WPA/RSN IE
480 * @wpa_ie_len: Length of the WPA/RSN IE
481 * @ptk: PTK to use for keyed hash and encryption
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800482 * Returns: >= 0 on success, < 0 on failure
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700483 */
484int wpa_supplicant_send_2_of_4(struct wpa_sm *sm, const unsigned char *dst,
485 const struct wpa_eapol_key *key,
486 int ver, const u8 *nonce,
487 const u8 *wpa_ie, size_t wpa_ie_len,
488 struct wpa_ptk *ptk)
489{
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800490 size_t mic_len, hdrlen, rlen;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700491 struct wpa_eapol_key *reply;
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800492 u8 *rbuf, *key_mic;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700493 u8 *rsn_ie_buf = NULL;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800494 u16 key_info;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700495
496 if (wpa_ie == NULL) {
497 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, "WPA: No wpa_ie set - "
498 "cannot generate msg 2/4");
499 return -1;
500 }
501
502#ifdef CONFIG_IEEE80211R
503 if (wpa_key_mgmt_ft(sm->key_mgmt)) {
504 int res;
505
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -0800506 wpa_hexdump(MSG_DEBUG, "WPA: WPA IE before FT processing",
507 wpa_ie, wpa_ie_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700508 /*
509 * Add PMKR1Name into RSN IE (PMKID-List) and add MDIE and
510 * FTIE from (Re)Association Response.
511 */
512 rsn_ie_buf = os_malloc(wpa_ie_len + 2 + 2 + PMKID_LEN +
513 sm->assoc_resp_ies_len);
514 if (rsn_ie_buf == NULL)
515 return -1;
516 os_memcpy(rsn_ie_buf, wpa_ie, wpa_ie_len);
Dmitry Shmidt55840ad2015-12-14 12:45:46 -0800517 res = wpa_insert_pmkid(rsn_ie_buf, &wpa_ie_len,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700518 sm->pmk_r1_name);
519 if (res < 0) {
520 os_free(rsn_ie_buf);
521 return -1;
522 }
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -0800523 wpa_hexdump(MSG_DEBUG,
524 "WPA: WPA IE after PMKID[PMKR1Name] addition into RSNE",
525 rsn_ie_buf, wpa_ie_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700526
527 if (sm->assoc_resp_ies) {
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -0800528 wpa_hexdump(MSG_DEBUG, "WPA: Add assoc_resp_ies",
529 sm->assoc_resp_ies,
530 sm->assoc_resp_ies_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700531 os_memcpy(rsn_ie_buf + wpa_ie_len, sm->assoc_resp_ies,
532 sm->assoc_resp_ies_len);
533 wpa_ie_len += sm->assoc_resp_ies_len;
534 }
535
536 wpa_ie = rsn_ie_buf;
537 }
538#endif /* CONFIG_IEEE80211R */
539
540 wpa_hexdump(MSG_DEBUG, "WPA: WPA IE for msg 2/4", wpa_ie, wpa_ie_len);
541
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700542 mic_len = wpa_mic_len(sm->key_mgmt, sm->pmk_len);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800543 hdrlen = sizeof(*reply) + mic_len + 2;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700544 rbuf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_KEY,
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800545 NULL, hdrlen + wpa_ie_len,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700546 &rlen, (void *) &reply);
547 if (rbuf == NULL) {
548 os_free(rsn_ie_buf);
549 return -1;
550 }
551
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800552 reply->type = (sm->proto == WPA_PROTO_RSN ||
553 sm->proto == WPA_PROTO_OSEN) ?
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700554 EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800555 key_info = ver | WPA_KEY_INFO_KEY_TYPE;
556 if (mic_len)
557 key_info |= WPA_KEY_INFO_MIC;
558 else
559 key_info |= WPA_KEY_INFO_ENCR_KEY_DATA;
560 WPA_PUT_BE16(reply->key_info, key_info);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800561 if (sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700562 WPA_PUT_BE16(reply->key_length, 0);
563 else
564 os_memcpy(reply->key_length, key->key_length, 2);
565 os_memcpy(reply->replay_counter, key->replay_counter,
566 WPA_REPLAY_COUNTER_LEN);
567 wpa_hexdump(MSG_DEBUG, "WPA: Replay Counter", reply->replay_counter,
568 WPA_REPLAY_COUNTER_LEN);
569
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800570 key_mic = (u8 *) (reply + 1);
571 WPA_PUT_BE16(key_mic + mic_len, wpa_ie_len); /* Key Data Length */
572 os_memcpy(key_mic + mic_len + 2, wpa_ie, wpa_ie_len); /* Key Data */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700573 os_free(rsn_ie_buf);
574
575 os_memcpy(reply->key_nonce, nonce, WPA_NONCE_LEN);
576
Roshan Pius5e7db942018-04-12 12:27:41 -0700577 wpa_dbg(sm->ctx->msg_ctx, MSG_INFO, "WPA: Sending EAPOL-Key 2/4");
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800578 return wpa_eapol_key_send(sm, ptk, ver, dst, ETH_P_EAPOL, rbuf, rlen,
579 key_mic);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700580}
581
582
583static int wpa_derive_ptk(struct wpa_sm *sm, const unsigned char *src_addr,
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800584 const struct wpa_eapol_key *key, struct wpa_ptk *ptk)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700585{
Hai Shalom021b0b52019-04-10 11:17:58 -0700586 const u8 *z = NULL;
Hai Shalom60840252021-02-19 19:02:11 -0800587 size_t z_len = 0, kdk_len;
Hai Shalomfdcde762020-04-02 11:19:20 -0700588 int akmp;
Hai Shalom021b0b52019-04-10 11:17:58 -0700589
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700590#ifdef CONFIG_IEEE80211R
591 if (wpa_key_mgmt_ft(sm->key_mgmt))
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800592 return wpa_derive_ptk_ft(sm, src_addr, key, ptk);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700593#endif /* CONFIG_IEEE80211R */
594
Hai Shalom021b0b52019-04-10 11:17:58 -0700595#ifdef CONFIG_DPP2
596 if (sm->key_mgmt == WPA_KEY_MGMT_DPP && sm->dpp_z) {
597 z = wpabuf_head(sm->dpp_z);
598 z_len = wpabuf_len(sm->dpp_z);
599 }
600#endif /* CONFIG_DPP2 */
601
Hai Shalomfdcde762020-04-02 11:19:20 -0700602 akmp = sm->key_mgmt;
603#ifdef CONFIG_OWE
604 if (sm->owe_ptk_workaround && akmp == WPA_KEY_MGMT_OWE &&
605 sm->pmk_len > 32) {
606 wpa_printf(MSG_DEBUG,
607 "OWE: Force SHA256 for PTK derivation");
608 akmp |= WPA_KEY_MGMT_PSK_SHA256;
609 }
610#endif /* CONFIG_OWE */
Hai Shalom60840252021-02-19 19:02:11 -0800611
612 if (sm->force_kdk_derivation ||
Hai Shalomc1a21442022-02-04 13:43:00 -0800613 (sm->secure_ltf &&
614 ieee802_11_rsnx_capab(sm->ap_rsnxe, WLAN_RSNX_CAPAB_SECURE_LTF)))
Hai Shalom60840252021-02-19 19:02:11 -0800615 kdk_len = WPA_KDK_MAX_LEN;
616 else
617 kdk_len = 0;
618
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800619 return wpa_pmk_to_ptk(sm->pmk, sm->pmk_len, "Pairwise key expansion",
620 sm->own_addr, sm->bssid, sm->snonce,
Hai Shalomfdcde762020-04-02 11:19:20 -0700621 key->key_nonce, ptk, akmp,
Hai Shalom60840252021-02-19 19:02:11 -0800622 sm->pairwise_cipher, z, z_len,
623 kdk_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700624}
625
626
Hai Shalomfdcde762020-04-02 11:19:20 -0700627static int wpa_handle_ext_key_id(struct wpa_sm *sm,
628 struct wpa_eapol_ie_parse *kde)
629{
630 if (sm->ext_key_id) {
631 u16 key_id;
632
633 if (!kde->key_id) {
634 wpa_msg(sm->ctx->msg_ctx,
635 sm->use_ext_key_id ? MSG_INFO : MSG_DEBUG,
636 "RSN: No Key ID in Extended Key ID handshake");
637 sm->keyidx_active = 0;
638 return sm->use_ext_key_id ? -1 : 0;
639 }
640
641 key_id = kde->key_id[0] & 0x03;
642 if (key_id > 1) {
643 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
644 "RSN: Invalid Extended Key ID: %d", key_id);
645 return -1;
646 }
647 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
648 "RSN: Using Extended Key ID %d", key_id);
649 sm->keyidx_active = key_id;
650 sm->use_ext_key_id = 1;
651 } else {
652 if (kde->key_id && (kde->key_id[0] & 0x03)) {
653 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
654 "RSN: Non-zero Extended Key ID Key ID in PTK0 handshake");
655 return -1;
656 }
657
658 if (kde->key_id) {
659 /* This is not supposed to be included here, but ignore
660 * the case of matching Key ID 0 just in case. */
661 wpa_msg(sm->ctx->msg_ctx, MSG_DEBUG,
662 "RSN: Extended Key ID Key ID 0 in PTK0 handshake");
663 }
664 sm->keyidx_active = 0;
665 sm->use_ext_key_id = 0;
666 }
667
668 return 0;
669}
670
671
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700672static void wpa_supplicant_process_1_of_4(struct wpa_sm *sm,
673 const unsigned char *src_addr,
674 const struct wpa_eapol_key *key,
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700675 u16 ver, const u8 *key_data,
676 size_t key_data_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700677{
678 struct wpa_eapol_ie_parse ie;
679 struct wpa_ptk *ptk;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700680 int res;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800681 u8 *kde, *kde_buf = NULL;
682 size_t kde_len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700683
684 if (wpa_sm_get_network_ctx(sm) == NULL) {
685 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, "WPA: No SSID info "
686 "found (msg 1 of 4)");
687 return;
688 }
689
Hai Shalomfdcde762020-04-02 11:19:20 -0700690 if (sm->wpa_deny_ptk0_rekey && !sm->use_ext_key_id &&
691 wpa_sm_get_state(sm) == WPA_COMPLETED) {
692 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
693 "WPA: PTK0 rekey not allowed, reconnecting");
694 wpa_sm_reconnect(sm);
695 return;
696 }
697
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700698 wpa_sm_set_state(sm, WPA_4WAY_HANDSHAKE);
Roshan Pius5e7db942018-04-12 12:27:41 -0700699 wpa_dbg(sm->ctx->msg_ctx, MSG_INFO, "WPA: RX message 1 of 4-Way "
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700700 "Handshake from " MACSTR " (ver=%d)", MAC2STR(src_addr), ver);
701
702 os_memset(&ie, 0, sizeof(ie));
703
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800704 if (sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700705 /* RSN: msg 1/4 should contain PMKID for the selected PMK */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700706 wpa_hexdump(MSG_DEBUG, "RSN: msg 1/4 key data",
707 key_data, key_data_len);
708 if (wpa_supplicant_parse_ies(key_data, key_data_len, &ie) < 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800709 goto failed;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700710 if (ie.pmkid) {
711 wpa_hexdump(MSG_DEBUG, "RSN: PMKID from "
712 "Authenticator", ie.pmkid, PMKID_LEN);
713 }
714 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700715
716 res = wpa_supplicant_get_pmk(sm, src_addr, ie.pmkid);
717 if (res == -2) {
718 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "RSN: Do not reply to "
719 "msg 1/4 - requesting full EAP authentication");
720 return;
721 }
722 if (res)
723 goto failed;
724
725 if (sm->renew_snonce) {
726 if (random_get_bytes(sm->snonce, WPA_NONCE_LEN)) {
727 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
728 "WPA: Failed to get random data for SNonce");
729 goto failed;
730 }
731 sm->renew_snonce = 0;
732 wpa_hexdump(MSG_DEBUG, "WPA: Renewed SNonce",
733 sm->snonce, WPA_NONCE_LEN);
734 }
735
736 /* Calculate PTK which will be stored as a temporary PTK until it has
737 * been verified when processing message 3/4. */
738 ptk = &sm->tptk;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700739 if (wpa_derive_ptk(sm, src_addr, key, ptk) < 0)
740 goto failed;
Dmitry Shmidt98660862014-03-11 17:26:21 -0700741 if (sm->pairwise_cipher == WPA_CIPHER_TKIP) {
Dmitry Shmidt61593f02014-04-21 16:27:35 -0700742 u8 buf[8];
Dmitry Shmidt98660862014-03-11 17:26:21 -0700743 /* Supplicant: swap tx/rx Mic keys */
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800744 os_memcpy(buf, &ptk->tk[16], 8);
745 os_memcpy(&ptk->tk[16], &ptk->tk[24], 8);
746 os_memcpy(&ptk->tk[24], buf, 8);
Hai Shalom81f62d82019-07-22 12:10:00 -0700747 forced_memzero(buf, sizeof(buf));
Dmitry Shmidt98660862014-03-11 17:26:21 -0700748 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700749 sm->tptk_set = 1;
750
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800751 kde = sm->assoc_wpa_ie;
752 kde_len = sm->assoc_wpa_ie_len;
Hai Shalomc3565922019-10-28 11:58:20 -0700753 kde_buf = os_malloc(kde_len +
754 2 + RSN_SELECTOR_LEN + 3 +
755 sm->assoc_rsnxe_len +
Hai Shalom4fbc08f2020-05-18 12:37:00 -0700756 2 + RSN_SELECTOR_LEN + 1 +
757 2 + RSN_SELECTOR_LEN + 2);
Hai Shalomc3565922019-10-28 11:58:20 -0700758 if (!kde_buf)
759 goto failed;
760 os_memcpy(kde_buf, kde, kde_len);
761 kde = kde_buf;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800762
Hai Shalom74f70d42019-02-11 14:42:39 -0800763#ifdef CONFIG_OCV
764 if (wpa_sm_ocv_enabled(sm)) {
765 struct wpa_channel_info ci;
766 u8 *pos;
767
Hai Shalomc3565922019-10-28 11:58:20 -0700768 pos = kde + kde_len;
Hai Shalom74f70d42019-02-11 14:42:39 -0800769 if (wpa_sm_channel_info(sm, &ci) != 0) {
770 wpa_printf(MSG_WARNING,
771 "Failed to get channel info for OCI element in EAPOL-Key 2/4");
772 goto failed;
773 }
Hai Shalom899fcc72020-10-19 14:38:18 -0700774#ifdef CONFIG_TESTING_OPTIONS
775 if (sm->oci_freq_override_eapol) {
776 wpa_printf(MSG_INFO,
777 "TEST: Override OCI KDE frequency %d -> %d MHz",
778 ci.frequency, sm->oci_freq_override_eapol);
779 ci.frequency = sm->oci_freq_override_eapol;
780 }
781#endif /* CONFIG_TESTING_OPTIONS */
Hai Shalom74f70d42019-02-11 14:42:39 -0800782
Hai Shalom74f70d42019-02-11 14:42:39 -0800783 if (ocv_insert_oci_kde(&ci, &pos) < 0)
784 goto failed;
785 kde_len = pos - kde;
786 }
787#endif /* CONFIG_OCV */
788
Hai Shalomc3565922019-10-28 11:58:20 -0700789 if (sm->assoc_rsnxe && sm->assoc_rsnxe_len) {
790 os_memcpy(kde + kde_len, sm->assoc_rsnxe, sm->assoc_rsnxe_len);
791 kde_len += sm->assoc_rsnxe_len;
792 }
793
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800794#ifdef CONFIG_P2P
795 if (sm->p2p) {
Hai Shalomc3565922019-10-28 11:58:20 -0700796 u8 *pos;
797
798 wpa_printf(MSG_DEBUG,
799 "P2P: Add IP Address Request KDE into EAPOL-Key 2/4");
800 pos = kde + kde_len;
801 *pos++ = WLAN_EID_VENDOR_SPECIFIC;
802 *pos++ = RSN_SELECTOR_LEN + 1;
803 RSN_SELECTOR_PUT(pos, WFA_KEY_DATA_IP_ADDR_REQ);
804 pos += RSN_SELECTOR_LEN;
805 *pos++ = 0x01;
806 kde_len = pos - kde;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800807 }
808#endif /* CONFIG_P2P */
809
Hai Shalom4fbc08f2020-05-18 12:37:00 -0700810#ifdef CONFIG_DPP2
811 if (DPP_VERSION > 1 && sm->key_mgmt == WPA_KEY_MGMT_DPP) {
812 u8 *pos;
813
814 wpa_printf(MSG_DEBUG, "DPP: Add DPP KDE into EAPOL-Key 2/4");
815 pos = kde + kde_len;
816 *pos++ = WLAN_EID_VENDOR_SPECIFIC;
817 *pos++ = RSN_SELECTOR_LEN + 2;
818 RSN_SELECTOR_PUT(pos, WFA_KEY_DATA_DPP);
819 pos += RSN_SELECTOR_LEN;
820 *pos++ = DPP_VERSION; /* Protocol Version */
821 *pos = 0; /* Flags */
822 if (sm->dpp_pfs == 0)
823 *pos |= DPP_KDE_PFS_ALLOWED;
824 else if (sm->dpp_pfs == 1)
825 *pos |= DPP_KDE_PFS_ALLOWED | DPP_KDE_PFS_REQUIRED;
826 pos++;
827 kde_len = pos - kde;
828 }
829#endif /* CONFIG_DPP2 */
830
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700831 if (wpa_supplicant_send_2_of_4(sm, sm->bssid, key, ver, sm->snonce,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800832 kde, kde_len, ptk) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700833 goto failed;
834
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800835 os_free(kde_buf);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700836 os_memcpy(sm->anonce, key->key_nonce, WPA_NONCE_LEN);
837 return;
838
839failed:
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800840 os_free(kde_buf);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700841 wpa_sm_deauthenticate(sm, WLAN_REASON_UNSPECIFIED);
842}
843
844
845static void wpa_sm_start_preauth(void *eloop_ctx, void *timeout_ctx)
846{
847 struct wpa_sm *sm = eloop_ctx;
848 rsn_preauth_candidate_process(sm);
849}
850
851
852static void wpa_supplicant_key_neg_complete(struct wpa_sm *sm,
853 const u8 *addr, int secure)
854{
855 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
856 "WPA: Key negotiation completed with "
857 MACSTR " [PTK=%s GTK=%s]", MAC2STR(addr),
858 wpa_cipher_txt(sm->pairwise_cipher),
859 wpa_cipher_txt(sm->group_cipher));
860 wpa_sm_cancel_auth_timeout(sm);
861 wpa_sm_set_state(sm, WPA_COMPLETED);
862
863 if (secure) {
864 wpa_sm_mlme_setprotection(
865 sm, addr, MLME_SETPROTECTION_PROTECT_TYPE_RX_TX,
866 MLME_SETPROTECTION_KEY_TYPE_PAIRWISE);
Hai Shalome21d4e82020-04-29 16:34:06 -0700867 eapol_sm_notify_portValid(sm->eapol, true);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700868 if (wpa_key_mgmt_wpa_psk(sm->key_mgmt) ||
869 sm->key_mgmt == WPA_KEY_MGMT_DPP ||
870 sm->key_mgmt == WPA_KEY_MGMT_OWE)
Hai Shalome21d4e82020-04-29 16:34:06 -0700871 eapol_sm_notify_eap_success(sm->eapol, true);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700872 /*
873 * Start preauthentication after a short wait to avoid a
874 * possible race condition between the data receive and key
875 * configuration after the 4-Way Handshake. This increases the
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800876 * likelihood of the first preauth EAPOL-Start frame getting to
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700877 * the target AP.
878 */
Hai Shalom74f70d42019-02-11 14:42:39 -0800879 if (!dl_list_empty(&sm->pmksa_candidates))
880 eloop_register_timeout(1, 0, wpa_sm_start_preauth,
881 sm, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700882 }
883
884 if (sm->cur_pmksa && sm->cur_pmksa->opportunistic) {
885 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
886 "RSN: Authenticator accepted "
887 "opportunistic PMKSA entry - marking it valid");
888 sm->cur_pmksa->opportunistic = 0;
889 }
890
891#ifdef CONFIG_IEEE80211R
892 if (wpa_key_mgmt_ft(sm->key_mgmt)) {
893 /* Prepare for the next transition */
894 wpa_ft_prepare_auth_request(sm, NULL);
895 }
896#endif /* CONFIG_IEEE80211R */
897}
898
899
900static void wpa_sm_rekey_ptk(void *eloop_ctx, void *timeout_ctx)
901{
902 struct wpa_sm *sm = eloop_ctx;
903 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Request PTK rekeying");
904 wpa_sm_key_request(sm, 0, 1);
905}
906
907
908static int wpa_supplicant_install_ptk(struct wpa_sm *sm,
Hai Shalomfdcde762020-04-02 11:19:20 -0700909 const struct wpa_eapol_key *key,
910 enum key_flag key_flag)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700911{
912 int keylen, rsclen;
913 enum wpa_alg alg;
914 const u8 *key_rsc;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800915
Mathy Vanhoefc66556c2017-09-29 04:22:51 +0200916 if (sm->ptk.installed) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800917 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
918 "WPA: Do not re-install same PTK to the driver");
919 return 0;
920 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700921
922 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
923 "WPA: Installing PTK to the driver");
924
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700925 if (sm->pairwise_cipher == WPA_CIPHER_NONE) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700926 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Pairwise Cipher "
927 "Suite: NONE - do not use pairwise keys");
928 return 0;
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700929 }
930
931 if (!wpa_cipher_valid_pairwise(sm->pairwise_cipher)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700932 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
933 "WPA: Unsupported pairwise cipher %d",
934 sm->pairwise_cipher);
935 return -1;
936 }
937
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700938 alg = wpa_cipher_to_alg(sm->pairwise_cipher);
939 keylen = wpa_cipher_key_len(sm->pairwise_cipher);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700940 if (keylen <= 0 || (unsigned int) keylen != sm->ptk.tk_len) {
941 wpa_printf(MSG_DEBUG, "WPA: TK length mismatch: %d != %lu",
942 keylen, (long unsigned int) sm->ptk.tk_len);
943 return -1;
944 }
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700945 rsclen = wpa_cipher_rsc_len(sm->pairwise_cipher);
946
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800947 if (sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700948 key_rsc = null_rsc;
949 } else {
950 key_rsc = key->key_rsc;
951 wpa_hexdump(MSG_DEBUG, "WPA: RSC", key_rsc, rsclen);
952 }
953
Hai Shalomfdcde762020-04-02 11:19:20 -0700954 if (wpa_sm_set_key(sm, alg, sm->bssid, sm->keyidx_active, 1, key_rsc,
955 rsclen, sm->ptk.tk, keylen,
956 KEY_FLAG_PAIRWISE | key_flag) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700957 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
Hai Shalomfdcde762020-04-02 11:19:20 -0700958 "WPA: Failed to set PTK to the driver (alg=%d keylen=%d bssid="
959 MACSTR " idx=%d key_flag=0x%x)",
960 alg, keylen, MAC2STR(sm->bssid),
961 sm->keyidx_active, key_flag);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700962 return -1;
963 }
964
Hai Shalom60840252021-02-19 19:02:11 -0800965 wpa_sm_store_ptk(sm, sm->bssid, sm->pairwise_cipher,
966 sm->dot11RSNAConfigPMKLifetime, &sm->ptk);
967
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800968 /* TK is not needed anymore in supplicant */
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800969 os_memset(sm->ptk.tk, 0, WPA_TK_MAX_LEN);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700970 sm->ptk.tk_len = 0;
Mathy Vanhoefc66556c2017-09-29 04:22:51 +0200971 sm->ptk.installed = 1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800972
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700973 if (sm->wpa_ptk_rekey) {
974 eloop_cancel_timeout(wpa_sm_rekey_ptk, sm, NULL);
975 eloop_register_timeout(sm->wpa_ptk_rekey, 0, wpa_sm_rekey_ptk,
976 sm, NULL);
977 }
Hai Shalomfdcde762020-04-02 11:19:20 -0700978 return 0;
979}
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700980
Hai Shalomfdcde762020-04-02 11:19:20 -0700981
982static int wpa_supplicant_activate_ptk(struct wpa_sm *sm)
983{
984 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
985 "WPA: Activate PTK (idx=%d bssid=" MACSTR ")",
986 sm->keyidx_active, MAC2STR(sm->bssid));
987
988 if (wpa_sm_set_key(sm, 0, sm->bssid, sm->keyidx_active, 0, NULL, 0,
989 NULL, 0, KEY_FLAG_PAIRWISE_RX_TX_MODIFY) < 0) {
990 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
991 "WPA: Failed to activate PTK for TX (idx=%d bssid="
992 MACSTR ")", sm->keyidx_active, MAC2STR(sm->bssid));
993 return -1;
994 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700995 return 0;
996}
997
998
999static int wpa_supplicant_check_group_cipher(struct wpa_sm *sm,
1000 int group_cipher,
1001 int keylen, int maxkeylen,
1002 int *key_rsc_len,
1003 enum wpa_alg *alg)
1004{
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07001005 int klen;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001006
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07001007 *alg = wpa_cipher_to_alg(group_cipher);
1008 if (*alg == WPA_ALG_NONE) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001009 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1010 "WPA: Unsupported Group Cipher %d",
1011 group_cipher);
1012 return -1;
1013 }
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07001014 *key_rsc_len = wpa_cipher_rsc_len(group_cipher);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001015
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07001016 klen = wpa_cipher_key_len(group_cipher);
1017 if (keylen != klen || maxkeylen < klen) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001018 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1019 "WPA: Unsupported %s Group Cipher key length %d (%d)",
1020 wpa_cipher_txt(group_cipher), keylen, maxkeylen);
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07001021 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001022 }
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07001023 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001024}
1025
1026
1027struct wpa_gtk_data {
1028 enum wpa_alg alg;
1029 int tx, key_rsc_len, keyidx;
1030 u8 gtk[32];
1031 int gtk_len;
1032};
1033
1034
1035static int wpa_supplicant_install_gtk(struct wpa_sm *sm,
1036 const struct wpa_gtk_data *gd,
Jouni Malinen58c0e962017-10-01 12:12:24 +03001037 const u8 *key_rsc, int wnm_sleep)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001038{
1039 const u8 *_gtk = gd->gtk;
1040 u8 gtk_buf[32];
1041
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02001042 /* Detect possible key reinstallation */
Jouni Malinen58c0e962017-10-01 12:12:24 +03001043 if ((sm->gtk.gtk_len == (size_t) gd->gtk_len &&
1044 os_memcmp(sm->gtk.gtk, gd->gtk, sm->gtk.gtk_len) == 0) ||
1045 (sm->gtk_wnm_sleep.gtk_len == (size_t) gd->gtk_len &&
1046 os_memcmp(sm->gtk_wnm_sleep.gtk, gd->gtk,
1047 sm->gtk_wnm_sleep.gtk_len) == 0)) {
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02001048 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1049 "WPA: Not reinstalling already in-use GTK to the driver (keyidx=%d tx=%d len=%d)",
1050 gd->keyidx, gd->tx, gd->gtk_len);
1051 return 0;
1052 }
1053
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001054 wpa_hexdump_key(MSG_DEBUG, "WPA: Group Key", gd->gtk, gd->gtk_len);
1055 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1056 "WPA: Installing GTK to the driver (keyidx=%d tx=%d len=%d)",
1057 gd->keyidx, gd->tx, gd->gtk_len);
1058 wpa_hexdump(MSG_DEBUG, "WPA: RSC", key_rsc, gd->key_rsc_len);
1059 if (sm->group_cipher == WPA_CIPHER_TKIP) {
1060 /* Swap Tx/Rx keys for Michael MIC */
1061 os_memcpy(gtk_buf, gd->gtk, 16);
1062 os_memcpy(gtk_buf + 16, gd->gtk + 24, 8);
1063 os_memcpy(gtk_buf + 24, gd->gtk + 16, 8);
1064 _gtk = gtk_buf;
1065 }
1066 if (sm->pairwise_cipher == WPA_CIPHER_NONE) {
1067 if (wpa_sm_set_key(sm, gd->alg, NULL,
1068 gd->keyidx, 1, key_rsc, gd->key_rsc_len,
Hai Shalomfdcde762020-04-02 11:19:20 -07001069 _gtk, gd->gtk_len,
1070 KEY_FLAG_GROUP_RX_TX_DEFAULT) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001071 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1072 "WPA: Failed to set GTK to the driver "
1073 "(Group only)");
Hai Shalom81f62d82019-07-22 12:10:00 -07001074 forced_memzero(gtk_buf, sizeof(gtk_buf));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001075 return -1;
1076 }
1077 } else if (wpa_sm_set_key(sm, gd->alg, broadcast_ether_addr,
1078 gd->keyidx, gd->tx, key_rsc, gd->key_rsc_len,
Hai Shalomfdcde762020-04-02 11:19:20 -07001079 _gtk, gd->gtk_len, KEY_FLAG_GROUP_RX) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001080 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1081 "WPA: Failed to set GTK to "
1082 "the driver (alg=%d keylen=%d keyidx=%d)",
1083 gd->alg, gd->gtk_len, gd->keyidx);
Hai Shalom81f62d82019-07-22 12:10:00 -07001084 forced_memzero(gtk_buf, sizeof(gtk_buf));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001085 return -1;
1086 }
Hai Shalom81f62d82019-07-22 12:10:00 -07001087 forced_memzero(gtk_buf, sizeof(gtk_buf));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001088
Jouni Malinen58c0e962017-10-01 12:12:24 +03001089 if (wnm_sleep) {
1090 sm->gtk_wnm_sleep.gtk_len = gd->gtk_len;
1091 os_memcpy(sm->gtk_wnm_sleep.gtk, gd->gtk,
1092 sm->gtk_wnm_sleep.gtk_len);
1093 } else {
1094 sm->gtk.gtk_len = gd->gtk_len;
1095 os_memcpy(sm->gtk.gtk, gd->gtk, sm->gtk.gtk_len);
1096 }
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02001097
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001098 return 0;
1099}
1100
1101
1102static int wpa_supplicant_gtk_tx_bit_workaround(const struct wpa_sm *sm,
1103 int tx)
1104{
1105 if (tx && sm->pairwise_cipher != WPA_CIPHER_NONE) {
1106 /* Ignore Tx bit for GTK if a pairwise key is used. One AP
1107 * seemed to set this bit (incorrectly, since Tx is only when
1108 * doing Group Key only APs) and without this workaround, the
1109 * data connection does not work because wpa_supplicant
1110 * configured non-zero keyidx to be used for unicast. */
1111 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1112 "WPA: Tx bit set for GTK, but pairwise "
1113 "keys are used - ignore Tx bit");
1114 return 0;
1115 }
1116 return tx;
1117}
1118
1119
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001120static int wpa_supplicant_rsc_relaxation(const struct wpa_sm *sm,
1121 const u8 *rsc)
1122{
1123 int rsclen;
1124
1125 if (!sm->wpa_rsc_relaxation)
1126 return 0;
1127
1128 rsclen = wpa_cipher_rsc_len(sm->group_cipher);
1129
1130 /*
1131 * Try to detect RSC (endian) corruption issue where the AP sends
1132 * the RSC bytes in EAPOL-Key message in the wrong order, both if
1133 * it's actually a 6-byte field (as it should be) and if it treats
1134 * it as an 8-byte field.
1135 * An AP model known to have this bug is the Sapido RB-1632.
1136 */
1137 if (rsclen == 6 && ((rsc[5] && !rsc[0]) || rsc[6] || rsc[7])) {
1138 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1139 "RSC %02x%02x%02x%02x%02x%02x%02x%02x is likely bogus, using 0",
1140 rsc[0], rsc[1], rsc[2], rsc[3],
1141 rsc[4], rsc[5], rsc[6], rsc[7]);
1142
1143 return 1;
1144 }
1145
1146 return 0;
1147}
1148
1149
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001150static int wpa_supplicant_pairwise_gtk(struct wpa_sm *sm,
1151 const struct wpa_eapol_key *key,
1152 const u8 *gtk, size_t gtk_len,
1153 int key_info)
1154{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001155 struct wpa_gtk_data gd;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001156 const u8 *key_rsc;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001157
1158 /*
1159 * IEEE Std 802.11i-2004 - 8.5.2 EAPOL-Key frames - Figure 43x
1160 * GTK KDE format:
1161 * KeyID[bits 0-1], Tx [bit 2], Reserved [bits 3-7]
1162 * Reserved [bits 0-7]
1163 * GTK
1164 */
1165
1166 os_memset(&gd, 0, sizeof(gd));
1167 wpa_hexdump_key(MSG_DEBUG, "RSN: received GTK in pairwise handshake",
1168 gtk, gtk_len);
1169
1170 if (gtk_len < 2 || gtk_len - 2 > sizeof(gd.gtk))
1171 return -1;
1172
1173 gd.keyidx = gtk[0] & 0x3;
1174 gd.tx = wpa_supplicant_gtk_tx_bit_workaround(sm,
1175 !!(gtk[0] & BIT(2)));
1176 gtk += 2;
1177 gtk_len -= 2;
1178
1179 os_memcpy(gd.gtk, gtk, gtk_len);
1180 gd.gtk_len = gtk_len;
1181
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001182 key_rsc = key->key_rsc;
1183 if (wpa_supplicant_rsc_relaxation(sm, key->key_rsc))
1184 key_rsc = null_rsc;
1185
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001186 if (sm->group_cipher != WPA_CIPHER_GTK_NOT_USED &&
1187 (wpa_supplicant_check_group_cipher(sm, sm->group_cipher,
1188 gtk_len, gtk_len,
1189 &gd.key_rsc_len, &gd.alg) ||
Jouni Malinen58c0e962017-10-01 12:12:24 +03001190 wpa_supplicant_install_gtk(sm, &gd, key_rsc, 0))) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001191 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1192 "RSN: Failed to install GTK");
Hai Shalom81f62d82019-07-22 12:10:00 -07001193 forced_memzero(&gd, sizeof(gd));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001194 return -1;
1195 }
Hai Shalom81f62d82019-07-22 12:10:00 -07001196 forced_memzero(&gd, sizeof(gd));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001197
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001198 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001199}
1200
1201
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02001202static int wpa_supplicant_install_igtk(struct wpa_sm *sm,
Jouni Malinen58c0e962017-10-01 12:12:24 +03001203 const struct wpa_igtk_kde *igtk,
1204 int wnm_sleep)
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02001205{
1206 size_t len = wpa_cipher_key_len(sm->mgmt_group_cipher);
1207 u16 keyidx = WPA_GET_LE16(igtk->keyid);
1208
1209 /* Detect possible key reinstallation */
Jouni Malinen58c0e962017-10-01 12:12:24 +03001210 if ((sm->igtk.igtk_len == len &&
1211 os_memcmp(sm->igtk.igtk, igtk->igtk, sm->igtk.igtk_len) == 0) ||
1212 (sm->igtk_wnm_sleep.igtk_len == len &&
1213 os_memcmp(sm->igtk_wnm_sleep.igtk, igtk->igtk,
1214 sm->igtk_wnm_sleep.igtk_len) == 0)) {
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02001215 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1216 "WPA: Not reinstalling already in-use IGTK to the driver (keyidx=%d)",
1217 keyidx);
1218 return 0;
1219 }
1220
1221 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
Hai Shalom39ba6fc2019-01-22 12:40:38 -08001222 "WPA: IGTK keyid %d pn " COMPACT_MACSTR,
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02001223 keyidx, MAC2STR(igtk->pn));
1224 wpa_hexdump_key(MSG_DEBUG, "WPA: IGTK", igtk->igtk, len);
1225 if (keyidx > 4095) {
1226 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1227 "WPA: Invalid IGTK KeyID %d", keyidx);
1228 return -1;
1229 }
1230 if (wpa_sm_set_key(sm, wpa_cipher_to_alg(sm->mgmt_group_cipher),
1231 broadcast_ether_addr,
1232 keyidx, 0, igtk->pn, sizeof(igtk->pn),
Hai Shalomfdcde762020-04-02 11:19:20 -07001233 igtk->igtk, len, KEY_FLAG_GROUP_RX) < 0) {
Hai Shalom5f92bc92019-04-18 11:54:11 -07001234 if (keyidx == 0x0400 || keyidx == 0x0500) {
1235 /* Assume the AP has broken PMF implementation since it
1236 * seems to have swapped the KeyID bytes. The AP cannot
1237 * be trusted to implement BIP correctly or provide a
1238 * valid IGTK, so do not try to configure this key with
1239 * swapped KeyID bytes. Instead, continue without
1240 * configuring the IGTK so that the driver can drop any
1241 * received group-addressed robust management frames due
1242 * to missing keys.
1243 *
1244 * Normally, this error behavior would result in us
1245 * disconnecting, but there are number of deployed APs
1246 * with this broken behavior, so as an interoperability
1247 * workaround, allow the connection to proceed. */
1248 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1249 "WPA: Ignore IGTK configuration error due to invalid IGTK KeyID byte order");
1250 } else {
1251 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1252 "WPA: Failed to configure IGTK to the driver");
1253 return -1;
1254 }
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02001255 }
1256
Jouni Malinen58c0e962017-10-01 12:12:24 +03001257 if (wnm_sleep) {
1258 sm->igtk_wnm_sleep.igtk_len = len;
1259 os_memcpy(sm->igtk_wnm_sleep.igtk, igtk->igtk,
1260 sm->igtk_wnm_sleep.igtk_len);
1261 } else {
1262 sm->igtk.igtk_len = len;
1263 os_memcpy(sm->igtk.igtk, igtk->igtk, sm->igtk.igtk_len);
1264 }
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02001265
1266 return 0;
1267}
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02001268
1269
Hai Shalomfdcde762020-04-02 11:19:20 -07001270static int wpa_supplicant_install_bigtk(struct wpa_sm *sm,
1271 const struct wpa_bigtk_kde *bigtk,
1272 int wnm_sleep)
1273{
1274 size_t len = wpa_cipher_key_len(sm->mgmt_group_cipher);
1275 u16 keyidx = WPA_GET_LE16(bigtk->keyid);
1276
1277 /* Detect possible key reinstallation */
1278 if ((sm->bigtk.bigtk_len == len &&
1279 os_memcmp(sm->bigtk.bigtk, bigtk->bigtk,
1280 sm->bigtk.bigtk_len) == 0) ||
1281 (sm->bigtk_wnm_sleep.bigtk_len == len &&
1282 os_memcmp(sm->bigtk_wnm_sleep.bigtk, bigtk->bigtk,
1283 sm->bigtk_wnm_sleep.bigtk_len) == 0)) {
1284 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1285 "WPA: Not reinstalling already in-use BIGTK to the driver (keyidx=%d)",
1286 keyidx);
1287 return 0;
1288 }
1289
1290 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1291 "WPA: BIGTK keyid %d pn " COMPACT_MACSTR,
1292 keyidx, MAC2STR(bigtk->pn));
1293 wpa_hexdump_key(MSG_DEBUG, "WPA: BIGTK", bigtk->bigtk, len);
1294 if (keyidx < 6 || keyidx > 7) {
1295 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1296 "WPA: Invalid BIGTK KeyID %d", keyidx);
1297 return -1;
1298 }
1299 if (wpa_sm_set_key(sm, wpa_cipher_to_alg(sm->mgmt_group_cipher),
1300 broadcast_ether_addr,
1301 keyidx, 0, bigtk->pn, sizeof(bigtk->pn),
1302 bigtk->bigtk, len, KEY_FLAG_GROUP_RX) < 0) {
1303 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1304 "WPA: Failed to configure BIGTK to the driver");
1305 return -1;
1306 }
1307
1308 if (wnm_sleep) {
1309 sm->bigtk_wnm_sleep.bigtk_len = len;
1310 os_memcpy(sm->bigtk_wnm_sleep.bigtk, bigtk->bigtk,
1311 sm->bigtk_wnm_sleep.bigtk_len);
1312 } else {
1313 sm->bigtk.bigtk_len = len;
1314 os_memcpy(sm->bigtk.bigtk, bigtk->bigtk, sm->bigtk.bigtk_len);
1315 }
1316
1317 return 0;
1318}
1319
1320
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001321static int ieee80211w_set_keys(struct wpa_sm *sm,
1322 struct wpa_eapol_ie_parse *ie)
1323{
Hai Shalomfdcde762020-04-02 11:19:20 -07001324 size_t len;
1325
Hai Shalom60840252021-02-19 19:02:11 -08001326 if (!wpa_cipher_valid_mgmt_group(sm->mgmt_group_cipher) ||
1327 sm->mgmt_group_cipher == WPA_CIPHER_GTK_NOT_USED)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001328 return 0;
1329
1330 if (ie->igtk) {
1331 const struct wpa_igtk_kde *igtk;
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02001332
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001333 len = wpa_cipher_key_len(sm->mgmt_group_cipher);
1334 if (ie->igtk_len != WPA_IGTK_KDE_PREFIX_LEN + len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001335 return -1;
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02001336
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001337 igtk = (const struct wpa_igtk_kde *) ie->igtk;
Jouni Malinen58c0e962017-10-01 12:12:24 +03001338 if (wpa_supplicant_install_igtk(sm, igtk, 0) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001339 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001340 }
1341
Hai Shalomfdcde762020-04-02 11:19:20 -07001342 if (ie->bigtk && sm->beacon_prot) {
1343 const struct wpa_bigtk_kde *bigtk;
1344
1345 len = wpa_cipher_key_len(sm->mgmt_group_cipher);
1346 if (ie->bigtk_len != WPA_BIGTK_KDE_PREFIX_LEN + len)
1347 return -1;
1348
1349 bigtk = (const struct wpa_bigtk_kde *) ie->bigtk;
1350 if (wpa_supplicant_install_bigtk(sm, bigtk, 0) < 0)
1351 return -1;
1352 }
1353
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001354 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001355}
1356
1357
1358static void wpa_report_ie_mismatch(struct wpa_sm *sm,
1359 const char *reason, const u8 *src_addr,
1360 const u8 *wpa_ie, size_t wpa_ie_len,
1361 const u8 *rsn_ie, size_t rsn_ie_len)
1362{
1363 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, "WPA: %s (src=" MACSTR ")",
1364 reason, MAC2STR(src_addr));
1365
1366 if (sm->ap_wpa_ie) {
1367 wpa_hexdump(MSG_INFO, "WPA: WPA IE in Beacon/ProbeResp",
1368 sm->ap_wpa_ie, sm->ap_wpa_ie_len);
1369 }
1370 if (wpa_ie) {
1371 if (!sm->ap_wpa_ie) {
1372 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1373 "WPA: No WPA IE in Beacon/ProbeResp");
1374 }
1375 wpa_hexdump(MSG_INFO, "WPA: WPA IE in 3/4 msg",
1376 wpa_ie, wpa_ie_len);
1377 }
1378
1379 if (sm->ap_rsn_ie) {
1380 wpa_hexdump(MSG_INFO, "WPA: RSN IE in Beacon/ProbeResp",
1381 sm->ap_rsn_ie, sm->ap_rsn_ie_len);
1382 }
1383 if (rsn_ie) {
1384 if (!sm->ap_rsn_ie) {
1385 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1386 "WPA: No RSN IE in Beacon/ProbeResp");
1387 }
1388 wpa_hexdump(MSG_INFO, "WPA: RSN IE in 3/4 msg",
1389 rsn_ie, rsn_ie_len);
1390 }
1391
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001392 wpa_sm_deauthenticate(sm, WLAN_REASON_IE_IN_4WAY_DIFFERS);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001393}
1394
1395
1396#ifdef CONFIG_IEEE80211R
1397
1398static int ft_validate_mdie(struct wpa_sm *sm,
1399 const unsigned char *src_addr,
1400 struct wpa_eapol_ie_parse *ie,
1401 const u8 *assoc_resp_mdie)
1402{
1403 struct rsn_mdie *mdie;
1404
1405 mdie = (struct rsn_mdie *) (ie->mdie + 2);
1406 if (ie->mdie == NULL || ie->mdie_len < 2 + sizeof(*mdie) ||
1407 os_memcmp(mdie->mobility_domain, sm->mobility_domain,
1408 MOBILITY_DOMAIN_ID_LEN) != 0) {
1409 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "FT: MDIE in msg 3/4 did "
1410 "not match with the current mobility domain");
1411 return -1;
1412 }
1413
1414 if (assoc_resp_mdie &&
1415 (assoc_resp_mdie[1] != ie->mdie[1] ||
1416 os_memcmp(assoc_resp_mdie, ie->mdie, 2 + ie->mdie[1]) != 0)) {
1417 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "FT: MDIE mismatch");
1418 wpa_hexdump(MSG_DEBUG, "FT: MDIE in EAPOL-Key msg 3/4",
1419 ie->mdie, 2 + ie->mdie[1]);
1420 wpa_hexdump(MSG_DEBUG, "FT: MDIE in (Re)Association Response",
1421 assoc_resp_mdie, 2 + assoc_resp_mdie[1]);
1422 return -1;
1423 }
1424
1425 return 0;
1426}
1427
1428
1429static int ft_validate_ftie(struct wpa_sm *sm,
1430 const unsigned char *src_addr,
1431 struct wpa_eapol_ie_parse *ie,
1432 const u8 *assoc_resp_ftie)
1433{
1434 if (ie->ftie == NULL) {
1435 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1436 "FT: No FTIE in EAPOL-Key msg 3/4");
1437 return -1;
1438 }
1439
1440 if (assoc_resp_ftie == NULL)
1441 return 0;
1442
1443 if (assoc_resp_ftie[1] != ie->ftie[1] ||
1444 os_memcmp(assoc_resp_ftie, ie->ftie, 2 + ie->ftie[1]) != 0) {
1445 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "FT: FTIE mismatch");
1446 wpa_hexdump(MSG_DEBUG, "FT: FTIE in EAPOL-Key msg 3/4",
1447 ie->ftie, 2 + ie->ftie[1]);
1448 wpa_hexdump(MSG_DEBUG, "FT: FTIE in (Re)Association Response",
1449 assoc_resp_ftie, 2 + assoc_resp_ftie[1]);
1450 return -1;
1451 }
1452
1453 return 0;
1454}
1455
1456
1457static int ft_validate_rsnie(struct wpa_sm *sm,
1458 const unsigned char *src_addr,
1459 struct wpa_eapol_ie_parse *ie)
1460{
1461 struct wpa_ie_data rsn;
1462
1463 if (!ie->rsn_ie)
1464 return 0;
1465
1466 /*
1467 * Verify that PMKR1Name from EAPOL-Key message 3/4
1468 * matches with the value we derived.
1469 */
1470 if (wpa_parse_wpa_ie_rsn(ie->rsn_ie, ie->rsn_ie_len, &rsn) < 0 ||
1471 rsn.num_pmkid != 1 || rsn.pmkid == NULL) {
1472 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "FT: No PMKR1Name in "
1473 "FT 4-way handshake message 3/4");
1474 return -1;
1475 }
1476
Dmitry Shmidtc2817022014-07-02 10:32:10 -07001477 if (os_memcmp_const(rsn.pmkid, sm->pmk_r1_name, WPA_PMK_NAME_LEN) != 0)
1478 {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001479 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1480 "FT: PMKR1Name mismatch in "
1481 "FT 4-way handshake message 3/4");
1482 wpa_hexdump(MSG_DEBUG, "FT: PMKR1Name from Authenticator",
1483 rsn.pmkid, WPA_PMK_NAME_LEN);
1484 wpa_hexdump(MSG_DEBUG, "FT: Derived PMKR1Name",
1485 sm->pmk_r1_name, WPA_PMK_NAME_LEN);
1486 return -1;
1487 }
1488
1489 return 0;
1490}
1491
1492
1493static int wpa_supplicant_validate_ie_ft(struct wpa_sm *sm,
1494 const unsigned char *src_addr,
1495 struct wpa_eapol_ie_parse *ie)
1496{
1497 const u8 *pos, *end, *mdie = NULL, *ftie = NULL;
1498
1499 if (sm->assoc_resp_ies) {
1500 pos = sm->assoc_resp_ies;
1501 end = pos + sm->assoc_resp_ies_len;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001502 while (end - pos > 2) {
1503 if (2 + pos[1] > end - pos)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001504 break;
1505 switch (*pos) {
1506 case WLAN_EID_MOBILITY_DOMAIN:
1507 mdie = pos;
1508 break;
1509 case WLAN_EID_FAST_BSS_TRANSITION:
1510 ftie = pos;
1511 break;
1512 }
1513 pos += 2 + pos[1];
1514 }
1515 }
1516
1517 if (ft_validate_mdie(sm, src_addr, ie, mdie) < 0 ||
1518 ft_validate_ftie(sm, src_addr, ie, ftie) < 0 ||
1519 ft_validate_rsnie(sm, src_addr, ie) < 0)
1520 return -1;
1521
1522 return 0;
1523}
1524
1525#endif /* CONFIG_IEEE80211R */
1526
1527
1528static int wpa_supplicant_validate_ie(struct wpa_sm *sm,
1529 const unsigned char *src_addr,
1530 struct wpa_eapol_ie_parse *ie)
1531{
1532 if (sm->ap_wpa_ie == NULL && sm->ap_rsn_ie == NULL) {
1533 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1534 "WPA: No WPA/RSN IE for this AP known. "
1535 "Trying to get from scan results");
1536 if (wpa_sm_get_beacon_ie(sm) < 0) {
1537 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1538 "WPA: Could not find AP from "
1539 "the scan results");
Hai Shalomfdcde762020-04-02 11:19:20 -07001540 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001541 }
Hai Shalomfdcde762020-04-02 11:19:20 -07001542 wpa_msg(sm->ctx->msg_ctx, MSG_DEBUG,
1543 "WPA: Found the current AP from updated scan results");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001544 }
1545
1546 if (ie->wpa_ie == NULL && ie->rsn_ie == NULL &&
1547 (sm->ap_wpa_ie || sm->ap_rsn_ie)) {
1548 wpa_report_ie_mismatch(sm, "IE in 3/4 msg does not match "
1549 "with IE in Beacon/ProbeResp (no IE?)",
1550 src_addr, ie->wpa_ie, ie->wpa_ie_len,
1551 ie->rsn_ie, ie->rsn_ie_len);
1552 return -1;
1553 }
1554
1555 if ((ie->wpa_ie && sm->ap_wpa_ie &&
1556 (ie->wpa_ie_len != sm->ap_wpa_ie_len ||
1557 os_memcmp(ie->wpa_ie, sm->ap_wpa_ie, ie->wpa_ie_len) != 0)) ||
1558 (ie->rsn_ie && sm->ap_rsn_ie &&
1559 wpa_compare_rsn_ie(wpa_key_mgmt_ft(sm->key_mgmt),
1560 sm->ap_rsn_ie, sm->ap_rsn_ie_len,
1561 ie->rsn_ie, ie->rsn_ie_len))) {
1562 wpa_report_ie_mismatch(sm, "IE in 3/4 msg does not match "
1563 "with IE in Beacon/ProbeResp",
1564 src_addr, ie->wpa_ie, ie->wpa_ie_len,
1565 ie->rsn_ie, ie->rsn_ie_len);
1566 return -1;
1567 }
1568
1569 if (sm->proto == WPA_PROTO_WPA &&
1570 ie->rsn_ie && sm->ap_rsn_ie == NULL && sm->rsn_enabled) {
1571 wpa_report_ie_mismatch(sm, "Possible downgrade attack "
1572 "detected - RSN was enabled and RSN IE "
1573 "was in msg 3/4, but not in "
1574 "Beacon/ProbeResp",
1575 src_addr, ie->wpa_ie, ie->wpa_ie_len,
1576 ie->rsn_ie, ie->rsn_ie_len);
1577 return -1;
1578 }
1579
Hai Shalom60840252021-02-19 19:02:11 -08001580 if (sm->proto == WPA_PROTO_RSN &&
1581 ((sm->ap_rsnxe && !ie->rsnxe) ||
1582 (!sm->ap_rsnxe && ie->rsnxe) ||
1583 (sm->ap_rsnxe && ie->rsnxe &&
1584 (sm->ap_rsnxe_len != ie->rsnxe_len ||
1585 os_memcmp(sm->ap_rsnxe, ie->rsnxe, sm->ap_rsnxe_len) != 0)))) {
Hai Shalomc3565922019-10-28 11:58:20 -07001586 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1587 "WPA: RSNXE mismatch between Beacon/ProbeResp and EAPOL-Key msg 3/4");
Hai Shalomfdcde762020-04-02 11:19:20 -07001588 wpa_hexdump(MSG_INFO, "RSNXE in Beacon/ProbeResp",
1589 sm->ap_rsnxe, sm->ap_rsnxe_len);
1590 wpa_hexdump(MSG_INFO, "RSNXE in EAPOL-Key msg 3/4",
1591 ie->rsnxe, ie->rsnxe_len);
1592 wpa_sm_deauthenticate(sm, WLAN_REASON_IE_IN_4WAY_DIFFERS);
Hai Shalomc3565922019-10-28 11:58:20 -07001593 return -1;
1594 }
1595
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001596#ifdef CONFIG_IEEE80211R
1597 if (wpa_key_mgmt_ft(sm->key_mgmt) &&
1598 wpa_supplicant_validate_ie_ft(sm, src_addr, ie) < 0)
1599 return -1;
1600#endif /* CONFIG_IEEE80211R */
1601
1602 return 0;
1603}
1604
1605
1606/**
1607 * wpa_supplicant_send_4_of_4 - Send message 4 of WPA/RSN 4-Way Handshake
1608 * @sm: Pointer to WPA state machine data from wpa_sm_init()
1609 * @dst: Destination address for the frame
1610 * @key: Pointer to the EAPOL-Key frame header
1611 * @ver: Version bits from EAPOL-Key Key Info
1612 * @key_info: Key Info
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001613 * @ptk: PTK to use for keyed hash and encryption
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001614 * Returns: >= 0 on success, < 0 on failure
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001615 */
1616int wpa_supplicant_send_4_of_4(struct wpa_sm *sm, const unsigned char *dst,
1617 const struct wpa_eapol_key *key,
1618 u16 ver, u16 key_info,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001619 struct wpa_ptk *ptk)
1620{
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001621 size_t mic_len, hdrlen, rlen;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001622 struct wpa_eapol_key *reply;
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001623 u8 *rbuf, *key_mic;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001624
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001625 mic_len = wpa_mic_len(sm->key_mgmt, sm->pmk_len);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001626 hdrlen = sizeof(*reply) + mic_len + 2;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001627 rbuf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_KEY, NULL,
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001628 hdrlen, &rlen, (void *) &reply);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001629 if (rbuf == NULL)
1630 return -1;
1631
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001632 reply->type = (sm->proto == WPA_PROTO_RSN ||
1633 sm->proto == WPA_PROTO_OSEN) ?
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001634 EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA;
1635 key_info &= WPA_KEY_INFO_SECURE;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001636 key_info |= ver | WPA_KEY_INFO_KEY_TYPE;
1637 if (mic_len)
1638 key_info |= WPA_KEY_INFO_MIC;
1639 else
1640 key_info |= WPA_KEY_INFO_ENCR_KEY_DATA;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001641 WPA_PUT_BE16(reply->key_info, key_info);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001642 if (sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001643 WPA_PUT_BE16(reply->key_length, 0);
1644 else
1645 os_memcpy(reply->key_length, key->key_length, 2);
1646 os_memcpy(reply->replay_counter, key->replay_counter,
1647 WPA_REPLAY_COUNTER_LEN);
1648
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001649 key_mic = (u8 *) (reply + 1);
1650 WPA_PUT_BE16(key_mic + mic_len, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001651
Roshan Pius5e7db942018-04-12 12:27:41 -07001652 wpa_dbg(sm->ctx->msg_ctx, MSG_INFO, "WPA: Sending EAPOL-Key 4/4");
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001653 return wpa_eapol_key_send(sm, ptk, ver, dst, ETH_P_EAPOL, rbuf, rlen,
1654 key_mic);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001655}
1656
1657
1658static void wpa_supplicant_process_3_of_4(struct wpa_sm *sm,
1659 const struct wpa_eapol_key *key,
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001660 u16 ver, const u8 *key_data,
1661 size_t key_data_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001662{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001663 u16 key_info, keylen;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001664 struct wpa_eapol_ie_parse ie;
1665
1666 wpa_sm_set_state(sm, WPA_4WAY_HANDSHAKE);
Roshan Pius5e7db942018-04-12 12:27:41 -07001667 wpa_dbg(sm->ctx->msg_ctx, MSG_INFO, "WPA: RX message 3 of 4-Way "
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001668 "Handshake from " MACSTR " (ver=%d)", MAC2STR(sm->bssid), ver);
1669
1670 key_info = WPA_GET_BE16(key->key_info);
1671
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001672 wpa_hexdump(MSG_DEBUG, "WPA: IE KeyData", key_data, key_data_len);
1673 if (wpa_supplicant_parse_ies(key_data, key_data_len, &ie) < 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001674 goto failed;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001675 if (ie.gtk && !(key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
1676 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1677 "WPA: GTK IE in unencrypted key data");
1678 goto failed;
1679 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001680 if (ie.igtk && !(key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
1681 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1682 "WPA: IGTK KDE in unencrypted key data");
1683 goto failed;
1684 }
1685
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001686 if (ie.igtk &&
Hai Shalom60840252021-02-19 19:02:11 -08001687 sm->mgmt_group_cipher != WPA_CIPHER_GTK_NOT_USED &&
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001688 wpa_cipher_valid_mgmt_group(sm->mgmt_group_cipher) &&
1689 ie.igtk_len != WPA_IGTK_KDE_PREFIX_LEN +
1690 (unsigned int) wpa_cipher_key_len(sm->mgmt_group_cipher)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001691 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1692 "WPA: Invalid IGTK KDE length %lu",
1693 (unsigned long) ie.igtk_len);
1694 goto failed;
1695 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001696
1697 if (wpa_supplicant_validate_ie(sm, sm->bssid, &ie) < 0)
1698 goto failed;
1699
Hai Shalomfdcde762020-04-02 11:19:20 -07001700 if (wpa_handle_ext_key_id(sm, &ie))
1701 goto failed;
1702
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001703 if (os_memcmp(sm->anonce, key->key_nonce, WPA_NONCE_LEN) != 0) {
1704 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1705 "WPA: ANonce from message 1 of 4-Way Handshake "
1706 "differs from 3 of 4-Way Handshake - drop packet (src="
1707 MACSTR ")", MAC2STR(sm->bssid));
1708 goto failed;
1709 }
1710
1711 keylen = WPA_GET_BE16(key->key_length);
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07001712 if (keylen != wpa_cipher_key_len(sm->pairwise_cipher)) {
1713 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1714 "WPA: Invalid %s key length %d (src=" MACSTR
1715 ")", wpa_cipher_txt(sm->pairwise_cipher), keylen,
1716 MAC2STR(sm->bssid));
1717 goto failed;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001718 }
1719
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001720#ifdef CONFIG_P2P
1721 if (ie.ip_addr_alloc) {
1722 os_memcpy(sm->p2p_ip_addr, ie.ip_addr_alloc, 3 * 4);
1723 wpa_hexdump(MSG_DEBUG, "P2P: IP address info",
1724 sm->p2p_ip_addr, sizeof(sm->p2p_ip_addr));
1725 }
1726#endif /* CONFIG_P2P */
1727
Hai Shalom74f70d42019-02-11 14:42:39 -08001728#ifdef CONFIG_OCV
1729 if (wpa_sm_ocv_enabled(sm)) {
1730 struct wpa_channel_info ci;
1731
1732 if (wpa_sm_channel_info(sm, &ci) != 0) {
1733 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1734 "Failed to get channel info to validate received OCI in EAPOL-Key 3/4");
1735 return;
1736 }
1737
1738 if (ocv_verify_tx_params(ie.oci, ie.oci_len, &ci,
1739 channel_width_to_int(ci.chanwidth),
Hai Shalom899fcc72020-10-19 14:38:18 -07001740 ci.seg1_idx) != OCI_SUCCESS) {
1741 wpa_msg(sm->ctx->msg_ctx, MSG_INFO, OCV_FAILURE
1742 "addr=" MACSTR " frame=eapol-key-m3 error=%s",
1743 MAC2STR(sm->bssid), ocv_errorstr);
Hai Shalom74f70d42019-02-11 14:42:39 -08001744 return;
1745 }
1746 }
1747#endif /* CONFIG_OCV */
1748
Hai Shalom4fbc08f2020-05-18 12:37:00 -07001749#ifdef CONFIG_DPP2
1750 if (DPP_VERSION > 1 && ie.dpp_kde) {
1751 wpa_printf(MSG_DEBUG,
1752 "DPP: peer Protocol Version %u Flags 0x%x",
1753 ie.dpp_kde[0], ie.dpp_kde[1]);
1754 if (sm->key_mgmt == WPA_KEY_MGMT_DPP && sm->dpp_pfs != 2 &&
1755 (ie.dpp_kde[1] & DPP_KDE_PFS_ALLOWED) && !sm->dpp_z) {
1756 wpa_printf(MSG_INFO,
1757 "DPP: Peer indicated it supports PFS and local configuration allows this, but PFS was not negotiated for the association");
1758 goto failed;
1759 }
1760 }
1761#endif /* CONFIG_DPP2 */
1762
Hai Shalomfdcde762020-04-02 11:19:20 -07001763 if (sm->use_ext_key_id &&
1764 wpa_supplicant_install_ptk(sm, key, KEY_FLAG_RX))
1765 goto failed;
1766
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001767 if (wpa_supplicant_send_4_of_4(sm, sm->bssid, key, ver, key_info,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001768 &sm->ptk) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001769 goto failed;
1770 }
1771
1772 /* SNonce was successfully used in msg 3/4, so mark it to be renewed
1773 * for the next 4-Way Handshake. If msg 3 is received again, the old
1774 * SNonce will still be used to avoid changing PTK. */
1775 sm->renew_snonce = 1;
1776
1777 if (key_info & WPA_KEY_INFO_INSTALL) {
Hai Shalomfdcde762020-04-02 11:19:20 -07001778 int res;
1779
1780 if (sm->use_ext_key_id)
1781 res = wpa_supplicant_activate_ptk(sm);
1782 else
1783 res = wpa_supplicant_install_ptk(sm, key,
1784 KEY_FLAG_RX_TX);
1785 if (res)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001786 goto failed;
1787 }
1788
1789 if (key_info & WPA_KEY_INFO_SECURE) {
1790 wpa_sm_mlme_setprotection(
1791 sm, sm->bssid, MLME_SETPROTECTION_PROTECT_TYPE_RX,
1792 MLME_SETPROTECTION_KEY_TYPE_PAIRWISE);
Hai Shalome21d4e82020-04-29 16:34:06 -07001793 eapol_sm_notify_portValid(sm->eapol, true);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001794 }
1795 wpa_sm_set_state(sm, WPA_GROUP_HANDSHAKE);
1796
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001797 if (sm->group_cipher == WPA_CIPHER_GTK_NOT_USED) {
Hai Shalom5f92bc92019-04-18 11:54:11 -07001798 /* No GTK to be set to the driver */
1799 } else if (!ie.gtk && sm->proto == WPA_PROTO_RSN) {
1800 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1801 "RSN: No GTK KDE included in EAPOL-Key msg 3/4");
1802 goto failed;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001803 } else if (ie.gtk &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001804 wpa_supplicant_pairwise_gtk(sm, key,
1805 ie.gtk, ie.gtk_len, key_info) < 0) {
1806 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1807 "RSN: Failed to configure GTK");
1808 goto failed;
1809 }
1810
1811 if (ieee80211w_set_keys(sm, &ie) < 0) {
1812 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1813 "RSN: Failed to configure IGTK");
1814 goto failed;
1815 }
1816
Hai Shalom5f92bc92019-04-18 11:54:11 -07001817 if (sm->group_cipher == WPA_CIPHER_GTK_NOT_USED || ie.gtk)
1818 wpa_supplicant_key_neg_complete(sm, sm->bssid,
1819 key_info & WPA_KEY_INFO_SECURE);
1820
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001821 if (ie.gtk)
1822 wpa_sm_set_rekey_offload(sm);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001823
Hai Shalom39ba6fc2019-01-22 12:40:38 -08001824 /* Add PMKSA cache entry for Suite B AKMs here since PMKID can be
1825 * calculated only after KCK has been derived. Though, do not replace an
1826 * existing PMKSA entry after each 4-way handshake (i.e., new KCK/PMKID)
1827 * to avoid unnecessary changes of PMKID while continuing to use the
1828 * same PMK. */
1829 if (sm->proto == WPA_PROTO_RSN && wpa_key_mgmt_suite_b(sm->key_mgmt) &&
1830 !sm->cur_pmksa) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001831 struct rsn_pmksa_cache_entry *sa;
1832
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001833 sa = pmksa_cache_add(sm->pmksa, sm->pmk, sm->pmk_len, NULL,
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001834 sm->ptk.kck, sm->ptk.kck_len,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001835 sm->bssid, sm->own_addr,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001836 sm->network_ctx, sm->key_mgmt, NULL);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001837 if (!sm->cur_pmksa)
1838 sm->cur_pmksa = sa;
1839 }
1840
Hai Shalomfdcde762020-04-02 11:19:20 -07001841 if (ie.transition_disable)
1842 wpa_sm_transition_disable(sm, ie.transition_disable[0]);
Dmitry Shmidt4dd28dc2015-03-10 11:21:43 -07001843 sm->msg_3_of_4_ok = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001844 return;
1845
1846failed:
1847 wpa_sm_deauthenticate(sm, WLAN_REASON_UNSPECIFIED);
1848}
1849
1850
1851static int wpa_supplicant_process_1_of_2_rsn(struct wpa_sm *sm,
1852 const u8 *keydata,
1853 size_t keydatalen,
1854 u16 key_info,
1855 struct wpa_gtk_data *gd)
1856{
1857 int maxkeylen;
1858 struct wpa_eapol_ie_parse ie;
Hai Shalom60840252021-02-19 19:02:11 -08001859 u16 gtk_len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001860
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08001861 wpa_hexdump_key(MSG_DEBUG, "RSN: msg 1/2 key data",
1862 keydata, keydatalen);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001863 if (wpa_supplicant_parse_ies(keydata, keydatalen, &ie) < 0)
1864 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001865 if (ie.gtk && !(key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
1866 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1867 "WPA: GTK IE in unencrypted key data");
1868 return -1;
1869 }
1870 if (ie.gtk == NULL) {
1871 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1872 "WPA: No GTK IE in Group Key msg 1/2");
1873 return -1;
1874 }
Hai Shalom60840252021-02-19 19:02:11 -08001875 gtk_len = ie.gtk_len;
1876 if (gtk_len < 2) {
1877 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1878 "RSN: Invalid GTK KDE length (%u) in Group Key msg 1/2",
1879 gtk_len);
1880 return -1;
1881 }
1882 gtk_len -= 2;
1883 if (gtk_len > sizeof(gd->gtk)) {
1884 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1885 "RSN: Too long GTK in GTK KDE (len=%u)", gtk_len);
1886 return -1;
1887 }
1888 maxkeylen = gd->gtk_len = gtk_len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001889
Hai Shalom74f70d42019-02-11 14:42:39 -08001890#ifdef CONFIG_OCV
1891 if (wpa_sm_ocv_enabled(sm)) {
1892 struct wpa_channel_info ci;
1893
1894 if (wpa_sm_channel_info(sm, &ci) != 0) {
1895 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1896 "Failed to get channel info to validate received OCI in EAPOL-Key group msg 1/2");
1897 return -1;
1898 }
1899
1900 if (ocv_verify_tx_params(ie.oci, ie.oci_len, &ci,
1901 channel_width_to_int(ci.chanwidth),
Hai Shalom899fcc72020-10-19 14:38:18 -07001902 ci.seg1_idx) != OCI_SUCCESS) {
1903 wpa_msg(sm->ctx->msg_ctx, MSG_INFO, OCV_FAILURE
1904 "addr=" MACSTR " frame=eapol-key-g1 error=%s",
1905 MAC2STR(sm->bssid), ocv_errorstr);
Hai Shalom74f70d42019-02-11 14:42:39 -08001906 return -1;
1907 }
1908 }
1909#endif /* CONFIG_OCV */
1910
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001911 if (wpa_supplicant_check_group_cipher(sm, sm->group_cipher,
Hai Shalom60840252021-02-19 19:02:11 -08001912 gtk_len, maxkeylen,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001913 &gd->key_rsc_len, &gd->alg))
1914 return -1;
1915
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001916 wpa_hexdump_key(MSG_DEBUG, "RSN: received GTK in group key handshake",
Hai Shalom60840252021-02-19 19:02:11 -08001917 ie.gtk, 2 + gtk_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001918 gd->keyidx = ie.gtk[0] & 0x3;
1919 gd->tx = wpa_supplicant_gtk_tx_bit_workaround(sm,
1920 !!(ie.gtk[0] & BIT(2)));
Hai Shalom60840252021-02-19 19:02:11 -08001921 os_memcpy(gd->gtk, ie.gtk + 2, gtk_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001922
1923 if (ieee80211w_set_keys(sm, &ie) < 0)
1924 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1925 "RSN: Failed to configure IGTK");
1926
1927 return 0;
1928}
1929
1930
1931static int wpa_supplicant_process_1_of_2_wpa(struct wpa_sm *sm,
1932 const struct wpa_eapol_key *key,
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001933 const u8 *key_data,
1934 size_t key_data_len, u16 key_info,
1935 u16 ver, struct wpa_gtk_data *gd)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001936{
1937 size_t maxkeylen;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001938 u16 gtk_len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001939
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001940 gtk_len = WPA_GET_BE16(key->key_length);
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001941 maxkeylen = key_data_len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001942 if (ver == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
1943 if (maxkeylen < 8) {
1944 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1945 "WPA: Too short maxkeylen (%lu)",
1946 (unsigned long) maxkeylen);
1947 return -1;
1948 }
1949 maxkeylen -= 8;
1950 }
1951
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001952 if (gtk_len > maxkeylen ||
1953 wpa_supplicant_check_group_cipher(sm, sm->group_cipher,
1954 gtk_len, maxkeylen,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001955 &gd->key_rsc_len, &gd->alg))
1956 return -1;
1957
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001958 gd->gtk_len = gtk_len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001959 gd->keyidx = (key_info & WPA_KEY_INFO_KEY_INDEX_MASK) >>
1960 WPA_KEY_INFO_KEY_INDEX_SHIFT;
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001961 if (ver == WPA_KEY_INFO_TYPE_HMAC_MD5_RC4 && sm->ptk.kek_len == 16) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001962#ifdef CONFIG_NO_RC4
1963 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1964 "WPA: RC4 not supported in the build");
1965 return -1;
1966#else /* CONFIG_NO_RC4 */
Dmitry Shmidt61593f02014-04-21 16:27:35 -07001967 u8 ek[32];
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001968 if (key_data_len > sizeof(gd->gtk)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001969 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1970 "WPA: RC4 key data too long (%lu)",
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001971 (unsigned long) key_data_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001972 return -1;
1973 }
Dmitry Shmidt61593f02014-04-21 16:27:35 -07001974 os_memcpy(ek, key->key_iv, 16);
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001975 os_memcpy(ek + 16, sm->ptk.kek, sm->ptk.kek_len);
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001976 os_memcpy(gd->gtk, key_data, key_data_len);
1977 if (rc4_skip(ek, 32, 256, gd->gtk, key_data_len)) {
Hai Shalom81f62d82019-07-22 12:10:00 -07001978 forced_memzero(ek, sizeof(ek));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001979 wpa_msg(sm->ctx->msg_ctx, MSG_ERROR,
1980 "WPA: RC4 failed");
1981 return -1;
1982 }
Hai Shalom81f62d82019-07-22 12:10:00 -07001983 forced_memzero(ek, sizeof(ek));
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001984#endif /* CONFIG_NO_RC4 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001985 } else if (ver == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001986 if (maxkeylen % 8) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001987 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1988 "WPA: Unsupported AES-WRAP len %lu",
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001989 (unsigned long) maxkeylen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001990 return -1;
1991 }
1992 if (maxkeylen > sizeof(gd->gtk)) {
1993 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1994 "WPA: AES-WRAP key data "
1995 "too long (keydatalen=%lu maxkeylen=%lu)",
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001996 (unsigned long) key_data_len,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001997 (unsigned long) maxkeylen);
1998 return -1;
1999 }
Dmitry Shmidt807291d2015-01-27 13:40:23 -08002000 if (aes_unwrap(sm->ptk.kek, sm->ptk.kek_len, maxkeylen / 8,
2001 key_data, gd->gtk)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002002 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
2003 "WPA: AES unwrap failed - could not decrypt "
2004 "GTK");
2005 return -1;
2006 }
2007 } else {
2008 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
2009 "WPA: Unsupported key_info type %d", ver);
2010 return -1;
2011 }
2012 gd->tx = wpa_supplicant_gtk_tx_bit_workaround(
2013 sm, !!(key_info & WPA_KEY_INFO_TXRX));
2014 return 0;
2015}
2016
2017
2018static int wpa_supplicant_send_2_of_2(struct wpa_sm *sm,
2019 const struct wpa_eapol_key *key,
2020 int ver, u16 key_info)
2021{
Dmitry Shmidt807291d2015-01-27 13:40:23 -08002022 size_t mic_len, hdrlen, rlen;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002023 struct wpa_eapol_key *reply;
Dmitry Shmidt807291d2015-01-27 13:40:23 -08002024 u8 *rbuf, *key_mic;
Hai Shalom74f70d42019-02-11 14:42:39 -08002025 size_t kde_len = 0;
2026
2027#ifdef CONFIG_OCV
2028 if (wpa_sm_ocv_enabled(sm))
2029 kde_len = OCV_OCI_KDE_LEN;
2030#endif /* CONFIG_OCV */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002031
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002032 mic_len = wpa_mic_len(sm->key_mgmt, sm->pmk_len);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002033 hdrlen = sizeof(*reply) + mic_len + 2;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002034 rbuf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_KEY, NULL,
Hai Shalom74f70d42019-02-11 14:42:39 -08002035 hdrlen + kde_len, &rlen, (void *) &reply);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002036 if (rbuf == NULL)
2037 return -1;
2038
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002039 reply->type = (sm->proto == WPA_PROTO_RSN ||
2040 sm->proto == WPA_PROTO_OSEN) ?
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002041 EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA;
2042 key_info &= WPA_KEY_INFO_KEY_INDEX_MASK;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002043 key_info |= ver | WPA_KEY_INFO_SECURE;
2044 if (mic_len)
2045 key_info |= WPA_KEY_INFO_MIC;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002046 else
2047 key_info |= WPA_KEY_INFO_ENCR_KEY_DATA;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002048 WPA_PUT_BE16(reply->key_info, key_info);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002049 if (sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002050 WPA_PUT_BE16(reply->key_length, 0);
2051 else
2052 os_memcpy(reply->key_length, key->key_length, 2);
2053 os_memcpy(reply->replay_counter, key->replay_counter,
2054 WPA_REPLAY_COUNTER_LEN);
2055
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002056 key_mic = (u8 *) (reply + 1);
Hai Shalom74f70d42019-02-11 14:42:39 -08002057 WPA_PUT_BE16(key_mic + mic_len, kde_len); /* Key Data Length */
2058
2059#ifdef CONFIG_OCV
2060 if (wpa_sm_ocv_enabled(sm)) {
2061 struct wpa_channel_info ci;
2062 u8 *pos;
2063
2064 if (wpa_sm_channel_info(sm, &ci) != 0) {
2065 wpa_printf(MSG_WARNING,
2066 "Failed to get channel info for OCI element in EAPOL-Key 2/2");
2067 os_free(rbuf);
2068 return -1;
2069 }
Hai Shalom899fcc72020-10-19 14:38:18 -07002070#ifdef CONFIG_TESTING_OPTIONS
2071 if (sm->oci_freq_override_eapol_g2) {
2072 wpa_printf(MSG_INFO,
2073 "TEST: Override OCI KDE frequency %d -> %d MHz",
2074 ci.frequency,
2075 sm->oci_freq_override_eapol_g2);
2076 ci.frequency = sm->oci_freq_override_eapol_g2;
2077 }
2078#endif /* CONFIG_TESTING_OPTIONS */
Hai Shalom74f70d42019-02-11 14:42:39 -08002079
2080 pos = key_mic + mic_len + 2; /* Key Data */
2081 if (ocv_insert_oci_kde(&ci, &pos) < 0) {
2082 os_free(rbuf);
2083 return -1;
2084 }
2085 }
2086#endif /* CONFIG_OCV */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002087
2088 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Sending EAPOL-Key 2/2");
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002089 return wpa_eapol_key_send(sm, &sm->ptk, ver, sm->bssid, ETH_P_EAPOL,
2090 rbuf, rlen, key_mic);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002091}
2092
2093
2094static void wpa_supplicant_process_1_of_2(struct wpa_sm *sm,
2095 const unsigned char *src_addr,
2096 const struct wpa_eapol_key *key,
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002097 const u8 *key_data,
2098 size_t key_data_len, u16 ver)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002099{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002100 u16 key_info;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002101 int rekey, ret;
2102 struct wpa_gtk_data gd;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002103 const u8 *key_rsc;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002104
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002105 if (!sm->msg_3_of_4_ok && !wpa_fils_is_completed(sm)) {
Dmitry Shmidt4dd28dc2015-03-10 11:21:43 -07002106 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2107 "WPA: Group Key Handshake started prior to completion of 4-way handshake");
2108 goto failed;
2109 }
2110
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002111 os_memset(&gd, 0, sizeof(gd));
2112
2113 rekey = wpa_sm_get_state(sm) == WPA_COMPLETED;
2114 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: RX message 1 of Group Key "
2115 "Handshake from " MACSTR " (ver=%d)", MAC2STR(src_addr), ver);
2116
2117 key_info = WPA_GET_BE16(key->key_info);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002118
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002119 if (sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN) {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002120 ret = wpa_supplicant_process_1_of_2_rsn(sm, key_data,
2121 key_data_len, key_info,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002122 &gd);
2123 } else {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002124 ret = wpa_supplicant_process_1_of_2_wpa(sm, key, key_data,
2125 key_data_len,
2126 key_info, ver, &gd);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002127 }
2128
2129 wpa_sm_set_state(sm, WPA_GROUP_HANDSHAKE);
2130
2131 if (ret)
2132 goto failed;
2133
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002134 key_rsc = key->key_rsc;
2135 if (wpa_supplicant_rsc_relaxation(sm, key->key_rsc))
2136 key_rsc = null_rsc;
2137
Jouni Malinen58c0e962017-10-01 12:12:24 +03002138 if (wpa_supplicant_install_gtk(sm, &gd, key_rsc, 0) ||
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002139 wpa_supplicant_send_2_of_2(sm, key, ver, key_info) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002140 goto failed;
Hai Shalom81f62d82019-07-22 12:10:00 -07002141 forced_memzero(&gd, sizeof(gd));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002142
2143 if (rekey) {
2144 wpa_msg(sm->ctx->msg_ctx, MSG_INFO, "WPA: Group rekeying "
2145 "completed with " MACSTR " [GTK=%s]",
2146 MAC2STR(sm->bssid), wpa_cipher_txt(sm->group_cipher));
2147 wpa_sm_cancel_auth_timeout(sm);
2148 wpa_sm_set_state(sm, WPA_COMPLETED);
2149 } else {
2150 wpa_supplicant_key_neg_complete(sm, sm->bssid,
2151 key_info &
2152 WPA_KEY_INFO_SECURE);
2153 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002154
2155 wpa_sm_set_rekey_offload(sm);
2156
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002157 return;
2158
2159failed:
Hai Shalom81f62d82019-07-22 12:10:00 -07002160 forced_memzero(&gd, sizeof(gd));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002161 wpa_sm_deauthenticate(sm, WLAN_REASON_UNSPECIFIED);
2162}
2163
2164
2165static int wpa_supplicant_verify_eapol_key_mic(struct wpa_sm *sm,
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002166 struct wpa_eapol_key *key,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002167 u16 ver,
2168 const u8 *buf, size_t len)
2169{
Dmitry Shmidt807291d2015-01-27 13:40:23 -08002170 u8 mic[WPA_EAPOL_KEY_MIC_MAX_LEN];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002171 int ok = 0;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002172 size_t mic_len = wpa_mic_len(sm->key_mgmt, sm->pmk_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002173
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002174 os_memcpy(mic, key + 1, mic_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002175 if (sm->tptk_set) {
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002176 os_memset(key + 1, 0, mic_len);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002177 if (wpa_eapol_key_mic(sm->tptk.kck, sm->tptk.kck_len,
2178 sm->key_mgmt,
2179 ver, buf, len, (u8 *) (key + 1)) < 0 ||
2180 os_memcmp_const(mic, key + 1, mic_len) != 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002181 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
2182 "WPA: Invalid EAPOL-Key MIC "
2183 "when using TPTK - ignoring TPTK");
Hai Shalom74f70d42019-02-11 14:42:39 -08002184#ifdef TEST_FUZZ
2185 wpa_printf(MSG_INFO,
2186 "TEST: Ignore Key MIC failure for fuzz testing");
2187 goto continue_fuzz;
2188#endif /* TEST_FUZZ */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002189 } else {
Hai Shalom74f70d42019-02-11 14:42:39 -08002190#ifdef TEST_FUZZ
2191 continue_fuzz:
2192#endif /* TEST_FUZZ */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002193 ok = 1;
2194 sm->tptk_set = 0;
2195 sm->ptk_set = 1;
2196 os_memcpy(&sm->ptk, &sm->tptk, sizeof(sm->ptk));
Dmitry Shmidt61593f02014-04-21 16:27:35 -07002197 os_memset(&sm->tptk, 0, sizeof(sm->tptk));
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002198 /*
2199 * This assures the same TPTK in sm->tptk can never be
Roshan Pius3a1667e2018-07-03 15:17:14 -07002200 * copied twice to sm->ptk as the new PTK. In
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002201 * combination with the installed flag in the wpa_ptk
2202 * struct, this assures the same PTK is only installed
2203 * once.
2204 */
2205 sm->renew_snonce = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002206 }
2207 }
2208
2209 if (!ok && sm->ptk_set) {
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002210 os_memset(key + 1, 0, mic_len);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002211 if (wpa_eapol_key_mic(sm->ptk.kck, sm->ptk.kck_len,
2212 sm->key_mgmt,
2213 ver, buf, len, (u8 *) (key + 1)) < 0 ||
2214 os_memcmp_const(mic, key + 1, mic_len) != 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002215 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
2216 "WPA: Invalid EAPOL-Key MIC - "
2217 "dropping packet");
Hai Shalom74f70d42019-02-11 14:42:39 -08002218#ifdef TEST_FUZZ
2219 wpa_printf(MSG_INFO,
2220 "TEST: Ignore Key MIC failure for fuzz testing");
2221 goto continue_fuzz2;
2222#endif /* TEST_FUZZ */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002223 return -1;
2224 }
Hai Shalom74f70d42019-02-11 14:42:39 -08002225#ifdef TEST_FUZZ
2226 continue_fuzz2:
2227#endif /* TEST_FUZZ */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002228 ok = 1;
2229 }
2230
2231 if (!ok) {
2232 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
2233 "WPA: Could not verify EAPOL-Key MIC - "
2234 "dropping packet");
2235 return -1;
2236 }
2237
2238 os_memcpy(sm->rx_replay_counter, key->replay_counter,
2239 WPA_REPLAY_COUNTER_LEN);
2240 sm->rx_replay_counter_set = 1;
2241 return 0;
2242}
2243
2244
2245/* Decrypt RSN EAPOL-Key key data (RC4 or AES-WRAP) */
2246static int wpa_supplicant_decrypt_key_data(struct wpa_sm *sm,
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002247 struct wpa_eapol_key *key,
2248 size_t mic_len, u16 ver,
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002249 u8 *key_data, size_t *key_data_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002250{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002251 wpa_hexdump(MSG_DEBUG, "RSN: encrypted key data",
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002252 key_data, *key_data_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002253 if (!sm->ptk_set) {
2254 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
2255 "WPA: PTK not available, cannot decrypt EAPOL-Key Key "
2256 "Data");
2257 return -1;
2258 }
2259
2260 /* Decrypt key data here so that this operation does not need
2261 * to be implemented separately for each message type. */
Dmitry Shmidt807291d2015-01-27 13:40:23 -08002262 if (ver == WPA_KEY_INFO_TYPE_HMAC_MD5_RC4 && sm->ptk.kek_len == 16) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002263#ifdef CONFIG_NO_RC4
2264 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
2265 "WPA: RC4 not supported in the build");
2266 return -1;
2267#else /* CONFIG_NO_RC4 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002268 u8 ek[32];
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002269
2270 wpa_printf(MSG_DEBUG, "WPA: Decrypt Key Data using RC4");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002271 os_memcpy(ek, key->key_iv, 16);
Dmitry Shmidt807291d2015-01-27 13:40:23 -08002272 os_memcpy(ek + 16, sm->ptk.kek, sm->ptk.kek_len);
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002273 if (rc4_skip(ek, 32, 256, key_data, *key_data_len)) {
Hai Shalom81f62d82019-07-22 12:10:00 -07002274 forced_memzero(ek, sizeof(ek));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002275 wpa_msg(sm->ctx->msg_ctx, MSG_ERROR,
2276 "WPA: RC4 failed");
2277 return -1;
2278 }
Hai Shalom81f62d82019-07-22 12:10:00 -07002279 forced_memzero(ek, sizeof(ek));
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002280#endif /* CONFIG_NO_RC4 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002281 } else if (ver == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES ||
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002282 ver == WPA_KEY_INFO_TYPE_AES_128_CMAC ||
Roshan Pius3a1667e2018-07-03 15:17:14 -07002283 wpa_use_aes_key_wrap(sm->key_mgmt)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002284 u8 *buf;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002285
2286 wpa_printf(MSG_DEBUG,
2287 "WPA: Decrypt Key Data using AES-UNWRAP (KEK length %u)",
2288 (unsigned int) sm->ptk.kek_len);
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002289 if (*key_data_len < 8 || *key_data_len % 8) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002290 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002291 "WPA: Unsupported AES-WRAP len %u",
2292 (unsigned int) *key_data_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002293 return -1;
2294 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002295 *key_data_len -= 8; /* AES-WRAP adds 8 bytes */
2296 buf = os_malloc(*key_data_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002297 if (buf == NULL) {
2298 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
2299 "WPA: No memory for AES-UNWRAP buffer");
2300 return -1;
2301 }
Hai Shalom74f70d42019-02-11 14:42:39 -08002302#ifdef TEST_FUZZ
2303 os_memset(buf, 0x11, *key_data_len);
2304#endif /* TEST_FUZZ */
Dmitry Shmidt807291d2015-01-27 13:40:23 -08002305 if (aes_unwrap(sm->ptk.kek, sm->ptk.kek_len, *key_data_len / 8,
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002306 key_data, buf)) {
Hai Shalom74f70d42019-02-11 14:42:39 -08002307#ifdef TEST_FUZZ
2308 wpa_printf(MSG_INFO,
2309 "TEST: Ignore AES unwrap failure for fuzz testing");
2310 goto continue_fuzz;
2311#endif /* TEST_FUZZ */
Dmitry Shmidt7d56b752015-12-22 10:59:44 -08002312 bin_clear_free(buf, *key_data_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002313 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
2314 "WPA: AES unwrap failed - "
2315 "could not decrypt EAPOL-Key key data");
2316 return -1;
2317 }
Hai Shalom74f70d42019-02-11 14:42:39 -08002318#ifdef TEST_FUZZ
2319 continue_fuzz:
2320#endif /* TEST_FUZZ */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002321 os_memcpy(key_data, buf, *key_data_len);
Dmitry Shmidt7d56b752015-12-22 10:59:44 -08002322 bin_clear_free(buf, *key_data_len);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002323 WPA_PUT_BE16(((u8 *) (key + 1)) + mic_len, *key_data_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002324 } else {
2325 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
2326 "WPA: Unsupported key_info type %d", ver);
2327 return -1;
2328 }
2329 wpa_hexdump_key(MSG_DEBUG, "WPA: decrypted EAPOL-Key key data",
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002330 key_data, *key_data_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002331 return 0;
2332}
2333
2334
2335/**
2336 * wpa_sm_aborted_cached - Notify WPA that PMKSA caching was aborted
2337 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2338 */
2339void wpa_sm_aborted_cached(struct wpa_sm *sm)
2340{
2341 if (sm && sm->cur_pmksa) {
2342 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2343 "RSN: Cancelling PMKSA caching attempt");
2344 sm->cur_pmksa = NULL;
2345 }
2346}
2347
2348
Hai Shalomc1a21442022-02-04 13:43:00 -08002349void wpa_sm_aborted_external_cached(struct wpa_sm *sm)
2350{
2351 if (sm && sm->cur_pmksa && sm->cur_pmksa->external) {
2352 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2353 "RSN: Cancelling external PMKSA caching attempt");
2354 sm->cur_pmksa = NULL;
2355 }
2356}
2357
2358
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002359static void wpa_eapol_key_dump(struct wpa_sm *sm,
Dmitry Shmidt807291d2015-01-27 13:40:23 -08002360 const struct wpa_eapol_key *key,
2361 unsigned int key_data_len,
2362 const u8 *mic, unsigned int mic_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002363{
2364#ifndef CONFIG_NO_STDOUT_DEBUG
2365 u16 key_info = WPA_GET_BE16(key->key_info);
2366
2367 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, " EAPOL-Key type=%d", key->type);
2368 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2369 " key_info 0x%x (ver=%d keyidx=%d rsvd=%d %s%s%s%s%s%s%s%s)",
2370 key_info, key_info & WPA_KEY_INFO_TYPE_MASK,
2371 (key_info & WPA_KEY_INFO_KEY_INDEX_MASK) >>
2372 WPA_KEY_INFO_KEY_INDEX_SHIFT,
2373 (key_info & (BIT(13) | BIT(14) | BIT(15))) >> 13,
2374 key_info & WPA_KEY_INFO_KEY_TYPE ? "Pairwise" : "Group",
2375 key_info & WPA_KEY_INFO_INSTALL ? " Install" : "",
2376 key_info & WPA_KEY_INFO_ACK ? " Ack" : "",
2377 key_info & WPA_KEY_INFO_MIC ? " MIC" : "",
2378 key_info & WPA_KEY_INFO_SECURE ? " Secure" : "",
2379 key_info & WPA_KEY_INFO_ERROR ? " Error" : "",
2380 key_info & WPA_KEY_INFO_REQUEST ? " Request" : "",
2381 key_info & WPA_KEY_INFO_ENCR_KEY_DATA ? " Encr" : "");
2382 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2383 " key_length=%u key_data_length=%u",
Dmitry Shmidt807291d2015-01-27 13:40:23 -08002384 WPA_GET_BE16(key->key_length), key_data_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002385 wpa_hexdump(MSG_DEBUG, " replay_counter",
2386 key->replay_counter, WPA_REPLAY_COUNTER_LEN);
2387 wpa_hexdump(MSG_DEBUG, " key_nonce", key->key_nonce, WPA_NONCE_LEN);
2388 wpa_hexdump(MSG_DEBUG, " key_iv", key->key_iv, 16);
2389 wpa_hexdump(MSG_DEBUG, " key_rsc", key->key_rsc, 8);
2390 wpa_hexdump(MSG_DEBUG, " key_id (reserved)", key->key_id, 8);
Dmitry Shmidt807291d2015-01-27 13:40:23 -08002391 wpa_hexdump(MSG_DEBUG, " key_mic", mic, mic_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002392#endif /* CONFIG_NO_STDOUT_DEBUG */
2393}
2394
2395
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002396#ifdef CONFIG_FILS
2397static int wpa_supp_aead_decrypt(struct wpa_sm *sm, u8 *buf, size_t buf_len,
2398 size_t *key_data_len)
2399{
2400 struct wpa_ptk *ptk;
2401 struct ieee802_1x_hdr *hdr;
2402 struct wpa_eapol_key *key;
2403 u8 *pos, *tmp;
2404 const u8 *aad[1];
2405 size_t aad_len[1];
2406
2407 if (*key_data_len < AES_BLOCK_SIZE) {
2408 wpa_printf(MSG_INFO, "No room for AES-SIV data in the frame");
2409 return -1;
2410 }
2411
2412 if (sm->tptk_set)
2413 ptk = &sm->tptk;
2414 else if (sm->ptk_set)
2415 ptk = &sm->ptk;
2416 else
2417 return -1;
2418
2419 hdr = (struct ieee802_1x_hdr *) buf;
2420 key = (struct wpa_eapol_key *) (hdr + 1);
2421 pos = (u8 *) (key + 1);
2422 pos += 2; /* Pointing at the Encrypted Key Data field */
2423
2424 tmp = os_malloc(*key_data_len);
2425 if (!tmp)
2426 return -1;
2427
2428 /* AES-SIV AAD from EAPOL protocol version field (inclusive) to
2429 * to Key Data (exclusive). */
2430 aad[0] = buf;
2431 aad_len[0] = pos - buf;
2432 if (aes_siv_decrypt(ptk->kek, ptk->kek_len, pos, *key_data_len,
2433 1, aad, aad_len, tmp) < 0) {
2434 wpa_printf(MSG_INFO, "Invalid AES-SIV data in the frame");
2435 bin_clear_free(tmp, *key_data_len);
2436 return -1;
2437 }
2438
2439 /* AEAD decryption and validation completed successfully */
2440 (*key_data_len) -= AES_BLOCK_SIZE;
2441 wpa_hexdump_key(MSG_DEBUG, "WPA: Decrypted Key Data",
2442 tmp, *key_data_len);
2443
2444 /* Replace Key Data field with the decrypted version */
2445 os_memcpy(pos, tmp, *key_data_len);
2446 pos -= 2; /* Key Data Length field */
2447 WPA_PUT_BE16(pos, *key_data_len);
2448 bin_clear_free(tmp, *key_data_len);
2449
2450 if (sm->tptk_set) {
2451 sm->tptk_set = 0;
2452 sm->ptk_set = 1;
2453 os_memcpy(&sm->ptk, &sm->tptk, sizeof(sm->ptk));
2454 os_memset(&sm->tptk, 0, sizeof(sm->tptk));
2455 }
2456
2457 os_memcpy(sm->rx_replay_counter, key->replay_counter,
2458 WPA_REPLAY_COUNTER_LEN);
2459 sm->rx_replay_counter_set = 1;
2460
2461 return 0;
2462}
2463#endif /* CONFIG_FILS */
2464
2465
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002466/**
2467 * wpa_sm_rx_eapol - Process received WPA EAPOL frames
2468 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2469 * @src_addr: Source MAC address of the EAPOL packet
2470 * @buf: Pointer to the beginning of the EAPOL data (EAPOL header)
2471 * @len: Length of the EAPOL frame
2472 * Returns: 1 = WPA EAPOL-Key processed, 0 = not a WPA EAPOL-Key, -1 failure
2473 *
2474 * This function is called for each received EAPOL frame. Other than EAPOL-Key
2475 * frames can be skipped if filtering is done elsewhere. wpa_sm_rx_eapol() is
2476 * only processing WPA and WPA2 EAPOL-Key frames.
2477 *
2478 * The received EAPOL-Key packets are validated and valid packets are replied
2479 * to. In addition, key material (PTK, GTK) is configured at the end of a
2480 * successful key handshake.
2481 */
2482int wpa_sm_rx_eapol(struct wpa_sm *sm, const u8 *src_addr,
2483 const u8 *buf, size_t len)
2484{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002485 size_t plen, data_len, key_data_len;
2486 const struct ieee802_1x_hdr *hdr;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002487 struct wpa_eapol_key *key;
2488 u16 key_info, ver;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002489 u8 *tmp = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002490 int ret = -1;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002491 u8 *mic, *key_data;
Hai Shalom899fcc72020-10-19 14:38:18 -07002492 size_t mic_len, keyhdrlen, pmk_len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002493
2494#ifdef CONFIG_IEEE80211R
2495 sm->ft_completed = 0;
2496#endif /* CONFIG_IEEE80211R */
2497
Hai Shalom899fcc72020-10-19 14:38:18 -07002498 pmk_len = sm->pmk_len;
2499 if (!pmk_len && sm->cur_pmksa)
2500 pmk_len = sm->cur_pmksa->pmk_len;
2501 mic_len = wpa_mic_len(sm->key_mgmt, pmk_len);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002502 keyhdrlen = sizeof(*key) + mic_len + 2;
Dmitry Shmidt807291d2015-01-27 13:40:23 -08002503
2504 if (len < sizeof(*hdr) + keyhdrlen) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002505 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2506 "WPA: EAPOL frame too short to be a WPA "
2507 "EAPOL-Key (len %lu, expecting at least %lu)",
2508 (unsigned long) len,
Dmitry Shmidt807291d2015-01-27 13:40:23 -08002509 (unsigned long) sizeof(*hdr) + keyhdrlen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002510 return 0;
2511 }
2512
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002513 hdr = (const struct ieee802_1x_hdr *) buf;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002514 plen = be_to_host16(hdr->length);
2515 data_len = plen + sizeof(*hdr);
2516 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2517 "IEEE 802.1X RX: version=%d type=%d length=%lu",
2518 hdr->version, hdr->type, (unsigned long) plen);
2519
2520 if (hdr->version < EAPOL_VERSION) {
2521 /* TODO: backwards compatibility */
2522 }
2523 if (hdr->type != IEEE802_1X_TYPE_EAPOL_KEY) {
2524 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2525 "WPA: EAPOL frame (type %u) discarded, "
2526 "not a Key frame", hdr->type);
2527 ret = 0;
2528 goto out;
2529 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002530 wpa_hexdump(MSG_MSGDUMP, "WPA: RX EAPOL-Key", buf, len);
Dmitry Shmidt807291d2015-01-27 13:40:23 -08002531 if (plen > len - sizeof(*hdr) || plen < keyhdrlen) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002532 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2533 "WPA: EAPOL frame payload size %lu "
2534 "invalid (frame size %lu)",
2535 (unsigned long) plen, (unsigned long) len);
2536 ret = 0;
2537 goto out;
2538 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002539 if (data_len < len) {
2540 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2541 "WPA: ignoring %lu bytes after the IEEE 802.1X data",
2542 (unsigned long) len - data_len);
2543 }
2544
2545 /*
2546 * Make a copy of the frame since we need to modify the buffer during
2547 * MAC validation and Key Data decryption.
2548 */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002549 tmp = os_memdup(buf, data_len);
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002550 if (tmp == NULL)
2551 goto out;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002552 key = (struct wpa_eapol_key *) (tmp + sizeof(struct ieee802_1x_hdr));
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002553 mic = (u8 *) (key + 1);
2554 key_data = mic + mic_len + 2;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002555
2556 if (key->type != EAPOL_KEY_TYPE_WPA && key->type != EAPOL_KEY_TYPE_RSN)
2557 {
2558 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2559 "WPA: EAPOL-Key type (%d) unknown, discarded",
2560 key->type);
2561 ret = 0;
2562 goto out;
2563 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002564
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002565 key_data_len = WPA_GET_BE16(mic + mic_len);
2566 wpa_eapol_key_dump(sm, key, key_data_len, mic, mic_len);
Dmitry Shmidt807291d2015-01-27 13:40:23 -08002567
2568 if (key_data_len > plen - keyhdrlen) {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002569 wpa_msg(sm->ctx->msg_ctx, MSG_INFO, "WPA: Invalid EAPOL-Key "
2570 "frame - key_data overflow (%u > %u)",
2571 (unsigned int) key_data_len,
Dmitry Shmidt807291d2015-01-27 13:40:23 -08002572 (unsigned int) (plen - keyhdrlen));
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002573 goto out;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002574 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002575
2576 eapol_sm_notify_lower_layer_success(sm->eapol, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002577 key_info = WPA_GET_BE16(key->key_info);
2578 ver = key_info & WPA_KEY_INFO_TYPE_MASK;
2579 if (ver != WPA_KEY_INFO_TYPE_HMAC_MD5_RC4 &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002580 ver != WPA_KEY_INFO_TYPE_AES_128_CMAC &&
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002581 ver != WPA_KEY_INFO_TYPE_HMAC_SHA1_AES &&
Roshan Pius3a1667e2018-07-03 15:17:14 -07002582 !wpa_use_akm_defined(sm->key_mgmt)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002583 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2584 "WPA: Unsupported EAPOL-Key descriptor version %d",
2585 ver);
2586 goto out;
2587 }
2588
Roshan Pius3a1667e2018-07-03 15:17:14 -07002589 if (wpa_use_akm_defined(sm->key_mgmt) &&
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002590 ver != WPA_KEY_INFO_TYPE_AKM_DEFINED) {
2591 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2592 "RSN: Unsupported EAPOL-Key descriptor version %d (expected AKM defined = 0)",
2593 ver);
2594 goto out;
2595 }
2596
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002597#ifdef CONFIG_IEEE80211R
2598 if (wpa_key_mgmt_ft(sm->key_mgmt)) {
2599 /* IEEE 802.11r uses a new key_info type (AES-128-CMAC). */
Roshan Pius3a1667e2018-07-03 15:17:14 -07002600 if (ver != WPA_KEY_INFO_TYPE_AES_128_CMAC &&
2601 !wpa_use_akm_defined(sm->key_mgmt)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002602 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2603 "FT: AP did not use AES-128-CMAC");
2604 goto out;
2605 }
2606 } else
2607#endif /* CONFIG_IEEE80211R */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002608 if (wpa_key_mgmt_sha256(sm->key_mgmt)) {
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002609 if (ver != WPA_KEY_INFO_TYPE_AES_128_CMAC &&
Roshan Pius3a1667e2018-07-03 15:17:14 -07002610 !wpa_use_akm_defined(sm->key_mgmt)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002611 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2612 "WPA: AP did not use the "
2613 "negotiated AES-128-CMAC");
2614 goto out;
2615 }
Hai Shalomc3565922019-10-28 11:58:20 -07002616 } else if (sm->pairwise_cipher == WPA_CIPHER_CCMP &&
2617 !wpa_use_akm_defined(sm->key_mgmt) &&
2618 ver != WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002619 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2620 "WPA: CCMP is used, but EAPOL-Key "
2621 "descriptor version (%d) is not 2", ver);
2622 if (sm->group_cipher != WPA_CIPHER_CCMP &&
2623 !(key_info & WPA_KEY_INFO_KEY_TYPE)) {
2624 /* Earlier versions of IEEE 802.11i did not explicitly
2625 * require version 2 descriptor for all EAPOL-Key
2626 * packets, so allow group keys to use version 1 if
2627 * CCMP is not used for them. */
2628 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2629 "WPA: Backwards compatibility: allow invalid "
2630 "version for non-CCMP group keys");
Jouni Malinen658fb4a2014-11-14 20:57:05 +02002631 } else if (ver == WPA_KEY_INFO_TYPE_AES_128_CMAC) {
2632 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2633 "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 -07002634 } else
2635 goto out;
Dmitry Shmidt71757432014-06-02 13:50:35 -07002636 } else if (sm->pairwise_cipher == WPA_CIPHER_GCMP &&
Roshan Pius3a1667e2018-07-03 15:17:14 -07002637 !wpa_use_akm_defined(sm->key_mgmt) &&
Dmitry Shmidt71757432014-06-02 13:50:35 -07002638 ver != WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002639 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2640 "WPA: GCMP is used, but EAPOL-Key "
2641 "descriptor version (%d) is not 2", ver);
2642 goto out;
2643 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002644
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002645 if (sm->rx_replay_counter_set &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002646 os_memcmp(key->replay_counter, sm->rx_replay_counter,
2647 WPA_REPLAY_COUNTER_LEN) <= 0) {
2648 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
2649 "WPA: EAPOL-Key Replay Counter did not increase - "
2650 "dropping packet");
2651 goto out;
2652 }
2653
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002654 if (key_info & WPA_KEY_INFO_SMK_MESSAGE) {
2655 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2656 "WPA: Unsupported SMK bit in key_info");
2657 goto out;
2658 }
2659
2660 if (!(key_info & WPA_KEY_INFO_ACK)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002661 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2662 "WPA: No Ack bit in key_info");
2663 goto out;
2664 }
2665
2666 if (key_info & WPA_KEY_INFO_REQUEST) {
2667 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2668 "WPA: EAPOL-Key with Request bit - dropped");
2669 goto out;
2670 }
2671
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002672 if ((key_info & WPA_KEY_INFO_MIC) &&
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002673 wpa_supplicant_verify_eapol_key_mic(sm, key, ver, tmp, data_len))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002674 goto out;
2675
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002676#ifdef CONFIG_FILS
2677 if (!mic_len && (key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
2678 if (wpa_supp_aead_decrypt(sm, tmp, data_len, &key_data_len))
2679 goto out;
2680 }
2681#endif /* CONFIG_FILS */
2682
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002683 if ((sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN) &&
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002684 (key_info & WPA_KEY_INFO_ENCR_KEY_DATA) && mic_len) {
Hai Shalomce48b4a2018-09-05 11:41:35 -07002685 /*
2686 * Only decrypt the Key Data field if the frame's authenticity
2687 * was verified. When using AES-SIV (FILS), the MIC flag is not
2688 * set, so this check should only be performed if mic_len != 0
2689 * which is the case in this code branch.
2690 */
2691 if (!(key_info & WPA_KEY_INFO_MIC)) {
2692 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
2693 "WPA: Ignore EAPOL-Key with encrypted but unauthenticated data");
2694 goto out;
2695 }
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002696 if (wpa_supplicant_decrypt_key_data(sm, key, mic_len,
2697 ver, key_data,
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002698 &key_data_len))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002699 goto out;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002700 }
2701
2702 if (key_info & WPA_KEY_INFO_KEY_TYPE) {
2703 if (key_info & WPA_KEY_INFO_KEY_INDEX_MASK) {
2704 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
2705 "WPA: Ignored EAPOL-Key (Pairwise) with "
2706 "non-zero key index");
2707 goto out;
2708 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002709 if (key_info & (WPA_KEY_INFO_MIC |
2710 WPA_KEY_INFO_ENCR_KEY_DATA)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002711 /* 3/4 4-Way Handshake */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002712 wpa_supplicant_process_3_of_4(sm, key, ver, key_data,
2713 key_data_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002714 } else {
2715 /* 1/4 4-Way Handshake */
2716 wpa_supplicant_process_1_of_4(sm, src_addr, key,
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002717 ver, key_data,
2718 key_data_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002719 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002720 } else {
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002721 if ((mic_len && (key_info & WPA_KEY_INFO_MIC)) ||
2722 (!mic_len && (key_info & WPA_KEY_INFO_ENCR_KEY_DATA))) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002723 /* 1/2 Group Key Handshake */
2724 wpa_supplicant_process_1_of_2(sm, src_addr, key,
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002725 key_data, key_data_len,
2726 ver);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002727 } else {
2728 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002729 "WPA: EAPOL-Key (Group) without Mic/Encr bit - "
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002730 "dropped");
2731 }
2732 }
2733
2734 ret = 1;
2735
2736out:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002737 bin_clear_free(tmp, data_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002738 return ret;
2739}
2740
2741
2742#ifdef CONFIG_CTRL_IFACE
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002743static u32 wpa_key_mgmt_suite(struct wpa_sm *sm)
2744{
2745 switch (sm->key_mgmt) {
2746 case WPA_KEY_MGMT_IEEE8021X:
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002747 return ((sm->proto == WPA_PROTO_RSN ||
2748 sm->proto == WPA_PROTO_OSEN) ?
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002749 RSN_AUTH_KEY_MGMT_UNSPEC_802_1X :
2750 WPA_AUTH_KEY_MGMT_UNSPEC_802_1X);
2751 case WPA_KEY_MGMT_PSK:
2752 return (sm->proto == WPA_PROTO_RSN ?
2753 RSN_AUTH_KEY_MGMT_PSK_OVER_802_1X :
2754 WPA_AUTH_KEY_MGMT_PSK_OVER_802_1X);
2755#ifdef CONFIG_IEEE80211R
2756 case WPA_KEY_MGMT_FT_IEEE8021X:
2757 return RSN_AUTH_KEY_MGMT_FT_802_1X;
2758 case WPA_KEY_MGMT_FT_PSK:
2759 return RSN_AUTH_KEY_MGMT_FT_PSK;
2760#endif /* CONFIG_IEEE80211R */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002761 case WPA_KEY_MGMT_IEEE8021X_SHA256:
2762 return RSN_AUTH_KEY_MGMT_802_1X_SHA256;
2763 case WPA_KEY_MGMT_PSK_SHA256:
2764 return RSN_AUTH_KEY_MGMT_PSK_SHA256;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002765 case WPA_KEY_MGMT_CCKM:
2766 return (sm->proto == WPA_PROTO_RSN ?
2767 RSN_AUTH_KEY_MGMT_CCKM:
2768 WPA_AUTH_KEY_MGMT_CCKM);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002769 case WPA_KEY_MGMT_WPA_NONE:
2770 return WPA_AUTH_KEY_MGMT_NONE;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002771 case WPA_KEY_MGMT_IEEE8021X_SUITE_B:
2772 return RSN_AUTH_KEY_MGMT_802_1X_SUITE_B;
Dmitry Shmidt807291d2015-01-27 13:40:23 -08002773 case WPA_KEY_MGMT_IEEE8021X_SUITE_B_192:
2774 return RSN_AUTH_KEY_MGMT_802_1X_SUITE_B_192;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002775 default:
2776 return 0;
2777 }
2778}
2779
2780
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002781#define RSN_SUITE "%02x-%02x-%02x-%d"
2782#define RSN_SUITE_ARG(s) \
2783((s) >> 24) & 0xff, ((s) >> 16) & 0xff, ((s) >> 8) & 0xff, (s) & 0xff
2784
2785/**
2786 * wpa_sm_get_mib - Dump text list of MIB entries
2787 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2788 * @buf: Buffer for the list
2789 * @buflen: Length of the buffer
2790 * Returns: Number of bytes written to buffer
2791 *
2792 * This function is used fetch dot11 MIB variables.
2793 */
2794int wpa_sm_get_mib(struct wpa_sm *sm, char *buf, size_t buflen)
2795{
2796 char pmkid_txt[PMKID_LEN * 2 + 1];
Hai Shalome21d4e82020-04-29 16:34:06 -07002797 bool rsna;
2798 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002799 size_t len;
2800
2801 if (sm->cur_pmksa) {
2802 wpa_snprintf_hex(pmkid_txt, sizeof(pmkid_txt),
2803 sm->cur_pmksa->pmkid, PMKID_LEN);
2804 } else
2805 pmkid_txt[0] = '\0';
2806
Hai Shalome21d4e82020-04-29 16:34:06 -07002807 rsna = (wpa_key_mgmt_wpa_psk(sm->key_mgmt) ||
2808 wpa_key_mgmt_wpa_ieee8021x(sm->key_mgmt)) &&
2809 sm->proto == WPA_PROTO_RSN;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002810
2811 ret = os_snprintf(buf, buflen,
2812 "dot11RSNAOptionImplemented=TRUE\n"
2813 "dot11RSNAPreauthenticationImplemented=TRUE\n"
2814 "dot11RSNAEnabled=%s\n"
2815 "dot11RSNAPreauthenticationEnabled=%s\n"
2816 "dot11RSNAConfigVersion=%d\n"
2817 "dot11RSNAConfigPairwiseKeysSupported=5\n"
2818 "dot11RSNAConfigGroupCipherSize=%d\n"
2819 "dot11RSNAConfigPMKLifetime=%d\n"
2820 "dot11RSNAConfigPMKReauthThreshold=%d\n"
2821 "dot11RSNAConfigNumberOfPTKSAReplayCounters=1\n"
2822 "dot11RSNAConfigSATimeout=%d\n",
2823 rsna ? "TRUE" : "FALSE",
2824 rsna ? "TRUE" : "FALSE",
2825 RSN_VERSION,
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07002826 wpa_cipher_key_len(sm->group_cipher) * 8,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002827 sm->dot11RSNAConfigPMKLifetime,
2828 sm->dot11RSNAConfigPMKReauthThreshold,
2829 sm->dot11RSNAConfigSATimeout);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002830 if (os_snprintf_error(buflen, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002831 return 0;
2832 len = ret;
2833
2834 ret = os_snprintf(
2835 buf + len, buflen - len,
2836 "dot11RSNAAuthenticationSuiteSelected=" RSN_SUITE "\n"
2837 "dot11RSNAPairwiseCipherSelected=" RSN_SUITE "\n"
2838 "dot11RSNAGroupCipherSelected=" RSN_SUITE "\n"
2839 "dot11RSNAPMKIDUsed=%s\n"
2840 "dot11RSNAAuthenticationSuiteRequested=" RSN_SUITE "\n"
2841 "dot11RSNAPairwiseCipherRequested=" RSN_SUITE "\n"
2842 "dot11RSNAGroupCipherRequested=" RSN_SUITE "\n"
2843 "dot11RSNAConfigNumberOfGTKSAReplayCounters=0\n"
2844 "dot11RSNA4WayHandshakeFailures=%u\n",
2845 RSN_SUITE_ARG(wpa_key_mgmt_suite(sm)),
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07002846 RSN_SUITE_ARG(wpa_cipher_to_suite(sm->proto,
2847 sm->pairwise_cipher)),
2848 RSN_SUITE_ARG(wpa_cipher_to_suite(sm->proto,
2849 sm->group_cipher)),
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002850 pmkid_txt,
2851 RSN_SUITE_ARG(wpa_key_mgmt_suite(sm)),
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07002852 RSN_SUITE_ARG(wpa_cipher_to_suite(sm->proto,
2853 sm->pairwise_cipher)),
2854 RSN_SUITE_ARG(wpa_cipher_to_suite(sm->proto,
2855 sm->group_cipher)),
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002856 sm->dot11RSNA4WayHandshakeFailures);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002857 if (!os_snprintf_error(buflen - len, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002858 len += ret;
2859
2860 return (int) len;
2861}
2862#endif /* CONFIG_CTRL_IFACE */
2863
2864
2865static void wpa_sm_pmksa_free_cb(struct rsn_pmksa_cache_entry *entry,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002866 void *ctx, enum pmksa_free_reason reason)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002867{
2868 struct wpa_sm *sm = ctx;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002869 int deauth = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002870
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002871 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "RSN: PMKSA cache entry free_cb: "
2872 MACSTR " reason=%d", MAC2STR(entry->aa), reason);
2873
2874 if (sm->cur_pmksa == entry) {
2875 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2876 "RSN: %s current PMKSA entry",
2877 reason == PMKSA_REPLACE ? "replaced" : "removed");
2878 pmksa_cache_clear_current(sm);
2879
2880 /*
2881 * If an entry is simply being replaced, there's no need to
2882 * deauthenticate because it will be immediately re-added.
2883 * This happens when EAP authentication is completed again
2884 * (reauth or failed PMKSA caching attempt).
2885 */
2886 if (reason != PMKSA_REPLACE)
2887 deauth = 1;
2888 }
2889
2890 if (reason == PMKSA_EXPIRE &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002891 (sm->pmk_len == entry->pmk_len &&
2892 os_memcmp(sm->pmk, entry->pmk, sm->pmk_len) == 0)) {
2893 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002894 "RSN: deauthenticating due to expired PMK");
2895 pmksa_cache_clear_current(sm);
2896 deauth = 1;
2897 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002898
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002899 if (deauth) {
Roshan Pius3a1667e2018-07-03 15:17:14 -07002900 sm->pmk_len = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002901 os_memset(sm->pmk, 0, sizeof(sm->pmk));
2902 wpa_sm_deauthenticate(sm, WLAN_REASON_UNSPECIFIED);
2903 }
2904}
2905
2906
Hai Shalomc1a21442022-02-04 13:43:00 -08002907static bool wpa_sm_pmksa_is_current_cb(struct rsn_pmksa_cache_entry *entry,
2908 void *ctx)
2909{
2910 struct wpa_sm *sm = ctx;
2911
2912 return sm->cur_pmksa == entry;
2913}
2914
2915
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002916/**
2917 * wpa_sm_init - Initialize WPA state machine
2918 * @ctx: Context pointer for callbacks; this needs to be an allocated buffer
2919 * Returns: Pointer to the allocated WPA state machine data
2920 *
2921 * This function is used to allocate a new WPA state machine and the returned
2922 * value is passed to all WPA state machine calls.
2923 */
2924struct wpa_sm * wpa_sm_init(struct wpa_sm_ctx *ctx)
2925{
2926 struct wpa_sm *sm;
2927
2928 sm = os_zalloc(sizeof(*sm));
2929 if (sm == NULL)
2930 return NULL;
2931 dl_list_init(&sm->pmksa_candidates);
2932 sm->renew_snonce = 1;
2933 sm->ctx = ctx;
2934
2935 sm->dot11RSNAConfigPMKLifetime = 43200;
2936 sm->dot11RSNAConfigPMKReauthThreshold = 70;
2937 sm->dot11RSNAConfigSATimeout = 60;
2938
Hai Shalomc1a21442022-02-04 13:43:00 -08002939 sm->pmksa = pmksa_cache_init(wpa_sm_pmksa_free_cb,
2940 wpa_sm_pmksa_is_current_cb, sm, sm);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002941 if (sm->pmksa == NULL) {
2942 wpa_msg(sm->ctx->msg_ctx, MSG_ERROR,
2943 "RSN: PMKSA cache initialization failed");
2944 os_free(sm);
2945 return NULL;
2946 }
2947
2948 return sm;
2949}
2950
2951
2952/**
2953 * wpa_sm_deinit - Deinitialize WPA state machine
2954 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2955 */
2956void wpa_sm_deinit(struct wpa_sm *sm)
2957{
2958 if (sm == NULL)
2959 return;
2960 pmksa_cache_deinit(sm->pmksa);
2961 eloop_cancel_timeout(wpa_sm_start_preauth, sm, NULL);
2962 eloop_cancel_timeout(wpa_sm_rekey_ptk, sm, NULL);
2963 os_free(sm->assoc_wpa_ie);
Hai Shalomc3565922019-10-28 11:58:20 -07002964 os_free(sm->assoc_rsnxe);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002965 os_free(sm->ap_wpa_ie);
2966 os_free(sm->ap_rsn_ie);
Hai Shalomc3565922019-10-28 11:58:20 -07002967 os_free(sm->ap_rsnxe);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002968 wpa_sm_drop_sa(sm);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002969 os_free(sm->ctx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002970#ifdef CONFIG_IEEE80211R
2971 os_free(sm->assoc_resp_ies);
2972#endif /* CONFIG_IEEE80211R */
Dmitry Shmidt55840ad2015-12-14 12:45:46 -08002973#ifdef CONFIG_TESTING_OPTIONS
2974 wpabuf_free(sm->test_assoc_ie);
2975#endif /* CONFIG_TESTING_OPTIONS */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002976#ifdef CONFIG_FILS_SK_PFS
2977 crypto_ecdh_deinit(sm->fils_ecdh);
2978#endif /* CONFIG_FILS_SK_PFS */
2979#ifdef CONFIG_FILS
2980 wpabuf_free(sm->fils_ft_ies);
2981#endif /* CONFIG_FILS */
2982#ifdef CONFIG_OWE
2983 crypto_ecdh_deinit(sm->owe_ecdh);
2984#endif /* CONFIG_OWE */
Hai Shalom021b0b52019-04-10 11:17:58 -07002985#ifdef CONFIG_DPP2
2986 wpabuf_clear_free(sm->dpp_z);
2987#endif /* CONFIG_DPP2 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002988 os_free(sm);
2989}
2990
2991
2992/**
2993 * wpa_sm_notify_assoc - Notify WPA state machine about association
2994 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2995 * @bssid: The BSSID of the new association
2996 *
2997 * This function is called to let WPA state machine know that the connection
2998 * was established.
2999 */
3000void wpa_sm_notify_assoc(struct wpa_sm *sm, const u8 *bssid)
3001{
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02003002 int clear_keys = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003003
3004 if (sm == NULL)
3005 return;
3006
3007 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
3008 "WPA: Association event - clear replay counter");
3009 os_memcpy(sm->bssid, bssid, ETH_ALEN);
3010 os_memset(sm->rx_replay_counter, 0, WPA_REPLAY_COUNTER_LEN);
3011 sm->rx_replay_counter_set = 0;
3012 sm->renew_snonce = 1;
3013 if (os_memcmp(sm->preauth_bssid, bssid, ETH_ALEN) == 0)
3014 rsn_preauth_deinit(sm);
3015
3016#ifdef CONFIG_IEEE80211R
3017 if (wpa_ft_is_completed(sm)) {
3018 /*
3019 * Clear portValid to kick EAPOL state machine to re-enter
3020 * AUTHENTICATED state to get the EAPOL port Authorized.
3021 */
Hai Shalome21d4e82020-04-29 16:34:06 -07003022 eapol_sm_notify_portValid(sm->eapol, false);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003023 wpa_supplicant_key_neg_complete(sm, sm->bssid, 1);
3024
3025 /* Prepare for the next transition */
3026 wpa_ft_prepare_auth_request(sm, NULL);
3027
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02003028 clear_keys = 0;
Hai Shalom81f62d82019-07-22 12:10:00 -07003029 sm->ft_protocol = 1;
3030 } else {
3031 sm->ft_protocol = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003032 }
3033#endif /* CONFIG_IEEE80211R */
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003034#ifdef CONFIG_FILS
3035 if (sm->fils_completed) {
3036 /*
3037 * Clear portValid to kick EAPOL state machine to re-enter
3038 * AUTHENTICATED state to get the EAPOL port Authorized.
3039 */
3040 wpa_supplicant_key_neg_complete(sm, sm->bssid, 1);
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02003041 clear_keys = 0;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003042 }
3043#endif /* CONFIG_FILS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003044
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02003045 if (clear_keys) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003046 /*
3047 * IEEE 802.11, 8.4.10: Delete PTK SA on (re)association if
3048 * this is not part of a Fast BSS Transition.
3049 */
3050 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Clear old PTK");
3051 sm->ptk_set = 0;
Dmitry Shmidt61593f02014-04-21 16:27:35 -07003052 os_memset(&sm->ptk, 0, sizeof(sm->ptk));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003053 sm->tptk_set = 0;
Dmitry Shmidt61593f02014-04-21 16:27:35 -07003054 os_memset(&sm->tptk, 0, sizeof(sm->tptk));
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02003055 os_memset(&sm->gtk, 0, sizeof(sm->gtk));
Jouni Malinen58c0e962017-10-01 12:12:24 +03003056 os_memset(&sm->gtk_wnm_sleep, 0, sizeof(sm->gtk_wnm_sleep));
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02003057 os_memset(&sm->igtk, 0, sizeof(sm->igtk));
Jouni Malinen58c0e962017-10-01 12:12:24 +03003058 os_memset(&sm->igtk_wnm_sleep, 0, sizeof(sm->igtk_wnm_sleep));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003059 }
3060
3061#ifdef CONFIG_TDLS
3062 wpa_tdls_assoc(sm);
3063#endif /* CONFIG_TDLS */
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003064
3065#ifdef CONFIG_P2P
3066 os_memset(sm->p2p_ip_addr, 0, sizeof(sm->p2p_ip_addr));
3067#endif /* CONFIG_P2P */
Hai Shalomfdcde762020-04-02 11:19:20 -07003068
3069 sm->keyidx_active = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003070}
3071
3072
3073/**
3074 * wpa_sm_notify_disassoc - Notify WPA state machine about disassociation
3075 * @sm: Pointer to WPA state machine data from wpa_sm_init()
3076 *
3077 * This function is called to let WPA state machine know that the connection
3078 * was lost. This will abort any existing pre-authentication session.
3079 */
3080void wpa_sm_notify_disassoc(struct wpa_sm *sm)
3081{
Dmitry Shmidt4dd28dc2015-03-10 11:21:43 -07003082 eloop_cancel_timeout(wpa_sm_start_preauth, sm, NULL);
3083 eloop_cancel_timeout(wpa_sm_rekey_ptk, sm, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003084 rsn_preauth_deinit(sm);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003085 pmksa_cache_clear_current(sm);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003086 if (wpa_sm_get_state(sm) == WPA_4WAY_HANDSHAKE)
3087 sm->dot11RSNA4WayHandshakeFailures++;
3088#ifdef CONFIG_TDLS
3089 wpa_tdls_disassoc(sm);
3090#endif /* CONFIG_TDLS */
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003091#ifdef CONFIG_FILS
3092 sm->fils_completed = 0;
3093#endif /* CONFIG_FILS */
Jouni Malinen4283f9e2017-09-22 12:06:37 +03003094#ifdef CONFIG_IEEE80211R
3095 sm->ft_reassoc_completed = 0;
Hai Shalom81f62d82019-07-22 12:10:00 -07003096 sm->ft_protocol = 0;
Jouni Malinen4283f9e2017-09-22 12:06:37 +03003097#endif /* CONFIG_IEEE80211R */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003098
3099 /* Keys are not needed in the WPA state machine anymore */
3100 wpa_sm_drop_sa(sm);
Hai Shalomfdcde762020-04-02 11:19:20 -07003101 sm->keyidx_active = 0;
Dmitry Shmidt4dd28dc2015-03-10 11:21:43 -07003102
3103 sm->msg_3_of_4_ok = 0;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003104 os_memset(sm->bssid, 0, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003105}
3106
3107
3108/**
3109 * wpa_sm_set_pmk - Set PMK
3110 * @sm: Pointer to WPA state machine data from wpa_sm_init()
3111 * @pmk: The new PMK
3112 * @pmk_len: The length of the new PMK in bytes
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08003113 * @pmkid: Calculated PMKID
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003114 * @bssid: AA to add into PMKSA cache or %NULL to not cache the PMK
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003115 *
3116 * Configure the PMK for WPA state machine.
3117 */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003118void wpa_sm_set_pmk(struct wpa_sm *sm, const u8 *pmk, size_t pmk_len,
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08003119 const u8 *pmkid, const u8 *bssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003120{
3121 if (sm == NULL)
3122 return;
3123
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003124 wpa_hexdump_key(MSG_DEBUG, "WPA: Set PMK based on external data",
3125 pmk, pmk_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003126 sm->pmk_len = pmk_len;
3127 os_memcpy(sm->pmk, pmk, pmk_len);
3128
3129#ifdef CONFIG_IEEE80211R
3130 /* Set XXKey to be PSK for FT key derivation */
3131 sm->xxkey_len = pmk_len;
3132 os_memcpy(sm->xxkey, pmk, pmk_len);
3133#endif /* CONFIG_IEEE80211R */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003134
3135 if (bssid) {
Hai Shalomc1a21442022-02-04 13:43:00 -08003136 sm->cur_pmksa = pmksa_cache_add(sm->pmksa, pmk, pmk_len,
3137 pmkid, NULL, 0, bssid,
3138 sm->own_addr,
3139 sm->network_ctx, sm->key_mgmt,
3140 NULL);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003141 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003142}
3143
3144
3145/**
3146 * wpa_sm_set_pmk_from_pmksa - Set PMK based on the current PMKSA
3147 * @sm: Pointer to WPA state machine data from wpa_sm_init()
3148 *
3149 * Take the PMK from the current PMKSA into use. If no PMKSA is active, the PMK
3150 * will be cleared.
3151 */
3152void wpa_sm_set_pmk_from_pmksa(struct wpa_sm *sm)
3153{
3154 if (sm == NULL)
3155 return;
3156
3157 if (sm->cur_pmksa) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003158 wpa_hexdump_key(MSG_DEBUG,
3159 "WPA: Set PMK based on current PMKSA",
3160 sm->cur_pmksa->pmk, sm->cur_pmksa->pmk_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003161 sm->pmk_len = sm->cur_pmksa->pmk_len;
3162 os_memcpy(sm->pmk, sm->cur_pmksa->pmk, sm->pmk_len);
3163 } else {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003164 wpa_printf(MSG_DEBUG, "WPA: No current PMKSA - clear PMK");
3165 sm->pmk_len = 0;
3166 os_memset(sm->pmk, 0, PMK_LEN_MAX);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003167 }
3168}
3169
3170
3171/**
3172 * wpa_sm_set_fast_reauth - Set fast reauthentication (EAP) enabled/disabled
3173 * @sm: Pointer to WPA state machine data from wpa_sm_init()
3174 * @fast_reauth: Whether fast reauthentication (EAP) is allowed
3175 */
3176void wpa_sm_set_fast_reauth(struct wpa_sm *sm, int fast_reauth)
3177{
3178 if (sm)
3179 sm->fast_reauth = fast_reauth;
3180}
3181
3182
3183/**
3184 * wpa_sm_set_scard_ctx - Set context pointer for smartcard callbacks
3185 * @sm: Pointer to WPA state machine data from wpa_sm_init()
3186 * @scard_ctx: Context pointer for smartcard related callback functions
3187 */
3188void wpa_sm_set_scard_ctx(struct wpa_sm *sm, void *scard_ctx)
3189{
3190 if (sm == NULL)
3191 return;
3192 sm->scard_ctx = scard_ctx;
3193 if (sm->preauth_eapol)
3194 eapol_sm_register_scard_ctx(sm->preauth_eapol, scard_ctx);
3195}
3196
3197
3198/**
Hai Shalomfdcde762020-04-02 11:19:20 -07003199 * wpa_sm_set_config - Notification of current configuration change
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003200 * @sm: Pointer to WPA state machine data from wpa_sm_init()
3201 * @config: Pointer to current network configuration
3202 *
3203 * Notify WPA state machine that configuration has changed. config will be
3204 * stored as a backpointer to network configuration. This can be %NULL to clear
3205 * the stored pointed.
3206 */
3207void wpa_sm_set_config(struct wpa_sm *sm, struct rsn_supp_config *config)
3208{
3209 if (!sm)
3210 return;
3211
3212 if (config) {
3213 sm->network_ctx = config->network_ctx;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003214 sm->allowed_pairwise_cipher = config->allowed_pairwise_cipher;
3215 sm->proactive_key_caching = config->proactive_key_caching;
3216 sm->eap_workaround = config->eap_workaround;
3217 sm->eap_conf_ctx = config->eap_conf_ctx;
3218 if (config->ssid) {
3219 os_memcpy(sm->ssid, config->ssid, config->ssid_len);
3220 sm->ssid_len = config->ssid_len;
3221 } else
3222 sm->ssid_len = 0;
3223 sm->wpa_ptk_rekey = config->wpa_ptk_rekey;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003224 sm->p2p = config->p2p;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003225 sm->wpa_rsc_relaxation = config->wpa_rsc_relaxation;
Hai Shalomfdcde762020-04-02 11:19:20 -07003226 sm->owe_ptk_workaround = config->owe_ptk_workaround;
Hai Shalom60840252021-02-19 19:02:11 -08003227 sm->force_kdk_derivation = config->force_kdk_derivation;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003228#ifdef CONFIG_FILS
3229 if (config->fils_cache_id) {
3230 sm->fils_cache_id_set = 1;
3231 os_memcpy(sm->fils_cache_id, config->fils_cache_id,
3232 FILS_CACHE_ID_LEN);
3233 } else {
3234 sm->fils_cache_id_set = 0;
3235 }
3236#endif /* CONFIG_FILS */
Hai Shalomfdcde762020-04-02 11:19:20 -07003237 sm->beacon_prot = config->beacon_prot;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003238 } else {
3239 sm->network_ctx = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003240 sm->allowed_pairwise_cipher = 0;
3241 sm->proactive_key_caching = 0;
3242 sm->eap_workaround = 0;
3243 sm->eap_conf_ctx = NULL;
3244 sm->ssid_len = 0;
3245 sm->wpa_ptk_rekey = 0;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003246 sm->p2p = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003247 sm->wpa_rsc_relaxation = 0;
Hai Shalomfdcde762020-04-02 11:19:20 -07003248 sm->owe_ptk_workaround = 0;
3249 sm->beacon_prot = 0;
Hai Shalom60840252021-02-19 19:02:11 -08003250 sm->force_kdk_derivation = false;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003251 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003252}
3253
3254
3255/**
3256 * wpa_sm_set_own_addr - Set own MAC address
3257 * @sm: Pointer to WPA state machine data from wpa_sm_init()
3258 * @addr: Own MAC address
3259 */
3260void wpa_sm_set_own_addr(struct wpa_sm *sm, const u8 *addr)
3261{
3262 if (sm)
3263 os_memcpy(sm->own_addr, addr, ETH_ALEN);
3264}
3265
3266
3267/**
3268 * wpa_sm_set_ifname - Set network interface name
3269 * @sm: Pointer to WPA state machine data from wpa_sm_init()
3270 * @ifname: Interface name
3271 * @bridge_ifname: Optional bridge interface name (for pre-auth)
3272 */
3273void wpa_sm_set_ifname(struct wpa_sm *sm, const char *ifname,
3274 const char *bridge_ifname)
3275{
3276 if (sm) {
3277 sm->ifname = ifname;
3278 sm->bridge_ifname = bridge_ifname;
3279 }
3280}
3281
3282
3283/**
3284 * wpa_sm_set_eapol - Set EAPOL state machine pointer
3285 * @sm: Pointer to WPA state machine data from wpa_sm_init()
3286 * @eapol: Pointer to EAPOL state machine allocated with eapol_sm_init()
3287 */
3288void wpa_sm_set_eapol(struct wpa_sm *sm, struct eapol_sm *eapol)
3289{
3290 if (sm)
3291 sm->eapol = eapol;
3292}
3293
3294
3295/**
3296 * wpa_sm_set_param - Set WPA state machine parameters
3297 * @sm: Pointer to WPA state machine data from wpa_sm_init()
3298 * @param: Parameter field
3299 * @value: Parameter value
3300 * Returns: 0 on success, -1 on failure
3301 */
3302int wpa_sm_set_param(struct wpa_sm *sm, enum wpa_sm_conf_params param,
3303 unsigned int value)
3304{
3305 int ret = 0;
3306
3307 if (sm == NULL)
3308 return -1;
3309
3310 switch (param) {
3311 case RSNA_PMK_LIFETIME:
3312 if (value > 0)
3313 sm->dot11RSNAConfigPMKLifetime = value;
3314 else
3315 ret = -1;
3316 break;
3317 case RSNA_PMK_REAUTH_THRESHOLD:
3318 if (value > 0 && value <= 100)
3319 sm->dot11RSNAConfigPMKReauthThreshold = value;
3320 else
3321 ret = -1;
3322 break;
3323 case RSNA_SA_TIMEOUT:
3324 if (value > 0)
3325 sm->dot11RSNAConfigSATimeout = value;
3326 else
3327 ret = -1;
3328 break;
3329 case WPA_PARAM_PROTO:
3330 sm->proto = value;
3331 break;
3332 case WPA_PARAM_PAIRWISE:
3333 sm->pairwise_cipher = value;
3334 break;
3335 case WPA_PARAM_GROUP:
3336 sm->group_cipher = value;
3337 break;
3338 case WPA_PARAM_KEY_MGMT:
3339 sm->key_mgmt = value;
3340 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003341 case WPA_PARAM_MGMT_GROUP:
3342 sm->mgmt_group_cipher = value;
3343 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003344 case WPA_PARAM_RSN_ENABLED:
3345 sm->rsn_enabled = value;
3346 break;
3347 case WPA_PARAM_MFP:
3348 sm->mfp = value;
3349 break;
Hai Shalom74f70d42019-02-11 14:42:39 -08003350 case WPA_PARAM_OCV:
3351 sm->ocv = value;
3352 break;
Hai Shalomc3565922019-10-28 11:58:20 -07003353 case WPA_PARAM_SAE_PWE:
3354 sm->sae_pwe = value;
3355 break;
Hai Shalom899fcc72020-10-19 14:38:18 -07003356 case WPA_PARAM_SAE_PK:
3357 sm->sae_pk = value;
3358 break;
Hai Shalomfdcde762020-04-02 11:19:20 -07003359 case WPA_PARAM_DENY_PTK0_REKEY:
3360 sm->wpa_deny_ptk0_rekey = value;
3361 break;
3362 case WPA_PARAM_EXT_KEY_ID:
3363 sm->ext_key_id = value;
3364 break;
3365 case WPA_PARAM_USE_EXT_KEY_ID:
3366 sm->use_ext_key_id = value;
3367 break;
Hai Shalomb755a2a2020-04-23 21:49:02 -07003368#ifdef CONFIG_TESTING_OPTIONS
3369 case WPA_PARAM_FT_RSNXE_USED:
3370 sm->ft_rsnxe_used = value;
3371 break;
Hai Shalom899fcc72020-10-19 14:38:18 -07003372 case WPA_PARAM_OCI_FREQ_EAPOL:
3373 sm->oci_freq_override_eapol = value;
3374 break;
3375 case WPA_PARAM_OCI_FREQ_EAPOL_G2:
3376 sm->oci_freq_override_eapol_g2 = value;
3377 break;
3378 case WPA_PARAM_OCI_FREQ_FT_ASSOC:
3379 sm->oci_freq_override_ft_assoc = value;
3380 break;
3381 case WPA_PARAM_OCI_FREQ_FILS_ASSOC:
3382 sm->oci_freq_override_fils_assoc = value;
3383 break;
Hai Shalomb755a2a2020-04-23 21:49:02 -07003384#endif /* CONFIG_TESTING_OPTIONS */
Hai Shalom4fbc08f2020-05-18 12:37:00 -07003385#ifdef CONFIG_DPP2
3386 case WPA_PARAM_DPP_PFS:
3387 sm->dpp_pfs = value;
3388 break;
3389#endif /* CONFIG_DPP2 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003390 default:
3391 break;
3392 }
3393
3394 return ret;
3395}
3396
3397
3398/**
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003399 * wpa_sm_get_status - Get WPA state machine
3400 * @sm: Pointer to WPA state machine data from wpa_sm_init()
3401 * @buf: Buffer for status information
3402 * @buflen: Maximum buffer length
3403 * @verbose: Whether to include verbose status information
3404 * Returns: Number of bytes written to buf.
3405 *
3406 * Query WPA state machine for status information. This function fills in
3407 * a text area with current status information. If the buffer (buf) is not
3408 * large enough, status information will be truncated to fit the buffer.
3409 */
3410int wpa_sm_get_status(struct wpa_sm *sm, char *buf, size_t buflen,
3411 int verbose)
3412{
3413 char *pos = buf, *end = buf + buflen;
3414 int ret;
3415
3416 ret = os_snprintf(pos, end - pos,
3417 "pairwise_cipher=%s\n"
3418 "group_cipher=%s\n"
3419 "key_mgmt=%s\n",
3420 wpa_cipher_txt(sm->pairwise_cipher),
3421 wpa_cipher_txt(sm->group_cipher),
3422 wpa_key_mgmt_txt(sm->key_mgmt, sm->proto));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003423 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003424 return pos - buf;
3425 pos += ret;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003426
Hai Shalom4fbc08f2020-05-18 12:37:00 -07003427#ifdef CONFIG_DPP2
3428 if (sm->key_mgmt == WPA_KEY_MGMT_DPP && sm->dpp_z) {
3429 ret = os_snprintf(pos, end - pos, "dpp_pfs=1\n");
3430 if (os_snprintf_error(end - pos, ret))
3431 return pos - buf;
3432 pos += ret;
3433 }
3434#endif /* CONFIG_DPP2 */
3435
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003436 if (sm->mfp != NO_MGMT_FRAME_PROTECTION && sm->ap_rsn_ie) {
3437 struct wpa_ie_data rsn;
3438 if (wpa_parse_wpa_ie_rsn(sm->ap_rsn_ie, sm->ap_rsn_ie_len, &rsn)
3439 >= 0 &&
3440 rsn.capabilities & (WPA_CAPABILITY_MFPR |
3441 WPA_CAPABILITY_MFPC)) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003442 ret = os_snprintf(pos, end - pos, "pmf=%d\n"
3443 "mgmt_group_cipher=%s\n",
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003444 (rsn.capabilities &
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003445 WPA_CAPABILITY_MFPR) ? 2 : 1,
3446 wpa_cipher_txt(
3447 sm->mgmt_group_cipher));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003448 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003449 return pos - buf;
3450 pos += ret;
3451 }
3452 }
3453
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003454 return pos - buf;
3455}
3456
3457
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07003458int wpa_sm_pmf_enabled(struct wpa_sm *sm)
3459{
3460 struct wpa_ie_data rsn;
3461
3462 if (sm->mfp == NO_MGMT_FRAME_PROTECTION || !sm->ap_rsn_ie)
3463 return 0;
3464
3465 if (wpa_parse_wpa_ie_rsn(sm->ap_rsn_ie, sm->ap_rsn_ie_len, &rsn) >= 0 &&
3466 rsn.capabilities & (WPA_CAPABILITY_MFPR | WPA_CAPABILITY_MFPC))
3467 return 1;
3468
3469 return 0;
3470}
3471
3472
Hai Shalomfdcde762020-04-02 11:19:20 -07003473int wpa_sm_ext_key_id(struct wpa_sm *sm)
3474{
3475 return sm ? sm->ext_key_id : 0;
3476}
3477
3478
3479int wpa_sm_ext_key_id_active(struct wpa_sm *sm)
3480{
3481 return sm ? sm->use_ext_key_id : 0;
3482}
3483
3484
Hai Shalom74f70d42019-02-11 14:42:39 -08003485int wpa_sm_ocv_enabled(struct wpa_sm *sm)
3486{
3487 struct wpa_ie_data rsn;
3488
3489 if (!sm->ocv || !sm->ap_rsn_ie)
3490 return 0;
3491
3492 return wpa_parse_wpa_ie_rsn(sm->ap_rsn_ie, sm->ap_rsn_ie_len,
3493 &rsn) >= 0 &&
3494 (rsn.capabilities & WPA_CAPABILITY_OCVC);
3495}
3496
3497
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003498/**
3499 * wpa_sm_set_assoc_wpa_ie_default - Generate own WPA/RSN IE from configuration
3500 * @sm: Pointer to WPA state machine data from wpa_sm_init()
3501 * @wpa_ie: Pointer to buffer for WPA/RSN IE
3502 * @wpa_ie_len: Pointer to the length of the wpa_ie buffer
3503 * Returns: 0 on success, -1 on failure
3504 */
3505int wpa_sm_set_assoc_wpa_ie_default(struct wpa_sm *sm, u8 *wpa_ie,
3506 size_t *wpa_ie_len)
3507{
3508 int res;
3509
3510 if (sm == NULL)
3511 return -1;
3512
Dmitry Shmidt55840ad2015-12-14 12:45:46 -08003513#ifdef CONFIG_TESTING_OPTIONS
3514 if (sm->test_assoc_ie) {
3515 wpa_printf(MSG_DEBUG,
3516 "TESTING: Replace association WPA/RSN IE");
3517 if (*wpa_ie_len < wpabuf_len(sm->test_assoc_ie))
3518 return -1;
3519 os_memcpy(wpa_ie, wpabuf_head(sm->test_assoc_ie),
3520 wpabuf_len(sm->test_assoc_ie));
3521 res = wpabuf_len(sm->test_assoc_ie);
3522 } else
3523#endif /* CONFIG_TESTING_OPTIONS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003524 res = wpa_gen_wpa_ie(sm, wpa_ie, *wpa_ie_len);
3525 if (res < 0)
3526 return -1;
3527 *wpa_ie_len = res;
3528
3529 wpa_hexdump(MSG_DEBUG, "WPA: Set own WPA IE default",
3530 wpa_ie, *wpa_ie_len);
3531
3532 if (sm->assoc_wpa_ie == NULL) {
3533 /*
3534 * Make a copy of the WPA/RSN IE so that 4-Way Handshake gets
3535 * the correct version of the IE even if PMKSA caching is
3536 * aborted (which would remove PMKID from IE generation).
3537 */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003538 sm->assoc_wpa_ie = os_memdup(wpa_ie, *wpa_ie_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003539 if (sm->assoc_wpa_ie == NULL)
3540 return -1;
3541
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003542 sm->assoc_wpa_ie_len = *wpa_ie_len;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003543 } else {
3544 wpa_hexdump(MSG_DEBUG,
3545 "WPA: Leave previously set WPA IE default",
3546 sm->assoc_wpa_ie, sm->assoc_wpa_ie_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003547 }
3548
3549 return 0;
3550}
3551
3552
3553/**
3554 * wpa_sm_set_assoc_wpa_ie - Set own WPA/RSN IE from (Re)AssocReq
3555 * @sm: Pointer to WPA state machine data from wpa_sm_init()
3556 * @ie: Pointer to IE data (starting from id)
3557 * @len: IE length
3558 * Returns: 0 on success, -1 on failure
3559 *
3560 * Inform WPA state machine about the WPA/RSN IE used in (Re)Association
3561 * Request frame. The IE will be used to override the default value generated
3562 * with wpa_sm_set_assoc_wpa_ie_default().
3563 */
3564int wpa_sm_set_assoc_wpa_ie(struct wpa_sm *sm, const u8 *ie, size_t len)
3565{
3566 if (sm == NULL)
3567 return -1;
3568
3569 os_free(sm->assoc_wpa_ie);
3570 if (ie == NULL || len == 0) {
3571 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
3572 "WPA: clearing own WPA/RSN IE");
3573 sm->assoc_wpa_ie = NULL;
3574 sm->assoc_wpa_ie_len = 0;
3575 } else {
3576 wpa_hexdump(MSG_DEBUG, "WPA: set own WPA/RSN IE", ie, len);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003577 sm->assoc_wpa_ie = os_memdup(ie, len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003578 if (sm->assoc_wpa_ie == NULL)
3579 return -1;
3580
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003581 sm->assoc_wpa_ie_len = len;
3582 }
3583
3584 return 0;
3585}
3586
3587
3588/**
Hai Shalomc3565922019-10-28 11:58:20 -07003589 * wpa_sm_set_assoc_rsnxe_default - Generate own RSNXE from configuration
3590 * @sm: Pointer to WPA state machine data from wpa_sm_init()
3591 * @rsnxe: Pointer to buffer for RSNXE
3592 * @rsnxe_len: Pointer to the length of the rsne buffer
3593 * Returns: 0 on success, -1 on failure
3594 */
3595int wpa_sm_set_assoc_rsnxe_default(struct wpa_sm *sm, u8 *rsnxe,
3596 size_t *rsnxe_len)
3597{
3598 int res;
3599
3600 if (!sm)
3601 return -1;
3602
3603 res = wpa_gen_rsnxe(sm, rsnxe, *rsnxe_len);
3604 if (res < 0)
3605 return -1;
3606 *rsnxe_len = res;
3607
3608 wpa_hexdump(MSG_DEBUG, "RSN: Set own RSNXE default", rsnxe, *rsnxe_len);
3609
3610 if (sm->assoc_rsnxe) {
3611 wpa_hexdump(MSG_DEBUG,
3612 "RSN: Leave previously set RSNXE default",
3613 sm->assoc_rsnxe, sm->assoc_rsnxe_len);
3614 } else if (*rsnxe_len > 0) {
3615 /*
3616 * Make a copy of the RSNXE so that 4-Way Handshake gets the
3617 * correct version of the IE even if it gets changed.
3618 */
3619 sm->assoc_rsnxe = os_memdup(rsnxe, *rsnxe_len);
3620 if (!sm->assoc_rsnxe)
3621 return -1;
3622
3623 sm->assoc_rsnxe_len = *rsnxe_len;
3624 }
3625
3626 return 0;
3627}
3628
3629
3630/**
3631 * wpa_sm_set_assoc_rsnxe - Set own RSNXE from (Re)AssocReq
3632 * @sm: Pointer to WPA state machine data from wpa_sm_init()
3633 * @ie: Pointer to IE data (starting from id)
3634 * @len: IE length
3635 * Returns: 0 on success, -1 on failure
3636 *
3637 * Inform WPA state machine about the RSNXE used in (Re)Association Request
3638 * frame. The IE will be used to override the default value generated
3639 * with wpa_sm_set_assoc_rsnxe_default().
3640 */
3641int wpa_sm_set_assoc_rsnxe(struct wpa_sm *sm, const u8 *ie, size_t len)
3642{
3643 if (!sm)
3644 return -1;
3645
3646 os_free(sm->assoc_rsnxe);
3647 if (!ie || len == 0) {
3648 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
3649 "RSN: clearing own RSNXE");
3650 sm->assoc_rsnxe = NULL;
3651 sm->assoc_rsnxe_len = 0;
3652 } else {
3653 wpa_hexdump(MSG_DEBUG, "RSN: set own RSNXE", ie, len);
3654 sm->assoc_rsnxe = os_memdup(ie, len);
3655 if (!sm->assoc_rsnxe)
3656 return -1;
3657
3658 sm->assoc_rsnxe_len = len;
3659 }
3660
3661 return 0;
3662}
3663
3664
3665/**
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003666 * wpa_sm_set_ap_wpa_ie - Set AP WPA IE from Beacon/ProbeResp
3667 * @sm: Pointer to WPA state machine data from wpa_sm_init()
3668 * @ie: Pointer to IE data (starting from id)
3669 * @len: IE length
3670 * Returns: 0 on success, -1 on failure
3671 *
3672 * Inform WPA state machine about the WPA IE used in Beacon / Probe Response
3673 * frame.
3674 */
3675int wpa_sm_set_ap_wpa_ie(struct wpa_sm *sm, const u8 *ie, size_t len)
3676{
3677 if (sm == NULL)
3678 return -1;
3679
3680 os_free(sm->ap_wpa_ie);
3681 if (ie == NULL || len == 0) {
3682 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
3683 "WPA: clearing AP WPA IE");
3684 sm->ap_wpa_ie = NULL;
3685 sm->ap_wpa_ie_len = 0;
3686 } else {
3687 wpa_hexdump(MSG_DEBUG, "WPA: set AP WPA IE", ie, len);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003688 sm->ap_wpa_ie = os_memdup(ie, len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003689 if (sm->ap_wpa_ie == NULL)
3690 return -1;
3691
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003692 sm->ap_wpa_ie_len = len;
3693 }
3694
3695 return 0;
3696}
3697
3698
3699/**
3700 * wpa_sm_set_ap_rsn_ie - Set AP RSN IE from Beacon/ProbeResp
3701 * @sm: Pointer to WPA state machine data from wpa_sm_init()
3702 * @ie: Pointer to IE data (starting from id)
3703 * @len: IE length
3704 * Returns: 0 on success, -1 on failure
3705 *
3706 * Inform WPA state machine about the RSN IE used in Beacon / Probe Response
3707 * frame.
3708 */
3709int wpa_sm_set_ap_rsn_ie(struct wpa_sm *sm, const u8 *ie, size_t len)
3710{
3711 if (sm == NULL)
3712 return -1;
3713
3714 os_free(sm->ap_rsn_ie);
3715 if (ie == NULL || len == 0) {
3716 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
3717 "WPA: clearing AP RSN IE");
3718 sm->ap_rsn_ie = NULL;
3719 sm->ap_rsn_ie_len = 0;
3720 } else {
3721 wpa_hexdump(MSG_DEBUG, "WPA: set AP RSN IE", ie, len);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003722 sm->ap_rsn_ie = os_memdup(ie, len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003723 if (sm->ap_rsn_ie == NULL)
3724 return -1;
3725
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003726 sm->ap_rsn_ie_len = len;
3727 }
3728
3729 return 0;
3730}
3731
3732
3733/**
Hai Shalomc3565922019-10-28 11:58:20 -07003734 * wpa_sm_set_ap_rsnxe - Set AP RSNXE from Beacon/ProbeResp
3735 * @sm: Pointer to WPA state machine data from wpa_sm_init()
3736 * @ie: Pointer to IE data (starting from id)
3737 * @len: IE length
3738 * Returns: 0 on success, -1 on failure
3739 *
3740 * Inform WPA state machine about the RSNXE used in Beacon / Probe Response
3741 * frame.
3742 */
3743int wpa_sm_set_ap_rsnxe(struct wpa_sm *sm, const u8 *ie, size_t len)
3744{
3745 if (!sm)
3746 return -1;
3747
3748 os_free(sm->ap_rsnxe);
3749 if (!ie || len == 0) {
3750 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: clearing AP RSNXE");
3751 sm->ap_rsnxe = NULL;
3752 sm->ap_rsnxe_len = 0;
3753 } else {
3754 wpa_hexdump(MSG_DEBUG, "WPA: set AP RSNXE", ie, len);
3755 sm->ap_rsnxe = os_memdup(ie, len);
3756 if (!sm->ap_rsnxe)
3757 return -1;
3758
3759 sm->ap_rsnxe_len = len;
3760 }
3761
3762 return 0;
3763}
3764
3765
3766/**
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003767 * wpa_sm_parse_own_wpa_ie - Parse own WPA/RSN IE
3768 * @sm: Pointer to WPA state machine data from wpa_sm_init()
3769 * @data: Pointer to data area for parsing results
3770 * Returns: 0 on success, -1 if IE is not known, or -2 on parsing failure
3771 *
3772 * Parse the contents of the own WPA or RSN IE from (Re)AssocReq and write the
3773 * parsed data into data.
3774 */
3775int wpa_sm_parse_own_wpa_ie(struct wpa_sm *sm, struct wpa_ie_data *data)
3776{
3777 if (sm == NULL)
3778 return -1;
3779
3780 if (sm->assoc_wpa_ie == NULL) {
3781 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
3782 "WPA: No WPA/RSN IE available from association info");
3783 return -1;
3784 }
3785 if (wpa_parse_wpa_ie(sm->assoc_wpa_ie, sm->assoc_wpa_ie_len, data))
3786 return -2;
3787 return 0;
3788}
3789
3790
3791int wpa_sm_pmksa_cache_list(struct wpa_sm *sm, char *buf, size_t len)
3792{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003793 return pmksa_cache_list(sm->pmksa, buf, len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003794}
3795
3796
Dmitry Shmidt29333592017-01-09 12:27:11 -08003797struct rsn_pmksa_cache_entry * wpa_sm_pmksa_cache_head(struct wpa_sm *sm)
3798{
3799 return pmksa_cache_head(sm->pmksa);
3800}
3801
3802
3803struct rsn_pmksa_cache_entry *
3804wpa_sm_pmksa_cache_add_entry(struct wpa_sm *sm,
3805 struct rsn_pmksa_cache_entry * entry)
3806{
3807 return pmksa_cache_add_entry(sm->pmksa, entry);
3808}
3809
3810
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003811void wpa_sm_pmksa_cache_add(struct wpa_sm *sm, const u8 *pmk, size_t pmk_len,
3812 const u8 *pmkid, const u8 *bssid,
3813 const u8 *fils_cache_id)
3814{
3815 sm->cur_pmksa = pmksa_cache_add(sm->pmksa, pmk, pmk_len, pmkid, NULL, 0,
3816 bssid, sm->own_addr, sm->network_ctx,
3817 sm->key_mgmt, fils_cache_id);
3818}
3819
3820
3821int wpa_sm_pmksa_exists(struct wpa_sm *sm, const u8 *bssid,
3822 const void *network_ctx)
3823{
Roshan Pius3a1667e2018-07-03 15:17:14 -07003824 return pmksa_cache_get(sm->pmksa, bssid, NULL, network_ctx, 0) != NULL;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003825}
3826
3827
Hai Shalom60840252021-02-19 19:02:11 -08003828struct rsn_pmksa_cache_entry * wpa_sm_pmksa_cache_get(struct wpa_sm *sm,
3829 const u8 *aa,
3830 const u8 *pmkid,
3831 const void *network_ctx,
3832 int akmp)
3833{
3834 return pmksa_cache_get(sm->pmksa, aa, pmkid, network_ctx, akmp);
3835}
3836
3837
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003838void wpa_sm_drop_sa(struct wpa_sm *sm)
3839{
3840 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Clear old PMK and PTK");
3841 sm->ptk_set = 0;
3842 sm->tptk_set = 0;
Roshan Pius3a1667e2018-07-03 15:17:14 -07003843 sm->pmk_len = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003844 os_memset(sm->pmk, 0, sizeof(sm->pmk));
3845 os_memset(&sm->ptk, 0, sizeof(sm->ptk));
3846 os_memset(&sm->tptk, 0, sizeof(sm->tptk));
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02003847 os_memset(&sm->gtk, 0, sizeof(sm->gtk));
Jouni Malinen58c0e962017-10-01 12:12:24 +03003848 os_memset(&sm->gtk_wnm_sleep, 0, sizeof(sm->gtk_wnm_sleep));
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02003849 os_memset(&sm->igtk, 0, sizeof(sm->igtk));
Jouni Malinen58c0e962017-10-01 12:12:24 +03003850 os_memset(&sm->igtk_wnm_sleep, 0, sizeof(sm->igtk_wnm_sleep));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003851#ifdef CONFIG_IEEE80211R
3852 os_memset(sm->xxkey, 0, sizeof(sm->xxkey));
Roshan Pius3a1667e2018-07-03 15:17:14 -07003853 sm->xxkey_len = 0;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003854 os_memset(sm->pmk_r0, 0, sizeof(sm->pmk_r0));
Roshan Pius3a1667e2018-07-03 15:17:14 -07003855 sm->pmk_r0_len = 0;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003856 os_memset(sm->pmk_r1, 0, sizeof(sm->pmk_r1));
Roshan Pius3a1667e2018-07-03 15:17:14 -07003857 sm->pmk_r1_len = 0;
Hai Shalom60840252021-02-19 19:02:11 -08003858#ifdef CONFIG_PASN
3859 os_free(sm->pasn_r1kh);
3860 sm->pasn_r1kh = NULL;
3861 sm->n_pasn_r1kh = 0;
3862#endif /* CONFIG_PASN */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003863#endif /* CONFIG_IEEE80211R */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003864}
3865
3866
3867int wpa_sm_has_ptk(struct wpa_sm *sm)
3868{
3869 if (sm == NULL)
3870 return 0;
3871 return sm->ptk_set;
3872}
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003873
3874
Hai Shalomfdcde762020-04-02 11:19:20 -07003875int wpa_sm_has_ptk_installed(struct wpa_sm *sm)
3876{
3877 if (!sm)
3878 return 0;
3879 return sm->ptk.installed;
3880}
3881
3882
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003883void wpa_sm_update_replay_ctr(struct wpa_sm *sm, const u8 *replay_ctr)
3884{
3885 os_memcpy(sm->rx_replay_counter, replay_ctr, WPA_REPLAY_COUNTER_LEN);
3886}
3887
3888
3889void wpa_sm_pmksa_cache_flush(struct wpa_sm *sm, void *network_ctx)
3890{
Hai Shalomc1a21442022-02-04 13:43:00 -08003891 pmksa_cache_flush(sm->pmksa, network_ctx, NULL, 0, false);
3892}
3893
3894
3895void wpa_sm_external_pmksa_cache_flush(struct wpa_sm *sm, void *network_ctx)
3896{
3897 pmksa_cache_flush(sm->pmksa, network_ctx, NULL, 0, true);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003898}
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003899
Andy Kuoaba17c12022-04-14 16:05:31 +08003900#if defined(CONFIG_DRIVER_NL80211_BRCM) || defined(CONFIG_DRIVER_NL80211_SYNA)
Mir Ali677e7482020-11-12 19:49:02 +05303901void wpa_sm_install_pmk(struct wpa_sm *sm)
3902{
3903 /* In case the driver wants to handle re-assocs, pass it down the PMK. */
3904 if (wpa_sm_set_key(sm, wpa_cipher_to_alg(sm->pairwise_cipher), NULL, 0, 0, NULL, 0,
3905 (u8*)sm->pmk, sm->pmk_len, KEY_FLAG_PMK) < 0) {
3906 wpa_hexdump(MSG_DEBUG, "PSK: Install PMK to the driver for driver reassociations",
3907 (u8*)sm->pmk, sm->pmk_len);
3908 /* No harm if the driver doesn't support. */
3909 wpa_msg(sm->ctx->msg_ctx, MSG_DEBUG,
3910 "WPA: Failed to set PMK to the driver");
3911 }
3912}
Mir Alieaaf04e2021-06-07 12:17:29 +05303913
3914void wpa_sm_notify_brcm_ft_reassoc(struct wpa_sm *sm, const u8 *bssid)
3915{
3916 u8 buf[256];
3917 struct wpa_supplicant *wpa_s = sm->ctx->ctx;
3918
3919 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
3920 "WPA: BRCM FT Reassociation event - clear replay counter");
3921 os_memcpy(sm->bssid, bssid, ETH_ALEN);
3922 os_memset(sm->rx_replay_counter, 0, WPA_REPLAY_COUNTER_LEN);
3923 sm->rx_replay_counter_set = 0;
3924
3925 if (wpa_drv_driver_cmd(wpa_s, "GET_FTKEY", (char *)buf, sizeof(buf)) < 0) {
3926 wpa_msg(sm->ctx->msg_ctx, MSG_ERROR,
3927 "WPA: Failed to get FT KEY information");
3928 wpa_supplicant_deauthenticate(
3929 wpa_s, WLAN_REASON_DEAUTH_LEAVING);
3930
3931 } else {
3932 /* update kck and kek */
3933 os_memcpy(sm->ptk.kck, buf, 16);
3934 os_memcpy(sm->ptk.kek, buf + 16, 16);
3935 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
3936 "WPA: Updated KCK and KEK after FT reassoc");
3937 }
3938}
Andy Kuoaba17c12022-04-14 16:05:31 +08003939#endif /* CONFIG_DRIVER_NL80211_BRCM || CONFIG_DRIVER_NL80211_SYNA */
3940
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003941
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003942#ifdef CONFIG_WNM
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003943int wpa_wnmsleep_install_key(struct wpa_sm *sm, u8 subelem_id, u8 *buf)
3944{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003945 u16 keyinfo;
3946 u8 keylen; /* plaintext key len */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003947 u8 *key_rsc;
3948
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003949 if (subelem_id == WNM_SLEEP_SUBELEM_GTK) {
Dmitry Shmidt61593f02014-04-21 16:27:35 -07003950 struct wpa_gtk_data gd;
3951
3952 os_memset(&gd, 0, sizeof(gd));
3953 keylen = wpa_cipher_key_len(sm->group_cipher);
3954 gd.key_rsc_len = wpa_cipher_rsc_len(sm->group_cipher);
3955 gd.alg = wpa_cipher_to_alg(sm->group_cipher);
3956 if (gd.alg == WPA_ALG_NONE) {
3957 wpa_printf(MSG_DEBUG, "Unsupported group cipher suite");
3958 return -1;
3959 }
3960
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003961 key_rsc = buf + 5;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003962 keyinfo = WPA_GET_LE16(buf + 2);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003963 gd.gtk_len = keylen;
3964 if (gd.gtk_len != buf[4]) {
3965 wpa_printf(MSG_DEBUG, "GTK len mismatch len %d vs %d",
3966 gd.gtk_len, buf[4]);
3967 return -1;
3968 }
3969 gd.keyidx = keyinfo & 0x03; /* B0 - B1 */
3970 gd.tx = wpa_supplicant_gtk_tx_bit_workaround(
3971 sm, !!(keyinfo & WPA_KEY_INFO_TXRX));
3972
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003973 os_memcpy(gd.gtk, buf + 13, gd.gtk_len);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003974
3975 wpa_hexdump_key(MSG_DEBUG, "Install GTK (WNM SLEEP)",
3976 gd.gtk, gd.gtk_len);
Jouni Malinen58c0e962017-10-01 12:12:24 +03003977 if (wpa_supplicant_install_gtk(sm, &gd, key_rsc, 1)) {
Hai Shalom81f62d82019-07-22 12:10:00 -07003978 forced_memzero(&gd, sizeof(gd));
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003979 wpa_printf(MSG_DEBUG, "Failed to install the GTK in "
3980 "WNM mode");
3981 return -1;
3982 }
Hai Shalom81f62d82019-07-22 12:10:00 -07003983 forced_memzero(&gd, sizeof(gd));
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003984 } else if (subelem_id == WNM_SLEEP_SUBELEM_IGTK) {
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02003985 const struct wpa_igtk_kde *igtk;
Dmitry Shmidt61593f02014-04-21 16:27:35 -07003986
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02003987 igtk = (const struct wpa_igtk_kde *) (buf + 2);
Jouni Malinen58c0e962017-10-01 12:12:24 +03003988 if (wpa_supplicant_install_igtk(sm, igtk, 1) < 0)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003989 return -1;
Hai Shalomfdcde762020-04-02 11:19:20 -07003990 } else if (subelem_id == WNM_SLEEP_SUBELEM_BIGTK) {
3991 const struct wpa_bigtk_kde *bigtk;
3992
3993 bigtk = (const struct wpa_bigtk_kde *) (buf + 2);
3994 if (sm->beacon_prot &&
3995 wpa_supplicant_install_bigtk(sm, bigtk, 1) < 0)
3996 return -1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003997 } else {
3998 wpa_printf(MSG_DEBUG, "Unknown element id");
3999 return -1;
4000 }
4001
4002 return 0;
4003}
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08004004#endif /* CONFIG_WNM */
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004005
4006
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004007#ifdef CONFIG_P2P
4008
4009int wpa_sm_get_p2p_ip_addr(struct wpa_sm *sm, u8 *buf)
4010{
4011 if (sm == NULL || WPA_GET_BE32(sm->p2p_ip_addr) == 0)
4012 return -1;
4013 os_memcpy(buf, sm->p2p_ip_addr, 3 * 4);
4014 return 0;
4015}
4016
4017#endif /* CONFIG_P2P */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004018
4019
4020void wpa_sm_set_rx_replay_ctr(struct wpa_sm *sm, const u8 *rx_replay_counter)
4021{
4022 if (rx_replay_counter == NULL)
4023 return;
4024
4025 os_memcpy(sm->rx_replay_counter, rx_replay_counter,
4026 WPA_REPLAY_COUNTER_LEN);
4027 sm->rx_replay_counter_set = 1;
4028 wpa_printf(MSG_DEBUG, "Updated key replay counter");
4029}
4030
4031
Dmitry Shmidt807291d2015-01-27 13:40:23 -08004032void wpa_sm_set_ptk_kck_kek(struct wpa_sm *sm,
4033 const u8 *ptk_kck, size_t ptk_kck_len,
4034 const u8 *ptk_kek, size_t ptk_kek_len)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004035{
Dmitry Shmidt807291d2015-01-27 13:40:23 -08004036 if (ptk_kck && ptk_kck_len <= WPA_KCK_MAX_LEN) {
4037 os_memcpy(sm->ptk.kck, ptk_kck, ptk_kck_len);
4038 sm->ptk.kck_len = ptk_kck_len;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004039 wpa_printf(MSG_DEBUG, "Updated PTK KCK");
4040 }
Dmitry Shmidt807291d2015-01-27 13:40:23 -08004041 if (ptk_kek && ptk_kek_len <= WPA_KEK_MAX_LEN) {
4042 os_memcpy(sm->ptk.kek, ptk_kek, ptk_kek_len);
4043 sm->ptk.kek_len = ptk_kek_len;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004044 wpa_printf(MSG_DEBUG, "Updated PTK KEK");
4045 }
4046 sm->ptk_set = 1;
4047}
Dmitry Shmidt55840ad2015-12-14 12:45:46 -08004048
4049
4050#ifdef CONFIG_TESTING_OPTIONS
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004051
Dmitry Shmidt55840ad2015-12-14 12:45:46 -08004052void wpa_sm_set_test_assoc_ie(struct wpa_sm *sm, struct wpabuf *buf)
4053{
4054 wpabuf_free(sm->test_assoc_ie);
4055 sm->test_assoc_ie = buf;
4056}
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004057
4058
4059const u8 * wpa_sm_get_anonce(struct wpa_sm *sm)
4060{
4061 return sm->anonce;
4062}
4063
Dmitry Shmidt55840ad2015-12-14 12:45:46 -08004064#endif /* CONFIG_TESTING_OPTIONS */
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004065
4066
Roshan Pius3a1667e2018-07-03 15:17:14 -07004067unsigned int wpa_sm_get_key_mgmt(struct wpa_sm *sm)
4068{
4069 return sm->key_mgmt;
4070}
4071
4072
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004073#ifdef CONFIG_FILS
4074
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004075struct wpabuf * fils_build_auth(struct wpa_sm *sm, int dh_group, const u8 *md)
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004076{
4077 struct wpabuf *buf = NULL;
4078 struct wpabuf *erp_msg;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004079 struct wpabuf *pub = NULL;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004080
4081 erp_msg = eapol_sm_build_erp_reauth_start(sm->eapol);
4082 if (!erp_msg && !sm->cur_pmksa) {
4083 wpa_printf(MSG_DEBUG,
4084 "FILS: Neither ERP EAP-Initiate/Re-auth nor PMKSA cache entry is available - skip FILS");
4085 goto fail;
4086 }
4087
4088 wpa_printf(MSG_DEBUG, "FILS: Try to use FILS (erp=%d pmksa_cache=%d)",
4089 erp_msg != NULL, sm->cur_pmksa != NULL);
4090
4091 sm->fils_completed = 0;
4092
4093 if (!sm->assoc_wpa_ie) {
4094 wpa_printf(MSG_INFO, "FILS: No own RSN IE set for FILS");
4095 goto fail;
4096 }
4097
4098 if (random_get_bytes(sm->fils_nonce, FILS_NONCE_LEN) < 0 ||
4099 random_get_bytes(sm->fils_session, FILS_SESSION_LEN) < 0)
4100 goto fail;
4101
4102 wpa_hexdump(MSG_DEBUG, "FILS: Generated FILS Nonce",
4103 sm->fils_nonce, FILS_NONCE_LEN);
4104 wpa_hexdump(MSG_DEBUG, "FILS: Generated FILS Session",
4105 sm->fils_session, FILS_SESSION_LEN);
4106
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004107#ifdef CONFIG_FILS_SK_PFS
4108 sm->fils_dh_group = dh_group;
4109 if (dh_group) {
4110 crypto_ecdh_deinit(sm->fils_ecdh);
4111 sm->fils_ecdh = crypto_ecdh_init(dh_group);
4112 if (!sm->fils_ecdh) {
4113 wpa_printf(MSG_INFO,
4114 "FILS: Could not initialize ECDH with group %d",
4115 dh_group);
4116 goto fail;
4117 }
4118 pub = crypto_ecdh_get_pubkey(sm->fils_ecdh, 1);
4119 if (!pub)
4120 goto fail;
4121 wpa_hexdump_buf(MSG_DEBUG, "FILS: Element (DH public key)",
4122 pub);
4123 sm->fils_dh_elem_len = wpabuf_len(pub);
4124 }
4125#endif /* CONFIG_FILS_SK_PFS */
4126
4127 buf = wpabuf_alloc(1000 + sm->assoc_wpa_ie_len +
4128 (pub ? wpabuf_len(pub) : 0));
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004129 if (!buf)
4130 goto fail;
4131
4132 /* Fields following the Authentication algorithm number field */
4133
4134 /* Authentication Transaction seq# */
4135 wpabuf_put_le16(buf, 1);
4136
4137 /* Status Code */
4138 wpabuf_put_le16(buf, WLAN_STATUS_SUCCESS);
4139
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004140 /* TODO: FILS PK */
4141#ifdef CONFIG_FILS_SK_PFS
4142 if (dh_group) {
4143 /* Finite Cyclic Group */
4144 wpabuf_put_le16(buf, dh_group);
4145 /* Element */
4146 wpabuf_put_buf(buf, pub);
4147 }
4148#endif /* CONFIG_FILS_SK_PFS */
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004149
4150 /* RSNE */
4151 wpa_hexdump(MSG_DEBUG, "FILS: RSNE in FILS Authentication frame",
4152 sm->assoc_wpa_ie, sm->assoc_wpa_ie_len);
4153 wpabuf_put_data(buf, sm->assoc_wpa_ie, sm->assoc_wpa_ie_len);
4154
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004155 if (md) {
4156 /* MDE when using FILS for FT initial association */
4157 struct rsn_mdie *mdie;
4158
4159 wpabuf_put_u8(buf, WLAN_EID_MOBILITY_DOMAIN);
4160 wpabuf_put_u8(buf, sizeof(*mdie));
4161 mdie = wpabuf_put(buf, sizeof(*mdie));
4162 os_memcpy(mdie->mobility_domain, md, MOBILITY_DOMAIN_ID_LEN);
4163 mdie->ft_capab = 0;
4164 }
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004165
4166 /* FILS Nonce */
4167 wpabuf_put_u8(buf, WLAN_EID_EXTENSION); /* Element ID */
4168 wpabuf_put_u8(buf, 1 + FILS_NONCE_LEN); /* Length */
4169 /* Element ID Extension */
4170 wpabuf_put_u8(buf, WLAN_EID_EXT_FILS_NONCE);
4171 wpabuf_put_data(buf, sm->fils_nonce, FILS_NONCE_LEN);
4172
4173 /* FILS Session */
4174 wpabuf_put_u8(buf, WLAN_EID_EXTENSION); /* Element ID */
4175 wpabuf_put_u8(buf, 1 + FILS_SESSION_LEN); /* Length */
4176 /* Element ID Extension */
4177 wpabuf_put_u8(buf, WLAN_EID_EXT_FILS_SESSION);
4178 wpabuf_put_data(buf, sm->fils_session, FILS_SESSION_LEN);
4179
Hai Shalomfdcde762020-04-02 11:19:20 -07004180 /* Wrapped Data */
Paul Stewart092955c2017-02-06 09:13:09 -08004181 sm->fils_erp_pmkid_set = 0;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004182 if (erp_msg) {
4183 wpabuf_put_u8(buf, WLAN_EID_EXTENSION); /* Element ID */
4184 wpabuf_put_u8(buf, 1 + wpabuf_len(erp_msg)); /* Length */
4185 /* Element ID Extension */
Hai Shalomfdcde762020-04-02 11:19:20 -07004186 wpabuf_put_u8(buf, WLAN_EID_EXT_WRAPPED_DATA);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004187 wpabuf_put_buf(buf, erp_msg);
Paul Stewart092955c2017-02-06 09:13:09 -08004188 /* Calculate pending PMKID here so that we do not need to
4189 * maintain a copy of the EAP-Initiate/Reauth message. */
4190 if (fils_pmkid_erp(sm->key_mgmt, wpabuf_head(erp_msg),
4191 wpabuf_len(erp_msg),
4192 sm->fils_erp_pmkid) == 0)
4193 sm->fils_erp_pmkid_set = 1;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004194 }
4195
4196 wpa_hexdump_buf(MSG_DEBUG, "RSN: FILS fields for Authentication frame",
4197 buf);
4198
4199fail:
4200 wpabuf_free(erp_msg);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004201 wpabuf_free(pub);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004202 return buf;
4203}
4204
4205
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004206int fils_process_auth(struct wpa_sm *sm, const u8 *bssid, const u8 *data,
4207 size_t len)
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004208{
4209 const u8 *pos, *end;
4210 struct ieee802_11_elems elems;
4211 struct wpa_ie_data rsn;
4212 int pmkid_match = 0;
4213 u8 ick[FILS_ICK_MAX_LEN];
4214 size_t ick_len;
4215 int res;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004216 struct wpabuf *dh_ss = NULL;
4217 const u8 *g_sta = NULL;
4218 size_t g_sta_len = 0;
4219 const u8 *g_ap = NULL;
Hai Shalom60840252021-02-19 19:02:11 -08004220 size_t g_ap_len = 0, kdk_len;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004221 struct wpabuf *pub = NULL;
4222
4223 os_memcpy(sm->bssid, bssid, ETH_ALEN);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004224
4225 wpa_hexdump(MSG_DEBUG, "FILS: Authentication frame fields",
4226 data, len);
4227 pos = data;
4228 end = data + len;
4229
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004230 /* TODO: FILS PK */
4231#ifdef CONFIG_FILS_SK_PFS
4232 if (sm->fils_dh_group) {
4233 u16 group;
4234
4235 /* Using FILS PFS */
4236
4237 /* Finite Cyclic Group */
4238 if (end - pos < 2) {
4239 wpa_printf(MSG_DEBUG,
4240 "FILS: No room for Finite Cyclic Group");
4241 goto fail;
4242 }
4243 group = WPA_GET_LE16(pos);
4244 pos += 2;
4245 if (group != sm->fils_dh_group) {
4246 wpa_printf(MSG_DEBUG,
4247 "FILS: Unexpected change in Finite Cyclic Group: %u (expected %u)",
4248 group, sm->fils_dh_group);
4249 goto fail;
4250 }
4251
4252 /* Element */
4253 if ((size_t) (end - pos) < sm->fils_dh_elem_len) {
4254 wpa_printf(MSG_DEBUG, "FILS: No room for Element");
4255 goto fail;
4256 }
4257
4258 if (!sm->fils_ecdh) {
4259 wpa_printf(MSG_DEBUG, "FILS: No ECDH state available");
4260 goto fail;
4261 }
4262 dh_ss = crypto_ecdh_set_peerkey(sm->fils_ecdh, 1, pos,
4263 sm->fils_dh_elem_len);
4264 if (!dh_ss) {
4265 wpa_printf(MSG_DEBUG, "FILS: ECDH operation failed");
4266 goto fail;
4267 }
4268 wpa_hexdump_buf_key(MSG_DEBUG, "FILS: DH_SS", dh_ss);
4269 g_ap = pos;
4270 g_ap_len = sm->fils_dh_elem_len;
4271 pos += sm->fils_dh_elem_len;
4272 }
4273#endif /* CONFIG_FILS_SK_PFS */
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004274
4275 wpa_hexdump(MSG_DEBUG, "FILS: Remaining IEs", pos, end - pos);
4276 if (ieee802_11_parse_elems(pos, end - pos, &elems, 1) == ParseFailed) {
4277 wpa_printf(MSG_DEBUG, "FILS: Could not parse elements");
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004278 goto fail;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004279 }
4280
4281 /* RSNE */
4282 wpa_hexdump(MSG_DEBUG, "FILS: RSN element", elems.rsn_ie,
4283 elems.rsn_ie_len);
4284 if (!elems.rsn_ie ||
4285 wpa_parse_wpa_ie_rsn(elems.rsn_ie - 2, elems.rsn_ie_len + 2,
4286 &rsn) < 0) {
4287 wpa_printf(MSG_DEBUG, "FILS: No RSN element");
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004288 goto fail;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004289 }
4290
4291 if (!elems.fils_nonce) {
4292 wpa_printf(MSG_DEBUG, "FILS: No FILS Nonce field");
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004293 goto fail;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004294 }
4295 os_memcpy(sm->fils_anonce, elems.fils_nonce, FILS_NONCE_LEN);
4296 wpa_hexdump(MSG_DEBUG, "FILS: ANonce", sm->fils_anonce, FILS_NONCE_LEN);
4297
Roshan Pius3a1667e2018-07-03 15:17:14 -07004298#ifdef CONFIG_IEEE80211R
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004299 if (wpa_key_mgmt_ft(sm->key_mgmt)) {
4300 struct wpa_ft_ies parse;
4301
4302 if (!elems.mdie || !elems.ftie) {
4303 wpa_printf(MSG_DEBUG, "FILS+FT: No MDE or FTE");
4304 goto fail;
4305 }
4306
Roshan Pius3a1667e2018-07-03 15:17:14 -07004307 if (wpa_ft_parse_ies(pos, end - pos, &parse,
4308 wpa_key_mgmt_sha384(sm->key_mgmt)) < 0) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004309 wpa_printf(MSG_DEBUG, "FILS+FT: Failed to parse IEs");
4310 goto fail;
4311 }
4312
4313 if (!parse.r0kh_id) {
4314 wpa_printf(MSG_DEBUG,
4315 "FILS+FT: No R0KH-ID subelem in FTE");
4316 goto fail;
4317 }
4318 os_memcpy(sm->r0kh_id, parse.r0kh_id, parse.r0kh_id_len);
4319 sm->r0kh_id_len = parse.r0kh_id_len;
4320 wpa_hexdump_ascii(MSG_DEBUG, "FILS+FT: R0KH-ID",
4321 sm->r0kh_id, sm->r0kh_id_len);
4322
4323 if (!parse.r1kh_id) {
4324 wpa_printf(MSG_DEBUG,
4325 "FILS+FT: No R1KH-ID subelem in FTE");
4326 goto fail;
4327 }
4328 os_memcpy(sm->r1kh_id, parse.r1kh_id, FT_R1KH_ID_LEN);
4329 wpa_hexdump(MSG_DEBUG, "FILS+FT: R1KH-ID",
4330 sm->r1kh_id, FT_R1KH_ID_LEN);
4331
4332 /* TODO: Check MDE and FTE payload */
4333
4334 wpabuf_free(sm->fils_ft_ies);
4335 sm->fils_ft_ies = wpabuf_alloc(2 + elems.mdie_len +
4336 2 + elems.ftie_len);
4337 if (!sm->fils_ft_ies)
4338 goto fail;
4339 wpabuf_put_data(sm->fils_ft_ies, elems.mdie - 2,
4340 2 + elems.mdie_len);
4341 wpabuf_put_data(sm->fils_ft_ies, elems.ftie - 2,
4342 2 + elems.ftie_len);
4343 } else {
4344 wpabuf_free(sm->fils_ft_ies);
4345 sm->fils_ft_ies = NULL;
4346 }
Roshan Pius3a1667e2018-07-03 15:17:14 -07004347#endif /* CONFIG_IEEE80211R */
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004348
4349 /* PMKID List */
4350 if (rsn.pmkid && rsn.num_pmkid > 0) {
4351 wpa_hexdump(MSG_DEBUG, "FILS: PMKID List",
4352 rsn.pmkid, rsn.num_pmkid * PMKID_LEN);
4353
4354 if (rsn.num_pmkid != 1) {
4355 wpa_printf(MSG_DEBUG, "FILS: Invalid PMKID selection");
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004356 goto fail;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004357 }
4358 wpa_hexdump(MSG_DEBUG, "FILS: PMKID", rsn.pmkid, PMKID_LEN);
4359 if (os_memcmp(sm->cur_pmksa->pmkid, rsn.pmkid, PMKID_LEN) != 0)
4360 {
4361 wpa_printf(MSG_DEBUG, "FILS: PMKID mismatch");
4362 wpa_hexdump(MSG_DEBUG, "FILS: Expected PMKID",
4363 sm->cur_pmksa->pmkid, PMKID_LEN);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004364 goto fail;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004365 }
4366 wpa_printf(MSG_DEBUG,
4367 "FILS: Matching PMKID - continue using PMKSA caching");
4368 pmkid_match = 1;
4369 }
4370 if (!pmkid_match && sm->cur_pmksa) {
4371 wpa_printf(MSG_DEBUG,
4372 "FILS: No PMKID match - cannot use cached PMKSA entry");
4373 sm->cur_pmksa = NULL;
4374 }
4375
4376 /* FILS Session */
4377 if (!elems.fils_session) {
4378 wpa_printf(MSG_DEBUG, "FILS: No FILS Session element");
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004379 goto fail;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004380 }
4381 wpa_hexdump(MSG_DEBUG, "FILS: FILS Session", elems.fils_session,
4382 FILS_SESSION_LEN);
4383 if (os_memcmp(sm->fils_session, elems.fils_session, FILS_SESSION_LEN)
4384 != 0) {
4385 wpa_printf(MSG_DEBUG, "FILS: Session mismatch");
4386 wpa_hexdump(MSG_DEBUG, "FILS: Expected FILS Session",
4387 sm->fils_session, FILS_SESSION_LEN);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004388 goto fail;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004389 }
4390
Hai Shalomfdcde762020-04-02 11:19:20 -07004391 /* Wrapped Data */
4392 if (!sm->cur_pmksa && elems.wrapped_data) {
Paul Stewart092955c2017-02-06 09:13:09 -08004393 u8 rmsk[ERP_MAX_KEY_LEN];
4394 size_t rmsk_len;
4395
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004396 wpa_hexdump(MSG_DEBUG, "FILS: Wrapped Data",
Hai Shalomfdcde762020-04-02 11:19:20 -07004397 elems.wrapped_data,
4398 elems.wrapped_data_len);
4399 eapol_sm_process_erp_finish(sm->eapol, elems.wrapped_data,
4400 elems.wrapped_data_len);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004401 if (eapol_sm_failed(sm->eapol))
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004402 goto fail;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004403
Paul Stewart092955c2017-02-06 09:13:09 -08004404 rmsk_len = ERP_MAX_KEY_LEN;
4405 res = eapol_sm_get_key(sm->eapol, rmsk, rmsk_len);
4406 if (res == PMK_LEN) {
4407 rmsk_len = PMK_LEN;
4408 res = eapol_sm_get_key(sm->eapol, rmsk, rmsk_len);
4409 }
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004410 if (res)
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004411 goto fail;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004412
Paul Stewart092955c2017-02-06 09:13:09 -08004413 res = fils_rmsk_to_pmk(sm->key_mgmt, rmsk, rmsk_len,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004414 sm->fils_nonce, sm->fils_anonce,
4415 dh_ss ? wpabuf_head(dh_ss) : NULL,
4416 dh_ss ? wpabuf_len(dh_ss) : 0,
Paul Stewart092955c2017-02-06 09:13:09 -08004417 sm->pmk, &sm->pmk_len);
Hai Shalom81f62d82019-07-22 12:10:00 -07004418 forced_memzero(rmsk, sizeof(rmsk));
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004419
4420 /* Don't use DHss in PTK derivation if PMKSA caching is not
4421 * used. */
4422 wpabuf_clear_free(dh_ss);
4423 dh_ss = NULL;
4424
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08004425 if (res)
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004426 goto fail;
Paul Stewart092955c2017-02-06 09:13:09 -08004427
4428 if (!sm->fils_erp_pmkid_set) {
4429 wpa_printf(MSG_DEBUG, "FILS: PMKID not available");
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004430 goto fail;
Paul Stewart092955c2017-02-06 09:13:09 -08004431 }
4432 wpa_hexdump(MSG_DEBUG, "FILS: PMKID", sm->fils_erp_pmkid,
4433 PMKID_LEN);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004434 wpa_printf(MSG_DEBUG, "FILS: ERP processing succeeded - add PMKSA cache entry for the result");
Paul Stewart092955c2017-02-06 09:13:09 -08004435 sm->cur_pmksa = pmksa_cache_add(sm->pmksa, sm->pmk, sm->pmk_len,
4436 sm->fils_erp_pmkid, NULL, 0,
4437 sm->bssid, sm->own_addr,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004438 sm->network_ctx, sm->key_mgmt,
4439 NULL);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004440 }
4441
4442 if (!sm->cur_pmksa) {
4443 wpa_printf(MSG_DEBUG,
4444 "FILS: No remaining options to continue FILS authentication");
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004445 goto fail;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004446 }
4447
Hai Shalom60840252021-02-19 19:02:11 -08004448 if (sm->force_kdk_derivation ||
Hai Shalomc1a21442022-02-04 13:43:00 -08004449 (sm->secure_ltf &&
4450 ieee802_11_rsnx_capab(sm->ap_rsnxe, WLAN_RSNX_CAPAB_SECURE_LTF)))
Hai Shalom60840252021-02-19 19:02:11 -08004451 kdk_len = WPA_KDK_MAX_LEN;
4452 else
4453 kdk_len = 0;
4454
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004455 if (fils_pmk_to_ptk(sm->pmk, sm->pmk_len, sm->own_addr, sm->bssid,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004456 sm->fils_nonce, sm->fils_anonce,
4457 dh_ss ? wpabuf_head(dh_ss) : NULL,
4458 dh_ss ? wpabuf_len(dh_ss) : 0,
4459 &sm->ptk, ick, &ick_len,
4460 sm->key_mgmt, sm->pairwise_cipher,
Hai Shalom60840252021-02-19 19:02:11 -08004461 sm->fils_ft, &sm->fils_ft_len,
4462 kdk_len) < 0) {
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004463 wpa_printf(MSG_DEBUG, "FILS: Failed to derive PTK");
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004464 goto fail;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004465 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004466
4467 wpabuf_clear_free(dh_ss);
4468 dh_ss = NULL;
4469
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004470 sm->ptk_set = 1;
4471 sm->tptk_set = 0;
4472 os_memset(&sm->tptk, 0, sizeof(sm->tptk));
4473
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004474#ifdef CONFIG_FILS_SK_PFS
4475 if (sm->fils_dh_group) {
4476 if (!sm->fils_ecdh) {
4477 wpa_printf(MSG_INFO, "FILS: ECDH not initialized");
4478 goto fail;
4479 }
4480 pub = crypto_ecdh_get_pubkey(sm->fils_ecdh, 1);
4481 if (!pub)
4482 goto fail;
4483 wpa_hexdump_buf(MSG_DEBUG, "FILS: gSTA", pub);
4484 g_sta = wpabuf_head(pub);
4485 g_sta_len = wpabuf_len(pub);
4486 if (!g_ap) {
4487 wpa_printf(MSG_INFO, "FILS: gAP not available");
4488 goto fail;
4489 }
4490 wpa_hexdump(MSG_DEBUG, "FILS: gAP", g_ap, g_ap_len);
4491 }
4492#endif /* CONFIG_FILS_SK_PFS */
4493
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004494 res = fils_key_auth_sk(ick, ick_len, sm->fils_nonce,
4495 sm->fils_anonce, sm->own_addr, sm->bssid,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004496 g_sta, g_sta_len, g_ap, g_ap_len,
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004497 sm->key_mgmt, sm->fils_key_auth_sta,
4498 sm->fils_key_auth_ap,
4499 &sm->fils_key_auth_len);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004500 wpabuf_free(pub);
Hai Shalom81f62d82019-07-22 12:10:00 -07004501 forced_memzero(ick, sizeof(ick));
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004502 return res;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004503fail:
4504 wpabuf_free(pub);
4505 wpabuf_clear_free(dh_ss);
4506 return -1;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004507}
4508
4509
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004510#ifdef CONFIG_IEEE80211R
4511static int fils_ft_build_assoc_req_rsne(struct wpa_sm *sm, struct wpabuf *buf)
4512{
4513 struct rsn_ie_hdr *rsnie;
4514 u16 capab;
4515 u8 *pos;
Roshan Pius3a1667e2018-07-03 15:17:14 -07004516 int use_sha384 = wpa_key_mgmt_sha384(sm->key_mgmt);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004517
4518 /* RSNIE[PMKR0Name/PMKR1Name] */
4519 rsnie = wpabuf_put(buf, sizeof(*rsnie));
4520 rsnie->elem_id = WLAN_EID_RSN;
4521 WPA_PUT_LE16(rsnie->version, RSN_VERSION);
4522
4523 /* Group Suite Selector */
4524 if (!wpa_cipher_valid_group(sm->group_cipher)) {
4525 wpa_printf(MSG_WARNING, "FT: Invalid group cipher (%d)",
4526 sm->group_cipher);
4527 return -1;
4528 }
4529 pos = wpabuf_put(buf, RSN_SELECTOR_LEN);
4530 RSN_SELECTOR_PUT(pos, wpa_cipher_to_suite(WPA_PROTO_RSN,
4531 sm->group_cipher));
4532
4533 /* Pairwise Suite Count */
4534 wpabuf_put_le16(buf, 1);
4535
4536 /* Pairwise Suite List */
4537 if (!wpa_cipher_valid_pairwise(sm->pairwise_cipher)) {
4538 wpa_printf(MSG_WARNING, "FT: Invalid pairwise cipher (%d)",
4539 sm->pairwise_cipher);
4540 return -1;
4541 }
4542 pos = wpabuf_put(buf, RSN_SELECTOR_LEN);
4543 RSN_SELECTOR_PUT(pos, wpa_cipher_to_suite(WPA_PROTO_RSN,
4544 sm->pairwise_cipher));
4545
4546 /* Authenticated Key Management Suite Count */
4547 wpabuf_put_le16(buf, 1);
4548
4549 /* Authenticated Key Management Suite List */
4550 pos = wpabuf_put(buf, RSN_SELECTOR_LEN);
4551 if (sm->key_mgmt == WPA_KEY_MGMT_FT_FILS_SHA256)
4552 RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_FT_FILS_SHA256);
4553 else if (sm->key_mgmt == WPA_KEY_MGMT_FT_FILS_SHA384)
4554 RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_FT_FILS_SHA384);
4555 else {
4556 wpa_printf(MSG_WARNING,
4557 "FILS+FT: Invalid key management type (%d)",
4558 sm->key_mgmt);
4559 return -1;
4560 }
4561
4562 /* RSN Capabilities */
4563 capab = 0;
Hai Shalomc3565922019-10-28 11:58:20 -07004564 if (sm->mfp)
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004565 capab |= WPA_CAPABILITY_MFPC;
Hai Shalomc3565922019-10-28 11:58:20 -07004566 if (sm->mfp == 2)
4567 capab |= WPA_CAPABILITY_MFPR;
Hai Shalom74f70d42019-02-11 14:42:39 -08004568 if (sm->ocv)
4569 capab |= WPA_CAPABILITY_OCVC;
Hai Shalomfdcde762020-04-02 11:19:20 -07004570 if (sm->ext_key_id)
4571 capab |= WPA_CAPABILITY_EXT_KEY_ID_FOR_UNICAST;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004572 wpabuf_put_le16(buf, capab);
4573
4574 /* PMKID Count */
4575 wpabuf_put_le16(buf, 1);
4576
4577 /* PMKID List [PMKR1Name] */
4578 wpa_hexdump_key(MSG_DEBUG, "FILS+FT: XXKey (FILS-FT)",
4579 sm->fils_ft, sm->fils_ft_len);
4580 wpa_hexdump_ascii(MSG_DEBUG, "FILS+FT: SSID", sm->ssid, sm->ssid_len);
4581 wpa_hexdump(MSG_DEBUG, "FILS+FT: MDID",
4582 sm->mobility_domain, MOBILITY_DOMAIN_ID_LEN);
4583 wpa_hexdump_ascii(MSG_DEBUG, "FILS+FT: R0KH-ID",
4584 sm->r0kh_id, sm->r0kh_id_len);
4585 if (wpa_derive_pmk_r0(sm->fils_ft, sm->fils_ft_len, sm->ssid,
4586 sm->ssid_len, sm->mobility_domain,
4587 sm->r0kh_id, sm->r0kh_id_len, sm->own_addr,
Roshan Pius3a1667e2018-07-03 15:17:14 -07004588 sm->pmk_r0, sm->pmk_r0_name, use_sha384) < 0) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004589 wpa_printf(MSG_WARNING, "FILS+FT: Could not derive PMK-R0");
4590 return -1;
4591 }
Roshan Pius3a1667e2018-07-03 15:17:14 -07004592 sm->pmk_r0_len = use_sha384 ? SHA384_MAC_LEN : PMK_LEN;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004593 wpa_printf(MSG_DEBUG, "FILS+FT: R1KH-ID: " MACSTR,
4594 MAC2STR(sm->r1kh_id));
4595 pos = wpabuf_put(buf, WPA_PMK_NAME_LEN);
4596 if (wpa_derive_pmk_r1_name(sm->pmk_r0_name, sm->r1kh_id, sm->own_addr,
Hai Shalom021b0b52019-04-10 11:17:58 -07004597 sm->pmk_r1_name, use_sha384) < 0) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004598 wpa_printf(MSG_WARNING, "FILS+FT: Could not derive PMKR1Name");
4599 return -1;
4600 }
Hai Shalom021b0b52019-04-10 11:17:58 -07004601 os_memcpy(pos, sm->pmk_r1_name, WPA_PMK_NAME_LEN);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004602
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004603 if (sm->mgmt_group_cipher == WPA_CIPHER_AES_128_CMAC) {
4604 /* Management Group Cipher Suite */
4605 pos = wpabuf_put(buf, RSN_SELECTOR_LEN);
4606 RSN_SELECTOR_PUT(pos, RSN_CIPHER_SUITE_AES_128_CMAC);
4607 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004608
4609 rsnie->len = ((u8 *) wpabuf_put(buf, 0) - (u8 *) rsnie) - 2;
4610 return 0;
4611}
4612#endif /* CONFIG_IEEE80211R */
4613
4614
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004615struct wpabuf * fils_build_assoc_req(struct wpa_sm *sm, const u8 **kek,
4616 size_t *kek_len, const u8 **snonce,
Paul Stewart092955c2017-02-06 09:13:09 -08004617 const u8 **anonce,
4618 const struct wpabuf **hlp,
4619 unsigned int num_hlp)
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004620{
4621 struct wpabuf *buf;
Paul Stewart092955c2017-02-06 09:13:09 -08004622 size_t len;
4623 unsigned int i;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004624
Paul Stewart092955c2017-02-06 09:13:09 -08004625 len = 1000;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004626#ifdef CONFIG_IEEE80211R
4627 if (sm->fils_ft_ies)
4628 len += wpabuf_len(sm->fils_ft_ies);
4629 if (wpa_key_mgmt_ft(sm->key_mgmt))
4630 len += 256;
4631#endif /* CONFIG_IEEE80211R */
Paul Stewart092955c2017-02-06 09:13:09 -08004632 for (i = 0; hlp && i < num_hlp; i++)
4633 len += 10 + wpabuf_len(hlp[i]);
4634 buf = wpabuf_alloc(len);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004635 if (!buf)
4636 return NULL;
4637
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004638#ifdef CONFIG_IEEE80211R
4639 if (wpa_key_mgmt_ft(sm->key_mgmt) && sm->fils_ft_ies) {
4640 /* MDE and FTE when using FILS+FT */
4641 wpabuf_put_buf(buf, sm->fils_ft_ies);
4642 /* RSNE with PMKR1Name in PMKID field */
4643 if (fils_ft_build_assoc_req_rsne(sm, buf) < 0) {
4644 wpabuf_free(buf);
4645 return NULL;
4646 }
4647 }
4648#endif /* CONFIG_IEEE80211R */
4649
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004650 /* FILS Session */
4651 wpabuf_put_u8(buf, WLAN_EID_EXTENSION); /* Element ID */
4652 wpabuf_put_u8(buf, 1 + FILS_SESSION_LEN); /* Length */
4653 /* Element ID Extension */
4654 wpabuf_put_u8(buf, WLAN_EID_EXT_FILS_SESSION);
4655 wpabuf_put_data(buf, sm->fils_session, FILS_SESSION_LEN);
4656
4657 /* Everything after FILS Session element gets encrypted in the driver
4658 * with KEK. The buffer returned from here is the plaintext version. */
4659
4660 /* TODO: FILS Public Key */
4661
4662 /* FILS Key Confirm */
4663 wpabuf_put_u8(buf, WLAN_EID_EXTENSION); /* Element ID */
4664 wpabuf_put_u8(buf, 1 + sm->fils_key_auth_len); /* Length */
4665 /* Element ID Extension */
4666 wpabuf_put_u8(buf, WLAN_EID_EXT_FILS_KEY_CONFIRM);
4667 wpabuf_put_data(buf, sm->fils_key_auth_sta, sm->fils_key_auth_len);
4668
Paul Stewart092955c2017-02-06 09:13:09 -08004669 /* FILS HLP Container */
4670 for (i = 0; hlp && i < num_hlp; i++) {
4671 const u8 *pos = wpabuf_head(hlp[i]);
4672 size_t left = wpabuf_len(hlp[i]);
4673
4674 wpabuf_put_u8(buf, WLAN_EID_EXTENSION); /* Element ID */
4675 if (left <= 254)
4676 len = 1 + left;
4677 else
4678 len = 255;
4679 wpabuf_put_u8(buf, len); /* Length */
4680 /* Element ID Extension */
4681 wpabuf_put_u8(buf, WLAN_EID_EXT_FILS_HLP_CONTAINER);
4682 /* Destination MAC Address, Source MAC Address, HLP Packet.
4683 * HLP Packet is in MSDU format (i.e., included the LLC/SNAP
4684 * header when LPD is used). */
4685 wpabuf_put_data(buf, pos, len - 1);
4686 pos += len - 1;
4687 left -= len - 1;
4688 while (left) {
4689 wpabuf_put_u8(buf, WLAN_EID_FRAGMENT);
4690 len = left > 255 ? 255 : left;
4691 wpabuf_put_u8(buf, len);
4692 wpabuf_put_data(buf, pos, len);
4693 pos += len;
4694 left -= len;
4695 }
4696 }
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004697
4698 /* TODO: FILS IP Address Assignment */
4699
Hai Shalom74f70d42019-02-11 14:42:39 -08004700#ifdef CONFIG_OCV
4701 if (wpa_sm_ocv_enabled(sm)) {
4702 struct wpa_channel_info ci;
4703 u8 *pos;
4704
4705 if (wpa_sm_channel_info(sm, &ci) != 0) {
4706 wpa_printf(MSG_WARNING,
4707 "FILS: Failed to get channel info for OCI element");
4708 wpabuf_free(buf);
4709 return NULL;
4710 }
Hai Shalom899fcc72020-10-19 14:38:18 -07004711#ifdef CONFIG_TESTING_OPTIONS
4712 if (sm->oci_freq_override_fils_assoc) {
4713 wpa_printf(MSG_INFO,
4714 "TEST: Override OCI KDE frequency %d -> %d MHz",
4715 ci.frequency,
4716 sm->oci_freq_override_fils_assoc);
4717 ci.frequency = sm->oci_freq_override_fils_assoc;
4718 }
4719#endif /* CONFIG_TESTING_OPTIONS */
Hai Shalom74f70d42019-02-11 14:42:39 -08004720
4721 pos = wpabuf_put(buf, OCV_OCI_EXTENDED_LEN);
4722 if (ocv_insert_extended_oci(&ci, pos) < 0) {
4723 wpabuf_free(buf);
4724 return NULL;
4725 }
4726 }
4727#endif /* CONFIG_OCV */
4728
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004729 wpa_hexdump_buf(MSG_DEBUG, "FILS: Association Request plaintext", buf);
4730
4731 *kek = sm->ptk.kek;
4732 *kek_len = sm->ptk.kek_len;
4733 wpa_hexdump_key(MSG_DEBUG, "FILS: KEK for AEAD", *kek, *kek_len);
4734 *snonce = sm->fils_nonce;
4735 wpa_hexdump(MSG_DEBUG, "FILS: SNonce for AEAD AAD",
4736 *snonce, FILS_NONCE_LEN);
4737 *anonce = sm->fils_anonce;
4738 wpa_hexdump(MSG_DEBUG, "FILS: ANonce for AEAD AAD",
4739 *anonce, FILS_NONCE_LEN);
4740
4741 return buf;
4742}
4743
4744
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08004745static void fils_process_hlp_resp(struct wpa_sm *sm, const u8 *resp, size_t len)
4746{
4747 const u8 *pos, *end;
4748
4749 wpa_hexdump(MSG_MSGDUMP, "FILS: HLP response", resp, len);
4750 if (len < 2 * ETH_ALEN)
4751 return;
4752 pos = resp + 2 * ETH_ALEN;
4753 end = resp + len;
4754 if (end - pos >= 6 &&
4755 os_memcmp(pos, "\xaa\xaa\x03\x00\x00\x00", 6) == 0)
4756 pos += 6; /* Remove SNAP/LLC header */
4757 wpa_sm_fils_hlp_rx(sm, resp, resp + ETH_ALEN, pos, end - pos);
4758}
4759
4760
4761static void fils_process_hlp_container(struct wpa_sm *sm, const u8 *pos,
4762 size_t len)
4763{
4764 const u8 *end = pos + len;
4765 u8 *tmp, *tmp_pos;
4766
4767 /* Check if there are any FILS HLP Container elements */
4768 while (end - pos >= 2) {
4769 if (2 + pos[1] > end - pos)
4770 return;
4771 if (pos[0] == WLAN_EID_EXTENSION &&
4772 pos[1] >= 1 + 2 * ETH_ALEN &&
4773 pos[2] == WLAN_EID_EXT_FILS_HLP_CONTAINER)
4774 break;
4775 pos += 2 + pos[1];
4776 }
4777 if (end - pos < 2)
4778 return; /* No FILS HLP Container elements */
4779
4780 tmp = os_malloc(end - pos);
4781 if (!tmp)
4782 return;
4783
4784 while (end - pos >= 2) {
4785 if (2 + pos[1] > end - pos ||
4786 pos[0] != WLAN_EID_EXTENSION ||
4787 pos[1] < 1 + 2 * ETH_ALEN ||
4788 pos[2] != WLAN_EID_EXT_FILS_HLP_CONTAINER)
4789 break;
4790 tmp_pos = tmp;
4791 os_memcpy(tmp_pos, pos + 3, pos[1] - 1);
4792 tmp_pos += pos[1] - 1;
4793 pos += 2 + pos[1];
4794
4795 /* Add possible fragments */
4796 while (end - pos >= 2 && pos[0] == WLAN_EID_FRAGMENT &&
4797 2 + pos[1] <= end - pos) {
4798 os_memcpy(tmp_pos, pos + 2, pos[1]);
4799 tmp_pos += pos[1];
4800 pos += 2 + pos[1];
4801 }
4802
4803 fils_process_hlp_resp(sm, tmp, tmp_pos - tmp);
4804 }
4805
4806 os_free(tmp);
4807}
4808
4809
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004810int fils_process_assoc_resp(struct wpa_sm *sm, const u8 *resp, size_t len)
4811{
4812 const struct ieee80211_mgmt *mgmt;
4813 const u8 *end, *ie_start;
4814 struct ieee802_11_elems elems;
4815 int keylen, rsclen;
4816 enum wpa_alg alg;
4817 struct wpa_gtk_data gd;
4818 int maxkeylen;
4819 struct wpa_eapol_ie_parse kde;
4820
4821 if (!sm || !sm->ptk_set) {
4822 wpa_printf(MSG_DEBUG, "FILS: No KEK available");
4823 return -1;
4824 }
4825
4826 if (!wpa_key_mgmt_fils(sm->key_mgmt)) {
4827 wpa_printf(MSG_DEBUG, "FILS: Not a FILS AKM");
4828 return -1;
4829 }
4830
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004831 if (sm->fils_completed) {
4832 wpa_printf(MSG_DEBUG,
4833 "FILS: Association has already been completed for this FILS authentication - ignore unexpected retransmission");
4834 return -1;
4835 }
4836
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004837 wpa_hexdump(MSG_DEBUG, "FILS: (Re)Association Response frame",
4838 resp, len);
4839
4840 mgmt = (const struct ieee80211_mgmt *) resp;
4841 if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.assoc_resp))
4842 return -1;
4843
4844 end = resp + len;
4845 /* Same offset for Association Response and Reassociation Response */
4846 ie_start = mgmt->u.assoc_resp.variable;
4847
4848 if (ieee802_11_parse_elems(ie_start, end - ie_start, &elems, 1) ==
4849 ParseFailed) {
4850 wpa_printf(MSG_DEBUG,
4851 "FILS: Failed to parse decrypted elements");
4852 goto fail;
4853 }
4854
4855 if (!elems.fils_session) {
4856 wpa_printf(MSG_DEBUG, "FILS: No FILS Session element");
4857 return -1;
4858 }
4859 if (os_memcmp(elems.fils_session, sm->fils_session,
4860 FILS_SESSION_LEN) != 0) {
4861 wpa_printf(MSG_DEBUG, "FILS: FILS Session mismatch");
4862 wpa_hexdump(MSG_DEBUG, "FILS: Received FILS Session",
4863 elems.fils_session, FILS_SESSION_LEN);
4864 wpa_hexdump(MSG_DEBUG, "FILS: Expected FILS Session",
4865 sm->fils_session, FILS_SESSION_LEN);
4866 }
4867
Hai Shalom81f62d82019-07-22 12:10:00 -07004868 if (!elems.rsn_ie) {
4869 wpa_printf(MSG_DEBUG,
4870 "FILS: No RSNE in (Re)Association Response");
4871 /* As an interop workaround, allow this for now since IEEE Std
4872 * 802.11ai-2016 did not include all the needed changes to make
4873 * a FILS AP include RSNE in the frame. This workaround might
4874 * eventually be removed and replaced with rejection (goto fail)
4875 * to follow a strict interpretation of the standard. */
4876 } else if (wpa_compare_rsn_ie(wpa_key_mgmt_ft(sm->key_mgmt),
4877 sm->ap_rsn_ie, sm->ap_rsn_ie_len,
4878 elems.rsn_ie - 2, elems.rsn_ie_len + 2)) {
4879 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
4880 "FILS: RSNE mismatch between Beacon/Probe Response and (Re)Association Response");
4881 wpa_hexdump(MSG_DEBUG, "FILS: RSNE in Beacon/Probe Response",
4882 sm->ap_rsn_ie, sm->ap_rsn_ie_len);
4883 wpa_hexdump(MSG_DEBUG, "FILS: RSNE in (Re)Association Response",
4884 elems.rsn_ie, elems.rsn_ie_len);
4885 goto fail;
4886 }
4887
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004888 /* TODO: FILS Public Key */
4889
4890 if (!elems.fils_key_confirm) {
4891 wpa_printf(MSG_DEBUG, "FILS: No FILS Key Confirm element");
4892 goto fail;
4893 }
4894 if (elems.fils_key_confirm_len != sm->fils_key_auth_len) {
4895 wpa_printf(MSG_DEBUG,
4896 "FILS: Unexpected Key-Auth length %d (expected %d)",
4897 elems.fils_key_confirm_len,
4898 (int) sm->fils_key_auth_len);
4899 goto fail;
4900 }
4901 if (os_memcmp(elems.fils_key_confirm, sm->fils_key_auth_ap,
4902 sm->fils_key_auth_len) != 0) {
4903 wpa_printf(MSG_DEBUG, "FILS: Key-Auth mismatch");
4904 wpa_hexdump(MSG_DEBUG, "FILS: Received Key-Auth",
4905 elems.fils_key_confirm,
4906 elems.fils_key_confirm_len);
4907 wpa_hexdump(MSG_DEBUG, "FILS: Expected Key-Auth",
4908 sm->fils_key_auth_ap, sm->fils_key_auth_len);
4909 goto fail;
4910 }
4911
Hai Shalom74f70d42019-02-11 14:42:39 -08004912#ifdef CONFIG_OCV
4913 if (wpa_sm_ocv_enabled(sm)) {
4914 struct wpa_channel_info ci;
4915
4916 if (wpa_sm_channel_info(sm, &ci) != 0) {
4917 wpa_printf(MSG_WARNING,
4918 "Failed to get channel info to validate received OCI in FILS (Re)Association Response frame");
4919 goto fail;
4920 }
4921
4922 if (ocv_verify_tx_params(elems.oci, elems.oci_len, &ci,
4923 channel_width_to_int(ci.chanwidth),
Hai Shalom899fcc72020-10-19 14:38:18 -07004924 ci.seg1_idx) != OCI_SUCCESS) {
4925 wpa_msg(sm->ctx->msg_ctx, MSG_INFO, OCV_FAILURE
4926 "addr=" MACSTR " frame=fils-assoc error=%s",
4927 MAC2STR(sm->bssid), ocv_errorstr);
Hai Shalom74f70d42019-02-11 14:42:39 -08004928 goto fail;
4929 }
4930 }
4931#endif /* CONFIG_OCV */
4932
Hai Shalom021b0b52019-04-10 11:17:58 -07004933#ifdef CONFIG_IEEE80211R
4934 if (wpa_key_mgmt_ft(sm->key_mgmt) && sm->fils_ft_ies) {
4935 struct wpa_ie_data rsn;
4936
4937 /* Check that PMKR1Name derived by the AP matches */
4938 if (!elems.rsn_ie ||
4939 wpa_parse_wpa_ie_rsn(elems.rsn_ie - 2, elems.rsn_ie_len + 2,
4940 &rsn) < 0 ||
4941 !rsn.pmkid || rsn.num_pmkid != 1 ||
4942 os_memcmp(rsn.pmkid, sm->pmk_r1_name,
4943 WPA_PMK_NAME_LEN) != 0) {
4944 wpa_printf(MSG_DEBUG,
4945 "FILS+FT: No RSNE[PMKR1Name] match in AssocResp");
4946 goto fail;
4947 }
4948 }
4949#endif /* CONFIG_IEEE80211R */
4950
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004951 /* Key Delivery */
4952 if (!elems.key_delivery) {
4953 wpa_printf(MSG_DEBUG, "FILS: No Key Delivery element");
4954 goto fail;
4955 }
4956
4957 /* Parse GTK and set the key to the driver */
4958 os_memset(&gd, 0, sizeof(gd));
4959 if (wpa_supplicant_parse_ies(elems.key_delivery + WPA_KEY_RSC_LEN,
4960 elems.key_delivery_len - WPA_KEY_RSC_LEN,
4961 &kde) < 0) {
4962 wpa_printf(MSG_DEBUG, "FILS: Failed to parse KDEs");
4963 goto fail;
4964 }
4965 if (!kde.gtk) {
4966 wpa_printf(MSG_DEBUG, "FILS: No GTK KDE");
4967 goto fail;
4968 }
4969 maxkeylen = gd.gtk_len = kde.gtk_len - 2;
4970 if (wpa_supplicant_check_group_cipher(sm, sm->group_cipher,
4971 gd.gtk_len, maxkeylen,
4972 &gd.key_rsc_len, &gd.alg))
4973 goto fail;
4974
4975 wpa_hexdump_key(MSG_DEBUG, "FILS: Received GTK", kde.gtk, kde.gtk_len);
4976 gd.keyidx = kde.gtk[0] & 0x3;
4977 gd.tx = wpa_supplicant_gtk_tx_bit_workaround(sm,
4978 !!(kde.gtk[0] & BIT(2)));
4979 if (kde.gtk_len - 2 > sizeof(gd.gtk)) {
4980 wpa_printf(MSG_DEBUG, "FILS: Too long GTK in GTK KDE (len=%lu)",
4981 (unsigned long) kde.gtk_len - 2);
4982 goto fail;
4983 }
4984 os_memcpy(gd.gtk, kde.gtk + 2, kde.gtk_len - 2);
4985
4986 wpa_printf(MSG_DEBUG, "FILS: Set GTK to driver");
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004987 if (wpa_supplicant_install_gtk(sm, &gd, elems.key_delivery, 0) < 0) {
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004988 wpa_printf(MSG_DEBUG, "FILS: Failed to set GTK");
4989 goto fail;
4990 }
4991
4992 if (ieee80211w_set_keys(sm, &kde) < 0) {
4993 wpa_printf(MSG_DEBUG, "FILS: Failed to set IGTK");
4994 goto fail;
4995 }
4996
4997 alg = wpa_cipher_to_alg(sm->pairwise_cipher);
4998 keylen = wpa_cipher_key_len(sm->pairwise_cipher);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004999 if (keylen <= 0 || (unsigned int) keylen != sm->ptk.tk_len) {
5000 wpa_printf(MSG_DEBUG, "FILS: TK length mismatch: %u != %lu",
5001 keylen, (long unsigned int) sm->ptk.tk_len);
5002 goto fail;
5003 }
Hai Shalomfdcde762020-04-02 11:19:20 -07005004
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08005005 rsclen = wpa_cipher_rsc_len(sm->pairwise_cipher);
5006 wpa_hexdump_key(MSG_DEBUG, "FILS: Set TK to driver",
5007 sm->ptk.tk, keylen);
5008 if (wpa_sm_set_key(sm, alg, sm->bssid, 0, 1, null_rsc, rsclen,
Hai Shalomfdcde762020-04-02 11:19:20 -07005009 sm->ptk.tk, keylen, KEY_FLAG_PAIRWISE_RX_TX) < 0) {
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08005010 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
5011 "FILS: Failed to set PTK to the driver (alg=%d keylen=%d bssid="
5012 MACSTR ")",
5013 alg, keylen, MAC2STR(sm->bssid));
5014 goto fail;
5015 }
5016
Hai Shalom60840252021-02-19 19:02:11 -08005017 wpa_sm_store_ptk(sm, sm->bssid, sm->pairwise_cipher,
5018 sm->dot11RSNAConfigPMKLifetime, &sm->ptk);
5019
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08005020 /* TODO: TK could be cleared after auth frame exchange now that driver
5021 * takes care of association frame encryption/decryption. */
5022 /* TK is not needed anymore in supplicant */
5023 os_memset(sm->ptk.tk, 0, WPA_TK_MAX_LEN);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005024 sm->ptk.tk_len = 0;
Mathy Vanhoefc66556c2017-09-29 04:22:51 +02005025 sm->ptk.installed = 1;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08005026
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08005027 /* FILS HLP Container */
5028 fils_process_hlp_container(sm, ie_start, end - ie_start);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08005029
5030 /* TODO: FILS IP Address Assignment */
5031
5032 wpa_printf(MSG_DEBUG, "FILS: Auth+Assoc completed successfully");
5033 sm->fils_completed = 1;
Hai Shalom81f62d82019-07-22 12:10:00 -07005034 forced_memzero(&gd, sizeof(gd));
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08005035
Hai Shalomfdcde762020-04-02 11:19:20 -07005036 if (kde.transition_disable)
5037 wpa_sm_transition_disable(sm, kde.transition_disable[0]);
5038
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08005039 return 0;
5040fail:
Hai Shalom81f62d82019-07-22 12:10:00 -07005041 forced_memzero(&gd, sizeof(gd));
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08005042 return -1;
5043}
5044
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005045
5046void wpa_sm_set_reset_fils_completed(struct wpa_sm *sm, int set)
5047{
5048 if (sm)
5049 sm->fils_completed = !!set;
5050}
5051
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08005052#endif /* CONFIG_FILS */
5053
5054
5055int wpa_fils_is_completed(struct wpa_sm *sm)
5056{
5057#ifdef CONFIG_FILS
5058 return sm && sm->fils_completed;
5059#else /* CONFIG_FILS */
5060 return 0;
5061#endif /* CONFIG_FILS */
5062}
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005063
5064
5065#ifdef CONFIG_OWE
5066
5067struct wpabuf * owe_build_assoc_req(struct wpa_sm *sm, u16 group)
5068{
5069 struct wpabuf *ie = NULL, *pub = NULL;
5070 size_t prime_len;
5071
5072 if (group == 19)
5073 prime_len = 32;
5074 else if (group == 20)
5075 prime_len = 48;
5076 else if (group == 21)
5077 prime_len = 66;
5078 else
5079 return NULL;
5080
5081 crypto_ecdh_deinit(sm->owe_ecdh);
5082 sm->owe_ecdh = crypto_ecdh_init(group);
5083 if (!sm->owe_ecdh)
5084 goto fail;
5085 sm->owe_group = group;
5086 pub = crypto_ecdh_get_pubkey(sm->owe_ecdh, 0);
5087 pub = wpabuf_zeropad(pub, prime_len);
5088 if (!pub)
5089 goto fail;
5090
5091 ie = wpabuf_alloc(5 + wpabuf_len(pub));
5092 if (!ie)
5093 goto fail;
5094 wpabuf_put_u8(ie, WLAN_EID_EXTENSION);
5095 wpabuf_put_u8(ie, 1 + 2 + wpabuf_len(pub));
5096 wpabuf_put_u8(ie, WLAN_EID_EXT_OWE_DH_PARAM);
5097 wpabuf_put_le16(ie, group);
5098 wpabuf_put_buf(ie, pub);
5099 wpabuf_free(pub);
5100 wpa_hexdump_buf(MSG_DEBUG, "OWE: Diffie-Hellman Parameter element",
5101 ie);
5102
5103 return ie;
5104fail:
5105 wpabuf_free(pub);
5106 crypto_ecdh_deinit(sm->owe_ecdh);
5107 sm->owe_ecdh = NULL;
5108 return NULL;
5109}
5110
5111
5112int owe_process_assoc_resp(struct wpa_sm *sm, const u8 *bssid,
5113 const u8 *resp_ies, size_t resp_ies_len)
5114{
5115 struct ieee802_11_elems elems;
5116 u16 group;
5117 struct wpabuf *secret, *pub, *hkey;
5118 int res;
5119 u8 prk[SHA512_MAC_LEN], pmkid[SHA512_MAC_LEN];
5120 const char *info = "OWE Key Generation";
5121 const u8 *addr[2];
5122 size_t len[2];
5123 size_t hash_len, prime_len;
5124 struct wpa_ie_data data;
5125
5126 if (!resp_ies ||
5127 ieee802_11_parse_elems(resp_ies, resp_ies_len, &elems, 1) ==
5128 ParseFailed) {
5129 wpa_printf(MSG_INFO,
5130 "OWE: Could not parse Association Response frame elements");
5131 return -1;
5132 }
5133
5134 if (sm->cur_pmksa && elems.rsn_ie &&
5135 wpa_parse_wpa_ie_rsn(elems.rsn_ie - 2, 2 + elems.rsn_ie_len,
5136 &data) == 0 &&
5137 data.num_pmkid == 1 && data.pmkid &&
5138 os_memcmp(sm->cur_pmksa->pmkid, data.pmkid, PMKID_LEN) == 0) {
5139 wpa_printf(MSG_DEBUG, "OWE: Use PMKSA caching");
5140 wpa_sm_set_pmk_from_pmksa(sm);
5141 return 0;
5142 }
5143
5144 if (!elems.owe_dh) {
5145 wpa_printf(MSG_INFO,
5146 "OWE: No Diffie-Hellman Parameter element found in Association Response frame");
5147 return -1;
5148 }
5149
5150 group = WPA_GET_LE16(elems.owe_dh);
5151 if (group != sm->owe_group) {
5152 wpa_printf(MSG_INFO,
5153 "OWE: Unexpected Diffie-Hellman group in response: %u",
5154 group);
5155 return -1;
5156 }
5157
5158 if (!sm->owe_ecdh) {
5159 wpa_printf(MSG_INFO, "OWE: No ECDH state available");
5160 return -1;
5161 }
5162
5163 if (group == 19)
5164 prime_len = 32;
5165 else if (group == 20)
5166 prime_len = 48;
5167 else if (group == 21)
5168 prime_len = 66;
5169 else
5170 return -1;
5171
5172 secret = crypto_ecdh_set_peerkey(sm->owe_ecdh, 0,
5173 elems.owe_dh + 2,
5174 elems.owe_dh_len - 2);
5175 secret = wpabuf_zeropad(secret, prime_len);
5176 if (!secret) {
5177 wpa_printf(MSG_DEBUG, "OWE: Invalid peer DH public key");
5178 return -1;
5179 }
5180 wpa_hexdump_buf_key(MSG_DEBUG, "OWE: DH shared secret", secret);
5181
5182 /* prk = HKDF-extract(C | A | group, z) */
5183
5184 pub = crypto_ecdh_get_pubkey(sm->owe_ecdh, 0);
5185 if (!pub) {
5186 wpabuf_clear_free(secret);
5187 return -1;
5188 }
5189
5190 /* PMKID = Truncate-128(Hash(C | A)) */
5191 addr[0] = wpabuf_head(pub);
5192 len[0] = wpabuf_len(pub);
5193 addr[1] = elems.owe_dh + 2;
5194 len[1] = elems.owe_dh_len - 2;
5195 if (group == 19) {
5196 res = sha256_vector(2, addr, len, pmkid);
5197 hash_len = SHA256_MAC_LEN;
5198 } else if (group == 20) {
5199 res = sha384_vector(2, addr, len, pmkid);
5200 hash_len = SHA384_MAC_LEN;
5201 } else if (group == 21) {
5202 res = sha512_vector(2, addr, len, pmkid);
5203 hash_len = SHA512_MAC_LEN;
5204 } else {
5205 res = -1;
5206 hash_len = 0;
5207 }
5208 pub = wpabuf_zeropad(pub, prime_len);
5209 if (res < 0 || !pub) {
5210 wpabuf_free(pub);
5211 wpabuf_clear_free(secret);
5212 return -1;
5213 }
5214
5215 hkey = wpabuf_alloc(wpabuf_len(pub) + elems.owe_dh_len - 2 + 2);
5216 if (!hkey) {
5217 wpabuf_free(pub);
5218 wpabuf_clear_free(secret);
5219 return -1;
5220 }
5221
5222 wpabuf_put_buf(hkey, pub); /* C */
5223 wpabuf_free(pub);
5224 wpabuf_put_data(hkey, elems.owe_dh + 2, elems.owe_dh_len - 2); /* A */
5225 wpabuf_put_le16(hkey, sm->owe_group); /* group */
5226 if (group == 19)
5227 res = hmac_sha256(wpabuf_head(hkey), wpabuf_len(hkey),
5228 wpabuf_head(secret), wpabuf_len(secret), prk);
5229 else if (group == 20)
5230 res = hmac_sha384(wpabuf_head(hkey), wpabuf_len(hkey),
5231 wpabuf_head(secret), wpabuf_len(secret), prk);
5232 else if (group == 21)
5233 res = hmac_sha512(wpabuf_head(hkey), wpabuf_len(hkey),
5234 wpabuf_head(secret), wpabuf_len(secret), prk);
5235 wpabuf_clear_free(hkey);
5236 wpabuf_clear_free(secret);
5237 if (res < 0)
5238 return -1;
5239
5240 wpa_hexdump_key(MSG_DEBUG, "OWE: prk", prk, hash_len);
5241
5242 /* PMK = HKDF-expand(prk, "OWE Key Generation", n) */
5243
5244 if (group == 19)
5245 res = hmac_sha256_kdf(prk, hash_len, NULL, (const u8 *) info,
5246 os_strlen(info), sm->pmk, hash_len);
5247 else if (group == 20)
5248 res = hmac_sha384_kdf(prk, hash_len, NULL, (const u8 *) info,
5249 os_strlen(info), sm->pmk, hash_len);
5250 else if (group == 21)
5251 res = hmac_sha512_kdf(prk, hash_len, NULL, (const u8 *) info,
5252 os_strlen(info), sm->pmk, hash_len);
Hai Shalom81f62d82019-07-22 12:10:00 -07005253 forced_memzero(prk, SHA512_MAC_LEN);
Roshan Pius3a1667e2018-07-03 15:17:14 -07005254 if (res < 0) {
5255 sm->pmk_len = 0;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005256 return -1;
Roshan Pius3a1667e2018-07-03 15:17:14 -07005257 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005258 sm->pmk_len = hash_len;
5259
5260 wpa_hexdump_key(MSG_DEBUG, "OWE: PMK", sm->pmk, sm->pmk_len);
5261 wpa_hexdump(MSG_DEBUG, "OWE: PMKID", pmkid, PMKID_LEN);
5262 pmksa_cache_add(sm->pmksa, sm->pmk, sm->pmk_len, pmkid, NULL, 0,
5263 bssid, sm->own_addr, sm->network_ctx, sm->key_mgmt,
5264 NULL);
5265
5266 return 0;
5267}
5268
5269#endif /* CONFIG_OWE */
5270
5271
5272void wpa_sm_set_fils_cache_id(struct wpa_sm *sm, const u8 *fils_cache_id)
5273{
5274#ifdef CONFIG_FILS
5275 if (sm && fils_cache_id) {
5276 sm->fils_cache_id_set = 1;
5277 os_memcpy(sm->fils_cache_id, fils_cache_id, FILS_CACHE_ID_LEN);
5278 }
5279#endif /* CONFIG_FILS */
5280}
Hai Shalom021b0b52019-04-10 11:17:58 -07005281
5282
5283#ifdef CONFIG_DPP2
5284void wpa_sm_set_dpp_z(struct wpa_sm *sm, const struct wpabuf *z)
5285{
5286 if (sm) {
5287 wpabuf_clear_free(sm->dpp_z);
5288 sm->dpp_z = z ? wpabuf_dup(z) : NULL;
5289 }
5290}
5291#endif /* CONFIG_DPP2 */
Hai Shalom60840252021-02-19 19:02:11 -08005292
5293
5294#ifdef CONFIG_PASN
5295void wpa_pasn_pmksa_cache_add(struct wpa_sm *sm, const u8 *pmk, size_t pmk_len,
5296 const u8 *pmkid, const u8 *bssid, int key_mgmt)
5297{
5298 sm->cur_pmksa = pmksa_cache_add(sm->pmksa, pmk, pmk_len, pmkid, NULL, 0,
5299 bssid, sm->own_addr, NULL,
5300 key_mgmt, 0);
5301}
5302#endif /* CONFIG_PASN */
Hai Shalomc1a21442022-02-04 13:43:00 -08005303
5304
5305void wpa_sm_pmksa_cache_reconfig(struct wpa_sm *sm)
5306{
5307 if (sm)
5308 pmksa_cache_reconfig(sm->pmksa);
5309}