1 /*
2 * WPA Supplicant - IEEE 802.11r - Fast BSS Transition
3 * Copyright (c) 2006-2015, Jouni Malinen <j@w1.fi>
4 *
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
7 */
8
9 #include "includes.h"
10
11 #include "common.h"
12 #include "crypto/aes_wrap.h"
13 #include "crypto/random.h"
14 #include "common/ieee802_11_defs.h"
15 #include "common/ieee802_11_common.h"
16 #include "wpa.h"
17 #include "wpa_i.h"
18
19 #ifdef CONFIG_IEEE80211R
20
wpa_derive_ptk_ft(struct wpa_sm * sm,const unsigned char * src_addr,const struct wpa_eapol_key * key,struct wpa_ptk * ptk)21 int wpa_derive_ptk_ft(struct wpa_sm *sm, const unsigned char *src_addr,
22 const struct wpa_eapol_key *key, struct wpa_ptk *ptk)
23 {
24 u8 ptk_name[WPA_PMK_NAME_LEN];
25 const u8 *anonce = key->key_nonce;
26
27 if (sm->xxkey_len == 0) {
28 wpa_printf(MSG_DEBUG, "FT: XXKey not available for key "
29 "derivation");
30 return -1;
31 }
32
33 wpa_derive_pmk_r0(sm->xxkey, sm->xxkey_len, sm->ssid,
34 sm->ssid_len, sm->mobility_domain,
35 sm->r0kh_id, sm->r0kh_id_len, sm->own_addr,
36 sm->pmk_r0, sm->pmk_r0_name);
37 wpa_hexdump_key(MSG_DEBUG, "FT: PMK-R0", sm->pmk_r0, PMK_LEN);
38 wpa_hexdump(MSG_DEBUG, "FT: PMKR0Name",
39 sm->pmk_r0_name, WPA_PMK_NAME_LEN);
40 wpa_derive_pmk_r1(sm->pmk_r0, sm->pmk_r0_name, sm->r1kh_id,
41 sm->own_addr, sm->pmk_r1, sm->pmk_r1_name);
42 wpa_hexdump_key(MSG_DEBUG, "FT: PMK-R1", sm->pmk_r1, PMK_LEN);
43 wpa_hexdump(MSG_DEBUG, "FT: PMKR1Name", sm->pmk_r1_name,
44 WPA_PMK_NAME_LEN);
45 return wpa_pmk_r1_to_ptk(sm->pmk_r1, sm->snonce, anonce, sm->own_addr,
46 sm->bssid, sm->pmk_r1_name, ptk, ptk_name,
47 sm->key_mgmt, sm->pairwise_cipher);
48 }
49
50
51 /**
52 * wpa_sm_set_ft_params - Set FT (IEEE 802.11r) parameters
53 * @sm: Pointer to WPA state machine data from wpa_sm_init()
54 * @ies: Association Response IEs or %NULL to clear FT parameters
55 * @ies_len: Length of ies buffer in octets
56 * Returns: 0 on success, -1 on failure
57 */
wpa_sm_set_ft_params(struct wpa_sm * sm,const u8 * ies,size_t ies_len)58 int wpa_sm_set_ft_params(struct wpa_sm *sm, const u8 *ies, size_t ies_len)
59 {
60 struct wpa_ft_ies ft;
61
62 if (sm == NULL)
63 return 0;
64
65 if (wpa_ft_parse_ies(ies, ies_len, &ft) < 0)
66 return -1;
67
68 if (ft.mdie && ft.mdie_len < MOBILITY_DOMAIN_ID_LEN + 1)
69 return -1;
70
71 if (ft.mdie) {
72 wpa_hexdump(MSG_DEBUG, "FT: Mobility domain",
73 ft.mdie, MOBILITY_DOMAIN_ID_LEN);
74 os_memcpy(sm->mobility_domain, ft.mdie,
75 MOBILITY_DOMAIN_ID_LEN);
76 sm->mdie_ft_capab = ft.mdie[MOBILITY_DOMAIN_ID_LEN];
77 wpa_printf(MSG_DEBUG, "FT: Capability and Policy: 0x%02x",
78 sm->mdie_ft_capab);
79 } else
80 os_memset(sm->mobility_domain, 0, MOBILITY_DOMAIN_ID_LEN);
81
82 if (ft.r0kh_id) {
83 wpa_hexdump(MSG_DEBUG, "FT: R0KH-ID",
84 ft.r0kh_id, ft.r0kh_id_len);
85 os_memcpy(sm->r0kh_id, ft.r0kh_id, ft.r0kh_id_len);
86 sm->r0kh_id_len = ft.r0kh_id_len;
87 } else {
88 /* FIX: When should R0KH-ID be cleared? We need to keep the
89 * old R0KH-ID in order to be able to use this during FT. */
90 /*
91 * os_memset(sm->r0kh_id, 0, FT_R0KH_ID_LEN);
92 * sm->r0kh_id_len = 0;
93 */
94 }
95
96 if (ft.r1kh_id) {
97 wpa_hexdump(MSG_DEBUG, "FT: R1KH-ID",
98 ft.r1kh_id, FT_R1KH_ID_LEN);
99 os_memcpy(sm->r1kh_id, ft.r1kh_id, FT_R1KH_ID_LEN);
100 } else
101 os_memset(sm->r1kh_id, 0, FT_R1KH_ID_LEN);
102
103 os_free(sm->assoc_resp_ies);
104 sm->assoc_resp_ies = os_malloc(ft.mdie_len + 2 + ft.ftie_len + 2);
105 if (sm->assoc_resp_ies) {
106 u8 *pos = sm->assoc_resp_ies;
107 if (ft.mdie) {
108 os_memcpy(pos, ft.mdie - 2, ft.mdie_len + 2);
109 pos += ft.mdie_len + 2;
110 }
111 if (ft.ftie) {
112 os_memcpy(pos, ft.ftie - 2, ft.ftie_len + 2);
113 pos += ft.ftie_len + 2;
114 }
115 sm->assoc_resp_ies_len = pos - sm->assoc_resp_ies;
116 wpa_hexdump(MSG_DEBUG, "FT: Stored MDIE and FTIE from "
117 "(Re)Association Response",
118 sm->assoc_resp_ies, sm->assoc_resp_ies_len);
119 }
120
121 return 0;
122 }
123
124
125 /**
126 * wpa_ft_gen_req_ies - Generate FT (IEEE 802.11r) IEs for Auth/ReAssoc Request
127 * @sm: Pointer to WPA state machine data from wpa_sm_init()
128 * @len: Buffer for returning the length of the IEs
129 * @anonce: ANonce or %NULL if not yet available
130 * @pmk_name: PMKR0Name or PMKR1Name to be added into the RSN IE PMKID List
131 * @kck: 128-bit KCK for MIC or %NULL if no MIC is used
132 * @kck_len: KCK length in octets
133 * @target_ap: Target AP address
134 * @ric_ies: Optional IE(s), e.g., WMM TSPEC(s), for RIC-Request or %NULL
135 * @ric_ies_len: Length of ric_ies buffer in octets
136 * @ap_mdie: Mobility Domain IE from the target AP
137 * Returns: Pointer to buffer with IEs or %NULL on failure
138 *
139 * Caller is responsible for freeing the returned buffer with os_free();
140 */
wpa_ft_gen_req_ies(struct wpa_sm * sm,size_t * len,const u8 * anonce,const u8 * pmk_name,const u8 * kck,size_t kck_len,const u8 * target_ap,const u8 * ric_ies,size_t ric_ies_len,const u8 * ap_mdie)141 static u8 * wpa_ft_gen_req_ies(struct wpa_sm *sm, size_t *len,
142 const u8 *anonce, const u8 *pmk_name,
143 const u8 *kck, size_t kck_len,
144 const u8 *target_ap,
145 const u8 *ric_ies, size_t ric_ies_len,
146 const u8 *ap_mdie)
147 {
148 size_t buf_len;
149 u8 *buf, *pos, *ftie_len, *ftie_pos;
150 struct rsn_mdie *mdie;
151 struct rsn_ftie *ftie;
152 struct rsn_ie_hdr *rsnie;
153 u16 capab;
154
155 sm->ft_completed = 0;
156 sm->ft_reassoc_completed = 0;
157
158 buf_len = 2 + sizeof(struct rsn_mdie) + 2 + sizeof(struct rsn_ftie) +
159 2 + sm->r0kh_id_len + ric_ies_len + 100;
160 buf = os_zalloc(buf_len);
161 if (buf == NULL)
162 return NULL;
163 pos = buf;
164
165 /* RSNIE[PMKR0Name/PMKR1Name] */
166 rsnie = (struct rsn_ie_hdr *) pos;
167 rsnie->elem_id = WLAN_EID_RSN;
168 WPA_PUT_LE16(rsnie->version, RSN_VERSION);
169 pos = (u8 *) (rsnie + 1);
170
171 /* Group Suite Selector */
172 if (!wpa_cipher_valid_group(sm->group_cipher)) {
173 wpa_printf(MSG_WARNING, "FT: Invalid group cipher (%d)",
174 sm->group_cipher);
175 os_free(buf);
176 return NULL;
177 }
178 RSN_SELECTOR_PUT(pos, wpa_cipher_to_suite(WPA_PROTO_RSN,
179 sm->group_cipher));
180 pos += RSN_SELECTOR_LEN;
181
182 /* Pairwise Suite Count */
183 WPA_PUT_LE16(pos, 1);
184 pos += 2;
185
186 /* Pairwise Suite List */
187 if (!wpa_cipher_valid_pairwise(sm->pairwise_cipher)) {
188 wpa_printf(MSG_WARNING, "FT: Invalid pairwise cipher (%d)",
189 sm->pairwise_cipher);
190 os_free(buf);
191 return NULL;
192 }
193 RSN_SELECTOR_PUT(pos, wpa_cipher_to_suite(WPA_PROTO_RSN,
194 sm->pairwise_cipher));
195 pos += RSN_SELECTOR_LEN;
196
197 /* Authenticated Key Management Suite Count */
198 WPA_PUT_LE16(pos, 1);
199 pos += 2;
200
201 /* Authenticated Key Management Suite List */
202 if (sm->key_mgmt == WPA_KEY_MGMT_FT_IEEE8021X)
203 RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_FT_802_1X);
204 else if (sm->key_mgmt == WPA_KEY_MGMT_FT_PSK)
205 RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_FT_PSK);
206 else if (sm->key_mgmt == WPA_KEY_MGMT_FT_SAE)
207 RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_FT_SAE);
208 #ifdef CONFIG_FILS
209 else if (sm->key_mgmt == WPA_KEY_MGMT_FT_FILS_SHA256)
210 RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_FT_FILS_SHA256);
211 else if (sm->key_mgmt == WPA_KEY_MGMT_FT_FILS_SHA384)
212 RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_FT_FILS_SHA384);
213 #endif /* CONFIG_FILS */
214 else {
215 wpa_printf(MSG_WARNING, "FT: Invalid key management type (%d)",
216 sm->key_mgmt);
217 os_free(buf);
218 return NULL;
219 }
220 pos += RSN_SELECTOR_LEN;
221
222 /* RSN Capabilities */
223 capab = 0;
224 #ifdef CONFIG_IEEE80211W
225 if (sm->mgmt_group_cipher == WPA_CIPHER_AES_128_CMAC)
226 capab |= WPA_CAPABILITY_MFPC;
227 #endif /* CONFIG_IEEE80211W */
228 WPA_PUT_LE16(pos, capab);
229 pos += 2;
230
231 /* PMKID Count */
232 WPA_PUT_LE16(pos, 1);
233 pos += 2;
234
235 /* PMKID List [PMKR0Name/PMKR1Name] */
236 os_memcpy(pos, pmk_name, WPA_PMK_NAME_LEN);
237 pos += WPA_PMK_NAME_LEN;
238
239 #ifdef CONFIG_IEEE80211W
240 if (sm->mgmt_group_cipher == WPA_CIPHER_AES_128_CMAC) {
241 /* Management Group Cipher Suite */
242 RSN_SELECTOR_PUT(pos, RSN_CIPHER_SUITE_AES_128_CMAC);
243 pos += RSN_SELECTOR_LEN;
244 }
245 #endif /* CONFIG_IEEE80211W */
246
247 rsnie->len = (pos - (u8 *) rsnie) - 2;
248
249 /* MDIE */
250 *pos++ = WLAN_EID_MOBILITY_DOMAIN;
251 *pos++ = sizeof(*mdie);
252 mdie = (struct rsn_mdie *) pos;
253 pos += sizeof(*mdie);
254 os_memcpy(mdie->mobility_domain, sm->mobility_domain,
255 MOBILITY_DOMAIN_ID_LEN);
256 mdie->ft_capab = ap_mdie && ap_mdie[1] >= 3 ? ap_mdie[4] :
257 sm->mdie_ft_capab;
258
259 /* FTIE[SNonce, [R1KH-ID,] R0KH-ID ] */
260 ftie_pos = pos;
261 *pos++ = WLAN_EID_FAST_BSS_TRANSITION;
262 ftie_len = pos++;
263 ftie = (struct rsn_ftie *) pos;
264 pos += sizeof(*ftie);
265 os_memcpy(ftie->snonce, sm->snonce, WPA_NONCE_LEN);
266 if (anonce)
267 os_memcpy(ftie->anonce, anonce, WPA_NONCE_LEN);
268 if (kck) {
269 /* R1KH-ID sub-element in third FT message */
270 *pos++ = FTIE_SUBELEM_R1KH_ID;
271 *pos++ = FT_R1KH_ID_LEN;
272 os_memcpy(pos, sm->r1kh_id, FT_R1KH_ID_LEN);
273 pos += FT_R1KH_ID_LEN;
274 }
275 /* R0KH-ID sub-element */
276 *pos++ = FTIE_SUBELEM_R0KH_ID;
277 *pos++ = sm->r0kh_id_len;
278 os_memcpy(pos, sm->r0kh_id, sm->r0kh_id_len);
279 pos += sm->r0kh_id_len;
280 *ftie_len = pos - ftie_len - 1;
281
282 if (ric_ies) {
283 /* RIC Request */
284 os_memcpy(pos, ric_ies, ric_ies_len);
285 pos += ric_ies_len;
286 }
287
288 if (kck) {
289 /*
290 * IEEE Std 802.11r-2008, 11A.8.4
291 * MIC shall be calculated over:
292 * non-AP STA MAC address
293 * Target AP MAC address
294 * Transaction seq number (5 for ReassocReq, 3 otherwise)
295 * RSN IE
296 * MDIE
297 * FTIE (with MIC field set to 0)
298 * RIC-Request (if present)
299 */
300 /* Information element count */
301 ftie->mic_control[1] = 3 + ieee802_11_ie_count(ric_ies,
302 ric_ies_len);
303 if (wpa_ft_mic(kck, kck_len, sm->own_addr, target_ap, 5,
304 ((u8 *) mdie) - 2, 2 + sizeof(*mdie),
305 ftie_pos, 2 + *ftie_len,
306 (u8 *) rsnie, 2 + rsnie->len, ric_ies,
307 ric_ies_len, ftie->mic) < 0) {
308 wpa_printf(MSG_INFO, "FT: Failed to calculate MIC");
309 os_free(buf);
310 return NULL;
311 }
312 }
313
314 *len = pos - buf;
315
316 return buf;
317 }
318
319
wpa_ft_install_ptk(struct wpa_sm * sm,const u8 * bssid)320 static int wpa_ft_install_ptk(struct wpa_sm *sm, const u8 *bssid)
321 {
322 int keylen;
323 enum wpa_alg alg;
324 u8 null_rsc[6] = { 0, 0, 0, 0, 0, 0 };
325
326 wpa_printf(MSG_DEBUG, "FT: Installing PTK to the driver.");
327
328 if (!wpa_cipher_valid_pairwise(sm->pairwise_cipher)) {
329 wpa_printf(MSG_WARNING, "FT: Unsupported pairwise cipher %d",
330 sm->pairwise_cipher);
331 return -1;
332 }
333
334 alg = wpa_cipher_to_alg(sm->pairwise_cipher);
335 keylen = wpa_cipher_key_len(sm->pairwise_cipher);
336
337 if (wpa_sm_set_key(sm, alg, bssid, 0, 1, null_rsc,
338 sizeof(null_rsc), (u8 *) sm->ptk.tk, keylen) < 0) {
339 wpa_printf(MSG_WARNING, "FT: Failed to set PTK to the driver");
340 return -1;
341 }
342
343 return 0;
344 }
345
346
347 /**
348 * wpa_ft_prepare_auth_request - Generate over-the-air auth request
349 * @sm: Pointer to WPA state machine data from wpa_sm_init()
350 * @mdie: Target AP MDIE
351 * Returns: 0 on success, -1 on failure
352 */
wpa_ft_prepare_auth_request(struct wpa_sm * sm,const u8 * mdie)353 int wpa_ft_prepare_auth_request(struct wpa_sm *sm, const u8 *mdie)
354 {
355 u8 *ft_ies;
356 size_t ft_ies_len;
357
358 /* Generate a new SNonce */
359 if (random_get_bytes(sm->snonce, WPA_NONCE_LEN)) {
360 wpa_printf(MSG_INFO, "FT: Failed to generate a new SNonce");
361 return -1;
362 }
363
364 ft_ies = wpa_ft_gen_req_ies(sm, &ft_ies_len, NULL, sm->pmk_r0_name,
365 NULL, 0, sm->bssid, NULL, 0, mdie);
366 if (ft_ies) {
367 wpa_sm_update_ft_ies(sm, sm->mobility_domain,
368 ft_ies, ft_ies_len);
369 os_free(ft_ies);
370 }
371
372 return 0;
373 }
374
375
wpa_ft_process_response(struct wpa_sm * sm,const u8 * ies,size_t ies_len,int ft_action,const u8 * target_ap,const u8 * ric_ies,size_t ric_ies_len)376 int wpa_ft_process_response(struct wpa_sm *sm, const u8 *ies, size_t ies_len,
377 int ft_action, const u8 *target_ap,
378 const u8 *ric_ies, size_t ric_ies_len)
379 {
380 u8 *ft_ies;
381 size_t ft_ies_len;
382 struct wpa_ft_ies parse;
383 struct rsn_mdie *mdie;
384 struct rsn_ftie *ftie;
385 u8 ptk_name[WPA_PMK_NAME_LEN];
386 int ret;
387 const u8 *bssid;
388
389 wpa_hexdump(MSG_DEBUG, "FT: Response IEs", ies, ies_len);
390 wpa_hexdump(MSG_DEBUG, "FT: RIC IEs", ric_ies, ric_ies_len);
391
392 if (ft_action) {
393 if (!sm->over_the_ds_in_progress) {
394 wpa_printf(MSG_DEBUG, "FT: No over-the-DS in progress "
395 "- drop FT Action Response");
396 return -1;
397 }
398
399 if (os_memcmp(target_ap, sm->target_ap, ETH_ALEN) != 0) {
400 wpa_printf(MSG_DEBUG, "FT: No over-the-DS in progress "
401 "with this Target AP - drop FT Action "
402 "Response");
403 return -1;
404 }
405 }
406
407 if (!wpa_key_mgmt_ft(sm->key_mgmt)) {
408 wpa_printf(MSG_DEBUG, "FT: Reject FT IEs since FT is not "
409 "enabled for this connection");
410 return -1;
411 }
412
413 if (wpa_ft_parse_ies(ies, ies_len, &parse) < 0) {
414 wpa_printf(MSG_DEBUG, "FT: Failed to parse IEs");
415 return -1;
416 }
417
418 mdie = (struct rsn_mdie *) parse.mdie;
419 if (mdie == NULL || parse.mdie_len < sizeof(*mdie) ||
420 os_memcmp(mdie->mobility_domain, sm->mobility_domain,
421 MOBILITY_DOMAIN_ID_LEN) != 0) {
422 wpa_printf(MSG_DEBUG, "FT: Invalid MDIE");
423 return -1;
424 }
425
426 ftie = (struct rsn_ftie *) parse.ftie;
427 if (ftie == NULL || parse.ftie_len < sizeof(*ftie)) {
428 wpa_printf(MSG_DEBUG, "FT: Invalid FTIE");
429 return -1;
430 }
431
432 if (os_memcmp(ftie->snonce, sm->snonce, WPA_NONCE_LEN) != 0) {
433 wpa_printf(MSG_DEBUG, "FT: SNonce mismatch in FTIE");
434 wpa_hexdump(MSG_DEBUG, "FT: Received SNonce",
435 ftie->snonce, WPA_NONCE_LEN);
436 wpa_hexdump(MSG_DEBUG, "FT: Expected SNonce",
437 sm->snonce, WPA_NONCE_LEN);
438 return -1;
439 }
440
441 if (parse.r0kh_id == NULL) {
442 wpa_printf(MSG_DEBUG, "FT: No R0KH-ID subelem in FTIE");
443 return -1;
444 }
445
446 if (parse.r0kh_id_len != sm->r0kh_id_len ||
447 os_memcmp_const(parse.r0kh_id, sm->r0kh_id, parse.r0kh_id_len) != 0)
448 {
449 wpa_printf(MSG_DEBUG, "FT: R0KH-ID in FTIE did not match with "
450 "the current R0KH-ID");
451 wpa_hexdump(MSG_DEBUG, "FT: R0KH-ID in FTIE",
452 parse.r0kh_id, parse.r0kh_id_len);
453 wpa_hexdump(MSG_DEBUG, "FT: The current R0KH-ID",
454 sm->r0kh_id, sm->r0kh_id_len);
455 return -1;
456 }
457
458 if (parse.r1kh_id == NULL) {
459 wpa_printf(MSG_DEBUG, "FT: No R1KH-ID subelem in FTIE");
460 return -1;
461 }
462
463 if (parse.rsn_pmkid == NULL ||
464 os_memcmp_const(parse.rsn_pmkid, sm->pmk_r0_name, WPA_PMK_NAME_LEN))
465 {
466 wpa_printf(MSG_DEBUG, "FT: No matching PMKR0Name (PMKID) in "
467 "RSNIE");
468 return -1;
469 }
470
471 os_memcpy(sm->r1kh_id, parse.r1kh_id, FT_R1KH_ID_LEN);
472 wpa_hexdump(MSG_DEBUG, "FT: R1KH-ID", sm->r1kh_id, FT_R1KH_ID_LEN);
473 wpa_hexdump(MSG_DEBUG, "FT: SNonce", sm->snonce, WPA_NONCE_LEN);
474 wpa_hexdump(MSG_DEBUG, "FT: ANonce", ftie->anonce, WPA_NONCE_LEN);
475 os_memcpy(sm->anonce, ftie->anonce, WPA_NONCE_LEN);
476 wpa_derive_pmk_r1(sm->pmk_r0, sm->pmk_r0_name, sm->r1kh_id,
477 sm->own_addr, sm->pmk_r1, sm->pmk_r1_name);
478 wpa_hexdump_key(MSG_DEBUG, "FT: PMK-R1", sm->pmk_r1, PMK_LEN);
479 wpa_hexdump(MSG_DEBUG, "FT: PMKR1Name",
480 sm->pmk_r1_name, WPA_PMK_NAME_LEN);
481
482 bssid = target_ap;
483 if (wpa_pmk_r1_to_ptk(sm->pmk_r1, sm->snonce, ftie->anonce,
484 sm->own_addr, bssid, sm->pmk_r1_name, &sm->ptk,
485 ptk_name, sm->key_mgmt, sm->pairwise_cipher) < 0)
486 return -1;
487
488 ft_ies = wpa_ft_gen_req_ies(sm, &ft_ies_len, ftie->anonce,
489 sm->pmk_r1_name,
490 sm->ptk.kck, sm->ptk.kck_len, bssid,
491 ric_ies, ric_ies_len,
492 parse.mdie ? parse.mdie - 2 : NULL);
493 if (ft_ies) {
494 wpa_sm_update_ft_ies(sm, sm->mobility_domain,
495 ft_ies, ft_ies_len);
496 os_free(ft_ies);
497 }
498
499 wpa_sm_mark_authenticated(sm, bssid);
500 ret = wpa_ft_install_ptk(sm, bssid);
501 if (ret) {
502 /*
503 * Some drivers do not support key configuration when we are
504 * not associated with the target AP. Work around this by
505 * trying again after the following reassociation gets
506 * completed.
507 */
508 wpa_printf(MSG_DEBUG, "FT: Failed to set PTK prior to "
509 "association - try again after reassociation");
510 sm->set_ptk_after_assoc = 1;
511 } else
512 sm->set_ptk_after_assoc = 0;
513
514 sm->ft_completed = 1;
515 if (ft_action) {
516 /*
517 * The caller is expected trigger re-association with the
518 * Target AP.
519 */
520 os_memcpy(sm->bssid, target_ap, ETH_ALEN);
521 }
522
523 return 0;
524 }
525
526
wpa_ft_is_completed(struct wpa_sm * sm)527 int wpa_ft_is_completed(struct wpa_sm *sm)
528 {
529 if (sm == NULL)
530 return 0;
531
532 if (!wpa_key_mgmt_ft(sm->key_mgmt))
533 return 0;
534
535 return sm->ft_completed;
536 }
537
538
wpa_reset_ft_completed(struct wpa_sm * sm)539 void wpa_reset_ft_completed(struct wpa_sm *sm)
540 {
541 if (sm != NULL)
542 sm->ft_completed = 0;
543 }
544
545
wpa_ft_process_gtk_subelem(struct wpa_sm * sm,const u8 * gtk_elem,size_t gtk_elem_len)546 static int wpa_ft_process_gtk_subelem(struct wpa_sm *sm, const u8 *gtk_elem,
547 size_t gtk_elem_len)
548 {
549 u8 gtk[32];
550 int keyidx;
551 enum wpa_alg alg;
552 size_t gtk_len, keylen, rsc_len;
553
554 if (gtk_elem == NULL) {
555 wpa_printf(MSG_DEBUG, "FT: No GTK included in FTIE");
556 return 0;
557 }
558
559 wpa_hexdump_key(MSG_DEBUG, "FT: Received GTK in Reassoc Resp",
560 gtk_elem, gtk_elem_len);
561
562 if (gtk_elem_len < 11 + 24 || (gtk_elem_len - 11) % 8 ||
563 gtk_elem_len - 19 > sizeof(gtk)) {
564 wpa_printf(MSG_DEBUG, "FT: Invalid GTK sub-elem "
565 "length %lu", (unsigned long) gtk_elem_len);
566 return -1;
567 }
568 gtk_len = gtk_elem_len - 19;
569 if (aes_unwrap(sm->ptk.kek, sm->ptk.kek_len, gtk_len / 8, gtk_elem + 11,
570 gtk)) {
571 wpa_printf(MSG_WARNING, "FT: AES unwrap failed - could not "
572 "decrypt GTK");
573 return -1;
574 }
575
576 keylen = wpa_cipher_key_len(sm->group_cipher);
577 rsc_len = wpa_cipher_rsc_len(sm->group_cipher);
578 alg = wpa_cipher_to_alg(sm->group_cipher);
579 if (alg == WPA_ALG_NONE) {
580 wpa_printf(MSG_WARNING, "WPA: Unsupported Group Cipher %d",
581 sm->group_cipher);
582 return -1;
583 }
584
585 if (gtk_len < keylen) {
586 wpa_printf(MSG_DEBUG, "FT: Too short GTK in FTIE");
587 return -1;
588 }
589
590 /* Key Info[2] | Key Length[1] | RSC[8] | Key[5..32]. */
591
592 keyidx = WPA_GET_LE16(gtk_elem) & 0x03;
593
594 if (gtk_elem[2] != keylen) {
595 wpa_printf(MSG_DEBUG, "FT: GTK length mismatch: received %d "
596 "negotiated %lu",
597 gtk_elem[2], (unsigned long) keylen);
598 return -1;
599 }
600
601 wpa_hexdump_key(MSG_DEBUG, "FT: GTK from Reassoc Resp", gtk, keylen);
602 if (sm->group_cipher == WPA_CIPHER_TKIP) {
603 /* Swap Tx/Rx keys for Michael MIC */
604 u8 tmp[8];
605 os_memcpy(tmp, gtk + 16, 8);
606 os_memcpy(gtk + 16, gtk + 24, 8);
607 os_memcpy(gtk + 24, tmp, 8);
608 }
609 if (wpa_sm_set_key(sm, alg, broadcast_ether_addr, keyidx, 0,
610 gtk_elem + 3, rsc_len, gtk, keylen) < 0) {
611 wpa_printf(MSG_WARNING, "WPA: Failed to set GTK to the "
612 "driver.");
613 return -1;
614 }
615
616 return 0;
617 }
618
619
620 #ifdef CONFIG_IEEE80211W
wpa_ft_process_igtk_subelem(struct wpa_sm * sm,const u8 * igtk_elem,size_t igtk_elem_len)621 static int wpa_ft_process_igtk_subelem(struct wpa_sm *sm, const u8 *igtk_elem,
622 size_t igtk_elem_len)
623 {
624 u8 igtk[WPA_IGTK_LEN];
625 u16 keyidx;
626
627 if (sm->mgmt_group_cipher != WPA_CIPHER_AES_128_CMAC)
628 return 0;
629
630 if (igtk_elem == NULL) {
631 wpa_printf(MSG_DEBUG, "FT: No IGTK included in FTIE");
632 return 0;
633 }
634
635 wpa_hexdump_key(MSG_DEBUG, "FT: Received IGTK in Reassoc Resp",
636 igtk_elem, igtk_elem_len);
637
638 if (igtk_elem_len != 2 + 6 + 1 + WPA_IGTK_LEN + 8) {
639 wpa_printf(MSG_DEBUG, "FT: Invalid IGTK sub-elem "
640 "length %lu", (unsigned long) igtk_elem_len);
641 return -1;
642 }
643 if (igtk_elem[8] != WPA_IGTK_LEN) {
644 wpa_printf(MSG_DEBUG, "FT: Invalid IGTK sub-elem Key Length "
645 "%d", igtk_elem[8]);
646 return -1;
647 }
648
649 if (aes_unwrap(sm->ptk.kek, sm->ptk.kek_len, WPA_IGTK_LEN / 8,
650 igtk_elem + 9, igtk)) {
651 wpa_printf(MSG_WARNING, "FT: AES unwrap failed - could not "
652 "decrypt IGTK");
653 return -1;
654 }
655
656 /* KeyID[2] | IPN[6] | Key Length[1] | Key[16+8] */
657
658 keyidx = WPA_GET_LE16(igtk_elem);
659
660 wpa_hexdump_key(MSG_DEBUG, "FT: IGTK from Reassoc Resp", igtk,
661 WPA_IGTK_LEN);
662 if (wpa_sm_set_key(sm, WPA_ALG_IGTK, broadcast_ether_addr, keyidx, 0,
663 igtk_elem + 2, 6, igtk, WPA_IGTK_LEN) < 0) {
664 wpa_printf(MSG_WARNING, "WPA: Failed to set IGTK to the "
665 "driver.");
666 return -1;
667 }
668
669 return 0;
670 }
671 #endif /* CONFIG_IEEE80211W */
672
673
wpa_ft_validate_reassoc_resp(struct wpa_sm * sm,const u8 * ies,size_t ies_len,const u8 * src_addr)674 int wpa_ft_validate_reassoc_resp(struct wpa_sm *sm, const u8 *ies,
675 size_t ies_len, const u8 *src_addr)
676 {
677 struct wpa_ft_ies parse;
678 struct rsn_mdie *mdie;
679 struct rsn_ftie *ftie;
680 unsigned int count;
681 u8 mic[WPA_EAPOL_KEY_MIC_MAX_LEN];
682
683 wpa_hexdump(MSG_DEBUG, "FT: Response IEs", ies, ies_len);
684
685 if (!wpa_key_mgmt_ft(sm->key_mgmt)) {
686 wpa_printf(MSG_DEBUG, "FT: Reject FT IEs since FT is not "
687 "enabled for this connection");
688 return -1;
689 }
690
691 if (sm->ft_reassoc_completed) {
692 wpa_printf(MSG_DEBUG, "FT: Reassociation has already been completed for this FT protocol instance - ignore unexpected retransmission");
693 return 0;
694 }
695
696 if (wpa_ft_parse_ies(ies, ies_len, &parse) < 0) {
697 wpa_printf(MSG_DEBUG, "FT: Failed to parse IEs");
698 return -1;
699 }
700
701 mdie = (struct rsn_mdie *) parse.mdie;
702 if (mdie == NULL || parse.mdie_len < sizeof(*mdie) ||
703 os_memcmp(mdie->mobility_domain, sm->mobility_domain,
704 MOBILITY_DOMAIN_ID_LEN) != 0) {
705 wpa_printf(MSG_DEBUG, "FT: Invalid MDIE");
706 return -1;
707 }
708
709 ftie = (struct rsn_ftie *) parse.ftie;
710 if (ftie == NULL || parse.ftie_len < sizeof(*ftie)) {
711 wpa_printf(MSG_DEBUG, "FT: Invalid FTIE");
712 return -1;
713 }
714
715 if (os_memcmp(ftie->snonce, sm->snonce, WPA_NONCE_LEN) != 0) {
716 wpa_printf(MSG_DEBUG, "FT: SNonce mismatch in FTIE");
717 wpa_hexdump(MSG_DEBUG, "FT: Received SNonce",
718 ftie->snonce, WPA_NONCE_LEN);
719 wpa_hexdump(MSG_DEBUG, "FT: Expected SNonce",
720 sm->snonce, WPA_NONCE_LEN);
721 return -1;
722 }
723
724 if (os_memcmp(ftie->anonce, sm->anonce, WPA_NONCE_LEN) != 0) {
725 wpa_printf(MSG_DEBUG, "FT: ANonce mismatch in FTIE");
726 wpa_hexdump(MSG_DEBUG, "FT: Received ANonce",
727 ftie->anonce, WPA_NONCE_LEN);
728 wpa_hexdump(MSG_DEBUG, "FT: Expected ANonce",
729 sm->anonce, WPA_NONCE_LEN);
730 return -1;
731 }
732
733 if (parse.r0kh_id == NULL) {
734 wpa_printf(MSG_DEBUG, "FT: No R0KH-ID subelem in FTIE");
735 return -1;
736 }
737
738 if (parse.r0kh_id_len != sm->r0kh_id_len ||
739 os_memcmp_const(parse.r0kh_id, sm->r0kh_id, parse.r0kh_id_len) != 0)
740 {
741 wpa_printf(MSG_DEBUG, "FT: R0KH-ID in FTIE did not match with "
742 "the current R0KH-ID");
743 wpa_hexdump(MSG_DEBUG, "FT: R0KH-ID in FTIE",
744 parse.r0kh_id, parse.r0kh_id_len);
745 wpa_hexdump(MSG_DEBUG, "FT: The current R0KH-ID",
746 sm->r0kh_id, sm->r0kh_id_len);
747 return -1;
748 }
749
750 if (parse.r1kh_id == NULL) {
751 wpa_printf(MSG_DEBUG, "FT: No R1KH-ID subelem in FTIE");
752 return -1;
753 }
754
755 if (os_memcmp_const(parse.r1kh_id, sm->r1kh_id, FT_R1KH_ID_LEN) != 0) {
756 wpa_printf(MSG_DEBUG, "FT: Unknown R1KH-ID used in "
757 "ReassocResp");
758 return -1;
759 }
760
761 if (parse.rsn_pmkid == NULL ||
762 os_memcmp_const(parse.rsn_pmkid, sm->pmk_r1_name, WPA_PMK_NAME_LEN))
763 {
764 wpa_printf(MSG_DEBUG, "FT: No matching PMKR1Name (PMKID) in "
765 "RSNIE (pmkid=%d)", !!parse.rsn_pmkid);
766 return -1;
767 }
768
769 count = 3;
770 if (parse.ric)
771 count += ieee802_11_ie_count(parse.ric, parse.ric_len);
772 if (ftie->mic_control[1] != count) {
773 wpa_printf(MSG_DEBUG, "FT: Unexpected IE count in MIC "
774 "Control: received %u expected %u",
775 ftie->mic_control[1], count);
776 return -1;
777 }
778
779 if (wpa_ft_mic(sm->ptk.kck, sm->ptk.kck_len, sm->own_addr, src_addr, 6,
780 parse.mdie - 2, parse.mdie_len + 2,
781 parse.ftie - 2, parse.ftie_len + 2,
782 parse.rsn - 2, parse.rsn_len + 2,
783 parse.ric, parse.ric_len,
784 mic) < 0) {
785 wpa_printf(MSG_DEBUG, "FT: Failed to calculate MIC");
786 return -1;
787 }
788
789 if (os_memcmp_const(mic, ftie->mic, 16) != 0) {
790 wpa_printf(MSG_DEBUG, "FT: Invalid MIC in FTIE");
791 wpa_hexdump(MSG_MSGDUMP, "FT: Received MIC", ftie->mic, 16);
792 wpa_hexdump(MSG_MSGDUMP, "FT: Calculated MIC", mic, 16);
793 return -1;
794 }
795
796 sm->ft_reassoc_completed = 1;
797
798 if (wpa_ft_process_gtk_subelem(sm, parse.gtk, parse.gtk_len) < 0)
799 return -1;
800
801 #ifdef CONFIG_IEEE80211W
802 if (wpa_ft_process_igtk_subelem(sm, parse.igtk, parse.igtk_len) < 0)
803 return -1;
804 #endif /* CONFIG_IEEE80211W */
805
806 if (sm->set_ptk_after_assoc) {
807 wpa_printf(MSG_DEBUG, "FT: Try to set PTK again now that we "
808 "are associated");
809 if (wpa_ft_install_ptk(sm, src_addr) < 0)
810 return -1;
811 sm->set_ptk_after_assoc = 0;
812 }
813
814 if (parse.ric) {
815 wpa_hexdump(MSG_MSGDUMP, "FT: RIC Response",
816 parse.ric, parse.ric_len);
817 /* TODO: parse response and inform driver about results when
818 * using wpa_supplicant SME */
819 }
820
821 wpa_printf(MSG_DEBUG, "FT: Completed successfully");
822
823 return 0;
824 }
825
826
827 /**
828 * wpa_ft_start_over_ds - Generate over-the-DS auth request
829 * @sm: Pointer to WPA state machine data from wpa_sm_init()
830 * @target_ap: Target AP Address
831 * @mdie: Mobility Domain IE from the target AP
832 * Returns: 0 on success, -1 on failure
833 */
wpa_ft_start_over_ds(struct wpa_sm * sm,const u8 * target_ap,const u8 * mdie)834 int wpa_ft_start_over_ds(struct wpa_sm *sm, const u8 *target_ap,
835 const u8 *mdie)
836 {
837 u8 *ft_ies;
838 size_t ft_ies_len;
839
840 wpa_printf(MSG_DEBUG, "FT: Request over-the-DS with " MACSTR,
841 MAC2STR(target_ap));
842
843 /* Generate a new SNonce */
844 if (random_get_bytes(sm->snonce, WPA_NONCE_LEN)) {
845 wpa_printf(MSG_INFO, "FT: Failed to generate a new SNonce");
846 return -1;
847 }
848
849 ft_ies = wpa_ft_gen_req_ies(sm, &ft_ies_len, NULL, sm->pmk_r0_name,
850 NULL, 0, target_ap, NULL, 0, mdie);
851 if (ft_ies) {
852 sm->over_the_ds_in_progress = 1;
853 os_memcpy(sm->target_ap, target_ap, ETH_ALEN);
854 wpa_sm_send_ft_action(sm, 1, target_ap, ft_ies, ft_ies_len);
855 os_free(ft_ies);
856 }
857
858 return 0;
859 }
860
861 #endif /* CONFIG_IEEE80211R */
862