~ubuntu-branches/ubuntu/karmic/wpasupplicant/karmic

« back to all changes in this revision

Viewing changes to src/rsn_supp/wpa_ft.c

  • Committer: Bazaar Package Importer
  • Author(s): Reinhard Tartler
  • Date: 2008-12-02 20:52:16 UTC
  • mfrom: (2.1.30 intrepid)
  • Revision ID: james.westby@ubuntu.com-20081202205216-72fqozu84sdt89a8
Tags: 0.6.4-3
Bugfix: "Missing -d in testing for a directory in init script". 
Thanks to Braun Gábor <braung@renyi.hu> for reporting and the patch.
(Closes: #506328)

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, *ftie_pos;
 
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
        ftie_pos = pos;
 
230
        *pos++ = WLAN_EID_FAST_BSS_TRANSITION;
 
231
        ftie_len = pos++;
 
232
        ftie = (struct rsn_ftie *) pos;
 
233
        pos += sizeof(*ftie);
 
234
        os_memcpy(ftie->snonce, sm->snonce, WPA_NONCE_LEN);
 
235
        if (anonce)
 
236
                os_memcpy(ftie->anonce, anonce, WPA_NONCE_LEN);
 
237
        /* R0KH-ID sub-element */
 
238
        *pos++ = FTIE_SUBELEM_R0KH_ID;
 
239
        *pos++ = sm->r0kh_id_len;
 
240
        os_memcpy(pos, sm->r0kh_id, sm->r0kh_id_len);
 
241
        pos += sm->r0kh_id_len;
 
242
        *ftie_len = pos - ftie_len - 1;
 
243
 
 
244
        if (kck) {
 
245
                /*
 
246
                 * IEEE 802.11r/D9.0, 11A.8.4
 
247
                 * MIC shall be calculated over:
 
248
                 * non-AP STA MAC address
 
249
                 * Target AP MAC address
 
250
                 * Transaction seq number (5 for ReassocReq, 3 otherwise)
 
251
                 * RSN IE
 
252
                 * MDIE
 
253
                 * FTIE (with MIC field set to 0)
 
254
                 * RIC-Request (if present)
 
255
                 */
 
256
                ftie->mic_control[1] = 3; /* Information element count */
 
257
                if (wpa_ft_mic(kck, sm->own_addr, target_ap, 5,
 
258
                               ((u8 *) mdie) - 2, 2 + sizeof(*mdie),
 
259
                               ftie_pos, 2 + *ftie_len,
 
260
                               (u8 *) rsnie, 2 + rsnie->len, NULL, 0,
 
261
                               ftie->mic) < 0) {
 
262
                        wpa_printf(MSG_INFO, "FT: Failed to calculate MIC");
 
263
                        os_free(buf);
 
264
                        return NULL;
 
265
                }
 
266
        }
 
267
 
 
268
        *len = pos - buf;
 
269
 
 
270
        return buf;
 
271
}
 
272
 
 
273
 
 
274
struct wpa_ft_ies {
 
275
        const u8 *mdie;
 
276
        size_t mdie_len;
 
277
        const u8 *ftie;
 
278
        size_t ftie_len;
 
279
        const u8 *r1kh_id;
 
280
        const u8 *gtk;
 
281
        size_t gtk_len;
 
282
        const u8 *r0kh_id;
 
283
        size_t r0kh_id_len;
 
284
        const u8 *rsn;
 
285
        size_t rsn_len;
 
286
        const u8 *rsn_pmkid;
 
287
        const u8 *tie;
 
288
        size_t tie_len;
 
289
};
 
290
 
 
291
 
 
292
static int wpa_ft_parse_ftie(const u8 *ie, size_t ie_len,
 
293
                             struct wpa_ft_ies *parse)
 
