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