Expand PMF_FN_* macros.
[netbsd-mini2440.git] / dist / wpa / src / rsn_supp / wpa_ft.c
blobb2ffd5594e352565a43e1dfe9cae8c84100e1fc6
1 /*
2 * WPA Supplicant - IEEE 802.11r - Fast BSS Transition
3 * Copyright (c) 2006-2007, Jouni Malinen <j@w1.fi>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
9 * Alternatively, this software may be distributed under the terms of BSD
10 * license.
12 * See README and COPYING for more details.
15 #include "includes.h"
17 #include "common.h"
18 #include "wpa.h"
19 #include "wpa_i.h"
20 #include "wpa_ie.h"
21 #include "aes_wrap.h"
22 #include "ieee802_11_defs.h"
24 #ifdef CONFIG_IEEE80211R
26 int wpa_derive_ptk_ft(struct wpa_sm *sm, const unsigned char *src_addr,
27 const struct wpa_eapol_key *key,
28 struct wpa_ptk *ptk)
30 u8 pmk_r1_name[WPA_PMK_NAME_LEN];
31 u8 ptk_name[WPA_PMK_NAME_LEN];
32 const u8 *anonce = key->key_nonce;
34 if (sm->xxkey_len == 0) {
35 wpa_printf(MSG_DEBUG, "FT: XXKey not available for key "
36 "derivation");
37 return -1;
40 wpa_derive_pmk_r0(sm->xxkey, sm->xxkey_len, sm->ssid,
41 sm->ssid_len, sm->mobility_domain,
42 sm->r0kh_id, sm->r0kh_id_len, sm->own_addr,
43 sm->pmk_r0, sm->pmk_r0_name);
44 wpa_hexdump_key(MSG_DEBUG, "FT: PMK-R0", sm->pmk_r0, PMK_LEN);
45 wpa_hexdump(MSG_DEBUG, "FT: PMKR0Name",
46 sm->pmk_r0_name, WPA_PMK_NAME_LEN);
47 wpa_derive_pmk_r1(sm->pmk_r0, sm->pmk_r0_name, sm->r1kh_id,
48 sm->own_addr, sm->pmk_r1, pmk_r1_name);
49 wpa_hexdump_key(MSG_DEBUG, "FT: PMK-R1", sm->pmk_r1, PMK_LEN);
50 wpa_hexdump(MSG_DEBUG, "FT: PMKR1Name", pmk_r1_name, WPA_PMK_NAME_LEN);
51 wpa_pmk_r1_to_ptk(sm->pmk_r1, sm->snonce, anonce, sm->own_addr,
52 sm->bssid, pmk_r1_name,
53 (u8 *) ptk, sizeof(*ptk), ptk_name);
54 wpa_hexdump_key(MSG_DEBUG, "FT: PTK", (u8 *) ptk, sizeof(*ptk));
55 wpa_hexdump(MSG_DEBUG, "FT: PTKName", ptk_name, WPA_PMK_NAME_LEN);
57 return 0;
61 /**
62 * wpa_sm_set_ft_params - Set FT (IEEE 802.11r) parameters
63 * @sm: Pointer to WPA state machine data from wpa_sm_init()
64 * @mobility_domain: Mobility domain identifier (2 octets)
65 * @r0kh_id: PMK-R0 key holder identity (1-48 octets)
66 * @r0kh_id_len: R0KH-ID length (1-48)
67 * @r1kh_id: PMK-R1 key holder identity (16 octets)
68 * Returns: 0 on success, -1 on failure
70 int wpa_sm_set_ft_params(struct wpa_sm *sm, const u8 *mobility_domain,
71 const u8 *r0kh_id, size_t r0kh_id_len,
72 const u8 *r1kh_id)
74 if (sm && mobility_domain) {
75 wpa_hexdump(MSG_DEBUG, "FT: Mobility domain",
76 mobility_domain, MOBILITY_DOMAIN_ID_LEN);
77 os_memcpy(sm->mobility_domain, mobility_domain,
78 MOBILITY_DOMAIN_ID_LEN);
79 } else if (sm)
80 os_memset(sm->mobility_domain, 0, MOBILITY_DOMAIN_ID_LEN);
82 if (sm && r0kh_id) {
83 if (r0kh_id_len > FT_R0KH_ID_MAX_LEN)
84 return -1;
85 wpa_hexdump(MSG_DEBUG, "FT: R0KH-ID", r0kh_id, r0kh_id_len);
86 os_memcpy(sm->r0kh_id, r0kh_id, r0kh_id_len);
87 sm->r0kh_id_len = r0kh_id_len;
88 } else if (sm) {
89 /* FIX: When should R0KH-ID be cleared? We need to keep the
90 * old R0KH-ID in order to be able to use this during FT. */
92 * os_memset(sm->r0kh_id, 0, FT_R0KH_ID_LEN);
93 * sm->r0kh_id_len = 0;
97 if (sm && r1kh_id) {
98 wpa_hexdump(MSG_DEBUG, "FT: R1KH-ID", r1kh_id, FT_R1KH_ID_LEN);
99 os_memcpy(sm->r1kh_id, r1kh_id, FT_R1KH_ID_LEN);
100 } else if (sm)
101 os_memset(sm->r1kh_id, 0, FT_R1KH_ID_LEN);
103 return 0;
108 * wpa_ft_gen_req_ies - Generate FT (IEEE 802.11r) IEs for Auth Request
109 * @sm: Pointer to WPA state machine data from wpa_sm_init()
110 * @len: Buffer for returning the length of the IEs
111 * @anonce: ANonce or %NULL if not yet available
112 * @pmk_name: PMKR0Name or PMKR1Name to be added into the RSN IE PMKID List
113 * @kck: 128-bit KCK for MIC or %NULL if no MIC is used
114 * @target_ap: Target AP address
115 * Returns: Pointer to buffer with IEs or %NULL on failure
117 * Caller is responsible for freeing the returned buffer with os_free();
119 static u8 * wpa_ft_gen_req_ies(struct wpa_sm *sm, size_t *len,
120 const u8 *anonce, const u8 *pmk_name,
121 const u8 *kck, const u8 *target_ap)
123 size_t buf_len;
124 u8 *buf, *pos, *ftie_len, *ftie_pos;
125 struct rsn_mdie *mdie;
126 struct rsn_ftie *ftie;
127 struct rsn_ie_hdr *rsnie;
128 u16 capab;
130 sm->ft_completed = 0;
132 buf_len = 2 + sizeof(struct rsn_mdie) + 2 + sizeof(struct rsn_ftie) +
133 2 + sm->r0kh_id_len + 100;
134 buf = os_zalloc(buf_len);
135 if (buf == NULL)
136 return NULL;
137 pos = buf;
139 /* RSNIE[PMKR0Name] */
140 rsnie = (struct rsn_ie_hdr *) pos;
141 rsnie->elem_id = WLAN_EID_RSN;
142 WPA_PUT_LE16(rsnie->version, RSN_VERSION);
143 pos = (u8 *) (rsnie + 1);
145 /* Group Suite Selector */
146 if (sm->group_cipher == WPA_CIPHER_CCMP)
147 RSN_SELECTOR_PUT(pos, RSN_CIPHER_SUITE_CCMP);
148 else if (sm->group_cipher == WPA_CIPHER_TKIP)
149 RSN_SELECTOR_PUT(pos, RSN_CIPHER_SUITE_TKIP);
150 else {
151 wpa_printf(MSG_WARNING, "FT: Invalid group cipher (%d)",
152 sm->group_cipher);
153 os_free(buf);
154 return NULL;
156 pos += RSN_SELECTOR_LEN;
158 /* Pairwise Suite Count */
159 WPA_PUT_LE16(pos, 1);
160 pos += 2;
162 /* Pairwise Suite List */
163 if (sm->pairwise_cipher == WPA_CIPHER_CCMP)
164 RSN_SELECTOR_PUT(pos, RSN_CIPHER_SUITE_CCMP);
165 else if (sm->pairwise_cipher == WPA_CIPHER_TKIP)
166 RSN_SELECTOR_PUT(pos, RSN_CIPHER_SUITE_TKIP);
167 else {
168 wpa_printf(MSG_WARNING, "FT: Invalid pairwise cipher (%d)",
169 sm->pairwise_cipher);
170 os_free(buf);
171 return NULL;
173 pos += RSN_SELECTOR_LEN;
175 /* Authenticated Key Management Suite Count */
176 WPA_PUT_LE16(pos, 1);
177 pos += 2;
179 /* Authenticated Key Management Suite List */
180 if (sm->key_mgmt == WPA_KEY_MGMT_FT_IEEE8021X)
181 RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_FT_802_1X);
182 else if (sm->key_mgmt == WPA_KEY_MGMT_FT_PSK)
183 RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_FT_PSK);
184 else {
185 wpa_printf(MSG_WARNING, "FT: Invalid key management type (%d)",
186 sm->key_mgmt);
187 os_free(buf);
188 return NULL;
190 pos += RSN_SELECTOR_LEN;
192 /* RSN Capabilities */
193 capab = 0;
194 #ifdef CONFIG_IEEE80211W
195 if (sm->mgmt_group_cipher == WPA_CIPHER_AES_128_CMAC)
196 capab |= WPA_CAPABILITY_MGMT_FRAME_PROTECTION;
197 #endif /* CONFIG_IEEE80211W */
198 WPA_PUT_LE16(pos, capab);
199 pos += 2;
201 /* PMKID Count */
202 WPA_PUT_LE16(pos, 1);
203 pos += 2;
205 /* PMKID List [PMKR0Name/PMKR1Name] */
206 os_memcpy(pos, pmk_name, WPA_PMK_NAME_LEN);
207 pos += WPA_PMK_NAME_LEN;
209 #ifdef CONFIG_IEEE80211W
210 if (sm->mgmt_group_cipher == WPA_CIPHER_AES_128_CMAC) {
211 /* Management Group Cipher Suite */
212 RSN_SELECTOR_PUT(pos, RSN_CIPHER_SUITE_AES_128_CMAC);
213 pos += RSN_SELECTOR_LEN;
215 #endif /* CONFIG_IEEE80211W */
217 rsnie->len = (pos - (u8 *) rsnie) - 2;
219 /* MDIE */
220 *pos++ = WLAN_EID_MOBILITY_DOMAIN;
221 *pos++ = sizeof(*mdie);
222 mdie = (struct rsn_mdie *) pos;
223 pos += sizeof(*mdie);
224 os_memcpy(mdie->mobility_domain, sm->mobility_domain,
225 MOBILITY_DOMAIN_ID_LEN);
226 mdie->ft_capab = 0; /* FIX: copy from the target AP's MDIE */
228 /* FTIE[SNonce, R0KH-ID] */
229 ftie_pos = pos;
230 *pos++ = WLAN_EID_FAST_BSS_TRANSITION;
231 ftie_len = pos++;
232 ftie = (struct rsn_ftie *) pos;
233 pos += sizeof(*ftie);
234 os_memcpy(ftie->snonce, sm->snonce, WPA_NONCE_LEN);
235 if (anonce)
236 os_memcpy(ftie->anonce, anonce, WPA_NONCE_LEN);
237 /* R0KH-ID sub-element */
238 *pos++ = FTIE_SUBELEM_R0KH_ID;
239 *pos++ = sm->r0kh_id_len;
240 os_memcpy(pos, sm->r0kh_id, sm->r0kh_id_len);
241 pos += sm->r0kh_id_len;
242 *ftie_len = pos - ftie_len - 1;
244 if (kck) {
246 * IEEE 802.11r/D9.0, 11A.8.4
247 * MIC shall be calculated over:
248 * non-AP STA MAC address
249 * Target AP MAC address
250 * Transaction seq number (5 for ReassocReq, 3 otherwise)
251 * RSN IE
252 * MDIE
253 * FTIE (with MIC field set to 0)
254 * RIC-Request (if present)
256 ftie->mic_control[1] = 3; /* Information element count */
257 if (wpa_ft_mic(kck, sm->own_addr, target_ap, 5,
258 ((u8 *) mdie) - 2, 2 + sizeof(*mdie),
259 ftie_pos, 2 + *ftie_len,
260 (u8 *) rsnie, 2 + rsnie->len, NULL, 0,
261 ftie->mic) < 0) {
262 wpa_printf(MSG_INFO, "FT: Failed to calculate MIC");
263 os_free(buf);
264 return NULL;
268 *len = pos - buf;
270 return buf;
274 struct wpa_ft_ies {
275 const u8 *mdie;
276 size_t mdie_len;
277 const u8 *ftie;
278 size_t ftie_len;
279 const u8 *r1kh_id;
280 const u8 *gtk;
281 size_t gtk_len;
282 const u8 *r0kh_id;
283 size_t r0kh_id_len;
284 const u8 *rsn;
285 size_t rsn_len;
286 const u8 *rsn_pmkid;
287 const u8 *tie;
288 size_t tie_len;
292 static int wpa_ft_parse_ftie(const u8 *ie, size_t ie_len,
293 struct wpa_ft_ies *parse)
295 const u8 *end, *pos;
297 parse->ftie = ie;
298 parse->ftie_len = ie_len;
300 pos = ie + sizeof(struct rsn_ftie);
301 end = ie + ie_len;
303 while (pos + 2 <= end && pos + 2 + pos[1] <= end) {
304 switch (pos[0]) {
305 case FTIE_SUBELEM_R1KH_ID:
306 if (pos[1] != FT_R1KH_ID_LEN) {
307 wpa_printf(MSG_DEBUG, "FT: Invalid R1KH-ID "
308 "length in FTIE: %d", pos[1]);
309 return -1;
311 parse->r1kh_id = pos + 2;
312 break;
313 case FTIE_SUBELEM_GTK:
314 parse->gtk = pos + 2;
315 parse->gtk_len = pos[1];
316 break;
317 case FTIE_SUBELEM_R0KH_ID:
318 if (pos[1] < 1 || pos[1] > FT_R0KH_ID_MAX_LEN) {
319 wpa_printf(MSG_DEBUG, "FT: Invalid R0KH-ID "
320 "length in FTIE: %d", pos[1]);
321 return -1;
323 parse->r0kh_id = pos + 2;
324 parse->r0kh_id_len = pos[1];
325 break;
328 pos += 2 + pos[1];
331 return 0;
335 static int wpa_ft_parse_ies(const u8 *ies, size_t ies_len,
336 struct wpa_ft_ies *parse)
338 const u8 *end, *pos;
339 struct wpa_ie_data data;
340 int ret;
342 os_memset(parse, 0, sizeof(*parse));
343 if (ies == NULL)
344 return 0;
346 pos = ies;
347 end = ies + ies_len;
348 while (pos + 2 <= end && pos + 2 + pos[1] <= end) {
349 switch (pos[0]) {
350 case WLAN_EID_RSN:
351 parse->rsn = pos + 2;
352 parse->rsn_len = pos[1];
353 ret = wpa_parse_wpa_ie_rsn(parse->rsn - 2,
354 parse->rsn_len + 2,
355 &data);
356 if (ret < 0) {
357 wpa_printf(MSG_DEBUG, "FT: Failed to parse "
358 "RSN IE: %d", ret);
359 return -1;
361 if (data.num_pmkid == 1 && data.pmkid)
362 parse->rsn_pmkid = data.pmkid;
363 break;
364 case WLAN_EID_MOBILITY_DOMAIN:
365 parse->mdie = pos + 2;
366 parse->mdie_len = pos[1];
367 break;
368 case WLAN_EID_FAST_BSS_TRANSITION:
369 if (wpa_ft_parse_ftie(pos + 2, pos[1], parse) < 0)
370 return -1;
371 break;
372 case WLAN_EID_TIMEOUT_INTERVAL:
373 parse->tie = pos + 2;
374 parse->tie_len = pos[1];
375 break;
378 pos += 2 + pos[1];
381 return 0;
385 static int wpa_ft_install_ptk(struct wpa_sm *sm, const u8 *bssid)
387 int keylen;
388 wpa_alg alg;
389 u8 null_rsc[6] = { 0, 0, 0, 0, 0, 0 };
391 wpa_printf(MSG_DEBUG, "FT: Installing PTK to the driver.");
393 switch (sm->pairwise_cipher) {
394 case WPA_CIPHER_CCMP:
395 alg = WPA_ALG_CCMP;
396 keylen = 16;
397 break;
398 case WPA_CIPHER_TKIP:
399 alg = WPA_ALG_TKIP;
400 keylen = 32;
401 break;
402 default:
403 wpa_printf(MSG_WARNING, "FT: Unsupported pairwise cipher %d",
404 sm->pairwise_cipher);
405 return -1;
408 if (wpa_sm_set_key(sm, alg, bssid, 0, 1, null_rsc,
409 sizeof(null_rsc), (u8 *) sm->ptk.tk1, keylen) < 0) {
410 wpa_printf(MSG_WARNING, "FT: Failed to set PTK to the driver");
411 return -1;
414 return 0;
419 * wpa_ft_prepare_auth_request - Generate over-the-air auth request
420 * @sm: Pointer to WPA state machine data from wpa_sm_init()
421 * Returns: 0 on success, -1 on failure
423 int wpa_ft_prepare_auth_request(struct wpa_sm *sm)
425 u8 *ft_ies;
426 size_t ft_ies_len;
428 /* Generate a new SNonce */
429 if (os_get_random(sm->snonce, WPA_NONCE_LEN)) {
430 wpa_printf(MSG_INFO, "FT: Failed to generate a new SNonce");
431 return -1;
434 ft_ies = wpa_ft_gen_req_ies(sm, &ft_ies_len, NULL, sm->pmk_r0_name,
435 NULL, sm->bssid);
436 if (ft_ies) {
437 wpa_sm_update_ft_ies(sm, sm->mobility_domain,
438 ft_ies, ft_ies_len);
439 os_free(ft_ies);
442 return 0;
446 int wpa_ft_process_response(struct wpa_sm *sm, const u8 *ies, size_t ies_len,
447 int ft_action, const u8 *target_ap)
449 u8 *ft_ies;
450 size_t ft_ies_len;
451 struct wpa_ft_ies parse;
452 struct rsn_mdie *mdie;
453 struct rsn_ftie *ftie;
454 u8 ptk_name[WPA_PMK_NAME_LEN];
455 int ret;
456 const u8 *bssid;
458 wpa_hexdump(MSG_DEBUG, "FT: Response IEs", ies, ies_len);
460 if (ft_action) {
461 if (!sm->over_the_ds_in_progress) {
462 wpa_printf(MSG_DEBUG, "FT: No over-the-DS in progress "
463 "- drop FT Action Response");
464 return -1;
467 if (os_memcmp(target_ap, sm->target_ap, ETH_ALEN) != 0) {
468 wpa_printf(MSG_DEBUG, "FT: No over-the-DS in progress "
469 "with this Target AP - drop FT Action "
470 "Response");
471 return -1;
475 if (sm->key_mgmt != WPA_KEY_MGMT_FT_IEEE8021X &&
476 sm->key_mgmt != WPA_KEY_MGMT_FT_PSK) {
477 wpa_printf(MSG_DEBUG, "FT: Reject FT IEs since FT is not "
478 "enabled for this connection");
479 return -1;
482 if (wpa_ft_parse_ies(ies, ies_len, &parse) < 0) {
483 wpa_printf(MSG_DEBUG, "FT: Failed to parse IEs");
484 return -1;
487 mdie = (struct rsn_mdie *) parse.mdie;
488 if (mdie == NULL || parse.mdie_len < sizeof(*mdie) ||
489 os_memcmp(mdie->mobility_domain, sm->mobility_domain,
490 MOBILITY_DOMAIN_ID_LEN) != 0) {
491 wpa_printf(MSG_DEBUG, "FT: Invalid MDIE");
492 return -1;
495 ftie = (struct rsn_ftie *) parse.ftie;
496 if (ftie == NULL || parse.ftie_len < sizeof(*ftie)) {
497 wpa_printf(MSG_DEBUG, "FT: Invalid FTIE");
498 return -1;
501 if (parse.r0kh_id == NULL) {
502 wpa_printf(MSG_DEBUG, "FT: No R0KH-ID subelem in FTIE");
503 return -1;
506 if (parse.r0kh_id_len != sm->r0kh_id_len ||
507 os_memcmp(parse.r0kh_id, sm->r0kh_id, parse.r0kh_id_len) != 0) {
508 wpa_printf(MSG_DEBUG, "FT: R0KH-ID in FTIE did not match with "
509 "the current R0KH-ID");
510 wpa_hexdump(MSG_DEBUG, "FT: R0KH-ID in FTIE",
511 parse.r0kh_id, parse.r0kh_id_len);
512 wpa_hexdump(MSG_DEBUG, "FT: The current R0KH-ID",
513 sm->r0kh_id, sm->r0kh_id_len);
514 return -1;
517 if (parse.r1kh_id == NULL) {
518 wpa_printf(MSG_DEBUG, "FT: No R1KH-ID subelem in FTIE");
519 return -1;
522 if (parse.rsn_pmkid == NULL ||
523 os_memcmp(parse.rsn_pmkid, sm->pmk_r0_name, WPA_PMK_NAME_LEN)) {
524 wpa_printf(MSG_DEBUG, "FT: No matching PMKR0Name (PMKID) in "
525 "RSNIE");
526 return -1;
529 os_memcpy(sm->r1kh_id, parse.r1kh_id, FT_R1KH_ID_LEN);
530 wpa_hexdump(MSG_DEBUG, "FT: R1KH-ID", sm->r1kh_id, FT_R1KH_ID_LEN);
531 wpa_hexdump(MSG_DEBUG, "FT: SNonce", sm->snonce, WPA_NONCE_LEN);
532 wpa_hexdump(MSG_DEBUG, "FT: ANonce", ftie->anonce, WPA_NONCE_LEN);
533 wpa_derive_pmk_r1(sm->pmk_r0, sm->pmk_r0_name, sm->r1kh_id,
534 sm->own_addr, sm->pmk_r1, sm->pmk_r1_name);
535 wpa_hexdump_key(MSG_DEBUG, "FT: PMK-R1", sm->pmk_r1, PMK_LEN);
536 wpa_hexdump(MSG_DEBUG, "FT: PMKR1Name",
537 sm->pmk_r1_name, WPA_PMK_NAME_LEN);
539 bssid = target_ap;
540 wpa_pmk_r1_to_ptk(sm->pmk_r1, sm->snonce, ftie->anonce, sm->own_addr,
541 bssid, sm->pmk_r1_name,
542 (u8 *) &sm->ptk, sizeof(sm->ptk), ptk_name);
543 wpa_hexdump_key(MSG_DEBUG, "FT: PTK",
544 (u8 *) &sm->ptk, sizeof(sm->ptk));
545 wpa_hexdump(MSG_DEBUG, "FT: PTKName", ptk_name, WPA_PMK_NAME_LEN);
547 ft_ies = wpa_ft_gen_req_ies(sm, &ft_ies_len, ftie->anonce,
548 sm->pmk_r1_name, sm->ptk.kck, bssid);
549 if (ft_ies) {
550 wpa_sm_update_ft_ies(sm, sm->mobility_domain,
551 ft_ies, ft_ies_len);
552 os_free(ft_ies);
555 ret = wpa_ft_install_ptk(sm, bssid);
557 if (ret == 0) {
558 sm->ft_completed = 1;
559 if (ft_action) {
560 /* TODO: trigger re-association to the Target AP;
561 * MLME is now doing this automatically, but it should
562 * really be done only if we get here successfully. */
563 os_memcpy(sm->bssid, target_ap, ETH_ALEN);
567 return ret;
571 int wpa_ft_is_completed(struct wpa_sm *sm)
573 if (sm == NULL)
574 return 0;
576 if (sm->key_mgmt != WPA_KEY_MGMT_FT_IEEE8021X &&
577 sm->key_mgmt != WPA_KEY_MGMT_FT_PSK)
578 return 0;
580 return sm->ft_completed;
584 int wpa_ft_validate_reassoc_resp(struct wpa_sm *sm, const u8 *ies,
585 size_t ies_len, const u8 *src_addr)
587 struct wpa_ft_ies parse;
588 struct rsn_mdie *mdie;
589 struct rsn_ftie *ftie;
590 size_t count, gtk_len, keylen, rsc_len;
591 u8 mic[16];
592 u8 gtk[32];
593 int keyidx;
594 wpa_alg alg;
596 wpa_hexdump(MSG_DEBUG, "FT: Response IEs", ies, ies_len);
598 if (sm->key_mgmt != WPA_KEY_MGMT_FT_IEEE8021X &&
599 sm->key_mgmt != WPA_KEY_MGMT_FT_PSK) {
600 wpa_printf(MSG_DEBUG, "FT: Reject FT IEs since FT is not "
601 "enabled for this connection");
602 return -1;
605 if (wpa_ft_parse_ies(ies, ies_len, &parse) < 0) {
606 wpa_printf(MSG_DEBUG, "FT: Failed to parse IEs");
607 return -1;
610 mdie = (struct rsn_mdie *) parse.mdie;
611 if (mdie == NULL || parse.mdie_len < sizeof(*mdie) ||
612 os_memcmp(mdie->mobility_domain, sm->mobility_domain,
613 MOBILITY_DOMAIN_ID_LEN) != 0) {
614 wpa_printf(MSG_DEBUG, "FT: Invalid MDIE");
615 return -1;
618 ftie = (struct rsn_ftie *) parse.ftie;
619 if (ftie == NULL || parse.ftie_len < sizeof(*ftie)) {
620 wpa_printf(MSG_DEBUG, "FT: Invalid FTIE");
621 return -1;
624 if (parse.r0kh_id == NULL) {
625 wpa_printf(MSG_DEBUG, "FT: No R0KH-ID subelem in FTIE");
626 return -1;
629 if (parse.r0kh_id_len != sm->r0kh_id_len ||
630 os_memcmp(parse.r0kh_id, sm->r0kh_id, parse.r0kh_id_len) != 0) {
631 wpa_printf(MSG_DEBUG, "FT: R0KH-ID in FTIE did not match with "
632 "the current R0KH-ID");
633 wpa_hexdump(MSG_DEBUG, "FT: R0KH-ID in FTIE",
634 parse.r0kh_id, parse.r0kh_id_len);
635 wpa_hexdump(MSG_DEBUG, "FT: The current R0KH-ID",
636 sm->r0kh_id, sm->r0kh_id_len);
637 return -1;
640 if (parse.r1kh_id == NULL) {
641 wpa_printf(MSG_DEBUG, "FT: No R1KH-ID subelem in FTIE");
642 return -1;
645 if (os_memcmp(parse.r1kh_id, sm->r1kh_id, FT_R1KH_ID_LEN) != 0) {
646 wpa_printf(MSG_DEBUG, "FT: Unknown R1KH-ID used in "
647 "ReassocResp");
648 return -1;
651 if (parse.rsn_pmkid == NULL ||
652 os_memcmp(parse.rsn_pmkid, sm->pmk_r1_name, WPA_PMK_NAME_LEN)) {
653 wpa_printf(MSG_DEBUG, "FT: No matching PMKR1Name (PMKID) in "
654 "RSNIE (pmkid=%d)", !!parse.rsn_pmkid);
655 return -1;
658 count = 3;
659 if (parse.tie)
660 count++;
662 if (ftie->mic_control[1] != count) {
663 wpa_printf(MSG_DEBUG, "FT: Unexpected IE count in FTIE (%d)",
664 ftie->mic_control[1]);
665 return -1;
668 if (wpa_ft_mic(sm->ptk.kck, sm->own_addr, src_addr, 6,
669 parse.mdie - 2, parse.mdie_len + 2,
670 parse.ftie - 2, parse.ftie_len + 2,
671 parse.rsn - 2, parse.rsn_len + 2, NULL, 0,
672 mic) < 0) {
673 wpa_printf(MSG_DEBUG, "FT: Failed to calculate MIC");
674 return -1;
677 if (os_memcmp(mic, ftie->mic, 16) != 0) {
678 wpa_printf(MSG_DEBUG, "FT: Invalid MIC in FTIE");
679 wpa_hexdump(MSG_MSGDUMP, "FT: Received MIC", ftie->mic, 16);
680 wpa_hexdump(MSG_MSGDUMP, "FT: Calculated MIC", mic, 16);
681 return -1;
684 if (parse.gtk == NULL) {
685 wpa_printf(MSG_DEBUG, "FT: No GTK included in FTIE");
686 return 0;
689 wpa_hexdump_key(MSG_DEBUG, "FT: Received GTK in Reassoc Resp",
690 parse.gtk, parse.gtk_len);
692 if (parse.gtk_len < 10 + 24 || (parse.gtk_len - 10) % 8 ||
693 parse.gtk_len - 18 > sizeof(gtk)) {
694 wpa_printf(MSG_DEBUG, "FT: Invalid GTK sub-elem "
695 "length %lu", (unsigned long) parse.gtk_len);
696 return -1;
698 gtk_len = parse.gtk_len - 18;
699 if (aes_unwrap(sm->ptk.kek, gtk_len / 8, parse.gtk + 10, gtk)) {
700 wpa_printf(MSG_WARNING, "FT: AES unwrap failed - could not "
701 "decrypt GTK");
702 return -1;
705 switch (sm->group_cipher) {
706 case WPA_CIPHER_CCMP:
707 keylen = 16;
708 rsc_len = 6;
709 alg = WPA_ALG_CCMP;
710 break;
711 case WPA_CIPHER_TKIP:
712 keylen = 32;
713 rsc_len = 6;
714 alg = WPA_ALG_TKIP;
715 break;
716 case WPA_CIPHER_WEP104:
717 keylen = 13;
718 rsc_len = 0;
719 alg = WPA_ALG_WEP;
720 break;
721 case WPA_CIPHER_WEP40:
722 keylen = 5;
723 rsc_len = 0;
724 alg = WPA_ALG_WEP;
725 break;
726 default:
727 wpa_printf(MSG_WARNING, "WPA: Unsupported Group Cipher %d",
728 sm->group_cipher);
729 return -1;
732 if (gtk_len < keylen) {
733 wpa_printf(MSG_DEBUG, "FT: Too short GTK in FTIE");
734 return -1;
737 /* Key Info[1] | Key Length[1] | RSC[8] | Key[5..32]. */
739 keyidx = parse.gtk[0] & 0x03;
741 if (parse.gtk[1] != keylen) {
742 wpa_printf(MSG_DEBUG, "FT: GTK length mismatch: received %d "
743 "negotiated %lu",
744 parse.gtk[1], (unsigned long) keylen);
745 return -1;
748 wpa_hexdump_key(MSG_DEBUG, "FT: GTK from Reassoc Resp", gtk, keylen);
749 if (wpa_sm_set_key(sm, alg, (u8 *) "\xff\xff\xff\xff\xff\xff",
750 keyidx, 0, parse.gtk + 2, rsc_len, gtk, keylen) < 0)
752 wpa_printf(MSG_WARNING, "WPA: Failed to set GTK to the "
753 "driver.");
754 return -1;
757 return 0;
762 * wpa_ft_start_over_ds - Generate over-the-DS auth request
763 * @sm: Pointer to WPA state machine data from wpa_sm_init()
764 * Returns: 0 on success, -1 on failure
766 int wpa_ft_start_over_ds(struct wpa_sm *sm, const u8 *target_ap)
768 u8 *ft_ies;
769 size_t ft_ies_len;
771 wpa_printf(MSG_DEBUG, "FT: Request over-the-DS with " MACSTR,
772 MAC2STR(target_ap));
774 /* Generate a new SNonce */
775 if (os_get_random(sm->snonce, WPA_NONCE_LEN)) {
776 wpa_printf(MSG_INFO, "FT: Failed to generate a new SNonce");
777 return -1;
780 ft_ies = wpa_ft_gen_req_ies(sm, &ft_ies_len, NULL, sm->pmk_r0_name,
781 NULL, target_ap);
782 if (ft_ies) {
783 sm->over_the_ds_in_progress = 1;
784 os_memcpy(sm->target_ap, target_ap, ETH_ALEN);
785 wpa_sm_send_ft_action(sm, 1, target_ap, ft_ies, ft_ies_len);
786 os_free(ft_ies);
789 return 0;
792 #endif /* CONFIG_IEEE80211R */