blob: 52aa766ed82c5d2d268dd40e105fe46c77414615 [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;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800308
Dmitry Shmidtebd93af2017-02-21 13:40:44 -0800309 if (wpa_key_mgmt_sha384(sm->key_mgmt))
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800310 pmk_len = PMK_LEN_SUITE_B_192;
311 else
312 pmk_len = PMK_LEN;
313 res = eapol_sm_get_key(sm->eapol, sm->pmk, pmk_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700314 if (res) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800315 if (pmk_len == PMK_LEN) {
316 /*
317 * EAP-LEAP is an exception from other EAP
318 * methods: it uses only 16-byte PMK.
319 */
320 res = eapol_sm_get_key(sm->eapol, sm->pmk, 16);
321 pmk_len = 16;
322 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700323 } else {
324#ifdef CONFIG_IEEE80211R
325 u8 buf[2 * PMK_LEN];
326 if (eapol_sm_get_key(sm->eapol, buf, 2 * PMK_LEN) == 0)
327 {
Roshan Pius3a1667e2018-07-03 15:17:14 -0700328 if (wpa_key_mgmt_sha384(sm->key_mgmt)) {
329 os_memcpy(sm->xxkey, buf,
330 SHA384_MAC_LEN);
331 sm->xxkey_len = SHA384_MAC_LEN;
332 } else {
333 os_memcpy(sm->xxkey, buf + PMK_LEN,
334 PMK_LEN);
335 sm->xxkey_len = PMK_LEN;
336 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700337 os_memset(buf, 0, sizeof(buf));
338 }
339#endif /* CONFIG_IEEE80211R */
340 }
341 if (res == 0) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700342 struct rsn_pmksa_cache_entry *sa = NULL;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700343 const u8 *fils_cache_id = NULL;
344
345#ifdef CONFIG_FILS
346 if (sm->fils_cache_id_set)
347 fils_cache_id = sm->fils_cache_id;
348#endif /* CONFIG_FILS */
349
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700350 wpa_hexdump_key(MSG_DEBUG, "WPA: PMK from EAPOL state "
351 "machines", sm->pmk, pmk_len);
352 sm->pmk_len = pmk_len;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800353 wpa_supplicant_key_mgmt_set_pmk(sm);
Dmitry Shmidtc55524a2011-07-07 11:18:38 -0700354 if (sm->proto == WPA_PROTO_RSN &&
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800355 !wpa_key_mgmt_suite_b(sm->key_mgmt) &&
Dmitry Shmidtc55524a2011-07-07 11:18:38 -0700356 !wpa_key_mgmt_ft(sm->key_mgmt)) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700357 sa = pmksa_cache_add(sm->pmksa,
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800358 sm->pmk, pmk_len, NULL,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800359 NULL, 0,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700360 src_addr, sm->own_addr,
361 sm->network_ctx,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700362 sm->key_mgmt,
363 fils_cache_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700364 }
365 if (!sm->cur_pmksa && pmkid &&
Roshan Pius3a1667e2018-07-03 15:17:14 -0700366 pmksa_cache_get(sm->pmksa, src_addr, pmkid, NULL,
367 0)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700368 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
369 "RSN: the new PMK matches with the "
370 "PMKID");
371 abort_cached = 0;
Jouni Malinen6ec30382015-07-08 20:48:18 +0300372 } else if (sa && !sm->cur_pmksa && pmkid) {
373 /*
374 * It looks like the authentication server
375 * derived mismatching MSK. This should not
376 * really happen, but bugs happen.. There is not
377 * much we can do here without knowing what
378 * exactly caused the server to misbehave.
379 */
Dmitry Shmidt014a3ff2015-12-28 13:27:49 -0800380 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
Jouni Malinen6ec30382015-07-08 20:48:18 +0300381 "RSN: PMKID mismatch - authentication server may have derived different MSK?!");
382 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700383 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700384
385 if (!sm->cur_pmksa)
386 sm->cur_pmksa = sa;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700387 } else {
388 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
389 "WPA: Failed to get master session key from "
390 "EAPOL state machines - key handshake "
391 "aborted");
392 if (sm->cur_pmksa) {
393 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
394 "RSN: Cancelled PMKSA caching "
395 "attempt");
396 sm->cur_pmksa = NULL;
397 abort_cached = 1;
398 } else if (!abort_cached) {
399 return -1;
400 }
401 }
402 }
403
Dmitry Shmidtc55524a2011-07-07 11:18:38 -0700404 if (abort_cached && wpa_key_mgmt_wpa_ieee8021x(sm->key_mgmt) &&
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800405 !wpa_key_mgmt_suite_b(sm->key_mgmt) &&
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800406 !wpa_key_mgmt_ft(sm->key_mgmt) && sm->key_mgmt != WPA_KEY_MGMT_OSEN)
407 {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700408 /* Send EAPOL-Start to trigger full EAP authentication. */
409 u8 *buf;
410 size_t buflen;
411
412 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
413 "RSN: no PMKSA entry found - trigger "
414 "full EAP authentication");
415 buf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_START,
416 NULL, 0, &buflen, NULL);
417 if (buf) {
418 wpa_sm_ether_send(sm, sm->bssid, ETH_P_EAPOL,
419 buf, buflen);
420 os_free(buf);
421 return -2;
422 }
423
424 return -1;
425 }
426
427 return 0;
428}
429
430
431/**
432 * wpa_supplicant_send_2_of_4 - Send message 2 of WPA/RSN 4-Way Handshake
433 * @sm: Pointer to WPA state machine data from wpa_sm_init()
434 * @dst: Destination address for the frame
435 * @key: Pointer to the EAPOL-Key frame header
436 * @ver: Version bits from EAPOL-Key Key Info
437 * @nonce: Nonce value for the EAPOL-Key frame
438 * @wpa_ie: WPA/RSN IE
439 * @wpa_ie_len: Length of the WPA/RSN IE
440 * @ptk: PTK to use for keyed hash and encryption
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800441 * Returns: >= 0 on success, < 0 on failure
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700442 */
443int wpa_supplicant_send_2_of_4(struct wpa_sm *sm, const unsigned char *dst,
444 const struct wpa_eapol_key *key,
445 int ver, const u8 *nonce,
446 const u8 *wpa_ie, size_t wpa_ie_len,
447 struct wpa_ptk *ptk)
448{
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800449 size_t mic_len, hdrlen, rlen;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700450 struct wpa_eapol_key *reply;
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800451 u8 *rbuf, *key_mic;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700452 u8 *rsn_ie_buf = NULL;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800453 u16 key_info;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700454
455 if (wpa_ie == NULL) {
456 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, "WPA: No wpa_ie set - "
457 "cannot generate msg 2/4");
458 return -1;
459 }
460
461#ifdef CONFIG_IEEE80211R
462 if (wpa_key_mgmt_ft(sm->key_mgmt)) {
463 int res;
464
465 /*
466 * Add PMKR1Name into RSN IE (PMKID-List) and add MDIE and
467 * FTIE from (Re)Association Response.
468 */
469 rsn_ie_buf = os_malloc(wpa_ie_len + 2 + 2 + PMKID_LEN +
470 sm->assoc_resp_ies_len);
471 if (rsn_ie_buf == NULL)
472 return -1;
473 os_memcpy(rsn_ie_buf, wpa_ie, wpa_ie_len);
Dmitry Shmidt55840ad2015-12-14 12:45:46 -0800474 res = wpa_insert_pmkid(rsn_ie_buf, &wpa_ie_len,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700475 sm->pmk_r1_name);
476 if (res < 0) {
477 os_free(rsn_ie_buf);
478 return -1;
479 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700480
481 if (sm->assoc_resp_ies) {
482 os_memcpy(rsn_ie_buf + wpa_ie_len, sm->assoc_resp_ies,
483 sm->assoc_resp_ies_len);
484 wpa_ie_len += sm->assoc_resp_ies_len;
485 }
486
487 wpa_ie = rsn_ie_buf;
488 }
489#endif /* CONFIG_IEEE80211R */
490
491 wpa_hexdump(MSG_DEBUG, "WPA: WPA IE for msg 2/4", wpa_ie, wpa_ie_len);
492
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700493 mic_len = wpa_mic_len(sm->key_mgmt, sm->pmk_len);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800494 hdrlen = sizeof(*reply) + mic_len + 2;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700495 rbuf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_KEY,
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800496 NULL, hdrlen + wpa_ie_len,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700497 &rlen, (void *) &reply);
498 if (rbuf == NULL) {
499 os_free(rsn_ie_buf);
500 return -1;
501 }
502
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800503 reply->type = (sm->proto == WPA_PROTO_RSN ||
504 sm->proto == WPA_PROTO_OSEN) ?
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700505 EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800506 key_info = ver | WPA_KEY_INFO_KEY_TYPE;
507 if (mic_len)
508 key_info |= WPA_KEY_INFO_MIC;
509 else
510 key_info |= WPA_KEY_INFO_ENCR_KEY_DATA;
511 WPA_PUT_BE16(reply->key_info, key_info);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800512 if (sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700513 WPA_PUT_BE16(reply->key_length, 0);
514 else
515 os_memcpy(reply->key_length, key->key_length, 2);
516 os_memcpy(reply->replay_counter, key->replay_counter,
517 WPA_REPLAY_COUNTER_LEN);
518 wpa_hexdump(MSG_DEBUG, "WPA: Replay Counter", reply->replay_counter,
519 WPA_REPLAY_COUNTER_LEN);
520
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800521 key_mic = (u8 *) (reply + 1);
522 WPA_PUT_BE16(key_mic + mic_len, wpa_ie_len); /* Key Data Length */
523 os_memcpy(key_mic + mic_len + 2, wpa_ie, wpa_ie_len); /* Key Data */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700524 os_free(rsn_ie_buf);
525
526 os_memcpy(reply->key_nonce, nonce, WPA_NONCE_LEN);
527
Roshan Pius5e7db942018-04-12 12:27:41 -0700528 wpa_dbg(sm->ctx->msg_ctx, MSG_INFO, "WPA: Sending EAPOL-Key 2/4");
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800529 return wpa_eapol_key_send(sm, ptk, ver, dst, ETH_P_EAPOL, rbuf, rlen,
530 key_mic);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700531}
532
533
534static int wpa_derive_ptk(struct wpa_sm *sm, const unsigned char *src_addr,
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800535 const struct wpa_eapol_key *key, struct wpa_ptk *ptk)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700536{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700537#ifdef CONFIG_IEEE80211R
538 if (wpa_key_mgmt_ft(sm->key_mgmt))
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800539 return wpa_derive_ptk_ft(sm, src_addr, key, ptk);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700540#endif /* CONFIG_IEEE80211R */
541
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800542 return wpa_pmk_to_ptk(sm->pmk, sm->pmk_len, "Pairwise key expansion",
543 sm->own_addr, sm->bssid, sm->snonce,
544 key->key_nonce, ptk, sm->key_mgmt,
545 sm->pairwise_cipher);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700546}
547
548
549static void wpa_supplicant_process_1_of_4(struct wpa_sm *sm,
550 const unsigned char *src_addr,
551 const struct wpa_eapol_key *key,
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700552 u16 ver, const u8 *key_data,
553 size_t key_data_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700554{
555 struct wpa_eapol_ie_parse ie;
556 struct wpa_ptk *ptk;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700557 int res;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800558 u8 *kde, *kde_buf = NULL;
559 size_t kde_len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700560
561 if (wpa_sm_get_network_ctx(sm) == NULL) {
562 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, "WPA: No SSID info "
563 "found (msg 1 of 4)");
564 return;
565 }
566
567 wpa_sm_set_state(sm, WPA_4WAY_HANDSHAKE);
Roshan Pius5e7db942018-04-12 12:27:41 -0700568 wpa_dbg(sm->ctx->msg_ctx, MSG_INFO, "WPA: RX message 1 of 4-Way "
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700569 "Handshake from " MACSTR " (ver=%d)", MAC2STR(src_addr), ver);
570
571 os_memset(&ie, 0, sizeof(ie));
572
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800573 if (sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700574 /* RSN: msg 1/4 should contain PMKID for the selected PMK */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700575 wpa_hexdump(MSG_DEBUG, "RSN: msg 1/4 key data",
576 key_data, key_data_len);
577 if (wpa_supplicant_parse_ies(key_data, key_data_len, &ie) < 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800578 goto failed;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700579 if (ie.pmkid) {
580 wpa_hexdump(MSG_DEBUG, "RSN: PMKID from "
581 "Authenticator", ie.pmkid, PMKID_LEN);
582 }
583 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700584
585 res = wpa_supplicant_get_pmk(sm, src_addr, ie.pmkid);
586 if (res == -2) {
587 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "RSN: Do not reply to "
588 "msg 1/4 - requesting full EAP authentication");
589 return;
590 }
591 if (res)
592 goto failed;
593
594 if (sm->renew_snonce) {
595 if (random_get_bytes(sm->snonce, WPA_NONCE_LEN)) {
596 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
597 "WPA: Failed to get random data for SNonce");
598 goto failed;
599 }
600 sm->renew_snonce = 0;
601 wpa_hexdump(MSG_DEBUG, "WPA: Renewed SNonce",
602 sm->snonce, WPA_NONCE_LEN);
603 }
604
605 /* Calculate PTK which will be stored as a temporary PTK until it has
606 * been verified when processing message 3/4. */
607 ptk = &sm->tptk;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700608 if (wpa_derive_ptk(sm, src_addr, key, ptk) < 0)
609 goto failed;
Dmitry Shmidt98660862014-03-11 17:26:21 -0700610 if (sm->pairwise_cipher == WPA_CIPHER_TKIP) {
Dmitry Shmidt61593f02014-04-21 16:27:35 -0700611 u8 buf[8];
Dmitry Shmidt98660862014-03-11 17:26:21 -0700612 /* Supplicant: swap tx/rx Mic keys */
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800613 os_memcpy(buf, &ptk->tk[16], 8);
614 os_memcpy(&ptk->tk[16], &ptk->tk[24], 8);
615 os_memcpy(&ptk->tk[24], buf, 8);
Dmitry Shmidt61593f02014-04-21 16:27:35 -0700616 os_memset(buf, 0, sizeof(buf));
Dmitry Shmidt98660862014-03-11 17:26:21 -0700617 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700618 sm->tptk_set = 1;
619
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800620 kde = sm->assoc_wpa_ie;
621 kde_len = sm->assoc_wpa_ie_len;
622
Hai Shalom74f70d42019-02-11 14:42:39 -0800623#ifdef CONFIG_OCV
624 if (wpa_sm_ocv_enabled(sm)) {
625 struct wpa_channel_info ci;
626 u8 *pos;
627
628 if (wpa_sm_channel_info(sm, &ci) != 0) {
629 wpa_printf(MSG_WARNING,
630 "Failed to get channel info for OCI element in EAPOL-Key 2/4");
631 goto failed;
632 }
633
634 kde_buf = os_malloc(kde_len + 2 + RSN_SELECTOR_LEN + 3);
635 if (!kde_buf) {
636 wpa_printf(MSG_WARNING,
637 "Failed to allocate memory for KDE with OCI in EAPOL-Key 2/4");
638 goto failed;
639 }
640
641 os_memcpy(kde_buf, kde, kde_len);
642 kde = kde_buf;
643 pos = kde + kde_len;
644 if (ocv_insert_oci_kde(&ci, &pos) < 0)
645 goto failed;
646 kde_len = pos - kde;
647 }
648#endif /* CONFIG_OCV */
649
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800650#ifdef CONFIG_P2P
651 if (sm->p2p) {
652 kde_buf = os_malloc(kde_len + 2 + RSN_SELECTOR_LEN + 1);
653 if (kde_buf) {
654 u8 *pos;
655 wpa_printf(MSG_DEBUG, "P2P: Add IP Address Request KDE "
656 "into EAPOL-Key 2/4");
657 os_memcpy(kde_buf, kde, kde_len);
658 kde = kde_buf;
659 pos = kde + kde_len;
660 *pos++ = WLAN_EID_VENDOR_SPECIFIC;
661 *pos++ = RSN_SELECTOR_LEN + 1;
662 RSN_SELECTOR_PUT(pos, WFA_KEY_DATA_IP_ADDR_REQ);
663 pos += RSN_SELECTOR_LEN;
664 *pos++ = 0x01;
665 kde_len = pos - kde;
666 }
667 }
668#endif /* CONFIG_P2P */
669
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700670 if (wpa_supplicant_send_2_of_4(sm, sm->bssid, key, ver, sm->snonce,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800671 kde, kde_len, ptk) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700672 goto failed;
673
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800674 os_free(kde_buf);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700675 os_memcpy(sm->anonce, key->key_nonce, WPA_NONCE_LEN);
676 return;
677
678failed:
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800679 os_free(kde_buf);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700680 wpa_sm_deauthenticate(sm, WLAN_REASON_UNSPECIFIED);
681}
682
683
684static void wpa_sm_start_preauth(void *eloop_ctx, void *timeout_ctx)
685{
686 struct wpa_sm *sm = eloop_ctx;
687 rsn_preauth_candidate_process(sm);
688}
689
690
691static void wpa_supplicant_key_neg_complete(struct wpa_sm *sm,
692 const u8 *addr, int secure)
693{
694 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
695 "WPA: Key negotiation completed with "
696 MACSTR " [PTK=%s GTK=%s]", MAC2STR(addr),
697 wpa_cipher_txt(sm->pairwise_cipher),
698 wpa_cipher_txt(sm->group_cipher));
699 wpa_sm_cancel_auth_timeout(sm);
700 wpa_sm_set_state(sm, WPA_COMPLETED);
701
702 if (secure) {
703 wpa_sm_mlme_setprotection(
704 sm, addr, MLME_SETPROTECTION_PROTECT_TYPE_RX_TX,
705 MLME_SETPROTECTION_KEY_TYPE_PAIRWISE);
706 eapol_sm_notify_portValid(sm->eapol, TRUE);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700707 if (wpa_key_mgmt_wpa_psk(sm->key_mgmt) ||
708 sm->key_mgmt == WPA_KEY_MGMT_DPP ||
709 sm->key_mgmt == WPA_KEY_MGMT_OWE)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700710 eapol_sm_notify_eap_success(sm->eapol, TRUE);
711 /*
712 * Start preauthentication after a short wait to avoid a
713 * possible race condition between the data receive and key
714 * configuration after the 4-Way Handshake. This increases the
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800715 * likelihood of the first preauth EAPOL-Start frame getting to
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700716 * the target AP.
717 */
Hai Shalom74f70d42019-02-11 14:42:39 -0800718 if (!dl_list_empty(&sm->pmksa_candidates))
719 eloop_register_timeout(1, 0, wpa_sm_start_preauth,
720 sm, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700721 }
722
723 if (sm->cur_pmksa && sm->cur_pmksa->opportunistic) {
724 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
725 "RSN: Authenticator accepted "
726 "opportunistic PMKSA entry - marking it valid");
727 sm->cur_pmksa->opportunistic = 0;
728 }
729
730#ifdef CONFIG_IEEE80211R
731 if (wpa_key_mgmt_ft(sm->key_mgmt)) {
732 /* Prepare for the next transition */
733 wpa_ft_prepare_auth_request(sm, NULL);
734 }
735#endif /* CONFIG_IEEE80211R */
736}
737
738
739static void wpa_sm_rekey_ptk(void *eloop_ctx, void *timeout_ctx)
740{
741 struct wpa_sm *sm = eloop_ctx;
742 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Request PTK rekeying");
743 wpa_sm_key_request(sm, 0, 1);
744}
745
746
747static int wpa_supplicant_install_ptk(struct wpa_sm *sm,
748 const struct wpa_eapol_key *key)
749{
750 int keylen, rsclen;
751 enum wpa_alg alg;
752 const u8 *key_rsc;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800753
Mathy Vanhoefc66556c2017-09-29 04:22:51 +0200754 if (sm->ptk.installed) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800755 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
756 "WPA: Do not re-install same PTK to the driver");
757 return 0;
758 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700759
760 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
761 "WPA: Installing PTK to the driver");
762
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700763 if (sm->pairwise_cipher == WPA_CIPHER_NONE) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700764 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Pairwise Cipher "
765 "Suite: NONE - do not use pairwise keys");
766 return 0;
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700767 }
768
769 if (!wpa_cipher_valid_pairwise(sm->pairwise_cipher)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700770 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
771 "WPA: Unsupported pairwise cipher %d",
772 sm->pairwise_cipher);
773 return -1;
774 }
775
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700776 alg = wpa_cipher_to_alg(sm->pairwise_cipher);
777 keylen = wpa_cipher_key_len(sm->pairwise_cipher);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700778 if (keylen <= 0 || (unsigned int) keylen != sm->ptk.tk_len) {
779 wpa_printf(MSG_DEBUG, "WPA: TK length mismatch: %d != %lu",
780 keylen, (long unsigned int) sm->ptk.tk_len);
781 return -1;
782 }
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700783 rsclen = wpa_cipher_rsc_len(sm->pairwise_cipher);
784
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800785 if (sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700786 key_rsc = null_rsc;
787 } else {
788 key_rsc = key->key_rsc;
789 wpa_hexdump(MSG_DEBUG, "WPA: RSC", key_rsc, rsclen);
790 }
791
792 if (wpa_sm_set_key(sm, alg, sm->bssid, 0, 1, key_rsc, rsclen,
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800793 sm->ptk.tk, keylen) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700794 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
795 "WPA: Failed to set PTK to the "
796 "driver (alg=%d keylen=%d bssid=" MACSTR ")",
797 alg, keylen, MAC2STR(sm->bssid));
798 return -1;
799 }
800
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800801 /* TK is not needed anymore in supplicant */
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800802 os_memset(sm->ptk.tk, 0, WPA_TK_MAX_LEN);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700803 sm->ptk.tk_len = 0;
Mathy Vanhoefc66556c2017-09-29 04:22:51 +0200804 sm->ptk.installed = 1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800805
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700806 if (sm->wpa_ptk_rekey) {
807 eloop_cancel_timeout(wpa_sm_rekey_ptk, sm, NULL);
808 eloop_register_timeout(sm->wpa_ptk_rekey, 0, wpa_sm_rekey_ptk,
809 sm, NULL);
810 }
811
812 return 0;
813}
814
815
816static int wpa_supplicant_check_group_cipher(struct wpa_sm *sm,
817 int group_cipher,
818 int keylen, int maxkeylen,
819 int *key_rsc_len,
820 enum wpa_alg *alg)
821{
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700822 int klen;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700823
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700824 *alg = wpa_cipher_to_alg(group_cipher);
825 if (*alg == WPA_ALG_NONE) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700826 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
827 "WPA: Unsupported Group Cipher %d",
828 group_cipher);
829 return -1;
830 }
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700831 *key_rsc_len = wpa_cipher_rsc_len(group_cipher);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700832
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700833 klen = wpa_cipher_key_len(group_cipher);
834 if (keylen != klen || maxkeylen < klen) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700835 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
836 "WPA: Unsupported %s Group Cipher key length %d (%d)",
837 wpa_cipher_txt(group_cipher), keylen, maxkeylen);
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700838 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700839 }
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700840 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700841}
842
843
844struct wpa_gtk_data {
845 enum wpa_alg alg;
846 int tx, key_rsc_len, keyidx;
847 u8 gtk[32];
848 int gtk_len;
849};
850
851
852static int wpa_supplicant_install_gtk(struct wpa_sm *sm,
853 const struct wpa_gtk_data *gd,
Jouni Malinen58c0e962017-10-01 12:12:24 +0300854 const u8 *key_rsc, int wnm_sleep)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700855{
856 const u8 *_gtk = gd->gtk;
857 u8 gtk_buf[32];
858
Mathy Vanhoef10bfd642017-07-12 16:03:24 +0200859 /* Detect possible key reinstallation */
Jouni Malinen58c0e962017-10-01 12:12:24 +0300860 if ((sm->gtk.gtk_len == (size_t) gd->gtk_len &&
861 os_memcmp(sm->gtk.gtk, gd->gtk, sm->gtk.gtk_len) == 0) ||
862 (sm->gtk_wnm_sleep.gtk_len == (size_t) gd->gtk_len &&
863 os_memcmp(sm->gtk_wnm_sleep.gtk, gd->gtk,
864 sm->gtk_wnm_sleep.gtk_len) == 0)) {
Mathy Vanhoef10bfd642017-07-12 16:03:24 +0200865 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
866 "WPA: Not reinstalling already in-use GTK to the driver (keyidx=%d tx=%d len=%d)",
867 gd->keyidx, gd->tx, gd->gtk_len);
868 return 0;
869 }
870
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700871 wpa_hexdump_key(MSG_DEBUG, "WPA: Group Key", gd->gtk, gd->gtk_len);
872 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
873 "WPA: Installing GTK to the driver (keyidx=%d tx=%d len=%d)",
874 gd->keyidx, gd->tx, gd->gtk_len);
875 wpa_hexdump(MSG_DEBUG, "WPA: RSC", key_rsc, gd->key_rsc_len);
876 if (sm->group_cipher == WPA_CIPHER_TKIP) {
877 /* Swap Tx/Rx keys for Michael MIC */
878 os_memcpy(gtk_buf, gd->gtk, 16);
879 os_memcpy(gtk_buf + 16, gd->gtk + 24, 8);
880 os_memcpy(gtk_buf + 24, gd->gtk + 16, 8);
881 _gtk = gtk_buf;
882 }
883 if (sm->pairwise_cipher == WPA_CIPHER_NONE) {
884 if (wpa_sm_set_key(sm, gd->alg, NULL,
885 gd->keyidx, 1, key_rsc, gd->key_rsc_len,
886 _gtk, gd->gtk_len) < 0) {
887 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
888 "WPA: Failed to set GTK to the driver "
889 "(Group only)");
Dmitry Shmidt61593f02014-04-21 16:27:35 -0700890 os_memset(gtk_buf, 0, sizeof(gtk_buf));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700891 return -1;
892 }
893 } else if (wpa_sm_set_key(sm, gd->alg, broadcast_ether_addr,
894 gd->keyidx, gd->tx, key_rsc, gd->key_rsc_len,
895 _gtk, gd->gtk_len) < 0) {
896 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
897 "WPA: Failed to set GTK to "
898 "the driver (alg=%d keylen=%d keyidx=%d)",
899 gd->alg, gd->gtk_len, gd->keyidx);
Dmitry Shmidt61593f02014-04-21 16:27:35 -0700900 os_memset(gtk_buf, 0, sizeof(gtk_buf));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700901 return -1;
902 }
Dmitry Shmidt61593f02014-04-21 16:27:35 -0700903 os_memset(gtk_buf, 0, sizeof(gtk_buf));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700904
Jouni Malinen58c0e962017-10-01 12:12:24 +0300905 if (wnm_sleep) {
906 sm->gtk_wnm_sleep.gtk_len = gd->gtk_len;
907 os_memcpy(sm->gtk_wnm_sleep.gtk, gd->gtk,
908 sm->gtk_wnm_sleep.gtk_len);
909 } else {
910 sm->gtk.gtk_len = gd->gtk_len;
911 os_memcpy(sm->gtk.gtk, gd->gtk, sm->gtk.gtk_len);
912 }
Mathy Vanhoef10bfd642017-07-12 16:03:24 +0200913
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700914 return 0;
915}
916
917
918static int wpa_supplicant_gtk_tx_bit_workaround(const struct wpa_sm *sm,
919 int tx)
920{
921 if (tx && sm->pairwise_cipher != WPA_CIPHER_NONE) {
922 /* Ignore Tx bit for GTK if a pairwise key is used. One AP
923 * seemed to set this bit (incorrectly, since Tx is only when
924 * doing Group Key only APs) and without this workaround, the
925 * data connection does not work because wpa_supplicant
926 * configured non-zero keyidx to be used for unicast. */
927 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
928 "WPA: Tx bit set for GTK, but pairwise "
929 "keys are used - ignore Tx bit");
930 return 0;
931 }
932 return tx;
933}
934
935
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800936static int wpa_supplicant_rsc_relaxation(const struct wpa_sm *sm,
937 const u8 *rsc)
938{
939 int rsclen;
940
941 if (!sm->wpa_rsc_relaxation)
942 return 0;
943
944 rsclen = wpa_cipher_rsc_len(sm->group_cipher);
945
946 /*
947 * Try to detect RSC (endian) corruption issue where the AP sends
948 * the RSC bytes in EAPOL-Key message in the wrong order, both if
949 * it's actually a 6-byte field (as it should be) and if it treats
950 * it as an 8-byte field.
951 * An AP model known to have this bug is the Sapido RB-1632.
952 */
953 if (rsclen == 6 && ((rsc[5] && !rsc[0]) || rsc[6] || rsc[7])) {
954 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
955 "RSC %02x%02x%02x%02x%02x%02x%02x%02x is likely bogus, using 0",
956 rsc[0], rsc[1], rsc[2], rsc[3],
957 rsc[4], rsc[5], rsc[6], rsc[7]);
958
959 return 1;
960 }
961
962 return 0;
963}
964
965
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700966static int wpa_supplicant_pairwise_gtk(struct wpa_sm *sm,
967 const struct wpa_eapol_key *key,
968 const u8 *gtk, size_t gtk_len,
969 int key_info)
970{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700971 struct wpa_gtk_data gd;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800972 const u8 *key_rsc;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700973
974 /*
975 * IEEE Std 802.11i-2004 - 8.5.2 EAPOL-Key frames - Figure 43x
976 * GTK KDE format:
977 * KeyID[bits 0-1], Tx [bit 2], Reserved [bits 3-7]
978 * Reserved [bits 0-7]
979 * GTK
980 */
981
982 os_memset(&gd, 0, sizeof(gd));
983 wpa_hexdump_key(MSG_DEBUG, "RSN: received GTK in pairwise handshake",
984 gtk, gtk_len);
985
986 if (gtk_len < 2 || gtk_len - 2 > sizeof(gd.gtk))
987 return -1;
988
989 gd.keyidx = gtk[0] & 0x3;
990 gd.tx = wpa_supplicant_gtk_tx_bit_workaround(sm,
991 !!(gtk[0] & BIT(2)));
992 gtk += 2;
993 gtk_len -= 2;
994
995 os_memcpy(gd.gtk, gtk, gtk_len);
996 gd.gtk_len = gtk_len;
997
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800998 key_rsc = key->key_rsc;
999 if (wpa_supplicant_rsc_relaxation(sm, key->key_rsc))
1000 key_rsc = null_rsc;
1001
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001002 if (sm->group_cipher != WPA_CIPHER_GTK_NOT_USED &&
1003 (wpa_supplicant_check_group_cipher(sm, sm->group_cipher,
1004 gtk_len, gtk_len,
1005 &gd.key_rsc_len, &gd.alg) ||
Jouni Malinen58c0e962017-10-01 12:12:24 +03001006 wpa_supplicant_install_gtk(sm, &gd, key_rsc, 0))) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001007 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1008 "RSN: Failed to install GTK");
Dmitry Shmidt61593f02014-04-21 16:27:35 -07001009 os_memset(&gd, 0, sizeof(gd));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001010 return -1;
1011 }
Dmitry Shmidt61593f02014-04-21 16:27:35 -07001012 os_memset(&gd, 0, sizeof(gd));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001013
1014 wpa_supplicant_key_neg_complete(sm, sm->bssid,
1015 key_info & WPA_KEY_INFO_SECURE);
1016 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001017}
1018
1019
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02001020#ifdef CONFIG_IEEE80211W
1021static int wpa_supplicant_install_igtk(struct wpa_sm *sm,
Jouni Malinen58c0e962017-10-01 12:12:24 +03001022 const struct wpa_igtk_kde *igtk,
1023 int wnm_sleep)
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02001024{
1025 size_t len = wpa_cipher_key_len(sm->mgmt_group_cipher);
1026 u16 keyidx = WPA_GET_LE16(igtk->keyid);
1027
1028 /* Detect possible key reinstallation */
Jouni Malinen58c0e962017-10-01 12:12:24 +03001029 if ((sm->igtk.igtk_len == len &&
1030 os_memcmp(sm->igtk.igtk, igtk->igtk, sm->igtk.igtk_len) == 0) ||
1031 (sm->igtk_wnm_sleep.igtk_len == len &&
1032 os_memcmp(sm->igtk_wnm_sleep.igtk, igtk->igtk,
1033 sm->igtk_wnm_sleep.igtk_len) == 0)) {
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02001034 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1035 "WPA: Not reinstalling already in-use IGTK to the driver (keyidx=%d)",
1036 keyidx);
1037 return 0;
1038 }
1039
1040 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
Hai Shalom39ba6fc2019-01-22 12:40:38 -08001041 "WPA: IGTK keyid %d pn " COMPACT_MACSTR,
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02001042 keyidx, MAC2STR(igtk->pn));
1043 wpa_hexdump_key(MSG_DEBUG, "WPA: IGTK", igtk->igtk, len);
1044 if (keyidx > 4095) {
1045 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1046 "WPA: Invalid IGTK KeyID %d", keyidx);
1047 return -1;
1048 }
1049 if (wpa_sm_set_key(sm, wpa_cipher_to_alg(sm->mgmt_group_cipher),
1050 broadcast_ether_addr,
1051 keyidx, 0, igtk->pn, sizeof(igtk->pn),
1052 igtk->igtk, len) < 0) {
1053 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1054 "WPA: Failed to configure IGTK to the driver");
1055 return -1;
1056 }
1057
Jouni Malinen58c0e962017-10-01 12:12:24 +03001058 if (wnm_sleep) {
1059 sm->igtk_wnm_sleep.igtk_len = len;
1060 os_memcpy(sm->igtk_wnm_sleep.igtk, igtk->igtk,
1061 sm->igtk_wnm_sleep.igtk_len);
1062 } else {
1063 sm->igtk.igtk_len = len;
1064 os_memcpy(sm->igtk.igtk, igtk->igtk, sm->igtk.igtk_len);
1065 }
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02001066
1067 return 0;
1068}
1069#endif /* CONFIG_IEEE80211W */
1070
1071
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001072static int ieee80211w_set_keys(struct wpa_sm *sm,
1073 struct wpa_eapol_ie_parse *ie)
1074{
1075#ifdef CONFIG_IEEE80211W
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001076 if (!wpa_cipher_valid_mgmt_group(sm->mgmt_group_cipher))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001077 return 0;
1078
1079 if (ie->igtk) {
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001080 size_t len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001081 const struct wpa_igtk_kde *igtk;
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02001082
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001083 len = wpa_cipher_key_len(sm->mgmt_group_cipher);
1084 if (ie->igtk_len != WPA_IGTK_KDE_PREFIX_LEN + len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001085 return -1;
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02001086
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001087 igtk = (const struct wpa_igtk_kde *) ie->igtk;
Jouni Malinen58c0e962017-10-01 12:12:24 +03001088 if (wpa_supplicant_install_igtk(sm, igtk, 0) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001089 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001090 }
1091
1092 return 0;
1093#else /* CONFIG_IEEE80211W */
1094 return 0;
1095#endif /* CONFIG_IEEE80211W */
1096}
1097
1098
1099static void wpa_report_ie_mismatch(struct wpa_sm *sm,
1100 const char *reason, const u8 *src_addr,
1101 const u8 *wpa_ie, size_t wpa_ie_len,
1102 const u8 *rsn_ie, size_t rsn_ie_len)
1103{
1104 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, "WPA: %s (src=" MACSTR ")",
1105 reason, MAC2STR(src_addr));
1106
1107 if (sm->ap_wpa_ie) {
1108 wpa_hexdump(MSG_INFO, "WPA: WPA IE in Beacon/ProbeResp",
1109 sm->ap_wpa_ie, sm->ap_wpa_ie_len);
1110 }
1111 if (wpa_ie) {
1112 if (!sm->ap_wpa_ie) {
1113 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1114 "WPA: No WPA IE in Beacon/ProbeResp");
1115 }
1116 wpa_hexdump(MSG_INFO, "WPA: WPA IE in 3/4 msg",
1117 wpa_ie, wpa_ie_len);
1118 }
1119
1120 if (sm->ap_rsn_ie) {
1121 wpa_hexdump(MSG_INFO, "WPA: RSN IE in Beacon/ProbeResp",
1122 sm->ap_rsn_ie, sm->ap_rsn_ie_len);
1123 }
1124 if (rsn_ie) {
1125 if (!sm->ap_rsn_ie) {
1126 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1127 "WPA: No RSN IE in Beacon/ProbeResp");
1128 }
1129 wpa_hexdump(MSG_INFO, "WPA: RSN IE in 3/4 msg",
1130 rsn_ie, rsn_ie_len);
1131 }
1132
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001133 wpa_sm_deauthenticate(sm, WLAN_REASON_IE_IN_4WAY_DIFFERS);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001134}
1135
1136
1137#ifdef CONFIG_IEEE80211R
1138
1139static int ft_validate_mdie(struct wpa_sm *sm,
1140 const unsigned char *src_addr,
1141 struct wpa_eapol_ie_parse *ie,
1142 const u8 *assoc_resp_mdie)
1143{
1144 struct rsn_mdie *mdie;
1145
1146 mdie = (struct rsn_mdie *) (ie->mdie + 2);
1147 if (ie->mdie == NULL || ie->mdie_len < 2 + sizeof(*mdie) ||
1148 os_memcmp(mdie->mobility_domain, sm->mobility_domain,
1149 MOBILITY_DOMAIN_ID_LEN) != 0) {
1150 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "FT: MDIE in msg 3/4 did "
1151 "not match with the current mobility domain");
1152 return -1;
1153 }
1154
1155 if (assoc_resp_mdie &&
1156 (assoc_resp_mdie[1] != ie->mdie[1] ||
1157 os_memcmp(assoc_resp_mdie, ie->mdie, 2 + ie->mdie[1]) != 0)) {
1158 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "FT: MDIE mismatch");
1159 wpa_hexdump(MSG_DEBUG, "FT: MDIE in EAPOL-Key msg 3/4",
1160 ie->mdie, 2 + ie->mdie[1]);
1161 wpa_hexdump(MSG_DEBUG, "FT: MDIE in (Re)Association Response",
1162 assoc_resp_mdie, 2 + assoc_resp_mdie[1]);
1163 return -1;
1164 }
1165
1166 return 0;
1167}
1168
1169
1170static int ft_validate_ftie(struct wpa_sm *sm,
1171 const unsigned char *src_addr,
1172 struct wpa_eapol_ie_parse *ie,
1173 const u8 *assoc_resp_ftie)
1174{
1175 if (ie->ftie == NULL) {
1176 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1177 "FT: No FTIE in EAPOL-Key msg 3/4");
1178 return -1;
1179 }
1180
1181 if (assoc_resp_ftie == NULL)
1182 return 0;
1183
1184 if (assoc_resp_ftie[1] != ie->ftie[1] ||
1185 os_memcmp(assoc_resp_ftie, ie->ftie, 2 + ie->ftie[1]) != 0) {
1186 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "FT: FTIE mismatch");
1187 wpa_hexdump(MSG_DEBUG, "FT: FTIE in EAPOL-Key msg 3/4",
1188 ie->ftie, 2 + ie->ftie[1]);
1189 wpa_hexdump(MSG_DEBUG, "FT: FTIE in (Re)Association Response",
1190 assoc_resp_ftie, 2 + assoc_resp_ftie[1]);
1191 return -1;
1192 }
1193
1194 return 0;
1195}
1196
1197
1198static int ft_validate_rsnie(struct wpa_sm *sm,
1199 const unsigned char *src_addr,
1200 struct wpa_eapol_ie_parse *ie)
1201{
1202 struct wpa_ie_data rsn;
1203
1204 if (!ie->rsn_ie)
1205 return 0;
1206
1207 /*
1208 * Verify that PMKR1Name from EAPOL-Key message 3/4
1209 * matches with the value we derived.
1210 */
1211 if (wpa_parse_wpa_ie_rsn(ie->rsn_ie, ie->rsn_ie_len, &rsn) < 0 ||
1212 rsn.num_pmkid != 1 || rsn.pmkid == NULL) {
1213 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "FT: No PMKR1Name in "
1214 "FT 4-way handshake message 3/4");
1215 return -1;
1216 }
1217
Dmitry Shmidtc2817022014-07-02 10:32:10 -07001218 if (os_memcmp_const(rsn.pmkid, sm->pmk_r1_name, WPA_PMK_NAME_LEN) != 0)
1219 {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001220 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1221 "FT: PMKR1Name mismatch in "
1222 "FT 4-way handshake message 3/4");
1223 wpa_hexdump(MSG_DEBUG, "FT: PMKR1Name from Authenticator",
1224 rsn.pmkid, WPA_PMK_NAME_LEN);
1225 wpa_hexdump(MSG_DEBUG, "FT: Derived PMKR1Name",
1226 sm->pmk_r1_name, WPA_PMK_NAME_LEN);
1227 return -1;
1228 }
1229
1230 return 0;
1231}
1232
1233
1234static int wpa_supplicant_validate_ie_ft(struct wpa_sm *sm,
1235 const unsigned char *src_addr,
1236 struct wpa_eapol_ie_parse *ie)
1237{
1238 const u8 *pos, *end, *mdie = NULL, *ftie = NULL;
1239
1240 if (sm->assoc_resp_ies) {
1241 pos = sm->assoc_resp_ies;
1242 end = pos + sm->assoc_resp_ies_len;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001243 while (end - pos > 2) {
1244 if (2 + pos[1] > end - pos)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001245 break;
1246 switch (*pos) {
1247 case WLAN_EID_MOBILITY_DOMAIN:
1248 mdie = pos;
1249 break;
1250 case WLAN_EID_FAST_BSS_TRANSITION:
1251 ftie = pos;
1252 break;
1253 }
1254 pos += 2 + pos[1];
1255 }
1256 }
1257
1258 if (ft_validate_mdie(sm, src_addr, ie, mdie) < 0 ||
1259 ft_validate_ftie(sm, src_addr, ie, ftie) < 0 ||
1260 ft_validate_rsnie(sm, src_addr, ie) < 0)
1261 return -1;
1262
1263 return 0;
1264}
1265
1266#endif /* CONFIG_IEEE80211R */
1267
1268
1269static int wpa_supplicant_validate_ie(struct wpa_sm *sm,
1270 const unsigned char *src_addr,
1271 struct wpa_eapol_ie_parse *ie)
1272{
1273 if (sm->ap_wpa_ie == NULL && sm->ap_rsn_ie == NULL) {
1274 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1275 "WPA: No WPA/RSN IE for this AP known. "
1276 "Trying to get from scan results");
1277 if (wpa_sm_get_beacon_ie(sm) < 0) {
1278 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1279 "WPA: Could not find AP from "
1280 "the scan results");
1281 } else {
1282 wpa_msg(sm->ctx->msg_ctx, MSG_DEBUG,
1283 "WPA: Found the current AP from "
1284 "updated scan results");
1285 }
1286 }
1287
1288 if (ie->wpa_ie == NULL && ie->rsn_ie == NULL &&
1289 (sm->ap_wpa_ie || sm->ap_rsn_ie)) {
1290 wpa_report_ie_mismatch(sm, "IE in 3/4 msg does not match "
1291 "with IE in Beacon/ProbeResp (no IE?)",
1292 src_addr, ie->wpa_ie, ie->wpa_ie_len,
1293 ie->rsn_ie, ie->rsn_ie_len);
1294 return -1;
1295 }
1296
1297 if ((ie->wpa_ie && sm->ap_wpa_ie &&
1298 (ie->wpa_ie_len != sm->ap_wpa_ie_len ||
1299 os_memcmp(ie->wpa_ie, sm->ap_wpa_ie, ie->wpa_ie_len) != 0)) ||
1300 (ie->rsn_ie && sm->ap_rsn_ie &&
1301 wpa_compare_rsn_ie(wpa_key_mgmt_ft(sm->key_mgmt),
1302 sm->ap_rsn_ie, sm->ap_rsn_ie_len,
1303 ie->rsn_ie, ie->rsn_ie_len))) {
1304 wpa_report_ie_mismatch(sm, "IE in 3/4 msg does not match "
1305 "with IE in Beacon/ProbeResp",
1306 src_addr, ie->wpa_ie, ie->wpa_ie_len,
1307 ie->rsn_ie, ie->rsn_ie_len);
1308 return -1;
1309 }
1310
1311 if (sm->proto == WPA_PROTO_WPA &&
1312 ie->rsn_ie && sm->ap_rsn_ie == NULL && sm->rsn_enabled) {
1313 wpa_report_ie_mismatch(sm, "Possible downgrade attack "
1314 "detected - RSN was enabled and RSN IE "
1315 "was in msg 3/4, but not in "
1316 "Beacon/ProbeResp",
1317 src_addr, ie->wpa_ie, ie->wpa_ie_len,
1318 ie->rsn_ie, ie->rsn_ie_len);
1319 return -1;
1320 }
1321
1322#ifdef CONFIG_IEEE80211R
1323 if (wpa_key_mgmt_ft(sm->key_mgmt) &&
1324 wpa_supplicant_validate_ie_ft(sm, src_addr, ie) < 0)
1325 return -1;
1326#endif /* CONFIG_IEEE80211R */
1327
1328 return 0;
1329}
1330
1331
1332/**
1333 * wpa_supplicant_send_4_of_4 - Send message 4 of WPA/RSN 4-Way Handshake
1334 * @sm: Pointer to WPA state machine data from wpa_sm_init()
1335 * @dst: Destination address for the frame
1336 * @key: Pointer to the EAPOL-Key frame header
1337 * @ver: Version bits from EAPOL-Key Key Info
1338 * @key_info: Key Info
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001339 * @ptk: PTK to use for keyed hash and encryption
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001340 * Returns: >= 0 on success, < 0 on failure
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001341 */
1342int wpa_supplicant_send_4_of_4(struct wpa_sm *sm, const unsigned char *dst,
1343 const struct wpa_eapol_key *key,
1344 u16 ver, u16 key_info,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001345 struct wpa_ptk *ptk)
1346{
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001347 size_t mic_len, hdrlen, rlen;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001348 struct wpa_eapol_key *reply;
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001349 u8 *rbuf, *key_mic;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001350
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001351 mic_len = wpa_mic_len(sm->key_mgmt, sm->pmk_len);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001352 hdrlen = sizeof(*reply) + mic_len + 2;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001353 rbuf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_KEY, NULL,
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001354 hdrlen, &rlen, (void *) &reply);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001355 if (rbuf == NULL)
1356 return -1;
1357
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001358 reply->type = (sm->proto == WPA_PROTO_RSN ||
1359 sm->proto == WPA_PROTO_OSEN) ?
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001360 EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA;
1361 key_info &= WPA_KEY_INFO_SECURE;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001362 key_info |= ver | WPA_KEY_INFO_KEY_TYPE;
1363 if (mic_len)
1364 key_info |= WPA_KEY_INFO_MIC;
1365 else
1366 key_info |= WPA_KEY_INFO_ENCR_KEY_DATA;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001367 WPA_PUT_BE16(reply->key_info, key_info);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001368 if (sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001369 WPA_PUT_BE16(reply->key_length, 0);
1370 else
1371 os_memcpy(reply->key_length, key->key_length, 2);
1372 os_memcpy(reply->replay_counter, key->replay_counter,
1373 WPA_REPLAY_COUNTER_LEN);
1374
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001375 key_mic = (u8 *) (reply + 1);
1376 WPA_PUT_BE16(key_mic + mic_len, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001377
Roshan Pius5e7db942018-04-12 12:27:41 -07001378 wpa_dbg(sm->ctx->msg_ctx, MSG_INFO, "WPA: Sending EAPOL-Key 4/4");
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001379 return wpa_eapol_key_send(sm, ptk, ver, dst, ETH_P_EAPOL, rbuf, rlen,
1380 key_mic);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001381}
1382
1383
1384static void wpa_supplicant_process_3_of_4(struct wpa_sm *sm,
1385 const struct wpa_eapol_key *key,
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001386 u16 ver, const u8 *key_data,
1387 size_t key_data_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001388{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001389 u16 key_info, keylen;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001390 struct wpa_eapol_ie_parse ie;
1391
1392 wpa_sm_set_state(sm, WPA_4WAY_HANDSHAKE);
Roshan Pius5e7db942018-04-12 12:27:41 -07001393 wpa_dbg(sm->ctx->msg_ctx, MSG_INFO, "WPA: RX message 3 of 4-Way "
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001394 "Handshake from " MACSTR " (ver=%d)", MAC2STR(sm->bssid), ver);
1395
1396 key_info = WPA_GET_BE16(key->key_info);
1397
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001398 wpa_hexdump(MSG_DEBUG, "WPA: IE KeyData", key_data, key_data_len);
1399 if (wpa_supplicant_parse_ies(key_data, key_data_len, &ie) < 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001400 goto failed;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001401 if (ie.gtk && !(key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
1402 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1403 "WPA: GTK IE in unencrypted key data");
1404 goto failed;
1405 }
1406#ifdef CONFIG_IEEE80211W
1407 if (ie.igtk && !(key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
1408 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1409 "WPA: IGTK KDE in unencrypted key data");
1410 goto failed;
1411 }
1412
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001413 if (ie.igtk &&
1414 wpa_cipher_valid_mgmt_group(sm->mgmt_group_cipher) &&
1415 ie.igtk_len != WPA_IGTK_KDE_PREFIX_LEN +
1416 (unsigned int) wpa_cipher_key_len(sm->mgmt_group_cipher)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001417 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1418 "WPA: Invalid IGTK KDE length %lu",
1419 (unsigned long) ie.igtk_len);
1420 goto failed;
1421 }
1422#endif /* CONFIG_IEEE80211W */
1423
1424 if (wpa_supplicant_validate_ie(sm, sm->bssid, &ie) < 0)
1425 goto failed;
1426
1427 if (os_memcmp(sm->anonce, key->key_nonce, WPA_NONCE_LEN) != 0) {
1428 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1429 "WPA: ANonce from message 1 of 4-Way Handshake "
1430 "differs from 3 of 4-Way Handshake - drop packet (src="
1431 MACSTR ")", MAC2STR(sm->bssid));
1432 goto failed;
1433 }
1434
1435 keylen = WPA_GET_BE16(key->key_length);
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07001436 if (keylen != wpa_cipher_key_len(sm->pairwise_cipher)) {
1437 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1438 "WPA: Invalid %s key length %d (src=" MACSTR
1439 ")", wpa_cipher_txt(sm->pairwise_cipher), keylen,
1440 MAC2STR(sm->bssid));
1441 goto failed;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001442 }
1443
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001444#ifdef CONFIG_P2P
1445 if (ie.ip_addr_alloc) {
1446 os_memcpy(sm->p2p_ip_addr, ie.ip_addr_alloc, 3 * 4);
1447 wpa_hexdump(MSG_DEBUG, "P2P: IP address info",
1448 sm->p2p_ip_addr, sizeof(sm->p2p_ip_addr));
1449 }
1450#endif /* CONFIG_P2P */
1451
Hai Shalom74f70d42019-02-11 14:42:39 -08001452#ifdef CONFIG_OCV
1453 if (wpa_sm_ocv_enabled(sm)) {
1454 struct wpa_channel_info ci;
1455
1456 if (wpa_sm_channel_info(sm, &ci) != 0) {
1457 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1458 "Failed to get channel info to validate received OCI in EAPOL-Key 3/4");
1459 return;
1460 }
1461
1462 if (ocv_verify_tx_params(ie.oci, ie.oci_len, &ci,
1463 channel_width_to_int(ci.chanwidth),
1464 ci.seg1_idx) != 0) {
1465 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, "%s",
1466 ocv_errorstr);
1467 return;
1468 }
1469 }
1470#endif /* CONFIG_OCV */
1471
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001472 if (wpa_supplicant_send_4_of_4(sm, sm->bssid, key, ver, key_info,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001473 &sm->ptk) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001474 goto failed;
1475 }
1476
1477 /* SNonce was successfully used in msg 3/4, so mark it to be renewed
1478 * for the next 4-Way Handshake. If msg 3 is received again, the old
1479 * SNonce will still be used to avoid changing PTK. */
1480 sm->renew_snonce = 1;
1481
1482 if (key_info & WPA_KEY_INFO_INSTALL) {
1483 if (wpa_supplicant_install_ptk(sm, key))
1484 goto failed;
1485 }
1486
1487 if (key_info & WPA_KEY_INFO_SECURE) {
1488 wpa_sm_mlme_setprotection(
1489 sm, sm->bssid, MLME_SETPROTECTION_PROTECT_TYPE_RX,
1490 MLME_SETPROTECTION_KEY_TYPE_PAIRWISE);
1491 eapol_sm_notify_portValid(sm->eapol, TRUE);
1492 }
1493 wpa_sm_set_state(sm, WPA_GROUP_HANDSHAKE);
1494
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001495 if (sm->group_cipher == WPA_CIPHER_GTK_NOT_USED) {
1496 wpa_supplicant_key_neg_complete(sm, sm->bssid,
1497 key_info & WPA_KEY_INFO_SECURE);
1498 } else if (ie.gtk &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001499 wpa_supplicant_pairwise_gtk(sm, key,
1500 ie.gtk, ie.gtk_len, key_info) < 0) {
1501 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1502 "RSN: Failed to configure GTK");
1503 goto failed;
1504 }
1505
1506 if (ieee80211w_set_keys(sm, &ie) < 0) {
1507 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1508 "RSN: Failed to configure IGTK");
1509 goto failed;
1510 }
1511
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001512 if (ie.gtk)
1513 wpa_sm_set_rekey_offload(sm);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001514
Hai Shalom39ba6fc2019-01-22 12:40:38 -08001515 /* Add PMKSA cache entry for Suite B AKMs here since PMKID can be
1516 * calculated only after KCK has been derived. Though, do not replace an
1517 * existing PMKSA entry after each 4-way handshake (i.e., new KCK/PMKID)
1518 * to avoid unnecessary changes of PMKID while continuing to use the
1519 * same PMK. */
1520 if (sm->proto == WPA_PROTO_RSN && wpa_key_mgmt_suite_b(sm->key_mgmt) &&
1521 !sm->cur_pmksa) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001522 struct rsn_pmksa_cache_entry *sa;
1523
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001524 sa = pmksa_cache_add(sm->pmksa, sm->pmk, sm->pmk_len, NULL,
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001525 sm->ptk.kck, sm->ptk.kck_len,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001526 sm->bssid, sm->own_addr,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001527 sm->network_ctx, sm->key_mgmt, NULL);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001528 if (!sm->cur_pmksa)
1529 sm->cur_pmksa = sa;
1530 }
1531
Dmitry Shmidt4dd28dc2015-03-10 11:21:43 -07001532 sm->msg_3_of_4_ok = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001533 return;
1534
1535failed:
1536 wpa_sm_deauthenticate(sm, WLAN_REASON_UNSPECIFIED);
1537}
1538
1539
1540static int wpa_supplicant_process_1_of_2_rsn(struct wpa_sm *sm,
1541 const u8 *keydata,
1542 size_t keydatalen,
1543 u16 key_info,
1544 struct wpa_gtk_data *gd)
1545{
1546 int maxkeylen;
1547 struct wpa_eapol_ie_parse ie;
1548
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08001549 wpa_hexdump_key(MSG_DEBUG, "RSN: msg 1/2 key data",
1550 keydata, keydatalen);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001551 if (wpa_supplicant_parse_ies(keydata, keydatalen, &ie) < 0)
1552 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001553 if (ie.gtk && !(key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
1554 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1555 "WPA: GTK IE in unencrypted key data");
1556 return -1;
1557 }
1558 if (ie.gtk == NULL) {
1559 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1560 "WPA: No GTK IE in Group Key msg 1/2");
1561 return -1;
1562 }
1563 maxkeylen = gd->gtk_len = ie.gtk_len - 2;
1564
Hai Shalom74f70d42019-02-11 14:42:39 -08001565#ifdef CONFIG_OCV
1566 if (wpa_sm_ocv_enabled(sm)) {
1567 struct wpa_channel_info ci;
1568
1569 if (wpa_sm_channel_info(sm, &ci) != 0) {
1570 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1571 "Failed to get channel info to validate received OCI in EAPOL-Key group msg 1/2");
1572 return -1;
1573 }
1574
1575 if (ocv_verify_tx_params(ie.oci, ie.oci_len, &ci,
1576 channel_width_to_int(ci.chanwidth),
1577 ci.seg1_idx) != 0) {
1578 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, "%s",
1579 ocv_errorstr);
1580 return -1;
1581 }
1582 }
1583#endif /* CONFIG_OCV */
1584
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001585 if (wpa_supplicant_check_group_cipher(sm, sm->group_cipher,
1586 gd->gtk_len, maxkeylen,
1587 &gd->key_rsc_len, &gd->alg))
1588 return -1;
1589
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001590 wpa_hexdump_key(MSG_DEBUG, "RSN: received GTK in group key handshake",
1591 ie.gtk, ie.gtk_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001592 gd->keyidx = ie.gtk[0] & 0x3;
1593 gd->tx = wpa_supplicant_gtk_tx_bit_workaround(sm,
1594 !!(ie.gtk[0] & BIT(2)));
1595 if (ie.gtk_len - 2 > sizeof(gd->gtk)) {
1596 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1597 "RSN: Too long GTK in GTK IE (len=%lu)",
1598 (unsigned long) ie.gtk_len - 2);
1599 return -1;
1600 }
1601 os_memcpy(gd->gtk, ie.gtk + 2, ie.gtk_len - 2);
1602
1603 if (ieee80211w_set_keys(sm, &ie) < 0)
1604 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1605 "RSN: Failed to configure IGTK");
1606
1607 return 0;
1608}
1609
1610
1611static int wpa_supplicant_process_1_of_2_wpa(struct wpa_sm *sm,
1612 const struct wpa_eapol_key *key,
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001613 const u8 *key_data,
1614 size_t key_data_len, u16 key_info,
1615 u16 ver, struct wpa_gtk_data *gd)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001616{
1617 size_t maxkeylen;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001618 u16 gtk_len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001619
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001620 gtk_len = WPA_GET_BE16(key->key_length);
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001621 maxkeylen = key_data_len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001622 if (ver == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
1623 if (maxkeylen < 8) {
1624 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1625 "WPA: Too short maxkeylen (%lu)",
1626 (unsigned long) maxkeylen);
1627 return -1;
1628 }
1629 maxkeylen -= 8;
1630 }
1631
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001632 if (gtk_len > maxkeylen ||
1633 wpa_supplicant_check_group_cipher(sm, sm->group_cipher,
1634 gtk_len, maxkeylen,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001635 &gd->key_rsc_len, &gd->alg))
1636 return -1;
1637
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001638 gd->gtk_len = gtk_len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001639 gd->keyidx = (key_info & WPA_KEY_INFO_KEY_INDEX_MASK) >>
1640 WPA_KEY_INFO_KEY_INDEX_SHIFT;
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001641 if (ver == WPA_KEY_INFO_TYPE_HMAC_MD5_RC4 && sm->ptk.kek_len == 16) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001642#ifdef CONFIG_NO_RC4
1643 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1644 "WPA: RC4 not supported in the build");
1645 return -1;
1646#else /* CONFIG_NO_RC4 */
Dmitry Shmidt61593f02014-04-21 16:27:35 -07001647 u8 ek[32];
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001648 if (key_data_len > sizeof(gd->gtk)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001649 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1650 "WPA: RC4 key data too long (%lu)",
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001651 (unsigned long) key_data_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001652 return -1;
1653 }
Dmitry Shmidt61593f02014-04-21 16:27:35 -07001654 os_memcpy(ek, key->key_iv, 16);
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001655 os_memcpy(ek + 16, sm->ptk.kek, sm->ptk.kek_len);
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001656 os_memcpy(gd->gtk, key_data, key_data_len);
1657 if (rc4_skip(ek, 32, 256, gd->gtk, key_data_len)) {
Dmitry Shmidt61593f02014-04-21 16:27:35 -07001658 os_memset(ek, 0, sizeof(ek));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001659 wpa_msg(sm->ctx->msg_ctx, MSG_ERROR,
1660 "WPA: RC4 failed");
1661 return -1;
1662 }
Dmitry Shmidt61593f02014-04-21 16:27:35 -07001663 os_memset(ek, 0, sizeof(ek));
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001664#endif /* CONFIG_NO_RC4 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001665 } else if (ver == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001666 if (maxkeylen % 8) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001667 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1668 "WPA: Unsupported AES-WRAP len %lu",
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001669 (unsigned long) maxkeylen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001670 return -1;
1671 }
1672 if (maxkeylen > sizeof(gd->gtk)) {
1673 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1674 "WPA: AES-WRAP key data "
1675 "too long (keydatalen=%lu maxkeylen=%lu)",
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001676 (unsigned long) key_data_len,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001677 (unsigned long) maxkeylen);
1678 return -1;
1679 }
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001680 if (aes_unwrap(sm->ptk.kek, sm->ptk.kek_len, maxkeylen / 8,
1681 key_data, gd->gtk)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001682 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1683 "WPA: AES unwrap failed - could not decrypt "
1684 "GTK");
1685 return -1;
1686 }
1687 } else {
1688 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1689 "WPA: Unsupported key_info type %d", ver);
1690 return -1;
1691 }
1692 gd->tx = wpa_supplicant_gtk_tx_bit_workaround(
1693 sm, !!(key_info & WPA_KEY_INFO_TXRX));
1694 return 0;
1695}
1696
1697
1698static int wpa_supplicant_send_2_of_2(struct wpa_sm *sm,
1699 const struct wpa_eapol_key *key,
1700 int ver, u16 key_info)
1701{
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001702 size_t mic_len, hdrlen, rlen;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001703 struct wpa_eapol_key *reply;
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001704 u8 *rbuf, *key_mic;
Hai Shalom74f70d42019-02-11 14:42:39 -08001705 size_t kde_len = 0;
1706
1707#ifdef CONFIG_OCV
1708 if (wpa_sm_ocv_enabled(sm))
1709 kde_len = OCV_OCI_KDE_LEN;
1710#endif /* CONFIG_OCV */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001711
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001712 mic_len = wpa_mic_len(sm->key_mgmt, sm->pmk_len);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001713 hdrlen = sizeof(*reply) + mic_len + 2;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001714 rbuf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_KEY, NULL,
Hai Shalom74f70d42019-02-11 14:42:39 -08001715 hdrlen + kde_len, &rlen, (void *) &reply);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001716 if (rbuf == NULL)
1717 return -1;
1718
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001719 reply->type = (sm->proto == WPA_PROTO_RSN ||
1720 sm->proto == WPA_PROTO_OSEN) ?
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001721 EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA;
1722 key_info &= WPA_KEY_INFO_KEY_INDEX_MASK;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001723 key_info |= ver | WPA_KEY_INFO_SECURE;
1724 if (mic_len)
1725 key_info |= WPA_KEY_INFO_MIC;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001726 else
1727 key_info |= WPA_KEY_INFO_ENCR_KEY_DATA;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001728 WPA_PUT_BE16(reply->key_info, key_info);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001729 if (sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001730 WPA_PUT_BE16(reply->key_length, 0);
1731 else
1732 os_memcpy(reply->key_length, key->key_length, 2);
1733 os_memcpy(reply->replay_counter, key->replay_counter,
1734 WPA_REPLAY_COUNTER_LEN);
1735
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001736 key_mic = (u8 *) (reply + 1);
Hai Shalom74f70d42019-02-11 14:42:39 -08001737 WPA_PUT_BE16(key_mic + mic_len, kde_len); /* Key Data Length */
1738
1739#ifdef CONFIG_OCV
1740 if (wpa_sm_ocv_enabled(sm)) {
1741 struct wpa_channel_info ci;
1742 u8 *pos;
1743
1744 if (wpa_sm_channel_info(sm, &ci) != 0) {
1745 wpa_printf(MSG_WARNING,
1746 "Failed to get channel info for OCI element in EAPOL-Key 2/2");
1747 os_free(rbuf);
1748 return -1;
1749 }
1750
1751 pos = key_mic + mic_len + 2; /* Key Data */
1752 if (ocv_insert_oci_kde(&ci, &pos) < 0) {
1753 os_free(rbuf);
1754 return -1;
1755 }
1756 }
1757#endif /* CONFIG_OCV */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001758
1759 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Sending EAPOL-Key 2/2");
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001760 return wpa_eapol_key_send(sm, &sm->ptk, ver, sm->bssid, ETH_P_EAPOL,
1761 rbuf, rlen, key_mic);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001762}
1763
1764
1765static void wpa_supplicant_process_1_of_2(struct wpa_sm *sm,
1766 const unsigned char *src_addr,
1767 const struct wpa_eapol_key *key,
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001768 const u8 *key_data,
1769 size_t key_data_len, u16 ver)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001770{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001771 u16 key_info;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001772 int rekey, ret;
1773 struct wpa_gtk_data gd;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001774 const u8 *key_rsc;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001775
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001776 if (!sm->msg_3_of_4_ok && !wpa_fils_is_completed(sm)) {
Dmitry Shmidt4dd28dc2015-03-10 11:21:43 -07001777 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1778 "WPA: Group Key Handshake started prior to completion of 4-way handshake");
1779 goto failed;
1780 }
1781
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001782 os_memset(&gd, 0, sizeof(gd));
1783
1784 rekey = wpa_sm_get_state(sm) == WPA_COMPLETED;
1785 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: RX message 1 of Group Key "
1786 "Handshake from " MACSTR " (ver=%d)", MAC2STR(src_addr), ver);
1787
1788 key_info = WPA_GET_BE16(key->key_info);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001789
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001790 if (sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN) {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001791 ret = wpa_supplicant_process_1_of_2_rsn(sm, key_data,
1792 key_data_len, key_info,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001793 &gd);
1794 } else {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001795 ret = wpa_supplicant_process_1_of_2_wpa(sm, key, key_data,
1796 key_data_len,
1797 key_info, ver, &gd);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001798 }
1799
1800 wpa_sm_set_state(sm, WPA_GROUP_HANDSHAKE);
1801
1802 if (ret)
1803 goto failed;
1804
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001805 key_rsc = key->key_rsc;
1806 if (wpa_supplicant_rsc_relaxation(sm, key->key_rsc))
1807 key_rsc = null_rsc;
1808
Jouni Malinen58c0e962017-10-01 12:12:24 +03001809 if (wpa_supplicant_install_gtk(sm, &gd, key_rsc, 0) ||
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001810 wpa_supplicant_send_2_of_2(sm, key, ver, key_info) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001811 goto failed;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001812 os_memset(&gd, 0, sizeof(gd));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001813
1814 if (rekey) {
1815 wpa_msg(sm->ctx->msg_ctx, MSG_INFO, "WPA: Group rekeying "
1816 "completed with " MACSTR " [GTK=%s]",
1817 MAC2STR(sm->bssid), wpa_cipher_txt(sm->group_cipher));
1818 wpa_sm_cancel_auth_timeout(sm);
1819 wpa_sm_set_state(sm, WPA_COMPLETED);
1820 } else {
1821 wpa_supplicant_key_neg_complete(sm, sm->bssid,
1822 key_info &
1823 WPA_KEY_INFO_SECURE);
1824 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001825
1826 wpa_sm_set_rekey_offload(sm);
1827
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001828 return;
1829
1830failed:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001831 os_memset(&gd, 0, sizeof(gd));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001832 wpa_sm_deauthenticate(sm, WLAN_REASON_UNSPECIFIED);
1833}
1834
1835
1836static int wpa_supplicant_verify_eapol_key_mic(struct wpa_sm *sm,
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001837 struct wpa_eapol_key *key,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001838 u16 ver,
1839 const u8 *buf, size_t len)
1840{
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001841 u8 mic[WPA_EAPOL_KEY_MIC_MAX_LEN];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001842 int ok = 0;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001843 size_t mic_len = wpa_mic_len(sm->key_mgmt, sm->pmk_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001844
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001845 os_memcpy(mic, key + 1, mic_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001846 if (sm->tptk_set) {
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001847 os_memset(key + 1, 0, mic_len);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001848 if (wpa_eapol_key_mic(sm->tptk.kck, sm->tptk.kck_len,
1849 sm->key_mgmt,
1850 ver, buf, len, (u8 *) (key + 1)) < 0 ||
1851 os_memcmp_const(mic, key + 1, mic_len) != 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001852 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1853 "WPA: Invalid EAPOL-Key MIC "
1854 "when using TPTK - ignoring TPTK");
Hai Shalom74f70d42019-02-11 14:42:39 -08001855#ifdef TEST_FUZZ
1856 wpa_printf(MSG_INFO,
1857 "TEST: Ignore Key MIC failure for fuzz testing");
1858 goto continue_fuzz;
1859#endif /* TEST_FUZZ */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001860 } else {
Hai Shalom74f70d42019-02-11 14:42:39 -08001861#ifdef TEST_FUZZ
1862 continue_fuzz:
1863#endif /* TEST_FUZZ */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001864 ok = 1;
1865 sm->tptk_set = 0;
1866 sm->ptk_set = 1;
1867 os_memcpy(&sm->ptk, &sm->tptk, sizeof(sm->ptk));
Dmitry Shmidt61593f02014-04-21 16:27:35 -07001868 os_memset(&sm->tptk, 0, sizeof(sm->tptk));
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001869 /*
1870 * This assures the same TPTK in sm->tptk can never be
Roshan Pius3a1667e2018-07-03 15:17:14 -07001871 * copied twice to sm->ptk as the new PTK. In
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001872 * combination with the installed flag in the wpa_ptk
1873 * struct, this assures the same PTK is only installed
1874 * once.
1875 */
1876 sm->renew_snonce = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001877 }
1878 }
1879
1880 if (!ok && sm->ptk_set) {
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001881 os_memset(key + 1, 0, mic_len);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001882 if (wpa_eapol_key_mic(sm->ptk.kck, sm->ptk.kck_len,
1883 sm->key_mgmt,
1884 ver, buf, len, (u8 *) (key + 1)) < 0 ||
1885 os_memcmp_const(mic, key + 1, mic_len) != 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001886 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1887 "WPA: Invalid EAPOL-Key MIC - "
1888 "dropping packet");
Hai Shalom74f70d42019-02-11 14:42:39 -08001889#ifdef TEST_FUZZ
1890 wpa_printf(MSG_INFO,
1891 "TEST: Ignore Key MIC failure for fuzz testing");
1892 goto continue_fuzz2;
1893#endif /* TEST_FUZZ */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001894 return -1;
1895 }
Hai Shalom74f70d42019-02-11 14:42:39 -08001896#ifdef TEST_FUZZ
1897 continue_fuzz2:
1898#endif /* TEST_FUZZ */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001899 ok = 1;
1900 }
1901
1902 if (!ok) {
1903 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1904 "WPA: Could not verify EAPOL-Key MIC - "
1905 "dropping packet");
1906 return -1;
1907 }
1908
1909 os_memcpy(sm->rx_replay_counter, key->replay_counter,
1910 WPA_REPLAY_COUNTER_LEN);
1911 sm->rx_replay_counter_set = 1;
1912 return 0;
1913}
1914
1915
1916/* Decrypt RSN EAPOL-Key key data (RC4 or AES-WRAP) */
1917static int wpa_supplicant_decrypt_key_data(struct wpa_sm *sm,
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001918 struct wpa_eapol_key *key,
1919 size_t mic_len, u16 ver,
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001920 u8 *key_data, size_t *key_data_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001921{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001922 wpa_hexdump(MSG_DEBUG, "RSN: encrypted key data",
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001923 key_data, *key_data_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001924 if (!sm->ptk_set) {
1925 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1926 "WPA: PTK not available, cannot decrypt EAPOL-Key Key "
1927 "Data");
1928 return -1;
1929 }
1930
1931 /* Decrypt key data here so that this operation does not need
1932 * to be implemented separately for each message type. */
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001933 if (ver == WPA_KEY_INFO_TYPE_HMAC_MD5_RC4 && sm->ptk.kek_len == 16) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001934#ifdef CONFIG_NO_RC4
1935 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1936 "WPA: RC4 not supported in the build");
1937 return -1;
1938#else /* CONFIG_NO_RC4 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001939 u8 ek[32];
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001940
1941 wpa_printf(MSG_DEBUG, "WPA: Decrypt Key Data using RC4");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001942 os_memcpy(ek, key->key_iv, 16);
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001943 os_memcpy(ek + 16, sm->ptk.kek, sm->ptk.kek_len);
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001944 if (rc4_skip(ek, 32, 256, key_data, *key_data_len)) {
Dmitry Shmidt61593f02014-04-21 16:27:35 -07001945 os_memset(ek, 0, sizeof(ek));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001946 wpa_msg(sm->ctx->msg_ctx, MSG_ERROR,
1947 "WPA: RC4 failed");
1948 return -1;
1949 }
Dmitry Shmidt61593f02014-04-21 16:27:35 -07001950 os_memset(ek, 0, sizeof(ek));
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001951#endif /* CONFIG_NO_RC4 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001952 } else if (ver == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES ||
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001953 ver == WPA_KEY_INFO_TYPE_AES_128_CMAC ||
Roshan Pius3a1667e2018-07-03 15:17:14 -07001954 wpa_use_aes_key_wrap(sm->key_mgmt)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001955 u8 *buf;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001956
1957 wpa_printf(MSG_DEBUG,
1958 "WPA: Decrypt Key Data using AES-UNWRAP (KEK length %u)",
1959 (unsigned int) sm->ptk.kek_len);
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001960 if (*key_data_len < 8 || *key_data_len % 8) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001961 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001962 "WPA: Unsupported AES-WRAP len %u",
1963 (unsigned int) *key_data_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001964 return -1;
1965 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001966 *key_data_len -= 8; /* AES-WRAP adds 8 bytes */
1967 buf = os_malloc(*key_data_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001968 if (buf == NULL) {
1969 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1970 "WPA: No memory for AES-UNWRAP buffer");
1971 return -1;
1972 }
Hai Shalom74f70d42019-02-11 14:42:39 -08001973#ifdef TEST_FUZZ
1974 os_memset(buf, 0x11, *key_data_len);
1975#endif /* TEST_FUZZ */
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001976 if (aes_unwrap(sm->ptk.kek, sm->ptk.kek_len, *key_data_len / 8,
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001977 key_data, buf)) {
Hai Shalom74f70d42019-02-11 14:42:39 -08001978#ifdef TEST_FUZZ
1979 wpa_printf(MSG_INFO,
1980 "TEST: Ignore AES unwrap failure for fuzz testing");
1981 goto continue_fuzz;
1982#endif /* TEST_FUZZ */
Dmitry Shmidt7d56b752015-12-22 10:59:44 -08001983 bin_clear_free(buf, *key_data_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001984 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1985 "WPA: AES unwrap failed - "
1986 "could not decrypt EAPOL-Key key data");
1987 return -1;
1988 }
Hai Shalom74f70d42019-02-11 14:42:39 -08001989#ifdef TEST_FUZZ
1990 continue_fuzz:
1991#endif /* TEST_FUZZ */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001992 os_memcpy(key_data, buf, *key_data_len);
Dmitry Shmidt7d56b752015-12-22 10:59:44 -08001993 bin_clear_free(buf, *key_data_len);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001994 WPA_PUT_BE16(((u8 *) (key + 1)) + mic_len, *key_data_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001995 } else {
1996 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1997 "WPA: Unsupported key_info type %d", ver);
1998 return -1;
1999 }
2000 wpa_hexdump_key(MSG_DEBUG, "WPA: decrypted EAPOL-Key key data",
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002001 key_data, *key_data_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002002 return 0;
2003}
2004
2005
2006/**
2007 * wpa_sm_aborted_cached - Notify WPA that PMKSA caching was aborted
2008 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2009 */
2010void wpa_sm_aborted_cached(struct wpa_sm *sm)
2011{
2012 if (sm && sm->cur_pmksa) {
2013 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2014 "RSN: Cancelling PMKSA caching attempt");
2015 sm->cur_pmksa = NULL;
2016 }
2017}
2018
2019
2020static void wpa_eapol_key_dump(struct wpa_sm *sm,
Dmitry Shmidt807291d2015-01-27 13:40:23 -08002021 const struct wpa_eapol_key *key,
2022 unsigned int key_data_len,
2023 const u8 *mic, unsigned int mic_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002024{
2025#ifndef CONFIG_NO_STDOUT_DEBUG
2026 u16 key_info = WPA_GET_BE16(key->key_info);
2027
2028 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, " EAPOL-Key type=%d", key->type);
2029 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2030 " key_info 0x%x (ver=%d keyidx=%d rsvd=%d %s%s%s%s%s%s%s%s)",
2031 key_info, key_info & WPA_KEY_INFO_TYPE_MASK,
2032 (key_info & WPA_KEY_INFO_KEY_INDEX_MASK) >>
2033 WPA_KEY_INFO_KEY_INDEX_SHIFT,
2034 (key_info & (BIT(13) | BIT(14) | BIT(15))) >> 13,
2035 key_info & WPA_KEY_INFO_KEY_TYPE ? "Pairwise" : "Group",
2036 key_info & WPA_KEY_INFO_INSTALL ? " Install" : "",
2037 key_info & WPA_KEY_INFO_ACK ? " Ack" : "",
2038 key_info & WPA_KEY_INFO_MIC ? " MIC" : "",
2039 key_info & WPA_KEY_INFO_SECURE ? " Secure" : "",
2040 key_info & WPA_KEY_INFO_ERROR ? " Error" : "",
2041 key_info & WPA_KEY_INFO_REQUEST ? " Request" : "",
2042 key_info & WPA_KEY_INFO_ENCR_KEY_DATA ? " Encr" : "");
2043 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2044 " key_length=%u key_data_length=%u",
Dmitry Shmidt807291d2015-01-27 13:40:23 -08002045 WPA_GET_BE16(key->key_length), key_data_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002046 wpa_hexdump(MSG_DEBUG, " replay_counter",
2047 key->replay_counter, WPA_REPLAY_COUNTER_LEN);
2048 wpa_hexdump(MSG_DEBUG, " key_nonce", key->key_nonce, WPA_NONCE_LEN);
2049 wpa_hexdump(MSG_DEBUG, " key_iv", key->key_iv, 16);
2050 wpa_hexdump(MSG_DEBUG, " key_rsc", key->key_rsc, 8);
2051 wpa_hexdump(MSG_DEBUG, " key_id (reserved)", key->key_id, 8);
Dmitry Shmidt807291d2015-01-27 13:40:23 -08002052 wpa_hexdump(MSG_DEBUG, " key_mic", mic, mic_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002053#endif /* CONFIG_NO_STDOUT_DEBUG */
2054}
2055
2056
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002057#ifdef CONFIG_FILS
2058static int wpa_supp_aead_decrypt(struct wpa_sm *sm, u8 *buf, size_t buf_len,
2059 size_t *key_data_len)
2060{
2061 struct wpa_ptk *ptk;
2062 struct ieee802_1x_hdr *hdr;
2063 struct wpa_eapol_key *key;
2064 u8 *pos, *tmp;
2065 const u8 *aad[1];
2066 size_t aad_len[1];
2067
2068 if (*key_data_len < AES_BLOCK_SIZE) {
2069 wpa_printf(MSG_INFO, "No room for AES-SIV data in the frame");
2070 return -1;
2071 }
2072
2073 if (sm->tptk_set)
2074 ptk = &sm->tptk;
2075 else if (sm->ptk_set)
2076 ptk = &sm->ptk;
2077 else
2078 return -1;
2079
2080 hdr = (struct ieee802_1x_hdr *) buf;
2081 key = (struct wpa_eapol_key *) (hdr + 1);
2082 pos = (u8 *) (key + 1);
2083 pos += 2; /* Pointing at the Encrypted Key Data field */
2084
2085 tmp = os_malloc(*key_data_len);
2086 if (!tmp)
2087 return -1;
2088
2089 /* AES-SIV AAD from EAPOL protocol version field (inclusive) to
2090 * to Key Data (exclusive). */
2091 aad[0] = buf;
2092 aad_len[0] = pos - buf;
2093 if (aes_siv_decrypt(ptk->kek, ptk->kek_len, pos, *key_data_len,
2094 1, aad, aad_len, tmp) < 0) {
2095 wpa_printf(MSG_INFO, "Invalid AES-SIV data in the frame");
2096 bin_clear_free(tmp, *key_data_len);
2097 return -1;
2098 }
2099
2100 /* AEAD decryption and validation completed successfully */
2101 (*key_data_len) -= AES_BLOCK_SIZE;
2102 wpa_hexdump_key(MSG_DEBUG, "WPA: Decrypted Key Data",
2103 tmp, *key_data_len);
2104
2105 /* Replace Key Data field with the decrypted version */
2106 os_memcpy(pos, tmp, *key_data_len);
2107 pos -= 2; /* Key Data Length field */
2108 WPA_PUT_BE16(pos, *key_data_len);
2109 bin_clear_free(tmp, *key_data_len);
2110
2111 if (sm->tptk_set) {
2112 sm->tptk_set = 0;
2113 sm->ptk_set = 1;
2114 os_memcpy(&sm->ptk, &sm->tptk, sizeof(sm->ptk));
2115 os_memset(&sm->tptk, 0, sizeof(sm->tptk));
2116 }
2117
2118 os_memcpy(sm->rx_replay_counter, key->replay_counter,
2119 WPA_REPLAY_COUNTER_LEN);
2120 sm->rx_replay_counter_set = 1;
2121
2122 return 0;
2123}
2124#endif /* CONFIG_FILS */
2125
2126
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002127/**
2128 * wpa_sm_rx_eapol - Process received WPA EAPOL frames
2129 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2130 * @src_addr: Source MAC address of the EAPOL packet
2131 * @buf: Pointer to the beginning of the EAPOL data (EAPOL header)
2132 * @len: Length of the EAPOL frame
2133 * Returns: 1 = WPA EAPOL-Key processed, 0 = not a WPA EAPOL-Key, -1 failure
2134 *
2135 * This function is called for each received EAPOL frame. Other than EAPOL-Key
2136 * frames can be skipped if filtering is done elsewhere. wpa_sm_rx_eapol() is
2137 * only processing WPA and WPA2 EAPOL-Key frames.
2138 *
2139 * The received EAPOL-Key packets are validated and valid packets are replied
2140 * to. In addition, key material (PTK, GTK) is configured at the end of a
2141 * successful key handshake.
2142 */
2143int wpa_sm_rx_eapol(struct wpa_sm *sm, const u8 *src_addr,
2144 const u8 *buf, size_t len)
2145{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002146 size_t plen, data_len, key_data_len;
2147 const struct ieee802_1x_hdr *hdr;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002148 struct wpa_eapol_key *key;
2149 u16 key_info, ver;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002150 u8 *tmp = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002151 int ret = -1;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002152 u8 *mic, *key_data;
Dmitry Shmidt807291d2015-01-27 13:40:23 -08002153 size_t mic_len, keyhdrlen;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002154
2155#ifdef CONFIG_IEEE80211R
2156 sm->ft_completed = 0;
2157#endif /* CONFIG_IEEE80211R */
2158
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002159 mic_len = wpa_mic_len(sm->key_mgmt, sm->pmk_len);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002160 keyhdrlen = sizeof(*key) + mic_len + 2;
Dmitry Shmidt807291d2015-01-27 13:40:23 -08002161
2162 if (len < sizeof(*hdr) + keyhdrlen) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002163 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2164 "WPA: EAPOL frame too short to be a WPA "
2165 "EAPOL-Key (len %lu, expecting at least %lu)",
2166 (unsigned long) len,
Dmitry Shmidt807291d2015-01-27 13:40:23 -08002167 (unsigned long) sizeof(*hdr) + keyhdrlen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002168 return 0;
2169 }
2170
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002171 hdr = (const struct ieee802_1x_hdr *) buf;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002172 plen = be_to_host16(hdr->length);
2173 data_len = plen + sizeof(*hdr);
2174 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2175 "IEEE 802.1X RX: version=%d type=%d length=%lu",
2176 hdr->version, hdr->type, (unsigned long) plen);
2177
2178 if (hdr->version < EAPOL_VERSION) {
2179 /* TODO: backwards compatibility */
2180 }
2181 if (hdr->type != IEEE802_1X_TYPE_EAPOL_KEY) {
2182 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2183 "WPA: EAPOL frame (type %u) discarded, "
2184 "not a Key frame", hdr->type);
2185 ret = 0;
2186 goto out;
2187 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002188 wpa_hexdump(MSG_MSGDUMP, "WPA: RX EAPOL-Key", buf, len);
Dmitry Shmidt807291d2015-01-27 13:40:23 -08002189 if (plen > len - sizeof(*hdr) || plen < keyhdrlen) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002190 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2191 "WPA: EAPOL frame payload size %lu "
2192 "invalid (frame size %lu)",
2193 (unsigned long) plen, (unsigned long) len);
2194 ret = 0;
2195 goto out;
2196 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002197 if (data_len < len) {
2198 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2199 "WPA: ignoring %lu bytes after the IEEE 802.1X data",
2200 (unsigned long) len - data_len);
2201 }
2202
2203 /*
2204 * Make a copy of the frame since we need to modify the buffer during
2205 * MAC validation and Key Data decryption.
2206 */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002207 tmp = os_memdup(buf, data_len);
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002208 if (tmp == NULL)
2209 goto out;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002210 key = (struct wpa_eapol_key *) (tmp + sizeof(struct ieee802_1x_hdr));
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002211 mic = (u8 *) (key + 1);
2212 key_data = mic + mic_len + 2;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002213
2214 if (key->type != EAPOL_KEY_TYPE_WPA && key->type != EAPOL_KEY_TYPE_RSN)
2215 {
2216 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2217 "WPA: EAPOL-Key type (%d) unknown, discarded",
2218 key->type);
2219 ret = 0;
2220 goto out;
2221 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002222
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002223 key_data_len = WPA_GET_BE16(mic + mic_len);
2224 wpa_eapol_key_dump(sm, key, key_data_len, mic, mic_len);
Dmitry Shmidt807291d2015-01-27 13:40:23 -08002225
2226 if (key_data_len > plen - keyhdrlen) {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002227 wpa_msg(sm->ctx->msg_ctx, MSG_INFO, "WPA: Invalid EAPOL-Key "
2228 "frame - key_data overflow (%u > %u)",
2229 (unsigned int) key_data_len,
Dmitry Shmidt807291d2015-01-27 13:40:23 -08002230 (unsigned int) (plen - keyhdrlen));
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002231 goto out;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002232 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002233
2234 eapol_sm_notify_lower_layer_success(sm->eapol, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002235 key_info = WPA_GET_BE16(key->key_info);
2236 ver = key_info & WPA_KEY_INFO_TYPE_MASK;
2237 if (ver != WPA_KEY_INFO_TYPE_HMAC_MD5_RC4 &&
2238#if defined(CONFIG_IEEE80211R) || defined(CONFIG_IEEE80211W)
2239 ver != WPA_KEY_INFO_TYPE_AES_128_CMAC &&
2240#endif /* CONFIG_IEEE80211R || CONFIG_IEEE80211W */
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002241 ver != WPA_KEY_INFO_TYPE_HMAC_SHA1_AES &&
Roshan Pius3a1667e2018-07-03 15:17:14 -07002242 !wpa_use_akm_defined(sm->key_mgmt)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002243 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2244 "WPA: Unsupported EAPOL-Key descriptor version %d",
2245 ver);
2246 goto out;
2247 }
2248
Roshan Pius3a1667e2018-07-03 15:17:14 -07002249 if (wpa_use_akm_defined(sm->key_mgmt) &&
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002250 ver != WPA_KEY_INFO_TYPE_AKM_DEFINED) {
2251 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2252 "RSN: Unsupported EAPOL-Key descriptor version %d (expected AKM defined = 0)",
2253 ver);
2254 goto out;
2255 }
2256
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002257#ifdef CONFIG_IEEE80211R
2258 if (wpa_key_mgmt_ft(sm->key_mgmt)) {
2259 /* IEEE 802.11r uses a new key_info type (AES-128-CMAC). */
Roshan Pius3a1667e2018-07-03 15:17:14 -07002260 if (ver != WPA_KEY_INFO_TYPE_AES_128_CMAC &&
2261 !wpa_use_akm_defined(sm->key_mgmt)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002262 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2263 "FT: AP did not use AES-128-CMAC");
2264 goto out;
2265 }
2266 } else
2267#endif /* CONFIG_IEEE80211R */
2268#ifdef CONFIG_IEEE80211W
2269 if (wpa_key_mgmt_sha256(sm->key_mgmt)) {
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002270 if (ver != WPA_KEY_INFO_TYPE_AES_128_CMAC &&
Roshan Pius3a1667e2018-07-03 15:17:14 -07002271 !wpa_use_akm_defined(sm->key_mgmt)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002272 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2273 "WPA: AP did not use the "
2274 "negotiated AES-128-CMAC");
2275 goto out;
2276 }
2277 } else
2278#endif /* CONFIG_IEEE80211W */
2279 if (sm->pairwise_cipher == WPA_CIPHER_CCMP &&
Roshan Pius3a1667e2018-07-03 15:17:14 -07002280 !wpa_use_akm_defined(sm->key_mgmt) &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002281 ver != WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
2282 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2283 "WPA: CCMP is used, but EAPOL-Key "
2284 "descriptor version (%d) is not 2", ver);
2285 if (sm->group_cipher != WPA_CIPHER_CCMP &&
2286 !(key_info & WPA_KEY_INFO_KEY_TYPE)) {
2287 /* Earlier versions of IEEE 802.11i did not explicitly
2288 * require version 2 descriptor for all EAPOL-Key
2289 * packets, so allow group keys to use version 1 if
2290 * CCMP is not used for them. */
2291 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2292 "WPA: Backwards compatibility: allow invalid "
2293 "version for non-CCMP group keys");
Jouni Malinen658fb4a2014-11-14 20:57:05 +02002294 } else if (ver == WPA_KEY_INFO_TYPE_AES_128_CMAC) {
2295 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2296 "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 -07002297 } else
2298 goto out;
Dmitry Shmidt71757432014-06-02 13:50:35 -07002299 } else if (sm->pairwise_cipher == WPA_CIPHER_GCMP &&
Roshan Pius3a1667e2018-07-03 15:17:14 -07002300 !wpa_use_akm_defined(sm->key_mgmt) &&
Dmitry Shmidt71757432014-06-02 13:50:35 -07002301 ver != WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002302 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2303 "WPA: GCMP is used, but EAPOL-Key "
2304 "descriptor version (%d) is not 2", ver);
2305 goto out;
2306 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002307
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002308 if (sm->rx_replay_counter_set &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002309 os_memcmp(key->replay_counter, sm->rx_replay_counter,
2310 WPA_REPLAY_COUNTER_LEN) <= 0) {
2311 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
2312 "WPA: EAPOL-Key Replay Counter did not increase - "
2313 "dropping packet");
2314 goto out;
2315 }
2316
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002317 if (key_info & WPA_KEY_INFO_SMK_MESSAGE) {
2318 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2319 "WPA: Unsupported SMK bit in key_info");
2320 goto out;
2321 }
2322
2323 if (!(key_info & WPA_KEY_INFO_ACK)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002324 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2325 "WPA: No Ack bit in key_info");
2326 goto out;
2327 }
2328
2329 if (key_info & WPA_KEY_INFO_REQUEST) {
2330 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2331 "WPA: EAPOL-Key with Request bit - dropped");
2332 goto out;
2333 }
2334
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002335 if ((key_info & WPA_KEY_INFO_MIC) &&
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002336 wpa_supplicant_verify_eapol_key_mic(sm, key, ver, tmp, data_len))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002337 goto out;
2338
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002339#ifdef CONFIG_FILS
2340 if (!mic_len && (key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
2341 if (wpa_supp_aead_decrypt(sm, tmp, data_len, &key_data_len))
2342 goto out;
2343 }
2344#endif /* CONFIG_FILS */
2345
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002346 if ((sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN) &&
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002347 (key_info & WPA_KEY_INFO_ENCR_KEY_DATA) && mic_len) {
Hai Shalomce48b4a2018-09-05 11:41:35 -07002348 /*
2349 * Only decrypt the Key Data field if the frame's authenticity
2350 * was verified. When using AES-SIV (FILS), the MIC flag is not
2351 * set, so this check should only be performed if mic_len != 0
2352 * which is the case in this code branch.
2353 */
2354 if (!(key_info & WPA_KEY_INFO_MIC)) {
2355 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
2356 "WPA: Ignore EAPOL-Key with encrypted but unauthenticated data");
2357 goto out;
2358 }
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002359 if (wpa_supplicant_decrypt_key_data(sm, key, mic_len,
2360 ver, key_data,
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002361 &key_data_len))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002362 goto out;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002363 }
2364
2365 if (key_info & WPA_KEY_INFO_KEY_TYPE) {
2366 if (key_info & WPA_KEY_INFO_KEY_INDEX_MASK) {
2367 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
2368 "WPA: Ignored EAPOL-Key (Pairwise) with "
2369 "non-zero key index");
2370 goto out;
2371 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002372 if (key_info & (WPA_KEY_INFO_MIC |
2373 WPA_KEY_INFO_ENCR_KEY_DATA)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002374 /* 3/4 4-Way Handshake */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002375 wpa_supplicant_process_3_of_4(sm, key, ver, key_data,
2376 key_data_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002377 } else {
2378 /* 1/4 4-Way Handshake */
2379 wpa_supplicant_process_1_of_4(sm, src_addr, key,
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002380 ver, key_data,
2381 key_data_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002382 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002383 } else {
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002384 if ((mic_len && (key_info & WPA_KEY_INFO_MIC)) ||
2385 (!mic_len && (key_info & WPA_KEY_INFO_ENCR_KEY_DATA))) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002386 /* 1/2 Group Key Handshake */
2387 wpa_supplicant_process_1_of_2(sm, src_addr, key,
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002388 key_data, key_data_len,
2389 ver);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002390 } else {
2391 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002392 "WPA: EAPOL-Key (Group) without Mic/Encr bit - "
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002393 "dropped");
2394 }
2395 }
2396
2397 ret = 1;
2398
2399out:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002400 bin_clear_free(tmp, data_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002401 return ret;
2402}
2403
2404
2405#ifdef CONFIG_CTRL_IFACE
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002406static u32 wpa_key_mgmt_suite(struct wpa_sm *sm)
2407{
2408 switch (sm->key_mgmt) {
2409 case WPA_KEY_MGMT_IEEE8021X:
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002410 return ((sm->proto == WPA_PROTO_RSN ||
2411 sm->proto == WPA_PROTO_OSEN) ?
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002412 RSN_AUTH_KEY_MGMT_UNSPEC_802_1X :
2413 WPA_AUTH_KEY_MGMT_UNSPEC_802_1X);
2414 case WPA_KEY_MGMT_PSK:
2415 return (sm->proto == WPA_PROTO_RSN ?
2416 RSN_AUTH_KEY_MGMT_PSK_OVER_802_1X :
2417 WPA_AUTH_KEY_MGMT_PSK_OVER_802_1X);
2418#ifdef CONFIG_IEEE80211R
2419 case WPA_KEY_MGMT_FT_IEEE8021X:
2420 return RSN_AUTH_KEY_MGMT_FT_802_1X;
2421 case WPA_KEY_MGMT_FT_PSK:
2422 return RSN_AUTH_KEY_MGMT_FT_PSK;
2423#endif /* CONFIG_IEEE80211R */
2424#ifdef CONFIG_IEEE80211W
2425 case WPA_KEY_MGMT_IEEE8021X_SHA256:
2426 return RSN_AUTH_KEY_MGMT_802_1X_SHA256;
2427 case WPA_KEY_MGMT_PSK_SHA256:
2428 return RSN_AUTH_KEY_MGMT_PSK_SHA256;
2429#endif /* CONFIG_IEEE80211W */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002430 case WPA_KEY_MGMT_CCKM:
2431 return (sm->proto == WPA_PROTO_RSN ?
2432 RSN_AUTH_KEY_MGMT_CCKM:
2433 WPA_AUTH_KEY_MGMT_CCKM);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002434 case WPA_KEY_MGMT_WPA_NONE:
2435 return WPA_AUTH_KEY_MGMT_NONE;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002436 case WPA_KEY_MGMT_IEEE8021X_SUITE_B:
2437 return RSN_AUTH_KEY_MGMT_802_1X_SUITE_B;
Dmitry Shmidt807291d2015-01-27 13:40:23 -08002438 case WPA_KEY_MGMT_IEEE8021X_SUITE_B_192:
2439 return RSN_AUTH_KEY_MGMT_802_1X_SUITE_B_192;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002440 default:
2441 return 0;
2442 }
2443}
2444
2445
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002446#define RSN_SUITE "%02x-%02x-%02x-%d"
2447#define RSN_SUITE_ARG(s) \
2448((s) >> 24) & 0xff, ((s) >> 16) & 0xff, ((s) >> 8) & 0xff, (s) & 0xff
2449
2450/**
2451 * wpa_sm_get_mib - Dump text list of MIB entries
2452 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2453 * @buf: Buffer for the list
2454 * @buflen: Length of the buffer
2455 * Returns: Number of bytes written to buffer
2456 *
2457 * This function is used fetch dot11 MIB variables.
2458 */
2459int wpa_sm_get_mib(struct wpa_sm *sm, char *buf, size_t buflen)
2460{
2461 char pmkid_txt[PMKID_LEN * 2 + 1];
2462 int rsna, ret;
2463 size_t len;
2464
2465 if (sm->cur_pmksa) {
2466 wpa_snprintf_hex(pmkid_txt, sizeof(pmkid_txt),
2467 sm->cur_pmksa->pmkid, PMKID_LEN);
2468 } else
2469 pmkid_txt[0] = '\0';
2470
2471 if ((wpa_key_mgmt_wpa_psk(sm->key_mgmt) ||
2472 wpa_key_mgmt_wpa_ieee8021x(sm->key_mgmt)) &&
2473 sm->proto == WPA_PROTO_RSN)
2474 rsna = 1;
2475 else
2476 rsna = 0;
2477
2478 ret = os_snprintf(buf, buflen,
2479 "dot11RSNAOptionImplemented=TRUE\n"
2480 "dot11RSNAPreauthenticationImplemented=TRUE\n"
2481 "dot11RSNAEnabled=%s\n"
2482 "dot11RSNAPreauthenticationEnabled=%s\n"
2483 "dot11RSNAConfigVersion=%d\n"
2484 "dot11RSNAConfigPairwiseKeysSupported=5\n"
2485 "dot11RSNAConfigGroupCipherSize=%d\n"
2486 "dot11RSNAConfigPMKLifetime=%d\n"
2487 "dot11RSNAConfigPMKReauthThreshold=%d\n"
2488 "dot11RSNAConfigNumberOfPTKSAReplayCounters=1\n"
2489 "dot11RSNAConfigSATimeout=%d\n",
2490 rsna ? "TRUE" : "FALSE",
2491 rsna ? "TRUE" : "FALSE",
2492 RSN_VERSION,
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07002493 wpa_cipher_key_len(sm->group_cipher) * 8,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002494 sm->dot11RSNAConfigPMKLifetime,
2495 sm->dot11RSNAConfigPMKReauthThreshold,
2496 sm->dot11RSNAConfigSATimeout);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002497 if (os_snprintf_error(buflen, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002498 return 0;
2499 len = ret;
2500
2501 ret = os_snprintf(
2502 buf + len, buflen - len,
2503 "dot11RSNAAuthenticationSuiteSelected=" RSN_SUITE "\n"
2504 "dot11RSNAPairwiseCipherSelected=" RSN_SUITE "\n"
2505 "dot11RSNAGroupCipherSelected=" RSN_SUITE "\n"
2506 "dot11RSNAPMKIDUsed=%s\n"
2507 "dot11RSNAAuthenticationSuiteRequested=" RSN_SUITE "\n"
2508 "dot11RSNAPairwiseCipherRequested=" RSN_SUITE "\n"
2509 "dot11RSNAGroupCipherRequested=" RSN_SUITE "\n"
2510 "dot11RSNAConfigNumberOfGTKSAReplayCounters=0\n"
2511 "dot11RSNA4WayHandshakeFailures=%u\n",
2512 RSN_SUITE_ARG(wpa_key_mgmt_suite(sm)),
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07002513 RSN_SUITE_ARG(wpa_cipher_to_suite(sm->proto,
2514 sm->pairwise_cipher)),
2515 RSN_SUITE_ARG(wpa_cipher_to_suite(sm->proto,
2516 sm->group_cipher)),
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002517 pmkid_txt,
2518 RSN_SUITE_ARG(wpa_key_mgmt_suite(sm)),
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07002519 RSN_SUITE_ARG(wpa_cipher_to_suite(sm->proto,
2520 sm->pairwise_cipher)),
2521 RSN_SUITE_ARG(wpa_cipher_to_suite(sm->proto,
2522 sm->group_cipher)),
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002523 sm->dot11RSNA4WayHandshakeFailures);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002524 if (!os_snprintf_error(buflen - len, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002525 len += ret;
2526
2527 return (int) len;
2528}
2529#endif /* CONFIG_CTRL_IFACE */
2530
2531
2532static void wpa_sm_pmksa_free_cb(struct rsn_pmksa_cache_entry *entry,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002533 void *ctx, enum pmksa_free_reason reason)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002534{
2535 struct wpa_sm *sm = ctx;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002536 int deauth = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002537
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002538 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "RSN: PMKSA cache entry free_cb: "
2539 MACSTR " reason=%d", MAC2STR(entry->aa), reason);
2540
2541 if (sm->cur_pmksa == entry) {
2542 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2543 "RSN: %s current PMKSA entry",
2544 reason == PMKSA_REPLACE ? "replaced" : "removed");
2545 pmksa_cache_clear_current(sm);
2546
2547 /*
2548 * If an entry is simply being replaced, there's no need to
2549 * deauthenticate because it will be immediately re-added.
2550 * This happens when EAP authentication is completed again
2551 * (reauth or failed PMKSA caching attempt).
2552 */
2553 if (reason != PMKSA_REPLACE)
2554 deauth = 1;
2555 }
2556
2557 if (reason == PMKSA_EXPIRE &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002558 (sm->pmk_len == entry->pmk_len &&
2559 os_memcmp(sm->pmk, entry->pmk, sm->pmk_len) == 0)) {
2560 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002561 "RSN: deauthenticating due to expired PMK");
2562 pmksa_cache_clear_current(sm);
2563 deauth = 1;
2564 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002565
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002566 if (deauth) {
Roshan Pius3a1667e2018-07-03 15:17:14 -07002567 sm->pmk_len = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002568 os_memset(sm->pmk, 0, sizeof(sm->pmk));
2569 wpa_sm_deauthenticate(sm, WLAN_REASON_UNSPECIFIED);
2570 }
2571}
2572
2573
2574/**
2575 * wpa_sm_init - Initialize WPA state machine
2576 * @ctx: Context pointer for callbacks; this needs to be an allocated buffer
2577 * Returns: Pointer to the allocated WPA state machine data
2578 *
2579 * This function is used to allocate a new WPA state machine and the returned
2580 * value is passed to all WPA state machine calls.
2581 */
2582struct wpa_sm * wpa_sm_init(struct wpa_sm_ctx *ctx)
2583{
2584 struct wpa_sm *sm;
2585
2586 sm = os_zalloc(sizeof(*sm));
2587 if (sm == NULL)
2588 return NULL;
2589 dl_list_init(&sm->pmksa_candidates);
2590 sm->renew_snonce = 1;
2591 sm->ctx = ctx;
2592
2593 sm->dot11RSNAConfigPMKLifetime = 43200;
2594 sm->dot11RSNAConfigPMKReauthThreshold = 70;
2595 sm->dot11RSNAConfigSATimeout = 60;
2596
2597 sm->pmksa = pmksa_cache_init(wpa_sm_pmksa_free_cb, sm, sm);
2598 if (sm->pmksa == NULL) {
2599 wpa_msg(sm->ctx->msg_ctx, MSG_ERROR,
2600 "RSN: PMKSA cache initialization failed");
2601 os_free(sm);
2602 return NULL;
2603 }
2604
2605 return sm;
2606}
2607
2608
2609/**
2610 * wpa_sm_deinit - Deinitialize WPA state machine
2611 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2612 */
2613void wpa_sm_deinit(struct wpa_sm *sm)
2614{
2615 if (sm == NULL)
2616 return;
2617 pmksa_cache_deinit(sm->pmksa);
2618 eloop_cancel_timeout(wpa_sm_start_preauth, sm, NULL);
2619 eloop_cancel_timeout(wpa_sm_rekey_ptk, sm, NULL);
2620 os_free(sm->assoc_wpa_ie);
2621 os_free(sm->ap_wpa_ie);
2622 os_free(sm->ap_rsn_ie);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002623 wpa_sm_drop_sa(sm);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002624 os_free(sm->ctx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002625#ifdef CONFIG_IEEE80211R
2626 os_free(sm->assoc_resp_ies);
2627#endif /* CONFIG_IEEE80211R */
Dmitry Shmidt55840ad2015-12-14 12:45:46 -08002628#ifdef CONFIG_TESTING_OPTIONS
2629 wpabuf_free(sm->test_assoc_ie);
2630#endif /* CONFIG_TESTING_OPTIONS */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002631#ifdef CONFIG_FILS_SK_PFS
2632 crypto_ecdh_deinit(sm->fils_ecdh);
2633#endif /* CONFIG_FILS_SK_PFS */
2634#ifdef CONFIG_FILS
2635 wpabuf_free(sm->fils_ft_ies);
2636#endif /* CONFIG_FILS */
2637#ifdef CONFIG_OWE
2638 crypto_ecdh_deinit(sm->owe_ecdh);
2639#endif /* CONFIG_OWE */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002640 os_free(sm);
2641}
2642
2643
2644/**
2645 * wpa_sm_notify_assoc - Notify WPA state machine about association
2646 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2647 * @bssid: The BSSID of the new association
2648 *
2649 * This function is called to let WPA state machine know that the connection
2650 * was established.
2651 */
2652void wpa_sm_notify_assoc(struct wpa_sm *sm, const u8 *bssid)
2653{
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02002654 int clear_keys = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002655
2656 if (sm == NULL)
2657 return;
2658
2659 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2660 "WPA: Association event - clear replay counter");
2661 os_memcpy(sm->bssid, bssid, ETH_ALEN);
2662 os_memset(sm->rx_replay_counter, 0, WPA_REPLAY_COUNTER_LEN);
2663 sm->rx_replay_counter_set = 0;
2664 sm->renew_snonce = 1;
2665 if (os_memcmp(sm->preauth_bssid, bssid, ETH_ALEN) == 0)
2666 rsn_preauth_deinit(sm);
2667
2668#ifdef CONFIG_IEEE80211R
2669 if (wpa_ft_is_completed(sm)) {
2670 /*
2671 * Clear portValid to kick EAPOL state machine to re-enter
2672 * AUTHENTICATED state to get the EAPOL port Authorized.
2673 */
2674 eapol_sm_notify_portValid(sm->eapol, FALSE);
2675 wpa_supplicant_key_neg_complete(sm, sm->bssid, 1);
2676
2677 /* Prepare for the next transition */
2678 wpa_ft_prepare_auth_request(sm, NULL);
2679
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02002680 clear_keys = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002681 }
2682#endif /* CONFIG_IEEE80211R */
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002683#ifdef CONFIG_FILS
2684 if (sm->fils_completed) {
2685 /*
2686 * Clear portValid to kick EAPOL state machine to re-enter
2687 * AUTHENTICATED state to get the EAPOL port Authorized.
2688 */
2689 wpa_supplicant_key_neg_complete(sm, sm->bssid, 1);
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02002690 clear_keys = 0;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002691 }
2692#endif /* CONFIG_FILS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002693
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02002694 if (clear_keys) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002695 /*
2696 * IEEE 802.11, 8.4.10: Delete PTK SA on (re)association if
2697 * this is not part of a Fast BSS Transition.
2698 */
2699 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Clear old PTK");
2700 sm->ptk_set = 0;
Dmitry Shmidt61593f02014-04-21 16:27:35 -07002701 os_memset(&sm->ptk, 0, sizeof(sm->ptk));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002702 sm->tptk_set = 0;
Dmitry Shmidt61593f02014-04-21 16:27:35 -07002703 os_memset(&sm->tptk, 0, sizeof(sm->tptk));
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02002704 os_memset(&sm->gtk, 0, sizeof(sm->gtk));
Jouni Malinen58c0e962017-10-01 12:12:24 +03002705 os_memset(&sm->gtk_wnm_sleep, 0, sizeof(sm->gtk_wnm_sleep));
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02002706#ifdef CONFIG_IEEE80211W
2707 os_memset(&sm->igtk, 0, sizeof(sm->igtk));
Jouni Malinen58c0e962017-10-01 12:12:24 +03002708 os_memset(&sm->igtk_wnm_sleep, 0, sizeof(sm->igtk_wnm_sleep));
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02002709#endif /* CONFIG_IEEE80211W */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002710 }
2711
2712#ifdef CONFIG_TDLS
2713 wpa_tdls_assoc(sm);
2714#endif /* CONFIG_TDLS */
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002715
2716#ifdef CONFIG_P2P
2717 os_memset(sm->p2p_ip_addr, 0, sizeof(sm->p2p_ip_addr));
2718#endif /* CONFIG_P2P */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002719}
2720
2721
2722/**
2723 * wpa_sm_notify_disassoc - Notify WPA state machine about disassociation
2724 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2725 *
2726 * This function is called to let WPA state machine know that the connection
2727 * was lost. This will abort any existing pre-authentication session.
2728 */
2729void wpa_sm_notify_disassoc(struct wpa_sm *sm)
2730{
Dmitry Shmidt4dd28dc2015-03-10 11:21:43 -07002731 eloop_cancel_timeout(wpa_sm_start_preauth, sm, NULL);
2732 eloop_cancel_timeout(wpa_sm_rekey_ptk, sm, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002733 rsn_preauth_deinit(sm);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002734 pmksa_cache_clear_current(sm);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002735 if (wpa_sm_get_state(sm) == WPA_4WAY_HANDSHAKE)
2736 sm->dot11RSNA4WayHandshakeFailures++;
2737#ifdef CONFIG_TDLS
2738 wpa_tdls_disassoc(sm);
2739#endif /* CONFIG_TDLS */
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002740#ifdef CONFIG_FILS
2741 sm->fils_completed = 0;
2742#endif /* CONFIG_FILS */
Jouni Malinen4283f9e2017-09-22 12:06:37 +03002743#ifdef CONFIG_IEEE80211R
2744 sm->ft_reassoc_completed = 0;
2745#endif /* CONFIG_IEEE80211R */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002746
2747 /* Keys are not needed in the WPA state machine anymore */
2748 wpa_sm_drop_sa(sm);
Dmitry Shmidt4dd28dc2015-03-10 11:21:43 -07002749
2750 sm->msg_3_of_4_ok = 0;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002751 os_memset(sm->bssid, 0, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002752}
2753
2754
2755/**
2756 * wpa_sm_set_pmk - Set PMK
2757 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2758 * @pmk: The new PMK
2759 * @pmk_len: The length of the new PMK in bytes
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08002760 * @pmkid: Calculated PMKID
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002761 * @bssid: AA to add into PMKSA cache or %NULL to not cache the PMK
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002762 *
2763 * Configure the PMK for WPA state machine.
2764 */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002765void wpa_sm_set_pmk(struct wpa_sm *sm, const u8 *pmk, size_t pmk_len,
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08002766 const u8 *pmkid, const u8 *bssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002767{
2768 if (sm == NULL)
2769 return;
2770
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002771 wpa_hexdump_key(MSG_DEBUG, "WPA: Set PMK based on external data",
2772 pmk, pmk_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002773 sm->pmk_len = pmk_len;
2774 os_memcpy(sm->pmk, pmk, pmk_len);
2775
2776#ifdef CONFIG_IEEE80211R
2777 /* Set XXKey to be PSK for FT key derivation */
2778 sm->xxkey_len = pmk_len;
2779 os_memcpy(sm->xxkey, pmk, pmk_len);
2780#endif /* CONFIG_IEEE80211R */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002781
2782 if (bssid) {
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08002783 pmksa_cache_add(sm->pmksa, pmk, pmk_len, pmkid, NULL, 0,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002784 bssid, sm->own_addr,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002785 sm->network_ctx, sm->key_mgmt, NULL);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002786 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002787}
2788
2789
2790/**
2791 * wpa_sm_set_pmk_from_pmksa - Set PMK based on the current PMKSA
2792 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2793 *
2794 * Take the PMK from the current PMKSA into use. If no PMKSA is active, the PMK
2795 * will be cleared.
2796 */
2797void wpa_sm_set_pmk_from_pmksa(struct wpa_sm *sm)
2798{
2799 if (sm == NULL)
2800 return;
2801
2802 if (sm->cur_pmksa) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002803 wpa_hexdump_key(MSG_DEBUG,
2804 "WPA: Set PMK based on current PMKSA",
2805 sm->cur_pmksa->pmk, sm->cur_pmksa->pmk_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002806 sm->pmk_len = sm->cur_pmksa->pmk_len;
2807 os_memcpy(sm->pmk, sm->cur_pmksa->pmk, sm->pmk_len);
2808 } else {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002809 wpa_printf(MSG_DEBUG, "WPA: No current PMKSA - clear PMK");
2810 sm->pmk_len = 0;
2811 os_memset(sm->pmk, 0, PMK_LEN_MAX);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002812 }
2813}
2814
2815
2816/**
2817 * wpa_sm_set_fast_reauth - Set fast reauthentication (EAP) enabled/disabled
2818 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2819 * @fast_reauth: Whether fast reauthentication (EAP) is allowed
2820 */
2821void wpa_sm_set_fast_reauth(struct wpa_sm *sm, int fast_reauth)
2822{
2823 if (sm)
2824 sm->fast_reauth = fast_reauth;
2825}
2826
2827
2828/**
2829 * wpa_sm_set_scard_ctx - Set context pointer for smartcard callbacks
2830 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2831 * @scard_ctx: Context pointer for smartcard related callback functions
2832 */
2833void wpa_sm_set_scard_ctx(struct wpa_sm *sm, void *scard_ctx)
2834{
2835 if (sm == NULL)
2836 return;
2837 sm->scard_ctx = scard_ctx;
2838 if (sm->preauth_eapol)
2839 eapol_sm_register_scard_ctx(sm->preauth_eapol, scard_ctx);
2840}
2841
2842
2843/**
2844 * wpa_sm_set_config - Notification of current configration change
2845 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2846 * @config: Pointer to current network configuration
2847 *
2848 * Notify WPA state machine that configuration has changed. config will be
2849 * stored as a backpointer to network configuration. This can be %NULL to clear
2850 * the stored pointed.
2851 */
2852void wpa_sm_set_config(struct wpa_sm *sm, struct rsn_supp_config *config)
2853{
2854 if (!sm)
2855 return;
2856
2857 if (config) {
2858 sm->network_ctx = config->network_ctx;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002859 sm->allowed_pairwise_cipher = config->allowed_pairwise_cipher;
2860 sm->proactive_key_caching = config->proactive_key_caching;
2861 sm->eap_workaround = config->eap_workaround;
2862 sm->eap_conf_ctx = config->eap_conf_ctx;
2863 if (config->ssid) {
2864 os_memcpy(sm->ssid, config->ssid, config->ssid_len);
2865 sm->ssid_len = config->ssid_len;
2866 } else
2867 sm->ssid_len = 0;
2868 sm->wpa_ptk_rekey = config->wpa_ptk_rekey;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002869 sm->p2p = config->p2p;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002870 sm->wpa_rsc_relaxation = config->wpa_rsc_relaxation;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002871#ifdef CONFIG_FILS
2872 if (config->fils_cache_id) {
2873 sm->fils_cache_id_set = 1;
2874 os_memcpy(sm->fils_cache_id, config->fils_cache_id,
2875 FILS_CACHE_ID_LEN);
2876 } else {
2877 sm->fils_cache_id_set = 0;
2878 }
2879#endif /* CONFIG_FILS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002880 } else {
2881 sm->network_ctx = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002882 sm->allowed_pairwise_cipher = 0;
2883 sm->proactive_key_caching = 0;
2884 sm->eap_workaround = 0;
2885 sm->eap_conf_ctx = NULL;
2886 sm->ssid_len = 0;
2887 sm->wpa_ptk_rekey = 0;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002888 sm->p2p = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002889 sm->wpa_rsc_relaxation = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002890 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002891}
2892
2893
2894/**
2895 * wpa_sm_set_own_addr - Set own MAC address
2896 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2897 * @addr: Own MAC address
2898 */
2899void wpa_sm_set_own_addr(struct wpa_sm *sm, const u8 *addr)
2900{
2901 if (sm)
2902 os_memcpy(sm->own_addr, addr, ETH_ALEN);
2903}
2904
2905
2906/**
2907 * wpa_sm_set_ifname - Set network interface name
2908 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2909 * @ifname: Interface name
2910 * @bridge_ifname: Optional bridge interface name (for pre-auth)
2911 */
2912void wpa_sm_set_ifname(struct wpa_sm *sm, const char *ifname,
2913 const char *bridge_ifname)
2914{
2915 if (sm) {
2916 sm->ifname = ifname;
2917 sm->bridge_ifname = bridge_ifname;
2918 }
2919}
2920
2921
2922/**
2923 * wpa_sm_set_eapol - Set EAPOL state machine pointer
2924 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2925 * @eapol: Pointer to EAPOL state machine allocated with eapol_sm_init()
2926 */
2927void wpa_sm_set_eapol(struct wpa_sm *sm, struct eapol_sm *eapol)
2928{
2929 if (sm)
2930 sm->eapol = eapol;
2931}
2932
2933
2934/**
2935 * wpa_sm_set_param - Set WPA state machine parameters
2936 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2937 * @param: Parameter field
2938 * @value: Parameter value
2939 * Returns: 0 on success, -1 on failure
2940 */
2941int wpa_sm_set_param(struct wpa_sm *sm, enum wpa_sm_conf_params param,
2942 unsigned int value)
2943{
2944 int ret = 0;
2945
2946 if (sm == NULL)
2947 return -1;
2948
2949 switch (param) {
2950 case RSNA_PMK_LIFETIME:
2951 if (value > 0)
2952 sm->dot11RSNAConfigPMKLifetime = value;
2953 else
2954 ret = -1;
2955 break;
2956 case RSNA_PMK_REAUTH_THRESHOLD:
2957 if (value > 0 && value <= 100)
2958 sm->dot11RSNAConfigPMKReauthThreshold = value;
2959 else
2960 ret = -1;
2961 break;
2962 case RSNA_SA_TIMEOUT:
2963 if (value > 0)
2964 sm->dot11RSNAConfigSATimeout = value;
2965 else
2966 ret = -1;
2967 break;
2968 case WPA_PARAM_PROTO:
2969 sm->proto = value;
2970 break;
2971 case WPA_PARAM_PAIRWISE:
2972 sm->pairwise_cipher = value;
2973 break;
2974 case WPA_PARAM_GROUP:
2975 sm->group_cipher = value;
2976 break;
2977 case WPA_PARAM_KEY_MGMT:
2978 sm->key_mgmt = value;
2979 break;
2980#ifdef CONFIG_IEEE80211W
2981 case WPA_PARAM_MGMT_GROUP:
2982 sm->mgmt_group_cipher = value;
2983 break;
2984#endif /* CONFIG_IEEE80211W */
2985 case WPA_PARAM_RSN_ENABLED:
2986 sm->rsn_enabled = value;
2987 break;
2988 case WPA_PARAM_MFP:
2989 sm->mfp = value;
2990 break;
Hai Shalom74f70d42019-02-11 14:42:39 -08002991 case WPA_PARAM_OCV:
2992 sm->ocv = value;
2993 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002994 default:
2995 break;
2996 }
2997
2998 return ret;
2999}
3000
3001
3002/**
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003003 * wpa_sm_get_status - Get WPA state machine
3004 * @sm: Pointer to WPA state machine data from wpa_sm_init()
3005 * @buf: Buffer for status information
3006 * @buflen: Maximum buffer length
3007 * @verbose: Whether to include verbose status information
3008 * Returns: Number of bytes written to buf.
3009 *
3010 * Query WPA state machine for status information. This function fills in
3011 * a text area with current status information. If the buffer (buf) is not
3012 * large enough, status information will be truncated to fit the buffer.
3013 */
3014int wpa_sm_get_status(struct wpa_sm *sm, char *buf, size_t buflen,
3015 int verbose)
3016{
3017 char *pos = buf, *end = buf + buflen;
3018 int ret;
3019
3020 ret = os_snprintf(pos, end - pos,
3021 "pairwise_cipher=%s\n"
3022 "group_cipher=%s\n"
3023 "key_mgmt=%s\n",
3024 wpa_cipher_txt(sm->pairwise_cipher),
3025 wpa_cipher_txt(sm->group_cipher),
3026 wpa_key_mgmt_txt(sm->key_mgmt, sm->proto));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003027 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003028 return pos - buf;
3029 pos += ret;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003030
3031 if (sm->mfp != NO_MGMT_FRAME_PROTECTION && sm->ap_rsn_ie) {
3032 struct wpa_ie_data rsn;
3033 if (wpa_parse_wpa_ie_rsn(sm->ap_rsn_ie, sm->ap_rsn_ie_len, &rsn)
3034 >= 0 &&
3035 rsn.capabilities & (WPA_CAPABILITY_MFPR |
3036 WPA_CAPABILITY_MFPC)) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003037 ret = os_snprintf(pos, end - pos, "pmf=%d\n"
3038 "mgmt_group_cipher=%s\n",
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003039 (rsn.capabilities &
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003040 WPA_CAPABILITY_MFPR) ? 2 : 1,
3041 wpa_cipher_txt(
3042 sm->mgmt_group_cipher));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003043 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003044 return pos - buf;
3045 pos += ret;
3046 }
3047 }
3048
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003049 return pos - buf;
3050}
3051
3052
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07003053int wpa_sm_pmf_enabled(struct wpa_sm *sm)
3054{
3055 struct wpa_ie_data rsn;
3056
3057 if (sm->mfp == NO_MGMT_FRAME_PROTECTION || !sm->ap_rsn_ie)
3058 return 0;
3059
3060 if (wpa_parse_wpa_ie_rsn(sm->ap_rsn_ie, sm->ap_rsn_ie_len, &rsn) >= 0 &&
3061 rsn.capabilities & (WPA_CAPABILITY_MFPR | WPA_CAPABILITY_MFPC))
3062 return 1;
3063
3064 return 0;
3065}
3066
3067
Hai Shalom74f70d42019-02-11 14:42:39 -08003068int wpa_sm_ocv_enabled(struct wpa_sm *sm)
3069{
3070 struct wpa_ie_data rsn;
3071
3072 if (!sm->ocv || !sm->ap_rsn_ie)
3073 return 0;
3074
3075 return wpa_parse_wpa_ie_rsn(sm->ap_rsn_ie, sm->ap_rsn_ie_len,
3076 &rsn) >= 0 &&
3077 (rsn.capabilities & WPA_CAPABILITY_OCVC);
3078}
3079
3080
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003081/**
3082 * wpa_sm_set_assoc_wpa_ie_default - Generate own WPA/RSN IE from configuration
3083 * @sm: Pointer to WPA state machine data from wpa_sm_init()
3084 * @wpa_ie: Pointer to buffer for WPA/RSN IE
3085 * @wpa_ie_len: Pointer to the length of the wpa_ie buffer
3086 * Returns: 0 on success, -1 on failure
3087 */
3088int wpa_sm_set_assoc_wpa_ie_default(struct wpa_sm *sm, u8 *wpa_ie,
3089 size_t *wpa_ie_len)
3090{
3091 int res;
3092
3093 if (sm == NULL)
3094 return -1;
3095
Dmitry Shmidt55840ad2015-12-14 12:45:46 -08003096#ifdef CONFIG_TESTING_OPTIONS
3097 if (sm->test_assoc_ie) {
3098 wpa_printf(MSG_DEBUG,
3099 "TESTING: Replace association WPA/RSN IE");
3100 if (*wpa_ie_len < wpabuf_len(sm->test_assoc_ie))
3101 return -1;
3102 os_memcpy(wpa_ie, wpabuf_head(sm->test_assoc_ie),
3103 wpabuf_len(sm->test_assoc_ie));
3104 res = wpabuf_len(sm->test_assoc_ie);
3105 } else
3106#endif /* CONFIG_TESTING_OPTIONS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003107 res = wpa_gen_wpa_ie(sm, wpa_ie, *wpa_ie_len);
3108 if (res < 0)
3109 return -1;
3110 *wpa_ie_len = res;
3111
3112 wpa_hexdump(MSG_DEBUG, "WPA: Set own WPA IE default",
3113 wpa_ie, *wpa_ie_len);
3114
3115 if (sm->assoc_wpa_ie == NULL) {
3116 /*
3117 * Make a copy of the WPA/RSN IE so that 4-Way Handshake gets
3118 * the correct version of the IE even if PMKSA caching is
3119 * aborted (which would remove PMKID from IE generation).
3120 */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003121 sm->assoc_wpa_ie = os_memdup(wpa_ie, *wpa_ie_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003122 if (sm->assoc_wpa_ie == NULL)
3123 return -1;
3124
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003125 sm->assoc_wpa_ie_len = *wpa_ie_len;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003126 } else {
3127 wpa_hexdump(MSG_DEBUG,
3128 "WPA: Leave previously set WPA IE default",
3129 sm->assoc_wpa_ie, sm->assoc_wpa_ie_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003130 }
3131
3132 return 0;
3133}
3134
3135
3136/**
3137 * wpa_sm_set_assoc_wpa_ie - Set own WPA/RSN IE from (Re)AssocReq
3138 * @sm: Pointer to WPA state machine data from wpa_sm_init()
3139 * @ie: Pointer to IE data (starting from id)
3140 * @len: IE length
3141 * Returns: 0 on success, -1 on failure
3142 *
3143 * Inform WPA state machine about the WPA/RSN IE used in (Re)Association
3144 * Request frame. The IE will be used to override the default value generated
3145 * with wpa_sm_set_assoc_wpa_ie_default().
3146 */
3147int wpa_sm_set_assoc_wpa_ie(struct wpa_sm *sm, const u8 *ie, size_t len)
3148{
3149 if (sm == NULL)
3150 return -1;
3151
3152 os_free(sm->assoc_wpa_ie);
3153 if (ie == NULL || len == 0) {
3154 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
3155 "WPA: clearing own WPA/RSN IE");
3156 sm->assoc_wpa_ie = NULL;
3157 sm->assoc_wpa_ie_len = 0;
3158 } else {
3159 wpa_hexdump(MSG_DEBUG, "WPA: set own WPA/RSN IE", ie, len);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003160 sm->assoc_wpa_ie = os_memdup(ie, len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003161 if (sm->assoc_wpa_ie == NULL)
3162 return -1;
3163
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003164 sm->assoc_wpa_ie_len = len;
3165 }
3166
3167 return 0;
3168}
3169
3170
3171/**
3172 * wpa_sm_set_ap_wpa_ie - Set AP WPA IE from Beacon/ProbeResp
3173 * @sm: Pointer to WPA state machine data from wpa_sm_init()
3174 * @ie: Pointer to IE data (starting from id)
3175 * @len: IE length
3176 * Returns: 0 on success, -1 on failure
3177 *
3178 * Inform WPA state machine about the WPA IE used in Beacon / Probe Response
3179 * frame.
3180 */
3181int wpa_sm_set_ap_wpa_ie(struct wpa_sm *sm, const u8 *ie, size_t len)
3182{
3183 if (sm == NULL)
3184 return -1;
3185
3186 os_free(sm->ap_wpa_ie);
3187 if (ie == NULL || len == 0) {
3188 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
3189 "WPA: clearing AP WPA IE");
3190 sm->ap_wpa_ie = NULL;
3191 sm->ap_wpa_ie_len = 0;
3192 } else {
3193 wpa_hexdump(MSG_DEBUG, "WPA: set AP WPA IE", ie, len);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003194 sm->ap_wpa_ie = os_memdup(ie, len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003195 if (sm->ap_wpa_ie == NULL)
3196 return -1;
3197
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003198 sm->ap_wpa_ie_len = len;
3199 }
3200
3201 return 0;
3202}
3203
3204
3205/**
3206 * wpa_sm_set_ap_rsn_ie - Set AP RSN IE from Beacon/ProbeResp
3207 * @sm: Pointer to WPA state machine data from wpa_sm_init()
3208 * @ie: Pointer to IE data (starting from id)
3209 * @len: IE length
3210 * Returns: 0 on success, -1 on failure
3211 *
3212 * Inform WPA state machine about the RSN IE used in Beacon / Probe Response
3213 * frame.
3214 */
3215int wpa_sm_set_ap_rsn_ie(struct wpa_sm *sm, const u8 *ie, size_t len)
3216{
3217 if (sm == NULL)
3218 return -1;
3219
3220 os_free(sm->ap_rsn_ie);
3221 if (ie == NULL || len == 0) {
3222 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
3223 "WPA: clearing AP RSN IE");
3224 sm->ap_rsn_ie = NULL;
3225 sm->ap_rsn_ie_len = 0;
3226 } else {
3227 wpa_hexdump(MSG_DEBUG, "WPA: set AP RSN IE", ie, len);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003228 sm->ap_rsn_ie = os_memdup(ie, len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003229 if (sm->ap_rsn_ie == NULL)
3230 return -1;
3231
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003232 sm->ap_rsn_ie_len = len;
3233 }
3234
3235 return 0;
3236}
3237
3238
3239/**
3240 * wpa_sm_parse_own_wpa_ie - Parse own WPA/RSN IE
3241 * @sm: Pointer to WPA state machine data from wpa_sm_init()
3242 * @data: Pointer to data area for parsing results
3243 * Returns: 0 on success, -1 if IE is not known, or -2 on parsing failure
3244 *
3245 * Parse the contents of the own WPA or RSN IE from (Re)AssocReq and write the
3246 * parsed data into data.
3247 */
3248int wpa_sm_parse_own_wpa_ie(struct wpa_sm *sm, struct wpa_ie_data *data)
3249{
3250 if (sm == NULL)
3251 return -1;
3252
3253 if (sm->assoc_wpa_ie == NULL) {
3254 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
3255 "WPA: No WPA/RSN IE available from association info");
3256 return -1;
3257 }
3258 if (wpa_parse_wpa_ie(sm->assoc_wpa_ie, sm->assoc_wpa_ie_len, data))
3259 return -2;
3260 return 0;
3261}
3262
3263
3264int wpa_sm_pmksa_cache_list(struct wpa_sm *sm, char *buf, size_t len)
3265{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003266 return pmksa_cache_list(sm->pmksa, buf, len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003267}
3268
3269
Dmitry Shmidt29333592017-01-09 12:27:11 -08003270struct rsn_pmksa_cache_entry * wpa_sm_pmksa_cache_head(struct wpa_sm *sm)
3271{
3272 return pmksa_cache_head(sm->pmksa);
3273}
3274
3275
3276struct rsn_pmksa_cache_entry *
3277wpa_sm_pmksa_cache_add_entry(struct wpa_sm *sm,
3278 struct rsn_pmksa_cache_entry * entry)
3279{
3280 return pmksa_cache_add_entry(sm->pmksa, entry);
3281}
3282
3283
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003284void wpa_sm_pmksa_cache_add(struct wpa_sm *sm, const u8 *pmk, size_t pmk_len,
3285 const u8 *pmkid, const u8 *bssid,
3286 const u8 *fils_cache_id)
3287{
3288 sm->cur_pmksa = pmksa_cache_add(sm->pmksa, pmk, pmk_len, pmkid, NULL, 0,
3289 bssid, sm->own_addr, sm->network_ctx,
3290 sm->key_mgmt, fils_cache_id);
3291}
3292
3293
3294int wpa_sm_pmksa_exists(struct wpa_sm *sm, const u8 *bssid,
3295 const void *network_ctx)
3296{
Roshan Pius3a1667e2018-07-03 15:17:14 -07003297 return pmksa_cache_get(sm->pmksa, bssid, NULL, network_ctx, 0) != NULL;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003298}
3299
3300
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003301void wpa_sm_drop_sa(struct wpa_sm *sm)
3302{
3303 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Clear old PMK and PTK");
3304 sm->ptk_set = 0;
3305 sm->tptk_set = 0;
Roshan Pius3a1667e2018-07-03 15:17:14 -07003306 sm->pmk_len = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003307 os_memset(sm->pmk, 0, sizeof(sm->pmk));
3308 os_memset(&sm->ptk, 0, sizeof(sm->ptk));
3309 os_memset(&sm->tptk, 0, sizeof(sm->tptk));
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02003310 os_memset(&sm->gtk, 0, sizeof(sm->gtk));
Jouni Malinen58c0e962017-10-01 12:12:24 +03003311 os_memset(&sm->gtk_wnm_sleep, 0, sizeof(sm->gtk_wnm_sleep));
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02003312#ifdef CONFIG_IEEE80211W
3313 os_memset(&sm->igtk, 0, sizeof(sm->igtk));
Jouni Malinen58c0e962017-10-01 12:12:24 +03003314 os_memset(&sm->igtk_wnm_sleep, 0, sizeof(sm->igtk_wnm_sleep));
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02003315#endif /* CONFIG_IEEE80211W */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003316#ifdef CONFIG_IEEE80211R
3317 os_memset(sm->xxkey, 0, sizeof(sm->xxkey));
Roshan Pius3a1667e2018-07-03 15:17:14 -07003318 sm->xxkey_len = 0;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003319 os_memset(sm->pmk_r0, 0, sizeof(sm->pmk_r0));
Roshan Pius3a1667e2018-07-03 15:17:14 -07003320 sm->pmk_r0_len = 0;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003321 os_memset(sm->pmk_r1, 0, sizeof(sm->pmk_r1));
Roshan Pius3a1667e2018-07-03 15:17:14 -07003322 sm->pmk_r1_len = 0;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003323#endif /* CONFIG_IEEE80211R */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003324}
3325
3326
3327int wpa_sm_has_ptk(struct wpa_sm *sm)
3328{
3329 if (sm == NULL)
3330 return 0;
3331 return sm->ptk_set;
3332}
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003333
3334
3335void wpa_sm_update_replay_ctr(struct wpa_sm *sm, const u8 *replay_ctr)
3336{
3337 os_memcpy(sm->rx_replay_counter, replay_ctr, WPA_REPLAY_COUNTER_LEN);
3338}
3339
3340
3341void wpa_sm_pmksa_cache_flush(struct wpa_sm *sm, void *network_ctx)
3342{
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07003343 pmksa_cache_flush(sm->pmksa, network_ctx, NULL, 0);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003344}
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003345
3346
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003347#ifdef CONFIG_WNM
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003348int wpa_wnmsleep_install_key(struct wpa_sm *sm, u8 subelem_id, u8 *buf)
3349{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003350 u16 keyinfo;
3351 u8 keylen; /* plaintext key len */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003352 u8 *key_rsc;
3353
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003354 if (subelem_id == WNM_SLEEP_SUBELEM_GTK) {
Dmitry Shmidt61593f02014-04-21 16:27:35 -07003355 struct wpa_gtk_data gd;
3356
3357 os_memset(&gd, 0, sizeof(gd));
3358 keylen = wpa_cipher_key_len(sm->group_cipher);
3359 gd.key_rsc_len = wpa_cipher_rsc_len(sm->group_cipher);
3360 gd.alg = wpa_cipher_to_alg(sm->group_cipher);
3361 if (gd.alg == WPA_ALG_NONE) {
3362 wpa_printf(MSG_DEBUG, "Unsupported group cipher suite");
3363 return -1;
3364 }
3365
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003366 key_rsc = buf + 5;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003367 keyinfo = WPA_GET_LE16(buf + 2);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003368 gd.gtk_len = keylen;
3369 if (gd.gtk_len != buf[4]) {
3370 wpa_printf(MSG_DEBUG, "GTK len mismatch len %d vs %d",
3371 gd.gtk_len, buf[4]);
3372 return -1;
3373 }
3374 gd.keyidx = keyinfo & 0x03; /* B0 - B1 */
3375 gd.tx = wpa_supplicant_gtk_tx_bit_workaround(
3376 sm, !!(keyinfo & WPA_KEY_INFO_TXRX));
3377
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003378 os_memcpy(gd.gtk, buf + 13, gd.gtk_len);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003379
3380 wpa_hexdump_key(MSG_DEBUG, "Install GTK (WNM SLEEP)",
3381 gd.gtk, gd.gtk_len);
Jouni Malinen58c0e962017-10-01 12:12:24 +03003382 if (wpa_supplicant_install_gtk(sm, &gd, key_rsc, 1)) {
Dmitry Shmidt61593f02014-04-21 16:27:35 -07003383 os_memset(&gd, 0, sizeof(gd));
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003384 wpa_printf(MSG_DEBUG, "Failed to install the GTK in "
3385 "WNM mode");
3386 return -1;
3387 }
Dmitry Shmidt61593f02014-04-21 16:27:35 -07003388 os_memset(&gd, 0, sizeof(gd));
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003389#ifdef CONFIG_IEEE80211W
3390 } else if (subelem_id == WNM_SLEEP_SUBELEM_IGTK) {
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02003391 const struct wpa_igtk_kde *igtk;
Dmitry Shmidt61593f02014-04-21 16:27:35 -07003392
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02003393 igtk = (const struct wpa_igtk_kde *) (buf + 2);
Jouni Malinen58c0e962017-10-01 12:12:24 +03003394 if (wpa_supplicant_install_igtk(sm, igtk, 1) < 0)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003395 return -1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003396#endif /* CONFIG_IEEE80211W */
3397 } else {
3398 wpa_printf(MSG_DEBUG, "Unknown element id");
3399 return -1;
3400 }
3401
3402 return 0;
3403}
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003404#endif /* CONFIG_WNM */
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003405
3406
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003407#ifdef CONFIG_P2P
3408
3409int wpa_sm_get_p2p_ip_addr(struct wpa_sm *sm, u8 *buf)
3410{
3411 if (sm == NULL || WPA_GET_BE32(sm->p2p_ip_addr) == 0)
3412 return -1;
3413 os_memcpy(buf, sm->p2p_ip_addr, 3 * 4);
3414 return 0;
3415}
3416
3417#endif /* CONFIG_P2P */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003418
3419
3420void wpa_sm_set_rx_replay_ctr(struct wpa_sm *sm, const u8 *rx_replay_counter)
3421{
3422 if (rx_replay_counter == NULL)
3423 return;
3424
3425 os_memcpy(sm->rx_replay_counter, rx_replay_counter,
3426 WPA_REPLAY_COUNTER_LEN);
3427 sm->rx_replay_counter_set = 1;
3428 wpa_printf(MSG_DEBUG, "Updated key replay counter");
3429}
3430
3431
Dmitry Shmidt807291d2015-01-27 13:40:23 -08003432void wpa_sm_set_ptk_kck_kek(struct wpa_sm *sm,
3433 const u8 *ptk_kck, size_t ptk_kck_len,
3434 const u8 *ptk_kek, size_t ptk_kek_len)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003435{
Dmitry Shmidt807291d2015-01-27 13:40:23 -08003436 if (ptk_kck && ptk_kck_len <= WPA_KCK_MAX_LEN) {
3437 os_memcpy(sm->ptk.kck, ptk_kck, ptk_kck_len);
3438 sm->ptk.kck_len = ptk_kck_len;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003439 wpa_printf(MSG_DEBUG, "Updated PTK KCK");
3440 }
Dmitry Shmidt807291d2015-01-27 13:40:23 -08003441 if (ptk_kek && ptk_kek_len <= WPA_KEK_MAX_LEN) {
3442 os_memcpy(sm->ptk.kek, ptk_kek, ptk_kek_len);
3443 sm->ptk.kek_len = ptk_kek_len;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003444 wpa_printf(MSG_DEBUG, "Updated PTK KEK");
3445 }
3446 sm->ptk_set = 1;
3447}
Dmitry Shmidt55840ad2015-12-14 12:45:46 -08003448
3449
3450#ifdef CONFIG_TESTING_OPTIONS
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003451
Dmitry Shmidt55840ad2015-12-14 12:45:46 -08003452void wpa_sm_set_test_assoc_ie(struct wpa_sm *sm, struct wpabuf *buf)
3453{
3454 wpabuf_free(sm->test_assoc_ie);
3455 sm->test_assoc_ie = buf;
3456}
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003457
3458
3459const u8 * wpa_sm_get_anonce(struct wpa_sm *sm)
3460{
3461 return sm->anonce;
3462}
3463
Dmitry Shmidt55840ad2015-12-14 12:45:46 -08003464#endif /* CONFIG_TESTING_OPTIONS */
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003465
3466
Roshan Pius3a1667e2018-07-03 15:17:14 -07003467unsigned int wpa_sm_get_key_mgmt(struct wpa_sm *sm)
3468{
3469 return sm->key_mgmt;
3470}
3471
3472
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003473#ifdef CONFIG_FILS
3474
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003475struct wpabuf * fils_build_auth(struct wpa_sm *sm, int dh_group, const u8 *md)
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003476{
3477 struct wpabuf *buf = NULL;
3478 struct wpabuf *erp_msg;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003479 struct wpabuf *pub = NULL;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003480
3481 erp_msg = eapol_sm_build_erp_reauth_start(sm->eapol);
3482 if (!erp_msg && !sm->cur_pmksa) {
3483 wpa_printf(MSG_DEBUG,
3484 "FILS: Neither ERP EAP-Initiate/Re-auth nor PMKSA cache entry is available - skip FILS");
3485 goto fail;
3486 }
3487
3488 wpa_printf(MSG_DEBUG, "FILS: Try to use FILS (erp=%d pmksa_cache=%d)",
3489 erp_msg != NULL, sm->cur_pmksa != NULL);
3490
3491 sm->fils_completed = 0;
3492
3493 if (!sm->assoc_wpa_ie) {
3494 wpa_printf(MSG_INFO, "FILS: No own RSN IE set for FILS");
3495 goto fail;
3496 }
3497
3498 if (random_get_bytes(sm->fils_nonce, FILS_NONCE_LEN) < 0 ||
3499 random_get_bytes(sm->fils_session, FILS_SESSION_LEN) < 0)
3500 goto fail;
3501
3502 wpa_hexdump(MSG_DEBUG, "FILS: Generated FILS Nonce",
3503 sm->fils_nonce, FILS_NONCE_LEN);
3504 wpa_hexdump(MSG_DEBUG, "FILS: Generated FILS Session",
3505 sm->fils_session, FILS_SESSION_LEN);
3506
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003507#ifdef CONFIG_FILS_SK_PFS
3508 sm->fils_dh_group = dh_group;
3509 if (dh_group) {
3510 crypto_ecdh_deinit(sm->fils_ecdh);
3511 sm->fils_ecdh = crypto_ecdh_init(dh_group);
3512 if (!sm->fils_ecdh) {
3513 wpa_printf(MSG_INFO,
3514 "FILS: Could not initialize ECDH with group %d",
3515 dh_group);
3516 goto fail;
3517 }
3518 pub = crypto_ecdh_get_pubkey(sm->fils_ecdh, 1);
3519 if (!pub)
3520 goto fail;
3521 wpa_hexdump_buf(MSG_DEBUG, "FILS: Element (DH public key)",
3522 pub);
3523 sm->fils_dh_elem_len = wpabuf_len(pub);
3524 }
3525#endif /* CONFIG_FILS_SK_PFS */
3526
3527 buf = wpabuf_alloc(1000 + sm->assoc_wpa_ie_len +
3528 (pub ? wpabuf_len(pub) : 0));
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003529 if (!buf)
3530 goto fail;
3531
3532 /* Fields following the Authentication algorithm number field */
3533
3534 /* Authentication Transaction seq# */
3535 wpabuf_put_le16(buf, 1);
3536
3537 /* Status Code */
3538 wpabuf_put_le16(buf, WLAN_STATUS_SUCCESS);
3539
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003540 /* TODO: FILS PK */
3541#ifdef CONFIG_FILS_SK_PFS
3542 if (dh_group) {
3543 /* Finite Cyclic Group */
3544 wpabuf_put_le16(buf, dh_group);
3545 /* Element */
3546 wpabuf_put_buf(buf, pub);
3547 }
3548#endif /* CONFIG_FILS_SK_PFS */
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003549
3550 /* RSNE */
3551 wpa_hexdump(MSG_DEBUG, "FILS: RSNE in FILS Authentication frame",
3552 sm->assoc_wpa_ie, sm->assoc_wpa_ie_len);
3553 wpabuf_put_data(buf, sm->assoc_wpa_ie, sm->assoc_wpa_ie_len);
3554
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003555 if (md) {
3556 /* MDE when using FILS for FT initial association */
3557 struct rsn_mdie *mdie;
3558
3559 wpabuf_put_u8(buf, WLAN_EID_MOBILITY_DOMAIN);
3560 wpabuf_put_u8(buf, sizeof(*mdie));
3561 mdie = wpabuf_put(buf, sizeof(*mdie));
3562 os_memcpy(mdie->mobility_domain, md, MOBILITY_DOMAIN_ID_LEN);
3563 mdie->ft_capab = 0;
3564 }
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003565
3566 /* FILS Nonce */
3567 wpabuf_put_u8(buf, WLAN_EID_EXTENSION); /* Element ID */
3568 wpabuf_put_u8(buf, 1 + FILS_NONCE_LEN); /* Length */
3569 /* Element ID Extension */
3570 wpabuf_put_u8(buf, WLAN_EID_EXT_FILS_NONCE);
3571 wpabuf_put_data(buf, sm->fils_nonce, FILS_NONCE_LEN);
3572
3573 /* FILS Session */
3574 wpabuf_put_u8(buf, WLAN_EID_EXTENSION); /* Element ID */
3575 wpabuf_put_u8(buf, 1 + FILS_SESSION_LEN); /* Length */
3576 /* Element ID Extension */
3577 wpabuf_put_u8(buf, WLAN_EID_EXT_FILS_SESSION);
3578 wpabuf_put_data(buf, sm->fils_session, FILS_SESSION_LEN);
3579
3580 /* FILS Wrapped Data */
Paul Stewart092955c2017-02-06 09:13:09 -08003581 sm->fils_erp_pmkid_set = 0;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003582 if (erp_msg) {
3583 wpabuf_put_u8(buf, WLAN_EID_EXTENSION); /* Element ID */
3584 wpabuf_put_u8(buf, 1 + wpabuf_len(erp_msg)); /* Length */
3585 /* Element ID Extension */
3586 wpabuf_put_u8(buf, WLAN_EID_EXT_FILS_WRAPPED_DATA);
3587 wpabuf_put_buf(buf, erp_msg);
Paul Stewart092955c2017-02-06 09:13:09 -08003588 /* Calculate pending PMKID here so that we do not need to
3589 * maintain a copy of the EAP-Initiate/Reauth message. */
3590 if (fils_pmkid_erp(sm->key_mgmt, wpabuf_head(erp_msg),
3591 wpabuf_len(erp_msg),
3592 sm->fils_erp_pmkid) == 0)
3593 sm->fils_erp_pmkid_set = 1;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003594 }
3595
3596 wpa_hexdump_buf(MSG_DEBUG, "RSN: FILS fields for Authentication frame",
3597 buf);
3598
3599fail:
3600 wpabuf_free(erp_msg);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003601 wpabuf_free(pub);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003602 return buf;
3603}
3604
3605
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003606int fils_process_auth(struct wpa_sm *sm, const u8 *bssid, const u8 *data,
3607 size_t len)
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003608{
3609 const u8 *pos, *end;
3610 struct ieee802_11_elems elems;
3611 struct wpa_ie_data rsn;
3612 int pmkid_match = 0;
3613 u8 ick[FILS_ICK_MAX_LEN];
3614 size_t ick_len;
3615 int res;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003616 struct wpabuf *dh_ss = NULL;
3617 const u8 *g_sta = NULL;
3618 size_t g_sta_len = 0;
3619 const u8 *g_ap = NULL;
3620 size_t g_ap_len = 0;
3621 struct wpabuf *pub = NULL;
3622
3623 os_memcpy(sm->bssid, bssid, ETH_ALEN);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003624
3625 wpa_hexdump(MSG_DEBUG, "FILS: Authentication frame fields",
3626 data, len);
3627 pos = data;
3628 end = data + len;
3629
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003630 /* TODO: FILS PK */
3631#ifdef CONFIG_FILS_SK_PFS
3632 if (sm->fils_dh_group) {
3633 u16 group;
3634
3635 /* Using FILS PFS */
3636
3637 /* Finite Cyclic Group */
3638 if (end - pos < 2) {
3639 wpa_printf(MSG_DEBUG,
3640 "FILS: No room for Finite Cyclic Group");
3641 goto fail;
3642 }
3643 group = WPA_GET_LE16(pos);
3644 pos += 2;
3645 if (group != sm->fils_dh_group) {
3646 wpa_printf(MSG_DEBUG,
3647 "FILS: Unexpected change in Finite Cyclic Group: %u (expected %u)",
3648 group, sm->fils_dh_group);
3649 goto fail;
3650 }
3651
3652 /* Element */
3653 if ((size_t) (end - pos) < sm->fils_dh_elem_len) {
3654 wpa_printf(MSG_DEBUG, "FILS: No room for Element");
3655 goto fail;
3656 }
3657
3658 if (!sm->fils_ecdh) {
3659 wpa_printf(MSG_DEBUG, "FILS: No ECDH state available");
3660 goto fail;
3661 }
3662 dh_ss = crypto_ecdh_set_peerkey(sm->fils_ecdh, 1, pos,
3663 sm->fils_dh_elem_len);
3664 if (!dh_ss) {
3665 wpa_printf(MSG_DEBUG, "FILS: ECDH operation failed");
3666 goto fail;
3667 }
3668 wpa_hexdump_buf_key(MSG_DEBUG, "FILS: DH_SS", dh_ss);
3669 g_ap = pos;
3670 g_ap_len = sm->fils_dh_elem_len;
3671 pos += sm->fils_dh_elem_len;
3672 }
3673#endif /* CONFIG_FILS_SK_PFS */
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003674
3675 wpa_hexdump(MSG_DEBUG, "FILS: Remaining IEs", pos, end - pos);
3676 if (ieee802_11_parse_elems(pos, end - pos, &elems, 1) == ParseFailed) {
3677 wpa_printf(MSG_DEBUG, "FILS: Could not parse elements");
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003678 goto fail;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003679 }
3680
3681 /* RSNE */
3682 wpa_hexdump(MSG_DEBUG, "FILS: RSN element", elems.rsn_ie,
3683 elems.rsn_ie_len);
3684 if (!elems.rsn_ie ||
3685 wpa_parse_wpa_ie_rsn(elems.rsn_ie - 2, elems.rsn_ie_len + 2,
3686 &rsn) < 0) {
3687 wpa_printf(MSG_DEBUG, "FILS: No RSN element");
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003688 goto fail;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003689 }
3690
3691 if (!elems.fils_nonce) {
3692 wpa_printf(MSG_DEBUG, "FILS: No FILS Nonce field");
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003693 goto fail;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003694 }
3695 os_memcpy(sm->fils_anonce, elems.fils_nonce, FILS_NONCE_LEN);
3696 wpa_hexdump(MSG_DEBUG, "FILS: ANonce", sm->fils_anonce, FILS_NONCE_LEN);
3697
Roshan Pius3a1667e2018-07-03 15:17:14 -07003698#ifdef CONFIG_IEEE80211R
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003699 if (wpa_key_mgmt_ft(sm->key_mgmt)) {
3700 struct wpa_ft_ies parse;
3701
3702 if (!elems.mdie || !elems.ftie) {
3703 wpa_printf(MSG_DEBUG, "FILS+FT: No MDE or FTE");
3704 goto fail;
3705 }
3706
Roshan Pius3a1667e2018-07-03 15:17:14 -07003707 if (wpa_ft_parse_ies(pos, end - pos, &parse,
3708 wpa_key_mgmt_sha384(sm->key_mgmt)) < 0) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003709 wpa_printf(MSG_DEBUG, "FILS+FT: Failed to parse IEs");
3710 goto fail;
3711 }
3712
3713 if (!parse.r0kh_id) {
3714 wpa_printf(MSG_DEBUG,
3715 "FILS+FT: No R0KH-ID subelem in FTE");
3716 goto fail;
3717 }
3718 os_memcpy(sm->r0kh_id, parse.r0kh_id, parse.r0kh_id_len);
3719 sm->r0kh_id_len = parse.r0kh_id_len;
3720 wpa_hexdump_ascii(MSG_DEBUG, "FILS+FT: R0KH-ID",
3721 sm->r0kh_id, sm->r0kh_id_len);
3722
3723 if (!parse.r1kh_id) {
3724 wpa_printf(MSG_DEBUG,
3725 "FILS+FT: No R1KH-ID subelem in FTE");
3726 goto fail;
3727 }
3728 os_memcpy(sm->r1kh_id, parse.r1kh_id, FT_R1KH_ID_LEN);
3729 wpa_hexdump(MSG_DEBUG, "FILS+FT: R1KH-ID",
3730 sm->r1kh_id, FT_R1KH_ID_LEN);
3731
3732 /* TODO: Check MDE and FTE payload */
3733
3734 wpabuf_free(sm->fils_ft_ies);
3735 sm->fils_ft_ies = wpabuf_alloc(2 + elems.mdie_len +
3736 2 + elems.ftie_len);
3737 if (!sm->fils_ft_ies)
3738 goto fail;
3739 wpabuf_put_data(sm->fils_ft_ies, elems.mdie - 2,
3740 2 + elems.mdie_len);
3741 wpabuf_put_data(sm->fils_ft_ies, elems.ftie - 2,
3742 2 + elems.ftie_len);
3743 } else {
3744 wpabuf_free(sm->fils_ft_ies);
3745 sm->fils_ft_ies = NULL;
3746 }
Roshan Pius3a1667e2018-07-03 15:17:14 -07003747#endif /* CONFIG_IEEE80211R */
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003748
3749 /* PMKID List */
3750 if (rsn.pmkid && rsn.num_pmkid > 0) {
3751 wpa_hexdump(MSG_DEBUG, "FILS: PMKID List",
3752 rsn.pmkid, rsn.num_pmkid * PMKID_LEN);
3753
3754 if (rsn.num_pmkid != 1) {
3755 wpa_printf(MSG_DEBUG, "FILS: Invalid PMKID selection");
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003756 goto fail;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003757 }
3758 wpa_hexdump(MSG_DEBUG, "FILS: PMKID", rsn.pmkid, PMKID_LEN);
3759 if (os_memcmp(sm->cur_pmksa->pmkid, rsn.pmkid, PMKID_LEN) != 0)
3760 {
3761 wpa_printf(MSG_DEBUG, "FILS: PMKID mismatch");
3762 wpa_hexdump(MSG_DEBUG, "FILS: Expected PMKID",
3763 sm->cur_pmksa->pmkid, PMKID_LEN);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003764 goto fail;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003765 }
3766 wpa_printf(MSG_DEBUG,
3767 "FILS: Matching PMKID - continue using PMKSA caching");
3768 pmkid_match = 1;
3769 }
3770 if (!pmkid_match && sm->cur_pmksa) {
3771 wpa_printf(MSG_DEBUG,
3772 "FILS: No PMKID match - cannot use cached PMKSA entry");
3773 sm->cur_pmksa = NULL;
3774 }
3775
3776 /* FILS Session */
3777 if (!elems.fils_session) {
3778 wpa_printf(MSG_DEBUG, "FILS: No FILS Session element");
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003779 goto fail;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003780 }
3781 wpa_hexdump(MSG_DEBUG, "FILS: FILS Session", elems.fils_session,
3782 FILS_SESSION_LEN);
3783 if (os_memcmp(sm->fils_session, elems.fils_session, FILS_SESSION_LEN)
3784 != 0) {
3785 wpa_printf(MSG_DEBUG, "FILS: Session mismatch");
3786 wpa_hexdump(MSG_DEBUG, "FILS: Expected FILS Session",
3787 sm->fils_session, FILS_SESSION_LEN);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003788 goto fail;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003789 }
3790
3791 /* FILS Wrapped Data */
3792 if (!sm->cur_pmksa && elems.fils_wrapped_data) {
Paul Stewart092955c2017-02-06 09:13:09 -08003793 u8 rmsk[ERP_MAX_KEY_LEN];
3794 size_t rmsk_len;
3795
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003796 wpa_hexdump(MSG_DEBUG, "FILS: Wrapped Data",
3797 elems.fils_wrapped_data,
3798 elems.fils_wrapped_data_len);
3799 eapol_sm_process_erp_finish(sm->eapol, elems.fils_wrapped_data,
3800 elems.fils_wrapped_data_len);
3801 if (eapol_sm_failed(sm->eapol))
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003802 goto fail;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003803
Paul Stewart092955c2017-02-06 09:13:09 -08003804 rmsk_len = ERP_MAX_KEY_LEN;
3805 res = eapol_sm_get_key(sm->eapol, rmsk, rmsk_len);
3806 if (res == PMK_LEN) {
3807 rmsk_len = PMK_LEN;
3808 res = eapol_sm_get_key(sm->eapol, rmsk, rmsk_len);
3809 }
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003810 if (res)
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003811 goto fail;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003812
Paul Stewart092955c2017-02-06 09:13:09 -08003813 res = fils_rmsk_to_pmk(sm->key_mgmt, rmsk, rmsk_len,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003814 sm->fils_nonce, sm->fils_anonce,
3815 dh_ss ? wpabuf_head(dh_ss) : NULL,
3816 dh_ss ? wpabuf_len(dh_ss) : 0,
Paul Stewart092955c2017-02-06 09:13:09 -08003817 sm->pmk, &sm->pmk_len);
3818 os_memset(rmsk, 0, sizeof(rmsk));
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003819
3820 /* Don't use DHss in PTK derivation if PMKSA caching is not
3821 * used. */
3822 wpabuf_clear_free(dh_ss);
3823 dh_ss = NULL;
3824
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08003825 if (res)
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003826 goto fail;
Paul Stewart092955c2017-02-06 09:13:09 -08003827
3828 if (!sm->fils_erp_pmkid_set) {
3829 wpa_printf(MSG_DEBUG, "FILS: PMKID not available");
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003830 goto fail;
Paul Stewart092955c2017-02-06 09:13:09 -08003831 }
3832 wpa_hexdump(MSG_DEBUG, "FILS: PMKID", sm->fils_erp_pmkid,
3833 PMKID_LEN);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003834 wpa_printf(MSG_DEBUG, "FILS: ERP processing succeeded - add PMKSA cache entry for the result");
Paul Stewart092955c2017-02-06 09:13:09 -08003835 sm->cur_pmksa = pmksa_cache_add(sm->pmksa, sm->pmk, sm->pmk_len,
3836 sm->fils_erp_pmkid, NULL, 0,
3837 sm->bssid, sm->own_addr,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003838 sm->network_ctx, sm->key_mgmt,
3839 NULL);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003840 }
3841
3842 if (!sm->cur_pmksa) {
3843 wpa_printf(MSG_DEBUG,
3844 "FILS: No remaining options to continue FILS authentication");
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003845 goto fail;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003846 }
3847
3848 if (fils_pmk_to_ptk(sm->pmk, sm->pmk_len, sm->own_addr, sm->bssid,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003849 sm->fils_nonce, sm->fils_anonce,
3850 dh_ss ? wpabuf_head(dh_ss) : NULL,
3851 dh_ss ? wpabuf_len(dh_ss) : 0,
3852 &sm->ptk, ick, &ick_len,
3853 sm->key_mgmt, sm->pairwise_cipher,
3854 sm->fils_ft, &sm->fils_ft_len) < 0) {
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003855 wpa_printf(MSG_DEBUG, "FILS: Failed to derive PTK");
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003856 goto fail;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003857 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003858
3859 wpabuf_clear_free(dh_ss);
3860 dh_ss = NULL;
3861
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003862 sm->ptk_set = 1;
3863 sm->tptk_set = 0;
3864 os_memset(&sm->tptk, 0, sizeof(sm->tptk));
3865
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003866#ifdef CONFIG_FILS_SK_PFS
3867 if (sm->fils_dh_group) {
3868 if (!sm->fils_ecdh) {
3869 wpa_printf(MSG_INFO, "FILS: ECDH not initialized");
3870 goto fail;
3871 }
3872 pub = crypto_ecdh_get_pubkey(sm->fils_ecdh, 1);
3873 if (!pub)
3874 goto fail;
3875 wpa_hexdump_buf(MSG_DEBUG, "FILS: gSTA", pub);
3876 g_sta = wpabuf_head(pub);
3877 g_sta_len = wpabuf_len(pub);
3878 if (!g_ap) {
3879 wpa_printf(MSG_INFO, "FILS: gAP not available");
3880 goto fail;
3881 }
3882 wpa_hexdump(MSG_DEBUG, "FILS: gAP", g_ap, g_ap_len);
3883 }
3884#endif /* CONFIG_FILS_SK_PFS */
3885
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003886 res = fils_key_auth_sk(ick, ick_len, sm->fils_nonce,
3887 sm->fils_anonce, sm->own_addr, sm->bssid,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003888 g_sta, g_sta_len, g_ap, g_ap_len,
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003889 sm->key_mgmt, sm->fils_key_auth_sta,
3890 sm->fils_key_auth_ap,
3891 &sm->fils_key_auth_len);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003892 wpabuf_free(pub);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003893 os_memset(ick, 0, sizeof(ick));
3894 return res;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003895fail:
3896 wpabuf_free(pub);
3897 wpabuf_clear_free(dh_ss);
3898 return -1;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003899}
3900
3901
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003902#ifdef CONFIG_IEEE80211R
3903static int fils_ft_build_assoc_req_rsne(struct wpa_sm *sm, struct wpabuf *buf)
3904{
3905 struct rsn_ie_hdr *rsnie;
3906 u16 capab;
3907 u8 *pos;
Roshan Pius3a1667e2018-07-03 15:17:14 -07003908 int use_sha384 = wpa_key_mgmt_sha384(sm->key_mgmt);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003909
3910 /* RSNIE[PMKR0Name/PMKR1Name] */
3911 rsnie = wpabuf_put(buf, sizeof(*rsnie));
3912 rsnie->elem_id = WLAN_EID_RSN;
3913 WPA_PUT_LE16(rsnie->version, RSN_VERSION);
3914
3915 /* Group Suite Selector */
3916 if (!wpa_cipher_valid_group(sm->group_cipher)) {
3917 wpa_printf(MSG_WARNING, "FT: Invalid group cipher (%d)",
3918 sm->group_cipher);
3919 return -1;
3920 }
3921 pos = wpabuf_put(buf, RSN_SELECTOR_LEN);
3922 RSN_SELECTOR_PUT(pos, wpa_cipher_to_suite(WPA_PROTO_RSN,
3923 sm->group_cipher));
3924
3925 /* Pairwise Suite Count */
3926 wpabuf_put_le16(buf, 1);
3927
3928 /* Pairwise Suite List */
3929 if (!wpa_cipher_valid_pairwise(sm->pairwise_cipher)) {
3930 wpa_printf(MSG_WARNING, "FT: Invalid pairwise cipher (%d)",
3931 sm->pairwise_cipher);
3932 return -1;
3933 }
3934 pos = wpabuf_put(buf, RSN_SELECTOR_LEN);
3935 RSN_SELECTOR_PUT(pos, wpa_cipher_to_suite(WPA_PROTO_RSN,
3936 sm->pairwise_cipher));
3937
3938 /* Authenticated Key Management Suite Count */
3939 wpabuf_put_le16(buf, 1);
3940
3941 /* Authenticated Key Management Suite List */
3942 pos = wpabuf_put(buf, RSN_SELECTOR_LEN);
3943 if (sm->key_mgmt == WPA_KEY_MGMT_FT_FILS_SHA256)
3944 RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_FT_FILS_SHA256);
3945 else if (sm->key_mgmt == WPA_KEY_MGMT_FT_FILS_SHA384)
3946 RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_FT_FILS_SHA384);
3947 else {
3948 wpa_printf(MSG_WARNING,
3949 "FILS+FT: Invalid key management type (%d)",
3950 sm->key_mgmt);
3951 return -1;
3952 }
3953
3954 /* RSN Capabilities */
3955 capab = 0;
3956#ifdef CONFIG_IEEE80211W
3957 if (sm->mgmt_group_cipher == WPA_CIPHER_AES_128_CMAC)
3958 capab |= WPA_CAPABILITY_MFPC;
3959#endif /* CONFIG_IEEE80211W */
Hai Shalom74f70d42019-02-11 14:42:39 -08003960 if (sm->ocv)
3961 capab |= WPA_CAPABILITY_OCVC;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003962 wpabuf_put_le16(buf, capab);
3963
3964 /* PMKID Count */
3965 wpabuf_put_le16(buf, 1);
3966
3967 /* PMKID List [PMKR1Name] */
3968 wpa_hexdump_key(MSG_DEBUG, "FILS+FT: XXKey (FILS-FT)",
3969 sm->fils_ft, sm->fils_ft_len);
3970 wpa_hexdump_ascii(MSG_DEBUG, "FILS+FT: SSID", sm->ssid, sm->ssid_len);
3971 wpa_hexdump(MSG_DEBUG, "FILS+FT: MDID",
3972 sm->mobility_domain, MOBILITY_DOMAIN_ID_LEN);
3973 wpa_hexdump_ascii(MSG_DEBUG, "FILS+FT: R0KH-ID",
3974 sm->r0kh_id, sm->r0kh_id_len);
3975 if (wpa_derive_pmk_r0(sm->fils_ft, sm->fils_ft_len, sm->ssid,
3976 sm->ssid_len, sm->mobility_domain,
3977 sm->r0kh_id, sm->r0kh_id_len, sm->own_addr,
Roshan Pius3a1667e2018-07-03 15:17:14 -07003978 sm->pmk_r0, sm->pmk_r0_name, use_sha384) < 0) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003979 wpa_printf(MSG_WARNING, "FILS+FT: Could not derive PMK-R0");
3980 return -1;
3981 }
Roshan Pius3a1667e2018-07-03 15:17:14 -07003982 sm->pmk_r0_len = use_sha384 ? SHA384_MAC_LEN : PMK_LEN;
3983 wpa_hexdump_key(MSG_DEBUG, "FILS+FT: PMK-R0",
3984 sm->pmk_r0, sm->pmk_r0_len);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003985 wpa_hexdump(MSG_DEBUG, "FILS+FT: PMKR0Name",
3986 sm->pmk_r0_name, WPA_PMK_NAME_LEN);
3987 wpa_printf(MSG_DEBUG, "FILS+FT: R1KH-ID: " MACSTR,
3988 MAC2STR(sm->r1kh_id));
3989 pos = wpabuf_put(buf, WPA_PMK_NAME_LEN);
3990 if (wpa_derive_pmk_r1_name(sm->pmk_r0_name, sm->r1kh_id, sm->own_addr,
Roshan Pius3a1667e2018-07-03 15:17:14 -07003991 pos, use_sha384) < 0) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003992 wpa_printf(MSG_WARNING, "FILS+FT: Could not derive PMKR1Name");
3993 return -1;
3994 }
3995 wpa_hexdump(MSG_DEBUG, "FILS+FT: PMKR1Name", pos, WPA_PMK_NAME_LEN);
3996
3997#ifdef CONFIG_IEEE80211W
3998 if (sm->mgmt_group_cipher == WPA_CIPHER_AES_128_CMAC) {
3999 /* Management Group Cipher Suite */
4000 pos = wpabuf_put(buf, RSN_SELECTOR_LEN);
4001 RSN_SELECTOR_PUT(pos, RSN_CIPHER_SUITE_AES_128_CMAC);
4002 }
4003#endif /* CONFIG_IEEE80211W */
4004
4005 rsnie->len = ((u8 *) wpabuf_put(buf, 0) - (u8 *) rsnie) - 2;
4006 return 0;
4007}
4008#endif /* CONFIG_IEEE80211R */
4009
4010
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004011struct wpabuf * fils_build_assoc_req(struct wpa_sm *sm, const u8 **kek,
4012 size_t *kek_len, const u8 **snonce,
Paul Stewart092955c2017-02-06 09:13:09 -08004013 const u8 **anonce,
4014 const struct wpabuf **hlp,
4015 unsigned int num_hlp)
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004016{
4017 struct wpabuf *buf;
Paul Stewart092955c2017-02-06 09:13:09 -08004018 size_t len;
4019 unsigned int i;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004020
Paul Stewart092955c2017-02-06 09:13:09 -08004021 len = 1000;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004022#ifdef CONFIG_IEEE80211R
4023 if (sm->fils_ft_ies)
4024 len += wpabuf_len(sm->fils_ft_ies);
4025 if (wpa_key_mgmt_ft(sm->key_mgmt))
4026 len += 256;
4027#endif /* CONFIG_IEEE80211R */
Paul Stewart092955c2017-02-06 09:13:09 -08004028 for (i = 0; hlp && i < num_hlp; i++)
4029 len += 10 + wpabuf_len(hlp[i]);
4030 buf = wpabuf_alloc(len);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004031 if (!buf)
4032 return NULL;
4033
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004034#ifdef CONFIG_IEEE80211R
4035 if (wpa_key_mgmt_ft(sm->key_mgmt) && sm->fils_ft_ies) {
4036 /* MDE and FTE when using FILS+FT */
4037 wpabuf_put_buf(buf, sm->fils_ft_ies);
4038 /* RSNE with PMKR1Name in PMKID field */
4039 if (fils_ft_build_assoc_req_rsne(sm, buf) < 0) {
4040 wpabuf_free(buf);
4041 return NULL;
4042 }
4043 }
4044#endif /* CONFIG_IEEE80211R */
4045
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004046 /* FILS Session */
4047 wpabuf_put_u8(buf, WLAN_EID_EXTENSION); /* Element ID */
4048 wpabuf_put_u8(buf, 1 + FILS_SESSION_LEN); /* Length */
4049 /* Element ID Extension */
4050 wpabuf_put_u8(buf, WLAN_EID_EXT_FILS_SESSION);
4051 wpabuf_put_data(buf, sm->fils_session, FILS_SESSION_LEN);
4052
4053 /* Everything after FILS Session element gets encrypted in the driver
4054 * with KEK. The buffer returned from here is the plaintext version. */
4055
4056 /* TODO: FILS Public Key */
4057
4058 /* FILS Key Confirm */
4059 wpabuf_put_u8(buf, WLAN_EID_EXTENSION); /* Element ID */
4060 wpabuf_put_u8(buf, 1 + sm->fils_key_auth_len); /* Length */
4061 /* Element ID Extension */
4062 wpabuf_put_u8(buf, WLAN_EID_EXT_FILS_KEY_CONFIRM);
4063 wpabuf_put_data(buf, sm->fils_key_auth_sta, sm->fils_key_auth_len);
4064
Paul Stewart092955c2017-02-06 09:13:09 -08004065 /* FILS HLP Container */
4066 for (i = 0; hlp && i < num_hlp; i++) {
4067 const u8 *pos = wpabuf_head(hlp[i]);
4068 size_t left = wpabuf_len(hlp[i]);
4069
4070 wpabuf_put_u8(buf, WLAN_EID_EXTENSION); /* Element ID */
4071 if (left <= 254)
4072 len = 1 + left;
4073 else
4074 len = 255;
4075 wpabuf_put_u8(buf, len); /* Length */
4076 /* Element ID Extension */
4077 wpabuf_put_u8(buf, WLAN_EID_EXT_FILS_HLP_CONTAINER);
4078 /* Destination MAC Address, Source MAC Address, HLP Packet.
4079 * HLP Packet is in MSDU format (i.e., included the LLC/SNAP
4080 * header when LPD is used). */
4081 wpabuf_put_data(buf, pos, len - 1);
4082 pos += len - 1;
4083 left -= len - 1;
4084 while (left) {
4085 wpabuf_put_u8(buf, WLAN_EID_FRAGMENT);
4086 len = left > 255 ? 255 : left;
4087 wpabuf_put_u8(buf, len);
4088 wpabuf_put_data(buf, pos, len);
4089 pos += len;
4090 left -= len;
4091 }
4092 }
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004093
4094 /* TODO: FILS IP Address Assignment */
4095
Hai Shalom74f70d42019-02-11 14:42:39 -08004096#ifdef CONFIG_OCV
4097 if (wpa_sm_ocv_enabled(sm)) {
4098 struct wpa_channel_info ci;
4099 u8 *pos;
4100
4101 if (wpa_sm_channel_info(sm, &ci) != 0) {
4102 wpa_printf(MSG_WARNING,
4103 "FILS: Failed to get channel info for OCI element");
4104 wpabuf_free(buf);
4105 return NULL;
4106 }
4107
4108 pos = wpabuf_put(buf, OCV_OCI_EXTENDED_LEN);
4109 if (ocv_insert_extended_oci(&ci, pos) < 0) {
4110 wpabuf_free(buf);
4111 return NULL;
4112 }
4113 }
4114#endif /* CONFIG_OCV */
4115
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004116 wpa_hexdump_buf(MSG_DEBUG, "FILS: Association Request plaintext", buf);
4117
4118 *kek = sm->ptk.kek;
4119 *kek_len = sm->ptk.kek_len;
4120 wpa_hexdump_key(MSG_DEBUG, "FILS: KEK for AEAD", *kek, *kek_len);
4121 *snonce = sm->fils_nonce;
4122 wpa_hexdump(MSG_DEBUG, "FILS: SNonce for AEAD AAD",
4123 *snonce, FILS_NONCE_LEN);
4124 *anonce = sm->fils_anonce;
4125 wpa_hexdump(MSG_DEBUG, "FILS: ANonce for AEAD AAD",
4126 *anonce, FILS_NONCE_LEN);
4127
4128 return buf;
4129}
4130
4131
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08004132static void fils_process_hlp_resp(struct wpa_sm *sm, const u8 *resp, size_t len)
4133{
4134 const u8 *pos, *end;
4135
4136 wpa_hexdump(MSG_MSGDUMP, "FILS: HLP response", resp, len);
4137 if (len < 2 * ETH_ALEN)
4138 return;
4139 pos = resp + 2 * ETH_ALEN;
4140 end = resp + len;
4141 if (end - pos >= 6 &&
4142 os_memcmp(pos, "\xaa\xaa\x03\x00\x00\x00", 6) == 0)
4143 pos += 6; /* Remove SNAP/LLC header */
4144 wpa_sm_fils_hlp_rx(sm, resp, resp + ETH_ALEN, pos, end - pos);
4145}
4146
4147
4148static void fils_process_hlp_container(struct wpa_sm *sm, const u8 *pos,
4149 size_t len)
4150{
4151 const u8 *end = pos + len;
4152 u8 *tmp, *tmp_pos;
4153
4154 /* Check if there are any FILS HLP Container elements */
4155 while (end - pos >= 2) {
4156 if (2 + pos[1] > end - pos)
4157 return;
4158 if (pos[0] == WLAN_EID_EXTENSION &&
4159 pos[1] >= 1 + 2 * ETH_ALEN &&
4160 pos[2] == WLAN_EID_EXT_FILS_HLP_CONTAINER)
4161 break;
4162 pos += 2 + pos[1];
4163 }
4164 if (end - pos < 2)
4165 return; /* No FILS HLP Container elements */
4166
4167 tmp = os_malloc(end - pos);
4168 if (!tmp)
4169 return;
4170
4171 while (end - pos >= 2) {
4172 if (2 + pos[1] > end - pos ||
4173 pos[0] != WLAN_EID_EXTENSION ||
4174 pos[1] < 1 + 2 * ETH_ALEN ||
4175 pos[2] != WLAN_EID_EXT_FILS_HLP_CONTAINER)
4176 break;
4177 tmp_pos = tmp;
4178 os_memcpy(tmp_pos, pos + 3, pos[1] - 1);
4179 tmp_pos += pos[1] - 1;
4180 pos += 2 + pos[1];
4181
4182 /* Add possible fragments */
4183 while (end - pos >= 2 && pos[0] == WLAN_EID_FRAGMENT &&
4184 2 + pos[1] <= end - pos) {
4185 os_memcpy(tmp_pos, pos + 2, pos[1]);
4186 tmp_pos += pos[1];
4187 pos += 2 + pos[1];
4188 }
4189
4190 fils_process_hlp_resp(sm, tmp, tmp_pos - tmp);
4191 }
4192
4193 os_free(tmp);
4194}
4195
4196
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004197int fils_process_assoc_resp(struct wpa_sm *sm, const u8 *resp, size_t len)
4198{
4199 const struct ieee80211_mgmt *mgmt;
4200 const u8 *end, *ie_start;
4201 struct ieee802_11_elems elems;
4202 int keylen, rsclen;
4203 enum wpa_alg alg;
4204 struct wpa_gtk_data gd;
4205 int maxkeylen;
4206 struct wpa_eapol_ie_parse kde;
4207
4208 if (!sm || !sm->ptk_set) {
4209 wpa_printf(MSG_DEBUG, "FILS: No KEK available");
4210 return -1;
4211 }
4212
4213 if (!wpa_key_mgmt_fils(sm->key_mgmt)) {
4214 wpa_printf(MSG_DEBUG, "FILS: Not a FILS AKM");
4215 return -1;
4216 }
4217
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004218 if (sm->fils_completed) {
4219 wpa_printf(MSG_DEBUG,
4220 "FILS: Association has already been completed for this FILS authentication - ignore unexpected retransmission");
4221 return -1;
4222 }
4223
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004224 wpa_hexdump(MSG_DEBUG, "FILS: (Re)Association Response frame",
4225 resp, len);
4226
4227 mgmt = (const struct ieee80211_mgmt *) resp;
4228 if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.assoc_resp))
4229 return -1;
4230
4231 end = resp + len;
4232 /* Same offset for Association Response and Reassociation Response */
4233 ie_start = mgmt->u.assoc_resp.variable;
4234
4235 if (ieee802_11_parse_elems(ie_start, end - ie_start, &elems, 1) ==
4236 ParseFailed) {
4237 wpa_printf(MSG_DEBUG,
4238 "FILS: Failed to parse decrypted elements");
4239 goto fail;
4240 }
4241
4242 if (!elems.fils_session) {
4243 wpa_printf(MSG_DEBUG, "FILS: No FILS Session element");
4244 return -1;
4245 }
4246 if (os_memcmp(elems.fils_session, sm->fils_session,
4247 FILS_SESSION_LEN) != 0) {
4248 wpa_printf(MSG_DEBUG, "FILS: FILS Session mismatch");
4249 wpa_hexdump(MSG_DEBUG, "FILS: Received FILS Session",
4250 elems.fils_session, FILS_SESSION_LEN);
4251 wpa_hexdump(MSG_DEBUG, "FILS: Expected FILS Session",
4252 sm->fils_session, FILS_SESSION_LEN);
4253 }
4254
4255 /* TODO: FILS Public Key */
4256
4257 if (!elems.fils_key_confirm) {
4258 wpa_printf(MSG_DEBUG, "FILS: No FILS Key Confirm element");
4259 goto fail;
4260 }
4261 if (elems.fils_key_confirm_len != sm->fils_key_auth_len) {
4262 wpa_printf(MSG_DEBUG,
4263 "FILS: Unexpected Key-Auth length %d (expected %d)",
4264 elems.fils_key_confirm_len,
4265 (int) sm->fils_key_auth_len);
4266 goto fail;
4267 }
4268 if (os_memcmp(elems.fils_key_confirm, sm->fils_key_auth_ap,
4269 sm->fils_key_auth_len) != 0) {
4270 wpa_printf(MSG_DEBUG, "FILS: Key-Auth mismatch");
4271 wpa_hexdump(MSG_DEBUG, "FILS: Received Key-Auth",
4272 elems.fils_key_confirm,
4273 elems.fils_key_confirm_len);
4274 wpa_hexdump(MSG_DEBUG, "FILS: Expected Key-Auth",
4275 sm->fils_key_auth_ap, sm->fils_key_auth_len);
4276 goto fail;
4277 }
4278
Hai Shalom74f70d42019-02-11 14:42:39 -08004279#ifdef CONFIG_OCV
4280 if (wpa_sm_ocv_enabled(sm)) {
4281 struct wpa_channel_info ci;
4282
4283 if (wpa_sm_channel_info(sm, &ci) != 0) {
4284 wpa_printf(MSG_WARNING,
4285 "Failed to get channel info to validate received OCI in FILS (Re)Association Response frame");
4286 goto fail;
4287 }
4288
4289 if (ocv_verify_tx_params(elems.oci, elems.oci_len, &ci,
4290 channel_width_to_int(ci.chanwidth),
4291 ci.seg1_idx) != 0) {
4292 wpa_printf(MSG_WARNING, "FILS: %s", ocv_errorstr);
4293 goto fail;
4294 }
4295 }
4296#endif /* CONFIG_OCV */
4297
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004298 /* Key Delivery */
4299 if (!elems.key_delivery) {
4300 wpa_printf(MSG_DEBUG, "FILS: No Key Delivery element");
4301 goto fail;
4302 }
4303
4304 /* Parse GTK and set the key to the driver */
4305 os_memset(&gd, 0, sizeof(gd));
4306 if (wpa_supplicant_parse_ies(elems.key_delivery + WPA_KEY_RSC_LEN,
4307 elems.key_delivery_len - WPA_KEY_RSC_LEN,
4308 &kde) < 0) {
4309 wpa_printf(MSG_DEBUG, "FILS: Failed to parse KDEs");
4310 goto fail;
4311 }
4312 if (!kde.gtk) {
4313 wpa_printf(MSG_DEBUG, "FILS: No GTK KDE");
4314 goto fail;
4315 }
4316 maxkeylen = gd.gtk_len = kde.gtk_len - 2;
4317 if (wpa_supplicant_check_group_cipher(sm, sm->group_cipher,
4318 gd.gtk_len, maxkeylen,
4319 &gd.key_rsc_len, &gd.alg))
4320 goto fail;
4321
4322 wpa_hexdump_key(MSG_DEBUG, "FILS: Received GTK", kde.gtk, kde.gtk_len);
4323 gd.keyidx = kde.gtk[0] & 0x3;
4324 gd.tx = wpa_supplicant_gtk_tx_bit_workaround(sm,
4325 !!(kde.gtk[0] & BIT(2)));
4326 if (kde.gtk_len - 2 > sizeof(gd.gtk)) {
4327 wpa_printf(MSG_DEBUG, "FILS: Too long GTK in GTK KDE (len=%lu)",
4328 (unsigned long) kde.gtk_len - 2);
4329 goto fail;
4330 }
4331 os_memcpy(gd.gtk, kde.gtk + 2, kde.gtk_len - 2);
4332
4333 wpa_printf(MSG_DEBUG, "FILS: Set GTK to driver");
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004334 if (wpa_supplicant_install_gtk(sm, &gd, elems.key_delivery, 0) < 0) {
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004335 wpa_printf(MSG_DEBUG, "FILS: Failed to set GTK");
4336 goto fail;
4337 }
4338
4339 if (ieee80211w_set_keys(sm, &kde) < 0) {
4340 wpa_printf(MSG_DEBUG, "FILS: Failed to set IGTK");
4341 goto fail;
4342 }
4343
4344 alg = wpa_cipher_to_alg(sm->pairwise_cipher);
4345 keylen = wpa_cipher_key_len(sm->pairwise_cipher);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004346 if (keylen <= 0 || (unsigned int) keylen != sm->ptk.tk_len) {
4347 wpa_printf(MSG_DEBUG, "FILS: TK length mismatch: %u != %lu",
4348 keylen, (long unsigned int) sm->ptk.tk_len);
4349 goto fail;
4350 }
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004351 rsclen = wpa_cipher_rsc_len(sm->pairwise_cipher);
4352 wpa_hexdump_key(MSG_DEBUG, "FILS: Set TK to driver",
4353 sm->ptk.tk, keylen);
4354 if (wpa_sm_set_key(sm, alg, sm->bssid, 0, 1, null_rsc, rsclen,
4355 sm->ptk.tk, keylen) < 0) {
4356 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
4357 "FILS: Failed to set PTK to the driver (alg=%d keylen=%d bssid="
4358 MACSTR ")",
4359 alg, keylen, MAC2STR(sm->bssid));
4360 goto fail;
4361 }
4362
4363 /* TODO: TK could be cleared after auth frame exchange now that driver
4364 * takes care of association frame encryption/decryption. */
4365 /* TK is not needed anymore in supplicant */
4366 os_memset(sm->ptk.tk, 0, WPA_TK_MAX_LEN);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004367 sm->ptk.tk_len = 0;
Mathy Vanhoefc66556c2017-09-29 04:22:51 +02004368 sm->ptk.installed = 1;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004369
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08004370 /* FILS HLP Container */
4371 fils_process_hlp_container(sm, ie_start, end - ie_start);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004372
4373 /* TODO: FILS IP Address Assignment */
4374
4375 wpa_printf(MSG_DEBUG, "FILS: Auth+Assoc completed successfully");
4376 sm->fils_completed = 1;
4377
4378 return 0;
4379fail:
4380 return -1;
4381}
4382
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004383
4384void wpa_sm_set_reset_fils_completed(struct wpa_sm *sm, int set)
4385{
4386 if (sm)
4387 sm->fils_completed = !!set;
4388}
4389
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004390#endif /* CONFIG_FILS */
4391
4392
4393int wpa_fils_is_completed(struct wpa_sm *sm)
4394{
4395#ifdef CONFIG_FILS
4396 return sm && sm->fils_completed;
4397#else /* CONFIG_FILS */
4398 return 0;
4399#endif /* CONFIG_FILS */
4400}
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004401
4402
4403#ifdef CONFIG_OWE
4404
4405struct wpabuf * owe_build_assoc_req(struct wpa_sm *sm, u16 group)
4406{
4407 struct wpabuf *ie = NULL, *pub = NULL;
4408 size_t prime_len;
4409
4410 if (group == 19)
4411 prime_len = 32;
4412 else if (group == 20)
4413 prime_len = 48;
4414 else if (group == 21)
4415 prime_len = 66;
4416 else
4417 return NULL;
4418
4419 crypto_ecdh_deinit(sm->owe_ecdh);
4420 sm->owe_ecdh = crypto_ecdh_init(group);
4421 if (!sm->owe_ecdh)
4422 goto fail;
4423 sm->owe_group = group;
4424 pub = crypto_ecdh_get_pubkey(sm->owe_ecdh, 0);
4425 pub = wpabuf_zeropad(pub, prime_len);
4426 if (!pub)
4427 goto fail;
4428
4429 ie = wpabuf_alloc(5 + wpabuf_len(pub));
4430 if (!ie)
4431 goto fail;
4432 wpabuf_put_u8(ie, WLAN_EID_EXTENSION);
4433 wpabuf_put_u8(ie, 1 + 2 + wpabuf_len(pub));
4434 wpabuf_put_u8(ie, WLAN_EID_EXT_OWE_DH_PARAM);
4435 wpabuf_put_le16(ie, group);
4436 wpabuf_put_buf(ie, pub);
4437 wpabuf_free(pub);
4438 wpa_hexdump_buf(MSG_DEBUG, "OWE: Diffie-Hellman Parameter element",
4439 ie);
4440
4441 return ie;
4442fail:
4443 wpabuf_free(pub);
4444 crypto_ecdh_deinit(sm->owe_ecdh);
4445 sm->owe_ecdh = NULL;
4446 return NULL;
4447}
4448
4449
4450int owe_process_assoc_resp(struct wpa_sm *sm, const u8 *bssid,
4451 const u8 *resp_ies, size_t resp_ies_len)
4452{
4453 struct ieee802_11_elems elems;
4454 u16 group;
4455 struct wpabuf *secret, *pub, *hkey;
4456 int res;
4457 u8 prk[SHA512_MAC_LEN], pmkid[SHA512_MAC_LEN];
4458 const char *info = "OWE Key Generation";
4459 const u8 *addr[2];
4460 size_t len[2];
4461 size_t hash_len, prime_len;
4462 struct wpa_ie_data data;
4463
4464 if (!resp_ies ||
4465 ieee802_11_parse_elems(resp_ies, resp_ies_len, &elems, 1) ==
4466 ParseFailed) {
4467 wpa_printf(MSG_INFO,
4468 "OWE: Could not parse Association Response frame elements");
4469 return -1;
4470 }
4471
4472 if (sm->cur_pmksa && elems.rsn_ie &&
4473 wpa_parse_wpa_ie_rsn(elems.rsn_ie - 2, 2 + elems.rsn_ie_len,
4474 &data) == 0 &&
4475 data.num_pmkid == 1 && data.pmkid &&
4476 os_memcmp(sm->cur_pmksa->pmkid, data.pmkid, PMKID_LEN) == 0) {
4477 wpa_printf(MSG_DEBUG, "OWE: Use PMKSA caching");
4478 wpa_sm_set_pmk_from_pmksa(sm);
4479 return 0;
4480 }
4481
4482 if (!elems.owe_dh) {
4483 wpa_printf(MSG_INFO,
4484 "OWE: No Diffie-Hellman Parameter element found in Association Response frame");
4485 return -1;
4486 }
4487
4488 group = WPA_GET_LE16(elems.owe_dh);
4489 if (group != sm->owe_group) {
4490 wpa_printf(MSG_INFO,
4491 "OWE: Unexpected Diffie-Hellman group in response: %u",
4492 group);
4493 return -1;
4494 }
4495
4496 if (!sm->owe_ecdh) {
4497 wpa_printf(MSG_INFO, "OWE: No ECDH state available");
4498 return -1;
4499 }
4500
4501 if (group == 19)
4502 prime_len = 32;
4503 else if (group == 20)
4504 prime_len = 48;
4505 else if (group == 21)
4506 prime_len = 66;
4507 else
4508 return -1;
4509
4510 secret = crypto_ecdh_set_peerkey(sm->owe_ecdh, 0,
4511 elems.owe_dh + 2,
4512 elems.owe_dh_len - 2);
4513 secret = wpabuf_zeropad(secret, prime_len);
4514 if (!secret) {
4515 wpa_printf(MSG_DEBUG, "OWE: Invalid peer DH public key");
4516 return -1;
4517 }
4518 wpa_hexdump_buf_key(MSG_DEBUG, "OWE: DH shared secret", secret);
4519
4520 /* prk = HKDF-extract(C | A | group, z) */
4521
4522 pub = crypto_ecdh_get_pubkey(sm->owe_ecdh, 0);
4523 if (!pub) {
4524 wpabuf_clear_free(secret);
4525 return -1;
4526 }
4527
4528 /* PMKID = Truncate-128(Hash(C | A)) */
4529 addr[0] = wpabuf_head(pub);
4530 len[0] = wpabuf_len(pub);
4531 addr[1] = elems.owe_dh + 2;
4532 len[1] = elems.owe_dh_len - 2;
4533 if (group == 19) {
4534 res = sha256_vector(2, addr, len, pmkid);
4535 hash_len = SHA256_MAC_LEN;
4536 } else if (group == 20) {
4537 res = sha384_vector(2, addr, len, pmkid);
4538 hash_len = SHA384_MAC_LEN;
4539 } else if (group == 21) {
4540 res = sha512_vector(2, addr, len, pmkid);
4541 hash_len = SHA512_MAC_LEN;
4542 } else {
4543 res = -1;
4544 hash_len = 0;
4545 }
4546 pub = wpabuf_zeropad(pub, prime_len);
4547 if (res < 0 || !pub) {
4548 wpabuf_free(pub);
4549 wpabuf_clear_free(secret);
4550 return -1;
4551 }
4552
4553 hkey = wpabuf_alloc(wpabuf_len(pub) + elems.owe_dh_len - 2 + 2);
4554 if (!hkey) {
4555 wpabuf_free(pub);
4556 wpabuf_clear_free(secret);
4557 return -1;
4558 }
4559
4560 wpabuf_put_buf(hkey, pub); /* C */
4561 wpabuf_free(pub);
4562 wpabuf_put_data(hkey, elems.owe_dh + 2, elems.owe_dh_len - 2); /* A */
4563 wpabuf_put_le16(hkey, sm->owe_group); /* group */
4564 if (group == 19)
4565 res = hmac_sha256(wpabuf_head(hkey), wpabuf_len(hkey),
4566 wpabuf_head(secret), wpabuf_len(secret), prk);
4567 else if (group == 20)
4568 res = hmac_sha384(wpabuf_head(hkey), wpabuf_len(hkey),
4569 wpabuf_head(secret), wpabuf_len(secret), prk);
4570 else if (group == 21)
4571 res = hmac_sha512(wpabuf_head(hkey), wpabuf_len(hkey),
4572 wpabuf_head(secret), wpabuf_len(secret), prk);
4573 wpabuf_clear_free(hkey);
4574 wpabuf_clear_free(secret);
4575 if (res < 0)
4576 return -1;
4577
4578 wpa_hexdump_key(MSG_DEBUG, "OWE: prk", prk, hash_len);
4579
4580 /* PMK = HKDF-expand(prk, "OWE Key Generation", n) */
4581
4582 if (group == 19)
4583 res = hmac_sha256_kdf(prk, hash_len, NULL, (const u8 *) info,
4584 os_strlen(info), sm->pmk, hash_len);
4585 else if (group == 20)
4586 res = hmac_sha384_kdf(prk, hash_len, NULL, (const u8 *) info,
4587 os_strlen(info), sm->pmk, hash_len);
4588 else if (group == 21)
4589 res = hmac_sha512_kdf(prk, hash_len, NULL, (const u8 *) info,
4590 os_strlen(info), sm->pmk, hash_len);
4591 os_memset(prk, 0, SHA512_MAC_LEN);
Roshan Pius3a1667e2018-07-03 15:17:14 -07004592 if (res < 0) {
4593 sm->pmk_len = 0;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004594 return -1;
Roshan Pius3a1667e2018-07-03 15:17:14 -07004595 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004596 sm->pmk_len = hash_len;
4597
4598 wpa_hexdump_key(MSG_DEBUG, "OWE: PMK", sm->pmk, sm->pmk_len);
4599 wpa_hexdump(MSG_DEBUG, "OWE: PMKID", pmkid, PMKID_LEN);
4600 pmksa_cache_add(sm->pmksa, sm->pmk, sm->pmk_len, pmkid, NULL, 0,
4601 bssid, sm->own_addr, sm->network_ctx, sm->key_mgmt,
4602 NULL);
4603
4604 return 0;
4605}
4606
4607#endif /* CONFIG_OWE */
4608
4609
4610void wpa_sm_set_fils_cache_id(struct wpa_sm *sm, const u8 *fils_cache_id)
4611{
4612#ifdef CONFIG_FILS
4613 if (sm && fils_cache_id) {
4614 sm->fils_cache_id_set = 1;
4615 os_memcpy(sm->fils_cache_id, fils_cache_id, FILS_CACHE_ID_LEN);
4616 }
4617#endif /* CONFIG_FILS */
4618}