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