blob: 0b5fe51eec1376d4155a65b5bfd6366d36404e43 [file] [log] [blame]
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001/*
2 * WPA Supplicant - WPA state machine and EAPOL-Key processing
Dmitry Shmidt807291d2015-01-27 13:40:23 -08003 * Copyright (c) 2003-2015, Jouni Malinen <j@w1.fi>
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004 * Copyright(c) 2015 Intel Deutschland GmbH
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005 *
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006 * This software may be distributed under the terms of the BSD license.
7 * See README for more details.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008 */
9
10#include "includes.h"
11
12#include "common.h"
13#include "crypto/aes_wrap.h"
14#include "crypto/crypto.h"
15#include "crypto/random.h"
16#include "common/ieee802_11_defs.h"
17#include "eapol_supp/eapol_supp_sm.h"
18#include "wpa.h"
19#include "eloop.h"
20#include "preauth.h"
21#include "pmksa_cache.h"
22#include "wpa_i.h"
23#include "wpa_ie.h"
24#include "peerkey.h"
25
26
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080027static const u8 null_rsc[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
28
29
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070030/**
31 * wpa_eapol_key_send - Send WPA/RSN EAPOL-Key message
32 * @sm: Pointer to WPA state machine data from wpa_sm_init()
33 * @kck: Key Confirmation Key (KCK, part of PTK)
Dmitry Shmidt807291d2015-01-27 13:40:23 -080034 * @kck_len: KCK length in octets
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070035 * @ver: Version field from Key Info
36 * @dest: Destination address for the frame
37 * @proto: Ethertype (usually ETH_P_EAPOL)
38 * @msg: EAPOL-Key message
39 * @msg_len: Length of message
40 * @key_mic: Pointer to the buffer to which the EAPOL-Key MIC is written
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080041 * Returns: >= 0 on success, < 0 on failure
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070042 */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080043int wpa_eapol_key_send(struct wpa_sm *sm, const u8 *kck, size_t kck_len,
44 int ver, const u8 *dest, u16 proto,
45 u8 *msg, size_t msg_len, u8 *key_mic)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070046{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080047 int ret = -1;
Dmitry Shmidt807291d2015-01-27 13:40:23 -080048 size_t mic_len = wpa_mic_len(sm->key_mgmt);
49
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070050 if (is_zero_ether_addr(dest) && is_zero_ether_addr(sm->bssid)) {
51 /*
52 * Association event was not yet received; try to fetch
53 * BSSID from the driver.
54 */
55 if (wpa_sm_get_bssid(sm, sm->bssid) < 0) {
56 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
57 "WPA: Failed to read BSSID for "
58 "EAPOL-Key destination address");
59 } else {
60 dest = sm->bssid;
61 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
62 "WPA: Use BSSID (" MACSTR
63 ") as the destination for EAPOL-Key",
64 MAC2STR(dest));
65 }
66 }
67 if (key_mic &&
Dmitry Shmidt807291d2015-01-27 13:40:23 -080068 wpa_eapol_key_mic(kck, kck_len, sm->key_mgmt, ver, msg, msg_len,
69 key_mic)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070070 wpa_msg(sm->ctx->msg_ctx, MSG_ERROR,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080071 "WPA: Failed to generate EAPOL-Key version %d key_mgmt 0x%x MIC",
72 ver, sm->key_mgmt);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070073 goto out;
74 }
Dmitry Shmidt807291d2015-01-27 13:40:23 -080075 wpa_hexdump_key(MSG_DEBUG, "WPA: KCK", kck, kck_len);
76 wpa_hexdump(MSG_DEBUG, "WPA: Derived Key MIC", key_mic, mic_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070077 wpa_hexdump(MSG_MSGDUMP, "WPA: TX EAPOL-Key", msg, msg_len);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080078 ret = wpa_sm_ether_send(sm, dest, proto, msg, msg_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070079 eapol_sm_notify_tx_eapol_key(sm->eapol);
80out:
81 os_free(msg);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080082 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070083}
84
85
86/**
87 * wpa_sm_key_request - Send EAPOL-Key Request
88 * @sm: Pointer to WPA state machine data from wpa_sm_init()
89 * @error: Indicate whether this is an Michael MIC error report
90 * @pairwise: 1 = error report for pairwise packet, 0 = for group packet
91 *
92 * Send an EAPOL-Key Request to the current authenticator. This function is
93 * used to request rekeying and it is usually called when a local Michael MIC
94 * failure is detected.
95 */
96void wpa_sm_key_request(struct wpa_sm *sm, int error, int pairwise)
97{
Dmitry Shmidt807291d2015-01-27 13:40:23 -080098 size_t mic_len, hdrlen, rlen;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070099 struct wpa_eapol_key *reply;
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800100 struct wpa_eapol_key_192 *reply192;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700101 int key_info, ver;
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800102 u8 bssid[ETH_ALEN], *rbuf, *key_mic;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700103
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800104 if (sm->key_mgmt == WPA_KEY_MGMT_OSEN ||
105 wpa_key_mgmt_suite_b(sm->key_mgmt))
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800106 ver = WPA_KEY_INFO_TYPE_AKM_DEFINED;
107 else if (wpa_key_mgmt_ft(sm->key_mgmt) ||
108 wpa_key_mgmt_sha256(sm->key_mgmt))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700109 ver = WPA_KEY_INFO_TYPE_AES_128_CMAC;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700110 else if (sm->pairwise_cipher != WPA_CIPHER_TKIP)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700111 ver = WPA_KEY_INFO_TYPE_HMAC_SHA1_AES;
112 else
113 ver = WPA_KEY_INFO_TYPE_HMAC_MD5_RC4;
114
115 if (wpa_sm_get_bssid(sm, bssid) < 0) {
116 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
117 "Failed to read BSSID for EAPOL-Key request");
118 return;
119 }
120
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800121 mic_len = wpa_mic_len(sm->key_mgmt);
122 hdrlen = mic_len == 24 ? sizeof(*reply192) : sizeof(*reply);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700123 rbuf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_KEY, NULL,
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800124 hdrlen, &rlen, (void *) &reply);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700125 if (rbuf == NULL)
126 return;
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800127 reply192 = (struct wpa_eapol_key_192 *) reply;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700128
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800129 reply->type = (sm->proto == WPA_PROTO_RSN ||
130 sm->proto == WPA_PROTO_OSEN) ?
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700131 EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA;
132 key_info = WPA_KEY_INFO_REQUEST | ver;
133 if (sm->ptk_set)
134 key_info |= WPA_KEY_INFO_MIC;
135 if (error)
136 key_info |= WPA_KEY_INFO_ERROR;
137 if (pairwise)
138 key_info |= WPA_KEY_INFO_KEY_TYPE;
139 WPA_PUT_BE16(reply->key_info, key_info);
140 WPA_PUT_BE16(reply->key_length, 0);
141 os_memcpy(reply->replay_counter, sm->request_counter,
142 WPA_REPLAY_COUNTER_LEN);
143 inc_byte_array(sm->request_counter, WPA_REPLAY_COUNTER_LEN);
144
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800145 if (mic_len == 24)
146 WPA_PUT_BE16(reply192->key_data_length, 0);
147 else
148 WPA_PUT_BE16(reply->key_data_length, 0);
149 if (!(key_info & WPA_KEY_INFO_MIC))
150 key_mic = NULL;
151 else
152 key_mic = reply192->key_mic; /* same offset in reply */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700153
154 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
155 "WPA: Sending EAPOL-Key Request (error=%d "
156 "pairwise=%d ptk_set=%d len=%lu)",
157 error, pairwise, sm->ptk_set, (unsigned long) rlen);
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800158 wpa_eapol_key_send(sm, sm->ptk.kck, sm->ptk.kck_len, ver, bssid,
159 ETH_P_EAPOL, rbuf, rlen, key_mic);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700160}
161
162
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800163static void wpa_supplicant_key_mgmt_set_pmk(struct wpa_sm *sm)
164{
165#ifdef CONFIG_IEEE80211R
166 if (sm->key_mgmt == WPA_KEY_MGMT_FT_IEEE8021X) {
167 if (wpa_sm_key_mgmt_set_pmk(sm, sm->xxkey, sm->xxkey_len))
168 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
169 "RSN: Cannot set low order 256 bits of MSK for key management offload");
170 } else {
171#endif /* CONFIG_IEEE80211R */
172 if (wpa_sm_key_mgmt_set_pmk(sm, sm->pmk, sm->pmk_len))
173 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
174 "RSN: Cannot set PMK for key management offload");
175#ifdef CONFIG_IEEE80211R
176 }
177#endif /* CONFIG_IEEE80211R */
178}
179
180
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700181static int wpa_supplicant_get_pmk(struct wpa_sm *sm,
182 const unsigned char *src_addr,
183 const u8 *pmkid)
184{
185 int abort_cached = 0;
186
187 if (pmkid && !sm->cur_pmksa) {
188 /* When using drivers that generate RSN IE, wpa_supplicant may
189 * not have enough time to get the association information
190 * event before receiving this 1/4 message, so try to find a
191 * matching PMKSA cache entry here. */
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800192 sm->cur_pmksa = pmksa_cache_get(sm->pmksa, src_addr, pmkid,
193 NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700194 if (sm->cur_pmksa) {
195 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
196 "RSN: found matching PMKID from PMKSA cache");
197 } else {
198 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
199 "RSN: no matching PMKID found");
200 abort_cached = 1;
201 }
202 }
203
204 if (pmkid && sm->cur_pmksa &&
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700205 os_memcmp_const(pmkid, sm->cur_pmksa->pmkid, PMKID_LEN) == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700206 wpa_hexdump(MSG_DEBUG, "RSN: matched PMKID", pmkid, PMKID_LEN);
207 wpa_sm_set_pmk_from_pmksa(sm);
208 wpa_hexdump_key(MSG_DEBUG, "RSN: PMK from PMKSA cache",
209 sm->pmk, sm->pmk_len);
210 eapol_sm_notify_cached(sm->eapol);
211#ifdef CONFIG_IEEE80211R
212 sm->xxkey_len = 0;
213#endif /* CONFIG_IEEE80211R */
214 } else if (wpa_key_mgmt_wpa_ieee8021x(sm->key_mgmt) && sm->eapol) {
215 int res, pmk_len;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800216
217 if (sm->key_mgmt & WPA_KEY_MGMT_IEEE8021X_SUITE_B_192)
218 pmk_len = PMK_LEN_SUITE_B_192;
219 else
220 pmk_len = PMK_LEN;
221 res = eapol_sm_get_key(sm->eapol, sm->pmk, pmk_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700222 if (res) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800223 if (pmk_len == PMK_LEN) {
224 /*
225 * EAP-LEAP is an exception from other EAP
226 * methods: it uses only 16-byte PMK.
227 */
228 res = eapol_sm_get_key(sm->eapol, sm->pmk, 16);
229 pmk_len = 16;
230 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700231 } else {
232#ifdef CONFIG_IEEE80211R
233 u8 buf[2 * PMK_LEN];
234 if (eapol_sm_get_key(sm->eapol, buf, 2 * PMK_LEN) == 0)
235 {
236 os_memcpy(sm->xxkey, buf + PMK_LEN, PMK_LEN);
237 sm->xxkey_len = PMK_LEN;
238 os_memset(buf, 0, sizeof(buf));
239 }
240#endif /* CONFIG_IEEE80211R */
241 }
242 if (res == 0) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700243 struct rsn_pmksa_cache_entry *sa = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700244 wpa_hexdump_key(MSG_DEBUG, "WPA: PMK from EAPOL state "
245 "machines", sm->pmk, pmk_len);
246 sm->pmk_len = pmk_len;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800247 wpa_supplicant_key_mgmt_set_pmk(sm);
Dmitry Shmidtc55524a2011-07-07 11:18:38 -0700248 if (sm->proto == WPA_PROTO_RSN &&
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800249 !wpa_key_mgmt_suite_b(sm->key_mgmt) &&
Dmitry Shmidtc55524a2011-07-07 11:18:38 -0700250 !wpa_key_mgmt_ft(sm->key_mgmt)) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700251 sa = pmksa_cache_add(sm->pmksa,
252 sm->pmk, pmk_len,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800253 NULL, 0,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700254 src_addr, sm->own_addr,
255 sm->network_ctx,
256 sm->key_mgmt);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700257 }
258 if (!sm->cur_pmksa && pmkid &&
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800259 pmksa_cache_get(sm->pmksa, src_addr, pmkid, NULL))
260 {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700261 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
262 "RSN: the new PMK matches with the "
263 "PMKID");
264 abort_cached = 0;
Jouni Malinen6ec30382015-07-08 20:48:18 +0300265 } else if (sa && !sm->cur_pmksa && pmkid) {
266 /*
267 * It looks like the authentication server
268 * derived mismatching MSK. This should not
269 * really happen, but bugs happen.. There is not
270 * much we can do here without knowing what
271 * exactly caused the server to misbehave.
272 */
273 wpa_dbg(sm->ctx->msg_ctx, MSG_INFO,
274 "RSN: PMKID mismatch - authentication server may have derived different MSK?!");
275 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700276 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700277
278 if (!sm->cur_pmksa)
279 sm->cur_pmksa = sa;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700280 } else {
281 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
282 "WPA: Failed to get master session key from "
283 "EAPOL state machines - key handshake "
284 "aborted");
285 if (sm->cur_pmksa) {
286 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
287 "RSN: Cancelled PMKSA caching "
288 "attempt");
289 sm->cur_pmksa = NULL;
290 abort_cached = 1;
291 } else if (!abort_cached) {
292 return -1;
293 }
294 }
295 }
296
Dmitry Shmidtc55524a2011-07-07 11:18:38 -0700297 if (abort_cached && wpa_key_mgmt_wpa_ieee8021x(sm->key_mgmt) &&
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800298 !wpa_key_mgmt_suite_b(sm->key_mgmt) &&
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800299 !wpa_key_mgmt_ft(sm->key_mgmt) && sm->key_mgmt != WPA_KEY_MGMT_OSEN)
300 {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700301 /* Send EAPOL-Start to trigger full EAP authentication. */
302 u8 *buf;
303 size_t buflen;
304
305 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
306 "RSN: no PMKSA entry found - trigger "
307 "full EAP authentication");
308 buf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_START,
309 NULL, 0, &buflen, NULL);
310 if (buf) {
311 wpa_sm_ether_send(sm, sm->bssid, ETH_P_EAPOL,
312 buf, buflen);
313 os_free(buf);
314 return -2;
315 }
316
317 return -1;
318 }
319
320 return 0;
321}
322
323
324/**
325 * wpa_supplicant_send_2_of_4 - Send message 2 of WPA/RSN 4-Way Handshake
326 * @sm: Pointer to WPA state machine data from wpa_sm_init()
327 * @dst: Destination address for the frame
328 * @key: Pointer to the EAPOL-Key frame header
329 * @ver: Version bits from EAPOL-Key Key Info
330 * @nonce: Nonce value for the EAPOL-Key frame
331 * @wpa_ie: WPA/RSN IE
332 * @wpa_ie_len: Length of the WPA/RSN IE
333 * @ptk: PTK to use for keyed hash and encryption
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800334 * Returns: >= 0 on success, < 0 on failure
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700335 */
336int wpa_supplicant_send_2_of_4(struct wpa_sm *sm, const unsigned char *dst,
337 const struct wpa_eapol_key *key,
338 int ver, const u8 *nonce,
339 const u8 *wpa_ie, size_t wpa_ie_len,
340 struct wpa_ptk *ptk)
341{
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800342 size_t mic_len, hdrlen, rlen;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700343 struct wpa_eapol_key *reply;
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800344 struct wpa_eapol_key_192 *reply192;
345 u8 *rbuf, *key_mic;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700346 u8 *rsn_ie_buf = NULL;
347
348 if (wpa_ie == NULL) {
349 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, "WPA: No wpa_ie set - "
350 "cannot generate msg 2/4");
351 return -1;
352 }
353
354#ifdef CONFIG_IEEE80211R
355 if (wpa_key_mgmt_ft(sm->key_mgmt)) {
356 int res;
357
358 /*
359 * Add PMKR1Name into RSN IE (PMKID-List) and add MDIE and
360 * FTIE from (Re)Association Response.
361 */
362 rsn_ie_buf = os_malloc(wpa_ie_len + 2 + 2 + PMKID_LEN +
363 sm->assoc_resp_ies_len);
364 if (rsn_ie_buf == NULL)
365 return -1;
366 os_memcpy(rsn_ie_buf, wpa_ie, wpa_ie_len);
367 res = wpa_insert_pmkid(rsn_ie_buf, wpa_ie_len,
368 sm->pmk_r1_name);
369 if (res < 0) {
370 os_free(rsn_ie_buf);
371 return -1;
372 }
373 wpa_ie_len += res;
374
375 if (sm->assoc_resp_ies) {
376 os_memcpy(rsn_ie_buf + wpa_ie_len, sm->assoc_resp_ies,
377 sm->assoc_resp_ies_len);
378 wpa_ie_len += sm->assoc_resp_ies_len;
379 }
380
381 wpa_ie = rsn_ie_buf;
382 }
383#endif /* CONFIG_IEEE80211R */
384
385 wpa_hexdump(MSG_DEBUG, "WPA: WPA IE for msg 2/4", wpa_ie, wpa_ie_len);
386
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800387 mic_len = wpa_mic_len(sm->key_mgmt);
388 hdrlen = mic_len == 24 ? sizeof(*reply192) : sizeof(*reply);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700389 rbuf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_KEY,
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800390 NULL, hdrlen + wpa_ie_len,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700391 &rlen, (void *) &reply);
392 if (rbuf == NULL) {
393 os_free(rsn_ie_buf);
394 return -1;
395 }
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800396 reply192 = (struct wpa_eapol_key_192 *) reply;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700397
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800398 reply->type = (sm->proto == WPA_PROTO_RSN ||
399 sm->proto == WPA_PROTO_OSEN) ?
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700400 EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA;
401 WPA_PUT_BE16(reply->key_info,
402 ver | WPA_KEY_INFO_KEY_TYPE | WPA_KEY_INFO_MIC);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800403 if (sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700404 WPA_PUT_BE16(reply->key_length, 0);
405 else
406 os_memcpy(reply->key_length, key->key_length, 2);
407 os_memcpy(reply->replay_counter, key->replay_counter,
408 WPA_REPLAY_COUNTER_LEN);
409 wpa_hexdump(MSG_DEBUG, "WPA: Replay Counter", reply->replay_counter,
410 WPA_REPLAY_COUNTER_LEN);
411
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800412 key_mic = reply192->key_mic; /* same offset for reply and reply192 */
413 if (mic_len == 24) {
414 WPA_PUT_BE16(reply192->key_data_length, wpa_ie_len);
415 os_memcpy(reply192 + 1, wpa_ie, wpa_ie_len);
416 } else {
417 WPA_PUT_BE16(reply->key_data_length, wpa_ie_len);
418 os_memcpy(reply + 1, wpa_ie, wpa_ie_len);
419 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700420 os_free(rsn_ie_buf);
421
422 os_memcpy(reply->key_nonce, nonce, WPA_NONCE_LEN);
423
424 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Sending EAPOL-Key 2/4");
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800425 return wpa_eapol_key_send(sm, ptk->kck, ptk->kck_len, ver, dst,
426 ETH_P_EAPOL, rbuf, rlen, key_mic);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700427}
428
429
430static int wpa_derive_ptk(struct wpa_sm *sm, const unsigned char *src_addr,
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800431 const struct wpa_eapol_key *key, struct wpa_ptk *ptk)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700432{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700433#ifdef CONFIG_IEEE80211R
434 if (wpa_key_mgmt_ft(sm->key_mgmt))
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800435 return wpa_derive_ptk_ft(sm, src_addr, key, ptk);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700436#endif /* CONFIG_IEEE80211R */
437
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800438 return wpa_pmk_to_ptk(sm->pmk, sm->pmk_len, "Pairwise key expansion",
439 sm->own_addr, sm->bssid, sm->snonce,
440 key->key_nonce, ptk, sm->key_mgmt,
441 sm->pairwise_cipher);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700442}
443
444
445static void wpa_supplicant_process_1_of_4(struct wpa_sm *sm,
446 const unsigned char *src_addr,
447 const struct wpa_eapol_key *key,
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700448 u16 ver, const u8 *key_data,
449 size_t key_data_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700450{
451 struct wpa_eapol_ie_parse ie;
452 struct wpa_ptk *ptk;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700453 int res;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800454 u8 *kde, *kde_buf = NULL;
455 size_t kde_len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700456
457 if (wpa_sm_get_network_ctx(sm) == NULL) {
458 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, "WPA: No SSID info "
459 "found (msg 1 of 4)");
460 return;
461 }
462
463 wpa_sm_set_state(sm, WPA_4WAY_HANDSHAKE);
464 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: RX message 1 of 4-Way "
465 "Handshake from " MACSTR " (ver=%d)", MAC2STR(src_addr), ver);
466
467 os_memset(&ie, 0, sizeof(ie));
468
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800469 if (sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700470 /* RSN: msg 1/4 should contain PMKID for the selected PMK */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700471 wpa_hexdump(MSG_DEBUG, "RSN: msg 1/4 key data",
472 key_data, key_data_len);
473 if (wpa_supplicant_parse_ies(key_data, key_data_len, &ie) < 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800474 goto failed;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700475 if (ie.pmkid) {
476 wpa_hexdump(MSG_DEBUG, "RSN: PMKID from "
477 "Authenticator", ie.pmkid, PMKID_LEN);
478 }
479 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700480
481 res = wpa_supplicant_get_pmk(sm, src_addr, ie.pmkid);
482 if (res == -2) {
483 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "RSN: Do not reply to "
484 "msg 1/4 - requesting full EAP authentication");
485 return;
486 }
487 if (res)
488 goto failed;
489
490 if (sm->renew_snonce) {
491 if (random_get_bytes(sm->snonce, WPA_NONCE_LEN)) {
492 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
493 "WPA: Failed to get random data for SNonce");
494 goto failed;
495 }
496 sm->renew_snonce = 0;
497 wpa_hexdump(MSG_DEBUG, "WPA: Renewed SNonce",
498 sm->snonce, WPA_NONCE_LEN);
499 }
500
501 /* Calculate PTK which will be stored as a temporary PTK until it has
502 * been verified when processing message 3/4. */
503 ptk = &sm->tptk;
504 wpa_derive_ptk(sm, src_addr, key, ptk);
Dmitry Shmidt98660862014-03-11 17:26:21 -0700505 if (sm->pairwise_cipher == WPA_CIPHER_TKIP) {
Dmitry Shmidt61593f02014-04-21 16:27:35 -0700506 u8 buf[8];
Dmitry Shmidt98660862014-03-11 17:26:21 -0700507 /* Supplicant: swap tx/rx Mic keys */
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800508 os_memcpy(buf, &ptk->tk[16], 8);
509 os_memcpy(&ptk->tk[16], &ptk->tk[24], 8);
510 os_memcpy(&ptk->tk[24], buf, 8);
Dmitry Shmidt61593f02014-04-21 16:27:35 -0700511 os_memset(buf, 0, sizeof(buf));
Dmitry Shmidt98660862014-03-11 17:26:21 -0700512 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700513 sm->tptk_set = 1;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800514 sm->tk_to_set = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700515
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800516 kde = sm->assoc_wpa_ie;
517 kde_len = sm->assoc_wpa_ie_len;
518
519#ifdef CONFIG_P2P
520 if (sm->p2p) {
521 kde_buf = os_malloc(kde_len + 2 + RSN_SELECTOR_LEN + 1);
522 if (kde_buf) {
523 u8 *pos;
524 wpa_printf(MSG_DEBUG, "P2P: Add IP Address Request KDE "
525 "into EAPOL-Key 2/4");
526 os_memcpy(kde_buf, kde, kde_len);
527 kde = kde_buf;
528 pos = kde + kde_len;
529 *pos++ = WLAN_EID_VENDOR_SPECIFIC;
530 *pos++ = RSN_SELECTOR_LEN + 1;
531 RSN_SELECTOR_PUT(pos, WFA_KEY_DATA_IP_ADDR_REQ);
532 pos += RSN_SELECTOR_LEN;
533 *pos++ = 0x01;
534 kde_len = pos - kde;
535 }
536 }
537#endif /* CONFIG_P2P */
538
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700539 if (wpa_supplicant_send_2_of_4(sm, sm->bssid, key, ver, sm->snonce,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800540 kde, kde_len, ptk) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700541 goto failed;
542
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800543 os_free(kde_buf);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700544 os_memcpy(sm->anonce, key->key_nonce, WPA_NONCE_LEN);
545 return;
546
547failed:
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800548 os_free(kde_buf);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700549 wpa_sm_deauthenticate(sm, WLAN_REASON_UNSPECIFIED);
550}
551
552
553static void wpa_sm_start_preauth(void *eloop_ctx, void *timeout_ctx)
554{
555 struct wpa_sm *sm = eloop_ctx;
556 rsn_preauth_candidate_process(sm);
557}
558
559
560static void wpa_supplicant_key_neg_complete(struct wpa_sm *sm,
561 const u8 *addr, int secure)
562{
563 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
564 "WPA: Key negotiation completed with "
565 MACSTR " [PTK=%s GTK=%s]", MAC2STR(addr),
566 wpa_cipher_txt(sm->pairwise_cipher),
567 wpa_cipher_txt(sm->group_cipher));
568 wpa_sm_cancel_auth_timeout(sm);
569 wpa_sm_set_state(sm, WPA_COMPLETED);
570
571 if (secure) {
572 wpa_sm_mlme_setprotection(
573 sm, addr, MLME_SETPROTECTION_PROTECT_TYPE_RX_TX,
574 MLME_SETPROTECTION_KEY_TYPE_PAIRWISE);
575 eapol_sm_notify_portValid(sm->eapol, TRUE);
576 if (wpa_key_mgmt_wpa_psk(sm->key_mgmt))
577 eapol_sm_notify_eap_success(sm->eapol, TRUE);
578 /*
579 * Start preauthentication after a short wait to avoid a
580 * possible race condition between the data receive and key
581 * configuration after the 4-Way Handshake. This increases the
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800582 * likelihood of the first preauth EAPOL-Start frame getting to
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700583 * the target AP.
584 */
585 eloop_register_timeout(1, 0, wpa_sm_start_preauth, sm, NULL);
586 }
587
588 if (sm->cur_pmksa && sm->cur_pmksa->opportunistic) {
589 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
590 "RSN: Authenticator accepted "
591 "opportunistic PMKSA entry - marking it valid");
592 sm->cur_pmksa->opportunistic = 0;
593 }
594
595#ifdef CONFIG_IEEE80211R
596 if (wpa_key_mgmt_ft(sm->key_mgmt)) {
597 /* Prepare for the next transition */
598 wpa_ft_prepare_auth_request(sm, NULL);
599 }
600#endif /* CONFIG_IEEE80211R */
601}
602
603
604static void wpa_sm_rekey_ptk(void *eloop_ctx, void *timeout_ctx)
605{
606 struct wpa_sm *sm = eloop_ctx;
607 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Request PTK rekeying");
608 wpa_sm_key_request(sm, 0, 1);
609}
610
611
612static int wpa_supplicant_install_ptk(struct wpa_sm *sm,
613 const struct wpa_eapol_key *key)
614{
615 int keylen, rsclen;
616 enum wpa_alg alg;
617 const u8 *key_rsc;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800618
619 if (!sm->tk_to_set) {
620 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
621 "WPA: Do not re-install same PTK to the driver");
622 return 0;
623 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700624
625 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
626 "WPA: Installing PTK to the driver");
627
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700628 if (sm->pairwise_cipher == WPA_CIPHER_NONE) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700629 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Pairwise Cipher "
630 "Suite: NONE - do not use pairwise keys");
631 return 0;
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700632 }
633
634 if (!wpa_cipher_valid_pairwise(sm->pairwise_cipher)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700635 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
636 "WPA: Unsupported pairwise cipher %d",
637 sm->pairwise_cipher);
638 return -1;
639 }
640
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700641 alg = wpa_cipher_to_alg(sm->pairwise_cipher);
642 keylen = wpa_cipher_key_len(sm->pairwise_cipher);
643 rsclen = wpa_cipher_rsc_len(sm->pairwise_cipher);
644
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800645 if (sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700646 key_rsc = null_rsc;
647 } else {
648 key_rsc = key->key_rsc;
649 wpa_hexdump(MSG_DEBUG, "WPA: RSC", key_rsc, rsclen);
650 }
651
652 if (wpa_sm_set_key(sm, alg, sm->bssid, 0, 1, key_rsc, rsclen,
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800653 sm->ptk.tk, keylen) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700654 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
655 "WPA: Failed to set PTK to the "
656 "driver (alg=%d keylen=%d bssid=" MACSTR ")",
657 alg, keylen, MAC2STR(sm->bssid));
658 return -1;
659 }
660
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800661 /* TK is not needed anymore in supplicant */
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800662 os_memset(sm->ptk.tk, 0, WPA_TK_MAX_LEN);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800663 sm->tk_to_set = 0;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800664
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700665 if (sm->wpa_ptk_rekey) {
666 eloop_cancel_timeout(wpa_sm_rekey_ptk, sm, NULL);
667 eloop_register_timeout(sm->wpa_ptk_rekey, 0, wpa_sm_rekey_ptk,
668 sm, NULL);
669 }
670
671 return 0;
672}
673
674
675static int wpa_supplicant_check_group_cipher(struct wpa_sm *sm,
676 int group_cipher,
677 int keylen, int maxkeylen,
678 int *key_rsc_len,
679 enum wpa_alg *alg)
680{
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700681 int klen;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700682
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700683 *alg = wpa_cipher_to_alg(group_cipher);
684 if (*alg == WPA_ALG_NONE) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700685 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
686 "WPA: Unsupported Group Cipher %d",
687 group_cipher);
688 return -1;
689 }
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700690 *key_rsc_len = wpa_cipher_rsc_len(group_cipher);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700691
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700692 klen = wpa_cipher_key_len(group_cipher);
693 if (keylen != klen || maxkeylen < klen) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700694 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
695 "WPA: Unsupported %s Group Cipher key length %d (%d)",
696 wpa_cipher_txt(group_cipher), keylen, maxkeylen);
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700697 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700698 }
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700699 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700700}
701
702
703struct wpa_gtk_data {
704 enum wpa_alg alg;
705 int tx, key_rsc_len, keyidx;
706 u8 gtk[32];
707 int gtk_len;
708};
709
710
711static int wpa_supplicant_install_gtk(struct wpa_sm *sm,
712 const struct wpa_gtk_data *gd,
713 const u8 *key_rsc)
714{
715 const u8 *_gtk = gd->gtk;
716 u8 gtk_buf[32];
717
718 wpa_hexdump_key(MSG_DEBUG, "WPA: Group Key", gd->gtk, gd->gtk_len);
719 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
720 "WPA: Installing GTK to the driver (keyidx=%d tx=%d len=%d)",
721 gd->keyidx, gd->tx, gd->gtk_len);
722 wpa_hexdump(MSG_DEBUG, "WPA: RSC", key_rsc, gd->key_rsc_len);
723 if (sm->group_cipher == WPA_CIPHER_TKIP) {
724 /* Swap Tx/Rx keys for Michael MIC */
725 os_memcpy(gtk_buf, gd->gtk, 16);
726 os_memcpy(gtk_buf + 16, gd->gtk + 24, 8);
727 os_memcpy(gtk_buf + 24, gd->gtk + 16, 8);
728 _gtk = gtk_buf;
729 }
730 if (sm->pairwise_cipher == WPA_CIPHER_NONE) {
731 if (wpa_sm_set_key(sm, gd->alg, NULL,
732 gd->keyidx, 1, key_rsc, gd->key_rsc_len,
733 _gtk, gd->gtk_len) < 0) {
734 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
735 "WPA: Failed to set GTK to the driver "
736 "(Group only)");
Dmitry Shmidt61593f02014-04-21 16:27:35 -0700737 os_memset(gtk_buf, 0, sizeof(gtk_buf));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700738 return -1;
739 }
740 } else if (wpa_sm_set_key(sm, gd->alg, broadcast_ether_addr,
741 gd->keyidx, gd->tx, key_rsc, gd->key_rsc_len,
742 _gtk, gd->gtk_len) < 0) {
743 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
744 "WPA: Failed to set GTK to "
745 "the driver (alg=%d keylen=%d keyidx=%d)",
746 gd->alg, gd->gtk_len, gd->keyidx);
Dmitry Shmidt61593f02014-04-21 16:27:35 -0700747 os_memset(gtk_buf, 0, sizeof(gtk_buf));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700748 return -1;
749 }
Dmitry Shmidt61593f02014-04-21 16:27:35 -0700750 os_memset(gtk_buf, 0, sizeof(gtk_buf));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700751
752 return 0;
753}
754
755
756static int wpa_supplicant_gtk_tx_bit_workaround(const struct wpa_sm *sm,
757 int tx)
758{
759 if (tx && sm->pairwise_cipher != WPA_CIPHER_NONE) {
760 /* Ignore Tx bit for GTK if a pairwise key is used. One AP
761 * seemed to set this bit (incorrectly, since Tx is only when
762 * doing Group Key only APs) and without this workaround, the
763 * data connection does not work because wpa_supplicant
764 * configured non-zero keyidx to be used for unicast. */
765 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
766 "WPA: Tx bit set for GTK, but pairwise "
767 "keys are used - ignore Tx bit");
768 return 0;
769 }
770 return tx;
771}
772
773
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800774static int wpa_supplicant_rsc_relaxation(const struct wpa_sm *sm,
775 const u8 *rsc)
776{
777 int rsclen;
778
779 if (!sm->wpa_rsc_relaxation)
780 return 0;
781
782 rsclen = wpa_cipher_rsc_len(sm->group_cipher);
783
784 /*
785 * Try to detect RSC (endian) corruption issue where the AP sends
786 * the RSC bytes in EAPOL-Key message in the wrong order, both if
787 * it's actually a 6-byte field (as it should be) and if it treats
788 * it as an 8-byte field.
789 * An AP model known to have this bug is the Sapido RB-1632.
790 */
791 if (rsclen == 6 && ((rsc[5] && !rsc[0]) || rsc[6] || rsc[7])) {
792 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
793 "RSC %02x%02x%02x%02x%02x%02x%02x%02x is likely bogus, using 0",
794 rsc[0], rsc[1], rsc[2], rsc[3],
795 rsc[4], rsc[5], rsc[6], rsc[7]);
796
797 return 1;
798 }
799
800 return 0;
801}
802
803
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700804static int wpa_supplicant_pairwise_gtk(struct wpa_sm *sm,
805 const struct wpa_eapol_key *key,
806 const u8 *gtk, size_t gtk_len,
807 int key_info)
808{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700809 struct wpa_gtk_data gd;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800810 const u8 *key_rsc;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700811
812 /*
813 * IEEE Std 802.11i-2004 - 8.5.2 EAPOL-Key frames - Figure 43x
814 * GTK KDE format:
815 * KeyID[bits 0-1], Tx [bit 2], Reserved [bits 3-7]
816 * Reserved [bits 0-7]
817 * GTK
818 */
819
820 os_memset(&gd, 0, sizeof(gd));
821 wpa_hexdump_key(MSG_DEBUG, "RSN: received GTK in pairwise handshake",
822 gtk, gtk_len);
823
824 if (gtk_len < 2 || gtk_len - 2 > sizeof(gd.gtk))
825 return -1;
826
827 gd.keyidx = gtk[0] & 0x3;
828 gd.tx = wpa_supplicant_gtk_tx_bit_workaround(sm,
829 !!(gtk[0] & BIT(2)));
830 gtk += 2;
831 gtk_len -= 2;
832
833 os_memcpy(gd.gtk, gtk, gtk_len);
834 gd.gtk_len = gtk_len;
835
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800836 key_rsc = key->key_rsc;
837 if (wpa_supplicant_rsc_relaxation(sm, key->key_rsc))
838 key_rsc = null_rsc;
839
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800840 if (sm->group_cipher != WPA_CIPHER_GTK_NOT_USED &&
841 (wpa_supplicant_check_group_cipher(sm, sm->group_cipher,
842 gtk_len, gtk_len,
843 &gd.key_rsc_len, &gd.alg) ||
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800844 wpa_supplicant_install_gtk(sm, &gd, key_rsc))) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700845 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
846 "RSN: Failed to install GTK");
Dmitry Shmidt61593f02014-04-21 16:27:35 -0700847 os_memset(&gd, 0, sizeof(gd));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700848 return -1;
849 }
Dmitry Shmidt61593f02014-04-21 16:27:35 -0700850 os_memset(&gd, 0, sizeof(gd));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700851
852 wpa_supplicant_key_neg_complete(sm, sm->bssid,
853 key_info & WPA_KEY_INFO_SECURE);
854 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700855}
856
857
858static int ieee80211w_set_keys(struct wpa_sm *sm,
859 struct wpa_eapol_ie_parse *ie)
860{
861#ifdef CONFIG_IEEE80211W
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -0700862 if (!wpa_cipher_valid_mgmt_group(sm->mgmt_group_cipher))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700863 return 0;
864
865 if (ie->igtk) {
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -0700866 size_t len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700867 const struct wpa_igtk_kde *igtk;
868 u16 keyidx;
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -0700869 len = wpa_cipher_key_len(sm->mgmt_group_cipher);
870 if (ie->igtk_len != WPA_IGTK_KDE_PREFIX_LEN + len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700871 return -1;
872 igtk = (const struct wpa_igtk_kde *) ie->igtk;
873 keyidx = WPA_GET_LE16(igtk->keyid);
874 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: IGTK keyid %d "
875 "pn %02x%02x%02x%02x%02x%02x",
876 keyidx, MAC2STR(igtk->pn));
877 wpa_hexdump_key(MSG_DEBUG, "WPA: IGTK",
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -0700878 igtk->igtk, len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700879 if (keyidx > 4095) {
880 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
881 "WPA: Invalid IGTK KeyID %d", keyidx);
882 return -1;
883 }
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -0700884 if (wpa_sm_set_key(sm, wpa_cipher_to_alg(sm->mgmt_group_cipher),
885 broadcast_ether_addr,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700886 keyidx, 0, igtk->pn, sizeof(igtk->pn),
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -0700887 igtk->igtk, len) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700888 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
889 "WPA: Failed to configure IGTK to the driver");
890 return -1;
891 }
892 }
893
894 return 0;
895#else /* CONFIG_IEEE80211W */
896 return 0;
897#endif /* CONFIG_IEEE80211W */
898}
899
900
901static void wpa_report_ie_mismatch(struct wpa_sm *sm,
902 const char *reason, const u8 *src_addr,
903 const u8 *wpa_ie, size_t wpa_ie_len,
904 const u8 *rsn_ie, size_t rsn_ie_len)
905{
906 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, "WPA: %s (src=" MACSTR ")",
907 reason, MAC2STR(src_addr));
908
909 if (sm->ap_wpa_ie) {
910 wpa_hexdump(MSG_INFO, "WPA: WPA IE in Beacon/ProbeResp",
911 sm->ap_wpa_ie, sm->ap_wpa_ie_len);
912 }
913 if (wpa_ie) {
914 if (!sm->ap_wpa_ie) {
915 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
916 "WPA: No WPA IE in Beacon/ProbeResp");
917 }
918 wpa_hexdump(MSG_INFO, "WPA: WPA IE in 3/4 msg",
919 wpa_ie, wpa_ie_len);
920 }
921
922 if (sm->ap_rsn_ie) {
923 wpa_hexdump(MSG_INFO, "WPA: RSN IE in Beacon/ProbeResp",
924 sm->ap_rsn_ie, sm->ap_rsn_ie_len);
925 }
926 if (rsn_ie) {
927 if (!sm->ap_rsn_ie) {
928 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
929 "WPA: No RSN IE in Beacon/ProbeResp");
930 }
931 wpa_hexdump(MSG_INFO, "WPA: RSN IE in 3/4 msg",
932 rsn_ie, rsn_ie_len);
933 }
934
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800935 wpa_sm_deauthenticate(sm, WLAN_REASON_IE_IN_4WAY_DIFFERS);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700936}
937
938
939#ifdef CONFIG_IEEE80211R
940
941static int ft_validate_mdie(struct wpa_sm *sm,
942 const unsigned char *src_addr,
943 struct wpa_eapol_ie_parse *ie,
944 const u8 *assoc_resp_mdie)
945{
946 struct rsn_mdie *mdie;
947
948 mdie = (struct rsn_mdie *) (ie->mdie + 2);
949 if (ie->mdie == NULL || ie->mdie_len < 2 + sizeof(*mdie) ||
950 os_memcmp(mdie->mobility_domain, sm->mobility_domain,
951 MOBILITY_DOMAIN_ID_LEN) != 0) {
952 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "FT: MDIE in msg 3/4 did "
953 "not match with the current mobility domain");
954 return -1;
955 }
956
957 if (assoc_resp_mdie &&
958 (assoc_resp_mdie[1] != ie->mdie[1] ||
959 os_memcmp(assoc_resp_mdie, ie->mdie, 2 + ie->mdie[1]) != 0)) {
960 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "FT: MDIE mismatch");
961 wpa_hexdump(MSG_DEBUG, "FT: MDIE in EAPOL-Key msg 3/4",
962 ie->mdie, 2 + ie->mdie[1]);
963 wpa_hexdump(MSG_DEBUG, "FT: MDIE in (Re)Association Response",
964 assoc_resp_mdie, 2 + assoc_resp_mdie[1]);
965 return -1;
966 }
967
968 return 0;
969}
970
971
972static int ft_validate_ftie(struct wpa_sm *sm,
973 const unsigned char *src_addr,
974 struct wpa_eapol_ie_parse *ie,
975 const u8 *assoc_resp_ftie)
976{
977 if (ie->ftie == NULL) {
978 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
979 "FT: No FTIE in EAPOL-Key msg 3/4");
980 return -1;
981 }
982
983 if (assoc_resp_ftie == NULL)
984 return 0;
985
986 if (assoc_resp_ftie[1] != ie->ftie[1] ||
987 os_memcmp(assoc_resp_ftie, ie->ftie, 2 + ie->ftie[1]) != 0) {
988 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "FT: FTIE mismatch");
989 wpa_hexdump(MSG_DEBUG, "FT: FTIE in EAPOL-Key msg 3/4",
990 ie->ftie, 2 + ie->ftie[1]);
991 wpa_hexdump(MSG_DEBUG, "FT: FTIE in (Re)Association Response",
992 assoc_resp_ftie, 2 + assoc_resp_ftie[1]);
993 return -1;
994 }
995
996 return 0;
997}
998
999
1000static int ft_validate_rsnie(struct wpa_sm *sm,
1001 const unsigned char *src_addr,
1002 struct wpa_eapol_ie_parse *ie)
1003{
1004 struct wpa_ie_data rsn;
1005
1006 if (!ie->rsn_ie)
1007 return 0;
1008
1009 /*
1010 * Verify that PMKR1Name from EAPOL-Key message 3/4
1011 * matches with the value we derived.
1012 */
1013 if (wpa_parse_wpa_ie_rsn(ie->rsn_ie, ie->rsn_ie_len, &rsn) < 0 ||
1014 rsn.num_pmkid != 1 || rsn.pmkid == NULL) {
1015 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "FT: No PMKR1Name in "
1016 "FT 4-way handshake message 3/4");
1017 return -1;
1018 }
1019
Dmitry Shmidtc2817022014-07-02 10:32:10 -07001020 if (os_memcmp_const(rsn.pmkid, sm->pmk_r1_name, WPA_PMK_NAME_LEN) != 0)
1021 {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001022 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1023 "FT: PMKR1Name mismatch in "
1024 "FT 4-way handshake message 3/4");
1025 wpa_hexdump(MSG_DEBUG, "FT: PMKR1Name from Authenticator",
1026 rsn.pmkid, WPA_PMK_NAME_LEN);
1027 wpa_hexdump(MSG_DEBUG, "FT: Derived PMKR1Name",
1028 sm->pmk_r1_name, WPA_PMK_NAME_LEN);
1029 return -1;
1030 }
1031
1032 return 0;
1033}
1034
1035
1036static int wpa_supplicant_validate_ie_ft(struct wpa_sm *sm,
1037 const unsigned char *src_addr,
1038 struct wpa_eapol_ie_parse *ie)
1039{
1040 const u8 *pos, *end, *mdie = NULL, *ftie = NULL;
1041
1042 if (sm->assoc_resp_ies) {
1043 pos = sm->assoc_resp_ies;
1044 end = pos + sm->assoc_resp_ies_len;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001045 while (end - pos > 2) {
1046 if (2 + pos[1] > end - pos)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001047 break;
1048 switch (*pos) {
1049 case WLAN_EID_MOBILITY_DOMAIN:
1050 mdie = pos;
1051 break;
1052 case WLAN_EID_FAST_BSS_TRANSITION:
1053 ftie = pos;
1054 break;
1055 }
1056 pos += 2 + pos[1];
1057 }
1058 }
1059
1060 if (ft_validate_mdie(sm, src_addr, ie, mdie) < 0 ||
1061 ft_validate_ftie(sm, src_addr, ie, ftie) < 0 ||
1062 ft_validate_rsnie(sm, src_addr, ie) < 0)
1063 return -1;
1064
1065 return 0;
1066}
1067
1068#endif /* CONFIG_IEEE80211R */
1069
1070
1071static int wpa_supplicant_validate_ie(struct wpa_sm *sm,
1072 const unsigned char *src_addr,
1073 struct wpa_eapol_ie_parse *ie)
1074{
1075 if (sm->ap_wpa_ie == NULL && sm->ap_rsn_ie == NULL) {
1076 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1077 "WPA: No WPA/RSN IE for this AP known. "
1078 "Trying to get from scan results");
1079 if (wpa_sm_get_beacon_ie(sm) < 0) {
1080 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1081 "WPA: Could not find AP from "
1082 "the scan results");
1083 } else {
1084 wpa_msg(sm->ctx->msg_ctx, MSG_DEBUG,
1085 "WPA: Found the current AP from "
1086 "updated scan results");
1087 }
1088 }
1089
1090 if (ie->wpa_ie == NULL && ie->rsn_ie == NULL &&
1091 (sm->ap_wpa_ie || sm->ap_rsn_ie)) {
1092 wpa_report_ie_mismatch(sm, "IE in 3/4 msg does not match "
1093 "with IE in Beacon/ProbeResp (no IE?)",
1094 src_addr, ie->wpa_ie, ie->wpa_ie_len,
1095 ie->rsn_ie, ie->rsn_ie_len);
1096 return -1;
1097 }
1098
1099 if ((ie->wpa_ie && sm->ap_wpa_ie &&
1100 (ie->wpa_ie_len != sm->ap_wpa_ie_len ||
1101 os_memcmp(ie->wpa_ie, sm->ap_wpa_ie, ie->wpa_ie_len) != 0)) ||
1102 (ie->rsn_ie && sm->ap_rsn_ie &&
1103 wpa_compare_rsn_ie(wpa_key_mgmt_ft(sm->key_mgmt),
1104 sm->ap_rsn_ie, sm->ap_rsn_ie_len,
1105 ie->rsn_ie, ie->rsn_ie_len))) {
1106 wpa_report_ie_mismatch(sm, "IE in 3/4 msg does not match "
1107 "with IE in Beacon/ProbeResp",
1108 src_addr, ie->wpa_ie, ie->wpa_ie_len,
1109 ie->rsn_ie, ie->rsn_ie_len);
1110 return -1;
1111 }
1112
1113 if (sm->proto == WPA_PROTO_WPA &&
1114 ie->rsn_ie && sm->ap_rsn_ie == NULL && sm->rsn_enabled) {
1115 wpa_report_ie_mismatch(sm, "Possible downgrade attack "
1116 "detected - RSN was enabled and RSN IE "
1117 "was in msg 3/4, but not in "
1118 "Beacon/ProbeResp",
1119 src_addr, ie->wpa_ie, ie->wpa_ie_len,
1120 ie->rsn_ie, ie->rsn_ie_len);
1121 return -1;
1122 }
1123
1124#ifdef CONFIG_IEEE80211R
1125 if (wpa_key_mgmt_ft(sm->key_mgmt) &&
1126 wpa_supplicant_validate_ie_ft(sm, src_addr, ie) < 0)
1127 return -1;
1128#endif /* CONFIG_IEEE80211R */
1129
1130 return 0;
1131}
1132
1133
1134/**
1135 * wpa_supplicant_send_4_of_4 - Send message 4 of WPA/RSN 4-Way Handshake
1136 * @sm: Pointer to WPA state machine data from wpa_sm_init()
1137 * @dst: Destination address for the frame
1138 * @key: Pointer to the EAPOL-Key frame header
1139 * @ver: Version bits from EAPOL-Key Key Info
1140 * @key_info: Key Info
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001141 * @ptk: PTK to use for keyed hash and encryption
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001142 * Returns: >= 0 on success, < 0 on failure
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001143 */
1144int wpa_supplicant_send_4_of_4(struct wpa_sm *sm, const unsigned char *dst,
1145 const struct wpa_eapol_key *key,
1146 u16 ver, u16 key_info,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001147 struct wpa_ptk *ptk)
1148{
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001149 size_t mic_len, hdrlen, rlen;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001150 struct wpa_eapol_key *reply;
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001151 struct wpa_eapol_key_192 *reply192;
1152 u8 *rbuf, *key_mic;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001153
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001154 mic_len = wpa_mic_len(sm->key_mgmt);
1155 hdrlen = mic_len == 24 ? sizeof(*reply192) : sizeof(*reply);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001156 rbuf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_KEY, NULL,
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001157 hdrlen, &rlen, (void *) &reply);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001158 if (rbuf == NULL)
1159 return -1;
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001160 reply192 = (struct wpa_eapol_key_192 *) reply;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001161
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001162 reply->type = (sm->proto == WPA_PROTO_RSN ||
1163 sm->proto == WPA_PROTO_OSEN) ?
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001164 EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA;
1165 key_info &= WPA_KEY_INFO_SECURE;
1166 key_info |= ver | WPA_KEY_INFO_KEY_TYPE | WPA_KEY_INFO_MIC;
1167 WPA_PUT_BE16(reply->key_info, key_info);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001168 if (sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001169 WPA_PUT_BE16(reply->key_length, 0);
1170 else
1171 os_memcpy(reply->key_length, key->key_length, 2);
1172 os_memcpy(reply->replay_counter, key->replay_counter,
1173 WPA_REPLAY_COUNTER_LEN);
1174
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001175 key_mic = reply192->key_mic; /* same offset for reply and reply192 */
1176 if (mic_len == 24)
1177 WPA_PUT_BE16(reply192->key_data_length, 0);
1178 else
1179 WPA_PUT_BE16(reply->key_data_length, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001180
1181 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Sending EAPOL-Key 4/4");
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001182 return wpa_eapol_key_send(sm, ptk->kck, ptk->kck_len, ver, dst,
1183 ETH_P_EAPOL, rbuf, rlen, key_mic);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001184}
1185
1186
1187static void wpa_supplicant_process_3_of_4(struct wpa_sm *sm,
1188 const struct wpa_eapol_key *key,
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001189 u16 ver, const u8 *key_data,
1190 size_t key_data_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001191{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001192 u16 key_info, keylen;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001193 struct wpa_eapol_ie_parse ie;
1194
1195 wpa_sm_set_state(sm, WPA_4WAY_HANDSHAKE);
1196 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: RX message 3 of 4-Way "
1197 "Handshake from " MACSTR " (ver=%d)", MAC2STR(sm->bssid), ver);
1198
1199 key_info = WPA_GET_BE16(key->key_info);
1200
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001201 wpa_hexdump(MSG_DEBUG, "WPA: IE KeyData", key_data, key_data_len);
1202 if (wpa_supplicant_parse_ies(key_data, key_data_len, &ie) < 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001203 goto failed;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001204 if (ie.gtk && !(key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
1205 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1206 "WPA: GTK IE in unencrypted key data");
1207 goto failed;
1208 }
1209#ifdef CONFIG_IEEE80211W
1210 if (ie.igtk && !(key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
1211 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1212 "WPA: IGTK KDE in unencrypted key data");
1213 goto failed;
1214 }
1215
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001216 if (ie.igtk &&
1217 wpa_cipher_valid_mgmt_group(sm->mgmt_group_cipher) &&
1218 ie.igtk_len != WPA_IGTK_KDE_PREFIX_LEN +
1219 (unsigned int) wpa_cipher_key_len(sm->mgmt_group_cipher)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001220 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1221 "WPA: Invalid IGTK KDE length %lu",
1222 (unsigned long) ie.igtk_len);
1223 goto failed;
1224 }
1225#endif /* CONFIG_IEEE80211W */
1226
1227 if (wpa_supplicant_validate_ie(sm, sm->bssid, &ie) < 0)
1228 goto failed;
1229
1230 if (os_memcmp(sm->anonce, key->key_nonce, WPA_NONCE_LEN) != 0) {
1231 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1232 "WPA: ANonce from message 1 of 4-Way Handshake "
1233 "differs from 3 of 4-Way Handshake - drop packet (src="
1234 MACSTR ")", MAC2STR(sm->bssid));
1235 goto failed;
1236 }
1237
1238 keylen = WPA_GET_BE16(key->key_length);
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07001239 if (keylen != wpa_cipher_key_len(sm->pairwise_cipher)) {
1240 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1241 "WPA: Invalid %s key length %d (src=" MACSTR
1242 ")", wpa_cipher_txt(sm->pairwise_cipher), keylen,
1243 MAC2STR(sm->bssid));
1244 goto failed;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001245 }
1246
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001247#ifdef CONFIG_P2P
1248 if (ie.ip_addr_alloc) {
1249 os_memcpy(sm->p2p_ip_addr, ie.ip_addr_alloc, 3 * 4);
1250 wpa_hexdump(MSG_DEBUG, "P2P: IP address info",
1251 sm->p2p_ip_addr, sizeof(sm->p2p_ip_addr));
1252 }
1253#endif /* CONFIG_P2P */
1254
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001255 if (wpa_supplicant_send_4_of_4(sm, sm->bssid, key, ver, key_info,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001256 &sm->ptk) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001257 goto failed;
1258 }
1259
1260 /* SNonce was successfully used in msg 3/4, so mark it to be renewed
1261 * for the next 4-Way Handshake. If msg 3 is received again, the old
1262 * SNonce will still be used to avoid changing PTK. */
1263 sm->renew_snonce = 1;
1264
1265 if (key_info & WPA_KEY_INFO_INSTALL) {
1266 if (wpa_supplicant_install_ptk(sm, key))
1267 goto failed;
1268 }
1269
1270 if (key_info & WPA_KEY_INFO_SECURE) {
1271 wpa_sm_mlme_setprotection(
1272 sm, sm->bssid, MLME_SETPROTECTION_PROTECT_TYPE_RX,
1273 MLME_SETPROTECTION_KEY_TYPE_PAIRWISE);
1274 eapol_sm_notify_portValid(sm->eapol, TRUE);
1275 }
1276 wpa_sm_set_state(sm, WPA_GROUP_HANDSHAKE);
1277
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001278 if (sm->group_cipher == WPA_CIPHER_GTK_NOT_USED) {
1279 wpa_supplicant_key_neg_complete(sm, sm->bssid,
1280 key_info & WPA_KEY_INFO_SECURE);
1281 } else if (ie.gtk &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001282 wpa_supplicant_pairwise_gtk(sm, key,
1283 ie.gtk, ie.gtk_len, key_info) < 0) {
1284 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1285 "RSN: Failed to configure GTK");
1286 goto failed;
1287 }
1288
1289 if (ieee80211w_set_keys(sm, &ie) < 0) {
1290 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1291 "RSN: Failed to configure IGTK");
1292 goto failed;
1293 }
1294
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001295 if (ie.gtk)
1296 wpa_sm_set_rekey_offload(sm);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001297
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001298 if (sm->proto == WPA_PROTO_RSN && wpa_key_mgmt_suite_b(sm->key_mgmt)) {
1299 struct rsn_pmksa_cache_entry *sa;
1300
1301 sa = pmksa_cache_add(sm->pmksa, sm->pmk, sm->pmk_len,
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001302 sm->ptk.kck, sm->ptk.kck_len,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001303 sm->bssid, sm->own_addr,
1304 sm->network_ctx, sm->key_mgmt);
1305 if (!sm->cur_pmksa)
1306 sm->cur_pmksa = sa;
1307 }
1308
Dmitry Shmidt4dd28dc2015-03-10 11:21:43 -07001309 sm->msg_3_of_4_ok = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001310 return;
1311
1312failed:
1313 wpa_sm_deauthenticate(sm, WLAN_REASON_UNSPECIFIED);
1314}
1315
1316
1317static int wpa_supplicant_process_1_of_2_rsn(struct wpa_sm *sm,
1318 const u8 *keydata,
1319 size_t keydatalen,
1320 u16 key_info,
1321 struct wpa_gtk_data *gd)
1322{
1323 int maxkeylen;
1324 struct wpa_eapol_ie_parse ie;
1325
1326 wpa_hexdump(MSG_DEBUG, "RSN: msg 1/2 key data", keydata, keydatalen);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001327 if (wpa_supplicant_parse_ies(keydata, keydatalen, &ie) < 0)
1328 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001329 if (ie.gtk && !(key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
1330 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1331 "WPA: GTK IE in unencrypted key data");
1332 return -1;
1333 }
1334 if (ie.gtk == NULL) {
1335 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1336 "WPA: No GTK IE in Group Key msg 1/2");
1337 return -1;
1338 }
1339 maxkeylen = gd->gtk_len = ie.gtk_len - 2;
1340
1341 if (wpa_supplicant_check_group_cipher(sm, sm->group_cipher,
1342 gd->gtk_len, maxkeylen,
1343 &gd->key_rsc_len, &gd->alg))
1344 return -1;
1345
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001346 wpa_hexdump_key(MSG_DEBUG, "RSN: received GTK in group key handshake",
1347 ie.gtk, ie.gtk_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001348 gd->keyidx = ie.gtk[0] & 0x3;
1349 gd->tx = wpa_supplicant_gtk_tx_bit_workaround(sm,
1350 !!(ie.gtk[0] & BIT(2)));
1351 if (ie.gtk_len - 2 > sizeof(gd->gtk)) {
1352 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1353 "RSN: Too long GTK in GTK IE (len=%lu)",
1354 (unsigned long) ie.gtk_len - 2);
1355 return -1;
1356 }
1357 os_memcpy(gd->gtk, ie.gtk + 2, ie.gtk_len - 2);
1358
1359 if (ieee80211w_set_keys(sm, &ie) < 0)
1360 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1361 "RSN: Failed to configure IGTK");
1362
1363 return 0;
1364}
1365
1366
1367static int wpa_supplicant_process_1_of_2_wpa(struct wpa_sm *sm,
1368 const struct wpa_eapol_key *key,
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001369 const u8 *key_data,
1370 size_t key_data_len, u16 key_info,
1371 u16 ver, struct wpa_gtk_data *gd)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001372{
1373 size_t maxkeylen;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001374 u16 gtk_len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001375
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001376 gtk_len = WPA_GET_BE16(key->key_length);
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001377 maxkeylen = key_data_len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001378 if (ver == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
1379 if (maxkeylen < 8) {
1380 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1381 "WPA: Too short maxkeylen (%lu)",
1382 (unsigned long) maxkeylen);
1383 return -1;
1384 }
1385 maxkeylen -= 8;
1386 }
1387
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001388 if (gtk_len > maxkeylen ||
1389 wpa_supplicant_check_group_cipher(sm, sm->group_cipher,
1390 gtk_len, maxkeylen,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001391 &gd->key_rsc_len, &gd->alg))
1392 return -1;
1393
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001394 gd->gtk_len = gtk_len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001395 gd->keyidx = (key_info & WPA_KEY_INFO_KEY_INDEX_MASK) >>
1396 WPA_KEY_INFO_KEY_INDEX_SHIFT;
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001397 if (ver == WPA_KEY_INFO_TYPE_HMAC_MD5_RC4 && sm->ptk.kek_len == 16) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001398#ifdef CONFIG_NO_RC4
1399 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1400 "WPA: RC4 not supported in the build");
1401 return -1;
1402#else /* CONFIG_NO_RC4 */
Dmitry Shmidt61593f02014-04-21 16:27:35 -07001403 u8 ek[32];
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001404 if (key_data_len > sizeof(gd->gtk)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001405 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1406 "WPA: RC4 key data too long (%lu)",
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001407 (unsigned long) key_data_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001408 return -1;
1409 }
Dmitry Shmidt61593f02014-04-21 16:27:35 -07001410 os_memcpy(ek, key->key_iv, 16);
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001411 os_memcpy(ek + 16, sm->ptk.kek, sm->ptk.kek_len);
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001412 os_memcpy(gd->gtk, key_data, key_data_len);
1413 if (rc4_skip(ek, 32, 256, gd->gtk, key_data_len)) {
Dmitry Shmidt61593f02014-04-21 16:27:35 -07001414 os_memset(ek, 0, sizeof(ek));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001415 wpa_msg(sm->ctx->msg_ctx, MSG_ERROR,
1416 "WPA: RC4 failed");
1417 return -1;
1418 }
Dmitry Shmidt61593f02014-04-21 16:27:35 -07001419 os_memset(ek, 0, sizeof(ek));
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001420#endif /* CONFIG_NO_RC4 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001421 } else if (ver == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001422 if (maxkeylen % 8) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001423 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1424 "WPA: Unsupported AES-WRAP len %lu",
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001425 (unsigned long) maxkeylen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001426 return -1;
1427 }
1428 if (maxkeylen > sizeof(gd->gtk)) {
1429 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1430 "WPA: AES-WRAP key data "
1431 "too long (keydatalen=%lu maxkeylen=%lu)",
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001432 (unsigned long) key_data_len,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001433 (unsigned long) maxkeylen);
1434 return -1;
1435 }
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001436 if (aes_unwrap(sm->ptk.kek, sm->ptk.kek_len, maxkeylen / 8,
1437 key_data, gd->gtk)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001438 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1439 "WPA: AES unwrap failed - could not decrypt "
1440 "GTK");
1441 return -1;
1442 }
1443 } else {
1444 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1445 "WPA: Unsupported key_info type %d", ver);
1446 return -1;
1447 }
1448 gd->tx = wpa_supplicant_gtk_tx_bit_workaround(
1449 sm, !!(key_info & WPA_KEY_INFO_TXRX));
1450 return 0;
1451}
1452
1453
1454static int wpa_supplicant_send_2_of_2(struct wpa_sm *sm,
1455 const struct wpa_eapol_key *key,
1456 int ver, u16 key_info)
1457{
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001458 size_t mic_len, hdrlen, rlen;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001459 struct wpa_eapol_key *reply;
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001460 struct wpa_eapol_key_192 *reply192;
1461 u8 *rbuf, *key_mic;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001462
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001463 mic_len = wpa_mic_len(sm->key_mgmt);
1464 hdrlen = mic_len == 24 ? sizeof(*reply192) : sizeof(*reply);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001465 rbuf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_KEY, NULL,
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001466 hdrlen, &rlen, (void *) &reply);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001467 if (rbuf == NULL)
1468 return -1;
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001469 reply192 = (struct wpa_eapol_key_192 *) reply;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001470
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001471 reply->type = (sm->proto == WPA_PROTO_RSN ||
1472 sm->proto == WPA_PROTO_OSEN) ?
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001473 EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA;
1474 key_info &= WPA_KEY_INFO_KEY_INDEX_MASK;
1475 key_info |= ver | WPA_KEY_INFO_MIC | WPA_KEY_INFO_SECURE;
1476 WPA_PUT_BE16(reply->key_info, key_info);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001477 if (sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001478 WPA_PUT_BE16(reply->key_length, 0);
1479 else
1480 os_memcpy(reply->key_length, key->key_length, 2);
1481 os_memcpy(reply->replay_counter, key->replay_counter,
1482 WPA_REPLAY_COUNTER_LEN);
1483
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001484 key_mic = reply192->key_mic; /* same offset for reply and reply192 */
1485 if (mic_len == 24)
1486 WPA_PUT_BE16(reply192->key_data_length, 0);
1487 else
1488 WPA_PUT_BE16(reply->key_data_length, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001489
1490 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Sending EAPOL-Key 2/2");
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001491 return wpa_eapol_key_send(sm, sm->ptk.kck, sm->ptk.kck_len, ver,
1492 sm->bssid, ETH_P_EAPOL, rbuf, rlen, key_mic);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001493}
1494
1495
1496static void wpa_supplicant_process_1_of_2(struct wpa_sm *sm,
1497 const unsigned char *src_addr,
1498 const struct wpa_eapol_key *key,
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001499 const u8 *key_data,
1500 size_t key_data_len, u16 ver)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001501{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001502 u16 key_info;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001503 int rekey, ret;
1504 struct wpa_gtk_data gd;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001505 const u8 *key_rsc;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001506
Dmitry Shmidt4dd28dc2015-03-10 11:21:43 -07001507 if (!sm->msg_3_of_4_ok) {
1508 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1509 "WPA: Group Key Handshake started prior to completion of 4-way handshake");
1510 goto failed;
1511 }
1512
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001513 os_memset(&gd, 0, sizeof(gd));
1514
1515 rekey = wpa_sm_get_state(sm) == WPA_COMPLETED;
1516 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: RX message 1 of Group Key "
1517 "Handshake from " MACSTR " (ver=%d)", MAC2STR(src_addr), ver);
1518
1519 key_info = WPA_GET_BE16(key->key_info);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001520
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001521 if (sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN) {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001522 ret = wpa_supplicant_process_1_of_2_rsn(sm, key_data,
1523 key_data_len, key_info,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001524 &gd);
1525 } else {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001526 ret = wpa_supplicant_process_1_of_2_wpa(sm, key, key_data,
1527 key_data_len,
1528 key_info, ver, &gd);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001529 }
1530
1531 wpa_sm_set_state(sm, WPA_GROUP_HANDSHAKE);
1532
1533 if (ret)
1534 goto failed;
1535
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001536 key_rsc = key->key_rsc;
1537 if (wpa_supplicant_rsc_relaxation(sm, key->key_rsc))
1538 key_rsc = null_rsc;
1539
1540 if (wpa_supplicant_install_gtk(sm, &gd, key_rsc) ||
1541 wpa_supplicant_send_2_of_2(sm, key, ver, key_info) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001542 goto failed;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001543 os_memset(&gd, 0, sizeof(gd));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001544
1545 if (rekey) {
1546 wpa_msg(sm->ctx->msg_ctx, MSG_INFO, "WPA: Group rekeying "
1547 "completed with " MACSTR " [GTK=%s]",
1548 MAC2STR(sm->bssid), wpa_cipher_txt(sm->group_cipher));
1549 wpa_sm_cancel_auth_timeout(sm);
1550 wpa_sm_set_state(sm, WPA_COMPLETED);
1551 } else {
1552 wpa_supplicant_key_neg_complete(sm, sm->bssid,
1553 key_info &
1554 WPA_KEY_INFO_SECURE);
1555 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001556
1557 wpa_sm_set_rekey_offload(sm);
1558
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001559 return;
1560
1561failed:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001562 os_memset(&gd, 0, sizeof(gd));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001563 wpa_sm_deauthenticate(sm, WLAN_REASON_UNSPECIFIED);
1564}
1565
1566
1567static int wpa_supplicant_verify_eapol_key_mic(struct wpa_sm *sm,
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001568 struct wpa_eapol_key_192 *key,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001569 u16 ver,
1570 const u8 *buf, size_t len)
1571{
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001572 u8 mic[WPA_EAPOL_KEY_MIC_MAX_LEN];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001573 int ok = 0;
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001574 size_t mic_len = wpa_mic_len(sm->key_mgmt);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001575
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001576 os_memcpy(mic, key->key_mic, mic_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001577 if (sm->tptk_set) {
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001578 os_memset(key->key_mic, 0, mic_len);
1579 wpa_eapol_key_mic(sm->tptk.kck, sm->tptk.kck_len, sm->key_mgmt,
1580 ver, buf, len, key->key_mic);
1581 if (os_memcmp_const(mic, key->key_mic, mic_len) != 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001582 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1583 "WPA: Invalid EAPOL-Key MIC "
1584 "when using TPTK - ignoring TPTK");
1585 } else {
1586 ok = 1;
1587 sm->tptk_set = 0;
1588 sm->ptk_set = 1;
1589 os_memcpy(&sm->ptk, &sm->tptk, sizeof(sm->ptk));
Dmitry Shmidt61593f02014-04-21 16:27:35 -07001590 os_memset(&sm->tptk, 0, sizeof(sm->tptk));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001591 }
1592 }
1593
1594 if (!ok && sm->ptk_set) {
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001595 os_memset(key->key_mic, 0, mic_len);
1596 wpa_eapol_key_mic(sm->ptk.kck, sm->ptk.kck_len, sm->key_mgmt,
1597 ver, buf, len, key->key_mic);
1598 if (os_memcmp_const(mic, key->key_mic, mic_len) != 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001599 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1600 "WPA: Invalid EAPOL-Key MIC - "
1601 "dropping packet");
1602 return -1;
1603 }
1604 ok = 1;
1605 }
1606
1607 if (!ok) {
1608 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1609 "WPA: Could not verify EAPOL-Key MIC - "
1610 "dropping packet");
1611 return -1;
1612 }
1613
1614 os_memcpy(sm->rx_replay_counter, key->replay_counter,
1615 WPA_REPLAY_COUNTER_LEN);
1616 sm->rx_replay_counter_set = 1;
1617 return 0;
1618}
1619
1620
1621/* Decrypt RSN EAPOL-Key key data (RC4 or AES-WRAP) */
1622static int wpa_supplicant_decrypt_key_data(struct wpa_sm *sm,
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001623 struct wpa_eapol_key *key, u16 ver,
1624 u8 *key_data, size_t *key_data_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001625{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001626 wpa_hexdump(MSG_DEBUG, "RSN: encrypted key data",
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001627 key_data, *key_data_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001628 if (!sm->ptk_set) {
1629 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1630 "WPA: PTK not available, cannot decrypt EAPOL-Key Key "
1631 "Data");
1632 return -1;
1633 }
1634
1635 /* Decrypt key data here so that this operation does not need
1636 * to be implemented separately for each message type. */
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001637 if (ver == WPA_KEY_INFO_TYPE_HMAC_MD5_RC4 && sm->ptk.kek_len == 16) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001638#ifdef CONFIG_NO_RC4
1639 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1640 "WPA: RC4 not supported in the build");
1641 return -1;
1642#else /* CONFIG_NO_RC4 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001643 u8 ek[32];
1644 os_memcpy(ek, key->key_iv, 16);
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001645 os_memcpy(ek + 16, sm->ptk.kek, sm->ptk.kek_len);
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001646 if (rc4_skip(ek, 32, 256, key_data, *key_data_len)) {
Dmitry Shmidt61593f02014-04-21 16:27:35 -07001647 os_memset(ek, 0, sizeof(ek));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001648 wpa_msg(sm->ctx->msg_ctx, MSG_ERROR,
1649 "WPA: RC4 failed");
1650 return -1;
1651 }
Dmitry Shmidt61593f02014-04-21 16:27:35 -07001652 os_memset(ek, 0, sizeof(ek));
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001653#endif /* CONFIG_NO_RC4 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001654 } else if (ver == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES ||
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001655 ver == WPA_KEY_INFO_TYPE_AES_128_CMAC ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001656 sm->key_mgmt == WPA_KEY_MGMT_OSEN ||
1657 wpa_key_mgmt_suite_b(sm->key_mgmt)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001658 u8 *buf;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001659 if (*key_data_len < 8 || *key_data_len % 8) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001660 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001661 "WPA: Unsupported AES-WRAP len %u",
1662 (unsigned int) *key_data_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001663 return -1;
1664 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001665 *key_data_len -= 8; /* AES-WRAP adds 8 bytes */
1666 buf = os_malloc(*key_data_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001667 if (buf == NULL) {
1668 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1669 "WPA: No memory for AES-UNWRAP buffer");
1670 return -1;
1671 }
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001672 if (aes_unwrap(sm->ptk.kek, sm->ptk.kek_len, *key_data_len / 8,
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001673 key_data, buf)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001674 os_free(buf);
1675 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1676 "WPA: AES unwrap failed - "
1677 "could not decrypt EAPOL-Key key data");
1678 return -1;
1679 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001680 os_memcpy(key_data, buf, *key_data_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001681 os_free(buf);
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001682 WPA_PUT_BE16(key->key_data_length, *key_data_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001683 } else {
1684 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1685 "WPA: Unsupported key_info type %d", ver);
1686 return -1;
1687 }
1688 wpa_hexdump_key(MSG_DEBUG, "WPA: decrypted EAPOL-Key key data",
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001689 key_data, *key_data_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001690 return 0;
1691}
1692
1693
1694/**
1695 * wpa_sm_aborted_cached - Notify WPA that PMKSA caching was aborted
1696 * @sm: Pointer to WPA state machine data from wpa_sm_init()
1697 */
1698void wpa_sm_aborted_cached(struct wpa_sm *sm)
1699{
1700 if (sm && sm->cur_pmksa) {
1701 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1702 "RSN: Cancelling PMKSA caching attempt");
1703 sm->cur_pmksa = NULL;
1704 }
1705}
1706
1707
1708static void wpa_eapol_key_dump(struct wpa_sm *sm,
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001709 const struct wpa_eapol_key *key,
1710 unsigned int key_data_len,
1711 const u8 *mic, unsigned int mic_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001712{
1713#ifndef CONFIG_NO_STDOUT_DEBUG
1714 u16 key_info = WPA_GET_BE16(key->key_info);
1715
1716 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, " EAPOL-Key type=%d", key->type);
1717 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1718 " key_info 0x%x (ver=%d keyidx=%d rsvd=%d %s%s%s%s%s%s%s%s)",
1719 key_info, key_info & WPA_KEY_INFO_TYPE_MASK,
1720 (key_info & WPA_KEY_INFO_KEY_INDEX_MASK) >>
1721 WPA_KEY_INFO_KEY_INDEX_SHIFT,
1722 (key_info & (BIT(13) | BIT(14) | BIT(15))) >> 13,
1723 key_info & WPA_KEY_INFO_KEY_TYPE ? "Pairwise" : "Group",
1724 key_info & WPA_KEY_INFO_INSTALL ? " Install" : "",
1725 key_info & WPA_KEY_INFO_ACK ? " Ack" : "",
1726 key_info & WPA_KEY_INFO_MIC ? " MIC" : "",
1727 key_info & WPA_KEY_INFO_SECURE ? " Secure" : "",
1728 key_info & WPA_KEY_INFO_ERROR ? " Error" : "",
1729 key_info & WPA_KEY_INFO_REQUEST ? " Request" : "",
1730 key_info & WPA_KEY_INFO_ENCR_KEY_DATA ? " Encr" : "");
1731 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1732 " key_length=%u key_data_length=%u",
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001733 WPA_GET_BE16(key->key_length), key_data_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001734 wpa_hexdump(MSG_DEBUG, " replay_counter",
1735 key->replay_counter, WPA_REPLAY_COUNTER_LEN);
1736 wpa_hexdump(MSG_DEBUG, " key_nonce", key->key_nonce, WPA_NONCE_LEN);
1737 wpa_hexdump(MSG_DEBUG, " key_iv", key->key_iv, 16);
1738 wpa_hexdump(MSG_DEBUG, " key_rsc", key->key_rsc, 8);
1739 wpa_hexdump(MSG_DEBUG, " key_id (reserved)", key->key_id, 8);
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001740 wpa_hexdump(MSG_DEBUG, " key_mic", mic, mic_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001741#endif /* CONFIG_NO_STDOUT_DEBUG */
1742}
1743
1744
1745/**
1746 * wpa_sm_rx_eapol - Process received WPA EAPOL frames
1747 * @sm: Pointer to WPA state machine data from wpa_sm_init()
1748 * @src_addr: Source MAC address of the EAPOL packet
1749 * @buf: Pointer to the beginning of the EAPOL data (EAPOL header)
1750 * @len: Length of the EAPOL frame
1751 * Returns: 1 = WPA EAPOL-Key processed, 0 = not a WPA EAPOL-Key, -1 failure
1752 *
1753 * This function is called for each received EAPOL frame. Other than EAPOL-Key
1754 * frames can be skipped if filtering is done elsewhere. wpa_sm_rx_eapol() is
1755 * only processing WPA and WPA2 EAPOL-Key frames.
1756 *
1757 * The received EAPOL-Key packets are validated and valid packets are replied
1758 * to. In addition, key material (PTK, GTK) is configured at the end of a
1759 * successful key handshake.
1760 */
1761int wpa_sm_rx_eapol(struct wpa_sm *sm, const u8 *src_addr,
1762 const u8 *buf, size_t len)
1763{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001764 size_t plen, data_len, key_data_len;
1765 const struct ieee802_1x_hdr *hdr;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001766 struct wpa_eapol_key *key;
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001767 struct wpa_eapol_key_192 *key192;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001768 u16 key_info, ver;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001769 u8 *tmp = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001770 int ret = -1;
1771 struct wpa_peerkey *peerkey = NULL;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001772 u8 *key_data;
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001773 size_t mic_len, keyhdrlen;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001774
1775#ifdef CONFIG_IEEE80211R
1776 sm->ft_completed = 0;
1777#endif /* CONFIG_IEEE80211R */
1778
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001779 mic_len = wpa_mic_len(sm->key_mgmt);
1780 keyhdrlen = mic_len == 24 ? sizeof(*key192) : sizeof(*key);
1781
1782 if (len < sizeof(*hdr) + keyhdrlen) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001783 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1784 "WPA: EAPOL frame too short to be a WPA "
1785 "EAPOL-Key (len %lu, expecting at least %lu)",
1786 (unsigned long) len,
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001787 (unsigned long) sizeof(*hdr) + keyhdrlen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001788 return 0;
1789 }
1790
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001791 hdr = (const struct ieee802_1x_hdr *) buf;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001792 plen = be_to_host16(hdr->length);
1793 data_len = plen + sizeof(*hdr);
1794 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1795 "IEEE 802.1X RX: version=%d type=%d length=%lu",
1796 hdr->version, hdr->type, (unsigned long) plen);
1797
1798 if (hdr->version < EAPOL_VERSION) {
1799 /* TODO: backwards compatibility */
1800 }
1801 if (hdr->type != IEEE802_1X_TYPE_EAPOL_KEY) {
1802 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1803 "WPA: EAPOL frame (type %u) discarded, "
1804 "not a Key frame", hdr->type);
1805 ret = 0;
1806 goto out;
1807 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001808 wpa_hexdump(MSG_MSGDUMP, "WPA: RX EAPOL-Key", buf, len);
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001809 if (plen > len - sizeof(*hdr) || plen < keyhdrlen) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001810 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1811 "WPA: EAPOL frame payload size %lu "
1812 "invalid (frame size %lu)",
1813 (unsigned long) plen, (unsigned long) len);
1814 ret = 0;
1815 goto out;
1816 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001817 if (data_len < len) {
1818 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1819 "WPA: ignoring %lu bytes after the IEEE 802.1X data",
1820 (unsigned long) len - data_len);
1821 }
1822
1823 /*
1824 * Make a copy of the frame since we need to modify the buffer during
1825 * MAC validation and Key Data decryption.
1826 */
1827 tmp = os_malloc(data_len);
1828 if (tmp == NULL)
1829 goto out;
1830 os_memcpy(tmp, buf, data_len);
1831 key = (struct wpa_eapol_key *) (tmp + sizeof(struct ieee802_1x_hdr));
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001832 key192 = (struct wpa_eapol_key_192 *)
1833 (tmp + sizeof(struct ieee802_1x_hdr));
1834 if (mic_len == 24)
1835 key_data = (u8 *) (key192 + 1);
1836 else
1837 key_data = (u8 *) (key + 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001838
1839 if (key->type != EAPOL_KEY_TYPE_WPA && key->type != EAPOL_KEY_TYPE_RSN)
1840 {
1841 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1842 "WPA: EAPOL-Key type (%d) unknown, discarded",
1843 key->type);
1844 ret = 0;
1845 goto out;
1846 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001847
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001848 if (mic_len == 24)
1849 key_data_len = WPA_GET_BE16(key192->key_data_length);
1850 else
1851 key_data_len = WPA_GET_BE16(key->key_data_length);
1852 wpa_eapol_key_dump(sm, key, key_data_len, key192->key_mic, mic_len);
1853
1854 if (key_data_len > plen - keyhdrlen) {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001855 wpa_msg(sm->ctx->msg_ctx, MSG_INFO, "WPA: Invalid EAPOL-Key "
1856 "frame - key_data overflow (%u > %u)",
1857 (unsigned int) key_data_len,
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001858 (unsigned int) (plen - keyhdrlen));
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001859 goto out;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001860 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001861
1862 eapol_sm_notify_lower_layer_success(sm->eapol, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001863 key_info = WPA_GET_BE16(key->key_info);
1864 ver = key_info & WPA_KEY_INFO_TYPE_MASK;
1865 if (ver != WPA_KEY_INFO_TYPE_HMAC_MD5_RC4 &&
1866#if defined(CONFIG_IEEE80211R) || defined(CONFIG_IEEE80211W)
1867 ver != WPA_KEY_INFO_TYPE_AES_128_CMAC &&
1868#endif /* CONFIG_IEEE80211R || CONFIG_IEEE80211W */
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001869 ver != WPA_KEY_INFO_TYPE_HMAC_SHA1_AES &&
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001870 !wpa_key_mgmt_suite_b(sm->key_mgmt) &&
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001871 sm->key_mgmt != WPA_KEY_MGMT_OSEN) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001872 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1873 "WPA: Unsupported EAPOL-Key descriptor version %d",
1874 ver);
1875 goto out;
1876 }
1877
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001878 if (sm->key_mgmt == WPA_KEY_MGMT_OSEN &&
1879 ver != WPA_KEY_INFO_TYPE_AKM_DEFINED) {
1880 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1881 "OSEN: Unsupported EAPOL-Key descriptor version %d",
1882 ver);
1883 goto out;
1884 }
1885
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001886 if (wpa_key_mgmt_suite_b(sm->key_mgmt) &&
1887 ver != WPA_KEY_INFO_TYPE_AKM_DEFINED) {
1888 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1889 "RSN: Unsupported EAPOL-Key descriptor version %d (expected AKM defined = 0)",
1890 ver);
1891 goto out;
1892 }
1893
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001894#ifdef CONFIG_IEEE80211R
1895 if (wpa_key_mgmt_ft(sm->key_mgmt)) {
1896 /* IEEE 802.11r uses a new key_info type (AES-128-CMAC). */
1897 if (ver != WPA_KEY_INFO_TYPE_AES_128_CMAC) {
1898 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1899 "FT: AP did not use AES-128-CMAC");
1900 goto out;
1901 }
1902 } else
1903#endif /* CONFIG_IEEE80211R */
1904#ifdef CONFIG_IEEE80211W
1905 if (wpa_key_mgmt_sha256(sm->key_mgmt)) {
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001906 if (ver != WPA_KEY_INFO_TYPE_AES_128_CMAC &&
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001907 sm->key_mgmt != WPA_KEY_MGMT_OSEN &&
1908 !wpa_key_mgmt_suite_b(sm->key_mgmt)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001909 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1910 "WPA: AP did not use the "
1911 "negotiated AES-128-CMAC");
1912 goto out;
1913 }
1914 } else
1915#endif /* CONFIG_IEEE80211W */
1916 if (sm->pairwise_cipher == WPA_CIPHER_CCMP &&
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001917 !wpa_key_mgmt_suite_b(sm->key_mgmt) &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001918 ver != WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
1919 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1920 "WPA: CCMP is used, but EAPOL-Key "
1921 "descriptor version (%d) is not 2", ver);
1922 if (sm->group_cipher != WPA_CIPHER_CCMP &&
1923 !(key_info & WPA_KEY_INFO_KEY_TYPE)) {
1924 /* Earlier versions of IEEE 802.11i did not explicitly
1925 * require version 2 descriptor for all EAPOL-Key
1926 * packets, so allow group keys to use version 1 if
1927 * CCMP is not used for them. */
1928 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1929 "WPA: Backwards compatibility: allow invalid "
1930 "version for non-CCMP group keys");
Jouni Malinen658fb4a2014-11-14 20:57:05 +02001931 } else if (ver == WPA_KEY_INFO_TYPE_AES_128_CMAC) {
1932 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1933 "WPA: Interoperability workaround: allow incorrect (should have been HMAC-SHA1), but stronger (is AES-128-CMAC), descriptor version to be used");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001934 } else
1935 goto out;
Dmitry Shmidt71757432014-06-02 13:50:35 -07001936 } else if (sm->pairwise_cipher == WPA_CIPHER_GCMP &&
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001937 !wpa_key_mgmt_suite_b(sm->key_mgmt) &&
Dmitry Shmidt71757432014-06-02 13:50:35 -07001938 ver != WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001939 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1940 "WPA: GCMP is used, but EAPOL-Key "
1941 "descriptor version (%d) is not 2", ver);
1942 goto out;
1943 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001944
1945#ifdef CONFIG_PEERKEY
1946 for (peerkey = sm->peerkey; peerkey; peerkey = peerkey->next) {
1947 if (os_memcmp(peerkey->addr, src_addr, ETH_ALEN) == 0)
1948 break;
1949 }
1950
1951 if (!(key_info & WPA_KEY_INFO_SMK_MESSAGE) && peerkey) {
1952 if (!peerkey->initiator && peerkey->replay_counter_set &&
1953 os_memcmp(key->replay_counter, peerkey->replay_counter,
1954 WPA_REPLAY_COUNTER_LEN) <= 0) {
1955 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1956 "RSN: EAPOL-Key Replay Counter did not "
1957 "increase (STK) - dropping packet");
1958 goto out;
1959 } else if (peerkey->initiator) {
1960 u8 _tmp[WPA_REPLAY_COUNTER_LEN];
1961 os_memcpy(_tmp, key->replay_counter,
1962 WPA_REPLAY_COUNTER_LEN);
1963 inc_byte_array(_tmp, WPA_REPLAY_COUNTER_LEN);
1964 if (os_memcmp(_tmp, peerkey->replay_counter,
1965 WPA_REPLAY_COUNTER_LEN) != 0) {
1966 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1967 "RSN: EAPOL-Key Replay "
1968 "Counter did not match (STK) - "
1969 "dropping packet");
1970 goto out;
1971 }
1972 }
1973 }
1974
1975 if (peerkey && peerkey->initiator && (key_info & WPA_KEY_INFO_ACK)) {
1976 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1977 "RSN: Ack bit in key_info from STK peer");
1978 goto out;
1979 }
1980#endif /* CONFIG_PEERKEY */
1981
1982 if (!peerkey && sm->rx_replay_counter_set &&
1983 os_memcmp(key->replay_counter, sm->rx_replay_counter,
1984 WPA_REPLAY_COUNTER_LEN) <= 0) {
1985 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1986 "WPA: EAPOL-Key Replay Counter did not increase - "
1987 "dropping packet");
1988 goto out;
1989 }
1990
1991 if (!(key_info & (WPA_KEY_INFO_ACK | WPA_KEY_INFO_SMK_MESSAGE))
1992#ifdef CONFIG_PEERKEY
1993 && (peerkey == NULL || !peerkey->initiator)
1994#endif /* CONFIG_PEERKEY */
1995 ) {
1996 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1997 "WPA: No Ack bit in key_info");
1998 goto out;
1999 }
2000
2001 if (key_info & WPA_KEY_INFO_REQUEST) {
2002 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2003 "WPA: EAPOL-Key with Request bit - dropped");
2004 goto out;
2005 }
2006
2007 if ((key_info & WPA_KEY_INFO_MIC) && !peerkey &&
Dmitry Shmidt807291d2015-01-27 13:40:23 -08002008 wpa_supplicant_verify_eapol_key_mic(sm, key192, ver, tmp, data_len))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002009 goto out;
2010
2011#ifdef CONFIG_PEERKEY
2012 if ((key_info & WPA_KEY_INFO_MIC) && peerkey &&
Dmitry Shmidt807291d2015-01-27 13:40:23 -08002013 peerkey_verify_eapol_key_mic(sm, peerkey, key192, ver, tmp,
2014 data_len))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002015 goto out;
2016#endif /* CONFIG_PEERKEY */
2017
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002018 if ((sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN) &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002019 (key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002020 if (wpa_supplicant_decrypt_key_data(sm, key, ver, key_data,
2021 &key_data_len))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002022 goto out;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002023 }
2024
2025 if (key_info & WPA_KEY_INFO_KEY_TYPE) {
2026 if (key_info & WPA_KEY_INFO_KEY_INDEX_MASK) {
2027 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
2028 "WPA: Ignored EAPOL-Key (Pairwise) with "
2029 "non-zero key index");
2030 goto out;
2031 }
2032 if (peerkey) {
2033 /* PeerKey 4-Way Handshake */
Dmitry Shmidtc2817022014-07-02 10:32:10 -07002034 peerkey_rx_eapol_4way(sm, peerkey, key, key_info, ver,
2035 key_data, key_data_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002036 } else if (key_info & WPA_KEY_INFO_MIC) {
2037 /* 3/4 4-Way Handshake */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002038 wpa_supplicant_process_3_of_4(sm, key, ver, key_data,
2039 key_data_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002040 } else {
2041 /* 1/4 4-Way Handshake */
2042 wpa_supplicant_process_1_of_4(sm, src_addr, key,
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002043 ver, key_data,
2044 key_data_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002045 }
2046 } else if (key_info & WPA_KEY_INFO_SMK_MESSAGE) {
2047 /* PeerKey SMK Handshake */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002048 peerkey_rx_eapol_smk(sm, src_addr, key, key_data_len, key_info,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002049 ver);
2050 } else {
2051 if (key_info & WPA_KEY_INFO_MIC) {
2052 /* 1/2 Group Key Handshake */
2053 wpa_supplicant_process_1_of_2(sm, src_addr, key,
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002054 key_data, key_data_len,
2055 ver);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002056 } else {
2057 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
2058 "WPA: EAPOL-Key (Group) without Mic bit - "
2059 "dropped");
2060 }
2061 }
2062
2063 ret = 1;
2064
2065out:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002066 bin_clear_free(tmp, data_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002067 return ret;
2068}
2069
2070
2071#ifdef CONFIG_CTRL_IFACE
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002072static u32 wpa_key_mgmt_suite(struct wpa_sm *sm)
2073{
2074 switch (sm->key_mgmt) {
2075 case WPA_KEY_MGMT_IEEE8021X:
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002076 return ((sm->proto == WPA_PROTO_RSN ||
2077 sm->proto == WPA_PROTO_OSEN) ?
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002078 RSN_AUTH_KEY_MGMT_UNSPEC_802_1X :
2079 WPA_AUTH_KEY_MGMT_UNSPEC_802_1X);
2080 case WPA_KEY_MGMT_PSK:
2081 return (sm->proto == WPA_PROTO_RSN ?
2082 RSN_AUTH_KEY_MGMT_PSK_OVER_802_1X :
2083 WPA_AUTH_KEY_MGMT_PSK_OVER_802_1X);
2084#ifdef CONFIG_IEEE80211R
2085 case WPA_KEY_MGMT_FT_IEEE8021X:
2086 return RSN_AUTH_KEY_MGMT_FT_802_1X;
2087 case WPA_KEY_MGMT_FT_PSK:
2088 return RSN_AUTH_KEY_MGMT_FT_PSK;
2089#endif /* CONFIG_IEEE80211R */
2090#ifdef CONFIG_IEEE80211W
2091 case WPA_KEY_MGMT_IEEE8021X_SHA256:
2092 return RSN_AUTH_KEY_MGMT_802_1X_SHA256;
2093 case WPA_KEY_MGMT_PSK_SHA256:
2094 return RSN_AUTH_KEY_MGMT_PSK_SHA256;
2095#endif /* CONFIG_IEEE80211W */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002096 case WPA_KEY_MGMT_CCKM:
2097 return (sm->proto == WPA_PROTO_RSN ?
2098 RSN_AUTH_KEY_MGMT_CCKM:
2099 WPA_AUTH_KEY_MGMT_CCKM);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002100 case WPA_KEY_MGMT_WPA_NONE:
2101 return WPA_AUTH_KEY_MGMT_NONE;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002102 case WPA_KEY_MGMT_IEEE8021X_SUITE_B:
2103 return RSN_AUTH_KEY_MGMT_802_1X_SUITE_B;
Dmitry Shmidt807291d2015-01-27 13:40:23 -08002104 case WPA_KEY_MGMT_IEEE8021X_SUITE_B_192:
2105 return RSN_AUTH_KEY_MGMT_802_1X_SUITE_B_192;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002106 default:
2107 return 0;
2108 }
2109}
2110
2111
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002112#define RSN_SUITE "%02x-%02x-%02x-%d"
2113#define RSN_SUITE_ARG(s) \
2114((s) >> 24) & 0xff, ((s) >> 16) & 0xff, ((s) >> 8) & 0xff, (s) & 0xff
2115
2116/**
2117 * wpa_sm_get_mib - Dump text list of MIB entries
2118 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2119 * @buf: Buffer for the list
2120 * @buflen: Length of the buffer
2121 * Returns: Number of bytes written to buffer
2122 *
2123 * This function is used fetch dot11 MIB variables.
2124 */
2125int wpa_sm_get_mib(struct wpa_sm *sm, char *buf, size_t buflen)
2126{
2127 char pmkid_txt[PMKID_LEN * 2 + 1];
2128 int rsna, ret;
2129 size_t len;
2130
2131 if (sm->cur_pmksa) {
2132 wpa_snprintf_hex(pmkid_txt, sizeof(pmkid_txt),
2133 sm->cur_pmksa->pmkid, PMKID_LEN);
2134 } else
2135 pmkid_txt[0] = '\0';
2136
2137 if ((wpa_key_mgmt_wpa_psk(sm->key_mgmt) ||
2138 wpa_key_mgmt_wpa_ieee8021x(sm->key_mgmt)) &&
2139 sm->proto == WPA_PROTO_RSN)
2140 rsna = 1;
2141 else
2142 rsna = 0;
2143
2144 ret = os_snprintf(buf, buflen,
2145 "dot11RSNAOptionImplemented=TRUE\n"
2146 "dot11RSNAPreauthenticationImplemented=TRUE\n"
2147 "dot11RSNAEnabled=%s\n"
2148 "dot11RSNAPreauthenticationEnabled=%s\n"
2149 "dot11RSNAConfigVersion=%d\n"
2150 "dot11RSNAConfigPairwiseKeysSupported=5\n"
2151 "dot11RSNAConfigGroupCipherSize=%d\n"
2152 "dot11RSNAConfigPMKLifetime=%d\n"
2153 "dot11RSNAConfigPMKReauthThreshold=%d\n"
2154 "dot11RSNAConfigNumberOfPTKSAReplayCounters=1\n"
2155 "dot11RSNAConfigSATimeout=%d\n",
2156 rsna ? "TRUE" : "FALSE",
2157 rsna ? "TRUE" : "FALSE",
2158 RSN_VERSION,
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07002159 wpa_cipher_key_len(sm->group_cipher) * 8,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002160 sm->dot11RSNAConfigPMKLifetime,
2161 sm->dot11RSNAConfigPMKReauthThreshold,
2162 sm->dot11RSNAConfigSATimeout);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002163 if (os_snprintf_error(buflen, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002164 return 0;
2165 len = ret;
2166
2167 ret = os_snprintf(
2168 buf + len, buflen - len,
2169 "dot11RSNAAuthenticationSuiteSelected=" RSN_SUITE "\n"
2170 "dot11RSNAPairwiseCipherSelected=" RSN_SUITE "\n"
2171 "dot11RSNAGroupCipherSelected=" RSN_SUITE "\n"
2172 "dot11RSNAPMKIDUsed=%s\n"
2173 "dot11RSNAAuthenticationSuiteRequested=" RSN_SUITE "\n"
2174 "dot11RSNAPairwiseCipherRequested=" RSN_SUITE "\n"
2175 "dot11RSNAGroupCipherRequested=" RSN_SUITE "\n"
2176 "dot11RSNAConfigNumberOfGTKSAReplayCounters=0\n"
2177 "dot11RSNA4WayHandshakeFailures=%u\n",
2178 RSN_SUITE_ARG(wpa_key_mgmt_suite(sm)),
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07002179 RSN_SUITE_ARG(wpa_cipher_to_suite(sm->proto,
2180 sm->pairwise_cipher)),
2181 RSN_SUITE_ARG(wpa_cipher_to_suite(sm->proto,
2182 sm->group_cipher)),
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002183 pmkid_txt,
2184 RSN_SUITE_ARG(wpa_key_mgmt_suite(sm)),
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07002185 RSN_SUITE_ARG(wpa_cipher_to_suite(sm->proto,
2186 sm->pairwise_cipher)),
2187 RSN_SUITE_ARG(wpa_cipher_to_suite(sm->proto,
2188 sm->group_cipher)),
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002189 sm->dot11RSNA4WayHandshakeFailures);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002190 if (!os_snprintf_error(buflen - len, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002191 len += ret;
2192
2193 return (int) len;
2194}
2195#endif /* CONFIG_CTRL_IFACE */
2196
2197
2198static void wpa_sm_pmksa_free_cb(struct rsn_pmksa_cache_entry *entry,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002199 void *ctx, enum pmksa_free_reason reason)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002200{
2201 struct wpa_sm *sm = ctx;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002202 int deauth = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002203
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002204 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "RSN: PMKSA cache entry free_cb: "
2205 MACSTR " reason=%d", MAC2STR(entry->aa), reason);
2206
2207 if (sm->cur_pmksa == entry) {
2208 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2209 "RSN: %s current PMKSA entry",
2210 reason == PMKSA_REPLACE ? "replaced" : "removed");
2211 pmksa_cache_clear_current(sm);
2212
2213 /*
2214 * If an entry is simply being replaced, there's no need to
2215 * deauthenticate because it will be immediately re-added.
2216 * This happens when EAP authentication is completed again
2217 * (reauth or failed PMKSA caching attempt).
2218 */
2219 if (reason != PMKSA_REPLACE)
2220 deauth = 1;
2221 }
2222
2223 if (reason == PMKSA_EXPIRE &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002224 (sm->pmk_len == entry->pmk_len &&
2225 os_memcmp(sm->pmk, entry->pmk, sm->pmk_len) == 0)) {
2226 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002227 "RSN: deauthenticating due to expired PMK");
2228 pmksa_cache_clear_current(sm);
2229 deauth = 1;
2230 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002231
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002232 if (deauth) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002233 os_memset(sm->pmk, 0, sizeof(sm->pmk));
2234 wpa_sm_deauthenticate(sm, WLAN_REASON_UNSPECIFIED);
2235 }
2236}
2237
2238
2239/**
2240 * wpa_sm_init - Initialize WPA state machine
2241 * @ctx: Context pointer for callbacks; this needs to be an allocated buffer
2242 * Returns: Pointer to the allocated WPA state machine data
2243 *
2244 * This function is used to allocate a new WPA state machine and the returned
2245 * value is passed to all WPA state machine calls.
2246 */
2247struct wpa_sm * wpa_sm_init(struct wpa_sm_ctx *ctx)
2248{
2249 struct wpa_sm *sm;
2250
2251 sm = os_zalloc(sizeof(*sm));
2252 if (sm == NULL)
2253 return NULL;
2254 dl_list_init(&sm->pmksa_candidates);
2255 sm->renew_snonce = 1;
2256 sm->ctx = ctx;
2257
2258 sm->dot11RSNAConfigPMKLifetime = 43200;
2259 sm->dot11RSNAConfigPMKReauthThreshold = 70;
2260 sm->dot11RSNAConfigSATimeout = 60;
2261
2262 sm->pmksa = pmksa_cache_init(wpa_sm_pmksa_free_cb, sm, sm);
2263 if (sm->pmksa == NULL) {
2264 wpa_msg(sm->ctx->msg_ctx, MSG_ERROR,
2265 "RSN: PMKSA cache initialization failed");
2266 os_free(sm);
2267 return NULL;
2268 }
2269
2270 return sm;
2271}
2272
2273
2274/**
2275 * wpa_sm_deinit - Deinitialize WPA state machine
2276 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2277 */
2278void wpa_sm_deinit(struct wpa_sm *sm)
2279{
2280 if (sm == NULL)
2281 return;
2282 pmksa_cache_deinit(sm->pmksa);
2283 eloop_cancel_timeout(wpa_sm_start_preauth, sm, NULL);
2284 eloop_cancel_timeout(wpa_sm_rekey_ptk, sm, NULL);
2285 os_free(sm->assoc_wpa_ie);
2286 os_free(sm->ap_wpa_ie);
2287 os_free(sm->ap_rsn_ie);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002288 wpa_sm_drop_sa(sm);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002289 os_free(sm->ctx);
2290 peerkey_deinit(sm);
2291#ifdef CONFIG_IEEE80211R
2292 os_free(sm->assoc_resp_ies);
2293#endif /* CONFIG_IEEE80211R */
2294 os_free(sm);
2295}
2296
2297
2298/**
2299 * wpa_sm_notify_assoc - Notify WPA state machine about association
2300 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2301 * @bssid: The BSSID of the new association
2302 *
2303 * This function is called to let WPA state machine know that the connection
2304 * was established.
2305 */
2306void wpa_sm_notify_assoc(struct wpa_sm *sm, const u8 *bssid)
2307{
2308 int clear_ptk = 1;
2309
2310 if (sm == NULL)
2311 return;
2312
2313 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2314 "WPA: Association event - clear replay counter");
2315 os_memcpy(sm->bssid, bssid, ETH_ALEN);
2316 os_memset(sm->rx_replay_counter, 0, WPA_REPLAY_COUNTER_LEN);
2317 sm->rx_replay_counter_set = 0;
2318 sm->renew_snonce = 1;
2319 if (os_memcmp(sm->preauth_bssid, bssid, ETH_ALEN) == 0)
2320 rsn_preauth_deinit(sm);
2321
2322#ifdef CONFIG_IEEE80211R
2323 if (wpa_ft_is_completed(sm)) {
2324 /*
2325 * Clear portValid to kick EAPOL state machine to re-enter
2326 * AUTHENTICATED state to get the EAPOL port Authorized.
2327 */
2328 eapol_sm_notify_portValid(sm->eapol, FALSE);
2329 wpa_supplicant_key_neg_complete(sm, sm->bssid, 1);
2330
2331 /* Prepare for the next transition */
2332 wpa_ft_prepare_auth_request(sm, NULL);
2333
2334 clear_ptk = 0;
2335 }
2336#endif /* CONFIG_IEEE80211R */
2337
2338 if (clear_ptk) {
2339 /*
2340 * IEEE 802.11, 8.4.10: Delete PTK SA on (re)association if
2341 * this is not part of a Fast BSS Transition.
2342 */
2343 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Clear old PTK");
2344 sm->ptk_set = 0;
Dmitry Shmidt61593f02014-04-21 16:27:35 -07002345 os_memset(&sm->ptk, 0, sizeof(sm->ptk));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002346 sm->tptk_set = 0;
Dmitry Shmidt61593f02014-04-21 16:27:35 -07002347 os_memset(&sm->tptk, 0, sizeof(sm->tptk));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002348 }
2349
2350#ifdef CONFIG_TDLS
2351 wpa_tdls_assoc(sm);
2352#endif /* CONFIG_TDLS */
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002353
2354#ifdef CONFIG_P2P
2355 os_memset(sm->p2p_ip_addr, 0, sizeof(sm->p2p_ip_addr));
2356#endif /* CONFIG_P2P */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002357}
2358
2359
2360/**
2361 * wpa_sm_notify_disassoc - Notify WPA state machine about disassociation
2362 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2363 *
2364 * This function is called to let WPA state machine know that the connection
2365 * was lost. This will abort any existing pre-authentication session.
2366 */
2367void wpa_sm_notify_disassoc(struct wpa_sm *sm)
2368{
Dmitry Shmidt4dd28dc2015-03-10 11:21:43 -07002369 eloop_cancel_timeout(wpa_sm_start_preauth, sm, NULL);
2370 eloop_cancel_timeout(wpa_sm_rekey_ptk, sm, NULL);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002371 peerkey_deinit(sm);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002372 rsn_preauth_deinit(sm);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002373 pmksa_cache_clear_current(sm);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002374 if (wpa_sm_get_state(sm) == WPA_4WAY_HANDSHAKE)
2375 sm->dot11RSNA4WayHandshakeFailures++;
2376#ifdef CONFIG_TDLS
2377 wpa_tdls_disassoc(sm);
2378#endif /* CONFIG_TDLS */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002379
2380 /* Keys are not needed in the WPA state machine anymore */
2381 wpa_sm_drop_sa(sm);
Dmitry Shmidt4dd28dc2015-03-10 11:21:43 -07002382
2383 sm->msg_3_of_4_ok = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002384}
2385
2386
2387/**
2388 * wpa_sm_set_pmk - Set PMK
2389 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2390 * @pmk: The new PMK
2391 * @pmk_len: The length of the new PMK in bytes
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002392 * @bssid: AA to add into PMKSA cache or %NULL to not cache the PMK
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002393 *
2394 * Configure the PMK for WPA state machine.
2395 */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002396void wpa_sm_set_pmk(struct wpa_sm *sm, const u8 *pmk, size_t pmk_len,
2397 const u8 *bssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002398{
2399 if (sm == NULL)
2400 return;
2401
2402 sm->pmk_len = pmk_len;
2403 os_memcpy(sm->pmk, pmk, pmk_len);
2404
2405#ifdef CONFIG_IEEE80211R
2406 /* Set XXKey to be PSK for FT key derivation */
2407 sm->xxkey_len = pmk_len;
2408 os_memcpy(sm->xxkey, pmk, pmk_len);
2409#endif /* CONFIG_IEEE80211R */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002410
2411 if (bssid) {
2412 pmksa_cache_add(sm->pmksa, pmk, pmk_len, NULL, 0,
2413 bssid, sm->own_addr,
2414 sm->network_ctx, sm->key_mgmt);
2415 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002416}
2417
2418
2419/**
2420 * wpa_sm_set_pmk_from_pmksa - Set PMK based on the current PMKSA
2421 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2422 *
2423 * Take the PMK from the current PMKSA into use. If no PMKSA is active, the PMK
2424 * will be cleared.
2425 */
2426void wpa_sm_set_pmk_from_pmksa(struct wpa_sm *sm)
2427{
2428 if (sm == NULL)
2429 return;
2430
2431 if (sm->cur_pmksa) {
2432 sm->pmk_len = sm->cur_pmksa->pmk_len;
2433 os_memcpy(sm->pmk, sm->cur_pmksa->pmk, sm->pmk_len);
2434 } else {
2435 sm->pmk_len = PMK_LEN;
2436 os_memset(sm->pmk, 0, PMK_LEN);
2437 }
2438}
2439
2440
2441/**
2442 * wpa_sm_set_fast_reauth - Set fast reauthentication (EAP) enabled/disabled
2443 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2444 * @fast_reauth: Whether fast reauthentication (EAP) is allowed
2445 */
2446void wpa_sm_set_fast_reauth(struct wpa_sm *sm, int fast_reauth)
2447{
2448 if (sm)
2449 sm->fast_reauth = fast_reauth;
2450}
2451
2452
2453/**
2454 * wpa_sm_set_scard_ctx - Set context pointer for smartcard callbacks
2455 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2456 * @scard_ctx: Context pointer for smartcard related callback functions
2457 */
2458void wpa_sm_set_scard_ctx(struct wpa_sm *sm, void *scard_ctx)
2459{
2460 if (sm == NULL)
2461 return;
2462 sm->scard_ctx = scard_ctx;
2463 if (sm->preauth_eapol)
2464 eapol_sm_register_scard_ctx(sm->preauth_eapol, scard_ctx);
2465}
2466
2467
2468/**
2469 * wpa_sm_set_config - Notification of current configration change
2470 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2471 * @config: Pointer to current network configuration
2472 *
2473 * Notify WPA state machine that configuration has changed. config will be
2474 * stored as a backpointer to network configuration. This can be %NULL to clear
2475 * the stored pointed.
2476 */
2477void wpa_sm_set_config(struct wpa_sm *sm, struct rsn_supp_config *config)
2478{
2479 if (!sm)
2480 return;
2481
2482 if (config) {
2483 sm->network_ctx = config->network_ctx;
2484 sm->peerkey_enabled = config->peerkey_enabled;
2485 sm->allowed_pairwise_cipher = config->allowed_pairwise_cipher;
2486 sm->proactive_key_caching = config->proactive_key_caching;
2487 sm->eap_workaround = config->eap_workaround;
2488 sm->eap_conf_ctx = config->eap_conf_ctx;
2489 if (config->ssid) {
2490 os_memcpy(sm->ssid, config->ssid, config->ssid_len);
2491 sm->ssid_len = config->ssid_len;
2492 } else
2493 sm->ssid_len = 0;
2494 sm->wpa_ptk_rekey = config->wpa_ptk_rekey;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002495 sm->p2p = config->p2p;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002496 sm->wpa_rsc_relaxation = config->wpa_rsc_relaxation;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002497 } else {
2498 sm->network_ctx = NULL;
2499 sm->peerkey_enabled = 0;
2500 sm->allowed_pairwise_cipher = 0;
2501 sm->proactive_key_caching = 0;
2502 sm->eap_workaround = 0;
2503 sm->eap_conf_ctx = NULL;
2504 sm->ssid_len = 0;
2505 sm->wpa_ptk_rekey = 0;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002506 sm->p2p = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002507 sm->wpa_rsc_relaxation = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002508 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002509}
2510
2511
2512/**
2513 * wpa_sm_set_own_addr - Set own MAC address
2514 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2515 * @addr: Own MAC address
2516 */
2517void wpa_sm_set_own_addr(struct wpa_sm *sm, const u8 *addr)
2518{
2519 if (sm)
2520 os_memcpy(sm->own_addr, addr, ETH_ALEN);
2521}
2522
2523
2524/**
2525 * wpa_sm_set_ifname - Set network interface name
2526 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2527 * @ifname: Interface name
2528 * @bridge_ifname: Optional bridge interface name (for pre-auth)
2529 */
2530void wpa_sm_set_ifname(struct wpa_sm *sm, const char *ifname,
2531 const char *bridge_ifname)
2532{
2533 if (sm) {
2534 sm->ifname = ifname;
2535 sm->bridge_ifname = bridge_ifname;
2536 }
2537}
2538
2539
2540/**
2541 * wpa_sm_set_eapol - Set EAPOL state machine pointer
2542 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2543 * @eapol: Pointer to EAPOL state machine allocated with eapol_sm_init()
2544 */
2545void wpa_sm_set_eapol(struct wpa_sm *sm, struct eapol_sm *eapol)
2546{
2547 if (sm)
2548 sm->eapol = eapol;
2549}
2550
2551
2552/**
2553 * wpa_sm_set_param - Set WPA state machine parameters
2554 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2555 * @param: Parameter field
2556 * @value: Parameter value
2557 * Returns: 0 on success, -1 on failure
2558 */
2559int wpa_sm_set_param(struct wpa_sm *sm, enum wpa_sm_conf_params param,
2560 unsigned int value)
2561{
2562 int ret = 0;
2563
2564 if (sm == NULL)
2565 return -1;
2566
2567 switch (param) {
2568 case RSNA_PMK_LIFETIME:
2569 if (value > 0)
2570 sm->dot11RSNAConfigPMKLifetime = value;
2571 else
2572 ret = -1;
2573 break;
2574 case RSNA_PMK_REAUTH_THRESHOLD:
2575 if (value > 0 && value <= 100)
2576 sm->dot11RSNAConfigPMKReauthThreshold = value;
2577 else
2578 ret = -1;
2579 break;
2580 case RSNA_SA_TIMEOUT:
2581 if (value > 0)
2582 sm->dot11RSNAConfigSATimeout = value;
2583 else
2584 ret = -1;
2585 break;
2586 case WPA_PARAM_PROTO:
2587 sm->proto = value;
2588 break;
2589 case WPA_PARAM_PAIRWISE:
2590 sm->pairwise_cipher = value;
2591 break;
2592 case WPA_PARAM_GROUP:
2593 sm->group_cipher = value;
2594 break;
2595 case WPA_PARAM_KEY_MGMT:
2596 sm->key_mgmt = value;
2597 break;
2598#ifdef CONFIG_IEEE80211W
2599 case WPA_PARAM_MGMT_GROUP:
2600 sm->mgmt_group_cipher = value;
2601 break;
2602#endif /* CONFIG_IEEE80211W */
2603 case WPA_PARAM_RSN_ENABLED:
2604 sm->rsn_enabled = value;
2605 break;
2606 case WPA_PARAM_MFP:
2607 sm->mfp = value;
2608 break;
2609 default:
2610 break;
2611 }
2612
2613 return ret;
2614}
2615
2616
2617/**
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002618 * wpa_sm_get_status - Get WPA state machine
2619 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2620 * @buf: Buffer for status information
2621 * @buflen: Maximum buffer length
2622 * @verbose: Whether to include verbose status information
2623 * Returns: Number of bytes written to buf.
2624 *
2625 * Query WPA state machine for status information. This function fills in
2626 * a text area with current status information. If the buffer (buf) is not
2627 * large enough, status information will be truncated to fit the buffer.
2628 */
2629int wpa_sm_get_status(struct wpa_sm *sm, char *buf, size_t buflen,
2630 int verbose)
2631{
2632 char *pos = buf, *end = buf + buflen;
2633 int ret;
2634
2635 ret = os_snprintf(pos, end - pos,
2636 "pairwise_cipher=%s\n"
2637 "group_cipher=%s\n"
2638 "key_mgmt=%s\n",
2639 wpa_cipher_txt(sm->pairwise_cipher),
2640 wpa_cipher_txt(sm->group_cipher),
2641 wpa_key_mgmt_txt(sm->key_mgmt, sm->proto));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002642 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002643 return pos - buf;
2644 pos += ret;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002645
2646 if (sm->mfp != NO_MGMT_FRAME_PROTECTION && sm->ap_rsn_ie) {
2647 struct wpa_ie_data rsn;
2648 if (wpa_parse_wpa_ie_rsn(sm->ap_rsn_ie, sm->ap_rsn_ie_len, &rsn)
2649 >= 0 &&
2650 rsn.capabilities & (WPA_CAPABILITY_MFPR |
2651 WPA_CAPABILITY_MFPC)) {
2652 ret = os_snprintf(pos, end - pos, "pmf=%d\n",
2653 (rsn.capabilities &
2654 WPA_CAPABILITY_MFPR) ? 2 : 1);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002655 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002656 return pos - buf;
2657 pos += ret;
2658 }
2659 }
2660
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002661 return pos - buf;
2662}
2663
2664
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07002665int wpa_sm_pmf_enabled(struct wpa_sm *sm)
2666{
2667 struct wpa_ie_data rsn;
2668
2669 if (sm->mfp == NO_MGMT_FRAME_PROTECTION || !sm->ap_rsn_ie)
2670 return 0;
2671
2672 if (wpa_parse_wpa_ie_rsn(sm->ap_rsn_ie, sm->ap_rsn_ie_len, &rsn) >= 0 &&
2673 rsn.capabilities & (WPA_CAPABILITY_MFPR | WPA_CAPABILITY_MFPC))
2674 return 1;
2675
2676 return 0;
2677}
2678
2679
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002680/**
2681 * wpa_sm_set_assoc_wpa_ie_default - Generate own WPA/RSN IE from configuration
2682 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2683 * @wpa_ie: Pointer to buffer for WPA/RSN IE
2684 * @wpa_ie_len: Pointer to the length of the wpa_ie buffer
2685 * Returns: 0 on success, -1 on failure
2686 */
2687int wpa_sm_set_assoc_wpa_ie_default(struct wpa_sm *sm, u8 *wpa_ie,
2688 size_t *wpa_ie_len)
2689{
2690 int res;
2691
2692 if (sm == NULL)
2693 return -1;
2694
2695 res = wpa_gen_wpa_ie(sm, wpa_ie, *wpa_ie_len);
2696 if (res < 0)
2697 return -1;
2698 *wpa_ie_len = res;
2699
2700 wpa_hexdump(MSG_DEBUG, "WPA: Set own WPA IE default",
2701 wpa_ie, *wpa_ie_len);
2702
2703 if (sm->assoc_wpa_ie == NULL) {
2704 /*
2705 * Make a copy of the WPA/RSN IE so that 4-Way Handshake gets
2706 * the correct version of the IE even if PMKSA caching is
2707 * aborted (which would remove PMKID from IE generation).
2708 */
2709 sm->assoc_wpa_ie = os_malloc(*wpa_ie_len);
2710 if (sm->assoc_wpa_ie == NULL)
2711 return -1;
2712
2713 os_memcpy(sm->assoc_wpa_ie, wpa_ie, *wpa_ie_len);
2714 sm->assoc_wpa_ie_len = *wpa_ie_len;
2715 }
2716
2717 return 0;
2718}
2719
2720
2721/**
2722 * wpa_sm_set_assoc_wpa_ie - Set own WPA/RSN IE from (Re)AssocReq
2723 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2724 * @ie: Pointer to IE data (starting from id)
2725 * @len: IE length
2726 * Returns: 0 on success, -1 on failure
2727 *
2728 * Inform WPA state machine about the WPA/RSN IE used in (Re)Association
2729 * Request frame. The IE will be used to override the default value generated
2730 * with wpa_sm_set_assoc_wpa_ie_default().
2731 */
2732int wpa_sm_set_assoc_wpa_ie(struct wpa_sm *sm, const u8 *ie, size_t len)
2733{
2734 if (sm == NULL)
2735 return -1;
2736
2737 os_free(sm->assoc_wpa_ie);
2738 if (ie == NULL || len == 0) {
2739 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2740 "WPA: clearing own WPA/RSN IE");
2741 sm->assoc_wpa_ie = NULL;
2742 sm->assoc_wpa_ie_len = 0;
2743 } else {
2744 wpa_hexdump(MSG_DEBUG, "WPA: set own WPA/RSN IE", ie, len);
2745 sm->assoc_wpa_ie = os_malloc(len);
2746 if (sm->assoc_wpa_ie == NULL)
2747 return -1;
2748
2749 os_memcpy(sm->assoc_wpa_ie, ie, len);
2750 sm->assoc_wpa_ie_len = len;
2751 }
2752
2753 return 0;
2754}
2755
2756
2757/**
2758 * wpa_sm_set_ap_wpa_ie - Set AP WPA IE from Beacon/ProbeResp
2759 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2760 * @ie: Pointer to IE data (starting from id)
2761 * @len: IE length
2762 * Returns: 0 on success, -1 on failure
2763 *
2764 * Inform WPA state machine about the WPA IE used in Beacon / Probe Response
2765 * frame.
2766 */
2767int wpa_sm_set_ap_wpa_ie(struct wpa_sm *sm, const u8 *ie, size_t len)
2768{
2769 if (sm == NULL)
2770 return -1;
2771
2772 os_free(sm->ap_wpa_ie);
2773 if (ie == NULL || len == 0) {
2774 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2775 "WPA: clearing AP WPA IE");
2776 sm->ap_wpa_ie = NULL;
2777 sm->ap_wpa_ie_len = 0;
2778 } else {
2779 wpa_hexdump(MSG_DEBUG, "WPA: set AP WPA IE", ie, len);
2780 sm->ap_wpa_ie = os_malloc(len);
2781 if (sm->ap_wpa_ie == NULL)
2782 return -1;
2783
2784 os_memcpy(sm->ap_wpa_ie, ie, len);
2785 sm->ap_wpa_ie_len = len;
2786 }
2787
2788 return 0;
2789}
2790
2791
2792/**
2793 * wpa_sm_set_ap_rsn_ie - Set AP RSN IE from Beacon/ProbeResp
2794 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2795 * @ie: Pointer to IE data (starting from id)
2796 * @len: IE length
2797 * Returns: 0 on success, -1 on failure
2798 *
2799 * Inform WPA state machine about the RSN IE used in Beacon / Probe Response
2800 * frame.
2801 */
2802int wpa_sm_set_ap_rsn_ie(struct wpa_sm *sm, const u8 *ie, size_t len)
2803{
2804 if (sm == NULL)
2805 return -1;
2806
2807 os_free(sm->ap_rsn_ie);
2808 if (ie == NULL || len == 0) {
2809 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2810 "WPA: clearing AP RSN IE");
2811 sm->ap_rsn_ie = NULL;
2812 sm->ap_rsn_ie_len = 0;
2813 } else {
2814 wpa_hexdump(MSG_DEBUG, "WPA: set AP RSN IE", ie, len);
2815 sm->ap_rsn_ie = os_malloc(len);
2816 if (sm->ap_rsn_ie == NULL)
2817 return -1;
2818
2819 os_memcpy(sm->ap_rsn_ie, ie, len);
2820 sm->ap_rsn_ie_len = len;
2821 }
2822
2823 return 0;
2824}
2825
2826
2827/**
2828 * wpa_sm_parse_own_wpa_ie - Parse own WPA/RSN IE
2829 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2830 * @data: Pointer to data area for parsing results
2831 * Returns: 0 on success, -1 if IE is not known, or -2 on parsing failure
2832 *
2833 * Parse the contents of the own WPA or RSN IE from (Re)AssocReq and write the
2834 * parsed data into data.
2835 */
2836int wpa_sm_parse_own_wpa_ie(struct wpa_sm *sm, struct wpa_ie_data *data)
2837{
2838 if (sm == NULL)
2839 return -1;
2840
2841 if (sm->assoc_wpa_ie == NULL) {
2842 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2843 "WPA: No WPA/RSN IE available from association info");
2844 return -1;
2845 }
2846 if (wpa_parse_wpa_ie(sm->assoc_wpa_ie, sm->assoc_wpa_ie_len, data))
2847 return -2;
2848 return 0;
2849}
2850
2851
2852int wpa_sm_pmksa_cache_list(struct wpa_sm *sm, char *buf, size_t len)
2853{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002854 return pmksa_cache_list(sm->pmksa, buf, len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002855}
2856
2857
2858void wpa_sm_drop_sa(struct wpa_sm *sm)
2859{
2860 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Clear old PMK and PTK");
2861 sm->ptk_set = 0;
2862 sm->tptk_set = 0;
2863 os_memset(sm->pmk, 0, sizeof(sm->pmk));
2864 os_memset(&sm->ptk, 0, sizeof(sm->ptk));
2865 os_memset(&sm->tptk, 0, sizeof(sm->tptk));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002866#ifdef CONFIG_IEEE80211R
2867 os_memset(sm->xxkey, 0, sizeof(sm->xxkey));
2868 os_memset(sm->pmk_r0, 0, sizeof(sm->pmk_r0));
2869 os_memset(sm->pmk_r1, 0, sizeof(sm->pmk_r1));
2870#endif /* CONFIG_IEEE80211R */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002871}
2872
2873
2874int wpa_sm_has_ptk(struct wpa_sm *sm)
2875{
2876 if (sm == NULL)
2877 return 0;
2878 return sm->ptk_set;
2879}
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002880
2881
2882void wpa_sm_update_replay_ctr(struct wpa_sm *sm, const u8 *replay_ctr)
2883{
2884 os_memcpy(sm->rx_replay_counter, replay_ctr, WPA_REPLAY_COUNTER_LEN);
2885}
2886
2887
2888void wpa_sm_pmksa_cache_flush(struct wpa_sm *sm, void *network_ctx)
2889{
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07002890 pmksa_cache_flush(sm->pmksa, network_ctx, NULL, 0);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002891}
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002892
2893
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002894#ifdef CONFIG_WNM
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002895int wpa_wnmsleep_install_key(struct wpa_sm *sm, u8 subelem_id, u8 *buf)
2896{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002897 u16 keyinfo;
2898 u8 keylen; /* plaintext key len */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002899 u8 *key_rsc;
2900
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002901 if (subelem_id == WNM_SLEEP_SUBELEM_GTK) {
Dmitry Shmidt61593f02014-04-21 16:27:35 -07002902 struct wpa_gtk_data gd;
2903
2904 os_memset(&gd, 0, sizeof(gd));
2905 keylen = wpa_cipher_key_len(sm->group_cipher);
2906 gd.key_rsc_len = wpa_cipher_rsc_len(sm->group_cipher);
2907 gd.alg = wpa_cipher_to_alg(sm->group_cipher);
2908 if (gd.alg == WPA_ALG_NONE) {
2909 wpa_printf(MSG_DEBUG, "Unsupported group cipher suite");
2910 return -1;
2911 }
2912
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002913 key_rsc = buf + 5;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002914 keyinfo = WPA_GET_LE16(buf + 2);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002915 gd.gtk_len = keylen;
2916 if (gd.gtk_len != buf[4]) {
2917 wpa_printf(MSG_DEBUG, "GTK len mismatch len %d vs %d",
2918 gd.gtk_len, buf[4]);
2919 return -1;
2920 }
2921 gd.keyidx = keyinfo & 0x03; /* B0 - B1 */
2922 gd.tx = wpa_supplicant_gtk_tx_bit_workaround(
2923 sm, !!(keyinfo & WPA_KEY_INFO_TXRX));
2924
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002925 os_memcpy(gd.gtk, buf + 13, gd.gtk_len);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002926
2927 wpa_hexdump_key(MSG_DEBUG, "Install GTK (WNM SLEEP)",
2928 gd.gtk, gd.gtk_len);
2929 if (wpa_supplicant_install_gtk(sm, &gd, key_rsc)) {
Dmitry Shmidt61593f02014-04-21 16:27:35 -07002930 os_memset(&gd, 0, sizeof(gd));
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002931 wpa_printf(MSG_DEBUG, "Failed to install the GTK in "
2932 "WNM mode");
2933 return -1;
2934 }
Dmitry Shmidt61593f02014-04-21 16:27:35 -07002935 os_memset(&gd, 0, sizeof(gd));
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002936#ifdef CONFIG_IEEE80211W
2937 } else if (subelem_id == WNM_SLEEP_SUBELEM_IGTK) {
Dmitry Shmidt61593f02014-04-21 16:27:35 -07002938 struct wpa_igtk_kde igd;
2939 u16 keyidx;
2940
2941 os_memset(&igd, 0, sizeof(igd));
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002942 keylen = wpa_cipher_key_len(sm->mgmt_group_cipher);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002943 os_memcpy(igd.keyid, buf + 2, 2);
2944 os_memcpy(igd.pn, buf + 4, 6);
2945
2946 keyidx = WPA_GET_LE16(igd.keyid);
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002947 os_memcpy(igd.igtk, buf + 10, keylen);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002948
2949 wpa_hexdump_key(MSG_DEBUG, "Install IGTK (WNM SLEEP)",
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002950 igd.igtk, keylen);
2951 if (wpa_sm_set_key(sm, wpa_cipher_to_alg(sm->mgmt_group_cipher),
2952 broadcast_ether_addr,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002953 keyidx, 0, igd.pn, sizeof(igd.pn),
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002954 igd.igtk, keylen) < 0) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002955 wpa_printf(MSG_DEBUG, "Failed to install the IGTK in "
2956 "WNM mode");
Dmitry Shmidt61593f02014-04-21 16:27:35 -07002957 os_memset(&igd, 0, sizeof(igd));
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002958 return -1;
2959 }
Dmitry Shmidt61593f02014-04-21 16:27:35 -07002960 os_memset(&igd, 0, sizeof(igd));
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002961#endif /* CONFIG_IEEE80211W */
2962 } else {
2963 wpa_printf(MSG_DEBUG, "Unknown element id");
2964 return -1;
2965 }
2966
2967 return 0;
2968}
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002969#endif /* CONFIG_WNM */
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002970
2971
2972#ifdef CONFIG_PEERKEY
2973int wpa_sm_rx_eapol_peerkey(struct wpa_sm *sm, const u8 *src_addr,
2974 const u8 *buf, size_t len)
2975{
2976 struct wpa_peerkey *peerkey;
2977
2978 for (peerkey = sm->peerkey; peerkey; peerkey = peerkey->next) {
2979 if (os_memcmp(peerkey->addr, src_addr, ETH_ALEN) == 0)
2980 break;
2981 }
2982
2983 if (!peerkey)
2984 return 0;
2985
2986 wpa_sm_rx_eapol(sm, src_addr, buf, len);
2987
2988 return 1;
2989}
2990#endif /* CONFIG_PEERKEY */
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002991
2992
2993#ifdef CONFIG_P2P
2994
2995int wpa_sm_get_p2p_ip_addr(struct wpa_sm *sm, u8 *buf)
2996{
2997 if (sm == NULL || WPA_GET_BE32(sm->p2p_ip_addr) == 0)
2998 return -1;
2999 os_memcpy(buf, sm->p2p_ip_addr, 3 * 4);
3000 return 0;
3001}
3002
3003#endif /* CONFIG_P2P */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003004
3005
3006void wpa_sm_set_rx_replay_ctr(struct wpa_sm *sm, const u8 *rx_replay_counter)
3007{
3008 if (rx_replay_counter == NULL)
3009 return;
3010
3011 os_memcpy(sm->rx_replay_counter, rx_replay_counter,
3012 WPA_REPLAY_COUNTER_LEN);
3013 sm->rx_replay_counter_set = 1;
3014 wpa_printf(MSG_DEBUG, "Updated key replay counter");
3015}
3016
3017
Dmitry Shmidt807291d2015-01-27 13:40:23 -08003018void wpa_sm_set_ptk_kck_kek(struct wpa_sm *sm,
3019 const u8 *ptk_kck, size_t ptk_kck_len,
3020 const u8 *ptk_kek, size_t ptk_kek_len)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003021{
Dmitry Shmidt807291d2015-01-27 13:40:23 -08003022 if (ptk_kck && ptk_kck_len <= WPA_KCK_MAX_LEN) {
3023 os_memcpy(sm->ptk.kck, ptk_kck, ptk_kck_len);
3024 sm->ptk.kck_len = ptk_kck_len;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003025 wpa_printf(MSG_DEBUG, "Updated PTK KCK");
3026 }
Dmitry Shmidt807291d2015-01-27 13:40:23 -08003027 if (ptk_kek && ptk_kek_len <= WPA_KEK_MAX_LEN) {
3028 os_memcpy(sm->ptk.kek, ptk_kek, ptk_kek_len);
3029 sm->ptk.kek_len = ptk_kek_len;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003030 wpa_printf(MSG_DEBUG, "Updated PTK KEK");
3031 }
3032 sm->ptk_set = 1;
3033}