294
{
 
295
        const u8 *end, *pos;
 
296
 
 
297
        parse->ftie = ie;
 
298
        parse->ftie_len = ie_len;
 
299
 
 
300
        pos = ie + sizeof(struct rsn_ftie);
 
301
        end = ie + ie_len;
 
302
 
 
303
        while (pos + 2 <= end && pos + 2 + pos[1] <= end) {
 
304
                switch (pos[0]) {
 
305
                case FTIE_SUBELEM_R1KH_ID:
 
306
                        if (pos[1] != FT_R1KH_ID_LEN) {
 
307
                                wpa_printf(MSG_DEBUG, "FT: Invalid R1KH-ID "
 
308
                                           "length in FTIE: %d", pos[1]);
 
309
                                return -1;
 
310
                        }
 
311
                        parse->r1kh_id = pos + 2;
 
312
                        break;
 
313
                case FTIE_SUBELEM_GTK:
 
314
                        parse->gtk = pos + 2;
 
315
                        parse->gtk_len = pos[1];
 
316
                        break;
 
317
                case FTIE_SUBELEM_R0KH_ID:
 
318
                        if (pos[1] < 1 || pos[1] > FT_R0KH_ID_MAX_LEN) {
 
319
                                wpa_printf(MSG_DEBUG, "FT: Invalid R0KH-ID "
 
320
                                           "length in FTIE: %d", pos[1]);
 
321
                                return -1;
 
322
                        }
 
323
                        parse->r0kh_id = pos + 2;
 
324
                        parse->r0kh_id_len = pos[1];
 
325
                        break;
 
326
                }
 
327
 
 
328
                pos += 2 + pos[1];
 
329
        }
 
330
 
 
331
        return 0;
 
332
}
 
333
 
 
334
 
 
335
static int wpa_ft_parse_ies(const u8 *ies, size_t ies_len,
 
336
                            struct wpa_ft_ies *parse)
 
337
{
 
338
        const u8 *end, *pos;
 
339
        struct wpa_ie_data data;
 
340
        int ret;
 
341
 
 
342
        os_memset(parse, 0, sizeof(*parse));
 
343
        if (ies == NULL)
 
344
                return 0;
 
345
 
 
346
        pos = ies;
 
347
        end = ies + ies_len;
 
348
        while (pos + 2 <= end && pos + 2 + pos[1] <= end) {
 
349
                switch (pos[0]) {
 
350
                case WLAN_EID_RSN:
 
351
                        parse->rsn = pos + 2;
 
352
                        parse->rsn_len = pos[1];
 
353
                        ret = wpa_parse_wpa_ie_rsn(parse->rsn - 2,
 
354
                                                   parse->rsn_len + 2,
 
355
                                                   &data);
 
356
                        if (ret < 0) {
 
357
                                wpa_printf(MSG_DEBUG, "FT: Failed to parse "
 
358
                                           "RSN IE: %d", ret);
 
359
                                return -1;
 
360
                        }
 
361
                        if (data.num_pmkid == 1 && data.pmkid)
 
362
                                parse->rsn_pmkid = data.pmkid;
 
363
                        break;
 
364
                case WLAN_EID_MOBILITY_DOMAIN:
 
365
                        parse->mdie = pos + 2;
 
366
                        parse->mdie_len = pos[1];
 
367
                        break;
 
368
                case WLAN_EID_FAST_BSS_TRANSITION:
 
369
                        if (wpa_ft_parse_ftie(pos + 2, pos[1], parse) < 0)
 
370
                                return -1;
 
371
                        break;
 
372
                case WLAN_EID_TIMEOUT_INTERVAL:
 
373
                        parse->tie = pos + 2;
 
374
                        parse->tie_len = pos[1];
 
375
                        break;
 
376
                }
 
377
 
 
378
                pos += 2 + pos[1];
 
379
        }
 
380
 
 
381
        return 0;
 
382
}
 
383
 
 
384
 
 
385
static int wpa_ft_install_ptk(struct wpa_sm *sm, const u8 *bssid)
 
