]> git.ipfire.org Git - thirdparty/hostap.git/blame - src/rsn_supp/wpa.c
wpa_supplicant configuration for Beacon protection
[thirdparty/hostap.git] / src / rsn_supp / wpa.c
CommitLineData
6fc6879b
JM
1/*
2 * WPA Supplicant - WPA state machine and EAPOL-Key processing
a3e18dbb 3 * Copyright (c) 2003-2018, Jouni Malinen <j@w1.fi>
73ed03f3 4 * Copyright(c) 2015 Intel Deutschland GmbH
6fc6879b 5 *
0f3d578e
JM
6 * This software may be distributed under the terms of the BSD license.
7 * See README for more details.
6fc6879b
JM
8 */
9
10#include "includes.h"
11
12#include "common.h"
2022f1d0 13#include "crypto/aes.h"
03da66bd
JM
14#include "crypto/aes_wrap.h"
15#include "crypto/crypto.h"
3642c431 16#include "crypto/random.h"
2022f1d0 17#include "crypto/aes_siv.h"
0a614799 18#include "crypto/sha256.h"
ec9f4837
JM
19#include "crypto/sha384.h"
20#include "crypto/sha512.h"
03da66bd 21#include "common/ieee802_11_defs.h"
a6609937 22#include "common/ieee802_11_common.h"
17c2559c 23#include "common/ocv.h"
fcd3d6ce 24#include "eap_common/eap_defs.h"
03da66bd 25#include "eapol_supp/eapol_supp_sm.h"
17c2559c 26#include "drivers/driver.h"
6fc6879b
JM
27#include "wpa.h"
28#include "eloop.h"
6fc6879b
JM
29#include "preauth.h"
30#include "pmksa_cache.h"
31#include "wpa_i.h"
32#include "wpa_ie.h"
6fc6879b
JM
33
34
73ed03f3
MS
35static const u8 null_rsc[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
36
37
6fc6879b
JM
38/**
39 * wpa_eapol_key_send - Send WPA/RSN EAPOL-Key message
40 * @sm: Pointer to WPA state machine data from wpa_sm_init()
1049af7e 41 * @ptk: PTK for Key Confirmation/Encryption Key
6fc6879b
JM
42 * @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
c93b7e18 48 * Returns: >= 0 on success, < 0 on failure
6fc6879b 49 */
1049af7e 50int wpa_eapol_key_send(struct wpa_sm *sm, struct wpa_ptk *ptk,
c93b7e18
AA
51 int ver, const u8 *dest, u16 proto,
52 u8 *msg, size_t msg_len, u8 *key_mic)
6fc6879b 53{
c93b7e18 54 int ret = -1;
567da5bb 55 size_t mic_len = wpa_mic_len(sm->key_mgmt, sm->pmk_len);
98cd3d1c 56
ef9627cb
JM
57 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);
a8e16edc 60 if (is_zero_ether_addr(dest) && is_zero_ether_addr(sm->bssid)) {
6fc6879b
JM
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) {
f049052b
BG
66 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
67 "WPA: Failed to read BSSID for "
68 "EAPOL-Key destination address");
6fc6879b
JM
69 } else {
70 dest = sm->bssid;
f049052b
BG
71 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
72 "WPA: Use BSSID (" MACSTR
73 ") as the destination for EAPOL-Key",
74 MAC2STR(dest));
6fc6879b
JM
75 }
76 }
2022f1d0
JM
77
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 }
28fb9bb1
JM
90 if (ptk)
91 wpa_hexdump_key(MSG_DEBUG, "WPA: KCK",
92 ptk->kck, ptk->kck_len);
2022f1d0
JM
93 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 */
04b6b3ed 156 goto out;
2022f1d0 157#endif /* CONFIG_FILS */
04b6b3ed 158 }
2022f1d0 159
6fc6879b 160 wpa_hexdump(MSG_MSGDUMP, "WPA: TX EAPOL-Key", msg, msg_len);
c93b7e18 161 ret = wpa_sm_ether_send(sm, dest, proto, msg, msg_len);
6fc6879b 162 eapol_sm_notify_tx_eapol_key(sm->eapol);
04b6b3ed 163out:
6fc6879b 164 os_free(msg);
c93b7e18 165 return ret;
6fc6879b
JM
166}
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
6fc6879b
JM
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{
5e3b5197 181 size_t mic_len, hdrlen, rlen;
6fc6879b
JM
182 struct wpa_eapol_key *reply;
183 int key_info, ver;
6d014ffc 184 u8 bssid[ETH_ALEN], *rbuf, *key_mic, *mic;
6fc6879b 185
4bc801ab 186 if (wpa_use_akm_defined(sm->key_mgmt))
df0f01d9
JM
187 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))
6fc6879b 190 ver = WPA_KEY_INFO_TYPE_AES_128_CMAC;
eb7719ff 191 else if (sm->pairwise_cipher != WPA_CIPHER_TKIP)
6fc6879b
JM
192 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) {
f049052b
BG
197 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
198 "Failed to read BSSID for EAPOL-Key request");
6fc6879b
JM
199 return;
200 }
201
567da5bb 202 mic_len = wpa_mic_len(sm->key_mgmt, sm->pmk_len);
6d014ffc 203 hdrlen = sizeof(*reply) + mic_len + 2;
6fc6879b 204 rbuf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_KEY, NULL,
5e3b5197 205 hdrlen, &rlen, (void *) &reply);
6fc6879b
JM
206 if (rbuf == NULL)
207 return;
208
a14896e8
JM
209 reply->type = (sm->proto == WPA_PROTO_RSN ||
210 sm->proto == WPA_PROTO_OSEN) ?
6fc6879b
JM
211 EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA;
212 key_info = WPA_KEY_INFO_REQUEST | ver;
213 if (sm->ptk_set)
4a26ccda
JM
214 key_info |= WPA_KEY_INFO_SECURE;
215 if (sm->ptk_set && mic_len)
216 key_info |= WPA_KEY_INFO_MIC;
6fc6879b
JM
217 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
6d014ffc
JM
227 mic = (u8 *) (reply + 1);
228 WPA_PUT_BE16(mic + mic_len, 0);
5e3b5197
JM
229 if (!(key_info & WPA_KEY_INFO_MIC))
230 key_mic = NULL;
231 else
6d014ffc 232 key_mic = mic;
6fc6879b 233
f049052b
BG
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);
1049af7e
JM
238 wpa_eapol_key_send(sm, &sm->ptk, ver, bssid, ETH_P_EAPOL, rbuf, rlen,
239 key_mic);
6fc6879b
JM
240}
241
242
b41f2684
CL
243static 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
6fc6879b
JM
261static 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. */
96efeeb6 272 sm->cur_pmksa = pmksa_cache_get(sm->pmksa, src_addr, pmkid,
852b2f27 273 NULL, 0);
6fc6879b 274 if (sm->cur_pmksa) {
f049052b
BG
275 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
276 "RSN: found matching PMKID from PMKSA cache");
6fc6879b 277 } else {
f049052b
BG
278 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
279 "RSN: no matching PMKID found");
6fc6879b
JM
280 abort_cached = 1;
281 }
282 }
283
284 if (pmkid && sm->cur_pmksa &&
0d15b69f 285 os_memcmp_const(pmkid, sm->cur_pmksa->pmkid, PMKID_LEN) == 0) {
6fc6879b
JM
286 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;
a03f9d17
JM
293#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 */
6fc6879b 305#endif /* CONFIG_IEEE80211R */
56586197 306 } else if (wpa_key_mgmt_wpa_ieee8021x(sm->key_mgmt) && sm->eapol) {
6fc6879b 307 int res, pmk_len;
16c51144
JM
308#ifdef CONFIG_IEEE80211R
309 u8 buf[2 * PMK_LEN];
310#endif /* CONFIG_IEEE80211R */
207976f0 311
834c5d68 312 if (wpa_key_mgmt_sha384(sm->key_mgmt))
207976f0
JM
313 pmk_len = PMK_LEN_SUITE_B_192;
314 else
315 pmk_len = PMK_LEN;
316 res = eapol_sm_get_key(sm->eapol, sm->pmk, pmk_len);
6fc6879b 317 if (res) {
207976f0
JM
318 if (pmk_len == PMK_LEN) {
319 /*
320 * EAP-LEAP is an exception from other EAP
321 * methods: it uses only 16-byte PMK.
322 */
323 res = eapol_sm_get_key(sm->eapol, sm->pmk, 16);
324 pmk_len = 16;
325 }
16c51144 326 }
6fc6879b 327#ifdef CONFIG_IEEE80211R
16c51144
JM
328 if (res == 0 &&
329 eapol_sm_get_key(sm->eapol, buf, 2 * PMK_LEN) == 0) {
330 if (wpa_key_mgmt_sha384(sm->key_mgmt)) {
331 os_memcpy(sm->xxkey, buf, SHA384_MAC_LEN);
332 sm->xxkey_len = SHA384_MAC_LEN;
333 } else {
334 os_memcpy(sm->xxkey, buf + PMK_LEN, PMK_LEN);
335 sm->xxkey_len = PMK_LEN;
336 }
31bc66e4 337 forced_memzero(buf, sizeof(buf));
16c51144
JM
338 if (sm->proto == WPA_PROTO_RSN &&
339 wpa_key_mgmt_ft(sm->key_mgmt)) {
340 struct rsn_pmksa_cache_entry *sa = NULL;
341 const u8 *fils_cache_id = NULL;
342
343#ifdef CONFIG_FILS
344 if (sm->fils_cache_id_set)
345 fils_cache_id = sm->fils_cache_id;
346#endif /* CONFIG_FILS */
347 wpa_hexdump_key(MSG_DEBUG,
348 "FT: Cache XXKey/MPMK",
349 sm->xxkey, sm->xxkey_len);
350 sa = pmksa_cache_add(sm->pmksa,
351 sm->xxkey, sm->xxkey_len,
352 NULL, NULL, 0,
353 src_addr, sm->own_addr,
354 sm->network_ctx,
355 sm->key_mgmt,
356 fils_cache_id);
357 if (!sm->cur_pmksa)
358 sm->cur_pmksa = sa;
6fc6879b 359 }
6fc6879b 360 }
16c51144 361#endif /* CONFIG_IEEE80211R */
6fc6879b 362 if (res == 0) {
a7f10d65 363 struct rsn_pmksa_cache_entry *sa = NULL;
869af307
JM
364 const u8 *fils_cache_id = NULL;
365
366#ifdef CONFIG_FILS
367 if (sm->fils_cache_id_set)
368 fils_cache_id = sm->fils_cache_id;
369#endif /* CONFIG_FILS */
370
6fc6879b
JM
371 wpa_hexdump_key(MSG_DEBUG, "WPA: PMK from EAPOL state "
372 "machines", sm->pmk, pmk_len);
373 sm->pmk_len = pmk_len;
b41f2684 374 wpa_supplicant_key_mgmt_set_pmk(sm);
715ed737 375 if (sm->proto == WPA_PROTO_RSN &&
087a1f4e 376 !wpa_key_mgmt_suite_b(sm->key_mgmt) &&
715ed737 377 !wpa_key_mgmt_ft(sm->key_mgmt)) {
a7f10d65 378 sa = pmksa_cache_add(sm->pmksa,
70c93963 379 sm->pmk, pmk_len, NULL,
087a1f4e 380 NULL, 0,
a7f10d65
JM
381 src_addr, sm->own_addr,
382 sm->network_ctx,
869af307
JM
383 sm->key_mgmt,
384 fils_cache_id);
f5a51b58 385 }
6fc6879b 386 if (!sm->cur_pmksa && pmkid &&
852b2f27
JM
387 pmksa_cache_get(sm->pmksa, src_addr, pmkid, NULL,
388 0)) {
f049052b
BG
389 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
390 "RSN: the new PMK matches with the "
391 "PMKID");
6fc6879b 392 abort_cached = 0;
bddc51e8
JM
393 } else if (sa && !sm->cur_pmksa && pmkid) {
394 /*
395 * It looks like the authentication server
396 * derived mismatching MSK. This should not
397 * really happen, but bugs happen.. There is not
398 * much we can do here without knowing what
399 * exactly caused the server to misbehave.
400 */
62417667 401 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
bddc51e8
JM
402 "RSN: PMKID mismatch - authentication server may have derived different MSK?!");
403 return -1;
6fc6879b 404 }
a7f10d65
JM
405
406 if (!sm->cur_pmksa)
407 sm->cur_pmksa = sa;
0a1a82db
JM
408#ifdef CONFIG_IEEE80211R
409 } else if (wpa_key_mgmt_ft(sm->key_mgmt) && sm->ft_protocol) {
410 wpa_printf(MSG_DEBUG,
411 "FT: Continue 4-way handshake without PMK/PMKID for association using FT protocol");
412#endif /* CONFIG_IEEE80211R */
6fc6879b 413 } else {
0f057fb2 414 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
6fc6879b 415 "WPA: Failed to get master session key from "
f049052b
BG
416 "EAPOL state machines - key handshake "
417 "aborted");
6fc6879b 418 if (sm->cur_pmksa) {
f049052b
BG
419 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
420 "RSN: Cancelled PMKSA caching "
421 "attempt");
6fc6879b
JM
422 sm->cur_pmksa = NULL;
423 abort_cached = 1;
1ac2d4a9 424 } else if (!abort_cached) {
6fc6879b
JM
425 return -1;
426 }
427 }
428 }
429
715ed737 430 if (abort_cached && wpa_key_mgmt_wpa_ieee8021x(sm->key_mgmt) &&
087a1f4e 431 !wpa_key_mgmt_suite_b(sm->key_mgmt) &&
a14896e8
JM
432 !wpa_key_mgmt_ft(sm->key_mgmt) && sm->key_mgmt != WPA_KEY_MGMT_OSEN)
433 {
6fc6879b
JM
434 /* Send EAPOL-Start to trigger full EAP authentication. */
435 u8 *buf;
436 size_t buflen;
437
f049052b
BG
438 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
439 "RSN: no PMKSA entry found - trigger "
440 "full EAP authentication");
6fc6879b
JM
441 buf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_START,
442 NULL, 0, &buflen, NULL);
443 if (buf) {
444 wpa_sm_ether_send(sm, sm->bssid, ETH_P_EAPOL,
445 buf, buflen);
446 os_free(buf);
b4a1256d 447 return -2;
6fc6879b
JM
448 }
449
450 return -1;
451 }
452
453 return 0;
454}
455
456
457/**
458 * wpa_supplicant_send_2_of_4 - Send message 2 of WPA/RSN 4-Way Handshake
459 * @sm: Pointer to WPA state machine data from wpa_sm_init()
460 * @dst: Destination address for the frame
461 * @key: Pointer to the EAPOL-Key frame header
462 * @ver: Version bits from EAPOL-Key Key Info
463 * @nonce: Nonce value for the EAPOL-Key frame
464 * @wpa_ie: WPA/RSN IE
465 * @wpa_ie_len: Length of the WPA/RSN IE
466 * @ptk: PTK to use for keyed hash and encryption
c93b7e18 467 * Returns: >= 0 on success, < 0 on failure
6fc6879b
JM
468 */
469int wpa_supplicant_send_2_of_4(struct wpa_sm *sm, const unsigned char *dst,
470 const struct wpa_eapol_key *key,
471 int ver, const u8 *nonce,
472 const u8 *wpa_ie, size_t wpa_ie_len,
473 struct wpa_ptk *ptk)
474{
5e3b5197 475 size_t mic_len, hdrlen, rlen;
6fc6879b 476 struct wpa_eapol_key *reply;
5e3b5197 477 u8 *rbuf, *key_mic;
26e23750 478 u8 *rsn_ie_buf = NULL;
4a26ccda 479 u16 key_info;
6fc6879b
JM
480
481 if (wpa_ie == NULL) {
f049052b
BG
482 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, "WPA: No wpa_ie set - "
483 "cannot generate msg 2/4");
6fc6879b
JM
484 return -1;
485 }
486
26e23750
JM
487#ifdef CONFIG_IEEE80211R
488 if (wpa_key_mgmt_ft(sm->key_mgmt)) {
489 int res;
490
84972999
JM
491 wpa_hexdump(MSG_DEBUG, "WPA: WPA IE before FT processing",
492 wpa_ie, wpa_ie_len);
55046414
JM
493 /*
494 * Add PMKR1Name into RSN IE (PMKID-List) and add MDIE and
495 * FTIE from (Re)Association Response.
496 */
497 rsn_ie_buf = os_malloc(wpa_ie_len + 2 + 2 + PMKID_LEN +
498 sm->assoc_resp_ies_len);
26e23750
JM
499 if (rsn_ie_buf == NULL)
500 return -1;
501 os_memcpy(rsn_ie_buf, wpa_ie, wpa_ie_len);
59e78c24 502 res = wpa_insert_pmkid(rsn_ie_buf, &wpa_ie_len,
26e23750
JM
503 sm->pmk_r1_name);
504 if (res < 0) {
505 os_free(rsn_ie_buf);
506 return -1;
507 }
84972999
JM
508 wpa_hexdump(MSG_DEBUG,
509 "WPA: WPA IE after PMKID[PMKR1Name] addition into RSNE",
510 rsn_ie_buf, wpa_ie_len);
55046414
JM
511
512 if (sm->assoc_resp_ies) {
84972999
JM
513 wpa_hexdump(MSG_DEBUG, "WPA: Add assoc_resp_ies",
514 sm->assoc_resp_ies,
515 sm->assoc_resp_ies_len);
55046414
JM
516 os_memcpy(rsn_ie_buf + wpa_ie_len, sm->assoc_resp_ies,
517 sm->assoc_resp_ies_len);
518 wpa_ie_len += sm->assoc_resp_ies_len;
519 }
26e23750
JM
520
521 wpa_ie = rsn_ie_buf;
26e23750
JM
522 }
523#endif /* CONFIG_IEEE80211R */
524
6fc6879b
JM
525 wpa_hexdump(MSG_DEBUG, "WPA: WPA IE for msg 2/4", wpa_ie, wpa_ie_len);
526
567da5bb 527 mic_len = wpa_mic_len(sm->key_mgmt, sm->pmk_len);
6d014ffc 528 hdrlen = sizeof(*reply) + mic_len + 2;
6fc6879b 529 rbuf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_KEY,
5e3b5197 530 NULL, hdrlen + wpa_ie_len,
6fc6879b 531 &rlen, (void *) &reply);
26e23750
JM
532 if (rbuf == NULL) {
533 os_free(rsn_ie_buf);
6fc6879b 534 return -1;
26e23750 535 }
6fc6879b 536
a14896e8
JM
537 reply->type = (sm->proto == WPA_PROTO_RSN ||
538 sm->proto == WPA_PROTO_OSEN) ?
6fc6879b 539 EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA;
4a26ccda
JM
540 key_info = ver | WPA_KEY_INFO_KEY_TYPE;
541 if (mic_len)
542 key_info |= WPA_KEY_INFO_MIC;
2022f1d0
JM
543 else
544 key_info |= WPA_KEY_INFO_ENCR_KEY_DATA;
4a26ccda 545 WPA_PUT_BE16(reply->key_info, key_info);
a14896e8 546 if (sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN)
6fc6879b
JM
547 WPA_PUT_BE16(reply->key_length, 0);
548 else
549 os_memcpy(reply->key_length, key->key_length, 2);
550 os_memcpy(reply->replay_counter, key->replay_counter,
551 WPA_REPLAY_COUNTER_LEN);
bc8318ac
JM
552 wpa_hexdump(MSG_DEBUG, "WPA: Replay Counter", reply->replay_counter,
553 WPA_REPLAY_COUNTER_LEN);
6fc6879b 554
6d014ffc
JM
555 key_mic = (u8 *) (reply + 1);
556 WPA_PUT_BE16(key_mic + mic_len, wpa_ie_len); /* Key Data Length */
557 os_memcpy(key_mic + mic_len + 2, wpa_ie, wpa_ie_len); /* Key Data */
26e23750 558 os_free(rsn_ie_buf);
6fc6879b
JM
559
560 os_memcpy(reply->key_nonce, nonce, WPA_NONCE_LEN);
561
f049052b 562 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Sending EAPOL-Key 2/4");
1049af7e
JM
563 return wpa_eapol_key_send(sm, ptk, ver, dst, ETH_P_EAPOL, rbuf, rlen,
564 key_mic);
6fc6879b
JM
565}
566
567
568static int wpa_derive_ptk(struct wpa_sm *sm, const unsigned char *src_addr,
98cd3d1c 569 const struct wpa_eapol_key *key, struct wpa_ptk *ptk)
6fc6879b 570{
10ec6a5f
JM
571 const u8 *z = NULL;
572 size_t z_len = 0;
8b138d28 573 int akmp;
10ec6a5f 574
6fc6879b 575#ifdef CONFIG_IEEE80211R
56586197 576 if (wpa_key_mgmt_ft(sm->key_mgmt))
98cd3d1c 577 return wpa_derive_ptk_ft(sm, src_addr, key, ptk);
6fc6879b
JM
578#endif /* CONFIG_IEEE80211R */
579
10ec6a5f
JM
580#ifdef CONFIG_DPP2
581 if (sm->key_mgmt == WPA_KEY_MGMT_DPP && sm->dpp_z) {
582 z = wpabuf_head(sm->dpp_z);
583 z_len = wpabuf_len(sm->dpp_z);
584 }
585#endif /* CONFIG_DPP2 */
586
8b138d28
JM
587 akmp = sm->key_mgmt;
588#ifdef CONFIG_OWE
589 if (sm->owe_ptk_workaround && akmp == WPA_KEY_MGMT_OWE &&
590 sm->pmk_len > 32) {
591 wpa_printf(MSG_DEBUG,
592 "OWE: Force SHA256 for PTK derivation");
593 akmp = WPA_KEY_MGMT_PSK_SHA256;
594 }
595#endif /* CONFIG_OWE */
98cd3d1c
JM
596 return wpa_pmk_to_ptk(sm->pmk, sm->pmk_len, "Pairwise key expansion",
597 sm->own_addr, sm->bssid, sm->snonce,
8b138d28 598 key->key_nonce, ptk, akmp,
10ec6a5f 599 sm->pairwise_cipher, z, z_len);
6fc6879b
JM
600}
601
602
603static void wpa_supplicant_process_1_of_4(struct wpa_sm *sm,
604 const unsigned char *src_addr,
605 const struct wpa_eapol_key *key,
e6270129
JM
606 u16 ver, const u8 *key_data,
607 size_t key_data_len)
6fc6879b
JM
608{
609 struct wpa_eapol_ie_parse ie;
610 struct wpa_ptk *ptk;
b4a1256d 611 int res;
25ef8529
JM
612 u8 *kde, *kde_buf = NULL;
613 size_t kde_len;
6fc6879b
JM
614
615 if (wpa_sm_get_network_ctx(sm) == NULL) {
f049052b
BG
616 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, "WPA: No SSID info "
617 "found (msg 1 of 4)");
6fc6879b
JM
618 return;
619 }
620
621 wpa_sm_set_state(sm, WPA_4WAY_HANDSHAKE);
f049052b
BG
622 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: RX message 1 of 4-Way "
623 "Handshake from " MACSTR " (ver=%d)", MAC2STR(src_addr), ver);
6fc6879b
JM
624
625 os_memset(&ie, 0, sizeof(ie));
626
a14896e8 627 if (sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN) {
6fc6879b 628 /* RSN: msg 1/4 should contain PMKID for the selected PMK */
e6270129
JM
629 wpa_hexdump(MSG_DEBUG, "RSN: msg 1/4 key data",
630 key_data, key_data_len);
631 if (wpa_supplicant_parse_ies(key_data, key_data_len, &ie) < 0)
d627a939 632 goto failed;
6fc6879b
JM
633 if (ie.pmkid) {
634 wpa_hexdump(MSG_DEBUG, "RSN: PMKID from "
635 "Authenticator", ie.pmkid, PMKID_LEN);
636 }
637 }
6fc6879b 638
b4a1256d
JM
639 res = wpa_supplicant_get_pmk(sm, src_addr, ie.pmkid);
640 if (res == -2) {
f049052b
BG
641 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "RSN: Do not reply to "
642 "msg 1/4 - requesting full EAP authentication");
b4a1256d
JM
643 return;
644 }
645 if (res)
83935317 646 goto failed;
6fc6879b
JM
647
648 if (sm->renew_snonce) {
3642c431 649 if (random_get_bytes(sm->snonce, WPA_NONCE_LEN)) {
0f057fb2 650 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
6fc6879b 651 "WPA: Failed to get random data for SNonce");
83935317 652 goto failed;
6fc6879b
JM
653 }
654 sm->renew_snonce = 0;
655 wpa_hexdump(MSG_DEBUG, "WPA: Renewed SNonce",
656 sm->snonce, WPA_NONCE_LEN);
657 }
658
659 /* Calculate PTK which will be stored as a temporary PTK until it has
660 * been verified when processing message 3/4. */
661 ptk = &sm->tptk;
b488a129
JM
662 if (wpa_derive_ptk(sm, src_addr, key, ptk) < 0)
663 goto failed;
3b9c5176 664 if (sm->pairwise_cipher == WPA_CIPHER_TKIP) {
d2c33b91 665 u8 buf[8];
3b9c5176 666 /* Supplicant: swap tx/rx Mic keys */
98cd3d1c
JM
667 os_memcpy(buf, &ptk->tk[16], 8);
668 os_memcpy(&ptk->tk[16], &ptk->tk[24], 8);
669 os_memcpy(&ptk->tk[24], buf, 8);
31bc66e4 670 forced_memzero(buf, sizeof(buf));
3b9c5176 671 }
6fc6879b
JM
672 sm->tptk_set = 1;
673
25ef8529
JM
674 kde = sm->assoc_wpa_ie;
675 kde_len = sm->assoc_wpa_ie_len;
6d6c8877
JM
676 kde_buf = os_malloc(kde_len +
677 2 + RSN_SELECTOR_LEN + 3 +
678 sm->assoc_rsnxe_len +
679 2 + RSN_SELECTOR_LEN + 1);
680 if (!kde_buf)
681 goto failed;
682 os_memcpy(kde_buf, kde, kde_len);
683 kde = kde_buf;
25ef8529 684
17c2559c
MV
685#ifdef CONFIG_OCV
686 if (wpa_sm_ocv_enabled(sm)) {
687 struct wpa_channel_info ci;
688 u8 *pos;
689
6d6c8877 690 pos = kde + kde_len;
17c2559c
MV
691 if (wpa_sm_channel_info(sm, &ci) != 0) {
692 wpa_printf(MSG_WARNING,
693 "Failed to get channel info for OCI element in EAPOL-Key 2/4");
694 goto failed;
695 }
696
17c2559c
MV
697 if (ocv_insert_oci_kde(&ci, &pos) < 0)
698 goto failed;
699 kde_len = pos - kde;
700 }
701#endif /* CONFIG_OCV */
702
6d6c8877
JM
703 if (sm->assoc_rsnxe && sm->assoc_rsnxe_len) {
704 os_memcpy(kde + kde_len, sm->assoc_rsnxe, sm->assoc_rsnxe_len);
705 kde_len += sm->assoc_rsnxe_len;
706 }
707
25ef8529
JM
708#ifdef CONFIG_P2P
709 if (sm->p2p) {
6d6c8877
JM
710 u8 *pos;
711
712 wpa_printf(MSG_DEBUG,
713 "P2P: Add IP Address Request KDE into EAPOL-Key 2/4");
714 pos = kde + kde_len;
715 *pos++ = WLAN_EID_VENDOR_SPECIFIC;
716 *pos++ = RSN_SELECTOR_LEN + 1;
717 RSN_SELECTOR_PUT(pos, WFA_KEY_DATA_IP_ADDR_REQ);
718 pos += RSN_SELECTOR_LEN;
719 *pos++ = 0x01;
720 kde_len = pos - kde;
25ef8529
JM
721 }
722#endif /* CONFIG_P2P */
723
6fc6879b 724 if (wpa_supplicant_send_2_of_4(sm, sm->bssid, key, ver, sm->snonce,
c93b7e18 725 kde, kde_len, ptk) < 0)
83935317 726 goto failed;
6fc6879b 727
25ef8529 728 os_free(kde_buf);
6fc6879b 729 os_memcpy(sm->anonce, key->key_nonce, WPA_NONCE_LEN);
83935317
JM
730 return;
731
732failed:
25ef8529 733 os_free(kde_buf);
83935317 734 wpa_sm_deauthenticate(sm, WLAN_REASON_UNSPECIFIED);
6fc6879b
JM
735}
736
737
738static void wpa_sm_start_preauth(void *eloop_ctx, void *timeout_ctx)
739{
740 struct wpa_sm *sm = eloop_ctx;
741 rsn_preauth_candidate_process(sm);
742}
743
744
745static void wpa_supplicant_key_neg_complete(struct wpa_sm *sm,
746 const u8 *addr, int secure)
747{
0f057fb2
JM
748 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
749 "WPA: Key negotiation completed with "
6fc6879b
JM
750 MACSTR " [PTK=%s GTK=%s]", MAC2STR(addr),
751 wpa_cipher_txt(sm->pairwise_cipher),
752 wpa_cipher_txt(sm->group_cipher));
6fc6879b
JM
753 wpa_sm_cancel_auth_timeout(sm);
754 wpa_sm_set_state(sm, WPA_COMPLETED);
755
756 if (secure) {
757 wpa_sm_mlme_setprotection(
758 sm, addr, MLME_SETPROTECTION_PROTECT_TYPE_RX_TX,
759 MLME_SETPROTECTION_KEY_TYPE_PAIRWISE);
760 eapol_sm_notify_portValid(sm->eapol, TRUE);
a1ea1b45 761 if (wpa_key_mgmt_wpa_psk(sm->key_mgmt) ||
567da5bb 762 sm->key_mgmt == WPA_KEY_MGMT_DPP ||
a1ea1b45 763 sm->key_mgmt == WPA_KEY_MGMT_OWE)
6fc6879b
JM
764 eapol_sm_notify_eap_success(sm->eapol, TRUE);
765 /*
766 * Start preauthentication after a short wait to avoid a
767 * possible race condition between the data receive and key
768 * configuration after the 4-Way Handshake. This increases the
ffbf1eaa 769 * likelihood of the first preauth EAPOL-Start frame getting to
6fc6879b
JM
770 * the target AP.
771 */
02a0a239
JM
772 if (!dl_list_empty(&sm->pmksa_candidates))
773 eloop_register_timeout(1, 0, wpa_sm_start_preauth,
774 sm, NULL);
6fc6879b
JM
775 }
776
777 if (sm->cur_pmksa && sm->cur_pmksa->opportunistic) {
f049052b
BG
778 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
779 "RSN: Authenticator accepted "
780 "opportunistic PMKSA entry - marking it valid");
6fc6879b
JM
781 sm->cur_pmksa->opportunistic = 0;
782 }
783
784#ifdef CONFIG_IEEE80211R
56586197 785 if (wpa_key_mgmt_ft(sm->key_mgmt)) {
6fc6879b 786 /* Prepare for the next transition */
76b7981d 787 wpa_ft_prepare_auth_request(sm, NULL);
6fc6879b
JM
788 }
789#endif /* CONFIG_IEEE80211R */
790}
791
792
581a8cde
JM
793static void wpa_sm_rekey_ptk(void *eloop_ctx, void *timeout_ctx)
794{
795 struct wpa_sm *sm = eloop_ctx;
f049052b 796 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Request PTK rekeying");
581a8cde
JM
797 wpa_sm_key_request(sm, 0, 1);
798}
799
800
6fc6879b 801static int wpa_supplicant_install_ptk(struct wpa_sm *sm,
a919a260
AW
802 const struct wpa_eapol_key *key,
803 enum key_flag key_flag)
6fc6879b
JM
804{
805 int keylen, rsclen;
71934751 806 enum wpa_alg alg;
6fc6879b 807 const u8 *key_rsc;
6fc6879b 808
53bb18cc 809 if (sm->ptk.installed) {
ad00d64e
JM
810 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
811 "WPA: Do not re-install same PTK to the driver");
812 return 0;
813 }
814
f049052b
BG
815 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
816 "WPA: Installing PTK to the driver");
6fc6879b 817
c3550295 818 if (sm->pairwise_cipher == WPA_CIPHER_NONE) {
f049052b
BG
819 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Pairwise Cipher "
820 "Suite: NONE - do not use pairwise keys");
6fc6879b 821 return 0;
c3550295
JM
822 }
823
824 if (!wpa_cipher_valid_pairwise(sm->pairwise_cipher)) {
f049052b
BG
825 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
826 "WPA: Unsupported pairwise cipher %d",
827 sm->pairwise_cipher);
6fc6879b
JM
828 return -1;
829 }
830
c3550295
JM
831 alg = wpa_cipher_to_alg(sm->pairwise_cipher);
832 keylen = wpa_cipher_key_len(sm->pairwise_cipher);
a6ea6653
JM
833 if (keylen <= 0 || (unsigned int) keylen != sm->ptk.tk_len) {
834 wpa_printf(MSG_DEBUG, "WPA: TK length mismatch: %d != %lu",
835 keylen, (long unsigned int) sm->ptk.tk_len);
836 return -1;
837 }
c3550295
JM
838 rsclen = wpa_cipher_rsc_len(sm->pairwise_cipher);
839
a14896e8 840 if (sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN) {
6fc6879b
JM
841 key_rsc = null_rsc;
842 } else {
843 key_rsc = key->key_rsc;
844 wpa_hexdump(MSG_DEBUG, "WPA: RSC", key_rsc, rsclen);
845 }
846
847 if (wpa_sm_set_key(sm, alg, sm->bssid, 0, 1, key_rsc, rsclen,
a919a260
AW
848 sm->ptk.tk, keylen,
849 KEY_FLAG_PAIRWISE | key_flag) < 0) {
f049052b
BG
850 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
851 "WPA: Failed to set PTK to the "
852 "driver (alg=%d keylen=%d bssid=" MACSTR ")",
853 alg, keylen, MAC2STR(sm->bssid));
6fc6879b
JM
854 return -1;
855 }
581a8cde 856
7d711541 857 /* TK is not needed anymore in supplicant */
98cd3d1c 858 os_memset(sm->ptk.tk, 0, WPA_TK_MAX_LEN);
a6ea6653 859 sm->ptk.tk_len = 0;
53bb18cc 860 sm->ptk.installed = 1;
7d711541 861
581a8cde
JM
862 if (sm->wpa_ptk_rekey) {
863 eloop_cancel_timeout(wpa_sm_rekey_ptk, sm, NULL);
864 eloop_register_timeout(sm->wpa_ptk_rekey, 0, wpa_sm_rekey_ptk,
865 sm, NULL);
866 }
867
6fc6879b
JM
868 return 0;
869}
870
871
f049052b
BG
872static int wpa_supplicant_check_group_cipher(struct wpa_sm *sm,
873 int group_cipher,
6fc6879b 874 int keylen, int maxkeylen,
71934751
JM
875 int *key_rsc_len,
876 enum wpa_alg *alg)
6fc6879b 877{
c3550295 878 int klen;
6fc6879b 879
c3550295
JM
880 *alg = wpa_cipher_to_alg(group_cipher);
881 if (*alg == WPA_ALG_NONE) {
f049052b
BG
882 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
883 "WPA: Unsupported Group Cipher %d",
884 group_cipher);
6fc6879b
JM
885 return -1;
886 }
c3550295 887 *key_rsc_len = wpa_cipher_rsc_len(group_cipher);
6fc6879b 888
c3550295
JM
889 klen = wpa_cipher_key_len(group_cipher);
890 if (keylen != klen || maxkeylen < klen) {
f049052b
BG
891 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
892 "WPA: Unsupported %s Group Cipher key length %d (%d)",
893 wpa_cipher_txt(group_cipher), keylen, maxkeylen);
c3550295 894 return -1;
6fc6879b 895 }
c3550295 896 return 0;
6fc6879b
JM
897}
898
899
900struct wpa_gtk_data {
71934751 901 enum wpa_alg alg;
6fc6879b
JM
902 int tx, key_rsc_len, keyidx;
903 u8 gtk[32];
904 int gtk_len;
905};
906
907
908static int wpa_supplicant_install_gtk(struct wpa_sm *sm,
909 const struct wpa_gtk_data *gd,
87e2db16 910 const u8 *key_rsc, int wnm_sleep)
6fc6879b
JM
911{
912 const u8 *_gtk = gd->gtk;
913 u8 gtk_buf[32];
914
cb5132bb 915 /* Detect possible key reinstallation */
87e2db16
JM
916 if ((sm->gtk.gtk_len == (size_t) gd->gtk_len &&
917 os_memcmp(sm->gtk.gtk, gd->gtk, sm->gtk.gtk_len) == 0) ||
918 (sm->gtk_wnm_sleep.gtk_len == (size_t) gd->gtk_len &&
919 os_memcmp(sm->gtk_wnm_sleep.gtk, gd->gtk,
920 sm->gtk_wnm_sleep.gtk_len) == 0)) {
cb5132bb
MV
921 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
922 "WPA: Not reinstalling already in-use GTK to the driver (keyidx=%d tx=%d len=%d)",
923 gd->keyidx, gd->tx, gd->gtk_len);
924 return 0;
925 }
926
6fc6879b 927 wpa_hexdump_key(MSG_DEBUG, "WPA: Group Key", gd->gtk, gd->gtk_len);
f049052b
BG
928 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
929 "WPA: Installing GTK to the driver (keyidx=%d tx=%d len=%d)",
930 gd->keyidx, gd->tx, gd->gtk_len);
6fc6879b
JM
931 wpa_hexdump(MSG_DEBUG, "WPA: RSC", key_rsc, gd->key_rsc_len);
932 if (sm->group_cipher == WPA_CIPHER_TKIP) {
933 /* Swap Tx/Rx keys for Michael MIC */
934 os_memcpy(gtk_buf, gd->gtk, 16);
935 os_memcpy(gtk_buf + 16, gd->gtk + 24, 8);
936 os_memcpy(gtk_buf + 24, gd->gtk + 16, 8);
937 _gtk = gtk_buf;
938 }
939 if (sm->pairwise_cipher == WPA_CIPHER_NONE) {
0382097e 940 if (wpa_sm_set_key(sm, gd->alg, NULL,
6fc6879b 941 gd->keyidx, 1, key_rsc, gd->key_rsc_len,
a919a260
AW
942 _gtk, gd->gtk_len,
943 KEY_FLAG_GROUP_RX_TX_DEFAULT) < 0) {
f049052b
BG
944 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
945 "WPA: Failed to set GTK to the driver "
946 "(Group only)");
31bc66e4 947 forced_memzero(gtk_buf, sizeof(gtk_buf));
6fc6879b
JM
948 return -1;
949 }
0382097e 950 } else if (wpa_sm_set_key(sm, gd->alg, broadcast_ether_addr,
6fc6879b 951 gd->keyidx, gd->tx, key_rsc, gd->key_rsc_len,
a919a260 952 _gtk, gd->gtk_len, KEY_FLAG_GROUP_RX) < 0) {
f049052b
BG
953 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
954 "WPA: Failed to set GTK to "
955 "the driver (alg=%d keylen=%d keyidx=%d)",
956 gd->alg, gd->gtk_len, gd->keyidx);
31bc66e4 957 forced_memzero(gtk_buf, sizeof(gtk_buf));
6fc6879b
JM
958 return -1;
959 }
31bc66e4 960 forced_memzero(gtk_buf, sizeof(gtk_buf));
6fc6879b 961
87e2db16
JM
962 if (wnm_sleep) {
963 sm->gtk_wnm_sleep.gtk_len = gd->gtk_len;
964 os_memcpy(sm->gtk_wnm_sleep.gtk, gd->gtk,
965 sm->gtk_wnm_sleep.gtk_len);
966 } else {
967 sm->gtk.gtk_len = gd->gtk_len;
968 os_memcpy(sm->gtk.gtk, gd->gtk, sm->gtk.gtk_len);
969 }
cb5132bb 970
6fc6879b
JM
971 return 0;
972}
973
974
975static int wpa_supplicant_gtk_tx_bit_workaround(const struct wpa_sm *sm,
976 int tx)
977{
978 if (tx && sm->pairwise_cipher != WPA_CIPHER_NONE) {
979 /* Ignore Tx bit for GTK if a pairwise key is used. One AP
980 * seemed to set this bit (incorrectly, since Tx is only when
981 * doing Group Key only APs) and without this workaround, the
982 * data connection does not work because wpa_supplicant
983 * configured non-zero keyidx to be used for unicast. */
f049052b
BG
984 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
985 "WPA: Tx bit set for GTK, but pairwise "
986 "keys are used - ignore Tx bit");
6fc6879b
JM
987 return 0;
988 }
989 return tx;
990}
991
992
73ed03f3
MS
993static int wpa_supplicant_rsc_relaxation(const struct wpa_sm *sm,
994 const u8 *rsc)
995{
996 int rsclen;
997
998 if (!sm->wpa_rsc_relaxation)
999 return 0;
1000
1001 rsclen = wpa_cipher_rsc_len(sm->group_cipher);
1002
1003 /*
1004 * Try to detect RSC (endian) corruption issue where the AP sends
1005 * the RSC bytes in EAPOL-Key message in the wrong order, both if
1006 * it's actually a 6-byte field (as it should be) and if it treats
1007 * it as an 8-byte field.
1008 * An AP model known to have this bug is the Sapido RB-1632.
1009 */
1010 if (rsclen == 6 && ((rsc[5] && !rsc[0]) || rsc[6] || rsc[7])) {
1011 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1012 "RSC %02x%02x%02x%02x%02x%02x%02x%02x is likely bogus, using 0",
1013 rsc[0], rsc[1], rsc[2], rsc[3],
1014 rsc[4], rsc[5], rsc[6], rsc[7]);
1015
1016 return 1;
1017 }
1018
1019 return 0;
1020}
1021
1022
6fc6879b
JM
1023static int wpa_supplicant_pairwise_gtk(struct wpa_sm *sm,
1024 const struct wpa_eapol_key *key,
1025 const u8 *gtk, size_t gtk_len,
1026 int key_info)
1027{
6fc6879b 1028 struct wpa_gtk_data gd;
73ed03f3 1029 const u8 *key_rsc;
6fc6879b
JM
1030
1031 /*
1032 * IEEE Std 802.11i-2004 - 8.5.2 EAPOL-Key frames - Figure 43x
1033 * GTK KDE format:
1034 * KeyID[bits 0-1], Tx [bit 2], Reserved [bits 3-7]
1035 * Reserved [bits 0-7]
1036 * GTK
1037 */
1038
1039 os_memset(&gd, 0, sizeof(gd));
1040 wpa_hexdump_key(MSG_DEBUG, "RSN: received GTK in pairwise handshake",
1041 gtk, gtk_len);
1042
1043 if (gtk_len < 2 || gtk_len - 2 > sizeof(gd.gtk))
1044 return -1;
1045
1046 gd.keyidx = gtk[0] & 0x3;
1047 gd.tx = wpa_supplicant_gtk_tx_bit_workaround(sm,
1048 !!(gtk[0] & BIT(2)));
1049 gtk += 2;
1050 gtk_len -= 2;
1051
1052 os_memcpy(gd.gtk, gtk, gtk_len);
1053 gd.gtk_len = gtk_len;
1054
73ed03f3
MS
1055 key_rsc = key->key_rsc;
1056 if (wpa_supplicant_rsc_relaxation(sm, key->key_rsc))
1057 key_rsc = null_rsc;
1058
dff1e285
JM
1059 if (sm->group_cipher != WPA_CIPHER_GTK_NOT_USED &&
1060 (wpa_supplicant_check_group_cipher(sm, sm->group_cipher,
1061 gtk_len, gtk_len,
1062 &gd.key_rsc_len, &gd.alg) ||
87e2db16 1063 wpa_supplicant_install_gtk(sm, &gd, key_rsc, 0))) {
f049052b
BG
1064 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1065 "RSN: Failed to install GTK");
31bc66e4 1066 forced_memzero(&gd, sizeof(gd));
6fc6879b
JM
1067 return -1;
1068 }
31bc66e4 1069 forced_memzero(&gd, sizeof(gd));
6fc6879b 1070
6fc6879b 1071 return 0;
6fc6879b
JM
1072}
1073
1074
cb5132bb 1075static int wpa_supplicant_install_igtk(struct wpa_sm *sm,
87e2db16
JM
1076 const struct wpa_igtk_kde *igtk,
1077 int wnm_sleep)
cb5132bb
MV
1078{
1079 size_t len = wpa_cipher_key_len(sm->mgmt_group_cipher);
1080 u16 keyidx = WPA_GET_LE16(igtk->keyid);
1081
1082 /* Detect possible key reinstallation */
87e2db16
JM
1083 if ((sm->igtk.igtk_len == len &&
1084 os_memcmp(sm->igtk.igtk, igtk->igtk, sm->igtk.igtk_len) == 0) ||
1085 (sm->igtk_wnm_sleep.igtk_len == len &&
1086 os_memcmp(sm->igtk_wnm_sleep.igtk, igtk->igtk,
1087 sm->igtk_wnm_sleep.igtk_len) == 0)) {
cb5132bb
MV
1088 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1089 "WPA: Not reinstalling already in-use IGTK to the driver (keyidx=%d)",
1090 keyidx);
1091 return 0;
1092 }
1093
1094 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
94919da2 1095 "WPA: IGTK keyid %d pn " COMPACT_MACSTR,
cb5132bb
MV
1096 keyidx, MAC2STR(igtk->pn));
1097 wpa_hexdump_key(MSG_DEBUG, "WPA: IGTK", igtk->igtk, len);
1098 if (keyidx > 4095) {
1099 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1100 "WPA: Invalid IGTK KeyID %d", keyidx);
1101 return -1;
1102 }
1103 if (wpa_sm_set_key(sm, wpa_cipher_to_alg(sm->mgmt_group_cipher),
1104 broadcast_ether_addr,
1105 keyidx, 0, igtk->pn, sizeof(igtk->pn),
a919a260 1106 igtk->igtk, len, KEY_FLAG_GROUP_RX) < 0) {
824cb5a5
JM
1107 if (keyidx == 0x0400 || keyidx == 0x0500) {
1108 /* Assume the AP has broken PMF implementation since it
1109 * seems to have swapped the KeyID bytes. The AP cannot
1110 * be trusted to implement BIP correctly or provide a
1111 * valid IGTK, so do not try to configure this key with
1112 * swapped KeyID bytes. Instead, continue without
1113 * configuring the IGTK so that the driver can drop any
1114 * received group-addressed robust management frames due
1115 * to missing keys.
1116 *
1117 * Normally, this error behavior would result in us
1118 * disconnecting, but there are number of deployed APs
1119 * with this broken behavior, so as an interoperability
1120 * workaround, allow the connection to proceed. */
1121 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1122 "WPA: Ignore IGTK configuration error due to invalid IGTK KeyID byte order");
1123 } else {
1124 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1125 "WPA: Failed to configure IGTK to the driver");
1126 return -1;
1127 }
cb5132bb
MV
1128 }
1129
87e2db16
JM
1130 if (wnm_sleep) {
1131 sm->igtk_wnm_sleep.igtk_len = len;
1132 os_memcpy(sm->igtk_wnm_sleep.igtk, igtk->igtk,
1133 sm->igtk_wnm_sleep.igtk_len);
1134 } else {
1135 sm->igtk.igtk_len = len;
1136 os_memcpy(sm->igtk.igtk, igtk->igtk, sm->igtk.igtk_len);
1137 }
cb5132bb
MV
1138
1139 return 0;
1140}
cb5132bb
MV
1141
1142
6fc6879b
JM
1143static int ieee80211w_set_keys(struct wpa_sm *sm,
1144 struct wpa_eapol_ie_parse *ie)
1145{
8dd9f9cd 1146 if (!wpa_cipher_valid_mgmt_group(sm->mgmt_group_cipher))
6fc6879b
JM
1147 return 0;
1148
1149 if (ie->igtk) {
8dd9f9cd 1150 size_t len;
6fc6879b 1151 const struct wpa_igtk_kde *igtk;
cb5132bb 1152
8dd9f9cd
JM
1153 len = wpa_cipher_key_len(sm->mgmt_group_cipher);
1154 if (ie->igtk_len != WPA_IGTK_KDE_PREFIX_LEN + len)
6fc6879b 1155 return -1;
cb5132bb 1156
6fc6879b 1157 igtk = (const struct wpa_igtk_kde *) ie->igtk;
87e2db16 1158 if (wpa_supplicant_install_igtk(sm, igtk, 0) < 0)
6fc6879b 1159 return -1;
6fc6879b
JM
1160 }
1161
1162 return 0;
6fc6879b
JM
1163}
1164
1165
1166static void wpa_report_ie_mismatch(struct wpa_sm *sm,
1167 const char *reason, const u8 *src_addr,
1168 const u8 *wpa_ie, size_t wpa_ie_len,
1169 const u8 *rsn_ie, size_t rsn_ie_len)
1170{
0f057fb2 1171 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, "WPA: %s (src=" MACSTR ")",
6fc6879b
JM
1172 reason, MAC2STR(src_addr));
1173
1174 if (sm->ap_wpa_ie) {
1175 wpa_hexdump(MSG_INFO, "WPA: WPA IE in Beacon/ProbeResp",
1176 sm->ap_wpa_ie, sm->ap_wpa_ie_len);
1177 }
1178 if (wpa_ie) {
1179 if (!sm->ap_wpa_ie) {
f049052b
BG
1180 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1181 "WPA: No WPA IE in Beacon/ProbeResp");
6fc6879b
JM
1182 }
1183 wpa_hexdump(MSG_INFO, "WPA: WPA IE in 3/4 msg",
1184 wpa_ie, wpa_ie_len);
1185 }
1186
1187 if (sm->ap_rsn_ie) {
1188 wpa_hexdump(MSG_INFO, "WPA: RSN IE in Beacon/ProbeResp",
1189 sm->ap_rsn_ie, sm->ap_rsn_ie_len);
1190 }
1191 if (rsn_ie) {
1192 if (!sm->ap_rsn_ie) {
f049052b
BG
1193 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1194 "WPA: No RSN IE in Beacon/ProbeResp");
6fc6879b
JM
1195 }
1196 wpa_hexdump(MSG_INFO, "WPA: RSN IE in 3/4 msg",
1197 rsn_ie, rsn_ie_len);
1198 }
1199
3da372fa 1200 wpa_sm_deauthenticate(sm, WLAN_REASON_IE_IN_4WAY_DIFFERS);
6fc6879b
JM
1201}
1202
1203
5af8187e
JM
1204#ifdef CONFIG_IEEE80211R
1205
1206static int ft_validate_mdie(struct wpa_sm *sm,
1207 const unsigned char *src_addr,
3b4f6dac
JM
1208 struct wpa_eapol_ie_parse *ie,
1209 const u8 *assoc_resp_mdie)
5af8187e
JM
1210{
1211 struct rsn_mdie *mdie;
1212
5af8187e
JM
1213 mdie = (struct rsn_mdie *) (ie->mdie + 2);
1214 if (ie->mdie == NULL || ie->mdie_len < 2 + sizeof(*mdie) ||
1215 os_memcmp(mdie->mobility_domain, sm->mobility_domain,
1216 MOBILITY_DOMAIN_ID_LEN) != 0) {
f049052b
BG
1217 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "FT: MDIE in msg 3/4 did "
1218 "not match with the current mobility domain");
5af8187e
JM
1219 return -1;
1220 }
1221
3b4f6dac
JM
1222 if (assoc_resp_mdie &&
1223 (assoc_resp_mdie[1] != ie->mdie[1] ||
1224 os_memcmp(assoc_resp_mdie, ie->mdie, 2 + ie->mdie[1]) != 0)) {
f049052b 1225 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "FT: MDIE mismatch");
3b4f6dac
JM
1226 wpa_hexdump(MSG_DEBUG, "FT: MDIE in EAPOL-Key msg 3/4",
1227 ie->mdie, 2 + ie->mdie[1]);
1228 wpa_hexdump(MSG_DEBUG, "FT: MDIE in (Re)Association Response",
1229 assoc_resp_mdie, 2 + assoc_resp_mdie[1]);
1230 return -1;
1231 }
1232
1233 return 0;
1234}
1235
1236
1237static int ft_validate_ftie(struct wpa_sm *sm,
1238 const unsigned char *src_addr,
1239 struct wpa_eapol_ie_parse *ie,
1240 const u8 *assoc_resp_ftie)
1241{
1242 if (ie->ftie == NULL) {
f049052b
BG
1243 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1244 "FT: No FTIE in EAPOL-Key msg 3/4");
3b4f6dac
JM
1245 return -1;
1246 }
1247
1248 if (assoc_resp_ftie == NULL)
1249 return 0;
1250
1251 if (assoc_resp_ftie[1] != ie->ftie[1] ||
1252 os_memcmp(assoc_resp_ftie, ie->ftie, 2 + ie->ftie[1]) != 0) {
f049052b 1253 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "FT: FTIE mismatch");
3b4f6dac
JM
1254 wpa_hexdump(MSG_DEBUG, "FT: FTIE in EAPOL-Key msg 3/4",
1255 ie->ftie, 2 + ie->ftie[1]);
1256 wpa_hexdump(MSG_DEBUG, "FT: FTIE in (Re)Association Response",
1257 assoc_resp_ftie, 2 + assoc_resp_ftie[1]);
1258 return -1;
1259 }
1260
5af8187e
JM
1261 return 0;
1262}
1263
1264
1265static int ft_validate_rsnie(struct wpa_sm *sm,
1266 const unsigned char *src_addr,
1267 struct wpa_eapol_ie_parse *ie)
1268{
1269 struct wpa_ie_data rsn;
1270
1271 if (!ie->rsn_ie)
1272 return 0;
1273
1274 /*
1275 * Verify that PMKR1Name from EAPOL-Key message 3/4
1276 * matches with the value we derived.
1277 */
1278 if (wpa_parse_wpa_ie_rsn(ie->rsn_ie, ie->rsn_ie_len, &rsn) < 0 ||
1279 rsn.num_pmkid != 1 || rsn.pmkid == NULL) {
f049052b
BG
1280 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "FT: No PMKR1Name in "
1281 "FT 4-way handshake message 3/4");
5af8187e
JM
1282 return -1;
1283 }
1284
0d15b69f
JM
1285 if (os_memcmp_const(rsn.pmkid, sm->pmk_r1_name, WPA_PMK_NAME_LEN) != 0)
1286 {
f049052b
BG
1287 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1288 "FT: PMKR1Name mismatch in "
1289 "FT 4-way handshake message 3/4");
5af8187e
JM
1290 wpa_hexdump(MSG_DEBUG, "FT: PMKR1Name from Authenticator",
1291 rsn.pmkid, WPA_PMK_NAME_LEN);
1292 wpa_hexdump(MSG_DEBUG, "FT: Derived PMKR1Name",
1293 sm->pmk_r1_name, WPA_PMK_NAME_LEN);
1294 return -1;
1295 }
1296
1297 return 0;
1298}
1299
1300
1301static int wpa_supplicant_validate_ie_ft(struct wpa_sm *sm,
1302 const unsigned char *src_addr,
1303 struct wpa_eapol_ie_parse *ie)
1304{
3b4f6dac
JM
1305 const u8 *pos, *end, *mdie = NULL, *ftie = NULL;
1306
1307 if (sm->assoc_resp_ies) {
1308 pos = sm->assoc_resp_ies;
1309 end = pos + sm->assoc_resp_ies_len;
2461724c
JM
1310 while (end - pos > 2) {
1311 if (2 + pos[1] > end - pos)
3b4f6dac
JM
1312 break;
1313 switch (*pos) {
1314 case WLAN_EID_MOBILITY_DOMAIN:
1315 mdie = pos;
1316 break;
1317 case WLAN_EID_FAST_BSS_TRANSITION:
1318 ftie = pos;
1319 break;
1320 }
1321 pos += 2 + pos[1];
1322 }
1323 }
1324
1325 if (ft_validate_mdie(sm, src_addr, ie, mdie) < 0 ||
1326 ft_validate_ftie(sm, src_addr, ie, ftie) < 0 ||
5af8187e
JM
1327 ft_validate_rsnie(sm, src_addr, ie) < 0)
1328 return -1;
1329
1330 return 0;
1331}
1332
1333#endif /* CONFIG_IEEE80211R */
1334
1335
6fc6879b
JM
1336static int wpa_supplicant_validate_ie(struct wpa_sm *sm,
1337 const unsigned char *src_addr,
1338 struct wpa_eapol_ie_parse *ie)
1339{
1340 if (sm->ap_wpa_ie == NULL && sm->ap_rsn_ie == NULL) {
f049052b
BG
1341 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1342 "WPA: No WPA/RSN IE for this AP known. "
1343 "Trying to get from scan results");
6fc6879b 1344 if (wpa_sm_get_beacon_ie(sm) < 0) {
f049052b
BG
1345 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1346 "WPA: Could not find AP from "
1347 "the scan results");
6fc6879b 1348 } else {
f049052b
BG
1349 wpa_msg(sm->ctx->msg_ctx, MSG_DEBUG,
1350 "WPA: Found the current AP from "
1351 "updated scan results");
6fc6879b
JM
1352 }
1353 }
1354
1355 if (ie->wpa_ie == NULL && ie->rsn_ie == NULL &&
1356 (sm->ap_wpa_ie || sm->ap_rsn_ie)) {
1357 wpa_report_ie_mismatch(sm, "IE in 3/4 msg does not match "
1358 "with IE in Beacon/ProbeResp (no IE?)",
1359 src_addr, ie->wpa_ie, ie->wpa_ie_len,
1360 ie->rsn_ie, ie->rsn_ie_len);
1361 return -1;
1362 }
1363
1364 if ((ie->wpa_ie && sm->ap_wpa_ie &&
1365 (ie->wpa_ie_len != sm->ap_wpa_ie_len ||
1366 os_memcmp(ie->wpa_ie, sm->ap_wpa_ie, ie->wpa_ie_len) != 0)) ||
1367 (ie->rsn_ie && sm->ap_rsn_ie &&
26e23750
JM
1368 wpa_compare_rsn_ie(wpa_key_mgmt_ft(sm->key_mgmt),
1369 sm->ap_rsn_ie, sm->ap_rsn_ie_len,
1370 ie->rsn_ie, ie->rsn_ie_len))) {
6fc6879b
JM
1371 wpa_report_ie_mismatch(sm, "IE in 3/4 msg does not match "
1372 "with IE in Beacon/ProbeResp",
1373 src_addr, ie->wpa_ie, ie->wpa_ie_len,
1374 ie->rsn_ie, ie->rsn_ie_len);
1375 return -1;
1376 }
1377
1378 if (sm->proto == WPA_PROTO_WPA &&
1379 ie->rsn_ie && sm->ap_rsn_ie == NULL && sm->rsn_enabled) {
1380 wpa_report_ie_mismatch(sm, "Possible downgrade attack "
1381 "detected - RSN was enabled and RSN IE "
1382 "was in msg 3/4, but not in "
1383 "Beacon/ProbeResp",
1384 src_addr, ie->wpa_ie, ie->wpa_ie_len,
1385 ie->rsn_ie, ie->rsn_ie_len);
1386 return -1;
1387 }
1388
146889e3
JM
1389 if ((sm->ap_rsnxe && !ie->rsnxe) ||
1390 (!sm->ap_rsnxe && ie->rsnxe) ||
1391 (sm->ap_rsnxe && ie->rsnxe &&
1392 (sm->ap_rsnxe_len != ie->rsnxe_len ||
1393 os_memcmp(sm->ap_rsnxe, ie->rsnxe, sm->ap_rsnxe_len) != 0))) {
1394 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1395 "WPA: RSNXE mismatch between Beacon/ProbeResp and EAPOL-Key msg 3/4");
1396 return -1;
1397 }
1398
6fc6879b 1399#ifdef CONFIG_IEEE80211R
5af8187e
JM
1400 if (wpa_key_mgmt_ft(sm->key_mgmt) &&
1401 wpa_supplicant_validate_ie_ft(sm, src_addr, ie) < 0)
1402 return -1;
6fc6879b
JM
1403#endif /* CONFIG_IEEE80211R */
1404
1405 return 0;
1406}
1407
1408
1409/**
1410 * wpa_supplicant_send_4_of_4 - Send message 4 of WPA/RSN 4-Way Handshake
1411 * @sm: Pointer to WPA state machine data from wpa_sm_init()
1412 * @dst: Destination address for the frame
1413 * @key: Pointer to the EAPOL-Key frame header
1414 * @ver: Version bits from EAPOL-Key Key Info
1415 * @key_info: Key Info
6fc6879b 1416 * @ptk: PTK to use for keyed hash and encryption
c93b7e18 1417 * Returns: >= 0 on success, < 0 on failure
6fc6879b
JM
1418 */
1419int wpa_supplicant_send_4_of_4(struct wpa_sm *sm, const unsigned char *dst,
1420 const struct wpa_eapol_key *key,
1421 u16 ver, u16 key_info,
6fc6879b
JM
1422 struct wpa_ptk *ptk)
1423{
5e3b5197 1424 size_t mic_len, hdrlen, rlen;
6fc6879b 1425 struct wpa_eapol_key *reply;
5e3b5197 1426 u8 *rbuf, *key_mic;
6fc6879b 1427
567da5bb 1428 mic_len = wpa_mic_len(sm->key_mgmt, sm->pmk_len);
6d014ffc 1429 hdrlen = sizeof(*reply) + mic_len + 2;
6fc6879b 1430 rbuf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_KEY, NULL,
5e3b5197 1431 hdrlen, &rlen, (void *) &reply);
6fc6879b
JM
1432 if (rbuf == NULL)
1433 return -1;
1434
a14896e8
JM
1435 reply->type = (sm->proto == WPA_PROTO_RSN ||
1436 sm->proto == WPA_PROTO_OSEN) ?
6fc6879b
JM
1437 EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA;
1438 key_info &= WPA_KEY_INFO_SECURE;
4a26ccda
JM
1439 key_info |= ver | WPA_KEY_INFO_KEY_TYPE;
1440 if (mic_len)
1441 key_info |= WPA_KEY_INFO_MIC;
2022f1d0
JM
1442 else
1443 key_info |= WPA_KEY_INFO_ENCR_KEY_DATA;
6fc6879b 1444 WPA_PUT_BE16(reply->key_info, key_info);
a14896e8 1445 if (sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN)
6fc6879b
JM
1446 WPA_PUT_BE16(reply->key_length, 0);
1447 else
1448 os_memcpy(reply->key_length, key->key_length, 2);
1449 os_memcpy(reply->replay_counter, key->replay_counter,
1450 WPA_REPLAY_COUNTER_LEN);
1451
6d014ffc
JM
1452 key_mic = (u8 *) (reply + 1);
1453 WPA_PUT_BE16(key_mic + mic_len, 0);
6fc6879b 1454
f049052b 1455 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Sending EAPOL-Key 4/4");
1049af7e
JM
1456 return wpa_eapol_key_send(sm, ptk, ver, dst, ETH_P_EAPOL, rbuf, rlen,
1457 key_mic);
6fc6879b
JM
1458}
1459
1460
1461static void wpa_supplicant_process_3_of_4(struct wpa_sm *sm,
1462 const struct wpa_eapol_key *key,
e6270129
JM
1463 u16 ver, const u8 *key_data,
1464 size_t key_data_len)
6fc6879b 1465{
e6270129 1466 u16 key_info, keylen;
6fc6879b
JM
1467 struct wpa_eapol_ie_parse ie;
1468
1469 wpa_sm_set_state(sm, WPA_4WAY_HANDSHAKE);
f049052b
BG
1470 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: RX message 3 of 4-Way "
1471 "Handshake from " MACSTR " (ver=%d)", MAC2STR(sm->bssid), ver);
6fc6879b
JM
1472
1473 key_info = WPA_GET_BE16(key->key_info);
1474
e6270129
JM
1475 wpa_hexdump(MSG_DEBUG, "WPA: IE KeyData", key_data, key_data_len);
1476 if (wpa_supplicant_parse_ies(key_data, key_data_len, &ie) < 0)
d627a939 1477 goto failed;
6fc6879b 1478 if (ie.gtk && !(key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
f049052b
BG
1479 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1480 "WPA: GTK IE in unencrypted key data");
83935317 1481 goto failed;
6fc6879b 1482 }
6fc6879b 1483 if (ie.igtk && !(key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
f049052b
BG
1484 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1485 "WPA: IGTK KDE in unencrypted key data");
83935317 1486 goto failed;
6fc6879b
JM
1487 }
1488
8dd9f9cd
JM
1489 if (ie.igtk &&
1490 wpa_cipher_valid_mgmt_group(sm->mgmt_group_cipher) &&
1491 ie.igtk_len != WPA_IGTK_KDE_PREFIX_LEN +
1492 (unsigned int) wpa_cipher_key_len(sm->mgmt_group_cipher)) {
f049052b
BG
1493 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1494 "WPA: Invalid IGTK KDE length %lu",
1495 (unsigned long) ie.igtk_len);
83935317 1496 goto failed;
6fc6879b 1497 }
6fc6879b
JM
1498
1499 if (wpa_supplicant_validate_ie(sm, sm->bssid, &ie) < 0)
83935317 1500 goto failed;
6fc6879b
JM
1501
1502 if (os_memcmp(sm->anonce, key->key_nonce, WPA_NONCE_LEN) != 0) {
f049052b
BG
1503 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1504 "WPA: ANonce from message 1 of 4-Way Handshake "
1505 "differs from 3 of 4-Way Handshake - drop packet (src="
1506 MACSTR ")", MAC2STR(sm->bssid));
83935317 1507 goto failed;
6fc6879b
JM
1508 }
1509
1510 keylen = WPA_GET_BE16(key->key_length);
c3550295
JM
1511 if (keylen != wpa_cipher_key_len(sm->pairwise_cipher)) {
1512 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1513 "WPA: Invalid %s key length %d (src=" MACSTR
1514 ")", wpa_cipher_txt(sm->pairwise_cipher), keylen,
1515 MAC2STR(sm->bssid));
1516 goto failed;
6fc6879b
JM
1517 }
1518
25ef8529
JM
1519#ifdef CONFIG_P2P
1520 if (ie.ip_addr_alloc) {
1521 os_memcpy(sm->p2p_ip_addr, ie.ip_addr_alloc, 3 * 4);
1522 wpa_hexdump(MSG_DEBUG, "P2P: IP address info",
1523 sm->p2p_ip_addr, sizeof(sm->p2p_ip_addr));
1524 }
1525#endif /* CONFIG_P2P */
1526
aed61c4e
MV
1527#ifdef CONFIG_OCV
1528 if (wpa_sm_ocv_enabled(sm)) {
1529 struct wpa_channel_info ci;
1530
1531 if (wpa_sm_channel_info(sm, &ci) != 0) {
1532 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1533 "Failed to get channel info to validate received OCI in EAPOL-Key 3/4");
1534 return;
1535 }
1536
1537 if (ocv_verify_tx_params(ie.oci, ie.oci_len, &ci,
1538 channel_width_to_int(ci.chanwidth),
1539 ci.seg1_idx) != 0) {
1540 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, "%s",
1541 ocv_errorstr);
1542 return;
1543 }
1544 }
1545#endif /* CONFIG_OCV */
1546
6fc6879b 1547 if (wpa_supplicant_send_4_of_4(sm, sm->bssid, key, ver, key_info,
c93b7e18 1548 &sm->ptk) < 0) {
83935317
JM
1549 goto failed;
1550 }
6fc6879b
JM
1551
1552 /* SNonce was successfully used in msg 3/4, so mark it to be renewed
1553 * for the next 4-Way Handshake. If msg 3 is received again, the old
1554 * SNonce will still be used to avoid changing PTK. */
1555 sm->renew_snonce = 1;
1556
1557 if (key_info & WPA_KEY_INFO_INSTALL) {
a919a260 1558 if (wpa_supplicant_install_ptk(sm, key, KEY_FLAG_RX_TX))
83935317 1559 goto failed;
6fc6879b
JM
1560 }
1561
1562 if (key_info & WPA_KEY_INFO_SECURE) {
1563 wpa_sm_mlme_setprotection(
1564 sm, sm->bssid, MLME_SETPROTECTION_PROTECT_TYPE_RX,
1565 MLME_SETPROTECTION_KEY_TYPE_PAIRWISE);
1566 eapol_sm_notify_portValid(sm->eapol, TRUE);
1567 }
1568 wpa_sm_set_state(sm, WPA_GROUP_HANDSHAKE);
1569
dff1e285 1570 if (sm->group_cipher == WPA_CIPHER_GTK_NOT_USED) {
dfdabd91
JM
1571 /* No GTK to be set to the driver */
1572 } else if (!ie.gtk && sm->proto == WPA_PROTO_RSN) {
1573 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1574 "RSN: No GTK KDE included in EAPOL-Key msg 3/4");
1575 goto failed;
dff1e285 1576 } else if (ie.gtk &&
6fc6879b
JM
1577 wpa_supplicant_pairwise_gtk(sm, key,
1578 ie.gtk, ie.gtk_len, key_info) < 0) {
f049052b
BG
1579 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1580 "RSN: Failed to configure GTK");
83935317 1581 goto failed;
6fc6879b
JM
1582 }
1583
83935317 1584 if (ieee80211w_set_keys(sm, &ie) < 0) {
f049052b
BG
1585 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1586 "RSN: Failed to configure IGTK");
83935317
JM
1587 goto failed;
1588 }
1589
dfdabd91
JM
1590 if (sm->group_cipher == WPA_CIPHER_GTK_NOT_USED || ie.gtk)
1591 wpa_supplicant_key_neg_complete(sm, sm->bssid,
1592 key_info & WPA_KEY_INFO_SECURE);
1593
392e68e8
SD
1594 if (ie.gtk)
1595 wpa_sm_set_rekey_offload(sm);
b14a210c 1596
17d4b774
JM
1597 /* Add PMKSA cache entry for Suite B AKMs here since PMKID can be
1598 * calculated only after KCK has been derived. Though, do not replace an
1599 * existing PMKSA entry after each 4-way handshake (i.e., new KCK/PMKID)
1600 * to avoid unnecessary changes of PMKID while continuing to use the
1601 * same PMK. */
1602 if (sm->proto == WPA_PROTO_RSN && wpa_key_mgmt_suite_b(sm->key_mgmt) &&
1603 !sm->cur_pmksa) {
087a1f4e
JM
1604 struct rsn_pmksa_cache_entry *sa;
1605
70c93963 1606 sa = pmksa_cache_add(sm->pmksa, sm->pmk, sm->pmk_len, NULL,
98cd3d1c 1607 sm->ptk.kck, sm->ptk.kck_len,
087a1f4e 1608 sm->bssid, sm->own_addr,
869af307 1609 sm->network_ctx, sm->key_mgmt, NULL);
087a1f4e
JM
1610 if (!sm->cur_pmksa)
1611 sm->cur_pmksa = sa;
1612 }
1613
761396e4 1614 sm->msg_3_of_4_ok = 1;
83935317
JM
1615 return;
1616
1617failed:
1618 wpa_sm_deauthenticate(sm, WLAN_REASON_UNSPECIFIED);
6fc6879b
JM
1619}
1620
1621
1622static int wpa_supplicant_process_1_of_2_rsn(struct wpa_sm *sm,
1623 const u8 *keydata,
1624 size_t keydatalen,
1625 u16 key_info,
1626 struct wpa_gtk_data *gd)
1627{
1628 int maxkeylen;
1629 struct wpa_eapol_ie_parse ie;
1630
ecbdc1a1
JM
1631 wpa_hexdump_key(MSG_DEBUG, "RSN: msg 1/2 key data",
1632 keydata, keydatalen);
d627a939
JM
1633 if (wpa_supplicant_parse_ies(keydata, keydatalen, &ie) < 0)
1634 return -1;
6fc6879b 1635 if (ie.gtk && !(key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
f049052b
BG
1636 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1637 "WPA: GTK IE in unencrypted key data");
6fc6879b
JM
1638 return -1;
1639 }
1640 if (ie.gtk == NULL) {
f049052b
BG
1641 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1642 "WPA: No GTK IE in Group Key msg 1/2");
6fc6879b
JM
1643 return -1;
1644 }
1645 maxkeylen = gd->gtk_len = ie.gtk_len - 2;
1646
aed61c4e
MV
1647#ifdef CONFIG_OCV
1648 if (wpa_sm_ocv_enabled(sm)) {
1649 struct wpa_channel_info ci;
1650
1651 if (wpa_sm_channel_info(sm, &ci) != 0) {
1652 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1653 "Failed to get channel info to validate received OCI in EAPOL-Key group msg 1/2");
1654 return -1;
1655 }
1656
1657 if (ocv_verify_tx_params(ie.oci, ie.oci_len, &ci,
1658 channel_width_to_int(ci.chanwidth),
1659 ci.seg1_idx) != 0) {
1660 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, "%s",
1661 ocv_errorstr);
1662 return -1;
1663 }
1664 }
1665#endif /* CONFIG_OCV */
1666
f049052b 1667 if (wpa_supplicant_check_group_cipher(sm, sm->group_cipher,
6fc6879b
JM
1668 gd->gtk_len, maxkeylen,
1669 &gd->key_rsc_len, &gd->alg))
1670 return -1;
1671
7cb9bb4d
JM
1672 wpa_hexdump_key(MSG_DEBUG, "RSN: received GTK in group key handshake",
1673 ie.gtk, ie.gtk_len);
6fc6879b
JM
1674 gd->keyidx = ie.gtk[0] & 0x3;
1675 gd->tx = wpa_supplicant_gtk_tx_bit_workaround(sm,
1676 !!(ie.gtk[0] & BIT(2)));
1677 if (ie.gtk_len - 2 > sizeof(gd->gtk)) {
f049052b
BG
1678 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1679 "RSN: Too long GTK in GTK IE (len=%lu)",
1680 (unsigned long) ie.gtk_len - 2);
6fc6879b
JM
1681 return -1;
1682 }
1683 os_memcpy(gd->gtk, ie.gtk + 2, ie.gtk_len - 2);
1684
1685 if (ieee80211w_set_keys(sm, &ie) < 0)
f049052b
BG
1686 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1687 "RSN: Failed to configure IGTK");
6fc6879b
JM
1688
1689 return 0;
1690}
1691
1692
1693static int wpa_supplicant_process_1_of_2_wpa(struct wpa_sm *sm,
1694 const struct wpa_eapol_key *key,
e6270129
JM
1695 const u8 *key_data,
1696 size_t key_data_len, u16 key_info,
1697 u16 ver, struct wpa_gtk_data *gd)
6fc6879b
JM
1698{
1699 size_t maxkeylen;
c397eff8 1700 u16 gtk_len;
6fc6879b 1701
c397eff8 1702 gtk_len = WPA_GET_BE16(key->key_length);
e6270129 1703 maxkeylen = key_data_len;
6fc6879b
JM
1704 if (ver == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
1705 if (maxkeylen < 8) {
f049052b
BG
1706 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1707 "WPA: Too short maxkeylen (%lu)",
1708 (unsigned long) maxkeylen);
6fc6879b
JM
1709 return -1;
1710 }
1711 maxkeylen -= 8;
1712 }
1713
c397eff8
JM
1714 if (gtk_len > maxkeylen ||
1715 wpa_supplicant_check_group_cipher(sm, sm->group_cipher,
1716 gtk_len, maxkeylen,
6fc6879b
JM
1717 &gd->key_rsc_len, &gd->alg))
1718 return -1;
1719
c397eff8 1720 gd->gtk_len = gtk_len;
6fc6879b
JM
1721 gd->keyidx = (key_info & WPA_KEY_INFO_KEY_INDEX_MASK) >>
1722 WPA_KEY_INFO_KEY_INDEX_SHIFT;
98cd3d1c 1723 if (ver == WPA_KEY_INFO_TYPE_HMAC_MD5_RC4 && sm->ptk.kek_len == 16) {
7cb53ded
JM
1724#ifdef CONFIG_NO_RC4
1725 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1726 "WPA: RC4 not supported in the build");
1727 return -1;
1728#else /* CONFIG_NO_RC4 */
d2c33b91 1729 u8 ek[32];
e6270129 1730 if (key_data_len > sizeof(gd->gtk)) {
f049052b
BG
1731 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1732 "WPA: RC4 key data too long (%lu)",
e6270129 1733 (unsigned long) key_data_len);
6fc6879b
JM
1734 return -1;
1735 }
d2c33b91 1736 os_memcpy(ek, key->key_iv, 16);
98cd3d1c 1737 os_memcpy(ek + 16, sm->ptk.kek, sm->ptk.kek_len);
e6270129
JM
1738 os_memcpy(gd->gtk, key_data, key_data_len);
1739 if (rc4_skip(ek, 32, 256, gd->gtk, key_data_len)) {
31bc66e4 1740 forced_memzero(ek, sizeof(ek));
f049052b
BG
1741 wpa_msg(sm->ctx->msg_ctx, MSG_ERROR,
1742 "WPA: RC4 failed");
7a215dfc
JM
1743 return -1;
1744 }
31bc66e4 1745 forced_memzero(ek, sizeof(ek));
7cb53ded 1746#endif /* CONFIG_NO_RC4 */
6fc6879b 1747 } else if (ver == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
e6270129 1748 if (maxkeylen % 8) {
f049052b
BG
1749 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1750 "WPA: Unsupported AES-WRAP len %lu",
e6270129 1751 (unsigned long) maxkeylen);
6fc6879b
JM
1752 return -1;
1753 }
1754 if (maxkeylen > sizeof(gd->gtk)) {
f049052b
BG
1755 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1756 "WPA: AES-WRAP key data "
1757 "too long (keydatalen=%lu maxkeylen=%lu)",
e6270129 1758 (unsigned long) key_data_len,
f049052b 1759 (unsigned long) maxkeylen);
6fc6879b
JM
1760 return -1;
1761 }
98cd3d1c
JM
1762 if (aes_unwrap(sm->ptk.kek, sm->ptk.kek_len, maxkeylen / 8,
1763 key_data, gd->gtk)) {
f049052b
BG
1764 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1765 "WPA: AES unwrap failed - could not decrypt "
1766 "GTK");
6fc6879b
JM
1767 return -1;
1768 }
1769 } else {
f049052b
BG
1770 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1771 "WPA: Unsupported key_info type %d", ver);
6fc6879b
JM
1772 return -1;
1773 }
1774 gd->tx = wpa_supplicant_gtk_tx_bit_workaround(
1775 sm, !!(key_info & WPA_KEY_INFO_TXRX));
1776 return 0;
1777}
1778
1779
1780static int wpa_supplicant_send_2_of_2(struct wpa_sm *sm,
1781 const struct wpa_eapol_key *key,
1782 int ver, u16 key_info)
1783{
5e3b5197 1784 size_t mic_len, hdrlen, rlen;
6fc6879b 1785 struct wpa_eapol_key *reply;
5e3b5197 1786 u8 *rbuf, *key_mic;
17c2559c
MV
1787 size_t kde_len = 0;
1788
1789#ifdef CONFIG_OCV
1790 if (wpa_sm_ocv_enabled(sm))
1791 kde_len = OCV_OCI_KDE_LEN;
1792#endif /* CONFIG_OCV */
6fc6879b 1793
567da5bb 1794 mic_len = wpa_mic_len(sm->key_mgmt, sm->pmk_len);
6d014ffc 1795 hdrlen = sizeof(*reply) + mic_len + 2;
6fc6879b 1796 rbuf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_KEY, NULL,
17c2559c 1797 hdrlen + kde_len, &rlen, (void *) &reply);
6fc6879b
JM
1798 if (rbuf == NULL)
1799 return -1;
1800
a14896e8
JM
1801 reply->type = (sm->proto == WPA_PROTO_RSN ||
1802 sm->proto == WPA_PROTO_OSEN) ?
6fc6879b
JM
1803 EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA;
1804 key_info &= WPA_KEY_INFO_KEY_INDEX_MASK;
4a26ccda
JM
1805 key_info |= ver | WPA_KEY_INFO_SECURE;
1806 if (mic_len)
1807 key_info |= WPA_KEY_INFO_MIC;
b44d1efd
VK
1808 else
1809 key_info |= WPA_KEY_INFO_ENCR_KEY_DATA;
6fc6879b 1810 WPA_PUT_BE16(reply->key_info, key_info);
a14896e8 1811 if (sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN)
6fc6879b
JM
1812 WPA_PUT_BE16(reply->key_length, 0);
1813 else
1814 os_memcpy(reply->key_length, key->key_length, 2);
1815 os_memcpy(reply->replay_counter, key->replay_counter,
1816 WPA_REPLAY_COUNTER_LEN);
1817
6d014ffc 1818 key_mic = (u8 *) (reply + 1);
17c2559c
MV
1819 WPA_PUT_BE16(key_mic + mic_len, kde_len); /* Key Data Length */
1820
1821#ifdef CONFIG_OCV
1822 if (wpa_sm_ocv_enabled(sm)) {
1823 struct wpa_channel_info ci;
1824 u8 *pos;
1825
1826 if (wpa_sm_channel_info(sm, &ci) != 0) {
1827 wpa_printf(MSG_WARNING,
1828 "Failed to get channel info for OCI element in EAPOL-Key 2/2");
1829 os_free(rbuf);
1830 return -1;
1831 }
1832
1833 pos = key_mic + mic_len + 2; /* Key Data */
1834 if (ocv_insert_oci_kde(&ci, &pos) < 0) {
1835 os_free(rbuf);
1836 return -1;
1837 }
1838 }
1839#endif /* CONFIG_OCV */
6fc6879b 1840
f049052b 1841 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Sending EAPOL-Key 2/2");
1049af7e
JM
1842 return wpa_eapol_key_send(sm, &sm->ptk, ver, sm->bssid, ETH_P_EAPOL,
1843 rbuf, rlen, key_mic);
6fc6879b
JM
1844}
1845
1846
1847static void wpa_supplicant_process_1_of_2(struct wpa_sm *sm,
1848 const unsigned char *src_addr,
1849 const struct wpa_eapol_key *key,
e6270129
JM
1850 const u8 *key_data,
1851 size_t key_data_len, u16 ver)
6fc6879b 1852{
e6270129 1853 u16 key_info;
6fc6879b
JM
1854 int rekey, ret;
1855 struct wpa_gtk_data gd;
73ed03f3 1856 const u8 *key_rsc;
6fc6879b 1857
04243740 1858 if (!sm->msg_3_of_4_ok && !wpa_fils_is_completed(sm)) {
761396e4
JM
1859 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1860 "WPA: Group Key Handshake started prior to completion of 4-way handshake");
1861 goto failed;
1862 }
1863
6fc6879b
JM
1864 os_memset(&gd, 0, sizeof(gd));
1865
1866 rekey = wpa_sm_get_state(sm) == WPA_COMPLETED;
f049052b
BG
1867 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: RX message 1 of Group Key "
1868 "Handshake from " MACSTR " (ver=%d)", MAC2STR(src_addr), ver);
6fc6879b
JM
1869
1870 key_info = WPA_GET_BE16(key->key_info);
6fc6879b 1871
a14896e8 1872 if (sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN) {
e6270129
JM
1873 ret = wpa_supplicant_process_1_of_2_rsn(sm, key_data,
1874 key_data_len, key_info,
6fc6879b
JM
1875 &gd);
1876 } else {
e6270129
JM
1877 ret = wpa_supplicant_process_1_of_2_wpa(sm, key, key_data,
1878 key_data_len,
1879 key_info, ver, &gd);
6fc6879b
JM
1880 }
1881
1882 wpa_sm_set_state(sm, WPA_GROUP_HANDSHAKE);
1883
1884 if (ret)
83935317 1885 goto failed;
6fc6879b 1886
73ed03f3
MS
1887 key_rsc = key->key_rsc;
1888 if (wpa_supplicant_rsc_relaxation(sm, key->key_rsc))
1889 key_rsc = null_rsc;
1890
87e2db16 1891 if (wpa_supplicant_install_gtk(sm, &gd, key_rsc, 0) ||
c93b7e18 1892 wpa_supplicant_send_2_of_2(sm, key, ver, key_info) < 0)
83935317 1893 goto failed;
31bc66e4 1894 forced_memzero(&gd, sizeof(gd));
6fc6879b
JM
1895
1896 if (rekey) {
0f057fb2 1897 wpa_msg(sm->ctx->msg_ctx, MSG_INFO, "WPA: Group rekeying "
6fc6879b
JM
1898 "completed with " MACSTR " [GTK=%s]",
1899 MAC2STR(sm->bssid), wpa_cipher_txt(sm->group_cipher));
1900 wpa_sm_cancel_auth_timeout(sm);
1901 wpa_sm_set_state(sm, WPA_COMPLETED);
1902 } else {
1903 wpa_supplicant_key_neg_complete(sm, sm->bssid,
1904 key_info &
1905 WPA_KEY_INFO_SECURE);
1906 }
392e68e8
SD
1907
1908 wpa_sm_set_rekey_offload(sm);
1909
83935317
JM
1910 return;
1911
1912failed:
31bc66e4 1913 forced_memzero(&gd, sizeof(gd));
83935317 1914 wpa_sm_deauthenticate(sm, WLAN_REASON_UNSPECIFIED);
6fc6879b
JM
1915}
1916
1917
1918static int wpa_supplicant_verify_eapol_key_mic(struct wpa_sm *sm,
6d014ffc 1919 struct wpa_eapol_key *key,
6fc6879b
JM
1920 u16 ver,
1921 const u8 *buf, size_t len)
1922{
98cd3d1c 1923 u8 mic[WPA_EAPOL_KEY_MIC_MAX_LEN];
6fc6879b 1924 int ok = 0;
567da5bb 1925 size_t mic_len = wpa_mic_len(sm->key_mgmt, sm->pmk_len);
6fc6879b 1926
6d014ffc 1927 os_memcpy(mic, key + 1, mic_len);
6fc6879b 1928 if (sm->tptk_set) {
6d014ffc 1929 os_memset(key + 1, 0, mic_len);
a6ea6653
JM
1930 if (wpa_eapol_key_mic(sm->tptk.kck, sm->tptk.kck_len,
1931 sm->key_mgmt,
1932 ver, buf, len, (u8 *) (key + 1)) < 0 ||
1933 os_memcmp_const(mic, key + 1, mic_len) != 0) {
f049052b
BG
1934 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1935 "WPA: Invalid EAPOL-Key MIC "
1936 "when using TPTK - ignoring TPTK");
525923b1
JM
1937#ifdef TEST_FUZZ
1938 wpa_printf(MSG_INFO,
1939 "TEST: Ignore Key MIC failure for fuzz testing");
1940 goto continue_fuzz;
1941#endif /* TEST_FUZZ */
6fc6879b 1942 } else {
525923b1
JM
1943#ifdef TEST_FUZZ
1944 continue_fuzz:
1945#endif /* TEST_FUZZ */
6fc6879b
JM
1946 ok = 1;
1947 sm->tptk_set = 0;
1948 sm->ptk_set = 1;
1949 os_memcpy(&sm->ptk, &sm->tptk, sizeof(sm->ptk));
d2c33b91 1950 os_memset(&sm->tptk, 0, sizeof(sm->tptk));
a00e946c
MV
1951 /*
1952 * This assures the same TPTK in sm->tptk can never be
a68e0d86 1953 * copied twice to sm->ptk as the new PTK. In
a00e946c
MV
1954 * combination with the installed flag in the wpa_ptk
1955 * struct, this assures the same PTK is only installed
1956 * once.
1957 */
1958 sm->renew_snonce = 1;
6fc6879b
JM
1959 }
1960 }
1961
1962 if (!ok && sm->ptk_set) {
6d014ffc 1963 os_memset(key + 1, 0, mic_len);
a6ea6653
JM
1964 if (wpa_eapol_key_mic(sm->ptk.kck, sm->ptk.kck_len,
1965 sm->key_mgmt,
1966 ver, buf, len, (u8 *) (key + 1)) < 0 ||
1967 os_memcmp_const(mic, key + 1, mic_len) != 0) {
f049052b
BG
1968 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1969 "WPA: Invalid EAPOL-Key MIC - "
1970 "dropping packet");
525923b1
JM
1971#ifdef TEST_FUZZ
1972 wpa_printf(MSG_INFO,
1973 "TEST: Ignore Key MIC failure for fuzz testing");
1974 goto continue_fuzz2;
1975#endif /* TEST_FUZZ */
6fc6879b
JM
1976 return -1;
1977 }
525923b1
JM
1978#ifdef TEST_FUZZ
1979 continue_fuzz2:
1980#endif /* TEST_FUZZ */
6fc6879b
JM
1981 ok = 1;
1982 }
1983
1984 if (!ok) {
f049052b
BG
1985 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1986 "WPA: Could not verify EAPOL-Key MIC - "
1987 "dropping packet");
6fc6879b
JM
1988 return -1;
1989 }
1990
1991 os_memcpy(sm->rx_replay_counter, key->replay_counter,
1992 WPA_REPLAY_COUNTER_LEN);
1993 sm->rx_replay_counter_set = 1;
1994 return 0;
1995}
1996
1997
1998/* Decrypt RSN EAPOL-Key key data (RC4 or AES-WRAP) */
1999static int wpa_supplicant_decrypt_key_data(struct wpa_sm *sm,
6d014ffc
JM
2000 struct wpa_eapol_key *key,
2001 size_t mic_len, u16 ver,
e6270129 2002 u8 *key_data, size_t *key_data_len)
6fc6879b 2003{
6fc6879b 2004 wpa_hexdump(MSG_DEBUG, "RSN: encrypted key data",
e6270129 2005 key_data, *key_data_len);
6fc6879b 2006 if (!sm->ptk_set) {
f049052b
BG
2007 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
2008 "WPA: PTK not available, cannot decrypt EAPOL-Key Key "
2009 "Data");
6fc6879b
JM
2010 return -1;
2011 }
2012
2013 /* Decrypt key data here so that this operation does not need
2014 * to be implemented separately for each message type. */
98cd3d1c 2015 if (ver == WPA_KEY_INFO_TYPE_HMAC_MD5_RC4 && sm->ptk.kek_len == 16) {
7cb53ded
JM
2016#ifdef CONFIG_NO_RC4
2017 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
2018 "WPA: RC4 not supported in the build");
2019 return -1;
2020#else /* CONFIG_NO_RC4 */
6fc6879b 2021 u8 ek[32];
ef9627cb
JM
2022
2023 wpa_printf(MSG_DEBUG, "WPA: Decrypt Key Data using RC4");
6fc6879b 2024 os_memcpy(ek, key->key_iv, 16);
98cd3d1c 2025 os_memcpy(ek + 16, sm->ptk.kek, sm->ptk.kek_len);
e6270129 2026 if (rc4_skip(ek, 32, 256, key_data, *key_data_len)) {
31bc66e4 2027 forced_memzero(ek, sizeof(ek));
f049052b
BG
2028 wpa_msg(sm->ctx->msg_ctx, MSG_ERROR,
2029 "WPA: RC4 failed");
7a215dfc
JM
2030 return -1;
2031 }
31bc66e4 2032 forced_memzero(ek, sizeof(ek));
7cb53ded 2033#endif /* CONFIG_NO_RC4 */
6fc6879b 2034 } else if (ver == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES ||
df0f01d9 2035 ver == WPA_KEY_INFO_TYPE_AES_128_CMAC ||
4bc801ab 2036 wpa_use_aes_key_wrap(sm->key_mgmt)) {
6fc6879b 2037 u8 *buf;
ef9627cb
JM
2038
2039 wpa_printf(MSG_DEBUG,
2040 "WPA: Decrypt Key Data using AES-UNWRAP (KEK length %u)",
2041 (unsigned int) sm->ptk.kek_len);
e6270129 2042 if (*key_data_len < 8 || *key_data_len % 8) {
f049052b 2043 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
e6270129
JM
2044 "WPA: Unsupported AES-WRAP len %u",
2045 (unsigned int) *key_data_len);
6fc6879b
JM
2046 return -1;
2047 }
e6270129
JM
2048 *key_data_len -= 8; /* AES-WRAP adds 8 bytes */
2049 buf = os_malloc(*key_data_len);
6fc6879b 2050 if (buf == NULL) {
f049052b
BG
2051 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
2052 "WPA: No memory for AES-UNWRAP buffer");
6fc6879b
JM
2053 return -1;
2054 }
525923b1
JM
2055#ifdef TEST_FUZZ
2056 os_memset(buf, 0x11, *key_data_len);
2057#endif /* TEST_FUZZ */
98cd3d1c 2058 if (aes_unwrap(sm->ptk.kek, sm->ptk.kek_len, *key_data_len / 8,
e6270129 2059 key_data, buf)) {
525923b1
JM
2060#ifdef TEST_FUZZ
2061 wpa_printf(MSG_INFO,
2062 "TEST: Ignore AES unwrap failure for fuzz testing");
2063 goto continue_fuzz;
2064#endif /* TEST_FUZZ */
cd5895e8 2065 bin_clear_free(buf, *key_data_len);
f049052b
BG
2066 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
2067 "WPA: AES unwrap failed - "
2068 "could not decrypt EAPOL-Key key data");
6fc6879b
JM
2069 return -1;
2070 }
525923b1
JM
2071#ifdef TEST_FUZZ
2072 continue_fuzz:
2073#endif /* TEST_FUZZ */
e6270129 2074 os_memcpy(key_data, buf, *key_data_len);
cd5895e8 2075 bin_clear_free(buf, *key_data_len);
6d014ffc 2076 WPA_PUT_BE16(((u8 *) (key + 1)) + mic_len, *key_data_len);
6fc6879b 2077 } else {
f049052b
BG
2078 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
2079 "WPA: Unsupported key_info type %d", ver);
6fc6879b
JM
2080 return -1;
2081 }
2082 wpa_hexdump_key(MSG_DEBUG, "WPA: decrypted EAPOL-Key key data",
e6270129 2083 key_data, *key_data_len);
6fc6879b
JM
2084 return 0;
2085}
2086
2087
2088/**
2089 * wpa_sm_aborted_cached - Notify WPA that PMKSA caching was aborted
2090 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2091 */
2092void wpa_sm_aborted_cached(struct wpa_sm *sm)
2093{
2094 if (sm && sm->cur_pmksa) {
f049052b
BG
2095 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2096 "RSN: Cancelling PMKSA caching attempt");
6fc6879b
JM
2097 sm->cur_pmksa = NULL;
2098 }
2099}
2100
2101
f049052b 2102static void wpa_eapol_key_dump(struct wpa_sm *sm,
5e3b5197
JM
2103 const struct wpa_eapol_key *key,
2104 unsigned int key_data_len,
2105 const u8 *mic, unsigned int mic_len)
6fc6879b
JM
2106{
2107#ifndef CONFIG_NO_STDOUT_DEBUG
2108 u16 key_info = WPA_GET_BE16(key->key_info);
2109
f049052b
BG
2110 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, " EAPOL-Key type=%d", key->type);
2111 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2112 " key_info 0x%x (ver=%d keyidx=%d rsvd=%d %s%s%s%s%s%s%s%s)",
2113 key_info, key_info & WPA_KEY_INFO_TYPE_MASK,
2114 (key_info & WPA_KEY_INFO_KEY_INDEX_MASK) >>
2115 WPA_KEY_INFO_KEY_INDEX_SHIFT,
2116 (key_info & (BIT(13) | BIT(14) | BIT(15))) >> 13,
2117 key_info & WPA_KEY_INFO_KEY_TYPE ? "Pairwise" : "Group",
2118 key_info & WPA_KEY_INFO_INSTALL ? " Install" : "",
2119 key_info & WPA_KEY_INFO_ACK ? " Ack" : "",
2120 key_info & WPA_KEY_INFO_MIC ? " MIC" : "",
2121 key_info & WPA_KEY_INFO_SECURE ? " Secure" : "",
2122 key_info & WPA_KEY_INFO_ERROR ? " Error" : "",
2123 key_info & WPA_KEY_INFO_REQUEST ? " Request" : "",
2124 key_info & WPA_KEY_INFO_ENCR_KEY_DATA ? " Encr" : "");
2125 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2126 " key_length=%u key_data_length=%u",
5e3b5197 2127 WPA_GET_BE16(key->key_length), key_data_len);
6fc6879b
JM
2128 wpa_hexdump(MSG_DEBUG, " replay_counter",
2129 key->replay_counter, WPA_REPLAY_COUNTER_LEN);
2130 wpa_hexdump(MSG_DEBUG, " key_nonce", key->key_nonce, WPA_NONCE_LEN);
2131 wpa_hexdump(MSG_DEBUG, " key_iv", key->key_iv, 16);
2132 wpa_hexdump(MSG_DEBUG, " key_rsc", key->key_rsc, 8);
2133 wpa_hexdump(MSG_DEBUG, " key_id (reserved)", key->key_id, 8);
5e3b5197 2134 wpa_hexdump(MSG_DEBUG, " key_mic", mic, mic_len);
6fc6879b
JM
2135#endif /* CONFIG_NO_STDOUT_DEBUG */
2136}
2137
2138
0ab1dd01
JM
2139#ifdef CONFIG_FILS
2140static int wpa_supp_aead_decrypt(struct wpa_sm *sm, u8 *buf, size_t buf_len,
2141 size_t *key_data_len)
2142{
2143 struct wpa_ptk *ptk;
2144 struct ieee802_1x_hdr *hdr;
2145 struct wpa_eapol_key *key;
2146 u8 *pos, *tmp;
2147 const u8 *aad[1];
2148 size_t aad_len[1];
2149
2150 if (*key_data_len < AES_BLOCK_SIZE) {
2151 wpa_printf(MSG_INFO, "No room for AES-SIV data in the frame");
2152 return -1;
2153 }
2154
2155 if (sm->tptk_set)
2156 ptk = &sm->tptk;
2157 else if (sm->ptk_set)
2158 ptk = &sm->ptk;
2159 else
2160 return -1;
2161
2162 hdr = (struct ieee802_1x_hdr *) buf;
2163 key = (struct wpa_eapol_key *) (hdr + 1);
2164 pos = (u8 *) (key + 1);
2165 pos += 2; /* Pointing at the Encrypted Key Data field */
2166
2167 tmp = os_malloc(*key_data_len);
2168 if (!tmp)
2169 return -1;
2170
2171 /* AES-SIV AAD from EAPOL protocol version field (inclusive) to
2172 * to Key Data (exclusive). */
2173 aad[0] = buf;
2174 aad_len[0] = pos - buf;
2175 if (aes_siv_decrypt(ptk->kek, ptk->kek_len, pos, *key_data_len,
2176 1, aad, aad_len, tmp) < 0) {
2177 wpa_printf(MSG_INFO, "Invalid AES-SIV data in the frame");
2178 bin_clear_free(tmp, *key_data_len);
2179 return -1;
2180 }
2181
2182 /* AEAD decryption and validation completed successfully */
2183 (*key_data_len) -= AES_BLOCK_SIZE;
2184 wpa_hexdump_key(MSG_DEBUG, "WPA: Decrypted Key Data",
2185 tmp, *key_data_len);
2186
2187 /* Replace Key Data field with the decrypted version */
2188 os_memcpy(pos, tmp, *key_data_len);
2189 pos -= 2; /* Key Data Length field */
2190 WPA_PUT_BE16(pos, *key_data_len);
2191 bin_clear_free(tmp, *key_data_len);
2192
2193 if (sm->tptk_set) {
2194 sm->tptk_set = 0;
2195 sm->ptk_set = 1;
2196 os_memcpy(&sm->ptk, &sm->tptk, sizeof(sm->ptk));
2197 os_memset(&sm->tptk, 0, sizeof(sm->tptk));
2198 }
2199
2200 os_memcpy(sm->rx_replay_counter, key->replay_counter,
2201 WPA_REPLAY_COUNTER_LEN);
2202 sm->rx_replay_counter_set = 1;
2203
2204 return 0;
2205}
2206#endif /* CONFIG_FILS */
2207
2208
6fc6879b
JM
2209/**
2210 * wpa_sm_rx_eapol - Process received WPA EAPOL frames
2211 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2212 * @src_addr: Source MAC address of the EAPOL packet
2213 * @buf: Pointer to the beginning of the EAPOL data (EAPOL header)
2214 * @len: Length of the EAPOL frame
2215 * Returns: 1 = WPA EAPOL-Key processed, 0 = not a WPA EAPOL-Key, -1 failure
2216 *
2217 * This function is called for each received EAPOL frame. Other than EAPOL-Key
2218 * frames can be skipped if filtering is done elsewhere. wpa_sm_rx_eapol() is
2219 * only processing WPA and WPA2 EAPOL-Key frames.
2220 *
2221 * The received EAPOL-Key packets are validated and valid packets are replied
2222 * to. In addition, key material (PTK, GTK) is configured at the end of a
2223 * successful key handshake.
2224 */
2225int wpa_sm_rx_eapol(struct wpa_sm *sm, const u8 *src_addr,
2226 const u8 *buf, size_t len)
2227{
e6270129 2228 size_t plen, data_len, key_data_len;
d56d7e56 2229 const struct ieee802_1x_hdr *hdr;
6fc6879b
JM
2230 struct wpa_eapol_key *key;
2231 u16 key_info, ver;
d56d7e56 2232 u8 *tmp = NULL;
6fc6879b 2233 int ret = -1;
6d014ffc 2234 u8 *mic, *key_data;
5e3b5197 2235 size_t mic_len, keyhdrlen;
6fc6879b
JM
2236
2237#ifdef CONFIG_IEEE80211R
2238 sm->ft_completed = 0;
2239#endif /* CONFIG_IEEE80211R */
2240
567da5bb 2241 mic_len = wpa_mic_len(sm->key_mgmt, sm->pmk_len);
6d014ffc 2242 keyhdrlen = sizeof(*key) + mic_len + 2;
5e3b5197
JM
2243
2244 if (len < sizeof(*hdr) + keyhdrlen) {
f049052b
BG
2245 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2246 "WPA: EAPOL frame too short to be a WPA "
2247 "EAPOL-Key (len %lu, expecting at least %lu)",
2248 (unsigned long) len,
5e3b5197 2249 (unsigned long) sizeof(*hdr) + keyhdrlen);
6fc6879b
JM
2250 return 0;
2251 }
2252
d56d7e56 2253 hdr = (const struct ieee802_1x_hdr *) buf;
6fc6879b
JM
2254 plen = be_to_host16(hdr->length);
2255 data_len = plen + sizeof(*hdr);
f049052b
BG
2256 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2257 "IEEE 802.1X RX: version=%d type=%d length=%lu",
2258 hdr->version, hdr->type, (unsigned long) plen);
6fc6879b
JM
2259
2260 if (hdr->version < EAPOL_VERSION) {
2261 /* TODO: backwards compatibility */
2262 }
2263 if (hdr->type != IEEE802_1X_TYPE_EAPOL_KEY) {
f049052b
BG
2264 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2265 "WPA: EAPOL frame (type %u) discarded, "
6fc6879b
JM
2266 "not a Key frame", hdr->type);
2267 ret = 0;
2268 goto out;
2269 }
d56d7e56 2270 wpa_hexdump(MSG_MSGDUMP, "WPA: RX EAPOL-Key", buf, len);
5e3b5197 2271 if (plen > len - sizeof(*hdr) || plen < keyhdrlen) {
f049052b
BG
2272 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2273 "WPA: EAPOL frame payload size %lu "
2274 "invalid (frame size %lu)",
2275 (unsigned long) plen, (unsigned long) len);
6fc6879b
JM
2276 ret = 0;
2277 goto out;
2278 }
d56d7e56
JM
2279 if (data_len < len) {
2280 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2281 "WPA: ignoring %lu bytes after the IEEE 802.1X data",
2282 (unsigned long) len - data_len);
2283 }
2284
2285 /*
2286 * Make a copy of the frame since we need to modify the buffer during
2287 * MAC validation and Key Data decryption.
2288 */
a1f11e34 2289 tmp = os_memdup(buf, data_len);
d56d7e56
JM
2290 if (tmp == NULL)
2291 goto out;
d56d7e56 2292 key = (struct wpa_eapol_key *) (tmp + sizeof(struct ieee802_1x_hdr));
6d014ffc
JM
2293 mic = (u8 *) (key + 1);
2294 key_data = mic + mic_len + 2;
6fc6879b
JM
2295
2296 if (key->type != EAPOL_KEY_TYPE_WPA && key->type != EAPOL_KEY_TYPE_RSN)
2297 {
f049052b
BG
2298 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2299 "WPA: EAPOL-Key type (%d) unknown, discarded",
2300 key->type);
6fc6879b
JM
2301 ret = 0;
2302 goto out;
2303 }
6fc6879b 2304
6d014ffc
JM
2305 key_data_len = WPA_GET_BE16(mic + mic_len);
2306 wpa_eapol_key_dump(sm, key, key_data_len, mic, mic_len);
5e3b5197
JM
2307
2308 if (key_data_len > plen - keyhdrlen) {
d56d7e56
JM
2309 wpa_msg(sm->ctx->msg_ctx, MSG_INFO, "WPA: Invalid EAPOL-Key "
2310 "frame - key_data overflow (%u > %u)",
e6270129 2311 (unsigned int) key_data_len,
5e3b5197 2312 (unsigned int) (plen - keyhdrlen));
d56d7e56 2313 goto out;
6fc6879b 2314 }
d56d7e56
JM
2315
2316 eapol_sm_notify_lower_layer_success(sm->eapol, 0);
6fc6879b
JM
2317 key_info = WPA_GET_BE16(key->key_info);
2318 ver = key_info & WPA_KEY_INFO_TYPE_MASK;
2319 if (ver != WPA_KEY_INFO_TYPE_HMAC_MD5_RC4 &&
6fc6879b 2320 ver != WPA_KEY_INFO_TYPE_AES_128_CMAC &&
df0f01d9 2321 ver != WPA_KEY_INFO_TYPE_HMAC_SHA1_AES &&
4bc801ab 2322 !wpa_use_akm_defined(sm->key_mgmt)) {
f049052b
BG
2323 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2324 "WPA: Unsupported EAPOL-Key descriptor version %d",
2325 ver);
6fc6879b
JM
2326 goto out;
2327 }
2328
4bc801ab 2329 if (wpa_use_akm_defined(sm->key_mgmt) &&
929a2ea5
JM
2330 ver != WPA_KEY_INFO_TYPE_AKM_DEFINED) {
2331 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2332 "RSN: Unsupported EAPOL-Key descriptor version %d (expected AKM defined = 0)",
2333 ver);
2334 goto out;
2335 }
2336
6fc6879b 2337#ifdef CONFIG_IEEE80211R
56586197 2338 if (wpa_key_mgmt_ft(sm->key_mgmt)) {
6fc6879b 2339 /* IEEE 802.11r uses a new key_info type (AES-128-CMAC). */
4bc801ab
JM
2340 if (ver != WPA_KEY_INFO_TYPE_AES_128_CMAC &&
2341 !wpa_use_akm_defined(sm->key_mgmt)) {
f049052b
BG
2342 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2343 "FT: AP did not use AES-128-CMAC");
6fc6879b
JM
2344 goto out;
2345 }
2346 } else
2347#endif /* CONFIG_IEEE80211R */
56586197 2348 if (wpa_key_mgmt_sha256(sm->key_mgmt)) {
df0f01d9 2349 if (ver != WPA_KEY_INFO_TYPE_AES_128_CMAC &&
4bc801ab 2350 !wpa_use_akm_defined(sm->key_mgmt)) {
f049052b
BG
2351 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2352 "WPA: AP did not use the "
2353 "negotiated AES-128-CMAC");
56586197
JM
2354 goto out;
2355 }
7d2ed8ba
JM
2356 } else if (sm->pairwise_cipher == WPA_CIPHER_CCMP &&
2357 !wpa_use_akm_defined(sm->key_mgmt) &&
2358 ver != WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
f049052b
BG
2359 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2360 "WPA: CCMP is used, but EAPOL-Key "
2361 "descriptor version (%d) is not 2", ver);
6fc6879b
JM
2362 if (sm->group_cipher != WPA_CIPHER_CCMP &&
2363 !(key_info & WPA_KEY_INFO_KEY_TYPE)) {
2364 /* Earlier versions of IEEE 802.11i did not explicitly
2365 * require version 2 descriptor for all EAPOL-Key
2366 * packets, so allow group keys to use version 1 if
2367 * CCMP is not used for them. */
f049052b
BG
2368 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2369 "WPA: Backwards compatibility: allow invalid "
2370 "version for non-CCMP group keys");
9f6a7cdd
JM
2371 } else if (ver == WPA_KEY_INFO_TYPE_AES_128_CMAC) {
2372 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2373 "WPA: Interoperability workaround: allow incorrect (should have been HMAC-SHA1), but stronger (is AES-128-CMAC), descriptor version to be used");
6fc6879b
JM
2374 } else
2375 goto out;
801e1173 2376 } else if (sm->pairwise_cipher == WPA_CIPHER_GCMP &&
4bc801ab 2377 !wpa_use_akm_defined(sm->key_mgmt) &&
801e1173 2378 ver != WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
eb7719ff
JM
2379 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2380 "WPA: GCMP is used, but EAPOL-Key "
2381 "descriptor version (%d) is not 2", ver);
2382 goto out;
2383 }
6fc6879b 2384
a0bf1b68 2385 if (sm->rx_replay_counter_set &&
6fc6879b
JM
2386 os_memcmp(key->replay_counter, sm->rx_replay_counter,
2387 WPA_REPLAY_COUNTER_LEN) <= 0) {
f049052b
BG
2388 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
2389 "WPA: EAPOL-Key Replay Counter did not increase - "
2390 "dropping packet");
6fc6879b
JM
2391 goto out;
2392 }
2393
a0bf1b68
JM
2394 if (key_info & WPA_KEY_INFO_SMK_MESSAGE) {
2395 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2396 "WPA: Unsupported SMK bit in key_info");
2397 goto out;
2398 }
2399
2400 if (!(key_info & WPA_KEY_INFO_ACK)) {
f049052b
BG
2401 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2402 "WPA: No Ack bit in key_info");
6fc6879b
JM
2403 goto out;
2404 }
2405
2406 if (key_info & WPA_KEY_INFO_REQUEST) {
f049052b
BG
2407 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2408 "WPA: EAPOL-Key with Request bit - dropped");
6fc6879b
JM
2409 goto out;
2410 }
2411
a0bf1b68 2412 if ((key_info & WPA_KEY_INFO_MIC) &&
6d014ffc 2413 wpa_supplicant_verify_eapol_key_mic(sm, key, ver, tmp, data_len))
6fc6879b
JM
2414 goto out;
2415
0ab1dd01
JM
2416#ifdef CONFIG_FILS
2417 if (!mic_len && (key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
2418 if (wpa_supp_aead_decrypt(sm, tmp, data_len, &key_data_len))
2419 goto out;
2420 }
2421#endif /* CONFIG_FILS */
2422
a14896e8 2423 if ((sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN) &&
0ab1dd01 2424 (key_info & WPA_KEY_INFO_ENCR_KEY_DATA) && mic_len) {
3e34cfdf
MV
2425 /*
2426 * Only decrypt the Key Data field if the frame's authenticity
2427 * was verified. When using AES-SIV (FILS), the MIC flag is not
2428 * set, so this check should only be performed if mic_len != 0
2429 * which is the case in this code branch.
2430 */
2431 if (!(key_info & WPA_KEY_INFO_MIC)) {
2432 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
2433 "WPA: Ignore EAPOL-Key with encrypted but unauthenticated data");
2434 goto out;
2435 }
6d014ffc
JM
2436 if (wpa_supplicant_decrypt_key_data(sm, key, mic_len,
2437 ver, key_data,
e6270129 2438 &key_data_len))
d56d7e56 2439 goto out;
6fc6879b
JM
2440 }
2441
2442 if (key_info & WPA_KEY_INFO_KEY_TYPE) {
2443 if (key_info & WPA_KEY_INFO_KEY_INDEX_MASK) {
f049052b
BG
2444 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
2445 "WPA: Ignored EAPOL-Key (Pairwise) with "
2446 "non-zero key index");
6fc6879b
JM
2447 goto out;
2448 }
a0bf1b68
JM
2449 if (key_info & (WPA_KEY_INFO_MIC |
2450 WPA_KEY_INFO_ENCR_KEY_DATA)) {
6fc6879b 2451 /* 3/4 4-Way Handshake */
e6270129
JM
2452 wpa_supplicant_process_3_of_4(sm, key, ver, key_data,
2453 key_data_len);
6fc6879b
JM
2454 } else {
2455 /* 1/4 4-Way Handshake */
2456 wpa_supplicant_process_1_of_4(sm, src_addr, key,
e6270129
JM
2457 ver, key_data,
2458 key_data_len);
6fc6879b 2459 }
6fc6879b 2460 } else {
16eb4858
JM
2461 if ((mic_len && (key_info & WPA_KEY_INFO_MIC)) ||
2462 (!mic_len && (key_info & WPA_KEY_INFO_ENCR_KEY_DATA))) {
6fc6879b
JM
2463 /* 1/2 Group Key Handshake */
2464 wpa_supplicant_process_1_of_2(sm, src_addr, key,
e6270129
JM
2465 key_data, key_data_len,
2466 ver);
6fc6879b 2467 } else {
f049052b 2468 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
16eb4858 2469 "WPA: EAPOL-Key (Group) without Mic/Encr bit - "
f049052b 2470 "dropped");
6fc6879b
JM
2471 }
2472 }
2473
2474 ret = 1;
2475
2476out:
fbfc974c 2477 bin_clear_free(tmp, data_len);
6fc6879b
JM
2478 return ret;
2479}
2480
2481
2482#ifdef CONFIG_CTRL_IFACE
6fc6879b
JM
2483static u32 wpa_key_mgmt_suite(struct wpa_sm *sm)
2484{
2485 switch (sm->key_mgmt) {
2486 case WPA_KEY_MGMT_IEEE8021X:
a14896e8
JM
2487 return ((sm->proto == WPA_PROTO_RSN ||
2488 sm->proto == WPA_PROTO_OSEN) ?
6fc6879b
JM
2489 RSN_AUTH_KEY_MGMT_UNSPEC_802_1X :
2490 WPA_AUTH_KEY_MGMT_UNSPEC_802_1X);
2491 case WPA_KEY_MGMT_PSK:
2492 return (sm->proto == WPA_PROTO_RSN ?
2493 RSN_AUTH_KEY_MGMT_PSK_OVER_802_1X :
2494 WPA_AUTH_KEY_MGMT_PSK_OVER_802_1X);
2495#ifdef CONFIG_IEEE80211R
2496 case WPA_KEY_MGMT_FT_IEEE8021X:
2497 return RSN_AUTH_KEY_MGMT_FT_802_1X;
2498 case WPA_KEY_MGMT_FT_PSK:
2499 return RSN_AUTH_KEY_MGMT_FT_PSK;
2500#endif /* CONFIG_IEEE80211R */
56586197
JM
2501 case WPA_KEY_MGMT_IEEE8021X_SHA256:
2502 return RSN_AUTH_KEY_MGMT_802_1X_SHA256;
2503 case WPA_KEY_MGMT_PSK_SHA256:
2504 return RSN_AUTH_KEY_MGMT_PSK_SHA256;
369c8d7b
JM
2505 case WPA_KEY_MGMT_CCKM:
2506 return (sm->proto == WPA_PROTO_RSN ?
2507 RSN_AUTH_KEY_MGMT_CCKM:
2508 WPA_AUTH_KEY_MGMT_CCKM);
6fc6879b
JM
2509 case WPA_KEY_MGMT_WPA_NONE:
2510 return WPA_AUTH_KEY_MGMT_NONE;
666497c8
JM
2511 case WPA_KEY_MGMT_IEEE8021X_SUITE_B:
2512 return RSN_AUTH_KEY_MGMT_802_1X_SUITE_B;
5e3b5197
JM
2513 case WPA_KEY_MGMT_IEEE8021X_SUITE_B_192:
2514 return RSN_AUTH_KEY_MGMT_802_1X_SUITE_B_192;
6fc6879b
JM
2515 default:
2516 return 0;
2517 }
2518}
2519
2520
6fc6879b
JM
2521#define RSN_SUITE "%02x-%02x-%02x-%d"
2522#define RSN_SUITE_ARG(s) \
2523((s) >> 24) & 0xff, ((s) >> 16) & 0xff, ((s) >> 8) & 0xff, (s) & 0xff
2524
2525/**
2526 * wpa_sm_get_mib - Dump text list of MIB entries
2527 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2528 * @buf: Buffer for the list
2529 * @buflen: Length of the buffer
2530 * Returns: Number of bytes written to buffer
2531 *
2532 * This function is used fetch dot11 MIB variables.
2533 */
2534int wpa_sm_get_mib(struct wpa_sm *sm, char *buf, size_t buflen)
2535{
2536 char pmkid_txt[PMKID_LEN * 2 + 1];
2537 int rsna, ret;
2538 size_t len;
2539
2540 if (sm->cur_pmksa) {
2541 wpa_snprintf_hex(pmkid_txt, sizeof(pmkid_txt),
2542 sm->cur_pmksa->pmkid, PMKID_LEN);
2543 } else
2544 pmkid_txt[0] = '\0';
2545
56586197
JM
2546 if ((wpa_key_mgmt_wpa_psk(sm->key_mgmt) ||
2547 wpa_key_mgmt_wpa_ieee8021x(sm->key_mgmt)) &&
6fc6879b
JM
2548 sm->proto == WPA_PROTO_RSN)
2549 rsna = 1;
2550 else
2551 rsna = 0;
2552
2553 ret = os_snprintf(buf, buflen,
2554 "dot11RSNAOptionImplemented=TRUE\n"
2555 "dot11RSNAPreauthenticationImplemented=TRUE\n"
2556 "dot11RSNAEnabled=%s\n"
2557 "dot11RSNAPreauthenticationEnabled=%s\n"
2558 "dot11RSNAConfigVersion=%d\n"
2559 "dot11RSNAConfigPairwiseKeysSupported=5\n"
2560 "dot11RSNAConfigGroupCipherSize=%d\n"
2561 "dot11RSNAConfigPMKLifetime=%d\n"
2562 "dot11RSNAConfigPMKReauthThreshold=%d\n"
2563 "dot11RSNAConfigNumberOfPTKSAReplayCounters=1\n"
2564 "dot11RSNAConfigSATimeout=%d\n",
2565 rsna ? "TRUE" : "FALSE",
2566 rsna ? "TRUE" : "FALSE",
2567 RSN_VERSION,
c3550295 2568 wpa_cipher_key_len(sm->group_cipher) * 8,
6fc6879b
JM
2569 sm->dot11RSNAConfigPMKLifetime,
2570 sm->dot11RSNAConfigPMKReauthThreshold,
2571 sm->dot11RSNAConfigSATimeout);
d85e1fc8 2572 if (os_snprintf_error(buflen, ret))
6fc6879b
JM
2573 return 0;
2574 len = ret;
2575
2576 ret = os_snprintf(
2577 buf + len, buflen - len,
2578 "dot11RSNAAuthenticationSuiteSelected=" RSN_SUITE "\n"
2579 "dot11RSNAPairwiseCipherSelected=" RSN_SUITE "\n"
2580 "dot11RSNAGroupCipherSelected=" RSN_SUITE "\n"
2581 "dot11RSNAPMKIDUsed=%s\n"
2582 "dot11RSNAAuthenticationSuiteRequested=" RSN_SUITE "\n"
2583 "dot11RSNAPairwiseCipherRequested=" RSN_SUITE "\n"
2584 "dot11RSNAGroupCipherRequested=" RSN_SUITE "\n"
2585 "dot11RSNAConfigNumberOfGTKSAReplayCounters=0\n"
2586 "dot11RSNA4WayHandshakeFailures=%u\n",
2587 RSN_SUITE_ARG(wpa_key_mgmt_suite(sm)),
c3550295
JM
2588 RSN_SUITE_ARG(wpa_cipher_to_suite(sm->proto,
2589 sm->pairwise_cipher)),
2590 RSN_SUITE_ARG(wpa_cipher_to_suite(sm->proto,
2591 sm->group_cipher)),
6fc6879b
JM
2592 pmkid_txt,
2593 RSN_SUITE_ARG(wpa_key_mgmt_suite(sm)),
c3550295
JM
2594 RSN_SUITE_ARG(wpa_cipher_to_suite(sm->proto,
2595 sm->pairwise_cipher)),
2596 RSN_SUITE_ARG(wpa_cipher_to_suite(sm->proto,
2597 sm->group_cipher)),
6fc6879b 2598 sm->dot11RSNA4WayHandshakeFailures);
1f102d3b 2599 if (!os_snprintf_error(buflen - len, ret))
6fc6879b
JM
2600 len += ret;
2601
2602 return (int) len;
2603}
2604#endif /* CONFIG_CTRL_IFACE */
2605
2606
2607static void wpa_sm_pmksa_free_cb(struct rsn_pmksa_cache_entry *entry,
6aaac006 2608 void *ctx, enum pmksa_free_reason reason)
6fc6879b
JM
2609{
2610 struct wpa_sm *sm = ctx;
6aaac006 2611 int deauth = 0;
6fc6879b 2612
6aaac006
DW
2613 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "RSN: PMKSA cache entry free_cb: "
2614 MACSTR " reason=%d", MAC2STR(entry->aa), reason);
2615
2616 if (sm->cur_pmksa == entry) {
2617 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2618 "RSN: %s current PMKSA entry",
2619 reason == PMKSA_REPLACE ? "replaced" : "removed");
2620 pmksa_cache_clear_current(sm);
2621
2622 /*
2623 * If an entry is simply being replaced, there's no need to
2624 * deauthenticate because it will be immediately re-added.
2625 * This happens when EAP authentication is completed again
2626 * (reauth or failed PMKSA caching attempt).
2627 */
2628 if (reason != PMKSA_REPLACE)
2629 deauth = 1;
2630 }
2631
2632 if (reason == PMKSA_EXPIRE &&
6fc6879b
JM
2633 (sm->pmk_len == entry->pmk_len &&
2634 os_memcmp(sm->pmk, entry->pmk, sm->pmk_len) == 0)) {
f049052b 2635 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
6aaac006
DW
2636 "RSN: deauthenticating due to expired PMK");
2637 pmksa_cache_clear_current(sm);
2638 deauth = 1;
2639 }
6fc6879b 2640
6aaac006 2641 if (deauth) {
1bd13110 2642 sm->pmk_len = 0;
6fc6879b
JM
2643 os_memset(sm->pmk, 0, sizeof(sm->pmk));
2644 wpa_sm_deauthenticate(sm, WLAN_REASON_UNSPECIFIED);
6fc6879b
JM
2645 }
2646}
2647
2648
2649/**
2650 * wpa_sm_init - Initialize WPA state machine
2651 * @ctx: Context pointer for callbacks; this needs to be an allocated buffer
2652 * Returns: Pointer to the allocated WPA state machine data
2653 *
2654 * This function is used to allocate a new WPA state machine and the returned
2655 * value is passed to all WPA state machine calls.
2656 */
2657struct wpa_sm * wpa_sm_init(struct wpa_sm_ctx *ctx)
2658{
2659 struct wpa_sm *sm;
2660
2661 sm = os_zalloc(sizeof(*sm));
2662 if (sm == NULL)
2663 return NULL;
c5b26e33 2664 dl_list_init(&sm->pmksa_candidates);
6fc6879b
JM
2665 sm->renew_snonce = 1;
2666 sm->ctx = ctx;
2667
2668 sm->dot11RSNAConfigPMKLifetime = 43200;
2669 sm->dot11RSNAConfigPMKReauthThreshold = 70;
2670 sm->dot11RSNAConfigSATimeout = 60;
2671
2672 sm->pmksa = pmksa_cache_init(wpa_sm_pmksa_free_cb, sm, sm);
2673 if (sm->pmksa == NULL) {
f049052b
BG
2674 wpa_msg(sm->ctx->msg_ctx, MSG_ERROR,
2675 "RSN: PMKSA cache initialization failed");
6fc6879b
JM
2676 os_free(sm);
2677 return NULL;
2678 }
2679
2680 return sm;
2681}
2682
2683
2684/**
2685 * wpa_sm_deinit - Deinitialize WPA state machine
2686 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2687 */
2688void wpa_sm_deinit(struct wpa_sm *sm)
2689{
2690 if (sm == NULL)
2691 return;
2692 pmksa_cache_deinit(sm->pmksa);
2693 eloop_cancel_timeout(wpa_sm_start_preauth, sm, NULL);
581a8cde 2694 eloop_cancel_timeout(wpa_sm_rekey_ptk, sm, NULL);
6fc6879b 2695 os_free(sm->assoc_wpa_ie);
6d6c8877 2696 os_free(sm->assoc_rsnxe);
6fc6879b
JM
2697 os_free(sm->ap_wpa_ie);
2698 os_free(sm->ap_rsn_ie);
146889e3 2699 os_free(sm->ap_rsnxe);
71932544 2700 wpa_sm_drop_sa(sm);
6fc6879b 2701 os_free(sm->ctx);
55046414
JM
2702#ifdef CONFIG_IEEE80211R
2703 os_free(sm->assoc_resp_ies);
2704#endif /* CONFIG_IEEE80211R */
651c6a84
JM
2705#ifdef CONFIG_TESTING_OPTIONS
2706 wpabuf_free(sm->test_assoc_ie);
2707#endif /* CONFIG_TESTING_OPTIONS */
0119d442 2708#ifdef CONFIG_FILS_SK_PFS
76e20f4f 2709 crypto_ecdh_deinit(sm->fils_ecdh);
0119d442 2710#endif /* CONFIG_FILS_SK_PFS */
7d440a3b
JM
2711#ifdef CONFIG_FILS
2712 wpabuf_free(sm->fils_ft_ies);
2713#endif /* CONFIG_FILS */
0a614799
JM
2714#ifdef CONFIG_OWE
2715 crypto_ecdh_deinit(sm->owe_ecdh);
2716#endif /* CONFIG_OWE */
10ec6a5f
JM
2717#ifdef CONFIG_DPP2
2718 wpabuf_clear_free(sm->dpp_z);
2719#endif /* CONFIG_DPP2 */
6fc6879b
JM
2720 os_free(sm);
2721}
2722
2723
2724/**
2725 * wpa_sm_notify_assoc - Notify WPA state machine about association
2726 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2727 * @bssid: The BSSID of the new association
2728 *
2729 * This function is called to let WPA state machine know that the connection
2730 * was established.
2731 */
2732void wpa_sm_notify_assoc(struct wpa_sm *sm, const u8 *bssid)
2733{
cb5132bb 2734 int clear_keys = 1;
58a98fb0 2735
6fc6879b
JM
2736 if (sm == NULL)
2737 return;
2738
f049052b
BG
2739 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2740 "WPA: Association event - clear replay counter");
6fc6879b
JM
2741 os_memcpy(sm->bssid, bssid, ETH_ALEN);
2742 os_memset(sm->rx_replay_counter, 0, WPA_REPLAY_COUNTER_LEN);
2743 sm->rx_replay_counter_set = 0;
2744 sm->renew_snonce = 1;
2745 if (os_memcmp(sm->preauth_bssid, bssid, ETH_ALEN) == 0)
2746 rsn_preauth_deinit(sm);
2747
2748#ifdef CONFIG_IEEE80211R
58a98fb0 2749 if (wpa_ft_is_completed(sm)) {
5d5a9f00
JM
2750 /*
2751 * Clear portValid to kick EAPOL state machine to re-enter
2752 * AUTHENTICATED state to get the EAPOL port Authorized.
2753 */
2754 eapol_sm_notify_portValid(sm->eapol, FALSE);
6fc6879b
JM
2755 wpa_supplicant_key_neg_complete(sm, sm->bssid, 1);
2756
2757 /* Prepare for the next transition */
76b7981d 2758 wpa_ft_prepare_auth_request(sm, NULL);
58a98fb0 2759
cb5132bb 2760 clear_keys = 0;
0a1a82db
JM
2761 sm->ft_protocol = 1;
2762 } else {
2763 sm->ft_protocol = 0;
6fc6879b
JM
2764 }
2765#endif /* CONFIG_IEEE80211R */
706df429
JM
2766#ifdef CONFIG_FILS
2767 if (sm->fils_completed) {
2768 /*
2769 * Clear portValid to kick EAPOL state machine to re-enter
2770 * AUTHENTICATED state to get the EAPOL port Authorized.
2771 */
2772 wpa_supplicant_key_neg_complete(sm, sm->bssid, 1);
cb5132bb 2773 clear_keys = 0;
706df429
JM
2774 }
2775#endif /* CONFIG_FILS */
58a98fb0 2776
cb5132bb 2777 if (clear_keys) {
58a98fb0
JM
2778 /*
2779 * IEEE 802.11, 8.4.10: Delete PTK SA on (re)association if
2780 * this is not part of a Fast BSS Transition.
2781 */
f049052b 2782 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Clear old PTK");
58a98fb0 2783 sm->ptk_set = 0;
d2c33b91 2784 os_memset(&sm->ptk, 0, sizeof(sm->ptk));
58a98fb0 2785 sm->tptk_set = 0;
d2c33b91 2786 os_memset(&sm->tptk, 0, sizeof(sm->tptk));
cb5132bb 2787 os_memset(&sm->gtk, 0, sizeof(sm->gtk));
87e2db16 2788 os_memset(&sm->gtk_wnm_sleep, 0, sizeof(sm->gtk_wnm_sleep));
cb5132bb 2789 os_memset(&sm->igtk, 0, sizeof(sm->igtk));
87e2db16 2790 os_memset(&sm->igtk_wnm_sleep, 0, sizeof(sm->igtk_wnm_sleep));
58a98fb0 2791 }
40cf22e6
JM
2792
2793#ifdef CONFIG_TDLS
2794 wpa_tdls_assoc(sm);
2795#endif /* CONFIG_TDLS */
25ef8529
JM
2796
2797#ifdef CONFIG_P2P
2798 os_memset(sm->p2p_ip_addr, 0, sizeof(sm->p2p_ip_addr));
2799#endif /* CONFIG_P2P */
6fc6879b
JM
2800}
2801
2802
2803/**
2804 * wpa_sm_notify_disassoc - Notify WPA state machine about disassociation
2805 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2806 *
2807 * This function is called to let WPA state machine know that the connection
2808 * was lost. This will abort any existing pre-authentication session.
2809 */
2810void wpa_sm_notify_disassoc(struct wpa_sm *sm)
2811{
ac8e074e
JM
2812 eloop_cancel_timeout(wpa_sm_start_preauth, sm, NULL);
2813 eloop_cancel_timeout(wpa_sm_rekey_ptk, sm, NULL);
6fc6879b 2814 rsn_preauth_deinit(sm);
0639970d 2815 pmksa_cache_clear_current(sm);
6fc6879b
JM
2816 if (wpa_sm_get_state(sm) == WPA_4WAY_HANDSHAKE)
2817 sm->dot11RSNA4WayHandshakeFailures++;
40cf22e6
JM
2818#ifdef CONFIG_TDLS
2819 wpa_tdls_disassoc(sm);
2820#endif /* CONFIG_TDLS */
706df429
JM
2821#ifdef CONFIG_FILS
2822 sm->fils_completed = 0;
2823#endif /* CONFIG_FILS */
2a9c5217
JM
2824#ifdef CONFIG_IEEE80211R
2825 sm->ft_reassoc_completed = 0;
0a1a82db 2826 sm->ft_protocol = 0;
2a9c5217 2827#endif /* CONFIG_IEEE80211R */
71932544
JM
2828
2829 /* Keys are not needed in the WPA state machine anymore */
2830 wpa_sm_drop_sa(sm);
761396e4
JM
2831
2832 sm->msg_3_of_4_ok = 0;
c0fe5f12 2833 os_memset(sm->bssid, 0, ETH_ALEN);
6fc6879b
JM
2834}
2835
2836
2837/**
2838 * wpa_sm_set_pmk - Set PMK
2839 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2840 * @pmk: The new PMK
2841 * @pmk_len: The length of the new PMK in bytes
70c93963 2842 * @pmkid: Calculated PMKID
bc26ac50 2843 * @bssid: AA to add into PMKSA cache or %NULL to not cache the PMK
6fc6879b
JM
2844 *
2845 * Configure the PMK for WPA state machine.
2846 */
bc26ac50 2847void wpa_sm_set_pmk(struct wpa_sm *sm, const u8 *pmk, size_t pmk_len,
70c93963 2848 const u8 *pmkid, const u8 *bssid)
6fc6879b
JM
2849{
2850 if (sm == NULL)
2851 return;
2852
00583ef1
JM
2853 wpa_hexdump_key(MSG_DEBUG, "WPA: Set PMK based on external data",
2854 pmk, pmk_len);
6fc6879b
JM
2855 sm->pmk_len = pmk_len;
2856 os_memcpy(sm->pmk, pmk, pmk_len);
2857
2858#ifdef CONFIG_IEEE80211R
2859 /* Set XXKey to be PSK for FT key derivation */
2860 sm->xxkey_len = pmk_len;
2861 os_memcpy(sm->xxkey, pmk, pmk_len);
2862#endif /* CONFIG_IEEE80211R */
bc26ac50
JM
2863
2864 if (bssid) {
70c93963 2865 pmksa_cache_add(sm->pmksa, pmk, pmk_len, pmkid, NULL, 0,
087a1f4e 2866 bssid, sm->own_addr,
869af307 2867 sm->network_ctx, sm->key_mgmt, NULL);
bc26ac50 2868 }
6fc6879b
JM
2869}
2870
2871
2872/**
2873 * wpa_sm_set_pmk_from_pmksa - Set PMK based on the current PMKSA
2874 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2875 *
2876 * Take the PMK from the current PMKSA into use. If no PMKSA is active, the PMK
2877 * will be cleared.
2878 */
2879void wpa_sm_set_pmk_from_pmksa(struct wpa_sm *sm)
2880{
2881 if (sm == NULL)
2882 return;
2883
2884 if (sm->cur_pmksa) {
00583ef1
JM
2885 wpa_hexdump_key(MSG_DEBUG,
2886 "WPA: Set PMK based on current PMKSA",
2887 sm->cur_pmksa->pmk, sm->cur_pmksa->pmk_len);
6fc6879b
JM
2888 sm->pmk_len = sm->cur_pmksa->pmk_len;
2889 os_memcpy(sm->pmk, sm->cur_pmksa->pmk, sm->pmk_len);
2890 } else {
00583ef1 2891 wpa_printf(MSG_DEBUG, "WPA: No current PMKSA - clear PMK");
b488a129
JM
2892 sm->pmk_len = 0;
2893 os_memset(sm->pmk, 0, PMK_LEN_MAX);
6fc6879b
JM
2894 }
2895}
2896
2897
2898/**
2899 * wpa_sm_set_fast_reauth - Set fast reauthentication (EAP) enabled/disabled
2900 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2901 * @fast_reauth: Whether fast reauthentication (EAP) is allowed
2902 */
2903void wpa_sm_set_fast_reauth(struct wpa_sm *sm, int fast_reauth)
2904{
2905 if (sm)
2906 sm->fast_reauth = fast_reauth;
2907}
2908
2909
2910/**
2911 * wpa_sm_set_scard_ctx - Set context pointer for smartcard callbacks
2912 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2913 * @scard_ctx: Context pointer for smartcard related callback functions
2914 */
2915void wpa_sm_set_scard_ctx(struct wpa_sm *sm, void *scard_ctx)
2916{
2917 if (sm == NULL)
2918 return;
2919 sm->scard_ctx = scard_ctx;
2920 if (sm->preauth_eapol)
2921 eapol_sm_register_scard_ctx(sm->preauth_eapol, scard_ctx);
2922}
2923
2924
2925/**
0d445cd3 2926 * wpa_sm_set_config - Notification of current configuration change
6fc6879b
JM
2927 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2928 * @config: Pointer to current network configuration
2929 *
2930 * Notify WPA state machine that configuration has changed. config will be
2931 * stored as a backpointer to network configuration. This can be %NULL to clear
2932 * the stored pointed.
2933 */
2934void wpa_sm_set_config(struct wpa_sm *sm, struct rsn_supp_config *config)
2935{
2936 if (!sm)
2937 return;
2938
6fc6879b 2939 if (config) {
886a807f 2940 sm->network_ctx = config->network_ctx;
6fc6879b
JM
2941 sm->allowed_pairwise_cipher = config->allowed_pairwise_cipher;
2942 sm->proactive_key_caching = config->proactive_key_caching;
2943 sm->eap_workaround = config->eap_workaround;
2944 sm->eap_conf_ctx = config->eap_conf_ctx;
2945 if (config->ssid) {
2946 os_memcpy(sm->ssid, config->ssid, config->ssid_len);
2947 sm->ssid_len = config->ssid_len;
2948 } else
2949 sm->ssid_len = 0;
581a8cde 2950 sm->wpa_ptk_rekey = config->wpa_ptk_rekey;
25ef8529 2951 sm->p2p = config->p2p;
73ed03f3 2952 sm->wpa_rsc_relaxation = config->wpa_rsc_relaxation;
8b138d28 2953 sm->owe_ptk_workaround = config->owe_ptk_workaround;
869af307
JM
2954#ifdef CONFIG_FILS
2955 if (config->fils_cache_id) {
2956 sm->fils_cache_id_set = 1;
2957 os_memcpy(sm->fils_cache_id, config->fils_cache_id,
2958 FILS_CACHE_ID_LEN);
2959 } else {
2960 sm->fils_cache_id_set = 0;
2961 }
ecbf59e6 2962 sm->beacon_prot = config->beacon_prot;
869af307 2963#endif /* CONFIG_FILS */
6fc6879b 2964 } else {
886a807f 2965 sm->network_ctx = NULL;
6fc6879b
JM
2966 sm->allowed_pairwise_cipher = 0;
2967 sm->proactive_key_caching = 0;
2968 sm->eap_workaround = 0;
2969 sm->eap_conf_ctx = NULL;
2970 sm->ssid_len = 0;
581a8cde 2971 sm->wpa_ptk_rekey = 0;
25ef8529 2972 sm->p2p = 0;
73ed03f3 2973 sm->wpa_rsc_relaxation = 0;
8b138d28 2974 sm->owe_ptk_workaround = 0;
ecbf59e6 2975 sm->beacon_prot = 0;
6fc6879b 2976 }
6fc6879b
JM
2977}
2978
2979
2980/**
2981 * wpa_sm_set_own_addr - Set own MAC address
2982 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2983 * @addr: Own MAC address
2984 */
2985void wpa_sm_set_own_addr(struct wpa_sm *sm, const u8 *addr)
2986{
2987 if (sm)
2988 os_memcpy(sm->own_addr, addr, ETH_ALEN);
2989}
2990
2991
2992/**
2993 * wpa_sm_set_ifname - Set network interface name
2994 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2995 * @ifname: Interface name
2996 * @bridge_ifname: Optional bridge interface name (for pre-auth)
2997 */
2998void wpa_sm_set_ifname(struct wpa_sm *sm, const char *ifname,
2999 const char *bridge_ifname)
3000{
3001 if (sm) {
3002 sm->ifname = ifname;
3003 sm->bridge_ifname = bridge_ifname;
3004 }
3005}
3006
3007
3008/**
3009 * wpa_sm_set_eapol - Set EAPOL state machine pointer
3010 * @sm: Pointer to WPA state machine data from wpa_sm_init()
3011 * @eapol: Pointer to EAPOL state machine allocated with eapol_sm_init()
3012 */
3013void wpa_sm_set_eapol(struct wpa_sm *sm, struct eapol_sm *eapol)
3014{
3015 if (sm)
3016 sm->eapol = eapol;
3017}
3018
3019
3020/**
3021 * wpa_sm_set_param - Set WPA state machine parameters
3022 * @sm: Pointer to WPA state machine data from wpa_sm_init()
3023 * @param: Parameter field
3024 * @value: Parameter value
3025 * Returns: 0 on success, -1 on failure
3026 */
3027int wpa_sm_set_param(struct wpa_sm *sm, enum wpa_sm_conf_params param,
3028 unsigned int value)
3029{
3030 int ret = 0;
3031
3032 if (sm == NULL)
3033 return -1;
3034
3035 switch (param) {
3036 case RSNA_PMK_LIFETIME:
3037 if (value > 0)
3038 sm->dot11RSNAConfigPMKLifetime = value;
3039 else
3040 ret = -1;
3041 break;
3042 case RSNA_PMK_REAUTH_THRESHOLD:
3043 if (value > 0 && value <= 100)
3044 sm->dot11RSNAConfigPMKReauthThreshold = value;
3045 else
3046 ret = -1;
3047 break;
3048 case RSNA_SA_TIMEOUT:
3049 if (value > 0)
3050 sm->dot11RSNAConfigSATimeout = value;
3051 else
3052 ret = -1;
3053 break;
3054 case WPA_PARAM_PROTO:
3055 sm->proto = value;
3056 break;
3057 case WPA_PARAM_PAIRWISE:
3058 sm->pairwise_cipher = value;
3059 break;
3060 case WPA_PARAM_GROUP:
3061 sm->group_cipher = value;
3062 break;
3063 case WPA_PARAM_KEY_MGMT:
3064 sm->key_mgmt = value;
3065 break;
6fc6879b
JM
3066 case WPA_PARAM_MGMT_GROUP:
3067 sm->mgmt_group_cipher = value;
3068 break;
6fc6879b
JM
3069 case WPA_PARAM_RSN_ENABLED:
3070 sm->rsn_enabled = value;
3071 break;
e820cf95
JM
3072 case WPA_PARAM_MFP:
3073 sm->mfp = value;
3074 break;
55c12f5d
MV
3075 case WPA_PARAM_OCV:
3076 sm->ocv = value;
3077 break;
6d6c8877
JM
3078 case WPA_PARAM_SAE_PWE:
3079 sm->sae_pwe = value;
3080 break;
6fc6879b
JM
3081 default:
3082 break;
3083 }
3084
3085 return ret;
3086}
3087
3088
6fc6879b
JM
3089/**
3090 * wpa_sm_get_status - Get WPA state machine
3091 * @sm: Pointer to WPA state machine data from wpa_sm_init()
3092 * @buf: Buffer for status information
3093 * @buflen: Maximum buffer length
3094 * @verbose: Whether to include verbose status information
3095 * Returns: Number of bytes written to buf.
3096 *
3097 * Query WPA state machine for status information. This function fills in
3098 * a text area with current status information. If the buffer (buf) is not
3099 * large enough, status information will be truncated to fit the buffer.
3100 */
3101int wpa_sm_get_status(struct wpa_sm *sm, char *buf, size_t buflen,
3102 int verbose)
3103{
3104 char *pos = buf, *end = buf + buflen;
3105 int ret;
3106
3107 ret = os_snprintf(pos, end - pos,
3108 "pairwise_cipher=%s\n"
3109 "group_cipher=%s\n"
3110 "key_mgmt=%s\n",
3111 wpa_cipher_txt(sm->pairwise_cipher),
3112 wpa_cipher_txt(sm->group_cipher),
3113 wpa_key_mgmt_txt(sm->key_mgmt, sm->proto));
d85e1fc8 3114 if (os_snprintf_error(end - pos, ret))
6fc6879b
JM
3115 return pos - buf;
3116 pos += ret;
13e1d2e2
JM
3117
3118 if (sm->mfp != NO_MGMT_FRAME_PROTECTION && sm->ap_rsn_ie) {
3119 struct wpa_ie_data rsn;
3120 if (wpa_parse_wpa_ie_rsn(sm->ap_rsn_ie, sm->ap_rsn_ie_len, &rsn)
3121 >= 0 &&
3122 rsn.capabilities & (WPA_CAPABILITY_MFPR |
3123 WPA_CAPABILITY_MFPC)) {
3a5954ef
JM
3124 ret = os_snprintf(pos, end - pos, "pmf=%d\n"
3125 "mgmt_group_cipher=%s\n",
13e1d2e2 3126 (rsn.capabilities &
3a5954ef
JM
3127 WPA_CAPABILITY_MFPR) ? 2 : 1,
3128 wpa_cipher_txt(
3129 sm->mgmt_group_cipher));
d85e1fc8 3130 if (os_snprintf_error(end - pos, ret))
13e1d2e2
JM
3131 return pos - buf;
3132 pos += ret;
3133 }
3134 }
3135
6fc6879b
JM
3136 return pos - buf;
3137}
3138
3139
ae8535b6
JM
3140int wpa_sm_pmf_enabled(struct wpa_sm *sm)
3141{
3142 struct wpa_ie_data rsn;
3143
3144 if (sm->mfp == NO_MGMT_FRAME_PROTECTION || !sm->ap_rsn_ie)
3145 return 0;
3146
3147 if (wpa_parse_wpa_ie_rsn(sm->ap_rsn_ie, sm->ap_rsn_ie_len, &rsn) >= 0 &&
3148 rsn.capabilities & (WPA_CAPABILITY_MFPR | WPA_CAPABILITY_MFPC))
3149 return 1;
3150
3151 return 0;
3152}
3153
3154
17c2559c
MV
3155int wpa_sm_ocv_enabled(struct wpa_sm *sm)
3156{
3157 struct wpa_ie_data rsn;
3158
3159 if (!sm->ocv || !sm->ap_rsn_ie)
3160 return 0;
3161
3162 return wpa_parse_wpa_ie_rsn(sm->ap_rsn_ie, sm->ap_rsn_ie_len,
3163 &rsn) >= 0 &&
3164 (rsn.capabilities & WPA_CAPABILITY_OCVC);
3165}
3166
3167
6fc6879b
JM
3168/**
3169 * wpa_sm_set_assoc_wpa_ie_default - Generate own WPA/RSN IE from configuration
3170 * @sm: Pointer to WPA state machine data from wpa_sm_init()
3171 * @wpa_ie: Pointer to buffer for WPA/RSN IE
3172 * @wpa_ie_len: Pointer to the length of the wpa_ie buffer
3173 * Returns: 0 on success, -1 on failure
3174 */
3175int wpa_sm_set_assoc_wpa_ie_default(struct wpa_sm *sm, u8 *wpa_ie,
3176 size_t *wpa_ie_len)
3177{
3178 int res;
3179
3180 if (sm == NULL)
3181 return -1;
3182
651c6a84
JM
3183#ifdef CONFIG_TESTING_OPTIONS
3184 if (sm->test_assoc_ie) {
3185 wpa_printf(MSG_DEBUG,
3186 "TESTING: Replace association WPA/RSN IE");
3187 if (*wpa_ie_len < wpabuf_len(sm->test_assoc_ie))
3188 return -1;
3189 os_memcpy(wpa_ie, wpabuf_head(sm->test_assoc_ie),
3190 wpabuf_len(sm->test_assoc_ie));
3191 res = wpabuf_len(sm->test_assoc_ie);
3192 } else
3193#endif /* CONFIG_TESTING_OPTIONS */
6fc6879b
JM
3194 res = wpa_gen_wpa_ie(sm, wpa_ie, *wpa_ie_len);
3195 if (res < 0)
3196 return -1;
3197 *wpa_ie_len = res;
3198
3199 wpa_hexdump(MSG_DEBUG, "WPA: Set own WPA IE default",
3200 wpa_ie, *wpa_ie_len);
3201
3202 if (sm->assoc_wpa_ie == NULL) {
3203 /*
3204 * Make a copy of the WPA/RSN IE so that 4-Way Handshake gets
3205 * the correct version of the IE even if PMKSA caching is
3206 * aborted (which would remove PMKID from IE generation).
3207 */
a1f11e34 3208 sm->assoc_wpa_ie = os_memdup(wpa_ie, *wpa_ie_len);
6fc6879b
JM
3209 if (sm->assoc_wpa_ie == NULL)
3210 return -1;
3211
6fc6879b 3212 sm->assoc_wpa_ie_len = *wpa_ie_len;
0866ed00
JM
3213 } else {
3214 wpa_hexdump(MSG_DEBUG,
3215 "WPA: Leave previously set WPA IE default",
3216 sm->assoc_wpa_ie, sm->assoc_wpa_ie_len);
6fc6879b
JM
3217 }
3218
3219 return 0;
3220}
3221
3222
3223/**
3224 * wpa_sm_set_assoc_wpa_ie - Set own WPA/RSN IE from (Re)AssocReq
3225 * @sm: Pointer to WPA state machine data from wpa_sm_init()
3226 * @ie: Pointer to IE data (starting from id)
3227 * @len: IE length
3228 * Returns: 0 on success, -1 on failure
3229 *
3230 * Inform WPA state machine about the WPA/RSN IE used in (Re)Association
3231 * Request frame. The IE will be used to override the default value generated
3232 * with wpa_sm_set_assoc_wpa_ie_default().
3233 */
3234int wpa_sm_set_assoc_wpa_ie(struct wpa_sm *sm, const u8 *ie, size_t len)
3235{
3236 if (sm == NULL)
3237 return -1;
3238
3239 os_free(sm->assoc_wpa_ie);
3240 if (ie == NULL || len == 0) {
f049052b
BG
3241 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
3242 "WPA: clearing own WPA/RSN IE");
6fc6879b
JM
3243 sm->assoc_wpa_ie = NULL;
3244 sm->assoc_wpa_ie_len = 0;
3245 } else {
3246 wpa_hexdump(MSG_DEBUG, "WPA: set own WPA/RSN IE", ie, len);
a1f11e34 3247 sm->assoc_wpa_ie = os_memdup(ie, len);
6fc6879b
JM
3248 if (sm->assoc_wpa_ie == NULL)
3249 return -1;
3250
6fc6879b
JM
3251 sm->assoc_wpa_ie_len = len;
3252 }
3253
3254 return 0;
3255}
3256
3257
6d6c8877
JM
3258/**
3259 * wpa_sm_set_assoc_rsnxe_default - Generate own RSNXE from configuration
3260 * @sm: Pointer to WPA state machine data from wpa_sm_init()
3261 * @rsnxe: Pointer to buffer for RSNXE
3262 * @rsnxe_len: Pointer to the length of the rsne buffer
3263 * Returns: 0 on success, -1 on failure
3264 */
3265int wpa_sm_set_assoc_rsnxe_default(struct wpa_sm *sm, u8 *rsnxe,
3266 size_t *rsnxe_len)
3267{
3268 int res;
3269
3270 if (!sm)
3271 return -1;
3272
3273 res = wpa_gen_rsnxe(sm, rsnxe, *rsnxe_len);
3274 if (res < 0)
3275 return -1;
3276 *rsnxe_len = res;
3277
3278 wpa_hexdump(MSG_DEBUG, "RSN: Set own RSNXE default", rsnxe, *rsnxe_len);
3279
3280 if (sm->assoc_rsnxe) {
3281 wpa_hexdump(MSG_DEBUG,
3282 "RSN: Leave previously set RSNXE default",
3283 sm->assoc_rsnxe, sm->assoc_rsnxe_len);
3284 } else if (*rsnxe_len > 0) {
3285 /*
3286 * Make a copy of the RSNXE so that 4-Way Handshake gets the
3287 * correct version of the IE even if it gets changed.
3288 */
3289 sm->assoc_rsnxe = os_memdup(rsnxe, *rsnxe_len);
3290 if (!sm->assoc_rsnxe)
3291 return -1;
3292
3293 sm->assoc_rsnxe_len = *rsnxe_len;
3294 }
3295
3296 return 0;
3297}
3298
3299
3300/**
3301 * wpa_sm_set_assoc_rsnxe - Set own RSNXE from (Re)AssocReq
3302 * @sm: Pointer to WPA state machine data from wpa_sm_init()
3303 * @ie: Pointer to IE data (starting from id)
3304 * @len: IE length
3305 * Returns: 0 on success, -1 on failure
3306 *
3307 * Inform WPA state machine about the RSNXE used in (Re)Association Request
3308 * frame. The IE will be used to override the default value generated
3309 * with wpa_sm_set_assoc_rsnxe_default().
3310 */
3311int wpa_sm_set_assoc_rsnxe(struct wpa_sm *sm, const u8 *ie, size_t len)
3312{
3313 if (!sm)
3314 return -1;
3315
3316 os_free(sm->assoc_rsnxe);
3317 if (!ie || len == 0) {
3318 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
3319 "RSN: clearing own RSNXE");
3320 sm->assoc_rsnxe = NULL;
3321 sm->assoc_rsnxe_len = 0;
3322 } else {
3323 wpa_hexdump(MSG_DEBUG, "RSN: set own RSNXE", ie, len);
3324 sm->assoc_rsnxe = os_memdup(ie, len);
3325 if (!sm->assoc_rsnxe)
3326 return -1;
3327
3328 sm->assoc_rsnxe_len = len;
3329 }
3330
3331 return 0;
3332}
3333
3334
6fc6879b
JM
3335/**
3336 * wpa_sm_set_ap_wpa_ie - Set AP WPA IE from Beacon/ProbeResp
3337 * @sm: Pointer to WPA state machine data from wpa_sm_init()
3338 * @ie: Pointer to IE data (starting from id)
3339 * @len: IE length
3340 * Returns: 0 on success, -1 on failure
3341 *
3342 * Inform WPA state machine about the WPA IE used in Beacon / Probe Response
3343 * frame.
3344 */
3345int wpa_sm_set_ap_wpa_ie(struct wpa_sm *sm, const u8 *ie, size_t len)
3346{
3347 if (sm == NULL)
3348 return -1;
3349
3350 os_free(sm->ap_wpa_ie);
3351 if (ie == NULL || len == 0) {
f049052b
BG
3352 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
3353 "WPA: clearing AP WPA IE");
6fc6879b
JM
3354 sm->ap_wpa_ie = NULL;
3355 sm->ap_wpa_ie_len = 0;
3356 } else {
3357 wpa_hexdump(MSG_DEBUG, "WPA: set AP WPA IE", ie, len);
a1f11e34 3358 sm->ap_wpa_ie = os_memdup(ie, len);
6fc6879b
JM
3359 if (sm->ap_wpa_ie == NULL)
3360 return -1;
3361
6fc6879b
JM
3362 sm->ap_wpa_ie_len = len;
3363 }
3364
3365 return 0;
3366}
3367
3368
3369/**
3370 * wpa_sm_set_ap_rsn_ie - Set AP RSN IE from Beacon/ProbeResp
3371 * @sm: Pointer to WPA state machine data from wpa_sm_init()
3372 * @ie: Pointer to IE data (starting from id)
3373 * @len: IE length
3374 * Returns: 0 on success, -1 on failure
3375 *
3376 * Inform WPA state machine about the RSN IE used in Beacon / Probe Response
3377 * frame.
3378 */
3379int wpa_sm_set_ap_rsn_ie(struct wpa_sm *sm, const u8 *ie, size_t len)
3380{
3381 if (sm == NULL)
3382 return -1;
3383
3384 os_free(sm->ap_rsn_ie);
3385 if (ie == NULL || len == 0) {
f049052b
BG
3386 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
3387 "WPA: clearing AP RSN IE");
6fc6879b
JM
3388 sm->ap_rsn_ie = NULL;
3389 sm->ap_rsn_ie_len = 0;
3390 } else {
3391 wpa_hexdump(MSG_DEBUG, "WPA: set AP RSN IE", ie, len);
a1f11e34 3392 sm->ap_rsn_ie = os_memdup(ie, len);
6fc6879b
JM
3393 if (sm->ap_rsn_ie == NULL)
3394 return -1;
3395
6fc6879b
JM
3396 sm->ap_rsn_ie_len = len;
3397 }
3398
3399 return 0;
3400}
3401
3402
146889e3
JM
3403/**
3404 * wpa_sm_set_ap_rsnxe - Set AP RSNXE from Beacon/ProbeResp
3405 * @sm: Pointer to WPA state machine data from wpa_sm_init()
3406 * @ie: Pointer to IE data (starting from id)
3407 * @len: IE length
3408 * Returns: 0 on success, -1 on failure
3409 *
3410 * Inform WPA state machine about the RSNXE used in Beacon / Probe Response
3411 * frame.
3412 */
3413int wpa_sm_set_ap_rsnxe(struct wpa_sm *sm, const u8 *ie, size_t len)
3414{
3415 if (!sm)
3416 return -1;
3417
3418 os_free(sm->ap_rsnxe);
3419 if (!ie || len == 0) {
3420 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: clearing AP RSNXE");
3421 sm->ap_rsnxe = NULL;
3422 sm->ap_rsnxe_len = 0;
3423 } else {
3424 wpa_hexdump(MSG_DEBUG, "WPA: set AP RSNXE", ie, len);
3425 sm->ap_rsnxe = os_memdup(ie, len);
3426 if (!sm->ap_rsnxe)
3427 return -1;
3428
3429 sm->ap_rsnxe_len = len;
3430 }
3431
3432 return 0;
3433}
3434
3435
6fc6879b
JM
3436/**
3437 * wpa_sm_parse_own_wpa_ie - Parse own WPA/RSN IE
3438 * @sm: Pointer to WPA state machine data from wpa_sm_init()
3439 * @data: Pointer to data area for parsing results
3440 * Returns: 0 on success, -1 if IE is not known, or -2 on parsing failure
3441 *
3442 * Parse the contents of the own WPA or RSN IE from (Re)AssocReq and write the
3443 * parsed data into data.
3444 */
3445int wpa_sm_parse_own_wpa_ie(struct wpa_sm *sm, struct wpa_ie_data *data)
3446{
2a522e71
JM
3447 if (sm == NULL)
3448 return -1;
3449
3450 if (sm->assoc_wpa_ie == NULL) {
f049052b
BG
3451 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
3452 "WPA: No WPA/RSN IE available from association info");
6fc6879b
JM
3453 return -1;
3454 }
3455 if (wpa_parse_wpa_ie(sm->assoc_wpa_ie, sm->assoc_wpa_ie_len, data))
3456 return -2;
3457 return 0;
3458}
540264a7
JM
3459
3460
3461int wpa_sm_pmksa_cache_list(struct wpa_sm *sm, char *buf, size_t len)
3462{
540264a7 3463 return pmksa_cache_list(sm->pmksa, buf, len);
540264a7 3464}
32d5295f
JM
3465
3466
3459381d
JM
3467struct rsn_pmksa_cache_entry * wpa_sm_pmksa_cache_head(struct wpa_sm *sm)
3468{
3469 return pmksa_cache_head(sm->pmksa);
3470}
3471
3472
3473struct rsn_pmksa_cache_entry *
3474wpa_sm_pmksa_cache_add_entry(struct wpa_sm *sm,
3475 struct rsn_pmksa_cache_entry * entry)
3476{
3477 return pmksa_cache_add_entry(sm->pmksa, entry);
3478}
3479
3480
f705f41b
VK
3481void wpa_sm_pmksa_cache_add(struct wpa_sm *sm, const u8 *pmk, size_t pmk_len,
3482 const u8 *pmkid, const u8 *bssid,
3483 const u8 *fils_cache_id)
3484{
3485 sm->cur_pmksa = pmksa_cache_add(sm->pmksa, pmk, pmk_len, pmkid, NULL, 0,
3486 bssid, sm->own_addr, sm->network_ctx,
3487 sm->key_mgmt, fils_cache_id);
3488}
3489
3490
567da5bb
JM
3491int wpa_sm_pmksa_exists(struct wpa_sm *sm, const u8 *bssid,
3492 const void *network_ctx)
3493{
852b2f27 3494 return pmksa_cache_get(sm->pmksa, bssid, NULL, network_ctx, 0) != NULL;
567da5bb
JM
3495}
3496
3497
32d5295f
JM
3498void wpa_sm_drop_sa(struct wpa_sm *sm)
3499{
f049052b 3500 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Clear old PMK and PTK");
32d5295f
JM
3501 sm->ptk_set = 0;
3502 sm->tptk_set = 0;
1bd13110 3503 sm->pmk_len = 0;
32d5295f
JM
3504 os_memset(sm->pmk, 0, sizeof(sm->pmk));
3505 os_memset(&sm->ptk, 0, sizeof(sm->ptk));
3506 os_memset(&sm->tptk, 0, sizeof(sm->tptk));
cb5132bb 3507 os_memset(&sm->gtk, 0, sizeof(sm->gtk));
87e2db16 3508 os_memset(&sm->gtk_wnm_sleep, 0, sizeof(sm->gtk_wnm_sleep));
cb5132bb 3509 os_memset(&sm->igtk, 0, sizeof(sm->igtk));
87e2db16 3510 os_memset(&sm->igtk_wnm_sleep, 0, sizeof(sm->igtk_wnm_sleep));
71932544
JM
3511#ifdef CONFIG_IEEE80211R
3512 os_memset(sm->xxkey, 0, sizeof(sm->xxkey));
a3e18dbb 3513 sm->xxkey_len = 0;
71932544 3514 os_memset(sm->pmk_r0, 0, sizeof(sm->pmk_r0));
a3e18dbb 3515 sm->pmk_r0_len = 0;
71932544 3516 os_memset(sm->pmk_r1, 0, sizeof(sm->pmk_r1));
a3e18dbb 3517 sm->pmk_r1_len = 0;
71932544 3518#endif /* CONFIG_IEEE80211R */
32d5295f 3519}
0d7b4409
JM
3520
3521
3522int wpa_sm_has_ptk(struct wpa_sm *sm)
3523{
3524 if (sm == NULL)
3525 return 0;
3526 return sm->ptk_set;
3527}
b14a210c
JB
3528
3529
a79ed068
MT
3530int wpa_sm_has_ptk_installed(struct wpa_sm *sm)
3531{
3532 if (!sm)
3533 return 0;
3534 return sm->ptk.installed;
3535}
3536
3537
b14a210c
JB
3538void wpa_sm_update_replay_ctr(struct wpa_sm *sm, const u8 *replay_ctr)
3539{
3540 os_memcpy(sm->rx_replay_counter, replay_ctr, WPA_REPLAY_COUNTER_LEN);
3541}
d8a790b9
JM
3542
3543
3544void wpa_sm_pmksa_cache_flush(struct wpa_sm *sm, void *network_ctx)
3545{
4033935d 3546 pmksa_cache_flush(sm->pmksa, network_ctx, NULL, 0);
d8a790b9 3547}
75cad1a0
XC
3548
3549
ad3872a3 3550#ifdef CONFIG_WNM
75cad1a0
XC
3551int wpa_wnmsleep_install_key(struct wpa_sm *sm, u8 subelem_id, u8 *buf)
3552{
75cad1a0
XC
3553 u16 keyinfo;
3554 u8 keylen; /* plaintext key len */
75cad1a0
XC
3555 u8 *key_rsc;
3556
75cad1a0 3557 if (subelem_id == WNM_SLEEP_SUBELEM_GTK) {
d2c33b91
JM
3558 struct wpa_gtk_data gd;
3559
3560 os_memset(&gd, 0, sizeof(gd));
3561 keylen = wpa_cipher_key_len(sm->group_cipher);
3562 gd.key_rsc_len = wpa_cipher_rsc_len(sm->group_cipher);
3563 gd.alg = wpa_cipher_to_alg(sm->group_cipher);
3564 if (gd.alg == WPA_ALG_NONE) {
3565 wpa_printf(MSG_DEBUG, "Unsupported group cipher suite");
3566 return -1;
3567 }
3568
75cad1a0 3569 key_rsc = buf + 5;
68db9ab0 3570 keyinfo = WPA_GET_LE16(buf + 2);
75cad1a0
XC
3571 gd.gtk_len = keylen;
3572 if (gd.gtk_len != buf[4]) {
3573 wpa_printf(MSG_DEBUG, "GTK len mismatch len %d vs %d",
3574 gd.gtk_len, buf[4]);
3575 return -1;
3576 }
3577 gd.keyidx = keyinfo & 0x03; /* B0 - B1 */
3578 gd.tx = wpa_supplicant_gtk_tx_bit_workaround(
3579 sm, !!(keyinfo & WPA_KEY_INFO_TXRX));
3580
68db9ab0 3581 os_memcpy(gd.gtk, buf + 13, gd.gtk_len);
75cad1a0
XC
3582
3583 wpa_hexdump_key(MSG_DEBUG, "Install GTK (WNM SLEEP)",
3584 gd.gtk, gd.gtk_len);
87e2db16 3585 if (wpa_supplicant_install_gtk(sm, &gd, key_rsc, 1)) {
31bc66e4 3586 forced_memzero(&gd, sizeof(gd));
75cad1a0
XC
3587 wpa_printf(MSG_DEBUG, "Failed to install the GTK in "
3588 "WNM mode");
3589 return -1;
3590 }
31bc66e4 3591 forced_memzero(&gd, sizeof(gd));
75cad1a0 3592 } else if (subelem_id == WNM_SLEEP_SUBELEM_IGTK) {
cb5132bb
MV
3593 const struct wpa_igtk_kde *igtk;
3594
3595 igtk = (const struct wpa_igtk_kde *) (buf + 2);
87e2db16 3596 if (wpa_supplicant_install_igtk(sm, igtk, 1) < 0)
75cad1a0 3597 return -1;
75cad1a0
XC
3598 } else {
3599 wpa_printf(MSG_DEBUG, "Unknown element id");
3600 return -1;
3601 }
3602
3603 return 0;
3604}
ad3872a3 3605#endif /* CONFIG_WNM */
db76aa64
JM
3606
3607
25ef8529
JM
3608#ifdef CONFIG_P2P
3609
3610int wpa_sm_get_p2p_ip_addr(struct wpa_sm *sm, u8 *buf)
3611{
3612 if (sm == NULL || WPA_GET_BE32(sm->p2p_ip_addr) == 0)
3613 return -1;
3614 os_memcpy(buf, sm->p2p_ip_addr, 3 * 4);
3615 return 0;
3616}
3617
3618#endif /* CONFIG_P2P */
b41f2684
CL
3619
3620
3621void wpa_sm_set_rx_replay_ctr(struct wpa_sm *sm, const u8 *rx_replay_counter)
3622{
3623 if (rx_replay_counter == NULL)
3624 return;
3625
3626 os_memcpy(sm->rx_replay_counter, rx_replay_counter,
3627 WPA_REPLAY_COUNTER_LEN);
3628 sm->rx_replay_counter_set = 1;
3629 wpa_printf(MSG_DEBUG, "Updated key replay counter");
3630}
3631
3632
98cd3d1c
JM
3633void wpa_sm_set_ptk_kck_kek(struct wpa_sm *sm,
3634 const u8 *ptk_kck, size_t ptk_kck_len,
3635 const u8 *ptk_kek, size_t ptk_kek_len)
b41f2684 3636{
98cd3d1c
JM
3637 if (ptk_kck && ptk_kck_len <= WPA_KCK_MAX_LEN) {
3638 os_memcpy(sm->ptk.kck, ptk_kck, ptk_kck_len);
3639 sm->ptk.kck_len = ptk_kck_len;
b41f2684
CL
3640 wpa_printf(MSG_DEBUG, "Updated PTK KCK");
3641 }
98cd3d1c
JM
3642 if (ptk_kek && ptk_kek_len <= WPA_KEK_MAX_LEN) {
3643 os_memcpy(sm->ptk.kek, ptk_kek, ptk_kek_len);
3644 sm->ptk.kek_len = ptk_kek_len;
b41f2684
CL
3645 wpa_printf(MSG_DEBUG, "Updated PTK KEK");
3646 }
3647 sm->ptk_set = 1;
3648}
651c6a84
JM
3649
3650
3651#ifdef CONFIG_TESTING_OPTIONS
bb06748f 3652
651c6a84
JM
3653void wpa_sm_set_test_assoc_ie(struct wpa_sm *sm, struct wpabuf *buf)
3654{
3655 wpabuf_free(sm->test_assoc_ie);
3656 sm->test_assoc_ie = buf;
3657}
bb06748f
JM
3658
3659
3660const u8 * wpa_sm_get_anonce(struct wpa_sm *sm)
3661{
3662 return sm->anonce;
3663}
3664
651c6a84 3665#endif /* CONFIG_TESTING_OPTIONS */
f00b9b88
JM
3666
3667
3dc3afe2
AM
3668unsigned int wpa_sm_get_key_mgmt(struct wpa_sm *sm)
3669{
3670 return sm->key_mgmt;
3671}
3672
3673
f00b9b88
JM
3674#ifdef CONFIG_FILS
3675
af3e362f 3676struct wpabuf * fils_build_auth(struct wpa_sm *sm, int dh_group, const u8 *md)
f00b9b88
JM
3677{
3678 struct wpabuf *buf = NULL;
3679 struct wpabuf *erp_msg;
76e20f4f 3680 struct wpabuf *pub = NULL;
f00b9b88
JM
3681
3682 erp_msg = eapol_sm_build_erp_reauth_start(sm->eapol);
3683 if (!erp_msg && !sm->cur_pmksa) {
3684 wpa_printf(MSG_DEBUG,
3685 "FILS: Neither ERP EAP-Initiate/Re-auth nor PMKSA cache entry is available - skip FILS");
3686 goto fail;
3687 }
3688
3689 wpa_printf(MSG_DEBUG, "FILS: Try to use FILS (erp=%d pmksa_cache=%d)",
3690 erp_msg != NULL, sm->cur_pmksa != NULL);
3691
706df429
JM
3692 sm->fils_completed = 0;
3693
f00b9b88
JM
3694 if (!sm->assoc_wpa_ie) {
3695 wpa_printf(MSG_INFO, "FILS: No own RSN IE set for FILS");
3696 goto fail;
3697 }
3698
3699 if (random_get_bytes(sm->fils_nonce, FILS_NONCE_LEN) < 0 ||
3700 random_get_bytes(sm->fils_session, FILS_SESSION_LEN) < 0)
3701 goto fail;
3702
3703 wpa_hexdump(MSG_DEBUG, "FILS: Generated FILS Nonce",
3704 sm->fils_nonce, FILS_NONCE_LEN);
3705 wpa_hexdump(MSG_DEBUG, "FILS: Generated FILS Session",
3706 sm->fils_session, FILS_SESSION_LEN);
3707
76e20f4f
JM
3708#ifdef CONFIG_FILS_SK_PFS
3709 sm->fils_dh_group = dh_group;
3710 if (dh_group) {
3711 crypto_ecdh_deinit(sm->fils_ecdh);
3712 sm->fils_ecdh = crypto_ecdh_init(dh_group);
3713 if (!sm->fils_ecdh) {
3714 wpa_printf(MSG_INFO,
3715 "FILS: Could not initialize ECDH with group %d",
3716 dh_group);
3717 goto fail;
3718 }
3719 pub = crypto_ecdh_get_pubkey(sm->fils_ecdh, 1);
3720 if (!pub)
3721 goto fail;
3722 wpa_hexdump_buf(MSG_DEBUG, "FILS: Element (DH public key)",
3723 pub);
3724 sm->fils_dh_elem_len = wpabuf_len(pub);
3725 }
3726#endif /* CONFIG_FILS_SK_PFS */
3727
3728 buf = wpabuf_alloc(1000 + sm->assoc_wpa_ie_len +
3729 (pub ? wpabuf_len(pub) : 0));
f00b9b88
JM
3730 if (!buf)
3731 goto fail;
3732
3733 /* Fields following the Authentication algorithm number field */
3734
3735 /* Authentication Transaction seq# */
3736 wpabuf_put_le16(buf, 1);
3737
3738 /* Status Code */
3739 wpabuf_put_le16(buf, WLAN_STATUS_SUCCESS);
3740
76e20f4f
JM
3741 /* TODO: FILS PK */
3742#ifdef CONFIG_FILS_SK_PFS
3743 if (dh_group) {
3744 /* Finite Cyclic Group */
3745 wpabuf_put_le16(buf, dh_group);
3746 /* Element */
3747 wpabuf_put_buf(buf, pub);
3748 }
3749#endif /* CONFIG_FILS_SK_PFS */
f00b9b88
JM
3750
3751 /* RSNE */
3752 wpa_hexdump(MSG_DEBUG, "FILS: RSNE in FILS Authentication frame",
3753 sm->assoc_wpa_ie, sm->assoc_wpa_ie_len);
3754 wpabuf_put_data(buf, sm->assoc_wpa_ie, sm->assoc_wpa_ie_len);
3755
af3e362f
JM
3756 if (md) {
3757 /* MDE when using FILS for FT initial association */
3758 struct rsn_mdie *mdie;
3759
3760 wpabuf_put_u8(buf, WLAN_EID_MOBILITY_DOMAIN);
3761 wpabuf_put_u8(buf, sizeof(*mdie));
3762 mdie = wpabuf_put(buf, sizeof(*mdie));
3763 os_memcpy(mdie->mobility_domain, md, MOBILITY_DOMAIN_ID_LEN);
3764 mdie->ft_capab = 0;
3765 }
f00b9b88
JM
3766
3767 /* FILS Nonce */
3768 wpabuf_put_u8(buf, WLAN_EID_EXTENSION); /* Element ID */
3769 wpabuf_put_u8(buf, 1 + FILS_NONCE_LEN); /* Length */
3770 /* Element ID Extension */
3771 wpabuf_put_u8(buf, WLAN_EID_EXT_FILS_NONCE);
3772 wpabuf_put_data(buf, sm->fils_nonce, FILS_NONCE_LEN);
3773
3774 /* FILS Session */
3775 wpabuf_put_u8(buf, WLAN_EID_EXTENSION); /* Element ID */
3776 wpabuf_put_u8(buf, 1 + FILS_SESSION_LEN); /* Length */
3777 /* Element ID Extension */
3778 wpabuf_put_u8(buf, WLAN_EID_EXT_FILS_SESSION);
3779 wpabuf_put_data(buf, sm->fils_session, FILS_SESSION_LEN);
3780
3781 /* FILS Wrapped Data */
fcd3d6ce 3782 sm->fils_erp_pmkid_set = 0;
f00b9b88
JM
3783 if (erp_msg) {
3784 wpabuf_put_u8(buf, WLAN_EID_EXTENSION); /* Element ID */
3785 wpabuf_put_u8(buf, 1 + wpabuf_len(erp_msg)); /* Length */
3786 /* Element ID Extension */
3787 wpabuf_put_u8(buf, WLAN_EID_EXT_FILS_WRAPPED_DATA);
3788 wpabuf_put_buf(buf, erp_msg);
fcd3d6ce
JM
3789 /* Calculate pending PMKID here so that we do not need to
3790 * maintain a copy of the EAP-Initiate/Reauth message. */
3791 if (fils_pmkid_erp(sm->key_mgmt, wpabuf_head(erp_msg),
3792 wpabuf_len(erp_msg),
3793 sm->fils_erp_pmkid) == 0)
3794 sm->fils_erp_pmkid_set = 1;
f00b9b88
JM
3795 }
3796
3797 wpa_hexdump_buf(MSG_DEBUG, "RSN: FILS fields for Authentication frame",
3798 buf);
3799
3800fail:
3801 wpabuf_free(erp_msg);
76e20f4f 3802 wpabuf_free(pub);
f00b9b88
JM
3803 return buf;
3804}
3805
a6609937 3806
ba9774bd
JM
3807int fils_process_auth(struct wpa_sm *sm, const u8 *bssid, const u8 *data,
3808 size_t len)
a6609937
JM
3809{
3810 const u8 *pos, *end;
3811 struct ieee802_11_elems elems;
3812 struct wpa_ie_data rsn;
3813 int pmkid_match = 0;
3814 u8 ick[FILS_ICK_MAX_LEN];
3815 size_t ick_len;
3816 int res;
76e20f4f 3817 struct wpabuf *dh_ss = NULL;
e6b62313
JM
3818 const u8 *g_sta = NULL;
3819 size_t g_sta_len = 0;
3820 const u8 *g_ap = NULL;
3821 size_t g_ap_len = 0;
3822 struct wpabuf *pub = NULL;
a6609937 3823
ba9774bd
JM
3824 os_memcpy(sm->bssid, bssid, ETH_ALEN);
3825
a6609937
JM
3826 wpa_hexdump(MSG_DEBUG, "FILS: Authentication frame fields",
3827 data, len);
3828 pos = data;
3829 end = data + len;
3830
76e20f4f
JM
3831 /* TODO: FILS PK */
3832#ifdef CONFIG_FILS_SK_PFS
3833 if (sm->fils_dh_group) {
3834 u16 group;
3835
3836 /* Using FILS PFS */
3837
3838 /* Finite Cyclic Group */
3839 if (end - pos < 2) {
3840 wpa_printf(MSG_DEBUG,
3841 "FILS: No room for Finite Cyclic Group");
3842 goto fail;
3843 }
3844 group = WPA_GET_LE16(pos);
3845 pos += 2;
3846 if (group != sm->fils_dh_group) {
3847 wpa_printf(MSG_DEBUG,
3848 "FILS: Unexpected change in Finite Cyclic Group: %u (expected %u)",
3849 group, sm->fils_dh_group);
3850 goto fail;
3851 }
3852
3853 /* Element */
3854 if ((size_t) (end - pos) < sm->fils_dh_elem_len) {
3855 wpa_printf(MSG_DEBUG, "FILS: No room for Element");
3856 goto fail;
3857 }
3858
3859 if (!sm->fils_ecdh) {
3860 wpa_printf(MSG_DEBUG, "FILS: No ECDH state available");
3861 goto fail;
3862 }
3863 dh_ss = crypto_ecdh_set_peerkey(sm->fils_ecdh, 1, pos,
3864 sm->fils_dh_elem_len);
3865 if (!dh_ss) {
3866 wpa_printf(MSG_DEBUG, "FILS: ECDH operation failed");
3867 goto fail;
3868 }
3869 wpa_hexdump_buf_key(MSG_DEBUG, "FILS: DH_SS", dh_ss);
e6b62313
JM
3870 g_ap = pos;
3871 g_ap_len = sm->fils_dh_elem_len;
76e20f4f
JM
3872 pos += sm->fils_dh_elem_len;
3873 }
3874#endif /* CONFIG_FILS_SK_PFS */
a6609937
JM
3875
3876 wpa_hexdump(MSG_DEBUG, "FILS: Remaining IEs", pos, end - pos);
3877 if (ieee802_11_parse_elems(pos, end - pos, &elems, 1) == ParseFailed) {
3878 wpa_printf(MSG_DEBUG, "FILS: Could not parse elements");
76e20f4f 3879 goto fail;
a6609937
JM
3880 }
3881
3882 /* RSNE */
3883 wpa_hexdump(MSG_DEBUG, "FILS: RSN element", elems.rsn_ie,
3884 elems.rsn_ie_len);
3885 if (!elems.rsn_ie ||
3886 wpa_parse_wpa_ie_rsn(elems.rsn_ie - 2, elems.rsn_ie_len + 2,
3887 &rsn) < 0) {
3888 wpa_printf(MSG_DEBUG, "FILS: No RSN element");
76e20f4f 3889 goto fail;
a6609937
JM
3890 }
3891
3892 if (!elems.fils_nonce) {
3893 wpa_printf(MSG_DEBUG, "FILS: No FILS Nonce field");
76e20f4f 3894 goto fail;
a6609937
JM
3895 }
3896 os_memcpy(sm->fils_anonce, elems.fils_nonce, FILS_NONCE_LEN);
3897 wpa_hexdump(MSG_DEBUG, "FILS: ANonce", sm->fils_anonce, FILS_NONCE_LEN);
3898
d555c39c 3899#ifdef CONFIG_IEEE80211R
7d440a3b
JM
3900 if (wpa_key_mgmt_ft(sm->key_mgmt)) {
3901 struct wpa_ft_ies parse;
3902
3903 if (!elems.mdie || !elems.ftie) {
3904 wpa_printf(MSG_DEBUG, "FILS+FT: No MDE or FTE");
3905 goto fail;
3906 }
3907
9a33737a
JM
3908 if (wpa_ft_parse_ies(pos, end - pos, &parse,
3909 wpa_key_mgmt_sha384(sm->key_mgmt)) < 0) {
7d440a3b
JM
3910 wpa_printf(MSG_DEBUG, "FILS+FT: Failed to parse IEs");
3911 goto fail;
3912 }
3913
3914 if (!parse.r0kh_id) {
3915 wpa_printf(MSG_DEBUG,
3916 "FILS+FT: No R0KH-ID subelem in FTE");
3917 goto fail;
3918 }
3919 os_memcpy(sm->r0kh_id, parse.r0kh_id, parse.r0kh_id_len);
3920 sm->r0kh_id_len = parse.r0kh_id_len;
3921 wpa_hexdump_ascii(MSG_DEBUG, "FILS+FT: R0KH-ID",
3922 sm->r0kh_id, sm->r0kh_id_len);
3923
3924 if (!parse.r1kh_id) {
3925 wpa_printf(MSG_DEBUG,
3926 "FILS+FT: No R1KH-ID subelem in FTE");
3927 goto fail;
3928 }
3929 os_memcpy(sm->r1kh_id, parse.r1kh_id, FT_R1KH_ID_LEN);
3930 wpa_hexdump(MSG_DEBUG, "FILS+FT: R1KH-ID",
3931 sm->r1kh_id, FT_R1KH_ID_LEN);
3932
3933 /* TODO: Check MDE and FTE payload */
3934
3935 wpabuf_free(sm->fils_ft_ies);
3936 sm->fils_ft_ies = wpabuf_alloc(2 + elems.mdie_len +
3937 2 + elems.ftie_len);
3938 if (!sm->fils_ft_ies)
3939 goto fail;
3940 wpabuf_put_data(sm->fils_ft_ies, elems.mdie - 2,
3941 2 + elems.mdie_len);
3942 wpabuf_put_data(sm->fils_ft_ies, elems.ftie - 2,
3943 2 + elems.ftie_len);
3944 } else {
3945 wpabuf_free(sm->fils_ft_ies);
3946 sm->fils_ft_ies = NULL;
3947 }
d555c39c 3948#endif /* CONFIG_IEEE80211R */
a6609937
JM
3949
3950 /* PMKID List */
3951 if (rsn.pmkid && rsn.num_pmkid > 0) {
3952 wpa_hexdump(MSG_DEBUG, "FILS: PMKID List",
3953 rsn.pmkid, rsn.num_pmkid * PMKID_LEN);
3954
3955 if (rsn.num_pmkid != 1) {
3956 wpa_printf(MSG_DEBUG, "FILS: Invalid PMKID selection");
76e20f4f 3957 goto fail;
a6609937
JM
3958 }
3959 wpa_hexdump(MSG_DEBUG, "FILS: PMKID", rsn.pmkid, PMKID_LEN);
3960 if (os_memcmp(sm->cur_pmksa->pmkid, rsn.pmkid, PMKID_LEN) != 0)
3961 {
3962 wpa_printf(MSG_DEBUG, "FILS: PMKID mismatch");
3963 wpa_hexdump(MSG_DEBUG, "FILS: Expected PMKID",
3964 sm->cur_pmksa->pmkid, PMKID_LEN);
76e20f4f 3965 goto fail;
a6609937
JM
3966 }
3967 wpa_printf(MSG_DEBUG,
3968 "FILS: Matching PMKID - continue using PMKSA caching");
3969 pmkid_match = 1;
3970 }
3971 if (!pmkid_match && sm->cur_pmksa) {
3972 wpa_printf(MSG_DEBUG,
3973 "FILS: No PMKID match - cannot use cached PMKSA entry");
3974 sm->cur_pmksa = NULL;
3975 }
3976
3977 /* FILS Session */
3978 if (!elems.fils_session) {
3979 wpa_printf(MSG_DEBUG, "FILS: No FILS Session element");
76e20f4f 3980 goto fail;
a6609937
JM
3981 }
3982 wpa_hexdump(MSG_DEBUG, "FILS: FILS Session", elems.fils_session,
3983 FILS_SESSION_LEN);
3984 if (os_memcmp(sm->fils_session, elems.fils_session, FILS_SESSION_LEN)
3985 != 0) {
3986 wpa_printf(MSG_DEBUG, "FILS: Session mismatch");
3987 wpa_hexdump(MSG_DEBUG, "FILS: Expected FILS Session",
3988 sm->fils_session, FILS_SESSION_LEN);
76e20f4f 3989 goto fail;
a6609937
JM
3990 }
3991
3992 /* FILS Wrapped Data */
3993 if (!sm->cur_pmksa && elems.fils_wrapped_data) {
fcd3d6ce
JM
3994 u8 rmsk[ERP_MAX_KEY_LEN];
3995 size_t rmsk_len;
3996
a6609937
JM
3997 wpa_hexdump(MSG_DEBUG, "FILS: Wrapped Data",
3998 elems.fils_wrapped_data,
3999 elems.fils_wrapped_data_len);
4000 eapol_sm_process_erp_finish(sm->eapol, elems.fils_wrapped_data,
4001 elems.fils_wrapped_data_len);
4002 if (eapol_sm_failed(sm->eapol))
76e20f4f 4003 goto fail;
a6609937 4004
fcd3d6ce
JM
4005 rmsk_len = ERP_MAX_KEY_LEN;
4006 res = eapol_sm_get_key(sm->eapol, rmsk, rmsk_len);
4007 if (res == PMK_LEN) {
4008 rmsk_len = PMK_LEN;
4009 res = eapol_sm_get_key(sm->eapol, rmsk, rmsk_len);
4010 }
a6609937 4011 if (res)
76e20f4f 4012 goto fail;
a6609937 4013
fcd3d6ce 4014 res = fils_rmsk_to_pmk(sm->key_mgmt, rmsk, rmsk_len,
76e20f4f
JM
4015 sm->fils_nonce, sm->fils_anonce,
4016 dh_ss ? wpabuf_head(dh_ss) : NULL,
4017 dh_ss ? wpabuf_len(dh_ss) : 0,
fcd3d6ce 4018 sm->pmk, &sm->pmk_len);
31bc66e4 4019 forced_memzero(rmsk, sizeof(rmsk));
4cada9dc
JM
4020
4021 /* Don't use DHss in PTK derivation if PMKSA caching is not
4022 * used. */
76e20f4f
JM
4023 wpabuf_clear_free(dh_ss);
4024 dh_ss = NULL;
4cada9dc 4025
275cc942 4026 if (res)
76e20f4f 4027 goto fail;
fcd3d6ce
JM
4028
4029 if (!sm->fils_erp_pmkid_set) {
4030 wpa_printf(MSG_DEBUG, "FILS: PMKID not available");
76e20f4f 4031 goto fail;
fcd3d6ce
JM
4032 }
4033 wpa_hexdump(MSG_DEBUG, "FILS: PMKID", sm->fils_erp_pmkid,
4034 PMKID_LEN);
a6609937 4035 wpa_printf(MSG_DEBUG, "FILS: ERP processing succeeded - add PMKSA cache entry for the result");
fcd3d6ce
JM
4036 sm->cur_pmksa = pmksa_cache_add(sm->pmksa, sm->pmk, sm->pmk_len,
4037 sm->fils_erp_pmkid, NULL, 0,
4038 sm->bssid, sm->own_addr,
869af307
JM
4039 sm->network_ctx, sm->key_mgmt,
4040 NULL);
a6609937
JM
4041 }
4042
4043 if (!sm->cur_pmksa) {
4044 wpa_printf(MSG_DEBUG,
4045 "FILS: No remaining options to continue FILS authentication");
76e20f4f 4046 goto fail;
a6609937
JM
4047 }
4048
4049 if (fils_pmk_to_ptk(sm->pmk, sm->pmk_len, sm->own_addr, sm->bssid,
4cada9dc
JM
4050 sm->fils_nonce, sm->fils_anonce,
4051 dh_ss ? wpabuf_head(dh_ss) : NULL,
4052 dh_ss ? wpabuf_len(dh_ss) : 0,
4053 &sm->ptk, ick, &ick_len,
4054 sm->key_mgmt, sm->pairwise_cipher,
7d440a3b 4055 sm->fils_ft, &sm->fils_ft_len) < 0) {
a6609937 4056 wpa_printf(MSG_DEBUG, "FILS: Failed to derive PTK");
76e20f4f 4057 goto fail;
a6609937 4058 }
4cada9dc
JM
4059
4060 wpabuf_clear_free(dh_ss);
4061 dh_ss = NULL;
4062
a6609937
JM
4063 sm->ptk_set = 1;
4064 sm->tptk_set = 0;
4065 os_memset(&sm->tptk, 0, sizeof(sm->tptk));
4066
e6b62313
JM
4067#ifdef CONFIG_FILS_SK_PFS
4068 if (sm->fils_dh_group) {
4069 if (!sm->fils_ecdh) {
4070 wpa_printf(MSG_INFO, "FILS: ECDH not initialized");
4071 goto fail;
4072 }
4073 pub = crypto_ecdh_get_pubkey(sm->fils_ecdh, 1);
4074 if (!pub)
4075 goto fail;
4076 wpa_hexdump_buf(MSG_DEBUG, "FILS: gSTA", pub);
4077 g_sta = wpabuf_head(pub);
4078 g_sta_len = wpabuf_len(pub);
4079 if (!g_ap) {
4080 wpa_printf(MSG_INFO, "FILS: gAP not available");
4081 goto fail;
4082 }
4083 wpa_hexdump(MSG_DEBUG, "FILS: gAP", g_ap, g_ap_len);
4084 }
4085#endif /* CONFIG_FILS_SK_PFS */
4086
a6609937
JM
4087 res = fils_key_auth_sk(ick, ick_len, sm->fils_nonce,
4088 sm->fils_anonce, sm->own_addr, sm->bssid,
e6b62313 4089 g_sta, g_sta_len, g_ap, g_ap_len,
a6609937
JM
4090 sm->key_mgmt, sm->fils_key_auth_sta,
4091 sm->fils_key_auth_ap,
4092 &sm->fils_key_auth_len);
e6b62313 4093 wpabuf_free(pub);
31bc66e4 4094 forced_memzero(ick, sizeof(ick));
a6609937 4095 return res;
76e20f4f 4096fail:
e6b62313 4097 wpabuf_free(pub);
76e20f4f
JM
4098 wpabuf_clear_free(dh_ss);
4099 return -1;
a6609937
JM
4100}
4101
86cd6928 4102
7d440a3b
JM
4103#ifdef CONFIG_IEEE80211R
4104static int fils_ft_build_assoc_req_rsne(struct wpa_sm *sm, struct wpabuf *buf)
4105{
4106 struct rsn_ie_hdr *rsnie;
4107 u16 capab;
4108 u8 *pos;
994eac7e 4109 int use_sha384 = wpa_key_mgmt_sha384(sm->key_mgmt);
7d440a3b
JM
4110
4111 /* RSNIE[PMKR0Name/PMKR1Name] */
4112 rsnie = wpabuf_put(buf, sizeof(*rsnie));
4113 rsnie->elem_id = WLAN_EID_RSN;
4114 WPA_PUT_LE16(rsnie->version, RSN_VERSION);
4115
4116 /* Group Suite Selector */
4117 if (!wpa_cipher_valid_group(sm->group_cipher)) {
4118 wpa_printf(MSG_WARNING, "FT: Invalid group cipher (%d)",
4119 sm->group_cipher);
4120 return -1;
4121 }
4122 pos = wpabuf_put(buf, RSN_SELECTOR_LEN);
4123 RSN_SELECTOR_PUT(pos, wpa_cipher_to_suite(WPA_PROTO_RSN,
4124 sm->group_cipher));
4125
4126 /* Pairwise Suite Count */
4127 wpabuf_put_le16(buf, 1);
4128
4129 /* Pairwise Suite List */
4130 if (!wpa_cipher_valid_pairwise(sm->pairwise_cipher)) {
4131 wpa_printf(MSG_WARNING, "FT: Invalid pairwise cipher (%d)",
4132 sm->pairwise_cipher);
4133 return -1;
4134 }
4135 pos = wpabuf_put(buf, RSN_SELECTOR_LEN);
4136 RSN_SELECTOR_PUT(pos, wpa_cipher_to_suite(WPA_PROTO_RSN,
4137 sm->pairwise_cipher));
4138
4139 /* Authenticated Key Management Suite Count */
4140 wpabuf_put_le16(buf, 1);
4141
4142 /* Authenticated Key Management Suite List */
4143 pos = wpabuf_put(buf, RSN_SELECTOR_LEN);
4144 if (sm->key_mgmt == WPA_KEY_MGMT_FT_FILS_SHA256)
4145 RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_FT_FILS_SHA256);
4146 else if (sm->key_mgmt == WPA_KEY_MGMT_FT_FILS_SHA384)
4147 RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_FT_FILS_SHA384);
4148 else {
4149 wpa_printf(MSG_WARNING,
4150 "FILS+FT: Invalid key management type (%d)",
4151 sm->key_mgmt);
4152 return -1;
4153 }
4154
4155 /* RSN Capabilities */
4156 capab = 0;
e0d22c84 4157 if (sm->mfp)
7d440a3b 4158 capab |= WPA_CAPABILITY_MFPC;
e0d22c84
JM
4159 if (sm->mfp == 2)
4160 capab |= WPA_CAPABILITY_MFPR;
55c12f5d
MV
4161 if (sm->ocv)
4162 capab |= WPA_CAPABILITY_OCVC;
7d440a3b
JM
4163 wpabuf_put_le16(buf, capab);
4164
4165 /* PMKID Count */
4166 wpabuf_put_le16(buf, 1);
4167
4168 /* PMKID List [PMKR1Name] */
4169 wpa_hexdump_key(MSG_DEBUG, "FILS+FT: XXKey (FILS-FT)",
4170 sm->fils_ft, sm->fils_ft_len);
4171 wpa_hexdump_ascii(MSG_DEBUG, "FILS+FT: SSID", sm->ssid, sm->ssid_len);
4172 wpa_hexdump(MSG_DEBUG, "FILS+FT: MDID",
4173 sm->mobility_domain, MOBILITY_DOMAIN_ID_LEN);
4174 wpa_hexdump_ascii(MSG_DEBUG, "FILS+FT: R0KH-ID",
4175 sm->r0kh_id, sm->r0kh_id_len);
4176 if (wpa_derive_pmk_r0(sm->fils_ft, sm->fils_ft_len, sm->ssid,
4177 sm->ssid_len, sm->mobility_domain,
4178 sm->r0kh_id, sm->r0kh_id_len, sm->own_addr,
994eac7e 4179 sm->pmk_r0, sm->pmk_r0_name, use_sha384) < 0) {
7d440a3b
JM
4180 wpa_printf(MSG_WARNING, "FILS+FT: Could not derive PMK-R0");
4181 return -1;
4182 }
994eac7e
JM
4183 sm->pmk_r0_len = use_sha384 ? SHA384_MAC_LEN : PMK_LEN;
4184 wpa_hexdump_key(MSG_DEBUG, "FILS+FT: PMK-R0",
4185 sm->pmk_r0, sm->pmk_r0_len);
7d440a3b
JM
4186 wpa_hexdump(MSG_DEBUG, "FILS+FT: PMKR0Name",
4187 sm->pmk_r0_name, WPA_PMK_NAME_LEN);
4188 wpa_printf(MSG_DEBUG, "FILS+FT: R1KH-ID: " MACSTR,
4189 MAC2STR(sm->r1kh_id));
4190 pos = wpabuf_put(buf, WPA_PMK_NAME_LEN);
4191 if (wpa_derive_pmk_r1_name(sm->pmk_r0_name, sm->r1kh_id, sm->own_addr,
b5e57699 4192 sm->pmk_r1_name, use_sha384) < 0) {
7d440a3b
JM
4193 wpa_printf(MSG_WARNING, "FILS+FT: Could not derive PMKR1Name");
4194 return -1;
4195 }
b5e57699
JM
4196 wpa_hexdump(MSG_DEBUG, "FILS+FT: PMKR1Name", sm->pmk_r1_name,
4197 WPA_PMK_NAME_LEN);
4198 os_memcpy(pos, sm->pmk_r1_name, WPA_PMK_NAME_LEN);
7d440a3b 4199
7d440a3b
JM
4200 if (sm->mgmt_group_cipher == WPA_CIPHER_AES_128_CMAC) {
4201 /* Management Group Cipher Suite */
4202 pos = wpabuf_put(buf, RSN_SELECTOR_LEN);
4203 RSN_SELECTOR_PUT(pos, RSN_CIPHER_SUITE_AES_128_CMAC);
4204 }
7d440a3b
JM
4205
4206 rsnie->len = ((u8 *) wpabuf_put(buf, 0) - (u8 *) rsnie) - 2;
4207 return 0;
4208}
4209#endif /* CONFIG_IEEE80211R */
4210
4211
86cd6928
JM
4212struct wpabuf * fils_build_assoc_req(struct wpa_sm *sm, const u8 **kek,
4213 size_t *kek_len, const u8 **snonce,
5732b770
JM
4214 const u8 **anonce,
4215 const struct wpabuf **hlp,
4216 unsigned int num_hlp)
86cd6928
JM
4217{
4218 struct wpabuf *buf;
5732b770
JM
4219 size_t len;
4220 unsigned int i;
86cd6928 4221
5732b770 4222 len = 1000;
7d440a3b
JM
4223#ifdef CONFIG_IEEE80211R
4224 if (sm->fils_ft_ies)
4225 len += wpabuf_len(sm->fils_ft_ies);
4226 if (wpa_key_mgmt_ft(sm->key_mgmt))
4227 len += 256;
4228#endif /* CONFIG_IEEE80211R */
5732b770
JM
4229 for (i = 0; hlp && i < num_hlp; i++)
4230 len += 10 + wpabuf_len(hlp[i]);
4231 buf = wpabuf_alloc(len);
86cd6928
JM
4232 if (!buf)
4233 return NULL;
4234
7d440a3b
JM
4235#ifdef CONFIG_IEEE80211R
4236 if (wpa_key_mgmt_ft(sm->key_mgmt) && sm->fils_ft_ies) {
4237 /* MDE and FTE when using FILS+FT */
4238 wpabuf_put_buf(buf, sm->fils_ft_ies);
4239 /* RSNE with PMKR1Name in PMKID field */
4240 if (fils_ft_build_assoc_req_rsne(sm, buf) < 0) {
4241 wpabuf_free(buf);
4242 return NULL;
4243 }
4244 }
4245#endif /* CONFIG_IEEE80211R */
4246
86cd6928
JM
4247 /* FILS Session */
4248 wpabuf_put_u8(buf, WLAN_EID_EXTENSION); /* Element ID */
4249 wpabuf_put_u8(buf, 1 + FILS_SESSION_LEN); /* Length */
4250 /* Element ID Extension */
4251 wpabuf_put_u8(buf, WLAN_EID_EXT_FILS_SESSION);
4252 wpabuf_put_data(buf, sm->fils_session, FILS_SESSION_LEN);
4253
4254 /* Everything after FILS Session element gets encrypted in the driver
4255 * with KEK. The buffer returned from here is the plaintext version. */
4256
4257 /* TODO: FILS Public Key */
4258
4259 /* FILS Key Confirm */
4260 wpabuf_put_u8(buf, WLAN_EID_EXTENSION); /* Element ID */
4261 wpabuf_put_u8(buf, 1 + sm->fils_key_auth_len); /* Length */
4262 /* Element ID Extension */
4263 wpabuf_put_u8(buf, WLAN_EID_EXT_FILS_KEY_CONFIRM);
4264 wpabuf_put_data(buf, sm->fils_key_auth_sta, sm->fils_key_auth_len);
4265
5732b770
JM
4266 /* FILS HLP Container */
4267 for (i = 0; hlp && i < num_hlp; i++) {
4268 const u8 *pos = wpabuf_head(hlp[i]);
4269 size_t left = wpabuf_len(hlp[i]);
4270
4271 wpabuf_put_u8(buf, WLAN_EID_EXTENSION); /* Element ID */
4272 if (left <= 254)
4273 len = 1 + left;
4274 else
4275 len = 255;
4276 wpabuf_put_u8(buf, len); /* Length */
4277 /* Element ID Extension */
4278 wpabuf_put_u8(buf, WLAN_EID_EXT_FILS_HLP_CONTAINER);
4279 /* Destination MAC Address, Source MAC Address, HLP Packet.
4280 * HLP Packet is in MSDU format (i.e., included the LLC/SNAP
4281 * header when LPD is used). */
4282 wpabuf_put_data(buf, pos, len - 1);
4283 pos += len - 1;
4284 left -= len - 1;
4285 while (left) {
4286 wpabuf_put_u8(buf, WLAN_EID_FRAGMENT);
4287 len = left > 255 ? 255 : left;
4288 wpabuf_put_u8(buf, len);
4289 wpabuf_put_data(buf, pos, len);
4290 pos += len;
4291 left -= len;
4292 }
4293 }
86cd6928
JM
4294
4295 /* TODO: FILS IP Address Assignment */
4296
99621dc1
MV
4297#ifdef CONFIG_OCV
4298 if (wpa_sm_ocv_enabled(sm)) {
4299 struct wpa_channel_info ci;
4300 u8 *pos;
4301
4302 if (wpa_sm_channel_info(sm, &ci) != 0) {
4303 wpa_printf(MSG_WARNING,
4304 "FILS: Failed to get channel info for OCI element");
4305 wpabuf_free(buf);
4306 return NULL;
4307 }
4308
4309 pos = wpabuf_put(buf, OCV_OCI_EXTENDED_LEN);
4310 if (ocv_insert_extended_oci(&ci, pos) < 0) {
4311 wpabuf_free(buf);
4312 return NULL;
4313 }
4314 }
4315#endif /* CONFIG_OCV */
4316
86cd6928
JM
4317 wpa_hexdump_buf(MSG_DEBUG, "FILS: Association Request plaintext", buf);
4318
4319 *kek = sm->ptk.kek;
4320 *kek_len = sm->ptk.kek_len;
4321 wpa_hexdump_key(MSG_DEBUG, "FILS: KEK for AEAD", *kek, *kek_len);
4322 *snonce = sm->fils_nonce;
4323 wpa_hexdump(MSG_DEBUG, "FILS: SNonce for AEAD AAD",
4324 *snonce, FILS_NONCE_LEN);
4325 *anonce = sm->fils_anonce;
4326 wpa_hexdump(MSG_DEBUG, "FILS: ANonce for AEAD AAD",
4327 *anonce, FILS_NONCE_LEN);
4328
4329 return buf;
4330}
4331
706df429 4332
124ddfa1
JM
4333static void fils_process_hlp_resp(struct wpa_sm *sm, const u8 *resp, size_t len)
4334{
4335 const u8 *pos, *end;
4336
4337 wpa_hexdump(MSG_MSGDUMP, "FILS: HLP response", resp, len);
4338 if (len < 2 * ETH_ALEN)
4339 return;
4340 pos = resp + 2 * ETH_ALEN;
4341 end = resp + len;
4342 if (end - pos >= 6 &&
4343 os_memcmp(pos, "\xaa\xaa\x03\x00\x00\x00", 6) == 0)
4344 pos += 6; /* Remove SNAP/LLC header */
4345 wpa_sm_fils_hlp_rx(sm, resp, resp + ETH_ALEN, pos, end - pos);
4346}
4347
4348
4349static void fils_process_hlp_container(struct wpa_sm *sm, const u8 *pos,
4350 size_t len)
4351{
4352 const u8 *end = pos + len;
4353 u8 *tmp, *tmp_pos;
4354
4355 /* Check if there are any FILS HLP Container elements */
4356 while (end - pos >= 2) {
4357 if (2 + pos[1] > end - pos)
4358 return;
4359 if (pos[0] == WLAN_EID_EXTENSION &&
4360 pos[1] >= 1 + 2 * ETH_ALEN &&
4361 pos[2] == WLAN_EID_EXT_FILS_HLP_CONTAINER)
4362 break;
4363 pos += 2 + pos[1];
4364 }
4365 if (end - pos < 2)
4366 return; /* No FILS HLP Container elements */
4367
4368 tmp = os_malloc(end - pos);
4369 if (!tmp)
4370 return;
4371
4372 while (end - pos >= 2) {
4373 if (2 + pos[1] > end - pos ||
4374 pos[0] != WLAN_EID_EXTENSION ||
4375 pos[1] < 1 + 2 * ETH_ALEN ||
4376 pos[2] != WLAN_EID_EXT_FILS_HLP_CONTAINER)
4377 break;
4378 tmp_pos = tmp;
4379 os_memcpy(tmp_pos, pos + 3, pos[1] - 1);
4380 tmp_pos += pos[1] - 1;
4381 pos += 2 + pos[1];
4382
4383 /* Add possible fragments */
4384 while (end - pos >= 2 && pos[0] == WLAN_EID_FRAGMENT &&
4385 2 + pos[1] <= end - pos) {
4386 os_memcpy(tmp_pos, pos + 2, pos[1]);
4387 tmp_pos += pos[1];
4388 pos += 2 + pos[1];
4389 }
4390
4391 fils_process_hlp_resp(sm, tmp, tmp_pos - tmp);
4392 }
4393
4394 os_free(tmp);
4395}
4396
4397
706df429
JM
4398int fils_process_assoc_resp(struct wpa_sm *sm, const u8 *resp, size_t len)
4399{
4400 const struct ieee80211_mgmt *mgmt;
4401 const u8 *end, *ie_start;
4402 struct ieee802_11_elems elems;
4403 int keylen, rsclen;
4404 enum wpa_alg alg;
4405 struct wpa_gtk_data gd;
4406 int maxkeylen;
4407 struct wpa_eapol_ie_parse kde;
4408
4409 if (!sm || !sm->ptk_set) {
4410 wpa_printf(MSG_DEBUG, "FILS: No KEK available");
4411 return -1;
4412 }
4413
4414 if (!wpa_key_mgmt_fils(sm->key_mgmt)) {
4415 wpa_printf(MSG_DEBUG, "FILS: Not a FILS AKM");
4416 return -1;
4417 }
4418
e7608511
JM
4419 if (sm->fils_completed) {
4420 wpa_printf(MSG_DEBUG,
4421 "FILS: Association has already been completed for this FILS authentication - ignore unexpected retransmission");
4422 return -1;
4423 }
4424
706df429
JM
4425 wpa_hexdump(MSG_DEBUG, "FILS: (Re)Association Response frame",
4426 resp, len);
4427
4428 mgmt = (const struct ieee80211_mgmt *) resp;
4429 if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.assoc_resp))
4430 return -1;
4431
4432 end = resp + len;
4433 /* Same offset for Association Response and Reassociation Response */
4434 ie_start = mgmt->u.assoc_resp.variable;
4435
4436 if (ieee802_11_parse_elems(ie_start, end - ie_start, &elems, 1) ==
4437 ParseFailed) {
4438 wpa_printf(MSG_DEBUG,
4439 "FILS: Failed to parse decrypted elements");
4440 goto fail;
4441 }
4442
4443 if (!elems.fils_session) {
4444 wpa_printf(MSG_DEBUG, "FILS: No FILS Session element");
4445 return -1;
4446 }
4447 if (os_memcmp(elems.fils_session, sm->fils_session,
4448 FILS_SESSION_LEN) != 0) {
4449 wpa_printf(MSG_DEBUG, "FILS: FILS Session mismatch");
4450 wpa_hexdump(MSG_DEBUG, "FILS: Received FILS Session",
4451 elems.fils_session, FILS_SESSION_LEN);
4452 wpa_hexdump(MSG_DEBUG, "FILS: Expected FILS Session",
4453 sm->fils_session, FILS_SESSION_LEN);
4454 }
4455
69afb6b3
JM
4456 if (!elems.rsn_ie) {
4457 wpa_printf(MSG_DEBUG,
4458 "FILS: No RSNE in (Re)Association Response");
4459 /* As an interop workaround, allow this for now since IEEE Std
4460 * 802.11ai-2016 did not include all the needed changes to make
4461 * a FILS AP include RSNE in the frame. This workaround might
4462 * eventually be removed and replaced with rejection (goto fail)
4463 * to follow a strict interpretation of the standard. */
4464 } else if (wpa_compare_rsn_ie(wpa_key_mgmt_ft(sm->key_mgmt),
4465 sm->ap_rsn_ie, sm->ap_rsn_ie_len,
4466 elems.rsn_ie - 2, elems.rsn_ie_len + 2)) {
4467 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
4468 "FILS: RSNE mismatch between Beacon/Probe Response and (Re)Association Response");
4469 wpa_hexdump(MSG_DEBUG, "FILS: RSNE in Beacon/Probe Response",
4470 sm->ap_rsn_ie, sm->ap_rsn_ie_len);
4471 wpa_hexdump(MSG_DEBUG, "FILS: RSNE in (Re)Association Response",
4472 elems.rsn_ie, elems.rsn_ie_len);
4473 goto fail;
4474 }
4475
706df429
JM
4476 /* TODO: FILS Public Key */
4477
4478 if (!elems.fils_key_confirm) {
4479 wpa_printf(MSG_DEBUG, "FILS: No FILS Key Confirm element");
4480 goto fail;
4481 }
4482 if (elems.fils_key_confirm_len != sm->fils_key_auth_len) {
4483 wpa_printf(MSG_DEBUG,
4484 "FILS: Unexpected Key-Auth length %d (expected %d)",
4485 elems.fils_key_confirm_len,
4486 (int) sm->fils_key_auth_len);
4487 goto fail;
4488 }
4489 if (os_memcmp(elems.fils_key_confirm, sm->fils_key_auth_ap,
4490 sm->fils_key_auth_len) != 0) {
4491 wpa_printf(MSG_DEBUG, "FILS: Key-Auth mismatch");
4492 wpa_hexdump(MSG_DEBUG, "FILS: Received Key-Auth",
4493 elems.fils_key_confirm,
4494 elems.fils_key_confirm_len);
4495 wpa_hexdump(MSG_DEBUG, "FILS: Expected Key-Auth",
4496 sm->fils_key_auth_ap, sm->fils_key_auth_len);
4497 goto fail;
4498 }
4499
99621dc1
MV
4500#ifdef CONFIG_OCV
4501 if (wpa_sm_ocv_enabled(sm)) {
4502 struct wpa_channel_info ci;
4503
4504 if (wpa_sm_channel_info(sm, &ci) != 0) {
4505 wpa_printf(MSG_WARNING,
4506 "Failed to get channel info to validate received OCI in FILS (Re)Association Response frame");
4507 goto fail;
4508 }
4509
4510 if (ocv_verify_tx_params(elems.oci, elems.oci_len, &ci,
4511 channel_width_to_int(ci.chanwidth),
4512 ci.seg1_idx) != 0) {
4513 wpa_printf(MSG_WARNING, "FILS: %s", ocv_errorstr);
4514 goto fail;
4515 }
4516 }
4517#endif /* CONFIG_OCV */
4518
b5e57699
JM
4519#ifdef CONFIG_IEEE80211R
4520 if (wpa_key_mgmt_ft(sm->key_mgmt) && sm->fils_ft_ies) {
4521 struct wpa_ie_data rsn;
4522
4523 /* Check that PMKR1Name derived by the AP matches */
4524 if (!elems.rsn_ie ||
4525 wpa_parse_wpa_ie_rsn(elems.rsn_ie - 2, elems.rsn_ie_len + 2,
4526 &rsn) < 0 ||
4527 !rsn.pmkid || rsn.num_pmkid != 1 ||
4528 os_memcmp(rsn.pmkid, sm->pmk_r1_name,
4529 WPA_PMK_NAME_LEN) != 0) {
4530 wpa_printf(MSG_DEBUG,
4531 "FILS+FT: No RSNE[PMKR1Name] match in AssocResp");
4532 goto fail;
4533 }
4534 }
4535#endif /* CONFIG_IEEE80211R */
4536
706df429
JM
4537 /* Key Delivery */
4538 if (!elems.key_delivery) {
4539 wpa_printf(MSG_DEBUG, "FILS: No Key Delivery element");
4540 goto fail;
4541 }
4542
4543 /* Parse GTK and set the key to the driver */
4544 os_memset(&gd, 0, sizeof(gd));
4545 if (wpa_supplicant_parse_ies(elems.key_delivery + WPA_KEY_RSC_LEN,
4546 elems.key_delivery_len - WPA_KEY_RSC_LEN,
4547 &kde) < 0) {
4548 wpa_printf(MSG_DEBUG, "FILS: Failed to parse KDEs");
4549 goto fail;
4550 }
4551 if (!kde.gtk) {
4552 wpa_printf(MSG_DEBUG, "FILS: No GTK KDE");
4553 goto fail;
4554 }
4555 maxkeylen = gd.gtk_len = kde.gtk_len - 2;
4556 if (wpa_supplicant_check_group_cipher(sm, sm->group_cipher,
4557 gd.gtk_len, maxkeylen,
4558 &gd.key_rsc_len, &gd.alg))
4559 goto fail;
4560
4561 wpa_hexdump_key(MSG_DEBUG, "FILS: Received GTK", kde.gtk, kde.gtk_len);
4562 gd.keyidx = kde.gtk[0] & 0x3;
4563 gd.tx = wpa_supplicant_gtk_tx_bit_workaround(sm,
4564 !!(kde.gtk[0] & BIT(2)));
4565 if (kde.gtk_len - 2 > sizeof(gd.gtk)) {
4566 wpa_printf(MSG_DEBUG, "FILS: Too long GTK in GTK KDE (len=%lu)",
4567 (unsigned long) kde.gtk_len - 2);
4568 goto fail;
4569 }
4570 os_memcpy(gd.gtk, kde.gtk + 2, kde.gtk_len - 2);
4571
4572 wpa_printf(MSG_DEBUG, "FILS: Set GTK to driver");
87e2db16 4573 if (wpa_supplicant_install_gtk(sm, &gd, elems.key_delivery, 0) < 0) {
706df429
JM
4574 wpa_printf(MSG_DEBUG, "FILS: Failed to set GTK");
4575 goto fail;
4576 }
4577
4578 if (ieee80211w_set_keys(sm, &kde) < 0) {
4579 wpa_printf(MSG_DEBUG, "FILS: Failed to set IGTK");
4580 goto fail;
4581 }
4582
4583 alg = wpa_cipher_to_alg(sm->pairwise_cipher);
4584 keylen = wpa_cipher_key_len(sm->pairwise_cipher);
a6ea6653
JM
4585 if (keylen <= 0 || (unsigned int) keylen != sm->ptk.tk_len) {
4586 wpa_printf(MSG_DEBUG, "FILS: TK length mismatch: %u != %lu",
4587 keylen, (long unsigned int) sm->ptk.tk_len);
4588 goto fail;
4589 }
706df429
JM
4590 rsclen = wpa_cipher_rsc_len(sm->pairwise_cipher);
4591 wpa_hexdump_key(MSG_DEBUG, "FILS: Set TK to driver",
4592 sm->ptk.tk, keylen);
4593 if (wpa_sm_set_key(sm, alg, sm->bssid, 0, 1, null_rsc, rsclen,
a919a260 4594 sm->ptk.tk, keylen, KEY_FLAG_PAIRWISE_RX_TX) < 0) {
706df429
JM
4595 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
4596 "FILS: Failed to set PTK to the driver (alg=%d keylen=%d bssid="
4597 MACSTR ")",
4598 alg, keylen, MAC2STR(sm->bssid));
4599 goto fail;
4600 }
4601
4602 /* TODO: TK could be cleared after auth frame exchange now that driver
4603 * takes care of association frame encryption/decryption. */
4604 /* TK is not needed anymore in supplicant */
4605 os_memset(sm->ptk.tk, 0, WPA_TK_MAX_LEN);
a6ea6653 4606 sm->ptk.tk_len = 0;
53bb18cc 4607 sm->ptk.installed = 1;
706df429 4608
124ddfa1
JM
4609 /* FILS HLP Container */
4610 fils_process_hlp_container(sm, ie_start, end - ie_start);
706df429
JM
4611
4612 /* TODO: FILS IP Address Assignment */
4613
4614 wpa_printf(MSG_DEBUG, "FILS: Auth+Assoc completed successfully");
4615 sm->fils_completed = 1;
31bc66e4 4616 forced_memzero(&gd, sizeof(gd));
706df429
JM
4617
4618 return 0;
4619fail:
31bc66e4 4620 forced_memzero(&gd, sizeof(gd));
706df429
JM
4621 return -1;
4622}
4623
5538fc93
VK
4624
4625void wpa_sm_set_reset_fils_completed(struct wpa_sm *sm, int set)
4626{
4627 if (sm)
4628 sm->fils_completed = !!set;
4629}
4630
706df429
JM
4631#endif /* CONFIG_FILS */
4632
4633
4634int wpa_fils_is_completed(struct wpa_sm *sm)
4635{
4636#ifdef CONFIG_FILS
4637 return sm && sm->fils_completed;
4638#else /* CONFIG_FILS */
4639 return 0;
f00b9b88 4640#endif /* CONFIG_FILS */
706df429 4641}
0a614799
JM
4642
4643
4644#ifdef CONFIG_OWE
4645
ec9f4837 4646struct wpabuf * owe_build_assoc_req(struct wpa_sm *sm, u16 group)
0a614799
JM
4647{
4648 struct wpabuf *ie = NULL, *pub = NULL;
ec9f4837
JM
4649 size_t prime_len;
4650
4651 if (group == 19)
4652 prime_len = 32;
4653 else if (group == 20)
4654 prime_len = 48;
4655 else if (group == 21)
4656 prime_len = 66;
4657 else
4658 return NULL;
0a614799
JM
4659
4660 crypto_ecdh_deinit(sm->owe_ecdh);
ec9f4837 4661 sm->owe_ecdh = crypto_ecdh_init(group);
0a614799
JM
4662 if (!sm->owe_ecdh)
4663 goto fail;
ec9f4837 4664 sm->owe_group = group;
0a614799 4665 pub = crypto_ecdh_get_pubkey(sm->owe_ecdh, 0);
ec9f4837 4666 pub = wpabuf_zeropad(pub, prime_len);
0a614799
JM
4667 if (!pub)
4668 goto fail;
4669
4670 ie = wpabuf_alloc(5 + wpabuf_len(pub));
4671 if (!ie)
4672 goto fail;
4673 wpabuf_put_u8(ie, WLAN_EID_EXTENSION);
4674 wpabuf_put_u8(ie, 1 + 2 + wpabuf_len(pub));
4675 wpabuf_put_u8(ie, WLAN_EID_EXT_OWE_DH_PARAM);
ec9f4837 4676 wpabuf_put_le16(ie, group);
0a614799
JM
4677 wpabuf_put_buf(ie, pub);
4678 wpabuf_free(pub);
4679 wpa_hexdump_buf(MSG_DEBUG, "OWE: Diffie-Hellman Parameter element",
4680 ie);
4681
4682 return ie;
4683fail:
4684 wpabuf_free(pub);
4685 crypto_ecdh_deinit(sm->owe_ecdh);
4686 sm->owe_ecdh = NULL;
4687 return NULL;
4688}
4689
4690
5a78c361
JM
4691int owe_process_assoc_resp(struct wpa_sm *sm, const u8 *bssid,
4692 const u8 *resp_ies, size_t resp_ies_len)
0a614799
JM
4693{
4694 struct ieee802_11_elems elems;
4695 u16 group;
4696 struct wpabuf *secret, *pub, *hkey;
4697 int res;
ec9f4837 4698 u8 prk[SHA512_MAC_LEN], pmkid[SHA512_MAC_LEN];
0a614799
JM
4699 const char *info = "OWE Key Generation";
4700 const u8 *addr[2];
4701 size_t len[2];
ec9f4837 4702 size_t hash_len, prime_len;
5a78c361 4703 struct wpa_ie_data data;
0a614799
JM
4704
4705 if (!resp_ies ||
4706 ieee802_11_parse_elems(resp_ies, resp_ies_len, &elems, 1) ==
5a78c361
JM
4707 ParseFailed) {
4708 wpa_printf(MSG_INFO,
4709 "OWE: Could not parse Association Response frame elements");
4710 return -1;
4711 }
4712
4713 if (sm->cur_pmksa && elems.rsn_ie &&
4714 wpa_parse_wpa_ie_rsn(elems.rsn_ie - 2, 2 + elems.rsn_ie_len,
4715 &data) == 0 &&
4716 data.num_pmkid == 1 && data.pmkid &&
4717 os_memcmp(sm->cur_pmksa->pmkid, data.pmkid, PMKID_LEN) == 0) {
4718 wpa_printf(MSG_DEBUG, "OWE: Use PMKSA caching");
4719 wpa_sm_set_pmk_from_pmksa(sm);
4720 return 0;
4721 }
4722
4723 if (!elems.owe_dh) {
0a614799
JM
4724 wpa_printf(MSG_INFO,
4725 "OWE: No Diffie-Hellman Parameter element found in Association Response frame");
4726 return -1;
4727 }
4728
4729 group = WPA_GET_LE16(elems.owe_dh);
ec9f4837 4730 if (group != sm->owe_group) {
0a614799
JM
4731 wpa_printf(MSG_INFO,
4732 "OWE: Unexpected Diffie-Hellman group in response: %u",
4733 group);
4734 return -1;
4735 }
4736
4737 if (!sm->owe_ecdh) {
4738 wpa_printf(MSG_INFO, "OWE: No ECDH state available");
4739 return -1;
4740 }
4741
ec9f4837
JM
4742 if (group == 19)
4743 prime_len = 32;
4744 else if (group == 20)
4745 prime_len = 48;
4746 else if (group == 21)
4747 prime_len = 66;
4748 else
4749 return -1;
4750
0a614799
JM
4751 secret = crypto_ecdh_set_peerkey(sm->owe_ecdh, 0,
4752 elems.owe_dh + 2,
4753 elems.owe_dh_len - 2);
ec9f4837 4754 secret = wpabuf_zeropad(secret, prime_len);
0a614799
JM
4755 if (!secret) {
4756 wpa_printf(MSG_DEBUG, "OWE: Invalid peer DH public key");
4757 return -1;
4758 }
4759 wpa_hexdump_buf_key(MSG_DEBUG, "OWE: DH shared secret", secret);
4760
4761 /* prk = HKDF-extract(C | A | group, z) */
4762
4763 pub = crypto_ecdh_get_pubkey(sm->owe_ecdh, 0);
4764 if (!pub) {
4765 wpabuf_clear_free(secret);
4766 return -1;
4767 }
4768
4769 /* PMKID = Truncate-128(Hash(C | A)) */
4770 addr[0] = wpabuf_head(pub);
4771 len[0] = wpabuf_len(pub);
4772 addr[1] = elems.owe_dh + 2;
4773 len[1] = elems.owe_dh_len - 2;
ec9f4837
JM
4774 if (group == 19) {
4775 res = sha256_vector(2, addr, len, pmkid);
4776 hash_len = SHA256_MAC_LEN;
4777 } else if (group == 20) {
4778 res = sha384_vector(2, addr, len, pmkid);
4779 hash_len = SHA384_MAC_LEN;
4780 } else if (group == 21) {
4781 res = sha512_vector(2, addr, len, pmkid);
4782 hash_len = SHA512_MAC_LEN;
4783 } else {
4784 res = -1;
4785 hash_len = 0;
4786 }
4787 pub = wpabuf_zeropad(pub, prime_len);
4788 if (res < 0 || !pub) {
0a614799
JM
4789 wpabuf_free(pub);
4790 wpabuf_clear_free(secret);
4791 return -1;
4792 }
4793
4794 hkey = wpabuf_alloc(wpabuf_len(pub) + elems.owe_dh_len - 2 + 2);
4795 if (!hkey) {
4796 wpabuf_free(pub);
4797 wpabuf_clear_free(secret);
4798 return -1;
4799 }
4800
4801 wpabuf_put_buf(hkey, pub); /* C */
4802 wpabuf_free(pub);
4803 wpabuf_put_data(hkey, elems.owe_dh + 2, elems.owe_dh_len - 2); /* A */
ec9f4837
JM
4804 wpabuf_put_le16(hkey, sm->owe_group); /* group */
4805 if (group == 19)
4806 res = hmac_sha256(wpabuf_head(hkey), wpabuf_len(hkey),
4807 wpabuf_head(secret), wpabuf_len(secret), prk);
4808 else if (group == 20)
4809 res = hmac_sha384(wpabuf_head(hkey), wpabuf_len(hkey),
4810 wpabuf_head(secret), wpabuf_len(secret), prk);
4811 else if (group == 21)
4812 res = hmac_sha512(wpabuf_head(hkey), wpabuf_len(hkey),
4813 wpabuf_head(secret), wpabuf_len(secret), prk);
0a614799
JM
4814 wpabuf_clear_free(hkey);
4815 wpabuf_clear_free(secret);
4816 if (res < 0)
4817 return -1;
4818
ec9f4837 4819 wpa_hexdump_key(MSG_DEBUG, "OWE: prk", prk, hash_len);
0a614799
JM
4820
4821 /* PMK = HKDF-expand(prk, "OWE Key Generation", n) */
4822
ec9f4837
JM
4823 if (group == 19)
4824 res = hmac_sha256_kdf(prk, hash_len, NULL, (const u8 *) info,
4825 os_strlen(info), sm->pmk, hash_len);
4826 else if (group == 20)
4827 res = hmac_sha384_kdf(prk, hash_len, NULL, (const u8 *) info,
4828 os_strlen(info), sm->pmk, hash_len);
4829 else if (group == 21)
4830 res = hmac_sha512_kdf(prk, hash_len, NULL, (const u8 *) info,
4831 os_strlen(info), sm->pmk, hash_len);
31bc66e4 4832 forced_memzero(prk, SHA512_MAC_LEN);
1bd13110
JM
4833 if (res < 0) {
4834 sm->pmk_len = 0;
0a614799 4835 return -1;
1bd13110 4836 }
ec9f4837 4837 sm->pmk_len = hash_len;
0a614799 4838
ec9f4837 4839 wpa_hexdump_key(MSG_DEBUG, "OWE: PMK", sm->pmk, sm->pmk_len);
0a614799 4840 wpa_hexdump(MSG_DEBUG, "OWE: PMKID", pmkid, PMKID_LEN);
5a78c361
JM
4841 pmksa_cache_add(sm->pmksa, sm->pmk, sm->pmk_len, pmkid, NULL, 0,
4842 bssid, sm->own_addr, sm->network_ctx, sm->key_mgmt,
4843 NULL);
0a614799
JM
4844
4845 return 0;
4846}
4847
4848#endif /* CONFIG_OWE */
bbe7969d
VK
4849
4850
4851void wpa_sm_set_fils_cache_id(struct wpa_sm *sm, const u8 *fils_cache_id)
4852{
4853#ifdef CONFIG_FILS
4854 if (sm && fils_cache_id) {
4855 sm->fils_cache_id_set = 1;
4856 os_memcpy(sm->fils_cache_id, fils_cache_id, FILS_CACHE_ID_LEN);
4857 }
4858#endif /* CONFIG_FILS */
4859}
10ec6a5f
JM
4860
4861
4862#ifdef CONFIG_DPP2
4863void wpa_sm_set_dpp_z(struct wpa_sm *sm, const struct wpabuf *z)
4864{
4865 if (sm) {
4866 wpabuf_clear_free(sm->dpp_z);
4867 sm->dpp_z = z ? wpabuf_dup(z) : NULL;
4868 }
4869}
4870#endif /* CONFIG_DPP2 */