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