386
{
 
387
        int keylen;
 
388
        wpa_alg alg;
 
389
        u8 null_rsc[6] = { 0, 0, 0, 0, 0, 0 };
 
390
 
 
391
        wpa_printf(MSG_DEBUG, "FT: Installing PTK to the driver.");
 
392
 
 
393
        switch (sm->pairwise_cipher) {
 
394
        case WPA_CIPHER_CCMP:
 
395
                alg = WPA_ALG_CCMP;
 
396
                keylen = 16;
 
397
                break;
 
398
        case WPA_CIPHER_TKIP:
 
399
                alg = WPA_ALG_TKIP;
 
400
                keylen = 32;
 
401
                break;
 
402
        default:
 
403
                wpa_printf(MSG_WARNING, "FT: Unsupported pairwise cipher %d",
 
404
                           sm->pairwise_cipher);
 
405
                return -1;
 
406
        }
 
407
 
 
408
        if (wpa_sm_set_key(sm, alg, bssid, 0, 1, null_rsc,
 
409
                           sizeof(null_rsc), (u8 *) sm->ptk.tk1, keylen) < 0) {
 
410
                wpa_printf(MSG_WARNING, "FT: Failed to set PTK to the driver");
 
411
                return -1;
 
412
        }
 
413
 
 
414
        return 0;
 
415
}
 
416
 
 
417
 
 
418
/**
 
419
 * wpa_ft_prepare_auth_request - Generate over-the-air auth request
 
420
 * @sm: Pointer to WPA state machine data from wpa_sm_init()
 
421
 * Returns: 0 on success, -1 on failure
 
422
 */
 
423
int wpa_ft_prepare_auth_request(struct wpa_sm *sm)
 
424
{
 
425
        u8 *ft_ies;
 
426
        size_t ft_ies_len;
 
427
 
 
428
        /* Generate a new SNonce */
 
429
        if (os_get_random(sm->snonce, WPA_NONCE_LEN)) {
 
430
                wpa_printf(MSG_INFO, "FT: Failed to generate a new SNonce");
 
431
                return -1;
 
432
        }
 
433
 
 
434
        ft_ies = wpa_ft_gen_req_ies(sm, &ft_ies_len, NULL, sm->pmk_r0_name,
 
435
                                    NULL, sm->bssid);
 
436
        if (ft_ies) {
 
437
                wpa_sm_update_ft_ies(sm, sm->mobility_domain,
 
438
                                     ft_ies, ft_ies_len);
 
439
                os_free(ft_ies);
 
440
        }
 
441
 
 
442
        return 0;
 
443
}
 
444
 
 
445
 
 
446
int wpa_ft_process_response(struct wpa_sm *sm, const u8 *ies, size_t ies_len,
 
447
                            int ft_action, const u8 *target_ap)
 
