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