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