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