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