~ubuntu-branches/ubuntu/hardy/wpasupplicant/hardy

« back to all changes in this revision

Viewing changes to src/rsn_supp/peerkey.c

  • Committer: Bazaar Package Importer
  • Author(s): Reinhard Tartler, Alexander Sack
  • Date: 2007-08-26 16:06:57 UTC
  • mfrom: (1.1.9 upstream)
  • Revision ID: james.westby@ubuntu.com-20070826160657-2m8pxoweuxe8f93t
Tags: 0.6.0+0.5.8-0ubuntu1
* New upstream release
* remove patch 11_erroneous_manpage_ref, applied upstream
* remove patch 25_wpas_dbus_unregister_iface_fix, applied upstream

[ Alexander Sack ]
* bumping upstream version to replace development version 0.6.0 with
  this package from stable release branch.
* attempt to fix wierd timeout and high latency issues by going
  back to stable upstream version (0.5.9) (LP: #140763,
  LP: #141233).

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * WPA Supplicant - PeerKey for Direct Link Setup (DLS)
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
 
#ifdef CONFIG_PEERKEY
18
 
 
19
 
#include "common.h"
20
 
#include "sha1.h"
21
 
#include "config_ssid.h"
22
 
#include "eloop.h"
23
 
#include "wpa.h"
24
 
#include "wpa_i.h"
25
 
#include "wpa_ie.h"
26
 
#include "ieee802_11_defs.h"
27
 
#include "peerkey.h"
28
 
 
29
 
 
30
 
static u8 * wpa_add_ie(u8 *pos, const u8 *ie, size_t ie_len)
31
 
{
32
 
        os_memcpy(pos, ie, ie_len);
33
 
        return pos + ie_len;
34
 
}
35
 
 
36
 
 
37
 
static u8 * wpa_add_kde(u8 *pos, u32 kde, const u8 *data, size_t data_len)
38
 
{
39
 
        *pos++ = WLAN_EID_VENDOR_SPECIFIC;
40
 
        *pos++ = RSN_SELECTOR_LEN + data_len;
41
 
        RSN_SELECTOR_PUT(pos, kde);
42
 
        pos += RSN_SELECTOR_LEN;
43
 
        os_memcpy(pos, data, data_len);
44
 
        pos += data_len;
45
 
        return pos;
46
 
}
47
 
 
48
 
 
49
 
static void wpa_supplicant_smk_timeout(void *eloop_ctx, void *timeout_ctx)
50
 
{
51
 
#if 0
52
 
        struct wpa_sm *sm = eloop_ctx;
53
 
        struct wpa_peerkey *peerkey = timeout_ctx;
54
 
#endif
55
 
        /* TODO: time out SMK and any STK that was generated using this SMK */
56
 
}
57
 
 
58
 
 
59
 
static void wpa_supplicant_peerkey_free(struct wpa_sm *sm,
60
 
                                        struct wpa_peerkey *peerkey)
61
 
{
62
 
        eloop_cancel_timeout(wpa_supplicant_smk_timeout, sm, peerkey);
63
 
        os_free(peerkey);
64
 
}
65
 
 
66
 
 
67
 
static int wpa_supplicant_send_smk_error(struct wpa_sm *sm, const u8 *dst,
68
 
                                         const u8 *peer,
69
 
                                         u16 mui, u16 error_type, int ver)
70
 
{
71
 
        size_t rlen;
72
 
        struct wpa_eapol_key *err;
73
 
        struct rsn_error_kde error;
74
 
        u8 *rbuf, *pos;
75
 
        size_t kde_len;
76
 
        u16 key_info;
77
 
 
78
 
        kde_len = 2 + RSN_SELECTOR_LEN + sizeof(error);
79
 
        if (peer)
80
 
                kde_len += 2 + RSN_SELECTOR_LEN + ETH_ALEN;
81
 
 
82
 
        rbuf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_KEY,
83
 
                                  NULL, sizeof(*err) + kde_len, &rlen,
84
 
                                  (void *) &err);
85
 
        if (rbuf == NULL)
86
 
                return -1;
87
 
 
88
 
        err->type = EAPOL_KEY_TYPE_RSN;
89
 
        key_info = ver | WPA_KEY_INFO_SMK_MESSAGE | WPA_KEY_INFO_MIC |
90
 
                WPA_KEY_INFO_SECURE | WPA_KEY_INFO_ERROR |
91
 
                WPA_KEY_INFO_REQUEST;
92
 
        WPA_PUT_BE16(err->key_info, key_info);
93
 
        WPA_PUT_BE16(err->key_length, 0);
94
 
        os_memcpy(err->replay_counter, sm->request_counter,
95
 
                  WPA_REPLAY_COUNTER_LEN);
96
 
        inc_byte_array(sm->request_counter, WPA_REPLAY_COUNTER_LEN);
97
 
 
98
 
        WPA_PUT_BE16(err->key_data_length, (u16) kde_len);
99
 
        pos = (u8 *) (err + 1);
100
 
 
101
 
        if (peer) {
102
 
                /* Peer MAC Address KDE */
103
 
                pos = wpa_add_kde(pos, RSN_KEY_DATA_MAC_ADDR, peer, ETH_ALEN);
104
 
        }
105
 
 
106
 
        /* Error KDE */
107
 
        error.mui = host_to_be16(mui);
108
 
        error.error_type = host_to_be16(error_type);
109
 
        wpa_add_kde(pos, RSN_KEY_DATA_ERROR, (u8 *) &error, sizeof(error));
110
 
 
111
 
        if (peer) {
112
 
                wpa_printf(MSG_DEBUG, "RSN: Sending EAPOL-Key SMK Error (peer "
113
 
                           MACSTR " mui %d error_type %d)",
114
 
                           MAC2STR(peer), mui, error_type);
115
 
        } else {
116
 
                wpa_printf(MSG_DEBUG, "RSN: Sending EAPOL-Key SMK Error "
117
 
                           "(mui %d error_type %d)", mui, error_type);
118
 
        }
119
 
 
120
 
        wpa_eapol_key_send(sm, sm->ptk.kck, ver, dst, ETH_P_EAPOL,
121
 
                           rbuf, rlen, err->key_mic);
122
 
 
123
 
        return 0;
124
 
}
125
 
 
126
 
 
127
 
static int wpa_supplicant_send_smk_m3(struct wpa_sm *sm,
128
 
                                      const unsigned char *src_addr,
129
 
                                      const struct wpa_eapol_key *key,
130
 
                                      int ver, struct wpa_peerkey *peerkey)
131
 
{
132
 
        size_t rlen;
133
 
        struct wpa_eapol_key *reply;
134
 
        u8 *rbuf, *pos;
135
 
        size_t kde_len;
136
 
        u16 key_info;
137
 
 
138
 
        /* KDEs: Peer RSN IE, Initiator MAC Address, Initiator Nonce */
139
 
        kde_len = peerkey->rsnie_p_len +
140
 
                2 + RSN_SELECTOR_LEN + ETH_ALEN +
141
 
                2 + RSN_SELECTOR_LEN + WPA_NONCE_LEN;
142
 
 
143
 
        rbuf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_KEY,
144
 
                                  NULL, sizeof(*reply) + kde_len, &rlen,
145
 
                                  (void *) &reply);
146
 
        if (rbuf == NULL)
147
 
                return -1;
148
 
 
149
 
        reply->type = EAPOL_KEY_TYPE_RSN;
150
 
        key_info = ver | WPA_KEY_INFO_SMK_MESSAGE | WPA_KEY_INFO_MIC |
151
 
                WPA_KEY_INFO_SECURE;
152
 
        WPA_PUT_BE16(reply->key_info, key_info);
153
 
        WPA_PUT_BE16(reply->key_length, 0);
154
 
        os_memcpy(reply->replay_counter, key->replay_counter,
155
 
                  WPA_REPLAY_COUNTER_LEN);
156
 
 
157
 
        os_memcpy(reply->key_nonce, peerkey->pnonce, WPA_NONCE_LEN);
