FT: Add RIC Request processing and RIC Response generation
[wpasupplicant] / 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 "ieee802_11_defs.h"
23 #include "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)
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, sizeof(*ptk), ptk_name);
55         wpa_hexdump_key(MSG_DEBUG, "FT: PTK", (u8 *) ptk, sizeof(*ptk));
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;
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         wpa_pmk_r1_to_ptk(sm->pmk_r1, sm->snonce, ftie->anonce, sm->own_addr,
615                           bssid, sm->pmk_r1_name,
616                           (u8 *) &sm->ptk, sizeof(sm->ptk), ptk_name);
617         wpa_hexdump_key(MSG_DEBUG, "FT: PTK",
618                         (u8 *) &sm->ptk, sizeof(sm->ptk));
619         wpa_hexdump(MSG_DEBUG, "FT: PTKName", ptk_name, WPA_PMK_NAME_LEN);
620
621         ft_ies = wpa_ft_gen_req_ies(sm, &ft_ies_len, ftie->anonce,
622                                     sm->pmk_r1_name, sm->ptk.kck, bssid,
623                                     ric_ies, ric_ies_len);
624         if (ft_ies) {
625                 wpa_sm_update_ft_ies(sm, sm->mobility_domain,
626                                      ft_ies, ft_ies_len);
627                 os_free(ft_ies);
628         }
629
630         ret = wpa_ft_install_ptk(sm, bssid);
631
632         if (ret == 0) {
633                 sm->ft_completed = 1;
634                 if (ft_action) {
635                         /* TODO: trigger re-association to the Target AP;
636                          * MLME is now doing this automatically, but it should
637                          * really be done only if we get here successfully. */
638                         os_memcpy(sm->bssid, target_ap, ETH_ALEN);
639                 }
640         }
641
642         return ret;
643 }
644
645
646 int wpa_ft_is_completed(struct wpa_sm *sm)
647 {
648         if (sm == NULL)
649                 return 0;
650
651         if (sm->key_mgmt != WPA_KEY_MGMT_FT_IEEE8021X &&
652             sm->key_mgmt != WPA_KEY_MGMT_FT_PSK)
653                 return 0;
654
655         return sm->ft_completed;
656 }
657
658
659 static int wpa_ft_process_gtk_subelem(struct wpa_sm *sm, const u8 *gtk_elem,
660                                       size_t gtk_elem_len)
661 {
662         u8 gtk[32];
663         int keyidx;
664         wpa_alg alg;
665         size_t gtk_len, keylen, rsc_len;
666
667         if (gtk_elem == NULL) {
668                 wpa_printf(MSG_DEBUG, "FT: No GTK included in FTIE");
669                 return 0;
670         }
671
672         wpa_hexdump_key(MSG_DEBUG, "FT: Received GTK in Reassoc Resp",
673                         gtk_elem, gtk_elem_len);
674
675         if (gtk_elem_len < 10 + 24 || (gtk_elem_len - 10) % 8 ||
676             gtk_elem_len - 18 > sizeof(gtk)) {
677                 wpa_printf(MSG_DEBUG, "FT: Invalid GTK sub-elem "
678                            "length %lu", (unsigned long) gtk_elem_len);
679                 return -1;
680         }
681         gtk_len = gtk_elem_len - 18;
682         if (aes_unwrap(sm->ptk.kek, gtk_len / 8, gtk_elem + 10, gtk)) {
683                 wpa_printf(MSG_WARNING, "FT: AES unwrap failed - could not "
684                            "decrypt GTK");
685                 return -1;
686         }
687
688         switch (sm->group_cipher) {
689         case WPA_CIPHER_CCMP:
690                 keylen = 16;
691                 rsc_len = 6;
692                 alg = WPA_ALG_CCMP;
693                 break;
694         case WPA_CIPHER_TKIP:
695                 keylen = 32;
696                 rsc_len = 6;
697                 alg = WPA_ALG_TKIP;
698                 break;
699         case WPA_CIPHER_WEP104:
700                 keylen = 13;
701                 rsc_len = 0;
702                 alg = WPA_ALG_WEP;
703                 break;
704         case WPA_CIPHER_WEP40:
705                 keylen = 5;
706                 rsc_len = 0;
707                 alg = WPA_ALG_WEP;
708                 break;
709         default:
710                 wpa_printf(MSG_WARNING, "WPA: Unsupported Group Cipher %d",
711                            sm->group_cipher);
712                 return -1;
713         }
714
715         if (gtk_len < keylen) {
716                 wpa_printf(MSG_DEBUG, "FT: Too short GTK in FTIE");
717                 return -1;
718         }
719
720         /* Key Info[1] | Key Length[1] | RSC[8] | Key[5..32]. */
721
722         keyidx = gtk_elem[0] & 0x03;
723
724         if (gtk_elem[1] != keylen) {
725                 wpa_printf(MSG_DEBUG, "FT: GTK length mismatch: received %d "
726                            "negotiated %lu",
727                            gtk_elem[1], (unsigned long) keylen);
728                 return -1;
729         }
730
731         wpa_hexdump_key(MSG_DEBUG, "FT: GTK from Reassoc Resp", gtk, keylen);
732         if (wpa_sm_set_key(sm, alg, (u8 *) "\xff\xff\xff\xff\xff\xff",
733                            keyidx, 0, gtk_elem + 2, rsc_len, gtk, keylen) <
734             0) {
735                 wpa_printf(MSG_WARNING, "WPA: Failed to set GTK to the "
736                            "driver.");
737                 return -1;
738         }
739
740         return 0;
741 }
742
743
744 #ifdef CONFIG_IEEE80211W
745 static int wpa_ft_process_igtk_subelem(struct wpa_sm *sm, const u8 *igtk_elem,
746                                        size_t igtk_elem_len)
747 {
748         u8 igtk[WPA_IGTK_LEN];
749         u16 keyidx;
750
751         if (sm->mgmt_group_cipher != WPA_CIPHER_AES_128_CMAC)
752                 return 0;
753
754         if (igtk_elem == NULL) {
755                 wpa_printf(MSG_DEBUG, "FT: No IGTK included in FTIE");
756                 return 0;
757         }
758
759         wpa_hexdump_key(MSG_DEBUG, "FT: Received IGTK in Reassoc Resp",
760                         igtk_elem, igtk_elem_len);
761
762         if (igtk_elem_len != 2 + 6 + 1 + WPA_IGTK_LEN + 8) {
763                 wpa_printf(MSG_DEBUG, "FT: Invalid IGTK sub-elem "
764                            "length %lu", (unsigned long) igtk_elem_len);
765                 return -1;
766         }
767         if (igtk_elem[8] != WPA_IGTK_LEN) {
768                 wpa_printf(MSG_DEBUG, "FT: Invalid IGTK sub-elem Key Length "
769                            "%d", igtk_elem[8]);
770                 return -1;
771         }
772
773         if (aes_unwrap(sm->ptk.kek, WPA_IGTK_LEN / 8, igtk_elem + 9, igtk)) {
774                 wpa_printf(MSG_WARNING, "FT: AES unwrap failed - could not "
775                            "decrypt IGTK");
776                 return -1;
777         }
778
779         /* KeyID[2] | IPN[6] | Key Length[1] | Key[16+8] */
780
781         keyidx = WPA_GET_LE16(igtk_elem);
782
783         wpa_hexdump_key(MSG_DEBUG, "FT: IGTK from Reassoc Resp", igtk,
784                         WPA_IGTK_LEN);
785         if (wpa_sm_set_key(sm, WPA_ALG_IGTK, (u8 *) "\xff\xff\xff\xff\xff\xff",
786                            keyidx, 0, igtk_elem + 2, 6, igtk, WPA_IGTK_LEN) <
787             0) {
788                 wpa_printf(MSG_WARNING, "WPA: Failed to set IGTK to the "
789                            "driver.");
790                 return -1;
791         }
792
793         return 0;
794 }
795 #endif /* CONFIG_IEEE80211W */
796
797
798 int wpa_ft_validate_reassoc_resp(struct wpa_sm *sm, const u8 *ies,
799                                  size_t ies_len, const u8 *src_addr)
800 {
801         struct wpa_ft_ies parse;
802         struct rsn_mdie *mdie;
803         struct rsn_ftie *ftie;
804         size_t count;
805         u8 mic[16];
806
807         wpa_hexdump(MSG_DEBUG, "FT: Response IEs", ies, ies_len);
808
809         if (sm->key_mgmt != WPA_KEY_MGMT_FT_IEEE8021X &&
810             sm->key_mgmt != WPA_KEY_MGMT_FT_PSK) {
811                 wpa_printf(MSG_DEBUG, "FT: Reject FT IEs since FT is not "
812                            "enabled for this connection");
813                 return -1;
814         }
815
816         if (wpa_ft_parse_ies(ies, ies_len, &parse) < 0) {
817                 wpa_printf(MSG_DEBUG, "FT: Failed to parse IEs");
818                 return -1;
819         }
820
821         mdie = (struct rsn_mdie *) parse.mdie;
822         if (mdie == NULL || parse.mdie_len < sizeof(*mdie) ||
823             os_memcmp(mdie->mobility_domain, sm->mobility_domain,
824                       MOBILITY_DOMAIN_ID_LEN) != 0) {
825                 wpa_printf(MSG_DEBUG, "FT: Invalid MDIE");
826                 return -1;
827         }
828
829         ftie = (struct rsn_ftie *) parse.ftie;
830         if (ftie == NULL || parse.ftie_len < sizeof(*ftie)) {
831                 wpa_printf(MSG_DEBUG, "FT: Invalid FTIE");
832                 return -1;
833         }
834
835         if (parse.r0kh_id == NULL) {
836                 wpa_printf(MSG_DEBUG, "FT: No R0KH-ID subelem in FTIE");
837                 return -1;
838         }
839
840         if (parse.r0kh_id_len != sm->r0kh_id_len ||
841             os_memcmp(parse.r0kh_id, sm->r0kh_id, parse.r0kh_id_len) != 0) {
842                 wpa_printf(MSG_DEBUG, "FT: R0KH-ID in FTIE did not match with "
843                            "the current R0KH-ID");
844                 wpa_hexdump(MSG_DEBUG, "FT: R0KH-ID in FTIE",
845                             parse.r0kh_id, parse.r0kh_id_len);
846                 wpa_hexdump(MSG_DEBUG, "FT: The current R0KH-ID",
847                             sm->r0kh_id, sm->r0kh_id_len);
848                 return -1;
849         }
850
851         if (parse.r1kh_id == NULL) {
852                 wpa_printf(MSG_DEBUG, "FT: No R1KH-ID subelem in FTIE");
853                 return -1;
854         }
855
856         if (os_memcmp(parse.r1kh_id, sm->r1kh_id, FT_R1KH_ID_LEN) != 0) {
857                 wpa_printf(MSG_DEBUG, "FT: Unknown R1KH-ID used in "
858                            "ReassocResp");
859                 return -1;
860         }
861
862         if (parse.rsn_pmkid == NULL ||
863             os_memcmp(parse.rsn_pmkid, sm->pmk_r1_name, WPA_PMK_NAME_LEN)) {
864                 wpa_printf(MSG_DEBUG, "FT: No matching PMKR1Name (PMKID) in "
865                            "RSNIE (pmkid=%d)", !!parse.rsn_pmkid);
866                 return -1;
867         }
868
869         count = 3;
870         if (parse.tie)
871                 count++;
872
873         if (wpa_ft_mic(sm->ptk.kck, sm->own_addr, src_addr, 6,
874                        parse.mdie - 2, parse.mdie_len + 2,
875                        parse.ftie - 2, parse.ftie_len + 2,
876                        parse.rsn - 2, parse.rsn_len + 2,
877                        parse.ric, parse.ric_len,
878                        mic) < 0) {
879                 wpa_printf(MSG_DEBUG, "FT: Failed to calculate MIC");
880                 return -1;
881         }
882
883         if (os_memcmp(mic, ftie->mic, 16) != 0) {
884                 wpa_printf(MSG_DEBUG, "FT: Invalid MIC in FTIE");
885                 wpa_hexdump(MSG_MSGDUMP, "FT: Received MIC", ftie->mic, 16);
886                 wpa_hexdump(MSG_MSGDUMP, "FT: Calculated MIC", mic, 16);
887                 return -1;
888         }
889
890         if (wpa_ft_process_gtk_subelem(sm, parse.gtk, parse.gtk_len) < 0)
891                 return -1;
892
893 #ifdef CONFIG_IEEE80211W
894         if (wpa_ft_process_igtk_subelem(sm, parse.igtk, parse.igtk_len) < 0)
895                 return -1;
896 #endif /* CONFIG_IEEE80211W */
897
898         if (parse.ric) {
899                 wpa_hexdump(MSG_MSGDUMP, "FT: RIC Response",
900                             parse.ric, parse.ric_len);
901                 /* TODO: parse response and inform driver about results */
902         }
903
904         return 0;
905 }
906
907
908 /**
909  * wpa_ft_start_over_ds - Generate over-the-DS auth request
910  * @sm: Pointer to WPA state machine data from wpa_sm_init()
911  * Returns: 0 on success, -1 on failure
912  */
913 int wpa_ft_start_over_ds(struct wpa_sm *sm, const u8 *target_ap)
914 {
915         u8 *ft_ies;
916         size_t ft_ies_len;
917
918         wpa_printf(MSG_DEBUG, "FT: Request over-the-DS with " MACSTR,
919                    MAC2STR(target_ap));
920
921         /* Generate a new SNonce */
922         if (os_get_random(sm->snonce, WPA_NONCE_LEN)) {
923                 wpa_printf(MSG_INFO, "FT: Failed to generate a new SNonce");
924                 return -1;
925         }
926
927         ft_ies = wpa_ft_gen_req_ies(sm, &ft_ies_len, NULL, sm->pmk_r0_name,
928                                     NULL, target_ap, NULL, 0);
929         if (ft_ies) {
930                 sm->over_the_ds_in_progress = 1;
931                 os_memcpy(sm->target_ap, target_ap, ETH_ALEN);
932                 wpa_sm_send_ft_action(sm, 1, target_ap, ft_ies, ft_ies_len);
933                 os_free(ft_ies);
934         }
935
936         return 0;
937 }
938
939 #endif /* CONFIG_IEEE80211R */