blob: 64ef933e3901d2fc2ee93cc9e1d074dfb3ef67f5 [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 */
Dmitry Shmidt014a3ff2015-12-28 13:27:49 -0800273 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
Jouni Malinen6ec30382015-07-08 20:48:18 +0300274 "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);
Dmitry Shmidt55840ad2015-12-14 12:45:46 -0800367 res = wpa_insert_pmkid(rsn_ie_buf, &wpa_ie_len,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700368 sm->pmk_r1_name);
369 if (res < 0) {
370 os_free(rsn_ie_buf);
371 return -1;
372 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700373
374 if (sm->assoc_resp_ies) {
375 os_memcpy(rsn_ie_buf + wpa_ie_len, sm->assoc_resp_ies,
376 sm->assoc_resp_ies_len);
377 wpa_ie_len += sm->assoc_resp_ies_len;
378 }
379
380 wpa_ie = rsn_ie_buf;
381 }
382#endif /* CONFIG_IEEE80211R */
383
384 wpa_hexdump(MSG_DEBUG, "WPA: WPA IE for msg 2/4", wpa_ie, wpa_ie_len);
385
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800386 mic_len = wpa_mic_len(sm->key_mgmt);
387 hdrlen = mic_len == 24 ? sizeof(*reply192) : sizeof(*reply);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700388 rbuf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_KEY,
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800389 NULL, hdrlen + wpa_ie_len,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700390 &rlen, (void *) &reply);
391 if (rbuf == NULL) {
392 os_free(rsn_ie_buf);
393 return -1;
394 }
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800395 reply192 = (struct wpa_eapol_key_192 *) reply;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700396
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800397 reply->type = (sm->proto == WPA_PROTO_RSN ||
398 sm->proto == WPA_PROTO_OSEN) ?
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700399 EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA;
400 WPA_PUT_BE16(reply->key_info,
401 ver | WPA_KEY_INFO_KEY_TYPE | WPA_KEY_INFO_MIC);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800402 if (sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700403 WPA_PUT_BE16(reply->key_length, 0);
404 else
405 os_memcpy(reply->key_length, key->key_length, 2);
406 os_memcpy(reply->replay_counter, key->replay_counter,
407 WPA_REPLAY_COUNTER_LEN);
408 wpa_hexdump(MSG_DEBUG, "WPA: Replay Counter", reply->replay_counter,
409 WPA_REPLAY_COUNTER_LEN);
410
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800411 key_mic = reply192->key_mic; /* same offset for reply and reply192 */
412 if (mic_len == 24) {
413 WPA_PUT_BE16(reply192->key_data_length, wpa_ie_len);
414 os_memcpy(reply192 + 1, wpa_ie, wpa_ie_len);
415 } else {
416 WPA_PUT_BE16(reply->key_data_length, wpa_ie_len);
417 os_memcpy(reply + 1, wpa_ie, wpa_ie_len);
418 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700419 os_free(rsn_ie_buf);
420
421 os_memcpy(reply->key_nonce, nonce, WPA_NONCE_LEN);
422
423 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Sending EAPOL-Key 2/4");
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800424 return wpa_eapol_key_send(sm, ptk->kck, ptk->kck_len, ver, dst,
425 ETH_P_EAPOL, rbuf, rlen, key_mic);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700426}
427
428
429static int wpa_derive_ptk(struct wpa_sm *sm, const unsigned char *src_addr,
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800430 const struct wpa_eapol_key *key, struct wpa_ptk *ptk)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700431{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700432#ifdef CONFIG_IEEE80211R
433 if (wpa_key_mgmt_ft(sm->key_mgmt))
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800434 return wpa_derive_ptk_ft(sm, src_addr, key, ptk);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700435#endif /* CONFIG_IEEE80211R */
436
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800437 return wpa_pmk_to_ptk(sm->pmk, sm->pmk_len, "Pairwise key expansion",
438 sm->own_addr, sm->bssid, sm->snonce,
439 key->key_nonce, ptk, sm->key_mgmt,
440 sm->pairwise_cipher);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700441}
442
443
444static void wpa_supplicant_process_1_of_4(struct wpa_sm *sm,
445 const unsigned char *src_addr,
446 const struct wpa_eapol_key *key,
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700447 u16 ver, const u8 *key_data,
448 size_t key_data_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700449{
450 struct wpa_eapol_ie_parse ie;
451 struct wpa_ptk *ptk;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700452 int res;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800453 u8 *kde, *kde_buf = NULL;
454 size_t kde_len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700455
456 if (wpa_sm_get_network_ctx(sm) == NULL) {
457 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, "WPA: No SSID info "
458 "found (msg 1 of 4)");
459 return;
460 }
461
462 wpa_sm_set_state(sm, WPA_4WAY_HANDSHAKE);
463 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: RX message 1 of 4-Way "
464 "Handshake from " MACSTR " (ver=%d)", MAC2STR(src_addr), ver);
465
466 os_memset(&ie, 0, sizeof(ie));
467
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800468 if (sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700469 /* RSN: msg 1/4 should contain PMKID for the selected PMK */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700470 wpa_hexdump(MSG_DEBUG, "RSN: msg 1/4 key data",
471 key_data, key_data_len);
472 if (wpa_supplicant_parse_ies(key_data, key_data_len, &ie) < 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800473 goto failed;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700474 if (ie.pmkid) {
475 wpa_hexdump(MSG_DEBUG, "RSN: PMKID from "
476 "Authenticator", ie.pmkid, PMKID_LEN);
477 }
478 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700479
480 res = wpa_supplicant_get_pmk(sm, src_addr, ie.pmkid);
481 if (res == -2) {
482 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "RSN: Do not reply to "
483 "msg 1/4 - requesting full EAP authentication");
484 return;
485 }
486 if (res)
487 goto failed;
488
489 if (sm->renew_snonce) {
490 if (random_get_bytes(sm->snonce, WPA_NONCE_LEN)) {
491 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
492 "WPA: Failed to get random data for SNonce");
493 goto failed;
494 }
495 sm->renew_snonce = 0;
496 wpa_hexdump(MSG_DEBUG, "WPA: Renewed SNonce",
497 sm->snonce, WPA_NONCE_LEN);
498 }
499
500 /* Calculate PTK which will be stored as a temporary PTK until it has
501 * been verified when processing message 3/4. */
502 ptk = &sm->tptk;
503 wpa_derive_ptk(sm, src_addr, key, ptk);
Dmitry Shmidt98660862014-03-11 17:26:21 -0700504 if (sm->pairwise_cipher == WPA_CIPHER_TKIP) {
Dmitry Shmidt61593f02014-04-21 16:27:35 -0700505 u8 buf[8];
Dmitry Shmidt98660862014-03-11 17:26:21 -0700506 /* Supplicant: swap tx/rx Mic keys */
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800507 os_memcpy(buf, &ptk->tk[16], 8);
508 os_memcpy(&ptk->tk[16], &ptk->tk[24], 8);
509 os_memcpy(&ptk->tk[24], buf, 8);
Dmitry Shmidt61593f02014-04-21 16:27:35 -0700510 os_memset(buf, 0, sizeof(buf));
Dmitry Shmidt98660862014-03-11 17:26:21 -0700511 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700512 sm->tptk_set = 1;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800513 sm->tk_to_set = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700514
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800515 kde = sm->assoc_wpa_ie;
516 kde_len = sm->assoc_wpa_ie_len;
517
518#ifdef CONFIG_P2P
519 if (sm->p2p) {
520 kde_buf = os_malloc(kde_len + 2 + RSN_SELECTOR_LEN + 1);
521 if (kde_buf) {
522 u8 *pos;
523 wpa_printf(MSG_DEBUG, "P2P: Add IP Address Request KDE "
524 "into EAPOL-Key 2/4");
525 os_memcpy(kde_buf, kde, kde_len);
526 kde = kde_buf;
527 pos = kde + kde_len;
528 *pos++ = WLAN_EID_VENDOR_SPECIFIC;
529 *pos++ = RSN_SELECTOR_LEN + 1;
530 RSN_SELECTOR_PUT(pos, WFA_KEY_DATA_IP_ADDR_REQ);
531 pos += RSN_SELECTOR_LEN;
532 *pos++ = 0x01;
533 kde_len = pos - kde;
534 }
535 }
536#endif /* CONFIG_P2P */
537
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700538 if (wpa_supplicant_send_2_of_4(sm, sm->bssid, key, ver, sm->snonce,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800539 kde, kde_len, ptk) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700540 goto failed;
541
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800542 os_free(kde_buf);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700543 os_memcpy(sm->anonce, key->key_nonce, WPA_NONCE_LEN);
544 return;
545
546failed:
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800547 os_free(kde_buf);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700548 wpa_sm_deauthenticate(sm, WLAN_REASON_UNSPECIFIED);
549}
550
551
552static void wpa_sm_start_preauth(void *eloop_ctx, void *timeout_ctx)
553{
554 struct wpa_sm *sm = eloop_ctx;
555 rsn_preauth_candidate_process(sm);
556}
557
558
559static void wpa_supplicant_key_neg_complete(struct wpa_sm *sm,
560 const u8 *addr, int secure)
561{
562 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
563 "WPA: Key negotiation completed with "
564 MACSTR " [PTK=%s GTK=%s]", MAC2STR(addr),
565 wpa_cipher_txt(sm->pairwise_cipher),
566 wpa_cipher_txt(sm->group_cipher));
567 wpa_sm_cancel_auth_timeout(sm);
568 wpa_sm_set_state(sm, WPA_COMPLETED);
569
570 if (secure) {
571 wpa_sm_mlme_setprotection(
572 sm, addr, MLME_SETPROTECTION_PROTECT_TYPE_RX_TX,
573 MLME_SETPROTECTION_KEY_TYPE_PAIRWISE);
574 eapol_sm_notify_portValid(sm->eapol, TRUE);
575 if (wpa_key_mgmt_wpa_psk(sm->key_mgmt))
576 eapol_sm_notify_eap_success(sm->eapol, TRUE);
577 /*
578 * Start preauthentication after a short wait to avoid a
579 * possible race condition between the data receive and key
580 * configuration after the 4-Way Handshake. This increases the
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800581 * likelihood of the first preauth EAPOL-Start frame getting to
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700582 * the target AP.
583 */
584 eloop_register_timeout(1, 0, wpa_sm_start_preauth, sm, NULL);
585 }
586
587 if (sm->cur_pmksa && sm->cur_pmksa->opportunistic) {
588 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
589 "RSN: Authenticator accepted "
590 "opportunistic PMKSA entry - marking it valid");
591 sm->cur_pmksa->opportunistic = 0;
592 }
593
594#ifdef CONFIG_IEEE80211R
595 if (wpa_key_mgmt_ft(sm->key_mgmt)) {
596 /* Prepare for the next transition */
597 wpa_ft_prepare_auth_request(sm, NULL);
598 }
599#endif /* CONFIG_IEEE80211R */
600}
601
602
603static void wpa_sm_rekey_ptk(void *eloop_ctx, void *timeout_ctx)
604{
605 struct wpa_sm *sm = eloop_ctx;
606 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Request PTK rekeying");
607 wpa_sm_key_request(sm, 0, 1);
608}
609
610
611static int wpa_supplicant_install_ptk(struct wpa_sm *sm,
612 const struct wpa_eapol_key *key)
613{
614 int keylen, rsclen;
615 enum wpa_alg alg;
616 const u8 *key_rsc;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800617
618 if (!sm->tk_to_set) {
619 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
620 "WPA: Do not re-install same PTK to the driver");
621 return 0;
622 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700623
624 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
625 "WPA: Installing PTK to the driver");
626
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700627 if (sm->pairwise_cipher == WPA_CIPHER_NONE) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700628 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Pairwise Cipher "
629 "Suite: NONE - do not use pairwise keys");
630 return 0;
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700631 }
632
633 if (!wpa_cipher_valid_pairwise(sm->pairwise_cipher)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700634 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
635 "WPA: Unsupported pairwise cipher %d",
636 sm->pairwise_cipher);
637 return -1;
638 }
639
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700640 alg = wpa_cipher_to_alg(sm->pairwise_cipher);
641 keylen = wpa_cipher_key_len(sm->pairwise_cipher);
642 rsclen = wpa_cipher_rsc_len(sm->pairwise_cipher);
643
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800644 if (sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700645 key_rsc = null_rsc;
646 } else {
647 key_rsc = key->key_rsc;
648 wpa_hexdump(MSG_DEBUG, "WPA: RSC", key_rsc, rsclen);
649 }
650
651 if (wpa_sm_set_key(sm, alg, sm->bssid, 0, 1, key_rsc, rsclen,
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800652 sm->ptk.tk, keylen) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700653 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
654 "WPA: Failed to set PTK to the "
655 "driver (alg=%d keylen=%d bssid=" MACSTR ")",
656 alg, keylen, MAC2STR(sm->bssid));
657 return -1;
658 }
659
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800660 /* TK is not needed anymore in supplicant */
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800661 os_memset(sm->ptk.tk, 0, WPA_TK_MAX_LEN);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800662 sm->tk_to_set = 0;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800663
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700664 if (sm->wpa_ptk_rekey) {
665 eloop_cancel_timeout(wpa_sm_rekey_ptk, sm, NULL);
666 eloop_register_timeout(sm->wpa_ptk_rekey, 0, wpa_sm_rekey_ptk,
667 sm, NULL);
668 }
669
670 return 0;
671}
672
673
674static int wpa_supplicant_check_group_cipher(struct wpa_sm *sm,
675 int group_cipher,
676 int keylen, int maxkeylen,
677 int *key_rsc_len,
678 enum wpa_alg *alg)
679{
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700680 int klen;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700681
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700682 *alg = wpa_cipher_to_alg(group_cipher);
683 if (*alg == WPA_ALG_NONE) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700684 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
685 "WPA: Unsupported Group Cipher %d",
686 group_cipher);
687 return -1;
688 }
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700689 *key_rsc_len = wpa_cipher_rsc_len(group_cipher);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700690
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700691 klen = wpa_cipher_key_len(group_cipher);
692 if (keylen != klen || maxkeylen < klen) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700693 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
694 "WPA: Unsupported %s Group Cipher key length %d (%d)",
695 wpa_cipher_txt(group_cipher), keylen, maxkeylen);
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700696 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700697 }
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700698 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700699}
700
701
702struct wpa_gtk_data {
703 enum wpa_alg alg;
704 int tx, key_rsc_len, keyidx;
705 u8 gtk[32];
706 int gtk_len;
707};
708
709
710static int wpa_supplicant_install_gtk(struct wpa_sm *sm,
711 const struct wpa_gtk_data *gd,
712 const u8 *key_rsc)
713{
714 const u8 *_gtk = gd->gtk;
715 u8 gtk_buf[32];
716
717 wpa_hexdump_key(MSG_DEBUG, "WPA: Group Key", gd->gtk, gd->gtk_len);
718 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
719 "WPA: Installing GTK to the driver (keyidx=%d tx=%d len=%d)",
720 gd->keyidx, gd->tx, gd->gtk_len);
721 wpa_hexdump(MSG_DEBUG, "WPA: RSC", key_rsc, gd->key_rsc_len);
722 if (sm->group_cipher == WPA_CIPHER_TKIP) {
723 /* Swap Tx/Rx keys for Michael MIC */
724 os_memcpy(gtk_buf, gd->gtk, 16);
725 os_memcpy(gtk_buf + 16, gd->gtk + 24, 8);
726 os_memcpy(gtk_buf + 24, gd->gtk + 16, 8);
727 _gtk = gtk_buf;
728 }
729 if (sm->pairwise_cipher == WPA_CIPHER_NONE) {
730 if (wpa_sm_set_key(sm, gd->alg, NULL,
731 gd->keyidx, 1, key_rsc, gd->key_rsc_len,
732 _gtk, gd->gtk_len) < 0) {
733 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
734 "WPA: Failed to set GTK to the driver "
735 "(Group only)");
Dmitry Shmidt61593f02014-04-21 16:27:35 -0700736 os_memset(gtk_buf, 0, sizeof(gtk_buf));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700737 return -1;
738 }
739 } else if (wpa_sm_set_key(sm, gd->alg, broadcast_ether_addr,
740 gd->keyidx, gd->tx, key_rsc, gd->key_rsc_len,
741 _gtk, gd->gtk_len) < 0) {
742 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
743 "WPA: Failed to set GTK to "
744 "the driver (alg=%d keylen=%d keyidx=%d)",
745 gd->alg, gd->gtk_len, gd->keyidx);
Dmitry Shmidt61593f02014-04-21 16:27:35 -0700746 os_memset(gtk_buf, 0, sizeof(gtk_buf));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700747 return -1;
748 }
Dmitry Shmidt61593f02014-04-21 16:27:35 -0700749 os_memset(gtk_buf, 0, sizeof(gtk_buf));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700750
751 return 0;
752}
753
754
755static int wpa_supplicant_gtk_tx_bit_workaround(const struct wpa_sm *sm,
756 int tx)
757{
758 if (tx && sm->pairwise_cipher != WPA_CIPHER_NONE) {
759 /* Ignore Tx bit for GTK if a pairwise key is used. One AP
760 * seemed to set this bit (incorrectly, since Tx is only when
761 * doing Group Key only APs) and without this workaround, the
762 * data connection does not work because wpa_supplicant
763 * configured non-zero keyidx to be used for unicast. */
764 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
765 "WPA: Tx bit set for GTK, but pairwise "
766 "keys are used - ignore Tx bit");
767 return 0;
768 }
769 return tx;
770}
771
772
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800773static int wpa_supplicant_rsc_relaxation(const struct wpa_sm *sm,
774 const u8 *rsc)
775{
776 int rsclen;
777
778 if (!sm->wpa_rsc_relaxation)
779 return 0;
780
781 rsclen = wpa_cipher_rsc_len(sm->group_cipher);
782
783 /*
784 * Try to detect RSC (endian) corruption issue where the AP sends
785 * the RSC bytes in EAPOL-Key message in the wrong order, both if
786 * it's actually a 6-byte field (as it should be) and if it treats
787 * it as an 8-byte field.
788 * An AP model known to have this bug is the Sapido RB-1632.
789 */
790 if (rsclen == 6 && ((rsc[5] && !rsc[0]) || rsc[6] || rsc[7])) {
791 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
792 "RSC %02x%02x%02x%02x%02x%02x%02x%02x is likely bogus, using 0",
793 rsc[0], rsc[1], rsc[2], rsc[3],
794 rsc[4], rsc[5], rsc[6], rsc[7]);
795
796 return 1;
797 }
798
799 return 0;
800}
801
802
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700803static int wpa_supplicant_pairwise_gtk(struct wpa_sm *sm,
804 const struct wpa_eapol_key *key,
805 const u8 *gtk, size_t gtk_len,
806 int key_info)
807{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700808 struct wpa_gtk_data gd;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800809 const u8 *key_rsc;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700810
811 /*
812 * IEEE Std 802.11i-2004 - 8.5.2 EAPOL-Key frames - Figure 43x
813 * GTK KDE format:
814 * KeyID[bits 0-1], Tx [bit 2], Reserved [bits 3-7]
815 * Reserved [bits 0-7]
816 * GTK
817 */
818
819 os_memset(&gd, 0, sizeof(gd));
820 wpa_hexdump_key(MSG_DEBUG, "RSN: received GTK in pairwise handshake",
821 gtk, gtk_len);
822
823 if (gtk_len < 2 || gtk_len - 2 > sizeof(gd.gtk))
824 return -1;
825
826 gd.keyidx = gtk[0] & 0x3;
827 gd.tx = wpa_supplicant_gtk_tx_bit_workaround(sm,
828 !!(gtk[0] & BIT(2)));
829 gtk += 2;
830 gtk_len -= 2;
831
832 os_memcpy(gd.gtk, gtk, gtk_len);
833 gd.gtk_len = gtk_len;
834
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800835 key_rsc = key->key_rsc;
836 if (wpa_supplicant_rsc_relaxation(sm, key->key_rsc))
837 key_rsc = null_rsc;
838
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800839 if (sm->group_cipher != WPA_CIPHER_GTK_NOT_USED &&
840 (wpa_supplicant_check_group_cipher(sm, sm->group_cipher,
841 gtk_len, gtk_len,
842 &gd.key_rsc_len, &gd.alg) ||
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800843 wpa_supplicant_install_gtk(sm, &gd, key_rsc))) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700844 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
845 "RSN: Failed to install GTK");
Dmitry Shmidt61593f02014-04-21 16:27:35 -0700846 os_memset(&gd, 0, sizeof(gd));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700847 return -1;
848 }
Dmitry Shmidt61593f02014-04-21 16:27:35 -0700849 os_memset(&gd, 0, sizeof(gd));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700850
851 wpa_supplicant_key_neg_complete(sm, sm->bssid,
852 key_info & WPA_KEY_INFO_SECURE);
853 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700854}
855
856
857static int ieee80211w_set_keys(struct wpa_sm *sm,
858 struct wpa_eapol_ie_parse *ie)
859{
860#ifdef CONFIG_IEEE80211W
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -0700861 if (!wpa_cipher_valid_mgmt_group(sm->mgmt_group_cipher))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700862 return 0;
863
864 if (ie->igtk) {
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -0700865 size_t len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700866 const struct wpa_igtk_kde *igtk;
867 u16 keyidx;
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -0700868 len = wpa_cipher_key_len(sm->mgmt_group_cipher);
869 if (ie->igtk_len != WPA_IGTK_KDE_PREFIX_LEN + len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700870 return -1;
871 igtk = (const struct wpa_igtk_kde *) ie->igtk;
872 keyidx = WPA_GET_LE16(igtk->keyid);
873 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: IGTK keyid %d "
874 "pn %02x%02x%02x%02x%02x%02x",
875 keyidx, MAC2STR(igtk->pn));
876 wpa_hexdump_key(MSG_DEBUG, "WPA: IGTK",
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -0700877 igtk->igtk, len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700878 if (keyidx > 4095) {
879 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
880 "WPA: Invalid IGTK KeyID %d", keyidx);
881 return -1;
882 }
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -0700883 if (wpa_sm_set_key(sm, wpa_cipher_to_alg(sm->mgmt_group_cipher),
884 broadcast_ether_addr,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700885 keyidx, 0, igtk->pn, sizeof(igtk->pn),
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -0700886 igtk->igtk, len) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700887 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
888 "WPA: Failed to configure IGTK to the driver");
889 return -1;
890 }
891 }
892
893 return 0;
894#else /* CONFIG_IEEE80211W */
895 return 0;
896#endif /* CONFIG_IEEE80211W */
897}
898
899
900static void wpa_report_ie_mismatch(struct wpa_sm *sm,
901 const char *reason, const u8 *src_addr,
902 const u8 *wpa_ie, size_t wpa_ie_len,
903 const u8 *rsn_ie, size_t rsn_ie_len)
904{
905 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, "WPA: %s (src=" MACSTR ")",
906 reason, MAC2STR(src_addr));
907
908 if (sm->ap_wpa_ie) {
909 wpa_hexdump(MSG_INFO, "WPA: WPA IE in Beacon/ProbeResp",
910 sm->ap_wpa_ie, sm->ap_wpa_ie_len);
911 }
912 if (wpa_ie) {
913 if (!sm->ap_wpa_ie) {
914 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
915 "WPA: No WPA IE in Beacon/ProbeResp");
916 }
917 wpa_hexdump(MSG_INFO, "WPA: WPA IE in 3/4 msg",
918 wpa_ie, wpa_ie_len);
919 }
920
921 if (sm->ap_rsn_ie) {
922 wpa_hexdump(MSG_INFO, "WPA: RSN IE in Beacon/ProbeResp",
923 sm->ap_rsn_ie, sm->ap_rsn_ie_len);
924 }
925 if (rsn_ie) {
926 if (!sm->ap_rsn_ie) {
927 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
928 "WPA: No RSN IE in Beacon/ProbeResp");
929 }
930 wpa_hexdump(MSG_INFO, "WPA: RSN IE in 3/4 msg",
931 rsn_ie, rsn_ie_len);
932 }
933
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800934 wpa_sm_deauthenticate(sm, WLAN_REASON_IE_IN_4WAY_DIFFERS);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700935}
936
937
938#ifdef CONFIG_IEEE80211R
939
940static int ft_validate_mdie(struct wpa_sm *sm,
941 const unsigned char *src_addr,
942 struct wpa_eapol_ie_parse *ie,
943 const u8 *assoc_resp_mdie)
944{
945 struct rsn_mdie *mdie;
946
947 mdie = (struct rsn_mdie *) (ie->mdie + 2);
948 if (ie->mdie == NULL || ie->mdie_len < 2 + sizeof(*mdie) ||
949 os_memcmp(mdie->mobility_domain, sm->mobility_domain,
950 MOBILITY_DOMAIN_ID_LEN) != 0) {
951 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "FT: MDIE in msg 3/4 did "
952 "not match with the current mobility domain");
953 return -1;
954 }
955
956 if (assoc_resp_mdie &&
957 (assoc_resp_mdie[1] != ie->mdie[1] ||
958 os_memcmp(assoc_resp_mdie, ie->mdie, 2 + ie->mdie[1]) != 0)) {
959 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "FT: MDIE mismatch");
960 wpa_hexdump(MSG_DEBUG, "FT: MDIE in EAPOL-Key msg 3/4",
961 ie->mdie, 2 + ie->mdie[1]);
962 wpa_hexdump(MSG_DEBUG, "FT: MDIE in (Re)Association Response",
963 assoc_resp_mdie, 2 + assoc_resp_mdie[1]);
964 return -1;
965 }
966
967 return 0;
968}
969
970
971static int ft_validate_ftie(struct wpa_sm *sm,
972 const unsigned char *src_addr,
973 struct wpa_eapol_ie_parse *ie,
974 const u8 *assoc_resp_ftie)
975{
976 if (ie->ftie == NULL) {
977 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
978 "FT: No FTIE in EAPOL-Key msg 3/4");
979 return -1;
980 }
981
982 if (assoc_resp_ftie == NULL)
983 return 0;
984
985 if (assoc_resp_ftie[1] != ie->ftie[1] ||
986 os_memcmp(assoc_resp_ftie, ie->ftie, 2 + ie->ftie[1]) != 0) {
987 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "FT: FTIE mismatch");
988 wpa_hexdump(MSG_DEBUG, "FT: FTIE in EAPOL-Key msg 3/4",
989 ie->ftie, 2 + ie->ftie[1]);
990 wpa_hexdump(MSG_DEBUG, "FT: FTIE in (Re)Association Response",
991 assoc_resp_ftie, 2 + assoc_resp_ftie[1]);
992 return -1;
993 }
994
995 return 0;
996}
997
998
999static int ft_validate_rsnie(struct wpa_sm *sm,
1000 const unsigned char *src_addr,
1001 struct wpa_eapol_ie_parse *ie)
1002{
1003 struct wpa_ie_data rsn;
1004
1005 if (!ie->rsn_ie)
1006 return 0;
1007
1008 /*
1009 * Verify that PMKR1Name from EAPOL-Key message 3/4
1010 * matches with the value we derived.
1011 */
1012 if (wpa_parse_wpa_ie_rsn(ie->rsn_ie, ie->rsn_ie_len, &rsn) < 0 ||
1013 rsn.num_pmkid != 1 || rsn.pmkid == NULL) {
1014 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "FT: No PMKR1Name in "
1015 "FT 4-way handshake message 3/4");
1016 return -1;
1017 }
1018
Dmitry Shmidtc2817022014-07-02 10:32:10 -07001019 if (os_memcmp_const(rsn.pmkid, sm->pmk_r1_name, WPA_PMK_NAME_LEN) != 0)
1020 {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001021 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1022 "FT: PMKR1Name mismatch in "
1023 "FT 4-way handshake message 3/4");
1024 wpa_hexdump(MSG_DEBUG, "FT: PMKR1Name from Authenticator",
1025 rsn.pmkid, WPA_PMK_NAME_LEN);
1026 wpa_hexdump(MSG_DEBUG, "FT: Derived PMKR1Name",
1027 sm->pmk_r1_name, WPA_PMK_NAME_LEN);
1028 return -1;
1029 }
1030
1031 return 0;
1032}
1033
1034
1035static int wpa_supplicant_validate_ie_ft(struct wpa_sm *sm,
1036 const unsigned char *src_addr,
1037 struct wpa_eapol_ie_parse *ie)
1038{
1039 const u8 *pos, *end, *mdie = NULL, *ftie = NULL;
1040
1041 if (sm->assoc_resp_ies) {
1042 pos = sm->assoc_resp_ies;
1043 end = pos + sm->assoc_resp_ies_len;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001044 while (end - pos > 2) {
1045 if (2 + pos[1] > end - pos)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001046 break;
1047 switch (*pos) {
1048 case WLAN_EID_MOBILITY_DOMAIN:
1049 mdie = pos;
1050 break;
1051 case WLAN_EID_FAST_BSS_TRANSITION:
1052 ftie = pos;
1053 break;
1054 }
1055 pos += 2 + pos[1];
1056 }
1057 }
1058
1059 if (ft_validate_mdie(sm, src_addr, ie, mdie) < 0 ||
1060 ft_validate_ftie(sm, src_addr, ie, ftie) < 0 ||
1061 ft_validate_rsnie(sm, src_addr, ie) < 0)
1062 return -1;
1063
1064 return 0;
1065}
1066
1067#endif /* CONFIG_IEEE80211R */
1068
1069
1070static int wpa_supplicant_validate_ie(struct wpa_sm *sm,
1071 const unsigned char *src_addr,
1072 struct wpa_eapol_ie_parse *ie)
1073{
1074 if (sm->ap_wpa_ie == NULL && sm->ap_rsn_ie == NULL) {
1075 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1076 "WPA: No WPA/RSN IE for this AP known. "
1077 "Trying to get from scan results");
1078 if (wpa_sm_get_beacon_ie(sm) < 0) {
1079 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1080 "WPA: Could not find AP from "
1081 "the scan results");
1082 } else {
1083 wpa_msg(sm->ctx->msg_ctx, MSG_DEBUG,
1084 "WPA: Found the current AP from "
1085 "updated scan results");
1086 }
1087 }
1088
1089 if (ie->wpa_ie == NULL && ie->rsn_ie == NULL &&
1090 (sm->ap_wpa_ie || sm->ap_rsn_ie)) {
1091 wpa_report_ie_mismatch(sm, "IE in 3/4 msg does not match "
1092 "with IE in Beacon/ProbeResp (no IE?)",
1093 src_addr, ie->wpa_ie, ie->wpa_ie_len,
1094 ie->rsn_ie, ie->rsn_ie_len);
1095 return -1;
1096 }
1097
1098 if ((ie->wpa_ie && sm->ap_wpa_ie &&
1099 (ie->wpa_ie_len != sm->ap_wpa_ie_len ||
1100 os_memcmp(ie->wpa_ie, sm->ap_wpa_ie, ie->wpa_ie_len) != 0)) ||
1101 (ie->rsn_ie && sm->ap_rsn_ie &&
1102 wpa_compare_rsn_ie(wpa_key_mgmt_ft(sm->key_mgmt),
1103 sm->ap_rsn_ie, sm->ap_rsn_ie_len,
1104 ie->rsn_ie, ie->rsn_ie_len))) {
1105 wpa_report_ie_mismatch(sm, "IE in 3/4 msg does not match "
1106 "with IE in Beacon/ProbeResp",
1107 src_addr, ie->wpa_ie, ie->wpa_ie_len,
1108 ie->rsn_ie, ie->rsn_ie_len);
1109 return -1;
1110 }
1111
1112 if (sm->proto == WPA_PROTO_WPA &&
1113 ie->rsn_ie && sm->ap_rsn_ie == NULL && sm->rsn_enabled) {
1114 wpa_report_ie_mismatch(sm, "Possible downgrade attack "
1115 "detected - RSN was enabled and RSN IE "
1116 "was in msg 3/4, but not in "
1117 "Beacon/ProbeResp",
1118 src_addr, ie->wpa_ie, ie->wpa_ie_len,
1119 ie->rsn_ie, ie->rsn_ie_len);
1120 return -1;
1121 }
1122
1123#ifdef CONFIG_IEEE80211R
1124 if (wpa_key_mgmt_ft(sm->key_mgmt) &&
1125 wpa_supplicant_validate_ie_ft(sm, src_addr, ie) < 0)
1126 return -1;
1127#endif /* CONFIG_IEEE80211R */
1128
1129 return 0;
1130}
1131
1132
1133/**
1134 * wpa_supplicant_send_4_of_4 - Send message 4 of WPA/RSN 4-Way Handshake
1135 * @sm: Pointer to WPA state machine data from wpa_sm_init()
1136 * @dst: Destination address for the frame
1137 * @key: Pointer to the EAPOL-Key frame header
1138 * @ver: Version bits from EAPOL-Key Key Info
1139 * @key_info: Key Info
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001140 * @ptk: PTK to use for keyed hash and encryption
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001141 * Returns: >= 0 on success, < 0 on failure
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001142 */
1143int wpa_supplicant_send_4_of_4(struct wpa_sm *sm, const unsigned char *dst,
1144 const struct wpa_eapol_key *key,
1145 u16 ver, u16 key_info,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001146 struct wpa_ptk *ptk)
1147{
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001148 size_t mic_len, hdrlen, rlen;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001149 struct wpa_eapol_key *reply;
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001150 struct wpa_eapol_key_192 *reply192;
1151 u8 *rbuf, *key_mic;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001152
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001153 mic_len = wpa_mic_len(sm->key_mgmt);
1154 hdrlen = mic_len == 24 ? sizeof(*reply192) : sizeof(*reply);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001155 rbuf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_KEY, NULL,
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001156 hdrlen, &rlen, (void *) &reply);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001157 if (rbuf == NULL)
1158 return -1;
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001159 reply192 = (struct wpa_eapol_key_192 *) reply;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001160
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001161 reply->type = (sm->proto == WPA_PROTO_RSN ||
1162 sm->proto == WPA_PROTO_OSEN) ?
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001163 EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA;
1164 key_info &= WPA_KEY_INFO_SECURE;
1165 key_info |= ver | WPA_KEY_INFO_KEY_TYPE | WPA_KEY_INFO_MIC;
1166 WPA_PUT_BE16(reply->key_info, key_info);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001167 if (sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001168 WPA_PUT_BE16(reply->key_length, 0);
1169 else
1170 os_memcpy(reply->key_length, key->key_length, 2);
1171 os_memcpy(reply->replay_counter, key->replay_counter,
1172 WPA_REPLAY_COUNTER_LEN);
1173
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001174 key_mic = reply192->key_mic; /* same offset for reply and reply192 */
1175 if (mic_len == 24)
1176 WPA_PUT_BE16(reply192->key_data_length, 0);
1177 else
1178 WPA_PUT_BE16(reply->key_data_length, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001179
1180 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Sending EAPOL-Key 4/4");
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001181 return wpa_eapol_key_send(sm, ptk->kck, ptk->kck_len, ver, dst,
1182 ETH_P_EAPOL, rbuf, rlen, key_mic);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001183}
1184
1185
1186static void wpa_supplicant_process_3_of_4(struct wpa_sm *sm,
1187 const struct wpa_eapol_key *key,
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001188 u16 ver, const u8 *key_data,
1189 size_t key_data_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001190{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001191 u16 key_info, keylen;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001192 struct wpa_eapol_ie_parse ie;
1193
1194 wpa_sm_set_state(sm, WPA_4WAY_HANDSHAKE);
1195 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: RX message 3 of 4-Way "
1196 "Handshake from " MACSTR " (ver=%d)", MAC2STR(sm->bssid), ver);
1197
1198 key_info = WPA_GET_BE16(key->key_info);
1199
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001200 wpa_hexdump(MSG_DEBUG, "WPA: IE KeyData", key_data, key_data_len);
1201 if (wpa_supplicant_parse_ies(key_data, key_data_len, &ie) < 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001202 goto failed;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001203 if (ie.gtk && !(key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
1204 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1205 "WPA: GTK IE in unencrypted key data");
1206 goto failed;
1207 }
1208#ifdef CONFIG_IEEE80211W
1209 if (ie.igtk && !(key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
1210 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1211 "WPA: IGTK KDE in unencrypted key data");
1212 goto failed;
1213 }
1214
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001215 if (ie.igtk &&
1216 wpa_cipher_valid_mgmt_group(sm->mgmt_group_cipher) &&
1217 ie.igtk_len != WPA_IGTK_KDE_PREFIX_LEN +
1218 (unsigned int) wpa_cipher_key_len(sm->mgmt_group_cipher)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001219 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1220 "WPA: Invalid IGTK KDE length %lu",
1221 (unsigned long) ie.igtk_len);
1222 goto failed;
1223 }
1224#endif /* CONFIG_IEEE80211W */
1225
1226 if (wpa_supplicant_validate_ie(sm, sm->bssid, &ie) < 0)
1227 goto failed;
1228
1229 if (os_memcmp(sm->anonce, key->key_nonce, WPA_NONCE_LEN) != 0) {
1230 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1231 "WPA: ANonce from message 1 of 4-Way Handshake "
1232 "differs from 3 of 4-Way Handshake - drop packet (src="
1233 MACSTR ")", MAC2STR(sm->bssid));
1234 goto failed;
1235 }
1236
1237 keylen = WPA_GET_BE16(key->key_length);
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07001238 if (keylen != wpa_cipher_key_len(sm->pairwise_cipher)) {
1239 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1240 "WPA: Invalid %s key length %d (src=" MACSTR
1241 ")", wpa_cipher_txt(sm->pairwise_cipher), keylen,
1242 MAC2STR(sm->bssid));
1243 goto failed;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001244 }
1245
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001246#ifdef CONFIG_P2P
1247 if (ie.ip_addr_alloc) {
1248 os_memcpy(sm->p2p_ip_addr, ie.ip_addr_alloc, 3 * 4);
1249 wpa_hexdump(MSG_DEBUG, "P2P: IP address info",
1250 sm->p2p_ip_addr, sizeof(sm->p2p_ip_addr));
1251 }
1252#endif /* CONFIG_P2P */
1253
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001254 if (wpa_supplicant_send_4_of_4(sm, sm->bssid, key, ver, key_info,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001255 &sm->ptk) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001256 goto failed;
1257 }
1258
1259 /* SNonce was successfully used in msg 3/4, so mark it to be renewed
1260 * for the next 4-Way Handshake. If msg 3 is received again, the old
1261 * SNonce will still be used to avoid changing PTK. */
1262 sm->renew_snonce = 1;
1263
1264 if (key_info & WPA_KEY_INFO_INSTALL) {
1265 if (wpa_supplicant_install_ptk(sm, key))
1266 goto failed;
1267 }
1268
1269 if (key_info & WPA_KEY_INFO_SECURE) {
1270 wpa_sm_mlme_setprotection(
1271 sm, sm->bssid, MLME_SETPROTECTION_PROTECT_TYPE_RX,
1272 MLME_SETPROTECTION_KEY_TYPE_PAIRWISE);
1273 eapol_sm_notify_portValid(sm->eapol, TRUE);
1274 }
1275 wpa_sm_set_state(sm, WPA_GROUP_HANDSHAKE);
1276
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001277 if (sm->group_cipher == WPA_CIPHER_GTK_NOT_USED) {
1278 wpa_supplicant_key_neg_complete(sm, sm->bssid,
1279 key_info & WPA_KEY_INFO_SECURE);
1280 } else if (ie.gtk &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001281 wpa_supplicant_pairwise_gtk(sm, key,
1282 ie.gtk, ie.gtk_len, key_info) < 0) {
1283 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1284 "RSN: Failed to configure GTK");
1285 goto failed;
1286 }
1287
1288 if (ieee80211w_set_keys(sm, &ie) < 0) {
1289 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1290 "RSN: Failed to configure IGTK");
1291 goto failed;
1292 }
1293
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001294 if (ie.gtk)
1295 wpa_sm_set_rekey_offload(sm);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001296
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001297 if (sm->proto == WPA_PROTO_RSN && wpa_key_mgmt_suite_b(sm->key_mgmt)) {
1298 struct rsn_pmksa_cache_entry *sa;
1299
1300 sa = pmksa_cache_add(sm->pmksa, sm->pmk, sm->pmk_len,
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001301 sm->ptk.kck, sm->ptk.kck_len,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001302 sm->bssid, sm->own_addr,
1303 sm->network_ctx, sm->key_mgmt);
1304 if (!sm->cur_pmksa)
1305 sm->cur_pmksa = sa;
1306 }
1307
Dmitry Shmidt4dd28dc2015-03-10 11:21:43 -07001308 sm->msg_3_of_4_ok = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001309 return;
1310
1311failed:
1312 wpa_sm_deauthenticate(sm, WLAN_REASON_UNSPECIFIED);
1313}
1314
1315
1316static int wpa_supplicant_process_1_of_2_rsn(struct wpa_sm *sm,
1317 const u8 *keydata,
1318 size_t keydatalen,
1319 u16 key_info,
1320 struct wpa_gtk_data *gd)
1321{
1322 int maxkeylen;
1323 struct wpa_eapol_ie_parse ie;
1324
1325 wpa_hexdump(MSG_DEBUG, "RSN: msg 1/2 key data", keydata, keydatalen);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001326 if (wpa_supplicant_parse_ies(keydata, keydatalen, &ie) < 0)
1327 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001328 if (ie.gtk && !(key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
1329 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1330 "WPA: GTK IE in unencrypted key data");
1331 return -1;
1332 }
1333 if (ie.gtk == NULL) {
1334 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1335 "WPA: No GTK IE in Group Key msg 1/2");
1336 return -1;
1337 }
1338 maxkeylen = gd->gtk_len = ie.gtk_len - 2;
1339
1340 if (wpa_supplicant_check_group_cipher(sm, sm->group_cipher,
1341 gd->gtk_len, maxkeylen,
1342 &gd->key_rsc_len, &gd->alg))
1343 return -1;
1344
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001345 wpa_hexdump_key(MSG_DEBUG, "RSN: received GTK in group key handshake",
1346 ie.gtk, ie.gtk_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001347 gd->keyidx = ie.gtk[0] & 0x3;
1348 gd->tx = wpa_supplicant_gtk_tx_bit_workaround(sm,
1349 !!(ie.gtk[0] & BIT(2)));
1350 if (ie.gtk_len - 2 > sizeof(gd->gtk)) {
1351 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1352 "RSN: Too long GTK in GTK IE (len=%lu)",
1353 (unsigned long) ie.gtk_len - 2);
1354 return -1;
1355 }
1356 os_memcpy(gd->gtk, ie.gtk + 2, ie.gtk_len - 2);
1357
1358 if (ieee80211w_set_keys(sm, &ie) < 0)
1359 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1360 "RSN: Failed to configure IGTK");
1361
1362 return 0;
1363}
1364
1365
1366static int wpa_supplicant_process_1_of_2_wpa(struct wpa_sm *sm,
1367 const struct wpa_eapol_key *key,
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001368 const u8 *key_data,
1369 size_t key_data_len, u16 key_info,
1370 u16 ver, struct wpa_gtk_data *gd)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001371{
1372 size_t maxkeylen;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001373 u16 gtk_len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001374
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001375 gtk_len = WPA_GET_BE16(key->key_length);
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001376 maxkeylen = key_data_len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001377 if (ver == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
1378 if (maxkeylen < 8) {
1379 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1380 "WPA: Too short maxkeylen (%lu)",
1381 (unsigned long) maxkeylen);
1382 return -1;
1383 }
1384 maxkeylen -= 8;
1385 }
1386
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001387 if (gtk_len > maxkeylen ||
1388 wpa_supplicant_check_group_cipher(sm, sm->group_cipher,
1389 gtk_len, maxkeylen,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001390 &gd->key_rsc_len, &gd->alg))
1391 return -1;
1392
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001393 gd->gtk_len = gtk_len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001394 gd->keyidx = (key_info & WPA_KEY_INFO_KEY_INDEX_MASK) >>
1395 WPA_KEY_INFO_KEY_INDEX_SHIFT;
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001396 if (ver == WPA_KEY_INFO_TYPE_HMAC_MD5_RC4 && sm->ptk.kek_len == 16) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001397#ifdef CONFIG_NO_RC4
1398 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1399 "WPA: RC4 not supported in the build");
1400 return -1;
1401#else /* CONFIG_NO_RC4 */
Dmitry Shmidt61593f02014-04-21 16:27:35 -07001402 u8 ek[32];
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001403 if (key_data_len > sizeof(gd->gtk)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001404 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1405 "WPA: RC4 key data too long (%lu)",
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001406 (unsigned long) key_data_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001407 return -1;
1408 }
Dmitry Shmidt61593f02014-04-21 16:27:35 -07001409 os_memcpy(ek, key->key_iv, 16);
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001410 os_memcpy(ek + 16, sm->ptk.kek, sm->ptk.kek_len);
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001411 os_memcpy(gd->gtk, key_data, key_data_len);
1412 if (rc4_skip(ek, 32, 256, gd->gtk, key_data_len)) {
Dmitry Shmidt61593f02014-04-21 16:27:35 -07001413 os_memset(ek, 0, sizeof(ek));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001414 wpa_msg(sm->ctx->msg_ctx, MSG_ERROR,
1415 "WPA: RC4 failed");
1416 return -1;
1417 }
Dmitry Shmidt61593f02014-04-21 16:27:35 -07001418 os_memset(ek, 0, sizeof(ek));
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001419#endif /* CONFIG_NO_RC4 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001420 } else if (ver == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001421 if (maxkeylen % 8) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001422 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1423 "WPA: Unsupported AES-WRAP len %lu",
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001424 (unsigned long) maxkeylen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001425 return -1;
1426 }
1427 if (maxkeylen > sizeof(gd->gtk)) {
1428 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1429 "WPA: AES-WRAP key data "
1430 "too long (keydatalen=%lu maxkeylen=%lu)",
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001431 (unsigned long) key_data_len,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001432 (unsigned long) maxkeylen);
1433 return -1;
1434 }
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001435 if (aes_unwrap(sm->ptk.kek, sm->ptk.kek_len, maxkeylen / 8,
1436 key_data, gd->gtk)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001437 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1438 "WPA: AES unwrap failed - could not decrypt "
1439 "GTK");
1440 return -1;
1441 }
1442 } else {
1443 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1444 "WPA: Unsupported key_info type %d", ver);
1445 return -1;
1446 }
1447 gd->tx = wpa_supplicant_gtk_tx_bit_workaround(
1448 sm, !!(key_info & WPA_KEY_INFO_TXRX));
1449 return 0;
1450}
1451
1452
1453static int wpa_supplicant_send_2_of_2(struct wpa_sm *sm,
1454 const struct wpa_eapol_key *key,
1455 int ver, u16 key_info)
1456{
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001457 size_t mic_len, hdrlen, rlen;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001458 struct wpa_eapol_key *reply;
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001459 struct wpa_eapol_key_192 *reply192;
1460 u8 *rbuf, *key_mic;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001461
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001462 mic_len = wpa_mic_len(sm->key_mgmt);
1463 hdrlen = mic_len == 24 ? sizeof(*reply192) : sizeof(*reply);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001464 rbuf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_KEY, NULL,
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001465 hdrlen, &rlen, (void *) &reply);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001466 if (rbuf == NULL)
1467 return -1;
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001468 reply192 = (struct wpa_eapol_key_192 *) reply;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001469
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001470 reply->type = (sm->proto == WPA_PROTO_RSN ||
1471 sm->proto == WPA_PROTO_OSEN) ?
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001472 EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA;
1473 key_info &= WPA_KEY_INFO_KEY_INDEX_MASK;
1474 key_info |= ver | WPA_KEY_INFO_MIC | WPA_KEY_INFO_SECURE;
1475 WPA_PUT_BE16(reply->key_info, key_info);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001476 if (sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001477 WPA_PUT_BE16(reply->key_length, 0);
1478 else
1479 os_memcpy(reply->key_length, key->key_length, 2);
1480 os_memcpy(reply->replay_counter, key->replay_counter,
1481 WPA_REPLAY_COUNTER_LEN);
1482
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001483 key_mic = reply192->key_mic; /* same offset for reply and reply192 */
1484 if (mic_len == 24)
1485 WPA_PUT_BE16(reply192->key_data_length, 0);
1486 else
1487 WPA_PUT_BE16(reply->key_data_length, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001488
1489 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Sending EAPOL-Key 2/2");
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001490 return wpa_eapol_key_send(sm, sm->ptk.kck, sm->ptk.kck_len, ver,
1491 sm->bssid, ETH_P_EAPOL, rbuf, rlen, key_mic);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001492}
1493
1494
1495static void wpa_supplicant_process_1_of_2(struct wpa_sm *sm,
1496 const unsigned char *src_addr,
1497 const struct wpa_eapol_key *key,
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001498 const u8 *key_data,
1499 size_t key_data_len, u16 ver)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001500{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001501 u16 key_info;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001502 int rekey, ret;
1503 struct wpa_gtk_data gd;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001504 const u8 *key_rsc;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001505
Dmitry Shmidt4dd28dc2015-03-10 11:21:43 -07001506 if (!sm->msg_3_of_4_ok) {
1507 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1508 "WPA: Group Key Handshake started prior to completion of 4-way handshake");
1509 goto failed;
1510 }
1511
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001512 os_memset(&gd, 0, sizeof(gd));
1513
1514 rekey = wpa_sm_get_state(sm) == WPA_COMPLETED;
1515 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: RX message 1 of Group Key "
1516 "Handshake from " MACSTR " (ver=%d)", MAC2STR(src_addr), ver);
1517
1518 key_info = WPA_GET_BE16(key->key_info);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001519
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001520 if (sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN) {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001521 ret = wpa_supplicant_process_1_of_2_rsn(sm, key_data,
1522 key_data_len, key_info,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001523 &gd);
1524 } else {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001525 ret = wpa_supplicant_process_1_of_2_wpa(sm, key, key_data,
1526 key_data_len,
1527 key_info, ver, &gd);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001528 }
1529
1530 wpa_sm_set_state(sm, WPA_GROUP_HANDSHAKE);
1531
1532 if (ret)
1533 goto failed;
1534
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001535 key_rsc = key->key_rsc;
1536 if (wpa_supplicant_rsc_relaxation(sm, key->key_rsc))
1537 key_rsc = null_rsc;
1538
1539 if (wpa_supplicant_install_gtk(sm, &gd, key_rsc) ||
1540 wpa_supplicant_send_2_of_2(sm, key, ver, key_info) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001541 goto failed;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001542 os_memset(&gd, 0, sizeof(gd));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001543
1544 if (rekey) {
1545 wpa_msg(sm->ctx->msg_ctx, MSG_INFO, "WPA: Group rekeying "
1546 "completed with " MACSTR " [GTK=%s]",
1547 MAC2STR(sm->bssid), wpa_cipher_txt(sm->group_cipher));
1548 wpa_sm_cancel_auth_timeout(sm);
1549 wpa_sm_set_state(sm, WPA_COMPLETED);
1550 } else {
1551 wpa_supplicant_key_neg_complete(sm, sm->bssid,
1552 key_info &
1553 WPA_KEY_INFO_SECURE);
1554 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001555
1556 wpa_sm_set_rekey_offload(sm);
1557
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001558 return;
1559
1560failed:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001561 os_memset(&gd, 0, sizeof(gd));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001562 wpa_sm_deauthenticate(sm, WLAN_REASON_UNSPECIFIED);
1563}
1564
1565
1566static int wpa_supplicant_verify_eapol_key_mic(struct wpa_sm *sm,
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001567 struct wpa_eapol_key_192 *key,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001568 u16 ver,
1569 const u8 *buf, size_t len)
1570{
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001571 u8 mic[WPA_EAPOL_KEY_MIC_MAX_LEN];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001572 int ok = 0;
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001573 size_t mic_len = wpa_mic_len(sm->key_mgmt);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001574
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001575 os_memcpy(mic, key->key_mic, mic_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001576 if (sm->tptk_set) {
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001577 os_memset(key->key_mic, 0, mic_len);
1578 wpa_eapol_key_mic(sm->tptk.kck, sm->tptk.kck_len, sm->key_mgmt,
1579 ver, buf, len, key->key_mic);
1580 if (os_memcmp_const(mic, key->key_mic, mic_len) != 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001581 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1582 "WPA: Invalid EAPOL-Key MIC "
1583 "when using TPTK - ignoring TPTK");
1584 } else {
1585 ok = 1;
1586 sm->tptk_set = 0;
1587 sm->ptk_set = 1;
1588 os_memcpy(&sm->ptk, &sm->tptk, sizeof(sm->ptk));
Dmitry Shmidt61593f02014-04-21 16:27:35 -07001589 os_memset(&sm->tptk, 0, sizeof(sm->tptk));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001590 }
1591 }
1592
1593 if (!ok && sm->ptk_set) {
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001594 os_memset(key->key_mic, 0, mic_len);
1595 wpa_eapol_key_mic(sm->ptk.kck, sm->ptk.kck_len, sm->key_mgmt,
1596 ver, buf, len, key->key_mic);
1597 if (os_memcmp_const(mic, key->key_mic, mic_len) != 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001598 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1599 "WPA: Invalid EAPOL-Key MIC - "
1600 "dropping packet");
1601 return -1;
1602 }
1603 ok = 1;
1604 }
1605
1606 if (!ok) {
1607 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1608 "WPA: Could not verify EAPOL-Key MIC - "
1609 "dropping packet");
1610 return -1;
1611 }
1612
1613 os_memcpy(sm->rx_replay_counter, key->replay_counter,
1614 WPA_REPLAY_COUNTER_LEN);
1615 sm->rx_replay_counter_set = 1;
1616 return 0;
1617}
1618
1619
1620/* Decrypt RSN EAPOL-Key key data (RC4 or AES-WRAP) */
1621static int wpa_supplicant_decrypt_key_data(struct wpa_sm *sm,
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001622 struct wpa_eapol_key *key, u16 ver,
1623 u8 *key_data, size_t *key_data_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001624{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001625 wpa_hexdump(MSG_DEBUG, "RSN: encrypted key data",
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001626 key_data, *key_data_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001627 if (!sm->ptk_set) {
1628 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1629 "WPA: PTK not available, cannot decrypt EAPOL-Key Key "
1630 "Data");
1631 return -1;
1632 }
1633
1634 /* Decrypt key data here so that this operation does not need
1635 * to be implemented separately for each message type. */
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001636 if (ver == WPA_KEY_INFO_TYPE_HMAC_MD5_RC4 && sm->ptk.kek_len == 16) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001637#ifdef CONFIG_NO_RC4
1638 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1639 "WPA: RC4 not supported in the build");
1640 return -1;
1641#else /* CONFIG_NO_RC4 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001642 u8 ek[32];
1643 os_memcpy(ek, key->key_iv, 16);
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001644 os_memcpy(ek + 16, sm->ptk.kek, sm->ptk.kek_len);
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001645 if (rc4_skip(ek, 32, 256, key_data, *key_data_len)) {
Dmitry Shmidt61593f02014-04-21 16:27:35 -07001646 os_memset(ek, 0, sizeof(ek));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001647 wpa_msg(sm->ctx->msg_ctx, MSG_ERROR,
1648 "WPA: RC4 failed");
1649 return -1;
1650 }
Dmitry Shmidt61593f02014-04-21 16:27:35 -07001651 os_memset(ek, 0, sizeof(ek));
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001652#endif /* CONFIG_NO_RC4 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001653 } else if (ver == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES ||
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001654 ver == WPA_KEY_INFO_TYPE_AES_128_CMAC ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001655 sm->key_mgmt == WPA_KEY_MGMT_OSEN ||
1656 wpa_key_mgmt_suite_b(sm->key_mgmt)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001657 u8 *buf;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001658 if (*key_data_len < 8 || *key_data_len % 8) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001659 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001660 "WPA: Unsupported AES-WRAP len %u",
1661 (unsigned int) *key_data_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001662 return -1;
1663 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001664 *key_data_len -= 8; /* AES-WRAP adds 8 bytes */
1665 buf = os_malloc(*key_data_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001666 if (buf == NULL) {
1667 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1668 "WPA: No memory for AES-UNWRAP buffer");
1669 return -1;
1670 }
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001671 if (aes_unwrap(sm->ptk.kek, sm->ptk.kek_len, *key_data_len / 8,
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001672 key_data, buf)) {
Dmitry Shmidt7d56b752015-12-22 10:59:44 -08001673 bin_clear_free(buf, *key_data_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001674 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1675 "WPA: AES unwrap failed - "
1676 "could not decrypt EAPOL-Key key data");
1677 return -1;
1678 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001679 os_memcpy(key_data, buf, *key_data_len);
Dmitry Shmidt7d56b752015-12-22 10:59:44 -08001680 bin_clear_free(buf, *key_data_len);
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001681 WPA_PUT_BE16(key->key_data_length, *key_data_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001682 } else {
1683 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1684 "WPA: Unsupported key_info type %d", ver);
1685 return -1;
1686 }
1687 wpa_hexdump_key(MSG_DEBUG, "WPA: decrypted EAPOL-Key key data",
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001688 key_data, *key_data_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001689 return 0;
1690}
1691
1692
1693/**
1694 * wpa_sm_aborted_cached - Notify WPA that PMKSA caching was aborted
1695 * @sm: Pointer to WPA state machine data from wpa_sm_init()
1696 */
1697void wpa_sm_aborted_cached(struct wpa_sm *sm)
1698{
1699 if (sm && sm->cur_pmksa) {
1700 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1701 "RSN: Cancelling PMKSA caching attempt");
1702 sm->cur_pmksa = NULL;
1703 }
1704}
1705
1706
1707static void wpa_eapol_key_dump(struct wpa_sm *sm,
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001708 const struct wpa_eapol_key *key,
1709 unsigned int key_data_len,
1710 const u8 *mic, unsigned int mic_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001711{
1712#ifndef CONFIG_NO_STDOUT_DEBUG
1713 u16 key_info = WPA_GET_BE16(key->key_info);
1714
1715 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, " EAPOL-Key type=%d", key->type);
1716 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1717 " key_info 0x%x (ver=%d keyidx=%d rsvd=%d %s%s%s%s%s%s%s%s)",
1718 key_info, key_info & WPA_KEY_INFO_TYPE_MASK,
1719 (key_info & WPA_KEY_INFO_KEY_INDEX_MASK) >>
1720 WPA_KEY_INFO_KEY_INDEX_SHIFT,
1721 (key_info & (BIT(13) | BIT(14) | BIT(15))) >> 13,
1722 key_info & WPA_KEY_INFO_KEY_TYPE ? "Pairwise" : "Group",
1723 key_info & WPA_KEY_INFO_INSTALL ? " Install" : "",
1724 key_info & WPA_KEY_INFO_ACK ? " Ack" : "",
1725 key_info & WPA_KEY_INFO_MIC ? " MIC" : "",
1726 key_info & WPA_KEY_INFO_SECURE ? " Secure" : "",
1727 key_info & WPA_KEY_INFO_ERROR ? " Error" : "",
1728 key_info & WPA_KEY_INFO_REQUEST ? " Request" : "",
1729 key_info & WPA_KEY_INFO_ENCR_KEY_DATA ? " Encr" : "");
1730 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1731 " key_length=%u key_data_length=%u",
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001732 WPA_GET_BE16(key->key_length), key_data_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001733 wpa_hexdump(MSG_DEBUG, " replay_counter",
1734 key->replay_counter, WPA_REPLAY_COUNTER_LEN);
1735 wpa_hexdump(MSG_DEBUG, " key_nonce", key->key_nonce, WPA_NONCE_LEN);
1736 wpa_hexdump(MSG_DEBUG, " key_iv", key->key_iv, 16);
1737 wpa_hexdump(MSG_DEBUG, " key_rsc", key->key_rsc, 8);
1738 wpa_hexdump(MSG_DEBUG, " key_id (reserved)", key->key_id, 8);
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001739 wpa_hexdump(MSG_DEBUG, " key_mic", mic, mic_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001740#endif /* CONFIG_NO_STDOUT_DEBUG */
1741}
1742
1743
1744/**
1745 * wpa_sm_rx_eapol - Process received WPA EAPOL frames
1746 * @sm: Pointer to WPA state machine data from wpa_sm_init()
1747 * @src_addr: Source MAC address of the EAPOL packet
1748 * @buf: Pointer to the beginning of the EAPOL data (EAPOL header)
1749 * @len: Length of the EAPOL frame
1750 * Returns: 1 = WPA EAPOL-Key processed, 0 = not a WPA EAPOL-Key, -1 failure
1751 *
1752 * This function is called for each received EAPOL frame. Other than EAPOL-Key
1753 * frames can be skipped if filtering is done elsewhere. wpa_sm_rx_eapol() is
1754 * only processing WPA and WPA2 EAPOL-Key frames.
1755 *
1756 * The received EAPOL-Key packets are validated and valid packets are replied
1757 * to. In addition, key material (PTK, GTK) is configured at the end of a
1758 * successful key handshake.
1759 */
1760int wpa_sm_rx_eapol(struct wpa_sm *sm, const u8 *src_addr,
1761 const u8 *buf, size_t len)
1762{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001763 size_t plen, data_len, key_data_len;
1764 const struct ieee802_1x_hdr *hdr;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001765 struct wpa_eapol_key *key;
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001766 struct wpa_eapol_key_192 *key192;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001767 u16 key_info, ver;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001768 u8 *tmp = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001769 int ret = -1;
1770 struct wpa_peerkey *peerkey = NULL;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001771 u8 *key_data;
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001772 size_t mic_len, keyhdrlen;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001773
1774#ifdef CONFIG_IEEE80211R
1775 sm->ft_completed = 0;
1776#endif /* CONFIG_IEEE80211R */
1777
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001778 mic_len = wpa_mic_len(sm->key_mgmt);
1779 keyhdrlen = mic_len == 24 ? sizeof(*key192) : sizeof(*key);
1780
1781 if (len < sizeof(*hdr) + keyhdrlen) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001782 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1783 "WPA: EAPOL frame too short to be a WPA "
1784 "EAPOL-Key (len %lu, expecting at least %lu)",
1785 (unsigned long) len,
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001786 (unsigned long) sizeof(*hdr) + keyhdrlen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001787 return 0;
1788 }
1789
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001790 hdr = (const struct ieee802_1x_hdr *) buf;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001791 plen = be_to_host16(hdr->length);
1792 data_len = plen + sizeof(*hdr);
1793 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1794 "IEEE 802.1X RX: version=%d type=%d length=%lu",
1795 hdr->version, hdr->type, (unsigned long) plen);
1796
1797 if (hdr->version < EAPOL_VERSION) {
1798 /* TODO: backwards compatibility */
1799 }
1800 if (hdr->type != IEEE802_1X_TYPE_EAPOL_KEY) {
1801 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1802 "WPA: EAPOL frame (type %u) discarded, "
1803 "not a Key frame", hdr->type);
1804 ret = 0;
1805 goto out;
1806 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001807 wpa_hexdump(MSG_MSGDUMP, "WPA: RX EAPOL-Key", buf, len);
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001808 if (plen > len - sizeof(*hdr) || plen < keyhdrlen) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001809 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1810 "WPA: EAPOL frame payload size %lu "
1811 "invalid (frame size %lu)",
1812 (unsigned long) plen, (unsigned long) len);
1813 ret = 0;
1814 goto out;
1815 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001816 if (data_len < len) {
1817 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1818 "WPA: ignoring %lu bytes after the IEEE 802.1X data",
1819 (unsigned long) len - data_len);
1820 }
1821
1822 /*
1823 * Make a copy of the frame since we need to modify the buffer during
1824 * MAC validation and Key Data decryption.
1825 */
1826 tmp = os_malloc(data_len);
1827 if (tmp == NULL)
1828 goto out;
1829 os_memcpy(tmp, buf, data_len);
1830 key = (struct wpa_eapol_key *) (tmp + sizeof(struct ieee802_1x_hdr));
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001831 key192 = (struct wpa_eapol_key_192 *)
1832 (tmp + sizeof(struct ieee802_1x_hdr));
1833 if (mic_len == 24)
1834 key_data = (u8 *) (key192 + 1);
1835 else
1836 key_data = (u8 *) (key + 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001837
1838 if (key->type != EAPOL_KEY_TYPE_WPA && key->type != EAPOL_KEY_TYPE_RSN)
1839 {
1840 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1841 "WPA: EAPOL-Key type (%d) unknown, discarded",
1842 key->type);
1843 ret = 0;
1844 goto out;
1845 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001846
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001847 if (mic_len == 24)
1848 key_data_len = WPA_GET_BE16(key192->key_data_length);
1849 else
1850 key_data_len = WPA_GET_BE16(key->key_data_length);
1851 wpa_eapol_key_dump(sm, key, key_data_len, key192->key_mic, mic_len);
1852
1853 if (key_data_len > plen - keyhdrlen) {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001854 wpa_msg(sm->ctx->msg_ctx, MSG_INFO, "WPA: Invalid EAPOL-Key "
1855 "frame - key_data overflow (%u > %u)",
1856 (unsigned int) key_data_len,
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001857 (unsigned int) (plen - keyhdrlen));
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001858 goto out;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001859 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001860
1861 eapol_sm_notify_lower_layer_success(sm->eapol, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001862 key_info = WPA_GET_BE16(key->key_info);
1863 ver = key_info & WPA_KEY_INFO_TYPE_MASK;
1864 if (ver != WPA_KEY_INFO_TYPE_HMAC_MD5_RC4 &&
1865#if defined(CONFIG_IEEE80211R) || defined(CONFIG_IEEE80211W)
1866 ver != WPA_KEY_INFO_TYPE_AES_128_CMAC &&
1867#endif /* CONFIG_IEEE80211R || CONFIG_IEEE80211W */
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001868 ver != WPA_KEY_INFO_TYPE_HMAC_SHA1_AES &&
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001869 !wpa_key_mgmt_suite_b(sm->key_mgmt) &&
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001870 sm->key_mgmt != WPA_KEY_MGMT_OSEN) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001871 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1872 "WPA: Unsupported EAPOL-Key descriptor version %d",
1873 ver);
1874 goto out;
1875 }
1876
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001877 if (sm->key_mgmt == WPA_KEY_MGMT_OSEN &&
1878 ver != WPA_KEY_INFO_TYPE_AKM_DEFINED) {
1879 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1880 "OSEN: Unsupported EAPOL-Key descriptor version %d",
1881 ver);
1882 goto out;
1883 }
1884
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001885 if (wpa_key_mgmt_suite_b(sm->key_mgmt) &&
1886 ver != WPA_KEY_INFO_TYPE_AKM_DEFINED) {
1887 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1888 "RSN: Unsupported EAPOL-Key descriptor version %d (expected AKM defined = 0)",
1889 ver);
1890 goto out;
1891 }
1892
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001893#ifdef CONFIG_IEEE80211R
1894 if (wpa_key_mgmt_ft(sm->key_mgmt)) {
1895 /* IEEE 802.11r uses a new key_info type (AES-128-CMAC). */
1896 if (ver != WPA_KEY_INFO_TYPE_AES_128_CMAC) {
1897 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1898 "FT: AP did not use AES-128-CMAC");
1899 goto out;
1900 }
1901 } else
1902#endif /* CONFIG_IEEE80211R */
1903#ifdef CONFIG_IEEE80211W
1904 if (wpa_key_mgmt_sha256(sm->key_mgmt)) {
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001905 if (ver != WPA_KEY_INFO_TYPE_AES_128_CMAC &&
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001906 sm->key_mgmt != WPA_KEY_MGMT_OSEN &&
1907 !wpa_key_mgmt_suite_b(sm->key_mgmt)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001908 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1909 "WPA: AP did not use the "
1910 "negotiated AES-128-CMAC");
1911 goto out;
1912 }
1913 } else
1914#endif /* CONFIG_IEEE80211W */
1915 if (sm->pairwise_cipher == WPA_CIPHER_CCMP &&
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001916 !wpa_key_mgmt_suite_b(sm->key_mgmt) &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001917 ver != WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
1918 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1919 "WPA: CCMP is used, but EAPOL-Key "
1920 "descriptor version (%d) is not 2", ver);
1921 if (sm->group_cipher != WPA_CIPHER_CCMP &&
1922 !(key_info & WPA_KEY_INFO_KEY_TYPE)) {
1923 /* Earlier versions of IEEE 802.11i did not explicitly
1924 * require version 2 descriptor for all EAPOL-Key
1925 * packets, so allow group keys to use version 1 if
1926 * CCMP is not used for them. */
1927 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1928 "WPA: Backwards compatibility: allow invalid "
1929 "version for non-CCMP group keys");
Jouni Malinen658fb4a2014-11-14 20:57:05 +02001930 } else if (ver == WPA_KEY_INFO_TYPE_AES_128_CMAC) {
1931 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1932 "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 -07001933 } else
1934 goto out;
Dmitry Shmidt71757432014-06-02 13:50:35 -07001935 } else if (sm->pairwise_cipher == WPA_CIPHER_GCMP &&
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001936 !wpa_key_mgmt_suite_b(sm->key_mgmt) &&
Dmitry Shmidt71757432014-06-02 13:50:35 -07001937 ver != WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001938 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1939 "WPA: GCMP is used, but EAPOL-Key "
1940 "descriptor version (%d) is not 2", ver);
1941 goto out;
1942 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001943
1944#ifdef CONFIG_PEERKEY
1945 for (peerkey = sm->peerkey; peerkey; peerkey = peerkey->next) {
1946 if (os_memcmp(peerkey->addr, src_addr, ETH_ALEN) == 0)
1947 break;
1948 }
1949
1950 if (!(key_info & WPA_KEY_INFO_SMK_MESSAGE) && peerkey) {
1951 if (!peerkey->initiator && peerkey->replay_counter_set &&
1952 os_memcmp(key->replay_counter, peerkey->replay_counter,
1953 WPA_REPLAY_COUNTER_LEN) <= 0) {
1954 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1955 "RSN: EAPOL-Key Replay Counter did not "
1956 "increase (STK) - dropping packet");
1957 goto out;
1958 } else if (peerkey->initiator) {
1959 u8 _tmp[WPA_REPLAY_COUNTER_LEN];
1960 os_memcpy(_tmp, key->replay_counter,
1961 WPA_REPLAY_COUNTER_LEN);
1962 inc_byte_array(_tmp, WPA_REPLAY_COUNTER_LEN);
1963 if (os_memcmp(_tmp, peerkey->replay_counter,
1964 WPA_REPLAY_COUNTER_LEN) != 0) {
1965 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1966 "RSN: EAPOL-Key Replay "
1967 "Counter did not match (STK) - "
1968 "dropping packet");
1969 goto out;
1970 }
1971 }
1972 }
1973
1974 if (peerkey && peerkey->initiator && (key_info & WPA_KEY_INFO_ACK)) {
1975 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1976 "RSN: Ack bit in key_info from STK peer");
1977 goto out;
1978 }
1979#endif /* CONFIG_PEERKEY */
1980
1981 if (!peerkey && sm->rx_replay_counter_set &&
1982 os_memcmp(key->replay_counter, sm->rx_replay_counter,
1983 WPA_REPLAY_COUNTER_LEN) <= 0) {
1984 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1985 "WPA: EAPOL-Key Replay Counter did not increase - "
1986 "dropping packet");
1987 goto out;
1988 }
1989
1990 if (!(key_info & (WPA_KEY_INFO_ACK | WPA_KEY_INFO_SMK_MESSAGE))
1991#ifdef CONFIG_PEERKEY
1992 && (peerkey == NULL || !peerkey->initiator)
1993#endif /* CONFIG_PEERKEY */
1994 ) {
1995 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1996 "WPA: No Ack bit in key_info");
1997 goto out;
1998 }
1999
2000 if (key_info & WPA_KEY_INFO_REQUEST) {
2001 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2002 "WPA: EAPOL-Key with Request bit - dropped");
2003 goto out;
2004 }
2005
2006 if ((key_info & WPA_KEY_INFO_MIC) && !peerkey &&
Dmitry Shmidt807291d2015-01-27 13:40:23 -08002007 wpa_supplicant_verify_eapol_key_mic(sm, key192, ver, tmp, data_len))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002008 goto out;
2009
2010#ifdef CONFIG_PEERKEY
2011 if ((key_info & WPA_KEY_INFO_MIC) && peerkey &&
Dmitry Shmidt807291d2015-01-27 13:40:23 -08002012 peerkey_verify_eapol_key_mic(sm, peerkey, key192, ver, tmp,
2013 data_len))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002014 goto out;
2015#endif /* CONFIG_PEERKEY */
2016
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002017 if ((sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN) &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002018 (key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002019 if (wpa_supplicant_decrypt_key_data(sm, key, ver, key_data,
2020 &key_data_len))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002021 goto out;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002022 }
2023
2024 if (key_info & WPA_KEY_INFO_KEY_TYPE) {
2025 if (key_info & WPA_KEY_INFO_KEY_INDEX_MASK) {
2026 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
2027 "WPA: Ignored EAPOL-Key (Pairwise) with "
2028 "non-zero key index");
2029 goto out;
2030 }
2031 if (peerkey) {
2032 /* PeerKey 4-Way Handshake */
Dmitry Shmidtc2817022014-07-02 10:32:10 -07002033 peerkey_rx_eapol_4way(sm, peerkey, key, key_info, ver,
2034 key_data, key_data_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002035 } else if (key_info & WPA_KEY_INFO_MIC) {
2036 /* 3/4 4-Way Handshake */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002037 wpa_supplicant_process_3_of_4(sm, key, ver, key_data,
2038 key_data_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002039 } else {
2040 /* 1/4 4-Way Handshake */
2041 wpa_supplicant_process_1_of_4(sm, src_addr, key,
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002042 ver, key_data,
2043 key_data_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002044 }
2045 } else if (key_info & WPA_KEY_INFO_SMK_MESSAGE) {
2046 /* PeerKey SMK Handshake */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002047 peerkey_rx_eapol_smk(sm, src_addr, key, key_data_len, key_info,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002048 ver);
2049 } else {
2050 if (key_info & WPA_KEY_INFO_MIC) {
2051 /* 1/2 Group Key Handshake */
2052 wpa_supplicant_process_1_of_2(sm, src_addr, key,
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002053 key_data, key_data_len,
2054 ver);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002055 } else {
2056 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
2057 "WPA: EAPOL-Key (Group) without Mic bit - "
2058 "dropped");
2059 }
2060 }
2061
2062 ret = 1;
2063
2064out:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002065 bin_clear_free(tmp, data_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002066 return ret;
2067}
2068
2069
2070#ifdef CONFIG_CTRL_IFACE
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002071static u32 wpa_key_mgmt_suite(struct wpa_sm *sm)
2072{
2073 switch (sm->key_mgmt) {
2074 case WPA_KEY_MGMT_IEEE8021X:
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002075 return ((sm->proto == WPA_PROTO_RSN ||
2076 sm->proto == WPA_PROTO_OSEN) ?
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002077 RSN_AUTH_KEY_MGMT_UNSPEC_802_1X :
2078 WPA_AUTH_KEY_MGMT_UNSPEC_802_1X);
2079 case WPA_KEY_MGMT_PSK:
2080 return (sm->proto == WPA_PROTO_RSN ?
2081 RSN_AUTH_KEY_MGMT_PSK_OVER_802_1X :
2082 WPA_AUTH_KEY_MGMT_PSK_OVER_802_1X);
2083#ifdef CONFIG_IEEE80211R
2084 case WPA_KEY_MGMT_FT_IEEE8021X:
2085 return RSN_AUTH_KEY_MGMT_FT_802_1X;
2086 case WPA_KEY_MGMT_FT_PSK:
2087 return RSN_AUTH_KEY_MGMT_FT_PSK;
2088#endif /* CONFIG_IEEE80211R */
2089#ifdef CONFIG_IEEE80211W
2090 case WPA_KEY_MGMT_IEEE8021X_SHA256:
2091 return RSN_AUTH_KEY_MGMT_802_1X_SHA256;
2092 case WPA_KEY_MGMT_PSK_SHA256:
2093 return RSN_AUTH_KEY_MGMT_PSK_SHA256;
2094#endif /* CONFIG_IEEE80211W */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002095 case WPA_KEY_MGMT_CCKM:
2096 return (sm->proto == WPA_PROTO_RSN ?
2097 RSN_AUTH_KEY_MGMT_CCKM:
2098 WPA_AUTH_KEY_MGMT_CCKM);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002099 case WPA_KEY_MGMT_WPA_NONE:
2100 return WPA_AUTH_KEY_MGMT_NONE;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002101 case WPA_KEY_MGMT_IEEE8021X_SUITE_B:
2102 return RSN_AUTH_KEY_MGMT_802_1X_SUITE_B;
Dmitry Shmidt807291d2015-01-27 13:40:23 -08002103 case WPA_KEY_MGMT_IEEE8021X_SUITE_B_192:
2104 return RSN_AUTH_KEY_MGMT_802_1X_SUITE_B_192;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002105 default:
2106 return 0;
2107 }
2108}
2109
2110
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002111#define RSN_SUITE "%02x-%02x-%02x-%d"
2112#define RSN_SUITE_ARG(s) \
2113((s) >> 24) & 0xff, ((s) >> 16) & 0xff, ((s) >> 8) & 0xff, (s) & 0xff
2114
2115/**
2116 * wpa_sm_get_mib - Dump text list of MIB entries
2117 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2118 * @buf: Buffer for the list
2119 * @buflen: Length of the buffer
2120 * Returns: Number of bytes written to buffer
2121 *
2122 * This function is used fetch dot11 MIB variables.
2123 */
2124int wpa_sm_get_mib(struct wpa_sm *sm, char *buf, size_t buflen)
2125{
2126 char pmkid_txt[PMKID_LEN * 2 + 1];
2127 int rsna, ret;
2128 size_t len;
2129
2130 if (sm->cur_pmksa) {
2131 wpa_snprintf_hex(pmkid_txt, sizeof(pmkid_txt),
2132 sm->cur_pmksa->pmkid, PMKID_LEN);
2133 } else
2134 pmkid_txt[0] = '\0';
2135
2136 if ((wpa_key_mgmt_wpa_psk(sm->key_mgmt) ||
2137 wpa_key_mgmt_wpa_ieee8021x(sm->key_mgmt)) &&
2138 sm->proto == WPA_PROTO_RSN)
2139 rsna = 1;
2140 else
2141 rsna = 0;
2142
2143 ret = os_snprintf(buf, buflen,
2144 "dot11RSNAOptionImplemented=TRUE\n"
2145 "dot11RSNAPreauthenticationImplemented=TRUE\n"
2146 "dot11RSNAEnabled=%s\n"
2147 "dot11RSNAPreauthenticationEnabled=%s\n"
2148 "dot11RSNAConfigVersion=%d\n"
2149 "dot11RSNAConfigPairwiseKeysSupported=5\n"
2150 "dot11RSNAConfigGroupCipherSize=%d\n"
2151 "dot11RSNAConfigPMKLifetime=%d\n"
2152 "dot11RSNAConfigPMKReauthThreshold=%d\n"
2153 "dot11RSNAConfigNumberOfPTKSAReplayCounters=1\n"
2154 "dot11RSNAConfigSATimeout=%d\n",
2155 rsna ? "TRUE" : "FALSE",
2156 rsna ? "TRUE" : "FALSE",
2157 RSN_VERSION,
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07002158 wpa_cipher_key_len(sm->group_cipher) * 8,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002159 sm->dot11RSNAConfigPMKLifetime,
2160 sm->dot11RSNAConfigPMKReauthThreshold,
2161 sm->dot11RSNAConfigSATimeout);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002162 if (os_snprintf_error(buflen, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002163 return 0;
2164 len = ret;
2165
2166 ret = os_snprintf(
2167 buf + len, buflen - len,
2168 "dot11RSNAAuthenticationSuiteSelected=" RSN_SUITE "\n"
2169 "dot11RSNAPairwiseCipherSelected=" RSN_SUITE "\n"
2170 "dot11RSNAGroupCipherSelected=" RSN_SUITE "\n"
2171 "dot11RSNAPMKIDUsed=%s\n"
2172 "dot11RSNAAuthenticationSuiteRequested=" RSN_SUITE "\n"
2173 "dot11RSNAPairwiseCipherRequested=" RSN_SUITE "\n"
2174 "dot11RSNAGroupCipherRequested=" RSN_SUITE "\n"
2175 "dot11RSNAConfigNumberOfGTKSAReplayCounters=0\n"
2176 "dot11RSNA4WayHandshakeFailures=%u\n",
2177 RSN_SUITE_ARG(wpa_key_mgmt_suite(sm)),
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07002178 RSN_SUITE_ARG(wpa_cipher_to_suite(sm->proto,
2179 sm->pairwise_cipher)),
2180 RSN_SUITE_ARG(wpa_cipher_to_suite(sm->proto,
2181 sm->group_cipher)),
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002182 pmkid_txt,
2183 RSN_SUITE_ARG(wpa_key_mgmt_suite(sm)),
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07002184 RSN_SUITE_ARG(wpa_cipher_to_suite(sm->proto,
2185 sm->pairwise_cipher)),
2186 RSN_SUITE_ARG(wpa_cipher_to_suite(sm->proto,
2187 sm->group_cipher)),
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002188 sm->dot11RSNA4WayHandshakeFailures);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002189 if (!os_snprintf_error(buflen - len, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002190 len += ret;
2191
2192 return (int) len;
2193}
2194#endif /* CONFIG_CTRL_IFACE */
2195
2196
2197static void wpa_sm_pmksa_free_cb(struct rsn_pmksa_cache_entry *entry,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002198 void *ctx, enum pmksa_free_reason reason)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002199{
2200 struct wpa_sm *sm = ctx;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002201 int deauth = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002202
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002203 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "RSN: PMKSA cache entry free_cb: "
2204 MACSTR " reason=%d", MAC2STR(entry->aa), reason);
2205
2206 if (sm->cur_pmksa == entry) {
2207 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2208 "RSN: %s current PMKSA entry",
2209 reason == PMKSA_REPLACE ? "replaced" : "removed");
2210 pmksa_cache_clear_current(sm);
2211
2212 /*
2213 * If an entry is simply being replaced, there's no need to
2214 * deauthenticate because it will be immediately re-added.
2215 * This happens when EAP authentication is completed again
2216 * (reauth or failed PMKSA caching attempt).
2217 */
2218 if (reason != PMKSA_REPLACE)
2219 deauth = 1;
2220 }
2221
2222 if (reason == PMKSA_EXPIRE &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002223 (sm->pmk_len == entry->pmk_len &&
2224 os_memcmp(sm->pmk, entry->pmk, sm->pmk_len) == 0)) {
2225 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002226 "RSN: deauthenticating due to expired PMK");
2227 pmksa_cache_clear_current(sm);
2228 deauth = 1;
2229 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002230
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002231 if (deauth) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002232 os_memset(sm->pmk, 0, sizeof(sm->pmk));
2233 wpa_sm_deauthenticate(sm, WLAN_REASON_UNSPECIFIED);
2234 }
2235}
2236
2237
2238/**
2239 * wpa_sm_init - Initialize WPA state machine
2240 * @ctx: Context pointer for callbacks; this needs to be an allocated buffer
2241 * Returns: Pointer to the allocated WPA state machine data
2242 *
2243 * This function is used to allocate a new WPA state machine and the returned
2244 * value is passed to all WPA state machine calls.
2245 */
2246struct wpa_sm * wpa_sm_init(struct wpa_sm_ctx *ctx)
2247{
2248 struct wpa_sm *sm;
2249
2250 sm = os_zalloc(sizeof(*sm));
2251 if (sm == NULL)
2252 return NULL;
2253 dl_list_init(&sm->pmksa_candidates);
2254 sm->renew_snonce = 1;
2255 sm->ctx = ctx;
2256
2257 sm->dot11RSNAConfigPMKLifetime = 43200;
2258 sm->dot11RSNAConfigPMKReauthThreshold = 70;
2259 sm->dot11RSNAConfigSATimeout = 60;
2260
2261 sm->pmksa = pmksa_cache_init(wpa_sm_pmksa_free_cb, sm, sm);
2262 if (sm->pmksa == NULL) {
2263 wpa_msg(sm->ctx->msg_ctx, MSG_ERROR,
2264 "RSN: PMKSA cache initialization failed");
2265 os_free(sm);
2266 return NULL;
2267 }
2268
2269 return sm;
2270}
2271
2272
2273/**
2274 * wpa_sm_deinit - Deinitialize WPA state machine
2275 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2276 */
2277void wpa_sm_deinit(struct wpa_sm *sm)
2278{
2279 if (sm == NULL)
2280 return;
2281 pmksa_cache_deinit(sm->pmksa);
2282 eloop_cancel_timeout(wpa_sm_start_preauth, sm, NULL);
2283 eloop_cancel_timeout(wpa_sm_rekey_ptk, sm, NULL);
2284 os_free(sm->assoc_wpa_ie);
2285 os_free(sm->ap_wpa_ie);
2286 os_free(sm->ap_rsn_ie);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002287 wpa_sm_drop_sa(sm);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002288 os_free(sm->ctx);
2289 peerkey_deinit(sm);
2290#ifdef CONFIG_IEEE80211R
2291 os_free(sm->assoc_resp_ies);
2292#endif /* CONFIG_IEEE80211R */
Dmitry Shmidt55840ad2015-12-14 12:45:46 -08002293#ifdef CONFIG_TESTING_OPTIONS
2294 wpabuf_free(sm->test_assoc_ie);
2295#endif /* CONFIG_TESTING_OPTIONS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002296 os_free(sm);
2297}
2298
2299
2300/**
2301 * wpa_sm_notify_assoc - Notify WPA state machine about association
2302 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2303 * @bssid: The BSSID of the new association
2304 *
2305 * This function is called to let WPA state machine know that the connection
2306 * was established.
2307 */
2308void wpa_sm_notify_assoc(struct wpa_sm *sm, const u8 *bssid)
2309{
2310 int clear_ptk = 1;
2311
2312 if (sm == NULL)
2313 return;
2314
2315 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2316 "WPA: Association event - clear replay counter");
2317 os_memcpy(sm->bssid, bssid, ETH_ALEN);
2318 os_memset(sm->rx_replay_counter, 0, WPA_REPLAY_COUNTER_LEN);
2319 sm->rx_replay_counter_set = 0;
2320 sm->renew_snonce = 1;
2321 if (os_memcmp(sm->preauth_bssid, bssid, ETH_ALEN) == 0)
2322 rsn_preauth_deinit(sm);
2323
2324#ifdef CONFIG_IEEE80211R
2325 if (wpa_ft_is_completed(sm)) {
2326 /*
2327 * Clear portValid to kick EAPOL state machine to re-enter
2328 * AUTHENTICATED state to get the EAPOL port Authorized.
2329 */
2330 eapol_sm_notify_portValid(sm->eapol, FALSE);
2331 wpa_supplicant_key_neg_complete(sm, sm->bssid, 1);
2332
2333 /* Prepare for the next transition */
2334 wpa_ft_prepare_auth_request(sm, NULL);
2335
2336 clear_ptk = 0;
2337 }
2338#endif /* CONFIG_IEEE80211R */
2339
2340 if (clear_ptk) {
2341 /*
2342 * IEEE 802.11, 8.4.10: Delete PTK SA on (re)association if
2343 * this is not part of a Fast BSS Transition.
2344 */
2345 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Clear old PTK");
2346 sm->ptk_set = 0;
Dmitry Shmidt61593f02014-04-21 16:27:35 -07002347 os_memset(&sm->ptk, 0, sizeof(sm->ptk));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002348 sm->tptk_set = 0;
Dmitry Shmidt61593f02014-04-21 16:27:35 -07002349 os_memset(&sm->tptk, 0, sizeof(sm->tptk));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002350 }
2351
2352#ifdef CONFIG_TDLS
2353 wpa_tdls_assoc(sm);
2354#endif /* CONFIG_TDLS */
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002355
2356#ifdef CONFIG_P2P
2357 os_memset(sm->p2p_ip_addr, 0, sizeof(sm->p2p_ip_addr));
2358#endif /* CONFIG_P2P */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002359}
2360
2361
2362/**
2363 * wpa_sm_notify_disassoc - Notify WPA state machine about disassociation
2364 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2365 *
2366 * This function is called to let WPA state machine know that the connection
2367 * was lost. This will abort any existing pre-authentication session.
2368 */
2369void wpa_sm_notify_disassoc(struct wpa_sm *sm)
2370{
Dmitry Shmidt4dd28dc2015-03-10 11:21:43 -07002371 eloop_cancel_timeout(wpa_sm_start_preauth, sm, NULL);
2372 eloop_cancel_timeout(wpa_sm_rekey_ptk, sm, NULL);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002373 peerkey_deinit(sm);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002374 rsn_preauth_deinit(sm);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002375 pmksa_cache_clear_current(sm);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002376 if (wpa_sm_get_state(sm) == WPA_4WAY_HANDSHAKE)
2377 sm->dot11RSNA4WayHandshakeFailures++;
2378#ifdef CONFIG_TDLS
2379 wpa_tdls_disassoc(sm);
2380#endif /* CONFIG_TDLS */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002381
2382 /* Keys are not needed in the WPA state machine anymore */
2383 wpa_sm_drop_sa(sm);
Dmitry Shmidt4dd28dc2015-03-10 11:21:43 -07002384
2385 sm->msg_3_of_4_ok = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002386}
2387
2388
2389/**
2390 * wpa_sm_set_pmk - Set PMK
2391 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2392 * @pmk: The new PMK
2393 * @pmk_len: The length of the new PMK in bytes
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002394 * @bssid: AA to add into PMKSA cache or %NULL to not cache the PMK
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002395 *
2396 * Configure the PMK for WPA state machine.
2397 */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002398void wpa_sm_set_pmk(struct wpa_sm *sm, const u8 *pmk, size_t pmk_len,
2399 const u8 *bssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002400{
2401 if (sm == NULL)
2402 return;
2403
2404 sm->pmk_len = pmk_len;
2405 os_memcpy(sm->pmk, pmk, pmk_len);
2406
2407#ifdef CONFIG_IEEE80211R
2408 /* Set XXKey to be PSK for FT key derivation */
2409 sm->xxkey_len = pmk_len;
2410 os_memcpy(sm->xxkey, pmk, pmk_len);
2411#endif /* CONFIG_IEEE80211R */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002412
2413 if (bssid) {
2414 pmksa_cache_add(sm->pmksa, pmk, pmk_len, NULL, 0,
2415 bssid, sm->own_addr,
2416 sm->network_ctx, sm->key_mgmt);
2417 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002418}
2419
2420
2421/**
2422 * wpa_sm_set_pmk_from_pmksa - Set PMK based on the current PMKSA
2423 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2424 *
2425 * Take the PMK from the current PMKSA into use. If no PMKSA is active, the PMK
2426 * will be cleared.
2427 */
2428void wpa_sm_set_pmk_from_pmksa(struct wpa_sm *sm)
2429{
2430 if (sm == NULL)
2431 return;
2432
2433 if (sm->cur_pmksa) {
2434 sm->pmk_len = sm->cur_pmksa->pmk_len;
2435 os_memcpy(sm->pmk, sm->cur_pmksa->pmk, sm->pmk_len);
2436 } else {
2437 sm->pmk_len = PMK_LEN;
2438 os_memset(sm->pmk, 0, PMK_LEN);
2439 }
2440}
2441
2442
2443/**
2444 * wpa_sm_set_fast_reauth - Set fast reauthentication (EAP) enabled/disabled
2445 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2446 * @fast_reauth: Whether fast reauthentication (EAP) is allowed
2447 */
2448void wpa_sm_set_fast_reauth(struct wpa_sm *sm, int fast_reauth)
2449{
2450 if (sm)
2451 sm->fast_reauth = fast_reauth;
2452}
2453
2454
2455/**
2456 * wpa_sm_set_scard_ctx - Set context pointer for smartcard callbacks
2457 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2458 * @scard_ctx: Context pointer for smartcard related callback functions
2459 */
2460void wpa_sm_set_scard_ctx(struct wpa_sm *sm, void *scard_ctx)
2461{
2462 if (sm == NULL)
2463 return;
2464 sm->scard_ctx = scard_ctx;
2465 if (sm->preauth_eapol)
2466 eapol_sm_register_scard_ctx(sm->preauth_eapol, scard_ctx);
2467}
2468
2469
2470/**
2471 * wpa_sm_set_config - Notification of current configration change
2472 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2473 * @config: Pointer to current network configuration
2474 *
2475 * Notify WPA state machine that configuration has changed. config will be
2476 * stored as a backpointer to network configuration. This can be %NULL to clear
2477 * the stored pointed.
2478 */
2479void wpa_sm_set_config(struct wpa_sm *sm, struct rsn_supp_config *config)
2480{
2481 if (!sm)
2482 return;
2483
2484 if (config) {
2485 sm->network_ctx = config->network_ctx;
2486 sm->peerkey_enabled = config->peerkey_enabled;
2487 sm->allowed_pairwise_cipher = config->allowed_pairwise_cipher;
2488 sm->proactive_key_caching = config->proactive_key_caching;
2489 sm->eap_workaround = config->eap_workaround;
2490 sm->eap_conf_ctx = config->eap_conf_ctx;
2491 if (config->ssid) {
2492 os_memcpy(sm->ssid, config->ssid, config->ssid_len);
2493 sm->ssid_len = config->ssid_len;
2494 } else
2495 sm->ssid_len = 0;
2496 sm->wpa_ptk_rekey = config->wpa_ptk_rekey;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002497 sm->p2p = config->p2p;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002498 sm->wpa_rsc_relaxation = config->wpa_rsc_relaxation;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002499 } else {
2500 sm->network_ctx = NULL;
2501 sm->peerkey_enabled = 0;
2502 sm->allowed_pairwise_cipher = 0;
2503 sm->proactive_key_caching = 0;
2504 sm->eap_workaround = 0;
2505 sm->eap_conf_ctx = NULL;
2506 sm->ssid_len = 0;
2507 sm->wpa_ptk_rekey = 0;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002508 sm->p2p = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002509 sm->wpa_rsc_relaxation = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002510 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002511}
2512
2513
2514/**
2515 * wpa_sm_set_own_addr - Set own MAC address
2516 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2517 * @addr: Own MAC address
2518 */
2519void wpa_sm_set_own_addr(struct wpa_sm *sm, const u8 *addr)
2520{
2521 if (sm)
2522 os_memcpy(sm->own_addr, addr, ETH_ALEN);
2523}
2524
2525
2526/**
2527 * wpa_sm_set_ifname - Set network interface name
2528 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2529 * @ifname: Interface name
2530 * @bridge_ifname: Optional bridge interface name (for pre-auth)
2531 */
2532void wpa_sm_set_ifname(struct wpa_sm *sm, const char *ifname,
2533 const char *bridge_ifname)
2534{
2535 if (sm) {
2536 sm->ifname = ifname;
2537 sm->bridge_ifname = bridge_ifname;
2538 }
2539}
2540
2541
2542/**
2543 * wpa_sm_set_eapol - Set EAPOL state machine pointer
2544 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2545 * @eapol: Pointer to EAPOL state machine allocated with eapol_sm_init()
2546 */
2547void wpa_sm_set_eapol(struct wpa_sm *sm, struct eapol_sm *eapol)
2548{
2549 if (sm)
2550 sm->eapol = eapol;
2551}
2552
2553
2554/**
2555 * wpa_sm_set_param - Set WPA state machine parameters
2556 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2557 * @param: Parameter field
2558 * @value: Parameter value
2559 * Returns: 0 on success, -1 on failure
2560 */
2561int wpa_sm_set_param(struct wpa_sm *sm, enum wpa_sm_conf_params param,
2562 unsigned int value)
2563{
2564 int ret = 0;
2565
2566 if (sm == NULL)
2567 return -1;
2568
2569 switch (param) {
2570 case RSNA_PMK_LIFETIME:
2571 if (value > 0)
2572 sm->dot11RSNAConfigPMKLifetime = value;
2573 else
2574 ret = -1;
2575 break;
2576 case RSNA_PMK_REAUTH_THRESHOLD:
2577 if (value > 0 && value <= 100)
2578 sm->dot11RSNAConfigPMKReauthThreshold = value;
2579 else
2580 ret = -1;
2581 break;
2582 case RSNA_SA_TIMEOUT:
2583 if (value > 0)
2584 sm->dot11RSNAConfigSATimeout = value;
2585 else
2586 ret = -1;
2587 break;
2588 case WPA_PARAM_PROTO:
2589 sm->proto = value;
2590 break;
2591 case WPA_PARAM_PAIRWISE:
2592 sm->pairwise_cipher = value;
2593 break;
2594 case WPA_PARAM_GROUP:
2595 sm->group_cipher = value;
2596 break;
2597 case WPA_PARAM_KEY_MGMT:
2598 sm->key_mgmt = value;
2599 break;
2600#ifdef CONFIG_IEEE80211W
2601 case WPA_PARAM_MGMT_GROUP:
2602 sm->mgmt_group_cipher = value;
2603 break;
2604#endif /* CONFIG_IEEE80211W */
2605 case WPA_PARAM_RSN_ENABLED:
2606 sm->rsn_enabled = value;
2607 break;
2608 case WPA_PARAM_MFP:
2609 sm->mfp = value;
2610 break;
2611 default:
2612 break;
2613 }
2614
2615 return ret;
2616}
2617
2618
2619/**
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002620 * wpa_sm_get_status - Get WPA state machine
2621 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2622 * @buf: Buffer for status information
2623 * @buflen: Maximum buffer length
2624 * @verbose: Whether to include verbose status information
2625 * Returns: Number of bytes written to buf.
2626 *
2627 * Query WPA state machine for status information. This function fills in
2628 * a text area with current status information. If the buffer (buf) is not
2629 * large enough, status information will be truncated to fit the buffer.
2630 */
2631int wpa_sm_get_status(struct wpa_sm *sm, char *buf, size_t buflen,
2632 int verbose)
2633{
2634 char *pos = buf, *end = buf + buflen;
2635 int ret;
2636
2637 ret = os_snprintf(pos, end - pos,
2638 "pairwise_cipher=%s\n"
2639 "group_cipher=%s\n"
2640 "key_mgmt=%s\n",
2641 wpa_cipher_txt(sm->pairwise_cipher),
2642 wpa_cipher_txt(sm->group_cipher),
2643 wpa_key_mgmt_txt(sm->key_mgmt, sm->proto));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002644 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002645 return pos - buf;
2646 pos += ret;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002647
2648 if (sm->mfp != NO_MGMT_FRAME_PROTECTION && sm->ap_rsn_ie) {
2649 struct wpa_ie_data rsn;
2650 if (wpa_parse_wpa_ie_rsn(sm->ap_rsn_ie, sm->ap_rsn_ie_len, &rsn)
2651 >= 0 &&
2652 rsn.capabilities & (WPA_CAPABILITY_MFPR |
2653 WPA_CAPABILITY_MFPC)) {
2654 ret = os_snprintf(pos, end - pos, "pmf=%d\n",
2655 (rsn.capabilities &
2656 WPA_CAPABILITY_MFPR) ? 2 : 1);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002657 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002658 return pos - buf;
2659 pos += ret;
2660 }
2661 }
2662
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002663 return pos - buf;
2664}
2665
2666
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07002667int wpa_sm_pmf_enabled(struct wpa_sm *sm)
2668{
2669 struct wpa_ie_data rsn;
2670
2671 if (sm->mfp == NO_MGMT_FRAME_PROTECTION || !sm->ap_rsn_ie)
2672 return 0;
2673
2674 if (wpa_parse_wpa_ie_rsn(sm->ap_rsn_ie, sm->ap_rsn_ie_len, &rsn) >= 0 &&
2675 rsn.capabilities & (WPA_CAPABILITY_MFPR | WPA_CAPABILITY_MFPC))
2676 return 1;
2677
2678 return 0;
2679}
2680
2681
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002682/**
2683 * wpa_sm_set_assoc_wpa_ie_default - Generate own WPA/RSN IE from configuration
2684 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2685 * @wpa_ie: Pointer to buffer for WPA/RSN IE
2686 * @wpa_ie_len: Pointer to the length of the wpa_ie buffer
2687 * Returns: 0 on success, -1 on failure
2688 */
2689int wpa_sm_set_assoc_wpa_ie_default(struct wpa_sm *sm, u8 *wpa_ie,
2690 size_t *wpa_ie_len)
2691{
2692 int res;
2693
2694 if (sm == NULL)
2695 return -1;
2696
Dmitry Shmidt55840ad2015-12-14 12:45:46 -08002697#ifdef CONFIG_TESTING_OPTIONS
2698 if (sm->test_assoc_ie) {
2699 wpa_printf(MSG_DEBUG,
2700 "TESTING: Replace association WPA/RSN IE");
2701 if (*wpa_ie_len < wpabuf_len(sm->test_assoc_ie))
2702 return -1;
2703 os_memcpy(wpa_ie, wpabuf_head(sm->test_assoc_ie),
2704 wpabuf_len(sm->test_assoc_ie));
2705 res = wpabuf_len(sm->test_assoc_ie);
2706 } else
2707#endif /* CONFIG_TESTING_OPTIONS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002708 res = wpa_gen_wpa_ie(sm, wpa_ie, *wpa_ie_len);
2709 if (res < 0)
2710 return -1;
2711 *wpa_ie_len = res;
2712
2713 wpa_hexdump(MSG_DEBUG, "WPA: Set own WPA IE default",
2714 wpa_ie, *wpa_ie_len);
2715
2716 if (sm->assoc_wpa_ie == NULL) {
2717 /*
2718 * Make a copy of the WPA/RSN IE so that 4-Way Handshake gets
2719 * the correct version of the IE even if PMKSA caching is
2720 * aborted (which would remove PMKID from IE generation).
2721 */
2722 sm->assoc_wpa_ie = os_malloc(*wpa_ie_len);
2723 if (sm->assoc_wpa_ie == NULL)
2724 return -1;
2725
2726 os_memcpy(sm->assoc_wpa_ie, wpa_ie, *wpa_ie_len);
2727 sm->assoc_wpa_ie_len = *wpa_ie_len;
2728 }
2729
2730 return 0;
2731}
2732
2733
2734/**
2735 * wpa_sm_set_assoc_wpa_ie - Set own WPA/RSN IE from (Re)AssocReq
2736 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2737 * @ie: Pointer to IE data (starting from id)
2738 * @len: IE length
2739 * Returns: 0 on success, -1 on failure
2740 *
2741 * Inform WPA state machine about the WPA/RSN IE used in (Re)Association
2742 * Request frame. The IE will be used to override the default value generated
2743 * with wpa_sm_set_assoc_wpa_ie_default().
2744 */
2745int wpa_sm_set_assoc_wpa_ie(struct wpa_sm *sm, const u8 *ie, size_t len)
2746{
2747 if (sm == NULL)
2748 return -1;
2749
2750 os_free(sm->assoc_wpa_ie);
2751 if (ie == NULL || len == 0) {
2752 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2753 "WPA: clearing own WPA/RSN IE");
2754 sm->assoc_wpa_ie = NULL;
2755 sm->assoc_wpa_ie_len = 0;
2756 } else {
2757 wpa_hexdump(MSG_DEBUG, "WPA: set own WPA/RSN IE", ie, len);
2758 sm->assoc_wpa_ie = os_malloc(len);
2759 if (sm->assoc_wpa_ie == NULL)
2760 return -1;
2761
2762 os_memcpy(sm->assoc_wpa_ie, ie, len);
2763 sm->assoc_wpa_ie_len = len;
2764 }
2765
2766 return 0;
2767}
2768
2769
2770/**
2771 * wpa_sm_set_ap_wpa_ie - Set AP WPA IE from Beacon/ProbeResp
2772 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2773 * @ie: Pointer to IE data (starting from id)
2774 * @len: IE length
2775 * Returns: 0 on success, -1 on failure
2776 *
2777 * Inform WPA state machine about the WPA IE used in Beacon / Probe Response
2778 * frame.
2779 */
2780int wpa_sm_set_ap_wpa_ie(struct wpa_sm *sm, const u8 *ie, size_t len)
2781{
2782 if (sm == NULL)
2783 return -1;
2784
2785 os_free(sm->ap_wpa_ie);
2786 if (ie == NULL || len == 0) {
2787 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2788 "WPA: clearing AP WPA IE");
2789 sm->ap_wpa_ie = NULL;
2790 sm->ap_wpa_ie_len = 0;
2791 } else {
2792 wpa_hexdump(MSG_DEBUG, "WPA: set AP WPA IE", ie, len);
2793 sm->ap_wpa_ie = os_malloc(len);
2794 if (sm->ap_wpa_ie == NULL)
2795 return -1;
2796
2797 os_memcpy(sm->ap_wpa_ie, ie, len);
2798 sm->ap_wpa_ie_len = len;
2799 }
2800
2801 return 0;
2802}
2803
2804
2805/**
2806 * wpa_sm_set_ap_rsn_ie - Set AP RSN IE from Beacon/ProbeResp
2807 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2808 * @ie: Pointer to IE data (starting from id)
2809 * @len: IE length
2810 * Returns: 0 on success, -1 on failure
2811 *
2812 * Inform WPA state machine about the RSN IE used in Beacon / Probe Response
2813 * frame.
2814 */
2815int wpa_sm_set_ap_rsn_ie(struct wpa_sm *sm, const u8 *ie, size_t len)
2816{
2817 if (sm == NULL)
2818 return -1;
2819
2820 os_free(sm->ap_rsn_ie);
2821 if (ie == NULL || len == 0) {
2822 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2823 "WPA: clearing AP RSN IE");
2824 sm->ap_rsn_ie = NULL;
2825 sm->ap_rsn_ie_len = 0;
2826 } else {
2827 wpa_hexdump(MSG_DEBUG, "WPA: set AP RSN IE", ie, len);
2828 sm->ap_rsn_ie = os_malloc(len);
2829 if (sm->ap_rsn_ie == NULL)
2830 return -1;
2831
2832 os_memcpy(sm->ap_rsn_ie, ie, len);
2833 sm->ap_rsn_ie_len = len;
2834 }
2835
2836 return 0;
2837}
2838
2839
2840/**
2841 * wpa_sm_parse_own_wpa_ie - Parse own WPA/RSN IE
2842 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2843 * @data: Pointer to data area for parsing results
2844 * Returns: 0 on success, -1 if IE is not known, or -2 on parsing failure
2845 *
2846 * Parse the contents of the own WPA or RSN IE from (Re)AssocReq and write the
2847 * parsed data into data.
2848 */
2849int wpa_sm_parse_own_wpa_ie(struct wpa_sm *sm, struct wpa_ie_data *data)
2850{
2851 if (sm == NULL)
2852 return -1;
2853
2854 if (sm->assoc_wpa_ie == NULL) {
2855 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2856 "WPA: No WPA/RSN IE available from association info");
2857 return -1;
2858 }
2859 if (wpa_parse_wpa_ie(sm->assoc_wpa_ie, sm->assoc_wpa_ie_len, data))
2860 return -2;
2861 return 0;
2862}
2863
2864
2865int wpa_sm_pmksa_cache_list(struct wpa_sm *sm, char *buf, size_t len)
2866{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002867 return pmksa_cache_list(sm->pmksa, buf, len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002868}
2869
2870
2871void wpa_sm_drop_sa(struct wpa_sm *sm)
2872{
2873 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Clear old PMK and PTK");
2874 sm->ptk_set = 0;
2875 sm->tptk_set = 0;
2876 os_memset(sm->pmk, 0, sizeof(sm->pmk));
2877 os_memset(&sm->ptk, 0, sizeof(sm->ptk));
2878 os_memset(&sm->tptk, 0, sizeof(sm->tptk));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002879#ifdef CONFIG_IEEE80211R
2880 os_memset(sm->xxkey, 0, sizeof(sm->xxkey));
2881 os_memset(sm->pmk_r0, 0, sizeof(sm->pmk_r0));
2882 os_memset(sm->pmk_r1, 0, sizeof(sm->pmk_r1));
2883#endif /* CONFIG_IEEE80211R */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002884}
2885
2886
2887int wpa_sm_has_ptk(struct wpa_sm *sm)
2888{
2889 if (sm == NULL)
2890 return 0;
2891 return sm->ptk_set;
2892}
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002893
2894
2895void wpa_sm_update_replay_ctr(struct wpa_sm *sm, const u8 *replay_ctr)
2896{
2897 os_memcpy(sm->rx_replay_counter, replay_ctr, WPA_REPLAY_COUNTER_LEN);
2898}
2899
2900
2901void wpa_sm_pmksa_cache_flush(struct wpa_sm *sm, void *network_ctx)
2902{
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07002903 pmksa_cache_flush(sm->pmksa, network_ctx, NULL, 0);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002904}
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002905
2906
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002907#ifdef CONFIG_WNM
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002908int wpa_wnmsleep_install_key(struct wpa_sm *sm, u8 subelem_id, u8 *buf)
2909{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002910 u16 keyinfo;
2911 u8 keylen; /* plaintext key len */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002912 u8 *key_rsc;
2913
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002914 if (subelem_id == WNM_SLEEP_SUBELEM_GTK) {
Dmitry Shmidt61593f02014-04-21 16:27:35 -07002915 struct wpa_gtk_data gd;
2916
2917 os_memset(&gd, 0, sizeof(gd));
2918 keylen = wpa_cipher_key_len(sm->group_cipher);
2919 gd.key_rsc_len = wpa_cipher_rsc_len(sm->group_cipher);
2920 gd.alg = wpa_cipher_to_alg(sm->group_cipher);
2921 if (gd.alg == WPA_ALG_NONE) {
2922 wpa_printf(MSG_DEBUG, "Unsupported group cipher suite");
2923 return -1;
2924 }
2925
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002926 key_rsc = buf + 5;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002927 keyinfo = WPA_GET_LE16(buf + 2);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002928 gd.gtk_len = keylen;
2929 if (gd.gtk_len != buf[4]) {
2930 wpa_printf(MSG_DEBUG, "GTK len mismatch len %d vs %d",
2931 gd.gtk_len, buf[4]);
2932 return -1;
2933 }
2934 gd.keyidx = keyinfo & 0x03; /* B0 - B1 */
2935 gd.tx = wpa_supplicant_gtk_tx_bit_workaround(
2936 sm, !!(keyinfo & WPA_KEY_INFO_TXRX));
2937
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002938 os_memcpy(gd.gtk, buf + 13, gd.gtk_len);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002939
2940 wpa_hexdump_key(MSG_DEBUG, "Install GTK (WNM SLEEP)",
2941 gd.gtk, gd.gtk_len);
2942 if (wpa_supplicant_install_gtk(sm, &gd, key_rsc)) {
Dmitry Shmidt61593f02014-04-21 16:27:35 -07002943 os_memset(&gd, 0, sizeof(gd));
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002944 wpa_printf(MSG_DEBUG, "Failed to install the GTK in "
2945 "WNM mode");
2946 return -1;
2947 }
Dmitry Shmidt61593f02014-04-21 16:27:35 -07002948 os_memset(&gd, 0, sizeof(gd));
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002949#ifdef CONFIG_IEEE80211W
2950 } else if (subelem_id == WNM_SLEEP_SUBELEM_IGTK) {
Dmitry Shmidt61593f02014-04-21 16:27:35 -07002951 struct wpa_igtk_kde igd;
2952 u16 keyidx;
2953
2954 os_memset(&igd, 0, sizeof(igd));
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002955 keylen = wpa_cipher_key_len(sm->mgmt_group_cipher);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002956 os_memcpy(igd.keyid, buf + 2, 2);
2957 os_memcpy(igd.pn, buf + 4, 6);
2958
2959 keyidx = WPA_GET_LE16(igd.keyid);
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002960 os_memcpy(igd.igtk, buf + 10, keylen);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002961
2962 wpa_hexdump_key(MSG_DEBUG, "Install IGTK (WNM SLEEP)",
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002963 igd.igtk, keylen);
2964 if (wpa_sm_set_key(sm, wpa_cipher_to_alg(sm->mgmt_group_cipher),
2965 broadcast_ether_addr,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002966 keyidx, 0, igd.pn, sizeof(igd.pn),
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002967 igd.igtk, keylen) < 0) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002968 wpa_printf(MSG_DEBUG, "Failed to install the IGTK in "
2969 "WNM mode");
Dmitry Shmidt61593f02014-04-21 16:27:35 -07002970 os_memset(&igd, 0, sizeof(igd));
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002971 return -1;
2972 }
Dmitry Shmidt61593f02014-04-21 16:27:35 -07002973 os_memset(&igd, 0, sizeof(igd));
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002974#endif /* CONFIG_IEEE80211W */
2975 } else {
2976 wpa_printf(MSG_DEBUG, "Unknown element id");
2977 return -1;
2978 }
2979
2980 return 0;
2981}
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002982#endif /* CONFIG_WNM */
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002983
2984
2985#ifdef CONFIG_PEERKEY
2986int wpa_sm_rx_eapol_peerkey(struct wpa_sm *sm, const u8 *src_addr,
2987 const u8 *buf, size_t len)
2988{
2989 struct wpa_peerkey *peerkey;
2990
2991 for (peerkey = sm->peerkey; peerkey; peerkey = peerkey->next) {
2992 if (os_memcmp(peerkey->addr, src_addr, ETH_ALEN) == 0)
2993 break;
2994 }
2995
2996 if (!peerkey)
2997 return 0;
2998
2999 wpa_sm_rx_eapol(sm, src_addr, buf, len);
3000
3001 return 1;
3002}
3003#endif /* CONFIG_PEERKEY */
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003004
3005
3006#ifdef CONFIG_P2P
3007
3008int wpa_sm_get_p2p_ip_addr(struct wpa_sm *sm, u8 *buf)
3009{
3010 if (sm == NULL || WPA_GET_BE32(sm->p2p_ip_addr) == 0)
3011 return -1;
3012 os_memcpy(buf, sm->p2p_ip_addr, 3 * 4);
3013 return 0;
3014}
3015
3016#endif /* CONFIG_P2P */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003017
3018
3019void wpa_sm_set_rx_replay_ctr(struct wpa_sm *sm, const u8 *rx_replay_counter)
3020{
3021 if (rx_replay_counter == NULL)
3022 return;
3023
3024 os_memcpy(sm->rx_replay_counter, rx_replay_counter,
3025 WPA_REPLAY_COUNTER_LEN);
3026 sm->rx_replay_counter_set = 1;
3027 wpa_printf(MSG_DEBUG, "Updated key replay counter");
3028}
3029
3030
Dmitry Shmidt807291d2015-01-27 13:40:23 -08003031void wpa_sm_set_ptk_kck_kek(struct wpa_sm *sm,
3032 const u8 *ptk_kck, size_t ptk_kck_len,
3033 const u8 *ptk_kek, size_t ptk_kek_len)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003034{
Dmitry Shmidt807291d2015-01-27 13:40:23 -08003035 if (ptk_kck && ptk_kck_len <= WPA_KCK_MAX_LEN) {
3036 os_memcpy(sm->ptk.kck, ptk_kck, ptk_kck_len);
3037 sm->ptk.kck_len = ptk_kck_len;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003038 wpa_printf(MSG_DEBUG, "Updated PTK KCK");
3039 }
Dmitry Shmidt807291d2015-01-27 13:40:23 -08003040 if (ptk_kek && ptk_kek_len <= WPA_KEK_MAX_LEN) {
3041 os_memcpy(sm->ptk.kek, ptk_kek, ptk_kek_len);
3042 sm->ptk.kek_len = ptk_kek_len;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003043 wpa_printf(MSG_DEBUG, "Updated PTK KEK");
3044 }
3045 sm->ptk_set = 1;
3046}
Dmitry Shmidt55840ad2015-12-14 12:45:46 -08003047
3048
3049#ifdef CONFIG_TESTING_OPTIONS
3050void wpa_sm_set_test_assoc_ie(struct wpa_sm *sm, struct wpabuf *buf)
3051{
3052 wpabuf_free(sm->test_assoc_ie);
3053 sm->test_assoc_ie = buf;
3054}
3055#endif /* CONFIG_TESTING_OPTIONS */