448
{
 
449
        u8 *ft_ies;
 
450
        size_t ft_ies_len;
 
451
        struct wpa_ft_ies parse;
 
452
        struct rsn_mdie *mdie;
 
453
        struct rsn_ftie *ftie;
 
454
        u8 ptk_name[WPA_PMK_NAME_LEN];
 
455
        int ret;
 
456
        const u8 *bssid;
 
457
 
 
458
        wpa_hexdump(MSG_DEBUG, "FT: Response IEs", ies, ies_len);
 
459
 
 
460
        if (ft_action) {
 
461
                if (!sm->over_the_ds_in_progress) {
 
462
                        wpa_printf(MSG_DEBUG, "FT: No over-the-DS in progress "
 
463
                                   "- drop FT Action Response");
 
464
                        return -1;
 
465
                }
 
466
 
 
467
                if (os_memcmp(target_ap, sm->target_ap, ETH_ALEN) != 0) {
 
468
                        wpa_printf(MSG_DEBUG, "FT: No over-the-DS in progress "
 
469
                                   "with this Target AP - drop FT Action "
 
470
                                   "Response");
 
471
                        return -1;
 
472
                }
 
473
        }
 
474
 
 
475
        if (sm->key_mgmt != WPA_KEY_MGMT_FT_IEEE8021X &&
 
476
            sm->key_mgmt != WPA_KEY_MGMT_FT_PSK) {
 
477
                wpa_printf(MSG_DEBUG, "FT: Reject FT IEs since FT is not "
 
478
                           "enabled for this connection");
 
479
                return -1;
 
480
        }
 
481
 
 
482
        if (wpa_ft_parse_ies(ies, ies_len, &parse) < 0) {
 
483
                wpa_printf(MSG_DEBUG, "FT: Failed to parse IEs");
 
484
                return -1;
 
485
        }
 
486
 
 
487
        mdie = (struct rsn_mdie *) parse.mdie;
 
488
        if (mdie == NULL || parse.mdie_len < sizeof(*mdie) ||
 
489
            os_memcmp(mdie->mobility_domain, sm->mobility_domain,
 
490
                      MOBILITY_DOMAIN_ID_LEN) != 0) {
 
491
                wpa_printf(MSG_DEBUG, "FT: Invalid MDIE");
 
492
                return -1;
 
493
        }
 
494
 
 
495
        ftie = (struct rsn_ftie *) parse.ftie;
 
496
        if (ftie == NULL || parse.ftie_len < sizeof(*ftie)) {
 
497
                wpa_printf(MSG_DEBUG, "FT: Invalid FTIE");
 
498
                return -1;
 
499
        }
 
500
 
 
501
        if (parse.r0kh_id == NULL) {
 
502
                wpa_printf(MSG_DEBUG, "FT: No R0KH-ID subelem in FTIE");
 
503
                return -1;
 
504
        }
 
505
 
 
506
        if (parse.r0kh_id_len != sm->r0kh_id_len ||
 
507
            os_memcmp(parse.r0kh_id, sm->r0kh_id, parse.r0kh_id_len) != 0) {
 
508
                wpa_printf(MSG_DEBUG, "FT: R0KH-ID in FTIE did not match with "
 
509
                           "the current R0KH-ID");
 
510
                wpa_hexdump(MSG_DEBUG, "FT: R0KH-ID in FTIE",
 
511
                            parse.r0kh_id, parse.r0kh_id_len);
 
512
                wpa_hexdump(MSG_DEBUG, "FT: The current R0KH-ID",
 
513
                            sm->r0kh_id, sm->r0kh_id_len);
 
514
                return -1;
 
515
        }
 
516
 
 
517
        if (parse.r1kh_id == NULL) {
 
518
                wpa_printf(MSG_DEBUG, "FT: No R1KH-ID subelem in FTIE");
 
519
                return -1;
 
520
        }
 
521
 
 
522
        if (parse.rsn_pmkid == NULL ||
 
523
            os_memcmp(parse.rsn_pmkid, sm->pmk_r0_name, WPA_PMK_NAME_LEN)) {
 
524
                wpa_printf(MSG_DEBUG, "FT: No matching PMKR0Name (PMKID) in "
 
525
                           "RSNIE");
 
526
                return -1;
 
527
        }
 
528
 
 
529
        os_memcpy(sm->r1kh_id, parse.r1kh_id, FT_R1KH_ID_LEN);
 
530
        wpa_hexdump(MSG_DEBUG, "FT: R1KH-ID", sm->r1kh_id, FT_R1KH_ID_LEN);
 
531
        wpa_hexdump(MSG_DEBUG, "FT: SNonce", sm->snonce, WPA_NONCE_LEN);
 
532
        wpa_hexdump(MSG_DEBUG, "FT: ANonce", ftie->anonce, WPA_NONCE_LEN);
 
533
        wpa_derive_pmk_r1(sm->pmk_r0, sm->pmk_r0_name, sm->r1kh_id,
 
534
                          sm->own_addr, sm->pmk_r1, sm->pmk_r1_name);
 
535
        wpa_hexdump_key(MSG_DEBUG, "FT: PMK-R1", sm->pmk_r1, PMK_LEN);
 
536
        wpa_hexdump(MSG_DEBUG, "FT: PMKR1Name",
 
537
                    sm->pmk_r1_name, WPA_PMK_NAME_LEN);
 
538
 
 
539
        bssid = target_ap;
 
540
        wpa_pmk_r1_to_ptk(sm->pmk_r1, sm->snonce, ftie->anonce, sm->own_addr,
 
541
                          bssid, sm->pmk_r1_name,
 
542
                          (u8 *) &sm->ptk, sizeof(sm->ptk), ptk_name);
 
543
        wpa_hexdump_key(MSG_DEBUG, "FT: PTK",
 
544
                        (u8 *) &sm->ptk, sizeof(sm->ptk));
 
545
        wpa_hexdump(MSG_DEBUG, "FT: PTKName", ptk_name, WPA_PMK_NAME_LEN);
 
546
 
 
547
        ft_ies = wpa_ft_gen_req_ies(sm, &ft_ies_len, ftie->anonce,
 
548
                                    sm->pmk_r1_name, sm->ptk.kck, bssid);
 
549
        if (ft_ies) {
 
550
                wpa_sm_update_ft_ies(sm, sm->mobility_domain,
 
551
                                     ft_ies, ft_ies_len);
 
552
                os_free(ft_ies);
 
553
        }
 
554
 
 
555
        ret = wpa_ft_install_ptk(sm, bssid);
 
556
 
 
557
        if (ret == 0) {
 
558
                sm->ft_completed = 1;
 
559
                if (ft_action) {
 
560
                        /* TODO: trigger re-association to the Target AP;
 
561
                         * MLME is now doing this automatically, but it should
 
562
                         * really be done only if we get here successfully. */
 
563
                        os_memcpy(sm->bssid, target_ap, ETH_ALEN);
 
564
                }
 
565
        }
 
566
 
 
567
        return ret;
 
568
}
 
