]> git.ipfire.org Git - thirdparty/hostap.git/blame - src/rsn_supp/wpa.c
Check os_snprintf() result more consistently - success case
[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;
c397eff8 1279 u16 gtk_len;
6fc6879b 1280
c397eff8 1281 gtk_len = WPA_GET_BE16(key->key_length);
e6270129 1282 maxkeylen = key_data_len;
6fc6879b
JM
1283 if (ver == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
1284 if (maxkeylen < 8) {
f049052b
BG
1285 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1286 "WPA: Too short maxkeylen (%lu)",
1287 (unsigned long) maxkeylen);
6fc6879b
JM
1288 return -1;
1289 }
1290 maxkeylen -= 8;
1291 }
1292
c397eff8
JM
1293 if (gtk_len > maxkeylen ||
1294 wpa_supplicant_check_group_cipher(sm, sm->group_cipher,
1295 gtk_len, maxkeylen,
6fc6879b
JM
1296 &gd->key_rsc_len, &gd->alg))
1297 return -1;
1298
c397eff8 1299 gd->gtk_len = gtk_len;
6fc6879b
JM
1300 gd->keyidx = (key_info & WPA_KEY_INFO_KEY_INDEX_MASK) >>
1301 WPA_KEY_INFO_KEY_INDEX_SHIFT;
1302 if (ver == WPA_KEY_INFO_TYPE_HMAC_MD5_RC4) {
d2c33b91 1303 u8 ek[32];
e6270129 1304 if (key_data_len > sizeof(gd->gtk)) {
f049052b
BG
1305 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1306 "WPA: RC4 key data too long (%lu)",
e6270129 1307 (unsigned long) key_data_len);
6fc6879b
JM
1308 return -1;
1309 }
d2c33b91
JM
1310 os_memcpy(ek, key->key_iv, 16);
1311 os_memcpy(ek + 16, sm->ptk.kek, 16);
e6270129
JM
1312 os_memcpy(gd->gtk, key_data, key_data_len);
1313 if (rc4_skip(ek, 32, 256, gd->gtk, key_data_len)) {
d2c33b91 1314 os_memset(ek, 0, sizeof(ek));
f049052b
BG
1315 wpa_msg(sm->ctx->msg_ctx, MSG_ERROR,
1316 "WPA: RC4 failed");
7a215dfc
JM
1317 return -1;
1318 }
d2c33b91 1319 os_memset(ek, 0, sizeof(ek));
6fc6879b 1320 } else if (ver == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
e6270129 1321 if (maxkeylen % 8) {
f049052b
BG
1322 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1323 "WPA: Unsupported AES-WRAP len %lu",
e6270129 1324 (unsigned long) maxkeylen);
6fc6879b
JM
1325 return -1;
1326 }
1327 if (maxkeylen > sizeof(gd->gtk)) {
f049052b
BG
1328 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1329 "WPA: AES-WRAP key data "
1330 "too long (keydatalen=%lu maxkeylen=%lu)",
e6270129 1331 (unsigned long) key_data_len,
f049052b 1332 (unsigned long) maxkeylen);
6fc6879b
JM
1333 return -1;
1334 }
eefec1e4
JM
1335 if (aes_unwrap(sm->ptk.kek, 16, maxkeylen / 8, key_data,
1336 gd->gtk)) {
f049052b
BG
1337 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1338 "WPA: AES unwrap failed - could not decrypt "
1339 "GTK");
6fc6879b
JM
1340 return -1;
1341 }
1342 } else {
f049052b
BG
1343 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1344 "WPA: Unsupported key_info type %d", ver);
6fc6879b
JM
1345 return -1;
1346 }
1347 gd->tx = wpa_supplicant_gtk_tx_bit_workaround(
1348 sm, !!(key_info & WPA_KEY_INFO_TXRX));
1349 return 0;
1350}
1351
1352
1353static int wpa_supplicant_send_2_of_2(struct wpa_sm *sm,
1354 const struct wpa_eapol_key *key,
1355 int ver, u16 key_info)
1356{
1357 size_t rlen;
1358 struct wpa_eapol_key *reply;
1359 u8 *rbuf;
1360
1361 rbuf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_KEY, NULL,
1362 sizeof(*reply), &rlen, (void *) &reply);
1363 if (rbuf == NULL)
1364 return -1;
1365
a14896e8
JM
1366 reply->type = (sm->proto == WPA_PROTO_RSN ||
1367 sm->proto == WPA_PROTO_OSEN) ?
6fc6879b
JM
1368 EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA;
1369 key_info &= WPA_KEY_INFO_KEY_INDEX_MASK;
1370 key_info |= ver | WPA_KEY_INFO_MIC | WPA_KEY_INFO_SECURE;
1371 WPA_PUT_BE16(reply->key_info, key_info);
a14896e8 1372 if (sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN)
6fc6879b
JM
1373 WPA_PUT_BE16(reply->key_length, 0);
1374 else
1375 os_memcpy(reply->key_length, key->key_length, 2);
1376 os_memcpy(reply->replay_counter, key->replay_counter,
1377 WPA_REPLAY_COUNTER_LEN);
1378
1379 WPA_PUT_BE16(reply->key_data_length, 0);
1380
f049052b 1381 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Sending EAPOL-Key 2/2");
6fc6879b
JM
1382 wpa_eapol_key_send(sm, sm->ptk.kck, ver, sm->bssid, ETH_P_EAPOL,
1383 rbuf, rlen, reply->key_mic);
1384
1385 return 0;
1386}
1387
1388
1389static void wpa_supplicant_process_1_of_2(struct wpa_sm *sm,
1390 const unsigned char *src_addr,
1391 const struct wpa_eapol_key *key,
e6270129
JM
1392 const u8 *key_data,
1393 size_t key_data_len, u16 ver)
6fc6879b 1394{
e6270129 1395 u16 key_info;
6fc6879b
JM
1396 int rekey, ret;
1397 struct wpa_gtk_data gd;
1398
1399 os_memset(&gd, 0, sizeof(gd));
1400
1401 rekey = wpa_sm_get_state(sm) == WPA_COMPLETED;
f049052b
BG
1402 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: RX message 1 of Group Key "
1403 "Handshake from " MACSTR " (ver=%d)", MAC2STR(src_addr), ver);
6fc6879b
JM
1404
1405 key_info = WPA_GET_BE16(key->key_info);
6fc6879b 1406
a14896e8 1407 if (sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN) {
e6270129
JM
1408 ret = wpa_supplicant_process_1_of_2_rsn(sm, key_data,
1409 key_data_len, key_info,
6fc6879b
JM
1410 &gd);
1411 } else {
e6270129
JM
1412 ret = wpa_supplicant_process_1_of_2_wpa(sm, key, key_data,
1413 key_data_len,
1414 key_info, ver, &gd);
6fc6879b
JM
1415 }
1416
1417 wpa_sm_set_state(sm, WPA_GROUP_HANDSHAKE);
1418
1419 if (ret)
83935317 1420 goto failed;
6fc6879b
JM
1421
1422 if (wpa_supplicant_install_gtk(sm, &gd, key->key_rsc) ||
1423 wpa_supplicant_send_2_of_2(sm, key, ver, key_info))
83935317 1424 goto failed;
6fc6879b
JM
1425
1426 if (rekey) {
0f057fb2 1427 wpa_msg(sm->ctx->msg_ctx, MSG_INFO, "WPA: Group rekeying "
6fc6879b
JM
1428 "completed with " MACSTR " [GTK=%s]",
1429 MAC2STR(sm->bssid), wpa_cipher_txt(sm->group_cipher));
1430 wpa_sm_cancel_auth_timeout(sm);
1431 wpa_sm_set_state(sm, WPA_COMPLETED);
1432 } else {
1433 wpa_supplicant_key_neg_complete(sm, sm->bssid,
1434 key_info &
1435 WPA_KEY_INFO_SECURE);
1436 }
392e68e8
SD
1437
1438 wpa_sm_set_rekey_offload(sm);
1439
83935317
JM
1440 return;
1441
1442failed:
1443 wpa_sm_deauthenticate(sm, WLAN_REASON_UNSPECIFIED);
6fc6879b
JM
1444}
1445
1446
1447static int wpa_supplicant_verify_eapol_key_mic(struct wpa_sm *sm,
1448 struct wpa_eapol_key *key,
1449 u16 ver,
1450 const u8 *buf, size_t len)
1451{
1452 u8 mic[16];
1453 int ok = 0;
1454
1455 os_memcpy(mic, key->key_mic, 16);
1456 if (sm->tptk_set) {
1457 os_memset(key->key_mic, 0, 16);
929a2ea5 1458 wpa_eapol_key_mic(sm->tptk.kck, sm->key_mgmt, ver, buf, len,
6fc6879b 1459 key->key_mic);
0d15b69f 1460 if (os_memcmp_const(mic, key->key_mic, 16) != 0) {
f049052b
BG
1461 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1462 "WPA: Invalid EAPOL-Key MIC "
1463 "when using TPTK - ignoring TPTK");
6fc6879b
JM
1464 } else {
1465 ok = 1;
1466 sm->tptk_set = 0;
1467 sm->ptk_set = 1;
1468 os_memcpy(&sm->ptk, &sm->tptk, sizeof(sm->ptk));
d2c33b91 1469 os_memset(&sm->tptk, 0, sizeof(sm->tptk));
6fc6879b
JM
1470 }
1471 }
1472
1473 if (!ok && sm->ptk_set) {
1474 os_memset(key->key_mic, 0, 16);
929a2ea5 1475 wpa_eapol_key_mic(sm->ptk.kck, sm->key_mgmt, ver, buf, len,
6fc6879b 1476 key->key_mic);
0d15b69f 1477 if (os_memcmp_const(mic, key->key_mic, 16) != 0) {
f049052b
BG
1478 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1479 "WPA: Invalid EAPOL-Key MIC - "
1480 "dropping packet");
6fc6879b
JM
1481 return -1;
1482 }
1483 ok = 1;
1484 }
1485
1486 if (!ok) {
f049052b
BG
1487 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1488 "WPA: Could not verify EAPOL-Key MIC - "
1489 "dropping packet");
6fc6879b
JM
1490 return -1;
1491 }
1492
1493 os_memcpy(sm->rx_replay_counter, key->replay_counter,
1494 WPA_REPLAY_COUNTER_LEN);
1495 sm->rx_replay_counter_set = 1;
1496 return 0;
1497}
1498
1499
1500/* Decrypt RSN EAPOL-Key key data (RC4 or AES-WRAP) */
1501static int wpa_supplicant_decrypt_key_data(struct wpa_sm *sm,
e6270129
JM
1502 struct wpa_eapol_key *key, u16 ver,
1503 u8 *key_data, size_t *key_data_len)
6fc6879b 1504{
6fc6879b 1505 wpa_hexdump(MSG_DEBUG, "RSN: encrypted key data",
e6270129 1506 key_data, *key_data_len);
6fc6879b 1507 if (!sm->ptk_set) {
f049052b
BG
1508 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1509 "WPA: PTK not available, cannot decrypt EAPOL-Key Key "
1510 "Data");
6fc6879b
JM
1511 return -1;
1512 }
1513
1514 /* Decrypt key data here so that this operation does not need
1515 * to be implemented separately for each message type. */
1516 if (ver == WPA_KEY_INFO_TYPE_HMAC_MD5_RC4) {
1517 u8 ek[32];
1518 os_memcpy(ek, key->key_iv, 16);
1519 os_memcpy(ek + 16, sm->ptk.kek, 16);
e6270129 1520 if (rc4_skip(ek, 32, 256, key_data, *key_data_len)) {
d2c33b91 1521 os_memset(ek, 0, sizeof(ek));
f049052b
BG
1522 wpa_msg(sm->ctx->msg_ctx, MSG_ERROR,
1523 "WPA: RC4 failed");
7a215dfc
JM
1524 return -1;
1525 }
d2c33b91 1526 os_memset(ek, 0, sizeof(ek));
6fc6879b 1527 } else if (ver == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES ||
df0f01d9 1528 ver == WPA_KEY_INFO_TYPE_AES_128_CMAC ||
929a2ea5
JM
1529 sm->key_mgmt == WPA_KEY_MGMT_OSEN ||
1530 wpa_key_mgmt_suite_b(sm->key_mgmt)) {
6fc6879b 1531 u8 *buf;
e6270129 1532 if (*key_data_len < 8 || *key_data_len % 8) {
f049052b 1533 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
e6270129
JM
1534 "WPA: Unsupported AES-WRAP len %u",
1535 (unsigned int) *key_data_len);
6fc6879b
JM
1536 return -1;
1537 }
e6270129
JM
1538 *key_data_len -= 8; /* AES-WRAP adds 8 bytes */
1539 buf = os_malloc(*key_data_len);
6fc6879b 1540 if (buf == NULL) {
f049052b
BG
1541 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1542 "WPA: No memory for AES-UNWRAP buffer");
6fc6879b
JM
1543 return -1;
1544 }
eefec1e4 1545 if (aes_unwrap(sm->ptk.kek, 16, *key_data_len / 8,
e6270129 1546 key_data, buf)) {
6fc6879b 1547 os_free(buf);
f049052b
BG
1548 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1549 "WPA: AES unwrap failed - "
1550 "could not decrypt EAPOL-Key key data");
6fc6879b
JM
1551 return -1;
1552 }
e6270129 1553 os_memcpy(key_data, buf, *key_data_len);
6fc6879b 1554 os_free(buf);
e6270129 1555 WPA_PUT_BE16(key->key_data_length, *key_data_len);
6fc6879b 1556 } else {
f049052b
BG
1557 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1558 "WPA: Unsupported key_info type %d", ver);
6fc6879b
JM
1559 return -1;
1560 }
1561 wpa_hexdump_key(MSG_DEBUG, "WPA: decrypted EAPOL-Key key data",
e6270129 1562 key_data, *key_data_len);
6fc6879b
JM
1563 return 0;
1564}
1565
1566
1567/**
1568 * wpa_sm_aborted_cached - Notify WPA that PMKSA caching was aborted
1569 * @sm: Pointer to WPA state machine data from wpa_sm_init()
1570 */
1571void wpa_sm_aborted_cached(struct wpa_sm *sm)
1572{
1573 if (sm && sm->cur_pmksa) {
f049052b
BG
1574 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1575 "RSN: Cancelling PMKSA caching attempt");
6fc6879b
JM
1576 sm->cur_pmksa = NULL;
1577 }
1578}
1579
1580
f049052b
BG
1581static void wpa_eapol_key_dump(struct wpa_sm *sm,
1582 const struct wpa_eapol_key *key)
6fc6879b
JM
1583{
1584#ifndef CONFIG_NO_STDOUT_DEBUG
1585 u16 key_info = WPA_GET_BE16(key->key_info);
1586
f049052b
BG
1587 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, " EAPOL-Key type=%d", key->type);
1588 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1589 " key_info 0x%x (ver=%d keyidx=%d rsvd=%d %s%s%s%s%s%s%s%s)",
1590 key_info, key_info & WPA_KEY_INFO_TYPE_MASK,
1591 (key_info & WPA_KEY_INFO_KEY_INDEX_MASK) >>
1592 WPA_KEY_INFO_KEY_INDEX_SHIFT,
1593 (key_info & (BIT(13) | BIT(14) | BIT(15))) >> 13,
1594 key_info & WPA_KEY_INFO_KEY_TYPE ? "Pairwise" : "Group",
1595 key_info & WPA_KEY_INFO_INSTALL ? " Install" : "",
1596 key_info & WPA_KEY_INFO_ACK ? " Ack" : "",
1597 key_info & WPA_KEY_INFO_MIC ? " MIC" : "",
1598 key_info & WPA_KEY_INFO_SECURE ? " Secure" : "",
1599 key_info & WPA_KEY_INFO_ERROR ? " Error" : "",
1600 key_info & WPA_KEY_INFO_REQUEST ? " Request" : "",
1601 key_info & WPA_KEY_INFO_ENCR_KEY_DATA ? " Encr" : "");
1602 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1603 " key_length=%u key_data_length=%u",
1604 WPA_GET_BE16(key->key_length),
1605 WPA_GET_BE16(key->key_data_length));
6fc6879b
JM
1606 wpa_hexdump(MSG_DEBUG, " replay_counter",
1607 key->replay_counter, WPA_REPLAY_COUNTER_LEN);
1608 wpa_hexdump(MSG_DEBUG, " key_nonce", key->key_nonce, WPA_NONCE_LEN);
1609 wpa_hexdump(MSG_DEBUG, " key_iv", key->key_iv, 16);
1610 wpa_hexdump(MSG_DEBUG, " key_rsc", key->key_rsc, 8);
1611 wpa_hexdump(MSG_DEBUG, " key_id (reserved)", key->key_id, 8);
1612 wpa_hexdump(MSG_DEBUG, " key_mic", key->key_mic, 16);
1613#endif /* CONFIG_NO_STDOUT_DEBUG */
1614}
1615
1616
1617/**
1618 * wpa_sm_rx_eapol - Process received WPA EAPOL frames
1619 * @sm: Pointer to WPA state machine data from wpa_sm_init()
1620 * @src_addr: Source MAC address of the EAPOL packet
1621 * @buf: Pointer to the beginning of the EAPOL data (EAPOL header)
1622 * @len: Length of the EAPOL frame
1623 * Returns: 1 = WPA EAPOL-Key processed, 0 = not a WPA EAPOL-Key, -1 failure
1624 *
1625 * This function is called for each received EAPOL frame. Other than EAPOL-Key
1626 * frames can be skipped if filtering is done elsewhere. wpa_sm_rx_eapol() is
1627 * only processing WPA and WPA2 EAPOL-Key frames.
1628 *
1629 * The received EAPOL-Key packets are validated and valid packets are replied
1630 * to. In addition, key material (PTK, GTK) is configured at the end of a
1631 * successful key handshake.
1632 */
1633int wpa_sm_rx_eapol(struct wpa_sm *sm, const u8 *src_addr,
1634 const u8 *buf, size_t len)
1635{
e6270129 1636 size_t plen, data_len, key_data_len;
d56d7e56 1637 const struct ieee802_1x_hdr *hdr;
6fc6879b
JM
1638 struct wpa_eapol_key *key;
1639 u16 key_info, ver;
d56d7e56 1640 u8 *tmp = NULL;
6fc6879b
JM
1641 int ret = -1;
1642 struct wpa_peerkey *peerkey = NULL;
e6270129 1643 u8 *key_data;
6fc6879b
JM
1644
1645#ifdef CONFIG_IEEE80211R
1646 sm->ft_completed = 0;
1647#endif /* CONFIG_IEEE80211R */
1648
1649 if (len < sizeof(*hdr) + sizeof(*key)) {
f049052b
BG
1650 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1651 "WPA: EAPOL frame too short to be a WPA "
1652 "EAPOL-Key (len %lu, expecting at least %lu)",
1653 (unsigned long) len,
1654 (unsigned long) sizeof(*hdr) + sizeof(*key));
6fc6879b
JM
1655 return 0;
1656 }
1657
d56d7e56 1658 hdr = (const struct ieee802_1x_hdr *) buf;
6fc6879b
JM
1659 plen = be_to_host16(hdr->length);
1660 data_len = plen + sizeof(*hdr);
f049052b
BG
1661 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1662 "IEEE 802.1X RX: version=%d type=%d length=%lu",
1663 hdr->version, hdr->type, (unsigned long) plen);
6fc6879b
JM
1664
1665 if (hdr->version < EAPOL_VERSION) {
1666 /* TODO: backwards compatibility */
1667 }
1668 if (hdr->type != IEEE802_1X_TYPE_EAPOL_KEY) {
f049052b
BG
1669 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1670 "WPA: EAPOL frame (type %u) discarded, "
6fc6879b
JM
1671 "not a Key frame", hdr->type);
1672 ret = 0;
1673 goto out;
1674 }
d56d7e56 1675 wpa_hexdump(MSG_MSGDUMP, "WPA: RX EAPOL-Key", buf, len);
6fc6879b 1676 if (plen > len - sizeof(*hdr) || plen < sizeof(*key)) {
f049052b
BG
1677 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1678 "WPA: EAPOL frame payload size %lu "
1679 "invalid (frame size %lu)",
1680 (unsigned long) plen, (unsigned long) len);
6fc6879b
JM
1681 ret = 0;
1682 goto out;
1683 }
d56d7e56
JM
1684 if (data_len < len) {
1685 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1686 "WPA: ignoring %lu bytes after the IEEE 802.1X data",
1687 (unsigned long) len - data_len);
1688 }
1689
1690 /*
1691 * Make a copy of the frame since we need to modify the buffer during
1692 * MAC validation and Key Data decryption.
1693 */
1694 tmp = os_malloc(data_len);
1695 if (tmp == NULL)
1696 goto out;
1697 os_memcpy(tmp, buf, data_len);
1698 key = (struct wpa_eapol_key *) (tmp + sizeof(struct ieee802_1x_hdr));
e6270129 1699 key_data = (u8 *) (key + 1);
6fc6879b
JM
1700
1701 if (key->type != EAPOL_KEY_TYPE_WPA && key->type != EAPOL_KEY_TYPE_RSN)
1702 {
f049052b
BG
1703 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1704 "WPA: EAPOL-Key type (%d) unknown, discarded",
1705 key->type);
6fc6879b
JM
1706 ret = 0;
1707 goto out;
1708 }
f049052b 1709 wpa_eapol_key_dump(sm, key);
6fc6879b 1710
e6270129
JM
1711 key_data_len = WPA_GET_BE16(key->key_data_length);
1712 if (key_data_len > plen - sizeof(struct wpa_eapol_key)) {
d56d7e56
JM
1713 wpa_msg(sm->ctx->msg_ctx, MSG_INFO, "WPA: Invalid EAPOL-Key "
1714 "frame - key_data overflow (%u > %u)",
e6270129 1715 (unsigned int) key_data_len,
d56d7e56
JM
1716 (unsigned int) (plen - sizeof(struct wpa_eapol_key)));
1717 goto out;
6fc6879b 1718 }
d56d7e56
JM
1719
1720 eapol_sm_notify_lower_layer_success(sm->eapol, 0);
6fc6879b
JM
1721 key_info = WPA_GET_BE16(key->key_info);
1722 ver = key_info & WPA_KEY_INFO_TYPE_MASK;
1723 if (ver != WPA_KEY_INFO_TYPE_HMAC_MD5_RC4 &&
a20088e5 1724#if defined(CONFIG_IEEE80211R) || defined(CONFIG_IEEE80211W)
6fc6879b 1725 ver != WPA_KEY_INFO_TYPE_AES_128_CMAC &&
a20088e5 1726#endif /* CONFIG_IEEE80211R || CONFIG_IEEE80211W */
df0f01d9 1727 ver != WPA_KEY_INFO_TYPE_HMAC_SHA1_AES &&
929a2ea5 1728 !wpa_key_mgmt_suite_b(sm->key_mgmt) &&
df0f01d9 1729 sm->key_mgmt != WPA_KEY_MGMT_OSEN) {
f049052b
BG
1730 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1731 "WPA: Unsupported EAPOL-Key descriptor version %d",
1732 ver);
6fc6879b
JM
1733 goto out;
1734 }
1735
df0f01d9
JM
1736 if (sm->key_mgmt == WPA_KEY_MGMT_OSEN &&
1737 ver != WPA_KEY_INFO_TYPE_AKM_DEFINED) {
1738 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1739 "OSEN: Unsupported EAPOL-Key descriptor version %d",
1740 ver);
1741 goto out;
1742 }
1743
929a2ea5
JM
1744 if (wpa_key_mgmt_suite_b(sm->key_mgmt) &&
1745 ver != WPA_KEY_INFO_TYPE_AKM_DEFINED) {
1746 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1747 "RSN: Unsupported EAPOL-Key descriptor version %d (expected AKM defined = 0)",
1748 ver);
1749 goto out;
1750 }
1751
6fc6879b 1752#ifdef CONFIG_IEEE80211R
56586197 1753 if (wpa_key_mgmt_ft(sm->key_mgmt)) {
6fc6879b
JM
1754 /* IEEE 802.11r uses a new key_info type (AES-128-CMAC). */
1755 if (ver != WPA_KEY_INFO_TYPE_AES_128_CMAC) {
f049052b
BG
1756 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1757 "FT: AP did not use AES-128-CMAC");
6fc6879b
JM
1758 goto out;
1759 }
1760 } else
1761#endif /* CONFIG_IEEE80211R */
56586197
JM
1762#ifdef CONFIG_IEEE80211W
1763 if (wpa_key_mgmt_sha256(sm->key_mgmt)) {
df0f01d9 1764 if (ver != WPA_KEY_INFO_TYPE_AES_128_CMAC &&
929a2ea5
JM
1765 sm->key_mgmt != WPA_KEY_MGMT_OSEN &&
1766 !wpa_key_mgmt_suite_b(sm->key_mgmt)) {
f049052b
BG
1767 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1768 "WPA: AP did not use the "
1769 "negotiated AES-128-CMAC");
56586197
JM
1770 goto out;
1771 }
1772 } else
1773#endif /* CONFIG_IEEE80211W */
6fc6879b 1774 if (sm->pairwise_cipher == WPA_CIPHER_CCMP &&
929a2ea5 1775 !wpa_key_mgmt_suite_b(sm->key_mgmt) &&
6fc6879b 1776 ver != WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
f049052b
BG
1777 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1778 "WPA: CCMP is used, but EAPOL-Key "
1779 "descriptor version (%d) is not 2", ver);
6fc6879b
JM
1780 if (sm->group_cipher != WPA_CIPHER_CCMP &&
1781 !(key_info & WPA_KEY_INFO_KEY_TYPE)) {
1782 /* Earlier versions of IEEE 802.11i did not explicitly
1783 * require version 2 descriptor for all EAPOL-Key
1784 * packets, so allow group keys to use version 1 if
1785 * CCMP is not used for them. */
f049052b
BG
1786 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1787 "WPA: Backwards compatibility: allow invalid "
1788 "version for non-CCMP group keys");
9f6a7cdd
JM
1789 } else if (ver == WPA_KEY_INFO_TYPE_AES_128_CMAC) {
1790 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1791 "WPA: Interoperability workaround: allow incorrect (should have been HMAC-SHA1), but stronger (is AES-128-CMAC), descriptor version to be used");
6fc6879b
JM
1792 } else
1793 goto out;
801e1173 1794 } else if (sm->pairwise_cipher == WPA_CIPHER_GCMP &&
929a2ea5 1795 !wpa_key_mgmt_suite_b(sm->key_mgmt) &&
801e1173 1796 ver != WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
eb7719ff
JM
1797 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1798 "WPA: GCMP is used, but EAPOL-Key "
1799 "descriptor version (%d) is not 2", ver);
1800 goto out;
1801 }
6fc6879b
JM
1802
1803#ifdef CONFIG_PEERKEY
1804 for (peerkey = sm->peerkey; peerkey; peerkey = peerkey->next) {
1805 if (os_memcmp(peerkey->addr, src_addr, ETH_ALEN) == 0)
1806 break;
1807 }
1808
1809 if (!(key_info & WPA_KEY_INFO_SMK_MESSAGE) && peerkey) {
1810 if (!peerkey->initiator && peerkey->replay_counter_set &&
1811 os_memcmp(key->replay_counter, peerkey->replay_counter,
1812 WPA_REPLAY_COUNTER_LEN) <= 0) {
f049052b
BG
1813 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1814 "RSN: EAPOL-Key Replay Counter did not "
1815 "increase (STK) - dropping packet");
6fc6879b
JM
1816 goto out;
1817 } else if (peerkey->initiator) {
1818 u8 _tmp[WPA_REPLAY_COUNTER_LEN];
1819 os_memcpy(_tmp, key->replay_counter,
1820 WPA_REPLAY_COUNTER_LEN);
1821 inc_byte_array(_tmp, WPA_REPLAY_COUNTER_LEN);
1822 if (os_memcmp(_tmp, peerkey->replay_counter,
1823 WPA_REPLAY_COUNTER_LEN) != 0) {
f049052b
BG
1824 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1825 "RSN: EAPOL-Key Replay "
1826 "Counter did not match (STK) - "
1827 "dropping packet");
6fc6879b
JM
1828 goto out;
1829 }
1830 }
1831 }
1832
1833 if (peerkey && peerkey->initiator && (key_info & WPA_KEY_INFO_ACK)) {
f049052b
BG
1834 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1835 "RSN: Ack bit in key_info from STK peer");
6fc6879b
JM
1836 goto out;
1837 }
1838#endif /* CONFIG_PEERKEY */
1839
1840 if (!peerkey && sm->rx_replay_counter_set &&
1841 os_memcmp(key->replay_counter, sm->rx_replay_counter,
1842 WPA_REPLAY_COUNTER_LEN) <= 0) {
f049052b
BG
1843 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1844 "WPA: EAPOL-Key Replay Counter did not increase - "
1845 "dropping packet");
6fc6879b
JM
1846 goto out;
1847 }
1848
1849 if (!(key_info & (WPA_KEY_INFO_ACK | WPA_KEY_INFO_SMK_MESSAGE))
1850#ifdef CONFIG_PEERKEY
1851 && (peerkey == NULL || !peerkey->initiator)
1852#endif /* CONFIG_PEERKEY */
1853 ) {
f049052b
BG
1854 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1855 "WPA: No Ack bit in key_info");
6fc6879b
JM
1856 goto out;
1857 }
1858
1859 if (key_info & WPA_KEY_INFO_REQUEST) {
f049052b
BG
1860 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1861 "WPA: EAPOL-Key with Request bit - dropped");
6fc6879b
JM
1862 goto out;
1863 }
1864
1865 if ((key_info & WPA_KEY_INFO_MIC) && !peerkey &&
1866 wpa_supplicant_verify_eapol_key_mic(sm, key, ver, tmp, data_len))
1867 goto out;
1868
1869#ifdef CONFIG_PEERKEY
1870 if ((key_info & WPA_KEY_INFO_MIC) && peerkey &&
1871 peerkey_verify_eapol_key_mic(sm, peerkey, key, ver, tmp, data_len))
1872 goto out;
1873#endif /* CONFIG_PEERKEY */
1874
a14896e8 1875 if ((sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN) &&
6fc6879b 1876 (key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
e6270129
JM
1877 if (wpa_supplicant_decrypt_key_data(sm, key, ver, key_data,
1878 &key_data_len))
d56d7e56 1879 goto out;
6fc6879b
JM
1880 }
1881
1882 if (key_info & WPA_KEY_INFO_KEY_TYPE) {
1883 if (key_info & WPA_KEY_INFO_KEY_INDEX_MASK) {
f049052b
BG
1884 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1885 "WPA: Ignored EAPOL-Key (Pairwise) with "
1886 "non-zero key index");
6fc6879b
JM
1887 goto out;
1888 }
1889 if (peerkey) {
1890 /* PeerKey 4-Way Handshake */
f107d00c
JM
1891 peerkey_rx_eapol_4way(sm, peerkey, key, key_info, ver,
1892 key_data, key_data_len);
6fc6879b
JM
1893 } else if (key_info & WPA_KEY_INFO_MIC) {
1894 /* 3/4 4-Way Handshake */
e6270129
JM
1895 wpa_supplicant_process_3_of_4(sm, key, ver, key_data,
1896 key_data_len);
6fc6879b
JM
1897 } else {
1898 /* 1/4 4-Way Handshake */
1899 wpa_supplicant_process_1_of_4(sm, src_addr, key,
e6270129
JM
1900 ver, key_data,
1901 key_data_len);
6fc6879b
JM
1902 }
1903 } else if (key_info & WPA_KEY_INFO_SMK_MESSAGE) {
1904 /* PeerKey SMK Handshake */
e6270129 1905 peerkey_rx_eapol_smk(sm, src_addr, key, key_data_len, key_info,
6fc6879b
JM
1906 ver);
1907 } else {
1908 if (key_info & WPA_KEY_INFO_MIC) {
1909 /* 1/2 Group Key Handshake */
1910 wpa_supplicant_process_1_of_2(sm, src_addr, key,
e6270129
JM
1911 key_data, key_data_len,
1912 ver);
6fc6879b 1913 } else {
f049052b
BG
1914 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1915 "WPA: EAPOL-Key (Group) without Mic bit - "
1916 "dropped");
6fc6879b
JM
1917 }
1918 }
1919
1920 ret = 1;
1921
1922out:
1923 os_free(tmp);
1924 return ret;
1925}
1926
1927
1928#ifdef CONFIG_CTRL_IFACE
6fc6879b
JM
1929static u32 wpa_key_mgmt_suite(struct wpa_sm *sm)
1930{
1931 switch (sm->key_mgmt) {
1932 case WPA_KEY_MGMT_IEEE8021X:
a14896e8
JM
1933 return ((sm->proto == WPA_PROTO_RSN ||
1934 sm->proto == WPA_PROTO_OSEN) ?
6fc6879b
JM
1935 RSN_AUTH_KEY_MGMT_UNSPEC_802_1X :
1936 WPA_AUTH_KEY_MGMT_UNSPEC_802_1X);
1937 case WPA_KEY_MGMT_PSK:
1938 return (sm->proto == WPA_PROTO_RSN ?
1939 RSN_AUTH_KEY_MGMT_PSK_OVER_802_1X :
1940 WPA_AUTH_KEY_MGMT_PSK_OVER_802_1X);
1941#ifdef CONFIG_IEEE80211R
1942 case WPA_KEY_MGMT_FT_IEEE8021X:
1943 return RSN_AUTH_KEY_MGMT_FT_802_1X;
1944 case WPA_KEY_MGMT_FT_PSK:
1945 return RSN_AUTH_KEY_MGMT_FT_PSK;
1946#endif /* CONFIG_IEEE80211R */
56586197
JM
1947#ifdef CONFIG_IEEE80211W
1948 case WPA_KEY_MGMT_IEEE8021X_SHA256:
1949 return RSN_AUTH_KEY_MGMT_802_1X_SHA256;
1950 case WPA_KEY_MGMT_PSK_SHA256:
1951 return RSN_AUTH_KEY_MGMT_PSK_SHA256;
1952#endif /* CONFIG_IEEE80211W */
369c8d7b
JM
1953 case WPA_KEY_MGMT_CCKM:
1954 return (sm->proto == WPA_PROTO_RSN ?
1955 RSN_AUTH_KEY_MGMT_CCKM:
1956 WPA_AUTH_KEY_MGMT_CCKM);
6fc6879b
JM
1957 case WPA_KEY_MGMT_WPA_NONE:
1958 return WPA_AUTH_KEY_MGMT_NONE;
666497c8
JM
1959 case WPA_KEY_MGMT_IEEE8021X_SUITE_B:
1960 return RSN_AUTH_KEY_MGMT_802_1X_SUITE_B;
6fc6879b
JM
1961 default:
1962 return 0;
1963 }
1964}
1965
1966
6fc6879b
JM
1967#define RSN_SUITE "%02x-%02x-%02x-%d"
1968#define RSN_SUITE_ARG(s) \
1969((s) >> 24) & 0xff, ((s) >> 16) & 0xff, ((s) >> 8) & 0xff, (s) & 0xff
1970
1971/**
1972 * wpa_sm_get_mib - Dump text list of MIB entries
1973 * @sm: Pointer to WPA state machine data from wpa_sm_init()
1974 * @buf: Buffer for the list
1975 * @buflen: Length of the buffer
1976 * Returns: Number of bytes written to buffer
1977 *
1978 * This function is used fetch dot11 MIB variables.
1979 */
1980int wpa_sm_get_mib(struct wpa_sm *sm, char *buf, size_t buflen)
1981{
1982 char pmkid_txt[PMKID_LEN * 2 + 1];
1983 int rsna, ret;
1984 size_t len;
1985
1986 if (sm->cur_pmksa) {
1987 wpa_snprintf_hex(pmkid_txt, sizeof(pmkid_txt),
1988 sm->cur_pmksa->pmkid, PMKID_LEN);
1989 } else
1990 pmkid_txt[0] = '\0';
1991
56586197
JM
1992 if ((wpa_key_mgmt_wpa_psk(sm->key_mgmt) ||
1993 wpa_key_mgmt_wpa_ieee8021x(sm->key_mgmt)) &&
6fc6879b
JM
1994 sm->proto == WPA_PROTO_RSN)
1995 rsna = 1;
1996 else
1997 rsna = 0;
1998
1999 ret = os_snprintf(buf, buflen,
2000 "dot11RSNAOptionImplemented=TRUE\n"
2001 "dot11RSNAPreauthenticationImplemented=TRUE\n"
2002 "dot11RSNAEnabled=%s\n"
2003 "dot11RSNAPreauthenticationEnabled=%s\n"
2004 "dot11RSNAConfigVersion=%d\n"
2005 "dot11RSNAConfigPairwiseKeysSupported=5\n"
2006 "dot11RSNAConfigGroupCipherSize=%d\n"
2007 "dot11RSNAConfigPMKLifetime=%d\n"
2008 "dot11RSNAConfigPMKReauthThreshold=%d\n"
2009 "dot11RSNAConfigNumberOfPTKSAReplayCounters=1\n"
2010 "dot11RSNAConfigSATimeout=%d\n",
2011 rsna ? "TRUE" : "FALSE",
2012 rsna ? "TRUE" : "FALSE",
2013 RSN_VERSION,
c3550295 2014 wpa_cipher_key_len(sm->group_cipher) * 8,
6fc6879b
JM
2015 sm->dot11RSNAConfigPMKLifetime,
2016 sm->dot11RSNAConfigPMKReauthThreshold,
2017 sm->dot11RSNAConfigSATimeout);
2018 if (ret < 0 || (size_t) ret >= buflen)
2019 return 0;
2020 len = ret;
2021
2022 ret = os_snprintf(
2023 buf + len, buflen - len,
2024 "dot11RSNAAuthenticationSuiteSelected=" RSN_SUITE "\n"
2025 "dot11RSNAPairwiseCipherSelected=" RSN_SUITE "\n"
2026 "dot11RSNAGroupCipherSelected=" RSN_SUITE "\n"
2027 "dot11RSNAPMKIDUsed=%s\n"
2028 "dot11RSNAAuthenticationSuiteRequested=" RSN_SUITE "\n"
2029 "dot11RSNAPairwiseCipherRequested=" RSN_SUITE "\n"
2030 "dot11RSNAGroupCipherRequested=" RSN_SUITE "\n"
2031 "dot11RSNAConfigNumberOfGTKSAReplayCounters=0\n"
2032 "dot11RSNA4WayHandshakeFailures=%u\n",
2033 RSN_SUITE_ARG(wpa_key_mgmt_suite(sm)),
c3550295
JM
2034 RSN_SUITE_ARG(wpa_cipher_to_suite(sm->proto,
2035 sm->pairwise_cipher)),
2036 RSN_SUITE_ARG(wpa_cipher_to_suite(sm->proto,
2037 sm->group_cipher)),
6fc6879b
JM
2038 pmkid_txt,
2039 RSN_SUITE_ARG(wpa_key_mgmt_suite(sm)),
c3550295
JM
2040 RSN_SUITE_ARG(wpa_cipher_to_suite(sm->proto,
2041 sm->pairwise_cipher)),
2042 RSN_SUITE_ARG(wpa_cipher_to_suite(sm->proto,
2043 sm->group_cipher)),
6fc6879b
JM
2044 sm->dot11RSNA4WayHandshakeFailures);
2045 if (ret >= 0 && (size_t) ret < buflen)
2046 len += ret;
2047
2048 return (int) len;
2049}
2050#endif /* CONFIG_CTRL_IFACE */
2051
2052
2053static void wpa_sm_pmksa_free_cb(struct rsn_pmksa_cache_entry *entry,
6aaac006 2054 void *ctx, enum pmksa_free_reason reason)
6fc6879b
JM
2055{
2056 struct wpa_sm *sm = ctx;
6aaac006 2057 int deauth = 0;
6fc6879b 2058
6aaac006
DW
2059 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "RSN: PMKSA cache entry free_cb: "
2060 MACSTR " reason=%d", MAC2STR(entry->aa), reason);
2061
2062 if (sm->cur_pmksa == entry) {
2063 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2064 "RSN: %s current PMKSA entry",
2065 reason == PMKSA_REPLACE ? "replaced" : "removed");
2066 pmksa_cache_clear_current(sm);
2067
2068 /*
2069 * If an entry is simply being replaced, there's no need to
2070 * deauthenticate because it will be immediately re-added.
2071 * This happens when EAP authentication is completed again
2072 * (reauth or failed PMKSA caching attempt).
2073 */
2074 if (reason != PMKSA_REPLACE)
2075 deauth = 1;
2076 }
2077
2078 if (reason == PMKSA_EXPIRE &&
6fc6879b
JM
2079 (sm->pmk_len == entry->pmk_len &&
2080 os_memcmp(sm->pmk, entry->pmk, sm->pmk_len) == 0)) {
f049052b 2081 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
6aaac006
DW
2082 "RSN: deauthenticating due to expired PMK");
2083 pmksa_cache_clear_current(sm);
2084 deauth = 1;
2085 }
6fc6879b 2086
6aaac006 2087 if (deauth) {
6fc6879b
JM
2088 os_memset(sm->pmk, 0, sizeof(sm->pmk));
2089 wpa_sm_deauthenticate(sm, WLAN_REASON_UNSPECIFIED);
6fc6879b
JM
2090 }
2091}
2092
2093
2094/**
2095 * wpa_sm_init - Initialize WPA state machine
2096 * @ctx: Context pointer for callbacks; this needs to be an allocated buffer
2097 * Returns: Pointer to the allocated WPA state machine data
2098 *
2099 * This function is used to allocate a new WPA state machine and the returned
2100 * value is passed to all WPA state machine calls.
2101 */
2102struct wpa_sm * wpa_sm_init(struct wpa_sm_ctx *ctx)
2103{
2104 struct wpa_sm *sm;
2105
2106 sm = os_zalloc(sizeof(*sm));
2107 if (sm == NULL)
2108 return NULL;
c5b26e33 2109 dl_list_init(&sm->pmksa_candidates);
6fc6879b
JM
2110 sm->renew_snonce = 1;
2111 sm->ctx = ctx;
2112
2113 sm->dot11RSNAConfigPMKLifetime = 43200;
2114 sm->dot11RSNAConfigPMKReauthThreshold = 70;
2115 sm->dot11RSNAConfigSATimeout = 60;
2116
2117 sm->pmksa = pmksa_cache_init(wpa_sm_pmksa_free_cb, sm, sm);
2118 if (sm->pmksa == NULL) {
f049052b
BG
2119 wpa_msg(sm->ctx->msg_ctx, MSG_ERROR,
2120 "RSN: PMKSA cache initialization failed");
6fc6879b
JM
2121 os_free(sm);
2122 return NULL;
2123 }
2124
2125 return sm;
2126}
2127
2128
2129/**
2130 * wpa_sm_deinit - Deinitialize WPA state machine
2131 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2132 */
2133void wpa_sm_deinit(struct wpa_sm *sm)
2134{
2135 if (sm == NULL)
2136 return;
2137 pmksa_cache_deinit(sm->pmksa);
2138 eloop_cancel_timeout(wpa_sm_start_preauth, sm, NULL);
581a8cde 2139 eloop_cancel_timeout(wpa_sm_rekey_ptk, sm, NULL);
6fc6879b
JM
2140 os_free(sm->assoc_wpa_ie);
2141 os_free(sm->ap_wpa_ie);
2142 os_free(sm->ap_rsn_ie);
2143 os_free(sm->ctx);
2144 peerkey_deinit(sm);
55046414
JM
2145#ifdef CONFIG_IEEE80211R
2146 os_free(sm->assoc_resp_ies);
2147#endif /* CONFIG_IEEE80211R */
6fc6879b
JM
2148 os_free(sm);
2149}
2150
2151
2152/**
2153 * wpa_sm_notify_assoc - Notify WPA state machine about association
2154 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2155 * @bssid: The BSSID of the new association
2156 *
2157 * This function is called to let WPA state machine know that the connection
2158 * was established.
2159 */
2160void wpa_sm_notify_assoc(struct wpa_sm *sm, const u8 *bssid)
2161{
58a98fb0
JM
2162 int clear_ptk = 1;
2163
6fc6879b
JM
2164 if (sm == NULL)
2165 return;
2166
f049052b
BG
2167 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2168 "WPA: Association event - clear replay counter");
6fc6879b
JM
2169 os_memcpy(sm->bssid, bssid, ETH_ALEN);
2170 os_memset(sm->rx_replay_counter, 0, WPA_REPLAY_COUNTER_LEN);
2171 sm->rx_replay_counter_set = 0;
2172 sm->renew_snonce = 1;
2173 if (os_memcmp(sm->preauth_bssid, bssid, ETH_ALEN) == 0)
2174 rsn_preauth_deinit(sm);
2175
2176#ifdef CONFIG_IEEE80211R
58a98fb0 2177 if (wpa_ft_is_completed(sm)) {
5d5a9f00
JM
2178 /*
2179 * Clear portValid to kick EAPOL state machine to re-enter
2180 * AUTHENTICATED state to get the EAPOL port Authorized.
2181 */
2182 eapol_sm_notify_portValid(sm->eapol, FALSE);
6fc6879b
JM
2183 wpa_supplicant_key_neg_complete(sm, sm->bssid, 1);
2184
2185 /* Prepare for the next transition */
76b7981d 2186 wpa_ft_prepare_auth_request(sm, NULL);
58a98fb0
JM
2187
2188 clear_ptk = 0;
6fc6879b
JM
2189 }
2190#endif /* CONFIG_IEEE80211R */
58a98fb0
JM
2191
2192 if (clear_ptk) {
2193 /*
2194 * IEEE 802.11, 8.4.10: Delete PTK SA on (re)association if
2195 * this is not part of a Fast BSS Transition.
2196 */
f049052b 2197 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Clear old PTK");
58a98fb0 2198 sm->ptk_set = 0;
d2c33b91 2199 os_memset(&sm->ptk, 0, sizeof(sm->ptk));
58a98fb0 2200 sm->tptk_set = 0;
d2c33b91 2201 os_memset(&sm->tptk, 0, sizeof(sm->tptk));
58a98fb0 2202 }
40cf22e6
JM
2203
2204#ifdef CONFIG_TDLS
2205 wpa_tdls_assoc(sm);
2206#endif /* CONFIG_TDLS */
25ef8529
JM
2207
2208#ifdef CONFIG_P2P
2209 os_memset(sm->p2p_ip_addr, 0, sizeof(sm->p2p_ip_addr));
2210#endif /* CONFIG_P2P */
6fc6879b
JM
2211}
2212
2213
2214/**
2215 * wpa_sm_notify_disassoc - Notify WPA state machine about disassociation
2216 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2217 *
2218 * This function is called to let WPA state machine know that the connection
2219 * was lost. This will abort any existing pre-authentication session.
2220 */
2221void wpa_sm_notify_disassoc(struct wpa_sm *sm)
2222{
77327298 2223 peerkey_deinit(sm);
6fc6879b 2224 rsn_preauth_deinit(sm);
0639970d 2225 pmksa_cache_clear_current(sm);
6fc6879b
JM
2226 if (wpa_sm_get_state(sm) == WPA_4WAY_HANDSHAKE)
2227 sm->dot11RSNA4WayHandshakeFailures++;
40cf22e6
JM
2228#ifdef CONFIG_TDLS
2229 wpa_tdls_disassoc(sm);
2230#endif /* CONFIG_TDLS */
6fc6879b
JM
2231}
2232
2233
2234/**
2235 * wpa_sm_set_pmk - Set PMK
2236 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2237 * @pmk: The new PMK
2238 * @pmk_len: The length of the new PMK in bytes
bc26ac50 2239 * @bssid: AA to add into PMKSA cache or %NULL to not cache the PMK
6fc6879b
JM
2240 *
2241 * Configure the PMK for WPA state machine.
2242 */
bc26ac50
JM
2243void wpa_sm_set_pmk(struct wpa_sm *sm, const u8 *pmk, size_t pmk_len,
2244 const u8 *bssid)
6fc6879b
JM
2245{
2246 if (sm == NULL)
2247 return;
2248
2249 sm->pmk_len = pmk_len;
2250 os_memcpy(sm->pmk, pmk, pmk_len);
2251
2252#ifdef CONFIG_IEEE80211R
2253 /* Set XXKey to be PSK for FT key derivation */
2254 sm->xxkey_len = pmk_len;
2255 os_memcpy(sm->xxkey, pmk, pmk_len);
2256#endif /* CONFIG_IEEE80211R */
bc26ac50
JM
2257
2258 if (bssid) {
087a1f4e
JM
2259 pmksa_cache_add(sm->pmksa, pmk, pmk_len, NULL, 0,
2260 bssid, sm->own_addr,
bc26ac50
JM
2261 sm->network_ctx, sm->key_mgmt);
2262 }
6fc6879b
JM
2263}
2264
2265
2266/**
2267 * wpa_sm_set_pmk_from_pmksa - Set PMK based on the current PMKSA
2268 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2269 *
2270 * Take the PMK from the current PMKSA into use. If no PMKSA is active, the PMK
2271 * will be cleared.
2272 */
2273void wpa_sm_set_pmk_from_pmksa(struct wpa_sm *sm)
2274{
2275 if (sm == NULL)
2276 return;
2277
2278 if (sm->cur_pmksa) {
2279 sm->pmk_len = sm->cur_pmksa->pmk_len;
2280 os_memcpy(sm->pmk, sm->cur_pmksa->pmk, sm->pmk_len);
2281 } else {
2282 sm->pmk_len = PMK_LEN;
2283 os_memset(sm->pmk, 0, PMK_LEN);
2284 }
2285}
2286
2287
2288/**
2289 * wpa_sm_set_fast_reauth - Set fast reauthentication (EAP) enabled/disabled
2290 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2291 * @fast_reauth: Whether fast reauthentication (EAP) is allowed
2292 */
2293void wpa_sm_set_fast_reauth(struct wpa_sm *sm, int fast_reauth)
2294{
2295 if (sm)
2296 sm->fast_reauth = fast_reauth;
2297}
2298
2299
2300/**
2301 * wpa_sm_set_scard_ctx - Set context pointer for smartcard callbacks
2302 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2303 * @scard_ctx: Context pointer for smartcard related callback functions
2304 */
2305void wpa_sm_set_scard_ctx(struct wpa_sm *sm, void *scard_ctx)
2306{
2307 if (sm == NULL)
2308 return;
2309 sm->scard_ctx = scard_ctx;
2310 if (sm->preauth_eapol)
2311 eapol_sm_register_scard_ctx(sm->preauth_eapol, scard_ctx);
2312}
2313
2314
2315/**
2316 * wpa_sm_set_config - Notification of current configration change
2317 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2318 * @config: Pointer to current network configuration
2319 *
2320 * Notify WPA state machine that configuration has changed. config will be
2321 * stored as a backpointer to network configuration. This can be %NULL to clear
2322 * the stored pointed.
2323 */
2324void wpa_sm_set_config(struct wpa_sm *sm, struct rsn_supp_config *config)
2325{
2326 if (!sm)
2327 return;
2328
6fc6879b 2329 if (config) {
886a807f 2330 sm->network_ctx = config->network_ctx;
6fc6879b
JM
2331 sm->peerkey_enabled = config->peerkey_enabled;
2332 sm->allowed_pairwise_cipher = config->allowed_pairwise_cipher;
2333 sm->proactive_key_caching = config->proactive_key_caching;
2334 sm->eap_workaround = config->eap_workaround;
2335 sm->eap_conf_ctx = config->eap_conf_ctx;
2336 if (config->ssid) {
2337 os_memcpy(sm->ssid, config->ssid, config->ssid_len);
2338 sm->ssid_len = config->ssid_len;
2339 } else
2340 sm->ssid_len = 0;
581a8cde 2341 sm->wpa_ptk_rekey = config->wpa_ptk_rekey;
25ef8529 2342 sm->p2p = config->p2p;
6fc6879b 2343 } else {
886a807f 2344 sm->network_ctx = NULL;
6fc6879b
JM
2345 sm->peerkey_enabled = 0;
2346 sm->allowed_pairwise_cipher = 0;
2347 sm->proactive_key_caching = 0;
2348 sm->eap_workaround = 0;
2349 sm->eap_conf_ctx = NULL;
2350 sm->ssid_len = 0;
581a8cde 2351 sm->wpa_ptk_rekey = 0;
25ef8529 2352 sm->p2p = 0;
6fc6879b 2353 }
6fc6879b
JM
2354}
2355
2356
2357/**
2358 * wpa_sm_set_own_addr - Set own MAC address
2359 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2360 * @addr: Own MAC address
2361 */
2362void wpa_sm_set_own_addr(struct wpa_sm *sm, const u8 *addr)
2363{
2364 if (sm)
2365 os_memcpy(sm->own_addr, addr, ETH_ALEN);
2366}
2367
2368
2369/**
2370 * wpa_sm_set_ifname - Set network interface name
2371 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2372 * @ifname: Interface name
2373 * @bridge_ifname: Optional bridge interface name (for pre-auth)
2374 */
2375void wpa_sm_set_ifname(struct wpa_sm *sm, const char *ifname,
2376 const char *bridge_ifname)
2377{
2378 if (sm) {
2379 sm->ifname = ifname;
2380 sm->bridge_ifname = bridge_ifname;
2381 }
2382}
2383
2384
2385/**
2386 * wpa_sm_set_eapol - Set EAPOL state machine pointer
2387 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2388 * @eapol: Pointer to EAPOL state machine allocated with eapol_sm_init()
2389 */
2390void wpa_sm_set_eapol(struct wpa_sm *sm, struct eapol_sm *eapol)
2391{
2392 if (sm)
2393 sm->eapol = eapol;
2394}
2395
2396
2397/**
2398 * wpa_sm_set_param - Set WPA state machine parameters
2399 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2400 * @param: Parameter field
2401 * @value: Parameter value
2402 * Returns: 0 on success, -1 on failure
2403 */
2404int wpa_sm_set_param(struct wpa_sm *sm, enum wpa_sm_conf_params param,
2405 unsigned int value)
2406{
2407 int ret = 0;
2408
2409 if (sm == NULL)
2410 return -1;
2411
2412 switch (param) {
2413 case RSNA_PMK_LIFETIME:
2414 if (value > 0)
2415 sm->dot11RSNAConfigPMKLifetime = value;
2416 else
2417 ret = -1;
2418 break;
2419 case RSNA_PMK_REAUTH_THRESHOLD:
2420 if (value > 0 && value <= 100)
2421 sm->dot11RSNAConfigPMKReauthThreshold = value;
2422 else
2423 ret = -1;
2424 break;
2425 case RSNA_SA_TIMEOUT:
2426 if (value > 0)
2427 sm->dot11RSNAConfigSATimeout = value;
2428 else
2429 ret = -1;
2430 break;
2431 case WPA_PARAM_PROTO:
2432 sm->proto = value;
2433 break;
2434 case WPA_PARAM_PAIRWISE:
2435 sm->pairwise_cipher = value;
2436 break;
2437 case WPA_PARAM_GROUP:
2438 sm->group_cipher = value;
2439 break;
2440 case WPA_PARAM_KEY_MGMT:
2441 sm->key_mgmt = value;
2442 break;
2443#ifdef CONFIG_IEEE80211W
2444 case WPA_PARAM_MGMT_GROUP:
2445 sm->mgmt_group_cipher = value;
2446 break;
2447#endif /* CONFIG_IEEE80211W */
2448 case WPA_PARAM_RSN_ENABLED:
2449 sm->rsn_enabled = value;
2450 break;
e820cf95
JM
2451 case WPA_PARAM_MFP:
2452 sm->mfp = value;
2453 break;
6fc6879b
JM
2454 default:
2455 break;
2456 }
2457
2458 return ret;
2459}
2460
2461
6fc6879b
JM
2462/**
2463 * wpa_sm_get_status - Get WPA state machine
2464 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2465 * @buf: Buffer for status information
2466 * @buflen: Maximum buffer length
2467 * @verbose: Whether to include verbose status information
2468 * Returns: Number of bytes written to buf.
2469 *
2470 * Query WPA state machine for status information. This function fills in
2471 * a text area with current status information. If the buffer (buf) is not
2472 * large enough, status information will be truncated to fit the buffer.
2473 */
2474int wpa_sm_get_status(struct wpa_sm *sm, char *buf, size_t buflen,
2475 int verbose)
2476{
2477 char *pos = buf, *end = buf + buflen;
2478 int ret;
2479
2480 ret = os_snprintf(pos, end - pos,
2481 "pairwise_cipher=%s\n"
2482 "group_cipher=%s\n"
2483 "key_mgmt=%s\n",
2484 wpa_cipher_txt(sm->pairwise_cipher),
2485 wpa_cipher_txt(sm->group_cipher),
2486 wpa_key_mgmt_txt(sm->key_mgmt, sm->proto));
2487 if (ret < 0 || ret >= end - pos)
2488 return pos - buf;
2489 pos += ret;
13e1d2e2
JM
2490
2491 if (sm->mfp != NO_MGMT_FRAME_PROTECTION && sm->ap_rsn_ie) {
2492 struct wpa_ie_data rsn;
2493 if (wpa_parse_wpa_ie_rsn(sm->ap_rsn_ie, sm->ap_rsn_ie_len, &rsn)
2494 >= 0 &&
2495 rsn.capabilities & (WPA_CAPABILITY_MFPR |
2496 WPA_CAPABILITY_MFPC)) {
2497 ret = os_snprintf(pos, end - pos, "pmf=%d\n",
2498 (rsn.capabilities &
2499 WPA_CAPABILITY_MFPR) ? 2 : 1);
2500 if (ret < 0 || ret >= end - pos)
2501 return pos - buf;
2502 pos += ret;
2503 }
2504 }
2505
6fc6879b
JM
2506 return pos - buf;
2507}
2508
2509
ae8535b6
JM
2510int wpa_sm_pmf_enabled(struct wpa_sm *sm)
2511{
2512 struct wpa_ie_data rsn;
2513
2514 if (sm->mfp == NO_MGMT_FRAME_PROTECTION || !sm->ap_rsn_ie)
2515 return 0;
2516
2517 if (wpa_parse_wpa_ie_rsn(sm->ap_rsn_ie, sm->ap_rsn_ie_len, &rsn) >= 0 &&
2518 rsn.capabilities & (WPA_CAPABILITY_MFPR | WPA_CAPABILITY_MFPC))
2519 return 1;
2520
2521 return 0;
2522}
2523
2524
6fc6879b
JM
2525/**
2526 * wpa_sm_set_assoc_wpa_ie_default - Generate own WPA/RSN IE from configuration
2527 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2528 * @wpa_ie: Pointer to buffer for WPA/RSN IE
2529 * @wpa_ie_len: Pointer to the length of the wpa_ie buffer
2530 * Returns: 0 on success, -1 on failure
2531 */
2532int wpa_sm_set_assoc_wpa_ie_default(struct wpa_sm *sm, u8 *wpa_ie,
2533 size_t *wpa_ie_len)
2534{
2535 int res;
2536
2537 if (sm == NULL)
2538 return -1;
2539
2540 res = wpa_gen_wpa_ie(sm, wpa_ie, *wpa_ie_len);
2541 if (res < 0)
2542 return -1;
2543 *wpa_ie_len = res;
2544
2545 wpa_hexdump(MSG_DEBUG, "WPA: Set own WPA IE default",
2546 wpa_ie, *wpa_ie_len);
2547
2548 if (sm->assoc_wpa_ie == NULL) {
2549 /*
2550 * Make a copy of the WPA/RSN IE so that 4-Way Handshake gets
2551 * the correct version of the IE even if PMKSA caching is
2552 * aborted (which would remove PMKID from IE generation).
2553 */
2554 sm->assoc_wpa_ie = os_malloc(*wpa_ie_len);
2555 if (sm->assoc_wpa_ie == NULL)
2556 return -1;
2557
2558 os_memcpy(sm->assoc_wpa_ie, wpa_ie, *wpa_ie_len);
2559 sm->assoc_wpa_ie_len = *wpa_ie_len;
2560 }
2561
2562 return 0;
2563}
2564
2565
2566/**
2567 * wpa_sm_set_assoc_wpa_ie - Set own WPA/RSN IE from (Re)AssocReq
2568 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2569 * @ie: Pointer to IE data (starting from id)
2570 * @len: IE length
2571 * Returns: 0 on success, -1 on failure
2572 *
2573 * Inform WPA state machine about the WPA/RSN IE used in (Re)Association
2574 * Request frame. The IE will be used to override the default value generated
2575 * with wpa_sm_set_assoc_wpa_ie_default().
2576 */
2577int wpa_sm_set_assoc_wpa_ie(struct wpa_sm *sm, const u8 *ie, size_t len)
2578{
2579 if (sm == NULL)
2580 return -1;
2581
2582 os_free(sm->assoc_wpa_ie);
2583 if (ie == NULL || len == 0) {
f049052b
BG
2584 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2585 "WPA: clearing own WPA/RSN IE");
6fc6879b
JM
2586 sm->assoc_wpa_ie = NULL;
2587 sm->assoc_wpa_ie_len = 0;
2588 } else {
2589 wpa_hexdump(MSG_DEBUG, "WPA: set own WPA/RSN IE", ie, len);
2590 sm->assoc_wpa_ie = os_malloc(len);
2591 if (sm->assoc_wpa_ie == NULL)
2592 return -1;
2593
2594 os_memcpy(sm->assoc_wpa_ie, ie, len);
2595 sm->assoc_wpa_ie_len = len;
2596 }
2597
2598 return 0;
2599}
2600
2601
2602/**
2603 * wpa_sm_set_ap_wpa_ie - Set AP WPA IE from Beacon/ProbeResp
2604 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2605 * @ie: Pointer to IE data (starting from id)
2606 * @len: IE length
2607 * Returns: 0 on success, -1 on failure
2608 *
2609 * Inform WPA state machine about the WPA IE used in Beacon / Probe Response
2610 * frame.
2611 */
2612int wpa_sm_set_ap_wpa_ie(struct wpa_sm *sm, const u8 *ie, size_t len)
2613{
2614 if (sm == NULL)
2615 return -1;
2616
2617 os_free(sm->ap_wpa_ie);
2618 if (ie == NULL || len == 0) {
f049052b
BG
2619 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2620 "WPA: clearing AP WPA IE");
6fc6879b
JM
2621 sm->ap_wpa_ie = NULL;
2622 sm->ap_wpa_ie_len = 0;
2623 } else {
2624 wpa_hexdump(MSG_DEBUG, "WPA: set AP WPA IE", ie, len);
2625 sm->ap_wpa_ie = os_malloc(len);
2626 if (sm->ap_wpa_ie == NULL)
2627 return -1;
2628
2629 os_memcpy(sm->ap_wpa_ie, ie, len);
2630 sm->ap_wpa_ie_len = len;
2631 }
2632
2633 return 0;
2634}
2635
2636
2637/**
2638 * wpa_sm_set_ap_rsn_ie - Set AP RSN IE from Beacon/ProbeResp
2639 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2640 * @ie: Pointer to IE data (starting from id)
2641 * @len: IE length
2642 * Returns: 0 on success, -1 on failure
2643 *
2644 * Inform WPA state machine about the RSN IE used in Beacon / Probe Response
2645 * frame.
2646 */
2647int wpa_sm_set_ap_rsn_ie(struct wpa_sm *sm, const u8 *ie, size_t len)
2648{
2649 if (sm == NULL)
2650 return -1;
2651
2652 os_free(sm->ap_rsn_ie);
2653 if (ie == NULL || len == 0) {
f049052b
BG
2654 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2655 "WPA: clearing AP RSN IE");
6fc6879b
JM
2656 sm->ap_rsn_ie = NULL;
2657 sm->ap_rsn_ie_len = 0;
2658 } else {
2659 wpa_hexdump(MSG_DEBUG, "WPA: set AP RSN IE", ie, len);
2660 sm->ap_rsn_ie = os_malloc(len);
2661 if (sm->ap_rsn_ie == NULL)
2662 return -1;
2663
2664 os_memcpy(sm->ap_rsn_ie, ie, len);
2665 sm->ap_rsn_ie_len = len;
2666 }
2667
2668 return 0;
2669}
2670
2671
2672/**
2673 * wpa_sm_parse_own_wpa_ie - Parse own WPA/RSN IE
2674 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2675 * @data: Pointer to data area for parsing results
2676 * Returns: 0 on success, -1 if IE is not known, or -2 on parsing failure
2677 *
2678 * Parse the contents of the own WPA or RSN IE from (Re)AssocReq and write the
2679 * parsed data into data.
2680 */
2681int wpa_sm_parse_own_wpa_ie(struct wpa_sm *sm, struct wpa_ie_data *data)
2682{
2a522e71
JM
2683 if (sm == NULL)
2684 return -1;
2685
2686 if (sm->assoc_wpa_ie == NULL) {
f049052b
BG
2687 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2688 "WPA: No WPA/RSN IE available from association info");
6fc6879b
JM
2689 return -1;
2690 }
2691 if (wpa_parse_wpa_ie(sm->assoc_wpa_ie, sm->assoc_wpa_ie_len, data))
2692 return -2;
2693 return 0;
2694}
540264a7
JM
2695
2696
2697int wpa_sm_pmksa_cache_list(struct wpa_sm *sm, char *buf, size_t len)
2698{
540264a7 2699 return pmksa_cache_list(sm->pmksa, buf, len);
540264a7 2700}
32d5295f
JM
2701
2702
9ff4de6d 2703#ifdef CONFIG_TESTING_OPTIONS
32d5295f
JM
2704void wpa_sm_drop_sa(struct wpa_sm *sm)
2705{
f049052b 2706 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Clear old PMK and PTK");
32d5295f
JM
2707 sm->ptk_set = 0;
2708 sm->tptk_set = 0;
2709 os_memset(sm->pmk, 0, sizeof(sm->pmk));
2710 os_memset(&sm->ptk, 0, sizeof(sm->ptk));
2711 os_memset(&sm->tptk, 0, sizeof(sm->tptk));
2712}
9ff4de6d 2713#endif /* CONFIG_TESTING_OPTIONS */
0d7b4409
JM
2714
2715
2716int wpa_sm_has_ptk(struct wpa_sm *sm)
2717{
2718 if (sm == NULL)
2719 return 0;
2720 return sm->ptk_set;
2721}
b14a210c
JB
2722
2723
2724void wpa_sm_update_replay_ctr(struct wpa_sm *sm, const u8 *replay_ctr)
2725{
2726 os_memcpy(sm->rx_replay_counter, replay_ctr, WPA_REPLAY_COUNTER_LEN);
2727}
d8a790b9
JM
2728
2729
2730void wpa_sm_pmksa_cache_flush(struct wpa_sm *sm, void *network_ctx)
2731{
4033935d 2732 pmksa_cache_flush(sm->pmksa, network_ctx, NULL, 0);
d8a790b9 2733}
75cad1a0
XC
2734
2735
ad3872a3 2736#ifdef CONFIG_WNM
75cad1a0
XC
2737int wpa_wnmsleep_install_key(struct wpa_sm *sm, u8 subelem_id, u8 *buf)
2738{
75cad1a0
XC
2739 u16 keyinfo;
2740 u8 keylen; /* plaintext key len */
75cad1a0
XC
2741 u8 *key_rsc;
2742
75cad1a0 2743 if (subelem_id == WNM_SLEEP_SUBELEM_GTK) {
d2c33b91
JM
2744 struct wpa_gtk_data gd;
2745
2746 os_memset(&gd, 0, sizeof(gd));
2747 keylen = wpa_cipher_key_len(sm->group_cipher);
2748 gd.key_rsc_len = wpa_cipher_rsc_len(sm->group_cipher);
2749 gd.alg = wpa_cipher_to_alg(sm->group_cipher);
2750 if (gd.alg == WPA_ALG_NONE) {
2751 wpa_printf(MSG_DEBUG, "Unsupported group cipher suite");
2752 return -1;
2753 }
2754
75cad1a0 2755 key_rsc = buf + 5;
68db9ab0 2756 keyinfo = WPA_GET_LE16(buf + 2);
75cad1a0
XC
2757 gd.gtk_len = keylen;
2758 if (gd.gtk_len != buf[4]) {
2759 wpa_printf(MSG_DEBUG, "GTK len mismatch len %d vs %d",
2760 gd.gtk_len, buf[4]);
2761 return -1;
2762 }
2763 gd.keyidx = keyinfo & 0x03; /* B0 - B1 */
2764 gd.tx = wpa_supplicant_gtk_tx_bit_workaround(
2765 sm, !!(keyinfo & WPA_KEY_INFO_TXRX));
2766
68db9ab0 2767 os_memcpy(gd.gtk, buf + 13, gd.gtk_len);
75cad1a0
XC
2768
2769 wpa_hexdump_key(MSG_DEBUG, "Install GTK (WNM SLEEP)",
2770 gd.gtk, gd.gtk_len);
2771 if (wpa_supplicant_install_gtk(sm, &gd, key_rsc)) {
d2c33b91 2772 os_memset(&gd, 0, sizeof(gd));
75cad1a0
XC
2773 wpa_printf(MSG_DEBUG, "Failed to install the GTK in "
2774 "WNM mode");
2775 return -1;
2776 }
d2c33b91 2777 os_memset(&gd, 0, sizeof(gd));
75cad1a0
XC
2778#ifdef CONFIG_IEEE80211W
2779 } else if (subelem_id == WNM_SLEEP_SUBELEM_IGTK) {
d2c33b91
JM
2780 struct wpa_igtk_kde igd;
2781 u16 keyidx;
2782
2783 os_memset(&igd, 0, sizeof(igd));
8dd9f9cd 2784 keylen = wpa_cipher_key_len(sm->mgmt_group_cipher);
75cad1a0
XC
2785 os_memcpy(igd.keyid, buf + 2, 2);
2786 os_memcpy(igd.pn, buf + 4, 6);
2787
2788 keyidx = WPA_GET_LE16(igd.keyid);
8dd9f9cd 2789 os_memcpy(igd.igtk, buf + 10, keylen);
75cad1a0
XC
2790
2791 wpa_hexdump_key(MSG_DEBUG, "Install IGTK (WNM SLEEP)",
8dd9f9cd
JM
2792 igd.igtk, keylen);
2793 if (wpa_sm_set_key(sm, wpa_cipher_to_alg(sm->mgmt_group_cipher),
2794 broadcast_ether_addr,
75cad1a0 2795 keyidx, 0, igd.pn, sizeof(igd.pn),
8dd9f9cd 2796 igd.igtk, keylen) < 0) {
75cad1a0
XC
2797 wpa_printf(MSG_DEBUG, "Failed to install the IGTK in "
2798 "WNM mode");
d2c33b91 2799 os_memset(&igd, 0, sizeof(igd));
75cad1a0
XC
2800 return -1;
2801 }
d2c33b91 2802 os_memset(&igd, 0, sizeof(igd));
75cad1a0
XC
2803#endif /* CONFIG_IEEE80211W */
2804 } else {
2805 wpa_printf(MSG_DEBUG, "Unknown element id");
2806 return -1;
2807 }
2808
2809 return 0;
2810}
ad3872a3 2811#endif /* CONFIG_WNM */
db76aa64
JM
2812
2813
2814#ifdef CONFIG_PEERKEY
2815int wpa_sm_rx_eapol_peerkey(struct wpa_sm *sm, const u8 *src_addr,
2816 const u8 *buf, size_t len)
2817{
2818 struct wpa_peerkey *peerkey;
2819
2820 for (peerkey = sm->peerkey; peerkey; peerkey = peerkey->next) {
2821 if (os_memcmp(peerkey->addr, src_addr, ETH_ALEN) == 0)
2822 break;
2823 }
2824
2825 if (!peerkey)
2826 return 0;
2827
2828 wpa_sm_rx_eapol(sm, src_addr, buf, len);
2829
2830 return 1;
2831}
2832#endif /* CONFIG_PEERKEY */
25ef8529
JM
2833
2834
2835#ifdef CONFIG_P2P
2836
2837int wpa_sm_get_p2p_ip_addr(struct wpa_sm *sm, u8 *buf)
2838{
2839 if (sm == NULL || WPA_GET_BE32(sm->p2p_ip_addr) == 0)
2840 return -1;
2841 os_memcpy(buf, sm->p2p_ip_addr, 3 * 4);
2842 return 0;
2843}
2844
2845#endif /* CONFIG_P2P */
b41f2684
CL
2846
2847
2848void wpa_sm_set_rx_replay_ctr(struct wpa_sm *sm, const u8 *rx_replay_counter)
2849{
2850 if (rx_replay_counter == NULL)
2851 return;
2852
2853 os_memcpy(sm->rx_replay_counter, rx_replay_counter,
2854 WPA_REPLAY_COUNTER_LEN);
2855 sm->rx_replay_counter_set = 1;
2856 wpa_printf(MSG_DEBUG, "Updated key replay counter");
2857}
2858
2859
2860void wpa_sm_set_ptk_kck_kek(struct wpa_sm *sm, const u8 *ptk_kck,
2861 const u8 *ptk_kek)
2862{
2863 if (ptk_kck) {
2864 os_memcpy(sm->ptk.kck, ptk_kck, 16);
2865 wpa_printf(MSG_DEBUG, "Updated PTK KCK");
2866 }
2867 if (ptk_kek) {
2868 os_memcpy(sm->ptk.kek, ptk_kek, 16);
2869 wpa_printf(MSG_DEBUG, "Updated PTK KEK");
2870 }
2871 sm->ptk_set = 1;
2872}