]> git.ipfire.org Git - thirdparty/hostap.git/blame - src/rsn_supp/wpa.c
FILS: Update EAPOL-Key RX rules for FILS (AP)
[thirdparty/hostap.git] / src / rsn_supp / wpa.c
CommitLineData
6fc6879b
JM
1/*
2 * WPA Supplicant - WPA state machine and EAPOL-Key processing
98cd3d1c 3 * Copyright (c) 2003-2015, Jouni Malinen <j@w1.fi>
73ed03f3 4 * Copyright(c) 2015 Intel Deutschland GmbH
6fc6879b 5 *
0f3d578e
JM
6 * This software may be distributed under the terms of the BSD license.
7 * See README for more details.
6fc6879b
JM
8 */
9
10#include "includes.h"
11
12#include "common.h"
03da66bd
JM
13#include "crypto/aes_wrap.h"
14#include "crypto/crypto.h"
3642c431 15#include "crypto/random.h"
03da66bd
JM
16#include "common/ieee802_11_defs.h"
17#include "eapol_supp/eapol_supp_sm.h"
6fc6879b
JM
18#include "wpa.h"
19#include "eloop.h"
6fc6879b
JM
20#include "preauth.h"
21#include "pmksa_cache.h"
22#include "wpa_i.h"
23#include "wpa_ie.h"
24#include "peerkey.h"
6fc6879b
JM
25
26
73ed03f3
MS
27static const u8 null_rsc[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
28
29
6fc6879b
JM
30/**
31 * wpa_eapol_key_send - Send WPA/RSN EAPOL-Key message
32 * @sm: Pointer to WPA state machine data from wpa_sm_init()
33 * @kck: Key Confirmation Key (KCK, part of PTK)
98cd3d1c 34 * @kck_len: KCK length in octets
6fc6879b
JM
35 * @ver: Version field from Key Info
36 * @dest: Destination address for the frame
37 * @proto: Ethertype (usually ETH_P_EAPOL)
38 * @msg: EAPOL-Key message
39 * @msg_len: Length of message
40 * @key_mic: Pointer to the buffer to which the EAPOL-Key MIC is written
c93b7e18 41 * Returns: >= 0 on success, < 0 on failure
6fc6879b 42 */
c93b7e18
AA
43int wpa_eapol_key_send(struct wpa_sm *sm, const u8 *kck, size_t kck_len,
44 int ver, const u8 *dest, u16 proto,
45 u8 *msg, size_t msg_len, u8 *key_mic)
6fc6879b 46{
c93b7e18 47 int ret = -1;
5e3b5197 48 size_t mic_len = wpa_mic_len(sm->key_mgmt);
98cd3d1c 49
a8e16edc 50 if (is_zero_ether_addr(dest) && is_zero_ether_addr(sm->bssid)) {
6fc6879b
JM
51 /*
52 * Association event was not yet received; try to fetch
53 * BSSID from the driver.
54 */
55 if (wpa_sm_get_bssid(sm, sm->bssid) < 0) {
f049052b
BG
56 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
57 "WPA: Failed to read BSSID for "
58 "EAPOL-Key destination address");
6fc6879b
JM
59 } else {
60 dest = sm->bssid;
f049052b
BG
61 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
62 "WPA: Use BSSID (" MACSTR
63 ") as the destination for EAPOL-Key",
64 MAC2STR(dest));
6fc6879b
JM
65 }
66 }
f5ff8ae6 67 if (key_mic && mic_len &&
98cd3d1c
JM
68 wpa_eapol_key_mic(kck, kck_len, sm->key_mgmt, ver, msg, msg_len,
69 key_mic)) {
f049052b 70 wpa_msg(sm->ctx->msg_ctx, MSG_ERROR,
929a2ea5
JM
71 "WPA: Failed to generate EAPOL-Key version %d key_mgmt 0x%x MIC",
72 ver, sm->key_mgmt);
04b6b3ed
JM
73 goto out;
74 }
98cd3d1c
JM
75 wpa_hexdump_key(MSG_DEBUG, "WPA: KCK", kck, kck_len);
76 wpa_hexdump(MSG_DEBUG, "WPA: Derived Key MIC", key_mic, mic_len);
6fc6879b 77 wpa_hexdump(MSG_MSGDUMP, "WPA: TX EAPOL-Key", msg, msg_len);
c93b7e18 78 ret = wpa_sm_ether_send(sm, dest, proto, msg, msg_len);
6fc6879b 79 eapol_sm_notify_tx_eapol_key(sm->eapol);
04b6b3ed 80out:
6fc6879b 81 os_free(msg);
c93b7e18 82 return ret;
6fc6879b
JM
83}
84
85
86/**
87 * wpa_sm_key_request - Send EAPOL-Key Request
88 * @sm: Pointer to WPA state machine data from wpa_sm_init()
89 * @error: Indicate whether this is an Michael MIC error report
90 * @pairwise: 1 = error report for pairwise packet, 0 = for group packet
6fc6879b
JM
91 *
92 * Send an EAPOL-Key Request to the current authenticator. This function is
93 * used to request rekeying and it is usually called when a local Michael MIC
94 * failure is detected.
95 */
96void wpa_sm_key_request(struct wpa_sm *sm, int error, int pairwise)
97{
5e3b5197 98 size_t mic_len, hdrlen, rlen;
6fc6879b
JM
99 struct wpa_eapol_key *reply;
100 int key_info, ver;
6d014ffc 101 u8 bssid[ETH_ALEN], *rbuf, *key_mic, *mic;
6fc6879b 102
929a2ea5
JM
103 if (sm->key_mgmt == WPA_KEY_MGMT_OSEN ||
104 wpa_key_mgmt_suite_b(sm->key_mgmt))
df0f01d9
JM
105 ver = WPA_KEY_INFO_TYPE_AKM_DEFINED;
106 else if (wpa_key_mgmt_ft(sm->key_mgmt) ||
107 wpa_key_mgmt_sha256(sm->key_mgmt))
6fc6879b 108 ver = WPA_KEY_INFO_TYPE_AES_128_CMAC;
eb7719ff 109 else if (sm->pairwise_cipher != WPA_CIPHER_TKIP)
6fc6879b
JM
110 ver = WPA_KEY_INFO_TYPE_HMAC_SHA1_AES;
111 else
112 ver = WPA_KEY_INFO_TYPE_HMAC_MD5_RC4;
113
114 if (wpa_sm_get_bssid(sm, bssid) < 0) {
f049052b
BG
115 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
116 "Failed to read BSSID for EAPOL-Key request");
6fc6879b
JM
117 return;
118 }
119
5e3b5197 120 mic_len = wpa_mic_len(sm->key_mgmt);
6d014ffc 121 hdrlen = sizeof(*reply) + mic_len + 2;
6fc6879b 122 rbuf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_KEY, NULL,
5e3b5197 123 hdrlen, &rlen, (void *) &reply);
6fc6879b
JM
124 if (rbuf == NULL)
125 return;
126
a14896e8
JM
127 reply->type = (sm->proto == WPA_PROTO_RSN ||
128 sm->proto == WPA_PROTO_OSEN) ?
6fc6879b
JM
129 EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA;
130 key_info = WPA_KEY_INFO_REQUEST | ver;
131 if (sm->ptk_set)
4a26ccda
JM
132 key_info |= WPA_KEY_INFO_SECURE;
133 if (sm->ptk_set && mic_len)
134 key_info |= WPA_KEY_INFO_MIC;
6fc6879b
JM
135 if (error)
136 key_info |= WPA_KEY_INFO_ERROR;
137 if (pairwise)
138 key_info |= WPA_KEY_INFO_KEY_TYPE;
139 WPA_PUT_BE16(reply->key_info, key_info);
140 WPA_PUT_BE16(reply->key_length, 0);
141 os_memcpy(reply->replay_counter, sm->request_counter,
142 WPA_REPLAY_COUNTER_LEN);
143 inc_byte_array(sm->request_counter, WPA_REPLAY_COUNTER_LEN);
144
6d014ffc
JM
145 mic = (u8 *) (reply + 1);
146 WPA_PUT_BE16(mic + mic_len, 0);
5e3b5197
JM
147 if (!(key_info & WPA_KEY_INFO_MIC))
148 key_mic = NULL;
149 else
6d014ffc 150 key_mic = mic;
6fc6879b 151
f049052b
BG
152 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
153 "WPA: Sending EAPOL-Key Request (error=%d "
154 "pairwise=%d ptk_set=%d len=%lu)",
155 error, pairwise, sm->ptk_set, (unsigned long) rlen);
98cd3d1c 156 wpa_eapol_key_send(sm, sm->ptk.kck, sm->ptk.kck_len, ver, bssid,
5e3b5197 157 ETH_P_EAPOL, rbuf, rlen, key_mic);
6fc6879b
JM
158}
159
160
b41f2684
CL
161static void wpa_supplicant_key_mgmt_set_pmk(struct wpa_sm *sm)
162{
163#ifdef CONFIG_IEEE80211R
164 if (sm->key_mgmt == WPA_KEY_MGMT_FT_IEEE8021X) {
165 if (wpa_sm_key_mgmt_set_pmk(sm, sm->xxkey, sm->xxkey_len))
166 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
167 "RSN: Cannot set low order 256 bits of MSK for key management offload");
168 } else {
169#endif /* CONFIG_IEEE80211R */
170 if (wpa_sm_key_mgmt_set_pmk(sm, sm->pmk, sm->pmk_len))
171 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
172 "RSN: Cannot set PMK for key management offload");
173#ifdef CONFIG_IEEE80211R
174 }
175#endif /* CONFIG_IEEE80211R */
176}
177
178
6fc6879b
JM
179static int wpa_supplicant_get_pmk(struct wpa_sm *sm,
180 const unsigned char *src_addr,
181 const u8 *pmkid)
182{
183 int abort_cached = 0;
184
185 if (pmkid && !sm->cur_pmksa) {
186 /* When using drivers that generate RSN IE, wpa_supplicant may
187 * not have enough time to get the association information
188 * event before receiving this 1/4 message, so try to find a
189 * matching PMKSA cache entry here. */
96efeeb6
JM
190 sm->cur_pmksa = pmksa_cache_get(sm->pmksa, src_addr, pmkid,
191 NULL);
6fc6879b 192 if (sm->cur_pmksa) {
f049052b
BG
193 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
194 "RSN: found matching PMKID from PMKSA cache");
6fc6879b 195 } else {
f049052b
BG
196 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
197 "RSN: no matching PMKID found");
6fc6879b
JM
198 abort_cached = 1;
199 }
200 }
201
202 if (pmkid && sm->cur_pmksa &&
0d15b69f 203 os_memcmp_const(pmkid, sm->cur_pmksa->pmkid, PMKID_LEN) == 0) {
6fc6879b
JM
204 wpa_hexdump(MSG_DEBUG, "RSN: matched PMKID", pmkid, PMKID_LEN);
205 wpa_sm_set_pmk_from_pmksa(sm);
206 wpa_hexdump_key(MSG_DEBUG, "RSN: PMK from PMKSA cache",
207 sm->pmk, sm->pmk_len);
208 eapol_sm_notify_cached(sm->eapol);
209#ifdef CONFIG_IEEE80211R
210 sm->xxkey_len = 0;
211#endif /* CONFIG_IEEE80211R */
56586197 212 } else if (wpa_key_mgmt_wpa_ieee8021x(sm->key_mgmt) && sm->eapol) {
6fc6879b 213 int res, pmk_len;
207976f0
JM
214
215 if (sm->key_mgmt & WPA_KEY_MGMT_IEEE8021X_SUITE_B_192)
216 pmk_len = PMK_LEN_SUITE_B_192;
217 else
218 pmk_len = PMK_LEN;
219 res = eapol_sm_get_key(sm->eapol, sm->pmk, pmk_len);
6fc6879b 220 if (res) {
207976f0
JM
221 if (pmk_len == PMK_LEN) {
222 /*
223 * EAP-LEAP is an exception from other EAP
224 * methods: it uses only 16-byte PMK.
225 */
226 res = eapol_sm_get_key(sm->eapol, sm->pmk, 16);
227 pmk_len = 16;
228 }
6fc6879b
JM
229 } else {
230#ifdef CONFIG_IEEE80211R
231 u8 buf[2 * PMK_LEN];
232 if (eapol_sm_get_key(sm->eapol, buf, 2 * PMK_LEN) == 0)
233 {
234 os_memcpy(sm->xxkey, buf + PMK_LEN, PMK_LEN);
235 sm->xxkey_len = PMK_LEN;
236 os_memset(buf, 0, sizeof(buf));
237 }
238#endif /* CONFIG_IEEE80211R */
239 }
240 if (res == 0) {
a7f10d65 241 struct rsn_pmksa_cache_entry *sa = NULL;
6fc6879b
JM
242 wpa_hexdump_key(MSG_DEBUG, "WPA: PMK from EAPOL state "
243 "machines", sm->pmk, pmk_len);
244 sm->pmk_len = pmk_len;
b41f2684 245 wpa_supplicant_key_mgmt_set_pmk(sm);
715ed737 246 if (sm->proto == WPA_PROTO_RSN &&
087a1f4e 247 !wpa_key_mgmt_suite_b(sm->key_mgmt) &&
715ed737 248 !wpa_key_mgmt_ft(sm->key_mgmt)) {
a7f10d65 249 sa = pmksa_cache_add(sm->pmksa,
70c93963 250 sm->pmk, pmk_len, NULL,
087a1f4e 251 NULL, 0,
a7f10d65
JM
252 src_addr, sm->own_addr,
253 sm->network_ctx,
254 sm->key_mgmt);
f5a51b58 255 }
6fc6879b 256 if (!sm->cur_pmksa && pmkid &&
96efeeb6
JM
257 pmksa_cache_get(sm->pmksa, src_addr, pmkid, NULL))
258 {
f049052b
BG
259 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
260 "RSN: the new PMK matches with the "
261 "PMKID");
6fc6879b 262 abort_cached = 0;
bddc51e8
JM
263 } else if (sa && !sm->cur_pmksa && pmkid) {
264 /*
265 * It looks like the authentication server
266 * derived mismatching MSK. This should not
267 * really happen, but bugs happen.. There is not
268 * much we can do here without knowing what
269 * exactly caused the server to misbehave.
270 */
62417667 271 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
bddc51e8
JM
272 "RSN: PMKID mismatch - authentication server may have derived different MSK?!");
273 return -1;
6fc6879b 274 }
a7f10d65
JM
275
276 if (!sm->cur_pmksa)
277 sm->cur_pmksa = sa;
6fc6879b 278 } else {
0f057fb2 279 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
6fc6879b 280 "WPA: Failed to get master session key from "
f049052b
BG
281 "EAPOL state machines - key handshake "
282 "aborted");
6fc6879b 283 if (sm->cur_pmksa) {
f049052b
BG
284 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
285 "RSN: Cancelled PMKSA caching "
286 "attempt");
6fc6879b
JM
287 sm->cur_pmksa = NULL;
288 abort_cached = 1;
1ac2d4a9 289 } else if (!abort_cached) {
6fc6879b
JM
290 return -1;
291 }
292 }
293 }
294
715ed737 295 if (abort_cached && wpa_key_mgmt_wpa_ieee8021x(sm->key_mgmt) &&
087a1f4e 296 !wpa_key_mgmt_suite_b(sm->key_mgmt) &&
a14896e8
JM
297 !wpa_key_mgmt_ft(sm->key_mgmt) && sm->key_mgmt != WPA_KEY_MGMT_OSEN)
298 {
6fc6879b
JM
299 /* Send EAPOL-Start to trigger full EAP authentication. */
300 u8 *buf;
301 size_t buflen;
302
f049052b
BG
303 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
304 "RSN: no PMKSA entry found - trigger "
305 "full EAP authentication");
6fc6879b
JM
306 buf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_START,
307 NULL, 0, &buflen, NULL);
308 if (buf) {
309 wpa_sm_ether_send(sm, sm->bssid, ETH_P_EAPOL,
310 buf, buflen);
311 os_free(buf);
b4a1256d 312 return -2;
6fc6879b
JM
313 }
314
315 return -1;
316 }
317
318 return 0;
319}
320
321
322/**
323 * wpa_supplicant_send_2_of_4 - Send message 2 of WPA/RSN 4-Way Handshake
324 * @sm: Pointer to WPA state machine data from wpa_sm_init()
325 * @dst: Destination address for the frame
326 * @key: Pointer to the EAPOL-Key frame header
327 * @ver: Version bits from EAPOL-Key Key Info
328 * @nonce: Nonce value for the EAPOL-Key frame
329 * @wpa_ie: WPA/RSN IE
330 * @wpa_ie_len: Length of the WPA/RSN IE
331 * @ptk: PTK to use for keyed hash and encryption
c93b7e18 332 * Returns: >= 0 on success, < 0 on failure
6fc6879b
JM
333 */
334int wpa_supplicant_send_2_of_4(struct wpa_sm *sm, const unsigned char *dst,
335 const struct wpa_eapol_key *key,
336 int ver, const u8 *nonce,
337 const u8 *wpa_ie, size_t wpa_ie_len,
338 struct wpa_ptk *ptk)
339{
5e3b5197 340 size_t mic_len, hdrlen, rlen;
6fc6879b 341 struct wpa_eapol_key *reply;
5e3b5197 342 u8 *rbuf, *key_mic;
26e23750 343 u8 *rsn_ie_buf = NULL;
4a26ccda 344 u16 key_info;
6fc6879b
JM
345
346 if (wpa_ie == NULL) {
f049052b
BG
347 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, "WPA: No wpa_ie set - "
348 "cannot generate msg 2/4");
6fc6879b
JM
349 return -1;
350 }
351
26e23750
JM
352#ifdef CONFIG_IEEE80211R
353 if (wpa_key_mgmt_ft(sm->key_mgmt)) {
354 int res;
355
55046414
JM
356 /*
357 * Add PMKR1Name into RSN IE (PMKID-List) and add MDIE and
358 * FTIE from (Re)Association Response.
359 */
360 rsn_ie_buf = os_malloc(wpa_ie_len + 2 + 2 + PMKID_LEN +
361 sm->assoc_resp_ies_len);
26e23750
JM
362 if (rsn_ie_buf == NULL)
363 return -1;
364 os_memcpy(rsn_ie_buf, wpa_ie, wpa_ie_len);
59e78c24 365 res = wpa_insert_pmkid(rsn_ie_buf, &wpa_ie_len,
26e23750
JM
366 sm->pmk_r1_name);
367 if (res < 0) {
368 os_free(rsn_ie_buf);
369 return -1;
370 }
55046414
JM
371
372 if (sm->assoc_resp_ies) {
373 os_memcpy(rsn_ie_buf + wpa_ie_len, sm->assoc_resp_ies,
374 sm->assoc_resp_ies_len);
375 wpa_ie_len += sm->assoc_resp_ies_len;
376 }
26e23750
JM
377
378 wpa_ie = rsn_ie_buf;
26e23750
JM
379 }
380#endif /* CONFIG_IEEE80211R */
381
6fc6879b
JM
382 wpa_hexdump(MSG_DEBUG, "WPA: WPA IE for msg 2/4", wpa_ie, wpa_ie_len);
383
5e3b5197 384 mic_len = wpa_mic_len(sm->key_mgmt);
6d014ffc 385 hdrlen = sizeof(*reply) + mic_len + 2;
6fc6879b 386 rbuf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_KEY,
5e3b5197 387 NULL, hdrlen + wpa_ie_len,
6fc6879b 388 &rlen, (void *) &reply);
26e23750
JM
389 if (rbuf == NULL) {
390 os_free(rsn_ie_buf);
6fc6879b 391 return -1;
26e23750 392 }
6fc6879b 393
a14896e8
JM
394 reply->type = (sm->proto == WPA_PROTO_RSN ||
395 sm->proto == WPA_PROTO_OSEN) ?
6fc6879b 396 EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA;
4a26ccda
JM
397 key_info = ver | WPA_KEY_INFO_KEY_TYPE;
398 if (mic_len)
399 key_info |= WPA_KEY_INFO_MIC;
400 WPA_PUT_BE16(reply->key_info, key_info);
a14896e8 401 if (sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN)
6fc6879b
JM
402 WPA_PUT_BE16(reply->key_length, 0);
403 else
404 os_memcpy(reply->key_length, key->key_length, 2);
405 os_memcpy(reply->replay_counter, key->replay_counter,
406 WPA_REPLAY_COUNTER_LEN);
bc8318ac
JM
407 wpa_hexdump(MSG_DEBUG, "WPA: Replay Counter", reply->replay_counter,
408 WPA_REPLAY_COUNTER_LEN);
6fc6879b 409
6d014ffc
JM
410 key_mic = (u8 *) (reply + 1);
411 WPA_PUT_BE16(key_mic + mic_len, wpa_ie_len); /* Key Data Length */
412 os_memcpy(key_mic + mic_len + 2, wpa_ie, wpa_ie_len); /* Key Data */
26e23750 413 os_free(rsn_ie_buf);
6fc6879b
JM
414
415 os_memcpy(reply->key_nonce, nonce, WPA_NONCE_LEN);
416
f049052b 417 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Sending EAPOL-Key 2/4");
c93b7e18
AA
418 return wpa_eapol_key_send(sm, ptk->kck, ptk->kck_len, ver, dst,
419 ETH_P_EAPOL, rbuf, rlen, key_mic);
6fc6879b
JM
420}
421
422
423static int wpa_derive_ptk(struct wpa_sm *sm, const unsigned char *src_addr,
98cd3d1c 424 const struct wpa_eapol_key *key, struct wpa_ptk *ptk)
6fc6879b
JM
425{
426#ifdef CONFIG_IEEE80211R
56586197 427 if (wpa_key_mgmt_ft(sm->key_mgmt))
98cd3d1c 428 return wpa_derive_ptk_ft(sm, src_addr, key, ptk);
6fc6879b
JM
429#endif /* CONFIG_IEEE80211R */
430
98cd3d1c
JM
431 return wpa_pmk_to_ptk(sm->pmk, sm->pmk_len, "Pairwise key expansion",
432 sm->own_addr, sm->bssid, sm->snonce,
433 key->key_nonce, ptk, sm->key_mgmt,
434 sm->pairwise_cipher);
6fc6879b
JM
435}
436
437
438static void wpa_supplicant_process_1_of_4(struct wpa_sm *sm,
439 const unsigned char *src_addr,
440 const struct wpa_eapol_key *key,
e6270129
JM
441 u16 ver, const u8 *key_data,
442 size_t key_data_len)
6fc6879b
JM
443{
444 struct wpa_eapol_ie_parse ie;
445 struct wpa_ptk *ptk;
b4a1256d 446 int res;
25ef8529
JM
447 u8 *kde, *kde_buf = NULL;
448 size_t kde_len;
6fc6879b
JM
449
450 if (wpa_sm_get_network_ctx(sm) == NULL) {
f049052b
BG
451 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, "WPA: No SSID info "
452 "found (msg 1 of 4)");
6fc6879b
JM
453 return;
454 }
455
456 wpa_sm_set_state(sm, WPA_4WAY_HANDSHAKE);
f049052b
BG
457 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: RX message 1 of 4-Way "
458 "Handshake from " MACSTR " (ver=%d)", MAC2STR(src_addr), ver);
6fc6879b
JM
459
460 os_memset(&ie, 0, sizeof(ie));
461
a14896e8 462 if (sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN) {
6fc6879b 463 /* RSN: msg 1/4 should contain PMKID for the selected PMK */
e6270129
JM
464 wpa_hexdump(MSG_DEBUG, "RSN: msg 1/4 key data",
465 key_data, key_data_len);
466 if (wpa_supplicant_parse_ies(key_data, key_data_len, &ie) < 0)
d627a939 467 goto failed;
6fc6879b
JM
468 if (ie.pmkid) {
469 wpa_hexdump(MSG_DEBUG, "RSN: PMKID from "
470 "Authenticator", ie.pmkid, PMKID_LEN);
471 }
472 }
6fc6879b 473
b4a1256d
JM
474 res = wpa_supplicant_get_pmk(sm, src_addr, ie.pmkid);
475 if (res == -2) {
f049052b
BG
476 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "RSN: Do not reply to "
477 "msg 1/4 - requesting full EAP authentication");
b4a1256d
JM
478 return;
479 }
480 if (res)
83935317 481 goto failed;
6fc6879b
JM
482
483 if (sm->renew_snonce) {
3642c431 484 if (random_get_bytes(sm->snonce, WPA_NONCE_LEN)) {
0f057fb2 485 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
6fc6879b 486 "WPA: Failed to get random data for SNonce");
83935317 487 goto failed;
6fc6879b
JM
488 }
489 sm->renew_snonce = 0;
490 wpa_hexdump(MSG_DEBUG, "WPA: Renewed SNonce",
491 sm->snonce, WPA_NONCE_LEN);
492 }
493
494 /* Calculate PTK which will be stored as a temporary PTK until it has
495 * been verified when processing message 3/4. */
496 ptk = &sm->tptk;
497 wpa_derive_ptk(sm, src_addr, key, ptk);
3b9c5176 498 if (sm->pairwise_cipher == WPA_CIPHER_TKIP) {
d2c33b91 499 u8 buf[8];
3b9c5176 500 /* Supplicant: swap tx/rx Mic keys */
98cd3d1c
JM
501 os_memcpy(buf, &ptk->tk[16], 8);
502 os_memcpy(&ptk->tk[16], &ptk->tk[24], 8);
503 os_memcpy(&ptk->tk[24], buf, 8);
d2c33b91 504 os_memset(buf, 0, sizeof(buf));
3b9c5176 505 }
6fc6879b 506 sm->tptk_set = 1;
ad00d64e 507 sm->tk_to_set = 1;
6fc6879b 508
25ef8529
JM
509 kde = sm->assoc_wpa_ie;
510 kde_len = sm->assoc_wpa_ie_len;
511
512#ifdef CONFIG_P2P
513 if (sm->p2p) {
514 kde_buf = os_malloc(kde_len + 2 + RSN_SELECTOR_LEN + 1);
515 if (kde_buf) {
516 u8 *pos;
517 wpa_printf(MSG_DEBUG, "P2P: Add IP Address Request KDE "
518 "into EAPOL-Key 2/4");
519 os_memcpy(kde_buf, kde, kde_len);
520 kde = kde_buf;
521 pos = kde + kde_len;
522 *pos++ = WLAN_EID_VENDOR_SPECIFIC;
523 *pos++ = RSN_SELECTOR_LEN + 1;
524 RSN_SELECTOR_PUT(pos, WFA_KEY_DATA_IP_ADDR_REQ);
525 pos += RSN_SELECTOR_LEN;
526 *pos++ = 0x01;
527 kde_len = pos - kde;
528 }
529 }
530#endif /* CONFIG_P2P */
531
6fc6879b 532 if (wpa_supplicant_send_2_of_4(sm, sm->bssid, key, ver, sm->snonce,
c93b7e18 533 kde, kde_len, ptk) < 0)
83935317 534 goto failed;
6fc6879b 535
25ef8529 536 os_free(kde_buf);
6fc6879b 537 os_memcpy(sm->anonce, key->key_nonce, WPA_NONCE_LEN);
83935317
JM
538 return;
539
540failed:
25ef8529 541 os_free(kde_buf);
83935317 542 wpa_sm_deauthenticate(sm, WLAN_REASON_UNSPECIFIED);
6fc6879b
JM
543}
544
545
546static void wpa_sm_start_preauth(void *eloop_ctx, void *timeout_ctx)
547{
548 struct wpa_sm *sm = eloop_ctx;
549 rsn_preauth_candidate_process(sm);
550}
551
552
553static void wpa_supplicant_key_neg_complete(struct wpa_sm *sm,
554 const u8 *addr, int secure)
555{
0f057fb2
JM
556 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
557 "WPA: Key negotiation completed with "
6fc6879b
JM
558 MACSTR " [PTK=%s GTK=%s]", MAC2STR(addr),
559 wpa_cipher_txt(sm->pairwise_cipher),
560 wpa_cipher_txt(sm->group_cipher));
6fc6879b
JM
561 wpa_sm_cancel_auth_timeout(sm);
562 wpa_sm_set_state(sm, WPA_COMPLETED);
563
564 if (secure) {
565 wpa_sm_mlme_setprotection(
566 sm, addr, MLME_SETPROTECTION_PROTECT_TYPE_RX_TX,
567 MLME_SETPROTECTION_KEY_TYPE_PAIRWISE);
568 eapol_sm_notify_portValid(sm->eapol, TRUE);
56586197 569 if (wpa_key_mgmt_wpa_psk(sm->key_mgmt))
6fc6879b
JM
570 eapol_sm_notify_eap_success(sm->eapol, TRUE);
571 /*
572 * Start preauthentication after a short wait to avoid a
573 * possible race condition between the data receive and key
574 * configuration after the 4-Way Handshake. This increases the
ffbf1eaa 575 * likelihood of the first preauth EAPOL-Start frame getting to
6fc6879b
JM
576 * the target AP.
577 */
578 eloop_register_timeout(1, 0, wpa_sm_start_preauth, sm, NULL);
579 }
580
581 if (sm->cur_pmksa && sm->cur_pmksa->opportunistic) {
f049052b
BG
582 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
583 "RSN: Authenticator accepted "
584 "opportunistic PMKSA entry - marking it valid");
6fc6879b
JM
585 sm->cur_pmksa->opportunistic = 0;
586 }
587
588#ifdef CONFIG_IEEE80211R
56586197 589 if (wpa_key_mgmt_ft(sm->key_mgmt)) {
6fc6879b 590 /* Prepare for the next transition */
76b7981d 591 wpa_ft_prepare_auth_request(sm, NULL);
6fc6879b
JM
592 }
593#endif /* CONFIG_IEEE80211R */
594}
595
596
581a8cde
JM
597static void wpa_sm_rekey_ptk(void *eloop_ctx, void *timeout_ctx)
598{
599 struct wpa_sm *sm = eloop_ctx;
f049052b 600 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Request PTK rekeying");
581a8cde
JM
601 wpa_sm_key_request(sm, 0, 1);
602}
603
604
6fc6879b
JM
605static int wpa_supplicant_install_ptk(struct wpa_sm *sm,
606 const struct wpa_eapol_key *key)
607{
608 int keylen, rsclen;
71934751 609 enum wpa_alg alg;
6fc6879b 610 const u8 *key_rsc;
6fc6879b 611
ad00d64e
JM
612 if (!sm->tk_to_set) {
613 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
614 "WPA: Do not re-install same PTK to the driver");
615 return 0;
616 }
617
f049052b
BG
618 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
619 "WPA: Installing PTK to the driver");
6fc6879b 620
c3550295 621 if (sm->pairwise_cipher == WPA_CIPHER_NONE) {
f049052b
BG
622 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Pairwise Cipher "
623 "Suite: NONE - do not use pairwise keys");
6fc6879b 624 return 0;
c3550295
JM
625 }
626
627 if (!wpa_cipher_valid_pairwise(sm->pairwise_cipher)) {
f049052b
BG
628 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
629 "WPA: Unsupported pairwise cipher %d",
630 sm->pairwise_cipher);
6fc6879b
JM
631 return -1;
632 }
633
c3550295
JM
634 alg = wpa_cipher_to_alg(sm->pairwise_cipher);
635 keylen = wpa_cipher_key_len(sm->pairwise_cipher);
636 rsclen = wpa_cipher_rsc_len(sm->pairwise_cipher);
637
a14896e8 638 if (sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN) {
6fc6879b
JM
639 key_rsc = null_rsc;
640 } else {
641 key_rsc = key->key_rsc;
642 wpa_hexdump(MSG_DEBUG, "WPA: RSC", key_rsc, rsclen);
643 }
644
645 if (wpa_sm_set_key(sm, alg, sm->bssid, 0, 1, key_rsc, rsclen,
98cd3d1c 646 sm->ptk.tk, keylen) < 0) {
f049052b
BG
647 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
648 "WPA: Failed to set PTK to the "
649 "driver (alg=%d keylen=%d bssid=" MACSTR ")",
650 alg, keylen, MAC2STR(sm->bssid));
6fc6879b
JM
651 return -1;
652 }
581a8cde 653
7d711541 654 /* TK is not needed anymore in supplicant */
98cd3d1c 655 os_memset(sm->ptk.tk, 0, WPA_TK_MAX_LEN);
ad00d64e 656 sm->tk_to_set = 0;
7d711541 657
581a8cde
JM
658 if (sm->wpa_ptk_rekey) {
659 eloop_cancel_timeout(wpa_sm_rekey_ptk, sm, NULL);
660 eloop_register_timeout(sm->wpa_ptk_rekey, 0, wpa_sm_rekey_ptk,
661 sm, NULL);
662 }
663
6fc6879b
JM
664 return 0;
665}
666
667
f049052b
BG
668static int wpa_supplicant_check_group_cipher(struct wpa_sm *sm,
669 int group_cipher,
6fc6879b 670 int keylen, int maxkeylen,
71934751
JM
671 int *key_rsc_len,
672 enum wpa_alg *alg)
6fc6879b 673{
c3550295 674 int klen;
6fc6879b 675
c3550295
JM
676 *alg = wpa_cipher_to_alg(group_cipher);
677 if (*alg == WPA_ALG_NONE) {
f049052b
BG
678 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
679 "WPA: Unsupported Group Cipher %d",
680 group_cipher);
6fc6879b
JM
681 return -1;
682 }
c3550295 683 *key_rsc_len = wpa_cipher_rsc_len(group_cipher);
6fc6879b 684
c3550295
JM
685 klen = wpa_cipher_key_len(group_cipher);
686 if (keylen != klen || maxkeylen < klen) {
f049052b
BG
687 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
688 "WPA: Unsupported %s Group Cipher key length %d (%d)",
689 wpa_cipher_txt(group_cipher), keylen, maxkeylen);
c3550295 690 return -1;
6fc6879b 691 }
c3550295 692 return 0;
6fc6879b
JM
693}
694
695
696struct wpa_gtk_data {
71934751 697 enum wpa_alg alg;
6fc6879b
JM
698 int tx, key_rsc_len, keyidx;
699 u8 gtk[32];
700 int gtk_len;
701};
702
703
704static int wpa_supplicant_install_gtk(struct wpa_sm *sm,
705 const struct wpa_gtk_data *gd,
706 const u8 *key_rsc)
707{
708 const u8 *_gtk = gd->gtk;
709 u8 gtk_buf[32];
710
711 wpa_hexdump_key(MSG_DEBUG, "WPA: Group Key", gd->gtk, gd->gtk_len);
f049052b
BG
712 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
713 "WPA: Installing GTK to the driver (keyidx=%d tx=%d len=%d)",
714 gd->keyidx, gd->tx, gd->gtk_len);
6fc6879b
JM
715 wpa_hexdump(MSG_DEBUG, "WPA: RSC", key_rsc, gd->key_rsc_len);
716 if (sm->group_cipher == WPA_CIPHER_TKIP) {
717 /* Swap Tx/Rx keys for Michael MIC */
718 os_memcpy(gtk_buf, gd->gtk, 16);
719 os_memcpy(gtk_buf + 16, gd->gtk + 24, 8);
720 os_memcpy(gtk_buf + 24, gd->gtk + 16, 8);
721 _gtk = gtk_buf;
722 }
723 if (sm->pairwise_cipher == WPA_CIPHER_NONE) {
0382097e 724 if (wpa_sm_set_key(sm, gd->alg, NULL,
6fc6879b
JM
725 gd->keyidx, 1, key_rsc, gd->key_rsc_len,
726 _gtk, gd->gtk_len) < 0) {
f049052b
BG
727 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
728 "WPA: Failed to set GTK to the driver "
729 "(Group only)");
d2c33b91 730 os_memset(gtk_buf, 0, sizeof(gtk_buf));
6fc6879b
JM
731 return -1;
732 }
0382097e 733 } else if (wpa_sm_set_key(sm, gd->alg, broadcast_ether_addr,
6fc6879b
JM
734 gd->keyidx, gd->tx, key_rsc, gd->key_rsc_len,
735 _gtk, gd->gtk_len) < 0) {
f049052b
BG
736 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
737 "WPA: Failed to set GTK to "
738 "the driver (alg=%d keylen=%d keyidx=%d)",
739 gd->alg, gd->gtk_len, gd->keyidx);
d2c33b91 740 os_memset(gtk_buf, 0, sizeof(gtk_buf));
6fc6879b
JM
741 return -1;
742 }
d2c33b91 743 os_memset(gtk_buf, 0, sizeof(gtk_buf));
6fc6879b
JM
744
745 return 0;
746}
747
748
749static int wpa_supplicant_gtk_tx_bit_workaround(const struct wpa_sm *sm,
750 int tx)
751{
752 if (tx && sm->pairwise_cipher != WPA_CIPHER_NONE) {
753 /* Ignore Tx bit for GTK if a pairwise key is used. One AP
754 * seemed to set this bit (incorrectly, since Tx is only when
755 * doing Group Key only APs) and without this workaround, the
756 * data connection does not work because wpa_supplicant
757 * configured non-zero keyidx to be used for unicast. */
f049052b
BG
758 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
759 "WPA: Tx bit set for GTK, but pairwise "
760 "keys are used - ignore Tx bit");
6fc6879b
JM
761 return 0;
762 }
763 return tx;
764}
765
766
73ed03f3
MS
767static int wpa_supplicant_rsc_relaxation(const struct wpa_sm *sm,
768 const u8 *rsc)
769{
770 int rsclen;
771
772 if (!sm->wpa_rsc_relaxation)
773 return 0;
774
775 rsclen = wpa_cipher_rsc_len(sm->group_cipher);
776
777 /*
778 * Try to detect RSC (endian) corruption issue where the AP sends
779 * the RSC bytes in EAPOL-Key message in the wrong order, both if
780 * it's actually a 6-byte field (as it should be) and if it treats
781 * it as an 8-byte field.
782 * An AP model known to have this bug is the Sapido RB-1632.
783 */
784 if (rsclen == 6 && ((rsc[5] && !rsc[0]) || rsc[6] || rsc[7])) {
785 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
786 "RSC %02x%02x%02x%02x%02x%02x%02x%02x is likely bogus, using 0",
787 rsc[0], rsc[1], rsc[2], rsc[3],
788 rsc[4], rsc[5], rsc[6], rsc[7]);
789
790 return 1;
791 }
792
793 return 0;
794}
795
796
6fc6879b
JM
797static int wpa_supplicant_pairwise_gtk(struct wpa_sm *sm,
798 const struct wpa_eapol_key *key,
799 const u8 *gtk, size_t gtk_len,
800 int key_info)
801{
6fc6879b 802 struct wpa_gtk_data gd;
73ed03f3 803 const u8 *key_rsc;
6fc6879b
JM
804
805 /*
806 * IEEE Std 802.11i-2004 - 8.5.2 EAPOL-Key frames - Figure 43x
807 * GTK KDE format:
808 * KeyID[bits 0-1], Tx [bit 2], Reserved [bits 3-7]
809 * Reserved [bits 0-7]
810 * GTK
811 */
812
813 os_memset(&gd, 0, sizeof(gd));
814 wpa_hexdump_key(MSG_DEBUG, "RSN: received GTK in pairwise handshake",
815 gtk, gtk_len);
816
817 if (gtk_len < 2 || gtk_len - 2 > sizeof(gd.gtk))
818 return -1;
819
820 gd.keyidx = gtk[0] & 0x3;
821 gd.tx = wpa_supplicant_gtk_tx_bit_workaround(sm,
822 !!(gtk[0] & BIT(2)));
823 gtk += 2;
824 gtk_len -= 2;
825
826 os_memcpy(gd.gtk, gtk, gtk_len);
827 gd.gtk_len = gtk_len;
828
73ed03f3
MS
829 key_rsc = key->key_rsc;
830 if (wpa_supplicant_rsc_relaxation(sm, key->key_rsc))
831 key_rsc = null_rsc;
832
dff1e285
JM
833 if (sm->group_cipher != WPA_CIPHER_GTK_NOT_USED &&
834 (wpa_supplicant_check_group_cipher(sm, sm->group_cipher,
835 gtk_len, gtk_len,
836 &gd.key_rsc_len, &gd.alg) ||
73ed03f3 837 wpa_supplicant_install_gtk(sm, &gd, key_rsc))) {
f049052b
BG
838 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
839 "RSN: Failed to install GTK");
d2c33b91 840 os_memset(&gd, 0, sizeof(gd));
6fc6879b
JM
841 return -1;
842 }
d2c33b91 843 os_memset(&gd, 0, sizeof(gd));
6fc6879b
JM
844
845 wpa_supplicant_key_neg_complete(sm, sm->bssid,
846 key_info & WPA_KEY_INFO_SECURE);
847 return 0;
6fc6879b
JM
848}
849
850
851static int ieee80211w_set_keys(struct wpa_sm *sm,
852 struct wpa_eapol_ie_parse *ie)
853{
854#ifdef CONFIG_IEEE80211W
8dd9f9cd 855 if (!wpa_cipher_valid_mgmt_group(sm->mgmt_group_cipher))
6fc6879b
JM
856 return 0;
857
858 if (ie->igtk) {
8dd9f9cd 859 size_t len;
6fc6879b
JM
860 const struct wpa_igtk_kde *igtk;
861 u16 keyidx;
8dd9f9cd
JM
862 len = wpa_cipher_key_len(sm->mgmt_group_cipher);
863 if (ie->igtk_len != WPA_IGTK_KDE_PREFIX_LEN + len)
6fc6879b
JM
864 return -1;
865 igtk = (const struct wpa_igtk_kde *) ie->igtk;
866 keyidx = WPA_GET_LE16(igtk->keyid);
f049052b
BG
867 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: IGTK keyid %d "
868 "pn %02x%02x%02x%02x%02x%02x",
869 keyidx, MAC2STR(igtk->pn));
6fc6879b 870 wpa_hexdump_key(MSG_DEBUG, "WPA: IGTK",
8dd9f9cd 871 igtk->igtk, len);
6fc6879b 872 if (keyidx > 4095) {
f049052b
BG
873 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
874 "WPA: Invalid IGTK KeyID %d", keyidx);
6fc6879b
JM
875 return -1;
876 }
8dd9f9cd
JM
877 if (wpa_sm_set_key(sm, wpa_cipher_to_alg(sm->mgmt_group_cipher),
878 broadcast_ether_addr,
6fc6879b 879 keyidx, 0, igtk->pn, sizeof(igtk->pn),
8dd9f9cd 880 igtk->igtk, len) < 0) {
f049052b
BG
881 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
882 "WPA: Failed to configure IGTK to the driver");
6fc6879b
JM
883 return -1;
884 }
885 }
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{
0f057fb2 899 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, "WPA: %s (src=" MACSTR ")",
6fc6879b
JM
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) {
f049052b
BG
908 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
909 "WPA: No WPA IE in Beacon/ProbeResp");
6fc6879b
JM
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) {
f049052b
BG
921 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
922 "WPA: No RSN IE in Beacon/ProbeResp");
6fc6879b
JM
923 }
924 wpa_hexdump(MSG_INFO, "WPA: RSN IE in 3/4 msg",
925 rsn_ie, rsn_ie_len);
926 }
927
3da372fa 928 wpa_sm_deauthenticate(sm, WLAN_REASON_IE_IN_4WAY_DIFFERS);
6fc6879b
JM
929}
930
931
5af8187e
JM
932#ifdef CONFIG_IEEE80211R
933
934static int ft_validate_mdie(struct wpa_sm *sm,
935 const unsigned char *src_addr,
3b4f6dac
JM
936 struct wpa_eapol_ie_parse *ie,
937 const u8 *assoc_resp_mdie)
5af8187e
JM
938{
939 struct rsn_mdie *mdie;
940
5af8187e
JM
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) {
f049052b
BG
945 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "FT: MDIE in msg 3/4 did "
946 "not match with the current mobility domain");
5af8187e
JM
947 return -1;
948 }
949
3b4f6dac
JM
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)) {
f049052b 953 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "FT: MDIE mismatch");
3b4f6dac
JM
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) {
f049052b
BG
971 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
972 "FT: No FTIE in EAPOL-Key msg 3/4");
3b4f6dac
JM
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) {
f049052b 981 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "FT: FTIE mismatch");
3b4f6dac
JM
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
5af8187e
JM
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) {
f049052b
BG
1008 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "FT: No PMKR1Name in "
1009 "FT 4-way handshake message 3/4");
5af8187e
JM
1010 return -1;
1011 }
1012
0d15b69f
JM
1013 if (os_memcmp_const(rsn.pmkid, sm->pmk_r1_name, WPA_PMK_NAME_LEN) != 0)
1014 {
f049052b
BG
1015 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1016 "FT: PMKR1Name mismatch in "
1017 "FT 4-way handshake message 3/4");
5af8187e
JM
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{
3b4f6dac
JM
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;
2461724c
JM
1038 while (end - pos > 2) {
1039 if (2 + pos[1] > end - pos)
3b4f6dac
JM
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 ||
5af8187e
JM
1055 ft_validate_rsnie(sm, src_addr, ie) < 0)
1056 return -1;
1057
1058 return 0;
1059}
1060
1061#endif /* CONFIG_IEEE80211R */
1062
1063
6fc6879b
JM
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) {
f049052b
BG
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");
6fc6879b 1072 if (wpa_sm_get_beacon_ie(sm) < 0) {
f049052b
BG
1073 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1074 "WPA: Could not find AP from "
1075 "the scan results");
6fc6879b 1076 } else {
f049052b
BG
1077 wpa_msg(sm->ctx->msg_ctx, MSG_DEBUG,
1078 "WPA: Found the current AP from "
1079 "updated scan results");
6fc6879b
JM
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 &&
26e23750
JM
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))) {
6fc6879b
JM
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
5af8187e
JM
1118 if (wpa_key_mgmt_ft(sm->key_mgmt) &&
1119 wpa_supplicant_validate_ie_ft(sm, src_addr, ie) < 0)
1120 return -1;
6fc6879b
JM
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
6fc6879b 1134 * @ptk: PTK to use for keyed hash and encryption
c93b7e18 1135 * Returns: >= 0 on success, < 0 on failure
6fc6879b
JM
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,
6fc6879b
JM
1140 struct wpa_ptk *ptk)
1141{
5e3b5197 1142 size_t mic_len, hdrlen, rlen;
6fc6879b 1143 struct wpa_eapol_key *reply;
5e3b5197 1144 u8 *rbuf, *key_mic;
6fc6879b 1145
5e3b5197 1146 mic_len = wpa_mic_len(sm->key_mgmt);
6d014ffc 1147 hdrlen = sizeof(*reply) + mic_len + 2;
6fc6879b 1148 rbuf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_KEY, NULL,
5e3b5197 1149 hdrlen, &rlen, (void *) &reply);
6fc6879b
JM
1150 if (rbuf == NULL)
1151 return -1;
1152
a14896e8
JM
1153 reply->type = (sm->proto == WPA_PROTO_RSN ||
1154 sm->proto == WPA_PROTO_OSEN) ?
6fc6879b
JM
1155 EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA;
1156 key_info &= WPA_KEY_INFO_SECURE;
4a26ccda
JM
1157 key_info |= ver | WPA_KEY_INFO_KEY_TYPE;
1158 if (mic_len)
1159 key_info |= WPA_KEY_INFO_MIC;
6fc6879b 1160 WPA_PUT_BE16(reply->key_info, key_info);
a14896e8 1161 if (sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN)
6fc6879b
JM
1162 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
6d014ffc
JM
1168 key_mic = (u8 *) (reply + 1);
1169 WPA_PUT_BE16(key_mic + mic_len, 0);
6fc6879b 1170
f049052b 1171 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Sending EAPOL-Key 4/4");
c93b7e18
AA
1172 return wpa_eapol_key_send(sm, ptk->kck, ptk->kck_len, ver, dst,
1173 ETH_P_EAPOL, rbuf, rlen, key_mic);
6fc6879b
JM
1174}
1175
1176
1177static void wpa_supplicant_process_3_of_4(struct wpa_sm *sm,
1178 const struct wpa_eapol_key *key,
e6270129
JM
1179 u16 ver, const u8 *key_data,
1180 size_t key_data_len)
6fc6879b 1181{
e6270129 1182 u16 key_info, keylen;
6fc6879b
JM
1183 struct wpa_eapol_ie_parse ie;
1184
1185 wpa_sm_set_state(sm, WPA_4WAY_HANDSHAKE);
f049052b
BG
1186 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: RX message 3 of 4-Way "
1187 "Handshake from " MACSTR " (ver=%d)", MAC2STR(sm->bssid), ver);
6fc6879b
JM
1188
1189 key_info = WPA_GET_BE16(key->key_info);
1190
e6270129
JM
1191 wpa_hexdump(MSG_DEBUG, "WPA: IE KeyData", key_data, key_data_len);
1192 if (wpa_supplicant_parse_ies(key_data, key_data_len, &ie) < 0)
d627a939 1193 goto failed;
6fc6879b 1194 if (ie.gtk && !(key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
f049052b
BG
1195 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1196 "WPA: GTK IE in unencrypted key data");
83935317 1197 goto failed;
6fc6879b
JM
1198 }
1199#ifdef CONFIG_IEEE80211W
1200 if (ie.igtk && !(key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
f049052b
BG
1201 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1202 "WPA: IGTK KDE in unencrypted key data");
83935317 1203 goto failed;
6fc6879b
JM
1204 }
1205
8dd9f9cd
JM
1206 if (ie.igtk &&
1207 wpa_cipher_valid_mgmt_group(sm->mgmt_group_cipher) &&
1208 ie.igtk_len != WPA_IGTK_KDE_PREFIX_LEN +
1209 (unsigned int) wpa_cipher_key_len(sm->mgmt_group_cipher)) {
f049052b
BG
1210 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1211 "WPA: Invalid IGTK KDE length %lu",
1212 (unsigned long) ie.igtk_len);
83935317 1213 goto failed;
6fc6879b
JM
1214 }
1215#endif /* CONFIG_IEEE80211W */
1216
1217 if (wpa_supplicant_validate_ie(sm, sm->bssid, &ie) < 0)
83935317 1218 goto failed;
6fc6879b
JM
1219
1220 if (os_memcmp(sm->anonce, key->key_nonce, WPA_NONCE_LEN) != 0) {
f049052b
BG
1221 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1222 "WPA: ANonce from message 1 of 4-Way Handshake "
1223 "differs from 3 of 4-Way Handshake - drop packet (src="
1224 MACSTR ")", MAC2STR(sm->bssid));
83935317 1225 goto failed;
6fc6879b
JM
1226 }
1227
1228 keylen = WPA_GET_BE16(key->key_length);
c3550295
JM
1229 if (keylen != wpa_cipher_key_len(sm->pairwise_cipher)) {
1230 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1231 "WPA: Invalid %s key length %d (src=" MACSTR
1232 ")", wpa_cipher_txt(sm->pairwise_cipher), keylen,
1233 MAC2STR(sm->bssid));
1234 goto failed;
6fc6879b
JM
1235 }
1236
25ef8529
JM
1237#ifdef CONFIG_P2P
1238 if (ie.ip_addr_alloc) {
1239 os_memcpy(sm->p2p_ip_addr, ie.ip_addr_alloc, 3 * 4);
1240 wpa_hexdump(MSG_DEBUG, "P2P: IP address info",
1241 sm->p2p_ip_addr, sizeof(sm->p2p_ip_addr));
1242 }
1243#endif /* CONFIG_P2P */
1244
6fc6879b 1245 if (wpa_supplicant_send_4_of_4(sm, sm->bssid, key, ver, key_info,
c93b7e18 1246 &sm->ptk) < 0) {
83935317
JM
1247 goto failed;
1248 }
6fc6879b
JM
1249
1250 /* SNonce was successfully used in msg 3/4, so mark it to be renewed
1251 * for the next 4-Way Handshake. If msg 3 is received again, the old
1252 * SNonce will still be used to avoid changing PTK. */
1253 sm->renew_snonce = 1;
1254
1255 if (key_info & WPA_KEY_INFO_INSTALL) {
83935317
JM
1256 if (wpa_supplicant_install_ptk(sm, key))
1257 goto failed;
6fc6879b
JM
1258 }
1259
1260 if (key_info & WPA_KEY_INFO_SECURE) {
1261 wpa_sm_mlme_setprotection(
1262 sm, sm->bssid, MLME_SETPROTECTION_PROTECT_TYPE_RX,
1263 MLME_SETPROTECTION_KEY_TYPE_PAIRWISE);
1264 eapol_sm_notify_portValid(sm->eapol, TRUE);
1265 }
1266 wpa_sm_set_state(sm, WPA_GROUP_HANDSHAKE);
1267
dff1e285
JM
1268 if (sm->group_cipher == WPA_CIPHER_GTK_NOT_USED) {
1269 wpa_supplicant_key_neg_complete(sm, sm->bssid,
1270 key_info & WPA_KEY_INFO_SECURE);
1271 } else if (ie.gtk &&
6fc6879b
JM
1272 wpa_supplicant_pairwise_gtk(sm, key,
1273 ie.gtk, ie.gtk_len, key_info) < 0) {
f049052b
BG
1274 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1275 "RSN: Failed to configure GTK");
83935317 1276 goto failed;
6fc6879b
JM
1277 }
1278
83935317 1279 if (ieee80211w_set_keys(sm, &ie) < 0) {
f049052b
BG
1280 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1281 "RSN: Failed to configure IGTK");
83935317
JM
1282 goto failed;
1283 }
1284
392e68e8
SD
1285 if (ie.gtk)
1286 wpa_sm_set_rekey_offload(sm);
b14a210c 1287
087a1f4e
JM
1288 if (sm->proto == WPA_PROTO_RSN && wpa_key_mgmt_suite_b(sm->key_mgmt)) {
1289 struct rsn_pmksa_cache_entry *sa;
1290
70c93963 1291 sa = pmksa_cache_add(sm->pmksa, sm->pmk, sm->pmk_len, NULL,
98cd3d1c 1292 sm->ptk.kck, sm->ptk.kck_len,
087a1f4e
JM
1293 sm->bssid, sm->own_addr,
1294 sm->network_ctx, sm->key_mgmt);
1295 if (!sm->cur_pmksa)
1296 sm->cur_pmksa = sa;
1297 }
1298
761396e4 1299 sm->msg_3_of_4_ok = 1;
83935317
JM
1300 return;
1301
1302failed:
1303 wpa_sm_deauthenticate(sm, WLAN_REASON_UNSPECIFIED);
6fc6879b
JM
1304}
1305
1306
1307static int wpa_supplicant_process_1_of_2_rsn(struct wpa_sm *sm,
1308 const u8 *keydata,
1309 size_t keydatalen,
1310 u16 key_info,
1311 struct wpa_gtk_data *gd)
1312{
1313 int maxkeylen;
1314 struct wpa_eapol_ie_parse ie;
1315
1316 wpa_hexdump(MSG_DEBUG, "RSN: msg 1/2 key data", keydata, keydatalen);
d627a939
JM
1317 if (wpa_supplicant_parse_ies(keydata, keydatalen, &ie) < 0)
1318 return -1;
6fc6879b 1319 if (ie.gtk && !(key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
f049052b
BG
1320 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1321 "WPA: GTK IE in unencrypted key data");
6fc6879b
JM
1322 return -1;
1323 }
1324 if (ie.gtk == NULL) {
f049052b
BG
1325 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1326 "WPA: No GTK IE in Group Key msg 1/2");
6fc6879b
JM
1327 return -1;
1328 }
1329 maxkeylen = gd->gtk_len = ie.gtk_len - 2;
1330
f049052b 1331 if (wpa_supplicant_check_group_cipher(sm, sm->group_cipher,
6fc6879b
JM
1332 gd->gtk_len, maxkeylen,
1333 &gd->key_rsc_len, &gd->alg))
1334 return -1;
1335
7cb9bb4d
JM
1336 wpa_hexdump_key(MSG_DEBUG, "RSN: received GTK in group key handshake",
1337 ie.gtk, ie.gtk_len);
6fc6879b
JM
1338 gd->keyidx = ie.gtk[0] & 0x3;
1339 gd->tx = wpa_supplicant_gtk_tx_bit_workaround(sm,
1340 !!(ie.gtk[0] & BIT(2)));
1341 if (ie.gtk_len - 2 > sizeof(gd->gtk)) {
f049052b
BG
1342 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1343 "RSN: Too long GTK in GTK IE (len=%lu)",
1344 (unsigned long) ie.gtk_len - 2);
6fc6879b
JM
1345 return -1;
1346 }
1347 os_memcpy(gd->gtk, ie.gtk + 2, ie.gtk_len - 2);
1348
1349 if (ieee80211w_set_keys(sm, &ie) < 0)
f049052b
BG
1350 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1351 "RSN: Failed to configure IGTK");
6fc6879b
JM
1352
1353 return 0;
1354}
1355
1356
1357static int wpa_supplicant_process_1_of_2_wpa(struct wpa_sm *sm,
1358 const struct wpa_eapol_key *key,
e6270129
JM
1359 const u8 *key_data,
1360 size_t key_data_len, u16 key_info,
1361 u16 ver, struct wpa_gtk_data *gd)
6fc6879b
JM
1362{
1363 size_t maxkeylen;
c397eff8 1364 u16 gtk_len;
6fc6879b 1365
c397eff8 1366 gtk_len = WPA_GET_BE16(key->key_length);
e6270129 1367 maxkeylen = key_data_len;
6fc6879b
JM
1368 if (ver == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
1369 if (maxkeylen < 8) {
f049052b
BG
1370 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1371 "WPA: Too short maxkeylen (%lu)",
1372 (unsigned long) maxkeylen);
6fc6879b
JM
1373 return -1;
1374 }
1375 maxkeylen -= 8;
1376 }
1377
c397eff8
JM
1378 if (gtk_len > maxkeylen ||
1379 wpa_supplicant_check_group_cipher(sm, sm->group_cipher,
1380 gtk_len, maxkeylen,
6fc6879b
JM
1381 &gd->key_rsc_len, &gd->alg))
1382 return -1;
1383
c397eff8 1384 gd->gtk_len = gtk_len;
6fc6879b
JM
1385 gd->keyidx = (key_info & WPA_KEY_INFO_KEY_INDEX_MASK) >>
1386 WPA_KEY_INFO_KEY_INDEX_SHIFT;
98cd3d1c 1387 if (ver == WPA_KEY_INFO_TYPE_HMAC_MD5_RC4 && sm->ptk.kek_len == 16) {
7cb53ded
JM
1388#ifdef CONFIG_NO_RC4
1389 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1390 "WPA: RC4 not supported in the build");
1391 return -1;
1392#else /* CONFIG_NO_RC4 */
d2c33b91 1393 u8 ek[32];
e6270129 1394 if (key_data_len > sizeof(gd->gtk)) {
f049052b
BG
1395 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1396 "WPA: RC4 key data too long (%lu)",
e6270129 1397 (unsigned long) key_data_len);
6fc6879b
JM
1398 return -1;
1399 }
d2c33b91 1400 os_memcpy(ek, key->key_iv, 16);
98cd3d1c 1401 os_memcpy(ek + 16, sm->ptk.kek, sm->ptk.kek_len);
e6270129
JM
1402 os_memcpy(gd->gtk, key_data, key_data_len);
1403 if (rc4_skip(ek, 32, 256, gd->gtk, key_data_len)) {
d2c33b91 1404 os_memset(ek, 0, sizeof(ek));
f049052b
BG
1405 wpa_msg(sm->ctx->msg_ctx, MSG_ERROR,
1406 "WPA: RC4 failed");
7a215dfc
JM
1407 return -1;
1408 }
d2c33b91 1409 os_memset(ek, 0, sizeof(ek));
7cb53ded 1410#endif /* CONFIG_NO_RC4 */
6fc6879b 1411 } else if (ver == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
e6270129 1412 if (maxkeylen % 8) {
f049052b
BG
1413 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1414 "WPA: Unsupported AES-WRAP len %lu",
e6270129 1415 (unsigned long) maxkeylen);
6fc6879b
JM
1416 return -1;
1417 }
1418 if (maxkeylen > sizeof(gd->gtk)) {
f049052b
BG
1419 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1420 "WPA: AES-WRAP key data "
1421 "too long (keydatalen=%lu maxkeylen=%lu)",
e6270129 1422 (unsigned long) key_data_len,
f049052b 1423 (unsigned long) maxkeylen);
6fc6879b
JM
1424 return -1;
1425 }
98cd3d1c
JM
1426 if (aes_unwrap(sm->ptk.kek, sm->ptk.kek_len, maxkeylen / 8,
1427 key_data, gd->gtk)) {
f049052b
BG
1428 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1429 "WPA: AES unwrap failed - could not decrypt "
1430 "GTK");
6fc6879b
JM
1431 return -1;
1432 }
1433 } else {
f049052b
BG
1434 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1435 "WPA: Unsupported key_info type %d", ver);
6fc6879b
JM
1436 return -1;
1437 }
1438 gd->tx = wpa_supplicant_gtk_tx_bit_workaround(
1439 sm, !!(key_info & WPA_KEY_INFO_TXRX));
1440 return 0;
1441}
1442
1443
1444static int wpa_supplicant_send_2_of_2(struct wpa_sm *sm,
1445 const struct wpa_eapol_key *key,
1446 int ver, u16 key_info)
1447{
5e3b5197 1448 size_t mic_len, hdrlen, rlen;
6fc6879b 1449 struct wpa_eapol_key *reply;
5e3b5197 1450 u8 *rbuf, *key_mic;
6fc6879b 1451
5e3b5197 1452 mic_len = wpa_mic_len(sm->key_mgmt);
6d014ffc 1453 hdrlen = sizeof(*reply) + mic_len + 2;
6fc6879b 1454 rbuf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_KEY, NULL,
5e3b5197 1455 hdrlen, &rlen, (void *) &reply);
6fc6879b
JM
1456 if (rbuf == NULL)
1457 return -1;
1458
a14896e8
JM
1459 reply->type = (sm->proto == WPA_PROTO_RSN ||
1460 sm->proto == WPA_PROTO_OSEN) ?
6fc6879b
JM
1461 EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA;
1462 key_info &= WPA_KEY_INFO_KEY_INDEX_MASK;
4a26ccda
JM
1463 key_info |= ver | WPA_KEY_INFO_SECURE;
1464 if (mic_len)
1465 key_info |= WPA_KEY_INFO_MIC;
6fc6879b 1466 WPA_PUT_BE16(reply->key_info, key_info);
a14896e8 1467 if (sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN)
6fc6879b
JM
1468 WPA_PUT_BE16(reply->key_length, 0);
1469 else
1470 os_memcpy(reply->key_length, key->key_length, 2);
1471 os_memcpy(reply->replay_counter, key->replay_counter,
1472 WPA_REPLAY_COUNTER_LEN);
1473
6d014ffc
JM
1474 key_mic = (u8 *) (reply + 1);
1475 WPA_PUT_BE16(key_mic + mic_len, 0);
6fc6879b 1476
f049052b 1477 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Sending EAPOL-Key 2/2");
c93b7e18
AA
1478 return wpa_eapol_key_send(sm, sm->ptk.kck, sm->ptk.kck_len, ver,
1479 sm->bssid, ETH_P_EAPOL, rbuf, rlen, key_mic);
6fc6879b
JM
1480}
1481
1482
1483static void wpa_supplicant_process_1_of_2(struct wpa_sm *sm,
1484 const unsigned char *src_addr,
1485 const struct wpa_eapol_key *key,
e6270129
JM
1486 const u8 *key_data,
1487 size_t key_data_len, u16 ver)
6fc6879b 1488{
e6270129 1489 u16 key_info;
6fc6879b
JM
1490 int rekey, ret;
1491 struct wpa_gtk_data gd;
73ed03f3 1492 const u8 *key_rsc;
6fc6879b 1493
761396e4
JM
1494 if (!sm->msg_3_of_4_ok) {
1495 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1496 "WPA: Group Key Handshake started prior to completion of 4-way handshake");
1497 goto failed;
1498 }
1499
6fc6879b
JM
1500 os_memset(&gd, 0, sizeof(gd));
1501
1502 rekey = wpa_sm_get_state(sm) == WPA_COMPLETED;
f049052b
BG
1503 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: RX message 1 of Group Key "
1504 "Handshake from " MACSTR " (ver=%d)", MAC2STR(src_addr), ver);
6fc6879b
JM
1505
1506 key_info = WPA_GET_BE16(key->key_info);
6fc6879b 1507
a14896e8 1508 if (sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN) {
e6270129
JM
1509 ret = wpa_supplicant_process_1_of_2_rsn(sm, key_data,
1510 key_data_len, key_info,
6fc6879b
JM
1511 &gd);
1512 } else {
e6270129
JM
1513 ret = wpa_supplicant_process_1_of_2_wpa(sm, key, key_data,
1514 key_data_len,
1515 key_info, ver, &gd);
6fc6879b
JM
1516 }
1517
1518 wpa_sm_set_state(sm, WPA_GROUP_HANDSHAKE);
1519
1520 if (ret)
83935317 1521 goto failed;
6fc6879b 1522
73ed03f3
MS
1523 key_rsc = key->key_rsc;
1524 if (wpa_supplicant_rsc_relaxation(sm, key->key_rsc))
1525 key_rsc = null_rsc;
1526
1527 if (wpa_supplicant_install_gtk(sm, &gd, key_rsc) ||
c93b7e18 1528 wpa_supplicant_send_2_of_2(sm, key, ver, key_info) < 0)
83935317 1529 goto failed;
fbfc974c 1530 os_memset(&gd, 0, sizeof(gd));
6fc6879b
JM
1531
1532 if (rekey) {
0f057fb2 1533 wpa_msg(sm->ctx->msg_ctx, MSG_INFO, "WPA: Group rekeying "
6fc6879b
JM
1534 "completed with " MACSTR " [GTK=%s]",
1535 MAC2STR(sm->bssid), wpa_cipher_txt(sm->group_cipher));
1536 wpa_sm_cancel_auth_timeout(sm);
1537 wpa_sm_set_state(sm, WPA_COMPLETED);
1538 } else {
1539 wpa_supplicant_key_neg_complete(sm, sm->bssid,
1540 key_info &
1541 WPA_KEY_INFO_SECURE);
1542 }
392e68e8
SD
1543
1544 wpa_sm_set_rekey_offload(sm);
1545
83935317
JM
1546 return;
1547
1548failed:
fbfc974c 1549 os_memset(&gd, 0, sizeof(gd));
83935317 1550 wpa_sm_deauthenticate(sm, WLAN_REASON_UNSPECIFIED);
6fc6879b
JM
1551}
1552
1553
1554static int wpa_supplicant_verify_eapol_key_mic(struct wpa_sm *sm,
6d014ffc 1555 struct wpa_eapol_key *key,
6fc6879b
JM
1556 u16 ver,
1557 const u8 *buf, size_t len)
1558{
98cd3d1c 1559 u8 mic[WPA_EAPOL_KEY_MIC_MAX_LEN];
6fc6879b 1560 int ok = 0;
5e3b5197 1561 size_t mic_len = wpa_mic_len(sm->key_mgmt);
6fc6879b 1562
6d014ffc 1563 os_memcpy(mic, key + 1, mic_len);
6fc6879b 1564 if (sm->tptk_set) {
6d014ffc 1565 os_memset(key + 1, 0, mic_len);
98cd3d1c 1566 wpa_eapol_key_mic(sm->tptk.kck, sm->tptk.kck_len, sm->key_mgmt,
6d014ffc
JM
1567 ver, buf, len, (u8 *) (key + 1));
1568 if (os_memcmp_const(mic, key + 1, mic_len) != 0) {
f049052b
BG
1569 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1570 "WPA: Invalid EAPOL-Key MIC "
1571 "when using TPTK - ignoring TPTK");
6fc6879b
JM
1572 } else {
1573 ok = 1;
1574 sm->tptk_set = 0;
1575 sm->ptk_set = 1;
1576 os_memcpy(&sm->ptk, &sm->tptk, sizeof(sm->ptk));
d2c33b91 1577 os_memset(&sm->tptk, 0, sizeof(sm->tptk));
6fc6879b
JM
1578 }
1579 }
1580
1581 if (!ok && sm->ptk_set) {
6d014ffc 1582 os_memset(key + 1, 0, mic_len);
98cd3d1c 1583 wpa_eapol_key_mic(sm->ptk.kck, sm->ptk.kck_len, sm->key_mgmt,
6d014ffc
JM
1584 ver, buf, len, (u8 *) (key + 1));
1585 if (os_memcmp_const(mic, key + 1, mic_len) != 0) {
f049052b
BG
1586 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1587 "WPA: Invalid EAPOL-Key MIC - "
1588 "dropping packet");
6fc6879b
JM
1589 return -1;
1590 }
1591 ok = 1;
1592 }
1593
1594 if (!ok) {
f049052b
BG
1595 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1596 "WPA: Could not verify EAPOL-Key MIC - "
1597 "dropping packet");
6fc6879b
JM
1598 return -1;
1599 }
1600
1601 os_memcpy(sm->rx_replay_counter, key->replay_counter,
1602 WPA_REPLAY_COUNTER_LEN);
1603 sm->rx_replay_counter_set = 1;
1604 return 0;
1605}
1606
1607
1608/* Decrypt RSN EAPOL-Key key data (RC4 or AES-WRAP) */
1609static int wpa_supplicant_decrypt_key_data(struct wpa_sm *sm,
6d014ffc
JM
1610 struct wpa_eapol_key *key,
1611 size_t mic_len, u16 ver,
e6270129 1612 u8 *key_data, size_t *key_data_len)
6fc6879b 1613{
6fc6879b 1614 wpa_hexdump(MSG_DEBUG, "RSN: encrypted key data",
e6270129 1615 key_data, *key_data_len);
6fc6879b 1616 if (!sm->ptk_set) {
f049052b
BG
1617 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1618 "WPA: PTK not available, cannot decrypt EAPOL-Key Key "
1619 "Data");
6fc6879b
JM
1620 return -1;
1621 }
1622
1623 /* Decrypt key data here so that this operation does not need
1624 * to be implemented separately for each message type. */
98cd3d1c 1625 if (ver == WPA_KEY_INFO_TYPE_HMAC_MD5_RC4 && sm->ptk.kek_len == 16) {
7cb53ded
JM
1626#ifdef CONFIG_NO_RC4
1627 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1628 "WPA: RC4 not supported in the build");
1629 return -1;
1630#else /* CONFIG_NO_RC4 */
6fc6879b
JM
1631 u8 ek[32];
1632 os_memcpy(ek, key->key_iv, 16);
98cd3d1c 1633 os_memcpy(ek + 16, sm->ptk.kek, sm->ptk.kek_len);
e6270129 1634 if (rc4_skip(ek, 32, 256, key_data, *key_data_len)) {
d2c33b91 1635 os_memset(ek, 0, sizeof(ek));
f049052b
BG
1636 wpa_msg(sm->ctx->msg_ctx, MSG_ERROR,
1637 "WPA: RC4 failed");
7a215dfc
JM
1638 return -1;
1639 }
d2c33b91 1640 os_memset(ek, 0, sizeof(ek));
7cb53ded 1641#endif /* CONFIG_NO_RC4 */
6fc6879b 1642 } else if (ver == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES ||
df0f01d9 1643 ver == WPA_KEY_INFO_TYPE_AES_128_CMAC ||
929a2ea5
JM
1644 sm->key_mgmt == WPA_KEY_MGMT_OSEN ||
1645 wpa_key_mgmt_suite_b(sm->key_mgmt)) {
6fc6879b 1646 u8 *buf;
e6270129 1647 if (*key_data_len < 8 || *key_data_len % 8) {
f049052b 1648 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
e6270129
JM
1649 "WPA: Unsupported AES-WRAP len %u",
1650 (unsigned int) *key_data_len);
6fc6879b
JM
1651 return -1;
1652 }
e6270129
JM
1653 *key_data_len -= 8; /* AES-WRAP adds 8 bytes */
1654 buf = os_malloc(*key_data_len);
6fc6879b 1655 if (buf == NULL) {
f049052b
BG
1656 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1657 "WPA: No memory for AES-UNWRAP buffer");
6fc6879b
JM
1658 return -1;
1659 }
98cd3d1c 1660 if (aes_unwrap(sm->ptk.kek, sm->ptk.kek_len, *key_data_len / 8,
e6270129 1661 key_data, buf)) {
cd5895e8 1662 bin_clear_free(buf, *key_data_len);
f049052b
BG
1663 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1664 "WPA: AES unwrap failed - "
1665 "could not decrypt EAPOL-Key key data");
6fc6879b
JM
1666 return -1;
1667 }
e6270129 1668 os_memcpy(key_data, buf, *key_data_len);
cd5895e8 1669 bin_clear_free(buf, *key_data_len);
6d014ffc 1670 WPA_PUT_BE16(((u8 *) (key + 1)) + mic_len, *key_data_len);
6fc6879b 1671 } else {
f049052b
BG
1672 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1673 "WPA: Unsupported key_info type %d", ver);
6fc6879b
JM
1674 return -1;
1675 }
1676 wpa_hexdump_key(MSG_DEBUG, "WPA: decrypted EAPOL-Key key data",
e6270129 1677 key_data, *key_data_len);
6fc6879b
JM
1678 return 0;
1679}
1680
1681
1682/**
1683 * wpa_sm_aborted_cached - Notify WPA that PMKSA caching was aborted
1684 * @sm: Pointer to WPA state machine data from wpa_sm_init()
1685 */
1686void wpa_sm_aborted_cached(struct wpa_sm *sm)
1687{
1688 if (sm && sm->cur_pmksa) {
f049052b
BG
1689 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1690 "RSN: Cancelling PMKSA caching attempt");
6fc6879b
JM
1691 sm->cur_pmksa = NULL;
1692 }
1693}
1694
1695
f049052b 1696static void wpa_eapol_key_dump(struct wpa_sm *sm,
5e3b5197
JM
1697 const struct wpa_eapol_key *key,
1698 unsigned int key_data_len,
1699 const u8 *mic, unsigned int mic_len)
6fc6879b
JM
1700{
1701#ifndef CONFIG_NO_STDOUT_DEBUG
1702 u16 key_info = WPA_GET_BE16(key->key_info);
1703
f049052b
BG
1704 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, " EAPOL-Key type=%d", key->type);
1705 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1706 " key_info 0x%x (ver=%d keyidx=%d rsvd=%d %s%s%s%s%s%s%s%s)",
1707 key_info, key_info & WPA_KEY_INFO_TYPE_MASK,
1708 (key_info & WPA_KEY_INFO_KEY_INDEX_MASK) >>
1709 WPA_KEY_INFO_KEY_INDEX_SHIFT,
1710 (key_info & (BIT(13) | BIT(14) | BIT(15))) >> 13,
1711 key_info & WPA_KEY_INFO_KEY_TYPE ? "Pairwise" : "Group",
1712 key_info & WPA_KEY_INFO_INSTALL ? " Install" : "",
1713 key_info & WPA_KEY_INFO_ACK ? " Ack" : "",
1714 key_info & WPA_KEY_INFO_MIC ? " MIC" : "",
1715 key_info & WPA_KEY_INFO_SECURE ? " Secure" : "",
1716 key_info & WPA_KEY_INFO_ERROR ? " Error" : "",
1717 key_info & WPA_KEY_INFO_REQUEST ? " Request" : "",
1718 key_info & WPA_KEY_INFO_ENCR_KEY_DATA ? " Encr" : "");
1719 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1720 " key_length=%u key_data_length=%u",
5e3b5197 1721 WPA_GET_BE16(key->key_length), key_data_len);
6fc6879b
JM
1722 wpa_hexdump(MSG_DEBUG, " replay_counter",
1723 key->replay_counter, WPA_REPLAY_COUNTER_LEN);
1724 wpa_hexdump(MSG_DEBUG, " key_nonce", key->key_nonce, WPA_NONCE_LEN);
1725 wpa_hexdump(MSG_DEBUG, " key_iv", key->key_iv, 16);
1726 wpa_hexdump(MSG_DEBUG, " key_rsc", key->key_rsc, 8);
1727 wpa_hexdump(MSG_DEBUG, " key_id (reserved)", key->key_id, 8);
5e3b5197 1728 wpa_hexdump(MSG_DEBUG, " key_mic", mic, mic_len);
6fc6879b
JM
1729#endif /* CONFIG_NO_STDOUT_DEBUG */
1730}
1731
1732
1733/**
1734 * wpa_sm_rx_eapol - Process received WPA EAPOL frames
1735 * @sm: Pointer to WPA state machine data from wpa_sm_init()
1736 * @src_addr: Source MAC address of the EAPOL packet
1737 * @buf: Pointer to the beginning of the EAPOL data (EAPOL header)
1738 * @len: Length of the EAPOL frame
1739 * Returns: 1 = WPA EAPOL-Key processed, 0 = not a WPA EAPOL-Key, -1 failure
1740 *
1741 * This function is called for each received EAPOL frame. Other than EAPOL-Key
1742 * frames can be skipped if filtering is done elsewhere. wpa_sm_rx_eapol() is
1743 * only processing WPA and WPA2 EAPOL-Key frames.
1744 *
1745 * The received EAPOL-Key packets are validated and valid packets are replied
1746 * to. In addition, key material (PTK, GTK) is configured at the end of a
1747 * successful key handshake.
1748 */
1749int wpa_sm_rx_eapol(struct wpa_sm *sm, const u8 *src_addr,
1750 const u8 *buf, size_t len)
1751{
e6270129 1752 size_t plen, data_len, key_data_len;
d56d7e56 1753 const struct ieee802_1x_hdr *hdr;
6fc6879b
JM
1754 struct wpa_eapol_key *key;
1755 u16 key_info, ver;
d56d7e56 1756 u8 *tmp = NULL;
6fc6879b
JM
1757 int ret = -1;
1758 struct wpa_peerkey *peerkey = NULL;
6d014ffc 1759 u8 *mic, *key_data;
5e3b5197 1760 size_t mic_len, keyhdrlen;
6fc6879b
JM
1761
1762#ifdef CONFIG_IEEE80211R
1763 sm->ft_completed = 0;
1764#endif /* CONFIG_IEEE80211R */
1765
5e3b5197 1766 mic_len = wpa_mic_len(sm->key_mgmt);
6d014ffc 1767 keyhdrlen = sizeof(*key) + mic_len + 2;
5e3b5197
JM
1768
1769 if (len < sizeof(*hdr) + keyhdrlen) {
f049052b
BG
1770 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1771 "WPA: EAPOL frame too short to be a WPA "
1772 "EAPOL-Key (len %lu, expecting at least %lu)",
1773 (unsigned long) len,
5e3b5197 1774 (unsigned long) sizeof(*hdr) + keyhdrlen);
6fc6879b
JM
1775 return 0;
1776 }
1777
d56d7e56 1778 hdr = (const struct ieee802_1x_hdr *) buf;
6fc6879b
JM
1779 plen = be_to_host16(hdr->length);
1780 data_len = plen + sizeof(*hdr);
f049052b
BG
1781 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1782 "IEEE 802.1X RX: version=%d type=%d length=%lu",
1783 hdr->version, hdr->type, (unsigned long) plen);
6fc6879b
JM
1784
1785 if (hdr->version < EAPOL_VERSION) {
1786 /* TODO: backwards compatibility */
1787 }
1788 if (hdr->type != IEEE802_1X_TYPE_EAPOL_KEY) {
f049052b
BG
1789 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1790 "WPA: EAPOL frame (type %u) discarded, "
6fc6879b
JM
1791 "not a Key frame", hdr->type);
1792 ret = 0;
1793 goto out;
1794 }
d56d7e56 1795 wpa_hexdump(MSG_MSGDUMP, "WPA: RX EAPOL-Key", buf, len);
5e3b5197 1796 if (plen > len - sizeof(*hdr) || plen < keyhdrlen) {
f049052b
BG
1797 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1798 "WPA: EAPOL frame payload size %lu "
1799 "invalid (frame size %lu)",
1800 (unsigned long) plen, (unsigned long) len);
6fc6879b
JM
1801 ret = 0;
1802 goto out;
1803 }
d56d7e56
JM
1804 if (data_len < len) {
1805 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1806 "WPA: ignoring %lu bytes after the IEEE 802.1X data",
1807 (unsigned long) len - data_len);
1808 }
1809
1810 /*
1811 * Make a copy of the frame since we need to modify the buffer during
1812 * MAC validation and Key Data decryption.
1813 */
1814 tmp = os_malloc(data_len);
1815 if (tmp == NULL)
1816 goto out;
1817 os_memcpy(tmp, buf, data_len);
1818 key = (struct wpa_eapol_key *) (tmp + sizeof(struct ieee802_1x_hdr));
6d014ffc
JM
1819 mic = (u8 *) (key + 1);
1820 key_data = mic + mic_len + 2;
6fc6879b
JM
1821
1822 if (key->type != EAPOL_KEY_TYPE_WPA && key->type != EAPOL_KEY_TYPE_RSN)
1823 {
f049052b
BG
1824 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1825 "WPA: EAPOL-Key type (%d) unknown, discarded",
1826 key->type);
6fc6879b
JM
1827 ret = 0;
1828 goto out;
1829 }
6fc6879b 1830
6d014ffc
JM
1831 key_data_len = WPA_GET_BE16(mic + mic_len);
1832 wpa_eapol_key_dump(sm, key, key_data_len, mic, mic_len);
5e3b5197
JM
1833
1834 if (key_data_len > plen - keyhdrlen) {
d56d7e56
JM
1835 wpa_msg(sm->ctx->msg_ctx, MSG_INFO, "WPA: Invalid EAPOL-Key "
1836 "frame - key_data overflow (%u > %u)",
e6270129 1837 (unsigned int) key_data_len,
5e3b5197 1838 (unsigned int) (plen - keyhdrlen));
d56d7e56 1839 goto out;
6fc6879b 1840 }
d56d7e56
JM
1841
1842 eapol_sm_notify_lower_layer_success(sm->eapol, 0);
6fc6879b
JM
1843 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 &&
a20088e5 1846#if defined(CONFIG_IEEE80211R) || defined(CONFIG_IEEE80211W)
6fc6879b 1847 ver != WPA_KEY_INFO_TYPE_AES_128_CMAC &&
a20088e5 1848#endif /* CONFIG_IEEE80211R || CONFIG_IEEE80211W */
df0f01d9 1849 ver != WPA_KEY_INFO_TYPE_HMAC_SHA1_AES &&
929a2ea5 1850 !wpa_key_mgmt_suite_b(sm->key_mgmt) &&
352caf00 1851 !wpa_key_mgmt_fils(sm->key_mgmt) &&
df0f01d9 1852 sm->key_mgmt != WPA_KEY_MGMT_OSEN) {
f049052b
BG
1853 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1854 "WPA: Unsupported EAPOL-Key descriptor version %d",
1855 ver);
6fc6879b
JM
1856 goto out;
1857 }
1858
df0f01d9
JM
1859 if (sm->key_mgmt == WPA_KEY_MGMT_OSEN &&
1860 ver != WPA_KEY_INFO_TYPE_AKM_DEFINED) {
1861 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1862 "OSEN: Unsupported EAPOL-Key descriptor version %d",
1863 ver);
1864 goto out;
1865 }
1866
352caf00
JM
1867 if ((wpa_key_mgmt_suite_b(sm->key_mgmt) ||
1868 wpa_key_mgmt_fils(sm->key_mgmt)) &&
929a2ea5
JM
1869 ver != WPA_KEY_INFO_TYPE_AKM_DEFINED) {
1870 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1871 "RSN: Unsupported EAPOL-Key descriptor version %d (expected AKM defined = 0)",
1872 ver);
1873 goto out;
1874 }
1875
6fc6879b 1876#ifdef CONFIG_IEEE80211R
56586197 1877 if (wpa_key_mgmt_ft(sm->key_mgmt)) {
6fc6879b
JM
1878 /* IEEE 802.11r uses a new key_info type (AES-128-CMAC). */
1879 if (ver != WPA_KEY_INFO_TYPE_AES_128_CMAC) {
f049052b
BG
1880 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1881 "FT: AP did not use AES-128-CMAC");
6fc6879b
JM
1882 goto out;
1883 }
1884 } else
1885#endif /* CONFIG_IEEE80211R */
56586197
JM
1886#ifdef CONFIG_IEEE80211W
1887 if (wpa_key_mgmt_sha256(sm->key_mgmt)) {
df0f01d9 1888 if (ver != WPA_KEY_INFO_TYPE_AES_128_CMAC &&
929a2ea5 1889 sm->key_mgmt != WPA_KEY_MGMT_OSEN &&
352caf00 1890 !wpa_key_mgmt_fils(sm->key_mgmt) &&
929a2ea5 1891 !wpa_key_mgmt_suite_b(sm->key_mgmt)) {
f049052b
BG
1892 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1893 "WPA: AP did not use the "
1894 "negotiated AES-128-CMAC");
56586197
JM
1895 goto out;
1896 }
1897 } else
1898#endif /* CONFIG_IEEE80211W */
6fc6879b 1899 if (sm->pairwise_cipher == WPA_CIPHER_CCMP &&
929a2ea5 1900 !wpa_key_mgmt_suite_b(sm->key_mgmt) &&
352caf00 1901 !wpa_key_mgmt_fils(sm->key_mgmt) &&
6fc6879b 1902 ver != WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
f049052b
BG
1903 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1904 "WPA: CCMP is used, but EAPOL-Key "
1905 "descriptor version (%d) is not 2", ver);
6fc6879b
JM
1906 if (sm->group_cipher != WPA_CIPHER_CCMP &&
1907 !(key_info & WPA_KEY_INFO_KEY_TYPE)) {
1908 /* Earlier versions of IEEE 802.11i did not explicitly
1909 * require version 2 descriptor for all EAPOL-Key
1910 * packets, so allow group keys to use version 1 if
1911 * CCMP is not used for them. */
f049052b
BG
1912 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1913 "WPA: Backwards compatibility: allow invalid "
1914 "version for non-CCMP group keys");
9f6a7cdd
JM
1915 } else if (ver == WPA_KEY_INFO_TYPE_AES_128_CMAC) {
1916 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1917 "WPA: Interoperability workaround: allow incorrect (should have been HMAC-SHA1), but stronger (is AES-128-CMAC), descriptor version to be used");
6fc6879b
JM
1918 } else
1919 goto out;
801e1173 1920 } else if (sm->pairwise_cipher == WPA_CIPHER_GCMP &&
929a2ea5 1921 !wpa_key_mgmt_suite_b(sm->key_mgmt) &&
801e1173 1922 ver != WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
eb7719ff
JM
1923 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1924 "WPA: GCMP is used, but EAPOL-Key "
1925 "descriptor version (%d) is not 2", ver);
1926 goto out;
1927 }
6fc6879b
JM
1928
1929#ifdef CONFIG_PEERKEY
1930 for (peerkey = sm->peerkey; peerkey; peerkey = peerkey->next) {
1931 if (os_memcmp(peerkey->addr, src_addr, ETH_ALEN) == 0)
1932 break;
1933 }
1934
1935 if (!(key_info & WPA_KEY_INFO_SMK_MESSAGE) && peerkey) {
1936 if (!peerkey->initiator && peerkey->replay_counter_set &&
1937 os_memcmp(key->replay_counter, peerkey->replay_counter,
1938 WPA_REPLAY_COUNTER_LEN) <= 0) {
f049052b
BG
1939 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1940 "RSN: EAPOL-Key Replay Counter did not "
1941 "increase (STK) - dropping packet");
6fc6879b
JM
1942 goto out;
1943 } else if (peerkey->initiator) {
1944 u8 _tmp[WPA_REPLAY_COUNTER_LEN];
1945 os_memcpy(_tmp, key->replay_counter,
1946 WPA_REPLAY_COUNTER_LEN);
1947 inc_byte_array(_tmp, WPA_REPLAY_COUNTER_LEN);
1948 if (os_memcmp(_tmp, peerkey->replay_counter,
1949 WPA_REPLAY_COUNTER_LEN) != 0) {
f049052b
BG
1950 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1951 "RSN: EAPOL-Key Replay "
1952 "Counter did not match (STK) - "
1953 "dropping packet");
6fc6879b
JM
1954 goto out;
1955 }
1956 }
1957 }
1958
1959 if (peerkey && peerkey->initiator && (key_info & WPA_KEY_INFO_ACK)) {
f049052b
BG
1960 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1961 "RSN: Ack bit in key_info from STK peer");
6fc6879b
JM
1962 goto out;
1963 }
1964#endif /* CONFIG_PEERKEY */
1965
1966 if (!peerkey && sm->rx_replay_counter_set &&
1967 os_memcmp(key->replay_counter, sm->rx_replay_counter,
1968 WPA_REPLAY_COUNTER_LEN) <= 0) {
f049052b
BG
1969 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1970 "WPA: EAPOL-Key Replay Counter did not increase - "
1971 "dropping packet");
6fc6879b
JM
1972 goto out;
1973 }
1974
1975 if (!(key_info & (WPA_KEY_INFO_ACK | WPA_KEY_INFO_SMK_MESSAGE))
1976#ifdef CONFIG_PEERKEY
1977 && (peerkey == NULL || !peerkey->initiator)
1978#endif /* CONFIG_PEERKEY */
1979 ) {
f049052b
BG
1980 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1981 "WPA: No Ack bit in key_info");
6fc6879b
JM
1982 goto out;
1983 }
1984
1985 if (key_info & WPA_KEY_INFO_REQUEST) {
f049052b
BG
1986 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1987 "WPA: EAPOL-Key with Request bit - dropped");
6fc6879b
JM
1988 goto out;
1989 }
1990
1991 if ((key_info & WPA_KEY_INFO_MIC) && !peerkey &&
6d014ffc 1992 wpa_supplicant_verify_eapol_key_mic(sm, key, ver, tmp, data_len))
6fc6879b
JM
1993 goto out;
1994
1995#ifdef CONFIG_PEERKEY
1996 if ((key_info & WPA_KEY_INFO_MIC) && peerkey &&
6d014ffc 1997 peerkey_verify_eapol_key_mic(sm, peerkey, key, ver, tmp,
5e3b5197 1998 data_len))
6fc6879b
JM
1999 goto out;
2000#endif /* CONFIG_PEERKEY */
2001
a14896e8 2002 if ((sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN) &&
6fc6879b 2003 (key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
6d014ffc
JM
2004 if (wpa_supplicant_decrypt_key_data(sm, key, mic_len,
2005 ver, key_data,
e6270129 2006 &key_data_len))
d56d7e56 2007 goto out;
6fc6879b
JM
2008 }
2009
2010 if (key_info & WPA_KEY_INFO_KEY_TYPE) {
2011 if (key_info & WPA_KEY_INFO_KEY_INDEX_MASK) {
f049052b
BG
2012 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
2013 "WPA: Ignored EAPOL-Key (Pairwise) with "
2014 "non-zero key index");
6fc6879b
JM
2015 goto out;
2016 }
2017 if (peerkey) {
2018 /* PeerKey 4-Way Handshake */
f107d00c
JM
2019 peerkey_rx_eapol_4way(sm, peerkey, key, key_info, ver,
2020 key_data, key_data_len);
6fc6879b
JM
2021 } else if (key_info & WPA_KEY_INFO_MIC) {
2022 /* 3/4 4-Way Handshake */
e6270129
JM
2023 wpa_supplicant_process_3_of_4(sm, key, ver, key_data,
2024 key_data_len);
6fc6879b
JM
2025 } else {
2026 /* 1/4 4-Way Handshake */
2027 wpa_supplicant_process_1_of_4(sm, src_addr, key,
e6270129
JM
2028 ver, key_data,
2029 key_data_len);
6fc6879b
JM
2030 }
2031 } else if (key_info & WPA_KEY_INFO_SMK_MESSAGE) {
2032 /* PeerKey SMK Handshake */
e6270129 2033 peerkey_rx_eapol_smk(sm, src_addr, key, key_data_len, key_info,
6fc6879b
JM
2034 ver);
2035 } else {
2036 if (key_info & WPA_KEY_INFO_MIC) {
2037 /* 1/2 Group Key Handshake */
2038 wpa_supplicant_process_1_of_2(sm, src_addr, key,
e6270129
JM
2039 key_data, key_data_len,
2040 ver);
6fc6879b 2041 } else {
f049052b
BG
2042 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
2043 "WPA: EAPOL-Key (Group) without Mic bit - "
2044 "dropped");
6fc6879b
JM
2045 }
2046 }
2047
2048 ret = 1;
2049
2050out:
fbfc974c 2051 bin_clear_free(tmp, data_len);
6fc6879b
JM
2052 return ret;
2053}
2054
2055
2056#ifdef CONFIG_CTRL_IFACE
6fc6879b
JM
2057static u32 wpa_key_mgmt_suite(struct wpa_sm *sm)
2058{
2059 switch (sm->key_mgmt) {
2060 case WPA_KEY_MGMT_IEEE8021X:
a14896e8
JM
2061 return ((sm->proto == WPA_PROTO_RSN ||
2062 sm->proto == WPA_PROTO_OSEN) ?
6fc6879b
JM
2063 RSN_AUTH_KEY_MGMT_UNSPEC_802_1X :
2064 WPA_AUTH_KEY_MGMT_UNSPEC_802_1X);
2065 case WPA_KEY_MGMT_PSK:
2066 return (sm->proto == WPA_PROTO_RSN ?
2067 RSN_AUTH_KEY_MGMT_PSK_OVER_802_1X :
2068 WPA_AUTH_KEY_MGMT_PSK_OVER_802_1X);
2069#ifdef CONFIG_IEEE80211R
2070 case WPA_KEY_MGMT_FT_IEEE8021X:
2071 return RSN_AUTH_KEY_MGMT_FT_802_1X;
2072 case WPA_KEY_MGMT_FT_PSK:
2073 return RSN_AUTH_KEY_MGMT_FT_PSK;
2074#endif /* CONFIG_IEEE80211R */
56586197
JM
2075#ifdef CONFIG_IEEE80211W
2076 case WPA_KEY_MGMT_IEEE8021X_SHA256:
2077 return RSN_AUTH_KEY_MGMT_802_1X_SHA256;
2078 case WPA_KEY_MGMT_PSK_SHA256:
2079 return RSN_AUTH_KEY_MGMT_PSK_SHA256;
2080#endif /* CONFIG_IEEE80211W */
369c8d7b
JM
2081 case WPA_KEY_MGMT_CCKM:
2082 return (sm->proto == WPA_PROTO_RSN ?
2083 RSN_AUTH_KEY_MGMT_CCKM:
2084 WPA_AUTH_KEY_MGMT_CCKM);
6fc6879b
JM
2085 case WPA_KEY_MGMT_WPA_NONE:
2086 return WPA_AUTH_KEY_MGMT_NONE;
666497c8
JM
2087 case WPA_KEY_MGMT_IEEE8021X_SUITE_B:
2088 return RSN_AUTH_KEY_MGMT_802_1X_SUITE_B;
5e3b5197
JM
2089 case WPA_KEY_MGMT_IEEE8021X_SUITE_B_192:
2090 return RSN_AUTH_KEY_MGMT_802_1X_SUITE_B_192;
6fc6879b
JM
2091 default:
2092 return 0;
2093 }
2094}
2095
2096
6fc6879b
JM
2097#define RSN_SUITE "%02x-%02x-%02x-%d"
2098#define RSN_SUITE_ARG(s) \
2099((s) >> 24) & 0xff, ((s) >> 16) & 0xff, ((s) >> 8) & 0xff, (s) & 0xff
2100
2101/**
2102 * wpa_sm_get_mib - Dump text list of MIB entries
2103 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2104 * @buf: Buffer for the list
2105 * @buflen: Length of the buffer
2106 * Returns: Number of bytes written to buffer
2107 *
2108 * This function is used fetch dot11 MIB variables.
2109 */
2110int wpa_sm_get_mib(struct wpa_sm *sm, char *buf, size_t buflen)
2111{
2112 char pmkid_txt[PMKID_LEN * 2 + 1];
2113 int rsna, ret;
2114 size_t len;
2115
2116 if (sm->cur_pmksa) {
2117 wpa_snprintf_hex(pmkid_txt, sizeof(pmkid_txt),
2118 sm->cur_pmksa->pmkid, PMKID_LEN);
2119 } else
2120 pmkid_txt[0] = '\0';
2121
56586197
JM
2122 if ((wpa_key_mgmt_wpa_psk(sm->key_mgmt) ||
2123 wpa_key_mgmt_wpa_ieee8021x(sm->key_mgmt)) &&
6fc6879b
JM
2124 sm->proto == WPA_PROTO_RSN)
2125 rsna = 1;
2126 else
2127 rsna = 0;
2128
2129 ret = os_snprintf(buf, buflen,
2130 "dot11RSNAOptionImplemented=TRUE\n"
2131 "dot11RSNAPreauthenticationImplemented=TRUE\n"
2132 "dot11RSNAEnabled=%s\n"
2133 "dot11RSNAPreauthenticationEnabled=%s\n"
2134 "dot11RSNAConfigVersion=%d\n"
2135 "dot11RSNAConfigPairwiseKeysSupported=5\n"
2136 "dot11RSNAConfigGroupCipherSize=%d\n"
2137 "dot11RSNAConfigPMKLifetime=%d\n"
2138 "dot11RSNAConfigPMKReauthThreshold=%d\n"
2139 "dot11RSNAConfigNumberOfPTKSAReplayCounters=1\n"
2140 "dot11RSNAConfigSATimeout=%d\n",
2141 rsna ? "TRUE" : "FALSE",
2142 rsna ? "TRUE" : "FALSE",
2143 RSN_VERSION,
c3550295 2144 wpa_cipher_key_len(sm->group_cipher) * 8,
6fc6879b
JM
2145 sm->dot11RSNAConfigPMKLifetime,
2146 sm->dot11RSNAConfigPMKReauthThreshold,
2147 sm->dot11RSNAConfigSATimeout);
d85e1fc8 2148 if (os_snprintf_error(buflen, ret))
6fc6879b
JM
2149 return 0;
2150 len = ret;
2151
2152 ret = os_snprintf(
2153 buf + len, buflen - len,
2154 "dot11RSNAAuthenticationSuiteSelected=" RSN_SUITE "\n"
2155 "dot11RSNAPairwiseCipherSelected=" RSN_SUITE "\n"
2156 "dot11RSNAGroupCipherSelected=" RSN_SUITE "\n"
2157 "dot11RSNAPMKIDUsed=%s\n"
2158 "dot11RSNAAuthenticationSuiteRequested=" RSN_SUITE "\n"
2159 "dot11RSNAPairwiseCipherRequested=" RSN_SUITE "\n"
2160 "dot11RSNAGroupCipherRequested=" RSN_SUITE "\n"
2161 "dot11RSNAConfigNumberOfGTKSAReplayCounters=0\n"
2162 "dot11RSNA4WayHandshakeFailures=%u\n",
2163 RSN_SUITE_ARG(wpa_key_mgmt_suite(sm)),
c3550295
JM
2164 RSN_SUITE_ARG(wpa_cipher_to_suite(sm->proto,
2165 sm->pairwise_cipher)),
2166 RSN_SUITE_ARG(wpa_cipher_to_suite(sm->proto,
2167 sm->group_cipher)),
6fc6879b
JM
2168 pmkid_txt,
2169 RSN_SUITE_ARG(wpa_key_mgmt_suite(sm)),
c3550295
JM
2170 RSN_SUITE_ARG(wpa_cipher_to_suite(sm->proto,
2171 sm->pairwise_cipher)),
2172 RSN_SUITE_ARG(wpa_cipher_to_suite(sm->proto,
2173 sm->group_cipher)),
6fc6879b 2174 sm->dot11RSNA4WayHandshakeFailures);
1f102d3b 2175 if (!os_snprintf_error(buflen - len, ret))
6fc6879b
JM
2176 len += ret;
2177
2178 return (int) len;
2179}
2180#endif /* CONFIG_CTRL_IFACE */
2181
2182
2183static void wpa_sm_pmksa_free_cb(struct rsn_pmksa_cache_entry *entry,
6aaac006 2184 void *ctx, enum pmksa_free_reason reason)
6fc6879b
JM
2185{
2186 struct wpa_sm *sm = ctx;
6aaac006 2187 int deauth = 0;
6fc6879b 2188
6aaac006
DW
2189 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "RSN: PMKSA cache entry free_cb: "
2190 MACSTR " reason=%d", MAC2STR(entry->aa), reason);
2191
2192 if (sm->cur_pmksa == entry) {
2193 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2194 "RSN: %s current PMKSA entry",
2195 reason == PMKSA_REPLACE ? "replaced" : "removed");
2196 pmksa_cache_clear_current(sm);
2197
2198 /*
2199 * If an entry is simply being replaced, there's no need to
2200 * deauthenticate because it will be immediately re-added.
2201 * This happens when EAP authentication is completed again
2202 * (reauth or failed PMKSA caching attempt).
2203 */
2204 if (reason != PMKSA_REPLACE)
2205 deauth = 1;
2206 }
2207
2208 if (reason == PMKSA_EXPIRE &&
6fc6879b
JM
2209 (sm->pmk_len == entry->pmk_len &&
2210 os_memcmp(sm->pmk, entry->pmk, sm->pmk_len) == 0)) {
f049052b 2211 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
6aaac006
DW
2212 "RSN: deauthenticating due to expired PMK");
2213 pmksa_cache_clear_current(sm);
2214 deauth = 1;
2215 }
6fc6879b 2216
6aaac006 2217 if (deauth) {
6fc6879b
JM
2218 os_memset(sm->pmk, 0, sizeof(sm->pmk));
2219 wpa_sm_deauthenticate(sm, WLAN_REASON_UNSPECIFIED);
6fc6879b
JM
2220 }
2221}
2222
2223
2224/**
2225 * wpa_sm_init - Initialize WPA state machine
2226 * @ctx: Context pointer for callbacks; this needs to be an allocated buffer
2227 * Returns: Pointer to the allocated WPA state machine data
2228 *
2229 * This function is used to allocate a new WPA state machine and the returned
2230 * value is passed to all WPA state machine calls.
2231 */
2232struct wpa_sm * wpa_sm_init(struct wpa_sm_ctx *ctx)
2233{
2234 struct wpa_sm *sm;
2235
2236 sm = os_zalloc(sizeof(*sm));
2237 if (sm == NULL)
2238 return NULL;
c5b26e33 2239 dl_list_init(&sm->pmksa_candidates);
6fc6879b
JM
2240 sm->renew_snonce = 1;
2241 sm->ctx = ctx;
2242
2243 sm->dot11RSNAConfigPMKLifetime = 43200;
2244 sm->dot11RSNAConfigPMKReauthThreshold = 70;
2245 sm->dot11RSNAConfigSATimeout = 60;
2246
2247 sm->pmksa = pmksa_cache_init(wpa_sm_pmksa_free_cb, sm, sm);
2248 if (sm->pmksa == NULL) {
f049052b
BG
2249 wpa_msg(sm->ctx->msg_ctx, MSG_ERROR,
2250 "RSN: PMKSA cache initialization failed");
6fc6879b
JM
2251 os_free(sm);
2252 return NULL;
2253 }
2254
2255 return sm;
2256}
2257
2258
2259/**
2260 * wpa_sm_deinit - Deinitialize WPA state machine
2261 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2262 */
2263void wpa_sm_deinit(struct wpa_sm *sm)
2264{
2265 if (sm == NULL)
2266 return;
2267 pmksa_cache_deinit(sm->pmksa);
2268 eloop_cancel_timeout(wpa_sm_start_preauth, sm, NULL);
581a8cde 2269 eloop_cancel_timeout(wpa_sm_rekey_ptk, sm, NULL);
6fc6879b
JM
2270 os_free(sm->assoc_wpa_ie);
2271 os_free(sm->ap_wpa_ie);
2272 os_free(sm->ap_rsn_ie);
71932544 2273 wpa_sm_drop_sa(sm);
6fc6879b
JM
2274 os_free(sm->ctx);
2275 peerkey_deinit(sm);
55046414
JM
2276#ifdef CONFIG_IEEE80211R
2277 os_free(sm->assoc_resp_ies);
2278#endif /* CONFIG_IEEE80211R */
651c6a84
JM
2279#ifdef CONFIG_TESTING_OPTIONS
2280 wpabuf_free(sm->test_assoc_ie);
2281#endif /* CONFIG_TESTING_OPTIONS */
6fc6879b
JM
2282 os_free(sm);
2283}
2284
2285
2286/**
2287 * wpa_sm_notify_assoc - Notify WPA state machine about association
2288 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2289 * @bssid: The BSSID of the new association
2290 *
2291 * This function is called to let WPA state machine know that the connection
2292 * was established.
2293 */
2294void wpa_sm_notify_assoc(struct wpa_sm *sm, const u8 *bssid)
2295{
58a98fb0
JM
2296 int clear_ptk = 1;
2297
6fc6879b
JM
2298 if (sm == NULL)
2299 return;
2300
f049052b
BG
2301 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2302 "WPA: Association event - clear replay counter");
6fc6879b
JM
2303 os_memcpy(sm->bssid, bssid, ETH_ALEN);
2304 os_memset(sm->rx_replay_counter, 0, WPA_REPLAY_COUNTER_LEN);
2305 sm->rx_replay_counter_set = 0;
2306 sm->renew_snonce = 1;
2307 if (os_memcmp(sm->preauth_bssid, bssid, ETH_ALEN) == 0)
2308 rsn_preauth_deinit(sm);
2309
2310#ifdef CONFIG_IEEE80211R
58a98fb0 2311 if (wpa_ft_is_completed(sm)) {
5d5a9f00
JM
2312 /*
2313 * Clear portValid to kick EAPOL state machine to re-enter
2314 * AUTHENTICATED state to get the EAPOL port Authorized.
2315 */
2316 eapol_sm_notify_portValid(sm->eapol, FALSE);
6fc6879b
JM
2317 wpa_supplicant_key_neg_complete(sm, sm->bssid, 1);
2318
2319 /* Prepare for the next transition */
76b7981d 2320 wpa_ft_prepare_auth_request(sm, NULL);
58a98fb0
JM
2321
2322 clear_ptk = 0;
6fc6879b
JM
2323 }
2324#endif /* CONFIG_IEEE80211R */
58a98fb0
JM
2325
2326 if (clear_ptk) {
2327 /*
2328 * IEEE 802.11, 8.4.10: Delete PTK SA on (re)association if
2329 * this is not part of a Fast BSS Transition.
2330 */
f049052b 2331 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Clear old PTK");
58a98fb0 2332 sm->ptk_set = 0;
d2c33b91 2333 os_memset(&sm->ptk, 0, sizeof(sm->ptk));
58a98fb0 2334 sm->tptk_set = 0;
d2c33b91 2335 os_memset(&sm->tptk, 0, sizeof(sm->tptk));
58a98fb0 2336 }
40cf22e6
JM
2337
2338#ifdef CONFIG_TDLS
2339 wpa_tdls_assoc(sm);
2340#endif /* CONFIG_TDLS */
25ef8529
JM
2341
2342#ifdef CONFIG_P2P
2343 os_memset(sm->p2p_ip_addr, 0, sizeof(sm->p2p_ip_addr));
2344#endif /* CONFIG_P2P */
6fc6879b
JM
2345}
2346
2347
2348/**
2349 * wpa_sm_notify_disassoc - Notify WPA state machine about disassociation
2350 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2351 *
2352 * This function is called to let WPA state machine know that the connection
2353 * was lost. This will abort any existing pre-authentication session.
2354 */
2355void wpa_sm_notify_disassoc(struct wpa_sm *sm)
2356{
ac8e074e
JM
2357 eloop_cancel_timeout(wpa_sm_start_preauth, sm, NULL);
2358 eloop_cancel_timeout(wpa_sm_rekey_ptk, sm, NULL);
77327298 2359 peerkey_deinit(sm);
6fc6879b 2360 rsn_preauth_deinit(sm);
0639970d 2361 pmksa_cache_clear_current(sm);
6fc6879b
JM
2362 if (wpa_sm_get_state(sm) == WPA_4WAY_HANDSHAKE)
2363 sm->dot11RSNA4WayHandshakeFailures++;
40cf22e6
JM
2364#ifdef CONFIG_TDLS
2365 wpa_tdls_disassoc(sm);
2366#endif /* CONFIG_TDLS */
71932544
JM
2367
2368 /* Keys are not needed in the WPA state machine anymore */
2369 wpa_sm_drop_sa(sm);
761396e4
JM
2370
2371 sm->msg_3_of_4_ok = 0;
6fc6879b
JM
2372}
2373
2374
2375/**
2376 * wpa_sm_set_pmk - Set PMK
2377 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2378 * @pmk: The new PMK
2379 * @pmk_len: The length of the new PMK in bytes
70c93963 2380 * @pmkid: Calculated PMKID
bc26ac50 2381 * @bssid: AA to add into PMKSA cache or %NULL to not cache the PMK
6fc6879b
JM
2382 *
2383 * Configure the PMK for WPA state machine.
2384 */
bc26ac50 2385void wpa_sm_set_pmk(struct wpa_sm *sm, const u8 *pmk, size_t pmk_len,
70c93963 2386 const u8 *pmkid, const u8 *bssid)
6fc6879b
JM
2387{
2388 if (sm == NULL)
2389 return;
2390
2391 sm->pmk_len = pmk_len;
2392 os_memcpy(sm->pmk, pmk, pmk_len);
2393
2394#ifdef CONFIG_IEEE80211R
2395 /* Set XXKey to be PSK for FT key derivation */
2396 sm->xxkey_len = pmk_len;
2397 os_memcpy(sm->xxkey, pmk, pmk_len);
2398#endif /* CONFIG_IEEE80211R */
bc26ac50
JM
2399
2400 if (bssid) {
70c93963 2401 pmksa_cache_add(sm->pmksa, pmk, pmk_len, pmkid, NULL, 0,
087a1f4e 2402 bssid, sm->own_addr,
bc26ac50
JM
2403 sm->network_ctx, sm->key_mgmt);
2404 }
6fc6879b
JM
2405}
2406
2407
2408/**
2409 * wpa_sm_set_pmk_from_pmksa - Set PMK based on the current PMKSA
2410 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2411 *
2412 * Take the PMK from the current PMKSA into use. If no PMKSA is active, the PMK
2413 * will be cleared.
2414 */
2415void wpa_sm_set_pmk_from_pmksa(struct wpa_sm *sm)
2416{
2417 if (sm == NULL)
2418 return;
2419
2420 if (sm->cur_pmksa) {
2421 sm->pmk_len = sm->cur_pmksa->pmk_len;
2422 os_memcpy(sm->pmk, sm->cur_pmksa->pmk, sm->pmk_len);
2423 } else {
2424 sm->pmk_len = PMK_LEN;
2425 os_memset(sm->pmk, 0, PMK_LEN);
2426 }
2427}
2428
2429
2430/**
2431 * wpa_sm_set_fast_reauth - Set fast reauthentication (EAP) enabled/disabled
2432 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2433 * @fast_reauth: Whether fast reauthentication (EAP) is allowed
2434 */
2435void wpa_sm_set_fast_reauth(struct wpa_sm *sm, int fast_reauth)
2436{
2437 if (sm)
2438 sm->fast_reauth = fast_reauth;
2439}
2440
2441
2442/**
2443 * wpa_sm_set_scard_ctx - Set context pointer for smartcard callbacks
2444 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2445 * @scard_ctx: Context pointer for smartcard related callback functions
2446 */
2447void wpa_sm_set_scard_ctx(struct wpa_sm *sm, void *scard_ctx)
2448{
2449 if (sm == NULL)
2450 return;
2451 sm->scard_ctx = scard_ctx;
2452 if (sm->preauth_eapol)
2453 eapol_sm_register_scard_ctx(sm->preauth_eapol, scard_ctx);
2454}
2455
2456
2457/**
2458 * wpa_sm_set_config - Notification of current configration change
2459 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2460 * @config: Pointer to current network configuration
2461 *
2462 * Notify WPA state machine that configuration has changed. config will be
2463 * stored as a backpointer to network configuration. This can be %NULL to clear
2464 * the stored pointed.
2465 */
2466void wpa_sm_set_config(struct wpa_sm *sm, struct rsn_supp_config *config)
2467{
2468 if (!sm)
2469 return;
2470
6fc6879b 2471 if (config) {
886a807f 2472 sm->network_ctx = config->network_ctx;
6fc6879b
JM
2473 sm->peerkey_enabled = config->peerkey_enabled;
2474 sm->allowed_pairwise_cipher = config->allowed_pairwise_cipher;
2475 sm->proactive_key_caching = config->proactive_key_caching;
2476 sm->eap_workaround = config->eap_workaround;
2477 sm->eap_conf_ctx = config->eap_conf_ctx;
2478 if (config->ssid) {
2479 os_memcpy(sm->ssid, config->ssid, config->ssid_len);
2480 sm->ssid_len = config->ssid_len;
2481 } else
2482 sm->ssid_len = 0;
581a8cde 2483 sm->wpa_ptk_rekey = config->wpa_ptk_rekey;
25ef8529 2484 sm->p2p = config->p2p;
73ed03f3 2485 sm->wpa_rsc_relaxation = config->wpa_rsc_relaxation;
6fc6879b 2486 } else {
886a807f 2487 sm->network_ctx = NULL;
6fc6879b
JM
2488 sm->peerkey_enabled = 0;
2489 sm->allowed_pairwise_cipher = 0;
2490 sm->proactive_key_caching = 0;
2491 sm->eap_workaround = 0;
2492 sm->eap_conf_ctx = NULL;
2493 sm->ssid_len = 0;
581a8cde 2494 sm->wpa_ptk_rekey = 0;
25ef8529 2495 sm->p2p = 0;
73ed03f3 2496 sm->wpa_rsc_relaxation = 0;
6fc6879b 2497 }
6fc6879b
JM
2498}
2499
2500
2501/**
2502 * wpa_sm_set_own_addr - Set own MAC address
2503 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2504 * @addr: Own MAC address
2505 */
2506void wpa_sm_set_own_addr(struct wpa_sm *sm, const u8 *addr)
2507{
2508 if (sm)
2509 os_memcpy(sm->own_addr, addr, ETH_ALEN);
2510}
2511
2512
2513/**
2514 * wpa_sm_set_ifname - Set network interface name
2515 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2516 * @ifname: Interface name
2517 * @bridge_ifname: Optional bridge interface name (for pre-auth)
2518 */
2519void wpa_sm_set_ifname(struct wpa_sm *sm, const char *ifname,
2520 const char *bridge_ifname)
2521{
2522 if (sm) {
2523 sm->ifname = ifname;
2524 sm->bridge_ifname = bridge_ifname;
2525 }
2526}
2527
2528
2529/**
2530 * wpa_sm_set_eapol - Set EAPOL state machine pointer
2531 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2532 * @eapol: Pointer to EAPOL state machine allocated with eapol_sm_init()
2533 */
2534void wpa_sm_set_eapol(struct wpa_sm *sm, struct eapol_sm *eapol)
2535{
2536 if (sm)
2537 sm->eapol = eapol;
2538}
2539
2540
2541/**
2542 * wpa_sm_set_param - Set WPA state machine parameters
2543 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2544 * @param: Parameter field
2545 * @value: Parameter value
2546 * Returns: 0 on success, -1 on failure
2547 */
2548int wpa_sm_set_param(struct wpa_sm *sm, enum wpa_sm_conf_params param,
2549 unsigned int value)
2550{
2551 int ret = 0;
2552
2553 if (sm == NULL)
2554 return -1;
2555
2556 switch (param) {
2557 case RSNA_PMK_LIFETIME:
2558 if (value > 0)
2559 sm->dot11RSNAConfigPMKLifetime = value;
2560 else
2561 ret = -1;
2562 break;
2563 case RSNA_PMK_REAUTH_THRESHOLD:
2564 if (value > 0 && value <= 100)
2565 sm->dot11RSNAConfigPMKReauthThreshold = value;
2566 else
2567 ret = -1;
2568 break;
2569 case RSNA_SA_TIMEOUT:
2570 if (value > 0)
2571 sm->dot11RSNAConfigSATimeout = value;
2572 else
2573 ret = -1;
2574 break;
2575 case WPA_PARAM_PROTO:
2576 sm->proto = value;
2577 break;
2578 case WPA_PARAM_PAIRWISE:
2579 sm->pairwise_cipher = value;
2580 break;
2581 case WPA_PARAM_GROUP:
2582 sm->group_cipher = value;
2583 break;
2584 case WPA_PARAM_KEY_MGMT:
2585 sm->key_mgmt = value;
2586 break;
2587#ifdef CONFIG_IEEE80211W
2588 case WPA_PARAM_MGMT_GROUP:
2589 sm->mgmt_group_cipher = value;
2590 break;
2591#endif /* CONFIG_IEEE80211W */
2592 case WPA_PARAM_RSN_ENABLED:
2593 sm->rsn_enabled = value;
2594 break;
e820cf95
JM
2595 case WPA_PARAM_MFP:
2596 sm->mfp = value;
2597 break;
6fc6879b
JM
2598 default:
2599 break;
2600 }
2601
2602 return ret;
2603}
2604
2605
6fc6879b
JM
2606/**
2607 * wpa_sm_get_status - Get WPA state machine
2608 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2609 * @buf: Buffer for status information
2610 * @buflen: Maximum buffer length
2611 * @verbose: Whether to include verbose status information
2612 * Returns: Number of bytes written to buf.
2613 *
2614 * Query WPA state machine for status information. This function fills in
2615 * a text area with current status information. If the buffer (buf) is not
2616 * large enough, status information will be truncated to fit the buffer.
2617 */
2618int wpa_sm_get_status(struct wpa_sm *sm, char *buf, size_t buflen,
2619 int verbose)
2620{
2621 char *pos = buf, *end = buf + buflen;
2622 int ret;
2623
2624 ret = os_snprintf(pos, end - pos,
2625 "pairwise_cipher=%s\n"
2626 "group_cipher=%s\n"
2627 "key_mgmt=%s\n",
2628 wpa_cipher_txt(sm->pairwise_cipher),
2629 wpa_cipher_txt(sm->group_cipher),
2630 wpa_key_mgmt_txt(sm->key_mgmt, sm->proto));
d85e1fc8 2631 if (os_snprintf_error(end - pos, ret))
6fc6879b
JM
2632 return pos - buf;
2633 pos += ret;
13e1d2e2
JM
2634
2635 if (sm->mfp != NO_MGMT_FRAME_PROTECTION && sm->ap_rsn_ie) {
2636 struct wpa_ie_data rsn;
2637 if (wpa_parse_wpa_ie_rsn(sm->ap_rsn_ie, sm->ap_rsn_ie_len, &rsn)
2638 >= 0 &&
2639 rsn.capabilities & (WPA_CAPABILITY_MFPR |
2640 WPA_CAPABILITY_MFPC)) {
2641 ret = os_snprintf(pos, end - pos, "pmf=%d\n",
2642 (rsn.capabilities &
2643 WPA_CAPABILITY_MFPR) ? 2 : 1);
d85e1fc8 2644 if (os_snprintf_error(end - pos, ret))
13e1d2e2
JM
2645 return pos - buf;
2646 pos += ret;
2647 }
2648 }
2649
6fc6879b
JM
2650 return pos - buf;
2651}
2652
2653
ae8535b6
JM
2654int wpa_sm_pmf_enabled(struct wpa_sm *sm)
2655{
2656 struct wpa_ie_data rsn;
2657
2658 if (sm->mfp == NO_MGMT_FRAME_PROTECTION || !sm->ap_rsn_ie)
2659 return 0;
2660
2661 if (wpa_parse_wpa_ie_rsn(sm->ap_rsn_ie, sm->ap_rsn_ie_len, &rsn) >= 0 &&
2662 rsn.capabilities & (WPA_CAPABILITY_MFPR | WPA_CAPABILITY_MFPC))
2663 return 1;
2664
2665 return 0;
2666}
2667
2668
6fc6879b
JM
2669/**
2670 * wpa_sm_set_assoc_wpa_ie_default - Generate own WPA/RSN IE from configuration
2671 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2672 * @wpa_ie: Pointer to buffer for WPA/RSN IE
2673 * @wpa_ie_len: Pointer to the length of the wpa_ie buffer
2674 * Returns: 0 on success, -1 on failure
2675 */
2676int wpa_sm_set_assoc_wpa_ie_default(struct wpa_sm *sm, u8 *wpa_ie,
2677 size_t *wpa_ie_len)
2678{
2679 int res;
2680
2681 if (sm == NULL)
2682 return -1;
2683
651c6a84
JM
2684#ifdef CONFIG_TESTING_OPTIONS
2685 if (sm->test_assoc_ie) {
2686 wpa_printf(MSG_DEBUG,
2687 "TESTING: Replace association WPA/RSN IE");
2688 if (*wpa_ie_len < wpabuf_len(sm->test_assoc_ie))
2689 return -1;
2690 os_memcpy(wpa_ie, wpabuf_head(sm->test_assoc_ie),
2691 wpabuf_len(sm->test_assoc_ie));
2692 res = wpabuf_len(sm->test_assoc_ie);
2693 } else
2694#endif /* CONFIG_TESTING_OPTIONS */
6fc6879b
JM
2695 res = wpa_gen_wpa_ie(sm, wpa_ie, *wpa_ie_len);
2696 if (res < 0)
2697 return -1;
2698 *wpa_ie_len = res;
2699
2700 wpa_hexdump(MSG_DEBUG, "WPA: Set own WPA IE default",
2701 wpa_ie, *wpa_ie_len);
2702
2703 if (sm->assoc_wpa_ie == NULL) {
2704 /*
2705 * Make a copy of the WPA/RSN IE so that 4-Way Handshake gets
2706 * the correct version of the IE even if PMKSA caching is
2707 * aborted (which would remove PMKID from IE generation).
2708 */
2709 sm->assoc_wpa_ie = os_malloc(*wpa_ie_len);
2710 if (sm->assoc_wpa_ie == NULL)
2711 return -1;
2712
2713 os_memcpy(sm->assoc_wpa_ie, wpa_ie, *wpa_ie_len);
2714 sm->assoc_wpa_ie_len = *wpa_ie_len;
2715 }
2716
2717 return 0;
2718}
2719
2720
2721/**
2722 * wpa_sm_set_assoc_wpa_ie - Set own WPA/RSN IE from (Re)AssocReq
2723 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2724 * @ie: Pointer to IE data (starting from id)
2725 * @len: IE length
2726 * Returns: 0 on success, -1 on failure
2727 *
2728 * Inform WPA state machine about the WPA/RSN IE used in (Re)Association
2729 * Request frame. The IE will be used to override the default value generated
2730 * with wpa_sm_set_assoc_wpa_ie_default().
2731 */
2732int wpa_sm_set_assoc_wpa_ie(struct wpa_sm *sm, const u8 *ie, size_t len)
2733{
2734 if (sm == NULL)
2735 return -1;
2736
2737 os_free(sm->assoc_wpa_ie);
2738 if (ie == NULL || len == 0) {
f049052b
BG
2739 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2740 "WPA: clearing own WPA/RSN IE");
6fc6879b
JM
2741 sm->assoc_wpa_ie = NULL;
2742 sm->assoc_wpa_ie_len = 0;
2743 } else {
2744 wpa_hexdump(MSG_DEBUG, "WPA: set own WPA/RSN IE", ie, len);
2745 sm->assoc_wpa_ie = os_malloc(len);
2746 if (sm->assoc_wpa_ie == NULL)
2747 return -1;
2748
2749 os_memcpy(sm->assoc_wpa_ie, ie, len);
2750 sm->assoc_wpa_ie_len = len;
2751 }
2752
2753 return 0;
2754}
2755
2756
2757/**
2758 * wpa_sm_set_ap_wpa_ie - Set AP WPA IE from Beacon/ProbeResp
2759 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2760 * @ie: Pointer to IE data (starting from id)
2761 * @len: IE length
2762 * Returns: 0 on success, -1 on failure
2763 *
2764 * Inform WPA state machine about the WPA IE used in Beacon / Probe Response
2765 * frame.
2766 */
2767int wpa_sm_set_ap_wpa_ie(struct wpa_sm *sm, const u8 *ie, size_t len)
2768{
2769 if (sm == NULL)
2770 return -1;
2771
2772 os_free(sm->ap_wpa_ie);
2773 if (ie == NULL || len == 0) {
f049052b
BG
2774 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2775 "WPA: clearing AP WPA IE");
6fc6879b
JM
2776 sm->ap_wpa_ie = NULL;
2777 sm->ap_wpa_ie_len = 0;
2778 } else {
2779 wpa_hexdump(MSG_DEBUG, "WPA: set AP WPA IE", ie, len);
2780 sm->ap_wpa_ie = os_malloc(len);
2781 if (sm->ap_wpa_ie == NULL)
2782 return -1;
2783
2784 os_memcpy(sm->ap_wpa_ie, ie, len);
2785 sm->ap_wpa_ie_len = len;
2786 }
2787
2788 return 0;
2789}
2790
2791
2792/**
2793 * wpa_sm_set_ap_rsn_ie - Set AP RSN IE from Beacon/ProbeResp
2794 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2795 * @ie: Pointer to IE data (starting from id)
2796 * @len: IE length
2797 * Returns: 0 on success, -1 on failure
2798 *
2799 * Inform WPA state machine about the RSN IE used in Beacon / Probe Response
2800 * frame.
2801 */
2802int wpa_sm_set_ap_rsn_ie(struct wpa_sm *sm, const u8 *ie, size_t len)
2803{
2804 if (sm == NULL)
2805 return -1;
2806
2807 os_free(sm->ap_rsn_ie);
2808 if (ie == NULL || len == 0) {
f049052b
BG
2809 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2810 "WPA: clearing AP RSN IE");
6fc6879b
JM
2811 sm->ap_rsn_ie = NULL;
2812 sm->ap_rsn_ie_len = 0;
2813 } else {
2814 wpa_hexdump(MSG_DEBUG, "WPA: set AP RSN IE", ie, len);
2815 sm->ap_rsn_ie = os_malloc(len);
2816 if (sm->ap_rsn_ie == NULL)
2817 return -1;
2818
2819 os_memcpy(sm->ap_rsn_ie, ie, len);
2820 sm->ap_rsn_ie_len = len;
2821 }
2822
2823 return 0;
2824}
2825
2826
2827/**
2828 * wpa_sm_parse_own_wpa_ie - Parse own WPA/RSN IE
2829 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2830 * @data: Pointer to data area for parsing results
2831 * Returns: 0 on success, -1 if IE is not known, or -2 on parsing failure
2832 *
2833 * Parse the contents of the own WPA or RSN IE from (Re)AssocReq and write the
2834 * parsed data into data.
2835 */
2836int wpa_sm_parse_own_wpa_ie(struct wpa_sm *sm, struct wpa_ie_data *data)
2837{
2a522e71
JM
2838 if (sm == NULL)
2839 return -1;
2840
2841 if (sm->assoc_wpa_ie == NULL) {
f049052b
BG
2842 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2843 "WPA: No WPA/RSN IE available from association info");
6fc6879b
JM
2844 return -1;
2845 }
2846 if (wpa_parse_wpa_ie(sm->assoc_wpa_ie, sm->assoc_wpa_ie_len, data))
2847 return -2;
2848 return 0;
2849}
540264a7
JM
2850
2851
2852int wpa_sm_pmksa_cache_list(struct wpa_sm *sm, char *buf, size_t len)
2853{
540264a7 2854 return pmksa_cache_list(sm->pmksa, buf, len);
540264a7 2855}
32d5295f
JM
2856
2857
2858void wpa_sm_drop_sa(struct wpa_sm *sm)
2859{
f049052b 2860 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Clear old PMK and PTK");
32d5295f
JM
2861 sm->ptk_set = 0;
2862 sm->tptk_set = 0;
2863 os_memset(sm->pmk, 0, sizeof(sm->pmk));
2864 os_memset(&sm->ptk, 0, sizeof(sm->ptk));
2865 os_memset(&sm->tptk, 0, sizeof(sm->tptk));
71932544
JM
2866#ifdef CONFIG_IEEE80211R
2867 os_memset(sm->xxkey, 0, sizeof(sm->xxkey));
2868 os_memset(sm->pmk_r0, 0, sizeof(sm->pmk_r0));
2869 os_memset(sm->pmk_r1, 0, sizeof(sm->pmk_r1));
2870#endif /* CONFIG_IEEE80211R */
32d5295f 2871}
0d7b4409
JM
2872
2873
2874int wpa_sm_has_ptk(struct wpa_sm *sm)
2875{
2876 if (sm == NULL)
2877 return 0;
2878 return sm->ptk_set;
2879}
b14a210c
JB
2880
2881
2882void wpa_sm_update_replay_ctr(struct wpa_sm *sm, const u8 *replay_ctr)
2883{
2884 os_memcpy(sm->rx_replay_counter, replay_ctr, WPA_REPLAY_COUNTER_LEN);
2885}
d8a790b9
JM
2886
2887
2888void wpa_sm_pmksa_cache_flush(struct wpa_sm *sm, void *network_ctx)
2889{
4033935d 2890 pmksa_cache_flush(sm->pmksa, network_ctx, NULL, 0);
d8a790b9 2891}
75cad1a0
XC
2892
2893
ad3872a3 2894#ifdef CONFIG_WNM
75cad1a0
XC
2895int wpa_wnmsleep_install_key(struct wpa_sm *sm, u8 subelem_id, u8 *buf)
2896{
75cad1a0
XC
2897 u16 keyinfo;
2898 u8 keylen; /* plaintext key len */
75cad1a0
XC
2899 u8 *key_rsc;
2900
75cad1a0 2901 if (subelem_id == WNM_SLEEP_SUBELEM_GTK) {
d2c33b91
JM
2902 struct wpa_gtk_data gd;
2903
2904 os_memset(&gd, 0, sizeof(gd));
2905 keylen = wpa_cipher_key_len(sm->group_cipher);
2906 gd.key_rsc_len = wpa_cipher_rsc_len(sm->group_cipher);
2907 gd.alg = wpa_cipher_to_alg(sm->group_cipher);
2908 if (gd.alg == WPA_ALG_NONE) {
2909 wpa_printf(MSG_DEBUG, "Unsupported group cipher suite");
2910 return -1;
2911 }
2912
75cad1a0 2913 key_rsc = buf + 5;
68db9ab0 2914 keyinfo = WPA_GET_LE16(buf + 2);
75cad1a0
XC
2915 gd.gtk_len = keylen;
2916 if (gd.gtk_len != buf[4]) {
2917 wpa_printf(MSG_DEBUG, "GTK len mismatch len %d vs %d",
2918 gd.gtk_len, buf[4]);
2919 return -1;
2920 }
2921 gd.keyidx = keyinfo & 0x03; /* B0 - B1 */
2922 gd.tx = wpa_supplicant_gtk_tx_bit_workaround(
2923 sm, !!(keyinfo & WPA_KEY_INFO_TXRX));
2924
68db9ab0 2925 os_memcpy(gd.gtk, buf + 13, gd.gtk_len);
75cad1a0
XC
2926
2927 wpa_hexdump_key(MSG_DEBUG, "Install GTK (WNM SLEEP)",
2928 gd.gtk, gd.gtk_len);
2929 if (wpa_supplicant_install_gtk(sm, &gd, key_rsc)) {
d2c33b91 2930 os_memset(&gd, 0, sizeof(gd));
75cad1a0
XC
2931 wpa_printf(MSG_DEBUG, "Failed to install the GTK in "
2932 "WNM mode");
2933 return -1;
2934 }
d2c33b91 2935 os_memset(&gd, 0, sizeof(gd));
75cad1a0
XC
2936#ifdef CONFIG_IEEE80211W
2937 } else if (subelem_id == WNM_SLEEP_SUBELEM_IGTK) {
d2c33b91
JM
2938 struct wpa_igtk_kde igd;
2939 u16 keyidx;
2940
2941 os_memset(&igd, 0, sizeof(igd));
8dd9f9cd 2942 keylen = wpa_cipher_key_len(sm->mgmt_group_cipher);
75cad1a0
XC
2943 os_memcpy(igd.keyid, buf + 2, 2);
2944 os_memcpy(igd.pn, buf + 4, 6);
2945
2946 keyidx = WPA_GET_LE16(igd.keyid);
8dd9f9cd 2947 os_memcpy(igd.igtk, buf + 10, keylen);
75cad1a0
XC
2948
2949 wpa_hexdump_key(MSG_DEBUG, "Install IGTK (WNM SLEEP)",
8dd9f9cd
JM
2950 igd.igtk, keylen);
2951 if (wpa_sm_set_key(sm, wpa_cipher_to_alg(sm->mgmt_group_cipher),
2952 broadcast_ether_addr,
75cad1a0 2953 keyidx, 0, igd.pn, sizeof(igd.pn),
8dd9f9cd 2954 igd.igtk, keylen) < 0) {
75cad1a0
XC
2955 wpa_printf(MSG_DEBUG, "Failed to install the IGTK in "
2956 "WNM mode");
d2c33b91 2957 os_memset(&igd, 0, sizeof(igd));
75cad1a0
XC
2958 return -1;
2959 }
d2c33b91 2960 os_memset(&igd, 0, sizeof(igd));
75cad1a0
XC
2961#endif /* CONFIG_IEEE80211W */
2962 } else {
2963 wpa_printf(MSG_DEBUG, "Unknown element id");
2964 return -1;
2965 }
2966
2967 return 0;
2968}
ad3872a3 2969#endif /* CONFIG_WNM */
db76aa64
JM
2970
2971
2972#ifdef CONFIG_PEERKEY
2973int wpa_sm_rx_eapol_peerkey(struct wpa_sm *sm, const u8 *src_addr,
2974 const u8 *buf, size_t len)
2975{
2976 struct wpa_peerkey *peerkey;
2977
2978 for (peerkey = sm->peerkey; peerkey; peerkey = peerkey->next) {
2979 if (os_memcmp(peerkey->addr, src_addr, ETH_ALEN) == 0)
2980 break;
2981 }
2982
2983 if (!peerkey)
2984 return 0;
2985
2986 wpa_sm_rx_eapol(sm, src_addr, buf, len);
2987
2988 return 1;
2989}
2990#endif /* CONFIG_PEERKEY */
25ef8529
JM
2991
2992
2993#ifdef CONFIG_P2P
2994
2995int wpa_sm_get_p2p_ip_addr(struct wpa_sm *sm, u8 *buf)
2996{
2997 if (sm == NULL || WPA_GET_BE32(sm->p2p_ip_addr) == 0)
2998 return -1;
2999 os_memcpy(buf, sm->p2p_ip_addr, 3 * 4);
3000 return 0;
3001}
3002
3003#endif /* CONFIG_P2P */
b41f2684
CL
3004
3005
3006void wpa_sm_set_rx_replay_ctr(struct wpa_sm *sm, const u8 *rx_replay_counter)
3007{
3008 if (rx_replay_counter == NULL)
3009 return;
3010
3011 os_memcpy(sm->rx_replay_counter, rx_replay_counter,
3012 WPA_REPLAY_COUNTER_LEN);
3013 sm->rx_replay_counter_set = 1;
3014 wpa_printf(MSG_DEBUG, "Updated key replay counter");
3015}
3016
3017
98cd3d1c
JM
3018void wpa_sm_set_ptk_kck_kek(struct wpa_sm *sm,
3019 const u8 *ptk_kck, size_t ptk_kck_len,
3020 const u8 *ptk_kek, size_t ptk_kek_len)
b41f2684 3021{
98cd3d1c
JM
3022 if (ptk_kck && ptk_kck_len <= WPA_KCK_MAX_LEN) {
3023 os_memcpy(sm->ptk.kck, ptk_kck, ptk_kck_len);
3024 sm->ptk.kck_len = ptk_kck_len;
b41f2684
CL
3025 wpa_printf(MSG_DEBUG, "Updated PTK KCK");
3026 }
98cd3d1c
JM
3027 if (ptk_kek && ptk_kek_len <= WPA_KEK_MAX_LEN) {
3028 os_memcpy(sm->ptk.kek, ptk_kek, ptk_kek_len);
3029 sm->ptk.kek_len = ptk_kek_len;
b41f2684
CL
3030 wpa_printf(MSG_DEBUG, "Updated PTK KEK");
3031 }
3032 sm->ptk_set = 1;
3033}
651c6a84
JM
3034
3035
3036#ifdef CONFIG_TESTING_OPTIONS
3037void wpa_sm_set_test_assoc_ie(struct wpa_sm *sm, struct wpabuf *buf)
3038{
3039 wpabuf_free(sm->test_assoc_ie);
3040 sm->test_assoc_ie = buf;
3041}
3042#endif /* CONFIG_TESTING_OPTIONS */