blob: d028e95d347cb5bcf8f3d4eedd9402c05f11d63c [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;
582 size_t z_len = 0;
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 */
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800606 return wpa_pmk_to_ptk(sm->pmk, sm->pmk_len, "Pairwise key expansion",
607 sm->own_addr, sm->bssid, sm->snonce,
Hai Shalomfdcde762020-04-02 11:19:20 -0700608 key->key_nonce, ptk, akmp,
Hai Shalom021b0b52019-04-10 11:17:58 -0700609 sm->pairwise_cipher, z, z_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700610}
611
612
Hai Shalomfdcde762020-04-02 11:19:20 -0700613static int wpa_handle_ext_key_id(struct wpa_sm *sm,
614 struct wpa_eapol_ie_parse *kde)
615{
616 if (sm->ext_key_id) {
617 u16 key_id;
618
619 if (!kde->key_id) {
620 wpa_msg(sm->ctx->msg_ctx,
621 sm->use_ext_key_id ? MSG_INFO : MSG_DEBUG,
622 "RSN: No Key ID in Extended Key ID handshake");
623 sm->keyidx_active = 0;
624 return sm->use_ext_key_id ? -1 : 0;
625 }
626
627 key_id = kde->key_id[0] & 0x03;
628 if (key_id > 1) {
629 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
630 "RSN: Invalid Extended Key ID: %d", key_id);
631 return -1;
632 }
633 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
634 "RSN: Using Extended Key ID %d", key_id);
635 sm->keyidx_active = key_id;
636 sm->use_ext_key_id = 1;
637 } else {
638 if (kde->key_id && (kde->key_id[0] & 0x03)) {
639 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
640 "RSN: Non-zero Extended Key ID Key ID in PTK0 handshake");
641 return -1;
642 }
643
644 if (kde->key_id) {
645 /* This is not supposed to be included here, but ignore
646 * the case of matching Key ID 0 just in case. */
647 wpa_msg(sm->ctx->msg_ctx, MSG_DEBUG,
648 "RSN: Extended Key ID Key ID 0 in PTK0 handshake");
649 }
650 sm->keyidx_active = 0;
651 sm->use_ext_key_id = 0;
652 }
653
654 return 0;
655}
656
657
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700658static void wpa_supplicant_process_1_of_4(struct wpa_sm *sm,
659 const unsigned char *src_addr,
660 const struct wpa_eapol_key *key,
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700661 u16 ver, const u8 *key_data,
662 size_t key_data_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700663{
664 struct wpa_eapol_ie_parse ie;
665 struct wpa_ptk *ptk;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700666 int res;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800667 u8 *kde, *kde_buf = NULL;
668 size_t kde_len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700669
670 if (wpa_sm_get_network_ctx(sm) == NULL) {
671 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, "WPA: No SSID info "
672 "found (msg 1 of 4)");
673 return;
674 }
675
Hai Shalomfdcde762020-04-02 11:19:20 -0700676 if (sm->wpa_deny_ptk0_rekey && !sm->use_ext_key_id &&
677 wpa_sm_get_state(sm) == WPA_COMPLETED) {
678 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
679 "WPA: PTK0 rekey not allowed, reconnecting");
680 wpa_sm_reconnect(sm);
681 return;
682 }
683
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700684 wpa_sm_set_state(sm, WPA_4WAY_HANDSHAKE);
Roshan Pius5e7db942018-04-12 12:27:41 -0700685 wpa_dbg(sm->ctx->msg_ctx, MSG_INFO, "WPA: RX message 1 of 4-Way "
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700686 "Handshake from " MACSTR " (ver=%d)", MAC2STR(src_addr), ver);
687
688 os_memset(&ie, 0, sizeof(ie));
689
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800690 if (sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700691 /* RSN: msg 1/4 should contain PMKID for the selected PMK */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700692 wpa_hexdump(MSG_DEBUG, "RSN: msg 1/4 key data",
693 key_data, key_data_len);
694 if (wpa_supplicant_parse_ies(key_data, key_data_len, &ie) < 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800695 goto failed;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700696 if (ie.pmkid) {
697 wpa_hexdump(MSG_DEBUG, "RSN: PMKID from "
698 "Authenticator", ie.pmkid, PMKID_LEN);
699 }
700 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700701
702 res = wpa_supplicant_get_pmk(sm, src_addr, ie.pmkid);
703 if (res == -2) {
704 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "RSN: Do not reply to "
705 "msg 1/4 - requesting full EAP authentication");
706 return;
707 }
708 if (res)
709 goto failed;
710
711 if (sm->renew_snonce) {
712 if (random_get_bytes(sm->snonce, WPA_NONCE_LEN)) {
713 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
714 "WPA: Failed to get random data for SNonce");
715 goto failed;
716 }
717 sm->renew_snonce = 0;
718 wpa_hexdump(MSG_DEBUG, "WPA: Renewed SNonce",
719 sm->snonce, WPA_NONCE_LEN);
720 }
721
722 /* Calculate PTK which will be stored as a temporary PTK until it has
723 * been verified when processing message 3/4. */
724 ptk = &sm->tptk;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700725 if (wpa_derive_ptk(sm, src_addr, key, ptk) < 0)
726 goto failed;
Dmitry Shmidt98660862014-03-11 17:26:21 -0700727 if (sm->pairwise_cipher == WPA_CIPHER_TKIP) {
Dmitry Shmidt61593f02014-04-21 16:27:35 -0700728 u8 buf[8];
Dmitry Shmidt98660862014-03-11 17:26:21 -0700729 /* Supplicant: swap tx/rx Mic keys */
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800730 os_memcpy(buf, &ptk->tk[16], 8);
731 os_memcpy(&ptk->tk[16], &ptk->tk[24], 8);
732 os_memcpy(&ptk->tk[24], buf, 8);
Hai Shalom81f62d82019-07-22 12:10:00 -0700733 forced_memzero(buf, sizeof(buf));
Dmitry Shmidt98660862014-03-11 17:26:21 -0700734 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700735 sm->tptk_set = 1;
736
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800737 kde = sm->assoc_wpa_ie;
738 kde_len = sm->assoc_wpa_ie_len;
Hai Shalomc3565922019-10-28 11:58:20 -0700739 kde_buf = os_malloc(kde_len +
740 2 + RSN_SELECTOR_LEN + 3 +
741 sm->assoc_rsnxe_len +
Hai Shalom4fbc08f2020-05-18 12:37:00 -0700742 2 + RSN_SELECTOR_LEN + 1 +
743 2 + RSN_SELECTOR_LEN + 2);
Hai Shalomc3565922019-10-28 11:58:20 -0700744 if (!kde_buf)
745 goto failed;
746 os_memcpy(kde_buf, kde, kde_len);
747 kde = kde_buf;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800748
Hai Shalom74f70d42019-02-11 14:42:39 -0800749#ifdef CONFIG_OCV
750 if (wpa_sm_ocv_enabled(sm)) {
751 struct wpa_channel_info ci;
752 u8 *pos;
753
Hai Shalomc3565922019-10-28 11:58:20 -0700754 pos = kde + kde_len;
Hai Shalom74f70d42019-02-11 14:42:39 -0800755 if (wpa_sm_channel_info(sm, &ci) != 0) {
756 wpa_printf(MSG_WARNING,
757 "Failed to get channel info for OCI element in EAPOL-Key 2/4");
758 goto failed;
759 }
Hai Shalom899fcc72020-10-19 14:38:18 -0700760#ifdef CONFIG_TESTING_OPTIONS
761 if (sm->oci_freq_override_eapol) {
762 wpa_printf(MSG_INFO,
763 "TEST: Override OCI KDE frequency %d -> %d MHz",
764 ci.frequency, sm->oci_freq_override_eapol);
765 ci.frequency = sm->oci_freq_override_eapol;
766 }
767#endif /* CONFIG_TESTING_OPTIONS */
Hai Shalom74f70d42019-02-11 14:42:39 -0800768
Hai Shalom74f70d42019-02-11 14:42:39 -0800769 if (ocv_insert_oci_kde(&ci, &pos) < 0)
770 goto failed;
771 kde_len = pos - kde;
772 }
773#endif /* CONFIG_OCV */
774
Hai Shalomc3565922019-10-28 11:58:20 -0700775 if (sm->assoc_rsnxe && sm->assoc_rsnxe_len) {
776 os_memcpy(kde + kde_len, sm->assoc_rsnxe, sm->assoc_rsnxe_len);
777 kde_len += sm->assoc_rsnxe_len;
778 }
779
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800780#ifdef CONFIG_P2P
781 if (sm->p2p) {
Hai Shalomc3565922019-10-28 11:58:20 -0700782 u8 *pos;
783
784 wpa_printf(MSG_DEBUG,
785 "P2P: Add IP Address Request KDE into EAPOL-Key 2/4");
786 pos = kde + kde_len;
787 *pos++ = WLAN_EID_VENDOR_SPECIFIC;
788 *pos++ = RSN_SELECTOR_LEN + 1;
789 RSN_SELECTOR_PUT(pos, WFA_KEY_DATA_IP_ADDR_REQ);
790 pos += RSN_SELECTOR_LEN;
791 *pos++ = 0x01;
792 kde_len = pos - kde;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800793 }
794#endif /* CONFIG_P2P */
795
Hai Shalom4fbc08f2020-05-18 12:37:00 -0700796#ifdef CONFIG_DPP2
797 if (DPP_VERSION > 1 && sm->key_mgmt == WPA_KEY_MGMT_DPP) {
798 u8 *pos;
799
800 wpa_printf(MSG_DEBUG, "DPP: Add DPP KDE into EAPOL-Key 2/4");
801 pos = kde + kde_len;
802 *pos++ = WLAN_EID_VENDOR_SPECIFIC;
803 *pos++ = RSN_SELECTOR_LEN + 2;
804 RSN_SELECTOR_PUT(pos, WFA_KEY_DATA_DPP);
805 pos += RSN_SELECTOR_LEN;
806 *pos++ = DPP_VERSION; /* Protocol Version */
807 *pos = 0; /* Flags */
808 if (sm->dpp_pfs == 0)
809 *pos |= DPP_KDE_PFS_ALLOWED;
810 else if (sm->dpp_pfs == 1)
811 *pos |= DPP_KDE_PFS_ALLOWED | DPP_KDE_PFS_REQUIRED;
812 pos++;
813 kde_len = pos - kde;
814 }
815#endif /* CONFIG_DPP2 */
816
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700817 if (wpa_supplicant_send_2_of_4(sm, sm->bssid, key, ver, sm->snonce,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800818 kde, kde_len, ptk) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700819 goto failed;
820
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800821 os_free(kde_buf);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700822 os_memcpy(sm->anonce, key->key_nonce, WPA_NONCE_LEN);
823 return;
824
825failed:
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800826 os_free(kde_buf);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700827 wpa_sm_deauthenticate(sm, WLAN_REASON_UNSPECIFIED);
828}
829
830
831static void wpa_sm_start_preauth(void *eloop_ctx, void *timeout_ctx)
832{
833 struct wpa_sm *sm = eloop_ctx;
834 rsn_preauth_candidate_process(sm);
835}
836
837
838static void wpa_supplicant_key_neg_complete(struct wpa_sm *sm,
839 const u8 *addr, int secure)
840{
841 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
842 "WPA: Key negotiation completed with "
843 MACSTR " [PTK=%s GTK=%s]", MAC2STR(addr),
844 wpa_cipher_txt(sm->pairwise_cipher),
845 wpa_cipher_txt(sm->group_cipher));
846 wpa_sm_cancel_auth_timeout(sm);
847 wpa_sm_set_state(sm, WPA_COMPLETED);
848
849 if (secure) {
850 wpa_sm_mlme_setprotection(
851 sm, addr, MLME_SETPROTECTION_PROTECT_TYPE_RX_TX,
852 MLME_SETPROTECTION_KEY_TYPE_PAIRWISE);
Hai Shalome21d4e82020-04-29 16:34:06 -0700853 eapol_sm_notify_portValid(sm->eapol, true);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700854 if (wpa_key_mgmt_wpa_psk(sm->key_mgmt) ||
855 sm->key_mgmt == WPA_KEY_MGMT_DPP ||
856 sm->key_mgmt == WPA_KEY_MGMT_OWE)
Hai Shalome21d4e82020-04-29 16:34:06 -0700857 eapol_sm_notify_eap_success(sm->eapol, true);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700858 /*
859 * Start preauthentication after a short wait to avoid a
860 * possible race condition between the data receive and key
861 * configuration after the 4-Way Handshake. This increases the
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800862 * likelihood of the first preauth EAPOL-Start frame getting to
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700863 * the target AP.
864 */
Hai Shalom74f70d42019-02-11 14:42:39 -0800865 if (!dl_list_empty(&sm->pmksa_candidates))
866 eloop_register_timeout(1, 0, wpa_sm_start_preauth,
867 sm, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700868 }
869
870 if (sm->cur_pmksa && sm->cur_pmksa->opportunistic) {
871 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
872 "RSN: Authenticator accepted "
873 "opportunistic PMKSA entry - marking it valid");
874 sm->cur_pmksa->opportunistic = 0;
875 }
876
877#ifdef CONFIG_IEEE80211R
878 if (wpa_key_mgmt_ft(sm->key_mgmt)) {
879 /* Prepare for the next transition */
880 wpa_ft_prepare_auth_request(sm, NULL);
881 }
882#endif /* CONFIG_IEEE80211R */
883}
884
885
886static void wpa_sm_rekey_ptk(void *eloop_ctx, void *timeout_ctx)
887{
888 struct wpa_sm *sm = eloop_ctx;
889 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Request PTK rekeying");
890 wpa_sm_key_request(sm, 0, 1);
891}
892
893
894static int wpa_supplicant_install_ptk(struct wpa_sm *sm,
Hai Shalomfdcde762020-04-02 11:19:20 -0700895 const struct wpa_eapol_key *key,
896 enum key_flag key_flag)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700897{
898 int keylen, rsclen;
899 enum wpa_alg alg;
900 const u8 *key_rsc;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800901
Mathy Vanhoefc66556c2017-09-29 04:22:51 +0200902 if (sm->ptk.installed) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800903 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
904 "WPA: Do not re-install same PTK to the driver");
905 return 0;
906 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700907
908 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
909 "WPA: Installing PTK to the driver");
910
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700911 if (sm->pairwise_cipher == WPA_CIPHER_NONE) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700912 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Pairwise Cipher "
913 "Suite: NONE - do not use pairwise keys");
914 return 0;
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700915 }
916
917 if (!wpa_cipher_valid_pairwise(sm->pairwise_cipher)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700918 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
919 "WPA: Unsupported pairwise cipher %d",
920 sm->pairwise_cipher);
921 return -1;
922 }
923
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700924 alg = wpa_cipher_to_alg(sm->pairwise_cipher);
925 keylen = wpa_cipher_key_len(sm->pairwise_cipher);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700926 if (keylen <= 0 || (unsigned int) keylen != sm->ptk.tk_len) {
927 wpa_printf(MSG_DEBUG, "WPA: TK length mismatch: %d != %lu",
928 keylen, (long unsigned int) sm->ptk.tk_len);
929 return -1;
930 }
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700931 rsclen = wpa_cipher_rsc_len(sm->pairwise_cipher);
932
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800933 if (sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700934 key_rsc = null_rsc;
935 } else {
936 key_rsc = key->key_rsc;
937 wpa_hexdump(MSG_DEBUG, "WPA: RSC", key_rsc, rsclen);
938 }
939
Hai Shalomfdcde762020-04-02 11:19:20 -0700940 if (wpa_sm_set_key(sm, alg, sm->bssid, sm->keyidx_active, 1, key_rsc,
941 rsclen, sm->ptk.tk, keylen,
942 KEY_FLAG_PAIRWISE | key_flag) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700943 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
Hai Shalomfdcde762020-04-02 11:19:20 -0700944 "WPA: Failed to set PTK to the driver (alg=%d keylen=%d bssid="
945 MACSTR " idx=%d key_flag=0x%x)",
946 alg, keylen, MAC2STR(sm->bssid),
947 sm->keyidx_active, key_flag);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700948 return -1;
949 }
950
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800951 /* TK is not needed anymore in supplicant */
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800952 os_memset(sm->ptk.tk, 0, WPA_TK_MAX_LEN);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700953 sm->ptk.tk_len = 0;
Mathy Vanhoefc66556c2017-09-29 04:22:51 +0200954 sm->ptk.installed = 1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800955
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700956 if (sm->wpa_ptk_rekey) {
957 eloop_cancel_timeout(wpa_sm_rekey_ptk, sm, NULL);
958 eloop_register_timeout(sm->wpa_ptk_rekey, 0, wpa_sm_rekey_ptk,
959 sm, NULL);
960 }
Hai Shalomfdcde762020-04-02 11:19:20 -0700961 return 0;
962}
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700963
Hai Shalomfdcde762020-04-02 11:19:20 -0700964
965static int wpa_supplicant_activate_ptk(struct wpa_sm *sm)
966{
967 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
968 "WPA: Activate PTK (idx=%d bssid=" MACSTR ")",
969 sm->keyidx_active, MAC2STR(sm->bssid));
970
971 if (wpa_sm_set_key(sm, 0, sm->bssid, sm->keyidx_active, 0, NULL, 0,
972 NULL, 0, KEY_FLAG_PAIRWISE_RX_TX_MODIFY) < 0) {
973 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
974 "WPA: Failed to activate PTK for TX (idx=%d bssid="
975 MACSTR ")", sm->keyidx_active, MAC2STR(sm->bssid));
976 return -1;
977 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700978 return 0;
979}
980
981
982static int wpa_supplicant_check_group_cipher(struct wpa_sm *sm,
983 int group_cipher,
984 int keylen, int maxkeylen,
985 int *key_rsc_len,
986 enum wpa_alg *alg)
987{
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700988 int klen;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700989
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700990 *alg = wpa_cipher_to_alg(group_cipher);
991 if (*alg == WPA_ALG_NONE) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700992 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
993 "WPA: Unsupported Group Cipher %d",
994 group_cipher);
995 return -1;
996 }
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700997 *key_rsc_len = wpa_cipher_rsc_len(group_cipher);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700998
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700999 klen = wpa_cipher_key_len(group_cipher);
1000 if (keylen != klen || maxkeylen < klen) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001001 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1002 "WPA: Unsupported %s Group Cipher key length %d (%d)",
1003 wpa_cipher_txt(group_cipher), keylen, maxkeylen);
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07001004 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001005 }
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07001006 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001007}
1008
1009
1010struct wpa_gtk_data {
1011 enum wpa_alg alg;
1012 int tx, key_rsc_len, keyidx;
1013 u8 gtk[32];
1014 int gtk_len;
1015};
1016
1017
1018static int wpa_supplicant_install_gtk(struct wpa_sm *sm,
1019 const struct wpa_gtk_data *gd,
Jouni Malinen58c0e962017-10-01 12:12:24 +03001020 const u8 *key_rsc, int wnm_sleep)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001021{
1022 const u8 *_gtk = gd->gtk;
1023 u8 gtk_buf[32];
1024
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02001025 /* Detect possible key reinstallation */
Jouni Malinen58c0e962017-10-01 12:12:24 +03001026 if ((sm->gtk.gtk_len == (size_t) gd->gtk_len &&
1027 os_memcmp(sm->gtk.gtk, gd->gtk, sm->gtk.gtk_len) == 0) ||
1028 (sm->gtk_wnm_sleep.gtk_len == (size_t) gd->gtk_len &&
1029 os_memcmp(sm->gtk_wnm_sleep.gtk, gd->gtk,
1030 sm->gtk_wnm_sleep.gtk_len) == 0)) {
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02001031 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1032 "WPA: Not reinstalling already in-use GTK to the driver (keyidx=%d tx=%d len=%d)",
1033 gd->keyidx, gd->tx, gd->gtk_len);
1034 return 0;
1035 }
1036
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001037 wpa_hexdump_key(MSG_DEBUG, "WPA: Group Key", gd->gtk, gd->gtk_len);
1038 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1039 "WPA: Installing GTK to the driver (keyidx=%d tx=%d len=%d)",
1040 gd->keyidx, gd->tx, gd->gtk_len);
1041 wpa_hexdump(MSG_DEBUG, "WPA: RSC", key_rsc, gd->key_rsc_len);
1042 if (sm->group_cipher == WPA_CIPHER_TKIP) {
1043 /* Swap Tx/Rx keys for Michael MIC */
1044 os_memcpy(gtk_buf, gd->gtk, 16);
1045 os_memcpy(gtk_buf + 16, gd->gtk + 24, 8);
1046 os_memcpy(gtk_buf + 24, gd->gtk + 16, 8);
1047 _gtk = gtk_buf;
1048 }
1049 if (sm->pairwise_cipher == WPA_CIPHER_NONE) {
1050 if (wpa_sm_set_key(sm, gd->alg, NULL,
1051 gd->keyidx, 1, key_rsc, gd->key_rsc_len,
Hai Shalomfdcde762020-04-02 11:19:20 -07001052 _gtk, gd->gtk_len,
1053 KEY_FLAG_GROUP_RX_TX_DEFAULT) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001054 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1055 "WPA: Failed to set GTK to the driver "
1056 "(Group only)");
Hai Shalom81f62d82019-07-22 12:10:00 -07001057 forced_memzero(gtk_buf, sizeof(gtk_buf));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001058 return -1;
1059 }
1060 } else if (wpa_sm_set_key(sm, gd->alg, broadcast_ether_addr,
1061 gd->keyidx, gd->tx, key_rsc, gd->key_rsc_len,
Hai Shalomfdcde762020-04-02 11:19:20 -07001062 _gtk, gd->gtk_len, KEY_FLAG_GROUP_RX) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001063 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1064 "WPA: Failed to set GTK to "
1065 "the driver (alg=%d keylen=%d keyidx=%d)",
1066 gd->alg, gd->gtk_len, gd->keyidx);
Hai Shalom81f62d82019-07-22 12:10:00 -07001067 forced_memzero(gtk_buf, sizeof(gtk_buf));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001068 return -1;
1069 }
Hai Shalom81f62d82019-07-22 12:10:00 -07001070 forced_memzero(gtk_buf, sizeof(gtk_buf));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001071
Jouni Malinen58c0e962017-10-01 12:12:24 +03001072 if (wnm_sleep) {
1073 sm->gtk_wnm_sleep.gtk_len = gd->gtk_len;
1074 os_memcpy(sm->gtk_wnm_sleep.gtk, gd->gtk,
1075 sm->gtk_wnm_sleep.gtk_len);
1076 } else {
1077 sm->gtk.gtk_len = gd->gtk_len;
1078 os_memcpy(sm->gtk.gtk, gd->gtk, sm->gtk.gtk_len);
1079 }
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02001080
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001081 return 0;
1082}
1083
1084
1085static int wpa_supplicant_gtk_tx_bit_workaround(const struct wpa_sm *sm,
1086 int tx)
1087{
1088 if (tx && sm->pairwise_cipher != WPA_CIPHER_NONE) {
1089 /* Ignore Tx bit for GTK if a pairwise key is used. One AP
1090 * seemed to set this bit (incorrectly, since Tx is only when
1091 * doing Group Key only APs) and without this workaround, the
1092 * data connection does not work because wpa_supplicant
1093 * configured non-zero keyidx to be used for unicast. */
1094 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1095 "WPA: Tx bit set for GTK, but pairwise "
1096 "keys are used - ignore Tx bit");
1097 return 0;
1098 }
1099 return tx;
1100}
1101
1102
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001103static int wpa_supplicant_rsc_relaxation(const struct wpa_sm *sm,
1104 const u8 *rsc)
1105{
1106 int rsclen;
1107
1108 if (!sm->wpa_rsc_relaxation)
1109 return 0;
1110
1111 rsclen = wpa_cipher_rsc_len(sm->group_cipher);
1112
1113 /*
1114 * Try to detect RSC (endian) corruption issue where the AP sends
1115 * the RSC bytes in EAPOL-Key message in the wrong order, both if
1116 * it's actually a 6-byte field (as it should be) and if it treats
1117 * it as an 8-byte field.
1118 * An AP model known to have this bug is the Sapido RB-1632.
1119 */
1120 if (rsclen == 6 && ((rsc[5] && !rsc[0]) || rsc[6] || rsc[7])) {
1121 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1122 "RSC %02x%02x%02x%02x%02x%02x%02x%02x is likely bogus, using 0",
1123 rsc[0], rsc[1], rsc[2], rsc[3],
1124 rsc[4], rsc[5], rsc[6], rsc[7]);
1125
1126 return 1;
1127 }
1128
1129 return 0;
1130}
1131
1132
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001133static int wpa_supplicant_pairwise_gtk(struct wpa_sm *sm,
1134 const struct wpa_eapol_key *key,
1135 const u8 *gtk, size_t gtk_len,
1136 int key_info)
1137{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001138 struct wpa_gtk_data gd;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001139 const u8 *key_rsc;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001140
1141 /*
1142 * IEEE Std 802.11i-2004 - 8.5.2 EAPOL-Key frames - Figure 43x
1143 * GTK KDE format:
1144 * KeyID[bits 0-1], Tx [bit 2], Reserved [bits 3-7]
1145 * Reserved [bits 0-7]
1146 * GTK
1147 */
1148
1149 os_memset(&gd, 0, sizeof(gd));
1150 wpa_hexdump_key(MSG_DEBUG, "RSN: received GTK in pairwise handshake",
1151 gtk, gtk_len);
1152
1153 if (gtk_len < 2 || gtk_len - 2 > sizeof(gd.gtk))
1154 return -1;
1155
1156 gd.keyidx = gtk[0] & 0x3;
1157 gd.tx = wpa_supplicant_gtk_tx_bit_workaround(sm,
1158 !!(gtk[0] & BIT(2)));
1159 gtk += 2;
1160 gtk_len -= 2;
1161
1162 os_memcpy(gd.gtk, gtk, gtk_len);
1163 gd.gtk_len = gtk_len;
1164
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001165 key_rsc = key->key_rsc;
1166 if (wpa_supplicant_rsc_relaxation(sm, key->key_rsc))
1167 key_rsc = null_rsc;
1168
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001169 if (sm->group_cipher != WPA_CIPHER_GTK_NOT_USED &&
1170 (wpa_supplicant_check_group_cipher(sm, sm->group_cipher,
1171 gtk_len, gtk_len,
1172 &gd.key_rsc_len, &gd.alg) ||
Jouni Malinen58c0e962017-10-01 12:12:24 +03001173 wpa_supplicant_install_gtk(sm, &gd, key_rsc, 0))) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001174 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1175 "RSN: Failed to install GTK");
Hai Shalom81f62d82019-07-22 12:10:00 -07001176 forced_memzero(&gd, sizeof(gd));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001177 return -1;
1178 }
Hai Shalom81f62d82019-07-22 12:10:00 -07001179 forced_memzero(&gd, sizeof(gd));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001180
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001181 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001182}
1183
1184
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02001185static int wpa_supplicant_install_igtk(struct wpa_sm *sm,
Jouni Malinen58c0e962017-10-01 12:12:24 +03001186 const struct wpa_igtk_kde *igtk,
1187 int wnm_sleep)
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02001188{
1189 size_t len = wpa_cipher_key_len(sm->mgmt_group_cipher);
1190 u16 keyidx = WPA_GET_LE16(igtk->keyid);
1191
1192 /* Detect possible key reinstallation */
Jouni Malinen58c0e962017-10-01 12:12:24 +03001193 if ((sm->igtk.igtk_len == len &&
1194 os_memcmp(sm->igtk.igtk, igtk->igtk, sm->igtk.igtk_len) == 0) ||
1195 (sm->igtk_wnm_sleep.igtk_len == len &&
1196 os_memcmp(sm->igtk_wnm_sleep.igtk, igtk->igtk,
1197 sm->igtk_wnm_sleep.igtk_len) == 0)) {
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02001198 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1199 "WPA: Not reinstalling already in-use IGTK to the driver (keyidx=%d)",
1200 keyidx);
1201 return 0;
1202 }
1203
1204 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
Hai Shalom39ba6fc2019-01-22 12:40:38 -08001205 "WPA: IGTK keyid %d pn " COMPACT_MACSTR,
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02001206 keyidx, MAC2STR(igtk->pn));
1207 wpa_hexdump_key(MSG_DEBUG, "WPA: IGTK", igtk->igtk, len);
1208 if (keyidx > 4095) {
1209 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1210 "WPA: Invalid IGTK KeyID %d", keyidx);
1211 return -1;
1212 }
1213 if (wpa_sm_set_key(sm, wpa_cipher_to_alg(sm->mgmt_group_cipher),
1214 broadcast_ether_addr,
1215 keyidx, 0, igtk->pn, sizeof(igtk->pn),
Hai Shalomfdcde762020-04-02 11:19:20 -07001216 igtk->igtk, len, KEY_FLAG_GROUP_RX) < 0) {
Hai Shalom5f92bc92019-04-18 11:54:11 -07001217 if (keyidx == 0x0400 || keyidx == 0x0500) {
1218 /* Assume the AP has broken PMF implementation since it
1219 * seems to have swapped the KeyID bytes. The AP cannot
1220 * be trusted to implement BIP correctly or provide a
1221 * valid IGTK, so do not try to configure this key with
1222 * swapped KeyID bytes. Instead, continue without
1223 * configuring the IGTK so that the driver can drop any
1224 * received group-addressed robust management frames due
1225 * to missing keys.
1226 *
1227 * Normally, this error behavior would result in us
1228 * disconnecting, but there are number of deployed APs
1229 * with this broken behavior, so as an interoperability
1230 * workaround, allow the connection to proceed. */
1231 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1232 "WPA: Ignore IGTK configuration error due to invalid IGTK KeyID byte order");
1233 } else {
1234 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1235 "WPA: Failed to configure IGTK to the driver");
1236 return -1;
1237 }
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02001238 }
1239
Jouni Malinen58c0e962017-10-01 12:12:24 +03001240 if (wnm_sleep) {
1241 sm->igtk_wnm_sleep.igtk_len = len;
1242 os_memcpy(sm->igtk_wnm_sleep.igtk, igtk->igtk,
1243 sm->igtk_wnm_sleep.igtk_len);
1244 } else {
1245 sm->igtk.igtk_len = len;
1246 os_memcpy(sm->igtk.igtk, igtk->igtk, sm->igtk.igtk_len);
1247 }
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02001248
1249 return 0;
1250}
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02001251
1252
Hai Shalomfdcde762020-04-02 11:19:20 -07001253static int wpa_supplicant_install_bigtk(struct wpa_sm *sm,
1254 const struct wpa_bigtk_kde *bigtk,
1255 int wnm_sleep)
1256{
1257 size_t len = wpa_cipher_key_len(sm->mgmt_group_cipher);
1258 u16 keyidx = WPA_GET_LE16(bigtk->keyid);
1259
1260 /* Detect possible key reinstallation */
1261 if ((sm->bigtk.bigtk_len == len &&
1262 os_memcmp(sm->bigtk.bigtk, bigtk->bigtk,
1263 sm->bigtk.bigtk_len) == 0) ||
1264 (sm->bigtk_wnm_sleep.bigtk_len == len &&
1265 os_memcmp(sm->bigtk_wnm_sleep.bigtk, bigtk->bigtk,
1266 sm->bigtk_wnm_sleep.bigtk_len) == 0)) {
1267 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1268 "WPA: Not reinstalling already in-use BIGTK to the driver (keyidx=%d)",
1269 keyidx);
1270 return 0;
1271 }
1272
1273 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1274 "WPA: BIGTK keyid %d pn " COMPACT_MACSTR,
1275 keyidx, MAC2STR(bigtk->pn));
1276 wpa_hexdump_key(MSG_DEBUG, "WPA: BIGTK", bigtk->bigtk, len);
1277 if (keyidx < 6 || keyidx > 7) {
1278 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1279 "WPA: Invalid BIGTK KeyID %d", keyidx);
1280 return -1;
1281 }
1282 if (wpa_sm_set_key(sm, wpa_cipher_to_alg(sm->mgmt_group_cipher),
1283 broadcast_ether_addr,
1284 keyidx, 0, bigtk->pn, sizeof(bigtk->pn),
1285 bigtk->bigtk, len, KEY_FLAG_GROUP_RX) < 0) {
1286 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1287 "WPA: Failed to configure BIGTK to the driver");
1288 return -1;
1289 }
1290
1291 if (wnm_sleep) {
1292 sm->bigtk_wnm_sleep.bigtk_len = len;
1293 os_memcpy(sm->bigtk_wnm_sleep.bigtk, bigtk->bigtk,
1294 sm->bigtk_wnm_sleep.bigtk_len);
1295 } else {
1296 sm->bigtk.bigtk_len = len;
1297 os_memcpy(sm->bigtk.bigtk, bigtk->bigtk, sm->bigtk.bigtk_len);
1298 }
1299
1300 return 0;
1301}
1302
1303
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001304static int ieee80211w_set_keys(struct wpa_sm *sm,
1305 struct wpa_eapol_ie_parse *ie)
1306{
Hai Shalomfdcde762020-04-02 11:19:20 -07001307 size_t len;
1308
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001309 if (!wpa_cipher_valid_mgmt_group(sm->mgmt_group_cipher))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001310 return 0;
1311
1312 if (ie->igtk) {
1313 const struct wpa_igtk_kde *igtk;
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02001314
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001315 len = wpa_cipher_key_len(sm->mgmt_group_cipher);
1316 if (ie->igtk_len != WPA_IGTK_KDE_PREFIX_LEN + len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001317 return -1;
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02001318
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001319 igtk = (const struct wpa_igtk_kde *) ie->igtk;
Jouni Malinen58c0e962017-10-01 12:12:24 +03001320 if (wpa_supplicant_install_igtk(sm, igtk, 0) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001321 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001322 }
1323
Hai Shalomfdcde762020-04-02 11:19:20 -07001324 if (ie->bigtk && sm->beacon_prot) {
1325 const struct wpa_bigtk_kde *bigtk;
1326
1327 len = wpa_cipher_key_len(sm->mgmt_group_cipher);
1328 if (ie->bigtk_len != WPA_BIGTK_KDE_PREFIX_LEN + len)
1329 return -1;
1330
1331 bigtk = (const struct wpa_bigtk_kde *) ie->bigtk;
1332 if (wpa_supplicant_install_bigtk(sm, bigtk, 0) < 0)
1333 return -1;
1334 }
1335
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001336 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001337}
1338
1339
1340static void wpa_report_ie_mismatch(struct wpa_sm *sm,
1341 const char *reason, const u8 *src_addr,
1342 const u8 *wpa_ie, size_t wpa_ie_len,
1343 const u8 *rsn_ie, size_t rsn_ie_len)
1344{
1345 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, "WPA: %s (src=" MACSTR ")",
1346 reason, MAC2STR(src_addr));
1347
1348 if (sm->ap_wpa_ie) {
1349 wpa_hexdump(MSG_INFO, "WPA: WPA IE in Beacon/ProbeResp",
1350 sm->ap_wpa_ie, sm->ap_wpa_ie_len);
1351 }
1352 if (wpa_ie) {
1353 if (!sm->ap_wpa_ie) {
1354 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1355 "WPA: No WPA IE in Beacon/ProbeResp");
1356 }
1357 wpa_hexdump(MSG_INFO, "WPA: WPA IE in 3/4 msg",
1358 wpa_ie, wpa_ie_len);
1359 }
1360
1361 if (sm->ap_rsn_ie) {
1362 wpa_hexdump(MSG_INFO, "WPA: RSN IE in Beacon/ProbeResp",
1363 sm->ap_rsn_ie, sm->ap_rsn_ie_len);
1364 }
1365 if (rsn_ie) {
1366 if (!sm->ap_rsn_ie) {
1367 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1368 "WPA: No RSN IE in Beacon/ProbeResp");
1369 }
1370 wpa_hexdump(MSG_INFO, "WPA: RSN IE in 3/4 msg",
1371 rsn_ie, rsn_ie_len);
1372 }
1373
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001374 wpa_sm_deauthenticate(sm, WLAN_REASON_IE_IN_4WAY_DIFFERS);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001375}
1376
1377
1378#ifdef CONFIG_IEEE80211R
1379
1380static int ft_validate_mdie(struct wpa_sm *sm,
1381 const unsigned char *src_addr,
1382 struct wpa_eapol_ie_parse *ie,
1383 const u8 *assoc_resp_mdie)
1384{
1385 struct rsn_mdie *mdie;
1386
1387 mdie = (struct rsn_mdie *) (ie->mdie + 2);
1388 if (ie->mdie == NULL || ie->mdie_len < 2 + sizeof(*mdie) ||
1389 os_memcmp(mdie->mobility_domain, sm->mobility_domain,
1390 MOBILITY_DOMAIN_ID_LEN) != 0) {
1391 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "FT: MDIE in msg 3/4 did "
1392 "not match with the current mobility domain");
1393 return -1;
1394 }
1395
1396 if (assoc_resp_mdie &&
1397 (assoc_resp_mdie[1] != ie->mdie[1] ||
1398 os_memcmp(assoc_resp_mdie, ie->mdie, 2 + ie->mdie[1]) != 0)) {
1399 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "FT: MDIE mismatch");
1400 wpa_hexdump(MSG_DEBUG, "FT: MDIE in EAPOL-Key msg 3/4",
1401 ie->mdie, 2 + ie->mdie[1]);
1402 wpa_hexdump(MSG_DEBUG, "FT: MDIE in (Re)Association Response",
1403 assoc_resp_mdie, 2 + assoc_resp_mdie[1]);
1404 return -1;
1405 }
1406
1407 return 0;
1408}
1409
1410
1411static int ft_validate_ftie(struct wpa_sm *sm,
1412 const unsigned char *src_addr,
1413 struct wpa_eapol_ie_parse *ie,
1414 const u8 *assoc_resp_ftie)
1415{
1416 if (ie->ftie == NULL) {
1417 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1418 "FT: No FTIE in EAPOL-Key msg 3/4");
1419 return -1;
1420 }
1421
1422 if (assoc_resp_ftie == NULL)
1423 return 0;
1424
1425 if (assoc_resp_ftie[1] != ie->ftie[1] ||
1426 os_memcmp(assoc_resp_ftie, ie->ftie, 2 + ie->ftie[1]) != 0) {
1427 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "FT: FTIE mismatch");
1428 wpa_hexdump(MSG_DEBUG, "FT: FTIE in EAPOL-Key msg 3/4",
1429 ie->ftie, 2 + ie->ftie[1]);
1430 wpa_hexdump(MSG_DEBUG, "FT: FTIE in (Re)Association Response",
1431 assoc_resp_ftie, 2 + assoc_resp_ftie[1]);
1432 return -1;
1433 }
1434
1435 return 0;
1436}
1437
1438
1439static int ft_validate_rsnie(struct wpa_sm *sm,
1440 const unsigned char *src_addr,
1441 struct wpa_eapol_ie_parse *ie)
1442{
1443 struct wpa_ie_data rsn;
1444
1445 if (!ie->rsn_ie)
1446 return 0;
1447
1448 /*
1449 * Verify that PMKR1Name from EAPOL-Key message 3/4
1450 * matches with the value we derived.
1451 */
1452 if (wpa_parse_wpa_ie_rsn(ie->rsn_ie, ie->rsn_ie_len, &rsn) < 0 ||
1453 rsn.num_pmkid != 1 || rsn.pmkid == NULL) {
1454 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "FT: No PMKR1Name in "
1455 "FT 4-way handshake message 3/4");
1456 return -1;
1457 }
1458
Dmitry Shmidtc2817022014-07-02 10:32:10 -07001459 if (os_memcmp_const(rsn.pmkid, sm->pmk_r1_name, WPA_PMK_NAME_LEN) != 0)
1460 {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001461 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1462 "FT: PMKR1Name mismatch in "
1463 "FT 4-way handshake message 3/4");
1464 wpa_hexdump(MSG_DEBUG, "FT: PMKR1Name from Authenticator",
1465 rsn.pmkid, WPA_PMK_NAME_LEN);
1466 wpa_hexdump(MSG_DEBUG, "FT: Derived PMKR1Name",
1467 sm->pmk_r1_name, WPA_PMK_NAME_LEN);
1468 return -1;
1469 }
1470
1471 return 0;
1472}
1473
1474
1475static int wpa_supplicant_validate_ie_ft(struct wpa_sm *sm,
1476 const unsigned char *src_addr,
1477 struct wpa_eapol_ie_parse *ie)
1478{
1479 const u8 *pos, *end, *mdie = NULL, *ftie = NULL;
1480
1481 if (sm->assoc_resp_ies) {
1482 pos = sm->assoc_resp_ies;
1483 end = pos + sm->assoc_resp_ies_len;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001484 while (end - pos > 2) {
1485 if (2 + pos[1] > end - pos)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001486 break;
1487 switch (*pos) {
1488 case WLAN_EID_MOBILITY_DOMAIN:
1489 mdie = pos;
1490 break;
1491 case WLAN_EID_FAST_BSS_TRANSITION:
1492 ftie = pos;
1493 break;
1494 }
1495 pos += 2 + pos[1];
1496 }
1497 }
1498
1499 if (ft_validate_mdie(sm, src_addr, ie, mdie) < 0 ||
1500 ft_validate_ftie(sm, src_addr, ie, ftie) < 0 ||
1501 ft_validate_rsnie(sm, src_addr, ie) < 0)
1502 return -1;
1503
1504 return 0;
1505}
1506
1507#endif /* CONFIG_IEEE80211R */
1508
1509
1510static int wpa_supplicant_validate_ie(struct wpa_sm *sm,
1511 const unsigned char *src_addr,
1512 struct wpa_eapol_ie_parse *ie)
1513{
1514 if (sm->ap_wpa_ie == NULL && sm->ap_rsn_ie == NULL) {
1515 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1516 "WPA: No WPA/RSN IE for this AP known. "
1517 "Trying to get from scan results");
1518 if (wpa_sm_get_beacon_ie(sm) < 0) {
1519 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1520 "WPA: Could not find AP from "
1521 "the scan results");
Hai Shalomfdcde762020-04-02 11:19:20 -07001522 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001523 }
Hai Shalomfdcde762020-04-02 11:19:20 -07001524 wpa_msg(sm->ctx->msg_ctx, MSG_DEBUG,
1525 "WPA: Found the current AP from updated scan results");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001526 }
1527
1528 if (ie->wpa_ie == NULL && ie->rsn_ie == NULL &&
1529 (sm->ap_wpa_ie || sm->ap_rsn_ie)) {
1530 wpa_report_ie_mismatch(sm, "IE in 3/4 msg does not match "
1531 "with IE in Beacon/ProbeResp (no IE?)",
1532 src_addr, ie->wpa_ie, ie->wpa_ie_len,
1533 ie->rsn_ie, ie->rsn_ie_len);
1534 return -1;
1535 }
1536
1537 if ((ie->wpa_ie && sm->ap_wpa_ie &&
1538 (ie->wpa_ie_len != sm->ap_wpa_ie_len ||
1539 os_memcmp(ie->wpa_ie, sm->ap_wpa_ie, ie->wpa_ie_len) != 0)) ||
1540 (ie->rsn_ie && sm->ap_rsn_ie &&
1541 wpa_compare_rsn_ie(wpa_key_mgmt_ft(sm->key_mgmt),
1542 sm->ap_rsn_ie, sm->ap_rsn_ie_len,
1543 ie->rsn_ie, ie->rsn_ie_len))) {
1544 wpa_report_ie_mismatch(sm, "IE in 3/4 msg does not match "
1545 "with IE in Beacon/ProbeResp",
1546 src_addr, ie->wpa_ie, ie->wpa_ie_len,
1547 ie->rsn_ie, ie->rsn_ie_len);
1548 return -1;
1549 }
1550
1551 if (sm->proto == WPA_PROTO_WPA &&
1552 ie->rsn_ie && sm->ap_rsn_ie == NULL && sm->rsn_enabled) {
1553 wpa_report_ie_mismatch(sm, "Possible downgrade attack "
1554 "detected - RSN was enabled and RSN IE "
1555 "was in msg 3/4, but not in "
1556 "Beacon/ProbeResp",
1557 src_addr, ie->wpa_ie, ie->wpa_ie_len,
1558 ie->rsn_ie, ie->rsn_ie_len);
1559 return -1;
1560 }
1561
Hai Shalomc3565922019-10-28 11:58:20 -07001562 if ((sm->ap_rsnxe && !ie->rsnxe) ||
1563 (!sm->ap_rsnxe && ie->rsnxe) ||
1564 (sm->ap_rsnxe && ie->rsnxe &&
1565 (sm->ap_rsnxe_len != ie->rsnxe_len ||
1566 os_memcmp(sm->ap_rsnxe, ie->rsnxe, sm->ap_rsnxe_len) != 0))) {
1567 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1568 "WPA: RSNXE mismatch between Beacon/ProbeResp and EAPOL-Key msg 3/4");
Hai Shalomfdcde762020-04-02 11:19:20 -07001569 wpa_hexdump(MSG_INFO, "RSNXE in Beacon/ProbeResp",
1570 sm->ap_rsnxe, sm->ap_rsnxe_len);
1571 wpa_hexdump(MSG_INFO, "RSNXE in EAPOL-Key msg 3/4",
1572 ie->rsnxe, ie->rsnxe_len);
1573 wpa_sm_deauthenticate(sm, WLAN_REASON_IE_IN_4WAY_DIFFERS);
Hai Shalomc3565922019-10-28 11:58:20 -07001574 return -1;
1575 }
1576
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001577#ifdef CONFIG_IEEE80211R
1578 if (wpa_key_mgmt_ft(sm->key_mgmt) &&
1579 wpa_supplicant_validate_ie_ft(sm, src_addr, ie) < 0)
1580 return -1;
1581#endif /* CONFIG_IEEE80211R */
1582
1583 return 0;
1584}
1585
1586
1587/**
1588 * wpa_supplicant_send_4_of_4 - Send message 4 of WPA/RSN 4-Way Handshake
1589 * @sm: Pointer to WPA state machine data from wpa_sm_init()
1590 * @dst: Destination address for the frame
1591 * @key: Pointer to the EAPOL-Key frame header
1592 * @ver: Version bits from EAPOL-Key Key Info
1593 * @key_info: Key Info
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001594 * @ptk: PTK to use for keyed hash and encryption
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001595 * Returns: >= 0 on success, < 0 on failure
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001596 */
1597int wpa_supplicant_send_4_of_4(struct wpa_sm *sm, const unsigned char *dst,
1598 const struct wpa_eapol_key *key,
1599 u16 ver, u16 key_info,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001600 struct wpa_ptk *ptk)
1601{
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001602 size_t mic_len, hdrlen, rlen;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001603 struct wpa_eapol_key *reply;
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001604 u8 *rbuf, *key_mic;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001605
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001606 mic_len = wpa_mic_len(sm->key_mgmt, sm->pmk_len);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001607 hdrlen = sizeof(*reply) + mic_len + 2;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001608 rbuf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_KEY, NULL,
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001609 hdrlen, &rlen, (void *) &reply);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001610 if (rbuf == NULL)
1611 return -1;
1612
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001613 reply->type = (sm->proto == WPA_PROTO_RSN ||
1614 sm->proto == WPA_PROTO_OSEN) ?
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001615 EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA;
1616 key_info &= WPA_KEY_INFO_SECURE;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001617 key_info |= ver | WPA_KEY_INFO_KEY_TYPE;
1618 if (mic_len)
1619 key_info |= WPA_KEY_INFO_MIC;
1620 else
1621 key_info |= WPA_KEY_INFO_ENCR_KEY_DATA;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001622 WPA_PUT_BE16(reply->key_info, key_info);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001623 if (sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001624 WPA_PUT_BE16(reply->key_length, 0);
1625 else
1626 os_memcpy(reply->key_length, key->key_length, 2);
1627 os_memcpy(reply->replay_counter, key->replay_counter,
1628 WPA_REPLAY_COUNTER_LEN);
1629
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001630 key_mic = (u8 *) (reply + 1);
1631 WPA_PUT_BE16(key_mic + mic_len, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001632
Roshan Pius5e7db942018-04-12 12:27:41 -07001633 wpa_dbg(sm->ctx->msg_ctx, MSG_INFO, "WPA: Sending EAPOL-Key 4/4");
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001634 return wpa_eapol_key_send(sm, ptk, ver, dst, ETH_P_EAPOL, rbuf, rlen,
1635 key_mic);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001636}
1637
1638
1639static void wpa_supplicant_process_3_of_4(struct wpa_sm *sm,
1640 const struct wpa_eapol_key *key,
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001641 u16 ver, const u8 *key_data,
1642 size_t key_data_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001643{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001644 u16 key_info, keylen;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001645 struct wpa_eapol_ie_parse ie;
1646
1647 wpa_sm_set_state(sm, WPA_4WAY_HANDSHAKE);
Roshan Pius5e7db942018-04-12 12:27:41 -07001648 wpa_dbg(sm->ctx->msg_ctx, MSG_INFO, "WPA: RX message 3 of 4-Way "
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001649 "Handshake from " MACSTR " (ver=%d)", MAC2STR(sm->bssid), ver);
1650
1651 key_info = WPA_GET_BE16(key->key_info);
1652
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001653 wpa_hexdump(MSG_DEBUG, "WPA: IE KeyData", key_data, key_data_len);
1654 if (wpa_supplicant_parse_ies(key_data, key_data_len, &ie) < 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001655 goto failed;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001656 if (ie.gtk && !(key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
1657 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1658 "WPA: GTK IE in unencrypted key data");
1659 goto failed;
1660 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001661 if (ie.igtk && !(key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
1662 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1663 "WPA: IGTK KDE in unencrypted key data");
1664 goto failed;
1665 }
1666
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001667 if (ie.igtk &&
1668 wpa_cipher_valid_mgmt_group(sm->mgmt_group_cipher) &&
1669 ie.igtk_len != WPA_IGTK_KDE_PREFIX_LEN +
1670 (unsigned int) wpa_cipher_key_len(sm->mgmt_group_cipher)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001671 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1672 "WPA: Invalid IGTK KDE length %lu",
1673 (unsigned long) ie.igtk_len);
1674 goto failed;
1675 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001676
1677 if (wpa_supplicant_validate_ie(sm, sm->bssid, &ie) < 0)
1678 goto failed;
1679
Hai Shalomfdcde762020-04-02 11:19:20 -07001680 if (wpa_handle_ext_key_id(sm, &ie))
1681 goto failed;
1682
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001683 if (os_memcmp(sm->anonce, key->key_nonce, WPA_NONCE_LEN) != 0) {
1684 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1685 "WPA: ANonce from message 1 of 4-Way Handshake "
1686 "differs from 3 of 4-Way Handshake - drop packet (src="
1687 MACSTR ")", MAC2STR(sm->bssid));
1688 goto failed;
1689 }
1690
1691 keylen = WPA_GET_BE16(key->key_length);
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07001692 if (keylen != wpa_cipher_key_len(sm->pairwise_cipher)) {
1693 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1694 "WPA: Invalid %s key length %d (src=" MACSTR
1695 ")", wpa_cipher_txt(sm->pairwise_cipher), keylen,
1696 MAC2STR(sm->bssid));
1697 goto failed;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001698 }
1699
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001700#ifdef CONFIG_P2P
1701 if (ie.ip_addr_alloc) {
1702 os_memcpy(sm->p2p_ip_addr, ie.ip_addr_alloc, 3 * 4);
1703 wpa_hexdump(MSG_DEBUG, "P2P: IP address info",
1704 sm->p2p_ip_addr, sizeof(sm->p2p_ip_addr));
1705 }
1706#endif /* CONFIG_P2P */
1707
Hai Shalom74f70d42019-02-11 14:42:39 -08001708#ifdef CONFIG_OCV
1709 if (wpa_sm_ocv_enabled(sm)) {
1710 struct wpa_channel_info ci;
1711
1712 if (wpa_sm_channel_info(sm, &ci) != 0) {
1713 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1714 "Failed to get channel info to validate received OCI in EAPOL-Key 3/4");
1715 return;
1716 }
1717
1718 if (ocv_verify_tx_params(ie.oci, ie.oci_len, &ci,
1719 channel_width_to_int(ci.chanwidth),
Hai Shalom899fcc72020-10-19 14:38:18 -07001720 ci.seg1_idx) != OCI_SUCCESS) {
1721 wpa_msg(sm->ctx->msg_ctx, MSG_INFO, OCV_FAILURE
1722 "addr=" MACSTR " frame=eapol-key-m3 error=%s",
1723 MAC2STR(sm->bssid), ocv_errorstr);
Hai Shalom74f70d42019-02-11 14:42:39 -08001724 return;
1725 }
1726 }
1727#endif /* CONFIG_OCV */
1728
Hai Shalom4fbc08f2020-05-18 12:37:00 -07001729#ifdef CONFIG_DPP2
1730 if (DPP_VERSION > 1 && ie.dpp_kde) {
1731 wpa_printf(MSG_DEBUG,
1732 "DPP: peer Protocol Version %u Flags 0x%x",
1733 ie.dpp_kde[0], ie.dpp_kde[1]);
1734 if (sm->key_mgmt == WPA_KEY_MGMT_DPP && sm->dpp_pfs != 2 &&
1735 (ie.dpp_kde[1] & DPP_KDE_PFS_ALLOWED) && !sm->dpp_z) {
1736 wpa_printf(MSG_INFO,
1737 "DPP: Peer indicated it supports PFS and local configuration allows this, but PFS was not negotiated for the association");
1738 goto failed;
1739 }
1740 }
1741#endif /* CONFIG_DPP2 */
1742
Hai Shalomfdcde762020-04-02 11:19:20 -07001743 if (sm->use_ext_key_id &&
1744 wpa_supplicant_install_ptk(sm, key, KEY_FLAG_RX))
1745 goto failed;
1746
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001747 if (wpa_supplicant_send_4_of_4(sm, sm->bssid, key, ver, key_info,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001748 &sm->ptk) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001749 goto failed;
1750 }
1751
1752 /* SNonce was successfully used in msg 3/4, so mark it to be renewed
1753 * for the next 4-Way Handshake. If msg 3 is received again, the old
1754 * SNonce will still be used to avoid changing PTK. */
1755 sm->renew_snonce = 1;
1756
1757 if (key_info & WPA_KEY_INFO_INSTALL) {
Hai Shalomfdcde762020-04-02 11:19:20 -07001758 int res;
1759
1760 if (sm->use_ext_key_id)
1761 res = wpa_supplicant_activate_ptk(sm);
1762 else
1763 res = wpa_supplicant_install_ptk(sm, key,
1764 KEY_FLAG_RX_TX);
1765 if (res)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001766 goto failed;
1767 }
1768
1769 if (key_info & WPA_KEY_INFO_SECURE) {
1770 wpa_sm_mlme_setprotection(
1771 sm, sm->bssid, MLME_SETPROTECTION_PROTECT_TYPE_RX,
1772 MLME_SETPROTECTION_KEY_TYPE_PAIRWISE);
Hai Shalome21d4e82020-04-29 16:34:06 -07001773 eapol_sm_notify_portValid(sm->eapol, true);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001774 }
1775 wpa_sm_set_state(sm, WPA_GROUP_HANDSHAKE);
1776
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001777 if (sm->group_cipher == WPA_CIPHER_GTK_NOT_USED) {
Hai Shalom5f92bc92019-04-18 11:54:11 -07001778 /* No GTK to be set to the driver */
1779 } else if (!ie.gtk && sm->proto == WPA_PROTO_RSN) {
1780 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1781 "RSN: No GTK KDE included in EAPOL-Key msg 3/4");
1782 goto failed;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001783 } else if (ie.gtk &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001784 wpa_supplicant_pairwise_gtk(sm, key,
1785 ie.gtk, ie.gtk_len, key_info) < 0) {
1786 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1787 "RSN: Failed to configure GTK");
1788 goto failed;
1789 }
1790
1791 if (ieee80211w_set_keys(sm, &ie) < 0) {
1792 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1793 "RSN: Failed to configure IGTK");
1794 goto failed;
1795 }
1796
Hai Shalom5f92bc92019-04-18 11:54:11 -07001797 if (sm->group_cipher == WPA_CIPHER_GTK_NOT_USED || ie.gtk)
1798 wpa_supplicant_key_neg_complete(sm, sm->bssid,
1799 key_info & WPA_KEY_INFO_SECURE);
1800
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001801 if (ie.gtk)
1802 wpa_sm_set_rekey_offload(sm);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001803
Hai Shalom39ba6fc2019-01-22 12:40:38 -08001804 /* Add PMKSA cache entry for Suite B AKMs here since PMKID can be
1805 * calculated only after KCK has been derived. Though, do not replace an
1806 * existing PMKSA entry after each 4-way handshake (i.e., new KCK/PMKID)
1807 * to avoid unnecessary changes of PMKID while continuing to use the
1808 * same PMK. */
1809 if (sm->proto == WPA_PROTO_RSN && wpa_key_mgmt_suite_b(sm->key_mgmt) &&
1810 !sm->cur_pmksa) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001811 struct rsn_pmksa_cache_entry *sa;
1812
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001813 sa = pmksa_cache_add(sm->pmksa, sm->pmk, sm->pmk_len, NULL,
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001814 sm->ptk.kck, sm->ptk.kck_len,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001815 sm->bssid, sm->own_addr,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001816 sm->network_ctx, sm->key_mgmt, NULL);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001817 if (!sm->cur_pmksa)
1818 sm->cur_pmksa = sa;
1819 }
1820
Hai Shalomfdcde762020-04-02 11:19:20 -07001821 if (ie.transition_disable)
1822 wpa_sm_transition_disable(sm, ie.transition_disable[0]);
Dmitry Shmidt4dd28dc2015-03-10 11:21:43 -07001823 sm->msg_3_of_4_ok = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001824 return;
1825
1826failed:
1827 wpa_sm_deauthenticate(sm, WLAN_REASON_UNSPECIFIED);
1828}
1829
1830
1831static int wpa_supplicant_process_1_of_2_rsn(struct wpa_sm *sm,
1832 const u8 *keydata,
1833 size_t keydatalen,
1834 u16 key_info,
1835 struct wpa_gtk_data *gd)
1836{
1837 int maxkeylen;
1838 struct wpa_eapol_ie_parse ie;
1839
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08001840 wpa_hexdump_key(MSG_DEBUG, "RSN: msg 1/2 key data",
1841 keydata, keydatalen);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001842 if (wpa_supplicant_parse_ies(keydata, keydatalen, &ie) < 0)
1843 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001844 if (ie.gtk && !(key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
1845 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1846 "WPA: GTK IE in unencrypted key data");
1847 return -1;
1848 }
1849 if (ie.gtk == NULL) {
1850 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1851 "WPA: No GTK IE in Group Key msg 1/2");
1852 return -1;
1853 }
1854 maxkeylen = gd->gtk_len = ie.gtk_len - 2;
1855
Hai Shalom74f70d42019-02-11 14:42:39 -08001856#ifdef CONFIG_OCV
1857 if (wpa_sm_ocv_enabled(sm)) {
1858 struct wpa_channel_info ci;
1859
1860 if (wpa_sm_channel_info(sm, &ci) != 0) {
1861 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1862 "Failed to get channel info to validate received OCI in EAPOL-Key group msg 1/2");
1863 return -1;
1864 }
1865
1866 if (ocv_verify_tx_params(ie.oci, ie.oci_len, &ci,
1867 channel_width_to_int(ci.chanwidth),
Hai Shalom899fcc72020-10-19 14:38:18 -07001868 ci.seg1_idx) != OCI_SUCCESS) {
1869 wpa_msg(sm->ctx->msg_ctx, MSG_INFO, OCV_FAILURE
1870 "addr=" MACSTR " frame=eapol-key-g1 error=%s",
1871 MAC2STR(sm->bssid), ocv_errorstr);
Hai Shalom74f70d42019-02-11 14:42:39 -08001872 return -1;
1873 }
1874 }
1875#endif /* CONFIG_OCV */
1876
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001877 if (wpa_supplicant_check_group_cipher(sm, sm->group_cipher,
1878 gd->gtk_len, maxkeylen,
1879 &gd->key_rsc_len, &gd->alg))
1880 return -1;
1881
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001882 wpa_hexdump_key(MSG_DEBUG, "RSN: received GTK in group key handshake",
1883 ie.gtk, ie.gtk_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001884 gd->keyidx = ie.gtk[0] & 0x3;
1885 gd->tx = wpa_supplicant_gtk_tx_bit_workaround(sm,
1886 !!(ie.gtk[0] & BIT(2)));
1887 if (ie.gtk_len - 2 > sizeof(gd->gtk)) {
1888 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1889 "RSN: Too long GTK in GTK IE (len=%lu)",
1890 (unsigned long) ie.gtk_len - 2);
1891 return -1;
1892 }
1893 os_memcpy(gd->gtk, ie.gtk + 2, ie.gtk_len - 2);
1894
1895 if (ieee80211w_set_keys(sm, &ie) < 0)
1896 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1897 "RSN: Failed to configure IGTK");
1898
1899 return 0;
1900}
1901
1902
1903static int wpa_supplicant_process_1_of_2_wpa(struct wpa_sm *sm,
1904 const struct wpa_eapol_key *key,
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001905 const u8 *key_data,
1906 size_t key_data_len, u16 key_info,
1907 u16 ver, struct wpa_gtk_data *gd)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001908{
1909 size_t maxkeylen;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001910 u16 gtk_len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001911
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001912 gtk_len = WPA_GET_BE16(key->key_length);
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001913 maxkeylen = key_data_len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001914 if (ver == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
1915 if (maxkeylen < 8) {
1916 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1917 "WPA: Too short maxkeylen (%lu)",
1918 (unsigned long) maxkeylen);
1919 return -1;
1920 }
1921 maxkeylen -= 8;
1922 }
1923
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001924 if (gtk_len > maxkeylen ||
1925 wpa_supplicant_check_group_cipher(sm, sm->group_cipher,
1926 gtk_len, maxkeylen,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001927 &gd->key_rsc_len, &gd->alg))
1928 return -1;
1929
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001930 gd->gtk_len = gtk_len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001931 gd->keyidx = (key_info & WPA_KEY_INFO_KEY_INDEX_MASK) >>
1932 WPA_KEY_INFO_KEY_INDEX_SHIFT;
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001933 if (ver == WPA_KEY_INFO_TYPE_HMAC_MD5_RC4 && sm->ptk.kek_len == 16) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001934#ifdef CONFIG_NO_RC4
1935 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1936 "WPA: RC4 not supported in the build");
1937 return -1;
1938#else /* CONFIG_NO_RC4 */
Dmitry Shmidt61593f02014-04-21 16:27:35 -07001939 u8 ek[32];
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001940 if (key_data_len > sizeof(gd->gtk)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001941 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1942 "WPA: RC4 key data too long (%lu)",
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001943 (unsigned long) key_data_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001944 return -1;
1945 }
Dmitry Shmidt61593f02014-04-21 16:27:35 -07001946 os_memcpy(ek, key->key_iv, 16);
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001947 os_memcpy(ek + 16, sm->ptk.kek, sm->ptk.kek_len);
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001948 os_memcpy(gd->gtk, key_data, key_data_len);
1949 if (rc4_skip(ek, 32, 256, gd->gtk, key_data_len)) {
Hai Shalom81f62d82019-07-22 12:10:00 -07001950 forced_memzero(ek, sizeof(ek));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001951 wpa_msg(sm->ctx->msg_ctx, MSG_ERROR,
1952 "WPA: RC4 failed");
1953 return -1;
1954 }
Hai Shalom81f62d82019-07-22 12:10:00 -07001955 forced_memzero(ek, sizeof(ek));
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001956#endif /* CONFIG_NO_RC4 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001957 } else if (ver == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001958 if (maxkeylen % 8) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001959 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1960 "WPA: Unsupported AES-WRAP len %lu",
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001961 (unsigned long) maxkeylen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001962 return -1;
1963 }
1964 if (maxkeylen > sizeof(gd->gtk)) {
1965 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1966 "WPA: AES-WRAP key data "
1967 "too long (keydatalen=%lu maxkeylen=%lu)",
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001968 (unsigned long) key_data_len,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001969 (unsigned long) maxkeylen);
1970 return -1;
1971 }
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001972 if (aes_unwrap(sm->ptk.kek, sm->ptk.kek_len, maxkeylen / 8,
1973 key_data, gd->gtk)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001974 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1975 "WPA: AES unwrap failed - could not decrypt "
1976 "GTK");
1977 return -1;
1978 }
1979 } else {
1980 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1981 "WPA: Unsupported key_info type %d", ver);
1982 return -1;
1983 }
1984 gd->tx = wpa_supplicant_gtk_tx_bit_workaround(
1985 sm, !!(key_info & WPA_KEY_INFO_TXRX));
1986 return 0;
1987}
1988
1989
1990static int wpa_supplicant_send_2_of_2(struct wpa_sm *sm,
1991 const struct wpa_eapol_key *key,
1992 int ver, u16 key_info)
1993{
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001994 size_t mic_len, hdrlen, rlen;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001995 struct wpa_eapol_key *reply;
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001996 u8 *rbuf, *key_mic;
Hai Shalom74f70d42019-02-11 14:42:39 -08001997 size_t kde_len = 0;
1998
1999#ifdef CONFIG_OCV
2000 if (wpa_sm_ocv_enabled(sm))
2001 kde_len = OCV_OCI_KDE_LEN;
2002#endif /* CONFIG_OCV */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002003
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002004 mic_len = wpa_mic_len(sm->key_mgmt, sm->pmk_len);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002005 hdrlen = sizeof(*reply) + mic_len + 2;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002006 rbuf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_KEY, NULL,
Hai Shalom74f70d42019-02-11 14:42:39 -08002007 hdrlen + kde_len, &rlen, (void *) &reply);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002008 if (rbuf == NULL)
2009 return -1;
2010
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002011 reply->type = (sm->proto == WPA_PROTO_RSN ||
2012 sm->proto == WPA_PROTO_OSEN) ?
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002013 EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA;
2014 key_info &= WPA_KEY_INFO_KEY_INDEX_MASK;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002015 key_info |= ver | WPA_KEY_INFO_SECURE;
2016 if (mic_len)
2017 key_info |= WPA_KEY_INFO_MIC;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002018 else
2019 key_info |= WPA_KEY_INFO_ENCR_KEY_DATA;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002020 WPA_PUT_BE16(reply->key_info, key_info);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002021 if (sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002022 WPA_PUT_BE16(reply->key_length, 0);
2023 else
2024 os_memcpy(reply->key_length, key->key_length, 2);
2025 os_memcpy(reply->replay_counter, key->replay_counter,
2026 WPA_REPLAY_COUNTER_LEN);
2027
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002028 key_mic = (u8 *) (reply + 1);
Hai Shalom74f70d42019-02-11 14:42:39 -08002029 WPA_PUT_BE16(key_mic + mic_len, kde_len); /* Key Data Length */
2030
2031#ifdef CONFIG_OCV
2032 if (wpa_sm_ocv_enabled(sm)) {
2033 struct wpa_channel_info ci;
2034 u8 *pos;
2035
2036 if (wpa_sm_channel_info(sm, &ci) != 0) {
2037 wpa_printf(MSG_WARNING,
2038 "Failed to get channel info for OCI element in EAPOL-Key 2/2");
2039 os_free(rbuf);
2040 return -1;
2041 }
Hai Shalom899fcc72020-10-19 14:38:18 -07002042#ifdef CONFIG_TESTING_OPTIONS
2043 if (sm->oci_freq_override_eapol_g2) {
2044 wpa_printf(MSG_INFO,
2045 "TEST: Override OCI KDE frequency %d -> %d MHz",
2046 ci.frequency,
2047 sm->oci_freq_override_eapol_g2);
2048 ci.frequency = sm->oci_freq_override_eapol_g2;
2049 }
2050#endif /* CONFIG_TESTING_OPTIONS */
Hai Shalom74f70d42019-02-11 14:42:39 -08002051
2052 pos = key_mic + mic_len + 2; /* Key Data */
2053 if (ocv_insert_oci_kde(&ci, &pos) < 0) {
2054 os_free(rbuf);
2055 return -1;
2056 }
2057 }
2058#endif /* CONFIG_OCV */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002059
2060 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Sending EAPOL-Key 2/2");
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002061 return wpa_eapol_key_send(sm, &sm->ptk, ver, sm->bssid, ETH_P_EAPOL,
2062 rbuf, rlen, key_mic);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002063}
2064
2065
2066static void wpa_supplicant_process_1_of_2(struct wpa_sm *sm,
2067 const unsigned char *src_addr,
2068 const struct wpa_eapol_key *key,
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002069 const u8 *key_data,
2070 size_t key_data_len, u16 ver)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002071{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002072 u16 key_info;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002073 int rekey, ret;
2074 struct wpa_gtk_data gd;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002075 const u8 *key_rsc;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002076
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002077 if (!sm->msg_3_of_4_ok && !wpa_fils_is_completed(sm)) {
Dmitry Shmidt4dd28dc2015-03-10 11:21:43 -07002078 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2079 "WPA: Group Key Handshake started prior to completion of 4-way handshake");
2080 goto failed;
2081 }
2082
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002083 os_memset(&gd, 0, sizeof(gd));
2084
2085 rekey = wpa_sm_get_state(sm) == WPA_COMPLETED;
2086 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: RX message 1 of Group Key "
2087 "Handshake from " MACSTR " (ver=%d)", MAC2STR(src_addr), ver);
2088
2089 key_info = WPA_GET_BE16(key->key_info);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002090
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002091 if (sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN) {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002092 ret = wpa_supplicant_process_1_of_2_rsn(sm, key_data,
2093 key_data_len, key_info,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002094 &gd);
2095 } else {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002096 ret = wpa_supplicant_process_1_of_2_wpa(sm, key, key_data,
2097 key_data_len,
2098 key_info, ver, &gd);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002099 }
2100
2101 wpa_sm_set_state(sm, WPA_GROUP_HANDSHAKE);
2102
2103 if (ret)
2104 goto failed;
2105
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002106 key_rsc = key->key_rsc;
2107 if (wpa_supplicant_rsc_relaxation(sm, key->key_rsc))
2108 key_rsc = null_rsc;
2109
Jouni Malinen58c0e962017-10-01 12:12:24 +03002110 if (wpa_supplicant_install_gtk(sm, &gd, key_rsc, 0) ||
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002111 wpa_supplicant_send_2_of_2(sm, key, ver, key_info) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002112 goto failed;
Hai Shalom81f62d82019-07-22 12:10:00 -07002113 forced_memzero(&gd, sizeof(gd));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002114
2115 if (rekey) {
2116 wpa_msg(sm->ctx->msg_ctx, MSG_INFO, "WPA: Group rekeying "
2117 "completed with " MACSTR " [GTK=%s]",
2118 MAC2STR(sm->bssid), wpa_cipher_txt(sm->group_cipher));
2119 wpa_sm_cancel_auth_timeout(sm);
2120 wpa_sm_set_state(sm, WPA_COMPLETED);
2121 } else {
2122 wpa_supplicant_key_neg_complete(sm, sm->bssid,
2123 key_info &
2124 WPA_KEY_INFO_SECURE);
2125 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08002126
2127 wpa_sm_set_rekey_offload(sm);
2128
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002129 return;
2130
2131failed:
Hai Shalom81f62d82019-07-22 12:10:00 -07002132 forced_memzero(&gd, sizeof(gd));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002133 wpa_sm_deauthenticate(sm, WLAN_REASON_UNSPECIFIED);
2134}
2135
2136
2137static int wpa_supplicant_verify_eapol_key_mic(struct wpa_sm *sm,
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002138 struct wpa_eapol_key *key,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002139 u16 ver,
2140 const u8 *buf, size_t len)
2141{
Dmitry Shmidt807291d2015-01-27 13:40:23 -08002142 u8 mic[WPA_EAPOL_KEY_MIC_MAX_LEN];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002143 int ok = 0;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002144 size_t mic_len = wpa_mic_len(sm->key_mgmt, sm->pmk_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002145
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002146 os_memcpy(mic, key + 1, mic_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002147 if (sm->tptk_set) {
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002148 os_memset(key + 1, 0, mic_len);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002149 if (wpa_eapol_key_mic(sm->tptk.kck, sm->tptk.kck_len,
2150 sm->key_mgmt,
2151 ver, buf, len, (u8 *) (key + 1)) < 0 ||
2152 os_memcmp_const(mic, key + 1, mic_len) != 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002153 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
2154 "WPA: Invalid EAPOL-Key MIC "
2155 "when using TPTK - ignoring TPTK");
Hai Shalom74f70d42019-02-11 14:42:39 -08002156#ifdef TEST_FUZZ
2157 wpa_printf(MSG_INFO,
2158 "TEST: Ignore Key MIC failure for fuzz testing");
2159 goto continue_fuzz;
2160#endif /* TEST_FUZZ */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002161 } else {
Hai Shalom74f70d42019-02-11 14:42:39 -08002162#ifdef TEST_FUZZ
2163 continue_fuzz:
2164#endif /* TEST_FUZZ */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002165 ok = 1;
2166 sm->tptk_set = 0;
2167 sm->ptk_set = 1;
2168 os_memcpy(&sm->ptk, &sm->tptk, sizeof(sm->ptk));
Dmitry Shmidt61593f02014-04-21 16:27:35 -07002169 os_memset(&sm->tptk, 0, sizeof(sm->tptk));
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002170 /*
2171 * This assures the same TPTK in sm->tptk can never be
Roshan Pius3a1667e2018-07-03 15:17:14 -07002172 * copied twice to sm->ptk as the new PTK. In
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002173 * combination with the installed flag in the wpa_ptk
2174 * struct, this assures the same PTK is only installed
2175 * once.
2176 */
2177 sm->renew_snonce = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002178 }
2179 }
2180
2181 if (!ok && sm->ptk_set) {
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002182 os_memset(key + 1, 0, mic_len);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002183 if (wpa_eapol_key_mic(sm->ptk.kck, sm->ptk.kck_len,
2184 sm->key_mgmt,
2185 ver, buf, len, (u8 *) (key + 1)) < 0 ||
2186 os_memcmp_const(mic, key + 1, mic_len) != 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002187 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
2188 "WPA: Invalid EAPOL-Key MIC - "
2189 "dropping packet");
Hai Shalom74f70d42019-02-11 14:42:39 -08002190#ifdef TEST_FUZZ
2191 wpa_printf(MSG_INFO,
2192 "TEST: Ignore Key MIC failure for fuzz testing");
2193 goto continue_fuzz2;
2194#endif /* TEST_FUZZ */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002195 return -1;
2196 }
Hai Shalom74f70d42019-02-11 14:42:39 -08002197#ifdef TEST_FUZZ
2198 continue_fuzz2:
2199#endif /* TEST_FUZZ */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002200 ok = 1;
2201 }
2202
2203 if (!ok) {
2204 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
2205 "WPA: Could not verify EAPOL-Key MIC - "
2206 "dropping packet");
2207 return -1;
2208 }
2209
2210 os_memcpy(sm->rx_replay_counter, key->replay_counter,
2211 WPA_REPLAY_COUNTER_LEN);
2212 sm->rx_replay_counter_set = 1;
2213 return 0;
2214}
2215
2216
2217/* Decrypt RSN EAPOL-Key key data (RC4 or AES-WRAP) */
2218static int wpa_supplicant_decrypt_key_data(struct wpa_sm *sm,
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002219 struct wpa_eapol_key *key,
2220 size_t mic_len, u16 ver,
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002221 u8 *key_data, size_t *key_data_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002222{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002223 wpa_hexdump(MSG_DEBUG, "RSN: encrypted key data",
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002224 key_data, *key_data_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002225 if (!sm->ptk_set) {
2226 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
2227 "WPA: PTK not available, cannot decrypt EAPOL-Key Key "
2228 "Data");
2229 return -1;
2230 }
2231
2232 /* Decrypt key data here so that this operation does not need
2233 * to be implemented separately for each message type. */
Dmitry Shmidt807291d2015-01-27 13:40:23 -08002234 if (ver == WPA_KEY_INFO_TYPE_HMAC_MD5_RC4 && sm->ptk.kek_len == 16) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002235#ifdef CONFIG_NO_RC4
2236 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
2237 "WPA: RC4 not supported in the build");
2238 return -1;
2239#else /* CONFIG_NO_RC4 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002240 u8 ek[32];
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002241
2242 wpa_printf(MSG_DEBUG, "WPA: Decrypt Key Data using RC4");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002243 os_memcpy(ek, key->key_iv, 16);
Dmitry Shmidt807291d2015-01-27 13:40:23 -08002244 os_memcpy(ek + 16, sm->ptk.kek, sm->ptk.kek_len);
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002245 if (rc4_skip(ek, 32, 256, key_data, *key_data_len)) {
Hai Shalom81f62d82019-07-22 12:10:00 -07002246 forced_memzero(ek, sizeof(ek));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002247 wpa_msg(sm->ctx->msg_ctx, MSG_ERROR,
2248 "WPA: RC4 failed");
2249 return -1;
2250 }
Hai Shalom81f62d82019-07-22 12:10:00 -07002251 forced_memzero(ek, sizeof(ek));
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002252#endif /* CONFIG_NO_RC4 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002253 } else if (ver == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES ||
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002254 ver == WPA_KEY_INFO_TYPE_AES_128_CMAC ||
Roshan Pius3a1667e2018-07-03 15:17:14 -07002255 wpa_use_aes_key_wrap(sm->key_mgmt)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002256 u8 *buf;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002257
2258 wpa_printf(MSG_DEBUG,
2259 "WPA: Decrypt Key Data using AES-UNWRAP (KEK length %u)",
2260 (unsigned int) sm->ptk.kek_len);
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002261 if (*key_data_len < 8 || *key_data_len % 8) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002262 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002263 "WPA: Unsupported AES-WRAP len %u",
2264 (unsigned int) *key_data_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002265 return -1;
2266 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002267 *key_data_len -= 8; /* AES-WRAP adds 8 bytes */
2268 buf = os_malloc(*key_data_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002269 if (buf == NULL) {
2270 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
2271 "WPA: No memory for AES-UNWRAP buffer");
2272 return -1;
2273 }
Hai Shalom74f70d42019-02-11 14:42:39 -08002274#ifdef TEST_FUZZ
2275 os_memset(buf, 0x11, *key_data_len);
2276#endif /* TEST_FUZZ */
Dmitry Shmidt807291d2015-01-27 13:40:23 -08002277 if (aes_unwrap(sm->ptk.kek, sm->ptk.kek_len, *key_data_len / 8,
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002278 key_data, buf)) {
Hai Shalom74f70d42019-02-11 14:42:39 -08002279#ifdef TEST_FUZZ
2280 wpa_printf(MSG_INFO,
2281 "TEST: Ignore AES unwrap failure for fuzz testing");
2282 goto continue_fuzz;
2283#endif /* TEST_FUZZ */
Dmitry Shmidt7d56b752015-12-22 10:59:44 -08002284 bin_clear_free(buf, *key_data_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002285 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
2286 "WPA: AES unwrap failed - "
2287 "could not decrypt EAPOL-Key key data");
2288 return -1;
2289 }
Hai Shalom74f70d42019-02-11 14:42:39 -08002290#ifdef TEST_FUZZ
2291 continue_fuzz:
2292#endif /* TEST_FUZZ */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002293 os_memcpy(key_data, buf, *key_data_len);
Dmitry Shmidt7d56b752015-12-22 10:59:44 -08002294 bin_clear_free(buf, *key_data_len);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002295 WPA_PUT_BE16(((u8 *) (key + 1)) + mic_len, *key_data_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002296 } else {
2297 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
2298 "WPA: Unsupported key_info type %d", ver);
2299 return -1;
2300 }
2301 wpa_hexdump_key(MSG_DEBUG, "WPA: decrypted EAPOL-Key key data",
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002302 key_data, *key_data_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002303 return 0;
2304}
2305
2306
2307/**
2308 * wpa_sm_aborted_cached - Notify WPA that PMKSA caching was aborted
2309 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2310 */
2311void wpa_sm_aborted_cached(struct wpa_sm *sm)
2312{
2313 if (sm && sm->cur_pmksa) {
2314 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2315 "RSN: Cancelling PMKSA caching attempt");
2316 sm->cur_pmksa = NULL;
2317 }
2318}
2319
2320
2321static void wpa_eapol_key_dump(struct wpa_sm *sm,
Dmitry Shmidt807291d2015-01-27 13:40:23 -08002322 const struct wpa_eapol_key *key,
2323 unsigned int key_data_len,
2324 const u8 *mic, unsigned int mic_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002325{
2326#ifndef CONFIG_NO_STDOUT_DEBUG
2327 u16 key_info = WPA_GET_BE16(key->key_info);
2328
2329 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, " EAPOL-Key type=%d", key->type);
2330 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2331 " key_info 0x%x (ver=%d keyidx=%d rsvd=%d %s%s%s%s%s%s%s%s)",
2332 key_info, key_info & WPA_KEY_INFO_TYPE_MASK,
2333 (key_info & WPA_KEY_INFO_KEY_INDEX_MASK) >>
2334 WPA_KEY_INFO_KEY_INDEX_SHIFT,
2335 (key_info & (BIT(13) | BIT(14) | BIT(15))) >> 13,
2336 key_info & WPA_KEY_INFO_KEY_TYPE ? "Pairwise" : "Group",
2337 key_info & WPA_KEY_INFO_INSTALL ? " Install" : "",
2338 key_info & WPA_KEY_INFO_ACK ? " Ack" : "",
2339 key_info & WPA_KEY_INFO_MIC ? " MIC" : "",
2340 key_info & WPA_KEY_INFO_SECURE ? " Secure" : "",
2341 key_info & WPA_KEY_INFO_ERROR ? " Error" : "",
2342 key_info & WPA_KEY_INFO_REQUEST ? " Request" : "",
2343 key_info & WPA_KEY_INFO_ENCR_KEY_DATA ? " Encr" : "");
2344 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2345 " key_length=%u key_data_length=%u",
Dmitry Shmidt807291d2015-01-27 13:40:23 -08002346 WPA_GET_BE16(key->key_length), key_data_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002347 wpa_hexdump(MSG_DEBUG, " replay_counter",
2348 key->replay_counter, WPA_REPLAY_COUNTER_LEN);
2349 wpa_hexdump(MSG_DEBUG, " key_nonce", key->key_nonce, WPA_NONCE_LEN);
2350 wpa_hexdump(MSG_DEBUG, " key_iv", key->key_iv, 16);
2351 wpa_hexdump(MSG_DEBUG, " key_rsc", key->key_rsc, 8);
2352 wpa_hexdump(MSG_DEBUG, " key_id (reserved)", key->key_id, 8);
Dmitry Shmidt807291d2015-01-27 13:40:23 -08002353 wpa_hexdump(MSG_DEBUG, " key_mic", mic, mic_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002354#endif /* CONFIG_NO_STDOUT_DEBUG */
2355}
2356
2357
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002358#ifdef CONFIG_FILS
2359static int wpa_supp_aead_decrypt(struct wpa_sm *sm, u8 *buf, size_t buf_len,
2360 size_t *key_data_len)
2361{
2362 struct wpa_ptk *ptk;
2363 struct ieee802_1x_hdr *hdr;
2364 struct wpa_eapol_key *key;
2365 u8 *pos, *tmp;
2366 const u8 *aad[1];
2367 size_t aad_len[1];
2368
2369 if (*key_data_len < AES_BLOCK_SIZE) {
2370 wpa_printf(MSG_INFO, "No room for AES-SIV data in the frame");
2371 return -1;
2372 }
2373
2374 if (sm->tptk_set)
2375 ptk = &sm->tptk;
2376 else if (sm->ptk_set)
2377 ptk = &sm->ptk;
2378 else
2379 return -1;
2380
2381 hdr = (struct ieee802_1x_hdr *) buf;
2382 key = (struct wpa_eapol_key *) (hdr + 1);
2383 pos = (u8 *) (key + 1);
2384 pos += 2; /* Pointing at the Encrypted Key Data field */
2385
2386 tmp = os_malloc(*key_data_len);
2387 if (!tmp)
2388 return -1;
2389
2390 /* AES-SIV AAD from EAPOL protocol version field (inclusive) to
2391 * to Key Data (exclusive). */
2392 aad[0] = buf;
2393 aad_len[0] = pos - buf;
2394 if (aes_siv_decrypt(ptk->kek, ptk->kek_len, pos, *key_data_len,
2395 1, aad, aad_len, tmp) < 0) {
2396 wpa_printf(MSG_INFO, "Invalid AES-SIV data in the frame");
2397 bin_clear_free(tmp, *key_data_len);
2398 return -1;
2399 }
2400
2401 /* AEAD decryption and validation completed successfully */
2402 (*key_data_len) -= AES_BLOCK_SIZE;
2403 wpa_hexdump_key(MSG_DEBUG, "WPA: Decrypted Key Data",
2404 tmp, *key_data_len);
2405
2406 /* Replace Key Data field with the decrypted version */
2407 os_memcpy(pos, tmp, *key_data_len);
2408 pos -= 2; /* Key Data Length field */
2409 WPA_PUT_BE16(pos, *key_data_len);
2410 bin_clear_free(tmp, *key_data_len);
2411
2412 if (sm->tptk_set) {
2413 sm->tptk_set = 0;
2414 sm->ptk_set = 1;
2415 os_memcpy(&sm->ptk, &sm->tptk, sizeof(sm->ptk));
2416 os_memset(&sm->tptk, 0, sizeof(sm->tptk));
2417 }
2418
2419 os_memcpy(sm->rx_replay_counter, key->replay_counter,
2420 WPA_REPLAY_COUNTER_LEN);
2421 sm->rx_replay_counter_set = 1;
2422
2423 return 0;
2424}
2425#endif /* CONFIG_FILS */
2426
2427
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002428/**
2429 * wpa_sm_rx_eapol - Process received WPA EAPOL frames
2430 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2431 * @src_addr: Source MAC address of the EAPOL packet
2432 * @buf: Pointer to the beginning of the EAPOL data (EAPOL header)
2433 * @len: Length of the EAPOL frame
2434 * Returns: 1 = WPA EAPOL-Key processed, 0 = not a WPA EAPOL-Key, -1 failure
2435 *
2436 * This function is called for each received EAPOL frame. Other than EAPOL-Key
2437 * frames can be skipped if filtering is done elsewhere. wpa_sm_rx_eapol() is
2438 * only processing WPA and WPA2 EAPOL-Key frames.
2439 *
2440 * The received EAPOL-Key packets are validated and valid packets are replied
2441 * to. In addition, key material (PTK, GTK) is configured at the end of a
2442 * successful key handshake.
2443 */
2444int wpa_sm_rx_eapol(struct wpa_sm *sm, const u8 *src_addr,
2445 const u8 *buf, size_t len)
2446{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002447 size_t plen, data_len, key_data_len;
2448 const struct ieee802_1x_hdr *hdr;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002449 struct wpa_eapol_key *key;
2450 u16 key_info, ver;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002451 u8 *tmp = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002452 int ret = -1;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002453 u8 *mic, *key_data;
Hai Shalom899fcc72020-10-19 14:38:18 -07002454 size_t mic_len, keyhdrlen, pmk_len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002455
2456#ifdef CONFIG_IEEE80211R
2457 sm->ft_completed = 0;
2458#endif /* CONFIG_IEEE80211R */
2459
Hai Shalom899fcc72020-10-19 14:38:18 -07002460 pmk_len = sm->pmk_len;
2461 if (!pmk_len && sm->cur_pmksa)
2462 pmk_len = sm->cur_pmksa->pmk_len;
2463 mic_len = wpa_mic_len(sm->key_mgmt, pmk_len);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002464 keyhdrlen = sizeof(*key) + mic_len + 2;
Dmitry Shmidt807291d2015-01-27 13:40:23 -08002465
2466 if (len < sizeof(*hdr) + keyhdrlen) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002467 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2468 "WPA: EAPOL frame too short to be a WPA "
2469 "EAPOL-Key (len %lu, expecting at least %lu)",
2470 (unsigned long) len,
Dmitry Shmidt807291d2015-01-27 13:40:23 -08002471 (unsigned long) sizeof(*hdr) + keyhdrlen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002472 return 0;
2473 }
2474
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002475 hdr = (const struct ieee802_1x_hdr *) buf;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002476 plen = be_to_host16(hdr->length);
2477 data_len = plen + sizeof(*hdr);
2478 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2479 "IEEE 802.1X RX: version=%d type=%d length=%lu",
2480 hdr->version, hdr->type, (unsigned long) plen);
2481
2482 if (hdr->version < EAPOL_VERSION) {
2483 /* TODO: backwards compatibility */
2484 }
2485 if (hdr->type != IEEE802_1X_TYPE_EAPOL_KEY) {
2486 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2487 "WPA: EAPOL frame (type %u) discarded, "
2488 "not a Key frame", hdr->type);
2489 ret = 0;
2490 goto out;
2491 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002492 wpa_hexdump(MSG_MSGDUMP, "WPA: RX EAPOL-Key", buf, len);
Dmitry Shmidt807291d2015-01-27 13:40:23 -08002493 if (plen > len - sizeof(*hdr) || plen < keyhdrlen) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002494 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2495 "WPA: EAPOL frame payload size %lu "
2496 "invalid (frame size %lu)",
2497 (unsigned long) plen, (unsigned long) len);
2498 ret = 0;
2499 goto out;
2500 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002501 if (data_len < len) {
2502 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2503 "WPA: ignoring %lu bytes after the IEEE 802.1X data",
2504 (unsigned long) len - data_len);
2505 }
2506
2507 /*
2508 * Make a copy of the frame since we need to modify the buffer during
2509 * MAC validation and Key Data decryption.
2510 */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002511 tmp = os_memdup(buf, data_len);
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002512 if (tmp == NULL)
2513 goto out;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002514 key = (struct wpa_eapol_key *) (tmp + sizeof(struct ieee802_1x_hdr));
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002515 mic = (u8 *) (key + 1);
2516 key_data = mic + mic_len + 2;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002517
2518 if (key->type != EAPOL_KEY_TYPE_WPA && key->type != EAPOL_KEY_TYPE_RSN)
2519 {
2520 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2521 "WPA: EAPOL-Key type (%d) unknown, discarded",
2522 key->type);
2523 ret = 0;
2524 goto out;
2525 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002526
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002527 key_data_len = WPA_GET_BE16(mic + mic_len);
2528 wpa_eapol_key_dump(sm, key, key_data_len, mic, mic_len);
Dmitry Shmidt807291d2015-01-27 13:40:23 -08002529
2530 if (key_data_len > plen - keyhdrlen) {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002531 wpa_msg(sm->ctx->msg_ctx, MSG_INFO, "WPA: Invalid EAPOL-Key "
2532 "frame - key_data overflow (%u > %u)",
2533 (unsigned int) key_data_len,
Dmitry Shmidt807291d2015-01-27 13:40:23 -08002534 (unsigned int) (plen - keyhdrlen));
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002535 goto out;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002536 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002537
2538 eapol_sm_notify_lower_layer_success(sm->eapol, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002539 key_info = WPA_GET_BE16(key->key_info);
2540 ver = key_info & WPA_KEY_INFO_TYPE_MASK;
2541 if (ver != WPA_KEY_INFO_TYPE_HMAC_MD5_RC4 &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002542 ver != WPA_KEY_INFO_TYPE_AES_128_CMAC &&
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002543 ver != WPA_KEY_INFO_TYPE_HMAC_SHA1_AES &&
Roshan Pius3a1667e2018-07-03 15:17:14 -07002544 !wpa_use_akm_defined(sm->key_mgmt)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002545 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2546 "WPA: Unsupported EAPOL-Key descriptor version %d",
2547 ver);
2548 goto out;
2549 }
2550
Roshan Pius3a1667e2018-07-03 15:17:14 -07002551 if (wpa_use_akm_defined(sm->key_mgmt) &&
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002552 ver != WPA_KEY_INFO_TYPE_AKM_DEFINED) {
2553 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2554 "RSN: Unsupported EAPOL-Key descriptor version %d (expected AKM defined = 0)",
2555 ver);
2556 goto out;
2557 }
2558
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002559#ifdef CONFIG_IEEE80211R
2560 if (wpa_key_mgmt_ft(sm->key_mgmt)) {
2561 /* IEEE 802.11r uses a new key_info type (AES-128-CMAC). */
Roshan Pius3a1667e2018-07-03 15:17:14 -07002562 if (ver != WPA_KEY_INFO_TYPE_AES_128_CMAC &&
2563 !wpa_use_akm_defined(sm->key_mgmt)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002564 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2565 "FT: AP did not use AES-128-CMAC");
2566 goto out;
2567 }
2568 } else
2569#endif /* CONFIG_IEEE80211R */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002570 if (wpa_key_mgmt_sha256(sm->key_mgmt)) {
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002571 if (ver != WPA_KEY_INFO_TYPE_AES_128_CMAC &&
Roshan Pius3a1667e2018-07-03 15:17:14 -07002572 !wpa_use_akm_defined(sm->key_mgmt)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002573 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2574 "WPA: AP did not use the "
2575 "negotiated AES-128-CMAC");
2576 goto out;
2577 }
Hai Shalomc3565922019-10-28 11:58:20 -07002578 } else if (sm->pairwise_cipher == WPA_CIPHER_CCMP &&
2579 !wpa_use_akm_defined(sm->key_mgmt) &&
2580 ver != WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002581 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2582 "WPA: CCMP is used, but EAPOL-Key "
2583 "descriptor version (%d) is not 2", ver);
2584 if (sm->group_cipher != WPA_CIPHER_CCMP &&
2585 !(key_info & WPA_KEY_INFO_KEY_TYPE)) {
2586 /* Earlier versions of IEEE 802.11i did not explicitly
2587 * require version 2 descriptor for all EAPOL-Key
2588 * packets, so allow group keys to use version 1 if
2589 * CCMP is not used for them. */
2590 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2591 "WPA: Backwards compatibility: allow invalid "
2592 "version for non-CCMP group keys");
Jouni Malinen658fb4a2014-11-14 20:57:05 +02002593 } else if (ver == WPA_KEY_INFO_TYPE_AES_128_CMAC) {
2594 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2595 "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 -07002596 } else
2597 goto out;
Dmitry Shmidt71757432014-06-02 13:50:35 -07002598 } else if (sm->pairwise_cipher == WPA_CIPHER_GCMP &&
Roshan Pius3a1667e2018-07-03 15:17:14 -07002599 !wpa_use_akm_defined(sm->key_mgmt) &&
Dmitry Shmidt71757432014-06-02 13:50:35 -07002600 ver != WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002601 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2602 "WPA: GCMP is used, but EAPOL-Key "
2603 "descriptor version (%d) is not 2", ver);
2604 goto out;
2605 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002606
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002607 if (sm->rx_replay_counter_set &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002608 os_memcmp(key->replay_counter, sm->rx_replay_counter,
2609 WPA_REPLAY_COUNTER_LEN) <= 0) {
2610 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
2611 "WPA: EAPOL-Key Replay Counter did not increase - "
2612 "dropping packet");
2613 goto out;
2614 }
2615
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002616 if (key_info & WPA_KEY_INFO_SMK_MESSAGE) {
2617 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2618 "WPA: Unsupported SMK bit in key_info");
2619 goto out;
2620 }
2621
2622 if (!(key_info & WPA_KEY_INFO_ACK)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002623 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2624 "WPA: No Ack bit in key_info");
2625 goto out;
2626 }
2627
2628 if (key_info & WPA_KEY_INFO_REQUEST) {
2629 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2630 "WPA: EAPOL-Key with Request bit - dropped");
2631 goto out;
2632 }
2633
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002634 if ((key_info & WPA_KEY_INFO_MIC) &&
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002635 wpa_supplicant_verify_eapol_key_mic(sm, key, ver, tmp, data_len))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002636 goto out;
2637
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002638#ifdef CONFIG_FILS
2639 if (!mic_len && (key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
2640 if (wpa_supp_aead_decrypt(sm, tmp, data_len, &key_data_len))
2641 goto out;
2642 }
2643#endif /* CONFIG_FILS */
2644
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002645 if ((sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN) &&
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002646 (key_info & WPA_KEY_INFO_ENCR_KEY_DATA) && mic_len) {
Hai Shalomce48b4a2018-09-05 11:41:35 -07002647 /*
2648 * Only decrypt the Key Data field if the frame's authenticity
2649 * was verified. When using AES-SIV (FILS), the MIC flag is not
2650 * set, so this check should only be performed if mic_len != 0
2651 * which is the case in this code branch.
2652 */
2653 if (!(key_info & WPA_KEY_INFO_MIC)) {
2654 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
2655 "WPA: Ignore EAPOL-Key with encrypted but unauthenticated data");
2656 goto out;
2657 }
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002658 if (wpa_supplicant_decrypt_key_data(sm, key, mic_len,
2659 ver, key_data,
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002660 &key_data_len))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002661 goto out;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002662 }
2663
2664 if (key_info & WPA_KEY_INFO_KEY_TYPE) {
2665 if (key_info & WPA_KEY_INFO_KEY_INDEX_MASK) {
2666 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
2667 "WPA: Ignored EAPOL-Key (Pairwise) with "
2668 "non-zero key index");
2669 goto out;
2670 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002671 if (key_info & (WPA_KEY_INFO_MIC |
2672 WPA_KEY_INFO_ENCR_KEY_DATA)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002673 /* 3/4 4-Way Handshake */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002674 wpa_supplicant_process_3_of_4(sm, key, ver, key_data,
2675 key_data_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002676 } else {
2677 /* 1/4 4-Way Handshake */
2678 wpa_supplicant_process_1_of_4(sm, src_addr, key,
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002679 ver, key_data,
2680 key_data_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002681 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002682 } else {
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002683 if ((mic_len && (key_info & WPA_KEY_INFO_MIC)) ||
2684 (!mic_len && (key_info & WPA_KEY_INFO_ENCR_KEY_DATA))) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002685 /* 1/2 Group Key Handshake */
2686 wpa_supplicant_process_1_of_2(sm, src_addr, key,
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002687 key_data, key_data_len,
2688 ver);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002689 } else {
2690 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002691 "WPA: EAPOL-Key (Group) without Mic/Encr bit - "
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002692 "dropped");
2693 }
2694 }
2695
2696 ret = 1;
2697
2698out:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002699 bin_clear_free(tmp, data_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002700 return ret;
2701}
2702
2703
2704#ifdef CONFIG_CTRL_IFACE
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002705static u32 wpa_key_mgmt_suite(struct wpa_sm *sm)
2706{
2707 switch (sm->key_mgmt) {
2708 case WPA_KEY_MGMT_IEEE8021X:
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002709 return ((sm->proto == WPA_PROTO_RSN ||
2710 sm->proto == WPA_PROTO_OSEN) ?
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002711 RSN_AUTH_KEY_MGMT_UNSPEC_802_1X :
2712 WPA_AUTH_KEY_MGMT_UNSPEC_802_1X);
2713 case WPA_KEY_MGMT_PSK:
2714 return (sm->proto == WPA_PROTO_RSN ?
2715 RSN_AUTH_KEY_MGMT_PSK_OVER_802_1X :
2716 WPA_AUTH_KEY_MGMT_PSK_OVER_802_1X);
2717#ifdef CONFIG_IEEE80211R
2718 case WPA_KEY_MGMT_FT_IEEE8021X:
2719 return RSN_AUTH_KEY_MGMT_FT_802_1X;
2720 case WPA_KEY_MGMT_FT_PSK:
2721 return RSN_AUTH_KEY_MGMT_FT_PSK;
2722#endif /* CONFIG_IEEE80211R */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002723 case WPA_KEY_MGMT_IEEE8021X_SHA256:
2724 return RSN_AUTH_KEY_MGMT_802_1X_SHA256;
2725 case WPA_KEY_MGMT_PSK_SHA256:
2726 return RSN_AUTH_KEY_MGMT_PSK_SHA256;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002727 case WPA_KEY_MGMT_CCKM:
2728 return (sm->proto == WPA_PROTO_RSN ?
2729 RSN_AUTH_KEY_MGMT_CCKM:
2730 WPA_AUTH_KEY_MGMT_CCKM);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002731 case WPA_KEY_MGMT_WPA_NONE:
2732 return WPA_AUTH_KEY_MGMT_NONE;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002733 case WPA_KEY_MGMT_IEEE8021X_SUITE_B:
2734 return RSN_AUTH_KEY_MGMT_802_1X_SUITE_B;
Dmitry Shmidt807291d2015-01-27 13:40:23 -08002735 case WPA_KEY_MGMT_IEEE8021X_SUITE_B_192:
2736 return RSN_AUTH_KEY_MGMT_802_1X_SUITE_B_192;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002737 default:
2738 return 0;
2739 }
2740}
2741
2742
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002743#define RSN_SUITE "%02x-%02x-%02x-%d"
2744#define RSN_SUITE_ARG(s) \
2745((s) >> 24) & 0xff, ((s) >> 16) & 0xff, ((s) >> 8) & 0xff, (s) & 0xff
2746
2747/**
2748 * wpa_sm_get_mib - Dump text list of MIB entries
2749 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2750 * @buf: Buffer for the list
2751 * @buflen: Length of the buffer
2752 * Returns: Number of bytes written to buffer
2753 *
2754 * This function is used fetch dot11 MIB variables.
2755 */
2756int wpa_sm_get_mib(struct wpa_sm *sm, char *buf, size_t buflen)
2757{
2758 char pmkid_txt[PMKID_LEN * 2 + 1];
Hai Shalome21d4e82020-04-29 16:34:06 -07002759 bool rsna;
2760 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002761 size_t len;
2762
2763 if (sm->cur_pmksa) {
2764 wpa_snprintf_hex(pmkid_txt, sizeof(pmkid_txt),
2765 sm->cur_pmksa->pmkid, PMKID_LEN);
2766 } else
2767 pmkid_txt[0] = '\0';
2768
Hai Shalome21d4e82020-04-29 16:34:06 -07002769 rsna = (wpa_key_mgmt_wpa_psk(sm->key_mgmt) ||
2770 wpa_key_mgmt_wpa_ieee8021x(sm->key_mgmt)) &&
2771 sm->proto == WPA_PROTO_RSN;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002772
2773 ret = os_snprintf(buf, buflen,
2774 "dot11RSNAOptionImplemented=TRUE\n"
2775 "dot11RSNAPreauthenticationImplemented=TRUE\n"
2776 "dot11RSNAEnabled=%s\n"
2777 "dot11RSNAPreauthenticationEnabled=%s\n"
2778 "dot11RSNAConfigVersion=%d\n"
2779 "dot11RSNAConfigPairwiseKeysSupported=5\n"
2780 "dot11RSNAConfigGroupCipherSize=%d\n"
2781 "dot11RSNAConfigPMKLifetime=%d\n"
2782 "dot11RSNAConfigPMKReauthThreshold=%d\n"
2783 "dot11RSNAConfigNumberOfPTKSAReplayCounters=1\n"
2784 "dot11RSNAConfigSATimeout=%d\n",
2785 rsna ? "TRUE" : "FALSE",
2786 rsna ? "TRUE" : "FALSE",
2787 RSN_VERSION,
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07002788 wpa_cipher_key_len(sm->group_cipher) * 8,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002789 sm->dot11RSNAConfigPMKLifetime,
2790 sm->dot11RSNAConfigPMKReauthThreshold,
2791 sm->dot11RSNAConfigSATimeout);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002792 if (os_snprintf_error(buflen, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002793 return 0;
2794 len = ret;
2795
2796 ret = os_snprintf(
2797 buf + len, buflen - len,
2798 "dot11RSNAAuthenticationSuiteSelected=" RSN_SUITE "\n"
2799 "dot11RSNAPairwiseCipherSelected=" RSN_SUITE "\n"
2800 "dot11RSNAGroupCipherSelected=" RSN_SUITE "\n"
2801 "dot11RSNAPMKIDUsed=%s\n"
2802 "dot11RSNAAuthenticationSuiteRequested=" RSN_SUITE "\n"
2803 "dot11RSNAPairwiseCipherRequested=" RSN_SUITE "\n"
2804 "dot11RSNAGroupCipherRequested=" RSN_SUITE "\n"
2805 "dot11RSNAConfigNumberOfGTKSAReplayCounters=0\n"
2806 "dot11RSNA4WayHandshakeFailures=%u\n",
2807 RSN_SUITE_ARG(wpa_key_mgmt_suite(sm)),
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07002808 RSN_SUITE_ARG(wpa_cipher_to_suite(sm->proto,
2809 sm->pairwise_cipher)),
2810 RSN_SUITE_ARG(wpa_cipher_to_suite(sm->proto,
2811 sm->group_cipher)),
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002812 pmkid_txt,
2813 RSN_SUITE_ARG(wpa_key_mgmt_suite(sm)),
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07002814 RSN_SUITE_ARG(wpa_cipher_to_suite(sm->proto,
2815 sm->pairwise_cipher)),
2816 RSN_SUITE_ARG(wpa_cipher_to_suite(sm->proto,
2817 sm->group_cipher)),
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002818 sm->dot11RSNA4WayHandshakeFailures);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002819 if (!os_snprintf_error(buflen - len, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002820 len += ret;
2821
2822 return (int) len;
2823}
2824#endif /* CONFIG_CTRL_IFACE */
2825
2826
2827static void wpa_sm_pmksa_free_cb(struct rsn_pmksa_cache_entry *entry,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002828 void *ctx, enum pmksa_free_reason reason)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002829{
2830 struct wpa_sm *sm = ctx;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002831 int deauth = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002832
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002833 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "RSN: PMKSA cache entry free_cb: "
2834 MACSTR " reason=%d", MAC2STR(entry->aa), reason);
2835
2836 if (sm->cur_pmksa == entry) {
2837 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2838 "RSN: %s current PMKSA entry",
2839 reason == PMKSA_REPLACE ? "replaced" : "removed");
2840 pmksa_cache_clear_current(sm);
2841
2842 /*
2843 * If an entry is simply being replaced, there's no need to
2844 * deauthenticate because it will be immediately re-added.
2845 * This happens when EAP authentication is completed again
2846 * (reauth or failed PMKSA caching attempt).
2847 */
2848 if (reason != PMKSA_REPLACE)
2849 deauth = 1;
2850 }
2851
2852 if (reason == PMKSA_EXPIRE &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002853 (sm->pmk_len == entry->pmk_len &&
2854 os_memcmp(sm->pmk, entry->pmk, sm->pmk_len) == 0)) {
2855 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002856 "RSN: deauthenticating due to expired PMK");
2857 pmksa_cache_clear_current(sm);
2858 deauth = 1;
2859 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002860
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002861 if (deauth) {
Roshan Pius3a1667e2018-07-03 15:17:14 -07002862 sm->pmk_len = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002863 os_memset(sm->pmk, 0, sizeof(sm->pmk));
2864 wpa_sm_deauthenticate(sm, WLAN_REASON_UNSPECIFIED);
2865 }
2866}
2867
2868
2869/**
2870 * wpa_sm_init - Initialize WPA state machine
2871 * @ctx: Context pointer for callbacks; this needs to be an allocated buffer
2872 * Returns: Pointer to the allocated WPA state machine data
2873 *
2874 * This function is used to allocate a new WPA state machine and the returned
2875 * value is passed to all WPA state machine calls.
2876 */
2877struct wpa_sm * wpa_sm_init(struct wpa_sm_ctx *ctx)
2878{
2879 struct wpa_sm *sm;
2880
2881 sm = os_zalloc(sizeof(*sm));
2882 if (sm == NULL)
2883 return NULL;
2884 dl_list_init(&sm->pmksa_candidates);
2885 sm->renew_snonce = 1;
2886 sm->ctx = ctx;
2887
2888 sm->dot11RSNAConfigPMKLifetime = 43200;
2889 sm->dot11RSNAConfigPMKReauthThreshold = 70;
2890 sm->dot11RSNAConfigSATimeout = 60;
2891
2892 sm->pmksa = pmksa_cache_init(wpa_sm_pmksa_free_cb, sm, sm);
2893 if (sm->pmksa == NULL) {
2894 wpa_msg(sm->ctx->msg_ctx, MSG_ERROR,
2895 "RSN: PMKSA cache initialization failed");
2896 os_free(sm);
2897 return NULL;
2898 }
2899
2900 return sm;
2901}
2902
2903
2904/**
2905 * wpa_sm_deinit - Deinitialize WPA state machine
2906 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2907 */
2908void wpa_sm_deinit(struct wpa_sm *sm)
2909{
2910 if (sm == NULL)
2911 return;
2912 pmksa_cache_deinit(sm->pmksa);
2913 eloop_cancel_timeout(wpa_sm_start_preauth, sm, NULL);
2914 eloop_cancel_timeout(wpa_sm_rekey_ptk, sm, NULL);
2915 os_free(sm->assoc_wpa_ie);
Hai Shalomc3565922019-10-28 11:58:20 -07002916 os_free(sm->assoc_rsnxe);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002917 os_free(sm->ap_wpa_ie);
2918 os_free(sm->ap_rsn_ie);
Hai Shalomc3565922019-10-28 11:58:20 -07002919 os_free(sm->ap_rsnxe);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002920 wpa_sm_drop_sa(sm);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002921 os_free(sm->ctx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002922#ifdef CONFIG_IEEE80211R
2923 os_free(sm->assoc_resp_ies);
2924#endif /* CONFIG_IEEE80211R */
Dmitry Shmidt55840ad2015-12-14 12:45:46 -08002925#ifdef CONFIG_TESTING_OPTIONS
2926 wpabuf_free(sm->test_assoc_ie);
2927#endif /* CONFIG_TESTING_OPTIONS */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002928#ifdef CONFIG_FILS_SK_PFS
2929 crypto_ecdh_deinit(sm->fils_ecdh);
2930#endif /* CONFIG_FILS_SK_PFS */
2931#ifdef CONFIG_FILS
2932 wpabuf_free(sm->fils_ft_ies);
2933#endif /* CONFIG_FILS */
2934#ifdef CONFIG_OWE
2935 crypto_ecdh_deinit(sm->owe_ecdh);
2936#endif /* CONFIG_OWE */
Hai Shalom021b0b52019-04-10 11:17:58 -07002937#ifdef CONFIG_DPP2
2938 wpabuf_clear_free(sm->dpp_z);
2939#endif /* CONFIG_DPP2 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002940 os_free(sm);
2941}
2942
2943
2944/**
2945 * wpa_sm_notify_assoc - Notify WPA state machine about association
2946 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2947 * @bssid: The BSSID of the new association
2948 *
2949 * This function is called to let WPA state machine know that the connection
2950 * was established.
2951 */
2952void wpa_sm_notify_assoc(struct wpa_sm *sm, const u8 *bssid)
2953{
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02002954 int clear_keys = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002955
2956 if (sm == NULL)
2957 return;
2958
2959 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2960 "WPA: Association event - clear replay counter");
2961 os_memcpy(sm->bssid, bssid, ETH_ALEN);
2962 os_memset(sm->rx_replay_counter, 0, WPA_REPLAY_COUNTER_LEN);
2963 sm->rx_replay_counter_set = 0;
2964 sm->renew_snonce = 1;
2965 if (os_memcmp(sm->preauth_bssid, bssid, ETH_ALEN) == 0)
2966 rsn_preauth_deinit(sm);
2967
2968#ifdef CONFIG_IEEE80211R
2969 if (wpa_ft_is_completed(sm)) {
2970 /*
2971 * Clear portValid to kick EAPOL state machine to re-enter
2972 * AUTHENTICATED state to get the EAPOL port Authorized.
2973 */
Hai Shalome21d4e82020-04-29 16:34:06 -07002974 eapol_sm_notify_portValid(sm->eapol, false);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002975 wpa_supplicant_key_neg_complete(sm, sm->bssid, 1);
2976
2977 /* Prepare for the next transition */
2978 wpa_ft_prepare_auth_request(sm, NULL);
2979
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02002980 clear_keys = 0;
Hai Shalom81f62d82019-07-22 12:10:00 -07002981 sm->ft_protocol = 1;
2982 } else {
2983 sm->ft_protocol = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002984 }
2985#endif /* CONFIG_IEEE80211R */
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002986#ifdef CONFIG_FILS
2987 if (sm->fils_completed) {
2988 /*
2989 * Clear portValid to kick EAPOL state machine to re-enter
2990 * AUTHENTICATED state to get the EAPOL port Authorized.
2991 */
2992 wpa_supplicant_key_neg_complete(sm, sm->bssid, 1);
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02002993 clear_keys = 0;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002994 }
2995#endif /* CONFIG_FILS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002996
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02002997 if (clear_keys) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002998 /*
2999 * IEEE 802.11, 8.4.10: Delete PTK SA on (re)association if
3000 * this is not part of a Fast BSS Transition.
3001 */
3002 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Clear old PTK");
3003 sm->ptk_set = 0;
Dmitry Shmidt61593f02014-04-21 16:27:35 -07003004 os_memset(&sm->ptk, 0, sizeof(sm->ptk));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003005 sm->tptk_set = 0;
Dmitry Shmidt61593f02014-04-21 16:27:35 -07003006 os_memset(&sm->tptk, 0, sizeof(sm->tptk));
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02003007 os_memset(&sm->gtk, 0, sizeof(sm->gtk));
Jouni Malinen58c0e962017-10-01 12:12:24 +03003008 os_memset(&sm->gtk_wnm_sleep, 0, sizeof(sm->gtk_wnm_sleep));
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02003009 os_memset(&sm->igtk, 0, sizeof(sm->igtk));
Jouni Malinen58c0e962017-10-01 12:12:24 +03003010 os_memset(&sm->igtk_wnm_sleep, 0, sizeof(sm->igtk_wnm_sleep));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003011 }
3012
3013#ifdef CONFIG_TDLS
3014 wpa_tdls_assoc(sm);
3015#endif /* CONFIG_TDLS */
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003016
3017#ifdef CONFIG_P2P
3018 os_memset(sm->p2p_ip_addr, 0, sizeof(sm->p2p_ip_addr));
3019#endif /* CONFIG_P2P */
Hai Shalomfdcde762020-04-02 11:19:20 -07003020
3021 sm->keyidx_active = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003022}
3023
3024
3025/**
3026 * wpa_sm_notify_disassoc - Notify WPA state machine about disassociation
3027 * @sm: Pointer to WPA state machine data from wpa_sm_init()
3028 *
3029 * This function is called to let WPA state machine know that the connection
3030 * was lost. This will abort any existing pre-authentication session.
3031 */
3032void wpa_sm_notify_disassoc(struct wpa_sm *sm)
3033{
Dmitry Shmidt4dd28dc2015-03-10 11:21:43 -07003034 eloop_cancel_timeout(wpa_sm_start_preauth, sm, NULL);
3035 eloop_cancel_timeout(wpa_sm_rekey_ptk, sm, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003036 rsn_preauth_deinit(sm);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003037 pmksa_cache_clear_current(sm);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003038 if (wpa_sm_get_state(sm) == WPA_4WAY_HANDSHAKE)
3039 sm->dot11RSNA4WayHandshakeFailures++;
3040#ifdef CONFIG_TDLS
3041 wpa_tdls_disassoc(sm);
3042#endif /* CONFIG_TDLS */
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003043#ifdef CONFIG_FILS
3044 sm->fils_completed = 0;
3045#endif /* CONFIG_FILS */
Jouni Malinen4283f9e2017-09-22 12:06:37 +03003046#ifdef CONFIG_IEEE80211R
3047 sm->ft_reassoc_completed = 0;
Hai Shalom81f62d82019-07-22 12:10:00 -07003048 sm->ft_protocol = 0;
Jouni Malinen4283f9e2017-09-22 12:06:37 +03003049#endif /* CONFIG_IEEE80211R */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003050
3051 /* Keys are not needed in the WPA state machine anymore */
3052 wpa_sm_drop_sa(sm);
Hai Shalomfdcde762020-04-02 11:19:20 -07003053 sm->keyidx_active = 0;
Dmitry Shmidt4dd28dc2015-03-10 11:21:43 -07003054
3055 sm->msg_3_of_4_ok = 0;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003056 os_memset(sm->bssid, 0, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003057}
3058
3059
3060/**
3061 * wpa_sm_set_pmk - Set PMK
3062 * @sm: Pointer to WPA state machine data from wpa_sm_init()
3063 * @pmk: The new PMK
3064 * @pmk_len: The length of the new PMK in bytes
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08003065 * @pmkid: Calculated PMKID
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003066 * @bssid: AA to add into PMKSA cache or %NULL to not cache the PMK
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003067 *
3068 * Configure the PMK for WPA state machine.
3069 */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003070void wpa_sm_set_pmk(struct wpa_sm *sm, const u8 *pmk, size_t pmk_len,
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08003071 const u8 *pmkid, const u8 *bssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003072{
3073 if (sm == NULL)
3074 return;
3075
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003076 wpa_hexdump_key(MSG_DEBUG, "WPA: Set PMK based on external data",
3077 pmk, pmk_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003078 sm->pmk_len = pmk_len;
3079 os_memcpy(sm->pmk, pmk, pmk_len);
3080
3081#ifdef CONFIG_IEEE80211R
3082 /* Set XXKey to be PSK for FT key derivation */
3083 sm->xxkey_len = pmk_len;
3084 os_memcpy(sm->xxkey, pmk, pmk_len);
3085#endif /* CONFIG_IEEE80211R */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003086
3087 if (bssid) {
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08003088 pmksa_cache_add(sm->pmksa, pmk, pmk_len, pmkid, NULL, 0,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003089 bssid, sm->own_addr,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003090 sm->network_ctx, sm->key_mgmt, NULL);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003091 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003092}
3093
3094
3095/**
3096 * wpa_sm_set_pmk_from_pmksa - Set PMK based on the current PMKSA
3097 * @sm: Pointer to WPA state machine data from wpa_sm_init()
3098 *
3099 * Take the PMK from the current PMKSA into use. If no PMKSA is active, the PMK
3100 * will be cleared.
3101 */
3102void wpa_sm_set_pmk_from_pmksa(struct wpa_sm *sm)
3103{
3104 if (sm == NULL)
3105 return;
3106
3107 if (sm->cur_pmksa) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003108 wpa_hexdump_key(MSG_DEBUG,
3109 "WPA: Set PMK based on current PMKSA",
3110 sm->cur_pmksa->pmk, sm->cur_pmksa->pmk_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003111 sm->pmk_len = sm->cur_pmksa->pmk_len;
3112 os_memcpy(sm->pmk, sm->cur_pmksa->pmk, sm->pmk_len);
3113 } else {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003114 wpa_printf(MSG_DEBUG, "WPA: No current PMKSA - clear PMK");
3115 sm->pmk_len = 0;
3116 os_memset(sm->pmk, 0, PMK_LEN_MAX);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003117 }
3118}
3119
3120
3121/**
3122 * wpa_sm_set_fast_reauth - Set fast reauthentication (EAP) enabled/disabled
3123 * @sm: Pointer to WPA state machine data from wpa_sm_init()
3124 * @fast_reauth: Whether fast reauthentication (EAP) is allowed
3125 */
3126void wpa_sm_set_fast_reauth(struct wpa_sm *sm, int fast_reauth)
3127{
3128 if (sm)
3129 sm->fast_reauth = fast_reauth;
3130}
3131
3132
3133/**
3134 * wpa_sm_set_scard_ctx - Set context pointer for smartcard callbacks
3135 * @sm: Pointer to WPA state machine data from wpa_sm_init()
3136 * @scard_ctx: Context pointer for smartcard related callback functions
3137 */
3138void wpa_sm_set_scard_ctx(struct wpa_sm *sm, void *scard_ctx)
3139{
3140 if (sm == NULL)
3141 return;
3142 sm->scard_ctx = scard_ctx;
3143 if (sm->preauth_eapol)
3144 eapol_sm_register_scard_ctx(sm->preauth_eapol, scard_ctx);
3145}
3146
3147
3148/**
Hai Shalomfdcde762020-04-02 11:19:20 -07003149 * wpa_sm_set_config - Notification of current configuration change
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003150 * @sm: Pointer to WPA state machine data from wpa_sm_init()
3151 * @config: Pointer to current network configuration
3152 *
3153 * Notify WPA state machine that configuration has changed. config will be
3154 * stored as a backpointer to network configuration. This can be %NULL to clear
3155 * the stored pointed.
3156 */
3157void wpa_sm_set_config(struct wpa_sm *sm, struct rsn_supp_config *config)
3158{
3159 if (!sm)
3160 return;
3161
3162 if (config) {
3163 sm->network_ctx = config->network_ctx;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003164 sm->allowed_pairwise_cipher = config->allowed_pairwise_cipher;
3165 sm->proactive_key_caching = config->proactive_key_caching;
3166 sm->eap_workaround = config->eap_workaround;
3167 sm->eap_conf_ctx = config->eap_conf_ctx;
3168 if (config->ssid) {
3169 os_memcpy(sm->ssid, config->ssid, config->ssid_len);
3170 sm->ssid_len = config->ssid_len;
3171 } else
3172 sm->ssid_len = 0;
3173 sm->wpa_ptk_rekey = config->wpa_ptk_rekey;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003174 sm->p2p = config->p2p;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003175 sm->wpa_rsc_relaxation = config->wpa_rsc_relaxation;
Hai Shalomfdcde762020-04-02 11:19:20 -07003176 sm->owe_ptk_workaround = config->owe_ptk_workaround;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003177#ifdef CONFIG_FILS
3178 if (config->fils_cache_id) {
3179 sm->fils_cache_id_set = 1;
3180 os_memcpy(sm->fils_cache_id, config->fils_cache_id,
3181 FILS_CACHE_ID_LEN);
3182 } else {
3183 sm->fils_cache_id_set = 0;
3184 }
3185#endif /* CONFIG_FILS */
Hai Shalomfdcde762020-04-02 11:19:20 -07003186 sm->beacon_prot = config->beacon_prot;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003187 } else {
3188 sm->network_ctx = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003189 sm->allowed_pairwise_cipher = 0;
3190 sm->proactive_key_caching = 0;
3191 sm->eap_workaround = 0;
3192 sm->eap_conf_ctx = NULL;
3193 sm->ssid_len = 0;
3194 sm->wpa_ptk_rekey = 0;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003195 sm->p2p = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003196 sm->wpa_rsc_relaxation = 0;
Hai Shalomfdcde762020-04-02 11:19:20 -07003197 sm->owe_ptk_workaround = 0;
3198 sm->beacon_prot = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003199 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003200}
3201
3202
3203/**
3204 * wpa_sm_set_own_addr - Set own MAC address
3205 * @sm: Pointer to WPA state machine data from wpa_sm_init()
3206 * @addr: Own MAC address
3207 */
3208void wpa_sm_set_own_addr(struct wpa_sm *sm, const u8 *addr)
3209{
3210 if (sm)
3211 os_memcpy(sm->own_addr, addr, ETH_ALEN);
3212}
3213
3214
3215/**
3216 * wpa_sm_set_ifname - Set network interface name
3217 * @sm: Pointer to WPA state machine data from wpa_sm_init()
3218 * @ifname: Interface name
3219 * @bridge_ifname: Optional bridge interface name (for pre-auth)
3220 */
3221void wpa_sm_set_ifname(struct wpa_sm *sm, const char *ifname,
3222 const char *bridge_ifname)
3223{
3224 if (sm) {
3225 sm->ifname = ifname;
3226 sm->bridge_ifname = bridge_ifname;
3227 }
3228}
3229
3230
3231/**
3232 * wpa_sm_set_eapol - Set EAPOL state machine pointer
3233 * @sm: Pointer to WPA state machine data from wpa_sm_init()
3234 * @eapol: Pointer to EAPOL state machine allocated with eapol_sm_init()
3235 */
3236void wpa_sm_set_eapol(struct wpa_sm *sm, struct eapol_sm *eapol)
3237{
3238 if (sm)
3239 sm->eapol = eapol;
3240}
3241
3242
3243/**
3244 * wpa_sm_set_param - Set WPA state machine parameters
3245 * @sm: Pointer to WPA state machine data from wpa_sm_init()
3246 * @param: Parameter field
3247 * @value: Parameter value
3248 * Returns: 0 on success, -1 on failure
3249 */
3250int wpa_sm_set_param(struct wpa_sm *sm, enum wpa_sm_conf_params param,
3251 unsigned int value)
3252{
3253 int ret = 0;
3254
3255 if (sm == NULL)
3256 return -1;
3257
3258 switch (param) {
3259 case RSNA_PMK_LIFETIME:
3260 if (value > 0)
3261 sm->dot11RSNAConfigPMKLifetime = value;
3262 else
3263 ret = -1;
3264 break;
3265 case RSNA_PMK_REAUTH_THRESHOLD:
3266 if (value > 0 && value <= 100)
3267 sm->dot11RSNAConfigPMKReauthThreshold = value;
3268 else
3269 ret = -1;
3270 break;
3271 case RSNA_SA_TIMEOUT:
3272 if (value > 0)
3273 sm->dot11RSNAConfigSATimeout = value;
3274 else
3275 ret = -1;
3276 break;
3277 case WPA_PARAM_PROTO:
3278 sm->proto = value;
3279 break;
3280 case WPA_PARAM_PAIRWISE:
3281 sm->pairwise_cipher = value;
3282 break;
3283 case WPA_PARAM_GROUP:
3284 sm->group_cipher = value;
3285 break;
3286 case WPA_PARAM_KEY_MGMT:
3287 sm->key_mgmt = value;
3288 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003289 case WPA_PARAM_MGMT_GROUP:
3290 sm->mgmt_group_cipher = value;
3291 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003292 case WPA_PARAM_RSN_ENABLED:
3293 sm->rsn_enabled = value;
3294 break;
3295 case WPA_PARAM_MFP:
3296 sm->mfp = value;
3297 break;
Hai Shalom74f70d42019-02-11 14:42:39 -08003298 case WPA_PARAM_OCV:
3299 sm->ocv = value;
3300 break;
Hai Shalomc3565922019-10-28 11:58:20 -07003301 case WPA_PARAM_SAE_PWE:
3302 sm->sae_pwe = value;
3303 break;
Hai Shalom899fcc72020-10-19 14:38:18 -07003304 case WPA_PARAM_SAE_PK:
3305 sm->sae_pk = value;
3306 break;
Hai Shalomfdcde762020-04-02 11:19:20 -07003307 case WPA_PARAM_DENY_PTK0_REKEY:
3308 sm->wpa_deny_ptk0_rekey = value;
3309 break;
3310 case WPA_PARAM_EXT_KEY_ID:
3311 sm->ext_key_id = value;
3312 break;
3313 case WPA_PARAM_USE_EXT_KEY_ID:
3314 sm->use_ext_key_id = value;
3315 break;
Hai Shalomb755a2a2020-04-23 21:49:02 -07003316#ifdef CONFIG_TESTING_OPTIONS
3317 case WPA_PARAM_FT_RSNXE_USED:
3318 sm->ft_rsnxe_used = value;
3319 break;
Hai Shalom899fcc72020-10-19 14:38:18 -07003320 case WPA_PARAM_OCI_FREQ_EAPOL:
3321 sm->oci_freq_override_eapol = value;
3322 break;
3323 case WPA_PARAM_OCI_FREQ_EAPOL_G2:
3324 sm->oci_freq_override_eapol_g2 = value;
3325 break;
3326 case WPA_PARAM_OCI_FREQ_FT_ASSOC:
3327 sm->oci_freq_override_ft_assoc = value;
3328 break;
3329 case WPA_PARAM_OCI_FREQ_FILS_ASSOC:
3330 sm->oci_freq_override_fils_assoc = value;
3331 break;
Hai Shalomb755a2a2020-04-23 21:49:02 -07003332#endif /* CONFIG_TESTING_OPTIONS */
Hai Shalom4fbc08f2020-05-18 12:37:00 -07003333#ifdef CONFIG_DPP2
3334 case WPA_PARAM_DPP_PFS:
3335 sm->dpp_pfs = value;
3336 break;
3337#endif /* CONFIG_DPP2 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003338 default:
3339 break;
3340 }
3341
3342 return ret;
3343}
3344
3345
3346/**
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003347 * wpa_sm_get_status - Get WPA state machine
3348 * @sm: Pointer to WPA state machine data from wpa_sm_init()
3349 * @buf: Buffer for status information
3350 * @buflen: Maximum buffer length
3351 * @verbose: Whether to include verbose status information
3352 * Returns: Number of bytes written to buf.
3353 *
3354 * Query WPA state machine for status information. This function fills in
3355 * a text area with current status information. If the buffer (buf) is not
3356 * large enough, status information will be truncated to fit the buffer.
3357 */
3358int wpa_sm_get_status(struct wpa_sm *sm, char *buf, size_t buflen,
3359 int verbose)
3360{
3361 char *pos = buf, *end = buf + buflen;
3362 int ret;
3363
3364 ret = os_snprintf(pos, end - pos,
3365 "pairwise_cipher=%s\n"
3366 "group_cipher=%s\n"
3367 "key_mgmt=%s\n",
3368 wpa_cipher_txt(sm->pairwise_cipher),
3369 wpa_cipher_txt(sm->group_cipher),
3370 wpa_key_mgmt_txt(sm->key_mgmt, sm->proto));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003371 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003372 return pos - buf;
3373 pos += ret;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003374
Hai Shalom4fbc08f2020-05-18 12:37:00 -07003375#ifdef CONFIG_DPP2
3376 if (sm->key_mgmt == WPA_KEY_MGMT_DPP && sm->dpp_z) {
3377 ret = os_snprintf(pos, end - pos, "dpp_pfs=1\n");
3378 if (os_snprintf_error(end - pos, ret))
3379 return pos - buf;
3380 pos += ret;
3381 }
3382#endif /* CONFIG_DPP2 */
3383
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003384 if (sm->mfp != NO_MGMT_FRAME_PROTECTION && sm->ap_rsn_ie) {
3385 struct wpa_ie_data rsn;
3386 if (wpa_parse_wpa_ie_rsn(sm->ap_rsn_ie, sm->ap_rsn_ie_len, &rsn)
3387 >= 0 &&
3388 rsn.capabilities & (WPA_CAPABILITY_MFPR |
3389 WPA_CAPABILITY_MFPC)) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003390 ret = os_snprintf(pos, end - pos, "pmf=%d\n"
3391 "mgmt_group_cipher=%s\n",
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003392 (rsn.capabilities &
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003393 WPA_CAPABILITY_MFPR) ? 2 : 1,
3394 wpa_cipher_txt(
3395 sm->mgmt_group_cipher));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003396 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003397 return pos - buf;
3398 pos += ret;
3399 }
3400 }
3401
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003402 return pos - buf;
3403}
3404
3405
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07003406int wpa_sm_pmf_enabled(struct wpa_sm *sm)
3407{
3408 struct wpa_ie_data rsn;
3409
3410 if (sm->mfp == NO_MGMT_FRAME_PROTECTION || !sm->ap_rsn_ie)
3411 return 0;
3412
3413 if (wpa_parse_wpa_ie_rsn(sm->ap_rsn_ie, sm->ap_rsn_ie_len, &rsn) >= 0 &&
3414 rsn.capabilities & (WPA_CAPABILITY_MFPR | WPA_CAPABILITY_MFPC))
3415 return 1;
3416
3417 return 0;
3418}
3419
3420
Hai Shalomfdcde762020-04-02 11:19:20 -07003421int wpa_sm_ext_key_id(struct wpa_sm *sm)
3422{
3423 return sm ? sm->ext_key_id : 0;
3424}
3425
3426
3427int wpa_sm_ext_key_id_active(struct wpa_sm *sm)
3428{
3429 return sm ? sm->use_ext_key_id : 0;
3430}
3431
3432
Hai Shalom74f70d42019-02-11 14:42:39 -08003433int wpa_sm_ocv_enabled(struct wpa_sm *sm)
3434{
3435 struct wpa_ie_data rsn;
3436
3437 if (!sm->ocv || !sm->ap_rsn_ie)
3438 return 0;
3439
3440 return wpa_parse_wpa_ie_rsn(sm->ap_rsn_ie, sm->ap_rsn_ie_len,
3441 &rsn) >= 0 &&
3442 (rsn.capabilities & WPA_CAPABILITY_OCVC);
3443}
3444
3445
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003446/**
3447 * wpa_sm_set_assoc_wpa_ie_default - Generate own WPA/RSN IE from configuration
3448 * @sm: Pointer to WPA state machine data from wpa_sm_init()
3449 * @wpa_ie: Pointer to buffer for WPA/RSN IE
3450 * @wpa_ie_len: Pointer to the length of the wpa_ie buffer
3451 * Returns: 0 on success, -1 on failure
3452 */
3453int wpa_sm_set_assoc_wpa_ie_default(struct wpa_sm *sm, u8 *wpa_ie,
3454 size_t *wpa_ie_len)
3455{
3456 int res;
3457
3458 if (sm == NULL)
3459 return -1;
3460
Dmitry Shmidt55840ad2015-12-14 12:45:46 -08003461#ifdef CONFIG_TESTING_OPTIONS
3462 if (sm->test_assoc_ie) {
3463 wpa_printf(MSG_DEBUG,
3464 "TESTING: Replace association WPA/RSN IE");
3465 if (*wpa_ie_len < wpabuf_len(sm->test_assoc_ie))
3466 return -1;
3467 os_memcpy(wpa_ie, wpabuf_head(sm->test_assoc_ie),
3468 wpabuf_len(sm->test_assoc_ie));
3469 res = wpabuf_len(sm->test_assoc_ie);
3470 } else
3471#endif /* CONFIG_TESTING_OPTIONS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003472 res = wpa_gen_wpa_ie(sm, wpa_ie, *wpa_ie_len);
3473 if (res < 0)
3474 return -1;
3475 *wpa_ie_len = res;
3476
3477 wpa_hexdump(MSG_DEBUG, "WPA: Set own WPA IE default",
3478 wpa_ie, *wpa_ie_len);
3479
3480 if (sm->assoc_wpa_ie == NULL) {
3481 /*
3482 * Make a copy of the WPA/RSN IE so that 4-Way Handshake gets
3483 * the correct version of the IE even if PMKSA caching is
3484 * aborted (which would remove PMKID from IE generation).
3485 */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003486 sm->assoc_wpa_ie = os_memdup(wpa_ie, *wpa_ie_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003487 if (sm->assoc_wpa_ie == NULL)
3488 return -1;
3489
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003490 sm->assoc_wpa_ie_len = *wpa_ie_len;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003491 } else {
3492 wpa_hexdump(MSG_DEBUG,
3493 "WPA: Leave previously set WPA IE default",
3494 sm->assoc_wpa_ie, sm->assoc_wpa_ie_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003495 }
3496
3497 return 0;
3498}
3499
3500
3501/**
3502 * wpa_sm_set_assoc_wpa_ie - Set own WPA/RSN IE from (Re)AssocReq
3503 * @sm: Pointer to WPA state machine data from wpa_sm_init()
3504 * @ie: Pointer to IE data (starting from id)
3505 * @len: IE length
3506 * Returns: 0 on success, -1 on failure
3507 *
3508 * Inform WPA state machine about the WPA/RSN IE used in (Re)Association
3509 * Request frame. The IE will be used to override the default value generated
3510 * with wpa_sm_set_assoc_wpa_ie_default().
3511 */
3512int wpa_sm_set_assoc_wpa_ie(struct wpa_sm *sm, const u8 *ie, size_t len)
3513{
3514 if (sm == NULL)
3515 return -1;
3516
3517 os_free(sm->assoc_wpa_ie);
3518 if (ie == NULL || len == 0) {
3519 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
3520 "WPA: clearing own WPA/RSN IE");
3521 sm->assoc_wpa_ie = NULL;
3522 sm->assoc_wpa_ie_len = 0;
3523 } else {
3524 wpa_hexdump(MSG_DEBUG, "WPA: set own WPA/RSN IE", ie, len);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003525 sm->assoc_wpa_ie = os_memdup(ie, len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003526 if (sm->assoc_wpa_ie == NULL)
3527 return -1;
3528
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003529 sm->assoc_wpa_ie_len = len;
3530 }
3531
3532 return 0;
3533}
3534
3535
3536/**
Hai Shalomc3565922019-10-28 11:58:20 -07003537 * wpa_sm_set_assoc_rsnxe_default - Generate own RSNXE from configuration
3538 * @sm: Pointer to WPA state machine data from wpa_sm_init()
3539 * @rsnxe: Pointer to buffer for RSNXE
3540 * @rsnxe_len: Pointer to the length of the rsne buffer
3541 * Returns: 0 on success, -1 on failure
3542 */
3543int wpa_sm_set_assoc_rsnxe_default(struct wpa_sm *sm, u8 *rsnxe,
3544 size_t *rsnxe_len)
3545{
3546 int res;
3547
3548 if (!sm)
3549 return -1;
3550
3551 res = wpa_gen_rsnxe(sm, rsnxe, *rsnxe_len);
3552 if (res < 0)
3553 return -1;
3554 *rsnxe_len = res;
3555
3556 wpa_hexdump(MSG_DEBUG, "RSN: Set own RSNXE default", rsnxe, *rsnxe_len);
3557
3558 if (sm->assoc_rsnxe) {
3559 wpa_hexdump(MSG_DEBUG,
3560 "RSN: Leave previously set RSNXE default",
3561 sm->assoc_rsnxe, sm->assoc_rsnxe_len);
3562 } else if (*rsnxe_len > 0) {
3563 /*
3564 * Make a copy of the RSNXE so that 4-Way Handshake gets the
3565 * correct version of the IE even if it gets changed.
3566 */
3567 sm->assoc_rsnxe = os_memdup(rsnxe, *rsnxe_len);
3568 if (!sm->assoc_rsnxe)
3569 return -1;
3570
3571 sm->assoc_rsnxe_len = *rsnxe_len;
3572 }
3573
3574 return 0;
3575}
3576
3577
3578/**
3579 * wpa_sm_set_assoc_rsnxe - Set own RSNXE from (Re)AssocReq
3580 * @sm: Pointer to WPA state machine data from wpa_sm_init()
3581 * @ie: Pointer to IE data (starting from id)
3582 * @len: IE length
3583 * Returns: 0 on success, -1 on failure
3584 *
3585 * Inform WPA state machine about the RSNXE used in (Re)Association Request
3586 * frame. The IE will be used to override the default value generated
3587 * with wpa_sm_set_assoc_rsnxe_default().
3588 */
3589int wpa_sm_set_assoc_rsnxe(struct wpa_sm *sm, const u8 *ie, size_t len)
3590{
3591 if (!sm)
3592 return -1;
3593
3594 os_free(sm->assoc_rsnxe);
3595 if (!ie || len == 0) {
3596 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
3597 "RSN: clearing own RSNXE");
3598 sm->assoc_rsnxe = NULL;
3599 sm->assoc_rsnxe_len = 0;
3600 } else {
3601 wpa_hexdump(MSG_DEBUG, "RSN: set own RSNXE", ie, len);
3602 sm->assoc_rsnxe = os_memdup(ie, len);
3603 if (!sm->assoc_rsnxe)
3604 return -1;
3605
3606 sm->assoc_rsnxe_len = len;
3607 }
3608
3609 return 0;
3610}
3611
3612
3613/**
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003614 * wpa_sm_set_ap_wpa_ie - Set AP WPA IE from Beacon/ProbeResp
3615 * @sm: Pointer to WPA state machine data from wpa_sm_init()
3616 * @ie: Pointer to IE data (starting from id)
3617 * @len: IE length
3618 * Returns: 0 on success, -1 on failure
3619 *
3620 * Inform WPA state machine about the WPA IE used in Beacon / Probe Response
3621 * frame.
3622 */
3623int wpa_sm_set_ap_wpa_ie(struct wpa_sm *sm, const u8 *ie, size_t len)
3624{
3625 if (sm == NULL)
3626 return -1;
3627
3628 os_free(sm->ap_wpa_ie);
3629 if (ie == NULL || len == 0) {
3630 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
3631 "WPA: clearing AP WPA IE");
3632 sm->ap_wpa_ie = NULL;
3633 sm->ap_wpa_ie_len = 0;
3634 } else {
3635 wpa_hexdump(MSG_DEBUG, "WPA: set AP WPA IE", ie, len);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003636 sm->ap_wpa_ie = os_memdup(ie, len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003637 if (sm->ap_wpa_ie == NULL)
3638 return -1;
3639
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003640 sm->ap_wpa_ie_len = len;
3641 }
3642
3643 return 0;
3644}
3645
3646
3647/**
3648 * wpa_sm_set_ap_rsn_ie - Set AP RSN IE from Beacon/ProbeResp
3649 * @sm: Pointer to WPA state machine data from wpa_sm_init()
3650 * @ie: Pointer to IE data (starting from id)
3651 * @len: IE length
3652 * Returns: 0 on success, -1 on failure
3653 *
3654 * Inform WPA state machine about the RSN IE used in Beacon / Probe Response
3655 * frame.
3656 */
3657int wpa_sm_set_ap_rsn_ie(struct wpa_sm *sm, const u8 *ie, size_t len)
3658{
3659 if (sm == NULL)
3660 return -1;
3661
3662 os_free(sm->ap_rsn_ie);
3663 if (ie == NULL || len == 0) {
3664 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
3665 "WPA: clearing AP RSN IE");
3666 sm->ap_rsn_ie = NULL;
3667 sm->ap_rsn_ie_len = 0;
3668 } else {
3669 wpa_hexdump(MSG_DEBUG, "WPA: set AP RSN IE", ie, len);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003670 sm->ap_rsn_ie = os_memdup(ie, len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003671 if (sm->ap_rsn_ie == NULL)
3672 return -1;
3673
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003674 sm->ap_rsn_ie_len = len;
3675 }
3676
3677 return 0;
3678}
3679
3680
3681/**
Hai Shalomc3565922019-10-28 11:58:20 -07003682 * wpa_sm_set_ap_rsnxe - Set AP RSNXE from Beacon/ProbeResp
3683 * @sm: Pointer to WPA state machine data from wpa_sm_init()
3684 * @ie: Pointer to IE data (starting from id)
3685 * @len: IE length
3686 * Returns: 0 on success, -1 on failure
3687 *
3688 * Inform WPA state machine about the RSNXE used in Beacon / Probe Response
3689 * frame.
3690 */
3691int wpa_sm_set_ap_rsnxe(struct wpa_sm *sm, const u8 *ie, size_t len)
3692{
3693 if (!sm)
3694 return -1;
3695
3696 os_free(sm->ap_rsnxe);
3697 if (!ie || len == 0) {
3698 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: clearing AP RSNXE");
3699 sm->ap_rsnxe = NULL;
3700 sm->ap_rsnxe_len = 0;
3701 } else {
3702 wpa_hexdump(MSG_DEBUG, "WPA: set AP RSNXE", ie, len);
3703 sm->ap_rsnxe = os_memdup(ie, len);
3704 if (!sm->ap_rsnxe)
3705 return -1;
3706
3707 sm->ap_rsnxe_len = len;
3708 }
3709
3710 return 0;
3711}
3712
3713
3714/**
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003715 * wpa_sm_parse_own_wpa_ie - Parse own WPA/RSN IE
3716 * @sm: Pointer to WPA state machine data from wpa_sm_init()
3717 * @data: Pointer to data area for parsing results
3718 * Returns: 0 on success, -1 if IE is not known, or -2 on parsing failure
3719 *
3720 * Parse the contents of the own WPA or RSN IE from (Re)AssocReq and write the
3721 * parsed data into data.
3722 */
3723int wpa_sm_parse_own_wpa_ie(struct wpa_sm *sm, struct wpa_ie_data *data)
3724{
3725 if (sm == NULL)
3726 return -1;
3727
3728 if (sm->assoc_wpa_ie == NULL) {
3729 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
3730 "WPA: No WPA/RSN IE available from association info");
3731 return -1;
3732 }
3733 if (wpa_parse_wpa_ie(sm->assoc_wpa_ie, sm->assoc_wpa_ie_len, data))
3734 return -2;
3735 return 0;
3736}
3737
3738
3739int wpa_sm_pmksa_cache_list(struct wpa_sm *sm, char *buf, size_t len)
3740{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003741 return pmksa_cache_list(sm->pmksa, buf, len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003742}
3743
3744
Dmitry Shmidt29333592017-01-09 12:27:11 -08003745struct rsn_pmksa_cache_entry * wpa_sm_pmksa_cache_head(struct wpa_sm *sm)
3746{
3747 return pmksa_cache_head(sm->pmksa);
3748}
3749
3750
3751struct rsn_pmksa_cache_entry *
3752wpa_sm_pmksa_cache_add_entry(struct wpa_sm *sm,
3753 struct rsn_pmksa_cache_entry * entry)
3754{
3755 return pmksa_cache_add_entry(sm->pmksa, entry);
3756}
3757
3758
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003759void wpa_sm_pmksa_cache_add(struct wpa_sm *sm, const u8 *pmk, size_t pmk_len,
3760 const u8 *pmkid, const u8 *bssid,
3761 const u8 *fils_cache_id)
3762{
3763 sm->cur_pmksa = pmksa_cache_add(sm->pmksa, pmk, pmk_len, pmkid, NULL, 0,
3764 bssid, sm->own_addr, sm->network_ctx,
3765 sm->key_mgmt, fils_cache_id);
3766}
3767
3768
3769int wpa_sm_pmksa_exists(struct wpa_sm *sm, const u8 *bssid,
3770 const void *network_ctx)
3771{
Roshan Pius3a1667e2018-07-03 15:17:14 -07003772 return pmksa_cache_get(sm->pmksa, bssid, NULL, network_ctx, 0) != NULL;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003773}
3774
3775
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003776void wpa_sm_drop_sa(struct wpa_sm *sm)
3777{
3778 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Clear old PMK and PTK");
3779 sm->ptk_set = 0;
3780 sm->tptk_set = 0;
Roshan Pius3a1667e2018-07-03 15:17:14 -07003781 sm->pmk_len = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003782 os_memset(sm->pmk, 0, sizeof(sm->pmk));
3783 os_memset(&sm->ptk, 0, sizeof(sm->ptk));
3784 os_memset(&sm->tptk, 0, sizeof(sm->tptk));
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02003785 os_memset(&sm->gtk, 0, sizeof(sm->gtk));
Jouni Malinen58c0e962017-10-01 12:12:24 +03003786 os_memset(&sm->gtk_wnm_sleep, 0, sizeof(sm->gtk_wnm_sleep));
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02003787 os_memset(&sm->igtk, 0, sizeof(sm->igtk));
Jouni Malinen58c0e962017-10-01 12:12:24 +03003788 os_memset(&sm->igtk_wnm_sleep, 0, sizeof(sm->igtk_wnm_sleep));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003789#ifdef CONFIG_IEEE80211R
3790 os_memset(sm->xxkey, 0, sizeof(sm->xxkey));
Roshan Pius3a1667e2018-07-03 15:17:14 -07003791 sm->xxkey_len = 0;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003792 os_memset(sm->pmk_r0, 0, sizeof(sm->pmk_r0));
Roshan Pius3a1667e2018-07-03 15:17:14 -07003793 sm->pmk_r0_len = 0;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003794 os_memset(sm->pmk_r1, 0, sizeof(sm->pmk_r1));
Roshan Pius3a1667e2018-07-03 15:17:14 -07003795 sm->pmk_r1_len = 0;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003796#endif /* CONFIG_IEEE80211R */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003797}
3798
3799
3800int wpa_sm_has_ptk(struct wpa_sm *sm)
3801{
3802 if (sm == NULL)
3803 return 0;
3804 return sm->ptk_set;
3805}
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003806
3807
Hai Shalomfdcde762020-04-02 11:19:20 -07003808int wpa_sm_has_ptk_installed(struct wpa_sm *sm)
3809{
3810 if (!sm)
3811 return 0;
3812 return sm->ptk.installed;
3813}
3814
3815
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003816void wpa_sm_update_replay_ctr(struct wpa_sm *sm, const u8 *replay_ctr)
3817{
3818 os_memcpy(sm->rx_replay_counter, replay_ctr, WPA_REPLAY_COUNTER_LEN);
3819}
3820
3821
3822void wpa_sm_pmksa_cache_flush(struct wpa_sm *sm, void *network_ctx)
3823{
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07003824 pmksa_cache_flush(sm->pmksa, network_ctx, NULL, 0);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003825}
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003826
Mir Ali677e7482020-11-12 19:49:02 +05303827#ifdef CONFIG_DRIVER_NL80211_BRCM
3828void wpa_sm_install_pmk(struct wpa_sm *sm)
3829{
3830 /* In case the driver wants to handle re-assocs, pass it down the PMK. */
3831 if (wpa_sm_set_key(sm, wpa_cipher_to_alg(sm->pairwise_cipher), NULL, 0, 0, NULL, 0,
3832 (u8*)sm->pmk, sm->pmk_len, KEY_FLAG_PMK) < 0) {
3833 wpa_hexdump(MSG_DEBUG, "PSK: Install PMK to the driver for driver reassociations",
3834 (u8*)sm->pmk, sm->pmk_len);
3835 /* No harm if the driver doesn't support. */
3836 wpa_msg(sm->ctx->msg_ctx, MSG_DEBUG,
3837 "WPA: Failed to set PMK to the driver");
3838 }
3839}
3840#endif /* CONFIG_DRIVER_NL80211_BRCM */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003841
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003842#ifdef CONFIG_WNM
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003843int wpa_wnmsleep_install_key(struct wpa_sm *sm, u8 subelem_id, u8 *buf)
3844{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003845 u16 keyinfo;
3846 u8 keylen; /* plaintext key len */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003847 u8 *key_rsc;
3848
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003849 if (subelem_id == WNM_SLEEP_SUBELEM_GTK) {
Dmitry Shmidt61593f02014-04-21 16:27:35 -07003850 struct wpa_gtk_data gd;
3851
3852 os_memset(&gd, 0, sizeof(gd));
3853 keylen = wpa_cipher_key_len(sm->group_cipher);
3854 gd.key_rsc_len = wpa_cipher_rsc_len(sm->group_cipher);
3855 gd.alg = wpa_cipher_to_alg(sm->group_cipher);
3856 if (gd.alg == WPA_ALG_NONE) {
3857 wpa_printf(MSG_DEBUG, "Unsupported group cipher suite");
3858 return -1;
3859 }
3860
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003861 key_rsc = buf + 5;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003862 keyinfo = WPA_GET_LE16(buf + 2);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003863 gd.gtk_len = keylen;
3864 if (gd.gtk_len != buf[4]) {
3865 wpa_printf(MSG_DEBUG, "GTK len mismatch len %d vs %d",
3866 gd.gtk_len, buf[4]);
3867 return -1;
3868 }
3869 gd.keyidx = keyinfo & 0x03; /* B0 - B1 */
3870 gd.tx = wpa_supplicant_gtk_tx_bit_workaround(
3871 sm, !!(keyinfo & WPA_KEY_INFO_TXRX));
3872
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003873 os_memcpy(gd.gtk, buf + 13, gd.gtk_len);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003874
3875 wpa_hexdump_key(MSG_DEBUG, "Install GTK (WNM SLEEP)",
3876 gd.gtk, gd.gtk_len);
Jouni Malinen58c0e962017-10-01 12:12:24 +03003877 if (wpa_supplicant_install_gtk(sm, &gd, key_rsc, 1)) {
Hai Shalom81f62d82019-07-22 12:10:00 -07003878 forced_memzero(&gd, sizeof(gd));
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003879 wpa_printf(MSG_DEBUG, "Failed to install the GTK in "
3880 "WNM mode");
3881 return -1;
3882 }
Hai Shalom81f62d82019-07-22 12:10:00 -07003883 forced_memzero(&gd, sizeof(gd));
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003884 } else if (subelem_id == WNM_SLEEP_SUBELEM_IGTK) {
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02003885 const struct wpa_igtk_kde *igtk;
Dmitry Shmidt61593f02014-04-21 16:27:35 -07003886
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02003887 igtk = (const struct wpa_igtk_kde *) (buf + 2);
Jouni Malinen58c0e962017-10-01 12:12:24 +03003888 if (wpa_supplicant_install_igtk(sm, igtk, 1) < 0)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003889 return -1;
Hai Shalomfdcde762020-04-02 11:19:20 -07003890 } else if (subelem_id == WNM_SLEEP_SUBELEM_BIGTK) {
3891 const struct wpa_bigtk_kde *bigtk;
3892
3893 bigtk = (const struct wpa_bigtk_kde *) (buf + 2);
3894 if (sm->beacon_prot &&
3895 wpa_supplicant_install_bigtk(sm, bigtk, 1) < 0)
3896 return -1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003897 } else {
3898 wpa_printf(MSG_DEBUG, "Unknown element id");
3899 return -1;
3900 }
3901
3902 return 0;
3903}
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003904#endif /* CONFIG_WNM */
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003905
3906
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003907#ifdef CONFIG_P2P
3908
3909int wpa_sm_get_p2p_ip_addr(struct wpa_sm *sm, u8 *buf)
3910{
3911 if (sm == NULL || WPA_GET_BE32(sm->p2p_ip_addr) == 0)
3912 return -1;
3913 os_memcpy(buf, sm->p2p_ip_addr, 3 * 4);
3914 return 0;
3915}
3916
3917#endif /* CONFIG_P2P */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003918
3919
3920void wpa_sm_set_rx_replay_ctr(struct wpa_sm *sm, const u8 *rx_replay_counter)
3921{
3922 if (rx_replay_counter == NULL)
3923 return;
3924
3925 os_memcpy(sm->rx_replay_counter, rx_replay_counter,
3926 WPA_REPLAY_COUNTER_LEN);
3927 sm->rx_replay_counter_set = 1;
3928 wpa_printf(MSG_DEBUG, "Updated key replay counter");
3929}
3930
3931
Dmitry Shmidt807291d2015-01-27 13:40:23 -08003932void wpa_sm_set_ptk_kck_kek(struct wpa_sm *sm,
3933 const u8 *ptk_kck, size_t ptk_kck_len,
3934 const u8 *ptk_kek, size_t ptk_kek_len)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003935{
Dmitry Shmidt807291d2015-01-27 13:40:23 -08003936 if (ptk_kck && ptk_kck_len <= WPA_KCK_MAX_LEN) {
3937 os_memcpy(sm->ptk.kck, ptk_kck, ptk_kck_len);
3938 sm->ptk.kck_len = ptk_kck_len;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003939 wpa_printf(MSG_DEBUG, "Updated PTK KCK");
3940 }
Dmitry Shmidt807291d2015-01-27 13:40:23 -08003941 if (ptk_kek && ptk_kek_len <= WPA_KEK_MAX_LEN) {
3942 os_memcpy(sm->ptk.kek, ptk_kek, ptk_kek_len);
3943 sm->ptk.kek_len = ptk_kek_len;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003944 wpa_printf(MSG_DEBUG, "Updated PTK KEK");
3945 }
3946 sm->ptk_set = 1;
3947}
Dmitry Shmidt55840ad2015-12-14 12:45:46 -08003948
3949
3950#ifdef CONFIG_TESTING_OPTIONS
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003951
Dmitry Shmidt55840ad2015-12-14 12:45:46 -08003952void wpa_sm_set_test_assoc_ie(struct wpa_sm *sm, struct wpabuf *buf)
3953{
3954 wpabuf_free(sm->test_assoc_ie);
3955 sm->test_assoc_ie = buf;
3956}
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003957
3958
3959const u8 * wpa_sm_get_anonce(struct wpa_sm *sm)
3960{
3961 return sm->anonce;
3962}
3963
Dmitry Shmidt55840ad2015-12-14 12:45:46 -08003964#endif /* CONFIG_TESTING_OPTIONS */
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003965
3966
Roshan Pius3a1667e2018-07-03 15:17:14 -07003967unsigned int wpa_sm_get_key_mgmt(struct wpa_sm *sm)
3968{
3969 return sm->key_mgmt;
3970}
3971
3972
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003973#ifdef CONFIG_FILS
3974
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003975struct wpabuf * fils_build_auth(struct wpa_sm *sm, int dh_group, const u8 *md)
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003976{
3977 struct wpabuf *buf = NULL;
3978 struct wpabuf *erp_msg;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003979 struct wpabuf *pub = NULL;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003980
3981 erp_msg = eapol_sm_build_erp_reauth_start(sm->eapol);
3982 if (!erp_msg && !sm->cur_pmksa) {
3983 wpa_printf(MSG_DEBUG,
3984 "FILS: Neither ERP EAP-Initiate/Re-auth nor PMKSA cache entry is available - skip FILS");
3985 goto fail;
3986 }
3987
3988 wpa_printf(MSG_DEBUG, "FILS: Try to use FILS (erp=%d pmksa_cache=%d)",
3989 erp_msg != NULL, sm->cur_pmksa != NULL);
3990
3991 sm->fils_completed = 0;
3992
3993 if (!sm->assoc_wpa_ie) {
3994 wpa_printf(MSG_INFO, "FILS: No own RSN IE set for FILS");
3995 goto fail;
3996 }
3997
3998 if (random_get_bytes(sm->fils_nonce, FILS_NONCE_LEN) < 0 ||
3999 random_get_bytes(sm->fils_session, FILS_SESSION_LEN) < 0)
4000 goto fail;
4001
4002 wpa_hexdump(MSG_DEBUG, "FILS: Generated FILS Nonce",
4003 sm->fils_nonce, FILS_NONCE_LEN);
4004 wpa_hexdump(MSG_DEBUG, "FILS: Generated FILS Session",
4005 sm->fils_session, FILS_SESSION_LEN);
4006
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004007#ifdef CONFIG_FILS_SK_PFS
4008 sm->fils_dh_group = dh_group;
4009 if (dh_group) {
4010 crypto_ecdh_deinit(sm->fils_ecdh);
4011 sm->fils_ecdh = crypto_ecdh_init(dh_group);
4012 if (!sm->fils_ecdh) {
4013 wpa_printf(MSG_INFO,
4014 "FILS: Could not initialize ECDH with group %d",
4015 dh_group);
4016 goto fail;
4017 }
4018 pub = crypto_ecdh_get_pubkey(sm->fils_ecdh, 1);
4019 if (!pub)
4020 goto fail;
4021 wpa_hexdump_buf(MSG_DEBUG, "FILS: Element (DH public key)",
4022 pub);
4023 sm->fils_dh_elem_len = wpabuf_len(pub);
4024 }
4025#endif /* CONFIG_FILS_SK_PFS */
4026
4027 buf = wpabuf_alloc(1000 + sm->assoc_wpa_ie_len +
4028 (pub ? wpabuf_len(pub) : 0));
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004029 if (!buf)
4030 goto fail;
4031
4032 /* Fields following the Authentication algorithm number field */
4033
4034 /* Authentication Transaction seq# */
4035 wpabuf_put_le16(buf, 1);
4036
4037 /* Status Code */
4038 wpabuf_put_le16(buf, WLAN_STATUS_SUCCESS);
4039
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004040 /* TODO: FILS PK */
4041#ifdef CONFIG_FILS_SK_PFS
4042 if (dh_group) {
4043 /* Finite Cyclic Group */
4044 wpabuf_put_le16(buf, dh_group);
4045 /* Element */
4046 wpabuf_put_buf(buf, pub);
4047 }
4048#endif /* CONFIG_FILS_SK_PFS */
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004049
4050 /* RSNE */
4051 wpa_hexdump(MSG_DEBUG, "FILS: RSNE in FILS Authentication frame",
4052 sm->assoc_wpa_ie, sm->assoc_wpa_ie_len);
4053 wpabuf_put_data(buf, sm->assoc_wpa_ie, sm->assoc_wpa_ie_len);
4054
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004055 if (md) {
4056 /* MDE when using FILS for FT initial association */
4057 struct rsn_mdie *mdie;
4058
4059 wpabuf_put_u8(buf, WLAN_EID_MOBILITY_DOMAIN);
4060 wpabuf_put_u8(buf, sizeof(*mdie));
4061 mdie = wpabuf_put(buf, sizeof(*mdie));
4062 os_memcpy(mdie->mobility_domain, md, MOBILITY_DOMAIN_ID_LEN);
4063 mdie->ft_capab = 0;
4064 }
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004065
4066 /* FILS Nonce */
4067 wpabuf_put_u8(buf, WLAN_EID_EXTENSION); /* Element ID */
4068 wpabuf_put_u8(buf, 1 + FILS_NONCE_LEN); /* Length */
4069 /* Element ID Extension */
4070 wpabuf_put_u8(buf, WLAN_EID_EXT_FILS_NONCE);
4071 wpabuf_put_data(buf, sm->fils_nonce, FILS_NONCE_LEN);
4072
4073 /* FILS Session */
4074 wpabuf_put_u8(buf, WLAN_EID_EXTENSION); /* Element ID */
4075 wpabuf_put_u8(buf, 1 + FILS_SESSION_LEN); /* Length */
4076 /* Element ID Extension */
4077 wpabuf_put_u8(buf, WLAN_EID_EXT_FILS_SESSION);
4078 wpabuf_put_data(buf, sm->fils_session, FILS_SESSION_LEN);
4079
Hai Shalomfdcde762020-04-02 11:19:20 -07004080 /* Wrapped Data */
Paul Stewart092955c2017-02-06 09:13:09 -08004081 sm->fils_erp_pmkid_set = 0;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004082 if (erp_msg) {
4083 wpabuf_put_u8(buf, WLAN_EID_EXTENSION); /* Element ID */
4084 wpabuf_put_u8(buf, 1 + wpabuf_len(erp_msg)); /* Length */
4085 /* Element ID Extension */
Hai Shalomfdcde762020-04-02 11:19:20 -07004086 wpabuf_put_u8(buf, WLAN_EID_EXT_WRAPPED_DATA);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004087 wpabuf_put_buf(buf, erp_msg);
Paul Stewart092955c2017-02-06 09:13:09 -08004088 /* Calculate pending PMKID here so that we do not need to
4089 * maintain a copy of the EAP-Initiate/Reauth message. */
4090 if (fils_pmkid_erp(sm->key_mgmt, wpabuf_head(erp_msg),
4091 wpabuf_len(erp_msg),
4092 sm->fils_erp_pmkid) == 0)
4093 sm->fils_erp_pmkid_set = 1;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004094 }
4095
4096 wpa_hexdump_buf(MSG_DEBUG, "RSN: FILS fields for Authentication frame",
4097 buf);
4098
4099fail:
4100 wpabuf_free(erp_msg);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004101 wpabuf_free(pub);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004102 return buf;
4103}
4104
4105
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004106int fils_process_auth(struct wpa_sm *sm, const u8 *bssid, const u8 *data,
4107 size_t len)
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004108{
4109 const u8 *pos, *end;
4110 struct ieee802_11_elems elems;
4111 struct wpa_ie_data rsn;
4112 int pmkid_match = 0;
4113 u8 ick[FILS_ICK_MAX_LEN];
4114 size_t ick_len;
4115 int res;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004116 struct wpabuf *dh_ss = NULL;
4117 const u8 *g_sta = NULL;
4118 size_t g_sta_len = 0;
4119 const u8 *g_ap = NULL;
4120 size_t g_ap_len = 0;
4121 struct wpabuf *pub = NULL;
4122
4123 os_memcpy(sm->bssid, bssid, ETH_ALEN);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004124
4125 wpa_hexdump(MSG_DEBUG, "FILS: Authentication frame fields",
4126 data, len);
4127 pos = data;
4128 end = data + len;
4129
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004130 /* TODO: FILS PK */
4131#ifdef CONFIG_FILS_SK_PFS
4132 if (sm->fils_dh_group) {
4133 u16 group;
4134
4135 /* Using FILS PFS */
4136
4137 /* Finite Cyclic Group */
4138 if (end - pos < 2) {
4139 wpa_printf(MSG_DEBUG,
4140 "FILS: No room for Finite Cyclic Group");
4141 goto fail;
4142 }
4143 group = WPA_GET_LE16(pos);
4144 pos += 2;
4145 if (group != sm->fils_dh_group) {
4146 wpa_printf(MSG_DEBUG,
4147 "FILS: Unexpected change in Finite Cyclic Group: %u (expected %u)",
4148 group, sm->fils_dh_group);
4149 goto fail;
4150 }
4151
4152 /* Element */
4153 if ((size_t) (end - pos) < sm->fils_dh_elem_len) {
4154 wpa_printf(MSG_DEBUG, "FILS: No room for Element");
4155 goto fail;
4156 }
4157
4158 if (!sm->fils_ecdh) {
4159 wpa_printf(MSG_DEBUG, "FILS: No ECDH state available");
4160 goto fail;
4161 }
4162 dh_ss = crypto_ecdh_set_peerkey(sm->fils_ecdh, 1, pos,
4163 sm->fils_dh_elem_len);
4164 if (!dh_ss) {
4165 wpa_printf(MSG_DEBUG, "FILS: ECDH operation failed");
4166 goto fail;
4167 }
4168 wpa_hexdump_buf_key(MSG_DEBUG, "FILS: DH_SS", dh_ss);
4169 g_ap = pos;
4170 g_ap_len = sm->fils_dh_elem_len;
4171 pos += sm->fils_dh_elem_len;
4172 }
4173#endif /* CONFIG_FILS_SK_PFS */
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004174
4175 wpa_hexdump(MSG_DEBUG, "FILS: Remaining IEs", pos, end - pos);
4176 if (ieee802_11_parse_elems(pos, end - pos, &elems, 1) == ParseFailed) {
4177 wpa_printf(MSG_DEBUG, "FILS: Could not parse elements");
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004178 goto fail;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004179 }
4180
4181 /* RSNE */
4182 wpa_hexdump(MSG_DEBUG, "FILS: RSN element", elems.rsn_ie,
4183 elems.rsn_ie_len);
4184 if (!elems.rsn_ie ||
4185 wpa_parse_wpa_ie_rsn(elems.rsn_ie - 2, elems.rsn_ie_len + 2,
4186 &rsn) < 0) {
4187 wpa_printf(MSG_DEBUG, "FILS: No RSN element");
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004188 goto fail;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004189 }
4190
4191 if (!elems.fils_nonce) {
4192 wpa_printf(MSG_DEBUG, "FILS: No FILS Nonce field");
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004193 goto fail;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004194 }
4195 os_memcpy(sm->fils_anonce, elems.fils_nonce, FILS_NONCE_LEN);
4196 wpa_hexdump(MSG_DEBUG, "FILS: ANonce", sm->fils_anonce, FILS_NONCE_LEN);
4197
Roshan Pius3a1667e2018-07-03 15:17:14 -07004198#ifdef CONFIG_IEEE80211R
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004199 if (wpa_key_mgmt_ft(sm->key_mgmt)) {
4200 struct wpa_ft_ies parse;
4201
4202 if (!elems.mdie || !elems.ftie) {
4203 wpa_printf(MSG_DEBUG, "FILS+FT: No MDE or FTE");
4204 goto fail;
4205 }
4206
Roshan Pius3a1667e2018-07-03 15:17:14 -07004207 if (wpa_ft_parse_ies(pos, end - pos, &parse,
4208 wpa_key_mgmt_sha384(sm->key_mgmt)) < 0) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004209 wpa_printf(MSG_DEBUG, "FILS+FT: Failed to parse IEs");
4210 goto fail;
4211 }
4212
4213 if (!parse.r0kh_id) {
4214 wpa_printf(MSG_DEBUG,
4215 "FILS+FT: No R0KH-ID subelem in FTE");
4216 goto fail;
4217 }
4218 os_memcpy(sm->r0kh_id, parse.r0kh_id, parse.r0kh_id_len);
4219 sm->r0kh_id_len = parse.r0kh_id_len;
4220 wpa_hexdump_ascii(MSG_DEBUG, "FILS+FT: R0KH-ID",
4221 sm->r0kh_id, sm->r0kh_id_len);
4222
4223 if (!parse.r1kh_id) {
4224 wpa_printf(MSG_DEBUG,
4225 "FILS+FT: No R1KH-ID subelem in FTE");
4226 goto fail;
4227 }
4228 os_memcpy(sm->r1kh_id, parse.r1kh_id, FT_R1KH_ID_LEN);
4229 wpa_hexdump(MSG_DEBUG, "FILS+FT: R1KH-ID",
4230 sm->r1kh_id, FT_R1KH_ID_LEN);
4231
4232 /* TODO: Check MDE and FTE payload */
4233
4234 wpabuf_free(sm->fils_ft_ies);
4235 sm->fils_ft_ies = wpabuf_alloc(2 + elems.mdie_len +
4236 2 + elems.ftie_len);
4237 if (!sm->fils_ft_ies)
4238 goto fail;
4239 wpabuf_put_data(sm->fils_ft_ies, elems.mdie - 2,
4240 2 + elems.mdie_len);
4241 wpabuf_put_data(sm->fils_ft_ies, elems.ftie - 2,
4242 2 + elems.ftie_len);
4243 } else {
4244 wpabuf_free(sm->fils_ft_ies);
4245 sm->fils_ft_ies = NULL;
4246 }
Roshan Pius3a1667e2018-07-03 15:17:14 -07004247#endif /* CONFIG_IEEE80211R */
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004248
4249 /* PMKID List */
4250 if (rsn.pmkid && rsn.num_pmkid > 0) {
4251 wpa_hexdump(MSG_DEBUG, "FILS: PMKID List",
4252 rsn.pmkid, rsn.num_pmkid * PMKID_LEN);
4253
4254 if (rsn.num_pmkid != 1) {
4255 wpa_printf(MSG_DEBUG, "FILS: Invalid PMKID selection");
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004256 goto fail;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004257 }
4258 wpa_hexdump(MSG_DEBUG, "FILS: PMKID", rsn.pmkid, PMKID_LEN);
4259 if (os_memcmp(sm->cur_pmksa->pmkid, rsn.pmkid, PMKID_LEN) != 0)
4260 {
4261 wpa_printf(MSG_DEBUG, "FILS: PMKID mismatch");
4262 wpa_hexdump(MSG_DEBUG, "FILS: Expected PMKID",
4263 sm->cur_pmksa->pmkid, PMKID_LEN);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004264 goto fail;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004265 }
4266 wpa_printf(MSG_DEBUG,
4267 "FILS: Matching PMKID - continue using PMKSA caching");
4268 pmkid_match = 1;
4269 }
4270 if (!pmkid_match && sm->cur_pmksa) {
4271 wpa_printf(MSG_DEBUG,
4272 "FILS: No PMKID match - cannot use cached PMKSA entry");
4273 sm->cur_pmksa = NULL;
4274 }
4275
4276 /* FILS Session */
4277 if (!elems.fils_session) {
4278 wpa_printf(MSG_DEBUG, "FILS: No FILS Session element");
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004279 goto fail;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004280 }
4281 wpa_hexdump(MSG_DEBUG, "FILS: FILS Session", elems.fils_session,
4282 FILS_SESSION_LEN);
4283 if (os_memcmp(sm->fils_session, elems.fils_session, FILS_SESSION_LEN)
4284 != 0) {
4285 wpa_printf(MSG_DEBUG, "FILS: Session mismatch");
4286 wpa_hexdump(MSG_DEBUG, "FILS: Expected FILS Session",
4287 sm->fils_session, FILS_SESSION_LEN);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004288 goto fail;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004289 }
4290
Hai Shalomfdcde762020-04-02 11:19:20 -07004291 /* Wrapped Data */
4292 if (!sm->cur_pmksa && elems.wrapped_data) {
Paul Stewart092955c2017-02-06 09:13:09 -08004293 u8 rmsk[ERP_MAX_KEY_LEN];
4294 size_t rmsk_len;
4295
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004296 wpa_hexdump(MSG_DEBUG, "FILS: Wrapped Data",
Hai Shalomfdcde762020-04-02 11:19:20 -07004297 elems.wrapped_data,
4298 elems.wrapped_data_len);
4299 eapol_sm_process_erp_finish(sm->eapol, elems.wrapped_data,
4300 elems.wrapped_data_len);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004301 if (eapol_sm_failed(sm->eapol))
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004302 goto fail;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004303
Paul Stewart092955c2017-02-06 09:13:09 -08004304 rmsk_len = ERP_MAX_KEY_LEN;
4305 res = eapol_sm_get_key(sm->eapol, rmsk, rmsk_len);
4306 if (res == PMK_LEN) {
4307 rmsk_len = PMK_LEN;
4308 res = eapol_sm_get_key(sm->eapol, rmsk, rmsk_len);
4309 }
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004310 if (res)
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004311 goto fail;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004312
Paul Stewart092955c2017-02-06 09:13:09 -08004313 res = fils_rmsk_to_pmk(sm->key_mgmt, rmsk, rmsk_len,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004314 sm->fils_nonce, sm->fils_anonce,
4315 dh_ss ? wpabuf_head(dh_ss) : NULL,
4316 dh_ss ? wpabuf_len(dh_ss) : 0,
Paul Stewart092955c2017-02-06 09:13:09 -08004317 sm->pmk, &sm->pmk_len);
Hai Shalom81f62d82019-07-22 12:10:00 -07004318 forced_memzero(rmsk, sizeof(rmsk));
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004319
4320 /* Don't use DHss in PTK derivation if PMKSA caching is not
4321 * used. */
4322 wpabuf_clear_free(dh_ss);
4323 dh_ss = NULL;
4324
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08004325 if (res)
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004326 goto fail;
Paul Stewart092955c2017-02-06 09:13:09 -08004327
4328 if (!sm->fils_erp_pmkid_set) {
4329 wpa_printf(MSG_DEBUG, "FILS: PMKID not available");
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004330 goto fail;
Paul Stewart092955c2017-02-06 09:13:09 -08004331 }
4332 wpa_hexdump(MSG_DEBUG, "FILS: PMKID", sm->fils_erp_pmkid,
4333 PMKID_LEN);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004334 wpa_printf(MSG_DEBUG, "FILS: ERP processing succeeded - add PMKSA cache entry for the result");
Paul Stewart092955c2017-02-06 09:13:09 -08004335 sm->cur_pmksa = pmksa_cache_add(sm->pmksa, sm->pmk, sm->pmk_len,
4336 sm->fils_erp_pmkid, NULL, 0,
4337 sm->bssid, sm->own_addr,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004338 sm->network_ctx, sm->key_mgmt,
4339 NULL);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004340 }
4341
4342 if (!sm->cur_pmksa) {
4343 wpa_printf(MSG_DEBUG,
4344 "FILS: No remaining options to continue FILS authentication");
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004345 goto fail;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004346 }
4347
4348 if (fils_pmk_to_ptk(sm->pmk, sm->pmk_len, sm->own_addr, sm->bssid,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004349 sm->fils_nonce, sm->fils_anonce,
4350 dh_ss ? wpabuf_head(dh_ss) : NULL,
4351 dh_ss ? wpabuf_len(dh_ss) : 0,
4352 &sm->ptk, ick, &ick_len,
4353 sm->key_mgmt, sm->pairwise_cipher,
4354 sm->fils_ft, &sm->fils_ft_len) < 0) {
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004355 wpa_printf(MSG_DEBUG, "FILS: Failed to derive PTK");
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004356 goto fail;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004357 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004358
4359 wpabuf_clear_free(dh_ss);
4360 dh_ss = NULL;
4361
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004362 sm->ptk_set = 1;
4363 sm->tptk_set = 0;
4364 os_memset(&sm->tptk, 0, sizeof(sm->tptk));
4365
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004366#ifdef CONFIG_FILS_SK_PFS
4367 if (sm->fils_dh_group) {
4368 if (!sm->fils_ecdh) {
4369 wpa_printf(MSG_INFO, "FILS: ECDH not initialized");
4370 goto fail;
4371 }
4372 pub = crypto_ecdh_get_pubkey(sm->fils_ecdh, 1);
4373 if (!pub)
4374 goto fail;
4375 wpa_hexdump_buf(MSG_DEBUG, "FILS: gSTA", pub);
4376 g_sta = wpabuf_head(pub);
4377 g_sta_len = wpabuf_len(pub);
4378 if (!g_ap) {
4379 wpa_printf(MSG_INFO, "FILS: gAP not available");
4380 goto fail;
4381 }
4382 wpa_hexdump(MSG_DEBUG, "FILS: gAP", g_ap, g_ap_len);
4383 }
4384#endif /* CONFIG_FILS_SK_PFS */
4385
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004386 res = fils_key_auth_sk(ick, ick_len, sm->fils_nonce,
4387 sm->fils_anonce, sm->own_addr, sm->bssid,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004388 g_sta, g_sta_len, g_ap, g_ap_len,
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004389 sm->key_mgmt, sm->fils_key_auth_sta,
4390 sm->fils_key_auth_ap,
4391 &sm->fils_key_auth_len);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004392 wpabuf_free(pub);
Hai Shalom81f62d82019-07-22 12:10:00 -07004393 forced_memzero(ick, sizeof(ick));
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004394 return res;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004395fail:
4396 wpabuf_free(pub);
4397 wpabuf_clear_free(dh_ss);
4398 return -1;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004399}
4400
4401
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004402#ifdef CONFIG_IEEE80211R
4403static int fils_ft_build_assoc_req_rsne(struct wpa_sm *sm, struct wpabuf *buf)
4404{
4405 struct rsn_ie_hdr *rsnie;
4406 u16 capab;
4407 u8 *pos;
Roshan Pius3a1667e2018-07-03 15:17:14 -07004408 int use_sha384 = wpa_key_mgmt_sha384(sm->key_mgmt);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004409
4410 /* RSNIE[PMKR0Name/PMKR1Name] */
4411 rsnie = wpabuf_put(buf, sizeof(*rsnie));
4412 rsnie->elem_id = WLAN_EID_RSN;
4413 WPA_PUT_LE16(rsnie->version, RSN_VERSION);
4414
4415 /* Group Suite Selector */
4416 if (!wpa_cipher_valid_group(sm->group_cipher)) {
4417 wpa_printf(MSG_WARNING, "FT: Invalid group cipher (%d)",
4418 sm->group_cipher);
4419 return -1;
4420 }
4421 pos = wpabuf_put(buf, RSN_SELECTOR_LEN);
4422 RSN_SELECTOR_PUT(pos, wpa_cipher_to_suite(WPA_PROTO_RSN,
4423 sm->group_cipher));
4424
4425 /* Pairwise Suite Count */
4426 wpabuf_put_le16(buf, 1);
4427
4428 /* Pairwise Suite List */
4429 if (!wpa_cipher_valid_pairwise(sm->pairwise_cipher)) {
4430 wpa_printf(MSG_WARNING, "FT: Invalid pairwise cipher (%d)",
4431 sm->pairwise_cipher);
4432 return -1;
4433 }
4434 pos = wpabuf_put(buf, RSN_SELECTOR_LEN);
4435 RSN_SELECTOR_PUT(pos, wpa_cipher_to_suite(WPA_PROTO_RSN,
4436 sm->pairwise_cipher));
4437
4438 /* Authenticated Key Management Suite Count */
4439 wpabuf_put_le16(buf, 1);
4440
4441 /* Authenticated Key Management Suite List */
4442 pos = wpabuf_put(buf, RSN_SELECTOR_LEN);
4443 if (sm->key_mgmt == WPA_KEY_MGMT_FT_FILS_SHA256)
4444 RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_FT_FILS_SHA256);
4445 else if (sm->key_mgmt == WPA_KEY_MGMT_FT_FILS_SHA384)
4446 RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_FT_FILS_SHA384);
4447 else {
4448 wpa_printf(MSG_WARNING,
4449 "FILS+FT: Invalid key management type (%d)",
4450 sm->key_mgmt);
4451 return -1;
4452 }
4453
4454 /* RSN Capabilities */
4455 capab = 0;
Hai Shalomc3565922019-10-28 11:58:20 -07004456 if (sm->mfp)
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004457 capab |= WPA_CAPABILITY_MFPC;
Hai Shalomc3565922019-10-28 11:58:20 -07004458 if (sm->mfp == 2)
4459 capab |= WPA_CAPABILITY_MFPR;
Hai Shalom74f70d42019-02-11 14:42:39 -08004460 if (sm->ocv)
4461 capab |= WPA_CAPABILITY_OCVC;
Hai Shalomfdcde762020-04-02 11:19:20 -07004462 if (sm->ext_key_id)
4463 capab |= WPA_CAPABILITY_EXT_KEY_ID_FOR_UNICAST;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004464 wpabuf_put_le16(buf, capab);
4465
4466 /* PMKID Count */
4467 wpabuf_put_le16(buf, 1);
4468
4469 /* PMKID List [PMKR1Name] */
4470 wpa_hexdump_key(MSG_DEBUG, "FILS+FT: XXKey (FILS-FT)",
4471 sm->fils_ft, sm->fils_ft_len);
4472 wpa_hexdump_ascii(MSG_DEBUG, "FILS+FT: SSID", sm->ssid, sm->ssid_len);
4473 wpa_hexdump(MSG_DEBUG, "FILS+FT: MDID",
4474 sm->mobility_domain, MOBILITY_DOMAIN_ID_LEN);
4475 wpa_hexdump_ascii(MSG_DEBUG, "FILS+FT: R0KH-ID",
4476 sm->r0kh_id, sm->r0kh_id_len);
4477 if (wpa_derive_pmk_r0(sm->fils_ft, sm->fils_ft_len, sm->ssid,
4478 sm->ssid_len, sm->mobility_domain,
4479 sm->r0kh_id, sm->r0kh_id_len, sm->own_addr,
Roshan Pius3a1667e2018-07-03 15:17:14 -07004480 sm->pmk_r0, sm->pmk_r0_name, use_sha384) < 0) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004481 wpa_printf(MSG_WARNING, "FILS+FT: Could not derive PMK-R0");
4482 return -1;
4483 }
Roshan Pius3a1667e2018-07-03 15:17:14 -07004484 sm->pmk_r0_len = use_sha384 ? SHA384_MAC_LEN : PMK_LEN;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004485 wpa_printf(MSG_DEBUG, "FILS+FT: R1KH-ID: " MACSTR,
4486 MAC2STR(sm->r1kh_id));
4487 pos = wpabuf_put(buf, WPA_PMK_NAME_LEN);
4488 if (wpa_derive_pmk_r1_name(sm->pmk_r0_name, sm->r1kh_id, sm->own_addr,
Hai Shalom021b0b52019-04-10 11:17:58 -07004489 sm->pmk_r1_name, use_sha384) < 0) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004490 wpa_printf(MSG_WARNING, "FILS+FT: Could not derive PMKR1Name");
4491 return -1;
4492 }
Hai Shalom021b0b52019-04-10 11:17:58 -07004493 os_memcpy(pos, sm->pmk_r1_name, WPA_PMK_NAME_LEN);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004494
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004495 if (sm->mgmt_group_cipher == WPA_CIPHER_AES_128_CMAC) {
4496 /* Management Group Cipher Suite */
4497 pos = wpabuf_put(buf, RSN_SELECTOR_LEN);
4498 RSN_SELECTOR_PUT(pos, RSN_CIPHER_SUITE_AES_128_CMAC);
4499 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004500
4501 rsnie->len = ((u8 *) wpabuf_put(buf, 0) - (u8 *) rsnie) - 2;
4502 return 0;
4503}
4504#endif /* CONFIG_IEEE80211R */
4505
4506
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004507struct wpabuf * fils_build_assoc_req(struct wpa_sm *sm, const u8 **kek,
4508 size_t *kek_len, const u8 **snonce,
Paul Stewart092955c2017-02-06 09:13:09 -08004509 const u8 **anonce,
4510 const struct wpabuf **hlp,
4511 unsigned int num_hlp)
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004512{
4513 struct wpabuf *buf;
Paul Stewart092955c2017-02-06 09:13:09 -08004514 size_t len;
4515 unsigned int i;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004516
Paul Stewart092955c2017-02-06 09:13:09 -08004517 len = 1000;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004518#ifdef CONFIG_IEEE80211R
4519 if (sm->fils_ft_ies)
4520 len += wpabuf_len(sm->fils_ft_ies);
4521 if (wpa_key_mgmt_ft(sm->key_mgmt))
4522 len += 256;
4523#endif /* CONFIG_IEEE80211R */
Paul Stewart092955c2017-02-06 09:13:09 -08004524 for (i = 0; hlp && i < num_hlp; i++)
4525 len += 10 + wpabuf_len(hlp[i]);
4526 buf = wpabuf_alloc(len);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004527 if (!buf)
4528 return NULL;
4529
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004530#ifdef CONFIG_IEEE80211R
4531 if (wpa_key_mgmt_ft(sm->key_mgmt) && sm->fils_ft_ies) {
4532 /* MDE and FTE when using FILS+FT */
4533 wpabuf_put_buf(buf, sm->fils_ft_ies);
4534 /* RSNE with PMKR1Name in PMKID field */
4535 if (fils_ft_build_assoc_req_rsne(sm, buf) < 0) {
4536 wpabuf_free(buf);
4537 return NULL;
4538 }
4539 }
4540#endif /* CONFIG_IEEE80211R */
4541
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004542 /* FILS Session */
4543 wpabuf_put_u8(buf, WLAN_EID_EXTENSION); /* Element ID */
4544 wpabuf_put_u8(buf, 1 + FILS_SESSION_LEN); /* Length */
4545 /* Element ID Extension */
4546 wpabuf_put_u8(buf, WLAN_EID_EXT_FILS_SESSION);
4547 wpabuf_put_data(buf, sm->fils_session, FILS_SESSION_LEN);
4548
4549 /* Everything after FILS Session element gets encrypted in the driver
4550 * with KEK. The buffer returned from here is the plaintext version. */
4551
4552 /* TODO: FILS Public Key */
4553
4554 /* FILS Key Confirm */
4555 wpabuf_put_u8(buf, WLAN_EID_EXTENSION); /* Element ID */
4556 wpabuf_put_u8(buf, 1 + sm->fils_key_auth_len); /* Length */
4557 /* Element ID Extension */
4558 wpabuf_put_u8(buf, WLAN_EID_EXT_FILS_KEY_CONFIRM);
4559 wpabuf_put_data(buf, sm->fils_key_auth_sta, sm->fils_key_auth_len);
4560
Paul Stewart092955c2017-02-06 09:13:09 -08004561 /* FILS HLP Container */
4562 for (i = 0; hlp && i < num_hlp; i++) {
4563 const u8 *pos = wpabuf_head(hlp[i]);
4564 size_t left = wpabuf_len(hlp[i]);
4565
4566 wpabuf_put_u8(buf, WLAN_EID_EXTENSION); /* Element ID */
4567 if (left <= 254)
4568 len = 1 + left;
4569 else
4570 len = 255;
4571 wpabuf_put_u8(buf, len); /* Length */
4572 /* Element ID Extension */
4573 wpabuf_put_u8(buf, WLAN_EID_EXT_FILS_HLP_CONTAINER);
4574 /* Destination MAC Address, Source MAC Address, HLP Packet.
4575 * HLP Packet is in MSDU format (i.e., included the LLC/SNAP
4576 * header when LPD is used). */
4577 wpabuf_put_data(buf, pos, len - 1);
4578 pos += len - 1;
4579 left -= len - 1;
4580 while (left) {
4581 wpabuf_put_u8(buf, WLAN_EID_FRAGMENT);
4582 len = left > 255 ? 255 : left;
4583 wpabuf_put_u8(buf, len);
4584 wpabuf_put_data(buf, pos, len);
4585 pos += len;
4586 left -= len;
4587 }
4588 }
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004589
4590 /* TODO: FILS IP Address Assignment */
4591
Hai Shalom74f70d42019-02-11 14:42:39 -08004592#ifdef CONFIG_OCV
4593 if (wpa_sm_ocv_enabled(sm)) {
4594 struct wpa_channel_info ci;
4595 u8 *pos;
4596
4597 if (wpa_sm_channel_info(sm, &ci) != 0) {
4598 wpa_printf(MSG_WARNING,
4599 "FILS: Failed to get channel info for OCI element");
4600 wpabuf_free(buf);
4601 return NULL;
4602 }
Hai Shalom899fcc72020-10-19 14:38:18 -07004603#ifdef CONFIG_TESTING_OPTIONS
4604 if (sm->oci_freq_override_fils_assoc) {
4605 wpa_printf(MSG_INFO,
4606 "TEST: Override OCI KDE frequency %d -> %d MHz",
4607 ci.frequency,
4608 sm->oci_freq_override_fils_assoc);
4609 ci.frequency = sm->oci_freq_override_fils_assoc;
4610 }
4611#endif /* CONFIG_TESTING_OPTIONS */
Hai Shalom74f70d42019-02-11 14:42:39 -08004612
4613 pos = wpabuf_put(buf, OCV_OCI_EXTENDED_LEN);
4614 if (ocv_insert_extended_oci(&ci, pos) < 0) {
4615 wpabuf_free(buf);
4616 return NULL;
4617 }
4618 }
4619#endif /* CONFIG_OCV */
4620
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004621 wpa_hexdump_buf(MSG_DEBUG, "FILS: Association Request plaintext", buf);
4622
4623 *kek = sm->ptk.kek;
4624 *kek_len = sm->ptk.kek_len;
4625 wpa_hexdump_key(MSG_DEBUG, "FILS: KEK for AEAD", *kek, *kek_len);
4626 *snonce = sm->fils_nonce;
4627 wpa_hexdump(MSG_DEBUG, "FILS: SNonce for AEAD AAD",
4628 *snonce, FILS_NONCE_LEN);
4629 *anonce = sm->fils_anonce;
4630 wpa_hexdump(MSG_DEBUG, "FILS: ANonce for AEAD AAD",
4631 *anonce, FILS_NONCE_LEN);
4632
4633 return buf;
4634}
4635
4636
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08004637static void fils_process_hlp_resp(struct wpa_sm *sm, const u8 *resp, size_t len)
4638{
4639 const u8 *pos, *end;
4640
4641 wpa_hexdump(MSG_MSGDUMP, "FILS: HLP response", resp, len);
4642 if (len < 2 * ETH_ALEN)
4643 return;
4644 pos = resp + 2 * ETH_ALEN;
4645 end = resp + len;
4646 if (end - pos >= 6 &&
4647 os_memcmp(pos, "\xaa\xaa\x03\x00\x00\x00", 6) == 0)
4648 pos += 6; /* Remove SNAP/LLC header */
4649 wpa_sm_fils_hlp_rx(sm, resp, resp + ETH_ALEN, pos, end - pos);
4650}
4651
4652
4653static void fils_process_hlp_container(struct wpa_sm *sm, const u8 *pos,
4654 size_t len)
4655{
4656 const u8 *end = pos + len;
4657 u8 *tmp, *tmp_pos;
4658
4659 /* Check if there are any FILS HLP Container elements */
4660 while (end - pos >= 2) {
4661 if (2 + pos[1] > end - pos)
4662 return;
4663 if (pos[0] == WLAN_EID_EXTENSION &&
4664 pos[1] >= 1 + 2 * ETH_ALEN &&
4665 pos[2] == WLAN_EID_EXT_FILS_HLP_CONTAINER)
4666 break;
4667 pos += 2 + pos[1];
4668 }
4669 if (end - pos < 2)
4670 return; /* No FILS HLP Container elements */
4671
4672 tmp = os_malloc(end - pos);
4673 if (!tmp)
4674 return;
4675
4676 while (end - pos >= 2) {
4677 if (2 + pos[1] > end - pos ||
4678 pos[0] != WLAN_EID_EXTENSION ||
4679 pos[1] < 1 + 2 * ETH_ALEN ||
4680 pos[2] != WLAN_EID_EXT_FILS_HLP_CONTAINER)
4681 break;
4682 tmp_pos = tmp;
4683 os_memcpy(tmp_pos, pos + 3, pos[1] - 1);
4684 tmp_pos += pos[1] - 1;
4685 pos += 2 + pos[1];
4686
4687 /* Add possible fragments */
4688 while (end - pos >= 2 && pos[0] == WLAN_EID_FRAGMENT &&
4689 2 + pos[1] <= end - pos) {
4690 os_memcpy(tmp_pos, pos + 2, pos[1]);
4691 tmp_pos += pos[1];
4692 pos += 2 + pos[1];
4693 }
4694
4695 fils_process_hlp_resp(sm, tmp, tmp_pos - tmp);
4696 }
4697
4698 os_free(tmp);
4699}
4700
4701
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004702int fils_process_assoc_resp(struct wpa_sm *sm, const u8 *resp, size_t len)
4703{
4704 const struct ieee80211_mgmt *mgmt;
4705 const u8 *end, *ie_start;
4706 struct ieee802_11_elems elems;
4707 int keylen, rsclen;
4708 enum wpa_alg alg;
4709 struct wpa_gtk_data gd;
4710 int maxkeylen;
4711 struct wpa_eapol_ie_parse kde;
4712
4713 if (!sm || !sm->ptk_set) {
4714 wpa_printf(MSG_DEBUG, "FILS: No KEK available");
4715 return -1;
4716 }
4717
4718 if (!wpa_key_mgmt_fils(sm->key_mgmt)) {
4719 wpa_printf(MSG_DEBUG, "FILS: Not a FILS AKM");
4720 return -1;
4721 }
4722
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004723 if (sm->fils_completed) {
4724 wpa_printf(MSG_DEBUG,
4725 "FILS: Association has already been completed for this FILS authentication - ignore unexpected retransmission");
4726 return -1;
4727 }
4728
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004729 wpa_hexdump(MSG_DEBUG, "FILS: (Re)Association Response frame",
4730 resp, len);
4731
4732 mgmt = (const struct ieee80211_mgmt *) resp;
4733 if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.assoc_resp))
4734 return -1;
4735
4736 end = resp + len;
4737 /* Same offset for Association Response and Reassociation Response */
4738 ie_start = mgmt->u.assoc_resp.variable;
4739
4740 if (ieee802_11_parse_elems(ie_start, end - ie_start, &elems, 1) ==
4741 ParseFailed) {
4742 wpa_printf(MSG_DEBUG,
4743 "FILS: Failed to parse decrypted elements");
4744 goto fail;
4745 }
4746
4747 if (!elems.fils_session) {
4748 wpa_printf(MSG_DEBUG, "FILS: No FILS Session element");
4749 return -1;
4750 }
4751 if (os_memcmp(elems.fils_session, sm->fils_session,
4752 FILS_SESSION_LEN) != 0) {
4753 wpa_printf(MSG_DEBUG, "FILS: FILS Session mismatch");
4754 wpa_hexdump(MSG_DEBUG, "FILS: Received FILS Session",
4755 elems.fils_session, FILS_SESSION_LEN);
4756 wpa_hexdump(MSG_DEBUG, "FILS: Expected FILS Session",
4757 sm->fils_session, FILS_SESSION_LEN);
4758 }
4759
Hai Shalom81f62d82019-07-22 12:10:00 -07004760 if (!elems.rsn_ie) {
4761 wpa_printf(MSG_DEBUG,
4762 "FILS: No RSNE in (Re)Association Response");
4763 /* As an interop workaround, allow this for now since IEEE Std
4764 * 802.11ai-2016 did not include all the needed changes to make
4765 * a FILS AP include RSNE in the frame. This workaround might
4766 * eventually be removed and replaced with rejection (goto fail)
4767 * to follow a strict interpretation of the standard. */
4768 } else if (wpa_compare_rsn_ie(wpa_key_mgmt_ft(sm->key_mgmt),
4769 sm->ap_rsn_ie, sm->ap_rsn_ie_len,
4770 elems.rsn_ie - 2, elems.rsn_ie_len + 2)) {
4771 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
4772 "FILS: RSNE mismatch between Beacon/Probe Response and (Re)Association Response");
4773 wpa_hexdump(MSG_DEBUG, "FILS: RSNE in Beacon/Probe Response",
4774 sm->ap_rsn_ie, sm->ap_rsn_ie_len);
4775 wpa_hexdump(MSG_DEBUG, "FILS: RSNE in (Re)Association Response",
4776 elems.rsn_ie, elems.rsn_ie_len);
4777 goto fail;
4778 }
4779
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004780 /* TODO: FILS Public Key */
4781
4782 if (!elems.fils_key_confirm) {
4783 wpa_printf(MSG_DEBUG, "FILS: No FILS Key Confirm element");
4784 goto fail;
4785 }
4786 if (elems.fils_key_confirm_len != sm->fils_key_auth_len) {
4787 wpa_printf(MSG_DEBUG,
4788 "FILS: Unexpected Key-Auth length %d (expected %d)",
4789 elems.fils_key_confirm_len,
4790 (int) sm->fils_key_auth_len);
4791 goto fail;
4792 }
4793 if (os_memcmp(elems.fils_key_confirm, sm->fils_key_auth_ap,
4794 sm->fils_key_auth_len) != 0) {
4795 wpa_printf(MSG_DEBUG, "FILS: Key-Auth mismatch");
4796 wpa_hexdump(MSG_DEBUG, "FILS: Received Key-Auth",
4797 elems.fils_key_confirm,
4798 elems.fils_key_confirm_len);
4799 wpa_hexdump(MSG_DEBUG, "FILS: Expected Key-Auth",
4800 sm->fils_key_auth_ap, sm->fils_key_auth_len);
4801 goto fail;
4802 }
4803
Hai Shalom74f70d42019-02-11 14:42:39 -08004804#ifdef CONFIG_OCV
4805 if (wpa_sm_ocv_enabled(sm)) {
4806 struct wpa_channel_info ci;
4807
4808 if (wpa_sm_channel_info(sm, &ci) != 0) {
4809 wpa_printf(MSG_WARNING,
4810 "Failed to get channel info to validate received OCI in FILS (Re)Association Response frame");
4811 goto fail;
4812 }
4813
4814 if (ocv_verify_tx_params(elems.oci, elems.oci_len, &ci,
4815 channel_width_to_int(ci.chanwidth),
Hai Shalom899fcc72020-10-19 14:38:18 -07004816 ci.seg1_idx) != OCI_SUCCESS) {
4817 wpa_msg(sm->ctx->msg_ctx, MSG_INFO, OCV_FAILURE
4818 "addr=" MACSTR " frame=fils-assoc error=%s",
4819 MAC2STR(sm->bssid), ocv_errorstr);
Hai Shalom74f70d42019-02-11 14:42:39 -08004820 goto fail;
4821 }
4822 }
4823#endif /* CONFIG_OCV */
4824
Hai Shalom021b0b52019-04-10 11:17:58 -07004825#ifdef CONFIG_IEEE80211R
4826 if (wpa_key_mgmt_ft(sm->key_mgmt) && sm->fils_ft_ies) {
4827 struct wpa_ie_data rsn;
4828
4829 /* Check that PMKR1Name derived by the AP matches */
4830 if (!elems.rsn_ie ||
4831 wpa_parse_wpa_ie_rsn(elems.rsn_ie - 2, elems.rsn_ie_len + 2,
4832 &rsn) < 0 ||
4833 !rsn.pmkid || rsn.num_pmkid != 1 ||
4834 os_memcmp(rsn.pmkid, sm->pmk_r1_name,
4835 WPA_PMK_NAME_LEN) != 0) {
4836 wpa_printf(MSG_DEBUG,
4837 "FILS+FT: No RSNE[PMKR1Name] match in AssocResp");
4838 goto fail;
4839 }
4840 }
4841#endif /* CONFIG_IEEE80211R */
4842
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004843 /* Key Delivery */
4844 if (!elems.key_delivery) {
4845 wpa_printf(MSG_DEBUG, "FILS: No Key Delivery element");
4846 goto fail;
4847 }
4848
4849 /* Parse GTK and set the key to the driver */
4850 os_memset(&gd, 0, sizeof(gd));
4851 if (wpa_supplicant_parse_ies(elems.key_delivery + WPA_KEY_RSC_LEN,
4852 elems.key_delivery_len - WPA_KEY_RSC_LEN,
4853 &kde) < 0) {
4854 wpa_printf(MSG_DEBUG, "FILS: Failed to parse KDEs");
4855 goto fail;
4856 }
4857 if (!kde.gtk) {
4858 wpa_printf(MSG_DEBUG, "FILS: No GTK KDE");
4859 goto fail;
4860 }
4861 maxkeylen = gd.gtk_len = kde.gtk_len - 2;
4862 if (wpa_supplicant_check_group_cipher(sm, sm->group_cipher,
4863 gd.gtk_len, maxkeylen,
4864 &gd.key_rsc_len, &gd.alg))
4865 goto fail;
4866
4867 wpa_hexdump_key(MSG_DEBUG, "FILS: Received GTK", kde.gtk, kde.gtk_len);
4868 gd.keyidx = kde.gtk[0] & 0x3;
4869 gd.tx = wpa_supplicant_gtk_tx_bit_workaround(sm,
4870 !!(kde.gtk[0] & BIT(2)));
4871 if (kde.gtk_len - 2 > sizeof(gd.gtk)) {
4872 wpa_printf(MSG_DEBUG, "FILS: Too long GTK in GTK KDE (len=%lu)",
4873 (unsigned long) kde.gtk_len - 2);
4874 goto fail;
4875 }
4876 os_memcpy(gd.gtk, kde.gtk + 2, kde.gtk_len - 2);
4877
4878 wpa_printf(MSG_DEBUG, "FILS: Set GTK to driver");
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004879 if (wpa_supplicant_install_gtk(sm, &gd, elems.key_delivery, 0) < 0) {
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004880 wpa_printf(MSG_DEBUG, "FILS: Failed to set GTK");
4881 goto fail;
4882 }
4883
4884 if (ieee80211w_set_keys(sm, &kde) < 0) {
4885 wpa_printf(MSG_DEBUG, "FILS: Failed to set IGTK");
4886 goto fail;
4887 }
4888
4889 alg = wpa_cipher_to_alg(sm->pairwise_cipher);
4890 keylen = wpa_cipher_key_len(sm->pairwise_cipher);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004891 if (keylen <= 0 || (unsigned int) keylen != sm->ptk.tk_len) {
4892 wpa_printf(MSG_DEBUG, "FILS: TK length mismatch: %u != %lu",
4893 keylen, (long unsigned int) sm->ptk.tk_len);
4894 goto fail;
4895 }
Hai Shalomfdcde762020-04-02 11:19:20 -07004896
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004897 rsclen = wpa_cipher_rsc_len(sm->pairwise_cipher);
4898 wpa_hexdump_key(MSG_DEBUG, "FILS: Set TK to driver",
4899 sm->ptk.tk, keylen);
4900 if (wpa_sm_set_key(sm, alg, sm->bssid, 0, 1, null_rsc, rsclen,
Hai Shalomfdcde762020-04-02 11:19:20 -07004901 sm->ptk.tk, keylen, KEY_FLAG_PAIRWISE_RX_TX) < 0) {
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004902 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
4903 "FILS: Failed to set PTK to the driver (alg=%d keylen=%d bssid="
4904 MACSTR ")",
4905 alg, keylen, MAC2STR(sm->bssid));
4906 goto fail;
4907 }
4908
4909 /* TODO: TK could be cleared after auth frame exchange now that driver
4910 * takes care of association frame encryption/decryption. */
4911 /* TK is not needed anymore in supplicant */
4912 os_memset(sm->ptk.tk, 0, WPA_TK_MAX_LEN);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004913 sm->ptk.tk_len = 0;
Mathy Vanhoefc66556c2017-09-29 04:22:51 +02004914 sm->ptk.installed = 1;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004915
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08004916 /* FILS HLP Container */
4917 fils_process_hlp_container(sm, ie_start, end - ie_start);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004918
4919 /* TODO: FILS IP Address Assignment */
4920
4921 wpa_printf(MSG_DEBUG, "FILS: Auth+Assoc completed successfully");
4922 sm->fils_completed = 1;
Hai Shalom81f62d82019-07-22 12:10:00 -07004923 forced_memzero(&gd, sizeof(gd));
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004924
Hai Shalomfdcde762020-04-02 11:19:20 -07004925 if (kde.transition_disable)
4926 wpa_sm_transition_disable(sm, kde.transition_disable[0]);
4927
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004928 return 0;
4929fail:
Hai Shalom81f62d82019-07-22 12:10:00 -07004930 forced_memzero(&gd, sizeof(gd));
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004931 return -1;
4932}
4933
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004934
4935void wpa_sm_set_reset_fils_completed(struct wpa_sm *sm, int set)
4936{
4937 if (sm)
4938 sm->fils_completed = !!set;
4939}
4940
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004941#endif /* CONFIG_FILS */
4942
4943
4944int wpa_fils_is_completed(struct wpa_sm *sm)
4945{
4946#ifdef CONFIG_FILS
4947 return sm && sm->fils_completed;
4948#else /* CONFIG_FILS */
4949 return 0;
4950#endif /* CONFIG_FILS */
4951}
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004952
4953
4954#ifdef CONFIG_OWE
4955
4956struct wpabuf * owe_build_assoc_req(struct wpa_sm *sm, u16 group)
4957{
4958 struct wpabuf *ie = NULL, *pub = NULL;
4959 size_t prime_len;
4960
4961 if (group == 19)
4962 prime_len = 32;
4963 else if (group == 20)
4964 prime_len = 48;
4965 else if (group == 21)
4966 prime_len = 66;
4967 else
4968 return NULL;
4969
4970 crypto_ecdh_deinit(sm->owe_ecdh);
4971 sm->owe_ecdh = crypto_ecdh_init(group);
4972 if (!sm->owe_ecdh)
4973 goto fail;
4974 sm->owe_group = group;
4975 pub = crypto_ecdh_get_pubkey(sm->owe_ecdh, 0);
4976 pub = wpabuf_zeropad(pub, prime_len);
4977 if (!pub)
4978 goto fail;
4979
4980 ie = wpabuf_alloc(5 + wpabuf_len(pub));
4981 if (!ie)
4982 goto fail;
4983 wpabuf_put_u8(ie, WLAN_EID_EXTENSION);
4984 wpabuf_put_u8(ie, 1 + 2 + wpabuf_len(pub));
4985 wpabuf_put_u8(ie, WLAN_EID_EXT_OWE_DH_PARAM);
4986 wpabuf_put_le16(ie, group);
4987 wpabuf_put_buf(ie, pub);
4988 wpabuf_free(pub);
4989 wpa_hexdump_buf(MSG_DEBUG, "OWE: Diffie-Hellman Parameter element",
4990 ie);
4991
4992 return ie;
4993fail:
4994 wpabuf_free(pub);
4995 crypto_ecdh_deinit(sm->owe_ecdh);
4996 sm->owe_ecdh = NULL;
4997 return NULL;
4998}
4999
5000
5001int owe_process_assoc_resp(struct wpa_sm *sm, const u8 *bssid,
5002 const u8 *resp_ies, size_t resp_ies_len)
5003{
5004 struct ieee802_11_elems elems;
5005 u16 group;
5006 struct wpabuf *secret, *pub, *hkey;
5007 int res;
5008 u8 prk[SHA512_MAC_LEN], pmkid[SHA512_MAC_LEN];
5009 const char *info = "OWE Key Generation";
5010 const u8 *addr[2];
5011 size_t len[2];
5012 size_t hash_len, prime_len;
5013 struct wpa_ie_data data;
5014
5015 if (!resp_ies ||
5016 ieee802_11_parse_elems(resp_ies, resp_ies_len, &elems, 1) ==
5017 ParseFailed) {
5018 wpa_printf(MSG_INFO,
5019 "OWE: Could not parse Association Response frame elements");
5020 return -1;
5021 }
5022
5023 if (sm->cur_pmksa && elems.rsn_ie &&
5024 wpa_parse_wpa_ie_rsn(elems.rsn_ie - 2, 2 + elems.rsn_ie_len,
5025 &data) == 0 &&
5026 data.num_pmkid == 1 && data.pmkid &&
5027 os_memcmp(sm->cur_pmksa->pmkid, data.pmkid, PMKID_LEN) == 0) {
5028 wpa_printf(MSG_DEBUG, "OWE: Use PMKSA caching");
5029 wpa_sm_set_pmk_from_pmksa(sm);
5030 return 0;
5031 }
5032
5033 if (!elems.owe_dh) {
5034 wpa_printf(MSG_INFO,
5035 "OWE: No Diffie-Hellman Parameter element found in Association Response frame");
5036 return -1;
5037 }
5038
5039 group = WPA_GET_LE16(elems.owe_dh);
5040 if (group != sm->owe_group) {
5041 wpa_printf(MSG_INFO,
5042 "OWE: Unexpected Diffie-Hellman group in response: %u",
5043 group);
5044 return -1;
5045 }
5046
5047 if (!sm->owe_ecdh) {
5048 wpa_printf(MSG_INFO, "OWE: No ECDH state available");
5049 return -1;
5050 }
5051
5052 if (group == 19)
5053 prime_len = 32;
5054 else if (group == 20)
5055 prime_len = 48;
5056 else if (group == 21)
5057 prime_len = 66;
5058 else
5059 return -1;
5060
5061 secret = crypto_ecdh_set_peerkey(sm->owe_ecdh, 0,
5062 elems.owe_dh + 2,
5063 elems.owe_dh_len - 2);
5064 secret = wpabuf_zeropad(secret, prime_len);
5065 if (!secret) {
5066 wpa_printf(MSG_DEBUG, "OWE: Invalid peer DH public key");
5067 return -1;
5068 }
5069 wpa_hexdump_buf_key(MSG_DEBUG, "OWE: DH shared secret", secret);
5070
5071 /* prk = HKDF-extract(C | A | group, z) */
5072
5073 pub = crypto_ecdh_get_pubkey(sm->owe_ecdh, 0);
5074 if (!pub) {
5075 wpabuf_clear_free(secret);
5076 return -1;
5077 }
5078
5079 /* PMKID = Truncate-128(Hash(C | A)) */
5080 addr[0] = wpabuf_head(pub);
5081 len[0] = wpabuf_len(pub);
5082 addr[1] = elems.owe_dh + 2;
5083 len[1] = elems.owe_dh_len - 2;
5084 if (group == 19) {
5085 res = sha256_vector(2, addr, len, pmkid);
5086 hash_len = SHA256_MAC_LEN;
5087 } else if (group == 20) {
5088 res = sha384_vector(2, addr, len, pmkid);
5089 hash_len = SHA384_MAC_LEN;
5090 } else if (group == 21) {
5091 res = sha512_vector(2, addr, len, pmkid);
5092 hash_len = SHA512_MAC_LEN;
5093 } else {
5094 res = -1;
5095 hash_len = 0;
5096 }
5097 pub = wpabuf_zeropad(pub, prime_len);
5098 if (res < 0 || !pub) {
5099 wpabuf_free(pub);
5100 wpabuf_clear_free(secret);
5101 return -1;
5102 }
5103
5104 hkey = wpabuf_alloc(wpabuf_len(pub) + elems.owe_dh_len - 2 + 2);
5105 if (!hkey) {
5106 wpabuf_free(pub);
5107 wpabuf_clear_free(secret);
5108 return -1;
5109 }
5110
5111 wpabuf_put_buf(hkey, pub); /* C */
5112 wpabuf_free(pub);
5113 wpabuf_put_data(hkey, elems.owe_dh + 2, elems.owe_dh_len - 2); /* A */
5114 wpabuf_put_le16(hkey, sm->owe_group); /* group */
5115 if (group == 19)
5116 res = hmac_sha256(wpabuf_head(hkey), wpabuf_len(hkey),
5117 wpabuf_head(secret), wpabuf_len(secret), prk);
5118 else if (group == 20)
5119 res = hmac_sha384(wpabuf_head(hkey), wpabuf_len(hkey),
5120 wpabuf_head(secret), wpabuf_len(secret), prk);
5121 else if (group == 21)
5122 res = hmac_sha512(wpabuf_head(hkey), wpabuf_len(hkey),
5123 wpabuf_head(secret), wpabuf_len(secret), prk);
5124 wpabuf_clear_free(hkey);
5125 wpabuf_clear_free(secret);
5126 if (res < 0)
5127 return -1;
5128
5129 wpa_hexdump_key(MSG_DEBUG, "OWE: prk", prk, hash_len);
5130
5131 /* PMK = HKDF-expand(prk, "OWE Key Generation", n) */
5132
5133 if (group == 19)
5134 res = hmac_sha256_kdf(prk, hash_len, NULL, (const u8 *) info,
5135 os_strlen(info), sm->pmk, hash_len);
5136 else if (group == 20)
5137 res = hmac_sha384_kdf(prk, hash_len, NULL, (const u8 *) info,
5138 os_strlen(info), sm->pmk, hash_len);
5139 else if (group == 21)
5140 res = hmac_sha512_kdf(prk, hash_len, NULL, (const u8 *) info,
5141 os_strlen(info), sm->pmk, hash_len);
Hai Shalom81f62d82019-07-22 12:10:00 -07005142 forced_memzero(prk, SHA512_MAC_LEN);
Roshan Pius3a1667e2018-07-03 15:17:14 -07005143 if (res < 0) {
5144 sm->pmk_len = 0;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005145 return -1;
Roshan Pius3a1667e2018-07-03 15:17:14 -07005146 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005147 sm->pmk_len = hash_len;
5148
5149 wpa_hexdump_key(MSG_DEBUG, "OWE: PMK", sm->pmk, sm->pmk_len);
5150 wpa_hexdump(MSG_DEBUG, "OWE: PMKID", pmkid, PMKID_LEN);
5151 pmksa_cache_add(sm->pmksa, sm->pmk, sm->pmk_len, pmkid, NULL, 0,
5152 bssid, sm->own_addr, sm->network_ctx, sm->key_mgmt,
5153 NULL);
5154
5155 return 0;
5156}
5157
5158#endif /* CONFIG_OWE */
5159
5160
5161void wpa_sm_set_fils_cache_id(struct wpa_sm *sm, const u8 *fils_cache_id)
5162{
5163#ifdef CONFIG_FILS
5164 if (sm && fils_cache_id) {
5165 sm->fils_cache_id_set = 1;
5166 os_memcpy(sm->fils_cache_id, fils_cache_id, FILS_CACHE_ID_LEN);
5167 }
5168#endif /* CONFIG_FILS */
5169}
Hai Shalom021b0b52019-04-10 11:17:58 -07005170
5171
5172#ifdef CONFIG_DPP2
5173void wpa_sm_set_dpp_z(struct wpa_sm *sm, const struct wpabuf *z)
5174{
5175 if (sm) {
5176 wpabuf_clear_free(sm->dpp_z);
5177 sm->dpp_z = z ? wpabuf_dup(z) : NULL;
5178 }
5179}
5180#endif /* CONFIG_DPP2 */