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