158
 
 
159
 
        WPA_PUT_BE16(reply->key_data_length, (u16) kde_len);
160
 
        pos = (u8 *) (reply + 1);
161
 
 
162
 
        /* Peer RSN IE */
163
 
        pos = wpa_add_ie(pos, peerkey->rsnie_p, peerkey->rsnie_p_len);
164
 
 
165
 
        /* Initiator MAC Address KDE */
166
 
        pos = wpa_add_kde(pos, RSN_KEY_DATA_MAC_ADDR, peerkey->addr, ETH_ALEN);
167
 
 
168
 
        /* Initiator Nonce */
169
 
        wpa_add_kde(pos, RSN_KEY_DATA_NONCE, peerkey->inonce, WPA_NONCE_LEN);
170
 
 
171
 
        wpa_printf(MSG_DEBUG, "RSN: Sending EAPOL-Key SMK M3");
172
 
        wpa_eapol_key_send(sm, sm->ptk.kck, ver, src_addr, ETH_P_EAPOL,
173
 
                           rbuf, rlen, reply->key_mic);
174
 
 
175
 
        return 0;
176
 
}
177
 
 
178
 
 
179
 
static int wpa_supplicant_process_smk_m2(
180
 
        struct wpa_sm *sm, const unsigned char *src_addr,
181
 
        const struct wpa_eapol_key *key, size_t extra_len, int ver)
182
 
{
183
 
        struct wpa_ssid *ssid = sm->cur_ssid;
184
 
        struct wpa_peerkey *peerkey;
185
 
        struct wpa_eapol_ie_parse kde;
186
 
        struct wpa_ie_data ie;
187
 
        int cipher;
188
 
        struct rsn_ie_hdr *hdr;
189
 
        u8 *pos;
190
 
 
191
 
        wpa_printf(MSG_DEBUG, "RSN: Received SMK M2");
192
 
 
193
 
        if (ssid == NULL || !ssid->peerkey || sm->proto != WPA_PROTO_RSN) {
194
 
                wpa_printf(MSG_INFO, "RSN: SMK handshake not allowed for "
195
 
                           "the current network");
196
 
                return -1;
197
 
        }
198
 
 
199
 
        if (wpa_supplicant_parse_ies((const u8 *) (key + 1), extra_len, &kde) <
200
 
            0) {
201
 
                wpa_printf(MSG_INFO, "RSN: Failed to parse KDEs in SMK M2");
202
 
                return -1;
203
 
        }
204
 
 
205
 
        if (kde.rsn_ie == NULL || kde.mac_addr == NULL ||
206
 
            kde.mac_addr_len < ETH_ALEN) {
207
 
                wpa_printf(MSG_INFO, "RSN: No RSN IE or MAC address KDE in "
208
 
                           "SMK M2");
209
 
                return -1;
210
 
        }
211
 
 
212
 
        wpa_printf(MSG_DEBUG, "RSN: SMK M2 - SMK initiator " MACSTR,
213
 
                   MAC2STR(kde.mac_addr));
214
 
 
215
 
        if (kde.rsn_ie_len > PEERKEY_MAX_IE_LEN) {
216
 
                wpa_printf(MSG_INFO, "RSN: Too long Initiator RSN IE in SMK "
217
 
                           "M2");
218
 
                return -1;
219
 
        }
220
 
 
221
 
        if (wpa_parse_wpa_ie_rsn(kde.rsn_ie, kde.rsn_ie_len, &ie) < 0) {
222
 
                wpa_printf(MSG_INFO, "RSN: Failed to parse RSN IE in SMK M2");
223
 
                return -1;
224
 
        }
225
 
 
226
 
        cipher = ie.pairwise_cipher & ssid->pairwise_cipher;
227
 
        if (cipher & WPA_CIPHER_CCMP) {
228
 
                wpa_printf(MSG_DEBUG, "RSN: Using CCMP for PeerKey");
229
 
                cipher = WPA_CIPHER_CCMP;
230
 
        } else if (cipher & WPA_CIPHER_TKIP) {
231
 
                wpa_printf(MSG_DEBUG, "RSN: Using TKIP for PeerKey");
232
 
                cipher = WPA_CIPHER_TKIP;
233
 
        } else {
234
 
                wpa_printf(MSG_INFO, "RSN: No acceptable cipher in SMK M2");
235
 
                wpa_supplicant_send_smk_error(sm, src_addr, kde.mac_addr,
236
 
                                              STK_MUI_SMK, STK_ERR_CPHR_NS,
237
 
                                              ver);
238
 
                return -1;
239
 
        }
240
 
 
241
 
        /* TODO: find existing entry and if found, use that instead of adding
242
 
         * a new one; how to handle the case where both ends initiate at the
243
 
         * same time? */
244
 
        peerkey = os_malloc(sizeof(*peerkey));
245
 
        if (peerkey == NULL)
246
 
                return -1;
247
 
        os_memset(peerkey, 0, sizeof(*peerkey));
248
 
        os_memcpy(peerkey->addr, kde.mac_addr, ETH_ALEN);
249
 
        os_memcpy(peerkey->inonce, key->key_nonce, WPA_NONCE_LEN);
250
 
        os_memcpy(peerkey->rsnie_i, kde.rsn_ie, kde.rsn_ie_len);
251
 
        peerkey->rsnie_i_len = kde.rsn_ie_len;
252
 
        peerkey->cipher = cipher;
253
 
 
254
 
        if (hostapd_get_rand(peerkey->pnonce, WPA_NONCE_LEN)) {
255
 
                wpa_msg(sm->ctx->ctx, MSG_WARNING,
256
 
                        "WPA: Failed to get random data for PNonce");
257
 
                wpa_supplicant_peerkey_free(sm, peerkey);
258
 
                return -1;
259
 
        }
260
 
 
261
 
        hdr = (struct rsn_ie_hdr *) peerkey->rsnie_p;
262
 
        hdr->elem_id = WLAN_EID_RSN;
263
 
        WPA_PUT_LE16(hdr->version, RSN_VERSION);
264
 
        pos = (u8 *) (hdr + 1);
265
 
        /* Group Suite can be anything for SMK RSN IE; receiver will just
266
 
         * ignore it. */
267
 
        RSN_SELECTOR_PUT(pos, RSN_CIPHER_SUITE_CCMP);
268
 
        pos += RSN_SELECTOR_LEN;
269
 
        /* Include only the selected cipher in pairwise cipher suite */
270
 
        WPA_PUT_LE16(pos, 1);
271
 
        pos += 2;
272
 
        if (cipher == WPA_CIPHER_CCMP)
273
 
                RSN_SELECTOR_PUT(pos, RSN_CIPHER_SUITE_CCMP);
274
 
        else if (cipher == WPA_CIPHER_TKIP)
275
 
                RSN_SELECTOR_PUT(pos, RSN_CIPHER_SUITE_TKIP);
276
 
        pos += RSN_SELECTOR_LEN;
277
 
 
278
 
        hdr->len = (pos - peerkey->rsnie_p) - 2;
279
 
        peerkey->rsnie_p_len = pos - peerkey->rsnie_p;
280
 
        wpa_hexdump(MSG_DEBUG, "WPA: RSN IE for SMK handshake",
281
 
                    peerkey->rsnie_p, peerkey->rsnie_p_len);
282
 
 
283
 
        wpa_supplicant_send_smk_m3(sm, src_addr, key, ver, peerkey);
284
 
 
285
 
        peerkey->next = sm->peerkey;
286
 
        sm->peerkey = peerkey;
287
 
 
288
 
        return 0;
289
 
}
290
 
 
291
 
 
292
 
/**
293
 
 * rsn_smkid - Derive SMK identifier
294
 
 * @smk: Station master key (32 bytes)
295
 
 * @pnonce: Peer Nonce
296
 
 * @mac_p: Peer MAC address
297
 
 * @inonce: Initiator Nonce
298
 
 * @mac_i: Initiator MAC address
299
 
 *
300
 
 * 8.5.1.4 Station to station (STK) key hierarchy
301
 
 * SMKID = HMAC-SHA1-128(SMK, "SMK Name" || PNonce || MAC_P || INonce || MAC_I)
302
 
 */
