blob: 2b4d2921050ae617d50c1a33ac1ed2294c0a7ff2 [file] [log] [blame]
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001/*
2 * WPA Supplicant - WPA state machine and EAPOL-Key processing
Roshan Pius3a1667e2018-07-03 15:17:14 -07003 * Copyright (c) 2003-2018, 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 Shmidtd2986c22017-10-23 14:22:09 -070018#include "crypto/sha256.h"
19#include "crypto/sha384.h"
20#include "crypto/sha512.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070021#include "common/ieee802_11_defs.h"
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -080022#include "common/ieee802_11_common.h"
Paul Stewart092955c2017-02-06 09:13:09 -080023#include "eap_common/eap_defs.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070024#include "eapol_supp/eapol_supp_sm.h"
25#include "wpa.h"
26#include "eloop.h"
27#include "preauth.h"
28#include "pmksa_cache.h"
29#include "wpa_i.h"
30#include "wpa_ie.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070031
32
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080033static const u8 null_rsc[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
34
35
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070036/**
37 * wpa_eapol_key_send - Send WPA/RSN EAPOL-Key message
38 * @sm: Pointer to WPA state machine data from wpa_sm_init()
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -080039 * @ptk: PTK for Key Confirmation/Encryption Key
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070040 * @ver: Version field from Key Info
41 * @dest: Destination address for the frame
42 * @proto: Ethertype (usually ETH_P_EAPOL)
43 * @msg: EAPOL-Key message
44 * @msg_len: Length of message
45 * @key_mic: Pointer to the buffer to which the EAPOL-Key MIC is written
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080046 * Returns: >= 0 on success, < 0 on failure
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070047 */
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -080048int wpa_eapol_key_send(struct wpa_sm *sm, struct wpa_ptk *ptk,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080049 int ver, const u8 *dest, u16 proto,
50 u8 *msg, size_t msg_len, u8 *key_mic)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070051{
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080052 int ret = -1;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070053 size_t mic_len = wpa_mic_len(sm->key_mgmt, sm->pmk_len);
Dmitry Shmidt807291d2015-01-27 13:40:23 -080054
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070055 wpa_printf(MSG_DEBUG, "WPA: Send EAPOL-Key frame to " MACSTR
56 " ver=%d mic_len=%d key_mgmt=0x%x",
57 MAC2STR(dest), ver, (int) mic_len, sm->key_mgmt);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070058 if (is_zero_ether_addr(dest) && is_zero_ether_addr(sm->bssid)) {
59 /*
60 * Association event was not yet received; try to fetch
61 * BSSID from the driver.
62 */
63 if (wpa_sm_get_bssid(sm, sm->bssid) < 0) {
64 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
65 "WPA: Failed to read BSSID for "
66 "EAPOL-Key destination address");
67 } else {
68 dest = sm->bssid;
69 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
70 "WPA: Use BSSID (" MACSTR
71 ") as the destination for EAPOL-Key",
72 MAC2STR(dest));
73 }
74 }
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -080075
76 if (mic_len) {
77 if (key_mic && (!ptk || !ptk->kck_len))
78 goto out;
79
80 if (key_mic &&
81 wpa_eapol_key_mic(ptk->kck, ptk->kck_len, sm->key_mgmt, ver,
82 msg, msg_len, key_mic)) {
83 wpa_msg(sm->ctx->msg_ctx, MSG_ERROR,
84 "WPA: Failed to generate EAPOL-Key version %d key_mgmt 0x%x MIC",
85 ver, sm->key_mgmt);
86 goto out;
87 }
Dmitry Shmidt29333592017-01-09 12:27:11 -080088 if (ptk)
89 wpa_hexdump_key(MSG_DEBUG, "WPA: KCK",
90 ptk->kck, ptk->kck_len);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -080091 wpa_hexdump(MSG_DEBUG, "WPA: Derived Key MIC",
92 key_mic, mic_len);
93 } else {
94#ifdef CONFIG_FILS
95 /* AEAD cipher - Key MIC field not used */
96 struct ieee802_1x_hdr *s_hdr, *hdr;
97 struct wpa_eapol_key *s_key, *key;
98 u8 *buf, *s_key_data, *key_data;
99 size_t buf_len = msg_len + AES_BLOCK_SIZE;
100 size_t key_data_len;
101 u16 eapol_len;
102 const u8 *aad[1];
103 size_t aad_len[1];
104
105 if (!ptk || !ptk->kek_len)
106 goto out;
107
108 key_data_len = msg_len - sizeof(struct ieee802_1x_hdr) -
109 sizeof(struct wpa_eapol_key) - 2;
110
111 buf = os_malloc(buf_len);
112 if (!buf)
113 goto out;
114
115 os_memcpy(buf, msg, msg_len);
116 hdr = (struct ieee802_1x_hdr *) buf;
117 key = (struct wpa_eapol_key *) (hdr + 1);
118 key_data = ((u8 *) (key + 1)) + 2;
119
120 /* Update EAPOL header to include AES-SIV overhead */
121 eapol_len = be_to_host16(hdr->length);
122 eapol_len += AES_BLOCK_SIZE;
123 hdr->length = host_to_be16(eapol_len);
124
125 /* Update Key Data Length field to include AES-SIV overhead */
126 WPA_PUT_BE16((u8 *) (key + 1), AES_BLOCK_SIZE + key_data_len);
127
128 s_hdr = (struct ieee802_1x_hdr *) msg;
129 s_key = (struct wpa_eapol_key *) (s_hdr + 1);
130 s_key_data = ((u8 *) (s_key + 1)) + 2;
131
132 wpa_hexdump_key(MSG_DEBUG, "WPA: Plaintext Key Data",
133 s_key_data, key_data_len);
134
135 wpa_hexdump_key(MSG_DEBUG, "WPA: KEK", ptk->kek, ptk->kek_len);
136 /* AES-SIV AAD from EAPOL protocol version field (inclusive) to
137 * to Key Data (exclusive). */
138 aad[0] = buf;
139 aad_len[0] = key_data - buf;
140 if (aes_siv_encrypt(ptk->kek, ptk->kek_len,
141 s_key_data, key_data_len,
142 1, aad, aad_len, key_data) < 0) {
143 os_free(buf);
144 goto out;
145 }
146
147 wpa_hexdump(MSG_DEBUG, "WPA: Encrypted Key Data from SIV",
148 key_data, AES_BLOCK_SIZE + key_data_len);
149
150 os_free(msg);
151 msg = buf;
152 msg_len = buf_len;
153#else /* CONFIG_FILS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700154 goto out;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800155#endif /* CONFIG_FILS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700156 }
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800157
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700158 wpa_hexdump(MSG_MSGDUMP, "WPA: TX EAPOL-Key", msg, msg_len);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800159 ret = wpa_sm_ether_send(sm, dest, proto, msg, msg_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700160 eapol_sm_notify_tx_eapol_key(sm->eapol);
161out:
162 os_free(msg);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800163 return ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700164}
165
166
167/**
168 * wpa_sm_key_request - Send EAPOL-Key Request
169 * @sm: Pointer to WPA state machine data from wpa_sm_init()
170 * @error: Indicate whether this is an Michael MIC error report
171 * @pairwise: 1 = error report for pairwise packet, 0 = for group packet
172 *
173 * Send an EAPOL-Key Request to the current authenticator. This function is
174 * used to request rekeying and it is usually called when a local Michael MIC
175 * failure is detected.
176 */
177void wpa_sm_key_request(struct wpa_sm *sm, int error, int pairwise)
178{
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800179 size_t mic_len, hdrlen, rlen;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700180 struct wpa_eapol_key *reply;
181 int key_info, ver;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800182 u8 bssid[ETH_ALEN], *rbuf, *key_mic, *mic;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700183
Roshan Pius3a1667e2018-07-03 15:17:14 -0700184 if (wpa_use_akm_defined(sm->key_mgmt))
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800185 ver = WPA_KEY_INFO_TYPE_AKM_DEFINED;
186 else if (wpa_key_mgmt_ft(sm->key_mgmt) ||
187 wpa_key_mgmt_sha256(sm->key_mgmt))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700188 ver = WPA_KEY_INFO_TYPE_AES_128_CMAC;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700189 else if (sm->pairwise_cipher != WPA_CIPHER_TKIP)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700190 ver = WPA_KEY_INFO_TYPE_HMAC_SHA1_AES;
191 else
192 ver = WPA_KEY_INFO_TYPE_HMAC_MD5_RC4;
193
194 if (wpa_sm_get_bssid(sm, bssid) < 0) {
195 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
196 "Failed to read BSSID for EAPOL-Key request");
197 return;
198 }
199
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700200 mic_len = wpa_mic_len(sm->key_mgmt, sm->pmk_len);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800201 hdrlen = sizeof(*reply) + mic_len + 2;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700202 rbuf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_KEY, NULL,
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800203 hdrlen, &rlen, (void *) &reply);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700204 if (rbuf == NULL)
205 return;
206
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800207 reply->type = (sm->proto == WPA_PROTO_RSN ||
208 sm->proto == WPA_PROTO_OSEN) ?
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700209 EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA;
210 key_info = WPA_KEY_INFO_REQUEST | ver;
211 if (sm->ptk_set)
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800212 key_info |= WPA_KEY_INFO_SECURE;
213 if (sm->ptk_set && mic_len)
214 key_info |= WPA_KEY_INFO_MIC;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700215 if (error)
216 key_info |= WPA_KEY_INFO_ERROR;
217 if (pairwise)
218 key_info |= WPA_KEY_INFO_KEY_TYPE;
219 WPA_PUT_BE16(reply->key_info, key_info);
220 WPA_PUT_BE16(reply->key_length, 0);
221 os_memcpy(reply->replay_counter, sm->request_counter,
222 WPA_REPLAY_COUNTER_LEN);
223 inc_byte_array(sm->request_counter, WPA_REPLAY_COUNTER_LEN);
224
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800225 mic = (u8 *) (reply + 1);
226 WPA_PUT_BE16(mic + mic_len, 0);
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800227 if (!(key_info & WPA_KEY_INFO_MIC))
228 key_mic = NULL;
229 else
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800230 key_mic = mic;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700231
232 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
233 "WPA: Sending EAPOL-Key Request (error=%d "
234 "pairwise=%d ptk_set=%d len=%lu)",
235 error, pairwise, sm->ptk_set, (unsigned long) rlen);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800236 wpa_eapol_key_send(sm, &sm->ptk, ver, bssid, ETH_P_EAPOL, rbuf, rlen,
237 key_mic);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700238}
239
240
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800241static void wpa_supplicant_key_mgmt_set_pmk(struct wpa_sm *sm)
242{
243#ifdef CONFIG_IEEE80211R
244 if (sm->key_mgmt == WPA_KEY_MGMT_FT_IEEE8021X) {
245 if (wpa_sm_key_mgmt_set_pmk(sm, sm->xxkey, sm->xxkey_len))
246 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
247 "RSN: Cannot set low order 256 bits of MSK for key management offload");
248 } else {
249#endif /* CONFIG_IEEE80211R */
250 if (wpa_sm_key_mgmt_set_pmk(sm, sm->pmk, sm->pmk_len))
251 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
252 "RSN: Cannot set PMK for key management offload");
253#ifdef CONFIG_IEEE80211R
254 }
255#endif /* CONFIG_IEEE80211R */
256}
257
258
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700259static int wpa_supplicant_get_pmk(struct wpa_sm *sm,
260 const unsigned char *src_addr,
261 const u8 *pmkid)
262{
263 int abort_cached = 0;
264
265 if (pmkid && !sm->cur_pmksa) {
266 /* When using drivers that generate RSN IE, wpa_supplicant may
267 * not have enough time to get the association information
268 * event before receiving this 1/4 message, so try to find a
269 * matching PMKSA cache entry here. */
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800270 sm->cur_pmksa = pmksa_cache_get(sm->pmksa, src_addr, pmkid,
Roshan Pius3a1667e2018-07-03 15:17:14 -0700271 NULL, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700272 if (sm->cur_pmksa) {
273 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
274 "RSN: found matching PMKID from PMKSA cache");
275 } else {
276 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
277 "RSN: no matching PMKID found");
278 abort_cached = 1;
279 }
280 }
281
282 if (pmkid && sm->cur_pmksa &&
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700283 os_memcmp_const(pmkid, sm->cur_pmksa->pmkid, PMKID_LEN) == 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700284 wpa_hexdump(MSG_DEBUG, "RSN: matched PMKID", pmkid, PMKID_LEN);
285 wpa_sm_set_pmk_from_pmksa(sm);
286 wpa_hexdump_key(MSG_DEBUG, "RSN: PMK from PMKSA cache",
287 sm->pmk, sm->pmk_len);
288 eapol_sm_notify_cached(sm->eapol);
289#ifdef CONFIG_IEEE80211R
290 sm->xxkey_len = 0;
Roshan Pius3a1667e2018-07-03 15:17:14 -0700291#ifdef CONFIG_SAE
292 if (sm->key_mgmt == WPA_KEY_MGMT_FT_SAE &&
293 sm->pmk_len == PMK_LEN) {
294 /* Need to allow FT key derivation to proceed with
295 * PMK from SAE being used as the XXKey in cases where
296 * the PMKID in msg 1/4 matches the PMKSA entry that was
297 * just added based on SAE authentication for the
298 * initial mobility domain association. */
299 os_memcpy(sm->xxkey, sm->pmk, sm->pmk_len);
300 sm->xxkey_len = sm->pmk_len;
301 }
302#endif /* CONFIG_SAE */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700303#endif /* CONFIG_IEEE80211R */
304 } else if (wpa_key_mgmt_wpa_ieee8021x(sm->key_mgmt) && sm->eapol) {
305 int res, pmk_len;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800306
Dmitry Shmidtebd93af2017-02-21 13:40:44 -0800307 if (wpa_key_mgmt_sha384(sm->key_mgmt))
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800308 pmk_len = PMK_LEN_SUITE_B_192;
309 else
310 pmk_len = PMK_LEN;
311 res = eapol_sm_get_key(sm->eapol, sm->pmk, pmk_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700312 if (res) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800313 if (pmk_len == PMK_LEN) {
314 /*
315 * EAP-LEAP is an exception from other EAP
316 * methods: it uses only 16-byte PMK.
317 */
318 res = eapol_sm_get_key(sm->eapol, sm->pmk, 16);
319 pmk_len = 16;
320 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700321 } else {
322#ifdef CONFIG_IEEE80211R
323 u8 buf[2 * PMK_LEN];
324 if (eapol_sm_get_key(sm->eapol, buf, 2 * PMK_LEN) == 0)
325 {
Roshan Pius3a1667e2018-07-03 15:17:14 -0700326 if (wpa_key_mgmt_sha384(sm->key_mgmt)) {
327 os_memcpy(sm->xxkey, buf,
328 SHA384_MAC_LEN);
329 sm->xxkey_len = SHA384_MAC_LEN;
330 } else {
331 os_memcpy(sm->xxkey, buf + PMK_LEN,
332 PMK_LEN);
333 sm->xxkey_len = PMK_LEN;
334 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700335 os_memset(buf, 0, sizeof(buf));
336 }
337#endif /* CONFIG_IEEE80211R */
338 }
339 if (res == 0) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700340 struct rsn_pmksa_cache_entry *sa = NULL;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700341 const u8 *fils_cache_id = NULL;
342
343#ifdef CONFIG_FILS
344 if (sm->fils_cache_id_set)
345 fils_cache_id = sm->fils_cache_id;
346#endif /* CONFIG_FILS */
347
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700348 wpa_hexdump_key(MSG_DEBUG, "WPA: PMK from EAPOL state "
349 "machines", sm->pmk, pmk_len);
350 sm->pmk_len = pmk_len;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800351 wpa_supplicant_key_mgmt_set_pmk(sm);
Dmitry Shmidtc55524a2011-07-07 11:18:38 -0700352 if (sm->proto == WPA_PROTO_RSN &&
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800353 !wpa_key_mgmt_suite_b(sm->key_mgmt) &&
Dmitry Shmidtc55524a2011-07-07 11:18:38 -0700354 !wpa_key_mgmt_ft(sm->key_mgmt)) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700355 sa = pmksa_cache_add(sm->pmksa,
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800356 sm->pmk, pmk_len, NULL,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800357 NULL, 0,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700358 src_addr, sm->own_addr,
359 sm->network_ctx,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700360 sm->key_mgmt,
361 fils_cache_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700362 }
363 if (!sm->cur_pmksa && pmkid &&
Roshan Pius3a1667e2018-07-03 15:17:14 -0700364 pmksa_cache_get(sm->pmksa, src_addr, pmkid, NULL,
365 0)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700366 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
367 "RSN: the new PMK matches with the "
368 "PMKID");
369 abort_cached = 0;
Jouni Malinen6ec30382015-07-08 20:48:18 +0300370 } else if (sa && !sm->cur_pmksa && pmkid) {
371 /*
372 * It looks like the authentication server
373 * derived mismatching MSK. This should not
374 * really happen, but bugs happen.. There is not
375 * much we can do here without knowing what
376 * exactly caused the server to misbehave.
377 */
Dmitry Shmidt014a3ff2015-12-28 13:27:49 -0800378 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
Jouni Malinen6ec30382015-07-08 20:48:18 +0300379 "RSN: PMKID mismatch - authentication server may have derived different MSK?!");
380 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700381 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700382
383 if (!sm->cur_pmksa)
384 sm->cur_pmksa = sa;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700385 } else {
386 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
387 "WPA: Failed to get master session key from "
388 "EAPOL state machines - key handshake "
389 "aborted");
390 if (sm->cur_pmksa) {
391 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
392 "RSN: Cancelled PMKSA caching "
393 "attempt");
394 sm->cur_pmksa = NULL;
395 abort_cached = 1;
396 } else if (!abort_cached) {
397 return -1;
398 }
399 }
400 }
401
Dmitry Shmidtc55524a2011-07-07 11:18:38 -0700402 if (abort_cached && wpa_key_mgmt_wpa_ieee8021x(sm->key_mgmt) &&
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800403 !wpa_key_mgmt_suite_b(sm->key_mgmt) &&
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800404 !wpa_key_mgmt_ft(sm->key_mgmt) && sm->key_mgmt != WPA_KEY_MGMT_OSEN)
405 {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700406 /* Send EAPOL-Start to trigger full EAP authentication. */
407 u8 *buf;
408 size_t buflen;
409
410 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
411 "RSN: no PMKSA entry found - trigger "
412 "full EAP authentication");
413 buf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_START,
414 NULL, 0, &buflen, NULL);
415 if (buf) {
416 wpa_sm_ether_send(sm, sm->bssid, ETH_P_EAPOL,
417 buf, buflen);
418 os_free(buf);
419 return -2;
420 }
421
422 return -1;
423 }
424
425 return 0;
426}
427
428
429/**
430 * wpa_supplicant_send_2_of_4 - Send message 2 of WPA/RSN 4-Way Handshake
431 * @sm: Pointer to WPA state machine data from wpa_sm_init()
432 * @dst: Destination address for the frame
433 * @key: Pointer to the EAPOL-Key frame header
434 * @ver: Version bits from EAPOL-Key Key Info
435 * @nonce: Nonce value for the EAPOL-Key frame
436 * @wpa_ie: WPA/RSN IE
437 * @wpa_ie_len: Length of the WPA/RSN IE
438 * @ptk: PTK to use for keyed hash and encryption
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800439 * Returns: >= 0 on success, < 0 on failure
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700440 */
441int wpa_supplicant_send_2_of_4(struct wpa_sm *sm, const unsigned char *dst,
442 const struct wpa_eapol_key *key,
443 int ver, const u8 *nonce,
444 const u8 *wpa_ie, size_t wpa_ie_len,
445 struct wpa_ptk *ptk)
446{
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800447 size_t mic_len, hdrlen, rlen;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700448 struct wpa_eapol_key *reply;
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800449 u8 *rbuf, *key_mic;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700450 u8 *rsn_ie_buf = NULL;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800451 u16 key_info;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700452
453 if (wpa_ie == NULL) {
454 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, "WPA: No wpa_ie set - "
455 "cannot generate msg 2/4");
456 return -1;
457 }
458
459#ifdef CONFIG_IEEE80211R
460 if (wpa_key_mgmt_ft(sm->key_mgmt)) {
461 int res;
462
463 /*
464 * Add PMKR1Name into RSN IE (PMKID-List) and add MDIE and
465 * FTIE from (Re)Association Response.
466 */
467 rsn_ie_buf = os_malloc(wpa_ie_len + 2 + 2 + PMKID_LEN +
468 sm->assoc_resp_ies_len);
469 if (rsn_ie_buf == NULL)
470 return -1;
471 os_memcpy(rsn_ie_buf, wpa_ie, wpa_ie_len);
Dmitry Shmidt55840ad2015-12-14 12:45:46 -0800472 res = wpa_insert_pmkid(rsn_ie_buf, &wpa_ie_len,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700473 sm->pmk_r1_name);
474 if (res < 0) {
475 os_free(rsn_ie_buf);
476 return -1;
477 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700478
479 if (sm->assoc_resp_ies) {
480 os_memcpy(rsn_ie_buf + wpa_ie_len, sm->assoc_resp_ies,
481 sm->assoc_resp_ies_len);
482 wpa_ie_len += sm->assoc_resp_ies_len;
483 }
484
485 wpa_ie = rsn_ie_buf;
486 }
487#endif /* CONFIG_IEEE80211R */
488
489 wpa_hexdump(MSG_DEBUG, "WPA: WPA IE for msg 2/4", wpa_ie, wpa_ie_len);
490
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700491 mic_len = wpa_mic_len(sm->key_mgmt, sm->pmk_len);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800492 hdrlen = sizeof(*reply) + mic_len + 2;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700493 rbuf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_KEY,
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800494 NULL, hdrlen + wpa_ie_len,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700495 &rlen, (void *) &reply);
496 if (rbuf == NULL) {
497 os_free(rsn_ie_buf);
498 return -1;
499 }
500
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800501 reply->type = (sm->proto == WPA_PROTO_RSN ||
502 sm->proto == WPA_PROTO_OSEN) ?
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700503 EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800504 key_info = ver | WPA_KEY_INFO_KEY_TYPE;
505 if (mic_len)
506 key_info |= WPA_KEY_INFO_MIC;
507 else
508 key_info |= WPA_KEY_INFO_ENCR_KEY_DATA;
509 WPA_PUT_BE16(reply->key_info, key_info);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800510 if (sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700511 WPA_PUT_BE16(reply->key_length, 0);
512 else
513 os_memcpy(reply->key_length, key->key_length, 2);
514 os_memcpy(reply->replay_counter, key->replay_counter,
515 WPA_REPLAY_COUNTER_LEN);
516 wpa_hexdump(MSG_DEBUG, "WPA: Replay Counter", reply->replay_counter,
517 WPA_REPLAY_COUNTER_LEN);
518
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800519 key_mic = (u8 *) (reply + 1);
520 WPA_PUT_BE16(key_mic + mic_len, wpa_ie_len); /* Key Data Length */
521 os_memcpy(key_mic + mic_len + 2, wpa_ie, wpa_ie_len); /* Key Data */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700522 os_free(rsn_ie_buf);
523
524 os_memcpy(reply->key_nonce, nonce, WPA_NONCE_LEN);
525
Roshan Pius5e7db942018-04-12 12:27:41 -0700526 wpa_dbg(sm->ctx->msg_ctx, MSG_INFO, "WPA: Sending EAPOL-Key 2/4");
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800527 return wpa_eapol_key_send(sm, ptk, ver, dst, ETH_P_EAPOL, rbuf, rlen,
528 key_mic);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700529}
530
531
532static int wpa_derive_ptk(struct wpa_sm *sm, const unsigned char *src_addr,
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800533 const struct wpa_eapol_key *key, struct wpa_ptk *ptk)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700534{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700535#ifdef CONFIG_IEEE80211R
536 if (wpa_key_mgmt_ft(sm->key_mgmt))
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800537 return wpa_derive_ptk_ft(sm, src_addr, key, ptk);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700538#endif /* CONFIG_IEEE80211R */
539
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800540 return wpa_pmk_to_ptk(sm->pmk, sm->pmk_len, "Pairwise key expansion",
541 sm->own_addr, sm->bssid, sm->snonce,
542 key->key_nonce, ptk, sm->key_mgmt,
543 sm->pairwise_cipher);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700544}
545
546
547static void wpa_supplicant_process_1_of_4(struct wpa_sm *sm,
548 const unsigned char *src_addr,
549 const struct wpa_eapol_key *key,
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700550 u16 ver, const u8 *key_data,
551 size_t key_data_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700552{
553 struct wpa_eapol_ie_parse ie;
554 struct wpa_ptk *ptk;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700555 int res;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800556 u8 *kde, *kde_buf = NULL;
557 size_t kde_len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700558
559 if (wpa_sm_get_network_ctx(sm) == NULL) {
560 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, "WPA: No SSID info "
561 "found (msg 1 of 4)");
562 return;
563 }
564
565 wpa_sm_set_state(sm, WPA_4WAY_HANDSHAKE);
Roshan Pius5e7db942018-04-12 12:27:41 -0700566 wpa_dbg(sm->ctx->msg_ctx, MSG_INFO, "WPA: RX message 1 of 4-Way "
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700567 "Handshake from " MACSTR " (ver=%d)", MAC2STR(src_addr), ver);
568
569 os_memset(&ie, 0, sizeof(ie));
570
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800571 if (sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700572 /* RSN: msg 1/4 should contain PMKID for the selected PMK */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700573 wpa_hexdump(MSG_DEBUG, "RSN: msg 1/4 key data",
574 key_data, key_data_len);
575 if (wpa_supplicant_parse_ies(key_data, key_data_len, &ie) < 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800576 goto failed;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700577 if (ie.pmkid) {
578 wpa_hexdump(MSG_DEBUG, "RSN: PMKID from "
579 "Authenticator", ie.pmkid, PMKID_LEN);
580 }
581 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700582
583 res = wpa_supplicant_get_pmk(sm, src_addr, ie.pmkid);
584 if (res == -2) {
585 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "RSN: Do not reply to "
586 "msg 1/4 - requesting full EAP authentication");
587 return;
588 }
589 if (res)
590 goto failed;
591
592 if (sm->renew_snonce) {
593 if (random_get_bytes(sm->snonce, WPA_NONCE_LEN)) {
594 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
595 "WPA: Failed to get random data for SNonce");
596 goto failed;
597 }
598 sm->renew_snonce = 0;
599 wpa_hexdump(MSG_DEBUG, "WPA: Renewed SNonce",
600 sm->snonce, WPA_NONCE_LEN);
601 }
602
603 /* Calculate PTK which will be stored as a temporary PTK until it has
604 * been verified when processing message 3/4. */
605 ptk = &sm->tptk;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700606 if (wpa_derive_ptk(sm, src_addr, key, ptk) < 0)
607 goto failed;
Dmitry Shmidt98660862014-03-11 17:26:21 -0700608 if (sm->pairwise_cipher == WPA_CIPHER_TKIP) {
Dmitry Shmidt61593f02014-04-21 16:27:35 -0700609 u8 buf[8];
Dmitry Shmidt98660862014-03-11 17:26:21 -0700610 /* Supplicant: swap tx/rx Mic keys */
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800611 os_memcpy(buf, &ptk->tk[16], 8);
612 os_memcpy(&ptk->tk[16], &ptk->tk[24], 8);
613 os_memcpy(&ptk->tk[24], buf, 8);
Dmitry Shmidt61593f02014-04-21 16:27:35 -0700614 os_memset(buf, 0, sizeof(buf));
Dmitry Shmidt98660862014-03-11 17:26:21 -0700615 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700616 sm->tptk_set = 1;
617
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800618 kde = sm->assoc_wpa_ie;
619 kde_len = sm->assoc_wpa_ie_len;
620
621#ifdef CONFIG_P2P
622 if (sm->p2p) {
623 kde_buf = os_malloc(kde_len + 2 + RSN_SELECTOR_LEN + 1);
624 if (kde_buf) {
625 u8 *pos;
626 wpa_printf(MSG_DEBUG, "P2P: Add IP Address Request KDE "
627 "into EAPOL-Key 2/4");
628 os_memcpy(kde_buf, kde, kde_len);
629 kde = kde_buf;
630 pos = kde + kde_len;
631 *pos++ = WLAN_EID_VENDOR_SPECIFIC;
632 *pos++ = RSN_SELECTOR_LEN + 1;
633 RSN_SELECTOR_PUT(pos, WFA_KEY_DATA_IP_ADDR_REQ);
634 pos += RSN_SELECTOR_LEN;
635 *pos++ = 0x01;
636 kde_len = pos - kde;
637 }
638 }
639#endif /* CONFIG_P2P */
640
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700641 if (wpa_supplicant_send_2_of_4(sm, sm->bssid, key, ver, sm->snonce,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800642 kde, kde_len, ptk) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700643 goto failed;
644
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800645 os_free(kde_buf);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700646 os_memcpy(sm->anonce, key->key_nonce, WPA_NONCE_LEN);
647 return;
648
649failed:
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800650 os_free(kde_buf);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700651 wpa_sm_deauthenticate(sm, WLAN_REASON_UNSPECIFIED);
652}
653
654
655static void wpa_sm_start_preauth(void *eloop_ctx, void *timeout_ctx)
656{
657 struct wpa_sm *sm = eloop_ctx;
658 rsn_preauth_candidate_process(sm);
659}
660
661
662static void wpa_supplicant_key_neg_complete(struct wpa_sm *sm,
663 const u8 *addr, int secure)
664{
665 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
666 "WPA: Key negotiation completed with "
667 MACSTR " [PTK=%s GTK=%s]", MAC2STR(addr),
668 wpa_cipher_txt(sm->pairwise_cipher),
669 wpa_cipher_txt(sm->group_cipher));
670 wpa_sm_cancel_auth_timeout(sm);
671 wpa_sm_set_state(sm, WPA_COMPLETED);
672
673 if (secure) {
674 wpa_sm_mlme_setprotection(
675 sm, addr, MLME_SETPROTECTION_PROTECT_TYPE_RX_TX,
676 MLME_SETPROTECTION_KEY_TYPE_PAIRWISE);
677 eapol_sm_notify_portValid(sm->eapol, TRUE);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700678 if (wpa_key_mgmt_wpa_psk(sm->key_mgmt) ||
679 sm->key_mgmt == WPA_KEY_MGMT_DPP ||
680 sm->key_mgmt == WPA_KEY_MGMT_OWE)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700681 eapol_sm_notify_eap_success(sm->eapol, TRUE);
682 /*
683 * Start preauthentication after a short wait to avoid a
684 * possible race condition between the data receive and key
685 * configuration after the 4-Way Handshake. This increases the
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800686 * likelihood of the first preauth EAPOL-Start frame getting to
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700687 * the target AP.
688 */
689 eloop_register_timeout(1, 0, wpa_sm_start_preauth, sm, NULL);
690 }
691
692 if (sm->cur_pmksa && sm->cur_pmksa->opportunistic) {
693 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
694 "RSN: Authenticator accepted "
695 "opportunistic PMKSA entry - marking it valid");
696 sm->cur_pmksa->opportunistic = 0;
697 }
698
699#ifdef CONFIG_IEEE80211R
700 if (wpa_key_mgmt_ft(sm->key_mgmt)) {
701 /* Prepare for the next transition */
702 wpa_ft_prepare_auth_request(sm, NULL);
703 }
704#endif /* CONFIG_IEEE80211R */
705}
706
707
708static void wpa_sm_rekey_ptk(void *eloop_ctx, void *timeout_ctx)
709{
710 struct wpa_sm *sm = eloop_ctx;
711 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Request PTK rekeying");
712 wpa_sm_key_request(sm, 0, 1);
713}
714
715
716static int wpa_supplicant_install_ptk(struct wpa_sm *sm,
717 const struct wpa_eapol_key *key)
718{
719 int keylen, rsclen;
720 enum wpa_alg alg;
721 const u8 *key_rsc;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800722
Mathy Vanhoefc66556c2017-09-29 04:22:51 +0200723 if (sm->ptk.installed) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800724 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
725 "WPA: Do not re-install same PTK to the driver");
726 return 0;
727 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700728
729 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
730 "WPA: Installing PTK to the driver");
731
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700732 if (sm->pairwise_cipher == WPA_CIPHER_NONE) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700733 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Pairwise Cipher "
734 "Suite: NONE - do not use pairwise keys");
735 return 0;
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700736 }
737
738 if (!wpa_cipher_valid_pairwise(sm->pairwise_cipher)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700739 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
740 "WPA: Unsupported pairwise cipher %d",
741 sm->pairwise_cipher);
742 return -1;
743 }
744
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700745 alg = wpa_cipher_to_alg(sm->pairwise_cipher);
746 keylen = wpa_cipher_key_len(sm->pairwise_cipher);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700747 if (keylen <= 0 || (unsigned int) keylen != sm->ptk.tk_len) {
748 wpa_printf(MSG_DEBUG, "WPA: TK length mismatch: %d != %lu",
749 keylen, (long unsigned int) sm->ptk.tk_len);
750 return -1;
751 }
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700752 rsclen = wpa_cipher_rsc_len(sm->pairwise_cipher);
753
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800754 if (sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700755 key_rsc = null_rsc;
756 } else {
757 key_rsc = key->key_rsc;
758 wpa_hexdump(MSG_DEBUG, "WPA: RSC", key_rsc, rsclen);
759 }
760
761 if (wpa_sm_set_key(sm, alg, sm->bssid, 0, 1, key_rsc, rsclen,
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800762 sm->ptk.tk, keylen) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700763 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
764 "WPA: Failed to set PTK to the "
765 "driver (alg=%d keylen=%d bssid=" MACSTR ")",
766 alg, keylen, MAC2STR(sm->bssid));
767 return -1;
768 }
769
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800770 /* TK is not needed anymore in supplicant */
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800771 os_memset(sm->ptk.tk, 0, WPA_TK_MAX_LEN);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700772 sm->ptk.tk_len = 0;
Mathy Vanhoefc66556c2017-09-29 04:22:51 +0200773 sm->ptk.installed = 1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800774
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700775 if (sm->wpa_ptk_rekey) {
776 eloop_cancel_timeout(wpa_sm_rekey_ptk, sm, NULL);
777 eloop_register_timeout(sm->wpa_ptk_rekey, 0, wpa_sm_rekey_ptk,
778 sm, NULL);
779 }
780
781 return 0;
782}
783
784
785static int wpa_supplicant_check_group_cipher(struct wpa_sm *sm,
786 int group_cipher,
787 int keylen, int maxkeylen,
788 int *key_rsc_len,
789 enum wpa_alg *alg)
790{
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700791 int klen;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700792
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700793 *alg = wpa_cipher_to_alg(group_cipher);
794 if (*alg == WPA_ALG_NONE) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700795 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
796 "WPA: Unsupported Group Cipher %d",
797 group_cipher);
798 return -1;
799 }
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700800 *key_rsc_len = wpa_cipher_rsc_len(group_cipher);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700801
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700802 klen = wpa_cipher_key_len(group_cipher);
803 if (keylen != klen || maxkeylen < klen) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700804 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
805 "WPA: Unsupported %s Group Cipher key length %d (%d)",
806 wpa_cipher_txt(group_cipher), keylen, maxkeylen);
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700807 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700808 }
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700809 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700810}
811
812
813struct wpa_gtk_data {
814 enum wpa_alg alg;
815 int tx, key_rsc_len, keyidx;
816 u8 gtk[32];
817 int gtk_len;
818};
819
820
821static int wpa_supplicant_install_gtk(struct wpa_sm *sm,
822 const struct wpa_gtk_data *gd,
Jouni Malinen58c0e962017-10-01 12:12:24 +0300823 const u8 *key_rsc, int wnm_sleep)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700824{
825 const u8 *_gtk = gd->gtk;
826 u8 gtk_buf[32];
827
Mathy Vanhoef10bfd642017-07-12 16:03:24 +0200828 /* Detect possible key reinstallation */
Jouni Malinen58c0e962017-10-01 12:12:24 +0300829 if ((sm->gtk.gtk_len == (size_t) gd->gtk_len &&
830 os_memcmp(sm->gtk.gtk, gd->gtk, sm->gtk.gtk_len) == 0) ||
831 (sm->gtk_wnm_sleep.gtk_len == (size_t) gd->gtk_len &&
832 os_memcmp(sm->gtk_wnm_sleep.gtk, gd->gtk,
833 sm->gtk_wnm_sleep.gtk_len) == 0)) {
Mathy Vanhoef10bfd642017-07-12 16:03:24 +0200834 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
835 "WPA: Not reinstalling already in-use GTK to the driver (keyidx=%d tx=%d len=%d)",
836 gd->keyidx, gd->tx, gd->gtk_len);
837 return 0;
838 }
839
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700840 wpa_hexdump_key(MSG_DEBUG, "WPA: Group Key", gd->gtk, gd->gtk_len);
841 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
842 "WPA: Installing GTK to the driver (keyidx=%d tx=%d len=%d)",
843 gd->keyidx, gd->tx, gd->gtk_len);
844 wpa_hexdump(MSG_DEBUG, "WPA: RSC", key_rsc, gd->key_rsc_len);
845 if (sm->group_cipher == WPA_CIPHER_TKIP) {
846 /* Swap Tx/Rx keys for Michael MIC */
847 os_memcpy(gtk_buf, gd->gtk, 16);
848 os_memcpy(gtk_buf + 16, gd->gtk + 24, 8);
849 os_memcpy(gtk_buf + 24, gd->gtk + 16, 8);
850 _gtk = gtk_buf;
851 }
852 if (sm->pairwise_cipher == WPA_CIPHER_NONE) {
853 if (wpa_sm_set_key(sm, gd->alg, NULL,
854 gd->keyidx, 1, key_rsc, gd->key_rsc_len,
855 _gtk, gd->gtk_len) < 0) {
856 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
857 "WPA: Failed to set GTK to the driver "
858 "(Group only)");
Dmitry Shmidt61593f02014-04-21 16:27:35 -0700859 os_memset(gtk_buf, 0, sizeof(gtk_buf));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700860 return -1;
861 }
862 } else if (wpa_sm_set_key(sm, gd->alg, broadcast_ether_addr,
863 gd->keyidx, gd->tx, key_rsc, gd->key_rsc_len,
864 _gtk, gd->gtk_len) < 0) {
865 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
866 "WPA: Failed to set GTK to "
867 "the driver (alg=%d keylen=%d keyidx=%d)",
868 gd->alg, gd->gtk_len, gd->keyidx);
Dmitry Shmidt61593f02014-04-21 16:27:35 -0700869 os_memset(gtk_buf, 0, sizeof(gtk_buf));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700870 return -1;
871 }
Dmitry Shmidt61593f02014-04-21 16:27:35 -0700872 os_memset(gtk_buf, 0, sizeof(gtk_buf));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700873
Jouni Malinen58c0e962017-10-01 12:12:24 +0300874 if (wnm_sleep) {
875 sm->gtk_wnm_sleep.gtk_len = gd->gtk_len;
876 os_memcpy(sm->gtk_wnm_sleep.gtk, gd->gtk,
877 sm->gtk_wnm_sleep.gtk_len);
878 } else {
879 sm->gtk.gtk_len = gd->gtk_len;
880 os_memcpy(sm->gtk.gtk, gd->gtk, sm->gtk.gtk_len);
881 }
Mathy Vanhoef10bfd642017-07-12 16:03:24 +0200882
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700883 return 0;
884}
885
886
887static int wpa_supplicant_gtk_tx_bit_workaround(const struct wpa_sm *sm,
888 int tx)
889{
890 if (tx && sm->pairwise_cipher != WPA_CIPHER_NONE) {
891 /* Ignore Tx bit for GTK if a pairwise key is used. One AP
892 * seemed to set this bit (incorrectly, since Tx is only when
893 * doing Group Key only APs) and without this workaround, the
894 * data connection does not work because wpa_supplicant
895 * configured non-zero keyidx to be used for unicast. */
896 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
897 "WPA: Tx bit set for GTK, but pairwise "
898 "keys are used - ignore Tx bit");
899 return 0;
900 }
901 return tx;
902}
903
904
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800905static int wpa_supplicant_rsc_relaxation(const struct wpa_sm *sm,
906 const u8 *rsc)
907{
908 int rsclen;
909
910 if (!sm->wpa_rsc_relaxation)
911 return 0;
912
913 rsclen = wpa_cipher_rsc_len(sm->group_cipher);
914
915 /*
916 * Try to detect RSC (endian) corruption issue where the AP sends
917 * the RSC bytes in EAPOL-Key message in the wrong order, both if
918 * it's actually a 6-byte field (as it should be) and if it treats
919 * it as an 8-byte field.
920 * An AP model known to have this bug is the Sapido RB-1632.
921 */
922 if (rsclen == 6 && ((rsc[5] && !rsc[0]) || rsc[6] || rsc[7])) {
923 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
924 "RSC %02x%02x%02x%02x%02x%02x%02x%02x is likely bogus, using 0",
925 rsc[0], rsc[1], rsc[2], rsc[3],
926 rsc[4], rsc[5], rsc[6], rsc[7]);
927
928 return 1;
929 }
930
931 return 0;
932}
933
934
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700935static int wpa_supplicant_pairwise_gtk(struct wpa_sm *sm,
936 const struct wpa_eapol_key *key,
937 const u8 *gtk, size_t gtk_len,
938 int key_info)
939{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700940 struct wpa_gtk_data gd;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800941 const u8 *key_rsc;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700942
943 /*
944 * IEEE Std 802.11i-2004 - 8.5.2 EAPOL-Key frames - Figure 43x
945 * GTK KDE format:
946 * KeyID[bits 0-1], Tx [bit 2], Reserved [bits 3-7]
947 * Reserved [bits 0-7]
948 * GTK
949 */
950
951 os_memset(&gd, 0, sizeof(gd));
952 wpa_hexdump_key(MSG_DEBUG, "RSN: received GTK in pairwise handshake",
953 gtk, gtk_len);
954
955 if (gtk_len < 2 || gtk_len - 2 > sizeof(gd.gtk))
956 return -1;
957
958 gd.keyidx = gtk[0] & 0x3;
959 gd.tx = wpa_supplicant_gtk_tx_bit_workaround(sm,
960 !!(gtk[0] & BIT(2)));
961 gtk += 2;
962 gtk_len -= 2;
963
964 os_memcpy(gd.gtk, gtk, gtk_len);
965 gd.gtk_len = gtk_len;
966
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800967 key_rsc = key->key_rsc;
968 if (wpa_supplicant_rsc_relaxation(sm, key->key_rsc))
969 key_rsc = null_rsc;
970
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800971 if (sm->group_cipher != WPA_CIPHER_GTK_NOT_USED &&
972 (wpa_supplicant_check_group_cipher(sm, sm->group_cipher,
973 gtk_len, gtk_len,
974 &gd.key_rsc_len, &gd.alg) ||
Jouni Malinen58c0e962017-10-01 12:12:24 +0300975 wpa_supplicant_install_gtk(sm, &gd, key_rsc, 0))) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700976 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
977 "RSN: Failed to install GTK");
Dmitry Shmidt61593f02014-04-21 16:27:35 -0700978 os_memset(&gd, 0, sizeof(gd));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700979 return -1;
980 }
Dmitry Shmidt61593f02014-04-21 16:27:35 -0700981 os_memset(&gd, 0, sizeof(gd));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700982
983 wpa_supplicant_key_neg_complete(sm, sm->bssid,
984 key_info & WPA_KEY_INFO_SECURE);
985 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700986}
987
988
Mathy Vanhoef10bfd642017-07-12 16:03:24 +0200989#ifdef CONFIG_IEEE80211W
990static int wpa_supplicant_install_igtk(struct wpa_sm *sm,
Jouni Malinen58c0e962017-10-01 12:12:24 +0300991 const struct wpa_igtk_kde *igtk,
992 int wnm_sleep)
Mathy Vanhoef10bfd642017-07-12 16:03:24 +0200993{
994 size_t len = wpa_cipher_key_len(sm->mgmt_group_cipher);
995 u16 keyidx = WPA_GET_LE16(igtk->keyid);
996
997 /* Detect possible key reinstallation */
Jouni Malinen58c0e962017-10-01 12:12:24 +0300998 if ((sm->igtk.igtk_len == len &&
999 os_memcmp(sm->igtk.igtk, igtk->igtk, sm->igtk.igtk_len) == 0) ||
1000 (sm->igtk_wnm_sleep.igtk_len == len &&
1001 os_memcmp(sm->igtk_wnm_sleep.igtk, igtk->igtk,
1002 sm->igtk_wnm_sleep.igtk_len) == 0)) {
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02001003 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1004 "WPA: Not reinstalling already in-use IGTK to the driver (keyidx=%d)",
1005 keyidx);
1006 return 0;
1007 }
1008
1009 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1010 "WPA: IGTK keyid %d pn %02x%02x%02x%02x%02x%02x",
1011 keyidx, MAC2STR(igtk->pn));
1012 wpa_hexdump_key(MSG_DEBUG, "WPA: IGTK", igtk->igtk, len);
1013 if (keyidx > 4095) {
1014 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1015 "WPA: Invalid IGTK KeyID %d", keyidx);
1016 return -1;
1017 }
1018 if (wpa_sm_set_key(sm, wpa_cipher_to_alg(sm->mgmt_group_cipher),
1019 broadcast_ether_addr,
1020 keyidx, 0, igtk->pn, sizeof(igtk->pn),
1021 igtk->igtk, len) < 0) {
1022 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1023 "WPA: Failed to configure IGTK to the driver");
1024 return -1;
1025 }
1026
Jouni Malinen58c0e962017-10-01 12:12:24 +03001027 if (wnm_sleep) {
1028 sm->igtk_wnm_sleep.igtk_len = len;
1029 os_memcpy(sm->igtk_wnm_sleep.igtk, igtk->igtk,
1030 sm->igtk_wnm_sleep.igtk_len);
1031 } else {
1032 sm->igtk.igtk_len = len;
1033 os_memcpy(sm->igtk.igtk, igtk->igtk, sm->igtk.igtk_len);
1034 }
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02001035
1036 return 0;
1037}
1038#endif /* CONFIG_IEEE80211W */
1039
1040
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001041static int ieee80211w_set_keys(struct wpa_sm *sm,
1042 struct wpa_eapol_ie_parse *ie)
1043{
1044#ifdef CONFIG_IEEE80211W
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001045 if (!wpa_cipher_valid_mgmt_group(sm->mgmt_group_cipher))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001046 return 0;
1047
1048 if (ie->igtk) {
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001049 size_t len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001050 const struct wpa_igtk_kde *igtk;
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02001051
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001052 len = wpa_cipher_key_len(sm->mgmt_group_cipher);
1053 if (ie->igtk_len != WPA_IGTK_KDE_PREFIX_LEN + len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001054 return -1;
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02001055
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001056 igtk = (const struct wpa_igtk_kde *) ie->igtk;
Jouni Malinen58c0e962017-10-01 12:12:24 +03001057 if (wpa_supplicant_install_igtk(sm, igtk, 0) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001058 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001059 }
1060
1061 return 0;
1062#else /* CONFIG_IEEE80211W */
1063 return 0;
1064#endif /* CONFIG_IEEE80211W */
1065}
1066
1067
1068static void wpa_report_ie_mismatch(struct wpa_sm *sm,
1069 const char *reason, const u8 *src_addr,
1070 const u8 *wpa_ie, size_t wpa_ie_len,
1071 const u8 *rsn_ie, size_t rsn_ie_len)
1072{
1073 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, "WPA: %s (src=" MACSTR ")",
1074 reason, MAC2STR(src_addr));
1075
1076 if (sm->ap_wpa_ie) {
1077 wpa_hexdump(MSG_INFO, "WPA: WPA IE in Beacon/ProbeResp",
1078 sm->ap_wpa_ie, sm->ap_wpa_ie_len);
1079 }
1080 if (wpa_ie) {
1081 if (!sm->ap_wpa_ie) {
1082 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1083 "WPA: No WPA IE in Beacon/ProbeResp");
1084 }
1085 wpa_hexdump(MSG_INFO, "WPA: WPA IE in 3/4 msg",
1086 wpa_ie, wpa_ie_len);
1087 }
1088
1089 if (sm->ap_rsn_ie) {
1090 wpa_hexdump(MSG_INFO, "WPA: RSN IE in Beacon/ProbeResp",
1091 sm->ap_rsn_ie, sm->ap_rsn_ie_len);
1092 }
1093 if (rsn_ie) {
1094 if (!sm->ap_rsn_ie) {
1095 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1096 "WPA: No RSN IE in Beacon/ProbeResp");
1097 }
1098 wpa_hexdump(MSG_INFO, "WPA: RSN IE in 3/4 msg",
1099 rsn_ie, rsn_ie_len);
1100 }
1101
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001102 wpa_sm_deauthenticate(sm, WLAN_REASON_IE_IN_4WAY_DIFFERS);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001103}
1104
1105
1106#ifdef CONFIG_IEEE80211R
1107
1108static int ft_validate_mdie(struct wpa_sm *sm,
1109 const unsigned char *src_addr,
1110 struct wpa_eapol_ie_parse *ie,
1111 const u8 *assoc_resp_mdie)
1112{
1113 struct rsn_mdie *mdie;
1114
1115 mdie = (struct rsn_mdie *) (ie->mdie + 2);
1116 if (ie->mdie == NULL || ie->mdie_len < 2 + sizeof(*mdie) ||
1117 os_memcmp(mdie->mobility_domain, sm->mobility_domain,
1118 MOBILITY_DOMAIN_ID_LEN) != 0) {
1119 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "FT: MDIE in msg 3/4 did "
1120 "not match with the current mobility domain");
1121 return -1;
1122 }
1123
1124 if (assoc_resp_mdie &&
1125 (assoc_resp_mdie[1] != ie->mdie[1] ||
1126 os_memcmp(assoc_resp_mdie, ie->mdie, 2 + ie->mdie[1]) != 0)) {
1127 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "FT: MDIE mismatch");
1128 wpa_hexdump(MSG_DEBUG, "FT: MDIE in EAPOL-Key msg 3/4",
1129 ie->mdie, 2 + ie->mdie[1]);
1130 wpa_hexdump(MSG_DEBUG, "FT: MDIE in (Re)Association Response",
1131 assoc_resp_mdie, 2 + assoc_resp_mdie[1]);
1132 return -1;
1133 }
1134
1135 return 0;
1136}
1137
1138
1139static int ft_validate_ftie(struct wpa_sm *sm,
1140 const unsigned char *src_addr,
1141 struct wpa_eapol_ie_parse *ie,
1142 const u8 *assoc_resp_ftie)
1143{
1144 if (ie->ftie == NULL) {
1145 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1146 "FT: No FTIE in EAPOL-Key msg 3/4");
1147 return -1;
1148 }
1149
1150 if (assoc_resp_ftie == NULL)
1151 return 0;
1152
1153 if (assoc_resp_ftie[1] != ie->ftie[1] ||
1154 os_memcmp(assoc_resp_ftie, ie->ftie, 2 + ie->ftie[1]) != 0) {
1155 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "FT: FTIE mismatch");
1156 wpa_hexdump(MSG_DEBUG, "FT: FTIE in EAPOL-Key msg 3/4",
1157 ie->ftie, 2 + ie->ftie[1]);
1158 wpa_hexdump(MSG_DEBUG, "FT: FTIE in (Re)Association Response",
1159 assoc_resp_ftie, 2 + assoc_resp_ftie[1]);
1160 return -1;
1161 }
1162
1163 return 0;
1164}
1165
1166
1167static int ft_validate_rsnie(struct wpa_sm *sm,
1168 const unsigned char *src_addr,
1169 struct wpa_eapol_ie_parse *ie)
1170{
1171 struct wpa_ie_data rsn;
1172
1173 if (!ie->rsn_ie)
1174 return 0;
1175
1176 /*
1177 * Verify that PMKR1Name from EAPOL-Key message 3/4
1178 * matches with the value we derived.
1179 */
1180 if (wpa_parse_wpa_ie_rsn(ie->rsn_ie, ie->rsn_ie_len, &rsn) < 0 ||
1181 rsn.num_pmkid != 1 || rsn.pmkid == NULL) {
1182 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "FT: No PMKR1Name in "
1183 "FT 4-way handshake message 3/4");
1184 return -1;
1185 }
1186
Dmitry Shmidtc2817022014-07-02 10:32:10 -07001187 if (os_memcmp_const(rsn.pmkid, sm->pmk_r1_name, WPA_PMK_NAME_LEN) != 0)
1188 {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001189 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1190 "FT: PMKR1Name mismatch in "
1191 "FT 4-way handshake message 3/4");
1192 wpa_hexdump(MSG_DEBUG, "FT: PMKR1Name from Authenticator",
1193 rsn.pmkid, WPA_PMK_NAME_LEN);
1194 wpa_hexdump(MSG_DEBUG, "FT: Derived PMKR1Name",
1195 sm->pmk_r1_name, WPA_PMK_NAME_LEN);
1196 return -1;
1197 }
1198
1199 return 0;
1200}
1201
1202
1203static int wpa_supplicant_validate_ie_ft(struct wpa_sm *sm,
1204 const unsigned char *src_addr,
1205 struct wpa_eapol_ie_parse *ie)
1206{
1207 const u8 *pos, *end, *mdie = NULL, *ftie = NULL;
1208
1209 if (sm->assoc_resp_ies) {
1210 pos = sm->assoc_resp_ies;
1211 end = pos + sm->assoc_resp_ies_len;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001212 while (end - pos > 2) {
1213 if (2 + pos[1] > end - pos)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001214 break;
1215 switch (*pos) {
1216 case WLAN_EID_MOBILITY_DOMAIN:
1217 mdie = pos;
1218 break;
1219 case WLAN_EID_FAST_BSS_TRANSITION:
1220 ftie = pos;
1221 break;
1222 }
1223 pos += 2 + pos[1];
1224 }
1225 }
1226
1227 if (ft_validate_mdie(sm, src_addr, ie, mdie) < 0 ||
1228 ft_validate_ftie(sm, src_addr, ie, ftie) < 0 ||
1229 ft_validate_rsnie(sm, src_addr, ie) < 0)
1230 return -1;
1231
1232 return 0;
1233}
1234
1235#endif /* CONFIG_IEEE80211R */
1236
1237
1238static int wpa_supplicant_validate_ie(struct wpa_sm *sm,
1239 const unsigned char *src_addr,
1240 struct wpa_eapol_ie_parse *ie)
1241{
1242 if (sm->ap_wpa_ie == NULL && sm->ap_rsn_ie == NULL) {
1243 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1244 "WPA: No WPA/RSN IE for this AP known. "
1245 "Trying to get from scan results");
1246 if (wpa_sm_get_beacon_ie(sm) < 0) {
1247 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1248 "WPA: Could not find AP from "
1249 "the scan results");
1250 } else {
1251 wpa_msg(sm->ctx->msg_ctx, MSG_DEBUG,
1252 "WPA: Found the current AP from "
1253 "updated scan results");
1254 }
1255 }
1256
1257 if (ie->wpa_ie == NULL && ie->rsn_ie == NULL &&
1258 (sm->ap_wpa_ie || sm->ap_rsn_ie)) {
1259 wpa_report_ie_mismatch(sm, "IE in 3/4 msg does not match "
1260 "with IE in Beacon/ProbeResp (no IE?)",
1261 src_addr, ie->wpa_ie, ie->wpa_ie_len,
1262 ie->rsn_ie, ie->rsn_ie_len);
1263 return -1;
1264 }
1265
1266 if ((ie->wpa_ie && sm->ap_wpa_ie &&
1267 (ie->wpa_ie_len != sm->ap_wpa_ie_len ||
1268 os_memcmp(ie->wpa_ie, sm->ap_wpa_ie, ie->wpa_ie_len) != 0)) ||
1269 (ie->rsn_ie && sm->ap_rsn_ie &&
1270 wpa_compare_rsn_ie(wpa_key_mgmt_ft(sm->key_mgmt),
1271 sm->ap_rsn_ie, sm->ap_rsn_ie_len,
1272 ie->rsn_ie, ie->rsn_ie_len))) {
1273 wpa_report_ie_mismatch(sm, "IE in 3/4 msg does not match "
1274 "with IE in Beacon/ProbeResp",
1275 src_addr, ie->wpa_ie, ie->wpa_ie_len,
1276 ie->rsn_ie, ie->rsn_ie_len);
1277 return -1;
1278 }
1279
1280 if (sm->proto == WPA_PROTO_WPA &&
1281 ie->rsn_ie && sm->ap_rsn_ie == NULL && sm->rsn_enabled) {
1282 wpa_report_ie_mismatch(sm, "Possible downgrade attack "
1283 "detected - RSN was enabled and RSN IE "
1284 "was in msg 3/4, but not in "
1285 "Beacon/ProbeResp",
1286 src_addr, ie->wpa_ie, ie->wpa_ie_len,
1287 ie->rsn_ie, ie->rsn_ie_len);
1288 return -1;
1289 }
1290
1291#ifdef CONFIG_IEEE80211R
1292 if (wpa_key_mgmt_ft(sm->key_mgmt) &&
1293 wpa_supplicant_validate_ie_ft(sm, src_addr, ie) < 0)
1294 return -1;
1295#endif /* CONFIG_IEEE80211R */
1296
1297 return 0;
1298}
1299
1300
1301/**
1302 * wpa_supplicant_send_4_of_4 - Send message 4 of WPA/RSN 4-Way Handshake
1303 * @sm: Pointer to WPA state machine data from wpa_sm_init()
1304 * @dst: Destination address for the frame
1305 * @key: Pointer to the EAPOL-Key frame header
1306 * @ver: Version bits from EAPOL-Key Key Info
1307 * @key_info: Key Info
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001308 * @ptk: PTK to use for keyed hash and encryption
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001309 * Returns: >= 0 on success, < 0 on failure
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001310 */
1311int wpa_supplicant_send_4_of_4(struct wpa_sm *sm, const unsigned char *dst,
1312 const struct wpa_eapol_key *key,
1313 u16 ver, u16 key_info,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001314 struct wpa_ptk *ptk)
1315{
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001316 size_t mic_len, hdrlen, rlen;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001317 struct wpa_eapol_key *reply;
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001318 u8 *rbuf, *key_mic;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001319
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001320 mic_len = wpa_mic_len(sm->key_mgmt, sm->pmk_len);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001321 hdrlen = sizeof(*reply) + mic_len + 2;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001322 rbuf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_KEY, NULL,
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001323 hdrlen, &rlen, (void *) &reply);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001324 if (rbuf == NULL)
1325 return -1;
1326
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001327 reply->type = (sm->proto == WPA_PROTO_RSN ||
1328 sm->proto == WPA_PROTO_OSEN) ?
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001329 EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA;
1330 key_info &= WPA_KEY_INFO_SECURE;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001331 key_info |= ver | WPA_KEY_INFO_KEY_TYPE;
1332 if (mic_len)
1333 key_info |= WPA_KEY_INFO_MIC;
1334 else
1335 key_info |= WPA_KEY_INFO_ENCR_KEY_DATA;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001336 WPA_PUT_BE16(reply->key_info, key_info);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001337 if (sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001338 WPA_PUT_BE16(reply->key_length, 0);
1339 else
1340 os_memcpy(reply->key_length, key->key_length, 2);
1341 os_memcpy(reply->replay_counter, key->replay_counter,
1342 WPA_REPLAY_COUNTER_LEN);
1343
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001344 key_mic = (u8 *) (reply + 1);
1345 WPA_PUT_BE16(key_mic + mic_len, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001346
Roshan Pius5e7db942018-04-12 12:27:41 -07001347 wpa_dbg(sm->ctx->msg_ctx, MSG_INFO, "WPA: Sending EAPOL-Key 4/4");
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001348 return wpa_eapol_key_send(sm, ptk, ver, dst, ETH_P_EAPOL, rbuf, rlen,
1349 key_mic);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001350}
1351
1352
1353static void wpa_supplicant_process_3_of_4(struct wpa_sm *sm,
1354 const struct wpa_eapol_key *key,
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001355 u16 ver, const u8 *key_data,
1356 size_t key_data_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001357{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001358 u16 key_info, keylen;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001359 struct wpa_eapol_ie_parse ie;
1360
1361 wpa_sm_set_state(sm, WPA_4WAY_HANDSHAKE);
Roshan Pius5e7db942018-04-12 12:27:41 -07001362 wpa_dbg(sm->ctx->msg_ctx, MSG_INFO, "WPA: RX message 3 of 4-Way "
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001363 "Handshake from " MACSTR " (ver=%d)", MAC2STR(sm->bssid), ver);
1364
1365 key_info = WPA_GET_BE16(key->key_info);
1366
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001367 wpa_hexdump(MSG_DEBUG, "WPA: IE KeyData", key_data, key_data_len);
1368 if (wpa_supplicant_parse_ies(key_data, key_data_len, &ie) < 0)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001369 goto failed;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001370 if (ie.gtk && !(key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
1371 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1372 "WPA: GTK IE in unencrypted key data");
1373 goto failed;
1374 }
1375#ifdef CONFIG_IEEE80211W
1376 if (ie.igtk && !(key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
1377 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1378 "WPA: IGTK KDE in unencrypted key data");
1379 goto failed;
1380 }
1381
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001382 if (ie.igtk &&
1383 wpa_cipher_valid_mgmt_group(sm->mgmt_group_cipher) &&
1384 ie.igtk_len != WPA_IGTK_KDE_PREFIX_LEN +
1385 (unsigned int) wpa_cipher_key_len(sm->mgmt_group_cipher)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001386 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1387 "WPA: Invalid IGTK KDE length %lu",
1388 (unsigned long) ie.igtk_len);
1389 goto failed;
1390 }
1391#endif /* CONFIG_IEEE80211W */
1392
1393 if (wpa_supplicant_validate_ie(sm, sm->bssid, &ie) < 0)
1394 goto failed;
1395
1396 if (os_memcmp(sm->anonce, key->key_nonce, WPA_NONCE_LEN) != 0) {
1397 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1398 "WPA: ANonce from message 1 of 4-Way Handshake "
1399 "differs from 3 of 4-Way Handshake - drop packet (src="
1400 MACSTR ")", MAC2STR(sm->bssid));
1401 goto failed;
1402 }
1403
1404 keylen = WPA_GET_BE16(key->key_length);
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07001405 if (keylen != wpa_cipher_key_len(sm->pairwise_cipher)) {
1406 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1407 "WPA: Invalid %s key length %d (src=" MACSTR
1408 ")", wpa_cipher_txt(sm->pairwise_cipher), keylen,
1409 MAC2STR(sm->bssid));
1410 goto failed;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001411 }
1412
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001413#ifdef CONFIG_P2P
1414 if (ie.ip_addr_alloc) {
1415 os_memcpy(sm->p2p_ip_addr, ie.ip_addr_alloc, 3 * 4);
1416 wpa_hexdump(MSG_DEBUG, "P2P: IP address info",
1417 sm->p2p_ip_addr, sizeof(sm->p2p_ip_addr));
1418 }
1419#endif /* CONFIG_P2P */
1420
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001421 if (wpa_supplicant_send_4_of_4(sm, sm->bssid, key, ver, key_info,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001422 &sm->ptk) < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001423 goto failed;
1424 }
1425
1426 /* SNonce was successfully used in msg 3/4, so mark it to be renewed
1427 * for the next 4-Way Handshake. If msg 3 is received again, the old
1428 * SNonce will still be used to avoid changing PTK. */
1429 sm->renew_snonce = 1;
1430
1431 if (key_info & WPA_KEY_INFO_INSTALL) {
1432 if (wpa_supplicant_install_ptk(sm, key))
1433 goto failed;
1434 }
1435
1436 if (key_info & WPA_KEY_INFO_SECURE) {
1437 wpa_sm_mlme_setprotection(
1438 sm, sm->bssid, MLME_SETPROTECTION_PROTECT_TYPE_RX,
1439 MLME_SETPROTECTION_KEY_TYPE_PAIRWISE);
1440 eapol_sm_notify_portValid(sm->eapol, TRUE);
1441 }
1442 wpa_sm_set_state(sm, WPA_GROUP_HANDSHAKE);
1443
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001444 if (sm->group_cipher == WPA_CIPHER_GTK_NOT_USED) {
1445 wpa_supplicant_key_neg_complete(sm, sm->bssid,
1446 key_info & WPA_KEY_INFO_SECURE);
1447 } else if (ie.gtk &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001448 wpa_supplicant_pairwise_gtk(sm, key,
1449 ie.gtk, ie.gtk_len, key_info) < 0) {
1450 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1451 "RSN: Failed to configure GTK");
1452 goto failed;
1453 }
1454
1455 if (ieee80211w_set_keys(sm, &ie) < 0) {
1456 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1457 "RSN: Failed to configure IGTK");
1458 goto failed;
1459 }
1460
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001461 if (ie.gtk)
1462 wpa_sm_set_rekey_offload(sm);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001463
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001464 if (sm->proto == WPA_PROTO_RSN && wpa_key_mgmt_suite_b(sm->key_mgmt)) {
1465 struct rsn_pmksa_cache_entry *sa;
1466
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001467 sa = pmksa_cache_add(sm->pmksa, sm->pmk, sm->pmk_len, NULL,
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001468 sm->ptk.kck, sm->ptk.kck_len,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001469 sm->bssid, sm->own_addr,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001470 sm->network_ctx, sm->key_mgmt, NULL);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001471 if (!sm->cur_pmksa)
1472 sm->cur_pmksa = sa;
1473 }
1474
Dmitry Shmidt4dd28dc2015-03-10 11:21:43 -07001475 sm->msg_3_of_4_ok = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001476 return;
1477
1478failed:
1479 wpa_sm_deauthenticate(sm, WLAN_REASON_UNSPECIFIED);
1480}
1481
1482
1483static int wpa_supplicant_process_1_of_2_rsn(struct wpa_sm *sm,
1484 const u8 *keydata,
1485 size_t keydatalen,
1486 u16 key_info,
1487 struct wpa_gtk_data *gd)
1488{
1489 int maxkeylen;
1490 struct wpa_eapol_ie_parse ie;
1491
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08001492 wpa_hexdump_key(MSG_DEBUG, "RSN: msg 1/2 key data",
1493 keydata, keydatalen);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001494 if (wpa_supplicant_parse_ies(keydata, keydatalen, &ie) < 0)
1495 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001496 if (ie.gtk && !(key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
1497 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1498 "WPA: GTK IE in unencrypted key data");
1499 return -1;
1500 }
1501 if (ie.gtk == NULL) {
1502 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1503 "WPA: No GTK IE in Group Key msg 1/2");
1504 return -1;
1505 }
1506 maxkeylen = gd->gtk_len = ie.gtk_len - 2;
1507
1508 if (wpa_supplicant_check_group_cipher(sm, sm->group_cipher,
1509 gd->gtk_len, maxkeylen,
1510 &gd->key_rsc_len, &gd->alg))
1511 return -1;
1512
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001513 wpa_hexdump_key(MSG_DEBUG, "RSN: received GTK in group key handshake",
1514 ie.gtk, ie.gtk_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001515 gd->keyidx = ie.gtk[0] & 0x3;
1516 gd->tx = wpa_supplicant_gtk_tx_bit_workaround(sm,
1517 !!(ie.gtk[0] & BIT(2)));
1518 if (ie.gtk_len - 2 > sizeof(gd->gtk)) {
1519 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1520 "RSN: Too long GTK in GTK IE (len=%lu)",
1521 (unsigned long) ie.gtk_len - 2);
1522 return -1;
1523 }
1524 os_memcpy(gd->gtk, ie.gtk + 2, ie.gtk_len - 2);
1525
1526 if (ieee80211w_set_keys(sm, &ie) < 0)
1527 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1528 "RSN: Failed to configure IGTK");
1529
1530 return 0;
1531}
1532
1533
1534static int wpa_supplicant_process_1_of_2_wpa(struct wpa_sm *sm,
1535 const struct wpa_eapol_key *key,
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001536 const u8 *key_data,
1537 size_t key_data_len, u16 key_info,
1538 u16 ver, struct wpa_gtk_data *gd)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001539{
1540 size_t maxkeylen;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001541 u16 gtk_len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001542
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001543 gtk_len = WPA_GET_BE16(key->key_length);
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001544 maxkeylen = key_data_len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001545 if (ver == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
1546 if (maxkeylen < 8) {
1547 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1548 "WPA: Too short maxkeylen (%lu)",
1549 (unsigned long) maxkeylen);
1550 return -1;
1551 }
1552 maxkeylen -= 8;
1553 }
1554
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001555 if (gtk_len > maxkeylen ||
1556 wpa_supplicant_check_group_cipher(sm, sm->group_cipher,
1557 gtk_len, maxkeylen,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001558 &gd->key_rsc_len, &gd->alg))
1559 return -1;
1560
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001561 gd->gtk_len = gtk_len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001562 gd->keyidx = (key_info & WPA_KEY_INFO_KEY_INDEX_MASK) >>
1563 WPA_KEY_INFO_KEY_INDEX_SHIFT;
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001564 if (ver == WPA_KEY_INFO_TYPE_HMAC_MD5_RC4 && sm->ptk.kek_len == 16) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001565#ifdef CONFIG_NO_RC4
1566 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1567 "WPA: RC4 not supported in the build");
1568 return -1;
1569#else /* CONFIG_NO_RC4 */
Dmitry Shmidt61593f02014-04-21 16:27:35 -07001570 u8 ek[32];
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001571 if (key_data_len > sizeof(gd->gtk)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001572 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1573 "WPA: RC4 key data too long (%lu)",
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001574 (unsigned long) key_data_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001575 return -1;
1576 }
Dmitry Shmidt61593f02014-04-21 16:27:35 -07001577 os_memcpy(ek, key->key_iv, 16);
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001578 os_memcpy(ek + 16, sm->ptk.kek, sm->ptk.kek_len);
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001579 os_memcpy(gd->gtk, key_data, key_data_len);
1580 if (rc4_skip(ek, 32, 256, gd->gtk, key_data_len)) {
Dmitry Shmidt61593f02014-04-21 16:27:35 -07001581 os_memset(ek, 0, sizeof(ek));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001582 wpa_msg(sm->ctx->msg_ctx, MSG_ERROR,
1583 "WPA: RC4 failed");
1584 return -1;
1585 }
Dmitry Shmidt61593f02014-04-21 16:27:35 -07001586 os_memset(ek, 0, sizeof(ek));
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001587#endif /* CONFIG_NO_RC4 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001588 } else if (ver == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001589 if (maxkeylen % 8) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001590 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1591 "WPA: Unsupported AES-WRAP len %lu",
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001592 (unsigned long) maxkeylen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001593 return -1;
1594 }
1595 if (maxkeylen > sizeof(gd->gtk)) {
1596 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1597 "WPA: AES-WRAP key data "
1598 "too long (keydatalen=%lu maxkeylen=%lu)",
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001599 (unsigned long) key_data_len,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001600 (unsigned long) maxkeylen);
1601 return -1;
1602 }
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001603 if (aes_unwrap(sm->ptk.kek, sm->ptk.kek_len, maxkeylen / 8,
1604 key_data, gd->gtk)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001605 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1606 "WPA: AES unwrap failed - could not decrypt "
1607 "GTK");
1608 return -1;
1609 }
1610 } else {
1611 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1612 "WPA: Unsupported key_info type %d", ver);
1613 return -1;
1614 }
1615 gd->tx = wpa_supplicant_gtk_tx_bit_workaround(
1616 sm, !!(key_info & WPA_KEY_INFO_TXRX));
1617 return 0;
1618}
1619
1620
1621static int wpa_supplicant_send_2_of_2(struct wpa_sm *sm,
1622 const struct wpa_eapol_key *key,
1623 int ver, u16 key_info)
1624{
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001625 size_t mic_len, hdrlen, rlen;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001626 struct wpa_eapol_key *reply;
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001627 u8 *rbuf, *key_mic;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001628
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001629 mic_len = wpa_mic_len(sm->key_mgmt, sm->pmk_len);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001630 hdrlen = sizeof(*reply) + mic_len + 2;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001631 rbuf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_KEY, NULL,
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001632 hdrlen, &rlen, (void *) &reply);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001633 if (rbuf == NULL)
1634 return -1;
1635
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001636 reply->type = (sm->proto == WPA_PROTO_RSN ||
1637 sm->proto == WPA_PROTO_OSEN) ?
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001638 EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA;
1639 key_info &= WPA_KEY_INFO_KEY_INDEX_MASK;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001640 key_info |= ver | WPA_KEY_INFO_SECURE;
1641 if (mic_len)
1642 key_info |= WPA_KEY_INFO_MIC;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001643 else
1644 key_info |= WPA_KEY_INFO_ENCR_KEY_DATA;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001645 WPA_PUT_BE16(reply->key_info, key_info);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001646 if (sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001647 WPA_PUT_BE16(reply->key_length, 0);
1648 else
1649 os_memcpy(reply->key_length, key->key_length, 2);
1650 os_memcpy(reply->replay_counter, key->replay_counter,
1651 WPA_REPLAY_COUNTER_LEN);
1652
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001653 key_mic = (u8 *) (reply + 1);
1654 WPA_PUT_BE16(key_mic + mic_len, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001655
1656 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Sending EAPOL-Key 2/2");
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001657 return wpa_eapol_key_send(sm, &sm->ptk, ver, sm->bssid, ETH_P_EAPOL,
1658 rbuf, rlen, key_mic);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001659}
1660
1661
1662static void wpa_supplicant_process_1_of_2(struct wpa_sm *sm,
1663 const unsigned char *src_addr,
1664 const struct wpa_eapol_key *key,
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001665 const u8 *key_data,
1666 size_t key_data_len, u16 ver)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001667{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001668 u16 key_info;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001669 int rekey, ret;
1670 struct wpa_gtk_data gd;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001671 const u8 *key_rsc;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001672
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001673 if (!sm->msg_3_of_4_ok && !wpa_fils_is_completed(sm)) {
Dmitry Shmidt4dd28dc2015-03-10 11:21:43 -07001674 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
1675 "WPA: Group Key Handshake started prior to completion of 4-way handshake");
1676 goto failed;
1677 }
1678
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001679 os_memset(&gd, 0, sizeof(gd));
1680
1681 rekey = wpa_sm_get_state(sm) == WPA_COMPLETED;
1682 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: RX message 1 of Group Key "
1683 "Handshake from " MACSTR " (ver=%d)", MAC2STR(src_addr), ver);
1684
1685 key_info = WPA_GET_BE16(key->key_info);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001686
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001687 if (sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN) {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001688 ret = wpa_supplicant_process_1_of_2_rsn(sm, key_data,
1689 key_data_len, key_info,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001690 &gd);
1691 } else {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001692 ret = wpa_supplicant_process_1_of_2_wpa(sm, key, key_data,
1693 key_data_len,
1694 key_info, ver, &gd);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001695 }
1696
1697 wpa_sm_set_state(sm, WPA_GROUP_HANDSHAKE);
1698
1699 if (ret)
1700 goto failed;
1701
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001702 key_rsc = key->key_rsc;
1703 if (wpa_supplicant_rsc_relaxation(sm, key->key_rsc))
1704 key_rsc = null_rsc;
1705
Jouni Malinen58c0e962017-10-01 12:12:24 +03001706 if (wpa_supplicant_install_gtk(sm, &gd, key_rsc, 0) ||
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001707 wpa_supplicant_send_2_of_2(sm, key, ver, key_info) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001708 goto failed;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001709 os_memset(&gd, 0, sizeof(gd));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001710
1711 if (rekey) {
1712 wpa_msg(sm->ctx->msg_ctx, MSG_INFO, "WPA: Group rekeying "
1713 "completed with " MACSTR " [GTK=%s]",
1714 MAC2STR(sm->bssid), wpa_cipher_txt(sm->group_cipher));
1715 wpa_sm_cancel_auth_timeout(sm);
1716 wpa_sm_set_state(sm, WPA_COMPLETED);
1717 } else {
1718 wpa_supplicant_key_neg_complete(sm, sm->bssid,
1719 key_info &
1720 WPA_KEY_INFO_SECURE);
1721 }
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001722
1723 wpa_sm_set_rekey_offload(sm);
1724
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001725 return;
1726
1727failed:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001728 os_memset(&gd, 0, sizeof(gd));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001729 wpa_sm_deauthenticate(sm, WLAN_REASON_UNSPECIFIED);
1730}
1731
1732
1733static int wpa_supplicant_verify_eapol_key_mic(struct wpa_sm *sm,
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001734 struct wpa_eapol_key *key,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001735 u16 ver,
1736 const u8 *buf, size_t len)
1737{
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001738 u8 mic[WPA_EAPOL_KEY_MIC_MAX_LEN];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001739 int ok = 0;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001740 size_t mic_len = wpa_mic_len(sm->key_mgmt, sm->pmk_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001741
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001742 os_memcpy(mic, key + 1, mic_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001743 if (sm->tptk_set) {
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001744 os_memset(key + 1, 0, mic_len);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001745 if (wpa_eapol_key_mic(sm->tptk.kck, sm->tptk.kck_len,
1746 sm->key_mgmt,
1747 ver, buf, len, (u8 *) (key + 1)) < 0 ||
1748 os_memcmp_const(mic, key + 1, mic_len) != 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001749 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1750 "WPA: Invalid EAPOL-Key MIC "
1751 "when using TPTK - ignoring TPTK");
1752 } else {
1753 ok = 1;
1754 sm->tptk_set = 0;
1755 sm->ptk_set = 1;
1756 os_memcpy(&sm->ptk, &sm->tptk, sizeof(sm->ptk));
Dmitry Shmidt61593f02014-04-21 16:27:35 -07001757 os_memset(&sm->tptk, 0, sizeof(sm->tptk));
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001758 /*
1759 * This assures the same TPTK in sm->tptk can never be
Roshan Pius3a1667e2018-07-03 15:17:14 -07001760 * copied twice to sm->ptk as the new PTK. In
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001761 * combination with the installed flag in the wpa_ptk
1762 * struct, this assures the same PTK is only installed
1763 * once.
1764 */
1765 sm->renew_snonce = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001766 }
1767 }
1768
1769 if (!ok && sm->ptk_set) {
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001770 os_memset(key + 1, 0, mic_len);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001771 if (wpa_eapol_key_mic(sm->ptk.kck, sm->ptk.kck_len,
1772 sm->key_mgmt,
1773 ver, buf, len, (u8 *) (key + 1)) < 0 ||
1774 os_memcmp_const(mic, key + 1, mic_len) != 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001775 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1776 "WPA: Invalid EAPOL-Key MIC - "
1777 "dropping packet");
1778 return -1;
1779 }
1780 ok = 1;
1781 }
1782
1783 if (!ok) {
1784 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1785 "WPA: Could not verify EAPOL-Key MIC - "
1786 "dropping packet");
1787 return -1;
1788 }
1789
1790 os_memcpy(sm->rx_replay_counter, key->replay_counter,
1791 WPA_REPLAY_COUNTER_LEN);
1792 sm->rx_replay_counter_set = 1;
1793 return 0;
1794}
1795
1796
1797/* Decrypt RSN EAPOL-Key key data (RC4 or AES-WRAP) */
1798static int wpa_supplicant_decrypt_key_data(struct wpa_sm *sm,
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001799 struct wpa_eapol_key *key,
1800 size_t mic_len, u16 ver,
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001801 u8 *key_data, size_t *key_data_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001802{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001803 wpa_hexdump(MSG_DEBUG, "RSN: encrypted key data",
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001804 key_data, *key_data_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001805 if (!sm->ptk_set) {
1806 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1807 "WPA: PTK not available, cannot decrypt EAPOL-Key Key "
1808 "Data");
1809 return -1;
1810 }
1811
1812 /* Decrypt key data here so that this operation does not need
1813 * to be implemented separately for each message type. */
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001814 if (ver == WPA_KEY_INFO_TYPE_HMAC_MD5_RC4 && sm->ptk.kek_len == 16) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001815#ifdef CONFIG_NO_RC4
1816 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1817 "WPA: RC4 not supported in the build");
1818 return -1;
1819#else /* CONFIG_NO_RC4 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001820 u8 ek[32];
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001821
1822 wpa_printf(MSG_DEBUG, "WPA: Decrypt Key Data using RC4");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001823 os_memcpy(ek, key->key_iv, 16);
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001824 os_memcpy(ek + 16, sm->ptk.kek, sm->ptk.kek_len);
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001825 if (rc4_skip(ek, 32, 256, key_data, *key_data_len)) {
Dmitry Shmidt61593f02014-04-21 16:27:35 -07001826 os_memset(ek, 0, sizeof(ek));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001827 wpa_msg(sm->ctx->msg_ctx, MSG_ERROR,
1828 "WPA: RC4 failed");
1829 return -1;
1830 }
Dmitry Shmidt61593f02014-04-21 16:27:35 -07001831 os_memset(ek, 0, sizeof(ek));
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001832#endif /* CONFIG_NO_RC4 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001833 } else if (ver == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES ||
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001834 ver == WPA_KEY_INFO_TYPE_AES_128_CMAC ||
Roshan Pius3a1667e2018-07-03 15:17:14 -07001835 wpa_use_aes_key_wrap(sm->key_mgmt)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001836 u8 *buf;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001837
1838 wpa_printf(MSG_DEBUG,
1839 "WPA: Decrypt Key Data using AES-UNWRAP (KEK length %u)",
1840 (unsigned int) sm->ptk.kek_len);
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001841 if (*key_data_len < 8 || *key_data_len % 8) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001842 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001843 "WPA: Unsupported AES-WRAP len %u",
1844 (unsigned int) *key_data_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001845 return -1;
1846 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001847 *key_data_len -= 8; /* AES-WRAP adds 8 bytes */
1848 buf = os_malloc(*key_data_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001849 if (buf == NULL) {
1850 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1851 "WPA: No memory for AES-UNWRAP buffer");
1852 return -1;
1853 }
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001854 if (aes_unwrap(sm->ptk.kek, sm->ptk.kek_len, *key_data_len / 8,
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001855 key_data, buf)) {
Dmitry Shmidt7d56b752015-12-22 10:59:44 -08001856 bin_clear_free(buf, *key_data_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001857 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1858 "WPA: AES unwrap failed - "
1859 "could not decrypt EAPOL-Key key data");
1860 return -1;
1861 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001862 os_memcpy(key_data, buf, *key_data_len);
Dmitry Shmidt7d56b752015-12-22 10:59:44 -08001863 bin_clear_free(buf, *key_data_len);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001864 WPA_PUT_BE16(((u8 *) (key + 1)) + mic_len, *key_data_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001865 } else {
1866 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
1867 "WPA: Unsupported key_info type %d", ver);
1868 return -1;
1869 }
1870 wpa_hexdump_key(MSG_DEBUG, "WPA: decrypted EAPOL-Key key data",
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07001871 key_data, *key_data_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001872 return 0;
1873}
1874
1875
1876/**
1877 * wpa_sm_aborted_cached - Notify WPA that PMKSA caching was aborted
1878 * @sm: Pointer to WPA state machine data from wpa_sm_init()
1879 */
1880void wpa_sm_aborted_cached(struct wpa_sm *sm)
1881{
1882 if (sm && sm->cur_pmksa) {
1883 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1884 "RSN: Cancelling PMKSA caching attempt");
1885 sm->cur_pmksa = NULL;
1886 }
1887}
1888
1889
1890static void wpa_eapol_key_dump(struct wpa_sm *sm,
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001891 const struct wpa_eapol_key *key,
1892 unsigned int key_data_len,
1893 const u8 *mic, unsigned int mic_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001894{
1895#ifndef CONFIG_NO_STDOUT_DEBUG
1896 u16 key_info = WPA_GET_BE16(key->key_info);
1897
1898 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, " EAPOL-Key type=%d", key->type);
1899 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1900 " key_info 0x%x (ver=%d keyidx=%d rsvd=%d %s%s%s%s%s%s%s%s)",
1901 key_info, key_info & WPA_KEY_INFO_TYPE_MASK,
1902 (key_info & WPA_KEY_INFO_KEY_INDEX_MASK) >>
1903 WPA_KEY_INFO_KEY_INDEX_SHIFT,
1904 (key_info & (BIT(13) | BIT(14) | BIT(15))) >> 13,
1905 key_info & WPA_KEY_INFO_KEY_TYPE ? "Pairwise" : "Group",
1906 key_info & WPA_KEY_INFO_INSTALL ? " Install" : "",
1907 key_info & WPA_KEY_INFO_ACK ? " Ack" : "",
1908 key_info & WPA_KEY_INFO_MIC ? " MIC" : "",
1909 key_info & WPA_KEY_INFO_SECURE ? " Secure" : "",
1910 key_info & WPA_KEY_INFO_ERROR ? " Error" : "",
1911 key_info & WPA_KEY_INFO_REQUEST ? " Request" : "",
1912 key_info & WPA_KEY_INFO_ENCR_KEY_DATA ? " Encr" : "");
1913 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
1914 " key_length=%u key_data_length=%u",
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001915 WPA_GET_BE16(key->key_length), key_data_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001916 wpa_hexdump(MSG_DEBUG, " replay_counter",
1917 key->replay_counter, WPA_REPLAY_COUNTER_LEN);
1918 wpa_hexdump(MSG_DEBUG, " key_nonce", key->key_nonce, WPA_NONCE_LEN);
1919 wpa_hexdump(MSG_DEBUG, " key_iv", key->key_iv, 16);
1920 wpa_hexdump(MSG_DEBUG, " key_rsc", key->key_rsc, 8);
1921 wpa_hexdump(MSG_DEBUG, " key_id (reserved)", key->key_id, 8);
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001922 wpa_hexdump(MSG_DEBUG, " key_mic", mic, mic_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001923#endif /* CONFIG_NO_STDOUT_DEBUG */
1924}
1925
1926
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001927#ifdef CONFIG_FILS
1928static int wpa_supp_aead_decrypt(struct wpa_sm *sm, u8 *buf, size_t buf_len,
1929 size_t *key_data_len)
1930{
1931 struct wpa_ptk *ptk;
1932 struct ieee802_1x_hdr *hdr;
1933 struct wpa_eapol_key *key;
1934 u8 *pos, *tmp;
1935 const u8 *aad[1];
1936 size_t aad_len[1];
1937
1938 if (*key_data_len < AES_BLOCK_SIZE) {
1939 wpa_printf(MSG_INFO, "No room for AES-SIV data in the frame");
1940 return -1;
1941 }
1942
1943 if (sm->tptk_set)
1944 ptk = &sm->tptk;
1945 else if (sm->ptk_set)
1946 ptk = &sm->ptk;
1947 else
1948 return -1;
1949
1950 hdr = (struct ieee802_1x_hdr *) buf;
1951 key = (struct wpa_eapol_key *) (hdr + 1);
1952 pos = (u8 *) (key + 1);
1953 pos += 2; /* Pointing at the Encrypted Key Data field */
1954
1955 tmp = os_malloc(*key_data_len);
1956 if (!tmp)
1957 return -1;
1958
1959 /* AES-SIV AAD from EAPOL protocol version field (inclusive) to
1960 * to Key Data (exclusive). */
1961 aad[0] = buf;
1962 aad_len[0] = pos - buf;
1963 if (aes_siv_decrypt(ptk->kek, ptk->kek_len, pos, *key_data_len,
1964 1, aad, aad_len, tmp) < 0) {
1965 wpa_printf(MSG_INFO, "Invalid AES-SIV data in the frame");
1966 bin_clear_free(tmp, *key_data_len);
1967 return -1;
1968 }
1969
1970 /* AEAD decryption and validation completed successfully */
1971 (*key_data_len) -= AES_BLOCK_SIZE;
1972 wpa_hexdump_key(MSG_DEBUG, "WPA: Decrypted Key Data",
1973 tmp, *key_data_len);
1974
1975 /* Replace Key Data field with the decrypted version */
1976 os_memcpy(pos, tmp, *key_data_len);
1977 pos -= 2; /* Key Data Length field */
1978 WPA_PUT_BE16(pos, *key_data_len);
1979 bin_clear_free(tmp, *key_data_len);
1980
1981 if (sm->tptk_set) {
1982 sm->tptk_set = 0;
1983 sm->ptk_set = 1;
1984 os_memcpy(&sm->ptk, &sm->tptk, sizeof(sm->ptk));
1985 os_memset(&sm->tptk, 0, sizeof(sm->tptk));
1986 }
1987
1988 os_memcpy(sm->rx_replay_counter, key->replay_counter,
1989 WPA_REPLAY_COUNTER_LEN);
1990 sm->rx_replay_counter_set = 1;
1991
1992 return 0;
1993}
1994#endif /* CONFIG_FILS */
1995
1996
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001997/**
1998 * wpa_sm_rx_eapol - Process received WPA EAPOL frames
1999 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2000 * @src_addr: Source MAC address of the EAPOL packet
2001 * @buf: Pointer to the beginning of the EAPOL data (EAPOL header)
2002 * @len: Length of the EAPOL frame
2003 * Returns: 1 = WPA EAPOL-Key processed, 0 = not a WPA EAPOL-Key, -1 failure
2004 *
2005 * This function is called for each received EAPOL frame. Other than EAPOL-Key
2006 * frames can be skipped if filtering is done elsewhere. wpa_sm_rx_eapol() is
2007 * only processing WPA and WPA2 EAPOL-Key frames.
2008 *
2009 * The received EAPOL-Key packets are validated and valid packets are replied
2010 * to. In addition, key material (PTK, GTK) is configured at the end of a
2011 * successful key handshake.
2012 */
2013int wpa_sm_rx_eapol(struct wpa_sm *sm, const u8 *src_addr,
2014 const u8 *buf, size_t len)
2015{
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002016 size_t plen, data_len, key_data_len;
2017 const struct ieee802_1x_hdr *hdr;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002018 struct wpa_eapol_key *key;
2019 u16 key_info, ver;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002020 u8 *tmp = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002021 int ret = -1;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002022 u8 *mic, *key_data;
Dmitry Shmidt807291d2015-01-27 13:40:23 -08002023 size_t mic_len, keyhdrlen;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002024
2025#ifdef CONFIG_IEEE80211R
2026 sm->ft_completed = 0;
2027#endif /* CONFIG_IEEE80211R */
2028
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002029 mic_len = wpa_mic_len(sm->key_mgmt, sm->pmk_len);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002030 keyhdrlen = sizeof(*key) + mic_len + 2;
Dmitry Shmidt807291d2015-01-27 13:40:23 -08002031
2032 if (len < sizeof(*hdr) + keyhdrlen) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002033 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2034 "WPA: EAPOL frame too short to be a WPA "
2035 "EAPOL-Key (len %lu, expecting at least %lu)",
2036 (unsigned long) len,
Dmitry Shmidt807291d2015-01-27 13:40:23 -08002037 (unsigned long) sizeof(*hdr) + keyhdrlen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002038 return 0;
2039 }
2040
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002041 hdr = (const struct ieee802_1x_hdr *) buf;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002042 plen = be_to_host16(hdr->length);
2043 data_len = plen + sizeof(*hdr);
2044 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2045 "IEEE 802.1X RX: version=%d type=%d length=%lu",
2046 hdr->version, hdr->type, (unsigned long) plen);
2047
2048 if (hdr->version < EAPOL_VERSION) {
2049 /* TODO: backwards compatibility */
2050 }
2051 if (hdr->type != IEEE802_1X_TYPE_EAPOL_KEY) {
2052 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2053 "WPA: EAPOL frame (type %u) discarded, "
2054 "not a Key frame", hdr->type);
2055 ret = 0;
2056 goto out;
2057 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002058 wpa_hexdump(MSG_MSGDUMP, "WPA: RX EAPOL-Key", buf, len);
Dmitry Shmidt807291d2015-01-27 13:40:23 -08002059 if (plen > len - sizeof(*hdr) || plen < keyhdrlen) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002060 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2061 "WPA: EAPOL frame payload size %lu "
2062 "invalid (frame size %lu)",
2063 (unsigned long) plen, (unsigned long) len);
2064 ret = 0;
2065 goto out;
2066 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002067 if (data_len < len) {
2068 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2069 "WPA: ignoring %lu bytes after the IEEE 802.1X data",
2070 (unsigned long) len - data_len);
2071 }
2072
2073 /*
2074 * Make a copy of the frame since we need to modify the buffer during
2075 * MAC validation and Key Data decryption.
2076 */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002077 tmp = os_memdup(buf, data_len);
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002078 if (tmp == NULL)
2079 goto out;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002080 key = (struct wpa_eapol_key *) (tmp + sizeof(struct ieee802_1x_hdr));
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002081 mic = (u8 *) (key + 1);
2082 key_data = mic + mic_len + 2;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002083
2084 if (key->type != EAPOL_KEY_TYPE_WPA && key->type != EAPOL_KEY_TYPE_RSN)
2085 {
2086 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2087 "WPA: EAPOL-Key type (%d) unknown, discarded",
2088 key->type);
2089 ret = 0;
2090 goto out;
2091 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002092
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002093 key_data_len = WPA_GET_BE16(mic + mic_len);
2094 wpa_eapol_key_dump(sm, key, key_data_len, mic, mic_len);
Dmitry Shmidt807291d2015-01-27 13:40:23 -08002095
2096 if (key_data_len > plen - keyhdrlen) {
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002097 wpa_msg(sm->ctx->msg_ctx, MSG_INFO, "WPA: Invalid EAPOL-Key "
2098 "frame - key_data overflow (%u > %u)",
2099 (unsigned int) key_data_len,
Dmitry Shmidt807291d2015-01-27 13:40:23 -08002100 (unsigned int) (plen - keyhdrlen));
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002101 goto out;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002102 }
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002103
2104 eapol_sm_notify_lower_layer_success(sm->eapol, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002105 key_info = WPA_GET_BE16(key->key_info);
2106 ver = key_info & WPA_KEY_INFO_TYPE_MASK;
2107 if (ver != WPA_KEY_INFO_TYPE_HMAC_MD5_RC4 &&
2108#if defined(CONFIG_IEEE80211R) || defined(CONFIG_IEEE80211W)
2109 ver != WPA_KEY_INFO_TYPE_AES_128_CMAC &&
2110#endif /* CONFIG_IEEE80211R || CONFIG_IEEE80211W */
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002111 ver != WPA_KEY_INFO_TYPE_HMAC_SHA1_AES &&
Roshan Pius3a1667e2018-07-03 15:17:14 -07002112 !wpa_use_akm_defined(sm->key_mgmt)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002113 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2114 "WPA: Unsupported EAPOL-Key descriptor version %d",
2115 ver);
2116 goto out;
2117 }
2118
Roshan Pius3a1667e2018-07-03 15:17:14 -07002119 if (wpa_use_akm_defined(sm->key_mgmt) &&
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002120 ver != WPA_KEY_INFO_TYPE_AKM_DEFINED) {
2121 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2122 "RSN: Unsupported EAPOL-Key descriptor version %d (expected AKM defined = 0)",
2123 ver);
2124 goto out;
2125 }
2126
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002127#ifdef CONFIG_IEEE80211R
2128 if (wpa_key_mgmt_ft(sm->key_mgmt)) {
2129 /* IEEE 802.11r uses a new key_info type (AES-128-CMAC). */
Roshan Pius3a1667e2018-07-03 15:17:14 -07002130 if (ver != WPA_KEY_INFO_TYPE_AES_128_CMAC &&
2131 !wpa_use_akm_defined(sm->key_mgmt)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002132 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2133 "FT: AP did not use AES-128-CMAC");
2134 goto out;
2135 }
2136 } else
2137#endif /* CONFIG_IEEE80211R */
2138#ifdef CONFIG_IEEE80211W
2139 if (wpa_key_mgmt_sha256(sm->key_mgmt)) {
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002140 if (ver != WPA_KEY_INFO_TYPE_AES_128_CMAC &&
Roshan Pius3a1667e2018-07-03 15:17:14 -07002141 !wpa_use_akm_defined(sm->key_mgmt)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002142 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2143 "WPA: AP did not use the "
2144 "negotiated AES-128-CMAC");
2145 goto out;
2146 }
2147 } else
2148#endif /* CONFIG_IEEE80211W */
2149 if (sm->pairwise_cipher == WPA_CIPHER_CCMP &&
Roshan Pius3a1667e2018-07-03 15:17:14 -07002150 !wpa_use_akm_defined(sm->key_mgmt) &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002151 ver != WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
2152 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2153 "WPA: CCMP is used, but EAPOL-Key "
2154 "descriptor version (%d) is not 2", ver);
2155 if (sm->group_cipher != WPA_CIPHER_CCMP &&
2156 !(key_info & WPA_KEY_INFO_KEY_TYPE)) {
2157 /* Earlier versions of IEEE 802.11i did not explicitly
2158 * require version 2 descriptor for all EAPOL-Key
2159 * packets, so allow group keys to use version 1 if
2160 * CCMP is not used for them. */
2161 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2162 "WPA: Backwards compatibility: allow invalid "
2163 "version for non-CCMP group keys");
Jouni Malinen658fb4a2014-11-14 20:57:05 +02002164 } else if (ver == WPA_KEY_INFO_TYPE_AES_128_CMAC) {
2165 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2166 "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 -07002167 } else
2168 goto out;
Dmitry Shmidt71757432014-06-02 13:50:35 -07002169 } else if (sm->pairwise_cipher == WPA_CIPHER_GCMP &&
Roshan Pius3a1667e2018-07-03 15:17:14 -07002170 !wpa_use_akm_defined(sm->key_mgmt) &&
Dmitry Shmidt71757432014-06-02 13:50:35 -07002171 ver != WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002172 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2173 "WPA: GCMP is used, but EAPOL-Key "
2174 "descriptor version (%d) is not 2", ver);
2175 goto out;
2176 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002177
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002178 if (sm->rx_replay_counter_set &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002179 os_memcmp(key->replay_counter, sm->rx_replay_counter,
2180 WPA_REPLAY_COUNTER_LEN) <= 0) {
2181 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
2182 "WPA: EAPOL-Key Replay Counter did not increase - "
2183 "dropping packet");
2184 goto out;
2185 }
2186
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002187 if (key_info & WPA_KEY_INFO_SMK_MESSAGE) {
2188 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2189 "WPA: Unsupported SMK bit in key_info");
2190 goto out;
2191 }
2192
2193 if (!(key_info & WPA_KEY_INFO_ACK)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002194 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2195 "WPA: No Ack bit in key_info");
2196 goto out;
2197 }
2198
2199 if (key_info & WPA_KEY_INFO_REQUEST) {
2200 wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
2201 "WPA: EAPOL-Key with Request bit - dropped");
2202 goto out;
2203 }
2204
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002205 if ((key_info & WPA_KEY_INFO_MIC) &&
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002206 wpa_supplicant_verify_eapol_key_mic(sm, key, ver, tmp, data_len))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002207 goto out;
2208
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002209#ifdef CONFIG_FILS
2210 if (!mic_len && (key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
2211 if (wpa_supp_aead_decrypt(sm, tmp, data_len, &key_data_len))
2212 goto out;
2213 }
2214#endif /* CONFIG_FILS */
2215
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002216 if ((sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN) &&
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002217 (key_info & WPA_KEY_INFO_ENCR_KEY_DATA) && mic_len) {
Hai Shalomce48b4a2018-09-05 11:41:35 -07002218 /*
2219 * Only decrypt the Key Data field if the frame's authenticity
2220 * was verified. When using AES-SIV (FILS), the MIC flag is not
2221 * set, so this check should only be performed if mic_len != 0
2222 * which is the case in this code branch.
2223 */
2224 if (!(key_info & WPA_KEY_INFO_MIC)) {
2225 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
2226 "WPA: Ignore EAPOL-Key with encrypted but unauthenticated data");
2227 goto out;
2228 }
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002229 if (wpa_supplicant_decrypt_key_data(sm, key, mic_len,
2230 ver, key_data,
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002231 &key_data_len))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002232 goto out;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002233 }
2234
2235 if (key_info & WPA_KEY_INFO_KEY_TYPE) {
2236 if (key_info & WPA_KEY_INFO_KEY_INDEX_MASK) {
2237 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
2238 "WPA: Ignored EAPOL-Key (Pairwise) with "
2239 "non-zero key index");
2240 goto out;
2241 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002242 if (key_info & (WPA_KEY_INFO_MIC |
2243 WPA_KEY_INFO_ENCR_KEY_DATA)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002244 /* 3/4 4-Way Handshake */
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002245 wpa_supplicant_process_3_of_4(sm, key, ver, key_data,
2246 key_data_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002247 } else {
2248 /* 1/4 4-Way Handshake */
2249 wpa_supplicant_process_1_of_4(sm, src_addr, key,
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002250 ver, key_data,
2251 key_data_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002252 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002253 } else {
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002254 if ((mic_len && (key_info & WPA_KEY_INFO_MIC)) ||
2255 (!mic_len && (key_info & WPA_KEY_INFO_ENCR_KEY_DATA))) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002256 /* 1/2 Group Key Handshake */
2257 wpa_supplicant_process_1_of_2(sm, src_addr, key,
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07002258 key_data, key_data_len,
2259 ver);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002260 } else {
2261 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002262 "WPA: EAPOL-Key (Group) without Mic/Encr bit - "
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002263 "dropped");
2264 }
2265 }
2266
2267 ret = 1;
2268
2269out:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002270 bin_clear_free(tmp, data_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002271 return ret;
2272}
2273
2274
2275#ifdef CONFIG_CTRL_IFACE
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002276static u32 wpa_key_mgmt_suite(struct wpa_sm *sm)
2277{
2278 switch (sm->key_mgmt) {
2279 case WPA_KEY_MGMT_IEEE8021X:
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002280 return ((sm->proto == WPA_PROTO_RSN ||
2281 sm->proto == WPA_PROTO_OSEN) ?
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002282 RSN_AUTH_KEY_MGMT_UNSPEC_802_1X :
2283 WPA_AUTH_KEY_MGMT_UNSPEC_802_1X);
2284 case WPA_KEY_MGMT_PSK:
2285 return (sm->proto == WPA_PROTO_RSN ?
2286 RSN_AUTH_KEY_MGMT_PSK_OVER_802_1X :
2287 WPA_AUTH_KEY_MGMT_PSK_OVER_802_1X);
2288#ifdef CONFIG_IEEE80211R
2289 case WPA_KEY_MGMT_FT_IEEE8021X:
2290 return RSN_AUTH_KEY_MGMT_FT_802_1X;
2291 case WPA_KEY_MGMT_FT_PSK:
2292 return RSN_AUTH_KEY_MGMT_FT_PSK;
2293#endif /* CONFIG_IEEE80211R */
2294#ifdef CONFIG_IEEE80211W
2295 case WPA_KEY_MGMT_IEEE8021X_SHA256:
2296 return RSN_AUTH_KEY_MGMT_802_1X_SHA256;
2297 case WPA_KEY_MGMT_PSK_SHA256:
2298 return RSN_AUTH_KEY_MGMT_PSK_SHA256;
2299#endif /* CONFIG_IEEE80211W */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002300 case WPA_KEY_MGMT_CCKM:
2301 return (sm->proto == WPA_PROTO_RSN ?
2302 RSN_AUTH_KEY_MGMT_CCKM:
2303 WPA_AUTH_KEY_MGMT_CCKM);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002304 case WPA_KEY_MGMT_WPA_NONE:
2305 return WPA_AUTH_KEY_MGMT_NONE;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002306 case WPA_KEY_MGMT_IEEE8021X_SUITE_B:
2307 return RSN_AUTH_KEY_MGMT_802_1X_SUITE_B;
Dmitry Shmidt807291d2015-01-27 13:40:23 -08002308 case WPA_KEY_MGMT_IEEE8021X_SUITE_B_192:
2309 return RSN_AUTH_KEY_MGMT_802_1X_SUITE_B_192;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002310 default:
2311 return 0;
2312 }
2313}
2314
2315
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002316#define RSN_SUITE "%02x-%02x-%02x-%d"
2317#define RSN_SUITE_ARG(s) \
2318((s) >> 24) & 0xff, ((s) >> 16) & 0xff, ((s) >> 8) & 0xff, (s) & 0xff
2319
2320/**
2321 * wpa_sm_get_mib - Dump text list of MIB entries
2322 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2323 * @buf: Buffer for the list
2324 * @buflen: Length of the buffer
2325 * Returns: Number of bytes written to buffer
2326 *
2327 * This function is used fetch dot11 MIB variables.
2328 */
2329int wpa_sm_get_mib(struct wpa_sm *sm, char *buf, size_t buflen)
2330{
2331 char pmkid_txt[PMKID_LEN * 2 + 1];
2332 int rsna, ret;
2333 size_t len;
2334
2335 if (sm->cur_pmksa) {
2336 wpa_snprintf_hex(pmkid_txt, sizeof(pmkid_txt),
2337 sm->cur_pmksa->pmkid, PMKID_LEN);
2338 } else
2339 pmkid_txt[0] = '\0';
2340
2341 if ((wpa_key_mgmt_wpa_psk(sm->key_mgmt) ||
2342 wpa_key_mgmt_wpa_ieee8021x(sm->key_mgmt)) &&
2343 sm->proto == WPA_PROTO_RSN)
2344 rsna = 1;
2345 else
2346 rsna = 0;
2347
2348 ret = os_snprintf(buf, buflen,
2349 "dot11RSNAOptionImplemented=TRUE\n"
2350 "dot11RSNAPreauthenticationImplemented=TRUE\n"
2351 "dot11RSNAEnabled=%s\n"
2352 "dot11RSNAPreauthenticationEnabled=%s\n"
2353 "dot11RSNAConfigVersion=%d\n"
2354 "dot11RSNAConfigPairwiseKeysSupported=5\n"
2355 "dot11RSNAConfigGroupCipherSize=%d\n"
2356 "dot11RSNAConfigPMKLifetime=%d\n"
2357 "dot11RSNAConfigPMKReauthThreshold=%d\n"
2358 "dot11RSNAConfigNumberOfPTKSAReplayCounters=1\n"
2359 "dot11RSNAConfigSATimeout=%d\n",
2360 rsna ? "TRUE" : "FALSE",
2361 rsna ? "TRUE" : "FALSE",
2362 RSN_VERSION,
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07002363 wpa_cipher_key_len(sm->group_cipher) * 8,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002364 sm->dot11RSNAConfigPMKLifetime,
2365 sm->dot11RSNAConfigPMKReauthThreshold,
2366 sm->dot11RSNAConfigSATimeout);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002367 if (os_snprintf_error(buflen, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002368 return 0;
2369 len = ret;
2370
2371 ret = os_snprintf(
2372 buf + len, buflen - len,
2373 "dot11RSNAAuthenticationSuiteSelected=" RSN_SUITE "\n"
2374 "dot11RSNAPairwiseCipherSelected=" RSN_SUITE "\n"
2375 "dot11RSNAGroupCipherSelected=" RSN_SUITE "\n"
2376 "dot11RSNAPMKIDUsed=%s\n"
2377 "dot11RSNAAuthenticationSuiteRequested=" RSN_SUITE "\n"
2378 "dot11RSNAPairwiseCipherRequested=" RSN_SUITE "\n"
2379 "dot11RSNAGroupCipherRequested=" RSN_SUITE "\n"
2380 "dot11RSNAConfigNumberOfGTKSAReplayCounters=0\n"
2381 "dot11RSNA4WayHandshakeFailures=%u\n",
2382 RSN_SUITE_ARG(wpa_key_mgmt_suite(sm)),
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07002383 RSN_SUITE_ARG(wpa_cipher_to_suite(sm->proto,
2384 sm->pairwise_cipher)),
2385 RSN_SUITE_ARG(wpa_cipher_to_suite(sm->proto,
2386 sm->group_cipher)),
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002387 pmkid_txt,
2388 RSN_SUITE_ARG(wpa_key_mgmt_suite(sm)),
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07002389 RSN_SUITE_ARG(wpa_cipher_to_suite(sm->proto,
2390 sm->pairwise_cipher)),
2391 RSN_SUITE_ARG(wpa_cipher_to_suite(sm->proto,
2392 sm->group_cipher)),
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002393 sm->dot11RSNA4WayHandshakeFailures);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002394 if (!os_snprintf_error(buflen - len, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002395 len += ret;
2396
2397 return (int) len;
2398}
2399#endif /* CONFIG_CTRL_IFACE */
2400
2401
2402static void wpa_sm_pmksa_free_cb(struct rsn_pmksa_cache_entry *entry,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002403 void *ctx, enum pmksa_free_reason reason)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002404{
2405 struct wpa_sm *sm = ctx;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002406 int deauth = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002407
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002408 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "RSN: PMKSA cache entry free_cb: "
2409 MACSTR " reason=%d", MAC2STR(entry->aa), reason);
2410
2411 if (sm->cur_pmksa == entry) {
2412 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2413 "RSN: %s current PMKSA entry",
2414 reason == PMKSA_REPLACE ? "replaced" : "removed");
2415 pmksa_cache_clear_current(sm);
2416
2417 /*
2418 * If an entry is simply being replaced, there's no need to
2419 * deauthenticate because it will be immediately re-added.
2420 * This happens when EAP authentication is completed again
2421 * (reauth or failed PMKSA caching attempt).
2422 */
2423 if (reason != PMKSA_REPLACE)
2424 deauth = 1;
2425 }
2426
2427 if (reason == PMKSA_EXPIRE &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002428 (sm->pmk_len == entry->pmk_len &&
2429 os_memcmp(sm->pmk, entry->pmk, sm->pmk_len) == 0)) {
2430 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002431 "RSN: deauthenticating due to expired PMK");
2432 pmksa_cache_clear_current(sm);
2433 deauth = 1;
2434 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002435
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002436 if (deauth) {
Roshan Pius3a1667e2018-07-03 15:17:14 -07002437 sm->pmk_len = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002438 os_memset(sm->pmk, 0, sizeof(sm->pmk));
2439 wpa_sm_deauthenticate(sm, WLAN_REASON_UNSPECIFIED);
2440 }
2441}
2442
2443
2444/**
2445 * wpa_sm_init - Initialize WPA state machine
2446 * @ctx: Context pointer for callbacks; this needs to be an allocated buffer
2447 * Returns: Pointer to the allocated WPA state machine data
2448 *
2449 * This function is used to allocate a new WPA state machine and the returned
2450 * value is passed to all WPA state machine calls.
2451 */
2452struct wpa_sm * wpa_sm_init(struct wpa_sm_ctx *ctx)
2453{
2454 struct wpa_sm *sm;
2455
2456 sm = os_zalloc(sizeof(*sm));
2457 if (sm == NULL)
2458 return NULL;
2459 dl_list_init(&sm->pmksa_candidates);
2460 sm->renew_snonce = 1;
2461 sm->ctx = ctx;
2462
2463 sm->dot11RSNAConfigPMKLifetime = 43200;
2464 sm->dot11RSNAConfigPMKReauthThreshold = 70;
2465 sm->dot11RSNAConfigSATimeout = 60;
2466
2467 sm->pmksa = pmksa_cache_init(wpa_sm_pmksa_free_cb, sm, sm);
2468 if (sm->pmksa == NULL) {
2469 wpa_msg(sm->ctx->msg_ctx, MSG_ERROR,
2470 "RSN: PMKSA cache initialization failed");
2471 os_free(sm);
2472 return NULL;
2473 }
2474
2475 return sm;
2476}
2477
2478
2479/**
2480 * wpa_sm_deinit - Deinitialize WPA state machine
2481 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2482 */
2483void wpa_sm_deinit(struct wpa_sm *sm)
2484{
2485 if (sm == NULL)
2486 return;
2487 pmksa_cache_deinit(sm->pmksa);
2488 eloop_cancel_timeout(wpa_sm_start_preauth, sm, NULL);
2489 eloop_cancel_timeout(wpa_sm_rekey_ptk, sm, NULL);
2490 os_free(sm->assoc_wpa_ie);
2491 os_free(sm->ap_wpa_ie);
2492 os_free(sm->ap_rsn_ie);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002493 wpa_sm_drop_sa(sm);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002494 os_free(sm->ctx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002495#ifdef CONFIG_IEEE80211R
2496 os_free(sm->assoc_resp_ies);
2497#endif /* CONFIG_IEEE80211R */
Dmitry Shmidt55840ad2015-12-14 12:45:46 -08002498#ifdef CONFIG_TESTING_OPTIONS
2499 wpabuf_free(sm->test_assoc_ie);
2500#endif /* CONFIG_TESTING_OPTIONS */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002501#ifdef CONFIG_FILS_SK_PFS
2502 crypto_ecdh_deinit(sm->fils_ecdh);
2503#endif /* CONFIG_FILS_SK_PFS */
2504#ifdef CONFIG_FILS
2505 wpabuf_free(sm->fils_ft_ies);
2506#endif /* CONFIG_FILS */
2507#ifdef CONFIG_OWE
2508 crypto_ecdh_deinit(sm->owe_ecdh);
2509#endif /* CONFIG_OWE */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002510 os_free(sm);
2511}
2512
2513
2514/**
2515 * wpa_sm_notify_assoc - Notify WPA state machine about association
2516 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2517 * @bssid: The BSSID of the new association
2518 *
2519 * This function is called to let WPA state machine know that the connection
2520 * was established.
2521 */
2522void wpa_sm_notify_assoc(struct wpa_sm *sm, const u8 *bssid)
2523{
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02002524 int clear_keys = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002525
2526 if (sm == NULL)
2527 return;
2528
2529 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
2530 "WPA: Association event - clear replay counter");
2531 os_memcpy(sm->bssid, bssid, ETH_ALEN);
2532 os_memset(sm->rx_replay_counter, 0, WPA_REPLAY_COUNTER_LEN);
2533 sm->rx_replay_counter_set = 0;
2534 sm->renew_snonce = 1;
2535 if (os_memcmp(sm->preauth_bssid, bssid, ETH_ALEN) == 0)
2536 rsn_preauth_deinit(sm);
2537
2538#ifdef CONFIG_IEEE80211R
2539 if (wpa_ft_is_completed(sm)) {
2540 /*
2541 * Clear portValid to kick EAPOL state machine to re-enter
2542 * AUTHENTICATED state to get the EAPOL port Authorized.
2543 */
2544 eapol_sm_notify_portValid(sm->eapol, FALSE);
2545 wpa_supplicant_key_neg_complete(sm, sm->bssid, 1);
2546
2547 /* Prepare for the next transition */
2548 wpa_ft_prepare_auth_request(sm, NULL);
2549
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02002550 clear_keys = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002551 }
2552#endif /* CONFIG_IEEE80211R */
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002553#ifdef CONFIG_FILS
2554 if (sm->fils_completed) {
2555 /*
2556 * Clear portValid to kick EAPOL state machine to re-enter
2557 * AUTHENTICATED state to get the EAPOL port Authorized.
2558 */
2559 wpa_supplicant_key_neg_complete(sm, sm->bssid, 1);
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02002560 clear_keys = 0;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002561 }
2562#endif /* CONFIG_FILS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002563
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02002564 if (clear_keys) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002565 /*
2566 * IEEE 802.11, 8.4.10: Delete PTK SA on (re)association if
2567 * this is not part of a Fast BSS Transition.
2568 */
2569 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Clear old PTK");
2570 sm->ptk_set = 0;
Dmitry Shmidt61593f02014-04-21 16:27:35 -07002571 os_memset(&sm->ptk, 0, sizeof(sm->ptk));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002572 sm->tptk_set = 0;
Dmitry Shmidt61593f02014-04-21 16:27:35 -07002573 os_memset(&sm->tptk, 0, sizeof(sm->tptk));
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02002574 os_memset(&sm->gtk, 0, sizeof(sm->gtk));
Jouni Malinen58c0e962017-10-01 12:12:24 +03002575 os_memset(&sm->gtk_wnm_sleep, 0, sizeof(sm->gtk_wnm_sleep));
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02002576#ifdef CONFIG_IEEE80211W
2577 os_memset(&sm->igtk, 0, sizeof(sm->igtk));
Jouni Malinen58c0e962017-10-01 12:12:24 +03002578 os_memset(&sm->igtk_wnm_sleep, 0, sizeof(sm->igtk_wnm_sleep));
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02002579#endif /* CONFIG_IEEE80211W */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002580 }
2581
2582#ifdef CONFIG_TDLS
2583 wpa_tdls_assoc(sm);
2584#endif /* CONFIG_TDLS */
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002585
2586#ifdef CONFIG_P2P
2587 os_memset(sm->p2p_ip_addr, 0, sizeof(sm->p2p_ip_addr));
2588#endif /* CONFIG_P2P */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002589}
2590
2591
2592/**
2593 * wpa_sm_notify_disassoc - Notify WPA state machine about disassociation
2594 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2595 *
2596 * This function is called to let WPA state machine know that the connection
2597 * was lost. This will abort any existing pre-authentication session.
2598 */
2599void wpa_sm_notify_disassoc(struct wpa_sm *sm)
2600{
Dmitry Shmidt4dd28dc2015-03-10 11:21:43 -07002601 eloop_cancel_timeout(wpa_sm_start_preauth, sm, NULL);
2602 eloop_cancel_timeout(wpa_sm_rekey_ptk, sm, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002603 rsn_preauth_deinit(sm);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002604 pmksa_cache_clear_current(sm);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002605 if (wpa_sm_get_state(sm) == WPA_4WAY_HANDSHAKE)
2606 sm->dot11RSNA4WayHandshakeFailures++;
2607#ifdef CONFIG_TDLS
2608 wpa_tdls_disassoc(sm);
2609#endif /* CONFIG_TDLS */
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002610#ifdef CONFIG_FILS
2611 sm->fils_completed = 0;
2612#endif /* CONFIG_FILS */
Jouni Malinen4283f9e2017-09-22 12:06:37 +03002613#ifdef CONFIG_IEEE80211R
2614 sm->ft_reassoc_completed = 0;
2615#endif /* CONFIG_IEEE80211R */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002616
2617 /* Keys are not needed in the WPA state machine anymore */
2618 wpa_sm_drop_sa(sm);
Dmitry Shmidt4dd28dc2015-03-10 11:21:43 -07002619
2620 sm->msg_3_of_4_ok = 0;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002621 os_memset(sm->bssid, 0, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002622}
2623
2624
2625/**
2626 * wpa_sm_set_pmk - Set PMK
2627 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2628 * @pmk: The new PMK
2629 * @pmk_len: The length of the new PMK in bytes
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08002630 * @pmkid: Calculated PMKID
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002631 * @bssid: AA to add into PMKSA cache or %NULL to not cache the PMK
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002632 *
2633 * Configure the PMK for WPA state machine.
2634 */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002635void wpa_sm_set_pmk(struct wpa_sm *sm, const u8 *pmk, size_t pmk_len,
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08002636 const u8 *pmkid, const u8 *bssid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002637{
2638 if (sm == NULL)
2639 return;
2640
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002641 wpa_hexdump_key(MSG_DEBUG, "WPA: Set PMK based on external data",
2642 pmk, pmk_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002643 sm->pmk_len = pmk_len;
2644 os_memcpy(sm->pmk, pmk, pmk_len);
2645
2646#ifdef CONFIG_IEEE80211R
2647 /* Set XXKey to be PSK for FT key derivation */
2648 sm->xxkey_len = pmk_len;
2649 os_memcpy(sm->xxkey, pmk, pmk_len);
2650#endif /* CONFIG_IEEE80211R */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002651
2652 if (bssid) {
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08002653 pmksa_cache_add(sm->pmksa, pmk, pmk_len, pmkid, NULL, 0,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002654 bssid, sm->own_addr,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002655 sm->network_ctx, sm->key_mgmt, NULL);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002656 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002657}
2658
2659
2660/**
2661 * wpa_sm_set_pmk_from_pmksa - Set PMK based on the current PMKSA
2662 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2663 *
2664 * Take the PMK from the current PMKSA into use. If no PMKSA is active, the PMK
2665 * will be cleared.
2666 */
2667void wpa_sm_set_pmk_from_pmksa(struct wpa_sm *sm)
2668{
2669 if (sm == NULL)
2670 return;
2671
2672 if (sm->cur_pmksa) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002673 wpa_hexdump_key(MSG_DEBUG,
2674 "WPA: Set PMK based on current PMKSA",
2675 sm->cur_pmksa->pmk, sm->cur_pmksa->pmk_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002676 sm->pmk_len = sm->cur_pmksa->pmk_len;
2677 os_memcpy(sm->pmk, sm->cur_pmksa->pmk, sm->pmk_len);
2678 } else {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002679 wpa_printf(MSG_DEBUG, "WPA: No current PMKSA - clear PMK");
2680 sm->pmk_len = 0;
2681 os_memset(sm->pmk, 0, PMK_LEN_MAX);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002682 }
2683}
2684
2685
2686/**
2687 * wpa_sm_set_fast_reauth - Set fast reauthentication (EAP) enabled/disabled
2688 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2689 * @fast_reauth: Whether fast reauthentication (EAP) is allowed
2690 */
2691void wpa_sm_set_fast_reauth(struct wpa_sm *sm, int fast_reauth)
2692{
2693 if (sm)
2694 sm->fast_reauth = fast_reauth;
2695}
2696
2697
2698/**
2699 * wpa_sm_set_scard_ctx - Set context pointer for smartcard callbacks
2700 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2701 * @scard_ctx: Context pointer for smartcard related callback functions
2702 */
2703void wpa_sm_set_scard_ctx(struct wpa_sm *sm, void *scard_ctx)
2704{
2705 if (sm == NULL)
2706 return;
2707 sm->scard_ctx = scard_ctx;
2708 if (sm->preauth_eapol)
2709 eapol_sm_register_scard_ctx(sm->preauth_eapol, scard_ctx);
2710}
2711
2712
2713/**
2714 * wpa_sm_set_config - Notification of current configration change
2715 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2716 * @config: Pointer to current network configuration
2717 *
2718 * Notify WPA state machine that configuration has changed. config will be
2719 * stored as a backpointer to network configuration. This can be %NULL to clear
2720 * the stored pointed.
2721 */
2722void wpa_sm_set_config(struct wpa_sm *sm, struct rsn_supp_config *config)
2723{
2724 if (!sm)
2725 return;
2726
2727 if (config) {
2728 sm->network_ctx = config->network_ctx;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002729 sm->allowed_pairwise_cipher = config->allowed_pairwise_cipher;
2730 sm->proactive_key_caching = config->proactive_key_caching;
2731 sm->eap_workaround = config->eap_workaround;
2732 sm->eap_conf_ctx = config->eap_conf_ctx;
2733 if (config->ssid) {
2734 os_memcpy(sm->ssid, config->ssid, config->ssid_len);
2735 sm->ssid_len = config->ssid_len;
2736 } else
2737 sm->ssid_len = 0;
2738 sm->wpa_ptk_rekey = config->wpa_ptk_rekey;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002739 sm->p2p = config->p2p;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002740 sm->wpa_rsc_relaxation = config->wpa_rsc_relaxation;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002741#ifdef CONFIG_FILS
2742 if (config->fils_cache_id) {
2743 sm->fils_cache_id_set = 1;
2744 os_memcpy(sm->fils_cache_id, config->fils_cache_id,
2745 FILS_CACHE_ID_LEN);
2746 } else {
2747 sm->fils_cache_id_set = 0;
2748 }
2749#endif /* CONFIG_FILS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002750 } else {
2751 sm->network_ctx = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002752 sm->allowed_pairwise_cipher = 0;
2753 sm->proactive_key_caching = 0;
2754 sm->eap_workaround = 0;
2755 sm->eap_conf_ctx = NULL;
2756 sm->ssid_len = 0;
2757 sm->wpa_ptk_rekey = 0;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002758 sm->p2p = 0;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002759 sm->wpa_rsc_relaxation = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002760 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002761}
2762
2763
2764/**
2765 * wpa_sm_set_own_addr - Set own MAC address
2766 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2767 * @addr: Own MAC address
2768 */
2769void wpa_sm_set_own_addr(struct wpa_sm *sm, const u8 *addr)
2770{
2771 if (sm)
2772 os_memcpy(sm->own_addr, addr, ETH_ALEN);
2773}
2774
2775
2776/**
2777 * wpa_sm_set_ifname - Set network interface name
2778 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2779 * @ifname: Interface name
2780 * @bridge_ifname: Optional bridge interface name (for pre-auth)
2781 */
2782void wpa_sm_set_ifname(struct wpa_sm *sm, const char *ifname,
2783 const char *bridge_ifname)
2784{
2785 if (sm) {
2786 sm->ifname = ifname;
2787 sm->bridge_ifname = bridge_ifname;
2788 }
2789}
2790
2791
2792/**
2793 * wpa_sm_set_eapol - Set EAPOL state machine pointer
2794 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2795 * @eapol: Pointer to EAPOL state machine allocated with eapol_sm_init()
2796 */
2797void wpa_sm_set_eapol(struct wpa_sm *sm, struct eapol_sm *eapol)
2798{
2799 if (sm)
2800 sm->eapol = eapol;
2801}
2802
2803
2804/**
2805 * wpa_sm_set_param - Set WPA state machine parameters
2806 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2807 * @param: Parameter field
2808 * @value: Parameter value
2809 * Returns: 0 on success, -1 on failure
2810 */
2811int wpa_sm_set_param(struct wpa_sm *sm, enum wpa_sm_conf_params param,
2812 unsigned int value)
2813{
2814 int ret = 0;
2815
2816 if (sm == NULL)
2817 return -1;
2818
2819 switch (param) {
2820 case RSNA_PMK_LIFETIME:
2821 if (value > 0)
2822 sm->dot11RSNAConfigPMKLifetime = value;
2823 else
2824 ret = -1;
2825 break;
2826 case RSNA_PMK_REAUTH_THRESHOLD:
2827 if (value > 0 && value <= 100)
2828 sm->dot11RSNAConfigPMKReauthThreshold = value;
2829 else
2830 ret = -1;
2831 break;
2832 case RSNA_SA_TIMEOUT:
2833 if (value > 0)
2834 sm->dot11RSNAConfigSATimeout = value;
2835 else
2836 ret = -1;
2837 break;
2838 case WPA_PARAM_PROTO:
2839 sm->proto = value;
2840 break;
2841 case WPA_PARAM_PAIRWISE:
2842 sm->pairwise_cipher = value;
2843 break;
2844 case WPA_PARAM_GROUP:
2845 sm->group_cipher = value;
2846 break;
2847 case WPA_PARAM_KEY_MGMT:
2848 sm->key_mgmt = value;
2849 break;
2850#ifdef CONFIG_IEEE80211W
2851 case WPA_PARAM_MGMT_GROUP:
2852 sm->mgmt_group_cipher = value;
2853 break;
2854#endif /* CONFIG_IEEE80211W */
2855 case WPA_PARAM_RSN_ENABLED:
2856 sm->rsn_enabled = value;
2857 break;
2858 case WPA_PARAM_MFP:
2859 sm->mfp = value;
2860 break;
2861 default:
2862 break;
2863 }
2864
2865 return ret;
2866}
2867
2868
2869/**
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002870 * wpa_sm_get_status - Get WPA state machine
2871 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2872 * @buf: Buffer for status information
2873 * @buflen: Maximum buffer length
2874 * @verbose: Whether to include verbose status information
2875 * Returns: Number of bytes written to buf.
2876 *
2877 * Query WPA state machine for status information. This function fills in
2878 * a text area with current status information. If the buffer (buf) is not
2879 * large enough, status information will be truncated to fit the buffer.
2880 */
2881int wpa_sm_get_status(struct wpa_sm *sm, char *buf, size_t buflen,
2882 int verbose)
2883{
2884 char *pos = buf, *end = buf + buflen;
2885 int ret;
2886
2887 ret = os_snprintf(pos, end - pos,
2888 "pairwise_cipher=%s\n"
2889 "group_cipher=%s\n"
2890 "key_mgmt=%s\n",
2891 wpa_cipher_txt(sm->pairwise_cipher),
2892 wpa_cipher_txt(sm->group_cipher),
2893 wpa_key_mgmt_txt(sm->key_mgmt, sm->proto));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002894 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002895 return pos - buf;
2896 pos += ret;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002897
2898 if (sm->mfp != NO_MGMT_FRAME_PROTECTION && sm->ap_rsn_ie) {
2899 struct wpa_ie_data rsn;
2900 if (wpa_parse_wpa_ie_rsn(sm->ap_rsn_ie, sm->ap_rsn_ie_len, &rsn)
2901 >= 0 &&
2902 rsn.capabilities & (WPA_CAPABILITY_MFPR |
2903 WPA_CAPABILITY_MFPC)) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002904 ret = os_snprintf(pos, end - pos, "pmf=%d\n"
2905 "mgmt_group_cipher=%s\n",
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002906 (rsn.capabilities &
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002907 WPA_CAPABILITY_MFPR) ? 2 : 1,
2908 wpa_cipher_txt(
2909 sm->mgmt_group_cipher));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002910 if (os_snprintf_error(end - pos, ret))
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002911 return pos - buf;
2912 pos += ret;
2913 }
2914 }
2915
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002916 return pos - buf;
2917}
2918
2919
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07002920int wpa_sm_pmf_enabled(struct wpa_sm *sm)
2921{
2922 struct wpa_ie_data rsn;
2923
2924 if (sm->mfp == NO_MGMT_FRAME_PROTECTION || !sm->ap_rsn_ie)
2925 return 0;
2926
2927 if (wpa_parse_wpa_ie_rsn(sm->ap_rsn_ie, sm->ap_rsn_ie_len, &rsn) >= 0 &&
2928 rsn.capabilities & (WPA_CAPABILITY_MFPR | WPA_CAPABILITY_MFPC))
2929 return 1;
2930
2931 return 0;
2932}
2933
2934
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002935/**
2936 * wpa_sm_set_assoc_wpa_ie_default - Generate own WPA/RSN IE from configuration
2937 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2938 * @wpa_ie: Pointer to buffer for WPA/RSN IE
2939 * @wpa_ie_len: Pointer to the length of the wpa_ie buffer
2940 * Returns: 0 on success, -1 on failure
2941 */
2942int wpa_sm_set_assoc_wpa_ie_default(struct wpa_sm *sm, u8 *wpa_ie,
2943 size_t *wpa_ie_len)
2944{
2945 int res;
2946
2947 if (sm == NULL)
2948 return -1;
2949
Dmitry Shmidt55840ad2015-12-14 12:45:46 -08002950#ifdef CONFIG_TESTING_OPTIONS
2951 if (sm->test_assoc_ie) {
2952 wpa_printf(MSG_DEBUG,
2953 "TESTING: Replace association WPA/RSN IE");
2954 if (*wpa_ie_len < wpabuf_len(sm->test_assoc_ie))
2955 return -1;
2956 os_memcpy(wpa_ie, wpabuf_head(sm->test_assoc_ie),
2957 wpabuf_len(sm->test_assoc_ie));
2958 res = wpabuf_len(sm->test_assoc_ie);
2959 } else
2960#endif /* CONFIG_TESTING_OPTIONS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002961 res = wpa_gen_wpa_ie(sm, wpa_ie, *wpa_ie_len);
2962 if (res < 0)
2963 return -1;
2964 *wpa_ie_len = res;
2965
2966 wpa_hexdump(MSG_DEBUG, "WPA: Set own WPA IE default",
2967 wpa_ie, *wpa_ie_len);
2968
2969 if (sm->assoc_wpa_ie == NULL) {
2970 /*
2971 * Make a copy of the WPA/RSN IE so that 4-Way Handshake gets
2972 * the correct version of the IE even if PMKSA caching is
2973 * aborted (which would remove PMKID from IE generation).
2974 */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002975 sm->assoc_wpa_ie = os_memdup(wpa_ie, *wpa_ie_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002976 if (sm->assoc_wpa_ie == NULL)
2977 return -1;
2978
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002979 sm->assoc_wpa_ie_len = *wpa_ie_len;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002980 } else {
2981 wpa_hexdump(MSG_DEBUG,
2982 "WPA: Leave previously set WPA IE default",
2983 sm->assoc_wpa_ie, sm->assoc_wpa_ie_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002984 }
2985
2986 return 0;
2987}
2988
2989
2990/**
2991 * wpa_sm_set_assoc_wpa_ie - Set own WPA/RSN IE from (Re)AssocReq
2992 * @sm: Pointer to WPA state machine data from wpa_sm_init()
2993 * @ie: Pointer to IE data (starting from id)
2994 * @len: IE length
2995 * Returns: 0 on success, -1 on failure
2996 *
2997 * Inform WPA state machine about the WPA/RSN IE used in (Re)Association
2998 * Request frame. The IE will be used to override the default value generated
2999 * with wpa_sm_set_assoc_wpa_ie_default().
3000 */
3001int wpa_sm_set_assoc_wpa_ie(struct wpa_sm *sm, const u8 *ie, size_t len)
3002{
3003 if (sm == NULL)
3004 return -1;
3005
3006 os_free(sm->assoc_wpa_ie);
3007 if (ie == NULL || len == 0) {
3008 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
3009 "WPA: clearing own WPA/RSN IE");
3010 sm->assoc_wpa_ie = NULL;
3011 sm->assoc_wpa_ie_len = 0;
3012 } else {
3013 wpa_hexdump(MSG_DEBUG, "WPA: set own WPA/RSN IE", ie, len);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003014 sm->assoc_wpa_ie = os_memdup(ie, len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003015 if (sm->assoc_wpa_ie == NULL)
3016 return -1;
3017
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003018 sm->assoc_wpa_ie_len = len;
3019 }
3020
3021 return 0;
3022}
3023
3024
3025/**
3026 * wpa_sm_set_ap_wpa_ie - Set AP WPA IE from Beacon/ProbeResp
3027 * @sm: Pointer to WPA state machine data from wpa_sm_init()
3028 * @ie: Pointer to IE data (starting from id)
3029 * @len: IE length
3030 * Returns: 0 on success, -1 on failure
3031 *
3032 * Inform WPA state machine about the WPA IE used in Beacon / Probe Response
3033 * frame.
3034 */
3035int wpa_sm_set_ap_wpa_ie(struct wpa_sm *sm, const u8 *ie, size_t len)
3036{
3037 if (sm == NULL)
3038 return -1;
3039
3040 os_free(sm->ap_wpa_ie);
3041 if (ie == NULL || len == 0) {
3042 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
3043 "WPA: clearing AP WPA IE");
3044 sm->ap_wpa_ie = NULL;
3045 sm->ap_wpa_ie_len = 0;
3046 } else {
3047 wpa_hexdump(MSG_DEBUG, "WPA: set AP WPA IE", ie, len);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003048 sm->ap_wpa_ie = os_memdup(ie, len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003049 if (sm->ap_wpa_ie == NULL)
3050 return -1;
3051
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003052 sm->ap_wpa_ie_len = len;
3053 }
3054
3055 return 0;
3056}
3057
3058
3059/**
3060 * wpa_sm_set_ap_rsn_ie - Set AP RSN IE from Beacon/ProbeResp
3061 * @sm: Pointer to WPA state machine data from wpa_sm_init()
3062 * @ie: Pointer to IE data (starting from id)
3063 * @len: IE length
3064 * Returns: 0 on success, -1 on failure
3065 *
3066 * Inform WPA state machine about the RSN IE used in Beacon / Probe Response
3067 * frame.
3068 */
3069int wpa_sm_set_ap_rsn_ie(struct wpa_sm *sm, const u8 *ie, size_t len)
3070{
3071 if (sm == NULL)
3072 return -1;
3073
3074 os_free(sm->ap_rsn_ie);
3075 if (ie == NULL || len == 0) {
3076 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
3077 "WPA: clearing AP RSN IE");
3078 sm->ap_rsn_ie = NULL;
3079 sm->ap_rsn_ie_len = 0;
3080 } else {
3081 wpa_hexdump(MSG_DEBUG, "WPA: set AP RSN IE", ie, len);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003082 sm->ap_rsn_ie = os_memdup(ie, len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003083 if (sm->ap_rsn_ie == NULL)
3084 return -1;
3085
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003086 sm->ap_rsn_ie_len = len;
3087 }
3088
3089 return 0;
3090}
3091
3092
3093/**
3094 * wpa_sm_parse_own_wpa_ie - Parse own WPA/RSN IE
3095 * @sm: Pointer to WPA state machine data from wpa_sm_init()
3096 * @data: Pointer to data area for parsing results
3097 * Returns: 0 on success, -1 if IE is not known, or -2 on parsing failure
3098 *
3099 * Parse the contents of the own WPA or RSN IE from (Re)AssocReq and write the
3100 * parsed data into data.
3101 */
3102int wpa_sm_parse_own_wpa_ie(struct wpa_sm *sm, struct wpa_ie_data *data)
3103{
3104 if (sm == NULL)
3105 return -1;
3106
3107 if (sm->assoc_wpa_ie == NULL) {
3108 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
3109 "WPA: No WPA/RSN IE available from association info");
3110 return -1;
3111 }
3112 if (wpa_parse_wpa_ie(sm->assoc_wpa_ie, sm->assoc_wpa_ie_len, data))
3113 return -2;
3114 return 0;
3115}
3116
3117
3118int wpa_sm_pmksa_cache_list(struct wpa_sm *sm, char *buf, size_t len)
3119{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003120 return pmksa_cache_list(sm->pmksa, buf, len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003121}
3122
3123
Dmitry Shmidt29333592017-01-09 12:27:11 -08003124struct rsn_pmksa_cache_entry * wpa_sm_pmksa_cache_head(struct wpa_sm *sm)
3125{
3126 return pmksa_cache_head(sm->pmksa);
3127}
3128
3129
3130struct rsn_pmksa_cache_entry *
3131wpa_sm_pmksa_cache_add_entry(struct wpa_sm *sm,
3132 struct rsn_pmksa_cache_entry * entry)
3133{
3134 return pmksa_cache_add_entry(sm->pmksa, entry);
3135}
3136
3137
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003138void wpa_sm_pmksa_cache_add(struct wpa_sm *sm, const u8 *pmk, size_t pmk_len,
3139 const u8 *pmkid, const u8 *bssid,
3140 const u8 *fils_cache_id)
3141{
3142 sm->cur_pmksa = pmksa_cache_add(sm->pmksa, pmk, pmk_len, pmkid, NULL, 0,
3143 bssid, sm->own_addr, sm->network_ctx,
3144 sm->key_mgmt, fils_cache_id);
3145}
3146
3147
3148int wpa_sm_pmksa_exists(struct wpa_sm *sm, const u8 *bssid,
3149 const void *network_ctx)
3150{
Roshan Pius3a1667e2018-07-03 15:17:14 -07003151 return pmksa_cache_get(sm->pmksa, bssid, NULL, network_ctx, 0) != NULL;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003152}
3153
3154
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003155void wpa_sm_drop_sa(struct wpa_sm *sm)
3156{
3157 wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Clear old PMK and PTK");
3158 sm->ptk_set = 0;
3159 sm->tptk_set = 0;
Roshan Pius3a1667e2018-07-03 15:17:14 -07003160 sm->pmk_len = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003161 os_memset(sm->pmk, 0, sizeof(sm->pmk));
3162 os_memset(&sm->ptk, 0, sizeof(sm->ptk));
3163 os_memset(&sm->tptk, 0, sizeof(sm->tptk));
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02003164 os_memset(&sm->gtk, 0, sizeof(sm->gtk));
Jouni Malinen58c0e962017-10-01 12:12:24 +03003165 os_memset(&sm->gtk_wnm_sleep, 0, sizeof(sm->gtk_wnm_sleep));
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02003166#ifdef CONFIG_IEEE80211W
3167 os_memset(&sm->igtk, 0, sizeof(sm->igtk));
Jouni Malinen58c0e962017-10-01 12:12:24 +03003168 os_memset(&sm->igtk_wnm_sleep, 0, sizeof(sm->igtk_wnm_sleep));
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02003169#endif /* CONFIG_IEEE80211W */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003170#ifdef CONFIG_IEEE80211R
3171 os_memset(sm->xxkey, 0, sizeof(sm->xxkey));
Roshan Pius3a1667e2018-07-03 15:17:14 -07003172 sm->xxkey_len = 0;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003173 os_memset(sm->pmk_r0, 0, sizeof(sm->pmk_r0));
Roshan Pius3a1667e2018-07-03 15:17:14 -07003174 sm->pmk_r0_len = 0;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003175 os_memset(sm->pmk_r1, 0, sizeof(sm->pmk_r1));
Roshan Pius3a1667e2018-07-03 15:17:14 -07003176 sm->pmk_r1_len = 0;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003177#endif /* CONFIG_IEEE80211R */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003178}
3179
3180
3181int wpa_sm_has_ptk(struct wpa_sm *sm)
3182{
3183 if (sm == NULL)
3184 return 0;
3185 return sm->ptk_set;
3186}
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003187
3188
3189void wpa_sm_update_replay_ctr(struct wpa_sm *sm, const u8 *replay_ctr)
3190{
3191 os_memcpy(sm->rx_replay_counter, replay_ctr, WPA_REPLAY_COUNTER_LEN);
3192}
3193
3194
3195void wpa_sm_pmksa_cache_flush(struct wpa_sm *sm, void *network_ctx)
3196{
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07003197 pmksa_cache_flush(sm->pmksa, network_ctx, NULL, 0);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003198}
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003199
3200
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003201#ifdef CONFIG_WNM
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003202int wpa_wnmsleep_install_key(struct wpa_sm *sm, u8 subelem_id, u8 *buf)
3203{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003204 u16 keyinfo;
3205 u8 keylen; /* plaintext key len */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003206 u8 *key_rsc;
3207
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003208 if (subelem_id == WNM_SLEEP_SUBELEM_GTK) {
Dmitry Shmidt61593f02014-04-21 16:27:35 -07003209 struct wpa_gtk_data gd;
3210
3211 os_memset(&gd, 0, sizeof(gd));
3212 keylen = wpa_cipher_key_len(sm->group_cipher);
3213 gd.key_rsc_len = wpa_cipher_rsc_len(sm->group_cipher);
3214 gd.alg = wpa_cipher_to_alg(sm->group_cipher);
3215 if (gd.alg == WPA_ALG_NONE) {
3216 wpa_printf(MSG_DEBUG, "Unsupported group cipher suite");
3217 return -1;
3218 }
3219
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003220 key_rsc = buf + 5;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003221 keyinfo = WPA_GET_LE16(buf + 2);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003222 gd.gtk_len = keylen;
3223 if (gd.gtk_len != buf[4]) {
3224 wpa_printf(MSG_DEBUG, "GTK len mismatch len %d vs %d",
3225 gd.gtk_len, buf[4]);
3226 return -1;
3227 }
3228 gd.keyidx = keyinfo & 0x03; /* B0 - B1 */
3229 gd.tx = wpa_supplicant_gtk_tx_bit_workaround(
3230 sm, !!(keyinfo & WPA_KEY_INFO_TXRX));
3231
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003232 os_memcpy(gd.gtk, buf + 13, gd.gtk_len);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003233
3234 wpa_hexdump_key(MSG_DEBUG, "Install GTK (WNM SLEEP)",
3235 gd.gtk, gd.gtk_len);
Jouni Malinen58c0e962017-10-01 12:12:24 +03003236 if (wpa_supplicant_install_gtk(sm, &gd, key_rsc, 1)) {
Dmitry Shmidt61593f02014-04-21 16:27:35 -07003237 os_memset(&gd, 0, sizeof(gd));
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003238 wpa_printf(MSG_DEBUG, "Failed to install the GTK in "
3239 "WNM mode");
3240 return -1;
3241 }
Dmitry Shmidt61593f02014-04-21 16:27:35 -07003242 os_memset(&gd, 0, sizeof(gd));
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003243#ifdef CONFIG_IEEE80211W
3244 } else if (subelem_id == WNM_SLEEP_SUBELEM_IGTK) {
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02003245 const struct wpa_igtk_kde *igtk;
Dmitry Shmidt61593f02014-04-21 16:27:35 -07003246
Mathy Vanhoef10bfd642017-07-12 16:03:24 +02003247 igtk = (const struct wpa_igtk_kde *) (buf + 2);
Jouni Malinen58c0e962017-10-01 12:12:24 +03003248 if (wpa_supplicant_install_igtk(sm, igtk, 1) < 0)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003249 return -1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003250#endif /* CONFIG_IEEE80211W */
3251 } else {
3252 wpa_printf(MSG_DEBUG, "Unknown element id");
3253 return -1;
3254 }
3255
3256 return 0;
3257}
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003258#endif /* CONFIG_WNM */
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003259
3260
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003261#ifdef CONFIG_P2P
3262
3263int wpa_sm_get_p2p_ip_addr(struct wpa_sm *sm, u8 *buf)
3264{
3265 if (sm == NULL || WPA_GET_BE32(sm->p2p_ip_addr) == 0)
3266 return -1;
3267 os_memcpy(buf, sm->p2p_ip_addr, 3 * 4);
3268 return 0;
3269}
3270
3271#endif /* CONFIG_P2P */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003272
3273
3274void wpa_sm_set_rx_replay_ctr(struct wpa_sm *sm, const u8 *rx_replay_counter)
3275{
3276 if (rx_replay_counter == NULL)
3277 return;
3278
3279 os_memcpy(sm->rx_replay_counter, rx_replay_counter,
3280 WPA_REPLAY_COUNTER_LEN);
3281 sm->rx_replay_counter_set = 1;
3282 wpa_printf(MSG_DEBUG, "Updated key replay counter");
3283}
3284
3285
Dmitry Shmidt807291d2015-01-27 13:40:23 -08003286void wpa_sm_set_ptk_kck_kek(struct wpa_sm *sm,
3287 const u8 *ptk_kck, size_t ptk_kck_len,
3288 const u8 *ptk_kek, size_t ptk_kek_len)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003289{
Dmitry Shmidt807291d2015-01-27 13:40:23 -08003290 if (ptk_kck && ptk_kck_len <= WPA_KCK_MAX_LEN) {
3291 os_memcpy(sm->ptk.kck, ptk_kck, ptk_kck_len);
3292 sm->ptk.kck_len = ptk_kck_len;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003293 wpa_printf(MSG_DEBUG, "Updated PTK KCK");
3294 }
Dmitry Shmidt807291d2015-01-27 13:40:23 -08003295 if (ptk_kek && ptk_kek_len <= WPA_KEK_MAX_LEN) {
3296 os_memcpy(sm->ptk.kek, ptk_kek, ptk_kek_len);
3297 sm->ptk.kek_len = ptk_kek_len;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003298 wpa_printf(MSG_DEBUG, "Updated PTK KEK");
3299 }
3300 sm->ptk_set = 1;
3301}
Dmitry Shmidt55840ad2015-12-14 12:45:46 -08003302
3303
3304#ifdef CONFIG_TESTING_OPTIONS
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003305
Dmitry Shmidt55840ad2015-12-14 12:45:46 -08003306void wpa_sm_set_test_assoc_ie(struct wpa_sm *sm, struct wpabuf *buf)
3307{
3308 wpabuf_free(sm->test_assoc_ie);
3309 sm->test_assoc_ie = buf;
3310}
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003311
3312
3313const u8 * wpa_sm_get_anonce(struct wpa_sm *sm)
3314{
3315 return sm->anonce;
3316}
3317
Dmitry Shmidt55840ad2015-12-14 12:45:46 -08003318#endif /* CONFIG_TESTING_OPTIONS */
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003319
3320
Roshan Pius3a1667e2018-07-03 15:17:14 -07003321unsigned int wpa_sm_get_key_mgmt(struct wpa_sm *sm)
3322{
3323 return sm->key_mgmt;
3324}
3325
3326
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003327#ifdef CONFIG_FILS
3328
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003329struct wpabuf * fils_build_auth(struct wpa_sm *sm, int dh_group, const u8 *md)
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003330{
3331 struct wpabuf *buf = NULL;
3332 struct wpabuf *erp_msg;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003333 struct wpabuf *pub = NULL;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003334
3335 erp_msg = eapol_sm_build_erp_reauth_start(sm->eapol);
3336 if (!erp_msg && !sm->cur_pmksa) {
3337 wpa_printf(MSG_DEBUG,
3338 "FILS: Neither ERP EAP-Initiate/Re-auth nor PMKSA cache entry is available - skip FILS");
3339 goto fail;
3340 }
3341
3342 wpa_printf(MSG_DEBUG, "FILS: Try to use FILS (erp=%d pmksa_cache=%d)",
3343 erp_msg != NULL, sm->cur_pmksa != NULL);
3344
3345 sm->fils_completed = 0;
3346
3347 if (!sm->assoc_wpa_ie) {
3348 wpa_printf(MSG_INFO, "FILS: No own RSN IE set for FILS");
3349 goto fail;
3350 }
3351
3352 if (random_get_bytes(sm->fils_nonce, FILS_NONCE_LEN) < 0 ||
3353 random_get_bytes(sm->fils_session, FILS_SESSION_LEN) < 0)
3354 goto fail;
3355
3356 wpa_hexdump(MSG_DEBUG, "FILS: Generated FILS Nonce",
3357 sm->fils_nonce, FILS_NONCE_LEN);
3358 wpa_hexdump(MSG_DEBUG, "FILS: Generated FILS Session",
3359 sm->fils_session, FILS_SESSION_LEN);
3360
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003361#ifdef CONFIG_FILS_SK_PFS
3362 sm->fils_dh_group = dh_group;
3363 if (dh_group) {
3364 crypto_ecdh_deinit(sm->fils_ecdh);
3365 sm->fils_ecdh = crypto_ecdh_init(dh_group);
3366 if (!sm->fils_ecdh) {
3367 wpa_printf(MSG_INFO,
3368 "FILS: Could not initialize ECDH with group %d",
3369 dh_group);
3370 goto fail;
3371 }
3372 pub = crypto_ecdh_get_pubkey(sm->fils_ecdh, 1);
3373 if (!pub)
3374 goto fail;
3375 wpa_hexdump_buf(MSG_DEBUG, "FILS: Element (DH public key)",
3376 pub);
3377 sm->fils_dh_elem_len = wpabuf_len(pub);
3378 }
3379#endif /* CONFIG_FILS_SK_PFS */
3380
3381 buf = wpabuf_alloc(1000 + sm->assoc_wpa_ie_len +
3382 (pub ? wpabuf_len(pub) : 0));
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003383 if (!buf)
3384 goto fail;
3385
3386 /* Fields following the Authentication algorithm number field */
3387
3388 /* Authentication Transaction seq# */
3389 wpabuf_put_le16(buf, 1);
3390
3391 /* Status Code */
3392 wpabuf_put_le16(buf, WLAN_STATUS_SUCCESS);
3393
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003394 /* TODO: FILS PK */
3395#ifdef CONFIG_FILS_SK_PFS
3396 if (dh_group) {
3397 /* Finite Cyclic Group */
3398 wpabuf_put_le16(buf, dh_group);
3399 /* Element */
3400 wpabuf_put_buf(buf, pub);
3401 }
3402#endif /* CONFIG_FILS_SK_PFS */
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003403
3404 /* RSNE */
3405 wpa_hexdump(MSG_DEBUG, "FILS: RSNE in FILS Authentication frame",
3406 sm->assoc_wpa_ie, sm->assoc_wpa_ie_len);
3407 wpabuf_put_data(buf, sm->assoc_wpa_ie, sm->assoc_wpa_ie_len);
3408
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003409 if (md) {
3410 /* MDE when using FILS for FT initial association */
3411 struct rsn_mdie *mdie;
3412
3413 wpabuf_put_u8(buf, WLAN_EID_MOBILITY_DOMAIN);
3414 wpabuf_put_u8(buf, sizeof(*mdie));
3415 mdie = wpabuf_put(buf, sizeof(*mdie));
3416 os_memcpy(mdie->mobility_domain, md, MOBILITY_DOMAIN_ID_LEN);
3417 mdie->ft_capab = 0;
3418 }
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003419
3420 /* FILS Nonce */
3421 wpabuf_put_u8(buf, WLAN_EID_EXTENSION); /* Element ID */
3422 wpabuf_put_u8(buf, 1 + FILS_NONCE_LEN); /* Length */
3423 /* Element ID Extension */
3424 wpabuf_put_u8(buf, WLAN_EID_EXT_FILS_NONCE);
3425 wpabuf_put_data(buf, sm->fils_nonce, FILS_NONCE_LEN);
3426
3427 /* FILS Session */
3428 wpabuf_put_u8(buf, WLAN_EID_EXTENSION); /* Element ID */
3429 wpabuf_put_u8(buf, 1 + FILS_SESSION_LEN); /* Length */
3430 /* Element ID Extension */
3431 wpabuf_put_u8(buf, WLAN_EID_EXT_FILS_SESSION);
3432 wpabuf_put_data(buf, sm->fils_session, FILS_SESSION_LEN);
3433
3434 /* FILS Wrapped Data */
Paul Stewart092955c2017-02-06 09:13:09 -08003435 sm->fils_erp_pmkid_set = 0;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003436 if (erp_msg) {
3437 wpabuf_put_u8(buf, WLAN_EID_EXTENSION); /* Element ID */
3438 wpabuf_put_u8(buf, 1 + wpabuf_len(erp_msg)); /* Length */
3439 /* Element ID Extension */
3440 wpabuf_put_u8(buf, WLAN_EID_EXT_FILS_WRAPPED_DATA);
3441 wpabuf_put_buf(buf, erp_msg);
Paul Stewart092955c2017-02-06 09:13:09 -08003442 /* Calculate pending PMKID here so that we do not need to
3443 * maintain a copy of the EAP-Initiate/Reauth message. */
3444 if (fils_pmkid_erp(sm->key_mgmt, wpabuf_head(erp_msg),
3445 wpabuf_len(erp_msg),
3446 sm->fils_erp_pmkid) == 0)
3447 sm->fils_erp_pmkid_set = 1;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003448 }
3449
3450 wpa_hexdump_buf(MSG_DEBUG, "RSN: FILS fields for Authentication frame",
3451 buf);
3452
3453fail:
3454 wpabuf_free(erp_msg);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003455 wpabuf_free(pub);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003456 return buf;
3457}
3458
3459
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003460int fils_process_auth(struct wpa_sm *sm, const u8 *bssid, const u8 *data,
3461 size_t len)
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003462{
3463 const u8 *pos, *end;
3464 struct ieee802_11_elems elems;
3465 struct wpa_ie_data rsn;
3466 int pmkid_match = 0;
3467 u8 ick[FILS_ICK_MAX_LEN];
3468 size_t ick_len;
3469 int res;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003470 struct wpabuf *dh_ss = NULL;
3471 const u8 *g_sta = NULL;
3472 size_t g_sta_len = 0;
3473 const u8 *g_ap = NULL;
3474 size_t g_ap_len = 0;
3475 struct wpabuf *pub = NULL;
3476
3477 os_memcpy(sm->bssid, bssid, ETH_ALEN);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003478
3479 wpa_hexdump(MSG_DEBUG, "FILS: Authentication frame fields",
3480 data, len);
3481 pos = data;
3482 end = data + len;
3483
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003484 /* TODO: FILS PK */
3485#ifdef CONFIG_FILS_SK_PFS
3486 if (sm->fils_dh_group) {
3487 u16 group;
3488
3489 /* Using FILS PFS */
3490
3491 /* Finite Cyclic Group */
3492 if (end - pos < 2) {
3493 wpa_printf(MSG_DEBUG,
3494 "FILS: No room for Finite Cyclic Group");
3495 goto fail;
3496 }
3497 group = WPA_GET_LE16(pos);
3498 pos += 2;
3499 if (group != sm->fils_dh_group) {
3500 wpa_printf(MSG_DEBUG,
3501 "FILS: Unexpected change in Finite Cyclic Group: %u (expected %u)",
3502 group, sm->fils_dh_group);
3503 goto fail;
3504 }
3505
3506 /* Element */
3507 if ((size_t) (end - pos) < sm->fils_dh_elem_len) {
3508 wpa_printf(MSG_DEBUG, "FILS: No room for Element");
3509 goto fail;
3510 }
3511
3512 if (!sm->fils_ecdh) {
3513 wpa_printf(MSG_DEBUG, "FILS: No ECDH state available");
3514 goto fail;
3515 }
3516 dh_ss = crypto_ecdh_set_peerkey(sm->fils_ecdh, 1, pos,
3517 sm->fils_dh_elem_len);
3518 if (!dh_ss) {
3519 wpa_printf(MSG_DEBUG, "FILS: ECDH operation failed");
3520 goto fail;
3521 }
3522 wpa_hexdump_buf_key(MSG_DEBUG, "FILS: DH_SS", dh_ss);
3523 g_ap = pos;
3524 g_ap_len = sm->fils_dh_elem_len;
3525 pos += sm->fils_dh_elem_len;
3526 }
3527#endif /* CONFIG_FILS_SK_PFS */
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003528
3529 wpa_hexdump(MSG_DEBUG, "FILS: Remaining IEs", pos, end - pos);
3530 if (ieee802_11_parse_elems(pos, end - pos, &elems, 1) == ParseFailed) {
3531 wpa_printf(MSG_DEBUG, "FILS: Could not parse elements");
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003532 goto fail;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003533 }
3534
3535 /* RSNE */
3536 wpa_hexdump(MSG_DEBUG, "FILS: RSN element", elems.rsn_ie,
3537 elems.rsn_ie_len);
3538 if (!elems.rsn_ie ||
3539 wpa_parse_wpa_ie_rsn(elems.rsn_ie - 2, elems.rsn_ie_len + 2,
3540 &rsn) < 0) {
3541 wpa_printf(MSG_DEBUG, "FILS: No RSN element");
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003542 goto fail;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003543 }
3544
3545 if (!elems.fils_nonce) {
3546 wpa_printf(MSG_DEBUG, "FILS: No FILS Nonce field");
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003547 goto fail;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003548 }
3549 os_memcpy(sm->fils_anonce, elems.fils_nonce, FILS_NONCE_LEN);
3550 wpa_hexdump(MSG_DEBUG, "FILS: ANonce", sm->fils_anonce, FILS_NONCE_LEN);
3551
Roshan Pius3a1667e2018-07-03 15:17:14 -07003552#ifdef CONFIG_IEEE80211R
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003553 if (wpa_key_mgmt_ft(sm->key_mgmt)) {
3554 struct wpa_ft_ies parse;
3555
3556 if (!elems.mdie || !elems.ftie) {
3557 wpa_printf(MSG_DEBUG, "FILS+FT: No MDE or FTE");
3558 goto fail;
3559 }
3560
Roshan Pius3a1667e2018-07-03 15:17:14 -07003561 if (wpa_ft_parse_ies(pos, end - pos, &parse,
3562 wpa_key_mgmt_sha384(sm->key_mgmt)) < 0) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003563 wpa_printf(MSG_DEBUG, "FILS+FT: Failed to parse IEs");
3564 goto fail;
3565 }
3566
3567 if (!parse.r0kh_id) {
3568 wpa_printf(MSG_DEBUG,
3569 "FILS+FT: No R0KH-ID subelem in FTE");
3570 goto fail;
3571 }
3572 os_memcpy(sm->r0kh_id, parse.r0kh_id, parse.r0kh_id_len);
3573 sm->r0kh_id_len = parse.r0kh_id_len;
3574 wpa_hexdump_ascii(MSG_DEBUG, "FILS+FT: R0KH-ID",
3575 sm->r0kh_id, sm->r0kh_id_len);
3576
3577 if (!parse.r1kh_id) {
3578 wpa_printf(MSG_DEBUG,
3579 "FILS+FT: No R1KH-ID subelem in FTE");
3580 goto fail;
3581 }
3582 os_memcpy(sm->r1kh_id, parse.r1kh_id, FT_R1KH_ID_LEN);
3583 wpa_hexdump(MSG_DEBUG, "FILS+FT: R1KH-ID",
3584 sm->r1kh_id, FT_R1KH_ID_LEN);
3585
3586 /* TODO: Check MDE and FTE payload */
3587
3588 wpabuf_free(sm->fils_ft_ies);
3589 sm->fils_ft_ies = wpabuf_alloc(2 + elems.mdie_len +
3590 2 + elems.ftie_len);
3591 if (!sm->fils_ft_ies)
3592 goto fail;
3593 wpabuf_put_data(sm->fils_ft_ies, elems.mdie - 2,
3594 2 + elems.mdie_len);
3595 wpabuf_put_data(sm->fils_ft_ies, elems.ftie - 2,
3596 2 + elems.ftie_len);
3597 } else {
3598 wpabuf_free(sm->fils_ft_ies);
3599 sm->fils_ft_ies = NULL;
3600 }
Roshan Pius3a1667e2018-07-03 15:17:14 -07003601#endif /* CONFIG_IEEE80211R */
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003602
3603 /* PMKID List */
3604 if (rsn.pmkid && rsn.num_pmkid > 0) {
3605 wpa_hexdump(MSG_DEBUG, "FILS: PMKID List",
3606 rsn.pmkid, rsn.num_pmkid * PMKID_LEN);
3607
3608 if (rsn.num_pmkid != 1) {
3609 wpa_printf(MSG_DEBUG, "FILS: Invalid PMKID selection");
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003610 goto fail;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003611 }
3612 wpa_hexdump(MSG_DEBUG, "FILS: PMKID", rsn.pmkid, PMKID_LEN);
3613 if (os_memcmp(sm->cur_pmksa->pmkid, rsn.pmkid, PMKID_LEN) != 0)
3614 {
3615 wpa_printf(MSG_DEBUG, "FILS: PMKID mismatch");
3616 wpa_hexdump(MSG_DEBUG, "FILS: Expected PMKID",
3617 sm->cur_pmksa->pmkid, PMKID_LEN);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003618 goto fail;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003619 }
3620 wpa_printf(MSG_DEBUG,
3621 "FILS: Matching PMKID - continue using PMKSA caching");
3622 pmkid_match = 1;
3623 }
3624 if (!pmkid_match && sm->cur_pmksa) {
3625 wpa_printf(MSG_DEBUG,
3626 "FILS: No PMKID match - cannot use cached PMKSA entry");
3627 sm->cur_pmksa = NULL;
3628 }
3629
3630 /* FILS Session */
3631 if (!elems.fils_session) {
3632 wpa_printf(MSG_DEBUG, "FILS: No FILS Session element");
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003633 goto fail;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003634 }
3635 wpa_hexdump(MSG_DEBUG, "FILS: FILS Session", elems.fils_session,
3636 FILS_SESSION_LEN);
3637 if (os_memcmp(sm->fils_session, elems.fils_session, FILS_SESSION_LEN)
3638 != 0) {
3639 wpa_printf(MSG_DEBUG, "FILS: Session mismatch");
3640 wpa_hexdump(MSG_DEBUG, "FILS: Expected FILS Session",
3641 sm->fils_session, FILS_SESSION_LEN);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003642 goto fail;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003643 }
3644
3645 /* FILS Wrapped Data */
3646 if (!sm->cur_pmksa && elems.fils_wrapped_data) {
Paul Stewart092955c2017-02-06 09:13:09 -08003647 u8 rmsk[ERP_MAX_KEY_LEN];
3648 size_t rmsk_len;
3649
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003650 wpa_hexdump(MSG_DEBUG, "FILS: Wrapped Data",
3651 elems.fils_wrapped_data,
3652 elems.fils_wrapped_data_len);
3653 eapol_sm_process_erp_finish(sm->eapol, elems.fils_wrapped_data,
3654 elems.fils_wrapped_data_len);
3655 if (eapol_sm_failed(sm->eapol))
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003656 goto fail;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003657
Paul Stewart092955c2017-02-06 09:13:09 -08003658 rmsk_len = ERP_MAX_KEY_LEN;
3659 res = eapol_sm_get_key(sm->eapol, rmsk, rmsk_len);
3660 if (res == PMK_LEN) {
3661 rmsk_len = PMK_LEN;
3662 res = eapol_sm_get_key(sm->eapol, rmsk, rmsk_len);
3663 }
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003664 if (res)
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003665 goto fail;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003666
Paul Stewart092955c2017-02-06 09:13:09 -08003667 res = fils_rmsk_to_pmk(sm->key_mgmt, rmsk, rmsk_len,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003668 sm->fils_nonce, sm->fils_anonce,
3669 dh_ss ? wpabuf_head(dh_ss) : NULL,
3670 dh_ss ? wpabuf_len(dh_ss) : 0,
Paul Stewart092955c2017-02-06 09:13:09 -08003671 sm->pmk, &sm->pmk_len);
3672 os_memset(rmsk, 0, sizeof(rmsk));
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003673
3674 /* Don't use DHss in PTK derivation if PMKSA caching is not
3675 * used. */
3676 wpabuf_clear_free(dh_ss);
3677 dh_ss = NULL;
3678
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08003679 if (res)
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003680 goto fail;
Paul Stewart092955c2017-02-06 09:13:09 -08003681
3682 if (!sm->fils_erp_pmkid_set) {
3683 wpa_printf(MSG_DEBUG, "FILS: PMKID not available");
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003684 goto fail;
Paul Stewart092955c2017-02-06 09:13:09 -08003685 }
3686 wpa_hexdump(MSG_DEBUG, "FILS: PMKID", sm->fils_erp_pmkid,
3687 PMKID_LEN);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003688 wpa_printf(MSG_DEBUG, "FILS: ERP processing succeeded - add PMKSA cache entry for the result");
Paul Stewart092955c2017-02-06 09:13:09 -08003689 sm->cur_pmksa = pmksa_cache_add(sm->pmksa, sm->pmk, sm->pmk_len,
3690 sm->fils_erp_pmkid, NULL, 0,
3691 sm->bssid, sm->own_addr,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003692 sm->network_ctx, sm->key_mgmt,
3693 NULL);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003694 }
3695
3696 if (!sm->cur_pmksa) {
3697 wpa_printf(MSG_DEBUG,
3698 "FILS: No remaining options to continue FILS authentication");
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003699 goto fail;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003700 }
3701
3702 if (fils_pmk_to_ptk(sm->pmk, sm->pmk_len, sm->own_addr, sm->bssid,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003703 sm->fils_nonce, sm->fils_anonce,
3704 dh_ss ? wpabuf_head(dh_ss) : NULL,
3705 dh_ss ? wpabuf_len(dh_ss) : 0,
3706 &sm->ptk, ick, &ick_len,
3707 sm->key_mgmt, sm->pairwise_cipher,
3708 sm->fils_ft, &sm->fils_ft_len) < 0) {
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003709 wpa_printf(MSG_DEBUG, "FILS: Failed to derive PTK");
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003710 goto fail;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003711 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003712
3713 wpabuf_clear_free(dh_ss);
3714 dh_ss = NULL;
3715
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003716 sm->ptk_set = 1;
3717 sm->tptk_set = 0;
3718 os_memset(&sm->tptk, 0, sizeof(sm->tptk));
3719
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003720#ifdef CONFIG_FILS_SK_PFS
3721 if (sm->fils_dh_group) {
3722 if (!sm->fils_ecdh) {
3723 wpa_printf(MSG_INFO, "FILS: ECDH not initialized");
3724 goto fail;
3725 }
3726 pub = crypto_ecdh_get_pubkey(sm->fils_ecdh, 1);
3727 if (!pub)
3728 goto fail;
3729 wpa_hexdump_buf(MSG_DEBUG, "FILS: gSTA", pub);
3730 g_sta = wpabuf_head(pub);
3731 g_sta_len = wpabuf_len(pub);
3732 if (!g_ap) {
3733 wpa_printf(MSG_INFO, "FILS: gAP not available");
3734 goto fail;
3735 }
3736 wpa_hexdump(MSG_DEBUG, "FILS: gAP", g_ap, g_ap_len);
3737 }
3738#endif /* CONFIG_FILS_SK_PFS */
3739
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003740 res = fils_key_auth_sk(ick, ick_len, sm->fils_nonce,
3741 sm->fils_anonce, sm->own_addr, sm->bssid,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003742 g_sta, g_sta_len, g_ap, g_ap_len,
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003743 sm->key_mgmt, sm->fils_key_auth_sta,
3744 sm->fils_key_auth_ap,
3745 &sm->fils_key_auth_len);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003746 wpabuf_free(pub);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003747 os_memset(ick, 0, sizeof(ick));
3748 return res;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003749fail:
3750 wpabuf_free(pub);
3751 wpabuf_clear_free(dh_ss);
3752 return -1;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003753}
3754
3755
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003756#ifdef CONFIG_IEEE80211R
3757static int fils_ft_build_assoc_req_rsne(struct wpa_sm *sm, struct wpabuf *buf)
3758{
3759 struct rsn_ie_hdr *rsnie;
3760 u16 capab;
3761 u8 *pos;
Roshan Pius3a1667e2018-07-03 15:17:14 -07003762 int use_sha384 = wpa_key_mgmt_sha384(sm->key_mgmt);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003763
3764 /* RSNIE[PMKR0Name/PMKR1Name] */
3765 rsnie = wpabuf_put(buf, sizeof(*rsnie));
3766 rsnie->elem_id = WLAN_EID_RSN;
3767 WPA_PUT_LE16(rsnie->version, RSN_VERSION);
3768
3769 /* Group Suite Selector */
3770 if (!wpa_cipher_valid_group(sm->group_cipher)) {
3771 wpa_printf(MSG_WARNING, "FT: Invalid group cipher (%d)",
3772 sm->group_cipher);
3773 return -1;
3774 }
3775 pos = wpabuf_put(buf, RSN_SELECTOR_LEN);
3776 RSN_SELECTOR_PUT(pos, wpa_cipher_to_suite(WPA_PROTO_RSN,
3777 sm->group_cipher));
3778
3779 /* Pairwise Suite Count */
3780 wpabuf_put_le16(buf, 1);
3781
3782 /* Pairwise Suite List */
3783 if (!wpa_cipher_valid_pairwise(sm->pairwise_cipher)) {
3784 wpa_printf(MSG_WARNING, "FT: Invalid pairwise cipher (%d)",
3785 sm->pairwise_cipher);
3786 return -1;
3787 }
3788 pos = wpabuf_put(buf, RSN_SELECTOR_LEN);
3789 RSN_SELECTOR_PUT(pos, wpa_cipher_to_suite(WPA_PROTO_RSN,
3790 sm->pairwise_cipher));
3791
3792 /* Authenticated Key Management Suite Count */
3793 wpabuf_put_le16(buf, 1);
3794
3795 /* Authenticated Key Management Suite List */
3796 pos = wpabuf_put(buf, RSN_SELECTOR_LEN);
3797 if (sm->key_mgmt == WPA_KEY_MGMT_FT_FILS_SHA256)
3798 RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_FT_FILS_SHA256);
3799 else if (sm->key_mgmt == WPA_KEY_MGMT_FT_FILS_SHA384)
3800 RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_FT_FILS_SHA384);
3801 else {
3802 wpa_printf(MSG_WARNING,
3803 "FILS+FT: Invalid key management type (%d)",
3804 sm->key_mgmt);
3805 return -1;
3806 }
3807
3808 /* RSN Capabilities */
3809 capab = 0;
3810#ifdef CONFIG_IEEE80211W
3811 if (sm->mgmt_group_cipher == WPA_CIPHER_AES_128_CMAC)
3812 capab |= WPA_CAPABILITY_MFPC;
3813#endif /* CONFIG_IEEE80211W */
3814 wpabuf_put_le16(buf, capab);
3815
3816 /* PMKID Count */
3817 wpabuf_put_le16(buf, 1);
3818
3819 /* PMKID List [PMKR1Name] */
3820 wpa_hexdump_key(MSG_DEBUG, "FILS+FT: XXKey (FILS-FT)",
3821 sm->fils_ft, sm->fils_ft_len);
3822 wpa_hexdump_ascii(MSG_DEBUG, "FILS+FT: SSID", sm->ssid, sm->ssid_len);
3823 wpa_hexdump(MSG_DEBUG, "FILS+FT: MDID",
3824 sm->mobility_domain, MOBILITY_DOMAIN_ID_LEN);
3825 wpa_hexdump_ascii(MSG_DEBUG, "FILS+FT: R0KH-ID",
3826 sm->r0kh_id, sm->r0kh_id_len);
3827 if (wpa_derive_pmk_r0(sm->fils_ft, sm->fils_ft_len, sm->ssid,
3828 sm->ssid_len, sm->mobility_domain,
3829 sm->r0kh_id, sm->r0kh_id_len, sm->own_addr,
Roshan Pius3a1667e2018-07-03 15:17:14 -07003830 sm->pmk_r0, sm->pmk_r0_name, use_sha384) < 0) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003831 wpa_printf(MSG_WARNING, "FILS+FT: Could not derive PMK-R0");
3832 return -1;
3833 }
Roshan Pius3a1667e2018-07-03 15:17:14 -07003834 sm->pmk_r0_len = use_sha384 ? SHA384_MAC_LEN : PMK_LEN;
3835 wpa_hexdump_key(MSG_DEBUG, "FILS+FT: PMK-R0",
3836 sm->pmk_r0, sm->pmk_r0_len);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003837 wpa_hexdump(MSG_DEBUG, "FILS+FT: PMKR0Name",
3838 sm->pmk_r0_name, WPA_PMK_NAME_LEN);
3839 wpa_printf(MSG_DEBUG, "FILS+FT: R1KH-ID: " MACSTR,
3840 MAC2STR(sm->r1kh_id));
3841 pos = wpabuf_put(buf, WPA_PMK_NAME_LEN);
3842 if (wpa_derive_pmk_r1_name(sm->pmk_r0_name, sm->r1kh_id, sm->own_addr,
Roshan Pius3a1667e2018-07-03 15:17:14 -07003843 pos, use_sha384) < 0) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003844 wpa_printf(MSG_WARNING, "FILS+FT: Could not derive PMKR1Name");
3845 return -1;
3846 }
3847 wpa_hexdump(MSG_DEBUG, "FILS+FT: PMKR1Name", pos, WPA_PMK_NAME_LEN);
3848
3849#ifdef CONFIG_IEEE80211W
3850 if (sm->mgmt_group_cipher == WPA_CIPHER_AES_128_CMAC) {
3851 /* Management Group Cipher Suite */
3852 pos = wpabuf_put(buf, RSN_SELECTOR_LEN);
3853 RSN_SELECTOR_PUT(pos, RSN_CIPHER_SUITE_AES_128_CMAC);
3854 }
3855#endif /* CONFIG_IEEE80211W */
3856
3857 rsnie->len = ((u8 *) wpabuf_put(buf, 0) - (u8 *) rsnie) - 2;
3858 return 0;
3859}
3860#endif /* CONFIG_IEEE80211R */
3861
3862
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003863struct wpabuf * fils_build_assoc_req(struct wpa_sm *sm, const u8 **kek,
3864 size_t *kek_len, const u8 **snonce,
Paul Stewart092955c2017-02-06 09:13:09 -08003865 const u8 **anonce,
3866 const struct wpabuf **hlp,
3867 unsigned int num_hlp)
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003868{
3869 struct wpabuf *buf;
Paul Stewart092955c2017-02-06 09:13:09 -08003870 size_t len;
3871 unsigned int i;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003872
Paul Stewart092955c2017-02-06 09:13:09 -08003873 len = 1000;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003874#ifdef CONFIG_IEEE80211R
3875 if (sm->fils_ft_ies)
3876 len += wpabuf_len(sm->fils_ft_ies);
3877 if (wpa_key_mgmt_ft(sm->key_mgmt))
3878 len += 256;
3879#endif /* CONFIG_IEEE80211R */
Paul Stewart092955c2017-02-06 09:13:09 -08003880 for (i = 0; hlp && i < num_hlp; i++)
3881 len += 10 + wpabuf_len(hlp[i]);
3882 buf = wpabuf_alloc(len);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003883 if (!buf)
3884 return NULL;
3885
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003886#ifdef CONFIG_IEEE80211R
3887 if (wpa_key_mgmt_ft(sm->key_mgmt) && sm->fils_ft_ies) {
3888 /* MDE and FTE when using FILS+FT */
3889 wpabuf_put_buf(buf, sm->fils_ft_ies);
3890 /* RSNE with PMKR1Name in PMKID field */
3891 if (fils_ft_build_assoc_req_rsne(sm, buf) < 0) {
3892 wpabuf_free(buf);
3893 return NULL;
3894 }
3895 }
3896#endif /* CONFIG_IEEE80211R */
3897
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003898 /* FILS Session */
3899 wpabuf_put_u8(buf, WLAN_EID_EXTENSION); /* Element ID */
3900 wpabuf_put_u8(buf, 1 + FILS_SESSION_LEN); /* Length */
3901 /* Element ID Extension */
3902 wpabuf_put_u8(buf, WLAN_EID_EXT_FILS_SESSION);
3903 wpabuf_put_data(buf, sm->fils_session, FILS_SESSION_LEN);
3904
3905 /* Everything after FILS Session element gets encrypted in the driver
3906 * with KEK. The buffer returned from here is the plaintext version. */
3907
3908 /* TODO: FILS Public Key */
3909
3910 /* FILS Key Confirm */
3911 wpabuf_put_u8(buf, WLAN_EID_EXTENSION); /* Element ID */
3912 wpabuf_put_u8(buf, 1 + sm->fils_key_auth_len); /* Length */
3913 /* Element ID Extension */
3914 wpabuf_put_u8(buf, WLAN_EID_EXT_FILS_KEY_CONFIRM);
3915 wpabuf_put_data(buf, sm->fils_key_auth_sta, sm->fils_key_auth_len);
3916
Paul Stewart092955c2017-02-06 09:13:09 -08003917 /* FILS HLP Container */
3918 for (i = 0; hlp && i < num_hlp; i++) {
3919 const u8 *pos = wpabuf_head(hlp[i]);
3920 size_t left = wpabuf_len(hlp[i]);
3921
3922 wpabuf_put_u8(buf, WLAN_EID_EXTENSION); /* Element ID */
3923 if (left <= 254)
3924 len = 1 + left;
3925 else
3926 len = 255;
3927 wpabuf_put_u8(buf, len); /* Length */
3928 /* Element ID Extension */
3929 wpabuf_put_u8(buf, WLAN_EID_EXT_FILS_HLP_CONTAINER);
3930 /* Destination MAC Address, Source MAC Address, HLP Packet.
3931 * HLP Packet is in MSDU format (i.e., included the LLC/SNAP
3932 * header when LPD is used). */
3933 wpabuf_put_data(buf, pos, len - 1);
3934 pos += len - 1;
3935 left -= len - 1;
3936 while (left) {
3937 wpabuf_put_u8(buf, WLAN_EID_FRAGMENT);
3938 len = left > 255 ? 255 : left;
3939 wpabuf_put_u8(buf, len);
3940 wpabuf_put_data(buf, pos, len);
3941 pos += len;
3942 left -= len;
3943 }
3944 }
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003945
3946 /* TODO: FILS IP Address Assignment */
3947
3948 wpa_hexdump_buf(MSG_DEBUG, "FILS: Association Request plaintext", buf);
3949
3950 *kek = sm->ptk.kek;
3951 *kek_len = sm->ptk.kek_len;
3952 wpa_hexdump_key(MSG_DEBUG, "FILS: KEK for AEAD", *kek, *kek_len);
3953 *snonce = sm->fils_nonce;
3954 wpa_hexdump(MSG_DEBUG, "FILS: SNonce for AEAD AAD",
3955 *snonce, FILS_NONCE_LEN);
3956 *anonce = sm->fils_anonce;
3957 wpa_hexdump(MSG_DEBUG, "FILS: ANonce for AEAD AAD",
3958 *anonce, FILS_NONCE_LEN);
3959
3960 return buf;
3961}
3962
3963
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08003964static void fils_process_hlp_resp(struct wpa_sm *sm, const u8 *resp, size_t len)
3965{
3966 const u8 *pos, *end;
3967
3968 wpa_hexdump(MSG_MSGDUMP, "FILS: HLP response", resp, len);
3969 if (len < 2 * ETH_ALEN)
3970 return;
3971 pos = resp + 2 * ETH_ALEN;
3972 end = resp + len;
3973 if (end - pos >= 6 &&
3974 os_memcmp(pos, "\xaa\xaa\x03\x00\x00\x00", 6) == 0)
3975 pos += 6; /* Remove SNAP/LLC header */
3976 wpa_sm_fils_hlp_rx(sm, resp, resp + ETH_ALEN, pos, end - pos);
3977}
3978
3979
3980static void fils_process_hlp_container(struct wpa_sm *sm, const u8 *pos,
3981 size_t len)
3982{
3983 const u8 *end = pos + len;
3984 u8 *tmp, *tmp_pos;
3985
3986 /* Check if there are any FILS HLP Container elements */
3987 while (end - pos >= 2) {
3988 if (2 + pos[1] > end - pos)
3989 return;
3990 if (pos[0] == WLAN_EID_EXTENSION &&
3991 pos[1] >= 1 + 2 * ETH_ALEN &&
3992 pos[2] == WLAN_EID_EXT_FILS_HLP_CONTAINER)
3993 break;
3994 pos += 2 + pos[1];
3995 }
3996 if (end - pos < 2)
3997 return; /* No FILS HLP Container elements */
3998
3999 tmp = os_malloc(end - pos);
4000 if (!tmp)
4001 return;
4002
4003 while (end - pos >= 2) {
4004 if (2 + pos[1] > end - pos ||
4005 pos[0] != WLAN_EID_EXTENSION ||
4006 pos[1] < 1 + 2 * ETH_ALEN ||
4007 pos[2] != WLAN_EID_EXT_FILS_HLP_CONTAINER)
4008 break;
4009 tmp_pos = tmp;
4010 os_memcpy(tmp_pos, pos + 3, pos[1] - 1);
4011 tmp_pos += pos[1] - 1;
4012 pos += 2 + pos[1];
4013
4014 /* Add possible fragments */
4015 while (end - pos >= 2 && pos[0] == WLAN_EID_FRAGMENT &&
4016 2 + pos[1] <= end - pos) {
4017 os_memcpy(tmp_pos, pos + 2, pos[1]);
4018 tmp_pos += pos[1];
4019 pos += 2 + pos[1];
4020 }
4021
4022 fils_process_hlp_resp(sm, tmp, tmp_pos - tmp);
4023 }
4024
4025 os_free(tmp);
4026}
4027
4028
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004029int fils_process_assoc_resp(struct wpa_sm *sm, const u8 *resp, size_t len)
4030{
4031 const struct ieee80211_mgmt *mgmt;
4032 const u8 *end, *ie_start;
4033 struct ieee802_11_elems elems;
4034 int keylen, rsclen;
4035 enum wpa_alg alg;
4036 struct wpa_gtk_data gd;
4037 int maxkeylen;
4038 struct wpa_eapol_ie_parse kde;
4039
4040 if (!sm || !sm->ptk_set) {
4041 wpa_printf(MSG_DEBUG, "FILS: No KEK available");
4042 return -1;
4043 }
4044
4045 if (!wpa_key_mgmt_fils(sm->key_mgmt)) {
4046 wpa_printf(MSG_DEBUG, "FILS: Not a FILS AKM");
4047 return -1;
4048 }
4049
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004050 if (sm->fils_completed) {
4051 wpa_printf(MSG_DEBUG,
4052 "FILS: Association has already been completed for this FILS authentication - ignore unexpected retransmission");
4053 return -1;
4054 }
4055
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004056 wpa_hexdump(MSG_DEBUG, "FILS: (Re)Association Response frame",
4057 resp, len);
4058
4059 mgmt = (const struct ieee80211_mgmt *) resp;
4060 if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.assoc_resp))
4061 return -1;
4062
4063 end = resp + len;
4064 /* Same offset for Association Response and Reassociation Response */
4065 ie_start = mgmt->u.assoc_resp.variable;
4066
4067 if (ieee802_11_parse_elems(ie_start, end - ie_start, &elems, 1) ==
4068 ParseFailed) {
4069 wpa_printf(MSG_DEBUG,
4070 "FILS: Failed to parse decrypted elements");
4071 goto fail;
4072 }
4073
4074 if (!elems.fils_session) {
4075 wpa_printf(MSG_DEBUG, "FILS: No FILS Session element");
4076 return -1;
4077 }
4078 if (os_memcmp(elems.fils_session, sm->fils_session,
4079 FILS_SESSION_LEN) != 0) {
4080 wpa_printf(MSG_DEBUG, "FILS: FILS Session mismatch");
4081 wpa_hexdump(MSG_DEBUG, "FILS: Received FILS Session",
4082 elems.fils_session, FILS_SESSION_LEN);
4083 wpa_hexdump(MSG_DEBUG, "FILS: Expected FILS Session",
4084 sm->fils_session, FILS_SESSION_LEN);
4085 }
4086
4087 /* TODO: FILS Public Key */
4088
4089 if (!elems.fils_key_confirm) {
4090 wpa_printf(MSG_DEBUG, "FILS: No FILS Key Confirm element");
4091 goto fail;
4092 }
4093 if (elems.fils_key_confirm_len != sm->fils_key_auth_len) {
4094 wpa_printf(MSG_DEBUG,
4095 "FILS: Unexpected Key-Auth length %d (expected %d)",
4096 elems.fils_key_confirm_len,
4097 (int) sm->fils_key_auth_len);
4098 goto fail;
4099 }
4100 if (os_memcmp(elems.fils_key_confirm, sm->fils_key_auth_ap,
4101 sm->fils_key_auth_len) != 0) {
4102 wpa_printf(MSG_DEBUG, "FILS: Key-Auth mismatch");
4103 wpa_hexdump(MSG_DEBUG, "FILS: Received Key-Auth",
4104 elems.fils_key_confirm,
4105 elems.fils_key_confirm_len);
4106 wpa_hexdump(MSG_DEBUG, "FILS: Expected Key-Auth",
4107 sm->fils_key_auth_ap, sm->fils_key_auth_len);
4108 goto fail;
4109 }
4110
4111 /* Key Delivery */
4112 if (!elems.key_delivery) {
4113 wpa_printf(MSG_DEBUG, "FILS: No Key Delivery element");
4114 goto fail;
4115 }
4116
4117 /* Parse GTK and set the key to the driver */
4118 os_memset(&gd, 0, sizeof(gd));
4119 if (wpa_supplicant_parse_ies(elems.key_delivery + WPA_KEY_RSC_LEN,
4120 elems.key_delivery_len - WPA_KEY_RSC_LEN,
4121 &kde) < 0) {
4122 wpa_printf(MSG_DEBUG, "FILS: Failed to parse KDEs");
4123 goto fail;
4124 }
4125 if (!kde.gtk) {
4126 wpa_printf(MSG_DEBUG, "FILS: No GTK KDE");
4127 goto fail;
4128 }
4129 maxkeylen = gd.gtk_len = kde.gtk_len - 2;
4130 if (wpa_supplicant_check_group_cipher(sm, sm->group_cipher,
4131 gd.gtk_len, maxkeylen,
4132 &gd.key_rsc_len, &gd.alg))
4133 goto fail;
4134
4135 wpa_hexdump_key(MSG_DEBUG, "FILS: Received GTK", kde.gtk, kde.gtk_len);
4136 gd.keyidx = kde.gtk[0] & 0x3;
4137 gd.tx = wpa_supplicant_gtk_tx_bit_workaround(sm,
4138 !!(kde.gtk[0] & BIT(2)));
4139 if (kde.gtk_len - 2 > sizeof(gd.gtk)) {
4140 wpa_printf(MSG_DEBUG, "FILS: Too long GTK in GTK KDE (len=%lu)",
4141 (unsigned long) kde.gtk_len - 2);
4142 goto fail;
4143 }
4144 os_memcpy(gd.gtk, kde.gtk + 2, kde.gtk_len - 2);
4145
4146 wpa_printf(MSG_DEBUG, "FILS: Set GTK to driver");
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004147 if (wpa_supplicant_install_gtk(sm, &gd, elems.key_delivery, 0) < 0) {
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004148 wpa_printf(MSG_DEBUG, "FILS: Failed to set GTK");
4149 goto fail;
4150 }
4151
4152 if (ieee80211w_set_keys(sm, &kde) < 0) {
4153 wpa_printf(MSG_DEBUG, "FILS: Failed to set IGTK");
4154 goto fail;
4155 }
4156
4157 alg = wpa_cipher_to_alg(sm->pairwise_cipher);
4158 keylen = wpa_cipher_key_len(sm->pairwise_cipher);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004159 if (keylen <= 0 || (unsigned int) keylen != sm->ptk.tk_len) {
4160 wpa_printf(MSG_DEBUG, "FILS: TK length mismatch: %u != %lu",
4161 keylen, (long unsigned int) sm->ptk.tk_len);
4162 goto fail;
4163 }
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004164 rsclen = wpa_cipher_rsc_len(sm->pairwise_cipher);
4165 wpa_hexdump_key(MSG_DEBUG, "FILS: Set TK to driver",
4166 sm->ptk.tk, keylen);
4167 if (wpa_sm_set_key(sm, alg, sm->bssid, 0, 1, null_rsc, rsclen,
4168 sm->ptk.tk, keylen) < 0) {
4169 wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
4170 "FILS: Failed to set PTK to the driver (alg=%d keylen=%d bssid="
4171 MACSTR ")",
4172 alg, keylen, MAC2STR(sm->bssid));
4173 goto fail;
4174 }
4175
4176 /* TODO: TK could be cleared after auth frame exchange now that driver
4177 * takes care of association frame encryption/decryption. */
4178 /* TK is not needed anymore in supplicant */
4179 os_memset(sm->ptk.tk, 0, WPA_TK_MAX_LEN);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004180 sm->ptk.tk_len = 0;
Mathy Vanhoefc66556c2017-09-29 04:22:51 +02004181 sm->ptk.installed = 1;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004182
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08004183 /* FILS HLP Container */
4184 fils_process_hlp_container(sm, ie_start, end - ie_start);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004185
4186 /* TODO: FILS IP Address Assignment */
4187
4188 wpa_printf(MSG_DEBUG, "FILS: Auth+Assoc completed successfully");
4189 sm->fils_completed = 1;
4190
4191 return 0;
4192fail:
4193 return -1;
4194}
4195
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004196
4197void wpa_sm_set_reset_fils_completed(struct wpa_sm *sm, int set)
4198{
4199 if (sm)
4200 sm->fils_completed = !!set;
4201}
4202
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004203#endif /* CONFIG_FILS */
4204
4205
4206int wpa_fils_is_completed(struct wpa_sm *sm)
4207{
4208#ifdef CONFIG_FILS
4209 return sm && sm->fils_completed;
4210#else /* CONFIG_FILS */
4211 return 0;
4212#endif /* CONFIG_FILS */
4213}
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004214
4215
4216#ifdef CONFIG_OWE
4217
4218struct wpabuf * owe_build_assoc_req(struct wpa_sm *sm, u16 group)
4219{
4220 struct wpabuf *ie = NULL, *pub = NULL;
4221 size_t prime_len;
4222
4223 if (group == 19)
4224 prime_len = 32;
4225 else if (group == 20)
4226 prime_len = 48;
4227 else if (group == 21)
4228 prime_len = 66;
4229 else
4230 return NULL;
4231
4232 crypto_ecdh_deinit(sm->owe_ecdh);
4233 sm->owe_ecdh = crypto_ecdh_init(group);
4234 if (!sm->owe_ecdh)
4235 goto fail;
4236 sm->owe_group = group;
4237 pub = crypto_ecdh_get_pubkey(sm->owe_ecdh, 0);
4238 pub = wpabuf_zeropad(pub, prime_len);
4239 if (!pub)
4240 goto fail;
4241
4242 ie = wpabuf_alloc(5 + wpabuf_len(pub));
4243 if (!ie)
4244 goto fail;
4245 wpabuf_put_u8(ie, WLAN_EID_EXTENSION);
4246 wpabuf_put_u8(ie, 1 + 2 + wpabuf_len(pub));
4247 wpabuf_put_u8(ie, WLAN_EID_EXT_OWE_DH_PARAM);
4248 wpabuf_put_le16(ie, group);
4249 wpabuf_put_buf(ie, pub);
4250 wpabuf_free(pub);
4251 wpa_hexdump_buf(MSG_DEBUG, "OWE: Diffie-Hellman Parameter element",
4252 ie);
4253
4254 return ie;
4255fail:
4256 wpabuf_free(pub);
4257 crypto_ecdh_deinit(sm->owe_ecdh);
4258 sm->owe_ecdh = NULL;
4259 return NULL;
4260}
4261
4262
4263int owe_process_assoc_resp(struct wpa_sm *sm, const u8 *bssid,
4264 const u8 *resp_ies, size_t resp_ies_len)
4265{
4266 struct ieee802_11_elems elems;
4267 u16 group;
4268 struct wpabuf *secret, *pub, *hkey;
4269 int res;
4270 u8 prk[SHA512_MAC_LEN], pmkid[SHA512_MAC_LEN];
4271 const char *info = "OWE Key Generation";
4272 const u8 *addr[2];
4273 size_t len[2];
4274 size_t hash_len, prime_len;
4275 struct wpa_ie_data data;
4276
4277 if (!resp_ies ||
4278 ieee802_11_parse_elems(resp_ies, resp_ies_len, &elems, 1) ==
4279 ParseFailed) {
4280 wpa_printf(MSG_INFO,
4281 "OWE: Could not parse Association Response frame elements");
4282 return -1;
4283 }
4284
4285 if (sm->cur_pmksa && elems.rsn_ie &&
4286 wpa_parse_wpa_ie_rsn(elems.rsn_ie - 2, 2 + elems.rsn_ie_len,
4287 &data) == 0 &&
4288 data.num_pmkid == 1 && data.pmkid &&
4289 os_memcmp(sm->cur_pmksa->pmkid, data.pmkid, PMKID_LEN) == 0) {
4290 wpa_printf(MSG_DEBUG, "OWE: Use PMKSA caching");
4291 wpa_sm_set_pmk_from_pmksa(sm);
4292 return 0;
4293 }
4294
4295 if (!elems.owe_dh) {
4296 wpa_printf(MSG_INFO,
4297 "OWE: No Diffie-Hellman Parameter element found in Association Response frame");
4298 return -1;
4299 }
4300
4301 group = WPA_GET_LE16(elems.owe_dh);
4302 if (group != sm->owe_group) {
4303 wpa_printf(MSG_INFO,
4304 "OWE: Unexpected Diffie-Hellman group in response: %u",
4305 group);
4306 return -1;
4307 }
4308
4309 if (!sm->owe_ecdh) {
4310 wpa_printf(MSG_INFO, "OWE: No ECDH state available");
4311 return -1;
4312 }
4313
4314 if (group == 19)
4315 prime_len = 32;
4316 else if (group == 20)
4317 prime_len = 48;
4318 else if (group == 21)
4319 prime_len = 66;
4320 else
4321 return -1;
4322
4323 secret = crypto_ecdh_set_peerkey(sm->owe_ecdh, 0,
4324 elems.owe_dh + 2,
4325 elems.owe_dh_len - 2);
4326 secret = wpabuf_zeropad(secret, prime_len);
4327 if (!secret) {
4328 wpa_printf(MSG_DEBUG, "OWE: Invalid peer DH public key");
4329 return -1;
4330 }
4331 wpa_hexdump_buf_key(MSG_DEBUG, "OWE: DH shared secret", secret);
4332
4333 /* prk = HKDF-extract(C | A | group, z) */
4334
4335 pub = crypto_ecdh_get_pubkey(sm->owe_ecdh, 0);
4336 if (!pub) {
4337 wpabuf_clear_free(secret);
4338 return -1;
4339 }
4340
4341 /* PMKID = Truncate-128(Hash(C | A)) */
4342 addr[0] = wpabuf_head(pub);
4343 len[0] = wpabuf_len(pub);
4344 addr[1] = elems.owe_dh + 2;
4345 len[1] = elems.owe_dh_len - 2;
4346 if (group == 19) {
4347 res = sha256_vector(2, addr, len, pmkid);
4348 hash_len = SHA256_MAC_LEN;
4349 } else if (group == 20) {
4350 res = sha384_vector(2, addr, len, pmkid);
4351 hash_len = SHA384_MAC_LEN;
4352 } else if (group == 21) {
4353 res = sha512_vector(2, addr, len, pmkid);
4354 hash_len = SHA512_MAC_LEN;
4355 } else {
4356 res = -1;
4357 hash_len = 0;
4358 }
4359 pub = wpabuf_zeropad(pub, prime_len);
4360 if (res < 0 || !pub) {
4361 wpabuf_free(pub);
4362 wpabuf_clear_free(secret);
4363 return -1;
4364 }
4365
4366 hkey = wpabuf_alloc(wpabuf_len(pub) + elems.owe_dh_len - 2 + 2);
4367 if (!hkey) {
4368 wpabuf_free(pub);
4369 wpabuf_clear_free(secret);
4370 return -1;
4371 }
4372
4373 wpabuf_put_buf(hkey, pub); /* C */
4374 wpabuf_free(pub);
4375 wpabuf_put_data(hkey, elems.owe_dh + 2, elems.owe_dh_len - 2); /* A */
4376 wpabuf_put_le16(hkey, sm->owe_group); /* group */
4377 if (group == 19)
4378 res = hmac_sha256(wpabuf_head(hkey), wpabuf_len(hkey),
4379 wpabuf_head(secret), wpabuf_len(secret), prk);
4380 else if (group == 20)
4381 res = hmac_sha384(wpabuf_head(hkey), wpabuf_len(hkey),
4382 wpabuf_head(secret), wpabuf_len(secret), prk);
4383 else if (group == 21)
4384 res = hmac_sha512(wpabuf_head(hkey), wpabuf_len(hkey),
4385 wpabuf_head(secret), wpabuf_len(secret), prk);
4386 wpabuf_clear_free(hkey);
4387 wpabuf_clear_free(secret);
4388 if (res < 0)
4389 return -1;
4390
4391 wpa_hexdump_key(MSG_DEBUG, "OWE: prk", prk, hash_len);
4392
4393 /* PMK = HKDF-expand(prk, "OWE Key Generation", n) */
4394
4395 if (group == 19)
4396 res = hmac_sha256_kdf(prk, hash_len, NULL, (const u8 *) info,
4397 os_strlen(info), sm->pmk, hash_len);
4398 else if (group == 20)
4399 res = hmac_sha384_kdf(prk, hash_len, NULL, (const u8 *) info,
4400 os_strlen(info), sm->pmk, hash_len);
4401 else if (group == 21)
4402 res = hmac_sha512_kdf(prk, hash_len, NULL, (const u8 *) info,
4403 os_strlen(info), sm->pmk, hash_len);
4404 os_memset(prk, 0, SHA512_MAC_LEN);
Roshan Pius3a1667e2018-07-03 15:17:14 -07004405 if (res < 0) {
4406 sm->pmk_len = 0;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004407 return -1;
Roshan Pius3a1667e2018-07-03 15:17:14 -07004408 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004409 sm->pmk_len = hash_len;
4410
4411 wpa_hexdump_key(MSG_DEBUG, "OWE: PMK", sm->pmk, sm->pmk_len);
4412 wpa_hexdump(MSG_DEBUG, "OWE: PMKID", pmkid, PMKID_LEN);
4413 pmksa_cache_add(sm->pmksa, sm->pmk, sm->pmk_len, pmkid, NULL, 0,
4414 bssid, sm->own_addr, sm->network_ctx, sm->key_mgmt,
4415 NULL);
4416
4417 return 0;
4418}
4419
4420#endif /* CONFIG_OWE */
4421
4422
4423void wpa_sm_set_fils_cache_id(struct wpa_sm *sm, const u8 *fils_cache_id)
4424{
4425#ifdef CONFIG_FILS
4426 if (sm && fils_cache_id) {
4427 sm->fils_cache_id_set = 1;
4428 os_memcpy(sm->fils_cache_id, fils_cache_id, FILS_CACHE_ID_LEN);
4429 }
4430#endif /* CONFIG_FILS */
4431}