blob: 9b1f098633ba15595b3eb110788f7d82671d4708 [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 Shalom39bc25d2019-02-06 16:32:13 -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 Shalom39bc25d2019-02-06 16:32:13 -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 Shalom39bc25d2019-02-06 16:32:13 -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 */
718 eloop_register_timeout(1, 0, wpa_sm_start_preauth, sm, NULL);
719 }
720
721 if (sm->cur_pmksa && sm->cur_pmksa->opportunistic) {
722 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
723 "RSN: Authenticator accepted "
724 "opportunistic PMKSA entry - marking it valid");
725 sm->cur_pmksa->opportunistic = 0;
726 }
727
728#ifdef CONFIG_IEEE80211R
729 if (wpa_key_mgmt_ft(sm->key_mgmt)) {
730 /* Prepare for the next transition */
731 wpa_ft_prepare_auth_request(sm, NULL);
732 }
733#endif /* CONFIG_IEEE80211R */
734}
735
736
737static void wpa_sm_rekey_ptk(void *eloop_ctx, void *timeout_ctx)
738{
739 struct wpa_sm *sm = eloop_ctx;
740 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Request PTK rekeying");
741 wpa_sm_key_request(sm, 0, 1);
742}
743
744
745static int wpa_supplicant_install_ptk(struct wpa_sm *sm,
746 const struct wpa_eapol_key *key)
747{
748 int keylen, rsclen;
749 enum wpa_alg alg;
750 const u8 *key_rsc;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800751
Mathy Vanhoefc66556c2017-09-29 04:22:51 +0200752 if (sm->ptk.installed) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800753 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
754 "WPA: Do not re-install same PTK to the driver");
755 return 0;
756 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700757
758 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
759 "WPA: Installing PTK to the driver");
760
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700761 if (sm->pairwise_cipher == WPA_CIPHER_NONE) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700762 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Pairwise Cipher "
763 "Suite: NONE - do not use pairwise keys");
764 return 0;
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700765 }
766
767 if (!wpa_cipher_valid_pairwise(sm->pairwise_cipher)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700768 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
769 "WPA: Unsupported pairwise cipher %d",
770 sm->pairwise_cipher);
771 return -1;
772 }
773
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700774 alg = wpa_cipher_to_alg(sm->pairwise_cipher);
775 keylen = wpa_cipher_key_len(sm->pairwise_cipher);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700776 if (keylen <= 0 || (unsigned int) keylen != sm->ptk.tk_len) {
777 wpa_printf(MSG_DEBUG, "WPA: TK length mismatch: %d != %lu",
778 keylen, (long unsigned int) sm->ptk.tk_len);
779 return -1;
780 }
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700781 rsclen = wpa_cipher_rsc_len(sm->pairwise_cipher);
782
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800783 if (sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700784 key_rsc = null_rsc;
785 } else {
786 key_rsc = key->key_rsc;
787 wpa_hexdump(MSG_DEBUG, "WPA: RSC", key_rsc, rsclen);
788 }
789
790 if (wpa_sm_set_key(sm, alg, sm->bssid, 0, 1, key_rsc, rsclen,
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800791 sm->ptk.tk, keylen) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700792 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
793 "WPA: Failed to set PTK to the "
794 "driver (alg=%d keylen=%d bssid=" MACSTR ")",
795 alg, keylen, MAC2STR(sm->bssid));
796 return -1;
797 }
798
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800799 /* TK is not needed anymore in supplicant */
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800800 os_memset(sm->ptk.tk, 0, WPA_TK_MAX_LEN);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700801 sm->ptk.tk_len = 0;
Mathy Vanhoefc66556c2017-09-29 04:22:51 +0200802 sm->ptk.installed = 1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800803
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700804 if (sm->wpa_ptk_rekey) {
805 eloop_cancel_timeout(wpa_sm_rekey_ptk, sm, NULL);
806 eloop_register_timeout(sm->wpa_ptk_rekey, 0, wpa_sm_rekey_ptk,
807 sm, NULL);
808 }
809
810 return 0;
811}
812
813
814static int wpa_supplicant_check_group_cipher(struct wpa_sm *sm,
815 int group_cipher,
816 int keylen, int maxkeylen,
817 int *key_rsc_len,
818 enum wpa_alg *alg)
819{
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700820 int klen;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700821
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700822 *alg = wpa_cipher_to_alg(group_cipher);
823 if (*alg == WPA_ALG_NONE) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700824 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
825 "WPA: Unsupported Group Cipher %d",
826 group_cipher);
827 return -1;
828 }
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700829 *key_rsc_len = wpa_cipher_rsc_len(group_cipher);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700830
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700831 klen = wpa_cipher_key_len(group_cipher);
832 if (keylen != klen || maxkeylen < klen) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700833 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
834 "WPA: Unsupported %s Group Cipher key length %d (%d)",
835 wpa_cipher_txt(group_cipher), keylen, maxkeylen);
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700836 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700837 }
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700838 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700839}
840
841
842struct wpa_gtk_data {
843 enum wpa_alg alg;
844 int tx, key_rsc_len, keyidx;
845 u8 gtk[32];
846 int gtk_len;
847};
848
849
850static int wpa_supplicant_install_gtk(struct wpa_sm *sm,
851 const struct wpa_gtk_data *gd,
Jouni Malinen58c0e962017-10-01 12:12:24 +0300852 const u8 *key_rsc, int wnm_sleep)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700853{
854 const u8 *_gtk = gd->gtk;
855 u8 gtk_buf[32];
856
Mathy Vanhoef10bfd642017-07-12 16:03:24 +0200857 /* Detect possible key reinstallation */
Jouni Malinen58c0e962017-10-01 12:12:24 +0300858 if ((sm->gtk.gtk_len == (size_t) gd->gtk_len &&
859 os_memcmp(sm->gtk.gtk, gd->gtk, sm->gtk.gtk_len) == 0) ||
860 (sm->gtk_wnm_sleep.gtk_len == (size_t) gd->gtk_len &&
861 os_memcmp(sm->gtk_wnm_sleep.gtk, gd->gtk,
862 sm->gtk_wnm_sleep.gtk_len) == 0)) {
Mathy Vanhoef10bfd642017-07-12 16:03:24 +0200863 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
864 "WPA: Not reinstalling already in-use GTK to the driver (keyidx=%d tx=%d len=%d)",
865 gd->keyidx, gd->tx, gd->gtk_len);
866 return 0;
867 }
868
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700869 wpa_hexdump_key(MSG_DEBUG, "WPA: Group Key", gd->gtk, gd->gtk_len);
870 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
871 "WPA: Installing GTK to the driver (keyidx=%d tx=%d len=%d)",
872 gd->keyidx, gd->tx, gd->gtk_len);
873 wpa_hexdump(MSG_DEBUG, "WPA: RSC", key_rsc, gd->key_rsc_len);
874 if (sm->group_cipher == WPA_CIPHER_TKIP) {
875 /* Swap Tx/Rx keys for Michael MIC */
876 os_memcpy(gtk_buf, gd->gtk, 16);
877 os_memcpy(gtk_buf + 16, gd->gtk + 24, 8);
878 os_memcpy(gtk_buf + 24, gd->gtk + 16, 8);
879 _gtk = gtk_buf;
880 }
881 if (sm->pairwise_cipher == WPA_CIPHER_NONE) {
882 if (wpa_sm_set_key(sm, gd->alg, NULL,
883 gd->keyidx, 1, key_rsc, gd->key_rsc_len,
884 _gtk, gd->gtk_len) < 0) {
885 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
886 "WPA: Failed to set GTK to the driver "
887 "(Group only)");
Dmitry Shmidt61593f02014-04-21 16:27:35 -0700888 os_memset(gtk_buf, 0, sizeof(gtk_buf));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700889 return -1;
890 }
891 } else if (wpa_sm_set_key(sm, gd->alg, broadcast_ether_addr,
892 gd->keyidx, gd->tx, key_rsc, gd->key_rsc_len,
893 _gtk, gd->gtk_len) < 0) {
894 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
895 "WPA: Failed to set GTK to "
896 "the driver (alg=%d keylen=%d keyidx=%d)",
897 gd->alg, gd->gtk_len, gd->keyidx);
Dmitry Shmidt61593f02014-04-21 16:27:35 -0700898 os_memset(gtk_buf, 0, sizeof(gtk_buf));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700899 return -1;
900 }
Dmitry Shmidt61593f02014-04-21 16:27:35 -0700901 os_memset(gtk_buf, 0, sizeof(gtk_buf));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700902
Jouni Malinen58c0e962017-10-01 12:12:24 +0300903 if (wnm_sleep) {
904 sm->gtk_wnm_sleep.gtk_len = gd->gtk_len;
905 os_memcpy(sm->gtk_wnm_sleep.gtk, gd->gtk,
906 sm->gtk_wnm_sleep.gtk_len);
907 } else {
908 sm->gtk.gtk_len = gd->gtk_len;
909 os_memcpy(sm->gtk.gtk, gd->gtk, sm->gtk.gtk_len);
910 }
Mathy Vanhoef10bfd642017-07-12 16:03:24 +0200911
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700912 return 0;
913}
914
915
916static int wpa_supplicant_gtk_tx_bit_workaround(const struct wpa_sm *sm,
917 int tx)
918{
919 if (tx && sm->pairwise_cipher != WPA_CIPHER_NONE) {
920 /* Ignore Tx bit for GTK if a pairwise key is used. One AP
921 * seemed to set this bit (incorrectly, since Tx is only when
922 * doing Group Key only APs) and without this workaround, the
923 * data connection does not work because wpa_supplicant
924 * configured non-zero keyidx to be used for unicast. */
925 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
926 "WPA: Tx bit set for GTK, but pairwise "
927 "keys are used - ignore Tx bit");
928 return 0;
929 }
930 return tx;
931}
932
933
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800934static int wpa_supplicant_rsc_relaxation(const struct wpa_sm *sm,
935 const u8 *rsc)
936{
937 int rsclen;
938
939 if (!sm->wpa_rsc_relaxation)
940 return 0;
941
942 rsclen = wpa_cipher_rsc_len(sm->group_cipher);
943
944 /*
945 * Try to detect RSC (endian) corruption issue where the AP sends
946 * the RSC bytes in EAPOL-Key message in the wrong order, both if
947 * it's actually a 6-byte field (as it should be) and if it treats
948 * it as an 8-byte field.
949 * An AP model known to have this bug is the Sapido RB-1632.
950 */
951 if (rsclen == 6 && ((rsc[5] && !rsc[0]) || rsc[6] || rsc[7])) {
952 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
953 "RSC %02x%02x%02x%02x%02x%02x%02x%02x is likely bogus, using 0",
954 rsc[0], rsc[1], rsc[2], rsc[3],
955 rsc[4], rsc[5], rsc[6], rsc[7]);
956
957 return 1;
958 }
959
960 return 0;
961}
962
963
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700964static int wpa_supplicant_pairwise_gtk(struct wpa_sm *sm,
965 const struct wpa_eapol_key *key,
966 const u8 *gtk, size_t gtk_len,
967 int key_info)
968{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700969 struct wpa_gtk_data gd;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800970 const u8 *key_rsc;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700971
972 /*
973 * IEEE Std 802.11i-2004 - 8.5.2 EAPOL-Key frames - Figure 43x
974 * GTK KDE format:
975 * KeyID[bits 0-1], Tx [bit 2], Reserved [bits 3-7]
976 * Reserved [bits 0-7]
977 * GTK
978 */
979
980 os_memset(&gd, 0, sizeof(gd));
981 wpa_hexdump_key(MSG_DEBUG, "RSN: received GTK in pairwise handshake",
982 gtk, gtk_len);
983
984 if (gtk_len < 2 || gtk_len - 2 > sizeof(gd.gtk))
985 return -1;
986
987 gd.keyidx = gtk[0] & 0x3;
988 gd.tx = wpa_supplicant_gtk_tx_bit_workaround(sm,
989 !!(gtk[0] & BIT(2)));
990 gtk += 2;
991 gtk_len -= 2;
992
993 os_memcpy(gd.gtk, gtk, gtk_len);
994 gd.gtk_len = gtk_len;
995
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800996 key_rsc = key->key_rsc;
997 if (wpa_supplicant_rsc_relaxation(sm, key->key_rsc))
998 key_rsc = null_rsc;
999
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001000 if (sm->group_cipher != WPA_CIPHER_GTK_NOT_USED &&
1001 (wpa_supplicant_check_group_cipher(sm, sm->group_cipher,
1002 gtk_len, gtk_len,
1003 &gd.key_rsc_len, &gd.alg) ||
Jouni Malinen58c0e962017-10-01 12:12:24 +03001004 wpa_supplicant_install_gtk(sm, &gd, key_rsc, 0))) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001005 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1006 "RSN: Failed to install GTK");
Dmitry Shmidt61593f02014-04-21 16:27:35 -07001007 os_memset(&gd, 0, sizeof(gd));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001008 return -1;
1009 }
Dmitry Shmidt61593f02014-04-21 16:27:35 -07001010 os_memset(&gd, 0, sizeof(gd));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001011
1012 wpa_supplicant_key_neg_complete(sm, sm->bssid,
1013 key_info & WPA_KEY_INFO_SECURE);
1014 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001015}
1016
1017
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02001018#ifdef CONFIG_IEEE80211W
1019static int wpa_supplicant_install_igtk(struct wpa_sm *sm,
Jouni Malinen58c0e962017-10-01 12:12:24 +03001020 const struct wpa_igtk_kde *igtk,
1021 int wnm_sleep)
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02001022{
1023 size_t len = wpa_cipher_key_len(sm->mgmt_group_cipher);
1024 u16 keyidx = WPA_GET_LE16(igtk->keyid);
1025
1026 /* Detect possible key reinstallation */
Jouni Malinen58c0e962017-10-01 12:12:24 +03001027 if ((sm->igtk.igtk_len == len &&
1028 os_memcmp(sm->igtk.igtk, igtk->igtk, sm->igtk.igtk_len) == 0) ||
1029 (sm->igtk_wnm_sleep.igtk_len == len &&
1030 os_memcmp(sm->igtk_wnm_sleep.igtk, igtk->igtk,
1031 sm->igtk_wnm_sleep.igtk_len) == 0)) {
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02001032 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1033 "WPA: Not reinstalling already in-use IGTK to the driver (keyidx=%d)",
1034 keyidx);
1035 return 0;
1036 }
1037
1038 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
Hai Shalom39ba6fc2019-01-22 12:40:38 -08001039 "WPA: IGTK keyid %d pn " COMPACT_MACSTR,
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02001040 keyidx, MAC2STR(igtk->pn));
1041 wpa_hexdump_key(MSG_DEBUG, "WPA: IGTK", igtk->igtk, len);
1042 if (keyidx > 4095) {
1043 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1044 "WPA: Invalid IGTK KeyID %d", keyidx);
1045 return -1;
1046 }
1047 if (wpa_sm_set_key(sm, wpa_cipher_to_alg(sm->mgmt_group_cipher),
1048 broadcast_ether_addr,
1049 keyidx, 0, igtk->pn, sizeof(igtk->pn),
1050 igtk->igtk, len) < 0) {
1051 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1052 "WPA: Failed to configure IGTK to the driver");
1053 return -1;
1054 }
1055
Jouni Malinen58c0e962017-10-01 12:12:24 +03001056 if (wnm_sleep) {
1057 sm->igtk_wnm_sleep.igtk_len = len;
1058 os_memcpy(sm->igtk_wnm_sleep.igtk, igtk->igtk,
1059 sm->igtk_wnm_sleep.igtk_len);
1060 } else {
1061 sm->igtk.igtk_len = len;
1062 os_memcpy(sm->igtk.igtk, igtk->igtk, sm->igtk.igtk_len);
1063 }
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02001064
1065 return 0;
1066}
1067#endif /* CONFIG_IEEE80211W */
1068
1069
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001070static int ieee80211w_set_keys(struct wpa_sm *sm,
1071 struct wpa_eapol_ie_parse *ie)
1072{
1073#ifdef CONFIG_IEEE80211W
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001074 if (!wpa_cipher_valid_mgmt_group(sm->mgmt_group_cipher))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001075 return 0;
1076
1077 if (ie->igtk) {
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001078 size_t len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001079 const struct wpa_igtk_kde *igtk;
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02001080
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001081 len = wpa_cipher_key_len(sm->mgmt_group_cipher);
1082 if (ie->igtk_len != WPA_IGTK_KDE_PREFIX_LEN + len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001083 return -1;
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02001084
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001085 igtk = (const struct wpa_igtk_kde *) ie->igtk;
Jouni Malinen58c0e962017-10-01 12:12:24 +03001086 if (wpa_supplicant_install_igtk(sm, igtk, 0) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001087 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001088 }
1089
1090 return 0;
1091#else /* CONFIG_IEEE80211W */
1092 return 0;
1093#endif /* CONFIG_IEEE80211W */
1094}
1095
1096
1097static void wpa_report_ie_mismatch(struct wpa_sm *sm,
1098 const char *reason, const u8 *src_addr,
1099 const u8 *wpa_ie, size_t wpa_ie_len,
1100 const u8 *rsn_ie, size_t rsn_ie_len)
1101{
1102 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, "WPA: %s (src=" MACSTR ")",
1103 reason, MAC2STR(src_addr));
1104
1105 if (sm->ap_wpa_ie) {
1106 wpa_hexdump(MSG_INFO, "WPA: WPA IE in Beacon/ProbeResp",
1107 sm->ap_wpa_ie, sm->ap_wpa_ie_len);
1108 }
1109 if (wpa_ie) {
1110 if (!sm->ap_wpa_ie) {
1111 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1112 "WPA: No WPA IE in Beacon/ProbeResp");
1113 }
1114 wpa_hexdump(MSG_INFO, "WPA: WPA IE in 3/4 msg",
1115 wpa_ie, wpa_ie_len);
1116 }
1117
1118 if (sm->ap_rsn_ie) {
1119 wpa_hexdump(MSG_INFO, "WPA: RSN IE in Beacon/ProbeResp",
1120 sm->ap_rsn_ie, sm->ap_rsn_ie_len);
1121 }
1122 if (rsn_ie) {
1123 if (!sm->ap_rsn_ie) {
1124 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1125 "WPA: No RSN IE in Beacon/ProbeResp");
1126 }
1127 wpa_hexdump(MSG_INFO, "WPA: RSN IE in 3/4 msg",
1128 rsn_ie, rsn_ie_len);
1129 }
1130
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001131 wpa_sm_deauthenticate(sm, WLAN_REASON_IE_IN_4WAY_DIFFERS);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001132}
1133
1134
1135#ifdef CONFIG_IEEE80211R
1136
1137static int ft_validate_mdie(struct wpa_sm *sm,
1138 const unsigned char *src_addr,
1139 struct wpa_eapol_ie_parse *ie,
1140 const u8 *assoc_resp_mdie)
1141{
1142 struct rsn_mdie *mdie;
1143
1144 mdie = (struct rsn_mdie *) (ie->mdie + 2);
1145 if (ie->mdie == NULL || ie->mdie_len < 2 + sizeof(*mdie) ||
1146 os_memcmp(mdie->mobility_domain, sm->mobility_domain,
1147 MOBILITY_DOMAIN_ID_LEN) != 0) {
1148 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "FT: MDIE in msg 3/4 did "
1149 "not match with the current mobility domain");
1150 return -1;
1151 }
1152
1153 if (assoc_resp_mdie &&
1154 (assoc_resp_mdie[1] != ie->mdie[1] ||
1155 os_memcmp(assoc_resp_mdie, ie->mdie, 2 + ie->mdie[1]) != 0)) {
1156 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "FT: MDIE mismatch");
1157 wpa_hexdump(MSG_DEBUG, "FT: MDIE in EAPOL-Key msg 3/4",
1158 ie->mdie, 2 + ie->mdie[1]);
1159 wpa_hexdump(MSG_DEBUG, "FT: MDIE in (Re)Association Response",
1160 assoc_resp_mdie, 2 + assoc_resp_mdie[1]);
1161 return -1;
1162 }
1163
1164 return 0;
1165}
1166
1167
1168static int ft_validate_ftie(struct wpa_sm *sm,
1169 const unsigned char *src_addr,
1170 struct wpa_eapol_ie_parse *ie,
1171 const u8 *assoc_resp_ftie)
1172{
1173 if (ie->ftie == NULL) {
1174 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1175 "FT: No FTIE in EAPOL-Key msg 3/4");
1176 return -1;
1177 }
1178
1179 if (assoc_resp_ftie == NULL)
1180 return 0;
1181
1182 if (assoc_resp_ftie[1] != ie->ftie[1] ||
1183 os_memcmp(assoc_resp_ftie, ie->ftie, 2 + ie->ftie[1]) != 0) {
1184 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "FT: FTIE mismatch");
1185 wpa_hexdump(MSG_DEBUG, "FT: FTIE in EAPOL-Key msg 3/4",
1186 ie->ftie, 2 + ie->ftie[1]);
1187 wpa_hexdump(MSG_DEBUG, "FT: FTIE in (Re)Association Response",
1188 assoc_resp_ftie, 2 + assoc_resp_ftie[1]);
1189 return -1;
1190 }
1191
1192 return 0;
1193}
1194
1195
1196static int ft_validate_rsnie(struct wpa_sm *sm,
1197 const unsigned char *src_addr,
1198 struct wpa_eapol_ie_parse *ie)
1199{
1200 struct wpa_ie_data rsn;
1201
1202 if (!ie->rsn_ie)
1203 return 0;
1204
1205 /*
1206 * Verify that PMKR1Name from EAPOL-Key message 3/4
1207 * matches with the value we derived.
1208 */
1209 if (wpa_parse_wpa_ie_rsn(ie->rsn_ie, ie->rsn_ie_len, &rsn) < 0 ||
1210 rsn.num_pmkid != 1 || rsn.pmkid == NULL) {
1211 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "FT: No PMKR1Name in "
1212 "FT 4-way handshake message 3/4");
1213 return -1;
1214 }
1215
Dmitry Shmidtc2817022014-07-02 10:32:10 -07001216 if (os_memcmp_const(rsn.pmkid, sm->pmk_r1_name, WPA_PMK_NAME_LEN) != 0)
1217 {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001218 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1219 "FT: PMKR1Name mismatch in "
1220 "FT 4-way handshake message 3/4");
1221 wpa_hexdump(MSG_DEBUG, "FT: PMKR1Name from Authenticator",
1222 rsn.pmkid, WPA_PMK_NAME_LEN);
1223 wpa_hexdump(MSG_DEBUG, "FT: Derived PMKR1Name",
1224 sm->pmk_r1_name, WPA_PMK_NAME_LEN);
1225 return -1;
1226 }
1227
1228 return 0;
1229}
1230
1231
1232static int wpa_supplicant_validate_ie_ft(struct wpa_sm *sm,
1233 const unsigned char *src_addr,
1234 struct wpa_eapol_ie_parse *ie)
1235{
1236 const u8 *pos, *end, *mdie = NULL, *ftie = NULL;
1237
1238 if (sm->assoc_resp_ies) {
1239 pos = sm->assoc_resp_ies;
1240 end = pos + sm->assoc_resp_ies_len;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001241 while (end - pos > 2) {
1242 if (2 + pos[1] > end - pos)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001243 break;
1244 switch (*pos) {
1245 case WLAN_EID_MOBILITY_DOMAIN:
1246 mdie = pos;
1247 break;
1248 case WLAN_EID_FAST_BSS_TRANSITION:
1249 ftie = pos;
1250 break;
1251 }
1252 pos += 2 + pos[1];
1253 }
1254 }
1255
1256 if (ft_validate_mdie(sm, src_addr, ie, mdie) < 0 ||
1257 ft_validate_ftie(sm, src_addr, ie, ftie) < 0 ||
1258 ft_validate_rsnie(sm, src_addr, ie) < 0)
1259 return -1;
1260
1261 return 0;
1262}
1263
1264#endif /* CONFIG_IEEE80211R */
1265
1266
1267static int wpa_supplicant_validate_ie(struct wpa_sm *sm,
1268 const unsigned char *src_addr,
1269 struct wpa_eapol_ie_parse *ie)
1270{
1271 if (sm->ap_wpa_ie == NULL && sm->ap_rsn_ie == NULL) {
1272 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1273 "WPA: No WPA/RSN IE for this AP known. "
1274 "Trying to get from scan results");
1275 if (wpa_sm_get_beacon_ie(sm) < 0) {
1276 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1277 "WPA: Could not find AP from "
1278 "the scan results");
1279 } else {
1280 wpa_msg(sm->ctx->msg_ctx, MSG_DEBUG,
1281 "WPA: Found the current AP from "
1282 "updated scan results");
1283 }
1284 }
1285
1286 if (ie->wpa_ie == NULL && ie->rsn_ie == NULL &&
1287 (sm->ap_wpa_ie || sm->ap_rsn_ie)) {
1288 wpa_report_ie_mismatch(sm, "IE in 3/4 msg does not match "
1289 "with IE in Beacon/ProbeResp (no IE?)",
1290 src_addr, ie->wpa_ie, ie->wpa_ie_len,
1291 ie->rsn_ie, ie->rsn_ie_len);
1292 return -1;
1293 }
1294
1295 if ((ie->wpa_ie && sm->ap_wpa_ie &&
1296 (ie->wpa_ie_len != sm->ap_wpa_ie_len ||
1297 os_memcmp(ie->wpa_ie, sm->ap_wpa_ie, ie->wpa_ie_len) != 0)) ||
1298 (ie->rsn_ie && sm->ap_rsn_ie &&
1299 wpa_compare_rsn_ie(wpa_key_mgmt_ft(sm->key_mgmt),
1300 sm->ap_rsn_ie, sm->ap_rsn_ie_len,
1301 ie->rsn_ie, ie->rsn_ie_len))) {
1302 wpa_report_ie_mismatch(sm, "IE in 3/4 msg does not match "
1303 "with IE in Beacon/ProbeResp",
1304 src_addr, ie->wpa_ie, ie->wpa_ie_len,
1305 ie->rsn_ie, ie->rsn_ie_len);
1306 return -1;
1307 }
1308
1309 if (sm->proto == WPA_PROTO_WPA &&
1310 ie->rsn_ie && sm->ap_rsn_ie == NULL && sm->rsn_enabled) {
1311 wpa_report_ie_mismatch(sm, "Possible downgrade attack "
1312 "detected - RSN was enabled and RSN IE "
1313 "was in msg 3/4, but not in "
1314 "Beacon/ProbeResp",
1315 src_addr, ie->wpa_ie, ie->wpa_ie_len,
1316 ie->rsn_ie, ie->rsn_ie_len);
1317 return -1;
1318 }
1319
1320#ifdef CONFIG_IEEE80211R
1321 if (wpa_key_mgmt_ft(sm->key_mgmt) &&
1322 wpa_supplicant_validate_ie_ft(sm, src_addr, ie) < 0)
1323 return -1;
1324#endif /* CONFIG_IEEE80211R */
1325
1326 return 0;
1327}
1328
1329
1330/**
1331 * wpa_supplicant_send_4_of_4 - Send message 4 of WPA/RSN 4-Way Handshake
1332 * @sm: Pointer to WPA state machine data from wpa_sm_init()
1333 * @dst: Destination address for the frame
1334 * @key: Pointer to the EAPOL-Key frame header
1335 * @ver: Version bits from EAPOL-Key Key Info
1336 * @key_info: Key Info
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001337 * @ptk: PTK to use for keyed hash and encryption
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001338 * Returns: >= 0 on success, < 0 on failure
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001339 */
1340int wpa_supplicant_send_4_of_4(struct wpa_sm *sm, const unsigned char *dst,
1341 const struct wpa_eapol_key *key,
1342 u16 ver, u16 key_info,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001343 struct wpa_ptk *ptk)
1344{
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001345 size_t mic_len, hdrlen, rlen;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001346 struct wpa_eapol_key *reply;
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001347 u8 *rbuf, *key_mic;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001348
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001349 mic_len = wpa_mic_len(sm->key_mgmt, sm->pmk_len);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001350 hdrlen = sizeof(*reply) + mic_len + 2;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001351 rbuf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_KEY, NULL,
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001352 hdrlen, &rlen, (void *) &reply);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001353 if (rbuf == NULL)
1354 return -1;
1355
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001356 reply->type = (sm->proto == WPA_PROTO_RSN ||
1357 sm->proto == WPA_PROTO_OSEN) ?
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001358 EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA;
1359 key_info &= WPA_KEY_INFO_SECURE;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001360 key_info |= ver | WPA_KEY_INFO_KEY_TYPE;
1361 if (mic_len)
1362 key_info |= WPA_KEY_INFO_MIC;
1363 else
1364 key_info |= WPA_KEY_INFO_ENCR_KEY_DATA;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001365 WPA_PUT_BE16(reply->key_info, key_info);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001366 if (sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001367 WPA_PUT_BE16(reply->key_length, 0);
1368 else
1369 os_memcpy(reply->key_length, key->key_length, 2);
1370 os_memcpy(reply->replay_counter, key->replay_counter,
1371 WPA_REPLAY_COUNTER_LEN);
1372
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001373 key_mic = (u8 *) (reply + 1);
1374 WPA_PUT_BE16(key_mic + mic_len, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001375
Roshan Pius5e7db942018-04-12 12:27:41 -07001376 wpa_dbg(sm->ctx->msg_ctx, MSG_INFO, "WPA: Sending EAPOL-Key 4/4");
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001377 return wpa_eapol_key_send(sm, ptk, ver, dst, ETH_P_EAPOL, rbuf, rlen,
1378 key_mic);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001379}
1380
1381
1382static void wpa_supplicant_process_3_of_4(struct wpa_sm *sm,
1383 const struct wpa_eapol_key *key,
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001384 u16 ver, const u8 *key_data,
1385 size_t key_data_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001386{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001387 u16 key_info, keylen;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001388 struct wpa_eapol_ie_parse ie;
1389
1390 wpa_sm_set_state(sm, WPA_4WAY_HANDSHAKE);
Roshan Pius5e7db942018-04-12 12:27:41 -07001391 wpa_dbg(sm->ctx->msg_ctx, MSG_INFO, "WPA: RX message 3 of 4-Way "
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001392 "Handshake from " MACSTR " (ver=%d)", MAC2STR(sm->bssid), ver);
1393
1394 key_info = WPA_GET_BE16(key->key_info);
1395
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001396 wpa_hexdump(MSG_DEBUG, "WPA: IE KeyData", key_data, key_data_len);
1397 if (wpa_supplicant_parse_ies(key_data, key_data_len, &ie) < 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001398 goto failed;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001399 if (ie.gtk && !(key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
1400 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1401 "WPA: GTK IE in unencrypted key data");
1402 goto failed;
1403 }
1404#ifdef CONFIG_IEEE80211W
1405 if (ie.igtk && !(key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
1406 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1407 "WPA: IGTK KDE in unencrypted key data");
1408 goto failed;
1409 }
1410
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001411 if (ie.igtk &&
1412 wpa_cipher_valid_mgmt_group(sm->mgmt_group_cipher) &&
1413 ie.igtk_len != WPA_IGTK_KDE_PREFIX_LEN +
1414 (unsigned int) wpa_cipher_key_len(sm->mgmt_group_cipher)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001415 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1416 "WPA: Invalid IGTK KDE length %lu",
1417 (unsigned long) ie.igtk_len);
1418 goto failed;
1419 }
1420#endif /* CONFIG_IEEE80211W */
1421
1422 if (wpa_supplicant_validate_ie(sm, sm->bssid, &ie) < 0)
1423 goto failed;
1424
1425 if (os_memcmp(sm->anonce, key->key_nonce, WPA_NONCE_LEN) != 0) {
1426 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1427 "WPA: ANonce from message 1 of 4-Way Handshake "
1428 "differs from 3 of 4-Way Handshake - drop packet (src="
1429 MACSTR ")", MAC2STR(sm->bssid));
1430 goto failed;
1431 }
1432
1433 keylen = WPA_GET_BE16(key->key_length);
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07001434 if (keylen != wpa_cipher_key_len(sm->pairwise_cipher)) {
1435 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1436 "WPA: Invalid %s key length %d (src=" MACSTR
1437 ")", wpa_cipher_txt(sm->pairwise_cipher), keylen,
1438 MAC2STR(sm->bssid));
1439 goto failed;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001440 }
1441
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001442#ifdef CONFIG_P2P
1443 if (ie.ip_addr_alloc) {
1444 os_memcpy(sm->p2p_ip_addr, ie.ip_addr_alloc, 3 * 4);
1445 wpa_hexdump(MSG_DEBUG, "P2P: IP address info",
1446 sm->p2p_ip_addr, sizeof(sm->p2p_ip_addr));
1447 }
1448#endif /* CONFIG_P2P */
1449
Hai Shalom39bc25d2019-02-06 16:32:13 -08001450#ifdef CONFIG_OCV
1451 if (wpa_sm_ocv_enabled(sm)) {
1452 struct wpa_channel_info ci;
1453
1454 if (wpa_sm_channel_info(sm, &ci) != 0) {
1455 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1456 "Failed to get channel info to validate received OCI in EAPOL-Key 3/4");
1457 return;
1458 }
1459
1460 if (ocv_verify_tx_params(ie.oci, ie.oci_len, &ci,
1461 channel_width_to_int(ci.chanwidth),
1462 ci.seg1_idx) != 0) {
1463 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, "%s",
1464 ocv_errorstr);
1465 return;
1466 }
1467 }
1468#endif /* CONFIG_OCV */
1469
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001470 if (wpa_supplicant_send_4_of_4(sm, sm->bssid, key, ver, key_info,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001471 &sm->ptk) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001472 goto failed;
1473 }
1474
1475 /* SNonce was successfully used in msg 3/4, so mark it to be renewed
1476 * for the next 4-Way Handshake. If msg 3 is received again, the old
1477 * SNonce will still be used to avoid changing PTK. */
1478 sm->renew_snonce = 1;
1479
1480 if (key_info & WPA_KEY_INFO_INSTALL) {
1481 if (wpa_supplicant_install_ptk(sm, key))
1482 goto failed;
1483 }
1484
1485 if (key_info & WPA_KEY_INFO_SECURE) {
1486 wpa_sm_mlme_setprotection(
1487 sm, sm->bssid, MLME_SETPROTECTION_PROTECT_TYPE_RX,
1488 MLME_SETPROTECTION_KEY_TYPE_PAIRWISE);
1489 eapol_sm_notify_portValid(sm->eapol, TRUE);
1490 }
1491 wpa_sm_set_state(sm, WPA_GROUP_HANDSHAKE);
1492
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001493 if (sm->group_cipher == WPA_CIPHER_GTK_NOT_USED) {
1494 wpa_supplicant_key_neg_complete(sm, sm->bssid,
1495 key_info & WPA_KEY_INFO_SECURE);
1496 } else if (ie.gtk &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001497 wpa_supplicant_pairwise_gtk(sm, key,
1498 ie.gtk, ie.gtk_len, key_info) < 0) {
1499 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1500 "RSN: Failed to configure GTK");
1501 goto failed;
1502 }
1503
1504 if (ieee80211w_set_keys(sm, &ie) < 0) {
1505 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1506 "RSN: Failed to configure IGTK");
1507 goto failed;
1508 }
1509
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001510 if (ie.gtk)
1511 wpa_sm_set_rekey_offload(sm);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001512
Hai Shalom39ba6fc2019-01-22 12:40:38 -08001513 /* Add PMKSA cache entry for Suite B AKMs here since PMKID can be
1514 * calculated only after KCK has been derived. Though, do not replace an
1515 * existing PMKSA entry after each 4-way handshake (i.e., new KCK/PMKID)
1516 * to avoid unnecessary changes of PMKID while continuing to use the
1517 * same PMK. */
1518 if (sm->proto == WPA_PROTO_RSN && wpa_key_mgmt_suite_b(sm->key_mgmt) &&
1519 !sm->cur_pmksa) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001520 struct rsn_pmksa_cache_entry *sa;
1521
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001522 sa = pmksa_cache_add(sm->pmksa, sm->pmk, sm->pmk_len, NULL,
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001523 sm->ptk.kck, sm->ptk.kck_len,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001524 sm->bssid, sm->own_addr,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001525 sm->network_ctx, sm->key_mgmt, NULL);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001526 if (!sm->cur_pmksa)
1527 sm->cur_pmksa = sa;
1528 }
1529
Dmitry Shmidt4dd28dc2015-03-10 11:21:43 -07001530 sm->msg_3_of_4_ok = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001531 return;
1532
1533failed:
1534 wpa_sm_deauthenticate(sm, WLAN_REASON_UNSPECIFIED);
1535}
1536
1537
1538static int wpa_supplicant_process_1_of_2_rsn(struct wpa_sm *sm,
1539 const u8 *keydata,
1540 size_t keydatalen,
1541 u16 key_info,
1542 struct wpa_gtk_data *gd)
1543{
1544 int maxkeylen;
1545 struct wpa_eapol_ie_parse ie;
1546
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08001547 wpa_hexdump_key(MSG_DEBUG, "RSN: msg 1/2 key data",
1548 keydata, keydatalen);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001549 if (wpa_supplicant_parse_ies(keydata, keydatalen, &ie) < 0)
1550 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001551 if (ie.gtk && !(key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
1552 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1553 "WPA: GTK IE in unencrypted key data");
1554 return -1;
1555 }
1556 if (ie.gtk == NULL) {
1557 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1558 "WPA: No GTK IE in Group Key msg 1/2");
1559 return -1;
1560 }
1561 maxkeylen = gd->gtk_len = ie.gtk_len - 2;
1562
Hai Shalom39bc25d2019-02-06 16:32:13 -08001563#ifdef CONFIG_OCV
1564 if (wpa_sm_ocv_enabled(sm)) {
1565 struct wpa_channel_info ci;
1566
1567 if (wpa_sm_channel_info(sm, &ci) != 0) {
1568 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1569 "Failed to get channel info to validate received OCI in EAPOL-Key group msg 1/2");
1570 return -1;
1571 }
1572
1573 if (ocv_verify_tx_params(ie.oci, ie.oci_len, &ci,
1574 channel_width_to_int(ci.chanwidth),
1575 ci.seg1_idx) != 0) {
1576 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, "%s",
1577 ocv_errorstr);
1578 return -1;
1579 }
1580 }
1581#endif /* CONFIG_OCV */
1582
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001583 if (wpa_supplicant_check_group_cipher(sm, sm->group_cipher,
1584 gd->gtk_len, maxkeylen,
1585 &gd->key_rsc_len, &gd->alg))
1586 return -1;
1587
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001588 wpa_hexdump_key(MSG_DEBUG, "RSN: received GTK in group key handshake",
1589 ie.gtk, ie.gtk_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001590 gd->keyidx = ie.gtk[0] & 0x3;
1591 gd->tx = wpa_supplicant_gtk_tx_bit_workaround(sm,
1592 !!(ie.gtk[0] & BIT(2)));
1593 if (ie.gtk_len - 2 > sizeof(gd->gtk)) {
1594 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1595 "RSN: Too long GTK in GTK IE (len=%lu)",
1596 (unsigned long) ie.gtk_len - 2);
1597 return -1;
1598 }
1599 os_memcpy(gd->gtk, ie.gtk + 2, ie.gtk_len - 2);
1600
1601 if (ieee80211w_set_keys(sm, &ie) < 0)
1602 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1603 "RSN: Failed to configure IGTK");
1604
1605 return 0;
1606}
1607
1608
1609static int wpa_supplicant_process_1_of_2_wpa(struct wpa_sm *sm,
1610 const struct wpa_eapol_key *key,
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001611 const u8 *key_data,
1612 size_t key_data_len, u16 key_info,
1613 u16 ver, struct wpa_gtk_data *gd)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001614{
1615 size_t maxkeylen;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001616 u16 gtk_len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001617
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001618 gtk_len = WPA_GET_BE16(key->key_length);
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001619 maxkeylen = key_data_len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001620 if (ver == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
1621 if (maxkeylen < 8) {
1622 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1623 "WPA: Too short maxkeylen (%lu)",
1624 (unsigned long) maxkeylen);
1625 return -1;
1626 }
1627 maxkeylen -= 8;
1628 }
1629
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001630 if (gtk_len > maxkeylen ||
1631 wpa_supplicant_check_group_cipher(sm, sm->group_cipher,
1632 gtk_len, maxkeylen,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001633 &gd->key_rsc_len, &gd->alg))
1634 return -1;
1635
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001636 gd->gtk_len = gtk_len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001637 gd->keyidx = (key_info & WPA_KEY_INFO_KEY_INDEX_MASK) >>
1638 WPA_KEY_INFO_KEY_INDEX_SHIFT;
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001639 if (ver == WPA_KEY_INFO_TYPE_HMAC_MD5_RC4 && sm->ptk.kek_len == 16) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001640#ifdef CONFIG_NO_RC4
1641 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1642 "WPA: RC4 not supported in the build");
1643 return -1;
1644#else /* CONFIG_NO_RC4 */
Dmitry Shmidt61593f02014-04-21 16:27:35 -07001645 u8 ek[32];
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001646 if (key_data_len > sizeof(gd->gtk)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001647 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1648 "WPA: RC4 key data too long (%lu)",
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001649 (unsigned long) key_data_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001650 return -1;
1651 }
Dmitry Shmidt61593f02014-04-21 16:27:35 -07001652 os_memcpy(ek, key->key_iv, 16);
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001653 os_memcpy(ek + 16, sm->ptk.kek, sm->ptk.kek_len);
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001654 os_memcpy(gd->gtk, key_data, key_data_len);
1655 if (rc4_skip(ek, 32, 256, gd->gtk, key_data_len)) {
Dmitry Shmidt61593f02014-04-21 16:27:35 -07001656 os_memset(ek, 0, sizeof(ek));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001657 wpa_msg(sm->ctx->msg_ctx, MSG_ERROR,
1658 "WPA: RC4 failed");
1659 return -1;
1660 }
Dmitry Shmidt61593f02014-04-21 16:27:35 -07001661 os_memset(ek, 0, sizeof(ek));
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001662#endif /* CONFIG_NO_RC4 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001663 } else if (ver == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001664 if (maxkeylen % 8) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001665 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1666 "WPA: Unsupported AES-WRAP len %lu",
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001667 (unsigned long) maxkeylen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001668 return -1;
1669 }
1670 if (maxkeylen > sizeof(gd->gtk)) {
1671 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1672 "WPA: AES-WRAP key data "
1673 "too long (keydatalen=%lu maxkeylen=%lu)",
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001674 (unsigned long) key_data_len,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001675 (unsigned long) maxkeylen);
1676 return -1;
1677 }
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001678 if (aes_unwrap(sm->ptk.kek, sm->ptk.kek_len, maxkeylen / 8,
1679 key_data, gd->gtk)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001680 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1681 "WPA: AES unwrap failed - could not decrypt "
1682 "GTK");
1683 return -1;
1684 }
1685 } else {
1686 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1687 "WPA: Unsupported key_info type %d", ver);
1688 return -1;
1689 }
1690 gd->tx = wpa_supplicant_gtk_tx_bit_workaround(
1691 sm, !!(key_info & WPA_KEY_INFO_TXRX));
1692 return 0;
1693}
1694
1695
1696static int wpa_supplicant_send_2_of_2(struct wpa_sm *sm,
1697 const struct wpa_eapol_key *key,
1698 int ver, u16 key_info)
1699{
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001700 size_t mic_len, hdrlen, rlen;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001701 struct wpa_eapol_key *reply;
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001702 u8 *rbuf, *key_mic;
Hai Shalom39bc25d2019-02-06 16:32:13 -08001703 size_t kde_len = 0;
1704
1705#ifdef CONFIG_OCV
1706 if (wpa_sm_ocv_enabled(sm))
1707 kde_len = OCV_OCI_KDE_LEN;
1708#endif /* CONFIG_OCV */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001709
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001710 mic_len = wpa_mic_len(sm->key_mgmt, sm->pmk_len);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001711 hdrlen = sizeof(*reply) + mic_len + 2;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001712 rbuf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_KEY, NULL,
Hai Shalom39bc25d2019-02-06 16:32:13 -08001713 hdrlen + kde_len, &rlen, (void *) &reply);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001714 if (rbuf == NULL)
1715 return -1;
1716
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001717 reply->type = (sm->proto == WPA_PROTO_RSN ||
1718 sm->proto == WPA_PROTO_OSEN) ?
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001719 EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA;
1720 key_info &= WPA_KEY_INFO_KEY_INDEX_MASK;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001721 key_info |= ver | WPA_KEY_INFO_SECURE;
1722 if (mic_len)
1723 key_info |= WPA_KEY_INFO_MIC;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001724 else
1725 key_info |= WPA_KEY_INFO_ENCR_KEY_DATA;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001726 WPA_PUT_BE16(reply->key_info, key_info);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001727 if (sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001728 WPA_PUT_BE16(reply->key_length, 0);
1729 else
1730 os_memcpy(reply->key_length, key->key_length, 2);
1731 os_memcpy(reply->replay_counter, key->replay_counter,
1732 WPA_REPLAY_COUNTER_LEN);
1733
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001734 key_mic = (u8 *) (reply + 1);
Hai Shalom39bc25d2019-02-06 16:32:13 -08001735 WPA_PUT_BE16(key_mic + mic_len, kde_len); /* Key Data Length */
1736
1737#ifdef CONFIG_OCV
1738 if (wpa_sm_ocv_enabled(sm)) {
1739 struct wpa_channel_info ci;
1740 u8 *pos;
1741
1742 if (wpa_sm_channel_info(sm, &ci) != 0) {
1743 wpa_printf(MSG_WARNING,
1744 "Failed to get channel info for OCI element in EAPOL-Key 2/2");
1745 os_free(rbuf);
1746 return -1;
1747 }
1748
1749 pos = key_mic + mic_len + 2; /* Key Data */
1750 if (ocv_insert_oci_kde(&ci, &pos) < 0) {
1751 os_free(rbuf);
1752 return -1;
1753 }
1754 }
1755#endif /* CONFIG_OCV */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001756
1757 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Sending EAPOL-Key 2/2");
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001758 return wpa_eapol_key_send(sm, &sm->ptk, ver, sm->bssid, ETH_P_EAPOL,
1759 rbuf, rlen, key_mic);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001760}
1761
1762
1763static void wpa_supplicant_process_1_of_2(struct wpa_sm *sm,
1764 const unsigned char *src_addr,
1765 const struct wpa_eapol_key *key,
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001766 const u8 *key_data,
1767 size_t key_data_len, u16 ver)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001768{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001769 u16 key_info;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001770 int rekey, ret;
1771 struct wpa_gtk_data gd;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001772 const u8 *key_rsc;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001773
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001774 if (!sm->msg_3_of_4_ok && !wpa_fils_is_completed(sm)) {
Dmitry Shmidt4dd28dc2015-03-10 11:21:43 -07001775 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1776 "WPA: Group Key Handshake started prior to completion of 4-way handshake");
1777 goto failed;
1778 }
1779
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001780 os_memset(&gd, 0, sizeof(gd));
1781
1782 rekey = wpa_sm_get_state(sm) == WPA_COMPLETED;
1783 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: RX message 1 of Group Key "
1784 "Handshake from " MACSTR " (ver=%d)", MAC2STR(src_addr), ver);
1785
1786 key_info = WPA_GET_BE16(key->key_info);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001787
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001788 if (sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN) {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001789 ret = wpa_supplicant_process_1_of_2_rsn(sm, key_data,
1790 key_data_len, key_info,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001791 &gd);
1792 } else {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001793 ret = wpa_supplicant_process_1_of_2_wpa(sm, key, key_data,
1794 key_data_len,
1795 key_info, ver, &gd);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001796 }
1797
1798 wpa_sm_set_state(sm, WPA_GROUP_HANDSHAKE);
1799
1800 if (ret)
1801 goto failed;
1802
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001803 key_rsc = key->key_rsc;
1804 if (wpa_supplicant_rsc_relaxation(sm, key->key_rsc))
1805 key_rsc = null_rsc;
1806
Jouni Malinen58c0e962017-10-01 12:12:24 +03001807 if (wpa_supplicant_install_gtk(sm, &gd, key_rsc, 0) ||
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001808 wpa_supplicant_send_2_of_2(sm, key, ver, key_info) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001809 goto failed;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001810 os_memset(&gd, 0, sizeof(gd));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001811
1812 if (rekey) {
1813 wpa_msg(sm->ctx->msg_ctx, MSG_INFO, "WPA: Group rekeying "
1814 "completed with " MACSTR " [GTK=%s]",
1815 MAC2STR(sm->bssid), wpa_cipher_txt(sm->group_cipher));
1816 wpa_sm_cancel_auth_timeout(sm);
1817 wpa_sm_set_state(sm, WPA_COMPLETED);
1818 } else {
1819 wpa_supplicant_key_neg_complete(sm, sm->bssid,
1820 key_info &
1821 WPA_KEY_INFO_SECURE);
1822 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001823
1824 wpa_sm_set_rekey_offload(sm);
1825
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001826 return;
1827
1828failed:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001829 os_memset(&gd, 0, sizeof(gd));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001830 wpa_sm_deauthenticate(sm, WLAN_REASON_UNSPECIFIED);
1831}
1832
1833
1834static int wpa_supplicant_verify_eapol_key_mic(struct wpa_sm *sm,
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001835 struct wpa_eapol_key *key,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001836 u16 ver,
1837 const u8 *buf, size_t len)
1838{
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001839 u8 mic[WPA_EAPOL_KEY_MIC_MAX_LEN];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001840 int ok = 0;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001841 size_t mic_len = wpa_mic_len(sm->key_mgmt, sm->pmk_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001842
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001843 os_memcpy(mic, key + 1, mic_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001844 if (sm->tptk_set) {
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001845 os_memset(key + 1, 0, mic_len);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001846 if (wpa_eapol_key_mic(sm->tptk.kck, sm->tptk.kck_len,
1847 sm->key_mgmt,
1848 ver, buf, len, (u8 *) (key + 1)) < 0 ||
1849 os_memcmp_const(mic, key + 1, mic_len) != 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001850 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1851 "WPA: Invalid EAPOL-Key MIC "
1852 "when using TPTK - ignoring TPTK");
1853 } else {
1854 ok = 1;
1855 sm->tptk_set = 0;
1856 sm->ptk_set = 1;
1857 os_memcpy(&sm->ptk, &sm->tptk, sizeof(sm->ptk));
Dmitry Shmidt61593f02014-04-21 16:27:35 -07001858 os_memset(&sm->tptk, 0, sizeof(sm->tptk));
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001859 /*
1860 * This assures the same TPTK in sm->tptk can never be
Roshan Pius3a1667e2018-07-03 15:17:14 -07001861 * copied twice to sm->ptk as the new PTK. In
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001862 * combination with the installed flag in the wpa_ptk
1863 * struct, this assures the same PTK is only installed
1864 * once.
1865 */
1866 sm->renew_snonce = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001867 }
1868 }
1869
1870 if (!ok && sm->ptk_set) {
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001871 os_memset(key + 1, 0, mic_len);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001872 if (wpa_eapol_key_mic(sm->ptk.kck, sm->ptk.kck_len,
1873 sm->key_mgmt,
1874 ver, buf, len, (u8 *) (key + 1)) < 0 ||
1875 os_memcmp_const(mic, key + 1, mic_len) != 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001876 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1877 "WPA: Invalid EAPOL-Key MIC - "
1878 "dropping packet");
1879 return -1;
1880 }
1881 ok = 1;
1882 }
1883
1884 if (!ok) {
1885 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1886 "WPA: Could not verify EAPOL-Key MIC - "
1887 "dropping packet");
1888 return -1;
1889 }
1890
1891 os_memcpy(sm->rx_replay_counter, key->replay_counter,
1892 WPA_REPLAY_COUNTER_LEN);
1893 sm->rx_replay_counter_set = 1;
1894 return 0;
1895}
1896
1897
1898/* Decrypt RSN EAPOL-Key key data (RC4 or AES-WRAP) */
1899static int wpa_supplicant_decrypt_key_data(struct wpa_sm *sm,
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001900 struct wpa_eapol_key *key,
1901 size_t mic_len, u16 ver,
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001902 u8 *key_data, size_t *key_data_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001903{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001904 wpa_hexdump(MSG_DEBUG, "RSN: encrypted key data",
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001905 key_data, *key_data_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001906 if (!sm->ptk_set) {
1907 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1908 "WPA: PTK not available, cannot decrypt EAPOL-Key Key "
1909 "Data");
1910 return -1;
1911 }
1912
1913 /* Decrypt key data here so that this operation does not need
1914 * to be implemented separately for each message type. */
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001915 if (ver == WPA_KEY_INFO_TYPE_HMAC_MD5_RC4 && sm->ptk.kek_len == 16) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001916#ifdef CONFIG_NO_RC4
1917 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1918 "WPA: RC4 not supported in the build");
1919 return -1;
1920#else /* CONFIG_NO_RC4 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001921 u8 ek[32];
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001922
1923 wpa_printf(MSG_DEBUG, "WPA: Decrypt Key Data using RC4");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001924 os_memcpy(ek, key->key_iv, 16);
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001925 os_memcpy(ek + 16, sm->ptk.kek, sm->ptk.kek_len);
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001926 if (rc4_skip(ek, 32, 256, key_data, *key_data_len)) {
Dmitry Shmidt61593f02014-04-21 16:27:35 -07001927 os_memset(ek, 0, sizeof(ek));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001928 wpa_msg(sm->ctx->msg_ctx, MSG_ERROR,
1929 "WPA: RC4 failed");
1930 return -1;
1931 }
Dmitry Shmidt61593f02014-04-21 16:27:35 -07001932 os_memset(ek, 0, sizeof(ek));
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001933#endif /* CONFIG_NO_RC4 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001934 } else if (ver == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES ||
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001935 ver == WPA_KEY_INFO_TYPE_AES_128_CMAC ||
Roshan Pius3a1667e2018-07-03 15:17:14 -07001936 wpa_use_aes_key_wrap(sm->key_mgmt)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001937 u8 *buf;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001938
1939 wpa_printf(MSG_DEBUG,
1940 "WPA: Decrypt Key Data using AES-UNWRAP (KEK length %u)",
1941 (unsigned int) sm->ptk.kek_len);
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001942 if (*key_data_len < 8 || *key_data_len % 8) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001943 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001944 "WPA: Unsupported AES-WRAP len %u",
1945 (unsigned int) *key_data_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001946 return -1;
1947 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001948 *key_data_len -= 8; /* AES-WRAP adds 8 bytes */
1949 buf = os_malloc(*key_data_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001950 if (buf == NULL) {
1951 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1952 "WPA: No memory for AES-UNWRAP buffer");
1953 return -1;
1954 }
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001955 if (aes_unwrap(sm->ptk.kek, sm->ptk.kek_len, *key_data_len / 8,
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001956 key_data, buf)) {
Dmitry Shmidt7d56b752015-12-22 10:59:44 -08001957 bin_clear_free(buf, *key_data_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001958 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1959 "WPA: AES unwrap failed - "
1960 "could not decrypt EAPOL-Key key data");
1961 return -1;
1962 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001963 os_memcpy(key_data, buf, *key_data_len);
Dmitry Shmidt7d56b752015-12-22 10:59:44 -08001964 bin_clear_free(buf, *key_data_len);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001965 WPA_PUT_BE16(((u8 *) (key + 1)) + mic_len, *key_data_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001966 } else {
1967 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1968 "WPA: Unsupported key_info type %d", ver);
1969 return -1;
1970 }
1971 wpa_hexdump_key(MSG_DEBUG, "WPA: decrypted EAPOL-Key key data",
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001972 key_data, *key_data_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001973 return 0;
1974}
1975
1976
1977/**
1978 * wpa_sm_aborted_cached - Notify WPA that PMKSA caching was aborted
1979 * @sm: Pointer to WPA state machine data from wpa_sm_init()
1980 */
1981void wpa_sm_aborted_cached(struct wpa_sm *sm)
1982{
1983 if (sm && sm->cur_pmksa) {
1984 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1985 "RSN: Cancelling PMKSA caching attempt");
1986 sm->cur_pmksa = NULL;
1987 }
1988}
1989
1990
1991static void wpa_eapol_key_dump(struct wpa_sm *sm,
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001992 const struct wpa_eapol_key *key,
1993 unsigned int key_data_len,
1994 const u8 *mic, unsigned int mic_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001995{
1996#ifndef CONFIG_NO_STDOUT_DEBUG
1997 u16 key_info = WPA_GET_BE16(key->key_info);
1998
1999 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, " EAPOL-Key type=%d", key->type);
2000 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2001 " key_info 0x%x (ver=%d keyidx=%d rsvd=%d %s%s%s%s%s%s%s%s)",
2002 key_info, key_info & WPA_KEY_INFO_TYPE_MASK,
2003 (key_info & WPA_KEY_INFO_KEY_INDEX_MASK) >>
2004 WPA_KEY_INFO_KEY_INDEX_SHIFT,
2005 (key_info & (BIT(13) | BIT(14) | BIT(15))) >> 13,
2006 key_info & WPA_KEY_INFO_KEY_TYPE ? "Pairwise" : "Group",
2007 key_info & WPA_KEY_INFO_INSTALL ? " Install" : "",
2008 key_info & WPA_KEY_INFO_ACK ? " Ack" : "",
2009 key_info & WPA_KEY_INFO_MIC ? " MIC" : "",
2010 key_info & WPA_KEY_INFO_SECURE ? " Secure" : "",
2011 key_info & WPA_KEY_INFO_ERROR ? " Error" : "",
2012 key_info & WPA_KEY_INFO_REQUEST ? " Request" : "",
2013 key_info & WPA_KEY_INFO_ENCR_KEY_DATA ? " Encr" : "");
2014 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2015 " key_length=%u key_data_length=%u",
Dmitry Shmidt807291d2015-01-27 13:40:23 -08002016 WPA_GET_BE16(key->key_length), key_data_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002017 wpa_hexdump(MSG_DEBUG, " replay_counter",
2018 key->replay_counter, WPA_REPLAY_COUNTER_LEN);
2019 wpa_hexdump(MSG_DEBUG, " key_nonce", key->key_nonce, WPA_NONCE_LEN);
2020 wpa_hexdump(MSG_DEBUG, " key_iv", key->key_iv, 16);
2021 wpa_hexdump(MSG_DEBUG, " key_rsc", key->key_rsc, 8);
2022 wpa_hexdump(MSG_DEBUG, " key_id (reserved)", key->key_id, 8);
Dmitry Shmidt807291d2015-01-27 13:40:23 -08002023 wpa_hexdump(MSG_DEBUG, " key_mic", mic, mic_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002024#endif /* CONFIG_NO_STDOUT_DEBUG */
2025}
2026
2027
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002028#ifdef CONFIG_FILS
2029static int wpa_supp_aead_decrypt(struct wpa_sm *sm, u8 *buf, size_t buf_len,
2030 size_t *key_data_len)
2031{
2032 struct wpa_ptk *ptk;
2033 struct ieee802_1x_hdr *hdr;
2034 struct wpa_eapol_key *key;
2035 u8 *pos, *tmp;
2036 const u8 *aad[1];
2037 size_t aad_len[1];
2038
2039 if (*key_data_len < AES_BLOCK_SIZE) {
2040 wpa_printf(MSG_INFO, "No room for AES-SIV data in the frame");
2041 return -1;
2042 }
2043
2044 if (sm->tptk_set)
2045 ptk = &sm->tptk;
2046 else if (sm->ptk_set)
2047 ptk = &sm->ptk;
2048 else
2049 return -1;
2050
2051 hdr = (struct ieee802_1x_hdr *) buf;
2052 key = (struct wpa_eapol_key *) (hdr + 1);
2053 pos = (u8 *) (key + 1);
2054 pos += 2; /* Pointing at the Encrypted Key Data field */
2055
2056 tmp = os_malloc(*key_data_len);
2057 if (!tmp)
2058 return -1;
2059
2060 /* AES-SIV AAD from EAPOL protocol version field (inclusive) to
2061 * to Key Data (exclusive). */
2062 aad[0] = buf;
2063 aad_len[0] = pos - buf;
2064 if (aes_siv_decrypt(ptk->kek, ptk->kek_len, pos, *key_data_len,
2065 1, aad, aad_len, tmp) < 0) {
2066 wpa_printf(MSG_INFO, "Invalid AES-SIV data in the frame");
2067 bin_clear_free(tmp, *key_data_len);
2068 return -1;
2069 }
2070
2071 /* AEAD decryption and validation completed successfully */
2072 (*key_data_len) -= AES_BLOCK_SIZE;
2073 wpa_hexdump_key(MSG_DEBUG, "WPA: Decrypted Key Data",
2074 tmp, *key_data_len);
2075
2076 /* Replace Key Data field with the decrypted version */
2077 os_memcpy(pos, tmp, *key_data_len);
2078 pos -= 2; /* Key Data Length field */
2079 WPA_PUT_BE16(pos, *key_data_len);
2080 bin_clear_free(tmp, *key_data_len);
2081
2082 if (sm->tptk_set) {
2083 sm->tptk_set = 0;
2084 sm->ptk_set = 1;
2085 os_memcpy(&sm->ptk, &sm->tptk, sizeof(sm->ptk));
2086 os_memset(&sm->tptk, 0, sizeof(sm->tptk));
2087 }
2088
2089 os_memcpy(sm->rx_replay_counter, key->replay_counter,
2090 WPA_REPLAY_COUNTER_LEN);
2091 sm->rx_replay_counter_set = 1;
2092
2093 return 0;
2094}
2095#endif /* CONFIG_FILS */
2096
2097
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002098/**
2099 * wpa_sm_rx_eapol - Process received WPA EAPOL frames
2100 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2101 * @src_addr: Source MAC address of the EAPOL packet
2102 * @buf: Pointer to the beginning of the EAPOL data (EAPOL header)
2103 * @len: Length of the EAPOL frame
2104 * Returns: 1 = WPA EAPOL-Key processed, 0 = not a WPA EAPOL-Key, -1 failure
2105 *
2106 * This function is called for each received EAPOL frame. Other than EAPOL-Key
2107 * frames can be skipped if filtering is done elsewhere. wpa_sm_rx_eapol() is
2108 * only processing WPA and WPA2 EAPOL-Key frames.
2109 *
2110 * The received EAPOL-Key packets are validated and valid packets are replied
2111 * to. In addition, key material (PTK, GTK) is configured at the end of a
2112 * successful key handshake.
2113 */
2114int wpa_sm_rx_eapol(struct wpa_sm *sm, const u8 *src_addr,
2115 const u8 *buf, size_t len)
2116{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002117 size_t plen, data_len, key_data_len;
2118 const struct ieee802_1x_hdr *hdr;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002119 struct wpa_eapol_key *key;
2120 u16 key_info, ver;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002121 u8 *tmp = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002122 int ret = -1;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002123 u8 *mic, *key_data;
Dmitry Shmidt807291d2015-01-27 13:40:23 -08002124 size_t mic_len, keyhdrlen;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002125
2126#ifdef CONFIG_IEEE80211R
2127 sm->ft_completed = 0;
2128#endif /* CONFIG_IEEE80211R */
2129
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002130 mic_len = wpa_mic_len(sm->key_mgmt, sm->pmk_len);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002131 keyhdrlen = sizeof(*key) + mic_len + 2;
Dmitry Shmidt807291d2015-01-27 13:40:23 -08002132
2133 if (len < sizeof(*hdr) + keyhdrlen) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002134 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2135 "WPA: EAPOL frame too short to be a WPA "
2136 "EAPOL-Key (len %lu, expecting at least %lu)",
2137 (unsigned long) len,
Dmitry Shmidt807291d2015-01-27 13:40:23 -08002138 (unsigned long) sizeof(*hdr) + keyhdrlen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002139 return 0;
2140 }
2141
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002142 hdr = (const struct ieee802_1x_hdr *) buf;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002143 plen = be_to_host16(hdr->length);
2144 data_len = plen + sizeof(*hdr);
2145 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2146 "IEEE 802.1X RX: version=%d type=%d length=%lu",
2147 hdr->version, hdr->type, (unsigned long) plen);
2148
2149 if (hdr->version < EAPOL_VERSION) {
2150 /* TODO: backwards compatibility */
2151 }
2152 if (hdr->type != IEEE802_1X_TYPE_EAPOL_KEY) {
2153 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2154 "WPA: EAPOL frame (type %u) discarded, "
2155 "not a Key frame", hdr->type);
2156 ret = 0;
2157 goto out;
2158 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002159 wpa_hexdump(MSG_MSGDUMP, "WPA: RX EAPOL-Key", buf, len);
Dmitry Shmidt807291d2015-01-27 13:40:23 -08002160 if (plen > len - sizeof(*hdr) || plen < keyhdrlen) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002161 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2162 "WPA: EAPOL frame payload size %lu "
2163 "invalid (frame size %lu)",
2164 (unsigned long) plen, (unsigned long) len);
2165 ret = 0;
2166 goto out;
2167 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002168 if (data_len < len) {
2169 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2170 "WPA: ignoring %lu bytes after the IEEE 802.1X data",
2171 (unsigned long) len - data_len);
2172 }
2173
2174 /*
2175 * Make a copy of the frame since we need to modify the buffer during
2176 * MAC validation and Key Data decryption.
2177 */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002178 tmp = os_memdup(buf, data_len);
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002179 if (tmp == NULL)
2180 goto out;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002181 key = (struct wpa_eapol_key *) (tmp + sizeof(struct ieee802_1x_hdr));
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002182 mic = (u8 *) (key + 1);
2183 key_data = mic + mic_len + 2;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002184
2185 if (key->type != EAPOL_KEY_TYPE_WPA && key->type != EAPOL_KEY_TYPE_RSN)
2186 {
2187 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2188 "WPA: EAPOL-Key type (%d) unknown, discarded",
2189 key->type);
2190 ret = 0;
2191 goto out;
2192 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002193
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002194 key_data_len = WPA_GET_BE16(mic + mic_len);
2195 wpa_eapol_key_dump(sm, key, key_data_len, mic, mic_len);
Dmitry Shmidt807291d2015-01-27 13:40:23 -08002196
2197 if (key_data_len > plen - keyhdrlen) {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002198 wpa_msg(sm->ctx->msg_ctx, MSG_INFO, "WPA: Invalid EAPOL-Key "
2199 "frame - key_data overflow (%u > %u)",
2200 (unsigned int) key_data_len,
Dmitry Shmidt807291d2015-01-27 13:40:23 -08002201 (unsigned int) (plen - keyhdrlen));
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002202 goto out;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002203 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002204
2205 eapol_sm_notify_lower_layer_success(sm->eapol, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002206 key_info = WPA_GET_BE16(key->key_info);
2207 ver = key_info & WPA_KEY_INFO_TYPE_MASK;
2208 if (ver != WPA_KEY_INFO_TYPE_HMAC_MD5_RC4 &&
2209#if defined(CONFIG_IEEE80211R) || defined(CONFIG_IEEE80211W)
2210 ver != WPA_KEY_INFO_TYPE_AES_128_CMAC &&
2211#endif /* CONFIG_IEEE80211R || CONFIG_IEEE80211W */
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002212 ver != WPA_KEY_INFO_TYPE_HMAC_SHA1_AES &&
Roshan Pius3a1667e2018-07-03 15:17:14 -07002213 !wpa_use_akm_defined(sm->key_mgmt)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002214 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2215 "WPA: Unsupported EAPOL-Key descriptor version %d",
2216 ver);
2217 goto out;
2218 }
2219
Roshan Pius3a1667e2018-07-03 15:17:14 -07002220 if (wpa_use_akm_defined(sm->key_mgmt) &&
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002221 ver != WPA_KEY_INFO_TYPE_AKM_DEFINED) {
2222 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2223 "RSN: Unsupported EAPOL-Key descriptor version %d (expected AKM defined = 0)",
2224 ver);
2225 goto out;
2226 }
2227
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002228#ifdef CONFIG_IEEE80211R
2229 if (wpa_key_mgmt_ft(sm->key_mgmt)) {
2230 /* IEEE 802.11r uses a new key_info type (AES-128-CMAC). */
Roshan Pius3a1667e2018-07-03 15:17:14 -07002231 if (ver != WPA_KEY_INFO_TYPE_AES_128_CMAC &&
2232 !wpa_use_akm_defined(sm->key_mgmt)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002233 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2234 "FT: AP did not use AES-128-CMAC");
2235 goto out;
2236 }
2237 } else
2238#endif /* CONFIG_IEEE80211R */
2239#ifdef CONFIG_IEEE80211W
2240 if (wpa_key_mgmt_sha256(sm->key_mgmt)) {
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002241 if (ver != WPA_KEY_INFO_TYPE_AES_128_CMAC &&
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: AP did not use the "
2245 "negotiated AES-128-CMAC");
2246 goto out;
2247 }
2248 } else
2249#endif /* CONFIG_IEEE80211W */
2250 if (sm->pairwise_cipher == WPA_CIPHER_CCMP &&
Roshan Pius3a1667e2018-07-03 15:17:14 -07002251 !wpa_use_akm_defined(sm->key_mgmt) &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002252 ver != WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
2253 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2254 "WPA: CCMP is used, but EAPOL-Key "
2255 "descriptor version (%d) is not 2", ver);
2256 if (sm->group_cipher != WPA_CIPHER_CCMP &&
2257 !(key_info & WPA_KEY_INFO_KEY_TYPE)) {
2258 /* Earlier versions of IEEE 802.11i did not explicitly
2259 * require version 2 descriptor for all EAPOL-Key
2260 * packets, so allow group keys to use version 1 if
2261 * CCMP is not used for them. */
2262 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2263 "WPA: Backwards compatibility: allow invalid "
2264 "version for non-CCMP group keys");
Jouni Malinen658fb4a2014-11-14 20:57:05 +02002265 } else if (ver == WPA_KEY_INFO_TYPE_AES_128_CMAC) {
2266 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2267 "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 -07002268 } else
2269 goto out;
Dmitry Shmidt71757432014-06-02 13:50:35 -07002270 } else if (sm->pairwise_cipher == WPA_CIPHER_GCMP &&
Roshan Pius3a1667e2018-07-03 15:17:14 -07002271 !wpa_use_akm_defined(sm->key_mgmt) &&
Dmitry Shmidt71757432014-06-02 13:50:35 -07002272 ver != WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002273 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2274 "WPA: GCMP is used, but EAPOL-Key "
2275 "descriptor version (%d) is not 2", ver);
2276 goto out;
2277 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002278
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002279 if (sm->rx_replay_counter_set &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002280 os_memcmp(key->replay_counter, sm->rx_replay_counter,
2281 WPA_REPLAY_COUNTER_LEN) <= 0) {
2282 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
2283 "WPA: EAPOL-Key Replay Counter did not increase - "
2284 "dropping packet");
2285 goto out;
2286 }
2287
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002288 if (key_info & WPA_KEY_INFO_SMK_MESSAGE) {
2289 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2290 "WPA: Unsupported SMK bit in key_info");
2291 goto out;
2292 }
2293
2294 if (!(key_info & WPA_KEY_INFO_ACK)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002295 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2296 "WPA: No Ack bit in key_info");
2297 goto out;
2298 }
2299
2300 if (key_info & WPA_KEY_INFO_REQUEST) {
2301 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2302 "WPA: EAPOL-Key with Request bit - dropped");
2303 goto out;
2304 }
2305
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002306 if ((key_info & WPA_KEY_INFO_MIC) &&
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002307 wpa_supplicant_verify_eapol_key_mic(sm, key, ver, tmp, data_len))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002308 goto out;
2309
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002310#ifdef CONFIG_FILS
2311 if (!mic_len && (key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
2312 if (wpa_supp_aead_decrypt(sm, tmp, data_len, &key_data_len))
2313 goto out;
2314 }
2315#endif /* CONFIG_FILS */
2316
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002317 if ((sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN) &&
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002318 (key_info & WPA_KEY_INFO_ENCR_KEY_DATA) && mic_len) {
Hai Shalomce48b4a2018-09-05 11:41:35 -07002319 /*
2320 * Only decrypt the Key Data field if the frame's authenticity
2321 * was verified. When using AES-SIV (FILS), the MIC flag is not
2322 * set, so this check should only be performed if mic_len != 0
2323 * which is the case in this code branch.
2324 */
2325 if (!(key_info & WPA_KEY_INFO_MIC)) {
2326 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
2327 "WPA: Ignore EAPOL-Key with encrypted but unauthenticated data");
2328 goto out;
2329 }
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002330 if (wpa_supplicant_decrypt_key_data(sm, key, mic_len,
2331 ver, key_data,
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002332 &key_data_len))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002333 goto out;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002334 }
2335
2336 if (key_info & WPA_KEY_INFO_KEY_TYPE) {
2337 if (key_info & WPA_KEY_INFO_KEY_INDEX_MASK) {
2338 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
2339 "WPA: Ignored EAPOL-Key (Pairwise) with "
2340 "non-zero key index");
2341 goto out;
2342 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002343 if (key_info & (WPA_KEY_INFO_MIC |
2344 WPA_KEY_INFO_ENCR_KEY_DATA)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002345 /* 3/4 4-Way Handshake */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002346 wpa_supplicant_process_3_of_4(sm, key, ver, key_data,
2347 key_data_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002348 } else {
2349 /* 1/4 4-Way Handshake */
2350 wpa_supplicant_process_1_of_4(sm, src_addr, key,
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002351 ver, key_data,
2352 key_data_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002353 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002354 } else {
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002355 if ((mic_len && (key_info & WPA_KEY_INFO_MIC)) ||
2356 (!mic_len && (key_info & WPA_KEY_INFO_ENCR_KEY_DATA))) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002357 /* 1/2 Group Key Handshake */
2358 wpa_supplicant_process_1_of_2(sm, src_addr, key,
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002359 key_data, key_data_len,
2360 ver);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002361 } else {
2362 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002363 "WPA: EAPOL-Key (Group) without Mic/Encr bit - "
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002364 "dropped");
2365 }
2366 }
2367
2368 ret = 1;
2369
2370out:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002371 bin_clear_free(tmp, data_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002372 return ret;
2373}
2374
2375
2376#ifdef CONFIG_CTRL_IFACE
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002377static u32 wpa_key_mgmt_suite(struct wpa_sm *sm)
2378{
2379 switch (sm->key_mgmt) {
2380 case WPA_KEY_MGMT_IEEE8021X:
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002381 return ((sm->proto == WPA_PROTO_RSN ||
2382 sm->proto == WPA_PROTO_OSEN) ?
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002383 RSN_AUTH_KEY_MGMT_UNSPEC_802_1X :
2384 WPA_AUTH_KEY_MGMT_UNSPEC_802_1X);
2385 case WPA_KEY_MGMT_PSK:
2386 return (sm->proto == WPA_PROTO_RSN ?
2387 RSN_AUTH_KEY_MGMT_PSK_OVER_802_1X :
2388 WPA_AUTH_KEY_MGMT_PSK_OVER_802_1X);
2389#ifdef CONFIG_IEEE80211R
2390 case WPA_KEY_MGMT_FT_IEEE8021X:
2391 return RSN_AUTH_KEY_MGMT_FT_802_1X;
2392 case WPA_KEY_MGMT_FT_PSK:
2393 return RSN_AUTH_KEY_MGMT_FT_PSK;
2394#endif /* CONFIG_IEEE80211R */
2395#ifdef CONFIG_IEEE80211W
2396 case WPA_KEY_MGMT_IEEE8021X_SHA256:
2397 return RSN_AUTH_KEY_MGMT_802_1X_SHA256;
2398 case WPA_KEY_MGMT_PSK_SHA256:
2399 return RSN_AUTH_KEY_MGMT_PSK_SHA256;
2400#endif /* CONFIG_IEEE80211W */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002401 case WPA_KEY_MGMT_CCKM:
2402 return (sm->proto == WPA_PROTO_RSN ?
2403 RSN_AUTH_KEY_MGMT_CCKM:
2404 WPA_AUTH_KEY_MGMT_CCKM);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002405 case WPA_KEY_MGMT_WPA_NONE:
2406 return WPA_AUTH_KEY_MGMT_NONE;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002407 case WPA_KEY_MGMT_IEEE8021X_SUITE_B:
2408 return RSN_AUTH_KEY_MGMT_802_1X_SUITE_B;
Dmitry Shmidt807291d2015-01-27 13:40:23 -08002409 case WPA_KEY_MGMT_IEEE8021X_SUITE_B_192:
2410 return RSN_AUTH_KEY_MGMT_802_1X_SUITE_B_192;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002411 default:
2412 return 0;
2413 }
2414}
2415
2416
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002417#define RSN_SUITE "%02x-%02x-%02x-%d"
2418#define RSN_SUITE_ARG(s) \
2419((s) >> 24) & 0xff, ((s) >> 16) & 0xff, ((s) >> 8) & 0xff, (s) & 0xff
2420
2421/**
2422 * wpa_sm_get_mib - Dump text list of MIB entries
2423 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2424 * @buf: Buffer for the list
2425 * @buflen: Length of the buffer
2426 * Returns: Number of bytes written to buffer
2427 *
2428 * This function is used fetch dot11 MIB variables.
2429 */
2430int wpa_sm_get_mib(struct wpa_sm *sm, char *buf, size_t buflen)
2431{
2432 char pmkid_txt[PMKID_LEN * 2 + 1];
2433 int rsna, ret;
2434 size_t len;
2435
2436 if (sm->cur_pmksa) {
2437 wpa_snprintf_hex(pmkid_txt, sizeof(pmkid_txt),
2438 sm->cur_pmksa->pmkid, PMKID_LEN);
2439 } else
2440 pmkid_txt[0] = '\0';
2441
2442 if ((wpa_key_mgmt_wpa_psk(sm->key_mgmt) ||
2443 wpa_key_mgmt_wpa_ieee8021x(sm->key_mgmt)) &&
2444 sm->proto == WPA_PROTO_RSN)
2445 rsna = 1;
2446 else
2447 rsna = 0;
2448
2449 ret = os_snprintf(buf, buflen,
2450 "dot11RSNAOptionImplemented=TRUE\n"
2451 "dot11RSNAPreauthenticationImplemented=TRUE\n"
2452 "dot11RSNAEnabled=%s\n"
2453 "dot11RSNAPreauthenticationEnabled=%s\n"
2454 "dot11RSNAConfigVersion=%d\n"
2455 "dot11RSNAConfigPairwiseKeysSupported=5\n"
2456 "dot11RSNAConfigGroupCipherSize=%d\n"
2457 "dot11RSNAConfigPMKLifetime=%d\n"
2458 "dot11RSNAConfigPMKReauthThreshold=%d\n"
2459 "dot11RSNAConfigNumberOfPTKSAReplayCounters=1\n"
2460 "dot11RSNAConfigSATimeout=%d\n",
2461 rsna ? "TRUE" : "FALSE",
2462 rsna ? "TRUE" : "FALSE",
2463 RSN_VERSION,
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07002464 wpa_cipher_key_len(sm->group_cipher) * 8,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002465 sm->dot11RSNAConfigPMKLifetime,
2466 sm->dot11RSNAConfigPMKReauthThreshold,
2467 sm->dot11RSNAConfigSATimeout);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002468 if (os_snprintf_error(buflen, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002469 return 0;
2470 len = ret;
2471
2472 ret = os_snprintf(
2473 buf + len, buflen - len,
2474 "dot11RSNAAuthenticationSuiteSelected=" RSN_SUITE "\n"
2475 "dot11RSNAPairwiseCipherSelected=" RSN_SUITE "\n"
2476 "dot11RSNAGroupCipherSelected=" RSN_SUITE "\n"
2477 "dot11RSNAPMKIDUsed=%s\n"
2478 "dot11RSNAAuthenticationSuiteRequested=" RSN_SUITE "\n"
2479 "dot11RSNAPairwiseCipherRequested=" RSN_SUITE "\n"
2480 "dot11RSNAGroupCipherRequested=" RSN_SUITE "\n"
2481 "dot11RSNAConfigNumberOfGTKSAReplayCounters=0\n"
2482 "dot11RSNA4WayHandshakeFailures=%u\n",
2483 RSN_SUITE_ARG(wpa_key_mgmt_suite(sm)),
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07002484 RSN_SUITE_ARG(wpa_cipher_to_suite(sm->proto,
2485 sm->pairwise_cipher)),
2486 RSN_SUITE_ARG(wpa_cipher_to_suite(sm->proto,
2487 sm->group_cipher)),
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002488 pmkid_txt,
2489 RSN_SUITE_ARG(wpa_key_mgmt_suite(sm)),
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07002490 RSN_SUITE_ARG(wpa_cipher_to_suite(sm->proto,
2491 sm->pairwise_cipher)),
2492 RSN_SUITE_ARG(wpa_cipher_to_suite(sm->proto,
2493 sm->group_cipher)),
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002494 sm->dot11RSNA4WayHandshakeFailures);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002495 if (!os_snprintf_error(buflen - len, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002496 len += ret;
2497
2498 return (int) len;
2499}
2500#endif /* CONFIG_CTRL_IFACE */
2501
2502
2503static void wpa_sm_pmksa_free_cb(struct rsn_pmksa_cache_entry *entry,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002504 void *ctx, enum pmksa_free_reason reason)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002505{
2506 struct wpa_sm *sm = ctx;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002507 int deauth = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002508
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002509 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "RSN: PMKSA cache entry free_cb: "
2510 MACSTR " reason=%d", MAC2STR(entry->aa), reason);
2511
2512 if (sm->cur_pmksa == entry) {
2513 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2514 "RSN: %s current PMKSA entry",
2515 reason == PMKSA_REPLACE ? "replaced" : "removed");
2516 pmksa_cache_clear_current(sm);
2517
2518 /*
2519 * If an entry is simply being replaced, there's no need to
2520 * deauthenticate because it will be immediately re-added.
2521 * This happens when EAP authentication is completed again
2522 * (reauth or failed PMKSA caching attempt).
2523 */
2524 if (reason != PMKSA_REPLACE)
2525 deauth = 1;
2526 }
2527
2528 if (reason == PMKSA_EXPIRE &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002529 (sm->pmk_len == entry->pmk_len &&
2530 os_memcmp(sm->pmk, entry->pmk, sm->pmk_len) == 0)) {
2531 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002532 "RSN: deauthenticating due to expired PMK");
2533 pmksa_cache_clear_current(sm);
2534 deauth = 1;
2535 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002536
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002537 if (deauth) {
Roshan Pius3a1667e2018-07-03 15:17:14 -07002538 sm->pmk_len = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002539 os_memset(sm->pmk, 0, sizeof(sm->pmk));
2540 wpa_sm_deauthenticate(sm, WLAN_REASON_UNSPECIFIED);
2541 }
2542}
2543
2544
2545/**
2546 * wpa_sm_init - Initialize WPA state machine
2547 * @ctx: Context pointer for callbacks; this needs to be an allocated buffer
2548 * Returns: Pointer to the allocated WPA state machine data
2549 *
2550 * This function is used to allocate a new WPA state machine and the returned
2551 * value is passed to all WPA state machine calls.
2552 */
2553struct wpa_sm * wpa_sm_init(struct wpa_sm_ctx *ctx)
2554{
2555 struct wpa_sm *sm;
2556
2557 sm = os_zalloc(sizeof(*sm));
2558 if (sm == NULL)
2559 return NULL;
2560 dl_list_init(&sm->pmksa_candidates);
2561 sm->renew_snonce = 1;
2562 sm->ctx = ctx;
2563
2564 sm->dot11RSNAConfigPMKLifetime = 43200;
2565 sm->dot11RSNAConfigPMKReauthThreshold = 70;
2566 sm->dot11RSNAConfigSATimeout = 60;
2567
2568 sm->pmksa = pmksa_cache_init(wpa_sm_pmksa_free_cb, sm, sm);
2569 if (sm->pmksa == NULL) {
2570 wpa_msg(sm->ctx->msg_ctx, MSG_ERROR,
2571 "RSN: PMKSA cache initialization failed");
2572 os_free(sm);
2573 return NULL;
2574 }
2575
2576 return sm;
2577}
2578
2579
2580/**
2581 * wpa_sm_deinit - Deinitialize WPA state machine
2582 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2583 */
2584void wpa_sm_deinit(struct wpa_sm *sm)
2585{
2586 if (sm == NULL)
2587 return;
2588 pmksa_cache_deinit(sm->pmksa);
2589 eloop_cancel_timeout(wpa_sm_start_preauth, sm, NULL);
2590 eloop_cancel_timeout(wpa_sm_rekey_ptk, sm, NULL);
2591 os_free(sm->assoc_wpa_ie);
2592 os_free(sm->ap_wpa_ie);
2593 os_free(sm->ap_rsn_ie);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002594 wpa_sm_drop_sa(sm);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002595 os_free(sm->ctx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002596#ifdef CONFIG_IEEE80211R
2597 os_free(sm->assoc_resp_ies);
2598#endif /* CONFIG_IEEE80211R */
Dmitry Shmidt55840ad2015-12-14 12:45:46 -08002599#ifdef CONFIG_TESTING_OPTIONS
2600 wpabuf_free(sm->test_assoc_ie);
2601#endif /* CONFIG_TESTING_OPTIONS */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002602#ifdef CONFIG_FILS_SK_PFS
2603 crypto_ecdh_deinit(sm->fils_ecdh);
2604#endif /* CONFIG_FILS_SK_PFS */
2605#ifdef CONFIG_FILS
2606 wpabuf_free(sm->fils_ft_ies);
2607#endif /* CONFIG_FILS */
2608#ifdef CONFIG_OWE
2609 crypto_ecdh_deinit(sm->owe_ecdh);
2610#endif /* CONFIG_OWE */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002611 os_free(sm);
2612}
2613
2614
2615/**
2616 * wpa_sm_notify_assoc - Notify WPA state machine about association
2617 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2618 * @bssid: The BSSID of the new association
2619 *
2620 * This function is called to let WPA state machine know that the connection
2621 * was established.
2622 */
2623void wpa_sm_notify_assoc(struct wpa_sm *sm, const u8 *bssid)
2624{
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02002625 int clear_keys = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002626
2627 if (sm == NULL)
2628 return;
2629
2630 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2631 "WPA: Association event - clear replay counter");
2632 os_memcpy(sm->bssid, bssid, ETH_ALEN);
2633 os_memset(sm->rx_replay_counter, 0, WPA_REPLAY_COUNTER_LEN);
2634 sm->rx_replay_counter_set = 0;
2635 sm->renew_snonce = 1;
2636 if (os_memcmp(sm->preauth_bssid, bssid, ETH_ALEN) == 0)
2637 rsn_preauth_deinit(sm);
2638
2639#ifdef CONFIG_IEEE80211R
2640 if (wpa_ft_is_completed(sm)) {
2641 /*
2642 * Clear portValid to kick EAPOL state machine to re-enter
2643 * AUTHENTICATED state to get the EAPOL port Authorized.
2644 */
2645 eapol_sm_notify_portValid(sm->eapol, FALSE);
2646 wpa_supplicant_key_neg_complete(sm, sm->bssid, 1);
2647
2648 /* Prepare for the next transition */
2649 wpa_ft_prepare_auth_request(sm, NULL);
2650
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02002651 clear_keys = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002652 }
2653#endif /* CONFIG_IEEE80211R */
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002654#ifdef CONFIG_FILS
2655 if (sm->fils_completed) {
2656 /*
2657 * Clear portValid to kick EAPOL state machine to re-enter
2658 * AUTHENTICATED state to get the EAPOL port Authorized.
2659 */
2660 wpa_supplicant_key_neg_complete(sm, sm->bssid, 1);
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02002661 clear_keys = 0;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002662 }
2663#endif /* CONFIG_FILS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002664
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02002665 if (clear_keys) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002666 /*
2667 * IEEE 802.11, 8.4.10: Delete PTK SA on (re)association if
2668 * this is not part of a Fast BSS Transition.
2669 */
2670 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Clear old PTK");
2671 sm->ptk_set = 0;
Dmitry Shmidt61593f02014-04-21 16:27:35 -07002672 os_memset(&sm->ptk, 0, sizeof(sm->ptk));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002673 sm->tptk_set = 0;
Dmitry Shmidt61593f02014-04-21 16:27:35 -07002674 os_memset(&sm->tptk, 0, sizeof(sm->tptk));
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02002675 os_memset(&sm->gtk, 0, sizeof(sm->gtk));
Jouni Malinen58c0e962017-10-01 12:12:24 +03002676 os_memset(&sm->gtk_wnm_sleep, 0, sizeof(sm->gtk_wnm_sleep));
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02002677#ifdef CONFIG_IEEE80211W
2678 os_memset(&sm->igtk, 0, sizeof(sm->igtk));
Jouni Malinen58c0e962017-10-01 12:12:24 +03002679 os_memset(&sm->igtk_wnm_sleep, 0, sizeof(sm->igtk_wnm_sleep));
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02002680#endif /* CONFIG_IEEE80211W */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002681 }
2682
2683#ifdef CONFIG_TDLS
2684 wpa_tdls_assoc(sm);
2685#endif /* CONFIG_TDLS */
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002686
2687#ifdef CONFIG_P2P
2688 os_memset(sm->p2p_ip_addr, 0, sizeof(sm->p2p_ip_addr));
2689#endif /* CONFIG_P2P */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002690}
2691
2692
2693/**
2694 * wpa_sm_notify_disassoc - Notify WPA state machine about disassociation
2695 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2696 *
2697 * This function is called to let WPA state machine know that the connection
2698 * was lost. This will abort any existing pre-authentication session.
2699 */
2700void wpa_sm_notify_disassoc(struct wpa_sm *sm)
2701{
Dmitry Shmidt4dd28dc2015-03-10 11:21:43 -07002702 eloop_cancel_timeout(wpa_sm_start_preauth, sm, NULL);
2703 eloop_cancel_timeout(wpa_sm_rekey_ptk, sm, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002704 rsn_preauth_deinit(sm);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002705 pmksa_cache_clear_current(sm);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002706 if (wpa_sm_get_state(sm) == WPA_4WAY_HANDSHAKE)
2707 sm->dot11RSNA4WayHandshakeFailures++;
2708#ifdef CONFIG_TDLS
2709 wpa_tdls_disassoc(sm);
2710#endif /* CONFIG_TDLS */
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002711#ifdef CONFIG_FILS
2712 sm->fils_completed = 0;
2713#endif /* CONFIG_FILS */
Jouni Malinen4283f9e2017-09-22 12:06:37 +03002714#ifdef CONFIG_IEEE80211R
2715 sm->ft_reassoc_completed = 0;
2716#endif /* CONFIG_IEEE80211R */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002717
2718 /* Keys are not needed in the WPA state machine anymore */
2719 wpa_sm_drop_sa(sm);
Dmitry Shmidt4dd28dc2015-03-10 11:21:43 -07002720
2721 sm->msg_3_of_4_ok = 0;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002722 os_memset(sm->bssid, 0, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002723}
2724
2725
2726/**
2727 * wpa_sm_set_pmk - Set PMK
2728 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2729 * @pmk: The new PMK
2730 * @pmk_len: The length of the new PMK in bytes
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08002731 * @pmkid: Calculated PMKID
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002732 * @bssid: AA to add into PMKSA cache or %NULL to not cache the PMK
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002733 *
2734 * Configure the PMK for WPA state machine.
2735 */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002736void wpa_sm_set_pmk(struct wpa_sm *sm, const u8 *pmk, size_t pmk_len,
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08002737 const u8 *pmkid, const u8 *bssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002738{
2739 if (sm == NULL)
2740 return;
2741
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002742 wpa_hexdump_key(MSG_DEBUG, "WPA: Set PMK based on external data",
2743 pmk, pmk_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002744 sm->pmk_len = pmk_len;
2745 os_memcpy(sm->pmk, pmk, pmk_len);
2746
2747#ifdef CONFIG_IEEE80211R
2748 /* Set XXKey to be PSK for FT key derivation */
2749 sm->xxkey_len = pmk_len;
2750 os_memcpy(sm->xxkey, pmk, pmk_len);
2751#endif /* CONFIG_IEEE80211R */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002752
2753 if (bssid) {
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08002754 pmksa_cache_add(sm->pmksa, pmk, pmk_len, pmkid, NULL, 0,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002755 bssid, sm->own_addr,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002756 sm->network_ctx, sm->key_mgmt, NULL);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002757 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002758}
2759
2760
2761/**
2762 * wpa_sm_set_pmk_from_pmksa - Set PMK based on the current PMKSA
2763 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2764 *
2765 * Take the PMK from the current PMKSA into use. If no PMKSA is active, the PMK
2766 * will be cleared.
2767 */
2768void wpa_sm_set_pmk_from_pmksa(struct wpa_sm *sm)
2769{
2770 if (sm == NULL)
2771 return;
2772
2773 if (sm->cur_pmksa) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002774 wpa_hexdump_key(MSG_DEBUG,
2775 "WPA: Set PMK based on current PMKSA",
2776 sm->cur_pmksa->pmk, sm->cur_pmksa->pmk_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002777 sm->pmk_len = sm->cur_pmksa->pmk_len;
2778 os_memcpy(sm->pmk, sm->cur_pmksa->pmk, sm->pmk_len);
2779 } else {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002780 wpa_printf(MSG_DEBUG, "WPA: No current PMKSA - clear PMK");
2781 sm->pmk_len = 0;
2782 os_memset(sm->pmk, 0, PMK_LEN_MAX);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002783 }
2784}
2785
2786
2787/**
2788 * wpa_sm_set_fast_reauth - Set fast reauthentication (EAP) enabled/disabled
2789 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2790 * @fast_reauth: Whether fast reauthentication (EAP) is allowed
2791 */
2792void wpa_sm_set_fast_reauth(struct wpa_sm *sm, int fast_reauth)
2793{
2794 if (sm)
2795 sm->fast_reauth = fast_reauth;
2796}
2797
2798
2799/**
2800 * wpa_sm_set_scard_ctx - Set context pointer for smartcard callbacks
2801 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2802 * @scard_ctx: Context pointer for smartcard related callback functions
2803 */
2804void wpa_sm_set_scard_ctx(struct wpa_sm *sm, void *scard_ctx)
2805{
2806 if (sm == NULL)
2807 return;
2808 sm->scard_ctx = scard_ctx;
2809 if (sm->preauth_eapol)
2810 eapol_sm_register_scard_ctx(sm->preauth_eapol, scard_ctx);
2811}
2812
2813
2814/**
2815 * wpa_sm_set_config - Notification of current configration change
2816 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2817 * @config: Pointer to current network configuration
2818 *
2819 * Notify WPA state machine that configuration has changed. config will be
2820 * stored as a backpointer to network configuration. This can be %NULL to clear
2821 * the stored pointed.
2822 */
2823void wpa_sm_set_config(struct wpa_sm *sm, struct rsn_supp_config *config)
2824{
2825 if (!sm)
2826 return;
2827
2828 if (config) {
2829 sm->network_ctx = config->network_ctx;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002830 sm->allowed_pairwise_cipher = config->allowed_pairwise_cipher;
2831 sm->proactive_key_caching = config->proactive_key_caching;
2832 sm->eap_workaround = config->eap_workaround;
2833 sm->eap_conf_ctx = config->eap_conf_ctx;
2834 if (config->ssid) {
2835 os_memcpy(sm->ssid, config->ssid, config->ssid_len);
2836 sm->ssid_len = config->ssid_len;
2837 } else
2838 sm->ssid_len = 0;
2839 sm->wpa_ptk_rekey = config->wpa_ptk_rekey;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002840 sm->p2p = config->p2p;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002841 sm->wpa_rsc_relaxation = config->wpa_rsc_relaxation;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002842#ifdef CONFIG_FILS
2843 if (config->fils_cache_id) {
2844 sm->fils_cache_id_set = 1;
2845 os_memcpy(sm->fils_cache_id, config->fils_cache_id,
2846 FILS_CACHE_ID_LEN);
2847 } else {
2848 sm->fils_cache_id_set = 0;
2849 }
2850#endif /* CONFIG_FILS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002851 } else {
2852 sm->network_ctx = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002853 sm->allowed_pairwise_cipher = 0;
2854 sm->proactive_key_caching = 0;
2855 sm->eap_workaround = 0;
2856 sm->eap_conf_ctx = NULL;
2857 sm->ssid_len = 0;
2858 sm->wpa_ptk_rekey = 0;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002859 sm->p2p = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002860 sm->wpa_rsc_relaxation = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002861 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002862}
2863
2864
2865/**
2866 * wpa_sm_set_own_addr - Set own MAC address
2867 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2868 * @addr: Own MAC address
2869 */
2870void wpa_sm_set_own_addr(struct wpa_sm *sm, const u8 *addr)
2871{
2872 if (sm)
2873 os_memcpy(sm->own_addr, addr, ETH_ALEN);
2874}
2875
2876
2877/**
2878 * wpa_sm_set_ifname - Set network interface name
2879 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2880 * @ifname: Interface name
2881 * @bridge_ifname: Optional bridge interface name (for pre-auth)
2882 */
2883void wpa_sm_set_ifname(struct wpa_sm *sm, const char *ifname,
2884 const char *bridge_ifname)
2885{
2886 if (sm) {
2887 sm->ifname = ifname;
2888 sm->bridge_ifname = bridge_ifname;
2889 }
2890}
2891
2892
2893/**
2894 * wpa_sm_set_eapol - Set EAPOL state machine pointer
2895 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2896 * @eapol: Pointer to EAPOL state machine allocated with eapol_sm_init()
2897 */
2898void wpa_sm_set_eapol(struct wpa_sm *sm, struct eapol_sm *eapol)
2899{
2900 if (sm)
2901 sm->eapol = eapol;
2902}
2903
2904
2905/**
2906 * wpa_sm_set_param - Set WPA state machine parameters
2907 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2908 * @param: Parameter field
2909 * @value: Parameter value
2910 * Returns: 0 on success, -1 on failure
2911 */
2912int wpa_sm_set_param(struct wpa_sm *sm, enum wpa_sm_conf_params param,
2913 unsigned int value)
2914{
2915 int ret = 0;
2916
2917 if (sm == NULL)
2918 return -1;
2919
2920 switch (param) {
2921 case RSNA_PMK_LIFETIME:
2922 if (value > 0)
2923 sm->dot11RSNAConfigPMKLifetime = value;
2924 else
2925 ret = -1;
2926 break;
2927 case RSNA_PMK_REAUTH_THRESHOLD:
2928 if (value > 0 && value <= 100)
2929 sm->dot11RSNAConfigPMKReauthThreshold = value;
2930 else
2931 ret = -1;
2932 break;
2933 case RSNA_SA_TIMEOUT:
2934 if (value > 0)
2935 sm->dot11RSNAConfigSATimeout = value;
2936 else
2937 ret = -1;
2938 break;
2939 case WPA_PARAM_PROTO:
2940 sm->proto = value;
2941 break;
2942 case WPA_PARAM_PAIRWISE:
2943 sm->pairwise_cipher = value;
2944 break;
2945 case WPA_PARAM_GROUP:
2946 sm->group_cipher = value;
2947 break;
2948 case WPA_PARAM_KEY_MGMT:
2949 sm->key_mgmt = value;
2950 break;
2951#ifdef CONFIG_IEEE80211W
2952 case WPA_PARAM_MGMT_GROUP:
2953 sm->mgmt_group_cipher = value;
2954 break;
2955#endif /* CONFIG_IEEE80211W */
2956 case WPA_PARAM_RSN_ENABLED:
2957 sm->rsn_enabled = value;
2958 break;
2959 case WPA_PARAM_MFP:
2960 sm->mfp = value;
2961 break;
Hai Shalom39bc25d2019-02-06 16:32:13 -08002962 case WPA_PARAM_OCV:
2963 sm->ocv = value;
2964 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002965 default:
2966 break;
2967 }
2968
2969 return ret;
2970}
2971
2972
2973/**
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002974 * wpa_sm_get_status - Get WPA state machine
2975 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2976 * @buf: Buffer for status information
2977 * @buflen: Maximum buffer length
2978 * @verbose: Whether to include verbose status information
2979 * Returns: Number of bytes written to buf.
2980 *
2981 * Query WPA state machine for status information. This function fills in
2982 * a text area with current status information. If the buffer (buf) is not
2983 * large enough, status information will be truncated to fit the buffer.
2984 */
2985int wpa_sm_get_status(struct wpa_sm *sm, char *buf, size_t buflen,
2986 int verbose)
2987{
2988 char *pos = buf, *end = buf + buflen;
2989 int ret;
2990
2991 ret = os_snprintf(pos, end - pos,
2992 "pairwise_cipher=%s\n"
2993 "group_cipher=%s\n"
2994 "key_mgmt=%s\n",
2995 wpa_cipher_txt(sm->pairwise_cipher),
2996 wpa_cipher_txt(sm->group_cipher),
2997 wpa_key_mgmt_txt(sm->key_mgmt, sm->proto));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002998 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002999 return pos - buf;
3000 pos += ret;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003001
3002 if (sm->mfp != NO_MGMT_FRAME_PROTECTION && sm->ap_rsn_ie) {
3003 struct wpa_ie_data rsn;
3004 if (wpa_parse_wpa_ie_rsn(sm->ap_rsn_ie, sm->ap_rsn_ie_len, &rsn)
3005 >= 0 &&
3006 rsn.capabilities & (WPA_CAPABILITY_MFPR |
3007 WPA_CAPABILITY_MFPC)) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003008 ret = os_snprintf(pos, end - pos, "pmf=%d\n"
3009 "mgmt_group_cipher=%s\n",
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003010 (rsn.capabilities &
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003011 WPA_CAPABILITY_MFPR) ? 2 : 1,
3012 wpa_cipher_txt(
3013 sm->mgmt_group_cipher));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003014 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003015 return pos - buf;
3016 pos += ret;
3017 }
3018 }
3019
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003020 return pos - buf;
3021}
3022
3023
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07003024int wpa_sm_pmf_enabled(struct wpa_sm *sm)
3025{
3026 struct wpa_ie_data rsn;
3027
3028 if (sm->mfp == NO_MGMT_FRAME_PROTECTION || !sm->ap_rsn_ie)
3029 return 0;
3030
3031 if (wpa_parse_wpa_ie_rsn(sm->ap_rsn_ie, sm->ap_rsn_ie_len, &rsn) >= 0 &&
3032 rsn.capabilities & (WPA_CAPABILITY_MFPR | WPA_CAPABILITY_MFPC))
3033 return 1;
3034
3035 return 0;
3036}
3037
3038
Hai Shalom39bc25d2019-02-06 16:32:13 -08003039int wpa_sm_ocv_enabled(struct wpa_sm *sm)
3040{
3041 struct wpa_ie_data rsn;
3042
3043 if (!sm->ocv || !sm->ap_rsn_ie)
3044 return 0;
3045
3046 return wpa_parse_wpa_ie_rsn(sm->ap_rsn_ie, sm->ap_rsn_ie_len,
3047 &rsn) >= 0 &&
3048 (rsn.capabilities & WPA_CAPABILITY_OCVC);
3049}
3050
3051
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003052/**
3053 * wpa_sm_set_assoc_wpa_ie_default - Generate own WPA/RSN IE from configuration
3054 * @sm: Pointer to WPA state machine data from wpa_sm_init()
3055 * @wpa_ie: Pointer to buffer for WPA/RSN IE
3056 * @wpa_ie_len: Pointer to the length of the wpa_ie buffer
3057 * Returns: 0 on success, -1 on failure
3058 */
3059int wpa_sm_set_assoc_wpa_ie_default(struct wpa_sm *sm, u8 *wpa_ie,
3060 size_t *wpa_ie_len)
3061{
3062 int res;
3063
3064 if (sm == NULL)
3065 return -1;
3066
Dmitry Shmidt55840ad2015-12-14 12:45:46 -08003067#ifdef CONFIG_TESTING_OPTIONS
3068 if (sm->test_assoc_ie) {
3069 wpa_printf(MSG_DEBUG,
3070 "TESTING: Replace association WPA/RSN IE");
3071 if (*wpa_ie_len < wpabuf_len(sm->test_assoc_ie))
3072 return -1;
3073 os_memcpy(wpa_ie, wpabuf_head(sm->test_assoc_ie),
3074 wpabuf_len(sm->test_assoc_ie));
3075 res = wpabuf_len(sm->test_assoc_ie);
3076 } else
3077#endif /* CONFIG_TESTING_OPTIONS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003078 res = wpa_gen_wpa_ie(sm, wpa_ie, *wpa_ie_len);
3079 if (res < 0)
3080 return -1;
3081 *wpa_ie_len = res;
3082
3083 wpa_hexdump(MSG_DEBUG, "WPA: Set own WPA IE default",
3084 wpa_ie, *wpa_ie_len);
3085
3086 if (sm->assoc_wpa_ie == NULL) {
3087 /*
3088 * Make a copy of the WPA/RSN IE so that 4-Way Handshake gets
3089 * the correct version of the IE even if PMKSA caching is
3090 * aborted (which would remove PMKID from IE generation).
3091 */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003092 sm->assoc_wpa_ie = os_memdup(wpa_ie, *wpa_ie_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003093 if (sm->assoc_wpa_ie == NULL)
3094 return -1;
3095
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003096 sm->assoc_wpa_ie_len = *wpa_ie_len;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003097 } else {
3098 wpa_hexdump(MSG_DEBUG,
3099 "WPA: Leave previously set WPA IE default",
3100 sm->assoc_wpa_ie, sm->assoc_wpa_ie_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003101 }
3102
3103 return 0;
3104}
3105
3106
3107/**
3108 * wpa_sm_set_assoc_wpa_ie - Set own WPA/RSN IE from (Re)AssocReq
3109 * @sm: Pointer to WPA state machine data from wpa_sm_init()
3110 * @ie: Pointer to IE data (starting from id)
3111 * @len: IE length
3112 * Returns: 0 on success, -1 on failure
3113 *
3114 * Inform WPA state machine about the WPA/RSN IE used in (Re)Association
3115 * Request frame. The IE will be used to override the default value generated
3116 * with wpa_sm_set_assoc_wpa_ie_default().
3117 */
3118int wpa_sm_set_assoc_wpa_ie(struct wpa_sm *sm, const u8 *ie, size_t len)
3119{
3120 if (sm == NULL)
3121 return -1;
3122
3123 os_free(sm->assoc_wpa_ie);
3124 if (ie == NULL || len == 0) {
3125 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
3126 "WPA: clearing own WPA/RSN IE");
3127 sm->assoc_wpa_ie = NULL;
3128 sm->assoc_wpa_ie_len = 0;
3129 } else {
3130 wpa_hexdump(MSG_DEBUG, "WPA: set own WPA/RSN IE", ie, len);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003131 sm->assoc_wpa_ie = os_memdup(ie, len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003132 if (sm->assoc_wpa_ie == NULL)
3133 return -1;
3134
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003135 sm->assoc_wpa_ie_len = len;
3136 }
3137
3138 return 0;
3139}
3140
3141
3142/**
3143 * wpa_sm_set_ap_wpa_ie - Set AP WPA IE from Beacon/ProbeResp
3144 * @sm: Pointer to WPA state machine data from wpa_sm_init()
3145 * @ie: Pointer to IE data (starting from id)
3146 * @len: IE length
3147 * Returns: 0 on success, -1 on failure
3148 *
3149 * Inform WPA state machine about the WPA IE used in Beacon / Probe Response
3150 * frame.
3151 */
3152int wpa_sm_set_ap_wpa_ie(struct wpa_sm *sm, const u8 *ie, size_t len)
3153{
3154 if (sm == NULL)
3155 return -1;
3156
3157 os_free(sm->ap_wpa_ie);
3158 if (ie == NULL || len == 0) {
3159 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
3160 "WPA: clearing AP WPA IE");
3161 sm->ap_wpa_ie = NULL;
3162 sm->ap_wpa_ie_len = 0;
3163 } else {
3164 wpa_hexdump(MSG_DEBUG, "WPA: set AP WPA IE", ie, len);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003165 sm->ap_wpa_ie = os_memdup(ie, len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003166 if (sm->ap_wpa_ie == NULL)
3167 return -1;
3168
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003169 sm->ap_wpa_ie_len = len;
3170 }
3171
3172 return 0;
3173}
3174
3175
3176/**
3177 * wpa_sm_set_ap_rsn_ie - Set AP RSN IE from Beacon/ProbeResp
3178 * @sm: Pointer to WPA state machine data from wpa_sm_init()
3179 * @ie: Pointer to IE data (starting from id)
3180 * @len: IE length
3181 * Returns: 0 on success, -1 on failure
3182 *
3183 * Inform WPA state machine about the RSN IE used in Beacon / Probe Response
3184 * frame.
3185 */
3186int wpa_sm_set_ap_rsn_ie(struct wpa_sm *sm, const u8 *ie, size_t len)
3187{
3188 if (sm == NULL)
3189 return -1;
3190
3191 os_free(sm->ap_rsn_ie);
3192 if (ie == NULL || len == 0) {
3193 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
3194 "WPA: clearing AP RSN IE");
3195 sm->ap_rsn_ie = NULL;
3196 sm->ap_rsn_ie_len = 0;
3197 } else {
3198 wpa_hexdump(MSG_DEBUG, "WPA: set AP RSN IE", ie, len);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003199 sm->ap_rsn_ie = os_memdup(ie, len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003200 if (sm->ap_rsn_ie == NULL)
3201 return -1;
3202
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003203 sm->ap_rsn_ie_len = len;
3204 }
3205
3206 return 0;
3207}
3208
3209
3210/**
3211 * wpa_sm_parse_own_wpa_ie - Parse own WPA/RSN IE
3212 * @sm: Pointer to WPA state machine data from wpa_sm_init()
3213 * @data: Pointer to data area for parsing results
3214 * Returns: 0 on success, -1 if IE is not known, or -2 on parsing failure
3215 *
3216 * Parse the contents of the own WPA or RSN IE from (Re)AssocReq and write the
3217 * parsed data into data.
3218 */
3219int wpa_sm_parse_own_wpa_ie(struct wpa_sm *sm, struct wpa_ie_data *data)
3220{
3221 if (sm == NULL)
3222 return -1;
3223
3224 if (sm->assoc_wpa_ie == NULL) {
3225 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
3226 "WPA: No WPA/RSN IE available from association info");
3227 return -1;
3228 }
3229 if (wpa_parse_wpa_ie(sm->assoc_wpa_ie, sm->assoc_wpa_ie_len, data))
3230 return -2;
3231 return 0;
3232}
3233
3234
3235int wpa_sm_pmksa_cache_list(struct wpa_sm *sm, char *buf, size_t len)
3236{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003237 return pmksa_cache_list(sm->pmksa, buf, len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003238}
3239
3240
Dmitry Shmidt29333592017-01-09 12:27:11 -08003241struct rsn_pmksa_cache_entry * wpa_sm_pmksa_cache_head(struct wpa_sm *sm)
3242{
3243 return pmksa_cache_head(sm->pmksa);
3244}
3245
3246
3247struct rsn_pmksa_cache_entry *
3248wpa_sm_pmksa_cache_add_entry(struct wpa_sm *sm,
3249 struct rsn_pmksa_cache_entry * entry)
3250{
3251 return pmksa_cache_add_entry(sm->pmksa, entry);
3252}
3253
3254
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003255void wpa_sm_pmksa_cache_add(struct wpa_sm *sm, const u8 *pmk, size_t pmk_len,
3256 const u8 *pmkid, const u8 *bssid,
3257 const u8 *fils_cache_id)
3258{
3259 sm->cur_pmksa = pmksa_cache_add(sm->pmksa, pmk, pmk_len, pmkid, NULL, 0,
3260 bssid, sm->own_addr, sm->network_ctx,
3261 sm->key_mgmt, fils_cache_id);
3262}
3263
3264
3265int wpa_sm_pmksa_exists(struct wpa_sm *sm, const u8 *bssid,
3266 const void *network_ctx)
3267{
Roshan Pius3a1667e2018-07-03 15:17:14 -07003268 return pmksa_cache_get(sm->pmksa, bssid, NULL, network_ctx, 0) != NULL;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003269}
3270
3271
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003272void wpa_sm_drop_sa(struct wpa_sm *sm)
3273{
3274 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Clear old PMK and PTK");
3275 sm->ptk_set = 0;
3276 sm->tptk_set = 0;
Roshan Pius3a1667e2018-07-03 15:17:14 -07003277 sm->pmk_len = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003278 os_memset(sm->pmk, 0, sizeof(sm->pmk));
3279 os_memset(&sm->ptk, 0, sizeof(sm->ptk));
3280 os_memset(&sm->tptk, 0, sizeof(sm->tptk));
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02003281 os_memset(&sm->gtk, 0, sizeof(sm->gtk));
Jouni Malinen58c0e962017-10-01 12:12:24 +03003282 os_memset(&sm->gtk_wnm_sleep, 0, sizeof(sm->gtk_wnm_sleep));
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02003283#ifdef CONFIG_IEEE80211W
3284 os_memset(&sm->igtk, 0, sizeof(sm->igtk));
Jouni Malinen58c0e962017-10-01 12:12:24 +03003285 os_memset(&sm->igtk_wnm_sleep, 0, sizeof(sm->igtk_wnm_sleep));
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02003286#endif /* CONFIG_IEEE80211W */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003287#ifdef CONFIG_IEEE80211R
3288 os_memset(sm->xxkey, 0, sizeof(sm->xxkey));
Roshan Pius3a1667e2018-07-03 15:17:14 -07003289 sm->xxkey_len = 0;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003290 os_memset(sm->pmk_r0, 0, sizeof(sm->pmk_r0));
Roshan Pius3a1667e2018-07-03 15:17:14 -07003291 sm->pmk_r0_len = 0;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003292 os_memset(sm->pmk_r1, 0, sizeof(sm->pmk_r1));
Roshan Pius3a1667e2018-07-03 15:17:14 -07003293 sm->pmk_r1_len = 0;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003294#endif /* CONFIG_IEEE80211R */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003295}
3296
3297
3298int wpa_sm_has_ptk(struct wpa_sm *sm)
3299{
3300 if (sm == NULL)
3301 return 0;
3302 return sm->ptk_set;
3303}
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003304
3305
3306void wpa_sm_update_replay_ctr(struct wpa_sm *sm, const u8 *replay_ctr)
3307{
3308 os_memcpy(sm->rx_replay_counter, replay_ctr, WPA_REPLAY_COUNTER_LEN);
3309}
3310
3311
3312void wpa_sm_pmksa_cache_flush(struct wpa_sm *sm, void *network_ctx)
3313{
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07003314 pmksa_cache_flush(sm->pmksa, network_ctx, NULL, 0);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003315}
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003316
3317
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003318#ifdef CONFIG_WNM
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003319int wpa_wnmsleep_install_key(struct wpa_sm *sm, u8 subelem_id, u8 *buf)
3320{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003321 u16 keyinfo;
3322 u8 keylen; /* plaintext key len */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003323 u8 *key_rsc;
3324
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003325 if (subelem_id == WNM_SLEEP_SUBELEM_GTK) {
Dmitry Shmidt61593f02014-04-21 16:27:35 -07003326 struct wpa_gtk_data gd;
3327
3328 os_memset(&gd, 0, sizeof(gd));
3329 keylen = wpa_cipher_key_len(sm->group_cipher);
3330 gd.key_rsc_len = wpa_cipher_rsc_len(sm->group_cipher);
3331 gd.alg = wpa_cipher_to_alg(sm->group_cipher);
3332 if (gd.alg == WPA_ALG_NONE) {
3333 wpa_printf(MSG_DEBUG, "Unsupported group cipher suite");
3334 return -1;
3335 }
3336
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003337 key_rsc = buf + 5;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003338 keyinfo = WPA_GET_LE16(buf + 2);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003339 gd.gtk_len = keylen;
3340 if (gd.gtk_len != buf[4]) {
3341 wpa_printf(MSG_DEBUG, "GTK len mismatch len %d vs %d",
3342 gd.gtk_len, buf[4]);
3343 return -1;
3344 }
3345 gd.keyidx = keyinfo & 0x03; /* B0 - B1 */
3346 gd.tx = wpa_supplicant_gtk_tx_bit_workaround(
3347 sm, !!(keyinfo & WPA_KEY_INFO_TXRX));
3348
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003349 os_memcpy(gd.gtk, buf + 13, gd.gtk_len);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003350
3351 wpa_hexdump_key(MSG_DEBUG, "Install GTK (WNM SLEEP)",
3352 gd.gtk, gd.gtk_len);
Jouni Malinen58c0e962017-10-01 12:12:24 +03003353 if (wpa_supplicant_install_gtk(sm, &gd, key_rsc, 1)) {
Dmitry Shmidt61593f02014-04-21 16:27:35 -07003354 os_memset(&gd, 0, sizeof(gd));
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003355 wpa_printf(MSG_DEBUG, "Failed to install the GTK in "
3356 "WNM mode");
3357 return -1;
3358 }
Dmitry Shmidt61593f02014-04-21 16:27:35 -07003359 os_memset(&gd, 0, sizeof(gd));
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003360#ifdef CONFIG_IEEE80211W
3361 } else if (subelem_id == WNM_SLEEP_SUBELEM_IGTK) {
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02003362 const struct wpa_igtk_kde *igtk;
Dmitry Shmidt61593f02014-04-21 16:27:35 -07003363
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02003364 igtk = (const struct wpa_igtk_kde *) (buf + 2);
Jouni Malinen58c0e962017-10-01 12:12:24 +03003365 if (wpa_supplicant_install_igtk(sm, igtk, 1) < 0)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003366 return -1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003367#endif /* CONFIG_IEEE80211W */
3368 } else {
3369 wpa_printf(MSG_DEBUG, "Unknown element id");
3370 return -1;
3371 }
3372
3373 return 0;
3374}
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003375#endif /* CONFIG_WNM */
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003376
3377
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003378#ifdef CONFIG_P2P
3379
3380int wpa_sm_get_p2p_ip_addr(struct wpa_sm *sm, u8 *buf)
3381{
3382 if (sm == NULL || WPA_GET_BE32(sm->p2p_ip_addr) == 0)
3383 return -1;
3384 os_memcpy(buf, sm->p2p_ip_addr, 3 * 4);
3385 return 0;
3386}
3387
3388#endif /* CONFIG_P2P */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003389
3390
3391void wpa_sm_set_rx_replay_ctr(struct wpa_sm *sm, const u8 *rx_replay_counter)
3392{
3393 if (rx_replay_counter == NULL)
3394 return;
3395
3396 os_memcpy(sm->rx_replay_counter, rx_replay_counter,
3397 WPA_REPLAY_COUNTER_LEN);
3398 sm->rx_replay_counter_set = 1;
3399 wpa_printf(MSG_DEBUG, "Updated key replay counter");
3400}
3401
3402
Dmitry Shmidt807291d2015-01-27 13:40:23 -08003403void wpa_sm_set_ptk_kck_kek(struct wpa_sm *sm,
3404 const u8 *ptk_kck, size_t ptk_kck_len,
3405 const u8 *ptk_kek, size_t ptk_kek_len)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003406{
Dmitry Shmidt807291d2015-01-27 13:40:23 -08003407 if (ptk_kck && ptk_kck_len <= WPA_KCK_MAX_LEN) {
3408 os_memcpy(sm->ptk.kck, ptk_kck, ptk_kck_len);
3409 sm->ptk.kck_len = ptk_kck_len;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003410 wpa_printf(MSG_DEBUG, "Updated PTK KCK");
3411 }
Dmitry Shmidt807291d2015-01-27 13:40:23 -08003412 if (ptk_kek && ptk_kek_len <= WPA_KEK_MAX_LEN) {
3413 os_memcpy(sm->ptk.kek, ptk_kek, ptk_kek_len);
3414 sm->ptk.kek_len = ptk_kek_len;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003415 wpa_printf(MSG_DEBUG, "Updated PTK KEK");
3416 }
3417 sm->ptk_set = 1;
3418}
Dmitry Shmidt55840ad2015-12-14 12:45:46 -08003419
3420
3421#ifdef CONFIG_TESTING_OPTIONS
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003422
Dmitry Shmidt55840ad2015-12-14 12:45:46 -08003423void wpa_sm_set_test_assoc_ie(struct wpa_sm *sm, struct wpabuf *buf)
3424{
3425 wpabuf_free(sm->test_assoc_ie);
3426 sm->test_assoc_ie = buf;
3427}
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003428
3429
3430const u8 * wpa_sm_get_anonce(struct wpa_sm *sm)
3431{
3432 return sm->anonce;
3433}
3434
Dmitry Shmidt55840ad2015-12-14 12:45:46 -08003435#endif /* CONFIG_TESTING_OPTIONS */
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003436
3437
Roshan Pius3a1667e2018-07-03 15:17:14 -07003438unsigned int wpa_sm_get_key_mgmt(struct wpa_sm *sm)
3439{
3440 return sm->key_mgmt;
3441}
3442
3443
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003444#ifdef CONFIG_FILS
3445
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003446struct wpabuf * fils_build_auth(struct wpa_sm *sm, int dh_group, const u8 *md)
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003447{
3448 struct wpabuf *buf = NULL;
3449 struct wpabuf *erp_msg;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003450 struct wpabuf *pub = NULL;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003451
3452 erp_msg = eapol_sm_build_erp_reauth_start(sm->eapol);
3453 if (!erp_msg && !sm->cur_pmksa) {
3454 wpa_printf(MSG_DEBUG,
3455 "FILS: Neither ERP EAP-Initiate/Re-auth nor PMKSA cache entry is available - skip FILS");
3456 goto fail;
3457 }
3458
3459 wpa_printf(MSG_DEBUG, "FILS: Try to use FILS (erp=%d pmksa_cache=%d)",
3460 erp_msg != NULL, sm->cur_pmksa != NULL);
3461
3462 sm->fils_completed = 0;
3463
3464 if (!sm->assoc_wpa_ie) {
3465 wpa_printf(MSG_INFO, "FILS: No own RSN IE set for FILS");
3466 goto fail;
3467 }
3468
3469 if (random_get_bytes(sm->fils_nonce, FILS_NONCE_LEN) < 0 ||
3470 random_get_bytes(sm->fils_session, FILS_SESSION_LEN) < 0)
3471 goto fail;
3472
3473 wpa_hexdump(MSG_DEBUG, "FILS: Generated FILS Nonce",
3474 sm->fils_nonce, FILS_NONCE_LEN);
3475 wpa_hexdump(MSG_DEBUG, "FILS: Generated FILS Session",
3476 sm->fils_session, FILS_SESSION_LEN);
3477
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003478#ifdef CONFIG_FILS_SK_PFS
3479 sm->fils_dh_group = dh_group;
3480 if (dh_group) {
3481 crypto_ecdh_deinit(sm->fils_ecdh);
3482 sm->fils_ecdh = crypto_ecdh_init(dh_group);
3483 if (!sm->fils_ecdh) {
3484 wpa_printf(MSG_INFO,
3485 "FILS: Could not initialize ECDH with group %d",
3486 dh_group);
3487 goto fail;
3488 }
3489 pub = crypto_ecdh_get_pubkey(sm->fils_ecdh, 1);
3490 if (!pub)
3491 goto fail;
3492 wpa_hexdump_buf(MSG_DEBUG, "FILS: Element (DH public key)",
3493 pub);
3494 sm->fils_dh_elem_len = wpabuf_len(pub);
3495 }
3496#endif /* CONFIG_FILS_SK_PFS */
3497
3498 buf = wpabuf_alloc(1000 + sm->assoc_wpa_ie_len +
3499 (pub ? wpabuf_len(pub) : 0));
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003500 if (!buf)
3501 goto fail;
3502
3503 /* Fields following the Authentication algorithm number field */
3504
3505 /* Authentication Transaction seq# */
3506 wpabuf_put_le16(buf, 1);
3507
3508 /* Status Code */
3509 wpabuf_put_le16(buf, WLAN_STATUS_SUCCESS);
3510
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003511 /* TODO: FILS PK */
3512#ifdef CONFIG_FILS_SK_PFS
3513 if (dh_group) {
3514 /* Finite Cyclic Group */
3515 wpabuf_put_le16(buf, dh_group);
3516 /* Element */
3517 wpabuf_put_buf(buf, pub);
3518 }
3519#endif /* CONFIG_FILS_SK_PFS */
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003520
3521 /* RSNE */
3522 wpa_hexdump(MSG_DEBUG, "FILS: RSNE in FILS Authentication frame",
3523 sm->assoc_wpa_ie, sm->assoc_wpa_ie_len);
3524 wpabuf_put_data(buf, sm->assoc_wpa_ie, sm->assoc_wpa_ie_len);
3525
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003526 if (md) {
3527 /* MDE when using FILS for FT initial association */
3528 struct rsn_mdie *mdie;
3529
3530 wpabuf_put_u8(buf, WLAN_EID_MOBILITY_DOMAIN);
3531 wpabuf_put_u8(buf, sizeof(*mdie));
3532 mdie = wpabuf_put(buf, sizeof(*mdie));
3533 os_memcpy(mdie->mobility_domain, md, MOBILITY_DOMAIN_ID_LEN);
3534 mdie->ft_capab = 0;
3535 }
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003536
3537 /* FILS Nonce */
3538 wpabuf_put_u8(buf, WLAN_EID_EXTENSION); /* Element ID */
3539 wpabuf_put_u8(buf, 1 + FILS_NONCE_LEN); /* Length */
3540 /* Element ID Extension */
3541 wpabuf_put_u8(buf, WLAN_EID_EXT_FILS_NONCE);
3542 wpabuf_put_data(buf, sm->fils_nonce, FILS_NONCE_LEN);
3543
3544 /* FILS Session */
3545 wpabuf_put_u8(buf, WLAN_EID_EXTENSION); /* Element ID */
3546 wpabuf_put_u8(buf, 1 + FILS_SESSION_LEN); /* Length */
3547 /* Element ID Extension */
3548 wpabuf_put_u8(buf, WLAN_EID_EXT_FILS_SESSION);
3549 wpabuf_put_data(buf, sm->fils_session, FILS_SESSION_LEN);
3550
3551 /* FILS Wrapped Data */
Paul Stewart092955c2017-02-06 09:13:09 -08003552 sm->fils_erp_pmkid_set = 0;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003553 if (erp_msg) {
3554 wpabuf_put_u8(buf, WLAN_EID_EXTENSION); /* Element ID */
3555 wpabuf_put_u8(buf, 1 + wpabuf_len(erp_msg)); /* Length */
3556 /* Element ID Extension */
3557 wpabuf_put_u8(buf, WLAN_EID_EXT_FILS_WRAPPED_DATA);
3558 wpabuf_put_buf(buf, erp_msg);
Paul Stewart092955c2017-02-06 09:13:09 -08003559 /* Calculate pending PMKID here so that we do not need to
3560 * maintain a copy of the EAP-Initiate/Reauth message. */
3561 if (fils_pmkid_erp(sm->key_mgmt, wpabuf_head(erp_msg),
3562 wpabuf_len(erp_msg),
3563 sm->fils_erp_pmkid) == 0)
3564 sm->fils_erp_pmkid_set = 1;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003565 }
3566
3567 wpa_hexdump_buf(MSG_DEBUG, "RSN: FILS fields for Authentication frame",
3568 buf);
3569
3570fail:
3571 wpabuf_free(erp_msg);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003572 wpabuf_free(pub);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003573 return buf;
3574}
3575
3576
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003577int fils_process_auth(struct wpa_sm *sm, const u8 *bssid, const u8 *data,
3578 size_t len)
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003579{
3580 const u8 *pos, *end;
3581 struct ieee802_11_elems elems;
3582 struct wpa_ie_data rsn;
3583 int pmkid_match = 0;
3584 u8 ick[FILS_ICK_MAX_LEN];
3585 size_t ick_len;
3586 int res;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003587 struct wpabuf *dh_ss = NULL;
3588 const u8 *g_sta = NULL;
3589 size_t g_sta_len = 0;
3590 const u8 *g_ap = NULL;
3591 size_t g_ap_len = 0;
3592 struct wpabuf *pub = NULL;
3593
3594 os_memcpy(sm->bssid, bssid, ETH_ALEN);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003595
3596 wpa_hexdump(MSG_DEBUG, "FILS: Authentication frame fields",
3597 data, len);
3598 pos = data;
3599 end = data + len;
3600
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003601 /* TODO: FILS PK */
3602#ifdef CONFIG_FILS_SK_PFS
3603 if (sm->fils_dh_group) {
3604 u16 group;
3605
3606 /* Using FILS PFS */
3607
3608 /* Finite Cyclic Group */
3609 if (end - pos < 2) {
3610 wpa_printf(MSG_DEBUG,
3611 "FILS: No room for Finite Cyclic Group");
3612 goto fail;
3613 }
3614 group = WPA_GET_LE16(pos);
3615 pos += 2;
3616 if (group != sm->fils_dh_group) {
3617 wpa_printf(MSG_DEBUG,
3618 "FILS: Unexpected change in Finite Cyclic Group: %u (expected %u)",
3619 group, sm->fils_dh_group);
3620 goto fail;
3621 }
3622
3623 /* Element */
3624 if ((size_t) (end - pos) < sm->fils_dh_elem_len) {
3625 wpa_printf(MSG_DEBUG, "FILS: No room for Element");
3626 goto fail;
3627 }
3628
3629 if (!sm->fils_ecdh) {
3630 wpa_printf(MSG_DEBUG, "FILS: No ECDH state available");
3631 goto fail;
3632 }
3633 dh_ss = crypto_ecdh_set_peerkey(sm->fils_ecdh, 1, pos,
3634 sm->fils_dh_elem_len);
3635 if (!dh_ss) {
3636 wpa_printf(MSG_DEBUG, "FILS: ECDH operation failed");
3637 goto fail;
3638 }
3639 wpa_hexdump_buf_key(MSG_DEBUG, "FILS: DH_SS", dh_ss);
3640 g_ap = pos;
3641 g_ap_len = sm->fils_dh_elem_len;
3642 pos += sm->fils_dh_elem_len;
3643 }
3644#endif /* CONFIG_FILS_SK_PFS */
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003645
3646 wpa_hexdump(MSG_DEBUG, "FILS: Remaining IEs", pos, end - pos);
3647 if (ieee802_11_parse_elems(pos, end - pos, &elems, 1) == ParseFailed) {
3648 wpa_printf(MSG_DEBUG, "FILS: Could not parse elements");
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003649 goto fail;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003650 }
3651
3652 /* RSNE */
3653 wpa_hexdump(MSG_DEBUG, "FILS: RSN element", elems.rsn_ie,
3654 elems.rsn_ie_len);
3655 if (!elems.rsn_ie ||
3656 wpa_parse_wpa_ie_rsn(elems.rsn_ie - 2, elems.rsn_ie_len + 2,
3657 &rsn) < 0) {
3658 wpa_printf(MSG_DEBUG, "FILS: No RSN element");
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003659 goto fail;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003660 }
3661
3662 if (!elems.fils_nonce) {
3663 wpa_printf(MSG_DEBUG, "FILS: No FILS Nonce field");
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003664 goto fail;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003665 }
3666 os_memcpy(sm->fils_anonce, elems.fils_nonce, FILS_NONCE_LEN);
3667 wpa_hexdump(MSG_DEBUG, "FILS: ANonce", sm->fils_anonce, FILS_NONCE_LEN);
3668
Roshan Pius3a1667e2018-07-03 15:17:14 -07003669#ifdef CONFIG_IEEE80211R
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003670 if (wpa_key_mgmt_ft(sm->key_mgmt)) {
3671 struct wpa_ft_ies parse;
3672
3673 if (!elems.mdie || !elems.ftie) {
3674 wpa_printf(MSG_DEBUG, "FILS+FT: No MDE or FTE");
3675 goto fail;
3676 }
3677
Roshan Pius3a1667e2018-07-03 15:17:14 -07003678 if (wpa_ft_parse_ies(pos, end - pos, &parse,
3679 wpa_key_mgmt_sha384(sm->key_mgmt)) < 0) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003680 wpa_printf(MSG_DEBUG, "FILS+FT: Failed to parse IEs");
3681 goto fail;
3682 }
3683
3684 if (!parse.r0kh_id) {
3685 wpa_printf(MSG_DEBUG,
3686 "FILS+FT: No R0KH-ID subelem in FTE");
3687 goto fail;
3688 }
3689 os_memcpy(sm->r0kh_id, parse.r0kh_id, parse.r0kh_id_len);
3690 sm->r0kh_id_len = parse.r0kh_id_len;
3691 wpa_hexdump_ascii(MSG_DEBUG, "FILS+FT: R0KH-ID",
3692 sm->r0kh_id, sm->r0kh_id_len);
3693
3694 if (!parse.r1kh_id) {
3695 wpa_printf(MSG_DEBUG,
3696 "FILS+FT: No R1KH-ID subelem in FTE");
3697 goto fail;
3698 }
3699 os_memcpy(sm->r1kh_id, parse.r1kh_id, FT_R1KH_ID_LEN);
3700 wpa_hexdump(MSG_DEBUG, "FILS+FT: R1KH-ID",
3701 sm->r1kh_id, FT_R1KH_ID_LEN);
3702
3703 /* TODO: Check MDE and FTE payload */
3704
3705 wpabuf_free(sm->fils_ft_ies);
3706 sm->fils_ft_ies = wpabuf_alloc(2 + elems.mdie_len +
3707 2 + elems.ftie_len);
3708 if (!sm->fils_ft_ies)
3709 goto fail;
3710 wpabuf_put_data(sm->fils_ft_ies, elems.mdie - 2,
3711 2 + elems.mdie_len);
3712 wpabuf_put_data(sm->fils_ft_ies, elems.ftie - 2,
3713 2 + elems.ftie_len);
3714 } else {
3715 wpabuf_free(sm->fils_ft_ies);
3716 sm->fils_ft_ies = NULL;
3717 }
Roshan Pius3a1667e2018-07-03 15:17:14 -07003718#endif /* CONFIG_IEEE80211R */
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003719
3720 /* PMKID List */
3721 if (rsn.pmkid && rsn.num_pmkid > 0) {
3722 wpa_hexdump(MSG_DEBUG, "FILS: PMKID List",
3723 rsn.pmkid, rsn.num_pmkid * PMKID_LEN);
3724
3725 if (rsn.num_pmkid != 1) {
3726 wpa_printf(MSG_DEBUG, "FILS: Invalid PMKID selection");
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003727 goto fail;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003728 }
3729 wpa_hexdump(MSG_DEBUG, "FILS: PMKID", rsn.pmkid, PMKID_LEN);
3730 if (os_memcmp(sm->cur_pmksa->pmkid, rsn.pmkid, PMKID_LEN) != 0)
3731 {
3732 wpa_printf(MSG_DEBUG, "FILS: PMKID mismatch");
3733 wpa_hexdump(MSG_DEBUG, "FILS: Expected PMKID",
3734 sm->cur_pmksa->pmkid, PMKID_LEN);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003735 goto fail;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003736 }
3737 wpa_printf(MSG_DEBUG,
3738 "FILS: Matching PMKID - continue using PMKSA caching");
3739 pmkid_match = 1;
3740 }
3741 if (!pmkid_match && sm->cur_pmksa) {
3742 wpa_printf(MSG_DEBUG,
3743 "FILS: No PMKID match - cannot use cached PMKSA entry");
3744 sm->cur_pmksa = NULL;
3745 }
3746
3747 /* FILS Session */
3748 if (!elems.fils_session) {
3749 wpa_printf(MSG_DEBUG, "FILS: No FILS Session element");
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003750 goto fail;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003751 }
3752 wpa_hexdump(MSG_DEBUG, "FILS: FILS Session", elems.fils_session,
3753 FILS_SESSION_LEN);
3754 if (os_memcmp(sm->fils_session, elems.fils_session, FILS_SESSION_LEN)
3755 != 0) {
3756 wpa_printf(MSG_DEBUG, "FILS: Session mismatch");
3757 wpa_hexdump(MSG_DEBUG, "FILS: Expected FILS Session",
3758 sm->fils_session, FILS_SESSION_LEN);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003759 goto fail;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003760 }
3761
3762 /* FILS Wrapped Data */
3763 if (!sm->cur_pmksa && elems.fils_wrapped_data) {
Paul Stewart092955c2017-02-06 09:13:09 -08003764 u8 rmsk[ERP_MAX_KEY_LEN];
3765 size_t rmsk_len;
3766
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003767 wpa_hexdump(MSG_DEBUG, "FILS: Wrapped Data",
3768 elems.fils_wrapped_data,
3769 elems.fils_wrapped_data_len);
3770 eapol_sm_process_erp_finish(sm->eapol, elems.fils_wrapped_data,
3771 elems.fils_wrapped_data_len);
3772 if (eapol_sm_failed(sm->eapol))
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003773 goto fail;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003774
Paul Stewart092955c2017-02-06 09:13:09 -08003775 rmsk_len = ERP_MAX_KEY_LEN;
3776 res = eapol_sm_get_key(sm->eapol, rmsk, rmsk_len);
3777 if (res == PMK_LEN) {
3778 rmsk_len = PMK_LEN;
3779 res = eapol_sm_get_key(sm->eapol, rmsk, rmsk_len);
3780 }
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003781 if (res)
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003782 goto fail;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003783
Paul Stewart092955c2017-02-06 09:13:09 -08003784 res = fils_rmsk_to_pmk(sm->key_mgmt, rmsk, rmsk_len,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003785 sm->fils_nonce, sm->fils_anonce,
3786 dh_ss ? wpabuf_head(dh_ss) : NULL,
3787 dh_ss ? wpabuf_len(dh_ss) : 0,
Paul Stewart092955c2017-02-06 09:13:09 -08003788 sm->pmk, &sm->pmk_len);
3789 os_memset(rmsk, 0, sizeof(rmsk));
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003790
3791 /* Don't use DHss in PTK derivation if PMKSA caching is not
3792 * used. */
3793 wpabuf_clear_free(dh_ss);
3794 dh_ss = NULL;
3795
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08003796 if (res)
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003797 goto fail;
Paul Stewart092955c2017-02-06 09:13:09 -08003798
3799 if (!sm->fils_erp_pmkid_set) {
3800 wpa_printf(MSG_DEBUG, "FILS: PMKID not available");
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003801 goto fail;
Paul Stewart092955c2017-02-06 09:13:09 -08003802 }
3803 wpa_hexdump(MSG_DEBUG, "FILS: PMKID", sm->fils_erp_pmkid,
3804 PMKID_LEN);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003805 wpa_printf(MSG_DEBUG, "FILS: ERP processing succeeded - add PMKSA cache entry for the result");
Paul Stewart092955c2017-02-06 09:13:09 -08003806 sm->cur_pmksa = pmksa_cache_add(sm->pmksa, sm->pmk, sm->pmk_len,
3807 sm->fils_erp_pmkid, NULL, 0,
3808 sm->bssid, sm->own_addr,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003809 sm->network_ctx, sm->key_mgmt,
3810 NULL);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003811 }
3812
3813 if (!sm->cur_pmksa) {
3814 wpa_printf(MSG_DEBUG,
3815 "FILS: No remaining options to continue FILS authentication");
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003816 goto fail;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003817 }
3818
3819 if (fils_pmk_to_ptk(sm->pmk, sm->pmk_len, sm->own_addr, sm->bssid,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003820 sm->fils_nonce, sm->fils_anonce,
3821 dh_ss ? wpabuf_head(dh_ss) : NULL,
3822 dh_ss ? wpabuf_len(dh_ss) : 0,
3823 &sm->ptk, ick, &ick_len,
3824 sm->key_mgmt, sm->pairwise_cipher,
3825 sm->fils_ft, &sm->fils_ft_len) < 0) {
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003826 wpa_printf(MSG_DEBUG, "FILS: Failed to derive PTK");
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003827 goto fail;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003828 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003829
3830 wpabuf_clear_free(dh_ss);
3831 dh_ss = NULL;
3832
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003833 sm->ptk_set = 1;
3834 sm->tptk_set = 0;
3835 os_memset(&sm->tptk, 0, sizeof(sm->tptk));
3836
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003837#ifdef CONFIG_FILS_SK_PFS
3838 if (sm->fils_dh_group) {
3839 if (!sm->fils_ecdh) {
3840 wpa_printf(MSG_INFO, "FILS: ECDH not initialized");
3841 goto fail;
3842 }
3843 pub = crypto_ecdh_get_pubkey(sm->fils_ecdh, 1);
3844 if (!pub)
3845 goto fail;
3846 wpa_hexdump_buf(MSG_DEBUG, "FILS: gSTA", pub);
3847 g_sta = wpabuf_head(pub);
3848 g_sta_len = wpabuf_len(pub);
3849 if (!g_ap) {
3850 wpa_printf(MSG_INFO, "FILS: gAP not available");
3851 goto fail;
3852 }
3853 wpa_hexdump(MSG_DEBUG, "FILS: gAP", g_ap, g_ap_len);
3854 }
3855#endif /* CONFIG_FILS_SK_PFS */
3856
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003857 res = fils_key_auth_sk(ick, ick_len, sm->fils_nonce,
3858 sm->fils_anonce, sm->own_addr, sm->bssid,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003859 g_sta, g_sta_len, g_ap, g_ap_len,
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003860 sm->key_mgmt, sm->fils_key_auth_sta,
3861 sm->fils_key_auth_ap,
3862 &sm->fils_key_auth_len);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003863 wpabuf_free(pub);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003864 os_memset(ick, 0, sizeof(ick));
3865 return res;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003866fail:
3867 wpabuf_free(pub);
3868 wpabuf_clear_free(dh_ss);
3869 return -1;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003870}
3871
3872
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003873#ifdef CONFIG_IEEE80211R
3874static int fils_ft_build_assoc_req_rsne(struct wpa_sm *sm, struct wpabuf *buf)
3875{
3876 struct rsn_ie_hdr *rsnie;
3877 u16 capab;
3878 u8 *pos;
Roshan Pius3a1667e2018-07-03 15:17:14 -07003879 int use_sha384 = wpa_key_mgmt_sha384(sm->key_mgmt);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003880
3881 /* RSNIE[PMKR0Name/PMKR1Name] */
3882 rsnie = wpabuf_put(buf, sizeof(*rsnie));
3883 rsnie->elem_id = WLAN_EID_RSN;
3884 WPA_PUT_LE16(rsnie->version, RSN_VERSION);
3885
3886 /* Group Suite Selector */
3887 if (!wpa_cipher_valid_group(sm->group_cipher)) {
3888 wpa_printf(MSG_WARNING, "FT: Invalid group cipher (%d)",
3889 sm->group_cipher);
3890 return -1;
3891 }
3892 pos = wpabuf_put(buf, RSN_SELECTOR_LEN);
3893 RSN_SELECTOR_PUT(pos, wpa_cipher_to_suite(WPA_PROTO_RSN,
3894 sm->group_cipher));
3895
3896 /* Pairwise Suite Count */
3897 wpabuf_put_le16(buf, 1);
3898
3899 /* Pairwise Suite List */
3900 if (!wpa_cipher_valid_pairwise(sm->pairwise_cipher)) {
3901 wpa_printf(MSG_WARNING, "FT: Invalid pairwise cipher (%d)",
3902 sm->pairwise_cipher);
3903 return -1;
3904 }
3905 pos = wpabuf_put(buf, RSN_SELECTOR_LEN);
3906 RSN_SELECTOR_PUT(pos, wpa_cipher_to_suite(WPA_PROTO_RSN,
3907 sm->pairwise_cipher));
3908
3909 /* Authenticated Key Management Suite Count */
3910 wpabuf_put_le16(buf, 1);
3911
3912 /* Authenticated Key Management Suite List */
3913 pos = wpabuf_put(buf, RSN_SELECTOR_LEN);
3914 if (sm->key_mgmt == WPA_KEY_MGMT_FT_FILS_SHA256)
3915 RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_FT_FILS_SHA256);
3916 else if (sm->key_mgmt == WPA_KEY_MGMT_FT_FILS_SHA384)
3917 RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_FT_FILS_SHA384);
3918 else {
3919 wpa_printf(MSG_WARNING,
3920 "FILS+FT: Invalid key management type (%d)",
3921 sm->key_mgmt);
3922 return -1;
3923 }
3924
3925 /* RSN Capabilities */
3926 capab = 0;
3927#ifdef CONFIG_IEEE80211W
3928 if (sm->mgmt_group_cipher == WPA_CIPHER_AES_128_CMAC)
3929 capab |= WPA_CAPABILITY_MFPC;
3930#endif /* CONFIG_IEEE80211W */
Hai Shalom39bc25d2019-02-06 16:32:13 -08003931 if (sm->ocv)
3932 capab |= WPA_CAPABILITY_OCVC;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003933 wpabuf_put_le16(buf, capab);
3934
3935 /* PMKID Count */
3936 wpabuf_put_le16(buf, 1);
3937
3938 /* PMKID List [PMKR1Name] */
3939 wpa_hexdump_key(MSG_DEBUG, "FILS+FT: XXKey (FILS-FT)",
3940 sm->fils_ft, sm->fils_ft_len);
3941 wpa_hexdump_ascii(MSG_DEBUG, "FILS+FT: SSID", sm->ssid, sm->ssid_len);
3942 wpa_hexdump(MSG_DEBUG, "FILS+FT: MDID",
3943 sm->mobility_domain, MOBILITY_DOMAIN_ID_LEN);
3944 wpa_hexdump_ascii(MSG_DEBUG, "FILS+FT: R0KH-ID",
3945 sm->r0kh_id, sm->r0kh_id_len);
3946 if (wpa_derive_pmk_r0(sm->fils_ft, sm->fils_ft_len, sm->ssid,
3947 sm->ssid_len, sm->mobility_domain,
3948 sm->r0kh_id, sm->r0kh_id_len, sm->own_addr,
Roshan Pius3a1667e2018-07-03 15:17:14 -07003949 sm->pmk_r0, sm->pmk_r0_name, use_sha384) < 0) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003950 wpa_printf(MSG_WARNING, "FILS+FT: Could not derive PMK-R0");
3951 return -1;
3952 }
Roshan Pius3a1667e2018-07-03 15:17:14 -07003953 sm->pmk_r0_len = use_sha384 ? SHA384_MAC_LEN : PMK_LEN;
3954 wpa_hexdump_key(MSG_DEBUG, "FILS+FT: PMK-R0",
3955 sm->pmk_r0, sm->pmk_r0_len);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003956 wpa_hexdump(MSG_DEBUG, "FILS+FT: PMKR0Name",
3957 sm->pmk_r0_name, WPA_PMK_NAME_LEN);
3958 wpa_printf(MSG_DEBUG, "FILS+FT: R1KH-ID: " MACSTR,
3959 MAC2STR(sm->r1kh_id));
3960 pos = wpabuf_put(buf, WPA_PMK_NAME_LEN);
3961 if (wpa_derive_pmk_r1_name(sm->pmk_r0_name, sm->r1kh_id, sm->own_addr,
Roshan Pius3a1667e2018-07-03 15:17:14 -07003962 pos, use_sha384) < 0) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003963 wpa_printf(MSG_WARNING, "FILS+FT: Could not derive PMKR1Name");
3964 return -1;
3965 }
3966 wpa_hexdump(MSG_DEBUG, "FILS+FT: PMKR1Name", pos, WPA_PMK_NAME_LEN);
3967
3968#ifdef CONFIG_IEEE80211W
3969 if (sm->mgmt_group_cipher == WPA_CIPHER_AES_128_CMAC) {
3970 /* Management Group Cipher Suite */
3971 pos = wpabuf_put(buf, RSN_SELECTOR_LEN);
3972 RSN_SELECTOR_PUT(pos, RSN_CIPHER_SUITE_AES_128_CMAC);
3973 }
3974#endif /* CONFIG_IEEE80211W */
3975
3976 rsnie->len = ((u8 *) wpabuf_put(buf, 0) - (u8 *) rsnie) - 2;
3977 return 0;
3978}
3979#endif /* CONFIG_IEEE80211R */
3980
3981
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003982struct wpabuf * fils_build_assoc_req(struct wpa_sm *sm, const u8 **kek,
3983 size_t *kek_len, const u8 **snonce,
Paul Stewart092955c2017-02-06 09:13:09 -08003984 const u8 **anonce,
3985 const struct wpabuf **hlp,
3986 unsigned int num_hlp)
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003987{
3988 struct wpabuf *buf;
Paul Stewart092955c2017-02-06 09:13:09 -08003989 size_t len;
3990 unsigned int i;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003991
Paul Stewart092955c2017-02-06 09:13:09 -08003992 len = 1000;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003993#ifdef CONFIG_IEEE80211R
3994 if (sm->fils_ft_ies)
3995 len += wpabuf_len(sm->fils_ft_ies);
3996 if (wpa_key_mgmt_ft(sm->key_mgmt))
3997 len += 256;
3998#endif /* CONFIG_IEEE80211R */
Paul Stewart092955c2017-02-06 09:13:09 -08003999 for (i = 0; hlp && i < num_hlp; i++)
4000 len += 10 + wpabuf_len(hlp[i]);
4001 buf = wpabuf_alloc(len);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004002 if (!buf)
4003 return NULL;
4004
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004005#ifdef CONFIG_IEEE80211R
4006 if (wpa_key_mgmt_ft(sm->key_mgmt) && sm->fils_ft_ies) {
4007 /* MDE and FTE when using FILS+FT */
4008 wpabuf_put_buf(buf, sm->fils_ft_ies);
4009 /* RSNE with PMKR1Name in PMKID field */
4010 if (fils_ft_build_assoc_req_rsne(sm, buf) < 0) {
4011 wpabuf_free(buf);
4012 return NULL;
4013 }
4014 }
4015#endif /* CONFIG_IEEE80211R */
4016
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004017 /* FILS Session */
4018 wpabuf_put_u8(buf, WLAN_EID_EXTENSION); /* Element ID */
4019 wpabuf_put_u8(buf, 1 + FILS_SESSION_LEN); /* Length */
4020 /* Element ID Extension */
4021 wpabuf_put_u8(buf, WLAN_EID_EXT_FILS_SESSION);
4022 wpabuf_put_data(buf, sm->fils_session, FILS_SESSION_LEN);
4023
4024 /* Everything after FILS Session element gets encrypted in the driver
4025 * with KEK. The buffer returned from here is the plaintext version. */
4026
4027 /* TODO: FILS Public Key */
4028
4029 /* FILS Key Confirm */
4030 wpabuf_put_u8(buf, WLAN_EID_EXTENSION); /* Element ID */
4031 wpabuf_put_u8(buf, 1 + sm->fils_key_auth_len); /* Length */
4032 /* Element ID Extension */
4033 wpabuf_put_u8(buf, WLAN_EID_EXT_FILS_KEY_CONFIRM);
4034 wpabuf_put_data(buf, sm->fils_key_auth_sta, sm->fils_key_auth_len);
4035
Paul Stewart092955c2017-02-06 09:13:09 -08004036 /* FILS HLP Container */
4037 for (i = 0; hlp && i < num_hlp; i++) {
4038 const u8 *pos = wpabuf_head(hlp[i]);
4039 size_t left = wpabuf_len(hlp[i]);
4040
4041 wpabuf_put_u8(buf, WLAN_EID_EXTENSION); /* Element ID */
4042 if (left <= 254)
4043 len = 1 + left;
4044 else
4045 len = 255;
4046 wpabuf_put_u8(buf, len); /* Length */
4047 /* Element ID Extension */
4048 wpabuf_put_u8(buf, WLAN_EID_EXT_FILS_HLP_CONTAINER);
4049 /* Destination MAC Address, Source MAC Address, HLP Packet.
4050 * HLP Packet is in MSDU format (i.e., included the LLC/SNAP
4051 * header when LPD is used). */
4052 wpabuf_put_data(buf, pos, len - 1);
4053 pos += len - 1;
4054 left -= len - 1;
4055 while (left) {
4056 wpabuf_put_u8(buf, WLAN_EID_FRAGMENT);
4057 len = left > 255 ? 255 : left;
4058 wpabuf_put_u8(buf, len);
4059 wpabuf_put_data(buf, pos, len);
4060 pos += len;
4061 left -= len;
4062 }
4063 }
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004064
4065 /* TODO: FILS IP Address Assignment */
4066
Hai Shalom39bc25d2019-02-06 16:32:13 -08004067#ifdef CONFIG_OCV
4068 if (wpa_sm_ocv_enabled(sm)) {
4069 struct wpa_channel_info ci;
4070 u8 *pos;
4071
4072 if (wpa_sm_channel_info(sm, &ci) != 0) {
4073 wpa_printf(MSG_WARNING,
4074 "FILS: Failed to get channel info for OCI element");
4075 wpabuf_free(buf);
4076 return NULL;
4077 }
4078
4079 pos = wpabuf_put(buf, OCV_OCI_EXTENDED_LEN);
4080 if (ocv_insert_extended_oci(&ci, pos) < 0) {
4081 wpabuf_free(buf);
4082 return NULL;
4083 }
4084 }
4085#endif /* CONFIG_OCV */
4086
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004087 wpa_hexdump_buf(MSG_DEBUG, "FILS: Association Request plaintext", buf);
4088
4089 *kek = sm->ptk.kek;
4090 *kek_len = sm->ptk.kek_len;
4091 wpa_hexdump_key(MSG_DEBUG, "FILS: KEK for AEAD", *kek, *kek_len);
4092 *snonce = sm->fils_nonce;
4093 wpa_hexdump(MSG_DEBUG, "FILS: SNonce for AEAD AAD",
4094 *snonce, FILS_NONCE_LEN);
4095 *anonce = sm->fils_anonce;
4096 wpa_hexdump(MSG_DEBUG, "FILS: ANonce for AEAD AAD",
4097 *anonce, FILS_NONCE_LEN);
4098
4099 return buf;
4100}
4101
4102
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08004103static void fils_process_hlp_resp(struct wpa_sm *sm, const u8 *resp, size_t len)
4104{
4105 const u8 *pos, *end;
4106
4107 wpa_hexdump(MSG_MSGDUMP, "FILS: HLP response", resp, len);
4108 if (len < 2 * ETH_ALEN)
4109 return;
4110 pos = resp + 2 * ETH_ALEN;
4111 end = resp + len;
4112 if (end - pos >= 6 &&
4113 os_memcmp(pos, "\xaa\xaa\x03\x00\x00\x00", 6) == 0)
4114 pos += 6; /* Remove SNAP/LLC header */
4115 wpa_sm_fils_hlp_rx(sm, resp, resp + ETH_ALEN, pos, end - pos);
4116}
4117
4118
4119static void fils_process_hlp_container(struct wpa_sm *sm, const u8 *pos,
4120 size_t len)
4121{
4122 const u8 *end = pos + len;
4123 u8 *tmp, *tmp_pos;
4124
4125 /* Check if there are any FILS HLP Container elements */
4126 while (end - pos >= 2) {
4127 if (2 + pos[1] > end - pos)
4128 return;
4129 if (pos[0] == WLAN_EID_EXTENSION &&
4130 pos[1] >= 1 + 2 * ETH_ALEN &&
4131 pos[2] == WLAN_EID_EXT_FILS_HLP_CONTAINER)
4132 break;
4133 pos += 2 + pos[1];
4134 }
4135 if (end - pos < 2)
4136 return; /* No FILS HLP Container elements */
4137
4138 tmp = os_malloc(end - pos);
4139 if (!tmp)
4140 return;
4141
4142 while (end - pos >= 2) {
4143 if (2 + pos[1] > end - pos ||
4144 pos[0] != WLAN_EID_EXTENSION ||
4145 pos[1] < 1 + 2 * ETH_ALEN ||
4146 pos[2] != WLAN_EID_EXT_FILS_HLP_CONTAINER)
4147 break;
4148 tmp_pos = tmp;
4149 os_memcpy(tmp_pos, pos + 3, pos[1] - 1);
4150 tmp_pos += pos[1] - 1;
4151 pos += 2 + pos[1];
4152
4153 /* Add possible fragments */
4154 while (end - pos >= 2 && pos[0] == WLAN_EID_FRAGMENT &&
4155 2 + pos[1] <= end - pos) {
4156 os_memcpy(tmp_pos, pos + 2, pos[1]);
4157 tmp_pos += pos[1];
4158 pos += 2 + pos[1];
4159 }
4160
4161 fils_process_hlp_resp(sm, tmp, tmp_pos - tmp);
4162 }
4163
4164 os_free(tmp);
4165}
4166
4167
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004168int fils_process_assoc_resp(struct wpa_sm *sm, const u8 *resp, size_t len)
4169{
4170 const struct ieee80211_mgmt *mgmt;
4171 const u8 *end, *ie_start;
4172 struct ieee802_11_elems elems;
4173 int keylen, rsclen;
4174 enum wpa_alg alg;
4175 struct wpa_gtk_data gd;
4176 int maxkeylen;
4177 struct wpa_eapol_ie_parse kde;
4178
4179 if (!sm || !sm->ptk_set) {
4180 wpa_printf(MSG_DEBUG, "FILS: No KEK available");
4181 return -1;
4182 }
4183
4184 if (!wpa_key_mgmt_fils(sm->key_mgmt)) {
4185 wpa_printf(MSG_DEBUG, "FILS: Not a FILS AKM");
4186 return -1;
4187 }
4188
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004189 if (sm->fils_completed) {
4190 wpa_printf(MSG_DEBUG,
4191 "FILS: Association has already been completed for this FILS authentication - ignore unexpected retransmission");
4192 return -1;
4193 }
4194
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004195 wpa_hexdump(MSG_DEBUG, "FILS: (Re)Association Response frame",
4196 resp, len);
4197
4198 mgmt = (const struct ieee80211_mgmt *) resp;
4199 if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.assoc_resp))
4200 return -1;
4201
4202 end = resp + len;
4203 /* Same offset for Association Response and Reassociation Response */
4204 ie_start = mgmt->u.assoc_resp.variable;
4205
4206 if (ieee802_11_parse_elems(ie_start, end - ie_start, &elems, 1) ==
4207 ParseFailed) {
4208 wpa_printf(MSG_DEBUG,
4209 "FILS: Failed to parse decrypted elements");
4210 goto fail;
4211 }
4212
4213 if (!elems.fils_session) {
4214 wpa_printf(MSG_DEBUG, "FILS: No FILS Session element");
4215 return -1;
4216 }
4217 if (os_memcmp(elems.fils_session, sm->fils_session,
4218 FILS_SESSION_LEN) != 0) {
4219 wpa_printf(MSG_DEBUG, "FILS: FILS Session mismatch");
4220 wpa_hexdump(MSG_DEBUG, "FILS: Received FILS Session",
4221 elems.fils_session, FILS_SESSION_LEN);
4222 wpa_hexdump(MSG_DEBUG, "FILS: Expected FILS Session",
4223 sm->fils_session, FILS_SESSION_LEN);
4224 }
4225
4226 /* TODO: FILS Public Key */
4227
4228 if (!elems.fils_key_confirm) {
4229 wpa_printf(MSG_DEBUG, "FILS: No FILS Key Confirm element");
4230 goto fail;
4231 }
4232 if (elems.fils_key_confirm_len != sm->fils_key_auth_len) {
4233 wpa_printf(MSG_DEBUG,
4234 "FILS: Unexpected Key-Auth length %d (expected %d)",
4235 elems.fils_key_confirm_len,
4236 (int) sm->fils_key_auth_len);
4237 goto fail;
4238 }
4239 if (os_memcmp(elems.fils_key_confirm, sm->fils_key_auth_ap,
4240 sm->fils_key_auth_len) != 0) {
4241 wpa_printf(MSG_DEBUG, "FILS: Key-Auth mismatch");
4242 wpa_hexdump(MSG_DEBUG, "FILS: Received Key-Auth",
4243 elems.fils_key_confirm,
4244 elems.fils_key_confirm_len);
4245 wpa_hexdump(MSG_DEBUG, "FILS: Expected Key-Auth",
4246 sm->fils_key_auth_ap, sm->fils_key_auth_len);
4247 goto fail;
4248 }
4249
Hai Shalom39bc25d2019-02-06 16:32:13 -08004250#ifdef CONFIG_OCV
4251 if (wpa_sm_ocv_enabled(sm)) {
4252 struct wpa_channel_info ci;
4253
4254 if (wpa_sm_channel_info(sm, &ci) != 0) {
4255 wpa_printf(MSG_WARNING,
4256 "Failed to get channel info to validate received OCI in FILS (Re)Association Response frame");
4257 goto fail;
4258 }
4259
4260 if (ocv_verify_tx_params(elems.oci, elems.oci_len, &ci,
4261 channel_width_to_int(ci.chanwidth),
4262 ci.seg1_idx) != 0) {
4263 wpa_printf(MSG_WARNING, "FILS: %s", ocv_errorstr);
4264 goto fail;
4265 }
4266 }
4267#endif /* CONFIG_OCV */
4268
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004269 /* Key Delivery */
4270 if (!elems.key_delivery) {
4271 wpa_printf(MSG_DEBUG, "FILS: No Key Delivery element");
4272 goto fail;
4273 }
4274
4275 /* Parse GTK and set the key to the driver */
4276 os_memset(&gd, 0, sizeof(gd));
4277 if (wpa_supplicant_parse_ies(elems.key_delivery + WPA_KEY_RSC_LEN,
4278 elems.key_delivery_len - WPA_KEY_RSC_LEN,
4279 &kde) < 0) {
4280 wpa_printf(MSG_DEBUG, "FILS: Failed to parse KDEs");
4281 goto fail;
4282 }
4283 if (!kde.gtk) {
4284 wpa_printf(MSG_DEBUG, "FILS: No GTK KDE");
4285 goto fail;
4286 }
4287 maxkeylen = gd.gtk_len = kde.gtk_len - 2;
4288 if (wpa_supplicant_check_group_cipher(sm, sm->group_cipher,
4289 gd.gtk_len, maxkeylen,
4290 &gd.key_rsc_len, &gd.alg))
4291 goto fail;
4292
4293 wpa_hexdump_key(MSG_DEBUG, "FILS: Received GTK", kde.gtk, kde.gtk_len);
4294 gd.keyidx = kde.gtk[0] & 0x3;
4295 gd.tx = wpa_supplicant_gtk_tx_bit_workaround(sm,
4296 !!(kde.gtk[0] & BIT(2)));
4297 if (kde.gtk_len - 2 > sizeof(gd.gtk)) {
4298 wpa_printf(MSG_DEBUG, "FILS: Too long GTK in GTK KDE (len=%lu)",
4299 (unsigned long) kde.gtk_len - 2);
4300 goto fail;
4301 }
4302 os_memcpy(gd.gtk, kde.gtk + 2, kde.gtk_len - 2);
4303
4304 wpa_printf(MSG_DEBUG, "FILS: Set GTK to driver");
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004305 if (wpa_supplicant_install_gtk(sm, &gd, elems.key_delivery, 0) < 0) {
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004306 wpa_printf(MSG_DEBUG, "FILS: Failed to set GTK");
4307 goto fail;
4308 }
4309
4310 if (ieee80211w_set_keys(sm, &kde) < 0) {
4311 wpa_printf(MSG_DEBUG, "FILS: Failed to set IGTK");
4312 goto fail;
4313 }
4314
4315 alg = wpa_cipher_to_alg(sm->pairwise_cipher);
4316 keylen = wpa_cipher_key_len(sm->pairwise_cipher);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004317 if (keylen <= 0 || (unsigned int) keylen != sm->ptk.tk_len) {
4318 wpa_printf(MSG_DEBUG, "FILS: TK length mismatch: %u != %lu",
4319 keylen, (long unsigned int) sm->ptk.tk_len);
4320 goto fail;
4321 }
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004322 rsclen = wpa_cipher_rsc_len(sm->pairwise_cipher);
4323 wpa_hexdump_key(MSG_DEBUG, "FILS: Set TK to driver",
4324 sm->ptk.tk, keylen);
4325 if (wpa_sm_set_key(sm, alg, sm->bssid, 0, 1, null_rsc, rsclen,
4326 sm->ptk.tk, keylen) < 0) {
4327 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
4328 "FILS: Failed to set PTK to the driver (alg=%d keylen=%d bssid="
4329 MACSTR ")",
4330 alg, keylen, MAC2STR(sm->bssid));
4331 goto fail;
4332 }
4333
4334 /* TODO: TK could be cleared after auth frame exchange now that driver
4335 * takes care of association frame encryption/decryption. */
4336 /* TK is not needed anymore in supplicant */
4337 os_memset(sm->ptk.tk, 0, WPA_TK_MAX_LEN);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004338 sm->ptk.tk_len = 0;
Mathy Vanhoefc66556c2017-09-29 04:22:51 +02004339 sm->ptk.installed = 1;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004340
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08004341 /* FILS HLP Container */
4342 fils_process_hlp_container(sm, ie_start, end - ie_start);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004343
4344 /* TODO: FILS IP Address Assignment */
4345
4346 wpa_printf(MSG_DEBUG, "FILS: Auth+Assoc completed successfully");
4347 sm->fils_completed = 1;
4348
4349 return 0;
4350fail:
4351 return -1;
4352}
4353
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004354
4355void wpa_sm_set_reset_fils_completed(struct wpa_sm *sm, int set)
4356{
4357 if (sm)
4358 sm->fils_completed = !!set;
4359}
4360
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004361#endif /* CONFIG_FILS */
4362
4363
4364int wpa_fils_is_completed(struct wpa_sm *sm)
4365{
4366#ifdef CONFIG_FILS
4367 return sm && sm->fils_completed;
4368#else /* CONFIG_FILS */
4369 return 0;
4370#endif /* CONFIG_FILS */
4371}
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004372
4373
4374#ifdef CONFIG_OWE
4375
4376struct wpabuf * owe_build_assoc_req(struct wpa_sm *sm, u16 group)
4377{
4378 struct wpabuf *ie = NULL, *pub = NULL;
4379 size_t prime_len;
4380
4381 if (group == 19)
4382 prime_len = 32;
4383 else if (group == 20)
4384 prime_len = 48;
4385 else if (group == 21)
4386 prime_len = 66;
4387 else
4388 return NULL;
4389
4390 crypto_ecdh_deinit(sm->owe_ecdh);
4391 sm->owe_ecdh = crypto_ecdh_init(group);
4392 if (!sm->owe_ecdh)
4393 goto fail;
4394 sm->owe_group = group;
4395 pub = crypto_ecdh_get_pubkey(sm->owe_ecdh, 0);
4396 pub = wpabuf_zeropad(pub, prime_len);
4397 if (!pub)
4398 goto fail;
4399
4400 ie = wpabuf_alloc(5 + wpabuf_len(pub));
4401 if (!ie)
4402 goto fail;
4403 wpabuf_put_u8(ie, WLAN_EID_EXTENSION);
4404 wpabuf_put_u8(ie, 1 + 2 + wpabuf_len(pub));
4405 wpabuf_put_u8(ie, WLAN_EID_EXT_OWE_DH_PARAM);
4406 wpabuf_put_le16(ie, group);
4407 wpabuf_put_buf(ie, pub);
4408 wpabuf_free(pub);
4409 wpa_hexdump_buf(MSG_DEBUG, "OWE: Diffie-Hellman Parameter element",
4410 ie);
4411
4412 return ie;
4413fail:
4414 wpabuf_free(pub);
4415 crypto_ecdh_deinit(sm->owe_ecdh);
4416 sm->owe_ecdh = NULL;
4417 return NULL;
4418}
4419
4420
4421int owe_process_assoc_resp(struct wpa_sm *sm, const u8 *bssid,
4422 const u8 *resp_ies, size_t resp_ies_len)
4423{
4424 struct ieee802_11_elems elems;
4425 u16 group;
4426 struct wpabuf *secret, *pub, *hkey;
4427 int res;
4428 u8 prk[SHA512_MAC_LEN], pmkid[SHA512_MAC_LEN];
4429 const char *info = "OWE Key Generation";
4430 const u8 *addr[2];
4431 size_t len[2];
4432 size_t hash_len, prime_len;
4433 struct wpa_ie_data data;
4434
4435 if (!resp_ies ||
4436 ieee802_11_parse_elems(resp_ies, resp_ies_len, &elems, 1) ==
4437 ParseFailed) {
4438 wpa_printf(MSG_INFO,
4439 "OWE: Could not parse Association Response frame elements");
4440 return -1;
4441 }
4442
4443 if (sm->cur_pmksa && elems.rsn_ie &&
4444 wpa_parse_wpa_ie_rsn(elems.rsn_ie - 2, 2 + elems.rsn_ie_len,
4445 &data) == 0 &&
4446 data.num_pmkid == 1 && data.pmkid &&
4447 os_memcmp(sm->cur_pmksa->pmkid, data.pmkid, PMKID_LEN) == 0) {
4448 wpa_printf(MSG_DEBUG, "OWE: Use PMKSA caching");
4449 wpa_sm_set_pmk_from_pmksa(sm);
4450 return 0;
4451 }
4452
4453 if (!elems.owe_dh) {
4454 wpa_printf(MSG_INFO,
4455 "OWE: No Diffie-Hellman Parameter element found in Association Response frame");
4456 return -1;
4457 }
4458
4459 group = WPA_GET_LE16(elems.owe_dh);
4460 if (group != sm->owe_group) {
4461 wpa_printf(MSG_INFO,
4462 "OWE: Unexpected Diffie-Hellman group in response: %u",
4463 group);
4464 return -1;
4465 }
4466
4467 if (!sm->owe_ecdh) {
4468 wpa_printf(MSG_INFO, "OWE: No ECDH state available");
4469 return -1;
4470 }
4471
4472 if (group == 19)
4473 prime_len = 32;
4474 else if (group == 20)
4475 prime_len = 48;
4476 else if (group == 21)
4477 prime_len = 66;
4478 else
4479 return -1;
4480
4481 secret = crypto_ecdh_set_peerkey(sm->owe_ecdh, 0,
4482 elems.owe_dh + 2,
4483 elems.owe_dh_len - 2);
4484 secret = wpabuf_zeropad(secret, prime_len);
4485 if (!secret) {
4486 wpa_printf(MSG_DEBUG, "OWE: Invalid peer DH public key");
4487 return -1;
4488 }
4489 wpa_hexdump_buf_key(MSG_DEBUG, "OWE: DH shared secret", secret);
4490
4491 /* prk = HKDF-extract(C | A | group, z) */
4492
4493 pub = crypto_ecdh_get_pubkey(sm->owe_ecdh, 0);
4494 if (!pub) {
4495 wpabuf_clear_free(secret);
4496 return -1;
4497 }
4498
4499 /* PMKID = Truncate-128(Hash(C | A)) */
4500 addr[0] = wpabuf_head(pub);
4501 len[0] = wpabuf_len(pub);
4502 addr[1] = elems.owe_dh + 2;
4503 len[1] = elems.owe_dh_len - 2;
4504 if (group == 19) {
4505 res = sha256_vector(2, addr, len, pmkid);
4506 hash_len = SHA256_MAC_LEN;
4507 } else if (group == 20) {
4508 res = sha384_vector(2, addr, len, pmkid);
4509 hash_len = SHA384_MAC_LEN;
4510 } else if (group == 21) {
4511 res = sha512_vector(2, addr, len, pmkid);
4512 hash_len = SHA512_MAC_LEN;
4513 } else {
4514 res = -1;
4515 hash_len = 0;
4516 }
4517 pub = wpabuf_zeropad(pub, prime_len);
4518 if (res < 0 || !pub) {
4519 wpabuf_free(pub);
4520 wpabuf_clear_free(secret);
4521 return -1;
4522 }
4523
4524 hkey = wpabuf_alloc(wpabuf_len(pub) + elems.owe_dh_len - 2 + 2);
4525 if (!hkey) {
4526 wpabuf_free(pub);
4527 wpabuf_clear_free(secret);
4528 return -1;
4529 }
4530
4531 wpabuf_put_buf(hkey, pub); /* C */
4532 wpabuf_free(pub);
4533 wpabuf_put_data(hkey, elems.owe_dh + 2, elems.owe_dh_len - 2); /* A */
4534 wpabuf_put_le16(hkey, sm->owe_group); /* group */
4535 if (group == 19)
4536 res = hmac_sha256(wpabuf_head(hkey), wpabuf_len(hkey),
4537 wpabuf_head(secret), wpabuf_len(secret), prk);
4538 else if (group == 20)
4539 res = hmac_sha384(wpabuf_head(hkey), wpabuf_len(hkey),
4540 wpabuf_head(secret), wpabuf_len(secret), prk);
4541 else if (group == 21)
4542 res = hmac_sha512(wpabuf_head(hkey), wpabuf_len(hkey),
4543 wpabuf_head(secret), wpabuf_len(secret), prk);
4544 wpabuf_clear_free(hkey);
4545 wpabuf_clear_free(secret);
4546 if (res < 0)
4547 return -1;
4548
4549 wpa_hexdump_key(MSG_DEBUG, "OWE: prk", prk, hash_len);
4550
4551 /* PMK = HKDF-expand(prk, "OWE Key Generation", n) */
4552
4553 if (group == 19)
4554 res = hmac_sha256_kdf(prk, hash_len, NULL, (const u8 *) info,
4555 os_strlen(info), sm->pmk, hash_len);
4556 else if (group == 20)
4557 res = hmac_sha384_kdf(prk, hash_len, NULL, (const u8 *) info,
4558 os_strlen(info), sm->pmk, hash_len);
4559 else if (group == 21)
4560 res = hmac_sha512_kdf(prk, hash_len, NULL, (const u8 *) info,
4561 os_strlen(info), sm->pmk, hash_len);
4562 os_memset(prk, 0, SHA512_MAC_LEN);
Roshan Pius3a1667e2018-07-03 15:17:14 -07004563 if (res < 0) {
4564 sm->pmk_len = 0;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004565 return -1;
Roshan Pius3a1667e2018-07-03 15:17:14 -07004566 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004567 sm->pmk_len = hash_len;
4568
4569 wpa_hexdump_key(MSG_DEBUG, "OWE: PMK", sm->pmk, sm->pmk_len);
4570 wpa_hexdump(MSG_DEBUG, "OWE: PMKID", pmkid, PMKID_LEN);
4571 pmksa_cache_add(sm->pmksa, sm->pmk, sm->pmk_len, pmkid, NULL, 0,
4572 bssid, sm->own_addr, sm->network_ctx, sm->key_mgmt,
4573 NULL);
4574
4575 return 0;
4576}
4577
4578#endif /* CONFIG_OWE */
4579
4580
4581void wpa_sm_set_fils_cache_id(struct wpa_sm *sm, const u8 *fils_cache_id)
4582{
4583#ifdef CONFIG_FILS
4584 if (sm && fils_cache_id) {
4585 sm->fils_cache_id_set = 1;
4586 os_memcpy(sm->fils_cache_id, fils_cache_id, FILS_CACHE_ID_LEN);
4587 }
4588#endif /* CONFIG_FILS */
4589}