303
 
static void rsn_smkid(const u8 *smk, const u8 *pnonce, const u8 *mac_p,
304
 
                      const u8 *inonce, const u8 *mac_i, u8 *smkid)
305
 
{
306
 
        char *title = "SMK Name";
307
 
        const u8 *addr[5];
308
 
        const size_t len[5] = { 8, WPA_NONCE_LEN, ETH_ALEN, WPA_NONCE_LEN,
309
 
                                ETH_ALEN };
310
 
        unsigned char hash[SHA1_MAC_LEN];
311
 
 
312
 
        addr[0] = (u8 *) title;
313
 
        addr[1] = pnonce;
314
 
        addr[2] = mac_p;
315
 
        addr[3] = inonce;
316
 
        addr[4] = mac_i;
317
 
 
318
 
        hmac_sha1_vector(smk, PMK_LEN, 5, addr, len, hash);
319
 
        os_memcpy(smkid, hash, PMKID_LEN);
320
 
}
321
 
 
322
 
 
323
 
static void wpa_supplicant_send_stk_1_of_4(struct wpa_sm *sm,
324
 
                                           struct wpa_peerkey *peerkey)
325
 
{
326
 
        size_t mlen;
327
 
        struct wpa_eapol_key *msg;
328
 
        u8 *mbuf;
329
 
        size_t kde_len;
330
 
        u16 key_info, ver;
331
 
 
332
 
        kde_len = 2 + RSN_SELECTOR_LEN + PMKID_LEN;
333
 
 
334
 
        mbuf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_KEY, NULL,
335
 
                                  sizeof(*msg) + kde_len, &mlen,
336
 
                                  (void *) &msg);
337
 
        if (mbuf == NULL)
338
 
                return;
339
 
 
340
 
        msg->type = EAPOL_KEY_TYPE_RSN;
341
 
 
342
 
        if (peerkey->cipher == WPA_CIPHER_CCMP)
343
 
                ver = WPA_KEY_INFO_TYPE_HMAC_SHA1_AES;
344
 
        else
345
 
                ver = WPA_KEY_INFO_TYPE_HMAC_MD5_RC4;
346
 
 
347
 
        key_info = ver | WPA_KEY_INFO_KEY_TYPE | WPA_KEY_INFO_ACK;
348
 
        WPA_PUT_BE16(msg->key_info, key_info);
349
 
 
350
 
        if (peerkey->cipher == WPA_CIPHER_CCMP)
351
 
                WPA_PUT_BE16(msg->key_length, 16);
352
 
        else
353
 
                WPA_PUT_BE16(msg->key_length, 32);
354
 
 
355
 
        os_memcpy(msg->replay_counter, peerkey->replay_counter,
356
 
                  WPA_REPLAY_COUNTER_LEN);
357
 
        inc_byte_array(peerkey->replay_counter, WPA_REPLAY_COUNTER_LEN);
358
 
 
359
 
        WPA_PUT_BE16(msg->key_data_length, kde_len);
360
 
        wpa_add_kde((u8 *) (msg + 1), RSN_KEY_DATA_PMKID,
361
 
                    peerkey->smkid, PMKID_LEN);
362
 
 
363
 
        if (hostapd_get_rand(peerkey->inonce, WPA_NONCE_LEN)) {
364
 
                wpa_msg(sm->ctx->ctx, MSG_WARNING,
365
 
                        "RSN: Failed to get random data for INonce (STK)");
366
 
                os_free(mbuf);
367
 
                return;
368
 
        }
369
 
        wpa_hexdump(MSG_DEBUG, "RSN: INonce for STK 4-Way Handshake",
370
 
                    peerkey->inonce, WPA_NONCE_LEN);
371
 
        os_memcpy(msg->key_nonce, peerkey->inonce, WPA_NONCE_LEN);
372
 
 
373
 
        wpa_printf(MSG_DEBUG, "RSN: Sending EAPOL-Key STK 1/4 to " MACSTR,
374
 
                   MAC2STR(peerkey->addr));
375
 
        wpa_eapol_key_send(sm, NULL, ver, peerkey->addr, ETH_P_EAPOL,
376
 
                           mbuf, mlen, NULL);
377
 
}
378
 
 
379
 
 
380
 
static void wpa_supplicant_send_stk_3_of_4(struct wpa_sm *sm,
381
 
                                           struct wpa_peerkey *peerkey)
382
 
{
383
 
        size_t mlen;
384
 
        struct wpa_eapol_key *msg;
385
 
        u8 *mbuf, *pos;
386
 
        size_t kde_len;
387
 
        u16 key_info, ver;
388
 
        be32 lifetime;
389
 
 
390
 
        kde_len = peerkey->rsnie_i_len +
391
 
                2 + RSN_SELECTOR_LEN + sizeof(lifetime);
392
 
 
393
 
        mbuf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_KEY, NULL,
394
 
                                  sizeof(*msg) + kde_len, &mlen,
395
 
                                  (void *) &msg);
396
 
        if (mbuf == NULL)
397
 
                return;
398
 
 
399
 
        msg->type = EAPOL_KEY_TYPE_RSN;
400
 
 
401
 
        if (peerkey->cipher == WPA_CIPHER_CCMP)
402
 
                ver = WPA_KEY_INFO_TYPE_HMAC_SHA1_AES;
403
 
        else
404
 
                ver = WPA_KEY_INFO_TYPE_HMAC_MD5_RC4;
405
 
 
406
 
        key_info = ver | WPA_KEY_INFO_KEY_TYPE | WPA_KEY_INFO_ACK |
407
 
                WPA_KEY_INFO_MIC | WPA_KEY_INFO_SECURE;
408
 
        WPA_PUT_BE16(msg->key_info, key_info);
409
 
 
410
 
        if (peerkey->cipher == WPA_CIPHER_CCMP)
411
 
                WPA_PUT_BE16(msg->key_length, 16);
412
 
        else
413
 
                WPA_PUT_BE16(msg->key_length, 32);
414
 
 
415
 
        os_memcpy(msg->replay_counter, peerkey->replay_counter,
416
 
                  WPA_REPLAY_COUNTER_LEN);
417
 
        inc_byte_array(peerkey->replay_counter, WPA_REPLAY_COUNTER_LEN);
418
 
 
419
 
        WPA_PUT_BE16(msg->key_data_length, kde_len);
420
 
        pos = (u8 *) (msg + 1);
421
 
        pos = wpa_add_ie(pos, peerkey->rsnie_i, peerkey->rsnie_i_len);
422
 
        lifetime = host_to_be32(peerkey->lifetime);
423
 
        wpa_add_kde(pos, RSN_KEY_DATA_LIFETIME,
424
 
                    (u8 *) &lifetime, sizeof(lifetime));
425
 
 
426
 
        os_memcpy(msg->key_nonce, peerkey->inonce, WPA_NONCE_LEN);
427
 
 
428
 
        wpa_printf(MSG_DEBUG, "RSN: Sending EAPOL-Key STK 3/4 to " MACSTR,
429
 
                   MAC2STR(peerkey->addr));
430
 
        wpa_eapol_key_send(sm, peerkey->stk.kck, ver, peerkey->addr,
431
 
                           ETH_P_EAPOL, mbuf, mlen, msg->key_mic);
432
 
}
433
 
 
434
 
 
435
 
static int wpa_supplicant_process_smk_m4(struct wpa_peerkey *peerkey,
436
 
                                         struct wpa_eapol_ie_parse *kde)
437
 
{
438
 
        wpa_printf(MSG_DEBUG, "RSN: Received SMK M4 (Initiator " MACSTR ")",
439
 
                   MAC2STR(kde->mac_addr));
440
 
 
441
 
        if (os_memcmp(kde->smk + PMK_LEN, peerkey->pnonce, WPA_NONCE_LEN) != 0)
442
 
        {
443
 
                wpa_printf(MSG_INFO, "RSN: PNonce in SMK KDE does not "
444
 
                           "match with the one used in SMK M3");
445
 
                return -1;
446
 
        }
447
 
 
448
 
        if (os_memcmp(kde->nonce, peerkey->inonce, WPA_NONCE_LEN) != 0) {
449
 
                wpa_printf(MSG_INFO, "RSN: INonce in SMK M4 did not "
450
 
                           "match with the one received in SMK M2");
451
 
                return -1;
452
 
        }
453
 
 
454
 
        return 0;
455
 
}
456
 
 
457
 
 
458
 
