blob: 1221e6766f1772b6f97a721389366be540d86624 [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)
Dmitry Shmidt17022322016-04-06 13:28:42 -0700134 key_info |= WPA_KEY_INFO_MIC | WPA_KEY_INFO_SECURE;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700135 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,
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800252 sm->pmk, pmk_len, NULL,
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;
513
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800514 kde = sm->assoc_wpa_ie;
515 kde_len = sm->assoc_wpa_ie_len;
516
517#ifdef CONFIG_P2P
518 if (sm->p2p) {
519 kde_buf = os_malloc(kde_len + 2 + RSN_SELECTOR_LEN + 1);
520 if (kde_buf) {
521 u8 *pos;
522 wpa_printf(MSG_DEBUG, "P2P: Add IP Address Request KDE "
523 "into EAPOL-Key 2/4");
524 os_memcpy(kde_buf, kde, kde_len);
525 kde = kde_buf;
526 pos = kde + kde_len;
527 *pos++ = WLAN_EID_VENDOR_SPECIFIC;
528 *pos++ = RSN_SELECTOR_LEN + 1;
529 RSN_SELECTOR_PUT(pos, WFA_KEY_DATA_IP_ADDR_REQ);
530 pos += RSN_SELECTOR_LEN;
531 *pos++ = 0x01;
532 kde_len = pos - kde;
533 }
534 }
535#endif /* CONFIG_P2P */
536
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700537 if (wpa_supplicant_send_2_of_4(sm, sm->bssid, key, ver, sm->snonce,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800538 kde, kde_len, ptk) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700539 goto failed;
540
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800541 os_free(kde_buf);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700542 os_memcpy(sm->anonce, key->key_nonce, WPA_NONCE_LEN);
543 return;
544
545failed:
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800546 os_free(kde_buf);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700547 wpa_sm_deauthenticate(sm, WLAN_REASON_UNSPECIFIED);
548}
549
550
551static void wpa_sm_start_preauth(void *eloop_ctx, void *timeout_ctx)
552{
553 struct wpa_sm *sm = eloop_ctx;
554 rsn_preauth_candidate_process(sm);
555}
556
557
558static void wpa_supplicant_key_neg_complete(struct wpa_sm *sm,
559 const u8 *addr, int secure)
560{
561 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
562 "WPA: Key negotiation completed with "
563 MACSTR " [PTK=%s GTK=%s]", MAC2STR(addr),
564 wpa_cipher_txt(sm->pairwise_cipher),
565 wpa_cipher_txt(sm->group_cipher));
566 wpa_sm_cancel_auth_timeout(sm);
567 wpa_sm_set_state(sm, WPA_COMPLETED);
568
569 if (secure) {
570 wpa_sm_mlme_setprotection(
571 sm, addr, MLME_SETPROTECTION_PROTECT_TYPE_RX_TX,
572 MLME_SETPROTECTION_KEY_TYPE_PAIRWISE);
573 eapol_sm_notify_portValid(sm->eapol, TRUE);
574 if (wpa_key_mgmt_wpa_psk(sm->key_mgmt))
575 eapol_sm_notify_eap_success(sm->eapol, TRUE);
576 /*
577 * Start preauthentication after a short wait to avoid a
578 * possible race condition between the data receive and key
579 * configuration after the 4-Way Handshake. This increases the
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800580 * likelihood of the first preauth EAPOL-Start frame getting to
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700581 * the target AP.
582 */
583 eloop_register_timeout(1, 0, wpa_sm_start_preauth, sm, NULL);
584 }
585
586 if (sm->cur_pmksa && sm->cur_pmksa->opportunistic) {
587 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
588 "RSN: Authenticator accepted "
589 "opportunistic PMKSA entry - marking it valid");
590 sm->cur_pmksa->opportunistic = 0;
591 }
592
593#ifdef CONFIG_IEEE80211R
594 if (wpa_key_mgmt_ft(sm->key_mgmt)) {
595 /* Prepare for the next transition */
596 wpa_ft_prepare_auth_request(sm, NULL);
597 }
598#endif /* CONFIG_IEEE80211R */
599}
600
601
602static void wpa_sm_rekey_ptk(void *eloop_ctx, void *timeout_ctx)
603{
604 struct wpa_sm *sm = eloop_ctx;
605 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Request PTK rekeying");
606 wpa_sm_key_request(sm, 0, 1);
607}
608
609
610static int wpa_supplicant_install_ptk(struct wpa_sm *sm,
611 const struct wpa_eapol_key *key)
612{
613 int keylen, rsclen;
614 enum wpa_alg alg;
615 const u8 *key_rsc;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800616
Mathy Vanhoef5ad4a6a2017-09-29 04:22:51 +0200617 if (sm->ptk.installed) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800618 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
619 "WPA: Do not re-install same PTK to the driver");
620 return 0;
621 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700622
623 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
624 "WPA: Installing PTK to the driver");
625
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700626 if (sm->pairwise_cipher == WPA_CIPHER_NONE) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700627 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Pairwise Cipher "
628 "Suite: NONE - do not use pairwise keys");
629 return 0;
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700630 }
631
632 if (!wpa_cipher_valid_pairwise(sm->pairwise_cipher)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700633 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
634 "WPA: Unsupported pairwise cipher %d",
635 sm->pairwise_cipher);
636 return -1;
637 }
638
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700639 alg = wpa_cipher_to_alg(sm->pairwise_cipher);
640 keylen = wpa_cipher_key_len(sm->pairwise_cipher);
641 rsclen = wpa_cipher_rsc_len(sm->pairwise_cipher);
642
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800643 if (sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700644 key_rsc = null_rsc;
645 } else {
646 key_rsc = key->key_rsc;
647 wpa_hexdump(MSG_DEBUG, "WPA: RSC", key_rsc, rsclen);
648 }
649
650 if (wpa_sm_set_key(sm, alg, sm->bssid, 0, 1, key_rsc, rsclen,
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800651 sm->ptk.tk, keylen) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700652 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
653 "WPA: Failed to set PTK to the "
654 "driver (alg=%d keylen=%d bssid=" MACSTR ")",
655 alg, keylen, MAC2STR(sm->bssid));
656 return -1;
657 }
658
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800659 /* TK is not needed anymore in supplicant */
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800660 os_memset(sm->ptk.tk, 0, WPA_TK_MAX_LEN);
Mathy Vanhoef5ad4a6a2017-09-29 04:22:51 +0200661 sm->ptk.installed = 1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800662
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700663 if (sm->wpa_ptk_rekey) {
664 eloop_cancel_timeout(wpa_sm_rekey_ptk, sm, NULL);
665 eloop_register_timeout(sm->wpa_ptk_rekey, 0, wpa_sm_rekey_ptk,
666 sm, NULL);
667 }
668
669 return 0;
670}
671
672
673static int wpa_supplicant_check_group_cipher(struct wpa_sm *sm,
674 int group_cipher,
675 int keylen, int maxkeylen,
676 int *key_rsc_len,
677 enum wpa_alg *alg)
678{
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700679 int klen;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700680
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700681 *alg = wpa_cipher_to_alg(group_cipher);
682 if (*alg == WPA_ALG_NONE) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700683 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
684 "WPA: Unsupported Group Cipher %d",
685 group_cipher);
686 return -1;
687 }
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700688 *key_rsc_len = wpa_cipher_rsc_len(group_cipher);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700689
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700690 klen = wpa_cipher_key_len(group_cipher);
691 if (keylen != klen || maxkeylen < klen) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700692 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
693 "WPA: Unsupported %s Group Cipher key length %d (%d)",
694 wpa_cipher_txt(group_cipher), keylen, maxkeylen);
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700695 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700696 }
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700697 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700698}
699
700
701struct wpa_gtk_data {
702 enum wpa_alg alg;
703 int tx, key_rsc_len, keyidx;
704 u8 gtk[32];
705 int gtk_len;
706};
707
708
709static int wpa_supplicant_install_gtk(struct wpa_sm *sm,
710 const struct wpa_gtk_data *gd,
711 const u8 *key_rsc)
712{
713 const u8 *_gtk = gd->gtk;
714 u8 gtk_buf[32];
715
Mathy Vanhoef480c21c2017-07-12 16:03:24 +0200716 /* Detect possible key reinstallation */
717 if (sm->gtk.gtk_len == (size_t) gd->gtk_len &&
718 os_memcmp(sm->gtk.gtk, gd->gtk, sm->gtk.gtk_len) == 0) {
719 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
720 "WPA: Not reinstalling already in-use GTK to the driver (keyidx=%d tx=%d len=%d)",
721 gd->keyidx, gd->tx, gd->gtk_len);
722 return 0;
723 }
724
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700725 wpa_hexdump_key(MSG_DEBUG, "WPA: Group Key", gd->gtk, gd->gtk_len);
726 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
727 "WPA: Installing GTK to the driver (keyidx=%d tx=%d len=%d)",
728 gd->keyidx, gd->tx, gd->gtk_len);
729 wpa_hexdump(MSG_DEBUG, "WPA: RSC", key_rsc, gd->key_rsc_len);
730 if (sm->group_cipher == WPA_CIPHER_TKIP) {
731 /* Swap Tx/Rx keys for Michael MIC */
732 os_memcpy(gtk_buf, gd->gtk, 16);
733 os_memcpy(gtk_buf + 16, gd->gtk + 24, 8);
734 os_memcpy(gtk_buf + 24, gd->gtk + 16, 8);
735 _gtk = gtk_buf;
736 }
737 if (sm->pairwise_cipher == WPA_CIPHER_NONE) {
738 if (wpa_sm_set_key(sm, gd->alg, NULL,
739 gd->keyidx, 1, key_rsc, gd->key_rsc_len,
740 _gtk, gd->gtk_len) < 0) {
741 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
742 "WPA: Failed to set GTK to the driver "
743 "(Group only)");
Dmitry Shmidt61593f02014-04-21 16:27:35 -0700744 os_memset(gtk_buf, 0, sizeof(gtk_buf));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700745 return -1;
746 }
747 } else if (wpa_sm_set_key(sm, gd->alg, broadcast_ether_addr,
748 gd->keyidx, gd->tx, key_rsc, gd->key_rsc_len,
749 _gtk, gd->gtk_len) < 0) {
750 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
751 "WPA: Failed to set GTK to "
752 "the driver (alg=%d keylen=%d keyidx=%d)",
753 gd->alg, gd->gtk_len, gd->keyidx);
Dmitry Shmidt61593f02014-04-21 16:27:35 -0700754 os_memset(gtk_buf, 0, sizeof(gtk_buf));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700755 return -1;
756 }
Dmitry Shmidt61593f02014-04-21 16:27:35 -0700757 os_memset(gtk_buf, 0, sizeof(gtk_buf));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700758
Mathy Vanhoef480c21c2017-07-12 16:03:24 +0200759 sm->gtk.gtk_len = gd->gtk_len;
760 os_memcpy(sm->gtk.gtk, gd->gtk, sm->gtk.gtk_len);
761
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700762 return 0;
763}
764
765
766static int wpa_supplicant_gtk_tx_bit_workaround(const struct wpa_sm *sm,
767 int tx)
768{
769 if (tx && sm->pairwise_cipher != WPA_CIPHER_NONE) {
770 /* Ignore Tx bit for GTK if a pairwise key is used. One AP
771 * seemed to set this bit (incorrectly, since Tx is only when
772 * doing Group Key only APs) and without this workaround, the
773 * data connection does not work because wpa_supplicant
774 * configured non-zero keyidx to be used for unicast. */
775 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
776 "WPA: Tx bit set for GTK, but pairwise "
777 "keys are used - ignore Tx bit");
778 return 0;
779 }
780 return tx;
781}
782
783
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800784static int wpa_supplicant_rsc_relaxation(const struct wpa_sm *sm,
785 const u8 *rsc)
786{
787 int rsclen;
788
789 if (!sm->wpa_rsc_relaxation)
790 return 0;
791
792 rsclen = wpa_cipher_rsc_len(sm->group_cipher);
793
794 /*
795 * Try to detect RSC (endian) corruption issue where the AP sends
796 * the RSC bytes in EAPOL-Key message in the wrong order, both if
797 * it's actually a 6-byte field (as it should be) and if it treats
798 * it as an 8-byte field.
799 * An AP model known to have this bug is the Sapido RB-1632.
800 */
801 if (rsclen == 6 && ((rsc[5] && !rsc[0]) || rsc[6] || rsc[7])) {
802 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
803 "RSC %02x%02x%02x%02x%02x%02x%02x%02x is likely bogus, using 0",
804 rsc[0], rsc[1], rsc[2], rsc[3],
805 rsc[4], rsc[5], rsc[6], rsc[7]);
806
807 return 1;
808 }
809
810 return 0;
811}
812
813
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700814static int wpa_supplicant_pairwise_gtk(struct wpa_sm *sm,
815 const struct wpa_eapol_key *key,
816 const u8 *gtk, size_t gtk_len,
817 int key_info)
818{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700819 struct wpa_gtk_data gd;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800820 const u8 *key_rsc;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700821
822 /*
823 * IEEE Std 802.11i-2004 - 8.5.2 EAPOL-Key frames - Figure 43x
824 * GTK KDE format:
825 * KeyID[bits 0-1], Tx [bit 2], Reserved [bits 3-7]
826 * Reserved [bits 0-7]
827 * GTK
828 */
829
830 os_memset(&gd, 0, sizeof(gd));
831 wpa_hexdump_key(MSG_DEBUG, "RSN: received GTK in pairwise handshake",
832 gtk, gtk_len);
833
834 if (gtk_len < 2 || gtk_len - 2 > sizeof(gd.gtk))
835 return -1;
836
837 gd.keyidx = gtk[0] & 0x3;
838 gd.tx = wpa_supplicant_gtk_tx_bit_workaround(sm,
839 !!(gtk[0] & BIT(2)));
840 gtk += 2;
841 gtk_len -= 2;
842
843 os_memcpy(gd.gtk, gtk, gtk_len);
844 gd.gtk_len = gtk_len;
845
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800846 key_rsc = key->key_rsc;
847 if (wpa_supplicant_rsc_relaxation(sm, key->key_rsc))
848 key_rsc = null_rsc;
849
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800850 if (sm->group_cipher != WPA_CIPHER_GTK_NOT_USED &&
851 (wpa_supplicant_check_group_cipher(sm, sm->group_cipher,
852 gtk_len, gtk_len,
853 &gd.key_rsc_len, &gd.alg) ||
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800854 wpa_supplicant_install_gtk(sm, &gd, key_rsc))) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700855 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
856 "RSN: Failed to install GTK");
Dmitry Shmidt61593f02014-04-21 16:27:35 -0700857 os_memset(&gd, 0, sizeof(gd));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700858 return -1;
859 }
Dmitry Shmidt61593f02014-04-21 16:27:35 -0700860 os_memset(&gd, 0, sizeof(gd));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700861
862 wpa_supplicant_key_neg_complete(sm, sm->bssid,
863 key_info & WPA_KEY_INFO_SECURE);
864 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700865}
866
867
Mathy Vanhoef480c21c2017-07-12 16:03:24 +0200868#ifdef CONFIG_IEEE80211W
869static int wpa_supplicant_install_igtk(struct wpa_sm *sm,
870 const struct wpa_igtk_kde *igtk)
871{
872 size_t len = wpa_cipher_key_len(sm->mgmt_group_cipher);
873 u16 keyidx = WPA_GET_LE16(igtk->keyid);
874
875 /* Detect possible key reinstallation */
876 if (sm->igtk.igtk_len == len &&
877 os_memcmp(sm->igtk.igtk, igtk->igtk, sm->igtk.igtk_len) == 0) {
878 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
879 "WPA: Not reinstalling already in-use IGTK to the driver (keyidx=%d)",
880 keyidx);
881 return 0;
882 }
883
884 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
885 "WPA: IGTK keyid %d pn %02x%02x%02x%02x%02x%02x",
886 keyidx, MAC2STR(igtk->pn));
887 wpa_hexdump_key(MSG_DEBUG, "WPA: IGTK", igtk->igtk, len);
888 if (keyidx > 4095) {
889 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
890 "WPA: Invalid IGTK KeyID %d", keyidx);
891 return -1;
892 }
893 if (wpa_sm_set_key(sm, wpa_cipher_to_alg(sm->mgmt_group_cipher),
894 broadcast_ether_addr,
895 keyidx, 0, igtk->pn, sizeof(igtk->pn),
896 igtk->igtk, len) < 0) {
897 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
898 "WPA: Failed to configure IGTK to the driver");
899 return -1;
900 }
901
902 sm->igtk.igtk_len = len;
903 os_memcpy(sm->igtk.igtk, igtk->igtk, sm->igtk.igtk_len);
904
905 return 0;
906}
907#endif /* CONFIG_IEEE80211W */
908
909
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700910static int ieee80211w_set_keys(struct wpa_sm *sm,
911 struct wpa_eapol_ie_parse *ie)
912{
913#ifdef CONFIG_IEEE80211W
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -0700914 if (!wpa_cipher_valid_mgmt_group(sm->mgmt_group_cipher))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700915 return 0;
916
917 if (ie->igtk) {
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -0700918 size_t len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700919 const struct wpa_igtk_kde *igtk;
Mathy Vanhoef480c21c2017-07-12 16:03:24 +0200920
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -0700921 len = wpa_cipher_key_len(sm->mgmt_group_cipher);
922 if (ie->igtk_len != WPA_IGTK_KDE_PREFIX_LEN + len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700923 return -1;
Mathy Vanhoef480c21c2017-07-12 16:03:24 +0200924
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700925 igtk = (const struct wpa_igtk_kde *) ie->igtk;
Mathy Vanhoef480c21c2017-07-12 16:03:24 +0200926 if (wpa_supplicant_install_igtk(sm, igtk) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700927 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700928 }
929
930 return 0;
931#else /* CONFIG_IEEE80211W */
932 return 0;
933#endif /* CONFIG_IEEE80211W */
934}
935
936
937static void wpa_report_ie_mismatch(struct wpa_sm *sm,
938 const char *reason, const u8 *src_addr,
939 const u8 *wpa_ie, size_t wpa_ie_len,
940 const u8 *rsn_ie, size_t rsn_ie_len)
941{
942 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, "WPA: %s (src=" MACSTR ")",
943 reason, MAC2STR(src_addr));
944
945 if (sm->ap_wpa_ie) {
946 wpa_hexdump(MSG_INFO, "WPA: WPA IE in Beacon/ProbeResp",
947 sm->ap_wpa_ie, sm->ap_wpa_ie_len);
948 }
949 if (wpa_ie) {
950 if (!sm->ap_wpa_ie) {
951 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
952 "WPA: No WPA IE in Beacon/ProbeResp");
953 }
954 wpa_hexdump(MSG_INFO, "WPA: WPA IE in 3/4 msg",
955 wpa_ie, wpa_ie_len);
956 }
957
958 if (sm->ap_rsn_ie) {
959 wpa_hexdump(MSG_INFO, "WPA: RSN IE in Beacon/ProbeResp",
960 sm->ap_rsn_ie, sm->ap_rsn_ie_len);
961 }
962 if (rsn_ie) {
963 if (!sm->ap_rsn_ie) {
964 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
965 "WPA: No RSN IE in Beacon/ProbeResp");
966 }
967 wpa_hexdump(MSG_INFO, "WPA: RSN IE in 3/4 msg",
968 rsn_ie, rsn_ie_len);
969 }
970
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800971 wpa_sm_deauthenticate(sm, WLAN_REASON_IE_IN_4WAY_DIFFERS);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700972}
973
974
975#ifdef CONFIG_IEEE80211R
976
977static int ft_validate_mdie(struct wpa_sm *sm,
978 const unsigned char *src_addr,
979 struct wpa_eapol_ie_parse *ie,
980 const u8 *assoc_resp_mdie)
981{
982 struct rsn_mdie *mdie;
983
984 mdie = (struct rsn_mdie *) (ie->mdie + 2);
985 if (ie->mdie == NULL || ie->mdie_len < 2 + sizeof(*mdie) ||
986 os_memcmp(mdie->mobility_domain, sm->mobility_domain,
987 MOBILITY_DOMAIN_ID_LEN) != 0) {
988 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "FT: MDIE in msg 3/4 did "
989 "not match with the current mobility domain");
990 return -1;
991 }
992
993 if (assoc_resp_mdie &&
994 (assoc_resp_mdie[1] != ie->mdie[1] ||
995 os_memcmp(assoc_resp_mdie, ie->mdie, 2 + ie->mdie[1]) != 0)) {
996 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "FT: MDIE mismatch");
997 wpa_hexdump(MSG_DEBUG, "FT: MDIE in EAPOL-Key msg 3/4",
998 ie->mdie, 2 + ie->mdie[1]);
999 wpa_hexdump(MSG_DEBUG, "FT: MDIE in (Re)Association Response",
1000 assoc_resp_mdie, 2 + assoc_resp_mdie[1]);
1001 return -1;
1002 }
1003
1004 return 0;
1005}
1006
1007
1008static int ft_validate_ftie(struct wpa_sm *sm,
1009 const unsigned char *src_addr,
1010 struct wpa_eapol_ie_parse *ie,
1011 const u8 *assoc_resp_ftie)
1012{
1013 if (ie->ftie == NULL) {
1014 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1015 "FT: No FTIE in EAPOL-Key msg 3/4");
1016 return -1;
1017 }
1018
1019 if (assoc_resp_ftie == NULL)
1020 return 0;
1021
1022 if (assoc_resp_ftie[1] != ie->ftie[1] ||
1023 os_memcmp(assoc_resp_ftie, ie->ftie, 2 + ie->ftie[1]) != 0) {
1024 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "FT: FTIE mismatch");
1025 wpa_hexdump(MSG_DEBUG, "FT: FTIE in EAPOL-Key msg 3/4",
1026 ie->ftie, 2 + ie->ftie[1]);
1027 wpa_hexdump(MSG_DEBUG, "FT: FTIE in (Re)Association Response",
1028 assoc_resp_ftie, 2 + assoc_resp_ftie[1]);
1029 return -1;
1030 }
1031
1032 return 0;
1033}
1034
1035
1036static int ft_validate_rsnie(struct wpa_sm *sm,
1037 const unsigned char *src_addr,
1038 struct wpa_eapol_ie_parse *ie)
1039{
1040 struct wpa_ie_data rsn;
1041
1042 if (!ie->rsn_ie)
1043 return 0;
1044
1045 /*
1046 * Verify that PMKR1Name from EAPOL-Key message 3/4
1047 * matches with the value we derived.
1048 */
1049 if (wpa_parse_wpa_ie_rsn(ie->rsn_ie, ie->rsn_ie_len, &rsn) < 0 ||
1050 rsn.num_pmkid != 1 || rsn.pmkid == NULL) {
1051 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "FT: No PMKR1Name in "
1052 "FT 4-way handshake message 3/4");
1053 return -1;
1054 }
1055
Dmitry Shmidtc2817022014-07-02 10:32:10 -07001056 if (os_memcmp_const(rsn.pmkid, sm->pmk_r1_name, WPA_PMK_NAME_LEN) != 0)
1057 {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001058 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1059 "FT: PMKR1Name mismatch in "
1060 "FT 4-way handshake message 3/4");
1061 wpa_hexdump(MSG_DEBUG, "FT: PMKR1Name from Authenticator",
1062 rsn.pmkid, WPA_PMK_NAME_LEN);
1063 wpa_hexdump(MSG_DEBUG, "FT: Derived PMKR1Name",
1064 sm->pmk_r1_name, WPA_PMK_NAME_LEN);
1065 return -1;
1066 }
1067
1068 return 0;
1069}
1070
1071
1072static int wpa_supplicant_validate_ie_ft(struct wpa_sm *sm,
1073 const unsigned char *src_addr,
1074 struct wpa_eapol_ie_parse *ie)
1075{
1076 const u8 *pos, *end, *mdie = NULL, *ftie = NULL;
1077
1078 if (sm->assoc_resp_ies) {
1079 pos = sm->assoc_resp_ies;
1080 end = pos + sm->assoc_resp_ies_len;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001081 while (end - pos > 2) {
1082 if (2 + pos[1] > end - pos)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001083 break;
1084 switch (*pos) {
1085 case WLAN_EID_MOBILITY_DOMAIN:
1086 mdie = pos;
1087 break;
1088 case WLAN_EID_FAST_BSS_TRANSITION:
1089 ftie = pos;
1090 break;
1091 }
1092 pos += 2 + pos[1];
1093 }
1094 }
1095
1096 if (ft_validate_mdie(sm, src_addr, ie, mdie) < 0 ||
1097 ft_validate_ftie(sm, src_addr, ie, ftie) < 0 ||
1098 ft_validate_rsnie(sm, src_addr, ie) < 0)
1099 return -1;
1100
1101 return 0;
1102}
1103
1104#endif /* CONFIG_IEEE80211R */
1105
1106
1107static int wpa_supplicant_validate_ie(struct wpa_sm *sm,
1108 const unsigned char *src_addr,
1109 struct wpa_eapol_ie_parse *ie)
1110{
1111 if (sm->ap_wpa_ie == NULL && sm->ap_rsn_ie == NULL) {
1112 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1113 "WPA: No WPA/RSN IE for this AP known. "
1114 "Trying to get from scan results");
1115 if (wpa_sm_get_beacon_ie(sm) < 0) {
1116 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1117 "WPA: Could not find AP from "
1118 "the scan results");
1119 } else {
1120 wpa_msg(sm->ctx->msg_ctx, MSG_DEBUG,
1121 "WPA: Found the current AP from "
1122 "updated scan results");
1123 }
1124 }
1125
1126 if (ie->wpa_ie == NULL && ie->rsn_ie == NULL &&
1127 (sm->ap_wpa_ie || sm->ap_rsn_ie)) {
1128 wpa_report_ie_mismatch(sm, "IE in 3/4 msg does not match "
1129 "with IE in Beacon/ProbeResp (no IE?)",
1130 src_addr, ie->wpa_ie, ie->wpa_ie_len,
1131 ie->rsn_ie, ie->rsn_ie_len);
1132 return -1;
1133 }
1134
1135 if ((ie->wpa_ie && sm->ap_wpa_ie &&
1136 (ie->wpa_ie_len != sm->ap_wpa_ie_len ||
1137 os_memcmp(ie->wpa_ie, sm->ap_wpa_ie, ie->wpa_ie_len) != 0)) ||
1138 (ie->rsn_ie && sm->ap_rsn_ie &&
1139 wpa_compare_rsn_ie(wpa_key_mgmt_ft(sm->key_mgmt),
1140 sm->ap_rsn_ie, sm->ap_rsn_ie_len,
1141 ie->rsn_ie, ie->rsn_ie_len))) {
1142 wpa_report_ie_mismatch(sm, "IE in 3/4 msg does not match "
1143 "with IE in Beacon/ProbeResp",
1144 src_addr, ie->wpa_ie, ie->wpa_ie_len,
1145 ie->rsn_ie, ie->rsn_ie_len);
1146 return -1;
1147 }
1148
1149 if (sm->proto == WPA_PROTO_WPA &&
1150 ie->rsn_ie && sm->ap_rsn_ie == NULL && sm->rsn_enabled) {
1151 wpa_report_ie_mismatch(sm, "Possible downgrade attack "
1152 "detected - RSN was enabled and RSN IE "
1153 "was in msg 3/4, but not in "
1154 "Beacon/ProbeResp",
1155 src_addr, ie->wpa_ie, ie->wpa_ie_len,
1156 ie->rsn_ie, ie->rsn_ie_len);
1157 return -1;
1158 }
1159
1160#ifdef CONFIG_IEEE80211R
1161 if (wpa_key_mgmt_ft(sm->key_mgmt) &&
1162 wpa_supplicant_validate_ie_ft(sm, src_addr, ie) < 0)
1163 return -1;
1164#endif /* CONFIG_IEEE80211R */
1165
1166 return 0;
1167}
1168
1169
1170/**
1171 * wpa_supplicant_send_4_of_4 - Send message 4 of WPA/RSN 4-Way Handshake
1172 * @sm: Pointer to WPA state machine data from wpa_sm_init()
1173 * @dst: Destination address for the frame
1174 * @key: Pointer to the EAPOL-Key frame header
1175 * @ver: Version bits from EAPOL-Key Key Info
1176 * @key_info: Key Info
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001177 * @ptk: PTK to use for keyed hash and encryption
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001178 * Returns: >= 0 on success, < 0 on failure
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001179 */
1180int wpa_supplicant_send_4_of_4(struct wpa_sm *sm, const unsigned char *dst,
1181 const struct wpa_eapol_key *key,
1182 u16 ver, u16 key_info,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001183 struct wpa_ptk *ptk)
1184{
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001185 size_t mic_len, hdrlen, rlen;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001186 struct wpa_eapol_key *reply;
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001187 struct wpa_eapol_key_192 *reply192;
1188 u8 *rbuf, *key_mic;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001189
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001190 mic_len = wpa_mic_len(sm->key_mgmt);
1191 hdrlen = mic_len == 24 ? sizeof(*reply192) : sizeof(*reply);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001192 rbuf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_KEY, NULL,
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001193 hdrlen, &rlen, (void *) &reply);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001194 if (rbuf == NULL)
1195 return -1;
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001196 reply192 = (struct wpa_eapol_key_192 *) reply;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001197
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001198 reply->type = (sm->proto == WPA_PROTO_RSN ||
1199 sm->proto == WPA_PROTO_OSEN) ?
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001200 EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA;
1201 key_info &= WPA_KEY_INFO_SECURE;
1202 key_info |= ver | WPA_KEY_INFO_KEY_TYPE | WPA_KEY_INFO_MIC;
1203 WPA_PUT_BE16(reply->key_info, key_info);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001204 if (sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001205 WPA_PUT_BE16(reply->key_length, 0);
1206 else
1207 os_memcpy(reply->key_length, key->key_length, 2);
1208 os_memcpy(reply->replay_counter, key->replay_counter,
1209 WPA_REPLAY_COUNTER_LEN);
1210
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001211 key_mic = reply192->key_mic; /* same offset for reply and reply192 */
1212 if (mic_len == 24)
1213 WPA_PUT_BE16(reply192->key_data_length, 0);
1214 else
1215 WPA_PUT_BE16(reply->key_data_length, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001216
1217 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Sending EAPOL-Key 4/4");
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001218 return wpa_eapol_key_send(sm, ptk->kck, ptk->kck_len, ver, dst,
1219 ETH_P_EAPOL, rbuf, rlen, key_mic);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001220}
1221
1222
1223static void wpa_supplicant_process_3_of_4(struct wpa_sm *sm,
1224 const struct wpa_eapol_key *key,
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001225 u16 ver, const u8 *key_data,
1226 size_t key_data_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001227{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001228 u16 key_info, keylen;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001229 struct wpa_eapol_ie_parse ie;
1230
1231 wpa_sm_set_state(sm, WPA_4WAY_HANDSHAKE);
1232 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: RX message 3 of 4-Way "
1233 "Handshake from " MACSTR " (ver=%d)", MAC2STR(sm->bssid), ver);
1234
1235 key_info = WPA_GET_BE16(key->key_info);
1236
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001237 wpa_hexdump(MSG_DEBUG, "WPA: IE KeyData", key_data, key_data_len);
1238 if (wpa_supplicant_parse_ies(key_data, key_data_len, &ie) < 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001239 goto failed;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001240 if (ie.gtk && !(key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
1241 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1242 "WPA: GTK IE in unencrypted key data");
1243 goto failed;
1244 }
1245#ifdef CONFIG_IEEE80211W
1246 if (ie.igtk && !(key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
1247 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1248 "WPA: IGTK KDE in unencrypted key data");
1249 goto failed;
1250 }
1251
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001252 if (ie.igtk &&
1253 wpa_cipher_valid_mgmt_group(sm->mgmt_group_cipher) &&
1254 ie.igtk_len != WPA_IGTK_KDE_PREFIX_LEN +
1255 (unsigned int) wpa_cipher_key_len(sm->mgmt_group_cipher)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001256 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1257 "WPA: Invalid IGTK KDE length %lu",
1258 (unsigned long) ie.igtk_len);
1259 goto failed;
1260 }
1261#endif /* CONFIG_IEEE80211W */
1262
1263 if (wpa_supplicant_validate_ie(sm, sm->bssid, &ie) < 0)
1264 goto failed;
1265
1266 if (os_memcmp(sm->anonce, key->key_nonce, WPA_NONCE_LEN) != 0) {
1267 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1268 "WPA: ANonce from message 1 of 4-Way Handshake "
1269 "differs from 3 of 4-Way Handshake - drop packet (src="
1270 MACSTR ")", MAC2STR(sm->bssid));
1271 goto failed;
1272 }
1273
1274 keylen = WPA_GET_BE16(key->key_length);
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07001275 if (keylen != wpa_cipher_key_len(sm->pairwise_cipher)) {
1276 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1277 "WPA: Invalid %s key length %d (src=" MACSTR
1278 ")", wpa_cipher_txt(sm->pairwise_cipher), keylen,
1279 MAC2STR(sm->bssid));
1280 goto failed;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001281 }
1282
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001283#ifdef CONFIG_P2P
1284 if (ie.ip_addr_alloc) {
1285 os_memcpy(sm->p2p_ip_addr, ie.ip_addr_alloc, 3 * 4);
1286 wpa_hexdump(MSG_DEBUG, "P2P: IP address info",
1287 sm->p2p_ip_addr, sizeof(sm->p2p_ip_addr));
1288 }
1289#endif /* CONFIG_P2P */
1290
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001291 if (wpa_supplicant_send_4_of_4(sm, sm->bssid, key, ver, key_info,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001292 &sm->ptk) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001293 goto failed;
1294 }
1295
1296 /* SNonce was successfully used in msg 3/4, so mark it to be renewed
1297 * for the next 4-Way Handshake. If msg 3 is received again, the old
1298 * SNonce will still be used to avoid changing PTK. */
1299 sm->renew_snonce = 1;
1300
1301 if (key_info & WPA_KEY_INFO_INSTALL) {
1302 if (wpa_supplicant_install_ptk(sm, key))
1303 goto failed;
1304 }
1305
1306 if (key_info & WPA_KEY_INFO_SECURE) {
1307 wpa_sm_mlme_setprotection(
1308 sm, sm->bssid, MLME_SETPROTECTION_PROTECT_TYPE_RX,
1309 MLME_SETPROTECTION_KEY_TYPE_PAIRWISE);
1310 eapol_sm_notify_portValid(sm->eapol, TRUE);
1311 }
1312 wpa_sm_set_state(sm, WPA_GROUP_HANDSHAKE);
1313
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001314 if (sm->group_cipher == WPA_CIPHER_GTK_NOT_USED) {
1315 wpa_supplicant_key_neg_complete(sm, sm->bssid,
1316 key_info & WPA_KEY_INFO_SECURE);
1317 } else if (ie.gtk &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001318 wpa_supplicant_pairwise_gtk(sm, key,
1319 ie.gtk, ie.gtk_len, key_info) < 0) {
1320 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1321 "RSN: Failed to configure GTK");
1322 goto failed;
1323 }
1324
1325 if (ieee80211w_set_keys(sm, &ie) < 0) {
1326 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1327 "RSN: Failed to configure IGTK");
1328 goto failed;
1329 }
1330
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001331 if (ie.gtk)
1332 wpa_sm_set_rekey_offload(sm);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001333
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001334 if (sm->proto == WPA_PROTO_RSN && wpa_key_mgmt_suite_b(sm->key_mgmt)) {
1335 struct rsn_pmksa_cache_entry *sa;
1336
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001337 sa = pmksa_cache_add(sm->pmksa, sm->pmk, sm->pmk_len, NULL,
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001338 sm->ptk.kck, sm->ptk.kck_len,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001339 sm->bssid, sm->own_addr,
1340 sm->network_ctx, sm->key_mgmt);
1341 if (!sm->cur_pmksa)
1342 sm->cur_pmksa = sa;
1343 }
1344
Dmitry Shmidt4dd28dc2015-03-10 11:21:43 -07001345 sm->msg_3_of_4_ok = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001346 return;
1347
1348failed:
1349 wpa_sm_deauthenticate(sm, WLAN_REASON_UNSPECIFIED);
1350}
1351
1352
1353static int wpa_supplicant_process_1_of_2_rsn(struct wpa_sm *sm,
1354 const u8 *keydata,
1355 size_t keydatalen,
1356 u16 key_info,
1357 struct wpa_gtk_data *gd)
1358{
1359 int maxkeylen;
1360 struct wpa_eapol_ie_parse ie;
1361
1362 wpa_hexdump(MSG_DEBUG, "RSN: msg 1/2 key data", keydata, keydatalen);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001363 if (wpa_supplicant_parse_ies(keydata, keydatalen, &ie) < 0)
1364 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001365 if (ie.gtk && !(key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
1366 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1367 "WPA: GTK IE in unencrypted key data");
1368 return -1;
1369 }
1370 if (ie.gtk == NULL) {
1371 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1372 "WPA: No GTK IE in Group Key msg 1/2");
1373 return -1;
1374 }
1375 maxkeylen = gd->gtk_len = ie.gtk_len - 2;
1376
1377 if (wpa_supplicant_check_group_cipher(sm, sm->group_cipher,
1378 gd->gtk_len, maxkeylen,
1379 &gd->key_rsc_len, &gd->alg))
1380 return -1;
1381
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001382 wpa_hexdump_key(MSG_DEBUG, "RSN: received GTK in group key handshake",
1383 ie.gtk, ie.gtk_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001384 gd->keyidx = ie.gtk[0] & 0x3;
1385 gd->tx = wpa_supplicant_gtk_tx_bit_workaround(sm,
1386 !!(ie.gtk[0] & BIT(2)));
1387 if (ie.gtk_len - 2 > sizeof(gd->gtk)) {
1388 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1389 "RSN: Too long GTK in GTK IE (len=%lu)",
1390 (unsigned long) ie.gtk_len - 2);
1391 return -1;
1392 }
1393 os_memcpy(gd->gtk, ie.gtk + 2, ie.gtk_len - 2);
1394
1395 if (ieee80211w_set_keys(sm, &ie) < 0)
1396 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1397 "RSN: Failed to configure IGTK");
1398
1399 return 0;
1400}
1401
1402
1403static int wpa_supplicant_process_1_of_2_wpa(struct wpa_sm *sm,
1404 const struct wpa_eapol_key *key,
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001405 const u8 *key_data,
1406 size_t key_data_len, u16 key_info,
1407 u16 ver, struct wpa_gtk_data *gd)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001408{
1409 size_t maxkeylen;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001410 u16 gtk_len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001411
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001412 gtk_len = WPA_GET_BE16(key->key_length);
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001413 maxkeylen = key_data_len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001414 if (ver == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
1415 if (maxkeylen < 8) {
1416 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1417 "WPA: Too short maxkeylen (%lu)",
1418 (unsigned long) maxkeylen);
1419 return -1;
1420 }
1421 maxkeylen -= 8;
1422 }
1423
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001424 if (gtk_len > maxkeylen ||
1425 wpa_supplicant_check_group_cipher(sm, sm->group_cipher,
1426 gtk_len, maxkeylen,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001427 &gd->key_rsc_len, &gd->alg))
1428 return -1;
1429
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001430 gd->gtk_len = gtk_len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001431 gd->keyidx = (key_info & WPA_KEY_INFO_KEY_INDEX_MASK) >>
1432 WPA_KEY_INFO_KEY_INDEX_SHIFT;
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001433 if (ver == WPA_KEY_INFO_TYPE_HMAC_MD5_RC4 && sm->ptk.kek_len == 16) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001434#ifdef CONFIG_NO_RC4
1435 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1436 "WPA: RC4 not supported in the build");
1437 return -1;
1438#else /* CONFIG_NO_RC4 */
Dmitry Shmidt61593f02014-04-21 16:27:35 -07001439 u8 ek[32];
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001440 if (key_data_len > sizeof(gd->gtk)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001441 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1442 "WPA: RC4 key data too long (%lu)",
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001443 (unsigned long) key_data_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001444 return -1;
1445 }
Dmitry Shmidt61593f02014-04-21 16:27:35 -07001446 os_memcpy(ek, key->key_iv, 16);
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001447 os_memcpy(ek + 16, sm->ptk.kek, sm->ptk.kek_len);
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001448 os_memcpy(gd->gtk, key_data, key_data_len);
1449 if (rc4_skip(ek, 32, 256, gd->gtk, key_data_len)) {
Dmitry Shmidt61593f02014-04-21 16:27:35 -07001450 os_memset(ek, 0, sizeof(ek));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001451 wpa_msg(sm->ctx->msg_ctx, MSG_ERROR,
1452 "WPA: RC4 failed");
1453 return -1;
1454 }
Dmitry Shmidt61593f02014-04-21 16:27:35 -07001455 os_memset(ek, 0, sizeof(ek));
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001456#endif /* CONFIG_NO_RC4 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001457 } else if (ver == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001458 if (maxkeylen % 8) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001459 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1460 "WPA: Unsupported AES-WRAP len %lu",
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001461 (unsigned long) maxkeylen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001462 return -1;
1463 }
1464 if (maxkeylen > sizeof(gd->gtk)) {
1465 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1466 "WPA: AES-WRAP key data "
1467 "too long (keydatalen=%lu maxkeylen=%lu)",
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001468 (unsigned long) key_data_len,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001469 (unsigned long) maxkeylen);
1470 return -1;
1471 }
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001472 if (aes_unwrap(sm->ptk.kek, sm->ptk.kek_len, maxkeylen / 8,
1473 key_data, gd->gtk)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001474 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1475 "WPA: AES unwrap failed - could not decrypt "
1476 "GTK");
1477 return -1;
1478 }
1479 } else {
1480 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1481 "WPA: Unsupported key_info type %d", ver);
1482 return -1;
1483 }
1484 gd->tx = wpa_supplicant_gtk_tx_bit_workaround(
1485 sm, !!(key_info & WPA_KEY_INFO_TXRX));
1486 return 0;
1487}
1488
1489
1490static int wpa_supplicant_send_2_of_2(struct wpa_sm *sm,
1491 const struct wpa_eapol_key *key,
1492 int ver, u16 key_info)
1493{
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001494 size_t mic_len, hdrlen, rlen;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001495 struct wpa_eapol_key *reply;
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001496 struct wpa_eapol_key_192 *reply192;
1497 u8 *rbuf, *key_mic;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001498
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001499 mic_len = wpa_mic_len(sm->key_mgmt);
1500 hdrlen = mic_len == 24 ? sizeof(*reply192) : sizeof(*reply);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001501 rbuf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_KEY, NULL,
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001502 hdrlen, &rlen, (void *) &reply);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001503 if (rbuf == NULL)
1504 return -1;
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001505 reply192 = (struct wpa_eapol_key_192 *) reply;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001506
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001507 reply->type = (sm->proto == WPA_PROTO_RSN ||
1508 sm->proto == WPA_PROTO_OSEN) ?
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001509 EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA;
1510 key_info &= WPA_KEY_INFO_KEY_INDEX_MASK;
1511 key_info |= ver | WPA_KEY_INFO_MIC | WPA_KEY_INFO_SECURE;
1512 WPA_PUT_BE16(reply->key_info, key_info);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001513 if (sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001514 WPA_PUT_BE16(reply->key_length, 0);
1515 else
1516 os_memcpy(reply->key_length, key->key_length, 2);
1517 os_memcpy(reply->replay_counter, key->replay_counter,
1518 WPA_REPLAY_COUNTER_LEN);
1519
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001520 key_mic = reply192->key_mic; /* same offset for reply and reply192 */
1521 if (mic_len == 24)
1522 WPA_PUT_BE16(reply192->key_data_length, 0);
1523 else
1524 WPA_PUT_BE16(reply->key_data_length, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001525
1526 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Sending EAPOL-Key 2/2");
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001527 return wpa_eapol_key_send(sm, sm->ptk.kck, sm->ptk.kck_len, ver,
1528 sm->bssid, ETH_P_EAPOL, rbuf, rlen, key_mic);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001529}
1530
1531
1532static void wpa_supplicant_process_1_of_2(struct wpa_sm *sm,
1533 const unsigned char *src_addr,
1534 const struct wpa_eapol_key *key,
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001535 const u8 *key_data,
1536 size_t key_data_len, u16 ver)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001537{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001538 u16 key_info;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001539 int rekey, ret;
1540 struct wpa_gtk_data gd;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001541 const u8 *key_rsc;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001542
Dmitry Shmidt4dd28dc2015-03-10 11:21:43 -07001543 if (!sm->msg_3_of_4_ok) {
1544 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1545 "WPA: Group Key Handshake started prior to completion of 4-way handshake");
1546 goto failed;
1547 }
1548
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001549 os_memset(&gd, 0, sizeof(gd));
1550
1551 rekey = wpa_sm_get_state(sm) == WPA_COMPLETED;
1552 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: RX message 1 of Group Key "
1553 "Handshake from " MACSTR " (ver=%d)", MAC2STR(src_addr), ver);
1554
1555 key_info = WPA_GET_BE16(key->key_info);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001556
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001557 if (sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN) {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001558 ret = wpa_supplicant_process_1_of_2_rsn(sm, key_data,
1559 key_data_len, key_info,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001560 &gd);
1561 } else {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001562 ret = wpa_supplicant_process_1_of_2_wpa(sm, key, key_data,
1563 key_data_len,
1564 key_info, ver, &gd);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001565 }
1566
1567 wpa_sm_set_state(sm, WPA_GROUP_HANDSHAKE);
1568
1569 if (ret)
1570 goto failed;
1571
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001572 key_rsc = key->key_rsc;
1573 if (wpa_supplicant_rsc_relaxation(sm, key->key_rsc))
1574 key_rsc = null_rsc;
1575
1576 if (wpa_supplicant_install_gtk(sm, &gd, key_rsc) ||
1577 wpa_supplicant_send_2_of_2(sm, key, ver, key_info) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001578 goto failed;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001579 os_memset(&gd, 0, sizeof(gd));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001580
1581 if (rekey) {
1582 wpa_msg(sm->ctx->msg_ctx, MSG_INFO, "WPA: Group rekeying "
1583 "completed with " MACSTR " [GTK=%s]",
1584 MAC2STR(sm->bssid), wpa_cipher_txt(sm->group_cipher));
1585 wpa_sm_cancel_auth_timeout(sm);
1586 wpa_sm_set_state(sm, WPA_COMPLETED);
1587 } else {
1588 wpa_supplicant_key_neg_complete(sm, sm->bssid,
1589 key_info &
1590 WPA_KEY_INFO_SECURE);
1591 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001592
1593 wpa_sm_set_rekey_offload(sm);
1594
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001595 return;
1596
1597failed:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001598 os_memset(&gd, 0, sizeof(gd));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001599 wpa_sm_deauthenticate(sm, WLAN_REASON_UNSPECIFIED);
1600}
1601
1602
1603static int wpa_supplicant_verify_eapol_key_mic(struct wpa_sm *sm,
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001604 struct wpa_eapol_key_192 *key,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001605 u16 ver,
1606 const u8 *buf, size_t len)
1607{
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001608 u8 mic[WPA_EAPOL_KEY_MIC_MAX_LEN];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001609 int ok = 0;
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001610 size_t mic_len = wpa_mic_len(sm->key_mgmt);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001611
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001612 os_memcpy(mic, key->key_mic, mic_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001613 if (sm->tptk_set) {
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001614 os_memset(key->key_mic, 0, mic_len);
1615 wpa_eapol_key_mic(sm->tptk.kck, sm->tptk.kck_len, sm->key_mgmt,
1616 ver, buf, len, key->key_mic);
1617 if (os_memcmp_const(mic, key->key_mic, mic_len) != 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001618 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1619 "WPA: Invalid EAPOL-Key MIC "
1620 "when using TPTK - ignoring TPTK");
1621 } else {
1622 ok = 1;
1623 sm->tptk_set = 0;
1624 sm->ptk_set = 1;
1625 os_memcpy(&sm->ptk, &sm->tptk, sizeof(sm->ptk));
Dmitry Shmidt61593f02014-04-21 16:27:35 -07001626 os_memset(&sm->tptk, 0, sizeof(sm->tptk));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001627 }
1628 }
1629
1630 if (!ok && sm->ptk_set) {
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001631 os_memset(key->key_mic, 0, mic_len);
1632 wpa_eapol_key_mic(sm->ptk.kck, sm->ptk.kck_len, sm->key_mgmt,
1633 ver, buf, len, key->key_mic);
1634 if (os_memcmp_const(mic, key->key_mic, mic_len) != 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001635 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1636 "WPA: Invalid EAPOL-Key MIC - "
1637 "dropping packet");
1638 return -1;
1639 }
1640 ok = 1;
1641 }
1642
1643 if (!ok) {
1644 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1645 "WPA: Could not verify EAPOL-Key MIC - "
1646 "dropping packet");
1647 return -1;
1648 }
1649
1650 os_memcpy(sm->rx_replay_counter, key->replay_counter,
1651 WPA_REPLAY_COUNTER_LEN);
1652 sm->rx_replay_counter_set = 1;
1653 return 0;
1654}
1655
1656
1657/* Decrypt RSN EAPOL-Key key data (RC4 or AES-WRAP) */
1658static int wpa_supplicant_decrypt_key_data(struct wpa_sm *sm,
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001659 struct wpa_eapol_key *key, u16 ver,
1660 u8 *key_data, size_t *key_data_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001661{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001662 wpa_hexdump(MSG_DEBUG, "RSN: encrypted key data",
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001663 key_data, *key_data_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001664 if (!sm->ptk_set) {
1665 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1666 "WPA: PTK not available, cannot decrypt EAPOL-Key Key "
1667 "Data");
1668 return -1;
1669 }
1670
1671 /* Decrypt key data here so that this operation does not need
1672 * to be implemented separately for each message type. */
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001673 if (ver == WPA_KEY_INFO_TYPE_HMAC_MD5_RC4 && sm->ptk.kek_len == 16) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001674#ifdef CONFIG_NO_RC4
1675 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1676 "WPA: RC4 not supported in the build");
1677 return -1;
1678#else /* CONFIG_NO_RC4 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001679 u8 ek[32];
1680 os_memcpy(ek, key->key_iv, 16);
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001681 os_memcpy(ek + 16, sm->ptk.kek, sm->ptk.kek_len);
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001682 if (rc4_skip(ek, 32, 256, key_data, *key_data_len)) {
Dmitry Shmidt61593f02014-04-21 16:27:35 -07001683 os_memset(ek, 0, sizeof(ek));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001684 wpa_msg(sm->ctx->msg_ctx, MSG_ERROR,
1685 "WPA: RC4 failed");
1686 return -1;
1687 }
Dmitry Shmidt61593f02014-04-21 16:27:35 -07001688 os_memset(ek, 0, sizeof(ek));
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001689#endif /* CONFIG_NO_RC4 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001690 } else if (ver == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES ||
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001691 ver == WPA_KEY_INFO_TYPE_AES_128_CMAC ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001692 sm->key_mgmt == WPA_KEY_MGMT_OSEN ||
1693 wpa_key_mgmt_suite_b(sm->key_mgmt)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001694 u8 *buf;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001695 if (*key_data_len < 8 || *key_data_len % 8) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001696 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001697 "WPA: Unsupported AES-WRAP len %u",
1698 (unsigned int) *key_data_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001699 return -1;
1700 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001701 *key_data_len -= 8; /* AES-WRAP adds 8 bytes */
1702 buf = os_malloc(*key_data_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001703 if (buf == NULL) {
1704 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1705 "WPA: No memory for AES-UNWRAP buffer");
1706 return -1;
1707 }
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001708 if (aes_unwrap(sm->ptk.kek, sm->ptk.kek_len, *key_data_len / 8,
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001709 key_data, buf)) {
Dmitry Shmidt7d56b752015-12-22 10:59:44 -08001710 bin_clear_free(buf, *key_data_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001711 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1712 "WPA: AES unwrap failed - "
1713 "could not decrypt EAPOL-Key key data");
1714 return -1;
1715 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001716 os_memcpy(key_data, buf, *key_data_len);
Dmitry Shmidt7d56b752015-12-22 10:59:44 -08001717 bin_clear_free(buf, *key_data_len);
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001718 WPA_PUT_BE16(key->key_data_length, *key_data_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001719 } else {
1720 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1721 "WPA: Unsupported key_info type %d", ver);
1722 return -1;
1723 }
1724 wpa_hexdump_key(MSG_DEBUG, "WPA: decrypted EAPOL-Key key data",
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001725 key_data, *key_data_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001726 return 0;
1727}
1728
1729
1730/**
1731 * wpa_sm_aborted_cached - Notify WPA that PMKSA caching was aborted
1732 * @sm: Pointer to WPA state machine data from wpa_sm_init()
1733 */
1734void wpa_sm_aborted_cached(struct wpa_sm *sm)
1735{
1736 if (sm && sm->cur_pmksa) {
1737 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1738 "RSN: Cancelling PMKSA caching attempt");
1739 sm->cur_pmksa = NULL;
1740 }
1741}
1742
1743
1744static void wpa_eapol_key_dump(struct wpa_sm *sm,
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001745 const struct wpa_eapol_key *key,
1746 unsigned int key_data_len,
1747 const u8 *mic, unsigned int mic_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001748{
1749#ifndef CONFIG_NO_STDOUT_DEBUG
1750 u16 key_info = WPA_GET_BE16(key->key_info);
1751
1752 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, " EAPOL-Key type=%d", key->type);
1753 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1754 " key_info 0x%x (ver=%d keyidx=%d rsvd=%d %s%s%s%s%s%s%s%s)",
1755 key_info, key_info & WPA_KEY_INFO_TYPE_MASK,
1756 (key_info & WPA_KEY_INFO_KEY_INDEX_MASK) >>
1757 WPA_KEY_INFO_KEY_INDEX_SHIFT,
1758 (key_info & (BIT(13) | BIT(14) | BIT(15))) >> 13,
1759 key_info & WPA_KEY_INFO_KEY_TYPE ? "Pairwise" : "Group",
1760 key_info & WPA_KEY_INFO_INSTALL ? " Install" : "",
1761 key_info & WPA_KEY_INFO_ACK ? " Ack" : "",
1762 key_info & WPA_KEY_INFO_MIC ? " MIC" : "",
1763 key_info & WPA_KEY_INFO_SECURE ? " Secure" : "",
1764 key_info & WPA_KEY_INFO_ERROR ? " Error" : "",
1765 key_info & WPA_KEY_INFO_REQUEST ? " Request" : "",
1766 key_info & WPA_KEY_INFO_ENCR_KEY_DATA ? " Encr" : "");
1767 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1768 " key_length=%u key_data_length=%u",
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001769 WPA_GET_BE16(key->key_length), key_data_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001770 wpa_hexdump(MSG_DEBUG, " replay_counter",
1771 key->replay_counter, WPA_REPLAY_COUNTER_LEN);
1772 wpa_hexdump(MSG_DEBUG, " key_nonce", key->key_nonce, WPA_NONCE_LEN);
1773 wpa_hexdump(MSG_DEBUG, " key_iv", key->key_iv, 16);
1774 wpa_hexdump(MSG_DEBUG, " key_rsc", key->key_rsc, 8);
1775 wpa_hexdump(MSG_DEBUG, " key_id (reserved)", key->key_id, 8);
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001776 wpa_hexdump(MSG_DEBUG, " key_mic", mic, mic_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001777#endif /* CONFIG_NO_STDOUT_DEBUG */
1778}
1779
1780
1781/**
1782 * wpa_sm_rx_eapol - Process received WPA EAPOL frames
1783 * @sm: Pointer to WPA state machine data from wpa_sm_init()
1784 * @src_addr: Source MAC address of the EAPOL packet
1785 * @buf: Pointer to the beginning of the EAPOL data (EAPOL header)
1786 * @len: Length of the EAPOL frame
1787 * Returns: 1 = WPA EAPOL-Key processed, 0 = not a WPA EAPOL-Key, -1 failure
1788 *
1789 * This function is called for each received EAPOL frame. Other than EAPOL-Key
1790 * frames can be skipped if filtering is done elsewhere. wpa_sm_rx_eapol() is
1791 * only processing WPA and WPA2 EAPOL-Key frames.
1792 *
1793 * The received EAPOL-Key packets are validated and valid packets are replied
1794 * to. In addition, key material (PTK, GTK) is configured at the end of a
1795 * successful key handshake.
1796 */
1797int wpa_sm_rx_eapol(struct wpa_sm *sm, const u8 *src_addr,
1798 const u8 *buf, size_t len)
1799{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001800 size_t plen, data_len, key_data_len;
1801 const struct ieee802_1x_hdr *hdr;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001802 struct wpa_eapol_key *key;
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001803 struct wpa_eapol_key_192 *key192;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001804 u16 key_info, ver;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001805 u8 *tmp = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001806 int ret = -1;
1807 struct wpa_peerkey *peerkey = NULL;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001808 u8 *key_data;
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001809 size_t mic_len, keyhdrlen;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001810
1811#ifdef CONFIG_IEEE80211R
1812 sm->ft_completed = 0;
1813#endif /* CONFIG_IEEE80211R */
1814
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001815 mic_len = wpa_mic_len(sm->key_mgmt);
1816 keyhdrlen = mic_len == 24 ? sizeof(*key192) : sizeof(*key);
1817
1818 if (len < sizeof(*hdr) + keyhdrlen) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001819 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1820 "WPA: EAPOL frame too short to be a WPA "
1821 "EAPOL-Key (len %lu, expecting at least %lu)",
1822 (unsigned long) len,
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001823 (unsigned long) sizeof(*hdr) + keyhdrlen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001824 return 0;
1825 }
1826
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001827 hdr = (const struct ieee802_1x_hdr *) buf;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001828 plen = be_to_host16(hdr->length);
1829 data_len = plen + sizeof(*hdr);
1830 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1831 "IEEE 802.1X RX: version=%d type=%d length=%lu",
1832 hdr->version, hdr->type, (unsigned long) plen);
1833
1834 if (hdr->version < EAPOL_VERSION) {
1835 /* TODO: backwards compatibility */
1836 }
1837 if (hdr->type != IEEE802_1X_TYPE_EAPOL_KEY) {
1838 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1839 "WPA: EAPOL frame (type %u) discarded, "
1840 "not a Key frame", hdr->type);
1841 ret = 0;
1842 goto out;
1843 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001844 wpa_hexdump(MSG_MSGDUMP, "WPA: RX EAPOL-Key", buf, len);
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001845 if (plen > len - sizeof(*hdr) || plen < keyhdrlen) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001846 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1847 "WPA: EAPOL frame payload size %lu "
1848 "invalid (frame size %lu)",
1849 (unsigned long) plen, (unsigned long) len);
1850 ret = 0;
1851 goto out;
1852 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001853 if (data_len < len) {
1854 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1855 "WPA: ignoring %lu bytes after the IEEE 802.1X data",
1856 (unsigned long) len - data_len);
1857 }
1858
1859 /*
1860 * Make a copy of the frame since we need to modify the buffer during
1861 * MAC validation and Key Data decryption.
1862 */
1863 tmp = os_malloc(data_len);
1864 if (tmp == NULL)
1865 goto out;
1866 os_memcpy(tmp, buf, data_len);
1867 key = (struct wpa_eapol_key *) (tmp + sizeof(struct ieee802_1x_hdr));
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001868 key192 = (struct wpa_eapol_key_192 *)
1869 (tmp + sizeof(struct ieee802_1x_hdr));
1870 if (mic_len == 24)
1871 key_data = (u8 *) (key192 + 1);
1872 else
1873 key_data = (u8 *) (key + 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001874
1875 if (key->type != EAPOL_KEY_TYPE_WPA && key->type != EAPOL_KEY_TYPE_RSN)
1876 {
1877 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1878 "WPA: EAPOL-Key type (%d) unknown, discarded",
1879 key->type);
1880 ret = 0;
1881 goto out;
1882 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001883
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001884 if (mic_len == 24)
1885 key_data_len = WPA_GET_BE16(key192->key_data_length);
1886 else
1887 key_data_len = WPA_GET_BE16(key->key_data_length);
1888 wpa_eapol_key_dump(sm, key, key_data_len, key192->key_mic, mic_len);
1889
1890 if (key_data_len > plen - keyhdrlen) {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001891 wpa_msg(sm->ctx->msg_ctx, MSG_INFO, "WPA: Invalid EAPOL-Key "
1892 "frame - key_data overflow (%u > %u)",
1893 (unsigned int) key_data_len,
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001894 (unsigned int) (plen - keyhdrlen));
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001895 goto out;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001896 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001897
1898 eapol_sm_notify_lower_layer_success(sm->eapol, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001899 key_info = WPA_GET_BE16(key->key_info);
1900 ver = key_info & WPA_KEY_INFO_TYPE_MASK;
1901 if (ver != WPA_KEY_INFO_TYPE_HMAC_MD5_RC4 &&
1902#if defined(CONFIG_IEEE80211R) || defined(CONFIG_IEEE80211W)
1903 ver != WPA_KEY_INFO_TYPE_AES_128_CMAC &&
1904#endif /* CONFIG_IEEE80211R || CONFIG_IEEE80211W */
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001905 ver != WPA_KEY_INFO_TYPE_HMAC_SHA1_AES &&
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001906 !wpa_key_mgmt_suite_b(sm->key_mgmt) &&
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001907 sm->key_mgmt != WPA_KEY_MGMT_OSEN) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001908 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1909 "WPA: Unsupported EAPOL-Key descriptor version %d",
1910 ver);
1911 goto out;
1912 }
1913
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001914 if (sm->key_mgmt == WPA_KEY_MGMT_OSEN &&
1915 ver != WPA_KEY_INFO_TYPE_AKM_DEFINED) {
1916 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1917 "OSEN: Unsupported EAPOL-Key descriptor version %d",
1918 ver);
1919 goto out;
1920 }
1921
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001922 if (wpa_key_mgmt_suite_b(sm->key_mgmt) &&
1923 ver != WPA_KEY_INFO_TYPE_AKM_DEFINED) {
1924 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1925 "RSN: Unsupported EAPOL-Key descriptor version %d (expected AKM defined = 0)",
1926 ver);
1927 goto out;
1928 }
1929
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001930#ifdef CONFIG_IEEE80211R
1931 if (wpa_key_mgmt_ft(sm->key_mgmt)) {
1932 /* IEEE 802.11r uses a new key_info type (AES-128-CMAC). */
1933 if (ver != WPA_KEY_INFO_TYPE_AES_128_CMAC) {
1934 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1935 "FT: AP did not use AES-128-CMAC");
1936 goto out;
1937 }
1938 } else
1939#endif /* CONFIG_IEEE80211R */
1940#ifdef CONFIG_IEEE80211W
1941 if (wpa_key_mgmt_sha256(sm->key_mgmt)) {
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001942 if (ver != WPA_KEY_INFO_TYPE_AES_128_CMAC &&
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001943 sm->key_mgmt != WPA_KEY_MGMT_OSEN &&
1944 !wpa_key_mgmt_suite_b(sm->key_mgmt)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001945 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1946 "WPA: AP did not use the "
1947 "negotiated AES-128-CMAC");
1948 goto out;
1949 }
1950 } else
1951#endif /* CONFIG_IEEE80211W */
1952 if (sm->pairwise_cipher == WPA_CIPHER_CCMP &&
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001953 !wpa_key_mgmt_suite_b(sm->key_mgmt) &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001954 ver != WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
1955 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1956 "WPA: CCMP is used, but EAPOL-Key "
1957 "descriptor version (%d) is not 2", ver);
1958 if (sm->group_cipher != WPA_CIPHER_CCMP &&
1959 !(key_info & WPA_KEY_INFO_KEY_TYPE)) {
1960 /* Earlier versions of IEEE 802.11i did not explicitly
1961 * require version 2 descriptor for all EAPOL-Key
1962 * packets, so allow group keys to use version 1 if
1963 * CCMP is not used for them. */
1964 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1965 "WPA: Backwards compatibility: allow invalid "
1966 "version for non-CCMP group keys");
Jouni Malinen658fb4a2014-11-14 20:57:05 +02001967 } else if (ver == WPA_KEY_INFO_TYPE_AES_128_CMAC) {
1968 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1969 "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 -07001970 } else
1971 goto out;
Dmitry Shmidt71757432014-06-02 13:50:35 -07001972 } else if (sm->pairwise_cipher == WPA_CIPHER_GCMP &&
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001973 !wpa_key_mgmt_suite_b(sm->key_mgmt) &&
Dmitry Shmidt71757432014-06-02 13:50:35 -07001974 ver != WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001975 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1976 "WPA: GCMP is used, but EAPOL-Key "
1977 "descriptor version (%d) is not 2", ver);
1978 goto out;
1979 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001980
1981#ifdef CONFIG_PEERKEY
1982 for (peerkey = sm->peerkey; peerkey; peerkey = peerkey->next) {
1983 if (os_memcmp(peerkey->addr, src_addr, ETH_ALEN) == 0)
1984 break;
1985 }
1986
1987 if (!(key_info & WPA_KEY_INFO_SMK_MESSAGE) && peerkey) {
1988 if (!peerkey->initiator && peerkey->replay_counter_set &&
1989 os_memcmp(key->replay_counter, peerkey->replay_counter,
1990 WPA_REPLAY_COUNTER_LEN) <= 0) {
1991 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1992 "RSN: EAPOL-Key Replay Counter did not "
1993 "increase (STK) - dropping packet");
1994 goto out;
1995 } else if (peerkey->initiator) {
1996 u8 _tmp[WPA_REPLAY_COUNTER_LEN];
1997 os_memcpy(_tmp, key->replay_counter,
1998 WPA_REPLAY_COUNTER_LEN);
1999 inc_byte_array(_tmp, WPA_REPLAY_COUNTER_LEN);
2000 if (os_memcmp(_tmp, peerkey->replay_counter,
2001 WPA_REPLAY_COUNTER_LEN) != 0) {
2002 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2003 "RSN: EAPOL-Key Replay "
2004 "Counter did not match (STK) - "
2005 "dropping packet");
2006 goto out;
2007 }
2008 }
2009 }
2010
2011 if (peerkey && peerkey->initiator && (key_info & WPA_KEY_INFO_ACK)) {
2012 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2013 "RSN: Ack bit in key_info from STK peer");
2014 goto out;
2015 }
2016#endif /* CONFIG_PEERKEY */
2017
2018 if (!peerkey && sm->rx_replay_counter_set &&
2019 os_memcmp(key->replay_counter, sm->rx_replay_counter,
2020 WPA_REPLAY_COUNTER_LEN) <= 0) {
2021 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
2022 "WPA: EAPOL-Key Replay Counter did not increase - "
2023 "dropping packet");
2024 goto out;
2025 }
2026
2027 if (!(key_info & (WPA_KEY_INFO_ACK | WPA_KEY_INFO_SMK_MESSAGE))
2028#ifdef CONFIG_PEERKEY
2029 && (peerkey == NULL || !peerkey->initiator)
2030#endif /* CONFIG_PEERKEY */
2031 ) {
2032 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2033 "WPA: No Ack bit in key_info");
2034 goto out;
2035 }
2036
2037 if (key_info & WPA_KEY_INFO_REQUEST) {
2038 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2039 "WPA: EAPOL-Key with Request bit - dropped");
2040 goto out;
2041 }
2042
2043 if ((key_info & WPA_KEY_INFO_MIC) && !peerkey &&
Dmitry Shmidt807291d2015-01-27 13:40:23 -08002044 wpa_supplicant_verify_eapol_key_mic(sm, key192, ver, tmp, data_len))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002045 goto out;
2046
2047#ifdef CONFIG_PEERKEY
2048 if ((key_info & WPA_KEY_INFO_MIC) && peerkey &&
Dmitry Shmidt807291d2015-01-27 13:40:23 -08002049 peerkey_verify_eapol_key_mic(sm, peerkey, key192, ver, tmp,
2050 data_len))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002051 goto out;
2052#endif /* CONFIG_PEERKEY */
2053
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002054 if ((sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN) &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002055 (key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002056 if (wpa_supplicant_decrypt_key_data(sm, key, ver, key_data,
2057 &key_data_len))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002058 goto out;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002059 }
2060
2061 if (key_info & WPA_KEY_INFO_KEY_TYPE) {
2062 if (key_info & WPA_KEY_INFO_KEY_INDEX_MASK) {
2063 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
2064 "WPA: Ignored EAPOL-Key (Pairwise) with "
2065 "non-zero key index");
2066 goto out;
2067 }
2068 if (peerkey) {
2069 /* PeerKey 4-Way Handshake */
Dmitry Shmidtc2817022014-07-02 10:32:10 -07002070 peerkey_rx_eapol_4way(sm, peerkey, key, key_info, ver,
2071 key_data, key_data_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002072 } else if (key_info & WPA_KEY_INFO_MIC) {
2073 /* 3/4 4-Way Handshake */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002074 wpa_supplicant_process_3_of_4(sm, key, ver, key_data,
2075 key_data_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002076 } else {
2077 /* 1/4 4-Way Handshake */
2078 wpa_supplicant_process_1_of_4(sm, src_addr, key,
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002079 ver, key_data,
2080 key_data_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002081 }
2082 } else if (key_info & WPA_KEY_INFO_SMK_MESSAGE) {
2083 /* PeerKey SMK Handshake */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002084 peerkey_rx_eapol_smk(sm, src_addr, key, key_data_len, key_info,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002085 ver);
2086 } else {
2087 if (key_info & WPA_KEY_INFO_MIC) {
2088 /* 1/2 Group Key Handshake */
2089 wpa_supplicant_process_1_of_2(sm, src_addr, key,
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002090 key_data, key_data_len,
2091 ver);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002092 } else {
2093 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
2094 "WPA: EAPOL-Key (Group) without Mic bit - "
2095 "dropped");
2096 }
2097 }
2098
2099 ret = 1;
2100
2101out:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002102 bin_clear_free(tmp, data_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002103 return ret;
2104}
2105
2106
2107#ifdef CONFIG_CTRL_IFACE
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002108static u32 wpa_key_mgmt_suite(struct wpa_sm *sm)
2109{
2110 switch (sm->key_mgmt) {
2111 case WPA_KEY_MGMT_IEEE8021X:
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002112 return ((sm->proto == WPA_PROTO_RSN ||
2113 sm->proto == WPA_PROTO_OSEN) ?
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002114 RSN_AUTH_KEY_MGMT_UNSPEC_802_1X :
2115 WPA_AUTH_KEY_MGMT_UNSPEC_802_1X);
2116 case WPA_KEY_MGMT_PSK:
2117 return (sm->proto == WPA_PROTO_RSN ?
2118 RSN_AUTH_KEY_MGMT_PSK_OVER_802_1X :
2119 WPA_AUTH_KEY_MGMT_PSK_OVER_802_1X);
2120#ifdef CONFIG_IEEE80211R
2121 case WPA_KEY_MGMT_FT_IEEE8021X:
2122 return RSN_AUTH_KEY_MGMT_FT_802_1X;
2123 case WPA_KEY_MGMT_FT_PSK:
2124 return RSN_AUTH_KEY_MGMT_FT_PSK;
2125#endif /* CONFIG_IEEE80211R */
2126#ifdef CONFIG_IEEE80211W
2127 case WPA_KEY_MGMT_IEEE8021X_SHA256:
2128 return RSN_AUTH_KEY_MGMT_802_1X_SHA256;
2129 case WPA_KEY_MGMT_PSK_SHA256:
2130 return RSN_AUTH_KEY_MGMT_PSK_SHA256;
2131#endif /* CONFIG_IEEE80211W */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002132 case WPA_KEY_MGMT_CCKM:
2133 return (sm->proto == WPA_PROTO_RSN ?
2134 RSN_AUTH_KEY_MGMT_CCKM:
2135 WPA_AUTH_KEY_MGMT_CCKM);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002136 case WPA_KEY_MGMT_WPA_NONE:
2137 return WPA_AUTH_KEY_MGMT_NONE;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002138 case WPA_KEY_MGMT_IEEE8021X_SUITE_B:
2139 return RSN_AUTH_KEY_MGMT_802_1X_SUITE_B;
Dmitry Shmidt807291d2015-01-27 13:40:23 -08002140 case WPA_KEY_MGMT_IEEE8021X_SUITE_B_192:
2141 return RSN_AUTH_KEY_MGMT_802_1X_SUITE_B_192;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002142 default:
2143 return 0;
2144 }
2145}
2146
2147
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002148#define RSN_SUITE "%02x-%02x-%02x-%d"
2149#define RSN_SUITE_ARG(s) \
2150((s) >> 24) & 0xff, ((s) >> 16) & 0xff, ((s) >> 8) & 0xff, (s) & 0xff
2151
2152/**
2153 * wpa_sm_get_mib - Dump text list of MIB entries
2154 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2155 * @buf: Buffer for the list
2156 * @buflen: Length of the buffer
2157 * Returns: Number of bytes written to buffer
2158 *
2159 * This function is used fetch dot11 MIB variables.
2160 */
2161int wpa_sm_get_mib(struct wpa_sm *sm, char *buf, size_t buflen)
2162{
2163 char pmkid_txt[PMKID_LEN * 2 + 1];
2164 int rsna, ret;
2165 size_t len;
2166
2167 if (sm->cur_pmksa) {
2168 wpa_snprintf_hex(pmkid_txt, sizeof(pmkid_txt),
2169 sm->cur_pmksa->pmkid, PMKID_LEN);
2170 } else
2171 pmkid_txt[0] = '\0';
2172
2173 if ((wpa_key_mgmt_wpa_psk(sm->key_mgmt) ||
2174 wpa_key_mgmt_wpa_ieee8021x(sm->key_mgmt)) &&
2175 sm->proto == WPA_PROTO_RSN)
2176 rsna = 1;
2177 else
2178 rsna = 0;
2179
2180 ret = os_snprintf(buf, buflen,
2181 "dot11RSNAOptionImplemented=TRUE\n"
2182 "dot11RSNAPreauthenticationImplemented=TRUE\n"
2183 "dot11RSNAEnabled=%s\n"
2184 "dot11RSNAPreauthenticationEnabled=%s\n"
2185 "dot11RSNAConfigVersion=%d\n"
2186 "dot11RSNAConfigPairwiseKeysSupported=5\n"
2187 "dot11RSNAConfigGroupCipherSize=%d\n"
2188 "dot11RSNAConfigPMKLifetime=%d\n"
2189 "dot11RSNAConfigPMKReauthThreshold=%d\n"
2190 "dot11RSNAConfigNumberOfPTKSAReplayCounters=1\n"
2191 "dot11RSNAConfigSATimeout=%d\n",
2192 rsna ? "TRUE" : "FALSE",
2193 rsna ? "TRUE" : "FALSE",
2194 RSN_VERSION,
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07002195 wpa_cipher_key_len(sm->group_cipher) * 8,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002196 sm->dot11RSNAConfigPMKLifetime,
2197 sm->dot11RSNAConfigPMKReauthThreshold,
2198 sm->dot11RSNAConfigSATimeout);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002199 if (os_snprintf_error(buflen, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002200 return 0;
2201 len = ret;
2202
2203 ret = os_snprintf(
2204 buf + len, buflen - len,
2205 "dot11RSNAAuthenticationSuiteSelected=" RSN_SUITE "\n"
2206 "dot11RSNAPairwiseCipherSelected=" RSN_SUITE "\n"
2207 "dot11RSNAGroupCipherSelected=" RSN_SUITE "\n"
2208 "dot11RSNAPMKIDUsed=%s\n"
2209 "dot11RSNAAuthenticationSuiteRequested=" RSN_SUITE "\n"
2210 "dot11RSNAPairwiseCipherRequested=" RSN_SUITE "\n"
2211 "dot11RSNAGroupCipherRequested=" RSN_SUITE "\n"
2212 "dot11RSNAConfigNumberOfGTKSAReplayCounters=0\n"
2213 "dot11RSNA4WayHandshakeFailures=%u\n",
2214 RSN_SUITE_ARG(wpa_key_mgmt_suite(sm)),
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07002215 RSN_SUITE_ARG(wpa_cipher_to_suite(sm->proto,
2216 sm->pairwise_cipher)),
2217 RSN_SUITE_ARG(wpa_cipher_to_suite(sm->proto,
2218 sm->group_cipher)),
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002219 pmkid_txt,
2220 RSN_SUITE_ARG(wpa_key_mgmt_suite(sm)),
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07002221 RSN_SUITE_ARG(wpa_cipher_to_suite(sm->proto,
2222 sm->pairwise_cipher)),
2223 RSN_SUITE_ARG(wpa_cipher_to_suite(sm->proto,
2224 sm->group_cipher)),
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002225 sm->dot11RSNA4WayHandshakeFailures);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002226 if (!os_snprintf_error(buflen - len, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002227 len += ret;
2228
2229 return (int) len;
2230}
2231#endif /* CONFIG_CTRL_IFACE */
2232
2233
2234static void wpa_sm_pmksa_free_cb(struct rsn_pmksa_cache_entry *entry,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002235 void *ctx, enum pmksa_free_reason reason)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002236{
2237 struct wpa_sm *sm = ctx;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002238 int deauth = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002239
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002240 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "RSN: PMKSA cache entry free_cb: "
2241 MACSTR " reason=%d", MAC2STR(entry->aa), reason);
2242
2243 if (sm->cur_pmksa == entry) {
2244 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2245 "RSN: %s current PMKSA entry",
2246 reason == PMKSA_REPLACE ? "replaced" : "removed");
2247 pmksa_cache_clear_current(sm);
2248
2249 /*
2250 * If an entry is simply being replaced, there's no need to
2251 * deauthenticate because it will be immediately re-added.
2252 * This happens when EAP authentication is completed again
2253 * (reauth or failed PMKSA caching attempt).
2254 */
2255 if (reason != PMKSA_REPLACE)
2256 deauth = 1;
2257 }
2258
2259 if (reason == PMKSA_EXPIRE &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002260 (sm->pmk_len == entry->pmk_len &&
2261 os_memcmp(sm->pmk, entry->pmk, sm->pmk_len) == 0)) {
2262 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002263 "RSN: deauthenticating due to expired PMK");
2264 pmksa_cache_clear_current(sm);
2265 deauth = 1;
2266 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002267
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002268 if (deauth) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002269 os_memset(sm->pmk, 0, sizeof(sm->pmk));
2270 wpa_sm_deauthenticate(sm, WLAN_REASON_UNSPECIFIED);
2271 }
2272}
2273
2274
2275/**
2276 * wpa_sm_init - Initialize WPA state machine
2277 * @ctx: Context pointer for callbacks; this needs to be an allocated buffer
2278 * Returns: Pointer to the allocated WPA state machine data
2279 *
2280 * This function is used to allocate a new WPA state machine and the returned
2281 * value is passed to all WPA state machine calls.
2282 */
2283struct wpa_sm * wpa_sm_init(struct wpa_sm_ctx *ctx)
2284{
2285 struct wpa_sm *sm;
2286
2287 sm = os_zalloc(sizeof(*sm));
2288 if (sm == NULL)
2289 return NULL;
2290 dl_list_init(&sm->pmksa_candidates);
2291 sm->renew_snonce = 1;
2292 sm->ctx = ctx;
2293
2294 sm->dot11RSNAConfigPMKLifetime = 43200;
2295 sm->dot11RSNAConfigPMKReauthThreshold = 70;
2296 sm->dot11RSNAConfigSATimeout = 60;
2297
2298 sm->pmksa = pmksa_cache_init(wpa_sm_pmksa_free_cb, sm, sm);
2299 if (sm->pmksa == NULL) {
2300 wpa_msg(sm->ctx->msg_ctx, MSG_ERROR,
2301 "RSN: PMKSA cache initialization failed");
2302 os_free(sm);
2303 return NULL;
2304 }
2305
2306 return sm;
2307}
2308
2309
2310/**
2311 * wpa_sm_deinit - Deinitialize WPA state machine
2312 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2313 */
2314void wpa_sm_deinit(struct wpa_sm *sm)
2315{
2316 if (sm == NULL)
2317 return;
2318 pmksa_cache_deinit(sm->pmksa);
2319 eloop_cancel_timeout(wpa_sm_start_preauth, sm, NULL);
2320 eloop_cancel_timeout(wpa_sm_rekey_ptk, sm, NULL);
2321 os_free(sm->assoc_wpa_ie);
2322 os_free(sm->ap_wpa_ie);
2323 os_free(sm->ap_rsn_ie);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002324 wpa_sm_drop_sa(sm);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002325 os_free(sm->ctx);
2326 peerkey_deinit(sm);
2327#ifdef CONFIG_IEEE80211R
2328 os_free(sm->assoc_resp_ies);
2329#endif /* CONFIG_IEEE80211R */
Dmitry Shmidt55840ad2015-12-14 12:45:46 -08002330#ifdef CONFIG_TESTING_OPTIONS
2331 wpabuf_free(sm->test_assoc_ie);
2332#endif /* CONFIG_TESTING_OPTIONS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002333 os_free(sm);
2334}
2335
2336
2337/**
2338 * wpa_sm_notify_assoc - Notify WPA state machine about association
2339 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2340 * @bssid: The BSSID of the new association
2341 *
2342 * This function is called to let WPA state machine know that the connection
2343 * was established.
2344 */
2345void wpa_sm_notify_assoc(struct wpa_sm *sm, const u8 *bssid)
2346{
Mathy Vanhoef480c21c2017-07-12 16:03:24 +02002347 int clear_keys = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002348
2349 if (sm == NULL)
2350 return;
2351
2352 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2353 "WPA: Association event - clear replay counter");
2354 os_memcpy(sm->bssid, bssid, ETH_ALEN);
2355 os_memset(sm->rx_replay_counter, 0, WPA_REPLAY_COUNTER_LEN);
2356 sm->rx_replay_counter_set = 0;
2357 sm->renew_snonce = 1;
2358 if (os_memcmp(sm->preauth_bssid, bssid, ETH_ALEN) == 0)
2359 rsn_preauth_deinit(sm);
2360
2361#ifdef CONFIG_IEEE80211R
2362 if (wpa_ft_is_completed(sm)) {
2363 /*
2364 * Clear portValid to kick EAPOL state machine to re-enter
2365 * AUTHENTICATED state to get the EAPOL port Authorized.
2366 */
2367 eapol_sm_notify_portValid(sm->eapol, FALSE);
2368 wpa_supplicant_key_neg_complete(sm, sm->bssid, 1);
2369
2370 /* Prepare for the next transition */
2371 wpa_ft_prepare_auth_request(sm, NULL);
2372
Mathy Vanhoef480c21c2017-07-12 16:03:24 +02002373 clear_keys = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002374 }
2375#endif /* CONFIG_IEEE80211R */
2376
Mathy Vanhoef480c21c2017-07-12 16:03:24 +02002377 if (clear_keys) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002378 /*
2379 * IEEE 802.11, 8.4.10: Delete PTK SA on (re)association if
2380 * this is not part of a Fast BSS Transition.
2381 */
2382 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Clear old PTK");
2383 sm->ptk_set = 0;
Dmitry Shmidt61593f02014-04-21 16:27:35 -07002384 os_memset(&sm->ptk, 0, sizeof(sm->ptk));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002385 sm->tptk_set = 0;
Dmitry Shmidt61593f02014-04-21 16:27:35 -07002386 os_memset(&sm->tptk, 0, sizeof(sm->tptk));
Mathy Vanhoef480c21c2017-07-12 16:03:24 +02002387 os_memset(&sm->gtk, 0, sizeof(sm->gtk));
2388#ifdef CONFIG_IEEE80211W
2389 os_memset(&sm->igtk, 0, sizeof(sm->igtk));
2390#endif /* CONFIG_IEEE80211W */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002391 }
2392
2393#ifdef CONFIG_TDLS
2394 wpa_tdls_assoc(sm);
2395#endif /* CONFIG_TDLS */
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002396
2397#ifdef CONFIG_P2P
2398 os_memset(sm->p2p_ip_addr, 0, sizeof(sm->p2p_ip_addr));
2399#endif /* CONFIG_P2P */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002400}
2401
2402
2403/**
2404 * wpa_sm_notify_disassoc - Notify WPA state machine about disassociation
2405 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2406 *
2407 * This function is called to let WPA state machine know that the connection
2408 * was lost. This will abort any existing pre-authentication session.
2409 */
2410void wpa_sm_notify_disassoc(struct wpa_sm *sm)
2411{
Dmitry Shmidt4dd28dc2015-03-10 11:21:43 -07002412 eloop_cancel_timeout(wpa_sm_start_preauth, sm, NULL);
2413 eloop_cancel_timeout(wpa_sm_rekey_ptk, sm, NULL);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002414 peerkey_deinit(sm);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002415 rsn_preauth_deinit(sm);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002416 pmksa_cache_clear_current(sm);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002417 if (wpa_sm_get_state(sm) == WPA_4WAY_HANDSHAKE)
2418 sm->dot11RSNA4WayHandshakeFailures++;
2419#ifdef CONFIG_TDLS
2420 wpa_tdls_disassoc(sm);
2421#endif /* CONFIG_TDLS */
Jouni Malinene01e4802017-09-22 12:06:37 +03002422#ifdef CONFIG_IEEE80211R
2423 sm->ft_reassoc_completed = 0;
2424#endif /* CONFIG_IEEE80211R */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002425 /* Keys are not needed in the WPA state machine anymore */
2426 wpa_sm_drop_sa(sm);
Dmitry Shmidt4dd28dc2015-03-10 11:21:43 -07002427
2428 sm->msg_3_of_4_ok = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002429}
2430
2431
2432/**
2433 * wpa_sm_set_pmk - Set PMK
2434 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2435 * @pmk: The new PMK
2436 * @pmk_len: The length of the new PMK in bytes
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08002437 * @pmkid: Calculated PMKID
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002438 * @bssid: AA to add into PMKSA cache or %NULL to not cache the PMK
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002439 *
2440 * Configure the PMK for WPA state machine.
2441 */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002442void wpa_sm_set_pmk(struct wpa_sm *sm, const u8 *pmk, size_t pmk_len,
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08002443 const u8 *pmkid, const u8 *bssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002444{
2445 if (sm == NULL)
2446 return;
2447
2448 sm->pmk_len = pmk_len;
2449 os_memcpy(sm->pmk, pmk, pmk_len);
2450
2451#ifdef CONFIG_IEEE80211R
2452 /* Set XXKey to be PSK for FT key derivation */
2453 sm->xxkey_len = pmk_len;
2454 os_memcpy(sm->xxkey, pmk, pmk_len);
2455#endif /* CONFIG_IEEE80211R */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002456
2457 if (bssid) {
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08002458 pmksa_cache_add(sm->pmksa, pmk, pmk_len, pmkid, NULL, 0,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002459 bssid, sm->own_addr,
2460 sm->network_ctx, sm->key_mgmt);
2461 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002462}
2463
2464
2465/**
2466 * wpa_sm_set_pmk_from_pmksa - Set PMK based on the current PMKSA
2467 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2468 *
2469 * Take the PMK from the current PMKSA into use. If no PMKSA is active, the PMK
2470 * will be cleared.
2471 */
2472void wpa_sm_set_pmk_from_pmksa(struct wpa_sm *sm)
2473{
2474 if (sm == NULL)
2475 return;
2476
2477 if (sm->cur_pmksa) {
2478 sm->pmk_len = sm->cur_pmksa->pmk_len;
2479 os_memcpy(sm->pmk, sm->cur_pmksa->pmk, sm->pmk_len);
2480 } else {
2481 sm->pmk_len = PMK_LEN;
2482 os_memset(sm->pmk, 0, PMK_LEN);
2483 }
2484}
2485
2486
2487/**
2488 * wpa_sm_set_fast_reauth - Set fast reauthentication (EAP) enabled/disabled
2489 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2490 * @fast_reauth: Whether fast reauthentication (EAP) is allowed
2491 */
2492void wpa_sm_set_fast_reauth(struct wpa_sm *sm, int fast_reauth)
2493{
2494 if (sm)
2495 sm->fast_reauth = fast_reauth;
2496}
2497
2498
2499/**
2500 * wpa_sm_set_scard_ctx - Set context pointer for smartcard callbacks
2501 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2502 * @scard_ctx: Context pointer for smartcard related callback functions
2503 */
2504void wpa_sm_set_scard_ctx(struct wpa_sm *sm, void *scard_ctx)
2505{
2506 if (sm == NULL)
2507 return;
2508 sm->scard_ctx = scard_ctx;
2509 if (sm->preauth_eapol)
2510 eapol_sm_register_scard_ctx(sm->preauth_eapol, scard_ctx);
2511}
2512
2513
2514/**
2515 * wpa_sm_set_config - Notification of current configration change
2516 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2517 * @config: Pointer to current network configuration
2518 *
2519 * Notify WPA state machine that configuration has changed. config will be
2520 * stored as a backpointer to network configuration. This can be %NULL to clear
2521 * the stored pointed.
2522 */
2523void wpa_sm_set_config(struct wpa_sm *sm, struct rsn_supp_config *config)
2524{
2525 if (!sm)
2526 return;
2527
2528 if (config) {
2529 sm->network_ctx = config->network_ctx;
2530 sm->peerkey_enabled = config->peerkey_enabled;
2531 sm->allowed_pairwise_cipher = config->allowed_pairwise_cipher;
2532 sm->proactive_key_caching = config->proactive_key_caching;
2533 sm->eap_workaround = config->eap_workaround;
2534 sm->eap_conf_ctx = config->eap_conf_ctx;
2535 if (config->ssid) {
2536 os_memcpy(sm->ssid, config->ssid, config->ssid_len);
2537 sm->ssid_len = config->ssid_len;
2538 } else
2539 sm->ssid_len = 0;
2540 sm->wpa_ptk_rekey = config->wpa_ptk_rekey;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002541 sm->p2p = config->p2p;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002542 sm->wpa_rsc_relaxation = config->wpa_rsc_relaxation;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002543 } else {
2544 sm->network_ctx = NULL;
2545 sm->peerkey_enabled = 0;
2546 sm->allowed_pairwise_cipher = 0;
2547 sm->proactive_key_caching = 0;
2548 sm->eap_workaround = 0;
2549 sm->eap_conf_ctx = NULL;
2550 sm->ssid_len = 0;
2551 sm->wpa_ptk_rekey = 0;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002552 sm->p2p = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002553 sm->wpa_rsc_relaxation = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002554 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002555}
2556
2557
2558/**
2559 * wpa_sm_set_own_addr - Set own MAC address
2560 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2561 * @addr: Own MAC address
2562 */
2563void wpa_sm_set_own_addr(struct wpa_sm *sm, const u8 *addr)
2564{
2565 if (sm)
2566 os_memcpy(sm->own_addr, addr, ETH_ALEN);
2567}
2568
2569
2570/**
2571 * wpa_sm_set_ifname - Set network interface name
2572 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2573 * @ifname: Interface name
2574 * @bridge_ifname: Optional bridge interface name (for pre-auth)
2575 */
2576void wpa_sm_set_ifname(struct wpa_sm *sm, const char *ifname,
2577 const char *bridge_ifname)
2578{
2579 if (sm) {
2580 sm->ifname = ifname;
2581 sm->bridge_ifname = bridge_ifname;
2582 }
2583}
2584
2585
2586/**
2587 * wpa_sm_set_eapol - Set EAPOL state machine pointer
2588 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2589 * @eapol: Pointer to EAPOL state machine allocated with eapol_sm_init()
2590 */
2591void wpa_sm_set_eapol(struct wpa_sm *sm, struct eapol_sm *eapol)
2592{
2593 if (sm)
2594 sm->eapol = eapol;
2595}
2596
2597
2598/**
2599 * wpa_sm_set_param - Set WPA state machine parameters
2600 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2601 * @param: Parameter field
2602 * @value: Parameter value
2603 * Returns: 0 on success, -1 on failure
2604 */
2605int wpa_sm_set_param(struct wpa_sm *sm, enum wpa_sm_conf_params param,
2606 unsigned int value)
2607{
2608 int ret = 0;
2609
2610 if (sm == NULL)
2611 return -1;
2612
2613 switch (param) {
2614 case RSNA_PMK_LIFETIME:
2615 if (value > 0)
2616 sm->dot11RSNAConfigPMKLifetime = value;
2617 else
2618 ret = -1;
2619 break;
2620 case RSNA_PMK_REAUTH_THRESHOLD:
2621 if (value > 0 && value <= 100)
2622 sm->dot11RSNAConfigPMKReauthThreshold = value;
2623 else
2624 ret = -1;
2625 break;
2626 case RSNA_SA_TIMEOUT:
2627 if (value > 0)
2628 sm->dot11RSNAConfigSATimeout = value;
2629 else
2630 ret = -1;
2631 break;
2632 case WPA_PARAM_PROTO:
2633 sm->proto = value;
2634 break;
2635 case WPA_PARAM_PAIRWISE:
2636 sm->pairwise_cipher = value;
2637 break;
2638 case WPA_PARAM_GROUP:
2639 sm->group_cipher = value;
2640 break;
2641 case WPA_PARAM_KEY_MGMT:
2642 sm->key_mgmt = value;
2643 break;
2644#ifdef CONFIG_IEEE80211W
2645 case WPA_PARAM_MGMT_GROUP:
2646 sm->mgmt_group_cipher = value;
2647 break;
2648#endif /* CONFIG_IEEE80211W */
2649 case WPA_PARAM_RSN_ENABLED:
2650 sm->rsn_enabled = value;
2651 break;
2652 case WPA_PARAM_MFP:
2653 sm->mfp = value;
2654 break;
2655 default:
2656 break;
2657 }
2658
2659 return ret;
2660}
2661
2662
2663/**
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002664 * wpa_sm_get_status - Get WPA state machine
2665 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2666 * @buf: Buffer for status information
2667 * @buflen: Maximum buffer length
2668 * @verbose: Whether to include verbose status information
2669 * Returns: Number of bytes written to buf.
2670 *
2671 * Query WPA state machine for status information. This function fills in
2672 * a text area with current status information. If the buffer (buf) is not
2673 * large enough, status information will be truncated to fit the buffer.
2674 */
2675int wpa_sm_get_status(struct wpa_sm *sm, char *buf, size_t buflen,
2676 int verbose)
2677{
2678 char *pos = buf, *end = buf + buflen;
2679 int ret;
2680
2681 ret = os_snprintf(pos, end - pos,
2682 "pairwise_cipher=%s\n"
2683 "group_cipher=%s\n"
2684 "key_mgmt=%s\n",
2685 wpa_cipher_txt(sm->pairwise_cipher),
2686 wpa_cipher_txt(sm->group_cipher),
2687 wpa_key_mgmt_txt(sm->key_mgmt, sm->proto));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002688 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002689 return pos - buf;
2690 pos += ret;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002691
2692 if (sm->mfp != NO_MGMT_FRAME_PROTECTION && sm->ap_rsn_ie) {
2693 struct wpa_ie_data rsn;
2694 if (wpa_parse_wpa_ie_rsn(sm->ap_rsn_ie, sm->ap_rsn_ie_len, &rsn)
2695 >= 0 &&
2696 rsn.capabilities & (WPA_CAPABILITY_MFPR |
2697 WPA_CAPABILITY_MFPC)) {
2698 ret = os_snprintf(pos, end - pos, "pmf=%d\n",
2699 (rsn.capabilities &
2700 WPA_CAPABILITY_MFPR) ? 2 : 1);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002701 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002702 return pos - buf;
2703 pos += ret;
2704 }
2705 }
2706
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002707 return pos - buf;
2708}
2709
2710
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07002711int wpa_sm_pmf_enabled(struct wpa_sm *sm)
2712{
2713 struct wpa_ie_data rsn;
2714
2715 if (sm->mfp == NO_MGMT_FRAME_PROTECTION || !sm->ap_rsn_ie)
2716 return 0;
2717
2718 if (wpa_parse_wpa_ie_rsn(sm->ap_rsn_ie, sm->ap_rsn_ie_len, &rsn) >= 0 &&
2719 rsn.capabilities & (WPA_CAPABILITY_MFPR | WPA_CAPABILITY_MFPC))
2720 return 1;
2721
2722 return 0;
2723}
2724
2725
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002726/**
2727 * wpa_sm_set_assoc_wpa_ie_default - Generate own WPA/RSN IE from configuration
2728 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2729 * @wpa_ie: Pointer to buffer for WPA/RSN IE
2730 * @wpa_ie_len: Pointer to the length of the wpa_ie buffer
2731 * Returns: 0 on success, -1 on failure
2732 */
2733int wpa_sm_set_assoc_wpa_ie_default(struct wpa_sm *sm, u8 *wpa_ie,
2734 size_t *wpa_ie_len)
2735{
2736 int res;
2737
2738 if (sm == NULL)
2739 return -1;
2740
Dmitry Shmidt55840ad2015-12-14 12:45:46 -08002741#ifdef CONFIG_TESTING_OPTIONS
2742 if (sm->test_assoc_ie) {
2743 wpa_printf(MSG_DEBUG,
2744 "TESTING: Replace association WPA/RSN IE");
2745 if (*wpa_ie_len < wpabuf_len(sm->test_assoc_ie))
2746 return -1;
2747 os_memcpy(wpa_ie, wpabuf_head(sm->test_assoc_ie),
2748 wpabuf_len(sm->test_assoc_ie));
2749 res = wpabuf_len(sm->test_assoc_ie);
2750 } else
2751#endif /* CONFIG_TESTING_OPTIONS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002752 res = wpa_gen_wpa_ie(sm, wpa_ie, *wpa_ie_len);
2753 if (res < 0)
2754 return -1;
2755 *wpa_ie_len = res;
2756
2757 wpa_hexdump(MSG_DEBUG, "WPA: Set own WPA IE default",
2758 wpa_ie, *wpa_ie_len);
2759
2760 if (sm->assoc_wpa_ie == NULL) {
2761 /*
2762 * Make a copy of the WPA/RSN IE so that 4-Way Handshake gets
2763 * the correct version of the IE even if PMKSA caching is
2764 * aborted (which would remove PMKID from IE generation).
2765 */
2766 sm->assoc_wpa_ie = os_malloc(*wpa_ie_len);
2767 if (sm->assoc_wpa_ie == NULL)
2768 return -1;
2769
2770 os_memcpy(sm->assoc_wpa_ie, wpa_ie, *wpa_ie_len);
2771 sm->assoc_wpa_ie_len = *wpa_ie_len;
2772 }
2773
2774 return 0;
2775}
2776
2777
2778/**
2779 * wpa_sm_set_assoc_wpa_ie - Set own WPA/RSN IE from (Re)AssocReq
2780 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2781 * @ie: Pointer to IE data (starting from id)
2782 * @len: IE length
2783 * Returns: 0 on success, -1 on failure
2784 *
2785 * Inform WPA state machine about the WPA/RSN IE used in (Re)Association
2786 * Request frame. The IE will be used to override the default value generated
2787 * with wpa_sm_set_assoc_wpa_ie_default().
2788 */
2789int wpa_sm_set_assoc_wpa_ie(struct wpa_sm *sm, const u8 *ie, size_t len)
2790{
2791 if (sm == NULL)
2792 return -1;
2793
2794 os_free(sm->assoc_wpa_ie);
2795 if (ie == NULL || len == 0) {
2796 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2797 "WPA: clearing own WPA/RSN IE");
2798 sm->assoc_wpa_ie = NULL;
2799 sm->assoc_wpa_ie_len = 0;
2800 } else {
2801 wpa_hexdump(MSG_DEBUG, "WPA: set own WPA/RSN IE", ie, len);
2802 sm->assoc_wpa_ie = os_malloc(len);
2803 if (sm->assoc_wpa_ie == NULL)
2804 return -1;
2805
2806 os_memcpy(sm->assoc_wpa_ie, ie, len);
2807 sm->assoc_wpa_ie_len = len;
2808 }
2809
2810 return 0;
2811}
2812
2813
2814/**
2815 * wpa_sm_set_ap_wpa_ie - Set AP WPA IE from Beacon/ProbeResp
2816 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2817 * @ie: Pointer to IE data (starting from id)
2818 * @len: IE length
2819 * Returns: 0 on success, -1 on failure
2820 *
2821 * Inform WPA state machine about the WPA IE used in Beacon / Probe Response
2822 * frame.
2823 */
2824int wpa_sm_set_ap_wpa_ie(struct wpa_sm *sm, const u8 *ie, size_t len)
2825{
2826 if (sm == NULL)
2827 return -1;
2828
2829 os_free(sm->ap_wpa_ie);
2830 if (ie == NULL || len == 0) {
2831 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2832 "WPA: clearing AP WPA IE");
2833 sm->ap_wpa_ie = NULL;
2834 sm->ap_wpa_ie_len = 0;
2835 } else {
2836 wpa_hexdump(MSG_DEBUG, "WPA: set AP WPA IE", ie, len);
2837 sm->ap_wpa_ie = os_malloc(len);
2838 if (sm->ap_wpa_ie == NULL)
2839 return -1;
2840
2841 os_memcpy(sm->ap_wpa_ie, ie, len);
2842 sm->ap_wpa_ie_len = len;
2843 }
2844
2845 return 0;
2846}
2847
2848
2849/**
2850 * wpa_sm_set_ap_rsn_ie - Set AP RSN IE from Beacon/ProbeResp
2851 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2852 * @ie: Pointer to IE data (starting from id)
2853 * @len: IE length
2854 * Returns: 0 on success, -1 on failure
2855 *
2856 * Inform WPA state machine about the RSN IE used in Beacon / Probe Response
2857 * frame.
2858 */
2859int wpa_sm_set_ap_rsn_ie(struct wpa_sm *sm, const u8 *ie, size_t len)
2860{
2861 if (sm == NULL)
2862 return -1;
2863
2864 os_free(sm->ap_rsn_ie);
2865 if (ie == NULL || len == 0) {
2866 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2867 "WPA: clearing AP RSN IE");
2868 sm->ap_rsn_ie = NULL;
2869 sm->ap_rsn_ie_len = 0;
2870 } else {
2871 wpa_hexdump(MSG_DEBUG, "WPA: set AP RSN IE", ie, len);
2872 sm->ap_rsn_ie = os_malloc(len);
2873 if (sm->ap_rsn_ie == NULL)
2874 return -1;
2875
2876 os_memcpy(sm->ap_rsn_ie, ie, len);
2877 sm->ap_rsn_ie_len = len;
2878 }
2879
2880 return 0;
2881}
2882
2883
2884/**
2885 * wpa_sm_parse_own_wpa_ie - Parse own WPA/RSN IE
2886 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2887 * @data: Pointer to data area for parsing results
2888 * Returns: 0 on success, -1 if IE is not known, or -2 on parsing failure
2889 *
2890 * Parse the contents of the own WPA or RSN IE from (Re)AssocReq and write the
2891 * parsed data into data.
2892 */
2893int wpa_sm_parse_own_wpa_ie(struct wpa_sm *sm, struct wpa_ie_data *data)
2894{
2895 if (sm == NULL)
2896 return -1;
2897
2898 if (sm->assoc_wpa_ie == NULL) {
2899 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2900 "WPA: No WPA/RSN IE available from association info");
2901 return -1;
2902 }
2903 if (wpa_parse_wpa_ie(sm->assoc_wpa_ie, sm->assoc_wpa_ie_len, data))
2904 return -2;
2905 return 0;
2906}
2907
2908
2909int wpa_sm_pmksa_cache_list(struct wpa_sm *sm, char *buf, size_t len)
2910{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002911 return pmksa_cache_list(sm->pmksa, buf, len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002912}
2913
2914
2915void wpa_sm_drop_sa(struct wpa_sm *sm)
2916{
2917 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Clear old PMK and PTK");
2918 sm->ptk_set = 0;
2919 sm->tptk_set = 0;
2920 os_memset(sm->pmk, 0, sizeof(sm->pmk));
2921 os_memset(&sm->ptk, 0, sizeof(sm->ptk));
2922 os_memset(&sm->tptk, 0, sizeof(sm->tptk));
Mathy Vanhoef480c21c2017-07-12 16:03:24 +02002923 os_memset(&sm->gtk, 0, sizeof(sm->gtk));
2924#ifdef CONFIG_IEEE80211W
2925 os_memset(&sm->igtk, 0, sizeof(sm->igtk));
2926#endif /* CONFIG_IEEE80211W */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002927#ifdef CONFIG_IEEE80211R
2928 os_memset(sm->xxkey, 0, sizeof(sm->xxkey));
2929 os_memset(sm->pmk_r0, 0, sizeof(sm->pmk_r0));
2930 os_memset(sm->pmk_r1, 0, sizeof(sm->pmk_r1));
2931#endif /* CONFIG_IEEE80211R */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002932}
2933
2934
2935int wpa_sm_has_ptk(struct wpa_sm *sm)
2936{
2937 if (sm == NULL)
2938 return 0;
2939 return sm->ptk_set;
2940}
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002941
2942
2943void wpa_sm_update_replay_ctr(struct wpa_sm *sm, const u8 *replay_ctr)
2944{
2945 os_memcpy(sm->rx_replay_counter, replay_ctr, WPA_REPLAY_COUNTER_LEN);
2946}
2947
2948
2949void wpa_sm_pmksa_cache_flush(struct wpa_sm *sm, void *network_ctx)
2950{
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07002951 pmksa_cache_flush(sm->pmksa, network_ctx, NULL, 0);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002952}
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002953
2954
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002955#ifdef CONFIG_WNM
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002956int wpa_wnmsleep_install_key(struct wpa_sm *sm, u8 subelem_id, u8 *buf)
2957{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002958 u16 keyinfo;
2959 u8 keylen; /* plaintext key len */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002960 u8 *key_rsc;
2961
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002962 if (subelem_id == WNM_SLEEP_SUBELEM_GTK) {
Dmitry Shmidt61593f02014-04-21 16:27:35 -07002963 struct wpa_gtk_data gd;
2964
2965 os_memset(&gd, 0, sizeof(gd));
2966 keylen = wpa_cipher_key_len(sm->group_cipher);
2967 gd.key_rsc_len = wpa_cipher_rsc_len(sm->group_cipher);
2968 gd.alg = wpa_cipher_to_alg(sm->group_cipher);
2969 if (gd.alg == WPA_ALG_NONE) {
2970 wpa_printf(MSG_DEBUG, "Unsupported group cipher suite");
2971 return -1;
2972 }
2973
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002974 key_rsc = buf + 5;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002975 keyinfo = WPA_GET_LE16(buf + 2);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002976 gd.gtk_len = keylen;
2977 if (gd.gtk_len != buf[4]) {
2978 wpa_printf(MSG_DEBUG, "GTK len mismatch len %d vs %d",
2979 gd.gtk_len, buf[4]);
2980 return -1;
2981 }
2982 gd.keyidx = keyinfo & 0x03; /* B0 - B1 */
2983 gd.tx = wpa_supplicant_gtk_tx_bit_workaround(
2984 sm, !!(keyinfo & WPA_KEY_INFO_TXRX));
2985
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002986 os_memcpy(gd.gtk, buf + 13, gd.gtk_len);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002987
2988 wpa_hexdump_key(MSG_DEBUG, "Install GTK (WNM SLEEP)",
2989 gd.gtk, gd.gtk_len);
2990 if (wpa_supplicant_install_gtk(sm, &gd, key_rsc)) {
Dmitry Shmidt61593f02014-04-21 16:27:35 -07002991 os_memset(&gd, 0, sizeof(gd));
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002992 wpa_printf(MSG_DEBUG, "Failed to install the GTK in "
2993 "WNM mode");
2994 return -1;
2995 }
Dmitry Shmidt61593f02014-04-21 16:27:35 -07002996 os_memset(&gd, 0, sizeof(gd));
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002997#ifdef CONFIG_IEEE80211W
2998 } else if (subelem_id == WNM_SLEEP_SUBELEM_IGTK) {
Mathy Vanhoef480c21c2017-07-12 16:03:24 +02002999 const struct wpa_igtk_kde *igtk;
Dmitry Shmidt61593f02014-04-21 16:27:35 -07003000
Mathy Vanhoef480c21c2017-07-12 16:03:24 +02003001 igtk = (const struct wpa_igtk_kde *) (buf + 2);
3002 if (wpa_supplicant_install_igtk(sm, igtk) < 0)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003003 return -1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003004#endif /* CONFIG_IEEE80211W */
3005 } else {
3006 wpa_printf(MSG_DEBUG, "Unknown element id");
3007 return -1;
3008 }
3009
3010 return 0;
3011}
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003012#endif /* CONFIG_WNM */
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003013
3014
3015#ifdef CONFIG_PEERKEY
3016int wpa_sm_rx_eapol_peerkey(struct wpa_sm *sm, const u8 *src_addr,
3017 const u8 *buf, size_t len)
3018{
3019 struct wpa_peerkey *peerkey;
3020
3021 for (peerkey = sm->peerkey; peerkey; peerkey = peerkey->next) {
3022 if (os_memcmp(peerkey->addr, src_addr, ETH_ALEN) == 0)
3023 break;
3024 }
3025
3026 if (!peerkey)
3027 return 0;
3028
3029 wpa_sm_rx_eapol(sm, src_addr, buf, len);
3030
3031 return 1;
3032}
3033#endif /* CONFIG_PEERKEY */
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003034
3035
3036#ifdef CONFIG_P2P
3037
3038int wpa_sm_get_p2p_ip_addr(struct wpa_sm *sm, u8 *buf)
3039{
3040 if (sm == NULL || WPA_GET_BE32(sm->p2p_ip_addr) == 0)
3041 return -1;
3042 os_memcpy(buf, sm->p2p_ip_addr, 3 * 4);
3043 return 0;
3044}
3045
3046#endif /* CONFIG_P2P */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003047
3048
3049void wpa_sm_set_rx_replay_ctr(struct wpa_sm *sm, const u8 *rx_replay_counter)
3050{
3051 if (rx_replay_counter == NULL)
3052 return;
3053
3054 os_memcpy(sm->rx_replay_counter, rx_replay_counter,
3055 WPA_REPLAY_COUNTER_LEN);
3056 sm->rx_replay_counter_set = 1;
3057 wpa_printf(MSG_DEBUG, "Updated key replay counter");
3058}
3059
3060
Dmitry Shmidt807291d2015-01-27 13:40:23 -08003061void wpa_sm_set_ptk_kck_kek(struct wpa_sm *sm,
3062 const u8 *ptk_kck, size_t ptk_kck_len,
3063 const u8 *ptk_kek, size_t ptk_kek_len)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003064{
Dmitry Shmidt807291d2015-01-27 13:40:23 -08003065 if (ptk_kck && ptk_kck_len <= WPA_KCK_MAX_LEN) {
3066 os_memcpy(sm->ptk.kck, ptk_kck, ptk_kck_len);
3067 sm->ptk.kck_len = ptk_kck_len;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003068 wpa_printf(MSG_DEBUG, "Updated PTK KCK");
3069 }
Dmitry Shmidt807291d2015-01-27 13:40:23 -08003070 if (ptk_kek && ptk_kek_len <= WPA_KEK_MAX_LEN) {
3071 os_memcpy(sm->ptk.kek, ptk_kek, ptk_kek_len);
3072 sm->ptk.kek_len = ptk_kek_len;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003073 wpa_printf(MSG_DEBUG, "Updated PTK KEK");
3074 }
3075 sm->ptk_set = 1;
3076}
Dmitry Shmidt55840ad2015-12-14 12:45:46 -08003077
3078
3079#ifdef CONFIG_TESTING_OPTIONS
3080void wpa_sm_set_test_assoc_ie(struct wpa_sm *sm, struct wpabuf *buf)
3081{
3082 wpabuf_free(sm->test_assoc_ie);
3083 sm->test_assoc_ie = buf;
3084}
3085#endif /* CONFIG_TESTING_OPTIONS */