569
 
 
570
 
 
571
int wpa_ft_is_completed(struct wpa_sm *sm)
 
572
{
 
573
        if (sm == NULL)
 
574
                return 0;
 
575
 
 
576
        if (sm->key_mgmt != WPA_KEY_MGMT_FT_IEEE8021X &&
 
577
            sm->key_mgmt != WPA_KEY_MGMT_FT_PSK)
 
578
                return 0;
 
579
 
 
580
        return sm->ft_completed;
 
581
}
 
582
 
 
583
 
 
584
int wpa_ft_validate_reassoc_resp(struct wpa_sm *sm, const u8 *ies,
 
585
                                 size_t ies_len, const u8 *src_addr)
 
586
{
 
587
        struct wpa_ft_ies parse;
 
588
        struct rsn_mdie *mdie;
 
589
        struct rsn_ftie *ftie;
 
590
        size_t count, gtk_len, keylen, rsc_len;
 
591
        u8 mic[16];
 
592
        u8 gtk[32];
 
593
        int keyidx;
 
594
        wpa_alg alg;
 
595
 
 
596
        wpa_hexdump(MSG_DEBUG, "FT: Response IEs", ies, ies_len);
 
597
 
 
598
        if (sm->key_mgmt != WPA_KEY_MGMT_FT_IEEE8021X &&
 
599
            sm->key_mgmt != WPA_KEY_MGMT_FT_PSK) {
 
600
                wpa_printf(MSG_DEBUG, "FT: Reject FT IEs since FT is not "
 
601
                           "enabled for this connection");
 
602
                return -1;
 
603
        }
 
604
 
 
605
        if (wpa_ft_parse_ies(ies, ies_len, &parse) < 0) {
 
606
                wpa_printf(MSG_DEBUG, "FT: Failed to parse IEs");
 
607
                return -1;
 
608
        }
 
609
 
 
610
        mdie = (struct rsn_mdie *) parse.mdie;
 
611
        if (mdie == NULL || parse.mdie_len < sizeof(*mdie) ||
 
612
            os_memcmp(mdie->mobility_domain, sm->mobility_domain,
 
613
                      MOBILITY_DOMAIN_ID_LEN) != 0) {
 
614
                wpa_printf(MSG_DEBUG, "FT: Invalid MDIE");
 
615
                return -1;
 
616
        }
 
617
 
 
618
        ftie = (struct rsn_ftie *) parse.ftie;
 
619
        if (ftie == NULL || parse.ftie_len < sizeof(*ftie)) {
 
620
                wpa_printf(MSG_DEBUG, "FT: Invalid FTIE");
 
621
                return -1;
 
622
        }
 
623
 
 
624
        if (parse.r0kh_id == NULL) {
 
625
                wpa_printf(MSG_DEBUG, "FT: No R0KH-ID subelem in FTIE");
 
626
                return -1;
 
627
        }
 
628
 
 
629
        if (parse.r0kh_id_len != sm->r0kh_id_len ||
 
630
            os_memcmp(parse.r0kh_id, sm->r0kh_id, parse.r0kh_id_len) != 0) {
 
631
                wpa_printf(MSG_DEBUG, "FT: R0KH-ID in FTIE did not match with "
 
632
                           "the current R0KH-ID");
 
633
                wpa_hexdump(MSG_DEBUG, "FT: R0KH-ID in FTIE",
 
634
                            parse.r0kh_id, parse.r0kh_id_len);
 
635
                wpa_hexdump(MSG_DEBUG, "FT: The current R0KH-ID",
 
636
                            sm->r0kh_id, sm->r0kh_id_len);
 
637
                return -1;
 
638
        }
 
639
 
 
640
        if (parse.r1kh_id == NULL) {
 
641
                wpa_printf(MSG_DEBUG, "FT: No R1KH-ID subelem in FTIE");
 
642
                return -1;
 
643
        }
 
644
 
 
645
        if (os_memcmp(parse.r1kh_id, sm->r1kh_id, FT_R1KH_ID_LEN) != 0) {
 
646
                wpa_printf(MSG_DEBUG, "FT: Unknown R1KH-ID used in "
 
647
                           "ReassocResp");
 
648
                return -1;
 
649
        }
 
650
 
 
651
        if (parse.rsn_pmkid == NULL ||
 
652
            os_memcmp(parse.rsn_pmkid, sm->pmk_r1_name, WPA_PMK_NAME_LEN)) {
 
653
                wpa_printf(MSG_DEBUG, "FT: No matching PMKR1Name (PMKID) in "
 
654
                           "RSNIE (pmkid=%d)", !!parse.rsn_pmkid);
 
655
                return -1;
 
656
        }
 
657
 
 
658
        count = 3;
 
659
        if (parse.tie)
 
660
                count++;
 
661
 
 
662
        if (ftie->mic_control[1] != count) {
 
663
                wpa_printf(MSG_DEBUG, "FT: Unexpected IE count in FTIE (%d)",
 
664
                           ftie->mic_control[1]);
 
665
                return -1;
 
666
        }
 
667
 
 
668
        if (wpa_ft_mic(sm->ptk.kck, sm->own_addr, src_addr, 6,
 
669
                       parse.mdie - 2, parse.mdie_len + 2,
 
670
                       parse.ftie - 2, parse.ftie_len + 2,
 
671
                       parse.rsn - 2, parse.rsn_len + 2, NULL, 0,
 
672
                       mic) < 0) {
 
673
                wpa_printf(MSG_DEBUG, "FT: Failed to calculate MIC");
 
674
                return -1;
 
675
        }
 
676
 
 
677
        if (os_memcmp(mic, ftie->mic, 16) != 0) {
 
678
                wpa_printf(MSG_DEBUG, "FT: Invalid MIC in FTIE");
 
679
                wpa_hexdump(MSG_MSGDUMP, "FT: Received MIC", ftie->mic, 16);
 
680
                wpa_hexdump(MSG_MSGDUMP, "FT: Calculated MIC", mic, 16);
 
681
                return -1;
 
682
        }
 
683
 
 
684
        if (parse.gtk == NULL) {
 
685
                wpa_printf(MSG_DEBUG, "FT: No GTK included in FTIE");
 
686
                return 0;
 
687
        }
 
688
 
 
689
        wpa_hexdump_key(MSG_DEBUG, "FT: Received GTK in Reassoc Resp",
 
690
                        parse.gtk, parse.gtk_len);
 
691
 
 
692
        if (parse.gtk_len < 10 + 24 || (parse.gtk_len - 10) % 8 ||
 
693
            parse.gtk_len - 18 > sizeof(gtk)) {
 
694
                wpa_printf(MSG_DEBUG, "FT: Invalid GTK sub-elem "
 
695
                           "length %lu", (unsigned long) parse.gtk_len);
 
696
                return -1;
 
697
        }
 
698
        gtk_len = parse.gtk_len - 18;
 
699
        if (aes_unwrap(sm->ptk.kek, gtk_len / 8, parse.gtk + 10, gtk)) {
 
700
                wpa_printf(MSG_WARNING, "FT: AES unwrap failed - could not "
 
701
                           "decrypt GTK");
 
702
                return -1;
 
703
        }
 
704
 
 
705
        switch (sm->group_cipher) {
 
706
        case WPA_CIPHER_CCMP:
 
707
                keylen = 16;
 
708
                rsc_len = 6;
 
709
                alg = WPA_ALG_CCMP;
 
710
                break;
 
711
        case WPA_CIPHER_TKIP:
 
712
                keylen = 32;
 
713
                rsc_len = 6;
 
714
                alg = WPA_ALG_TKIP;
 
715
                break;
 
716
        case WPA_CIPHER_WEP104:
 
717
                keylen = 13;
 
718
                rsc_len = 0;
 
719
                alg = WPA_ALG_WEP;
 
720
                break;
 
721
        case WPA_CIPHER_WEP40:
 
722
                keylen = 5;
 
723
                rsc_len = 0;
 
724
                alg = WPA_ALG_WEP;
 
725
                break;
 
726
        default:
 
727
                wpa_printf(MSG_WARNING, "WPA: Unsupported Group Cipher %d",
 
728
                           sm->group_cipher);
 
729
                return -1;
 
730
        }
 
731
 
 
732
        if (gtk_len < keylen) {
 
733
                wpa_printf(MSG_DEBUG, "FT: Too short GTK in FTIE");
 
734
                return -1;
 
735
        }
 
736
 
 
737
        /* Key Info[1] | Key Length[1] | RSC[8] | Key[5..32]. */
 
738
 
 
739
        keyidx = parse.gtk[0] & 0x03;
 
740
 
 
741
        if (parse.gtk[1] != keylen) {
 
742
                wpa_printf(MSG_DEBUG, "FT: GTK length mismatch: received %d "
 
743
                           "negotiated %lu",
 
744
                           parse.gtk[1], (unsigned long) keylen);
 
745
                return -1;
 
746
        }
 
747
 
 
748
        wpa_hexdump_key(MSG_DEBUG, "FT: GTK from Reassoc Resp", gtk, keylen);
 
749
        if (wpa_sm_set_key(sm, alg, (u8 *) "\xff\xff\xff\xff\xff\xff",
 
750
                           keyidx, 0, parse.gtk + 2, rsc_len, gtk, keylen) < 0)
 
751
        {
 
752
                wpa_printf(MSG_WARNING, "WPA: Failed to set GTK to the "
 
753
                           "driver.");
 
754
                return -1;
 
755
        }
 
756
 
 
757
        return 0;
 
758
}
 