static int wpa_supplicant_process_smk_m5(struct wpa_sm *sm,
459
 
                                         const unsigned char *src_addr,
460
 
                                         const struct wpa_eapol_key *key,
461
 
                                         int ver,
462
 
                                         struct wpa_peerkey *peerkey,
463
 
                                         struct wpa_eapol_ie_parse *kde)
464
 
{
465
 
        int cipher;
466
 
        struct wpa_ie_data ie;
467
 
        struct wpa_ssid *ssid = sm->cur_ssid;
468
 
 
469
 
        wpa_printf(MSG_DEBUG, "RSN: Received SMK M5 (Peer " MACSTR ")",
470
 
                   MAC2STR(kde->mac_addr));
471
 
        if (kde->rsn_ie == NULL || kde->rsn_ie_len > PEERKEY_MAX_IE_LEN ||
472
 
            wpa_parse_wpa_ie_rsn(kde->rsn_ie, kde->rsn_ie_len, &ie) < 0) {
473
 
                wpa_printf(MSG_INFO, "RSN: No RSN IE in SMK M5");
474
 
                /* TODO: abort negotiation */
475
 
                return -1;
476
 
        }
477
 
 
478
 
        if (os_memcmp(key->key_nonce, peerkey->inonce, WPA_NONCE_LEN) != 0) {
479
 
                wpa_printf(MSG_INFO, "RSN: Key Nonce in SMK M5 does "
480
 
                           "not match with INonce used in SMK M1");
481
 
                return -1;
482
 
        }
483
 
 
484
 
        if (os_memcmp(kde->smk + PMK_LEN, peerkey->inonce, WPA_NONCE_LEN) != 0)
485
 
        {
486
 
                wpa_printf(MSG_INFO, "RSN: INonce in SMK KDE does not "
487
 
                           "match with the one used in SMK M1");
488
 
                return -1;
489
 
        }
490
 
 
491
 
        os_memcpy(peerkey->rsnie_p, kde->rsn_ie, kde->rsn_ie_len);
492
 
        peerkey->rsnie_p_len = kde->rsn_ie_len;
493
 
        os_memcpy(peerkey->pnonce, kde->nonce, WPA_NONCE_LEN);
494
 
 
495
 
        cipher = ie.pairwise_cipher & ssid->pairwise_cipher;
496
 
        if (cipher & WPA_CIPHER_CCMP) {
497
 
                wpa_printf(MSG_DEBUG, "RSN: Using CCMP for PeerKey");
498
 
                peerkey->cipher = WPA_CIPHER_CCMP;
499
 
        } else if (cipher & WPA_CIPHER_TKIP) {
500
 
                wpa_printf(MSG_DEBUG, "RSN: Using TKIP for PeerKey");
501
 
                peerkey->cipher = WPA_CIPHER_TKIP;
502
 
        } else {
503
 
                wpa_printf(MSG_INFO, "RSN: SMK Peer STA " MACSTR " selected "
504
 
                           "unacceptable cipher", MAC2STR(kde->mac_addr));
505
 
                wpa_supplicant_send_smk_error(sm, src_addr, kde->mac_addr,
506
 
                                              STK_MUI_SMK, STK_ERR_CPHR_NS,
507
 
                                              ver);
508
 
                /* TODO: abort negotiation */
509
 
                return -1;
510
 
        }
511
 
 
512
 
        return 0;
513
 
}
514
 
 
515
 
 
516
 
static int wpa_supplicant_process_smk_m45(
517
 
        struct wpa_sm *sm, const unsigned char *src_addr,
518
 
        const struct wpa_eapol_key *key, size_t extra_len, int ver)
519
 
{
520
 
        struct wpa_ssid *ssid = sm->cur_ssid;
521
 
        struct wpa_peerkey *peerkey;
522
 
        struct wpa_eapol_ie_parse kde;
523
 
        u32 lifetime;
524
 
        struct os_time now;
525
 
 
526
 
        if (ssid == NULL || !ssid->peerkey || sm->proto != WPA_PROTO_RSN) {
527
 
                wpa_printf(MSG_DEBUG, "RSN: SMK handshake not allowed for "
528
 
                           "the current network");
529
 
                return -1;
530
 
        }
531
 
 
532
 
        if (wpa_supplicant_parse_ies((const u8 *) (key + 1), extra_len, &kde) <
533
 
            0) {
534
 
                wpa_printf(MSG_INFO, "RSN: Failed to parse KDEs in SMK M4/M5");
535
 
                return -1;
536
 
        }
537
 
 
538
 
        if (kde.mac_addr == NULL || kde.mac_addr_len < ETH_ALEN ||
539
 
            kde.nonce == NULL || kde.nonce_len < WPA_NONCE_LEN ||
540
 
            kde.smk == NULL || kde.smk_len < PMK_LEN + WPA_NONCE_LEN ||
541
 
            kde.lifetime == NULL || kde.lifetime_len < 4) {
542
 
                wpa_printf(MSG_INFO, "RSN: No MAC Address, Nonce, SMK, or "
543
 
                           "Lifetime KDE in SMK M4/M5");
544
 
                return -1;
545
 
        }
546
 
 
547
 
        for (peerkey = sm->peerkey; peerkey; peerkey = peerkey->next) {
548
 
                if (os_memcmp(peerkey->addr, kde.mac_addr, ETH_ALEN) == 0 &&
549
 
                    os_memcmp(peerkey->initiator ? peerkey->inonce :
550
 
                           peerkey->pnonce,
551
 
                           key->key_nonce, WPA_NONCE_LEN) == 0)
552
 
                        break;
553
 
        }
554
 
        if (peerkey == NULL) {
555
 
                wpa_printf(MSG_INFO, "RSN: No matching SMK handshake found "
556
 
                           "for SMK M4/M5: peer " MACSTR,
557
 
                           MAC2STR(kde.mac_addr));
558
 
                return -1;
559
 
        }
560
 
 
561
 
        if (peerkey->initiator) {
562
 
                if (wpa_supplicant_process_smk_m5(sm, src_addr, key, ver,
563
 
                                                  peerkey, &kde) < 0)
564
 
                        return -1;
565
 
        } else {
566
 
                if (wpa_supplicant_process_smk_m4(peerkey, &kde) < 0)
567
 
                        return -1;
568
 
        }
569
 
 
570
 
        os_memcpy(peerkey->smk, kde.smk, PMK_LEN);
571
 
        peerkey->smk_complete = 1;
572
 
        wpa_hexdump_key(MSG_DEBUG, "RSN: SMK", peerkey->smk, PMK_LEN);
573
 
        lifetime = WPA_GET_BE32(kde.lifetime);
574
 
        wpa_printf(MSG_DEBUG, "RSN: SMK lifetime %u seconds", lifetime);
575
 
        if (lifetime > 1000000000)
576
 
                lifetime = 1000000000; /* avoid overflowing expiration time */
577
 
        peerkey->lifetime = lifetime;
578
 
        os_get_time(&now);
579
 
        peerkey->expiration = now.sec + lifetime;
580
 
        eloop_register_timeout(lifetime, 0, wpa_supplicant_smk_timeout,
581
 
                               sm, peerkey);
582
 
 
583
 
        if (peerkey->initiator) {
584
 
                rsn_smkid(peerkey->smk, peerkey->pnonce, peerkey->addr,
585
 
                          peerkey->inonce, sm->own_addr, peerkey->smkid);
586
 
                wpa_supplicant_send_stk_1_of_4(sm, peerkey);
587
 
        } else {
588
 
                rsn_smkid(peerkey->smk, peerkey->pnonce, sm->own_addr,
589
 
                          peerkey->inonce, peerkey->addr, peerkey->smkid);
590
 
        }
591
 
        wpa_hexdump(MSG_DEBUG, "RSN: SMKID", peerkey->smkid, PMKID_LEN);
592
 
 
593
 
        return 0;
594
 
}
595
 
 
596
 
 
597
 
