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