blob: 64a6ccb5c237b64fdf8ca3eda6daa55a0053ca3b [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"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070035
36
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080037static const u8 null_rsc[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
38
39
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070040/**
41 * wpa_eapol_key_send - Send WPA/RSN EAPOL-Key message
42 * @sm: Pointer to WPA state machine data from wpa_sm_init()
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -080043 * @ptk: PTK for Key Confirmation/Encryption Key
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070044 * @ver: Version field from Key Info
45 * @dest: Destination address for the frame
46 * @proto: Ethertype (usually ETH_P_EAPOL)
47 * @msg: EAPOL-Key message
48 * @msg_len: Length of message
49 * @key_mic: Pointer to the buffer to which the EAPOL-Key MIC is written
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080050 * Returns: >= 0 on success, < 0 on failure
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070051 */
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -080052int wpa_eapol_key_send(struct wpa_sm *sm, struct wpa_ptk *ptk,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080053 int ver, const u8 *dest, u16 proto,
54 u8 *msg, size_t msg_len, u8 *key_mic)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070055{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080056 int ret = -1;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070057 size_t mic_len = wpa_mic_len(sm->key_mgmt, sm->pmk_len);
Dmitry Shmidt807291d2015-01-27 13:40:23 -080058
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070059 wpa_printf(MSG_DEBUG, "WPA: Send EAPOL-Key frame to " MACSTR
60 " ver=%d mic_len=%d key_mgmt=0x%x",
61 MAC2STR(dest), ver, (int) mic_len, sm->key_mgmt);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070062 if (is_zero_ether_addr(dest) && is_zero_ether_addr(sm->bssid)) {
63 /*
64 * Association event was not yet received; try to fetch
65 * BSSID from the driver.
66 */
67 if (wpa_sm_get_bssid(sm, sm->bssid) < 0) {
68 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
69 "WPA: Failed to read BSSID for "
70 "EAPOL-Key destination address");
71 } else {
72 dest = sm->bssid;
73 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
74 "WPA: Use BSSID (" MACSTR
75 ") as the destination for EAPOL-Key",
76 MAC2STR(dest));
77 }
78 }
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -080079
80 if (mic_len) {
81 if (key_mic && (!ptk || !ptk->kck_len))
82 goto out;
83
84 if (key_mic &&
85 wpa_eapol_key_mic(ptk->kck, ptk->kck_len, sm->key_mgmt, ver,
86 msg, msg_len, key_mic)) {
87 wpa_msg(sm->ctx->msg_ctx, MSG_ERROR,
88 "WPA: Failed to generate EAPOL-Key version %d key_mgmt 0x%x MIC",
89 ver, sm->key_mgmt);
90 goto out;
91 }
Dmitry Shmidt29333592017-01-09 12:27:11 -080092 if (ptk)
93 wpa_hexdump_key(MSG_DEBUG, "WPA: KCK",
94 ptk->kck, ptk->kck_len);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -080095 wpa_hexdump(MSG_DEBUG, "WPA: Derived Key MIC",
96 key_mic, mic_len);
97 } else {
98#ifdef CONFIG_FILS
99 /* AEAD cipher - Key MIC field not used */
100 struct ieee802_1x_hdr *s_hdr, *hdr;
101 struct wpa_eapol_key *s_key, *key;
102 u8 *buf, *s_key_data, *key_data;
103 size_t buf_len = msg_len + AES_BLOCK_SIZE;
104 size_t key_data_len;
105 u16 eapol_len;
106 const u8 *aad[1];
107 size_t aad_len[1];
108
109 if (!ptk || !ptk->kek_len)
110 goto out;
111
112 key_data_len = msg_len - sizeof(struct ieee802_1x_hdr) -
113 sizeof(struct wpa_eapol_key) - 2;
114
115 buf = os_malloc(buf_len);
116 if (!buf)
117 goto out;
118
119 os_memcpy(buf, msg, msg_len);
120 hdr = (struct ieee802_1x_hdr *) buf;
121 key = (struct wpa_eapol_key *) (hdr + 1);
122 key_data = ((u8 *) (key + 1)) + 2;
123
124 /* Update EAPOL header to include AES-SIV overhead */
125 eapol_len = be_to_host16(hdr->length);
126 eapol_len += AES_BLOCK_SIZE;
127 hdr->length = host_to_be16(eapol_len);
128
129 /* Update Key Data Length field to include AES-SIV overhead */
130 WPA_PUT_BE16((u8 *) (key + 1), AES_BLOCK_SIZE + key_data_len);
131
132 s_hdr = (struct ieee802_1x_hdr *) msg;
133 s_key = (struct wpa_eapol_key *) (s_hdr + 1);
134 s_key_data = ((u8 *) (s_key + 1)) + 2;
135
136 wpa_hexdump_key(MSG_DEBUG, "WPA: Plaintext Key Data",
137 s_key_data, key_data_len);
138
139 wpa_hexdump_key(MSG_DEBUG, "WPA: KEK", ptk->kek, ptk->kek_len);
140 /* AES-SIV AAD from EAPOL protocol version field (inclusive) to
141 * to Key Data (exclusive). */
142 aad[0] = buf;
143 aad_len[0] = key_data - buf;
144 if (aes_siv_encrypt(ptk->kek, ptk->kek_len,
145 s_key_data, key_data_len,
146 1, aad, aad_len, key_data) < 0) {
147 os_free(buf);
148 goto out;
149 }
150
151 wpa_hexdump(MSG_DEBUG, "WPA: Encrypted Key Data from SIV",
152 key_data, AES_BLOCK_SIZE + key_data_len);
153
154 os_free(msg);
155 msg = buf;
156 msg_len = buf_len;
157#else /* CONFIG_FILS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700158 goto out;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800159#endif /* CONFIG_FILS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700160 }
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800161
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700162 wpa_hexdump(MSG_MSGDUMP, "WPA: TX EAPOL-Key", msg, msg_len);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800163 ret = wpa_sm_ether_send(sm, dest, proto, msg, msg_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700164 eapol_sm_notify_tx_eapol_key(sm->eapol);
165out:
166 os_free(msg);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800167 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700168}
169
170
171/**
172 * wpa_sm_key_request - Send EAPOL-Key Request
173 * @sm: Pointer to WPA state machine data from wpa_sm_init()
174 * @error: Indicate whether this is an Michael MIC error report
175 * @pairwise: 1 = error report for pairwise packet, 0 = for group packet
176 *
177 * Send an EAPOL-Key Request to the current authenticator. This function is
178 * used to request rekeying and it is usually called when a local Michael MIC
179 * failure is detected.
180 */
181void wpa_sm_key_request(struct wpa_sm *sm, int error, int pairwise)
182{
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800183 size_t mic_len, hdrlen, rlen;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700184 struct wpa_eapol_key *reply;
185 int key_info, ver;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800186 u8 bssid[ETH_ALEN], *rbuf, *key_mic, *mic;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700187
Hai Shalomfdcde762020-04-02 11:19:20 -0700188 if (pairwise && sm->wpa_deny_ptk0_rekey && !sm->use_ext_key_id &&
189 wpa_sm_get_state(sm) == WPA_COMPLETED) {
190 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
191 "WPA: PTK0 rekey not allowed, reconnecting");
192 wpa_sm_reconnect(sm);
193 return;
194 }
195
Roshan Pius3a1667e2018-07-03 15:17:14 -0700196 if (wpa_use_akm_defined(sm->key_mgmt))
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800197 ver = WPA_KEY_INFO_TYPE_AKM_DEFINED;
198 else if (wpa_key_mgmt_ft(sm->key_mgmt) ||
199 wpa_key_mgmt_sha256(sm->key_mgmt))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700200 ver = WPA_KEY_INFO_TYPE_AES_128_CMAC;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700201 else if (sm->pairwise_cipher != WPA_CIPHER_TKIP)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700202 ver = WPA_KEY_INFO_TYPE_HMAC_SHA1_AES;
203 else
204 ver = WPA_KEY_INFO_TYPE_HMAC_MD5_RC4;
205
206 if (wpa_sm_get_bssid(sm, bssid) < 0) {
207 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
208 "Failed to read BSSID for EAPOL-Key request");
209 return;
210 }
211
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700212 mic_len = wpa_mic_len(sm->key_mgmt, sm->pmk_len);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800213 hdrlen = sizeof(*reply) + mic_len + 2;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700214 rbuf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_KEY, NULL,
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800215 hdrlen, &rlen, (void *) &reply);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700216 if (rbuf == NULL)
217 return;
218
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800219 reply->type = (sm->proto == WPA_PROTO_RSN ||
220 sm->proto == WPA_PROTO_OSEN) ?
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700221 EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA;
222 key_info = WPA_KEY_INFO_REQUEST | ver;
223 if (sm->ptk_set)
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800224 key_info |= WPA_KEY_INFO_SECURE;
225 if (sm->ptk_set && mic_len)
226 key_info |= WPA_KEY_INFO_MIC;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700227 if (error)
228 key_info |= WPA_KEY_INFO_ERROR;
229 if (pairwise)
230 key_info |= WPA_KEY_INFO_KEY_TYPE;
231 WPA_PUT_BE16(reply->key_info, key_info);
232 WPA_PUT_BE16(reply->key_length, 0);
233 os_memcpy(reply->replay_counter, sm->request_counter,
234 WPA_REPLAY_COUNTER_LEN);
235 inc_byte_array(sm->request_counter, WPA_REPLAY_COUNTER_LEN);
236
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800237 mic = (u8 *) (reply + 1);
238 WPA_PUT_BE16(mic + mic_len, 0);
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800239 if (!(key_info & WPA_KEY_INFO_MIC))
240 key_mic = NULL;
241 else
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800242 key_mic = mic;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700243
244 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
245 "WPA: Sending EAPOL-Key Request (error=%d "
246 "pairwise=%d ptk_set=%d len=%lu)",
247 error, pairwise, sm->ptk_set, (unsigned long) rlen);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800248 wpa_eapol_key_send(sm, &sm->ptk, ver, bssid, ETH_P_EAPOL, rbuf, rlen,
249 key_mic);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700250}
251
252
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800253static void wpa_supplicant_key_mgmt_set_pmk(struct wpa_sm *sm)
254{
255#ifdef CONFIG_IEEE80211R
256 if (sm->key_mgmt == WPA_KEY_MGMT_FT_IEEE8021X) {
257 if (wpa_sm_key_mgmt_set_pmk(sm, sm->xxkey, sm->xxkey_len))
258 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
259 "RSN: Cannot set low order 256 bits of MSK for key management offload");
260 } else {
261#endif /* CONFIG_IEEE80211R */
262 if (wpa_sm_key_mgmt_set_pmk(sm, sm->pmk, sm->pmk_len))
263 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
264 "RSN: Cannot set PMK for key management offload");
265#ifdef CONFIG_IEEE80211R
266 }
267#endif /* CONFIG_IEEE80211R */
268}
269
270
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700271static int wpa_supplicant_get_pmk(struct wpa_sm *sm,
272 const unsigned char *src_addr,
273 const u8 *pmkid)
274{
275 int abort_cached = 0;
276
277 if (pmkid && !sm->cur_pmksa) {
278 /* When using drivers that generate RSN IE, wpa_supplicant may
279 * not have enough time to get the association information
280 * event before receiving this 1/4 message, so try to find a
281 * matching PMKSA cache entry here. */
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800282 sm->cur_pmksa = pmksa_cache_get(sm->pmksa, src_addr, pmkid,
Roshan Pius3a1667e2018-07-03 15:17:14 -0700283 NULL, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700284 if (sm->cur_pmksa) {
285 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
286 "RSN: found matching PMKID from PMKSA cache");
287 } else {
288 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
289 "RSN: no matching PMKID found");
290 abort_cached = 1;
291 }
292 }
293
294 if (pmkid && sm->cur_pmksa &&
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700295 os_memcmp_const(pmkid, sm->cur_pmksa->pmkid, PMKID_LEN) == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700296 wpa_hexdump(MSG_DEBUG, "RSN: matched PMKID", pmkid, PMKID_LEN);
297 wpa_sm_set_pmk_from_pmksa(sm);
298 wpa_hexdump_key(MSG_DEBUG, "RSN: PMK from PMKSA cache",
299 sm->pmk, sm->pmk_len);
300 eapol_sm_notify_cached(sm->eapol);
301#ifdef CONFIG_IEEE80211R
302 sm->xxkey_len = 0;
Roshan Pius3a1667e2018-07-03 15:17:14 -0700303#ifdef CONFIG_SAE
304 if (sm->key_mgmt == WPA_KEY_MGMT_FT_SAE &&
305 sm->pmk_len == PMK_LEN) {
306 /* Need to allow FT key derivation to proceed with
307 * PMK from SAE being used as the XXKey in cases where
308 * the PMKID in msg 1/4 matches the PMKSA entry that was
309 * just added based on SAE authentication for the
310 * initial mobility domain association. */
311 os_memcpy(sm->xxkey, sm->pmk, sm->pmk_len);
312 sm->xxkey_len = sm->pmk_len;
313 }
314#endif /* CONFIG_SAE */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700315#endif /* CONFIG_IEEE80211R */
316 } else if (wpa_key_mgmt_wpa_ieee8021x(sm->key_mgmt) && sm->eapol) {
317 int res, pmk_len;
Hai Shalom81f62d82019-07-22 12:10:00 -0700318#ifdef CONFIG_IEEE80211R
319 u8 buf[2 * PMK_LEN];
320#endif /* CONFIG_IEEE80211R */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800321
Dmitry Shmidtebd93af2017-02-21 13:40:44 -0800322 if (wpa_key_mgmt_sha384(sm->key_mgmt))
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800323 pmk_len = PMK_LEN_SUITE_B_192;
324 else
325 pmk_len = PMK_LEN;
326 res = eapol_sm_get_key(sm->eapol, sm->pmk, pmk_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700327 if (res) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800328 if (pmk_len == PMK_LEN) {
329 /*
330 * EAP-LEAP is an exception from other EAP
331 * methods: it uses only 16-byte PMK.
332 */
333 res = eapol_sm_get_key(sm->eapol, sm->pmk, 16);
334 pmk_len = 16;
335 }
Hai Shalomf1c97642019-07-19 23:42:07 +0000336 }
Hai Shalom81f62d82019-07-22 12:10:00 -0700337#ifdef CONFIG_IEEE80211R
338 if (res == 0 &&
339 eapol_sm_get_key(sm->eapol, buf, 2 * PMK_LEN) == 0) {
340 if (wpa_key_mgmt_sha384(sm->key_mgmt)) {
341 os_memcpy(sm->xxkey, buf, SHA384_MAC_LEN);
342 sm->xxkey_len = SHA384_MAC_LEN;
343 } else {
344 os_memcpy(sm->xxkey, buf + PMK_LEN, PMK_LEN);
345 sm->xxkey_len = PMK_LEN;
346 }
347 forced_memzero(buf, sizeof(buf));
348 if (sm->proto == WPA_PROTO_RSN &&
349 wpa_key_mgmt_ft(sm->key_mgmt)) {
350 struct rsn_pmksa_cache_entry *sa = NULL;
351 const u8 *fils_cache_id = NULL;
352
353#ifdef CONFIG_FILS
354 if (sm->fils_cache_id_set)
355 fils_cache_id = sm->fils_cache_id;
356#endif /* CONFIG_FILS */
357 wpa_hexdump_key(MSG_DEBUG,
358 "FT: Cache XXKey/MPMK",
359 sm->xxkey, sm->xxkey_len);
360 sa = pmksa_cache_add(sm->pmksa,
361 sm->xxkey, sm->xxkey_len,
362 NULL, NULL, 0,
363 src_addr, sm->own_addr,
364 sm->network_ctx,
365 sm->key_mgmt,
366 fils_cache_id);
367 if (!sm->cur_pmksa)
368 sm->cur_pmksa = sa;
369 }
370 }
371#endif /* CONFIG_IEEE80211R */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700372 if (res == 0) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700373 struct rsn_pmksa_cache_entry *sa = NULL;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700374 const u8 *fils_cache_id = NULL;
375
376#ifdef CONFIG_FILS
377 if (sm->fils_cache_id_set)
378 fils_cache_id = sm->fils_cache_id;
379#endif /* CONFIG_FILS */
380
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700381 wpa_hexdump_key(MSG_DEBUG, "WPA: PMK from EAPOL state "
382 "machines", sm->pmk, pmk_len);
383 sm->pmk_len = pmk_len;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800384 wpa_supplicant_key_mgmt_set_pmk(sm);
Dmitry Shmidtc55524a2011-07-07 11:18:38 -0700385 if (sm->proto == WPA_PROTO_RSN &&
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800386 !wpa_key_mgmt_suite_b(sm->key_mgmt) &&
Dmitry Shmidtc55524a2011-07-07 11:18:38 -0700387 !wpa_key_mgmt_ft(sm->key_mgmt)) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700388 sa = pmksa_cache_add(sm->pmksa,
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800389 sm->pmk, pmk_len, NULL,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800390 NULL, 0,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700391 src_addr, sm->own_addr,
392 sm->network_ctx,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700393 sm->key_mgmt,
394 fils_cache_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700395 }
396 if (!sm->cur_pmksa && pmkid &&
Roshan Pius3a1667e2018-07-03 15:17:14 -0700397 pmksa_cache_get(sm->pmksa, src_addr, pmkid, NULL,
398 0)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700399 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
400 "RSN: the new PMK matches with the "
401 "PMKID");
402 abort_cached = 0;
Jouni Malinen6ec30382015-07-08 20:48:18 +0300403 } else if (sa && !sm->cur_pmksa && pmkid) {
404 /*
405 * It looks like the authentication server
406 * derived mismatching MSK. This should not
407 * really happen, but bugs happen.. There is not
408 * much we can do here without knowing what
409 * exactly caused the server to misbehave.
410 */
Dmitry Shmidt014a3ff2015-12-28 13:27:49 -0800411 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
Jouni Malinen6ec30382015-07-08 20:48:18 +0300412 "RSN: PMKID mismatch - authentication server may have derived different MSK?!");
413 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700414 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700415
416 if (!sm->cur_pmksa)
417 sm->cur_pmksa = sa;
Hai Shalom81f62d82019-07-22 12:10:00 -0700418#ifdef CONFIG_IEEE80211R
419 } else if (wpa_key_mgmt_ft(sm->key_mgmt) && sm->ft_protocol) {
420 wpa_printf(MSG_DEBUG,
421 "FT: Continue 4-way handshake without PMK/PMKID for association using FT protocol");
422#endif /* CONFIG_IEEE80211R */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700423 } else {
424 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
425 "WPA: Failed to get master session key from "
426 "EAPOL state machines - key handshake "
427 "aborted");
428 if (sm->cur_pmksa) {
429 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
430 "RSN: Cancelled PMKSA caching "
431 "attempt");
432 sm->cur_pmksa = NULL;
433 abort_cached = 1;
434 } else if (!abort_cached) {
435 return -1;
436 }
437 }
438 }
439
Dmitry Shmidtc55524a2011-07-07 11:18:38 -0700440 if (abort_cached && wpa_key_mgmt_wpa_ieee8021x(sm->key_mgmt) &&
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800441 !wpa_key_mgmt_suite_b(sm->key_mgmt) &&
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800442 !wpa_key_mgmt_ft(sm->key_mgmt) && sm->key_mgmt != WPA_KEY_MGMT_OSEN)
443 {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700444 /* Send EAPOL-Start to trigger full EAP authentication. */
445 u8 *buf;
446 size_t buflen;
447
448 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
449 "RSN: no PMKSA entry found - trigger "
450 "full EAP authentication");
451 buf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_START,
452 NULL, 0, &buflen, NULL);
453 if (buf) {
454 wpa_sm_ether_send(sm, sm->bssid, ETH_P_EAPOL,
455 buf, buflen);
456 os_free(buf);
457 return -2;
458 }
459
460 return -1;
461 }
462
463 return 0;
464}
465
466
467/**
468 * wpa_supplicant_send_2_of_4 - Send message 2 of WPA/RSN 4-Way Handshake
469 * @sm: Pointer to WPA state machine data from wpa_sm_init()
470 * @dst: Destination address for the frame
471 * @key: Pointer to the EAPOL-Key frame header
472 * @ver: Version bits from EAPOL-Key Key Info
473 * @nonce: Nonce value for the EAPOL-Key frame
474 * @wpa_ie: WPA/RSN IE
475 * @wpa_ie_len: Length of the WPA/RSN IE
476 * @ptk: PTK to use for keyed hash and encryption
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800477 * Returns: >= 0 on success, < 0 on failure
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700478 */
479int wpa_supplicant_send_2_of_4(struct wpa_sm *sm, const unsigned char *dst,
480 const struct wpa_eapol_key *key,
481 int ver, const u8 *nonce,
482 const u8 *wpa_ie, size_t wpa_ie_len,
483 struct wpa_ptk *ptk)
484{
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800485 size_t mic_len, hdrlen, rlen;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700486 struct wpa_eapol_key *reply;
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800487 u8 *rbuf, *key_mic;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700488 u8 *rsn_ie_buf = NULL;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800489 u16 key_info;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700490
491 if (wpa_ie == NULL) {
492 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, "WPA: No wpa_ie set - "
493 "cannot generate msg 2/4");
494 return -1;
495 }
496
497#ifdef CONFIG_IEEE80211R
498 if (wpa_key_mgmt_ft(sm->key_mgmt)) {
499 int res;
500
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -0800501 wpa_hexdump(MSG_DEBUG, "WPA: WPA IE before FT processing",
502 wpa_ie, wpa_ie_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700503 /*
504 * Add PMKR1Name into RSN IE (PMKID-List) and add MDIE and
505 * FTIE from (Re)Association Response.
506 */
507 rsn_ie_buf = os_malloc(wpa_ie_len + 2 + 2 + PMKID_LEN +
508 sm->assoc_resp_ies_len);
509 if (rsn_ie_buf == NULL)
510 return -1;
511 os_memcpy(rsn_ie_buf, wpa_ie, wpa_ie_len);
Dmitry Shmidt55840ad2015-12-14 12:45:46 -0800512 res = wpa_insert_pmkid(rsn_ie_buf, &wpa_ie_len,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700513 sm->pmk_r1_name);
514 if (res < 0) {
515 os_free(rsn_ie_buf);
516 return -1;
517 }
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -0800518 wpa_hexdump(MSG_DEBUG,
519 "WPA: WPA IE after PMKID[PMKR1Name] addition into RSNE",
520 rsn_ie_buf, wpa_ie_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700521
522 if (sm->assoc_resp_ies) {
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -0800523 wpa_hexdump(MSG_DEBUG, "WPA: Add assoc_resp_ies",
524 sm->assoc_resp_ies,
525 sm->assoc_resp_ies_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700526 os_memcpy(rsn_ie_buf + wpa_ie_len, sm->assoc_resp_ies,
527 sm->assoc_resp_ies_len);
528 wpa_ie_len += sm->assoc_resp_ies_len;
529 }
530
531 wpa_ie = rsn_ie_buf;
532 }
533#endif /* CONFIG_IEEE80211R */
534
535 wpa_hexdump(MSG_DEBUG, "WPA: WPA IE for msg 2/4", wpa_ie, wpa_ie_len);
536
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700537 mic_len = wpa_mic_len(sm->key_mgmt, sm->pmk_len);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800538 hdrlen = sizeof(*reply) + mic_len + 2;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700539 rbuf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_KEY,
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800540 NULL, hdrlen + wpa_ie_len,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700541 &rlen, (void *) &reply);
542 if (rbuf == NULL) {
543 os_free(rsn_ie_buf);
544 return -1;
545 }
546
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800547 reply->type = (sm->proto == WPA_PROTO_RSN ||
548 sm->proto == WPA_PROTO_OSEN) ?
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700549 EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800550 key_info = ver | WPA_KEY_INFO_KEY_TYPE;
551 if (mic_len)
552 key_info |= WPA_KEY_INFO_MIC;
553 else
554 key_info |= WPA_KEY_INFO_ENCR_KEY_DATA;
555 WPA_PUT_BE16(reply->key_info, key_info);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800556 if (sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700557 WPA_PUT_BE16(reply->key_length, 0);
558 else
559 os_memcpy(reply->key_length, key->key_length, 2);
560 os_memcpy(reply->replay_counter, key->replay_counter,
561 WPA_REPLAY_COUNTER_LEN);
562 wpa_hexdump(MSG_DEBUG, "WPA: Replay Counter", reply->replay_counter,
563 WPA_REPLAY_COUNTER_LEN);
564
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800565 key_mic = (u8 *) (reply + 1);
566 WPA_PUT_BE16(key_mic + mic_len, wpa_ie_len); /* Key Data Length */
567 os_memcpy(key_mic + mic_len + 2, wpa_ie, wpa_ie_len); /* Key Data */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700568 os_free(rsn_ie_buf);
569
570 os_memcpy(reply->key_nonce, nonce, WPA_NONCE_LEN);
571
Roshan Pius5e7db942018-04-12 12:27:41 -0700572 wpa_dbg(sm->ctx->msg_ctx, MSG_INFO, "WPA: Sending EAPOL-Key 2/4");
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800573 return wpa_eapol_key_send(sm, ptk, ver, dst, ETH_P_EAPOL, rbuf, rlen,
574 key_mic);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700575}
576
577
578static int wpa_derive_ptk(struct wpa_sm *sm, const unsigned char *src_addr,
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800579 const struct wpa_eapol_key *key, struct wpa_ptk *ptk)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700580{
Hai Shalom021b0b52019-04-10 11:17:58 -0700581 const u8 *z = NULL;
Hai Shalom60840252021-02-19 19:02:11 -0800582 size_t z_len = 0, kdk_len;
Hai Shalomfdcde762020-04-02 11:19:20 -0700583 int akmp;
Hai Shalom021b0b52019-04-10 11:17:58 -0700584
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700585#ifdef CONFIG_IEEE80211R
586 if (wpa_key_mgmt_ft(sm->key_mgmt))
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800587 return wpa_derive_ptk_ft(sm, src_addr, key, ptk);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700588#endif /* CONFIG_IEEE80211R */
589
Hai Shalom021b0b52019-04-10 11:17:58 -0700590#ifdef CONFIG_DPP2
591 if (sm->key_mgmt == WPA_KEY_MGMT_DPP && sm->dpp_z) {
592 z = wpabuf_head(sm->dpp_z);
593 z_len = wpabuf_len(sm->dpp_z);
594 }
595#endif /* CONFIG_DPP2 */
596
Hai Shalomfdcde762020-04-02 11:19:20 -0700597 akmp = sm->key_mgmt;
598#ifdef CONFIG_OWE
599 if (sm->owe_ptk_workaround && akmp == WPA_KEY_MGMT_OWE &&
600 sm->pmk_len > 32) {
601 wpa_printf(MSG_DEBUG,
602 "OWE: Force SHA256 for PTK derivation");
603 akmp |= WPA_KEY_MGMT_PSK_SHA256;
604 }
605#endif /* CONFIG_OWE */
Hai Shalom60840252021-02-19 19:02:11 -0800606
607 if (sm->force_kdk_derivation ||
608 (sm->secure_ltf && sm->ap_rsnxe && sm->ap_rsnxe_len >= 4 &&
609 sm->ap_rsnxe[3] & BIT(WLAN_RSNX_CAPAB_SECURE_LTF - 8)))
610 kdk_len = WPA_KDK_MAX_LEN;
611 else
612 kdk_len = 0;
613
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800614 return wpa_pmk_to_ptk(sm->pmk, sm->pmk_len, "Pairwise key expansion",
615 sm->own_addr, sm->bssid, sm->snonce,
Hai Shalomfdcde762020-04-02 11:19:20 -0700616 key->key_nonce, ptk, akmp,
Hai Shalom60840252021-02-19 19:02:11 -0800617 sm->pairwise_cipher, z, z_len,
618 kdk_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700619}
620
621
Hai Shalomfdcde762020-04-02 11:19:20 -0700622static int wpa_handle_ext_key_id(struct wpa_sm *sm,
623 struct wpa_eapol_ie_parse *kde)
624{
625 if (sm->ext_key_id) {
626 u16 key_id;
627
628 if (!kde->key_id) {
629 wpa_msg(sm->ctx->msg_ctx,
630 sm->use_ext_key_id ? MSG_INFO : MSG_DEBUG,
631 "RSN: No Key ID in Extended Key ID handshake");
632 sm->keyidx_active = 0;
633 return sm->use_ext_key_id ? -1 : 0;
634 }
635
636 key_id = kde->key_id[0] & 0x03;
637 if (key_id > 1) {
638 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
639 "RSN: Invalid Extended Key ID: %d", key_id);
640 return -1;
641 }
642 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
643 "RSN: Using Extended Key ID %d", key_id);
644 sm->keyidx_active = key_id;
645 sm->use_ext_key_id = 1;
646 } else {
647 if (kde->key_id && (kde->key_id[0] & 0x03)) {
648 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
649 "RSN: Non-zero Extended Key ID Key ID in PTK0 handshake");
650 return -1;
651 }
652
653 if (kde->key_id) {
654 /* This is not supposed to be included here, but ignore
655 * the case of matching Key ID 0 just in case. */
656 wpa_msg(sm->ctx->msg_ctx, MSG_DEBUG,
657 "RSN: Extended Key ID Key ID 0 in PTK0 handshake");
658 }
659 sm->keyidx_active = 0;
660 sm->use_ext_key_id = 0;
661 }
662
663 return 0;
664}
665
666
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700667static void wpa_supplicant_process_1_of_4(struct wpa_sm *sm,
668 const unsigned char *src_addr,
669 const struct wpa_eapol_key *key,
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700670 u16 ver, const u8 *key_data,
671 size_t key_data_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700672{
673 struct wpa_eapol_ie_parse ie;
674 struct wpa_ptk *ptk;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700675 int res;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800676 u8 *kde, *kde_buf = NULL;
677 size_t kde_len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700678
679 if (wpa_sm_get_network_ctx(sm) == NULL) {
680 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, "WPA: No SSID info "
681 "found (msg 1 of 4)");
682 return;
683 }
684
Hai Shalomfdcde762020-04-02 11:19:20 -0700685 if (sm->wpa_deny_ptk0_rekey && !sm->use_ext_key_id &&
686 wpa_sm_get_state(sm) == WPA_COMPLETED) {
687 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
688 "WPA: PTK0 rekey not allowed, reconnecting");
689 wpa_sm_reconnect(sm);
690 return;
691 }
692
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700693 wpa_sm_set_state(sm, WPA_4WAY_HANDSHAKE);
Roshan Pius5e7db942018-04-12 12:27:41 -0700694 wpa_dbg(sm->ctx->msg_ctx, MSG_INFO, "WPA: RX message 1 of 4-Way "
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700695 "Handshake from " MACSTR " (ver=%d)", MAC2STR(src_addr), ver);
696
697 os_memset(&ie, 0, sizeof(ie));
698
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800699 if (sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700700 /* RSN: msg 1/4 should contain PMKID for the selected PMK */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700701 wpa_hexdump(MSG_DEBUG, "RSN: msg 1/4 key data",
702 key_data, key_data_len);
703 if (wpa_supplicant_parse_ies(key_data, key_data_len, &ie) < 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800704 goto failed;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700705 if (ie.pmkid) {
706 wpa_hexdump(MSG_DEBUG, "RSN: PMKID from "
707 "Authenticator", ie.pmkid, PMKID_LEN);
708 }
709 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700710
711 res = wpa_supplicant_get_pmk(sm, src_addr, ie.pmkid);
712 if (res == -2) {
713 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "RSN: Do not reply to "
714 "msg 1/4 - requesting full EAP authentication");
715 return;
716 }
717 if (res)
718 goto failed;
719
720 if (sm->renew_snonce) {
721 if (random_get_bytes(sm->snonce, WPA_NONCE_LEN)) {
722 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
723 "WPA: Failed to get random data for SNonce");
724 goto failed;
725 }
726 sm->renew_snonce = 0;
727 wpa_hexdump(MSG_DEBUG, "WPA: Renewed SNonce",
728 sm->snonce, WPA_NONCE_LEN);
729 }
730
731 /* Calculate PTK which will be stored as a temporary PTK until it has
732 * been verified when processing message 3/4. */
733 ptk = &sm->tptk;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700734 if (wpa_derive_ptk(sm, src_addr, key, ptk) < 0)
735 goto failed;
Dmitry Shmidt98660862014-03-11 17:26:21 -0700736 if (sm->pairwise_cipher == WPA_CIPHER_TKIP) {
Dmitry Shmidt61593f02014-04-21 16:27:35 -0700737 u8 buf[8];
Dmitry Shmidt98660862014-03-11 17:26:21 -0700738 /* Supplicant: swap tx/rx Mic keys */
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800739 os_memcpy(buf, &ptk->tk[16], 8);
740 os_memcpy(&ptk->tk[16], &ptk->tk[24], 8);
741 os_memcpy(&ptk->tk[24], buf, 8);
Hai Shalom81f62d82019-07-22 12:10:00 -0700742 forced_memzero(buf, sizeof(buf));
Dmitry Shmidt98660862014-03-11 17:26:21 -0700743 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700744 sm->tptk_set = 1;
745
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800746 kde = sm->assoc_wpa_ie;
747 kde_len = sm->assoc_wpa_ie_len;
Hai Shalomc3565922019-10-28 11:58:20 -0700748 kde_buf = os_malloc(kde_len +
749 2 + RSN_SELECTOR_LEN + 3 +
750 sm->assoc_rsnxe_len +
Hai Shalom4fbc08f2020-05-18 12:37:00 -0700751 2 + RSN_SELECTOR_LEN + 1 +
752 2 + RSN_SELECTOR_LEN + 2);
Hai Shalomc3565922019-10-28 11:58:20 -0700753 if (!kde_buf)
754 goto failed;
755 os_memcpy(kde_buf, kde, kde_len);
756 kde = kde_buf;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800757
Hai Shalom74f70d42019-02-11 14:42:39 -0800758#ifdef CONFIG_OCV
759 if (wpa_sm_ocv_enabled(sm)) {
760 struct wpa_channel_info ci;
761 u8 *pos;
762
Hai Shalomc3565922019-10-28 11:58:20 -0700763 pos = kde + kde_len;
Hai Shalom74f70d42019-02-11 14:42:39 -0800764 if (wpa_sm_channel_info(sm, &ci) != 0) {
765 wpa_printf(MSG_WARNING,
766 "Failed to get channel info for OCI element in EAPOL-Key 2/4");
767 goto failed;
768 }
Hai Shalom899fcc72020-10-19 14:38:18 -0700769#ifdef CONFIG_TESTING_OPTIONS
770 if (sm->oci_freq_override_eapol) {
771 wpa_printf(MSG_INFO,
772 "TEST: Override OCI KDE frequency %d -> %d MHz",
773 ci.frequency, sm->oci_freq_override_eapol);
774 ci.frequency = sm->oci_freq_override_eapol;
775 }
776#endif /* CONFIG_TESTING_OPTIONS */
Hai Shalom74f70d42019-02-11 14:42:39 -0800777
Hai Shalom74f70d42019-02-11 14:42:39 -0800778 if (ocv_insert_oci_kde(&ci, &pos) < 0)
779 goto failed;
780 kde_len = pos - kde;
781 }
782#endif /* CONFIG_OCV */
783
Hai Shalomc3565922019-10-28 11:58:20 -0700784 if (sm->assoc_rsnxe && sm->assoc_rsnxe_len) {
785 os_memcpy(kde + kde_len, sm->assoc_rsnxe, sm->assoc_rsnxe_len);
786 kde_len += sm->assoc_rsnxe_len;
787 }
788
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800789#ifdef CONFIG_P2P
790 if (sm->p2p) {
Hai Shalomc3565922019-10-28 11:58:20 -0700791 u8 *pos;
792
793 wpa_printf(MSG_DEBUG,
794 "P2P: Add IP Address Request KDE into EAPOL-Key 2/4");
795 pos = kde + kde_len;
796 *pos++ = WLAN_EID_VENDOR_SPECIFIC;
797 *pos++ = RSN_SELECTOR_LEN + 1;
798 RSN_SELECTOR_PUT(pos, WFA_KEY_DATA_IP_ADDR_REQ);
799 pos += RSN_SELECTOR_LEN;
800 *pos++ = 0x01;
801 kde_len = pos - kde;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800802 }
803#endif /* CONFIG_P2P */
804
Hai Shalom4fbc08f2020-05-18 12:37:00 -0700805#ifdef CONFIG_DPP2
806 if (DPP_VERSION > 1 && sm->key_mgmt == WPA_KEY_MGMT_DPP) {
807 u8 *pos;
808
809 wpa_printf(MSG_DEBUG, "DPP: Add DPP KDE into EAPOL-Key 2/4");
810 pos = kde + kde_len;
811 *pos++ = WLAN_EID_VENDOR_SPECIFIC;
812 *pos++ = RSN_SELECTOR_LEN + 2;
813 RSN_SELECTOR_PUT(pos, WFA_KEY_DATA_DPP);
814 pos += RSN_SELECTOR_LEN;
815 *pos++ = DPP_VERSION; /* Protocol Version */
816 *pos = 0; /* Flags */
817 if (sm->dpp_pfs == 0)
818 *pos |= DPP_KDE_PFS_ALLOWED;
819 else if (sm->dpp_pfs == 1)
820 *pos |= DPP_KDE_PFS_ALLOWED | DPP_KDE_PFS_REQUIRED;
821 pos++;
822 kde_len = pos - kde;
823 }
824#endif /* CONFIG_DPP2 */
825
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700826 if (wpa_supplicant_send_2_of_4(sm, sm->bssid, key, ver, sm->snonce,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800827 kde, kde_len, ptk) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700828 goto failed;
829
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800830 os_free(kde_buf);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700831 os_memcpy(sm->anonce, key->key_nonce, WPA_NONCE_LEN);
832 return;
833
834failed:
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800835 os_free(kde_buf);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700836 wpa_sm_deauthenticate(sm, WLAN_REASON_UNSPECIFIED);
837}
838
839
840static void wpa_sm_start_preauth(void *eloop_ctx, void *timeout_ctx)
841{
842 struct wpa_sm *sm = eloop_ctx;
843 rsn_preauth_candidate_process(sm);
844}
845
846
847static void wpa_supplicant_key_neg_complete(struct wpa_sm *sm,
848 const u8 *addr, int secure)
849{
850 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
851 "WPA: Key negotiation completed with "
852 MACSTR " [PTK=%s GTK=%s]", MAC2STR(addr),
853 wpa_cipher_txt(sm->pairwise_cipher),
854 wpa_cipher_txt(sm->group_cipher));
855 wpa_sm_cancel_auth_timeout(sm);
856 wpa_sm_set_state(sm, WPA_COMPLETED);
857
858 if (secure) {
859 wpa_sm_mlme_setprotection(
860 sm, addr, MLME_SETPROTECTION_PROTECT_TYPE_RX_TX,
861 MLME_SETPROTECTION_KEY_TYPE_PAIRWISE);
Hai Shalome21d4e82020-04-29 16:34:06 -0700862 eapol_sm_notify_portValid(sm->eapol, true);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700863 if (wpa_key_mgmt_wpa_psk(sm->key_mgmt) ||
864 sm->key_mgmt == WPA_KEY_MGMT_DPP ||
865 sm->key_mgmt == WPA_KEY_MGMT_OWE)
Hai Shalome21d4e82020-04-29 16:34:06 -0700866 eapol_sm_notify_eap_success(sm->eapol, true);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700867 /*
868 * Start preauthentication after a short wait to avoid a
869 * possible race condition between the data receive and key
870 * configuration after the 4-Way Handshake. This increases the
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800871 * likelihood of the first preauth EAPOL-Start frame getting to
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700872 * the target AP.
873 */
Hai Shalom74f70d42019-02-11 14:42:39 -0800874 if (!dl_list_empty(&sm->pmksa_candidates))
875 eloop_register_timeout(1, 0, wpa_sm_start_preauth,
876 sm, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700877 }
878
879 if (sm->cur_pmksa && sm->cur_pmksa->opportunistic) {
880 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
881 "RSN: Authenticator accepted "
882 "opportunistic PMKSA entry - marking it valid");
883 sm->cur_pmksa->opportunistic = 0;
884 }
885
886#ifdef CONFIG_IEEE80211R
887 if (wpa_key_mgmt_ft(sm->key_mgmt)) {
888 /* Prepare for the next transition */
889 wpa_ft_prepare_auth_request(sm, NULL);
890 }
891#endif /* CONFIG_IEEE80211R */
892}
893
894
895static void wpa_sm_rekey_ptk(void *eloop_ctx, void *timeout_ctx)
896{
897 struct wpa_sm *sm = eloop_ctx;
898 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Request PTK rekeying");
899 wpa_sm_key_request(sm, 0, 1);
900}
901
902
903static int wpa_supplicant_install_ptk(struct wpa_sm *sm,
Hai Shalomfdcde762020-04-02 11:19:20 -0700904 const struct wpa_eapol_key *key,
905 enum key_flag key_flag)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700906{
907 int keylen, rsclen;
908 enum wpa_alg alg;
909 const u8 *key_rsc;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800910
Mathy Vanhoefc66556c2017-09-29 04:22:51 +0200911 if (sm->ptk.installed) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800912 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
913 "WPA: Do not re-install same PTK to the driver");
914 return 0;
915 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700916
917 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
918 "WPA: Installing PTK to the driver");
919
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700920 if (sm->pairwise_cipher == WPA_CIPHER_NONE) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700921 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Pairwise Cipher "
922 "Suite: NONE - do not use pairwise keys");
923 return 0;
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700924 }
925
926 if (!wpa_cipher_valid_pairwise(sm->pairwise_cipher)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700927 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
928 "WPA: Unsupported pairwise cipher %d",
929 sm->pairwise_cipher);
930 return -1;
931 }
932
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700933 alg = wpa_cipher_to_alg(sm->pairwise_cipher);
934 keylen = wpa_cipher_key_len(sm->pairwise_cipher);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700935 if (keylen <= 0 || (unsigned int) keylen != sm->ptk.tk_len) {
936 wpa_printf(MSG_DEBUG, "WPA: TK length mismatch: %d != %lu",
937 keylen, (long unsigned int) sm->ptk.tk_len);
938 return -1;
939 }
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700940 rsclen = wpa_cipher_rsc_len(sm->pairwise_cipher);
941
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800942 if (sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700943 key_rsc = null_rsc;
944 } else {
945 key_rsc = key->key_rsc;
946 wpa_hexdump(MSG_DEBUG, "WPA: RSC", key_rsc, rsclen);
947 }
948
Hai Shalomfdcde762020-04-02 11:19:20 -0700949 if (wpa_sm_set_key(sm, alg, sm->bssid, sm->keyidx_active, 1, key_rsc,
950 rsclen, sm->ptk.tk, keylen,
951 KEY_FLAG_PAIRWISE | key_flag) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700952 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
Hai Shalomfdcde762020-04-02 11:19:20 -0700953 "WPA: Failed to set PTK to the driver (alg=%d keylen=%d bssid="
954 MACSTR " idx=%d key_flag=0x%x)",
955 alg, keylen, MAC2STR(sm->bssid),
956 sm->keyidx_active, key_flag);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700957 return -1;
958 }
959
Hai Shalom60840252021-02-19 19:02:11 -0800960 wpa_sm_store_ptk(sm, sm->bssid, sm->pairwise_cipher,
961 sm->dot11RSNAConfigPMKLifetime, &sm->ptk);
962
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800963 /* TK is not needed anymore in supplicant */
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800964 os_memset(sm->ptk.tk, 0, WPA_TK_MAX_LEN);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700965 sm->ptk.tk_len = 0;
Mathy Vanhoefc66556c2017-09-29 04:22:51 +0200966 sm->ptk.installed = 1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800967
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700968 if (sm->wpa_ptk_rekey) {
969 eloop_cancel_timeout(wpa_sm_rekey_ptk, sm, NULL);
970 eloop_register_timeout(sm->wpa_ptk_rekey, 0, wpa_sm_rekey_ptk,
971 sm, NULL);
972 }
Hai Shalomfdcde762020-04-02 11:19:20 -0700973 return 0;
974}
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700975
Hai Shalomfdcde762020-04-02 11:19:20 -0700976
977static int wpa_supplicant_activate_ptk(struct wpa_sm *sm)
978{
979 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
980 "WPA: Activate PTK (idx=%d bssid=" MACSTR ")",
981 sm->keyidx_active, MAC2STR(sm->bssid));
982
983 if (wpa_sm_set_key(sm, 0, sm->bssid, sm->keyidx_active, 0, NULL, 0,
984 NULL, 0, KEY_FLAG_PAIRWISE_RX_TX_MODIFY) < 0) {
985 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
986 "WPA: Failed to activate PTK for TX (idx=%d bssid="
987 MACSTR ")", sm->keyidx_active, MAC2STR(sm->bssid));
988 return -1;
989 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700990 return 0;
991}
992
993
994static int wpa_supplicant_check_group_cipher(struct wpa_sm *sm,
995 int group_cipher,
996 int keylen, int maxkeylen,
997 int *key_rsc_len,
998 enum wpa_alg *alg)
999{
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07001000 int klen;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001001
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07001002 *alg = wpa_cipher_to_alg(group_cipher);
1003 if (*alg == WPA_ALG_NONE) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001004 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1005 "WPA: Unsupported Group Cipher %d",
1006 group_cipher);
1007 return -1;
1008 }
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07001009 *key_rsc_len = wpa_cipher_rsc_len(group_cipher);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001010
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07001011 klen = wpa_cipher_key_len(group_cipher);
1012 if (keylen != klen || maxkeylen < klen) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001013 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1014 "WPA: Unsupported %s Group Cipher key length %d (%d)",
1015 wpa_cipher_txt(group_cipher), keylen, maxkeylen);
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07001016 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001017 }
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07001018 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001019}
1020
1021
1022struct wpa_gtk_data {
1023 enum wpa_alg alg;
1024 int tx, key_rsc_len, keyidx;
1025 u8 gtk[32];
1026 int gtk_len;
1027};
1028
1029
1030static int wpa_supplicant_install_gtk(struct wpa_sm *sm,
1031 const struct wpa_gtk_data *gd,
Jouni Malinen58c0e962017-10-01 12:12:24 +03001032 const u8 *key_rsc, int wnm_sleep)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001033{
1034 const u8 *_gtk = gd->gtk;
1035 u8 gtk_buf[32];
1036
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02001037 /* Detect possible key reinstallation */
Jouni Malinen58c0e962017-10-01 12:12:24 +03001038 if ((sm->gtk.gtk_len == (size_t) gd->gtk_len &&
1039 os_memcmp(sm->gtk.gtk, gd->gtk, sm->gtk.gtk_len) == 0) ||
1040 (sm->gtk_wnm_sleep.gtk_len == (size_t) gd->gtk_len &&
1041 os_memcmp(sm->gtk_wnm_sleep.gtk, gd->gtk,
1042 sm->gtk_wnm_sleep.gtk_len) == 0)) {
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02001043 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1044 "WPA: Not reinstalling already in-use GTK to the driver (keyidx=%d tx=%d len=%d)",
1045 gd->keyidx, gd->tx, gd->gtk_len);
1046 return 0;
1047 }
1048
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001049 wpa_hexdump_key(MSG_DEBUG, "WPA: Group Key", gd->gtk, gd->gtk_len);
1050 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1051 "WPA: Installing GTK to the driver (keyidx=%d tx=%d len=%d)",
1052 gd->keyidx, gd->tx, gd->gtk_len);
1053 wpa_hexdump(MSG_DEBUG, "WPA: RSC", key_rsc, gd->key_rsc_len);
1054 if (sm->group_cipher == WPA_CIPHER_TKIP) {
1055 /* Swap Tx/Rx keys for Michael MIC */
1056 os_memcpy(gtk_buf, gd->gtk, 16);
1057 os_memcpy(gtk_buf + 16, gd->gtk + 24, 8);
1058 os_memcpy(gtk_buf + 24, gd->gtk + 16, 8);
1059 _gtk = gtk_buf;
1060 }
1061 if (sm->pairwise_cipher == WPA_CIPHER_NONE) {
1062 if (wpa_sm_set_key(sm, gd->alg, NULL,
1063 gd->keyidx, 1, key_rsc, gd->key_rsc_len,
Hai Shalomfdcde762020-04-02 11:19:20 -07001064 _gtk, gd->gtk_len,
1065 KEY_FLAG_GROUP_RX_TX_DEFAULT) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001066 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1067 "WPA: Failed to set GTK to the driver "
1068 "(Group only)");
Hai Shalom81f62d82019-07-22 12:10:00 -07001069 forced_memzero(gtk_buf, sizeof(gtk_buf));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001070 return -1;
1071 }
1072 } else if (wpa_sm_set_key(sm, gd->alg, broadcast_ether_addr,
1073 gd->keyidx, gd->tx, key_rsc, gd->key_rsc_len,
Hai Shalomfdcde762020-04-02 11:19:20 -07001074 _gtk, gd->gtk_len, KEY_FLAG_GROUP_RX) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001075 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1076 "WPA: Failed to set GTK to "
1077 "the driver (alg=%d keylen=%d keyidx=%d)",
1078 gd->alg, gd->gtk_len, gd->keyidx);
Hai Shalom81f62d82019-07-22 12:10:00 -07001079 forced_memzero(gtk_buf, sizeof(gtk_buf));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001080 return -1;
1081 }
Hai Shalom81f62d82019-07-22 12:10:00 -07001082 forced_memzero(gtk_buf, sizeof(gtk_buf));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001083
Jouni Malinen58c0e962017-10-01 12:12:24 +03001084 if (wnm_sleep) {
1085 sm->gtk_wnm_sleep.gtk_len = gd->gtk_len;
1086 os_memcpy(sm->gtk_wnm_sleep.gtk, gd->gtk,
1087 sm->gtk_wnm_sleep.gtk_len);
1088 } else {
1089 sm->gtk.gtk_len = gd->gtk_len;
1090 os_memcpy(sm->gtk.gtk, gd->gtk, sm->gtk.gtk_len);
1091 }
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02001092
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001093 return 0;
1094}
1095
1096
1097static int wpa_supplicant_gtk_tx_bit_workaround(const struct wpa_sm *sm,
1098 int tx)
1099{
1100 if (tx && sm->pairwise_cipher != WPA_CIPHER_NONE) {
1101 /* Ignore Tx bit for GTK if a pairwise key is used. One AP
1102 * seemed to set this bit (incorrectly, since Tx is only when
1103 * doing Group Key only APs) and without this workaround, the
1104 * data connection does not work because wpa_supplicant
1105 * configured non-zero keyidx to be used for unicast. */
1106 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1107 "WPA: Tx bit set for GTK, but pairwise "
1108 "keys are used - ignore Tx bit");
1109 return 0;
1110 }
1111 return tx;
1112}
1113
1114
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001115static int wpa_supplicant_rsc_relaxation(const struct wpa_sm *sm,
1116 const u8 *rsc)
1117{
1118 int rsclen;
1119
1120 if (!sm->wpa_rsc_relaxation)
1121 return 0;
1122
1123 rsclen = wpa_cipher_rsc_len(sm->group_cipher);
1124
1125 /*
1126 * Try to detect RSC (endian) corruption issue where the AP sends
1127 * the RSC bytes in EAPOL-Key message in the wrong order, both if
1128 * it's actually a 6-byte field (as it should be) and if it treats
1129 * it as an 8-byte field.
1130 * An AP model known to have this bug is the Sapido RB-1632.
1131 */
1132 if (rsclen == 6 && ((rsc[5] && !rsc[0]) || rsc[6] || rsc[7])) {
1133 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1134 "RSC %02x%02x%02x%02x%02x%02x%02x%02x is likely bogus, using 0",
1135 rsc[0], rsc[1], rsc[2], rsc[3],
1136 rsc[4], rsc[5], rsc[6], rsc[7]);
1137
1138 return 1;
1139 }
1140
1141 return 0;
1142}
1143
1144
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001145static int wpa_supplicant_pairwise_gtk(struct wpa_sm *sm,
1146 const struct wpa_eapol_key *key,
1147 const u8 *gtk, size_t gtk_len,
1148 int key_info)
1149{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001150 struct wpa_gtk_data gd;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001151 const u8 *key_rsc;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001152
1153 /*
1154 * IEEE Std 802.11i-2004 - 8.5.2 EAPOL-Key frames - Figure 43x
1155 * GTK KDE format:
1156 * KeyID[bits 0-1], Tx [bit 2], Reserved [bits 3-7]
1157 * Reserved [bits 0-7]
1158 * GTK
1159 */
1160
1161 os_memset(&gd, 0, sizeof(gd));
1162 wpa_hexdump_key(MSG_DEBUG, "RSN: received GTK in pairwise handshake",
1163 gtk, gtk_len);
1164
1165 if (gtk_len < 2 || gtk_len - 2 > sizeof(gd.gtk))
1166 return -1;
1167
1168 gd.keyidx = gtk[0] & 0x3;
1169 gd.tx = wpa_supplicant_gtk_tx_bit_workaround(sm,
1170 !!(gtk[0] & BIT(2)));
1171 gtk += 2;
1172 gtk_len -= 2;
1173
1174 os_memcpy(gd.gtk, gtk, gtk_len);
1175 gd.gtk_len = gtk_len;
1176
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001177 key_rsc = key->key_rsc;
1178 if (wpa_supplicant_rsc_relaxation(sm, key->key_rsc))
1179 key_rsc = null_rsc;
1180
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001181 if (sm->group_cipher != WPA_CIPHER_GTK_NOT_USED &&
1182 (wpa_supplicant_check_group_cipher(sm, sm->group_cipher,
1183 gtk_len, gtk_len,
1184 &gd.key_rsc_len, &gd.alg) ||
Jouni Malinen58c0e962017-10-01 12:12:24 +03001185 wpa_supplicant_install_gtk(sm, &gd, key_rsc, 0))) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001186 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1187 "RSN: Failed to install GTK");
Hai Shalom81f62d82019-07-22 12:10:00 -07001188 forced_memzero(&gd, sizeof(gd));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001189 return -1;
1190 }
Hai Shalom81f62d82019-07-22 12:10:00 -07001191 forced_memzero(&gd, sizeof(gd));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001192
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001193 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001194}
1195
1196
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02001197static int wpa_supplicant_install_igtk(struct wpa_sm *sm,
Jouni Malinen58c0e962017-10-01 12:12:24 +03001198 const struct wpa_igtk_kde *igtk,
1199 int wnm_sleep)
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02001200{
1201 size_t len = wpa_cipher_key_len(sm->mgmt_group_cipher);
1202 u16 keyidx = WPA_GET_LE16(igtk->keyid);
1203
1204 /* Detect possible key reinstallation */
Jouni Malinen58c0e962017-10-01 12:12:24 +03001205 if ((sm->igtk.igtk_len == len &&
1206 os_memcmp(sm->igtk.igtk, igtk->igtk, sm->igtk.igtk_len) == 0) ||
1207 (sm->igtk_wnm_sleep.igtk_len == len &&
1208 os_memcmp(sm->igtk_wnm_sleep.igtk, igtk->igtk,
1209 sm->igtk_wnm_sleep.igtk_len) == 0)) {
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02001210 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1211 "WPA: Not reinstalling already in-use IGTK to the driver (keyidx=%d)",
1212 keyidx);
1213 return 0;
1214 }
1215
1216 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
Hai Shalom39ba6fc2019-01-22 12:40:38 -08001217 "WPA: IGTK keyid %d pn " COMPACT_MACSTR,
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02001218 keyidx, MAC2STR(igtk->pn));
1219 wpa_hexdump_key(MSG_DEBUG, "WPA: IGTK", igtk->igtk, len);
1220 if (keyidx > 4095) {
1221 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1222 "WPA: Invalid IGTK KeyID %d", keyidx);
1223 return -1;
1224 }
1225 if (wpa_sm_set_key(sm, wpa_cipher_to_alg(sm->mgmt_group_cipher),
1226 broadcast_ether_addr,
1227 keyidx, 0, igtk->pn, sizeof(igtk->pn),
Hai Shalomfdcde762020-04-02 11:19:20 -07001228 igtk->igtk, len, KEY_FLAG_GROUP_RX) < 0) {
Hai Shalom5f92bc92019-04-18 11:54:11 -07001229 if (keyidx == 0x0400 || keyidx == 0x0500) {
1230 /* Assume the AP has broken PMF implementation since it
1231 * seems to have swapped the KeyID bytes. The AP cannot
1232 * be trusted to implement BIP correctly or provide a
1233 * valid IGTK, so do not try to configure this key with
1234 * swapped KeyID bytes. Instead, continue without
1235 * configuring the IGTK so that the driver can drop any
1236 * received group-addressed robust management frames due
1237 * to missing keys.
1238 *
1239 * Normally, this error behavior would result in us
1240 * disconnecting, but there are number of deployed APs
1241 * with this broken behavior, so as an interoperability
1242 * workaround, allow the connection to proceed. */
1243 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1244 "WPA: Ignore IGTK configuration error due to invalid IGTK KeyID byte order");
1245 } else {
1246 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1247 "WPA: Failed to configure IGTK to the driver");
1248 return -1;
1249 }
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02001250 }
1251
Jouni Malinen58c0e962017-10-01 12:12:24 +03001252 if (wnm_sleep) {
1253 sm->igtk_wnm_sleep.igtk_len = len;
1254 os_memcpy(sm->igtk_wnm_sleep.igtk, igtk->igtk,
1255 sm->igtk_wnm_sleep.igtk_len);
1256 } else {
1257 sm->igtk.igtk_len = len;
1258 os_memcpy(sm->igtk.igtk, igtk->igtk, sm->igtk.igtk_len);
1259 }
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02001260
1261 return 0;
1262}
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02001263
1264
Hai Shalomfdcde762020-04-02 11:19:20 -07001265static int wpa_supplicant_install_bigtk(struct wpa_sm *sm,
1266 const struct wpa_bigtk_kde *bigtk,
1267 int wnm_sleep)
1268{
1269 size_t len = wpa_cipher_key_len(sm->mgmt_group_cipher);
1270 u16 keyidx = WPA_GET_LE16(bigtk->keyid);
1271
1272 /* Detect possible key reinstallation */
1273 if ((sm->bigtk.bigtk_len == len &&
1274 os_memcmp(sm->bigtk.bigtk, bigtk->bigtk,
1275 sm->bigtk.bigtk_len) == 0) ||
1276 (sm->bigtk_wnm_sleep.bigtk_len == len &&
1277 os_memcmp(sm->bigtk_wnm_sleep.bigtk, bigtk->bigtk,
1278 sm->bigtk_wnm_sleep.bigtk_len) == 0)) {
1279 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1280 "WPA: Not reinstalling already in-use BIGTK to the driver (keyidx=%d)",
1281 keyidx);
1282 return 0;
1283 }
1284
1285 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1286 "WPA: BIGTK keyid %d pn " COMPACT_MACSTR,
1287 keyidx, MAC2STR(bigtk->pn));
1288 wpa_hexdump_key(MSG_DEBUG, "WPA: BIGTK", bigtk->bigtk, len);
1289 if (keyidx < 6 || keyidx > 7) {
1290 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1291 "WPA: Invalid BIGTK KeyID %d", keyidx);
1292 return -1;
1293 }
1294 if (wpa_sm_set_key(sm, wpa_cipher_to_alg(sm->mgmt_group_cipher),
1295 broadcast_ether_addr,
1296 keyidx, 0, bigtk->pn, sizeof(bigtk->pn),
1297 bigtk->bigtk, len, KEY_FLAG_GROUP_RX) < 0) {
1298 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1299 "WPA: Failed to configure BIGTK to the driver");
1300 return -1;
1301 }
1302
1303 if (wnm_sleep) {
1304 sm->bigtk_wnm_sleep.bigtk_len = len;
1305 os_memcpy(sm->bigtk_wnm_sleep.bigtk, bigtk->bigtk,
1306 sm->bigtk_wnm_sleep.bigtk_len);
1307 } else {
1308 sm->bigtk.bigtk_len = len;
1309 os_memcpy(sm->bigtk.bigtk, bigtk->bigtk, sm->bigtk.bigtk_len);
1310 }
1311
1312 return 0;
1313}
1314
1315
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001316static int ieee80211w_set_keys(struct wpa_sm *sm,
1317 struct wpa_eapol_ie_parse *ie)
1318{
Hai Shalomfdcde762020-04-02 11:19:20 -07001319 size_t len;
1320
Hai Shalom60840252021-02-19 19:02:11 -08001321 if (!wpa_cipher_valid_mgmt_group(sm->mgmt_group_cipher) ||
1322 sm->mgmt_group_cipher == WPA_CIPHER_GTK_NOT_USED)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001323 return 0;
1324
1325 if (ie->igtk) {
1326 const struct wpa_igtk_kde *igtk;
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02001327
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001328 len = wpa_cipher_key_len(sm->mgmt_group_cipher);
1329 if (ie->igtk_len != WPA_IGTK_KDE_PREFIX_LEN + len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001330 return -1;
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02001331
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001332 igtk = (const struct wpa_igtk_kde *) ie->igtk;
Jouni Malinen58c0e962017-10-01 12:12:24 +03001333 if (wpa_supplicant_install_igtk(sm, igtk, 0) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001334 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001335 }
1336
Hai Shalomfdcde762020-04-02 11:19:20 -07001337 if (ie->bigtk && sm->beacon_prot) {
1338 const struct wpa_bigtk_kde *bigtk;
1339
1340 len = wpa_cipher_key_len(sm->mgmt_group_cipher);
1341 if (ie->bigtk_len != WPA_BIGTK_KDE_PREFIX_LEN + len)
1342 return -1;
1343
1344 bigtk = (const struct wpa_bigtk_kde *) ie->bigtk;
1345 if (wpa_supplicant_install_bigtk(sm, bigtk, 0) < 0)
1346 return -1;
1347 }
1348
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001349 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001350}
1351
1352
1353static void wpa_report_ie_mismatch(struct wpa_sm *sm,
1354 const char *reason, const u8 *src_addr,
1355 const u8 *wpa_ie, size_t wpa_ie_len,
1356 const u8 *rsn_ie, size_t rsn_ie_len)
1357{
1358 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, "WPA: %s (src=" MACSTR ")",
1359 reason, MAC2STR(src_addr));
1360
1361 if (sm->ap_wpa_ie) {
1362 wpa_hexdump(MSG_INFO, "WPA: WPA IE in Beacon/ProbeResp",
1363 sm->ap_wpa_ie, sm->ap_wpa_ie_len);
1364 }
1365 if (wpa_ie) {
1366 if (!sm->ap_wpa_ie) {
1367 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1368 "WPA: No WPA IE in Beacon/ProbeResp");
1369 }
1370 wpa_hexdump(MSG_INFO, "WPA: WPA IE in 3/4 msg",
1371 wpa_ie, wpa_ie_len);
1372 }
1373
1374 if (sm->ap_rsn_ie) {
1375 wpa_hexdump(MSG_INFO, "WPA: RSN IE in Beacon/ProbeResp",
1376 sm->ap_rsn_ie, sm->ap_rsn_ie_len);
1377 }
1378 if (rsn_ie) {
1379 if (!sm->ap_rsn_ie) {
1380 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1381 "WPA: No RSN IE in Beacon/ProbeResp");
1382 }
1383 wpa_hexdump(MSG_INFO, "WPA: RSN IE in 3/4 msg",
1384 rsn_ie, rsn_ie_len);
1385 }
1386
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001387 wpa_sm_deauthenticate(sm, WLAN_REASON_IE_IN_4WAY_DIFFERS);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001388}
1389
1390
1391#ifdef CONFIG_IEEE80211R
1392
1393static int ft_validate_mdie(struct wpa_sm *sm,
1394 const unsigned char *src_addr,
1395 struct wpa_eapol_ie_parse *ie,
1396 const u8 *assoc_resp_mdie)
1397{
1398 struct rsn_mdie *mdie;
1399
1400 mdie = (struct rsn_mdie *) (ie->mdie + 2);
1401 if (ie->mdie == NULL || ie->mdie_len < 2 + sizeof(*mdie) ||
1402 os_memcmp(mdie->mobility_domain, sm->mobility_domain,
1403 MOBILITY_DOMAIN_ID_LEN) != 0) {
1404 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "FT: MDIE in msg 3/4 did "
1405 "not match with the current mobility domain");
1406 return -1;
1407 }
1408
1409 if (assoc_resp_mdie &&
1410 (assoc_resp_mdie[1] != ie->mdie[1] ||
1411 os_memcmp(assoc_resp_mdie, ie->mdie, 2 + ie->mdie[1]) != 0)) {
1412 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "FT: MDIE mismatch");
1413 wpa_hexdump(MSG_DEBUG, "FT: MDIE in EAPOL-Key msg 3/4",
1414 ie->mdie, 2 + ie->mdie[1]);
1415 wpa_hexdump(MSG_DEBUG, "FT: MDIE in (Re)Association Response",
1416 assoc_resp_mdie, 2 + assoc_resp_mdie[1]);
1417 return -1;
1418 }
1419
1420 return 0;
1421}
1422
1423
1424static int ft_validate_ftie(struct wpa_sm *sm,
1425 const unsigned char *src_addr,
1426 struct wpa_eapol_ie_parse *ie,
1427 const u8 *assoc_resp_ftie)
1428{
1429 if (ie->ftie == NULL) {
1430 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1431 "FT: No FTIE in EAPOL-Key msg 3/4");
1432 return -1;
1433 }
1434
1435 if (assoc_resp_ftie == NULL)
1436 return 0;
1437
1438 if (assoc_resp_ftie[1] != ie->ftie[1] ||
1439 os_memcmp(assoc_resp_ftie, ie->ftie, 2 + ie->ftie[1]) != 0) {
1440 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "FT: FTIE mismatch");
1441 wpa_hexdump(MSG_DEBUG, "FT: FTIE in EAPOL-Key msg 3/4",
1442 ie->ftie, 2 + ie->ftie[1]);
1443 wpa_hexdump(MSG_DEBUG, "FT: FTIE in (Re)Association Response",
1444 assoc_resp_ftie, 2 + assoc_resp_ftie[1]);
1445 return -1;
1446 }
1447
1448 return 0;
1449}
1450
1451
1452static int ft_validate_rsnie(struct wpa_sm *sm,
1453 const unsigned char *src_addr,
1454 struct wpa_eapol_ie_parse *ie)
1455{
1456 struct wpa_ie_data rsn;
1457
1458 if (!ie->rsn_ie)
1459 return 0;
1460
1461 /*
1462 * Verify that PMKR1Name from EAPOL-Key message 3/4
1463 * matches with the value we derived.
1464 */
1465 if (wpa_parse_wpa_ie_rsn(ie->rsn_ie, ie->rsn_ie_len, &rsn) < 0 ||
1466 rsn.num_pmkid != 1 || rsn.pmkid == NULL) {
1467 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "FT: No PMKR1Name in "
1468 "FT 4-way handshake message 3/4");
1469 return -1;
1470 }
1471
Dmitry Shmidtc2817022014-07-02 10:32:10 -07001472 if (os_memcmp_const(rsn.pmkid, sm->pmk_r1_name, WPA_PMK_NAME_LEN) != 0)
1473 {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001474 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1475 "FT: PMKR1Name mismatch in "
1476 "FT 4-way handshake message 3/4");
1477 wpa_hexdump(MSG_DEBUG, "FT: PMKR1Name from Authenticator",
1478 rsn.pmkid, WPA_PMK_NAME_LEN);
1479 wpa_hexdump(MSG_DEBUG, "FT: Derived PMKR1Name",
1480 sm->pmk_r1_name, WPA_PMK_NAME_LEN);
1481 return -1;
1482 }
1483
1484 return 0;
1485}
1486
1487
1488static int wpa_supplicant_validate_ie_ft(struct wpa_sm *sm,
1489 const unsigned char *src_addr,
1490 struct wpa_eapol_ie_parse *ie)
1491{
1492 const u8 *pos, *end, *mdie = NULL, *ftie = NULL;
1493
1494 if (sm->assoc_resp_ies) {
1495 pos = sm->assoc_resp_ies;
1496 end = pos + sm->assoc_resp_ies_len;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001497 while (end - pos > 2) {
1498 if (2 + pos[1] > end - pos)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001499 break;
1500 switch (*pos) {
1501 case WLAN_EID_MOBILITY_DOMAIN:
1502 mdie = pos;
1503 break;
1504 case WLAN_EID_FAST_BSS_TRANSITION:
1505 ftie = pos;
1506 break;
1507 }
1508 pos += 2 + pos[1];
1509 }
1510 }
1511
1512 if (ft_validate_mdie(sm, src_addr, ie, mdie) < 0 ||
1513 ft_validate_ftie(sm, src_addr, ie, ftie) < 0 ||
1514 ft_validate_rsnie(sm, src_addr, ie) < 0)
1515 return -1;
1516
1517 return 0;
1518}
1519
1520#endif /* CONFIG_IEEE80211R */
1521
1522
1523static int wpa_supplicant_validate_ie(struct wpa_sm *sm,
1524 const unsigned char *src_addr,
1525 struct wpa_eapol_ie_parse *ie)
1526{
1527 if (sm->ap_wpa_ie == NULL && sm->ap_rsn_ie == NULL) {
1528 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1529 "WPA: No WPA/RSN IE for this AP known. "
1530 "Trying to get from scan results");
1531 if (wpa_sm_get_beacon_ie(sm) < 0) {
1532 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1533 "WPA: Could not find AP from "
1534 "the scan results");
Hai Shalomfdcde762020-04-02 11:19:20 -07001535 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001536 }
Hai Shalomfdcde762020-04-02 11:19:20 -07001537 wpa_msg(sm->ctx->msg_ctx, MSG_DEBUG,
1538 "WPA: Found the current AP from updated scan results");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001539 }
1540
1541 if (ie->wpa_ie == NULL && ie->rsn_ie == NULL &&
1542 (sm->ap_wpa_ie || sm->ap_rsn_ie)) {
1543 wpa_report_ie_mismatch(sm, "IE in 3/4 msg does not match "
1544 "with IE in Beacon/ProbeResp (no IE?)",
1545 src_addr, ie->wpa_ie, ie->wpa_ie_len,
1546 ie->rsn_ie, ie->rsn_ie_len);
1547 return -1;
1548 }
1549
1550 if ((ie->wpa_ie && sm->ap_wpa_ie &&
1551 (ie->wpa_ie_len != sm->ap_wpa_ie_len ||
1552 os_memcmp(ie->wpa_ie, sm->ap_wpa_ie, ie->wpa_ie_len) != 0)) ||
1553 (ie->rsn_ie && sm->ap_rsn_ie &&
1554 wpa_compare_rsn_ie(wpa_key_mgmt_ft(sm->key_mgmt),
1555 sm->ap_rsn_ie, sm->ap_rsn_ie_len,
1556 ie->rsn_ie, ie->rsn_ie_len))) {
1557 wpa_report_ie_mismatch(sm, "IE in 3/4 msg does not match "
1558 "with IE in Beacon/ProbeResp",
1559 src_addr, ie->wpa_ie, ie->wpa_ie_len,
1560 ie->rsn_ie, ie->rsn_ie_len);
1561 return -1;
1562 }
1563
1564 if (sm->proto == WPA_PROTO_WPA &&
1565 ie->rsn_ie && sm->ap_rsn_ie == NULL && sm->rsn_enabled) {
1566 wpa_report_ie_mismatch(sm, "Possible downgrade attack "
1567 "detected - RSN was enabled and RSN IE "
1568 "was in msg 3/4, but not in "
1569 "Beacon/ProbeResp",
1570 src_addr, ie->wpa_ie, ie->wpa_ie_len,
1571 ie->rsn_ie, ie->rsn_ie_len);
1572 return -1;
1573 }
1574
Hai Shalom60840252021-02-19 19:02:11 -08001575 if (sm->proto == WPA_PROTO_RSN &&
1576 ((sm->ap_rsnxe && !ie->rsnxe) ||
1577 (!sm->ap_rsnxe && ie->rsnxe) ||
1578 (sm->ap_rsnxe && ie->rsnxe &&
1579 (sm->ap_rsnxe_len != ie->rsnxe_len ||
1580 os_memcmp(sm->ap_rsnxe, ie->rsnxe, sm->ap_rsnxe_len) != 0)))) {
Hai Shalomc3565922019-10-28 11:58:20 -07001581 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1582 "WPA: RSNXE mismatch between Beacon/ProbeResp and EAPOL-Key msg 3/4");
Hai Shalomfdcde762020-04-02 11:19:20 -07001583 wpa_hexdump(MSG_INFO, "RSNXE in Beacon/ProbeResp",
1584 sm->ap_rsnxe, sm->ap_rsnxe_len);
1585 wpa_hexdump(MSG_INFO, "RSNXE in EAPOL-Key msg 3/4",
1586 ie->rsnxe, ie->rsnxe_len);
1587 wpa_sm_deauthenticate(sm, WLAN_REASON_IE_IN_4WAY_DIFFERS);
Hai Shalomc3565922019-10-28 11:58:20 -07001588 return -1;
1589 }
1590
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001591#ifdef CONFIG_IEEE80211R
1592 if (wpa_key_mgmt_ft(sm->key_mgmt) &&
1593 wpa_supplicant_validate_ie_ft(sm, src_addr, ie) < 0)
1594 return -1;
1595#endif /* CONFIG_IEEE80211R */
1596
1597 return 0;
1598}
1599
1600
1601/**
1602 * wpa_supplicant_send_4_of_4 - Send message 4 of WPA/RSN 4-Way Handshake
1603 * @sm: Pointer to WPA state machine data from wpa_sm_init()
1604 * @dst: Destination address for the frame
1605 * @key: Pointer to the EAPOL-Key frame header
1606 * @ver: Version bits from EAPOL-Key Key Info
1607 * @key_info: Key Info
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001608 * @ptk: PTK to use for keyed hash and encryption
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001609 * Returns: >= 0 on success, < 0 on failure
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001610 */
1611int wpa_supplicant_send_4_of_4(struct wpa_sm *sm, const unsigned char *dst,
1612 const struct wpa_eapol_key *key,
1613 u16 ver, u16 key_info,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001614 struct wpa_ptk *ptk)
1615{
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001616 size_t mic_len, hdrlen, rlen;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001617 struct wpa_eapol_key *reply;
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001618 u8 *rbuf, *key_mic;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001619
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001620 mic_len = wpa_mic_len(sm->key_mgmt, sm->pmk_len);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001621 hdrlen = sizeof(*reply) + mic_len + 2;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001622 rbuf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_KEY, NULL,
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001623 hdrlen, &rlen, (void *) &reply);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001624 if (rbuf == NULL)
1625 return -1;
1626
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001627 reply->type = (sm->proto == WPA_PROTO_RSN ||
1628 sm->proto == WPA_PROTO_OSEN) ?
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001629 EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA;
1630 key_info &= WPA_KEY_INFO_SECURE;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001631 key_info |= ver | WPA_KEY_INFO_KEY_TYPE;
1632 if (mic_len)
1633 key_info |= WPA_KEY_INFO_MIC;
1634 else
1635 key_info |= WPA_KEY_INFO_ENCR_KEY_DATA;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001636 WPA_PUT_BE16(reply->key_info, key_info);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001637 if (sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001638 WPA_PUT_BE16(reply->key_length, 0);
1639 else
1640 os_memcpy(reply->key_length, key->key_length, 2);
1641 os_memcpy(reply->replay_counter, key->replay_counter,
1642 WPA_REPLAY_COUNTER_LEN);
1643
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001644 key_mic = (u8 *) (reply + 1);
1645 WPA_PUT_BE16(key_mic + mic_len, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001646
Roshan Pius5e7db942018-04-12 12:27:41 -07001647 wpa_dbg(sm->ctx->msg_ctx, MSG_INFO, "WPA: Sending EAPOL-Key 4/4");
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001648 return wpa_eapol_key_send(sm, ptk, ver, dst, ETH_P_EAPOL, rbuf, rlen,
1649 key_mic);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001650}
1651
1652
1653static void wpa_supplicant_process_3_of_4(struct wpa_sm *sm,
1654 const struct wpa_eapol_key *key,
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001655 u16 ver, const u8 *key_data,
1656 size_t key_data_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001657{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001658 u16 key_info, keylen;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001659 struct wpa_eapol_ie_parse ie;
1660
1661 wpa_sm_set_state(sm, WPA_4WAY_HANDSHAKE);
Roshan Pius5e7db942018-04-12 12:27:41 -07001662 wpa_dbg(sm->ctx->msg_ctx, MSG_INFO, "WPA: RX message 3 of 4-Way "
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001663 "Handshake from " MACSTR " (ver=%d)", MAC2STR(sm->bssid), ver);
1664
1665 key_info = WPA_GET_BE16(key->key_info);
1666
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001667 wpa_hexdump(MSG_DEBUG, "WPA: IE KeyData", key_data, key_data_len);
1668 if (wpa_supplicant_parse_ies(key_data, key_data_len, &ie) < 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001669 goto failed;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001670 if (ie.gtk && !(key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
1671 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1672 "WPA: GTK IE in unencrypted key data");
1673 goto failed;
1674 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001675 if (ie.igtk && !(key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
1676 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1677 "WPA: IGTK KDE in unencrypted key data");
1678 goto failed;
1679 }
1680
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001681 if (ie.igtk &&
Hai Shalom60840252021-02-19 19:02:11 -08001682 sm->mgmt_group_cipher != WPA_CIPHER_GTK_NOT_USED &&
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001683 wpa_cipher_valid_mgmt_group(sm->mgmt_group_cipher) &&
1684 ie.igtk_len != WPA_IGTK_KDE_PREFIX_LEN +
1685 (unsigned int) wpa_cipher_key_len(sm->mgmt_group_cipher)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001686 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1687 "WPA: Invalid IGTK KDE length %lu",
1688 (unsigned long) ie.igtk_len);
1689 goto failed;
1690 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001691
1692 if (wpa_supplicant_validate_ie(sm, sm->bssid, &ie) < 0)
1693 goto failed;
1694
Hai Shalomfdcde762020-04-02 11:19:20 -07001695 if (wpa_handle_ext_key_id(sm, &ie))
1696 goto failed;
1697
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001698 if (os_memcmp(sm->anonce, key->key_nonce, WPA_NONCE_LEN) != 0) {
1699 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1700 "WPA: ANonce from message 1 of 4-Way Handshake "
1701 "differs from 3 of 4-Way Handshake - drop packet (src="
1702 MACSTR ")", MAC2STR(sm->bssid));
1703 goto failed;
1704 }
1705
1706 keylen = WPA_GET_BE16(key->key_length);
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07001707 if (keylen != wpa_cipher_key_len(sm->pairwise_cipher)) {
1708 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1709 "WPA: Invalid %s key length %d (src=" MACSTR
1710 ")", wpa_cipher_txt(sm->pairwise_cipher), keylen,
1711 MAC2STR(sm->bssid));
1712 goto failed;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001713 }
1714
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001715#ifdef CONFIG_P2P
1716 if (ie.ip_addr_alloc) {
1717 os_memcpy(sm->p2p_ip_addr, ie.ip_addr_alloc, 3 * 4);
1718 wpa_hexdump(MSG_DEBUG, "P2P: IP address info",
1719 sm->p2p_ip_addr, sizeof(sm->p2p_ip_addr));
1720 }
1721#endif /* CONFIG_P2P */
1722
Hai Shalom74f70d42019-02-11 14:42:39 -08001723#ifdef CONFIG_OCV
1724 if (wpa_sm_ocv_enabled(sm)) {
1725 struct wpa_channel_info ci;
1726
1727 if (wpa_sm_channel_info(sm, &ci) != 0) {
1728 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1729 "Failed to get channel info to validate received OCI in EAPOL-Key 3/4");
1730 return;
1731 }
1732
1733 if (ocv_verify_tx_params(ie.oci, ie.oci_len, &ci,
1734 channel_width_to_int(ci.chanwidth),
Hai Shalom899fcc72020-10-19 14:38:18 -07001735 ci.seg1_idx) != OCI_SUCCESS) {
1736 wpa_msg(sm->ctx->msg_ctx, MSG_INFO, OCV_FAILURE
1737 "addr=" MACSTR " frame=eapol-key-m3 error=%s",
1738 MAC2STR(sm->bssid), ocv_errorstr);
Hai Shalom74f70d42019-02-11 14:42:39 -08001739 return;
1740 }
1741 }
1742#endif /* CONFIG_OCV */
1743
Hai Shalom4fbc08f2020-05-18 12:37:00 -07001744#ifdef CONFIG_DPP2
1745 if (DPP_VERSION > 1 && ie.dpp_kde) {
1746 wpa_printf(MSG_DEBUG,
1747 "DPP: peer Protocol Version %u Flags 0x%x",
1748 ie.dpp_kde[0], ie.dpp_kde[1]);
1749 if (sm->key_mgmt == WPA_KEY_MGMT_DPP && sm->dpp_pfs != 2 &&
1750 (ie.dpp_kde[1] & DPP_KDE_PFS_ALLOWED) && !sm->dpp_z) {
1751 wpa_printf(MSG_INFO,
1752 "DPP: Peer indicated it supports PFS and local configuration allows this, but PFS was not negotiated for the association");
1753 goto failed;
1754 }
1755 }
1756#endif /* CONFIG_DPP2 */
1757
Hai Shalomfdcde762020-04-02 11:19:20 -07001758 if (sm->use_ext_key_id &&
1759 wpa_supplicant_install_ptk(sm, key, KEY_FLAG_RX))
1760 goto failed;
1761
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001762 if (wpa_supplicant_send_4_of_4(sm, sm->bssid, key, ver, key_info,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001763 &sm->ptk) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001764 goto failed;
1765 }
1766
1767 /* SNonce was successfully used in msg 3/4, so mark it to be renewed
1768 * for the next 4-Way Handshake. If msg 3 is received again, the old
1769 * SNonce will still be used to avoid changing PTK. */
1770 sm->renew_snonce = 1;
1771
1772 if (key_info & WPA_KEY_INFO_INSTALL) {
Hai Shalomfdcde762020-04-02 11:19:20 -07001773 int res;
1774
1775 if (sm->use_ext_key_id)
1776 res = wpa_supplicant_activate_ptk(sm);
1777 else
1778 res = wpa_supplicant_install_ptk(sm, key,
1779 KEY_FLAG_RX_TX);
1780 if (res)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001781 goto failed;
1782 }
1783
1784 if (key_info & WPA_KEY_INFO_SECURE) {
1785 wpa_sm_mlme_setprotection(
1786 sm, sm->bssid, MLME_SETPROTECTION_PROTECT_TYPE_RX,
1787 MLME_SETPROTECTION_KEY_TYPE_PAIRWISE);
Hai Shalome21d4e82020-04-29 16:34:06 -07001788 eapol_sm_notify_portValid(sm->eapol, true);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001789 }
1790 wpa_sm_set_state(sm, WPA_GROUP_HANDSHAKE);
1791
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001792 if (sm->group_cipher == WPA_CIPHER_GTK_NOT_USED) {
Hai Shalom5f92bc92019-04-18 11:54:11 -07001793 /* No GTK to be set to the driver */
1794 } else if (!ie.gtk && sm->proto == WPA_PROTO_RSN) {
1795 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1796 "RSN: No GTK KDE included in EAPOL-Key msg 3/4");
1797 goto failed;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001798 } else if (ie.gtk &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001799 wpa_supplicant_pairwise_gtk(sm, key,
1800 ie.gtk, ie.gtk_len, key_info) < 0) {
1801 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1802 "RSN: Failed to configure GTK");
1803 goto failed;
1804 }
1805
1806 if (ieee80211w_set_keys(sm, &ie) < 0) {
1807 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1808 "RSN: Failed to configure IGTK");
1809 goto failed;
1810 }
1811
Hai Shalom5f92bc92019-04-18 11:54:11 -07001812 if (sm->group_cipher == WPA_CIPHER_GTK_NOT_USED || ie.gtk)
1813 wpa_supplicant_key_neg_complete(sm, sm->bssid,
1814 key_info & WPA_KEY_INFO_SECURE);
1815
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001816 if (ie.gtk)
1817 wpa_sm_set_rekey_offload(sm);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001818
Hai Shalom39ba6fc2019-01-22 12:40:38 -08001819 /* Add PMKSA cache entry for Suite B AKMs here since PMKID can be
1820 * calculated only after KCK has been derived. Though, do not replace an
1821 * existing PMKSA entry after each 4-way handshake (i.e., new KCK/PMKID)
1822 * to avoid unnecessary changes of PMKID while continuing to use the
1823 * same PMK. */
1824 if (sm->proto == WPA_PROTO_RSN && wpa_key_mgmt_suite_b(sm->key_mgmt) &&
1825 !sm->cur_pmksa) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001826 struct rsn_pmksa_cache_entry *sa;
1827
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001828 sa = pmksa_cache_add(sm->pmksa, sm->pmk, sm->pmk_len, NULL,
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001829 sm->ptk.kck, sm->ptk.kck_len,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001830 sm->bssid, sm->own_addr,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001831 sm->network_ctx, sm->key_mgmt, NULL);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001832 if (!sm->cur_pmksa)
1833 sm->cur_pmksa = sa;
1834 }
1835
Hai Shalomfdcde762020-04-02 11:19:20 -07001836 if (ie.transition_disable)
1837 wpa_sm_transition_disable(sm, ie.transition_disable[0]);
Dmitry Shmidt4dd28dc2015-03-10 11:21:43 -07001838 sm->msg_3_of_4_ok = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001839 return;
1840
1841failed:
1842 wpa_sm_deauthenticate(sm, WLAN_REASON_UNSPECIFIED);
1843}
1844
1845
1846static int wpa_supplicant_process_1_of_2_rsn(struct wpa_sm *sm,
1847 const u8 *keydata,
1848 size_t keydatalen,
1849 u16 key_info,
1850 struct wpa_gtk_data *gd)
1851{
1852 int maxkeylen;
1853 struct wpa_eapol_ie_parse ie;
Hai Shalom60840252021-02-19 19:02:11 -08001854 u16 gtk_len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001855
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08001856 wpa_hexdump_key(MSG_DEBUG, "RSN: msg 1/2 key data",
1857 keydata, keydatalen);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001858 if (wpa_supplicant_parse_ies(keydata, keydatalen, &ie) < 0)
1859 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001860 if (ie.gtk && !(key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
1861 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1862 "WPA: GTK IE in unencrypted key data");
1863 return -1;
1864 }
1865 if (ie.gtk == NULL) {
1866 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1867 "WPA: No GTK IE in Group Key msg 1/2");
1868 return -1;
1869 }
Hai Shalom60840252021-02-19 19:02:11 -08001870 gtk_len = ie.gtk_len;
1871 if (gtk_len < 2) {
1872 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1873 "RSN: Invalid GTK KDE length (%u) in Group Key msg 1/2",
1874 gtk_len);
1875 return -1;
1876 }
1877 gtk_len -= 2;
1878 if (gtk_len > sizeof(gd->gtk)) {
1879 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1880 "RSN: Too long GTK in GTK KDE (len=%u)", gtk_len);
1881 return -1;
1882 }
1883 maxkeylen = gd->gtk_len = gtk_len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001884
Hai Shalom74f70d42019-02-11 14:42:39 -08001885#ifdef CONFIG_OCV
1886 if (wpa_sm_ocv_enabled(sm)) {
1887 struct wpa_channel_info ci;
1888
1889 if (wpa_sm_channel_info(sm, &ci) != 0) {
1890 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1891 "Failed to get channel info to validate received OCI in EAPOL-Key group msg 1/2");
1892 return -1;
1893 }
1894
1895 if (ocv_verify_tx_params(ie.oci, ie.oci_len, &ci,
1896 channel_width_to_int(ci.chanwidth),
Hai Shalom899fcc72020-10-19 14:38:18 -07001897 ci.seg1_idx) != OCI_SUCCESS) {
1898 wpa_msg(sm->ctx->msg_ctx, MSG_INFO, OCV_FAILURE
1899 "addr=" MACSTR " frame=eapol-key-g1 error=%s",
1900 MAC2STR(sm->bssid), ocv_errorstr);
Hai Shalom74f70d42019-02-11 14:42:39 -08001901 return -1;
1902 }
1903 }
1904#endif /* CONFIG_OCV */
1905
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001906 if (wpa_supplicant_check_group_cipher(sm, sm->group_cipher,
Hai Shalom60840252021-02-19 19:02:11 -08001907 gtk_len, maxkeylen,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001908 &gd->key_rsc_len, &gd->alg))
1909 return -1;
1910
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001911 wpa_hexdump_key(MSG_DEBUG, "RSN: received GTK in group key handshake",
Hai Shalom60840252021-02-19 19:02:11 -08001912 ie.gtk, 2 + gtk_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001913 gd->keyidx = ie.gtk[0] & 0x3;
1914 gd->tx = wpa_supplicant_gtk_tx_bit_workaround(sm,
1915 !!(ie.gtk[0] & BIT(2)));
Hai Shalom60840252021-02-19 19:02:11 -08001916 os_memcpy(gd->gtk, ie.gtk + 2, gtk_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001917
1918 if (ieee80211w_set_keys(sm, &ie) < 0)
1919 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1920 "RSN: Failed to configure IGTK");
1921
1922 return 0;
1923}
1924
1925
1926static int wpa_supplicant_process_1_of_2_wpa(struct wpa_sm *sm,
1927 const struct wpa_eapol_key *key,
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001928 const u8 *key_data,
1929 size_t key_data_len, u16 key_info,
1930 u16 ver, struct wpa_gtk_data *gd)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001931{
1932 size_t maxkeylen;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001933 u16 gtk_len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001934
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001935 gtk_len = WPA_GET_BE16(key->key_length);
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001936 maxkeylen = key_data_len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001937 if (ver == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
1938 if (maxkeylen < 8) {
1939 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1940 "WPA: Too short maxkeylen (%lu)",
1941 (unsigned long) maxkeylen);
1942 return -1;
1943 }
1944 maxkeylen -= 8;
1945 }
1946
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001947 if (gtk_len > maxkeylen ||
1948 wpa_supplicant_check_group_cipher(sm, sm->group_cipher,
1949 gtk_len, maxkeylen,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001950 &gd->key_rsc_len, &gd->alg))
1951 return -1;
1952
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001953 gd->gtk_len = gtk_len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001954 gd->keyidx = (key_info & WPA_KEY_INFO_KEY_INDEX_MASK) >>
1955 WPA_KEY_INFO_KEY_INDEX_SHIFT;
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001956 if (ver == WPA_KEY_INFO_TYPE_HMAC_MD5_RC4 && sm->ptk.kek_len == 16) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001957#ifdef CONFIG_NO_RC4
1958 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1959 "WPA: RC4 not supported in the build");
1960 return -1;
1961#else /* CONFIG_NO_RC4 */
Dmitry Shmidt61593f02014-04-21 16:27:35 -07001962 u8 ek[32];
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001963 if (key_data_len > sizeof(gd->gtk)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001964 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1965 "WPA: RC4 key data too long (%lu)",
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001966 (unsigned long) key_data_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001967 return -1;
1968 }
Dmitry Shmidt61593f02014-04-21 16:27:35 -07001969 os_memcpy(ek, key->key_iv, 16);
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001970 os_memcpy(ek + 16, sm->ptk.kek, sm->ptk.kek_len);
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001971 os_memcpy(gd->gtk, key_data, key_data_len);
1972 if (rc4_skip(ek, 32, 256, gd->gtk, key_data_len)) {
Hai Shalom81f62d82019-07-22 12:10:00 -07001973 forced_memzero(ek, sizeof(ek));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001974 wpa_msg(sm->ctx->msg_ctx, MSG_ERROR,
1975 "WPA: RC4 failed");
1976 return -1;
1977 }
Hai Shalom81f62d82019-07-22 12:10:00 -07001978 forced_memzero(ek, sizeof(ek));
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001979#endif /* CONFIG_NO_RC4 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001980 } else if (ver == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001981 if (maxkeylen % 8) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001982 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1983 "WPA: Unsupported AES-WRAP len %lu",
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001984 (unsigned long) maxkeylen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001985 return -1;
1986 }
1987 if (maxkeylen > sizeof(gd->gtk)) {
1988 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1989 "WPA: AES-WRAP key data "
1990 "too long (keydatalen=%lu maxkeylen=%lu)",
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001991 (unsigned long) key_data_len,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001992 (unsigned long) maxkeylen);
1993 return -1;
1994 }
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001995 if (aes_unwrap(sm->ptk.kek, sm->ptk.kek_len, maxkeylen / 8,
1996 key_data, gd->gtk)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001997 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1998 "WPA: AES unwrap failed - could not decrypt "
1999 "GTK");
2000 return -1;
2001 }
2002 } else {
2003 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
2004 "WPA: Unsupported key_info type %d", ver);
2005 return -1;
2006 }
2007 gd->tx = wpa_supplicant_gtk_tx_bit_workaround(
2008 sm, !!(key_info & WPA_KEY_INFO_TXRX));
2009 return 0;
2010}
2011
2012
2013static int wpa_supplicant_send_2_of_2(struct wpa_sm *sm,
2014 const struct wpa_eapol_key *key,
2015 int ver, u16 key_info)
2016{
Dmitry Shmidt807291d2015-01-27 13:40:23 -08002017 size_t mic_len, hdrlen, rlen;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002018 struct wpa_eapol_key *reply;
Dmitry Shmidt807291d2015-01-27 13:40:23 -08002019 u8 *rbuf, *key_mic;
Hai Shalom74f70d42019-02-11 14:42:39 -08002020 size_t kde_len = 0;
2021
2022#ifdef CONFIG_OCV
2023 if (wpa_sm_ocv_enabled(sm))
2024 kde_len = OCV_OCI_KDE_LEN;
2025#endif /* CONFIG_OCV */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002026
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002027 mic_len = wpa_mic_len(sm->key_mgmt, sm->pmk_len);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002028 hdrlen = sizeof(*reply) + mic_len + 2;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002029 rbuf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_KEY, NULL,
Hai Shalom74f70d42019-02-11 14:42:39 -08002030 hdrlen + kde_len, &rlen, (void *) &reply);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002031 if (rbuf == NULL)
2032 return -1;
2033
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002034 reply->type = (sm->proto == WPA_PROTO_RSN ||
2035 sm->proto == WPA_PROTO_OSEN) ?
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002036 EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA;
2037 key_info &= WPA_KEY_INFO_KEY_INDEX_MASK;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002038 key_info |= ver | WPA_KEY_INFO_SECURE;
2039 if (mic_len)
2040 key_info |= WPA_KEY_INFO_MIC;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002041 else
2042 key_info |= WPA_KEY_INFO_ENCR_KEY_DATA;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002043 WPA_PUT_BE16(reply->key_info, key_info);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002044 if (sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002045 WPA_PUT_BE16(reply->key_length, 0);
2046 else
2047 os_memcpy(reply->key_length, key->key_length, 2);
2048 os_memcpy(reply->replay_counter, key->replay_counter,
2049 WPA_REPLAY_COUNTER_LEN);
2050
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002051 key_mic = (u8 *) (reply + 1);
Hai Shalom74f70d42019-02-11 14:42:39 -08002052 WPA_PUT_BE16(key_mic + mic_len, kde_len); /* Key Data Length */
2053
2054#ifdef CONFIG_OCV
2055 if (wpa_sm_ocv_enabled(sm)) {
2056 struct wpa_channel_info ci;
2057 u8 *pos;
2058
2059 if (wpa_sm_channel_info(sm, &ci) != 0) {
2060 wpa_printf(MSG_WARNING,
2061 "Failed to get channel info for OCI element in EAPOL-Key 2/2");
2062 os_free(rbuf);
2063 return -1;
2064 }
Hai Shalom899fcc72020-10-19 14:38:18 -07002065#ifdef CONFIG_TESTING_OPTIONS
2066 if (sm->oci_freq_override_eapol_g2) {
2067 wpa_printf(MSG_INFO,
2068 "TEST: Override OCI KDE frequency %d -> %d MHz",
2069 ci.frequency,
2070 sm->oci_freq_override_eapol_g2);
2071 ci.frequency = sm->oci_freq_override_eapol_g2;
2072 }
2073#endif /* CONFIG_TESTING_OPTIONS */
Hai Shalom74f70d42019-02-11 14:42:39 -08002074
2075 pos = key_mic + mic_len + 2; /* Key Data */
2076 if (ocv_insert_oci_kde(&ci, &pos) < 0) {
2077 os_free(rbuf);
2078 return -1;
2079 }
2080 }
2081#endif /* CONFIG_OCV */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002082
2083 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Sending EAPOL-Key 2/2");
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002084 return wpa_eapol_key_send(sm, &sm->ptk, ver, sm->bssid, ETH_P_EAPOL,
2085 rbuf, rlen, key_mic);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002086}
2087
2088
2089static void wpa_supplicant_process_1_of_2(struct wpa_sm *sm,
2090 const unsigned char *src_addr,
2091 const struct wpa_eapol_key *key,
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002092 const u8 *key_data,
2093 size_t key_data_len, u16 ver)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002094{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002095 u16 key_info;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002096 int rekey, ret;
2097 struct wpa_gtk_data gd;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002098 const u8 *key_rsc;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002099
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002100 if (!sm->msg_3_of_4_ok && !wpa_fils_is_completed(sm)) {
Dmitry Shmidt4dd28dc2015-03-10 11:21:43 -07002101 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2102 "WPA: Group Key Handshake started prior to completion of 4-way handshake");
2103 goto failed;
2104 }
2105
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002106 os_memset(&gd, 0, sizeof(gd));
2107
2108 rekey = wpa_sm_get_state(sm) == WPA_COMPLETED;
2109 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: RX message 1 of Group Key "
2110 "Handshake from " MACSTR " (ver=%d)", MAC2STR(src_addr), ver);
2111
2112 key_info = WPA_GET_BE16(key->key_info);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002113
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002114 if (sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN) {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002115 ret = wpa_supplicant_process_1_of_2_rsn(sm, key_data,
2116 key_data_len, key_info,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002117 &gd);
2118 } else {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002119 ret = wpa_supplicant_process_1_of_2_wpa(sm, key, key_data,
2120 key_data_len,
2121 key_info, ver, &gd);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002122 }
2123
2124 wpa_sm_set_state(sm, WPA_GROUP_HANDSHAKE);
2125
2126 if (ret)
2127 goto failed;
2128
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002129 key_rsc = key->key_rsc;
2130 if (wpa_supplicant_rsc_relaxation(sm, key->key_rsc))
2131 key_rsc = null_rsc;
2132
Jouni Malinen58c0e962017-10-01 12:12:24 +03002133 if (wpa_supplicant_install_gtk(sm, &gd, key_rsc, 0) ||
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002134 wpa_supplicant_send_2_of_2(sm, key, ver, key_info) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002135 goto failed;
Hai Shalom81f62d82019-07-22 12:10:00 -07002136 forced_memzero(&gd, sizeof(gd));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002137
2138 if (rekey) {
2139 wpa_msg(sm->ctx->msg_ctx, MSG_INFO, "WPA: Group rekeying "
2140 "completed with " MACSTR " [GTK=%s]",
2141 MAC2STR(sm->bssid), wpa_cipher_txt(sm->group_cipher));
2142 wpa_sm_cancel_auth_timeout(sm);
2143 wpa_sm_set_state(sm, WPA_COMPLETED);
2144 } else {
2145 wpa_supplicant_key_neg_complete(sm, sm->bssid,
2146 key_info &
2147 WPA_KEY_INFO_SECURE);
2148 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002149
2150 wpa_sm_set_rekey_offload(sm);
2151
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002152 return;
2153
2154failed:
Hai Shalom81f62d82019-07-22 12:10:00 -07002155 forced_memzero(&gd, sizeof(gd));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002156 wpa_sm_deauthenticate(sm, WLAN_REASON_UNSPECIFIED);
2157}
2158
2159
2160static int wpa_supplicant_verify_eapol_key_mic(struct wpa_sm *sm,
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002161 struct wpa_eapol_key *key,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002162 u16 ver,
2163 const u8 *buf, size_t len)
2164{
Dmitry Shmidt807291d2015-01-27 13:40:23 -08002165 u8 mic[WPA_EAPOL_KEY_MIC_MAX_LEN];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002166 int ok = 0;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002167 size_t mic_len = wpa_mic_len(sm->key_mgmt, sm->pmk_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002168
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002169 os_memcpy(mic, key + 1, mic_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002170 if (sm->tptk_set) {
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002171 os_memset(key + 1, 0, mic_len);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002172 if (wpa_eapol_key_mic(sm->tptk.kck, sm->tptk.kck_len,
2173 sm->key_mgmt,
2174 ver, buf, len, (u8 *) (key + 1)) < 0 ||
2175 os_memcmp_const(mic, key + 1, mic_len) != 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002176 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
2177 "WPA: Invalid EAPOL-Key MIC "
2178 "when using TPTK - ignoring TPTK");
Hai Shalom74f70d42019-02-11 14:42:39 -08002179#ifdef TEST_FUZZ
2180 wpa_printf(MSG_INFO,
2181 "TEST: Ignore Key MIC failure for fuzz testing");
2182 goto continue_fuzz;
2183#endif /* TEST_FUZZ */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002184 } else {
Hai Shalom74f70d42019-02-11 14:42:39 -08002185#ifdef TEST_FUZZ
2186 continue_fuzz:
2187#endif /* TEST_FUZZ */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002188 ok = 1;
2189 sm->tptk_set = 0;
2190 sm->ptk_set = 1;
2191 os_memcpy(&sm->ptk, &sm->tptk, sizeof(sm->ptk));
Dmitry Shmidt61593f02014-04-21 16:27:35 -07002192 os_memset(&sm->tptk, 0, sizeof(sm->tptk));
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002193 /*
2194 * This assures the same TPTK in sm->tptk can never be
Roshan Pius3a1667e2018-07-03 15:17:14 -07002195 * copied twice to sm->ptk as the new PTK. In
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002196 * combination with the installed flag in the wpa_ptk
2197 * struct, this assures the same PTK is only installed
2198 * once.
2199 */
2200 sm->renew_snonce = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002201 }
2202 }
2203
2204 if (!ok && sm->ptk_set) {
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002205 os_memset(key + 1, 0, mic_len);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002206 if (wpa_eapol_key_mic(sm->ptk.kck, sm->ptk.kck_len,
2207 sm->key_mgmt,
2208 ver, buf, len, (u8 *) (key + 1)) < 0 ||
2209 os_memcmp_const(mic, key + 1, mic_len) != 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002210 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
2211 "WPA: Invalid EAPOL-Key MIC - "
2212 "dropping packet");
Hai Shalom74f70d42019-02-11 14:42:39 -08002213#ifdef TEST_FUZZ
2214 wpa_printf(MSG_INFO,
2215 "TEST: Ignore Key MIC failure for fuzz testing");
2216 goto continue_fuzz2;
2217#endif /* TEST_FUZZ */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002218 return -1;
2219 }
Hai Shalom74f70d42019-02-11 14:42:39 -08002220#ifdef TEST_FUZZ
2221 continue_fuzz2:
2222#endif /* TEST_FUZZ */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002223 ok = 1;
2224 }
2225
2226 if (!ok) {
2227 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
2228 "WPA: Could not verify EAPOL-Key MIC - "
2229 "dropping packet");
2230 return -1;
2231 }
2232
2233 os_memcpy(sm->rx_replay_counter, key->replay_counter,
2234 WPA_REPLAY_COUNTER_LEN);
2235 sm->rx_replay_counter_set = 1;
2236 return 0;
2237}
2238
2239
2240/* Decrypt RSN EAPOL-Key key data (RC4 or AES-WRAP) */
2241static int wpa_supplicant_decrypt_key_data(struct wpa_sm *sm,
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002242 struct wpa_eapol_key *key,
2243 size_t mic_len, u16 ver,
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002244 u8 *key_data, size_t *key_data_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002245{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002246 wpa_hexdump(MSG_DEBUG, "RSN: encrypted key data",
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002247 key_data, *key_data_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002248 if (!sm->ptk_set) {
2249 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
2250 "WPA: PTK not available, cannot decrypt EAPOL-Key Key "
2251 "Data");
2252 return -1;
2253 }
2254
2255 /* Decrypt key data here so that this operation does not need
2256 * to be implemented separately for each message type. */
Dmitry Shmidt807291d2015-01-27 13:40:23 -08002257 if (ver == WPA_KEY_INFO_TYPE_HMAC_MD5_RC4 && sm->ptk.kek_len == 16) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002258#ifdef CONFIG_NO_RC4
2259 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
2260 "WPA: RC4 not supported in the build");
2261 return -1;
2262#else /* CONFIG_NO_RC4 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002263 u8 ek[32];
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002264
2265 wpa_printf(MSG_DEBUG, "WPA: Decrypt Key Data using RC4");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002266 os_memcpy(ek, key->key_iv, 16);
Dmitry Shmidt807291d2015-01-27 13:40:23 -08002267 os_memcpy(ek + 16, sm->ptk.kek, sm->ptk.kek_len);
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002268 if (rc4_skip(ek, 32, 256, key_data, *key_data_len)) {
Hai Shalom81f62d82019-07-22 12:10:00 -07002269 forced_memzero(ek, sizeof(ek));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002270 wpa_msg(sm->ctx->msg_ctx, MSG_ERROR,
2271 "WPA: RC4 failed");
2272 return -1;
2273 }
Hai Shalom81f62d82019-07-22 12:10:00 -07002274 forced_memzero(ek, sizeof(ek));
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002275#endif /* CONFIG_NO_RC4 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002276 } else if (ver == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES ||
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002277 ver == WPA_KEY_INFO_TYPE_AES_128_CMAC ||
Roshan Pius3a1667e2018-07-03 15:17:14 -07002278 wpa_use_aes_key_wrap(sm->key_mgmt)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002279 u8 *buf;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002280
2281 wpa_printf(MSG_DEBUG,
2282 "WPA: Decrypt Key Data using AES-UNWRAP (KEK length %u)",
2283 (unsigned int) sm->ptk.kek_len);
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002284 if (*key_data_len < 8 || *key_data_len % 8) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002285 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002286 "WPA: Unsupported AES-WRAP len %u",
2287 (unsigned int) *key_data_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002288 return -1;
2289 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002290 *key_data_len -= 8; /* AES-WRAP adds 8 bytes */
2291 buf = os_malloc(*key_data_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002292 if (buf == NULL) {
2293 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
2294 "WPA: No memory for AES-UNWRAP buffer");
2295 return -1;
2296 }
Hai Shalom74f70d42019-02-11 14:42:39 -08002297#ifdef TEST_FUZZ
2298 os_memset(buf, 0x11, *key_data_len);
2299#endif /* TEST_FUZZ */
Dmitry Shmidt807291d2015-01-27 13:40:23 -08002300 if (aes_unwrap(sm->ptk.kek, sm->ptk.kek_len, *key_data_len / 8,
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002301 key_data, buf)) {
Hai Shalom74f70d42019-02-11 14:42:39 -08002302#ifdef TEST_FUZZ
2303 wpa_printf(MSG_INFO,
2304 "TEST: Ignore AES unwrap failure for fuzz testing");
2305 goto continue_fuzz;
2306#endif /* TEST_FUZZ */
Dmitry Shmidt7d56b752015-12-22 10:59:44 -08002307 bin_clear_free(buf, *key_data_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002308 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
2309 "WPA: AES unwrap failed - "
2310 "could not decrypt EAPOL-Key key data");
2311 return -1;
2312 }
Hai Shalom74f70d42019-02-11 14:42:39 -08002313#ifdef TEST_FUZZ
2314 continue_fuzz:
2315#endif /* TEST_FUZZ */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002316 os_memcpy(key_data, buf, *key_data_len);
Dmitry Shmidt7d56b752015-12-22 10:59:44 -08002317 bin_clear_free(buf, *key_data_len);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002318 WPA_PUT_BE16(((u8 *) (key + 1)) + mic_len, *key_data_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002319 } else {
2320 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
2321 "WPA: Unsupported key_info type %d", ver);
2322 return -1;
2323 }
2324 wpa_hexdump_key(MSG_DEBUG, "WPA: decrypted EAPOL-Key key data",
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002325 key_data, *key_data_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002326 return 0;
2327}
2328
2329
2330/**
2331 * wpa_sm_aborted_cached - Notify WPA that PMKSA caching was aborted
2332 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2333 */
2334void wpa_sm_aborted_cached(struct wpa_sm *sm)
2335{
2336 if (sm && sm->cur_pmksa) {
2337 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2338 "RSN: Cancelling PMKSA caching attempt");
2339 sm->cur_pmksa = NULL;
2340 }
2341}
2342
2343
2344static void wpa_eapol_key_dump(struct wpa_sm *sm,
Dmitry Shmidt807291d2015-01-27 13:40:23 -08002345 const struct wpa_eapol_key *key,
2346 unsigned int key_data_len,
2347 const u8 *mic, unsigned int mic_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002348{
2349#ifndef CONFIG_NO_STDOUT_DEBUG
2350 u16 key_info = WPA_GET_BE16(key->key_info);
2351
2352 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, " EAPOL-Key type=%d", key->type);
2353 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2354 " key_info 0x%x (ver=%d keyidx=%d rsvd=%d %s%s%s%s%s%s%s%s)",
2355 key_info, key_info & WPA_KEY_INFO_TYPE_MASK,
2356 (key_info & WPA_KEY_INFO_KEY_INDEX_MASK) >>
2357 WPA_KEY_INFO_KEY_INDEX_SHIFT,
2358 (key_info & (BIT(13) | BIT(14) | BIT(15))) >> 13,
2359 key_info & WPA_KEY_INFO_KEY_TYPE ? "Pairwise" : "Group",
2360 key_info & WPA_KEY_INFO_INSTALL ? " Install" : "",
2361 key_info & WPA_KEY_INFO_ACK ? " Ack" : "",
2362 key_info & WPA_KEY_INFO_MIC ? " MIC" : "",
2363 key_info & WPA_KEY_INFO_SECURE ? " Secure" : "",
2364 key_info & WPA_KEY_INFO_ERROR ? " Error" : "",
2365 key_info & WPA_KEY_INFO_REQUEST ? " Request" : "",
2366 key_info & WPA_KEY_INFO_ENCR_KEY_DATA ? " Encr" : "");
2367 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2368 " key_length=%u key_data_length=%u",
Dmitry Shmidt807291d2015-01-27 13:40:23 -08002369 WPA_GET_BE16(key->key_length), key_data_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002370 wpa_hexdump(MSG_DEBUG, " replay_counter",
2371 key->replay_counter, WPA_REPLAY_COUNTER_LEN);
2372 wpa_hexdump(MSG_DEBUG, " key_nonce", key->key_nonce, WPA_NONCE_LEN);
2373 wpa_hexdump(MSG_DEBUG, " key_iv", key->key_iv, 16);
2374 wpa_hexdump(MSG_DEBUG, " key_rsc", key->key_rsc, 8);
2375 wpa_hexdump(MSG_DEBUG, " key_id (reserved)", key->key_id, 8);
Dmitry Shmidt807291d2015-01-27 13:40:23 -08002376 wpa_hexdump(MSG_DEBUG, " key_mic", mic, mic_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002377#endif /* CONFIG_NO_STDOUT_DEBUG */
2378}
2379
2380
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002381#ifdef CONFIG_FILS
2382static int wpa_supp_aead_decrypt(struct wpa_sm *sm, u8 *buf, size_t buf_len,
2383 size_t *key_data_len)
2384{
2385 struct wpa_ptk *ptk;
2386 struct ieee802_1x_hdr *hdr;
2387 struct wpa_eapol_key *key;
2388 u8 *pos, *tmp;
2389 const u8 *aad[1];
2390 size_t aad_len[1];
2391
2392 if (*key_data_len < AES_BLOCK_SIZE) {
2393 wpa_printf(MSG_INFO, "No room for AES-SIV data in the frame");
2394 return -1;
2395 }
2396
2397 if (sm->tptk_set)
2398 ptk = &sm->tptk;
2399 else if (sm->ptk_set)
2400 ptk = &sm->ptk;
2401 else
2402 return -1;
2403
2404 hdr = (struct ieee802_1x_hdr *) buf;
2405 key = (struct wpa_eapol_key *) (hdr + 1);
2406 pos = (u8 *) (key + 1);
2407 pos += 2; /* Pointing at the Encrypted Key Data field */
2408
2409 tmp = os_malloc(*key_data_len);
2410 if (!tmp)
2411 return -1;
2412
2413 /* AES-SIV AAD from EAPOL protocol version field (inclusive) to
2414 * to Key Data (exclusive). */
2415 aad[0] = buf;
2416 aad_len[0] = pos - buf;
2417 if (aes_siv_decrypt(ptk->kek, ptk->kek_len, pos, *key_data_len,
2418 1, aad, aad_len, tmp) < 0) {
2419 wpa_printf(MSG_INFO, "Invalid AES-SIV data in the frame");
2420 bin_clear_free(tmp, *key_data_len);
2421 return -1;
2422 }
2423
2424 /* AEAD decryption and validation completed successfully */
2425 (*key_data_len) -= AES_BLOCK_SIZE;
2426 wpa_hexdump_key(MSG_DEBUG, "WPA: Decrypted Key Data",
2427 tmp, *key_data_len);
2428
2429 /* Replace Key Data field with the decrypted version */
2430 os_memcpy(pos, tmp, *key_data_len);
2431 pos -= 2; /* Key Data Length field */
2432 WPA_PUT_BE16(pos, *key_data_len);
2433 bin_clear_free(tmp, *key_data_len);
2434
2435 if (sm->tptk_set) {
2436 sm->tptk_set = 0;
2437 sm->ptk_set = 1;
2438 os_memcpy(&sm->ptk, &sm->tptk, sizeof(sm->ptk));
2439 os_memset(&sm->tptk, 0, sizeof(sm->tptk));
2440 }
2441
2442 os_memcpy(sm->rx_replay_counter, key->replay_counter,
2443 WPA_REPLAY_COUNTER_LEN);
2444 sm->rx_replay_counter_set = 1;
2445
2446 return 0;
2447}
2448#endif /* CONFIG_FILS */
2449
2450
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002451/**
2452 * wpa_sm_rx_eapol - Process received WPA EAPOL frames
2453 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2454 * @src_addr: Source MAC address of the EAPOL packet
2455 * @buf: Pointer to the beginning of the EAPOL data (EAPOL header)
2456 * @len: Length of the EAPOL frame
2457 * Returns: 1 = WPA EAPOL-Key processed, 0 = not a WPA EAPOL-Key, -1 failure
2458 *
2459 * This function is called for each received EAPOL frame. Other than EAPOL-Key
2460 * frames can be skipped if filtering is done elsewhere. wpa_sm_rx_eapol() is
2461 * only processing WPA and WPA2 EAPOL-Key frames.
2462 *
2463 * The received EAPOL-Key packets are validated and valid packets are replied
2464 * to. In addition, key material (PTK, GTK) is configured at the end of a
2465 * successful key handshake.
2466 */
2467int wpa_sm_rx_eapol(struct wpa_sm *sm, const u8 *src_addr,
2468 const u8 *buf, size_t len)
2469{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002470 size_t plen, data_len, key_data_len;
2471 const struct ieee802_1x_hdr *hdr;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002472 struct wpa_eapol_key *key;
2473 u16 key_info, ver;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002474 u8 *tmp = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002475 int ret = -1;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002476 u8 *mic, *key_data;
Hai Shalom899fcc72020-10-19 14:38:18 -07002477 size_t mic_len, keyhdrlen, pmk_len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002478
2479#ifdef CONFIG_IEEE80211R
2480 sm->ft_completed = 0;
2481#endif /* CONFIG_IEEE80211R */
2482
Hai Shalom899fcc72020-10-19 14:38:18 -07002483 pmk_len = sm->pmk_len;
2484 if (!pmk_len && sm->cur_pmksa)
2485 pmk_len = sm->cur_pmksa->pmk_len;
2486 mic_len = wpa_mic_len(sm->key_mgmt, pmk_len);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002487 keyhdrlen = sizeof(*key) + mic_len + 2;
Dmitry Shmidt807291d2015-01-27 13:40:23 -08002488
2489 if (len < sizeof(*hdr) + keyhdrlen) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002490 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2491 "WPA: EAPOL frame too short to be a WPA "
2492 "EAPOL-Key (len %lu, expecting at least %lu)",
2493 (unsigned long) len,
Dmitry Shmidt807291d2015-01-27 13:40:23 -08002494 (unsigned long) sizeof(*hdr) + keyhdrlen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002495 return 0;
2496 }
2497
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002498 hdr = (const struct ieee802_1x_hdr *) buf;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002499 plen = be_to_host16(hdr->length);
2500 data_len = plen + sizeof(*hdr);
2501 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2502 "IEEE 802.1X RX: version=%d type=%d length=%lu",
2503 hdr->version, hdr->type, (unsigned long) plen);
2504
2505 if (hdr->version < EAPOL_VERSION) {
2506 /* TODO: backwards compatibility */
2507 }
2508 if (hdr->type != IEEE802_1X_TYPE_EAPOL_KEY) {
2509 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2510 "WPA: EAPOL frame (type %u) discarded, "
2511 "not a Key frame", hdr->type);
2512 ret = 0;
2513 goto out;
2514 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002515 wpa_hexdump(MSG_MSGDUMP, "WPA: RX EAPOL-Key", buf, len);
Dmitry Shmidt807291d2015-01-27 13:40:23 -08002516 if (plen > len - sizeof(*hdr) || plen < keyhdrlen) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002517 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2518 "WPA: EAPOL frame payload size %lu "
2519 "invalid (frame size %lu)",
2520 (unsigned long) plen, (unsigned long) len);
2521 ret = 0;
2522 goto out;
2523 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002524 if (data_len < len) {
2525 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2526 "WPA: ignoring %lu bytes after the IEEE 802.1X data",
2527 (unsigned long) len - data_len);
2528 }
2529
2530 /*
2531 * Make a copy of the frame since we need to modify the buffer during
2532 * MAC validation and Key Data decryption.
2533 */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002534 tmp = os_memdup(buf, data_len);
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002535 if (tmp == NULL)
2536 goto out;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002537 key = (struct wpa_eapol_key *) (tmp + sizeof(struct ieee802_1x_hdr));
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002538 mic = (u8 *) (key + 1);
2539 key_data = mic + mic_len + 2;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002540
2541 if (key->type != EAPOL_KEY_TYPE_WPA && key->type != EAPOL_KEY_TYPE_RSN)
2542 {
2543 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2544 "WPA: EAPOL-Key type (%d) unknown, discarded",
2545 key->type);
2546 ret = 0;
2547 goto out;
2548 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002549
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002550 key_data_len = WPA_GET_BE16(mic + mic_len);
2551 wpa_eapol_key_dump(sm, key, key_data_len, mic, mic_len);
Dmitry Shmidt807291d2015-01-27 13:40:23 -08002552
2553 if (key_data_len > plen - keyhdrlen) {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002554 wpa_msg(sm->ctx->msg_ctx, MSG_INFO, "WPA: Invalid EAPOL-Key "
2555 "frame - key_data overflow (%u > %u)",
2556 (unsigned int) key_data_len,
Dmitry Shmidt807291d2015-01-27 13:40:23 -08002557 (unsigned int) (plen - keyhdrlen));
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002558 goto out;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002559 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002560
2561 eapol_sm_notify_lower_layer_success(sm->eapol, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002562 key_info = WPA_GET_BE16(key->key_info);
2563 ver = key_info & WPA_KEY_INFO_TYPE_MASK;
2564 if (ver != WPA_KEY_INFO_TYPE_HMAC_MD5_RC4 &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002565 ver != WPA_KEY_INFO_TYPE_AES_128_CMAC &&
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002566 ver != WPA_KEY_INFO_TYPE_HMAC_SHA1_AES &&
Roshan Pius3a1667e2018-07-03 15:17:14 -07002567 !wpa_use_akm_defined(sm->key_mgmt)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002568 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2569 "WPA: Unsupported EAPOL-Key descriptor version %d",
2570 ver);
2571 goto out;
2572 }
2573
Roshan Pius3a1667e2018-07-03 15:17:14 -07002574 if (wpa_use_akm_defined(sm->key_mgmt) &&
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002575 ver != WPA_KEY_INFO_TYPE_AKM_DEFINED) {
2576 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2577 "RSN: Unsupported EAPOL-Key descriptor version %d (expected AKM defined = 0)",
2578 ver);
2579 goto out;
2580 }
2581
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002582#ifdef CONFIG_IEEE80211R
2583 if (wpa_key_mgmt_ft(sm->key_mgmt)) {
2584 /* IEEE 802.11r uses a new key_info type (AES-128-CMAC). */
Roshan Pius3a1667e2018-07-03 15:17:14 -07002585 if (ver != WPA_KEY_INFO_TYPE_AES_128_CMAC &&
2586 !wpa_use_akm_defined(sm->key_mgmt)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002587 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2588 "FT: AP did not use AES-128-CMAC");
2589 goto out;
2590 }
2591 } else
2592#endif /* CONFIG_IEEE80211R */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002593 if (wpa_key_mgmt_sha256(sm->key_mgmt)) {
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002594 if (ver != WPA_KEY_INFO_TYPE_AES_128_CMAC &&
Roshan Pius3a1667e2018-07-03 15:17:14 -07002595 !wpa_use_akm_defined(sm->key_mgmt)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002596 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2597 "WPA: AP did not use the "
2598 "negotiated AES-128-CMAC");
2599 goto out;
2600 }
Hai Shalomc3565922019-10-28 11:58:20 -07002601 } else if (sm->pairwise_cipher == WPA_CIPHER_CCMP &&
2602 !wpa_use_akm_defined(sm->key_mgmt) &&
2603 ver != WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002604 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2605 "WPA: CCMP is used, but EAPOL-Key "
2606 "descriptor version (%d) is not 2", ver);
2607 if (sm->group_cipher != WPA_CIPHER_CCMP &&
2608 !(key_info & WPA_KEY_INFO_KEY_TYPE)) {
2609 /* Earlier versions of IEEE 802.11i did not explicitly
2610 * require version 2 descriptor for all EAPOL-Key
2611 * packets, so allow group keys to use version 1 if
2612 * CCMP is not used for them. */
2613 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2614 "WPA: Backwards compatibility: allow invalid "
2615 "version for non-CCMP group keys");
Jouni Malinen658fb4a2014-11-14 20:57:05 +02002616 } else if (ver == WPA_KEY_INFO_TYPE_AES_128_CMAC) {
2617 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2618 "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 -07002619 } else
2620 goto out;
Dmitry Shmidt71757432014-06-02 13:50:35 -07002621 } else if (sm->pairwise_cipher == WPA_CIPHER_GCMP &&
Roshan Pius3a1667e2018-07-03 15:17:14 -07002622 !wpa_use_akm_defined(sm->key_mgmt) &&
Dmitry Shmidt71757432014-06-02 13:50:35 -07002623 ver != WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002624 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2625 "WPA: GCMP is used, but EAPOL-Key "
2626 "descriptor version (%d) is not 2", ver);
2627 goto out;
2628 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002629
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002630 if (sm->rx_replay_counter_set &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002631 os_memcmp(key->replay_counter, sm->rx_replay_counter,
2632 WPA_REPLAY_COUNTER_LEN) <= 0) {
2633 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
2634 "WPA: EAPOL-Key Replay Counter did not increase - "
2635 "dropping packet");
2636 goto out;
2637 }
2638
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002639 if (key_info & WPA_KEY_INFO_SMK_MESSAGE) {
2640 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2641 "WPA: Unsupported SMK bit in key_info");
2642 goto out;
2643 }
2644
2645 if (!(key_info & WPA_KEY_INFO_ACK)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002646 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2647 "WPA: No Ack bit in key_info");
2648 goto out;
2649 }
2650
2651 if (key_info & WPA_KEY_INFO_REQUEST) {
2652 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2653 "WPA: EAPOL-Key with Request bit - dropped");
2654 goto out;
2655 }
2656
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002657 if ((key_info & WPA_KEY_INFO_MIC) &&
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002658 wpa_supplicant_verify_eapol_key_mic(sm, key, ver, tmp, data_len))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002659 goto out;
2660
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002661#ifdef CONFIG_FILS
2662 if (!mic_len && (key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
2663 if (wpa_supp_aead_decrypt(sm, tmp, data_len, &key_data_len))
2664 goto out;
2665 }
2666#endif /* CONFIG_FILS */
2667
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002668 if ((sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN) &&
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002669 (key_info & WPA_KEY_INFO_ENCR_KEY_DATA) && mic_len) {
Hai Shalomce48b4a2018-09-05 11:41:35 -07002670 /*
2671 * Only decrypt the Key Data field if the frame's authenticity
2672 * was verified. When using AES-SIV (FILS), the MIC flag is not
2673 * set, so this check should only be performed if mic_len != 0
2674 * which is the case in this code branch.
2675 */
2676 if (!(key_info & WPA_KEY_INFO_MIC)) {
2677 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
2678 "WPA: Ignore EAPOL-Key with encrypted but unauthenticated data");
2679 goto out;
2680 }
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002681 if (wpa_supplicant_decrypt_key_data(sm, key, mic_len,
2682 ver, key_data,
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002683 &key_data_len))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002684 goto out;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002685 }
2686
2687 if (key_info & WPA_KEY_INFO_KEY_TYPE) {
2688 if (key_info & WPA_KEY_INFO_KEY_INDEX_MASK) {
2689 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
2690 "WPA: Ignored EAPOL-Key (Pairwise) with "
2691 "non-zero key index");
2692 goto out;
2693 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002694 if (key_info & (WPA_KEY_INFO_MIC |
2695 WPA_KEY_INFO_ENCR_KEY_DATA)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002696 /* 3/4 4-Way Handshake */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002697 wpa_supplicant_process_3_of_4(sm, key, ver, key_data,
2698 key_data_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002699 } else {
2700 /* 1/4 4-Way Handshake */
2701 wpa_supplicant_process_1_of_4(sm, src_addr, key,
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002702 ver, key_data,
2703 key_data_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002704 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002705 } else {
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002706 if ((mic_len && (key_info & WPA_KEY_INFO_MIC)) ||
2707 (!mic_len && (key_info & WPA_KEY_INFO_ENCR_KEY_DATA))) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002708 /* 1/2 Group Key Handshake */
2709 wpa_supplicant_process_1_of_2(sm, src_addr, key,
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002710 key_data, key_data_len,
2711 ver);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002712 } else {
2713 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002714 "WPA: EAPOL-Key (Group) without Mic/Encr bit - "
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002715 "dropped");
2716 }
2717 }
2718
2719 ret = 1;
2720
2721out:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002722 bin_clear_free(tmp, data_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002723 return ret;
2724}
2725
2726
2727#ifdef CONFIG_CTRL_IFACE
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002728static u32 wpa_key_mgmt_suite(struct wpa_sm *sm)
2729{
2730 switch (sm->key_mgmt) {
2731 case WPA_KEY_MGMT_IEEE8021X:
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002732 return ((sm->proto == WPA_PROTO_RSN ||
2733 sm->proto == WPA_PROTO_OSEN) ?
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002734 RSN_AUTH_KEY_MGMT_UNSPEC_802_1X :
2735 WPA_AUTH_KEY_MGMT_UNSPEC_802_1X);
2736 case WPA_KEY_MGMT_PSK:
2737 return (sm->proto == WPA_PROTO_RSN ?
2738 RSN_AUTH_KEY_MGMT_PSK_OVER_802_1X :
2739 WPA_AUTH_KEY_MGMT_PSK_OVER_802_1X);
2740#ifdef CONFIG_IEEE80211R
2741 case WPA_KEY_MGMT_FT_IEEE8021X:
2742 return RSN_AUTH_KEY_MGMT_FT_802_1X;
2743 case WPA_KEY_MGMT_FT_PSK:
2744 return RSN_AUTH_KEY_MGMT_FT_PSK;
2745#endif /* CONFIG_IEEE80211R */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002746 case WPA_KEY_MGMT_IEEE8021X_SHA256:
2747 return RSN_AUTH_KEY_MGMT_802_1X_SHA256;
2748 case WPA_KEY_MGMT_PSK_SHA256:
2749 return RSN_AUTH_KEY_MGMT_PSK_SHA256;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002750 case WPA_KEY_MGMT_CCKM:
2751 return (sm->proto == WPA_PROTO_RSN ?
2752 RSN_AUTH_KEY_MGMT_CCKM:
2753 WPA_AUTH_KEY_MGMT_CCKM);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002754 case WPA_KEY_MGMT_WPA_NONE:
2755 return WPA_AUTH_KEY_MGMT_NONE;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002756 case WPA_KEY_MGMT_IEEE8021X_SUITE_B:
2757 return RSN_AUTH_KEY_MGMT_802_1X_SUITE_B;
Dmitry Shmidt807291d2015-01-27 13:40:23 -08002758 case WPA_KEY_MGMT_IEEE8021X_SUITE_B_192:
2759 return RSN_AUTH_KEY_MGMT_802_1X_SUITE_B_192;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002760 default:
2761 return 0;
2762 }
2763}
2764
2765
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002766#define RSN_SUITE "%02x-%02x-%02x-%d"
2767#define RSN_SUITE_ARG(s) \
2768((s) >> 24) & 0xff, ((s) >> 16) & 0xff, ((s) >> 8) & 0xff, (s) & 0xff
2769
2770/**
2771 * wpa_sm_get_mib - Dump text list of MIB entries
2772 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2773 * @buf: Buffer for the list
2774 * @buflen: Length of the buffer
2775 * Returns: Number of bytes written to buffer
2776 *
2777 * This function is used fetch dot11 MIB variables.
2778 */
2779int wpa_sm_get_mib(struct wpa_sm *sm, char *buf, size_t buflen)
2780{
2781 char pmkid_txt[PMKID_LEN * 2 + 1];
Hai Shalome21d4e82020-04-29 16:34:06 -07002782 bool rsna;
2783 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002784 size_t len;
2785
2786 if (sm->cur_pmksa) {
2787 wpa_snprintf_hex(pmkid_txt, sizeof(pmkid_txt),
2788 sm->cur_pmksa->pmkid, PMKID_LEN);
2789 } else
2790 pmkid_txt[0] = '\0';
2791
Hai Shalome21d4e82020-04-29 16:34:06 -07002792 rsna = (wpa_key_mgmt_wpa_psk(sm->key_mgmt) ||
2793 wpa_key_mgmt_wpa_ieee8021x(sm->key_mgmt)) &&
2794 sm->proto == WPA_PROTO_RSN;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002795
2796 ret = os_snprintf(buf, buflen,
2797 "dot11RSNAOptionImplemented=TRUE\n"
2798 "dot11RSNAPreauthenticationImplemented=TRUE\n"
2799 "dot11RSNAEnabled=%s\n"
2800 "dot11RSNAPreauthenticationEnabled=%s\n"
2801 "dot11RSNAConfigVersion=%d\n"
2802 "dot11RSNAConfigPairwiseKeysSupported=5\n"
2803 "dot11RSNAConfigGroupCipherSize=%d\n"
2804 "dot11RSNAConfigPMKLifetime=%d\n"
2805 "dot11RSNAConfigPMKReauthThreshold=%d\n"
2806 "dot11RSNAConfigNumberOfPTKSAReplayCounters=1\n"
2807 "dot11RSNAConfigSATimeout=%d\n",
2808 rsna ? "TRUE" : "FALSE",
2809 rsna ? "TRUE" : "FALSE",
2810 RSN_VERSION,
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07002811 wpa_cipher_key_len(sm->group_cipher) * 8,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002812 sm->dot11RSNAConfigPMKLifetime,
2813 sm->dot11RSNAConfigPMKReauthThreshold,
2814 sm->dot11RSNAConfigSATimeout);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002815 if (os_snprintf_error(buflen, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002816 return 0;
2817 len = ret;
2818
2819 ret = os_snprintf(
2820 buf + len, buflen - len,
2821 "dot11RSNAAuthenticationSuiteSelected=" RSN_SUITE "\n"
2822 "dot11RSNAPairwiseCipherSelected=" RSN_SUITE "\n"
2823 "dot11RSNAGroupCipherSelected=" RSN_SUITE "\n"
2824 "dot11RSNAPMKIDUsed=%s\n"
2825 "dot11RSNAAuthenticationSuiteRequested=" RSN_SUITE "\n"
2826 "dot11RSNAPairwiseCipherRequested=" RSN_SUITE "\n"
2827 "dot11RSNAGroupCipherRequested=" RSN_SUITE "\n"
2828 "dot11RSNAConfigNumberOfGTKSAReplayCounters=0\n"
2829 "dot11RSNA4WayHandshakeFailures=%u\n",
2830 RSN_SUITE_ARG(wpa_key_mgmt_suite(sm)),
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07002831 RSN_SUITE_ARG(wpa_cipher_to_suite(sm->proto,
2832 sm->pairwise_cipher)),
2833 RSN_SUITE_ARG(wpa_cipher_to_suite(sm->proto,
2834 sm->group_cipher)),
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002835 pmkid_txt,
2836 RSN_SUITE_ARG(wpa_key_mgmt_suite(sm)),
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07002837 RSN_SUITE_ARG(wpa_cipher_to_suite(sm->proto,
2838 sm->pairwise_cipher)),
2839 RSN_SUITE_ARG(wpa_cipher_to_suite(sm->proto,
2840 sm->group_cipher)),
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002841 sm->dot11RSNA4WayHandshakeFailures);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002842 if (!os_snprintf_error(buflen - len, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002843 len += ret;
2844
2845 return (int) len;
2846}
2847#endif /* CONFIG_CTRL_IFACE */
2848
2849
2850static void wpa_sm_pmksa_free_cb(struct rsn_pmksa_cache_entry *entry,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002851 void *ctx, enum pmksa_free_reason reason)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002852{
2853 struct wpa_sm *sm = ctx;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002854 int deauth = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002855
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002856 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "RSN: PMKSA cache entry free_cb: "
2857 MACSTR " reason=%d", MAC2STR(entry->aa), reason);
2858
2859 if (sm->cur_pmksa == entry) {
2860 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2861 "RSN: %s current PMKSA entry",
2862 reason == PMKSA_REPLACE ? "replaced" : "removed");
2863 pmksa_cache_clear_current(sm);
2864
2865 /*
2866 * If an entry is simply being replaced, there's no need to
2867 * deauthenticate because it will be immediately re-added.
2868 * This happens when EAP authentication is completed again
2869 * (reauth or failed PMKSA caching attempt).
2870 */
2871 if (reason != PMKSA_REPLACE)
2872 deauth = 1;
2873 }
2874
2875 if (reason == PMKSA_EXPIRE &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002876 (sm->pmk_len == entry->pmk_len &&
2877 os_memcmp(sm->pmk, entry->pmk, sm->pmk_len) == 0)) {
2878 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002879 "RSN: deauthenticating due to expired PMK");
2880 pmksa_cache_clear_current(sm);
2881 deauth = 1;
2882 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002883
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002884 if (deauth) {
Roshan Pius3a1667e2018-07-03 15:17:14 -07002885 sm->pmk_len = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002886 os_memset(sm->pmk, 0, sizeof(sm->pmk));
2887 wpa_sm_deauthenticate(sm, WLAN_REASON_UNSPECIFIED);
2888 }
2889}
2890
2891
2892/**
2893 * wpa_sm_init - Initialize WPA state machine
2894 * @ctx: Context pointer for callbacks; this needs to be an allocated buffer
2895 * Returns: Pointer to the allocated WPA state machine data
2896 *
2897 * This function is used to allocate a new WPA state machine and the returned
2898 * value is passed to all WPA state machine calls.
2899 */
2900struct wpa_sm * wpa_sm_init(struct wpa_sm_ctx *ctx)
2901{
2902 struct wpa_sm *sm;
2903
2904 sm = os_zalloc(sizeof(*sm));
2905 if (sm == NULL)
2906 return NULL;
2907 dl_list_init(&sm->pmksa_candidates);
2908 sm->renew_snonce = 1;
2909 sm->ctx = ctx;
2910
2911 sm->dot11RSNAConfigPMKLifetime = 43200;
2912 sm->dot11RSNAConfigPMKReauthThreshold = 70;
2913 sm->dot11RSNAConfigSATimeout = 60;
2914
2915 sm->pmksa = pmksa_cache_init(wpa_sm_pmksa_free_cb, sm, sm);
2916 if (sm->pmksa == NULL) {
2917 wpa_msg(sm->ctx->msg_ctx, MSG_ERROR,
2918 "RSN: PMKSA cache initialization failed");
2919 os_free(sm);
2920 return NULL;
2921 }
2922
2923 return sm;
2924}
2925
2926
2927/**
2928 * wpa_sm_deinit - Deinitialize WPA state machine
2929 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2930 */
2931void wpa_sm_deinit(struct wpa_sm *sm)
2932{
2933 if (sm == NULL)
2934 return;
2935 pmksa_cache_deinit(sm->pmksa);
2936 eloop_cancel_timeout(wpa_sm_start_preauth, sm, NULL);
2937 eloop_cancel_timeout(wpa_sm_rekey_ptk, sm, NULL);
2938 os_free(sm->assoc_wpa_ie);
Hai Shalomc3565922019-10-28 11:58:20 -07002939 os_free(sm->assoc_rsnxe);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002940 os_free(sm->ap_wpa_ie);
2941 os_free(sm->ap_rsn_ie);
Hai Shalomc3565922019-10-28 11:58:20 -07002942 os_free(sm->ap_rsnxe);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002943 wpa_sm_drop_sa(sm);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002944 os_free(sm->ctx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002945#ifdef CONFIG_IEEE80211R
2946 os_free(sm->assoc_resp_ies);
2947#endif /* CONFIG_IEEE80211R */
Dmitry Shmidt55840ad2015-12-14 12:45:46 -08002948#ifdef CONFIG_TESTING_OPTIONS
2949 wpabuf_free(sm->test_assoc_ie);
2950#endif /* CONFIG_TESTING_OPTIONS */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002951#ifdef CONFIG_FILS_SK_PFS
2952 crypto_ecdh_deinit(sm->fils_ecdh);
2953#endif /* CONFIG_FILS_SK_PFS */
2954#ifdef CONFIG_FILS
2955 wpabuf_free(sm->fils_ft_ies);
2956#endif /* CONFIG_FILS */
2957#ifdef CONFIG_OWE
2958 crypto_ecdh_deinit(sm->owe_ecdh);
2959#endif /* CONFIG_OWE */
Hai Shalom021b0b52019-04-10 11:17:58 -07002960#ifdef CONFIG_DPP2
2961 wpabuf_clear_free(sm->dpp_z);
2962#endif /* CONFIG_DPP2 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002963 os_free(sm);
2964}
2965
2966
2967/**
2968 * wpa_sm_notify_assoc - Notify WPA state machine about association
2969 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2970 * @bssid: The BSSID of the new association
2971 *
2972 * This function is called to let WPA state machine know that the connection
2973 * was established.
2974 */
2975void wpa_sm_notify_assoc(struct wpa_sm *sm, const u8 *bssid)
2976{
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02002977 int clear_keys = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002978
2979 if (sm == NULL)
2980 return;
2981
2982 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2983 "WPA: Association event - clear replay counter");
2984 os_memcpy(sm->bssid, bssid, ETH_ALEN);
2985 os_memset(sm->rx_replay_counter, 0, WPA_REPLAY_COUNTER_LEN);
2986 sm->rx_replay_counter_set = 0;
2987 sm->renew_snonce = 1;
2988 if (os_memcmp(sm->preauth_bssid, bssid, ETH_ALEN) == 0)
2989 rsn_preauth_deinit(sm);
2990
2991#ifdef CONFIG_IEEE80211R
2992 if (wpa_ft_is_completed(sm)) {
2993 /*
2994 * Clear portValid to kick EAPOL state machine to re-enter
2995 * AUTHENTICATED state to get the EAPOL port Authorized.
2996 */
Hai Shalome21d4e82020-04-29 16:34:06 -07002997 eapol_sm_notify_portValid(sm->eapol, false);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002998 wpa_supplicant_key_neg_complete(sm, sm->bssid, 1);
2999
3000 /* Prepare for the next transition */
3001 wpa_ft_prepare_auth_request(sm, NULL);
3002
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02003003 clear_keys = 0;
Hai Shalom81f62d82019-07-22 12:10:00 -07003004 sm->ft_protocol = 1;
3005 } else {
3006 sm->ft_protocol = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003007 }
3008#endif /* CONFIG_IEEE80211R */
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003009#ifdef CONFIG_FILS
3010 if (sm->fils_completed) {
3011 /*
3012 * Clear portValid to kick EAPOL state machine to re-enter
3013 * AUTHENTICATED state to get the EAPOL port Authorized.
3014 */
3015 wpa_supplicant_key_neg_complete(sm, sm->bssid, 1);
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02003016 clear_keys = 0;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003017 }
3018#endif /* CONFIG_FILS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003019
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02003020 if (clear_keys) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003021 /*
3022 * IEEE 802.11, 8.4.10: Delete PTK SA on (re)association if
3023 * this is not part of a Fast BSS Transition.
3024 */
3025 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Clear old PTK");
3026 sm->ptk_set = 0;
Dmitry Shmidt61593f02014-04-21 16:27:35 -07003027 os_memset(&sm->ptk, 0, sizeof(sm->ptk));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003028 sm->tptk_set = 0;
Dmitry Shmidt61593f02014-04-21 16:27:35 -07003029 os_memset(&sm->tptk, 0, sizeof(sm->tptk));
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02003030 os_memset(&sm->gtk, 0, sizeof(sm->gtk));
Jouni Malinen58c0e962017-10-01 12:12:24 +03003031 os_memset(&sm->gtk_wnm_sleep, 0, sizeof(sm->gtk_wnm_sleep));
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02003032 os_memset(&sm->igtk, 0, sizeof(sm->igtk));
Jouni Malinen58c0e962017-10-01 12:12:24 +03003033 os_memset(&sm->igtk_wnm_sleep, 0, sizeof(sm->igtk_wnm_sleep));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003034 }
3035
3036#ifdef CONFIG_TDLS
3037 wpa_tdls_assoc(sm);
3038#endif /* CONFIG_TDLS */
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003039
3040#ifdef CONFIG_P2P
3041 os_memset(sm->p2p_ip_addr, 0, sizeof(sm->p2p_ip_addr));
3042#endif /* CONFIG_P2P */
Hai Shalomfdcde762020-04-02 11:19:20 -07003043
3044 sm->keyidx_active = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003045}
3046
3047
3048/**
3049 * wpa_sm_notify_disassoc - Notify WPA state machine about disassociation
3050 * @sm: Pointer to WPA state machine data from wpa_sm_init()
3051 *
3052 * This function is called to let WPA state machine know that the connection
3053 * was lost. This will abort any existing pre-authentication session.
3054 */
3055void wpa_sm_notify_disassoc(struct wpa_sm *sm)
3056{
Dmitry Shmidt4dd28dc2015-03-10 11:21:43 -07003057 eloop_cancel_timeout(wpa_sm_start_preauth, sm, NULL);
3058 eloop_cancel_timeout(wpa_sm_rekey_ptk, sm, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003059 rsn_preauth_deinit(sm);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003060 pmksa_cache_clear_current(sm);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003061 if (wpa_sm_get_state(sm) == WPA_4WAY_HANDSHAKE)
3062 sm->dot11RSNA4WayHandshakeFailures++;
3063#ifdef CONFIG_TDLS
3064 wpa_tdls_disassoc(sm);
3065#endif /* CONFIG_TDLS */
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003066#ifdef CONFIG_FILS
3067 sm->fils_completed = 0;
3068#endif /* CONFIG_FILS */
Jouni Malinen4283f9e2017-09-22 12:06:37 +03003069#ifdef CONFIG_IEEE80211R
3070 sm->ft_reassoc_completed = 0;
Hai Shalom81f62d82019-07-22 12:10:00 -07003071 sm->ft_protocol = 0;
Jouni Malinen4283f9e2017-09-22 12:06:37 +03003072#endif /* CONFIG_IEEE80211R */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003073
3074 /* Keys are not needed in the WPA state machine anymore */
3075 wpa_sm_drop_sa(sm);
Hai Shalomfdcde762020-04-02 11:19:20 -07003076 sm->keyidx_active = 0;
Dmitry Shmidt4dd28dc2015-03-10 11:21:43 -07003077
3078 sm->msg_3_of_4_ok = 0;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003079 os_memset(sm->bssid, 0, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003080}
3081
3082
3083/**
3084 * wpa_sm_set_pmk - Set PMK
3085 * @sm: Pointer to WPA state machine data from wpa_sm_init()
3086 * @pmk: The new PMK
3087 * @pmk_len: The length of the new PMK in bytes
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08003088 * @pmkid: Calculated PMKID
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003089 * @bssid: AA to add into PMKSA cache or %NULL to not cache the PMK
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003090 *
3091 * Configure the PMK for WPA state machine.
3092 */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003093void wpa_sm_set_pmk(struct wpa_sm *sm, const u8 *pmk, size_t pmk_len,
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08003094 const u8 *pmkid, const u8 *bssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003095{
3096 if (sm == NULL)
3097 return;
3098
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003099 wpa_hexdump_key(MSG_DEBUG, "WPA: Set PMK based on external data",
3100 pmk, pmk_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003101 sm->pmk_len = pmk_len;
3102 os_memcpy(sm->pmk, pmk, pmk_len);
3103
3104#ifdef CONFIG_IEEE80211R
3105 /* Set XXKey to be PSK for FT key derivation */
3106 sm->xxkey_len = pmk_len;
3107 os_memcpy(sm->xxkey, pmk, pmk_len);
3108#endif /* CONFIG_IEEE80211R */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003109
3110 if (bssid) {
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08003111 pmksa_cache_add(sm->pmksa, pmk, pmk_len, pmkid, NULL, 0,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003112 bssid, sm->own_addr,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003113 sm->network_ctx, sm->key_mgmt, NULL);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003114 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003115}
3116
3117
3118/**
3119 * wpa_sm_set_pmk_from_pmksa - Set PMK based on the current PMKSA
3120 * @sm: Pointer to WPA state machine data from wpa_sm_init()
3121 *
3122 * Take the PMK from the current PMKSA into use. If no PMKSA is active, the PMK
3123 * will be cleared.
3124 */
3125void wpa_sm_set_pmk_from_pmksa(struct wpa_sm *sm)
3126{
3127 if (sm == NULL)
3128 return;
3129
3130 if (sm->cur_pmksa) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003131 wpa_hexdump_key(MSG_DEBUG,
3132 "WPA: Set PMK based on current PMKSA",
3133 sm->cur_pmksa->pmk, sm->cur_pmksa->pmk_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003134 sm->pmk_len = sm->cur_pmksa->pmk_len;
3135 os_memcpy(sm->pmk, sm->cur_pmksa->pmk, sm->pmk_len);
3136 } else {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003137 wpa_printf(MSG_DEBUG, "WPA: No current PMKSA - clear PMK");
3138 sm->pmk_len = 0;
3139 os_memset(sm->pmk, 0, PMK_LEN_MAX);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003140 }
3141}
3142
3143
3144/**
3145 * wpa_sm_set_fast_reauth - Set fast reauthentication (EAP) enabled/disabled
3146 * @sm: Pointer to WPA state machine data from wpa_sm_init()
3147 * @fast_reauth: Whether fast reauthentication (EAP) is allowed
3148 */
3149void wpa_sm_set_fast_reauth(struct wpa_sm *sm, int fast_reauth)
3150{
3151 if (sm)
3152 sm->fast_reauth = fast_reauth;
3153}
3154
3155
3156/**
3157 * wpa_sm_set_scard_ctx - Set context pointer for smartcard callbacks
3158 * @sm: Pointer to WPA state machine data from wpa_sm_init()
3159 * @scard_ctx: Context pointer for smartcard related callback functions
3160 */
3161void wpa_sm_set_scard_ctx(struct wpa_sm *sm, void *scard_ctx)
3162{
3163 if (sm == NULL)
3164 return;
3165 sm->scard_ctx = scard_ctx;
3166 if (sm->preauth_eapol)
3167 eapol_sm_register_scard_ctx(sm->preauth_eapol, scard_ctx);
3168}
3169
3170
3171/**
Hai Shalomfdcde762020-04-02 11:19:20 -07003172 * wpa_sm_set_config - Notification of current configuration change
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003173 * @sm: Pointer to WPA state machine data from wpa_sm_init()
3174 * @config: Pointer to current network configuration
3175 *
3176 * Notify WPA state machine that configuration has changed. config will be
3177 * stored as a backpointer to network configuration. This can be %NULL to clear
3178 * the stored pointed.
3179 */
3180void wpa_sm_set_config(struct wpa_sm *sm, struct rsn_supp_config *config)
3181{
3182 if (!sm)
3183 return;
3184
3185 if (config) {
3186 sm->network_ctx = config->network_ctx;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003187 sm->allowed_pairwise_cipher = config->allowed_pairwise_cipher;
3188 sm->proactive_key_caching = config->proactive_key_caching;
3189 sm->eap_workaround = config->eap_workaround;
3190 sm->eap_conf_ctx = config->eap_conf_ctx;
3191 if (config->ssid) {
3192 os_memcpy(sm->ssid, config->ssid, config->ssid_len);
3193 sm->ssid_len = config->ssid_len;
3194 } else
3195 sm->ssid_len = 0;
3196 sm->wpa_ptk_rekey = config->wpa_ptk_rekey;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003197 sm->p2p = config->p2p;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003198 sm->wpa_rsc_relaxation = config->wpa_rsc_relaxation;
Hai Shalomfdcde762020-04-02 11:19:20 -07003199 sm->owe_ptk_workaround = config->owe_ptk_workaround;
Hai Shalom60840252021-02-19 19:02:11 -08003200 sm->force_kdk_derivation = config->force_kdk_derivation;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003201#ifdef CONFIG_FILS
3202 if (config->fils_cache_id) {
3203 sm->fils_cache_id_set = 1;
3204 os_memcpy(sm->fils_cache_id, config->fils_cache_id,
3205 FILS_CACHE_ID_LEN);
3206 } else {
3207 sm->fils_cache_id_set = 0;
3208 }
3209#endif /* CONFIG_FILS */
Hai Shalomfdcde762020-04-02 11:19:20 -07003210 sm->beacon_prot = config->beacon_prot;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003211 } else {
3212 sm->network_ctx = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003213 sm->allowed_pairwise_cipher = 0;
3214 sm->proactive_key_caching = 0;
3215 sm->eap_workaround = 0;
3216 sm->eap_conf_ctx = NULL;
3217 sm->ssid_len = 0;
3218 sm->wpa_ptk_rekey = 0;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003219 sm->p2p = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003220 sm->wpa_rsc_relaxation = 0;
Hai Shalomfdcde762020-04-02 11:19:20 -07003221 sm->owe_ptk_workaround = 0;
3222 sm->beacon_prot = 0;
Hai Shalom60840252021-02-19 19:02:11 -08003223 sm->force_kdk_derivation = false;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003224 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003225}
3226
3227
3228/**
3229 * wpa_sm_set_own_addr - Set own MAC address
3230 * @sm: Pointer to WPA state machine data from wpa_sm_init()
3231 * @addr: Own MAC address
3232 */
3233void wpa_sm_set_own_addr(struct wpa_sm *sm, const u8 *addr)
3234{
3235 if (sm)
3236 os_memcpy(sm->own_addr, addr, ETH_ALEN);
3237}
3238
3239
3240/**
3241 * wpa_sm_set_ifname - Set network interface name
3242 * @sm: Pointer to WPA state machine data from wpa_sm_init()
3243 * @ifname: Interface name
3244 * @bridge_ifname: Optional bridge interface name (for pre-auth)
3245 */
3246void wpa_sm_set_ifname(struct wpa_sm *sm, const char *ifname,
3247 const char *bridge_ifname)
3248{
3249 if (sm) {
3250 sm->ifname = ifname;
3251 sm->bridge_ifname = bridge_ifname;
3252 }
3253}
3254
3255
3256/**
3257 * wpa_sm_set_eapol - Set EAPOL state machine pointer
3258 * @sm: Pointer to WPA state machine data from wpa_sm_init()
3259 * @eapol: Pointer to EAPOL state machine allocated with eapol_sm_init()
3260 */
3261void wpa_sm_set_eapol(struct wpa_sm *sm, struct eapol_sm *eapol)
3262{
3263 if (sm)
3264 sm->eapol = eapol;
3265}
3266
3267
3268/**
3269 * wpa_sm_set_param - Set WPA state machine parameters
3270 * @sm: Pointer to WPA state machine data from wpa_sm_init()
3271 * @param: Parameter field
3272 * @value: Parameter value
3273 * Returns: 0 on success, -1 on failure
3274 */
3275int wpa_sm_set_param(struct wpa_sm *sm, enum wpa_sm_conf_params param,
3276 unsigned int value)
3277{
3278 int ret = 0;
3279
3280 if (sm == NULL)
3281 return -1;
3282
3283 switch (param) {
3284 case RSNA_PMK_LIFETIME:
3285 if (value > 0)
3286 sm->dot11RSNAConfigPMKLifetime = value;
3287 else
3288 ret = -1;
3289 break;
3290 case RSNA_PMK_REAUTH_THRESHOLD:
3291 if (value > 0 && value <= 100)
3292 sm->dot11RSNAConfigPMKReauthThreshold = value;
3293 else
3294 ret = -1;
3295 break;
3296 case RSNA_SA_TIMEOUT:
3297 if (value > 0)
3298 sm->dot11RSNAConfigSATimeout = value;
3299 else
3300 ret = -1;
3301 break;
3302 case WPA_PARAM_PROTO:
3303 sm->proto = value;
3304 break;
3305 case WPA_PARAM_PAIRWISE:
3306 sm->pairwise_cipher = value;
3307 break;
3308 case WPA_PARAM_GROUP:
3309 sm->group_cipher = value;
3310 break;
3311 case WPA_PARAM_KEY_MGMT:
3312 sm->key_mgmt = value;
3313 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003314 case WPA_PARAM_MGMT_GROUP:
3315 sm->mgmt_group_cipher = value;
3316 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003317 case WPA_PARAM_RSN_ENABLED:
3318 sm->rsn_enabled = value;
3319 break;
3320 case WPA_PARAM_MFP:
3321 sm->mfp = value;
3322 break;
Hai Shalom74f70d42019-02-11 14:42:39 -08003323 case WPA_PARAM_OCV:
3324 sm->ocv = value;
3325 break;
Hai Shalomc3565922019-10-28 11:58:20 -07003326 case WPA_PARAM_SAE_PWE:
3327 sm->sae_pwe = value;
3328 break;
Hai Shalom899fcc72020-10-19 14:38:18 -07003329 case WPA_PARAM_SAE_PK:
3330 sm->sae_pk = value;
3331 break;
Hai Shalomfdcde762020-04-02 11:19:20 -07003332 case WPA_PARAM_DENY_PTK0_REKEY:
3333 sm->wpa_deny_ptk0_rekey = value;
3334 break;
3335 case WPA_PARAM_EXT_KEY_ID:
3336 sm->ext_key_id = value;
3337 break;
3338 case WPA_PARAM_USE_EXT_KEY_ID:
3339 sm->use_ext_key_id = value;
3340 break;
Hai Shalomb755a2a2020-04-23 21:49:02 -07003341#ifdef CONFIG_TESTING_OPTIONS
3342 case WPA_PARAM_FT_RSNXE_USED:
3343 sm->ft_rsnxe_used = value;
3344 break;
Hai Shalom899fcc72020-10-19 14:38:18 -07003345 case WPA_PARAM_OCI_FREQ_EAPOL:
3346 sm->oci_freq_override_eapol = value;
3347 break;
3348 case WPA_PARAM_OCI_FREQ_EAPOL_G2:
3349 sm->oci_freq_override_eapol_g2 = value;
3350 break;
3351 case WPA_PARAM_OCI_FREQ_FT_ASSOC:
3352 sm->oci_freq_override_ft_assoc = value;
3353 break;
3354 case WPA_PARAM_OCI_FREQ_FILS_ASSOC:
3355 sm->oci_freq_override_fils_assoc = value;
3356 break;
Hai Shalomb755a2a2020-04-23 21:49:02 -07003357#endif /* CONFIG_TESTING_OPTIONS */
Hai Shalom4fbc08f2020-05-18 12:37:00 -07003358#ifdef CONFIG_DPP2
3359 case WPA_PARAM_DPP_PFS:
3360 sm->dpp_pfs = value;
3361 break;
3362#endif /* CONFIG_DPP2 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003363 default:
3364 break;
3365 }
3366
3367 return ret;
3368}
3369
3370
3371/**
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003372 * wpa_sm_get_status - Get WPA state machine
3373 * @sm: Pointer to WPA state machine data from wpa_sm_init()
3374 * @buf: Buffer for status information
3375 * @buflen: Maximum buffer length
3376 * @verbose: Whether to include verbose status information
3377 * Returns: Number of bytes written to buf.
3378 *
3379 * Query WPA state machine for status information. This function fills in
3380 * a text area with current status information. If the buffer (buf) is not
3381 * large enough, status information will be truncated to fit the buffer.
3382 */
3383int wpa_sm_get_status(struct wpa_sm *sm, char *buf, size_t buflen,
3384 int verbose)
3385{
3386 char *pos = buf, *end = buf + buflen;
3387 int ret;
3388
3389 ret = os_snprintf(pos, end - pos,
3390 "pairwise_cipher=%s\n"
3391 "group_cipher=%s\n"
3392 "key_mgmt=%s\n",
3393 wpa_cipher_txt(sm->pairwise_cipher),
3394 wpa_cipher_txt(sm->group_cipher),
3395 wpa_key_mgmt_txt(sm->key_mgmt, sm->proto));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003396 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003397 return pos - buf;
3398 pos += ret;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003399
Hai Shalom4fbc08f2020-05-18 12:37:00 -07003400#ifdef CONFIG_DPP2
3401 if (sm->key_mgmt == WPA_KEY_MGMT_DPP && sm->dpp_z) {
3402 ret = os_snprintf(pos, end - pos, "dpp_pfs=1\n");
3403 if (os_snprintf_error(end - pos, ret))
3404 return pos - buf;
3405 pos += ret;
3406 }
3407#endif /* CONFIG_DPP2 */
3408
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003409 if (sm->mfp != NO_MGMT_FRAME_PROTECTION && sm->ap_rsn_ie) {
3410 struct wpa_ie_data rsn;
3411 if (wpa_parse_wpa_ie_rsn(sm->ap_rsn_ie, sm->ap_rsn_ie_len, &rsn)
3412 >= 0 &&
3413 rsn.capabilities & (WPA_CAPABILITY_MFPR |
3414 WPA_CAPABILITY_MFPC)) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003415 ret = os_snprintf(pos, end - pos, "pmf=%d\n"
3416 "mgmt_group_cipher=%s\n",
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003417 (rsn.capabilities &
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003418 WPA_CAPABILITY_MFPR) ? 2 : 1,
3419 wpa_cipher_txt(
3420 sm->mgmt_group_cipher));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003421 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003422 return pos - buf;
3423 pos += ret;
3424 }
3425 }
3426
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003427 return pos - buf;
3428}
3429
3430
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07003431int wpa_sm_pmf_enabled(struct wpa_sm *sm)
3432{
3433 struct wpa_ie_data rsn;
3434
3435 if (sm->mfp == NO_MGMT_FRAME_PROTECTION || !sm->ap_rsn_ie)
3436 return 0;
3437
3438 if (wpa_parse_wpa_ie_rsn(sm->ap_rsn_ie, sm->ap_rsn_ie_len, &rsn) >= 0 &&
3439 rsn.capabilities & (WPA_CAPABILITY_MFPR | WPA_CAPABILITY_MFPC))
3440 return 1;
3441
3442 return 0;
3443}
3444
3445
Hai Shalomfdcde762020-04-02 11:19:20 -07003446int wpa_sm_ext_key_id(struct wpa_sm *sm)
3447{
3448 return sm ? sm->ext_key_id : 0;
3449}
3450
3451
3452int wpa_sm_ext_key_id_active(struct wpa_sm *sm)
3453{
3454 return sm ? sm->use_ext_key_id : 0;
3455}
3456
3457
Hai Shalom74f70d42019-02-11 14:42:39 -08003458int wpa_sm_ocv_enabled(struct wpa_sm *sm)
3459{
3460 struct wpa_ie_data rsn;
3461
3462 if (!sm->ocv || !sm->ap_rsn_ie)
3463 return 0;
3464
3465 return wpa_parse_wpa_ie_rsn(sm->ap_rsn_ie, sm->ap_rsn_ie_len,
3466 &rsn) >= 0 &&
3467 (rsn.capabilities & WPA_CAPABILITY_OCVC);
3468}
3469
3470
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003471/**
3472 * wpa_sm_set_assoc_wpa_ie_default - Generate own WPA/RSN IE from configuration
3473 * @sm: Pointer to WPA state machine data from wpa_sm_init()
3474 * @wpa_ie: Pointer to buffer for WPA/RSN IE
3475 * @wpa_ie_len: Pointer to the length of the wpa_ie buffer
3476 * Returns: 0 on success, -1 on failure
3477 */
3478int wpa_sm_set_assoc_wpa_ie_default(struct wpa_sm *sm, u8 *wpa_ie,
3479 size_t *wpa_ie_len)
3480{
3481 int res;
3482
3483 if (sm == NULL)
3484 return -1;
3485
Dmitry Shmidt55840ad2015-12-14 12:45:46 -08003486#ifdef CONFIG_TESTING_OPTIONS
3487 if (sm->test_assoc_ie) {
3488 wpa_printf(MSG_DEBUG,
3489 "TESTING: Replace association WPA/RSN IE");
3490 if (*wpa_ie_len < wpabuf_len(sm->test_assoc_ie))
3491 return -1;
3492 os_memcpy(wpa_ie, wpabuf_head(sm->test_assoc_ie),
3493 wpabuf_len(sm->test_assoc_ie));
3494 res = wpabuf_len(sm->test_assoc_ie);
3495 } else
3496#endif /* CONFIG_TESTING_OPTIONS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003497 res = wpa_gen_wpa_ie(sm, wpa_ie, *wpa_ie_len);
3498 if (res < 0)
3499 return -1;
3500 *wpa_ie_len = res;
3501
3502 wpa_hexdump(MSG_DEBUG, "WPA: Set own WPA IE default",
3503 wpa_ie, *wpa_ie_len);
3504
3505 if (sm->assoc_wpa_ie == NULL) {
3506 /*
3507 * Make a copy of the WPA/RSN IE so that 4-Way Handshake gets
3508 * the correct version of the IE even if PMKSA caching is
3509 * aborted (which would remove PMKID from IE generation).
3510 */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003511 sm->assoc_wpa_ie = os_memdup(wpa_ie, *wpa_ie_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003512 if (sm->assoc_wpa_ie == NULL)
3513 return -1;
3514
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003515 sm->assoc_wpa_ie_len = *wpa_ie_len;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003516 } else {
3517 wpa_hexdump(MSG_DEBUG,
3518 "WPA: Leave previously set WPA IE default",
3519 sm->assoc_wpa_ie, sm->assoc_wpa_ie_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003520 }
3521
3522 return 0;
3523}
3524
3525
3526/**
3527 * wpa_sm_set_assoc_wpa_ie - Set own WPA/RSN IE from (Re)AssocReq
3528 * @sm: Pointer to WPA state machine data from wpa_sm_init()
3529 * @ie: Pointer to IE data (starting from id)
3530 * @len: IE length
3531 * Returns: 0 on success, -1 on failure
3532 *
3533 * Inform WPA state machine about the WPA/RSN IE used in (Re)Association
3534 * Request frame. The IE will be used to override the default value generated
3535 * with wpa_sm_set_assoc_wpa_ie_default().
3536 */
3537int wpa_sm_set_assoc_wpa_ie(struct wpa_sm *sm, const u8 *ie, size_t len)
3538{
3539 if (sm == NULL)
3540 return -1;
3541
3542 os_free(sm->assoc_wpa_ie);
3543 if (ie == NULL || len == 0) {
3544 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
3545 "WPA: clearing own WPA/RSN IE");
3546 sm->assoc_wpa_ie = NULL;
3547 sm->assoc_wpa_ie_len = 0;
3548 } else {
3549 wpa_hexdump(MSG_DEBUG, "WPA: set own WPA/RSN IE", ie, len);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003550 sm->assoc_wpa_ie = os_memdup(ie, len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003551 if (sm->assoc_wpa_ie == NULL)
3552 return -1;
3553
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003554 sm->assoc_wpa_ie_len = len;
3555 }
3556
3557 return 0;
3558}
3559
3560
3561/**
Hai Shalomc3565922019-10-28 11:58:20 -07003562 * wpa_sm_set_assoc_rsnxe_default - Generate own RSNXE from configuration
3563 * @sm: Pointer to WPA state machine data from wpa_sm_init()
3564 * @rsnxe: Pointer to buffer for RSNXE
3565 * @rsnxe_len: Pointer to the length of the rsne buffer
3566 * Returns: 0 on success, -1 on failure
3567 */
3568int wpa_sm_set_assoc_rsnxe_default(struct wpa_sm *sm, u8 *rsnxe,
3569 size_t *rsnxe_len)
3570{
3571 int res;
3572
3573 if (!sm)
3574 return -1;
3575
3576 res = wpa_gen_rsnxe(sm, rsnxe, *rsnxe_len);
3577 if (res < 0)
3578 return -1;
3579 *rsnxe_len = res;
3580
3581 wpa_hexdump(MSG_DEBUG, "RSN: Set own RSNXE default", rsnxe, *rsnxe_len);
3582
3583 if (sm->assoc_rsnxe) {
3584 wpa_hexdump(MSG_DEBUG,
3585 "RSN: Leave previously set RSNXE default",
3586 sm->assoc_rsnxe, sm->assoc_rsnxe_len);
3587 } else if (*rsnxe_len > 0) {
3588 /*
3589 * Make a copy of the RSNXE so that 4-Way Handshake gets the
3590 * correct version of the IE even if it gets changed.
3591 */
3592 sm->assoc_rsnxe = os_memdup(rsnxe, *rsnxe_len);
3593 if (!sm->assoc_rsnxe)
3594 return -1;
3595
3596 sm->assoc_rsnxe_len = *rsnxe_len;
3597 }
3598
3599 return 0;
3600}
3601
3602
3603/**
3604 * wpa_sm_set_assoc_rsnxe - Set own RSNXE from (Re)AssocReq
3605 * @sm: Pointer to WPA state machine data from wpa_sm_init()
3606 * @ie: Pointer to IE data (starting from id)
3607 * @len: IE length
3608 * Returns: 0 on success, -1 on failure
3609 *
3610 * Inform WPA state machine about the RSNXE used in (Re)Association Request
3611 * frame. The IE will be used to override the default value generated
3612 * with wpa_sm_set_assoc_rsnxe_default().
3613 */
3614int wpa_sm_set_assoc_rsnxe(struct wpa_sm *sm, const u8 *ie, size_t len)
3615{
3616 if (!sm)
3617 return -1;
3618
3619 os_free(sm->assoc_rsnxe);
3620 if (!ie || len == 0) {
3621 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
3622 "RSN: clearing own RSNXE");
3623 sm->assoc_rsnxe = NULL;
3624 sm->assoc_rsnxe_len = 0;
3625 } else {
3626 wpa_hexdump(MSG_DEBUG, "RSN: set own RSNXE", ie, len);
3627 sm->assoc_rsnxe = os_memdup(ie, len);
3628 if (!sm->assoc_rsnxe)
3629 return -1;
3630
3631 sm->assoc_rsnxe_len = len;
3632 }
3633
3634 return 0;
3635}
3636
3637
3638/**
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003639 * wpa_sm_set_ap_wpa_ie - Set AP WPA IE from Beacon/ProbeResp
3640 * @sm: Pointer to WPA state machine data from wpa_sm_init()
3641 * @ie: Pointer to IE data (starting from id)
3642 * @len: IE length
3643 * Returns: 0 on success, -1 on failure
3644 *
3645 * Inform WPA state machine about the WPA IE used in Beacon / Probe Response
3646 * frame.
3647 */
3648int wpa_sm_set_ap_wpa_ie(struct wpa_sm *sm, const u8 *ie, size_t len)
3649{
3650 if (sm == NULL)
3651 return -1;
3652
3653 os_free(sm->ap_wpa_ie);
3654 if (ie == NULL || len == 0) {
3655 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
3656 "WPA: clearing AP WPA IE");
3657 sm->ap_wpa_ie = NULL;
3658 sm->ap_wpa_ie_len = 0;
3659 } else {
3660 wpa_hexdump(MSG_DEBUG, "WPA: set AP WPA IE", ie, len);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003661 sm->ap_wpa_ie = os_memdup(ie, len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003662 if (sm->ap_wpa_ie == NULL)
3663 return -1;
3664
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003665 sm->ap_wpa_ie_len = len;
3666 }
3667
3668 return 0;
3669}
3670
3671
3672/**
3673 * wpa_sm_set_ap_rsn_ie - Set AP RSN IE from Beacon/ProbeResp
3674 * @sm: Pointer to WPA state machine data from wpa_sm_init()
3675 * @ie: Pointer to IE data (starting from id)
3676 * @len: IE length
3677 * Returns: 0 on success, -1 on failure
3678 *
3679 * Inform WPA state machine about the RSN IE used in Beacon / Probe Response
3680 * frame.
3681 */
3682int wpa_sm_set_ap_rsn_ie(struct wpa_sm *sm, const u8 *ie, size_t len)
3683{
3684 if (sm == NULL)
3685 return -1;
3686
3687 os_free(sm->ap_rsn_ie);
3688 if (ie == NULL || len == 0) {
3689 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
3690 "WPA: clearing AP RSN IE");
3691 sm->ap_rsn_ie = NULL;
3692 sm->ap_rsn_ie_len = 0;
3693 } else {
3694 wpa_hexdump(MSG_DEBUG, "WPA: set AP RSN IE", ie, len);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003695 sm->ap_rsn_ie = os_memdup(ie, len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003696 if (sm->ap_rsn_ie == NULL)
3697 return -1;
3698
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003699 sm->ap_rsn_ie_len = len;
3700 }
3701
3702 return 0;
3703}
3704
3705
3706/**
Hai Shalomc3565922019-10-28 11:58:20 -07003707 * wpa_sm_set_ap_rsnxe - Set AP RSNXE from Beacon/ProbeResp
3708 * @sm: Pointer to WPA state machine data from wpa_sm_init()
3709 * @ie: Pointer to IE data (starting from id)
3710 * @len: IE length
3711 * Returns: 0 on success, -1 on failure
3712 *
3713 * Inform WPA state machine about the RSNXE used in Beacon / Probe Response
3714 * frame.
3715 */
3716int wpa_sm_set_ap_rsnxe(struct wpa_sm *sm, const u8 *ie, size_t len)
3717{
3718 if (!sm)
3719 return -1;
3720
3721 os_free(sm->ap_rsnxe);
3722 if (!ie || len == 0) {
3723 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: clearing AP RSNXE");
3724 sm->ap_rsnxe = NULL;
3725 sm->ap_rsnxe_len = 0;
3726 } else {
3727 wpa_hexdump(MSG_DEBUG, "WPA: set AP RSNXE", ie, len);
3728 sm->ap_rsnxe = os_memdup(ie, len);
3729 if (!sm->ap_rsnxe)
3730 return -1;
3731
3732 sm->ap_rsnxe_len = len;
3733 }
3734
3735 return 0;
3736}
3737
3738
3739/**
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003740 * wpa_sm_parse_own_wpa_ie - Parse own WPA/RSN IE
3741 * @sm: Pointer to WPA state machine data from wpa_sm_init()
3742 * @data: Pointer to data area for parsing results
3743 * Returns: 0 on success, -1 if IE is not known, or -2 on parsing failure
3744 *
3745 * Parse the contents of the own WPA or RSN IE from (Re)AssocReq and write the
3746 * parsed data into data.
3747 */
3748int wpa_sm_parse_own_wpa_ie(struct wpa_sm *sm, struct wpa_ie_data *data)
3749{
3750 if (sm == NULL)
3751 return -1;
3752
3753 if (sm->assoc_wpa_ie == NULL) {
3754 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
3755 "WPA: No WPA/RSN IE available from association info");
3756 return -1;
3757 }
3758 if (wpa_parse_wpa_ie(sm->assoc_wpa_ie, sm->assoc_wpa_ie_len, data))
3759 return -2;
3760 return 0;
3761}
3762
3763
3764int wpa_sm_pmksa_cache_list(struct wpa_sm *sm, char *buf, size_t len)
3765{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003766 return pmksa_cache_list(sm->pmksa, buf, len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003767}
3768
3769
Dmitry Shmidt29333592017-01-09 12:27:11 -08003770struct rsn_pmksa_cache_entry * wpa_sm_pmksa_cache_head(struct wpa_sm *sm)
3771{
3772 return pmksa_cache_head(sm->pmksa);
3773}
3774
3775
3776struct rsn_pmksa_cache_entry *
3777wpa_sm_pmksa_cache_add_entry(struct wpa_sm *sm,
3778 struct rsn_pmksa_cache_entry * entry)
3779{
3780 return pmksa_cache_add_entry(sm->pmksa, entry);
3781}
3782
3783
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003784void wpa_sm_pmksa_cache_add(struct wpa_sm *sm, const u8 *pmk, size_t pmk_len,
3785 const u8 *pmkid, const u8 *bssid,
3786 const u8 *fils_cache_id)
3787{
3788 sm->cur_pmksa = pmksa_cache_add(sm->pmksa, pmk, pmk_len, pmkid, NULL, 0,
3789 bssid, sm->own_addr, sm->network_ctx,
3790 sm->key_mgmt, fils_cache_id);
3791}
3792
3793
3794int wpa_sm_pmksa_exists(struct wpa_sm *sm, const u8 *bssid,
3795 const void *network_ctx)
3796{
Roshan Pius3a1667e2018-07-03 15:17:14 -07003797 return pmksa_cache_get(sm->pmksa, bssid, NULL, network_ctx, 0) != NULL;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003798}
3799
3800
Hai Shalom60840252021-02-19 19:02:11 -08003801struct rsn_pmksa_cache_entry * wpa_sm_pmksa_cache_get(struct wpa_sm *sm,
3802 const u8 *aa,
3803 const u8 *pmkid,
3804 const void *network_ctx,
3805 int akmp)
3806{
3807 return pmksa_cache_get(sm->pmksa, aa, pmkid, network_ctx, akmp);
3808}
3809
3810
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003811void wpa_sm_drop_sa(struct wpa_sm *sm)
3812{
3813 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Clear old PMK and PTK");
3814 sm->ptk_set = 0;
3815 sm->tptk_set = 0;
Roshan Pius3a1667e2018-07-03 15:17:14 -07003816 sm->pmk_len = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003817 os_memset(sm->pmk, 0, sizeof(sm->pmk));
3818 os_memset(&sm->ptk, 0, sizeof(sm->ptk));
3819 os_memset(&sm->tptk, 0, sizeof(sm->tptk));
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02003820 os_memset(&sm->gtk, 0, sizeof(sm->gtk));
Jouni Malinen58c0e962017-10-01 12:12:24 +03003821 os_memset(&sm->gtk_wnm_sleep, 0, sizeof(sm->gtk_wnm_sleep));
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02003822 os_memset(&sm->igtk, 0, sizeof(sm->igtk));
Jouni Malinen58c0e962017-10-01 12:12:24 +03003823 os_memset(&sm->igtk_wnm_sleep, 0, sizeof(sm->igtk_wnm_sleep));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003824#ifdef CONFIG_IEEE80211R
3825 os_memset(sm->xxkey, 0, sizeof(sm->xxkey));
Roshan Pius3a1667e2018-07-03 15:17:14 -07003826 sm->xxkey_len = 0;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003827 os_memset(sm->pmk_r0, 0, sizeof(sm->pmk_r0));
Roshan Pius3a1667e2018-07-03 15:17:14 -07003828 sm->pmk_r0_len = 0;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003829 os_memset(sm->pmk_r1, 0, sizeof(sm->pmk_r1));
Roshan Pius3a1667e2018-07-03 15:17:14 -07003830 sm->pmk_r1_len = 0;
Hai Shalom60840252021-02-19 19:02:11 -08003831#ifdef CONFIG_PASN
3832 os_free(sm->pasn_r1kh);
3833 sm->pasn_r1kh = NULL;
3834 sm->n_pasn_r1kh = 0;
3835#endif /* CONFIG_PASN */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003836#endif /* CONFIG_IEEE80211R */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003837}
3838
3839
3840int wpa_sm_has_ptk(struct wpa_sm *sm)
3841{
3842 if (sm == NULL)
3843 return 0;
3844 return sm->ptk_set;
3845}
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003846
3847
Hai Shalomfdcde762020-04-02 11:19:20 -07003848int wpa_sm_has_ptk_installed(struct wpa_sm *sm)
3849{
3850 if (!sm)
3851 return 0;
3852 return sm->ptk.installed;
3853}
3854
3855
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003856void wpa_sm_update_replay_ctr(struct wpa_sm *sm, const u8 *replay_ctr)
3857{
3858 os_memcpy(sm->rx_replay_counter, replay_ctr, WPA_REPLAY_COUNTER_LEN);
3859}
3860
3861
3862void wpa_sm_pmksa_cache_flush(struct wpa_sm *sm, void *network_ctx)
3863{
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07003864 pmksa_cache_flush(sm->pmksa, network_ctx, NULL, 0);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003865}
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003866
Mir Ali677e7482020-11-12 19:49:02 +05303867#ifdef CONFIG_DRIVER_NL80211_BRCM
3868void wpa_sm_install_pmk(struct wpa_sm *sm)
3869{
3870 /* In case the driver wants to handle re-assocs, pass it down the PMK. */
3871 if (wpa_sm_set_key(sm, wpa_cipher_to_alg(sm->pairwise_cipher), NULL, 0, 0, NULL, 0,
3872 (u8*)sm->pmk, sm->pmk_len, KEY_FLAG_PMK) < 0) {
3873 wpa_hexdump(MSG_DEBUG, "PSK: Install PMK to the driver for driver reassociations",
3874 (u8*)sm->pmk, sm->pmk_len);
3875 /* No harm if the driver doesn't support. */
3876 wpa_msg(sm->ctx->msg_ctx, MSG_DEBUG,
3877 "WPA: Failed to set PMK to the driver");
3878 }
3879}
3880#endif /* CONFIG_DRIVER_NL80211_BRCM */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003881
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003882#ifdef CONFIG_WNM
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003883int wpa_wnmsleep_install_key(struct wpa_sm *sm, u8 subelem_id, u8 *buf)
3884{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003885 u16 keyinfo;
3886 u8 keylen; /* plaintext key len */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003887 u8 *key_rsc;
3888
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003889 if (subelem_id == WNM_SLEEP_SUBELEM_GTK) {
Dmitry Shmidt61593f02014-04-21 16:27:35 -07003890 struct wpa_gtk_data gd;
3891
3892 os_memset(&gd, 0, sizeof(gd));
3893 keylen = wpa_cipher_key_len(sm->group_cipher);
3894 gd.key_rsc_len = wpa_cipher_rsc_len(sm->group_cipher);
3895 gd.alg = wpa_cipher_to_alg(sm->group_cipher);
3896 if (gd.alg == WPA_ALG_NONE) {
3897 wpa_printf(MSG_DEBUG, "Unsupported group cipher suite");
3898 return -1;
3899 }
3900
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003901 key_rsc = buf + 5;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003902 keyinfo = WPA_GET_LE16(buf + 2);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003903 gd.gtk_len = keylen;
3904 if (gd.gtk_len != buf[4]) {
3905 wpa_printf(MSG_DEBUG, "GTK len mismatch len %d vs %d",
3906 gd.gtk_len, buf[4]);
3907 return -1;
3908 }
3909 gd.keyidx = keyinfo & 0x03; /* B0 - B1 */
3910 gd.tx = wpa_supplicant_gtk_tx_bit_workaround(
3911 sm, !!(keyinfo & WPA_KEY_INFO_TXRX));
3912
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003913 os_memcpy(gd.gtk, buf + 13, gd.gtk_len);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003914
3915 wpa_hexdump_key(MSG_DEBUG, "Install GTK (WNM SLEEP)",
3916 gd.gtk, gd.gtk_len);
Jouni Malinen58c0e962017-10-01 12:12:24 +03003917 if (wpa_supplicant_install_gtk(sm, &gd, key_rsc, 1)) {
Hai Shalom81f62d82019-07-22 12:10:00 -07003918 forced_memzero(&gd, sizeof(gd));
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003919 wpa_printf(MSG_DEBUG, "Failed to install the GTK in "
3920 "WNM mode");
3921 return -1;
3922 }
Hai Shalom81f62d82019-07-22 12:10:00 -07003923 forced_memzero(&gd, sizeof(gd));
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003924 } else if (subelem_id == WNM_SLEEP_SUBELEM_IGTK) {
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02003925 const struct wpa_igtk_kde *igtk;
Dmitry Shmidt61593f02014-04-21 16:27:35 -07003926
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02003927 igtk = (const struct wpa_igtk_kde *) (buf + 2);
Jouni Malinen58c0e962017-10-01 12:12:24 +03003928 if (wpa_supplicant_install_igtk(sm, igtk, 1) < 0)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003929 return -1;
Hai Shalomfdcde762020-04-02 11:19:20 -07003930 } else if (subelem_id == WNM_SLEEP_SUBELEM_BIGTK) {
3931 const struct wpa_bigtk_kde *bigtk;
3932
3933 bigtk = (const struct wpa_bigtk_kde *) (buf + 2);
3934 if (sm->beacon_prot &&
3935 wpa_supplicant_install_bigtk(sm, bigtk, 1) < 0)
3936 return -1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003937 } else {
3938 wpa_printf(MSG_DEBUG, "Unknown element id");
3939 return -1;
3940 }
3941
3942 return 0;
3943}
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003944#endif /* CONFIG_WNM */
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003945
3946
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003947#ifdef CONFIG_P2P
3948
3949int wpa_sm_get_p2p_ip_addr(struct wpa_sm *sm, u8 *buf)
3950{
3951 if (sm == NULL || WPA_GET_BE32(sm->p2p_ip_addr) == 0)
3952 return -1;
3953 os_memcpy(buf, sm->p2p_ip_addr, 3 * 4);
3954 return 0;
3955}
3956
3957#endif /* CONFIG_P2P */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003958
3959
3960void wpa_sm_set_rx_replay_ctr(struct wpa_sm *sm, const u8 *rx_replay_counter)
3961{
3962 if (rx_replay_counter == NULL)
3963 return;
3964
3965 os_memcpy(sm->rx_replay_counter, rx_replay_counter,
3966 WPA_REPLAY_COUNTER_LEN);
3967 sm->rx_replay_counter_set = 1;
3968 wpa_printf(MSG_DEBUG, "Updated key replay counter");
3969}
3970
3971
Dmitry Shmidt807291d2015-01-27 13:40:23 -08003972void wpa_sm_set_ptk_kck_kek(struct wpa_sm *sm,
3973 const u8 *ptk_kck, size_t ptk_kck_len,
3974 const u8 *ptk_kek, size_t ptk_kek_len)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003975{
Dmitry Shmidt807291d2015-01-27 13:40:23 -08003976 if (ptk_kck && ptk_kck_len <= WPA_KCK_MAX_LEN) {
3977 os_memcpy(sm->ptk.kck, ptk_kck, ptk_kck_len);
3978 sm->ptk.kck_len = ptk_kck_len;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003979 wpa_printf(MSG_DEBUG, "Updated PTK KCK");
3980 }
Dmitry Shmidt807291d2015-01-27 13:40:23 -08003981 if (ptk_kek && ptk_kek_len <= WPA_KEK_MAX_LEN) {
3982 os_memcpy(sm->ptk.kek, ptk_kek, ptk_kek_len);
3983 sm->ptk.kek_len = ptk_kek_len;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003984 wpa_printf(MSG_DEBUG, "Updated PTK KEK");
3985 }
3986 sm->ptk_set = 1;
3987}
Dmitry Shmidt55840ad2015-12-14 12:45:46 -08003988
3989
3990#ifdef CONFIG_TESTING_OPTIONS
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003991
Dmitry Shmidt55840ad2015-12-14 12:45:46 -08003992void wpa_sm_set_test_assoc_ie(struct wpa_sm *sm, struct wpabuf *buf)
3993{
3994 wpabuf_free(sm->test_assoc_ie);
3995 sm->test_assoc_ie = buf;
3996}
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003997
3998
3999const u8 * wpa_sm_get_anonce(struct wpa_sm *sm)
4000{
4001 return sm->anonce;
4002}
4003
Dmitry Shmidt55840ad2015-12-14 12:45:46 -08004004#endif /* CONFIG_TESTING_OPTIONS */
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004005
4006
Roshan Pius3a1667e2018-07-03 15:17:14 -07004007unsigned int wpa_sm_get_key_mgmt(struct wpa_sm *sm)
4008{
4009 return sm->key_mgmt;
4010}
4011
4012
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004013#ifdef CONFIG_FILS
4014
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004015struct wpabuf * fils_build_auth(struct wpa_sm *sm, int dh_group, const u8 *md)
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004016{
4017 struct wpabuf *buf = NULL;
4018 struct wpabuf *erp_msg;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004019 struct wpabuf *pub = NULL;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004020
4021 erp_msg = eapol_sm_build_erp_reauth_start(sm->eapol);
4022 if (!erp_msg && !sm->cur_pmksa) {
4023 wpa_printf(MSG_DEBUG,
4024 "FILS: Neither ERP EAP-Initiate/Re-auth nor PMKSA cache entry is available - skip FILS");
4025 goto fail;
4026 }
4027
4028 wpa_printf(MSG_DEBUG, "FILS: Try to use FILS (erp=%d pmksa_cache=%d)",
4029 erp_msg != NULL, sm->cur_pmksa != NULL);
4030
4031 sm->fils_completed = 0;
4032
4033 if (!sm->assoc_wpa_ie) {
4034 wpa_printf(MSG_INFO, "FILS: No own RSN IE set for FILS");
4035 goto fail;
4036 }
4037
4038 if (random_get_bytes(sm->fils_nonce, FILS_NONCE_LEN) < 0 ||
4039 random_get_bytes(sm->fils_session, FILS_SESSION_LEN) < 0)
4040 goto fail;
4041
4042 wpa_hexdump(MSG_DEBUG, "FILS: Generated FILS Nonce",
4043 sm->fils_nonce, FILS_NONCE_LEN);
4044 wpa_hexdump(MSG_DEBUG, "FILS: Generated FILS Session",
4045 sm->fils_session, FILS_SESSION_LEN);
4046
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004047#ifdef CONFIG_FILS_SK_PFS
4048 sm->fils_dh_group = dh_group;
4049 if (dh_group) {
4050 crypto_ecdh_deinit(sm->fils_ecdh);
4051 sm->fils_ecdh = crypto_ecdh_init(dh_group);
4052 if (!sm->fils_ecdh) {
4053 wpa_printf(MSG_INFO,
4054 "FILS: Could not initialize ECDH with group %d",
4055 dh_group);
4056 goto fail;
4057 }
4058 pub = crypto_ecdh_get_pubkey(sm->fils_ecdh, 1);
4059 if (!pub)
4060 goto fail;
4061 wpa_hexdump_buf(MSG_DEBUG, "FILS: Element (DH public key)",
4062 pub);
4063 sm->fils_dh_elem_len = wpabuf_len(pub);
4064 }
4065#endif /* CONFIG_FILS_SK_PFS */
4066
4067 buf = wpabuf_alloc(1000 + sm->assoc_wpa_ie_len +
4068 (pub ? wpabuf_len(pub) : 0));
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004069 if (!buf)
4070 goto fail;
4071
4072 /* Fields following the Authentication algorithm number field */
4073
4074 /* Authentication Transaction seq# */
4075 wpabuf_put_le16(buf, 1);
4076
4077 /* Status Code */
4078 wpabuf_put_le16(buf, WLAN_STATUS_SUCCESS);
4079
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004080 /* TODO: FILS PK */
4081#ifdef CONFIG_FILS_SK_PFS
4082 if (dh_group) {
4083 /* Finite Cyclic Group */
4084 wpabuf_put_le16(buf, dh_group);
4085 /* Element */
4086 wpabuf_put_buf(buf, pub);
4087 }
4088#endif /* CONFIG_FILS_SK_PFS */
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004089
4090 /* RSNE */
4091 wpa_hexdump(MSG_DEBUG, "FILS: RSNE in FILS Authentication frame",
4092 sm->assoc_wpa_ie, sm->assoc_wpa_ie_len);
4093 wpabuf_put_data(buf, sm->assoc_wpa_ie, sm->assoc_wpa_ie_len);
4094
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004095 if (md) {
4096 /* MDE when using FILS for FT initial association */
4097 struct rsn_mdie *mdie;
4098
4099 wpabuf_put_u8(buf, WLAN_EID_MOBILITY_DOMAIN);
4100 wpabuf_put_u8(buf, sizeof(*mdie));
4101 mdie = wpabuf_put(buf, sizeof(*mdie));
4102 os_memcpy(mdie->mobility_domain, md, MOBILITY_DOMAIN_ID_LEN);
4103 mdie->ft_capab = 0;
4104 }
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004105
4106 /* FILS Nonce */
4107 wpabuf_put_u8(buf, WLAN_EID_EXTENSION); /* Element ID */
4108 wpabuf_put_u8(buf, 1 + FILS_NONCE_LEN); /* Length */
4109 /* Element ID Extension */
4110 wpabuf_put_u8(buf, WLAN_EID_EXT_FILS_NONCE);
4111 wpabuf_put_data(buf, sm->fils_nonce, FILS_NONCE_LEN);
4112
4113 /* FILS Session */
4114 wpabuf_put_u8(buf, WLAN_EID_EXTENSION); /* Element ID */
4115 wpabuf_put_u8(buf, 1 + FILS_SESSION_LEN); /* Length */
4116 /* Element ID Extension */
4117 wpabuf_put_u8(buf, WLAN_EID_EXT_FILS_SESSION);
4118 wpabuf_put_data(buf, sm->fils_session, FILS_SESSION_LEN);
4119
Hai Shalomfdcde762020-04-02 11:19:20 -07004120 /* Wrapped Data */
Paul Stewart092955c2017-02-06 09:13:09 -08004121 sm->fils_erp_pmkid_set = 0;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004122 if (erp_msg) {
4123 wpabuf_put_u8(buf, WLAN_EID_EXTENSION); /* Element ID */
4124 wpabuf_put_u8(buf, 1 + wpabuf_len(erp_msg)); /* Length */
4125 /* Element ID Extension */
Hai Shalomfdcde762020-04-02 11:19:20 -07004126 wpabuf_put_u8(buf, WLAN_EID_EXT_WRAPPED_DATA);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004127 wpabuf_put_buf(buf, erp_msg);
Paul Stewart092955c2017-02-06 09:13:09 -08004128 /* Calculate pending PMKID here so that we do not need to
4129 * maintain a copy of the EAP-Initiate/Reauth message. */
4130 if (fils_pmkid_erp(sm->key_mgmt, wpabuf_head(erp_msg),
4131 wpabuf_len(erp_msg),
4132 sm->fils_erp_pmkid) == 0)
4133 sm->fils_erp_pmkid_set = 1;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004134 }
4135
4136 wpa_hexdump_buf(MSG_DEBUG, "RSN: FILS fields for Authentication frame",
4137 buf);
4138
4139fail:
4140 wpabuf_free(erp_msg);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004141 wpabuf_free(pub);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004142 return buf;
4143}
4144
4145
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004146int fils_process_auth(struct wpa_sm *sm, const u8 *bssid, const u8 *data,
4147 size_t len)
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004148{
4149 const u8 *pos, *end;
4150 struct ieee802_11_elems elems;
4151 struct wpa_ie_data rsn;
4152 int pmkid_match = 0;
4153 u8 ick[FILS_ICK_MAX_LEN];
4154 size_t ick_len;
4155 int res;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004156 struct wpabuf *dh_ss = NULL;
4157 const u8 *g_sta = NULL;
4158 size_t g_sta_len = 0;
4159 const u8 *g_ap = NULL;
Hai Shalom60840252021-02-19 19:02:11 -08004160 size_t g_ap_len = 0, kdk_len;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004161 struct wpabuf *pub = NULL;
4162
4163 os_memcpy(sm->bssid, bssid, ETH_ALEN);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004164
4165 wpa_hexdump(MSG_DEBUG, "FILS: Authentication frame fields",
4166 data, len);
4167 pos = data;
4168 end = data + len;
4169
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004170 /* TODO: FILS PK */
4171#ifdef CONFIG_FILS_SK_PFS
4172 if (sm->fils_dh_group) {
4173 u16 group;
4174
4175 /* Using FILS PFS */
4176
4177 /* Finite Cyclic Group */
4178 if (end - pos < 2) {
4179 wpa_printf(MSG_DEBUG,
4180 "FILS: No room for Finite Cyclic Group");
4181 goto fail;
4182 }
4183 group = WPA_GET_LE16(pos);
4184 pos += 2;
4185 if (group != sm->fils_dh_group) {
4186 wpa_printf(MSG_DEBUG,
4187 "FILS: Unexpected change in Finite Cyclic Group: %u (expected %u)",
4188 group, sm->fils_dh_group);
4189 goto fail;
4190 }
4191
4192 /* Element */
4193 if ((size_t) (end - pos) < sm->fils_dh_elem_len) {
4194 wpa_printf(MSG_DEBUG, "FILS: No room for Element");
4195 goto fail;
4196 }
4197
4198 if (!sm->fils_ecdh) {
4199 wpa_printf(MSG_DEBUG, "FILS: No ECDH state available");
4200 goto fail;
4201 }
4202 dh_ss = crypto_ecdh_set_peerkey(sm->fils_ecdh, 1, pos,
4203 sm->fils_dh_elem_len);
4204 if (!dh_ss) {
4205 wpa_printf(MSG_DEBUG, "FILS: ECDH operation failed");
4206 goto fail;
4207 }
4208 wpa_hexdump_buf_key(MSG_DEBUG, "FILS: DH_SS", dh_ss);
4209 g_ap = pos;
4210 g_ap_len = sm->fils_dh_elem_len;
4211 pos += sm->fils_dh_elem_len;
4212 }
4213#endif /* CONFIG_FILS_SK_PFS */
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004214
4215 wpa_hexdump(MSG_DEBUG, "FILS: Remaining IEs", pos, end - pos);
4216 if (ieee802_11_parse_elems(pos, end - pos, &elems, 1) == ParseFailed) {
4217 wpa_printf(MSG_DEBUG, "FILS: Could not parse elements");
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004218 goto fail;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004219 }
4220
4221 /* RSNE */
4222 wpa_hexdump(MSG_DEBUG, "FILS: RSN element", elems.rsn_ie,
4223 elems.rsn_ie_len);
4224 if (!elems.rsn_ie ||
4225 wpa_parse_wpa_ie_rsn(elems.rsn_ie - 2, elems.rsn_ie_len + 2,
4226 &rsn) < 0) {
4227 wpa_printf(MSG_DEBUG, "FILS: No RSN element");
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004228 goto fail;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004229 }
4230
4231 if (!elems.fils_nonce) {
4232 wpa_printf(MSG_DEBUG, "FILS: No FILS Nonce field");
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004233 goto fail;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004234 }
4235 os_memcpy(sm->fils_anonce, elems.fils_nonce, FILS_NONCE_LEN);
4236 wpa_hexdump(MSG_DEBUG, "FILS: ANonce", sm->fils_anonce, FILS_NONCE_LEN);
4237
Roshan Pius3a1667e2018-07-03 15:17:14 -07004238#ifdef CONFIG_IEEE80211R
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004239 if (wpa_key_mgmt_ft(sm->key_mgmt)) {
4240 struct wpa_ft_ies parse;
4241
4242 if (!elems.mdie || !elems.ftie) {
4243 wpa_printf(MSG_DEBUG, "FILS+FT: No MDE or FTE");
4244 goto fail;
4245 }
4246
Roshan Pius3a1667e2018-07-03 15:17:14 -07004247 if (wpa_ft_parse_ies(pos, end - pos, &parse,
4248 wpa_key_mgmt_sha384(sm->key_mgmt)) < 0) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004249 wpa_printf(MSG_DEBUG, "FILS+FT: Failed to parse IEs");
4250 goto fail;
4251 }
4252
4253 if (!parse.r0kh_id) {
4254 wpa_printf(MSG_DEBUG,
4255 "FILS+FT: No R0KH-ID subelem in FTE");
4256 goto fail;
4257 }
4258 os_memcpy(sm->r0kh_id, parse.r0kh_id, parse.r0kh_id_len);
4259 sm->r0kh_id_len = parse.r0kh_id_len;
4260 wpa_hexdump_ascii(MSG_DEBUG, "FILS+FT: R0KH-ID",
4261 sm->r0kh_id, sm->r0kh_id_len);
4262
4263 if (!parse.r1kh_id) {
4264 wpa_printf(MSG_DEBUG,
4265 "FILS+FT: No R1KH-ID subelem in FTE");
4266 goto fail;
4267 }
4268 os_memcpy(sm->r1kh_id, parse.r1kh_id, FT_R1KH_ID_LEN);
4269 wpa_hexdump(MSG_DEBUG, "FILS+FT: R1KH-ID",
4270 sm->r1kh_id, FT_R1KH_ID_LEN);
4271
4272 /* TODO: Check MDE and FTE payload */
4273
4274 wpabuf_free(sm->fils_ft_ies);
4275 sm->fils_ft_ies = wpabuf_alloc(2 + elems.mdie_len +
4276 2 + elems.ftie_len);
4277 if (!sm->fils_ft_ies)
4278 goto fail;
4279 wpabuf_put_data(sm->fils_ft_ies, elems.mdie - 2,
4280 2 + elems.mdie_len);
4281 wpabuf_put_data(sm->fils_ft_ies, elems.ftie - 2,
4282 2 + elems.ftie_len);
4283 } else {
4284 wpabuf_free(sm->fils_ft_ies);
4285 sm->fils_ft_ies = NULL;
4286 }
Roshan Pius3a1667e2018-07-03 15:17:14 -07004287#endif /* CONFIG_IEEE80211R */
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004288
4289 /* PMKID List */
4290 if (rsn.pmkid && rsn.num_pmkid > 0) {
4291 wpa_hexdump(MSG_DEBUG, "FILS: PMKID List",
4292 rsn.pmkid, rsn.num_pmkid * PMKID_LEN);
4293
4294 if (rsn.num_pmkid != 1) {
4295 wpa_printf(MSG_DEBUG, "FILS: Invalid PMKID selection");
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004296 goto fail;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004297 }
4298 wpa_hexdump(MSG_DEBUG, "FILS: PMKID", rsn.pmkid, PMKID_LEN);
4299 if (os_memcmp(sm->cur_pmksa->pmkid, rsn.pmkid, PMKID_LEN) != 0)
4300 {
4301 wpa_printf(MSG_DEBUG, "FILS: PMKID mismatch");
4302 wpa_hexdump(MSG_DEBUG, "FILS: Expected PMKID",
4303 sm->cur_pmksa->pmkid, PMKID_LEN);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004304 goto fail;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004305 }
4306 wpa_printf(MSG_DEBUG,
4307 "FILS: Matching PMKID - continue using PMKSA caching");
4308 pmkid_match = 1;
4309 }
4310 if (!pmkid_match && sm->cur_pmksa) {
4311 wpa_printf(MSG_DEBUG,
4312 "FILS: No PMKID match - cannot use cached PMKSA entry");
4313 sm->cur_pmksa = NULL;
4314 }
4315
4316 /* FILS Session */
4317 if (!elems.fils_session) {
4318 wpa_printf(MSG_DEBUG, "FILS: No FILS Session element");
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004319 goto fail;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004320 }
4321 wpa_hexdump(MSG_DEBUG, "FILS: FILS Session", elems.fils_session,
4322 FILS_SESSION_LEN);
4323 if (os_memcmp(sm->fils_session, elems.fils_session, FILS_SESSION_LEN)
4324 != 0) {
4325 wpa_printf(MSG_DEBUG, "FILS: Session mismatch");
4326 wpa_hexdump(MSG_DEBUG, "FILS: Expected FILS Session",
4327 sm->fils_session, FILS_SESSION_LEN);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004328 goto fail;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004329 }
4330
Hai Shalomfdcde762020-04-02 11:19:20 -07004331 /* Wrapped Data */
4332 if (!sm->cur_pmksa && elems.wrapped_data) {
Paul Stewart092955c2017-02-06 09:13:09 -08004333 u8 rmsk[ERP_MAX_KEY_LEN];
4334 size_t rmsk_len;
4335
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004336 wpa_hexdump(MSG_DEBUG, "FILS: Wrapped Data",
Hai Shalomfdcde762020-04-02 11:19:20 -07004337 elems.wrapped_data,
4338 elems.wrapped_data_len);
4339 eapol_sm_process_erp_finish(sm->eapol, elems.wrapped_data,
4340 elems.wrapped_data_len);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004341 if (eapol_sm_failed(sm->eapol))
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004342 goto fail;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004343
Paul Stewart092955c2017-02-06 09:13:09 -08004344 rmsk_len = ERP_MAX_KEY_LEN;
4345 res = eapol_sm_get_key(sm->eapol, rmsk, rmsk_len);
4346 if (res == PMK_LEN) {
4347 rmsk_len = PMK_LEN;
4348 res = eapol_sm_get_key(sm->eapol, rmsk, rmsk_len);
4349 }
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004350 if (res)
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004351 goto fail;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004352
Paul Stewart092955c2017-02-06 09:13:09 -08004353 res = fils_rmsk_to_pmk(sm->key_mgmt, rmsk, rmsk_len,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004354 sm->fils_nonce, sm->fils_anonce,
4355 dh_ss ? wpabuf_head(dh_ss) : NULL,
4356 dh_ss ? wpabuf_len(dh_ss) : 0,
Paul Stewart092955c2017-02-06 09:13:09 -08004357 sm->pmk, &sm->pmk_len);
Hai Shalom81f62d82019-07-22 12:10:00 -07004358 forced_memzero(rmsk, sizeof(rmsk));
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004359
4360 /* Don't use DHss in PTK derivation if PMKSA caching is not
4361 * used. */
4362 wpabuf_clear_free(dh_ss);
4363 dh_ss = NULL;
4364
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08004365 if (res)
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004366 goto fail;
Paul Stewart092955c2017-02-06 09:13:09 -08004367
4368 if (!sm->fils_erp_pmkid_set) {
4369 wpa_printf(MSG_DEBUG, "FILS: PMKID not available");
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004370 goto fail;
Paul Stewart092955c2017-02-06 09:13:09 -08004371 }
4372 wpa_hexdump(MSG_DEBUG, "FILS: PMKID", sm->fils_erp_pmkid,
4373 PMKID_LEN);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004374 wpa_printf(MSG_DEBUG, "FILS: ERP processing succeeded - add PMKSA cache entry for the result");
Paul Stewart092955c2017-02-06 09:13:09 -08004375 sm->cur_pmksa = pmksa_cache_add(sm->pmksa, sm->pmk, sm->pmk_len,
4376 sm->fils_erp_pmkid, NULL, 0,
4377 sm->bssid, sm->own_addr,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004378 sm->network_ctx, sm->key_mgmt,
4379 NULL);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004380 }
4381
4382 if (!sm->cur_pmksa) {
4383 wpa_printf(MSG_DEBUG,
4384 "FILS: No remaining options to continue FILS authentication");
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004385 goto fail;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004386 }
4387
Hai Shalom60840252021-02-19 19:02:11 -08004388 if (sm->force_kdk_derivation ||
4389 (sm->secure_ltf && sm->ap_rsnxe && sm->ap_rsnxe_len >= 4 &&
4390 sm->ap_rsnxe[3] & BIT(WLAN_RSNX_CAPAB_SECURE_LTF - 8)))
4391 kdk_len = WPA_KDK_MAX_LEN;
4392 else
4393 kdk_len = 0;
4394
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004395 if (fils_pmk_to_ptk(sm->pmk, sm->pmk_len, sm->own_addr, sm->bssid,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004396 sm->fils_nonce, sm->fils_anonce,
4397 dh_ss ? wpabuf_head(dh_ss) : NULL,
4398 dh_ss ? wpabuf_len(dh_ss) : 0,
4399 &sm->ptk, ick, &ick_len,
4400 sm->key_mgmt, sm->pairwise_cipher,
Hai Shalom60840252021-02-19 19:02:11 -08004401 sm->fils_ft, &sm->fils_ft_len,
4402 kdk_len) < 0) {
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004403 wpa_printf(MSG_DEBUG, "FILS: Failed to derive PTK");
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004404 goto fail;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004405 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004406
4407 wpabuf_clear_free(dh_ss);
4408 dh_ss = NULL;
4409
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004410 sm->ptk_set = 1;
4411 sm->tptk_set = 0;
4412 os_memset(&sm->tptk, 0, sizeof(sm->tptk));
4413
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004414#ifdef CONFIG_FILS_SK_PFS
4415 if (sm->fils_dh_group) {
4416 if (!sm->fils_ecdh) {
4417 wpa_printf(MSG_INFO, "FILS: ECDH not initialized");
4418 goto fail;
4419 }
4420 pub = crypto_ecdh_get_pubkey(sm->fils_ecdh, 1);
4421 if (!pub)
4422 goto fail;
4423 wpa_hexdump_buf(MSG_DEBUG, "FILS: gSTA", pub);
4424 g_sta = wpabuf_head(pub);
4425 g_sta_len = wpabuf_len(pub);
4426 if (!g_ap) {
4427 wpa_printf(MSG_INFO, "FILS: gAP not available");
4428 goto fail;
4429 }
4430 wpa_hexdump(MSG_DEBUG, "FILS: gAP", g_ap, g_ap_len);
4431 }
4432#endif /* CONFIG_FILS_SK_PFS */
4433
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004434 res = fils_key_auth_sk(ick, ick_len, sm->fils_nonce,
4435 sm->fils_anonce, sm->own_addr, sm->bssid,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004436 g_sta, g_sta_len, g_ap, g_ap_len,
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004437 sm->key_mgmt, sm->fils_key_auth_sta,
4438 sm->fils_key_auth_ap,
4439 &sm->fils_key_auth_len);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004440 wpabuf_free(pub);
Hai Shalom81f62d82019-07-22 12:10:00 -07004441 forced_memzero(ick, sizeof(ick));
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004442 return res;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004443fail:
4444 wpabuf_free(pub);
4445 wpabuf_clear_free(dh_ss);
4446 return -1;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004447}
4448
4449
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004450#ifdef CONFIG_IEEE80211R
4451static int fils_ft_build_assoc_req_rsne(struct wpa_sm *sm, struct wpabuf *buf)
4452{
4453 struct rsn_ie_hdr *rsnie;
4454 u16 capab;
4455 u8 *pos;
Roshan Pius3a1667e2018-07-03 15:17:14 -07004456 int use_sha384 = wpa_key_mgmt_sha384(sm->key_mgmt);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004457
4458 /* RSNIE[PMKR0Name/PMKR1Name] */
4459 rsnie = wpabuf_put(buf, sizeof(*rsnie));
4460 rsnie->elem_id = WLAN_EID_RSN;
4461 WPA_PUT_LE16(rsnie->version, RSN_VERSION);
4462
4463 /* Group Suite Selector */
4464 if (!wpa_cipher_valid_group(sm->group_cipher)) {
4465 wpa_printf(MSG_WARNING, "FT: Invalid group cipher (%d)",
4466 sm->group_cipher);
4467 return -1;
4468 }
4469 pos = wpabuf_put(buf, RSN_SELECTOR_LEN);
4470 RSN_SELECTOR_PUT(pos, wpa_cipher_to_suite(WPA_PROTO_RSN,
4471 sm->group_cipher));
4472
4473 /* Pairwise Suite Count */
4474 wpabuf_put_le16(buf, 1);
4475
4476 /* Pairwise Suite List */
4477 if (!wpa_cipher_valid_pairwise(sm->pairwise_cipher)) {
4478 wpa_printf(MSG_WARNING, "FT: Invalid pairwise cipher (%d)",
4479 sm->pairwise_cipher);
4480 return -1;
4481 }
4482 pos = wpabuf_put(buf, RSN_SELECTOR_LEN);
4483 RSN_SELECTOR_PUT(pos, wpa_cipher_to_suite(WPA_PROTO_RSN,
4484 sm->pairwise_cipher));
4485
4486 /* Authenticated Key Management Suite Count */
4487 wpabuf_put_le16(buf, 1);
4488
4489 /* Authenticated Key Management Suite List */
4490 pos = wpabuf_put(buf, RSN_SELECTOR_LEN);
4491 if (sm->key_mgmt == WPA_KEY_MGMT_FT_FILS_SHA256)
4492 RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_FT_FILS_SHA256);
4493 else if (sm->key_mgmt == WPA_KEY_MGMT_FT_FILS_SHA384)
4494 RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_FT_FILS_SHA384);
4495 else {
4496 wpa_printf(MSG_WARNING,
4497 "FILS+FT: Invalid key management type (%d)",
4498 sm->key_mgmt);
4499 return -1;
4500 }
4501
4502 /* RSN Capabilities */
4503 capab = 0;
Hai Shalomc3565922019-10-28 11:58:20 -07004504 if (sm->mfp)
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004505 capab |= WPA_CAPABILITY_MFPC;
Hai Shalomc3565922019-10-28 11:58:20 -07004506 if (sm->mfp == 2)
4507 capab |= WPA_CAPABILITY_MFPR;
Hai Shalom74f70d42019-02-11 14:42:39 -08004508 if (sm->ocv)
4509 capab |= WPA_CAPABILITY_OCVC;
Hai Shalomfdcde762020-04-02 11:19:20 -07004510 if (sm->ext_key_id)
4511 capab |= WPA_CAPABILITY_EXT_KEY_ID_FOR_UNICAST;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004512 wpabuf_put_le16(buf, capab);
4513
4514 /* PMKID Count */
4515 wpabuf_put_le16(buf, 1);
4516
4517 /* PMKID List [PMKR1Name] */
4518 wpa_hexdump_key(MSG_DEBUG, "FILS+FT: XXKey (FILS-FT)",
4519 sm->fils_ft, sm->fils_ft_len);
4520 wpa_hexdump_ascii(MSG_DEBUG, "FILS+FT: SSID", sm->ssid, sm->ssid_len);
4521 wpa_hexdump(MSG_DEBUG, "FILS+FT: MDID",
4522 sm->mobility_domain, MOBILITY_DOMAIN_ID_LEN);
4523 wpa_hexdump_ascii(MSG_DEBUG, "FILS+FT: R0KH-ID",
4524 sm->r0kh_id, sm->r0kh_id_len);
4525 if (wpa_derive_pmk_r0(sm->fils_ft, sm->fils_ft_len, sm->ssid,
4526 sm->ssid_len, sm->mobility_domain,
4527 sm->r0kh_id, sm->r0kh_id_len, sm->own_addr,
Roshan Pius3a1667e2018-07-03 15:17:14 -07004528 sm->pmk_r0, sm->pmk_r0_name, use_sha384) < 0) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004529 wpa_printf(MSG_WARNING, "FILS+FT: Could not derive PMK-R0");
4530 return -1;
4531 }
Roshan Pius3a1667e2018-07-03 15:17:14 -07004532 sm->pmk_r0_len = use_sha384 ? SHA384_MAC_LEN : PMK_LEN;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004533 wpa_printf(MSG_DEBUG, "FILS+FT: R1KH-ID: " MACSTR,
4534 MAC2STR(sm->r1kh_id));
4535 pos = wpabuf_put(buf, WPA_PMK_NAME_LEN);
4536 if (wpa_derive_pmk_r1_name(sm->pmk_r0_name, sm->r1kh_id, sm->own_addr,
Hai Shalom021b0b52019-04-10 11:17:58 -07004537 sm->pmk_r1_name, use_sha384) < 0) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004538 wpa_printf(MSG_WARNING, "FILS+FT: Could not derive PMKR1Name");
4539 return -1;
4540 }
Hai Shalom021b0b52019-04-10 11:17:58 -07004541 os_memcpy(pos, sm->pmk_r1_name, WPA_PMK_NAME_LEN);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004542
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004543 if (sm->mgmt_group_cipher == WPA_CIPHER_AES_128_CMAC) {
4544 /* Management Group Cipher Suite */
4545 pos = wpabuf_put(buf, RSN_SELECTOR_LEN);
4546 RSN_SELECTOR_PUT(pos, RSN_CIPHER_SUITE_AES_128_CMAC);
4547 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004548
4549 rsnie->len = ((u8 *) wpabuf_put(buf, 0) - (u8 *) rsnie) - 2;
4550 return 0;
4551}
4552#endif /* CONFIG_IEEE80211R */
4553
4554
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004555struct wpabuf * fils_build_assoc_req(struct wpa_sm *sm, const u8 **kek,
4556 size_t *kek_len, const u8 **snonce,
Paul Stewart092955c2017-02-06 09:13:09 -08004557 const u8 **anonce,
4558 const struct wpabuf **hlp,
4559 unsigned int num_hlp)
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004560{
4561 struct wpabuf *buf;
Paul Stewart092955c2017-02-06 09:13:09 -08004562 size_t len;
4563 unsigned int i;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004564
Paul Stewart092955c2017-02-06 09:13:09 -08004565 len = 1000;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004566#ifdef CONFIG_IEEE80211R
4567 if (sm->fils_ft_ies)
4568 len += wpabuf_len(sm->fils_ft_ies);
4569 if (wpa_key_mgmt_ft(sm->key_mgmt))
4570 len += 256;
4571#endif /* CONFIG_IEEE80211R */
Paul Stewart092955c2017-02-06 09:13:09 -08004572 for (i = 0; hlp && i < num_hlp; i++)
4573 len += 10 + wpabuf_len(hlp[i]);
4574 buf = wpabuf_alloc(len);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004575 if (!buf)
4576 return NULL;
4577
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004578#ifdef CONFIG_IEEE80211R
4579 if (wpa_key_mgmt_ft(sm->key_mgmt) && sm->fils_ft_ies) {
4580 /* MDE and FTE when using FILS+FT */
4581 wpabuf_put_buf(buf, sm->fils_ft_ies);
4582 /* RSNE with PMKR1Name in PMKID field */
4583 if (fils_ft_build_assoc_req_rsne(sm, buf) < 0) {
4584 wpabuf_free(buf);
4585 return NULL;
4586 }
4587 }
4588#endif /* CONFIG_IEEE80211R */
4589
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004590 /* FILS Session */
4591 wpabuf_put_u8(buf, WLAN_EID_EXTENSION); /* Element ID */
4592 wpabuf_put_u8(buf, 1 + FILS_SESSION_LEN); /* Length */
4593 /* Element ID Extension */
4594 wpabuf_put_u8(buf, WLAN_EID_EXT_FILS_SESSION);
4595 wpabuf_put_data(buf, sm->fils_session, FILS_SESSION_LEN);
4596
4597 /* Everything after FILS Session element gets encrypted in the driver
4598 * with KEK. The buffer returned from here is the plaintext version. */
4599
4600 /* TODO: FILS Public Key */
4601
4602 /* FILS Key Confirm */
4603 wpabuf_put_u8(buf, WLAN_EID_EXTENSION); /* Element ID */
4604 wpabuf_put_u8(buf, 1 + sm->fils_key_auth_len); /* Length */
4605 /* Element ID Extension */
4606 wpabuf_put_u8(buf, WLAN_EID_EXT_FILS_KEY_CONFIRM);
4607 wpabuf_put_data(buf, sm->fils_key_auth_sta, sm->fils_key_auth_len);
4608
Paul Stewart092955c2017-02-06 09:13:09 -08004609 /* FILS HLP Container */
4610 for (i = 0; hlp && i < num_hlp; i++) {
4611 const u8 *pos = wpabuf_head(hlp[i]);
4612 size_t left = wpabuf_len(hlp[i]);
4613
4614 wpabuf_put_u8(buf, WLAN_EID_EXTENSION); /* Element ID */
4615 if (left <= 254)
4616 len = 1 + left;
4617 else
4618 len = 255;
4619 wpabuf_put_u8(buf, len); /* Length */
4620 /* Element ID Extension */
4621 wpabuf_put_u8(buf, WLAN_EID_EXT_FILS_HLP_CONTAINER);
4622 /* Destination MAC Address, Source MAC Address, HLP Packet.
4623 * HLP Packet is in MSDU format (i.e., included the LLC/SNAP
4624 * header when LPD is used). */
4625 wpabuf_put_data(buf, pos, len - 1);
4626 pos += len - 1;
4627 left -= len - 1;
4628 while (left) {
4629 wpabuf_put_u8(buf, WLAN_EID_FRAGMENT);
4630 len = left > 255 ? 255 : left;
4631 wpabuf_put_u8(buf, len);
4632 wpabuf_put_data(buf, pos, len);
4633 pos += len;
4634 left -= len;
4635 }
4636 }
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004637
4638 /* TODO: FILS IP Address Assignment */
4639
Hai Shalom74f70d42019-02-11 14:42:39 -08004640#ifdef CONFIG_OCV
4641 if (wpa_sm_ocv_enabled(sm)) {
4642 struct wpa_channel_info ci;
4643 u8 *pos;
4644
4645 if (wpa_sm_channel_info(sm, &ci) != 0) {
4646 wpa_printf(MSG_WARNING,
4647 "FILS: Failed to get channel info for OCI element");
4648 wpabuf_free(buf);
4649 return NULL;
4650 }
Hai Shalom899fcc72020-10-19 14:38:18 -07004651#ifdef CONFIG_TESTING_OPTIONS
4652 if (sm->oci_freq_override_fils_assoc) {
4653 wpa_printf(MSG_INFO,
4654 "TEST: Override OCI KDE frequency %d -> %d MHz",
4655 ci.frequency,
4656 sm->oci_freq_override_fils_assoc);
4657 ci.frequency = sm->oci_freq_override_fils_assoc;
4658 }
4659#endif /* CONFIG_TESTING_OPTIONS */
Hai Shalom74f70d42019-02-11 14:42:39 -08004660
4661 pos = wpabuf_put(buf, OCV_OCI_EXTENDED_LEN);
4662 if (ocv_insert_extended_oci(&ci, pos) < 0) {
4663 wpabuf_free(buf);
4664 return NULL;
4665 }
4666 }
4667#endif /* CONFIG_OCV */
4668
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004669 wpa_hexdump_buf(MSG_DEBUG, "FILS: Association Request plaintext", buf);
4670
4671 *kek = sm->ptk.kek;
4672 *kek_len = sm->ptk.kek_len;
4673 wpa_hexdump_key(MSG_DEBUG, "FILS: KEK for AEAD", *kek, *kek_len);
4674 *snonce = sm->fils_nonce;
4675 wpa_hexdump(MSG_DEBUG, "FILS: SNonce for AEAD AAD",
4676 *snonce, FILS_NONCE_LEN);
4677 *anonce = sm->fils_anonce;
4678 wpa_hexdump(MSG_DEBUG, "FILS: ANonce for AEAD AAD",
4679 *anonce, FILS_NONCE_LEN);
4680
4681 return buf;
4682}
4683
4684
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08004685static void fils_process_hlp_resp(struct wpa_sm *sm, const u8 *resp, size_t len)
4686{
4687 const u8 *pos, *end;
4688
4689 wpa_hexdump(MSG_MSGDUMP, "FILS: HLP response", resp, len);
4690 if (len < 2 * ETH_ALEN)
4691 return;
4692 pos = resp + 2 * ETH_ALEN;
4693 end = resp + len;
4694 if (end - pos >= 6 &&
4695 os_memcmp(pos, "\xaa\xaa\x03\x00\x00\x00", 6) == 0)
4696 pos += 6; /* Remove SNAP/LLC header */
4697 wpa_sm_fils_hlp_rx(sm, resp, resp + ETH_ALEN, pos, end - pos);
4698}
4699
4700
4701static void fils_process_hlp_container(struct wpa_sm *sm, const u8 *pos,
4702 size_t len)
4703{
4704 const u8 *end = pos + len;
4705 u8 *tmp, *tmp_pos;
4706
4707 /* Check if there are any FILS HLP Container elements */
4708 while (end - pos >= 2) {
4709 if (2 + pos[1] > end - pos)
4710 return;
4711 if (pos[0] == WLAN_EID_EXTENSION &&
4712 pos[1] >= 1 + 2 * ETH_ALEN &&
4713 pos[2] == WLAN_EID_EXT_FILS_HLP_CONTAINER)
4714 break;
4715 pos += 2 + pos[1];
4716 }
4717 if (end - pos < 2)
4718 return; /* No FILS HLP Container elements */
4719
4720 tmp = os_malloc(end - pos);
4721 if (!tmp)
4722 return;
4723
4724 while (end - pos >= 2) {
4725 if (2 + pos[1] > end - pos ||
4726 pos[0] != WLAN_EID_EXTENSION ||
4727 pos[1] < 1 + 2 * ETH_ALEN ||
4728 pos[2] != WLAN_EID_EXT_FILS_HLP_CONTAINER)
4729 break;
4730 tmp_pos = tmp;
4731 os_memcpy(tmp_pos, pos + 3, pos[1] - 1);
4732 tmp_pos += pos[1] - 1;
4733 pos += 2 + pos[1];
4734
4735 /* Add possible fragments */
4736 while (end - pos >= 2 && pos[0] == WLAN_EID_FRAGMENT &&
4737 2 + pos[1] <= end - pos) {
4738 os_memcpy(tmp_pos, pos + 2, pos[1]);
4739 tmp_pos += pos[1];
4740 pos += 2 + pos[1];
4741 }
4742
4743 fils_process_hlp_resp(sm, tmp, tmp_pos - tmp);
4744 }
4745
4746 os_free(tmp);
4747}
4748
4749
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004750int fils_process_assoc_resp(struct wpa_sm *sm, const u8 *resp, size_t len)
4751{
4752 const struct ieee80211_mgmt *mgmt;
4753 const u8 *end, *ie_start;
4754 struct ieee802_11_elems elems;
4755 int keylen, rsclen;
4756 enum wpa_alg alg;
4757 struct wpa_gtk_data gd;
4758 int maxkeylen;
4759 struct wpa_eapol_ie_parse kde;
4760
4761 if (!sm || !sm->ptk_set) {
4762 wpa_printf(MSG_DEBUG, "FILS: No KEK available");
4763 return -1;
4764 }
4765
4766 if (!wpa_key_mgmt_fils(sm->key_mgmt)) {
4767 wpa_printf(MSG_DEBUG, "FILS: Not a FILS AKM");
4768 return -1;
4769 }
4770
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004771 if (sm->fils_completed) {
4772 wpa_printf(MSG_DEBUG,
4773 "FILS: Association has already been completed for this FILS authentication - ignore unexpected retransmission");
4774 return -1;
4775 }
4776
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004777 wpa_hexdump(MSG_DEBUG, "FILS: (Re)Association Response frame",
4778 resp, len);
4779
4780 mgmt = (const struct ieee80211_mgmt *) resp;
4781 if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.assoc_resp))
4782 return -1;
4783
4784 end = resp + len;
4785 /* Same offset for Association Response and Reassociation Response */
4786 ie_start = mgmt->u.assoc_resp.variable;
4787
4788 if (ieee802_11_parse_elems(ie_start, end - ie_start, &elems, 1) ==
4789 ParseFailed) {
4790 wpa_printf(MSG_DEBUG,
4791 "FILS: Failed to parse decrypted elements");
4792 goto fail;
4793 }
4794
4795 if (!elems.fils_session) {
4796 wpa_printf(MSG_DEBUG, "FILS: No FILS Session element");
4797 return -1;
4798 }
4799 if (os_memcmp(elems.fils_session, sm->fils_session,
4800 FILS_SESSION_LEN) != 0) {
4801 wpa_printf(MSG_DEBUG, "FILS: FILS Session mismatch");
4802 wpa_hexdump(MSG_DEBUG, "FILS: Received FILS Session",
4803 elems.fils_session, FILS_SESSION_LEN);
4804 wpa_hexdump(MSG_DEBUG, "FILS: Expected FILS Session",
4805 sm->fils_session, FILS_SESSION_LEN);
4806 }
4807
Hai Shalom81f62d82019-07-22 12:10:00 -07004808 if (!elems.rsn_ie) {
4809 wpa_printf(MSG_DEBUG,
4810 "FILS: No RSNE in (Re)Association Response");
4811 /* As an interop workaround, allow this for now since IEEE Std
4812 * 802.11ai-2016 did not include all the needed changes to make
4813 * a FILS AP include RSNE in the frame. This workaround might
4814 * eventually be removed and replaced with rejection (goto fail)
4815 * to follow a strict interpretation of the standard. */
4816 } else if (wpa_compare_rsn_ie(wpa_key_mgmt_ft(sm->key_mgmt),
4817 sm->ap_rsn_ie, sm->ap_rsn_ie_len,
4818 elems.rsn_ie - 2, elems.rsn_ie_len + 2)) {
4819 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
4820 "FILS: RSNE mismatch between Beacon/Probe Response and (Re)Association Response");
4821 wpa_hexdump(MSG_DEBUG, "FILS: RSNE in Beacon/Probe Response",
4822 sm->ap_rsn_ie, sm->ap_rsn_ie_len);
4823 wpa_hexdump(MSG_DEBUG, "FILS: RSNE in (Re)Association Response",
4824 elems.rsn_ie, elems.rsn_ie_len);
4825 goto fail;
4826 }
4827
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004828 /* TODO: FILS Public Key */
4829
4830 if (!elems.fils_key_confirm) {
4831 wpa_printf(MSG_DEBUG, "FILS: No FILS Key Confirm element");
4832 goto fail;
4833 }
4834 if (elems.fils_key_confirm_len != sm->fils_key_auth_len) {
4835 wpa_printf(MSG_DEBUG,
4836 "FILS: Unexpected Key-Auth length %d (expected %d)",
4837 elems.fils_key_confirm_len,
4838 (int) sm->fils_key_auth_len);
4839 goto fail;
4840 }
4841 if (os_memcmp(elems.fils_key_confirm, sm->fils_key_auth_ap,
4842 sm->fils_key_auth_len) != 0) {
4843 wpa_printf(MSG_DEBUG, "FILS: Key-Auth mismatch");
4844 wpa_hexdump(MSG_DEBUG, "FILS: Received Key-Auth",
4845 elems.fils_key_confirm,
4846 elems.fils_key_confirm_len);
4847 wpa_hexdump(MSG_DEBUG, "FILS: Expected Key-Auth",
4848 sm->fils_key_auth_ap, sm->fils_key_auth_len);
4849 goto fail;
4850 }
4851
Hai Shalom74f70d42019-02-11 14:42:39 -08004852#ifdef CONFIG_OCV
4853 if (wpa_sm_ocv_enabled(sm)) {
4854 struct wpa_channel_info ci;
4855
4856 if (wpa_sm_channel_info(sm, &ci) != 0) {
4857 wpa_printf(MSG_WARNING,
4858 "Failed to get channel info to validate received OCI in FILS (Re)Association Response frame");
4859 goto fail;
4860 }
4861
4862 if (ocv_verify_tx_params(elems.oci, elems.oci_len, &ci,
4863 channel_width_to_int(ci.chanwidth),
Hai Shalom899fcc72020-10-19 14:38:18 -07004864 ci.seg1_idx) != OCI_SUCCESS) {
4865 wpa_msg(sm->ctx->msg_ctx, MSG_INFO, OCV_FAILURE
4866 "addr=" MACSTR " frame=fils-assoc error=%s",
4867 MAC2STR(sm->bssid), ocv_errorstr);
Hai Shalom74f70d42019-02-11 14:42:39 -08004868 goto fail;
4869 }
4870 }
4871#endif /* CONFIG_OCV */
4872
Hai Shalom021b0b52019-04-10 11:17:58 -07004873#ifdef CONFIG_IEEE80211R
4874 if (wpa_key_mgmt_ft(sm->key_mgmt) && sm->fils_ft_ies) {
4875 struct wpa_ie_data rsn;
4876
4877 /* Check that PMKR1Name derived by the AP matches */
4878 if (!elems.rsn_ie ||
4879 wpa_parse_wpa_ie_rsn(elems.rsn_ie - 2, elems.rsn_ie_len + 2,
4880 &rsn) < 0 ||
4881 !rsn.pmkid || rsn.num_pmkid != 1 ||
4882 os_memcmp(rsn.pmkid, sm->pmk_r1_name,
4883 WPA_PMK_NAME_LEN) != 0) {
4884 wpa_printf(MSG_DEBUG,
4885 "FILS+FT: No RSNE[PMKR1Name] match in AssocResp");
4886 goto fail;
4887 }
4888 }
4889#endif /* CONFIG_IEEE80211R */
4890
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004891 /* Key Delivery */
4892 if (!elems.key_delivery) {
4893 wpa_printf(MSG_DEBUG, "FILS: No Key Delivery element");
4894 goto fail;
4895 }
4896
4897 /* Parse GTK and set the key to the driver */
4898 os_memset(&gd, 0, sizeof(gd));
4899 if (wpa_supplicant_parse_ies(elems.key_delivery + WPA_KEY_RSC_LEN,
4900 elems.key_delivery_len - WPA_KEY_RSC_LEN,
4901 &kde) < 0) {
4902 wpa_printf(MSG_DEBUG, "FILS: Failed to parse KDEs");
4903 goto fail;
4904 }
4905 if (!kde.gtk) {
4906 wpa_printf(MSG_DEBUG, "FILS: No GTK KDE");
4907 goto fail;
4908 }
4909 maxkeylen = gd.gtk_len = kde.gtk_len - 2;
4910 if (wpa_supplicant_check_group_cipher(sm, sm->group_cipher,
4911 gd.gtk_len, maxkeylen,
4912 &gd.key_rsc_len, &gd.alg))
4913 goto fail;
4914
4915 wpa_hexdump_key(MSG_DEBUG, "FILS: Received GTK", kde.gtk, kde.gtk_len);
4916 gd.keyidx = kde.gtk[0] & 0x3;
4917 gd.tx = wpa_supplicant_gtk_tx_bit_workaround(sm,
4918 !!(kde.gtk[0] & BIT(2)));
4919 if (kde.gtk_len - 2 > sizeof(gd.gtk)) {
4920 wpa_printf(MSG_DEBUG, "FILS: Too long GTK in GTK KDE (len=%lu)",
4921 (unsigned long) kde.gtk_len - 2);
4922 goto fail;
4923 }
4924 os_memcpy(gd.gtk, kde.gtk + 2, kde.gtk_len - 2);
4925
4926 wpa_printf(MSG_DEBUG, "FILS: Set GTK to driver");
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004927 if (wpa_supplicant_install_gtk(sm, &gd, elems.key_delivery, 0) < 0) {
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004928 wpa_printf(MSG_DEBUG, "FILS: Failed to set GTK");
4929 goto fail;
4930 }
4931
4932 if (ieee80211w_set_keys(sm, &kde) < 0) {
4933 wpa_printf(MSG_DEBUG, "FILS: Failed to set IGTK");
4934 goto fail;
4935 }
4936
4937 alg = wpa_cipher_to_alg(sm->pairwise_cipher);
4938 keylen = wpa_cipher_key_len(sm->pairwise_cipher);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004939 if (keylen <= 0 || (unsigned int) keylen != sm->ptk.tk_len) {
4940 wpa_printf(MSG_DEBUG, "FILS: TK length mismatch: %u != %lu",
4941 keylen, (long unsigned int) sm->ptk.tk_len);
4942 goto fail;
4943 }
Hai Shalomfdcde762020-04-02 11:19:20 -07004944
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004945 rsclen = wpa_cipher_rsc_len(sm->pairwise_cipher);
4946 wpa_hexdump_key(MSG_DEBUG, "FILS: Set TK to driver",
4947 sm->ptk.tk, keylen);
4948 if (wpa_sm_set_key(sm, alg, sm->bssid, 0, 1, null_rsc, rsclen,
Hai Shalomfdcde762020-04-02 11:19:20 -07004949 sm->ptk.tk, keylen, KEY_FLAG_PAIRWISE_RX_TX) < 0) {
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004950 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
4951 "FILS: Failed to set PTK to the driver (alg=%d keylen=%d bssid="
4952 MACSTR ")",
4953 alg, keylen, MAC2STR(sm->bssid));
4954 goto fail;
4955 }
4956
Hai Shalom60840252021-02-19 19:02:11 -08004957 wpa_sm_store_ptk(sm, sm->bssid, sm->pairwise_cipher,
4958 sm->dot11RSNAConfigPMKLifetime, &sm->ptk);
4959
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004960 /* TODO: TK could be cleared after auth frame exchange now that driver
4961 * takes care of association frame encryption/decryption. */
4962 /* TK is not needed anymore in supplicant */
4963 os_memset(sm->ptk.tk, 0, WPA_TK_MAX_LEN);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004964 sm->ptk.tk_len = 0;
Mathy Vanhoefc66556c2017-09-29 04:22:51 +02004965 sm->ptk.installed = 1;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004966
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08004967 /* FILS HLP Container */
4968 fils_process_hlp_container(sm, ie_start, end - ie_start);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004969
4970 /* TODO: FILS IP Address Assignment */
4971
4972 wpa_printf(MSG_DEBUG, "FILS: Auth+Assoc completed successfully");
4973 sm->fils_completed = 1;
Hai Shalom81f62d82019-07-22 12:10:00 -07004974 forced_memzero(&gd, sizeof(gd));
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004975
Hai Shalomfdcde762020-04-02 11:19:20 -07004976 if (kde.transition_disable)
4977 wpa_sm_transition_disable(sm, kde.transition_disable[0]);
4978
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004979 return 0;
4980fail:
Hai Shalom81f62d82019-07-22 12:10:00 -07004981 forced_memzero(&gd, sizeof(gd));
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004982 return -1;
4983}
4984
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004985
4986void wpa_sm_set_reset_fils_completed(struct wpa_sm *sm, int set)
4987{
4988 if (sm)
4989 sm->fils_completed = !!set;
4990}
4991
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004992#endif /* CONFIG_FILS */
4993
4994
4995int wpa_fils_is_completed(struct wpa_sm *sm)
4996{
4997#ifdef CONFIG_FILS
4998 return sm && sm->fils_completed;
4999#else /* CONFIG_FILS */
5000 return 0;
5001#endif /* CONFIG_FILS */
5002}
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005003
5004
5005#ifdef CONFIG_OWE
5006
5007struct wpabuf * owe_build_assoc_req(struct wpa_sm *sm, u16 group)
5008{
5009 struct wpabuf *ie = NULL, *pub = NULL;
5010 size_t prime_len;
5011
5012 if (group == 19)
5013 prime_len = 32;
5014 else if (group == 20)
5015 prime_len = 48;
5016 else if (group == 21)
5017 prime_len = 66;
5018 else
5019 return NULL;
5020
5021 crypto_ecdh_deinit(sm->owe_ecdh);
5022 sm->owe_ecdh = crypto_ecdh_init(group);
5023 if (!sm->owe_ecdh)
5024 goto fail;
5025 sm->owe_group = group;
5026 pub = crypto_ecdh_get_pubkey(sm->owe_ecdh, 0);
5027 pub = wpabuf_zeropad(pub, prime_len);
5028 if (!pub)
5029 goto fail;
5030
5031 ie = wpabuf_alloc(5 + wpabuf_len(pub));
5032 if (!ie)
5033 goto fail;
5034 wpabuf_put_u8(ie, WLAN_EID_EXTENSION);
5035 wpabuf_put_u8(ie, 1 + 2 + wpabuf_len(pub));
5036 wpabuf_put_u8(ie, WLAN_EID_EXT_OWE_DH_PARAM);
5037 wpabuf_put_le16(ie, group);
5038 wpabuf_put_buf(ie, pub);
5039 wpabuf_free(pub);
5040 wpa_hexdump_buf(MSG_DEBUG, "OWE: Diffie-Hellman Parameter element",
5041 ie);
5042
5043 return ie;
5044fail:
5045 wpabuf_free(pub);
5046 crypto_ecdh_deinit(sm->owe_ecdh);
5047 sm->owe_ecdh = NULL;
5048 return NULL;
5049}
5050
5051
5052int owe_process_assoc_resp(struct wpa_sm *sm, const u8 *bssid,
5053 const u8 *resp_ies, size_t resp_ies_len)
5054{
5055 struct ieee802_11_elems elems;
5056 u16 group;
5057 struct wpabuf *secret, *pub, *hkey;
5058 int res;
5059 u8 prk[SHA512_MAC_LEN], pmkid[SHA512_MAC_LEN];
5060 const char *info = "OWE Key Generation";
5061 const u8 *addr[2];
5062 size_t len[2];
5063 size_t hash_len, prime_len;
5064 struct wpa_ie_data data;
5065
5066 if (!resp_ies ||
5067 ieee802_11_parse_elems(resp_ies, resp_ies_len, &elems, 1) ==
5068 ParseFailed) {
5069 wpa_printf(MSG_INFO,
5070 "OWE: Could not parse Association Response frame elements");
5071 return -1;
5072 }
5073
5074 if (sm->cur_pmksa && elems.rsn_ie &&
5075 wpa_parse_wpa_ie_rsn(elems.rsn_ie - 2, 2 + elems.rsn_ie_len,
5076 &data) == 0 &&
5077 data.num_pmkid == 1 && data.pmkid &&
5078 os_memcmp(sm->cur_pmksa->pmkid, data.pmkid, PMKID_LEN) == 0) {
5079 wpa_printf(MSG_DEBUG, "OWE: Use PMKSA caching");
5080 wpa_sm_set_pmk_from_pmksa(sm);
5081 return 0;
5082 }
5083
5084 if (!elems.owe_dh) {
5085 wpa_printf(MSG_INFO,
5086 "OWE: No Diffie-Hellman Parameter element found in Association Response frame");
5087 return -1;
5088 }
5089
5090 group = WPA_GET_LE16(elems.owe_dh);
5091 if (group != sm->owe_group) {
5092 wpa_printf(MSG_INFO,
5093 "OWE: Unexpected Diffie-Hellman group in response: %u",
5094 group);
5095 return -1;
5096 }
5097
5098 if (!sm->owe_ecdh) {
5099 wpa_printf(MSG_INFO, "OWE: No ECDH state available");
5100 return -1;
5101 }
5102
5103 if (group == 19)
5104 prime_len = 32;
5105 else if (group == 20)
5106 prime_len = 48;
5107 else if (group == 21)
5108 prime_len = 66;
5109 else
5110 return -1;
5111
5112 secret = crypto_ecdh_set_peerkey(sm->owe_ecdh, 0,
5113 elems.owe_dh + 2,
5114 elems.owe_dh_len - 2);
5115 secret = wpabuf_zeropad(secret, prime_len);
5116 if (!secret) {
5117 wpa_printf(MSG_DEBUG, "OWE: Invalid peer DH public key");
5118 return -1;
5119 }
5120 wpa_hexdump_buf_key(MSG_DEBUG, "OWE: DH shared secret", secret);
5121
5122 /* prk = HKDF-extract(C | A | group, z) */
5123
5124 pub = crypto_ecdh_get_pubkey(sm->owe_ecdh, 0);
5125 if (!pub) {
5126 wpabuf_clear_free(secret);
5127 return -1;
5128 }
5129
5130 /* PMKID = Truncate-128(Hash(C | A)) */
5131 addr[0] = wpabuf_head(pub);
5132 len[0] = wpabuf_len(pub);
5133 addr[1] = elems.owe_dh + 2;
5134 len[1] = elems.owe_dh_len - 2;
5135 if (group == 19) {
5136 res = sha256_vector(2, addr, len, pmkid);
5137 hash_len = SHA256_MAC_LEN;
5138 } else if (group == 20) {
5139 res = sha384_vector(2, addr, len, pmkid);
5140 hash_len = SHA384_MAC_LEN;
5141 } else if (group == 21) {
5142 res = sha512_vector(2, addr, len, pmkid);
5143 hash_len = SHA512_MAC_LEN;
5144 } else {
5145 res = -1;
5146 hash_len = 0;
5147 }
5148 pub = wpabuf_zeropad(pub, prime_len);
5149 if (res < 0 || !pub) {
5150 wpabuf_free(pub);
5151 wpabuf_clear_free(secret);
5152 return -1;
5153 }
5154
5155 hkey = wpabuf_alloc(wpabuf_len(pub) + elems.owe_dh_len - 2 + 2);
5156 if (!hkey) {
5157 wpabuf_free(pub);
5158 wpabuf_clear_free(secret);
5159 return -1;
5160 }
5161
5162 wpabuf_put_buf(hkey, pub); /* C */
5163 wpabuf_free(pub);
5164 wpabuf_put_data(hkey, elems.owe_dh + 2, elems.owe_dh_len - 2); /* A */
5165 wpabuf_put_le16(hkey, sm->owe_group); /* group */
5166 if (group == 19)
5167 res = hmac_sha256(wpabuf_head(hkey), wpabuf_len(hkey),
5168 wpabuf_head(secret), wpabuf_len(secret), prk);
5169 else if (group == 20)
5170 res = hmac_sha384(wpabuf_head(hkey), wpabuf_len(hkey),
5171 wpabuf_head(secret), wpabuf_len(secret), prk);
5172 else if (group == 21)
5173 res = hmac_sha512(wpabuf_head(hkey), wpabuf_len(hkey),
5174 wpabuf_head(secret), wpabuf_len(secret), prk);
5175 wpabuf_clear_free(hkey);
5176 wpabuf_clear_free(secret);
5177 if (res < 0)
5178 return -1;
5179
5180 wpa_hexdump_key(MSG_DEBUG, "OWE: prk", prk, hash_len);
5181
5182 /* PMK = HKDF-expand(prk, "OWE Key Generation", n) */
5183
5184 if (group == 19)
5185 res = hmac_sha256_kdf(prk, hash_len, NULL, (const u8 *) info,
5186 os_strlen(info), sm->pmk, hash_len);
5187 else if (group == 20)
5188 res = hmac_sha384_kdf(prk, hash_len, NULL, (const u8 *) info,
5189 os_strlen(info), sm->pmk, hash_len);
5190 else if (group == 21)
5191 res = hmac_sha512_kdf(prk, hash_len, NULL, (const u8 *) info,
5192 os_strlen(info), sm->pmk, hash_len);
Hai Shalom81f62d82019-07-22 12:10:00 -07005193 forced_memzero(prk, SHA512_MAC_LEN);
Roshan Pius3a1667e2018-07-03 15:17:14 -07005194 if (res < 0) {
5195 sm->pmk_len = 0;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005196 return -1;
Roshan Pius3a1667e2018-07-03 15:17:14 -07005197 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005198 sm->pmk_len = hash_len;
5199
5200 wpa_hexdump_key(MSG_DEBUG, "OWE: PMK", sm->pmk, sm->pmk_len);
5201 wpa_hexdump(MSG_DEBUG, "OWE: PMKID", pmkid, PMKID_LEN);
5202 pmksa_cache_add(sm->pmksa, sm->pmk, sm->pmk_len, pmkid, NULL, 0,
5203 bssid, sm->own_addr, sm->network_ctx, sm->key_mgmt,
5204 NULL);
5205
5206 return 0;
5207}
5208
5209#endif /* CONFIG_OWE */
5210
5211
5212void wpa_sm_set_fils_cache_id(struct wpa_sm *sm, const u8 *fils_cache_id)
5213{
5214#ifdef CONFIG_FILS
5215 if (sm && fils_cache_id) {
5216 sm->fils_cache_id_set = 1;
5217 os_memcpy(sm->fils_cache_id, fils_cache_id, FILS_CACHE_ID_LEN);
5218 }
5219#endif /* CONFIG_FILS */
5220}
Hai Shalom021b0b52019-04-10 11:17:58 -07005221
5222
5223#ifdef CONFIG_DPP2
5224void wpa_sm_set_dpp_z(struct wpa_sm *sm, const struct wpabuf *z)
5225{
5226 if (sm) {
5227 wpabuf_clear_free(sm->dpp_z);
5228 sm->dpp_z = z ? wpabuf_dup(z) : NULL;
5229 }
5230}
5231#endif /* CONFIG_DPP2 */
Hai Shalom60840252021-02-19 19:02:11 -08005232
5233
5234#ifdef CONFIG_PASN
5235void wpa_pasn_pmksa_cache_add(struct wpa_sm *sm, const u8 *pmk, size_t pmk_len,
5236 const u8 *pmkid, const u8 *bssid, int key_mgmt)
5237{
5238 sm->cur_pmksa = pmksa_cache_add(sm->pmksa, pmk, pmk_len, pmkid, NULL, 0,
5239 bssid, sm->own_addr, NULL,
5240 key_mgmt, 0);
5241}
5242#endif /* CONFIG_PASN */