static int wpa_supplicant_process_smk_error(
598
 
        struct wpa_sm *sm, const unsigned char *src_addr,
599
 
        const struct wpa_eapol_key *key, size_t extra_len)
600
 
{
601
 
        struct wpa_ssid *ssid = sm->cur_ssid;
602
 
        struct wpa_eapol_ie_parse kde;
603
 
        struct rsn_error_kde error;
604
 
        u8 peer[ETH_ALEN];
605
 
        u16 error_type;
606
 
 
607
 
        wpa_printf(MSG_DEBUG, "RSN: Received SMK Error");
608
 
 
609
 
        if (ssid == NULL || !ssid->peerkey || sm->proto != WPA_PROTO_RSN) {
610
 
                wpa_printf(MSG_DEBUG, "RSN: SMK handshake not allowed for "
611
 
                           "the current network");
612
 
                return -1;
613
 
        }
614
 
 
615
 
        if (wpa_supplicant_parse_ies((const u8 *) (key + 1), extra_len, &kde) <
616
 
            0) {
617
 
                wpa_printf(MSG_INFO, "RSN: Failed to parse KDEs in SMK Error");
618
 
                return -1;
619
 
        }
620
 
 
621
 
        if (kde.error == NULL || kde.error_len < sizeof(error)) {
622
 
                wpa_printf(MSG_INFO, "RSN: No Error KDE in SMK Error");
623
 
                return -1;
624
 
        }
625
 
 
626
 
        if (kde.mac_addr && kde.mac_addr_len >= ETH_ALEN)
627
 
                os_memcpy(peer, kde.mac_addr, ETH_ALEN);
628
 
        os_memcpy(&error, kde.error, sizeof(error));
629
 
        error_type = be_to_host16(error.error_type);
630
 
        wpa_msg(sm->ctx->ctx, MSG_INFO,
631
 
                "RSN: SMK Error KDE received: MUI %d error_type %d peer "
632
 
                MACSTR,
633
 
                be_to_host16(error.mui), error_type,
634
 
                MAC2STR(peer));
635
 
 
636
 
        if (kde.mac_addr &&
637
 
            (error_type == STK_ERR_STA_NR || error_type == STK_ERR_STA_NRSN ||
638
 
             error_type == STK_ERR_CPHR_NS)) {
639
 
                struct wpa_peerkey *peerkey;
640
 
 
641
 
                for (peerkey = sm->peerkey; peerkey; peerkey = peerkey->next) {
642
 
                        if (os_memcmp(peerkey->addr, kde.mac_addr, ETH_ALEN) ==
643
 
                            0)
644
 
                                break;
645
 
                }
646
 
                if (peerkey == NULL) {
647
 
                        wpa_printf(MSG_DEBUG, "RSN: No matching SMK handshake "
648
 
                                   "found for SMK Error");
649
 
                        return -1;
650
 
                }
651
 
                /* TODO: abort SMK/STK handshake and remove all related keys */
652
 
        }
653
 
 
654
 
        return 0;
655
 
}
656
 
 
657
 
 
658
 
static void wpa_supplicant_process_stk_1_of_4(struct wpa_sm *sm,
659
 
                                              struct wpa_peerkey *peerkey,
660
 
                                              const struct wpa_eapol_key *key,
661
 
                                              u16 ver)
662
 
{
663
 
        struct wpa_eapol_ie_parse ie;
664
 
        const u8 *kde;
665
 
        size_t len, kde_buf_len;
666
 
        struct wpa_ptk *stk;
667
 
        u8 buf[8], *kde_buf, *pos;
668
 
        be32 lifetime;
669
 
 
670
 
        wpa_printf(MSG_DEBUG, "RSN: RX message 1 of STK 4-Way Handshake from "
671
 
                   MACSTR " (ver=%d)", MAC2STR(peerkey->addr), ver);
672
 
 
673
 
        os_memset(&ie, 0, sizeof(ie));
674
 
 
675
 
        /* RSN: msg 1/4 should contain SMKID for the selected SMK */
676
 
        kde = (const u8 *) (key + 1);
677
 
        len = WPA_GET_BE16(key->key_data_length);
678
 
        wpa_hexdump(MSG_DEBUG, "RSN: msg 1/4 key data", kde, len);
679
 
        if (wpa_supplicant_parse_ies(kde, len, &ie) < 0 || ie.pmkid == NULL) {
680
 
                wpa_printf(MSG_DEBUG, "RSN: No SMKID in STK 1/4");
681
 
                return;
682
 
        }
683
 
        if (os_memcmp(ie.pmkid, peerkey->smkid, PMKID_LEN) != 0) {
684
 
                wpa_hexdump(MSG_DEBUG, "RSN: Unknown SMKID in STK 1/4",
685
 
                            ie.pmkid, PMKID_LEN);
686
 
                return;
687
 
        }
688
 
 
689
 
        if (hostapd_get_rand(peerkey->pnonce, WPA_NONCE_LEN)) {
690
 
                wpa_msg(sm->ctx->ctx, MSG_WARNING,
691
 
                        "RSN: Failed to get random data for PNonce");
692
 
                return;
693
 
        }
694
 
        wpa_hexdump(MSG_DEBUG, "WPA: Renewed PNonce",
695
 
                    peerkey->pnonce, WPA_NONCE_LEN);
696
 
 
697
 
        /* Calculate STK which will be stored as a temporary STK until it has
698
 
         * been verified when processing message 3/4. */
699
 
        stk = &peerkey->tstk;
700
 
        wpa_pmk_to_ptk(peerkey->smk, PMK_LEN, "Peer key expansion",
701
 
                       sm->own_addr, peerkey->addr,
702
 
                       peerkey->pnonce, key->key_nonce,
703
 
                       (u8 *) stk, sizeof(*stk));
704
 
        /* Supplicant: swap tx/rx Mic keys */
705
 
        os_memcpy(buf, stk->u.auth.tx_mic_key, 8);
706
 
        os_memcpy(stk->u.auth.tx_mic_key, stk->u.auth.rx_mic_key, 8);
707
 
        os_memcpy(stk->u.auth.rx_mic_key, buf, 8);
708
 
        peerkey->tstk_set = 1;
709
 
 
710
 
        kde_buf_len = peerkey->rsnie_p_len +
711
 
                2 + RSN_SELECTOR_LEN + sizeof(lifetime) +
712
 
                2 + RSN_SELECTOR_LEN + PMKID_LEN;
713
 
        kde_buf = os_malloc(kde_buf_len);
714
 
        if (kde_buf == NULL)
715
 
                return;
716
 
        pos = kde_buf;
717
 
        pos = wpa_add_ie(pos, peerkey->rsnie_p, peerkey->rsnie_p_len);
718
 
        lifetime = host_to_be32(peerkey->lifetime);
719
 
        pos = wpa_add_kde(pos, RSN_KEY_DATA_LIFETIME,
720
 
                          (u8 *) &lifetime, sizeof(lifetime));
721
 
        wpa_add_kde(pos, RSN_KEY_DATA_PMKID, peerkey->smkid, PMKID_LEN);
722
 
 
723
 
        if (wpa_supplicant_send_2_of_4(sm, peerkey->addr, key, ver,
724
 
                                       peerkey->pnonce, kde_buf, kde_buf_len,
725
 
                                       stk)) {
726
 
                os_free(kde_buf);
727
 
                return;
728
 
        }
729
 
        os_free(kde_buf);
730
 
 
731
 
        os_memcpy(peerkey->inonce, key->key_nonce, WPA_NONCE_LEN);
732
 
}
733
 
 
734
 
 
735
 
static void wpa_supplicant_update_smk_lifetime(struct wpa_sm *sm,
736
 
                                               struct wpa_peerkey *peerkey,
737
 
                                               struct wpa_eapol_ie_parse *kde)
