blob: 6611b0e534886f7463f2e3c0a3f65b96ed23a885 [file] [log] [blame]
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001/*
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002 * IEEE 802.11 RSN / WPA Authenticator
Hai Shalom021b0b52019-04-10 11:17:58 -07003 * Copyright (c) 2004-2019, Jouni Malinen <j@w1.fi>
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004 *
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007 */
8
9#include "utils/includes.h"
10
11#include "utils/common.h"
12#include "utils/eloop.h"
13#include "utils/state_machine.h"
Dmitry Shmidtcf32e602014-01-28 10:57:39 -080014#include "utils/bitfield.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070015#include "common/ieee802_11_defs.h"
Hai Shalom74f70d42019-02-11 14:42:39 -080016#include "common/ocv.h"
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -080017#include "crypto/aes.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070018#include "crypto/aes_wrap.h"
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -080019#include "crypto/aes_siv.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070020#include "crypto/crypto.h"
21#include "crypto/sha1.h"
22#include "crypto/sha256.h"
Roshan Pius3a1667e2018-07-03 15:17:14 -070023#include "crypto/sha384.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070024#include "crypto/random.h"
25#include "eapol_auth/eapol_auth_sm.h"
Hai Shalom74f70d42019-02-11 14:42:39 -080026#include "drivers/driver.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070027#include "ap_config.h"
28#include "ieee802_11.h"
29#include "wpa_auth.h"
30#include "pmksa_cache_auth.h"
31#include "wpa_auth_i.h"
32#include "wpa_auth_ie.h"
33
34#define STATE_MACHINE_DATA struct wpa_state_machine
35#define STATE_MACHINE_DEBUG_PREFIX "WPA"
36#define STATE_MACHINE_ADDR sm->addr
37
38
39static void wpa_send_eapol_timeout(void *eloop_ctx, void *timeout_ctx);
40static int wpa_sm_step(struct wpa_state_machine *sm);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070041static int wpa_verify_key_mic(int akmp, size_t pmk_len, struct wpa_ptk *PTK,
42 u8 *data, size_t data_len);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -080043#ifdef CONFIG_FILS
44static int wpa_aead_decrypt(struct wpa_state_machine *sm, struct wpa_ptk *ptk,
45 u8 *buf, size_t buf_len, u16 *_key_data_len);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070046static struct wpabuf * fils_prepare_plainbuf(struct wpa_state_machine *sm,
47 const struct wpabuf *hlp);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -080048#endif /* CONFIG_FILS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070049static void wpa_sm_call_step(void *eloop_ctx, void *timeout_ctx);
50static void wpa_group_sm_step(struct wpa_authenticator *wpa_auth,
51 struct wpa_group *group);
52static void wpa_request_new_ptk(struct wpa_state_machine *sm);
53static int wpa_gtk_update(struct wpa_authenticator *wpa_auth,
54 struct wpa_group *group);
55static int wpa_group_config_group_keys(struct wpa_authenticator *wpa_auth,
56 struct wpa_group *group);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080057static int wpa_derive_ptk(struct wpa_state_machine *sm, const u8 *snonce,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080058 const u8 *pmk, unsigned int pmk_len,
59 struct wpa_ptk *ptk);
Dmitry Shmidt1d755d02015-04-28 10:34:29 -070060static void wpa_group_free(struct wpa_authenticator *wpa_auth,
61 struct wpa_group *group);
62static void wpa_group_get(struct wpa_authenticator *wpa_auth,
63 struct wpa_group *group);
64static void wpa_group_put(struct wpa_authenticator *wpa_auth,
65 struct wpa_group *group);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -080066static u8 * ieee80211w_kde_add(struct wpa_state_machine *sm, u8 *pos);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070067
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070068static const u32 eapol_key_timeout_first = 100; /* ms */
69static const u32 eapol_key_timeout_subseq = 1000; /* ms */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080070static const u32 eapol_key_timeout_first_group = 500; /* ms */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070071static const u32 eapol_key_timeout_no_retrans = 4000; /* ms */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070072
73/* TODO: make these configurable */
74static const int dot11RSNAConfigPMKLifetime = 43200;
75static const int dot11RSNAConfigPMKReauthThreshold = 70;
76static const int dot11RSNAConfigSATimeout = 60;
77
78
Dmitry Shmidtd5e49232012-12-03 15:08:10 -080079static inline int wpa_auth_mic_failure_report(
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070080 struct wpa_authenticator *wpa_auth, const u8 *addr)
81{
Paul Stewart092955c2017-02-06 09:13:09 -080082 if (wpa_auth->cb->mic_failure_report)
83 return wpa_auth->cb->mic_failure_report(wpa_auth->cb_ctx, addr);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -080084 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070085}
86
87
Dmitry Shmidtdda10c22015-03-24 16:05:01 -070088static inline void wpa_auth_psk_failure_report(
89 struct wpa_authenticator *wpa_auth, const u8 *addr)
90{
Paul Stewart092955c2017-02-06 09:13:09 -080091 if (wpa_auth->cb->psk_failure_report)
92 wpa_auth->cb->psk_failure_report(wpa_auth->cb_ctx, addr);
Dmitry Shmidtdda10c22015-03-24 16:05:01 -070093}
94
95
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070096static inline void wpa_auth_set_eapol(struct wpa_authenticator *wpa_auth,
97 const u8 *addr, wpa_eapol_variable var,
98 int value)
99{
Paul Stewart092955c2017-02-06 09:13:09 -0800100 if (wpa_auth->cb->set_eapol)
101 wpa_auth->cb->set_eapol(wpa_auth->cb_ctx, addr, var, value);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700102}
103
104
105static inline int wpa_auth_get_eapol(struct wpa_authenticator *wpa_auth,
106 const u8 *addr, wpa_eapol_variable var)
107{
Paul Stewart092955c2017-02-06 09:13:09 -0800108 if (wpa_auth->cb->get_eapol == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700109 return -1;
Paul Stewart092955c2017-02-06 09:13:09 -0800110 return wpa_auth->cb->get_eapol(wpa_auth->cb_ctx, addr, var);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700111}
112
113
114static inline const u8 * wpa_auth_get_psk(struct wpa_authenticator *wpa_auth,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700115 const u8 *addr,
116 const u8 *p2p_dev_addr,
Hai Shalom021b0b52019-04-10 11:17:58 -0700117 const u8 *prev_psk, size_t *psk_len,
118 int *vlan_id)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700119{
Paul Stewart092955c2017-02-06 09:13:09 -0800120 if (wpa_auth->cb->get_psk == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700121 return NULL;
Paul Stewart092955c2017-02-06 09:13:09 -0800122 return wpa_auth->cb->get_psk(wpa_auth->cb_ctx, addr, p2p_dev_addr,
Hai Shalom021b0b52019-04-10 11:17:58 -0700123 prev_psk, psk_len, vlan_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700124}
125
126
127static inline int wpa_auth_get_msk(struct wpa_authenticator *wpa_auth,
128 const u8 *addr, u8 *msk, size_t *len)
129{
Paul Stewart092955c2017-02-06 09:13:09 -0800130 if (wpa_auth->cb->get_msk == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700131 return -1;
Paul Stewart092955c2017-02-06 09:13:09 -0800132 return wpa_auth->cb->get_msk(wpa_auth->cb_ctx, addr, msk, len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700133}
134
135
136static inline int wpa_auth_set_key(struct wpa_authenticator *wpa_auth,
137 int vlan_id,
138 enum wpa_alg alg, const u8 *addr, int idx,
139 u8 *key, size_t key_len)
140{
Paul Stewart092955c2017-02-06 09:13:09 -0800141 if (wpa_auth->cb->set_key == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700142 return -1;
Paul Stewart092955c2017-02-06 09:13:09 -0800143 return wpa_auth->cb->set_key(wpa_auth->cb_ctx, vlan_id, alg, addr, idx,
144 key, key_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700145}
146
147
148static inline int wpa_auth_get_seqnum(struct wpa_authenticator *wpa_auth,
149 const u8 *addr, int idx, u8 *seq)
150{
Paul Stewart092955c2017-02-06 09:13:09 -0800151 if (wpa_auth->cb->get_seqnum == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700152 return -1;
Paul Stewart092955c2017-02-06 09:13:09 -0800153 return wpa_auth->cb->get_seqnum(wpa_auth->cb_ctx, addr, idx, seq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700154}
155
156
157static inline int
158wpa_auth_send_eapol(struct wpa_authenticator *wpa_auth, const u8 *addr,
159 const u8 *data, size_t data_len, int encrypt)
160{
Paul Stewart092955c2017-02-06 09:13:09 -0800161 if (wpa_auth->cb->send_eapol == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700162 return -1;
Paul Stewart092955c2017-02-06 09:13:09 -0800163 return wpa_auth->cb->send_eapol(wpa_auth->cb_ctx, addr, data, data_len,
164 encrypt);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700165}
166
167
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800168#ifdef CONFIG_MESH
169static inline int wpa_auth_start_ampe(struct wpa_authenticator *wpa_auth,
170 const u8 *addr)
171{
Paul Stewart092955c2017-02-06 09:13:09 -0800172 if (wpa_auth->cb->start_ampe == NULL)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800173 return -1;
Paul Stewart092955c2017-02-06 09:13:09 -0800174 return wpa_auth->cb->start_ampe(wpa_auth->cb_ctx, addr);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800175}
176#endif /* CONFIG_MESH */
177
178
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700179int wpa_auth_for_each_sta(struct wpa_authenticator *wpa_auth,
180 int (*cb)(struct wpa_state_machine *sm, void *ctx),
181 void *cb_ctx)
182{
Paul Stewart092955c2017-02-06 09:13:09 -0800183 if (wpa_auth->cb->for_each_sta == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700184 return 0;
Paul Stewart092955c2017-02-06 09:13:09 -0800185 return wpa_auth->cb->for_each_sta(wpa_auth->cb_ctx, cb, cb_ctx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700186}
187
188
189int wpa_auth_for_each_auth(struct wpa_authenticator *wpa_auth,
190 int (*cb)(struct wpa_authenticator *a, void *ctx),
191 void *cb_ctx)
192{
Paul Stewart092955c2017-02-06 09:13:09 -0800193 if (wpa_auth->cb->for_each_auth == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700194 return 0;
Paul Stewart092955c2017-02-06 09:13:09 -0800195 return wpa_auth->cb->for_each_auth(wpa_auth->cb_ctx, cb, cb_ctx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700196}
197
198
199void wpa_auth_logger(struct wpa_authenticator *wpa_auth, const u8 *addr,
200 logger_level level, const char *txt)
201{
Paul Stewart092955c2017-02-06 09:13:09 -0800202 if (wpa_auth->cb->logger == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700203 return;
Paul Stewart092955c2017-02-06 09:13:09 -0800204 wpa_auth->cb->logger(wpa_auth->cb_ctx, addr, level, txt);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700205}
206
207
208void wpa_auth_vlogger(struct wpa_authenticator *wpa_auth, const u8 *addr,
209 logger_level level, const char *fmt, ...)
210{
211 char *format;
212 int maxlen;
213 va_list ap;
214
Paul Stewart092955c2017-02-06 09:13:09 -0800215 if (wpa_auth->cb->logger == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700216 return;
217
218 maxlen = os_strlen(fmt) + 100;
219 format = os_malloc(maxlen);
220 if (!format)
221 return;
222
223 va_start(ap, fmt);
224 vsnprintf(format, maxlen, fmt, ap);
225 va_end(ap);
226
227 wpa_auth_logger(wpa_auth, addr, level, format);
228
229 os_free(format);
230}
231
232
233static void wpa_sta_disconnect(struct wpa_authenticator *wpa_auth,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700234 const u8 *addr, u16 reason)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700235{
Paul Stewart092955c2017-02-06 09:13:09 -0800236 if (wpa_auth->cb->disconnect == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700237 return;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700238 wpa_printf(MSG_DEBUG, "wpa_sta_disconnect STA " MACSTR " (reason %u)",
239 MAC2STR(addr), reason);
240 wpa_auth->cb->disconnect(wpa_auth->cb_ctx, addr, reason);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700241}
242
243
Hai Shalom74f70d42019-02-11 14:42:39 -0800244#ifdef CONFIG_OCV
245static int wpa_channel_info(struct wpa_authenticator *wpa_auth,
246 struct wpa_channel_info *ci)
247{
248 if (!wpa_auth->cb->channel_info)
249 return -1;
250 return wpa_auth->cb->channel_info(wpa_auth->cb_ctx, ci);
251}
252#endif /* CONFIG_OCV */
253
254
Hai Shalom021b0b52019-04-10 11:17:58 -0700255static int wpa_auth_update_vlan(struct wpa_authenticator *wpa_auth,
256 const u8 *addr, int vlan_id)
257{
258 if (!wpa_auth->cb->update_vlan)
259 return -1;
260 return wpa_auth->cb->update_vlan(wpa_auth->cb_ctx, addr, vlan_id);
261}
262
263
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700264static void wpa_rekey_gmk(void *eloop_ctx, void *timeout_ctx)
265{
266 struct wpa_authenticator *wpa_auth = eloop_ctx;
267
268 if (random_get_bytes(wpa_auth->group->GMK, WPA_GMK_LEN)) {
269 wpa_printf(MSG_ERROR, "Failed to get random data for WPA "
270 "initialization.");
271 } else {
272 wpa_auth_logger(wpa_auth, NULL, LOGGER_DEBUG, "GMK rekeyd");
273 wpa_hexdump_key(MSG_DEBUG, "GMK",
274 wpa_auth->group->GMK, WPA_GMK_LEN);
275 }
276
277 if (wpa_auth->conf.wpa_gmk_rekey) {
278 eloop_register_timeout(wpa_auth->conf.wpa_gmk_rekey, 0,
279 wpa_rekey_gmk, wpa_auth, NULL);
280 }
281}
282
283
284static void wpa_rekey_gtk(void *eloop_ctx, void *timeout_ctx)
285{
286 struct wpa_authenticator *wpa_auth = eloop_ctx;
Dmitry Shmidt1d755d02015-04-28 10:34:29 -0700287 struct wpa_group *group, *next;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700288
289 wpa_auth_logger(wpa_auth, NULL, LOGGER_DEBUG, "rekeying GTK");
Dmitry Shmidt1d755d02015-04-28 10:34:29 -0700290 group = wpa_auth->group;
291 while (group) {
292 wpa_group_get(wpa_auth, group);
293
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700294 group->GTKReKey = TRUE;
295 do {
296 group->changed = FALSE;
297 wpa_group_sm_step(wpa_auth, group);
298 } while (group->changed);
Dmitry Shmidt1d755d02015-04-28 10:34:29 -0700299
300 next = group->next;
301 wpa_group_put(wpa_auth, group);
302 group = next;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700303 }
304
305 if (wpa_auth->conf.wpa_group_rekey) {
306 eloop_register_timeout(wpa_auth->conf.wpa_group_rekey,
307 0, wpa_rekey_gtk, wpa_auth, NULL);
308 }
309}
310
311
312static void wpa_rekey_ptk(void *eloop_ctx, void *timeout_ctx)
313{
314 struct wpa_authenticator *wpa_auth = eloop_ctx;
315 struct wpa_state_machine *sm = timeout_ctx;
316
317 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG, "rekeying PTK");
318 wpa_request_new_ptk(sm);
319 wpa_sm_step(sm);
320}
321
322
Hai Shalom81f62d82019-07-22 12:10:00 -0700323void wpa_auth_set_ptk_rekey_timer(struct wpa_state_machine *sm)
324{
325 if (sm && sm->wpa_auth->conf.wpa_ptk_rekey) {
326 wpa_printf(MSG_DEBUG, "WPA: Start PTK rekeying timer for "
327 MACSTR " (%d seconds)", MAC2STR(sm->addr),
328 sm->wpa_auth->conf.wpa_ptk_rekey);
329 eloop_cancel_timeout(wpa_rekey_ptk, sm->wpa_auth, sm);
330 eloop_register_timeout(sm->wpa_auth->conf.wpa_ptk_rekey, 0,
331 wpa_rekey_ptk, sm->wpa_auth, sm);
332 }
333}
334
335
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700336static int wpa_auth_pmksa_clear_cb(struct wpa_state_machine *sm, void *ctx)
337{
338 if (sm->pmksa == ctx)
339 sm->pmksa = NULL;
340 return 0;
341}
342
343
344static void wpa_auth_pmksa_free_cb(struct rsn_pmksa_cache_entry *entry,
345 void *ctx)
346{
347 struct wpa_authenticator *wpa_auth = ctx;
348 wpa_auth_for_each_sta(wpa_auth, wpa_auth_pmksa_clear_cb, entry);
349}
350
351
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700352static int wpa_group_init_gmk_and_counter(struct wpa_authenticator *wpa_auth,
353 struct wpa_group *group)
354{
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800355 u8 buf[ETH_ALEN + 8 + sizeof(unsigned long)];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700356 u8 rkey[32];
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800357 unsigned long ptr;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700358
359 if (random_get_bytes(group->GMK, WPA_GMK_LEN) < 0)
360 return -1;
361 wpa_hexdump_key(MSG_DEBUG, "GMK", group->GMK, WPA_GMK_LEN);
362
363 /*
364 * Counter = PRF-256(Random number, "Init Counter",
365 * Local MAC Address || Time)
366 */
367 os_memcpy(buf, wpa_auth->addr, ETH_ALEN);
368 wpa_get_ntp_timestamp(buf + ETH_ALEN);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800369 ptr = (unsigned long) group;
370 os_memcpy(buf + ETH_ALEN + 8, &ptr, sizeof(ptr));
Hai Shalom74f70d42019-02-11 14:42:39 -0800371#ifdef TEST_FUZZ
372 os_memset(buf + ETH_ALEN, 0xab, 8);
373 os_memset(buf + ETH_ALEN + 8, 0xcd, sizeof(ptr));
374#endif /* TEST_FUZZ */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700375 if (random_get_bytes(rkey, sizeof(rkey)) < 0)
376 return -1;
377
378 if (sha1_prf(rkey, sizeof(rkey), "Init Counter", buf, sizeof(buf),
379 group->Counter, WPA_NONCE_LEN) < 0)
380 return -1;
381 wpa_hexdump_key(MSG_DEBUG, "Key Counter",
382 group->Counter, WPA_NONCE_LEN);
383
384 return 0;
385}
386
387
388static struct wpa_group * wpa_group_init(struct wpa_authenticator *wpa_auth,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800389 int vlan_id, int delay_init)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700390{
391 struct wpa_group *group;
392
393 group = os_zalloc(sizeof(struct wpa_group));
394 if (group == NULL)
395 return NULL;
396
397 group->GTKAuthenticator = TRUE;
398 group->vlan_id = vlan_id;
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700399 group->GTK_len = wpa_cipher_key_len(wpa_auth->conf.wpa_group);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700400
401 if (random_pool_ready() != 1) {
402 wpa_printf(MSG_INFO, "WPA: Not enough entropy in random pool "
403 "for secure operations - update keys later when "
404 "the first station connects");
405 }
406
407 /*
408 * Set initial GMK/Counter value here. The actual values that will be
409 * used in negotiations will be set once the first station tries to
410 * connect. This allows more time for collecting additional randomness
411 * on embedded devices.
412 */
413 if (wpa_group_init_gmk_and_counter(wpa_auth, group) < 0) {
414 wpa_printf(MSG_ERROR, "Failed to get random data for WPA "
415 "initialization.");
416 os_free(group);
417 return NULL;
418 }
419
420 group->GInit = TRUE;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800421 if (delay_init) {
422 wpa_printf(MSG_DEBUG, "WPA: Delay group state machine start "
423 "until Beacon frames have been configured");
424 /* Initialization is completed in wpa_init_keys(). */
425 } else {
426 wpa_group_sm_step(wpa_auth, group);
427 group->GInit = FALSE;
428 wpa_group_sm_step(wpa_auth, group);
429 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700430
431 return group;
432}
433
434
435/**
436 * wpa_init - Initialize WPA authenticator
437 * @addr: Authenticator address
438 * @conf: Configuration for WPA authenticator
439 * @cb: Callback functions for WPA authenticator
440 * Returns: Pointer to WPA authenticator data or %NULL on failure
441 */
442struct wpa_authenticator * wpa_init(const u8 *addr,
443 struct wpa_auth_config *conf,
Paul Stewart092955c2017-02-06 09:13:09 -0800444 const struct wpa_auth_callbacks *cb,
445 void *cb_ctx)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700446{
447 struct wpa_authenticator *wpa_auth;
448
449 wpa_auth = os_zalloc(sizeof(struct wpa_authenticator));
450 if (wpa_auth == NULL)
451 return NULL;
452 os_memcpy(wpa_auth->addr, addr, ETH_ALEN);
453 os_memcpy(&wpa_auth->conf, conf, sizeof(*conf));
Paul Stewart092955c2017-02-06 09:13:09 -0800454 wpa_auth->cb = cb;
455 wpa_auth->cb_ctx = cb_ctx;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700456
457 if (wpa_auth_gen_wpa_ie(wpa_auth)) {
458 wpa_printf(MSG_ERROR, "Could not generate WPA IE.");
459 os_free(wpa_auth);
460 return NULL;
461 }
462
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800463 wpa_auth->group = wpa_group_init(wpa_auth, 0, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700464 if (wpa_auth->group == NULL) {
465 os_free(wpa_auth->wpa_ie);
466 os_free(wpa_auth);
467 return NULL;
468 }
469
470 wpa_auth->pmksa = pmksa_cache_auth_init(wpa_auth_pmksa_free_cb,
471 wpa_auth);
472 if (wpa_auth->pmksa == NULL) {
473 wpa_printf(MSG_ERROR, "PMKSA cache initialization failed.");
Dmitry Shmidtff787d52015-01-12 13:01:47 -0800474 os_free(wpa_auth->group);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700475 os_free(wpa_auth->wpa_ie);
476 os_free(wpa_auth);
477 return NULL;
478 }
479
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800480#ifdef CONFIG_IEEE80211R_AP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700481 wpa_auth->ft_pmk_cache = wpa_ft_pmk_cache_init();
482 if (wpa_auth->ft_pmk_cache == NULL) {
483 wpa_printf(MSG_ERROR, "FT PMK cache initialization failed.");
Dmitry Shmidtff787d52015-01-12 13:01:47 -0800484 os_free(wpa_auth->group);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700485 os_free(wpa_auth->wpa_ie);
486 pmksa_cache_auth_deinit(wpa_auth->pmksa);
487 os_free(wpa_auth);
488 return NULL;
489 }
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800490#endif /* CONFIG_IEEE80211R_AP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700491
492 if (wpa_auth->conf.wpa_gmk_rekey) {
493 eloop_register_timeout(wpa_auth->conf.wpa_gmk_rekey, 0,
494 wpa_rekey_gmk, wpa_auth, NULL);
495 }
496
497 if (wpa_auth->conf.wpa_group_rekey) {
498 eloop_register_timeout(wpa_auth->conf.wpa_group_rekey, 0,
499 wpa_rekey_gtk, wpa_auth, NULL);
500 }
501
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800502#ifdef CONFIG_P2P
503 if (WPA_GET_BE32(conf->ip_addr_start)) {
504 int count = WPA_GET_BE32(conf->ip_addr_end) -
505 WPA_GET_BE32(conf->ip_addr_start) + 1;
506 if (count > 1000)
507 count = 1000;
508 if (count > 0)
509 wpa_auth->ip_pool = bitfield_alloc(count);
510 }
511#endif /* CONFIG_P2P */
512
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700513 return wpa_auth;
514}
515
516
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800517int wpa_init_keys(struct wpa_authenticator *wpa_auth)
518{
519 struct wpa_group *group = wpa_auth->group;
520
521 wpa_printf(MSG_DEBUG, "WPA: Start group state machine to set initial "
522 "keys");
523 wpa_group_sm_step(wpa_auth, group);
524 group->GInit = FALSE;
525 wpa_group_sm_step(wpa_auth, group);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800526 if (group->wpa_group_state == WPA_GROUP_FATAL_FAILURE)
527 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800528 return 0;
529}
530
531
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700532/**
533 * wpa_deinit - Deinitialize WPA authenticator
534 * @wpa_auth: Pointer to WPA authenticator data from wpa_init()
535 */
536void wpa_deinit(struct wpa_authenticator *wpa_auth)
537{
538 struct wpa_group *group, *prev;
539
540 eloop_cancel_timeout(wpa_rekey_gmk, wpa_auth, NULL);
541 eloop_cancel_timeout(wpa_rekey_gtk, wpa_auth, NULL);
542
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700543 pmksa_cache_auth_deinit(wpa_auth->pmksa);
544
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800545#ifdef CONFIG_IEEE80211R_AP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700546 wpa_ft_pmk_cache_deinit(wpa_auth->ft_pmk_cache);
547 wpa_auth->ft_pmk_cache = NULL;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700548 wpa_ft_deinit(wpa_auth);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800549#endif /* CONFIG_IEEE80211R_AP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700550
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800551#ifdef CONFIG_P2P
552 bitfield_free(wpa_auth->ip_pool);
553#endif /* CONFIG_P2P */
554
555
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700556 os_free(wpa_auth->wpa_ie);
557
558 group = wpa_auth->group;
559 while (group) {
560 prev = group;
561 group = group->next;
562 os_free(prev);
563 }
564
565 os_free(wpa_auth);
566}
567
568
569/**
570 * wpa_reconfig - Update WPA authenticator configuration
571 * @wpa_auth: Pointer to WPA authenticator data from wpa_init()
572 * @conf: Configuration for WPA authenticator
573 */
574int wpa_reconfig(struct wpa_authenticator *wpa_auth,
575 struct wpa_auth_config *conf)
576{
577 struct wpa_group *group;
578 if (wpa_auth == NULL)
579 return 0;
580
581 os_memcpy(&wpa_auth->conf, conf, sizeof(*conf));
582 if (wpa_auth_gen_wpa_ie(wpa_auth)) {
583 wpa_printf(MSG_ERROR, "Could not generate WPA IE.");
584 return -1;
585 }
586
587 /*
588 * Reinitialize GTK to make sure it is suitable for the new
589 * configuration.
590 */
591 group = wpa_auth->group;
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700592 group->GTK_len = wpa_cipher_key_len(wpa_auth->conf.wpa_group);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700593 group->GInit = TRUE;
594 wpa_group_sm_step(wpa_auth, group);
595 group->GInit = FALSE;
596 wpa_group_sm_step(wpa_auth, group);
597
598 return 0;
599}
600
601
602struct wpa_state_machine *
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700603wpa_auth_sta_init(struct wpa_authenticator *wpa_auth, const u8 *addr,
604 const u8 *p2p_dev_addr)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700605{
606 struct wpa_state_machine *sm;
607
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800608 if (wpa_auth->group->wpa_group_state == WPA_GROUP_FATAL_FAILURE)
609 return NULL;
610
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700611 sm = os_zalloc(sizeof(struct wpa_state_machine));
612 if (sm == NULL)
613 return NULL;
614 os_memcpy(sm->addr, addr, ETH_ALEN);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700615 if (p2p_dev_addr)
616 os_memcpy(sm->p2p_dev_addr, p2p_dev_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700617
618 sm->wpa_auth = wpa_auth;
619 sm->group = wpa_auth->group;
Dmitry Shmidt1d755d02015-04-28 10:34:29 -0700620 wpa_group_get(sm->wpa_auth, sm->group);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700621
622 return sm;
623}
624
625
626int wpa_auth_sta_associated(struct wpa_authenticator *wpa_auth,
627 struct wpa_state_machine *sm)
628{
629 if (wpa_auth == NULL || !wpa_auth->conf.wpa || sm == NULL)
630 return -1;
631
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800632#ifdef CONFIG_IEEE80211R_AP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700633 if (sm->ft_completed) {
634 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG,
635 "FT authentication already completed - do not "
636 "start 4-way handshake");
Dmitry Shmidt71757432014-06-02 13:50:35 -0700637 /* Go to PTKINITDONE state to allow GTK rekeying */
638 sm->wpa_ptk_state = WPA_PTK_PTKINITDONE;
Dmitry Shmidtebd93af2017-02-21 13:40:44 -0800639 sm->Pair = TRUE;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700640 return 0;
641 }
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800642#endif /* CONFIG_IEEE80211R_AP */
643
644#ifdef CONFIG_FILS
645 if (sm->fils_completed) {
646 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG,
647 "FILS authentication already completed - do not start 4-way handshake");
648 /* Go to PTKINITDONE state to allow GTK rekeying */
649 sm->wpa_ptk_state = WPA_PTK_PTKINITDONE;
Dmitry Shmidtebd93af2017-02-21 13:40:44 -0800650 sm->Pair = TRUE;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800651 return 0;
652 }
653#endif /* CONFIG_FILS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700654
655 if (sm->started) {
656 os_memset(&sm->key_replay, 0, sizeof(sm->key_replay));
657 sm->ReAuthenticationRequest = TRUE;
658 return wpa_sm_step(sm);
659 }
660
661 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG,
662 "start authentication");
663 sm->started = 1;
664
665 sm->Init = TRUE;
666 if (wpa_sm_step(sm) == 1)
667 return 1; /* should not really happen */
668 sm->Init = FALSE;
669 sm->AuthenticationRequest = TRUE;
670 return wpa_sm_step(sm);
671}
672
673
674void wpa_auth_sta_no_wpa(struct wpa_state_machine *sm)
675{
676 /* WPA/RSN was not used - clear WPA state. This is needed if the STA
677 * reassociates back to the same AP while the previous entry for the
678 * STA has not yet been removed. */
679 if (sm == NULL)
680 return;
681
682 sm->wpa_key_mgmt = 0;
683}
684
685
686static void wpa_free_sta_sm(struct wpa_state_machine *sm)
687{
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800688#ifdef CONFIG_P2P
689 if (WPA_GET_BE32(sm->ip_addr)) {
690 u32 start;
691 wpa_printf(MSG_DEBUG, "P2P: Free assigned IP "
692 "address %u.%u.%u.%u from " MACSTR,
693 sm->ip_addr[0], sm->ip_addr[1],
694 sm->ip_addr[2], sm->ip_addr[3],
695 MAC2STR(sm->addr));
696 start = WPA_GET_BE32(sm->wpa_auth->conf.ip_addr_start);
697 bitfield_clear(sm->wpa_auth->ip_pool,
698 WPA_GET_BE32(sm->ip_addr) - start);
699 }
700#endif /* CONFIG_P2P */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700701 if (sm->GUpdateStationKeys) {
702 sm->group->GKeyDoneStations--;
703 sm->GUpdateStationKeys = FALSE;
704 }
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800705#ifdef CONFIG_IEEE80211R_AP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700706 os_free(sm->assoc_resp_ftie);
Dmitry Shmidtd11f0192014-03-24 12:09:47 -0700707 wpabuf_free(sm->ft_pending_req_ies);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800708#endif /* CONFIG_IEEE80211R_AP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700709 os_free(sm->last_rx_eapol_key);
710 os_free(sm->wpa_ie);
Hai Shalomc3565922019-10-28 11:58:20 -0700711 os_free(sm->rsnxe);
Dmitry Shmidt1d755d02015-04-28 10:34:29 -0700712 wpa_group_put(sm->wpa_auth, sm->group);
Hai Shalom021b0b52019-04-10 11:17:58 -0700713#ifdef CONFIG_DPP2
714 wpabuf_clear_free(sm->dpp_z);
715#endif /* CONFIG_DPP2 */
Hai Shalom1dc4d202019-04-29 16:22:27 -0700716 bin_clear_free(sm, sizeof(*sm));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700717}
718
719
720void wpa_auth_sta_deinit(struct wpa_state_machine *sm)
721{
722 if (sm == NULL)
723 return;
724
725 if (sm->wpa_auth->conf.wpa_strict_rekey && sm->has_GTK) {
726 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
727 "strict rekeying - force GTK rekey since STA "
728 "is leaving");
Roshan Pius3a1667e2018-07-03 15:17:14 -0700729 if (eloop_deplete_timeout(0, 500000, wpa_rekey_gtk,
730 sm->wpa_auth, NULL) == -1)
731 eloop_register_timeout(0, 500000, wpa_rekey_gtk, sm->wpa_auth,
732 NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700733 }
734
735 eloop_cancel_timeout(wpa_send_eapol_timeout, sm->wpa_auth, sm);
736 sm->pending_1_of_4_timeout = 0;
737 eloop_cancel_timeout(wpa_sm_call_step, sm, NULL);
738 eloop_cancel_timeout(wpa_rekey_ptk, sm->wpa_auth, sm);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700739#ifdef CONFIG_IEEE80211R_AP
740 wpa_ft_sta_deinit(sm);
741#endif /* CONFIG_IEEE80211R_AP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700742 if (sm->in_step_loop) {
743 /* Must not free state machine while wpa_sm_step() is running.
744 * Freeing will be completed in the end of wpa_sm_step(). */
745 wpa_printf(MSG_DEBUG, "WPA: Registering pending STA state "
746 "machine deinit for " MACSTR, MAC2STR(sm->addr));
747 sm->pending_deinit = 1;
748 } else
749 wpa_free_sta_sm(sm);
750}
751
752
753static void wpa_request_new_ptk(struct wpa_state_machine *sm)
754{
755 if (sm == NULL)
756 return;
757
758 sm->PTKRequest = TRUE;
759 sm->PTK_valid = 0;
760}
761
762
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800763static int wpa_replay_counter_valid(struct wpa_key_replay_counter *ctr,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700764 const u8 *replay_counter)
765{
766 int i;
767 for (i = 0; i < RSNA_MAX_EAPOL_RETRIES; i++) {
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800768 if (!ctr[i].valid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700769 break;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800770 if (os_memcmp(replay_counter, ctr[i].counter,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700771 WPA_REPLAY_COUNTER_LEN) == 0)
772 return 1;
773 }
774 return 0;
775}
776
777
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800778static void wpa_replay_counter_mark_invalid(struct wpa_key_replay_counter *ctr,
779 const u8 *replay_counter)
780{
781 int i;
782 for (i = 0; i < RSNA_MAX_EAPOL_RETRIES; i++) {
783 if (ctr[i].valid &&
784 (replay_counter == NULL ||
785 os_memcmp(replay_counter, ctr[i].counter,
786 WPA_REPLAY_COUNTER_LEN) == 0))
787 ctr[i].valid = FALSE;
788 }
789}
790
791
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800792#ifdef CONFIG_IEEE80211R_AP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700793static int ft_check_msg_2_of_4(struct wpa_authenticator *wpa_auth,
794 struct wpa_state_machine *sm,
795 struct wpa_eapol_ie_parse *kde)
796{
797 struct wpa_ie_data ie;
798 struct rsn_mdie *mdie;
799
800 if (wpa_parse_wpa_ie_rsn(kde->rsn_ie, kde->rsn_ie_len, &ie) < 0 ||
801 ie.num_pmkid != 1 || ie.pmkid == NULL) {
802 wpa_printf(MSG_DEBUG, "FT: No PMKR1Name in "
803 "FT 4-way handshake message 2/4");
804 return -1;
805 }
806
807 os_memcpy(sm->sup_pmk_r1_name, ie.pmkid, PMKID_LEN);
808 wpa_hexdump(MSG_DEBUG, "FT: PMKR1Name from Supplicant",
809 sm->sup_pmk_r1_name, PMKID_LEN);
810
811 if (!kde->mdie || !kde->ftie) {
812 wpa_printf(MSG_DEBUG, "FT: No %s in FT 4-way handshake "
813 "message 2/4", kde->mdie ? "FTIE" : "MDIE");
814 return -1;
815 }
816
817 mdie = (struct rsn_mdie *) (kde->mdie + 2);
818 if (kde->mdie[1] < sizeof(struct rsn_mdie) ||
819 os_memcmp(wpa_auth->conf.mobility_domain, mdie->mobility_domain,
820 MOBILITY_DOMAIN_ID_LEN) != 0) {
821 wpa_printf(MSG_DEBUG, "FT: MDIE mismatch");
822 return -1;
823 }
824
825 if (sm->assoc_resp_ftie &&
826 (kde->ftie[1] != sm->assoc_resp_ftie[1] ||
827 os_memcmp(kde->ftie, sm->assoc_resp_ftie,
828 2 + sm->assoc_resp_ftie[1]) != 0)) {
829 wpa_printf(MSG_DEBUG, "FT: FTIE mismatch");
830 wpa_hexdump(MSG_DEBUG, "FT: FTIE in EAPOL-Key msg 2/4",
831 kde->ftie, kde->ftie_len);
832 wpa_hexdump(MSG_DEBUG, "FT: FTIE in (Re)AssocResp",
833 sm->assoc_resp_ftie, 2 + sm->assoc_resp_ftie[1]);
834 return -1;
835 }
836
837 return 0;
838}
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800839#endif /* CONFIG_IEEE80211R_AP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700840
841
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800842static int wpa_receive_error_report(struct wpa_authenticator *wpa_auth,
843 struct wpa_state_machine *sm, int group)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800844{
845 /* Supplicant reported a Michael MIC error */
846 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_INFO,
847 "received EAPOL-Key Error Request "
848 "(STA detected Michael MIC failure (group=%d))",
849 group);
850
851 if (group && wpa_auth->conf.wpa_group != WPA_CIPHER_TKIP) {
852 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
853 "ignore Michael MIC failure report since "
854 "group cipher is not TKIP");
855 } else if (!group && sm->pairwise != WPA_CIPHER_TKIP) {
856 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
857 "ignore Michael MIC failure report since "
858 "pairwise cipher is not TKIP");
859 } else {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800860 if (wpa_auth_mic_failure_report(wpa_auth, sm->addr) > 0)
861 return 1; /* STA entry was removed */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800862 sm->dot11RSNAStatsTKIPRemoteMICFailures++;
863 wpa_auth->dot11RSNAStatsTKIPRemoteMICFailures++;
864 }
865
866 /*
867 * Error report is not a request for a new key handshake, but since
868 * Authenticator may do it, let's change the keys now anyway.
869 */
870 wpa_request_new_ptk(sm);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800871 return 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800872}
873
874
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800875static int wpa_try_alt_snonce(struct wpa_state_machine *sm, u8 *data,
876 size_t data_len)
877{
878 struct wpa_ptk PTK;
879 int ok = 0;
880 const u8 *pmk = NULL;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700881 size_t pmk_len;
Hai Shalom021b0b52019-04-10 11:17:58 -0700882 int vlan_id = 0;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800883
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800884 os_memset(&PTK, 0, sizeof(PTK));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800885 for (;;) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700886 if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt) &&
887 !wpa_key_mgmt_sae(sm->wpa_key_mgmt)) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800888 pmk = wpa_auth_get_psk(sm->wpa_auth, sm->addr,
Hai Shalom021b0b52019-04-10 11:17:58 -0700889 sm->p2p_dev_addr, pmk, &pmk_len,
890 &vlan_id);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800891 if (pmk == NULL)
892 break;
Roshan Pius3a1667e2018-07-03 15:17:14 -0700893#ifdef CONFIG_IEEE80211R_AP
894 if (wpa_key_mgmt_ft_psk(sm->wpa_key_mgmt)) {
895 os_memcpy(sm->xxkey, pmk, pmk_len);
896 sm->xxkey_len = pmk_len;
897 }
898#endif /* CONFIG_IEEE80211R_AP */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800899 } else {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800900 pmk = sm->PMK;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800901 pmk_len = sm->pmk_len;
902 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800903
Dmitry Shmidtebd93af2017-02-21 13:40:44 -0800904 if (wpa_derive_ptk(sm, sm->alt_SNonce, pmk, pmk_len, &PTK) < 0)
905 break;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800906
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700907 if (wpa_verify_key_mic(sm->wpa_key_mgmt, pmk_len, &PTK,
908 data, data_len) == 0) {
Hai Shalom021b0b52019-04-10 11:17:58 -0700909 if (sm->PMK != pmk) {
910 os_memcpy(sm->PMK, pmk, pmk_len);
911 sm->pmk_len = pmk_len;
912 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800913 ok = 1;
914 break;
915 }
916
Roshan Pius3a1667e2018-07-03 15:17:14 -0700917 if (!wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt) ||
918 wpa_key_mgmt_sae(sm->wpa_key_mgmt))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800919 break;
920 }
921
922 if (!ok) {
923 wpa_printf(MSG_DEBUG,
924 "WPA: Earlier SNonce did not result in matching MIC");
925 return -1;
926 }
927
928 wpa_printf(MSG_DEBUG,
929 "WPA: Earlier SNonce resulted in matching MIC");
930 sm->alt_snonce_valid = 0;
Hai Shalom021b0b52019-04-10 11:17:58 -0700931
932 if (vlan_id && wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt) &&
933 wpa_auth_update_vlan(sm->wpa_auth, sm->addr, vlan_id) < 0)
934 return -1;
935
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800936 os_memcpy(sm->SNonce, sm->alt_SNonce, WPA_NONCE_LEN);
937 os_memcpy(&sm->PTK, &PTK, sizeof(PTK));
Hai Shalom81f62d82019-07-22 12:10:00 -0700938 forced_memzero(&PTK, sizeof(PTK));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800939 sm->PTK_valid = TRUE;
940
941 return 0;
942}
943
944
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700945void wpa_receive(struct wpa_authenticator *wpa_auth,
946 struct wpa_state_machine *sm,
947 u8 *data, size_t data_len)
948{
949 struct ieee802_1x_hdr *hdr;
950 struct wpa_eapol_key *key;
951 u16 key_info, key_data_length;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700952 enum { PAIRWISE_2, PAIRWISE_4, GROUP_2, REQUEST } msg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700953 char *msgtxt;
954 struct wpa_eapol_ie_parse kde;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800955 const u8 *key_data;
956 size_t keyhdrlen, mic_len;
957 u8 *mic;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700958
959 if (wpa_auth == NULL || !wpa_auth->conf.wpa || sm == NULL)
960 return;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800961 wpa_hexdump(MSG_MSGDUMP, "WPA: RX EAPOL data", data, data_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700962
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700963 mic_len = wpa_mic_len(sm->wpa_key_mgmt, sm->pmk_len);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800964 keyhdrlen = sizeof(*key) + mic_len + 2;
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800965
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800966 if (data_len < sizeof(*hdr) + keyhdrlen) {
967 wpa_printf(MSG_DEBUG, "WPA: Ignore too short EAPOL-Key frame");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700968 return;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800969 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700970
971 hdr = (struct ieee802_1x_hdr *) data;
972 key = (struct wpa_eapol_key *) (hdr + 1);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800973 mic = (u8 *) (key + 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700974 key_info = WPA_GET_BE16(key->key_info);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800975 key_data = mic + mic_len + 2;
976 key_data_length = WPA_GET_BE16(mic + mic_len);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800977 wpa_printf(MSG_DEBUG, "WPA: Received EAPOL-Key from " MACSTR
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800978 " key_info=0x%x type=%u mic_len=%u key_data_length=%u",
979 MAC2STR(sm->addr), key_info, key->type,
980 (unsigned int) mic_len, key_data_length);
981 wpa_hexdump(MSG_MSGDUMP,
982 "WPA: EAPOL-Key header (ending before Key MIC)",
983 key, sizeof(*key));
984 wpa_hexdump(MSG_MSGDUMP, "WPA: EAPOL-Key Key MIC",
985 mic, mic_len);
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800986 if (key_data_length > data_len - sizeof(*hdr) - keyhdrlen) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700987 wpa_printf(MSG_INFO, "WPA: Invalid EAPOL-Key frame - "
988 "key_data overflow (%d > %lu)",
989 key_data_length,
990 (unsigned long) (data_len - sizeof(*hdr) -
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800991 keyhdrlen));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700992 return;
993 }
994
995 if (sm->wpa == WPA_VERSION_WPA2) {
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800996 if (key->type == EAPOL_KEY_TYPE_WPA) {
997 /*
998 * Some deployed station implementations seem to send
999 * msg 4/4 with incorrect type value in WPA2 mode.
1000 */
1001 wpa_printf(MSG_DEBUG, "Workaround: Allow EAPOL-Key "
1002 "with unexpected WPA type in RSN mode");
1003 } else if (key->type != EAPOL_KEY_TYPE_RSN) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001004 wpa_printf(MSG_DEBUG, "Ignore EAPOL-Key with "
1005 "unexpected type %d in RSN mode",
1006 key->type);
1007 return;
1008 }
1009 } else {
1010 if (key->type != EAPOL_KEY_TYPE_WPA) {
1011 wpa_printf(MSG_DEBUG, "Ignore EAPOL-Key with "
1012 "unexpected type %d in WPA mode",
1013 key->type);
1014 return;
1015 }
1016 }
1017
1018 wpa_hexdump(MSG_DEBUG, "WPA: Received Key Nonce", key->key_nonce,
1019 WPA_NONCE_LEN);
1020 wpa_hexdump(MSG_DEBUG, "WPA: Received Replay Counter",
1021 key->replay_counter, WPA_REPLAY_COUNTER_LEN);
1022
1023 /* FIX: verify that the EAPOL-Key frame was encrypted if pairwise keys
1024 * are set */
1025
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001026 if (key_info & WPA_KEY_INFO_SMK_MESSAGE) {
1027 wpa_printf(MSG_DEBUG, "WPA: Ignore SMK message");
1028 return;
1029 }
1030
1031 if (key_info & WPA_KEY_INFO_REQUEST) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001032 msg = REQUEST;
1033 msgtxt = "Request";
1034 } else if (!(key_info & WPA_KEY_INFO_KEY_TYPE)) {
1035 msg = GROUP_2;
1036 msgtxt = "2/2 Group";
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001037 } else if (key_data_length == 0 ||
1038 (mic_len == 0 && (key_info & WPA_KEY_INFO_ENCR_KEY_DATA) &&
1039 key_data_length == AES_BLOCK_SIZE)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001040 msg = PAIRWISE_4;
1041 msgtxt = "4/4 Pairwise";
1042 } else {
1043 msg = PAIRWISE_2;
1044 msgtxt = "2/4 Pairwise";
1045 }
1046
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001047 if (msg == REQUEST || msg == PAIRWISE_2 || msg == PAIRWISE_4 ||
1048 msg == GROUP_2) {
1049 u16 ver = key_info & WPA_KEY_INFO_TYPE_MASK;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001050 if (sm->pairwise == WPA_CIPHER_CCMP ||
1051 sm->pairwise == WPA_CIPHER_GCMP) {
Roshan Pius3a1667e2018-07-03 15:17:14 -07001052 if (wpa_use_cmac(sm->wpa_key_mgmt) &&
1053 !wpa_use_akm_defined(sm->wpa_key_mgmt) &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001054 ver != WPA_KEY_INFO_TYPE_AES_128_CMAC) {
1055 wpa_auth_logger(wpa_auth, sm->addr,
1056 LOGGER_WARNING,
1057 "advertised support for "
1058 "AES-128-CMAC, but did not "
1059 "use it");
1060 return;
1061 }
1062
Roshan Pius3a1667e2018-07-03 15:17:14 -07001063 if (!wpa_use_cmac(sm->wpa_key_mgmt) &&
1064 !wpa_use_akm_defined(sm->wpa_key_mgmt) &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001065 ver != WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
1066 wpa_auth_logger(wpa_auth, sm->addr,
1067 LOGGER_WARNING,
1068 "did not use HMAC-SHA1-AES "
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001069 "with CCMP/GCMP");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001070 return;
1071 }
1072 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001073
Roshan Pius3a1667e2018-07-03 15:17:14 -07001074 if (wpa_use_akm_defined(sm->wpa_key_mgmt) &&
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001075 ver != WPA_KEY_INFO_TYPE_AKM_DEFINED) {
1076 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_WARNING,
1077 "did not use EAPOL-Key descriptor version 0 as required for AKM-defined cases");
1078 return;
1079 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001080 }
1081
1082 if (key_info & WPA_KEY_INFO_REQUEST) {
1083 if (sm->req_replay_counter_used &&
1084 os_memcmp(key->replay_counter, sm->req_replay_counter,
1085 WPA_REPLAY_COUNTER_LEN) <= 0) {
1086 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_WARNING,
1087 "received EAPOL-Key request with "
1088 "replayed counter");
1089 return;
1090 }
1091 }
1092
1093 if (!(key_info & WPA_KEY_INFO_REQUEST) &&
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001094 !wpa_replay_counter_valid(sm->key_replay, key->replay_counter)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001095 int i;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001096
1097 if (msg == PAIRWISE_2 &&
1098 wpa_replay_counter_valid(sm->prev_key_replay,
1099 key->replay_counter) &&
1100 sm->wpa_ptk_state == WPA_PTK_PTKINITNEGOTIATING &&
1101 os_memcmp(sm->SNonce, key->key_nonce, WPA_NONCE_LEN) != 0)
1102 {
1103 /*
1104 * Some supplicant implementations (e.g., Windows XP
1105 * WZC) update SNonce for each EAPOL-Key 2/4. This
1106 * breaks the workaround on accepting any of the
1107 * pending requests, so allow the SNonce to be updated
1108 * even if we have already sent out EAPOL-Key 3/4.
1109 */
1110 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_DEBUG,
1111 "Process SNonce update from STA "
1112 "based on retransmitted EAPOL-Key "
1113 "1/4");
1114 sm->update_snonce = 1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001115 os_memcpy(sm->alt_SNonce, sm->SNonce, WPA_NONCE_LEN);
1116 sm->alt_snonce_valid = TRUE;
1117 os_memcpy(sm->alt_replay_counter,
1118 sm->key_replay[0].counter,
1119 WPA_REPLAY_COUNTER_LEN);
1120 goto continue_processing;
1121 }
1122
1123 if (msg == PAIRWISE_4 && sm->alt_snonce_valid &&
1124 sm->wpa_ptk_state == WPA_PTK_PTKINITNEGOTIATING &&
1125 os_memcmp(key->replay_counter, sm->alt_replay_counter,
1126 WPA_REPLAY_COUNTER_LEN) == 0) {
1127 /*
1128 * Supplicant may still be using the old SNonce since
1129 * there was two EAPOL-Key 2/4 messages and they had
1130 * different SNonce values.
1131 */
1132 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_DEBUG,
1133 "Try to process received EAPOL-Key 4/4 based on old Replay Counter and SNonce from an earlier EAPOL-Key 1/4");
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001134 goto continue_processing;
1135 }
1136
1137 if (msg == PAIRWISE_2 &&
1138 wpa_replay_counter_valid(sm->prev_key_replay,
1139 key->replay_counter) &&
1140 sm->wpa_ptk_state == WPA_PTK_PTKINITNEGOTIATING) {
1141 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_DEBUG,
1142 "ignore retransmitted EAPOL-Key %s - "
1143 "SNonce did not change", msgtxt);
1144 } else {
1145 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_DEBUG,
1146 "received EAPOL-Key %s with "
1147 "unexpected replay counter", msgtxt);
1148 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001149 for (i = 0; i < RSNA_MAX_EAPOL_RETRIES; i++) {
1150 if (!sm->key_replay[i].valid)
1151 break;
1152 wpa_hexdump(MSG_DEBUG, "pending replay counter",
1153 sm->key_replay[i].counter,
1154 WPA_REPLAY_COUNTER_LEN);
1155 }
1156 wpa_hexdump(MSG_DEBUG, "received replay counter",
1157 key->replay_counter, WPA_REPLAY_COUNTER_LEN);
1158 return;
1159 }
1160
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001161continue_processing:
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001162#ifdef CONFIG_FILS
1163 if (sm->wpa == WPA_VERSION_WPA2 && mic_len == 0 &&
1164 !(key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
1165 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_DEBUG,
1166 "WPA: Encr Key Data bit not set even though AEAD cipher is supposed to be used - drop frame");
1167 return;
1168 }
1169#endif /* CONFIG_FILS */
1170
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001171 switch (msg) {
1172 case PAIRWISE_2:
1173 if (sm->wpa_ptk_state != WPA_PTK_PTKSTART &&
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001174 sm->wpa_ptk_state != WPA_PTK_PTKCALCNEGOTIATING &&
1175 (!sm->update_snonce ||
1176 sm->wpa_ptk_state != WPA_PTK_PTKINITNEGOTIATING)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001177 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_INFO,
1178 "received EAPOL-Key msg 2/4 in "
1179 "invalid state (%d) - dropped",
1180 sm->wpa_ptk_state);
1181 return;
1182 }
1183 random_add_randomness(key->key_nonce, WPA_NONCE_LEN);
1184 if (sm->group->reject_4way_hs_for_entropy) {
1185 /*
1186 * The system did not have enough entropy to generate
1187 * strong random numbers. Reject the first 4-way
1188 * handshake(s) and collect some entropy based on the
1189 * information from it. Once enough entropy is
1190 * available, the next atempt will trigger GMK/Key
1191 * Counter update and the station will be allowed to
1192 * continue.
1193 */
1194 wpa_printf(MSG_DEBUG, "WPA: Reject 4-way handshake to "
1195 "collect more entropy for random number "
1196 "generation");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001197 random_mark_pool_ready();
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001198 wpa_sta_disconnect(wpa_auth, sm->addr,
1199 WLAN_REASON_PREV_AUTH_NOT_VALID);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001200 return;
1201 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001202 break;
1203 case PAIRWISE_4:
1204 if (sm->wpa_ptk_state != WPA_PTK_PTKINITNEGOTIATING ||
1205 !sm->PTK_valid) {
1206 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_INFO,
1207 "received EAPOL-Key msg 4/4 in "
1208 "invalid state (%d) - dropped",
1209 sm->wpa_ptk_state);
1210 return;
1211 }
1212 break;
1213 case GROUP_2:
1214 if (sm->wpa_ptk_group_state != WPA_PTK_GROUP_REKEYNEGOTIATING
1215 || !sm->PTK_valid) {
1216 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_INFO,
1217 "received EAPOL-Key msg 2/2 in "
1218 "invalid state (%d) - dropped",
1219 sm->wpa_ptk_group_state);
1220 return;
1221 }
1222 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001223 case REQUEST:
1224 break;
1225 }
1226
1227 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_DEBUG,
1228 "received EAPOL-Key frame (%s)", msgtxt);
1229
1230 if (key_info & WPA_KEY_INFO_ACK) {
1231 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
1232 "received invalid EAPOL-Key: Key Ack set");
1233 return;
1234 }
1235
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001236 if (!wpa_key_mgmt_fils(sm->wpa_key_mgmt) &&
1237 !(key_info & WPA_KEY_INFO_MIC)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001238 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
1239 "received invalid EAPOL-Key: Key MIC not set");
1240 return;
1241 }
1242
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001243#ifdef CONFIG_FILS
1244 if (wpa_key_mgmt_fils(sm->wpa_key_mgmt) &&
1245 (key_info & WPA_KEY_INFO_MIC)) {
1246 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
1247 "received invalid EAPOL-Key: Key MIC set");
1248 return;
1249 }
1250#endif /* CONFIG_FILS */
1251
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001252 sm->MICVerified = FALSE;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001253 if (sm->PTK_valid && !sm->update_snonce) {
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001254 if (mic_len &&
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001255 wpa_verify_key_mic(sm->wpa_key_mgmt, sm->pmk_len, &sm->PTK,
1256 data, data_len) &&
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001257 (msg != PAIRWISE_4 || !sm->alt_snonce_valid ||
1258 wpa_try_alt_snonce(sm, data, data_len))) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001259 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
1260 "received EAPOL-Key with invalid MIC");
Hai Shalom74f70d42019-02-11 14:42:39 -08001261#ifdef TEST_FUZZ
1262 wpa_printf(MSG_INFO,
1263 "TEST: Ignore Key MIC failure for fuzz testing");
1264 goto continue_fuzz;
1265#endif /* TEST_FUZZ */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001266 return;
1267 }
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001268#ifdef CONFIG_FILS
1269 if (!mic_len &&
1270 wpa_aead_decrypt(sm, &sm->PTK, data, data_len,
1271 &key_data_length) < 0) {
1272 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
1273 "received EAPOL-Key with invalid MIC");
Hai Shalom74f70d42019-02-11 14:42:39 -08001274#ifdef TEST_FUZZ
1275 wpa_printf(MSG_INFO,
1276 "TEST: Ignore Key MIC failure for fuzz testing");
1277 goto continue_fuzz;
1278#endif /* TEST_FUZZ */
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001279 return;
1280 }
1281#endif /* CONFIG_FILS */
Hai Shalom74f70d42019-02-11 14:42:39 -08001282#ifdef TEST_FUZZ
1283 continue_fuzz:
1284#endif /* TEST_FUZZ */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001285 sm->MICVerified = TRUE;
1286 eloop_cancel_timeout(wpa_send_eapol_timeout, wpa_auth, sm);
1287 sm->pending_1_of_4_timeout = 0;
1288 }
1289
1290 if (key_info & WPA_KEY_INFO_REQUEST) {
1291 if (sm->MICVerified) {
1292 sm->req_replay_counter_used = 1;
1293 os_memcpy(sm->req_replay_counter, key->replay_counter,
1294 WPA_REPLAY_COUNTER_LEN);
1295 } else {
1296 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
1297 "received EAPOL-Key request with "
1298 "invalid MIC");
1299 return;
1300 }
1301
1302 /*
1303 * TODO: should decrypt key data field if encryption was used;
1304 * even though MAC address KDE is not normally encrypted,
1305 * supplicant is allowed to encrypt it.
1306 */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001307 if (key_info & WPA_KEY_INFO_ERROR) {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001308 if (wpa_receive_error_report(
1309 wpa_auth, sm,
1310 !(key_info & WPA_KEY_INFO_KEY_TYPE)) > 0)
1311 return; /* STA entry was removed */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001312 } else if (key_info & WPA_KEY_INFO_KEY_TYPE) {
1313 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
1314 "received EAPOL-Key Request for new "
1315 "4-Way Handshake");
1316 wpa_request_new_ptk(sm);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001317 } else if (key_data_length > 0 &&
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001318 wpa_parse_kde_ies(key_data, key_data_length,
1319 &kde) == 0 &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001320 kde.mac_addr) {
1321 } else {
1322 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
1323 "received EAPOL-Key Request for GTK "
1324 "rekeying");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001325 eloop_cancel_timeout(wpa_rekey_gtk, wpa_auth, NULL);
1326 wpa_rekey_gtk(wpa_auth, NULL);
1327 }
1328 } else {
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001329 /* Do not allow the same key replay counter to be reused. */
1330 wpa_replay_counter_mark_invalid(sm->key_replay,
1331 key->replay_counter);
1332
1333 if (msg == PAIRWISE_2) {
1334 /*
1335 * Maintain a copy of the pending EAPOL-Key frames in
1336 * case the EAPOL-Key frame was retransmitted. This is
1337 * needed to allow EAPOL-Key msg 2/4 reply to another
1338 * pending msg 1/4 to update the SNonce to work around
1339 * unexpected supplicant behavior.
1340 */
1341 os_memcpy(sm->prev_key_replay, sm->key_replay,
1342 sizeof(sm->key_replay));
1343 } else {
1344 os_memset(sm->prev_key_replay, 0,
1345 sizeof(sm->prev_key_replay));
1346 }
1347
1348 /*
1349 * Make sure old valid counters are not accepted anymore and
1350 * do not get copied again.
1351 */
1352 wpa_replay_counter_mark_invalid(sm->key_replay, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001353 }
1354
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001355 os_free(sm->last_rx_eapol_key);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001356 sm->last_rx_eapol_key = os_memdup(data, data_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001357 if (sm->last_rx_eapol_key == NULL)
1358 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001359 sm->last_rx_eapol_key_len = data_len;
1360
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001361 sm->rx_eapol_key_secure = !!(key_info & WPA_KEY_INFO_SECURE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001362 sm->EAPOLKeyReceived = TRUE;
1363 sm->EAPOLKeyPairwise = !!(key_info & WPA_KEY_INFO_KEY_TYPE);
1364 sm->EAPOLKeyRequest = !!(key_info & WPA_KEY_INFO_REQUEST);
1365 os_memcpy(sm->SNonce, key->key_nonce, WPA_NONCE_LEN);
1366 wpa_sm_step(sm);
1367}
1368
1369
1370static int wpa_gmk_to_gtk(const u8 *gmk, const char *label, const u8 *addr,
1371 const u8 *gnonce, u8 *gtk, size_t gtk_len)
1372{
Roshan Pius3a1667e2018-07-03 15:17:14 -07001373 u8 data[ETH_ALEN + WPA_NONCE_LEN + 8 + WPA_GTK_MAX_LEN];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001374 u8 *pos;
1375 int ret = 0;
1376
1377 /* GTK = PRF-X(GMK, "Group key expansion",
1378 * AA || GNonce || Time || random data)
1379 * The example described in the IEEE 802.11 standard uses only AA and
1380 * GNonce as inputs here. Add some more entropy since this derivation
1381 * is done only at the Authenticator and as such, does not need to be
1382 * exactly same.
1383 */
Roshan Pius3a1667e2018-07-03 15:17:14 -07001384 os_memset(data, 0, sizeof(data));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001385 os_memcpy(data, addr, ETH_ALEN);
1386 os_memcpy(data + ETH_ALEN, gnonce, WPA_NONCE_LEN);
1387 pos = data + ETH_ALEN + WPA_NONCE_LEN;
1388 wpa_get_ntp_timestamp(pos);
Hai Shalom74f70d42019-02-11 14:42:39 -08001389#ifdef TEST_FUZZ
1390 os_memset(pos, 0xef, 8);
1391#endif /* TEST_FUZZ */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001392 pos += 8;
Roshan Pius3a1667e2018-07-03 15:17:14 -07001393 if (random_get_bytes(pos, gtk_len) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001394 ret = -1;
1395
Roshan Pius3a1667e2018-07-03 15:17:14 -07001396#ifdef CONFIG_SHA384
1397 if (sha384_prf(gmk, WPA_GMK_LEN, label, data, sizeof(data),
1398 gtk, gtk_len) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001399 ret = -1;
Roshan Pius3a1667e2018-07-03 15:17:14 -07001400#else /* CONFIG_SHA384 */
1401#ifdef CONFIG_SHA256
1402 if (sha256_prf(gmk, WPA_GMK_LEN, label, data, sizeof(data),
1403 gtk, gtk_len) < 0)
1404 ret = -1;
1405#else /* CONFIG_SHA256 */
1406 if (sha1_prf(gmk, WPA_GMK_LEN, label, data, sizeof(data),
1407 gtk, gtk_len) < 0)
1408 ret = -1;
1409#endif /* CONFIG_SHA256 */
1410#endif /* CONFIG_SHA384 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001411
Hai Shalom81f62d82019-07-22 12:10:00 -07001412 forced_memzero(data, sizeof(data));
1413
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001414 return ret;
1415}
1416
1417
1418static void wpa_send_eapol_timeout(void *eloop_ctx, void *timeout_ctx)
1419{
1420 struct wpa_authenticator *wpa_auth = eloop_ctx;
1421 struct wpa_state_machine *sm = timeout_ctx;
1422
1423 sm->pending_1_of_4_timeout = 0;
1424 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG, "EAPOL-Key timeout");
1425 sm->TimeoutEvt = TRUE;
1426 wpa_sm_step(sm);
1427}
1428
1429
1430void __wpa_send_eapol(struct wpa_authenticator *wpa_auth,
1431 struct wpa_state_machine *sm, int key_info,
1432 const u8 *key_rsc, const u8 *nonce,
1433 const u8 *kde, size_t kde_len,
1434 int keyidx, int encr, int force_version)
1435{
1436 struct ieee802_1x_hdr *hdr;
1437 struct wpa_eapol_key *key;
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001438 size_t len, mic_len, keyhdrlen;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001439 int alg;
1440 int key_data_len, pad_len = 0;
1441 u8 *buf, *pos;
1442 int version, pairwise;
1443 int i;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001444 u8 *key_mic, *key_data;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001445
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001446 mic_len = wpa_mic_len(sm->wpa_key_mgmt, sm->pmk_len);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001447 keyhdrlen = sizeof(*key) + mic_len + 2;
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001448
1449 len = sizeof(struct ieee802_1x_hdr) + keyhdrlen;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001450
1451 if (force_version)
1452 version = force_version;
Roshan Pius3a1667e2018-07-03 15:17:14 -07001453 else if (wpa_use_akm_defined(sm->wpa_key_mgmt))
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001454 version = WPA_KEY_INFO_TYPE_AKM_DEFINED;
Roshan Pius3a1667e2018-07-03 15:17:14 -07001455 else if (wpa_use_cmac(sm->wpa_key_mgmt))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001456 version = WPA_KEY_INFO_TYPE_AES_128_CMAC;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001457 else if (sm->pairwise != WPA_CIPHER_TKIP)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001458 version = WPA_KEY_INFO_TYPE_HMAC_SHA1_AES;
1459 else
1460 version = WPA_KEY_INFO_TYPE_HMAC_MD5_RC4;
1461
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07001462 pairwise = !!(key_info & WPA_KEY_INFO_KEY_TYPE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001463
1464 wpa_printf(MSG_DEBUG, "WPA: Send EAPOL(version=%d secure=%d mic=%d "
1465 "ack=%d install=%d pairwise=%d kde_len=%lu keyidx=%d "
1466 "encr=%d)",
1467 version,
1468 (key_info & WPA_KEY_INFO_SECURE) ? 1 : 0,
1469 (key_info & WPA_KEY_INFO_MIC) ? 1 : 0,
1470 (key_info & WPA_KEY_INFO_ACK) ? 1 : 0,
1471 (key_info & WPA_KEY_INFO_INSTALL) ? 1 : 0,
1472 pairwise, (unsigned long) kde_len, keyidx, encr);
1473
1474 key_data_len = kde_len;
1475
1476 if ((version == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES ||
Roshan Pius3a1667e2018-07-03 15:17:14 -07001477 wpa_use_aes_key_wrap(sm->wpa_key_mgmt) ||
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001478 version == WPA_KEY_INFO_TYPE_AES_128_CMAC) && encr) {
1479 pad_len = key_data_len % 8;
1480 if (pad_len)
1481 pad_len = 8 - pad_len;
1482 key_data_len += pad_len + 8;
1483 }
1484
1485 len += key_data_len;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001486 if (!mic_len && encr)
1487 len += AES_BLOCK_SIZE;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001488
1489 hdr = os_zalloc(len);
1490 if (hdr == NULL)
1491 return;
1492 hdr->version = wpa_auth->conf.eapol_version;
1493 hdr->type = IEEE802_1X_TYPE_EAPOL_KEY;
1494 hdr->length = host_to_be16(len - sizeof(*hdr));
1495 key = (struct wpa_eapol_key *) (hdr + 1);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001496 key_mic = (u8 *) (key + 1);
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001497 key_data = ((u8 *) (hdr + 1)) + keyhdrlen;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001498
1499 key->type = sm->wpa == WPA_VERSION_WPA2 ?
1500 EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA;
1501 key_info |= version;
1502 if (encr && sm->wpa == WPA_VERSION_WPA2)
1503 key_info |= WPA_KEY_INFO_ENCR_KEY_DATA;
1504 if (sm->wpa != WPA_VERSION_WPA2)
1505 key_info |= keyidx << WPA_KEY_INFO_KEY_INDEX_SHIFT;
1506 WPA_PUT_BE16(key->key_info, key_info);
1507
1508 alg = pairwise ? sm->pairwise : wpa_auth->conf.wpa_group;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001509 if (sm->wpa == WPA_VERSION_WPA2 && !pairwise)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001510 WPA_PUT_BE16(key->key_length, 0);
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08001511 else
1512 WPA_PUT_BE16(key->key_length, wpa_cipher_key_len(alg));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001513
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001514 for (i = RSNA_MAX_EAPOL_RETRIES - 1; i > 0; i--) {
1515 sm->key_replay[i].valid = sm->key_replay[i - 1].valid;
1516 os_memcpy(sm->key_replay[i].counter,
1517 sm->key_replay[i - 1].counter,
1518 WPA_REPLAY_COUNTER_LEN);
1519 }
1520 inc_byte_array(sm->key_replay[0].counter, WPA_REPLAY_COUNTER_LEN);
1521 os_memcpy(key->replay_counter, sm->key_replay[0].counter,
1522 WPA_REPLAY_COUNTER_LEN);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001523 wpa_hexdump(MSG_DEBUG, "WPA: Replay Counter",
1524 key->replay_counter, WPA_REPLAY_COUNTER_LEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001525 sm->key_replay[0].valid = TRUE;
1526
1527 if (nonce)
1528 os_memcpy(key->key_nonce, nonce, WPA_NONCE_LEN);
1529
1530 if (key_rsc)
1531 os_memcpy(key->key_rsc, key_rsc, WPA_KEY_RSC_LEN);
1532
1533 if (kde && !encr) {
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001534 os_memcpy(key_data, kde, kde_len);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001535 WPA_PUT_BE16(key_mic + mic_len, kde_len);
1536#ifdef CONFIG_FILS
Roshan Pius3a1667e2018-07-03 15:17:14 -07001537 } else if (!mic_len && kde) {
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001538 const u8 *aad[1];
1539 size_t aad_len[1];
1540
1541 WPA_PUT_BE16(key_mic, AES_BLOCK_SIZE + kde_len);
1542 wpa_hexdump_key(MSG_DEBUG, "Plaintext EAPOL-Key Key Data",
1543 kde, kde_len);
1544
1545 wpa_hexdump_key(MSG_DEBUG, "WPA: KEK",
1546 sm->PTK.kek, sm->PTK.kek_len);
1547 /* AES-SIV AAD from EAPOL protocol version field (inclusive) to
1548 * to Key Data (exclusive). */
1549 aad[0] = (u8 *) hdr;
1550 aad_len[0] = key_mic + 2 - (u8 *) hdr;
1551 if (aes_siv_encrypt(sm->PTK.kek, sm->PTK.kek_len, kde, kde_len,
1552 1, aad, aad_len, key_mic + 2) < 0) {
1553 wpa_printf(MSG_DEBUG, "WPA: AES-SIV encryption failed");
1554 return;
1555 }
1556
1557 wpa_hexdump(MSG_DEBUG, "WPA: Encrypted Key Data from SIV",
1558 key_mic + 2, AES_BLOCK_SIZE + kde_len);
1559#endif /* CONFIG_FILS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001560 } else if (encr && kde) {
1561 buf = os_zalloc(key_data_len);
1562 if (buf == NULL) {
1563 os_free(hdr);
1564 return;
1565 }
1566 pos = buf;
1567 os_memcpy(pos, kde, kde_len);
1568 pos += kde_len;
1569
1570 if (pad_len)
1571 *pos++ = 0xdd;
1572
1573 wpa_hexdump_key(MSG_DEBUG, "Plaintext EAPOL-Key Key Data",
1574 buf, key_data_len);
1575 if (version == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES ||
Roshan Pius3a1667e2018-07-03 15:17:14 -07001576 wpa_use_aes_key_wrap(sm->wpa_key_mgmt) ||
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001577 version == WPA_KEY_INFO_TYPE_AES_128_CMAC) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001578 wpa_printf(MSG_DEBUG,
1579 "WPA: Encrypt Key Data using AES-WRAP (KEK length %u)",
1580 (unsigned int) sm->PTK.kek_len);
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001581 if (aes_wrap(sm->PTK.kek, sm->PTK.kek_len,
1582 (key_data_len - 8) / 8, buf, key_data)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001583 os_free(hdr);
1584 os_free(buf);
1585 return;
1586 }
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001587 WPA_PUT_BE16(key_mic + mic_len, key_data_len);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001588#ifndef CONFIG_NO_RC4
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001589 } else if (sm->PTK.kek_len == 16) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001590 u8 ek[32];
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001591
1592 wpa_printf(MSG_DEBUG,
1593 "WPA: Encrypt Key Data using RC4");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001594 os_memcpy(key->key_iv,
1595 sm->group->Counter + WPA_NONCE_LEN - 16, 16);
1596 inc_byte_array(sm->group->Counter, WPA_NONCE_LEN);
1597 os_memcpy(ek, key->key_iv, 16);
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001598 os_memcpy(ek + 16, sm->PTK.kek, sm->PTK.kek_len);
1599 os_memcpy(key_data, buf, key_data_len);
1600 rc4_skip(ek, 32, 256, key_data, key_data_len);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001601 WPA_PUT_BE16(key_mic + mic_len, key_data_len);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001602#endif /* CONFIG_NO_RC4 */
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001603 } else {
1604 os_free(hdr);
1605 os_free(buf);
1606 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001607 }
1608 os_free(buf);
1609 }
1610
1611 if (key_info & WPA_KEY_INFO_MIC) {
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001612 if (!sm->PTK_valid || !mic_len) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001613 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG,
1614 "PTK not valid when sending EAPOL-Key "
1615 "frame");
1616 os_free(hdr);
1617 return;
1618 }
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001619
Roshan Pius3a1667e2018-07-03 15:17:14 -07001620 if (wpa_eapol_key_mic(sm->PTK.kck, sm->PTK.kck_len,
1621 sm->wpa_key_mgmt, version,
1622 (u8 *) hdr, len, key_mic) < 0) {
1623 os_free(hdr);
1624 return;
1625 }
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07001626#ifdef CONFIG_TESTING_OPTIONS
1627 if (!pairwise &&
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07001628 wpa_auth->conf.corrupt_gtk_rekey_mic_probability > 0.0 &&
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07001629 drand48() <
1630 wpa_auth->conf.corrupt_gtk_rekey_mic_probability) {
1631 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
1632 "Corrupting group EAPOL-Key Key MIC");
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001633 key_mic[0]++;
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07001634 }
1635#endif /* CONFIG_TESTING_OPTIONS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001636 }
1637
1638 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_inc_EapolFramesTx,
1639 1);
1640 wpa_auth_send_eapol(wpa_auth, sm->addr, (u8 *) hdr, len,
1641 sm->pairwise_set);
1642 os_free(hdr);
1643}
1644
1645
1646static void wpa_send_eapol(struct wpa_authenticator *wpa_auth,
1647 struct wpa_state_machine *sm, int key_info,
1648 const u8 *key_rsc, const u8 *nonce,
1649 const u8 *kde, size_t kde_len,
1650 int keyidx, int encr)
1651{
1652 int timeout_ms;
1653 int pairwise = key_info & WPA_KEY_INFO_KEY_TYPE;
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08001654 u32 ctr;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001655
1656 if (sm == NULL)
1657 return;
1658
1659 __wpa_send_eapol(wpa_auth, sm, key_info, key_rsc, nonce, kde, kde_len,
1660 keyidx, encr, 0);
1661
1662 ctr = pairwise ? sm->TimeoutCtr : sm->GTimeoutCtr;
1663 if (ctr == 1 && wpa_auth->conf.tx_status)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001664 timeout_ms = pairwise ? eapol_key_timeout_first :
1665 eapol_key_timeout_first_group;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001666 else
1667 timeout_ms = eapol_key_timeout_subseq;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001668 if (wpa_auth->conf.wpa_disable_eapol_key_retries &&
1669 (!pairwise || (key_info & WPA_KEY_INFO_MIC)))
1670 timeout_ms = eapol_key_timeout_no_retrans;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001671 if (pairwise && ctr == 1 && !(key_info & WPA_KEY_INFO_MIC))
1672 sm->pending_1_of_4_timeout = 1;
Hai Shalom74f70d42019-02-11 14:42:39 -08001673#ifdef TEST_FUZZ
1674 timeout_ms = 1;
1675#endif /* TEST_FUZZ */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001676 wpa_printf(MSG_DEBUG, "WPA: Use EAPOL-Key timeout of %u ms (retry "
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08001677 "counter %u)", timeout_ms, ctr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001678 eloop_register_timeout(timeout_ms / 1000, (timeout_ms % 1000) * 1000,
1679 wpa_send_eapol_timeout, wpa_auth, sm);
1680}
1681
1682
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001683static int wpa_verify_key_mic(int akmp, size_t pmk_len, struct wpa_ptk *PTK,
1684 u8 *data, size_t data_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001685{
1686 struct ieee802_1x_hdr *hdr;
1687 struct wpa_eapol_key *key;
1688 u16 key_info;
1689 int ret = 0;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001690 u8 mic[WPA_EAPOL_KEY_MIC_MAX_LEN], *mic_pos;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001691 size_t mic_len = wpa_mic_len(akmp, pmk_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001692
1693 if (data_len < sizeof(*hdr) + sizeof(*key))
1694 return -1;
1695
1696 hdr = (struct ieee802_1x_hdr *) data;
1697 key = (struct wpa_eapol_key *) (hdr + 1);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001698 mic_pos = (u8 *) (key + 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001699 key_info = WPA_GET_BE16(key->key_info);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001700 os_memcpy(mic, mic_pos, mic_len);
1701 os_memset(mic_pos, 0, mic_len);
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001702 if (wpa_eapol_key_mic(PTK->kck, PTK->kck_len, akmp,
1703 key_info & WPA_KEY_INFO_TYPE_MASK,
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001704 data, data_len, mic_pos) ||
1705 os_memcmp_const(mic, mic_pos, mic_len) != 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001706 ret = -1;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001707 os_memcpy(mic_pos, mic, mic_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001708 return ret;
1709}
1710
1711
1712void wpa_remove_ptk(struct wpa_state_machine *sm)
1713{
1714 sm->PTK_valid = FALSE;
1715 os_memset(&sm->PTK, 0, sizeof(sm->PTK));
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001716 if (wpa_auth_set_key(sm->wpa_auth, 0, WPA_ALG_NONE, sm->addr, 0, NULL,
1717 0))
1718 wpa_printf(MSG_DEBUG,
1719 "RSN: PTK removal from the driver failed");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001720 sm->pairwise_set = FALSE;
1721 eloop_cancel_timeout(wpa_rekey_ptk, sm->wpa_auth, sm);
1722}
1723
1724
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001725int wpa_auth_sm_event(struct wpa_state_machine *sm, enum wpa_event event)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001726{
1727 int remove_ptk = 1;
1728
1729 if (sm == NULL)
1730 return -1;
1731
1732 wpa_auth_vlogger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
1733 "event %d notification", event);
1734
1735 switch (event) {
1736 case WPA_AUTH:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001737#ifdef CONFIG_MESH
1738 /* PTKs are derived through AMPE */
1739 if (wpa_auth_start_ampe(sm->wpa_auth, sm->addr)) {
1740 /* not mesh */
1741 break;
1742 }
1743 return 0;
1744#endif /* CONFIG_MESH */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001745 case WPA_ASSOC:
1746 break;
1747 case WPA_DEAUTH:
1748 case WPA_DISASSOC:
1749 sm->DeauthenticationRequest = TRUE;
Hai Shalom1dc4d202019-04-29 16:22:27 -07001750#ifdef CONFIG_IEEE80211R_AP
1751 os_memset(sm->PMK, 0, sizeof(sm->PMK));
1752 sm->pmk_len = 0;
1753 os_memset(sm->xxkey, 0, sizeof(sm->xxkey));
1754 sm->xxkey_len = 0;
Hai Shalom81f62d82019-07-22 12:10:00 -07001755 os_memset(sm->pmk_r1, 0, sizeof(sm->pmk_r1));
1756 sm->pmk_r1_len = 0;
Hai Shalom1dc4d202019-04-29 16:22:27 -07001757#endif /* CONFIG_IEEE80211R_AP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001758 break;
1759 case WPA_REAUTH:
1760 case WPA_REAUTH_EAPOL:
1761 if (!sm->started) {
1762 /*
1763 * When using WPS, we may end up here if the STA
1764 * manages to re-associate without the previous STA
1765 * entry getting removed. Consequently, we need to make
1766 * sure that the WPA state machines gets initialized
1767 * properly at this point.
1768 */
1769 wpa_printf(MSG_DEBUG, "WPA state machine had not been "
1770 "started - initialize now");
1771 sm->started = 1;
1772 sm->Init = TRUE;
1773 if (wpa_sm_step(sm) == 1)
1774 return 1; /* should not really happen */
1775 sm->Init = FALSE;
1776 sm->AuthenticationRequest = TRUE;
1777 break;
1778 }
1779 if (sm->GUpdateStationKeys) {
1780 /*
1781 * Reauthentication cancels the pending group key
1782 * update for this STA.
1783 */
1784 sm->group->GKeyDoneStations--;
1785 sm->GUpdateStationKeys = FALSE;
1786 sm->PtkGroupInit = TRUE;
1787 }
1788 sm->ReAuthenticationRequest = TRUE;
1789 break;
1790 case WPA_ASSOC_FT:
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001791#ifdef CONFIG_IEEE80211R_AP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001792 wpa_printf(MSG_DEBUG, "FT: Retry PTK configuration "
1793 "after association");
1794 wpa_ft_install_ptk(sm);
1795
1796 /* Using FT protocol, not WPA auth state machine */
1797 sm->ft_completed = 1;
Hai Shalom81f62d82019-07-22 12:10:00 -07001798 wpa_auth_set_ptk_rekey_timer(sm);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001799 return 0;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001800#else /* CONFIG_IEEE80211R_AP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001801 break;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001802#endif /* CONFIG_IEEE80211R_AP */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001803 case WPA_ASSOC_FILS:
1804#ifdef CONFIG_FILS
1805 wpa_printf(MSG_DEBUG,
1806 "FILS: TK configuration after association");
1807 fils_set_tk(sm);
1808 sm->fils_completed = 1;
1809 return 0;
1810#else /* CONFIG_FILS */
1811 break;
1812#endif /* CONFIG_FILS */
Mathy Vanhoeff6e1f662017-07-14 15:15:35 +02001813 case WPA_DRV_STA_REMOVED:
1814 sm->tk_already_set = FALSE;
1815 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001816 }
1817
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001818#ifdef CONFIG_IEEE80211R_AP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001819 sm->ft_completed = 0;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001820#endif /* CONFIG_IEEE80211R_AP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001821
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001822 if (sm->mgmt_frame_prot && event == WPA_AUTH)
1823 remove_ptk = 0;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001824#ifdef CONFIG_FILS
1825 if (wpa_key_mgmt_fils(sm->wpa_key_mgmt) &&
1826 (event == WPA_AUTH || event == WPA_ASSOC))
1827 remove_ptk = 0;
1828#endif /* CONFIG_FILS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001829
1830 if (remove_ptk) {
1831 sm->PTK_valid = FALSE;
1832 os_memset(&sm->PTK, 0, sizeof(sm->PTK));
1833
1834 if (event != WPA_REAUTH_EAPOL)
1835 wpa_remove_ptk(sm);
1836 }
1837
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001838 if (sm->in_step_loop) {
1839 /*
1840 * wpa_sm_step() is already running - avoid recursive call to
1841 * it by making the existing loop process the new update.
1842 */
1843 sm->changed = TRUE;
1844 return 0;
1845 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001846 return wpa_sm_step(sm);
1847}
1848
1849
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001850SM_STATE(WPA_PTK, INITIALIZE)
1851{
1852 SM_ENTRY_MA(WPA_PTK, INITIALIZE, wpa_ptk);
1853 if (sm->Init) {
1854 /* Init flag is not cleared here, so avoid busy
1855 * loop by claiming nothing changed. */
1856 sm->changed = FALSE;
1857 }
1858
1859 sm->keycount = 0;
1860 if (sm->GUpdateStationKeys)
1861 sm->group->GKeyDoneStations--;
1862 sm->GUpdateStationKeys = FALSE;
1863 if (sm->wpa == WPA_VERSION_WPA)
1864 sm->PInitAKeys = FALSE;
1865 if (1 /* Unicast cipher supported AND (ESS OR ((IBSS or WDS) and
1866 * Local AA > Remote AA)) */) {
1867 sm->Pair = TRUE;
1868 }
1869 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portEnabled, 0);
1870 wpa_remove_ptk(sm);
1871 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portValid, 0);
1872 sm->TimeoutCtr = 0;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001873 if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt) ||
1874 sm->wpa_key_mgmt == WPA_KEY_MGMT_DPP ||
1875 sm->wpa_key_mgmt == WPA_KEY_MGMT_OWE) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001876 wpa_auth_set_eapol(sm->wpa_auth, sm->addr,
1877 WPA_EAPOL_authorized, 0);
1878 }
1879}
1880
1881
1882SM_STATE(WPA_PTK, DISCONNECT)
1883{
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001884 u16 reason = sm->disconnect_reason;
1885
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001886 SM_ENTRY_MA(WPA_PTK, DISCONNECT, wpa_ptk);
1887 sm->Disconnect = FALSE;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07001888 sm->disconnect_reason = 0;
1889 if (!reason)
1890 reason = WLAN_REASON_PREV_AUTH_NOT_VALID;
1891 wpa_sta_disconnect(sm->wpa_auth, sm->addr, reason);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001892}
1893
1894
1895SM_STATE(WPA_PTK, DISCONNECTED)
1896{
1897 SM_ENTRY_MA(WPA_PTK, DISCONNECTED, wpa_ptk);
1898 sm->DeauthenticationRequest = FALSE;
1899}
1900
1901
1902SM_STATE(WPA_PTK, AUTHENTICATION)
1903{
1904 SM_ENTRY_MA(WPA_PTK, AUTHENTICATION, wpa_ptk);
1905 os_memset(&sm->PTK, 0, sizeof(sm->PTK));
1906 sm->PTK_valid = FALSE;
1907 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portControl_Auto,
1908 1);
1909 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portEnabled, 1);
1910 sm->AuthenticationRequest = FALSE;
1911}
1912
1913
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001914static void wpa_group_ensure_init(struct wpa_authenticator *wpa_auth,
1915 struct wpa_group *group)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001916{
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001917 if (group->first_sta_seen)
1918 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001919 /*
1920 * System has run bit further than at the time hostapd was started
1921 * potentially very early during boot up. This provides better chances
1922 * of collecting more randomness on embedded systems. Re-initialize the
1923 * GMK and Counter here to improve their strength if there was not
1924 * enough entropy available immediately after system startup.
1925 */
1926 wpa_printf(MSG_DEBUG, "WPA: Re-initialize GMK/Counter on first "
1927 "station");
1928 if (random_pool_ready() != 1) {
1929 wpa_printf(MSG_INFO, "WPA: Not enough entropy in random pool "
1930 "to proceed - reject first 4-way handshake");
1931 group->reject_4way_hs_for_entropy = TRUE;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001932 } else {
1933 group->first_sta_seen = TRUE;
1934 group->reject_4way_hs_for_entropy = FALSE;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001935 }
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001936
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001937 if (wpa_group_init_gmk_and_counter(wpa_auth, group) < 0 ||
1938 wpa_gtk_update(wpa_auth, group) < 0 ||
1939 wpa_group_config_group_keys(wpa_auth, group) < 0) {
1940 wpa_printf(MSG_INFO, "WPA: GMK/GTK setup failed");
1941 group->first_sta_seen = FALSE;
1942 group->reject_4way_hs_for_entropy = TRUE;
1943 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001944}
1945
1946
1947SM_STATE(WPA_PTK, AUTHENTICATION2)
1948{
1949 SM_ENTRY_MA(WPA_PTK, AUTHENTICATION2, wpa_ptk);
1950
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001951 wpa_group_ensure_init(sm->wpa_auth, sm->group);
Dmitry Shmidt444d5672013-04-01 13:08:44 -07001952 sm->ReAuthenticationRequest = FALSE;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001953
Dmitry Shmidt04949592012-07-19 12:16:46 -07001954 /*
1955 * Definition of ANonce selection in IEEE Std 802.11i-2004 is somewhat
1956 * ambiguous. The Authenticator state machine uses a counter that is
1957 * incremented by one for each 4-way handshake. However, the security
1958 * analysis of 4-way handshake points out that unpredictable nonces
1959 * help in preventing precomputation attacks. Instead of the state
1960 * machine definition, use an unpredictable nonce value here to provide
1961 * stronger protection against potential precomputation attacks.
1962 */
1963 if (random_get_bytes(sm->ANonce, WPA_NONCE_LEN)) {
1964 wpa_printf(MSG_ERROR, "WPA: Failed to get random data for "
1965 "ANonce.");
Dmitry Shmidt444d5672013-04-01 13:08:44 -07001966 sm->Disconnect = TRUE;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001967 return;
1968 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001969 wpa_hexdump(MSG_DEBUG, "WPA: Assign ANonce", sm->ANonce,
1970 WPA_NONCE_LEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001971 /* IEEE 802.11i does not clear TimeoutCtr here, but this is more
1972 * logical place than INITIALIZE since AUTHENTICATION2 can be
1973 * re-entered on ReAuthenticationRequest without going through
1974 * INITIALIZE. */
1975 sm->TimeoutCtr = 0;
1976}
1977
1978
Jouni Malinen1420a892017-10-01 12:32:57 +03001979static int wpa_auth_sm_ptk_update(struct wpa_state_machine *sm)
1980{
1981 if (random_get_bytes(sm->ANonce, WPA_NONCE_LEN)) {
1982 wpa_printf(MSG_ERROR,
1983 "WPA: Failed to get random data for ANonce");
1984 sm->Disconnect = TRUE;
1985 return -1;
1986 }
1987 wpa_hexdump(MSG_DEBUG, "WPA: Assign new ANonce", sm->ANonce,
1988 WPA_NONCE_LEN);
1989 sm->TimeoutCtr = 0;
1990 return 0;
1991}
1992
1993
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001994SM_STATE(WPA_PTK, INITPMK)
1995{
1996 u8 msk[2 * PMK_LEN];
1997 size_t len = 2 * PMK_LEN;
1998
1999 SM_ENTRY_MA(WPA_PTK, INITPMK, wpa_ptk);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002000#ifdef CONFIG_IEEE80211R_AP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002001 sm->xxkey_len = 0;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002002#endif /* CONFIG_IEEE80211R_AP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002003 if (sm->pmksa) {
2004 wpa_printf(MSG_DEBUG, "WPA: PMK from PMKSA cache");
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002005 os_memcpy(sm->PMK, sm->pmksa->pmk, sm->pmksa->pmk_len);
2006 sm->pmk_len = sm->pmksa->pmk_len;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002007#ifdef CONFIG_DPP
2008 } else if (sm->wpa_key_mgmt == WPA_KEY_MGMT_DPP) {
2009 wpa_printf(MSG_DEBUG,
2010 "DPP: No PMKSA cache entry for STA - reject connection");
2011 sm->Disconnect = TRUE;
2012 sm->disconnect_reason = WLAN_REASON_INVALID_PMKID;
2013 return;
2014#endif /* CONFIG_DPP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002015 } else if (wpa_auth_get_msk(sm->wpa_auth, sm->addr, msk, &len) == 0) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002016 unsigned int pmk_len;
2017
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08002018 if (wpa_key_mgmt_sha384(sm->wpa_key_mgmt))
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002019 pmk_len = PMK_LEN_SUITE_B_192;
2020 else
2021 pmk_len = PMK_LEN;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002022 wpa_printf(MSG_DEBUG, "WPA: PMK from EAPOL state machine "
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002023 "(MSK len=%lu PMK len=%u)", (unsigned long) len,
2024 pmk_len);
2025 if (len < pmk_len) {
2026 wpa_printf(MSG_DEBUG,
2027 "WPA: MSK not long enough (%u) to create PMK (%u)",
2028 (unsigned int) len, (unsigned int) pmk_len);
2029 sm->Disconnect = TRUE;
2030 return;
2031 }
2032 os_memcpy(sm->PMK, msk, pmk_len);
2033 sm->pmk_len = pmk_len;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002034#ifdef CONFIG_IEEE80211R_AP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002035 if (len >= 2 * PMK_LEN) {
Roshan Pius3a1667e2018-07-03 15:17:14 -07002036 if (wpa_key_mgmt_sha384(sm->wpa_key_mgmt)) {
2037 os_memcpy(sm->xxkey, msk, SHA384_MAC_LEN);
2038 sm->xxkey_len = SHA384_MAC_LEN;
2039 } else {
2040 os_memcpy(sm->xxkey, msk + PMK_LEN, PMK_LEN);
2041 sm->xxkey_len = PMK_LEN;
2042 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002043 }
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002044#endif /* CONFIG_IEEE80211R_AP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002045 } else {
Dmitry Shmidt807291d2015-01-27 13:40:23 -08002046 wpa_printf(MSG_DEBUG, "WPA: Could not get PMK, get_msk: %p",
Paul Stewart092955c2017-02-06 09:13:09 -08002047 sm->wpa_auth->cb->get_msk);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08002048 sm->Disconnect = TRUE;
2049 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002050 }
Hai Shalom81f62d82019-07-22 12:10:00 -07002051 forced_memzero(msk, sizeof(msk));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002052
2053 sm->req_replay_counter_used = 0;
2054 /* IEEE 802.11i does not set keyRun to FALSE, but not doing this
2055 * will break reauthentication since EAPOL state machines may not be
2056 * get into AUTHENTICATING state that clears keyRun before WPA state
2057 * machine enters AUTHENTICATION2 state and goes immediately to INITPMK
2058 * state and takes PMK from the previously used AAA Key. This will
2059 * eventually fail in 4-Way Handshake because Supplicant uses PMK
2060 * derived from the new AAA Key. Setting keyRun = FALSE here seems to
2061 * be good workaround for this issue. */
2062 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_keyRun, 0);
2063}
2064
2065
2066SM_STATE(WPA_PTK, INITPSK)
2067{
2068 const u8 *psk;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002069 size_t psk_len;
2070
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002071 SM_ENTRY_MA(WPA_PTK, INITPSK, wpa_ptk);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002072 psk = wpa_auth_get_psk(sm->wpa_auth, sm->addr, sm->p2p_dev_addr, NULL,
Hai Shalom021b0b52019-04-10 11:17:58 -07002073 &psk_len, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002074 if (psk) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002075 os_memcpy(sm->PMK, psk, psk_len);
2076 sm->pmk_len = psk_len;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002077#ifdef CONFIG_IEEE80211R_AP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002078 os_memcpy(sm->xxkey, psk, PMK_LEN);
2079 sm->xxkey_len = PMK_LEN;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002080#endif /* CONFIG_IEEE80211R_AP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002081 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002082#ifdef CONFIG_SAE
2083 if (wpa_auth_uses_sae(sm) && sm->pmksa) {
2084 wpa_printf(MSG_DEBUG, "SAE: PMK from PMKSA cache");
2085 os_memcpy(sm->PMK, sm->pmksa->pmk, sm->pmksa->pmk_len);
2086 sm->pmk_len = sm->pmksa->pmk_len;
Hai Shalom021b0b52019-04-10 11:17:58 -07002087#ifdef CONFIG_IEEE80211R_AP
2088 os_memcpy(sm->xxkey, sm->pmksa->pmk, sm->pmksa->pmk_len);
2089 sm->xxkey_len = sm->pmksa->pmk_len;
2090#endif /* CONFIG_IEEE80211R_AP */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002091 }
2092#endif /* CONFIG_SAE */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002093 sm->req_replay_counter_used = 0;
2094}
2095
2096
2097SM_STATE(WPA_PTK, PTKSTART)
2098{
2099 u8 buf[2 + RSN_SELECTOR_LEN + PMKID_LEN], *pmkid = NULL;
2100 size_t pmkid_len = 0;
2101
2102 SM_ENTRY_MA(WPA_PTK, PTKSTART, wpa_ptk);
2103 sm->PTKRequest = FALSE;
2104 sm->TimeoutEvt = FALSE;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002105 sm->alt_snonce_valid = FALSE;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002106
2107 sm->TimeoutCtr++;
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08002108 if (sm->TimeoutCtr > sm->wpa_auth->conf.wpa_pairwise_update_count) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002109 /* No point in sending the EAPOL-Key - we will disconnect
2110 * immediately following this. */
2111 return;
2112 }
2113
2114 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
2115 "sending 1/4 msg of 4-Way Handshake");
2116 /*
Hai Shalomce48b4a2018-09-05 11:41:35 -07002117 * For infrastructure BSS cases, it is better for the AP not to include
2118 * the PMKID KDE in EAPOL-Key msg 1/4 since it could be used to initiate
2119 * offline search for the passphrase/PSK without having to be able to
2120 * capture a 4-way handshake from a STA that has access to the network.
2121 *
2122 * For IBSS cases, addition of PMKID KDE could be considered even with
2123 * WPA2-PSK cases that use multiple PSKs, but only if there is a single
2124 * possible PSK for this STA. However, this should not be done unless
2125 * there is support for using that information on the supplicant side.
2126 * The concern about exposing PMKID unnecessarily in infrastructure BSS
2127 * cases would also apply here, but at least in the IBSS case, this
2128 * would cover a potential real use case.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002129 */
2130 if (sm->wpa == WPA_VERSION_WPA2 &&
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002131 (wpa_key_mgmt_wpa_ieee8021x(sm->wpa_key_mgmt) ||
2132 (sm->wpa_key_mgmt == WPA_KEY_MGMT_OWE && sm->pmksa) ||
2133 wpa_key_mgmt_sae(sm->wpa_key_mgmt)) &&
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002134 sm->wpa_key_mgmt != WPA_KEY_MGMT_OSEN) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002135 pmkid = buf;
2136 pmkid_len = 2 + RSN_SELECTOR_LEN + PMKID_LEN;
2137 pmkid[0] = WLAN_EID_VENDOR_SPECIFIC;
2138 pmkid[1] = RSN_SELECTOR_LEN + PMKID_LEN;
2139 RSN_SELECTOR_PUT(&pmkid[2], RSN_KEY_DATA_PMKID);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002140 if (sm->pmksa) {
Roshan Pius3a1667e2018-07-03 15:17:14 -07002141 wpa_hexdump(MSG_DEBUG,
2142 "RSN: Message 1/4 PMKID from PMKSA entry",
2143 sm->pmksa->pmkid, PMKID_LEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002144 os_memcpy(&pmkid[2 + RSN_SELECTOR_LEN],
2145 sm->pmksa->pmkid, PMKID_LEN);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002146 } else if (wpa_key_mgmt_suite_b(sm->wpa_key_mgmt)) {
2147 /* No KCK available to derive PMKID */
Roshan Pius3a1667e2018-07-03 15:17:14 -07002148 wpa_printf(MSG_DEBUG,
2149 "RSN: No KCK available to derive PMKID for message 1/4");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002150 pmkid = NULL;
Hai Shalom81f62d82019-07-22 12:10:00 -07002151#ifdef CONFIG_FILS
2152 } else if (wpa_key_mgmt_fils(sm->wpa_key_mgmt)) {
2153 if (sm->pmkid_set) {
2154 wpa_hexdump(MSG_DEBUG,
2155 "RSN: Message 1/4 PMKID from FILS/ERP",
2156 sm->pmkid, PMKID_LEN);
2157 os_memcpy(&pmkid[2 + RSN_SELECTOR_LEN],
2158 sm->pmkid, PMKID_LEN);
2159 } else {
2160 /* No PMKID available */
2161 wpa_printf(MSG_DEBUG,
2162 "RSN: No FILS/ERP PMKID available for message 1/4");
2163 pmkid = NULL;
2164 }
2165#endif /* CONFIG_FILS */
2166#ifdef CONFIG_IEEE80211R_AP
2167 } else if (wpa_key_mgmt_ft(sm->wpa_key_mgmt) &&
2168 sm->ft_completed) {
2169 wpa_printf(MSG_DEBUG,
2170 "FT: No PMKID in message 1/4 when using FT protocol");
2171 pmkid = NULL;
Hai Shalom81f62d82019-07-22 12:10:00 -07002172#endif /* CONFIG_IEEE80211R_AP */
Roshan Pius3a1667e2018-07-03 15:17:14 -07002173#ifdef CONFIG_SAE
2174 } else if (wpa_key_mgmt_sae(sm->wpa_key_mgmt)) {
2175 if (sm->pmkid_set) {
2176 wpa_hexdump(MSG_DEBUG,
2177 "RSN: Message 1/4 PMKID from SAE",
2178 sm->pmkid, PMKID_LEN);
2179 os_memcpy(&pmkid[2 + RSN_SELECTOR_LEN],
2180 sm->pmkid, PMKID_LEN);
2181 } else {
2182 /* No PMKID available */
2183 wpa_printf(MSG_DEBUG,
2184 "RSN: No SAE PMKID available for message 1/4");
2185 pmkid = NULL;
2186 }
2187#endif /* CONFIG_SAE */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002188 } else {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002189 /*
2190 * Calculate PMKID since no PMKSA cache entry was
2191 * available with pre-calculated PMKID.
2192 */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002193 rsn_pmkid(sm->PMK, sm->pmk_len, sm->wpa_auth->addr,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002194 sm->addr, &pmkid[2 + RSN_SELECTOR_LEN],
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002195 sm->wpa_key_mgmt);
Roshan Pius3a1667e2018-07-03 15:17:14 -07002196 wpa_hexdump(MSG_DEBUG,
2197 "RSN: Message 1/4 PMKID derived from PMK",
2198 &pmkid[2 + RSN_SELECTOR_LEN], PMKID_LEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002199 }
2200 }
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08002201 if (!pmkid)
2202 pmkid_len = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002203 wpa_send_eapol(sm->wpa_auth, sm,
2204 WPA_KEY_INFO_ACK | WPA_KEY_INFO_KEY_TYPE, NULL,
2205 sm->ANonce, pmkid, pmkid_len, 0, 0);
2206}
2207
2208
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002209static int wpa_derive_ptk(struct wpa_state_machine *sm, const u8 *snonce,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002210 const u8 *pmk, unsigned int pmk_len,
2211 struct wpa_ptk *ptk)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002212{
Hai Shalom021b0b52019-04-10 11:17:58 -07002213 const u8 *z = NULL;
2214 size_t z_len = 0;
2215
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002216#ifdef CONFIG_IEEE80211R_AP
Hai Shalom81f62d82019-07-22 12:10:00 -07002217 if (wpa_key_mgmt_ft(sm->wpa_key_mgmt)) {
2218 if (sm->ft_completed) {
2219 u8 ptk_name[WPA_PMK_NAME_LEN];
2220
2221 return wpa_pmk_r1_to_ptk(sm->pmk_r1, sm->pmk_r1_len,
2222 sm->SNonce, sm->ANonce,
2223 sm->addr, sm->wpa_auth->addr,
2224 sm->pmk_r1_name,
2225 ptk, ptk_name,
2226 sm->wpa_key_mgmt,
2227 sm->pairwise);
2228 }
2229 return wpa_auth_derive_ptk_ft(sm, ptk);
2230 }
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002231#endif /* CONFIG_IEEE80211R_AP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002232
Hai Shalom021b0b52019-04-10 11:17:58 -07002233#ifdef CONFIG_DPP2
2234 if (sm->wpa_key_mgmt == WPA_KEY_MGMT_DPP && sm->dpp_z) {
2235 z = wpabuf_head(sm->dpp_z);
2236 z_len = wpabuf_len(sm->dpp_z);
2237 }
2238#endif /* CONFIG_DPP2 */
2239
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002240 return wpa_pmk_to_ptk(pmk, pmk_len, "Pairwise key expansion",
Dmitry Shmidt807291d2015-01-27 13:40:23 -08002241 sm->wpa_auth->addr, sm->addr, sm->ANonce, snonce,
Hai Shalom021b0b52019-04-10 11:17:58 -07002242 ptk, sm->wpa_key_mgmt, sm->pairwise, z, z_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002243}
2244
2245
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002246#ifdef CONFIG_FILS
2247
2248int fils_auth_pmk_to_ptk(struct wpa_state_machine *sm, const u8 *pmk,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002249 size_t pmk_len, const u8 *snonce, const u8 *anonce,
2250 const u8 *dhss, size_t dhss_len,
2251 struct wpabuf *g_sta, struct wpabuf *g_ap)
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002252{
2253 u8 ick[FILS_ICK_MAX_LEN];
2254 size_t ick_len;
2255 int res;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002256 u8 fils_ft[FILS_FT_MAX_LEN];
2257 size_t fils_ft_len = 0;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002258
2259 res = fils_pmk_to_ptk(pmk, pmk_len, sm->addr, sm->wpa_auth->addr,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002260 snonce, anonce, dhss, dhss_len,
2261 &sm->PTK, ick, &ick_len,
2262 sm->wpa_key_mgmt, sm->pairwise,
2263 fils_ft, &fils_ft_len);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002264 if (res < 0)
2265 return res;
2266 sm->PTK_valid = TRUE;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002267 sm->tk_already_set = FALSE;
2268
2269#ifdef CONFIG_IEEE80211R_AP
2270 if (fils_ft_len) {
2271 struct wpa_authenticator *wpa_auth = sm->wpa_auth;
2272 struct wpa_auth_config *conf = &wpa_auth->conf;
Roshan Pius3a1667e2018-07-03 15:17:14 -07002273 u8 pmk_r0[PMK_LEN_MAX], pmk_r0_name[WPA_PMK_NAME_LEN];
2274 int use_sha384 = wpa_key_mgmt_sha384(sm->wpa_key_mgmt);
2275 size_t pmk_r0_len = use_sha384 ? SHA384_MAC_LEN : PMK_LEN;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002276
2277 if (wpa_derive_pmk_r0(fils_ft, fils_ft_len,
2278 conf->ssid, conf->ssid_len,
2279 conf->mobility_domain,
2280 conf->r0_key_holder,
2281 conf->r0_key_holder_len,
Roshan Pius3a1667e2018-07-03 15:17:14 -07002282 sm->addr, pmk_r0, pmk_r0_name,
2283 use_sha384) < 0)
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002284 return -1;
2285
Roshan Pius3a1667e2018-07-03 15:17:14 -07002286 wpa_hexdump_key(MSG_DEBUG, "FILS+FT: PMK-R0",
2287 pmk_r0, pmk_r0_len);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002288 wpa_hexdump(MSG_DEBUG, "FILS+FT: PMKR0Name",
2289 pmk_r0_name, WPA_PMK_NAME_LEN);
Roshan Pius3a1667e2018-07-03 15:17:14 -07002290 wpa_ft_store_pmk_fils(sm, pmk_r0, pmk_r0_name);
Hai Shalom81f62d82019-07-22 12:10:00 -07002291 forced_memzero(fils_ft, sizeof(fils_ft));
Hai Shalom021b0b52019-04-10 11:17:58 -07002292
2293 res = wpa_derive_pmk_r1_name(pmk_r0_name, conf->r1_key_holder,
2294 sm->addr, sm->pmk_r1_name,
2295 use_sha384);
Hai Shalom81f62d82019-07-22 12:10:00 -07002296 forced_memzero(pmk_r0, PMK_LEN_MAX);
Hai Shalom021b0b52019-04-10 11:17:58 -07002297 if (res < 0)
2298 return -1;
2299 wpa_hexdump(MSG_DEBUG, "FILS+FT: PMKR1Name", sm->pmk_r1_name,
2300 WPA_PMK_NAME_LEN);
2301 sm->pmk_r1_name_valid = 1;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002302 }
2303#endif /* CONFIG_IEEE80211R_AP */
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002304
2305 res = fils_key_auth_sk(ick, ick_len, snonce, anonce,
2306 sm->addr, sm->wpa_auth->addr,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002307 g_sta ? wpabuf_head(g_sta) : NULL,
2308 g_sta ? wpabuf_len(g_sta) : 0,
2309 g_ap ? wpabuf_head(g_ap) : NULL,
2310 g_ap ? wpabuf_len(g_ap) : 0,
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002311 sm->wpa_key_mgmt, sm->fils_key_auth_sta,
2312 sm->fils_key_auth_ap,
2313 &sm->fils_key_auth_len);
Hai Shalom81f62d82019-07-22 12:10:00 -07002314 forced_memzero(ick, sizeof(ick));
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002315
2316 /* Store nonces for (Re)Association Request/Response frame processing */
2317 os_memcpy(sm->SNonce, snonce, FILS_NONCE_LEN);
2318 os_memcpy(sm->ANonce, anonce, FILS_NONCE_LEN);
2319
2320 return res;
2321}
2322
2323
2324static int wpa_aead_decrypt(struct wpa_state_machine *sm, struct wpa_ptk *ptk,
2325 u8 *buf, size_t buf_len, u16 *_key_data_len)
2326{
2327 struct ieee802_1x_hdr *hdr;
2328 struct wpa_eapol_key *key;
2329 u8 *pos;
2330 u16 key_data_len;
2331 u8 *tmp;
2332 const u8 *aad[1];
2333 size_t aad_len[1];
2334
2335 hdr = (struct ieee802_1x_hdr *) buf;
2336 key = (struct wpa_eapol_key *) (hdr + 1);
2337 pos = (u8 *) (key + 1);
2338 key_data_len = WPA_GET_BE16(pos);
2339 if (key_data_len < AES_BLOCK_SIZE ||
2340 key_data_len > buf_len - sizeof(*hdr) - sizeof(*key) - 2) {
2341 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_INFO,
2342 "No room for AES-SIV data in the frame");
2343 return -1;
2344 }
2345 pos += 2; /* Pointing at the Encrypted Key Data field */
2346
2347 tmp = os_malloc(key_data_len);
2348 if (!tmp)
2349 return -1;
2350
2351 /* AES-SIV AAD from EAPOL protocol version field (inclusive) to
2352 * to Key Data (exclusive). */
2353 aad[0] = buf;
2354 aad_len[0] = pos - buf;
2355 if (aes_siv_decrypt(ptk->kek, ptk->kek_len, pos, key_data_len,
2356 1, aad, aad_len, tmp) < 0) {
2357 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_INFO,
2358 "Invalid AES-SIV data in the frame");
2359 bin_clear_free(tmp, key_data_len);
2360 return -1;
2361 }
2362
2363 /* AEAD decryption and validation completed successfully */
2364 key_data_len -= AES_BLOCK_SIZE;
2365 wpa_hexdump_key(MSG_DEBUG, "WPA: Decrypted Key Data",
2366 tmp, key_data_len);
2367
2368 /* Replace Key Data field with the decrypted version */
2369 os_memcpy(pos, tmp, key_data_len);
2370 pos -= 2; /* Key Data Length field */
2371 WPA_PUT_BE16(pos, key_data_len);
2372 bin_clear_free(tmp, key_data_len);
2373 if (_key_data_len)
2374 *_key_data_len = key_data_len;
2375 return 0;
2376}
2377
2378
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002379const u8 * wpa_fils_validate_fils_session(struct wpa_state_machine *sm,
2380 const u8 *ies, size_t ies_len,
2381 const u8 *fils_session)
2382{
2383 const u8 *ie, *end;
2384 const u8 *session = NULL;
2385
2386 if (!wpa_key_mgmt_fils(sm->wpa_key_mgmt)) {
2387 wpa_printf(MSG_DEBUG,
2388 "FILS: Not a FILS AKM - reject association");
2389 return NULL;
2390 }
2391
2392 /* Verify Session element */
2393 ie = ies;
2394 end = ((const u8 *) ie) + ies_len;
2395 while (ie + 1 < end) {
2396 if (ie + 2 + ie[1] > end)
2397 break;
2398 if (ie[0] == WLAN_EID_EXTENSION &&
2399 ie[1] >= 1 + FILS_SESSION_LEN &&
2400 ie[2] == WLAN_EID_EXT_FILS_SESSION) {
2401 session = ie;
2402 break;
2403 }
2404 ie += 2 + ie[1];
2405 }
2406
2407 if (!session) {
2408 wpa_printf(MSG_DEBUG,
2409 "FILS: %s: Could not find FILS Session element in Assoc Req - reject",
2410 __func__);
2411 return NULL;
2412 }
2413
2414 if (!fils_session) {
2415 wpa_printf(MSG_DEBUG,
2416 "FILS: %s: Could not find FILS Session element in STA entry - reject",
2417 __func__);
2418 return NULL;
2419 }
2420
2421 if (os_memcmp(fils_session, session + 3, FILS_SESSION_LEN) != 0) {
2422 wpa_printf(MSG_DEBUG, "FILS: Session mismatch");
2423 wpa_hexdump(MSG_DEBUG, "FILS: Expected FILS Session",
2424 fils_session, FILS_SESSION_LEN);
2425 wpa_hexdump(MSG_DEBUG, "FILS: Received FILS Session",
2426 session + 3, FILS_SESSION_LEN);
2427 return NULL;
2428 }
2429 return session;
2430}
2431
2432
2433int wpa_fils_validate_key_confirm(struct wpa_state_machine *sm, const u8 *ies,
2434 size_t ies_len)
2435{
2436 struct ieee802_11_elems elems;
2437
2438 if (ieee802_11_parse_elems(ies, ies_len, &elems, 1) == ParseFailed) {
2439 wpa_printf(MSG_DEBUG,
2440 "FILS: Failed to parse decrypted elements");
2441 return -1;
2442 }
2443
2444 if (!elems.fils_session) {
2445 wpa_printf(MSG_DEBUG, "FILS: No FILS Session element");
2446 return -1;
2447 }
2448
2449 if (!elems.fils_key_confirm) {
2450 wpa_printf(MSG_DEBUG, "FILS: No FILS Key Confirm element");
2451 return -1;
2452 }
2453
2454 if (elems.fils_key_confirm_len != sm->fils_key_auth_len) {
2455 wpa_printf(MSG_DEBUG,
2456 "FILS: Unexpected Key-Auth length %d (expected %d)",
2457 elems.fils_key_confirm_len,
2458 (int) sm->fils_key_auth_len);
2459 return -1;
2460 }
2461
2462 if (os_memcmp(elems.fils_key_confirm, sm->fils_key_auth_sta,
2463 sm->fils_key_auth_len) != 0) {
2464 wpa_printf(MSG_DEBUG, "FILS: Key-Auth mismatch");
2465 wpa_hexdump(MSG_DEBUG, "FILS: Received Key-Auth",
2466 elems.fils_key_confirm, elems.fils_key_confirm_len);
2467 wpa_hexdump(MSG_DEBUG, "FILS: Expected Key-Auth",
2468 sm->fils_key_auth_sta, sm->fils_key_auth_len);
2469 return -1;
2470 }
2471
2472 return 0;
2473}
2474
2475
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002476int fils_decrypt_assoc(struct wpa_state_machine *sm, const u8 *fils_session,
2477 const struct ieee80211_mgmt *mgmt, size_t frame_len,
2478 u8 *pos, size_t left)
2479{
2480 u16 fc, stype;
2481 const u8 *end, *ie_start, *ie, *session, *crypt;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002482 const u8 *aad[5];
2483 size_t aad_len[5];
2484
2485 if (!sm || !sm->PTK_valid) {
2486 wpa_printf(MSG_DEBUG,
2487 "FILS: No KEK to decrypt Assocication Request frame");
2488 return -1;
2489 }
2490
2491 if (!wpa_key_mgmt_fils(sm->wpa_key_mgmt)) {
2492 wpa_printf(MSG_DEBUG,
2493 "FILS: Not a FILS AKM - reject association");
2494 return -1;
2495 }
2496
2497 end = ((const u8 *) mgmt) + frame_len;
2498 fc = le_to_host16(mgmt->frame_control);
2499 stype = WLAN_FC_GET_STYPE(fc);
2500 if (stype == WLAN_FC_STYPE_REASSOC_REQ)
2501 ie_start = mgmt->u.reassoc_req.variable;
2502 else
2503 ie_start = mgmt->u.assoc_req.variable;
2504 ie = ie_start;
2505
2506 /*
2507 * Find FILS Session element which is the last unencrypted element in
2508 * the frame.
2509 */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002510 session = wpa_fils_validate_fils_session(sm, ie, end - ie,
2511 fils_session);
2512 if (!session) {
2513 wpa_printf(MSG_DEBUG, "FILS: Session validation failed");
2514 return -1;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002515 }
2516
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002517 crypt = session + 2 + session[1];
2518
2519 if (end - crypt < AES_BLOCK_SIZE) {
2520 wpa_printf(MSG_DEBUG,
2521 "FILS: Too short frame to include AES-SIV data");
2522 return -1;
2523 }
2524
2525 /* AES-SIV AAD vectors */
2526
2527 /* The STA's MAC address */
2528 aad[0] = mgmt->sa;
2529 aad_len[0] = ETH_ALEN;
2530 /* The AP's BSSID */
2531 aad[1] = mgmt->da;
2532 aad_len[1] = ETH_ALEN;
2533 /* The STA's nonce */
2534 aad[2] = sm->SNonce;
2535 aad_len[2] = FILS_NONCE_LEN;
2536 /* The AP's nonce */
2537 aad[3] = sm->ANonce;
2538 aad_len[3] = FILS_NONCE_LEN;
2539 /*
2540 * The (Re)Association Request frame from the Capability Information
2541 * field to the FILS Session element (both inclusive).
2542 */
2543 aad[4] = (const u8 *) &mgmt->u.assoc_req.capab_info;
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08002544 aad_len[4] = crypt - aad[4];
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002545
2546 if (aes_siv_decrypt(sm->PTK.kek, sm->PTK.kek_len, crypt, end - crypt,
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08002547 5, aad, aad_len, pos + (crypt - ie_start)) < 0) {
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002548 wpa_printf(MSG_DEBUG,
2549 "FILS: Invalid AES-SIV data in the frame");
2550 return -1;
2551 }
2552 wpa_hexdump(MSG_DEBUG, "FILS: Decrypted Association Request elements",
2553 pos, left - AES_BLOCK_SIZE);
2554
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002555 if (wpa_fils_validate_key_confirm(sm, pos, left - AES_BLOCK_SIZE) < 0) {
2556 wpa_printf(MSG_DEBUG, "FILS: Key Confirm validation failed");
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002557 return -1;
2558 }
2559
2560 return left - AES_BLOCK_SIZE;
2561}
2562
2563
2564int fils_encrypt_assoc(struct wpa_state_machine *sm, u8 *buf,
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08002565 size_t current_len, size_t max_len,
2566 const struct wpabuf *hlp)
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002567{
2568 u8 *end = buf + max_len;
2569 u8 *pos = buf + current_len;
2570 struct ieee80211_mgmt *mgmt;
2571 struct wpabuf *plain;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002572 const u8 *aad[5];
2573 size_t aad_len[5];
2574
2575 if (!sm || !sm->PTK_valid)
2576 return -1;
2577
2578 wpa_hexdump(MSG_DEBUG,
2579 "FILS: Association Response frame before FILS processing",
2580 buf, current_len);
2581
2582 mgmt = (struct ieee80211_mgmt *) buf;
2583
2584 /* AES-SIV AAD vectors */
2585
2586 /* The AP's BSSID */
2587 aad[0] = mgmt->sa;
2588 aad_len[0] = ETH_ALEN;
2589 /* The STA's MAC address */
2590 aad[1] = mgmt->da;
2591 aad_len[1] = ETH_ALEN;
2592 /* The AP's nonce */
2593 aad[2] = sm->ANonce;
2594 aad_len[2] = FILS_NONCE_LEN;
2595 /* The STA's nonce */
2596 aad[3] = sm->SNonce;
2597 aad_len[3] = FILS_NONCE_LEN;
2598 /*
2599 * The (Re)Association Response frame from the Capability Information
2600 * field (the same offset in both Association and Reassociation
2601 * Response frames) to the FILS Session element (both inclusive).
2602 */
2603 aad[4] = (const u8 *) &mgmt->u.assoc_resp.capab_info;
2604 aad_len[4] = pos - aad[4];
2605
2606 /* The following elements will be encrypted with AES-SIV */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002607 plain = fils_prepare_plainbuf(sm, hlp);
2608 if (!plain) {
2609 wpa_printf(MSG_DEBUG, "FILS: Plain buffer prep failed");
2610 return -1;
2611 }
2612
2613 if (pos + wpabuf_len(plain) + AES_BLOCK_SIZE > end) {
2614 wpa_printf(MSG_DEBUG,
2615 "FILS: Not enough room for FILS elements");
Hai Shalom81f62d82019-07-22 12:10:00 -07002616 wpabuf_clear_free(plain);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002617 return -1;
2618 }
2619
2620 wpa_hexdump_buf_key(MSG_DEBUG, "FILS: Association Response plaintext",
2621 plain);
2622
2623 if (aes_siv_encrypt(sm->PTK.kek, sm->PTK.kek_len,
2624 wpabuf_head(plain), wpabuf_len(plain),
2625 5, aad, aad_len, pos) < 0) {
Hai Shalom81f62d82019-07-22 12:10:00 -07002626 wpabuf_clear_free(plain);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002627 return -1;
2628 }
2629
2630 wpa_hexdump(MSG_DEBUG,
2631 "FILS: Encrypted Association Response elements",
2632 pos, AES_BLOCK_SIZE + wpabuf_len(plain));
2633 current_len += wpabuf_len(plain) + AES_BLOCK_SIZE;
Hai Shalom81f62d82019-07-22 12:10:00 -07002634 wpabuf_clear_free(plain);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002635
2636 sm->fils_completed = 1;
2637
2638 return current_len;
2639}
2640
2641
2642static struct wpabuf * fils_prepare_plainbuf(struct wpa_state_machine *sm,
2643 const struct wpabuf *hlp)
2644{
2645 struct wpabuf *plain;
2646 u8 *len, *tmp, *tmp2;
2647 u8 hdr[2];
2648 u8 *gtk, dummy_gtk[32];
2649 size_t gtk_len;
2650 struct wpa_group *gsm;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002651
2652 plain = wpabuf_alloc(1000);
2653 if (!plain)
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002654 return NULL;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002655
2656 /* TODO: FILS Public Key */
2657
2658 /* FILS Key Confirmation */
2659 wpabuf_put_u8(plain, WLAN_EID_EXTENSION); /* Element ID */
2660 wpabuf_put_u8(plain, 1 + sm->fils_key_auth_len); /* Length */
2661 /* Element ID Extension */
2662 wpabuf_put_u8(plain, WLAN_EID_EXT_FILS_KEY_CONFIRM);
2663 wpabuf_put_data(plain, sm->fils_key_auth_ap, sm->fils_key_auth_len);
2664
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08002665 /* FILS HLP Container */
2666 if (hlp)
2667 wpabuf_put_buf(plain, hlp);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002668
2669 /* TODO: FILS IP Address Assignment */
2670
2671 /* Key Delivery */
2672 gsm = sm->group;
2673 wpabuf_put_u8(plain, WLAN_EID_EXTENSION); /* Element ID */
2674 len = wpabuf_put(plain, 1);
2675 wpabuf_put_u8(plain, WLAN_EID_EXT_KEY_DELIVERY);
2676 wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN,
2677 wpabuf_put(plain, WPA_KEY_RSC_LEN));
2678 /* GTK KDE */
2679 gtk = gsm->GTK[gsm->GN - 1];
2680 gtk_len = gsm->GTK_len;
Roshan Pius3a1667e2018-07-03 15:17:14 -07002681 if (sm->wpa_auth->conf.disable_gtk ||
2682 sm->wpa_key_mgmt == WPA_KEY_MGMT_OSEN) {
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002683 /*
2684 * Provide unique random GTK to each STA to prevent use
2685 * of GTK in the BSS.
2686 */
2687 if (random_get_bytes(dummy_gtk, gtk_len) < 0) {
Hai Shalom81f62d82019-07-22 12:10:00 -07002688 wpabuf_clear_free(plain);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002689 return NULL;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002690 }
2691 gtk = dummy_gtk;
2692 }
2693 hdr[0] = gsm->GN & 0x03;
2694 hdr[1] = 0;
2695 tmp = wpabuf_put(plain, 0);
2696 tmp2 = wpa_add_kde(tmp, RSN_KEY_DATA_GROUPKEY, hdr, 2,
2697 gtk, gtk_len);
2698 wpabuf_put(plain, tmp2 - tmp);
2699
2700 /* IGTK KDE */
2701 tmp = wpabuf_put(plain, 0);
2702 tmp2 = ieee80211w_kde_add(sm, tmp);
2703 wpabuf_put(plain, tmp2 - tmp);
2704
2705 *len = (u8 *) wpabuf_put(plain, 0) - len - 1;
Hai Shalom74f70d42019-02-11 14:42:39 -08002706
2707#ifdef CONFIG_OCV
2708 if (wpa_auth_uses_ocv(sm)) {
2709 struct wpa_channel_info ci;
2710 u8 *pos;
2711
2712 if (wpa_channel_info(sm->wpa_auth, &ci) != 0) {
2713 wpa_printf(MSG_WARNING,
2714 "FILS: Failed to get channel info for OCI element");
Hai Shalom81f62d82019-07-22 12:10:00 -07002715 wpabuf_clear_free(plain);
Hai Shalom74f70d42019-02-11 14:42:39 -08002716 return NULL;
2717 }
2718
2719 pos = wpabuf_put(plain, OCV_OCI_EXTENDED_LEN);
2720 if (ocv_insert_extended_oci(&ci, pos) < 0) {
Hai Shalom81f62d82019-07-22 12:10:00 -07002721 wpabuf_clear_free(plain);
Hai Shalom74f70d42019-02-11 14:42:39 -08002722 return NULL;
2723 }
2724 }
2725#endif /* CONFIG_OCV */
2726
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002727 return plain;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002728}
2729
2730
2731int fils_set_tk(struct wpa_state_machine *sm)
2732{
2733 enum wpa_alg alg;
2734 int klen;
2735
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002736 if (!sm || !sm->PTK_valid) {
2737 wpa_printf(MSG_DEBUG, "FILS: No valid PTK available to set TK");
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002738 return -1;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002739 }
2740 if (sm->tk_already_set) {
2741 wpa_printf(MSG_DEBUG, "FILS: TK already set to the driver");
2742 return -1;
2743 }
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002744
2745 alg = wpa_cipher_to_alg(sm->pairwise);
2746 klen = wpa_cipher_key_len(sm->pairwise);
2747
2748 wpa_printf(MSG_DEBUG, "FILS: Configure TK to the driver");
2749 if (wpa_auth_set_key(sm->wpa_auth, 0, alg, sm->addr, 0,
2750 sm->PTK.tk, klen)) {
2751 wpa_printf(MSG_DEBUG, "FILS: Failed to set TK to the driver");
2752 return -1;
2753 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002754 sm->tk_already_set = TRUE;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002755
2756 return 0;
2757}
2758
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002759
2760u8 * hostapd_eid_assoc_fils_session(struct wpa_state_machine *sm, u8 *buf,
2761 const u8 *fils_session, struct wpabuf *hlp)
2762{
2763 struct wpabuf *plain;
2764 u8 *pos = buf;
2765
2766 /* FILS Session */
2767 *pos++ = WLAN_EID_EXTENSION; /* Element ID */
2768 *pos++ = 1 + FILS_SESSION_LEN; /* Length */
2769 *pos++ = WLAN_EID_EXT_FILS_SESSION; /* Element ID Extension */
2770 os_memcpy(pos, fils_session, FILS_SESSION_LEN);
2771 pos += FILS_SESSION_LEN;
2772
2773 plain = fils_prepare_plainbuf(sm, hlp);
2774 if (!plain) {
2775 wpa_printf(MSG_DEBUG, "FILS: Plain buffer prep failed");
2776 return NULL;
2777 }
2778
2779 os_memcpy(pos, wpabuf_head(plain), wpabuf_len(plain));
2780 pos += wpabuf_len(plain);
2781
2782 wpa_printf(MSG_DEBUG, "%s: plain buf_len: %u", __func__,
2783 (unsigned int) wpabuf_len(plain));
Hai Shalom81f62d82019-07-22 12:10:00 -07002784 wpabuf_clear_free(plain);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002785 sm->fils_completed = 1;
2786 return pos;
2787}
2788
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002789#endif /* CONFIG_FILS */
2790
2791
Hai Shalom74f70d42019-02-11 14:42:39 -08002792#ifdef CONFIG_OCV
2793int get_sta_tx_parameters(struct wpa_state_machine *sm, int ap_max_chanwidth,
2794 int ap_seg1_idx, int *bandwidth, int *seg1_idx)
2795{
2796 struct wpa_authenticator *wpa_auth = sm->wpa_auth;
2797
2798 if (!wpa_auth->cb->get_sta_tx_params)
2799 return -1;
2800 return wpa_auth->cb->get_sta_tx_params(wpa_auth->cb_ctx, sm->addr,
2801 ap_max_chanwidth, ap_seg1_idx,
2802 bandwidth, seg1_idx);
2803}
2804#endif /* CONFIG_OCV */
2805
2806
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002807SM_STATE(WPA_PTK, PTKCALCNEGOTIATING)
2808{
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002809 struct wpa_authenticator *wpa_auth = sm->wpa_auth;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002810 struct wpa_ptk PTK;
Dmitry Shmidtdda10c22015-03-24 16:05:01 -07002811 int ok = 0, psk_found = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002812 const u8 *pmk = NULL;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002813 size_t pmk_len;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002814 int ft;
2815 const u8 *eapol_key_ie, *key_data, *mic;
2816 u16 key_data_length;
2817 size_t mic_len, eapol_key_ie_len;
2818 struct ieee802_1x_hdr *hdr;
2819 struct wpa_eapol_key *key;
2820 struct wpa_eapol_ie_parse kde;
Hai Shalom021b0b52019-04-10 11:17:58 -07002821 int vlan_id = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002822
2823 SM_ENTRY_MA(WPA_PTK, PTKCALCNEGOTIATING, wpa_ptk);
2824 sm->EAPOLKeyReceived = FALSE;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002825 sm->update_snonce = FALSE;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002826 os_memset(&PTK, 0, sizeof(PTK));
2827
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002828 mic_len = wpa_mic_len(sm->wpa_key_mgmt, sm->pmk_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002829
2830 /* WPA with IEEE 802.1X: use the derived PMK from EAP
2831 * WPA-PSK: iterate through possible PSKs and select the one matching
2832 * the packet */
2833 for (;;) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002834 if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt) &&
2835 !wpa_key_mgmt_sae(sm->wpa_key_mgmt)) {
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07002836 pmk = wpa_auth_get_psk(sm->wpa_auth, sm->addr,
Hai Shalom021b0b52019-04-10 11:17:58 -07002837 sm->p2p_dev_addr, pmk, &pmk_len,
2838 &vlan_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002839 if (pmk == NULL)
2840 break;
Dmitry Shmidtdda10c22015-03-24 16:05:01 -07002841 psk_found = 1;
Roshan Pius3a1667e2018-07-03 15:17:14 -07002842#ifdef CONFIG_IEEE80211R_AP
2843 if (wpa_key_mgmt_ft_psk(sm->wpa_key_mgmt)) {
2844 os_memcpy(sm->xxkey, pmk, pmk_len);
2845 sm->xxkey_len = pmk_len;
2846 }
2847#endif /* CONFIG_IEEE80211R_AP */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002848 } else {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002849 pmk = sm->PMK;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002850 pmk_len = sm->pmk_len;
2851 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002852
Hai Shalom81f62d82019-07-22 12:10:00 -07002853 if ((!pmk || !pmk_len) && sm->pmksa) {
2854 wpa_printf(MSG_DEBUG, "WPA: Use PMK from PMKSA cache");
2855 pmk = sm->pmksa->pmk;
2856 pmk_len = sm->pmksa->pmk_len;
2857 }
2858
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08002859 if (wpa_derive_ptk(sm, sm->SNonce, pmk, pmk_len, &PTK) < 0)
2860 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002861
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002862 if (mic_len &&
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002863 wpa_verify_key_mic(sm->wpa_key_mgmt, pmk_len, &PTK,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002864 sm->last_rx_eapol_key,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002865 sm->last_rx_eapol_key_len) == 0) {
Hai Shalom021b0b52019-04-10 11:17:58 -07002866 if (sm->PMK != pmk) {
2867 os_memcpy(sm->PMK, pmk, pmk_len);
2868 sm->pmk_len = pmk_len;
2869 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002870 ok = 1;
2871 break;
2872 }
2873
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002874#ifdef CONFIG_FILS
2875 if (!mic_len &&
2876 wpa_aead_decrypt(sm, &PTK, sm->last_rx_eapol_key,
2877 sm->last_rx_eapol_key_len, NULL) == 0) {
2878 ok = 1;
2879 break;
2880 }
2881#endif /* CONFIG_FILS */
2882
Roshan Pius3a1667e2018-07-03 15:17:14 -07002883 if (!wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt) ||
2884 wpa_key_mgmt_sae(sm->wpa_key_mgmt))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002885 break;
2886 }
2887
2888 if (!ok) {
2889 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
2890 "invalid MIC in msg 2/4 of 4-Way Handshake");
Dmitry Shmidtdda10c22015-03-24 16:05:01 -07002891 if (psk_found)
2892 wpa_auth_psk_failure_report(sm->wpa_auth, sm->addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002893 return;
2894 }
2895
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002896 /*
2897 * Note: last_rx_eapol_key length fields have already been validated in
2898 * wpa_receive().
2899 */
2900 hdr = (struct ieee802_1x_hdr *) sm->last_rx_eapol_key;
2901 key = (struct wpa_eapol_key *) (hdr + 1);
2902 mic = (u8 *) (key + 1);
2903 key_data = mic + mic_len + 2;
2904 key_data_length = WPA_GET_BE16(mic + mic_len);
2905 if (key_data_length > sm->last_rx_eapol_key_len - sizeof(*hdr) -
2906 sizeof(*key) - mic_len - 2)
2907 return;
2908
2909 if (wpa_parse_kde_ies(key_data, key_data_length, &kde) < 0) {
2910 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_INFO,
2911 "received EAPOL-Key msg 2/4 with invalid Key Data contents");
2912 return;
2913 }
2914 if (kde.rsn_ie) {
2915 eapol_key_ie = kde.rsn_ie;
2916 eapol_key_ie_len = kde.rsn_ie_len;
2917 } else if (kde.osen) {
2918 eapol_key_ie = kde.osen;
2919 eapol_key_ie_len = kde.osen_len;
2920 } else {
2921 eapol_key_ie = kde.wpa_ie;
2922 eapol_key_ie_len = kde.wpa_ie_len;
2923 }
2924 ft = sm->wpa == WPA_VERSION_WPA2 && wpa_key_mgmt_ft(sm->wpa_key_mgmt);
2925 if (sm->wpa_ie == NULL ||
2926 wpa_compare_rsn_ie(ft, sm->wpa_ie, sm->wpa_ie_len,
2927 eapol_key_ie, eapol_key_ie_len)) {
2928 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
2929 "WPA IE from (Re)AssocReq did not match with msg 2/4");
2930 if (sm->wpa_ie) {
2931 wpa_hexdump(MSG_DEBUG, "WPA IE in AssocReq",
2932 sm->wpa_ie, sm->wpa_ie_len);
2933 }
2934 wpa_hexdump(MSG_DEBUG, "WPA IE in msg 2/4",
2935 eapol_key_ie, eapol_key_ie_len);
2936 /* MLME-DEAUTHENTICATE.request */
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002937 wpa_sta_disconnect(wpa_auth, sm->addr,
2938 WLAN_REASON_PREV_AUTH_NOT_VALID);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002939 return;
2940 }
Hai Shalomc3565922019-10-28 11:58:20 -07002941 if ((!sm->rsnxe && kde.rsnxe) ||
2942 (sm->rsnxe && !kde.rsnxe) ||
2943 (sm->rsnxe && kde.rsnxe &&
2944 (sm->rsnxe_len != kde.rsnxe_len ||
2945 os_memcmp(sm->rsnxe, kde.rsnxe, sm->rsnxe_len) != 0))) {
2946 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
2947 "RSNXE from (Re)AssocReq did not match the one in EAPOL-Key msg 2/4");
2948 wpa_hexdump(MSG_DEBUG, "RSNXE in AssocReq",
2949 sm->rsnxe, sm->rsnxe_len);
2950 wpa_hexdump(MSG_DEBUG, "RSNXE in EAPOL-Key msg 2/4",
2951 kde.rsnxe, kde.rsnxe_len);
2952 /* MLME-DEAUTHENTICATE.request */
2953 wpa_sta_disconnect(wpa_auth, sm->addr,
2954 WLAN_REASON_PREV_AUTH_NOT_VALID);
2955 return;
2956 }
Hai Shalom74f70d42019-02-11 14:42:39 -08002957#ifdef CONFIG_OCV
2958 if (wpa_auth_uses_ocv(sm)) {
2959 struct wpa_channel_info ci;
2960 int tx_chanwidth;
2961 int tx_seg1_idx;
2962
2963 if (wpa_channel_info(wpa_auth, &ci) != 0) {
2964 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
2965 "Failed to get channel info to validate received OCI in EAPOL-Key 2/4");
2966 return;
2967 }
2968
2969 if (get_sta_tx_parameters(sm,
2970 channel_width_to_int(ci.chanwidth),
2971 ci.seg1_idx, &tx_chanwidth,
2972 &tx_seg1_idx) < 0)
2973 return;
2974
2975 if (ocv_verify_tx_params(kde.oci, kde.oci_len, &ci,
2976 tx_chanwidth, tx_seg1_idx) != 0) {
2977 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
2978 ocv_errorstr);
2979 return;
2980 }
2981 }
2982#endif /* CONFIG_OCV */
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002983#ifdef CONFIG_IEEE80211R_AP
2984 if (ft && ft_check_msg_2_of_4(wpa_auth, sm, &kde) < 0) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07002985 wpa_sta_disconnect(wpa_auth, sm->addr,
2986 WLAN_REASON_PREV_AUTH_NOT_VALID);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002987 return;
2988 }
2989#endif /* CONFIG_IEEE80211R_AP */
2990#ifdef CONFIG_P2P
2991 if (kde.ip_addr_req && kde.ip_addr_req[0] &&
2992 wpa_auth->ip_pool && WPA_GET_BE32(sm->ip_addr) == 0) {
2993 int idx;
2994 wpa_printf(MSG_DEBUG,
2995 "P2P: IP address requested in EAPOL-Key exchange");
2996 idx = bitfield_get_first_zero(wpa_auth->ip_pool);
2997 if (idx >= 0) {
2998 u32 start = WPA_GET_BE32(wpa_auth->conf.ip_addr_start);
2999 bitfield_set(wpa_auth->ip_pool, idx);
3000 WPA_PUT_BE32(sm->ip_addr, start + idx);
3001 wpa_printf(MSG_DEBUG,
3002 "P2P: Assigned IP address %u.%u.%u.%u to "
3003 MACSTR, sm->ip_addr[0], sm->ip_addr[1],
3004 sm->ip_addr[2], sm->ip_addr[3],
3005 MAC2STR(sm->addr));
3006 }
3007 }
3008#endif /* CONFIG_P2P */
3009
3010#ifdef CONFIG_IEEE80211R_AP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003011 if (sm->wpa == WPA_VERSION_WPA2 && wpa_key_mgmt_ft(sm->wpa_key_mgmt)) {
3012 /*
3013 * Verify that PMKR1Name from EAPOL-Key message 2/4 matches
3014 * with the value we derived.
3015 */
Dmitry Shmidtc2817022014-07-02 10:32:10 -07003016 if (os_memcmp_const(sm->sup_pmk_r1_name, sm->pmk_r1_name,
3017 WPA_PMK_NAME_LEN) != 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003018 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
3019 "PMKR1Name mismatch in FT 4-way "
3020 "handshake");
3021 wpa_hexdump(MSG_DEBUG, "FT: PMKR1Name from "
3022 "Supplicant",
3023 sm->sup_pmk_r1_name, WPA_PMK_NAME_LEN);
3024 wpa_hexdump(MSG_DEBUG, "FT: Derived PMKR1Name",
3025 sm->pmk_r1_name, WPA_PMK_NAME_LEN);
3026 return;
3027 }
3028 }
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003029#endif /* CONFIG_IEEE80211R_AP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003030
Hai Shalom021b0b52019-04-10 11:17:58 -07003031 if (vlan_id && wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt) &&
3032 wpa_auth_update_vlan(wpa_auth, sm->addr, vlan_id) < 0) {
3033 wpa_sta_disconnect(wpa_auth, sm->addr,
3034 WLAN_REASON_PREV_AUTH_NOT_VALID);
3035 return;
3036 }
3037
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003038 sm->pending_1_of_4_timeout = 0;
3039 eloop_cancel_timeout(wpa_send_eapol_timeout, sm->wpa_auth, sm);
3040
3041 if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt)) {
3042 /* PSK may have changed from the previous choice, so update
3043 * state machine data based on whatever PSK was selected here.
3044 */
3045 os_memcpy(sm->PMK, pmk, PMK_LEN);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003046 sm->pmk_len = PMK_LEN;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003047 }
3048
3049 sm->MICVerified = TRUE;
3050
3051 os_memcpy(&sm->PTK, &PTK, sizeof(PTK));
Hai Shalom81f62d82019-07-22 12:10:00 -07003052 forced_memzero(&PTK, sizeof(PTK));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003053 sm->PTK_valid = TRUE;
3054}
3055
3056
3057SM_STATE(WPA_PTK, PTKCALCNEGOTIATING2)
3058{
3059 SM_ENTRY_MA(WPA_PTK, PTKCALCNEGOTIATING2, wpa_ptk);
3060 sm->TimeoutCtr = 0;
3061}
3062
3063
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003064static int ieee80211w_kde_len(struct wpa_state_machine *sm)
3065{
3066 if (sm->mgmt_frame_prot) {
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07003067 size_t len;
3068 len = wpa_cipher_key_len(sm->wpa_auth->conf.group_mgmt_cipher);
3069 return 2 + RSN_SELECTOR_LEN + WPA_IGTK_KDE_PREFIX_LEN + len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003070 }
3071
3072 return 0;
3073}
3074
3075
3076static u8 * ieee80211w_kde_add(struct wpa_state_machine *sm, u8 *pos)
3077{
3078 struct wpa_igtk_kde igtk;
3079 struct wpa_group *gsm = sm->group;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003080 u8 rsc[WPA_KEY_RSC_LEN];
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07003081 size_t len = wpa_cipher_key_len(sm->wpa_auth->conf.group_mgmt_cipher);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003082
3083 if (!sm->mgmt_frame_prot)
3084 return pos;
3085
3086 igtk.keyid[0] = gsm->GN_igtk;
3087 igtk.keyid[1] = 0;
3088 if (gsm->wpa_group_state != WPA_GROUP_SETKEYSDONE ||
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003089 wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN_igtk, rsc) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003090 os_memset(igtk.pn, 0, sizeof(igtk.pn));
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07003091 else
3092 os_memcpy(igtk.pn, rsc, sizeof(igtk.pn));
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07003093 os_memcpy(igtk.igtk, gsm->IGTK[gsm->GN_igtk - 4], len);
Roshan Pius3a1667e2018-07-03 15:17:14 -07003094 if (sm->wpa_auth->conf.disable_gtk ||
3095 sm->wpa_key_mgmt == WPA_KEY_MGMT_OSEN) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003096 /*
3097 * Provide unique random IGTK to each STA to prevent use of
3098 * IGTK in the BSS.
3099 */
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07003100 if (random_get_bytes(igtk.igtk, len) < 0)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003101 return pos;
3102 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003103 pos = wpa_add_kde(pos, RSN_KEY_DATA_IGTK,
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07003104 (const u8 *) &igtk, WPA_IGTK_KDE_PREFIX_LEN + len,
3105 NULL, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003106
3107 return pos;
3108}
3109
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003110
Hai Shalom74f70d42019-02-11 14:42:39 -08003111static int ocv_oci_len(struct wpa_state_machine *sm)
3112{
3113#ifdef CONFIG_OCV
3114 if (wpa_auth_uses_ocv(sm))
3115 return OCV_OCI_KDE_LEN;
3116#endif /* CONFIG_OCV */
3117 return 0;
3118}
3119
3120static int ocv_oci_add(struct wpa_state_machine *sm, u8 **argpos)
3121{
3122#ifdef CONFIG_OCV
3123 struct wpa_channel_info ci;
3124
3125 if (!wpa_auth_uses_ocv(sm))
3126 return 0;
3127
3128 if (wpa_channel_info(sm->wpa_auth, &ci) != 0) {
3129 wpa_printf(MSG_WARNING,
3130 "Failed to get channel info for OCI element");
3131 return -1;
3132 }
3133
3134 return ocv_insert_oci_kde(&ci, argpos);
3135#else /* CONFIG_OCV */
3136 return 0;
3137#endif /* CONFIG_OCV */
3138}
3139
3140
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003141SM_STATE(WPA_PTK, PTKINITNEGOTIATING)
3142{
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08003143 u8 rsc[WPA_KEY_RSC_LEN], *_rsc, *gtk, *kde = NULL, *pos, dummy_gtk[32];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003144 size_t gtk_len, kde_len;
3145 struct wpa_group *gsm = sm->group;
3146 u8 *wpa_ie;
Hai Shalomc3565922019-10-28 11:58:20 -07003147 int wpa_ie_len, secure, gtkidx, encr = 0;
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08003148 u8 *wpa_ie_buf = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003149
3150 SM_ENTRY_MA(WPA_PTK, PTKINITNEGOTIATING, wpa_ptk);
3151 sm->TimeoutEvt = FALSE;
3152
3153 sm->TimeoutCtr++;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003154 if (sm->wpa_auth->conf.wpa_disable_eapol_key_retries &&
3155 sm->TimeoutCtr > 1) {
3156 /* Do not allow retransmission of EAPOL-Key msg 3/4 */
3157 return;
3158 }
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08003159 if (sm->TimeoutCtr > sm->wpa_auth->conf.wpa_pairwise_update_count) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003160 /* No point in sending the EAPOL-Key - we will disconnect
3161 * immediately following this. */
3162 return;
3163 }
3164
3165 /* Send EAPOL(1, 1, 1, Pair, P, RSC, ANonce, MIC(PTK), RSNIE, [MDIE],
3166 GTK[GN], IGTK, [FTIE], [TIE * 2])
3167 */
3168 os_memset(rsc, 0, WPA_KEY_RSC_LEN);
3169 wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN, rsc);
3170 /* If FT is used, wpa_auth->wpa_ie includes both RSNIE and MDIE */
3171 wpa_ie = sm->wpa_auth->wpa_ie;
3172 wpa_ie_len = sm->wpa_auth->wpa_ie_len;
3173 if (sm->wpa == WPA_VERSION_WPA &&
3174 (sm->wpa_auth->conf.wpa & WPA_PROTO_RSN) &&
3175 wpa_ie_len > wpa_ie[1] + 2 && wpa_ie[0] == WLAN_EID_RSN) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003176 /* WPA-only STA, remove RSN IE and possible MDIE */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003177 wpa_ie = wpa_ie + wpa_ie[1] + 2;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003178 if (wpa_ie[0] == WLAN_EID_MOBILITY_DOMAIN)
3179 wpa_ie = wpa_ie + wpa_ie[1] + 2;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003180 wpa_ie_len = wpa_ie[1] + 2;
3181 }
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08003182#ifdef CONFIG_TESTING_OPTIONS
3183 if (sm->wpa_auth->conf.rsnxe_override_eapol_len) {
3184 u8 *obuf = sm->wpa_auth->conf.rsnxe_override_eapol;
3185 size_t olen = sm->wpa_auth->conf.rsnxe_override_eapol_len;
3186 const u8 *rsnxe;
3187
3188 wpa_hexdump(MSG_DEBUG,
3189 "TESTING: wpa_ie before RSNXE EAPOL override",
3190 wpa_ie, wpa_ie_len);
3191 wpa_ie_buf = os_malloc(wpa_ie_len + olen);
3192 if (!wpa_ie_buf)
3193 return;
3194 os_memcpy(wpa_ie_buf, wpa_ie, wpa_ie_len);
3195 wpa_ie = wpa_ie_buf;
3196 rsnxe = get_ie(wpa_ie, wpa_ie_len, WLAN_EID_RSNX);
3197 if (rsnxe) {
3198 u8 rsnxe_len = 2 + rsnxe[1];
3199
3200 os_memmove((void *) rsnxe, rsnxe + rsnxe_len,
3201 wpa_ie_len - (rsnxe - wpa_ie) - rsnxe_len);
3202 wpa_ie_len -= rsnxe_len;
3203 }
3204 os_memcpy(wpa_ie + wpa_ie_len, obuf, olen);
3205 wpa_ie_len += olen;
3206 wpa_hexdump(MSG_DEBUG,
3207 "TESTING: wpa_ie after RSNXE EAPOL override",
3208 wpa_ie, wpa_ie_len);
3209 }
3210#endif /* CONFIG_TESTING_OPTIONS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003211 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
3212 "sending 3/4 msg of 4-Way Handshake");
3213 if (sm->wpa == WPA_VERSION_WPA2) {
3214 /* WPA2 send GTK in the 4-way handshake */
3215 secure = 1;
3216 gtk = gsm->GTK[gsm->GN - 1];
3217 gtk_len = gsm->GTK_len;
Roshan Pius3a1667e2018-07-03 15:17:14 -07003218 if (sm->wpa_auth->conf.disable_gtk ||
3219 sm->wpa_key_mgmt == WPA_KEY_MGMT_OSEN) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003220 /*
3221 * Provide unique random GTK to each STA to prevent use
3222 * of GTK in the BSS.
3223 */
3224 if (random_get_bytes(dummy_gtk, gtk_len) < 0)
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08003225 goto done;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003226 gtk = dummy_gtk;
3227 }
Hai Shalomc3565922019-10-28 11:58:20 -07003228 gtkidx = gsm->GN;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003229 _rsc = rsc;
3230 encr = 1;
3231 } else {
3232 /* WPA does not include GTK in msg 3/4 */
3233 secure = 0;
3234 gtk = NULL;
3235 gtk_len = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003236 _rsc = NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003237 if (sm->rx_eapol_key_secure) {
3238 /*
3239 * It looks like Windows 7 supplicant tries to use
3240 * Secure bit in msg 2/4 after having reported Michael
3241 * MIC failure and it then rejects the 4-way handshake
3242 * if msg 3/4 does not set Secure bit. Work around this
3243 * by setting the Secure bit here even in the case of
3244 * WPA if the supplicant used it first.
3245 */
3246 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
3247 "STA used Secure bit in WPA msg 2/4 - "
3248 "set Secure for 3/4 as workaround");
3249 secure = 1;
3250 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003251 }
3252
Hai Shalom74f70d42019-02-11 14:42:39 -08003253 kde_len = wpa_ie_len + ieee80211w_kde_len(sm) + ocv_oci_len(sm);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003254 if (gtk)
3255 kde_len += 2 + RSN_SELECTOR_LEN + 2 + gtk_len;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003256#ifdef CONFIG_IEEE80211R_AP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003257 if (wpa_key_mgmt_ft(sm->wpa_key_mgmt)) {
3258 kde_len += 2 + PMKID_LEN; /* PMKR1Name into RSN IE */
3259 kde_len += 300; /* FTIE + 2 * TIE */
3260 }
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003261#endif /* CONFIG_IEEE80211R_AP */
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003262#ifdef CONFIG_P2P
3263 if (WPA_GET_BE32(sm->ip_addr) > 0)
3264 kde_len += 2 + RSN_SELECTOR_LEN + 3 * 4;
3265#endif /* CONFIG_P2P */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003266 kde = os_malloc(kde_len);
3267 if (kde == NULL)
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08003268 goto done;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003269
3270 pos = kde;
3271 os_memcpy(pos, wpa_ie, wpa_ie_len);
3272 pos += wpa_ie_len;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003273#ifdef CONFIG_IEEE80211R_AP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003274 if (wpa_key_mgmt_ft(sm->wpa_key_mgmt)) {
Dmitry Shmidt55840ad2015-12-14 12:45:46 -08003275 int res;
3276 size_t elen;
3277
3278 elen = pos - kde;
3279 res = wpa_insert_pmkid(kde, &elen, sm->pmk_r1_name);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003280 if (res < 0) {
3281 wpa_printf(MSG_ERROR, "FT: Failed to insert "
3282 "PMKR1Name into RSN IE in EAPOL-Key data");
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08003283 goto done;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003284 }
Dmitry Shmidt55840ad2015-12-14 12:45:46 -08003285 pos -= wpa_ie_len;
3286 pos += elen;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003287 }
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003288#endif /* CONFIG_IEEE80211R_AP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003289 if (gtk) {
3290 u8 hdr[2];
Hai Shalomc3565922019-10-28 11:58:20 -07003291 hdr[0] = gtkidx & 0x03;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003292 hdr[1] = 0;
3293 pos = wpa_add_kde(pos, RSN_KEY_DATA_GROUPKEY, hdr, 2,
3294 gtk, gtk_len);
3295 }
3296 pos = ieee80211w_kde_add(sm, pos);
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08003297 if (ocv_oci_add(sm, &pos) < 0)
3298 goto done;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003299
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003300#ifdef CONFIG_IEEE80211R_AP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003301 if (wpa_key_mgmt_ft(sm->wpa_key_mgmt)) {
3302 int res;
3303 struct wpa_auth_config *conf;
3304
3305 conf = &sm->wpa_auth->conf;
Dmitry Shmidt55840ad2015-12-14 12:45:46 -08003306 if (sm->assoc_resp_ftie &&
3307 kde + kde_len - pos >= 2 + sm->assoc_resp_ftie[1]) {
3308 os_memcpy(pos, sm->assoc_resp_ftie,
3309 2 + sm->assoc_resp_ftie[1]);
3310 res = 2 + sm->assoc_resp_ftie[1];
3311 } else {
Roshan Pius3a1667e2018-07-03 15:17:14 -07003312 int use_sha384 = wpa_key_mgmt_sha384(sm->wpa_key_mgmt);
3313
3314 res = wpa_write_ftie(conf, use_sha384,
3315 conf->r0_key_holder,
Dmitry Shmidt55840ad2015-12-14 12:45:46 -08003316 conf->r0_key_holder_len,
3317 NULL, NULL, pos,
3318 kde + kde_len - pos,
3319 NULL, 0);
3320 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003321 if (res < 0) {
3322 wpa_printf(MSG_ERROR, "FT: Failed to insert FTIE "
3323 "into EAPOL-Key Key Data");
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08003324 goto done;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003325 }
3326 pos += res;
3327
3328 /* TIE[ReassociationDeadline] (TU) */
3329 *pos++ = WLAN_EID_TIMEOUT_INTERVAL;
3330 *pos++ = 5;
3331 *pos++ = WLAN_TIMEOUT_REASSOC_DEADLINE;
3332 WPA_PUT_LE32(pos, conf->reassociation_deadline);
3333 pos += 4;
3334
3335 /* TIE[KeyLifetime] (seconds) */
3336 *pos++ = WLAN_EID_TIMEOUT_INTERVAL;
3337 *pos++ = 5;
3338 *pos++ = WLAN_TIMEOUT_KEY_LIFETIME;
Roshan Pius3a1667e2018-07-03 15:17:14 -07003339 WPA_PUT_LE32(pos, conf->r0_key_lifetime);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003340 pos += 4;
3341 }
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003342#endif /* CONFIG_IEEE80211R_AP */
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003343#ifdef CONFIG_P2P
3344 if (WPA_GET_BE32(sm->ip_addr) > 0) {
3345 u8 addr[3 * 4];
3346 os_memcpy(addr, sm->ip_addr, 4);
3347 os_memcpy(addr + 4, sm->wpa_auth->conf.ip_addr_mask, 4);
3348 os_memcpy(addr + 8, sm->wpa_auth->conf.ip_addr_go, 4);
3349 pos = wpa_add_kde(pos, WFA_KEY_DATA_IP_ADDR_ALLOC,
3350 addr, sizeof(addr), NULL, 0);
3351 }
3352#endif /* CONFIG_P2P */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003353
3354 wpa_send_eapol(sm->wpa_auth, sm,
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003355 (secure ? WPA_KEY_INFO_SECURE : 0) |
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003356 (wpa_mic_len(sm->wpa_key_mgmt, sm->pmk_len) ?
3357 WPA_KEY_INFO_MIC : 0) |
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003358 WPA_KEY_INFO_ACK | WPA_KEY_INFO_INSTALL |
3359 WPA_KEY_INFO_KEY_TYPE,
Hai Shalomc3565922019-10-28 11:58:20 -07003360 _rsc, sm->ANonce, kde, pos - kde, 0, encr);
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08003361done:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003362 os_free(kde);
Ahmed ElArabawy0ff61c52019-12-26 12:38:39 -08003363 os_free(wpa_ie_buf);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003364}
3365
3366
3367SM_STATE(WPA_PTK, PTKINITDONE)
3368{
3369 SM_ENTRY_MA(WPA_PTK, PTKINITDONE, wpa_ptk);
3370 sm->EAPOLKeyReceived = FALSE;
3371 if (sm->Pair) {
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07003372 enum wpa_alg alg = wpa_cipher_to_alg(sm->pairwise);
3373 int klen = wpa_cipher_key_len(sm->pairwise);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003374 if (wpa_auth_set_key(sm->wpa_auth, 0, alg, sm->addr, 0,
Dmitry Shmidt807291d2015-01-27 13:40:23 -08003375 sm->PTK.tk, klen)) {
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003376 wpa_sta_disconnect(sm->wpa_auth, sm->addr,
3377 WLAN_REASON_PREV_AUTH_NOT_VALID);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003378 return;
3379 }
3380 /* FIX: MLME-SetProtection.Request(TA, Tx_Rx) */
3381 sm->pairwise_set = TRUE;
3382
Hai Shalom81f62d82019-07-22 12:10:00 -07003383 wpa_auth_set_ptk_rekey_timer(sm);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003384
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003385 if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt) ||
3386 sm->wpa_key_mgmt == WPA_KEY_MGMT_DPP ||
3387 sm->wpa_key_mgmt == WPA_KEY_MGMT_OWE) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003388 wpa_auth_set_eapol(sm->wpa_auth, sm->addr,
3389 WPA_EAPOL_authorized, 1);
3390 }
3391 }
3392
3393 if (0 /* IBSS == TRUE */) {
3394 sm->keycount++;
3395 if (sm->keycount == 2) {
3396 wpa_auth_set_eapol(sm->wpa_auth, sm->addr,
3397 WPA_EAPOL_portValid, 1);
3398 }
3399 } else {
3400 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portValid,
3401 1);
3402 }
3403 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_keyAvailable, 0);
3404 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_keyDone, 1);
3405 if (sm->wpa == WPA_VERSION_WPA)
3406 sm->PInitAKeys = TRUE;
3407 else
3408 sm->has_GTK = TRUE;
3409 wpa_auth_vlogger(sm->wpa_auth, sm->addr, LOGGER_INFO,
3410 "pairwise key handshake completed (%s)",
3411 sm->wpa == WPA_VERSION_WPA ? "WPA" : "RSN");
3412
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003413#ifdef CONFIG_IEEE80211R_AP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003414 wpa_ft_push_pmk_r1(sm->wpa_auth, sm->addr);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003415#endif /* CONFIG_IEEE80211R_AP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003416}
3417
3418
3419SM_STEP(WPA_PTK)
3420{
3421 struct wpa_authenticator *wpa_auth = sm->wpa_auth;
3422
3423 if (sm->Init)
3424 SM_ENTER(WPA_PTK, INITIALIZE);
3425 else if (sm->Disconnect
3426 /* || FIX: dot11RSNAConfigSALifetime timeout */) {
3427 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG,
3428 "WPA_PTK: sm->Disconnect");
3429 SM_ENTER(WPA_PTK, DISCONNECT);
3430 }
3431 else if (sm->DeauthenticationRequest)
3432 SM_ENTER(WPA_PTK, DISCONNECTED);
3433 else if (sm->AuthenticationRequest)
3434 SM_ENTER(WPA_PTK, AUTHENTICATION);
3435 else if (sm->ReAuthenticationRequest)
3436 SM_ENTER(WPA_PTK, AUTHENTICATION2);
Jouni Malinen1420a892017-10-01 12:32:57 +03003437 else if (sm->PTKRequest) {
3438 if (wpa_auth_sm_ptk_update(sm) < 0)
3439 SM_ENTER(WPA_PTK, DISCONNECTED);
3440 else
3441 SM_ENTER(WPA_PTK, PTKSTART);
3442 } else switch (sm->wpa_ptk_state) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003443 case WPA_PTK_INITIALIZE:
3444 break;
3445 case WPA_PTK_DISCONNECT:
3446 SM_ENTER(WPA_PTK, DISCONNECTED);
3447 break;
3448 case WPA_PTK_DISCONNECTED:
3449 SM_ENTER(WPA_PTK, INITIALIZE);
3450 break;
3451 case WPA_PTK_AUTHENTICATION:
3452 SM_ENTER(WPA_PTK, AUTHENTICATION2);
3453 break;
3454 case WPA_PTK_AUTHENTICATION2:
3455 if (wpa_key_mgmt_wpa_ieee8021x(sm->wpa_key_mgmt) &&
3456 wpa_auth_get_eapol(sm->wpa_auth, sm->addr,
3457 WPA_EAPOL_keyRun) > 0)
3458 SM_ENTER(WPA_PTK, INITPMK);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003459 else if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt) ||
3460 sm->wpa_key_mgmt == WPA_KEY_MGMT_OWE
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003461 /* FIX: && 802.1X::keyRun */)
3462 SM_ENTER(WPA_PTK, INITPSK);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003463 else if (sm->wpa_key_mgmt == WPA_KEY_MGMT_DPP)
3464 SM_ENTER(WPA_PTK, INITPMK);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003465 break;
3466 case WPA_PTK_INITPMK:
3467 if (wpa_auth_get_eapol(sm->wpa_auth, sm->addr,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003468 WPA_EAPOL_keyAvailable) > 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003469 SM_ENTER(WPA_PTK, PTKSTART);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003470#ifdef CONFIG_DPP
3471 } else if (sm->wpa_key_mgmt == WPA_KEY_MGMT_DPP && sm->pmksa) {
3472 SM_ENTER(WPA_PTK, PTKSTART);
3473#endif /* CONFIG_DPP */
3474 } else {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003475 wpa_auth->dot11RSNA4WayHandshakeFailures++;
3476 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_INFO,
3477 "INITPMK - keyAvailable = false");
3478 SM_ENTER(WPA_PTK, DISCONNECT);
3479 }
3480 break;
3481 case WPA_PTK_INITPSK:
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07003482 if (wpa_auth_get_psk(sm->wpa_auth, sm->addr, sm->p2p_dev_addr,
Hai Shalom021b0b52019-04-10 11:17:58 -07003483 NULL, NULL, NULL)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003484 SM_ENTER(WPA_PTK, PTKSTART);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003485#ifdef CONFIG_SAE
3486 } else if (wpa_auth_uses_sae(sm) && sm->pmksa) {
3487 SM_ENTER(WPA_PTK, PTKSTART);
3488#endif /* CONFIG_SAE */
3489 } else {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003490 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_INFO,
3491 "no PSK configured for the STA");
3492 wpa_auth->dot11RSNA4WayHandshakeFailures++;
3493 SM_ENTER(WPA_PTK, DISCONNECT);
3494 }
3495 break;
3496 case WPA_PTK_PTKSTART:
3497 if (sm->EAPOLKeyReceived && !sm->EAPOLKeyRequest &&
3498 sm->EAPOLKeyPairwise)
3499 SM_ENTER(WPA_PTK, PTKCALCNEGOTIATING);
3500 else if (sm->TimeoutCtr >
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08003501 sm->wpa_auth->conf.wpa_pairwise_update_count) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003502 wpa_auth->dot11RSNA4WayHandshakeFailures++;
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08003503 wpa_auth_vlogger(
3504 sm->wpa_auth, sm->addr, LOGGER_DEBUG,
3505 "PTKSTART: Retry limit %u reached",
3506 sm->wpa_auth->conf.wpa_pairwise_update_count);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003507 SM_ENTER(WPA_PTK, DISCONNECT);
3508 } else if (sm->TimeoutEvt)
3509 SM_ENTER(WPA_PTK, PTKSTART);
3510 break;
3511 case WPA_PTK_PTKCALCNEGOTIATING:
3512 if (sm->MICVerified)
3513 SM_ENTER(WPA_PTK, PTKCALCNEGOTIATING2);
3514 else if (sm->EAPOLKeyReceived && !sm->EAPOLKeyRequest &&
3515 sm->EAPOLKeyPairwise)
3516 SM_ENTER(WPA_PTK, PTKCALCNEGOTIATING);
3517 else if (sm->TimeoutEvt)
3518 SM_ENTER(WPA_PTK, PTKSTART);
3519 break;
3520 case WPA_PTK_PTKCALCNEGOTIATING2:
3521 SM_ENTER(WPA_PTK, PTKINITNEGOTIATING);
3522 break;
3523 case WPA_PTK_PTKINITNEGOTIATING:
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003524 if (sm->update_snonce)
3525 SM_ENTER(WPA_PTK, PTKCALCNEGOTIATING);
3526 else if (sm->EAPOLKeyReceived && !sm->EAPOLKeyRequest &&
3527 sm->EAPOLKeyPairwise && sm->MICVerified)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003528 SM_ENTER(WPA_PTK, PTKINITDONE);
3529 else if (sm->TimeoutCtr >
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003530 sm->wpa_auth->conf.wpa_pairwise_update_count ||
3531 (sm->wpa_auth->conf.wpa_disable_eapol_key_retries &&
3532 sm->TimeoutCtr > 1)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003533 wpa_auth->dot11RSNA4WayHandshakeFailures++;
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08003534 wpa_auth_vlogger(
3535 sm->wpa_auth, sm->addr, LOGGER_DEBUG,
3536 "PTKINITNEGOTIATING: Retry limit %u reached",
3537 sm->wpa_auth->conf.wpa_pairwise_update_count);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003538 SM_ENTER(WPA_PTK, DISCONNECT);
3539 } else if (sm->TimeoutEvt)
3540 SM_ENTER(WPA_PTK, PTKINITNEGOTIATING);
3541 break;
3542 case WPA_PTK_PTKINITDONE:
3543 break;
3544 }
3545}
3546
3547
3548SM_STATE(WPA_PTK_GROUP, IDLE)
3549{
3550 SM_ENTRY_MA(WPA_PTK_GROUP, IDLE, wpa_ptk_group);
3551 if (sm->Init) {
3552 /* Init flag is not cleared here, so avoid busy
3553 * loop by claiming nothing changed. */
3554 sm->changed = FALSE;
3555 }
3556 sm->GTimeoutCtr = 0;
3557}
3558
3559
3560SM_STATE(WPA_PTK_GROUP, REKEYNEGOTIATING)
3561{
3562 u8 rsc[WPA_KEY_RSC_LEN];
3563 struct wpa_group *gsm = sm->group;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003564 const u8 *kde;
3565 u8 *kde_buf = NULL, *pos, hdr[2];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003566 size_t kde_len;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003567 u8 *gtk, dummy_gtk[32];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003568
3569 SM_ENTRY_MA(WPA_PTK_GROUP, REKEYNEGOTIATING, wpa_ptk_group);
3570
3571 sm->GTimeoutCtr++;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003572 if (sm->wpa_auth->conf.wpa_disable_eapol_key_retries &&
3573 sm->GTimeoutCtr > 1) {
3574 /* Do not allow retransmission of EAPOL-Key group msg 1/2 */
3575 return;
3576 }
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08003577 if (sm->GTimeoutCtr > sm->wpa_auth->conf.wpa_group_update_count) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003578 /* No point in sending the EAPOL-Key - we will disconnect
3579 * immediately following this. */
3580 return;
3581 }
3582
3583 if (sm->wpa == WPA_VERSION_WPA)
3584 sm->PInitAKeys = FALSE;
3585 sm->TimeoutEvt = FALSE;
3586 /* Send EAPOL(1, 1, 1, !Pair, G, RSC, GNonce, MIC(PTK), GTK[GN]) */
3587 os_memset(rsc, 0, WPA_KEY_RSC_LEN);
3588 if (gsm->wpa_group_state == WPA_GROUP_SETKEYSDONE)
3589 wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN, rsc);
3590 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
3591 "sending 1/2 msg of Group Key Handshake");
3592
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003593 gtk = gsm->GTK[gsm->GN - 1];
Roshan Pius3a1667e2018-07-03 15:17:14 -07003594 if (sm->wpa_auth->conf.disable_gtk ||
3595 sm->wpa_key_mgmt == WPA_KEY_MGMT_OSEN) {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003596 /*
3597 * Provide unique random GTK to each STA to prevent use
3598 * of GTK in the BSS.
3599 */
3600 if (random_get_bytes(dummy_gtk, gsm->GTK_len) < 0)
3601 return;
3602 gtk = dummy_gtk;
3603 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003604 if (sm->wpa == WPA_VERSION_WPA2) {
3605 kde_len = 2 + RSN_SELECTOR_LEN + 2 + gsm->GTK_len +
Hai Shalom74f70d42019-02-11 14:42:39 -08003606 ieee80211w_kde_len(sm) + ocv_oci_len(sm);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003607 kde_buf = os_malloc(kde_len);
3608 if (kde_buf == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003609 return;
3610
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003611 kde = pos = kde_buf;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003612 hdr[0] = gsm->GN & 0x03;
3613 hdr[1] = 0;
3614 pos = wpa_add_kde(pos, RSN_KEY_DATA_GROUPKEY, hdr, 2,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003615 gtk, gsm->GTK_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003616 pos = ieee80211w_kde_add(sm, pos);
Hai Shalom74f70d42019-02-11 14:42:39 -08003617 if (ocv_oci_add(sm, &pos) < 0) {
3618 os_free(kde_buf);
3619 return;
3620 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003621 kde_len = pos - kde;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003622 } else {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003623 kde = gtk;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003624 kde_len = gsm->GTK_len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003625 }
3626
3627 wpa_send_eapol(sm->wpa_auth, sm,
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003628 WPA_KEY_INFO_SECURE |
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003629 (wpa_mic_len(sm->wpa_key_mgmt, sm->pmk_len) ?
3630 WPA_KEY_INFO_MIC : 0) |
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003631 WPA_KEY_INFO_ACK |
3632 (!sm->Pair ? WPA_KEY_INFO_INSTALL : 0),
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08003633 rsc, NULL, kde, kde_len, gsm->GN, 1);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003634
3635 os_free(kde_buf);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003636}
3637
3638
3639SM_STATE(WPA_PTK_GROUP, REKEYESTABLISHED)
3640{
Hai Shalom74f70d42019-02-11 14:42:39 -08003641#ifdef CONFIG_OCV
3642 struct wpa_authenticator *wpa_auth = sm->wpa_auth;
3643 const u8 *key_data, *mic;
3644 struct ieee802_1x_hdr *hdr;
3645 struct wpa_eapol_key *key;
3646 struct wpa_eapol_ie_parse kde;
3647 size_t mic_len;
3648 u16 key_data_length;
3649#endif /* CONFIG_OCV */
3650
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003651 SM_ENTRY_MA(WPA_PTK_GROUP, REKEYESTABLISHED, wpa_ptk_group);
3652 sm->EAPOLKeyReceived = FALSE;
Hai Shalom74f70d42019-02-11 14:42:39 -08003653
3654#ifdef CONFIG_OCV
3655 mic_len = wpa_mic_len(sm->wpa_key_mgmt, sm->pmk_len);
3656
3657 /*
3658 * Note: last_rx_eapol_key length fields have already been validated in
3659 * wpa_receive().
3660 */
3661 hdr = (struct ieee802_1x_hdr *) sm->last_rx_eapol_key;
3662 key = (struct wpa_eapol_key *) (hdr + 1);
3663 mic = (u8 *) (key + 1);
3664 key_data = mic + mic_len + 2;
3665 key_data_length = WPA_GET_BE16(mic + mic_len);
3666 if (key_data_length > sm->last_rx_eapol_key_len - sizeof(*hdr) -
3667 sizeof(*key) - mic_len - 2)
3668 return;
3669
3670 if (wpa_parse_kde_ies(key_data, key_data_length, &kde) < 0) {
3671 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_INFO,
3672 "received EAPOL-Key group msg 2/2 with invalid Key Data contents");
3673 return;
3674 }
3675
3676 if (wpa_auth_uses_ocv(sm)) {
3677 struct wpa_channel_info ci;
3678 int tx_chanwidth;
3679 int tx_seg1_idx;
3680
3681 if (wpa_channel_info(wpa_auth, &ci) != 0) {
3682 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
3683 "Failed to get channel info to validate received OCI in EAPOL-Key group 1/2");
3684 return;
3685 }
3686
3687 if (get_sta_tx_parameters(sm,
3688 channel_width_to_int(ci.chanwidth),
3689 ci.seg1_idx, &tx_chanwidth,
3690 &tx_seg1_idx) < 0)
3691 return;
3692
3693 if (ocv_verify_tx_params(kde.oci, kde.oci_len, &ci,
3694 tx_chanwidth, tx_seg1_idx) != 0) {
3695 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
3696 ocv_errorstr);
3697 return;
3698 }
3699 }
3700#endif /* CONFIG_OCV */
3701
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003702 if (sm->GUpdateStationKeys)
3703 sm->group->GKeyDoneStations--;
3704 sm->GUpdateStationKeys = FALSE;
3705 sm->GTimeoutCtr = 0;
3706 /* FIX: MLME.SetProtection.Request(TA, Tx_Rx) */
3707 wpa_auth_vlogger(sm->wpa_auth, sm->addr, LOGGER_INFO,
3708 "group key handshake completed (%s)",
3709 sm->wpa == WPA_VERSION_WPA ? "WPA" : "RSN");
3710 sm->has_GTK = TRUE;
3711}
3712
3713
3714SM_STATE(WPA_PTK_GROUP, KEYERROR)
3715{
3716 SM_ENTRY_MA(WPA_PTK_GROUP, KEYERROR, wpa_ptk_group);
3717 if (sm->GUpdateStationKeys)
3718 sm->group->GKeyDoneStations--;
3719 sm->GUpdateStationKeys = FALSE;
3720 sm->Disconnect = TRUE;
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08003721 wpa_auth_vlogger(sm->wpa_auth, sm->addr, LOGGER_INFO,
3722 "group key handshake failed (%s) after %u tries",
3723 sm->wpa == WPA_VERSION_WPA ? "WPA" : "RSN",
3724 sm->wpa_auth->conf.wpa_group_update_count);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003725}
3726
3727
3728SM_STEP(WPA_PTK_GROUP)
3729{
3730 if (sm->Init || sm->PtkGroupInit) {
3731 SM_ENTER(WPA_PTK_GROUP, IDLE);
3732 sm->PtkGroupInit = FALSE;
3733 } else switch (sm->wpa_ptk_group_state) {
3734 case WPA_PTK_GROUP_IDLE:
3735 if (sm->GUpdateStationKeys ||
3736 (sm->wpa == WPA_VERSION_WPA && sm->PInitAKeys))
3737 SM_ENTER(WPA_PTK_GROUP, REKEYNEGOTIATING);
3738 break;
3739 case WPA_PTK_GROUP_REKEYNEGOTIATING:
3740 if (sm->EAPOLKeyReceived && !sm->EAPOLKeyRequest &&
3741 !sm->EAPOLKeyPairwise && sm->MICVerified)
3742 SM_ENTER(WPA_PTK_GROUP, REKEYESTABLISHED);
3743 else if (sm->GTimeoutCtr >
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003744 sm->wpa_auth->conf.wpa_group_update_count ||
3745 (sm->wpa_auth->conf.wpa_disable_eapol_key_retries &&
3746 sm->GTimeoutCtr > 1))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003747 SM_ENTER(WPA_PTK_GROUP, KEYERROR);
3748 else if (sm->TimeoutEvt)
3749 SM_ENTER(WPA_PTK_GROUP, REKEYNEGOTIATING);
3750 break;
3751 case WPA_PTK_GROUP_KEYERROR:
3752 SM_ENTER(WPA_PTK_GROUP, IDLE);
3753 break;
3754 case WPA_PTK_GROUP_REKEYESTABLISHED:
3755 SM_ENTER(WPA_PTK_GROUP, IDLE);
3756 break;
3757 }
3758}
3759
3760
3761static int wpa_gtk_update(struct wpa_authenticator *wpa_auth,
3762 struct wpa_group *group)
3763{
3764 int ret = 0;
3765
3766 os_memcpy(group->GNonce, group->Counter, WPA_NONCE_LEN);
3767 inc_byte_array(group->Counter, WPA_NONCE_LEN);
3768 if (wpa_gmk_to_gtk(group->GMK, "Group key expansion",
3769 wpa_auth->addr, group->GNonce,
3770 group->GTK[group->GN - 1], group->GTK_len) < 0)
3771 ret = -1;
3772 wpa_hexdump_key(MSG_DEBUG, "GTK",
3773 group->GTK[group->GN - 1], group->GTK_len);
3774
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003775 if (wpa_auth->conf.ieee80211w != NO_MGMT_FRAME_PROTECTION) {
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07003776 size_t len;
3777 len = wpa_cipher_key_len(wpa_auth->conf.group_mgmt_cipher);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003778 os_memcpy(group->GNonce, group->Counter, WPA_NONCE_LEN);
3779 inc_byte_array(group->Counter, WPA_NONCE_LEN);
3780 if (wpa_gmk_to_gtk(group->GMK, "IGTK key expansion",
3781 wpa_auth->addr, group->GNonce,
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07003782 group->IGTK[group->GN_igtk - 4], len) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003783 ret = -1;
3784 wpa_hexdump_key(MSG_DEBUG, "IGTK",
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07003785 group->IGTK[group->GN_igtk - 4], len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003786 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003787
3788 return ret;
3789}
3790
3791
3792static void wpa_group_gtk_init(struct wpa_authenticator *wpa_auth,
3793 struct wpa_group *group)
3794{
3795 wpa_printf(MSG_DEBUG, "WPA: group state machine entering state "
3796 "GTK_INIT (VLAN-ID %d)", group->vlan_id);
3797 group->changed = FALSE; /* GInit is not cleared here; avoid loop */
3798 group->wpa_group_state = WPA_GROUP_GTK_INIT;
3799
3800 /* GTK[0..N] = 0 */
3801 os_memset(group->GTK, 0, sizeof(group->GTK));
3802 group->GN = 1;
3803 group->GM = 2;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003804 group->GN_igtk = 4;
3805 group->GM_igtk = 5;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003806 /* GTK[GN] = CalcGTK() */
3807 wpa_gtk_update(wpa_auth, group);
3808}
3809
3810
3811static int wpa_group_update_sta(struct wpa_state_machine *sm, void *ctx)
3812{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003813 if (ctx != NULL && ctx != sm->group)
3814 return 0;
3815
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003816 if (sm->wpa_ptk_state != WPA_PTK_PTKINITDONE) {
3817 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
3818 "Not in PTKINITDONE; skip Group Key update");
3819 sm->GUpdateStationKeys = FALSE;
3820 return 0;
3821 }
3822 if (sm->GUpdateStationKeys) {
3823 /*
3824 * This should not really happen, so add a debug log entry.
3825 * Since we clear the GKeyDoneStations before the loop, the
3826 * station needs to be counted here anyway.
3827 */
3828 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
3829 "GUpdateStationKeys was already set when "
3830 "marking station for GTK rekeying");
3831 }
3832
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003833 /* Do not rekey GTK/IGTK when STA is in WNM-Sleep Mode */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003834 if (sm->is_wnmsleep)
3835 return 0;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003836
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003837 sm->group->GKeyDoneStations++;
3838 sm->GUpdateStationKeys = TRUE;
3839
3840 wpa_sm_step(sm);
3841 return 0;
3842}
3843
3844
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003845#ifdef CONFIG_WNM_AP
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003846/* update GTK when exiting WNM-Sleep Mode */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003847void wpa_wnmsleep_rekey_gtk(struct wpa_state_machine *sm)
3848{
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003849 if (sm == NULL || sm->is_wnmsleep)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003850 return;
3851
3852 wpa_group_update_sta(sm, NULL);
3853}
3854
3855
3856void wpa_set_wnmsleep(struct wpa_state_machine *sm, int flag)
3857{
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003858 if (sm)
3859 sm->is_wnmsleep = !!flag;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003860}
3861
3862
3863int wpa_wnmsleep_gtk_subelem(struct wpa_state_machine *sm, u8 *pos)
3864{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003865 struct wpa_group *gsm = sm->group;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003866 u8 *start = pos;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003867
3868 /*
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003869 * GTK subelement:
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003870 * Sub-elem ID[1] | Length[1] | Key Info[2] | Key Length[1] | RSC[8] |
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003871 * Key[5..32]
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003872 */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003873 *pos++ = WNM_SLEEP_SUBELEM_GTK;
3874 *pos++ = 11 + gsm->GTK_len;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003875 /* Key ID in B0-B1 of Key Info */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003876 WPA_PUT_LE16(pos, gsm->GN & 0x03);
3877 pos += 2;
3878 *pos++ = gsm->GTK_len;
3879 if (wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN, pos) != 0)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003880 return 0;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003881 pos += 8;
3882 os_memcpy(pos, gsm->GTK[gsm->GN - 1], gsm->GTK_len);
3883 pos += gsm->GTK_len;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003884
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003885 wpa_printf(MSG_DEBUG, "WNM: GTK Key ID %u in WNM-Sleep Mode exit",
3886 gsm->GN);
3887 wpa_hexdump_key(MSG_DEBUG, "WNM: GTK in WNM-Sleep Mode exit",
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003888 gsm->GTK[gsm->GN - 1], gsm->GTK_len);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003889
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003890 return pos - start;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003891}
3892
3893
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003894int wpa_wnmsleep_igtk_subelem(struct wpa_state_machine *sm, u8 *pos)
3895{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003896 struct wpa_group *gsm = sm->group;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003897 u8 *start = pos;
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07003898 size_t len = wpa_cipher_key_len(sm->wpa_auth->conf.group_mgmt_cipher);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003899
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003900 /*
3901 * IGTK subelement:
3902 * Sub-elem ID[1] | Length[1] | KeyID[2] | PN[6] | Key[16]
3903 */
3904 *pos++ = WNM_SLEEP_SUBELEM_IGTK;
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07003905 *pos++ = 2 + 6 + len;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003906 WPA_PUT_LE16(pos, gsm->GN_igtk);
3907 pos += 2;
3908 if (wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN_igtk, pos) != 0)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003909 return 0;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003910 pos += 6;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003911
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07003912 os_memcpy(pos, gsm->IGTK[gsm->GN_igtk - 4], len);
3913 pos += len;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003914
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003915 wpa_printf(MSG_DEBUG, "WNM: IGTK Key ID %u in WNM-Sleep Mode exit",
3916 gsm->GN_igtk);
3917 wpa_hexdump_key(MSG_DEBUG, "WNM: IGTK in WNM-Sleep Mode exit",
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07003918 gsm->IGTK[gsm->GN_igtk - 4], len);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003919
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003920 return pos - start;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003921}
Hai Shalomc3565922019-10-28 11:58:20 -07003922
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003923#endif /* CONFIG_WNM_AP */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003924
3925
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003926static void wpa_group_setkeys(struct wpa_authenticator *wpa_auth,
3927 struct wpa_group *group)
3928{
3929 int tmp;
3930
3931 wpa_printf(MSG_DEBUG, "WPA: group state machine entering state "
3932 "SETKEYS (VLAN-ID %d)", group->vlan_id);
3933 group->changed = TRUE;
3934 group->wpa_group_state = WPA_GROUP_SETKEYS;
3935 group->GTKReKey = FALSE;
3936 tmp = group->GM;
3937 group->GM = group->GN;
3938 group->GN = tmp;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003939 tmp = group->GM_igtk;
3940 group->GM_igtk = group->GN_igtk;
3941 group->GN_igtk = tmp;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003942 /* "GKeyDoneStations = GNoStations" is done in more robust way by
3943 * counting the STAs that are marked with GUpdateStationKeys instead of
3944 * including all STAs that could be in not-yet-completed state. */
3945 wpa_gtk_update(wpa_auth, group);
3946
3947 if (group->GKeyDoneStations) {
3948 wpa_printf(MSG_DEBUG, "wpa_group_setkeys: Unexpected "
3949 "GKeyDoneStations=%d when starting new GTK rekey",
3950 group->GKeyDoneStations);
3951 group->GKeyDoneStations = 0;
3952 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003953 wpa_auth_for_each_sta(wpa_auth, wpa_group_update_sta, group);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003954 wpa_printf(MSG_DEBUG, "wpa_group_setkeys: GKeyDoneStations=%d",
3955 group->GKeyDoneStations);
3956}
3957
3958
3959static int wpa_group_config_group_keys(struct wpa_authenticator *wpa_auth,
3960 struct wpa_group *group)
3961{
3962 int ret = 0;
3963
3964 if (wpa_auth_set_key(wpa_auth, group->vlan_id,
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07003965 wpa_cipher_to_alg(wpa_auth->conf.wpa_group),
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003966 broadcast_ether_addr, group->GN,
3967 group->GTK[group->GN - 1], group->GTK_len) < 0)
3968 ret = -1;
3969
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07003970 if (wpa_auth->conf.ieee80211w != NO_MGMT_FRAME_PROTECTION) {
3971 enum wpa_alg alg;
3972 size_t len;
3973
3974 alg = wpa_cipher_to_alg(wpa_auth->conf.group_mgmt_cipher);
3975 len = wpa_cipher_key_len(wpa_auth->conf.group_mgmt_cipher);
3976
3977 if (ret == 0 &&
3978 wpa_auth_set_key(wpa_auth, group->vlan_id, alg,
3979 broadcast_ether_addr, group->GN_igtk,
3980 group->IGTK[group->GN_igtk - 4], len) < 0)
3981 ret = -1;
3982 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003983
3984 return ret;
3985}
3986
3987
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003988static int wpa_group_disconnect_cb(struct wpa_state_machine *sm, void *ctx)
3989{
3990 if (sm->group == ctx) {
3991 wpa_printf(MSG_DEBUG, "WPA: Mark STA " MACSTR
3992 " for discconnection due to fatal failure",
3993 MAC2STR(sm->addr));
3994 sm->Disconnect = TRUE;
3995 }
3996
3997 return 0;
3998}
3999
4000
4001static void wpa_group_fatal_failure(struct wpa_authenticator *wpa_auth,
4002 struct wpa_group *group)
4003{
4004 wpa_printf(MSG_DEBUG, "WPA: group state machine entering state FATAL_FAILURE");
4005 group->changed = TRUE;
4006 group->wpa_group_state = WPA_GROUP_FATAL_FAILURE;
4007 wpa_auth_for_each_sta(wpa_auth, wpa_group_disconnect_cb, group);
4008}
4009
4010
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004011static int wpa_group_setkeysdone(struct wpa_authenticator *wpa_auth,
4012 struct wpa_group *group)
4013{
4014 wpa_printf(MSG_DEBUG, "WPA: group state machine entering state "
4015 "SETKEYSDONE (VLAN-ID %d)", group->vlan_id);
4016 group->changed = TRUE;
4017 group->wpa_group_state = WPA_GROUP_SETKEYSDONE;
4018
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004019 if (wpa_group_config_group_keys(wpa_auth, group) < 0) {
4020 wpa_group_fatal_failure(wpa_auth, group);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004021 return -1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004022 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004023
4024 return 0;
4025}
4026
4027
4028static void wpa_group_sm_step(struct wpa_authenticator *wpa_auth,
4029 struct wpa_group *group)
4030{
4031 if (group->GInit) {
4032 wpa_group_gtk_init(wpa_auth, group);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004033 } else if (group->wpa_group_state == WPA_GROUP_FATAL_FAILURE) {
4034 /* Do not allow group operations */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004035 } else if (group->wpa_group_state == WPA_GROUP_GTK_INIT &&
4036 group->GTKAuthenticator) {
4037 wpa_group_setkeysdone(wpa_auth, group);
4038 } else if (group->wpa_group_state == WPA_GROUP_SETKEYSDONE &&
4039 group->GTKReKey) {
4040 wpa_group_setkeys(wpa_auth, group);
4041 } else if (group->wpa_group_state == WPA_GROUP_SETKEYS) {
4042 if (group->GKeyDoneStations == 0)
4043 wpa_group_setkeysdone(wpa_auth, group);
4044 else if (group->GTKReKey)
4045 wpa_group_setkeys(wpa_auth, group);
4046 }
4047}
4048
4049
4050static int wpa_sm_step(struct wpa_state_machine *sm)
4051{
4052 if (sm == NULL)
4053 return 0;
4054
4055 if (sm->in_step_loop) {
4056 /* This should not happen, but if it does, make sure we do not
4057 * end up freeing the state machine too early by exiting the
4058 * recursive call. */
4059 wpa_printf(MSG_ERROR, "WPA: wpa_sm_step() called recursively");
4060 return 0;
4061 }
4062
4063 sm->in_step_loop = 1;
4064 do {
4065 if (sm->pending_deinit)
4066 break;
4067
4068 sm->changed = FALSE;
4069 sm->wpa_auth->group->changed = FALSE;
4070
4071 SM_STEP_RUN(WPA_PTK);
4072 if (sm->pending_deinit)
4073 break;
4074 SM_STEP_RUN(WPA_PTK_GROUP);
4075 if (sm->pending_deinit)
4076 break;
4077 wpa_group_sm_step(sm->wpa_auth, sm->group);
4078 } while (sm->changed || sm->wpa_auth->group->changed);
4079 sm->in_step_loop = 0;
4080
4081 if (sm->pending_deinit) {
4082 wpa_printf(MSG_DEBUG, "WPA: Completing pending STA state "
4083 "machine deinit for " MACSTR, MAC2STR(sm->addr));
4084 wpa_free_sta_sm(sm);
4085 return 1;
4086 }
4087 return 0;
4088}
4089
4090
4091static void wpa_sm_call_step(void *eloop_ctx, void *timeout_ctx)
4092{
4093 struct wpa_state_machine *sm = eloop_ctx;
4094 wpa_sm_step(sm);
4095}
4096
4097
4098void wpa_auth_sm_notify(struct wpa_state_machine *sm)
4099{
4100 if (sm == NULL)
4101 return;
4102 eloop_register_timeout(0, 0, wpa_sm_call_step, sm, NULL);
4103}
4104
4105
4106void wpa_gtk_rekey(struct wpa_authenticator *wpa_auth)
4107{
4108 int tmp, i;
4109 struct wpa_group *group;
4110
4111 if (wpa_auth == NULL)
4112 return;
4113
4114 group = wpa_auth->group;
4115
4116 for (i = 0; i < 2; i++) {
4117 tmp = group->GM;
4118 group->GM = group->GN;
4119 group->GN = tmp;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004120 tmp = group->GM_igtk;
4121 group->GM_igtk = group->GN_igtk;
4122 group->GN_igtk = tmp;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004123 wpa_gtk_update(wpa_auth, group);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004124 wpa_group_config_group_keys(wpa_auth, group);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004125 }
4126}
4127
4128
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07004129static const char * wpa_bool_txt(int val)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004130{
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07004131 return val ? "TRUE" : "FALSE";
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004132}
4133
4134
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004135#define RSN_SUITE "%02x-%02x-%02x-%d"
4136#define RSN_SUITE_ARG(s) \
4137((s) >> 24) & 0xff, ((s) >> 16) & 0xff, ((s) >> 8) & 0xff, (s) & 0xff
4138
4139int wpa_get_mib(struct wpa_authenticator *wpa_auth, char *buf, size_t buflen)
4140{
4141 int len = 0, ret;
4142 char pmkid_txt[PMKID_LEN * 2 + 1];
4143#ifdef CONFIG_RSN_PREAUTH
4144 const int preauth = 1;
4145#else /* CONFIG_RSN_PREAUTH */
4146 const int preauth = 0;
4147#endif /* CONFIG_RSN_PREAUTH */
4148
4149 if (wpa_auth == NULL)
4150 return len;
4151
4152 ret = os_snprintf(buf + len, buflen - len,
4153 "dot11RSNAOptionImplemented=TRUE\n"
4154 "dot11RSNAPreauthenticationImplemented=%s\n"
4155 "dot11RSNAEnabled=%s\n"
4156 "dot11RSNAPreauthenticationEnabled=%s\n",
4157 wpa_bool_txt(preauth),
4158 wpa_bool_txt(wpa_auth->conf.wpa & WPA_PROTO_RSN),
4159 wpa_bool_txt(wpa_auth->conf.rsn_preauth));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004160 if (os_snprintf_error(buflen - len, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004161 return len;
4162 len += ret;
4163
4164 wpa_snprintf_hex(pmkid_txt, sizeof(pmkid_txt),
4165 wpa_auth->dot11RSNAPMKIDUsed, PMKID_LEN);
4166
4167 ret = os_snprintf(
4168 buf + len, buflen - len,
4169 "dot11RSNAConfigVersion=%u\n"
4170 "dot11RSNAConfigPairwiseKeysSupported=9999\n"
4171 /* FIX: dot11RSNAConfigGroupCipher */
4172 /* FIX: dot11RSNAConfigGroupRekeyMethod */
4173 /* FIX: dot11RSNAConfigGroupRekeyTime */
4174 /* FIX: dot11RSNAConfigGroupRekeyPackets */
4175 "dot11RSNAConfigGroupRekeyStrict=%u\n"
4176 "dot11RSNAConfigGroupUpdateCount=%u\n"
4177 "dot11RSNAConfigPairwiseUpdateCount=%u\n"
4178 "dot11RSNAConfigGroupCipherSize=%u\n"
4179 "dot11RSNAConfigPMKLifetime=%u\n"
4180 "dot11RSNAConfigPMKReauthThreshold=%u\n"
4181 "dot11RSNAConfigNumberOfPTKSAReplayCounters=0\n"
4182 "dot11RSNAConfigSATimeout=%u\n"
4183 "dot11RSNAAuthenticationSuiteSelected=" RSN_SUITE "\n"
4184 "dot11RSNAPairwiseCipherSelected=" RSN_SUITE "\n"
4185 "dot11RSNAGroupCipherSelected=" RSN_SUITE "\n"
4186 "dot11RSNAPMKIDUsed=%s\n"
4187 "dot11RSNAAuthenticationSuiteRequested=" RSN_SUITE "\n"
4188 "dot11RSNAPairwiseCipherRequested=" RSN_SUITE "\n"
4189 "dot11RSNAGroupCipherRequested=" RSN_SUITE "\n"
4190 "dot11RSNATKIPCounterMeasuresInvoked=%u\n"
4191 "dot11RSNA4WayHandshakeFailures=%u\n"
4192 "dot11RSNAConfigNumberOfGTKSAReplayCounters=0\n",
4193 RSN_VERSION,
4194 !!wpa_auth->conf.wpa_strict_rekey,
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08004195 wpa_auth->conf.wpa_group_update_count,
4196 wpa_auth->conf.wpa_pairwise_update_count,
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07004197 wpa_cipher_key_len(wpa_auth->conf.wpa_group) * 8,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004198 dot11RSNAConfigPMKLifetime,
4199 dot11RSNAConfigPMKReauthThreshold,
4200 dot11RSNAConfigSATimeout,
4201 RSN_SUITE_ARG(wpa_auth->dot11RSNAAuthenticationSuiteSelected),
4202 RSN_SUITE_ARG(wpa_auth->dot11RSNAPairwiseCipherSelected),
4203 RSN_SUITE_ARG(wpa_auth->dot11RSNAGroupCipherSelected),
4204 pmkid_txt,
4205 RSN_SUITE_ARG(wpa_auth->dot11RSNAAuthenticationSuiteRequested),
4206 RSN_SUITE_ARG(wpa_auth->dot11RSNAPairwiseCipherRequested),
4207 RSN_SUITE_ARG(wpa_auth->dot11RSNAGroupCipherRequested),
4208 wpa_auth->dot11RSNATKIPCounterMeasuresInvoked,
4209 wpa_auth->dot11RSNA4WayHandshakeFailures);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004210 if (os_snprintf_error(buflen - len, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004211 return len;
4212 len += ret;
4213
4214 /* TODO: dot11RSNAConfigPairwiseCiphersTable */
4215 /* TODO: dot11RSNAConfigAuthenticationSuitesTable */
4216
4217 /* Private MIB */
4218 ret = os_snprintf(buf + len, buflen - len, "hostapdWPAGroupState=%d\n",
4219 wpa_auth->group->wpa_group_state);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004220 if (os_snprintf_error(buflen - len, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004221 return len;
4222 len += ret;
4223
4224 return len;
4225}
4226
4227
4228int wpa_get_mib_sta(struct wpa_state_machine *sm, char *buf, size_t buflen)
4229{
4230 int len = 0, ret;
4231 u32 pairwise = 0;
4232
4233 if (sm == NULL)
4234 return 0;
4235
4236 /* TODO: FF-FF-FF-FF-FF-FF entry for broadcast/multicast stats */
4237
4238 /* dot11RSNAStatsEntry */
4239
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07004240 pairwise = wpa_cipher_to_suite(sm->wpa == WPA_VERSION_WPA2 ?
4241 WPA_PROTO_RSN : WPA_PROTO_WPA,
4242 sm->pairwise);
4243 if (pairwise == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004244 return 0;
4245
4246 ret = os_snprintf(
4247 buf + len, buflen - len,
4248 /* TODO: dot11RSNAStatsIndex */
4249 "dot11RSNAStatsSTAAddress=" MACSTR "\n"
4250 "dot11RSNAStatsVersion=1\n"
4251 "dot11RSNAStatsSelectedPairwiseCipher=" RSN_SUITE "\n"
4252 /* TODO: dot11RSNAStatsTKIPICVErrors */
4253 "dot11RSNAStatsTKIPLocalMICFailures=%u\n"
4254 "dot11RSNAStatsTKIPRemoteMICFailures=%u\n"
4255 /* TODO: dot11RSNAStatsCCMPReplays */
4256 /* TODO: dot11RSNAStatsCCMPDecryptErrors */
4257 /* TODO: dot11RSNAStatsTKIPReplays */,
4258 MAC2STR(sm->addr),
4259 RSN_SUITE_ARG(pairwise),
4260 sm->dot11RSNAStatsTKIPLocalMICFailures,
4261 sm->dot11RSNAStatsTKIPRemoteMICFailures);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004262 if (os_snprintf_error(buflen - len, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004263 return len;
4264 len += ret;
4265
4266 /* Private MIB */
4267 ret = os_snprintf(buf + len, buflen - len,
Hai Shalomc3565922019-10-28 11:58:20 -07004268 "wpa=%d\n"
4269 "AKMSuiteSelector=" RSN_SUITE "\n"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004270 "hostapdWPAPTKState=%d\n"
4271 "hostapdWPAPTKGroupState=%d\n",
Hai Shalomc3565922019-10-28 11:58:20 -07004272 sm->wpa,
4273 RSN_SUITE_ARG(wpa_akm_to_suite(sm->wpa_key_mgmt)),
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004274 sm->wpa_ptk_state,
4275 sm->wpa_ptk_group_state);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004276 if (os_snprintf_error(buflen - len, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004277 return len;
4278 len += ret;
4279
4280 return len;
4281}
4282
4283
4284void wpa_auth_countermeasures_start(struct wpa_authenticator *wpa_auth)
4285{
4286 if (wpa_auth)
4287 wpa_auth->dot11RSNATKIPCounterMeasuresInvoked++;
4288}
4289
4290
4291int wpa_auth_pairwise_set(struct wpa_state_machine *sm)
4292{
4293 return sm && sm->pairwise_set;
4294}
4295
4296
4297int wpa_auth_get_pairwise(struct wpa_state_machine *sm)
4298{
4299 return sm->pairwise;
4300}
4301
4302
Hai Shalom74f70d42019-02-11 14:42:39 -08004303const u8 * wpa_auth_get_pmk(struct wpa_state_machine *sm, int *len)
4304{
4305 if (!sm)
4306 return NULL;
4307 *len = sm->pmk_len;
4308 return sm->PMK;
4309}
4310
4311
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004312int wpa_auth_sta_key_mgmt(struct wpa_state_machine *sm)
4313{
4314 if (sm == NULL)
4315 return -1;
4316 return sm->wpa_key_mgmt;
4317}
4318
4319
4320int wpa_auth_sta_wpa_version(struct wpa_state_machine *sm)
4321{
4322 if (sm == NULL)
4323 return 0;
4324 return sm->wpa;
4325}
4326
4327
Mathy Vanhoeff6e1f662017-07-14 15:15:35 +02004328int wpa_auth_sta_ft_tk_already_set(struct wpa_state_machine *sm)
4329{
4330 if (!sm || !wpa_key_mgmt_ft(sm->wpa_key_mgmt))
4331 return 0;
4332 return sm->tk_already_set;
4333}
4334
4335
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004336int wpa_auth_sta_fils_tk_already_set(struct wpa_state_machine *sm)
4337{
4338 if (!sm || !wpa_key_mgmt_fils(sm->wpa_key_mgmt))
4339 return 0;
4340 return sm->tk_already_set;
4341}
4342
4343
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004344int wpa_auth_sta_clear_pmksa(struct wpa_state_machine *sm,
4345 struct rsn_pmksa_cache_entry *entry)
4346{
4347 if (sm == NULL || sm->pmksa != entry)
4348 return -1;
4349 sm->pmksa = NULL;
4350 return 0;
4351}
4352
4353
4354struct rsn_pmksa_cache_entry *
4355wpa_auth_sta_get_pmksa(struct wpa_state_machine *sm)
4356{
4357 return sm ? sm->pmksa : NULL;
4358}
4359
4360
4361void wpa_auth_sta_local_mic_failure_report(struct wpa_state_machine *sm)
4362{
4363 if (sm)
4364 sm->dot11RSNAStatsTKIPLocalMICFailures++;
4365}
4366
4367
4368const u8 * wpa_auth_get_wpa_ie(struct wpa_authenticator *wpa_auth, size_t *len)
4369{
4370 if (wpa_auth == NULL)
4371 return NULL;
4372 *len = wpa_auth->wpa_ie_len;
4373 return wpa_auth->wpa_ie;
4374}
4375
4376
4377int wpa_auth_pmksa_add(struct wpa_state_machine *sm, const u8 *pmk,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004378 unsigned int pmk_len,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004379 int session_timeout, struct eapol_state_machine *eapol)
4380{
Dmitry Shmidtc55524a2011-07-07 11:18:38 -07004381 if (sm == NULL || sm->wpa != WPA_VERSION_WPA2 ||
4382 sm->wpa_auth->conf.disable_pmksa_caching)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004383 return -1;
4384
Hai Shalom81f62d82019-07-22 12:10:00 -07004385#ifdef CONFIG_IEEE80211R_AP
4386 if (pmk_len >= 2 * PMK_LEN && wpa_key_mgmt_ft(sm->wpa_key_mgmt) &&
4387 wpa_key_mgmt_wpa_ieee8021x(sm->wpa_key_mgmt) &&
4388 !wpa_key_mgmt_sha384(sm->wpa_key_mgmt)) {
4389 /* Cache MPMK/XXKey instead of initial part from MSK */
4390 pmk = pmk + PMK_LEN;
4391 pmk_len = PMK_LEN;
4392 } else
4393#endif /* CONFIG_IEEE80211R_AP */
Dmitry Shmidtebd93af2017-02-21 13:40:44 -08004394 if (wpa_key_mgmt_sha384(sm->wpa_key_mgmt)) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004395 if (pmk_len > PMK_LEN_SUITE_B_192)
4396 pmk_len = PMK_LEN_SUITE_B_192;
4397 } else if (pmk_len > PMK_LEN) {
4398 pmk_len = PMK_LEN;
4399 }
4400
Hai Shalom81f62d82019-07-22 12:10:00 -07004401 wpa_hexdump_key(MSG_DEBUG, "RSN: Cache PMK", pmk, pmk_len);
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08004402 if (pmksa_cache_auth_add(sm->wpa_auth->pmksa, pmk, pmk_len, NULL,
Dmitry Shmidt807291d2015-01-27 13:40:23 -08004403 sm->PTK.kck, sm->PTK.kck_len,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004404 sm->wpa_auth->addr, sm->addr, session_timeout,
4405 eapol, sm->wpa_key_mgmt))
4406 return 0;
4407
4408 return -1;
4409}
4410
4411
4412int wpa_auth_pmksa_add_preauth(struct wpa_authenticator *wpa_auth,
4413 const u8 *pmk, size_t len, const u8 *sta_addr,
4414 int session_timeout,
4415 struct eapol_state_machine *eapol)
4416{
4417 if (wpa_auth == NULL)
4418 return -1;
4419
Hai Shalom81f62d82019-07-22 12:10:00 -07004420 wpa_hexdump_key(MSG_DEBUG, "RSN: Cache PMK from preauth", pmk, len);
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08004421 if (pmksa_cache_auth_add(wpa_auth->pmksa, pmk, len, NULL,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004422 NULL, 0,
4423 wpa_auth->addr,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004424 sta_addr, session_timeout, eapol,
4425 WPA_KEY_MGMT_IEEE8021X))
4426 return 0;
4427
4428 return -1;
4429}
4430
4431
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004432int wpa_auth_pmksa_add_sae(struct wpa_authenticator *wpa_auth, const u8 *addr,
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08004433 const u8 *pmk, const u8 *pmkid)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004434{
4435 if (wpa_auth->conf.disable_pmksa_caching)
4436 return -1;
4437
Hai Shalom81f62d82019-07-22 12:10:00 -07004438 wpa_hexdump_key(MSG_DEBUG, "RSN: Cache PMK from SAE", pmk, PMK_LEN);
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08004439 if (pmksa_cache_auth_add(wpa_auth->pmksa, pmk, PMK_LEN, pmkid,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004440 NULL, 0,
4441 wpa_auth->addr, addr, 0, NULL,
4442 WPA_KEY_MGMT_SAE))
4443 return 0;
4444
4445 return -1;
4446}
4447
4448
Roshan Pius3a1667e2018-07-03 15:17:14 -07004449void wpa_auth_add_sae_pmkid(struct wpa_state_machine *sm, const u8 *pmkid)
4450{
4451 os_memcpy(sm->pmkid, pmkid, PMKID_LEN);
4452 sm->pmkid_set = 1;
4453}
4454
4455
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004456int wpa_auth_pmksa_add2(struct wpa_authenticator *wpa_auth, const u8 *addr,
4457 const u8 *pmk, size_t pmk_len, const u8 *pmkid,
4458 int session_timeout, int akmp)
4459{
4460 if (wpa_auth->conf.disable_pmksa_caching)
4461 return -1;
4462
Hai Shalom81f62d82019-07-22 12:10:00 -07004463 wpa_hexdump_key(MSG_DEBUG, "RSN: Cache PMK (2)", pmk, PMK_LEN);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004464 if (pmksa_cache_auth_add(wpa_auth->pmksa, pmk, pmk_len, pmkid,
4465 NULL, 0, wpa_auth->addr, addr, session_timeout,
4466 NULL, akmp))
4467 return 0;
4468
4469 return -1;
4470}
4471
4472
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07004473void wpa_auth_pmksa_remove(struct wpa_authenticator *wpa_auth,
4474 const u8 *sta_addr)
4475{
4476 struct rsn_pmksa_cache_entry *pmksa;
4477
4478 if (wpa_auth == NULL || wpa_auth->pmksa == NULL)
4479 return;
4480 pmksa = pmksa_cache_auth_get(wpa_auth->pmksa, sta_addr, NULL);
4481 if (pmksa) {
4482 wpa_printf(MSG_DEBUG, "WPA: Remove PMKSA cache entry for "
4483 MACSTR " based on request", MAC2STR(sta_addr));
4484 pmksa_cache_free_entry(wpa_auth->pmksa, pmksa);
4485 }
4486}
4487
4488
Dmitry Shmidte4663042016-04-04 10:07:49 -07004489int wpa_auth_pmksa_list(struct wpa_authenticator *wpa_auth, char *buf,
4490 size_t len)
4491{
4492 if (!wpa_auth || !wpa_auth->pmksa)
4493 return 0;
4494 return pmksa_cache_auth_list(wpa_auth->pmksa, buf, len);
4495}
4496
4497
4498void wpa_auth_pmksa_flush(struct wpa_authenticator *wpa_auth)
4499{
4500 if (wpa_auth && wpa_auth->pmksa)
4501 pmksa_cache_auth_flush(wpa_auth->pmksa);
4502}
4503
4504
Paul Stewart092955c2017-02-06 09:13:09 -08004505#ifdef CONFIG_PMKSA_CACHE_EXTERNAL
4506#ifdef CONFIG_MESH
4507
4508int wpa_auth_pmksa_list_mesh(struct wpa_authenticator *wpa_auth, const u8 *addr,
4509 char *buf, size_t len)
4510{
4511 if (!wpa_auth || !wpa_auth->pmksa)
4512 return 0;
4513
4514 return pmksa_cache_auth_list_mesh(wpa_auth->pmksa, addr, buf, len);
4515}
4516
4517
4518struct rsn_pmksa_cache_entry *
4519wpa_auth_pmksa_create_entry(const u8 *aa, const u8 *spa, const u8 *pmk,
4520 const u8 *pmkid, int expiration)
4521{
4522 struct rsn_pmksa_cache_entry *entry;
4523 struct os_reltime now;
4524
4525 entry = pmksa_cache_auth_create_entry(pmk, PMK_LEN, pmkid, NULL, 0, aa,
4526 spa, 0, NULL, WPA_KEY_MGMT_SAE);
4527 if (!entry)
4528 return NULL;
4529
4530 os_get_reltime(&now);
4531 entry->expiration = now.sec + expiration;
4532 return entry;
4533}
4534
4535
4536int wpa_auth_pmksa_add_entry(struct wpa_authenticator *wpa_auth,
4537 struct rsn_pmksa_cache_entry *entry)
4538{
4539 int ret;
4540
4541 if (!wpa_auth || !wpa_auth->pmksa)
4542 return -1;
4543
4544 ret = pmksa_cache_auth_add_entry(wpa_auth->pmksa, entry);
4545 if (ret < 0)
4546 wpa_printf(MSG_DEBUG,
4547 "RSN: Failed to store external PMKSA cache for "
4548 MACSTR, MAC2STR(entry->spa));
4549
4550 return ret;
4551}
4552
4553#endif /* CONFIG_MESH */
4554#endif /* CONFIG_PMKSA_CACHE_EXTERNAL */
4555
4556
Dmitry Shmidte4663042016-04-04 10:07:49 -07004557struct rsn_pmksa_cache_entry *
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004558wpa_auth_pmksa_get(struct wpa_authenticator *wpa_auth, const u8 *sta_addr,
4559 const u8 *pmkid)
Dmitry Shmidte4663042016-04-04 10:07:49 -07004560{
4561 if (!wpa_auth || !wpa_auth->pmksa)
4562 return NULL;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08004563 return pmksa_cache_auth_get(wpa_auth->pmksa, sta_addr, pmkid);
Dmitry Shmidte4663042016-04-04 10:07:49 -07004564}
4565
4566
4567void wpa_auth_pmksa_set_to_sm(struct rsn_pmksa_cache_entry *pmksa,
4568 struct wpa_state_machine *sm,
4569 struct wpa_authenticator *wpa_auth,
4570 u8 *pmkid, u8 *pmk)
4571{
4572 if (!sm)
4573 return;
4574
4575 sm->pmksa = pmksa;
4576 os_memcpy(pmk, pmksa->pmk, PMK_LEN);
4577 os_memcpy(pmkid, pmksa->pmkid, PMKID_LEN);
4578 os_memcpy(wpa_auth->dot11RSNAPMKIDUsed, pmksa->pmkid, PMKID_LEN);
4579}
4580
4581
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07004582/*
4583 * Remove and free the group from wpa_authenticator. This is triggered by a
4584 * callback to make sure nobody is currently iterating the group list while it
4585 * gets modified.
4586 */
4587static void wpa_group_free(struct wpa_authenticator *wpa_auth,
4588 struct wpa_group *group)
4589{
4590 struct wpa_group *prev = wpa_auth->group;
4591
4592 wpa_printf(MSG_DEBUG, "WPA: Remove group state machine for VLAN-ID %d",
4593 group->vlan_id);
4594
4595 while (prev) {
4596 if (prev->next == group) {
4597 /* This never frees the special first group as needed */
4598 prev->next = group->next;
4599 os_free(group);
4600 break;
4601 }
4602 prev = prev->next;
4603 }
4604
4605}
4606
4607
4608/* Increase the reference counter for group */
4609static void wpa_group_get(struct wpa_authenticator *wpa_auth,
4610 struct wpa_group *group)
4611{
4612 /* Skip the special first group */
4613 if (wpa_auth->group == group)
4614 return;
4615
4616 group->references++;
4617}
4618
4619
4620/* Decrease the reference counter and maybe free the group */
4621static void wpa_group_put(struct wpa_authenticator *wpa_auth,
4622 struct wpa_group *group)
4623{
4624 /* Skip the special first group */
4625 if (wpa_auth->group == group)
4626 return;
4627
4628 group->references--;
4629 if (group->references)
4630 return;
4631 wpa_group_free(wpa_auth, group);
4632}
4633
4634
4635/*
4636 * Add a group that has its references counter set to zero. Caller needs to
4637 * call wpa_group_get() on the return value to mark the entry in use.
4638 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004639static struct wpa_group *
4640wpa_auth_add_group(struct wpa_authenticator *wpa_auth, int vlan_id)
4641{
4642 struct wpa_group *group;
4643
4644 if (wpa_auth == NULL || wpa_auth->group == NULL)
4645 return NULL;
4646
4647 wpa_printf(MSG_DEBUG, "WPA: Add group state machine for VLAN-ID %d",
4648 vlan_id);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004649 group = wpa_group_init(wpa_auth, vlan_id, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004650 if (group == NULL)
4651 return NULL;
4652
4653 group->next = wpa_auth->group->next;
4654 wpa_auth->group->next = group;
4655
4656 return group;
4657}
4658
4659
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004660/*
4661 * Enforce that the group state machine for the VLAN is running, increase
4662 * reference counter as interface is up. References might have been increased
4663 * even if a negative value is returned.
4664 * Returns: -1 on error (group missing, group already failed); otherwise, 0
4665 */
4666int wpa_auth_ensure_group(struct wpa_authenticator *wpa_auth, int vlan_id)
4667{
4668 struct wpa_group *group;
4669
4670 if (wpa_auth == NULL)
4671 return 0;
4672
4673 group = wpa_auth->group;
4674 while (group) {
4675 if (group->vlan_id == vlan_id)
4676 break;
4677 group = group->next;
4678 }
4679
4680 if (group == NULL) {
4681 group = wpa_auth_add_group(wpa_auth, vlan_id);
4682 if (group == NULL)
4683 return -1;
4684 }
4685
4686 wpa_printf(MSG_DEBUG,
4687 "WPA: Ensure group state machine running for VLAN ID %d",
4688 vlan_id);
4689
4690 wpa_group_get(wpa_auth, group);
4691 group->num_setup_iface++;
4692
4693 if (group->wpa_group_state == WPA_GROUP_FATAL_FAILURE)
4694 return -1;
4695
4696 return 0;
4697}
4698
4699
4700/*
4701 * Decrease reference counter, expected to be zero afterwards.
4702 * returns: -1 on error (group not found, group in fail state)
4703 * -2 if wpa_group is still referenced
4704 * 0 else
4705 */
4706int wpa_auth_release_group(struct wpa_authenticator *wpa_auth, int vlan_id)
4707{
4708 struct wpa_group *group;
4709 int ret = 0;
4710
4711 if (wpa_auth == NULL)
4712 return 0;
4713
4714 group = wpa_auth->group;
4715 while (group) {
4716 if (group->vlan_id == vlan_id)
4717 break;
4718 group = group->next;
4719 }
4720
4721 if (group == NULL)
4722 return -1;
4723
4724 wpa_printf(MSG_DEBUG,
4725 "WPA: Try stopping group state machine for VLAN ID %d",
4726 vlan_id);
4727
4728 if (group->num_setup_iface <= 0) {
4729 wpa_printf(MSG_ERROR,
4730 "WPA: wpa_auth_release_group called more often than wpa_auth_ensure_group for VLAN ID %d, skipping.",
4731 vlan_id);
4732 return -1;
4733 }
4734 group->num_setup_iface--;
4735
4736 if (group->wpa_group_state == WPA_GROUP_FATAL_FAILURE)
4737 ret = -1;
4738
4739 if (group->references > 1) {
4740 wpa_printf(MSG_DEBUG,
4741 "WPA: Cannot stop group state machine for VLAN ID %d as references are still hold",
4742 vlan_id);
4743 ret = -2;
4744 }
4745
4746 wpa_group_put(wpa_auth, group);
4747
4748 return ret;
4749}
4750
4751
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004752int wpa_auth_sta_set_vlan(struct wpa_state_machine *sm, int vlan_id)
4753{
4754 struct wpa_group *group;
4755
4756 if (sm == NULL || sm->wpa_auth == NULL)
4757 return 0;
4758
4759 group = sm->wpa_auth->group;
4760 while (group) {
4761 if (group->vlan_id == vlan_id)
4762 break;
4763 group = group->next;
4764 }
4765
4766 if (group == NULL) {
4767 group = wpa_auth_add_group(sm->wpa_auth, vlan_id);
4768 if (group == NULL)
4769 return -1;
4770 }
4771
4772 if (sm->group == group)
4773 return 0;
4774
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004775 if (group->wpa_group_state == WPA_GROUP_FATAL_FAILURE)
4776 return -1;
4777
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004778 wpa_printf(MSG_DEBUG, "WPA: Moving STA " MACSTR " to use group state "
4779 "machine for VLAN ID %d", MAC2STR(sm->addr), vlan_id);
4780
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07004781 wpa_group_get(sm->wpa_auth, group);
4782 wpa_group_put(sm->wpa_auth, sm->group);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004783 sm->group = group;
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07004784
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004785 return 0;
4786}
4787
4788
4789void wpa_auth_eapol_key_tx_status(struct wpa_authenticator *wpa_auth,
4790 struct wpa_state_machine *sm, int ack)
4791{
4792 if (wpa_auth == NULL || sm == NULL)
4793 return;
4794 wpa_printf(MSG_DEBUG, "WPA: EAPOL-Key TX status for STA " MACSTR
4795 " ack=%d", MAC2STR(sm->addr), ack);
4796 if (sm->pending_1_of_4_timeout && ack) {
4797 /*
4798 * Some deployed supplicant implementations update their SNonce
4799 * for each EAPOL-Key 2/4 message even within the same 4-way
4800 * handshake and then fail to use the first SNonce when
4801 * deriving the PTK. This results in unsuccessful 4-way
4802 * handshake whenever the relatively short initial timeout is
4803 * reached and EAPOL-Key 1/4 is retransmitted. Try to work
4804 * around this by increasing the timeout now that we know that
4805 * the station has received the frame.
4806 */
4807 int timeout_ms = eapol_key_timeout_subseq;
4808 wpa_printf(MSG_DEBUG, "WPA: Increase initial EAPOL-Key 1/4 "
4809 "timeout by %u ms because of acknowledged frame",
4810 timeout_ms);
4811 eloop_cancel_timeout(wpa_send_eapol_timeout, wpa_auth, sm);
4812 eloop_register_timeout(timeout_ms / 1000,
4813 (timeout_ms % 1000) * 1000,
4814 wpa_send_eapol_timeout, wpa_auth, sm);
4815 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004816
4817#ifdef CONFIG_TESTING_OPTIONS
4818 if (sm->eapol_status_cb) {
4819 sm->eapol_status_cb(sm->eapol_status_cb_ctx1,
4820 sm->eapol_status_cb_ctx2);
4821 sm->eapol_status_cb = NULL;
4822 }
4823#endif /* CONFIG_TESTING_OPTIONS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004824}
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004825
4826
4827int wpa_auth_uses_sae(struct wpa_state_machine *sm)
4828{
4829 if (sm == NULL)
4830 return 0;
4831 return wpa_key_mgmt_sae(sm->wpa_key_mgmt);
4832}
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004833
4834
4835int wpa_auth_uses_ft_sae(struct wpa_state_machine *sm)
4836{
4837 if (sm == NULL)
4838 return 0;
4839 return sm->wpa_key_mgmt == WPA_KEY_MGMT_FT_SAE;
4840}
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004841
4842
4843#ifdef CONFIG_P2P
4844int wpa_auth_get_ip_addr(struct wpa_state_machine *sm, u8 *addr)
4845{
4846 if (sm == NULL || WPA_GET_BE32(sm->ip_addr) == 0)
4847 return -1;
4848 os_memcpy(addr, sm->ip_addr, 4);
4849 return 0;
4850}
4851#endif /* CONFIG_P2P */
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08004852
4853
4854int wpa_auth_radius_das_disconnect_pmksa(struct wpa_authenticator *wpa_auth,
4855 struct radius_das_attrs *attr)
4856{
4857 return pmksa_cache_auth_radius_das_disconnect(wpa_auth->pmksa, attr);
4858}
Dmitry Shmidt7f656022015-02-25 14:36:37 -08004859
4860
4861void wpa_auth_reconfig_group_keys(struct wpa_authenticator *wpa_auth)
4862{
4863 struct wpa_group *group;
4864
4865 if (!wpa_auth)
4866 return;
4867 for (group = wpa_auth->group; group; group = group->next)
4868 wpa_group_config_group_keys(wpa_auth, group);
4869}
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004870
4871
4872#ifdef CONFIG_FILS
4873
4874struct wpa_auth_fils_iter_data {
4875 struct wpa_authenticator *auth;
4876 const u8 *cache_id;
4877 struct rsn_pmksa_cache_entry *pmksa;
4878 const u8 *spa;
4879 const u8 *pmkid;
4880};
4881
4882
4883static int wpa_auth_fils_iter(struct wpa_authenticator *a, void *ctx)
4884{
4885 struct wpa_auth_fils_iter_data *data = ctx;
4886
4887 if (a == data->auth || !a->conf.fils_cache_id_set ||
4888 os_memcmp(a->conf.fils_cache_id, data->cache_id,
4889 FILS_CACHE_ID_LEN) != 0)
4890 return 0;
4891 data->pmksa = pmksa_cache_auth_get(a->pmksa, data->spa, data->pmkid);
4892 return data->pmksa != NULL;
4893}
4894
4895
4896struct rsn_pmksa_cache_entry *
4897wpa_auth_pmksa_get_fils_cache_id(struct wpa_authenticator *wpa_auth,
4898 const u8 *sta_addr, const u8 *pmkid)
4899{
4900 struct wpa_auth_fils_iter_data idata;
4901
4902 if (!wpa_auth->conf.fils_cache_id_set)
4903 return NULL;
4904 idata.auth = wpa_auth;
4905 idata.cache_id = wpa_auth->conf.fils_cache_id;
4906 idata.pmksa = NULL;
4907 idata.spa = sta_addr;
4908 idata.pmkid = pmkid;
4909 wpa_auth_for_each_auth(wpa_auth, wpa_auth_fils_iter, &idata);
4910 return idata.pmksa;
4911}
4912
4913
4914#ifdef CONFIG_IEEE80211R_AP
Roshan Pius3a1667e2018-07-03 15:17:14 -07004915int wpa_auth_write_fte(struct wpa_authenticator *wpa_auth, int use_sha384,
4916 u8 *buf, size_t len)
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004917{
4918 struct wpa_auth_config *conf = &wpa_auth->conf;
4919
Roshan Pius3a1667e2018-07-03 15:17:14 -07004920 return wpa_write_ftie(conf, use_sha384, conf->r0_key_holder,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004921 conf->r0_key_holder_len,
4922 NULL, NULL, buf, len, NULL, 0);
4923}
4924#endif /* CONFIG_IEEE80211R_AP */
4925
4926
4927void wpa_auth_get_fils_aead_params(struct wpa_state_machine *sm,
4928 u8 *fils_anonce, u8 *fils_snonce,
4929 u8 *fils_kek, size_t *fils_kek_len)
4930{
4931 os_memcpy(fils_anonce, sm->ANonce, WPA_NONCE_LEN);
4932 os_memcpy(fils_snonce, sm->SNonce, WPA_NONCE_LEN);
4933 os_memcpy(fils_kek, sm->PTK.kek, WPA_KEK_MAX_LEN);
4934 *fils_kek_len = sm->PTK.kek_len;
4935}
4936
Hai Shalom81f62d82019-07-22 12:10:00 -07004937
4938void wpa_auth_add_fils_pmk_pmkid(struct wpa_state_machine *sm, const u8 *pmk,
4939 size_t pmk_len, const u8 *pmkid)
4940{
4941 os_memcpy(sm->PMK, pmk, pmk_len);
4942 sm->pmk_len = pmk_len;
4943 os_memcpy(sm->pmkid, pmkid, PMKID_LEN);
4944 sm->pmkid_set = 1;
4945}
4946
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004947#endif /* CONFIG_FILS */
4948
4949
Hai Shalom021b0b52019-04-10 11:17:58 -07004950void wpa_auth_set_auth_alg(struct wpa_state_machine *sm, u16 auth_alg)
4951{
4952 if (sm)
4953 sm->auth_alg = auth_alg;
4954}
4955
4956
4957#ifdef CONFIG_DPP2
4958void wpa_auth_set_dpp_z(struct wpa_state_machine *sm, const struct wpabuf *z)
4959{
4960 if (sm) {
4961 wpabuf_clear_free(sm->dpp_z);
4962 sm->dpp_z = z ? wpabuf_dup(z) : NULL;
4963 }
4964}
4965#endif /* CONFIG_DPP2 */
4966
4967
Roshan Pius3a1667e2018-07-03 15:17:14 -07004968#ifdef CONFIG_TESTING_OPTIONS
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004969
4970int wpa_auth_resend_m1(struct wpa_state_machine *sm, int change_anonce,
4971 void (*cb)(void *ctx1, void *ctx2),
4972 void *ctx1, void *ctx2)
4973{
4974 const u8 *anonce = sm->ANonce;
4975 u8 anonce_buf[WPA_NONCE_LEN];
4976
4977 if (change_anonce) {
4978 if (random_get_bytes(anonce_buf, WPA_NONCE_LEN))
4979 return -1;
4980 anonce = anonce_buf;
4981 }
4982
4983 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
4984 "sending 1/4 msg of 4-Way Handshake (TESTING)");
4985 wpa_send_eapol(sm->wpa_auth, sm,
4986 WPA_KEY_INFO_ACK | WPA_KEY_INFO_KEY_TYPE, NULL,
4987 anonce, NULL, 0, 0, 0);
4988 return 0;
4989}
4990
4991
4992int wpa_auth_resend_m3(struct wpa_state_machine *sm,
4993 void (*cb)(void *ctx1, void *ctx2),
4994 void *ctx1, void *ctx2)
4995{
Roshan Pius3a1667e2018-07-03 15:17:14 -07004996 u8 rsc[WPA_KEY_RSC_LEN], *_rsc, *gtk, *kde, *pos;
Roshan Pius3a1667e2018-07-03 15:17:14 -07004997 u8 *opos;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07004998 size_t gtk_len, kde_len;
4999 struct wpa_group *gsm = sm->group;
5000 u8 *wpa_ie;
Hai Shalomc3565922019-10-28 11:58:20 -07005001 int wpa_ie_len, secure, gtkidx, encr = 0;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005002
5003 /* Send EAPOL(1, 1, 1, Pair, P, RSC, ANonce, MIC(PTK), RSNIE, [MDIE],
5004 GTK[GN], IGTK, [FTIE], [TIE * 2])
5005 */
5006
5007 /* Use 0 RSC */
5008 os_memset(rsc, 0, WPA_KEY_RSC_LEN);
5009 /* If FT is used, wpa_auth->wpa_ie includes both RSNIE and MDIE */
5010 wpa_ie = sm->wpa_auth->wpa_ie;
5011 wpa_ie_len = sm->wpa_auth->wpa_ie_len;
5012 if (sm->wpa == WPA_VERSION_WPA &&
5013 (sm->wpa_auth->conf.wpa & WPA_PROTO_RSN) &&
5014 wpa_ie_len > wpa_ie[1] + 2 && wpa_ie[0] == WLAN_EID_RSN) {
5015 /* WPA-only STA, remove RSN IE and possible MDIE */
5016 wpa_ie = wpa_ie + wpa_ie[1] + 2;
5017 if (wpa_ie[0] == WLAN_EID_MOBILITY_DOMAIN)
5018 wpa_ie = wpa_ie + wpa_ie[1] + 2;
5019 wpa_ie_len = wpa_ie[1] + 2;
5020 }
5021 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
5022 "sending 3/4 msg of 4-Way Handshake (TESTING)");
5023 if (sm->wpa == WPA_VERSION_WPA2) {
5024 /* WPA2 send GTK in the 4-way handshake */
5025 secure = 1;
5026 gtk = gsm->GTK[gsm->GN - 1];
5027 gtk_len = gsm->GTK_len;
Hai Shalomc3565922019-10-28 11:58:20 -07005028 gtkidx = gsm->GN;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005029 _rsc = rsc;
5030 encr = 1;
5031 } else {
5032 /* WPA does not include GTK in msg 3/4 */
5033 secure = 0;
5034 gtk = NULL;
5035 gtk_len = 0;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005036 _rsc = NULL;
5037 if (sm->rx_eapol_key_secure) {
5038 /*
5039 * It looks like Windows 7 supplicant tries to use
5040 * Secure bit in msg 2/4 after having reported Michael
5041 * MIC failure and it then rejects the 4-way handshake
5042 * if msg 3/4 does not set Secure bit. Work around this
5043 * by setting the Secure bit here even in the case of
5044 * WPA if the supplicant used it first.
5045 */
5046 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
5047 "STA used Secure bit in WPA msg 2/4 - "
5048 "set Secure for 3/4 as workaround");
5049 secure = 1;
5050 }
5051 }
5052
Hai Shalom74f70d42019-02-11 14:42:39 -08005053 kde_len = wpa_ie_len + ieee80211w_kde_len(sm) + ocv_oci_len(sm);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005054 if (gtk)
5055 kde_len += 2 + RSN_SELECTOR_LEN + 2 + gtk_len;
5056#ifdef CONFIG_IEEE80211R_AP
5057 if (wpa_key_mgmt_ft(sm->wpa_key_mgmt)) {
5058 kde_len += 2 + PMKID_LEN; /* PMKR1Name into RSN IE */
5059 kde_len += 300; /* FTIE + 2 * TIE */
5060 }
5061#endif /* CONFIG_IEEE80211R_AP */
5062 kde = os_malloc(kde_len);
5063 if (kde == NULL)
5064 return -1;
5065
5066 pos = kde;
5067 os_memcpy(pos, wpa_ie, wpa_ie_len);
5068 pos += wpa_ie_len;
5069#ifdef CONFIG_IEEE80211R_AP
5070 if (wpa_key_mgmt_ft(sm->wpa_key_mgmt)) {
5071 int res;
5072 size_t elen;
5073
5074 elen = pos - kde;
5075 res = wpa_insert_pmkid(kde, &elen, sm->pmk_r1_name);
5076 if (res < 0) {
5077 wpa_printf(MSG_ERROR, "FT: Failed to insert "
5078 "PMKR1Name into RSN IE in EAPOL-Key data");
5079 os_free(kde);
5080 return -1;
5081 }
5082 pos -= wpa_ie_len;
5083 pos += elen;
5084 }
5085#endif /* CONFIG_IEEE80211R_AP */
5086 if (gtk) {
5087 u8 hdr[2];
Hai Shalomc3565922019-10-28 11:58:20 -07005088 hdr[0] = gtkidx & 0x03;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005089 hdr[1] = 0;
5090 pos = wpa_add_kde(pos, RSN_KEY_DATA_GROUPKEY, hdr, 2,
5091 gtk, gtk_len);
5092 }
5093 opos = pos;
5094 pos = ieee80211w_kde_add(sm, pos);
Roshan Pius3a1667e2018-07-03 15:17:14 -07005095 if (pos - opos >= 2 + RSN_SELECTOR_LEN + WPA_IGTK_KDE_PREFIX_LEN) {
5096 /* skip KDE header and keyid */
5097 opos += 2 + RSN_SELECTOR_LEN + 2;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005098 os_memset(opos, 0, 6); /* clear PN */
5099 }
Hai Shalom74f70d42019-02-11 14:42:39 -08005100 if (ocv_oci_add(sm, &pos) < 0) {
5101 os_free(kde);
5102 return -1;
5103 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005104
5105#ifdef CONFIG_IEEE80211R_AP
5106 if (wpa_key_mgmt_ft(sm->wpa_key_mgmt)) {
5107 int res;
5108 struct wpa_auth_config *conf;
5109
5110 conf = &sm->wpa_auth->conf;
5111 if (sm->assoc_resp_ftie &&
5112 kde + kde_len - pos >= 2 + sm->assoc_resp_ftie[1]) {
5113 os_memcpy(pos, sm->assoc_resp_ftie,
5114 2 + sm->assoc_resp_ftie[1]);
5115 res = 2 + sm->assoc_resp_ftie[1];
5116 } else {
Roshan Pius3a1667e2018-07-03 15:17:14 -07005117 int use_sha384 = wpa_key_mgmt_sha384(sm->wpa_key_mgmt);
5118
5119 res = wpa_write_ftie(conf, use_sha384,
5120 conf->r0_key_holder,
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005121 conf->r0_key_holder_len,
5122 NULL, NULL, pos,
5123 kde + kde_len - pos,
5124 NULL, 0);
5125 }
5126 if (res < 0) {
5127 wpa_printf(MSG_ERROR, "FT: Failed to insert FTIE "
5128 "into EAPOL-Key Key Data");
5129 os_free(kde);
5130 return -1;
5131 }
5132 pos += res;
5133
5134 /* TIE[ReassociationDeadline] (TU) */
5135 *pos++ = WLAN_EID_TIMEOUT_INTERVAL;
5136 *pos++ = 5;
5137 *pos++ = WLAN_TIMEOUT_REASSOC_DEADLINE;
5138 WPA_PUT_LE32(pos, conf->reassociation_deadline);
5139 pos += 4;
5140
5141 /* TIE[KeyLifetime] (seconds) */
5142 *pos++ = WLAN_EID_TIMEOUT_INTERVAL;
5143 *pos++ = 5;
5144 *pos++ = WLAN_TIMEOUT_KEY_LIFETIME;
Roshan Pius3a1667e2018-07-03 15:17:14 -07005145 WPA_PUT_LE32(pos, conf->r0_key_lifetime);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005146 pos += 4;
5147 }
5148#endif /* CONFIG_IEEE80211R_AP */
5149
5150 wpa_send_eapol(sm->wpa_auth, sm,
5151 (secure ? WPA_KEY_INFO_SECURE : 0) |
5152 (wpa_mic_len(sm->wpa_key_mgmt, sm->pmk_len) ?
5153 WPA_KEY_INFO_MIC : 0) |
5154 WPA_KEY_INFO_ACK | WPA_KEY_INFO_INSTALL |
5155 WPA_KEY_INFO_KEY_TYPE,
Hai Shalomc3565922019-10-28 11:58:20 -07005156 _rsc, sm->ANonce, kde, pos - kde, 0, encr);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005157 os_free(kde);
5158 return 0;
5159}
5160
5161
5162int wpa_auth_resend_group_m1(struct wpa_state_machine *sm,
5163 void (*cb)(void *ctx1, void *ctx2),
5164 void *ctx1, void *ctx2)
5165{
5166 u8 rsc[WPA_KEY_RSC_LEN];
5167 struct wpa_group *gsm = sm->group;
5168 const u8 *kde;
Roshan Pius3a1667e2018-07-03 15:17:14 -07005169 u8 *kde_buf = NULL, *pos, hdr[2];
Roshan Pius3a1667e2018-07-03 15:17:14 -07005170 u8 *opos;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005171 size_t kde_len;
5172 u8 *gtk;
5173
5174 /* Send EAPOL(1, 1, 1, !Pair, G, RSC, GNonce, MIC(PTK), GTK[GN]) */
5175 os_memset(rsc, 0, WPA_KEY_RSC_LEN);
5176 /* Use 0 RSC */
5177 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
5178 "sending 1/2 msg of Group Key Handshake (TESTING)");
5179
5180 gtk = gsm->GTK[gsm->GN - 1];
5181 if (sm->wpa == WPA_VERSION_WPA2) {
5182 kde_len = 2 + RSN_SELECTOR_LEN + 2 + gsm->GTK_len +
Hai Shalom74f70d42019-02-11 14:42:39 -08005183 ieee80211w_kde_len(sm) + ocv_oci_len(sm);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005184 kde_buf = os_malloc(kde_len);
5185 if (kde_buf == NULL)
5186 return -1;
5187
5188 kde = pos = kde_buf;
5189 hdr[0] = gsm->GN & 0x03;
5190 hdr[1] = 0;
5191 pos = wpa_add_kde(pos, RSN_KEY_DATA_GROUPKEY, hdr, 2,
5192 gtk, gsm->GTK_len);
5193 opos = pos;
5194 pos = ieee80211w_kde_add(sm, pos);
Roshan Pius3a1667e2018-07-03 15:17:14 -07005195 if (pos - opos >=
5196 2 + RSN_SELECTOR_LEN + WPA_IGTK_KDE_PREFIX_LEN) {
5197 /* skip KDE header and keyid */
5198 opos += 2 + RSN_SELECTOR_LEN + 2;
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005199 os_memset(opos, 0, 6); /* clear PN */
5200 }
Hai Shalom74f70d42019-02-11 14:42:39 -08005201 if (ocv_oci_add(sm, &pos) < 0) {
5202 os_free(kde_buf);
5203 return -1;
5204 }
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005205 kde_len = pos - kde;
5206 } else {
5207 kde = gtk;
5208 kde_len = gsm->GTK_len;
5209 }
5210
5211 sm->eapol_status_cb = cb;
5212 sm->eapol_status_cb_ctx1 = ctx1;
5213 sm->eapol_status_cb_ctx2 = ctx2;
5214
5215 wpa_send_eapol(sm->wpa_auth, sm,
5216 WPA_KEY_INFO_SECURE |
5217 (wpa_mic_len(sm->wpa_key_mgmt, sm->pmk_len) ?
5218 WPA_KEY_INFO_MIC : 0) |
5219 WPA_KEY_INFO_ACK |
5220 (!sm->Pair ? WPA_KEY_INFO_INSTALL : 0),
5221 rsc, NULL, kde, kde_len, gsm->GN, 1);
5222
5223 os_free(kde_buf);
5224 return 0;
5225}
5226
Roshan Pius3a1667e2018-07-03 15:17:14 -07005227
5228int wpa_auth_rekey_gtk(struct wpa_authenticator *wpa_auth)
5229{
5230 if (!wpa_auth)
5231 return -1;
5232 eloop_cancel_timeout(wpa_rekey_gtk, wpa_auth, NULL);
5233 return eloop_register_timeout(0, 0, wpa_rekey_gtk, wpa_auth, NULL);
5234}
5235
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07005236#endif /* CONFIG_TESTING_OPTIONS */