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