738
 
{
739
 
        u32 lifetime;
740
 
        struct os_time now;
741
 
 
742
 
        if (kde->lifetime == NULL || kde->lifetime_len < sizeof(lifetime))
743
 
                return;
744
 
 
745
 
        lifetime = WPA_GET_BE32(kde->lifetime);
746
 
 
747
 
        if (lifetime >= peerkey->lifetime) {
748
 
                wpa_printf(MSG_DEBUG, "RSN: Peer used SMK lifetime %u seconds "
749
 
                           "which is larger than or equal to own value %u "
750
 
                           "seconds - ignored", lifetime, peerkey->lifetime);
751
 
                return;
752
 
        }
753
 
 
754
 
        wpa_printf(MSG_DEBUG, "RSN: Peer used shorter SMK lifetime %u seconds "
755
 
                   "(own was %u seconds) - updated",
756
 
                   lifetime, peerkey->lifetime);
757
 
        peerkey->lifetime = lifetime;
758
 
 
759
 
        os_get_time(&now);
760
 
        peerkey->expiration = now.sec + lifetime;
761
 
        eloop_cancel_timeout(wpa_supplicant_smk_timeout, sm, peerkey);
762
 
        eloop_register_timeout(lifetime, 0, wpa_supplicant_smk_timeout,
763
 
                               sm, peerkey);
764
 
}
765
 
 
766
 
 
767
 
static void wpa_supplicant_process_stk_2_of_4(struct wpa_sm *sm,
768
 
                                              struct wpa_peerkey *peerkey,
769
 
                                              const struct wpa_eapol_key *key,
770
 
                                              u16 ver)
771
 
{
772
 
        struct wpa_eapol_ie_parse kde;
773
 
        const u8 *keydata;
774
 
        size_t len;
775
 
 
776
 
        wpa_printf(MSG_DEBUG, "RSN: RX message 2 of STK 4-Way Handshake from "
777
 
                   MACSTR " (ver=%d)", MAC2STR(peerkey->addr), ver);
778
 
 
779
 
        os_memset(&kde, 0, sizeof(kde));
780
 
 
781
 
        /* RSN: msg 2/4 should contain SMKID for the selected SMK and RSN IE
782
 
         * from the peer. It may also include Lifetime KDE. */
783
 
        keydata = (const u8 *) (key + 1);
784
 
        len = WPA_GET_BE16(key->key_data_length);
785
 
        wpa_hexdump(MSG_DEBUG, "RSN: msg 2/4 key data", keydata, len);
786
 
        if (wpa_supplicant_parse_ies(keydata, len, &kde) < 0 ||
787
 
            kde.pmkid == NULL || kde.rsn_ie == NULL) {
788
 
                wpa_printf(MSG_DEBUG, "RSN: No SMKID or RSN IE in STK 2/4");
789
 
                return;
790
 
        }
791
 
 
792
 
        if (os_memcmp(kde.pmkid, peerkey->smkid, PMKID_LEN) != 0) {
793
 
                wpa_hexdump(MSG_DEBUG, "RSN: Unknown SMKID in STK 2/4",
794
 
                            kde.pmkid, PMKID_LEN);
795
 
                return;
796
 
        }
797
 
 
798
 
        if (kde.rsn_ie_len != peerkey->rsnie_p_len ||
799
 
            os_memcmp(kde.rsn_ie, peerkey->rsnie_p, kde.rsn_ie_len) != 0) {
800
 
                wpa_printf(MSG_INFO, "RSN: Peer RSN IE in SMK and STK "
801
 
                           "handshakes did not match");
802
 
                wpa_hexdump(MSG_DEBUG, "RSN: Peer RSN IE in SMK handshake",
803
 
                            peerkey->rsnie_p, peerkey->rsnie_p_len);
804
 
                wpa_hexdump(MSG_DEBUG, "RSN: Peer RSN IE in STK handshake",
805
 
                            kde.rsn_ie, kde.rsn_ie_len);
806
 
                return;
807
 
        }
808
 
 
809
 
        wpa_supplicant_update_smk_lifetime(sm, peerkey, &kde);
810
 
 
811
 
        wpa_supplicant_send_stk_3_of_4(sm, peerkey);
812
 
        os_memcpy(peerkey->pnonce, key->key_nonce, WPA_NONCE_LEN);
813
 
}
814
 
 
815
 
 
816
 
static void wpa_supplicant_process_stk_3_of_4(struct wpa_sm *sm,
817
 
                                              struct wpa_peerkey *peerkey,
818
 
                                              const struct wpa_eapol_key *key,
819
 
                                              u16 ver)
820
 
{
821
 
        struct wpa_eapol_ie_parse kde;
822
 
        const u8 *keydata;
823
 
        size_t len, key_len;
824
 
        const u8 *_key;
825
 
        u8 key_buf[32], rsc[6];
826
 
 
827
 
        wpa_printf(MSG_DEBUG, "RSN: RX message 3 of STK 4-Way Handshake from "
828
 
                   MACSTR " (ver=%d)", MAC2STR(peerkey->addr), ver);
829
 
 
830
 
        os_memset(&kde, 0, sizeof(kde));
831
 
 
832
 
        /* RSN: msg 3/4 should contain Initiator RSN IE. It may also include
833
 
         * Lifetime KDE. */
834
 
        keydata = (const u8 *) (key + 1);
835
 
        len = WPA_GET_BE16(key->key_data_length);
836
 
        wpa_hexdump(MSG_DEBUG, "RSN: msg 3/4 key data", keydata, len);
837
 
        if (wpa_supplicant_parse_ies(keydata, len, &kde) < 0) {
838
 
                wpa_printf(MSG_DEBUG, "RSN: Failed to parse key data in "
839
 
                           "STK 3/4");
840
 
                return;
841
 
        }
842
 
 
843
 
        if (kde.rsn_ie_len != peerkey->rsnie_i_len ||
844
 
            os_memcmp(kde.rsn_ie, peerkey->rsnie_i, kde.rsn_ie_len) != 0) {
845
 
                wpa_printf(MSG_INFO, "RSN: Initiator RSN IE in SMK and STK "
846
 
                           "handshakes did not match");
847
 
                wpa_hexdump(MSG_DEBUG, "RSN: Initiator RSN IE in SMK "
848
 
                            "handshake",
849
 
                            peerkey->rsnie_i, peerkey->rsnie_i_len);
850
 
                wpa_hexdump(MSG_DEBUG, "RSN: Initiator RSN IE in STK "
851
 
                            "handshake",
852
 
                            kde.rsn_ie, kde.rsn_ie_len);
853
 
                return;
854
 
        }
855
 
 
856
 
        if (os_memcmp(peerkey->inonce, key->key_nonce, WPA_NONCE_LEN) != 0) {
857
 
                wpa_printf(MSG_WARNING, "RSN: INonce from message 1 of STK "
858
 
                           "4-Way Handshake differs from 3 of STK 4-Way "
859
 
                           "Handshake - drop packet (src=" MACSTR ")",
860
 
                           MAC2STR(peerkey->addr));
861
 
                return;
862
 
        }
863
 
 
864
 
        wpa_supplicant_update_smk_lifetime(sm, peerkey, &kde);
865
 
 
866
 
        if (wpa_supplicant_send_4_of_4(sm, peerkey->addr, key, ver,
867
 
                                       WPA_GET_BE16(key->key_info),
868
 
                                       NULL, 0, &peerkey->stk))
869
 
                return;
870
 
 
871
 
        _key = (u8 *) peerkey->stk.tk1;
872
 
        if (peerkey->cipher == WPA_CIPHER_TKIP) {
873
 
                /* Swap Tx/Rx keys for Michael MIC */
874
 
                os_memcpy(key_buf, _key, 16);
875
 
                os_memcpy(key_buf + 16, _key + 24, 8);
876
 
                os_memcpy(key_buf + 24, _key + 16, 8);
877
 
                _key = key_buf;
878
 
                key_len = 32;
879
 
        } else
880
 
                key_len = 16;
881
 
 
882
 
        os_memset(rsc, 0, 6);
883
 
        if (wpa_sm_set_key(sm, peerkey->cipher, peerkey->addr, 0, 1,
884
 
                           rsc, sizeof(rsc), _key, key_len) < 0) {
885
 
                wpa_printf(MSG_WARNING, "RSN: Failed to set STK to the "
886
 
                           "driver.");
887
 
                return;
888
 
        }
889
 
}
890
 
 
891
 
 
892
 
