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