blob: 63fe6b7b3f7f8e4db61c3709172a0315b52e5dbe [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 Shmidt8d520ff2011-05-09 14:06:53 -07004 *
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007 */
8
9#include "includes.h"
10
11#include "common.h"
12#include "crypto/aes_wrap.h"
13#include "crypto/crypto.h"
14#include "crypto/random.h"
15#include "common/ieee802_11_defs.h"
16#include "eapol_supp/eapol_supp_sm.h"
17#include "wpa.h"
18#include "eloop.h"
19#include "preauth.h"
20#include "pmksa_cache.h"
21#include "wpa_i.h"
22#include "wpa_ie.h"
23#include "peerkey.h"
24
25
26/**
27 * wpa_eapol_key_send - Send WPA/RSN EAPOL-Key message
28 * @sm: Pointer to WPA state machine data from wpa_sm_init()
29 * @kck: Key Confirmation Key (KCK, part of PTK)
Dmitry Shmidt807291d2015-01-27 13:40:23 -080030 * @kck_len: KCK length in octets
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070031 * @ver: Version field from Key Info
32 * @dest: Destination address for the frame
33 * @proto: Ethertype (usually ETH_P_EAPOL)
34 * @msg: EAPOL-Key message
35 * @msg_len: Length of message
36 * @key_mic: Pointer to the buffer to which the EAPOL-Key MIC is written
37 */
Dmitry Shmidt807291d2015-01-27 13:40:23 -080038void wpa_eapol_key_send(struct wpa_sm *sm, const u8 *kck, size_t kck_len,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070039 int ver, const u8 *dest, u16 proto,
40 u8 *msg, size_t msg_len, u8 *key_mic)
41{
Dmitry Shmidt807291d2015-01-27 13:40:23 -080042 size_t mic_len = wpa_mic_len(sm->key_mgmt);
43
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070044 if (is_zero_ether_addr(dest) && is_zero_ether_addr(sm->bssid)) {
45 /*
46 * Association event was not yet received; try to fetch
47 * BSSID from the driver.
48 */
49 if (wpa_sm_get_bssid(sm, sm->bssid) < 0) {
50 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
51 "WPA: Failed to read BSSID for "
52 "EAPOL-Key destination address");
53 } else {
54 dest = sm->bssid;
55 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
56 "WPA: Use BSSID (" MACSTR
57 ") as the destination for EAPOL-Key",
58 MAC2STR(dest));
59 }
60 }
61 if (key_mic &&
Dmitry Shmidt807291d2015-01-27 13:40:23 -080062 wpa_eapol_key_mic(kck, kck_len, sm->key_mgmt, ver, msg, msg_len,
63 key_mic)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070064 wpa_msg(sm->ctx->msg_ctx, MSG_ERROR,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080065 "WPA: Failed to generate EAPOL-Key version %d key_mgmt 0x%x MIC",
66 ver, sm->key_mgmt);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070067 goto out;
68 }
Dmitry Shmidt807291d2015-01-27 13:40:23 -080069 wpa_hexdump_key(MSG_DEBUG, "WPA: KCK", kck, kck_len);
70 wpa_hexdump(MSG_DEBUG, "WPA: Derived Key MIC", key_mic, mic_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070071 wpa_hexdump(MSG_MSGDUMP, "WPA: TX EAPOL-Key", msg, msg_len);
72 wpa_sm_ether_send(sm, dest, proto, msg, msg_len);
73 eapol_sm_notify_tx_eapol_key(sm->eapol);
74out:
75 os_free(msg);
76}
77
78
79/**
80 * wpa_sm_key_request - Send EAPOL-Key Request
81 * @sm: Pointer to WPA state machine data from wpa_sm_init()
82 * @error: Indicate whether this is an Michael MIC error report
83 * @pairwise: 1 = error report for pairwise packet, 0 = for group packet
84 *
85 * Send an EAPOL-Key Request to the current authenticator. This function is
86 * used to request rekeying and it is usually called when a local Michael MIC
87 * failure is detected.
88 */
89void wpa_sm_key_request(struct wpa_sm *sm, int error, int pairwise)
90{
Dmitry Shmidt807291d2015-01-27 13:40:23 -080091 size_t mic_len, hdrlen, rlen;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070092 struct wpa_eapol_key *reply;
Dmitry Shmidt807291d2015-01-27 13:40:23 -080093 struct wpa_eapol_key_192 *reply192;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070094 int key_info, ver;
Dmitry Shmidt807291d2015-01-27 13:40:23 -080095 u8 bssid[ETH_ALEN], *rbuf, *key_mic;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070096
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080097 if (sm->key_mgmt == WPA_KEY_MGMT_OSEN ||
98 wpa_key_mgmt_suite_b(sm->key_mgmt))
Dmitry Shmidtf21452a2014-02-26 10:55:25 -080099 ver = WPA_KEY_INFO_TYPE_AKM_DEFINED;
100 else if (wpa_key_mgmt_ft(sm->key_mgmt) ||
101 wpa_key_mgmt_sha256(sm->key_mgmt))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700102 ver = WPA_KEY_INFO_TYPE_AES_128_CMAC;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700103 else if (sm->pairwise_cipher != WPA_CIPHER_TKIP)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700104 ver = WPA_KEY_INFO_TYPE_HMAC_SHA1_AES;
105 else
106 ver = WPA_KEY_INFO_TYPE_HMAC_MD5_RC4;
107
108 if (wpa_sm_get_bssid(sm, bssid) < 0) {
109 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
110 "Failed to read BSSID for EAPOL-Key request");
111 return;
112 }
113
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800114 mic_len = wpa_mic_len(sm->key_mgmt);
115 hdrlen = mic_len == 24 ? sizeof(*reply192) : sizeof(*reply);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700116 rbuf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_KEY, NULL,
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800117 hdrlen, &rlen, (void *) &reply);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700118 if (rbuf == NULL)
119 return;
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800120 reply192 = (struct wpa_eapol_key_192 *) reply;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700121
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800122 reply->type = (sm->proto == WPA_PROTO_RSN ||
123 sm->proto == WPA_PROTO_OSEN) ?
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700124 EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA;
125 key_info = WPA_KEY_INFO_REQUEST | ver;
126 if (sm->ptk_set)
127 key_info |= WPA_KEY_INFO_MIC;
128 if (error)
129 key_info |= WPA_KEY_INFO_ERROR;
130 if (pairwise)
131 key_info |= WPA_KEY_INFO_KEY_TYPE;
132 WPA_PUT_BE16(reply->key_info, key_info);
133 WPA_PUT_BE16(reply->key_length, 0);
134 os_memcpy(reply->replay_counter, sm->request_counter,
135 WPA_REPLAY_COUNTER_LEN);
136 inc_byte_array(sm->request_counter, WPA_REPLAY_COUNTER_LEN);
137
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800138 if (mic_len == 24)
139 WPA_PUT_BE16(reply192->key_data_length, 0);
140 else
141 WPA_PUT_BE16(reply->key_data_length, 0);
142 if (!(key_info & WPA_KEY_INFO_MIC))
143 key_mic = NULL;
144 else
145 key_mic = reply192->key_mic; /* same offset in reply */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700146
147 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
148 "WPA: Sending EAPOL-Key Request (error=%d "
149 "pairwise=%d ptk_set=%d len=%lu)",
150 error, pairwise, sm->ptk_set, (unsigned long) rlen);
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800151 wpa_eapol_key_send(sm, sm->ptk.kck, sm->ptk.kck_len, ver, bssid,
152 ETH_P_EAPOL, rbuf, rlen, key_mic);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700153}
154
155
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800156static void wpa_supplicant_key_mgmt_set_pmk(struct wpa_sm *sm)
157{
158#ifdef CONFIG_IEEE80211R
159 if (sm->key_mgmt == WPA_KEY_MGMT_FT_IEEE8021X) {
160 if (wpa_sm_key_mgmt_set_pmk(sm, sm->xxkey, sm->xxkey_len))
161 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
162 "RSN: Cannot set low order 256 bits of MSK for key management offload");
163 } else {
164#endif /* CONFIG_IEEE80211R */
165 if (wpa_sm_key_mgmt_set_pmk(sm, sm->pmk, sm->pmk_len))
166 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
167 "RSN: Cannot set PMK for key management offload");
168#ifdef CONFIG_IEEE80211R
169 }
170#endif /* CONFIG_IEEE80211R */
171}
172
173
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700174static int wpa_supplicant_get_pmk(struct wpa_sm *sm,
175 const unsigned char *src_addr,
176 const u8 *pmkid)
177{
178 int abort_cached = 0;
179
180 if (pmkid && !sm->cur_pmksa) {
181 /* When using drivers that generate RSN IE, wpa_supplicant may
182 * not have enough time to get the association information
183 * event before receiving this 1/4 message, so try to find a
184 * matching PMKSA cache entry here. */
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800185 sm->cur_pmksa = pmksa_cache_get(sm->pmksa, src_addr, pmkid,
186 NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700187 if (sm->cur_pmksa) {
188 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
189 "RSN: found matching PMKID from PMKSA cache");
190 } else {
191 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
192 "RSN: no matching PMKID found");
193 abort_cached = 1;
194 }
195 }
196
197 if (pmkid && sm->cur_pmksa &&
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700198 os_memcmp_const(pmkid, sm->cur_pmksa->pmkid, PMKID_LEN) == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700199 wpa_hexdump(MSG_DEBUG, "RSN: matched PMKID", pmkid, PMKID_LEN);
200 wpa_sm_set_pmk_from_pmksa(sm);
201 wpa_hexdump_key(MSG_DEBUG, "RSN: PMK from PMKSA cache",
202 sm->pmk, sm->pmk_len);
203 eapol_sm_notify_cached(sm->eapol);
204#ifdef CONFIG_IEEE80211R
205 sm->xxkey_len = 0;
206#endif /* CONFIG_IEEE80211R */
207 } else if (wpa_key_mgmt_wpa_ieee8021x(sm->key_mgmt) && sm->eapol) {
208 int res, pmk_len;
209 pmk_len = PMK_LEN;
210 res = eapol_sm_get_key(sm->eapol, sm->pmk, PMK_LEN);
211 if (res) {
212 /*
213 * EAP-LEAP is an exception from other EAP methods: it
214 * uses only 16-byte PMK.
215 */
216 res = eapol_sm_get_key(sm->eapol, sm->pmk, 16);
217 pmk_len = 16;
218 } else {
219#ifdef CONFIG_IEEE80211R
220 u8 buf[2 * PMK_LEN];
221 if (eapol_sm_get_key(sm->eapol, buf, 2 * PMK_LEN) == 0)
222 {
223 os_memcpy(sm->xxkey, buf + PMK_LEN, PMK_LEN);
224 sm->xxkey_len = PMK_LEN;
225 os_memset(buf, 0, sizeof(buf));
226 }
227#endif /* CONFIG_IEEE80211R */
228 }
229 if (res == 0) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700230 struct rsn_pmksa_cache_entry *sa = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700231 wpa_hexdump_key(MSG_DEBUG, "WPA: PMK from EAPOL state "
232 "machines", sm->pmk, pmk_len);
233 sm->pmk_len = pmk_len;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800234 wpa_supplicant_key_mgmt_set_pmk(sm);
Dmitry Shmidtc55524a2011-07-07 11:18:38 -0700235 if (sm->proto == WPA_PROTO_RSN &&
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800236 !wpa_key_mgmt_suite_b(sm->key_mgmt) &&
Dmitry Shmidtc55524a2011-07-07 11:18:38 -0700237 !wpa_key_mgmt_ft(sm->key_mgmt)) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700238 sa = pmksa_cache_add(sm->pmksa,
239 sm->pmk, pmk_len,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800240 NULL, 0,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700241 src_addr, sm->own_addr,
242 sm->network_ctx,
243 sm->key_mgmt);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700244 }
245 if (!sm->cur_pmksa && pmkid &&
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800246 pmksa_cache_get(sm->pmksa, src_addr, pmkid, NULL))
247 {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700248 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
249 "RSN: the new PMK matches with the "
250 "PMKID");
251 abort_cached = 0;
Jouni Malinen6ec30382015-07-08 20:48:18 +0300252 } else if (sa && !sm->cur_pmksa && pmkid) {
253 /*
254 * It looks like the authentication server
255 * derived mismatching MSK. This should not
256 * really happen, but bugs happen.. There is not
257 * much we can do here without knowing what
258 * exactly caused the server to misbehave.
259 */
260 wpa_dbg(sm->ctx->msg_ctx, MSG_INFO,
261 "RSN: PMKID mismatch - authentication server may have derived different MSK?!");
262 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700263 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700264
265 if (!sm->cur_pmksa)
266 sm->cur_pmksa = sa;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700267 } else {
268 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
269 "WPA: Failed to get master session key from "
270 "EAPOL state machines - key handshake "
271 "aborted");
272 if (sm->cur_pmksa) {
273 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
274 "RSN: Cancelled PMKSA caching "
275 "attempt");
276 sm->cur_pmksa = NULL;
277 abort_cached = 1;
278 } else if (!abort_cached) {
279 return -1;
280 }
281 }
282 }
283
Dmitry Shmidtc55524a2011-07-07 11:18:38 -0700284 if (abort_cached && wpa_key_mgmt_wpa_ieee8021x(sm->key_mgmt) &&
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800285 !wpa_key_mgmt_suite_b(sm->key_mgmt) &&
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800286 !wpa_key_mgmt_ft(sm->key_mgmt) && sm->key_mgmt != WPA_KEY_MGMT_OSEN)
287 {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700288 /* Send EAPOL-Start to trigger full EAP authentication. */
289 u8 *buf;
290 size_t buflen;
291
292 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
293 "RSN: no PMKSA entry found - trigger "
294 "full EAP authentication");
295 buf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_START,
296 NULL, 0, &buflen, NULL);
297 if (buf) {
298 wpa_sm_ether_send(sm, sm->bssid, ETH_P_EAPOL,
299 buf, buflen);
300 os_free(buf);
301 return -2;
302 }
303
304 return -1;
305 }
306
307 return 0;
308}
309
310
311/**
312 * wpa_supplicant_send_2_of_4 - Send message 2 of WPA/RSN 4-Way Handshake
313 * @sm: Pointer to WPA state machine data from wpa_sm_init()
314 * @dst: Destination address for the frame
315 * @key: Pointer to the EAPOL-Key frame header
316 * @ver: Version bits from EAPOL-Key Key Info
317 * @nonce: Nonce value for the EAPOL-Key frame
318 * @wpa_ie: WPA/RSN IE
319 * @wpa_ie_len: Length of the WPA/RSN IE
320 * @ptk: PTK to use for keyed hash and encryption
321 * Returns: 0 on success, -1 on failure
322 */
323int wpa_supplicant_send_2_of_4(struct wpa_sm *sm, const unsigned char *dst,
324 const struct wpa_eapol_key *key,
325 int ver, const u8 *nonce,
326 const u8 *wpa_ie, size_t wpa_ie_len,
327 struct wpa_ptk *ptk)
328{
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800329 size_t mic_len, hdrlen, rlen;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700330 struct wpa_eapol_key *reply;
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800331 struct wpa_eapol_key_192 *reply192;
332 u8 *rbuf, *key_mic;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700333 u8 *rsn_ie_buf = NULL;
334
335 if (wpa_ie == NULL) {
336 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, "WPA: No wpa_ie set - "
337 "cannot generate msg 2/4");
338 return -1;
339 }
340
341#ifdef CONFIG_IEEE80211R
342 if (wpa_key_mgmt_ft(sm->key_mgmt)) {
343 int res;
344
345 /*
346 * Add PMKR1Name into RSN IE (PMKID-List) and add MDIE and
347 * FTIE from (Re)Association Response.
348 */
349 rsn_ie_buf = os_malloc(wpa_ie_len + 2 + 2 + PMKID_LEN +
350 sm->assoc_resp_ies_len);
351 if (rsn_ie_buf == NULL)
352 return -1;
353 os_memcpy(rsn_ie_buf, wpa_ie, wpa_ie_len);
354 res = wpa_insert_pmkid(rsn_ie_buf, wpa_ie_len,
355 sm->pmk_r1_name);
356 if (res < 0) {
357 os_free(rsn_ie_buf);
358 return -1;
359 }
360 wpa_ie_len += res;
361
362 if (sm->assoc_resp_ies) {
363 os_memcpy(rsn_ie_buf + wpa_ie_len, sm->assoc_resp_ies,
364 sm->assoc_resp_ies_len);
365 wpa_ie_len += sm->assoc_resp_ies_len;
366 }
367
368 wpa_ie = rsn_ie_buf;
369 }
370#endif /* CONFIG_IEEE80211R */
371
372 wpa_hexdump(MSG_DEBUG, "WPA: WPA IE for msg 2/4", wpa_ie, wpa_ie_len);
373
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800374 mic_len = wpa_mic_len(sm->key_mgmt);
375 hdrlen = mic_len == 24 ? sizeof(*reply192) : sizeof(*reply);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700376 rbuf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_KEY,
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800377 NULL, hdrlen + wpa_ie_len,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700378 &rlen, (void *) &reply);
379 if (rbuf == NULL) {
380 os_free(rsn_ie_buf);
381 return -1;
382 }
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800383 reply192 = (struct wpa_eapol_key_192 *) reply;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700384
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800385 reply->type = (sm->proto == WPA_PROTO_RSN ||
386 sm->proto == WPA_PROTO_OSEN) ?
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700387 EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA;
388 WPA_PUT_BE16(reply->key_info,
389 ver | WPA_KEY_INFO_KEY_TYPE | WPA_KEY_INFO_MIC);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800390 if (sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700391 WPA_PUT_BE16(reply->key_length, 0);
392 else
393 os_memcpy(reply->key_length, key->key_length, 2);
394 os_memcpy(reply->replay_counter, key->replay_counter,
395 WPA_REPLAY_COUNTER_LEN);
396 wpa_hexdump(MSG_DEBUG, "WPA: Replay Counter", reply->replay_counter,
397 WPA_REPLAY_COUNTER_LEN);
398
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800399 key_mic = reply192->key_mic; /* same offset for reply and reply192 */
400 if (mic_len == 24) {
401 WPA_PUT_BE16(reply192->key_data_length, wpa_ie_len);
402 os_memcpy(reply192 + 1, wpa_ie, wpa_ie_len);
403 } else {
404 WPA_PUT_BE16(reply->key_data_length, wpa_ie_len);
405 os_memcpy(reply + 1, wpa_ie, wpa_ie_len);
406 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700407 os_free(rsn_ie_buf);
408
409 os_memcpy(reply->key_nonce, nonce, WPA_NONCE_LEN);
410
411 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Sending EAPOL-Key 2/4");
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800412 wpa_eapol_key_send(sm, ptk->kck, ptk->kck_len, ver, dst, ETH_P_EAPOL,
413 rbuf, rlen, key_mic);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700414
415 return 0;
416}
417
418
419static int wpa_derive_ptk(struct wpa_sm *sm, const unsigned char *src_addr,
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800420 const struct wpa_eapol_key *key, struct wpa_ptk *ptk)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700421{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700422#ifdef CONFIG_IEEE80211R
423 if (wpa_key_mgmt_ft(sm->key_mgmt))
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800424 return wpa_derive_ptk_ft(sm, src_addr, key, ptk);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700425#endif /* CONFIG_IEEE80211R */
426
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800427 return wpa_pmk_to_ptk(sm->pmk, sm->pmk_len, "Pairwise key expansion",
428 sm->own_addr, sm->bssid, sm->snonce,
429 key->key_nonce, ptk, sm->key_mgmt,
430 sm->pairwise_cipher);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700431}
432
433
434static void wpa_supplicant_process_1_of_4(struct wpa_sm *sm,
435 const unsigned char *src_addr,
436 const struct wpa_eapol_key *key,
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700437 u16 ver, const u8 *key_data,
438 size_t key_data_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700439{
440 struct wpa_eapol_ie_parse ie;
441 struct wpa_ptk *ptk;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700442 int res;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800443 u8 *kde, *kde_buf = NULL;
444 size_t kde_len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700445
446 if (wpa_sm_get_network_ctx(sm) == NULL) {
447 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, "WPA: No SSID info "
448 "found (msg 1 of 4)");
449 return;
450 }
451
452 wpa_sm_set_state(sm, WPA_4WAY_HANDSHAKE);
453 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: RX message 1 of 4-Way "
454 "Handshake from " MACSTR " (ver=%d)", MAC2STR(src_addr), ver);
455
456 os_memset(&ie, 0, sizeof(ie));
457
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800458 if (sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700459 /* RSN: msg 1/4 should contain PMKID for the selected PMK */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700460 wpa_hexdump(MSG_DEBUG, "RSN: msg 1/4 key data",
461 key_data, key_data_len);
462 if (wpa_supplicant_parse_ies(key_data, key_data_len, &ie) < 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800463 goto failed;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700464 if (ie.pmkid) {
465 wpa_hexdump(MSG_DEBUG, "RSN: PMKID from "
466 "Authenticator", ie.pmkid, PMKID_LEN);
467 }
468 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700469
470 res = wpa_supplicant_get_pmk(sm, src_addr, ie.pmkid);
471 if (res == -2) {
472 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "RSN: Do not reply to "
473 "msg 1/4 - requesting full EAP authentication");
474 return;
475 }
476 if (res)
477 goto failed;
478
479 if (sm->renew_snonce) {
480 if (random_get_bytes(sm->snonce, WPA_NONCE_LEN)) {
481 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
482 "WPA: Failed to get random data for SNonce");
483 goto failed;
484 }
485 sm->renew_snonce = 0;
486 wpa_hexdump(MSG_DEBUG, "WPA: Renewed SNonce",
487 sm->snonce, WPA_NONCE_LEN);
488 }
489
490 /* Calculate PTK which will be stored as a temporary PTK until it has
491 * been verified when processing message 3/4. */
492 ptk = &sm->tptk;
493 wpa_derive_ptk(sm, src_addr, key, ptk);
Dmitry Shmidt98660862014-03-11 17:26:21 -0700494 if (sm->pairwise_cipher == WPA_CIPHER_TKIP) {
Dmitry Shmidt61593f02014-04-21 16:27:35 -0700495 u8 buf[8];
Dmitry Shmidt98660862014-03-11 17:26:21 -0700496 /* Supplicant: swap tx/rx Mic keys */
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800497 os_memcpy(buf, &ptk->tk[16], 8);
498 os_memcpy(&ptk->tk[16], &ptk->tk[24], 8);
499 os_memcpy(&ptk->tk[24], buf, 8);
Dmitry Shmidt61593f02014-04-21 16:27:35 -0700500 os_memset(buf, 0, sizeof(buf));
Dmitry Shmidt98660862014-03-11 17:26:21 -0700501 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700502 sm->tptk_set = 1;
Jouni Malinen244b71b2015-10-01 18:51:04 +0300503 sm->tk_to_set = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700504
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800505 kde = sm->assoc_wpa_ie;
506 kde_len = sm->assoc_wpa_ie_len;
507
508#ifdef CONFIG_P2P
509 if (sm->p2p) {
510 kde_buf = os_malloc(kde_len + 2 + RSN_SELECTOR_LEN + 1);
511 if (kde_buf) {
512 u8 *pos;
513 wpa_printf(MSG_DEBUG, "P2P: Add IP Address Request KDE "
514 "into EAPOL-Key 2/4");
515 os_memcpy(kde_buf, kde, kde_len);
516 kde = kde_buf;
517 pos = kde + kde_len;
518 *pos++ = WLAN_EID_VENDOR_SPECIFIC;
519 *pos++ = RSN_SELECTOR_LEN + 1;
520 RSN_SELECTOR_PUT(pos, WFA_KEY_DATA_IP_ADDR_REQ);
521 pos += RSN_SELECTOR_LEN;
522 *pos++ = 0x01;
523 kde_len = pos - kde;
524 }
525 }
526#endif /* CONFIG_P2P */
527
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700528 if (wpa_supplicant_send_2_of_4(sm, sm->bssid, key, ver, sm->snonce,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800529 kde, kde_len, ptk))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700530 goto failed;
531
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800532 os_free(kde_buf);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700533 os_memcpy(sm->anonce, key->key_nonce, WPA_NONCE_LEN);
534 return;
535
536failed:
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800537 os_free(kde_buf);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700538 wpa_sm_deauthenticate(sm, WLAN_REASON_UNSPECIFIED);
539}
540
541
542static void wpa_sm_start_preauth(void *eloop_ctx, void *timeout_ctx)
543{
544 struct wpa_sm *sm = eloop_ctx;
545 rsn_preauth_candidate_process(sm);
546}
547
548
549static void wpa_supplicant_key_neg_complete(struct wpa_sm *sm,
550 const u8 *addr, int secure)
551{
552 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
553 "WPA: Key negotiation completed with "
554 MACSTR " [PTK=%s GTK=%s]", MAC2STR(addr),
555 wpa_cipher_txt(sm->pairwise_cipher),
556 wpa_cipher_txt(sm->group_cipher));
557 wpa_sm_cancel_auth_timeout(sm);
558 wpa_sm_set_state(sm, WPA_COMPLETED);
559
560 if (secure) {
561 wpa_sm_mlme_setprotection(
562 sm, addr, MLME_SETPROTECTION_PROTECT_TYPE_RX_TX,
563 MLME_SETPROTECTION_KEY_TYPE_PAIRWISE);
564 eapol_sm_notify_portValid(sm->eapol, TRUE);
565 if (wpa_key_mgmt_wpa_psk(sm->key_mgmt))
566 eapol_sm_notify_eap_success(sm->eapol, TRUE);
567 /*
568 * Start preauthentication after a short wait to avoid a
569 * possible race condition between the data receive and key
570 * configuration after the 4-Way Handshake. This increases the
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800571 * likelihood of the first preauth EAPOL-Start frame getting to
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700572 * the target AP.
573 */
574 eloop_register_timeout(1, 0, wpa_sm_start_preauth, sm, NULL);
575 }
576
577 if (sm->cur_pmksa && sm->cur_pmksa->opportunistic) {
578 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
579 "RSN: Authenticator accepted "
580 "opportunistic PMKSA entry - marking it valid");
581 sm->cur_pmksa->opportunistic = 0;
582 }
583
584#ifdef CONFIG_IEEE80211R
585 if (wpa_key_mgmt_ft(sm->key_mgmt)) {
586 /* Prepare for the next transition */
587 wpa_ft_prepare_auth_request(sm, NULL);
588 }
589#endif /* CONFIG_IEEE80211R */
590}
591
592
593static void wpa_sm_rekey_ptk(void *eloop_ctx, void *timeout_ctx)
594{
595 struct wpa_sm *sm = eloop_ctx;
596 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Request PTK rekeying");
597 wpa_sm_key_request(sm, 0, 1);
598}
599
600
601static int wpa_supplicant_install_ptk(struct wpa_sm *sm,
602 const struct wpa_eapol_key *key)
603{
604 int keylen, rsclen;
605 enum wpa_alg alg;
606 const u8 *key_rsc;
607 u8 null_rsc[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
608
Jouni Malinen244b71b2015-10-01 18:51:04 +0300609 if (!sm->tk_to_set) {
610 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
611 "WPA: Do not re-install same PTK to the driver");
612 return 0;
613 }
614
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700615 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
616 "WPA: Installing PTK to the driver");
617
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700618 if (sm->pairwise_cipher == WPA_CIPHER_NONE) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700619 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Pairwise Cipher "
620 "Suite: NONE - do not use pairwise keys");
621 return 0;
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700622 }
623
624 if (!wpa_cipher_valid_pairwise(sm->pairwise_cipher)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700625 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
626 "WPA: Unsupported pairwise cipher %d",
627 sm->pairwise_cipher);
628 return -1;
629 }
630
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700631 alg = wpa_cipher_to_alg(sm->pairwise_cipher);
632 keylen = wpa_cipher_key_len(sm->pairwise_cipher);
633 rsclen = wpa_cipher_rsc_len(sm->pairwise_cipher);
634
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800635 if (sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700636 key_rsc = null_rsc;
637 } else {
638 key_rsc = key->key_rsc;
639 wpa_hexdump(MSG_DEBUG, "WPA: RSC", key_rsc, rsclen);
640 }
641
642 if (wpa_sm_set_key(sm, alg, sm->bssid, 0, 1, key_rsc, rsclen,
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800643 sm->ptk.tk, keylen) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700644 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
645 "WPA: Failed to set PTK to the "
646 "driver (alg=%d keylen=%d bssid=" MACSTR ")",
647 alg, keylen, MAC2STR(sm->bssid));
648 return -1;
649 }
650
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800651 /* TK is not needed anymore in supplicant */
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800652 os_memset(sm->ptk.tk, 0, WPA_TK_MAX_LEN);
Jouni Malinen244b71b2015-10-01 18:51:04 +0300653 sm->tk_to_set = 0;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800654
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700655 if (sm->wpa_ptk_rekey) {
656 eloop_cancel_timeout(wpa_sm_rekey_ptk, sm, NULL);
657 eloop_register_timeout(sm->wpa_ptk_rekey, 0, wpa_sm_rekey_ptk,
658 sm, NULL);
659 }
660
661 return 0;
662}
663
664
665static int wpa_supplicant_check_group_cipher(struct wpa_sm *sm,
666 int group_cipher,
667 int keylen, int maxkeylen,
668 int *key_rsc_len,
669 enum wpa_alg *alg)
670{
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700671 int klen;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700672
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700673 *alg = wpa_cipher_to_alg(group_cipher);
674 if (*alg == WPA_ALG_NONE) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700675 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
676 "WPA: Unsupported Group Cipher %d",
677 group_cipher);
678 return -1;
679 }
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700680 *key_rsc_len = wpa_cipher_rsc_len(group_cipher);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700681
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700682 klen = wpa_cipher_key_len(group_cipher);
683 if (keylen != klen || maxkeylen < klen) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700684 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
685 "WPA: Unsupported %s Group Cipher key length %d (%d)",
686 wpa_cipher_txt(group_cipher), keylen, maxkeylen);
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700687 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700688 }
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700689 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700690}
691
692
693struct wpa_gtk_data {
694 enum wpa_alg alg;
695 int tx, key_rsc_len, keyidx;
696 u8 gtk[32];
697 int gtk_len;
698};
699
700
701static int wpa_supplicant_install_gtk(struct wpa_sm *sm,
702 const struct wpa_gtk_data *gd,
703 const u8 *key_rsc)
704{
705 const u8 *_gtk = gd->gtk;
706 u8 gtk_buf[32];
707
Mathy Vanhoef2468ddd2017-07-12 16:03:24 +0200708 /* Detect possible key reinstallation */
709 if (sm->gtk.gtk_len == (size_t) gd->gtk_len &&
710 os_memcmp(sm->gtk.gtk, gd->gtk, sm->gtk.gtk_len) == 0) {
711 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
712 "WPA: Not reinstalling already in-use GTK to the driver (keyidx=%d tx=%d len=%d)",
713 gd->keyidx, gd->tx, gd->gtk_len);
714 return 0;
715 }
716
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700717 wpa_hexdump_key(MSG_DEBUG, "WPA: Group Key", gd->gtk, gd->gtk_len);
718 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
719 "WPA: Installing GTK to the driver (keyidx=%d tx=%d len=%d)",
720 gd->keyidx, gd->tx, gd->gtk_len);
721 wpa_hexdump(MSG_DEBUG, "WPA: RSC", key_rsc, gd->key_rsc_len);
722 if (sm->group_cipher == WPA_CIPHER_TKIP) {
723 /* Swap Tx/Rx keys for Michael MIC */
724 os_memcpy(gtk_buf, gd->gtk, 16);
725 os_memcpy(gtk_buf + 16, gd->gtk + 24, 8);
726 os_memcpy(gtk_buf + 24, gd->gtk + 16, 8);
727 _gtk = gtk_buf;
728 }
729 if (sm->pairwise_cipher == WPA_CIPHER_NONE) {
730 if (wpa_sm_set_key(sm, gd->alg, NULL,
731 gd->keyidx, 1, key_rsc, gd->key_rsc_len,
732 _gtk, gd->gtk_len) < 0) {
733 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
734 "WPA: Failed to set GTK to the driver "
735 "(Group only)");
Dmitry Shmidt61593f02014-04-21 16:27:35 -0700736 os_memset(gtk_buf, 0, sizeof(gtk_buf));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700737 return -1;
738 }
739 } else if (wpa_sm_set_key(sm, gd->alg, broadcast_ether_addr,
740 gd->keyidx, gd->tx, key_rsc, gd->key_rsc_len,
741 _gtk, gd->gtk_len) < 0) {
742 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
743 "WPA: Failed to set GTK to "
744 "the driver (alg=%d keylen=%d keyidx=%d)",
745 gd->alg, gd->gtk_len, gd->keyidx);
Dmitry Shmidt61593f02014-04-21 16:27:35 -0700746 os_memset(gtk_buf, 0, sizeof(gtk_buf));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700747 return -1;
748 }
Dmitry Shmidt61593f02014-04-21 16:27:35 -0700749 os_memset(gtk_buf, 0, sizeof(gtk_buf));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700750
Mathy Vanhoef2468ddd2017-07-12 16:03:24 +0200751 sm->gtk.gtk_len = gd->gtk_len;
752 os_memcpy(sm->gtk.gtk, gd->gtk, sm->gtk.gtk_len);
753
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700754 return 0;
755}
756
757
758static int wpa_supplicant_gtk_tx_bit_workaround(const struct wpa_sm *sm,
759 int tx)
760{
761 if (tx && sm->pairwise_cipher != WPA_CIPHER_NONE) {
762 /* Ignore Tx bit for GTK if a pairwise key is used. One AP
763 * seemed to set this bit (incorrectly, since Tx is only when
764 * doing Group Key only APs) and without this workaround, the
765 * data connection does not work because wpa_supplicant
766 * configured non-zero keyidx to be used for unicast. */
767 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
768 "WPA: Tx bit set for GTK, but pairwise "
769 "keys are used - ignore Tx bit");
770 return 0;
771 }
772 return tx;
773}
774
775
776static int wpa_supplicant_pairwise_gtk(struct wpa_sm *sm,
777 const struct wpa_eapol_key *key,
778 const u8 *gtk, size_t gtk_len,
779 int key_info)
780{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700781 struct wpa_gtk_data gd;
782
783 /*
784 * IEEE Std 802.11i-2004 - 8.5.2 EAPOL-Key frames - Figure 43x
785 * GTK KDE format:
786 * KeyID[bits 0-1], Tx [bit 2], Reserved [bits 3-7]
787 * Reserved [bits 0-7]
788 * GTK
789 */
790
791 os_memset(&gd, 0, sizeof(gd));
792 wpa_hexdump_key(MSG_DEBUG, "RSN: received GTK in pairwise handshake",
793 gtk, gtk_len);
794
795 if (gtk_len < 2 || gtk_len - 2 > sizeof(gd.gtk))
796 return -1;
797
798 gd.keyidx = gtk[0] & 0x3;
799 gd.tx = wpa_supplicant_gtk_tx_bit_workaround(sm,
800 !!(gtk[0] & BIT(2)));
801 gtk += 2;
802 gtk_len -= 2;
803
804 os_memcpy(gd.gtk, gtk, gtk_len);
805 gd.gtk_len = gtk_len;
806
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800807 if (sm->group_cipher != WPA_CIPHER_GTK_NOT_USED &&
808 (wpa_supplicant_check_group_cipher(sm, sm->group_cipher,
809 gtk_len, gtk_len,
810 &gd.key_rsc_len, &gd.alg) ||
811 wpa_supplicant_install_gtk(sm, &gd, key->key_rsc))) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700812 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
813 "RSN: Failed to install GTK");
Dmitry Shmidt61593f02014-04-21 16:27:35 -0700814 os_memset(&gd, 0, sizeof(gd));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700815 return -1;
816 }
Dmitry Shmidt61593f02014-04-21 16:27:35 -0700817 os_memset(&gd, 0, sizeof(gd));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700818
819 wpa_supplicant_key_neg_complete(sm, sm->bssid,
820 key_info & WPA_KEY_INFO_SECURE);
821 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700822}
823
824
Mathy Vanhoef2468ddd2017-07-12 16:03:24 +0200825#ifdef CONFIG_IEEE80211W
826static int wpa_supplicant_install_igtk(struct wpa_sm *sm,
827 const struct wpa_igtk_kde *igtk)
828{
829 size_t len = wpa_cipher_key_len(sm->mgmt_group_cipher);
830 u16 keyidx = WPA_GET_LE16(igtk->keyid);
831
832 /* Detect possible key reinstallation */
833 if (sm->igtk.igtk_len == len &&
834 os_memcmp(sm->igtk.igtk, igtk->igtk, sm->igtk.igtk_len) == 0) {
835 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
836 "WPA: Not reinstalling already in-use IGTK to the driver (keyidx=%d)",
837 keyidx);
838 return 0;
839 }
840
841 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
842 "WPA: IGTK keyid %d pn %02x%02x%02x%02x%02x%02x",
843 keyidx, MAC2STR(igtk->pn));
844 wpa_hexdump_key(MSG_DEBUG, "WPA: IGTK", igtk->igtk, len);
845 if (keyidx > 4095) {
846 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
847 "WPA: Invalid IGTK KeyID %d", keyidx);
848 return -1;
849 }
850 if (wpa_sm_set_key(sm, wpa_cipher_to_alg(sm->mgmt_group_cipher),
851 broadcast_ether_addr,
852 keyidx, 0, igtk->pn, sizeof(igtk->pn),
853 igtk->igtk, len) < 0) {
854 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
855 "WPA: Failed to configure IGTK to the driver");
856 return -1;
857 }
858
859 sm->igtk.igtk_len = len;
860 os_memcpy(sm->igtk.igtk, igtk->igtk, sm->igtk.igtk_len);
861
862 return 0;
863}
864#endif /* CONFIG_IEEE80211W */
865
866
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700867static int ieee80211w_set_keys(struct wpa_sm *sm,
868 struct wpa_eapol_ie_parse *ie)
869{
870#ifdef CONFIG_IEEE80211W
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -0700871 if (!wpa_cipher_valid_mgmt_group(sm->mgmt_group_cipher))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700872 return 0;
873
874 if (ie->igtk) {
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -0700875 size_t len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700876 const struct wpa_igtk_kde *igtk;
Mathy Vanhoef2468ddd2017-07-12 16:03:24 +0200877
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -0700878 len = wpa_cipher_key_len(sm->mgmt_group_cipher);
879 if (ie->igtk_len != WPA_IGTK_KDE_PREFIX_LEN + len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700880 return -1;
Mathy Vanhoef2468ddd2017-07-12 16:03:24 +0200881
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700882 igtk = (const struct wpa_igtk_kde *) ie->igtk;
Mathy Vanhoef2468ddd2017-07-12 16:03:24 +0200883 if (wpa_supplicant_install_igtk(sm, igtk) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700884 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700885 }
886
887 return 0;
888#else /* CONFIG_IEEE80211W */
889 return 0;
890#endif /* CONFIG_IEEE80211W */
891}
892
893
894static void wpa_report_ie_mismatch(struct wpa_sm *sm,
895 const char *reason, const u8 *src_addr,
896 const u8 *wpa_ie, size_t wpa_ie_len,
897 const u8 *rsn_ie, size_t rsn_ie_len)
898{
899 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, "WPA: %s (src=" MACSTR ")",
900 reason, MAC2STR(src_addr));
901
902 if (sm->ap_wpa_ie) {
903 wpa_hexdump(MSG_INFO, "WPA: WPA IE in Beacon/ProbeResp",
904 sm->ap_wpa_ie, sm->ap_wpa_ie_len);
905 }
906 if (wpa_ie) {
907 if (!sm->ap_wpa_ie) {
908 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
909 "WPA: No WPA IE in Beacon/ProbeResp");
910 }
911 wpa_hexdump(MSG_INFO, "WPA: WPA IE in 3/4 msg",
912 wpa_ie, wpa_ie_len);
913 }
914
915 if (sm->ap_rsn_ie) {
916 wpa_hexdump(MSG_INFO, "WPA: RSN IE in Beacon/ProbeResp",
917 sm->ap_rsn_ie, sm->ap_rsn_ie_len);
918 }
919 if (rsn_ie) {
920 if (!sm->ap_rsn_ie) {
921 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
922 "WPA: No RSN IE in Beacon/ProbeResp");
923 }
924 wpa_hexdump(MSG_INFO, "WPA: RSN IE in 3/4 msg",
925 rsn_ie, rsn_ie_len);
926 }
927
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800928 wpa_sm_deauthenticate(sm, WLAN_REASON_IE_IN_4WAY_DIFFERS);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700929}
930
931
932#ifdef CONFIG_IEEE80211R
933
934static int ft_validate_mdie(struct wpa_sm *sm,
935 const unsigned char *src_addr,
936 struct wpa_eapol_ie_parse *ie,
937 const u8 *assoc_resp_mdie)
938{
939 struct rsn_mdie *mdie;
940
941 mdie = (struct rsn_mdie *) (ie->mdie + 2);
942 if (ie->mdie == NULL || ie->mdie_len < 2 + sizeof(*mdie) ||
943 os_memcmp(mdie->mobility_domain, sm->mobility_domain,
944 MOBILITY_DOMAIN_ID_LEN) != 0) {
945 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "FT: MDIE in msg 3/4 did "
946 "not match with the current mobility domain");
947 return -1;
948 }
949
950 if (assoc_resp_mdie &&
951 (assoc_resp_mdie[1] != ie->mdie[1] ||
952 os_memcmp(assoc_resp_mdie, ie->mdie, 2 + ie->mdie[1]) != 0)) {
953 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "FT: MDIE mismatch");
954 wpa_hexdump(MSG_DEBUG, "FT: MDIE in EAPOL-Key msg 3/4",
955 ie->mdie, 2 + ie->mdie[1]);
956 wpa_hexdump(MSG_DEBUG, "FT: MDIE in (Re)Association Response",
957 assoc_resp_mdie, 2 + assoc_resp_mdie[1]);
958 return -1;
959 }
960
961 return 0;
962}
963
964
965static int ft_validate_ftie(struct wpa_sm *sm,
966 const unsigned char *src_addr,
967 struct wpa_eapol_ie_parse *ie,
968 const u8 *assoc_resp_ftie)
969{
970 if (ie->ftie == NULL) {
971 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
972 "FT: No FTIE in EAPOL-Key msg 3/4");
973 return -1;
974 }
975
976 if (assoc_resp_ftie == NULL)
977 return 0;
978
979 if (assoc_resp_ftie[1] != ie->ftie[1] ||
980 os_memcmp(assoc_resp_ftie, ie->ftie, 2 + ie->ftie[1]) != 0) {
981 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "FT: FTIE mismatch");
982 wpa_hexdump(MSG_DEBUG, "FT: FTIE in EAPOL-Key msg 3/4",
983 ie->ftie, 2 + ie->ftie[1]);
984 wpa_hexdump(MSG_DEBUG, "FT: FTIE in (Re)Association Response",
985 assoc_resp_ftie, 2 + assoc_resp_ftie[1]);
986 return -1;
987 }
988
989 return 0;
990}
991
992
993static int ft_validate_rsnie(struct wpa_sm *sm,
994 const unsigned char *src_addr,
995 struct wpa_eapol_ie_parse *ie)
996{
997 struct wpa_ie_data rsn;
998
999 if (!ie->rsn_ie)
1000 return 0;
1001
1002 /*
1003 * Verify that PMKR1Name from EAPOL-Key message 3/4
1004 * matches with the value we derived.
1005 */
1006 if (wpa_parse_wpa_ie_rsn(ie->rsn_ie, ie->rsn_ie_len, &rsn) < 0 ||
1007 rsn.num_pmkid != 1 || rsn.pmkid == NULL) {
1008 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "FT: No PMKR1Name in "
1009 "FT 4-way handshake message 3/4");
1010 return -1;
1011 }
1012
Dmitry Shmidtc2817022014-07-02 10:32:10 -07001013 if (os_memcmp_const(rsn.pmkid, sm->pmk_r1_name, WPA_PMK_NAME_LEN) != 0)
1014 {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001015 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1016 "FT: PMKR1Name mismatch in "
1017 "FT 4-way handshake message 3/4");
1018 wpa_hexdump(MSG_DEBUG, "FT: PMKR1Name from Authenticator",
1019 rsn.pmkid, WPA_PMK_NAME_LEN);
1020 wpa_hexdump(MSG_DEBUG, "FT: Derived PMKR1Name",
1021 sm->pmk_r1_name, WPA_PMK_NAME_LEN);
1022 return -1;
1023 }
1024
1025 return 0;
1026}
1027
1028
1029static int wpa_supplicant_validate_ie_ft(struct wpa_sm *sm,
1030 const unsigned char *src_addr,
1031 struct wpa_eapol_ie_parse *ie)
1032{
1033 const u8 *pos, *end, *mdie = NULL, *ftie = NULL;
1034
1035 if (sm->assoc_resp_ies) {
1036 pos = sm->assoc_resp_ies;
1037 end = pos + sm->assoc_resp_ies_len;
1038 while (pos + 2 < end) {
1039 if (pos + 2 + pos[1] > end)
1040 break;
1041 switch (*pos) {
1042 case WLAN_EID_MOBILITY_DOMAIN:
1043 mdie = pos;
1044 break;
1045 case WLAN_EID_FAST_BSS_TRANSITION:
1046 ftie = pos;
1047 break;
1048 }
1049 pos += 2 + pos[1];
1050 }
1051 }
1052
1053 if (ft_validate_mdie(sm, src_addr, ie, mdie) < 0 ||
1054 ft_validate_ftie(sm, src_addr, ie, ftie) < 0 ||
1055 ft_validate_rsnie(sm, src_addr, ie) < 0)
1056 return -1;
1057
1058 return 0;
1059}
1060
1061#endif /* CONFIG_IEEE80211R */
1062
1063
1064static int wpa_supplicant_validate_ie(struct wpa_sm *sm,
1065 const unsigned char *src_addr,
1066 struct wpa_eapol_ie_parse *ie)
1067{
1068 if (sm->ap_wpa_ie == NULL && sm->ap_rsn_ie == NULL) {
1069 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1070 "WPA: No WPA/RSN IE for this AP known. "
1071 "Trying to get from scan results");
1072 if (wpa_sm_get_beacon_ie(sm) < 0) {
1073 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1074 "WPA: Could not find AP from "
1075 "the scan results");
1076 } else {
1077 wpa_msg(sm->ctx->msg_ctx, MSG_DEBUG,
1078 "WPA: Found the current AP from "
1079 "updated scan results");
1080 }
1081 }
1082
1083 if (ie->wpa_ie == NULL && ie->rsn_ie == NULL &&
1084 (sm->ap_wpa_ie || sm->ap_rsn_ie)) {
1085 wpa_report_ie_mismatch(sm, "IE in 3/4 msg does not match "
1086 "with IE in Beacon/ProbeResp (no IE?)",
1087 src_addr, ie->wpa_ie, ie->wpa_ie_len,
1088 ie->rsn_ie, ie->rsn_ie_len);
1089 return -1;
1090 }
1091
1092 if ((ie->wpa_ie && sm->ap_wpa_ie &&
1093 (ie->wpa_ie_len != sm->ap_wpa_ie_len ||
1094 os_memcmp(ie->wpa_ie, sm->ap_wpa_ie, ie->wpa_ie_len) != 0)) ||
1095 (ie->rsn_ie && sm->ap_rsn_ie &&
1096 wpa_compare_rsn_ie(wpa_key_mgmt_ft(sm->key_mgmt),
1097 sm->ap_rsn_ie, sm->ap_rsn_ie_len,
1098 ie->rsn_ie, ie->rsn_ie_len))) {
1099 wpa_report_ie_mismatch(sm, "IE in 3/4 msg does not match "
1100 "with IE in Beacon/ProbeResp",
1101 src_addr, ie->wpa_ie, ie->wpa_ie_len,
1102 ie->rsn_ie, ie->rsn_ie_len);
1103 return -1;
1104 }
1105
1106 if (sm->proto == WPA_PROTO_WPA &&
1107 ie->rsn_ie && sm->ap_rsn_ie == NULL && sm->rsn_enabled) {
1108 wpa_report_ie_mismatch(sm, "Possible downgrade attack "
1109 "detected - RSN was enabled and RSN IE "
1110 "was in msg 3/4, but not in "
1111 "Beacon/ProbeResp",
1112 src_addr, ie->wpa_ie, ie->wpa_ie_len,
1113 ie->rsn_ie, ie->rsn_ie_len);
1114 return -1;
1115 }
1116
1117#ifdef CONFIG_IEEE80211R
1118 if (wpa_key_mgmt_ft(sm->key_mgmt) &&
1119 wpa_supplicant_validate_ie_ft(sm, src_addr, ie) < 0)
1120 return -1;
1121#endif /* CONFIG_IEEE80211R */
1122
1123 return 0;
1124}
1125
1126
1127/**
1128 * wpa_supplicant_send_4_of_4 - Send message 4 of WPA/RSN 4-Way Handshake
1129 * @sm: Pointer to WPA state machine data from wpa_sm_init()
1130 * @dst: Destination address for the frame
1131 * @key: Pointer to the EAPOL-Key frame header
1132 * @ver: Version bits from EAPOL-Key Key Info
1133 * @key_info: Key Info
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001134 * @ptk: PTK to use for keyed hash and encryption
1135 * Returns: 0 on success, -1 on failure
1136 */
1137int wpa_supplicant_send_4_of_4(struct wpa_sm *sm, const unsigned char *dst,
1138 const struct wpa_eapol_key *key,
1139 u16 ver, u16 key_info,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001140 struct wpa_ptk *ptk)
1141{
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001142 size_t mic_len, hdrlen, rlen;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001143 struct wpa_eapol_key *reply;
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001144 struct wpa_eapol_key_192 *reply192;
1145 u8 *rbuf, *key_mic;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001146
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001147 mic_len = wpa_mic_len(sm->key_mgmt);
1148 hdrlen = mic_len == 24 ? sizeof(*reply192) : sizeof(*reply);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001149 rbuf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_KEY, NULL,
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001150 hdrlen, &rlen, (void *) &reply);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001151 if (rbuf == NULL)
1152 return -1;
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001153 reply192 = (struct wpa_eapol_key_192 *) reply;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001154
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001155 reply->type = (sm->proto == WPA_PROTO_RSN ||
1156 sm->proto == WPA_PROTO_OSEN) ?
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001157 EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA;
1158 key_info &= WPA_KEY_INFO_SECURE;
1159 key_info |= ver | WPA_KEY_INFO_KEY_TYPE | WPA_KEY_INFO_MIC;
1160 WPA_PUT_BE16(reply->key_info, key_info);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001161 if (sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001162 WPA_PUT_BE16(reply->key_length, 0);
1163 else
1164 os_memcpy(reply->key_length, key->key_length, 2);
1165 os_memcpy(reply->replay_counter, key->replay_counter,
1166 WPA_REPLAY_COUNTER_LEN);
1167
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001168 key_mic = reply192->key_mic; /* same offset for reply and reply192 */
1169 if (mic_len == 24)
1170 WPA_PUT_BE16(reply192->key_data_length, 0);
1171 else
1172 WPA_PUT_BE16(reply->key_data_length, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001173
1174 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Sending EAPOL-Key 4/4");
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001175 wpa_eapol_key_send(sm, ptk->kck, ptk->kck_len, ver, dst, ETH_P_EAPOL,
1176 rbuf, rlen, key_mic);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001177
1178 return 0;
1179}
1180
1181
1182static void wpa_supplicant_process_3_of_4(struct wpa_sm *sm,
1183 const struct wpa_eapol_key *key,
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001184 u16 ver, const u8 *key_data,
1185 size_t key_data_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001186{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001187 u16 key_info, keylen;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001188 struct wpa_eapol_ie_parse ie;
1189
1190 wpa_sm_set_state(sm, WPA_4WAY_HANDSHAKE);
1191 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: RX message 3 of 4-Way "
1192 "Handshake from " MACSTR " (ver=%d)", MAC2STR(sm->bssid), ver);
1193
1194 key_info = WPA_GET_BE16(key->key_info);
1195
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001196 wpa_hexdump(MSG_DEBUG, "WPA: IE KeyData", key_data, key_data_len);
1197 if (wpa_supplicant_parse_ies(key_data, key_data_len, &ie) < 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001198 goto failed;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001199 if (ie.gtk && !(key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
1200 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1201 "WPA: GTK IE in unencrypted key data");
1202 goto failed;
1203 }
1204#ifdef CONFIG_IEEE80211W
1205 if (ie.igtk && !(key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
1206 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1207 "WPA: IGTK KDE in unencrypted key data");
1208 goto failed;
1209 }
1210
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001211 if (ie.igtk &&
1212 wpa_cipher_valid_mgmt_group(sm->mgmt_group_cipher) &&
1213 ie.igtk_len != WPA_IGTK_KDE_PREFIX_LEN +
1214 (unsigned int) wpa_cipher_key_len(sm->mgmt_group_cipher)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001215 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1216 "WPA: Invalid IGTK KDE length %lu",
1217 (unsigned long) ie.igtk_len);
1218 goto failed;
1219 }
1220#endif /* CONFIG_IEEE80211W */
1221
1222 if (wpa_supplicant_validate_ie(sm, sm->bssid, &ie) < 0)
1223 goto failed;
1224
1225 if (os_memcmp(sm->anonce, key->key_nonce, WPA_NONCE_LEN) != 0) {
1226 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1227 "WPA: ANonce from message 1 of 4-Way Handshake "
1228 "differs from 3 of 4-Way Handshake - drop packet (src="
1229 MACSTR ")", MAC2STR(sm->bssid));
1230 goto failed;
1231 }
1232
1233 keylen = WPA_GET_BE16(key->key_length);
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07001234 if (keylen != wpa_cipher_key_len(sm->pairwise_cipher)) {
1235 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1236 "WPA: Invalid %s key length %d (src=" MACSTR
1237 ")", wpa_cipher_txt(sm->pairwise_cipher), keylen,
1238 MAC2STR(sm->bssid));
1239 goto failed;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001240 }
1241
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001242#ifdef CONFIG_P2P
1243 if (ie.ip_addr_alloc) {
1244 os_memcpy(sm->p2p_ip_addr, ie.ip_addr_alloc, 3 * 4);
1245 wpa_hexdump(MSG_DEBUG, "P2P: IP address info",
1246 sm->p2p_ip_addr, sizeof(sm->p2p_ip_addr));
1247 }
1248#endif /* CONFIG_P2P */
1249
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001250 if (wpa_supplicant_send_4_of_4(sm, sm->bssid, key, ver, key_info,
Dmitry Shmidt21de2142014-04-08 10:50:52 -07001251 &sm->ptk)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001252 goto failed;
1253 }
1254
1255 /* SNonce was successfully used in msg 3/4, so mark it to be renewed
1256 * for the next 4-Way Handshake. If msg 3 is received again, the old
1257 * SNonce will still be used to avoid changing PTK. */
1258 sm->renew_snonce = 1;
1259
1260 if (key_info & WPA_KEY_INFO_INSTALL) {
1261 if (wpa_supplicant_install_ptk(sm, key))
1262 goto failed;
1263 }
1264
1265 if (key_info & WPA_KEY_INFO_SECURE) {
1266 wpa_sm_mlme_setprotection(
1267 sm, sm->bssid, MLME_SETPROTECTION_PROTECT_TYPE_RX,
1268 MLME_SETPROTECTION_KEY_TYPE_PAIRWISE);
1269 eapol_sm_notify_portValid(sm->eapol, TRUE);
1270 }
1271 wpa_sm_set_state(sm, WPA_GROUP_HANDSHAKE);
1272
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001273 if (sm->group_cipher == WPA_CIPHER_GTK_NOT_USED) {
1274 wpa_supplicant_key_neg_complete(sm, sm->bssid,
1275 key_info & WPA_KEY_INFO_SECURE);
1276 } else if (ie.gtk &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001277 wpa_supplicant_pairwise_gtk(sm, key,
1278 ie.gtk, ie.gtk_len, key_info) < 0) {
1279 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1280 "RSN: Failed to configure GTK");
1281 goto failed;
1282 }
1283
1284 if (ieee80211w_set_keys(sm, &ie) < 0) {
1285 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1286 "RSN: Failed to configure IGTK");
1287 goto failed;
1288 }
1289
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001290 if (ie.gtk)
1291 wpa_sm_set_rekey_offload(sm);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001292
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001293 if (sm->proto == WPA_PROTO_RSN && wpa_key_mgmt_suite_b(sm->key_mgmt)) {
1294 struct rsn_pmksa_cache_entry *sa;
1295
1296 sa = pmksa_cache_add(sm->pmksa, sm->pmk, sm->pmk_len,
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001297 sm->ptk.kck, sm->ptk.kck_len,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001298 sm->bssid, sm->own_addr,
1299 sm->network_ctx, sm->key_mgmt);
1300 if (!sm->cur_pmksa)
1301 sm->cur_pmksa = sa;
1302 }
1303
Dmitry Shmidt4dd28dc2015-03-10 11:21:43 -07001304 sm->msg_3_of_4_ok = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001305 return;
1306
1307failed:
1308 wpa_sm_deauthenticate(sm, WLAN_REASON_UNSPECIFIED);
1309}
1310
1311
1312static int wpa_supplicant_process_1_of_2_rsn(struct wpa_sm *sm,
1313 const u8 *keydata,
1314 size_t keydatalen,
1315 u16 key_info,
1316 struct wpa_gtk_data *gd)
1317{
1318 int maxkeylen;
1319 struct wpa_eapol_ie_parse ie;
1320
1321 wpa_hexdump(MSG_DEBUG, "RSN: msg 1/2 key data", keydata, keydatalen);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001322 if (wpa_supplicant_parse_ies(keydata, keydatalen, &ie) < 0)
1323 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001324 if (ie.gtk && !(key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
1325 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1326 "WPA: GTK IE in unencrypted key data");
1327 return -1;
1328 }
1329 if (ie.gtk == NULL) {
1330 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1331 "WPA: No GTK IE in Group Key msg 1/2");
1332 return -1;
1333 }
1334 maxkeylen = gd->gtk_len = ie.gtk_len - 2;
1335
1336 if (wpa_supplicant_check_group_cipher(sm, sm->group_cipher,
1337 gd->gtk_len, maxkeylen,
1338 &gd->key_rsc_len, &gd->alg))
1339 return -1;
1340
1341 wpa_hexdump(MSG_DEBUG, "RSN: received GTK in group key handshake",
1342 ie.gtk, ie.gtk_len);
1343 gd->keyidx = ie.gtk[0] & 0x3;
1344 gd->tx = wpa_supplicant_gtk_tx_bit_workaround(sm,
1345 !!(ie.gtk[0] & BIT(2)));
1346 if (ie.gtk_len - 2 > sizeof(gd->gtk)) {
1347 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1348 "RSN: Too long GTK in GTK IE (len=%lu)",
1349 (unsigned long) ie.gtk_len - 2);
1350 return -1;
1351 }
1352 os_memcpy(gd->gtk, ie.gtk + 2, ie.gtk_len - 2);
1353
1354 if (ieee80211w_set_keys(sm, &ie) < 0)
1355 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1356 "RSN: Failed to configure IGTK");
1357
1358 return 0;
1359}
1360
1361
1362static int wpa_supplicant_process_1_of_2_wpa(struct wpa_sm *sm,
1363 const struct wpa_eapol_key *key,
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001364 const u8 *key_data,
1365 size_t key_data_len, u16 key_info,
1366 u16 ver, struct wpa_gtk_data *gd)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001367{
1368 size_t maxkeylen;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001369 u16 gtk_len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001370
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001371 gtk_len = WPA_GET_BE16(key->key_length);
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001372 maxkeylen = key_data_len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001373 if (ver == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
1374 if (maxkeylen < 8) {
1375 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1376 "WPA: Too short maxkeylen (%lu)",
1377 (unsigned long) maxkeylen);
1378 return -1;
1379 }
1380 maxkeylen -= 8;
1381 }
1382
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001383 if (gtk_len > maxkeylen ||
1384 wpa_supplicant_check_group_cipher(sm, sm->group_cipher,
1385 gtk_len, maxkeylen,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001386 &gd->key_rsc_len, &gd->alg))
1387 return -1;
1388
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001389 gd->gtk_len = gtk_len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001390 gd->keyidx = (key_info & WPA_KEY_INFO_KEY_INDEX_MASK) >>
1391 WPA_KEY_INFO_KEY_INDEX_SHIFT;
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001392 if (ver == WPA_KEY_INFO_TYPE_HMAC_MD5_RC4 && sm->ptk.kek_len == 16) {
Dmitry Shmidt61593f02014-04-21 16:27:35 -07001393 u8 ek[32];
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001394 if (key_data_len > sizeof(gd->gtk)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001395 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1396 "WPA: RC4 key data too long (%lu)",
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001397 (unsigned long) key_data_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001398 return -1;
1399 }
Dmitry Shmidt61593f02014-04-21 16:27:35 -07001400 os_memcpy(ek, key->key_iv, 16);
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001401 os_memcpy(ek + 16, sm->ptk.kek, sm->ptk.kek_len);
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001402 os_memcpy(gd->gtk, key_data, key_data_len);
1403 if (rc4_skip(ek, 32, 256, gd->gtk, key_data_len)) {
Dmitry Shmidt61593f02014-04-21 16:27:35 -07001404 os_memset(ek, 0, sizeof(ek));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001405 wpa_msg(sm->ctx->msg_ctx, MSG_ERROR,
1406 "WPA: RC4 failed");
1407 return -1;
1408 }
Dmitry Shmidt61593f02014-04-21 16:27:35 -07001409 os_memset(ek, 0, sizeof(ek));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001410 } else if (ver == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001411 if (maxkeylen % 8) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001412 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1413 "WPA: Unsupported AES-WRAP len %lu",
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001414 (unsigned long) maxkeylen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001415 return -1;
1416 }
1417 if (maxkeylen > sizeof(gd->gtk)) {
1418 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1419 "WPA: AES-WRAP key data "
1420 "too long (keydatalen=%lu maxkeylen=%lu)",
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001421 (unsigned long) key_data_len,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001422 (unsigned long) maxkeylen);
1423 return -1;
1424 }
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001425 if (aes_unwrap(sm->ptk.kek, sm->ptk.kek_len, maxkeylen / 8,
1426 key_data, gd->gtk)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001427 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1428 "WPA: AES unwrap failed - could not decrypt "
1429 "GTK");
1430 return -1;
1431 }
1432 } else {
1433 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1434 "WPA: Unsupported key_info type %d", ver);
1435 return -1;
1436 }
1437 gd->tx = wpa_supplicant_gtk_tx_bit_workaround(
1438 sm, !!(key_info & WPA_KEY_INFO_TXRX));
1439 return 0;
1440}
1441
1442
1443static int wpa_supplicant_send_2_of_2(struct wpa_sm *sm,
1444 const struct wpa_eapol_key *key,
1445 int ver, u16 key_info)
1446{
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001447 size_t mic_len, hdrlen, rlen;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001448 struct wpa_eapol_key *reply;
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001449 struct wpa_eapol_key_192 *reply192;
1450 u8 *rbuf, *key_mic;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001451
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001452 mic_len = wpa_mic_len(sm->key_mgmt);
1453 hdrlen = mic_len == 24 ? sizeof(*reply192) : sizeof(*reply);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001454 rbuf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_KEY, NULL,
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001455 hdrlen, &rlen, (void *) &reply);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001456 if (rbuf == NULL)
1457 return -1;
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001458 reply192 = (struct wpa_eapol_key_192 *) reply;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001459
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001460 reply->type = (sm->proto == WPA_PROTO_RSN ||
1461 sm->proto == WPA_PROTO_OSEN) ?
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001462 EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA;
1463 key_info &= WPA_KEY_INFO_KEY_INDEX_MASK;
1464 key_info |= ver | WPA_KEY_INFO_MIC | WPA_KEY_INFO_SECURE;
1465 WPA_PUT_BE16(reply->key_info, key_info);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001466 if (sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001467 WPA_PUT_BE16(reply->key_length, 0);
1468 else
1469 os_memcpy(reply->key_length, key->key_length, 2);
1470 os_memcpy(reply->replay_counter, key->replay_counter,
1471 WPA_REPLAY_COUNTER_LEN);
1472
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001473 key_mic = reply192->key_mic; /* same offset for reply and reply192 */
1474 if (mic_len == 24)
1475 WPA_PUT_BE16(reply192->key_data_length, 0);
1476 else
1477 WPA_PUT_BE16(reply->key_data_length, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001478
1479 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Sending EAPOL-Key 2/2");
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001480 wpa_eapol_key_send(sm, sm->ptk.kck, sm->ptk.kck_len, ver, sm->bssid,
1481 ETH_P_EAPOL, rbuf, rlen, key_mic);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001482
1483 return 0;
1484}
1485
1486
1487static void wpa_supplicant_process_1_of_2(struct wpa_sm *sm,
1488 const unsigned char *src_addr,
1489 const struct wpa_eapol_key *key,
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001490 const u8 *key_data,
1491 size_t key_data_len, u16 ver)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001492{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001493 u16 key_info;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001494 int rekey, ret;
1495 struct wpa_gtk_data gd;
1496
Dmitry Shmidt4dd28dc2015-03-10 11:21:43 -07001497 if (!sm->msg_3_of_4_ok) {
1498 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1499 "WPA: Group Key Handshake started prior to completion of 4-way handshake");
1500 goto failed;
1501 }
1502
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001503 os_memset(&gd, 0, sizeof(gd));
1504
1505 rekey = wpa_sm_get_state(sm) == WPA_COMPLETED;
1506 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: RX message 1 of Group Key "
1507 "Handshake from " MACSTR " (ver=%d)", MAC2STR(src_addr), ver);
1508
1509 key_info = WPA_GET_BE16(key->key_info);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001510
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001511 if (sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN) {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001512 ret = wpa_supplicant_process_1_of_2_rsn(sm, key_data,
1513 key_data_len, key_info,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001514 &gd);
1515 } else {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001516 ret = wpa_supplicant_process_1_of_2_wpa(sm, key, key_data,
1517 key_data_len,
1518 key_info, ver, &gd);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001519 }
1520
1521 wpa_sm_set_state(sm, WPA_GROUP_HANDSHAKE);
1522
1523 if (ret)
1524 goto failed;
1525
1526 if (wpa_supplicant_install_gtk(sm, &gd, key->key_rsc) ||
1527 wpa_supplicant_send_2_of_2(sm, key, ver, key_info))
1528 goto failed;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001529 os_memset(&gd, 0, sizeof(gd));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001530
1531 if (rekey) {
1532 wpa_msg(sm->ctx->msg_ctx, MSG_INFO, "WPA: Group rekeying "
1533 "completed with " MACSTR " [GTK=%s]",
1534 MAC2STR(sm->bssid), wpa_cipher_txt(sm->group_cipher));
1535 wpa_sm_cancel_auth_timeout(sm);
1536 wpa_sm_set_state(sm, WPA_COMPLETED);
1537 } else {
1538 wpa_supplicant_key_neg_complete(sm, sm->bssid,
1539 key_info &
1540 WPA_KEY_INFO_SECURE);
1541 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001542
1543 wpa_sm_set_rekey_offload(sm);
1544
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001545 return;
1546
1547failed:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001548 os_memset(&gd, 0, sizeof(gd));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001549 wpa_sm_deauthenticate(sm, WLAN_REASON_UNSPECIFIED);
1550}
1551
1552
1553static int wpa_supplicant_verify_eapol_key_mic(struct wpa_sm *sm,
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001554 struct wpa_eapol_key_192 *key,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001555 u16 ver,
1556 const u8 *buf, size_t len)
1557{
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001558 u8 mic[WPA_EAPOL_KEY_MIC_MAX_LEN];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001559 int ok = 0;
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001560 size_t mic_len = wpa_mic_len(sm->key_mgmt);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001561
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001562 os_memcpy(mic, key->key_mic, mic_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001563 if (sm->tptk_set) {
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001564 os_memset(key->key_mic, 0, mic_len);
1565 wpa_eapol_key_mic(sm->tptk.kck, sm->tptk.kck_len, sm->key_mgmt,
1566 ver, buf, len, key->key_mic);
1567 if (os_memcmp_const(mic, key->key_mic, mic_len) != 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001568 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1569 "WPA: Invalid EAPOL-Key MIC "
1570 "when using TPTK - ignoring TPTK");
1571 } else {
1572 ok = 1;
1573 sm->tptk_set = 0;
1574 sm->ptk_set = 1;
1575 os_memcpy(&sm->ptk, &sm->tptk, sizeof(sm->ptk));
Dmitry Shmidt61593f02014-04-21 16:27:35 -07001576 os_memset(&sm->tptk, 0, sizeof(sm->tptk));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001577 }
1578 }
1579
1580 if (!ok && sm->ptk_set) {
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001581 os_memset(key->key_mic, 0, mic_len);
1582 wpa_eapol_key_mic(sm->ptk.kck, sm->ptk.kck_len, sm->key_mgmt,
1583 ver, buf, len, key->key_mic);
1584 if (os_memcmp_const(mic, key->key_mic, mic_len) != 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001585 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1586 "WPA: Invalid EAPOL-Key MIC - "
1587 "dropping packet");
1588 return -1;
1589 }
1590 ok = 1;
1591 }
1592
1593 if (!ok) {
1594 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1595 "WPA: Could not verify EAPOL-Key MIC - "
1596 "dropping packet");
1597 return -1;
1598 }
1599
1600 os_memcpy(sm->rx_replay_counter, key->replay_counter,
1601 WPA_REPLAY_COUNTER_LEN);
1602 sm->rx_replay_counter_set = 1;
1603 return 0;
1604}
1605
1606
1607/* Decrypt RSN EAPOL-Key key data (RC4 or AES-WRAP) */
1608static int wpa_supplicant_decrypt_key_data(struct wpa_sm *sm,
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001609 struct wpa_eapol_key *key, u16 ver,
1610 u8 *key_data, size_t *key_data_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001611{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001612 wpa_hexdump(MSG_DEBUG, "RSN: encrypted key data",
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001613 key_data, *key_data_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001614 if (!sm->ptk_set) {
1615 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1616 "WPA: PTK not available, cannot decrypt EAPOL-Key Key "
1617 "Data");
1618 return -1;
1619 }
1620
1621 /* Decrypt key data here so that this operation does not need
1622 * to be implemented separately for each message type. */
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001623 if (ver == WPA_KEY_INFO_TYPE_HMAC_MD5_RC4 && sm->ptk.kek_len == 16) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001624 u8 ek[32];
1625 os_memcpy(ek, key->key_iv, 16);
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001626 os_memcpy(ek + 16, sm->ptk.kek, sm->ptk.kek_len);
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001627 if (rc4_skip(ek, 32, 256, key_data, *key_data_len)) {
Dmitry Shmidt61593f02014-04-21 16:27:35 -07001628 os_memset(ek, 0, sizeof(ek));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001629 wpa_msg(sm->ctx->msg_ctx, MSG_ERROR,
1630 "WPA: RC4 failed");
1631 return -1;
1632 }
Dmitry Shmidt61593f02014-04-21 16:27:35 -07001633 os_memset(ek, 0, sizeof(ek));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001634 } else if (ver == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES ||
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001635 ver == WPA_KEY_INFO_TYPE_AES_128_CMAC ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001636 sm->key_mgmt == WPA_KEY_MGMT_OSEN ||
1637 wpa_key_mgmt_suite_b(sm->key_mgmt)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001638 u8 *buf;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001639 if (*key_data_len < 8 || *key_data_len % 8) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001640 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001641 "WPA: Unsupported AES-WRAP len %u",
1642 (unsigned int) *key_data_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001643 return -1;
1644 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001645 *key_data_len -= 8; /* AES-WRAP adds 8 bytes */
1646 buf = os_malloc(*key_data_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001647 if (buf == NULL) {
1648 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1649 "WPA: No memory for AES-UNWRAP buffer");
1650 return -1;
1651 }
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001652 if (aes_unwrap(sm->ptk.kek, sm->ptk.kek_len, *key_data_len / 8,
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001653 key_data, buf)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001654 os_free(buf);
1655 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1656 "WPA: AES unwrap failed - "
1657 "could not decrypt EAPOL-Key key data");
1658 return -1;
1659 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001660 os_memcpy(key_data, buf, *key_data_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001661 os_free(buf);
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001662 WPA_PUT_BE16(key->key_data_length, *key_data_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001663 } else {
1664 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1665 "WPA: Unsupported key_info type %d", ver);
1666 return -1;
1667 }
1668 wpa_hexdump_key(MSG_DEBUG, "WPA: decrypted EAPOL-Key key data",
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001669 key_data, *key_data_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001670 return 0;
1671}
1672
1673
1674/**
1675 * wpa_sm_aborted_cached - Notify WPA that PMKSA caching was aborted
1676 * @sm: Pointer to WPA state machine data from wpa_sm_init()
1677 */
1678void wpa_sm_aborted_cached(struct wpa_sm *sm)
1679{
1680 if (sm && sm->cur_pmksa) {
1681 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1682 "RSN: Cancelling PMKSA caching attempt");
1683 sm->cur_pmksa = NULL;
1684 }
1685}
1686
1687
1688static void wpa_eapol_key_dump(struct wpa_sm *sm,
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001689 const struct wpa_eapol_key *key,
1690 unsigned int key_data_len,
1691 const u8 *mic, unsigned int mic_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001692{
1693#ifndef CONFIG_NO_STDOUT_DEBUG
1694 u16 key_info = WPA_GET_BE16(key->key_info);
1695
1696 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, " EAPOL-Key type=%d", key->type);
1697 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1698 " key_info 0x%x (ver=%d keyidx=%d rsvd=%d %s%s%s%s%s%s%s%s)",
1699 key_info, key_info & WPA_KEY_INFO_TYPE_MASK,
1700 (key_info & WPA_KEY_INFO_KEY_INDEX_MASK) >>
1701 WPA_KEY_INFO_KEY_INDEX_SHIFT,
1702 (key_info & (BIT(13) | BIT(14) | BIT(15))) >> 13,
1703 key_info & WPA_KEY_INFO_KEY_TYPE ? "Pairwise" : "Group",
1704 key_info & WPA_KEY_INFO_INSTALL ? " Install" : "",
1705 key_info & WPA_KEY_INFO_ACK ? " Ack" : "",
1706 key_info & WPA_KEY_INFO_MIC ? " MIC" : "",
1707 key_info & WPA_KEY_INFO_SECURE ? " Secure" : "",
1708 key_info & WPA_KEY_INFO_ERROR ? " Error" : "",
1709 key_info & WPA_KEY_INFO_REQUEST ? " Request" : "",
1710 key_info & WPA_KEY_INFO_ENCR_KEY_DATA ? " Encr" : "");
1711 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1712 " key_length=%u key_data_length=%u",
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001713 WPA_GET_BE16(key->key_length), key_data_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001714 wpa_hexdump(MSG_DEBUG, " replay_counter",
1715 key->replay_counter, WPA_REPLAY_COUNTER_LEN);
1716 wpa_hexdump(MSG_DEBUG, " key_nonce", key->key_nonce, WPA_NONCE_LEN);
1717 wpa_hexdump(MSG_DEBUG, " key_iv", key->key_iv, 16);
1718 wpa_hexdump(MSG_DEBUG, " key_rsc", key->key_rsc, 8);
1719 wpa_hexdump(MSG_DEBUG, " key_id (reserved)", key->key_id, 8);
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001720 wpa_hexdump(MSG_DEBUG, " key_mic", mic, mic_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001721#endif /* CONFIG_NO_STDOUT_DEBUG */
1722}
1723
1724
1725/**
1726 * wpa_sm_rx_eapol - Process received WPA EAPOL frames
1727 * @sm: Pointer to WPA state machine data from wpa_sm_init()
1728 * @src_addr: Source MAC address of the EAPOL packet
1729 * @buf: Pointer to the beginning of the EAPOL data (EAPOL header)
1730 * @len: Length of the EAPOL frame
1731 * Returns: 1 = WPA EAPOL-Key processed, 0 = not a WPA EAPOL-Key, -1 failure
1732 *
1733 * This function is called for each received EAPOL frame. Other than EAPOL-Key
1734 * frames can be skipped if filtering is done elsewhere. wpa_sm_rx_eapol() is
1735 * only processing WPA and WPA2 EAPOL-Key frames.
1736 *
1737 * The received EAPOL-Key packets are validated and valid packets are replied
1738 * to. In addition, key material (PTK, GTK) is configured at the end of a
1739 * successful key handshake.
1740 */
1741int wpa_sm_rx_eapol(struct wpa_sm *sm, const u8 *src_addr,
1742 const u8 *buf, size_t len)
1743{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001744 size_t plen, data_len, key_data_len;
1745 const struct ieee802_1x_hdr *hdr;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001746 struct wpa_eapol_key *key;
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001747 struct wpa_eapol_key_192 *key192;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001748 u16 key_info, ver;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001749 u8 *tmp = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001750 int ret = -1;
1751 struct wpa_peerkey *peerkey = NULL;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001752 u8 *key_data;
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001753 size_t mic_len, keyhdrlen;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001754
1755#ifdef CONFIG_IEEE80211R
1756 sm->ft_completed = 0;
1757#endif /* CONFIG_IEEE80211R */
1758
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001759 mic_len = wpa_mic_len(sm->key_mgmt);
1760 keyhdrlen = mic_len == 24 ? sizeof(*key192) : sizeof(*key);
1761
1762 if (len < sizeof(*hdr) + keyhdrlen) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001763 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1764 "WPA: EAPOL frame too short to be a WPA "
1765 "EAPOL-Key (len %lu, expecting at least %lu)",
1766 (unsigned long) len,
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001767 (unsigned long) sizeof(*hdr) + keyhdrlen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001768 return 0;
1769 }
1770
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001771 hdr = (const struct ieee802_1x_hdr *) buf;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001772 plen = be_to_host16(hdr->length);
1773 data_len = plen + sizeof(*hdr);
1774 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1775 "IEEE 802.1X RX: version=%d type=%d length=%lu",
1776 hdr->version, hdr->type, (unsigned long) plen);
1777
1778 if (hdr->version < EAPOL_VERSION) {
1779 /* TODO: backwards compatibility */
1780 }
1781 if (hdr->type != IEEE802_1X_TYPE_EAPOL_KEY) {
1782 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1783 "WPA: EAPOL frame (type %u) discarded, "
1784 "not a Key frame", hdr->type);
1785 ret = 0;
1786 goto out;
1787 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001788 wpa_hexdump(MSG_MSGDUMP, "WPA: RX EAPOL-Key", buf, len);
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001789 if (plen > len - sizeof(*hdr) || plen < keyhdrlen) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001790 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1791 "WPA: EAPOL frame payload size %lu "
1792 "invalid (frame size %lu)",
1793 (unsigned long) plen, (unsigned long) len);
1794 ret = 0;
1795 goto out;
1796 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001797 if (data_len < len) {
1798 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1799 "WPA: ignoring %lu bytes after the IEEE 802.1X data",
1800 (unsigned long) len - data_len);
1801 }
1802
1803 /*
1804 * Make a copy of the frame since we need to modify the buffer during
1805 * MAC validation and Key Data decryption.
1806 */
1807 tmp = os_malloc(data_len);
1808 if (tmp == NULL)
1809 goto out;
1810 os_memcpy(tmp, buf, data_len);
1811 key = (struct wpa_eapol_key *) (tmp + sizeof(struct ieee802_1x_hdr));
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001812 key192 = (struct wpa_eapol_key_192 *)
1813 (tmp + sizeof(struct ieee802_1x_hdr));
1814 if (mic_len == 24)
1815 key_data = (u8 *) (key192 + 1);
1816 else
1817 key_data = (u8 *) (key + 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001818
1819 if (key->type != EAPOL_KEY_TYPE_WPA && key->type != EAPOL_KEY_TYPE_RSN)
1820 {
1821 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1822 "WPA: EAPOL-Key type (%d) unknown, discarded",
1823 key->type);
1824 ret = 0;
1825 goto out;
1826 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001827
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001828 if (mic_len == 24)
1829 key_data_len = WPA_GET_BE16(key192->key_data_length);
1830 else
1831 key_data_len = WPA_GET_BE16(key->key_data_length);
1832 wpa_eapol_key_dump(sm, key, key_data_len, key192->key_mic, mic_len);
1833
1834 if (key_data_len > plen - keyhdrlen) {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001835 wpa_msg(sm->ctx->msg_ctx, MSG_INFO, "WPA: Invalid EAPOL-Key "
1836 "frame - key_data overflow (%u > %u)",
1837 (unsigned int) key_data_len,
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001838 (unsigned int) (plen - keyhdrlen));
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001839 goto out;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001840 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001841
1842 eapol_sm_notify_lower_layer_success(sm->eapol, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001843 key_info = WPA_GET_BE16(key->key_info);
1844 ver = key_info & WPA_KEY_INFO_TYPE_MASK;
1845 if (ver != WPA_KEY_INFO_TYPE_HMAC_MD5_RC4 &&
1846#if defined(CONFIG_IEEE80211R) || defined(CONFIG_IEEE80211W)
1847 ver != WPA_KEY_INFO_TYPE_AES_128_CMAC &&
1848#endif /* CONFIG_IEEE80211R || CONFIG_IEEE80211W */
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001849 ver != WPA_KEY_INFO_TYPE_HMAC_SHA1_AES &&
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001850 !wpa_key_mgmt_suite_b(sm->key_mgmt) &&
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001851 sm->key_mgmt != WPA_KEY_MGMT_OSEN) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001852 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1853 "WPA: Unsupported EAPOL-Key descriptor version %d",
1854 ver);
1855 goto out;
1856 }
1857
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001858 if (sm->key_mgmt == WPA_KEY_MGMT_OSEN &&
1859 ver != WPA_KEY_INFO_TYPE_AKM_DEFINED) {
1860 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1861 "OSEN: Unsupported EAPOL-Key descriptor version %d",
1862 ver);
1863 goto out;
1864 }
1865
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001866 if (wpa_key_mgmt_suite_b(sm->key_mgmt) &&
1867 ver != WPA_KEY_INFO_TYPE_AKM_DEFINED) {
1868 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1869 "RSN: Unsupported EAPOL-Key descriptor version %d (expected AKM defined = 0)",
1870 ver);
1871 goto out;
1872 }
1873
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001874#ifdef CONFIG_IEEE80211R
1875 if (wpa_key_mgmt_ft(sm->key_mgmt)) {
1876 /* IEEE 802.11r uses a new key_info type (AES-128-CMAC). */
1877 if (ver != WPA_KEY_INFO_TYPE_AES_128_CMAC) {
1878 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1879 "FT: AP did not use AES-128-CMAC");
1880 goto out;
1881 }
1882 } else
1883#endif /* CONFIG_IEEE80211R */
1884#ifdef CONFIG_IEEE80211W
1885 if (wpa_key_mgmt_sha256(sm->key_mgmt)) {
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001886 if (ver != WPA_KEY_INFO_TYPE_AES_128_CMAC &&
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001887 sm->key_mgmt != WPA_KEY_MGMT_OSEN &&
1888 !wpa_key_mgmt_suite_b(sm->key_mgmt)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001889 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1890 "WPA: AP did not use the "
1891 "negotiated AES-128-CMAC");
1892 goto out;
1893 }
1894 } else
1895#endif /* CONFIG_IEEE80211W */
1896 if (sm->pairwise_cipher == WPA_CIPHER_CCMP &&
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001897 !wpa_key_mgmt_suite_b(sm->key_mgmt) &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001898 ver != WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
1899 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1900 "WPA: CCMP is used, but EAPOL-Key "
1901 "descriptor version (%d) is not 2", ver);
1902 if (sm->group_cipher != WPA_CIPHER_CCMP &&
1903 !(key_info & WPA_KEY_INFO_KEY_TYPE)) {
1904 /* Earlier versions of IEEE 802.11i did not explicitly
1905 * require version 2 descriptor for all EAPOL-Key
1906 * packets, so allow group keys to use version 1 if
1907 * CCMP is not used for them. */
1908 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1909 "WPA: Backwards compatibility: allow invalid "
1910 "version for non-CCMP group keys");
Jouni Malinen658fb4a2014-11-14 20:57:05 +02001911 } else if (ver == WPA_KEY_INFO_TYPE_AES_128_CMAC) {
1912 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1913 "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 -07001914 } else
1915 goto out;
Dmitry Shmidt71757432014-06-02 13:50:35 -07001916 } else if (sm->pairwise_cipher == WPA_CIPHER_GCMP &&
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001917 !wpa_key_mgmt_suite_b(sm->key_mgmt) &&
Dmitry Shmidt71757432014-06-02 13:50:35 -07001918 ver != WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001919 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1920 "WPA: GCMP is used, but EAPOL-Key "
1921 "descriptor version (%d) is not 2", ver);
1922 goto out;
1923 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001924
1925#ifdef CONFIG_PEERKEY
1926 for (peerkey = sm->peerkey; peerkey; peerkey = peerkey->next) {
1927 if (os_memcmp(peerkey->addr, src_addr, ETH_ALEN) == 0)
1928 break;
1929 }
1930
1931 if (!(key_info & WPA_KEY_INFO_SMK_MESSAGE) && peerkey) {
1932 if (!peerkey->initiator && peerkey->replay_counter_set &&
1933 os_memcmp(key->replay_counter, peerkey->replay_counter,
1934 WPA_REPLAY_COUNTER_LEN) <= 0) {
1935 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1936 "RSN: EAPOL-Key Replay Counter did not "
1937 "increase (STK) - dropping packet");
1938 goto out;
1939 } else if (peerkey->initiator) {
1940 u8 _tmp[WPA_REPLAY_COUNTER_LEN];
1941 os_memcpy(_tmp, key->replay_counter,
1942 WPA_REPLAY_COUNTER_LEN);
1943 inc_byte_array(_tmp, WPA_REPLAY_COUNTER_LEN);
1944 if (os_memcmp(_tmp, peerkey->replay_counter,
1945 WPA_REPLAY_COUNTER_LEN) != 0) {
1946 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1947 "RSN: EAPOL-Key Replay "
1948 "Counter did not match (STK) - "
1949 "dropping packet");
1950 goto out;
1951 }
1952 }
1953 }
1954
1955 if (peerkey && peerkey->initiator && (key_info & WPA_KEY_INFO_ACK)) {
1956 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1957 "RSN: Ack bit in key_info from STK peer");
1958 goto out;
1959 }
1960#endif /* CONFIG_PEERKEY */
1961
1962 if (!peerkey && sm->rx_replay_counter_set &&
1963 os_memcmp(key->replay_counter, sm->rx_replay_counter,
1964 WPA_REPLAY_COUNTER_LEN) <= 0) {
1965 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1966 "WPA: EAPOL-Key Replay Counter did not increase - "
1967 "dropping packet");
1968 goto out;
1969 }
1970
1971 if (!(key_info & (WPA_KEY_INFO_ACK | WPA_KEY_INFO_SMK_MESSAGE))
1972#ifdef CONFIG_PEERKEY
1973 && (peerkey == NULL || !peerkey->initiator)
1974#endif /* CONFIG_PEERKEY */
1975 ) {
1976 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1977 "WPA: No Ack bit in key_info");
1978 goto out;
1979 }
1980
1981 if (key_info & WPA_KEY_INFO_REQUEST) {
1982 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1983 "WPA: EAPOL-Key with Request bit - dropped");
1984 goto out;
1985 }
1986
1987 if ((key_info & WPA_KEY_INFO_MIC) && !peerkey &&
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001988 wpa_supplicant_verify_eapol_key_mic(sm, key192, ver, tmp, data_len))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001989 goto out;
1990
1991#ifdef CONFIG_PEERKEY
1992 if ((key_info & WPA_KEY_INFO_MIC) && peerkey &&
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001993 peerkey_verify_eapol_key_mic(sm, peerkey, key192, ver, tmp,
1994 data_len))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001995 goto out;
1996#endif /* CONFIG_PEERKEY */
1997
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001998 if ((sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN) &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001999 (key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002000 if (wpa_supplicant_decrypt_key_data(sm, key, ver, key_data,
2001 &key_data_len))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002002 goto out;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002003 }
2004
2005 if (key_info & WPA_KEY_INFO_KEY_TYPE) {
2006 if (key_info & WPA_KEY_INFO_KEY_INDEX_MASK) {
2007 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
2008 "WPA: Ignored EAPOL-Key (Pairwise) with "
2009 "non-zero key index");
2010 goto out;
2011 }
2012 if (peerkey) {
2013 /* PeerKey 4-Way Handshake */
Dmitry Shmidtc2817022014-07-02 10:32:10 -07002014 peerkey_rx_eapol_4way(sm, peerkey, key, key_info, ver,
2015 key_data, key_data_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002016 } else if (key_info & WPA_KEY_INFO_MIC) {
2017 /* 3/4 4-Way Handshake */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002018 wpa_supplicant_process_3_of_4(sm, key, ver, key_data,
2019 key_data_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002020 } else {
2021 /* 1/4 4-Way Handshake */
2022 wpa_supplicant_process_1_of_4(sm, src_addr, key,
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002023 ver, key_data,
2024 key_data_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002025 }
2026 } else if (key_info & WPA_KEY_INFO_SMK_MESSAGE) {
2027 /* PeerKey SMK Handshake */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002028 peerkey_rx_eapol_smk(sm, src_addr, key, key_data_len, key_info,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002029 ver);
2030 } else {
2031 if (key_info & WPA_KEY_INFO_MIC) {
2032 /* 1/2 Group Key Handshake */
2033 wpa_supplicant_process_1_of_2(sm, src_addr, key,
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002034 key_data, key_data_len,
2035 ver);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002036 } else {
2037 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
2038 "WPA: EAPOL-Key (Group) without Mic bit - "
2039 "dropped");
2040 }
2041 }
2042
2043 ret = 1;
2044
2045out:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002046 bin_clear_free(tmp, data_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002047 return ret;
2048}
2049
2050
2051#ifdef CONFIG_CTRL_IFACE
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002052static u32 wpa_key_mgmt_suite(struct wpa_sm *sm)
2053{
2054 switch (sm->key_mgmt) {
2055 case WPA_KEY_MGMT_IEEE8021X:
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002056 return ((sm->proto == WPA_PROTO_RSN ||
2057 sm->proto == WPA_PROTO_OSEN) ?
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002058 RSN_AUTH_KEY_MGMT_UNSPEC_802_1X :
2059 WPA_AUTH_KEY_MGMT_UNSPEC_802_1X);
2060 case WPA_KEY_MGMT_PSK:
2061 return (sm->proto == WPA_PROTO_RSN ?
2062 RSN_AUTH_KEY_MGMT_PSK_OVER_802_1X :
2063 WPA_AUTH_KEY_MGMT_PSK_OVER_802_1X);
2064#ifdef CONFIG_IEEE80211R
2065 case WPA_KEY_MGMT_FT_IEEE8021X:
2066 return RSN_AUTH_KEY_MGMT_FT_802_1X;
2067 case WPA_KEY_MGMT_FT_PSK:
2068 return RSN_AUTH_KEY_MGMT_FT_PSK;
2069#endif /* CONFIG_IEEE80211R */
2070#ifdef CONFIG_IEEE80211W
2071 case WPA_KEY_MGMT_IEEE8021X_SHA256:
2072 return RSN_AUTH_KEY_MGMT_802_1X_SHA256;
2073 case WPA_KEY_MGMT_PSK_SHA256:
2074 return RSN_AUTH_KEY_MGMT_PSK_SHA256;
2075#endif /* CONFIG_IEEE80211W */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002076 case WPA_KEY_MGMT_CCKM:
2077 return (sm->proto == WPA_PROTO_RSN ?
2078 RSN_AUTH_KEY_MGMT_CCKM:
2079 WPA_AUTH_KEY_MGMT_CCKM);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002080 case WPA_KEY_MGMT_WPA_NONE:
2081 return WPA_AUTH_KEY_MGMT_NONE;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002082 case WPA_KEY_MGMT_IEEE8021X_SUITE_B:
2083 return RSN_AUTH_KEY_MGMT_802_1X_SUITE_B;
Dmitry Shmidt807291d2015-01-27 13:40:23 -08002084 case WPA_KEY_MGMT_IEEE8021X_SUITE_B_192:
2085 return RSN_AUTH_KEY_MGMT_802_1X_SUITE_B_192;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002086 default:
2087 return 0;
2088 }
2089}
2090
2091
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002092#define RSN_SUITE "%02x-%02x-%02x-%d"
2093#define RSN_SUITE_ARG(s) \
2094((s) >> 24) & 0xff, ((s) >> 16) & 0xff, ((s) >> 8) & 0xff, (s) & 0xff
2095
2096/**
2097 * wpa_sm_get_mib - Dump text list of MIB entries
2098 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2099 * @buf: Buffer for the list
2100 * @buflen: Length of the buffer
2101 * Returns: Number of bytes written to buffer
2102 *
2103 * This function is used fetch dot11 MIB variables.
2104 */
2105int wpa_sm_get_mib(struct wpa_sm *sm, char *buf, size_t buflen)
2106{
2107 char pmkid_txt[PMKID_LEN * 2 + 1];
2108 int rsna, ret;
2109 size_t len;
2110
2111 if (sm->cur_pmksa) {
2112 wpa_snprintf_hex(pmkid_txt, sizeof(pmkid_txt),
2113 sm->cur_pmksa->pmkid, PMKID_LEN);
2114 } else
2115 pmkid_txt[0] = '\0';
2116
2117 if ((wpa_key_mgmt_wpa_psk(sm->key_mgmt) ||
2118 wpa_key_mgmt_wpa_ieee8021x(sm->key_mgmt)) &&
2119 sm->proto == WPA_PROTO_RSN)
2120 rsna = 1;
2121 else
2122 rsna = 0;
2123
2124 ret = os_snprintf(buf, buflen,
2125 "dot11RSNAOptionImplemented=TRUE\n"
2126 "dot11RSNAPreauthenticationImplemented=TRUE\n"
2127 "dot11RSNAEnabled=%s\n"
2128 "dot11RSNAPreauthenticationEnabled=%s\n"
2129 "dot11RSNAConfigVersion=%d\n"
2130 "dot11RSNAConfigPairwiseKeysSupported=5\n"
2131 "dot11RSNAConfigGroupCipherSize=%d\n"
2132 "dot11RSNAConfigPMKLifetime=%d\n"
2133 "dot11RSNAConfigPMKReauthThreshold=%d\n"
2134 "dot11RSNAConfigNumberOfPTKSAReplayCounters=1\n"
2135 "dot11RSNAConfigSATimeout=%d\n",
2136 rsna ? "TRUE" : "FALSE",
2137 rsna ? "TRUE" : "FALSE",
2138 RSN_VERSION,
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07002139 wpa_cipher_key_len(sm->group_cipher) * 8,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002140 sm->dot11RSNAConfigPMKLifetime,
2141 sm->dot11RSNAConfigPMKReauthThreshold,
2142 sm->dot11RSNAConfigSATimeout);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002143 if (os_snprintf_error(buflen, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002144 return 0;
2145 len = ret;
2146
2147 ret = os_snprintf(
2148 buf + len, buflen - len,
2149 "dot11RSNAAuthenticationSuiteSelected=" RSN_SUITE "\n"
2150 "dot11RSNAPairwiseCipherSelected=" RSN_SUITE "\n"
2151 "dot11RSNAGroupCipherSelected=" RSN_SUITE "\n"
2152 "dot11RSNAPMKIDUsed=%s\n"
2153 "dot11RSNAAuthenticationSuiteRequested=" RSN_SUITE "\n"
2154 "dot11RSNAPairwiseCipherRequested=" RSN_SUITE "\n"
2155 "dot11RSNAGroupCipherRequested=" RSN_SUITE "\n"
2156 "dot11RSNAConfigNumberOfGTKSAReplayCounters=0\n"
2157 "dot11RSNA4WayHandshakeFailures=%u\n",
2158 RSN_SUITE_ARG(wpa_key_mgmt_suite(sm)),
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07002159 RSN_SUITE_ARG(wpa_cipher_to_suite(sm->proto,
2160 sm->pairwise_cipher)),
2161 RSN_SUITE_ARG(wpa_cipher_to_suite(sm->proto,
2162 sm->group_cipher)),
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002163 pmkid_txt,
2164 RSN_SUITE_ARG(wpa_key_mgmt_suite(sm)),
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07002165 RSN_SUITE_ARG(wpa_cipher_to_suite(sm->proto,
2166 sm->pairwise_cipher)),
2167 RSN_SUITE_ARG(wpa_cipher_to_suite(sm->proto,
2168 sm->group_cipher)),
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002169 sm->dot11RSNA4WayHandshakeFailures);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002170 if (!os_snprintf_error(buflen - len, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002171 len += ret;
2172
2173 return (int) len;
2174}
2175#endif /* CONFIG_CTRL_IFACE */
2176
2177
2178static void wpa_sm_pmksa_free_cb(struct rsn_pmksa_cache_entry *entry,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002179 void *ctx, enum pmksa_free_reason reason)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002180{
2181 struct wpa_sm *sm = ctx;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002182 int deauth = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002183
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002184 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "RSN: PMKSA cache entry free_cb: "
2185 MACSTR " reason=%d", MAC2STR(entry->aa), reason);
2186
2187 if (sm->cur_pmksa == entry) {
2188 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2189 "RSN: %s current PMKSA entry",
2190 reason == PMKSA_REPLACE ? "replaced" : "removed");
2191 pmksa_cache_clear_current(sm);
2192
2193 /*
2194 * If an entry is simply being replaced, there's no need to
2195 * deauthenticate because it will be immediately re-added.
2196 * This happens when EAP authentication is completed again
2197 * (reauth or failed PMKSA caching attempt).
2198 */
2199 if (reason != PMKSA_REPLACE)
2200 deauth = 1;
2201 }
2202
2203 if (reason == PMKSA_EXPIRE &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002204 (sm->pmk_len == entry->pmk_len &&
2205 os_memcmp(sm->pmk, entry->pmk, sm->pmk_len) == 0)) {
2206 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002207 "RSN: deauthenticating due to expired PMK");
2208 pmksa_cache_clear_current(sm);
2209 deauth = 1;
2210 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002211
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002212 if (deauth) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002213 os_memset(sm->pmk, 0, sizeof(sm->pmk));
2214 wpa_sm_deauthenticate(sm, WLAN_REASON_UNSPECIFIED);
2215 }
2216}
2217
2218
2219/**
2220 * wpa_sm_init - Initialize WPA state machine
2221 * @ctx: Context pointer for callbacks; this needs to be an allocated buffer
2222 * Returns: Pointer to the allocated WPA state machine data
2223 *
2224 * This function is used to allocate a new WPA state machine and the returned
2225 * value is passed to all WPA state machine calls.
2226 */
2227struct wpa_sm * wpa_sm_init(struct wpa_sm_ctx *ctx)
2228{
2229 struct wpa_sm *sm;
2230
2231 sm = os_zalloc(sizeof(*sm));
2232 if (sm == NULL)
2233 return NULL;
2234 dl_list_init(&sm->pmksa_candidates);
2235 sm->renew_snonce = 1;
2236 sm->ctx = ctx;
2237
2238 sm->dot11RSNAConfigPMKLifetime = 43200;
2239 sm->dot11RSNAConfigPMKReauthThreshold = 70;
2240 sm->dot11RSNAConfigSATimeout = 60;
2241
2242 sm->pmksa = pmksa_cache_init(wpa_sm_pmksa_free_cb, sm, sm);
2243 if (sm->pmksa == NULL) {
2244 wpa_msg(sm->ctx->msg_ctx, MSG_ERROR,
2245 "RSN: PMKSA cache initialization failed");
2246 os_free(sm);
2247 return NULL;
2248 }
2249
2250 return sm;
2251}
2252
2253
2254/**
2255 * wpa_sm_deinit - Deinitialize WPA state machine
2256 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2257 */
2258void wpa_sm_deinit(struct wpa_sm *sm)
2259{
2260 if (sm == NULL)
2261 return;
2262 pmksa_cache_deinit(sm->pmksa);
2263 eloop_cancel_timeout(wpa_sm_start_preauth, sm, NULL);
2264 eloop_cancel_timeout(wpa_sm_rekey_ptk, sm, NULL);
2265 os_free(sm->assoc_wpa_ie);
2266 os_free(sm->ap_wpa_ie);
2267 os_free(sm->ap_rsn_ie);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002268 wpa_sm_drop_sa(sm);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002269 os_free(sm->ctx);
2270 peerkey_deinit(sm);
2271#ifdef CONFIG_IEEE80211R
2272 os_free(sm->assoc_resp_ies);
2273#endif /* CONFIG_IEEE80211R */
2274 os_free(sm);
2275}
2276
2277
2278/**
2279 * wpa_sm_notify_assoc - Notify WPA state machine about association
2280 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2281 * @bssid: The BSSID of the new association
2282 *
2283 * This function is called to let WPA state machine know that the connection
2284 * was established.
2285 */
2286void wpa_sm_notify_assoc(struct wpa_sm *sm, const u8 *bssid)
2287{
Mathy Vanhoef2468ddd2017-07-12 16:03:24 +02002288 int clear_keys = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002289
2290 if (sm == NULL)
2291 return;
2292
2293 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2294 "WPA: Association event - clear replay counter");
2295 os_memcpy(sm->bssid, bssid, ETH_ALEN);
2296 os_memset(sm->rx_replay_counter, 0, WPA_REPLAY_COUNTER_LEN);
2297 sm->rx_replay_counter_set = 0;
2298 sm->renew_snonce = 1;
2299 if (os_memcmp(sm->preauth_bssid, bssid, ETH_ALEN) == 0)
2300 rsn_preauth_deinit(sm);
2301
2302#ifdef CONFIG_IEEE80211R
2303 if (wpa_ft_is_completed(sm)) {
2304 /*
2305 * Clear portValid to kick EAPOL state machine to re-enter
2306 * AUTHENTICATED state to get the EAPOL port Authorized.
2307 */
2308 eapol_sm_notify_portValid(sm->eapol, FALSE);
2309 wpa_supplicant_key_neg_complete(sm, sm->bssid, 1);
2310
2311 /* Prepare for the next transition */
2312 wpa_ft_prepare_auth_request(sm, NULL);
2313
Mathy Vanhoef2468ddd2017-07-12 16:03:24 +02002314 clear_keys = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002315 }
2316#endif /* CONFIG_IEEE80211R */
2317
Mathy Vanhoef2468ddd2017-07-12 16:03:24 +02002318 if (clear_keys) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002319 /*
2320 * IEEE 802.11, 8.4.10: Delete PTK SA on (re)association if
2321 * this is not part of a Fast BSS Transition.
2322 */
2323 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Clear old PTK");
2324 sm->ptk_set = 0;
Dmitry Shmidt61593f02014-04-21 16:27:35 -07002325 os_memset(&sm->ptk, 0, sizeof(sm->ptk));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002326 sm->tptk_set = 0;
Dmitry Shmidt61593f02014-04-21 16:27:35 -07002327 os_memset(&sm->tptk, 0, sizeof(sm->tptk));
Mathy Vanhoef2468ddd2017-07-12 16:03:24 +02002328 os_memset(&sm->gtk, 0, sizeof(sm->gtk));
2329#ifdef CONFIG_IEEE80211W
2330 os_memset(&sm->igtk, 0, sizeof(sm->igtk));
2331#endif /* CONFIG_IEEE80211W */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002332 }
2333
2334#ifdef CONFIG_TDLS
2335 wpa_tdls_assoc(sm);
2336#endif /* CONFIG_TDLS */
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002337
2338#ifdef CONFIG_P2P
2339 os_memset(sm->p2p_ip_addr, 0, sizeof(sm->p2p_ip_addr));
2340#endif /* CONFIG_P2P */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002341}
2342
2343
2344/**
2345 * wpa_sm_notify_disassoc - Notify WPA state machine about disassociation
2346 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2347 *
2348 * This function is called to let WPA state machine know that the connection
2349 * was lost. This will abort any existing pre-authentication session.
2350 */
2351void wpa_sm_notify_disassoc(struct wpa_sm *sm)
2352{
Dmitry Shmidt4dd28dc2015-03-10 11:21:43 -07002353 eloop_cancel_timeout(wpa_sm_start_preauth, sm, NULL);
2354 eloop_cancel_timeout(wpa_sm_rekey_ptk, sm, NULL);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002355 peerkey_deinit(sm);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002356 rsn_preauth_deinit(sm);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002357 pmksa_cache_clear_current(sm);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002358 if (wpa_sm_get_state(sm) == WPA_4WAY_HANDSHAKE)
2359 sm->dot11RSNA4WayHandshakeFailures++;
2360#ifdef CONFIG_TDLS
2361 wpa_tdls_disassoc(sm);
2362#endif /* CONFIG_TDLS */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002363
2364 /* Keys are not needed in the WPA state machine anymore */
2365 wpa_sm_drop_sa(sm);
Dmitry Shmidt4dd28dc2015-03-10 11:21:43 -07002366
2367 sm->msg_3_of_4_ok = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002368}
2369
2370
2371/**
2372 * wpa_sm_set_pmk - Set PMK
2373 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2374 * @pmk: The new PMK
2375 * @pmk_len: The length of the new PMK in bytes
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002376 * @bssid: AA to add into PMKSA cache or %NULL to not cache the PMK
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002377 *
2378 * Configure the PMK for WPA state machine.
2379 */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002380void wpa_sm_set_pmk(struct wpa_sm *sm, const u8 *pmk, size_t pmk_len,
2381 const u8 *bssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002382{
2383 if (sm == NULL)
2384 return;
2385
2386 sm->pmk_len = pmk_len;
2387 os_memcpy(sm->pmk, pmk, pmk_len);
2388
2389#ifdef CONFIG_IEEE80211R
2390 /* Set XXKey to be PSK for FT key derivation */
2391 sm->xxkey_len = pmk_len;
2392 os_memcpy(sm->xxkey, pmk, pmk_len);
2393#endif /* CONFIG_IEEE80211R */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002394
2395 if (bssid) {
2396 pmksa_cache_add(sm->pmksa, pmk, pmk_len, NULL, 0,
2397 bssid, sm->own_addr,
2398 sm->network_ctx, sm->key_mgmt);
2399 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002400}
2401
2402
2403/**
2404 * wpa_sm_set_pmk_from_pmksa - Set PMK based on the current PMKSA
2405 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2406 *
2407 * Take the PMK from the current PMKSA into use. If no PMKSA is active, the PMK
2408 * will be cleared.
2409 */
2410void wpa_sm_set_pmk_from_pmksa(struct wpa_sm *sm)
2411{
2412 if (sm == NULL)
2413 return;
2414
2415 if (sm->cur_pmksa) {
2416 sm->pmk_len = sm->cur_pmksa->pmk_len;
2417 os_memcpy(sm->pmk, sm->cur_pmksa->pmk, sm->pmk_len);
2418 } else {
2419 sm->pmk_len = PMK_LEN;
2420 os_memset(sm->pmk, 0, PMK_LEN);
2421 }
2422}
2423
2424
2425/**
2426 * wpa_sm_set_fast_reauth - Set fast reauthentication (EAP) enabled/disabled
2427 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2428 * @fast_reauth: Whether fast reauthentication (EAP) is allowed
2429 */
2430void wpa_sm_set_fast_reauth(struct wpa_sm *sm, int fast_reauth)
2431{
2432 if (sm)
2433 sm->fast_reauth = fast_reauth;
2434}
2435
2436
2437/**
2438 * wpa_sm_set_scard_ctx - Set context pointer for smartcard callbacks
2439 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2440 * @scard_ctx: Context pointer for smartcard related callback functions
2441 */
2442void wpa_sm_set_scard_ctx(struct wpa_sm *sm, void *scard_ctx)
2443{
2444 if (sm == NULL)
2445 return;
2446 sm->scard_ctx = scard_ctx;
2447 if (sm->preauth_eapol)
2448 eapol_sm_register_scard_ctx(sm->preauth_eapol, scard_ctx);
2449}
2450
2451
2452/**
2453 * wpa_sm_set_config - Notification of current configration change
2454 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2455 * @config: Pointer to current network configuration
2456 *
2457 * Notify WPA state machine that configuration has changed. config will be
2458 * stored as a backpointer to network configuration. This can be %NULL to clear
2459 * the stored pointed.
2460 */
2461void wpa_sm_set_config(struct wpa_sm *sm, struct rsn_supp_config *config)
2462{
2463 if (!sm)
2464 return;
2465
2466 if (config) {
2467 sm->network_ctx = config->network_ctx;
2468 sm->peerkey_enabled = config->peerkey_enabled;
2469 sm->allowed_pairwise_cipher = config->allowed_pairwise_cipher;
2470 sm->proactive_key_caching = config->proactive_key_caching;
2471 sm->eap_workaround = config->eap_workaround;
2472 sm->eap_conf_ctx = config->eap_conf_ctx;
2473 if (config->ssid) {
2474 os_memcpy(sm->ssid, config->ssid, config->ssid_len);
2475 sm->ssid_len = config->ssid_len;
2476 } else
2477 sm->ssid_len = 0;
2478 sm->wpa_ptk_rekey = config->wpa_ptk_rekey;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002479 sm->p2p = config->p2p;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002480 } else {
2481 sm->network_ctx = NULL;
2482 sm->peerkey_enabled = 0;
2483 sm->allowed_pairwise_cipher = 0;
2484 sm->proactive_key_caching = 0;
2485 sm->eap_workaround = 0;
2486 sm->eap_conf_ctx = NULL;
2487 sm->ssid_len = 0;
2488 sm->wpa_ptk_rekey = 0;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002489 sm->p2p = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002490 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002491}
2492
2493
2494/**
2495 * wpa_sm_set_own_addr - Set own MAC address
2496 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2497 * @addr: Own MAC address
2498 */
2499void wpa_sm_set_own_addr(struct wpa_sm *sm, const u8 *addr)
2500{
2501 if (sm)
2502 os_memcpy(sm->own_addr, addr, ETH_ALEN);
2503}
2504
2505
2506/**
2507 * wpa_sm_set_ifname - Set network interface name
2508 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2509 * @ifname: Interface name
2510 * @bridge_ifname: Optional bridge interface name (for pre-auth)
2511 */
2512void wpa_sm_set_ifname(struct wpa_sm *sm, const char *ifname,
2513 const char *bridge_ifname)
2514{
2515 if (sm) {
2516 sm->ifname = ifname;
2517 sm->bridge_ifname = bridge_ifname;
2518 }
2519}
2520
2521
2522/**
2523 * wpa_sm_set_eapol - Set EAPOL state machine pointer
2524 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2525 * @eapol: Pointer to EAPOL state machine allocated with eapol_sm_init()
2526 */
2527void wpa_sm_set_eapol(struct wpa_sm *sm, struct eapol_sm *eapol)
2528{
2529 if (sm)
2530 sm->eapol = eapol;
2531}
2532
2533
2534/**
2535 * wpa_sm_set_param - Set WPA state machine parameters
2536 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2537 * @param: Parameter field
2538 * @value: Parameter value
2539 * Returns: 0 on success, -1 on failure
2540 */
2541int wpa_sm_set_param(struct wpa_sm *sm, enum wpa_sm_conf_params param,
2542 unsigned int value)
2543{
2544 int ret = 0;
2545
2546 if (sm == NULL)
2547 return -1;
2548
2549 switch (param) {
2550 case RSNA_PMK_LIFETIME:
2551 if (value > 0)
2552 sm->dot11RSNAConfigPMKLifetime = value;
2553 else
2554 ret = -1;
2555 break;
2556 case RSNA_PMK_REAUTH_THRESHOLD:
2557 if (value > 0 && value <= 100)
2558 sm->dot11RSNAConfigPMKReauthThreshold = value;
2559 else
2560 ret = -1;
2561 break;
2562 case RSNA_SA_TIMEOUT:
2563 if (value > 0)
2564 sm->dot11RSNAConfigSATimeout = value;
2565 else
2566 ret = -1;
2567 break;
2568 case WPA_PARAM_PROTO:
2569 sm->proto = value;
2570 break;
2571 case WPA_PARAM_PAIRWISE:
2572 sm->pairwise_cipher = value;
2573 break;
2574 case WPA_PARAM_GROUP:
2575 sm->group_cipher = value;
2576 break;
2577 case WPA_PARAM_KEY_MGMT:
2578 sm->key_mgmt = value;
2579 break;
2580#ifdef CONFIG_IEEE80211W
2581 case WPA_PARAM_MGMT_GROUP:
2582 sm->mgmt_group_cipher = value;
2583 break;
2584#endif /* CONFIG_IEEE80211W */
2585 case WPA_PARAM_RSN_ENABLED:
2586 sm->rsn_enabled = value;
2587 break;
2588 case WPA_PARAM_MFP:
2589 sm->mfp = value;
2590 break;
2591 default:
2592 break;
2593 }
2594
2595 return ret;
2596}
2597
2598
2599/**
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002600 * wpa_sm_get_status - Get WPA state machine
2601 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2602 * @buf: Buffer for status information
2603 * @buflen: Maximum buffer length
2604 * @verbose: Whether to include verbose status information
2605 * Returns: Number of bytes written to buf.
2606 *
2607 * Query WPA state machine for status information. This function fills in
2608 * a text area with current status information. If the buffer (buf) is not
2609 * large enough, status information will be truncated to fit the buffer.
2610 */
2611int wpa_sm_get_status(struct wpa_sm *sm, char *buf, size_t buflen,
2612 int verbose)
2613{
2614 char *pos = buf, *end = buf + buflen;
2615 int ret;
2616
2617 ret = os_snprintf(pos, end - pos,
2618 "pairwise_cipher=%s\n"
2619 "group_cipher=%s\n"
2620 "key_mgmt=%s\n",
2621 wpa_cipher_txt(sm->pairwise_cipher),
2622 wpa_cipher_txt(sm->group_cipher),
2623 wpa_key_mgmt_txt(sm->key_mgmt, sm->proto));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002624 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002625 return pos - buf;
2626 pos += ret;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002627
2628 if (sm->mfp != NO_MGMT_FRAME_PROTECTION && sm->ap_rsn_ie) {
2629 struct wpa_ie_data rsn;
2630 if (wpa_parse_wpa_ie_rsn(sm->ap_rsn_ie, sm->ap_rsn_ie_len, &rsn)
2631 >= 0 &&
2632 rsn.capabilities & (WPA_CAPABILITY_MFPR |
2633 WPA_CAPABILITY_MFPC)) {
2634 ret = os_snprintf(pos, end - pos, "pmf=%d\n",
2635 (rsn.capabilities &
2636 WPA_CAPABILITY_MFPR) ? 2 : 1);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002637 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002638 return pos - buf;
2639 pos += ret;
2640 }
2641 }
2642
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002643 return pos - buf;
2644}
2645
2646
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07002647int wpa_sm_pmf_enabled(struct wpa_sm *sm)
2648{
2649 struct wpa_ie_data rsn;
2650
2651 if (sm->mfp == NO_MGMT_FRAME_PROTECTION || !sm->ap_rsn_ie)
2652 return 0;
2653
2654 if (wpa_parse_wpa_ie_rsn(sm->ap_rsn_ie, sm->ap_rsn_ie_len, &rsn) >= 0 &&
2655 rsn.capabilities & (WPA_CAPABILITY_MFPR | WPA_CAPABILITY_MFPC))
2656 return 1;
2657
2658 return 0;
2659}
2660
2661
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002662/**
2663 * wpa_sm_set_assoc_wpa_ie_default - Generate own WPA/RSN IE from configuration
2664 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2665 * @wpa_ie: Pointer to buffer for WPA/RSN IE
2666 * @wpa_ie_len: Pointer to the length of the wpa_ie buffer
2667 * Returns: 0 on success, -1 on failure
2668 */
2669int wpa_sm_set_assoc_wpa_ie_default(struct wpa_sm *sm, u8 *wpa_ie,
2670 size_t *wpa_ie_len)
2671{
2672 int res;
2673
2674 if (sm == NULL)
2675 return -1;
2676
2677 res = wpa_gen_wpa_ie(sm, wpa_ie, *wpa_ie_len);
2678 if (res < 0)
2679 return -1;
2680 *wpa_ie_len = res;
2681
2682 wpa_hexdump(MSG_DEBUG, "WPA: Set own WPA IE default",
2683 wpa_ie, *wpa_ie_len);
2684
2685 if (sm->assoc_wpa_ie == NULL) {
2686 /*
2687 * Make a copy of the WPA/RSN IE so that 4-Way Handshake gets
2688 * the correct version of the IE even if PMKSA caching is
2689 * aborted (which would remove PMKID from IE generation).
2690 */
2691 sm->assoc_wpa_ie = os_malloc(*wpa_ie_len);
2692 if (sm->assoc_wpa_ie == NULL)
2693 return -1;
2694
2695 os_memcpy(sm->assoc_wpa_ie, wpa_ie, *wpa_ie_len);
2696 sm->assoc_wpa_ie_len = *wpa_ie_len;
2697 }
2698
2699 return 0;
2700}
2701
2702
2703/**
2704 * wpa_sm_set_assoc_wpa_ie - Set own WPA/RSN IE from (Re)AssocReq
2705 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2706 * @ie: Pointer to IE data (starting from id)
2707 * @len: IE length
2708 * Returns: 0 on success, -1 on failure
2709 *
2710 * Inform WPA state machine about the WPA/RSN IE used in (Re)Association
2711 * Request frame. The IE will be used to override the default value generated
2712 * with wpa_sm_set_assoc_wpa_ie_default().
2713 */
2714int wpa_sm_set_assoc_wpa_ie(struct wpa_sm *sm, const u8 *ie, size_t len)
2715{
2716 if (sm == NULL)
2717 return -1;
2718
2719 os_free(sm->assoc_wpa_ie);
2720 if (ie == NULL || len == 0) {
2721 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2722 "WPA: clearing own WPA/RSN IE");
2723 sm->assoc_wpa_ie = NULL;
2724 sm->assoc_wpa_ie_len = 0;
2725 } else {
2726 wpa_hexdump(MSG_DEBUG, "WPA: set own WPA/RSN IE", ie, len);
2727 sm->assoc_wpa_ie = os_malloc(len);
2728 if (sm->assoc_wpa_ie == NULL)
2729 return -1;
2730
2731 os_memcpy(sm->assoc_wpa_ie, ie, len);
2732 sm->assoc_wpa_ie_len = len;
2733 }
2734
2735 return 0;
2736}
2737
2738
2739/**
2740 * wpa_sm_set_ap_wpa_ie - Set AP WPA IE from Beacon/ProbeResp
2741 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2742 * @ie: Pointer to IE data (starting from id)
2743 * @len: IE length
2744 * Returns: 0 on success, -1 on failure
2745 *
2746 * Inform WPA state machine about the WPA IE used in Beacon / Probe Response
2747 * frame.
2748 */
2749int wpa_sm_set_ap_wpa_ie(struct wpa_sm *sm, const u8 *ie, size_t len)
2750{
2751 if (sm == NULL)
2752 return -1;
2753
2754 os_free(sm->ap_wpa_ie);
2755 if (ie == NULL || len == 0) {
2756 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2757 "WPA: clearing AP WPA IE");
2758 sm->ap_wpa_ie = NULL;
2759 sm->ap_wpa_ie_len = 0;
2760 } else {
2761 wpa_hexdump(MSG_DEBUG, "WPA: set AP WPA IE", ie, len);
2762 sm->ap_wpa_ie = os_malloc(len);
2763 if (sm->ap_wpa_ie == NULL)
2764 return -1;
2765
2766 os_memcpy(sm->ap_wpa_ie, ie, len);
2767 sm->ap_wpa_ie_len = len;
2768 }
2769
2770 return 0;
2771}
2772
2773
2774/**
2775 * wpa_sm_set_ap_rsn_ie - Set AP RSN IE from Beacon/ProbeResp
2776 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2777 * @ie: Pointer to IE data (starting from id)
2778 * @len: IE length
2779 * Returns: 0 on success, -1 on failure
2780 *
2781 * Inform WPA state machine about the RSN IE used in Beacon / Probe Response
2782 * frame.
2783 */
2784int wpa_sm_set_ap_rsn_ie(struct wpa_sm *sm, const u8 *ie, size_t len)
2785{
2786 if (sm == NULL)
2787 return -1;
2788
2789 os_free(sm->ap_rsn_ie);
2790 if (ie == NULL || len == 0) {
2791 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2792 "WPA: clearing AP RSN IE");
2793 sm->ap_rsn_ie = NULL;
2794 sm->ap_rsn_ie_len = 0;
2795 } else {
2796 wpa_hexdump(MSG_DEBUG, "WPA: set AP RSN IE", ie, len);
2797 sm->ap_rsn_ie = os_malloc(len);
2798 if (sm->ap_rsn_ie == NULL)
2799 return -1;
2800
2801 os_memcpy(sm->ap_rsn_ie, ie, len);
2802 sm->ap_rsn_ie_len = len;
2803 }
2804
2805 return 0;
2806}
2807
2808
2809/**
2810 * wpa_sm_parse_own_wpa_ie - Parse own WPA/RSN IE
2811 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2812 * @data: Pointer to data area for parsing results
2813 * Returns: 0 on success, -1 if IE is not known, or -2 on parsing failure
2814 *
2815 * Parse the contents of the own WPA or RSN IE from (Re)AssocReq and write the
2816 * parsed data into data.
2817 */
2818int wpa_sm_parse_own_wpa_ie(struct wpa_sm *sm, struct wpa_ie_data *data)
2819{
2820 if (sm == NULL)
2821 return -1;
2822
2823 if (sm->assoc_wpa_ie == NULL) {
2824 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2825 "WPA: No WPA/RSN IE available from association info");
2826 return -1;
2827 }
2828 if (wpa_parse_wpa_ie(sm->assoc_wpa_ie, sm->assoc_wpa_ie_len, data))
2829 return -2;
2830 return 0;
2831}
2832
2833
2834int wpa_sm_pmksa_cache_list(struct wpa_sm *sm, char *buf, size_t len)
2835{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002836 return pmksa_cache_list(sm->pmksa, buf, len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002837}
2838
2839
2840void wpa_sm_drop_sa(struct wpa_sm *sm)
2841{
2842 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Clear old PMK and PTK");
2843 sm->ptk_set = 0;
2844 sm->tptk_set = 0;
2845 os_memset(sm->pmk, 0, sizeof(sm->pmk));
2846 os_memset(&sm->ptk, 0, sizeof(sm->ptk));
2847 os_memset(&sm->tptk, 0, sizeof(sm->tptk));
Mathy Vanhoef2468ddd2017-07-12 16:03:24 +02002848 os_memset(&sm->gtk, 0, sizeof(sm->gtk));
2849#ifdef CONFIG_IEEE80211W
2850 os_memset(&sm->igtk, 0, sizeof(sm->igtk));
2851#endif /* CONFIG_IEEE80211W */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002852#ifdef CONFIG_IEEE80211R
2853 os_memset(sm->xxkey, 0, sizeof(sm->xxkey));
2854 os_memset(sm->pmk_r0, 0, sizeof(sm->pmk_r0));
2855 os_memset(sm->pmk_r1, 0, sizeof(sm->pmk_r1));
2856#endif /* CONFIG_IEEE80211R */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002857}
2858
2859
2860int wpa_sm_has_ptk(struct wpa_sm *sm)
2861{
2862 if (sm == NULL)
2863 return 0;
2864 return sm->ptk_set;
2865}
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002866
2867
2868void wpa_sm_update_replay_ctr(struct wpa_sm *sm, const u8 *replay_ctr)
2869{
2870 os_memcpy(sm->rx_replay_counter, replay_ctr, WPA_REPLAY_COUNTER_LEN);
2871}
2872
2873
2874void wpa_sm_pmksa_cache_flush(struct wpa_sm *sm, void *network_ctx)
2875{
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07002876 pmksa_cache_flush(sm->pmksa, network_ctx, NULL, 0);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002877}
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002878
2879
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002880#ifdef CONFIG_WNM
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002881int wpa_wnmsleep_install_key(struct wpa_sm *sm, u8 subelem_id, u8 *buf)
2882{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002883 u16 keyinfo;
2884 u8 keylen; /* plaintext key len */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002885 u8 *key_rsc;
2886
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002887 if (subelem_id == WNM_SLEEP_SUBELEM_GTK) {
Dmitry Shmidt61593f02014-04-21 16:27:35 -07002888 struct wpa_gtk_data gd;
2889
2890 os_memset(&gd, 0, sizeof(gd));
2891 keylen = wpa_cipher_key_len(sm->group_cipher);
2892 gd.key_rsc_len = wpa_cipher_rsc_len(sm->group_cipher);
2893 gd.alg = wpa_cipher_to_alg(sm->group_cipher);
2894 if (gd.alg == WPA_ALG_NONE) {
2895 wpa_printf(MSG_DEBUG, "Unsupported group cipher suite");
2896 return -1;
2897 }
2898
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002899 key_rsc = buf + 5;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002900 keyinfo = WPA_GET_LE16(buf + 2);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002901 gd.gtk_len = keylen;
2902 if (gd.gtk_len != buf[4]) {
2903 wpa_printf(MSG_DEBUG, "GTK len mismatch len %d vs %d",
2904 gd.gtk_len, buf[4]);
2905 return -1;
2906 }
2907 gd.keyidx = keyinfo & 0x03; /* B0 - B1 */
2908 gd.tx = wpa_supplicant_gtk_tx_bit_workaround(
2909 sm, !!(keyinfo & WPA_KEY_INFO_TXRX));
2910
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002911 os_memcpy(gd.gtk, buf + 13, gd.gtk_len);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002912
2913 wpa_hexdump_key(MSG_DEBUG, "Install GTK (WNM SLEEP)",
2914 gd.gtk, gd.gtk_len);
2915 if (wpa_supplicant_install_gtk(sm, &gd, key_rsc)) {
Dmitry Shmidt61593f02014-04-21 16:27:35 -07002916 os_memset(&gd, 0, sizeof(gd));
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002917 wpa_printf(MSG_DEBUG, "Failed to install the GTK in "
2918 "WNM mode");
2919 return -1;
2920 }
Dmitry Shmidt61593f02014-04-21 16:27:35 -07002921 os_memset(&gd, 0, sizeof(gd));
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002922#ifdef CONFIG_IEEE80211W
2923 } else if (subelem_id == WNM_SLEEP_SUBELEM_IGTK) {
Mathy Vanhoef2468ddd2017-07-12 16:03:24 +02002924 const struct wpa_igtk_kde *igtk;
Dmitry Shmidt61593f02014-04-21 16:27:35 -07002925
Mathy Vanhoef2468ddd2017-07-12 16:03:24 +02002926 igtk = (const struct wpa_igtk_kde *) (buf + 2);
2927 if (wpa_supplicant_install_igtk(sm, igtk) < 0)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002928 return -1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002929#endif /* CONFIG_IEEE80211W */
2930 } else {
2931 wpa_printf(MSG_DEBUG, "Unknown element id");
2932 return -1;
2933 }
2934
2935 return 0;
2936}
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002937#endif /* CONFIG_WNM */
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002938
2939
2940#ifdef CONFIG_PEERKEY
2941int wpa_sm_rx_eapol_peerkey(struct wpa_sm *sm, const u8 *src_addr,
2942 const u8 *buf, size_t len)
2943{
2944 struct wpa_peerkey *peerkey;
2945
2946 for (peerkey = sm->peerkey; peerkey; peerkey = peerkey->next) {
2947 if (os_memcmp(peerkey->addr, src_addr, ETH_ALEN) == 0)
2948 break;
2949 }
2950
2951 if (!peerkey)
2952 return 0;
2953
2954 wpa_sm_rx_eapol(sm, src_addr, buf, len);
2955
2956 return 1;
2957}
2958#endif /* CONFIG_PEERKEY */
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002959
2960
2961#ifdef CONFIG_P2P
2962
2963int wpa_sm_get_p2p_ip_addr(struct wpa_sm *sm, u8 *buf)
2964{
2965 if (sm == NULL || WPA_GET_BE32(sm->p2p_ip_addr) == 0)
2966 return -1;
2967 os_memcpy(buf, sm->p2p_ip_addr, 3 * 4);
2968 return 0;
2969}
2970
2971#endif /* CONFIG_P2P */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002972
2973
2974void wpa_sm_set_rx_replay_ctr(struct wpa_sm *sm, const u8 *rx_replay_counter)
2975{
2976 if (rx_replay_counter == NULL)
2977 return;
2978
2979 os_memcpy(sm->rx_replay_counter, rx_replay_counter,
2980 WPA_REPLAY_COUNTER_LEN);
2981 sm->rx_replay_counter_set = 1;
2982 wpa_printf(MSG_DEBUG, "Updated key replay counter");
2983}
2984
2985
Dmitry Shmidt807291d2015-01-27 13:40:23 -08002986void wpa_sm_set_ptk_kck_kek(struct wpa_sm *sm,
2987 const u8 *ptk_kck, size_t ptk_kck_len,
2988 const u8 *ptk_kek, size_t ptk_kek_len)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002989{
Dmitry Shmidt807291d2015-01-27 13:40:23 -08002990 if (ptk_kck && ptk_kck_len <= WPA_KCK_MAX_LEN) {
2991 os_memcpy(sm->ptk.kck, ptk_kck, ptk_kck_len);
2992 sm->ptk.kck_len = ptk_kck_len;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002993 wpa_printf(MSG_DEBUG, "Updated PTK KCK");
2994 }
Dmitry Shmidt807291d2015-01-27 13:40:23 -08002995 if (ptk_kek && ptk_kek_len <= WPA_KEK_MAX_LEN) {
2996 os_memcpy(sm->ptk.kek, ptk_kek, ptk_kek_len);
2997 sm->ptk.kek_len = ptk_kek_len;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002998 wpa_printf(MSG_DEBUG, "Updated PTK KEK");
2999 }
3000 sm->ptk_set = 1;
3001}