blob: bbb7ad9e8da75006808f1c04b0e04af1ecba5b37 [file] [log] [blame]
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001/*
2 * WPA Supplicant - WPA state machine and EAPOL-Key processing
Dmitry Shmidt807291d2015-01-27 13:40:23 -08003 * Copyright (c) 2003-2015, Jouni Malinen <j@w1.fi>
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004 * Copyright(c) 2015 Intel Deutschland GmbH
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07005 *
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08006 * This software may be distributed under the terms of the BSD license.
7 * See README for more details.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07008 */
9
10#include "includes.h"
11
12#include "common.h"
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -080013#include "crypto/aes.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070014#include "crypto/aes_wrap.h"
15#include "crypto/crypto.h"
16#include "crypto/random.h"
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -080017#include "crypto/aes_siv.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070018#include "common/ieee802_11_defs.h"
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -080019#include "common/ieee802_11_common.h"
Paul Stewart092955c2017-02-06 09:13:09 -080020#include "eap_common/eap_defs.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070021#include "eapol_supp/eapol_supp_sm.h"
22#include "wpa.h"
23#include "eloop.h"
24#include "preauth.h"
25#include "pmksa_cache.h"
26#include "wpa_i.h"
27#include "wpa_ie.h"
28#include "peerkey.h"
29
30
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080031static const u8 null_rsc[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
32
33
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070034/**
35 * wpa_eapol_key_send - Send WPA/RSN EAPOL-Key message
36 * @sm: Pointer to WPA state machine data from wpa_sm_init()
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -080037 * @ptk: PTK for Key Confirmation/Encryption Key
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070038 * @ver: Version field from Key Info
39 * @dest: Destination address for the frame
40 * @proto: Ethertype (usually ETH_P_EAPOL)
41 * @msg: EAPOL-Key message
42 * @msg_len: Length of message
43 * @key_mic: Pointer to the buffer to which the EAPOL-Key MIC is written
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080044 * Returns: >= 0 on success, < 0 on failure
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070045 */
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -080046int wpa_eapol_key_send(struct wpa_sm *sm, struct wpa_ptk *ptk,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080047 int ver, const u8 *dest, u16 proto,
48 u8 *msg, size_t msg_len, u8 *key_mic)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070049{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080050 int ret = -1;
Dmitry Shmidt807291d2015-01-27 13:40:23 -080051 size_t mic_len = wpa_mic_len(sm->key_mgmt);
52
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070053 if (is_zero_ether_addr(dest) && is_zero_ether_addr(sm->bssid)) {
54 /*
55 * Association event was not yet received; try to fetch
56 * BSSID from the driver.
57 */
58 if (wpa_sm_get_bssid(sm, sm->bssid) < 0) {
59 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
60 "WPA: Failed to read BSSID for "
61 "EAPOL-Key destination address");
62 } else {
63 dest = sm->bssid;
64 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
65 "WPA: Use BSSID (" MACSTR
66 ") as the destination for EAPOL-Key",
67 MAC2STR(dest));
68 }
69 }
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -080070
71 if (mic_len) {
72 if (key_mic && (!ptk || !ptk->kck_len))
73 goto out;
74
75 if (key_mic &&
76 wpa_eapol_key_mic(ptk->kck, ptk->kck_len, sm->key_mgmt, ver,
77 msg, msg_len, key_mic)) {
78 wpa_msg(sm->ctx->msg_ctx, MSG_ERROR,
79 "WPA: Failed to generate EAPOL-Key version %d key_mgmt 0x%x MIC",
80 ver, sm->key_mgmt);
81 goto out;
82 }
Dmitry Shmidt29333592017-01-09 12:27:11 -080083 if (ptk)
84 wpa_hexdump_key(MSG_DEBUG, "WPA: KCK",
85 ptk->kck, ptk->kck_len);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -080086 wpa_hexdump(MSG_DEBUG, "WPA: Derived Key MIC",
87 key_mic, mic_len);
88 } else {
89#ifdef CONFIG_FILS
90 /* AEAD cipher - Key MIC field not used */
91 struct ieee802_1x_hdr *s_hdr, *hdr;
92 struct wpa_eapol_key *s_key, *key;
93 u8 *buf, *s_key_data, *key_data;
94 size_t buf_len = msg_len + AES_BLOCK_SIZE;
95 size_t key_data_len;
96 u16 eapol_len;
97 const u8 *aad[1];
98 size_t aad_len[1];
99
100 if (!ptk || !ptk->kek_len)
101 goto out;
102
103 key_data_len = msg_len - sizeof(struct ieee802_1x_hdr) -
104 sizeof(struct wpa_eapol_key) - 2;
105
106 buf = os_malloc(buf_len);
107 if (!buf)
108 goto out;
109
110 os_memcpy(buf, msg, msg_len);
111 hdr = (struct ieee802_1x_hdr *) buf;
112 key = (struct wpa_eapol_key *) (hdr + 1);
113 key_data = ((u8 *) (key + 1)) + 2;
114
115 /* Update EAPOL header to include AES-SIV overhead */
116 eapol_len = be_to_host16(hdr->length);
117 eapol_len += AES_BLOCK_SIZE;
118 hdr->length = host_to_be16(eapol_len);
119
120 /* Update Key Data Length field to include AES-SIV overhead */
121 WPA_PUT_BE16((u8 *) (key + 1), AES_BLOCK_SIZE + key_data_len);
122
123 s_hdr = (struct ieee802_1x_hdr *) msg;
124 s_key = (struct wpa_eapol_key *) (s_hdr + 1);
125 s_key_data = ((u8 *) (s_key + 1)) + 2;
126
127 wpa_hexdump_key(MSG_DEBUG, "WPA: Plaintext Key Data",
128 s_key_data, key_data_len);
129
130 wpa_hexdump_key(MSG_DEBUG, "WPA: KEK", ptk->kek, ptk->kek_len);
131 /* AES-SIV AAD from EAPOL protocol version field (inclusive) to
132 * to Key Data (exclusive). */
133 aad[0] = buf;
134 aad_len[0] = key_data - buf;
135 if (aes_siv_encrypt(ptk->kek, ptk->kek_len,
136 s_key_data, key_data_len,
137 1, aad, aad_len, key_data) < 0) {
138 os_free(buf);
139 goto out;
140 }
141
142 wpa_hexdump(MSG_DEBUG, "WPA: Encrypted Key Data from SIV",
143 key_data, AES_BLOCK_SIZE + key_data_len);
144
145 os_free(msg);
146 msg = buf;
147 msg_len = buf_len;
148#else /* CONFIG_FILS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700149 goto out;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800150#endif /* CONFIG_FILS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700151 }
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800152
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700153 wpa_hexdump(MSG_MSGDUMP, "WPA: TX EAPOL-Key", msg, msg_len);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800154 ret = wpa_sm_ether_send(sm, dest, proto, msg, msg_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700155 eapol_sm_notify_tx_eapol_key(sm->eapol);
156out:
157 os_free(msg);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800158 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700159}
160
161
162/**
163 * wpa_sm_key_request - Send EAPOL-Key Request
164 * @sm: Pointer to WPA state machine data from wpa_sm_init()
165 * @error: Indicate whether this is an Michael MIC error report
166 * @pairwise: 1 = error report for pairwise packet, 0 = for group packet
167 *
168 * Send an EAPOL-Key Request to the current authenticator. This function is
169 * used to request rekeying and it is usually called when a local Michael MIC
170 * failure is detected.
171 */
172void wpa_sm_key_request(struct wpa_sm *sm, int error, int pairwise)
173{
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800174 size_t mic_len, hdrlen, rlen;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700175 struct wpa_eapol_key *reply;
176 int key_info, ver;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800177 u8 bssid[ETH_ALEN], *rbuf, *key_mic, *mic;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700178
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800179 if (sm->key_mgmt == WPA_KEY_MGMT_OSEN ||
180 wpa_key_mgmt_suite_b(sm->key_mgmt))
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800181 ver = WPA_KEY_INFO_TYPE_AKM_DEFINED;
182 else if (wpa_key_mgmt_ft(sm->key_mgmt) ||
183 wpa_key_mgmt_sha256(sm->key_mgmt))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700184 ver = WPA_KEY_INFO_TYPE_AES_128_CMAC;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700185 else if (sm->pairwise_cipher != WPA_CIPHER_TKIP)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700186 ver = WPA_KEY_INFO_TYPE_HMAC_SHA1_AES;
187 else
188 ver = WPA_KEY_INFO_TYPE_HMAC_MD5_RC4;
189
190 if (wpa_sm_get_bssid(sm, bssid) < 0) {
191 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
192 "Failed to read BSSID for EAPOL-Key request");
193 return;
194 }
195
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800196 mic_len = wpa_mic_len(sm->key_mgmt);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800197 hdrlen = sizeof(*reply) + mic_len + 2;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700198 rbuf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_KEY, NULL,
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800199 hdrlen, &rlen, (void *) &reply);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700200 if (rbuf == NULL)
201 return;
202
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800203 reply->type = (sm->proto == WPA_PROTO_RSN ||
204 sm->proto == WPA_PROTO_OSEN) ?
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700205 EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA;
206 key_info = WPA_KEY_INFO_REQUEST | ver;
207 if (sm->ptk_set)
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800208 key_info |= WPA_KEY_INFO_SECURE;
209 if (sm->ptk_set && mic_len)
210 key_info |= WPA_KEY_INFO_MIC;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700211 if (error)
212 key_info |= WPA_KEY_INFO_ERROR;
213 if (pairwise)
214 key_info |= WPA_KEY_INFO_KEY_TYPE;
215 WPA_PUT_BE16(reply->key_info, key_info);
216 WPA_PUT_BE16(reply->key_length, 0);
217 os_memcpy(reply->replay_counter, sm->request_counter,
218 WPA_REPLAY_COUNTER_LEN);
219 inc_byte_array(sm->request_counter, WPA_REPLAY_COUNTER_LEN);
220
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800221 mic = (u8 *) (reply + 1);
222 WPA_PUT_BE16(mic + mic_len, 0);
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800223 if (!(key_info & WPA_KEY_INFO_MIC))
224 key_mic = NULL;
225 else
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800226 key_mic = mic;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700227
228 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
229 "WPA: Sending EAPOL-Key Request (error=%d "
230 "pairwise=%d ptk_set=%d len=%lu)",
231 error, pairwise, sm->ptk_set, (unsigned long) rlen);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800232 wpa_eapol_key_send(sm, &sm->ptk, ver, bssid, ETH_P_EAPOL, rbuf, rlen,
233 key_mic);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700234}
235
236
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800237static void wpa_supplicant_key_mgmt_set_pmk(struct wpa_sm *sm)
238{
239#ifdef CONFIG_IEEE80211R
240 if (sm->key_mgmt == WPA_KEY_MGMT_FT_IEEE8021X) {
241 if (wpa_sm_key_mgmt_set_pmk(sm, sm->xxkey, sm->xxkey_len))
242 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
243 "RSN: Cannot set low order 256 bits of MSK for key management offload");
244 } else {
245#endif /* CONFIG_IEEE80211R */
246 if (wpa_sm_key_mgmt_set_pmk(sm, sm->pmk, sm->pmk_len))
247 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
248 "RSN: Cannot set PMK for key management offload");
249#ifdef CONFIG_IEEE80211R
250 }
251#endif /* CONFIG_IEEE80211R */
252}
253
254
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700255static int wpa_supplicant_get_pmk(struct wpa_sm *sm,
256 const unsigned char *src_addr,
257 const u8 *pmkid)
258{
259 int abort_cached = 0;
260
261 if (pmkid && !sm->cur_pmksa) {
262 /* When using drivers that generate RSN IE, wpa_supplicant may
263 * not have enough time to get the association information
264 * event before receiving this 1/4 message, so try to find a
265 * matching PMKSA cache entry here. */
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800266 sm->cur_pmksa = pmksa_cache_get(sm->pmksa, src_addr, pmkid,
267 NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700268 if (sm->cur_pmksa) {
269 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
270 "RSN: found matching PMKID from PMKSA cache");
271 } else {
272 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
273 "RSN: no matching PMKID found");
274 abort_cached = 1;
275 }
276 }
277
278 if (pmkid && sm->cur_pmksa &&
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700279 os_memcmp_const(pmkid, sm->cur_pmksa->pmkid, PMKID_LEN) == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700280 wpa_hexdump(MSG_DEBUG, "RSN: matched PMKID", pmkid, PMKID_LEN);
281 wpa_sm_set_pmk_from_pmksa(sm);
282 wpa_hexdump_key(MSG_DEBUG, "RSN: PMK from PMKSA cache",
283 sm->pmk, sm->pmk_len);
284 eapol_sm_notify_cached(sm->eapol);
285#ifdef CONFIG_IEEE80211R
286 sm->xxkey_len = 0;
287#endif /* CONFIG_IEEE80211R */
288 } else if (wpa_key_mgmt_wpa_ieee8021x(sm->key_mgmt) && sm->eapol) {
289 int res, pmk_len;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800290
Dmitry Shmidtebd93af2017-02-21 13:40:44 -0800291 if (wpa_key_mgmt_sha384(sm->key_mgmt))
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800292 pmk_len = PMK_LEN_SUITE_B_192;
293 else
294 pmk_len = PMK_LEN;
295 res = eapol_sm_get_key(sm->eapol, sm->pmk, pmk_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700296 if (res) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800297 if (pmk_len == PMK_LEN) {
298 /*
299 * EAP-LEAP is an exception from other EAP
300 * methods: it uses only 16-byte PMK.
301 */
302 res = eapol_sm_get_key(sm->eapol, sm->pmk, 16);
303 pmk_len = 16;
304 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700305 } else {
306#ifdef CONFIG_IEEE80211R
307 u8 buf[2 * PMK_LEN];
308 if (eapol_sm_get_key(sm->eapol, buf, 2 * PMK_LEN) == 0)
309 {
310 os_memcpy(sm->xxkey, buf + PMK_LEN, PMK_LEN);
311 sm->xxkey_len = PMK_LEN;
312 os_memset(buf, 0, sizeof(buf));
313 }
314#endif /* CONFIG_IEEE80211R */
315 }
316 if (res == 0) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700317 struct rsn_pmksa_cache_entry *sa = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700318 wpa_hexdump_key(MSG_DEBUG, "WPA: PMK from EAPOL state "
319 "machines", sm->pmk, pmk_len);
320 sm->pmk_len = pmk_len;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800321 wpa_supplicant_key_mgmt_set_pmk(sm);
Dmitry Shmidtc55524a2011-07-07 11:18:38 -0700322 if (sm->proto == WPA_PROTO_RSN &&
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800323 !wpa_key_mgmt_suite_b(sm->key_mgmt) &&
Dmitry Shmidtc55524a2011-07-07 11:18:38 -0700324 !wpa_key_mgmt_ft(sm->key_mgmt)) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700325 sa = pmksa_cache_add(sm->pmksa,
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800326 sm->pmk, pmk_len, NULL,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800327 NULL, 0,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700328 src_addr, sm->own_addr,
329 sm->network_ctx,
330 sm->key_mgmt);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700331 }
332 if (!sm->cur_pmksa && pmkid &&
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800333 pmksa_cache_get(sm->pmksa, src_addr, pmkid, NULL))
334 {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700335 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
336 "RSN: the new PMK matches with the "
337 "PMKID");
338 abort_cached = 0;
Jouni Malinen6ec30382015-07-08 20:48:18 +0300339 } else if (sa && !sm->cur_pmksa && pmkid) {
340 /*
341 * It looks like the authentication server
342 * derived mismatching MSK. This should not
343 * really happen, but bugs happen.. There is not
344 * much we can do here without knowing what
345 * exactly caused the server to misbehave.
346 */
Dmitry Shmidt014a3ff2015-12-28 13:27:49 -0800347 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
Jouni Malinen6ec30382015-07-08 20:48:18 +0300348 "RSN: PMKID mismatch - authentication server may have derived different MSK?!");
349 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700350 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700351
352 if (!sm->cur_pmksa)
353 sm->cur_pmksa = sa;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700354 } else {
355 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
356 "WPA: Failed to get master session key from "
357 "EAPOL state machines - key handshake "
358 "aborted");
359 if (sm->cur_pmksa) {
360 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
361 "RSN: Cancelled PMKSA caching "
362 "attempt");
363 sm->cur_pmksa = NULL;
364 abort_cached = 1;
365 } else if (!abort_cached) {
366 return -1;
367 }
368 }
369 }
370
Dmitry Shmidtc55524a2011-07-07 11:18:38 -0700371 if (abort_cached && wpa_key_mgmt_wpa_ieee8021x(sm->key_mgmt) &&
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800372 !wpa_key_mgmt_suite_b(sm->key_mgmt) &&
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800373 !wpa_key_mgmt_ft(sm->key_mgmt) && sm->key_mgmt != WPA_KEY_MGMT_OSEN)
374 {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700375 /* Send EAPOL-Start to trigger full EAP authentication. */
376 u8 *buf;
377 size_t buflen;
378
379 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
380 "RSN: no PMKSA entry found - trigger "
381 "full EAP authentication");
382 buf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_START,
383 NULL, 0, &buflen, NULL);
384 if (buf) {
385 wpa_sm_ether_send(sm, sm->bssid, ETH_P_EAPOL,
386 buf, buflen);
387 os_free(buf);
388 return -2;
389 }
390
391 return -1;
392 }
393
394 return 0;
395}
396
397
398/**
399 * wpa_supplicant_send_2_of_4 - Send message 2 of WPA/RSN 4-Way Handshake
400 * @sm: Pointer to WPA state machine data from wpa_sm_init()
401 * @dst: Destination address for the frame
402 * @key: Pointer to the EAPOL-Key frame header
403 * @ver: Version bits from EAPOL-Key Key Info
404 * @nonce: Nonce value for the EAPOL-Key frame
405 * @wpa_ie: WPA/RSN IE
406 * @wpa_ie_len: Length of the WPA/RSN IE
407 * @ptk: PTK to use for keyed hash and encryption
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800408 * Returns: >= 0 on success, < 0 on failure
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700409 */
410int wpa_supplicant_send_2_of_4(struct wpa_sm *sm, const unsigned char *dst,
411 const struct wpa_eapol_key *key,
412 int ver, const u8 *nonce,
413 const u8 *wpa_ie, size_t wpa_ie_len,
414 struct wpa_ptk *ptk)
415{
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800416 size_t mic_len, hdrlen, rlen;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700417 struct wpa_eapol_key *reply;
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800418 u8 *rbuf, *key_mic;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700419 u8 *rsn_ie_buf = NULL;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800420 u16 key_info;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700421
422 if (wpa_ie == NULL) {
423 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, "WPA: No wpa_ie set - "
424 "cannot generate msg 2/4");
425 return -1;
426 }
427
428#ifdef CONFIG_IEEE80211R
429 if (wpa_key_mgmt_ft(sm->key_mgmt)) {
430 int res;
431
432 /*
433 * Add PMKR1Name into RSN IE (PMKID-List) and add MDIE and
434 * FTIE from (Re)Association Response.
435 */
436 rsn_ie_buf = os_malloc(wpa_ie_len + 2 + 2 + PMKID_LEN +
437 sm->assoc_resp_ies_len);
438 if (rsn_ie_buf == NULL)
439 return -1;
440 os_memcpy(rsn_ie_buf, wpa_ie, wpa_ie_len);
Dmitry Shmidt55840ad2015-12-14 12:45:46 -0800441 res = wpa_insert_pmkid(rsn_ie_buf, &wpa_ie_len,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700442 sm->pmk_r1_name);
443 if (res < 0) {
444 os_free(rsn_ie_buf);
445 return -1;
446 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700447
448 if (sm->assoc_resp_ies) {
449 os_memcpy(rsn_ie_buf + wpa_ie_len, sm->assoc_resp_ies,
450 sm->assoc_resp_ies_len);
451 wpa_ie_len += sm->assoc_resp_ies_len;
452 }
453
454 wpa_ie = rsn_ie_buf;
455 }
456#endif /* CONFIG_IEEE80211R */
457
458 wpa_hexdump(MSG_DEBUG, "WPA: WPA IE for msg 2/4", wpa_ie, wpa_ie_len);
459
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800460 mic_len = wpa_mic_len(sm->key_mgmt);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800461 hdrlen = sizeof(*reply) + mic_len + 2;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700462 rbuf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_KEY,
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800463 NULL, hdrlen + wpa_ie_len,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700464 &rlen, (void *) &reply);
465 if (rbuf == NULL) {
466 os_free(rsn_ie_buf);
467 return -1;
468 }
469
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800470 reply->type = (sm->proto == WPA_PROTO_RSN ||
471 sm->proto == WPA_PROTO_OSEN) ?
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700472 EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800473 key_info = ver | WPA_KEY_INFO_KEY_TYPE;
474 if (mic_len)
475 key_info |= WPA_KEY_INFO_MIC;
476 else
477 key_info |= WPA_KEY_INFO_ENCR_KEY_DATA;
478 WPA_PUT_BE16(reply->key_info, key_info);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800479 if (sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700480 WPA_PUT_BE16(reply->key_length, 0);
481 else
482 os_memcpy(reply->key_length, key->key_length, 2);
483 os_memcpy(reply->replay_counter, key->replay_counter,
484 WPA_REPLAY_COUNTER_LEN);
485 wpa_hexdump(MSG_DEBUG, "WPA: Replay Counter", reply->replay_counter,
486 WPA_REPLAY_COUNTER_LEN);
487
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800488 key_mic = (u8 *) (reply + 1);
489 WPA_PUT_BE16(key_mic + mic_len, wpa_ie_len); /* Key Data Length */
490 os_memcpy(key_mic + mic_len + 2, wpa_ie, wpa_ie_len); /* Key Data */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700491 os_free(rsn_ie_buf);
492
493 os_memcpy(reply->key_nonce, nonce, WPA_NONCE_LEN);
494
495 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Sending EAPOL-Key 2/4");
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800496 return wpa_eapol_key_send(sm, ptk, ver, dst, ETH_P_EAPOL, rbuf, rlen,
497 key_mic);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700498}
499
500
501static int wpa_derive_ptk(struct wpa_sm *sm, const unsigned char *src_addr,
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800502 const struct wpa_eapol_key *key, struct wpa_ptk *ptk)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700503{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700504#ifdef CONFIG_IEEE80211R
505 if (wpa_key_mgmt_ft(sm->key_mgmt))
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800506 return wpa_derive_ptk_ft(sm, src_addr, key, ptk);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700507#endif /* CONFIG_IEEE80211R */
508
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800509 return wpa_pmk_to_ptk(sm->pmk, sm->pmk_len, "Pairwise key expansion",
510 sm->own_addr, sm->bssid, sm->snonce,
511 key->key_nonce, ptk, sm->key_mgmt,
512 sm->pairwise_cipher);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700513}
514
515
516static void wpa_supplicant_process_1_of_4(struct wpa_sm *sm,
517 const unsigned char *src_addr,
518 const struct wpa_eapol_key *key,
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700519 u16 ver, const u8 *key_data,
520 size_t key_data_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700521{
522 struct wpa_eapol_ie_parse ie;
523 struct wpa_ptk *ptk;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700524 int res;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800525 u8 *kde, *kde_buf = NULL;
526 size_t kde_len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700527
528 if (wpa_sm_get_network_ctx(sm) == NULL) {
529 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, "WPA: No SSID info "
530 "found (msg 1 of 4)");
531 return;
532 }
533
534 wpa_sm_set_state(sm, WPA_4WAY_HANDSHAKE);
535 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: RX message 1 of 4-Way "
536 "Handshake from " MACSTR " (ver=%d)", MAC2STR(src_addr), ver);
537
538 os_memset(&ie, 0, sizeof(ie));
539
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800540 if (sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700541 /* RSN: msg 1/4 should contain PMKID for the selected PMK */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700542 wpa_hexdump(MSG_DEBUG, "RSN: msg 1/4 key data",
543 key_data, key_data_len);
544 if (wpa_supplicant_parse_ies(key_data, key_data_len, &ie) < 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800545 goto failed;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700546 if (ie.pmkid) {
547 wpa_hexdump(MSG_DEBUG, "RSN: PMKID from "
548 "Authenticator", ie.pmkid, PMKID_LEN);
549 }
550 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700551
552 res = wpa_supplicant_get_pmk(sm, src_addr, ie.pmkid);
553 if (res == -2) {
554 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "RSN: Do not reply to "
555 "msg 1/4 - requesting full EAP authentication");
556 return;
557 }
558 if (res)
559 goto failed;
560
561 if (sm->renew_snonce) {
562 if (random_get_bytes(sm->snonce, WPA_NONCE_LEN)) {
563 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
564 "WPA: Failed to get random data for SNonce");
565 goto failed;
566 }
567 sm->renew_snonce = 0;
568 wpa_hexdump(MSG_DEBUG, "WPA: Renewed SNonce",
569 sm->snonce, WPA_NONCE_LEN);
570 }
571
572 /* Calculate PTK which will be stored as a temporary PTK until it has
573 * been verified when processing message 3/4. */
574 ptk = &sm->tptk;
575 wpa_derive_ptk(sm, src_addr, key, ptk);
Dmitry Shmidt98660862014-03-11 17:26:21 -0700576 if (sm->pairwise_cipher == WPA_CIPHER_TKIP) {
Dmitry Shmidt61593f02014-04-21 16:27:35 -0700577 u8 buf[8];
Dmitry Shmidt98660862014-03-11 17:26:21 -0700578 /* Supplicant: swap tx/rx Mic keys */
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800579 os_memcpy(buf, &ptk->tk[16], 8);
580 os_memcpy(&ptk->tk[16], &ptk->tk[24], 8);
581 os_memcpy(&ptk->tk[24], buf, 8);
Dmitry Shmidt61593f02014-04-21 16:27:35 -0700582 os_memset(buf, 0, sizeof(buf));
Dmitry Shmidt98660862014-03-11 17:26:21 -0700583 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700584 sm->tptk_set = 1;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800585 sm->tk_to_set = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700586
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800587 kde = sm->assoc_wpa_ie;
588 kde_len = sm->assoc_wpa_ie_len;
589
590#ifdef CONFIG_P2P
591 if (sm->p2p) {
592 kde_buf = os_malloc(kde_len + 2 + RSN_SELECTOR_LEN + 1);
593 if (kde_buf) {
594 u8 *pos;
595 wpa_printf(MSG_DEBUG, "P2P: Add IP Address Request KDE "
596 "into EAPOL-Key 2/4");
597 os_memcpy(kde_buf, kde, kde_len);
598 kde = kde_buf;
599 pos = kde + kde_len;
600 *pos++ = WLAN_EID_VENDOR_SPECIFIC;
601 *pos++ = RSN_SELECTOR_LEN + 1;
602 RSN_SELECTOR_PUT(pos, WFA_KEY_DATA_IP_ADDR_REQ);
603 pos += RSN_SELECTOR_LEN;
604 *pos++ = 0x01;
605 kde_len = pos - kde;
606 }
607 }
608#endif /* CONFIG_P2P */
609
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700610 if (wpa_supplicant_send_2_of_4(sm, sm->bssid, key, ver, sm->snonce,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800611 kde, kde_len, ptk) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700612 goto failed;
613
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800614 os_free(kde_buf);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700615 os_memcpy(sm->anonce, key->key_nonce, WPA_NONCE_LEN);
616 return;
617
618failed:
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800619 os_free(kde_buf);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700620 wpa_sm_deauthenticate(sm, WLAN_REASON_UNSPECIFIED);
621}
622
623
624static void wpa_sm_start_preauth(void *eloop_ctx, void *timeout_ctx)
625{
626 struct wpa_sm *sm = eloop_ctx;
627 rsn_preauth_candidate_process(sm);
628}
629
630
631static void wpa_supplicant_key_neg_complete(struct wpa_sm *sm,
632 const u8 *addr, int secure)
633{
634 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
635 "WPA: Key negotiation completed with "
636 MACSTR " [PTK=%s GTK=%s]", MAC2STR(addr),
637 wpa_cipher_txt(sm->pairwise_cipher),
638 wpa_cipher_txt(sm->group_cipher));
639 wpa_sm_cancel_auth_timeout(sm);
640 wpa_sm_set_state(sm, WPA_COMPLETED);
641
642 if (secure) {
643 wpa_sm_mlme_setprotection(
644 sm, addr, MLME_SETPROTECTION_PROTECT_TYPE_RX_TX,
645 MLME_SETPROTECTION_KEY_TYPE_PAIRWISE);
646 eapol_sm_notify_portValid(sm->eapol, TRUE);
647 if (wpa_key_mgmt_wpa_psk(sm->key_mgmt))
648 eapol_sm_notify_eap_success(sm->eapol, TRUE);
649 /*
650 * Start preauthentication after a short wait to avoid a
651 * possible race condition between the data receive and key
652 * configuration after the 4-Way Handshake. This increases the
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800653 * likelihood of the first preauth EAPOL-Start frame getting to
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700654 * the target AP.
655 */
656 eloop_register_timeout(1, 0, wpa_sm_start_preauth, sm, NULL);
657 }
658
659 if (sm->cur_pmksa && sm->cur_pmksa->opportunistic) {
660 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
661 "RSN: Authenticator accepted "
662 "opportunistic PMKSA entry - marking it valid");
663 sm->cur_pmksa->opportunistic = 0;
664 }
665
666#ifdef CONFIG_IEEE80211R
667 if (wpa_key_mgmt_ft(sm->key_mgmt)) {
668 /* Prepare for the next transition */
669 wpa_ft_prepare_auth_request(sm, NULL);
670 }
671#endif /* CONFIG_IEEE80211R */
672}
673
674
675static void wpa_sm_rekey_ptk(void *eloop_ctx, void *timeout_ctx)
676{
677 struct wpa_sm *sm = eloop_ctx;
678 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Request PTK rekeying");
679 wpa_sm_key_request(sm, 0, 1);
680}
681
682
683static int wpa_supplicant_install_ptk(struct wpa_sm *sm,
684 const struct wpa_eapol_key *key)
685{
686 int keylen, rsclen;
687 enum wpa_alg alg;
688 const u8 *key_rsc;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800689
690 if (!sm->tk_to_set) {
691 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
692 "WPA: Do not re-install same PTK to the driver");
693 return 0;
694 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700695
696 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
697 "WPA: Installing PTK to the driver");
698
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700699 if (sm->pairwise_cipher == WPA_CIPHER_NONE) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700700 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Pairwise Cipher "
701 "Suite: NONE - do not use pairwise keys");
702 return 0;
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700703 }
704
705 if (!wpa_cipher_valid_pairwise(sm->pairwise_cipher)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700706 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
707 "WPA: Unsupported pairwise cipher %d",
708 sm->pairwise_cipher);
709 return -1;
710 }
711
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700712 alg = wpa_cipher_to_alg(sm->pairwise_cipher);
713 keylen = wpa_cipher_key_len(sm->pairwise_cipher);
714 rsclen = wpa_cipher_rsc_len(sm->pairwise_cipher);
715
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800716 if (sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700717 key_rsc = null_rsc;
718 } else {
719 key_rsc = key->key_rsc;
720 wpa_hexdump(MSG_DEBUG, "WPA: RSC", key_rsc, rsclen);
721 }
722
723 if (wpa_sm_set_key(sm, alg, sm->bssid, 0, 1, key_rsc, rsclen,
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800724 sm->ptk.tk, keylen) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700725 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
726 "WPA: Failed to set PTK to the "
727 "driver (alg=%d keylen=%d bssid=" MACSTR ")",
728 alg, keylen, MAC2STR(sm->bssid));
729 return -1;
730 }
731
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800732 /* TK is not needed anymore in supplicant */
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800733 os_memset(sm->ptk.tk, 0, WPA_TK_MAX_LEN);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800734 sm->tk_to_set = 0;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800735
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700736 if (sm->wpa_ptk_rekey) {
737 eloop_cancel_timeout(wpa_sm_rekey_ptk, sm, NULL);
738 eloop_register_timeout(sm->wpa_ptk_rekey, 0, wpa_sm_rekey_ptk,
739 sm, NULL);
740 }
741
742 return 0;
743}
744
745
746static int wpa_supplicant_check_group_cipher(struct wpa_sm *sm,
747 int group_cipher,
748 int keylen, int maxkeylen,
749 int *key_rsc_len,
750 enum wpa_alg *alg)
751{
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700752 int klen;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700753
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700754 *alg = wpa_cipher_to_alg(group_cipher);
755 if (*alg == WPA_ALG_NONE) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700756 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
757 "WPA: Unsupported Group Cipher %d",
758 group_cipher);
759 return -1;
760 }
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700761 *key_rsc_len = wpa_cipher_rsc_len(group_cipher);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700762
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700763 klen = wpa_cipher_key_len(group_cipher);
764 if (keylen != klen || maxkeylen < klen) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700765 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
766 "WPA: Unsupported %s Group Cipher key length %d (%d)",
767 wpa_cipher_txt(group_cipher), keylen, maxkeylen);
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700768 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700769 }
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700770 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700771}
772
773
774struct wpa_gtk_data {
775 enum wpa_alg alg;
776 int tx, key_rsc_len, keyidx;
777 u8 gtk[32];
778 int gtk_len;
779};
780
781
782static int wpa_supplicant_install_gtk(struct wpa_sm *sm,
783 const struct wpa_gtk_data *gd,
784 const u8 *key_rsc)
785{
786 const u8 *_gtk = gd->gtk;
787 u8 gtk_buf[32];
788
Mathy Vanhoef10bfd642017-07-12 16:03:24 +0200789 /* Detect possible key reinstallation */
790 if (sm->gtk.gtk_len == (size_t) gd->gtk_len &&
791 os_memcmp(sm->gtk.gtk, gd->gtk, sm->gtk.gtk_len) == 0) {
792 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
793 "WPA: Not reinstalling already in-use GTK to the driver (keyidx=%d tx=%d len=%d)",
794 gd->keyidx, gd->tx, gd->gtk_len);
795 return 0;
796 }
797
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700798 wpa_hexdump_key(MSG_DEBUG, "WPA: Group Key", gd->gtk, gd->gtk_len);
799 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
800 "WPA: Installing GTK to the driver (keyidx=%d tx=%d len=%d)",
801 gd->keyidx, gd->tx, gd->gtk_len);
802 wpa_hexdump(MSG_DEBUG, "WPA: RSC", key_rsc, gd->key_rsc_len);
803 if (sm->group_cipher == WPA_CIPHER_TKIP) {
804 /* Swap Tx/Rx keys for Michael MIC */
805 os_memcpy(gtk_buf, gd->gtk, 16);
806 os_memcpy(gtk_buf + 16, gd->gtk + 24, 8);
807 os_memcpy(gtk_buf + 24, gd->gtk + 16, 8);
808 _gtk = gtk_buf;
809 }
810 if (sm->pairwise_cipher == WPA_CIPHER_NONE) {
811 if (wpa_sm_set_key(sm, gd->alg, NULL,
812 gd->keyidx, 1, key_rsc, gd->key_rsc_len,
813 _gtk, gd->gtk_len) < 0) {
814 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
815 "WPA: Failed to set GTK to the driver "
816 "(Group only)");
Dmitry Shmidt61593f02014-04-21 16:27:35 -0700817 os_memset(gtk_buf, 0, sizeof(gtk_buf));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700818 return -1;
819 }
820 } else if (wpa_sm_set_key(sm, gd->alg, broadcast_ether_addr,
821 gd->keyidx, gd->tx, key_rsc, gd->key_rsc_len,
822 _gtk, gd->gtk_len) < 0) {
823 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
824 "WPA: Failed to set GTK to "
825 "the driver (alg=%d keylen=%d keyidx=%d)",
826 gd->alg, gd->gtk_len, gd->keyidx);
Dmitry Shmidt61593f02014-04-21 16:27:35 -0700827 os_memset(gtk_buf, 0, sizeof(gtk_buf));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700828 return -1;
829 }
Dmitry Shmidt61593f02014-04-21 16:27:35 -0700830 os_memset(gtk_buf, 0, sizeof(gtk_buf));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700831
Mathy Vanhoef10bfd642017-07-12 16:03:24 +0200832 sm->gtk.gtk_len = gd->gtk_len;
833 os_memcpy(sm->gtk.gtk, gd->gtk, sm->gtk.gtk_len);
834
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700835 return 0;
836}
837
838
839static int wpa_supplicant_gtk_tx_bit_workaround(const struct wpa_sm *sm,
840 int tx)
841{
842 if (tx && sm->pairwise_cipher != WPA_CIPHER_NONE) {
843 /* Ignore Tx bit for GTK if a pairwise key is used. One AP
844 * seemed to set this bit (incorrectly, since Tx is only when
845 * doing Group Key only APs) and without this workaround, the
846 * data connection does not work because wpa_supplicant
847 * configured non-zero keyidx to be used for unicast. */
848 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
849 "WPA: Tx bit set for GTK, but pairwise "
850 "keys are used - ignore Tx bit");
851 return 0;
852 }
853 return tx;
854}
855
856
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800857static int wpa_supplicant_rsc_relaxation(const struct wpa_sm *sm,
858 const u8 *rsc)
859{
860 int rsclen;
861
862 if (!sm->wpa_rsc_relaxation)
863 return 0;
864
865 rsclen = wpa_cipher_rsc_len(sm->group_cipher);
866
867 /*
868 * Try to detect RSC (endian) corruption issue where the AP sends
869 * the RSC bytes in EAPOL-Key message in the wrong order, both if
870 * it's actually a 6-byte field (as it should be) and if it treats
871 * it as an 8-byte field.
872 * An AP model known to have this bug is the Sapido RB-1632.
873 */
874 if (rsclen == 6 && ((rsc[5] && !rsc[0]) || rsc[6] || rsc[7])) {
875 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
876 "RSC %02x%02x%02x%02x%02x%02x%02x%02x is likely bogus, using 0",
877 rsc[0], rsc[1], rsc[2], rsc[3],
878 rsc[4], rsc[5], rsc[6], rsc[7]);
879
880 return 1;
881 }
882
883 return 0;
884}
885
886
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700887static int wpa_supplicant_pairwise_gtk(struct wpa_sm *sm,
888 const struct wpa_eapol_key *key,
889 const u8 *gtk, size_t gtk_len,
890 int key_info)
891{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700892 struct wpa_gtk_data gd;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800893 const u8 *key_rsc;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700894
895 /*
896 * IEEE Std 802.11i-2004 - 8.5.2 EAPOL-Key frames - Figure 43x
897 * GTK KDE format:
898 * KeyID[bits 0-1], Tx [bit 2], Reserved [bits 3-7]
899 * Reserved [bits 0-7]
900 * GTK
901 */
902
903 os_memset(&gd, 0, sizeof(gd));
904 wpa_hexdump_key(MSG_DEBUG, "RSN: received GTK in pairwise handshake",
905 gtk, gtk_len);
906
907 if (gtk_len < 2 || gtk_len - 2 > sizeof(gd.gtk))
908 return -1;
909
910 gd.keyidx = gtk[0] & 0x3;
911 gd.tx = wpa_supplicant_gtk_tx_bit_workaround(sm,
912 !!(gtk[0] & BIT(2)));
913 gtk += 2;
914 gtk_len -= 2;
915
916 os_memcpy(gd.gtk, gtk, gtk_len);
917 gd.gtk_len = gtk_len;
918
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800919 key_rsc = key->key_rsc;
920 if (wpa_supplicant_rsc_relaxation(sm, key->key_rsc))
921 key_rsc = null_rsc;
922
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800923 if (sm->group_cipher != WPA_CIPHER_GTK_NOT_USED &&
924 (wpa_supplicant_check_group_cipher(sm, sm->group_cipher,
925 gtk_len, gtk_len,
926 &gd.key_rsc_len, &gd.alg) ||
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800927 wpa_supplicant_install_gtk(sm, &gd, key_rsc))) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700928 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
929 "RSN: Failed to install GTK");
Dmitry Shmidt61593f02014-04-21 16:27:35 -0700930 os_memset(&gd, 0, sizeof(gd));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700931 return -1;
932 }
Dmitry Shmidt61593f02014-04-21 16:27:35 -0700933 os_memset(&gd, 0, sizeof(gd));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700934
935 wpa_supplicant_key_neg_complete(sm, sm->bssid,
936 key_info & WPA_KEY_INFO_SECURE);
937 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700938}
939
940
Mathy Vanhoef10bfd642017-07-12 16:03:24 +0200941#ifdef CONFIG_IEEE80211W
942static int wpa_supplicant_install_igtk(struct wpa_sm *sm,
943 const struct wpa_igtk_kde *igtk)
944{
945 size_t len = wpa_cipher_key_len(sm->mgmt_group_cipher);
946 u16 keyidx = WPA_GET_LE16(igtk->keyid);
947
948 /* Detect possible key reinstallation */
949 if (sm->igtk.igtk_len == len &&
950 os_memcmp(sm->igtk.igtk, igtk->igtk, sm->igtk.igtk_len) == 0) {
951 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
952 "WPA: Not reinstalling already in-use IGTK to the driver (keyidx=%d)",
953 keyidx);
954 return 0;
955 }
956
957 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
958 "WPA: IGTK keyid %d pn %02x%02x%02x%02x%02x%02x",
959 keyidx, MAC2STR(igtk->pn));
960 wpa_hexdump_key(MSG_DEBUG, "WPA: IGTK", igtk->igtk, len);
961 if (keyidx > 4095) {
962 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
963 "WPA: Invalid IGTK KeyID %d", keyidx);
964 return -1;
965 }
966 if (wpa_sm_set_key(sm, wpa_cipher_to_alg(sm->mgmt_group_cipher),
967 broadcast_ether_addr,
968 keyidx, 0, igtk->pn, sizeof(igtk->pn),
969 igtk->igtk, len) < 0) {
970 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
971 "WPA: Failed to configure IGTK to the driver");
972 return -1;
973 }
974
975 sm->igtk.igtk_len = len;
976 os_memcpy(sm->igtk.igtk, igtk->igtk, sm->igtk.igtk_len);
977
978 return 0;
979}
980#endif /* CONFIG_IEEE80211W */
981
982
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700983static int ieee80211w_set_keys(struct wpa_sm *sm,
984 struct wpa_eapol_ie_parse *ie)
985{
986#ifdef CONFIG_IEEE80211W
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -0700987 if (!wpa_cipher_valid_mgmt_group(sm->mgmt_group_cipher))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700988 return 0;
989
990 if (ie->igtk) {
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -0700991 size_t len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700992 const struct wpa_igtk_kde *igtk;
Mathy Vanhoef10bfd642017-07-12 16:03:24 +0200993
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -0700994 len = wpa_cipher_key_len(sm->mgmt_group_cipher);
995 if (ie->igtk_len != WPA_IGTK_KDE_PREFIX_LEN + len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700996 return -1;
Mathy Vanhoef10bfd642017-07-12 16:03:24 +0200997
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700998 igtk = (const struct wpa_igtk_kde *) ie->igtk;
Mathy Vanhoef10bfd642017-07-12 16:03:24 +0200999 if (wpa_supplicant_install_igtk(sm, igtk) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001000 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001001 }
1002
1003 return 0;
1004#else /* CONFIG_IEEE80211W */
1005 return 0;
1006#endif /* CONFIG_IEEE80211W */
1007}
1008
1009
1010static void wpa_report_ie_mismatch(struct wpa_sm *sm,
1011 const char *reason, const u8 *src_addr,
1012 const u8 *wpa_ie, size_t wpa_ie_len,
1013 const u8 *rsn_ie, size_t rsn_ie_len)
1014{
1015 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, "WPA: %s (src=" MACSTR ")",
1016 reason, MAC2STR(src_addr));
1017
1018 if (sm->ap_wpa_ie) {
1019 wpa_hexdump(MSG_INFO, "WPA: WPA IE in Beacon/ProbeResp",
1020 sm->ap_wpa_ie, sm->ap_wpa_ie_len);
1021 }
1022 if (wpa_ie) {
1023 if (!sm->ap_wpa_ie) {
1024 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1025 "WPA: No WPA IE in Beacon/ProbeResp");
1026 }
1027 wpa_hexdump(MSG_INFO, "WPA: WPA IE in 3/4 msg",
1028 wpa_ie, wpa_ie_len);
1029 }
1030
1031 if (sm->ap_rsn_ie) {
1032 wpa_hexdump(MSG_INFO, "WPA: RSN IE in Beacon/ProbeResp",
1033 sm->ap_rsn_ie, sm->ap_rsn_ie_len);
1034 }
1035 if (rsn_ie) {
1036 if (!sm->ap_rsn_ie) {
1037 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1038 "WPA: No RSN IE in Beacon/ProbeResp");
1039 }
1040 wpa_hexdump(MSG_INFO, "WPA: RSN IE in 3/4 msg",
1041 rsn_ie, rsn_ie_len);
1042 }
1043
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001044 wpa_sm_deauthenticate(sm, WLAN_REASON_IE_IN_4WAY_DIFFERS);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001045}
1046
1047
1048#ifdef CONFIG_IEEE80211R
1049
1050static int ft_validate_mdie(struct wpa_sm *sm,
1051 const unsigned char *src_addr,
1052 struct wpa_eapol_ie_parse *ie,
1053 const u8 *assoc_resp_mdie)
1054{
1055 struct rsn_mdie *mdie;
1056
1057 mdie = (struct rsn_mdie *) (ie->mdie + 2);
1058 if (ie->mdie == NULL || ie->mdie_len < 2 + sizeof(*mdie) ||
1059 os_memcmp(mdie->mobility_domain, sm->mobility_domain,
1060 MOBILITY_DOMAIN_ID_LEN) != 0) {
1061 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "FT: MDIE in msg 3/4 did "
1062 "not match with the current mobility domain");
1063 return -1;
1064 }
1065
1066 if (assoc_resp_mdie &&
1067 (assoc_resp_mdie[1] != ie->mdie[1] ||
1068 os_memcmp(assoc_resp_mdie, ie->mdie, 2 + ie->mdie[1]) != 0)) {
1069 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "FT: MDIE mismatch");
1070 wpa_hexdump(MSG_DEBUG, "FT: MDIE in EAPOL-Key msg 3/4",
1071 ie->mdie, 2 + ie->mdie[1]);
1072 wpa_hexdump(MSG_DEBUG, "FT: MDIE in (Re)Association Response",
1073 assoc_resp_mdie, 2 + assoc_resp_mdie[1]);
1074 return -1;
1075 }
1076
1077 return 0;
1078}
1079
1080
1081static int ft_validate_ftie(struct wpa_sm *sm,
1082 const unsigned char *src_addr,
1083 struct wpa_eapol_ie_parse *ie,
1084 const u8 *assoc_resp_ftie)
1085{
1086 if (ie->ftie == NULL) {
1087 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1088 "FT: No FTIE in EAPOL-Key msg 3/4");
1089 return -1;
1090 }
1091
1092 if (assoc_resp_ftie == NULL)
1093 return 0;
1094
1095 if (assoc_resp_ftie[1] != ie->ftie[1] ||
1096 os_memcmp(assoc_resp_ftie, ie->ftie, 2 + ie->ftie[1]) != 0) {
1097 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "FT: FTIE mismatch");
1098 wpa_hexdump(MSG_DEBUG, "FT: FTIE in EAPOL-Key msg 3/4",
1099 ie->ftie, 2 + ie->ftie[1]);
1100 wpa_hexdump(MSG_DEBUG, "FT: FTIE in (Re)Association Response",
1101 assoc_resp_ftie, 2 + assoc_resp_ftie[1]);
1102 return -1;
1103 }
1104
1105 return 0;
1106}
1107
1108
1109static int ft_validate_rsnie(struct wpa_sm *sm,
1110 const unsigned char *src_addr,
1111 struct wpa_eapol_ie_parse *ie)
1112{
1113 struct wpa_ie_data rsn;
1114
1115 if (!ie->rsn_ie)
1116 return 0;
1117
1118 /*
1119 * Verify that PMKR1Name from EAPOL-Key message 3/4
1120 * matches with the value we derived.
1121 */
1122 if (wpa_parse_wpa_ie_rsn(ie->rsn_ie, ie->rsn_ie_len, &rsn) < 0 ||
1123 rsn.num_pmkid != 1 || rsn.pmkid == NULL) {
1124 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "FT: No PMKR1Name in "
1125 "FT 4-way handshake message 3/4");
1126 return -1;
1127 }
1128
Dmitry Shmidtc2817022014-07-02 10:32:10 -07001129 if (os_memcmp_const(rsn.pmkid, sm->pmk_r1_name, WPA_PMK_NAME_LEN) != 0)
1130 {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001131 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1132 "FT: PMKR1Name mismatch in "
1133 "FT 4-way handshake message 3/4");
1134 wpa_hexdump(MSG_DEBUG, "FT: PMKR1Name from Authenticator",
1135 rsn.pmkid, WPA_PMK_NAME_LEN);
1136 wpa_hexdump(MSG_DEBUG, "FT: Derived PMKR1Name",
1137 sm->pmk_r1_name, WPA_PMK_NAME_LEN);
1138 return -1;
1139 }
1140
1141 return 0;
1142}
1143
1144
1145static int wpa_supplicant_validate_ie_ft(struct wpa_sm *sm,
1146 const unsigned char *src_addr,
1147 struct wpa_eapol_ie_parse *ie)
1148{
1149 const u8 *pos, *end, *mdie = NULL, *ftie = NULL;
1150
1151 if (sm->assoc_resp_ies) {
1152 pos = sm->assoc_resp_ies;
1153 end = pos + sm->assoc_resp_ies_len;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001154 while (end - pos > 2) {
1155 if (2 + pos[1] > end - pos)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001156 break;
1157 switch (*pos) {
1158 case WLAN_EID_MOBILITY_DOMAIN:
1159 mdie = pos;
1160 break;
1161 case WLAN_EID_FAST_BSS_TRANSITION:
1162 ftie = pos;
1163 break;
1164 }
1165 pos += 2 + pos[1];
1166 }
1167 }
1168
1169 if (ft_validate_mdie(sm, src_addr, ie, mdie) < 0 ||
1170 ft_validate_ftie(sm, src_addr, ie, ftie) < 0 ||
1171 ft_validate_rsnie(sm, src_addr, ie) < 0)
1172 return -1;
1173
1174 return 0;
1175}
1176
1177#endif /* CONFIG_IEEE80211R */
1178
1179
1180static int wpa_supplicant_validate_ie(struct wpa_sm *sm,
1181 const unsigned char *src_addr,
1182 struct wpa_eapol_ie_parse *ie)
1183{
1184 if (sm->ap_wpa_ie == NULL && sm->ap_rsn_ie == NULL) {
1185 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1186 "WPA: No WPA/RSN IE for this AP known. "
1187 "Trying to get from scan results");
1188 if (wpa_sm_get_beacon_ie(sm) < 0) {
1189 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1190 "WPA: Could not find AP from "
1191 "the scan results");
1192 } else {
1193 wpa_msg(sm->ctx->msg_ctx, MSG_DEBUG,
1194 "WPA: Found the current AP from "
1195 "updated scan results");
1196 }
1197 }
1198
1199 if (ie->wpa_ie == NULL && ie->rsn_ie == NULL &&
1200 (sm->ap_wpa_ie || sm->ap_rsn_ie)) {
1201 wpa_report_ie_mismatch(sm, "IE in 3/4 msg does not match "
1202 "with IE in Beacon/ProbeResp (no IE?)",
1203 src_addr, ie->wpa_ie, ie->wpa_ie_len,
1204 ie->rsn_ie, ie->rsn_ie_len);
1205 return -1;
1206 }
1207
1208 if ((ie->wpa_ie && sm->ap_wpa_ie &&
1209 (ie->wpa_ie_len != sm->ap_wpa_ie_len ||
1210 os_memcmp(ie->wpa_ie, sm->ap_wpa_ie, ie->wpa_ie_len) != 0)) ||
1211 (ie->rsn_ie && sm->ap_rsn_ie &&
1212 wpa_compare_rsn_ie(wpa_key_mgmt_ft(sm->key_mgmt),
1213 sm->ap_rsn_ie, sm->ap_rsn_ie_len,
1214 ie->rsn_ie, ie->rsn_ie_len))) {
1215 wpa_report_ie_mismatch(sm, "IE in 3/4 msg does not match "
1216 "with IE in Beacon/ProbeResp",
1217 src_addr, ie->wpa_ie, ie->wpa_ie_len,
1218 ie->rsn_ie, ie->rsn_ie_len);
1219 return -1;
1220 }
1221
1222 if (sm->proto == WPA_PROTO_WPA &&
1223 ie->rsn_ie && sm->ap_rsn_ie == NULL && sm->rsn_enabled) {
1224 wpa_report_ie_mismatch(sm, "Possible downgrade attack "
1225 "detected - RSN was enabled and RSN IE "
1226 "was in msg 3/4, but not in "
1227 "Beacon/ProbeResp",
1228 src_addr, ie->wpa_ie, ie->wpa_ie_len,
1229 ie->rsn_ie, ie->rsn_ie_len);
1230 return -1;
1231 }
1232
1233#ifdef CONFIG_IEEE80211R
1234 if (wpa_key_mgmt_ft(sm->key_mgmt) &&
1235 wpa_supplicant_validate_ie_ft(sm, src_addr, ie) < 0)
1236 return -1;
1237#endif /* CONFIG_IEEE80211R */
1238
1239 return 0;
1240}
1241
1242
1243/**
1244 * wpa_supplicant_send_4_of_4 - Send message 4 of WPA/RSN 4-Way Handshake
1245 * @sm: Pointer to WPA state machine data from wpa_sm_init()
1246 * @dst: Destination address for the frame
1247 * @key: Pointer to the EAPOL-Key frame header
1248 * @ver: Version bits from EAPOL-Key Key Info
1249 * @key_info: Key Info
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001250 * @ptk: PTK to use for keyed hash and encryption
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001251 * Returns: >= 0 on success, < 0 on failure
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001252 */
1253int wpa_supplicant_send_4_of_4(struct wpa_sm *sm, const unsigned char *dst,
1254 const struct wpa_eapol_key *key,
1255 u16 ver, u16 key_info,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001256 struct wpa_ptk *ptk)
1257{
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001258 size_t mic_len, hdrlen, rlen;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001259 struct wpa_eapol_key *reply;
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001260 u8 *rbuf, *key_mic;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001261
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001262 mic_len = wpa_mic_len(sm->key_mgmt);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001263 hdrlen = sizeof(*reply) + mic_len + 2;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001264 rbuf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_KEY, NULL,
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001265 hdrlen, &rlen, (void *) &reply);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001266 if (rbuf == NULL)
1267 return -1;
1268
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001269 reply->type = (sm->proto == WPA_PROTO_RSN ||
1270 sm->proto == WPA_PROTO_OSEN) ?
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001271 EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA;
1272 key_info &= WPA_KEY_INFO_SECURE;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001273 key_info |= ver | WPA_KEY_INFO_KEY_TYPE;
1274 if (mic_len)
1275 key_info |= WPA_KEY_INFO_MIC;
1276 else
1277 key_info |= WPA_KEY_INFO_ENCR_KEY_DATA;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001278 WPA_PUT_BE16(reply->key_info, key_info);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001279 if (sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001280 WPA_PUT_BE16(reply->key_length, 0);
1281 else
1282 os_memcpy(reply->key_length, key->key_length, 2);
1283 os_memcpy(reply->replay_counter, key->replay_counter,
1284 WPA_REPLAY_COUNTER_LEN);
1285
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001286 key_mic = (u8 *) (reply + 1);
1287 WPA_PUT_BE16(key_mic + mic_len, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001288
1289 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Sending EAPOL-Key 4/4");
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001290 return wpa_eapol_key_send(sm, ptk, ver, dst, ETH_P_EAPOL, rbuf, rlen,
1291 key_mic);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001292}
1293
1294
1295static void wpa_supplicant_process_3_of_4(struct wpa_sm *sm,
1296 const struct wpa_eapol_key *key,
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001297 u16 ver, const u8 *key_data,
1298 size_t key_data_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001299{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001300 u16 key_info, keylen;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001301 struct wpa_eapol_ie_parse ie;
1302
1303 wpa_sm_set_state(sm, WPA_4WAY_HANDSHAKE);
1304 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: RX message 3 of 4-Way "
1305 "Handshake from " MACSTR " (ver=%d)", MAC2STR(sm->bssid), ver);
1306
1307 key_info = WPA_GET_BE16(key->key_info);
1308
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001309 wpa_hexdump(MSG_DEBUG, "WPA: IE KeyData", key_data, key_data_len);
1310 if (wpa_supplicant_parse_ies(key_data, key_data_len, &ie) < 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001311 goto failed;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001312 if (ie.gtk && !(key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
1313 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1314 "WPA: GTK IE in unencrypted key data");
1315 goto failed;
1316 }
1317#ifdef CONFIG_IEEE80211W
1318 if (ie.igtk && !(key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
1319 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1320 "WPA: IGTK KDE in unencrypted key data");
1321 goto failed;
1322 }
1323
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001324 if (ie.igtk &&
1325 wpa_cipher_valid_mgmt_group(sm->mgmt_group_cipher) &&
1326 ie.igtk_len != WPA_IGTK_KDE_PREFIX_LEN +
1327 (unsigned int) wpa_cipher_key_len(sm->mgmt_group_cipher)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001328 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1329 "WPA: Invalid IGTK KDE length %lu",
1330 (unsigned long) ie.igtk_len);
1331 goto failed;
1332 }
1333#endif /* CONFIG_IEEE80211W */
1334
1335 if (wpa_supplicant_validate_ie(sm, sm->bssid, &ie) < 0)
1336 goto failed;
1337
1338 if (os_memcmp(sm->anonce, key->key_nonce, WPA_NONCE_LEN) != 0) {
1339 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1340 "WPA: ANonce from message 1 of 4-Way Handshake "
1341 "differs from 3 of 4-Way Handshake - drop packet (src="
1342 MACSTR ")", MAC2STR(sm->bssid));
1343 goto failed;
1344 }
1345
1346 keylen = WPA_GET_BE16(key->key_length);
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07001347 if (keylen != wpa_cipher_key_len(sm->pairwise_cipher)) {
1348 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1349 "WPA: Invalid %s key length %d (src=" MACSTR
1350 ")", wpa_cipher_txt(sm->pairwise_cipher), keylen,
1351 MAC2STR(sm->bssid));
1352 goto failed;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001353 }
1354
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001355#ifdef CONFIG_P2P
1356 if (ie.ip_addr_alloc) {
1357 os_memcpy(sm->p2p_ip_addr, ie.ip_addr_alloc, 3 * 4);
1358 wpa_hexdump(MSG_DEBUG, "P2P: IP address info",
1359 sm->p2p_ip_addr, sizeof(sm->p2p_ip_addr));
1360 }
1361#endif /* CONFIG_P2P */
1362
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001363 if (wpa_supplicant_send_4_of_4(sm, sm->bssid, key, ver, key_info,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001364 &sm->ptk) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001365 goto failed;
1366 }
1367
1368 /* SNonce was successfully used in msg 3/4, so mark it to be renewed
1369 * for the next 4-Way Handshake. If msg 3 is received again, the old
1370 * SNonce will still be used to avoid changing PTK. */
1371 sm->renew_snonce = 1;
1372
1373 if (key_info & WPA_KEY_INFO_INSTALL) {
1374 if (wpa_supplicant_install_ptk(sm, key))
1375 goto failed;
1376 }
1377
1378 if (key_info & WPA_KEY_INFO_SECURE) {
1379 wpa_sm_mlme_setprotection(
1380 sm, sm->bssid, MLME_SETPROTECTION_PROTECT_TYPE_RX,
1381 MLME_SETPROTECTION_KEY_TYPE_PAIRWISE);
1382 eapol_sm_notify_portValid(sm->eapol, TRUE);
1383 }
1384 wpa_sm_set_state(sm, WPA_GROUP_HANDSHAKE);
1385
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001386 if (sm->group_cipher == WPA_CIPHER_GTK_NOT_USED) {
1387 wpa_supplicant_key_neg_complete(sm, sm->bssid,
1388 key_info & WPA_KEY_INFO_SECURE);
1389 } else if (ie.gtk &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001390 wpa_supplicant_pairwise_gtk(sm, key,
1391 ie.gtk, ie.gtk_len, key_info) < 0) {
1392 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1393 "RSN: Failed to configure GTK");
1394 goto failed;
1395 }
1396
1397 if (ieee80211w_set_keys(sm, &ie) < 0) {
1398 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1399 "RSN: Failed to configure IGTK");
1400 goto failed;
1401 }
1402
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001403 if (ie.gtk)
1404 wpa_sm_set_rekey_offload(sm);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001405
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001406 if (sm->proto == WPA_PROTO_RSN && wpa_key_mgmt_suite_b(sm->key_mgmt)) {
1407 struct rsn_pmksa_cache_entry *sa;
1408
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001409 sa = pmksa_cache_add(sm->pmksa, sm->pmk, sm->pmk_len, NULL,
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001410 sm->ptk.kck, sm->ptk.kck_len,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001411 sm->bssid, sm->own_addr,
1412 sm->network_ctx, sm->key_mgmt);
1413 if (!sm->cur_pmksa)
1414 sm->cur_pmksa = sa;
1415 }
1416
Dmitry Shmidt4dd28dc2015-03-10 11:21:43 -07001417 sm->msg_3_of_4_ok = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001418 return;
1419
1420failed:
1421 wpa_sm_deauthenticate(sm, WLAN_REASON_UNSPECIFIED);
1422}
1423
1424
1425static int wpa_supplicant_process_1_of_2_rsn(struct wpa_sm *sm,
1426 const u8 *keydata,
1427 size_t keydatalen,
1428 u16 key_info,
1429 struct wpa_gtk_data *gd)
1430{
1431 int maxkeylen;
1432 struct wpa_eapol_ie_parse ie;
1433
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08001434 wpa_hexdump_key(MSG_DEBUG, "RSN: msg 1/2 key data",
1435 keydata, keydatalen);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001436 if (wpa_supplicant_parse_ies(keydata, keydatalen, &ie) < 0)
1437 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001438 if (ie.gtk && !(key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
1439 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1440 "WPA: GTK IE in unencrypted key data");
1441 return -1;
1442 }
1443 if (ie.gtk == NULL) {
1444 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1445 "WPA: No GTK IE in Group Key msg 1/2");
1446 return -1;
1447 }
1448 maxkeylen = gd->gtk_len = ie.gtk_len - 2;
1449
1450 if (wpa_supplicant_check_group_cipher(sm, sm->group_cipher,
1451 gd->gtk_len, maxkeylen,
1452 &gd->key_rsc_len, &gd->alg))
1453 return -1;
1454
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001455 wpa_hexdump_key(MSG_DEBUG, "RSN: received GTK in group key handshake",
1456 ie.gtk, ie.gtk_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001457 gd->keyidx = ie.gtk[0] & 0x3;
1458 gd->tx = wpa_supplicant_gtk_tx_bit_workaround(sm,
1459 !!(ie.gtk[0] & BIT(2)));
1460 if (ie.gtk_len - 2 > sizeof(gd->gtk)) {
1461 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1462 "RSN: Too long GTK in GTK IE (len=%lu)",
1463 (unsigned long) ie.gtk_len - 2);
1464 return -1;
1465 }
1466 os_memcpy(gd->gtk, ie.gtk + 2, ie.gtk_len - 2);
1467
1468 if (ieee80211w_set_keys(sm, &ie) < 0)
1469 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1470 "RSN: Failed to configure IGTK");
1471
1472 return 0;
1473}
1474
1475
1476static int wpa_supplicant_process_1_of_2_wpa(struct wpa_sm *sm,
1477 const struct wpa_eapol_key *key,
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001478 const u8 *key_data,
1479 size_t key_data_len, u16 key_info,
1480 u16 ver, struct wpa_gtk_data *gd)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001481{
1482 size_t maxkeylen;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001483 u16 gtk_len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001484
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001485 gtk_len = WPA_GET_BE16(key->key_length);
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001486 maxkeylen = key_data_len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001487 if (ver == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
1488 if (maxkeylen < 8) {
1489 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1490 "WPA: Too short maxkeylen (%lu)",
1491 (unsigned long) maxkeylen);
1492 return -1;
1493 }
1494 maxkeylen -= 8;
1495 }
1496
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001497 if (gtk_len > maxkeylen ||
1498 wpa_supplicant_check_group_cipher(sm, sm->group_cipher,
1499 gtk_len, maxkeylen,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001500 &gd->key_rsc_len, &gd->alg))
1501 return -1;
1502
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001503 gd->gtk_len = gtk_len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001504 gd->keyidx = (key_info & WPA_KEY_INFO_KEY_INDEX_MASK) >>
1505 WPA_KEY_INFO_KEY_INDEX_SHIFT;
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001506 if (ver == WPA_KEY_INFO_TYPE_HMAC_MD5_RC4 && sm->ptk.kek_len == 16) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001507#ifdef CONFIG_NO_RC4
1508 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1509 "WPA: RC4 not supported in the build");
1510 return -1;
1511#else /* CONFIG_NO_RC4 */
Dmitry Shmidt61593f02014-04-21 16:27:35 -07001512 u8 ek[32];
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001513 if (key_data_len > sizeof(gd->gtk)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001514 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1515 "WPA: RC4 key data too long (%lu)",
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001516 (unsigned long) key_data_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001517 return -1;
1518 }
Dmitry Shmidt61593f02014-04-21 16:27:35 -07001519 os_memcpy(ek, key->key_iv, 16);
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001520 os_memcpy(ek + 16, sm->ptk.kek, sm->ptk.kek_len);
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001521 os_memcpy(gd->gtk, key_data, key_data_len);
1522 if (rc4_skip(ek, 32, 256, gd->gtk, key_data_len)) {
Dmitry Shmidt61593f02014-04-21 16:27:35 -07001523 os_memset(ek, 0, sizeof(ek));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001524 wpa_msg(sm->ctx->msg_ctx, MSG_ERROR,
1525 "WPA: RC4 failed");
1526 return -1;
1527 }
Dmitry Shmidt61593f02014-04-21 16:27:35 -07001528 os_memset(ek, 0, sizeof(ek));
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001529#endif /* CONFIG_NO_RC4 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001530 } else if (ver == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001531 if (maxkeylen % 8) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001532 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1533 "WPA: Unsupported AES-WRAP len %lu",
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001534 (unsigned long) maxkeylen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001535 return -1;
1536 }
1537 if (maxkeylen > sizeof(gd->gtk)) {
1538 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1539 "WPA: AES-WRAP key data "
1540 "too long (keydatalen=%lu maxkeylen=%lu)",
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001541 (unsigned long) key_data_len,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001542 (unsigned long) maxkeylen);
1543 return -1;
1544 }
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001545 if (aes_unwrap(sm->ptk.kek, sm->ptk.kek_len, maxkeylen / 8,
1546 key_data, gd->gtk)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001547 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1548 "WPA: AES unwrap failed - could not decrypt "
1549 "GTK");
1550 return -1;
1551 }
1552 } else {
1553 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1554 "WPA: Unsupported key_info type %d", ver);
1555 return -1;
1556 }
1557 gd->tx = wpa_supplicant_gtk_tx_bit_workaround(
1558 sm, !!(key_info & WPA_KEY_INFO_TXRX));
1559 return 0;
1560}
1561
1562
1563static int wpa_supplicant_send_2_of_2(struct wpa_sm *sm,
1564 const struct wpa_eapol_key *key,
1565 int ver, u16 key_info)
1566{
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001567 size_t mic_len, hdrlen, rlen;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001568 struct wpa_eapol_key *reply;
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001569 u8 *rbuf, *key_mic;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001570
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001571 mic_len = wpa_mic_len(sm->key_mgmt);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001572 hdrlen = sizeof(*reply) + mic_len + 2;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001573 rbuf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_KEY, NULL,
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001574 hdrlen, &rlen, (void *) &reply);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001575 if (rbuf == NULL)
1576 return -1;
1577
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001578 reply->type = (sm->proto == WPA_PROTO_RSN ||
1579 sm->proto == WPA_PROTO_OSEN) ?
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001580 EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA;
1581 key_info &= WPA_KEY_INFO_KEY_INDEX_MASK;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001582 key_info |= ver | WPA_KEY_INFO_SECURE;
1583 if (mic_len)
1584 key_info |= WPA_KEY_INFO_MIC;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001585 WPA_PUT_BE16(reply->key_info, key_info);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001586 if (sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001587 WPA_PUT_BE16(reply->key_length, 0);
1588 else
1589 os_memcpy(reply->key_length, key->key_length, 2);
1590 os_memcpy(reply->replay_counter, key->replay_counter,
1591 WPA_REPLAY_COUNTER_LEN);
1592
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001593 key_mic = (u8 *) (reply + 1);
1594 WPA_PUT_BE16(key_mic + mic_len, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001595
1596 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Sending EAPOL-Key 2/2");
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001597 return wpa_eapol_key_send(sm, &sm->ptk, ver, sm->bssid, ETH_P_EAPOL,
1598 rbuf, rlen, key_mic);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001599}
1600
1601
1602static void wpa_supplicant_process_1_of_2(struct wpa_sm *sm,
1603 const unsigned char *src_addr,
1604 const struct wpa_eapol_key *key,
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001605 const u8 *key_data,
1606 size_t key_data_len, u16 ver)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001607{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001608 u16 key_info;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001609 int rekey, ret;
1610 struct wpa_gtk_data gd;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001611 const u8 *key_rsc;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001612
Dmitry Shmidt4dd28dc2015-03-10 11:21:43 -07001613 if (!sm->msg_3_of_4_ok) {
1614 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1615 "WPA: Group Key Handshake started prior to completion of 4-way handshake");
1616 goto failed;
1617 }
1618
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001619 os_memset(&gd, 0, sizeof(gd));
1620
1621 rekey = wpa_sm_get_state(sm) == WPA_COMPLETED;
1622 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: RX message 1 of Group Key "
1623 "Handshake from " MACSTR " (ver=%d)", MAC2STR(src_addr), ver);
1624
1625 key_info = WPA_GET_BE16(key->key_info);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001626
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001627 if (sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN) {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001628 ret = wpa_supplicant_process_1_of_2_rsn(sm, key_data,
1629 key_data_len, key_info,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001630 &gd);
1631 } else {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001632 ret = wpa_supplicant_process_1_of_2_wpa(sm, key, key_data,
1633 key_data_len,
1634 key_info, ver, &gd);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001635 }
1636
1637 wpa_sm_set_state(sm, WPA_GROUP_HANDSHAKE);
1638
1639 if (ret)
1640 goto failed;
1641
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001642 key_rsc = key->key_rsc;
1643 if (wpa_supplicant_rsc_relaxation(sm, key->key_rsc))
1644 key_rsc = null_rsc;
1645
1646 if (wpa_supplicant_install_gtk(sm, &gd, key_rsc) ||
1647 wpa_supplicant_send_2_of_2(sm, key, ver, key_info) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001648 goto failed;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001649 os_memset(&gd, 0, sizeof(gd));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001650
1651 if (rekey) {
1652 wpa_msg(sm->ctx->msg_ctx, MSG_INFO, "WPA: Group rekeying "
1653 "completed with " MACSTR " [GTK=%s]",
1654 MAC2STR(sm->bssid), wpa_cipher_txt(sm->group_cipher));
1655 wpa_sm_cancel_auth_timeout(sm);
1656 wpa_sm_set_state(sm, WPA_COMPLETED);
1657 } else {
1658 wpa_supplicant_key_neg_complete(sm, sm->bssid,
1659 key_info &
1660 WPA_KEY_INFO_SECURE);
1661 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001662
1663 wpa_sm_set_rekey_offload(sm);
1664
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001665 return;
1666
1667failed:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001668 os_memset(&gd, 0, sizeof(gd));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001669 wpa_sm_deauthenticate(sm, WLAN_REASON_UNSPECIFIED);
1670}
1671
1672
1673static int wpa_supplicant_verify_eapol_key_mic(struct wpa_sm *sm,
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001674 struct wpa_eapol_key *key,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001675 u16 ver,
1676 const u8 *buf, size_t len)
1677{
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001678 u8 mic[WPA_EAPOL_KEY_MIC_MAX_LEN];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001679 int ok = 0;
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001680 size_t mic_len = wpa_mic_len(sm->key_mgmt);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001681
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001682 os_memcpy(mic, key + 1, mic_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001683 if (sm->tptk_set) {
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001684 os_memset(key + 1, 0, mic_len);
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001685 wpa_eapol_key_mic(sm->tptk.kck, sm->tptk.kck_len, sm->key_mgmt,
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001686 ver, buf, len, (u8 *) (key + 1));
1687 if (os_memcmp_const(mic, key + 1, mic_len) != 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001688 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1689 "WPA: Invalid EAPOL-Key MIC "
1690 "when using TPTK - ignoring TPTK");
1691 } else {
1692 ok = 1;
1693 sm->tptk_set = 0;
1694 sm->ptk_set = 1;
1695 os_memcpy(&sm->ptk, &sm->tptk, sizeof(sm->ptk));
Dmitry Shmidt61593f02014-04-21 16:27:35 -07001696 os_memset(&sm->tptk, 0, sizeof(sm->tptk));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001697 }
1698 }
1699
1700 if (!ok && sm->ptk_set) {
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001701 os_memset(key + 1, 0, mic_len);
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001702 wpa_eapol_key_mic(sm->ptk.kck, sm->ptk.kck_len, sm->key_mgmt,
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001703 ver, buf, len, (u8 *) (key + 1));
1704 if (os_memcmp_const(mic, key + 1, mic_len) != 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001705 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1706 "WPA: Invalid EAPOL-Key MIC - "
1707 "dropping packet");
1708 return -1;
1709 }
1710 ok = 1;
1711 }
1712
1713 if (!ok) {
1714 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1715 "WPA: Could not verify EAPOL-Key MIC - "
1716 "dropping packet");
1717 return -1;
1718 }
1719
1720 os_memcpy(sm->rx_replay_counter, key->replay_counter,
1721 WPA_REPLAY_COUNTER_LEN);
1722 sm->rx_replay_counter_set = 1;
1723 return 0;
1724}
1725
1726
1727/* Decrypt RSN EAPOL-Key key data (RC4 or AES-WRAP) */
1728static int wpa_supplicant_decrypt_key_data(struct wpa_sm *sm,
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001729 struct wpa_eapol_key *key,
1730 size_t mic_len, u16 ver,
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001731 u8 *key_data, size_t *key_data_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001732{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001733 wpa_hexdump(MSG_DEBUG, "RSN: encrypted key data",
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001734 key_data, *key_data_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001735 if (!sm->ptk_set) {
1736 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1737 "WPA: PTK not available, cannot decrypt EAPOL-Key Key "
1738 "Data");
1739 return -1;
1740 }
1741
1742 /* Decrypt key data here so that this operation does not need
1743 * to be implemented separately for each message type. */
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001744 if (ver == WPA_KEY_INFO_TYPE_HMAC_MD5_RC4 && sm->ptk.kek_len == 16) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001745#ifdef CONFIG_NO_RC4
1746 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1747 "WPA: RC4 not supported in the build");
1748 return -1;
1749#else /* CONFIG_NO_RC4 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001750 u8 ek[32];
1751 os_memcpy(ek, key->key_iv, 16);
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001752 os_memcpy(ek + 16, sm->ptk.kek, sm->ptk.kek_len);
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001753 if (rc4_skip(ek, 32, 256, key_data, *key_data_len)) {
Dmitry Shmidt61593f02014-04-21 16:27:35 -07001754 os_memset(ek, 0, sizeof(ek));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001755 wpa_msg(sm->ctx->msg_ctx, MSG_ERROR,
1756 "WPA: RC4 failed");
1757 return -1;
1758 }
Dmitry Shmidt61593f02014-04-21 16:27:35 -07001759 os_memset(ek, 0, sizeof(ek));
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001760#endif /* CONFIG_NO_RC4 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001761 } else if (ver == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES ||
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001762 ver == WPA_KEY_INFO_TYPE_AES_128_CMAC ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001763 sm->key_mgmt == WPA_KEY_MGMT_OSEN ||
1764 wpa_key_mgmt_suite_b(sm->key_mgmt)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001765 u8 *buf;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001766 if (*key_data_len < 8 || *key_data_len % 8) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001767 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001768 "WPA: Unsupported AES-WRAP len %u",
1769 (unsigned int) *key_data_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001770 return -1;
1771 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001772 *key_data_len -= 8; /* AES-WRAP adds 8 bytes */
1773 buf = os_malloc(*key_data_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001774 if (buf == NULL) {
1775 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1776 "WPA: No memory for AES-UNWRAP buffer");
1777 return -1;
1778 }
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001779 if (aes_unwrap(sm->ptk.kek, sm->ptk.kek_len, *key_data_len / 8,
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001780 key_data, buf)) {
Dmitry Shmidt7d56b752015-12-22 10:59:44 -08001781 bin_clear_free(buf, *key_data_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001782 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1783 "WPA: AES unwrap failed - "
1784 "could not decrypt EAPOL-Key key data");
1785 return -1;
1786 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001787 os_memcpy(key_data, buf, *key_data_len);
Dmitry Shmidt7d56b752015-12-22 10:59:44 -08001788 bin_clear_free(buf, *key_data_len);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001789 WPA_PUT_BE16(((u8 *) (key + 1)) + mic_len, *key_data_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001790 } else {
1791 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1792 "WPA: Unsupported key_info type %d", ver);
1793 return -1;
1794 }
1795 wpa_hexdump_key(MSG_DEBUG, "WPA: decrypted EAPOL-Key key data",
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001796 key_data, *key_data_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001797 return 0;
1798}
1799
1800
1801/**
1802 * wpa_sm_aborted_cached - Notify WPA that PMKSA caching was aborted
1803 * @sm: Pointer to WPA state machine data from wpa_sm_init()
1804 */
1805void wpa_sm_aborted_cached(struct wpa_sm *sm)
1806{
1807 if (sm && sm->cur_pmksa) {
1808 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1809 "RSN: Cancelling PMKSA caching attempt");
1810 sm->cur_pmksa = NULL;
1811 }
1812}
1813
1814
1815static void wpa_eapol_key_dump(struct wpa_sm *sm,
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001816 const struct wpa_eapol_key *key,
1817 unsigned int key_data_len,
1818 const u8 *mic, unsigned int mic_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001819{
1820#ifndef CONFIG_NO_STDOUT_DEBUG
1821 u16 key_info = WPA_GET_BE16(key->key_info);
1822
1823 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, " EAPOL-Key type=%d", key->type);
1824 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1825 " key_info 0x%x (ver=%d keyidx=%d rsvd=%d %s%s%s%s%s%s%s%s)",
1826 key_info, key_info & WPA_KEY_INFO_TYPE_MASK,
1827 (key_info & WPA_KEY_INFO_KEY_INDEX_MASK) >>
1828 WPA_KEY_INFO_KEY_INDEX_SHIFT,
1829 (key_info & (BIT(13) | BIT(14) | BIT(15))) >> 13,
1830 key_info & WPA_KEY_INFO_KEY_TYPE ? "Pairwise" : "Group",
1831 key_info & WPA_KEY_INFO_INSTALL ? " Install" : "",
1832 key_info & WPA_KEY_INFO_ACK ? " Ack" : "",
1833 key_info & WPA_KEY_INFO_MIC ? " MIC" : "",
1834 key_info & WPA_KEY_INFO_SECURE ? " Secure" : "",
1835 key_info & WPA_KEY_INFO_ERROR ? " Error" : "",
1836 key_info & WPA_KEY_INFO_REQUEST ? " Request" : "",
1837 key_info & WPA_KEY_INFO_ENCR_KEY_DATA ? " Encr" : "");
1838 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1839 " key_length=%u key_data_length=%u",
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001840 WPA_GET_BE16(key->key_length), key_data_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001841 wpa_hexdump(MSG_DEBUG, " replay_counter",
1842 key->replay_counter, WPA_REPLAY_COUNTER_LEN);
1843 wpa_hexdump(MSG_DEBUG, " key_nonce", key->key_nonce, WPA_NONCE_LEN);
1844 wpa_hexdump(MSG_DEBUG, " key_iv", key->key_iv, 16);
1845 wpa_hexdump(MSG_DEBUG, " key_rsc", key->key_rsc, 8);
1846 wpa_hexdump(MSG_DEBUG, " key_id (reserved)", key->key_id, 8);
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001847 wpa_hexdump(MSG_DEBUG, " key_mic", mic, mic_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001848#endif /* CONFIG_NO_STDOUT_DEBUG */
1849}
1850
1851
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001852#ifdef CONFIG_FILS
1853static int wpa_supp_aead_decrypt(struct wpa_sm *sm, u8 *buf, size_t buf_len,
1854 size_t *key_data_len)
1855{
1856 struct wpa_ptk *ptk;
1857 struct ieee802_1x_hdr *hdr;
1858 struct wpa_eapol_key *key;
1859 u8 *pos, *tmp;
1860 const u8 *aad[1];
1861 size_t aad_len[1];
1862
1863 if (*key_data_len < AES_BLOCK_SIZE) {
1864 wpa_printf(MSG_INFO, "No room for AES-SIV data in the frame");
1865 return -1;
1866 }
1867
1868 if (sm->tptk_set)
1869 ptk = &sm->tptk;
1870 else if (sm->ptk_set)
1871 ptk = &sm->ptk;
1872 else
1873 return -1;
1874
1875 hdr = (struct ieee802_1x_hdr *) buf;
1876 key = (struct wpa_eapol_key *) (hdr + 1);
1877 pos = (u8 *) (key + 1);
1878 pos += 2; /* Pointing at the Encrypted Key Data field */
1879
1880 tmp = os_malloc(*key_data_len);
1881 if (!tmp)
1882 return -1;
1883
1884 /* AES-SIV AAD from EAPOL protocol version field (inclusive) to
1885 * to Key Data (exclusive). */
1886 aad[0] = buf;
1887 aad_len[0] = pos - buf;
1888 if (aes_siv_decrypt(ptk->kek, ptk->kek_len, pos, *key_data_len,
1889 1, aad, aad_len, tmp) < 0) {
1890 wpa_printf(MSG_INFO, "Invalid AES-SIV data in the frame");
1891 bin_clear_free(tmp, *key_data_len);
1892 return -1;
1893 }
1894
1895 /* AEAD decryption and validation completed successfully */
1896 (*key_data_len) -= AES_BLOCK_SIZE;
1897 wpa_hexdump_key(MSG_DEBUG, "WPA: Decrypted Key Data",
1898 tmp, *key_data_len);
1899
1900 /* Replace Key Data field with the decrypted version */
1901 os_memcpy(pos, tmp, *key_data_len);
1902 pos -= 2; /* Key Data Length field */
1903 WPA_PUT_BE16(pos, *key_data_len);
1904 bin_clear_free(tmp, *key_data_len);
1905
1906 if (sm->tptk_set) {
1907 sm->tptk_set = 0;
1908 sm->ptk_set = 1;
1909 os_memcpy(&sm->ptk, &sm->tptk, sizeof(sm->ptk));
1910 os_memset(&sm->tptk, 0, sizeof(sm->tptk));
1911 }
1912
1913 os_memcpy(sm->rx_replay_counter, key->replay_counter,
1914 WPA_REPLAY_COUNTER_LEN);
1915 sm->rx_replay_counter_set = 1;
1916
1917 return 0;
1918}
1919#endif /* CONFIG_FILS */
1920
1921
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001922/**
1923 * wpa_sm_rx_eapol - Process received WPA EAPOL frames
1924 * @sm: Pointer to WPA state machine data from wpa_sm_init()
1925 * @src_addr: Source MAC address of the EAPOL packet
1926 * @buf: Pointer to the beginning of the EAPOL data (EAPOL header)
1927 * @len: Length of the EAPOL frame
1928 * Returns: 1 = WPA EAPOL-Key processed, 0 = not a WPA EAPOL-Key, -1 failure
1929 *
1930 * This function is called for each received EAPOL frame. Other than EAPOL-Key
1931 * frames can be skipped if filtering is done elsewhere. wpa_sm_rx_eapol() is
1932 * only processing WPA and WPA2 EAPOL-Key frames.
1933 *
1934 * The received EAPOL-Key packets are validated and valid packets are replied
1935 * to. In addition, key material (PTK, GTK) is configured at the end of a
1936 * successful key handshake.
1937 */
1938int wpa_sm_rx_eapol(struct wpa_sm *sm, const u8 *src_addr,
1939 const u8 *buf, size_t len)
1940{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001941 size_t plen, data_len, key_data_len;
1942 const struct ieee802_1x_hdr *hdr;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001943 struct wpa_eapol_key *key;
1944 u16 key_info, ver;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001945 u8 *tmp = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001946 int ret = -1;
1947 struct wpa_peerkey *peerkey = NULL;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001948 u8 *mic, *key_data;
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001949 size_t mic_len, keyhdrlen;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001950
1951#ifdef CONFIG_IEEE80211R
1952 sm->ft_completed = 0;
1953#endif /* CONFIG_IEEE80211R */
1954
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001955 mic_len = wpa_mic_len(sm->key_mgmt);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001956 keyhdrlen = sizeof(*key) + mic_len + 2;
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001957
1958 if (len < sizeof(*hdr) + keyhdrlen) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001959 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1960 "WPA: EAPOL frame too short to be a WPA "
1961 "EAPOL-Key (len %lu, expecting at least %lu)",
1962 (unsigned long) len,
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001963 (unsigned long) sizeof(*hdr) + keyhdrlen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001964 return 0;
1965 }
1966
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001967 hdr = (const struct ieee802_1x_hdr *) buf;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001968 plen = be_to_host16(hdr->length);
1969 data_len = plen + sizeof(*hdr);
1970 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1971 "IEEE 802.1X RX: version=%d type=%d length=%lu",
1972 hdr->version, hdr->type, (unsigned long) plen);
1973
1974 if (hdr->version < EAPOL_VERSION) {
1975 /* TODO: backwards compatibility */
1976 }
1977 if (hdr->type != IEEE802_1X_TYPE_EAPOL_KEY) {
1978 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1979 "WPA: EAPOL frame (type %u) discarded, "
1980 "not a Key frame", hdr->type);
1981 ret = 0;
1982 goto out;
1983 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001984 wpa_hexdump(MSG_MSGDUMP, "WPA: RX EAPOL-Key", buf, len);
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001985 if (plen > len - sizeof(*hdr) || plen < keyhdrlen) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001986 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1987 "WPA: EAPOL frame payload size %lu "
1988 "invalid (frame size %lu)",
1989 (unsigned long) plen, (unsigned long) len);
1990 ret = 0;
1991 goto out;
1992 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001993 if (data_len < len) {
1994 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1995 "WPA: ignoring %lu bytes after the IEEE 802.1X data",
1996 (unsigned long) len - data_len);
1997 }
1998
1999 /*
2000 * Make a copy of the frame since we need to modify the buffer during
2001 * MAC validation and Key Data decryption.
2002 */
2003 tmp = os_malloc(data_len);
2004 if (tmp == NULL)
2005 goto out;
2006 os_memcpy(tmp, buf, data_len);
2007 key = (struct wpa_eapol_key *) (tmp + sizeof(struct ieee802_1x_hdr));
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002008 mic = (u8 *) (key + 1);
2009 key_data = mic + mic_len + 2;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002010
2011 if (key->type != EAPOL_KEY_TYPE_WPA && key->type != EAPOL_KEY_TYPE_RSN)
2012 {
2013 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2014 "WPA: EAPOL-Key type (%d) unknown, discarded",
2015 key->type);
2016 ret = 0;
2017 goto out;
2018 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002019
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002020 key_data_len = WPA_GET_BE16(mic + mic_len);
2021 wpa_eapol_key_dump(sm, key, key_data_len, mic, mic_len);
Dmitry Shmidt807291d2015-01-27 13:40:23 -08002022
2023 if (key_data_len > plen - keyhdrlen) {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002024 wpa_msg(sm->ctx->msg_ctx, MSG_INFO, "WPA: Invalid EAPOL-Key "
2025 "frame - key_data overflow (%u > %u)",
2026 (unsigned int) key_data_len,
Dmitry Shmidt807291d2015-01-27 13:40:23 -08002027 (unsigned int) (plen - keyhdrlen));
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002028 goto out;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002029 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002030
2031 eapol_sm_notify_lower_layer_success(sm->eapol, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002032 key_info = WPA_GET_BE16(key->key_info);
2033 ver = key_info & WPA_KEY_INFO_TYPE_MASK;
2034 if (ver != WPA_KEY_INFO_TYPE_HMAC_MD5_RC4 &&
2035#if defined(CONFIG_IEEE80211R) || defined(CONFIG_IEEE80211W)
2036 ver != WPA_KEY_INFO_TYPE_AES_128_CMAC &&
2037#endif /* CONFIG_IEEE80211R || CONFIG_IEEE80211W */
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002038 ver != WPA_KEY_INFO_TYPE_HMAC_SHA1_AES &&
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002039 !wpa_key_mgmt_suite_b(sm->key_mgmt) &&
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002040 !wpa_key_mgmt_fils(sm->key_mgmt) &&
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002041 sm->key_mgmt != WPA_KEY_MGMT_OSEN) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002042 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2043 "WPA: Unsupported EAPOL-Key descriptor version %d",
2044 ver);
2045 goto out;
2046 }
2047
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002048 if (sm->key_mgmt == WPA_KEY_MGMT_OSEN &&
2049 ver != WPA_KEY_INFO_TYPE_AKM_DEFINED) {
2050 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2051 "OSEN: Unsupported EAPOL-Key descriptor version %d",
2052 ver);
2053 goto out;
2054 }
2055
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002056 if ((wpa_key_mgmt_suite_b(sm->key_mgmt) ||
2057 wpa_key_mgmt_fils(sm->key_mgmt)) &&
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002058 ver != WPA_KEY_INFO_TYPE_AKM_DEFINED) {
2059 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2060 "RSN: Unsupported EAPOL-Key descriptor version %d (expected AKM defined = 0)",
2061 ver);
2062 goto out;
2063 }
2064
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002065#ifdef CONFIG_IEEE80211R
2066 if (wpa_key_mgmt_ft(sm->key_mgmt)) {
2067 /* IEEE 802.11r uses a new key_info type (AES-128-CMAC). */
2068 if (ver != WPA_KEY_INFO_TYPE_AES_128_CMAC) {
2069 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2070 "FT: AP did not use AES-128-CMAC");
2071 goto out;
2072 }
2073 } else
2074#endif /* CONFIG_IEEE80211R */
2075#ifdef CONFIG_IEEE80211W
2076 if (wpa_key_mgmt_sha256(sm->key_mgmt)) {
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002077 if (ver != WPA_KEY_INFO_TYPE_AES_128_CMAC &&
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002078 sm->key_mgmt != WPA_KEY_MGMT_OSEN &&
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002079 !wpa_key_mgmt_fils(sm->key_mgmt) &&
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002080 !wpa_key_mgmt_suite_b(sm->key_mgmt)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002081 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2082 "WPA: AP did not use the "
2083 "negotiated AES-128-CMAC");
2084 goto out;
2085 }
2086 } else
2087#endif /* CONFIG_IEEE80211W */
2088 if (sm->pairwise_cipher == WPA_CIPHER_CCMP &&
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002089 !wpa_key_mgmt_suite_b(sm->key_mgmt) &&
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002090 !wpa_key_mgmt_fils(sm->key_mgmt) &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002091 ver != WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
2092 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2093 "WPA: CCMP is used, but EAPOL-Key "
2094 "descriptor version (%d) is not 2", ver);
2095 if (sm->group_cipher != WPA_CIPHER_CCMP &&
2096 !(key_info & WPA_KEY_INFO_KEY_TYPE)) {
2097 /* Earlier versions of IEEE 802.11i did not explicitly
2098 * require version 2 descriptor for all EAPOL-Key
2099 * packets, so allow group keys to use version 1 if
2100 * CCMP is not used for them. */
2101 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2102 "WPA: Backwards compatibility: allow invalid "
2103 "version for non-CCMP group keys");
Jouni Malinen658fb4a2014-11-14 20:57:05 +02002104 } else if (ver == WPA_KEY_INFO_TYPE_AES_128_CMAC) {
2105 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2106 "WPA: Interoperability workaround: allow incorrect (should have been HMAC-SHA1), but stronger (is AES-128-CMAC), descriptor version to be used");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002107 } else
2108 goto out;
Dmitry Shmidt71757432014-06-02 13:50:35 -07002109 } else if (sm->pairwise_cipher == WPA_CIPHER_GCMP &&
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002110 !wpa_key_mgmt_suite_b(sm->key_mgmt) &&
Dmitry Shmidt71757432014-06-02 13:50:35 -07002111 ver != WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002112 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2113 "WPA: GCMP is used, but EAPOL-Key "
2114 "descriptor version (%d) is not 2", ver);
2115 goto out;
2116 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002117
2118#ifdef CONFIG_PEERKEY
2119 for (peerkey = sm->peerkey; peerkey; peerkey = peerkey->next) {
2120 if (os_memcmp(peerkey->addr, src_addr, ETH_ALEN) == 0)
2121 break;
2122 }
2123
2124 if (!(key_info & WPA_KEY_INFO_SMK_MESSAGE) && peerkey) {
2125 if (!peerkey->initiator && peerkey->replay_counter_set &&
2126 os_memcmp(key->replay_counter, peerkey->replay_counter,
2127 WPA_REPLAY_COUNTER_LEN) <= 0) {
2128 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
2129 "RSN: EAPOL-Key Replay Counter did not "
2130 "increase (STK) - dropping packet");
2131 goto out;
2132 } else if (peerkey->initiator) {
2133 u8 _tmp[WPA_REPLAY_COUNTER_LEN];
2134 os_memcpy(_tmp, key->replay_counter,
2135 WPA_REPLAY_COUNTER_LEN);
2136 inc_byte_array(_tmp, WPA_REPLAY_COUNTER_LEN);
2137 if (os_memcmp(_tmp, peerkey->replay_counter,
2138 WPA_REPLAY_COUNTER_LEN) != 0) {
2139 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2140 "RSN: EAPOL-Key Replay "
2141 "Counter did not match (STK) - "
2142 "dropping packet");
2143 goto out;
2144 }
2145 }
2146 }
2147
2148 if (peerkey && peerkey->initiator && (key_info & WPA_KEY_INFO_ACK)) {
2149 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2150 "RSN: Ack bit in key_info from STK peer");
2151 goto out;
2152 }
2153#endif /* CONFIG_PEERKEY */
2154
2155 if (!peerkey && sm->rx_replay_counter_set &&
2156 os_memcmp(key->replay_counter, sm->rx_replay_counter,
2157 WPA_REPLAY_COUNTER_LEN) <= 0) {
2158 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
2159 "WPA: EAPOL-Key Replay Counter did not increase - "
2160 "dropping packet");
2161 goto out;
2162 }
2163
2164 if (!(key_info & (WPA_KEY_INFO_ACK | WPA_KEY_INFO_SMK_MESSAGE))
2165#ifdef CONFIG_PEERKEY
2166 && (peerkey == NULL || !peerkey->initiator)
2167#endif /* CONFIG_PEERKEY */
2168 ) {
2169 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2170 "WPA: No Ack bit in key_info");
2171 goto out;
2172 }
2173
2174 if (key_info & WPA_KEY_INFO_REQUEST) {
2175 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2176 "WPA: EAPOL-Key with Request bit - dropped");
2177 goto out;
2178 }
2179
2180 if ((key_info & WPA_KEY_INFO_MIC) && !peerkey &&
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002181 wpa_supplicant_verify_eapol_key_mic(sm, key, ver, tmp, data_len))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002182 goto out;
2183
2184#ifdef CONFIG_PEERKEY
2185 if ((key_info & WPA_KEY_INFO_MIC) && peerkey &&
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002186 peerkey_verify_eapol_key_mic(sm, peerkey, key, ver, tmp,
Dmitry Shmidt807291d2015-01-27 13:40:23 -08002187 data_len))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002188 goto out;
2189#endif /* CONFIG_PEERKEY */
2190
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002191#ifdef CONFIG_FILS
2192 if (!mic_len && (key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
2193 if (wpa_supp_aead_decrypt(sm, tmp, data_len, &key_data_len))
2194 goto out;
2195 }
2196#endif /* CONFIG_FILS */
2197
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002198 if ((sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN) &&
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002199 (key_info & WPA_KEY_INFO_ENCR_KEY_DATA) && mic_len) {
2200 if (wpa_supplicant_decrypt_key_data(sm, key, mic_len,
2201 ver, key_data,
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002202 &key_data_len))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002203 goto out;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002204 }
2205
2206 if (key_info & WPA_KEY_INFO_KEY_TYPE) {
2207 if (key_info & WPA_KEY_INFO_KEY_INDEX_MASK) {
2208 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
2209 "WPA: Ignored EAPOL-Key (Pairwise) with "
2210 "non-zero key index");
2211 goto out;
2212 }
2213 if (peerkey) {
2214 /* PeerKey 4-Way Handshake */
Dmitry Shmidtc2817022014-07-02 10:32:10 -07002215 peerkey_rx_eapol_4way(sm, peerkey, key, key_info, ver,
2216 key_data, key_data_len);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002217 } else if (key_info & (WPA_KEY_INFO_MIC |
2218 WPA_KEY_INFO_ENCR_KEY_DATA)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002219 /* 3/4 4-Way Handshake */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002220 wpa_supplicant_process_3_of_4(sm, key, ver, key_data,
2221 key_data_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002222 } else {
2223 /* 1/4 4-Way Handshake */
2224 wpa_supplicant_process_1_of_4(sm, src_addr, key,
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002225 ver, key_data,
2226 key_data_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002227 }
2228 } else if (key_info & WPA_KEY_INFO_SMK_MESSAGE) {
2229 /* PeerKey SMK Handshake */
Dmitry Shmidt29333592017-01-09 12:27:11 -08002230 peerkey_rx_eapol_smk(sm, src_addr, key, key_data, key_data_len,
2231 key_info, ver);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002232 } else {
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002233 if ((mic_len && (key_info & WPA_KEY_INFO_MIC)) ||
2234 (!mic_len && (key_info & WPA_KEY_INFO_ENCR_KEY_DATA))) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002235 /* 1/2 Group Key Handshake */
2236 wpa_supplicant_process_1_of_2(sm, src_addr, key,
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002237 key_data, key_data_len,
2238 ver);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002239 } else {
2240 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002241 "WPA: EAPOL-Key (Group) without Mic/Encr bit - "
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002242 "dropped");
2243 }
2244 }
2245
2246 ret = 1;
2247
2248out:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002249 bin_clear_free(tmp, data_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002250 return ret;
2251}
2252
2253
2254#ifdef CONFIG_CTRL_IFACE
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002255static u32 wpa_key_mgmt_suite(struct wpa_sm *sm)
2256{
2257 switch (sm->key_mgmt) {
2258 case WPA_KEY_MGMT_IEEE8021X:
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002259 return ((sm->proto == WPA_PROTO_RSN ||
2260 sm->proto == WPA_PROTO_OSEN) ?
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002261 RSN_AUTH_KEY_MGMT_UNSPEC_802_1X :
2262 WPA_AUTH_KEY_MGMT_UNSPEC_802_1X);
2263 case WPA_KEY_MGMT_PSK:
2264 return (sm->proto == WPA_PROTO_RSN ?
2265 RSN_AUTH_KEY_MGMT_PSK_OVER_802_1X :
2266 WPA_AUTH_KEY_MGMT_PSK_OVER_802_1X);
2267#ifdef CONFIG_IEEE80211R
2268 case WPA_KEY_MGMT_FT_IEEE8021X:
2269 return RSN_AUTH_KEY_MGMT_FT_802_1X;
2270 case WPA_KEY_MGMT_FT_PSK:
2271 return RSN_AUTH_KEY_MGMT_FT_PSK;
2272#endif /* CONFIG_IEEE80211R */
2273#ifdef CONFIG_IEEE80211W
2274 case WPA_KEY_MGMT_IEEE8021X_SHA256:
2275 return RSN_AUTH_KEY_MGMT_802_1X_SHA256;
2276 case WPA_KEY_MGMT_PSK_SHA256:
2277 return RSN_AUTH_KEY_MGMT_PSK_SHA256;
2278#endif /* CONFIG_IEEE80211W */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002279 case WPA_KEY_MGMT_CCKM:
2280 return (sm->proto == WPA_PROTO_RSN ?
2281 RSN_AUTH_KEY_MGMT_CCKM:
2282 WPA_AUTH_KEY_MGMT_CCKM);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002283 case WPA_KEY_MGMT_WPA_NONE:
2284 return WPA_AUTH_KEY_MGMT_NONE;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002285 case WPA_KEY_MGMT_IEEE8021X_SUITE_B:
2286 return RSN_AUTH_KEY_MGMT_802_1X_SUITE_B;
Dmitry Shmidt807291d2015-01-27 13:40:23 -08002287 case WPA_KEY_MGMT_IEEE8021X_SUITE_B_192:
2288 return RSN_AUTH_KEY_MGMT_802_1X_SUITE_B_192;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002289 default:
2290 return 0;
2291 }
2292}
2293
2294
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002295#define RSN_SUITE "%02x-%02x-%02x-%d"
2296#define RSN_SUITE_ARG(s) \
2297((s) >> 24) & 0xff, ((s) >> 16) & 0xff, ((s) >> 8) & 0xff, (s) & 0xff
2298
2299/**
2300 * wpa_sm_get_mib - Dump text list of MIB entries
2301 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2302 * @buf: Buffer for the list
2303 * @buflen: Length of the buffer
2304 * Returns: Number of bytes written to buffer
2305 *
2306 * This function is used fetch dot11 MIB variables.
2307 */
2308int wpa_sm_get_mib(struct wpa_sm *sm, char *buf, size_t buflen)
2309{
2310 char pmkid_txt[PMKID_LEN * 2 + 1];
2311 int rsna, ret;
2312 size_t len;
2313
2314 if (sm->cur_pmksa) {
2315 wpa_snprintf_hex(pmkid_txt, sizeof(pmkid_txt),
2316 sm->cur_pmksa->pmkid, PMKID_LEN);
2317 } else
2318 pmkid_txt[0] = '\0';
2319
2320 if ((wpa_key_mgmt_wpa_psk(sm->key_mgmt) ||
2321 wpa_key_mgmt_wpa_ieee8021x(sm->key_mgmt)) &&
2322 sm->proto == WPA_PROTO_RSN)
2323 rsna = 1;
2324 else
2325 rsna = 0;
2326
2327 ret = os_snprintf(buf, buflen,
2328 "dot11RSNAOptionImplemented=TRUE\n"
2329 "dot11RSNAPreauthenticationImplemented=TRUE\n"
2330 "dot11RSNAEnabled=%s\n"
2331 "dot11RSNAPreauthenticationEnabled=%s\n"
2332 "dot11RSNAConfigVersion=%d\n"
2333 "dot11RSNAConfigPairwiseKeysSupported=5\n"
2334 "dot11RSNAConfigGroupCipherSize=%d\n"
2335 "dot11RSNAConfigPMKLifetime=%d\n"
2336 "dot11RSNAConfigPMKReauthThreshold=%d\n"
2337 "dot11RSNAConfigNumberOfPTKSAReplayCounters=1\n"
2338 "dot11RSNAConfigSATimeout=%d\n",
2339 rsna ? "TRUE" : "FALSE",
2340 rsna ? "TRUE" : "FALSE",
2341 RSN_VERSION,
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07002342 wpa_cipher_key_len(sm->group_cipher) * 8,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002343 sm->dot11RSNAConfigPMKLifetime,
2344 sm->dot11RSNAConfigPMKReauthThreshold,
2345 sm->dot11RSNAConfigSATimeout);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002346 if (os_snprintf_error(buflen, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002347 return 0;
2348 len = ret;
2349
2350 ret = os_snprintf(
2351 buf + len, buflen - len,
2352 "dot11RSNAAuthenticationSuiteSelected=" RSN_SUITE "\n"
2353 "dot11RSNAPairwiseCipherSelected=" RSN_SUITE "\n"
2354 "dot11RSNAGroupCipherSelected=" RSN_SUITE "\n"
2355 "dot11RSNAPMKIDUsed=%s\n"
2356 "dot11RSNAAuthenticationSuiteRequested=" RSN_SUITE "\n"
2357 "dot11RSNAPairwiseCipherRequested=" RSN_SUITE "\n"
2358 "dot11RSNAGroupCipherRequested=" RSN_SUITE "\n"
2359 "dot11RSNAConfigNumberOfGTKSAReplayCounters=0\n"
2360 "dot11RSNA4WayHandshakeFailures=%u\n",
2361 RSN_SUITE_ARG(wpa_key_mgmt_suite(sm)),
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07002362 RSN_SUITE_ARG(wpa_cipher_to_suite(sm->proto,
2363 sm->pairwise_cipher)),
2364 RSN_SUITE_ARG(wpa_cipher_to_suite(sm->proto,
2365 sm->group_cipher)),
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002366 pmkid_txt,
2367 RSN_SUITE_ARG(wpa_key_mgmt_suite(sm)),
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07002368 RSN_SUITE_ARG(wpa_cipher_to_suite(sm->proto,
2369 sm->pairwise_cipher)),
2370 RSN_SUITE_ARG(wpa_cipher_to_suite(sm->proto,
2371 sm->group_cipher)),
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002372 sm->dot11RSNA4WayHandshakeFailures);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002373 if (!os_snprintf_error(buflen - len, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002374 len += ret;
2375
2376 return (int) len;
2377}
2378#endif /* CONFIG_CTRL_IFACE */
2379
2380
2381static void wpa_sm_pmksa_free_cb(struct rsn_pmksa_cache_entry *entry,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002382 void *ctx, enum pmksa_free_reason reason)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002383{
2384 struct wpa_sm *sm = ctx;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002385 int deauth = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002386
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002387 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "RSN: PMKSA cache entry free_cb: "
2388 MACSTR " reason=%d", MAC2STR(entry->aa), reason);
2389
2390 if (sm->cur_pmksa == entry) {
2391 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2392 "RSN: %s current PMKSA entry",
2393 reason == PMKSA_REPLACE ? "replaced" : "removed");
2394 pmksa_cache_clear_current(sm);
2395
2396 /*
2397 * If an entry is simply being replaced, there's no need to
2398 * deauthenticate because it will be immediately re-added.
2399 * This happens when EAP authentication is completed again
2400 * (reauth or failed PMKSA caching attempt).
2401 */
2402 if (reason != PMKSA_REPLACE)
2403 deauth = 1;
2404 }
2405
2406 if (reason == PMKSA_EXPIRE &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002407 (sm->pmk_len == entry->pmk_len &&
2408 os_memcmp(sm->pmk, entry->pmk, sm->pmk_len) == 0)) {
2409 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002410 "RSN: deauthenticating due to expired PMK");
2411 pmksa_cache_clear_current(sm);
2412 deauth = 1;
2413 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002414
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002415 if (deauth) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002416 os_memset(sm->pmk, 0, sizeof(sm->pmk));
2417 wpa_sm_deauthenticate(sm, WLAN_REASON_UNSPECIFIED);
2418 }
2419}
2420
2421
2422/**
2423 * wpa_sm_init - Initialize WPA state machine
2424 * @ctx: Context pointer for callbacks; this needs to be an allocated buffer
2425 * Returns: Pointer to the allocated WPA state machine data
2426 *
2427 * This function is used to allocate a new WPA state machine and the returned
2428 * value is passed to all WPA state machine calls.
2429 */
2430struct wpa_sm * wpa_sm_init(struct wpa_sm_ctx *ctx)
2431{
2432 struct wpa_sm *sm;
2433
2434 sm = os_zalloc(sizeof(*sm));
2435 if (sm == NULL)
2436 return NULL;
2437 dl_list_init(&sm->pmksa_candidates);
2438 sm->renew_snonce = 1;
2439 sm->ctx = ctx;
2440
2441 sm->dot11RSNAConfigPMKLifetime = 43200;
2442 sm->dot11RSNAConfigPMKReauthThreshold = 70;
2443 sm->dot11RSNAConfigSATimeout = 60;
2444
2445 sm->pmksa = pmksa_cache_init(wpa_sm_pmksa_free_cb, sm, sm);
2446 if (sm->pmksa == NULL) {
2447 wpa_msg(sm->ctx->msg_ctx, MSG_ERROR,
2448 "RSN: PMKSA cache initialization failed");
2449 os_free(sm);
2450 return NULL;
2451 }
2452
2453 return sm;
2454}
2455
2456
2457/**
2458 * wpa_sm_deinit - Deinitialize WPA state machine
2459 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2460 */
2461void wpa_sm_deinit(struct wpa_sm *sm)
2462{
2463 if (sm == NULL)
2464 return;
2465 pmksa_cache_deinit(sm->pmksa);
2466 eloop_cancel_timeout(wpa_sm_start_preauth, sm, NULL);
2467 eloop_cancel_timeout(wpa_sm_rekey_ptk, sm, NULL);
2468 os_free(sm->assoc_wpa_ie);
2469 os_free(sm->ap_wpa_ie);
2470 os_free(sm->ap_rsn_ie);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002471 wpa_sm_drop_sa(sm);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002472 os_free(sm->ctx);
2473 peerkey_deinit(sm);
2474#ifdef CONFIG_IEEE80211R
2475 os_free(sm->assoc_resp_ies);
2476#endif /* CONFIG_IEEE80211R */
Dmitry Shmidt55840ad2015-12-14 12:45:46 -08002477#ifdef CONFIG_TESTING_OPTIONS
2478 wpabuf_free(sm->test_assoc_ie);
2479#endif /* CONFIG_TESTING_OPTIONS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002480 os_free(sm);
2481}
2482
2483
2484/**
2485 * wpa_sm_notify_assoc - Notify WPA state machine about association
2486 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2487 * @bssid: The BSSID of the new association
2488 *
2489 * This function is called to let WPA state machine know that the connection
2490 * was established.
2491 */
2492void wpa_sm_notify_assoc(struct wpa_sm *sm, const u8 *bssid)
2493{
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02002494 int clear_keys = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002495
2496 if (sm == NULL)
2497 return;
2498
2499 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2500 "WPA: Association event - clear replay counter");
2501 os_memcpy(sm->bssid, bssid, ETH_ALEN);
2502 os_memset(sm->rx_replay_counter, 0, WPA_REPLAY_COUNTER_LEN);
2503 sm->rx_replay_counter_set = 0;
2504 sm->renew_snonce = 1;
2505 if (os_memcmp(sm->preauth_bssid, bssid, ETH_ALEN) == 0)
2506 rsn_preauth_deinit(sm);
2507
2508#ifdef CONFIG_IEEE80211R
2509 if (wpa_ft_is_completed(sm)) {
2510 /*
2511 * Clear portValid to kick EAPOL state machine to re-enter
2512 * AUTHENTICATED state to get the EAPOL port Authorized.
2513 */
2514 eapol_sm_notify_portValid(sm->eapol, FALSE);
2515 wpa_supplicant_key_neg_complete(sm, sm->bssid, 1);
2516
2517 /* Prepare for the next transition */
2518 wpa_ft_prepare_auth_request(sm, NULL);
2519
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02002520 clear_keys = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002521 }
2522#endif /* CONFIG_IEEE80211R */
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002523#ifdef CONFIG_FILS
2524 if (sm->fils_completed) {
2525 /*
2526 * Clear portValid to kick EAPOL state machine to re-enter
2527 * AUTHENTICATED state to get the EAPOL port Authorized.
2528 */
2529 wpa_supplicant_key_neg_complete(sm, sm->bssid, 1);
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02002530 clear_keys = 0;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002531 }
2532#endif /* CONFIG_FILS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002533
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02002534 if (clear_keys) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002535 /*
2536 * IEEE 802.11, 8.4.10: Delete PTK SA on (re)association if
2537 * this is not part of a Fast BSS Transition.
2538 */
2539 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Clear old PTK");
2540 sm->ptk_set = 0;
Dmitry Shmidt61593f02014-04-21 16:27:35 -07002541 os_memset(&sm->ptk, 0, sizeof(sm->ptk));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002542 sm->tptk_set = 0;
Dmitry Shmidt61593f02014-04-21 16:27:35 -07002543 os_memset(&sm->tptk, 0, sizeof(sm->tptk));
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02002544 os_memset(&sm->gtk, 0, sizeof(sm->gtk));
2545#ifdef CONFIG_IEEE80211W
2546 os_memset(&sm->igtk, 0, sizeof(sm->igtk));
2547#endif /* CONFIG_IEEE80211W */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002548 }
2549
2550#ifdef CONFIG_TDLS
2551 wpa_tdls_assoc(sm);
2552#endif /* CONFIG_TDLS */
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002553
2554#ifdef CONFIG_P2P
2555 os_memset(sm->p2p_ip_addr, 0, sizeof(sm->p2p_ip_addr));
2556#endif /* CONFIG_P2P */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002557}
2558
2559
2560/**
2561 * wpa_sm_notify_disassoc - Notify WPA state machine about disassociation
2562 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2563 *
2564 * This function is called to let WPA state machine know that the connection
2565 * was lost. This will abort any existing pre-authentication session.
2566 */
2567void wpa_sm_notify_disassoc(struct wpa_sm *sm)
2568{
Dmitry Shmidt4dd28dc2015-03-10 11:21:43 -07002569 eloop_cancel_timeout(wpa_sm_start_preauth, sm, NULL);
2570 eloop_cancel_timeout(wpa_sm_rekey_ptk, sm, NULL);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002571 peerkey_deinit(sm);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002572 rsn_preauth_deinit(sm);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002573 pmksa_cache_clear_current(sm);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002574 if (wpa_sm_get_state(sm) == WPA_4WAY_HANDSHAKE)
2575 sm->dot11RSNA4WayHandshakeFailures++;
2576#ifdef CONFIG_TDLS
2577 wpa_tdls_disassoc(sm);
2578#endif /* CONFIG_TDLS */
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002579#ifdef CONFIG_FILS
2580 sm->fils_completed = 0;
2581#endif /* CONFIG_FILS */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002582
2583 /* Keys are not needed in the WPA state machine anymore */
2584 wpa_sm_drop_sa(sm);
Dmitry Shmidt4dd28dc2015-03-10 11:21:43 -07002585
2586 sm->msg_3_of_4_ok = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002587}
2588
2589
2590/**
2591 * wpa_sm_set_pmk - Set PMK
2592 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2593 * @pmk: The new PMK
2594 * @pmk_len: The length of the new PMK in bytes
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08002595 * @pmkid: Calculated PMKID
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002596 * @bssid: AA to add into PMKSA cache or %NULL to not cache the PMK
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002597 *
2598 * Configure the PMK for WPA state machine.
2599 */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002600void wpa_sm_set_pmk(struct wpa_sm *sm, const u8 *pmk, size_t pmk_len,
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08002601 const u8 *pmkid, const u8 *bssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002602{
2603 if (sm == NULL)
2604 return;
2605
2606 sm->pmk_len = pmk_len;
2607 os_memcpy(sm->pmk, pmk, pmk_len);
2608
2609#ifdef CONFIG_IEEE80211R
2610 /* Set XXKey to be PSK for FT key derivation */
2611 sm->xxkey_len = pmk_len;
2612 os_memcpy(sm->xxkey, pmk, pmk_len);
2613#endif /* CONFIG_IEEE80211R */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002614
2615 if (bssid) {
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08002616 pmksa_cache_add(sm->pmksa, pmk, pmk_len, pmkid, NULL, 0,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002617 bssid, sm->own_addr,
2618 sm->network_ctx, sm->key_mgmt);
2619 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002620}
2621
2622
2623/**
2624 * wpa_sm_set_pmk_from_pmksa - Set PMK based on the current PMKSA
2625 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2626 *
2627 * Take the PMK from the current PMKSA into use. If no PMKSA is active, the PMK
2628 * will be cleared.
2629 */
2630void wpa_sm_set_pmk_from_pmksa(struct wpa_sm *sm)
2631{
2632 if (sm == NULL)
2633 return;
2634
2635 if (sm->cur_pmksa) {
2636 sm->pmk_len = sm->cur_pmksa->pmk_len;
2637 os_memcpy(sm->pmk, sm->cur_pmksa->pmk, sm->pmk_len);
2638 } else {
2639 sm->pmk_len = PMK_LEN;
2640 os_memset(sm->pmk, 0, PMK_LEN);
2641 }
2642}
2643
2644
2645/**
2646 * wpa_sm_set_fast_reauth - Set fast reauthentication (EAP) enabled/disabled
2647 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2648 * @fast_reauth: Whether fast reauthentication (EAP) is allowed
2649 */
2650void wpa_sm_set_fast_reauth(struct wpa_sm *sm, int fast_reauth)
2651{
2652 if (sm)
2653 sm->fast_reauth = fast_reauth;
2654}
2655
2656
2657/**
2658 * wpa_sm_set_scard_ctx - Set context pointer for smartcard callbacks
2659 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2660 * @scard_ctx: Context pointer for smartcard related callback functions
2661 */
2662void wpa_sm_set_scard_ctx(struct wpa_sm *sm, void *scard_ctx)
2663{
2664 if (sm == NULL)
2665 return;
2666 sm->scard_ctx = scard_ctx;
2667 if (sm->preauth_eapol)
2668 eapol_sm_register_scard_ctx(sm->preauth_eapol, scard_ctx);
2669}
2670
2671
2672/**
2673 * wpa_sm_set_config - Notification of current configration change
2674 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2675 * @config: Pointer to current network configuration
2676 *
2677 * Notify WPA state machine that configuration has changed. config will be
2678 * stored as a backpointer to network configuration. This can be %NULL to clear
2679 * the stored pointed.
2680 */
2681void wpa_sm_set_config(struct wpa_sm *sm, struct rsn_supp_config *config)
2682{
2683 if (!sm)
2684 return;
2685
2686 if (config) {
2687 sm->network_ctx = config->network_ctx;
2688 sm->peerkey_enabled = config->peerkey_enabled;
2689 sm->allowed_pairwise_cipher = config->allowed_pairwise_cipher;
2690 sm->proactive_key_caching = config->proactive_key_caching;
2691 sm->eap_workaround = config->eap_workaround;
2692 sm->eap_conf_ctx = config->eap_conf_ctx;
2693 if (config->ssid) {
2694 os_memcpy(sm->ssid, config->ssid, config->ssid_len);
2695 sm->ssid_len = config->ssid_len;
2696 } else
2697 sm->ssid_len = 0;
2698 sm->wpa_ptk_rekey = config->wpa_ptk_rekey;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002699 sm->p2p = config->p2p;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002700 sm->wpa_rsc_relaxation = config->wpa_rsc_relaxation;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002701 } else {
2702 sm->network_ctx = NULL;
2703 sm->peerkey_enabled = 0;
2704 sm->allowed_pairwise_cipher = 0;
2705 sm->proactive_key_caching = 0;
2706 sm->eap_workaround = 0;
2707 sm->eap_conf_ctx = NULL;
2708 sm->ssid_len = 0;
2709 sm->wpa_ptk_rekey = 0;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002710 sm->p2p = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002711 sm->wpa_rsc_relaxation = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002712 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002713}
2714
2715
2716/**
2717 * wpa_sm_set_own_addr - Set own MAC address
2718 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2719 * @addr: Own MAC address
2720 */
2721void wpa_sm_set_own_addr(struct wpa_sm *sm, const u8 *addr)
2722{
2723 if (sm)
2724 os_memcpy(sm->own_addr, addr, ETH_ALEN);
2725}
2726
2727
2728/**
2729 * wpa_sm_set_ifname - Set network interface name
2730 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2731 * @ifname: Interface name
2732 * @bridge_ifname: Optional bridge interface name (for pre-auth)
2733 */
2734void wpa_sm_set_ifname(struct wpa_sm *sm, const char *ifname,
2735 const char *bridge_ifname)
2736{
2737 if (sm) {
2738 sm->ifname = ifname;
2739 sm->bridge_ifname = bridge_ifname;
2740 }
2741}
2742
2743
2744/**
2745 * wpa_sm_set_eapol - Set EAPOL state machine pointer
2746 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2747 * @eapol: Pointer to EAPOL state machine allocated with eapol_sm_init()
2748 */
2749void wpa_sm_set_eapol(struct wpa_sm *sm, struct eapol_sm *eapol)
2750{
2751 if (sm)
2752 sm->eapol = eapol;
2753}
2754
2755
2756/**
2757 * wpa_sm_set_param - Set WPA state machine parameters
2758 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2759 * @param: Parameter field
2760 * @value: Parameter value
2761 * Returns: 0 on success, -1 on failure
2762 */
2763int wpa_sm_set_param(struct wpa_sm *sm, enum wpa_sm_conf_params param,
2764 unsigned int value)
2765{
2766 int ret = 0;
2767
2768 if (sm == NULL)
2769 return -1;
2770
2771 switch (param) {
2772 case RSNA_PMK_LIFETIME:
2773 if (value > 0)
2774 sm->dot11RSNAConfigPMKLifetime = value;
2775 else
2776 ret = -1;
2777 break;
2778 case RSNA_PMK_REAUTH_THRESHOLD:
2779 if (value > 0 && value <= 100)
2780 sm->dot11RSNAConfigPMKReauthThreshold = value;
2781 else
2782 ret = -1;
2783 break;
2784 case RSNA_SA_TIMEOUT:
2785 if (value > 0)
2786 sm->dot11RSNAConfigSATimeout = value;
2787 else
2788 ret = -1;
2789 break;
2790 case WPA_PARAM_PROTO:
2791 sm->proto = value;
2792 break;
2793 case WPA_PARAM_PAIRWISE:
2794 sm->pairwise_cipher = value;
2795 break;
2796 case WPA_PARAM_GROUP:
2797 sm->group_cipher = value;
2798 break;
2799 case WPA_PARAM_KEY_MGMT:
2800 sm->key_mgmt = value;
2801 break;
2802#ifdef CONFIG_IEEE80211W
2803 case WPA_PARAM_MGMT_GROUP:
2804 sm->mgmt_group_cipher = value;
2805 break;
2806#endif /* CONFIG_IEEE80211W */
2807 case WPA_PARAM_RSN_ENABLED:
2808 sm->rsn_enabled = value;
2809 break;
2810 case WPA_PARAM_MFP:
2811 sm->mfp = value;
2812 break;
2813 default:
2814 break;
2815 }
2816
2817 return ret;
2818}
2819
2820
2821/**
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002822 * wpa_sm_get_status - Get WPA state machine
2823 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2824 * @buf: Buffer for status information
2825 * @buflen: Maximum buffer length
2826 * @verbose: Whether to include verbose status information
2827 * Returns: Number of bytes written to buf.
2828 *
2829 * Query WPA state machine for status information. This function fills in
2830 * a text area with current status information. If the buffer (buf) is not
2831 * large enough, status information will be truncated to fit the buffer.
2832 */
2833int wpa_sm_get_status(struct wpa_sm *sm, char *buf, size_t buflen,
2834 int verbose)
2835{
2836 char *pos = buf, *end = buf + buflen;
2837 int ret;
2838
2839 ret = os_snprintf(pos, end - pos,
2840 "pairwise_cipher=%s\n"
2841 "group_cipher=%s\n"
2842 "key_mgmt=%s\n",
2843 wpa_cipher_txt(sm->pairwise_cipher),
2844 wpa_cipher_txt(sm->group_cipher),
2845 wpa_key_mgmt_txt(sm->key_mgmt, sm->proto));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002846 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002847 return pos - buf;
2848 pos += ret;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002849
2850 if (sm->mfp != NO_MGMT_FRAME_PROTECTION && sm->ap_rsn_ie) {
2851 struct wpa_ie_data rsn;
2852 if (wpa_parse_wpa_ie_rsn(sm->ap_rsn_ie, sm->ap_rsn_ie_len, &rsn)
2853 >= 0 &&
2854 rsn.capabilities & (WPA_CAPABILITY_MFPR |
2855 WPA_CAPABILITY_MFPC)) {
2856 ret = os_snprintf(pos, end - pos, "pmf=%d\n",
2857 (rsn.capabilities &
2858 WPA_CAPABILITY_MFPR) ? 2 : 1);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002859 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002860 return pos - buf;
2861 pos += ret;
2862 }
2863 }
2864
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002865 return pos - buf;
2866}
2867
2868
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07002869int wpa_sm_pmf_enabled(struct wpa_sm *sm)
2870{
2871 struct wpa_ie_data rsn;
2872
2873 if (sm->mfp == NO_MGMT_FRAME_PROTECTION || !sm->ap_rsn_ie)
2874 return 0;
2875
2876 if (wpa_parse_wpa_ie_rsn(sm->ap_rsn_ie, sm->ap_rsn_ie_len, &rsn) >= 0 &&
2877 rsn.capabilities & (WPA_CAPABILITY_MFPR | WPA_CAPABILITY_MFPC))
2878 return 1;
2879
2880 return 0;
2881}
2882
2883
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002884/**
2885 * wpa_sm_set_assoc_wpa_ie_default - Generate own WPA/RSN IE from configuration
2886 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2887 * @wpa_ie: Pointer to buffer for WPA/RSN IE
2888 * @wpa_ie_len: Pointer to the length of the wpa_ie buffer
2889 * Returns: 0 on success, -1 on failure
2890 */
2891int wpa_sm_set_assoc_wpa_ie_default(struct wpa_sm *sm, u8 *wpa_ie,
2892 size_t *wpa_ie_len)
2893{
2894 int res;
2895
2896 if (sm == NULL)
2897 return -1;
2898
Dmitry Shmidt55840ad2015-12-14 12:45:46 -08002899#ifdef CONFIG_TESTING_OPTIONS
2900 if (sm->test_assoc_ie) {
2901 wpa_printf(MSG_DEBUG,
2902 "TESTING: Replace association WPA/RSN IE");
2903 if (*wpa_ie_len < wpabuf_len(sm->test_assoc_ie))
2904 return -1;
2905 os_memcpy(wpa_ie, wpabuf_head(sm->test_assoc_ie),
2906 wpabuf_len(sm->test_assoc_ie));
2907 res = wpabuf_len(sm->test_assoc_ie);
2908 } else
2909#endif /* CONFIG_TESTING_OPTIONS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002910 res = wpa_gen_wpa_ie(sm, wpa_ie, *wpa_ie_len);
2911 if (res < 0)
2912 return -1;
2913 *wpa_ie_len = res;
2914
2915 wpa_hexdump(MSG_DEBUG, "WPA: Set own WPA IE default",
2916 wpa_ie, *wpa_ie_len);
2917
2918 if (sm->assoc_wpa_ie == NULL) {
2919 /*
2920 * Make a copy of the WPA/RSN IE so that 4-Way Handshake gets
2921 * the correct version of the IE even if PMKSA caching is
2922 * aborted (which would remove PMKID from IE generation).
2923 */
2924 sm->assoc_wpa_ie = os_malloc(*wpa_ie_len);
2925 if (sm->assoc_wpa_ie == NULL)
2926 return -1;
2927
2928 os_memcpy(sm->assoc_wpa_ie, wpa_ie, *wpa_ie_len);
2929 sm->assoc_wpa_ie_len = *wpa_ie_len;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002930 } else {
2931 wpa_hexdump(MSG_DEBUG,
2932 "WPA: Leave previously set WPA IE default",
2933 sm->assoc_wpa_ie, sm->assoc_wpa_ie_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002934 }
2935
2936 return 0;
2937}
2938
2939
2940/**
2941 * wpa_sm_set_assoc_wpa_ie - Set own WPA/RSN IE from (Re)AssocReq
2942 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2943 * @ie: Pointer to IE data (starting from id)
2944 * @len: IE length
2945 * Returns: 0 on success, -1 on failure
2946 *
2947 * Inform WPA state machine about the WPA/RSN IE used in (Re)Association
2948 * Request frame. The IE will be used to override the default value generated
2949 * with wpa_sm_set_assoc_wpa_ie_default().
2950 */
2951int wpa_sm_set_assoc_wpa_ie(struct wpa_sm *sm, const u8 *ie, size_t len)
2952{
2953 if (sm == NULL)
2954 return -1;
2955
2956 os_free(sm->assoc_wpa_ie);
2957 if (ie == NULL || len == 0) {
2958 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2959 "WPA: clearing own WPA/RSN IE");
2960 sm->assoc_wpa_ie = NULL;
2961 sm->assoc_wpa_ie_len = 0;
2962 } else {
2963 wpa_hexdump(MSG_DEBUG, "WPA: set own WPA/RSN IE", ie, len);
2964 sm->assoc_wpa_ie = os_malloc(len);
2965 if (sm->assoc_wpa_ie == NULL)
2966 return -1;
2967
2968 os_memcpy(sm->assoc_wpa_ie, ie, len);
2969 sm->assoc_wpa_ie_len = len;
2970 }
2971
2972 return 0;
2973}
2974
2975
2976/**
2977 * wpa_sm_set_ap_wpa_ie - Set AP WPA IE from Beacon/ProbeResp
2978 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2979 * @ie: Pointer to IE data (starting from id)
2980 * @len: IE length
2981 * Returns: 0 on success, -1 on failure
2982 *
2983 * Inform WPA state machine about the WPA IE used in Beacon / Probe Response
2984 * frame.
2985 */
2986int wpa_sm_set_ap_wpa_ie(struct wpa_sm *sm, const u8 *ie, size_t len)
2987{
2988 if (sm == NULL)
2989 return -1;
2990
2991 os_free(sm->ap_wpa_ie);
2992 if (ie == NULL || len == 0) {
2993 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2994 "WPA: clearing AP WPA IE");
2995 sm->ap_wpa_ie = NULL;
2996 sm->ap_wpa_ie_len = 0;
2997 } else {
2998 wpa_hexdump(MSG_DEBUG, "WPA: set AP WPA IE", ie, len);
2999 sm->ap_wpa_ie = os_malloc(len);
3000 if (sm->ap_wpa_ie == NULL)
3001 return -1;
3002
3003 os_memcpy(sm->ap_wpa_ie, ie, len);
3004 sm->ap_wpa_ie_len = len;
3005 }
3006
3007 return 0;
3008}
3009
3010
3011/**
3012 * wpa_sm_set_ap_rsn_ie - Set AP RSN IE from Beacon/ProbeResp
3013 * @sm: Pointer to WPA state machine data from wpa_sm_init()
3014 * @ie: Pointer to IE data (starting from id)
3015 * @len: IE length
3016 * Returns: 0 on success, -1 on failure
3017 *
3018 * Inform WPA state machine about the RSN IE used in Beacon / Probe Response
3019 * frame.
3020 */
3021int wpa_sm_set_ap_rsn_ie(struct wpa_sm *sm, const u8 *ie, size_t len)
3022{
3023 if (sm == NULL)
3024 return -1;
3025
3026 os_free(sm->ap_rsn_ie);
3027 if (ie == NULL || len == 0) {
3028 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
3029 "WPA: clearing AP RSN IE");
3030 sm->ap_rsn_ie = NULL;
3031 sm->ap_rsn_ie_len = 0;
3032 } else {
3033 wpa_hexdump(MSG_DEBUG, "WPA: set AP RSN IE", ie, len);
3034 sm->ap_rsn_ie = os_malloc(len);
3035 if (sm->ap_rsn_ie == NULL)
3036 return -1;
3037
3038 os_memcpy(sm->ap_rsn_ie, ie, len);
3039 sm->ap_rsn_ie_len = len;
3040 }
3041
3042 return 0;
3043}
3044
3045
3046/**
3047 * wpa_sm_parse_own_wpa_ie - Parse own WPA/RSN IE
3048 * @sm: Pointer to WPA state machine data from wpa_sm_init()
3049 * @data: Pointer to data area for parsing results
3050 * Returns: 0 on success, -1 if IE is not known, or -2 on parsing failure
3051 *
3052 * Parse the contents of the own WPA or RSN IE from (Re)AssocReq and write the
3053 * parsed data into data.
3054 */
3055int wpa_sm_parse_own_wpa_ie(struct wpa_sm *sm, struct wpa_ie_data *data)
3056{
3057 if (sm == NULL)
3058 return -1;
3059
3060 if (sm->assoc_wpa_ie == NULL) {
3061 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
3062 "WPA: No WPA/RSN IE available from association info");
3063 return -1;
3064 }
3065 if (wpa_parse_wpa_ie(sm->assoc_wpa_ie, sm->assoc_wpa_ie_len, data))
3066 return -2;
3067 return 0;
3068}
3069
3070
3071int wpa_sm_pmksa_cache_list(struct wpa_sm *sm, char *buf, size_t len)
3072{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003073 return pmksa_cache_list(sm->pmksa, buf, len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003074}
3075
3076
Dmitry Shmidt29333592017-01-09 12:27:11 -08003077struct rsn_pmksa_cache_entry * wpa_sm_pmksa_cache_head(struct wpa_sm *sm)
3078{
3079 return pmksa_cache_head(sm->pmksa);
3080}
3081
3082
3083struct rsn_pmksa_cache_entry *
3084wpa_sm_pmksa_cache_add_entry(struct wpa_sm *sm,
3085 struct rsn_pmksa_cache_entry * entry)
3086{
3087 return pmksa_cache_add_entry(sm->pmksa, entry);
3088}
3089
3090
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003091void wpa_sm_drop_sa(struct wpa_sm *sm)
3092{
3093 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Clear old PMK and PTK");
3094 sm->ptk_set = 0;
3095 sm->tptk_set = 0;
3096 os_memset(sm->pmk, 0, sizeof(sm->pmk));
3097 os_memset(&sm->ptk, 0, sizeof(sm->ptk));
3098 os_memset(&sm->tptk, 0, sizeof(sm->tptk));
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02003099 os_memset(&sm->gtk, 0, sizeof(sm->gtk));
3100#ifdef CONFIG_IEEE80211W
3101 os_memset(&sm->igtk, 0, sizeof(sm->igtk));
3102#endif /* CONFIG_IEEE80211W */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003103#ifdef CONFIG_IEEE80211R
3104 os_memset(sm->xxkey, 0, sizeof(sm->xxkey));
3105 os_memset(sm->pmk_r0, 0, sizeof(sm->pmk_r0));
3106 os_memset(sm->pmk_r1, 0, sizeof(sm->pmk_r1));
3107#endif /* CONFIG_IEEE80211R */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003108}
3109
3110
3111int wpa_sm_has_ptk(struct wpa_sm *sm)
3112{
3113 if (sm == NULL)
3114 return 0;
3115 return sm->ptk_set;
3116}
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003117
3118
3119void wpa_sm_update_replay_ctr(struct wpa_sm *sm, const u8 *replay_ctr)
3120{
3121 os_memcpy(sm->rx_replay_counter, replay_ctr, WPA_REPLAY_COUNTER_LEN);
3122}
3123
3124
3125void wpa_sm_pmksa_cache_flush(struct wpa_sm *sm, void *network_ctx)
3126{
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07003127 pmksa_cache_flush(sm->pmksa, network_ctx, NULL, 0);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003128}
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003129
3130
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003131#ifdef CONFIG_WNM
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003132int wpa_wnmsleep_install_key(struct wpa_sm *sm, u8 subelem_id, u8 *buf)
3133{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003134 u16 keyinfo;
3135 u8 keylen; /* plaintext key len */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003136 u8 *key_rsc;
3137
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003138 if (subelem_id == WNM_SLEEP_SUBELEM_GTK) {
Dmitry Shmidt61593f02014-04-21 16:27:35 -07003139 struct wpa_gtk_data gd;
3140
3141 os_memset(&gd, 0, sizeof(gd));
3142 keylen = wpa_cipher_key_len(sm->group_cipher);
3143 gd.key_rsc_len = wpa_cipher_rsc_len(sm->group_cipher);
3144 gd.alg = wpa_cipher_to_alg(sm->group_cipher);
3145 if (gd.alg == WPA_ALG_NONE) {
3146 wpa_printf(MSG_DEBUG, "Unsupported group cipher suite");
3147 return -1;
3148 }
3149
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003150 key_rsc = buf + 5;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003151 keyinfo = WPA_GET_LE16(buf + 2);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003152 gd.gtk_len = keylen;
3153 if (gd.gtk_len != buf[4]) {
3154 wpa_printf(MSG_DEBUG, "GTK len mismatch len %d vs %d",
3155 gd.gtk_len, buf[4]);
3156 return -1;
3157 }
3158 gd.keyidx = keyinfo & 0x03; /* B0 - B1 */
3159 gd.tx = wpa_supplicant_gtk_tx_bit_workaround(
3160 sm, !!(keyinfo & WPA_KEY_INFO_TXRX));
3161
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003162 os_memcpy(gd.gtk, buf + 13, gd.gtk_len);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003163
3164 wpa_hexdump_key(MSG_DEBUG, "Install GTK (WNM SLEEP)",
3165 gd.gtk, gd.gtk_len);
3166 if (wpa_supplicant_install_gtk(sm, &gd, key_rsc)) {
Dmitry Shmidt61593f02014-04-21 16:27:35 -07003167 os_memset(&gd, 0, sizeof(gd));
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003168 wpa_printf(MSG_DEBUG, "Failed to install the GTK in "
3169 "WNM mode");
3170 return -1;
3171 }
Dmitry Shmidt61593f02014-04-21 16:27:35 -07003172 os_memset(&gd, 0, sizeof(gd));
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003173#ifdef CONFIG_IEEE80211W
3174 } else if (subelem_id == WNM_SLEEP_SUBELEM_IGTK) {
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02003175 const struct wpa_igtk_kde *igtk;
Dmitry Shmidt61593f02014-04-21 16:27:35 -07003176
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02003177 igtk = (const struct wpa_igtk_kde *) (buf + 2);
3178 if (wpa_supplicant_install_igtk(sm, igtk) < 0)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003179 return -1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003180#endif /* CONFIG_IEEE80211W */
3181 } else {
3182 wpa_printf(MSG_DEBUG, "Unknown element id");
3183 return -1;
3184 }
3185
3186 return 0;
3187}
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003188#endif /* CONFIG_WNM */
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003189
3190
3191#ifdef CONFIG_PEERKEY
3192int wpa_sm_rx_eapol_peerkey(struct wpa_sm *sm, const u8 *src_addr,
3193 const u8 *buf, size_t len)
3194{
3195 struct wpa_peerkey *peerkey;
3196
3197 for (peerkey = sm->peerkey; peerkey; peerkey = peerkey->next) {
3198 if (os_memcmp(peerkey->addr, src_addr, ETH_ALEN) == 0)
3199 break;
3200 }
3201
3202 if (!peerkey)
3203 return 0;
3204
3205 wpa_sm_rx_eapol(sm, src_addr, buf, len);
3206
3207 return 1;
3208}
3209#endif /* CONFIG_PEERKEY */
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003210
3211
3212#ifdef CONFIG_P2P
3213
3214int wpa_sm_get_p2p_ip_addr(struct wpa_sm *sm, u8 *buf)
3215{
3216 if (sm == NULL || WPA_GET_BE32(sm->p2p_ip_addr) == 0)
3217 return -1;
3218 os_memcpy(buf, sm->p2p_ip_addr, 3 * 4);
3219 return 0;
3220}
3221
3222#endif /* CONFIG_P2P */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003223
3224
3225void wpa_sm_set_rx_replay_ctr(struct wpa_sm *sm, const u8 *rx_replay_counter)
3226{
3227 if (rx_replay_counter == NULL)
3228 return;
3229
3230 os_memcpy(sm->rx_replay_counter, rx_replay_counter,
3231 WPA_REPLAY_COUNTER_LEN);
3232 sm->rx_replay_counter_set = 1;
3233 wpa_printf(MSG_DEBUG, "Updated key replay counter");
3234}
3235
3236
Dmitry Shmidt807291d2015-01-27 13:40:23 -08003237void wpa_sm_set_ptk_kck_kek(struct wpa_sm *sm,
3238 const u8 *ptk_kck, size_t ptk_kck_len,
3239 const u8 *ptk_kek, size_t ptk_kek_len)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003240{
Dmitry Shmidt807291d2015-01-27 13:40:23 -08003241 if (ptk_kck && ptk_kck_len <= WPA_KCK_MAX_LEN) {
3242 os_memcpy(sm->ptk.kck, ptk_kck, ptk_kck_len);
3243 sm->ptk.kck_len = ptk_kck_len;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003244 wpa_printf(MSG_DEBUG, "Updated PTK KCK");
3245 }
Dmitry Shmidt807291d2015-01-27 13:40:23 -08003246 if (ptk_kek && ptk_kek_len <= WPA_KEK_MAX_LEN) {
3247 os_memcpy(sm->ptk.kek, ptk_kek, ptk_kek_len);
3248 sm->ptk.kek_len = ptk_kek_len;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003249 wpa_printf(MSG_DEBUG, "Updated PTK KEK");
3250 }
3251 sm->ptk_set = 1;
3252}
Dmitry Shmidt55840ad2015-12-14 12:45:46 -08003253
3254
3255#ifdef CONFIG_TESTING_OPTIONS
3256void wpa_sm_set_test_assoc_ie(struct wpa_sm *sm, struct wpabuf *buf)
3257{
3258 wpabuf_free(sm->test_assoc_ie);
3259 sm->test_assoc_ie = buf;
3260}
3261#endif /* CONFIG_TESTING_OPTIONS */
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003262
3263
3264#ifdef CONFIG_FILS
3265
3266struct wpabuf * fils_build_auth(struct wpa_sm *sm)
3267{
3268 struct wpabuf *buf = NULL;
3269 struct wpabuf *erp_msg;
3270
3271 erp_msg = eapol_sm_build_erp_reauth_start(sm->eapol);
3272 if (!erp_msg && !sm->cur_pmksa) {
3273 wpa_printf(MSG_DEBUG,
3274 "FILS: Neither ERP EAP-Initiate/Re-auth nor PMKSA cache entry is available - skip FILS");
3275 goto fail;
3276 }
3277
3278 wpa_printf(MSG_DEBUG, "FILS: Try to use FILS (erp=%d pmksa_cache=%d)",
3279 erp_msg != NULL, sm->cur_pmksa != NULL);
3280
3281 sm->fils_completed = 0;
3282
3283 if (!sm->assoc_wpa_ie) {
3284 wpa_printf(MSG_INFO, "FILS: No own RSN IE set for FILS");
3285 goto fail;
3286 }
3287
3288 if (random_get_bytes(sm->fils_nonce, FILS_NONCE_LEN) < 0 ||
3289 random_get_bytes(sm->fils_session, FILS_SESSION_LEN) < 0)
3290 goto fail;
3291
3292 wpa_hexdump(MSG_DEBUG, "FILS: Generated FILS Nonce",
3293 sm->fils_nonce, FILS_NONCE_LEN);
3294 wpa_hexdump(MSG_DEBUG, "FILS: Generated FILS Session",
3295 sm->fils_session, FILS_SESSION_LEN);
3296
3297 buf = wpabuf_alloc(1000 + sm->assoc_wpa_ie_len);
3298 if (!buf)
3299 goto fail;
3300
3301 /* Fields following the Authentication algorithm number field */
3302
3303 /* Authentication Transaction seq# */
3304 wpabuf_put_le16(buf, 1);
3305
3306 /* Status Code */
3307 wpabuf_put_le16(buf, WLAN_STATUS_SUCCESS);
3308
3309 /* TODO: Finite Cyclic Group when using PK or PFS */
3310 /* TODO: Element when using PK or PFS */
3311
3312 /* RSNE */
3313 wpa_hexdump(MSG_DEBUG, "FILS: RSNE in FILS Authentication frame",
3314 sm->assoc_wpa_ie, sm->assoc_wpa_ie_len);
3315 wpabuf_put_data(buf, sm->assoc_wpa_ie, sm->assoc_wpa_ie_len);
3316
3317 /* TODO: MDE when using FILS for FT initial association */
3318 /* TODO: FTE when using FILS for FT initial association */
3319
3320 /* FILS Nonce */
3321 wpabuf_put_u8(buf, WLAN_EID_EXTENSION); /* Element ID */
3322 wpabuf_put_u8(buf, 1 + FILS_NONCE_LEN); /* Length */
3323 /* Element ID Extension */
3324 wpabuf_put_u8(buf, WLAN_EID_EXT_FILS_NONCE);
3325 wpabuf_put_data(buf, sm->fils_nonce, FILS_NONCE_LEN);
3326
3327 /* FILS Session */
3328 wpabuf_put_u8(buf, WLAN_EID_EXTENSION); /* Element ID */
3329 wpabuf_put_u8(buf, 1 + FILS_SESSION_LEN); /* Length */
3330 /* Element ID Extension */
3331 wpabuf_put_u8(buf, WLAN_EID_EXT_FILS_SESSION);
3332 wpabuf_put_data(buf, sm->fils_session, FILS_SESSION_LEN);
3333
3334 /* FILS Wrapped Data */
Paul Stewart092955c2017-02-06 09:13:09 -08003335 sm->fils_erp_pmkid_set = 0;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003336 if (erp_msg) {
3337 wpabuf_put_u8(buf, WLAN_EID_EXTENSION); /* Element ID */
3338 wpabuf_put_u8(buf, 1 + wpabuf_len(erp_msg)); /* Length */
3339 /* Element ID Extension */
3340 wpabuf_put_u8(buf, WLAN_EID_EXT_FILS_WRAPPED_DATA);
3341 wpabuf_put_buf(buf, erp_msg);
Paul Stewart092955c2017-02-06 09:13:09 -08003342 /* Calculate pending PMKID here so that we do not need to
3343 * maintain a copy of the EAP-Initiate/Reauth message. */
3344 if (fils_pmkid_erp(sm->key_mgmt, wpabuf_head(erp_msg),
3345 wpabuf_len(erp_msg),
3346 sm->fils_erp_pmkid) == 0)
3347 sm->fils_erp_pmkid_set = 1;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003348 }
3349
3350 wpa_hexdump_buf(MSG_DEBUG, "RSN: FILS fields for Authentication frame",
3351 buf);
3352
3353fail:
3354 wpabuf_free(erp_msg);
3355 return buf;
3356}
3357
3358
3359int fils_process_auth(struct wpa_sm *sm, const u8 *data, size_t len)
3360{
3361 const u8 *pos, *end;
3362 struct ieee802_11_elems elems;
3363 struct wpa_ie_data rsn;
3364 int pmkid_match = 0;
3365 u8 ick[FILS_ICK_MAX_LEN];
3366 size_t ick_len;
3367 int res;
3368
3369 wpa_hexdump(MSG_DEBUG, "FILS: Authentication frame fields",
3370 data, len);
3371 pos = data;
3372 end = data + len;
3373
3374 /* TODO: Finite Cyclic Group when using PK or PFS */
3375 /* TODO: Element when using PK or PFS */
3376
3377 wpa_hexdump(MSG_DEBUG, "FILS: Remaining IEs", pos, end - pos);
3378 if (ieee802_11_parse_elems(pos, end - pos, &elems, 1) == ParseFailed) {
3379 wpa_printf(MSG_DEBUG, "FILS: Could not parse elements");
3380 return -1;
3381 }
3382
3383 /* RSNE */
3384 wpa_hexdump(MSG_DEBUG, "FILS: RSN element", elems.rsn_ie,
3385 elems.rsn_ie_len);
3386 if (!elems.rsn_ie ||
3387 wpa_parse_wpa_ie_rsn(elems.rsn_ie - 2, elems.rsn_ie_len + 2,
3388 &rsn) < 0) {
3389 wpa_printf(MSG_DEBUG, "FILS: No RSN element");
3390 return -1;
3391 }
3392
3393 if (!elems.fils_nonce) {
3394 wpa_printf(MSG_DEBUG, "FILS: No FILS Nonce field");
3395 return -1;
3396 }
3397 os_memcpy(sm->fils_anonce, elems.fils_nonce, FILS_NONCE_LEN);
3398 wpa_hexdump(MSG_DEBUG, "FILS: ANonce", sm->fils_anonce, FILS_NONCE_LEN);
3399
3400 /* TODO: MDE when using FILS+FT */
3401 /* TODO: FTE when using FILS+FT */
3402
3403 /* PMKID List */
3404 if (rsn.pmkid && rsn.num_pmkid > 0) {
3405 wpa_hexdump(MSG_DEBUG, "FILS: PMKID List",
3406 rsn.pmkid, rsn.num_pmkid * PMKID_LEN);
3407
3408 if (rsn.num_pmkid != 1) {
3409 wpa_printf(MSG_DEBUG, "FILS: Invalid PMKID selection");
3410 return -1;
3411 }
3412 wpa_hexdump(MSG_DEBUG, "FILS: PMKID", rsn.pmkid, PMKID_LEN);
3413 if (os_memcmp(sm->cur_pmksa->pmkid, rsn.pmkid, PMKID_LEN) != 0)
3414 {
3415 wpa_printf(MSG_DEBUG, "FILS: PMKID mismatch");
3416 wpa_hexdump(MSG_DEBUG, "FILS: Expected PMKID",
3417 sm->cur_pmksa->pmkid, PMKID_LEN);
3418 return -1;
3419 }
3420 wpa_printf(MSG_DEBUG,
3421 "FILS: Matching PMKID - continue using PMKSA caching");
3422 pmkid_match = 1;
3423 }
3424 if (!pmkid_match && sm->cur_pmksa) {
3425 wpa_printf(MSG_DEBUG,
3426 "FILS: No PMKID match - cannot use cached PMKSA entry");
3427 sm->cur_pmksa = NULL;
3428 }
3429
3430 /* FILS Session */
3431 if (!elems.fils_session) {
3432 wpa_printf(MSG_DEBUG, "FILS: No FILS Session element");
3433 return -1;
3434 }
3435 wpa_hexdump(MSG_DEBUG, "FILS: FILS Session", elems.fils_session,
3436 FILS_SESSION_LEN);
3437 if (os_memcmp(sm->fils_session, elems.fils_session, FILS_SESSION_LEN)
3438 != 0) {
3439 wpa_printf(MSG_DEBUG, "FILS: Session mismatch");
3440 wpa_hexdump(MSG_DEBUG, "FILS: Expected FILS Session",
3441 sm->fils_session, FILS_SESSION_LEN);
3442 return -1;
3443 }
3444
3445 /* FILS Wrapped Data */
3446 if (!sm->cur_pmksa && elems.fils_wrapped_data) {
Paul Stewart092955c2017-02-06 09:13:09 -08003447 u8 rmsk[ERP_MAX_KEY_LEN];
3448 size_t rmsk_len;
3449
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003450 wpa_hexdump(MSG_DEBUG, "FILS: Wrapped Data",
3451 elems.fils_wrapped_data,
3452 elems.fils_wrapped_data_len);
3453 eapol_sm_process_erp_finish(sm->eapol, elems.fils_wrapped_data,
3454 elems.fils_wrapped_data_len);
3455 if (eapol_sm_failed(sm->eapol))
3456 return -1;
3457
Paul Stewart092955c2017-02-06 09:13:09 -08003458 rmsk_len = ERP_MAX_KEY_LEN;
3459 res = eapol_sm_get_key(sm->eapol, rmsk, rmsk_len);
3460 if (res == PMK_LEN) {
3461 rmsk_len = PMK_LEN;
3462 res = eapol_sm_get_key(sm->eapol, rmsk, rmsk_len);
3463 }
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003464 if (res)
3465 return -1;
3466
Paul Stewart092955c2017-02-06 09:13:09 -08003467 res = fils_rmsk_to_pmk(sm->key_mgmt, rmsk, rmsk_len,
3468 sm->fils_nonce, sm->fils_anonce, NULL, 0,
3469 sm->pmk, &sm->pmk_len);
3470 os_memset(rmsk, 0, sizeof(rmsk));
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08003471 if (res)
3472 return -1;
Paul Stewart092955c2017-02-06 09:13:09 -08003473
3474 if (!sm->fils_erp_pmkid_set) {
3475 wpa_printf(MSG_DEBUG, "FILS: PMKID not available");
3476 return -1;
3477 }
3478 wpa_hexdump(MSG_DEBUG, "FILS: PMKID", sm->fils_erp_pmkid,
3479 PMKID_LEN);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003480 wpa_printf(MSG_DEBUG, "FILS: ERP processing succeeded - add PMKSA cache entry for the result");
Paul Stewart092955c2017-02-06 09:13:09 -08003481 sm->cur_pmksa = pmksa_cache_add(sm->pmksa, sm->pmk, sm->pmk_len,
3482 sm->fils_erp_pmkid, NULL, 0,
3483 sm->bssid, sm->own_addr,
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003484 sm->network_ctx, sm->key_mgmt);
3485 }
3486
3487 if (!sm->cur_pmksa) {
3488 wpa_printf(MSG_DEBUG,
3489 "FILS: No remaining options to continue FILS authentication");
3490 return -1;
3491 }
3492
3493 if (fils_pmk_to_ptk(sm->pmk, sm->pmk_len, sm->own_addr, sm->bssid,
3494 sm->fils_nonce, sm->fils_anonce, &sm->ptk,
3495 ick, &ick_len, sm->key_mgmt, sm->pairwise_cipher) <
3496 0) {
3497 wpa_printf(MSG_DEBUG, "FILS: Failed to derive PTK");
3498 return -1;
3499 }
3500 sm->ptk_set = 1;
3501 sm->tptk_set = 0;
3502 os_memset(&sm->tptk, 0, sizeof(sm->tptk));
3503
3504 res = fils_key_auth_sk(ick, ick_len, sm->fils_nonce,
3505 sm->fils_anonce, sm->own_addr, sm->bssid,
3506 NULL, 0, NULL, 0, /* TODO: SK+PFS */
3507 sm->key_mgmt, sm->fils_key_auth_sta,
3508 sm->fils_key_auth_ap,
3509 &sm->fils_key_auth_len);
3510 os_memset(ick, 0, sizeof(ick));
3511 return res;
3512}
3513
3514
3515struct wpabuf * fils_build_assoc_req(struct wpa_sm *sm, const u8 **kek,
3516 size_t *kek_len, const u8 **snonce,
Paul Stewart092955c2017-02-06 09:13:09 -08003517 const u8 **anonce,
3518 const struct wpabuf **hlp,
3519 unsigned int num_hlp)
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003520{
3521 struct wpabuf *buf;
Paul Stewart092955c2017-02-06 09:13:09 -08003522 size_t len;
3523 unsigned int i;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003524
Paul Stewart092955c2017-02-06 09:13:09 -08003525 len = 1000;
3526 for (i = 0; hlp && i < num_hlp; i++)
3527 len += 10 + wpabuf_len(hlp[i]);
3528 buf = wpabuf_alloc(len);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003529 if (!buf)
3530 return NULL;
3531
3532 /* FILS Session */
3533 wpabuf_put_u8(buf, WLAN_EID_EXTENSION); /* Element ID */
3534 wpabuf_put_u8(buf, 1 + FILS_SESSION_LEN); /* Length */
3535 /* Element ID Extension */
3536 wpabuf_put_u8(buf, WLAN_EID_EXT_FILS_SESSION);
3537 wpabuf_put_data(buf, sm->fils_session, FILS_SESSION_LEN);
3538
3539 /* Everything after FILS Session element gets encrypted in the driver
3540 * with KEK. The buffer returned from here is the plaintext version. */
3541
3542 /* TODO: FILS Public Key */
3543
3544 /* FILS Key Confirm */
3545 wpabuf_put_u8(buf, WLAN_EID_EXTENSION); /* Element ID */
3546 wpabuf_put_u8(buf, 1 + sm->fils_key_auth_len); /* Length */
3547 /* Element ID Extension */
3548 wpabuf_put_u8(buf, WLAN_EID_EXT_FILS_KEY_CONFIRM);
3549 wpabuf_put_data(buf, sm->fils_key_auth_sta, sm->fils_key_auth_len);
3550
Paul Stewart092955c2017-02-06 09:13:09 -08003551 /* FILS HLP Container */
3552 for (i = 0; hlp && i < num_hlp; i++) {
3553 const u8 *pos = wpabuf_head(hlp[i]);
3554 size_t left = wpabuf_len(hlp[i]);
3555
3556 wpabuf_put_u8(buf, WLAN_EID_EXTENSION); /* Element ID */
3557 if (left <= 254)
3558 len = 1 + left;
3559 else
3560 len = 255;
3561 wpabuf_put_u8(buf, len); /* Length */
3562 /* Element ID Extension */
3563 wpabuf_put_u8(buf, WLAN_EID_EXT_FILS_HLP_CONTAINER);
3564 /* Destination MAC Address, Source MAC Address, HLP Packet.
3565 * HLP Packet is in MSDU format (i.e., included the LLC/SNAP
3566 * header when LPD is used). */
3567 wpabuf_put_data(buf, pos, len - 1);
3568 pos += len - 1;
3569 left -= len - 1;
3570 while (left) {
3571 wpabuf_put_u8(buf, WLAN_EID_FRAGMENT);
3572 len = left > 255 ? 255 : left;
3573 wpabuf_put_u8(buf, len);
3574 wpabuf_put_data(buf, pos, len);
3575 pos += len;
3576 left -= len;
3577 }
3578 }
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003579
3580 /* TODO: FILS IP Address Assignment */
3581
3582 wpa_hexdump_buf(MSG_DEBUG, "FILS: Association Request plaintext", buf);
3583
3584 *kek = sm->ptk.kek;
3585 *kek_len = sm->ptk.kek_len;
3586 wpa_hexdump_key(MSG_DEBUG, "FILS: KEK for AEAD", *kek, *kek_len);
3587 *snonce = sm->fils_nonce;
3588 wpa_hexdump(MSG_DEBUG, "FILS: SNonce for AEAD AAD",
3589 *snonce, FILS_NONCE_LEN);
3590 *anonce = sm->fils_anonce;
3591 wpa_hexdump(MSG_DEBUG, "FILS: ANonce for AEAD AAD",
3592 *anonce, FILS_NONCE_LEN);
3593
3594 return buf;
3595}
3596
3597
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08003598static void fils_process_hlp_resp(struct wpa_sm *sm, const u8 *resp, size_t len)
3599{
3600 const u8 *pos, *end;
3601
3602 wpa_hexdump(MSG_MSGDUMP, "FILS: HLP response", resp, len);
3603 if (len < 2 * ETH_ALEN)
3604 return;
3605 pos = resp + 2 * ETH_ALEN;
3606 end = resp + len;
3607 if (end - pos >= 6 &&
3608 os_memcmp(pos, "\xaa\xaa\x03\x00\x00\x00", 6) == 0)
3609 pos += 6; /* Remove SNAP/LLC header */
3610 wpa_sm_fils_hlp_rx(sm, resp, resp + ETH_ALEN, pos, end - pos);
3611}
3612
3613
3614static void fils_process_hlp_container(struct wpa_sm *sm, const u8 *pos,
3615 size_t len)
3616{
3617 const u8 *end = pos + len;
3618 u8 *tmp, *tmp_pos;
3619
3620 /* Check if there are any FILS HLP Container elements */
3621 while (end - pos >= 2) {
3622 if (2 + pos[1] > end - pos)
3623 return;
3624 if (pos[0] == WLAN_EID_EXTENSION &&
3625 pos[1] >= 1 + 2 * ETH_ALEN &&
3626 pos[2] == WLAN_EID_EXT_FILS_HLP_CONTAINER)
3627 break;
3628 pos += 2 + pos[1];
3629 }
3630 if (end - pos < 2)
3631 return; /* No FILS HLP Container elements */
3632
3633 tmp = os_malloc(end - pos);
3634 if (!tmp)
3635 return;
3636
3637 while (end - pos >= 2) {
3638 if (2 + pos[1] > end - pos ||
3639 pos[0] != WLAN_EID_EXTENSION ||
3640 pos[1] < 1 + 2 * ETH_ALEN ||
3641 pos[2] != WLAN_EID_EXT_FILS_HLP_CONTAINER)
3642 break;
3643 tmp_pos = tmp;
3644 os_memcpy(tmp_pos, pos + 3, pos[1] - 1);
3645 tmp_pos += pos[1] - 1;
3646 pos += 2 + pos[1];
3647
3648 /* Add possible fragments */
3649 while (end - pos >= 2 && pos[0] == WLAN_EID_FRAGMENT &&
3650 2 + pos[1] <= end - pos) {
3651 os_memcpy(tmp_pos, pos + 2, pos[1]);
3652 tmp_pos += pos[1];
3653 pos += 2 + pos[1];
3654 }
3655
3656 fils_process_hlp_resp(sm, tmp, tmp_pos - tmp);
3657 }
3658
3659 os_free(tmp);
3660}
3661
3662
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003663int fils_process_assoc_resp(struct wpa_sm *sm, const u8 *resp, size_t len)
3664{
3665 const struct ieee80211_mgmt *mgmt;
3666 const u8 *end, *ie_start;
3667 struct ieee802_11_elems elems;
3668 int keylen, rsclen;
3669 enum wpa_alg alg;
3670 struct wpa_gtk_data gd;
3671 int maxkeylen;
3672 struct wpa_eapol_ie_parse kde;
3673
3674 if (!sm || !sm->ptk_set) {
3675 wpa_printf(MSG_DEBUG, "FILS: No KEK available");
3676 return -1;
3677 }
3678
3679 if (!wpa_key_mgmt_fils(sm->key_mgmt)) {
3680 wpa_printf(MSG_DEBUG, "FILS: Not a FILS AKM");
3681 return -1;
3682 }
3683
3684 wpa_hexdump(MSG_DEBUG, "FILS: (Re)Association Response frame",
3685 resp, len);
3686
3687 mgmt = (const struct ieee80211_mgmt *) resp;
3688 if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.assoc_resp))
3689 return -1;
3690
3691 end = resp + len;
3692 /* Same offset for Association Response and Reassociation Response */
3693 ie_start = mgmt->u.assoc_resp.variable;
3694
3695 if (ieee802_11_parse_elems(ie_start, end - ie_start, &elems, 1) ==
3696 ParseFailed) {
3697 wpa_printf(MSG_DEBUG,
3698 "FILS: Failed to parse decrypted elements");
3699 goto fail;
3700 }
3701
3702 if (!elems.fils_session) {
3703 wpa_printf(MSG_DEBUG, "FILS: No FILS Session element");
3704 return -1;
3705 }
3706 if (os_memcmp(elems.fils_session, sm->fils_session,
3707 FILS_SESSION_LEN) != 0) {
3708 wpa_printf(MSG_DEBUG, "FILS: FILS Session mismatch");
3709 wpa_hexdump(MSG_DEBUG, "FILS: Received FILS Session",
3710 elems.fils_session, FILS_SESSION_LEN);
3711 wpa_hexdump(MSG_DEBUG, "FILS: Expected FILS Session",
3712 sm->fils_session, FILS_SESSION_LEN);
3713 }
3714
3715 /* TODO: FILS Public Key */
3716
3717 if (!elems.fils_key_confirm) {
3718 wpa_printf(MSG_DEBUG, "FILS: No FILS Key Confirm element");
3719 goto fail;
3720 }
3721 if (elems.fils_key_confirm_len != sm->fils_key_auth_len) {
3722 wpa_printf(MSG_DEBUG,
3723 "FILS: Unexpected Key-Auth length %d (expected %d)",
3724 elems.fils_key_confirm_len,
3725 (int) sm->fils_key_auth_len);
3726 goto fail;
3727 }
3728 if (os_memcmp(elems.fils_key_confirm, sm->fils_key_auth_ap,
3729 sm->fils_key_auth_len) != 0) {
3730 wpa_printf(MSG_DEBUG, "FILS: Key-Auth mismatch");
3731 wpa_hexdump(MSG_DEBUG, "FILS: Received Key-Auth",
3732 elems.fils_key_confirm,
3733 elems.fils_key_confirm_len);
3734 wpa_hexdump(MSG_DEBUG, "FILS: Expected Key-Auth",
3735 sm->fils_key_auth_ap, sm->fils_key_auth_len);
3736 goto fail;
3737 }
3738
3739 /* Key Delivery */
3740 if (!elems.key_delivery) {
3741 wpa_printf(MSG_DEBUG, "FILS: No Key Delivery element");
3742 goto fail;
3743 }
3744
3745 /* Parse GTK and set the key to the driver */
3746 os_memset(&gd, 0, sizeof(gd));
3747 if (wpa_supplicant_parse_ies(elems.key_delivery + WPA_KEY_RSC_LEN,
3748 elems.key_delivery_len - WPA_KEY_RSC_LEN,
3749 &kde) < 0) {
3750 wpa_printf(MSG_DEBUG, "FILS: Failed to parse KDEs");
3751 goto fail;
3752 }
3753 if (!kde.gtk) {
3754 wpa_printf(MSG_DEBUG, "FILS: No GTK KDE");
3755 goto fail;
3756 }
3757 maxkeylen = gd.gtk_len = kde.gtk_len - 2;
3758 if (wpa_supplicant_check_group_cipher(sm, sm->group_cipher,
3759 gd.gtk_len, maxkeylen,
3760 &gd.key_rsc_len, &gd.alg))
3761 goto fail;
3762
3763 wpa_hexdump_key(MSG_DEBUG, "FILS: Received GTK", kde.gtk, kde.gtk_len);
3764 gd.keyidx = kde.gtk[0] & 0x3;
3765 gd.tx = wpa_supplicant_gtk_tx_bit_workaround(sm,
3766 !!(kde.gtk[0] & BIT(2)));
3767 if (kde.gtk_len - 2 > sizeof(gd.gtk)) {
3768 wpa_printf(MSG_DEBUG, "FILS: Too long GTK in GTK KDE (len=%lu)",
3769 (unsigned long) kde.gtk_len - 2);
3770 goto fail;
3771 }
3772 os_memcpy(gd.gtk, kde.gtk + 2, kde.gtk_len - 2);
3773
3774 wpa_printf(MSG_DEBUG, "FILS: Set GTK to driver");
3775 if (wpa_supplicant_install_gtk(sm, &gd, elems.key_delivery) < 0) {
3776 wpa_printf(MSG_DEBUG, "FILS: Failed to set GTK");
3777 goto fail;
3778 }
3779
3780 if (ieee80211w_set_keys(sm, &kde) < 0) {
3781 wpa_printf(MSG_DEBUG, "FILS: Failed to set IGTK");
3782 goto fail;
3783 }
3784
3785 alg = wpa_cipher_to_alg(sm->pairwise_cipher);
3786 keylen = wpa_cipher_key_len(sm->pairwise_cipher);
3787 rsclen = wpa_cipher_rsc_len(sm->pairwise_cipher);
3788 wpa_hexdump_key(MSG_DEBUG, "FILS: Set TK to driver",
3789 sm->ptk.tk, keylen);
3790 if (wpa_sm_set_key(sm, alg, sm->bssid, 0, 1, null_rsc, rsclen,
3791 sm->ptk.tk, keylen) < 0) {
3792 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
3793 "FILS: Failed to set PTK to the driver (alg=%d keylen=%d bssid="
3794 MACSTR ")",
3795 alg, keylen, MAC2STR(sm->bssid));
3796 goto fail;
3797 }
3798
3799 /* TODO: TK could be cleared after auth frame exchange now that driver
3800 * takes care of association frame encryption/decryption. */
3801 /* TK is not needed anymore in supplicant */
3802 os_memset(sm->ptk.tk, 0, WPA_TK_MAX_LEN);
3803
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08003804 /* FILS HLP Container */
3805 fils_process_hlp_container(sm, ie_start, end - ie_start);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003806
3807 /* TODO: FILS IP Address Assignment */
3808
3809 wpa_printf(MSG_DEBUG, "FILS: Auth+Assoc completed successfully");
3810 sm->fils_completed = 1;
3811
3812 return 0;
3813fail:
3814 return -1;
3815}
3816
3817#endif /* CONFIG_FILS */
3818
3819
3820int wpa_fils_is_completed(struct wpa_sm *sm)
3821{
3822#ifdef CONFIG_FILS
3823 return sm && sm->fils_completed;
3824#else /* CONFIG_FILS */
3825 return 0;
3826#endif /* CONFIG_FILS */
3827}