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"
20 #ifdef CONFIG_IEEE80211R
22 int wpa_derive_ptk_ft(struct wpa_sm *sm, const unsigned char *src_addr,
23 const struct wpa_eapol_key *key, struct wpa_ptk *ptk)
25 u8 ptk_name[WPA_PMK_NAME_LEN];
26 const u8 *anonce = key->key_nonce;
27 int use_sha384 = wpa_key_mgmt_sha384(sm->key_mgmt);
29 if (sm->xxkey_len == 0) {
30 wpa_printf(MSG_DEBUG, "FT: XXKey not available for key "
35 sm->pmk_r0_len = use_sha384 ? SHA384_MAC_LEN : PMK_LEN;
36 if (wpa_derive_pmk_r0(sm->xxkey, sm->xxkey_len, sm->ssid,
37 sm->ssid_len, sm->mobility_domain,
38 sm->r0kh_id, sm->r0kh_id_len, sm->own_addr,
39 sm->pmk_r0, sm->pmk_r0_name, use_sha384) < 0)
41 wpa_hexdump_key(MSG_DEBUG, "FT: PMK-R0", sm->pmk_r0, sm->pmk_r0_len);
42 wpa_hexdump(MSG_DEBUG, "FT: PMKR0Name",
43 sm->pmk_r0_name, WPA_PMK_NAME_LEN);
44 sm->pmk_r1_len = sm->pmk_r0_len;
45 if (wpa_derive_pmk_r1(sm->pmk_r0, sm->pmk_r0_len, sm->pmk_r0_name,
46 sm->r1kh_id, sm->own_addr, sm->pmk_r1,
49 wpa_hexdump_key(MSG_DEBUG, "FT: PMK-R1", sm->pmk_r1, sm->pmk_r1_len);
50 wpa_hexdump(MSG_DEBUG, "FT: PMKR1Name", sm->pmk_r1_name,
52 return wpa_pmk_r1_to_ptk(sm->pmk_r1, sm->pmk_r1_len, sm->snonce, anonce,
53 sm->own_addr, sm->bssid, sm->pmk_r1_name, ptk,
54 ptk_name, sm->key_mgmt, sm->pairwise_cipher);
59 * wpa_sm_set_ft_params - Set FT (IEEE 802.11r) parameters
60 * @sm: Pointer to WPA state machine data from wpa_sm_init()
61 * @ies: Association Response IEs or %NULL to clear FT parameters
62 * @ies_len: Length of ies buffer in octets
63 * Returns: 0 on success, -1 on failure
65 int wpa_sm_set_ft_params(struct wpa_sm *sm, const u8 *ies, size_t ies_len)
73 use_sha384 = wpa_key_mgmt_sha384(sm->key_mgmt);
74 if (wpa_ft_parse_ies(ies, ies_len, &ft, use_sha384) < 0)
77 if (ft.mdie && ft.mdie_len < MOBILITY_DOMAIN_ID_LEN + 1)
81 wpa_hexdump(MSG_DEBUG, "FT: Mobility domain",
82 ft.mdie, MOBILITY_DOMAIN_ID_LEN);
83 os_memcpy(sm->mobility_domain, ft.mdie,
84 MOBILITY_DOMAIN_ID_LEN);
85 sm->mdie_ft_capab = ft.mdie[MOBILITY_DOMAIN_ID_LEN];
86 wpa_printf(MSG_DEBUG, "FT: Capability and Policy: 0x%02x",
89 os_memset(sm->mobility_domain, 0, MOBILITY_DOMAIN_ID_LEN);
92 wpa_hexdump(MSG_DEBUG, "FT: R0KH-ID",
93 ft.r0kh_id, ft.r0kh_id_len);
94 os_memcpy(sm->r0kh_id, ft.r0kh_id, ft.r0kh_id_len);
95 sm->r0kh_id_len = ft.r0kh_id_len;
97 /* FIX: When should R0KH-ID be cleared? We need to keep the
98 * old R0KH-ID in order to be able to use this during FT. */
100 * os_memset(sm->r0kh_id, 0, FT_R0KH_ID_LEN);
101 * sm->r0kh_id_len = 0;
106 wpa_hexdump(MSG_DEBUG, "FT: R1KH-ID",
107 ft.r1kh_id, FT_R1KH_ID_LEN);
108 os_memcpy(sm->r1kh_id, ft.r1kh_id, FT_R1KH_ID_LEN);
110 os_memset(sm->r1kh_id, 0, FT_R1KH_ID_LEN);
112 os_free(sm->assoc_resp_ies);
113 sm->assoc_resp_ies = os_malloc(ft.mdie_len + 2 + ft.ftie_len + 2);
114 if (sm->assoc_resp_ies) {
115 u8 *pos = sm->assoc_resp_ies;
117 os_memcpy(pos, ft.mdie - 2, ft.mdie_len + 2);
118 pos += ft.mdie_len + 2;
121 os_memcpy(pos, ft.ftie - 2, ft.ftie_len + 2);
122 pos += ft.ftie_len + 2;
124 sm->assoc_resp_ies_len = pos - sm->assoc_resp_ies;
125 wpa_hexdump(MSG_DEBUG, "FT: Stored MDIE and FTIE from "
126 "(Re)Association Response",
127 sm->assoc_resp_ies, sm->assoc_resp_ies_len);
135 * wpa_ft_gen_req_ies - Generate FT (IEEE 802.11r) IEs for Auth/ReAssoc Request
136 * @sm: Pointer to WPA state machine data from wpa_sm_init()
137 * @len: Buffer for returning the length of the IEs
138 * @anonce: ANonce or %NULL if not yet available
139 * @pmk_name: PMKR0Name or PMKR1Name to be added into the RSN IE PMKID List
140 * @kck: 128-bit KCK for MIC or %NULL if no MIC is used
141 * @kck_len: KCK length in octets
142 * @target_ap: Target AP address
143 * @ric_ies: Optional IE(s), e.g., WMM TSPEC(s), for RIC-Request or %NULL
144 * @ric_ies_len: Length of ric_ies buffer in octets
145 * @ap_mdie: Mobility Domain IE from the target AP
146 * Returns: Pointer to buffer with IEs or %NULL on failure
148 * Caller is responsible for freeing the returned buffer with os_free();
150 static u8 * wpa_ft_gen_req_ies(struct wpa_sm *sm, size_t *len,
151 const u8 *anonce, const u8 *pmk_name,
152 const u8 *kck, size_t kck_len,
154 const u8 *ric_ies, size_t ric_ies_len,
158 u8 *buf, *pos, *ftie_len, *ftie_pos, *fte_mic, *elem_count;
159 struct rsn_mdie *mdie;
160 struct rsn_ie_hdr *rsnie;
164 sm->ft_completed = 0;
165 sm->ft_reassoc_completed = 0;
167 buf_len = 2 + sizeof(struct rsn_mdie) + 2 +
168 sizeof(struct rsn_ftie_sha384) +
169 2 + sm->r0kh_id_len + ric_ies_len + 100;
170 buf = os_zalloc(buf_len);
175 /* RSNIE[PMKR0Name/PMKR1Name] */
176 rsnie = (struct rsn_ie_hdr *) pos;
177 rsnie->elem_id = WLAN_EID_RSN;
178 WPA_PUT_LE16(rsnie->version, RSN_VERSION);
179 pos = (u8 *) (rsnie + 1);
181 /* Group Suite Selector */
182 if (!wpa_cipher_valid_group(sm->group_cipher)) {
183 wpa_printf(MSG_WARNING, "FT: Invalid group cipher (%d)",
188 RSN_SELECTOR_PUT(pos, wpa_cipher_to_suite(WPA_PROTO_RSN,
190 pos += RSN_SELECTOR_LEN;
192 /* Pairwise Suite Count */
193 WPA_PUT_LE16(pos, 1);
196 /* Pairwise Suite List */
197 if (!wpa_cipher_valid_pairwise(sm->pairwise_cipher)) {
198 wpa_printf(MSG_WARNING, "FT: Invalid pairwise cipher (%d)",
199 sm->pairwise_cipher);
203 RSN_SELECTOR_PUT(pos, wpa_cipher_to_suite(WPA_PROTO_RSN,
204 sm->pairwise_cipher));
205 pos += RSN_SELECTOR_LEN;
207 /* Authenticated Key Management Suite Count */
208 WPA_PUT_LE16(pos, 1);
211 /* Authenticated Key Management Suite List */
212 if (sm->key_mgmt == WPA_KEY_MGMT_FT_IEEE8021X)
213 RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_FT_802_1X);
215 else if (sm->key_mgmt == WPA_KEY_MGMT_FT_IEEE8021X_SHA384)
216 RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_FT_802_1X_SHA384);
217 #endif /* CONFIG_SHA384 */
218 else if (sm->key_mgmt == WPA_KEY_MGMT_FT_PSK)
219 RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_FT_PSK);
220 else if (sm->key_mgmt == WPA_KEY_MGMT_FT_SAE)
221 RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_FT_SAE);
223 else if (sm->key_mgmt == WPA_KEY_MGMT_FT_FILS_SHA256)
224 RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_FT_FILS_SHA256);
225 else if (sm->key_mgmt == WPA_KEY_MGMT_FT_FILS_SHA384)
226 RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_FT_FILS_SHA384);
227 #endif /* CONFIG_FILS */
229 wpa_printf(MSG_WARNING, "FT: Invalid key management type (%d)",
234 pos += RSN_SELECTOR_LEN;
236 /* RSN Capabilities */
238 #ifdef CONFIG_IEEE80211W
239 if (sm->mgmt_group_cipher == WPA_CIPHER_AES_128_CMAC ||
240 sm->mgmt_group_cipher == WPA_CIPHER_BIP_GMAC_128 ||
241 sm->mgmt_group_cipher == WPA_CIPHER_BIP_GMAC_256 ||
242 sm->mgmt_group_cipher == WPA_CIPHER_BIP_CMAC_256)
243 capab |= WPA_CAPABILITY_MFPC;
244 #endif /* CONFIG_IEEE80211W */
245 WPA_PUT_LE16(pos, capab);
249 WPA_PUT_LE16(pos, 1);
252 /* PMKID List [PMKR0Name/PMKR1Name] */
253 os_memcpy(pos, pmk_name, WPA_PMK_NAME_LEN);
254 pos += WPA_PMK_NAME_LEN;
256 #ifdef CONFIG_IEEE80211W
257 /* Management Group Cipher Suite */
258 switch (sm->mgmt_group_cipher) {
259 case WPA_CIPHER_AES_128_CMAC:
260 RSN_SELECTOR_PUT(pos, RSN_CIPHER_SUITE_AES_128_CMAC);
261 pos += RSN_SELECTOR_LEN;
263 case WPA_CIPHER_BIP_GMAC_128:
264 RSN_SELECTOR_PUT(pos, RSN_CIPHER_SUITE_BIP_GMAC_128);
265 pos += RSN_SELECTOR_LEN;
267 case WPA_CIPHER_BIP_GMAC_256:
268 RSN_SELECTOR_PUT(pos, RSN_CIPHER_SUITE_BIP_GMAC_256);
269 pos += RSN_SELECTOR_LEN;
271 case WPA_CIPHER_BIP_CMAC_256:
272 RSN_SELECTOR_PUT(pos, RSN_CIPHER_SUITE_BIP_CMAC_256);
273 pos += RSN_SELECTOR_LEN;
276 #endif /* CONFIG_IEEE80211W */
278 rsnie->len = (pos - (u8 *) rsnie) - 2;
281 mdie_len = wpa_ft_add_mdie(sm, pos, buf_len - (pos - buf), ap_mdie);
286 mdie = (struct rsn_mdie *) (pos + 2);
289 /* FTIE[SNonce, [R1KH-ID,] R0KH-ID ] */
291 *pos++ = WLAN_EID_FAST_BSS_TRANSITION;
293 if (wpa_key_mgmt_sha384(sm->key_mgmt)) {
294 struct rsn_ftie_sha384 *ftie;
296 ftie = (struct rsn_ftie_sha384 *) pos;
298 elem_count = &ftie->mic_control[1];
299 pos += sizeof(*ftie);
300 os_memcpy(ftie->snonce, sm->snonce, WPA_NONCE_LEN);
302 os_memcpy(ftie->anonce, anonce, WPA_NONCE_LEN);
304 struct rsn_ftie *ftie;
306 ftie = (struct rsn_ftie *) pos;
308 elem_count = &ftie->mic_control[1];
309 pos += sizeof(*ftie);
310 os_memcpy(ftie->snonce, sm->snonce, WPA_NONCE_LEN);
312 os_memcpy(ftie->anonce, anonce, WPA_NONCE_LEN);
315 /* R1KH-ID sub-element in third FT message */
316 *pos++ = FTIE_SUBELEM_R1KH_ID;
317 *pos++ = FT_R1KH_ID_LEN;
318 os_memcpy(pos, sm->r1kh_id, FT_R1KH_ID_LEN);
319 pos += FT_R1KH_ID_LEN;
321 /* R0KH-ID sub-element */
322 *pos++ = FTIE_SUBELEM_R0KH_ID;
323 *pos++ = sm->r0kh_id_len;
324 os_memcpy(pos, sm->r0kh_id, sm->r0kh_id_len);
325 pos += sm->r0kh_id_len;
326 *ftie_len = pos - ftie_len - 1;
330 os_memcpy(pos, ric_ies, ric_ies_len);
336 * IEEE Std 802.11r-2008, 11A.8.4
337 * MIC shall be calculated over:
338 * non-AP STA MAC address
339 * Target AP MAC address
340 * Transaction seq number (5 for ReassocReq, 3 otherwise)
343 * FTIE (with MIC field set to 0)
344 * RIC-Request (if present)
346 /* Information element count */
347 *elem_count = 3 + ieee802_11_ie_count(ric_ies, ric_ies_len);
348 if (wpa_ft_mic(kck, kck_len, sm->own_addr, target_ap, 5,
349 ((u8 *) mdie) - 2, 2 + sizeof(*mdie),
350 ftie_pos, 2 + *ftie_len,
351 (u8 *) rsnie, 2 + rsnie->len, ric_ies,
352 ric_ies_len, fte_mic) < 0) {
353 wpa_printf(MSG_INFO, "FT: Failed to calculate MIC");
365 static int wpa_ft_install_ptk(struct wpa_sm *sm, const u8 *bssid)
369 u8 null_rsc[6] = { 0, 0, 0, 0, 0, 0 };
371 wpa_printf(MSG_DEBUG, "FT: Installing PTK to the driver.");
373 if (!wpa_cipher_valid_pairwise(sm->pairwise_cipher)) {
374 wpa_printf(MSG_WARNING, "FT: Unsupported pairwise cipher %d",
375 sm->pairwise_cipher);
379 alg = wpa_cipher_to_alg(sm->pairwise_cipher);
380 keylen = wpa_cipher_key_len(sm->pairwise_cipher);
382 if (wpa_sm_set_key(sm, alg, bssid, 0, 1, null_rsc,
383 sizeof(null_rsc), (u8 *) sm->ptk.tk, keylen) < 0) {
384 wpa_printf(MSG_WARNING, "FT: Failed to set PTK to the driver");
393 * wpa_ft_prepare_auth_request - Generate over-the-air auth request
394 * @sm: Pointer to WPA state machine data from wpa_sm_init()
395 * @mdie: Target AP MDIE
396 * Returns: 0 on success, -1 on failure
398 int wpa_ft_prepare_auth_request(struct wpa_sm *sm, const u8 *mdie)
403 /* Generate a new SNonce */
404 if (random_get_bytes(sm->snonce, WPA_NONCE_LEN)) {
405 wpa_printf(MSG_INFO, "FT: Failed to generate a new SNonce");
409 ft_ies = wpa_ft_gen_req_ies(sm, &ft_ies_len, NULL, sm->pmk_r0_name,
410 NULL, 0, sm->bssid, NULL, 0, mdie);
412 wpa_sm_update_ft_ies(sm, sm->mobility_domain,
421 int wpa_ft_add_mdie(struct wpa_sm *sm, u8 *buf, size_t buf_len,
425 struct rsn_mdie *mdie;
427 if (buf_len < 2 + sizeof(*mdie)) {
429 "FT: Failed to add MDIE: short buffer, length=%zu",
434 *pos++ = WLAN_EID_MOBILITY_DOMAIN;
435 *pos++ = sizeof(*mdie);
436 mdie = (struct rsn_mdie *) pos;
437 os_memcpy(mdie->mobility_domain, sm->mobility_domain,
438 MOBILITY_DOMAIN_ID_LEN);
439 mdie->ft_capab = ap_mdie && ap_mdie[1] >= 3 ? ap_mdie[4] :
442 return 2 + sizeof(*mdie);
446 const u8 * wpa_sm_get_ft_md(struct wpa_sm *sm)
448 return sm->mobility_domain;
452 int wpa_ft_process_response(struct wpa_sm *sm, const u8 *ies, size_t ies_len,
453 int ft_action, const u8 *target_ap,
454 const u8 *ric_ies, size_t ric_ies_len)
458 struct wpa_ft_ies parse;
459 struct rsn_mdie *mdie;
460 u8 ptk_name[WPA_PMK_NAME_LEN];
465 int use_sha384 = wpa_key_mgmt_sha384(sm->key_mgmt);
466 const u8 *anonce, *snonce;
468 wpa_hexdump(MSG_DEBUG, "FT: Response IEs", ies, ies_len);
469 wpa_hexdump(MSG_DEBUG, "FT: RIC IEs", ric_ies, ric_ies_len);
472 if (!sm->over_the_ds_in_progress) {
473 wpa_printf(MSG_DEBUG, "FT: No over-the-DS in progress "
474 "- drop FT Action Response");
478 if (os_memcmp(target_ap, sm->target_ap, ETH_ALEN) != 0) {
479 wpa_printf(MSG_DEBUG, "FT: No over-the-DS in progress "
480 "with this Target AP - drop FT Action "
486 if (!wpa_key_mgmt_ft(sm->key_mgmt)) {
487 wpa_printf(MSG_DEBUG, "FT: Reject FT IEs since FT is not "
488 "enabled for this connection");
492 if (wpa_ft_parse_ies(ies, ies_len, &parse, use_sha384) < 0) {
493 wpa_printf(MSG_DEBUG, "FT: Failed to parse IEs");
497 mdie = (struct rsn_mdie *) parse.mdie;
498 if (mdie == NULL || parse.mdie_len < sizeof(*mdie) ||
499 os_memcmp(mdie->mobility_domain, sm->mobility_domain,
500 MOBILITY_DOMAIN_ID_LEN) != 0) {
501 wpa_printf(MSG_DEBUG, "FT: Invalid MDIE");
506 struct rsn_ftie_sha384 *ftie;
508 ftie = (struct rsn_ftie_sha384 *) parse.ftie;
509 if (!ftie || parse.ftie_len < sizeof(*ftie)) {
510 wpa_printf(MSG_DEBUG, "FT: Invalid FTIE");
514 anonce = ftie->anonce;
515 snonce = ftie->snonce;
517 struct rsn_ftie *ftie;
519 ftie = (struct rsn_ftie *) parse.ftie;
520 if (!ftie || parse.ftie_len < sizeof(*ftie)) {
521 wpa_printf(MSG_DEBUG, "FT: Invalid FTIE");
525 anonce = ftie->anonce;
526 snonce = ftie->snonce;
529 if (os_memcmp(snonce, sm->snonce, WPA_NONCE_LEN) != 0) {
530 wpa_printf(MSG_DEBUG, "FT: SNonce mismatch in FTIE");
531 wpa_hexdump(MSG_DEBUG, "FT: Received SNonce",
532 snonce, WPA_NONCE_LEN);
533 wpa_hexdump(MSG_DEBUG, "FT: Expected SNonce",
534 sm->snonce, WPA_NONCE_LEN);
538 if (parse.r0kh_id == NULL) {
539 wpa_printf(MSG_DEBUG, "FT: No R0KH-ID subelem in FTIE");
543 if (parse.r0kh_id_len != sm->r0kh_id_len ||
544 os_memcmp_const(parse.r0kh_id, sm->r0kh_id, parse.r0kh_id_len) != 0)
546 wpa_printf(MSG_DEBUG, "FT: R0KH-ID in FTIE did not match with "
547 "the current R0KH-ID");
548 wpa_hexdump(MSG_DEBUG, "FT: R0KH-ID in FTIE",
549 parse.r0kh_id, parse.r0kh_id_len);
550 wpa_hexdump(MSG_DEBUG, "FT: The current R0KH-ID",
551 sm->r0kh_id, sm->r0kh_id_len);
555 if (parse.r1kh_id == NULL) {
556 wpa_printf(MSG_DEBUG, "FT: No R1KH-ID subelem in FTIE");
560 if (parse.rsn_pmkid == NULL ||
561 os_memcmp_const(parse.rsn_pmkid, sm->pmk_r0_name, WPA_PMK_NAME_LEN))
563 wpa_printf(MSG_DEBUG, "FT: No matching PMKR0Name (PMKID) in "
568 os_memcpy(sm->r1kh_id, parse.r1kh_id, FT_R1KH_ID_LEN);
569 wpa_hexdump(MSG_DEBUG, "FT: R1KH-ID", sm->r1kh_id, FT_R1KH_ID_LEN);
570 wpa_hexdump(MSG_DEBUG, "FT: SNonce", sm->snonce, WPA_NONCE_LEN);
571 wpa_hexdump(MSG_DEBUG, "FT: ANonce", anonce, WPA_NONCE_LEN);
572 os_memcpy(sm->anonce, anonce, WPA_NONCE_LEN);
573 if (wpa_derive_pmk_r1(sm->pmk_r0, sm->pmk_r0_len, sm->pmk_r0_name,
574 sm->r1kh_id, sm->own_addr, sm->pmk_r1,
575 sm->pmk_r1_name) < 0)
577 sm->pmk_r1_len = sm->pmk_r0_len;
578 wpa_hexdump_key(MSG_DEBUG, "FT: PMK-R1", sm->pmk_r1, sm->pmk_r1_len);
579 wpa_hexdump(MSG_DEBUG, "FT: PMKR1Name",
580 sm->pmk_r1_name, WPA_PMK_NAME_LEN);
583 if (wpa_pmk_r1_to_ptk(sm->pmk_r1, sm->pmk_r1_len, sm->snonce,
584 anonce, sm->own_addr, bssid,
585 sm->pmk_r1_name, &sm->ptk, ptk_name, sm->key_mgmt,
586 sm->pairwise_cipher) < 0)
589 if (wpa_key_mgmt_fils(sm->key_mgmt)) {
591 kck_len = sm->ptk.kck2_len;
594 kck_len = sm->ptk.kck_len;
596 ft_ies = wpa_ft_gen_req_ies(sm, &ft_ies_len, anonce,
599 ric_ies, ric_ies_len,
600 parse.mdie ? parse.mdie - 2 : NULL);
602 wpa_sm_update_ft_ies(sm, sm->mobility_domain,
607 wpa_sm_mark_authenticated(sm, bssid);
608 ret = wpa_ft_install_ptk(sm, bssid);
611 * Some drivers do not support key configuration when we are
612 * not associated with the target AP. Work around this by
613 * trying again after the following reassociation gets
616 wpa_printf(MSG_DEBUG, "FT: Failed to set PTK prior to "
617 "association - try again after reassociation");
618 sm->set_ptk_after_assoc = 1;
620 sm->set_ptk_after_assoc = 0;
622 sm->ft_completed = 1;
625 * The caller is expected trigger re-association with the
628 os_memcpy(sm->bssid, target_ap, ETH_ALEN);
635 int wpa_ft_is_completed(struct wpa_sm *sm)
640 if (!wpa_key_mgmt_ft(sm->key_mgmt))
643 return sm->ft_completed;
647 void wpa_reset_ft_completed(struct wpa_sm *sm)
650 sm->ft_completed = 0;
654 static int wpa_ft_process_gtk_subelem(struct wpa_sm *sm, const u8 *gtk_elem,
660 size_t gtk_len, keylen, rsc_len;
664 if (wpa_key_mgmt_fils(sm->key_mgmt)) {
666 kek_len = sm->ptk.kek2_len;
669 kek_len = sm->ptk.kek_len;
672 if (gtk_elem == NULL) {
673 wpa_printf(MSG_DEBUG, "FT: No GTK included in FTIE");
677 wpa_hexdump_key(MSG_DEBUG, "FT: Received GTK in Reassoc Resp",
678 gtk_elem, gtk_elem_len);
680 if (gtk_elem_len < 11 + 24 || (gtk_elem_len - 11) % 8 ||
681 gtk_elem_len - 19 > sizeof(gtk)) {
682 wpa_printf(MSG_DEBUG, "FT: Invalid GTK sub-elem "
683 "length %lu", (unsigned long) gtk_elem_len);
686 gtk_len = gtk_elem_len - 19;
687 if (aes_unwrap(kek, kek_len, gtk_len / 8, gtk_elem + 11, gtk)) {
688 wpa_printf(MSG_WARNING, "FT: AES unwrap failed - could not "
693 keylen = wpa_cipher_key_len(sm->group_cipher);
694 rsc_len = wpa_cipher_rsc_len(sm->group_cipher);
695 alg = wpa_cipher_to_alg(sm->group_cipher);
696 if (alg == WPA_ALG_NONE) {
697 wpa_printf(MSG_WARNING, "WPA: Unsupported Group Cipher %d",
702 if (gtk_len < keylen) {
703 wpa_printf(MSG_DEBUG, "FT: Too short GTK in FTIE");
707 /* Key Info[2] | Key Length[1] | RSC[8] | Key[5..32]. */
709 keyidx = WPA_GET_LE16(gtk_elem) & 0x03;
711 if (gtk_elem[2] != keylen) {
712 wpa_printf(MSG_DEBUG, "FT: GTK length mismatch: received %d "
714 gtk_elem[2], (unsigned long) keylen);
718 wpa_hexdump_key(MSG_DEBUG, "FT: GTK from Reassoc Resp", gtk, keylen);
719 if (sm->group_cipher == WPA_CIPHER_TKIP) {
720 /* Swap Tx/Rx keys for Michael MIC */
722 os_memcpy(tmp, gtk + 16, 8);
723 os_memcpy(gtk + 16, gtk + 24, 8);
724 os_memcpy(gtk + 24, tmp, 8);
726 if (wpa_sm_set_key(sm, alg, broadcast_ether_addr, keyidx, 0,
727 gtk_elem + 3, rsc_len, gtk, keylen) < 0) {
728 wpa_printf(MSG_WARNING, "WPA: Failed to set GTK to the "
737 #ifdef CONFIG_IEEE80211W
738 static int wpa_ft_process_igtk_subelem(struct wpa_sm *sm, const u8 *igtk_elem,
739 size_t igtk_elem_len)
741 u8 igtk[WPA_IGTK_MAX_LEN];
747 if (wpa_key_mgmt_fils(sm->key_mgmt)) {
749 kek_len = sm->ptk.kek2_len;
752 kek_len = sm->ptk.kek_len;
755 if (sm->mgmt_group_cipher != WPA_CIPHER_AES_128_CMAC &&
756 sm->mgmt_group_cipher != WPA_CIPHER_BIP_GMAC_128 &&
757 sm->mgmt_group_cipher != WPA_CIPHER_BIP_GMAC_256 &&
758 sm->mgmt_group_cipher != WPA_CIPHER_BIP_CMAC_256)
761 if (igtk_elem == NULL) {
762 wpa_printf(MSG_DEBUG, "FT: No IGTK included in FTIE");
766 wpa_hexdump_key(MSG_DEBUG, "FT: Received IGTK in Reassoc Resp",
767 igtk_elem, igtk_elem_len);
769 igtk_len = wpa_cipher_key_len(sm->mgmt_group_cipher);
770 if (igtk_elem_len != 2 + 6 + 1 + igtk_len + 8) {
771 wpa_printf(MSG_DEBUG, "FT: Invalid IGTK sub-elem "
772 "length %lu", (unsigned long) igtk_elem_len);
775 if (igtk_elem[8] != igtk_len) {
776 wpa_printf(MSG_DEBUG, "FT: Invalid IGTK sub-elem Key Length "
781 if (aes_unwrap(kek, kek_len, igtk_len / 8, igtk_elem + 9, igtk)) {
782 wpa_printf(MSG_WARNING, "FT: AES unwrap failed - could not "
787 /* KeyID[2] | IPN[6] | Key Length[1] | Key[16+8] */
789 keyidx = WPA_GET_LE16(igtk_elem);
791 wpa_hexdump_key(MSG_DEBUG, "FT: IGTK from Reassoc Resp", igtk,
793 if (wpa_sm_set_key(sm, wpa_cipher_to_alg(sm->mgmt_group_cipher),
794 broadcast_ether_addr, keyidx, 0,
795 igtk_elem + 2, 6, igtk, igtk_len) < 0) {
796 wpa_printf(MSG_WARNING, "WPA: Failed to set IGTK to the "
798 os_memset(igtk, 0, sizeof(igtk));
801 os_memset(igtk, 0, sizeof(igtk));
805 #endif /* CONFIG_IEEE80211W */
808 int wpa_ft_validate_reassoc_resp(struct wpa_sm *sm, const u8 *ies,
809 size_t ies_len, const u8 *src_addr)
811 struct wpa_ft_ies parse;
812 struct rsn_mdie *mdie;
814 u8 mic[WPA_EAPOL_KEY_MIC_MAX_LEN];
817 int use_sha384 = wpa_key_mgmt_sha384(sm->key_mgmt);
818 const u8 *anonce, *snonce, *fte_mic;
821 wpa_hexdump(MSG_DEBUG, "FT: Response IEs", ies, ies_len);
823 if (!wpa_key_mgmt_ft(sm->key_mgmt)) {
824 wpa_printf(MSG_DEBUG, "FT: Reject FT IEs since FT is not "
825 "enabled for this connection");
829 if (sm->ft_reassoc_completed) {
830 wpa_printf(MSG_DEBUG, "FT: Reassociation has already been completed for this FT protocol instance - ignore unexpected retransmission");
834 if (wpa_ft_parse_ies(ies, ies_len, &parse, use_sha384) < 0) {
835 wpa_printf(MSG_DEBUG, "FT: Failed to parse IEs");
839 mdie = (struct rsn_mdie *) parse.mdie;
840 if (mdie == NULL || parse.mdie_len < sizeof(*mdie) ||
841 os_memcmp(mdie->mobility_domain, sm->mobility_domain,
842 MOBILITY_DOMAIN_ID_LEN) != 0) {
843 wpa_printf(MSG_DEBUG, "FT: Invalid MDIE");
848 struct rsn_ftie_sha384 *ftie;
850 ftie = (struct rsn_ftie_sha384 *) parse.ftie;
851 if (!ftie || parse.ftie_len < sizeof(*ftie)) {
852 wpa_printf(MSG_DEBUG, "FT: Invalid FTIE");
856 anonce = ftie->anonce;
857 snonce = ftie->snonce;
858 fte_elem_count = ftie->mic_control[1];
861 struct rsn_ftie *ftie;
863 ftie = (struct rsn_ftie *) parse.ftie;
864 if (!ftie || parse.ftie_len < sizeof(*ftie)) {
865 wpa_printf(MSG_DEBUG, "FT: Invalid FTIE");
869 anonce = ftie->anonce;
870 snonce = ftie->snonce;
871 fte_elem_count = ftie->mic_control[1];
875 if (os_memcmp(snonce, sm->snonce, WPA_NONCE_LEN) != 0) {
876 wpa_printf(MSG_DEBUG, "FT: SNonce mismatch in FTIE");
877 wpa_hexdump(MSG_DEBUG, "FT: Received SNonce",
878 snonce, WPA_NONCE_LEN);
879 wpa_hexdump(MSG_DEBUG, "FT: Expected SNonce",
880 sm->snonce, WPA_NONCE_LEN);
884 if (os_memcmp(anonce, sm->anonce, WPA_NONCE_LEN) != 0) {
885 wpa_printf(MSG_DEBUG, "FT: ANonce mismatch in FTIE");
886 wpa_hexdump(MSG_DEBUG, "FT: Received ANonce",
887 anonce, WPA_NONCE_LEN);
888 wpa_hexdump(MSG_DEBUG, "FT: Expected ANonce",
889 sm->anonce, WPA_NONCE_LEN);
893 if (parse.r0kh_id == NULL) {
894 wpa_printf(MSG_DEBUG, "FT: No R0KH-ID subelem in FTIE");
898 if (parse.r0kh_id_len != sm->r0kh_id_len ||
899 os_memcmp_const(parse.r0kh_id, sm->r0kh_id, parse.r0kh_id_len) != 0)
901 wpa_printf(MSG_DEBUG, "FT: R0KH-ID in FTIE did not match with "
902 "the current R0KH-ID");
903 wpa_hexdump(MSG_DEBUG, "FT: R0KH-ID in FTIE",
904 parse.r0kh_id, parse.r0kh_id_len);
905 wpa_hexdump(MSG_DEBUG, "FT: The current R0KH-ID",
906 sm->r0kh_id, sm->r0kh_id_len);
910 if (parse.r1kh_id == NULL) {
911 wpa_printf(MSG_DEBUG, "FT: No R1KH-ID subelem in FTIE");
915 if (os_memcmp_const(parse.r1kh_id, sm->r1kh_id, FT_R1KH_ID_LEN) != 0) {
916 wpa_printf(MSG_DEBUG, "FT: Unknown R1KH-ID used in "
921 if (parse.rsn_pmkid == NULL ||
922 os_memcmp_const(parse.rsn_pmkid, sm->pmk_r1_name, WPA_PMK_NAME_LEN))
924 wpa_printf(MSG_DEBUG, "FT: No matching PMKR1Name (PMKID) in "
925 "RSNIE (pmkid=%d)", !!parse.rsn_pmkid);
931 count += ieee802_11_ie_count(parse.ric, parse.ric_len);
932 if (fte_elem_count != count) {
933 wpa_printf(MSG_DEBUG, "FT: Unexpected IE count in MIC "
934 "Control: received %u expected %u",
935 fte_elem_count, count);
939 if (wpa_key_mgmt_fils(sm->key_mgmt)) {
941 kck_len = sm->ptk.kck2_len;
944 kck_len = sm->ptk.kck_len;
947 if (wpa_ft_mic(kck, kck_len, sm->own_addr, src_addr, 6,
948 parse.mdie - 2, parse.mdie_len + 2,
949 parse.ftie - 2, parse.ftie_len + 2,
950 parse.rsn - 2, parse.rsn_len + 2,
951 parse.ric, parse.ric_len,
953 wpa_printf(MSG_DEBUG, "FT: Failed to calculate MIC");
957 if (os_memcmp_const(mic, fte_mic, 16) != 0) {
958 wpa_printf(MSG_DEBUG, "FT: Invalid MIC in FTIE");
959 wpa_hexdump(MSG_MSGDUMP, "FT: Received MIC", fte_mic, 16);
960 wpa_hexdump(MSG_MSGDUMP, "FT: Calculated MIC", mic, 16);
964 sm->ft_reassoc_completed = 1;
966 if (wpa_ft_process_gtk_subelem(sm, parse.gtk, parse.gtk_len) < 0)
969 #ifdef CONFIG_IEEE80211W
970 if (wpa_ft_process_igtk_subelem(sm, parse.igtk, parse.igtk_len) < 0)
972 #endif /* CONFIG_IEEE80211W */
974 if (sm->set_ptk_after_assoc) {
975 wpa_printf(MSG_DEBUG, "FT: Try to set PTK again now that we "
977 if (wpa_ft_install_ptk(sm, src_addr) < 0)
979 sm->set_ptk_after_assoc = 0;
983 wpa_hexdump(MSG_MSGDUMP, "FT: RIC Response",
984 parse.ric, parse.ric_len);
985 /* TODO: parse response and inform driver about results when
986 * using wpa_supplicant SME */
989 wpa_printf(MSG_DEBUG, "FT: Completed successfully");
996 * wpa_ft_start_over_ds - Generate over-the-DS auth request
997 * @sm: Pointer to WPA state machine data from wpa_sm_init()
998 * @target_ap: Target AP Address
999 * @mdie: Mobility Domain IE from the target AP
1000 * Returns: 0 on success, -1 on failure
1002 int wpa_ft_start_over_ds(struct wpa_sm *sm, const u8 *target_ap,
1008 wpa_printf(MSG_DEBUG, "FT: Request over-the-DS with " MACSTR,
1009 MAC2STR(target_ap));
1011 /* Generate a new SNonce */
1012 if (random_get_bytes(sm->snonce, WPA_NONCE_LEN)) {
1013 wpa_printf(MSG_INFO, "FT: Failed to generate a new SNonce");
1017 ft_ies = wpa_ft_gen_req_ies(sm, &ft_ies_len, NULL, sm->pmk_r0_name,
1018 NULL, 0, target_ap, NULL, 0, mdie);
1020 sm->over_the_ds_in_progress = 1;
1021 os_memcpy(sm->target_ap, target_ap, ETH_ALEN);
1022 wpa_sm_send_ft_action(sm, 1, target_ap, ft_ies, ft_ies_len);
1029 #endif /* CONFIG_IEEE80211R */