~ubuntu-branches/ubuntu/vivid/wpasupplicant/vivid

« back to all changes in this revision

Viewing changes to src/rsn_supp/wpa_ft.c

  • Committer: Bazaar Package Importer
  • Author(s): Kel Modderman
  • Date: 2008-03-12 20:03:04 UTC
  • mfrom: (1.1.10 upstream)
  • mto: This revision was merged to the branch mainline in revision 4.
  • Revision ID: james.westby@ubuntu.com-20080312200304-4331y9wj46pdd34z
Tags: 0.6.3-1
* New upstream release.
* Drop patches applied upstream:
  - debian/patches/30_wpa_gui_qt4_eventhistoryui_rework.patch
  - debian/patches/31_wpa_gui_qt4_eventhistory_always_scrollbar.patch
  - debian/patches/32_wpa_gui_qt4_eventhistory_scroll_with_events.patch
  - debian/patches/40_dbus_ssid_data.patch
* Tidy up the clean target of debian/rules. Now that the madwifi headers are
  handled differently we no longer need to do any cleanup.
* Fix formatting error in debian/ifupdown/wpa_action.8 to make lintian
  quieter.
* Add patch to fix formatting errors in manpages build from sgml source. Use
  <emphasis> tags to hightlight keywords instead of surrounding them in
  strong quotes.
  - debian/patches/41_manpage_format_fixes.patch
* wpasupplicant binary package no longer suggests pcscd, guessnet, iproute
  or wireless-tools, nor does it recommend dhcp3-client. These are not
  needed.
* Add debian/patches/10_silence_siocsiwauth_icotl_failure.patch to disable
  ioctl failure messages that occur under normal conditions.
* Cherry pick two upstream git commits concerning the dbus interface:
  - debian/patches/11_avoid_dbus_version_namespace.patch
  - debian/patches/12_fix_potential_use_after_free.patch
* Add debian/patches/42_manpage_explain_available_drivers.patch to explain
  that not all of the driver backends are available in the provided
  wpa_supplicant binary, and that the canonical list of supported driver
  backends can be retrieved from the wpa_supplicant -h (help) output.
  (Closes: #466910)
* Add debian/patches/20_wpa_gui_qt4_disable_link_prl.patch to remove
  link_prl CONFIG compile flag added by qmake-qt4 >= 4.3.4-2 to avoid excess
  linking.

Show diffs side-by-side

added added

removed removed

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