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