static void wpa_supplicant_process_stk_4_of_4(struct wpa_sm *sm,
893
 
                                              struct wpa_peerkey *peerkey,
894
 
                                              const struct wpa_eapol_key *key,
895
 
                                              u16 ver)
896
 
{
897
 
        u8 rsc[6];
898
 
 
899
 
        wpa_printf(MSG_DEBUG, "RSN: RX message 4 of STK 4-Way Handshake from "
900
 
                   MACSTR " (ver=%d)", MAC2STR(peerkey->addr), ver);
901
 
 
902
 
        os_memset(rsc, 0, 6);
903
 
        if (wpa_sm_set_key(sm, peerkey->cipher, peerkey->addr, 0, 1,
904
 
                           rsc, sizeof(rsc), (u8 *) peerkey->stk.tk1,
905
 
                           peerkey->cipher == WPA_CIPHER_TKIP ? 32 : 16) < 0) {
906
 
                wpa_printf(MSG_WARNING, "RSN: Failed to set STK to the "
907
 
                           "driver.");
908
 
                return;
909
 
        }
910
 
}
911
 
 
912
 
 
913
 
/**
914
 
 * peerkey_verify_eapol_key_mic - Verify PeerKey MIC
915
 
 * @sm: Pointer to WPA state machine data from wpa_sm_init()
916
 
 * @peerkey: Pointer to the PeerKey data for the peer
917
 
 * @key: Pointer to the EAPOL-Key frame header
918
 
 * @ver: Version bits from EAPOL-Key Key Info
919
 
 * @buf: Pointer to the beginning of EAPOL-Key frame
920
 
 * @len: Length of the EAPOL-Key frame
921
 
 * Returns: 0 on success, -1 on failure
922
 
 */
923
 
int peerkey_verify_eapol_key_mic(struct wpa_sm *sm,
924
 
                                 struct wpa_peerkey *peerkey,
925
 
                                 struct wpa_eapol_key *key, u16 ver,
926
 
                                 const u8 *buf, size_t len)
927
 
{
928
 
        u8 mic[16];
929
 
        int ok = 0;
930
 
 
931
 
        if (peerkey->initiator && !peerkey->stk_set) {
932
 
                wpa_pmk_to_ptk(peerkey->smk, PMK_LEN, "Peer key expansion",
933
 
                               sm->own_addr, peerkey->addr,
934
 
                               peerkey->inonce, key->key_nonce,
935
 
                               (u8 *) &peerkey->stk, sizeof(peerkey->stk));
936
 
                peerkey->stk_set = 1;
937
 
        }
938
 
 
939
 
        os_memcpy(mic, key->key_mic, 16);
940
 
        if (peerkey->tstk_set) {
941
 
                os_memset(key->key_mic, 0, 16);
942
 
                wpa_eapol_key_mic(peerkey->tstk.kck, ver, buf, len,
943
 
                                  key->key_mic);
944
 
                if (os_memcmp(mic, key->key_mic, 16) != 0) {
945
 
                        wpa_printf(MSG_WARNING, "RSN: Invalid EAPOL-Key MIC "
946
 
                                   "when using TSTK - ignoring TSTK");
947
 
                } else {
948
 
                        ok = 1;
949
 
                        peerkey->tstk_set = 0;
950
 
                        peerkey->stk_set = 1;
951
 
                        os_memcpy(&peerkey->stk, &peerkey->tstk,
952
 
                                  sizeof(peerkey->stk));
953
 
                }
954
 
        }
955
 
 
956
 
        if (!ok && peerkey->stk_set) {
957
 
                os_memset(key->key_mic, 0, 16);
958
 
                wpa_eapol_key_mic(peerkey->stk.kck, ver, buf, len,
959
 
                                  key->key_mic);
960
 
                if (os_memcmp(mic, key->key_mic, 16) != 0) {
961
 
                        wpa_printf(MSG_WARNING, "RSN: Invalid EAPOL-Key MIC "
962
 
                                   "- dropping packet");
963
 
                        return -1;
964
 
                }
965
 
                ok = 1;
966
 
        }
967
 
 
968
 
        if (!ok) {
969
 
                wpa_printf(MSG_WARNING, "RSN: Could not verify EAPOL-Key MIC "
970
 
                           "- dropping packet");
971
 
                return -1;
972
 
        }
973
 
 
974
 
        os_memcpy(peerkey->replay_counter, key->replay_counter,
975
 
                  WPA_REPLAY_COUNTER_LEN);
976
 
        peerkey->replay_counter_set = 1;
977
 
        return 0;
978
 
}
979
 
 
980
 
 
981
 
/**
982
 
 * wpa_sm_stkstart - Send EAPOL-Key Request for STK handshake (STK M1)
983
 
 * @sm: Pointer to WPA state machine data from wpa_sm_init()
984
 
 * @peer: MAC address of the peer STA
985
 
 * Returns: 0 on success, or -1 on failure
986
 
 *
987
 
 * Send an EAPOL-Key Request to the current authenticator to start STK
988
 
 * handshake with the peer.
989
 
 */
990
 
int wpa_sm_stkstart(struct wpa_sm *sm, const u8 *peer)
991
 
{
992
 
        size_t rlen, kde_len;
993
 
        struct wpa_eapol_key *req;
994
 
        int key_info, ver;
995
 
        u8 bssid[ETH_ALEN], *rbuf, *pos, *count_pos;
996
 
        u16 count;
997
 
        struct wpa_ssid *ssid = sm->cur_ssid;
998
 
        struct rsn_ie_hdr *hdr;
999
 
        struct wpa_peerkey *peerkey;
1000
 
        struct wpa_ie_data ie;
1001
 
 
1002
 
        if (sm->proto != WPA_PROTO_RSN || !sm->ptk_set ||
1003
 
            ssid == NULL || !ssid->peerkey)
1004
 
                return -1;
1005
 
 
1006
 
        if (sm->ap_rsn_ie &&
1007
 
            wpa_parse_wpa_ie_rsn(sm->ap_rsn_ie, sm->ap_rsn_ie_len, &ie) == 0 &&
1008
 
            !(ie.capabilities & WPA_CAPABILITY_PEERKEY_ENABLED)) {
1009
 
                wpa_printf(MSG_DEBUG, "RSN: Current AP does not support STK");
1010
 
                return -1;
1011
 
        }
1012
 
 
1013
 
        if (sm->pairwise_cipher == WPA_CIPHER_CCMP)
1014
 
                ver = WPA_KEY_INFO_TYPE_HMAC_SHA1_AES;
1015
 
        else
1016
 
                ver = WPA_KEY_INFO_TYPE_HMAC_MD5_RC4;
1017
 
 
1018
 
        if (wpa_sm_get_bssid(sm, bssid) < 0) {
1019
 
                wpa_printf(MSG_WARNING, "Failed to read BSSID for EAPOL-Key "
1020
 
                           "SMK M1");
1021
 
                return -1;
1022
 
        }
1023
 
 
1024
 
        /* TODO: find existing entry and if found, use that instead of adding
1025
 
         * a new one */
1026
 
        peerkey = os_malloc(sizeof(*peerkey));
1027
 
        if (peerkey == NULL)
1028
 
                return -1;
1029
 
        os_memset(peerkey, 0, sizeof(*peerkey));
1030
 
        peerkey->initiator = 1;
1031
 
        os_memcpy(peerkey->addr, peer, ETH_ALEN);
1032
 
 
1033
 
        /* SMK M1:
1034
 
         * EAPOL-Key(S=1, M=1, A=0, I=0, K=0, SM=1, KeyRSC=0, Nonce=INonce,
1035
 
         *           MIC=MIC, DataKDs=(RSNIE_I, MAC_P KDE))
1036
 
         */
1037
 
 
1038
 
        hdr = (struct rsn_ie_hdr *) peerkey->rsnie_i;
1039
 
        hdr->elem_id = WLAN_EID_RSN;
1040
 
        WPA_PUT_LE16(hdr->version, RSN_VERSION);
1041
 
        pos = (u8 *) (hdr + 1);
1042
 
        /* Group Suite can be anything for SMK RSN IE; receiver will just
1043
 
         * ignore it. */
1044
 
        RSN_SELECTOR_PUT(pos, RSN_CIPHER_SUITE_CCMP);
1045
 
        pos += RSN_SELECTOR_LEN;
1046
 
        count_pos = pos;
1047
 
        pos += 2;
1048
 
 
1049
 
        count = 0;
1050
 
        if (ssid->pairwise_cipher & WPA_CIPHER_CCMP) {
1051
 
                RSN_SELECTOR_PUT(pos, RSN_CIPHER_SUITE_CCMP);
1052
 
                pos += RSN_SELECTOR_LEN;
1053
 
                count++;
1054
 
        }
1055
 
        if (ssid->pairwise_cipher & WPA_CIPHER_TKIP) {
1056
 
                RSN_SELECTOR_PUT(pos, RSN_CIPHER_SUITE_TKIP);
1057
 
                pos += RSN_SELECTOR_LEN;
1058
 
                count++;
1059
 
        }
1060
 
        WPA_PUT_LE16(count_pos, count);
1061
 
 
1062
 
        hdr->len = (pos - peerkey->rsnie_i) - 2;
1063
 
        peerkey->rsnie_i_len = pos - peerkey->rsnie_i;
1064
 
        wpa_hexdump(MSG_DEBUG, "WPA: RSN IE for SMK handshake",
1065
 
                    peerkey->rsnie_i, peerkey->rsnie_i_len);
1066
 
 
1067
 
        kde_len = peerkey->rsnie_i_len + 2 + RSN_SELECTOR_LEN + ETH_ALEN;
1068
 
 
1069
 
        rbuf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_KEY, NULL,
1070
 
                                  sizeof(*req) + kde_len, &rlen,
1071
 
                                  (void *) &req);
