blob: ace0b6a780422279dc6a37cf2f83a551ef9cad6d [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"
Paul Stewart092955c2017-02-06 09:13:09 -080024#include "eap_common/eap_defs.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070025#include "eapol_supp/eapol_supp_sm.h"
Hai Shalom74f70d42019-02-11 14:42:39 -080026#include "drivers/driver.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070027#include "wpa.h"
28#include "eloop.h"
29#include "preauth.h"
30#include "pmksa_cache.h"
31#include "wpa_i.h"
32#include "wpa_ie.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070033
34
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080035static const u8 null_rsc[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
36
37
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070038/**
39 * wpa_eapol_key_send - Send WPA/RSN EAPOL-Key message
40 * @sm: Pointer to WPA state machine data from wpa_sm_init()
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -080041 * @ptk: PTK for Key Confirmation/Encryption Key
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070042 * @ver: Version field from Key Info
43 * @dest: Destination address for the frame
44 * @proto: Ethertype (usually ETH_P_EAPOL)
45 * @msg: EAPOL-Key message
46 * @msg_len: Length of message
47 * @key_mic: Pointer to the buffer to which the EAPOL-Key MIC is written
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080048 * Returns: >= 0 on success, < 0 on failure
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070049 */
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -080050int wpa_eapol_key_send(struct wpa_sm *sm, struct wpa_ptk *ptk,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080051 int ver, const u8 *dest, u16 proto,
52 u8 *msg, size_t msg_len, u8 *key_mic)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070053{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080054 int ret = -1;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070055 size_t mic_len = wpa_mic_len(sm->key_mgmt, sm->pmk_len);
Dmitry Shmidt807291d2015-01-27 13:40:23 -080056
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070057 wpa_printf(MSG_DEBUG, "WPA: Send EAPOL-Key frame to " MACSTR
58 " ver=%d mic_len=%d key_mgmt=0x%x",
59 MAC2STR(dest), ver, (int) mic_len, sm->key_mgmt);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070060 if (is_zero_ether_addr(dest) && is_zero_ether_addr(sm->bssid)) {
61 /*
62 * Association event was not yet received; try to fetch
63 * BSSID from the driver.
64 */
65 if (wpa_sm_get_bssid(sm, sm->bssid) < 0) {
66 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
67 "WPA: Failed to read BSSID for "
68 "EAPOL-Key destination address");
69 } else {
70 dest = sm->bssid;
71 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
72 "WPA: Use BSSID (" MACSTR
73 ") as the destination for EAPOL-Key",
74 MAC2STR(dest));
75 }
76 }
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -080077
78 if (mic_len) {
79 if (key_mic && (!ptk || !ptk->kck_len))
80 goto out;
81
82 if (key_mic &&
83 wpa_eapol_key_mic(ptk->kck, ptk->kck_len, sm->key_mgmt, ver,
84 msg, msg_len, key_mic)) {
85 wpa_msg(sm->ctx->msg_ctx, MSG_ERROR,
86 "WPA: Failed to generate EAPOL-Key version %d key_mgmt 0x%x MIC",
87 ver, sm->key_mgmt);
88 goto out;
89 }
Dmitry Shmidt29333592017-01-09 12:27:11 -080090 if (ptk)
91 wpa_hexdump_key(MSG_DEBUG, "WPA: KCK",
92 ptk->kck, ptk->kck_len);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -080093 wpa_hexdump(MSG_DEBUG, "WPA: Derived Key MIC",
94 key_mic, mic_len);
95 } else {
96#ifdef CONFIG_FILS
97 /* AEAD cipher - Key MIC field not used */
98 struct ieee802_1x_hdr *s_hdr, *hdr;
99 struct wpa_eapol_key *s_key, *key;
100 u8 *buf, *s_key_data, *key_data;
101 size_t buf_len = msg_len + AES_BLOCK_SIZE;
102 size_t key_data_len;
103 u16 eapol_len;
104 const u8 *aad[1];
105 size_t aad_len[1];
106
107 if (!ptk || !ptk->kek_len)
108 goto out;
109
110 key_data_len = msg_len - sizeof(struct ieee802_1x_hdr) -
111 sizeof(struct wpa_eapol_key) - 2;
112
113 buf = os_malloc(buf_len);
114 if (!buf)
115 goto out;
116
117 os_memcpy(buf, msg, msg_len);
118 hdr = (struct ieee802_1x_hdr *) buf;
119 key = (struct wpa_eapol_key *) (hdr + 1);
120 key_data = ((u8 *) (key + 1)) + 2;
121
122 /* Update EAPOL header to include AES-SIV overhead */
123 eapol_len = be_to_host16(hdr->length);
124 eapol_len += AES_BLOCK_SIZE;
125 hdr->length = host_to_be16(eapol_len);
126
127 /* Update Key Data Length field to include AES-SIV overhead */
128 WPA_PUT_BE16((u8 *) (key + 1), AES_BLOCK_SIZE + key_data_len);
129
130 s_hdr = (struct ieee802_1x_hdr *) msg;
131 s_key = (struct wpa_eapol_key *) (s_hdr + 1);
132 s_key_data = ((u8 *) (s_key + 1)) + 2;
133
134 wpa_hexdump_key(MSG_DEBUG, "WPA: Plaintext Key Data",
135 s_key_data, key_data_len);
136
137 wpa_hexdump_key(MSG_DEBUG, "WPA: KEK", ptk->kek, ptk->kek_len);
138 /* AES-SIV AAD from EAPOL protocol version field (inclusive) to
139 * to Key Data (exclusive). */
140 aad[0] = buf;
141 aad_len[0] = key_data - buf;
142 if (aes_siv_encrypt(ptk->kek, ptk->kek_len,
143 s_key_data, key_data_len,
144 1, aad, aad_len, key_data) < 0) {
145 os_free(buf);
146 goto out;
147 }
148
149 wpa_hexdump(MSG_DEBUG, "WPA: Encrypted Key Data from SIV",
150 key_data, AES_BLOCK_SIZE + key_data_len);
151
152 os_free(msg);
153 msg = buf;
154 msg_len = buf_len;
155#else /* CONFIG_FILS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700156 goto out;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800157#endif /* CONFIG_FILS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700158 }
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800159
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700160 wpa_hexdump(MSG_MSGDUMP, "WPA: TX EAPOL-Key", msg, msg_len);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800161 ret = wpa_sm_ether_send(sm, dest, proto, msg, msg_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700162 eapol_sm_notify_tx_eapol_key(sm->eapol);
163out:
164 os_free(msg);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800165 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700166}
167
168
169/**
170 * wpa_sm_key_request - Send EAPOL-Key Request
171 * @sm: Pointer to WPA state machine data from wpa_sm_init()
172 * @error: Indicate whether this is an Michael MIC error report
173 * @pairwise: 1 = error report for pairwise packet, 0 = for group packet
174 *
175 * Send an EAPOL-Key Request to the current authenticator. This function is
176 * used to request rekeying and it is usually called when a local Michael MIC
177 * failure is detected.
178 */
179void wpa_sm_key_request(struct wpa_sm *sm, int error, int pairwise)
180{
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800181 size_t mic_len, hdrlen, rlen;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700182 struct wpa_eapol_key *reply;
183 int key_info, ver;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800184 u8 bssid[ETH_ALEN], *rbuf, *key_mic, *mic;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700185
Roshan Pius3a1667e2018-07-03 15:17:14 -0700186 if (wpa_use_akm_defined(sm->key_mgmt))
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800187 ver = WPA_KEY_INFO_TYPE_AKM_DEFINED;
188 else if (wpa_key_mgmt_ft(sm->key_mgmt) ||
189 wpa_key_mgmt_sha256(sm->key_mgmt))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700190 ver = WPA_KEY_INFO_TYPE_AES_128_CMAC;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700191 else if (sm->pairwise_cipher != WPA_CIPHER_TKIP)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700192 ver = WPA_KEY_INFO_TYPE_HMAC_SHA1_AES;
193 else
194 ver = WPA_KEY_INFO_TYPE_HMAC_MD5_RC4;
195
196 if (wpa_sm_get_bssid(sm, bssid) < 0) {
197 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
198 "Failed to read BSSID for EAPOL-Key request");
199 return;
200 }
201
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700202 mic_len = wpa_mic_len(sm->key_mgmt, sm->pmk_len);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800203 hdrlen = sizeof(*reply) + mic_len + 2;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700204 rbuf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_KEY, NULL,
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800205 hdrlen, &rlen, (void *) &reply);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700206 if (rbuf == NULL)
207 return;
208
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800209 reply->type = (sm->proto == WPA_PROTO_RSN ||
210 sm->proto == WPA_PROTO_OSEN) ?
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700211 EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA;
212 key_info = WPA_KEY_INFO_REQUEST | ver;
213 if (sm->ptk_set)
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800214 key_info |= WPA_KEY_INFO_SECURE;
215 if (sm->ptk_set && mic_len)
216 key_info |= WPA_KEY_INFO_MIC;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700217 if (error)
218 key_info |= WPA_KEY_INFO_ERROR;
219 if (pairwise)
220 key_info |= WPA_KEY_INFO_KEY_TYPE;
221 WPA_PUT_BE16(reply->key_info, key_info);
222 WPA_PUT_BE16(reply->key_length, 0);
223 os_memcpy(reply->replay_counter, sm->request_counter,
224 WPA_REPLAY_COUNTER_LEN);
225 inc_byte_array(sm->request_counter, WPA_REPLAY_COUNTER_LEN);
226
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800227 mic = (u8 *) (reply + 1);
228 WPA_PUT_BE16(mic + mic_len, 0);
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800229 if (!(key_info & WPA_KEY_INFO_MIC))
230 key_mic = NULL;
231 else
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800232 key_mic = mic;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700233
234 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
235 "WPA: Sending EAPOL-Key Request (error=%d "
236 "pairwise=%d ptk_set=%d len=%lu)",
237 error, pairwise, sm->ptk_set, (unsigned long) rlen);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800238 wpa_eapol_key_send(sm, &sm->ptk, ver, bssid, ETH_P_EAPOL, rbuf, rlen,
239 key_mic);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700240}
241
242
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800243static void wpa_supplicant_key_mgmt_set_pmk(struct wpa_sm *sm)
244{
245#ifdef CONFIG_IEEE80211R
246 if (sm->key_mgmt == WPA_KEY_MGMT_FT_IEEE8021X) {
247 if (wpa_sm_key_mgmt_set_pmk(sm, sm->xxkey, sm->xxkey_len))
248 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
249 "RSN: Cannot set low order 256 bits of MSK for key management offload");
250 } else {
251#endif /* CONFIG_IEEE80211R */
252 if (wpa_sm_key_mgmt_set_pmk(sm, sm->pmk, sm->pmk_len))
253 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
254 "RSN: Cannot set PMK for key management offload");
255#ifdef CONFIG_IEEE80211R
256 }
257#endif /* CONFIG_IEEE80211R */
258}
259
260
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700261static int wpa_supplicant_get_pmk(struct wpa_sm *sm,
262 const unsigned char *src_addr,
263 const u8 *pmkid)
264{
265 int abort_cached = 0;
266
267 if (pmkid && !sm->cur_pmksa) {
268 /* When using drivers that generate RSN IE, wpa_supplicant may
269 * not have enough time to get the association information
270 * event before receiving this 1/4 message, so try to find a
271 * matching PMKSA cache entry here. */
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800272 sm->cur_pmksa = pmksa_cache_get(sm->pmksa, src_addr, pmkid,
Roshan Pius3a1667e2018-07-03 15:17:14 -0700273 NULL, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700274 if (sm->cur_pmksa) {
275 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
276 "RSN: found matching PMKID from PMKSA cache");
277 } else {
278 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
279 "RSN: no matching PMKID found");
280 abort_cached = 1;
281 }
282 }
283
284 if (pmkid && sm->cur_pmksa &&
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700285 os_memcmp_const(pmkid, sm->cur_pmksa->pmkid, PMKID_LEN) == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700286 wpa_hexdump(MSG_DEBUG, "RSN: matched PMKID", pmkid, PMKID_LEN);
287 wpa_sm_set_pmk_from_pmksa(sm);
288 wpa_hexdump_key(MSG_DEBUG, "RSN: PMK from PMKSA cache",
289 sm->pmk, sm->pmk_len);
290 eapol_sm_notify_cached(sm->eapol);
291#ifdef CONFIG_IEEE80211R
292 sm->xxkey_len = 0;
Roshan Pius3a1667e2018-07-03 15:17:14 -0700293#ifdef CONFIG_SAE
294 if (sm->key_mgmt == WPA_KEY_MGMT_FT_SAE &&
295 sm->pmk_len == PMK_LEN) {
296 /* Need to allow FT key derivation to proceed with
297 * PMK from SAE being used as the XXKey in cases where
298 * the PMKID in msg 1/4 matches the PMKSA entry that was
299 * just added based on SAE authentication for the
300 * initial mobility domain association. */
301 os_memcpy(sm->xxkey, sm->pmk, sm->pmk_len);
302 sm->xxkey_len = sm->pmk_len;
303 }
304#endif /* CONFIG_SAE */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700305#endif /* CONFIG_IEEE80211R */
306 } else if (wpa_key_mgmt_wpa_ieee8021x(sm->key_mgmt) && sm->eapol) {
307 int res, pmk_len;
Hai Shalom81f62d82019-07-22 12:10:00 -0700308#ifdef CONFIG_IEEE80211R
309 u8 buf[2 * PMK_LEN];
310#endif /* CONFIG_IEEE80211R */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800311
Dmitry Shmidtebd93af2017-02-21 13:40:44 -0800312 if (wpa_key_mgmt_sha384(sm->key_mgmt))
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800313 pmk_len = PMK_LEN_SUITE_B_192;
314 else
315 pmk_len = PMK_LEN;
316 res = eapol_sm_get_key(sm->eapol, sm->pmk, pmk_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700317 if (res) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800318 if (pmk_len == PMK_LEN) {
319 /*
320 * EAP-LEAP is an exception from other EAP
321 * methods: it uses only 16-byte PMK.
322 */
323 res = eapol_sm_get_key(sm->eapol, sm->pmk, 16);
324 pmk_len = 16;
325 }
Hai Shalomf1c97642019-07-19 23:42:07 +0000326 }
Hai Shalom81f62d82019-07-22 12:10:00 -0700327#ifdef CONFIG_IEEE80211R
328 if (res == 0 &&
329 eapol_sm_get_key(sm->eapol, buf, 2 * PMK_LEN) == 0) {
330 if (wpa_key_mgmt_sha384(sm->key_mgmt)) {
331 os_memcpy(sm->xxkey, buf, SHA384_MAC_LEN);
332 sm->xxkey_len = SHA384_MAC_LEN;
333 } else {
334 os_memcpy(sm->xxkey, buf + PMK_LEN, PMK_LEN);
335 sm->xxkey_len = PMK_LEN;
336 }
337 forced_memzero(buf, sizeof(buf));
338 if (sm->proto == WPA_PROTO_RSN &&
339 wpa_key_mgmt_ft(sm->key_mgmt)) {
340 struct rsn_pmksa_cache_entry *sa = NULL;
341 const u8 *fils_cache_id = NULL;
342
343#ifdef CONFIG_FILS
344 if (sm->fils_cache_id_set)
345 fils_cache_id = sm->fils_cache_id;
346#endif /* CONFIG_FILS */
347 wpa_hexdump_key(MSG_DEBUG,
348 "FT: Cache XXKey/MPMK",
349 sm->xxkey, sm->xxkey_len);
350 sa = pmksa_cache_add(sm->pmksa,
351 sm->xxkey, sm->xxkey_len,
352 NULL, NULL, 0,
353 src_addr, sm->own_addr,
354 sm->network_ctx,
355 sm->key_mgmt,
356 fils_cache_id);
357 if (!sm->cur_pmksa)
358 sm->cur_pmksa = sa;
359 }
360 }
361#endif /* CONFIG_IEEE80211R */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700362 if (res == 0) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700363 struct rsn_pmksa_cache_entry *sa = NULL;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700364 const u8 *fils_cache_id = NULL;
365
366#ifdef CONFIG_FILS
367 if (sm->fils_cache_id_set)
368 fils_cache_id = sm->fils_cache_id;
369#endif /* CONFIG_FILS */
370
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700371 wpa_hexdump_key(MSG_DEBUG, "WPA: PMK from EAPOL state "
372 "machines", sm->pmk, pmk_len);
373 sm->pmk_len = pmk_len;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800374 wpa_supplicant_key_mgmt_set_pmk(sm);
Dmitry Shmidtc55524a2011-07-07 11:18:38 -0700375 if (sm->proto == WPA_PROTO_RSN &&
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800376 !wpa_key_mgmt_suite_b(sm->key_mgmt) &&
Dmitry Shmidtc55524a2011-07-07 11:18:38 -0700377 !wpa_key_mgmt_ft(sm->key_mgmt)) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700378 sa = pmksa_cache_add(sm->pmksa,
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800379 sm->pmk, pmk_len, NULL,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800380 NULL, 0,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700381 src_addr, sm->own_addr,
382 sm->network_ctx,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700383 sm->key_mgmt,
384 fils_cache_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700385 }
386 if (!sm->cur_pmksa && pmkid &&
Roshan Pius3a1667e2018-07-03 15:17:14 -0700387 pmksa_cache_get(sm->pmksa, src_addr, pmkid, NULL,
388 0)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700389 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
390 "RSN: the new PMK matches with the "
391 "PMKID");
392 abort_cached = 0;
Jouni Malinen6ec30382015-07-08 20:48:18 +0300393 } else if (sa && !sm->cur_pmksa && pmkid) {
394 /*
395 * It looks like the authentication server
396 * derived mismatching MSK. This should not
397 * really happen, but bugs happen.. There is not
398 * much we can do here without knowing what
399 * exactly caused the server to misbehave.
400 */
Dmitry Shmidt014a3ff2015-12-28 13:27:49 -0800401 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
Jouni Malinen6ec30382015-07-08 20:48:18 +0300402 "RSN: PMKID mismatch - authentication server may have derived different MSK?!");
403 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700404 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700405
406 if (!sm->cur_pmksa)
407 sm->cur_pmksa = sa;
Hai Shalom81f62d82019-07-22 12:10:00 -0700408#ifdef CONFIG_IEEE80211R
409 } else if (wpa_key_mgmt_ft(sm->key_mgmt) && sm->ft_protocol) {
410 wpa_printf(MSG_DEBUG,
411 "FT: Continue 4-way handshake without PMK/PMKID for association using FT protocol");
412#endif /* CONFIG_IEEE80211R */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700413 } else {
414 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
415 "WPA: Failed to get master session key from "
416 "EAPOL state machines - key handshake "
417 "aborted");
418 if (sm->cur_pmksa) {
419 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
420 "RSN: Cancelled PMKSA caching "
421 "attempt");
422 sm->cur_pmksa = NULL;
423 abort_cached = 1;
424 } else if (!abort_cached) {
425 return -1;
426 }
427 }
428 }
429
Dmitry Shmidtc55524a2011-07-07 11:18:38 -0700430 if (abort_cached && wpa_key_mgmt_wpa_ieee8021x(sm->key_mgmt) &&
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800431 !wpa_key_mgmt_suite_b(sm->key_mgmt) &&
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800432 !wpa_key_mgmt_ft(sm->key_mgmt) && sm->key_mgmt != WPA_KEY_MGMT_OSEN)
433 {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700434 /* Send EAPOL-Start to trigger full EAP authentication. */
435 u8 *buf;
436 size_t buflen;
437
438 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
439 "RSN: no PMKSA entry found - trigger "
440 "full EAP authentication");
441 buf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_START,
442 NULL, 0, &buflen, NULL);
443 if (buf) {
444 wpa_sm_ether_send(sm, sm->bssid, ETH_P_EAPOL,
445 buf, buflen);
446 os_free(buf);
447 return -2;
448 }
449
450 return -1;
451 }
452
453 return 0;
454}
455
456
457/**
458 * wpa_supplicant_send_2_of_4 - Send message 2 of WPA/RSN 4-Way Handshake
459 * @sm: Pointer to WPA state machine data from wpa_sm_init()
460 * @dst: Destination address for the frame
461 * @key: Pointer to the EAPOL-Key frame header
462 * @ver: Version bits from EAPOL-Key Key Info
463 * @nonce: Nonce value for the EAPOL-Key frame
464 * @wpa_ie: WPA/RSN IE
465 * @wpa_ie_len: Length of the WPA/RSN IE
466 * @ptk: PTK to use for keyed hash and encryption
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800467 * Returns: >= 0 on success, < 0 on failure
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700468 */
469int wpa_supplicant_send_2_of_4(struct wpa_sm *sm, const unsigned char *dst,
470 const struct wpa_eapol_key *key,
471 int ver, const u8 *nonce,
472 const u8 *wpa_ie, size_t wpa_ie_len,
473 struct wpa_ptk *ptk)
474{
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800475 size_t mic_len, hdrlen, rlen;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700476 struct wpa_eapol_key *reply;
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800477 u8 *rbuf, *key_mic;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700478 u8 *rsn_ie_buf = NULL;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800479 u16 key_info;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700480
481 if (wpa_ie == NULL) {
482 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, "WPA: No wpa_ie set - "
483 "cannot generate msg 2/4");
484 return -1;
485 }
486
487#ifdef CONFIG_IEEE80211R
488 if (wpa_key_mgmt_ft(sm->key_mgmt)) {
489 int res;
490
491 /*
492 * Add PMKR1Name into RSN IE (PMKID-List) and add MDIE and
493 * FTIE from (Re)Association Response.
494 */
495 rsn_ie_buf = os_malloc(wpa_ie_len + 2 + 2 + PMKID_LEN +
496 sm->assoc_resp_ies_len);
497 if (rsn_ie_buf == NULL)
498 return -1;
499 os_memcpy(rsn_ie_buf, wpa_ie, wpa_ie_len);
Dmitry Shmidt55840ad2015-12-14 12:45:46 -0800500 res = wpa_insert_pmkid(rsn_ie_buf, &wpa_ie_len,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700501 sm->pmk_r1_name);
502 if (res < 0) {
503 os_free(rsn_ie_buf);
504 return -1;
505 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700506
507 if (sm->assoc_resp_ies) {
508 os_memcpy(rsn_ie_buf + wpa_ie_len, sm->assoc_resp_ies,
509 sm->assoc_resp_ies_len);
510 wpa_ie_len += sm->assoc_resp_ies_len;
511 }
512
513 wpa_ie = rsn_ie_buf;
514 }
515#endif /* CONFIG_IEEE80211R */
516
517 wpa_hexdump(MSG_DEBUG, "WPA: WPA IE for msg 2/4", wpa_ie, wpa_ie_len);
518
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700519 mic_len = wpa_mic_len(sm->key_mgmt, sm->pmk_len);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800520 hdrlen = sizeof(*reply) + mic_len + 2;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700521 rbuf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_KEY,
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800522 NULL, hdrlen + wpa_ie_len,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700523 &rlen, (void *) &reply);
524 if (rbuf == NULL) {
525 os_free(rsn_ie_buf);
526 return -1;
527 }
528
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800529 reply->type = (sm->proto == WPA_PROTO_RSN ||
530 sm->proto == WPA_PROTO_OSEN) ?
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700531 EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800532 key_info = ver | WPA_KEY_INFO_KEY_TYPE;
533 if (mic_len)
534 key_info |= WPA_KEY_INFO_MIC;
535 else
536 key_info |= WPA_KEY_INFO_ENCR_KEY_DATA;
537 WPA_PUT_BE16(reply->key_info, key_info);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800538 if (sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700539 WPA_PUT_BE16(reply->key_length, 0);
540 else
541 os_memcpy(reply->key_length, key->key_length, 2);
542 os_memcpy(reply->replay_counter, key->replay_counter,
543 WPA_REPLAY_COUNTER_LEN);
544 wpa_hexdump(MSG_DEBUG, "WPA: Replay Counter", reply->replay_counter,
545 WPA_REPLAY_COUNTER_LEN);
546
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800547 key_mic = (u8 *) (reply + 1);
548 WPA_PUT_BE16(key_mic + mic_len, wpa_ie_len); /* Key Data Length */
549 os_memcpy(key_mic + mic_len + 2, wpa_ie, wpa_ie_len); /* Key Data */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700550 os_free(rsn_ie_buf);
551
552 os_memcpy(reply->key_nonce, nonce, WPA_NONCE_LEN);
553
Roshan Pius5e7db942018-04-12 12:27:41 -0700554 wpa_dbg(sm->ctx->msg_ctx, MSG_INFO, "WPA: Sending EAPOL-Key 2/4");
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800555 return wpa_eapol_key_send(sm, ptk, ver, dst, ETH_P_EAPOL, rbuf, rlen,
556 key_mic);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700557}
558
559
560static int wpa_derive_ptk(struct wpa_sm *sm, const unsigned char *src_addr,
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800561 const struct wpa_eapol_key *key, struct wpa_ptk *ptk)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700562{
Hai Shalom021b0b52019-04-10 11:17:58 -0700563 const u8 *z = NULL;
564 size_t z_len = 0;
565
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700566#ifdef CONFIG_IEEE80211R
567 if (wpa_key_mgmt_ft(sm->key_mgmt))
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800568 return wpa_derive_ptk_ft(sm, src_addr, key, ptk);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700569#endif /* CONFIG_IEEE80211R */
570
Hai Shalom021b0b52019-04-10 11:17:58 -0700571#ifdef CONFIG_DPP2
572 if (sm->key_mgmt == WPA_KEY_MGMT_DPP && sm->dpp_z) {
573 z = wpabuf_head(sm->dpp_z);
574 z_len = wpabuf_len(sm->dpp_z);
575 }
576#endif /* CONFIG_DPP2 */
577
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800578 return wpa_pmk_to_ptk(sm->pmk, sm->pmk_len, "Pairwise key expansion",
579 sm->own_addr, sm->bssid, sm->snonce,
580 key->key_nonce, ptk, sm->key_mgmt,
Hai Shalom021b0b52019-04-10 11:17:58 -0700581 sm->pairwise_cipher, z, z_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700582}
583
584
585static void wpa_supplicant_process_1_of_4(struct wpa_sm *sm,
586 const unsigned char *src_addr,
587 const struct wpa_eapol_key *key,
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700588 u16 ver, const u8 *key_data,
589 size_t key_data_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700590{
591 struct wpa_eapol_ie_parse ie;
592 struct wpa_ptk *ptk;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700593 int res;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800594 u8 *kde, *kde_buf = NULL;
595 size_t kde_len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700596
597 if (wpa_sm_get_network_ctx(sm) == NULL) {
598 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, "WPA: No SSID info "
599 "found (msg 1 of 4)");
600 return;
601 }
602
603 wpa_sm_set_state(sm, WPA_4WAY_HANDSHAKE);
Roshan Pius5e7db942018-04-12 12:27:41 -0700604 wpa_dbg(sm->ctx->msg_ctx, MSG_INFO, "WPA: RX message 1 of 4-Way "
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700605 "Handshake from " MACSTR " (ver=%d)", MAC2STR(src_addr), ver);
606
607 os_memset(&ie, 0, sizeof(ie));
608
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800609 if (sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700610 /* RSN: msg 1/4 should contain PMKID for the selected PMK */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700611 wpa_hexdump(MSG_DEBUG, "RSN: msg 1/4 key data",
612 key_data, key_data_len);
613 if (wpa_supplicant_parse_ies(key_data, key_data_len, &ie) < 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800614 goto failed;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700615 if (ie.pmkid) {
616 wpa_hexdump(MSG_DEBUG, "RSN: PMKID from "
617 "Authenticator", ie.pmkid, PMKID_LEN);
618 }
619 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700620
621 res = wpa_supplicant_get_pmk(sm, src_addr, ie.pmkid);
622 if (res == -2) {
623 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "RSN: Do not reply to "
624 "msg 1/4 - requesting full EAP authentication");
625 return;
626 }
627 if (res)
628 goto failed;
629
630 if (sm->renew_snonce) {
631 if (random_get_bytes(sm->snonce, WPA_NONCE_LEN)) {
632 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
633 "WPA: Failed to get random data for SNonce");
634 goto failed;
635 }
636 sm->renew_snonce = 0;
637 wpa_hexdump(MSG_DEBUG, "WPA: Renewed SNonce",
638 sm->snonce, WPA_NONCE_LEN);
639 }
640
641 /* Calculate PTK which will be stored as a temporary PTK until it has
642 * been verified when processing message 3/4. */
643 ptk = &sm->tptk;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700644 if (wpa_derive_ptk(sm, src_addr, key, ptk) < 0)
645 goto failed;
Dmitry Shmidt98660862014-03-11 17:26:21 -0700646 if (sm->pairwise_cipher == WPA_CIPHER_TKIP) {
Dmitry Shmidt61593f02014-04-21 16:27:35 -0700647 u8 buf[8];
Dmitry Shmidt98660862014-03-11 17:26:21 -0700648 /* Supplicant: swap tx/rx Mic keys */
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800649 os_memcpy(buf, &ptk->tk[16], 8);
650 os_memcpy(&ptk->tk[16], &ptk->tk[24], 8);
651 os_memcpy(&ptk->tk[24], buf, 8);
Hai Shalom81f62d82019-07-22 12:10:00 -0700652 forced_memzero(buf, sizeof(buf));
Dmitry Shmidt98660862014-03-11 17:26:21 -0700653 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700654 sm->tptk_set = 1;
655
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800656 kde = sm->assoc_wpa_ie;
657 kde_len = sm->assoc_wpa_ie_len;
Hai Shalomc3565922019-10-28 11:58:20 -0700658 kde_buf = os_malloc(kde_len +
659 2 + RSN_SELECTOR_LEN + 3 +
660 sm->assoc_rsnxe_len +
661 2 + RSN_SELECTOR_LEN + 1);
662 if (!kde_buf)
663 goto failed;
664 os_memcpy(kde_buf, kde, kde_len);
665 kde = kde_buf;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800666
Hai Shalom74f70d42019-02-11 14:42:39 -0800667#ifdef CONFIG_OCV
668 if (wpa_sm_ocv_enabled(sm)) {
669 struct wpa_channel_info ci;
670 u8 *pos;
671
Hai Shalomc3565922019-10-28 11:58:20 -0700672 pos = kde + kde_len;
Hai Shalom74f70d42019-02-11 14:42:39 -0800673 if (wpa_sm_channel_info(sm, &ci) != 0) {
674 wpa_printf(MSG_WARNING,
675 "Failed to get channel info for OCI element in EAPOL-Key 2/4");
676 goto failed;
677 }
678
Hai Shalom74f70d42019-02-11 14:42:39 -0800679 if (ocv_insert_oci_kde(&ci, &pos) < 0)
680 goto failed;
681 kde_len = pos - kde;
682 }
683#endif /* CONFIG_OCV */
684
Hai Shalomc3565922019-10-28 11:58:20 -0700685 if (sm->assoc_rsnxe && sm->assoc_rsnxe_len) {
686 os_memcpy(kde + kde_len, sm->assoc_rsnxe, sm->assoc_rsnxe_len);
687 kde_len += sm->assoc_rsnxe_len;
688 }
689
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800690#ifdef CONFIG_P2P
691 if (sm->p2p) {
Hai Shalomc3565922019-10-28 11:58:20 -0700692 u8 *pos;
693
694 wpa_printf(MSG_DEBUG,
695 "P2P: Add IP Address Request KDE into EAPOL-Key 2/4");
696 pos = kde + kde_len;
697 *pos++ = WLAN_EID_VENDOR_SPECIFIC;
698 *pos++ = RSN_SELECTOR_LEN + 1;
699 RSN_SELECTOR_PUT(pos, WFA_KEY_DATA_IP_ADDR_REQ);
700 pos += RSN_SELECTOR_LEN;
701 *pos++ = 0x01;
702 kde_len = pos - kde;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800703 }
704#endif /* CONFIG_P2P */
705
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700706 if (wpa_supplicant_send_2_of_4(sm, sm->bssid, key, ver, sm->snonce,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800707 kde, kde_len, ptk) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700708 goto failed;
709
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800710 os_free(kde_buf);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700711 os_memcpy(sm->anonce, key->key_nonce, WPA_NONCE_LEN);
712 return;
713
714failed:
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800715 os_free(kde_buf);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700716 wpa_sm_deauthenticate(sm, WLAN_REASON_UNSPECIFIED);
717}
718
719
720static void wpa_sm_start_preauth(void *eloop_ctx, void *timeout_ctx)
721{
722 struct wpa_sm *sm = eloop_ctx;
723 rsn_preauth_candidate_process(sm);
724}
725
726
727static void wpa_supplicant_key_neg_complete(struct wpa_sm *sm,
728 const u8 *addr, int secure)
729{
730 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
731 "WPA: Key negotiation completed with "
732 MACSTR " [PTK=%s GTK=%s]", MAC2STR(addr),
733 wpa_cipher_txt(sm->pairwise_cipher),
734 wpa_cipher_txt(sm->group_cipher));
735 wpa_sm_cancel_auth_timeout(sm);
736 wpa_sm_set_state(sm, WPA_COMPLETED);
737
738 if (secure) {
739 wpa_sm_mlme_setprotection(
740 sm, addr, MLME_SETPROTECTION_PROTECT_TYPE_RX_TX,
741 MLME_SETPROTECTION_KEY_TYPE_PAIRWISE);
742 eapol_sm_notify_portValid(sm->eapol, TRUE);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700743 if (wpa_key_mgmt_wpa_psk(sm->key_mgmt) ||
744 sm->key_mgmt == WPA_KEY_MGMT_DPP ||
745 sm->key_mgmt == WPA_KEY_MGMT_OWE)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700746 eapol_sm_notify_eap_success(sm->eapol, TRUE);
747 /*
748 * Start preauthentication after a short wait to avoid a
749 * possible race condition between the data receive and key
750 * configuration after the 4-Way Handshake. This increases the
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800751 * likelihood of the first preauth EAPOL-Start frame getting to
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700752 * the target AP.
753 */
Hai Shalom74f70d42019-02-11 14:42:39 -0800754 if (!dl_list_empty(&sm->pmksa_candidates))
755 eloop_register_timeout(1, 0, wpa_sm_start_preauth,
756 sm, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700757 }
758
759 if (sm->cur_pmksa && sm->cur_pmksa->opportunistic) {
760 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
761 "RSN: Authenticator accepted "
762 "opportunistic PMKSA entry - marking it valid");
763 sm->cur_pmksa->opportunistic = 0;
764 }
765
766#ifdef CONFIG_IEEE80211R
767 if (wpa_key_mgmt_ft(sm->key_mgmt)) {
768 /* Prepare for the next transition */
769 wpa_ft_prepare_auth_request(sm, NULL);
770 }
771#endif /* CONFIG_IEEE80211R */
772}
773
774
775static void wpa_sm_rekey_ptk(void *eloop_ctx, void *timeout_ctx)
776{
777 struct wpa_sm *sm = eloop_ctx;
778 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Request PTK rekeying");
779 wpa_sm_key_request(sm, 0, 1);
780}
781
782
783static int wpa_supplicant_install_ptk(struct wpa_sm *sm,
784 const struct wpa_eapol_key *key)
785{
786 int keylen, rsclen;
787 enum wpa_alg alg;
788 const u8 *key_rsc;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800789
Mathy Vanhoefc66556c2017-09-29 04:22:51 +0200790 if (sm->ptk.installed) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800791 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
792 "WPA: Do not re-install same PTK to the driver");
793 return 0;
794 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700795
796 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
797 "WPA: Installing PTK to the driver");
798
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700799 if (sm->pairwise_cipher == WPA_CIPHER_NONE) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700800 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Pairwise Cipher "
801 "Suite: NONE - do not use pairwise keys");
802 return 0;
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700803 }
804
805 if (!wpa_cipher_valid_pairwise(sm->pairwise_cipher)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700806 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
807 "WPA: Unsupported pairwise cipher %d",
808 sm->pairwise_cipher);
809 return -1;
810 }
811
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700812 alg = wpa_cipher_to_alg(sm->pairwise_cipher);
813 keylen = wpa_cipher_key_len(sm->pairwise_cipher);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700814 if (keylen <= 0 || (unsigned int) keylen != sm->ptk.tk_len) {
815 wpa_printf(MSG_DEBUG, "WPA: TK length mismatch: %d != %lu",
816 keylen, (long unsigned int) sm->ptk.tk_len);
817 return -1;
818 }
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700819 rsclen = wpa_cipher_rsc_len(sm->pairwise_cipher);
820
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800821 if (sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700822 key_rsc = null_rsc;
823 } else {
824 key_rsc = key->key_rsc;
825 wpa_hexdump(MSG_DEBUG, "WPA: RSC", key_rsc, rsclen);
826 }
827
828 if (wpa_sm_set_key(sm, alg, sm->bssid, 0, 1, key_rsc, rsclen,
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800829 sm->ptk.tk, keylen) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700830 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
831 "WPA: Failed to set PTK to the "
832 "driver (alg=%d keylen=%d bssid=" MACSTR ")",
833 alg, keylen, MAC2STR(sm->bssid));
834 return -1;
835 }
836
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800837 /* TK is not needed anymore in supplicant */
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800838 os_memset(sm->ptk.tk, 0, WPA_TK_MAX_LEN);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700839 sm->ptk.tk_len = 0;
Mathy Vanhoefc66556c2017-09-29 04:22:51 +0200840 sm->ptk.installed = 1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800841
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700842 if (sm->wpa_ptk_rekey) {
843 eloop_cancel_timeout(wpa_sm_rekey_ptk, sm, NULL);
844 eloop_register_timeout(sm->wpa_ptk_rekey, 0, wpa_sm_rekey_ptk,
845 sm, NULL);
846 }
847
848 return 0;
849}
850
851
852static int wpa_supplicant_check_group_cipher(struct wpa_sm *sm,
853 int group_cipher,
854 int keylen, int maxkeylen,
855 int *key_rsc_len,
856 enum wpa_alg *alg)
857{
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700858 int klen;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700859
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700860 *alg = wpa_cipher_to_alg(group_cipher);
861 if (*alg == WPA_ALG_NONE) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700862 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
863 "WPA: Unsupported Group Cipher %d",
864 group_cipher);
865 return -1;
866 }
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700867 *key_rsc_len = wpa_cipher_rsc_len(group_cipher);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700868
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700869 klen = wpa_cipher_key_len(group_cipher);
870 if (keylen != klen || maxkeylen < klen) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700871 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
872 "WPA: Unsupported %s Group Cipher key length %d (%d)",
873 wpa_cipher_txt(group_cipher), keylen, maxkeylen);
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700874 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700875 }
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700876 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700877}
878
879
880struct wpa_gtk_data {
881 enum wpa_alg alg;
882 int tx, key_rsc_len, keyidx;
883 u8 gtk[32];
884 int gtk_len;
885};
886
887
888static int wpa_supplicant_install_gtk(struct wpa_sm *sm,
889 const struct wpa_gtk_data *gd,
Jouni Malinen58c0e962017-10-01 12:12:24 +0300890 const u8 *key_rsc, int wnm_sleep)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700891{
892 const u8 *_gtk = gd->gtk;
893 u8 gtk_buf[32];
894
Mathy Vanhoef10bfd642017-07-12 16:03:24 +0200895 /* Detect possible key reinstallation */
Jouni Malinen58c0e962017-10-01 12:12:24 +0300896 if ((sm->gtk.gtk_len == (size_t) gd->gtk_len &&
897 os_memcmp(sm->gtk.gtk, gd->gtk, sm->gtk.gtk_len) == 0) ||
898 (sm->gtk_wnm_sleep.gtk_len == (size_t) gd->gtk_len &&
899 os_memcmp(sm->gtk_wnm_sleep.gtk, gd->gtk,
900 sm->gtk_wnm_sleep.gtk_len) == 0)) {
Mathy Vanhoef10bfd642017-07-12 16:03:24 +0200901 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
902 "WPA: Not reinstalling already in-use GTK to the driver (keyidx=%d tx=%d len=%d)",
903 gd->keyidx, gd->tx, gd->gtk_len);
904 return 0;
905 }
906
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700907 wpa_hexdump_key(MSG_DEBUG, "WPA: Group Key", gd->gtk, gd->gtk_len);
908 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
909 "WPA: Installing GTK to the driver (keyidx=%d tx=%d len=%d)",
910 gd->keyidx, gd->tx, gd->gtk_len);
911 wpa_hexdump(MSG_DEBUG, "WPA: RSC", key_rsc, gd->key_rsc_len);
912 if (sm->group_cipher == WPA_CIPHER_TKIP) {
913 /* Swap Tx/Rx keys for Michael MIC */
914 os_memcpy(gtk_buf, gd->gtk, 16);
915 os_memcpy(gtk_buf + 16, gd->gtk + 24, 8);
916 os_memcpy(gtk_buf + 24, gd->gtk + 16, 8);
917 _gtk = gtk_buf;
918 }
919 if (sm->pairwise_cipher == WPA_CIPHER_NONE) {
920 if (wpa_sm_set_key(sm, gd->alg, NULL,
921 gd->keyidx, 1, key_rsc, gd->key_rsc_len,
922 _gtk, gd->gtk_len) < 0) {
923 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
924 "WPA: Failed to set GTK to the driver "
925 "(Group only)");
Hai Shalom81f62d82019-07-22 12:10:00 -0700926 forced_memzero(gtk_buf, sizeof(gtk_buf));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700927 return -1;
928 }
929 } else if (wpa_sm_set_key(sm, gd->alg, broadcast_ether_addr,
930 gd->keyidx, gd->tx, key_rsc, gd->key_rsc_len,
931 _gtk, gd->gtk_len) < 0) {
932 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
933 "WPA: Failed to set GTK to "
934 "the driver (alg=%d keylen=%d keyidx=%d)",
935 gd->alg, gd->gtk_len, gd->keyidx);
Hai Shalom81f62d82019-07-22 12:10:00 -0700936 forced_memzero(gtk_buf, sizeof(gtk_buf));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700937 return -1;
938 }
Hai Shalom81f62d82019-07-22 12:10:00 -0700939 forced_memzero(gtk_buf, sizeof(gtk_buf));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700940
Jouni Malinen58c0e962017-10-01 12:12:24 +0300941 if (wnm_sleep) {
942 sm->gtk_wnm_sleep.gtk_len = gd->gtk_len;
943 os_memcpy(sm->gtk_wnm_sleep.gtk, gd->gtk,
944 sm->gtk_wnm_sleep.gtk_len);
945 } else {
946 sm->gtk.gtk_len = gd->gtk_len;
947 os_memcpy(sm->gtk.gtk, gd->gtk, sm->gtk.gtk_len);
948 }
Mathy Vanhoef10bfd642017-07-12 16:03:24 +0200949
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700950 return 0;
951}
952
953
954static int wpa_supplicant_gtk_tx_bit_workaround(const struct wpa_sm *sm,
955 int tx)
956{
957 if (tx && sm->pairwise_cipher != WPA_CIPHER_NONE) {
958 /* Ignore Tx bit for GTK if a pairwise key is used. One AP
959 * seemed to set this bit (incorrectly, since Tx is only when
960 * doing Group Key only APs) and without this workaround, the
961 * data connection does not work because wpa_supplicant
962 * configured non-zero keyidx to be used for unicast. */
963 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
964 "WPA: Tx bit set for GTK, but pairwise "
965 "keys are used - ignore Tx bit");
966 return 0;
967 }
968 return tx;
969}
970
971
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800972static int wpa_supplicant_rsc_relaxation(const struct wpa_sm *sm,
973 const u8 *rsc)
974{
975 int rsclen;
976
977 if (!sm->wpa_rsc_relaxation)
978 return 0;
979
980 rsclen = wpa_cipher_rsc_len(sm->group_cipher);
981
982 /*
983 * Try to detect RSC (endian) corruption issue where the AP sends
984 * the RSC bytes in EAPOL-Key message in the wrong order, both if
985 * it's actually a 6-byte field (as it should be) and if it treats
986 * it as an 8-byte field.
987 * An AP model known to have this bug is the Sapido RB-1632.
988 */
989 if (rsclen == 6 && ((rsc[5] && !rsc[0]) || rsc[6] || rsc[7])) {
990 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
991 "RSC %02x%02x%02x%02x%02x%02x%02x%02x is likely bogus, using 0",
992 rsc[0], rsc[1], rsc[2], rsc[3],
993 rsc[4], rsc[5], rsc[6], rsc[7]);
994
995 return 1;
996 }
997
998 return 0;
999}
1000
1001
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001002static int wpa_supplicant_pairwise_gtk(struct wpa_sm *sm,
1003 const struct wpa_eapol_key *key,
1004 const u8 *gtk, size_t gtk_len,
1005 int key_info)
1006{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001007 struct wpa_gtk_data gd;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001008 const u8 *key_rsc;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001009
1010 /*
1011 * IEEE Std 802.11i-2004 - 8.5.2 EAPOL-Key frames - Figure 43x
1012 * GTK KDE format:
1013 * KeyID[bits 0-1], Tx [bit 2], Reserved [bits 3-7]
1014 * Reserved [bits 0-7]
1015 * GTK
1016 */
1017
1018 os_memset(&gd, 0, sizeof(gd));
1019 wpa_hexdump_key(MSG_DEBUG, "RSN: received GTK in pairwise handshake",
1020 gtk, gtk_len);
1021
1022 if (gtk_len < 2 || gtk_len - 2 > sizeof(gd.gtk))
1023 return -1;
1024
1025 gd.keyidx = gtk[0] & 0x3;
1026 gd.tx = wpa_supplicant_gtk_tx_bit_workaround(sm,
1027 !!(gtk[0] & BIT(2)));
1028 gtk += 2;
1029 gtk_len -= 2;
1030
1031 os_memcpy(gd.gtk, gtk, gtk_len);
1032 gd.gtk_len = gtk_len;
1033
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001034 key_rsc = key->key_rsc;
1035 if (wpa_supplicant_rsc_relaxation(sm, key->key_rsc))
1036 key_rsc = null_rsc;
1037
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001038 if (sm->group_cipher != WPA_CIPHER_GTK_NOT_USED &&
1039 (wpa_supplicant_check_group_cipher(sm, sm->group_cipher,
1040 gtk_len, gtk_len,
1041 &gd.key_rsc_len, &gd.alg) ||
Jouni Malinen58c0e962017-10-01 12:12:24 +03001042 wpa_supplicant_install_gtk(sm, &gd, key_rsc, 0))) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001043 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1044 "RSN: Failed to install GTK");
Hai Shalom81f62d82019-07-22 12:10:00 -07001045 forced_memzero(&gd, sizeof(gd));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001046 return -1;
1047 }
Hai Shalom81f62d82019-07-22 12:10:00 -07001048 forced_memzero(&gd, sizeof(gd));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001049
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001050 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001051}
1052
1053
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02001054static int wpa_supplicant_install_igtk(struct wpa_sm *sm,
Jouni Malinen58c0e962017-10-01 12:12:24 +03001055 const struct wpa_igtk_kde *igtk,
1056 int wnm_sleep)
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02001057{
1058 size_t len = wpa_cipher_key_len(sm->mgmt_group_cipher);
1059 u16 keyidx = WPA_GET_LE16(igtk->keyid);
1060
1061 /* Detect possible key reinstallation */
Jouni Malinen58c0e962017-10-01 12:12:24 +03001062 if ((sm->igtk.igtk_len == len &&
1063 os_memcmp(sm->igtk.igtk, igtk->igtk, sm->igtk.igtk_len) == 0) ||
1064 (sm->igtk_wnm_sleep.igtk_len == len &&
1065 os_memcmp(sm->igtk_wnm_sleep.igtk, igtk->igtk,
1066 sm->igtk_wnm_sleep.igtk_len) == 0)) {
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02001067 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1068 "WPA: Not reinstalling already in-use IGTK to the driver (keyidx=%d)",
1069 keyidx);
1070 return 0;
1071 }
1072
1073 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
Hai Shalom39ba6fc2019-01-22 12:40:38 -08001074 "WPA: IGTK keyid %d pn " COMPACT_MACSTR,
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02001075 keyidx, MAC2STR(igtk->pn));
1076 wpa_hexdump_key(MSG_DEBUG, "WPA: IGTK", igtk->igtk, len);
1077 if (keyidx > 4095) {
1078 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1079 "WPA: Invalid IGTK KeyID %d", keyidx);
1080 return -1;
1081 }
1082 if (wpa_sm_set_key(sm, wpa_cipher_to_alg(sm->mgmt_group_cipher),
1083 broadcast_ether_addr,
1084 keyidx, 0, igtk->pn, sizeof(igtk->pn),
1085 igtk->igtk, len) < 0) {
Hai Shalom5f92bc92019-04-18 11:54:11 -07001086 if (keyidx == 0x0400 || keyidx == 0x0500) {
1087 /* Assume the AP has broken PMF implementation since it
1088 * seems to have swapped the KeyID bytes. The AP cannot
1089 * be trusted to implement BIP correctly or provide a
1090 * valid IGTK, so do not try to configure this key with
1091 * swapped KeyID bytes. Instead, continue without
1092 * configuring the IGTK so that the driver can drop any
1093 * received group-addressed robust management frames due
1094 * to missing keys.
1095 *
1096 * Normally, this error behavior would result in us
1097 * disconnecting, but there are number of deployed APs
1098 * with this broken behavior, so as an interoperability
1099 * workaround, allow the connection to proceed. */
1100 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1101 "WPA: Ignore IGTK configuration error due to invalid IGTK KeyID byte order");
1102 } else {
1103 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1104 "WPA: Failed to configure IGTK to the driver");
1105 return -1;
1106 }
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02001107 }
1108
Jouni Malinen58c0e962017-10-01 12:12:24 +03001109 if (wnm_sleep) {
1110 sm->igtk_wnm_sleep.igtk_len = len;
1111 os_memcpy(sm->igtk_wnm_sleep.igtk, igtk->igtk,
1112 sm->igtk_wnm_sleep.igtk_len);
1113 } else {
1114 sm->igtk.igtk_len = len;
1115 os_memcpy(sm->igtk.igtk, igtk->igtk, sm->igtk.igtk_len);
1116 }
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02001117
1118 return 0;
1119}
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02001120
1121
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001122static int ieee80211w_set_keys(struct wpa_sm *sm,
1123 struct wpa_eapol_ie_parse *ie)
1124{
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001125 if (!wpa_cipher_valid_mgmt_group(sm->mgmt_group_cipher))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001126 return 0;
1127
1128 if (ie->igtk) {
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001129 size_t len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001130 const struct wpa_igtk_kde *igtk;
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02001131
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001132 len = wpa_cipher_key_len(sm->mgmt_group_cipher);
1133 if (ie->igtk_len != WPA_IGTK_KDE_PREFIX_LEN + len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001134 return -1;
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02001135
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001136 igtk = (const struct wpa_igtk_kde *) ie->igtk;
Jouni Malinen58c0e962017-10-01 12:12:24 +03001137 if (wpa_supplicant_install_igtk(sm, igtk, 0) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001138 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001139 }
1140
1141 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001142}
1143
1144
1145static void wpa_report_ie_mismatch(struct wpa_sm *sm,
1146 const char *reason, const u8 *src_addr,
1147 const u8 *wpa_ie, size_t wpa_ie_len,
1148 const u8 *rsn_ie, size_t rsn_ie_len)
1149{
1150 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, "WPA: %s (src=" MACSTR ")",
1151 reason, MAC2STR(src_addr));
1152
1153 if (sm->ap_wpa_ie) {
1154 wpa_hexdump(MSG_INFO, "WPA: WPA IE in Beacon/ProbeResp",
1155 sm->ap_wpa_ie, sm->ap_wpa_ie_len);
1156 }
1157 if (wpa_ie) {
1158 if (!sm->ap_wpa_ie) {
1159 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1160 "WPA: No WPA IE in Beacon/ProbeResp");
1161 }
1162 wpa_hexdump(MSG_INFO, "WPA: WPA IE in 3/4 msg",
1163 wpa_ie, wpa_ie_len);
1164 }
1165
1166 if (sm->ap_rsn_ie) {
1167 wpa_hexdump(MSG_INFO, "WPA: RSN IE in Beacon/ProbeResp",
1168 sm->ap_rsn_ie, sm->ap_rsn_ie_len);
1169 }
1170 if (rsn_ie) {
1171 if (!sm->ap_rsn_ie) {
1172 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1173 "WPA: No RSN IE in Beacon/ProbeResp");
1174 }
1175 wpa_hexdump(MSG_INFO, "WPA: RSN IE in 3/4 msg",
1176 rsn_ie, rsn_ie_len);
1177 }
1178
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001179 wpa_sm_deauthenticate(sm, WLAN_REASON_IE_IN_4WAY_DIFFERS);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001180}
1181
1182
1183#ifdef CONFIG_IEEE80211R
1184
1185static int ft_validate_mdie(struct wpa_sm *sm,
1186 const unsigned char *src_addr,
1187 struct wpa_eapol_ie_parse *ie,
1188 const u8 *assoc_resp_mdie)
1189{
1190 struct rsn_mdie *mdie;
1191
1192 mdie = (struct rsn_mdie *) (ie->mdie + 2);
1193 if (ie->mdie == NULL || ie->mdie_len < 2 + sizeof(*mdie) ||
1194 os_memcmp(mdie->mobility_domain, sm->mobility_domain,
1195 MOBILITY_DOMAIN_ID_LEN) != 0) {
1196 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "FT: MDIE in msg 3/4 did "
1197 "not match with the current mobility domain");
1198 return -1;
1199 }
1200
1201 if (assoc_resp_mdie &&
1202 (assoc_resp_mdie[1] != ie->mdie[1] ||
1203 os_memcmp(assoc_resp_mdie, ie->mdie, 2 + ie->mdie[1]) != 0)) {
1204 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "FT: MDIE mismatch");
1205 wpa_hexdump(MSG_DEBUG, "FT: MDIE in EAPOL-Key msg 3/4",
1206 ie->mdie, 2 + ie->mdie[1]);
1207 wpa_hexdump(MSG_DEBUG, "FT: MDIE in (Re)Association Response",
1208 assoc_resp_mdie, 2 + assoc_resp_mdie[1]);
1209 return -1;
1210 }
1211
1212 return 0;
1213}
1214
1215
1216static int ft_validate_ftie(struct wpa_sm *sm,
1217 const unsigned char *src_addr,
1218 struct wpa_eapol_ie_parse *ie,
1219 const u8 *assoc_resp_ftie)
1220{
1221 if (ie->ftie == NULL) {
1222 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1223 "FT: No FTIE in EAPOL-Key msg 3/4");
1224 return -1;
1225 }
1226
1227 if (assoc_resp_ftie == NULL)
1228 return 0;
1229
1230 if (assoc_resp_ftie[1] != ie->ftie[1] ||
1231 os_memcmp(assoc_resp_ftie, ie->ftie, 2 + ie->ftie[1]) != 0) {
1232 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "FT: FTIE mismatch");
1233 wpa_hexdump(MSG_DEBUG, "FT: FTIE in EAPOL-Key msg 3/4",
1234 ie->ftie, 2 + ie->ftie[1]);
1235 wpa_hexdump(MSG_DEBUG, "FT: FTIE in (Re)Association Response",
1236 assoc_resp_ftie, 2 + assoc_resp_ftie[1]);
1237 return -1;
1238 }
1239
1240 return 0;
1241}
1242
1243
1244static int ft_validate_rsnie(struct wpa_sm *sm,
1245 const unsigned char *src_addr,
1246 struct wpa_eapol_ie_parse *ie)
1247{
1248 struct wpa_ie_data rsn;
1249
1250 if (!ie->rsn_ie)
1251 return 0;
1252
1253 /*
1254 * Verify that PMKR1Name from EAPOL-Key message 3/4
1255 * matches with the value we derived.
1256 */
1257 if (wpa_parse_wpa_ie_rsn(ie->rsn_ie, ie->rsn_ie_len, &rsn) < 0 ||
1258 rsn.num_pmkid != 1 || rsn.pmkid == NULL) {
1259 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "FT: No PMKR1Name in "
1260 "FT 4-way handshake message 3/4");
1261 return -1;
1262 }
1263
Dmitry Shmidtc2817022014-07-02 10:32:10 -07001264 if (os_memcmp_const(rsn.pmkid, sm->pmk_r1_name, WPA_PMK_NAME_LEN) != 0)
1265 {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001266 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1267 "FT: PMKR1Name mismatch in "
1268 "FT 4-way handshake message 3/4");
1269 wpa_hexdump(MSG_DEBUG, "FT: PMKR1Name from Authenticator",
1270 rsn.pmkid, WPA_PMK_NAME_LEN);
1271 wpa_hexdump(MSG_DEBUG, "FT: Derived PMKR1Name",
1272 sm->pmk_r1_name, WPA_PMK_NAME_LEN);
1273 return -1;
1274 }
1275
1276 return 0;
1277}
1278
1279
1280static int wpa_supplicant_validate_ie_ft(struct wpa_sm *sm,
1281 const unsigned char *src_addr,
1282 struct wpa_eapol_ie_parse *ie)
1283{
1284 const u8 *pos, *end, *mdie = NULL, *ftie = NULL;
1285
1286 if (sm->assoc_resp_ies) {
1287 pos = sm->assoc_resp_ies;
1288 end = pos + sm->assoc_resp_ies_len;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001289 while (end - pos > 2) {
1290 if (2 + pos[1] > end - pos)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001291 break;
1292 switch (*pos) {
1293 case WLAN_EID_MOBILITY_DOMAIN:
1294 mdie = pos;
1295 break;
1296 case WLAN_EID_FAST_BSS_TRANSITION:
1297 ftie = pos;
1298 break;
1299 }
1300 pos += 2 + pos[1];
1301 }
1302 }
1303
1304 if (ft_validate_mdie(sm, src_addr, ie, mdie) < 0 ||
1305 ft_validate_ftie(sm, src_addr, ie, ftie) < 0 ||
1306 ft_validate_rsnie(sm, src_addr, ie) < 0)
1307 return -1;
1308
1309 return 0;
1310}
1311
1312#endif /* CONFIG_IEEE80211R */
1313
1314
1315static int wpa_supplicant_validate_ie(struct wpa_sm *sm,
1316 const unsigned char *src_addr,
1317 struct wpa_eapol_ie_parse *ie)
1318{
1319 if (sm->ap_wpa_ie == NULL && sm->ap_rsn_ie == NULL) {
1320 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1321 "WPA: No WPA/RSN IE for this AP known. "
1322 "Trying to get from scan results");
1323 if (wpa_sm_get_beacon_ie(sm) < 0) {
1324 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1325 "WPA: Could not find AP from "
1326 "the scan results");
1327 } else {
1328 wpa_msg(sm->ctx->msg_ctx, MSG_DEBUG,
1329 "WPA: Found the current AP from "
1330 "updated scan results");
1331 }
1332 }
1333
1334 if (ie->wpa_ie == NULL && ie->rsn_ie == NULL &&
1335 (sm->ap_wpa_ie || sm->ap_rsn_ie)) {
1336 wpa_report_ie_mismatch(sm, "IE in 3/4 msg does not match "
1337 "with IE in Beacon/ProbeResp (no IE?)",
1338 src_addr, ie->wpa_ie, ie->wpa_ie_len,
1339 ie->rsn_ie, ie->rsn_ie_len);
1340 return -1;
1341 }
1342
1343 if ((ie->wpa_ie && sm->ap_wpa_ie &&
1344 (ie->wpa_ie_len != sm->ap_wpa_ie_len ||
1345 os_memcmp(ie->wpa_ie, sm->ap_wpa_ie, ie->wpa_ie_len) != 0)) ||
1346 (ie->rsn_ie && sm->ap_rsn_ie &&
1347 wpa_compare_rsn_ie(wpa_key_mgmt_ft(sm->key_mgmt),
1348 sm->ap_rsn_ie, sm->ap_rsn_ie_len,
1349 ie->rsn_ie, ie->rsn_ie_len))) {
1350 wpa_report_ie_mismatch(sm, "IE in 3/4 msg does not match "
1351 "with IE in Beacon/ProbeResp",
1352 src_addr, ie->wpa_ie, ie->wpa_ie_len,
1353 ie->rsn_ie, ie->rsn_ie_len);
1354 return -1;
1355 }
1356
1357 if (sm->proto == WPA_PROTO_WPA &&
1358 ie->rsn_ie && sm->ap_rsn_ie == NULL && sm->rsn_enabled) {
1359 wpa_report_ie_mismatch(sm, "Possible downgrade attack "
1360 "detected - RSN was enabled and RSN IE "
1361 "was in msg 3/4, but not in "
1362 "Beacon/ProbeResp",
1363 src_addr, ie->wpa_ie, ie->wpa_ie_len,
1364 ie->rsn_ie, ie->rsn_ie_len);
1365 return -1;
1366 }
1367
Hai Shalomc3565922019-10-28 11:58:20 -07001368 if ((sm->ap_rsnxe && !ie->rsnxe) ||
1369 (!sm->ap_rsnxe && ie->rsnxe) ||
1370 (sm->ap_rsnxe && ie->rsnxe &&
1371 (sm->ap_rsnxe_len != ie->rsnxe_len ||
1372 os_memcmp(sm->ap_rsnxe, ie->rsnxe, sm->ap_rsnxe_len) != 0))) {
1373 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1374 "WPA: RSNXE mismatch between Beacon/ProbeResp and EAPOL-Key msg 3/4");
1375 return -1;
1376 }
1377
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001378#ifdef CONFIG_IEEE80211R
1379 if (wpa_key_mgmt_ft(sm->key_mgmt) &&
1380 wpa_supplicant_validate_ie_ft(sm, src_addr, ie) < 0)
1381 return -1;
1382#endif /* CONFIG_IEEE80211R */
1383
1384 return 0;
1385}
1386
1387
1388/**
1389 * wpa_supplicant_send_4_of_4 - Send message 4 of WPA/RSN 4-Way Handshake
1390 * @sm: Pointer to WPA state machine data from wpa_sm_init()
1391 * @dst: Destination address for the frame
1392 * @key: Pointer to the EAPOL-Key frame header
1393 * @ver: Version bits from EAPOL-Key Key Info
1394 * @key_info: Key Info
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001395 * @ptk: PTK to use for keyed hash and encryption
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001396 * Returns: >= 0 on success, < 0 on failure
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001397 */
1398int wpa_supplicant_send_4_of_4(struct wpa_sm *sm, const unsigned char *dst,
1399 const struct wpa_eapol_key *key,
1400 u16 ver, u16 key_info,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001401 struct wpa_ptk *ptk)
1402{
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001403 size_t mic_len, hdrlen, rlen;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001404 struct wpa_eapol_key *reply;
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001405 u8 *rbuf, *key_mic;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001406
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001407 mic_len = wpa_mic_len(sm->key_mgmt, sm->pmk_len);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001408 hdrlen = sizeof(*reply) + mic_len + 2;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001409 rbuf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_KEY, NULL,
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001410 hdrlen, &rlen, (void *) &reply);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001411 if (rbuf == NULL)
1412 return -1;
1413
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001414 reply->type = (sm->proto == WPA_PROTO_RSN ||
1415 sm->proto == WPA_PROTO_OSEN) ?
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001416 EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA;
1417 key_info &= WPA_KEY_INFO_SECURE;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001418 key_info |= ver | WPA_KEY_INFO_KEY_TYPE;
1419 if (mic_len)
1420 key_info |= WPA_KEY_INFO_MIC;
1421 else
1422 key_info |= WPA_KEY_INFO_ENCR_KEY_DATA;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001423 WPA_PUT_BE16(reply->key_info, key_info);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001424 if (sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001425 WPA_PUT_BE16(reply->key_length, 0);
1426 else
1427 os_memcpy(reply->key_length, key->key_length, 2);
1428 os_memcpy(reply->replay_counter, key->replay_counter,
1429 WPA_REPLAY_COUNTER_LEN);
1430
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001431 key_mic = (u8 *) (reply + 1);
1432 WPA_PUT_BE16(key_mic + mic_len, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001433
Roshan Pius5e7db942018-04-12 12:27:41 -07001434 wpa_dbg(sm->ctx->msg_ctx, MSG_INFO, "WPA: Sending EAPOL-Key 4/4");
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001435 return wpa_eapol_key_send(sm, ptk, ver, dst, ETH_P_EAPOL, rbuf, rlen,
1436 key_mic);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001437}
1438
1439
1440static void wpa_supplicant_process_3_of_4(struct wpa_sm *sm,
1441 const struct wpa_eapol_key *key,
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001442 u16 ver, const u8 *key_data,
1443 size_t key_data_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001444{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001445 u16 key_info, keylen;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001446 struct wpa_eapol_ie_parse ie;
1447
1448 wpa_sm_set_state(sm, WPA_4WAY_HANDSHAKE);
Roshan Pius5e7db942018-04-12 12:27:41 -07001449 wpa_dbg(sm->ctx->msg_ctx, MSG_INFO, "WPA: RX message 3 of 4-Way "
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001450 "Handshake from " MACSTR " (ver=%d)", MAC2STR(sm->bssid), ver);
1451
1452 key_info = WPA_GET_BE16(key->key_info);
1453
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001454 wpa_hexdump(MSG_DEBUG, "WPA: IE KeyData", key_data, key_data_len);
1455 if (wpa_supplicant_parse_ies(key_data, key_data_len, &ie) < 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001456 goto failed;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001457 if (ie.gtk && !(key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
1458 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1459 "WPA: GTK IE in unencrypted key data");
1460 goto failed;
1461 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001462 if (ie.igtk && !(key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
1463 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1464 "WPA: IGTK KDE in unencrypted key data");
1465 goto failed;
1466 }
1467
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001468 if (ie.igtk &&
1469 wpa_cipher_valid_mgmt_group(sm->mgmt_group_cipher) &&
1470 ie.igtk_len != WPA_IGTK_KDE_PREFIX_LEN +
1471 (unsigned int) wpa_cipher_key_len(sm->mgmt_group_cipher)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001472 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1473 "WPA: Invalid IGTK KDE length %lu",
1474 (unsigned long) ie.igtk_len);
1475 goto failed;
1476 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001477
1478 if (wpa_supplicant_validate_ie(sm, sm->bssid, &ie) < 0)
1479 goto failed;
1480
1481 if (os_memcmp(sm->anonce, key->key_nonce, WPA_NONCE_LEN) != 0) {
1482 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1483 "WPA: ANonce from message 1 of 4-Way Handshake "
1484 "differs from 3 of 4-Way Handshake - drop packet (src="
1485 MACSTR ")", MAC2STR(sm->bssid));
1486 goto failed;
1487 }
1488
1489 keylen = WPA_GET_BE16(key->key_length);
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07001490 if (keylen != wpa_cipher_key_len(sm->pairwise_cipher)) {
1491 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1492 "WPA: Invalid %s key length %d (src=" MACSTR
1493 ")", wpa_cipher_txt(sm->pairwise_cipher), keylen,
1494 MAC2STR(sm->bssid));
1495 goto failed;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001496 }
1497
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001498#ifdef CONFIG_P2P
1499 if (ie.ip_addr_alloc) {
1500 os_memcpy(sm->p2p_ip_addr, ie.ip_addr_alloc, 3 * 4);
1501 wpa_hexdump(MSG_DEBUG, "P2P: IP address info",
1502 sm->p2p_ip_addr, sizeof(sm->p2p_ip_addr));
1503 }
1504#endif /* CONFIG_P2P */
1505
Hai Shalom74f70d42019-02-11 14:42:39 -08001506#ifdef CONFIG_OCV
1507 if (wpa_sm_ocv_enabled(sm)) {
1508 struct wpa_channel_info ci;
1509
1510 if (wpa_sm_channel_info(sm, &ci) != 0) {
1511 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1512 "Failed to get channel info to validate received OCI in EAPOL-Key 3/4");
1513 return;
1514 }
1515
1516 if (ocv_verify_tx_params(ie.oci, ie.oci_len, &ci,
1517 channel_width_to_int(ci.chanwidth),
1518 ci.seg1_idx) != 0) {
1519 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, "%s",
1520 ocv_errorstr);
1521 return;
1522 }
1523 }
1524#endif /* CONFIG_OCV */
1525
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001526 if (wpa_supplicant_send_4_of_4(sm, sm->bssid, key, ver, key_info,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001527 &sm->ptk) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001528 goto failed;
1529 }
1530
1531 /* SNonce was successfully used in msg 3/4, so mark it to be renewed
1532 * for the next 4-Way Handshake. If msg 3 is received again, the old
1533 * SNonce will still be used to avoid changing PTK. */
1534 sm->renew_snonce = 1;
1535
1536 if (key_info & WPA_KEY_INFO_INSTALL) {
1537 if (wpa_supplicant_install_ptk(sm, key))
1538 goto failed;
1539 }
1540
1541 if (key_info & WPA_KEY_INFO_SECURE) {
1542 wpa_sm_mlme_setprotection(
1543 sm, sm->bssid, MLME_SETPROTECTION_PROTECT_TYPE_RX,
1544 MLME_SETPROTECTION_KEY_TYPE_PAIRWISE);
1545 eapol_sm_notify_portValid(sm->eapol, TRUE);
1546 }
1547 wpa_sm_set_state(sm, WPA_GROUP_HANDSHAKE);
1548
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001549 if (sm->group_cipher == WPA_CIPHER_GTK_NOT_USED) {
Hai Shalom5f92bc92019-04-18 11:54:11 -07001550 /* No GTK to be set to the driver */
1551 } else if (!ie.gtk && sm->proto == WPA_PROTO_RSN) {
1552 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1553 "RSN: No GTK KDE included in EAPOL-Key msg 3/4");
1554 goto failed;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001555 } else if (ie.gtk &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001556 wpa_supplicant_pairwise_gtk(sm, key,
1557 ie.gtk, ie.gtk_len, key_info) < 0) {
1558 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1559 "RSN: Failed to configure GTK");
1560 goto failed;
1561 }
1562
1563 if (ieee80211w_set_keys(sm, &ie) < 0) {
1564 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1565 "RSN: Failed to configure IGTK");
1566 goto failed;
1567 }
1568
Hai Shalom5f92bc92019-04-18 11:54:11 -07001569 if (sm->group_cipher == WPA_CIPHER_GTK_NOT_USED || ie.gtk)
1570 wpa_supplicant_key_neg_complete(sm, sm->bssid,
1571 key_info & WPA_KEY_INFO_SECURE);
1572
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001573 if (ie.gtk)
1574 wpa_sm_set_rekey_offload(sm);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001575
Hai Shalom39ba6fc2019-01-22 12:40:38 -08001576 /* Add PMKSA cache entry for Suite B AKMs here since PMKID can be
1577 * calculated only after KCK has been derived. Though, do not replace an
1578 * existing PMKSA entry after each 4-way handshake (i.e., new KCK/PMKID)
1579 * to avoid unnecessary changes of PMKID while continuing to use the
1580 * same PMK. */
1581 if (sm->proto == WPA_PROTO_RSN && wpa_key_mgmt_suite_b(sm->key_mgmt) &&
1582 !sm->cur_pmksa) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001583 struct rsn_pmksa_cache_entry *sa;
1584
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001585 sa = pmksa_cache_add(sm->pmksa, sm->pmk, sm->pmk_len, NULL,
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001586 sm->ptk.kck, sm->ptk.kck_len,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001587 sm->bssid, sm->own_addr,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001588 sm->network_ctx, sm->key_mgmt, NULL);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001589 if (!sm->cur_pmksa)
1590 sm->cur_pmksa = sa;
1591 }
1592
Dmitry Shmidt4dd28dc2015-03-10 11:21:43 -07001593 sm->msg_3_of_4_ok = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001594 return;
1595
1596failed:
1597 wpa_sm_deauthenticate(sm, WLAN_REASON_UNSPECIFIED);
1598}
1599
1600
1601static int wpa_supplicant_process_1_of_2_rsn(struct wpa_sm *sm,
1602 const u8 *keydata,
1603 size_t keydatalen,
1604 u16 key_info,
1605 struct wpa_gtk_data *gd)
1606{
1607 int maxkeylen;
1608 struct wpa_eapol_ie_parse ie;
1609
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08001610 wpa_hexdump_key(MSG_DEBUG, "RSN: msg 1/2 key data",
1611 keydata, keydatalen);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001612 if (wpa_supplicant_parse_ies(keydata, keydatalen, &ie) < 0)
1613 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001614 if (ie.gtk && !(key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
1615 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1616 "WPA: GTK IE in unencrypted key data");
1617 return -1;
1618 }
1619 if (ie.gtk == NULL) {
1620 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1621 "WPA: No GTK IE in Group Key msg 1/2");
1622 return -1;
1623 }
1624 maxkeylen = gd->gtk_len = ie.gtk_len - 2;
1625
Hai Shalom74f70d42019-02-11 14:42:39 -08001626#ifdef CONFIG_OCV
1627 if (wpa_sm_ocv_enabled(sm)) {
1628 struct wpa_channel_info ci;
1629
1630 if (wpa_sm_channel_info(sm, &ci) != 0) {
1631 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1632 "Failed to get channel info to validate received OCI in EAPOL-Key group msg 1/2");
1633 return -1;
1634 }
1635
1636 if (ocv_verify_tx_params(ie.oci, ie.oci_len, &ci,
1637 channel_width_to_int(ci.chanwidth),
1638 ci.seg1_idx) != 0) {
1639 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, "%s",
1640 ocv_errorstr);
1641 return -1;
1642 }
1643 }
1644#endif /* CONFIG_OCV */
1645
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001646 if (wpa_supplicant_check_group_cipher(sm, sm->group_cipher,
1647 gd->gtk_len, maxkeylen,
1648 &gd->key_rsc_len, &gd->alg))
1649 return -1;
1650
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001651 wpa_hexdump_key(MSG_DEBUG, "RSN: received GTK in group key handshake",
1652 ie.gtk, ie.gtk_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001653 gd->keyidx = ie.gtk[0] & 0x3;
1654 gd->tx = wpa_supplicant_gtk_tx_bit_workaround(sm,
1655 !!(ie.gtk[0] & BIT(2)));
1656 if (ie.gtk_len - 2 > sizeof(gd->gtk)) {
1657 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1658 "RSN: Too long GTK in GTK IE (len=%lu)",
1659 (unsigned long) ie.gtk_len - 2);
1660 return -1;
1661 }
1662 os_memcpy(gd->gtk, ie.gtk + 2, ie.gtk_len - 2);
1663
1664 if (ieee80211w_set_keys(sm, &ie) < 0)
1665 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1666 "RSN: Failed to configure IGTK");
1667
1668 return 0;
1669}
1670
1671
1672static int wpa_supplicant_process_1_of_2_wpa(struct wpa_sm *sm,
1673 const struct wpa_eapol_key *key,
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001674 const u8 *key_data,
1675 size_t key_data_len, u16 key_info,
1676 u16 ver, struct wpa_gtk_data *gd)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001677{
1678 size_t maxkeylen;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001679 u16 gtk_len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001680
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001681 gtk_len = WPA_GET_BE16(key->key_length);
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001682 maxkeylen = key_data_len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001683 if (ver == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
1684 if (maxkeylen < 8) {
1685 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1686 "WPA: Too short maxkeylen (%lu)",
1687 (unsigned long) maxkeylen);
1688 return -1;
1689 }
1690 maxkeylen -= 8;
1691 }
1692
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001693 if (gtk_len > maxkeylen ||
1694 wpa_supplicant_check_group_cipher(sm, sm->group_cipher,
1695 gtk_len, maxkeylen,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001696 &gd->key_rsc_len, &gd->alg))
1697 return -1;
1698
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001699 gd->gtk_len = gtk_len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001700 gd->keyidx = (key_info & WPA_KEY_INFO_KEY_INDEX_MASK) >>
1701 WPA_KEY_INFO_KEY_INDEX_SHIFT;
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001702 if (ver == WPA_KEY_INFO_TYPE_HMAC_MD5_RC4 && sm->ptk.kek_len == 16) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001703#ifdef CONFIG_NO_RC4
1704 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1705 "WPA: RC4 not supported in the build");
1706 return -1;
1707#else /* CONFIG_NO_RC4 */
Dmitry Shmidt61593f02014-04-21 16:27:35 -07001708 u8 ek[32];
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001709 if (key_data_len > sizeof(gd->gtk)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001710 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1711 "WPA: RC4 key data too long (%lu)",
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001712 (unsigned long) key_data_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001713 return -1;
1714 }
Dmitry Shmidt61593f02014-04-21 16:27:35 -07001715 os_memcpy(ek, key->key_iv, 16);
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001716 os_memcpy(ek + 16, sm->ptk.kek, sm->ptk.kek_len);
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001717 os_memcpy(gd->gtk, key_data, key_data_len);
1718 if (rc4_skip(ek, 32, 256, gd->gtk, key_data_len)) {
Hai Shalom81f62d82019-07-22 12:10:00 -07001719 forced_memzero(ek, sizeof(ek));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001720 wpa_msg(sm->ctx->msg_ctx, MSG_ERROR,
1721 "WPA: RC4 failed");
1722 return -1;
1723 }
Hai Shalom81f62d82019-07-22 12:10:00 -07001724 forced_memzero(ek, sizeof(ek));
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001725#endif /* CONFIG_NO_RC4 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001726 } else if (ver == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001727 if (maxkeylen % 8) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001728 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1729 "WPA: Unsupported AES-WRAP len %lu",
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001730 (unsigned long) maxkeylen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001731 return -1;
1732 }
1733 if (maxkeylen > sizeof(gd->gtk)) {
1734 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1735 "WPA: AES-WRAP key data "
1736 "too long (keydatalen=%lu maxkeylen=%lu)",
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001737 (unsigned long) key_data_len,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001738 (unsigned long) maxkeylen);
1739 return -1;
1740 }
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001741 if (aes_unwrap(sm->ptk.kek, sm->ptk.kek_len, maxkeylen / 8,
1742 key_data, gd->gtk)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001743 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1744 "WPA: AES unwrap failed - could not decrypt "
1745 "GTK");
1746 return -1;
1747 }
1748 } else {
1749 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1750 "WPA: Unsupported key_info type %d", ver);
1751 return -1;
1752 }
1753 gd->tx = wpa_supplicant_gtk_tx_bit_workaround(
1754 sm, !!(key_info & WPA_KEY_INFO_TXRX));
1755 return 0;
1756}
1757
1758
1759static int wpa_supplicant_send_2_of_2(struct wpa_sm *sm,
1760 const struct wpa_eapol_key *key,
1761 int ver, u16 key_info)
1762{
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001763 size_t mic_len, hdrlen, rlen;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001764 struct wpa_eapol_key *reply;
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001765 u8 *rbuf, *key_mic;
Hai Shalom74f70d42019-02-11 14:42:39 -08001766 size_t kde_len = 0;
1767
1768#ifdef CONFIG_OCV
1769 if (wpa_sm_ocv_enabled(sm))
1770 kde_len = OCV_OCI_KDE_LEN;
1771#endif /* CONFIG_OCV */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001772
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001773 mic_len = wpa_mic_len(sm->key_mgmt, sm->pmk_len);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001774 hdrlen = sizeof(*reply) + mic_len + 2;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001775 rbuf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_KEY, NULL,
Hai Shalom74f70d42019-02-11 14:42:39 -08001776 hdrlen + kde_len, &rlen, (void *) &reply);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001777 if (rbuf == NULL)
1778 return -1;
1779
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001780 reply->type = (sm->proto == WPA_PROTO_RSN ||
1781 sm->proto == WPA_PROTO_OSEN) ?
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001782 EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA;
1783 key_info &= WPA_KEY_INFO_KEY_INDEX_MASK;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001784 key_info |= ver | WPA_KEY_INFO_SECURE;
1785 if (mic_len)
1786 key_info |= WPA_KEY_INFO_MIC;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001787 else
1788 key_info |= WPA_KEY_INFO_ENCR_KEY_DATA;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001789 WPA_PUT_BE16(reply->key_info, key_info);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001790 if (sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001791 WPA_PUT_BE16(reply->key_length, 0);
1792 else
1793 os_memcpy(reply->key_length, key->key_length, 2);
1794 os_memcpy(reply->replay_counter, key->replay_counter,
1795 WPA_REPLAY_COUNTER_LEN);
1796
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001797 key_mic = (u8 *) (reply + 1);
Hai Shalom74f70d42019-02-11 14:42:39 -08001798 WPA_PUT_BE16(key_mic + mic_len, kde_len); /* Key Data Length */
1799
1800#ifdef CONFIG_OCV
1801 if (wpa_sm_ocv_enabled(sm)) {
1802 struct wpa_channel_info ci;
1803 u8 *pos;
1804
1805 if (wpa_sm_channel_info(sm, &ci) != 0) {
1806 wpa_printf(MSG_WARNING,
1807 "Failed to get channel info for OCI element in EAPOL-Key 2/2");
1808 os_free(rbuf);
1809 return -1;
1810 }
1811
1812 pos = key_mic + mic_len + 2; /* Key Data */
1813 if (ocv_insert_oci_kde(&ci, &pos) < 0) {
1814 os_free(rbuf);
1815 return -1;
1816 }
1817 }
1818#endif /* CONFIG_OCV */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001819
1820 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Sending EAPOL-Key 2/2");
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001821 return wpa_eapol_key_send(sm, &sm->ptk, ver, sm->bssid, ETH_P_EAPOL,
1822 rbuf, rlen, key_mic);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001823}
1824
1825
1826static void wpa_supplicant_process_1_of_2(struct wpa_sm *sm,
1827 const unsigned char *src_addr,
1828 const struct wpa_eapol_key *key,
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001829 const u8 *key_data,
1830 size_t key_data_len, u16 ver)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001831{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001832 u16 key_info;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001833 int rekey, ret;
1834 struct wpa_gtk_data gd;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001835 const u8 *key_rsc;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001836
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001837 if (!sm->msg_3_of_4_ok && !wpa_fils_is_completed(sm)) {
Dmitry Shmidt4dd28dc2015-03-10 11:21:43 -07001838 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1839 "WPA: Group Key Handshake started prior to completion of 4-way handshake");
1840 goto failed;
1841 }
1842
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001843 os_memset(&gd, 0, sizeof(gd));
1844
1845 rekey = wpa_sm_get_state(sm) == WPA_COMPLETED;
1846 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: RX message 1 of Group Key "
1847 "Handshake from " MACSTR " (ver=%d)", MAC2STR(src_addr), ver);
1848
1849 key_info = WPA_GET_BE16(key->key_info);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001850
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001851 if (sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN) {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001852 ret = wpa_supplicant_process_1_of_2_rsn(sm, key_data,
1853 key_data_len, key_info,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001854 &gd);
1855 } else {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001856 ret = wpa_supplicant_process_1_of_2_wpa(sm, key, key_data,
1857 key_data_len,
1858 key_info, ver, &gd);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001859 }
1860
1861 wpa_sm_set_state(sm, WPA_GROUP_HANDSHAKE);
1862
1863 if (ret)
1864 goto failed;
1865
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001866 key_rsc = key->key_rsc;
1867 if (wpa_supplicant_rsc_relaxation(sm, key->key_rsc))
1868 key_rsc = null_rsc;
1869
Jouni Malinen58c0e962017-10-01 12:12:24 +03001870 if (wpa_supplicant_install_gtk(sm, &gd, key_rsc, 0) ||
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001871 wpa_supplicant_send_2_of_2(sm, key, ver, key_info) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001872 goto failed;
Hai Shalom81f62d82019-07-22 12:10:00 -07001873 forced_memzero(&gd, sizeof(gd));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001874
1875 if (rekey) {
1876 wpa_msg(sm->ctx->msg_ctx, MSG_INFO, "WPA: Group rekeying "
1877 "completed with " MACSTR " [GTK=%s]",
1878 MAC2STR(sm->bssid), wpa_cipher_txt(sm->group_cipher));
1879 wpa_sm_cancel_auth_timeout(sm);
1880 wpa_sm_set_state(sm, WPA_COMPLETED);
1881 } else {
1882 wpa_supplicant_key_neg_complete(sm, sm->bssid,
1883 key_info &
1884 WPA_KEY_INFO_SECURE);
1885 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001886
1887 wpa_sm_set_rekey_offload(sm);
1888
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001889 return;
1890
1891failed:
Hai Shalom81f62d82019-07-22 12:10:00 -07001892 forced_memzero(&gd, sizeof(gd));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001893 wpa_sm_deauthenticate(sm, WLAN_REASON_UNSPECIFIED);
1894}
1895
1896
1897static int wpa_supplicant_verify_eapol_key_mic(struct wpa_sm *sm,
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001898 struct wpa_eapol_key *key,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001899 u16 ver,
1900 const u8 *buf, size_t len)
1901{
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001902 u8 mic[WPA_EAPOL_KEY_MIC_MAX_LEN];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001903 int ok = 0;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001904 size_t mic_len = wpa_mic_len(sm->key_mgmt, sm->pmk_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001905
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001906 os_memcpy(mic, key + 1, mic_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001907 if (sm->tptk_set) {
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001908 os_memset(key + 1, 0, mic_len);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001909 if (wpa_eapol_key_mic(sm->tptk.kck, sm->tptk.kck_len,
1910 sm->key_mgmt,
1911 ver, buf, len, (u8 *) (key + 1)) < 0 ||
1912 os_memcmp_const(mic, key + 1, mic_len) != 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001913 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1914 "WPA: Invalid EAPOL-Key MIC "
1915 "when using TPTK - ignoring TPTK");
Hai Shalom74f70d42019-02-11 14:42:39 -08001916#ifdef TEST_FUZZ
1917 wpa_printf(MSG_INFO,
1918 "TEST: Ignore Key MIC failure for fuzz testing");
1919 goto continue_fuzz;
1920#endif /* TEST_FUZZ */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001921 } else {
Hai Shalom74f70d42019-02-11 14:42:39 -08001922#ifdef TEST_FUZZ
1923 continue_fuzz:
1924#endif /* TEST_FUZZ */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001925 ok = 1;
1926 sm->tptk_set = 0;
1927 sm->ptk_set = 1;
1928 os_memcpy(&sm->ptk, &sm->tptk, sizeof(sm->ptk));
Dmitry Shmidt61593f02014-04-21 16:27:35 -07001929 os_memset(&sm->tptk, 0, sizeof(sm->tptk));
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001930 /*
1931 * This assures the same TPTK in sm->tptk can never be
Roshan Pius3a1667e2018-07-03 15:17:14 -07001932 * copied twice to sm->ptk as the new PTK. In
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001933 * combination with the installed flag in the wpa_ptk
1934 * struct, this assures the same PTK is only installed
1935 * once.
1936 */
1937 sm->renew_snonce = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001938 }
1939 }
1940
1941 if (!ok && sm->ptk_set) {
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001942 os_memset(key + 1, 0, mic_len);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001943 if (wpa_eapol_key_mic(sm->ptk.kck, sm->ptk.kck_len,
1944 sm->key_mgmt,
1945 ver, buf, len, (u8 *) (key + 1)) < 0 ||
1946 os_memcmp_const(mic, key + 1, mic_len) != 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001947 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1948 "WPA: Invalid EAPOL-Key MIC - "
1949 "dropping packet");
Hai Shalom74f70d42019-02-11 14:42:39 -08001950#ifdef TEST_FUZZ
1951 wpa_printf(MSG_INFO,
1952 "TEST: Ignore Key MIC failure for fuzz testing");
1953 goto continue_fuzz2;
1954#endif /* TEST_FUZZ */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001955 return -1;
1956 }
Hai Shalom74f70d42019-02-11 14:42:39 -08001957#ifdef TEST_FUZZ
1958 continue_fuzz2:
1959#endif /* TEST_FUZZ */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001960 ok = 1;
1961 }
1962
1963 if (!ok) {
1964 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1965 "WPA: Could not verify EAPOL-Key MIC - "
1966 "dropping packet");
1967 return -1;
1968 }
1969
1970 os_memcpy(sm->rx_replay_counter, key->replay_counter,
1971 WPA_REPLAY_COUNTER_LEN);
1972 sm->rx_replay_counter_set = 1;
1973 return 0;
1974}
1975
1976
1977/* Decrypt RSN EAPOL-Key key data (RC4 or AES-WRAP) */
1978static int wpa_supplicant_decrypt_key_data(struct wpa_sm *sm,
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001979 struct wpa_eapol_key *key,
1980 size_t mic_len, u16 ver,
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001981 u8 *key_data, size_t *key_data_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001982{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001983 wpa_hexdump(MSG_DEBUG, "RSN: encrypted key data",
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001984 key_data, *key_data_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001985 if (!sm->ptk_set) {
1986 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1987 "WPA: PTK not available, cannot decrypt EAPOL-Key Key "
1988 "Data");
1989 return -1;
1990 }
1991
1992 /* Decrypt key data here so that this operation does not need
1993 * to be implemented separately for each message type. */
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001994 if (ver == WPA_KEY_INFO_TYPE_HMAC_MD5_RC4 && sm->ptk.kek_len == 16) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001995#ifdef CONFIG_NO_RC4
1996 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1997 "WPA: RC4 not supported in the build");
1998 return -1;
1999#else /* CONFIG_NO_RC4 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002000 u8 ek[32];
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002001
2002 wpa_printf(MSG_DEBUG, "WPA: Decrypt Key Data using RC4");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002003 os_memcpy(ek, key->key_iv, 16);
Dmitry Shmidt807291d2015-01-27 13:40:23 -08002004 os_memcpy(ek + 16, sm->ptk.kek, sm->ptk.kek_len);
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002005 if (rc4_skip(ek, 32, 256, key_data, *key_data_len)) {
Hai Shalom81f62d82019-07-22 12:10:00 -07002006 forced_memzero(ek, sizeof(ek));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002007 wpa_msg(sm->ctx->msg_ctx, MSG_ERROR,
2008 "WPA: RC4 failed");
2009 return -1;
2010 }
Hai Shalom81f62d82019-07-22 12:10:00 -07002011 forced_memzero(ek, sizeof(ek));
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002012#endif /* CONFIG_NO_RC4 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002013 } else if (ver == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES ||
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002014 ver == WPA_KEY_INFO_TYPE_AES_128_CMAC ||
Roshan Pius3a1667e2018-07-03 15:17:14 -07002015 wpa_use_aes_key_wrap(sm->key_mgmt)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002016 u8 *buf;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002017
2018 wpa_printf(MSG_DEBUG,
2019 "WPA: Decrypt Key Data using AES-UNWRAP (KEK length %u)",
2020 (unsigned int) sm->ptk.kek_len);
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002021 if (*key_data_len < 8 || *key_data_len % 8) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002022 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002023 "WPA: Unsupported AES-WRAP len %u",
2024 (unsigned int) *key_data_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002025 return -1;
2026 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002027 *key_data_len -= 8; /* AES-WRAP adds 8 bytes */
2028 buf = os_malloc(*key_data_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002029 if (buf == NULL) {
2030 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
2031 "WPA: No memory for AES-UNWRAP buffer");
2032 return -1;
2033 }
Hai Shalom74f70d42019-02-11 14:42:39 -08002034#ifdef TEST_FUZZ
2035 os_memset(buf, 0x11, *key_data_len);
2036#endif /* TEST_FUZZ */
Dmitry Shmidt807291d2015-01-27 13:40:23 -08002037 if (aes_unwrap(sm->ptk.kek, sm->ptk.kek_len, *key_data_len / 8,
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002038 key_data, buf)) {
Hai Shalom74f70d42019-02-11 14:42:39 -08002039#ifdef TEST_FUZZ
2040 wpa_printf(MSG_INFO,
2041 "TEST: Ignore AES unwrap failure for fuzz testing");
2042 goto continue_fuzz;
2043#endif /* TEST_FUZZ */
Dmitry Shmidt7d56b752015-12-22 10:59:44 -08002044 bin_clear_free(buf, *key_data_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002045 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
2046 "WPA: AES unwrap failed - "
2047 "could not decrypt EAPOL-Key key data");
2048 return -1;
2049 }
Hai Shalom74f70d42019-02-11 14:42:39 -08002050#ifdef TEST_FUZZ
2051 continue_fuzz:
2052#endif /* TEST_FUZZ */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002053 os_memcpy(key_data, buf, *key_data_len);
Dmitry Shmidt7d56b752015-12-22 10:59:44 -08002054 bin_clear_free(buf, *key_data_len);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002055 WPA_PUT_BE16(((u8 *) (key + 1)) + mic_len, *key_data_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002056 } else {
2057 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
2058 "WPA: Unsupported key_info type %d", ver);
2059 return -1;
2060 }
2061 wpa_hexdump_key(MSG_DEBUG, "WPA: decrypted EAPOL-Key key data",
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002062 key_data, *key_data_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002063 return 0;
2064}
2065
2066
2067/**
2068 * wpa_sm_aborted_cached - Notify WPA that PMKSA caching was aborted
2069 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2070 */
2071void wpa_sm_aborted_cached(struct wpa_sm *sm)
2072{
2073 if (sm && sm->cur_pmksa) {
2074 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2075 "RSN: Cancelling PMKSA caching attempt");
2076 sm->cur_pmksa = NULL;
2077 }
2078}
2079
2080
2081static void wpa_eapol_key_dump(struct wpa_sm *sm,
Dmitry Shmidt807291d2015-01-27 13:40:23 -08002082 const struct wpa_eapol_key *key,
2083 unsigned int key_data_len,
2084 const u8 *mic, unsigned int mic_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002085{
2086#ifndef CONFIG_NO_STDOUT_DEBUG
2087 u16 key_info = WPA_GET_BE16(key->key_info);
2088
2089 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, " EAPOL-Key type=%d", key->type);
2090 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2091 " key_info 0x%x (ver=%d keyidx=%d rsvd=%d %s%s%s%s%s%s%s%s)",
2092 key_info, key_info & WPA_KEY_INFO_TYPE_MASK,
2093 (key_info & WPA_KEY_INFO_KEY_INDEX_MASK) >>
2094 WPA_KEY_INFO_KEY_INDEX_SHIFT,
2095 (key_info & (BIT(13) | BIT(14) | BIT(15))) >> 13,
2096 key_info & WPA_KEY_INFO_KEY_TYPE ? "Pairwise" : "Group",
2097 key_info & WPA_KEY_INFO_INSTALL ? " Install" : "",
2098 key_info & WPA_KEY_INFO_ACK ? " Ack" : "",
2099 key_info & WPA_KEY_INFO_MIC ? " MIC" : "",
2100 key_info & WPA_KEY_INFO_SECURE ? " Secure" : "",
2101 key_info & WPA_KEY_INFO_ERROR ? " Error" : "",
2102 key_info & WPA_KEY_INFO_REQUEST ? " Request" : "",
2103 key_info & WPA_KEY_INFO_ENCR_KEY_DATA ? " Encr" : "");
2104 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2105 " key_length=%u key_data_length=%u",
Dmitry Shmidt807291d2015-01-27 13:40:23 -08002106 WPA_GET_BE16(key->key_length), key_data_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002107 wpa_hexdump(MSG_DEBUG, " replay_counter",
2108 key->replay_counter, WPA_REPLAY_COUNTER_LEN);
2109 wpa_hexdump(MSG_DEBUG, " key_nonce", key->key_nonce, WPA_NONCE_LEN);
2110 wpa_hexdump(MSG_DEBUG, " key_iv", key->key_iv, 16);
2111 wpa_hexdump(MSG_DEBUG, " key_rsc", key->key_rsc, 8);
2112 wpa_hexdump(MSG_DEBUG, " key_id (reserved)", key->key_id, 8);
Dmitry Shmidt807291d2015-01-27 13:40:23 -08002113 wpa_hexdump(MSG_DEBUG, " key_mic", mic, mic_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002114#endif /* CONFIG_NO_STDOUT_DEBUG */
2115}
2116
2117
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002118#ifdef CONFIG_FILS
2119static int wpa_supp_aead_decrypt(struct wpa_sm *sm, u8 *buf, size_t buf_len,
2120 size_t *key_data_len)
2121{
2122 struct wpa_ptk *ptk;
2123 struct ieee802_1x_hdr *hdr;
2124 struct wpa_eapol_key *key;
2125 u8 *pos, *tmp;
2126 const u8 *aad[1];
2127 size_t aad_len[1];
2128
2129 if (*key_data_len < AES_BLOCK_SIZE) {
2130 wpa_printf(MSG_INFO, "No room for AES-SIV data in the frame");
2131 return -1;
2132 }
2133
2134 if (sm->tptk_set)
2135 ptk = &sm->tptk;
2136 else if (sm->ptk_set)
2137 ptk = &sm->ptk;
2138 else
2139 return -1;
2140
2141 hdr = (struct ieee802_1x_hdr *) buf;
2142 key = (struct wpa_eapol_key *) (hdr + 1);
2143 pos = (u8 *) (key + 1);
2144 pos += 2; /* Pointing at the Encrypted Key Data field */
2145
2146 tmp = os_malloc(*key_data_len);
2147 if (!tmp)
2148 return -1;
2149
2150 /* AES-SIV AAD from EAPOL protocol version field (inclusive) to
2151 * to Key Data (exclusive). */
2152 aad[0] = buf;
2153 aad_len[0] = pos - buf;
2154 if (aes_siv_decrypt(ptk->kek, ptk->kek_len, pos, *key_data_len,
2155 1, aad, aad_len, tmp) < 0) {
2156 wpa_printf(MSG_INFO, "Invalid AES-SIV data in the frame");
2157 bin_clear_free(tmp, *key_data_len);
2158 return -1;
2159 }
2160
2161 /* AEAD decryption and validation completed successfully */
2162 (*key_data_len) -= AES_BLOCK_SIZE;
2163 wpa_hexdump_key(MSG_DEBUG, "WPA: Decrypted Key Data",
2164 tmp, *key_data_len);
2165
2166 /* Replace Key Data field with the decrypted version */
2167 os_memcpy(pos, tmp, *key_data_len);
2168 pos -= 2; /* Key Data Length field */
2169 WPA_PUT_BE16(pos, *key_data_len);
2170 bin_clear_free(tmp, *key_data_len);
2171
2172 if (sm->tptk_set) {
2173 sm->tptk_set = 0;
2174 sm->ptk_set = 1;
2175 os_memcpy(&sm->ptk, &sm->tptk, sizeof(sm->ptk));
2176 os_memset(&sm->tptk, 0, sizeof(sm->tptk));
2177 }
2178
2179 os_memcpy(sm->rx_replay_counter, key->replay_counter,
2180 WPA_REPLAY_COUNTER_LEN);
2181 sm->rx_replay_counter_set = 1;
2182
2183 return 0;
2184}
2185#endif /* CONFIG_FILS */
2186
2187
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002188/**
2189 * wpa_sm_rx_eapol - Process received WPA EAPOL frames
2190 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2191 * @src_addr: Source MAC address of the EAPOL packet
2192 * @buf: Pointer to the beginning of the EAPOL data (EAPOL header)
2193 * @len: Length of the EAPOL frame
2194 * Returns: 1 = WPA EAPOL-Key processed, 0 = not a WPA EAPOL-Key, -1 failure
2195 *
2196 * This function is called for each received EAPOL frame. Other than EAPOL-Key
2197 * frames can be skipped if filtering is done elsewhere. wpa_sm_rx_eapol() is
2198 * only processing WPA and WPA2 EAPOL-Key frames.
2199 *
2200 * The received EAPOL-Key packets are validated and valid packets are replied
2201 * to. In addition, key material (PTK, GTK) is configured at the end of a
2202 * successful key handshake.
2203 */
2204int wpa_sm_rx_eapol(struct wpa_sm *sm, const u8 *src_addr,
2205 const u8 *buf, size_t len)
2206{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002207 size_t plen, data_len, key_data_len;
2208 const struct ieee802_1x_hdr *hdr;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002209 struct wpa_eapol_key *key;
2210 u16 key_info, ver;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002211 u8 *tmp = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002212 int ret = -1;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002213 u8 *mic, *key_data;
Dmitry Shmidt807291d2015-01-27 13:40:23 -08002214 size_t mic_len, keyhdrlen;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002215
2216#ifdef CONFIG_IEEE80211R
2217 sm->ft_completed = 0;
2218#endif /* CONFIG_IEEE80211R */
2219
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002220 mic_len = wpa_mic_len(sm->key_mgmt, sm->pmk_len);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002221 keyhdrlen = sizeof(*key) + mic_len + 2;
Dmitry Shmidt807291d2015-01-27 13:40:23 -08002222
2223 if (len < sizeof(*hdr) + keyhdrlen) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002224 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2225 "WPA: EAPOL frame too short to be a WPA "
2226 "EAPOL-Key (len %lu, expecting at least %lu)",
2227 (unsigned long) len,
Dmitry Shmidt807291d2015-01-27 13:40:23 -08002228 (unsigned long) sizeof(*hdr) + keyhdrlen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002229 return 0;
2230 }
2231
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002232 hdr = (const struct ieee802_1x_hdr *) buf;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002233 plen = be_to_host16(hdr->length);
2234 data_len = plen + sizeof(*hdr);
2235 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2236 "IEEE 802.1X RX: version=%d type=%d length=%lu",
2237 hdr->version, hdr->type, (unsigned long) plen);
2238
2239 if (hdr->version < EAPOL_VERSION) {
2240 /* TODO: backwards compatibility */
2241 }
2242 if (hdr->type != IEEE802_1X_TYPE_EAPOL_KEY) {
2243 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2244 "WPA: EAPOL frame (type %u) discarded, "
2245 "not a Key frame", hdr->type);
2246 ret = 0;
2247 goto out;
2248 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002249 wpa_hexdump(MSG_MSGDUMP, "WPA: RX EAPOL-Key", buf, len);
Dmitry Shmidt807291d2015-01-27 13:40:23 -08002250 if (plen > len - sizeof(*hdr) || plen < keyhdrlen) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002251 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2252 "WPA: EAPOL frame payload size %lu "
2253 "invalid (frame size %lu)",
2254 (unsigned long) plen, (unsigned long) len);
2255 ret = 0;
2256 goto out;
2257 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002258 if (data_len < len) {
2259 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2260 "WPA: ignoring %lu bytes after the IEEE 802.1X data",
2261 (unsigned long) len - data_len);
2262 }
2263
2264 /*
2265 * Make a copy of the frame since we need to modify the buffer during
2266 * MAC validation and Key Data decryption.
2267 */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002268 tmp = os_memdup(buf, data_len);
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002269 if (tmp == NULL)
2270 goto out;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002271 key = (struct wpa_eapol_key *) (tmp + sizeof(struct ieee802_1x_hdr));
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002272 mic = (u8 *) (key + 1);
2273 key_data = mic + mic_len + 2;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002274
2275 if (key->type != EAPOL_KEY_TYPE_WPA && key->type != EAPOL_KEY_TYPE_RSN)
2276 {
2277 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2278 "WPA: EAPOL-Key type (%d) unknown, discarded",
2279 key->type);
2280 ret = 0;
2281 goto out;
2282 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002283
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002284 key_data_len = WPA_GET_BE16(mic + mic_len);
2285 wpa_eapol_key_dump(sm, key, key_data_len, mic, mic_len);
Dmitry Shmidt807291d2015-01-27 13:40:23 -08002286
2287 if (key_data_len > plen - keyhdrlen) {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002288 wpa_msg(sm->ctx->msg_ctx, MSG_INFO, "WPA: Invalid EAPOL-Key "
2289 "frame - key_data overflow (%u > %u)",
2290 (unsigned int) key_data_len,
Dmitry Shmidt807291d2015-01-27 13:40:23 -08002291 (unsigned int) (plen - keyhdrlen));
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002292 goto out;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002293 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002294
2295 eapol_sm_notify_lower_layer_success(sm->eapol, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002296 key_info = WPA_GET_BE16(key->key_info);
2297 ver = key_info & WPA_KEY_INFO_TYPE_MASK;
2298 if (ver != WPA_KEY_INFO_TYPE_HMAC_MD5_RC4 &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002299 ver != WPA_KEY_INFO_TYPE_AES_128_CMAC &&
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002300 ver != WPA_KEY_INFO_TYPE_HMAC_SHA1_AES &&
Roshan Pius3a1667e2018-07-03 15:17:14 -07002301 !wpa_use_akm_defined(sm->key_mgmt)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002302 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2303 "WPA: Unsupported EAPOL-Key descriptor version %d",
2304 ver);
2305 goto out;
2306 }
2307
Roshan Pius3a1667e2018-07-03 15:17:14 -07002308 if (wpa_use_akm_defined(sm->key_mgmt) &&
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002309 ver != WPA_KEY_INFO_TYPE_AKM_DEFINED) {
2310 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2311 "RSN: Unsupported EAPOL-Key descriptor version %d (expected AKM defined = 0)",
2312 ver);
2313 goto out;
2314 }
2315
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002316#ifdef CONFIG_IEEE80211R
2317 if (wpa_key_mgmt_ft(sm->key_mgmt)) {
2318 /* IEEE 802.11r uses a new key_info type (AES-128-CMAC). */
Roshan Pius3a1667e2018-07-03 15:17:14 -07002319 if (ver != WPA_KEY_INFO_TYPE_AES_128_CMAC &&
2320 !wpa_use_akm_defined(sm->key_mgmt)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002321 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2322 "FT: AP did not use AES-128-CMAC");
2323 goto out;
2324 }
2325 } else
2326#endif /* CONFIG_IEEE80211R */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002327 if (wpa_key_mgmt_sha256(sm->key_mgmt)) {
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002328 if (ver != WPA_KEY_INFO_TYPE_AES_128_CMAC &&
Roshan Pius3a1667e2018-07-03 15:17:14 -07002329 !wpa_use_akm_defined(sm->key_mgmt)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002330 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2331 "WPA: AP did not use the "
2332 "negotiated AES-128-CMAC");
2333 goto out;
2334 }
Hai Shalomc3565922019-10-28 11:58:20 -07002335 } else if (sm->pairwise_cipher == WPA_CIPHER_CCMP &&
2336 !wpa_use_akm_defined(sm->key_mgmt) &&
2337 ver != WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002338 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2339 "WPA: CCMP is used, but EAPOL-Key "
2340 "descriptor version (%d) is not 2", ver);
2341 if (sm->group_cipher != WPA_CIPHER_CCMP &&
2342 !(key_info & WPA_KEY_INFO_KEY_TYPE)) {
2343 /* Earlier versions of IEEE 802.11i did not explicitly
2344 * require version 2 descriptor for all EAPOL-Key
2345 * packets, so allow group keys to use version 1 if
2346 * CCMP is not used for them. */
2347 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2348 "WPA: Backwards compatibility: allow invalid "
2349 "version for non-CCMP group keys");
Jouni Malinen658fb4a2014-11-14 20:57:05 +02002350 } else if (ver == WPA_KEY_INFO_TYPE_AES_128_CMAC) {
2351 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2352 "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 -07002353 } else
2354 goto out;
Dmitry Shmidt71757432014-06-02 13:50:35 -07002355 } else if (sm->pairwise_cipher == WPA_CIPHER_GCMP &&
Roshan Pius3a1667e2018-07-03 15:17:14 -07002356 !wpa_use_akm_defined(sm->key_mgmt) &&
Dmitry Shmidt71757432014-06-02 13:50:35 -07002357 ver != WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002358 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2359 "WPA: GCMP is used, but EAPOL-Key "
2360 "descriptor version (%d) is not 2", ver);
2361 goto out;
2362 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002363
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002364 if (sm->rx_replay_counter_set &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002365 os_memcmp(key->replay_counter, sm->rx_replay_counter,
2366 WPA_REPLAY_COUNTER_LEN) <= 0) {
2367 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
2368 "WPA: EAPOL-Key Replay Counter did not increase - "
2369 "dropping packet");
2370 goto out;
2371 }
2372
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002373 if (key_info & WPA_KEY_INFO_SMK_MESSAGE) {
2374 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2375 "WPA: Unsupported SMK bit in key_info");
2376 goto out;
2377 }
2378
2379 if (!(key_info & WPA_KEY_INFO_ACK)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002380 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2381 "WPA: No Ack bit in key_info");
2382 goto out;
2383 }
2384
2385 if (key_info & WPA_KEY_INFO_REQUEST) {
2386 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2387 "WPA: EAPOL-Key with Request bit - dropped");
2388 goto out;
2389 }
2390
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002391 if ((key_info & WPA_KEY_INFO_MIC) &&
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002392 wpa_supplicant_verify_eapol_key_mic(sm, key, ver, tmp, data_len))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002393 goto out;
2394
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002395#ifdef CONFIG_FILS
2396 if (!mic_len && (key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
2397 if (wpa_supp_aead_decrypt(sm, tmp, data_len, &key_data_len))
2398 goto out;
2399 }
2400#endif /* CONFIG_FILS */
2401
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002402 if ((sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN) &&
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002403 (key_info & WPA_KEY_INFO_ENCR_KEY_DATA) && mic_len) {
Hai Shalomce48b4a2018-09-05 11:41:35 -07002404 /*
2405 * Only decrypt the Key Data field if the frame's authenticity
2406 * was verified. When using AES-SIV (FILS), the MIC flag is not
2407 * set, so this check should only be performed if mic_len != 0
2408 * which is the case in this code branch.
2409 */
2410 if (!(key_info & WPA_KEY_INFO_MIC)) {
2411 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
2412 "WPA: Ignore EAPOL-Key with encrypted but unauthenticated data");
2413 goto out;
2414 }
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002415 if (wpa_supplicant_decrypt_key_data(sm, key, mic_len,
2416 ver, key_data,
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002417 &key_data_len))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002418 goto out;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002419 }
2420
2421 if (key_info & WPA_KEY_INFO_KEY_TYPE) {
2422 if (key_info & WPA_KEY_INFO_KEY_INDEX_MASK) {
2423 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
2424 "WPA: Ignored EAPOL-Key (Pairwise) with "
2425 "non-zero key index");
2426 goto out;
2427 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002428 if (key_info & (WPA_KEY_INFO_MIC |
2429 WPA_KEY_INFO_ENCR_KEY_DATA)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002430 /* 3/4 4-Way Handshake */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002431 wpa_supplicant_process_3_of_4(sm, key, ver, key_data,
2432 key_data_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002433 } else {
2434 /* 1/4 4-Way Handshake */
2435 wpa_supplicant_process_1_of_4(sm, src_addr, key,
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002436 ver, key_data,
2437 key_data_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002438 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002439 } else {
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002440 if ((mic_len && (key_info & WPA_KEY_INFO_MIC)) ||
2441 (!mic_len && (key_info & WPA_KEY_INFO_ENCR_KEY_DATA))) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002442 /* 1/2 Group Key Handshake */
2443 wpa_supplicant_process_1_of_2(sm, src_addr, key,
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002444 key_data, key_data_len,
2445 ver);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002446 } else {
2447 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002448 "WPA: EAPOL-Key (Group) without Mic/Encr bit - "
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002449 "dropped");
2450 }
2451 }
2452
2453 ret = 1;
2454
2455out:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002456 bin_clear_free(tmp, data_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002457 return ret;
2458}
2459
2460
2461#ifdef CONFIG_CTRL_IFACE
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002462static u32 wpa_key_mgmt_suite(struct wpa_sm *sm)
2463{
2464 switch (sm->key_mgmt) {
2465 case WPA_KEY_MGMT_IEEE8021X:
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002466 return ((sm->proto == WPA_PROTO_RSN ||
2467 sm->proto == WPA_PROTO_OSEN) ?
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002468 RSN_AUTH_KEY_MGMT_UNSPEC_802_1X :
2469 WPA_AUTH_KEY_MGMT_UNSPEC_802_1X);
2470 case WPA_KEY_MGMT_PSK:
2471 return (sm->proto == WPA_PROTO_RSN ?
2472 RSN_AUTH_KEY_MGMT_PSK_OVER_802_1X :
2473 WPA_AUTH_KEY_MGMT_PSK_OVER_802_1X);
2474#ifdef CONFIG_IEEE80211R
2475 case WPA_KEY_MGMT_FT_IEEE8021X:
2476 return RSN_AUTH_KEY_MGMT_FT_802_1X;
2477 case WPA_KEY_MGMT_FT_PSK:
2478 return RSN_AUTH_KEY_MGMT_FT_PSK;
2479#endif /* CONFIG_IEEE80211R */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002480 case WPA_KEY_MGMT_IEEE8021X_SHA256:
2481 return RSN_AUTH_KEY_MGMT_802_1X_SHA256;
2482 case WPA_KEY_MGMT_PSK_SHA256:
2483 return RSN_AUTH_KEY_MGMT_PSK_SHA256;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002484 case WPA_KEY_MGMT_CCKM:
2485 return (sm->proto == WPA_PROTO_RSN ?
2486 RSN_AUTH_KEY_MGMT_CCKM:
2487 WPA_AUTH_KEY_MGMT_CCKM);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002488 case WPA_KEY_MGMT_WPA_NONE:
2489 return WPA_AUTH_KEY_MGMT_NONE;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002490 case WPA_KEY_MGMT_IEEE8021X_SUITE_B:
2491 return RSN_AUTH_KEY_MGMT_802_1X_SUITE_B;
Dmitry Shmidt807291d2015-01-27 13:40:23 -08002492 case WPA_KEY_MGMT_IEEE8021X_SUITE_B_192:
2493 return RSN_AUTH_KEY_MGMT_802_1X_SUITE_B_192;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002494 default:
2495 return 0;
2496 }
2497}
2498
2499
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002500#define RSN_SUITE "%02x-%02x-%02x-%d"
2501#define RSN_SUITE_ARG(s) \
2502((s) >> 24) & 0xff, ((s) >> 16) & 0xff, ((s) >> 8) & 0xff, (s) & 0xff
2503
2504/**
2505 * wpa_sm_get_mib - Dump text list of MIB entries
2506 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2507 * @buf: Buffer for the list
2508 * @buflen: Length of the buffer
2509 * Returns: Number of bytes written to buffer
2510 *
2511 * This function is used fetch dot11 MIB variables.
2512 */
2513int wpa_sm_get_mib(struct wpa_sm *sm, char *buf, size_t buflen)
2514{
2515 char pmkid_txt[PMKID_LEN * 2 + 1];
2516 int rsna, ret;
2517 size_t len;
2518
2519 if (sm->cur_pmksa) {
2520 wpa_snprintf_hex(pmkid_txt, sizeof(pmkid_txt),
2521 sm->cur_pmksa->pmkid, PMKID_LEN);
2522 } else
2523 pmkid_txt[0] = '\0';
2524
2525 if ((wpa_key_mgmt_wpa_psk(sm->key_mgmt) ||
2526 wpa_key_mgmt_wpa_ieee8021x(sm->key_mgmt)) &&
2527 sm->proto == WPA_PROTO_RSN)
2528 rsna = 1;
2529 else
2530 rsna = 0;
2531
2532 ret = os_snprintf(buf, buflen,
2533 "dot11RSNAOptionImplemented=TRUE\n"
2534 "dot11RSNAPreauthenticationImplemented=TRUE\n"
2535 "dot11RSNAEnabled=%s\n"
2536 "dot11RSNAPreauthenticationEnabled=%s\n"
2537 "dot11RSNAConfigVersion=%d\n"
2538 "dot11RSNAConfigPairwiseKeysSupported=5\n"
2539 "dot11RSNAConfigGroupCipherSize=%d\n"
2540 "dot11RSNAConfigPMKLifetime=%d\n"
2541 "dot11RSNAConfigPMKReauthThreshold=%d\n"
2542 "dot11RSNAConfigNumberOfPTKSAReplayCounters=1\n"
2543 "dot11RSNAConfigSATimeout=%d\n",
2544 rsna ? "TRUE" : "FALSE",
2545 rsna ? "TRUE" : "FALSE",
2546 RSN_VERSION,
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07002547 wpa_cipher_key_len(sm->group_cipher) * 8,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002548 sm->dot11RSNAConfigPMKLifetime,
2549 sm->dot11RSNAConfigPMKReauthThreshold,
2550 sm->dot11RSNAConfigSATimeout);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002551 if (os_snprintf_error(buflen, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002552 return 0;
2553 len = ret;
2554
2555 ret = os_snprintf(
2556 buf + len, buflen - len,
2557 "dot11RSNAAuthenticationSuiteSelected=" RSN_SUITE "\n"
2558 "dot11RSNAPairwiseCipherSelected=" RSN_SUITE "\n"
2559 "dot11RSNAGroupCipherSelected=" RSN_SUITE "\n"
2560 "dot11RSNAPMKIDUsed=%s\n"
2561 "dot11RSNAAuthenticationSuiteRequested=" RSN_SUITE "\n"
2562 "dot11RSNAPairwiseCipherRequested=" RSN_SUITE "\n"
2563 "dot11RSNAGroupCipherRequested=" RSN_SUITE "\n"
2564 "dot11RSNAConfigNumberOfGTKSAReplayCounters=0\n"
2565 "dot11RSNA4WayHandshakeFailures=%u\n",
2566 RSN_SUITE_ARG(wpa_key_mgmt_suite(sm)),
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07002567 RSN_SUITE_ARG(wpa_cipher_to_suite(sm->proto,
2568 sm->pairwise_cipher)),
2569 RSN_SUITE_ARG(wpa_cipher_to_suite(sm->proto,
2570 sm->group_cipher)),
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002571 pmkid_txt,
2572 RSN_SUITE_ARG(wpa_key_mgmt_suite(sm)),
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07002573 RSN_SUITE_ARG(wpa_cipher_to_suite(sm->proto,
2574 sm->pairwise_cipher)),
2575 RSN_SUITE_ARG(wpa_cipher_to_suite(sm->proto,
2576 sm->group_cipher)),
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002577 sm->dot11RSNA4WayHandshakeFailures);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002578 if (!os_snprintf_error(buflen - len, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002579 len += ret;
2580
2581 return (int) len;
2582}
2583#endif /* CONFIG_CTRL_IFACE */
2584
2585
2586static void wpa_sm_pmksa_free_cb(struct rsn_pmksa_cache_entry *entry,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002587 void *ctx, enum pmksa_free_reason reason)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002588{
2589 struct wpa_sm *sm = ctx;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002590 int deauth = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002591
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002592 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "RSN: PMKSA cache entry free_cb: "
2593 MACSTR " reason=%d", MAC2STR(entry->aa), reason);
2594
2595 if (sm->cur_pmksa == entry) {
2596 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2597 "RSN: %s current PMKSA entry",
2598 reason == PMKSA_REPLACE ? "replaced" : "removed");
2599 pmksa_cache_clear_current(sm);
2600
2601 /*
2602 * If an entry is simply being replaced, there's no need to
2603 * deauthenticate because it will be immediately re-added.
2604 * This happens when EAP authentication is completed again
2605 * (reauth or failed PMKSA caching attempt).
2606 */
2607 if (reason != PMKSA_REPLACE)
2608 deauth = 1;
2609 }
2610
2611 if (reason == PMKSA_EXPIRE &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002612 (sm->pmk_len == entry->pmk_len &&
2613 os_memcmp(sm->pmk, entry->pmk, sm->pmk_len) == 0)) {
2614 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002615 "RSN: deauthenticating due to expired PMK");
2616 pmksa_cache_clear_current(sm);
2617 deauth = 1;
2618 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002619
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002620 if (deauth) {
Roshan Pius3a1667e2018-07-03 15:17:14 -07002621 sm->pmk_len = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002622 os_memset(sm->pmk, 0, sizeof(sm->pmk));
2623 wpa_sm_deauthenticate(sm, WLAN_REASON_UNSPECIFIED);
2624 }
2625}
2626
2627
2628/**
2629 * wpa_sm_init - Initialize WPA state machine
2630 * @ctx: Context pointer for callbacks; this needs to be an allocated buffer
2631 * Returns: Pointer to the allocated WPA state machine data
2632 *
2633 * This function is used to allocate a new WPA state machine and the returned
2634 * value is passed to all WPA state machine calls.
2635 */
2636struct wpa_sm * wpa_sm_init(struct wpa_sm_ctx *ctx)
2637{
2638 struct wpa_sm *sm;
2639
2640 sm = os_zalloc(sizeof(*sm));
2641 if (sm == NULL)
2642 return NULL;
2643 dl_list_init(&sm->pmksa_candidates);
2644 sm->renew_snonce = 1;
2645 sm->ctx = ctx;
2646
2647 sm->dot11RSNAConfigPMKLifetime = 43200;
2648 sm->dot11RSNAConfigPMKReauthThreshold = 70;
2649 sm->dot11RSNAConfigSATimeout = 60;
2650
2651 sm->pmksa = pmksa_cache_init(wpa_sm_pmksa_free_cb, sm, sm);
2652 if (sm->pmksa == NULL) {
2653 wpa_msg(sm->ctx->msg_ctx, MSG_ERROR,
2654 "RSN: PMKSA cache initialization failed");
2655 os_free(sm);
2656 return NULL;
2657 }
2658
2659 return sm;
2660}
2661
2662
2663/**
2664 * wpa_sm_deinit - Deinitialize WPA state machine
2665 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2666 */
2667void wpa_sm_deinit(struct wpa_sm *sm)
2668{
2669 if (sm == NULL)
2670 return;
2671 pmksa_cache_deinit(sm->pmksa);
2672 eloop_cancel_timeout(wpa_sm_start_preauth, sm, NULL);
2673 eloop_cancel_timeout(wpa_sm_rekey_ptk, sm, NULL);
2674 os_free(sm->assoc_wpa_ie);
Hai Shalomc3565922019-10-28 11:58:20 -07002675 os_free(sm->assoc_rsnxe);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002676 os_free(sm->ap_wpa_ie);
2677 os_free(sm->ap_rsn_ie);
Hai Shalomc3565922019-10-28 11:58:20 -07002678 os_free(sm->ap_rsnxe);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002679 wpa_sm_drop_sa(sm);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002680 os_free(sm->ctx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002681#ifdef CONFIG_IEEE80211R
2682 os_free(sm->assoc_resp_ies);
2683#endif /* CONFIG_IEEE80211R */
Dmitry Shmidt55840ad2015-12-14 12:45:46 -08002684#ifdef CONFIG_TESTING_OPTIONS
2685 wpabuf_free(sm->test_assoc_ie);
2686#endif /* CONFIG_TESTING_OPTIONS */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002687#ifdef CONFIG_FILS_SK_PFS
2688 crypto_ecdh_deinit(sm->fils_ecdh);
2689#endif /* CONFIG_FILS_SK_PFS */
2690#ifdef CONFIG_FILS
2691 wpabuf_free(sm->fils_ft_ies);
2692#endif /* CONFIG_FILS */
2693#ifdef CONFIG_OWE
2694 crypto_ecdh_deinit(sm->owe_ecdh);
2695#endif /* CONFIG_OWE */
Hai Shalom021b0b52019-04-10 11:17:58 -07002696#ifdef CONFIG_DPP2
2697 wpabuf_clear_free(sm->dpp_z);
2698#endif /* CONFIG_DPP2 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002699 os_free(sm);
2700}
2701
2702
2703/**
2704 * wpa_sm_notify_assoc - Notify WPA state machine about association
2705 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2706 * @bssid: The BSSID of the new association
2707 *
2708 * This function is called to let WPA state machine know that the connection
2709 * was established.
2710 */
2711void wpa_sm_notify_assoc(struct wpa_sm *sm, const u8 *bssid)
2712{
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02002713 int clear_keys = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002714
2715 if (sm == NULL)
2716 return;
2717
2718 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2719 "WPA: Association event - clear replay counter");
2720 os_memcpy(sm->bssid, bssid, ETH_ALEN);
2721 os_memset(sm->rx_replay_counter, 0, WPA_REPLAY_COUNTER_LEN);
2722 sm->rx_replay_counter_set = 0;
2723 sm->renew_snonce = 1;
2724 if (os_memcmp(sm->preauth_bssid, bssid, ETH_ALEN) == 0)
2725 rsn_preauth_deinit(sm);
2726
2727#ifdef CONFIG_IEEE80211R
2728 if (wpa_ft_is_completed(sm)) {
2729 /*
2730 * Clear portValid to kick EAPOL state machine to re-enter
2731 * AUTHENTICATED state to get the EAPOL port Authorized.
2732 */
2733 eapol_sm_notify_portValid(sm->eapol, FALSE);
2734 wpa_supplicant_key_neg_complete(sm, sm->bssid, 1);
2735
2736 /* Prepare for the next transition */
2737 wpa_ft_prepare_auth_request(sm, NULL);
2738
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02002739 clear_keys = 0;
Hai Shalom81f62d82019-07-22 12:10:00 -07002740 sm->ft_protocol = 1;
2741 } else {
2742 sm->ft_protocol = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002743 }
2744#endif /* CONFIG_IEEE80211R */
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002745#ifdef CONFIG_FILS
2746 if (sm->fils_completed) {
2747 /*
2748 * Clear portValid to kick EAPOL state machine to re-enter
2749 * AUTHENTICATED state to get the EAPOL port Authorized.
2750 */
2751 wpa_supplicant_key_neg_complete(sm, sm->bssid, 1);
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02002752 clear_keys = 0;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002753 }
2754#endif /* CONFIG_FILS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002755
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02002756 if (clear_keys) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002757 /*
2758 * IEEE 802.11, 8.4.10: Delete PTK SA on (re)association if
2759 * this is not part of a Fast BSS Transition.
2760 */
2761 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Clear old PTK");
2762 sm->ptk_set = 0;
Dmitry Shmidt61593f02014-04-21 16:27:35 -07002763 os_memset(&sm->ptk, 0, sizeof(sm->ptk));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002764 sm->tptk_set = 0;
Dmitry Shmidt61593f02014-04-21 16:27:35 -07002765 os_memset(&sm->tptk, 0, sizeof(sm->tptk));
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02002766 os_memset(&sm->gtk, 0, sizeof(sm->gtk));
Jouni Malinen58c0e962017-10-01 12:12:24 +03002767 os_memset(&sm->gtk_wnm_sleep, 0, sizeof(sm->gtk_wnm_sleep));
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02002768 os_memset(&sm->igtk, 0, sizeof(sm->igtk));
Jouni Malinen58c0e962017-10-01 12:12:24 +03002769 os_memset(&sm->igtk_wnm_sleep, 0, sizeof(sm->igtk_wnm_sleep));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002770 }
2771
2772#ifdef CONFIG_TDLS
2773 wpa_tdls_assoc(sm);
2774#endif /* CONFIG_TDLS */
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002775
2776#ifdef CONFIG_P2P
2777 os_memset(sm->p2p_ip_addr, 0, sizeof(sm->p2p_ip_addr));
2778#endif /* CONFIG_P2P */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002779}
2780
2781
2782/**
2783 * wpa_sm_notify_disassoc - Notify WPA state machine about disassociation
2784 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2785 *
2786 * This function is called to let WPA state machine know that the connection
2787 * was lost. This will abort any existing pre-authentication session.
2788 */
2789void wpa_sm_notify_disassoc(struct wpa_sm *sm)
2790{
Dmitry Shmidt4dd28dc2015-03-10 11:21:43 -07002791 eloop_cancel_timeout(wpa_sm_start_preauth, sm, NULL);
2792 eloop_cancel_timeout(wpa_sm_rekey_ptk, sm, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002793 rsn_preauth_deinit(sm);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002794 pmksa_cache_clear_current(sm);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002795 if (wpa_sm_get_state(sm) == WPA_4WAY_HANDSHAKE)
2796 sm->dot11RSNA4WayHandshakeFailures++;
2797#ifdef CONFIG_TDLS
2798 wpa_tdls_disassoc(sm);
2799#endif /* CONFIG_TDLS */
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002800#ifdef CONFIG_FILS
2801 sm->fils_completed = 0;
2802#endif /* CONFIG_FILS */
Jouni Malinen4283f9e2017-09-22 12:06:37 +03002803#ifdef CONFIG_IEEE80211R
2804 sm->ft_reassoc_completed = 0;
Hai Shalom81f62d82019-07-22 12:10:00 -07002805 sm->ft_protocol = 0;
Jouni Malinen4283f9e2017-09-22 12:06:37 +03002806#endif /* CONFIG_IEEE80211R */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002807
2808 /* Keys are not needed in the WPA state machine anymore */
2809 wpa_sm_drop_sa(sm);
Dmitry Shmidt4dd28dc2015-03-10 11:21:43 -07002810
2811 sm->msg_3_of_4_ok = 0;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002812 os_memset(sm->bssid, 0, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002813}
2814
2815
2816/**
2817 * wpa_sm_set_pmk - Set PMK
2818 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2819 * @pmk: The new PMK
2820 * @pmk_len: The length of the new PMK in bytes
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08002821 * @pmkid: Calculated PMKID
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002822 * @bssid: AA to add into PMKSA cache or %NULL to not cache the PMK
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002823 *
2824 * Configure the PMK for WPA state machine.
2825 */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002826void wpa_sm_set_pmk(struct wpa_sm *sm, const u8 *pmk, size_t pmk_len,
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08002827 const u8 *pmkid, const u8 *bssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002828{
2829 if (sm == NULL)
2830 return;
2831
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002832 wpa_hexdump_key(MSG_DEBUG, "WPA: Set PMK based on external data",
2833 pmk, pmk_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002834 sm->pmk_len = pmk_len;
2835 os_memcpy(sm->pmk, pmk, pmk_len);
2836
2837#ifdef CONFIG_IEEE80211R
2838 /* Set XXKey to be PSK for FT key derivation */
2839 sm->xxkey_len = pmk_len;
2840 os_memcpy(sm->xxkey, pmk, pmk_len);
2841#endif /* CONFIG_IEEE80211R */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002842
2843 if (bssid) {
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08002844 pmksa_cache_add(sm->pmksa, pmk, pmk_len, pmkid, NULL, 0,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002845 bssid, sm->own_addr,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002846 sm->network_ctx, sm->key_mgmt, NULL);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002847 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002848}
2849
2850
2851/**
2852 * wpa_sm_set_pmk_from_pmksa - Set PMK based on the current PMKSA
2853 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2854 *
2855 * Take the PMK from the current PMKSA into use. If no PMKSA is active, the PMK
2856 * will be cleared.
2857 */
2858void wpa_sm_set_pmk_from_pmksa(struct wpa_sm *sm)
2859{
2860 if (sm == NULL)
2861 return;
2862
2863 if (sm->cur_pmksa) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002864 wpa_hexdump_key(MSG_DEBUG,
2865 "WPA: Set PMK based on current PMKSA",
2866 sm->cur_pmksa->pmk, sm->cur_pmksa->pmk_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002867 sm->pmk_len = sm->cur_pmksa->pmk_len;
2868 os_memcpy(sm->pmk, sm->cur_pmksa->pmk, sm->pmk_len);
2869 } else {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002870 wpa_printf(MSG_DEBUG, "WPA: No current PMKSA - clear PMK");
2871 sm->pmk_len = 0;
2872 os_memset(sm->pmk, 0, PMK_LEN_MAX);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002873 }
2874}
2875
2876
2877/**
2878 * wpa_sm_set_fast_reauth - Set fast reauthentication (EAP) enabled/disabled
2879 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2880 * @fast_reauth: Whether fast reauthentication (EAP) is allowed
2881 */
2882void wpa_sm_set_fast_reauth(struct wpa_sm *sm, int fast_reauth)
2883{
2884 if (sm)
2885 sm->fast_reauth = fast_reauth;
2886}
2887
2888
2889/**
2890 * wpa_sm_set_scard_ctx - Set context pointer for smartcard callbacks
2891 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2892 * @scard_ctx: Context pointer for smartcard related callback functions
2893 */
2894void wpa_sm_set_scard_ctx(struct wpa_sm *sm, void *scard_ctx)
2895{
2896 if (sm == NULL)
2897 return;
2898 sm->scard_ctx = scard_ctx;
2899 if (sm->preauth_eapol)
2900 eapol_sm_register_scard_ctx(sm->preauth_eapol, scard_ctx);
2901}
2902
2903
2904/**
2905 * wpa_sm_set_config - Notification of current configration change
2906 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2907 * @config: Pointer to current network configuration
2908 *
2909 * Notify WPA state machine that configuration has changed. config will be
2910 * stored as a backpointer to network configuration. This can be %NULL to clear
2911 * the stored pointed.
2912 */
2913void wpa_sm_set_config(struct wpa_sm *sm, struct rsn_supp_config *config)
2914{
2915 if (!sm)
2916 return;
2917
2918 if (config) {
2919 sm->network_ctx = config->network_ctx;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002920 sm->allowed_pairwise_cipher = config->allowed_pairwise_cipher;
2921 sm->proactive_key_caching = config->proactive_key_caching;
2922 sm->eap_workaround = config->eap_workaround;
2923 sm->eap_conf_ctx = config->eap_conf_ctx;
2924 if (config->ssid) {
2925 os_memcpy(sm->ssid, config->ssid, config->ssid_len);
2926 sm->ssid_len = config->ssid_len;
2927 } else
2928 sm->ssid_len = 0;
2929 sm->wpa_ptk_rekey = config->wpa_ptk_rekey;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002930 sm->p2p = config->p2p;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002931 sm->wpa_rsc_relaxation = config->wpa_rsc_relaxation;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002932#ifdef CONFIG_FILS
2933 if (config->fils_cache_id) {
2934 sm->fils_cache_id_set = 1;
2935 os_memcpy(sm->fils_cache_id, config->fils_cache_id,
2936 FILS_CACHE_ID_LEN);
2937 } else {
2938 sm->fils_cache_id_set = 0;
2939 }
2940#endif /* CONFIG_FILS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002941 } else {
2942 sm->network_ctx = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002943 sm->allowed_pairwise_cipher = 0;
2944 sm->proactive_key_caching = 0;
2945 sm->eap_workaround = 0;
2946 sm->eap_conf_ctx = NULL;
2947 sm->ssid_len = 0;
2948 sm->wpa_ptk_rekey = 0;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002949 sm->p2p = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002950 sm->wpa_rsc_relaxation = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002951 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002952}
2953
2954
2955/**
2956 * wpa_sm_set_own_addr - Set own MAC address
2957 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2958 * @addr: Own MAC address
2959 */
2960void wpa_sm_set_own_addr(struct wpa_sm *sm, const u8 *addr)
2961{
2962 if (sm)
2963 os_memcpy(sm->own_addr, addr, ETH_ALEN);
2964}
2965
2966
2967/**
2968 * wpa_sm_set_ifname - Set network interface name
2969 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2970 * @ifname: Interface name
2971 * @bridge_ifname: Optional bridge interface name (for pre-auth)
2972 */
2973void wpa_sm_set_ifname(struct wpa_sm *sm, const char *ifname,
2974 const char *bridge_ifname)
2975{
2976 if (sm) {
2977 sm->ifname = ifname;
2978 sm->bridge_ifname = bridge_ifname;
2979 }
2980}
2981
2982
2983/**
2984 * wpa_sm_set_eapol - Set EAPOL state machine pointer
2985 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2986 * @eapol: Pointer to EAPOL state machine allocated with eapol_sm_init()
2987 */
2988void wpa_sm_set_eapol(struct wpa_sm *sm, struct eapol_sm *eapol)
2989{
2990 if (sm)
2991 sm->eapol = eapol;
2992}
2993
2994
2995/**
2996 * wpa_sm_set_param - Set WPA state machine parameters
2997 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2998 * @param: Parameter field
2999 * @value: Parameter value
3000 * Returns: 0 on success, -1 on failure
3001 */
3002int wpa_sm_set_param(struct wpa_sm *sm, enum wpa_sm_conf_params param,
3003 unsigned int value)
3004{
3005 int ret = 0;
3006
3007 if (sm == NULL)
3008 return -1;
3009
3010 switch (param) {
3011 case RSNA_PMK_LIFETIME:
3012 if (value > 0)
3013 sm->dot11RSNAConfigPMKLifetime = value;
3014 else
3015 ret = -1;
3016 break;
3017 case RSNA_PMK_REAUTH_THRESHOLD:
3018 if (value > 0 && value <= 100)
3019 sm->dot11RSNAConfigPMKReauthThreshold = value;
3020 else
3021 ret = -1;
3022 break;
3023 case RSNA_SA_TIMEOUT:
3024 if (value > 0)
3025 sm->dot11RSNAConfigSATimeout = value;
3026 else
3027 ret = -1;
3028 break;
3029 case WPA_PARAM_PROTO:
3030 sm->proto = value;
3031 break;
3032 case WPA_PARAM_PAIRWISE:
3033 sm->pairwise_cipher = value;
3034 break;
3035 case WPA_PARAM_GROUP:
3036 sm->group_cipher = value;
3037 break;
3038 case WPA_PARAM_KEY_MGMT:
3039 sm->key_mgmt = value;
3040 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003041 case WPA_PARAM_MGMT_GROUP:
3042 sm->mgmt_group_cipher = value;
3043 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003044 case WPA_PARAM_RSN_ENABLED:
3045 sm->rsn_enabled = value;
3046 break;
3047 case WPA_PARAM_MFP:
3048 sm->mfp = value;
3049 break;
Hai Shalom74f70d42019-02-11 14:42:39 -08003050 case WPA_PARAM_OCV:
3051 sm->ocv = value;
3052 break;
Hai Shalomc3565922019-10-28 11:58:20 -07003053 case WPA_PARAM_SAE_PWE:
3054 sm->sae_pwe = value;
3055 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003056 default:
3057 break;
3058 }
3059
3060 return ret;
3061}
3062
3063
3064/**
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003065 * wpa_sm_get_status - Get WPA state machine
3066 * @sm: Pointer to WPA state machine data from wpa_sm_init()
3067 * @buf: Buffer for status information
3068 * @buflen: Maximum buffer length
3069 * @verbose: Whether to include verbose status information
3070 * Returns: Number of bytes written to buf.
3071 *
3072 * Query WPA state machine for status information. This function fills in
3073 * a text area with current status information. If the buffer (buf) is not
3074 * large enough, status information will be truncated to fit the buffer.
3075 */
3076int wpa_sm_get_status(struct wpa_sm *sm, char *buf, size_t buflen,
3077 int verbose)
3078{
3079 char *pos = buf, *end = buf + buflen;
3080 int ret;
3081
3082 ret = os_snprintf(pos, end - pos,
3083 "pairwise_cipher=%s\n"
3084 "group_cipher=%s\n"
3085 "key_mgmt=%s\n",
3086 wpa_cipher_txt(sm->pairwise_cipher),
3087 wpa_cipher_txt(sm->group_cipher),
3088 wpa_key_mgmt_txt(sm->key_mgmt, sm->proto));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003089 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003090 return pos - buf;
3091 pos += ret;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003092
3093 if (sm->mfp != NO_MGMT_FRAME_PROTECTION && sm->ap_rsn_ie) {
3094 struct wpa_ie_data rsn;
3095 if (wpa_parse_wpa_ie_rsn(sm->ap_rsn_ie, sm->ap_rsn_ie_len, &rsn)
3096 >= 0 &&
3097 rsn.capabilities & (WPA_CAPABILITY_MFPR |
3098 WPA_CAPABILITY_MFPC)) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003099 ret = os_snprintf(pos, end - pos, "pmf=%d\n"
3100 "mgmt_group_cipher=%s\n",
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003101 (rsn.capabilities &
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003102 WPA_CAPABILITY_MFPR) ? 2 : 1,
3103 wpa_cipher_txt(
3104 sm->mgmt_group_cipher));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003105 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003106 return pos - buf;
3107 pos += ret;
3108 }
3109 }
3110
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003111 return pos - buf;
3112}
3113
3114
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07003115int wpa_sm_pmf_enabled(struct wpa_sm *sm)
3116{
3117 struct wpa_ie_data rsn;
3118
3119 if (sm->mfp == NO_MGMT_FRAME_PROTECTION || !sm->ap_rsn_ie)
3120 return 0;
3121
3122 if (wpa_parse_wpa_ie_rsn(sm->ap_rsn_ie, sm->ap_rsn_ie_len, &rsn) >= 0 &&
3123 rsn.capabilities & (WPA_CAPABILITY_MFPR | WPA_CAPABILITY_MFPC))
3124 return 1;
3125
3126 return 0;
3127}
3128
3129
Hai Shalom74f70d42019-02-11 14:42:39 -08003130int wpa_sm_ocv_enabled(struct wpa_sm *sm)
3131{
3132 struct wpa_ie_data rsn;
3133
3134 if (!sm->ocv || !sm->ap_rsn_ie)
3135 return 0;
3136
3137 return wpa_parse_wpa_ie_rsn(sm->ap_rsn_ie, sm->ap_rsn_ie_len,
3138 &rsn) >= 0 &&
3139 (rsn.capabilities & WPA_CAPABILITY_OCVC);
3140}
3141
3142
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003143/**
3144 * wpa_sm_set_assoc_wpa_ie_default - Generate own WPA/RSN IE from configuration
3145 * @sm: Pointer to WPA state machine data from wpa_sm_init()
3146 * @wpa_ie: Pointer to buffer for WPA/RSN IE
3147 * @wpa_ie_len: Pointer to the length of the wpa_ie buffer
3148 * Returns: 0 on success, -1 on failure
3149 */
3150int wpa_sm_set_assoc_wpa_ie_default(struct wpa_sm *sm, u8 *wpa_ie,
3151 size_t *wpa_ie_len)
3152{
3153 int res;
3154
3155 if (sm == NULL)
3156 return -1;
3157
Dmitry Shmidt55840ad2015-12-14 12:45:46 -08003158#ifdef CONFIG_TESTING_OPTIONS
3159 if (sm->test_assoc_ie) {
3160 wpa_printf(MSG_DEBUG,
3161 "TESTING: Replace association WPA/RSN IE");
3162 if (*wpa_ie_len < wpabuf_len(sm->test_assoc_ie))
3163 return -1;
3164 os_memcpy(wpa_ie, wpabuf_head(sm->test_assoc_ie),
3165 wpabuf_len(sm->test_assoc_ie));
3166 res = wpabuf_len(sm->test_assoc_ie);
3167 } else
3168#endif /* CONFIG_TESTING_OPTIONS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003169 res = wpa_gen_wpa_ie(sm, wpa_ie, *wpa_ie_len);
3170 if (res < 0)
3171 return -1;
3172 *wpa_ie_len = res;
3173
3174 wpa_hexdump(MSG_DEBUG, "WPA: Set own WPA IE default",
3175 wpa_ie, *wpa_ie_len);
3176
3177 if (sm->assoc_wpa_ie == NULL) {
3178 /*
3179 * Make a copy of the WPA/RSN IE so that 4-Way Handshake gets
3180 * the correct version of the IE even if PMKSA caching is
3181 * aborted (which would remove PMKID from IE generation).
3182 */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003183 sm->assoc_wpa_ie = os_memdup(wpa_ie, *wpa_ie_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003184 if (sm->assoc_wpa_ie == NULL)
3185 return -1;
3186
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003187 sm->assoc_wpa_ie_len = *wpa_ie_len;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003188 } else {
3189 wpa_hexdump(MSG_DEBUG,
3190 "WPA: Leave previously set WPA IE default",
3191 sm->assoc_wpa_ie, sm->assoc_wpa_ie_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003192 }
3193
3194 return 0;
3195}
3196
3197
3198/**
3199 * wpa_sm_set_assoc_wpa_ie - Set own WPA/RSN IE from (Re)AssocReq
3200 * @sm: Pointer to WPA state machine data from wpa_sm_init()
3201 * @ie: Pointer to IE data (starting from id)
3202 * @len: IE length
3203 * Returns: 0 on success, -1 on failure
3204 *
3205 * Inform WPA state machine about the WPA/RSN IE used in (Re)Association
3206 * Request frame. The IE will be used to override the default value generated
3207 * with wpa_sm_set_assoc_wpa_ie_default().
3208 */
3209int wpa_sm_set_assoc_wpa_ie(struct wpa_sm *sm, const u8 *ie, size_t len)
3210{
3211 if (sm == NULL)
3212 return -1;
3213
3214 os_free(sm->assoc_wpa_ie);
3215 if (ie == NULL || len == 0) {
3216 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
3217 "WPA: clearing own WPA/RSN IE");
3218 sm->assoc_wpa_ie = NULL;
3219 sm->assoc_wpa_ie_len = 0;
3220 } else {
3221 wpa_hexdump(MSG_DEBUG, "WPA: set own WPA/RSN IE", ie, len);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003222 sm->assoc_wpa_ie = os_memdup(ie, len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003223 if (sm->assoc_wpa_ie == NULL)
3224 return -1;
3225
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003226 sm->assoc_wpa_ie_len = len;
3227 }
3228
3229 return 0;
3230}
3231
3232
3233/**
Hai Shalomc3565922019-10-28 11:58:20 -07003234 * wpa_sm_set_assoc_rsnxe_default - Generate own RSNXE from configuration
3235 * @sm: Pointer to WPA state machine data from wpa_sm_init()
3236 * @rsnxe: Pointer to buffer for RSNXE
3237 * @rsnxe_len: Pointer to the length of the rsne buffer
3238 * Returns: 0 on success, -1 on failure
3239 */
3240int wpa_sm_set_assoc_rsnxe_default(struct wpa_sm *sm, u8 *rsnxe,
3241 size_t *rsnxe_len)
3242{
3243 int res;
3244
3245 if (!sm)
3246 return -1;
3247
3248 res = wpa_gen_rsnxe(sm, rsnxe, *rsnxe_len);
3249 if (res < 0)
3250 return -1;
3251 *rsnxe_len = res;
3252
3253 wpa_hexdump(MSG_DEBUG, "RSN: Set own RSNXE default", rsnxe, *rsnxe_len);
3254
3255 if (sm->assoc_rsnxe) {
3256 wpa_hexdump(MSG_DEBUG,
3257 "RSN: Leave previously set RSNXE default",
3258 sm->assoc_rsnxe, sm->assoc_rsnxe_len);
3259 } else if (*rsnxe_len > 0) {
3260 /*
3261 * Make a copy of the RSNXE so that 4-Way Handshake gets the
3262 * correct version of the IE even if it gets changed.
3263 */
3264 sm->assoc_rsnxe = os_memdup(rsnxe, *rsnxe_len);
3265 if (!sm->assoc_rsnxe)
3266 return -1;
3267
3268 sm->assoc_rsnxe_len = *rsnxe_len;
3269 }
3270
3271 return 0;
3272}
3273
3274
3275/**
3276 * wpa_sm_set_assoc_rsnxe - Set own RSNXE from (Re)AssocReq
3277 * @sm: Pointer to WPA state machine data from wpa_sm_init()
3278 * @ie: Pointer to IE data (starting from id)
3279 * @len: IE length
3280 * Returns: 0 on success, -1 on failure
3281 *
3282 * Inform WPA state machine about the RSNXE used in (Re)Association Request
3283 * frame. The IE will be used to override the default value generated
3284 * with wpa_sm_set_assoc_rsnxe_default().
3285 */
3286int wpa_sm_set_assoc_rsnxe(struct wpa_sm *sm, const u8 *ie, size_t len)
3287{
3288 if (!sm)
3289 return -1;
3290
3291 os_free(sm->assoc_rsnxe);
3292 if (!ie || len == 0) {
3293 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
3294 "RSN: clearing own RSNXE");
3295 sm->assoc_rsnxe = NULL;
3296 sm->assoc_rsnxe_len = 0;
3297 } else {
3298 wpa_hexdump(MSG_DEBUG, "RSN: set own RSNXE", ie, len);
3299 sm->assoc_rsnxe = os_memdup(ie, len);
3300 if (!sm->assoc_rsnxe)
3301 return -1;
3302
3303 sm->assoc_rsnxe_len = len;
3304 }
3305
3306 return 0;
3307}
3308
3309
3310/**
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003311 * wpa_sm_set_ap_wpa_ie - Set AP WPA IE from Beacon/ProbeResp
3312 * @sm: Pointer to WPA state machine data from wpa_sm_init()
3313 * @ie: Pointer to IE data (starting from id)
3314 * @len: IE length
3315 * Returns: 0 on success, -1 on failure
3316 *
3317 * Inform WPA state machine about the WPA IE used in Beacon / Probe Response
3318 * frame.
3319 */
3320int wpa_sm_set_ap_wpa_ie(struct wpa_sm *sm, const u8 *ie, size_t len)
3321{
3322 if (sm == NULL)
3323 return -1;
3324
3325 os_free(sm->ap_wpa_ie);
3326 if (ie == NULL || len == 0) {
3327 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
3328 "WPA: clearing AP WPA IE");
3329 sm->ap_wpa_ie = NULL;
3330 sm->ap_wpa_ie_len = 0;
3331 } else {
3332 wpa_hexdump(MSG_DEBUG, "WPA: set AP WPA IE", ie, len);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003333 sm->ap_wpa_ie = os_memdup(ie, len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003334 if (sm->ap_wpa_ie == NULL)
3335 return -1;
3336
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003337 sm->ap_wpa_ie_len = len;
3338 }
3339
3340 return 0;
3341}
3342
3343
3344/**
3345 * wpa_sm_set_ap_rsn_ie - Set AP RSN IE from Beacon/ProbeResp
3346 * @sm: Pointer to WPA state machine data from wpa_sm_init()
3347 * @ie: Pointer to IE data (starting from id)
3348 * @len: IE length
3349 * Returns: 0 on success, -1 on failure
3350 *
3351 * Inform WPA state machine about the RSN IE used in Beacon / Probe Response
3352 * frame.
3353 */
3354int wpa_sm_set_ap_rsn_ie(struct wpa_sm *sm, const u8 *ie, size_t len)
3355{
3356 if (sm == NULL)
3357 return -1;
3358
3359 os_free(sm->ap_rsn_ie);
3360 if (ie == NULL || len == 0) {
3361 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
3362 "WPA: clearing AP RSN IE");
3363 sm->ap_rsn_ie = NULL;
3364 sm->ap_rsn_ie_len = 0;
3365 } else {
3366 wpa_hexdump(MSG_DEBUG, "WPA: set AP RSN IE", ie, len);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003367 sm->ap_rsn_ie = os_memdup(ie, len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003368 if (sm->ap_rsn_ie == NULL)
3369 return -1;
3370
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003371 sm->ap_rsn_ie_len = len;
3372 }
3373
3374 return 0;
3375}
3376
3377
3378/**
Hai Shalomc3565922019-10-28 11:58:20 -07003379 * wpa_sm_set_ap_rsnxe - Set AP RSNXE from Beacon/ProbeResp
3380 * @sm: Pointer to WPA state machine data from wpa_sm_init()
3381 * @ie: Pointer to IE data (starting from id)
3382 * @len: IE length
3383 * Returns: 0 on success, -1 on failure
3384 *
3385 * Inform WPA state machine about the RSNXE used in Beacon / Probe Response
3386 * frame.
3387 */
3388int wpa_sm_set_ap_rsnxe(struct wpa_sm *sm, const u8 *ie, size_t len)
3389{
3390 if (!sm)
3391 return -1;
3392
3393 os_free(sm->ap_rsnxe);
3394 if (!ie || len == 0) {
3395 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: clearing AP RSNXE");
3396 sm->ap_rsnxe = NULL;
3397 sm->ap_rsnxe_len = 0;
3398 } else {
3399 wpa_hexdump(MSG_DEBUG, "WPA: set AP RSNXE", ie, len);
3400 sm->ap_rsnxe = os_memdup(ie, len);
3401 if (!sm->ap_rsnxe)
3402 return -1;
3403
3404 sm->ap_rsnxe_len = len;
3405 }
3406
3407 return 0;
3408}
3409
3410
3411/**
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003412 * wpa_sm_parse_own_wpa_ie - Parse own WPA/RSN IE
3413 * @sm: Pointer to WPA state machine data from wpa_sm_init()
3414 * @data: Pointer to data area for parsing results
3415 * Returns: 0 on success, -1 if IE is not known, or -2 on parsing failure
3416 *
3417 * Parse the contents of the own WPA or RSN IE from (Re)AssocReq and write the
3418 * parsed data into data.
3419 */
3420int wpa_sm_parse_own_wpa_ie(struct wpa_sm *sm, struct wpa_ie_data *data)
3421{
3422 if (sm == NULL)
3423 return -1;
3424
3425 if (sm->assoc_wpa_ie == NULL) {
3426 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
3427 "WPA: No WPA/RSN IE available from association info");
3428 return -1;
3429 }
3430 if (wpa_parse_wpa_ie(sm->assoc_wpa_ie, sm->assoc_wpa_ie_len, data))
3431 return -2;
3432 return 0;
3433}
3434
3435
3436int wpa_sm_pmksa_cache_list(struct wpa_sm *sm, char *buf, size_t len)
3437{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003438 return pmksa_cache_list(sm->pmksa, buf, len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003439}
3440
3441
Dmitry Shmidt29333592017-01-09 12:27:11 -08003442struct rsn_pmksa_cache_entry * wpa_sm_pmksa_cache_head(struct wpa_sm *sm)
3443{
3444 return pmksa_cache_head(sm->pmksa);
3445}
3446
3447
3448struct rsn_pmksa_cache_entry *
3449wpa_sm_pmksa_cache_add_entry(struct wpa_sm *sm,
3450 struct rsn_pmksa_cache_entry * entry)
3451{
3452 return pmksa_cache_add_entry(sm->pmksa, entry);
3453}
3454
3455
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003456void wpa_sm_pmksa_cache_add(struct wpa_sm *sm, const u8 *pmk, size_t pmk_len,
3457 const u8 *pmkid, const u8 *bssid,
3458 const u8 *fils_cache_id)
3459{
3460 sm->cur_pmksa = pmksa_cache_add(sm->pmksa, pmk, pmk_len, pmkid, NULL, 0,
3461 bssid, sm->own_addr, sm->network_ctx,
3462 sm->key_mgmt, fils_cache_id);
3463}
3464
3465
3466int wpa_sm_pmksa_exists(struct wpa_sm *sm, const u8 *bssid,
3467 const void *network_ctx)
3468{
Roshan Pius3a1667e2018-07-03 15:17:14 -07003469 return pmksa_cache_get(sm->pmksa, bssid, NULL, network_ctx, 0) != NULL;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003470}
3471
3472
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003473void wpa_sm_drop_sa(struct wpa_sm *sm)
3474{
3475 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Clear old PMK and PTK");
3476 sm->ptk_set = 0;
3477 sm->tptk_set = 0;
Roshan Pius3a1667e2018-07-03 15:17:14 -07003478 sm->pmk_len = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003479 os_memset(sm->pmk, 0, sizeof(sm->pmk));
3480 os_memset(&sm->ptk, 0, sizeof(sm->ptk));
3481 os_memset(&sm->tptk, 0, sizeof(sm->tptk));
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02003482 os_memset(&sm->gtk, 0, sizeof(sm->gtk));
Jouni Malinen58c0e962017-10-01 12:12:24 +03003483 os_memset(&sm->gtk_wnm_sleep, 0, sizeof(sm->gtk_wnm_sleep));
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02003484 os_memset(&sm->igtk, 0, sizeof(sm->igtk));
Jouni Malinen58c0e962017-10-01 12:12:24 +03003485 os_memset(&sm->igtk_wnm_sleep, 0, sizeof(sm->igtk_wnm_sleep));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003486#ifdef CONFIG_IEEE80211R
3487 os_memset(sm->xxkey, 0, sizeof(sm->xxkey));
Roshan Pius3a1667e2018-07-03 15:17:14 -07003488 sm->xxkey_len = 0;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003489 os_memset(sm->pmk_r0, 0, sizeof(sm->pmk_r0));
Roshan Pius3a1667e2018-07-03 15:17:14 -07003490 sm->pmk_r0_len = 0;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003491 os_memset(sm->pmk_r1, 0, sizeof(sm->pmk_r1));
Roshan Pius3a1667e2018-07-03 15:17:14 -07003492 sm->pmk_r1_len = 0;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003493#endif /* CONFIG_IEEE80211R */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003494}
3495
3496
3497int wpa_sm_has_ptk(struct wpa_sm *sm)
3498{
3499 if (sm == NULL)
3500 return 0;
3501 return sm->ptk_set;
3502}
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003503
3504
3505void wpa_sm_update_replay_ctr(struct wpa_sm *sm, const u8 *replay_ctr)
3506{
3507 os_memcpy(sm->rx_replay_counter, replay_ctr, WPA_REPLAY_COUNTER_LEN);
3508}
3509
3510
3511void wpa_sm_pmksa_cache_flush(struct wpa_sm *sm, void *network_ctx)
3512{
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07003513 pmksa_cache_flush(sm->pmksa, network_ctx, NULL, 0);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003514}
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003515
3516
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003517#ifdef CONFIG_WNM
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003518int wpa_wnmsleep_install_key(struct wpa_sm *sm, u8 subelem_id, u8 *buf)
3519{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003520 u16 keyinfo;
3521 u8 keylen; /* plaintext key len */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003522 u8 *key_rsc;
3523
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003524 if (subelem_id == WNM_SLEEP_SUBELEM_GTK) {
Dmitry Shmidt61593f02014-04-21 16:27:35 -07003525 struct wpa_gtk_data gd;
3526
3527 os_memset(&gd, 0, sizeof(gd));
3528 keylen = wpa_cipher_key_len(sm->group_cipher);
3529 gd.key_rsc_len = wpa_cipher_rsc_len(sm->group_cipher);
3530 gd.alg = wpa_cipher_to_alg(sm->group_cipher);
3531 if (gd.alg == WPA_ALG_NONE) {
3532 wpa_printf(MSG_DEBUG, "Unsupported group cipher suite");
3533 return -1;
3534 }
3535
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003536 key_rsc = buf + 5;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003537 keyinfo = WPA_GET_LE16(buf + 2);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003538 gd.gtk_len = keylen;
3539 if (gd.gtk_len != buf[4]) {
3540 wpa_printf(MSG_DEBUG, "GTK len mismatch len %d vs %d",
3541 gd.gtk_len, buf[4]);
3542 return -1;
3543 }
3544 gd.keyidx = keyinfo & 0x03; /* B0 - B1 */
3545 gd.tx = wpa_supplicant_gtk_tx_bit_workaround(
3546 sm, !!(keyinfo & WPA_KEY_INFO_TXRX));
3547
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003548 os_memcpy(gd.gtk, buf + 13, gd.gtk_len);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003549
3550 wpa_hexdump_key(MSG_DEBUG, "Install GTK (WNM SLEEP)",
3551 gd.gtk, gd.gtk_len);
Jouni Malinen58c0e962017-10-01 12:12:24 +03003552 if (wpa_supplicant_install_gtk(sm, &gd, key_rsc, 1)) {
Hai Shalom81f62d82019-07-22 12:10:00 -07003553 forced_memzero(&gd, sizeof(gd));
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003554 wpa_printf(MSG_DEBUG, "Failed to install the GTK in "
3555 "WNM mode");
3556 return -1;
3557 }
Hai Shalom81f62d82019-07-22 12:10:00 -07003558 forced_memzero(&gd, sizeof(gd));
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003559 } else if (subelem_id == WNM_SLEEP_SUBELEM_IGTK) {
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02003560 const struct wpa_igtk_kde *igtk;
Dmitry Shmidt61593f02014-04-21 16:27:35 -07003561
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02003562 igtk = (const struct wpa_igtk_kde *) (buf + 2);
Jouni Malinen58c0e962017-10-01 12:12:24 +03003563 if (wpa_supplicant_install_igtk(sm, igtk, 1) < 0)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003564 return -1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003565 } else {
3566 wpa_printf(MSG_DEBUG, "Unknown element id");
3567 return -1;
3568 }
3569
3570 return 0;
3571}
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003572#endif /* CONFIG_WNM */
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003573
3574
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003575#ifdef CONFIG_P2P
3576
3577int wpa_sm_get_p2p_ip_addr(struct wpa_sm *sm, u8 *buf)
3578{
3579 if (sm == NULL || WPA_GET_BE32(sm->p2p_ip_addr) == 0)
3580 return -1;
3581 os_memcpy(buf, sm->p2p_ip_addr, 3 * 4);
3582 return 0;
3583}
3584
3585#endif /* CONFIG_P2P */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003586
3587
3588void wpa_sm_set_rx_replay_ctr(struct wpa_sm *sm, const u8 *rx_replay_counter)
3589{
3590 if (rx_replay_counter == NULL)
3591 return;
3592
3593 os_memcpy(sm->rx_replay_counter, rx_replay_counter,
3594 WPA_REPLAY_COUNTER_LEN);
3595 sm->rx_replay_counter_set = 1;
3596 wpa_printf(MSG_DEBUG, "Updated key replay counter");
3597}
3598
3599
Dmitry Shmidt807291d2015-01-27 13:40:23 -08003600void wpa_sm_set_ptk_kck_kek(struct wpa_sm *sm,
3601 const u8 *ptk_kck, size_t ptk_kck_len,
3602 const u8 *ptk_kek, size_t ptk_kek_len)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003603{
Dmitry Shmidt807291d2015-01-27 13:40:23 -08003604 if (ptk_kck && ptk_kck_len <= WPA_KCK_MAX_LEN) {
3605 os_memcpy(sm->ptk.kck, ptk_kck, ptk_kck_len);
3606 sm->ptk.kck_len = ptk_kck_len;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003607 wpa_printf(MSG_DEBUG, "Updated PTK KCK");
3608 }
Dmitry Shmidt807291d2015-01-27 13:40:23 -08003609 if (ptk_kek && ptk_kek_len <= WPA_KEK_MAX_LEN) {
3610 os_memcpy(sm->ptk.kek, ptk_kek, ptk_kek_len);
3611 sm->ptk.kek_len = ptk_kek_len;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003612 wpa_printf(MSG_DEBUG, "Updated PTK KEK");
3613 }
3614 sm->ptk_set = 1;
3615}
Dmitry Shmidt55840ad2015-12-14 12:45:46 -08003616
3617
3618#ifdef CONFIG_TESTING_OPTIONS
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003619
Dmitry Shmidt55840ad2015-12-14 12:45:46 -08003620void wpa_sm_set_test_assoc_ie(struct wpa_sm *sm, struct wpabuf *buf)
3621{
3622 wpabuf_free(sm->test_assoc_ie);
3623 sm->test_assoc_ie = buf;
3624}
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003625
3626
3627const u8 * wpa_sm_get_anonce(struct wpa_sm *sm)
3628{
3629 return sm->anonce;
3630}
3631
Dmitry Shmidt55840ad2015-12-14 12:45:46 -08003632#endif /* CONFIG_TESTING_OPTIONS */
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003633
3634
Roshan Pius3a1667e2018-07-03 15:17:14 -07003635unsigned int wpa_sm_get_key_mgmt(struct wpa_sm *sm)
3636{
3637 return sm->key_mgmt;
3638}
3639
3640
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003641#ifdef CONFIG_FILS
3642
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003643struct wpabuf * fils_build_auth(struct wpa_sm *sm, int dh_group, const u8 *md)
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003644{
3645 struct wpabuf *buf = NULL;
3646 struct wpabuf *erp_msg;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003647 struct wpabuf *pub = NULL;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003648
3649 erp_msg = eapol_sm_build_erp_reauth_start(sm->eapol);
3650 if (!erp_msg && !sm->cur_pmksa) {
3651 wpa_printf(MSG_DEBUG,
3652 "FILS: Neither ERP EAP-Initiate/Re-auth nor PMKSA cache entry is available - skip FILS");
3653 goto fail;
3654 }
3655
3656 wpa_printf(MSG_DEBUG, "FILS: Try to use FILS (erp=%d pmksa_cache=%d)",
3657 erp_msg != NULL, sm->cur_pmksa != NULL);
3658
3659 sm->fils_completed = 0;
3660
3661 if (!sm->assoc_wpa_ie) {
3662 wpa_printf(MSG_INFO, "FILS: No own RSN IE set for FILS");
3663 goto fail;
3664 }
3665
3666 if (random_get_bytes(sm->fils_nonce, FILS_NONCE_LEN) < 0 ||
3667 random_get_bytes(sm->fils_session, FILS_SESSION_LEN) < 0)
3668 goto fail;
3669
3670 wpa_hexdump(MSG_DEBUG, "FILS: Generated FILS Nonce",
3671 sm->fils_nonce, FILS_NONCE_LEN);
3672 wpa_hexdump(MSG_DEBUG, "FILS: Generated FILS Session",
3673 sm->fils_session, FILS_SESSION_LEN);
3674
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003675#ifdef CONFIG_FILS_SK_PFS
3676 sm->fils_dh_group = dh_group;
3677 if (dh_group) {
3678 crypto_ecdh_deinit(sm->fils_ecdh);
3679 sm->fils_ecdh = crypto_ecdh_init(dh_group);
3680 if (!sm->fils_ecdh) {
3681 wpa_printf(MSG_INFO,
3682 "FILS: Could not initialize ECDH with group %d",
3683 dh_group);
3684 goto fail;
3685 }
3686 pub = crypto_ecdh_get_pubkey(sm->fils_ecdh, 1);
3687 if (!pub)
3688 goto fail;
3689 wpa_hexdump_buf(MSG_DEBUG, "FILS: Element (DH public key)",
3690 pub);
3691 sm->fils_dh_elem_len = wpabuf_len(pub);
3692 }
3693#endif /* CONFIG_FILS_SK_PFS */
3694
3695 buf = wpabuf_alloc(1000 + sm->assoc_wpa_ie_len +
3696 (pub ? wpabuf_len(pub) : 0));
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003697 if (!buf)
3698 goto fail;
3699
3700 /* Fields following the Authentication algorithm number field */
3701
3702 /* Authentication Transaction seq# */
3703 wpabuf_put_le16(buf, 1);
3704
3705 /* Status Code */
3706 wpabuf_put_le16(buf, WLAN_STATUS_SUCCESS);
3707
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003708 /* TODO: FILS PK */
3709#ifdef CONFIG_FILS_SK_PFS
3710 if (dh_group) {
3711 /* Finite Cyclic Group */
3712 wpabuf_put_le16(buf, dh_group);
3713 /* Element */
3714 wpabuf_put_buf(buf, pub);
3715 }
3716#endif /* CONFIG_FILS_SK_PFS */
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003717
3718 /* RSNE */
3719 wpa_hexdump(MSG_DEBUG, "FILS: RSNE in FILS Authentication frame",
3720 sm->assoc_wpa_ie, sm->assoc_wpa_ie_len);
3721 wpabuf_put_data(buf, sm->assoc_wpa_ie, sm->assoc_wpa_ie_len);
3722
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003723 if (md) {
3724 /* MDE when using FILS for FT initial association */
3725 struct rsn_mdie *mdie;
3726
3727 wpabuf_put_u8(buf, WLAN_EID_MOBILITY_DOMAIN);
3728 wpabuf_put_u8(buf, sizeof(*mdie));
3729 mdie = wpabuf_put(buf, sizeof(*mdie));
3730 os_memcpy(mdie->mobility_domain, md, MOBILITY_DOMAIN_ID_LEN);
3731 mdie->ft_capab = 0;
3732 }
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003733
3734 /* FILS Nonce */
3735 wpabuf_put_u8(buf, WLAN_EID_EXTENSION); /* Element ID */
3736 wpabuf_put_u8(buf, 1 + FILS_NONCE_LEN); /* Length */
3737 /* Element ID Extension */
3738 wpabuf_put_u8(buf, WLAN_EID_EXT_FILS_NONCE);
3739 wpabuf_put_data(buf, sm->fils_nonce, FILS_NONCE_LEN);
3740
3741 /* FILS Session */
3742 wpabuf_put_u8(buf, WLAN_EID_EXTENSION); /* Element ID */
3743 wpabuf_put_u8(buf, 1 + FILS_SESSION_LEN); /* Length */
3744 /* Element ID Extension */
3745 wpabuf_put_u8(buf, WLAN_EID_EXT_FILS_SESSION);
3746 wpabuf_put_data(buf, sm->fils_session, FILS_SESSION_LEN);
3747
3748 /* FILS Wrapped Data */
Paul Stewart092955c2017-02-06 09:13:09 -08003749 sm->fils_erp_pmkid_set = 0;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003750 if (erp_msg) {
3751 wpabuf_put_u8(buf, WLAN_EID_EXTENSION); /* Element ID */
3752 wpabuf_put_u8(buf, 1 + wpabuf_len(erp_msg)); /* Length */
3753 /* Element ID Extension */
3754 wpabuf_put_u8(buf, WLAN_EID_EXT_FILS_WRAPPED_DATA);
3755 wpabuf_put_buf(buf, erp_msg);
Paul Stewart092955c2017-02-06 09:13:09 -08003756 /* Calculate pending PMKID here so that we do not need to
3757 * maintain a copy of the EAP-Initiate/Reauth message. */
3758 if (fils_pmkid_erp(sm->key_mgmt, wpabuf_head(erp_msg),
3759 wpabuf_len(erp_msg),
3760 sm->fils_erp_pmkid) == 0)
3761 sm->fils_erp_pmkid_set = 1;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003762 }
3763
3764 wpa_hexdump_buf(MSG_DEBUG, "RSN: FILS fields for Authentication frame",
3765 buf);
3766
3767fail:
3768 wpabuf_free(erp_msg);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003769 wpabuf_free(pub);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003770 return buf;
3771}
3772
3773
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003774int fils_process_auth(struct wpa_sm *sm, const u8 *bssid, const u8 *data,
3775 size_t len)
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003776{
3777 const u8 *pos, *end;
3778 struct ieee802_11_elems elems;
3779 struct wpa_ie_data rsn;
3780 int pmkid_match = 0;
3781 u8 ick[FILS_ICK_MAX_LEN];
3782 size_t ick_len;
3783 int res;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003784 struct wpabuf *dh_ss = NULL;
3785 const u8 *g_sta = NULL;
3786 size_t g_sta_len = 0;
3787 const u8 *g_ap = NULL;
3788 size_t g_ap_len = 0;
3789 struct wpabuf *pub = NULL;
3790
3791 os_memcpy(sm->bssid, bssid, ETH_ALEN);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003792
3793 wpa_hexdump(MSG_DEBUG, "FILS: Authentication frame fields",
3794 data, len);
3795 pos = data;
3796 end = data + len;
3797
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003798 /* TODO: FILS PK */
3799#ifdef CONFIG_FILS_SK_PFS
3800 if (sm->fils_dh_group) {
3801 u16 group;
3802
3803 /* Using FILS PFS */
3804
3805 /* Finite Cyclic Group */
3806 if (end - pos < 2) {
3807 wpa_printf(MSG_DEBUG,
3808 "FILS: No room for Finite Cyclic Group");
3809 goto fail;
3810 }
3811 group = WPA_GET_LE16(pos);
3812 pos += 2;
3813 if (group != sm->fils_dh_group) {
3814 wpa_printf(MSG_DEBUG,
3815 "FILS: Unexpected change in Finite Cyclic Group: %u (expected %u)",
3816 group, sm->fils_dh_group);
3817 goto fail;
3818 }
3819
3820 /* Element */
3821 if ((size_t) (end - pos) < sm->fils_dh_elem_len) {
3822 wpa_printf(MSG_DEBUG, "FILS: No room for Element");
3823 goto fail;
3824 }
3825
3826 if (!sm->fils_ecdh) {
3827 wpa_printf(MSG_DEBUG, "FILS: No ECDH state available");
3828 goto fail;
3829 }
3830 dh_ss = crypto_ecdh_set_peerkey(sm->fils_ecdh, 1, pos,
3831 sm->fils_dh_elem_len);
3832 if (!dh_ss) {
3833 wpa_printf(MSG_DEBUG, "FILS: ECDH operation failed");
3834 goto fail;
3835 }
3836 wpa_hexdump_buf_key(MSG_DEBUG, "FILS: DH_SS", dh_ss);
3837 g_ap = pos;
3838 g_ap_len = sm->fils_dh_elem_len;
3839 pos += sm->fils_dh_elem_len;
3840 }
3841#endif /* CONFIG_FILS_SK_PFS */
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003842
3843 wpa_hexdump(MSG_DEBUG, "FILS: Remaining IEs", pos, end - pos);
3844 if (ieee802_11_parse_elems(pos, end - pos, &elems, 1) == ParseFailed) {
3845 wpa_printf(MSG_DEBUG, "FILS: Could not parse elements");
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003846 goto fail;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003847 }
3848
3849 /* RSNE */
3850 wpa_hexdump(MSG_DEBUG, "FILS: RSN element", elems.rsn_ie,
3851 elems.rsn_ie_len);
3852 if (!elems.rsn_ie ||
3853 wpa_parse_wpa_ie_rsn(elems.rsn_ie - 2, elems.rsn_ie_len + 2,
3854 &rsn) < 0) {
3855 wpa_printf(MSG_DEBUG, "FILS: No RSN element");
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003856 goto fail;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003857 }
3858
3859 if (!elems.fils_nonce) {
3860 wpa_printf(MSG_DEBUG, "FILS: No FILS Nonce field");
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003861 goto fail;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003862 }
3863 os_memcpy(sm->fils_anonce, elems.fils_nonce, FILS_NONCE_LEN);
3864 wpa_hexdump(MSG_DEBUG, "FILS: ANonce", sm->fils_anonce, FILS_NONCE_LEN);
3865
Roshan Pius3a1667e2018-07-03 15:17:14 -07003866#ifdef CONFIG_IEEE80211R
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003867 if (wpa_key_mgmt_ft(sm->key_mgmt)) {
3868 struct wpa_ft_ies parse;
3869
3870 if (!elems.mdie || !elems.ftie) {
3871 wpa_printf(MSG_DEBUG, "FILS+FT: No MDE or FTE");
3872 goto fail;
3873 }
3874
Roshan Pius3a1667e2018-07-03 15:17:14 -07003875 if (wpa_ft_parse_ies(pos, end - pos, &parse,
3876 wpa_key_mgmt_sha384(sm->key_mgmt)) < 0) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003877 wpa_printf(MSG_DEBUG, "FILS+FT: Failed to parse IEs");
3878 goto fail;
3879 }
3880
3881 if (!parse.r0kh_id) {
3882 wpa_printf(MSG_DEBUG,
3883 "FILS+FT: No R0KH-ID subelem in FTE");
3884 goto fail;
3885 }
3886 os_memcpy(sm->r0kh_id, parse.r0kh_id, parse.r0kh_id_len);
3887 sm->r0kh_id_len = parse.r0kh_id_len;
3888 wpa_hexdump_ascii(MSG_DEBUG, "FILS+FT: R0KH-ID",
3889 sm->r0kh_id, sm->r0kh_id_len);
3890
3891 if (!parse.r1kh_id) {
3892 wpa_printf(MSG_DEBUG,
3893 "FILS+FT: No R1KH-ID subelem in FTE");
3894 goto fail;
3895 }
3896 os_memcpy(sm->r1kh_id, parse.r1kh_id, FT_R1KH_ID_LEN);
3897 wpa_hexdump(MSG_DEBUG, "FILS+FT: R1KH-ID",
3898 sm->r1kh_id, FT_R1KH_ID_LEN);
3899
3900 /* TODO: Check MDE and FTE payload */
3901
3902 wpabuf_free(sm->fils_ft_ies);
3903 sm->fils_ft_ies = wpabuf_alloc(2 + elems.mdie_len +
3904 2 + elems.ftie_len);
3905 if (!sm->fils_ft_ies)
3906 goto fail;
3907 wpabuf_put_data(sm->fils_ft_ies, elems.mdie - 2,
3908 2 + elems.mdie_len);
3909 wpabuf_put_data(sm->fils_ft_ies, elems.ftie - 2,
3910 2 + elems.ftie_len);
3911 } else {
3912 wpabuf_free(sm->fils_ft_ies);
3913 sm->fils_ft_ies = NULL;
3914 }
Roshan Pius3a1667e2018-07-03 15:17:14 -07003915#endif /* CONFIG_IEEE80211R */
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003916
3917 /* PMKID List */
3918 if (rsn.pmkid && rsn.num_pmkid > 0) {
3919 wpa_hexdump(MSG_DEBUG, "FILS: PMKID List",
3920 rsn.pmkid, rsn.num_pmkid * PMKID_LEN);
3921
3922 if (rsn.num_pmkid != 1) {
3923 wpa_printf(MSG_DEBUG, "FILS: Invalid PMKID selection");
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003924 goto fail;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003925 }
3926 wpa_hexdump(MSG_DEBUG, "FILS: PMKID", rsn.pmkid, PMKID_LEN);
3927 if (os_memcmp(sm->cur_pmksa->pmkid, rsn.pmkid, PMKID_LEN) != 0)
3928 {
3929 wpa_printf(MSG_DEBUG, "FILS: PMKID mismatch");
3930 wpa_hexdump(MSG_DEBUG, "FILS: Expected PMKID",
3931 sm->cur_pmksa->pmkid, PMKID_LEN);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003932 goto fail;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003933 }
3934 wpa_printf(MSG_DEBUG,
3935 "FILS: Matching PMKID - continue using PMKSA caching");
3936 pmkid_match = 1;
3937 }
3938 if (!pmkid_match && sm->cur_pmksa) {
3939 wpa_printf(MSG_DEBUG,
3940 "FILS: No PMKID match - cannot use cached PMKSA entry");
3941 sm->cur_pmksa = NULL;
3942 }
3943
3944 /* FILS Session */
3945 if (!elems.fils_session) {
3946 wpa_printf(MSG_DEBUG, "FILS: No FILS Session element");
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003947 goto fail;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003948 }
3949 wpa_hexdump(MSG_DEBUG, "FILS: FILS Session", elems.fils_session,
3950 FILS_SESSION_LEN);
3951 if (os_memcmp(sm->fils_session, elems.fils_session, FILS_SESSION_LEN)
3952 != 0) {
3953 wpa_printf(MSG_DEBUG, "FILS: Session mismatch");
3954 wpa_hexdump(MSG_DEBUG, "FILS: Expected FILS Session",
3955 sm->fils_session, FILS_SESSION_LEN);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003956 goto fail;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003957 }
3958
3959 /* FILS Wrapped Data */
3960 if (!sm->cur_pmksa && elems.fils_wrapped_data) {
Paul Stewart092955c2017-02-06 09:13:09 -08003961 u8 rmsk[ERP_MAX_KEY_LEN];
3962 size_t rmsk_len;
3963
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003964 wpa_hexdump(MSG_DEBUG, "FILS: Wrapped Data",
3965 elems.fils_wrapped_data,
3966 elems.fils_wrapped_data_len);
3967 eapol_sm_process_erp_finish(sm->eapol, elems.fils_wrapped_data,
3968 elems.fils_wrapped_data_len);
3969 if (eapol_sm_failed(sm->eapol))
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003970 goto fail;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003971
Paul Stewart092955c2017-02-06 09:13:09 -08003972 rmsk_len = ERP_MAX_KEY_LEN;
3973 res = eapol_sm_get_key(sm->eapol, rmsk, rmsk_len);
3974 if (res == PMK_LEN) {
3975 rmsk_len = PMK_LEN;
3976 res = eapol_sm_get_key(sm->eapol, rmsk, rmsk_len);
3977 }
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003978 if (res)
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003979 goto fail;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003980
Paul Stewart092955c2017-02-06 09:13:09 -08003981 res = fils_rmsk_to_pmk(sm->key_mgmt, rmsk, rmsk_len,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003982 sm->fils_nonce, sm->fils_anonce,
3983 dh_ss ? wpabuf_head(dh_ss) : NULL,
3984 dh_ss ? wpabuf_len(dh_ss) : 0,
Paul Stewart092955c2017-02-06 09:13:09 -08003985 sm->pmk, &sm->pmk_len);
Hai Shalom81f62d82019-07-22 12:10:00 -07003986 forced_memzero(rmsk, sizeof(rmsk));
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003987
3988 /* Don't use DHss in PTK derivation if PMKSA caching is not
3989 * used. */
3990 wpabuf_clear_free(dh_ss);
3991 dh_ss = NULL;
3992
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08003993 if (res)
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003994 goto fail;
Paul Stewart092955c2017-02-06 09:13:09 -08003995
3996 if (!sm->fils_erp_pmkid_set) {
3997 wpa_printf(MSG_DEBUG, "FILS: PMKID not available");
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003998 goto fail;
Paul Stewart092955c2017-02-06 09:13:09 -08003999 }
4000 wpa_hexdump(MSG_DEBUG, "FILS: PMKID", sm->fils_erp_pmkid,
4001 PMKID_LEN);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004002 wpa_printf(MSG_DEBUG, "FILS: ERP processing succeeded - add PMKSA cache entry for the result");
Paul Stewart092955c2017-02-06 09:13:09 -08004003 sm->cur_pmksa = pmksa_cache_add(sm->pmksa, sm->pmk, sm->pmk_len,
4004 sm->fils_erp_pmkid, NULL, 0,
4005 sm->bssid, sm->own_addr,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004006 sm->network_ctx, sm->key_mgmt,
4007 NULL);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004008 }
4009
4010 if (!sm->cur_pmksa) {
4011 wpa_printf(MSG_DEBUG,
4012 "FILS: No remaining options to continue FILS authentication");
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004013 goto fail;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004014 }
4015
4016 if (fils_pmk_to_ptk(sm->pmk, sm->pmk_len, sm->own_addr, sm->bssid,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004017 sm->fils_nonce, sm->fils_anonce,
4018 dh_ss ? wpabuf_head(dh_ss) : NULL,
4019 dh_ss ? wpabuf_len(dh_ss) : 0,
4020 &sm->ptk, ick, &ick_len,
4021 sm->key_mgmt, sm->pairwise_cipher,
4022 sm->fils_ft, &sm->fils_ft_len) < 0) {
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004023 wpa_printf(MSG_DEBUG, "FILS: Failed to derive PTK");
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004024 goto fail;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004025 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004026
4027 wpabuf_clear_free(dh_ss);
4028 dh_ss = NULL;
4029
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004030 sm->ptk_set = 1;
4031 sm->tptk_set = 0;
4032 os_memset(&sm->tptk, 0, sizeof(sm->tptk));
4033
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004034#ifdef CONFIG_FILS_SK_PFS
4035 if (sm->fils_dh_group) {
4036 if (!sm->fils_ecdh) {
4037 wpa_printf(MSG_INFO, "FILS: ECDH not initialized");
4038 goto fail;
4039 }
4040 pub = crypto_ecdh_get_pubkey(sm->fils_ecdh, 1);
4041 if (!pub)
4042 goto fail;
4043 wpa_hexdump_buf(MSG_DEBUG, "FILS: gSTA", pub);
4044 g_sta = wpabuf_head(pub);
4045 g_sta_len = wpabuf_len(pub);
4046 if (!g_ap) {
4047 wpa_printf(MSG_INFO, "FILS: gAP not available");
4048 goto fail;
4049 }
4050 wpa_hexdump(MSG_DEBUG, "FILS: gAP", g_ap, g_ap_len);
4051 }
4052#endif /* CONFIG_FILS_SK_PFS */
4053
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004054 res = fils_key_auth_sk(ick, ick_len, sm->fils_nonce,
4055 sm->fils_anonce, sm->own_addr, sm->bssid,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004056 g_sta, g_sta_len, g_ap, g_ap_len,
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004057 sm->key_mgmt, sm->fils_key_auth_sta,
4058 sm->fils_key_auth_ap,
4059 &sm->fils_key_auth_len);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004060 wpabuf_free(pub);
Hai Shalom81f62d82019-07-22 12:10:00 -07004061 forced_memzero(ick, sizeof(ick));
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004062 return res;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004063fail:
4064 wpabuf_free(pub);
4065 wpabuf_clear_free(dh_ss);
4066 return -1;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004067}
4068
4069
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004070#ifdef CONFIG_IEEE80211R
4071static int fils_ft_build_assoc_req_rsne(struct wpa_sm *sm, struct wpabuf *buf)
4072{
4073 struct rsn_ie_hdr *rsnie;
4074 u16 capab;
4075 u8 *pos;
Roshan Pius3a1667e2018-07-03 15:17:14 -07004076 int use_sha384 = wpa_key_mgmt_sha384(sm->key_mgmt);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004077
4078 /* RSNIE[PMKR0Name/PMKR1Name] */
4079 rsnie = wpabuf_put(buf, sizeof(*rsnie));
4080 rsnie->elem_id = WLAN_EID_RSN;
4081 WPA_PUT_LE16(rsnie->version, RSN_VERSION);
4082
4083 /* Group Suite Selector */
4084 if (!wpa_cipher_valid_group(sm->group_cipher)) {
4085 wpa_printf(MSG_WARNING, "FT: Invalid group cipher (%d)",
4086 sm->group_cipher);
4087 return -1;
4088 }
4089 pos = wpabuf_put(buf, RSN_SELECTOR_LEN);
4090 RSN_SELECTOR_PUT(pos, wpa_cipher_to_suite(WPA_PROTO_RSN,
4091 sm->group_cipher));
4092
4093 /* Pairwise Suite Count */
4094 wpabuf_put_le16(buf, 1);
4095
4096 /* Pairwise Suite List */
4097 if (!wpa_cipher_valid_pairwise(sm->pairwise_cipher)) {
4098 wpa_printf(MSG_WARNING, "FT: Invalid pairwise cipher (%d)",
4099 sm->pairwise_cipher);
4100 return -1;
4101 }
4102 pos = wpabuf_put(buf, RSN_SELECTOR_LEN);
4103 RSN_SELECTOR_PUT(pos, wpa_cipher_to_suite(WPA_PROTO_RSN,
4104 sm->pairwise_cipher));
4105
4106 /* Authenticated Key Management Suite Count */
4107 wpabuf_put_le16(buf, 1);
4108
4109 /* Authenticated Key Management Suite List */
4110 pos = wpabuf_put(buf, RSN_SELECTOR_LEN);
4111 if (sm->key_mgmt == WPA_KEY_MGMT_FT_FILS_SHA256)
4112 RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_FT_FILS_SHA256);
4113 else if (sm->key_mgmt == WPA_KEY_MGMT_FT_FILS_SHA384)
4114 RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_FT_FILS_SHA384);
4115 else {
4116 wpa_printf(MSG_WARNING,
4117 "FILS+FT: Invalid key management type (%d)",
4118 sm->key_mgmt);
4119 return -1;
4120 }
4121
4122 /* RSN Capabilities */
4123 capab = 0;
Hai Shalomc3565922019-10-28 11:58:20 -07004124 if (sm->mfp)
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004125 capab |= WPA_CAPABILITY_MFPC;
Hai Shalomc3565922019-10-28 11:58:20 -07004126 if (sm->mfp == 2)
4127 capab |= WPA_CAPABILITY_MFPR;
Hai Shalom74f70d42019-02-11 14:42:39 -08004128 if (sm->ocv)
4129 capab |= WPA_CAPABILITY_OCVC;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004130 wpabuf_put_le16(buf, capab);
4131
4132 /* PMKID Count */
4133 wpabuf_put_le16(buf, 1);
4134
4135 /* PMKID List [PMKR1Name] */
4136 wpa_hexdump_key(MSG_DEBUG, "FILS+FT: XXKey (FILS-FT)",
4137 sm->fils_ft, sm->fils_ft_len);
4138 wpa_hexdump_ascii(MSG_DEBUG, "FILS+FT: SSID", sm->ssid, sm->ssid_len);
4139 wpa_hexdump(MSG_DEBUG, "FILS+FT: MDID",
4140 sm->mobility_domain, MOBILITY_DOMAIN_ID_LEN);
4141 wpa_hexdump_ascii(MSG_DEBUG, "FILS+FT: R0KH-ID",
4142 sm->r0kh_id, sm->r0kh_id_len);
4143 if (wpa_derive_pmk_r0(sm->fils_ft, sm->fils_ft_len, sm->ssid,
4144 sm->ssid_len, sm->mobility_domain,
4145 sm->r0kh_id, sm->r0kh_id_len, sm->own_addr,
Roshan Pius3a1667e2018-07-03 15:17:14 -07004146 sm->pmk_r0, sm->pmk_r0_name, use_sha384) < 0) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004147 wpa_printf(MSG_WARNING, "FILS+FT: Could not derive PMK-R0");
4148 return -1;
4149 }
Roshan Pius3a1667e2018-07-03 15:17:14 -07004150 sm->pmk_r0_len = use_sha384 ? SHA384_MAC_LEN : PMK_LEN;
4151 wpa_hexdump_key(MSG_DEBUG, "FILS+FT: PMK-R0",
4152 sm->pmk_r0, sm->pmk_r0_len);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004153 wpa_hexdump(MSG_DEBUG, "FILS+FT: PMKR0Name",
4154 sm->pmk_r0_name, WPA_PMK_NAME_LEN);
4155 wpa_printf(MSG_DEBUG, "FILS+FT: R1KH-ID: " MACSTR,
4156 MAC2STR(sm->r1kh_id));
4157 pos = wpabuf_put(buf, WPA_PMK_NAME_LEN);
4158 if (wpa_derive_pmk_r1_name(sm->pmk_r0_name, sm->r1kh_id, sm->own_addr,
Hai Shalom021b0b52019-04-10 11:17:58 -07004159 sm->pmk_r1_name, use_sha384) < 0) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004160 wpa_printf(MSG_WARNING, "FILS+FT: Could not derive PMKR1Name");
4161 return -1;
4162 }
Hai Shalom021b0b52019-04-10 11:17:58 -07004163 wpa_hexdump(MSG_DEBUG, "FILS+FT: PMKR1Name", sm->pmk_r1_name,
4164 WPA_PMK_NAME_LEN);
4165 os_memcpy(pos, sm->pmk_r1_name, WPA_PMK_NAME_LEN);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004166
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004167 if (sm->mgmt_group_cipher == WPA_CIPHER_AES_128_CMAC) {
4168 /* Management Group Cipher Suite */
4169 pos = wpabuf_put(buf, RSN_SELECTOR_LEN);
4170 RSN_SELECTOR_PUT(pos, RSN_CIPHER_SUITE_AES_128_CMAC);
4171 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004172
4173 rsnie->len = ((u8 *) wpabuf_put(buf, 0) - (u8 *) rsnie) - 2;
4174 return 0;
4175}
4176#endif /* CONFIG_IEEE80211R */
4177
4178
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004179struct wpabuf * fils_build_assoc_req(struct wpa_sm *sm, const u8 **kek,
4180 size_t *kek_len, const u8 **snonce,
Paul Stewart092955c2017-02-06 09:13:09 -08004181 const u8 **anonce,
4182 const struct wpabuf **hlp,
4183 unsigned int num_hlp)
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004184{
4185 struct wpabuf *buf;
Paul Stewart092955c2017-02-06 09:13:09 -08004186 size_t len;
4187 unsigned int i;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004188
Paul Stewart092955c2017-02-06 09:13:09 -08004189 len = 1000;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004190#ifdef CONFIG_IEEE80211R
4191 if (sm->fils_ft_ies)
4192 len += wpabuf_len(sm->fils_ft_ies);
4193 if (wpa_key_mgmt_ft(sm->key_mgmt))
4194 len += 256;
4195#endif /* CONFIG_IEEE80211R */
Paul Stewart092955c2017-02-06 09:13:09 -08004196 for (i = 0; hlp && i < num_hlp; i++)
4197 len += 10 + wpabuf_len(hlp[i]);
4198 buf = wpabuf_alloc(len);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004199 if (!buf)
4200 return NULL;
4201
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004202#ifdef CONFIG_IEEE80211R
4203 if (wpa_key_mgmt_ft(sm->key_mgmt) && sm->fils_ft_ies) {
4204 /* MDE and FTE when using FILS+FT */
4205 wpabuf_put_buf(buf, sm->fils_ft_ies);
4206 /* RSNE with PMKR1Name in PMKID field */
4207 if (fils_ft_build_assoc_req_rsne(sm, buf) < 0) {
4208 wpabuf_free(buf);
4209 return NULL;
4210 }
4211 }
4212#endif /* CONFIG_IEEE80211R */
4213
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004214 /* FILS Session */
4215 wpabuf_put_u8(buf, WLAN_EID_EXTENSION); /* Element ID */
4216 wpabuf_put_u8(buf, 1 + FILS_SESSION_LEN); /* Length */
4217 /* Element ID Extension */
4218 wpabuf_put_u8(buf, WLAN_EID_EXT_FILS_SESSION);
4219 wpabuf_put_data(buf, sm->fils_session, FILS_SESSION_LEN);
4220
4221 /* Everything after FILS Session element gets encrypted in the driver
4222 * with KEK. The buffer returned from here is the plaintext version. */
4223
4224 /* TODO: FILS Public Key */
4225
4226 /* FILS Key Confirm */
4227 wpabuf_put_u8(buf, WLAN_EID_EXTENSION); /* Element ID */
4228 wpabuf_put_u8(buf, 1 + sm->fils_key_auth_len); /* Length */
4229 /* Element ID Extension */
4230 wpabuf_put_u8(buf, WLAN_EID_EXT_FILS_KEY_CONFIRM);
4231 wpabuf_put_data(buf, sm->fils_key_auth_sta, sm->fils_key_auth_len);
4232
Paul Stewart092955c2017-02-06 09:13:09 -08004233 /* FILS HLP Container */
4234 for (i = 0; hlp && i < num_hlp; i++) {
4235 const u8 *pos = wpabuf_head(hlp[i]);
4236 size_t left = wpabuf_len(hlp[i]);
4237
4238 wpabuf_put_u8(buf, WLAN_EID_EXTENSION); /* Element ID */
4239 if (left <= 254)
4240 len = 1 + left;
4241 else
4242 len = 255;
4243 wpabuf_put_u8(buf, len); /* Length */
4244 /* Element ID Extension */
4245 wpabuf_put_u8(buf, WLAN_EID_EXT_FILS_HLP_CONTAINER);
4246 /* Destination MAC Address, Source MAC Address, HLP Packet.
4247 * HLP Packet is in MSDU format (i.e., included the LLC/SNAP
4248 * header when LPD is used). */
4249 wpabuf_put_data(buf, pos, len - 1);
4250 pos += len - 1;
4251 left -= len - 1;
4252 while (left) {
4253 wpabuf_put_u8(buf, WLAN_EID_FRAGMENT);
4254 len = left > 255 ? 255 : left;
4255 wpabuf_put_u8(buf, len);
4256 wpabuf_put_data(buf, pos, len);
4257 pos += len;
4258 left -= len;
4259 }
4260 }
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004261
4262 /* TODO: FILS IP Address Assignment */
4263
Hai Shalom74f70d42019-02-11 14:42:39 -08004264#ifdef CONFIG_OCV
4265 if (wpa_sm_ocv_enabled(sm)) {
4266 struct wpa_channel_info ci;
4267 u8 *pos;
4268
4269 if (wpa_sm_channel_info(sm, &ci) != 0) {
4270 wpa_printf(MSG_WARNING,
4271 "FILS: Failed to get channel info for OCI element");
4272 wpabuf_free(buf);
4273 return NULL;
4274 }
4275
4276 pos = wpabuf_put(buf, OCV_OCI_EXTENDED_LEN);
4277 if (ocv_insert_extended_oci(&ci, pos) < 0) {
4278 wpabuf_free(buf);
4279 return NULL;
4280 }
4281 }
4282#endif /* CONFIG_OCV */
4283
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004284 wpa_hexdump_buf(MSG_DEBUG, "FILS: Association Request plaintext", buf);
4285
4286 *kek = sm->ptk.kek;
4287 *kek_len = sm->ptk.kek_len;
4288 wpa_hexdump_key(MSG_DEBUG, "FILS: KEK for AEAD", *kek, *kek_len);
4289 *snonce = sm->fils_nonce;
4290 wpa_hexdump(MSG_DEBUG, "FILS: SNonce for AEAD AAD",
4291 *snonce, FILS_NONCE_LEN);
4292 *anonce = sm->fils_anonce;
4293 wpa_hexdump(MSG_DEBUG, "FILS: ANonce for AEAD AAD",
4294 *anonce, FILS_NONCE_LEN);
4295
4296 return buf;
4297}
4298
4299
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08004300static void fils_process_hlp_resp(struct wpa_sm *sm, const u8 *resp, size_t len)
4301{
4302 const u8 *pos, *end;
4303
4304 wpa_hexdump(MSG_MSGDUMP, "FILS: HLP response", resp, len);
4305 if (len < 2 * ETH_ALEN)
4306 return;
4307 pos = resp + 2 * ETH_ALEN;
4308 end = resp + len;
4309 if (end - pos >= 6 &&
4310 os_memcmp(pos, "\xaa\xaa\x03\x00\x00\x00", 6) == 0)
4311 pos += 6; /* Remove SNAP/LLC header */
4312 wpa_sm_fils_hlp_rx(sm, resp, resp + ETH_ALEN, pos, end - pos);
4313}
4314
4315
4316static void fils_process_hlp_container(struct wpa_sm *sm, const u8 *pos,
4317 size_t len)
4318{
4319 const u8 *end = pos + len;
4320 u8 *tmp, *tmp_pos;
4321
4322 /* Check if there are any FILS HLP Container elements */
4323 while (end - pos >= 2) {
4324 if (2 + pos[1] > end - pos)
4325 return;
4326 if (pos[0] == WLAN_EID_EXTENSION &&
4327 pos[1] >= 1 + 2 * ETH_ALEN &&
4328 pos[2] == WLAN_EID_EXT_FILS_HLP_CONTAINER)
4329 break;
4330 pos += 2 + pos[1];
4331 }
4332 if (end - pos < 2)
4333 return; /* No FILS HLP Container elements */
4334
4335 tmp = os_malloc(end - pos);
4336 if (!tmp)
4337 return;
4338
4339 while (end - pos >= 2) {
4340 if (2 + pos[1] > end - pos ||
4341 pos[0] != WLAN_EID_EXTENSION ||
4342 pos[1] < 1 + 2 * ETH_ALEN ||
4343 pos[2] != WLAN_EID_EXT_FILS_HLP_CONTAINER)
4344 break;
4345 tmp_pos = tmp;
4346 os_memcpy(tmp_pos, pos + 3, pos[1] - 1);
4347 tmp_pos += pos[1] - 1;
4348 pos += 2 + pos[1];
4349
4350 /* Add possible fragments */
4351 while (end - pos >= 2 && pos[0] == WLAN_EID_FRAGMENT &&
4352 2 + pos[1] <= end - pos) {
4353 os_memcpy(tmp_pos, pos + 2, pos[1]);
4354 tmp_pos += pos[1];
4355 pos += 2 + pos[1];
4356 }
4357
4358 fils_process_hlp_resp(sm, tmp, tmp_pos - tmp);
4359 }
4360
4361 os_free(tmp);
4362}
4363
4364
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004365int fils_process_assoc_resp(struct wpa_sm *sm, const u8 *resp, size_t len)
4366{
4367 const struct ieee80211_mgmt *mgmt;
4368 const u8 *end, *ie_start;
4369 struct ieee802_11_elems elems;
4370 int keylen, rsclen;
4371 enum wpa_alg alg;
4372 struct wpa_gtk_data gd;
4373 int maxkeylen;
4374 struct wpa_eapol_ie_parse kde;
4375
4376 if (!sm || !sm->ptk_set) {
4377 wpa_printf(MSG_DEBUG, "FILS: No KEK available");
4378 return -1;
4379 }
4380
4381 if (!wpa_key_mgmt_fils(sm->key_mgmt)) {
4382 wpa_printf(MSG_DEBUG, "FILS: Not a FILS AKM");
4383 return -1;
4384 }
4385
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004386 if (sm->fils_completed) {
4387 wpa_printf(MSG_DEBUG,
4388 "FILS: Association has already been completed for this FILS authentication - ignore unexpected retransmission");
4389 return -1;
4390 }
4391
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004392 wpa_hexdump(MSG_DEBUG, "FILS: (Re)Association Response frame",
4393 resp, len);
4394
4395 mgmt = (const struct ieee80211_mgmt *) resp;
4396 if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.assoc_resp))
4397 return -1;
4398
4399 end = resp + len;
4400 /* Same offset for Association Response and Reassociation Response */
4401 ie_start = mgmt->u.assoc_resp.variable;
4402
4403 if (ieee802_11_parse_elems(ie_start, end - ie_start, &elems, 1) ==
4404 ParseFailed) {
4405 wpa_printf(MSG_DEBUG,
4406 "FILS: Failed to parse decrypted elements");
4407 goto fail;
4408 }
4409
4410 if (!elems.fils_session) {
4411 wpa_printf(MSG_DEBUG, "FILS: No FILS Session element");
4412 return -1;
4413 }
4414 if (os_memcmp(elems.fils_session, sm->fils_session,
4415 FILS_SESSION_LEN) != 0) {
4416 wpa_printf(MSG_DEBUG, "FILS: FILS Session mismatch");
4417 wpa_hexdump(MSG_DEBUG, "FILS: Received FILS Session",
4418 elems.fils_session, FILS_SESSION_LEN);
4419 wpa_hexdump(MSG_DEBUG, "FILS: Expected FILS Session",
4420 sm->fils_session, FILS_SESSION_LEN);
4421 }
4422
Hai Shalom81f62d82019-07-22 12:10:00 -07004423 if (!elems.rsn_ie) {
4424 wpa_printf(MSG_DEBUG,
4425 "FILS: No RSNE in (Re)Association Response");
4426 /* As an interop workaround, allow this for now since IEEE Std
4427 * 802.11ai-2016 did not include all the needed changes to make
4428 * a FILS AP include RSNE in the frame. This workaround might
4429 * eventually be removed and replaced with rejection (goto fail)
4430 * to follow a strict interpretation of the standard. */
4431 } else if (wpa_compare_rsn_ie(wpa_key_mgmt_ft(sm->key_mgmt),
4432 sm->ap_rsn_ie, sm->ap_rsn_ie_len,
4433 elems.rsn_ie - 2, elems.rsn_ie_len + 2)) {
4434 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
4435 "FILS: RSNE mismatch between Beacon/Probe Response and (Re)Association Response");
4436 wpa_hexdump(MSG_DEBUG, "FILS: RSNE in Beacon/Probe Response",
4437 sm->ap_rsn_ie, sm->ap_rsn_ie_len);
4438 wpa_hexdump(MSG_DEBUG, "FILS: RSNE in (Re)Association Response",
4439 elems.rsn_ie, elems.rsn_ie_len);
4440 goto fail;
4441 }
4442
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004443 /* TODO: FILS Public Key */
4444
4445 if (!elems.fils_key_confirm) {
4446 wpa_printf(MSG_DEBUG, "FILS: No FILS Key Confirm element");
4447 goto fail;
4448 }
4449 if (elems.fils_key_confirm_len != sm->fils_key_auth_len) {
4450 wpa_printf(MSG_DEBUG,
4451 "FILS: Unexpected Key-Auth length %d (expected %d)",
4452 elems.fils_key_confirm_len,
4453 (int) sm->fils_key_auth_len);
4454 goto fail;
4455 }
4456 if (os_memcmp(elems.fils_key_confirm, sm->fils_key_auth_ap,
4457 sm->fils_key_auth_len) != 0) {
4458 wpa_printf(MSG_DEBUG, "FILS: Key-Auth mismatch");
4459 wpa_hexdump(MSG_DEBUG, "FILS: Received Key-Auth",
4460 elems.fils_key_confirm,
4461 elems.fils_key_confirm_len);
4462 wpa_hexdump(MSG_DEBUG, "FILS: Expected Key-Auth",
4463 sm->fils_key_auth_ap, sm->fils_key_auth_len);
4464 goto fail;
4465 }
4466
Hai Shalom74f70d42019-02-11 14:42:39 -08004467#ifdef CONFIG_OCV
4468 if (wpa_sm_ocv_enabled(sm)) {
4469 struct wpa_channel_info ci;
4470
4471 if (wpa_sm_channel_info(sm, &ci) != 0) {
4472 wpa_printf(MSG_WARNING,
4473 "Failed to get channel info to validate received OCI in FILS (Re)Association Response frame");
4474 goto fail;
4475 }
4476
4477 if (ocv_verify_tx_params(elems.oci, elems.oci_len, &ci,
4478 channel_width_to_int(ci.chanwidth),
4479 ci.seg1_idx) != 0) {
4480 wpa_printf(MSG_WARNING, "FILS: %s", ocv_errorstr);
4481 goto fail;
4482 }
4483 }
4484#endif /* CONFIG_OCV */
4485
Hai Shalom021b0b52019-04-10 11:17:58 -07004486#ifdef CONFIG_IEEE80211R
4487 if (wpa_key_mgmt_ft(sm->key_mgmt) && sm->fils_ft_ies) {
4488 struct wpa_ie_data rsn;
4489
4490 /* Check that PMKR1Name derived by the AP matches */
4491 if (!elems.rsn_ie ||
4492 wpa_parse_wpa_ie_rsn(elems.rsn_ie - 2, elems.rsn_ie_len + 2,
4493 &rsn) < 0 ||
4494 !rsn.pmkid || rsn.num_pmkid != 1 ||
4495 os_memcmp(rsn.pmkid, sm->pmk_r1_name,
4496 WPA_PMK_NAME_LEN) != 0) {
4497 wpa_printf(MSG_DEBUG,
4498 "FILS+FT: No RSNE[PMKR1Name] match in AssocResp");
4499 goto fail;
4500 }
4501 }
4502#endif /* CONFIG_IEEE80211R */
4503
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004504 /* Key Delivery */
4505 if (!elems.key_delivery) {
4506 wpa_printf(MSG_DEBUG, "FILS: No Key Delivery element");
4507 goto fail;
4508 }
4509
4510 /* Parse GTK and set the key to the driver */
4511 os_memset(&gd, 0, sizeof(gd));
4512 if (wpa_supplicant_parse_ies(elems.key_delivery + WPA_KEY_RSC_LEN,
4513 elems.key_delivery_len - WPA_KEY_RSC_LEN,
4514 &kde) < 0) {
4515 wpa_printf(MSG_DEBUG, "FILS: Failed to parse KDEs");
4516 goto fail;
4517 }
4518 if (!kde.gtk) {
4519 wpa_printf(MSG_DEBUG, "FILS: No GTK KDE");
4520 goto fail;
4521 }
4522 maxkeylen = gd.gtk_len = kde.gtk_len - 2;
4523 if (wpa_supplicant_check_group_cipher(sm, sm->group_cipher,
4524 gd.gtk_len, maxkeylen,
4525 &gd.key_rsc_len, &gd.alg))
4526 goto fail;
4527
4528 wpa_hexdump_key(MSG_DEBUG, "FILS: Received GTK", kde.gtk, kde.gtk_len);
4529 gd.keyidx = kde.gtk[0] & 0x3;
4530 gd.tx = wpa_supplicant_gtk_tx_bit_workaround(sm,
4531 !!(kde.gtk[0] & BIT(2)));
4532 if (kde.gtk_len - 2 > sizeof(gd.gtk)) {
4533 wpa_printf(MSG_DEBUG, "FILS: Too long GTK in GTK KDE (len=%lu)",
4534 (unsigned long) kde.gtk_len - 2);
4535 goto fail;
4536 }
4537 os_memcpy(gd.gtk, kde.gtk + 2, kde.gtk_len - 2);
4538
4539 wpa_printf(MSG_DEBUG, "FILS: Set GTK to driver");
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004540 if (wpa_supplicant_install_gtk(sm, &gd, elems.key_delivery, 0) < 0) {
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004541 wpa_printf(MSG_DEBUG, "FILS: Failed to set GTK");
4542 goto fail;
4543 }
4544
4545 if (ieee80211w_set_keys(sm, &kde) < 0) {
4546 wpa_printf(MSG_DEBUG, "FILS: Failed to set IGTK");
4547 goto fail;
4548 }
4549
4550 alg = wpa_cipher_to_alg(sm->pairwise_cipher);
4551 keylen = wpa_cipher_key_len(sm->pairwise_cipher);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004552 if (keylen <= 0 || (unsigned int) keylen != sm->ptk.tk_len) {
4553 wpa_printf(MSG_DEBUG, "FILS: TK length mismatch: %u != %lu",
4554 keylen, (long unsigned int) sm->ptk.tk_len);
4555 goto fail;
4556 }
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004557 rsclen = wpa_cipher_rsc_len(sm->pairwise_cipher);
4558 wpa_hexdump_key(MSG_DEBUG, "FILS: Set TK to driver",
4559 sm->ptk.tk, keylen);
4560 if (wpa_sm_set_key(sm, alg, sm->bssid, 0, 1, null_rsc, rsclen,
4561 sm->ptk.tk, keylen) < 0) {
4562 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
4563 "FILS: Failed to set PTK to the driver (alg=%d keylen=%d bssid="
4564 MACSTR ")",
4565 alg, keylen, MAC2STR(sm->bssid));
4566 goto fail;
4567 }
4568
4569 /* TODO: TK could be cleared after auth frame exchange now that driver
4570 * takes care of association frame encryption/decryption. */
4571 /* TK is not needed anymore in supplicant */
4572 os_memset(sm->ptk.tk, 0, WPA_TK_MAX_LEN);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004573 sm->ptk.tk_len = 0;
Mathy Vanhoefc66556c2017-09-29 04:22:51 +02004574 sm->ptk.installed = 1;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004575
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08004576 /* FILS HLP Container */
4577 fils_process_hlp_container(sm, ie_start, end - ie_start);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004578
4579 /* TODO: FILS IP Address Assignment */
4580
4581 wpa_printf(MSG_DEBUG, "FILS: Auth+Assoc completed successfully");
4582 sm->fils_completed = 1;
Hai Shalom81f62d82019-07-22 12:10:00 -07004583 forced_memzero(&gd, sizeof(gd));
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004584
4585 return 0;
4586fail:
Hai Shalom81f62d82019-07-22 12:10:00 -07004587 forced_memzero(&gd, sizeof(gd));
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004588 return -1;
4589}
4590
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004591
4592void wpa_sm_set_reset_fils_completed(struct wpa_sm *sm, int set)
4593{
4594 if (sm)
4595 sm->fils_completed = !!set;
4596}
4597
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004598#endif /* CONFIG_FILS */
4599
4600
4601int wpa_fils_is_completed(struct wpa_sm *sm)
4602{
4603#ifdef CONFIG_FILS
4604 return sm && sm->fils_completed;
4605#else /* CONFIG_FILS */
4606 return 0;
4607#endif /* CONFIG_FILS */
4608}
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004609
4610
4611#ifdef CONFIG_OWE
4612
4613struct wpabuf * owe_build_assoc_req(struct wpa_sm *sm, u16 group)
4614{
4615 struct wpabuf *ie = NULL, *pub = NULL;
4616 size_t prime_len;
4617
4618 if (group == 19)
4619 prime_len = 32;
4620 else if (group == 20)
4621 prime_len = 48;
4622 else if (group == 21)
4623 prime_len = 66;
4624 else
4625 return NULL;
4626
4627 crypto_ecdh_deinit(sm->owe_ecdh);
4628 sm->owe_ecdh = crypto_ecdh_init(group);
4629 if (!sm->owe_ecdh)
4630 goto fail;
4631 sm->owe_group = group;
4632 pub = crypto_ecdh_get_pubkey(sm->owe_ecdh, 0);
4633 pub = wpabuf_zeropad(pub, prime_len);
4634 if (!pub)
4635 goto fail;
4636
4637 ie = wpabuf_alloc(5 + wpabuf_len(pub));
4638 if (!ie)
4639 goto fail;
4640 wpabuf_put_u8(ie, WLAN_EID_EXTENSION);
4641 wpabuf_put_u8(ie, 1 + 2 + wpabuf_len(pub));
4642 wpabuf_put_u8(ie, WLAN_EID_EXT_OWE_DH_PARAM);
4643 wpabuf_put_le16(ie, group);
4644 wpabuf_put_buf(ie, pub);
4645 wpabuf_free(pub);
4646 wpa_hexdump_buf(MSG_DEBUG, "OWE: Diffie-Hellman Parameter element",
4647 ie);
4648
4649 return ie;
4650fail:
4651 wpabuf_free(pub);
4652 crypto_ecdh_deinit(sm->owe_ecdh);
4653 sm->owe_ecdh = NULL;
4654 return NULL;
4655}
4656
4657
4658int owe_process_assoc_resp(struct wpa_sm *sm, const u8 *bssid,
4659 const u8 *resp_ies, size_t resp_ies_len)
4660{
4661 struct ieee802_11_elems elems;
4662 u16 group;
4663 struct wpabuf *secret, *pub, *hkey;
4664 int res;
4665 u8 prk[SHA512_MAC_LEN], pmkid[SHA512_MAC_LEN];
4666 const char *info = "OWE Key Generation";
4667 const u8 *addr[2];
4668 size_t len[2];
4669 size_t hash_len, prime_len;
4670 struct wpa_ie_data data;
4671
4672 if (!resp_ies ||
4673 ieee802_11_parse_elems(resp_ies, resp_ies_len, &elems, 1) ==
4674 ParseFailed) {
4675 wpa_printf(MSG_INFO,
4676 "OWE: Could not parse Association Response frame elements");
4677 return -1;
4678 }
4679
4680 if (sm->cur_pmksa && elems.rsn_ie &&
4681 wpa_parse_wpa_ie_rsn(elems.rsn_ie - 2, 2 + elems.rsn_ie_len,
4682 &data) == 0 &&
4683 data.num_pmkid == 1 && data.pmkid &&
4684 os_memcmp(sm->cur_pmksa->pmkid, data.pmkid, PMKID_LEN) == 0) {
4685 wpa_printf(MSG_DEBUG, "OWE: Use PMKSA caching");
4686 wpa_sm_set_pmk_from_pmksa(sm);
4687 return 0;
4688 }
4689
4690 if (!elems.owe_dh) {
4691 wpa_printf(MSG_INFO,
4692 "OWE: No Diffie-Hellman Parameter element found in Association Response frame");
4693 return -1;
4694 }
4695
4696 group = WPA_GET_LE16(elems.owe_dh);
4697 if (group != sm->owe_group) {
4698 wpa_printf(MSG_INFO,
4699 "OWE: Unexpected Diffie-Hellman group in response: %u",
4700 group);
4701 return -1;
4702 }
4703
4704 if (!sm->owe_ecdh) {
4705 wpa_printf(MSG_INFO, "OWE: No ECDH state available");
4706 return -1;
4707 }
4708
4709 if (group == 19)
4710 prime_len = 32;
4711 else if (group == 20)
4712 prime_len = 48;
4713 else if (group == 21)
4714 prime_len = 66;
4715 else
4716 return -1;
4717
4718 secret = crypto_ecdh_set_peerkey(sm->owe_ecdh, 0,
4719 elems.owe_dh + 2,
4720 elems.owe_dh_len - 2);
4721 secret = wpabuf_zeropad(secret, prime_len);
4722 if (!secret) {
4723 wpa_printf(MSG_DEBUG, "OWE: Invalid peer DH public key");
4724 return -1;
4725 }
4726 wpa_hexdump_buf_key(MSG_DEBUG, "OWE: DH shared secret", secret);
4727
4728 /* prk = HKDF-extract(C | A | group, z) */
4729
4730 pub = crypto_ecdh_get_pubkey(sm->owe_ecdh, 0);
4731 if (!pub) {
4732 wpabuf_clear_free(secret);
4733 return -1;
4734 }
4735
4736 /* PMKID = Truncate-128(Hash(C | A)) */
4737 addr[0] = wpabuf_head(pub);
4738 len[0] = wpabuf_len(pub);
4739 addr[1] = elems.owe_dh + 2;
4740 len[1] = elems.owe_dh_len - 2;
4741 if (group == 19) {
4742 res = sha256_vector(2, addr, len, pmkid);
4743 hash_len = SHA256_MAC_LEN;
4744 } else if (group == 20) {
4745 res = sha384_vector(2, addr, len, pmkid);
4746 hash_len = SHA384_MAC_LEN;
4747 } else if (group == 21) {
4748 res = sha512_vector(2, addr, len, pmkid);
4749 hash_len = SHA512_MAC_LEN;
4750 } else {
4751 res = -1;
4752 hash_len = 0;
4753 }
4754 pub = wpabuf_zeropad(pub, prime_len);
4755 if (res < 0 || !pub) {
4756 wpabuf_free(pub);
4757 wpabuf_clear_free(secret);
4758 return -1;
4759 }
4760
4761 hkey = wpabuf_alloc(wpabuf_len(pub) + elems.owe_dh_len - 2 + 2);
4762 if (!hkey) {
4763 wpabuf_free(pub);
4764 wpabuf_clear_free(secret);
4765 return -1;
4766 }
4767
4768 wpabuf_put_buf(hkey, pub); /* C */
4769 wpabuf_free(pub);
4770 wpabuf_put_data(hkey, elems.owe_dh + 2, elems.owe_dh_len - 2); /* A */
4771 wpabuf_put_le16(hkey, sm->owe_group); /* group */
4772 if (group == 19)
4773 res = hmac_sha256(wpabuf_head(hkey), wpabuf_len(hkey),
4774 wpabuf_head(secret), wpabuf_len(secret), prk);
4775 else if (group == 20)
4776 res = hmac_sha384(wpabuf_head(hkey), wpabuf_len(hkey),
4777 wpabuf_head(secret), wpabuf_len(secret), prk);
4778 else if (group == 21)
4779 res = hmac_sha512(wpabuf_head(hkey), wpabuf_len(hkey),
4780 wpabuf_head(secret), wpabuf_len(secret), prk);
4781 wpabuf_clear_free(hkey);
4782 wpabuf_clear_free(secret);
4783 if (res < 0)
4784 return -1;
4785
4786 wpa_hexdump_key(MSG_DEBUG, "OWE: prk", prk, hash_len);
4787
4788 /* PMK = HKDF-expand(prk, "OWE Key Generation", n) */
4789
4790 if (group == 19)
4791 res = hmac_sha256_kdf(prk, hash_len, NULL, (const u8 *) info,
4792 os_strlen(info), sm->pmk, hash_len);
4793 else if (group == 20)
4794 res = hmac_sha384_kdf(prk, hash_len, NULL, (const u8 *) info,
4795 os_strlen(info), sm->pmk, hash_len);
4796 else if (group == 21)
4797 res = hmac_sha512_kdf(prk, hash_len, NULL, (const u8 *) info,
4798 os_strlen(info), sm->pmk, hash_len);
Hai Shalom81f62d82019-07-22 12:10:00 -07004799 forced_memzero(prk, SHA512_MAC_LEN);
Roshan Pius3a1667e2018-07-03 15:17:14 -07004800 if (res < 0) {
4801 sm->pmk_len = 0;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004802 return -1;
Roshan Pius3a1667e2018-07-03 15:17:14 -07004803 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004804 sm->pmk_len = hash_len;
4805
4806 wpa_hexdump_key(MSG_DEBUG, "OWE: PMK", sm->pmk, sm->pmk_len);
4807 wpa_hexdump(MSG_DEBUG, "OWE: PMKID", pmkid, PMKID_LEN);
4808 pmksa_cache_add(sm->pmksa, sm->pmk, sm->pmk_len, pmkid, NULL, 0,
4809 bssid, sm->own_addr, sm->network_ctx, sm->key_mgmt,
4810 NULL);
4811
4812 return 0;
4813}
4814
4815#endif /* CONFIG_OWE */
4816
4817
4818void wpa_sm_set_fils_cache_id(struct wpa_sm *sm, const u8 *fils_cache_id)
4819{
4820#ifdef CONFIG_FILS
4821 if (sm && fils_cache_id) {
4822 sm->fils_cache_id_set = 1;
4823 os_memcpy(sm->fils_cache_id, fils_cache_id, FILS_CACHE_ID_LEN);
4824 }
4825#endif /* CONFIG_FILS */
4826}
Hai Shalom021b0b52019-04-10 11:17:58 -07004827
4828
4829#ifdef CONFIG_DPP2
4830void wpa_sm_set_dpp_z(struct wpa_sm *sm, const struct wpabuf *z)
4831{
4832 if (sm) {
4833 wpabuf_clear_free(sm->dpp_z);
4834 sm->dpp_z = z ? wpabuf_dup(z) : NULL;
4835 }
4836}
4837#endif /* CONFIG_DPP2 */