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