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