2
* WPA Supplicant - PeerKey for Direct Link Setup (DLS)
3
* Copyright (c) 2006-2007, Jouni Malinen <j@w1.fi>
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.
9
* Alternatively, this software may be distributed under the terms of BSD
12
* See README and COPYING for more details.
21
#include "config_ssid.h"
26
#include "ieee802_11_defs.h"
30
static u8 * wpa_add_ie(u8 *pos, const u8 *ie, size_t ie_len)
32
os_memcpy(pos, ie, ie_len);
37
static u8 * wpa_add_kde(u8 *pos, u32 kde, const u8 *data, size_t data_len)
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);
49
static void wpa_supplicant_smk_timeout(void *eloop_ctx, void *timeout_ctx)
52
struct wpa_sm *sm = eloop_ctx;
53
struct wpa_peerkey *peerkey = timeout_ctx;
55
/* TODO: time out SMK and any STK that was generated using this SMK */
59
static void wpa_supplicant_peerkey_free(struct wpa_sm *sm,
60
struct wpa_peerkey *peerkey)
62
eloop_cancel_timeout(wpa_supplicant_smk_timeout, sm, peerkey);
67
static int wpa_supplicant_send_smk_error(struct wpa_sm *sm, const u8 *dst,
69
u16 mui, u16 error_type, int ver)
72
struct wpa_eapol_key *err;
73
struct rsn_error_kde error;
78
kde_len = 2 + RSN_SELECTOR_LEN + sizeof(error);
80
kde_len += 2 + RSN_SELECTOR_LEN + ETH_ALEN;
82
rbuf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_KEY,
83
NULL, sizeof(*err) + kde_len, &rlen,
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 |
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);
98
WPA_PUT_BE16(err->key_data_length, (u16) kde_len);
99
pos = (u8 *) (err + 1);
102
/* Peer MAC Address KDE */
103
pos = wpa_add_kde(pos, RSN_KEY_DATA_MAC_ADDR, peer, ETH_ALEN);
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));
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);
116
wpa_printf(MSG_DEBUG, "RSN: Sending EAPOL-Key SMK Error "
117
"(mui %d error_type %d)", mui, error_type);
120
wpa_eapol_key_send(sm, sm->ptk.kck, ver, dst, ETH_P_EAPOL,
121
rbuf, rlen, err->key_mic);
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)
133
struct wpa_eapol_key *reply;
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;
143
rbuf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_KEY,
144
NULL, sizeof(*reply) + kde_len, &rlen,
149
reply->type = EAPOL_KEY_TYPE_RSN;
150
key_info = ver | WPA_KEY_INFO_SMK_MESSAGE | WPA_KEY_INFO_MIC |
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);
157
os_memcpy(reply->key_nonce, peerkey->pnonce, WPA_NONCE_LEN);
159
WPA_PUT_BE16(reply->key_data_length, (u16) kde_len);
160
pos = (u8 *) (reply + 1);
163
pos = wpa_add_ie(pos, peerkey->rsnie_p, peerkey->rsnie_p_len);
165
/* Initiator MAC Address KDE */
166
pos = wpa_add_kde(pos, RSN_KEY_DATA_MAC_ADDR, peerkey->addr, ETH_ALEN);
168
/* Initiator Nonce */
169
wpa_add_kde(pos, RSN_KEY_DATA_NONCE, peerkey->inonce, WPA_NONCE_LEN);
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);
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)
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;
188
struct rsn_ie_hdr *hdr;
191
wpa_printf(MSG_DEBUG, "RSN: Received SMK M2");
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");
199
if (wpa_supplicant_parse_ies((const u8 *) (key + 1), extra_len, &kde) <
201
wpa_printf(MSG_INFO, "RSN: Failed to parse KDEs in SMK M2");
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 "
212
wpa_printf(MSG_DEBUG, "RSN: SMK M2 - SMK initiator " MACSTR,
213
MAC2STR(kde.mac_addr));
215
if (kde.rsn_ie_len > PEERKEY_MAX_IE_LEN) {
216
wpa_printf(MSG_INFO, "RSN: Too long Initiator RSN IE in SMK "
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");
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;
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,
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
244
peerkey = os_malloc(sizeof(*peerkey));
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;
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);
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
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);
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;
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);
283
wpa_supplicant_send_smk_m3(sm, src_addr, key, ver, peerkey);
285
peerkey->next = sm->peerkey;
286
sm->peerkey = peerkey;
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
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)
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)
306
char *title = "SMK Name";
308
const size_t len[5] = { 8, WPA_NONCE_LEN, ETH_ALEN, WPA_NONCE_LEN,
310
unsigned char hash[SHA1_MAC_LEN];
312
addr[0] = (u8 *) title;
318
hmac_sha1_vector(smk, PMK_LEN, 5, addr, len, hash);
319
os_memcpy(smkid, hash, PMKID_LEN);
323
static void wpa_supplicant_send_stk_1_of_4(struct wpa_sm *sm,
324
struct wpa_peerkey *peerkey)
327
struct wpa_eapol_key *msg;
332
kde_len = 2 + RSN_SELECTOR_LEN + PMKID_LEN;
334
mbuf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_KEY, NULL,
335
sizeof(*msg) + kde_len, &mlen,
340
msg->type = EAPOL_KEY_TYPE_RSN;
342
if (peerkey->cipher == WPA_CIPHER_CCMP)
343
ver = WPA_KEY_INFO_TYPE_HMAC_SHA1_AES;
345
ver = WPA_KEY_INFO_TYPE_HMAC_MD5_RC4;
347
key_info = ver | WPA_KEY_INFO_KEY_TYPE | WPA_KEY_INFO_ACK;
348
WPA_PUT_BE16(msg->key_info, key_info);
350
if (peerkey->cipher == WPA_CIPHER_CCMP)
351
WPA_PUT_BE16(msg->key_length, 16);
353
WPA_PUT_BE16(msg->key_length, 32);
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);
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);
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)");
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);
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,
380
static void wpa_supplicant_send_stk_3_of_4(struct wpa_sm *sm,
381
struct wpa_peerkey *peerkey)
384
struct wpa_eapol_key *msg;
390
kde_len = peerkey->rsnie_i_len +
391
2 + RSN_SELECTOR_LEN + sizeof(lifetime);
393
mbuf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_KEY, NULL,
394
sizeof(*msg) + kde_len, &mlen,
399
msg->type = EAPOL_KEY_TYPE_RSN;
401
if (peerkey->cipher == WPA_CIPHER_CCMP)
402
ver = WPA_KEY_INFO_TYPE_HMAC_SHA1_AES;
404
ver = WPA_KEY_INFO_TYPE_HMAC_MD5_RC4;
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);
410
if (peerkey->cipher == WPA_CIPHER_CCMP)
411
WPA_PUT_BE16(msg->key_length, 16);
413
WPA_PUT_BE16(msg->key_length, 32);
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);
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));
426
os_memcpy(msg->key_nonce, peerkey->inonce, WPA_NONCE_LEN);
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);
435
static int wpa_supplicant_process_smk_m4(struct wpa_peerkey *peerkey,
436
struct wpa_eapol_ie_parse *kde)
438
wpa_printf(MSG_DEBUG, "RSN: Received SMK M4 (Initiator " MACSTR ")",
439
MAC2STR(kde->mac_addr));
441
if (os_memcmp(kde->smk + PMK_LEN, peerkey->pnonce, WPA_NONCE_LEN) != 0)
443
wpa_printf(MSG_INFO, "RSN: PNonce in SMK KDE does not "
444
"match with the one used in SMK M3");
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");
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,
462
struct wpa_peerkey *peerkey,
463
struct wpa_eapol_ie_parse *kde)
466
struct wpa_ie_data ie;
467
struct wpa_ssid *ssid = sm->cur_ssid;
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 */
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");
484
if (os_memcmp(kde->smk + PMK_LEN, peerkey->inonce, WPA_NONCE_LEN) != 0)
486
wpa_printf(MSG_INFO, "RSN: INonce in SMK KDE does not "
487
"match with the one used in SMK M1");
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);
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;
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,
508
/* TODO: abort negotiation */
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)
520
struct wpa_ssid *ssid = sm->cur_ssid;
521
struct wpa_peerkey *peerkey;
522
struct wpa_eapol_ie_parse kde;
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");
532
if (wpa_supplicant_parse_ies((const u8 *) (key + 1), extra_len, &kde) <
534
wpa_printf(MSG_INFO, "RSN: Failed to parse KDEs in SMK M4/M5");
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");
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 :
551
key->key_nonce, WPA_NONCE_LEN) == 0)
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));
561
if (peerkey->initiator) {
562
if (wpa_supplicant_process_smk_m5(sm, src_addr, key, ver,
566
if (wpa_supplicant_process_smk_m4(peerkey, &kde) < 0)
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;
579
peerkey->expiration = now.sec + lifetime;
580
eloop_register_timeout(lifetime, 0, wpa_supplicant_smk_timeout,
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);
588
rsn_smkid(peerkey->smk, peerkey->pnonce, sm->own_addr,
589
peerkey->inonce, peerkey->addr, peerkey->smkid);
591
wpa_hexdump(MSG_DEBUG, "RSN: SMKID", peerkey->smkid, PMKID_LEN);
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)
601
struct wpa_ssid *ssid = sm->cur_ssid;
602
struct wpa_eapol_ie_parse kde;
603
struct rsn_error_kde error;
607
wpa_printf(MSG_DEBUG, "RSN: Received SMK Error");
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");
615
if (wpa_supplicant_parse_ies((const u8 *) (key + 1), extra_len, &kde) <
617
wpa_printf(MSG_INFO, "RSN: Failed to parse KDEs in SMK Error");
621
if (kde.error == NULL || kde.error_len < sizeof(error)) {
622
wpa_printf(MSG_INFO, "RSN: No Error KDE in SMK Error");
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 "
633
be_to_host16(error.mui), error_type,
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;
641
for (peerkey = sm->peerkey; peerkey; peerkey = peerkey->next) {
642
if (os_memcmp(peerkey->addr, kde.mac_addr, ETH_ALEN) ==
646
if (peerkey == NULL) {
647
wpa_printf(MSG_DEBUG, "RSN: No matching SMK handshake "
648
"found for SMK Error");
651
/* TODO: abort SMK/STK handshake and remove all related keys */
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,
663
struct wpa_eapol_ie_parse ie;
665
size_t len, kde_buf_len;
667
u8 buf[8], *kde_buf, *pos;
670
wpa_printf(MSG_DEBUG, "RSN: RX message 1 of STK 4-Way Handshake from "
671
MACSTR " (ver=%d)", MAC2STR(peerkey->addr), ver);
673
os_memset(&ie, 0, sizeof(ie));
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");
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);
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");
694
wpa_hexdump(MSG_DEBUG, "WPA: Renewed PNonce",
695
peerkey->pnonce, WPA_NONCE_LEN);
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;
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);
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);
723
if (wpa_supplicant_send_2_of_4(sm, peerkey->addr, key, ver,
724
peerkey->pnonce, kde_buf, kde_buf_len,
731
os_memcpy(peerkey->inonce, key->key_nonce, WPA_NONCE_LEN);
735
static void wpa_supplicant_update_smk_lifetime(struct wpa_sm *sm,
736
struct wpa_peerkey *peerkey,
737
struct wpa_eapol_ie_parse *kde)
742
if (kde->lifetime == NULL || kde->lifetime_len < sizeof(lifetime))
745
lifetime = WPA_GET_BE32(kde->lifetime);
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);
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;
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,
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,
772
struct wpa_eapol_ie_parse kde;
776
wpa_printf(MSG_DEBUG, "RSN: RX message 2 of STK 4-Way Handshake from "
777
MACSTR " (ver=%d)", MAC2STR(peerkey->addr), ver);
779
os_memset(&kde, 0, sizeof(kde));
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");
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);
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);
809
wpa_supplicant_update_smk_lifetime(sm, peerkey, &kde);
811
wpa_supplicant_send_stk_3_of_4(sm, peerkey);
812
os_memcpy(peerkey->pnonce, key->key_nonce, WPA_NONCE_LEN);
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,
821
struct wpa_eapol_ie_parse kde;
825
u8 key_buf[32], rsc[6];
827
wpa_printf(MSG_DEBUG, "RSN: RX message 3 of STK 4-Way Handshake from "
828
MACSTR " (ver=%d)", MAC2STR(peerkey->addr), ver);
830
os_memset(&kde, 0, sizeof(kde));
832
/* RSN: msg 3/4 should contain Initiator RSN IE. It may also include
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 "
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 "
849
peerkey->rsnie_i, peerkey->rsnie_i_len);
850
wpa_hexdump(MSG_DEBUG, "RSN: Initiator RSN IE in STK "
852
kde.rsn_ie, kde.rsn_ie_len);
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));
864
wpa_supplicant_update_smk_lifetime(sm, peerkey, &kde);
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))
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);
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 "
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,
899
wpa_printf(MSG_DEBUG, "RSN: RX message 4 of STK 4-Way Handshake from "
900
MACSTR " (ver=%d)", MAC2STR(peerkey->addr), ver);
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 "
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
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)
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;
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,
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");
949
peerkey->tstk_set = 0;
950
peerkey->stk_set = 1;
951
os_memcpy(&peerkey->stk, &peerkey->tstk,
952
sizeof(peerkey->stk));
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,
960
if (os_memcmp(mic, key->key_mic, 16) != 0) {
961
wpa_printf(MSG_WARNING, "RSN: Invalid EAPOL-Key MIC "
962
"- dropping packet");
969
wpa_printf(MSG_WARNING, "RSN: Could not verify EAPOL-Key MIC "
970
"- dropping packet");
974
os_memcpy(peerkey->replay_counter, key->replay_counter,
975
WPA_REPLAY_COUNTER_LEN);
976
peerkey->replay_counter_set = 1;
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
987
* Send an EAPOL-Key Request to the current authenticator to start STK
988
* handshake with the peer.
990
int wpa_sm_stkstart(struct wpa_sm *sm, const u8 *peer)
992
size_t rlen, kde_len;
993
struct wpa_eapol_key *req;
995
u8 bssid[ETH_ALEN], *rbuf, *pos, *count_pos;
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;
1002
if (sm->proto != WPA_PROTO_RSN || !sm->ptk_set ||
1003
ssid == NULL || !ssid->peerkey)
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");
1013
if (sm->pairwise_cipher == WPA_CIPHER_CCMP)
1014
ver = WPA_KEY_INFO_TYPE_HMAC_SHA1_AES;
1016
ver = WPA_KEY_INFO_TYPE_HMAC_MD5_RC4;
1018
if (wpa_sm_get_bssid(sm, bssid) < 0) {
1019
wpa_printf(MSG_WARNING, "Failed to read BSSID for EAPOL-Key "
1024
/* TODO: find existing entry and if found, use that instead of adding
1026
peerkey = os_malloc(sizeof(*peerkey));
1027
if (peerkey == NULL)
1029
os_memset(peerkey, 0, sizeof(*peerkey));
1030
peerkey->initiator = 1;
1031
os_memcpy(peerkey->addr, peer, ETH_ALEN);
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))
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
1044
RSN_SELECTOR_PUT(pos, RSN_CIPHER_SUITE_CCMP);
1045
pos += RSN_SELECTOR_LEN;
1050
if (ssid->pairwise_cipher & WPA_CIPHER_CCMP) {
1051
RSN_SELECTOR_PUT(pos, RSN_CIPHER_SUITE_CCMP);
1052
pos += RSN_SELECTOR_LEN;
1055
if (ssid->pairwise_cipher & WPA_CIPHER_TKIP) {
1056
RSN_SELECTOR_PUT(pos, RSN_CIPHER_SUITE_TKIP);
1057
pos += RSN_SELECTOR_LEN;
1060
WPA_PUT_LE16(count_pos, count);
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);
1067
kde_len = peerkey->rsnie_i_len + 2 + RSN_SELECTOR_LEN + ETH_ALEN;
1069
rbuf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_KEY, NULL,
1070
sizeof(*req) + kde_len, &rlen,
1073
wpa_supplicant_peerkey_free(sm, peerkey);
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);
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");
1090
wpa_supplicant_peerkey_free(sm, peerkey);
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);
1097
WPA_PUT_BE16(req->key_data_length, (u16) kde_len);
1098
pos = (u8 *) (req + 1);
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);
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);
1110
peerkey->next = sm->peerkey;
1111
sm->peerkey = peerkey;
1118
* peerkey_deinit - Free PeerKey values
1119
* @sm: Pointer to WPA state machine data from wpa_sm_init()
1121
void peerkey_deinit(struct wpa_sm *sm)
1123
struct wpa_peerkey *prev, *peerkey = sm->peerkey;
1126
peerkey = peerkey->next;
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)
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);
1146
/* 2/4 STK 4-Way Handshake */
1147
wpa_supplicant_process_stk_2_of_4(sm, peerkey, key, ver);
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)
1156
if (key_info & WPA_KEY_INFO_ERROR) {
1158
wpa_supplicant_process_smk_error(sm, src_addr, key, extra_len);
1159
} else if (key_info & WPA_KEY_INFO_ACK) {
1161
wpa_supplicant_process_smk_m2(sm, src_addr, key, extra_len,
1165
wpa_supplicant_process_smk_m45(sm, src_addr, key, extra_len,
1170
#endif /* CONFIG_PEERKEY */