2 * WPA Supplicant - IEEE 802.11r - Fast BSS Transition
3 * Copyright (c) 2006-2018, Jouni Malinen <j@w1.fi>
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
12 #include "crypto/aes_wrap.h"
13 #include "crypto/sha384.h"
14 #include "crypto/random.h"
15 #include "common/ieee802_11_defs.h"
16 #include "common/ieee802_11_common.h"
17 #include "common/ocv.h"
18 #include "drivers/driver.h"
22 #ifdef CONFIG_IEEE80211R
24 int wpa_derive_ptk_ft(struct wpa_sm *sm, const unsigned char *src_addr,
25 const struct wpa_eapol_key *key, struct wpa_ptk *ptk)
27 u8 ptk_name[WPA_PMK_NAME_LEN];
28 const u8 *anonce = key->key_nonce;
29 int use_sha384 = wpa_key_mgmt_sha384(sm->key_mgmt);
31 if (sm->xxkey_len == 0) {
32 wpa_printf(MSG_DEBUG, "FT: XXKey not available for key "
37 sm->pmk_r0_len = use_sha384 ? SHA384_MAC_LEN : PMK_LEN;
38 if (wpa_derive_pmk_r0(sm->xxkey, sm->xxkey_len, sm->ssid,
39 sm->ssid_len, sm->mobility_domain,
40 sm->r0kh_id, sm->r0kh_id_len, sm->own_addr,
41 sm->pmk_r0, sm->pmk_r0_name, use_sha384) < 0)
43 wpa_hexdump_key(MSG_DEBUG, "FT: PMK-R0", sm->pmk_r0, sm->pmk_r0_len);
44 wpa_hexdump(MSG_DEBUG, "FT: PMKR0Name",
45 sm->pmk_r0_name, WPA_PMK_NAME_LEN);
46 sm->pmk_r1_len = sm->pmk_r0_len;
47 if (wpa_derive_pmk_r1(sm->pmk_r0, sm->pmk_r0_len, sm->pmk_r0_name,
48 sm->r1kh_id, sm->own_addr, sm->pmk_r1,
51 wpa_hexdump_key(MSG_DEBUG, "FT: PMK-R1", sm->pmk_r1, sm->pmk_r1_len);
52 wpa_hexdump(MSG_DEBUG, "FT: PMKR1Name", sm->pmk_r1_name,
54 return wpa_pmk_r1_to_ptk(sm->pmk_r1, sm->pmk_r1_len, sm->snonce, anonce,
55 sm->own_addr, sm->bssid, sm->pmk_r1_name, ptk,
56 ptk_name, sm->key_mgmt, sm->pairwise_cipher);
61 * wpa_sm_set_ft_params - Set FT (IEEE 802.11r) parameters
62 * @sm: Pointer to WPA state machine data from wpa_sm_init()
63 * @ies: Association Response IEs or %NULL to clear FT parameters
64 * @ies_len: Length of ies buffer in octets
65 * Returns: 0 on success, -1 on failure
67 int wpa_sm_set_ft_params(struct wpa_sm *sm, const u8 *ies, size_t ies_len)
75 use_sha384 = wpa_key_mgmt_sha384(sm->key_mgmt);
76 if (wpa_ft_parse_ies(ies, ies_len, &ft, use_sha384) < 0)
79 if (ft.mdie && ft.mdie_len < MOBILITY_DOMAIN_ID_LEN + 1)
83 wpa_hexdump(MSG_DEBUG, "FT: Mobility domain",
84 ft.mdie, MOBILITY_DOMAIN_ID_LEN);
85 os_memcpy(sm->mobility_domain, ft.mdie,
86 MOBILITY_DOMAIN_ID_LEN);
87 sm->mdie_ft_capab = ft.mdie[MOBILITY_DOMAIN_ID_LEN];
88 wpa_printf(MSG_DEBUG, "FT: Capability and Policy: 0x%02x",
91 os_memset(sm->mobility_domain, 0, MOBILITY_DOMAIN_ID_LEN);
94 wpa_hexdump(MSG_DEBUG, "FT: R0KH-ID",
95 ft.r0kh_id, ft.r0kh_id_len);
96 os_memcpy(sm->r0kh_id, ft.r0kh_id, ft.r0kh_id_len);
97 sm->r0kh_id_len = ft.r0kh_id_len;
99 /* FIX: When should R0KH-ID be cleared? We need to keep the
100 * old R0KH-ID in order to be able to use this during FT. */
102 * os_memset(sm->r0kh_id, 0, FT_R0KH_ID_LEN);
103 * sm->r0kh_id_len = 0;
108 wpa_hexdump(MSG_DEBUG, "FT: R1KH-ID",
109 ft.r1kh_id, FT_R1KH_ID_LEN);
110 os_memcpy(sm->r1kh_id, ft.r1kh_id, FT_R1KH_ID_LEN);
112 os_memset(sm->r1kh_id, 0, FT_R1KH_ID_LEN);
114 os_free(sm->assoc_resp_ies);
115 sm->assoc_resp_ies = os_malloc(ft.mdie_len + 2 + ft.ftie_len + 2);
116 if (sm->assoc_resp_ies) {
117 u8 *pos = sm->assoc_resp_ies;
119 os_memcpy(pos, ft.mdie - 2, ft.mdie_len + 2);
120 pos += ft.mdie_len + 2;
123 os_memcpy(pos, ft.ftie - 2, ft.ftie_len + 2);
124 pos += ft.ftie_len + 2;
126 sm->assoc_resp_ies_len = pos - sm->assoc_resp_ies;
127 wpa_hexdump(MSG_DEBUG, "FT: Stored MDIE and FTIE from "
128 "(Re)Association Response",
129 sm->assoc_resp_ies, sm->assoc_resp_ies_len);
137 * wpa_ft_gen_req_ies - Generate FT (IEEE 802.11r) IEs for Auth/ReAssoc Request
138 * @sm: Pointer to WPA state machine data from wpa_sm_init()
139 * @len: Buffer for returning the length of the IEs
140 * @anonce: ANonce or %NULL if not yet available
141 * @pmk_name: PMKR0Name or PMKR1Name to be added into the RSN IE PMKID List
142 * @kck: 128-bit KCK for MIC or %NULL if no MIC is used
143 * @kck_len: KCK length in octets
144 * @target_ap: Target AP address
145 * @ric_ies: Optional IE(s), e.g., WMM TSPEC(s), for RIC-Request or %NULL
146 * @ric_ies_len: Length of ric_ies buffer in octets
147 * @ap_mdie: Mobility Domain IE from the target AP
148 * Returns: Pointer to buffer with IEs or %NULL on failure
150 * Caller is responsible for freeing the returned buffer with os_free();
152 static u8 * wpa_ft_gen_req_ies(struct wpa_sm *sm, size_t *len,
153 const u8 *anonce, const u8 *pmk_name,
154 const u8 *kck, size_t kck_len,
156 const u8 *ric_ies, size_t ric_ies_len,
160 u8 *buf, *pos, *ftie_len, *ftie_pos, *fte_mic, *elem_count;
161 struct rsn_mdie *mdie;
162 struct rsn_ie_hdr *rsnie;
166 sm->ft_completed = 0;
167 sm->ft_reassoc_completed = 0;
169 buf_len = 2 + sizeof(struct rsn_mdie) + 2 +
170 sizeof(struct rsn_ftie_sha384) +
171 2 + sm->r0kh_id_len + ric_ies_len + 100;
172 buf = os_zalloc(buf_len);
177 /* RSNIE[PMKR0Name/PMKR1Name] */
178 rsnie = (struct rsn_ie_hdr *) pos;
179 rsnie->elem_id = WLAN_EID_RSN;
180 WPA_PUT_LE16(rsnie->version, RSN_VERSION);
181 pos = (u8 *) (rsnie + 1);
183 /* Group Suite Selector */
184 if (!wpa_cipher_valid_group(sm->group_cipher)) {
185 wpa_printf(MSG_WARNING, "FT: Invalid group cipher (%d)",
190 RSN_SELECTOR_PUT(pos, wpa_cipher_to_suite(WPA_PROTO_RSN,
192 pos += RSN_SELECTOR_LEN;
194 /* Pairwise Suite Count */
195 WPA_PUT_LE16(pos, 1);
198 /* Pairwise Suite List */
199 if (!wpa_cipher_valid_pairwise(sm->pairwise_cipher)) {
200 wpa_printf(MSG_WARNING, "FT: Invalid pairwise cipher (%d)",
201 sm->pairwise_cipher);
205 RSN_SELECTOR_PUT(pos, wpa_cipher_to_suite(WPA_PROTO_RSN,
206 sm->pairwise_cipher));
207 pos += RSN_SELECTOR_LEN;
209 /* Authenticated Key Management Suite Count */
210 WPA_PUT_LE16(pos, 1);
213 /* Authenticated Key Management Suite List */
214 if (sm->key_mgmt == WPA_KEY_MGMT_FT_IEEE8021X)
215 RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_FT_802_1X);
217 else if (sm->key_mgmt == WPA_KEY_MGMT_FT_IEEE8021X_SHA384)
218 RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_FT_802_1X_SHA384);
219 #endif /* CONFIG_SHA384 */
220 else if (sm->key_mgmt == WPA_KEY_MGMT_FT_PSK)
221 RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_FT_PSK);
222 else if (sm->key_mgmt == WPA_KEY_MGMT_FT_SAE)
223 RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_FT_SAE);
225 else if (sm->key_mgmt == WPA_KEY_MGMT_FT_FILS_SHA256)
226 RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_FT_FILS_SHA256);
227 else if (sm->key_mgmt == WPA_KEY_MGMT_FT_FILS_SHA384)
228 RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_FT_FILS_SHA384);
229 #endif /* CONFIG_FILS */
231 wpa_printf(MSG_WARNING, "FT: Invalid key management type (%d)",
236 pos += RSN_SELECTOR_LEN;
238 /* RSN Capabilities */
240 #ifdef CONFIG_IEEE80211W
241 if (sm->mgmt_group_cipher == WPA_CIPHER_AES_128_CMAC ||
242 sm->mgmt_group_cipher == WPA_CIPHER_BIP_GMAC_128 ||
243 sm->mgmt_group_cipher == WPA_CIPHER_BIP_GMAC_256 ||
244 sm->mgmt_group_cipher == WPA_CIPHER_BIP_CMAC_256)
245 capab |= WPA_CAPABILITY_MFPC;
246 #endif /* CONFIG_IEEE80211W */
248 capab |= WPA_CAPABILITY_OCVC;
249 WPA_PUT_LE16(pos, capab);
253 WPA_PUT_LE16(pos, 1);
256 /* PMKID List [PMKR0Name/PMKR1Name] */
257 os_memcpy(pos, pmk_name, WPA_PMK_NAME_LEN);
258 pos += WPA_PMK_NAME_LEN;
260 #ifdef CONFIG_IEEE80211W
261 /* Management Group Cipher Suite */
262 switch (sm->mgmt_group_cipher) {
263 case WPA_CIPHER_AES_128_CMAC:
264 RSN_SELECTOR_PUT(pos, RSN_CIPHER_SUITE_AES_128_CMAC);
265 pos += RSN_SELECTOR_LEN;
267 case WPA_CIPHER_BIP_GMAC_128:
268 RSN_SELECTOR_PUT(pos, RSN_CIPHER_SUITE_BIP_GMAC_128);
269 pos += RSN_SELECTOR_LEN;
271 case WPA_CIPHER_BIP_GMAC_256:
272 RSN_SELECTOR_PUT(pos, RSN_CIPHER_SUITE_BIP_GMAC_256);
273 pos += RSN_SELECTOR_LEN;
275 case WPA_CIPHER_BIP_CMAC_256:
276 RSN_SELECTOR_PUT(pos, RSN_CIPHER_SUITE_BIP_CMAC_256);
277 pos += RSN_SELECTOR_LEN;
280 #endif /* CONFIG_IEEE80211W */
282 rsnie->len = (pos - (u8 *) rsnie) - 2;
285 mdie_len = wpa_ft_add_mdie(sm, pos, buf_len - (pos - buf), ap_mdie);
290 mdie = (struct rsn_mdie *) (pos + 2);
293 /* FTIE[SNonce, [R1KH-ID,] R0KH-ID ] */
295 *pos++ = WLAN_EID_FAST_BSS_TRANSITION;
297 if (wpa_key_mgmt_sha384(sm->key_mgmt)) {
298 struct rsn_ftie_sha384 *ftie;
300 ftie = (struct rsn_ftie_sha384 *) pos;
302 elem_count = &ftie->mic_control[1];
303 pos += sizeof(*ftie);
304 os_memcpy(ftie->snonce, sm->snonce, WPA_NONCE_LEN);
306 os_memcpy(ftie->anonce, anonce, WPA_NONCE_LEN);
308 struct rsn_ftie *ftie;
310 ftie = (struct rsn_ftie *) pos;
312 elem_count = &ftie->mic_control[1];
313 pos += sizeof(*ftie);
314 os_memcpy(ftie->snonce, sm->snonce, WPA_NONCE_LEN);
316 os_memcpy(ftie->anonce, anonce, WPA_NONCE_LEN);
319 /* R1KH-ID sub-element in third FT message */
320 *pos++ = FTIE_SUBELEM_R1KH_ID;
321 *pos++ = FT_R1KH_ID_LEN;
322 os_memcpy(pos, sm->r1kh_id, FT_R1KH_ID_LEN);
323 pos += FT_R1KH_ID_LEN;
325 /* R0KH-ID sub-element */
326 *pos++ = FTIE_SUBELEM_R0KH_ID;
327 *pos++ = sm->r0kh_id_len;
328 os_memcpy(pos, sm->r0kh_id, sm->r0kh_id_len);
329 pos += sm->r0kh_id_len;
331 if (kck && wpa_sm_ocv_enabled(sm)) {
332 /* OCI sub-element in the third FT message */
333 struct wpa_channel_info ci;
335 if (wpa_sm_channel_info(sm, &ci) != 0) {
336 wpa_printf(MSG_WARNING,
337 "Failed to get channel info for OCI element in FTE");
342 *pos++ = FTIE_SUBELEM_OCI;
343 *pos++ = OCV_OCI_LEN;
344 if (ocv_insert_oci(&ci, &pos) < 0) {
349 #endif /* CONFIG_OCV */
350 *ftie_len = pos - ftie_len - 1;
354 os_memcpy(pos, ric_ies, ric_ies_len);
360 * IEEE Std 802.11r-2008, 11A.8.4
361 * MIC shall be calculated over:
362 * non-AP STA MAC address
363 * Target AP MAC address
364 * Transaction seq number (5 for ReassocReq, 3 otherwise)
367 * FTIE (with MIC field set to 0)
368 * RIC-Request (if present)
370 /* Information element count */
371 *elem_count = 3 + ieee802_11_ie_count(ric_ies, ric_ies_len);
372 if (wpa_ft_mic(kck, kck_len, sm->own_addr, target_ap, 5,
373 ((u8 *) mdie) - 2, 2 + sizeof(*mdie),
374 ftie_pos, 2 + *ftie_len,
375 (u8 *) rsnie, 2 + rsnie->len, ric_ies,
376 ric_ies_len, fte_mic) < 0) {
377 wpa_printf(MSG_INFO, "FT: Failed to calculate MIC");
389 static int wpa_ft_install_ptk(struct wpa_sm *sm, const u8 *bssid)
393 u8 null_rsc[6] = { 0, 0, 0, 0, 0, 0 };
395 wpa_printf(MSG_DEBUG, "FT: Installing PTK to the driver.");
397 if (!wpa_cipher_valid_pairwise(sm->pairwise_cipher)) {
398 wpa_printf(MSG_WARNING, "FT: Unsupported pairwise cipher %d",
399 sm->pairwise_cipher);
403 alg = wpa_cipher_to_alg(sm->pairwise_cipher);
404 keylen = wpa_cipher_key_len(sm->pairwise_cipher);
406 if (wpa_sm_set_key(sm, alg, bssid, 0, 1, null_rsc,
407 sizeof(null_rsc), (u8 *) sm->ptk.tk, keylen) < 0) {
408 wpa_printf(MSG_WARNING, "FT: Failed to set PTK to the driver");
417 * wpa_ft_prepare_auth_request - Generate over-the-air auth request
418 * @sm: Pointer to WPA state machine data from wpa_sm_init()
419 * @mdie: Target AP MDIE
420 * Returns: 0 on success, -1 on failure
422 int wpa_ft_prepare_auth_request(struct wpa_sm *sm, const u8 *mdie)
427 /* Generate a new SNonce */
428 if (random_get_bytes(sm->snonce, WPA_NONCE_LEN)) {
429 wpa_printf(MSG_INFO, "FT: Failed to generate a new SNonce");
433 ft_ies = wpa_ft_gen_req_ies(sm, &ft_ies_len, NULL, sm->pmk_r0_name,
434 NULL, 0, sm->bssid, NULL, 0, mdie);
436 wpa_sm_update_ft_ies(sm, sm->mobility_domain,
445 int wpa_ft_add_mdie(struct wpa_sm *sm, u8 *buf, size_t buf_len,
449 struct rsn_mdie *mdie;
451 if (buf_len < 2 + sizeof(*mdie)) {
453 "FT: Failed to add MDIE: short buffer, length=%zu",
458 *pos++ = WLAN_EID_MOBILITY_DOMAIN;
459 *pos++ = sizeof(*mdie);
460 mdie = (struct rsn_mdie *) pos;
461 os_memcpy(mdie->mobility_domain, sm->mobility_domain,
462 MOBILITY_DOMAIN_ID_LEN);
463 mdie->ft_capab = ap_mdie && ap_mdie[1] >= 3 ? ap_mdie[4] :
466 return 2 + sizeof(*mdie);
470 const u8 * wpa_sm_get_ft_md(struct wpa_sm *sm)
472 return sm->mobility_domain;
476 int wpa_ft_process_response(struct wpa_sm *sm, const u8 *ies, size_t ies_len,
477 int ft_action, const u8 *target_ap,
478 const u8 *ric_ies, size_t ric_ies_len)
482 struct wpa_ft_ies parse;
483 struct rsn_mdie *mdie;
484 u8 ptk_name[WPA_PMK_NAME_LEN];
489 int use_sha384 = wpa_key_mgmt_sha384(sm->key_mgmt);
490 const u8 *anonce, *snonce;
492 wpa_hexdump(MSG_DEBUG, "FT: Response IEs", ies, ies_len);
493 wpa_hexdump(MSG_DEBUG, "FT: RIC IEs", ric_ies, ric_ies_len);
496 if (!sm->over_the_ds_in_progress) {
497 wpa_printf(MSG_DEBUG, "FT: No over-the-DS in progress "
498 "- drop FT Action Response");
502 if (os_memcmp(target_ap, sm->target_ap, ETH_ALEN) != 0) {
503 wpa_printf(MSG_DEBUG, "FT: No over-the-DS in progress "
504 "with this Target AP - drop FT Action "
510 if (!wpa_key_mgmt_ft(sm->key_mgmt)) {
511 wpa_printf(MSG_DEBUG, "FT: Reject FT IEs since FT is not "
512 "enabled for this connection");
516 if (wpa_ft_parse_ies(ies, ies_len, &parse, use_sha384) < 0) {
517 wpa_printf(MSG_DEBUG, "FT: Failed to parse IEs");
521 mdie = (struct rsn_mdie *) parse.mdie;
522 if (mdie == NULL || parse.mdie_len < sizeof(*mdie) ||
523 os_memcmp(mdie->mobility_domain, sm->mobility_domain,
524 MOBILITY_DOMAIN_ID_LEN) != 0) {
525 wpa_printf(MSG_DEBUG, "FT: Invalid MDIE");
530 struct rsn_ftie_sha384 *ftie;
532 ftie = (struct rsn_ftie_sha384 *) parse.ftie;
533 if (!ftie || parse.ftie_len < sizeof(*ftie)) {
534 wpa_printf(MSG_DEBUG, "FT: Invalid FTIE");
538 anonce = ftie->anonce;
539 snonce = ftie->snonce;
541 struct rsn_ftie *ftie;
543 ftie = (struct rsn_ftie *) parse.ftie;
544 if (!ftie || parse.ftie_len < sizeof(*ftie)) {
545 wpa_printf(MSG_DEBUG, "FT: Invalid FTIE");
549 anonce = ftie->anonce;
550 snonce = ftie->snonce;
553 if (os_memcmp(snonce, sm->snonce, WPA_NONCE_LEN) != 0) {
554 wpa_printf(MSG_DEBUG, "FT: SNonce mismatch in FTIE");
555 wpa_hexdump(MSG_DEBUG, "FT: Received SNonce",
556 snonce, WPA_NONCE_LEN);
557 wpa_hexdump(MSG_DEBUG, "FT: Expected SNonce",
558 sm->snonce, WPA_NONCE_LEN);
562 if (parse.r0kh_id == NULL) {
563 wpa_printf(MSG_DEBUG, "FT: No R0KH-ID subelem in FTIE");
567 if (parse.r0kh_id_len != sm->r0kh_id_len ||
568 os_memcmp_const(parse.r0kh_id, sm->r0kh_id, parse.r0kh_id_len) != 0)
570 wpa_printf(MSG_DEBUG, "FT: R0KH-ID in FTIE did not match with "
571 "the current R0KH-ID");
572 wpa_hexdump(MSG_DEBUG, "FT: R0KH-ID in FTIE",
573 parse.r0kh_id, parse.r0kh_id_len);
574 wpa_hexdump(MSG_DEBUG, "FT: The current R0KH-ID",
575 sm->r0kh_id, sm->r0kh_id_len);
579 if (parse.r1kh_id == NULL) {
580 wpa_printf(MSG_DEBUG, "FT: No R1KH-ID subelem in FTIE");
584 if (parse.rsn_pmkid == NULL ||
585 os_memcmp_const(parse.rsn_pmkid, sm->pmk_r0_name, WPA_PMK_NAME_LEN))
587 wpa_printf(MSG_DEBUG, "FT: No matching PMKR0Name (PMKID) in "
592 os_memcpy(sm->r1kh_id, parse.r1kh_id, FT_R1KH_ID_LEN);
593 wpa_hexdump(MSG_DEBUG, "FT: R1KH-ID", sm->r1kh_id, FT_R1KH_ID_LEN);
594 wpa_hexdump(MSG_DEBUG, "FT: SNonce", sm->snonce, WPA_NONCE_LEN);
595 wpa_hexdump(MSG_DEBUG, "FT: ANonce", anonce, WPA_NONCE_LEN);
596 os_memcpy(sm->anonce, anonce, WPA_NONCE_LEN);
597 if (wpa_derive_pmk_r1(sm->pmk_r0, sm->pmk_r0_len, sm->pmk_r0_name,
598 sm->r1kh_id, sm->own_addr, sm->pmk_r1,
599 sm->pmk_r1_name) < 0)
601 sm->pmk_r1_len = sm->pmk_r0_len;
602 wpa_hexdump_key(MSG_DEBUG, "FT: PMK-R1", sm->pmk_r1, sm->pmk_r1_len);
603 wpa_hexdump(MSG_DEBUG, "FT: PMKR1Name",
604 sm->pmk_r1_name, WPA_PMK_NAME_LEN);
607 if (wpa_pmk_r1_to_ptk(sm->pmk_r1, sm->pmk_r1_len, sm->snonce,
608 anonce, sm->own_addr, bssid,
609 sm->pmk_r1_name, &sm->ptk, ptk_name, sm->key_mgmt,
610 sm->pairwise_cipher) < 0)
613 if (wpa_key_mgmt_fils(sm->key_mgmt)) {
615 kck_len = sm->ptk.kck2_len;
618 kck_len = sm->ptk.kck_len;
620 ft_ies = wpa_ft_gen_req_ies(sm, &ft_ies_len, anonce,
623 ric_ies, ric_ies_len,
624 parse.mdie ? parse.mdie - 2 : NULL);
626 wpa_sm_update_ft_ies(sm, sm->mobility_domain,
631 wpa_sm_mark_authenticated(sm, bssid);
632 ret = wpa_ft_install_ptk(sm, bssid);
635 * Some drivers do not support key configuration when we are
636 * not associated with the target AP. Work around this by
637 * trying again after the following reassociation gets
640 wpa_printf(MSG_DEBUG, "FT: Failed to set PTK prior to "
641 "association - try again after reassociation");
642 sm->set_ptk_after_assoc = 1;
644 sm->set_ptk_after_assoc = 0;
646 sm->ft_completed = 1;
649 * The caller is expected trigger re-association with the
652 os_memcpy(sm->bssid, target_ap, ETH_ALEN);
659 int wpa_ft_is_completed(struct wpa_sm *sm)
664 if (!wpa_key_mgmt_ft(sm->key_mgmt))
667 return sm->ft_completed;
671 void wpa_reset_ft_completed(struct wpa_sm *sm)
674 sm->ft_completed = 0;
678 static int wpa_ft_process_gtk_subelem(struct wpa_sm *sm, const u8 *gtk_elem,
684 size_t gtk_len, keylen, rsc_len;
688 if (wpa_key_mgmt_fils(sm->key_mgmt)) {
690 kek_len = sm->ptk.kek2_len;
693 kek_len = sm->ptk.kek_len;
696 if (gtk_elem == NULL) {
697 wpa_printf(MSG_DEBUG, "FT: No GTK included in FTIE");
701 wpa_hexdump_key(MSG_DEBUG, "FT: Received GTK in Reassoc Resp",
702 gtk_elem, gtk_elem_len);
704 if (gtk_elem_len < 11 + 24 || (gtk_elem_len - 11) % 8 ||
705 gtk_elem_len - 19 > sizeof(gtk)) {
706 wpa_printf(MSG_DEBUG, "FT: Invalid GTK sub-elem "
707 "length %lu", (unsigned long) gtk_elem_len);
710 gtk_len = gtk_elem_len - 19;
711 if (aes_unwrap(kek, kek_len, gtk_len / 8, gtk_elem + 11, gtk)) {
712 wpa_printf(MSG_WARNING, "FT: AES unwrap failed - could not "
717 keylen = wpa_cipher_key_len(sm->group_cipher);
718 rsc_len = wpa_cipher_rsc_len(sm->group_cipher);
719 alg = wpa_cipher_to_alg(sm->group_cipher);
720 if (alg == WPA_ALG_NONE) {
721 wpa_printf(MSG_WARNING, "WPA: Unsupported Group Cipher %d",
726 if (gtk_len < keylen) {
727 wpa_printf(MSG_DEBUG, "FT: Too short GTK in FTIE");
731 /* Key Info[2] | Key Length[1] | RSC[8] | Key[5..32]. */
733 keyidx = WPA_GET_LE16(gtk_elem) & 0x03;
735 if (gtk_elem[2] != keylen) {
736 wpa_printf(MSG_DEBUG, "FT: GTK length mismatch: received %d "
738 gtk_elem[2], (unsigned long) keylen);
742 wpa_hexdump_key(MSG_DEBUG, "FT: GTK from Reassoc Resp", gtk, keylen);
743 if (sm->group_cipher == WPA_CIPHER_TKIP) {
744 /* Swap Tx/Rx keys for Michael MIC */
746 os_memcpy(tmp, gtk + 16, 8);
747 os_memcpy(gtk + 16, gtk + 24, 8);
748 os_memcpy(gtk + 24, tmp, 8);
750 if (wpa_sm_set_key(sm, alg, broadcast_ether_addr, keyidx, 0,
751 gtk_elem + 3, rsc_len, gtk, keylen) < 0) {
752 wpa_printf(MSG_WARNING, "WPA: Failed to set GTK to the "
761 #ifdef CONFIG_IEEE80211W
762 static int wpa_ft_process_igtk_subelem(struct wpa_sm *sm, const u8 *igtk_elem,
763 size_t igtk_elem_len)
765 u8 igtk[WPA_IGTK_MAX_LEN];
771 if (wpa_key_mgmt_fils(sm->key_mgmt)) {
773 kek_len = sm->ptk.kek2_len;
776 kek_len = sm->ptk.kek_len;
779 if (sm->mgmt_group_cipher != WPA_CIPHER_AES_128_CMAC &&
780 sm->mgmt_group_cipher != WPA_CIPHER_BIP_GMAC_128 &&
781 sm->mgmt_group_cipher != WPA_CIPHER_BIP_GMAC_256 &&
782 sm->mgmt_group_cipher != WPA_CIPHER_BIP_CMAC_256)
785 if (igtk_elem == NULL) {
786 wpa_printf(MSG_DEBUG, "FT: No IGTK included in FTIE");
790 wpa_hexdump_key(MSG_DEBUG, "FT: Received IGTK in Reassoc Resp",
791 igtk_elem, igtk_elem_len);
793 igtk_len = wpa_cipher_key_len(sm->mgmt_group_cipher);
794 if (igtk_elem_len != 2 + 6 + 1 + igtk_len + 8) {
795 wpa_printf(MSG_DEBUG, "FT: Invalid IGTK sub-elem "
796 "length %lu", (unsigned long) igtk_elem_len);
799 if (igtk_elem[8] != igtk_len) {
800 wpa_printf(MSG_DEBUG, "FT: Invalid IGTK sub-elem Key Length "
805 if (aes_unwrap(kek, kek_len, igtk_len / 8, igtk_elem + 9, igtk)) {
806 wpa_printf(MSG_WARNING, "FT: AES unwrap failed - could not "
811 /* KeyID[2] | IPN[6] | Key Length[1] | Key[16+8] */
813 keyidx = WPA_GET_LE16(igtk_elem);
815 wpa_hexdump_key(MSG_DEBUG, "FT: IGTK from Reassoc Resp", igtk,
817 if (wpa_sm_set_key(sm, wpa_cipher_to_alg(sm->mgmt_group_cipher),
818 broadcast_ether_addr, keyidx, 0,
819 igtk_elem + 2, 6, igtk, igtk_len) < 0) {
820 wpa_printf(MSG_WARNING, "WPA: Failed to set IGTK to the "
822 os_memset(igtk, 0, sizeof(igtk));
825 os_memset(igtk, 0, sizeof(igtk));
829 #endif /* CONFIG_IEEE80211W */
832 int wpa_ft_validate_reassoc_resp(struct wpa_sm *sm, const u8 *ies,
833 size_t ies_len, const u8 *src_addr)
835 struct wpa_ft_ies parse;
836 struct rsn_mdie *mdie;
838 u8 mic[WPA_EAPOL_KEY_MIC_MAX_LEN];
841 int use_sha384 = wpa_key_mgmt_sha384(sm->key_mgmt);
842 const u8 *anonce, *snonce, *fte_mic;
845 wpa_hexdump(MSG_DEBUG, "FT: Response IEs", ies, ies_len);
847 if (!wpa_key_mgmt_ft(sm->key_mgmt)) {
848 wpa_printf(MSG_DEBUG, "FT: Reject FT IEs since FT is not "
849 "enabled for this connection");
853 if (sm->ft_reassoc_completed) {
854 wpa_printf(MSG_DEBUG, "FT: Reassociation has already been completed for this FT protocol instance - ignore unexpected retransmission");
858 if (wpa_ft_parse_ies(ies, ies_len, &parse, use_sha384) < 0) {
859 wpa_printf(MSG_DEBUG, "FT: Failed to parse IEs");
863 mdie = (struct rsn_mdie *) parse.mdie;
864 if (mdie == NULL || parse.mdie_len < sizeof(*mdie) ||
865 os_memcmp(mdie->mobility_domain, sm->mobility_domain,
866 MOBILITY_DOMAIN_ID_LEN) != 0) {
867 wpa_printf(MSG_DEBUG, "FT: Invalid MDIE");
872 struct rsn_ftie_sha384 *ftie;
874 ftie = (struct rsn_ftie_sha384 *) parse.ftie;
875 if (!ftie || parse.ftie_len < sizeof(*ftie)) {
876 wpa_printf(MSG_DEBUG, "FT: Invalid FTIE");
880 anonce = ftie->anonce;
881 snonce = ftie->snonce;
882 fte_elem_count = ftie->mic_control[1];
885 struct rsn_ftie *ftie;
887 ftie = (struct rsn_ftie *) parse.ftie;
888 if (!ftie || parse.ftie_len < sizeof(*ftie)) {
889 wpa_printf(MSG_DEBUG, "FT: Invalid FTIE");
893 anonce = ftie->anonce;
894 snonce = ftie->snonce;
895 fte_elem_count = ftie->mic_control[1];
899 if (os_memcmp(snonce, sm->snonce, WPA_NONCE_LEN) != 0) {
900 wpa_printf(MSG_DEBUG, "FT: SNonce mismatch in FTIE");
901 wpa_hexdump(MSG_DEBUG, "FT: Received SNonce",
902 snonce, WPA_NONCE_LEN);
903 wpa_hexdump(MSG_DEBUG, "FT: Expected SNonce",
904 sm->snonce, WPA_NONCE_LEN);
908 if (os_memcmp(anonce, sm->anonce, WPA_NONCE_LEN) != 0) {
909 wpa_printf(MSG_DEBUG, "FT: ANonce mismatch in FTIE");
910 wpa_hexdump(MSG_DEBUG, "FT: Received ANonce",
911 anonce, WPA_NONCE_LEN);
912 wpa_hexdump(MSG_DEBUG, "FT: Expected ANonce",
913 sm->anonce, WPA_NONCE_LEN);
917 if (parse.r0kh_id == NULL) {
918 wpa_printf(MSG_DEBUG, "FT: No R0KH-ID subelem in FTIE");
922 if (parse.r0kh_id_len != sm->r0kh_id_len ||
923 os_memcmp_const(parse.r0kh_id, sm->r0kh_id, parse.r0kh_id_len) != 0)
925 wpa_printf(MSG_DEBUG, "FT: R0KH-ID in FTIE did not match with "
926 "the current R0KH-ID");
927 wpa_hexdump(MSG_DEBUG, "FT: R0KH-ID in FTIE",
928 parse.r0kh_id, parse.r0kh_id_len);
929 wpa_hexdump(MSG_DEBUG, "FT: The current R0KH-ID",
930 sm->r0kh_id, sm->r0kh_id_len);
934 if (parse.r1kh_id == NULL) {
935 wpa_printf(MSG_DEBUG, "FT: No R1KH-ID subelem in FTIE");
939 if (os_memcmp_const(parse.r1kh_id, sm->r1kh_id, FT_R1KH_ID_LEN) != 0) {
940 wpa_printf(MSG_DEBUG, "FT: Unknown R1KH-ID used in "
945 if (parse.rsn_pmkid == NULL ||
946 os_memcmp_const(parse.rsn_pmkid, sm->pmk_r1_name, WPA_PMK_NAME_LEN))
948 wpa_printf(MSG_DEBUG, "FT: No matching PMKR1Name (PMKID) in "
949 "RSNIE (pmkid=%d)", !!parse.rsn_pmkid);
955 count += ieee802_11_ie_count(parse.ric, parse.ric_len);
956 if (fte_elem_count != count) {
957 wpa_printf(MSG_DEBUG, "FT: Unexpected IE count in MIC "
958 "Control: received %u expected %u",
959 fte_elem_count, count);
963 if (wpa_key_mgmt_fils(sm->key_mgmt)) {
965 kck_len = sm->ptk.kck2_len;
968 kck_len = sm->ptk.kck_len;
971 if (wpa_ft_mic(kck, kck_len, sm->own_addr, src_addr, 6,
972 parse.mdie - 2, parse.mdie_len + 2,
973 parse.ftie - 2, parse.ftie_len + 2,
974 parse.rsn - 2, parse.rsn_len + 2,
975 parse.ric, parse.ric_len,
977 wpa_printf(MSG_DEBUG, "FT: Failed to calculate MIC");
981 if (os_memcmp_const(mic, fte_mic, 16) != 0) {
982 wpa_printf(MSG_DEBUG, "FT: Invalid MIC in FTIE");
983 wpa_hexdump(MSG_MSGDUMP, "FT: Received MIC", fte_mic, 16);
984 wpa_hexdump(MSG_MSGDUMP, "FT: Calculated MIC", mic, 16);
989 if (wpa_sm_ocv_enabled(sm)) {
990 struct wpa_channel_info ci;
992 if (wpa_sm_channel_info(sm, &ci) != 0) {
993 wpa_printf(MSG_WARNING,
994 "Failed to get channel info to validate received OCI in (Re)Assoc Response");
998 if (ocv_verify_tx_params(parse.oci, parse.oci_len, &ci,
999 channel_width_to_int(ci.chanwidth),
1000 ci.seg1_idx) != 0) {
1001 wpa_printf(MSG_WARNING, "%s", ocv_errorstr);
1005 #endif /* CONFIG_OCV */
1007 sm->ft_reassoc_completed = 1;
1009 if (wpa_ft_process_gtk_subelem(sm, parse.gtk, parse.gtk_len) < 0)
1012 #ifdef CONFIG_IEEE80211W
1013 if (wpa_ft_process_igtk_subelem(sm, parse.igtk, parse.igtk_len) < 0)
1015 #endif /* CONFIG_IEEE80211W */
1017 if (sm->set_ptk_after_assoc) {
1018 wpa_printf(MSG_DEBUG, "FT: Try to set PTK again now that we "
1020 if (wpa_ft_install_ptk(sm, src_addr) < 0)
1022 sm->set_ptk_after_assoc = 0;
1026 wpa_hexdump(MSG_MSGDUMP, "FT: RIC Response",
1027 parse.ric, parse.ric_len);
1028 /* TODO: parse response and inform driver about results when
1029 * using wpa_supplicant SME */
1032 wpa_printf(MSG_DEBUG, "FT: Completed successfully");
1039 * wpa_ft_start_over_ds - Generate over-the-DS auth request
1040 * @sm: Pointer to WPA state machine data from wpa_sm_init()
1041 * @target_ap: Target AP Address
1042 * @mdie: Mobility Domain IE from the target AP
1043 * Returns: 0 on success, -1 on failure
1045 int wpa_ft_start_over_ds(struct wpa_sm *sm, const u8 *target_ap,
1051 wpa_printf(MSG_DEBUG, "FT: Request over-the-DS with " MACSTR,
1052 MAC2STR(target_ap));
1054 /* Generate a new SNonce */
1055 if (random_get_bytes(sm->snonce, WPA_NONCE_LEN)) {
1056 wpa_printf(MSG_INFO, "FT: Failed to generate a new SNonce");
1060 ft_ies = wpa_ft_gen_req_ies(sm, &ft_ies_len, NULL, sm->pmk_r0_name,
1061 NULL, 0, target_ap, NULL, 0, mdie);
1063 sm->over_the_ds_in_progress = 1;
1064 os_memcpy(sm->target_ap, target_ap, ETH_ALEN);
1065 wpa_sm_send_ft_action(sm, 1, target_ap, ft_ies, ft_ies_len);
1072 #endif /* CONFIG_IEEE80211R */