blob: a7adbadf5c4c647d41247045f0dd16b80f419eb3 [file] [log] [blame]
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001/*
2 * WPA Supplicant - WPA state machine and EAPOL-Key processing
Roshan Pius3a1667e2018-07-03 15:17:14 -07003 * Copyright (c) 2003-2018, Jouni Malinen <j@w1.fi>
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004 * Copyright(c) 2015 Intel Deutschland GmbH
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005 *
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006 * This software may be distributed under the terms of the BSD license.
7 * See README for more details.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008 */
9
10#include "includes.h"
11
12#include "common.h"
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -080013#include "crypto/aes.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070014#include "crypto/aes_wrap.h"
15#include "crypto/crypto.h"
16#include "crypto/random.h"
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -080017#include "crypto/aes_siv.h"
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070018#include "crypto/sha256.h"
19#include "crypto/sha384.h"
20#include "crypto/sha512.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070021#include "common/ieee802_11_defs.h"
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -080022#include "common/ieee802_11_common.h"
Hai Shalom74f70d42019-02-11 14:42:39 -080023#include "common/ocv.h"
Paul Stewart092955c2017-02-06 09:13:09 -080024#include "eap_common/eap_defs.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070025#include "eapol_supp/eapol_supp_sm.h"
Hai Shalom74f70d42019-02-11 14:42:39 -080026#include "drivers/driver.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070027#include "wpa.h"
28#include "eloop.h"
29#include "preauth.h"
30#include "pmksa_cache.h"
31#include "wpa_i.h"
32#include "wpa_ie.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070033
34
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080035static const u8 null_rsc[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
36
37
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070038/**
39 * wpa_eapol_key_send - Send WPA/RSN EAPOL-Key message
40 * @sm: Pointer to WPA state machine data from wpa_sm_init()
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -080041 * @ptk: PTK for Key Confirmation/Encryption Key
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070042 * @ver: Version field from Key Info
43 * @dest: Destination address for the frame
44 * @proto: Ethertype (usually ETH_P_EAPOL)
45 * @msg: EAPOL-Key message
46 * @msg_len: Length of message
47 * @key_mic: Pointer to the buffer to which the EAPOL-Key MIC is written
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080048 * Returns: >= 0 on success, < 0 on failure
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070049 */
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -080050int wpa_eapol_key_send(struct wpa_sm *sm, struct wpa_ptk *ptk,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080051 int ver, const u8 *dest, u16 proto,
52 u8 *msg, size_t msg_len, u8 *key_mic)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070053{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080054 int ret = -1;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070055 size_t mic_len = wpa_mic_len(sm->key_mgmt, sm->pmk_len);
Dmitry Shmidt807291d2015-01-27 13:40:23 -080056
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070057 wpa_printf(MSG_DEBUG, "WPA: Send EAPOL-Key frame to " MACSTR
58 " ver=%d mic_len=%d key_mgmt=0x%x",
59 MAC2STR(dest), ver, (int) mic_len, sm->key_mgmt);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070060 if (is_zero_ether_addr(dest) && is_zero_ether_addr(sm->bssid)) {
61 /*
62 * Association event was not yet received; try to fetch
63 * BSSID from the driver.
64 */
65 if (wpa_sm_get_bssid(sm, sm->bssid) < 0) {
66 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
67 "WPA: Failed to read BSSID for "
68 "EAPOL-Key destination address");
69 } else {
70 dest = sm->bssid;
71 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
72 "WPA: Use BSSID (" MACSTR
73 ") as the destination for EAPOL-Key",
74 MAC2STR(dest));
75 }
76 }
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -080077
78 if (mic_len) {
79 if (key_mic && (!ptk || !ptk->kck_len))
80 goto out;
81
82 if (key_mic &&
83 wpa_eapol_key_mic(ptk->kck, ptk->kck_len, sm->key_mgmt, ver,
84 msg, msg_len, key_mic)) {
85 wpa_msg(sm->ctx->msg_ctx, MSG_ERROR,
86 "WPA: Failed to generate EAPOL-Key version %d key_mgmt 0x%x MIC",
87 ver, sm->key_mgmt);
88 goto out;
89 }
Dmitry Shmidt29333592017-01-09 12:27:11 -080090 if (ptk)
91 wpa_hexdump_key(MSG_DEBUG, "WPA: KCK",
92 ptk->kck, ptk->kck_len);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -080093 wpa_hexdump(MSG_DEBUG, "WPA: Derived Key MIC",
94 key_mic, mic_len);
95 } else {
96#ifdef CONFIG_FILS
97 /* AEAD cipher - Key MIC field not used */
98 struct ieee802_1x_hdr *s_hdr, *hdr;
99 struct wpa_eapol_key *s_key, *key;
100 u8 *buf, *s_key_data, *key_data;
101 size_t buf_len = msg_len + AES_BLOCK_SIZE;
102 size_t key_data_len;
103 u16 eapol_len;
104 const u8 *aad[1];
105 size_t aad_len[1];
106
107 if (!ptk || !ptk->kek_len)
108 goto out;
109
110 key_data_len = msg_len - sizeof(struct ieee802_1x_hdr) -
111 sizeof(struct wpa_eapol_key) - 2;
112
113 buf = os_malloc(buf_len);
114 if (!buf)
115 goto out;
116
117 os_memcpy(buf, msg, msg_len);
118 hdr = (struct ieee802_1x_hdr *) buf;
119 key = (struct wpa_eapol_key *) (hdr + 1);
120 key_data = ((u8 *) (key + 1)) + 2;
121
122 /* Update EAPOL header to include AES-SIV overhead */
123 eapol_len = be_to_host16(hdr->length);
124 eapol_len += AES_BLOCK_SIZE;
125 hdr->length = host_to_be16(eapol_len);
126
127 /* Update Key Data Length field to include AES-SIV overhead */
128 WPA_PUT_BE16((u8 *) (key + 1), AES_BLOCK_SIZE + key_data_len);
129
130 s_hdr = (struct ieee802_1x_hdr *) msg;
131 s_key = (struct wpa_eapol_key *) (s_hdr + 1);
132 s_key_data = ((u8 *) (s_key + 1)) + 2;
133
134 wpa_hexdump_key(MSG_DEBUG, "WPA: Plaintext Key Data",
135 s_key_data, key_data_len);
136
137 wpa_hexdump_key(MSG_DEBUG, "WPA: KEK", ptk->kek, ptk->kek_len);
138 /* AES-SIV AAD from EAPOL protocol version field (inclusive) to
139 * to Key Data (exclusive). */
140 aad[0] = buf;
141 aad_len[0] = key_data - buf;
142 if (aes_siv_encrypt(ptk->kek, ptk->kek_len,
143 s_key_data, key_data_len,
144 1, aad, aad_len, key_data) < 0) {
145 os_free(buf);
146 goto out;
147 }
148
149 wpa_hexdump(MSG_DEBUG, "WPA: Encrypted Key Data from SIV",
150 key_data, AES_BLOCK_SIZE + key_data_len);
151
152 os_free(msg);
153 msg = buf;
154 msg_len = buf_len;
155#else /* CONFIG_FILS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700156 goto out;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800157#endif /* CONFIG_FILS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700158 }
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800159
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700160 wpa_hexdump(MSG_MSGDUMP, "WPA: TX EAPOL-Key", msg, msg_len);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800161 ret = wpa_sm_ether_send(sm, dest, proto, msg, msg_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700162 eapol_sm_notify_tx_eapol_key(sm->eapol);
163out:
164 os_free(msg);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800165 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700166}
167
168
169/**
170 * wpa_sm_key_request - Send EAPOL-Key Request
171 * @sm: Pointer to WPA state machine data from wpa_sm_init()
172 * @error: Indicate whether this is an Michael MIC error report
173 * @pairwise: 1 = error report for pairwise packet, 0 = for group packet
174 *
175 * Send an EAPOL-Key Request to the current authenticator. This function is
176 * used to request rekeying and it is usually called when a local Michael MIC
177 * failure is detected.
178 */
179void wpa_sm_key_request(struct wpa_sm *sm, int error, int pairwise)
180{
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800181 size_t mic_len, hdrlen, rlen;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700182 struct wpa_eapol_key *reply;
183 int key_info, ver;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800184 u8 bssid[ETH_ALEN], *rbuf, *key_mic, *mic;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700185
Roshan Pius3a1667e2018-07-03 15:17:14 -0700186 if (wpa_use_akm_defined(sm->key_mgmt))
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800187 ver = WPA_KEY_INFO_TYPE_AKM_DEFINED;
188 else if (wpa_key_mgmt_ft(sm->key_mgmt) ||
189 wpa_key_mgmt_sha256(sm->key_mgmt))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700190 ver = WPA_KEY_INFO_TYPE_AES_128_CMAC;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700191 else if (sm->pairwise_cipher != WPA_CIPHER_TKIP)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700192 ver = WPA_KEY_INFO_TYPE_HMAC_SHA1_AES;
193 else
194 ver = WPA_KEY_INFO_TYPE_HMAC_MD5_RC4;
195
196 if (wpa_sm_get_bssid(sm, bssid) < 0) {
197 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
198 "Failed to read BSSID for EAPOL-Key request");
199 return;
200 }
201
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700202 mic_len = wpa_mic_len(sm->key_mgmt, sm->pmk_len);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800203 hdrlen = sizeof(*reply) + mic_len + 2;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700204 rbuf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_KEY, NULL,
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800205 hdrlen, &rlen, (void *) &reply);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700206 if (rbuf == NULL)
207 return;
208
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800209 reply->type = (sm->proto == WPA_PROTO_RSN ||
210 sm->proto == WPA_PROTO_OSEN) ?
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700211 EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA;
212 key_info = WPA_KEY_INFO_REQUEST | ver;
213 if (sm->ptk_set)
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800214 key_info |= WPA_KEY_INFO_SECURE;
215 if (sm->ptk_set && mic_len)
216 key_info |= WPA_KEY_INFO_MIC;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700217 if (error)
218 key_info |= WPA_KEY_INFO_ERROR;
219 if (pairwise)
220 key_info |= WPA_KEY_INFO_KEY_TYPE;
221 WPA_PUT_BE16(reply->key_info, key_info);
222 WPA_PUT_BE16(reply->key_length, 0);
223 os_memcpy(reply->replay_counter, sm->request_counter,
224 WPA_REPLAY_COUNTER_LEN);
225 inc_byte_array(sm->request_counter, WPA_REPLAY_COUNTER_LEN);
226
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800227 mic = (u8 *) (reply + 1);
228 WPA_PUT_BE16(mic + mic_len, 0);
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800229 if (!(key_info & WPA_KEY_INFO_MIC))
230 key_mic = NULL;
231 else
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800232 key_mic = mic;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700233
234 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
235 "WPA: Sending EAPOL-Key Request (error=%d "
236 "pairwise=%d ptk_set=%d len=%lu)",
237 error, pairwise, sm->ptk_set, (unsigned long) rlen);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800238 wpa_eapol_key_send(sm, &sm->ptk, ver, bssid, ETH_P_EAPOL, rbuf, rlen,
239 key_mic);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700240}
241
242
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800243static void wpa_supplicant_key_mgmt_set_pmk(struct wpa_sm *sm)
244{
245#ifdef CONFIG_IEEE80211R
246 if (sm->key_mgmt == WPA_KEY_MGMT_FT_IEEE8021X) {
247 if (wpa_sm_key_mgmt_set_pmk(sm, sm->xxkey, sm->xxkey_len))
248 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
249 "RSN: Cannot set low order 256 bits of MSK for key management offload");
250 } else {
251#endif /* CONFIG_IEEE80211R */
252 if (wpa_sm_key_mgmt_set_pmk(sm, sm->pmk, sm->pmk_len))
253 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
254 "RSN: Cannot set PMK for key management offload");
255#ifdef CONFIG_IEEE80211R
256 }
257#endif /* CONFIG_IEEE80211R */
258}
259
260
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700261static int wpa_supplicant_get_pmk(struct wpa_sm *sm,
262 const unsigned char *src_addr,
263 const u8 *pmkid)
264{
265 int abort_cached = 0;
266
267 if (pmkid && !sm->cur_pmksa) {
268 /* When using drivers that generate RSN IE, wpa_supplicant may
269 * not have enough time to get the association information
270 * event before receiving this 1/4 message, so try to find a
271 * matching PMKSA cache entry here. */
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800272 sm->cur_pmksa = pmksa_cache_get(sm->pmksa, src_addr, pmkid,
Roshan Pius3a1667e2018-07-03 15:17:14 -0700273 NULL, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700274 if (sm->cur_pmksa) {
275 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
276 "RSN: found matching PMKID from PMKSA cache");
277 } else {
278 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
279 "RSN: no matching PMKID found");
280 abort_cached = 1;
281 }
282 }
283
284 if (pmkid && sm->cur_pmksa &&
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700285 os_memcmp_const(pmkid, sm->cur_pmksa->pmkid, PMKID_LEN) == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700286 wpa_hexdump(MSG_DEBUG, "RSN: matched PMKID", pmkid, PMKID_LEN);
287 wpa_sm_set_pmk_from_pmksa(sm);
288 wpa_hexdump_key(MSG_DEBUG, "RSN: PMK from PMKSA cache",
289 sm->pmk, sm->pmk_len);
290 eapol_sm_notify_cached(sm->eapol);
291#ifdef CONFIG_IEEE80211R
292 sm->xxkey_len = 0;
Roshan Pius3a1667e2018-07-03 15:17:14 -0700293#ifdef CONFIG_SAE
294 if (sm->key_mgmt == WPA_KEY_MGMT_FT_SAE &&
295 sm->pmk_len == PMK_LEN) {
296 /* Need to allow FT key derivation to proceed with
297 * PMK from SAE being used as the XXKey in cases where
298 * the PMKID in msg 1/4 matches the PMKSA entry that was
299 * just added based on SAE authentication for the
300 * initial mobility domain association. */
301 os_memcpy(sm->xxkey, sm->pmk, sm->pmk_len);
302 sm->xxkey_len = sm->pmk_len;
303 }
304#endif /* CONFIG_SAE */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700305#endif /* CONFIG_IEEE80211R */
306 } else if (wpa_key_mgmt_wpa_ieee8021x(sm->key_mgmt) && sm->eapol) {
307 int res, pmk_len;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800308
Dmitry Shmidtebd93af2017-02-21 13:40:44 -0800309 if (wpa_key_mgmt_sha384(sm->key_mgmt))
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800310 pmk_len = PMK_LEN_SUITE_B_192;
311 else
312 pmk_len = PMK_LEN;
313 res = eapol_sm_get_key(sm->eapol, sm->pmk, pmk_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700314 if (res) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800315 if (pmk_len == PMK_LEN) {
316 /*
317 * EAP-LEAP is an exception from other EAP
318 * methods: it uses only 16-byte PMK.
319 */
320 res = eapol_sm_get_key(sm->eapol, sm->pmk, 16);
321 pmk_len = 16;
322 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700323 } else {
324#ifdef CONFIG_IEEE80211R
325 u8 buf[2 * PMK_LEN];
326 if (eapol_sm_get_key(sm->eapol, buf, 2 * PMK_LEN) == 0)
327 {
Roshan Pius3a1667e2018-07-03 15:17:14 -0700328 if (wpa_key_mgmt_sha384(sm->key_mgmt)) {
329 os_memcpy(sm->xxkey, buf,
330 SHA384_MAC_LEN);
331 sm->xxkey_len = SHA384_MAC_LEN;
332 } else {
333 os_memcpy(sm->xxkey, buf + PMK_LEN,
334 PMK_LEN);
335 sm->xxkey_len = PMK_LEN;
336 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700337 os_memset(buf, 0, sizeof(buf));
338 }
339#endif /* CONFIG_IEEE80211R */
340 }
341 if (res == 0) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700342 struct rsn_pmksa_cache_entry *sa = NULL;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700343 const u8 *fils_cache_id = NULL;
344
345#ifdef CONFIG_FILS
346 if (sm->fils_cache_id_set)
347 fils_cache_id = sm->fils_cache_id;
348#endif /* CONFIG_FILS */
349
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700350 wpa_hexdump_key(MSG_DEBUG, "WPA: PMK from EAPOL state "
351 "machines", sm->pmk, pmk_len);
352 sm->pmk_len = pmk_len;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800353 wpa_supplicant_key_mgmt_set_pmk(sm);
Dmitry Shmidtc55524a2011-07-07 11:18:38 -0700354 if (sm->proto == WPA_PROTO_RSN &&
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800355 !wpa_key_mgmt_suite_b(sm->key_mgmt) &&
Dmitry Shmidtc55524a2011-07-07 11:18:38 -0700356 !wpa_key_mgmt_ft(sm->key_mgmt)) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700357 sa = pmksa_cache_add(sm->pmksa,
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800358 sm->pmk, pmk_len, NULL,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800359 NULL, 0,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700360 src_addr, sm->own_addr,
361 sm->network_ctx,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700362 sm->key_mgmt,
363 fils_cache_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700364 }
365 if (!sm->cur_pmksa && pmkid &&
Roshan Pius3a1667e2018-07-03 15:17:14 -0700366 pmksa_cache_get(sm->pmksa, src_addr, pmkid, NULL,
367 0)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700368 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
369 "RSN: the new PMK matches with the "
370 "PMKID");
371 abort_cached = 0;
Jouni Malinen6ec30382015-07-08 20:48:18 +0300372 } else if (sa && !sm->cur_pmksa && pmkid) {
373 /*
374 * It looks like the authentication server
375 * derived mismatching MSK. This should not
376 * really happen, but bugs happen.. There is not
377 * much we can do here without knowing what
378 * exactly caused the server to misbehave.
379 */
Dmitry Shmidt014a3ff2015-12-28 13:27:49 -0800380 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
Jouni Malinen6ec30382015-07-08 20:48:18 +0300381 "RSN: PMKID mismatch - authentication server may have derived different MSK?!");
382 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700383 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700384
385 if (!sm->cur_pmksa)
386 sm->cur_pmksa = sa;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700387 } else {
388 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
389 "WPA: Failed to get master session key from "
390 "EAPOL state machines - key handshake "
391 "aborted");
392 if (sm->cur_pmksa) {
393 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
394 "RSN: Cancelled PMKSA caching "
395 "attempt");
396 sm->cur_pmksa = NULL;
397 abort_cached = 1;
398 } else if (!abort_cached) {
399 return -1;
400 }
401 }
402 }
403
Dmitry Shmidtc55524a2011-07-07 11:18:38 -0700404 if (abort_cached && wpa_key_mgmt_wpa_ieee8021x(sm->key_mgmt) &&
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800405 !wpa_key_mgmt_suite_b(sm->key_mgmt) &&
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800406 !wpa_key_mgmt_ft(sm->key_mgmt) && sm->key_mgmt != WPA_KEY_MGMT_OSEN)
407 {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700408 /* Send EAPOL-Start to trigger full EAP authentication. */
409 u8 *buf;
410 size_t buflen;
411
412 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
413 "RSN: no PMKSA entry found - trigger "
414 "full EAP authentication");
415 buf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_START,
416 NULL, 0, &buflen, NULL);
417 if (buf) {
418 wpa_sm_ether_send(sm, sm->bssid, ETH_P_EAPOL,
419 buf, buflen);
420 os_free(buf);
421 return -2;
422 }
423
424 return -1;
425 }
426
427 return 0;
428}
429
430
431/**
432 * wpa_supplicant_send_2_of_4 - Send message 2 of WPA/RSN 4-Way Handshake
433 * @sm: Pointer to WPA state machine data from wpa_sm_init()
434 * @dst: Destination address for the frame
435 * @key: Pointer to the EAPOL-Key frame header
436 * @ver: Version bits from EAPOL-Key Key Info
437 * @nonce: Nonce value for the EAPOL-Key frame
438 * @wpa_ie: WPA/RSN IE
439 * @wpa_ie_len: Length of the WPA/RSN IE
440 * @ptk: PTK to use for keyed hash and encryption
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800441 * Returns: >= 0 on success, < 0 on failure
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700442 */
443int wpa_supplicant_send_2_of_4(struct wpa_sm *sm, const unsigned char *dst,
444 const struct wpa_eapol_key *key,
445 int ver, const u8 *nonce,
446 const u8 *wpa_ie, size_t wpa_ie_len,
447 struct wpa_ptk *ptk)
448{
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800449 size_t mic_len, hdrlen, rlen;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700450 struct wpa_eapol_key *reply;
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800451 u8 *rbuf, *key_mic;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700452 u8 *rsn_ie_buf = NULL;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800453 u16 key_info;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700454
455 if (wpa_ie == NULL) {
456 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, "WPA: No wpa_ie set - "
457 "cannot generate msg 2/4");
458 return -1;
459 }
460
461#ifdef CONFIG_IEEE80211R
462 if (wpa_key_mgmt_ft(sm->key_mgmt)) {
463 int res;
464
465 /*
466 * Add PMKR1Name into RSN IE (PMKID-List) and add MDIE and
467 * FTIE from (Re)Association Response.
468 */
469 rsn_ie_buf = os_malloc(wpa_ie_len + 2 + 2 + PMKID_LEN +
470 sm->assoc_resp_ies_len);
471 if (rsn_ie_buf == NULL)
472 return -1;
473 os_memcpy(rsn_ie_buf, wpa_ie, wpa_ie_len);
Dmitry Shmidt55840ad2015-12-14 12:45:46 -0800474 res = wpa_insert_pmkid(rsn_ie_buf, &wpa_ie_len,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700475 sm->pmk_r1_name);
476 if (res < 0) {
477 os_free(rsn_ie_buf);
478 return -1;
479 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700480
481 if (sm->assoc_resp_ies) {
482 os_memcpy(rsn_ie_buf + wpa_ie_len, sm->assoc_resp_ies,
483 sm->assoc_resp_ies_len);
484 wpa_ie_len += sm->assoc_resp_ies_len;
485 }
486
487 wpa_ie = rsn_ie_buf;
488 }
489#endif /* CONFIG_IEEE80211R */
490
491 wpa_hexdump(MSG_DEBUG, "WPA: WPA IE for msg 2/4", wpa_ie, wpa_ie_len);
492
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700493 mic_len = wpa_mic_len(sm->key_mgmt, sm->pmk_len);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800494 hdrlen = sizeof(*reply) + mic_len + 2;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700495 rbuf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_KEY,
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800496 NULL, hdrlen + wpa_ie_len,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700497 &rlen, (void *) &reply);
498 if (rbuf == NULL) {
499 os_free(rsn_ie_buf);
500 return -1;
501 }
502
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800503 reply->type = (sm->proto == WPA_PROTO_RSN ||
504 sm->proto == WPA_PROTO_OSEN) ?
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700505 EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800506 key_info = ver | WPA_KEY_INFO_KEY_TYPE;
507 if (mic_len)
508 key_info |= WPA_KEY_INFO_MIC;
509 else
510 key_info |= WPA_KEY_INFO_ENCR_KEY_DATA;
511 WPA_PUT_BE16(reply->key_info, key_info);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800512 if (sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700513 WPA_PUT_BE16(reply->key_length, 0);
514 else
515 os_memcpy(reply->key_length, key->key_length, 2);
516 os_memcpy(reply->replay_counter, key->replay_counter,
517 WPA_REPLAY_COUNTER_LEN);
518 wpa_hexdump(MSG_DEBUG, "WPA: Replay Counter", reply->replay_counter,
519 WPA_REPLAY_COUNTER_LEN);
520
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800521 key_mic = (u8 *) (reply + 1);
522 WPA_PUT_BE16(key_mic + mic_len, wpa_ie_len); /* Key Data Length */
523 os_memcpy(key_mic + mic_len + 2, wpa_ie, wpa_ie_len); /* Key Data */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700524 os_free(rsn_ie_buf);
525
526 os_memcpy(reply->key_nonce, nonce, WPA_NONCE_LEN);
527
Roshan Pius5e7db942018-04-12 12:27:41 -0700528 wpa_dbg(sm->ctx->msg_ctx, MSG_INFO, "WPA: Sending EAPOL-Key 2/4");
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800529 return wpa_eapol_key_send(sm, ptk, ver, dst, ETH_P_EAPOL, rbuf, rlen,
530 key_mic);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700531}
532
533
534static int wpa_derive_ptk(struct wpa_sm *sm, const unsigned char *src_addr,
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800535 const struct wpa_eapol_key *key, struct wpa_ptk *ptk)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700536{
Hai Shalom021b0b52019-04-10 11:17:58 -0700537 const u8 *z = NULL;
538 size_t z_len = 0;
539
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700540#ifdef CONFIG_IEEE80211R
541 if (wpa_key_mgmt_ft(sm->key_mgmt))
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800542 return wpa_derive_ptk_ft(sm, src_addr, key, ptk);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700543#endif /* CONFIG_IEEE80211R */
544
Hai Shalom021b0b52019-04-10 11:17:58 -0700545#ifdef CONFIG_DPP2
546 if (sm->key_mgmt == WPA_KEY_MGMT_DPP && sm->dpp_z) {
547 z = wpabuf_head(sm->dpp_z);
548 z_len = wpabuf_len(sm->dpp_z);
549 }
550#endif /* CONFIG_DPP2 */
551
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800552 return wpa_pmk_to_ptk(sm->pmk, sm->pmk_len, "Pairwise key expansion",
553 sm->own_addr, sm->bssid, sm->snonce,
554 key->key_nonce, ptk, sm->key_mgmt,
Hai Shalom021b0b52019-04-10 11:17:58 -0700555 sm->pairwise_cipher, z, z_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700556}
557
558
559static void wpa_supplicant_process_1_of_4(struct wpa_sm *sm,
560 const unsigned char *src_addr,
561 const struct wpa_eapol_key *key,
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700562 u16 ver, const u8 *key_data,
563 size_t key_data_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700564{
565 struct wpa_eapol_ie_parse ie;
566 struct wpa_ptk *ptk;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700567 int res;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800568 u8 *kde, *kde_buf = NULL;
569 size_t kde_len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700570
571 if (wpa_sm_get_network_ctx(sm) == NULL) {
572 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, "WPA: No SSID info "
573 "found (msg 1 of 4)");
574 return;
575 }
576
577 wpa_sm_set_state(sm, WPA_4WAY_HANDSHAKE);
Roshan Pius5e7db942018-04-12 12:27:41 -0700578 wpa_dbg(sm->ctx->msg_ctx, MSG_INFO, "WPA: RX message 1 of 4-Way "
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700579 "Handshake from " MACSTR " (ver=%d)", MAC2STR(src_addr), ver);
580
581 os_memset(&ie, 0, sizeof(ie));
582
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800583 if (sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700584 /* RSN: msg 1/4 should contain PMKID for the selected PMK */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700585 wpa_hexdump(MSG_DEBUG, "RSN: msg 1/4 key data",
586 key_data, key_data_len);
587 if (wpa_supplicant_parse_ies(key_data, key_data_len, &ie) < 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800588 goto failed;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700589 if (ie.pmkid) {
590 wpa_hexdump(MSG_DEBUG, "RSN: PMKID from "
591 "Authenticator", ie.pmkid, PMKID_LEN);
592 }
593 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700594
595 res = wpa_supplicant_get_pmk(sm, src_addr, ie.pmkid);
596 if (res == -2) {
597 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "RSN: Do not reply to "
598 "msg 1/4 - requesting full EAP authentication");
599 return;
600 }
601 if (res)
602 goto failed;
603
604 if (sm->renew_snonce) {
605 if (random_get_bytes(sm->snonce, WPA_NONCE_LEN)) {
606 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
607 "WPA: Failed to get random data for SNonce");
608 goto failed;
609 }
610 sm->renew_snonce = 0;
611 wpa_hexdump(MSG_DEBUG, "WPA: Renewed SNonce",
612 sm->snonce, WPA_NONCE_LEN);
613 }
614
615 /* Calculate PTK which will be stored as a temporary PTK until it has
616 * been verified when processing message 3/4. */
617 ptk = &sm->tptk;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700618 if (wpa_derive_ptk(sm, src_addr, key, ptk) < 0)
619 goto failed;
Dmitry Shmidt98660862014-03-11 17:26:21 -0700620 if (sm->pairwise_cipher == WPA_CIPHER_TKIP) {
Dmitry Shmidt61593f02014-04-21 16:27:35 -0700621 u8 buf[8];
Dmitry Shmidt98660862014-03-11 17:26:21 -0700622 /* Supplicant: swap tx/rx Mic keys */
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800623 os_memcpy(buf, &ptk->tk[16], 8);
624 os_memcpy(&ptk->tk[16], &ptk->tk[24], 8);
625 os_memcpy(&ptk->tk[24], buf, 8);
Dmitry Shmidt61593f02014-04-21 16:27:35 -0700626 os_memset(buf, 0, sizeof(buf));
Dmitry Shmidt98660862014-03-11 17:26:21 -0700627 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700628 sm->tptk_set = 1;
629
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800630 kde = sm->assoc_wpa_ie;
631 kde_len = sm->assoc_wpa_ie_len;
632
Hai Shalom74f70d42019-02-11 14:42:39 -0800633#ifdef CONFIG_OCV
634 if (wpa_sm_ocv_enabled(sm)) {
635 struct wpa_channel_info ci;
636 u8 *pos;
637
638 if (wpa_sm_channel_info(sm, &ci) != 0) {
639 wpa_printf(MSG_WARNING,
640 "Failed to get channel info for OCI element in EAPOL-Key 2/4");
641 goto failed;
642 }
643
644 kde_buf = os_malloc(kde_len + 2 + RSN_SELECTOR_LEN + 3);
645 if (!kde_buf) {
646 wpa_printf(MSG_WARNING,
647 "Failed to allocate memory for KDE with OCI in EAPOL-Key 2/4");
648 goto failed;
649 }
650
651 os_memcpy(kde_buf, kde, kde_len);
652 kde = kde_buf;
653 pos = kde + kde_len;
654 if (ocv_insert_oci_kde(&ci, &pos) < 0)
655 goto failed;
656 kde_len = pos - kde;
657 }
658#endif /* CONFIG_OCV */
659
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800660#ifdef CONFIG_P2P
661 if (sm->p2p) {
662 kde_buf = os_malloc(kde_len + 2 + RSN_SELECTOR_LEN + 1);
663 if (kde_buf) {
664 u8 *pos;
665 wpa_printf(MSG_DEBUG, "P2P: Add IP Address Request KDE "
666 "into EAPOL-Key 2/4");
667 os_memcpy(kde_buf, kde, kde_len);
668 kde = kde_buf;
669 pos = kde + kde_len;
670 *pos++ = WLAN_EID_VENDOR_SPECIFIC;
671 *pos++ = RSN_SELECTOR_LEN + 1;
672 RSN_SELECTOR_PUT(pos, WFA_KEY_DATA_IP_ADDR_REQ);
673 pos += RSN_SELECTOR_LEN;
674 *pos++ = 0x01;
675 kde_len = pos - kde;
676 }
677 }
678#endif /* CONFIG_P2P */
679
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700680 if (wpa_supplicant_send_2_of_4(sm, sm->bssid, key, ver, sm->snonce,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800681 kde, kde_len, ptk) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700682 goto failed;
683
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800684 os_free(kde_buf);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700685 os_memcpy(sm->anonce, key->key_nonce, WPA_NONCE_LEN);
686 return;
687
688failed:
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800689 os_free(kde_buf);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700690 wpa_sm_deauthenticate(sm, WLAN_REASON_UNSPECIFIED);
691}
692
693
694static void wpa_sm_start_preauth(void *eloop_ctx, void *timeout_ctx)
695{
696 struct wpa_sm *sm = eloop_ctx;
697 rsn_preauth_candidate_process(sm);
698}
699
700
701static void wpa_supplicant_key_neg_complete(struct wpa_sm *sm,
702 const u8 *addr, int secure)
703{
704 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
705 "WPA: Key negotiation completed with "
706 MACSTR " [PTK=%s GTK=%s]", MAC2STR(addr),
707 wpa_cipher_txt(sm->pairwise_cipher),
708 wpa_cipher_txt(sm->group_cipher));
709 wpa_sm_cancel_auth_timeout(sm);
710 wpa_sm_set_state(sm, WPA_COMPLETED);
711
712 if (secure) {
713 wpa_sm_mlme_setprotection(
714 sm, addr, MLME_SETPROTECTION_PROTECT_TYPE_RX_TX,
715 MLME_SETPROTECTION_KEY_TYPE_PAIRWISE);
716 eapol_sm_notify_portValid(sm->eapol, TRUE);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700717 if (wpa_key_mgmt_wpa_psk(sm->key_mgmt) ||
718 sm->key_mgmt == WPA_KEY_MGMT_DPP ||
719 sm->key_mgmt == WPA_KEY_MGMT_OWE)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700720 eapol_sm_notify_eap_success(sm->eapol, TRUE);
721 /*
722 * Start preauthentication after a short wait to avoid a
723 * possible race condition between the data receive and key
724 * configuration after the 4-Way Handshake. This increases the
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800725 * likelihood of the first preauth EAPOL-Start frame getting to
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700726 * the target AP.
727 */
Hai Shalom74f70d42019-02-11 14:42:39 -0800728 if (!dl_list_empty(&sm->pmksa_candidates))
729 eloop_register_timeout(1, 0, wpa_sm_start_preauth,
730 sm, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700731 }
732
733 if (sm->cur_pmksa && sm->cur_pmksa->opportunistic) {
734 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
735 "RSN: Authenticator accepted "
736 "opportunistic PMKSA entry - marking it valid");
737 sm->cur_pmksa->opportunistic = 0;
738 }
739
740#ifdef CONFIG_IEEE80211R
741 if (wpa_key_mgmt_ft(sm->key_mgmt)) {
742 /* Prepare for the next transition */
743 wpa_ft_prepare_auth_request(sm, NULL);
744 }
745#endif /* CONFIG_IEEE80211R */
746}
747
748
749static void wpa_sm_rekey_ptk(void *eloop_ctx, void *timeout_ctx)
750{
751 struct wpa_sm *sm = eloop_ctx;
752 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Request PTK rekeying");
753 wpa_sm_key_request(sm, 0, 1);
754}
755
756
757static int wpa_supplicant_install_ptk(struct wpa_sm *sm,
758 const struct wpa_eapol_key *key)
759{
760 int keylen, rsclen;
761 enum wpa_alg alg;
762 const u8 *key_rsc;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800763
Mathy Vanhoefc66556c2017-09-29 04:22:51 +0200764 if (sm->ptk.installed) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800765 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
766 "WPA: Do not re-install same PTK to the driver");
767 return 0;
768 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700769
770 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
771 "WPA: Installing PTK to the driver");
772
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700773 if (sm->pairwise_cipher == WPA_CIPHER_NONE) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700774 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Pairwise Cipher "
775 "Suite: NONE - do not use pairwise keys");
776 return 0;
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700777 }
778
779 if (!wpa_cipher_valid_pairwise(sm->pairwise_cipher)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700780 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
781 "WPA: Unsupported pairwise cipher %d",
782 sm->pairwise_cipher);
783 return -1;
784 }
785
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700786 alg = wpa_cipher_to_alg(sm->pairwise_cipher);
787 keylen = wpa_cipher_key_len(sm->pairwise_cipher);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700788 if (keylen <= 0 || (unsigned int) keylen != sm->ptk.tk_len) {
789 wpa_printf(MSG_DEBUG, "WPA: TK length mismatch: %d != %lu",
790 keylen, (long unsigned int) sm->ptk.tk_len);
791 return -1;
792 }
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700793 rsclen = wpa_cipher_rsc_len(sm->pairwise_cipher);
794
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800795 if (sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700796 key_rsc = null_rsc;
797 } else {
798 key_rsc = key->key_rsc;
799 wpa_hexdump(MSG_DEBUG, "WPA: RSC", key_rsc, rsclen);
800 }
801
802 if (wpa_sm_set_key(sm, alg, sm->bssid, 0, 1, key_rsc, rsclen,
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800803 sm->ptk.tk, keylen) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700804 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
805 "WPA: Failed to set PTK to the "
806 "driver (alg=%d keylen=%d bssid=" MACSTR ")",
807 alg, keylen, MAC2STR(sm->bssid));
808 return -1;
809 }
810
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800811 /* TK is not needed anymore in supplicant */
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800812 os_memset(sm->ptk.tk, 0, WPA_TK_MAX_LEN);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700813 sm->ptk.tk_len = 0;
Mathy Vanhoefc66556c2017-09-29 04:22:51 +0200814 sm->ptk.installed = 1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800815
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700816 if (sm->wpa_ptk_rekey) {
817 eloop_cancel_timeout(wpa_sm_rekey_ptk, sm, NULL);
818 eloop_register_timeout(sm->wpa_ptk_rekey, 0, wpa_sm_rekey_ptk,
819 sm, NULL);
820 }
821
822 return 0;
823}
824
825
826static int wpa_supplicant_check_group_cipher(struct wpa_sm *sm,
827 int group_cipher,
828 int keylen, int maxkeylen,
829 int *key_rsc_len,
830 enum wpa_alg *alg)
831{
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700832 int klen;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700833
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700834 *alg = wpa_cipher_to_alg(group_cipher);
835 if (*alg == WPA_ALG_NONE) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700836 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
837 "WPA: Unsupported Group Cipher %d",
838 group_cipher);
839 return -1;
840 }
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700841 *key_rsc_len = wpa_cipher_rsc_len(group_cipher);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700842
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700843 klen = wpa_cipher_key_len(group_cipher);
844 if (keylen != klen || maxkeylen < klen) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700845 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
846 "WPA: Unsupported %s Group Cipher key length %d (%d)",
847 wpa_cipher_txt(group_cipher), keylen, maxkeylen);
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700848 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700849 }
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700850 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700851}
852
853
854struct wpa_gtk_data {
855 enum wpa_alg alg;
856 int tx, key_rsc_len, keyidx;
857 u8 gtk[32];
858 int gtk_len;
859};
860
861
862static int wpa_supplicant_install_gtk(struct wpa_sm *sm,
863 const struct wpa_gtk_data *gd,
Jouni Malinen58c0e962017-10-01 12:12:24 +0300864 const u8 *key_rsc, int wnm_sleep)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700865{
866 const u8 *_gtk = gd->gtk;
867 u8 gtk_buf[32];
868
Mathy Vanhoef10bfd642017-07-12 16:03:24 +0200869 /* Detect possible key reinstallation */
Jouni Malinen58c0e962017-10-01 12:12:24 +0300870 if ((sm->gtk.gtk_len == (size_t) gd->gtk_len &&
871 os_memcmp(sm->gtk.gtk, gd->gtk, sm->gtk.gtk_len) == 0) ||
872 (sm->gtk_wnm_sleep.gtk_len == (size_t) gd->gtk_len &&
873 os_memcmp(sm->gtk_wnm_sleep.gtk, gd->gtk,
874 sm->gtk_wnm_sleep.gtk_len) == 0)) {
Mathy Vanhoef10bfd642017-07-12 16:03:24 +0200875 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
876 "WPA: Not reinstalling already in-use GTK to the driver (keyidx=%d tx=%d len=%d)",
877 gd->keyidx, gd->tx, gd->gtk_len);
878 return 0;
879 }
880
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700881 wpa_hexdump_key(MSG_DEBUG, "WPA: Group Key", gd->gtk, gd->gtk_len);
882 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
883 "WPA: Installing GTK to the driver (keyidx=%d tx=%d len=%d)",
884 gd->keyidx, gd->tx, gd->gtk_len);
885 wpa_hexdump(MSG_DEBUG, "WPA: RSC", key_rsc, gd->key_rsc_len);
886 if (sm->group_cipher == WPA_CIPHER_TKIP) {
887 /* Swap Tx/Rx keys for Michael MIC */
888 os_memcpy(gtk_buf, gd->gtk, 16);
889 os_memcpy(gtk_buf + 16, gd->gtk + 24, 8);
890 os_memcpy(gtk_buf + 24, gd->gtk + 16, 8);
891 _gtk = gtk_buf;
892 }
893 if (sm->pairwise_cipher == WPA_CIPHER_NONE) {
894 if (wpa_sm_set_key(sm, gd->alg, NULL,
895 gd->keyidx, 1, key_rsc, gd->key_rsc_len,
896 _gtk, gd->gtk_len) < 0) {
897 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
898 "WPA: Failed to set GTK to the driver "
899 "(Group only)");
Dmitry Shmidt61593f02014-04-21 16:27:35 -0700900 os_memset(gtk_buf, 0, sizeof(gtk_buf));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700901 return -1;
902 }
903 } else if (wpa_sm_set_key(sm, gd->alg, broadcast_ether_addr,
904 gd->keyidx, gd->tx, key_rsc, gd->key_rsc_len,
905 _gtk, gd->gtk_len) < 0) {
906 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
907 "WPA: Failed to set GTK to "
908 "the driver (alg=%d keylen=%d keyidx=%d)",
909 gd->alg, gd->gtk_len, gd->keyidx);
Dmitry Shmidt61593f02014-04-21 16:27:35 -0700910 os_memset(gtk_buf, 0, sizeof(gtk_buf));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700911 return -1;
912 }
Dmitry Shmidt61593f02014-04-21 16:27:35 -0700913 os_memset(gtk_buf, 0, sizeof(gtk_buf));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700914
Jouni Malinen58c0e962017-10-01 12:12:24 +0300915 if (wnm_sleep) {
916 sm->gtk_wnm_sleep.gtk_len = gd->gtk_len;
917 os_memcpy(sm->gtk_wnm_sleep.gtk, gd->gtk,
918 sm->gtk_wnm_sleep.gtk_len);
919 } else {
920 sm->gtk.gtk_len = gd->gtk_len;
921 os_memcpy(sm->gtk.gtk, gd->gtk, sm->gtk.gtk_len);
922 }
Mathy Vanhoef10bfd642017-07-12 16:03:24 +0200923
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700924 return 0;
925}
926
927
928static int wpa_supplicant_gtk_tx_bit_workaround(const struct wpa_sm *sm,
929 int tx)
930{
931 if (tx && sm->pairwise_cipher != WPA_CIPHER_NONE) {
932 /* Ignore Tx bit for GTK if a pairwise key is used. One AP
933 * seemed to set this bit (incorrectly, since Tx is only when
934 * doing Group Key only APs) and without this workaround, the
935 * data connection does not work because wpa_supplicant
936 * configured non-zero keyidx to be used for unicast. */
937 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
938 "WPA: Tx bit set for GTK, but pairwise "
939 "keys are used - ignore Tx bit");
940 return 0;
941 }
942 return tx;
943}
944
945
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800946static int wpa_supplicant_rsc_relaxation(const struct wpa_sm *sm,
947 const u8 *rsc)
948{
949 int rsclen;
950
951 if (!sm->wpa_rsc_relaxation)
952 return 0;
953
954 rsclen = wpa_cipher_rsc_len(sm->group_cipher);
955
956 /*
957 * Try to detect RSC (endian) corruption issue where the AP sends
958 * the RSC bytes in EAPOL-Key message in the wrong order, both if
959 * it's actually a 6-byte field (as it should be) and if it treats
960 * it as an 8-byte field.
961 * An AP model known to have this bug is the Sapido RB-1632.
962 */
963 if (rsclen == 6 && ((rsc[5] && !rsc[0]) || rsc[6] || rsc[7])) {
964 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
965 "RSC %02x%02x%02x%02x%02x%02x%02x%02x is likely bogus, using 0",
966 rsc[0], rsc[1], rsc[2], rsc[3],
967 rsc[4], rsc[5], rsc[6], rsc[7]);
968
969 return 1;
970 }
971
972 return 0;
973}
974
975
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700976static int wpa_supplicant_pairwise_gtk(struct wpa_sm *sm,
977 const struct wpa_eapol_key *key,
978 const u8 *gtk, size_t gtk_len,
979 int key_info)
980{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700981 struct wpa_gtk_data gd;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800982 const u8 *key_rsc;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700983
984 /*
985 * IEEE Std 802.11i-2004 - 8.5.2 EAPOL-Key frames - Figure 43x
986 * GTK KDE format:
987 * KeyID[bits 0-1], Tx [bit 2], Reserved [bits 3-7]
988 * Reserved [bits 0-7]
989 * GTK
990 */
991
992 os_memset(&gd, 0, sizeof(gd));
993 wpa_hexdump_key(MSG_DEBUG, "RSN: received GTK in pairwise handshake",
994 gtk, gtk_len);
995
996 if (gtk_len < 2 || gtk_len - 2 > sizeof(gd.gtk))
997 return -1;
998
999 gd.keyidx = gtk[0] & 0x3;
1000 gd.tx = wpa_supplicant_gtk_tx_bit_workaround(sm,
1001 !!(gtk[0] & BIT(2)));
1002 gtk += 2;
1003 gtk_len -= 2;
1004
1005 os_memcpy(gd.gtk, gtk, gtk_len);
1006 gd.gtk_len = gtk_len;
1007
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001008 key_rsc = key->key_rsc;
1009 if (wpa_supplicant_rsc_relaxation(sm, key->key_rsc))
1010 key_rsc = null_rsc;
1011
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001012 if (sm->group_cipher != WPA_CIPHER_GTK_NOT_USED &&
1013 (wpa_supplicant_check_group_cipher(sm, sm->group_cipher,
1014 gtk_len, gtk_len,
1015 &gd.key_rsc_len, &gd.alg) ||
Jouni Malinen58c0e962017-10-01 12:12:24 +03001016 wpa_supplicant_install_gtk(sm, &gd, key_rsc, 0))) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001017 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1018 "RSN: Failed to install GTK");
Dmitry Shmidt61593f02014-04-21 16:27:35 -07001019 os_memset(&gd, 0, sizeof(gd));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001020 return -1;
1021 }
Dmitry Shmidt61593f02014-04-21 16:27:35 -07001022 os_memset(&gd, 0, sizeof(gd));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001023
1024 wpa_supplicant_key_neg_complete(sm, sm->bssid,
1025 key_info & WPA_KEY_INFO_SECURE);
1026 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001027}
1028
1029
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02001030#ifdef CONFIG_IEEE80211W
1031static int wpa_supplicant_install_igtk(struct wpa_sm *sm,
Jouni Malinen58c0e962017-10-01 12:12:24 +03001032 const struct wpa_igtk_kde *igtk,
1033 int wnm_sleep)
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02001034{
1035 size_t len = wpa_cipher_key_len(sm->mgmt_group_cipher);
1036 u16 keyidx = WPA_GET_LE16(igtk->keyid);
1037
1038 /* Detect possible key reinstallation */
Jouni Malinen58c0e962017-10-01 12:12:24 +03001039 if ((sm->igtk.igtk_len == len &&
1040 os_memcmp(sm->igtk.igtk, igtk->igtk, sm->igtk.igtk_len) == 0) ||
1041 (sm->igtk_wnm_sleep.igtk_len == len &&
1042 os_memcmp(sm->igtk_wnm_sleep.igtk, igtk->igtk,
1043 sm->igtk_wnm_sleep.igtk_len) == 0)) {
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02001044 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1045 "WPA: Not reinstalling already in-use IGTK to the driver (keyidx=%d)",
1046 keyidx);
1047 return 0;
1048 }
1049
1050 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
Hai Shalom39ba6fc2019-01-22 12:40:38 -08001051 "WPA: IGTK keyid %d pn " COMPACT_MACSTR,
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02001052 keyidx, MAC2STR(igtk->pn));
1053 wpa_hexdump_key(MSG_DEBUG, "WPA: IGTK", igtk->igtk, len);
1054 if (keyidx > 4095) {
1055 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1056 "WPA: Invalid IGTK KeyID %d", keyidx);
1057 return -1;
1058 }
1059 if (wpa_sm_set_key(sm, wpa_cipher_to_alg(sm->mgmt_group_cipher),
1060 broadcast_ether_addr,
1061 keyidx, 0, igtk->pn, sizeof(igtk->pn),
1062 igtk->igtk, len) < 0) {
1063 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1064 "WPA: Failed to configure IGTK to the driver");
1065 return -1;
1066 }
1067
Jouni Malinen58c0e962017-10-01 12:12:24 +03001068 if (wnm_sleep) {
1069 sm->igtk_wnm_sleep.igtk_len = len;
1070 os_memcpy(sm->igtk_wnm_sleep.igtk, igtk->igtk,
1071 sm->igtk_wnm_sleep.igtk_len);
1072 } else {
1073 sm->igtk.igtk_len = len;
1074 os_memcpy(sm->igtk.igtk, igtk->igtk, sm->igtk.igtk_len);
1075 }
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02001076
1077 return 0;
1078}
1079#endif /* CONFIG_IEEE80211W */
1080
1081
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001082static int ieee80211w_set_keys(struct wpa_sm *sm,
1083 struct wpa_eapol_ie_parse *ie)
1084{
1085#ifdef CONFIG_IEEE80211W
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001086 if (!wpa_cipher_valid_mgmt_group(sm->mgmt_group_cipher))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001087 return 0;
1088
1089 if (ie->igtk) {
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001090 size_t len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001091 const struct wpa_igtk_kde *igtk;
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02001092
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001093 len = wpa_cipher_key_len(sm->mgmt_group_cipher);
1094 if (ie->igtk_len != WPA_IGTK_KDE_PREFIX_LEN + len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001095 return -1;
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02001096
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001097 igtk = (const struct wpa_igtk_kde *) ie->igtk;
Jouni Malinen58c0e962017-10-01 12:12:24 +03001098 if (wpa_supplicant_install_igtk(sm, igtk, 0) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001099 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001100 }
1101
1102 return 0;
1103#else /* CONFIG_IEEE80211W */
1104 return 0;
1105#endif /* CONFIG_IEEE80211W */
1106}
1107
1108
1109static void wpa_report_ie_mismatch(struct wpa_sm *sm,
1110 const char *reason, const u8 *src_addr,
1111 const u8 *wpa_ie, size_t wpa_ie_len,
1112 const u8 *rsn_ie, size_t rsn_ie_len)
1113{
1114 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, "WPA: %s (src=" MACSTR ")",
1115 reason, MAC2STR(src_addr));
1116
1117 if (sm->ap_wpa_ie) {
1118 wpa_hexdump(MSG_INFO, "WPA: WPA IE in Beacon/ProbeResp",
1119 sm->ap_wpa_ie, sm->ap_wpa_ie_len);
1120 }
1121 if (wpa_ie) {
1122 if (!sm->ap_wpa_ie) {
1123 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1124 "WPA: No WPA IE in Beacon/ProbeResp");
1125 }
1126 wpa_hexdump(MSG_INFO, "WPA: WPA IE in 3/4 msg",
1127 wpa_ie, wpa_ie_len);
1128 }
1129
1130 if (sm->ap_rsn_ie) {
1131 wpa_hexdump(MSG_INFO, "WPA: RSN IE in Beacon/ProbeResp",
1132 sm->ap_rsn_ie, sm->ap_rsn_ie_len);
1133 }
1134 if (rsn_ie) {
1135 if (!sm->ap_rsn_ie) {
1136 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1137 "WPA: No RSN IE in Beacon/ProbeResp");
1138 }
1139 wpa_hexdump(MSG_INFO, "WPA: RSN IE in 3/4 msg",
1140 rsn_ie, rsn_ie_len);
1141 }
1142
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001143 wpa_sm_deauthenticate(sm, WLAN_REASON_IE_IN_4WAY_DIFFERS);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001144}
1145
1146
1147#ifdef CONFIG_IEEE80211R
1148
1149static int ft_validate_mdie(struct wpa_sm *sm,
1150 const unsigned char *src_addr,
1151 struct wpa_eapol_ie_parse *ie,
1152 const u8 *assoc_resp_mdie)
1153{
1154 struct rsn_mdie *mdie;
1155
1156 mdie = (struct rsn_mdie *) (ie->mdie + 2);
1157 if (ie->mdie == NULL || ie->mdie_len < 2 + sizeof(*mdie) ||
1158 os_memcmp(mdie->mobility_domain, sm->mobility_domain,
1159 MOBILITY_DOMAIN_ID_LEN) != 0) {
1160 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "FT: MDIE in msg 3/4 did "
1161 "not match with the current mobility domain");
1162 return -1;
1163 }
1164
1165 if (assoc_resp_mdie &&
1166 (assoc_resp_mdie[1] != ie->mdie[1] ||
1167 os_memcmp(assoc_resp_mdie, ie->mdie, 2 + ie->mdie[1]) != 0)) {
1168 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "FT: MDIE mismatch");
1169 wpa_hexdump(MSG_DEBUG, "FT: MDIE in EAPOL-Key msg 3/4",
1170 ie->mdie, 2 + ie->mdie[1]);
1171 wpa_hexdump(MSG_DEBUG, "FT: MDIE in (Re)Association Response",
1172 assoc_resp_mdie, 2 + assoc_resp_mdie[1]);
1173 return -1;
1174 }
1175
1176 return 0;
1177}
1178
1179
1180static int ft_validate_ftie(struct wpa_sm *sm,
1181 const unsigned char *src_addr,
1182 struct wpa_eapol_ie_parse *ie,
1183 const u8 *assoc_resp_ftie)
1184{
1185 if (ie->ftie == NULL) {
1186 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1187 "FT: No FTIE in EAPOL-Key msg 3/4");
1188 return -1;
1189 }
1190
1191 if (assoc_resp_ftie == NULL)
1192 return 0;
1193
1194 if (assoc_resp_ftie[1] != ie->ftie[1] ||
1195 os_memcmp(assoc_resp_ftie, ie->ftie, 2 + ie->ftie[1]) != 0) {
1196 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "FT: FTIE mismatch");
1197 wpa_hexdump(MSG_DEBUG, "FT: FTIE in EAPOL-Key msg 3/4",
1198 ie->ftie, 2 + ie->ftie[1]);
1199 wpa_hexdump(MSG_DEBUG, "FT: FTIE in (Re)Association Response",
1200 assoc_resp_ftie, 2 + assoc_resp_ftie[1]);
1201 return -1;
1202 }
1203
1204 return 0;
1205}
1206
1207
1208static int ft_validate_rsnie(struct wpa_sm *sm,
1209 const unsigned char *src_addr,
1210 struct wpa_eapol_ie_parse *ie)
1211{
1212 struct wpa_ie_data rsn;
1213
1214 if (!ie->rsn_ie)
1215 return 0;
1216
1217 /*
1218 * Verify that PMKR1Name from EAPOL-Key message 3/4
1219 * matches with the value we derived.
1220 */
1221 if (wpa_parse_wpa_ie_rsn(ie->rsn_ie, ie->rsn_ie_len, &rsn) < 0 ||
1222 rsn.num_pmkid != 1 || rsn.pmkid == NULL) {
1223 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "FT: No PMKR1Name in "
1224 "FT 4-way handshake message 3/4");
1225 return -1;
1226 }
1227
Dmitry Shmidtc2817022014-07-02 10:32:10 -07001228 if (os_memcmp_const(rsn.pmkid, sm->pmk_r1_name, WPA_PMK_NAME_LEN) != 0)
1229 {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001230 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1231 "FT: PMKR1Name mismatch in "
1232 "FT 4-way handshake message 3/4");
1233 wpa_hexdump(MSG_DEBUG, "FT: PMKR1Name from Authenticator",
1234 rsn.pmkid, WPA_PMK_NAME_LEN);
1235 wpa_hexdump(MSG_DEBUG, "FT: Derived PMKR1Name",
1236 sm->pmk_r1_name, WPA_PMK_NAME_LEN);
1237 return -1;
1238 }
1239
1240 return 0;
1241}
1242
1243
1244static int wpa_supplicant_validate_ie_ft(struct wpa_sm *sm,
1245 const unsigned char *src_addr,
1246 struct wpa_eapol_ie_parse *ie)
1247{
1248 const u8 *pos, *end, *mdie = NULL, *ftie = NULL;
1249
1250 if (sm->assoc_resp_ies) {
1251 pos = sm->assoc_resp_ies;
1252 end = pos + sm->assoc_resp_ies_len;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001253 while (end - pos > 2) {
1254 if (2 + pos[1] > end - pos)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001255 break;
1256 switch (*pos) {
1257 case WLAN_EID_MOBILITY_DOMAIN:
1258 mdie = pos;
1259 break;
1260 case WLAN_EID_FAST_BSS_TRANSITION:
1261 ftie = pos;
1262 break;
1263 }
1264 pos += 2 + pos[1];
1265 }
1266 }
1267
1268 if (ft_validate_mdie(sm, src_addr, ie, mdie) < 0 ||
1269 ft_validate_ftie(sm, src_addr, ie, ftie) < 0 ||
1270 ft_validate_rsnie(sm, src_addr, ie) < 0)
1271 return -1;
1272
1273 return 0;
1274}
1275
1276#endif /* CONFIG_IEEE80211R */
1277
1278
1279static int wpa_supplicant_validate_ie(struct wpa_sm *sm,
1280 const unsigned char *src_addr,
1281 struct wpa_eapol_ie_parse *ie)
1282{
1283 if (sm->ap_wpa_ie == NULL && sm->ap_rsn_ie == NULL) {
1284 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1285 "WPA: No WPA/RSN IE for this AP known. "
1286 "Trying to get from scan results");
1287 if (wpa_sm_get_beacon_ie(sm) < 0) {
1288 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1289 "WPA: Could not find AP from "
1290 "the scan results");
1291 } else {
1292 wpa_msg(sm->ctx->msg_ctx, MSG_DEBUG,
1293 "WPA: Found the current AP from "
1294 "updated scan results");
1295 }
1296 }
1297
1298 if (ie->wpa_ie == NULL && ie->rsn_ie == NULL &&
1299 (sm->ap_wpa_ie || sm->ap_rsn_ie)) {
1300 wpa_report_ie_mismatch(sm, "IE in 3/4 msg does not match "
1301 "with IE in Beacon/ProbeResp (no IE?)",
1302 src_addr, ie->wpa_ie, ie->wpa_ie_len,
1303 ie->rsn_ie, ie->rsn_ie_len);
1304 return -1;
1305 }
1306
1307 if ((ie->wpa_ie && sm->ap_wpa_ie &&
1308 (ie->wpa_ie_len != sm->ap_wpa_ie_len ||
1309 os_memcmp(ie->wpa_ie, sm->ap_wpa_ie, ie->wpa_ie_len) != 0)) ||
1310 (ie->rsn_ie && sm->ap_rsn_ie &&
1311 wpa_compare_rsn_ie(wpa_key_mgmt_ft(sm->key_mgmt),
1312 sm->ap_rsn_ie, sm->ap_rsn_ie_len,
1313 ie->rsn_ie, ie->rsn_ie_len))) {
1314 wpa_report_ie_mismatch(sm, "IE in 3/4 msg does not match "
1315 "with IE in Beacon/ProbeResp",
1316 src_addr, ie->wpa_ie, ie->wpa_ie_len,
1317 ie->rsn_ie, ie->rsn_ie_len);
1318 return -1;
1319 }
1320
1321 if (sm->proto == WPA_PROTO_WPA &&
1322 ie->rsn_ie && sm->ap_rsn_ie == NULL && sm->rsn_enabled) {
1323 wpa_report_ie_mismatch(sm, "Possible downgrade attack "
1324 "detected - RSN was enabled and RSN IE "
1325 "was in msg 3/4, but not in "
1326 "Beacon/ProbeResp",
1327 src_addr, ie->wpa_ie, ie->wpa_ie_len,
1328 ie->rsn_ie, ie->rsn_ie_len);
1329 return -1;
1330 }
1331
1332#ifdef CONFIG_IEEE80211R
1333 if (wpa_key_mgmt_ft(sm->key_mgmt) &&
1334 wpa_supplicant_validate_ie_ft(sm, src_addr, ie) < 0)
1335 return -1;
1336#endif /* CONFIG_IEEE80211R */
1337
1338 return 0;
1339}
1340
1341
1342/**
1343 * wpa_supplicant_send_4_of_4 - Send message 4 of WPA/RSN 4-Way Handshake
1344 * @sm: Pointer to WPA state machine data from wpa_sm_init()
1345 * @dst: Destination address for the frame
1346 * @key: Pointer to the EAPOL-Key frame header
1347 * @ver: Version bits from EAPOL-Key Key Info
1348 * @key_info: Key Info
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001349 * @ptk: PTK to use for keyed hash and encryption
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001350 * Returns: >= 0 on success, < 0 on failure
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001351 */
1352int wpa_supplicant_send_4_of_4(struct wpa_sm *sm, const unsigned char *dst,
1353 const struct wpa_eapol_key *key,
1354 u16 ver, u16 key_info,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001355 struct wpa_ptk *ptk)
1356{
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001357 size_t mic_len, hdrlen, rlen;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001358 struct wpa_eapol_key *reply;
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001359 u8 *rbuf, *key_mic;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001360
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001361 mic_len = wpa_mic_len(sm->key_mgmt, sm->pmk_len);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001362 hdrlen = sizeof(*reply) + mic_len + 2;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001363 rbuf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_KEY, NULL,
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001364 hdrlen, &rlen, (void *) &reply);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001365 if (rbuf == NULL)
1366 return -1;
1367
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001368 reply->type = (sm->proto == WPA_PROTO_RSN ||
1369 sm->proto == WPA_PROTO_OSEN) ?
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001370 EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA;
1371 key_info &= WPA_KEY_INFO_SECURE;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001372 key_info |= ver | WPA_KEY_INFO_KEY_TYPE;
1373 if (mic_len)
1374 key_info |= WPA_KEY_INFO_MIC;
1375 else
1376 key_info |= WPA_KEY_INFO_ENCR_KEY_DATA;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001377 WPA_PUT_BE16(reply->key_info, key_info);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001378 if (sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001379 WPA_PUT_BE16(reply->key_length, 0);
1380 else
1381 os_memcpy(reply->key_length, key->key_length, 2);
1382 os_memcpy(reply->replay_counter, key->replay_counter,
1383 WPA_REPLAY_COUNTER_LEN);
1384
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001385 key_mic = (u8 *) (reply + 1);
1386 WPA_PUT_BE16(key_mic + mic_len, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001387
Roshan Pius5e7db942018-04-12 12:27:41 -07001388 wpa_dbg(sm->ctx->msg_ctx, MSG_INFO, "WPA: Sending EAPOL-Key 4/4");
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001389 return wpa_eapol_key_send(sm, ptk, ver, dst, ETH_P_EAPOL, rbuf, rlen,
1390 key_mic);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001391}
1392
1393
1394static void wpa_supplicant_process_3_of_4(struct wpa_sm *sm,
1395 const struct wpa_eapol_key *key,
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001396 u16 ver, const u8 *key_data,
1397 size_t key_data_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001398{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001399 u16 key_info, keylen;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001400 struct wpa_eapol_ie_parse ie;
1401
1402 wpa_sm_set_state(sm, WPA_4WAY_HANDSHAKE);
Roshan Pius5e7db942018-04-12 12:27:41 -07001403 wpa_dbg(sm->ctx->msg_ctx, MSG_INFO, "WPA: RX message 3 of 4-Way "
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001404 "Handshake from " MACSTR " (ver=%d)", MAC2STR(sm->bssid), ver);
1405
1406 key_info = WPA_GET_BE16(key->key_info);
1407
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001408 wpa_hexdump(MSG_DEBUG, "WPA: IE KeyData", key_data, key_data_len);
1409 if (wpa_supplicant_parse_ies(key_data, key_data_len, &ie) < 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001410 goto failed;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001411 if (ie.gtk && !(key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
1412 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1413 "WPA: GTK IE in unencrypted key data");
1414 goto failed;
1415 }
1416#ifdef CONFIG_IEEE80211W
1417 if (ie.igtk && !(key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
1418 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1419 "WPA: IGTK KDE in unencrypted key data");
1420 goto failed;
1421 }
1422
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001423 if (ie.igtk &&
1424 wpa_cipher_valid_mgmt_group(sm->mgmt_group_cipher) &&
1425 ie.igtk_len != WPA_IGTK_KDE_PREFIX_LEN +
1426 (unsigned int) wpa_cipher_key_len(sm->mgmt_group_cipher)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001427 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1428 "WPA: Invalid IGTK KDE length %lu",
1429 (unsigned long) ie.igtk_len);
1430 goto failed;
1431 }
1432#endif /* CONFIG_IEEE80211W */
1433
1434 if (wpa_supplicant_validate_ie(sm, sm->bssid, &ie) < 0)
1435 goto failed;
1436
1437 if (os_memcmp(sm->anonce, key->key_nonce, WPA_NONCE_LEN) != 0) {
1438 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1439 "WPA: ANonce from message 1 of 4-Way Handshake "
1440 "differs from 3 of 4-Way Handshake - drop packet (src="
1441 MACSTR ")", MAC2STR(sm->bssid));
1442 goto failed;
1443 }
1444
1445 keylen = WPA_GET_BE16(key->key_length);
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07001446 if (keylen != wpa_cipher_key_len(sm->pairwise_cipher)) {
1447 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1448 "WPA: Invalid %s key length %d (src=" MACSTR
1449 ")", wpa_cipher_txt(sm->pairwise_cipher), keylen,
1450 MAC2STR(sm->bssid));
1451 goto failed;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001452 }
1453
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001454#ifdef CONFIG_P2P
1455 if (ie.ip_addr_alloc) {
1456 os_memcpy(sm->p2p_ip_addr, ie.ip_addr_alloc, 3 * 4);
1457 wpa_hexdump(MSG_DEBUG, "P2P: IP address info",
1458 sm->p2p_ip_addr, sizeof(sm->p2p_ip_addr));
1459 }
1460#endif /* CONFIG_P2P */
1461
Hai Shalom74f70d42019-02-11 14:42:39 -08001462#ifdef CONFIG_OCV
1463 if (wpa_sm_ocv_enabled(sm)) {
1464 struct wpa_channel_info ci;
1465
1466 if (wpa_sm_channel_info(sm, &ci) != 0) {
1467 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1468 "Failed to get channel info to validate received OCI in EAPOL-Key 3/4");
1469 return;
1470 }
1471
1472 if (ocv_verify_tx_params(ie.oci, ie.oci_len, &ci,
1473 channel_width_to_int(ci.chanwidth),
1474 ci.seg1_idx) != 0) {
1475 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, "%s",
1476 ocv_errorstr);
1477 return;
1478 }
1479 }
1480#endif /* CONFIG_OCV */
1481
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001482 if (wpa_supplicant_send_4_of_4(sm, sm->bssid, key, ver, key_info,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001483 &sm->ptk) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001484 goto failed;
1485 }
1486
1487 /* SNonce was successfully used in msg 3/4, so mark it to be renewed
1488 * for the next 4-Way Handshake. If msg 3 is received again, the old
1489 * SNonce will still be used to avoid changing PTK. */
1490 sm->renew_snonce = 1;
1491
1492 if (key_info & WPA_KEY_INFO_INSTALL) {
1493 if (wpa_supplicant_install_ptk(sm, key))
1494 goto failed;
1495 }
1496
1497 if (key_info & WPA_KEY_INFO_SECURE) {
1498 wpa_sm_mlme_setprotection(
1499 sm, sm->bssid, MLME_SETPROTECTION_PROTECT_TYPE_RX,
1500 MLME_SETPROTECTION_KEY_TYPE_PAIRWISE);
1501 eapol_sm_notify_portValid(sm->eapol, TRUE);
1502 }
1503 wpa_sm_set_state(sm, WPA_GROUP_HANDSHAKE);
1504
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001505 if (sm->group_cipher == WPA_CIPHER_GTK_NOT_USED) {
1506 wpa_supplicant_key_neg_complete(sm, sm->bssid,
1507 key_info & WPA_KEY_INFO_SECURE);
1508 } else if (ie.gtk &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001509 wpa_supplicant_pairwise_gtk(sm, key,
1510 ie.gtk, ie.gtk_len, key_info) < 0) {
1511 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1512 "RSN: Failed to configure GTK");
1513 goto failed;
1514 }
1515
1516 if (ieee80211w_set_keys(sm, &ie) < 0) {
1517 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1518 "RSN: Failed to configure IGTK");
1519 goto failed;
1520 }
1521
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001522 if (ie.gtk)
1523 wpa_sm_set_rekey_offload(sm);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001524
Hai Shalom39ba6fc2019-01-22 12:40:38 -08001525 /* Add PMKSA cache entry for Suite B AKMs here since PMKID can be
1526 * calculated only after KCK has been derived. Though, do not replace an
1527 * existing PMKSA entry after each 4-way handshake (i.e., new KCK/PMKID)
1528 * to avoid unnecessary changes of PMKID while continuing to use the
1529 * same PMK. */
1530 if (sm->proto == WPA_PROTO_RSN && wpa_key_mgmt_suite_b(sm->key_mgmt) &&
1531 !sm->cur_pmksa) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001532 struct rsn_pmksa_cache_entry *sa;
1533
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001534 sa = pmksa_cache_add(sm->pmksa, sm->pmk, sm->pmk_len, NULL,
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001535 sm->ptk.kck, sm->ptk.kck_len,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001536 sm->bssid, sm->own_addr,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001537 sm->network_ctx, sm->key_mgmt, NULL);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001538 if (!sm->cur_pmksa)
1539 sm->cur_pmksa = sa;
1540 }
1541
Dmitry Shmidt4dd28dc2015-03-10 11:21:43 -07001542 sm->msg_3_of_4_ok = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001543 return;
1544
1545failed:
1546 wpa_sm_deauthenticate(sm, WLAN_REASON_UNSPECIFIED);
1547}
1548
1549
1550static int wpa_supplicant_process_1_of_2_rsn(struct wpa_sm *sm,
1551 const u8 *keydata,
1552 size_t keydatalen,
1553 u16 key_info,
1554 struct wpa_gtk_data *gd)
1555{
1556 int maxkeylen;
1557 struct wpa_eapol_ie_parse ie;
1558
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08001559 wpa_hexdump_key(MSG_DEBUG, "RSN: msg 1/2 key data",
1560 keydata, keydatalen);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001561 if (wpa_supplicant_parse_ies(keydata, keydatalen, &ie) < 0)
1562 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001563 if (ie.gtk && !(key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
1564 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1565 "WPA: GTK IE in unencrypted key data");
1566 return -1;
1567 }
1568 if (ie.gtk == NULL) {
1569 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1570 "WPA: No GTK IE in Group Key msg 1/2");
1571 return -1;
1572 }
1573 maxkeylen = gd->gtk_len = ie.gtk_len - 2;
1574
Hai Shalom74f70d42019-02-11 14:42:39 -08001575#ifdef CONFIG_OCV
1576 if (wpa_sm_ocv_enabled(sm)) {
1577 struct wpa_channel_info ci;
1578
1579 if (wpa_sm_channel_info(sm, &ci) != 0) {
1580 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1581 "Failed to get channel info to validate received OCI in EAPOL-Key group msg 1/2");
1582 return -1;
1583 }
1584
1585 if (ocv_verify_tx_params(ie.oci, ie.oci_len, &ci,
1586 channel_width_to_int(ci.chanwidth),
1587 ci.seg1_idx) != 0) {
1588 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, "%s",
1589 ocv_errorstr);
1590 return -1;
1591 }
1592 }
1593#endif /* CONFIG_OCV */
1594
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001595 if (wpa_supplicant_check_group_cipher(sm, sm->group_cipher,
1596 gd->gtk_len, maxkeylen,
1597 &gd->key_rsc_len, &gd->alg))
1598 return -1;
1599
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001600 wpa_hexdump_key(MSG_DEBUG, "RSN: received GTK in group key handshake",
1601 ie.gtk, ie.gtk_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001602 gd->keyidx = ie.gtk[0] & 0x3;
1603 gd->tx = wpa_supplicant_gtk_tx_bit_workaround(sm,
1604 !!(ie.gtk[0] & BIT(2)));
1605 if (ie.gtk_len - 2 > sizeof(gd->gtk)) {
1606 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1607 "RSN: Too long GTK in GTK IE (len=%lu)",
1608 (unsigned long) ie.gtk_len - 2);
1609 return -1;
1610 }
1611 os_memcpy(gd->gtk, ie.gtk + 2, ie.gtk_len - 2);
1612
1613 if (ieee80211w_set_keys(sm, &ie) < 0)
1614 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1615 "RSN: Failed to configure IGTK");
1616
1617 return 0;
1618}
1619
1620
1621static int wpa_supplicant_process_1_of_2_wpa(struct wpa_sm *sm,
1622 const struct wpa_eapol_key *key,
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001623 const u8 *key_data,
1624 size_t key_data_len, u16 key_info,
1625 u16 ver, struct wpa_gtk_data *gd)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001626{
1627 size_t maxkeylen;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001628 u16 gtk_len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001629
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001630 gtk_len = WPA_GET_BE16(key->key_length);
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001631 maxkeylen = key_data_len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001632 if (ver == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
1633 if (maxkeylen < 8) {
1634 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1635 "WPA: Too short maxkeylen (%lu)",
1636 (unsigned long) maxkeylen);
1637 return -1;
1638 }
1639 maxkeylen -= 8;
1640 }
1641
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001642 if (gtk_len > maxkeylen ||
1643 wpa_supplicant_check_group_cipher(sm, sm->group_cipher,
1644 gtk_len, maxkeylen,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001645 &gd->key_rsc_len, &gd->alg))
1646 return -1;
1647
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001648 gd->gtk_len = gtk_len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001649 gd->keyidx = (key_info & WPA_KEY_INFO_KEY_INDEX_MASK) >>
1650 WPA_KEY_INFO_KEY_INDEX_SHIFT;
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001651 if (ver == WPA_KEY_INFO_TYPE_HMAC_MD5_RC4 && sm->ptk.kek_len == 16) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001652#ifdef CONFIG_NO_RC4
1653 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1654 "WPA: RC4 not supported in the build");
1655 return -1;
1656#else /* CONFIG_NO_RC4 */
Dmitry Shmidt61593f02014-04-21 16:27:35 -07001657 u8 ek[32];
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001658 if (key_data_len > sizeof(gd->gtk)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001659 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1660 "WPA: RC4 key data too long (%lu)",
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001661 (unsigned long) key_data_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001662 return -1;
1663 }
Dmitry Shmidt61593f02014-04-21 16:27:35 -07001664 os_memcpy(ek, key->key_iv, 16);
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001665 os_memcpy(ek + 16, sm->ptk.kek, sm->ptk.kek_len);
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001666 os_memcpy(gd->gtk, key_data, key_data_len);
1667 if (rc4_skip(ek, 32, 256, gd->gtk, key_data_len)) {
Dmitry Shmidt61593f02014-04-21 16:27:35 -07001668 os_memset(ek, 0, sizeof(ek));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001669 wpa_msg(sm->ctx->msg_ctx, MSG_ERROR,
1670 "WPA: RC4 failed");
1671 return -1;
1672 }
Dmitry Shmidt61593f02014-04-21 16:27:35 -07001673 os_memset(ek, 0, sizeof(ek));
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001674#endif /* CONFIG_NO_RC4 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001675 } else if (ver == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001676 if (maxkeylen % 8) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001677 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1678 "WPA: Unsupported AES-WRAP len %lu",
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001679 (unsigned long) maxkeylen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001680 return -1;
1681 }
1682 if (maxkeylen > sizeof(gd->gtk)) {
1683 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1684 "WPA: AES-WRAP key data "
1685 "too long (keydatalen=%lu maxkeylen=%lu)",
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001686 (unsigned long) key_data_len,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001687 (unsigned long) maxkeylen);
1688 return -1;
1689 }
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001690 if (aes_unwrap(sm->ptk.kek, sm->ptk.kek_len, maxkeylen / 8,
1691 key_data, gd->gtk)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001692 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1693 "WPA: AES unwrap failed - could not decrypt "
1694 "GTK");
1695 return -1;
1696 }
1697 } else {
1698 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1699 "WPA: Unsupported key_info type %d", ver);
1700 return -1;
1701 }
1702 gd->tx = wpa_supplicant_gtk_tx_bit_workaround(
1703 sm, !!(key_info & WPA_KEY_INFO_TXRX));
1704 return 0;
1705}
1706
1707
1708static int wpa_supplicant_send_2_of_2(struct wpa_sm *sm,
1709 const struct wpa_eapol_key *key,
1710 int ver, u16 key_info)
1711{
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001712 size_t mic_len, hdrlen, rlen;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001713 struct wpa_eapol_key *reply;
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001714 u8 *rbuf, *key_mic;
Hai Shalom74f70d42019-02-11 14:42:39 -08001715 size_t kde_len = 0;
1716
1717#ifdef CONFIG_OCV
1718 if (wpa_sm_ocv_enabled(sm))
1719 kde_len = OCV_OCI_KDE_LEN;
1720#endif /* CONFIG_OCV */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001721
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001722 mic_len = wpa_mic_len(sm->key_mgmt, sm->pmk_len);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001723 hdrlen = sizeof(*reply) + mic_len + 2;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001724 rbuf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_KEY, NULL,
Hai Shalom74f70d42019-02-11 14:42:39 -08001725 hdrlen + kde_len, &rlen, (void *) &reply);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001726 if (rbuf == NULL)
1727 return -1;
1728
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001729 reply->type = (sm->proto == WPA_PROTO_RSN ||
1730 sm->proto == WPA_PROTO_OSEN) ?
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001731 EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA;
1732 key_info &= WPA_KEY_INFO_KEY_INDEX_MASK;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001733 key_info |= ver | WPA_KEY_INFO_SECURE;
1734 if (mic_len)
1735 key_info |= WPA_KEY_INFO_MIC;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001736 else
1737 key_info |= WPA_KEY_INFO_ENCR_KEY_DATA;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001738 WPA_PUT_BE16(reply->key_info, key_info);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001739 if (sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001740 WPA_PUT_BE16(reply->key_length, 0);
1741 else
1742 os_memcpy(reply->key_length, key->key_length, 2);
1743 os_memcpy(reply->replay_counter, key->replay_counter,
1744 WPA_REPLAY_COUNTER_LEN);
1745
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001746 key_mic = (u8 *) (reply + 1);
Hai Shalom74f70d42019-02-11 14:42:39 -08001747 WPA_PUT_BE16(key_mic + mic_len, kde_len); /* Key Data Length */
1748
1749#ifdef CONFIG_OCV
1750 if (wpa_sm_ocv_enabled(sm)) {
1751 struct wpa_channel_info ci;
1752 u8 *pos;
1753
1754 if (wpa_sm_channel_info(sm, &ci) != 0) {
1755 wpa_printf(MSG_WARNING,
1756 "Failed to get channel info for OCI element in EAPOL-Key 2/2");
1757 os_free(rbuf);
1758 return -1;
1759 }
1760
1761 pos = key_mic + mic_len + 2; /* Key Data */
1762 if (ocv_insert_oci_kde(&ci, &pos) < 0) {
1763 os_free(rbuf);
1764 return -1;
1765 }
1766 }
1767#endif /* CONFIG_OCV */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001768
1769 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Sending EAPOL-Key 2/2");
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001770 return wpa_eapol_key_send(sm, &sm->ptk, ver, sm->bssid, ETH_P_EAPOL,
1771 rbuf, rlen, key_mic);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001772}
1773
1774
1775static void wpa_supplicant_process_1_of_2(struct wpa_sm *sm,
1776 const unsigned char *src_addr,
1777 const struct wpa_eapol_key *key,
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001778 const u8 *key_data,
1779 size_t key_data_len, u16 ver)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001780{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001781 u16 key_info;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001782 int rekey, ret;
1783 struct wpa_gtk_data gd;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001784 const u8 *key_rsc;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001785
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001786 if (!sm->msg_3_of_4_ok && !wpa_fils_is_completed(sm)) {
Dmitry Shmidt4dd28dc2015-03-10 11:21:43 -07001787 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1788 "WPA: Group Key Handshake started prior to completion of 4-way handshake");
1789 goto failed;
1790 }
1791
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001792 os_memset(&gd, 0, sizeof(gd));
1793
1794 rekey = wpa_sm_get_state(sm) == WPA_COMPLETED;
1795 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: RX message 1 of Group Key "
1796 "Handshake from " MACSTR " (ver=%d)", MAC2STR(src_addr), ver);
1797
1798 key_info = WPA_GET_BE16(key->key_info);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001799
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001800 if (sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN) {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001801 ret = wpa_supplicant_process_1_of_2_rsn(sm, key_data,
1802 key_data_len, key_info,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001803 &gd);
1804 } else {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001805 ret = wpa_supplicant_process_1_of_2_wpa(sm, key, key_data,
1806 key_data_len,
1807 key_info, ver, &gd);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001808 }
1809
1810 wpa_sm_set_state(sm, WPA_GROUP_HANDSHAKE);
1811
1812 if (ret)
1813 goto failed;
1814
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001815 key_rsc = key->key_rsc;
1816 if (wpa_supplicant_rsc_relaxation(sm, key->key_rsc))
1817 key_rsc = null_rsc;
1818
Jouni Malinen58c0e962017-10-01 12:12:24 +03001819 if (wpa_supplicant_install_gtk(sm, &gd, key_rsc, 0) ||
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001820 wpa_supplicant_send_2_of_2(sm, key, ver, key_info) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001821 goto failed;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001822 os_memset(&gd, 0, sizeof(gd));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001823
1824 if (rekey) {
1825 wpa_msg(sm->ctx->msg_ctx, MSG_INFO, "WPA: Group rekeying "
1826 "completed with " MACSTR " [GTK=%s]",
1827 MAC2STR(sm->bssid), wpa_cipher_txt(sm->group_cipher));
1828 wpa_sm_cancel_auth_timeout(sm);
1829 wpa_sm_set_state(sm, WPA_COMPLETED);
1830 } else {
1831 wpa_supplicant_key_neg_complete(sm, sm->bssid,
1832 key_info &
1833 WPA_KEY_INFO_SECURE);
1834 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001835
1836 wpa_sm_set_rekey_offload(sm);
1837
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001838 return;
1839
1840failed:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001841 os_memset(&gd, 0, sizeof(gd));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001842 wpa_sm_deauthenticate(sm, WLAN_REASON_UNSPECIFIED);
1843}
1844
1845
1846static int wpa_supplicant_verify_eapol_key_mic(struct wpa_sm *sm,
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001847 struct wpa_eapol_key *key,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001848 u16 ver,
1849 const u8 *buf, size_t len)
1850{
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001851 u8 mic[WPA_EAPOL_KEY_MIC_MAX_LEN];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001852 int ok = 0;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001853 size_t mic_len = wpa_mic_len(sm->key_mgmt, sm->pmk_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001854
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001855 os_memcpy(mic, key + 1, mic_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001856 if (sm->tptk_set) {
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001857 os_memset(key + 1, 0, mic_len);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001858 if (wpa_eapol_key_mic(sm->tptk.kck, sm->tptk.kck_len,
1859 sm->key_mgmt,
1860 ver, buf, len, (u8 *) (key + 1)) < 0 ||
1861 os_memcmp_const(mic, key + 1, mic_len) != 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001862 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1863 "WPA: Invalid EAPOL-Key MIC "
1864 "when using TPTK - ignoring TPTK");
Hai Shalom74f70d42019-02-11 14:42:39 -08001865#ifdef TEST_FUZZ
1866 wpa_printf(MSG_INFO,
1867 "TEST: Ignore Key MIC failure for fuzz testing");
1868 goto continue_fuzz;
1869#endif /* TEST_FUZZ */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001870 } else {
Hai Shalom74f70d42019-02-11 14:42:39 -08001871#ifdef TEST_FUZZ
1872 continue_fuzz:
1873#endif /* TEST_FUZZ */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001874 ok = 1;
1875 sm->tptk_set = 0;
1876 sm->ptk_set = 1;
1877 os_memcpy(&sm->ptk, &sm->tptk, sizeof(sm->ptk));
Dmitry Shmidt61593f02014-04-21 16:27:35 -07001878 os_memset(&sm->tptk, 0, sizeof(sm->tptk));
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001879 /*
1880 * This assures the same TPTK in sm->tptk can never be
Roshan Pius3a1667e2018-07-03 15:17:14 -07001881 * copied twice to sm->ptk as the new PTK. In
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001882 * combination with the installed flag in the wpa_ptk
1883 * struct, this assures the same PTK is only installed
1884 * once.
1885 */
1886 sm->renew_snonce = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001887 }
1888 }
1889
1890 if (!ok && sm->ptk_set) {
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001891 os_memset(key + 1, 0, mic_len);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001892 if (wpa_eapol_key_mic(sm->ptk.kck, sm->ptk.kck_len,
1893 sm->key_mgmt,
1894 ver, buf, len, (u8 *) (key + 1)) < 0 ||
1895 os_memcmp_const(mic, key + 1, mic_len) != 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001896 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1897 "WPA: Invalid EAPOL-Key MIC - "
1898 "dropping packet");
Hai Shalom74f70d42019-02-11 14:42:39 -08001899#ifdef TEST_FUZZ
1900 wpa_printf(MSG_INFO,
1901 "TEST: Ignore Key MIC failure for fuzz testing");
1902 goto continue_fuzz2;
1903#endif /* TEST_FUZZ */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001904 return -1;
1905 }
Hai Shalom74f70d42019-02-11 14:42:39 -08001906#ifdef TEST_FUZZ
1907 continue_fuzz2:
1908#endif /* TEST_FUZZ */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001909 ok = 1;
1910 }
1911
1912 if (!ok) {
1913 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1914 "WPA: Could not verify EAPOL-Key MIC - "
1915 "dropping packet");
1916 return -1;
1917 }
1918
1919 os_memcpy(sm->rx_replay_counter, key->replay_counter,
1920 WPA_REPLAY_COUNTER_LEN);
1921 sm->rx_replay_counter_set = 1;
1922 return 0;
1923}
1924
1925
1926/* Decrypt RSN EAPOL-Key key data (RC4 or AES-WRAP) */
1927static int wpa_supplicant_decrypt_key_data(struct wpa_sm *sm,
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001928 struct wpa_eapol_key *key,
1929 size_t mic_len, u16 ver,
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001930 u8 *key_data, size_t *key_data_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001931{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001932 wpa_hexdump(MSG_DEBUG, "RSN: encrypted key data",
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001933 key_data, *key_data_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001934 if (!sm->ptk_set) {
1935 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1936 "WPA: PTK not available, cannot decrypt EAPOL-Key Key "
1937 "Data");
1938 return -1;
1939 }
1940
1941 /* Decrypt key data here so that this operation does not need
1942 * to be implemented separately for each message type. */
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001943 if (ver == WPA_KEY_INFO_TYPE_HMAC_MD5_RC4 && sm->ptk.kek_len == 16) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001944#ifdef CONFIG_NO_RC4
1945 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1946 "WPA: RC4 not supported in the build");
1947 return -1;
1948#else /* CONFIG_NO_RC4 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001949 u8 ek[32];
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001950
1951 wpa_printf(MSG_DEBUG, "WPA: Decrypt Key Data using RC4");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001952 os_memcpy(ek, key->key_iv, 16);
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001953 os_memcpy(ek + 16, sm->ptk.kek, sm->ptk.kek_len);
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001954 if (rc4_skip(ek, 32, 256, key_data, *key_data_len)) {
Dmitry Shmidt61593f02014-04-21 16:27:35 -07001955 os_memset(ek, 0, sizeof(ek));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001956 wpa_msg(sm->ctx->msg_ctx, MSG_ERROR,
1957 "WPA: RC4 failed");
1958 return -1;
1959 }
Dmitry Shmidt61593f02014-04-21 16:27:35 -07001960 os_memset(ek, 0, sizeof(ek));
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001961#endif /* CONFIG_NO_RC4 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001962 } else if (ver == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES ||
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001963 ver == WPA_KEY_INFO_TYPE_AES_128_CMAC ||
Roshan Pius3a1667e2018-07-03 15:17:14 -07001964 wpa_use_aes_key_wrap(sm->key_mgmt)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001965 u8 *buf;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001966
1967 wpa_printf(MSG_DEBUG,
1968 "WPA: Decrypt Key Data using AES-UNWRAP (KEK length %u)",
1969 (unsigned int) sm->ptk.kek_len);
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001970 if (*key_data_len < 8 || *key_data_len % 8) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001971 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001972 "WPA: Unsupported AES-WRAP len %u",
1973 (unsigned int) *key_data_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001974 return -1;
1975 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001976 *key_data_len -= 8; /* AES-WRAP adds 8 bytes */
1977 buf = os_malloc(*key_data_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001978 if (buf == NULL) {
1979 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1980 "WPA: No memory for AES-UNWRAP buffer");
1981 return -1;
1982 }
Hai Shalom74f70d42019-02-11 14:42:39 -08001983#ifdef TEST_FUZZ
1984 os_memset(buf, 0x11, *key_data_len);
1985#endif /* TEST_FUZZ */
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001986 if (aes_unwrap(sm->ptk.kek, sm->ptk.kek_len, *key_data_len / 8,
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001987 key_data, buf)) {
Hai Shalom74f70d42019-02-11 14:42:39 -08001988#ifdef TEST_FUZZ
1989 wpa_printf(MSG_INFO,
1990 "TEST: Ignore AES unwrap failure for fuzz testing");
1991 goto continue_fuzz;
1992#endif /* TEST_FUZZ */
Dmitry Shmidt7d56b752015-12-22 10:59:44 -08001993 bin_clear_free(buf, *key_data_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001994 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1995 "WPA: AES unwrap failed - "
1996 "could not decrypt EAPOL-Key key data");
1997 return -1;
1998 }
Hai Shalom74f70d42019-02-11 14:42:39 -08001999#ifdef TEST_FUZZ
2000 continue_fuzz:
2001#endif /* TEST_FUZZ */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002002 os_memcpy(key_data, buf, *key_data_len);
Dmitry Shmidt7d56b752015-12-22 10:59:44 -08002003 bin_clear_free(buf, *key_data_len);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002004 WPA_PUT_BE16(((u8 *) (key + 1)) + mic_len, *key_data_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002005 } else {
2006 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
2007 "WPA: Unsupported key_info type %d", ver);
2008 return -1;
2009 }
2010 wpa_hexdump_key(MSG_DEBUG, "WPA: decrypted EAPOL-Key key data",
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002011 key_data, *key_data_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002012 return 0;
2013}
2014
2015
2016/**
2017 * wpa_sm_aborted_cached - Notify WPA that PMKSA caching was aborted
2018 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2019 */
2020void wpa_sm_aborted_cached(struct wpa_sm *sm)
2021{
2022 if (sm && sm->cur_pmksa) {
2023 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2024 "RSN: Cancelling PMKSA caching attempt");
2025 sm->cur_pmksa = NULL;
2026 }
2027}
2028
2029
2030static void wpa_eapol_key_dump(struct wpa_sm *sm,
Dmitry Shmidt807291d2015-01-27 13:40:23 -08002031 const struct wpa_eapol_key *key,
2032 unsigned int key_data_len,
2033 const u8 *mic, unsigned int mic_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002034{
2035#ifndef CONFIG_NO_STDOUT_DEBUG
2036 u16 key_info = WPA_GET_BE16(key->key_info);
2037
2038 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, " EAPOL-Key type=%d", key->type);
2039 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2040 " key_info 0x%x (ver=%d keyidx=%d rsvd=%d %s%s%s%s%s%s%s%s)",
2041 key_info, key_info & WPA_KEY_INFO_TYPE_MASK,
2042 (key_info & WPA_KEY_INFO_KEY_INDEX_MASK) >>
2043 WPA_KEY_INFO_KEY_INDEX_SHIFT,
2044 (key_info & (BIT(13) | BIT(14) | BIT(15))) >> 13,
2045 key_info & WPA_KEY_INFO_KEY_TYPE ? "Pairwise" : "Group",
2046 key_info & WPA_KEY_INFO_INSTALL ? " Install" : "",
2047 key_info & WPA_KEY_INFO_ACK ? " Ack" : "",
2048 key_info & WPA_KEY_INFO_MIC ? " MIC" : "",
2049 key_info & WPA_KEY_INFO_SECURE ? " Secure" : "",
2050 key_info & WPA_KEY_INFO_ERROR ? " Error" : "",
2051 key_info & WPA_KEY_INFO_REQUEST ? " Request" : "",
2052 key_info & WPA_KEY_INFO_ENCR_KEY_DATA ? " Encr" : "");
2053 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2054 " key_length=%u key_data_length=%u",
Dmitry Shmidt807291d2015-01-27 13:40:23 -08002055 WPA_GET_BE16(key->key_length), key_data_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002056 wpa_hexdump(MSG_DEBUG, " replay_counter",
2057 key->replay_counter, WPA_REPLAY_COUNTER_LEN);
2058 wpa_hexdump(MSG_DEBUG, " key_nonce", key->key_nonce, WPA_NONCE_LEN);
2059 wpa_hexdump(MSG_DEBUG, " key_iv", key->key_iv, 16);
2060 wpa_hexdump(MSG_DEBUG, " key_rsc", key->key_rsc, 8);
2061 wpa_hexdump(MSG_DEBUG, " key_id (reserved)", key->key_id, 8);
Dmitry Shmidt807291d2015-01-27 13:40:23 -08002062 wpa_hexdump(MSG_DEBUG, " key_mic", mic, mic_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002063#endif /* CONFIG_NO_STDOUT_DEBUG */
2064}
2065
2066
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002067#ifdef CONFIG_FILS
2068static int wpa_supp_aead_decrypt(struct wpa_sm *sm, u8 *buf, size_t buf_len,
2069 size_t *key_data_len)
2070{
2071 struct wpa_ptk *ptk;
2072 struct ieee802_1x_hdr *hdr;
2073 struct wpa_eapol_key *key;
2074 u8 *pos, *tmp;
2075 const u8 *aad[1];
2076 size_t aad_len[1];
2077
2078 if (*key_data_len < AES_BLOCK_SIZE) {
2079 wpa_printf(MSG_INFO, "No room for AES-SIV data in the frame");
2080 return -1;
2081 }
2082
2083 if (sm->tptk_set)
2084 ptk = &sm->tptk;
2085 else if (sm->ptk_set)
2086 ptk = &sm->ptk;
2087 else
2088 return -1;
2089
2090 hdr = (struct ieee802_1x_hdr *) buf;
2091 key = (struct wpa_eapol_key *) (hdr + 1);
2092 pos = (u8 *) (key + 1);
2093 pos += 2; /* Pointing at the Encrypted Key Data field */
2094
2095 tmp = os_malloc(*key_data_len);
2096 if (!tmp)
2097 return -1;
2098
2099 /* AES-SIV AAD from EAPOL protocol version field (inclusive) to
2100 * to Key Data (exclusive). */
2101 aad[0] = buf;
2102 aad_len[0] = pos - buf;
2103 if (aes_siv_decrypt(ptk->kek, ptk->kek_len, pos, *key_data_len,
2104 1, aad, aad_len, tmp) < 0) {
2105 wpa_printf(MSG_INFO, "Invalid AES-SIV data in the frame");
2106 bin_clear_free(tmp, *key_data_len);
2107 return -1;
2108 }
2109
2110 /* AEAD decryption and validation completed successfully */
2111 (*key_data_len) -= AES_BLOCK_SIZE;
2112 wpa_hexdump_key(MSG_DEBUG, "WPA: Decrypted Key Data",
2113 tmp, *key_data_len);
2114
2115 /* Replace Key Data field with the decrypted version */
2116 os_memcpy(pos, tmp, *key_data_len);
2117 pos -= 2; /* Key Data Length field */
2118 WPA_PUT_BE16(pos, *key_data_len);
2119 bin_clear_free(tmp, *key_data_len);
2120
2121 if (sm->tptk_set) {
2122 sm->tptk_set = 0;
2123 sm->ptk_set = 1;
2124 os_memcpy(&sm->ptk, &sm->tptk, sizeof(sm->ptk));
2125 os_memset(&sm->tptk, 0, sizeof(sm->tptk));
2126 }
2127
2128 os_memcpy(sm->rx_replay_counter, key->replay_counter,
2129 WPA_REPLAY_COUNTER_LEN);
2130 sm->rx_replay_counter_set = 1;
2131
2132 return 0;
2133}
2134#endif /* CONFIG_FILS */
2135
2136
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002137/**
2138 * wpa_sm_rx_eapol - Process received WPA EAPOL frames
2139 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2140 * @src_addr: Source MAC address of the EAPOL packet
2141 * @buf: Pointer to the beginning of the EAPOL data (EAPOL header)
2142 * @len: Length of the EAPOL frame
2143 * Returns: 1 = WPA EAPOL-Key processed, 0 = not a WPA EAPOL-Key, -1 failure
2144 *
2145 * This function is called for each received EAPOL frame. Other than EAPOL-Key
2146 * frames can be skipped if filtering is done elsewhere. wpa_sm_rx_eapol() is
2147 * only processing WPA and WPA2 EAPOL-Key frames.
2148 *
2149 * The received EAPOL-Key packets are validated and valid packets are replied
2150 * to. In addition, key material (PTK, GTK) is configured at the end of a
2151 * successful key handshake.
2152 */
2153int wpa_sm_rx_eapol(struct wpa_sm *sm, const u8 *src_addr,
2154 const u8 *buf, size_t len)
2155{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002156 size_t plen, data_len, key_data_len;
2157 const struct ieee802_1x_hdr *hdr;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002158 struct wpa_eapol_key *key;
2159 u16 key_info, ver;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002160 u8 *tmp = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002161 int ret = -1;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002162 u8 *mic, *key_data;
Dmitry Shmidt807291d2015-01-27 13:40:23 -08002163 size_t mic_len, keyhdrlen;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002164
2165#ifdef CONFIG_IEEE80211R
2166 sm->ft_completed = 0;
2167#endif /* CONFIG_IEEE80211R */
2168
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002169 mic_len = wpa_mic_len(sm->key_mgmt, sm->pmk_len);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002170 keyhdrlen = sizeof(*key) + mic_len + 2;
Dmitry Shmidt807291d2015-01-27 13:40:23 -08002171
2172 if (len < sizeof(*hdr) + keyhdrlen) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002173 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2174 "WPA: EAPOL frame too short to be a WPA "
2175 "EAPOL-Key (len %lu, expecting at least %lu)",
2176 (unsigned long) len,
Dmitry Shmidt807291d2015-01-27 13:40:23 -08002177 (unsigned long) sizeof(*hdr) + keyhdrlen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002178 return 0;
2179 }
2180
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002181 hdr = (const struct ieee802_1x_hdr *) buf;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002182 plen = be_to_host16(hdr->length);
2183 data_len = plen + sizeof(*hdr);
2184 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2185 "IEEE 802.1X RX: version=%d type=%d length=%lu",
2186 hdr->version, hdr->type, (unsigned long) plen);
2187
2188 if (hdr->version < EAPOL_VERSION) {
2189 /* TODO: backwards compatibility */
2190 }
2191 if (hdr->type != IEEE802_1X_TYPE_EAPOL_KEY) {
2192 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2193 "WPA: EAPOL frame (type %u) discarded, "
2194 "not a Key frame", hdr->type);
2195 ret = 0;
2196 goto out;
2197 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002198 wpa_hexdump(MSG_MSGDUMP, "WPA: RX EAPOL-Key", buf, len);
Dmitry Shmidt807291d2015-01-27 13:40:23 -08002199 if (plen > len - sizeof(*hdr) || plen < keyhdrlen) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002200 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2201 "WPA: EAPOL frame payload size %lu "
2202 "invalid (frame size %lu)",
2203 (unsigned long) plen, (unsigned long) len);
2204 ret = 0;
2205 goto out;
2206 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002207 if (data_len < len) {
2208 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2209 "WPA: ignoring %lu bytes after the IEEE 802.1X data",
2210 (unsigned long) len - data_len);
2211 }
2212
2213 /*
2214 * Make a copy of the frame since we need to modify the buffer during
2215 * MAC validation and Key Data decryption.
2216 */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002217 tmp = os_memdup(buf, data_len);
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002218 if (tmp == NULL)
2219 goto out;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002220 key = (struct wpa_eapol_key *) (tmp + sizeof(struct ieee802_1x_hdr));
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002221 mic = (u8 *) (key + 1);
2222 key_data = mic + mic_len + 2;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002223
2224 if (key->type != EAPOL_KEY_TYPE_WPA && key->type != EAPOL_KEY_TYPE_RSN)
2225 {
2226 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2227 "WPA: EAPOL-Key type (%d) unknown, discarded",
2228 key->type);
2229 ret = 0;
2230 goto out;
2231 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002232
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002233 key_data_len = WPA_GET_BE16(mic + mic_len);
2234 wpa_eapol_key_dump(sm, key, key_data_len, mic, mic_len);
Dmitry Shmidt807291d2015-01-27 13:40:23 -08002235
2236 if (key_data_len > plen - keyhdrlen) {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002237 wpa_msg(sm->ctx->msg_ctx, MSG_INFO, "WPA: Invalid EAPOL-Key "
2238 "frame - key_data overflow (%u > %u)",
2239 (unsigned int) key_data_len,
Dmitry Shmidt807291d2015-01-27 13:40:23 -08002240 (unsigned int) (plen - keyhdrlen));
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002241 goto out;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002242 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002243
2244 eapol_sm_notify_lower_layer_success(sm->eapol, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002245 key_info = WPA_GET_BE16(key->key_info);
2246 ver = key_info & WPA_KEY_INFO_TYPE_MASK;
2247 if (ver != WPA_KEY_INFO_TYPE_HMAC_MD5_RC4 &&
2248#if defined(CONFIG_IEEE80211R) || defined(CONFIG_IEEE80211W)
2249 ver != WPA_KEY_INFO_TYPE_AES_128_CMAC &&
2250#endif /* CONFIG_IEEE80211R || CONFIG_IEEE80211W */
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002251 ver != WPA_KEY_INFO_TYPE_HMAC_SHA1_AES &&
Roshan Pius3a1667e2018-07-03 15:17:14 -07002252 !wpa_use_akm_defined(sm->key_mgmt)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002253 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2254 "WPA: Unsupported EAPOL-Key descriptor version %d",
2255 ver);
2256 goto out;
2257 }
2258
Roshan Pius3a1667e2018-07-03 15:17:14 -07002259 if (wpa_use_akm_defined(sm->key_mgmt) &&
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002260 ver != WPA_KEY_INFO_TYPE_AKM_DEFINED) {
2261 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2262 "RSN: Unsupported EAPOL-Key descriptor version %d (expected AKM defined = 0)",
2263 ver);
2264 goto out;
2265 }
2266
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002267#ifdef CONFIG_IEEE80211R
2268 if (wpa_key_mgmt_ft(sm->key_mgmt)) {
2269 /* IEEE 802.11r uses a new key_info type (AES-128-CMAC). */
Roshan Pius3a1667e2018-07-03 15:17:14 -07002270 if (ver != WPA_KEY_INFO_TYPE_AES_128_CMAC &&
2271 !wpa_use_akm_defined(sm->key_mgmt)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002272 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2273 "FT: AP did not use AES-128-CMAC");
2274 goto out;
2275 }
2276 } else
2277#endif /* CONFIG_IEEE80211R */
2278#ifdef CONFIG_IEEE80211W
2279 if (wpa_key_mgmt_sha256(sm->key_mgmt)) {
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002280 if (ver != WPA_KEY_INFO_TYPE_AES_128_CMAC &&
Roshan Pius3a1667e2018-07-03 15:17:14 -07002281 !wpa_use_akm_defined(sm->key_mgmt)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002282 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2283 "WPA: AP did not use the "
2284 "negotiated AES-128-CMAC");
2285 goto out;
2286 }
2287 } else
2288#endif /* CONFIG_IEEE80211W */
2289 if (sm->pairwise_cipher == WPA_CIPHER_CCMP &&
Roshan Pius3a1667e2018-07-03 15:17:14 -07002290 !wpa_use_akm_defined(sm->key_mgmt) &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002291 ver != WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
2292 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2293 "WPA: CCMP is used, but EAPOL-Key "
2294 "descriptor version (%d) is not 2", ver);
2295 if (sm->group_cipher != WPA_CIPHER_CCMP &&
2296 !(key_info & WPA_KEY_INFO_KEY_TYPE)) {
2297 /* Earlier versions of IEEE 802.11i did not explicitly
2298 * require version 2 descriptor for all EAPOL-Key
2299 * packets, so allow group keys to use version 1 if
2300 * CCMP is not used for them. */
2301 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2302 "WPA: Backwards compatibility: allow invalid "
2303 "version for non-CCMP group keys");
Jouni Malinen658fb4a2014-11-14 20:57:05 +02002304 } else if (ver == WPA_KEY_INFO_TYPE_AES_128_CMAC) {
2305 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2306 "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 -07002307 } else
2308 goto out;
Dmitry Shmidt71757432014-06-02 13:50:35 -07002309 } else if (sm->pairwise_cipher == WPA_CIPHER_GCMP &&
Roshan Pius3a1667e2018-07-03 15:17:14 -07002310 !wpa_use_akm_defined(sm->key_mgmt) &&
Dmitry Shmidt71757432014-06-02 13:50:35 -07002311 ver != WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002312 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2313 "WPA: GCMP is used, but EAPOL-Key "
2314 "descriptor version (%d) is not 2", ver);
2315 goto out;
2316 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002317
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002318 if (sm->rx_replay_counter_set &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002319 os_memcmp(key->replay_counter, sm->rx_replay_counter,
2320 WPA_REPLAY_COUNTER_LEN) <= 0) {
2321 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
2322 "WPA: EAPOL-Key Replay Counter did not increase - "
2323 "dropping packet");
2324 goto out;
2325 }
2326
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002327 if (key_info & WPA_KEY_INFO_SMK_MESSAGE) {
2328 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2329 "WPA: Unsupported SMK bit in key_info");
2330 goto out;
2331 }
2332
2333 if (!(key_info & WPA_KEY_INFO_ACK)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002334 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2335 "WPA: No Ack bit in key_info");
2336 goto out;
2337 }
2338
2339 if (key_info & WPA_KEY_INFO_REQUEST) {
2340 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2341 "WPA: EAPOL-Key with Request bit - dropped");
2342 goto out;
2343 }
2344
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002345 if ((key_info & WPA_KEY_INFO_MIC) &&
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002346 wpa_supplicant_verify_eapol_key_mic(sm, key, ver, tmp, data_len))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002347 goto out;
2348
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002349#ifdef CONFIG_FILS
2350 if (!mic_len && (key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
2351 if (wpa_supp_aead_decrypt(sm, tmp, data_len, &key_data_len))
2352 goto out;
2353 }
2354#endif /* CONFIG_FILS */
2355
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002356 if ((sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN) &&
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002357 (key_info & WPA_KEY_INFO_ENCR_KEY_DATA) && mic_len) {
Hai Shalomce48b4a2018-09-05 11:41:35 -07002358 /*
2359 * Only decrypt the Key Data field if the frame's authenticity
2360 * was verified. When using AES-SIV (FILS), the MIC flag is not
2361 * set, so this check should only be performed if mic_len != 0
2362 * which is the case in this code branch.
2363 */
2364 if (!(key_info & WPA_KEY_INFO_MIC)) {
2365 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
2366 "WPA: Ignore EAPOL-Key with encrypted but unauthenticated data");
2367 goto out;
2368 }
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002369 if (wpa_supplicant_decrypt_key_data(sm, key, mic_len,
2370 ver, key_data,
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002371 &key_data_len))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002372 goto out;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002373 }
2374
2375 if (key_info & WPA_KEY_INFO_KEY_TYPE) {
2376 if (key_info & WPA_KEY_INFO_KEY_INDEX_MASK) {
2377 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
2378 "WPA: Ignored EAPOL-Key (Pairwise) with "
2379 "non-zero key index");
2380 goto out;
2381 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002382 if (key_info & (WPA_KEY_INFO_MIC |
2383 WPA_KEY_INFO_ENCR_KEY_DATA)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002384 /* 3/4 4-Way Handshake */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002385 wpa_supplicant_process_3_of_4(sm, key, ver, key_data,
2386 key_data_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002387 } else {
2388 /* 1/4 4-Way Handshake */
2389 wpa_supplicant_process_1_of_4(sm, src_addr, key,
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002390 ver, key_data,
2391 key_data_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002392 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002393 } else {
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002394 if ((mic_len && (key_info & WPA_KEY_INFO_MIC)) ||
2395 (!mic_len && (key_info & WPA_KEY_INFO_ENCR_KEY_DATA))) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002396 /* 1/2 Group Key Handshake */
2397 wpa_supplicant_process_1_of_2(sm, src_addr, key,
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002398 key_data, key_data_len,
2399 ver);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002400 } else {
2401 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002402 "WPA: EAPOL-Key (Group) without Mic/Encr bit - "
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002403 "dropped");
2404 }
2405 }
2406
2407 ret = 1;
2408
2409out:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002410 bin_clear_free(tmp, data_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002411 return ret;
2412}
2413
2414
2415#ifdef CONFIG_CTRL_IFACE
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002416static u32 wpa_key_mgmt_suite(struct wpa_sm *sm)
2417{
2418 switch (sm->key_mgmt) {
2419 case WPA_KEY_MGMT_IEEE8021X:
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002420 return ((sm->proto == WPA_PROTO_RSN ||
2421 sm->proto == WPA_PROTO_OSEN) ?
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002422 RSN_AUTH_KEY_MGMT_UNSPEC_802_1X :
2423 WPA_AUTH_KEY_MGMT_UNSPEC_802_1X);
2424 case WPA_KEY_MGMT_PSK:
2425 return (sm->proto == WPA_PROTO_RSN ?
2426 RSN_AUTH_KEY_MGMT_PSK_OVER_802_1X :
2427 WPA_AUTH_KEY_MGMT_PSK_OVER_802_1X);
2428#ifdef CONFIG_IEEE80211R
2429 case WPA_KEY_MGMT_FT_IEEE8021X:
2430 return RSN_AUTH_KEY_MGMT_FT_802_1X;
2431 case WPA_KEY_MGMT_FT_PSK:
2432 return RSN_AUTH_KEY_MGMT_FT_PSK;
2433#endif /* CONFIG_IEEE80211R */
2434#ifdef CONFIG_IEEE80211W
2435 case WPA_KEY_MGMT_IEEE8021X_SHA256:
2436 return RSN_AUTH_KEY_MGMT_802_1X_SHA256;
2437 case WPA_KEY_MGMT_PSK_SHA256:
2438 return RSN_AUTH_KEY_MGMT_PSK_SHA256;
2439#endif /* CONFIG_IEEE80211W */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002440 case WPA_KEY_MGMT_CCKM:
2441 return (sm->proto == WPA_PROTO_RSN ?
2442 RSN_AUTH_KEY_MGMT_CCKM:
2443 WPA_AUTH_KEY_MGMT_CCKM);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002444 case WPA_KEY_MGMT_WPA_NONE:
2445 return WPA_AUTH_KEY_MGMT_NONE;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002446 case WPA_KEY_MGMT_IEEE8021X_SUITE_B:
2447 return RSN_AUTH_KEY_MGMT_802_1X_SUITE_B;
Dmitry Shmidt807291d2015-01-27 13:40:23 -08002448 case WPA_KEY_MGMT_IEEE8021X_SUITE_B_192:
2449 return RSN_AUTH_KEY_MGMT_802_1X_SUITE_B_192;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002450 default:
2451 return 0;
2452 }
2453}
2454
2455
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002456#define RSN_SUITE "%02x-%02x-%02x-%d"
2457#define RSN_SUITE_ARG(s) \
2458((s) >> 24) & 0xff, ((s) >> 16) & 0xff, ((s) >> 8) & 0xff, (s) & 0xff
2459
2460/**
2461 * wpa_sm_get_mib - Dump text list of MIB entries
2462 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2463 * @buf: Buffer for the list
2464 * @buflen: Length of the buffer
2465 * Returns: Number of bytes written to buffer
2466 *
2467 * This function is used fetch dot11 MIB variables.
2468 */
2469int wpa_sm_get_mib(struct wpa_sm *sm, char *buf, size_t buflen)
2470{
2471 char pmkid_txt[PMKID_LEN * 2 + 1];
2472 int rsna, ret;
2473 size_t len;
2474
2475 if (sm->cur_pmksa) {
2476 wpa_snprintf_hex(pmkid_txt, sizeof(pmkid_txt),
2477 sm->cur_pmksa->pmkid, PMKID_LEN);
2478 } else
2479 pmkid_txt[0] = '\0';
2480
2481 if ((wpa_key_mgmt_wpa_psk(sm->key_mgmt) ||
2482 wpa_key_mgmt_wpa_ieee8021x(sm->key_mgmt)) &&
2483 sm->proto == WPA_PROTO_RSN)
2484 rsna = 1;
2485 else
2486 rsna = 0;
2487
2488 ret = os_snprintf(buf, buflen,
2489 "dot11RSNAOptionImplemented=TRUE\n"
2490 "dot11RSNAPreauthenticationImplemented=TRUE\n"
2491 "dot11RSNAEnabled=%s\n"
2492 "dot11RSNAPreauthenticationEnabled=%s\n"
2493 "dot11RSNAConfigVersion=%d\n"
2494 "dot11RSNAConfigPairwiseKeysSupported=5\n"
2495 "dot11RSNAConfigGroupCipherSize=%d\n"
2496 "dot11RSNAConfigPMKLifetime=%d\n"
2497 "dot11RSNAConfigPMKReauthThreshold=%d\n"
2498 "dot11RSNAConfigNumberOfPTKSAReplayCounters=1\n"
2499 "dot11RSNAConfigSATimeout=%d\n",
2500 rsna ? "TRUE" : "FALSE",
2501 rsna ? "TRUE" : "FALSE",
2502 RSN_VERSION,
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07002503 wpa_cipher_key_len(sm->group_cipher) * 8,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002504 sm->dot11RSNAConfigPMKLifetime,
2505 sm->dot11RSNAConfigPMKReauthThreshold,
2506 sm->dot11RSNAConfigSATimeout);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002507 if (os_snprintf_error(buflen, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002508 return 0;
2509 len = ret;
2510
2511 ret = os_snprintf(
2512 buf + len, buflen - len,
2513 "dot11RSNAAuthenticationSuiteSelected=" RSN_SUITE "\n"
2514 "dot11RSNAPairwiseCipherSelected=" RSN_SUITE "\n"
2515 "dot11RSNAGroupCipherSelected=" RSN_SUITE "\n"
2516 "dot11RSNAPMKIDUsed=%s\n"
2517 "dot11RSNAAuthenticationSuiteRequested=" RSN_SUITE "\n"
2518 "dot11RSNAPairwiseCipherRequested=" RSN_SUITE "\n"
2519 "dot11RSNAGroupCipherRequested=" RSN_SUITE "\n"
2520 "dot11RSNAConfigNumberOfGTKSAReplayCounters=0\n"
2521 "dot11RSNA4WayHandshakeFailures=%u\n",
2522 RSN_SUITE_ARG(wpa_key_mgmt_suite(sm)),
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07002523 RSN_SUITE_ARG(wpa_cipher_to_suite(sm->proto,
2524 sm->pairwise_cipher)),
2525 RSN_SUITE_ARG(wpa_cipher_to_suite(sm->proto,
2526 sm->group_cipher)),
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002527 pmkid_txt,
2528 RSN_SUITE_ARG(wpa_key_mgmt_suite(sm)),
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07002529 RSN_SUITE_ARG(wpa_cipher_to_suite(sm->proto,
2530 sm->pairwise_cipher)),
2531 RSN_SUITE_ARG(wpa_cipher_to_suite(sm->proto,
2532 sm->group_cipher)),
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002533 sm->dot11RSNA4WayHandshakeFailures);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002534 if (!os_snprintf_error(buflen - len, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002535 len += ret;
2536
2537 return (int) len;
2538}
2539#endif /* CONFIG_CTRL_IFACE */
2540
2541
2542static void wpa_sm_pmksa_free_cb(struct rsn_pmksa_cache_entry *entry,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002543 void *ctx, enum pmksa_free_reason reason)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002544{
2545 struct wpa_sm *sm = ctx;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002546 int deauth = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002547
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002548 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "RSN: PMKSA cache entry free_cb: "
2549 MACSTR " reason=%d", MAC2STR(entry->aa), reason);
2550
2551 if (sm->cur_pmksa == entry) {
2552 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2553 "RSN: %s current PMKSA entry",
2554 reason == PMKSA_REPLACE ? "replaced" : "removed");
2555 pmksa_cache_clear_current(sm);
2556
2557 /*
2558 * If an entry is simply being replaced, there's no need to
2559 * deauthenticate because it will be immediately re-added.
2560 * This happens when EAP authentication is completed again
2561 * (reauth or failed PMKSA caching attempt).
2562 */
2563 if (reason != PMKSA_REPLACE)
2564 deauth = 1;
2565 }
2566
2567 if (reason == PMKSA_EXPIRE &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002568 (sm->pmk_len == entry->pmk_len &&
2569 os_memcmp(sm->pmk, entry->pmk, sm->pmk_len) == 0)) {
2570 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002571 "RSN: deauthenticating due to expired PMK");
2572 pmksa_cache_clear_current(sm);
2573 deauth = 1;
2574 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002575
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002576 if (deauth) {
Roshan Pius3a1667e2018-07-03 15:17:14 -07002577 sm->pmk_len = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002578 os_memset(sm->pmk, 0, sizeof(sm->pmk));
2579 wpa_sm_deauthenticate(sm, WLAN_REASON_UNSPECIFIED);
2580 }
2581}
2582
2583
2584/**
2585 * wpa_sm_init - Initialize WPA state machine
2586 * @ctx: Context pointer for callbacks; this needs to be an allocated buffer
2587 * Returns: Pointer to the allocated WPA state machine data
2588 *
2589 * This function is used to allocate a new WPA state machine and the returned
2590 * value is passed to all WPA state machine calls.
2591 */
2592struct wpa_sm * wpa_sm_init(struct wpa_sm_ctx *ctx)
2593{
2594 struct wpa_sm *sm;
2595
2596 sm = os_zalloc(sizeof(*sm));
2597 if (sm == NULL)
2598 return NULL;
2599 dl_list_init(&sm->pmksa_candidates);
2600 sm->renew_snonce = 1;
2601 sm->ctx = ctx;
2602
2603 sm->dot11RSNAConfigPMKLifetime = 43200;
2604 sm->dot11RSNAConfigPMKReauthThreshold = 70;
2605 sm->dot11RSNAConfigSATimeout = 60;
2606
2607 sm->pmksa = pmksa_cache_init(wpa_sm_pmksa_free_cb, sm, sm);
2608 if (sm->pmksa == NULL) {
2609 wpa_msg(sm->ctx->msg_ctx, MSG_ERROR,
2610 "RSN: PMKSA cache initialization failed");
2611 os_free(sm);
2612 return NULL;
2613 }
2614
2615 return sm;
2616}
2617
2618
2619/**
2620 * wpa_sm_deinit - Deinitialize WPA state machine
2621 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2622 */
2623void wpa_sm_deinit(struct wpa_sm *sm)
2624{
2625 if (sm == NULL)
2626 return;
2627 pmksa_cache_deinit(sm->pmksa);
2628 eloop_cancel_timeout(wpa_sm_start_preauth, sm, NULL);
2629 eloop_cancel_timeout(wpa_sm_rekey_ptk, sm, NULL);
2630 os_free(sm->assoc_wpa_ie);
2631 os_free(sm->ap_wpa_ie);
2632 os_free(sm->ap_rsn_ie);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002633 wpa_sm_drop_sa(sm);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002634 os_free(sm->ctx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002635#ifdef CONFIG_IEEE80211R
2636 os_free(sm->assoc_resp_ies);
2637#endif /* CONFIG_IEEE80211R */
Dmitry Shmidt55840ad2015-12-14 12:45:46 -08002638#ifdef CONFIG_TESTING_OPTIONS
2639 wpabuf_free(sm->test_assoc_ie);
2640#endif /* CONFIG_TESTING_OPTIONS */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002641#ifdef CONFIG_FILS_SK_PFS
2642 crypto_ecdh_deinit(sm->fils_ecdh);
2643#endif /* CONFIG_FILS_SK_PFS */
2644#ifdef CONFIG_FILS
2645 wpabuf_free(sm->fils_ft_ies);
2646#endif /* CONFIG_FILS */
2647#ifdef CONFIG_OWE
2648 crypto_ecdh_deinit(sm->owe_ecdh);
2649#endif /* CONFIG_OWE */
Hai Shalom021b0b52019-04-10 11:17:58 -07002650#ifdef CONFIG_DPP2
2651 wpabuf_clear_free(sm->dpp_z);
2652#endif /* CONFIG_DPP2 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002653 os_free(sm);
2654}
2655
2656
2657/**
2658 * wpa_sm_notify_assoc - Notify WPA state machine about association
2659 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2660 * @bssid: The BSSID of the new association
2661 *
2662 * This function is called to let WPA state machine know that the connection
2663 * was established.
2664 */
2665void wpa_sm_notify_assoc(struct wpa_sm *sm, const u8 *bssid)
2666{
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02002667 int clear_keys = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002668
2669 if (sm == NULL)
2670 return;
2671
2672 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2673 "WPA: Association event - clear replay counter");
2674 os_memcpy(sm->bssid, bssid, ETH_ALEN);
2675 os_memset(sm->rx_replay_counter, 0, WPA_REPLAY_COUNTER_LEN);
2676 sm->rx_replay_counter_set = 0;
2677 sm->renew_snonce = 1;
2678 if (os_memcmp(sm->preauth_bssid, bssid, ETH_ALEN) == 0)
2679 rsn_preauth_deinit(sm);
2680
2681#ifdef CONFIG_IEEE80211R
2682 if (wpa_ft_is_completed(sm)) {
2683 /*
2684 * Clear portValid to kick EAPOL state machine to re-enter
2685 * AUTHENTICATED state to get the EAPOL port Authorized.
2686 */
2687 eapol_sm_notify_portValid(sm->eapol, FALSE);
2688 wpa_supplicant_key_neg_complete(sm, sm->bssid, 1);
2689
2690 /* Prepare for the next transition */
2691 wpa_ft_prepare_auth_request(sm, NULL);
2692
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02002693 clear_keys = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002694 }
2695#endif /* CONFIG_IEEE80211R */
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002696#ifdef CONFIG_FILS
2697 if (sm->fils_completed) {
2698 /*
2699 * Clear portValid to kick EAPOL state machine to re-enter
2700 * AUTHENTICATED state to get the EAPOL port Authorized.
2701 */
2702 wpa_supplicant_key_neg_complete(sm, sm->bssid, 1);
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02002703 clear_keys = 0;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002704 }
2705#endif /* CONFIG_FILS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002706
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02002707 if (clear_keys) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002708 /*
2709 * IEEE 802.11, 8.4.10: Delete PTK SA on (re)association if
2710 * this is not part of a Fast BSS Transition.
2711 */
2712 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Clear old PTK");
2713 sm->ptk_set = 0;
Dmitry Shmidt61593f02014-04-21 16:27:35 -07002714 os_memset(&sm->ptk, 0, sizeof(sm->ptk));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002715 sm->tptk_set = 0;
Dmitry Shmidt61593f02014-04-21 16:27:35 -07002716 os_memset(&sm->tptk, 0, sizeof(sm->tptk));
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02002717 os_memset(&sm->gtk, 0, sizeof(sm->gtk));
Jouni Malinen58c0e962017-10-01 12:12:24 +03002718 os_memset(&sm->gtk_wnm_sleep, 0, sizeof(sm->gtk_wnm_sleep));
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02002719#ifdef CONFIG_IEEE80211W
2720 os_memset(&sm->igtk, 0, sizeof(sm->igtk));
Jouni Malinen58c0e962017-10-01 12:12:24 +03002721 os_memset(&sm->igtk_wnm_sleep, 0, sizeof(sm->igtk_wnm_sleep));
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02002722#endif /* CONFIG_IEEE80211W */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002723 }
2724
2725#ifdef CONFIG_TDLS
2726 wpa_tdls_assoc(sm);
2727#endif /* CONFIG_TDLS */
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002728
2729#ifdef CONFIG_P2P
2730 os_memset(sm->p2p_ip_addr, 0, sizeof(sm->p2p_ip_addr));
2731#endif /* CONFIG_P2P */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002732}
2733
2734
2735/**
2736 * wpa_sm_notify_disassoc - Notify WPA state machine about disassociation
2737 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2738 *
2739 * This function is called to let WPA state machine know that the connection
2740 * was lost. This will abort any existing pre-authentication session.
2741 */
2742void wpa_sm_notify_disassoc(struct wpa_sm *sm)
2743{
Dmitry Shmidt4dd28dc2015-03-10 11:21:43 -07002744 eloop_cancel_timeout(wpa_sm_start_preauth, sm, NULL);
2745 eloop_cancel_timeout(wpa_sm_rekey_ptk, sm, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002746 rsn_preauth_deinit(sm);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002747 pmksa_cache_clear_current(sm);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002748 if (wpa_sm_get_state(sm) == WPA_4WAY_HANDSHAKE)
2749 sm->dot11RSNA4WayHandshakeFailures++;
2750#ifdef CONFIG_TDLS
2751 wpa_tdls_disassoc(sm);
2752#endif /* CONFIG_TDLS */
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002753#ifdef CONFIG_FILS
2754 sm->fils_completed = 0;
2755#endif /* CONFIG_FILS */
Jouni Malinen4283f9e2017-09-22 12:06:37 +03002756#ifdef CONFIG_IEEE80211R
2757 sm->ft_reassoc_completed = 0;
2758#endif /* CONFIG_IEEE80211R */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002759
2760 /* Keys are not needed in the WPA state machine anymore */
2761 wpa_sm_drop_sa(sm);
Dmitry Shmidt4dd28dc2015-03-10 11:21:43 -07002762
2763 sm->msg_3_of_4_ok = 0;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002764 os_memset(sm->bssid, 0, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002765}
2766
2767
2768/**
2769 * wpa_sm_set_pmk - Set PMK
2770 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2771 * @pmk: The new PMK
2772 * @pmk_len: The length of the new PMK in bytes
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08002773 * @pmkid: Calculated PMKID
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002774 * @bssid: AA to add into PMKSA cache or %NULL to not cache the PMK
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002775 *
2776 * Configure the PMK for WPA state machine.
2777 */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002778void wpa_sm_set_pmk(struct wpa_sm *sm, const u8 *pmk, size_t pmk_len,
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08002779 const u8 *pmkid, const u8 *bssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002780{
2781 if (sm == NULL)
2782 return;
2783
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002784 wpa_hexdump_key(MSG_DEBUG, "WPA: Set PMK based on external data",
2785 pmk, pmk_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002786 sm->pmk_len = pmk_len;
2787 os_memcpy(sm->pmk, pmk, pmk_len);
2788
2789#ifdef CONFIG_IEEE80211R
2790 /* Set XXKey to be PSK for FT key derivation */
2791 sm->xxkey_len = pmk_len;
2792 os_memcpy(sm->xxkey, pmk, pmk_len);
2793#endif /* CONFIG_IEEE80211R */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002794
2795 if (bssid) {
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08002796 pmksa_cache_add(sm->pmksa, pmk, pmk_len, pmkid, NULL, 0,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002797 bssid, sm->own_addr,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002798 sm->network_ctx, sm->key_mgmt, NULL);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002799 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002800}
2801
2802
2803/**
2804 * wpa_sm_set_pmk_from_pmksa - Set PMK based on the current PMKSA
2805 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2806 *
2807 * Take the PMK from the current PMKSA into use. If no PMKSA is active, the PMK
2808 * will be cleared.
2809 */
2810void wpa_sm_set_pmk_from_pmksa(struct wpa_sm *sm)
2811{
2812 if (sm == NULL)
2813 return;
2814
2815 if (sm->cur_pmksa) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002816 wpa_hexdump_key(MSG_DEBUG,
2817 "WPA: Set PMK based on current PMKSA",
2818 sm->cur_pmksa->pmk, sm->cur_pmksa->pmk_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002819 sm->pmk_len = sm->cur_pmksa->pmk_len;
2820 os_memcpy(sm->pmk, sm->cur_pmksa->pmk, sm->pmk_len);
2821 } else {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002822 wpa_printf(MSG_DEBUG, "WPA: No current PMKSA - clear PMK");
2823 sm->pmk_len = 0;
2824 os_memset(sm->pmk, 0, PMK_LEN_MAX);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002825 }
2826}
2827
2828
2829/**
2830 * wpa_sm_set_fast_reauth - Set fast reauthentication (EAP) enabled/disabled
2831 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2832 * @fast_reauth: Whether fast reauthentication (EAP) is allowed
2833 */
2834void wpa_sm_set_fast_reauth(struct wpa_sm *sm, int fast_reauth)
2835{
2836 if (sm)
2837 sm->fast_reauth = fast_reauth;
2838}
2839
2840
2841/**
2842 * wpa_sm_set_scard_ctx - Set context pointer for smartcard callbacks
2843 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2844 * @scard_ctx: Context pointer for smartcard related callback functions
2845 */
2846void wpa_sm_set_scard_ctx(struct wpa_sm *sm, void *scard_ctx)
2847{
2848 if (sm == NULL)
2849 return;
2850 sm->scard_ctx = scard_ctx;
2851 if (sm->preauth_eapol)
2852 eapol_sm_register_scard_ctx(sm->preauth_eapol, scard_ctx);
2853}
2854
2855
2856/**
2857 * wpa_sm_set_config - Notification of current configration change
2858 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2859 * @config: Pointer to current network configuration
2860 *
2861 * Notify WPA state machine that configuration has changed. config will be
2862 * stored as a backpointer to network configuration. This can be %NULL to clear
2863 * the stored pointed.
2864 */
2865void wpa_sm_set_config(struct wpa_sm *sm, struct rsn_supp_config *config)
2866{
2867 if (!sm)
2868 return;
2869
2870 if (config) {
2871 sm->network_ctx = config->network_ctx;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002872 sm->allowed_pairwise_cipher = config->allowed_pairwise_cipher;
2873 sm->proactive_key_caching = config->proactive_key_caching;
2874 sm->eap_workaround = config->eap_workaround;
2875 sm->eap_conf_ctx = config->eap_conf_ctx;
2876 if (config->ssid) {
2877 os_memcpy(sm->ssid, config->ssid, config->ssid_len);
2878 sm->ssid_len = config->ssid_len;
2879 } else
2880 sm->ssid_len = 0;
2881 sm->wpa_ptk_rekey = config->wpa_ptk_rekey;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002882 sm->p2p = config->p2p;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002883 sm->wpa_rsc_relaxation = config->wpa_rsc_relaxation;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002884#ifdef CONFIG_FILS
2885 if (config->fils_cache_id) {
2886 sm->fils_cache_id_set = 1;
2887 os_memcpy(sm->fils_cache_id, config->fils_cache_id,
2888 FILS_CACHE_ID_LEN);
2889 } else {
2890 sm->fils_cache_id_set = 0;
2891 }
2892#endif /* CONFIG_FILS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002893 } else {
2894 sm->network_ctx = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002895 sm->allowed_pairwise_cipher = 0;
2896 sm->proactive_key_caching = 0;
2897 sm->eap_workaround = 0;
2898 sm->eap_conf_ctx = NULL;
2899 sm->ssid_len = 0;
2900 sm->wpa_ptk_rekey = 0;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002901 sm->p2p = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002902 sm->wpa_rsc_relaxation = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002903 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002904}
2905
2906
2907/**
2908 * wpa_sm_set_own_addr - Set own MAC address
2909 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2910 * @addr: Own MAC address
2911 */
2912void wpa_sm_set_own_addr(struct wpa_sm *sm, const u8 *addr)
2913{
2914 if (sm)
2915 os_memcpy(sm->own_addr, addr, ETH_ALEN);
2916}
2917
2918
2919/**
2920 * wpa_sm_set_ifname - Set network interface name
2921 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2922 * @ifname: Interface name
2923 * @bridge_ifname: Optional bridge interface name (for pre-auth)
2924 */
2925void wpa_sm_set_ifname(struct wpa_sm *sm, const char *ifname,
2926 const char *bridge_ifname)
2927{
2928 if (sm) {
2929 sm->ifname = ifname;
2930 sm->bridge_ifname = bridge_ifname;
2931 }
2932}
2933
2934
2935/**
2936 * wpa_sm_set_eapol - Set EAPOL state machine pointer
2937 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2938 * @eapol: Pointer to EAPOL state machine allocated with eapol_sm_init()
2939 */
2940void wpa_sm_set_eapol(struct wpa_sm *sm, struct eapol_sm *eapol)
2941{
2942 if (sm)
2943 sm->eapol = eapol;
2944}
2945
2946
2947/**
2948 * wpa_sm_set_param - Set WPA state machine parameters
2949 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2950 * @param: Parameter field
2951 * @value: Parameter value
2952 * Returns: 0 on success, -1 on failure
2953 */
2954int wpa_sm_set_param(struct wpa_sm *sm, enum wpa_sm_conf_params param,
2955 unsigned int value)
2956{
2957 int ret = 0;
2958
2959 if (sm == NULL)
2960 return -1;
2961
2962 switch (param) {
2963 case RSNA_PMK_LIFETIME:
2964 if (value > 0)
2965 sm->dot11RSNAConfigPMKLifetime = value;
2966 else
2967 ret = -1;
2968 break;
2969 case RSNA_PMK_REAUTH_THRESHOLD:
2970 if (value > 0 && value <= 100)
2971 sm->dot11RSNAConfigPMKReauthThreshold = value;
2972 else
2973 ret = -1;
2974 break;
2975 case RSNA_SA_TIMEOUT:
2976 if (value > 0)
2977 sm->dot11RSNAConfigSATimeout = value;
2978 else
2979 ret = -1;
2980 break;
2981 case WPA_PARAM_PROTO:
2982 sm->proto = value;
2983 break;
2984 case WPA_PARAM_PAIRWISE:
2985 sm->pairwise_cipher = value;
2986 break;
2987 case WPA_PARAM_GROUP:
2988 sm->group_cipher = value;
2989 break;
2990 case WPA_PARAM_KEY_MGMT:
2991 sm->key_mgmt = value;
2992 break;
2993#ifdef CONFIG_IEEE80211W
2994 case WPA_PARAM_MGMT_GROUP:
2995 sm->mgmt_group_cipher = value;
2996 break;
2997#endif /* CONFIG_IEEE80211W */
2998 case WPA_PARAM_RSN_ENABLED:
2999 sm->rsn_enabled = value;
3000 break;
3001 case WPA_PARAM_MFP:
3002 sm->mfp = value;
3003 break;
Hai Shalom74f70d42019-02-11 14:42:39 -08003004 case WPA_PARAM_OCV:
3005 sm->ocv = value;
3006 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003007 default:
3008 break;
3009 }
3010
3011 return ret;
3012}
3013
3014
3015/**
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003016 * wpa_sm_get_status - Get WPA state machine
3017 * @sm: Pointer to WPA state machine data from wpa_sm_init()
3018 * @buf: Buffer for status information
3019 * @buflen: Maximum buffer length
3020 * @verbose: Whether to include verbose status information
3021 * Returns: Number of bytes written to buf.
3022 *
3023 * Query WPA state machine for status information. This function fills in
3024 * a text area with current status information. If the buffer (buf) is not
3025 * large enough, status information will be truncated to fit the buffer.
3026 */
3027int wpa_sm_get_status(struct wpa_sm *sm, char *buf, size_t buflen,
3028 int verbose)
3029{
3030 char *pos = buf, *end = buf + buflen;
3031 int ret;
3032
3033 ret = os_snprintf(pos, end - pos,
3034 "pairwise_cipher=%s\n"
3035 "group_cipher=%s\n"
3036 "key_mgmt=%s\n",
3037 wpa_cipher_txt(sm->pairwise_cipher),
3038 wpa_cipher_txt(sm->group_cipher),
3039 wpa_key_mgmt_txt(sm->key_mgmt, sm->proto));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003040 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003041 return pos - buf;
3042 pos += ret;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003043
3044 if (sm->mfp != NO_MGMT_FRAME_PROTECTION && sm->ap_rsn_ie) {
3045 struct wpa_ie_data rsn;
3046 if (wpa_parse_wpa_ie_rsn(sm->ap_rsn_ie, sm->ap_rsn_ie_len, &rsn)
3047 >= 0 &&
3048 rsn.capabilities & (WPA_CAPABILITY_MFPR |
3049 WPA_CAPABILITY_MFPC)) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003050 ret = os_snprintf(pos, end - pos, "pmf=%d\n"
3051 "mgmt_group_cipher=%s\n",
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003052 (rsn.capabilities &
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003053 WPA_CAPABILITY_MFPR) ? 2 : 1,
3054 wpa_cipher_txt(
3055 sm->mgmt_group_cipher));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003056 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003057 return pos - buf;
3058 pos += ret;
3059 }
3060 }
3061
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003062 return pos - buf;
3063}
3064
3065
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07003066int wpa_sm_pmf_enabled(struct wpa_sm *sm)
3067{
3068 struct wpa_ie_data rsn;
3069
3070 if (sm->mfp == NO_MGMT_FRAME_PROTECTION || !sm->ap_rsn_ie)
3071 return 0;
3072
3073 if (wpa_parse_wpa_ie_rsn(sm->ap_rsn_ie, sm->ap_rsn_ie_len, &rsn) >= 0 &&
3074 rsn.capabilities & (WPA_CAPABILITY_MFPR | WPA_CAPABILITY_MFPC))
3075 return 1;
3076
3077 return 0;
3078}
3079
3080
Hai Shalom74f70d42019-02-11 14:42:39 -08003081int wpa_sm_ocv_enabled(struct wpa_sm *sm)
3082{
3083 struct wpa_ie_data rsn;
3084
3085 if (!sm->ocv || !sm->ap_rsn_ie)
3086 return 0;
3087
3088 return wpa_parse_wpa_ie_rsn(sm->ap_rsn_ie, sm->ap_rsn_ie_len,
3089 &rsn) >= 0 &&
3090 (rsn.capabilities & WPA_CAPABILITY_OCVC);
3091}
3092
3093
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003094/**
3095 * wpa_sm_set_assoc_wpa_ie_default - Generate own WPA/RSN IE from configuration
3096 * @sm: Pointer to WPA state machine data from wpa_sm_init()
3097 * @wpa_ie: Pointer to buffer for WPA/RSN IE
3098 * @wpa_ie_len: Pointer to the length of the wpa_ie buffer
3099 * Returns: 0 on success, -1 on failure
3100 */
3101int wpa_sm_set_assoc_wpa_ie_default(struct wpa_sm *sm, u8 *wpa_ie,
3102 size_t *wpa_ie_len)
3103{
3104 int res;
3105
3106 if (sm == NULL)
3107 return -1;
3108
Dmitry Shmidt55840ad2015-12-14 12:45:46 -08003109#ifdef CONFIG_TESTING_OPTIONS
3110 if (sm->test_assoc_ie) {
3111 wpa_printf(MSG_DEBUG,
3112 "TESTING: Replace association WPA/RSN IE");
3113 if (*wpa_ie_len < wpabuf_len(sm->test_assoc_ie))
3114 return -1;
3115 os_memcpy(wpa_ie, wpabuf_head(sm->test_assoc_ie),
3116 wpabuf_len(sm->test_assoc_ie));
3117 res = wpabuf_len(sm->test_assoc_ie);
3118 } else
3119#endif /* CONFIG_TESTING_OPTIONS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003120 res = wpa_gen_wpa_ie(sm, wpa_ie, *wpa_ie_len);
3121 if (res < 0)
3122 return -1;
3123 *wpa_ie_len = res;
3124
3125 wpa_hexdump(MSG_DEBUG, "WPA: Set own WPA IE default",
3126 wpa_ie, *wpa_ie_len);
3127
3128 if (sm->assoc_wpa_ie == NULL) {
3129 /*
3130 * Make a copy of the WPA/RSN IE so that 4-Way Handshake gets
3131 * the correct version of the IE even if PMKSA caching is
3132 * aborted (which would remove PMKID from IE generation).
3133 */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003134 sm->assoc_wpa_ie = os_memdup(wpa_ie, *wpa_ie_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003135 if (sm->assoc_wpa_ie == NULL)
3136 return -1;
3137
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003138 sm->assoc_wpa_ie_len = *wpa_ie_len;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003139 } else {
3140 wpa_hexdump(MSG_DEBUG,
3141 "WPA: Leave previously set WPA IE default",
3142 sm->assoc_wpa_ie, sm->assoc_wpa_ie_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003143 }
3144
3145 return 0;
3146}
3147
3148
3149/**
3150 * wpa_sm_set_assoc_wpa_ie - Set own WPA/RSN IE from (Re)AssocReq
3151 * @sm: Pointer to WPA state machine data from wpa_sm_init()
3152 * @ie: Pointer to IE data (starting from id)
3153 * @len: IE length
3154 * Returns: 0 on success, -1 on failure
3155 *
3156 * Inform WPA state machine about the WPA/RSN IE used in (Re)Association
3157 * Request frame. The IE will be used to override the default value generated
3158 * with wpa_sm_set_assoc_wpa_ie_default().
3159 */
3160int wpa_sm_set_assoc_wpa_ie(struct wpa_sm *sm, const u8 *ie, size_t len)
3161{
3162 if (sm == NULL)
3163 return -1;
3164
3165 os_free(sm->assoc_wpa_ie);
3166 if (ie == NULL || len == 0) {
3167 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
3168 "WPA: clearing own WPA/RSN IE");
3169 sm->assoc_wpa_ie = NULL;
3170 sm->assoc_wpa_ie_len = 0;
3171 } else {
3172 wpa_hexdump(MSG_DEBUG, "WPA: set own WPA/RSN IE", ie, len);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003173 sm->assoc_wpa_ie = os_memdup(ie, len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003174 if (sm->assoc_wpa_ie == NULL)
3175 return -1;
3176
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003177 sm->assoc_wpa_ie_len = len;
3178 }
3179
3180 return 0;
3181}
3182
3183
3184/**
3185 * wpa_sm_set_ap_wpa_ie - Set AP WPA IE from Beacon/ProbeResp
3186 * @sm: Pointer to WPA state machine data from wpa_sm_init()
3187 * @ie: Pointer to IE data (starting from id)
3188 * @len: IE length
3189 * Returns: 0 on success, -1 on failure
3190 *
3191 * Inform WPA state machine about the WPA IE used in Beacon / Probe Response
3192 * frame.
3193 */
3194int wpa_sm_set_ap_wpa_ie(struct wpa_sm *sm, const u8 *ie, size_t len)
3195{
3196 if (sm == NULL)
3197 return -1;
3198
3199 os_free(sm->ap_wpa_ie);
3200 if (ie == NULL || len == 0) {
3201 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
3202 "WPA: clearing AP WPA IE");
3203 sm->ap_wpa_ie = NULL;
3204 sm->ap_wpa_ie_len = 0;
3205 } else {
3206 wpa_hexdump(MSG_DEBUG, "WPA: set AP WPA IE", ie, len);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003207 sm->ap_wpa_ie = os_memdup(ie, len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003208 if (sm->ap_wpa_ie == NULL)
3209 return -1;
3210
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003211 sm->ap_wpa_ie_len = len;
3212 }
3213
3214 return 0;
3215}
3216
3217
3218/**
3219 * wpa_sm_set_ap_rsn_ie - Set AP RSN IE from Beacon/ProbeResp
3220 * @sm: Pointer to WPA state machine data from wpa_sm_init()
3221 * @ie: Pointer to IE data (starting from id)
3222 * @len: IE length
3223 * Returns: 0 on success, -1 on failure
3224 *
3225 * Inform WPA state machine about the RSN IE used in Beacon / Probe Response
3226 * frame.
3227 */
3228int wpa_sm_set_ap_rsn_ie(struct wpa_sm *sm, const u8 *ie, size_t len)
3229{
3230 if (sm == NULL)
3231 return -1;
3232
3233 os_free(sm->ap_rsn_ie);
3234 if (ie == NULL || len == 0) {
3235 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
3236 "WPA: clearing AP RSN IE");
3237 sm->ap_rsn_ie = NULL;
3238 sm->ap_rsn_ie_len = 0;
3239 } else {
3240 wpa_hexdump(MSG_DEBUG, "WPA: set AP RSN IE", ie, len);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003241 sm->ap_rsn_ie = os_memdup(ie, len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003242 if (sm->ap_rsn_ie == NULL)
3243 return -1;
3244
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003245 sm->ap_rsn_ie_len = len;
3246 }
3247
3248 return 0;
3249}
3250
3251
3252/**
3253 * wpa_sm_parse_own_wpa_ie - Parse own WPA/RSN IE
3254 * @sm: Pointer to WPA state machine data from wpa_sm_init()
3255 * @data: Pointer to data area for parsing results
3256 * Returns: 0 on success, -1 if IE is not known, or -2 on parsing failure
3257 *
3258 * Parse the contents of the own WPA or RSN IE from (Re)AssocReq and write the
3259 * parsed data into data.
3260 */
3261int wpa_sm_parse_own_wpa_ie(struct wpa_sm *sm, struct wpa_ie_data *data)
3262{
3263 if (sm == NULL)
3264 return -1;
3265
3266 if (sm->assoc_wpa_ie == NULL) {
3267 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
3268 "WPA: No WPA/RSN IE available from association info");
3269 return -1;
3270 }
3271 if (wpa_parse_wpa_ie(sm->assoc_wpa_ie, sm->assoc_wpa_ie_len, data))
3272 return -2;
3273 return 0;
3274}
3275
3276
3277int wpa_sm_pmksa_cache_list(struct wpa_sm *sm, char *buf, size_t len)
3278{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003279 return pmksa_cache_list(sm->pmksa, buf, len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003280}
3281
3282
Dmitry Shmidt29333592017-01-09 12:27:11 -08003283struct rsn_pmksa_cache_entry * wpa_sm_pmksa_cache_head(struct wpa_sm *sm)
3284{
3285 return pmksa_cache_head(sm->pmksa);
3286}
3287
3288
3289struct rsn_pmksa_cache_entry *
3290wpa_sm_pmksa_cache_add_entry(struct wpa_sm *sm,
3291 struct rsn_pmksa_cache_entry * entry)
3292{
3293 return pmksa_cache_add_entry(sm->pmksa, entry);
3294}
3295
3296
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003297void wpa_sm_pmksa_cache_add(struct wpa_sm *sm, const u8 *pmk, size_t pmk_len,
3298 const u8 *pmkid, const u8 *bssid,
3299 const u8 *fils_cache_id)
3300{
3301 sm->cur_pmksa = pmksa_cache_add(sm->pmksa, pmk, pmk_len, pmkid, NULL, 0,
3302 bssid, sm->own_addr, sm->network_ctx,
3303 sm->key_mgmt, fils_cache_id);
3304}
3305
3306
3307int wpa_sm_pmksa_exists(struct wpa_sm *sm, const u8 *bssid,
3308 const void *network_ctx)
3309{
Roshan Pius3a1667e2018-07-03 15:17:14 -07003310 return pmksa_cache_get(sm->pmksa, bssid, NULL, network_ctx, 0) != NULL;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003311}
3312
3313
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003314void wpa_sm_drop_sa(struct wpa_sm *sm)
3315{
3316 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Clear old PMK and PTK");
3317 sm->ptk_set = 0;
3318 sm->tptk_set = 0;
Roshan Pius3a1667e2018-07-03 15:17:14 -07003319 sm->pmk_len = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003320 os_memset(sm->pmk, 0, sizeof(sm->pmk));
3321 os_memset(&sm->ptk, 0, sizeof(sm->ptk));
3322 os_memset(&sm->tptk, 0, sizeof(sm->tptk));
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02003323 os_memset(&sm->gtk, 0, sizeof(sm->gtk));
Jouni Malinen58c0e962017-10-01 12:12:24 +03003324 os_memset(&sm->gtk_wnm_sleep, 0, sizeof(sm->gtk_wnm_sleep));
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02003325#ifdef CONFIG_IEEE80211W
3326 os_memset(&sm->igtk, 0, sizeof(sm->igtk));
Jouni Malinen58c0e962017-10-01 12:12:24 +03003327 os_memset(&sm->igtk_wnm_sleep, 0, sizeof(sm->igtk_wnm_sleep));
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02003328#endif /* CONFIG_IEEE80211W */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003329#ifdef CONFIG_IEEE80211R
3330 os_memset(sm->xxkey, 0, sizeof(sm->xxkey));
Roshan Pius3a1667e2018-07-03 15:17:14 -07003331 sm->xxkey_len = 0;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003332 os_memset(sm->pmk_r0, 0, sizeof(sm->pmk_r0));
Roshan Pius3a1667e2018-07-03 15:17:14 -07003333 sm->pmk_r0_len = 0;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003334 os_memset(sm->pmk_r1, 0, sizeof(sm->pmk_r1));
Roshan Pius3a1667e2018-07-03 15:17:14 -07003335 sm->pmk_r1_len = 0;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003336#endif /* CONFIG_IEEE80211R */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003337}
3338
3339
3340int wpa_sm_has_ptk(struct wpa_sm *sm)
3341{
3342 if (sm == NULL)
3343 return 0;
3344 return sm->ptk_set;
3345}
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003346
3347
3348void wpa_sm_update_replay_ctr(struct wpa_sm *sm, const u8 *replay_ctr)
3349{
3350 os_memcpy(sm->rx_replay_counter, replay_ctr, WPA_REPLAY_COUNTER_LEN);
3351}
3352
3353
3354void wpa_sm_pmksa_cache_flush(struct wpa_sm *sm, void *network_ctx)
3355{
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07003356 pmksa_cache_flush(sm->pmksa, network_ctx, NULL, 0);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003357}
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003358
3359
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003360#ifdef CONFIG_WNM
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003361int wpa_wnmsleep_install_key(struct wpa_sm *sm, u8 subelem_id, u8 *buf)
3362{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003363 u16 keyinfo;
3364 u8 keylen; /* plaintext key len */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003365 u8 *key_rsc;
3366
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003367 if (subelem_id == WNM_SLEEP_SUBELEM_GTK) {
Dmitry Shmidt61593f02014-04-21 16:27:35 -07003368 struct wpa_gtk_data gd;
3369
3370 os_memset(&gd, 0, sizeof(gd));
3371 keylen = wpa_cipher_key_len(sm->group_cipher);
3372 gd.key_rsc_len = wpa_cipher_rsc_len(sm->group_cipher);
3373 gd.alg = wpa_cipher_to_alg(sm->group_cipher);
3374 if (gd.alg == WPA_ALG_NONE) {
3375 wpa_printf(MSG_DEBUG, "Unsupported group cipher suite");
3376 return -1;
3377 }
3378
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003379 key_rsc = buf + 5;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003380 keyinfo = WPA_GET_LE16(buf + 2);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003381 gd.gtk_len = keylen;
3382 if (gd.gtk_len != buf[4]) {
3383 wpa_printf(MSG_DEBUG, "GTK len mismatch len %d vs %d",
3384 gd.gtk_len, buf[4]);
3385 return -1;
3386 }
3387 gd.keyidx = keyinfo & 0x03; /* B0 - B1 */
3388 gd.tx = wpa_supplicant_gtk_tx_bit_workaround(
3389 sm, !!(keyinfo & WPA_KEY_INFO_TXRX));
3390
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003391 os_memcpy(gd.gtk, buf + 13, gd.gtk_len);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003392
3393 wpa_hexdump_key(MSG_DEBUG, "Install GTK (WNM SLEEP)",
3394 gd.gtk, gd.gtk_len);
Jouni Malinen58c0e962017-10-01 12:12:24 +03003395 if (wpa_supplicant_install_gtk(sm, &gd, key_rsc, 1)) {
Dmitry Shmidt61593f02014-04-21 16:27:35 -07003396 os_memset(&gd, 0, sizeof(gd));
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003397 wpa_printf(MSG_DEBUG, "Failed to install the GTK in "
3398 "WNM mode");
3399 return -1;
3400 }
Dmitry Shmidt61593f02014-04-21 16:27:35 -07003401 os_memset(&gd, 0, sizeof(gd));
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003402#ifdef CONFIG_IEEE80211W
3403 } else if (subelem_id == WNM_SLEEP_SUBELEM_IGTK) {
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02003404 const struct wpa_igtk_kde *igtk;
Dmitry Shmidt61593f02014-04-21 16:27:35 -07003405
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02003406 igtk = (const struct wpa_igtk_kde *) (buf + 2);
Jouni Malinen58c0e962017-10-01 12:12:24 +03003407 if (wpa_supplicant_install_igtk(sm, igtk, 1) < 0)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003408 return -1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003409#endif /* CONFIG_IEEE80211W */
3410 } else {
3411 wpa_printf(MSG_DEBUG, "Unknown element id");
3412 return -1;
3413 }
3414
3415 return 0;
3416}
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003417#endif /* CONFIG_WNM */
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003418
3419
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003420#ifdef CONFIG_P2P
3421
3422int wpa_sm_get_p2p_ip_addr(struct wpa_sm *sm, u8 *buf)
3423{
3424 if (sm == NULL || WPA_GET_BE32(sm->p2p_ip_addr) == 0)
3425 return -1;
3426 os_memcpy(buf, sm->p2p_ip_addr, 3 * 4);
3427 return 0;
3428}
3429
3430#endif /* CONFIG_P2P */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003431
3432
3433void wpa_sm_set_rx_replay_ctr(struct wpa_sm *sm, const u8 *rx_replay_counter)
3434{
3435 if (rx_replay_counter == NULL)
3436 return;
3437
3438 os_memcpy(sm->rx_replay_counter, rx_replay_counter,
3439 WPA_REPLAY_COUNTER_LEN);
3440 sm->rx_replay_counter_set = 1;
3441 wpa_printf(MSG_DEBUG, "Updated key replay counter");
3442}
3443
3444
Dmitry Shmidt807291d2015-01-27 13:40:23 -08003445void wpa_sm_set_ptk_kck_kek(struct wpa_sm *sm,
3446 const u8 *ptk_kck, size_t ptk_kck_len,
3447 const u8 *ptk_kek, size_t ptk_kek_len)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003448{
Dmitry Shmidt807291d2015-01-27 13:40:23 -08003449 if (ptk_kck && ptk_kck_len <= WPA_KCK_MAX_LEN) {
3450 os_memcpy(sm->ptk.kck, ptk_kck, ptk_kck_len);
3451 sm->ptk.kck_len = ptk_kck_len;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003452 wpa_printf(MSG_DEBUG, "Updated PTK KCK");
3453 }
Dmitry Shmidt807291d2015-01-27 13:40:23 -08003454 if (ptk_kek && ptk_kek_len <= WPA_KEK_MAX_LEN) {
3455 os_memcpy(sm->ptk.kek, ptk_kek, ptk_kek_len);
3456 sm->ptk.kek_len = ptk_kek_len;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003457 wpa_printf(MSG_DEBUG, "Updated PTK KEK");
3458 }
3459 sm->ptk_set = 1;
3460}
Dmitry Shmidt55840ad2015-12-14 12:45:46 -08003461
3462
3463#ifdef CONFIG_TESTING_OPTIONS
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003464
Dmitry Shmidt55840ad2015-12-14 12:45:46 -08003465void wpa_sm_set_test_assoc_ie(struct wpa_sm *sm, struct wpabuf *buf)
3466{
3467 wpabuf_free(sm->test_assoc_ie);
3468 sm->test_assoc_ie = buf;
3469}
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003470
3471
3472const u8 * wpa_sm_get_anonce(struct wpa_sm *sm)
3473{
3474 return sm->anonce;
3475}
3476
Dmitry Shmidt55840ad2015-12-14 12:45:46 -08003477#endif /* CONFIG_TESTING_OPTIONS */
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003478
3479
Roshan Pius3a1667e2018-07-03 15:17:14 -07003480unsigned int wpa_sm_get_key_mgmt(struct wpa_sm *sm)
3481{
3482 return sm->key_mgmt;
3483}
3484
3485
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003486#ifdef CONFIG_FILS
3487
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003488struct wpabuf * fils_build_auth(struct wpa_sm *sm, int dh_group, const u8 *md)
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003489{
3490 struct wpabuf *buf = NULL;
3491 struct wpabuf *erp_msg;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003492 struct wpabuf *pub = NULL;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003493
3494 erp_msg = eapol_sm_build_erp_reauth_start(sm->eapol);
3495 if (!erp_msg && !sm->cur_pmksa) {
3496 wpa_printf(MSG_DEBUG,
3497 "FILS: Neither ERP EAP-Initiate/Re-auth nor PMKSA cache entry is available - skip FILS");
3498 goto fail;
3499 }
3500
3501 wpa_printf(MSG_DEBUG, "FILS: Try to use FILS (erp=%d pmksa_cache=%d)",
3502 erp_msg != NULL, sm->cur_pmksa != NULL);
3503
3504 sm->fils_completed = 0;
3505
3506 if (!sm->assoc_wpa_ie) {
3507 wpa_printf(MSG_INFO, "FILS: No own RSN IE set for FILS");
3508 goto fail;
3509 }
3510
3511 if (random_get_bytes(sm->fils_nonce, FILS_NONCE_LEN) < 0 ||
3512 random_get_bytes(sm->fils_session, FILS_SESSION_LEN) < 0)
3513 goto fail;
3514
3515 wpa_hexdump(MSG_DEBUG, "FILS: Generated FILS Nonce",
3516 sm->fils_nonce, FILS_NONCE_LEN);
3517 wpa_hexdump(MSG_DEBUG, "FILS: Generated FILS Session",
3518 sm->fils_session, FILS_SESSION_LEN);
3519
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003520#ifdef CONFIG_FILS_SK_PFS
3521 sm->fils_dh_group = dh_group;
3522 if (dh_group) {
3523 crypto_ecdh_deinit(sm->fils_ecdh);
3524 sm->fils_ecdh = crypto_ecdh_init(dh_group);
3525 if (!sm->fils_ecdh) {
3526 wpa_printf(MSG_INFO,
3527 "FILS: Could not initialize ECDH with group %d",
3528 dh_group);
3529 goto fail;
3530 }
3531 pub = crypto_ecdh_get_pubkey(sm->fils_ecdh, 1);
3532 if (!pub)
3533 goto fail;
3534 wpa_hexdump_buf(MSG_DEBUG, "FILS: Element (DH public key)",
3535 pub);
3536 sm->fils_dh_elem_len = wpabuf_len(pub);
3537 }
3538#endif /* CONFIG_FILS_SK_PFS */
3539
3540 buf = wpabuf_alloc(1000 + sm->assoc_wpa_ie_len +
3541 (pub ? wpabuf_len(pub) : 0));
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003542 if (!buf)
3543 goto fail;
3544
3545 /* Fields following the Authentication algorithm number field */
3546
3547 /* Authentication Transaction seq# */
3548 wpabuf_put_le16(buf, 1);
3549
3550 /* Status Code */
3551 wpabuf_put_le16(buf, WLAN_STATUS_SUCCESS);
3552
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003553 /* TODO: FILS PK */
3554#ifdef CONFIG_FILS_SK_PFS
3555 if (dh_group) {
3556 /* Finite Cyclic Group */
3557 wpabuf_put_le16(buf, dh_group);
3558 /* Element */
3559 wpabuf_put_buf(buf, pub);
3560 }
3561#endif /* CONFIG_FILS_SK_PFS */
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003562
3563 /* RSNE */
3564 wpa_hexdump(MSG_DEBUG, "FILS: RSNE in FILS Authentication frame",
3565 sm->assoc_wpa_ie, sm->assoc_wpa_ie_len);
3566 wpabuf_put_data(buf, sm->assoc_wpa_ie, sm->assoc_wpa_ie_len);
3567
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003568 if (md) {
3569 /* MDE when using FILS for FT initial association */
3570 struct rsn_mdie *mdie;
3571
3572 wpabuf_put_u8(buf, WLAN_EID_MOBILITY_DOMAIN);
3573 wpabuf_put_u8(buf, sizeof(*mdie));
3574 mdie = wpabuf_put(buf, sizeof(*mdie));
3575 os_memcpy(mdie->mobility_domain, md, MOBILITY_DOMAIN_ID_LEN);
3576 mdie->ft_capab = 0;
3577 }
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003578
3579 /* FILS Nonce */
3580 wpabuf_put_u8(buf, WLAN_EID_EXTENSION); /* Element ID */
3581 wpabuf_put_u8(buf, 1 + FILS_NONCE_LEN); /* Length */
3582 /* Element ID Extension */
3583 wpabuf_put_u8(buf, WLAN_EID_EXT_FILS_NONCE);
3584 wpabuf_put_data(buf, sm->fils_nonce, FILS_NONCE_LEN);
3585
3586 /* FILS Session */
3587 wpabuf_put_u8(buf, WLAN_EID_EXTENSION); /* Element ID */
3588 wpabuf_put_u8(buf, 1 + FILS_SESSION_LEN); /* Length */
3589 /* Element ID Extension */
3590 wpabuf_put_u8(buf, WLAN_EID_EXT_FILS_SESSION);
3591 wpabuf_put_data(buf, sm->fils_session, FILS_SESSION_LEN);
3592
3593 /* FILS Wrapped Data */
Paul Stewart092955c2017-02-06 09:13:09 -08003594 sm->fils_erp_pmkid_set = 0;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003595 if (erp_msg) {
3596 wpabuf_put_u8(buf, WLAN_EID_EXTENSION); /* Element ID */
3597 wpabuf_put_u8(buf, 1 + wpabuf_len(erp_msg)); /* Length */
3598 /* Element ID Extension */
3599 wpabuf_put_u8(buf, WLAN_EID_EXT_FILS_WRAPPED_DATA);
3600 wpabuf_put_buf(buf, erp_msg);
Paul Stewart092955c2017-02-06 09:13:09 -08003601 /* Calculate pending PMKID here so that we do not need to
3602 * maintain a copy of the EAP-Initiate/Reauth message. */
3603 if (fils_pmkid_erp(sm->key_mgmt, wpabuf_head(erp_msg),
3604 wpabuf_len(erp_msg),
3605 sm->fils_erp_pmkid) == 0)
3606 sm->fils_erp_pmkid_set = 1;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003607 }
3608
3609 wpa_hexdump_buf(MSG_DEBUG, "RSN: FILS fields for Authentication frame",
3610 buf);
3611
3612fail:
3613 wpabuf_free(erp_msg);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003614 wpabuf_free(pub);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003615 return buf;
3616}
3617
3618
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003619int fils_process_auth(struct wpa_sm *sm, const u8 *bssid, const u8 *data,
3620 size_t len)
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003621{
3622 const u8 *pos, *end;
3623 struct ieee802_11_elems elems;
3624 struct wpa_ie_data rsn;
3625 int pmkid_match = 0;
3626 u8 ick[FILS_ICK_MAX_LEN];
3627 size_t ick_len;
3628 int res;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003629 struct wpabuf *dh_ss = NULL;
3630 const u8 *g_sta = NULL;
3631 size_t g_sta_len = 0;
3632 const u8 *g_ap = NULL;
3633 size_t g_ap_len = 0;
3634 struct wpabuf *pub = NULL;
3635
3636 os_memcpy(sm->bssid, bssid, ETH_ALEN);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003637
3638 wpa_hexdump(MSG_DEBUG, "FILS: Authentication frame fields",
3639 data, len);
3640 pos = data;
3641 end = data + len;
3642
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003643 /* TODO: FILS PK */
3644#ifdef CONFIG_FILS_SK_PFS
3645 if (sm->fils_dh_group) {
3646 u16 group;
3647
3648 /* Using FILS PFS */
3649
3650 /* Finite Cyclic Group */
3651 if (end - pos < 2) {
3652 wpa_printf(MSG_DEBUG,
3653 "FILS: No room for Finite Cyclic Group");
3654 goto fail;
3655 }
3656 group = WPA_GET_LE16(pos);
3657 pos += 2;
3658 if (group != sm->fils_dh_group) {
3659 wpa_printf(MSG_DEBUG,
3660 "FILS: Unexpected change in Finite Cyclic Group: %u (expected %u)",
3661 group, sm->fils_dh_group);
3662 goto fail;
3663 }
3664
3665 /* Element */
3666 if ((size_t) (end - pos) < sm->fils_dh_elem_len) {
3667 wpa_printf(MSG_DEBUG, "FILS: No room for Element");
3668 goto fail;
3669 }
3670
3671 if (!sm->fils_ecdh) {
3672 wpa_printf(MSG_DEBUG, "FILS: No ECDH state available");
3673 goto fail;
3674 }
3675 dh_ss = crypto_ecdh_set_peerkey(sm->fils_ecdh, 1, pos,
3676 sm->fils_dh_elem_len);
3677 if (!dh_ss) {
3678 wpa_printf(MSG_DEBUG, "FILS: ECDH operation failed");
3679 goto fail;
3680 }
3681 wpa_hexdump_buf_key(MSG_DEBUG, "FILS: DH_SS", dh_ss);
3682 g_ap = pos;
3683 g_ap_len = sm->fils_dh_elem_len;
3684 pos += sm->fils_dh_elem_len;
3685 }
3686#endif /* CONFIG_FILS_SK_PFS */
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003687
3688 wpa_hexdump(MSG_DEBUG, "FILS: Remaining IEs", pos, end - pos);
3689 if (ieee802_11_parse_elems(pos, end - pos, &elems, 1) == ParseFailed) {
3690 wpa_printf(MSG_DEBUG, "FILS: Could not parse elements");
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003691 goto fail;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003692 }
3693
3694 /* RSNE */
3695 wpa_hexdump(MSG_DEBUG, "FILS: RSN element", elems.rsn_ie,
3696 elems.rsn_ie_len);
3697 if (!elems.rsn_ie ||
3698 wpa_parse_wpa_ie_rsn(elems.rsn_ie - 2, elems.rsn_ie_len + 2,
3699 &rsn) < 0) {
3700 wpa_printf(MSG_DEBUG, "FILS: No RSN element");
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003701 goto fail;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003702 }
3703
3704 if (!elems.fils_nonce) {
3705 wpa_printf(MSG_DEBUG, "FILS: No FILS Nonce field");
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003706 goto fail;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003707 }
3708 os_memcpy(sm->fils_anonce, elems.fils_nonce, FILS_NONCE_LEN);
3709 wpa_hexdump(MSG_DEBUG, "FILS: ANonce", sm->fils_anonce, FILS_NONCE_LEN);
3710
Roshan Pius3a1667e2018-07-03 15:17:14 -07003711#ifdef CONFIG_IEEE80211R
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003712 if (wpa_key_mgmt_ft(sm->key_mgmt)) {
3713 struct wpa_ft_ies parse;
3714
3715 if (!elems.mdie || !elems.ftie) {
3716 wpa_printf(MSG_DEBUG, "FILS+FT: No MDE or FTE");
3717 goto fail;
3718 }
3719
Roshan Pius3a1667e2018-07-03 15:17:14 -07003720 if (wpa_ft_parse_ies(pos, end - pos, &parse,
3721 wpa_key_mgmt_sha384(sm->key_mgmt)) < 0) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003722 wpa_printf(MSG_DEBUG, "FILS+FT: Failed to parse IEs");
3723 goto fail;
3724 }
3725
3726 if (!parse.r0kh_id) {
3727 wpa_printf(MSG_DEBUG,
3728 "FILS+FT: No R0KH-ID subelem in FTE");
3729 goto fail;
3730 }
3731 os_memcpy(sm->r0kh_id, parse.r0kh_id, parse.r0kh_id_len);
3732 sm->r0kh_id_len = parse.r0kh_id_len;
3733 wpa_hexdump_ascii(MSG_DEBUG, "FILS+FT: R0KH-ID",
3734 sm->r0kh_id, sm->r0kh_id_len);
3735
3736 if (!parse.r1kh_id) {
3737 wpa_printf(MSG_DEBUG,
3738 "FILS+FT: No R1KH-ID subelem in FTE");
3739 goto fail;
3740 }
3741 os_memcpy(sm->r1kh_id, parse.r1kh_id, FT_R1KH_ID_LEN);
3742 wpa_hexdump(MSG_DEBUG, "FILS+FT: R1KH-ID",
3743 sm->r1kh_id, FT_R1KH_ID_LEN);
3744
3745 /* TODO: Check MDE and FTE payload */
3746
3747 wpabuf_free(sm->fils_ft_ies);
3748 sm->fils_ft_ies = wpabuf_alloc(2 + elems.mdie_len +
3749 2 + elems.ftie_len);
3750 if (!sm->fils_ft_ies)
3751 goto fail;
3752 wpabuf_put_data(sm->fils_ft_ies, elems.mdie - 2,
3753 2 + elems.mdie_len);
3754 wpabuf_put_data(sm->fils_ft_ies, elems.ftie - 2,
3755 2 + elems.ftie_len);
3756 } else {
3757 wpabuf_free(sm->fils_ft_ies);
3758 sm->fils_ft_ies = NULL;
3759 }
Roshan Pius3a1667e2018-07-03 15:17:14 -07003760#endif /* CONFIG_IEEE80211R */
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003761
3762 /* PMKID List */
3763 if (rsn.pmkid && rsn.num_pmkid > 0) {
3764 wpa_hexdump(MSG_DEBUG, "FILS: PMKID List",
3765 rsn.pmkid, rsn.num_pmkid * PMKID_LEN);
3766
3767 if (rsn.num_pmkid != 1) {
3768 wpa_printf(MSG_DEBUG, "FILS: Invalid PMKID selection");
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003769 goto fail;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003770 }
3771 wpa_hexdump(MSG_DEBUG, "FILS: PMKID", rsn.pmkid, PMKID_LEN);
3772 if (os_memcmp(sm->cur_pmksa->pmkid, rsn.pmkid, PMKID_LEN) != 0)
3773 {
3774 wpa_printf(MSG_DEBUG, "FILS: PMKID mismatch");
3775 wpa_hexdump(MSG_DEBUG, "FILS: Expected PMKID",
3776 sm->cur_pmksa->pmkid, PMKID_LEN);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003777 goto fail;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003778 }
3779 wpa_printf(MSG_DEBUG,
3780 "FILS: Matching PMKID - continue using PMKSA caching");
3781 pmkid_match = 1;
3782 }
3783 if (!pmkid_match && sm->cur_pmksa) {
3784 wpa_printf(MSG_DEBUG,
3785 "FILS: No PMKID match - cannot use cached PMKSA entry");
3786 sm->cur_pmksa = NULL;
3787 }
3788
3789 /* FILS Session */
3790 if (!elems.fils_session) {
3791 wpa_printf(MSG_DEBUG, "FILS: No FILS Session element");
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003792 goto fail;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003793 }
3794 wpa_hexdump(MSG_DEBUG, "FILS: FILS Session", elems.fils_session,
3795 FILS_SESSION_LEN);
3796 if (os_memcmp(sm->fils_session, elems.fils_session, FILS_SESSION_LEN)
3797 != 0) {
3798 wpa_printf(MSG_DEBUG, "FILS: Session mismatch");
3799 wpa_hexdump(MSG_DEBUG, "FILS: Expected FILS Session",
3800 sm->fils_session, FILS_SESSION_LEN);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003801 goto fail;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003802 }
3803
3804 /* FILS Wrapped Data */
3805 if (!sm->cur_pmksa && elems.fils_wrapped_data) {
Paul Stewart092955c2017-02-06 09:13:09 -08003806 u8 rmsk[ERP_MAX_KEY_LEN];
3807 size_t rmsk_len;
3808
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003809 wpa_hexdump(MSG_DEBUG, "FILS: Wrapped Data",
3810 elems.fils_wrapped_data,
3811 elems.fils_wrapped_data_len);
3812 eapol_sm_process_erp_finish(sm->eapol, elems.fils_wrapped_data,
3813 elems.fils_wrapped_data_len);
3814 if (eapol_sm_failed(sm->eapol))
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003815 goto fail;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003816
Paul Stewart092955c2017-02-06 09:13:09 -08003817 rmsk_len = ERP_MAX_KEY_LEN;
3818 res = eapol_sm_get_key(sm->eapol, rmsk, rmsk_len);
3819 if (res == PMK_LEN) {
3820 rmsk_len = PMK_LEN;
3821 res = eapol_sm_get_key(sm->eapol, rmsk, rmsk_len);
3822 }
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003823 if (res)
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003824 goto fail;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003825
Paul Stewart092955c2017-02-06 09:13:09 -08003826 res = fils_rmsk_to_pmk(sm->key_mgmt, rmsk, rmsk_len,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003827 sm->fils_nonce, sm->fils_anonce,
3828 dh_ss ? wpabuf_head(dh_ss) : NULL,
3829 dh_ss ? wpabuf_len(dh_ss) : 0,
Paul Stewart092955c2017-02-06 09:13:09 -08003830 sm->pmk, &sm->pmk_len);
3831 os_memset(rmsk, 0, sizeof(rmsk));
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003832
3833 /* Don't use DHss in PTK derivation if PMKSA caching is not
3834 * used. */
3835 wpabuf_clear_free(dh_ss);
3836 dh_ss = NULL;
3837
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08003838 if (res)
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003839 goto fail;
Paul Stewart092955c2017-02-06 09:13:09 -08003840
3841 if (!sm->fils_erp_pmkid_set) {
3842 wpa_printf(MSG_DEBUG, "FILS: PMKID not available");
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003843 goto fail;
Paul Stewart092955c2017-02-06 09:13:09 -08003844 }
3845 wpa_hexdump(MSG_DEBUG, "FILS: PMKID", sm->fils_erp_pmkid,
3846 PMKID_LEN);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003847 wpa_printf(MSG_DEBUG, "FILS: ERP processing succeeded - add PMKSA cache entry for the result");
Paul Stewart092955c2017-02-06 09:13:09 -08003848 sm->cur_pmksa = pmksa_cache_add(sm->pmksa, sm->pmk, sm->pmk_len,
3849 sm->fils_erp_pmkid, NULL, 0,
3850 sm->bssid, sm->own_addr,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003851 sm->network_ctx, sm->key_mgmt,
3852 NULL);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003853 }
3854
3855 if (!sm->cur_pmksa) {
3856 wpa_printf(MSG_DEBUG,
3857 "FILS: No remaining options to continue FILS authentication");
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003858 goto fail;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003859 }
3860
3861 if (fils_pmk_to_ptk(sm->pmk, sm->pmk_len, sm->own_addr, sm->bssid,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003862 sm->fils_nonce, sm->fils_anonce,
3863 dh_ss ? wpabuf_head(dh_ss) : NULL,
3864 dh_ss ? wpabuf_len(dh_ss) : 0,
3865 &sm->ptk, ick, &ick_len,
3866 sm->key_mgmt, sm->pairwise_cipher,
3867 sm->fils_ft, &sm->fils_ft_len) < 0) {
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003868 wpa_printf(MSG_DEBUG, "FILS: Failed to derive PTK");
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003869 goto fail;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003870 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003871
3872 wpabuf_clear_free(dh_ss);
3873 dh_ss = NULL;
3874
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003875 sm->ptk_set = 1;
3876 sm->tptk_set = 0;
3877 os_memset(&sm->tptk, 0, sizeof(sm->tptk));
3878
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003879#ifdef CONFIG_FILS_SK_PFS
3880 if (sm->fils_dh_group) {
3881 if (!sm->fils_ecdh) {
3882 wpa_printf(MSG_INFO, "FILS: ECDH not initialized");
3883 goto fail;
3884 }
3885 pub = crypto_ecdh_get_pubkey(sm->fils_ecdh, 1);
3886 if (!pub)
3887 goto fail;
3888 wpa_hexdump_buf(MSG_DEBUG, "FILS: gSTA", pub);
3889 g_sta = wpabuf_head(pub);
3890 g_sta_len = wpabuf_len(pub);
3891 if (!g_ap) {
3892 wpa_printf(MSG_INFO, "FILS: gAP not available");
3893 goto fail;
3894 }
3895 wpa_hexdump(MSG_DEBUG, "FILS: gAP", g_ap, g_ap_len);
3896 }
3897#endif /* CONFIG_FILS_SK_PFS */
3898
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003899 res = fils_key_auth_sk(ick, ick_len, sm->fils_nonce,
3900 sm->fils_anonce, sm->own_addr, sm->bssid,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003901 g_sta, g_sta_len, g_ap, g_ap_len,
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003902 sm->key_mgmt, sm->fils_key_auth_sta,
3903 sm->fils_key_auth_ap,
3904 &sm->fils_key_auth_len);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003905 wpabuf_free(pub);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003906 os_memset(ick, 0, sizeof(ick));
3907 return res;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003908fail:
3909 wpabuf_free(pub);
3910 wpabuf_clear_free(dh_ss);
3911 return -1;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003912}
3913
3914
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003915#ifdef CONFIG_IEEE80211R
3916static int fils_ft_build_assoc_req_rsne(struct wpa_sm *sm, struct wpabuf *buf)
3917{
3918 struct rsn_ie_hdr *rsnie;
3919 u16 capab;
3920 u8 *pos;
Roshan Pius3a1667e2018-07-03 15:17:14 -07003921 int use_sha384 = wpa_key_mgmt_sha384(sm->key_mgmt);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003922
3923 /* RSNIE[PMKR0Name/PMKR1Name] */
3924 rsnie = wpabuf_put(buf, sizeof(*rsnie));
3925 rsnie->elem_id = WLAN_EID_RSN;
3926 WPA_PUT_LE16(rsnie->version, RSN_VERSION);
3927
3928 /* Group Suite Selector */
3929 if (!wpa_cipher_valid_group(sm->group_cipher)) {
3930 wpa_printf(MSG_WARNING, "FT: Invalid group cipher (%d)",
3931 sm->group_cipher);
3932 return -1;
3933 }
3934 pos = wpabuf_put(buf, RSN_SELECTOR_LEN);
3935 RSN_SELECTOR_PUT(pos, wpa_cipher_to_suite(WPA_PROTO_RSN,
3936 sm->group_cipher));
3937
3938 /* Pairwise Suite Count */
3939 wpabuf_put_le16(buf, 1);
3940
3941 /* Pairwise Suite List */
3942 if (!wpa_cipher_valid_pairwise(sm->pairwise_cipher)) {
3943 wpa_printf(MSG_WARNING, "FT: Invalid pairwise cipher (%d)",
3944 sm->pairwise_cipher);
3945 return -1;
3946 }
3947 pos = wpabuf_put(buf, RSN_SELECTOR_LEN);
3948 RSN_SELECTOR_PUT(pos, wpa_cipher_to_suite(WPA_PROTO_RSN,
3949 sm->pairwise_cipher));
3950
3951 /* Authenticated Key Management Suite Count */
3952 wpabuf_put_le16(buf, 1);
3953
3954 /* Authenticated Key Management Suite List */
3955 pos = wpabuf_put(buf, RSN_SELECTOR_LEN);
3956 if (sm->key_mgmt == WPA_KEY_MGMT_FT_FILS_SHA256)
3957 RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_FT_FILS_SHA256);
3958 else if (sm->key_mgmt == WPA_KEY_MGMT_FT_FILS_SHA384)
3959 RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_FT_FILS_SHA384);
3960 else {
3961 wpa_printf(MSG_WARNING,
3962 "FILS+FT: Invalid key management type (%d)",
3963 sm->key_mgmt);
3964 return -1;
3965 }
3966
3967 /* RSN Capabilities */
3968 capab = 0;
3969#ifdef CONFIG_IEEE80211W
3970 if (sm->mgmt_group_cipher == WPA_CIPHER_AES_128_CMAC)
3971 capab |= WPA_CAPABILITY_MFPC;
3972#endif /* CONFIG_IEEE80211W */
Hai Shalom74f70d42019-02-11 14:42:39 -08003973 if (sm->ocv)
3974 capab |= WPA_CAPABILITY_OCVC;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003975 wpabuf_put_le16(buf, capab);
3976
3977 /* PMKID Count */
3978 wpabuf_put_le16(buf, 1);
3979
3980 /* PMKID List [PMKR1Name] */
3981 wpa_hexdump_key(MSG_DEBUG, "FILS+FT: XXKey (FILS-FT)",
3982 sm->fils_ft, sm->fils_ft_len);
3983 wpa_hexdump_ascii(MSG_DEBUG, "FILS+FT: SSID", sm->ssid, sm->ssid_len);
3984 wpa_hexdump(MSG_DEBUG, "FILS+FT: MDID",
3985 sm->mobility_domain, MOBILITY_DOMAIN_ID_LEN);
3986 wpa_hexdump_ascii(MSG_DEBUG, "FILS+FT: R0KH-ID",
3987 sm->r0kh_id, sm->r0kh_id_len);
3988 if (wpa_derive_pmk_r0(sm->fils_ft, sm->fils_ft_len, sm->ssid,
3989 sm->ssid_len, sm->mobility_domain,
3990 sm->r0kh_id, sm->r0kh_id_len, sm->own_addr,
Roshan Pius3a1667e2018-07-03 15:17:14 -07003991 sm->pmk_r0, sm->pmk_r0_name, use_sha384) < 0) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003992 wpa_printf(MSG_WARNING, "FILS+FT: Could not derive PMK-R0");
3993 return -1;
3994 }
Roshan Pius3a1667e2018-07-03 15:17:14 -07003995 sm->pmk_r0_len = use_sha384 ? SHA384_MAC_LEN : PMK_LEN;
3996 wpa_hexdump_key(MSG_DEBUG, "FILS+FT: PMK-R0",
3997 sm->pmk_r0, sm->pmk_r0_len);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003998 wpa_hexdump(MSG_DEBUG, "FILS+FT: PMKR0Name",
3999 sm->pmk_r0_name, WPA_PMK_NAME_LEN);
4000 wpa_printf(MSG_DEBUG, "FILS+FT: R1KH-ID: " MACSTR,
4001 MAC2STR(sm->r1kh_id));
4002 pos = wpabuf_put(buf, WPA_PMK_NAME_LEN);
4003 if (wpa_derive_pmk_r1_name(sm->pmk_r0_name, sm->r1kh_id, sm->own_addr,
Hai Shalom021b0b52019-04-10 11:17:58 -07004004 sm->pmk_r1_name, use_sha384) < 0) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004005 wpa_printf(MSG_WARNING, "FILS+FT: Could not derive PMKR1Name");
4006 return -1;
4007 }
Hai Shalom021b0b52019-04-10 11:17:58 -07004008 wpa_hexdump(MSG_DEBUG, "FILS+FT: PMKR1Name", sm->pmk_r1_name,
4009 WPA_PMK_NAME_LEN);
4010 os_memcpy(pos, sm->pmk_r1_name, WPA_PMK_NAME_LEN);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004011
4012#ifdef CONFIG_IEEE80211W
4013 if (sm->mgmt_group_cipher == WPA_CIPHER_AES_128_CMAC) {
4014 /* Management Group Cipher Suite */
4015 pos = wpabuf_put(buf, RSN_SELECTOR_LEN);
4016 RSN_SELECTOR_PUT(pos, RSN_CIPHER_SUITE_AES_128_CMAC);
4017 }
4018#endif /* CONFIG_IEEE80211W */
4019
4020 rsnie->len = ((u8 *) wpabuf_put(buf, 0) - (u8 *) rsnie) - 2;
4021 return 0;
4022}
4023#endif /* CONFIG_IEEE80211R */
4024
4025
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004026struct wpabuf * fils_build_assoc_req(struct wpa_sm *sm, const u8 **kek,
4027 size_t *kek_len, const u8 **snonce,
Paul Stewart092955c2017-02-06 09:13:09 -08004028 const u8 **anonce,
4029 const struct wpabuf **hlp,
4030 unsigned int num_hlp)
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004031{
4032 struct wpabuf *buf;
Paul Stewart092955c2017-02-06 09:13:09 -08004033 size_t len;
4034 unsigned int i;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004035
Paul Stewart092955c2017-02-06 09:13:09 -08004036 len = 1000;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004037#ifdef CONFIG_IEEE80211R
4038 if (sm->fils_ft_ies)
4039 len += wpabuf_len(sm->fils_ft_ies);
4040 if (wpa_key_mgmt_ft(sm->key_mgmt))
4041 len += 256;
4042#endif /* CONFIG_IEEE80211R */
Paul Stewart092955c2017-02-06 09:13:09 -08004043 for (i = 0; hlp && i < num_hlp; i++)
4044 len += 10 + wpabuf_len(hlp[i]);
4045 buf = wpabuf_alloc(len);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004046 if (!buf)
4047 return NULL;
4048
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004049#ifdef CONFIG_IEEE80211R
4050 if (wpa_key_mgmt_ft(sm->key_mgmt) && sm->fils_ft_ies) {
4051 /* MDE and FTE when using FILS+FT */
4052 wpabuf_put_buf(buf, sm->fils_ft_ies);
4053 /* RSNE with PMKR1Name in PMKID field */
4054 if (fils_ft_build_assoc_req_rsne(sm, buf) < 0) {
4055 wpabuf_free(buf);
4056 return NULL;
4057 }
4058 }
4059#endif /* CONFIG_IEEE80211R */
4060
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004061 /* FILS Session */
4062 wpabuf_put_u8(buf, WLAN_EID_EXTENSION); /* Element ID */
4063 wpabuf_put_u8(buf, 1 + FILS_SESSION_LEN); /* Length */
4064 /* Element ID Extension */
4065 wpabuf_put_u8(buf, WLAN_EID_EXT_FILS_SESSION);
4066 wpabuf_put_data(buf, sm->fils_session, FILS_SESSION_LEN);
4067
4068 /* Everything after FILS Session element gets encrypted in the driver
4069 * with KEK. The buffer returned from here is the plaintext version. */
4070
4071 /* TODO: FILS Public Key */
4072
4073 /* FILS Key Confirm */
4074 wpabuf_put_u8(buf, WLAN_EID_EXTENSION); /* Element ID */
4075 wpabuf_put_u8(buf, 1 + sm->fils_key_auth_len); /* Length */
4076 /* Element ID Extension */
4077 wpabuf_put_u8(buf, WLAN_EID_EXT_FILS_KEY_CONFIRM);
4078 wpabuf_put_data(buf, sm->fils_key_auth_sta, sm->fils_key_auth_len);
4079
Paul Stewart092955c2017-02-06 09:13:09 -08004080 /* FILS HLP Container */
4081 for (i = 0; hlp && i < num_hlp; i++) {
4082 const u8 *pos = wpabuf_head(hlp[i]);
4083 size_t left = wpabuf_len(hlp[i]);
4084
4085 wpabuf_put_u8(buf, WLAN_EID_EXTENSION); /* Element ID */
4086 if (left <= 254)
4087 len = 1 + left;
4088 else
4089 len = 255;
4090 wpabuf_put_u8(buf, len); /* Length */
4091 /* Element ID Extension */
4092 wpabuf_put_u8(buf, WLAN_EID_EXT_FILS_HLP_CONTAINER);
4093 /* Destination MAC Address, Source MAC Address, HLP Packet.
4094 * HLP Packet is in MSDU format (i.e., included the LLC/SNAP
4095 * header when LPD is used). */
4096 wpabuf_put_data(buf, pos, len - 1);
4097 pos += len - 1;
4098 left -= len - 1;
4099 while (left) {
4100 wpabuf_put_u8(buf, WLAN_EID_FRAGMENT);
4101 len = left > 255 ? 255 : left;
4102 wpabuf_put_u8(buf, len);
4103 wpabuf_put_data(buf, pos, len);
4104 pos += len;
4105 left -= len;
4106 }
4107 }
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004108
4109 /* TODO: FILS IP Address Assignment */
4110
Hai Shalom74f70d42019-02-11 14:42:39 -08004111#ifdef CONFIG_OCV
4112 if (wpa_sm_ocv_enabled(sm)) {
4113 struct wpa_channel_info ci;
4114 u8 *pos;
4115
4116 if (wpa_sm_channel_info(sm, &ci) != 0) {
4117 wpa_printf(MSG_WARNING,
4118 "FILS: Failed to get channel info for OCI element");
4119 wpabuf_free(buf);
4120 return NULL;
4121 }
4122
4123 pos = wpabuf_put(buf, OCV_OCI_EXTENDED_LEN);
4124 if (ocv_insert_extended_oci(&ci, pos) < 0) {
4125 wpabuf_free(buf);
4126 return NULL;
4127 }
4128 }
4129#endif /* CONFIG_OCV */
4130
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004131 wpa_hexdump_buf(MSG_DEBUG, "FILS: Association Request plaintext", buf);
4132
4133 *kek = sm->ptk.kek;
4134 *kek_len = sm->ptk.kek_len;
4135 wpa_hexdump_key(MSG_DEBUG, "FILS: KEK for AEAD", *kek, *kek_len);
4136 *snonce = sm->fils_nonce;
4137 wpa_hexdump(MSG_DEBUG, "FILS: SNonce for AEAD AAD",
4138 *snonce, FILS_NONCE_LEN);
4139 *anonce = sm->fils_anonce;
4140 wpa_hexdump(MSG_DEBUG, "FILS: ANonce for AEAD AAD",
4141 *anonce, FILS_NONCE_LEN);
4142
4143 return buf;
4144}
4145
4146
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08004147static void fils_process_hlp_resp(struct wpa_sm *sm, const u8 *resp, size_t len)
4148{
4149 const u8 *pos, *end;
4150
4151 wpa_hexdump(MSG_MSGDUMP, "FILS: HLP response", resp, len);
4152 if (len < 2 * ETH_ALEN)
4153 return;
4154 pos = resp + 2 * ETH_ALEN;
4155 end = resp + len;
4156 if (end - pos >= 6 &&
4157 os_memcmp(pos, "\xaa\xaa\x03\x00\x00\x00", 6) == 0)
4158 pos += 6; /* Remove SNAP/LLC header */
4159 wpa_sm_fils_hlp_rx(sm, resp, resp + ETH_ALEN, pos, end - pos);
4160}
4161
4162
4163static void fils_process_hlp_container(struct wpa_sm *sm, const u8 *pos,
4164 size_t len)
4165{
4166 const u8 *end = pos + len;
4167 u8 *tmp, *tmp_pos;
4168
4169 /* Check if there are any FILS HLP Container elements */
4170 while (end - pos >= 2) {
4171 if (2 + pos[1] > end - pos)
4172 return;
4173 if (pos[0] == WLAN_EID_EXTENSION &&
4174 pos[1] >= 1 + 2 * ETH_ALEN &&
4175 pos[2] == WLAN_EID_EXT_FILS_HLP_CONTAINER)
4176 break;
4177 pos += 2 + pos[1];
4178 }
4179 if (end - pos < 2)
4180 return; /* No FILS HLP Container elements */
4181
4182 tmp = os_malloc(end - pos);
4183 if (!tmp)
4184 return;
4185
4186 while (end - pos >= 2) {
4187 if (2 + pos[1] > end - pos ||
4188 pos[0] != WLAN_EID_EXTENSION ||
4189 pos[1] < 1 + 2 * ETH_ALEN ||
4190 pos[2] != WLAN_EID_EXT_FILS_HLP_CONTAINER)
4191 break;
4192 tmp_pos = tmp;
4193 os_memcpy(tmp_pos, pos + 3, pos[1] - 1);
4194 tmp_pos += pos[1] - 1;
4195 pos += 2 + pos[1];
4196
4197 /* Add possible fragments */
4198 while (end - pos >= 2 && pos[0] == WLAN_EID_FRAGMENT &&
4199 2 + pos[1] <= end - pos) {
4200 os_memcpy(tmp_pos, pos + 2, pos[1]);
4201 tmp_pos += pos[1];
4202 pos += 2 + pos[1];
4203 }
4204
4205 fils_process_hlp_resp(sm, tmp, tmp_pos - tmp);
4206 }
4207
4208 os_free(tmp);
4209}
4210
4211
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004212int fils_process_assoc_resp(struct wpa_sm *sm, const u8 *resp, size_t len)
4213{
4214 const struct ieee80211_mgmt *mgmt;
4215 const u8 *end, *ie_start;
4216 struct ieee802_11_elems elems;
4217 int keylen, rsclen;
4218 enum wpa_alg alg;
4219 struct wpa_gtk_data gd;
4220 int maxkeylen;
4221 struct wpa_eapol_ie_parse kde;
4222
4223 if (!sm || !sm->ptk_set) {
4224 wpa_printf(MSG_DEBUG, "FILS: No KEK available");
4225 return -1;
4226 }
4227
4228 if (!wpa_key_mgmt_fils(sm->key_mgmt)) {
4229 wpa_printf(MSG_DEBUG, "FILS: Not a FILS AKM");
4230 return -1;
4231 }
4232
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004233 if (sm->fils_completed) {
4234 wpa_printf(MSG_DEBUG,
4235 "FILS: Association has already been completed for this FILS authentication - ignore unexpected retransmission");
4236 return -1;
4237 }
4238
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004239 wpa_hexdump(MSG_DEBUG, "FILS: (Re)Association Response frame",
4240 resp, len);
4241
4242 mgmt = (const struct ieee80211_mgmt *) resp;
4243 if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.assoc_resp))
4244 return -1;
4245
4246 end = resp + len;
4247 /* Same offset for Association Response and Reassociation Response */
4248 ie_start = mgmt->u.assoc_resp.variable;
4249
4250 if (ieee802_11_parse_elems(ie_start, end - ie_start, &elems, 1) ==
4251 ParseFailed) {
4252 wpa_printf(MSG_DEBUG,
4253 "FILS: Failed to parse decrypted elements");
4254 goto fail;
4255 }
4256
4257 if (!elems.fils_session) {
4258 wpa_printf(MSG_DEBUG, "FILS: No FILS Session element");
4259 return -1;
4260 }
4261 if (os_memcmp(elems.fils_session, sm->fils_session,
4262 FILS_SESSION_LEN) != 0) {
4263 wpa_printf(MSG_DEBUG, "FILS: FILS Session mismatch");
4264 wpa_hexdump(MSG_DEBUG, "FILS: Received FILS Session",
4265 elems.fils_session, FILS_SESSION_LEN);
4266 wpa_hexdump(MSG_DEBUG, "FILS: Expected FILS Session",
4267 sm->fils_session, FILS_SESSION_LEN);
4268 }
4269
4270 /* TODO: FILS Public Key */
4271
4272 if (!elems.fils_key_confirm) {
4273 wpa_printf(MSG_DEBUG, "FILS: No FILS Key Confirm element");
4274 goto fail;
4275 }
4276 if (elems.fils_key_confirm_len != sm->fils_key_auth_len) {
4277 wpa_printf(MSG_DEBUG,
4278 "FILS: Unexpected Key-Auth length %d (expected %d)",
4279 elems.fils_key_confirm_len,
4280 (int) sm->fils_key_auth_len);
4281 goto fail;
4282 }
4283 if (os_memcmp(elems.fils_key_confirm, sm->fils_key_auth_ap,
4284 sm->fils_key_auth_len) != 0) {
4285 wpa_printf(MSG_DEBUG, "FILS: Key-Auth mismatch");
4286 wpa_hexdump(MSG_DEBUG, "FILS: Received Key-Auth",
4287 elems.fils_key_confirm,
4288 elems.fils_key_confirm_len);
4289 wpa_hexdump(MSG_DEBUG, "FILS: Expected Key-Auth",
4290 sm->fils_key_auth_ap, sm->fils_key_auth_len);
4291 goto fail;
4292 }
4293
Hai Shalom74f70d42019-02-11 14:42:39 -08004294#ifdef CONFIG_OCV
4295 if (wpa_sm_ocv_enabled(sm)) {
4296 struct wpa_channel_info ci;
4297
4298 if (wpa_sm_channel_info(sm, &ci) != 0) {
4299 wpa_printf(MSG_WARNING,
4300 "Failed to get channel info to validate received OCI in FILS (Re)Association Response frame");
4301 goto fail;
4302 }
4303
4304 if (ocv_verify_tx_params(elems.oci, elems.oci_len, &ci,
4305 channel_width_to_int(ci.chanwidth),
4306 ci.seg1_idx) != 0) {
4307 wpa_printf(MSG_WARNING, "FILS: %s", ocv_errorstr);
4308 goto fail;
4309 }
4310 }
4311#endif /* CONFIG_OCV */
4312
Hai Shalom021b0b52019-04-10 11:17:58 -07004313#ifdef CONFIG_IEEE80211R
4314 if (wpa_key_mgmt_ft(sm->key_mgmt) && sm->fils_ft_ies) {
4315 struct wpa_ie_data rsn;
4316
4317 /* Check that PMKR1Name derived by the AP matches */
4318 if (!elems.rsn_ie ||
4319 wpa_parse_wpa_ie_rsn(elems.rsn_ie - 2, elems.rsn_ie_len + 2,
4320 &rsn) < 0 ||
4321 !rsn.pmkid || rsn.num_pmkid != 1 ||
4322 os_memcmp(rsn.pmkid, sm->pmk_r1_name,
4323 WPA_PMK_NAME_LEN) != 0) {
4324 wpa_printf(MSG_DEBUG,
4325 "FILS+FT: No RSNE[PMKR1Name] match in AssocResp");
4326 goto fail;
4327 }
4328 }
4329#endif /* CONFIG_IEEE80211R */
4330
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004331 /* Key Delivery */
4332 if (!elems.key_delivery) {
4333 wpa_printf(MSG_DEBUG, "FILS: No Key Delivery element");
4334 goto fail;
4335 }
4336
4337 /* Parse GTK and set the key to the driver */
4338 os_memset(&gd, 0, sizeof(gd));
4339 if (wpa_supplicant_parse_ies(elems.key_delivery + WPA_KEY_RSC_LEN,
4340 elems.key_delivery_len - WPA_KEY_RSC_LEN,
4341 &kde) < 0) {
4342 wpa_printf(MSG_DEBUG, "FILS: Failed to parse KDEs");
4343 goto fail;
4344 }
4345 if (!kde.gtk) {
4346 wpa_printf(MSG_DEBUG, "FILS: No GTK KDE");
4347 goto fail;
4348 }
4349 maxkeylen = gd.gtk_len = kde.gtk_len - 2;
4350 if (wpa_supplicant_check_group_cipher(sm, sm->group_cipher,
4351 gd.gtk_len, maxkeylen,
4352 &gd.key_rsc_len, &gd.alg))
4353 goto fail;
4354
4355 wpa_hexdump_key(MSG_DEBUG, "FILS: Received GTK", kde.gtk, kde.gtk_len);
4356 gd.keyidx = kde.gtk[0] & 0x3;
4357 gd.tx = wpa_supplicant_gtk_tx_bit_workaround(sm,
4358 !!(kde.gtk[0] & BIT(2)));
4359 if (kde.gtk_len - 2 > sizeof(gd.gtk)) {
4360 wpa_printf(MSG_DEBUG, "FILS: Too long GTK in GTK KDE (len=%lu)",
4361 (unsigned long) kde.gtk_len - 2);
4362 goto fail;
4363 }
4364 os_memcpy(gd.gtk, kde.gtk + 2, kde.gtk_len - 2);
4365
4366 wpa_printf(MSG_DEBUG, "FILS: Set GTK to driver");
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004367 if (wpa_supplicant_install_gtk(sm, &gd, elems.key_delivery, 0) < 0) {
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004368 wpa_printf(MSG_DEBUG, "FILS: Failed to set GTK");
4369 goto fail;
4370 }
4371
4372 if (ieee80211w_set_keys(sm, &kde) < 0) {
4373 wpa_printf(MSG_DEBUG, "FILS: Failed to set IGTK");
4374 goto fail;
4375 }
4376
4377 alg = wpa_cipher_to_alg(sm->pairwise_cipher);
4378 keylen = wpa_cipher_key_len(sm->pairwise_cipher);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004379 if (keylen <= 0 || (unsigned int) keylen != sm->ptk.tk_len) {
4380 wpa_printf(MSG_DEBUG, "FILS: TK length mismatch: %u != %lu",
4381 keylen, (long unsigned int) sm->ptk.tk_len);
4382 goto fail;
4383 }
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004384 rsclen = wpa_cipher_rsc_len(sm->pairwise_cipher);
4385 wpa_hexdump_key(MSG_DEBUG, "FILS: Set TK to driver",
4386 sm->ptk.tk, keylen);
4387 if (wpa_sm_set_key(sm, alg, sm->bssid, 0, 1, null_rsc, rsclen,
4388 sm->ptk.tk, keylen) < 0) {
4389 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
4390 "FILS: Failed to set PTK to the driver (alg=%d keylen=%d bssid="
4391 MACSTR ")",
4392 alg, keylen, MAC2STR(sm->bssid));
4393 goto fail;
4394 }
4395
4396 /* TODO: TK could be cleared after auth frame exchange now that driver
4397 * takes care of association frame encryption/decryption. */
4398 /* TK is not needed anymore in supplicant */
4399 os_memset(sm->ptk.tk, 0, WPA_TK_MAX_LEN);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004400 sm->ptk.tk_len = 0;
Mathy Vanhoefc66556c2017-09-29 04:22:51 +02004401 sm->ptk.installed = 1;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004402
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08004403 /* FILS HLP Container */
4404 fils_process_hlp_container(sm, ie_start, end - ie_start);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004405
4406 /* TODO: FILS IP Address Assignment */
4407
4408 wpa_printf(MSG_DEBUG, "FILS: Auth+Assoc completed successfully");
4409 sm->fils_completed = 1;
4410
4411 return 0;
4412fail:
4413 return -1;
4414}
4415
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004416
4417void wpa_sm_set_reset_fils_completed(struct wpa_sm *sm, int set)
4418{
4419 if (sm)
4420 sm->fils_completed = !!set;
4421}
4422
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004423#endif /* CONFIG_FILS */
4424
4425
4426int wpa_fils_is_completed(struct wpa_sm *sm)
4427{
4428#ifdef CONFIG_FILS
4429 return sm && sm->fils_completed;
4430#else /* CONFIG_FILS */
4431 return 0;
4432#endif /* CONFIG_FILS */
4433}
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004434
4435
4436#ifdef CONFIG_OWE
4437
4438struct wpabuf * owe_build_assoc_req(struct wpa_sm *sm, u16 group)
4439{
4440 struct wpabuf *ie = NULL, *pub = NULL;
4441 size_t prime_len;
4442
4443 if (group == 19)
4444 prime_len = 32;
4445 else if (group == 20)
4446 prime_len = 48;
4447 else if (group == 21)
4448 prime_len = 66;
4449 else
4450 return NULL;
4451
4452 crypto_ecdh_deinit(sm->owe_ecdh);
4453 sm->owe_ecdh = crypto_ecdh_init(group);
4454 if (!sm->owe_ecdh)
4455 goto fail;
4456 sm->owe_group = group;
4457 pub = crypto_ecdh_get_pubkey(sm->owe_ecdh, 0);
4458 pub = wpabuf_zeropad(pub, prime_len);
4459 if (!pub)
4460 goto fail;
4461
4462 ie = wpabuf_alloc(5 + wpabuf_len(pub));
4463 if (!ie)
4464 goto fail;
4465 wpabuf_put_u8(ie, WLAN_EID_EXTENSION);
4466 wpabuf_put_u8(ie, 1 + 2 + wpabuf_len(pub));
4467 wpabuf_put_u8(ie, WLAN_EID_EXT_OWE_DH_PARAM);
4468 wpabuf_put_le16(ie, group);
4469 wpabuf_put_buf(ie, pub);
4470 wpabuf_free(pub);
4471 wpa_hexdump_buf(MSG_DEBUG, "OWE: Diffie-Hellman Parameter element",
4472 ie);
4473
4474 return ie;
4475fail:
4476 wpabuf_free(pub);
4477 crypto_ecdh_deinit(sm->owe_ecdh);
4478 sm->owe_ecdh = NULL;
4479 return NULL;
4480}
4481
4482
4483int owe_process_assoc_resp(struct wpa_sm *sm, const u8 *bssid,
4484 const u8 *resp_ies, size_t resp_ies_len)
4485{
4486 struct ieee802_11_elems elems;
4487 u16 group;
4488 struct wpabuf *secret, *pub, *hkey;
4489 int res;
4490 u8 prk[SHA512_MAC_LEN], pmkid[SHA512_MAC_LEN];
4491 const char *info = "OWE Key Generation";
4492 const u8 *addr[2];
4493 size_t len[2];
4494 size_t hash_len, prime_len;
4495 struct wpa_ie_data data;
4496
4497 if (!resp_ies ||
4498 ieee802_11_parse_elems(resp_ies, resp_ies_len, &elems, 1) ==
4499 ParseFailed) {
4500 wpa_printf(MSG_INFO,
4501 "OWE: Could not parse Association Response frame elements");
4502 return -1;
4503 }
4504
4505 if (sm->cur_pmksa && elems.rsn_ie &&
4506 wpa_parse_wpa_ie_rsn(elems.rsn_ie - 2, 2 + elems.rsn_ie_len,
4507 &data) == 0 &&
4508 data.num_pmkid == 1 && data.pmkid &&
4509 os_memcmp(sm->cur_pmksa->pmkid, data.pmkid, PMKID_LEN) == 0) {
4510 wpa_printf(MSG_DEBUG, "OWE: Use PMKSA caching");
4511 wpa_sm_set_pmk_from_pmksa(sm);
4512 return 0;
4513 }
4514
4515 if (!elems.owe_dh) {
4516 wpa_printf(MSG_INFO,
4517 "OWE: No Diffie-Hellman Parameter element found in Association Response frame");
4518 return -1;
4519 }
4520
4521 group = WPA_GET_LE16(elems.owe_dh);
4522 if (group != sm->owe_group) {
4523 wpa_printf(MSG_INFO,
4524 "OWE: Unexpected Diffie-Hellman group in response: %u",
4525 group);
4526 return -1;
4527 }
4528
4529 if (!sm->owe_ecdh) {
4530 wpa_printf(MSG_INFO, "OWE: No ECDH state available");
4531 return -1;
4532 }
4533
4534 if (group == 19)
4535 prime_len = 32;
4536 else if (group == 20)
4537 prime_len = 48;
4538 else if (group == 21)
4539 prime_len = 66;
4540 else
4541 return -1;
4542
4543 secret = crypto_ecdh_set_peerkey(sm->owe_ecdh, 0,
4544 elems.owe_dh + 2,
4545 elems.owe_dh_len - 2);
4546 secret = wpabuf_zeropad(secret, prime_len);
4547 if (!secret) {
4548 wpa_printf(MSG_DEBUG, "OWE: Invalid peer DH public key");
4549 return -1;
4550 }
4551 wpa_hexdump_buf_key(MSG_DEBUG, "OWE: DH shared secret", secret);
4552
4553 /* prk = HKDF-extract(C | A | group, z) */
4554
4555 pub = crypto_ecdh_get_pubkey(sm->owe_ecdh, 0);
4556 if (!pub) {
4557 wpabuf_clear_free(secret);
4558 return -1;
4559 }
4560
4561 /* PMKID = Truncate-128(Hash(C | A)) */
4562 addr[0] = wpabuf_head(pub);
4563 len[0] = wpabuf_len(pub);
4564 addr[1] = elems.owe_dh + 2;
4565 len[1] = elems.owe_dh_len - 2;
4566 if (group == 19) {
4567 res = sha256_vector(2, addr, len, pmkid);
4568 hash_len = SHA256_MAC_LEN;
4569 } else if (group == 20) {
4570 res = sha384_vector(2, addr, len, pmkid);
4571 hash_len = SHA384_MAC_LEN;
4572 } else if (group == 21) {
4573 res = sha512_vector(2, addr, len, pmkid);
4574 hash_len = SHA512_MAC_LEN;
4575 } else {
4576 res = -1;
4577 hash_len = 0;
4578 }
4579 pub = wpabuf_zeropad(pub, prime_len);
4580 if (res < 0 || !pub) {
4581 wpabuf_free(pub);
4582 wpabuf_clear_free(secret);
4583 return -1;
4584 }
4585
4586 hkey = wpabuf_alloc(wpabuf_len(pub) + elems.owe_dh_len - 2 + 2);
4587 if (!hkey) {
4588 wpabuf_free(pub);
4589 wpabuf_clear_free(secret);
4590 return -1;
4591 }
4592
4593 wpabuf_put_buf(hkey, pub); /* C */
4594 wpabuf_free(pub);
4595 wpabuf_put_data(hkey, elems.owe_dh + 2, elems.owe_dh_len - 2); /* A */
4596 wpabuf_put_le16(hkey, sm->owe_group); /* group */
4597 if (group == 19)
4598 res = hmac_sha256(wpabuf_head(hkey), wpabuf_len(hkey),
4599 wpabuf_head(secret), wpabuf_len(secret), prk);
4600 else if (group == 20)
4601 res = hmac_sha384(wpabuf_head(hkey), wpabuf_len(hkey),
4602 wpabuf_head(secret), wpabuf_len(secret), prk);
4603 else if (group == 21)
4604 res = hmac_sha512(wpabuf_head(hkey), wpabuf_len(hkey),
4605 wpabuf_head(secret), wpabuf_len(secret), prk);
4606 wpabuf_clear_free(hkey);
4607 wpabuf_clear_free(secret);
4608 if (res < 0)
4609 return -1;
4610
4611 wpa_hexdump_key(MSG_DEBUG, "OWE: prk", prk, hash_len);
4612
4613 /* PMK = HKDF-expand(prk, "OWE Key Generation", n) */
4614
4615 if (group == 19)
4616 res = hmac_sha256_kdf(prk, hash_len, NULL, (const u8 *) info,
4617 os_strlen(info), sm->pmk, hash_len);
4618 else if (group == 20)
4619 res = hmac_sha384_kdf(prk, hash_len, NULL, (const u8 *) info,
4620 os_strlen(info), sm->pmk, hash_len);
4621 else if (group == 21)
4622 res = hmac_sha512_kdf(prk, hash_len, NULL, (const u8 *) info,
4623 os_strlen(info), sm->pmk, hash_len);
4624 os_memset(prk, 0, SHA512_MAC_LEN);
Roshan Pius3a1667e2018-07-03 15:17:14 -07004625 if (res < 0) {
4626 sm->pmk_len = 0;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004627 return -1;
Roshan Pius3a1667e2018-07-03 15:17:14 -07004628 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004629 sm->pmk_len = hash_len;
4630
4631 wpa_hexdump_key(MSG_DEBUG, "OWE: PMK", sm->pmk, sm->pmk_len);
4632 wpa_hexdump(MSG_DEBUG, "OWE: PMKID", pmkid, PMKID_LEN);
4633 pmksa_cache_add(sm->pmksa, sm->pmk, sm->pmk_len, pmkid, NULL, 0,
4634 bssid, sm->own_addr, sm->network_ctx, sm->key_mgmt,
4635 NULL);
4636
4637 return 0;
4638}
4639
4640#endif /* CONFIG_OWE */
4641
4642
4643void wpa_sm_set_fils_cache_id(struct wpa_sm *sm, const u8 *fils_cache_id)
4644{
4645#ifdef CONFIG_FILS
4646 if (sm && fils_cache_id) {
4647 sm->fils_cache_id_set = 1;
4648 os_memcpy(sm->fils_cache_id, fils_cache_id, FILS_CACHE_ID_LEN);
4649 }
4650#endif /* CONFIG_FILS */
4651}
Hai Shalom021b0b52019-04-10 11:17:58 -07004652
4653
4654#ifdef CONFIG_DPP2
4655void wpa_sm_set_dpp_z(struct wpa_sm *sm, const struct wpabuf *z)
4656{
4657 if (sm) {
4658 wpabuf_clear_free(sm->dpp_z);
4659 sm->dpp_z = z ? wpabuf_dup(z) : NULL;
4660 }
4661}
4662#endif /* CONFIG_DPP2 */