759
 
 
760
 
 
761
/**
 
762
 * wpa_ft_start_over_ds - Generate over-the-DS auth request
 
763
 * @sm: Pointer to WPA state machine data from wpa_sm_init()
 
764
 * Returns: 0 on success, -1 on failure
 
765
 */
 
766
int wpa_ft_start_over_ds(struct wpa_sm *sm, const u8 *target_ap)
 
767
{
 
768
        u8 *ft_ies;
 
769
        size_t ft_ies_len;
 
770
 
 
771
        wpa_printf(MSG_DEBUG, "FT: Request over-the-DS with " MACSTR,
 
772
                   MAC2STR(target_ap));
 
773
 
 
774
        /* Generate a new SNonce */
 
775
        if (os_get_random(sm->snonce, WPA_NONCE_LEN)) {
 
776
                wpa_printf(MSG_INFO, "FT: Failed to generate a new SNonce");
 
777
                return -1;
 
778
        }
 
779
 
 
780
        ft_ies = wpa_ft_gen_req_ies(sm, &ft_ies_len, NULL, sm->pmk_r0_name,
 
781
                                    NULL, target_ap);
 
782
        if (ft_ies) {
 
783
                sm->over_the_ds_in_progress = 1;
 
784
                os_memcpy(sm->target_ap, target_ap, ETH_ALEN);
 
785
                wpa_sm_send_ft_action(sm, 1, target_ap, ft_ies, ft_ies_len);
 
786
                os_free(ft_ies);
 
787
        }
 
788
 
 
789
        return 0;
 
790
}
 
791
 
 
792
#endif /* CONFIG_IEEE80211R */