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