blob: 8ea54bbab5cccdf0f0a04156650a4fa0ca1b581c [file] [log] [blame]
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001/*
2 * WPA Supplicant - WPA state machine and EAPOL-Key processing
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003 * Copyright (c) 2003-2012, Jouni Malinen <j@w1.fi>
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004 *
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007 */
8
9#include "includes.h"
10
11#include "common.h"
12#include "crypto/aes_wrap.h"
13#include "crypto/crypto.h"
14#include "crypto/random.h"
15#include "common/ieee802_11_defs.h"
16#include "eapol_supp/eapol_supp_sm.h"
17#include "wpa.h"
18#include "eloop.h"
19#include "preauth.h"
20#include "pmksa_cache.h"
21#include "wpa_i.h"
22#include "wpa_ie.h"
23#include "peerkey.h"
24
25
26/**
27 * wpa_eapol_key_send - Send WPA/RSN EAPOL-Key message
28 * @sm: Pointer to WPA state machine data from wpa_sm_init()
29 * @kck: Key Confirmation Key (KCK, part of PTK)
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{
41 if (is_zero_ether_addr(dest) && is_zero_ether_addr(sm->bssid)) {
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) {
47 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
48 "WPA: Failed to read BSSID for "
49 "EAPOL-Key destination address");
50 } else {
51 dest = sm->bssid;
52 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
53 "WPA: Use BSSID (" MACSTR
54 ") as the destination for EAPOL-Key",
55 MAC2STR(dest));
56 }
57 }
58 if (key_mic &&
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -080059 wpa_eapol_key_mic(kck, sm->key_mgmt, ver, msg, msg_len, key_mic)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070060 wpa_msg(sm->ctx->msg_ctx, MSG_ERROR,
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -080061 "WPA: Failed to generate EAPOL-Key version %d key_mgmt 0x%x MIC",
62 ver, sm->key_mgmt);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070063 goto out;
64 }
65 wpa_hexdump_key(MSG_DEBUG, "WPA: KCK", kck, 16);
66 wpa_hexdump(MSG_DEBUG, "WPA: Derived Key MIC", key_mic, 16);
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);
70out:
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
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
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -080092 if (sm->key_mgmt == WPA_KEY_MGMT_OSEN ||
93 wpa_key_mgmt_suite_b(sm->key_mgmt))
Dmitry Shmidtf21452a2014-02-26 10:55:25 -080094 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))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070097 ver = WPA_KEY_INFO_TYPE_AES_128_CMAC;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070098 else if (sm->pairwise_cipher != WPA_CIPHER_TKIP)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070099 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) {
104 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
105 "Failed to read BSSID for EAPOL-Key request");
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
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800114 reply->type = (sm->proto == WPA_PROTO_RSN ||
115 sm->proto == WPA_PROTO_OSEN) ?
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700116 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
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);
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
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800142static 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
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700160static 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. */
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800171 sm->cur_pmksa = pmksa_cache_get(sm->pmksa, src_addr, pmkid,
172 NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700173 if (sm->cur_pmksa) {
174 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
175 "RSN: found matching PMKID from PMKSA cache");
176 } else {
177 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
178 "RSN: no matching PMKID found");
179 abort_cached = 1;
180 }
181 }
182
183 if (pmkid && sm->cur_pmksa &&
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700184 os_memcmp_const(pmkid, sm->cur_pmksa->pmkid, PMKID_LEN) == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700185 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 */
193 } else if (wpa_key_mgmt_wpa_ieee8021x(sm->key_mgmt) && sm->eapol) {
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) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700216 struct rsn_pmksa_cache_entry *sa = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700217 wpa_hexdump_key(MSG_DEBUG, "WPA: PMK from EAPOL state "
218 "machines", sm->pmk, pmk_len);
219 sm->pmk_len = pmk_len;
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800220 wpa_supplicant_key_mgmt_set_pmk(sm);
Dmitry Shmidtc55524a2011-07-07 11:18:38 -0700221 if (sm->proto == WPA_PROTO_RSN &&
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800222 !wpa_key_mgmt_suite_b(sm->key_mgmt) &&
Dmitry Shmidtc55524a2011-07-07 11:18:38 -0700223 !wpa_key_mgmt_ft(sm->key_mgmt)) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700224 sa = pmksa_cache_add(sm->pmksa,
225 sm->pmk, pmk_len,
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800226 NULL, 0,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700227 src_addr, sm->own_addr,
228 sm->network_ctx,
229 sm->key_mgmt);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700230 }
231 if (!sm->cur_pmksa && pmkid &&
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800232 pmksa_cache_get(sm->pmksa, src_addr, pmkid, NULL))
233 {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700234 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
235 "RSN: the new PMK matches with the "
236 "PMKID");
237 abort_cached = 0;
238 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700239
240 if (!sm->cur_pmksa)
241 sm->cur_pmksa = sa;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700242 } else {
243 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
244 "WPA: Failed to get master session key from "
245 "EAPOL state machines - key handshake "
246 "aborted");
247 if (sm->cur_pmksa) {
248 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
249 "RSN: Cancelled PMKSA caching "
250 "attempt");
251 sm->cur_pmksa = NULL;
252 abort_cached = 1;
253 } else if (!abort_cached) {
254 return -1;
255 }
256 }
257 }
258
Dmitry Shmidtc55524a2011-07-07 11:18:38 -0700259 if (abort_cached && wpa_key_mgmt_wpa_ieee8021x(sm->key_mgmt) &&
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800260 !wpa_key_mgmt_suite_b(sm->key_mgmt) &&
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800261 !wpa_key_mgmt_ft(sm->key_mgmt) && sm->key_mgmt != WPA_KEY_MGMT_OSEN)
262 {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700263 /* Send EAPOL-Start to trigger full EAP authentication. */
264 u8 *buf;
265 size_t buflen;
266
267 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
268 "RSN: no PMKSA entry found - trigger "
269 "full EAP authentication");
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);
276 return -2;
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;
307 u8 *rsn_ie_buf = NULL;
308
309 if (wpa_ie == NULL) {
310 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, "WPA: No wpa_ie set - "
311 "cannot generate msg 2/4");
312 return -1;
313 }
314
315#ifdef CONFIG_IEEE80211R
316 if (wpa_key_mgmt_ft(sm->key_mgmt)) {
317 int res;
318
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);
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 }
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 }
341
342 wpa_ie = rsn_ie_buf;
343 }
344#endif /* CONFIG_IEEE80211R */
345
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);
351 if (rbuf == NULL) {
352 os_free(rsn_ie_buf);
353 return -1;
354 }
355
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800356 reply->type = (sm->proto == WPA_PROTO_RSN ||
357 sm->proto == WPA_PROTO_OSEN) ?
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700358 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);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800361 if (sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700362 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);
367 wpa_hexdump(MSG_DEBUG, "WPA: Replay Counter", reply->replay_counter,
368 WPA_REPLAY_COUNTER_LEN);
369
370 WPA_PUT_BE16(reply->key_data_length, wpa_ie_len);
371 os_memcpy(reply + 1, wpa_ie, wpa_ie_len);
372 os_free(rsn_ie_buf);
373
374 os_memcpy(reply->key_nonce, nonce, WPA_NONCE_LEN);
375
376 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Sending EAPOL-Key 2/4");
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{
Dmitry Shmidt98660862014-03-11 17:26:21 -0700388 size_t ptk_len = wpa_cipher_key_len(sm->pairwise_cipher) + 32;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700389#ifdef CONFIG_IEEE80211R
390 if (wpa_key_mgmt_ft(sm->key_mgmt))
391 return wpa_derive_ptk_ft(sm, src_addr, key, ptk, ptk_len);
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,
396 (u8 *) ptk, ptk_len,
397 wpa_key_mgmt_sha256(sm->key_mgmt));
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,
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700405 u16 ver, const u8 *key_data,
406 size_t key_data_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700407{
408 struct wpa_eapol_ie_parse ie;
409 struct wpa_ptk *ptk;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700410 int res;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800411 u8 *kde, *kde_buf = NULL;
412 size_t kde_len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700413
414 if (wpa_sm_get_network_ctx(sm) == NULL) {
415 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, "WPA: No SSID info "
416 "found (msg 1 of 4)");
417 return;
418 }
419
420 wpa_sm_set_state(sm, WPA_4WAY_HANDSHAKE);
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);
423
424 os_memset(&ie, 0, sizeof(ie));
425
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800426 if (sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700427 /* RSN: msg 1/4 should contain PMKID for the selected PMK */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700428 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)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800431 goto failed;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700432 if (ie.pmkid) {
433 wpa_hexdump(MSG_DEBUG, "RSN: PMKID from "
434 "Authenticator", ie.pmkid, PMKID_LEN);
435 }
436 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700437
438 res = wpa_supplicant_get_pmk(sm, src_addr, ie.pmkid);
439 if (res == -2) {
440 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "RSN: Do not reply to "
441 "msg 1/4 - requesting full EAP authentication");
442 return;
443 }
444 if (res)
445 goto failed;
446
447 if (sm->renew_snonce) {
448 if (random_get_bytes(sm->snonce, WPA_NONCE_LEN)) {
449 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
450 "WPA: Failed to get random data for SNonce");
451 goto failed;
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);
Dmitry Shmidt98660862014-03-11 17:26:21 -0700462 if (sm->pairwise_cipher == WPA_CIPHER_TKIP) {
Dmitry Shmidt61593f02014-04-21 16:27:35 -0700463 u8 buf[8];
Dmitry Shmidt98660862014-03-11 17:26:21 -0700464 /* 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);
Dmitry Shmidt61593f02014-04-21 16:27:35 -0700468 os_memset(buf, 0, sizeof(buf));
Dmitry Shmidt98660862014-03-11 17:26:21 -0700469 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700470 sm->tptk_set = 1;
471
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800472 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
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700495 if (wpa_supplicant_send_2_of_4(sm, sm->bssid, key, ver, sm->snonce,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800496 kde, kde_len, ptk))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700497 goto failed;
498
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800499 os_free(kde_buf);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700500 os_memcpy(sm->anonce, key->key_nonce, WPA_NONCE_LEN);
501 return;
502
503failed:
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800504 os_free(kde_buf);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700505 wpa_sm_deauthenticate(sm, WLAN_REASON_UNSPECIFIED);
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{
519 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
520 "WPA: Key negotiation completed with "
521 MACSTR " [PTK=%s GTK=%s]", MAC2STR(addr),
522 wpa_cipher_txt(sm->pairwise_cipher),
523 wpa_cipher_txt(sm->group_cipher));
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);
532 if (wpa_key_mgmt_wpa_psk(sm->key_mgmt))
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
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800538 * likelihood of the first preauth EAPOL-Start frame getting to
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700539 * 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) {
545 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
546 "RSN: Authenticator accepted "
547 "opportunistic PMKSA entry - marking it valid");
548 sm->cur_pmksa->opportunistic = 0;
549 }
550
551#ifdef CONFIG_IEEE80211R
552 if (wpa_key_mgmt_ft(sm->key_mgmt)) {
553 /* Prepare for the next transition */
554 wpa_ft_prepare_auth_request(sm, NULL);
555 }
556#endif /* CONFIG_IEEE80211R */
557}
558
559
560static void wpa_sm_rekey_ptk(void *eloop_ctx, void *timeout_ctx)
561{
562 struct wpa_sm *sm = eloop_ctx;
563 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Request PTK rekeying");
564 wpa_sm_key_request(sm, 0, 1);
565}
566
567
568static int wpa_supplicant_install_ptk(struct wpa_sm *sm,
569 const struct wpa_eapol_key *key)
570{
571 int keylen, rsclen;
572 enum wpa_alg alg;
573 const u8 *key_rsc;
574 u8 null_rsc[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
575
576 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
577 "WPA: Installing PTK to the driver");
578
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700579 if (sm->pairwise_cipher == WPA_CIPHER_NONE) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700580 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Pairwise Cipher "
581 "Suite: NONE - do not use pairwise keys");
582 return 0;
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700583 }
584
585 if (!wpa_cipher_valid_pairwise(sm->pairwise_cipher)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700586 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
587 "WPA: Unsupported pairwise cipher %d",
588 sm->pairwise_cipher);
589 return -1;
590 }
591
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700592 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
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800596 if (sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700597 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) {
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));
609 return -1;
610 }
611
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -0800612 /* TK is not needed anymore in supplicant */
613 os_memset(sm->ptk.tk1, 0, sizeof(sm->ptk.tk1));
614 os_memset(sm->ptk.u.tk2, 0, sizeof(sm->ptk.u.tk2));
615
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700616 if (sm->wpa_ptk_rekey) {
617 eloop_cancel_timeout(wpa_sm_rekey_ptk, sm, NULL);
618 eloop_register_timeout(sm->wpa_ptk_rekey, 0, wpa_sm_rekey_ptk,
619 sm, NULL);
620 }
621
622 return 0;
623}
624
625
626static int wpa_supplicant_check_group_cipher(struct wpa_sm *sm,
627 int group_cipher,
628 int keylen, int maxkeylen,
629 int *key_rsc_len,
630 enum wpa_alg *alg)
631{
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700632 int klen;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700633
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700634 *alg = wpa_cipher_to_alg(group_cipher);
635 if (*alg == WPA_ALG_NONE) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700636 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
637 "WPA: Unsupported Group Cipher %d",
638 group_cipher);
639 return -1;
640 }
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700641 *key_rsc_len = wpa_cipher_rsc_len(group_cipher);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700642
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700643 klen = wpa_cipher_key_len(group_cipher);
644 if (keylen != klen || maxkeylen < klen) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700645 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
646 "WPA: Unsupported %s Group Cipher key length %d (%d)",
647 wpa_cipher_txt(group_cipher), keylen, maxkeylen);
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700648 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700649 }
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700650 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700651}
652
653
654struct wpa_gtk_data {
655 enum wpa_alg alg;
656 int tx, key_rsc_len, keyidx;
657 u8 gtk[32];
658 int gtk_len;
659};
660
661
662static int wpa_supplicant_install_gtk(struct wpa_sm *sm,
663 const struct wpa_gtk_data *gd,
664 const u8 *key_rsc)
665{
666 const u8 *_gtk = gd->gtk;
667 u8 gtk_buf[32];
668
669 wpa_hexdump_key(MSG_DEBUG, "WPA: Group Key", gd->gtk, gd->gtk_len);
670 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
671 "WPA: Installing GTK to the driver (keyidx=%d tx=%d len=%d)",
672 gd->keyidx, gd->tx, gd->gtk_len);
673 wpa_hexdump(MSG_DEBUG, "WPA: RSC", key_rsc, gd->key_rsc_len);
674 if (sm->group_cipher == WPA_CIPHER_TKIP) {
675 /* Swap Tx/Rx keys for Michael MIC */
676 os_memcpy(gtk_buf, gd->gtk, 16);
677 os_memcpy(gtk_buf + 16, gd->gtk + 24, 8);
678 os_memcpy(gtk_buf + 24, gd->gtk + 16, 8);
679 _gtk = gtk_buf;
680 }
681 if (sm->pairwise_cipher == WPA_CIPHER_NONE) {
682 if (wpa_sm_set_key(sm, gd->alg, NULL,
683 gd->keyidx, 1, key_rsc, gd->key_rsc_len,
684 _gtk, gd->gtk_len) < 0) {
685 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
686 "WPA: Failed to set GTK to the driver "
687 "(Group only)");
Dmitry Shmidt61593f02014-04-21 16:27:35 -0700688 os_memset(gtk_buf, 0, sizeof(gtk_buf));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700689 return -1;
690 }
691 } else if (wpa_sm_set_key(sm, gd->alg, broadcast_ether_addr,
692 gd->keyidx, gd->tx, key_rsc, gd->key_rsc_len,
693 _gtk, gd->gtk_len) < 0) {
694 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
695 "WPA: Failed to set GTK to "
696 "the driver (alg=%d keylen=%d keyidx=%d)",
697 gd->alg, gd->gtk_len, gd->keyidx);
Dmitry Shmidt61593f02014-04-21 16:27:35 -0700698 os_memset(gtk_buf, 0, sizeof(gtk_buf));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700699 return -1;
700 }
Dmitry Shmidt61593f02014-04-21 16:27:35 -0700701 os_memset(gtk_buf, 0, sizeof(gtk_buf));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700702
703 return 0;
704}
705
706
707static int wpa_supplicant_gtk_tx_bit_workaround(const struct wpa_sm *sm,
708 int tx)
709{
710 if (tx && sm->pairwise_cipher != WPA_CIPHER_NONE) {
711 /* Ignore Tx bit for GTK if a pairwise key is used. One AP
712 * seemed to set this bit (incorrectly, since Tx is only when
713 * doing Group Key only APs) and without this workaround, the
714 * data connection does not work because wpa_supplicant
715 * configured non-zero keyidx to be used for unicast. */
716 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
717 "WPA: Tx bit set for GTK, but pairwise "
718 "keys are used - ignore Tx bit");
719 return 0;
720 }
721 return tx;
722}
723
724
725static int wpa_supplicant_pairwise_gtk(struct wpa_sm *sm,
726 const struct wpa_eapol_key *key,
727 const u8 *gtk, size_t gtk_len,
728 int key_info)
729{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700730 struct wpa_gtk_data gd;
731
732 /*
733 * IEEE Std 802.11i-2004 - 8.5.2 EAPOL-Key frames - Figure 43x
734 * GTK KDE format:
735 * KeyID[bits 0-1], Tx [bit 2], Reserved [bits 3-7]
736 * Reserved [bits 0-7]
737 * GTK
738 */
739
740 os_memset(&gd, 0, sizeof(gd));
741 wpa_hexdump_key(MSG_DEBUG, "RSN: received GTK in pairwise handshake",
742 gtk, gtk_len);
743
744 if (gtk_len < 2 || gtk_len - 2 > sizeof(gd.gtk))
745 return -1;
746
747 gd.keyidx = gtk[0] & 0x3;
748 gd.tx = wpa_supplicant_gtk_tx_bit_workaround(sm,
749 !!(gtk[0] & BIT(2)));
750 gtk += 2;
751 gtk_len -= 2;
752
753 os_memcpy(gd.gtk, gtk, gtk_len);
754 gd.gtk_len = gtk_len;
755
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800756 if (sm->group_cipher != WPA_CIPHER_GTK_NOT_USED &&
757 (wpa_supplicant_check_group_cipher(sm, sm->group_cipher,
758 gtk_len, gtk_len,
759 &gd.key_rsc_len, &gd.alg) ||
760 wpa_supplicant_install_gtk(sm, &gd, key->key_rsc))) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700761 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
762 "RSN: Failed to install GTK");
Dmitry Shmidt61593f02014-04-21 16:27:35 -0700763 os_memset(&gd, 0, sizeof(gd));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700764 return -1;
765 }
Dmitry Shmidt61593f02014-04-21 16:27:35 -0700766 os_memset(&gd, 0, sizeof(gd));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700767
768 wpa_supplicant_key_neg_complete(sm, sm->bssid,
769 key_info & WPA_KEY_INFO_SECURE);
770 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700771}
772
773
774static int ieee80211w_set_keys(struct wpa_sm *sm,
775 struct wpa_eapol_ie_parse *ie)
776{
777#ifdef CONFIG_IEEE80211W
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -0700778 if (!wpa_cipher_valid_mgmt_group(sm->mgmt_group_cipher))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700779 return 0;
780
781 if (ie->igtk) {
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -0700782 size_t len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700783 const struct wpa_igtk_kde *igtk;
784 u16 keyidx;
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -0700785 len = wpa_cipher_key_len(sm->mgmt_group_cipher);
786 if (ie->igtk_len != WPA_IGTK_KDE_PREFIX_LEN + len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700787 return -1;
788 igtk = (const struct wpa_igtk_kde *) ie->igtk;
789 keyidx = WPA_GET_LE16(igtk->keyid);
790 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: IGTK keyid %d "
791 "pn %02x%02x%02x%02x%02x%02x",
792 keyidx, MAC2STR(igtk->pn));
793 wpa_hexdump_key(MSG_DEBUG, "WPA: IGTK",
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -0700794 igtk->igtk, len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700795 if (keyidx > 4095) {
796 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
797 "WPA: Invalid IGTK KeyID %d", keyidx);
798 return -1;
799 }
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -0700800 if (wpa_sm_set_key(sm, wpa_cipher_to_alg(sm->mgmt_group_cipher),
801 broadcast_ether_addr,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700802 keyidx, 0, igtk->pn, sizeof(igtk->pn),
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -0700803 igtk->igtk, len) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700804 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
805 "WPA: Failed to configure IGTK to the driver");
806 return -1;
807 }
808 }
809
810 return 0;
811#else /* CONFIG_IEEE80211W */
812 return 0;
813#endif /* CONFIG_IEEE80211W */
814}
815
816
817static void wpa_report_ie_mismatch(struct wpa_sm *sm,
818 const char *reason, const u8 *src_addr,
819 const u8 *wpa_ie, size_t wpa_ie_len,
820 const u8 *rsn_ie, size_t rsn_ie_len)
821{
822 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, "WPA: %s (src=" MACSTR ")",
823 reason, MAC2STR(src_addr));
824
825 if (sm->ap_wpa_ie) {
826 wpa_hexdump(MSG_INFO, "WPA: WPA IE in Beacon/ProbeResp",
827 sm->ap_wpa_ie, sm->ap_wpa_ie_len);
828 }
829 if (wpa_ie) {
830 if (!sm->ap_wpa_ie) {
831 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
832 "WPA: No WPA IE in Beacon/ProbeResp");
833 }
834 wpa_hexdump(MSG_INFO, "WPA: WPA IE in 3/4 msg",
835 wpa_ie, wpa_ie_len);
836 }
837
838 if (sm->ap_rsn_ie) {
839 wpa_hexdump(MSG_INFO, "WPA: RSN IE in Beacon/ProbeResp",
840 sm->ap_rsn_ie, sm->ap_rsn_ie_len);
841 }
842 if (rsn_ie) {
843 if (!sm->ap_rsn_ie) {
844 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
845 "WPA: No RSN IE in Beacon/ProbeResp");
846 }
847 wpa_hexdump(MSG_INFO, "WPA: RSN IE in 3/4 msg",
848 rsn_ie, rsn_ie_len);
849 }
850
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800851 wpa_sm_deauthenticate(sm, WLAN_REASON_IE_IN_4WAY_DIFFERS);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700852}
853
854
855#ifdef CONFIG_IEEE80211R
856
857static int ft_validate_mdie(struct wpa_sm *sm,
858 const unsigned char *src_addr,
859 struct wpa_eapol_ie_parse *ie,
860 const u8 *assoc_resp_mdie)
861{
862 struct rsn_mdie *mdie;
863
864 mdie = (struct rsn_mdie *) (ie->mdie + 2);
865 if (ie->mdie == NULL || ie->mdie_len < 2 + sizeof(*mdie) ||
866 os_memcmp(mdie->mobility_domain, sm->mobility_domain,
867 MOBILITY_DOMAIN_ID_LEN) != 0) {
868 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "FT: MDIE in msg 3/4 did "
869 "not match with the current mobility domain");
870 return -1;
871 }
872
873 if (assoc_resp_mdie &&
874 (assoc_resp_mdie[1] != ie->mdie[1] ||
875 os_memcmp(assoc_resp_mdie, ie->mdie, 2 + ie->mdie[1]) != 0)) {
876 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "FT: MDIE mismatch");
877 wpa_hexdump(MSG_DEBUG, "FT: MDIE in EAPOL-Key msg 3/4",
878 ie->mdie, 2 + ie->mdie[1]);
879 wpa_hexdump(MSG_DEBUG, "FT: MDIE in (Re)Association Response",
880 assoc_resp_mdie, 2 + assoc_resp_mdie[1]);
881 return -1;
882 }
883
884 return 0;
885}
886
887
888static int ft_validate_ftie(struct wpa_sm *sm,
889 const unsigned char *src_addr,
890 struct wpa_eapol_ie_parse *ie,
891 const u8 *assoc_resp_ftie)
892{
893 if (ie->ftie == NULL) {
894 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
895 "FT: No FTIE in EAPOL-Key msg 3/4");
896 return -1;
897 }
898
899 if (assoc_resp_ftie == NULL)
900 return 0;
901
902 if (assoc_resp_ftie[1] != ie->ftie[1] ||
903 os_memcmp(assoc_resp_ftie, ie->ftie, 2 + ie->ftie[1]) != 0) {
904 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "FT: FTIE mismatch");
905 wpa_hexdump(MSG_DEBUG, "FT: FTIE in EAPOL-Key msg 3/4",
906 ie->ftie, 2 + ie->ftie[1]);
907 wpa_hexdump(MSG_DEBUG, "FT: FTIE in (Re)Association Response",
908 assoc_resp_ftie, 2 + assoc_resp_ftie[1]);
909 return -1;
910 }
911
912 return 0;
913}
914
915
916static int ft_validate_rsnie(struct wpa_sm *sm,
917 const unsigned char *src_addr,
918 struct wpa_eapol_ie_parse *ie)
919{
920 struct wpa_ie_data rsn;
921
922 if (!ie->rsn_ie)
923 return 0;
924
925 /*
926 * Verify that PMKR1Name from EAPOL-Key message 3/4
927 * matches with the value we derived.
928 */
929 if (wpa_parse_wpa_ie_rsn(ie->rsn_ie, ie->rsn_ie_len, &rsn) < 0 ||
930 rsn.num_pmkid != 1 || rsn.pmkid == NULL) {
931 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "FT: No PMKR1Name in "
932 "FT 4-way handshake message 3/4");
933 return -1;
934 }
935
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700936 if (os_memcmp_const(rsn.pmkid, sm->pmk_r1_name, WPA_PMK_NAME_LEN) != 0)
937 {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700938 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
939 "FT: PMKR1Name mismatch in "
940 "FT 4-way handshake message 3/4");
941 wpa_hexdump(MSG_DEBUG, "FT: PMKR1Name from Authenticator",
942 rsn.pmkid, WPA_PMK_NAME_LEN);
943 wpa_hexdump(MSG_DEBUG, "FT: Derived PMKR1Name",
944 sm->pmk_r1_name, WPA_PMK_NAME_LEN);
945 return -1;
946 }
947
948 return 0;
949}
950
951
952static int wpa_supplicant_validate_ie_ft(struct wpa_sm *sm,
953 const unsigned char *src_addr,
954 struct wpa_eapol_ie_parse *ie)
955{
956 const u8 *pos, *end, *mdie = NULL, *ftie = NULL;
957
958 if (sm->assoc_resp_ies) {
959 pos = sm->assoc_resp_ies;
960 end = pos + sm->assoc_resp_ies_len;
961 while (pos + 2 < end) {
962 if (pos + 2 + pos[1] > end)
963 break;
964 switch (*pos) {
965 case WLAN_EID_MOBILITY_DOMAIN:
966 mdie = pos;
967 break;
968 case WLAN_EID_FAST_BSS_TRANSITION:
969 ftie = pos;
970 break;
971 }
972 pos += 2 + pos[1];
973 }
974 }
975
976 if (ft_validate_mdie(sm, src_addr, ie, mdie) < 0 ||
977 ft_validate_ftie(sm, src_addr, ie, ftie) < 0 ||
978 ft_validate_rsnie(sm, src_addr, ie) < 0)
979 return -1;
980
981 return 0;
982}
983
984#endif /* CONFIG_IEEE80211R */
985
986
987static int wpa_supplicant_validate_ie(struct wpa_sm *sm,
988 const unsigned char *src_addr,
989 struct wpa_eapol_ie_parse *ie)
990{
991 if (sm->ap_wpa_ie == NULL && sm->ap_rsn_ie == NULL) {
992 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
993 "WPA: No WPA/RSN IE for this AP known. "
994 "Trying to get from scan results");
995 if (wpa_sm_get_beacon_ie(sm) < 0) {
996 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
997 "WPA: Could not find AP from "
998 "the scan results");
999 } else {
1000 wpa_msg(sm->ctx->msg_ctx, MSG_DEBUG,
1001 "WPA: Found the current AP from "
1002 "updated scan results");
1003 }
1004 }
1005
1006 if (ie->wpa_ie == NULL && ie->rsn_ie == NULL &&
1007 (sm->ap_wpa_ie || sm->ap_rsn_ie)) {
1008 wpa_report_ie_mismatch(sm, "IE in 3/4 msg does not match "
1009 "with IE in Beacon/ProbeResp (no IE?)",
1010 src_addr, ie->wpa_ie, ie->wpa_ie_len,
1011 ie->rsn_ie, ie->rsn_ie_len);
1012 return -1;
1013 }
1014
1015 if ((ie->wpa_ie && sm->ap_wpa_ie &&
1016 (ie->wpa_ie_len != sm->ap_wpa_ie_len ||
1017 os_memcmp(ie->wpa_ie, sm->ap_wpa_ie, ie->wpa_ie_len) != 0)) ||
1018 (ie->rsn_ie && sm->ap_rsn_ie &&
1019 wpa_compare_rsn_ie(wpa_key_mgmt_ft(sm->key_mgmt),
1020 sm->ap_rsn_ie, sm->ap_rsn_ie_len,
1021 ie->rsn_ie, ie->rsn_ie_len))) {
1022 wpa_report_ie_mismatch(sm, "IE in 3/4 msg does not match "
1023 "with IE in Beacon/ProbeResp",
1024 src_addr, ie->wpa_ie, ie->wpa_ie_len,
1025 ie->rsn_ie, ie->rsn_ie_len);
1026 return -1;
1027 }
1028
1029 if (sm->proto == WPA_PROTO_WPA &&
1030 ie->rsn_ie && sm->ap_rsn_ie == NULL && sm->rsn_enabled) {
1031 wpa_report_ie_mismatch(sm, "Possible downgrade attack "
1032 "detected - RSN was enabled and RSN IE "
1033 "was in msg 3/4, but not in "
1034 "Beacon/ProbeResp",
1035 src_addr, ie->wpa_ie, ie->wpa_ie_len,
1036 ie->rsn_ie, ie->rsn_ie_len);
1037 return -1;
1038 }
1039
1040#ifdef CONFIG_IEEE80211R
1041 if (wpa_key_mgmt_ft(sm->key_mgmt) &&
1042 wpa_supplicant_validate_ie_ft(sm, src_addr, ie) < 0)
1043 return -1;
1044#endif /* CONFIG_IEEE80211R */
1045
1046 return 0;
1047}
1048
1049
1050/**
1051 * wpa_supplicant_send_4_of_4 - Send message 4 of WPA/RSN 4-Way Handshake
1052 * @sm: Pointer to WPA state machine data from wpa_sm_init()
1053 * @dst: Destination address for the frame
1054 * @key: Pointer to the EAPOL-Key frame header
1055 * @ver: Version bits from EAPOL-Key Key Info
1056 * @key_info: Key Info
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001057 * @ptk: PTK to use for keyed hash and encryption
1058 * Returns: 0 on success, -1 on failure
1059 */
1060int wpa_supplicant_send_4_of_4(struct wpa_sm *sm, const unsigned char *dst,
1061 const struct wpa_eapol_key *key,
1062 u16 ver, u16 key_info,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001063 struct wpa_ptk *ptk)
1064{
1065 size_t rlen;
1066 struct wpa_eapol_key *reply;
1067 u8 *rbuf;
1068
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001069 rbuf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_KEY, NULL,
Dmitry Shmidt21de2142014-04-08 10:50:52 -07001070 sizeof(*reply), &rlen, (void *) &reply);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001071 if (rbuf == NULL)
1072 return -1;
1073
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001074 reply->type = (sm->proto == WPA_PROTO_RSN ||
1075 sm->proto == WPA_PROTO_OSEN) ?
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001076 EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA;
1077 key_info &= WPA_KEY_INFO_SECURE;
1078 key_info |= ver | WPA_KEY_INFO_KEY_TYPE | WPA_KEY_INFO_MIC;
1079 WPA_PUT_BE16(reply->key_info, key_info);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001080 if (sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001081 WPA_PUT_BE16(reply->key_length, 0);
1082 else
1083 os_memcpy(reply->key_length, key->key_length, 2);
1084 os_memcpy(reply->replay_counter, key->replay_counter,
1085 WPA_REPLAY_COUNTER_LEN);
1086
Dmitry Shmidt21de2142014-04-08 10:50:52 -07001087 WPA_PUT_BE16(reply->key_data_length, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001088
1089 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Sending EAPOL-Key 4/4");
1090 wpa_eapol_key_send(sm, ptk->kck, ver, dst, ETH_P_EAPOL,
1091 rbuf, rlen, reply->key_mic);
1092
1093 return 0;
1094}
1095
1096
1097static void wpa_supplicant_process_3_of_4(struct wpa_sm *sm,
1098 const struct wpa_eapol_key *key,
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001099 u16 ver, const u8 *key_data,
1100 size_t key_data_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001101{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001102 u16 key_info, keylen;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001103 struct wpa_eapol_ie_parse ie;
1104
1105 wpa_sm_set_state(sm, WPA_4WAY_HANDSHAKE);
1106 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: RX message 3 of 4-Way "
1107 "Handshake from " MACSTR " (ver=%d)", MAC2STR(sm->bssid), ver);
1108
1109 key_info = WPA_GET_BE16(key->key_info);
1110
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001111 wpa_hexdump(MSG_DEBUG, "WPA: IE KeyData", key_data, key_data_len);
1112 if (wpa_supplicant_parse_ies(key_data, key_data_len, &ie) < 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001113 goto failed;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001114 if (ie.gtk && !(key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
1115 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1116 "WPA: GTK IE in unencrypted key data");
1117 goto failed;
1118 }
1119#ifdef CONFIG_IEEE80211W
1120 if (ie.igtk && !(key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
1121 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1122 "WPA: IGTK KDE in unencrypted key data");
1123 goto failed;
1124 }
1125
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001126 if (ie.igtk &&
1127 wpa_cipher_valid_mgmt_group(sm->mgmt_group_cipher) &&
1128 ie.igtk_len != WPA_IGTK_KDE_PREFIX_LEN +
1129 (unsigned int) wpa_cipher_key_len(sm->mgmt_group_cipher)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001130 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1131 "WPA: Invalid IGTK KDE length %lu",
1132 (unsigned long) ie.igtk_len);
1133 goto failed;
1134 }
1135#endif /* CONFIG_IEEE80211W */
1136
1137 if (wpa_supplicant_validate_ie(sm, sm->bssid, &ie) < 0)
1138 goto failed;
1139
1140 if (os_memcmp(sm->anonce, key->key_nonce, WPA_NONCE_LEN) != 0) {
1141 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1142 "WPA: ANonce from message 1 of 4-Way Handshake "
1143 "differs from 3 of 4-Way Handshake - drop packet (src="
1144 MACSTR ")", MAC2STR(sm->bssid));
1145 goto failed;
1146 }
1147
1148 keylen = WPA_GET_BE16(key->key_length);
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07001149 if (keylen != wpa_cipher_key_len(sm->pairwise_cipher)) {
1150 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1151 "WPA: Invalid %s key length %d (src=" MACSTR
1152 ")", wpa_cipher_txt(sm->pairwise_cipher), keylen,
1153 MAC2STR(sm->bssid));
1154 goto failed;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001155 }
1156
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001157#ifdef CONFIG_P2P
1158 if (ie.ip_addr_alloc) {
1159 os_memcpy(sm->p2p_ip_addr, ie.ip_addr_alloc, 3 * 4);
1160 wpa_hexdump(MSG_DEBUG, "P2P: IP address info",
1161 sm->p2p_ip_addr, sizeof(sm->p2p_ip_addr));
1162 }
1163#endif /* CONFIG_P2P */
1164
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001165 if (wpa_supplicant_send_4_of_4(sm, sm->bssid, key, ver, key_info,
Dmitry Shmidt21de2142014-04-08 10:50:52 -07001166 &sm->ptk)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001167 goto failed;
1168 }
1169
1170 /* SNonce was successfully used in msg 3/4, so mark it to be renewed
1171 * for the next 4-Way Handshake. If msg 3 is received again, the old
1172 * SNonce will still be used to avoid changing PTK. */
1173 sm->renew_snonce = 1;
1174
1175 if (key_info & WPA_KEY_INFO_INSTALL) {
1176 if (wpa_supplicant_install_ptk(sm, key))
1177 goto failed;
1178 }
1179
1180 if (key_info & WPA_KEY_INFO_SECURE) {
1181 wpa_sm_mlme_setprotection(
1182 sm, sm->bssid, MLME_SETPROTECTION_PROTECT_TYPE_RX,
1183 MLME_SETPROTECTION_KEY_TYPE_PAIRWISE);
1184 eapol_sm_notify_portValid(sm->eapol, TRUE);
1185 }
1186 wpa_sm_set_state(sm, WPA_GROUP_HANDSHAKE);
1187
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001188 if (sm->group_cipher == WPA_CIPHER_GTK_NOT_USED) {
1189 wpa_supplicant_key_neg_complete(sm, sm->bssid,
1190 key_info & WPA_KEY_INFO_SECURE);
1191 } else if (ie.gtk &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001192 wpa_supplicant_pairwise_gtk(sm, key,
1193 ie.gtk, ie.gtk_len, key_info) < 0) {
1194 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1195 "RSN: Failed to configure GTK");
1196 goto failed;
1197 }
1198
1199 if (ieee80211w_set_keys(sm, &ie) < 0) {
1200 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1201 "RSN: Failed to configure IGTK");
1202 goto failed;
1203 }
1204
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001205 if (ie.gtk)
1206 wpa_sm_set_rekey_offload(sm);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001207
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -08001208 if (sm->proto == WPA_PROTO_RSN && wpa_key_mgmt_suite_b(sm->key_mgmt)) {
1209 struct rsn_pmksa_cache_entry *sa;
1210
1211 sa = pmksa_cache_add(sm->pmksa, sm->pmk, sm->pmk_len,
1212 sm->ptk.kck, sizeof(sm->ptk.kck),
1213 sm->bssid, sm->own_addr,
1214 sm->network_ctx, sm->key_mgmt);
1215 if (!sm->cur_pmksa)
1216 sm->cur_pmksa = sa;
1217 }
1218
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001219 return;
1220
1221failed:
1222 wpa_sm_deauthenticate(sm, WLAN_REASON_UNSPECIFIED);
1223}
1224
1225
1226static int wpa_supplicant_process_1_of_2_rsn(struct wpa_sm *sm,
1227 const u8 *keydata,
1228 size_t keydatalen,
1229 u16 key_info,
1230 struct wpa_gtk_data *gd)
1231{
1232 int maxkeylen;
1233 struct wpa_eapol_ie_parse ie;
1234
1235 wpa_hexdump(MSG_DEBUG, "RSN: msg 1/2 key data", keydata, keydatalen);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001236 if (wpa_supplicant_parse_ies(keydata, keydatalen, &ie) < 0)
1237 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001238 if (ie.gtk && !(key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
1239 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1240 "WPA: GTK IE in unencrypted key data");
1241 return -1;
1242 }
1243 if (ie.gtk == NULL) {
1244 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1245 "WPA: No GTK IE in Group Key msg 1/2");
1246 return -1;
1247 }
1248 maxkeylen = gd->gtk_len = ie.gtk_len - 2;
1249
1250 if (wpa_supplicant_check_group_cipher(sm, sm->group_cipher,
1251 gd->gtk_len, maxkeylen,
1252 &gd->key_rsc_len, &gd->alg))
1253 return -1;
1254
1255 wpa_hexdump(MSG_DEBUG, "RSN: received GTK in group key handshake",
1256 ie.gtk, ie.gtk_len);
1257 gd->keyidx = ie.gtk[0] & 0x3;
1258 gd->tx = wpa_supplicant_gtk_tx_bit_workaround(sm,
1259 !!(ie.gtk[0] & BIT(2)));
1260 if (ie.gtk_len - 2 > sizeof(gd->gtk)) {
1261 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1262 "RSN: Too long GTK in GTK IE (len=%lu)",
1263 (unsigned long) ie.gtk_len - 2);
1264 return -1;
1265 }
1266 os_memcpy(gd->gtk, ie.gtk + 2, ie.gtk_len - 2);
1267
1268 if (ieee80211w_set_keys(sm, &ie) < 0)
1269 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1270 "RSN: Failed to configure IGTK");
1271
1272 return 0;
1273}
1274
1275
1276static int wpa_supplicant_process_1_of_2_wpa(struct wpa_sm *sm,
1277 const struct wpa_eapol_key *key,
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001278 const u8 *key_data,
1279 size_t key_data_len, u16 key_info,
1280 u16 ver, struct wpa_gtk_data *gd)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001281{
1282 size_t maxkeylen;
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -08001283 u16 gtk_len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001284
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -08001285 gtk_len = WPA_GET_BE16(key->key_length);
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001286 maxkeylen = key_data_len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001287 if (ver == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
1288 if (maxkeylen < 8) {
1289 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1290 "WPA: Too short maxkeylen (%lu)",
1291 (unsigned long) maxkeylen);
1292 return -1;
1293 }
1294 maxkeylen -= 8;
1295 }
1296
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -08001297 if (gtk_len > maxkeylen ||
1298 wpa_supplicant_check_group_cipher(sm, sm->group_cipher,
1299 gtk_len, maxkeylen,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001300 &gd->key_rsc_len, &gd->alg))
1301 return -1;
1302
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -08001303 gd->gtk_len = gtk_len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001304 gd->keyidx = (key_info & WPA_KEY_INFO_KEY_INDEX_MASK) >>
1305 WPA_KEY_INFO_KEY_INDEX_SHIFT;
1306 if (ver == WPA_KEY_INFO_TYPE_HMAC_MD5_RC4) {
Dmitry Shmidt61593f02014-04-21 16:27:35 -07001307 u8 ek[32];
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001308 if (key_data_len > sizeof(gd->gtk)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001309 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1310 "WPA: RC4 key data too long (%lu)",
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001311 (unsigned long) key_data_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001312 return -1;
1313 }
Dmitry Shmidt61593f02014-04-21 16:27:35 -07001314 os_memcpy(ek, key->key_iv, 16);
1315 os_memcpy(ek + 16, sm->ptk.kek, 16);
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001316 os_memcpy(gd->gtk, key_data, key_data_len);
1317 if (rc4_skip(ek, 32, 256, gd->gtk, key_data_len)) {
Dmitry Shmidt61593f02014-04-21 16:27:35 -07001318 os_memset(ek, 0, sizeof(ek));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001319 wpa_msg(sm->ctx->msg_ctx, MSG_ERROR,
1320 "WPA: RC4 failed");
1321 return -1;
1322 }
Dmitry Shmidt61593f02014-04-21 16:27:35 -07001323 os_memset(ek, 0, sizeof(ek));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001324 } else if (ver == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001325 if (maxkeylen % 8) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001326 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1327 "WPA: Unsupported AES-WRAP len %lu",
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001328 (unsigned long) maxkeylen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001329 return -1;
1330 }
1331 if (maxkeylen > sizeof(gd->gtk)) {
1332 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1333 "WPA: AES-WRAP key data "
1334 "too long (keydatalen=%lu maxkeylen=%lu)",
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001335 (unsigned long) key_data_len,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001336 (unsigned long) maxkeylen);
1337 return -1;
1338 }
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07001339 if (aes_unwrap(sm->ptk.kek, 16, maxkeylen / 8, key_data,
1340 gd->gtk)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001341 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1342 "WPA: AES unwrap failed - could not decrypt "
1343 "GTK");
1344 return -1;
1345 }
1346 } else {
1347 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1348 "WPA: Unsupported key_info type %d", ver);
1349 return -1;
1350 }
1351 gd->tx = wpa_supplicant_gtk_tx_bit_workaround(
1352 sm, !!(key_info & WPA_KEY_INFO_TXRX));
1353 return 0;
1354}
1355
1356
1357static int wpa_supplicant_send_2_of_2(struct wpa_sm *sm,
1358 const struct wpa_eapol_key *key,
1359 int ver, u16 key_info)
1360{
1361 size_t rlen;
1362 struct wpa_eapol_key *reply;
1363 u8 *rbuf;
1364
1365 rbuf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_KEY, NULL,
1366 sizeof(*reply), &rlen, (void *) &reply);
1367 if (rbuf == NULL)
1368 return -1;
1369
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001370 reply->type = (sm->proto == WPA_PROTO_RSN ||
1371 sm->proto == WPA_PROTO_OSEN) ?
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001372 EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA;
1373 key_info &= WPA_KEY_INFO_KEY_INDEX_MASK;
1374 key_info |= ver | WPA_KEY_INFO_MIC | WPA_KEY_INFO_SECURE;
1375 WPA_PUT_BE16(reply->key_info, key_info);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001376 if (sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001377 WPA_PUT_BE16(reply->key_length, 0);
1378 else
1379 os_memcpy(reply->key_length, key->key_length, 2);
1380 os_memcpy(reply->replay_counter, key->replay_counter,
1381 WPA_REPLAY_COUNTER_LEN);
1382
1383 WPA_PUT_BE16(reply->key_data_length, 0);
1384
1385 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Sending EAPOL-Key 2/2");
1386 wpa_eapol_key_send(sm, sm->ptk.kck, ver, sm->bssid, ETH_P_EAPOL,
1387 rbuf, rlen, reply->key_mic);
1388
1389 return 0;
1390}
1391
1392
1393static void wpa_supplicant_process_1_of_2(struct wpa_sm *sm,
1394 const unsigned char *src_addr,
1395 const struct wpa_eapol_key *key,
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001396 const u8 *key_data,
1397 size_t key_data_len, u16 ver)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001398{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001399 u16 key_info;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001400 int rekey, ret;
1401 struct wpa_gtk_data gd;
1402
1403 os_memset(&gd, 0, sizeof(gd));
1404
1405 rekey = wpa_sm_get_state(sm) == WPA_COMPLETED;
1406 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: RX message 1 of Group Key "
1407 "Handshake from " MACSTR " (ver=%d)", MAC2STR(src_addr), ver);
1408
1409 key_info = WPA_GET_BE16(key->key_info);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001410
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001411 if (sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN) {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001412 ret = wpa_supplicant_process_1_of_2_rsn(sm, key_data,
1413 key_data_len, key_info,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001414 &gd);
1415 } else {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001416 ret = wpa_supplicant_process_1_of_2_wpa(sm, key, key_data,
1417 key_data_len,
1418 key_info, ver, &gd);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001419 }
1420
1421 wpa_sm_set_state(sm, WPA_GROUP_HANDSHAKE);
1422
1423 if (ret)
1424 goto failed;
1425
1426 if (wpa_supplicant_install_gtk(sm, &gd, key->key_rsc) ||
1427 wpa_supplicant_send_2_of_2(sm, key, ver, key_info))
1428 goto failed;
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -08001429 os_memset(&gd, 0, sizeof(gd));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001430
1431 if (rekey) {
1432 wpa_msg(sm->ctx->msg_ctx, MSG_INFO, "WPA: Group rekeying "
1433 "completed with " MACSTR " [GTK=%s]",
1434 MAC2STR(sm->bssid), wpa_cipher_txt(sm->group_cipher));
1435 wpa_sm_cancel_auth_timeout(sm);
1436 wpa_sm_set_state(sm, WPA_COMPLETED);
1437 } else {
1438 wpa_supplicant_key_neg_complete(sm, sm->bssid,
1439 key_info &
1440 WPA_KEY_INFO_SECURE);
1441 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001442
1443 wpa_sm_set_rekey_offload(sm);
1444
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001445 return;
1446
1447failed:
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -08001448 os_memset(&gd, 0, sizeof(gd));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001449 wpa_sm_deauthenticate(sm, WLAN_REASON_UNSPECIFIED);
1450}
1451
1452
1453static int wpa_supplicant_verify_eapol_key_mic(struct wpa_sm *sm,
1454 struct wpa_eapol_key *key,
1455 u16 ver,
1456 const u8 *buf, size_t len)
1457{
1458 u8 mic[16];
1459 int ok = 0;
1460
1461 os_memcpy(mic, key->key_mic, 16);
1462 if (sm->tptk_set) {
1463 os_memset(key->key_mic, 0, 16);
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -08001464 wpa_eapol_key_mic(sm->tptk.kck, sm->key_mgmt, ver, buf, len,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001465 key->key_mic);
Dmitry Shmidtc2817022014-07-02 10:32:10 -07001466 if (os_memcmp_const(mic, key->key_mic, 16) != 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001467 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1468 "WPA: Invalid EAPOL-Key MIC "
1469 "when using TPTK - ignoring TPTK");
1470 } else {
1471 ok = 1;
1472 sm->tptk_set = 0;
1473 sm->ptk_set = 1;
1474 os_memcpy(&sm->ptk, &sm->tptk, sizeof(sm->ptk));
Dmitry Shmidt61593f02014-04-21 16:27:35 -07001475 os_memset(&sm->tptk, 0, sizeof(sm->tptk));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001476 }
1477 }
1478
1479 if (!ok && sm->ptk_set) {
1480 os_memset(key->key_mic, 0, 16);
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -08001481 wpa_eapol_key_mic(sm->ptk.kck, sm->key_mgmt, ver, buf, len,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001482 key->key_mic);
Dmitry Shmidtc2817022014-07-02 10:32:10 -07001483 if (os_memcmp_const(mic, key->key_mic, 16) != 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001484 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1485 "WPA: Invalid EAPOL-Key MIC - "
1486 "dropping packet");
1487 return -1;
1488 }
1489 ok = 1;
1490 }
1491
1492 if (!ok) {
1493 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1494 "WPA: Could not verify EAPOL-Key MIC - "
1495 "dropping packet");
1496 return -1;
1497 }
1498
1499 os_memcpy(sm->rx_replay_counter, key->replay_counter,
1500 WPA_REPLAY_COUNTER_LEN);
1501 sm->rx_replay_counter_set = 1;
1502 return 0;
1503}
1504
1505
1506/* Decrypt RSN EAPOL-Key key data (RC4 or AES-WRAP) */
1507static int wpa_supplicant_decrypt_key_data(struct wpa_sm *sm,
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001508 struct wpa_eapol_key *key, u16 ver,
1509 u8 *key_data, size_t *key_data_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001510{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001511 wpa_hexdump(MSG_DEBUG, "RSN: encrypted key data",
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001512 key_data, *key_data_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001513 if (!sm->ptk_set) {
1514 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1515 "WPA: PTK not available, cannot decrypt EAPOL-Key Key "
1516 "Data");
1517 return -1;
1518 }
1519
1520 /* Decrypt key data here so that this operation does not need
1521 * to be implemented separately for each message type. */
1522 if (ver == WPA_KEY_INFO_TYPE_HMAC_MD5_RC4) {
1523 u8 ek[32];
1524 os_memcpy(ek, key->key_iv, 16);
1525 os_memcpy(ek + 16, sm->ptk.kek, 16);
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001526 if (rc4_skip(ek, 32, 256, key_data, *key_data_len)) {
Dmitry Shmidt61593f02014-04-21 16:27:35 -07001527 os_memset(ek, 0, sizeof(ek));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001528 wpa_msg(sm->ctx->msg_ctx, MSG_ERROR,
1529 "WPA: RC4 failed");
1530 return -1;
1531 }
Dmitry Shmidt61593f02014-04-21 16:27:35 -07001532 os_memset(ek, 0, sizeof(ek));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001533 } else if (ver == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES ||
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001534 ver == WPA_KEY_INFO_TYPE_AES_128_CMAC ||
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -08001535 sm->key_mgmt == WPA_KEY_MGMT_OSEN ||
1536 wpa_key_mgmt_suite_b(sm->key_mgmt)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001537 u8 *buf;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001538 if (*key_data_len < 8 || *key_data_len % 8) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001539 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001540 "WPA: Unsupported AES-WRAP len %u",
1541 (unsigned int) *key_data_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001542 return -1;
1543 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001544 *key_data_len -= 8; /* AES-WRAP adds 8 bytes */
1545 buf = os_malloc(*key_data_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001546 if (buf == NULL) {
1547 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1548 "WPA: No memory for AES-UNWRAP buffer");
1549 return -1;
1550 }
Dmitry Shmidt9ead16e2014-10-07 13:15:23 -07001551 if (aes_unwrap(sm->ptk.kek, 16, *key_data_len / 8,
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001552 key_data, buf)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001553 os_free(buf);
1554 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1555 "WPA: AES unwrap failed - "
1556 "could not decrypt EAPOL-Key key data");
1557 return -1;
1558 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001559 os_memcpy(key_data, buf, *key_data_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001560 os_free(buf);
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001561 WPA_PUT_BE16(key->key_data_length, *key_data_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001562 } else {
1563 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1564 "WPA: Unsupported key_info type %d", ver);
1565 return -1;
1566 }
1567 wpa_hexdump_key(MSG_DEBUG, "WPA: decrypted EAPOL-Key key data",
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001568 key_data, *key_data_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001569 return 0;
1570}
1571
1572
1573/**
1574 * wpa_sm_aborted_cached - Notify WPA that PMKSA caching was aborted
1575 * @sm: Pointer to WPA state machine data from wpa_sm_init()
1576 */
1577void wpa_sm_aborted_cached(struct wpa_sm *sm)
1578{
1579 if (sm && sm->cur_pmksa) {
1580 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1581 "RSN: Cancelling PMKSA caching attempt");
1582 sm->cur_pmksa = NULL;
1583 }
1584}
1585
1586
1587static void wpa_eapol_key_dump(struct wpa_sm *sm,
1588 const struct wpa_eapol_key *key)
1589{
1590#ifndef CONFIG_NO_STDOUT_DEBUG
1591 u16 key_info = WPA_GET_BE16(key->key_info);
1592
1593 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, " EAPOL-Key type=%d", key->type);
1594 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1595 " key_info 0x%x (ver=%d keyidx=%d rsvd=%d %s%s%s%s%s%s%s%s)",
1596 key_info, key_info & WPA_KEY_INFO_TYPE_MASK,
1597 (key_info & WPA_KEY_INFO_KEY_INDEX_MASK) >>
1598 WPA_KEY_INFO_KEY_INDEX_SHIFT,
1599 (key_info & (BIT(13) | BIT(14) | BIT(15))) >> 13,
1600 key_info & WPA_KEY_INFO_KEY_TYPE ? "Pairwise" : "Group",
1601 key_info & WPA_KEY_INFO_INSTALL ? " Install" : "",
1602 key_info & WPA_KEY_INFO_ACK ? " Ack" : "",
1603 key_info & WPA_KEY_INFO_MIC ? " MIC" : "",
1604 key_info & WPA_KEY_INFO_SECURE ? " Secure" : "",
1605 key_info & WPA_KEY_INFO_ERROR ? " Error" : "",
1606 key_info & WPA_KEY_INFO_REQUEST ? " Request" : "",
1607 key_info & WPA_KEY_INFO_ENCR_KEY_DATA ? " Encr" : "");
1608 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1609 " key_length=%u key_data_length=%u",
1610 WPA_GET_BE16(key->key_length),
1611 WPA_GET_BE16(key->key_data_length));
1612 wpa_hexdump(MSG_DEBUG, " replay_counter",
1613 key->replay_counter, WPA_REPLAY_COUNTER_LEN);
1614 wpa_hexdump(MSG_DEBUG, " key_nonce", key->key_nonce, WPA_NONCE_LEN);
1615 wpa_hexdump(MSG_DEBUG, " key_iv", key->key_iv, 16);
1616 wpa_hexdump(MSG_DEBUG, " key_rsc", key->key_rsc, 8);
1617 wpa_hexdump(MSG_DEBUG, " key_id (reserved)", key->key_id, 8);
1618 wpa_hexdump(MSG_DEBUG, " key_mic", key->key_mic, 16);
1619#endif /* CONFIG_NO_STDOUT_DEBUG */
1620}
1621
1622
1623/**
1624 * wpa_sm_rx_eapol - Process received WPA EAPOL frames
1625 * @sm: Pointer to WPA state machine data from wpa_sm_init()
1626 * @src_addr: Source MAC address of the EAPOL packet
1627 * @buf: Pointer to the beginning of the EAPOL data (EAPOL header)
1628 * @len: Length of the EAPOL frame
1629 * Returns: 1 = WPA EAPOL-Key processed, 0 = not a WPA EAPOL-Key, -1 failure
1630 *
1631 * This function is called for each received EAPOL frame. Other than EAPOL-Key
1632 * frames can be skipped if filtering is done elsewhere. wpa_sm_rx_eapol() is
1633 * only processing WPA and WPA2 EAPOL-Key frames.
1634 *
1635 * The received EAPOL-Key packets are validated and valid packets are replied
1636 * to. In addition, key material (PTK, GTK) is configured at the end of a
1637 * successful key handshake.
1638 */
1639int wpa_sm_rx_eapol(struct wpa_sm *sm, const u8 *src_addr,
1640 const u8 *buf, size_t len)
1641{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001642 size_t plen, data_len, key_data_len;
1643 const struct ieee802_1x_hdr *hdr;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001644 struct wpa_eapol_key *key;
1645 u16 key_info, ver;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001646 u8 *tmp = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001647 int ret = -1;
1648 struct wpa_peerkey *peerkey = NULL;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001649 u8 *key_data;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001650
1651#ifdef CONFIG_IEEE80211R
1652 sm->ft_completed = 0;
1653#endif /* CONFIG_IEEE80211R */
1654
1655 if (len < sizeof(*hdr) + sizeof(*key)) {
1656 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1657 "WPA: EAPOL frame too short to be a WPA "
1658 "EAPOL-Key (len %lu, expecting at least %lu)",
1659 (unsigned long) len,
1660 (unsigned long) sizeof(*hdr) + sizeof(*key));
1661 return 0;
1662 }
1663
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001664 hdr = (const struct ieee802_1x_hdr *) buf;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001665 plen = be_to_host16(hdr->length);
1666 data_len = plen + sizeof(*hdr);
1667 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1668 "IEEE 802.1X RX: version=%d type=%d length=%lu",
1669 hdr->version, hdr->type, (unsigned long) plen);
1670
1671 if (hdr->version < EAPOL_VERSION) {
1672 /* TODO: backwards compatibility */
1673 }
1674 if (hdr->type != IEEE802_1X_TYPE_EAPOL_KEY) {
1675 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1676 "WPA: EAPOL frame (type %u) discarded, "
1677 "not a Key frame", hdr->type);
1678 ret = 0;
1679 goto out;
1680 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001681 wpa_hexdump(MSG_MSGDUMP, "WPA: RX EAPOL-Key", buf, len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001682 if (plen > len - sizeof(*hdr) || plen < sizeof(*key)) {
1683 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1684 "WPA: EAPOL frame payload size %lu "
1685 "invalid (frame size %lu)",
1686 (unsigned long) plen, (unsigned long) len);
1687 ret = 0;
1688 goto out;
1689 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001690 if (data_len < len) {
1691 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1692 "WPA: ignoring %lu bytes after the IEEE 802.1X data",
1693 (unsigned long) len - data_len);
1694 }
1695
1696 /*
1697 * Make a copy of the frame since we need to modify the buffer during
1698 * MAC validation and Key Data decryption.
1699 */
1700 tmp = os_malloc(data_len);
1701 if (tmp == NULL)
1702 goto out;
1703 os_memcpy(tmp, buf, data_len);
1704 key = (struct wpa_eapol_key *) (tmp + sizeof(struct ieee802_1x_hdr));
1705 key_data = (u8 *) (key + 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001706
1707 if (key->type != EAPOL_KEY_TYPE_WPA && key->type != EAPOL_KEY_TYPE_RSN)
1708 {
1709 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1710 "WPA: EAPOL-Key type (%d) unknown, discarded",
1711 key->type);
1712 ret = 0;
1713 goto out;
1714 }
1715 wpa_eapol_key_dump(sm, key);
1716
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001717 key_data_len = WPA_GET_BE16(key->key_data_length);
1718 if (key_data_len > plen - sizeof(struct wpa_eapol_key)) {
1719 wpa_msg(sm->ctx->msg_ctx, MSG_INFO, "WPA: Invalid EAPOL-Key "
1720 "frame - key_data overflow (%u > %u)",
1721 (unsigned int) key_data_len,
1722 (unsigned int) (plen - sizeof(struct wpa_eapol_key)));
1723 goto out;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001724 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001725
1726 eapol_sm_notify_lower_layer_success(sm->eapol, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001727 key_info = WPA_GET_BE16(key->key_info);
1728 ver = key_info & WPA_KEY_INFO_TYPE_MASK;
1729 if (ver != WPA_KEY_INFO_TYPE_HMAC_MD5_RC4 &&
1730#if defined(CONFIG_IEEE80211R) || defined(CONFIG_IEEE80211W)
1731 ver != WPA_KEY_INFO_TYPE_AES_128_CMAC &&
1732#endif /* CONFIG_IEEE80211R || CONFIG_IEEE80211W */
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001733 ver != WPA_KEY_INFO_TYPE_HMAC_SHA1_AES &&
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -08001734 !wpa_key_mgmt_suite_b(sm->key_mgmt) &&
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001735 sm->key_mgmt != WPA_KEY_MGMT_OSEN) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001736 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1737 "WPA: Unsupported EAPOL-Key descriptor version %d",
1738 ver);
1739 goto out;
1740 }
1741
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001742 if (sm->key_mgmt == WPA_KEY_MGMT_OSEN &&
1743 ver != WPA_KEY_INFO_TYPE_AKM_DEFINED) {
1744 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1745 "OSEN: Unsupported EAPOL-Key descriptor version %d",
1746 ver);
1747 goto out;
1748 }
1749
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -08001750 if (wpa_key_mgmt_suite_b(sm->key_mgmt) &&
1751 ver != WPA_KEY_INFO_TYPE_AKM_DEFINED) {
1752 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1753 "RSN: Unsupported EAPOL-Key descriptor version %d (expected AKM defined = 0)",
1754 ver);
1755 goto out;
1756 }
1757
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001758#ifdef CONFIG_IEEE80211R
1759 if (wpa_key_mgmt_ft(sm->key_mgmt)) {
1760 /* IEEE 802.11r uses a new key_info type (AES-128-CMAC). */
1761 if (ver != WPA_KEY_INFO_TYPE_AES_128_CMAC) {
1762 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1763 "FT: AP did not use AES-128-CMAC");
1764 goto out;
1765 }
1766 } else
1767#endif /* CONFIG_IEEE80211R */
1768#ifdef CONFIG_IEEE80211W
1769 if (wpa_key_mgmt_sha256(sm->key_mgmt)) {
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001770 if (ver != WPA_KEY_INFO_TYPE_AES_128_CMAC &&
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -08001771 sm->key_mgmt != WPA_KEY_MGMT_OSEN &&
1772 !wpa_key_mgmt_suite_b(sm->key_mgmt)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001773 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1774 "WPA: AP did not use the "
1775 "negotiated AES-128-CMAC");
1776 goto out;
1777 }
1778 } else
1779#endif /* CONFIG_IEEE80211W */
1780 if (sm->pairwise_cipher == WPA_CIPHER_CCMP &&
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -08001781 !wpa_key_mgmt_suite_b(sm->key_mgmt) &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001782 ver != WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
1783 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1784 "WPA: CCMP is used, but EAPOL-Key "
1785 "descriptor version (%d) is not 2", ver);
1786 if (sm->group_cipher != WPA_CIPHER_CCMP &&
1787 !(key_info & WPA_KEY_INFO_KEY_TYPE)) {
1788 /* Earlier versions of IEEE 802.11i did not explicitly
1789 * require version 2 descriptor for all EAPOL-Key
1790 * packets, so allow group keys to use version 1 if
1791 * CCMP is not used for them. */
1792 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1793 "WPA: Backwards compatibility: allow invalid "
1794 "version for non-CCMP group keys");
Jouni Malinen658fb4a2014-11-14 20:57:05 +02001795 } else if (ver == WPA_KEY_INFO_TYPE_AES_128_CMAC) {
1796 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1797 "WPA: Interoperability workaround: allow incorrect (should have been HMAC-SHA1), but stronger (is AES-128-CMAC), descriptor version to be used");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001798 } else
1799 goto out;
Dmitry Shmidt71757432014-06-02 13:50:35 -07001800 } else if (sm->pairwise_cipher == WPA_CIPHER_GCMP &&
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -08001801 !wpa_key_mgmt_suite_b(sm->key_mgmt) &&
Dmitry Shmidt71757432014-06-02 13:50:35 -07001802 ver != WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001803 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1804 "WPA: GCMP is used, but EAPOL-Key "
1805 "descriptor version (%d) is not 2", ver);
1806 goto out;
1807 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001808
1809#ifdef CONFIG_PEERKEY
1810 for (peerkey = sm->peerkey; peerkey; peerkey = peerkey->next) {
1811 if (os_memcmp(peerkey->addr, src_addr, ETH_ALEN) == 0)
1812 break;
1813 }
1814
1815 if (!(key_info & WPA_KEY_INFO_SMK_MESSAGE) && peerkey) {
1816 if (!peerkey->initiator && peerkey->replay_counter_set &&
1817 os_memcmp(key->replay_counter, peerkey->replay_counter,
1818 WPA_REPLAY_COUNTER_LEN) <= 0) {
1819 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1820 "RSN: EAPOL-Key Replay Counter did not "
1821 "increase (STK) - dropping packet");
1822 goto out;
1823 } else if (peerkey->initiator) {
1824 u8 _tmp[WPA_REPLAY_COUNTER_LEN];
1825 os_memcpy(_tmp, key->replay_counter,
1826 WPA_REPLAY_COUNTER_LEN);
1827 inc_byte_array(_tmp, WPA_REPLAY_COUNTER_LEN);
1828 if (os_memcmp(_tmp, peerkey->replay_counter,
1829 WPA_REPLAY_COUNTER_LEN) != 0) {
1830 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1831 "RSN: EAPOL-Key Replay "
1832 "Counter did not match (STK) - "
1833 "dropping packet");
1834 goto out;
1835 }
1836 }
1837 }
1838
1839 if (peerkey && peerkey->initiator && (key_info & WPA_KEY_INFO_ACK)) {
1840 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1841 "RSN: Ack bit in key_info from STK peer");
1842 goto out;
1843 }
1844#endif /* CONFIG_PEERKEY */
1845
1846 if (!peerkey && sm->rx_replay_counter_set &&
1847 os_memcmp(key->replay_counter, sm->rx_replay_counter,
1848 WPA_REPLAY_COUNTER_LEN) <= 0) {
1849 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1850 "WPA: EAPOL-Key Replay Counter did not increase - "
1851 "dropping packet");
1852 goto out;
1853 }
1854
1855 if (!(key_info & (WPA_KEY_INFO_ACK | WPA_KEY_INFO_SMK_MESSAGE))
1856#ifdef CONFIG_PEERKEY
1857 && (peerkey == NULL || !peerkey->initiator)
1858#endif /* CONFIG_PEERKEY */
1859 ) {
1860 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1861 "WPA: No Ack bit in key_info");
1862 goto out;
1863 }
1864
1865 if (key_info & WPA_KEY_INFO_REQUEST) {
1866 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1867 "WPA: EAPOL-Key with Request bit - dropped");
1868 goto out;
1869 }
1870
1871 if ((key_info & WPA_KEY_INFO_MIC) && !peerkey &&
1872 wpa_supplicant_verify_eapol_key_mic(sm, key, ver, tmp, data_len))
1873 goto out;
1874
1875#ifdef CONFIG_PEERKEY
1876 if ((key_info & WPA_KEY_INFO_MIC) && peerkey &&
1877 peerkey_verify_eapol_key_mic(sm, peerkey, key, ver, tmp, data_len))
1878 goto out;
1879#endif /* CONFIG_PEERKEY */
1880
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001881 if ((sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN) &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001882 (key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001883 if (wpa_supplicant_decrypt_key_data(sm, key, ver, key_data,
1884 &key_data_len))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001885 goto out;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001886 }
1887
1888 if (key_info & WPA_KEY_INFO_KEY_TYPE) {
1889 if (key_info & WPA_KEY_INFO_KEY_INDEX_MASK) {
1890 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1891 "WPA: Ignored EAPOL-Key (Pairwise) with "
1892 "non-zero key index");
1893 goto out;
1894 }
1895 if (peerkey) {
1896 /* PeerKey 4-Way Handshake */
Dmitry Shmidtc2817022014-07-02 10:32:10 -07001897 peerkey_rx_eapol_4way(sm, peerkey, key, key_info, ver,
1898 key_data, key_data_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001899 } else if (key_info & WPA_KEY_INFO_MIC) {
1900 /* 3/4 4-Way Handshake */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001901 wpa_supplicant_process_3_of_4(sm, key, ver, key_data,
1902 key_data_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001903 } else {
1904 /* 1/4 4-Way Handshake */
1905 wpa_supplicant_process_1_of_4(sm, src_addr, key,
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001906 ver, key_data,
1907 key_data_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001908 }
1909 } else if (key_info & WPA_KEY_INFO_SMK_MESSAGE) {
1910 /* PeerKey SMK Handshake */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001911 peerkey_rx_eapol_smk(sm, src_addr, key, key_data_len, key_info,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001912 ver);
1913 } else {
1914 if (key_info & WPA_KEY_INFO_MIC) {
1915 /* 1/2 Group Key Handshake */
1916 wpa_supplicant_process_1_of_2(sm, src_addr, key,
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001917 key_data, key_data_len,
1918 ver);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001919 } else {
1920 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1921 "WPA: EAPOL-Key (Group) without Mic bit - "
1922 "dropped");
1923 }
1924 }
1925
1926 ret = 1;
1927
1928out:
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -08001929 bin_clear_free(tmp, data_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001930 return ret;
1931}
1932
1933
1934#ifdef CONFIG_CTRL_IFACE
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001935static u32 wpa_key_mgmt_suite(struct wpa_sm *sm)
1936{
1937 switch (sm->key_mgmt) {
1938 case WPA_KEY_MGMT_IEEE8021X:
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001939 return ((sm->proto == WPA_PROTO_RSN ||
1940 sm->proto == WPA_PROTO_OSEN) ?
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001941 RSN_AUTH_KEY_MGMT_UNSPEC_802_1X :
1942 WPA_AUTH_KEY_MGMT_UNSPEC_802_1X);
1943 case WPA_KEY_MGMT_PSK:
1944 return (sm->proto == WPA_PROTO_RSN ?
1945 RSN_AUTH_KEY_MGMT_PSK_OVER_802_1X :
1946 WPA_AUTH_KEY_MGMT_PSK_OVER_802_1X);
1947#ifdef CONFIG_IEEE80211R
1948 case WPA_KEY_MGMT_FT_IEEE8021X:
1949 return RSN_AUTH_KEY_MGMT_FT_802_1X;
1950 case WPA_KEY_MGMT_FT_PSK:
1951 return RSN_AUTH_KEY_MGMT_FT_PSK;
1952#endif /* CONFIG_IEEE80211R */
1953#ifdef CONFIG_IEEE80211W
1954 case WPA_KEY_MGMT_IEEE8021X_SHA256:
1955 return RSN_AUTH_KEY_MGMT_802_1X_SHA256;
1956 case WPA_KEY_MGMT_PSK_SHA256:
1957 return RSN_AUTH_KEY_MGMT_PSK_SHA256;
1958#endif /* CONFIG_IEEE80211W */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001959 case WPA_KEY_MGMT_CCKM:
1960 return (sm->proto == WPA_PROTO_RSN ?
1961 RSN_AUTH_KEY_MGMT_CCKM:
1962 WPA_AUTH_KEY_MGMT_CCKM);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001963 case WPA_KEY_MGMT_WPA_NONE:
1964 return WPA_AUTH_KEY_MGMT_NONE;
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -08001965 case WPA_KEY_MGMT_IEEE8021X_SUITE_B:
1966 return RSN_AUTH_KEY_MGMT_802_1X_SUITE_B;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001967 default:
1968 return 0;
1969 }
1970}
1971
1972
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001973#define RSN_SUITE "%02x-%02x-%02x-%d"
1974#define RSN_SUITE_ARG(s) \
1975((s) >> 24) & 0xff, ((s) >> 16) & 0xff, ((s) >> 8) & 0xff, (s) & 0xff
1976
1977/**
1978 * wpa_sm_get_mib - Dump text list of MIB entries
1979 * @sm: Pointer to WPA state machine data from wpa_sm_init()
1980 * @buf: Buffer for the list
1981 * @buflen: Length of the buffer
1982 * Returns: Number of bytes written to buffer
1983 *
1984 * This function is used fetch dot11 MIB variables.
1985 */
1986int wpa_sm_get_mib(struct wpa_sm *sm, char *buf, size_t buflen)
1987{
1988 char pmkid_txt[PMKID_LEN * 2 + 1];
1989 int rsna, ret;
1990 size_t len;
1991
1992 if (sm->cur_pmksa) {
1993 wpa_snprintf_hex(pmkid_txt, sizeof(pmkid_txt),
1994 sm->cur_pmksa->pmkid, PMKID_LEN);
1995 } else
1996 pmkid_txt[0] = '\0';
1997
1998 if ((wpa_key_mgmt_wpa_psk(sm->key_mgmt) ||
1999 wpa_key_mgmt_wpa_ieee8021x(sm->key_mgmt)) &&
2000 sm->proto == WPA_PROTO_RSN)
2001 rsna = 1;
2002 else
2003 rsna = 0;
2004
2005 ret = os_snprintf(buf, buflen,
2006 "dot11RSNAOptionImplemented=TRUE\n"
2007 "dot11RSNAPreauthenticationImplemented=TRUE\n"
2008 "dot11RSNAEnabled=%s\n"
2009 "dot11RSNAPreauthenticationEnabled=%s\n"
2010 "dot11RSNAConfigVersion=%d\n"
2011 "dot11RSNAConfigPairwiseKeysSupported=5\n"
2012 "dot11RSNAConfigGroupCipherSize=%d\n"
2013 "dot11RSNAConfigPMKLifetime=%d\n"
2014 "dot11RSNAConfigPMKReauthThreshold=%d\n"
2015 "dot11RSNAConfigNumberOfPTKSAReplayCounters=1\n"
2016 "dot11RSNAConfigSATimeout=%d\n",
2017 rsna ? "TRUE" : "FALSE",
2018 rsna ? "TRUE" : "FALSE",
2019 RSN_VERSION,
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07002020 wpa_cipher_key_len(sm->group_cipher) * 8,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002021 sm->dot11RSNAConfigPMKLifetime,
2022 sm->dot11RSNAConfigPMKReauthThreshold,
2023 sm->dot11RSNAConfigSATimeout);
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -08002024 if (os_snprintf_error(buflen, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002025 return 0;
2026 len = ret;
2027
2028 ret = os_snprintf(
2029 buf + len, buflen - len,
2030 "dot11RSNAAuthenticationSuiteSelected=" RSN_SUITE "\n"
2031 "dot11RSNAPairwiseCipherSelected=" RSN_SUITE "\n"
2032 "dot11RSNAGroupCipherSelected=" RSN_SUITE "\n"
2033 "dot11RSNAPMKIDUsed=%s\n"
2034 "dot11RSNAAuthenticationSuiteRequested=" RSN_SUITE "\n"
2035 "dot11RSNAPairwiseCipherRequested=" RSN_SUITE "\n"
2036 "dot11RSNAGroupCipherRequested=" RSN_SUITE "\n"
2037 "dot11RSNAConfigNumberOfGTKSAReplayCounters=0\n"
2038 "dot11RSNA4WayHandshakeFailures=%u\n",
2039 RSN_SUITE_ARG(wpa_key_mgmt_suite(sm)),
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07002040 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)),
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002044 pmkid_txt,
2045 RSN_SUITE_ARG(wpa_key_mgmt_suite(sm)),
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07002046 RSN_SUITE_ARG(wpa_cipher_to_suite(sm->proto,
2047 sm->pairwise_cipher)),
2048 RSN_SUITE_ARG(wpa_cipher_to_suite(sm->proto,
2049 sm->group_cipher)),
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002050 sm->dot11RSNA4WayHandshakeFailures);
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -08002051 if (!os_snprintf_error(buflen - len, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002052 len += ret;
2053
2054 return (int) len;
2055}
2056#endif /* CONFIG_CTRL_IFACE */
2057
2058
2059static void wpa_sm_pmksa_free_cb(struct rsn_pmksa_cache_entry *entry,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002060 void *ctx, enum pmksa_free_reason reason)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002061{
2062 struct wpa_sm *sm = ctx;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002063 int deauth = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002064
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002065 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "RSN: PMKSA cache entry free_cb: "
2066 MACSTR " reason=%d", MAC2STR(entry->aa), reason);
2067
2068 if (sm->cur_pmksa == entry) {
2069 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2070 "RSN: %s current PMKSA entry",
2071 reason == PMKSA_REPLACE ? "replaced" : "removed");
2072 pmksa_cache_clear_current(sm);
2073
2074 /*
2075 * If an entry is simply being replaced, there's no need to
2076 * deauthenticate because it will be immediately re-added.
2077 * This happens when EAP authentication is completed again
2078 * (reauth or failed PMKSA caching attempt).
2079 */
2080 if (reason != PMKSA_REPLACE)
2081 deauth = 1;
2082 }
2083
2084 if (reason == PMKSA_EXPIRE &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002085 (sm->pmk_len == entry->pmk_len &&
2086 os_memcmp(sm->pmk, entry->pmk, sm->pmk_len) == 0)) {
2087 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002088 "RSN: deauthenticating due to expired PMK");
2089 pmksa_cache_clear_current(sm);
2090 deauth = 1;
2091 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002092
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002093 if (deauth) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002094 os_memset(sm->pmk, 0, sizeof(sm->pmk));
2095 wpa_sm_deauthenticate(sm, WLAN_REASON_UNSPECIFIED);
2096 }
2097}
2098
2099
2100/**
2101 * wpa_sm_init - Initialize WPA state machine
2102 * @ctx: Context pointer for callbacks; this needs to be an allocated buffer
2103 * Returns: Pointer to the allocated WPA state machine data
2104 *
2105 * This function is used to allocate a new WPA state machine and the returned
2106 * value is passed to all WPA state machine calls.
2107 */
2108struct wpa_sm * wpa_sm_init(struct wpa_sm_ctx *ctx)
2109{
2110 struct wpa_sm *sm;
2111
2112 sm = os_zalloc(sizeof(*sm));
2113 if (sm == NULL)
2114 return NULL;
2115 dl_list_init(&sm->pmksa_candidates);
2116 sm->renew_snonce = 1;
2117 sm->ctx = ctx;
2118
2119 sm->dot11RSNAConfigPMKLifetime = 43200;
2120 sm->dot11RSNAConfigPMKReauthThreshold = 70;
2121 sm->dot11RSNAConfigSATimeout = 60;
2122
2123 sm->pmksa = pmksa_cache_init(wpa_sm_pmksa_free_cb, sm, sm);
2124 if (sm->pmksa == NULL) {
2125 wpa_msg(sm->ctx->msg_ctx, MSG_ERROR,
2126 "RSN: PMKSA cache initialization failed");
2127 os_free(sm);
2128 return NULL;
2129 }
2130
2131 return sm;
2132}
2133
2134
2135/**
2136 * wpa_sm_deinit - Deinitialize WPA state machine
2137 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2138 */
2139void wpa_sm_deinit(struct wpa_sm *sm)
2140{
2141 if (sm == NULL)
2142 return;
2143 pmksa_cache_deinit(sm->pmksa);
2144 eloop_cancel_timeout(wpa_sm_start_preauth, sm, NULL);
2145 eloop_cancel_timeout(wpa_sm_rekey_ptk, sm, NULL);
2146 os_free(sm->assoc_wpa_ie);
2147 os_free(sm->ap_wpa_ie);
2148 os_free(sm->ap_rsn_ie);
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -08002149 wpa_sm_drop_sa(sm);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002150 os_free(sm->ctx);
2151 peerkey_deinit(sm);
2152#ifdef CONFIG_IEEE80211R
2153 os_free(sm->assoc_resp_ies);
2154#endif /* CONFIG_IEEE80211R */
2155 os_free(sm);
2156}
2157
2158
2159/**
2160 * wpa_sm_notify_assoc - Notify WPA state machine about association
2161 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2162 * @bssid: The BSSID of the new association
2163 *
2164 * This function is called to let WPA state machine know that the connection
2165 * was established.
2166 */
2167void wpa_sm_notify_assoc(struct wpa_sm *sm, const u8 *bssid)
2168{
2169 int clear_ptk = 1;
2170
2171 if (sm == NULL)
2172 return;
2173
2174 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2175 "WPA: Association event - clear replay counter");
2176 os_memcpy(sm->bssid, bssid, ETH_ALEN);
2177 os_memset(sm->rx_replay_counter, 0, WPA_REPLAY_COUNTER_LEN);
2178 sm->rx_replay_counter_set = 0;
2179 sm->renew_snonce = 1;
2180 if (os_memcmp(sm->preauth_bssid, bssid, ETH_ALEN) == 0)
2181 rsn_preauth_deinit(sm);
2182
2183#ifdef CONFIG_IEEE80211R
2184 if (wpa_ft_is_completed(sm)) {
2185 /*
2186 * Clear portValid to kick EAPOL state machine to re-enter
2187 * AUTHENTICATED state to get the EAPOL port Authorized.
2188 */
2189 eapol_sm_notify_portValid(sm->eapol, FALSE);
2190 wpa_supplicant_key_neg_complete(sm, sm->bssid, 1);
2191
2192 /* Prepare for the next transition */
2193 wpa_ft_prepare_auth_request(sm, NULL);
2194
2195 clear_ptk = 0;
2196 }
2197#endif /* CONFIG_IEEE80211R */
2198
2199 if (clear_ptk) {
2200 /*
2201 * IEEE 802.11, 8.4.10: Delete PTK SA on (re)association if
2202 * this is not part of a Fast BSS Transition.
2203 */
2204 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Clear old PTK");
2205 sm->ptk_set = 0;
Dmitry Shmidt61593f02014-04-21 16:27:35 -07002206 os_memset(&sm->ptk, 0, sizeof(sm->ptk));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002207 sm->tptk_set = 0;
Dmitry Shmidt61593f02014-04-21 16:27:35 -07002208 os_memset(&sm->tptk, 0, sizeof(sm->tptk));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002209 }
2210
2211#ifdef CONFIG_TDLS
2212 wpa_tdls_assoc(sm);
2213#endif /* CONFIG_TDLS */
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002214
2215#ifdef CONFIG_P2P
2216 os_memset(sm->p2p_ip_addr, 0, sizeof(sm->p2p_ip_addr));
2217#endif /* CONFIG_P2P */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002218}
2219
2220
2221/**
2222 * wpa_sm_notify_disassoc - Notify WPA state machine about disassociation
2223 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2224 *
2225 * This function is called to let WPA state machine know that the connection
2226 * was lost. This will abort any existing pre-authentication session.
2227 */
2228void wpa_sm_notify_disassoc(struct wpa_sm *sm)
2229{
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002230 peerkey_deinit(sm);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002231 rsn_preauth_deinit(sm);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002232 pmksa_cache_clear_current(sm);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002233 if (wpa_sm_get_state(sm) == WPA_4WAY_HANDSHAKE)
2234 sm->dot11RSNA4WayHandshakeFailures++;
2235#ifdef CONFIG_TDLS
2236 wpa_tdls_disassoc(sm);
2237#endif /* CONFIG_TDLS */
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -08002238
2239 /* Keys are not needed in the WPA state machine anymore */
2240 wpa_sm_drop_sa(sm);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002241}
2242
2243
2244/**
2245 * wpa_sm_set_pmk - Set PMK
2246 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2247 * @pmk: The new PMK
2248 * @pmk_len: The length of the new PMK in bytes
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -08002249 * @bssid: AA to add into PMKSA cache or %NULL to not cache the PMK
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002250 *
2251 * Configure the PMK for WPA state machine.
2252 */
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -08002253void wpa_sm_set_pmk(struct wpa_sm *sm, const u8 *pmk, size_t pmk_len,
2254 const u8 *bssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002255{
2256 if (sm == NULL)
2257 return;
2258
2259 sm->pmk_len = pmk_len;
2260 os_memcpy(sm->pmk, pmk, pmk_len);
2261
2262#ifdef CONFIG_IEEE80211R
2263 /* Set XXKey to be PSK for FT key derivation */
2264 sm->xxkey_len = pmk_len;
2265 os_memcpy(sm->xxkey, pmk, pmk_len);
2266#endif /* CONFIG_IEEE80211R */
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -08002267
2268 if (bssid) {
2269 pmksa_cache_add(sm->pmksa, pmk, pmk_len, NULL, 0,
2270 bssid, sm->own_addr,
2271 sm->network_ctx, sm->key_mgmt);
2272 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002273}
2274
2275
2276/**
2277 * wpa_sm_set_pmk_from_pmksa - Set PMK based on the current PMKSA
2278 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2279 *
2280 * Take the PMK from the current PMKSA into use. If no PMKSA is active, the PMK
2281 * will be cleared.
2282 */
2283void wpa_sm_set_pmk_from_pmksa(struct wpa_sm *sm)
2284{
2285 if (sm == NULL)
2286 return;
2287
2288 if (sm->cur_pmksa) {
2289 sm->pmk_len = sm->cur_pmksa->pmk_len;
2290 os_memcpy(sm->pmk, sm->cur_pmksa->pmk, sm->pmk_len);
2291 } else {
2292 sm->pmk_len = PMK_LEN;
2293 os_memset(sm->pmk, 0, PMK_LEN);
2294 }
2295}
2296
2297
2298/**
2299 * wpa_sm_set_fast_reauth - Set fast reauthentication (EAP) enabled/disabled
2300 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2301 * @fast_reauth: Whether fast reauthentication (EAP) is allowed
2302 */
2303void wpa_sm_set_fast_reauth(struct wpa_sm *sm, int fast_reauth)
2304{
2305 if (sm)
2306 sm->fast_reauth = fast_reauth;
2307}
2308
2309
2310/**
2311 * wpa_sm_set_scard_ctx - Set context pointer for smartcard callbacks
2312 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2313 * @scard_ctx: Context pointer for smartcard related callback functions
2314 */
2315void wpa_sm_set_scard_ctx(struct wpa_sm *sm, void *scard_ctx)
2316{
2317 if (sm == NULL)
2318 return;
2319 sm->scard_ctx = scard_ctx;
2320 if (sm->preauth_eapol)
2321 eapol_sm_register_scard_ctx(sm->preauth_eapol, scard_ctx);
2322}
2323
2324
2325/**
2326 * wpa_sm_set_config - Notification of current configration change
2327 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2328 * @config: Pointer to current network configuration
2329 *
2330 * Notify WPA state machine that configuration has changed. config will be
2331 * stored as a backpointer to network configuration. This can be %NULL to clear
2332 * the stored pointed.
2333 */
2334void wpa_sm_set_config(struct wpa_sm *sm, struct rsn_supp_config *config)
2335{
2336 if (!sm)
2337 return;
2338
2339 if (config) {
2340 sm->network_ctx = config->network_ctx;
2341 sm->peerkey_enabled = config->peerkey_enabled;
2342 sm->allowed_pairwise_cipher = config->allowed_pairwise_cipher;
2343 sm->proactive_key_caching = config->proactive_key_caching;
2344 sm->eap_workaround = config->eap_workaround;
2345 sm->eap_conf_ctx = config->eap_conf_ctx;
2346 if (config->ssid) {
2347 os_memcpy(sm->ssid, config->ssid, config->ssid_len);
2348 sm->ssid_len = config->ssid_len;
2349 } else
2350 sm->ssid_len = 0;
2351 sm->wpa_ptk_rekey = config->wpa_ptk_rekey;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002352 sm->p2p = config->p2p;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002353 } else {
2354 sm->network_ctx = NULL;
2355 sm->peerkey_enabled = 0;
2356 sm->allowed_pairwise_cipher = 0;
2357 sm->proactive_key_caching = 0;
2358 sm->eap_workaround = 0;
2359 sm->eap_conf_ctx = NULL;
2360 sm->ssid_len = 0;
2361 sm->wpa_ptk_rekey = 0;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002362 sm->p2p = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002363 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002364}
2365
2366
2367/**
2368 * wpa_sm_set_own_addr - Set own MAC address
2369 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2370 * @addr: Own MAC address
2371 */
2372void wpa_sm_set_own_addr(struct wpa_sm *sm, const u8 *addr)
2373{
2374 if (sm)
2375 os_memcpy(sm->own_addr, addr, ETH_ALEN);
2376}
2377
2378
2379/**
2380 * wpa_sm_set_ifname - Set network interface name
2381 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2382 * @ifname: Interface name
2383 * @bridge_ifname: Optional bridge interface name (for pre-auth)
2384 */
2385void wpa_sm_set_ifname(struct wpa_sm *sm, const char *ifname,
2386 const char *bridge_ifname)
2387{
2388 if (sm) {
2389 sm->ifname = ifname;
2390 sm->bridge_ifname = bridge_ifname;
2391 }
2392}
2393
2394
2395/**
2396 * wpa_sm_set_eapol - Set EAPOL state machine pointer
2397 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2398 * @eapol: Pointer to EAPOL state machine allocated with eapol_sm_init()
2399 */
2400void wpa_sm_set_eapol(struct wpa_sm *sm, struct eapol_sm *eapol)
2401{
2402 if (sm)
2403 sm->eapol = eapol;
2404}
2405
2406
2407/**
2408 * wpa_sm_set_param - Set WPA state machine parameters
2409 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2410 * @param: Parameter field
2411 * @value: Parameter value
2412 * Returns: 0 on success, -1 on failure
2413 */
2414int wpa_sm_set_param(struct wpa_sm *sm, enum wpa_sm_conf_params param,
2415 unsigned int value)
2416{
2417 int ret = 0;
2418
2419 if (sm == NULL)
2420 return -1;
2421
2422 switch (param) {
2423 case RSNA_PMK_LIFETIME:
2424 if (value > 0)
2425 sm->dot11RSNAConfigPMKLifetime = value;
2426 else
2427 ret = -1;
2428 break;
2429 case RSNA_PMK_REAUTH_THRESHOLD:
2430 if (value > 0 && value <= 100)
2431 sm->dot11RSNAConfigPMKReauthThreshold = value;
2432 else
2433 ret = -1;
2434 break;
2435 case RSNA_SA_TIMEOUT:
2436 if (value > 0)
2437 sm->dot11RSNAConfigSATimeout = value;
2438 else
2439 ret = -1;
2440 break;
2441 case WPA_PARAM_PROTO:
2442 sm->proto = value;
2443 break;
2444 case WPA_PARAM_PAIRWISE:
2445 sm->pairwise_cipher = value;
2446 break;
2447 case WPA_PARAM_GROUP:
2448 sm->group_cipher = value;
2449 break;
2450 case WPA_PARAM_KEY_MGMT:
2451 sm->key_mgmt = value;
2452 break;
2453#ifdef CONFIG_IEEE80211W
2454 case WPA_PARAM_MGMT_GROUP:
2455 sm->mgmt_group_cipher = value;
2456 break;
2457#endif /* CONFIG_IEEE80211W */
2458 case WPA_PARAM_RSN_ENABLED:
2459 sm->rsn_enabled = value;
2460 break;
2461 case WPA_PARAM_MFP:
2462 sm->mfp = value;
2463 break;
2464 default:
2465 break;
2466 }
2467
2468 return ret;
2469}
2470
2471
2472/**
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002473 * wpa_sm_get_status - Get WPA state machine
2474 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2475 * @buf: Buffer for status information
2476 * @buflen: Maximum buffer length
2477 * @verbose: Whether to include verbose status information
2478 * Returns: Number of bytes written to buf.
2479 *
2480 * Query WPA state machine for status information. This function fills in
2481 * a text area with current status information. If the buffer (buf) is not
2482 * large enough, status information will be truncated to fit the buffer.
2483 */
2484int wpa_sm_get_status(struct wpa_sm *sm, char *buf, size_t buflen,
2485 int verbose)
2486{
2487 char *pos = buf, *end = buf + buflen;
2488 int ret;
2489
2490 ret = os_snprintf(pos, end - pos,
2491 "pairwise_cipher=%s\n"
2492 "group_cipher=%s\n"
2493 "key_mgmt=%s\n",
2494 wpa_cipher_txt(sm->pairwise_cipher),
2495 wpa_cipher_txt(sm->group_cipher),
2496 wpa_key_mgmt_txt(sm->key_mgmt, sm->proto));
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -08002497 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002498 return pos - buf;
2499 pos += ret;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002500
2501 if (sm->mfp != NO_MGMT_FRAME_PROTECTION && sm->ap_rsn_ie) {
2502 struct wpa_ie_data rsn;
2503 if (wpa_parse_wpa_ie_rsn(sm->ap_rsn_ie, sm->ap_rsn_ie_len, &rsn)
2504 >= 0 &&
2505 rsn.capabilities & (WPA_CAPABILITY_MFPR |
2506 WPA_CAPABILITY_MFPC)) {
2507 ret = os_snprintf(pos, end - pos, "pmf=%d\n",
2508 (rsn.capabilities &
2509 WPA_CAPABILITY_MFPR) ? 2 : 1);
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -08002510 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002511 return pos - buf;
2512 pos += ret;
2513 }
2514 }
2515
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002516 return pos - buf;
2517}
2518
2519
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07002520int wpa_sm_pmf_enabled(struct wpa_sm *sm)
2521{
2522 struct wpa_ie_data rsn;
2523
2524 if (sm->mfp == NO_MGMT_FRAME_PROTECTION || !sm->ap_rsn_ie)
2525 return 0;
2526
2527 if (wpa_parse_wpa_ie_rsn(sm->ap_rsn_ie, sm->ap_rsn_ie_len, &rsn) >= 0 &&
2528 rsn.capabilities & (WPA_CAPABILITY_MFPR | WPA_CAPABILITY_MFPC))
2529 return 1;
2530
2531 return 0;
2532}
2533
2534
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002535/**
2536 * wpa_sm_set_assoc_wpa_ie_default - Generate own WPA/RSN IE from configuration
2537 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2538 * @wpa_ie: Pointer to buffer for WPA/RSN IE
2539 * @wpa_ie_len: Pointer to the length of the wpa_ie buffer
2540 * Returns: 0 on success, -1 on failure
2541 */
2542int wpa_sm_set_assoc_wpa_ie_default(struct wpa_sm *sm, u8 *wpa_ie,
2543 size_t *wpa_ie_len)
2544{
2545 int res;
2546
2547 if (sm == NULL)
2548 return -1;
2549
2550 res = wpa_gen_wpa_ie(sm, wpa_ie, *wpa_ie_len);
2551 if (res < 0)
2552 return -1;
2553 *wpa_ie_len = res;
2554
2555 wpa_hexdump(MSG_DEBUG, "WPA: Set own WPA IE default",
2556 wpa_ie, *wpa_ie_len);
2557
2558 if (sm->assoc_wpa_ie == NULL) {
2559 /*
2560 * Make a copy of the WPA/RSN IE so that 4-Way Handshake gets
2561 * the correct version of the IE even if PMKSA caching is
2562 * aborted (which would remove PMKID from IE generation).
2563 */
2564 sm->assoc_wpa_ie = os_malloc(*wpa_ie_len);
2565 if (sm->assoc_wpa_ie == NULL)
2566 return -1;
2567
2568 os_memcpy(sm->assoc_wpa_ie, wpa_ie, *wpa_ie_len);
2569 sm->assoc_wpa_ie_len = *wpa_ie_len;
2570 }
2571
2572 return 0;
2573}
2574
2575
2576/**
2577 * wpa_sm_set_assoc_wpa_ie - Set own WPA/RSN IE from (Re)AssocReq
2578 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2579 * @ie: Pointer to IE data (starting from id)
2580 * @len: IE length
2581 * Returns: 0 on success, -1 on failure
2582 *
2583 * Inform WPA state machine about the WPA/RSN IE used in (Re)Association
2584 * Request frame. The IE will be used to override the default value generated
2585 * with wpa_sm_set_assoc_wpa_ie_default().
2586 */
2587int wpa_sm_set_assoc_wpa_ie(struct wpa_sm *sm, const u8 *ie, size_t len)
2588{
2589 if (sm == NULL)
2590 return -1;
2591
2592 os_free(sm->assoc_wpa_ie);
2593 if (ie == NULL || len == 0) {
2594 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2595 "WPA: clearing own WPA/RSN IE");
2596 sm->assoc_wpa_ie = NULL;
2597 sm->assoc_wpa_ie_len = 0;
2598 } else {
2599 wpa_hexdump(MSG_DEBUG, "WPA: set own WPA/RSN IE", ie, len);
2600 sm->assoc_wpa_ie = os_malloc(len);
2601 if (sm->assoc_wpa_ie == NULL)
2602 return -1;
2603
2604 os_memcpy(sm->assoc_wpa_ie, ie, len);
2605 sm->assoc_wpa_ie_len = len;
2606 }
2607
2608 return 0;
2609}
2610
2611
2612/**
2613 * wpa_sm_set_ap_wpa_ie - Set AP WPA IE from Beacon/ProbeResp
2614 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2615 * @ie: Pointer to IE data (starting from id)
2616 * @len: IE length
2617 * Returns: 0 on success, -1 on failure
2618 *
2619 * Inform WPA state machine about the WPA IE used in Beacon / Probe Response
2620 * frame.
2621 */
2622int wpa_sm_set_ap_wpa_ie(struct wpa_sm *sm, const u8 *ie, size_t len)
2623{
2624 if (sm == NULL)
2625 return -1;
2626
2627 os_free(sm->ap_wpa_ie);
2628 if (ie == NULL || len == 0) {
2629 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2630 "WPA: clearing AP WPA IE");
2631 sm->ap_wpa_ie = NULL;
2632 sm->ap_wpa_ie_len = 0;
2633 } else {
2634 wpa_hexdump(MSG_DEBUG, "WPA: set AP WPA IE", ie, len);
2635 sm->ap_wpa_ie = os_malloc(len);
2636 if (sm->ap_wpa_ie == NULL)
2637 return -1;
2638
2639 os_memcpy(sm->ap_wpa_ie, ie, len);
2640 sm->ap_wpa_ie_len = len;
2641 }
2642
2643 return 0;
2644}
2645
2646
2647/**
2648 * wpa_sm_set_ap_rsn_ie - Set AP RSN IE from Beacon/ProbeResp
2649 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2650 * @ie: Pointer to IE data (starting from id)
2651 * @len: IE length
2652 * Returns: 0 on success, -1 on failure
2653 *
2654 * Inform WPA state machine about the RSN IE used in Beacon / Probe Response
2655 * frame.
2656 */
2657int wpa_sm_set_ap_rsn_ie(struct wpa_sm *sm, const u8 *ie, size_t len)
2658{
2659 if (sm == NULL)
2660 return -1;
2661
2662 os_free(sm->ap_rsn_ie);
2663 if (ie == NULL || len == 0) {
2664 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2665 "WPA: clearing AP RSN IE");
2666 sm->ap_rsn_ie = NULL;
2667 sm->ap_rsn_ie_len = 0;
2668 } else {
2669 wpa_hexdump(MSG_DEBUG, "WPA: set AP RSN IE", ie, len);
2670 sm->ap_rsn_ie = os_malloc(len);
2671 if (sm->ap_rsn_ie == NULL)
2672 return -1;
2673
2674 os_memcpy(sm->ap_rsn_ie, ie, len);
2675 sm->ap_rsn_ie_len = len;
2676 }
2677
2678 return 0;
2679}
2680
2681
2682/**
2683 * wpa_sm_parse_own_wpa_ie - Parse own WPA/RSN IE
2684 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2685 * @data: Pointer to data area for parsing results
2686 * Returns: 0 on success, -1 if IE is not known, or -2 on parsing failure
2687 *
2688 * Parse the contents of the own WPA or RSN IE from (Re)AssocReq and write the
2689 * parsed data into data.
2690 */
2691int wpa_sm_parse_own_wpa_ie(struct wpa_sm *sm, struct wpa_ie_data *data)
2692{
2693 if (sm == NULL)
2694 return -1;
2695
2696 if (sm->assoc_wpa_ie == NULL) {
2697 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2698 "WPA: No WPA/RSN IE available from association info");
2699 return -1;
2700 }
2701 if (wpa_parse_wpa_ie(sm->assoc_wpa_ie, sm->assoc_wpa_ie_len, data))
2702 return -2;
2703 return 0;
2704}
2705
2706
2707int wpa_sm_pmksa_cache_list(struct wpa_sm *sm, char *buf, size_t len)
2708{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002709 return pmksa_cache_list(sm->pmksa, buf, len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002710}
2711
2712
2713void wpa_sm_drop_sa(struct wpa_sm *sm)
2714{
2715 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Clear old PMK and PTK");
2716 sm->ptk_set = 0;
2717 sm->tptk_set = 0;
2718 os_memset(sm->pmk, 0, sizeof(sm->pmk));
2719 os_memset(&sm->ptk, 0, sizeof(sm->ptk));
2720 os_memset(&sm->tptk, 0, sizeof(sm->tptk));
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -08002721#ifdef CONFIG_IEEE80211R
2722 os_memset(sm->xxkey, 0, sizeof(sm->xxkey));
2723 os_memset(sm->pmk_r0, 0, sizeof(sm->pmk_r0));
2724 os_memset(sm->pmk_r1, 0, sizeof(sm->pmk_r1));
2725#endif /* CONFIG_IEEE80211R */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002726}
2727
2728
2729int wpa_sm_has_ptk(struct wpa_sm *sm)
2730{
2731 if (sm == NULL)
2732 return 0;
2733 return sm->ptk_set;
2734}
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002735
2736
2737void wpa_sm_update_replay_ctr(struct wpa_sm *sm, const u8 *replay_ctr)
2738{
2739 os_memcpy(sm->rx_replay_counter, replay_ctr, WPA_REPLAY_COUNTER_LEN);
2740}
2741
2742
2743void wpa_sm_pmksa_cache_flush(struct wpa_sm *sm, void *network_ctx)
2744{
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07002745 pmksa_cache_flush(sm->pmksa, network_ctx, NULL, 0);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002746}
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002747
2748
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002749#ifdef CONFIG_WNM
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002750int wpa_wnmsleep_install_key(struct wpa_sm *sm, u8 subelem_id, u8 *buf)
2751{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002752 u16 keyinfo;
2753 u8 keylen; /* plaintext key len */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002754 u8 *key_rsc;
2755
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002756 if (subelem_id == WNM_SLEEP_SUBELEM_GTK) {
Dmitry Shmidt61593f02014-04-21 16:27:35 -07002757 struct wpa_gtk_data gd;
2758
2759 os_memset(&gd, 0, sizeof(gd));
2760 keylen = wpa_cipher_key_len(sm->group_cipher);
2761 gd.key_rsc_len = wpa_cipher_rsc_len(sm->group_cipher);
2762 gd.alg = wpa_cipher_to_alg(sm->group_cipher);
2763 if (gd.alg == WPA_ALG_NONE) {
2764 wpa_printf(MSG_DEBUG, "Unsupported group cipher suite");
2765 return -1;
2766 }
2767
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002768 key_rsc = buf + 5;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002769 keyinfo = WPA_GET_LE16(buf + 2);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002770 gd.gtk_len = keylen;
2771 if (gd.gtk_len != buf[4]) {
2772 wpa_printf(MSG_DEBUG, "GTK len mismatch len %d vs %d",
2773 gd.gtk_len, buf[4]);
2774 return -1;
2775 }
2776 gd.keyidx = keyinfo & 0x03; /* B0 - B1 */
2777 gd.tx = wpa_supplicant_gtk_tx_bit_workaround(
2778 sm, !!(keyinfo & WPA_KEY_INFO_TXRX));
2779
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002780 os_memcpy(gd.gtk, buf + 13, gd.gtk_len);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002781
2782 wpa_hexdump_key(MSG_DEBUG, "Install GTK (WNM SLEEP)",
2783 gd.gtk, gd.gtk_len);
2784 if (wpa_supplicant_install_gtk(sm, &gd, key_rsc)) {
Dmitry Shmidt61593f02014-04-21 16:27:35 -07002785 os_memset(&gd, 0, sizeof(gd));
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002786 wpa_printf(MSG_DEBUG, "Failed to install the GTK in "
2787 "WNM mode");
2788 return -1;
2789 }
Dmitry Shmidt61593f02014-04-21 16:27:35 -07002790 os_memset(&gd, 0, sizeof(gd));
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002791#ifdef CONFIG_IEEE80211W
2792 } else if (subelem_id == WNM_SLEEP_SUBELEM_IGTK) {
Dmitry Shmidt61593f02014-04-21 16:27:35 -07002793 struct wpa_igtk_kde igd;
2794 u16 keyidx;
2795
2796 os_memset(&igd, 0, sizeof(igd));
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002797 keylen = wpa_cipher_key_len(sm->mgmt_group_cipher);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002798 os_memcpy(igd.keyid, buf + 2, 2);
2799 os_memcpy(igd.pn, buf + 4, 6);
2800
2801 keyidx = WPA_GET_LE16(igd.keyid);
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002802 os_memcpy(igd.igtk, buf + 10, keylen);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002803
2804 wpa_hexdump_key(MSG_DEBUG, "Install IGTK (WNM SLEEP)",
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002805 igd.igtk, keylen);
2806 if (wpa_sm_set_key(sm, wpa_cipher_to_alg(sm->mgmt_group_cipher),
2807 broadcast_ether_addr,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002808 keyidx, 0, igd.pn, sizeof(igd.pn),
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002809 igd.igtk, keylen) < 0) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002810 wpa_printf(MSG_DEBUG, "Failed to install the IGTK in "
2811 "WNM mode");
Dmitry Shmidt61593f02014-04-21 16:27:35 -07002812 os_memset(&igd, 0, sizeof(igd));
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002813 return -1;
2814 }
Dmitry Shmidt61593f02014-04-21 16:27:35 -07002815 os_memset(&igd, 0, sizeof(igd));
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002816#endif /* CONFIG_IEEE80211W */
2817 } else {
2818 wpa_printf(MSG_DEBUG, "Unknown element id");
2819 return -1;
2820 }
2821
2822 return 0;
2823}
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002824#endif /* CONFIG_WNM */
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002825
2826
2827#ifdef CONFIG_PEERKEY
2828int wpa_sm_rx_eapol_peerkey(struct wpa_sm *sm, const u8 *src_addr,
2829 const u8 *buf, size_t len)
2830{
2831 struct wpa_peerkey *peerkey;
2832
2833 for (peerkey = sm->peerkey; peerkey; peerkey = peerkey->next) {
2834 if (os_memcmp(peerkey->addr, src_addr, ETH_ALEN) == 0)
2835 break;
2836 }
2837
2838 if (!peerkey)
2839 return 0;
2840
2841 wpa_sm_rx_eapol(sm, src_addr, buf, len);
2842
2843 return 1;
2844}
2845#endif /* CONFIG_PEERKEY */
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002846
2847
2848#ifdef CONFIG_P2P
2849
2850int wpa_sm_get_p2p_ip_addr(struct wpa_sm *sm, u8 *buf)
2851{
2852 if (sm == NULL || WPA_GET_BE32(sm->p2p_ip_addr) == 0)
2853 return -1;
2854 os_memcpy(buf, sm->p2p_ip_addr, 3 * 4);
2855 return 0;
2856}
2857
2858#endif /* CONFIG_P2P */
Dmitry Shmidtfb45fd52015-01-05 13:08:17 -08002859
2860
2861void wpa_sm_set_rx_replay_ctr(struct wpa_sm *sm, const u8 *rx_replay_counter)
2862{
2863 if (rx_replay_counter == NULL)
2864 return;
2865
2866 os_memcpy(sm->rx_replay_counter, rx_replay_counter,
2867 WPA_REPLAY_COUNTER_LEN);
2868 sm->rx_replay_counter_set = 1;
2869 wpa_printf(MSG_DEBUG, "Updated key replay counter");
2870}
2871
2872
2873void wpa_sm_set_ptk_kck_kek(struct wpa_sm *sm, const u8 *ptk_kck,
2874 const u8 *ptk_kek)
2875{
2876 if (ptk_kck) {
2877 os_memcpy(sm->ptk.kck, ptk_kck, 16);
2878 wpa_printf(MSG_DEBUG, "Updated PTK KCK");
2879 }
2880 if (ptk_kek) {
2881 os_memcpy(sm->ptk.kek, ptk_kek, 16);
2882 wpa_printf(MSG_DEBUG, "Updated PTK KEK");
2883 }
2884 sm->ptk_set = 1;
2885}