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