1072
 
        if (rbuf == NULL) {
1073
 
                wpa_supplicant_peerkey_free(sm, peerkey);
1074
 
                return -1;
1075
 
        }
1076
 
 
1077
 
        req->type = EAPOL_KEY_TYPE_RSN;
1078
 
        key_info = WPA_KEY_INFO_SMK_MESSAGE | WPA_KEY_INFO_MIC |
1079
 
                WPA_KEY_INFO_SECURE | WPA_KEY_INFO_REQUEST | ver;
1080
 
        WPA_PUT_BE16(req->key_info, key_info);
1081
 
        WPA_PUT_BE16(req->key_length, 0);
1082
 
        os_memcpy(req->replay_counter, sm->request_counter,
1083
 
                  WPA_REPLAY_COUNTER_LEN);
1084
 
        inc_byte_array(sm->request_counter, WPA_REPLAY_COUNTER_LEN);
1085
 
 
1086
 
        if (hostapd_get_rand(peerkey->inonce, WPA_NONCE_LEN)) {
1087
 
                wpa_msg(sm->ctx->ctx, MSG_WARNING,
1088
 
                        "WPA: Failed to get random data for INonce");
1089
 
                os_free(rbuf);
1090
 
                wpa_supplicant_peerkey_free(sm, peerkey);
1091
 
                return -1;
1092
 
        }
1093
 
        os_memcpy(req->key_nonce, peerkey->inonce, WPA_NONCE_LEN);
1094
 
        wpa_hexdump(MSG_DEBUG, "WPA: INonce for SMK handshake",
1095
 
                    req->key_nonce, WPA_NONCE_LEN);
1096
 
 
1097
 
        WPA_PUT_BE16(req->key_data_length, (u16) kde_len);
1098
 
        pos = (u8 *) (req + 1);
1099
 
 
1100
 
        /* Initiator RSN IE */
1101
 
        pos = wpa_add_ie(pos, peerkey->rsnie_i, peerkey->rsnie_i_len);
1102
 
        /* Peer MAC address KDE */
1103
 
        wpa_add_kde(pos, RSN_KEY_DATA_MAC_ADDR, peer, ETH_ALEN);
1104
 
 
1105
 
        wpa_printf(MSG_INFO, "RSN: Sending EAPOL-Key SMK M1 Request (peer "
1106
 
                   MACSTR ")", MAC2STR(peer));
1107
 
        wpa_eapol_key_send(sm, sm->ptk.kck, ver, bssid, ETH_P_EAPOL,
1108
 
                           rbuf, rlen, req->key_mic);
1109
 
 
1110
 
        peerkey->next = sm->peerkey;
1111
 
        sm->peerkey = peerkey;
1112
 
 
1113
 
        return 0;
1114
 
}
1115
 
 
1116
 
 
1117
 
/**
1118
 
 * peerkey_deinit - Free PeerKey values
1119
 
 * @sm: Pointer to WPA state machine data from wpa_sm_init()
1120
 
 */
1121
 
void peerkey_deinit(struct wpa_sm *sm)
1122
 
{
1123
 
        struct wpa_peerkey *prev, *peerkey = sm->peerkey;
1124
 
        while (peerkey) {
1125
 
                prev = peerkey;
1126
 
                peerkey = peerkey->next;
1127
 
                os_free(prev);
1128
 
        }
1129
 
}
1130
 
 
1131
 
 
1132
 
void peerkey_rx_eapol_4way(struct wpa_sm *sm, struct wpa_peerkey *peerkey,
1133
 
                           struct wpa_eapol_key *key, u16 key_info, u16 ver)
1134
 
{
1135
 
        if ((key_info & (WPA_KEY_INFO_MIC | WPA_KEY_INFO_ACK)) ==
1136
 
            (WPA_KEY_INFO_MIC | WPA_KEY_INFO_ACK)) {
1137
 
                /* 3/4 STK 4-Way Handshake */
1138
 
                wpa_supplicant_process_stk_3_of_4(sm, peerkey, key, ver);
1139
 
        } else if (key_info & WPA_KEY_INFO_ACK) {
1140
 
                /* 1/4 STK 4-Way Handshake */
1141
 
                wpa_supplicant_process_stk_1_of_4(sm, peerkey, key, ver);
1142
 
        } else if (key_info & WPA_KEY_INFO_SECURE) {
1143
 
                /* 4/4 STK 4-Way Handshake */
1144
 
                wpa_supplicant_process_stk_4_of_4(sm, peerkey, key, ver);
1145
 
        } else {
1146
 
                /* 2/4 STK 4-Way Handshake */
1147
 
                wpa_supplicant_process_stk_2_of_4(sm, peerkey, key, ver);
1148
 
        }
1149
 
}
1150
 
 
1151
 
 
1152
 
void peerkey_rx_eapol_smk(struct wpa_sm *sm, const u8 *src_addr,
1153
 
                          struct wpa_eapol_key *key, size_t extra_len,
1154
 
                          u16 key_info, u16 ver)
1155
 
{
1156
 
        if (key_info & WPA_KEY_INFO_ERROR) {
1157
 
                /* SMK Error */
1158
 
                wpa_supplicant_process_smk_error(sm, src_addr, key, extra_len);
1159
 
        } else if (key_info & WPA_KEY_INFO_ACK) {
1160
 
                /* SMK M2 */
1161
 
                wpa_supplicant_process_smk_m2(sm, src_addr, key, extra_len,
1162
 
                                              ver);
1163
 
        } else {
1164
 
                /* SMK M4 or M5 */
1165
 
                wpa_supplicant_process_smk_m45(sm, src_addr, key, extra_len,
1166
 
                                               ver);
1167
 
        }
1168
 
}
1169
 
 
1170
 
#endif /* CONFIG_PEERKEY */