blob: 69e3a5ded1b372b9762703e23e28f9eb7884d1dd [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
Dmitry Shmidt807291d2015-01-27 13:40:23 -08003 * Copyright (c) 2004-2015, 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"
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -080016#include "crypto/aes.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070017#include "crypto/aes_wrap.h"
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -080018#include "crypto/aes_siv.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070019#include "crypto/crypto.h"
20#include "crypto/sha1.h"
21#include "crypto/sha256.h"
22#include "crypto/random.h"
23#include "eapol_auth/eapol_auth_sm.h"
24#include "ap_config.h"
25#include "ieee802_11.h"
26#include "wpa_auth.h"
27#include "pmksa_cache_auth.h"
28#include "wpa_auth_i.h"
29#include "wpa_auth_ie.h"
30
31#define STATE_MACHINE_DATA struct wpa_state_machine
32#define STATE_MACHINE_DEBUG_PREFIX "WPA"
33#define STATE_MACHINE_ADDR sm->addr
34
35
36static void wpa_send_eapol_timeout(void *eloop_ctx, void *timeout_ctx);
37static int wpa_sm_step(struct wpa_state_machine *sm);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080038static int wpa_verify_key_mic(int akmp, struct wpa_ptk *PTK, u8 *data,
39 size_t data_len);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -080040#ifdef CONFIG_FILS
41static int wpa_aead_decrypt(struct wpa_state_machine *sm, struct wpa_ptk *ptk,
42 u8 *buf, size_t buf_len, u16 *_key_data_len);
43#endif /* CONFIG_FILS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070044static void wpa_sm_call_step(void *eloop_ctx, void *timeout_ctx);
45static void wpa_group_sm_step(struct wpa_authenticator *wpa_auth,
46 struct wpa_group *group);
47static void wpa_request_new_ptk(struct wpa_state_machine *sm);
48static int wpa_gtk_update(struct wpa_authenticator *wpa_auth,
49 struct wpa_group *group);
50static int wpa_group_config_group_keys(struct wpa_authenticator *wpa_auth,
51 struct wpa_group *group);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080052static int wpa_derive_ptk(struct wpa_state_machine *sm, const u8 *snonce,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080053 const u8 *pmk, unsigned int pmk_len,
54 struct wpa_ptk *ptk);
Dmitry Shmidt1d755d02015-04-28 10:34:29 -070055static void wpa_group_free(struct wpa_authenticator *wpa_auth,
56 struct wpa_group *group);
57static void wpa_group_get(struct wpa_authenticator *wpa_auth,
58 struct wpa_group *group);
59static void wpa_group_put(struct wpa_authenticator *wpa_auth,
60 struct wpa_group *group);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -080061static u8 * ieee80211w_kde_add(struct wpa_state_machine *sm, u8 *pos);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070062
63static const u32 dot11RSNAConfigGroupUpdateCount = 4;
64static const u32 dot11RSNAConfigPairwiseUpdateCount = 4;
65static const u32 eapol_key_timeout_first = 100; /* ms */
66static const u32 eapol_key_timeout_subseq = 1000; /* ms */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080067static const u32 eapol_key_timeout_first_group = 500; /* ms */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070068
69/* TODO: make these configurable */
70static const int dot11RSNAConfigPMKLifetime = 43200;
71static const int dot11RSNAConfigPMKReauthThreshold = 70;
72static const int dot11RSNAConfigSATimeout = 60;
73
74
Dmitry Shmidtd5e49232012-12-03 15:08:10 -080075static inline int wpa_auth_mic_failure_report(
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070076 struct wpa_authenticator *wpa_auth, const u8 *addr)
77{
Paul Stewart092955c2017-02-06 09:13:09 -080078 if (wpa_auth->cb->mic_failure_report)
79 return wpa_auth->cb->mic_failure_report(wpa_auth->cb_ctx, addr);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -080080 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070081}
82
83
Dmitry Shmidtdda10c22015-03-24 16:05:01 -070084static inline void wpa_auth_psk_failure_report(
85 struct wpa_authenticator *wpa_auth, const u8 *addr)
86{
Paul Stewart092955c2017-02-06 09:13:09 -080087 if (wpa_auth->cb->psk_failure_report)
88 wpa_auth->cb->psk_failure_report(wpa_auth->cb_ctx, addr);
Dmitry Shmidtdda10c22015-03-24 16:05:01 -070089}
90
91
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070092static inline void wpa_auth_set_eapol(struct wpa_authenticator *wpa_auth,
93 const u8 *addr, wpa_eapol_variable var,
94 int value)
95{
Paul Stewart092955c2017-02-06 09:13:09 -080096 if (wpa_auth->cb->set_eapol)
97 wpa_auth->cb->set_eapol(wpa_auth->cb_ctx, addr, var, value);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070098}
99
100
101static inline int wpa_auth_get_eapol(struct wpa_authenticator *wpa_auth,
102 const u8 *addr, wpa_eapol_variable var)
103{
Paul Stewart092955c2017-02-06 09:13:09 -0800104 if (wpa_auth->cb->get_eapol == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700105 return -1;
Paul Stewart092955c2017-02-06 09:13:09 -0800106 return wpa_auth->cb->get_eapol(wpa_auth->cb_ctx, addr, var);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700107}
108
109
110static inline const u8 * wpa_auth_get_psk(struct wpa_authenticator *wpa_auth,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700111 const u8 *addr,
112 const u8 *p2p_dev_addr,
113 const u8 *prev_psk)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700114{
Paul Stewart092955c2017-02-06 09:13:09 -0800115 if (wpa_auth->cb->get_psk == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700116 return NULL;
Paul Stewart092955c2017-02-06 09:13:09 -0800117 return wpa_auth->cb->get_psk(wpa_auth->cb_ctx, addr, p2p_dev_addr,
118 prev_psk);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700119}
120
121
122static inline int wpa_auth_get_msk(struct wpa_authenticator *wpa_auth,
123 const u8 *addr, u8 *msk, size_t *len)
124{
Paul Stewart092955c2017-02-06 09:13:09 -0800125 if (wpa_auth->cb->get_msk == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700126 return -1;
Paul Stewart092955c2017-02-06 09:13:09 -0800127 return wpa_auth->cb->get_msk(wpa_auth->cb_ctx, addr, msk, len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700128}
129
130
131static inline int wpa_auth_set_key(struct wpa_authenticator *wpa_auth,
132 int vlan_id,
133 enum wpa_alg alg, const u8 *addr, int idx,
134 u8 *key, size_t key_len)
135{
Paul Stewart092955c2017-02-06 09:13:09 -0800136 if (wpa_auth->cb->set_key == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700137 return -1;
Paul Stewart092955c2017-02-06 09:13:09 -0800138 return wpa_auth->cb->set_key(wpa_auth->cb_ctx, vlan_id, alg, addr, idx,
139 key, key_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700140}
141
142
143static inline int wpa_auth_get_seqnum(struct wpa_authenticator *wpa_auth,
144 const u8 *addr, int idx, u8 *seq)
145{
Paul Stewart092955c2017-02-06 09:13:09 -0800146 if (wpa_auth->cb->get_seqnum == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700147 return -1;
Paul Stewart092955c2017-02-06 09:13:09 -0800148 return wpa_auth->cb->get_seqnum(wpa_auth->cb_ctx, addr, idx, seq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700149}
150
151
152static inline int
153wpa_auth_send_eapol(struct wpa_authenticator *wpa_auth, const u8 *addr,
154 const u8 *data, size_t data_len, int encrypt)
155{
Paul Stewart092955c2017-02-06 09:13:09 -0800156 if (wpa_auth->cb->send_eapol == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700157 return -1;
Paul Stewart092955c2017-02-06 09:13:09 -0800158 return wpa_auth->cb->send_eapol(wpa_auth->cb_ctx, addr, data, data_len,
159 encrypt);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700160}
161
162
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800163#ifdef CONFIG_MESH
164static inline int wpa_auth_start_ampe(struct wpa_authenticator *wpa_auth,
165 const u8 *addr)
166{
Paul Stewart092955c2017-02-06 09:13:09 -0800167 if (wpa_auth->cb->start_ampe == NULL)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800168 return -1;
Paul Stewart092955c2017-02-06 09:13:09 -0800169 return wpa_auth->cb->start_ampe(wpa_auth->cb_ctx, addr);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800170}
171#endif /* CONFIG_MESH */
172
173
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700174int wpa_auth_for_each_sta(struct wpa_authenticator *wpa_auth,
175 int (*cb)(struct wpa_state_machine *sm, void *ctx),
176 void *cb_ctx)
177{
Paul Stewart092955c2017-02-06 09:13:09 -0800178 if (wpa_auth->cb->for_each_sta == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700179 return 0;
Paul Stewart092955c2017-02-06 09:13:09 -0800180 return wpa_auth->cb->for_each_sta(wpa_auth->cb_ctx, cb, cb_ctx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700181}
182
183
184int wpa_auth_for_each_auth(struct wpa_authenticator *wpa_auth,
185 int (*cb)(struct wpa_authenticator *a, void *ctx),
186 void *cb_ctx)
187{
Paul Stewart092955c2017-02-06 09:13:09 -0800188 if (wpa_auth->cb->for_each_auth == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700189 return 0;
Paul Stewart092955c2017-02-06 09:13:09 -0800190 return wpa_auth->cb->for_each_auth(wpa_auth->cb_ctx, cb, cb_ctx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700191}
192
193
194void wpa_auth_logger(struct wpa_authenticator *wpa_auth, const u8 *addr,
195 logger_level level, const char *txt)
196{
Paul Stewart092955c2017-02-06 09:13:09 -0800197 if (wpa_auth->cb->logger == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700198 return;
Paul Stewart092955c2017-02-06 09:13:09 -0800199 wpa_auth->cb->logger(wpa_auth->cb_ctx, addr, level, txt);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700200}
201
202
203void wpa_auth_vlogger(struct wpa_authenticator *wpa_auth, const u8 *addr,
204 logger_level level, const char *fmt, ...)
205{
206 char *format;
207 int maxlen;
208 va_list ap;
209
Paul Stewart092955c2017-02-06 09:13:09 -0800210 if (wpa_auth->cb->logger == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700211 return;
212
213 maxlen = os_strlen(fmt) + 100;
214 format = os_malloc(maxlen);
215 if (!format)
216 return;
217
218 va_start(ap, fmt);
219 vsnprintf(format, maxlen, fmt, ap);
220 va_end(ap);
221
222 wpa_auth_logger(wpa_auth, addr, level, format);
223
224 os_free(format);
225}
226
227
228static void wpa_sta_disconnect(struct wpa_authenticator *wpa_auth,
229 const u8 *addr)
230{
Paul Stewart092955c2017-02-06 09:13:09 -0800231 if (wpa_auth->cb->disconnect == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700232 return;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800233 wpa_printf(MSG_DEBUG, "wpa_sta_disconnect STA " MACSTR, MAC2STR(addr));
Paul Stewart092955c2017-02-06 09:13:09 -0800234 wpa_auth->cb->disconnect(wpa_auth->cb_ctx, addr,
235 WLAN_REASON_PREV_AUTH_NOT_VALID);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700236}
237
238
239static int wpa_use_aes_cmac(struct wpa_state_machine *sm)
240{
241 int ret = 0;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800242#ifdef CONFIG_IEEE80211R_AP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700243 if (wpa_key_mgmt_ft(sm->wpa_key_mgmt))
244 ret = 1;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800245#endif /* CONFIG_IEEE80211R_AP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700246#ifdef CONFIG_IEEE80211W
247 if (wpa_key_mgmt_sha256(sm->wpa_key_mgmt))
248 ret = 1;
249#endif /* CONFIG_IEEE80211W */
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800250 if (sm->wpa_key_mgmt == WPA_KEY_MGMT_OSEN)
251 ret = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700252 return ret;
253}
254
255
256static void wpa_rekey_gmk(void *eloop_ctx, void *timeout_ctx)
257{
258 struct wpa_authenticator *wpa_auth = eloop_ctx;
259
260 if (random_get_bytes(wpa_auth->group->GMK, WPA_GMK_LEN)) {
261 wpa_printf(MSG_ERROR, "Failed to get random data for WPA "
262 "initialization.");
263 } else {
264 wpa_auth_logger(wpa_auth, NULL, LOGGER_DEBUG, "GMK rekeyd");
265 wpa_hexdump_key(MSG_DEBUG, "GMK",
266 wpa_auth->group->GMK, WPA_GMK_LEN);
267 }
268
269 if (wpa_auth->conf.wpa_gmk_rekey) {
270 eloop_register_timeout(wpa_auth->conf.wpa_gmk_rekey, 0,
271 wpa_rekey_gmk, wpa_auth, NULL);
272 }
273}
274
275
276static void wpa_rekey_gtk(void *eloop_ctx, void *timeout_ctx)
277{
278 struct wpa_authenticator *wpa_auth = eloop_ctx;
Dmitry Shmidt1d755d02015-04-28 10:34:29 -0700279 struct wpa_group *group, *next;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700280
281 wpa_auth_logger(wpa_auth, NULL, LOGGER_DEBUG, "rekeying GTK");
Dmitry Shmidt1d755d02015-04-28 10:34:29 -0700282 group = wpa_auth->group;
283 while (group) {
284 wpa_group_get(wpa_auth, group);
285
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700286 group->GTKReKey = TRUE;
287 do {
288 group->changed = FALSE;
289 wpa_group_sm_step(wpa_auth, group);
290 } while (group->changed);
Dmitry Shmidt1d755d02015-04-28 10:34:29 -0700291
292 next = group->next;
293 wpa_group_put(wpa_auth, group);
294 group = next;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700295 }
296
297 if (wpa_auth->conf.wpa_group_rekey) {
298 eloop_register_timeout(wpa_auth->conf.wpa_group_rekey,
299 0, wpa_rekey_gtk, wpa_auth, NULL);
300 }
301}
302
303
304static void wpa_rekey_ptk(void *eloop_ctx, void *timeout_ctx)
305{
306 struct wpa_authenticator *wpa_auth = eloop_ctx;
307 struct wpa_state_machine *sm = timeout_ctx;
308
309 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG, "rekeying PTK");
310 wpa_request_new_ptk(sm);
311 wpa_sm_step(sm);
312}
313
314
315static int wpa_auth_pmksa_clear_cb(struct wpa_state_machine *sm, void *ctx)
316{
317 if (sm->pmksa == ctx)
318 sm->pmksa = NULL;
319 return 0;
320}
321
322
323static void wpa_auth_pmksa_free_cb(struct rsn_pmksa_cache_entry *entry,
324 void *ctx)
325{
326 struct wpa_authenticator *wpa_auth = ctx;
327 wpa_auth_for_each_sta(wpa_auth, wpa_auth_pmksa_clear_cb, entry);
328}
329
330
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700331static int wpa_group_init_gmk_and_counter(struct wpa_authenticator *wpa_auth,
332 struct wpa_group *group)
333{
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800334 u8 buf[ETH_ALEN + 8 + sizeof(unsigned long)];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700335 u8 rkey[32];
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800336 unsigned long ptr;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700337
338 if (random_get_bytes(group->GMK, WPA_GMK_LEN) < 0)
339 return -1;
340 wpa_hexdump_key(MSG_DEBUG, "GMK", group->GMK, WPA_GMK_LEN);
341
342 /*
343 * Counter = PRF-256(Random number, "Init Counter",
344 * Local MAC Address || Time)
345 */
346 os_memcpy(buf, wpa_auth->addr, ETH_ALEN);
347 wpa_get_ntp_timestamp(buf + ETH_ALEN);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800348 ptr = (unsigned long) group;
349 os_memcpy(buf + ETH_ALEN + 8, &ptr, sizeof(ptr));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700350 if (random_get_bytes(rkey, sizeof(rkey)) < 0)
351 return -1;
352
353 if (sha1_prf(rkey, sizeof(rkey), "Init Counter", buf, sizeof(buf),
354 group->Counter, WPA_NONCE_LEN) < 0)
355 return -1;
356 wpa_hexdump_key(MSG_DEBUG, "Key Counter",
357 group->Counter, WPA_NONCE_LEN);
358
359 return 0;
360}
361
362
363static struct wpa_group * wpa_group_init(struct wpa_authenticator *wpa_auth,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800364 int vlan_id, int delay_init)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700365{
366 struct wpa_group *group;
367
368 group = os_zalloc(sizeof(struct wpa_group));
369 if (group == NULL)
370 return NULL;
371
372 group->GTKAuthenticator = TRUE;
373 group->vlan_id = vlan_id;
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700374 group->GTK_len = wpa_cipher_key_len(wpa_auth->conf.wpa_group);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700375
376 if (random_pool_ready() != 1) {
377 wpa_printf(MSG_INFO, "WPA: Not enough entropy in random pool "
378 "for secure operations - update keys later when "
379 "the first station connects");
380 }
381
382 /*
383 * Set initial GMK/Counter value here. The actual values that will be
384 * used in negotiations will be set once the first station tries to
385 * connect. This allows more time for collecting additional randomness
386 * on embedded devices.
387 */
388 if (wpa_group_init_gmk_and_counter(wpa_auth, group) < 0) {
389 wpa_printf(MSG_ERROR, "Failed to get random data for WPA "
390 "initialization.");
391 os_free(group);
392 return NULL;
393 }
394
395 group->GInit = TRUE;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800396 if (delay_init) {
397 wpa_printf(MSG_DEBUG, "WPA: Delay group state machine start "
398 "until Beacon frames have been configured");
399 /* Initialization is completed in wpa_init_keys(). */
400 } else {
401 wpa_group_sm_step(wpa_auth, group);
402 group->GInit = FALSE;
403 wpa_group_sm_step(wpa_auth, group);
404 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700405
406 return group;
407}
408
409
410/**
411 * wpa_init - Initialize WPA authenticator
412 * @addr: Authenticator address
413 * @conf: Configuration for WPA authenticator
414 * @cb: Callback functions for WPA authenticator
415 * Returns: Pointer to WPA authenticator data or %NULL on failure
416 */
417struct wpa_authenticator * wpa_init(const u8 *addr,
418 struct wpa_auth_config *conf,
Paul Stewart092955c2017-02-06 09:13:09 -0800419 const struct wpa_auth_callbacks *cb,
420 void *cb_ctx)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700421{
422 struct wpa_authenticator *wpa_auth;
423
424 wpa_auth = os_zalloc(sizeof(struct wpa_authenticator));
425 if (wpa_auth == NULL)
426 return NULL;
427 os_memcpy(wpa_auth->addr, addr, ETH_ALEN);
428 os_memcpy(&wpa_auth->conf, conf, sizeof(*conf));
Paul Stewart092955c2017-02-06 09:13:09 -0800429 wpa_auth->cb = cb;
430 wpa_auth->cb_ctx = cb_ctx;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700431
432 if (wpa_auth_gen_wpa_ie(wpa_auth)) {
433 wpa_printf(MSG_ERROR, "Could not generate WPA IE.");
434 os_free(wpa_auth);
435 return NULL;
436 }
437
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800438 wpa_auth->group = wpa_group_init(wpa_auth, 0, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700439 if (wpa_auth->group == NULL) {
440 os_free(wpa_auth->wpa_ie);
441 os_free(wpa_auth);
442 return NULL;
443 }
444
445 wpa_auth->pmksa = pmksa_cache_auth_init(wpa_auth_pmksa_free_cb,
446 wpa_auth);
447 if (wpa_auth->pmksa == NULL) {
448 wpa_printf(MSG_ERROR, "PMKSA cache initialization failed.");
Dmitry Shmidtff787d52015-01-12 13:01:47 -0800449 os_free(wpa_auth->group);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700450 os_free(wpa_auth->wpa_ie);
451 os_free(wpa_auth);
452 return NULL;
453 }
454
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800455#ifdef CONFIG_IEEE80211R_AP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700456 wpa_auth->ft_pmk_cache = wpa_ft_pmk_cache_init();
457 if (wpa_auth->ft_pmk_cache == NULL) {
458 wpa_printf(MSG_ERROR, "FT PMK cache initialization failed.");
Dmitry Shmidtff787d52015-01-12 13:01:47 -0800459 os_free(wpa_auth->group);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700460 os_free(wpa_auth->wpa_ie);
461 pmksa_cache_auth_deinit(wpa_auth->pmksa);
462 os_free(wpa_auth);
463 return NULL;
464 }
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800465#endif /* CONFIG_IEEE80211R_AP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700466
467 if (wpa_auth->conf.wpa_gmk_rekey) {
468 eloop_register_timeout(wpa_auth->conf.wpa_gmk_rekey, 0,
469 wpa_rekey_gmk, wpa_auth, NULL);
470 }
471
472 if (wpa_auth->conf.wpa_group_rekey) {
473 eloop_register_timeout(wpa_auth->conf.wpa_group_rekey, 0,
474 wpa_rekey_gtk, wpa_auth, NULL);
475 }
476
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800477#ifdef CONFIG_P2P
478 if (WPA_GET_BE32(conf->ip_addr_start)) {
479 int count = WPA_GET_BE32(conf->ip_addr_end) -
480 WPA_GET_BE32(conf->ip_addr_start) + 1;
481 if (count > 1000)
482 count = 1000;
483 if (count > 0)
484 wpa_auth->ip_pool = bitfield_alloc(count);
485 }
486#endif /* CONFIG_P2P */
487
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700488 return wpa_auth;
489}
490
491
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800492int wpa_init_keys(struct wpa_authenticator *wpa_auth)
493{
494 struct wpa_group *group = wpa_auth->group;
495
496 wpa_printf(MSG_DEBUG, "WPA: Start group state machine to set initial "
497 "keys");
498 wpa_group_sm_step(wpa_auth, group);
499 group->GInit = FALSE;
500 wpa_group_sm_step(wpa_auth, group);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800501 if (group->wpa_group_state == WPA_GROUP_FATAL_FAILURE)
502 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800503 return 0;
504}
505
506
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700507/**
508 * wpa_deinit - Deinitialize WPA authenticator
509 * @wpa_auth: Pointer to WPA authenticator data from wpa_init()
510 */
511void wpa_deinit(struct wpa_authenticator *wpa_auth)
512{
513 struct wpa_group *group, *prev;
514
515 eloop_cancel_timeout(wpa_rekey_gmk, wpa_auth, NULL);
516 eloop_cancel_timeout(wpa_rekey_gtk, wpa_auth, NULL);
517
518#ifdef CONFIG_PEERKEY
519 while (wpa_auth->stsl_negotiations)
520 wpa_stsl_remove(wpa_auth, wpa_auth->stsl_negotiations);
521#endif /* CONFIG_PEERKEY */
522
523 pmksa_cache_auth_deinit(wpa_auth->pmksa);
524
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800525#ifdef CONFIG_IEEE80211R_AP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700526 wpa_ft_pmk_cache_deinit(wpa_auth->ft_pmk_cache);
527 wpa_auth->ft_pmk_cache = NULL;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800528#endif /* CONFIG_IEEE80211R_AP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700529
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800530#ifdef CONFIG_P2P
531 bitfield_free(wpa_auth->ip_pool);
532#endif /* CONFIG_P2P */
533
534
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700535 os_free(wpa_auth->wpa_ie);
536
537 group = wpa_auth->group;
538 while (group) {
539 prev = group;
540 group = group->next;
541 os_free(prev);
542 }
543
544 os_free(wpa_auth);
545}
546
547
548/**
549 * wpa_reconfig - Update WPA authenticator configuration
550 * @wpa_auth: Pointer to WPA authenticator data from wpa_init()
551 * @conf: Configuration for WPA authenticator
552 */
553int wpa_reconfig(struct wpa_authenticator *wpa_auth,
554 struct wpa_auth_config *conf)
555{
556 struct wpa_group *group;
557 if (wpa_auth == NULL)
558 return 0;
559
560 os_memcpy(&wpa_auth->conf, conf, sizeof(*conf));
561 if (wpa_auth_gen_wpa_ie(wpa_auth)) {
562 wpa_printf(MSG_ERROR, "Could not generate WPA IE.");
563 return -1;
564 }
565
566 /*
567 * Reinitialize GTK to make sure it is suitable for the new
568 * configuration.
569 */
570 group = wpa_auth->group;
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700571 group->GTK_len = wpa_cipher_key_len(wpa_auth->conf.wpa_group);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700572 group->GInit = TRUE;
573 wpa_group_sm_step(wpa_auth, group);
574 group->GInit = FALSE;
575 wpa_group_sm_step(wpa_auth, group);
576
577 return 0;
578}
579
580
581struct wpa_state_machine *
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700582wpa_auth_sta_init(struct wpa_authenticator *wpa_auth, const u8 *addr,
583 const u8 *p2p_dev_addr)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700584{
585 struct wpa_state_machine *sm;
586
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800587 if (wpa_auth->group->wpa_group_state == WPA_GROUP_FATAL_FAILURE)
588 return NULL;
589
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700590 sm = os_zalloc(sizeof(struct wpa_state_machine));
591 if (sm == NULL)
592 return NULL;
593 os_memcpy(sm->addr, addr, ETH_ALEN);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700594 if (p2p_dev_addr)
595 os_memcpy(sm->p2p_dev_addr, p2p_dev_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700596
597 sm->wpa_auth = wpa_auth;
598 sm->group = wpa_auth->group;
Dmitry Shmidt1d755d02015-04-28 10:34:29 -0700599 wpa_group_get(sm->wpa_auth, sm->group);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700600
601 return sm;
602}
603
604
605int wpa_auth_sta_associated(struct wpa_authenticator *wpa_auth,
606 struct wpa_state_machine *sm)
607{
608 if (wpa_auth == NULL || !wpa_auth->conf.wpa || sm == NULL)
609 return -1;
610
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800611#ifdef CONFIG_IEEE80211R_AP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700612 if (sm->ft_completed) {
613 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG,
614 "FT authentication already completed - do not "
615 "start 4-way handshake");
Dmitry Shmidt71757432014-06-02 13:50:35 -0700616 /* Go to PTKINITDONE state to allow GTK rekeying */
617 sm->wpa_ptk_state = WPA_PTK_PTKINITDONE;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700618 return 0;
619 }
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800620#endif /* CONFIG_IEEE80211R_AP */
621
622#ifdef CONFIG_FILS
623 if (sm->fils_completed) {
624 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG,
625 "FILS authentication already completed - do not start 4-way handshake");
626 /* Go to PTKINITDONE state to allow GTK rekeying */
627 sm->wpa_ptk_state = WPA_PTK_PTKINITDONE;
628 return 0;
629 }
630#endif /* CONFIG_FILS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700631
632 if (sm->started) {
633 os_memset(&sm->key_replay, 0, sizeof(sm->key_replay));
634 sm->ReAuthenticationRequest = TRUE;
635 return wpa_sm_step(sm);
636 }
637
638 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG,
639 "start authentication");
640 sm->started = 1;
641
642 sm->Init = TRUE;
643 if (wpa_sm_step(sm) == 1)
644 return 1; /* should not really happen */
645 sm->Init = FALSE;
646 sm->AuthenticationRequest = TRUE;
647 return wpa_sm_step(sm);
648}
649
650
651void wpa_auth_sta_no_wpa(struct wpa_state_machine *sm)
652{
653 /* WPA/RSN was not used - clear WPA state. This is needed if the STA
654 * reassociates back to the same AP while the previous entry for the
655 * STA has not yet been removed. */
656 if (sm == NULL)
657 return;
658
659 sm->wpa_key_mgmt = 0;
660}
661
662
663static void wpa_free_sta_sm(struct wpa_state_machine *sm)
664{
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800665#ifdef CONFIG_P2P
666 if (WPA_GET_BE32(sm->ip_addr)) {
667 u32 start;
668 wpa_printf(MSG_DEBUG, "P2P: Free assigned IP "
669 "address %u.%u.%u.%u from " MACSTR,
670 sm->ip_addr[0], sm->ip_addr[1],
671 sm->ip_addr[2], sm->ip_addr[3],
672 MAC2STR(sm->addr));
673 start = WPA_GET_BE32(sm->wpa_auth->conf.ip_addr_start);
674 bitfield_clear(sm->wpa_auth->ip_pool,
675 WPA_GET_BE32(sm->ip_addr) - start);
676 }
677#endif /* CONFIG_P2P */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700678 if (sm->GUpdateStationKeys) {
679 sm->group->GKeyDoneStations--;
680 sm->GUpdateStationKeys = FALSE;
681 }
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800682#ifdef CONFIG_IEEE80211R_AP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700683 os_free(sm->assoc_resp_ftie);
Dmitry Shmidtd11f0192014-03-24 12:09:47 -0700684 wpabuf_free(sm->ft_pending_req_ies);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800685#endif /* CONFIG_IEEE80211R_AP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700686 os_free(sm->last_rx_eapol_key);
687 os_free(sm->wpa_ie);
Dmitry Shmidt1d755d02015-04-28 10:34:29 -0700688 wpa_group_put(sm->wpa_auth, sm->group);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700689 os_free(sm);
690}
691
692
693void wpa_auth_sta_deinit(struct wpa_state_machine *sm)
694{
695 if (sm == NULL)
696 return;
697
698 if (sm->wpa_auth->conf.wpa_strict_rekey && sm->has_GTK) {
699 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
700 "strict rekeying - force GTK rekey since STA "
701 "is leaving");
702 eloop_cancel_timeout(wpa_rekey_gtk, sm->wpa_auth, NULL);
703 eloop_register_timeout(0, 500000, wpa_rekey_gtk, sm->wpa_auth,
704 NULL);
705 }
706
707 eloop_cancel_timeout(wpa_send_eapol_timeout, sm->wpa_auth, sm);
708 sm->pending_1_of_4_timeout = 0;
709 eloop_cancel_timeout(wpa_sm_call_step, sm, NULL);
710 eloop_cancel_timeout(wpa_rekey_ptk, sm->wpa_auth, sm);
711 if (sm->in_step_loop) {
712 /* Must not free state machine while wpa_sm_step() is running.
713 * Freeing will be completed in the end of wpa_sm_step(). */
714 wpa_printf(MSG_DEBUG, "WPA: Registering pending STA state "
715 "machine deinit for " MACSTR, MAC2STR(sm->addr));
716 sm->pending_deinit = 1;
717 } else
718 wpa_free_sta_sm(sm);
719}
720
721
722static void wpa_request_new_ptk(struct wpa_state_machine *sm)
723{
724 if (sm == NULL)
725 return;
726
727 sm->PTKRequest = TRUE;
728 sm->PTK_valid = 0;
729}
730
731
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800732static int wpa_replay_counter_valid(struct wpa_key_replay_counter *ctr,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700733 const u8 *replay_counter)
734{
735 int i;
736 for (i = 0; i < RSNA_MAX_EAPOL_RETRIES; i++) {
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800737 if (!ctr[i].valid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700738 break;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800739 if (os_memcmp(replay_counter, ctr[i].counter,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700740 WPA_REPLAY_COUNTER_LEN) == 0)
741 return 1;
742 }
743 return 0;
744}
745
746
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800747static void wpa_replay_counter_mark_invalid(struct wpa_key_replay_counter *ctr,
748 const u8 *replay_counter)
749{
750 int i;
751 for (i = 0; i < RSNA_MAX_EAPOL_RETRIES; i++) {
752 if (ctr[i].valid &&
753 (replay_counter == NULL ||
754 os_memcmp(replay_counter, ctr[i].counter,
755 WPA_REPLAY_COUNTER_LEN) == 0))
756 ctr[i].valid = FALSE;
757 }
758}
759
760
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800761#ifdef CONFIG_IEEE80211R_AP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700762static int ft_check_msg_2_of_4(struct wpa_authenticator *wpa_auth,
763 struct wpa_state_machine *sm,
764 struct wpa_eapol_ie_parse *kde)
765{
766 struct wpa_ie_data ie;
767 struct rsn_mdie *mdie;
768
769 if (wpa_parse_wpa_ie_rsn(kde->rsn_ie, kde->rsn_ie_len, &ie) < 0 ||
770 ie.num_pmkid != 1 || ie.pmkid == NULL) {
771 wpa_printf(MSG_DEBUG, "FT: No PMKR1Name in "
772 "FT 4-way handshake message 2/4");
773 return -1;
774 }
775
776 os_memcpy(sm->sup_pmk_r1_name, ie.pmkid, PMKID_LEN);
777 wpa_hexdump(MSG_DEBUG, "FT: PMKR1Name from Supplicant",
778 sm->sup_pmk_r1_name, PMKID_LEN);
779
780 if (!kde->mdie || !kde->ftie) {
781 wpa_printf(MSG_DEBUG, "FT: No %s in FT 4-way handshake "
782 "message 2/4", kde->mdie ? "FTIE" : "MDIE");
783 return -1;
784 }
785
786 mdie = (struct rsn_mdie *) (kde->mdie + 2);
787 if (kde->mdie[1] < sizeof(struct rsn_mdie) ||
788 os_memcmp(wpa_auth->conf.mobility_domain, mdie->mobility_domain,
789 MOBILITY_DOMAIN_ID_LEN) != 0) {
790 wpa_printf(MSG_DEBUG, "FT: MDIE mismatch");
791 return -1;
792 }
793
794 if (sm->assoc_resp_ftie &&
795 (kde->ftie[1] != sm->assoc_resp_ftie[1] ||
796 os_memcmp(kde->ftie, sm->assoc_resp_ftie,
797 2 + sm->assoc_resp_ftie[1]) != 0)) {
798 wpa_printf(MSG_DEBUG, "FT: FTIE mismatch");
799 wpa_hexdump(MSG_DEBUG, "FT: FTIE in EAPOL-Key msg 2/4",
800 kde->ftie, kde->ftie_len);
801 wpa_hexdump(MSG_DEBUG, "FT: FTIE in (Re)AssocResp",
802 sm->assoc_resp_ftie, 2 + sm->assoc_resp_ftie[1]);
803 return -1;
804 }
805
806 return 0;
807}
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800808#endif /* CONFIG_IEEE80211R_AP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700809
810
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800811static int wpa_receive_error_report(struct wpa_authenticator *wpa_auth,
812 struct wpa_state_machine *sm, int group)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800813{
814 /* Supplicant reported a Michael MIC error */
815 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_INFO,
816 "received EAPOL-Key Error Request "
817 "(STA detected Michael MIC failure (group=%d))",
818 group);
819
820 if (group && wpa_auth->conf.wpa_group != WPA_CIPHER_TKIP) {
821 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
822 "ignore Michael MIC failure report since "
823 "group cipher is not TKIP");
824 } else if (!group && sm->pairwise != WPA_CIPHER_TKIP) {
825 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
826 "ignore Michael MIC failure report since "
827 "pairwise cipher is not TKIP");
828 } else {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800829 if (wpa_auth_mic_failure_report(wpa_auth, sm->addr) > 0)
830 return 1; /* STA entry was removed */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800831 sm->dot11RSNAStatsTKIPRemoteMICFailures++;
832 wpa_auth->dot11RSNAStatsTKIPRemoteMICFailures++;
833 }
834
835 /*
836 * Error report is not a request for a new key handshake, but since
837 * Authenticator may do it, let's change the keys now anyway.
838 */
839 wpa_request_new_ptk(sm);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800840 return 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800841}
842
843
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800844static int wpa_try_alt_snonce(struct wpa_state_machine *sm, u8 *data,
845 size_t data_len)
846{
847 struct wpa_ptk PTK;
848 int ok = 0;
849 const u8 *pmk = NULL;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800850 unsigned int pmk_len;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800851
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800852 os_memset(&PTK, 0, sizeof(PTK));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800853 for (;;) {
854 if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt)) {
855 pmk = wpa_auth_get_psk(sm->wpa_auth, sm->addr,
856 sm->p2p_dev_addr, pmk);
857 if (pmk == NULL)
858 break;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800859 pmk_len = PMK_LEN;
860 } else {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800861 pmk = sm->PMK;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800862 pmk_len = sm->pmk_len;
863 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800864
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800865 wpa_derive_ptk(sm, sm->alt_SNonce, pmk, pmk_len, &PTK);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800866
867 if (wpa_verify_key_mic(sm->wpa_key_mgmt, &PTK, data, data_len)
868 == 0) {
869 ok = 1;
870 break;
871 }
872
873 if (!wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt))
874 break;
875 }
876
877 if (!ok) {
878 wpa_printf(MSG_DEBUG,
879 "WPA: Earlier SNonce did not result in matching MIC");
880 return -1;
881 }
882
883 wpa_printf(MSG_DEBUG,
884 "WPA: Earlier SNonce resulted in matching MIC");
885 sm->alt_snonce_valid = 0;
886 os_memcpy(sm->SNonce, sm->alt_SNonce, WPA_NONCE_LEN);
887 os_memcpy(&sm->PTK, &PTK, sizeof(PTK));
888 sm->PTK_valid = TRUE;
889
890 return 0;
891}
892
893
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700894void wpa_receive(struct wpa_authenticator *wpa_auth,
895 struct wpa_state_machine *sm,
896 u8 *data, size_t data_len)
897{
898 struct ieee802_1x_hdr *hdr;
899 struct wpa_eapol_key *key;
900 u16 key_info, key_data_length;
901 enum { PAIRWISE_2, PAIRWISE_4, GROUP_2, REQUEST,
902 SMK_M1, SMK_M3, SMK_ERROR } msg;
903 char *msgtxt;
904 struct wpa_eapol_ie_parse kde;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800905 const u8 *key_data;
906 size_t keyhdrlen, mic_len;
907 u8 *mic;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700908
909 if (wpa_auth == NULL || !wpa_auth->conf.wpa || sm == NULL)
910 return;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800911 wpa_hexdump(MSG_MSGDUMP, "WPA: RX EAPOL data", data, data_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700912
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800913 mic_len = wpa_mic_len(sm->wpa_key_mgmt);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800914 keyhdrlen = sizeof(*key) + mic_len + 2;
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800915
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800916 if (data_len < sizeof(*hdr) + keyhdrlen) {
917 wpa_printf(MSG_DEBUG, "WPA: Ignore too short EAPOL-Key frame");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700918 return;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800919 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700920
921 hdr = (struct ieee802_1x_hdr *) data;
922 key = (struct wpa_eapol_key *) (hdr + 1);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800923 mic = (u8 *) (key + 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700924 key_info = WPA_GET_BE16(key->key_info);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800925 key_data = mic + mic_len + 2;
926 key_data_length = WPA_GET_BE16(mic + mic_len);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800927 wpa_printf(MSG_DEBUG, "WPA: Received EAPOL-Key from " MACSTR
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800928 " key_info=0x%x type=%u mic_len=%u key_data_length=%u",
929 MAC2STR(sm->addr), key_info, key->type,
930 (unsigned int) mic_len, key_data_length);
931 wpa_hexdump(MSG_MSGDUMP,
932 "WPA: EAPOL-Key header (ending before Key MIC)",
933 key, sizeof(*key));
934 wpa_hexdump(MSG_MSGDUMP, "WPA: EAPOL-Key Key MIC",
935 mic, mic_len);
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800936 if (key_data_length > data_len - sizeof(*hdr) - keyhdrlen) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700937 wpa_printf(MSG_INFO, "WPA: Invalid EAPOL-Key frame - "
938 "key_data overflow (%d > %lu)",
939 key_data_length,
940 (unsigned long) (data_len - sizeof(*hdr) -
Dmitry Shmidt807291d2015-01-27 13:40:23 -0800941 keyhdrlen));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700942 return;
943 }
944
945 if (sm->wpa == WPA_VERSION_WPA2) {
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800946 if (key->type == EAPOL_KEY_TYPE_WPA) {
947 /*
948 * Some deployed station implementations seem to send
949 * msg 4/4 with incorrect type value in WPA2 mode.
950 */
951 wpa_printf(MSG_DEBUG, "Workaround: Allow EAPOL-Key "
952 "with unexpected WPA type in RSN mode");
953 } else if (key->type != EAPOL_KEY_TYPE_RSN) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700954 wpa_printf(MSG_DEBUG, "Ignore EAPOL-Key with "
955 "unexpected type %d in RSN mode",
956 key->type);
957 return;
958 }
959 } else {
960 if (key->type != EAPOL_KEY_TYPE_WPA) {
961 wpa_printf(MSG_DEBUG, "Ignore EAPOL-Key with "
962 "unexpected type %d in WPA mode",
963 key->type);
964 return;
965 }
966 }
967
968 wpa_hexdump(MSG_DEBUG, "WPA: Received Key Nonce", key->key_nonce,
969 WPA_NONCE_LEN);
970 wpa_hexdump(MSG_DEBUG, "WPA: Received Replay Counter",
971 key->replay_counter, WPA_REPLAY_COUNTER_LEN);
972
973 /* FIX: verify that the EAPOL-Key frame was encrypted if pairwise keys
974 * are set */
975
976 if ((key_info & (WPA_KEY_INFO_SMK_MESSAGE | WPA_KEY_INFO_REQUEST)) ==
977 (WPA_KEY_INFO_SMK_MESSAGE | WPA_KEY_INFO_REQUEST)) {
978 if (key_info & WPA_KEY_INFO_ERROR) {
979 msg = SMK_ERROR;
980 msgtxt = "SMK Error";
981 } else {
982 msg = SMK_M1;
983 msgtxt = "SMK M1";
984 }
985 } else if (key_info & WPA_KEY_INFO_SMK_MESSAGE) {
986 msg = SMK_M3;
987 msgtxt = "SMK M3";
988 } else if (key_info & WPA_KEY_INFO_REQUEST) {
989 msg = REQUEST;
990 msgtxt = "Request";
991 } else if (!(key_info & WPA_KEY_INFO_KEY_TYPE)) {
992 msg = GROUP_2;
993 msgtxt = "2/2 Group";
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -0800994 } else if (key_data_length == 0 ||
995 (mic_len == 0 && (key_info & WPA_KEY_INFO_ENCR_KEY_DATA) &&
996 key_data_length == AES_BLOCK_SIZE)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700997 msg = PAIRWISE_4;
998 msgtxt = "4/4 Pairwise";
999 } else {
1000 msg = PAIRWISE_2;
1001 msgtxt = "2/4 Pairwise";
1002 }
1003
1004 /* TODO: key_info type validation for PeerKey */
1005 if (msg == REQUEST || msg == PAIRWISE_2 || msg == PAIRWISE_4 ||
1006 msg == GROUP_2) {
1007 u16 ver = key_info & WPA_KEY_INFO_TYPE_MASK;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001008 if (sm->pairwise == WPA_CIPHER_CCMP ||
1009 sm->pairwise == WPA_CIPHER_GCMP) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001010 if (wpa_use_aes_cmac(sm) &&
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001011 sm->wpa_key_mgmt != WPA_KEY_MGMT_OSEN &&
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001012 !wpa_key_mgmt_suite_b(sm->wpa_key_mgmt) &&
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001013 !wpa_key_mgmt_fils(sm->wpa_key_mgmt) &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001014 ver != WPA_KEY_INFO_TYPE_AES_128_CMAC) {
1015 wpa_auth_logger(wpa_auth, sm->addr,
1016 LOGGER_WARNING,
1017 "advertised support for "
1018 "AES-128-CMAC, but did not "
1019 "use it");
1020 return;
1021 }
1022
1023 if (!wpa_use_aes_cmac(sm) &&
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001024 !wpa_key_mgmt_fils(sm->wpa_key_mgmt) &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001025 ver != WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
1026 wpa_auth_logger(wpa_auth, sm->addr,
1027 LOGGER_WARNING,
1028 "did not use HMAC-SHA1-AES "
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001029 "with CCMP/GCMP");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001030 return;
1031 }
1032 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001033
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001034 if ((wpa_key_mgmt_suite_b(sm->wpa_key_mgmt) ||
1035 wpa_key_mgmt_fils(sm->wpa_key_mgmt)) &&
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001036 ver != WPA_KEY_INFO_TYPE_AKM_DEFINED) {
1037 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_WARNING,
1038 "did not use EAPOL-Key descriptor version 0 as required for AKM-defined cases");
1039 return;
1040 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001041 }
1042
1043 if (key_info & WPA_KEY_INFO_REQUEST) {
1044 if (sm->req_replay_counter_used &&
1045 os_memcmp(key->replay_counter, sm->req_replay_counter,
1046 WPA_REPLAY_COUNTER_LEN) <= 0) {
1047 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_WARNING,
1048 "received EAPOL-Key request with "
1049 "replayed counter");
1050 return;
1051 }
1052 }
1053
1054 if (!(key_info & WPA_KEY_INFO_REQUEST) &&
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001055 !wpa_replay_counter_valid(sm->key_replay, key->replay_counter)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001056 int i;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001057
1058 if (msg == PAIRWISE_2 &&
1059 wpa_replay_counter_valid(sm->prev_key_replay,
1060 key->replay_counter) &&
1061 sm->wpa_ptk_state == WPA_PTK_PTKINITNEGOTIATING &&
1062 os_memcmp(sm->SNonce, key->key_nonce, WPA_NONCE_LEN) != 0)
1063 {
1064 /*
1065 * Some supplicant implementations (e.g., Windows XP
1066 * WZC) update SNonce for each EAPOL-Key 2/4. This
1067 * breaks the workaround on accepting any of the
1068 * pending requests, so allow the SNonce to be updated
1069 * even if we have already sent out EAPOL-Key 3/4.
1070 */
1071 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_DEBUG,
1072 "Process SNonce update from STA "
1073 "based on retransmitted EAPOL-Key "
1074 "1/4");
1075 sm->update_snonce = 1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001076 os_memcpy(sm->alt_SNonce, sm->SNonce, WPA_NONCE_LEN);
1077 sm->alt_snonce_valid = TRUE;
1078 os_memcpy(sm->alt_replay_counter,
1079 sm->key_replay[0].counter,
1080 WPA_REPLAY_COUNTER_LEN);
1081 goto continue_processing;
1082 }
1083
1084 if (msg == PAIRWISE_4 && sm->alt_snonce_valid &&
1085 sm->wpa_ptk_state == WPA_PTK_PTKINITNEGOTIATING &&
1086 os_memcmp(key->replay_counter, sm->alt_replay_counter,
1087 WPA_REPLAY_COUNTER_LEN) == 0) {
1088 /*
1089 * Supplicant may still be using the old SNonce since
1090 * there was two EAPOL-Key 2/4 messages and they had
1091 * different SNonce values.
1092 */
1093 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_DEBUG,
1094 "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 -08001095 goto continue_processing;
1096 }
1097
1098 if (msg == PAIRWISE_2 &&
1099 wpa_replay_counter_valid(sm->prev_key_replay,
1100 key->replay_counter) &&
1101 sm->wpa_ptk_state == WPA_PTK_PTKINITNEGOTIATING) {
1102 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_DEBUG,
1103 "ignore retransmitted EAPOL-Key %s - "
1104 "SNonce did not change", msgtxt);
1105 } else {
1106 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_DEBUG,
1107 "received EAPOL-Key %s with "
1108 "unexpected replay counter", msgtxt);
1109 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001110 for (i = 0; i < RSNA_MAX_EAPOL_RETRIES; i++) {
1111 if (!sm->key_replay[i].valid)
1112 break;
1113 wpa_hexdump(MSG_DEBUG, "pending replay counter",
1114 sm->key_replay[i].counter,
1115 WPA_REPLAY_COUNTER_LEN);
1116 }
1117 wpa_hexdump(MSG_DEBUG, "received replay counter",
1118 key->replay_counter, WPA_REPLAY_COUNTER_LEN);
1119 return;
1120 }
1121
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001122continue_processing:
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001123#ifdef CONFIG_FILS
1124 if (sm->wpa == WPA_VERSION_WPA2 && mic_len == 0 &&
1125 !(key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
1126 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_DEBUG,
1127 "WPA: Encr Key Data bit not set even though AEAD cipher is supposed to be used - drop frame");
1128 return;
1129 }
1130#endif /* CONFIG_FILS */
1131
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001132 switch (msg) {
1133 case PAIRWISE_2:
1134 if (sm->wpa_ptk_state != WPA_PTK_PTKSTART &&
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001135 sm->wpa_ptk_state != WPA_PTK_PTKCALCNEGOTIATING &&
1136 (!sm->update_snonce ||
1137 sm->wpa_ptk_state != WPA_PTK_PTKINITNEGOTIATING)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001138 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_INFO,
1139 "received EAPOL-Key msg 2/4 in "
1140 "invalid state (%d) - dropped",
1141 sm->wpa_ptk_state);
1142 return;
1143 }
1144 random_add_randomness(key->key_nonce, WPA_NONCE_LEN);
1145 if (sm->group->reject_4way_hs_for_entropy) {
1146 /*
1147 * The system did not have enough entropy to generate
1148 * strong random numbers. Reject the first 4-way
1149 * handshake(s) and collect some entropy based on the
1150 * information from it. Once enough entropy is
1151 * available, the next atempt will trigger GMK/Key
1152 * Counter update and the station will be allowed to
1153 * continue.
1154 */
1155 wpa_printf(MSG_DEBUG, "WPA: Reject 4-way handshake to "
1156 "collect more entropy for random number "
1157 "generation");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001158 random_mark_pool_ready();
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001159 wpa_sta_disconnect(wpa_auth, sm->addr);
1160 return;
1161 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001162 break;
1163 case PAIRWISE_4:
1164 if (sm->wpa_ptk_state != WPA_PTK_PTKINITNEGOTIATING ||
1165 !sm->PTK_valid) {
1166 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_INFO,
1167 "received EAPOL-Key msg 4/4 in "
1168 "invalid state (%d) - dropped",
1169 sm->wpa_ptk_state);
1170 return;
1171 }
1172 break;
1173 case GROUP_2:
1174 if (sm->wpa_ptk_group_state != WPA_PTK_GROUP_REKEYNEGOTIATING
1175 || !sm->PTK_valid) {
1176 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_INFO,
1177 "received EAPOL-Key msg 2/2 in "
1178 "invalid state (%d) - dropped",
1179 sm->wpa_ptk_group_state);
1180 return;
1181 }
1182 break;
1183#ifdef CONFIG_PEERKEY
1184 case SMK_M1:
1185 case SMK_M3:
1186 case SMK_ERROR:
1187 if (!wpa_auth->conf.peerkey) {
1188 wpa_printf(MSG_DEBUG, "RSN: SMK M1/M3/Error, but "
1189 "PeerKey use disabled - ignoring message");
1190 return;
1191 }
1192 if (!sm->PTK_valid) {
1193 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
1194 "received EAPOL-Key msg SMK in "
1195 "invalid state - dropped");
1196 return;
1197 }
1198 break;
1199#else /* CONFIG_PEERKEY */
1200 case SMK_M1:
1201 case SMK_M3:
1202 case SMK_ERROR:
1203 return; /* STSL disabled - ignore SMK messages */
1204#endif /* CONFIG_PEERKEY */
1205 case REQUEST:
1206 break;
1207 }
1208
1209 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_DEBUG,
1210 "received EAPOL-Key frame (%s)", msgtxt);
1211
1212 if (key_info & WPA_KEY_INFO_ACK) {
1213 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
1214 "received invalid EAPOL-Key: Key Ack set");
1215 return;
1216 }
1217
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001218 if (!wpa_key_mgmt_fils(sm->wpa_key_mgmt) &&
1219 !(key_info & WPA_KEY_INFO_MIC)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001220 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
1221 "received invalid EAPOL-Key: Key MIC not set");
1222 return;
1223 }
1224
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001225#ifdef CONFIG_FILS
1226 if (wpa_key_mgmt_fils(sm->wpa_key_mgmt) &&
1227 (key_info & WPA_KEY_INFO_MIC)) {
1228 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
1229 "received invalid EAPOL-Key: Key MIC set");
1230 return;
1231 }
1232#endif /* CONFIG_FILS */
1233
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001234 sm->MICVerified = FALSE;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001235 if (sm->PTK_valid && !sm->update_snonce) {
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001236 if (mic_len &&
1237 wpa_verify_key_mic(sm->wpa_key_mgmt, &sm->PTK, data,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001238 data_len) &&
1239 (msg != PAIRWISE_4 || !sm->alt_snonce_valid ||
1240 wpa_try_alt_snonce(sm, data, data_len))) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001241 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
1242 "received EAPOL-Key with invalid MIC");
1243 return;
1244 }
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001245#ifdef CONFIG_FILS
1246 if (!mic_len &&
1247 wpa_aead_decrypt(sm, &sm->PTK, data, data_len,
1248 &key_data_length) < 0) {
1249 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
1250 "received EAPOL-Key with invalid MIC");
1251 return;
1252 }
1253#endif /* CONFIG_FILS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001254 sm->MICVerified = TRUE;
1255 eloop_cancel_timeout(wpa_send_eapol_timeout, wpa_auth, sm);
1256 sm->pending_1_of_4_timeout = 0;
1257 }
1258
1259 if (key_info & WPA_KEY_INFO_REQUEST) {
1260 if (sm->MICVerified) {
1261 sm->req_replay_counter_used = 1;
1262 os_memcpy(sm->req_replay_counter, key->replay_counter,
1263 WPA_REPLAY_COUNTER_LEN);
1264 } else {
1265 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
1266 "received EAPOL-Key request with "
1267 "invalid MIC");
1268 return;
1269 }
1270
1271 /*
1272 * TODO: should decrypt key data field if encryption was used;
1273 * even though MAC address KDE is not normally encrypted,
1274 * supplicant is allowed to encrypt it.
1275 */
1276 if (msg == SMK_ERROR) {
1277#ifdef CONFIG_PEERKEY
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001278 wpa_smk_error(wpa_auth, sm, key_data, key_data_length);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001279#endif /* CONFIG_PEERKEY */
1280 return;
1281 } else if (key_info & WPA_KEY_INFO_ERROR) {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001282 if (wpa_receive_error_report(
1283 wpa_auth, sm,
1284 !(key_info & WPA_KEY_INFO_KEY_TYPE)) > 0)
1285 return; /* STA entry was removed */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001286 } else if (key_info & WPA_KEY_INFO_KEY_TYPE) {
1287 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
1288 "received EAPOL-Key Request for new "
1289 "4-Way Handshake");
1290 wpa_request_new_ptk(sm);
1291#ifdef CONFIG_PEERKEY
1292 } else if (msg == SMK_M1) {
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001293 wpa_smk_m1(wpa_auth, sm, key, key_data,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001294 key_data_length);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001295#endif /* CONFIG_PEERKEY */
1296 } else if (key_data_length > 0 &&
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001297 wpa_parse_kde_ies(key_data, key_data_length,
1298 &kde) == 0 &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001299 kde.mac_addr) {
1300 } else {
1301 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
1302 "received EAPOL-Key Request for GTK "
1303 "rekeying");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001304 eloop_cancel_timeout(wpa_rekey_gtk, wpa_auth, NULL);
1305 wpa_rekey_gtk(wpa_auth, NULL);
1306 }
1307 } else {
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001308 /* Do not allow the same key replay counter to be reused. */
1309 wpa_replay_counter_mark_invalid(sm->key_replay,
1310 key->replay_counter);
1311
1312 if (msg == PAIRWISE_2) {
1313 /*
1314 * Maintain a copy of the pending EAPOL-Key frames in
1315 * case the EAPOL-Key frame was retransmitted. This is
1316 * needed to allow EAPOL-Key msg 2/4 reply to another
1317 * pending msg 1/4 to update the SNonce to work around
1318 * unexpected supplicant behavior.
1319 */
1320 os_memcpy(sm->prev_key_replay, sm->key_replay,
1321 sizeof(sm->key_replay));
1322 } else {
1323 os_memset(sm->prev_key_replay, 0,
1324 sizeof(sm->prev_key_replay));
1325 }
1326
1327 /*
1328 * Make sure old valid counters are not accepted anymore and
1329 * do not get copied again.
1330 */
1331 wpa_replay_counter_mark_invalid(sm->key_replay, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001332 }
1333
1334#ifdef CONFIG_PEERKEY
1335 if (msg == SMK_M3) {
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001336 wpa_smk_m3(wpa_auth, sm, key, key_data, key_data_length);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001337 return;
1338 }
1339#endif /* CONFIG_PEERKEY */
1340
1341 os_free(sm->last_rx_eapol_key);
1342 sm->last_rx_eapol_key = os_malloc(data_len);
1343 if (sm->last_rx_eapol_key == NULL)
1344 return;
1345 os_memcpy(sm->last_rx_eapol_key, data, data_len);
1346 sm->last_rx_eapol_key_len = data_len;
1347
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001348 sm->rx_eapol_key_secure = !!(key_info & WPA_KEY_INFO_SECURE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001349 sm->EAPOLKeyReceived = TRUE;
1350 sm->EAPOLKeyPairwise = !!(key_info & WPA_KEY_INFO_KEY_TYPE);
1351 sm->EAPOLKeyRequest = !!(key_info & WPA_KEY_INFO_REQUEST);
1352 os_memcpy(sm->SNonce, key->key_nonce, WPA_NONCE_LEN);
1353 wpa_sm_step(sm);
1354}
1355
1356
1357static int wpa_gmk_to_gtk(const u8 *gmk, const char *label, const u8 *addr,
1358 const u8 *gnonce, u8 *gtk, size_t gtk_len)
1359{
1360 u8 data[ETH_ALEN + WPA_NONCE_LEN + 8 + 16];
1361 u8 *pos;
1362 int ret = 0;
1363
1364 /* GTK = PRF-X(GMK, "Group key expansion",
1365 * AA || GNonce || Time || random data)
1366 * The example described in the IEEE 802.11 standard uses only AA and
1367 * GNonce as inputs here. Add some more entropy since this derivation
1368 * is done only at the Authenticator and as such, does not need to be
1369 * exactly same.
1370 */
1371 os_memcpy(data, addr, ETH_ALEN);
1372 os_memcpy(data + ETH_ALEN, gnonce, WPA_NONCE_LEN);
1373 pos = data + ETH_ALEN + WPA_NONCE_LEN;
1374 wpa_get_ntp_timestamp(pos);
1375 pos += 8;
1376 if (random_get_bytes(pos, 16) < 0)
1377 ret = -1;
1378
1379#ifdef CONFIG_IEEE80211W
1380 sha256_prf(gmk, WPA_GMK_LEN, label, data, sizeof(data), gtk, gtk_len);
1381#else /* CONFIG_IEEE80211W */
1382 if (sha1_prf(gmk, WPA_GMK_LEN, label, data, sizeof(data), gtk, gtk_len)
1383 < 0)
1384 ret = -1;
1385#endif /* CONFIG_IEEE80211W */
1386
1387 return ret;
1388}
1389
1390
1391static void wpa_send_eapol_timeout(void *eloop_ctx, void *timeout_ctx)
1392{
1393 struct wpa_authenticator *wpa_auth = eloop_ctx;
1394 struct wpa_state_machine *sm = timeout_ctx;
1395
1396 sm->pending_1_of_4_timeout = 0;
1397 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG, "EAPOL-Key timeout");
1398 sm->TimeoutEvt = TRUE;
1399 wpa_sm_step(sm);
1400}
1401
1402
1403void __wpa_send_eapol(struct wpa_authenticator *wpa_auth,
1404 struct wpa_state_machine *sm, int key_info,
1405 const u8 *key_rsc, const u8 *nonce,
1406 const u8 *kde, size_t kde_len,
1407 int keyidx, int encr, int force_version)
1408{
1409 struct ieee802_1x_hdr *hdr;
1410 struct wpa_eapol_key *key;
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001411 size_t len, mic_len, keyhdrlen;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001412 int alg;
1413 int key_data_len, pad_len = 0;
1414 u8 *buf, *pos;
1415 int version, pairwise;
1416 int i;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001417 u8 *key_mic, *key_data;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001418
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001419 mic_len = wpa_mic_len(sm->wpa_key_mgmt);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001420 keyhdrlen = sizeof(*key) + mic_len + 2;
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001421
1422 len = sizeof(struct ieee802_1x_hdr) + keyhdrlen;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001423
1424 if (force_version)
1425 version = force_version;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001426 else if (sm->wpa_key_mgmt == WPA_KEY_MGMT_OSEN ||
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001427 wpa_key_mgmt_suite_b(sm->wpa_key_mgmt) ||
1428 wpa_key_mgmt_fils(sm->wpa_key_mgmt))
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001429 version = WPA_KEY_INFO_TYPE_AKM_DEFINED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001430 else if (wpa_use_aes_cmac(sm))
1431 version = WPA_KEY_INFO_TYPE_AES_128_CMAC;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001432 else if (sm->pairwise != WPA_CIPHER_TKIP)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001433 version = WPA_KEY_INFO_TYPE_HMAC_SHA1_AES;
1434 else
1435 version = WPA_KEY_INFO_TYPE_HMAC_MD5_RC4;
1436
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07001437 pairwise = !!(key_info & WPA_KEY_INFO_KEY_TYPE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001438
1439 wpa_printf(MSG_DEBUG, "WPA: Send EAPOL(version=%d secure=%d mic=%d "
1440 "ack=%d install=%d pairwise=%d kde_len=%lu keyidx=%d "
1441 "encr=%d)",
1442 version,
1443 (key_info & WPA_KEY_INFO_SECURE) ? 1 : 0,
1444 (key_info & WPA_KEY_INFO_MIC) ? 1 : 0,
1445 (key_info & WPA_KEY_INFO_ACK) ? 1 : 0,
1446 (key_info & WPA_KEY_INFO_INSTALL) ? 1 : 0,
1447 pairwise, (unsigned long) kde_len, keyidx, encr);
1448
1449 key_data_len = kde_len;
1450
1451 if ((version == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES ||
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001452 sm->wpa_key_mgmt == WPA_KEY_MGMT_OSEN ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001453 wpa_key_mgmt_suite_b(sm->wpa_key_mgmt) ||
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001454 version == WPA_KEY_INFO_TYPE_AES_128_CMAC) && encr) {
1455 pad_len = key_data_len % 8;
1456 if (pad_len)
1457 pad_len = 8 - pad_len;
1458 key_data_len += pad_len + 8;
1459 }
1460
1461 len += key_data_len;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001462 if (!mic_len && encr)
1463 len += AES_BLOCK_SIZE;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001464
1465 hdr = os_zalloc(len);
1466 if (hdr == NULL)
1467 return;
1468 hdr->version = wpa_auth->conf.eapol_version;
1469 hdr->type = IEEE802_1X_TYPE_EAPOL_KEY;
1470 hdr->length = host_to_be16(len - sizeof(*hdr));
1471 key = (struct wpa_eapol_key *) (hdr + 1);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001472 key_mic = (u8 *) (key + 1);
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001473 key_data = ((u8 *) (hdr + 1)) + keyhdrlen;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001474
1475 key->type = sm->wpa == WPA_VERSION_WPA2 ?
1476 EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA;
1477 key_info |= version;
1478 if (encr && sm->wpa == WPA_VERSION_WPA2)
1479 key_info |= WPA_KEY_INFO_ENCR_KEY_DATA;
1480 if (sm->wpa != WPA_VERSION_WPA2)
1481 key_info |= keyidx << WPA_KEY_INFO_KEY_INDEX_SHIFT;
1482 WPA_PUT_BE16(key->key_info, key_info);
1483
1484 alg = pairwise ? sm->pairwise : wpa_auth->conf.wpa_group;
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07001485 WPA_PUT_BE16(key->key_length, wpa_cipher_key_len(alg));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001486 if (key_info & WPA_KEY_INFO_SMK_MESSAGE)
1487 WPA_PUT_BE16(key->key_length, 0);
1488
1489 /* FIX: STSL: what to use as key_replay_counter? */
1490 for (i = RSNA_MAX_EAPOL_RETRIES - 1; i > 0; i--) {
1491 sm->key_replay[i].valid = sm->key_replay[i - 1].valid;
1492 os_memcpy(sm->key_replay[i].counter,
1493 sm->key_replay[i - 1].counter,
1494 WPA_REPLAY_COUNTER_LEN);
1495 }
1496 inc_byte_array(sm->key_replay[0].counter, WPA_REPLAY_COUNTER_LEN);
1497 os_memcpy(key->replay_counter, sm->key_replay[0].counter,
1498 WPA_REPLAY_COUNTER_LEN);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001499 wpa_hexdump(MSG_DEBUG, "WPA: Replay Counter",
1500 key->replay_counter, WPA_REPLAY_COUNTER_LEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001501 sm->key_replay[0].valid = TRUE;
1502
1503 if (nonce)
1504 os_memcpy(key->key_nonce, nonce, WPA_NONCE_LEN);
1505
1506 if (key_rsc)
1507 os_memcpy(key->key_rsc, key_rsc, WPA_KEY_RSC_LEN);
1508
1509 if (kde && !encr) {
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001510 os_memcpy(key_data, kde, kde_len);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001511 WPA_PUT_BE16(key_mic + mic_len, kde_len);
1512#ifdef CONFIG_FILS
1513 } else if (!mic_len) {
1514 const u8 *aad[1];
1515 size_t aad_len[1];
1516
1517 WPA_PUT_BE16(key_mic, AES_BLOCK_SIZE + kde_len);
1518 wpa_hexdump_key(MSG_DEBUG, "Plaintext EAPOL-Key Key Data",
1519 kde, kde_len);
1520
1521 wpa_hexdump_key(MSG_DEBUG, "WPA: KEK",
1522 sm->PTK.kek, sm->PTK.kek_len);
1523 /* AES-SIV AAD from EAPOL protocol version field (inclusive) to
1524 * to Key Data (exclusive). */
1525 aad[0] = (u8 *) hdr;
1526 aad_len[0] = key_mic + 2 - (u8 *) hdr;
1527 if (aes_siv_encrypt(sm->PTK.kek, sm->PTK.kek_len, kde, kde_len,
1528 1, aad, aad_len, key_mic + 2) < 0) {
1529 wpa_printf(MSG_DEBUG, "WPA: AES-SIV encryption failed");
1530 return;
1531 }
1532
1533 wpa_hexdump(MSG_DEBUG, "WPA: Encrypted Key Data from SIV",
1534 key_mic + 2, AES_BLOCK_SIZE + kde_len);
1535#endif /* CONFIG_FILS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001536 } else if (encr && kde) {
1537 buf = os_zalloc(key_data_len);
1538 if (buf == NULL) {
1539 os_free(hdr);
1540 return;
1541 }
1542 pos = buf;
1543 os_memcpy(pos, kde, kde_len);
1544 pos += kde_len;
1545
1546 if (pad_len)
1547 *pos++ = 0xdd;
1548
1549 wpa_hexdump_key(MSG_DEBUG, "Plaintext EAPOL-Key Key Data",
1550 buf, key_data_len);
1551 if (version == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES ||
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001552 sm->wpa_key_mgmt == WPA_KEY_MGMT_OSEN ||
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001553 wpa_key_mgmt_suite_b(sm->wpa_key_mgmt) ||
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001554 version == WPA_KEY_INFO_TYPE_AES_128_CMAC) {
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001555 if (aes_wrap(sm->PTK.kek, sm->PTK.kek_len,
1556 (key_data_len - 8) / 8, buf, key_data)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001557 os_free(hdr);
1558 os_free(buf);
1559 return;
1560 }
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001561 WPA_PUT_BE16(key_mic + mic_len, key_data_len);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001562#ifndef CONFIG_NO_RC4
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001563 } else if (sm->PTK.kek_len == 16) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001564 u8 ek[32];
1565 os_memcpy(key->key_iv,
1566 sm->group->Counter + WPA_NONCE_LEN - 16, 16);
1567 inc_byte_array(sm->group->Counter, WPA_NONCE_LEN);
1568 os_memcpy(ek, key->key_iv, 16);
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001569 os_memcpy(ek + 16, sm->PTK.kek, sm->PTK.kek_len);
1570 os_memcpy(key_data, buf, key_data_len);
1571 rc4_skip(ek, 32, 256, key_data, key_data_len);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001572 WPA_PUT_BE16(key_mic + mic_len, key_data_len);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001573#endif /* CONFIG_NO_RC4 */
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001574 } else {
1575 os_free(hdr);
1576 os_free(buf);
1577 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001578 }
1579 os_free(buf);
1580 }
1581
1582 if (key_info & WPA_KEY_INFO_MIC) {
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001583 if (!sm->PTK_valid || !mic_len) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001584 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG,
1585 "PTK not valid when sending EAPOL-Key "
1586 "frame");
1587 os_free(hdr);
1588 return;
1589 }
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001590
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001591 wpa_eapol_key_mic(sm->PTK.kck, sm->PTK.kck_len,
1592 sm->wpa_key_mgmt, version,
1593 (u8 *) hdr, len, key_mic);
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07001594#ifdef CONFIG_TESTING_OPTIONS
1595 if (!pairwise &&
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07001596 wpa_auth->conf.corrupt_gtk_rekey_mic_probability > 0.0 &&
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07001597 drand48() <
1598 wpa_auth->conf.corrupt_gtk_rekey_mic_probability) {
1599 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
1600 "Corrupting group EAPOL-Key Key MIC");
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001601 key_mic[0]++;
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07001602 }
1603#endif /* CONFIG_TESTING_OPTIONS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001604 }
1605
1606 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_inc_EapolFramesTx,
1607 1);
1608 wpa_auth_send_eapol(wpa_auth, sm->addr, (u8 *) hdr, len,
1609 sm->pairwise_set);
1610 os_free(hdr);
1611}
1612
1613
1614static void wpa_send_eapol(struct wpa_authenticator *wpa_auth,
1615 struct wpa_state_machine *sm, int key_info,
1616 const u8 *key_rsc, const u8 *nonce,
1617 const u8 *kde, size_t kde_len,
1618 int keyidx, int encr)
1619{
1620 int timeout_ms;
1621 int pairwise = key_info & WPA_KEY_INFO_KEY_TYPE;
1622 int ctr;
1623
1624 if (sm == NULL)
1625 return;
1626
1627 __wpa_send_eapol(wpa_auth, sm, key_info, key_rsc, nonce, kde, kde_len,
1628 keyidx, encr, 0);
1629
1630 ctr = pairwise ? sm->TimeoutCtr : sm->GTimeoutCtr;
1631 if (ctr == 1 && wpa_auth->conf.tx_status)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001632 timeout_ms = pairwise ? eapol_key_timeout_first :
1633 eapol_key_timeout_first_group;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001634 else
1635 timeout_ms = eapol_key_timeout_subseq;
1636 if (pairwise && ctr == 1 && !(key_info & WPA_KEY_INFO_MIC))
1637 sm->pending_1_of_4_timeout = 1;
1638 wpa_printf(MSG_DEBUG, "WPA: Use EAPOL-Key timeout of %u ms (retry "
1639 "counter %d)", timeout_ms, ctr);
1640 eloop_register_timeout(timeout_ms / 1000, (timeout_ms % 1000) * 1000,
1641 wpa_send_eapol_timeout, wpa_auth, sm);
1642}
1643
1644
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001645static int wpa_verify_key_mic(int akmp, struct wpa_ptk *PTK, u8 *data,
1646 size_t data_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001647{
1648 struct ieee802_1x_hdr *hdr;
1649 struct wpa_eapol_key *key;
1650 u16 key_info;
1651 int ret = 0;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001652 u8 mic[WPA_EAPOL_KEY_MIC_MAX_LEN], *mic_pos;
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001653 size_t mic_len = wpa_mic_len(akmp);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001654
1655 if (data_len < sizeof(*hdr) + sizeof(*key))
1656 return -1;
1657
1658 hdr = (struct ieee802_1x_hdr *) data;
1659 key = (struct wpa_eapol_key *) (hdr + 1);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001660 mic_pos = (u8 *) (key + 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001661 key_info = WPA_GET_BE16(key->key_info);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001662 os_memcpy(mic, mic_pos, mic_len);
1663 os_memset(mic_pos, 0, mic_len);
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001664 if (wpa_eapol_key_mic(PTK->kck, PTK->kck_len, akmp,
1665 key_info & WPA_KEY_INFO_TYPE_MASK,
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001666 data, data_len, mic_pos) ||
1667 os_memcmp_const(mic, mic_pos, mic_len) != 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001668 ret = -1;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001669 os_memcpy(mic_pos, mic, mic_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001670 return ret;
1671}
1672
1673
1674void wpa_remove_ptk(struct wpa_state_machine *sm)
1675{
1676 sm->PTK_valid = FALSE;
1677 os_memset(&sm->PTK, 0, sizeof(sm->PTK));
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001678 if (wpa_auth_set_key(sm->wpa_auth, 0, WPA_ALG_NONE, sm->addr, 0, NULL,
1679 0))
1680 wpa_printf(MSG_DEBUG,
1681 "RSN: PTK removal from the driver failed");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001682 sm->pairwise_set = FALSE;
1683 eloop_cancel_timeout(wpa_rekey_ptk, sm->wpa_auth, sm);
1684}
1685
1686
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001687int wpa_auth_sm_event(struct wpa_state_machine *sm, enum wpa_event event)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001688{
1689 int remove_ptk = 1;
1690
1691 if (sm == NULL)
1692 return -1;
1693
1694 wpa_auth_vlogger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
1695 "event %d notification", event);
1696
1697 switch (event) {
1698 case WPA_AUTH:
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001699#ifdef CONFIG_MESH
1700 /* PTKs are derived through AMPE */
1701 if (wpa_auth_start_ampe(sm->wpa_auth, sm->addr)) {
1702 /* not mesh */
1703 break;
1704 }
1705 return 0;
1706#endif /* CONFIG_MESH */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001707 case WPA_ASSOC:
1708 break;
1709 case WPA_DEAUTH:
1710 case WPA_DISASSOC:
1711 sm->DeauthenticationRequest = TRUE;
1712 break;
1713 case WPA_REAUTH:
1714 case WPA_REAUTH_EAPOL:
1715 if (!sm->started) {
1716 /*
1717 * When using WPS, we may end up here if the STA
1718 * manages to re-associate without the previous STA
1719 * entry getting removed. Consequently, we need to make
1720 * sure that the WPA state machines gets initialized
1721 * properly at this point.
1722 */
1723 wpa_printf(MSG_DEBUG, "WPA state machine had not been "
1724 "started - initialize now");
1725 sm->started = 1;
1726 sm->Init = TRUE;
1727 if (wpa_sm_step(sm) == 1)
1728 return 1; /* should not really happen */
1729 sm->Init = FALSE;
1730 sm->AuthenticationRequest = TRUE;
1731 break;
1732 }
1733 if (sm->GUpdateStationKeys) {
1734 /*
1735 * Reauthentication cancels the pending group key
1736 * update for this STA.
1737 */
1738 sm->group->GKeyDoneStations--;
1739 sm->GUpdateStationKeys = FALSE;
1740 sm->PtkGroupInit = TRUE;
1741 }
1742 sm->ReAuthenticationRequest = TRUE;
1743 break;
1744 case WPA_ASSOC_FT:
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001745#ifdef CONFIG_IEEE80211R_AP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001746 wpa_printf(MSG_DEBUG, "FT: Retry PTK configuration "
1747 "after association");
1748 wpa_ft_install_ptk(sm);
1749
1750 /* Using FT protocol, not WPA auth state machine */
1751 sm->ft_completed = 1;
1752 return 0;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001753#else /* CONFIG_IEEE80211R_AP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001754 break;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001755#endif /* CONFIG_IEEE80211R_AP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001756 }
1757
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001758#ifdef CONFIG_IEEE80211R_AP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001759 sm->ft_completed = 0;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001760#endif /* CONFIG_IEEE80211R_AP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001761
1762#ifdef CONFIG_IEEE80211W
1763 if (sm->mgmt_frame_prot && event == WPA_AUTH)
1764 remove_ptk = 0;
1765#endif /* CONFIG_IEEE80211W */
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001766#ifdef CONFIG_FILS
1767 if (wpa_key_mgmt_fils(sm->wpa_key_mgmt) &&
1768 (event == WPA_AUTH || event == WPA_ASSOC))
1769 remove_ptk = 0;
1770#endif /* CONFIG_FILS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001771
1772 if (remove_ptk) {
1773 sm->PTK_valid = FALSE;
1774 os_memset(&sm->PTK, 0, sizeof(sm->PTK));
1775
1776 if (event != WPA_REAUTH_EAPOL)
1777 wpa_remove_ptk(sm);
1778 }
1779
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001780 if (sm->in_step_loop) {
1781 /*
1782 * wpa_sm_step() is already running - avoid recursive call to
1783 * it by making the existing loop process the new update.
1784 */
1785 sm->changed = TRUE;
1786 return 0;
1787 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001788 return wpa_sm_step(sm);
1789}
1790
1791
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001792SM_STATE(WPA_PTK, INITIALIZE)
1793{
1794 SM_ENTRY_MA(WPA_PTK, INITIALIZE, wpa_ptk);
1795 if (sm->Init) {
1796 /* Init flag is not cleared here, so avoid busy
1797 * loop by claiming nothing changed. */
1798 sm->changed = FALSE;
1799 }
1800
1801 sm->keycount = 0;
1802 if (sm->GUpdateStationKeys)
1803 sm->group->GKeyDoneStations--;
1804 sm->GUpdateStationKeys = FALSE;
1805 if (sm->wpa == WPA_VERSION_WPA)
1806 sm->PInitAKeys = FALSE;
1807 if (1 /* Unicast cipher supported AND (ESS OR ((IBSS or WDS) and
1808 * Local AA > Remote AA)) */) {
1809 sm->Pair = TRUE;
1810 }
1811 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portEnabled, 0);
1812 wpa_remove_ptk(sm);
1813 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portValid, 0);
1814 sm->TimeoutCtr = 0;
1815 if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt)) {
1816 wpa_auth_set_eapol(sm->wpa_auth, sm->addr,
1817 WPA_EAPOL_authorized, 0);
1818 }
1819}
1820
1821
1822SM_STATE(WPA_PTK, DISCONNECT)
1823{
1824 SM_ENTRY_MA(WPA_PTK, DISCONNECT, wpa_ptk);
1825 sm->Disconnect = FALSE;
1826 wpa_sta_disconnect(sm->wpa_auth, sm->addr);
1827}
1828
1829
1830SM_STATE(WPA_PTK, DISCONNECTED)
1831{
1832 SM_ENTRY_MA(WPA_PTK, DISCONNECTED, wpa_ptk);
1833 sm->DeauthenticationRequest = FALSE;
1834}
1835
1836
1837SM_STATE(WPA_PTK, AUTHENTICATION)
1838{
1839 SM_ENTRY_MA(WPA_PTK, AUTHENTICATION, wpa_ptk);
1840 os_memset(&sm->PTK, 0, sizeof(sm->PTK));
1841 sm->PTK_valid = FALSE;
1842 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portControl_Auto,
1843 1);
1844 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portEnabled, 1);
1845 sm->AuthenticationRequest = FALSE;
1846}
1847
1848
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001849static void wpa_group_ensure_init(struct wpa_authenticator *wpa_auth,
1850 struct wpa_group *group)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001851{
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001852 if (group->first_sta_seen)
1853 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001854 /*
1855 * System has run bit further than at the time hostapd was started
1856 * potentially very early during boot up. This provides better chances
1857 * of collecting more randomness on embedded systems. Re-initialize the
1858 * GMK and Counter here to improve their strength if there was not
1859 * enough entropy available immediately after system startup.
1860 */
1861 wpa_printf(MSG_DEBUG, "WPA: Re-initialize GMK/Counter on first "
1862 "station");
1863 if (random_pool_ready() != 1) {
1864 wpa_printf(MSG_INFO, "WPA: Not enough entropy in random pool "
1865 "to proceed - reject first 4-way handshake");
1866 group->reject_4way_hs_for_entropy = TRUE;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001867 } else {
1868 group->first_sta_seen = TRUE;
1869 group->reject_4way_hs_for_entropy = FALSE;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001870 }
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001871
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001872 if (wpa_group_init_gmk_and_counter(wpa_auth, group) < 0 ||
1873 wpa_gtk_update(wpa_auth, group) < 0 ||
1874 wpa_group_config_group_keys(wpa_auth, group) < 0) {
1875 wpa_printf(MSG_INFO, "WPA: GMK/GTK setup failed");
1876 group->first_sta_seen = FALSE;
1877 group->reject_4way_hs_for_entropy = TRUE;
1878 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001879}
1880
1881
1882SM_STATE(WPA_PTK, AUTHENTICATION2)
1883{
1884 SM_ENTRY_MA(WPA_PTK, AUTHENTICATION2, wpa_ptk);
1885
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001886 wpa_group_ensure_init(sm->wpa_auth, sm->group);
Dmitry Shmidt444d5672013-04-01 13:08:44 -07001887 sm->ReAuthenticationRequest = FALSE;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001888
Dmitry Shmidt04949592012-07-19 12:16:46 -07001889 /*
1890 * Definition of ANonce selection in IEEE Std 802.11i-2004 is somewhat
1891 * ambiguous. The Authenticator state machine uses a counter that is
1892 * incremented by one for each 4-way handshake. However, the security
1893 * analysis of 4-way handshake points out that unpredictable nonces
1894 * help in preventing precomputation attacks. Instead of the state
1895 * machine definition, use an unpredictable nonce value here to provide
1896 * stronger protection against potential precomputation attacks.
1897 */
1898 if (random_get_bytes(sm->ANonce, WPA_NONCE_LEN)) {
1899 wpa_printf(MSG_ERROR, "WPA: Failed to get random data for "
1900 "ANonce.");
Dmitry Shmidt444d5672013-04-01 13:08:44 -07001901 sm->Disconnect = TRUE;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001902 return;
1903 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001904 wpa_hexdump(MSG_DEBUG, "WPA: Assign ANonce", sm->ANonce,
1905 WPA_NONCE_LEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001906 /* IEEE 802.11i does not clear TimeoutCtr here, but this is more
1907 * logical place than INITIALIZE since AUTHENTICATION2 can be
1908 * re-entered on ReAuthenticationRequest without going through
1909 * INITIALIZE. */
1910 sm->TimeoutCtr = 0;
1911}
1912
1913
1914SM_STATE(WPA_PTK, INITPMK)
1915{
1916 u8 msk[2 * PMK_LEN];
1917 size_t len = 2 * PMK_LEN;
1918
1919 SM_ENTRY_MA(WPA_PTK, INITPMK, wpa_ptk);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001920#ifdef CONFIG_IEEE80211R_AP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001921 sm->xxkey_len = 0;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001922#endif /* CONFIG_IEEE80211R_AP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001923 if (sm->pmksa) {
1924 wpa_printf(MSG_DEBUG, "WPA: PMK from PMKSA cache");
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001925 os_memcpy(sm->PMK, sm->pmksa->pmk, sm->pmksa->pmk_len);
1926 sm->pmk_len = sm->pmksa->pmk_len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001927 } else if (wpa_auth_get_msk(sm->wpa_auth, sm->addr, msk, &len) == 0) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001928 unsigned int pmk_len;
1929
1930 if (sm->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X_SUITE_B_192)
1931 pmk_len = PMK_LEN_SUITE_B_192;
1932 else
1933 pmk_len = PMK_LEN;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001934 wpa_printf(MSG_DEBUG, "WPA: PMK from EAPOL state machine "
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001935 "(MSK len=%lu PMK len=%u)", (unsigned long) len,
1936 pmk_len);
1937 if (len < pmk_len) {
1938 wpa_printf(MSG_DEBUG,
1939 "WPA: MSK not long enough (%u) to create PMK (%u)",
1940 (unsigned int) len, (unsigned int) pmk_len);
1941 sm->Disconnect = TRUE;
1942 return;
1943 }
1944 os_memcpy(sm->PMK, msk, pmk_len);
1945 sm->pmk_len = pmk_len;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001946#ifdef CONFIG_IEEE80211R_AP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001947 if (len >= 2 * PMK_LEN) {
1948 os_memcpy(sm->xxkey, msk + PMK_LEN, PMK_LEN);
1949 sm->xxkey_len = PMK_LEN;
1950 }
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001951#endif /* CONFIG_IEEE80211R_AP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001952 } else {
Dmitry Shmidt807291d2015-01-27 13:40:23 -08001953 wpa_printf(MSG_DEBUG, "WPA: Could not get PMK, get_msk: %p",
Paul Stewart092955c2017-02-06 09:13:09 -08001954 sm->wpa_auth->cb->get_msk);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001955 sm->Disconnect = TRUE;
1956 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001957 }
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001958 os_memset(msk, 0, sizeof(msk));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001959
1960 sm->req_replay_counter_used = 0;
1961 /* IEEE 802.11i does not set keyRun to FALSE, but not doing this
1962 * will break reauthentication since EAPOL state machines may not be
1963 * get into AUTHENTICATING state that clears keyRun before WPA state
1964 * machine enters AUTHENTICATION2 state and goes immediately to INITPMK
1965 * state and takes PMK from the previously used AAA Key. This will
1966 * eventually fail in 4-Way Handshake because Supplicant uses PMK
1967 * derived from the new AAA Key. Setting keyRun = FALSE here seems to
1968 * be good workaround for this issue. */
1969 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_keyRun, 0);
1970}
1971
1972
1973SM_STATE(WPA_PTK, INITPSK)
1974{
1975 const u8 *psk;
1976 SM_ENTRY_MA(WPA_PTK, INITPSK, wpa_ptk);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001977 psk = wpa_auth_get_psk(sm->wpa_auth, sm->addr, sm->p2p_dev_addr, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001978 if (psk) {
1979 os_memcpy(sm->PMK, psk, PMK_LEN);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001980 sm->pmk_len = PMK_LEN;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001981#ifdef CONFIG_IEEE80211R_AP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001982 os_memcpy(sm->xxkey, psk, PMK_LEN);
1983 sm->xxkey_len = PMK_LEN;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08001984#endif /* CONFIG_IEEE80211R_AP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001985 }
1986 sm->req_replay_counter_used = 0;
1987}
1988
1989
1990SM_STATE(WPA_PTK, PTKSTART)
1991{
1992 u8 buf[2 + RSN_SELECTOR_LEN + PMKID_LEN], *pmkid = NULL;
1993 size_t pmkid_len = 0;
1994
1995 SM_ENTRY_MA(WPA_PTK, PTKSTART, wpa_ptk);
1996 sm->PTKRequest = FALSE;
1997 sm->TimeoutEvt = FALSE;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001998 sm->alt_snonce_valid = FALSE;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001999
2000 sm->TimeoutCtr++;
2001 if (sm->TimeoutCtr > (int) dot11RSNAConfigPairwiseUpdateCount) {
2002 /* No point in sending the EAPOL-Key - we will disconnect
2003 * immediately following this. */
2004 return;
2005 }
2006
2007 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
2008 "sending 1/4 msg of 4-Way Handshake");
2009 /*
2010 * TODO: Could add PMKID even with WPA2-PSK, but only if there is only
2011 * one possible PSK for this STA.
2012 */
2013 if (sm->wpa == WPA_VERSION_WPA2 &&
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08002014 wpa_key_mgmt_wpa_ieee8021x(sm->wpa_key_mgmt) &&
2015 sm->wpa_key_mgmt != WPA_KEY_MGMT_OSEN) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002016 pmkid = buf;
2017 pmkid_len = 2 + RSN_SELECTOR_LEN + PMKID_LEN;
2018 pmkid[0] = WLAN_EID_VENDOR_SPECIFIC;
2019 pmkid[1] = RSN_SELECTOR_LEN + PMKID_LEN;
2020 RSN_SELECTOR_PUT(&pmkid[2], RSN_KEY_DATA_PMKID);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002021 if (sm->pmksa) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002022 os_memcpy(&pmkid[2 + RSN_SELECTOR_LEN],
2023 sm->pmksa->pmkid, PMKID_LEN);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002024 } else if (wpa_key_mgmt_suite_b(sm->wpa_key_mgmt)) {
2025 /* No KCK available to derive PMKID */
2026 pmkid = NULL;
2027 } else {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002028 /*
2029 * Calculate PMKID since no PMKSA cache entry was
2030 * available with pre-calculated PMKID.
2031 */
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002032 rsn_pmkid(sm->PMK, sm->pmk_len, sm->wpa_auth->addr,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002033 sm->addr, &pmkid[2 + RSN_SELECTOR_LEN],
2034 wpa_key_mgmt_sha256(sm->wpa_key_mgmt));
2035 }
2036 }
2037 wpa_send_eapol(sm->wpa_auth, sm,
2038 WPA_KEY_INFO_ACK | WPA_KEY_INFO_KEY_TYPE, NULL,
2039 sm->ANonce, pmkid, pmkid_len, 0, 0);
2040}
2041
2042
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002043static int wpa_derive_ptk(struct wpa_state_machine *sm, const u8 *snonce,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002044 const u8 *pmk, unsigned int pmk_len,
2045 struct wpa_ptk *ptk)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002046{
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002047#ifdef CONFIG_IEEE80211R_AP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002048 if (wpa_key_mgmt_ft(sm->wpa_key_mgmt))
Dmitry Shmidt807291d2015-01-27 13:40:23 -08002049 return wpa_auth_derive_ptk_ft(sm, pmk, ptk);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002050#endif /* CONFIG_IEEE80211R_AP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002051
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002052 return wpa_pmk_to_ptk(pmk, pmk_len, "Pairwise key expansion",
Dmitry Shmidt807291d2015-01-27 13:40:23 -08002053 sm->wpa_auth->addr, sm->addr, sm->ANonce, snonce,
2054 ptk, sm->wpa_key_mgmt, sm->pairwise);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002055}
2056
2057
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002058#ifdef CONFIG_FILS
2059
2060int fils_auth_pmk_to_ptk(struct wpa_state_machine *sm, const u8 *pmk,
2061 size_t pmk_len, const u8 *snonce, const u8 *anonce)
2062{
2063 u8 ick[FILS_ICK_MAX_LEN];
2064 size_t ick_len;
2065 int res;
2066
2067 res = fils_pmk_to_ptk(pmk, pmk_len, sm->addr, sm->wpa_auth->addr,
2068 snonce, anonce, &sm->PTK, ick, &ick_len,
2069 sm->wpa_key_mgmt, sm->pairwise);
2070 if (res < 0)
2071 return res;
2072 sm->PTK_valid = TRUE;
2073
2074 res = fils_key_auth_sk(ick, ick_len, snonce, anonce,
2075 sm->addr, sm->wpa_auth->addr,
2076 NULL, 0, NULL, 0, /* TODO: SK+PFS */
2077 sm->wpa_key_mgmt, sm->fils_key_auth_sta,
2078 sm->fils_key_auth_ap,
2079 &sm->fils_key_auth_len);
2080 os_memset(ick, 0, sizeof(ick));
2081
2082 /* Store nonces for (Re)Association Request/Response frame processing */
2083 os_memcpy(sm->SNonce, snonce, FILS_NONCE_LEN);
2084 os_memcpy(sm->ANonce, anonce, FILS_NONCE_LEN);
2085
2086 return res;
2087}
2088
2089
2090static int wpa_aead_decrypt(struct wpa_state_machine *sm, struct wpa_ptk *ptk,
2091 u8 *buf, size_t buf_len, u16 *_key_data_len)
2092{
2093 struct ieee802_1x_hdr *hdr;
2094 struct wpa_eapol_key *key;
2095 u8 *pos;
2096 u16 key_data_len;
2097 u8 *tmp;
2098 const u8 *aad[1];
2099 size_t aad_len[1];
2100
2101 hdr = (struct ieee802_1x_hdr *) buf;
2102 key = (struct wpa_eapol_key *) (hdr + 1);
2103 pos = (u8 *) (key + 1);
2104 key_data_len = WPA_GET_BE16(pos);
2105 if (key_data_len < AES_BLOCK_SIZE ||
2106 key_data_len > buf_len - sizeof(*hdr) - sizeof(*key) - 2) {
2107 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_INFO,
2108 "No room for AES-SIV data in the frame");
2109 return -1;
2110 }
2111 pos += 2; /* Pointing at the Encrypted Key Data field */
2112
2113 tmp = os_malloc(key_data_len);
2114 if (!tmp)
2115 return -1;
2116
2117 /* AES-SIV AAD from EAPOL protocol version field (inclusive) to
2118 * to Key Data (exclusive). */
2119 aad[0] = buf;
2120 aad_len[0] = pos - buf;
2121 if (aes_siv_decrypt(ptk->kek, ptk->kek_len, pos, key_data_len,
2122 1, aad, aad_len, tmp) < 0) {
2123 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_INFO,
2124 "Invalid AES-SIV data in the frame");
2125 bin_clear_free(tmp, key_data_len);
2126 return -1;
2127 }
2128
2129 /* AEAD decryption and validation completed successfully */
2130 key_data_len -= AES_BLOCK_SIZE;
2131 wpa_hexdump_key(MSG_DEBUG, "WPA: Decrypted Key Data",
2132 tmp, key_data_len);
2133
2134 /* Replace Key Data field with the decrypted version */
2135 os_memcpy(pos, tmp, key_data_len);
2136 pos -= 2; /* Key Data Length field */
2137 WPA_PUT_BE16(pos, key_data_len);
2138 bin_clear_free(tmp, key_data_len);
2139 if (_key_data_len)
2140 *_key_data_len = key_data_len;
2141 return 0;
2142}
2143
2144
2145int fils_decrypt_assoc(struct wpa_state_machine *sm, const u8 *fils_session,
2146 const struct ieee80211_mgmt *mgmt, size_t frame_len,
2147 u8 *pos, size_t left)
2148{
2149 u16 fc, stype;
2150 const u8 *end, *ie_start, *ie, *session, *crypt;
2151 struct ieee802_11_elems elems;
2152 const u8 *aad[5];
2153 size_t aad_len[5];
2154
2155 if (!sm || !sm->PTK_valid) {
2156 wpa_printf(MSG_DEBUG,
2157 "FILS: No KEK to decrypt Assocication Request frame");
2158 return -1;
2159 }
2160
2161 if (!wpa_key_mgmt_fils(sm->wpa_key_mgmt)) {
2162 wpa_printf(MSG_DEBUG,
2163 "FILS: Not a FILS AKM - reject association");
2164 return -1;
2165 }
2166
2167 end = ((const u8 *) mgmt) + frame_len;
2168 fc = le_to_host16(mgmt->frame_control);
2169 stype = WLAN_FC_GET_STYPE(fc);
2170 if (stype == WLAN_FC_STYPE_REASSOC_REQ)
2171 ie_start = mgmt->u.reassoc_req.variable;
2172 else
2173 ie_start = mgmt->u.assoc_req.variable;
2174 ie = ie_start;
2175
2176 /*
2177 * Find FILS Session element which is the last unencrypted element in
2178 * the frame.
2179 */
2180 session = NULL;
2181 while (ie + 1 < end) {
2182 if (ie + 2 + ie[1] > end)
2183 break;
2184 if (ie[0] == WLAN_EID_EXTENSION &&
2185 ie[1] >= 1 + FILS_SESSION_LEN &&
2186 ie[2] == WLAN_EID_EXT_FILS_SESSION) {
2187 session = ie;
2188 break;
2189 }
2190 ie += 2 + ie[1];
2191 }
2192
2193 if (!session) {
2194 wpa_printf(MSG_DEBUG,
2195 "FILS: Could not find FILS Session element in Association Request frame - reject");
2196 return -1;
2197 }
2198 if (os_memcmp(fils_session, session + 3, FILS_SESSION_LEN) != 0) {
2199 wpa_printf(MSG_DEBUG, "FILS: Session mismatch");
2200 wpa_hexdump(MSG_DEBUG, "FILS: Expected FILS Session",
2201 fils_session, FILS_SESSION_LEN);
2202 wpa_hexdump(MSG_DEBUG, "FILS: Received FILS Session",
2203 session + 3, FILS_SESSION_LEN);
2204 return -1;
2205 }
2206 crypt = session + 2 + session[1];
2207
2208 if (end - crypt < AES_BLOCK_SIZE) {
2209 wpa_printf(MSG_DEBUG,
2210 "FILS: Too short frame to include AES-SIV data");
2211 return -1;
2212 }
2213
2214 /* AES-SIV AAD vectors */
2215
2216 /* The STA's MAC address */
2217 aad[0] = mgmt->sa;
2218 aad_len[0] = ETH_ALEN;
2219 /* The AP's BSSID */
2220 aad[1] = mgmt->da;
2221 aad_len[1] = ETH_ALEN;
2222 /* The STA's nonce */
2223 aad[2] = sm->SNonce;
2224 aad_len[2] = FILS_NONCE_LEN;
2225 /* The AP's nonce */
2226 aad[3] = sm->ANonce;
2227 aad_len[3] = FILS_NONCE_LEN;
2228 /*
2229 * The (Re)Association Request frame from the Capability Information
2230 * field to the FILS Session element (both inclusive).
2231 */
2232 aad[4] = (const u8 *) &mgmt->u.assoc_req.capab_info;
2233 aad_len[4] = crypt - aad[0];
2234
2235 if (aes_siv_decrypt(sm->PTK.kek, sm->PTK.kek_len, crypt, end - crypt,
2236 1, aad, aad_len, pos + (crypt - ie_start)) < 0) {
2237 wpa_printf(MSG_DEBUG,
2238 "FILS: Invalid AES-SIV data in the frame");
2239 return -1;
2240 }
2241 wpa_hexdump(MSG_DEBUG, "FILS: Decrypted Association Request elements",
2242 pos, left - AES_BLOCK_SIZE);
2243
2244 if (ieee802_11_parse_elems(pos, left - AES_BLOCK_SIZE, &elems, 1) ==
2245 ParseFailed) {
2246 wpa_printf(MSG_DEBUG,
2247 "FILS: Failed to parse decrypted elements");
2248 return -1;
2249 }
2250 if (!elems.fils_key_confirm) {
2251 wpa_printf(MSG_DEBUG, "FILS: No FILS Key Confirm element");
2252 return -1;
2253 }
2254 if (elems.fils_key_confirm_len != sm->fils_key_auth_len) {
2255 wpa_printf(MSG_DEBUG,
2256 "FILS: Unexpected Key-Auth length %d (expected %d)",
2257 elems.fils_key_confirm_len,
2258 (int) sm->fils_key_auth_len);
2259 return -1;
2260 }
2261 if (os_memcmp(elems.fils_key_confirm, sm->fils_key_auth_sta,
2262 sm->fils_key_auth_len) != 0) {
2263 wpa_printf(MSG_DEBUG, "FILS: Key-Auth mismatch");
2264 wpa_hexdump(MSG_DEBUG, "FILS: Received Key-Auth",
2265 elems.fils_key_confirm,
2266 elems.fils_key_confirm_len);
2267 wpa_hexdump(MSG_DEBUG, "FILS: Expected Key-Auth",
2268 sm->fils_key_auth_sta, sm->fils_key_auth_len);
2269 return -1;
2270 }
2271
2272 return left - AES_BLOCK_SIZE;
2273}
2274
2275
2276int fils_encrypt_assoc(struct wpa_state_machine *sm, u8 *buf,
2277 size_t current_len, size_t max_len)
2278{
2279 u8 *end = buf + max_len;
2280 u8 *pos = buf + current_len;
2281 struct ieee80211_mgmt *mgmt;
2282 struct wpabuf *plain;
2283 u8 *len, *tmp, *tmp2;
2284 u8 hdr[2];
2285 u8 *gtk, dummy_gtk[32];
2286 size_t gtk_len;
2287 struct wpa_group *gsm;
2288 const u8 *aad[5];
2289 size_t aad_len[5];
2290
2291 if (!sm || !sm->PTK_valid)
2292 return -1;
2293
2294 wpa_hexdump(MSG_DEBUG,
2295 "FILS: Association Response frame before FILS processing",
2296 buf, current_len);
2297
2298 mgmt = (struct ieee80211_mgmt *) buf;
2299
2300 /* AES-SIV AAD vectors */
2301
2302 /* The AP's BSSID */
2303 aad[0] = mgmt->sa;
2304 aad_len[0] = ETH_ALEN;
2305 /* The STA's MAC address */
2306 aad[1] = mgmt->da;
2307 aad_len[1] = ETH_ALEN;
2308 /* The AP's nonce */
2309 aad[2] = sm->ANonce;
2310 aad_len[2] = FILS_NONCE_LEN;
2311 /* The STA's nonce */
2312 aad[3] = sm->SNonce;
2313 aad_len[3] = FILS_NONCE_LEN;
2314 /*
2315 * The (Re)Association Response frame from the Capability Information
2316 * field (the same offset in both Association and Reassociation
2317 * Response frames) to the FILS Session element (both inclusive).
2318 */
2319 aad[4] = (const u8 *) &mgmt->u.assoc_resp.capab_info;
2320 aad_len[4] = pos - aad[4];
2321
2322 /* The following elements will be encrypted with AES-SIV */
2323
2324 plain = wpabuf_alloc(1000);
2325 if (!plain)
2326 return -1;
2327
2328 /* TODO: FILS Public Key */
2329
2330 /* FILS Key Confirmation */
2331 wpabuf_put_u8(plain, WLAN_EID_EXTENSION); /* Element ID */
2332 wpabuf_put_u8(plain, 1 + sm->fils_key_auth_len); /* Length */
2333 /* Element ID Extension */
2334 wpabuf_put_u8(plain, WLAN_EID_EXT_FILS_KEY_CONFIRM);
2335 wpabuf_put_data(plain, sm->fils_key_auth_ap, sm->fils_key_auth_len);
2336
2337 /* TODO: FILS HLP Container */
2338
2339 /* TODO: FILS IP Address Assignment */
2340
2341 /* Key Delivery */
2342 gsm = sm->group;
2343 wpabuf_put_u8(plain, WLAN_EID_EXTENSION); /* Element ID */
2344 len = wpabuf_put(plain, 1);
2345 wpabuf_put_u8(plain, WLAN_EID_EXT_KEY_DELIVERY);
2346 wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN,
2347 wpabuf_put(plain, WPA_KEY_RSC_LEN));
2348 /* GTK KDE */
2349 gtk = gsm->GTK[gsm->GN - 1];
2350 gtk_len = gsm->GTK_len;
2351 if (sm->wpa_auth->conf.disable_gtk) {
2352 /*
2353 * Provide unique random GTK to each STA to prevent use
2354 * of GTK in the BSS.
2355 */
2356 if (random_get_bytes(dummy_gtk, gtk_len) < 0) {
2357 wpabuf_free(plain);
2358 return -1;
2359 }
2360 gtk = dummy_gtk;
2361 }
2362 hdr[0] = gsm->GN & 0x03;
2363 hdr[1] = 0;
2364 tmp = wpabuf_put(plain, 0);
2365 tmp2 = wpa_add_kde(tmp, RSN_KEY_DATA_GROUPKEY, hdr, 2,
2366 gtk, gtk_len);
2367 wpabuf_put(plain, tmp2 - tmp);
2368
2369 /* IGTK KDE */
2370 tmp = wpabuf_put(plain, 0);
2371 tmp2 = ieee80211w_kde_add(sm, tmp);
2372 wpabuf_put(plain, tmp2 - tmp);
2373
2374 *len = (u8 *) wpabuf_put(plain, 0) - len - 1;
2375
2376 if (pos + wpabuf_len(plain) + AES_BLOCK_SIZE > end) {
2377 wpa_printf(MSG_DEBUG,
2378 "FILS: Not enough room for FILS elements");
2379 wpabuf_free(plain);
2380 return -1;
2381 }
2382
2383 wpa_hexdump_buf_key(MSG_DEBUG, "FILS: Association Response plaintext",
2384 plain);
2385
2386 if (aes_siv_encrypt(sm->PTK.kek, sm->PTK.kek_len,
2387 wpabuf_head(plain), wpabuf_len(plain),
2388 5, aad, aad_len, pos) < 0) {
2389 wpabuf_free(plain);
2390 return -1;
2391 }
2392
2393 wpa_hexdump(MSG_DEBUG,
2394 "FILS: Encrypted Association Response elements",
2395 pos, AES_BLOCK_SIZE + wpabuf_len(plain));
2396 current_len += wpabuf_len(plain) + AES_BLOCK_SIZE;
2397 wpabuf_free(plain);
2398
2399 sm->fils_completed = 1;
2400
2401 return current_len;
2402}
2403
2404
2405int fils_set_tk(struct wpa_state_machine *sm)
2406{
2407 enum wpa_alg alg;
2408 int klen;
2409
2410 if (!sm || !sm->PTK_valid)
2411 return -1;
2412
2413 alg = wpa_cipher_to_alg(sm->pairwise);
2414 klen = wpa_cipher_key_len(sm->pairwise);
2415
2416 wpa_printf(MSG_DEBUG, "FILS: Configure TK to the driver");
2417 if (wpa_auth_set_key(sm->wpa_auth, 0, alg, sm->addr, 0,
2418 sm->PTK.tk, klen)) {
2419 wpa_printf(MSG_DEBUG, "FILS: Failed to set TK to the driver");
2420 return -1;
2421 }
2422
2423 return 0;
2424}
2425
2426#endif /* CONFIG_FILS */
2427
2428
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002429SM_STATE(WPA_PTK, PTKCALCNEGOTIATING)
2430{
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002431 struct wpa_authenticator *wpa_auth = sm->wpa_auth;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002432 struct wpa_ptk PTK;
Dmitry Shmidtdda10c22015-03-24 16:05:01 -07002433 int ok = 0, psk_found = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002434 const u8 *pmk = NULL;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002435 unsigned int pmk_len;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002436 int ft;
2437 const u8 *eapol_key_ie, *key_data, *mic;
2438 u16 key_data_length;
2439 size_t mic_len, eapol_key_ie_len;
2440 struct ieee802_1x_hdr *hdr;
2441 struct wpa_eapol_key *key;
2442 struct wpa_eapol_ie_parse kde;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002443
2444 SM_ENTRY_MA(WPA_PTK, PTKCALCNEGOTIATING, wpa_ptk);
2445 sm->EAPOLKeyReceived = FALSE;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002446 sm->update_snonce = FALSE;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002447 os_memset(&PTK, 0, sizeof(PTK));
2448
2449 mic_len = wpa_mic_len(sm->wpa_key_mgmt);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002450
2451 /* WPA with IEEE 802.1X: use the derived PMK from EAP
2452 * WPA-PSK: iterate through possible PSKs and select the one matching
2453 * the packet */
2454 for (;;) {
2455 if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt)) {
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07002456 pmk = wpa_auth_get_psk(sm->wpa_auth, sm->addr,
2457 sm->p2p_dev_addr, pmk);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002458 if (pmk == NULL)
2459 break;
Dmitry Shmidtdda10c22015-03-24 16:05:01 -07002460 psk_found = 1;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002461 pmk_len = PMK_LEN;
2462 } else {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002463 pmk = sm->PMK;
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002464 pmk_len = sm->pmk_len;
2465 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002466
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002467 wpa_derive_ptk(sm, sm->SNonce, pmk, pmk_len, &PTK);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002468
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002469 if (mic_len &&
2470 wpa_verify_key_mic(sm->wpa_key_mgmt, &PTK,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002471 sm->last_rx_eapol_key,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002472 sm->last_rx_eapol_key_len) == 0) {
2473 ok = 1;
2474 break;
2475 }
2476
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002477#ifdef CONFIG_FILS
2478 if (!mic_len &&
2479 wpa_aead_decrypt(sm, &PTK, sm->last_rx_eapol_key,
2480 sm->last_rx_eapol_key_len, NULL) == 0) {
2481 ok = 1;
2482 break;
2483 }
2484#endif /* CONFIG_FILS */
2485
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002486 if (!wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt))
2487 break;
2488 }
2489
2490 if (!ok) {
2491 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
2492 "invalid MIC in msg 2/4 of 4-Way Handshake");
Dmitry Shmidtdda10c22015-03-24 16:05:01 -07002493 if (psk_found)
2494 wpa_auth_psk_failure_report(sm->wpa_auth, sm->addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002495 return;
2496 }
2497
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002498 /*
2499 * Note: last_rx_eapol_key length fields have already been validated in
2500 * wpa_receive().
2501 */
2502 hdr = (struct ieee802_1x_hdr *) sm->last_rx_eapol_key;
2503 key = (struct wpa_eapol_key *) (hdr + 1);
2504 mic = (u8 *) (key + 1);
2505 key_data = mic + mic_len + 2;
2506 key_data_length = WPA_GET_BE16(mic + mic_len);
2507 if (key_data_length > sm->last_rx_eapol_key_len - sizeof(*hdr) -
2508 sizeof(*key) - mic_len - 2)
2509 return;
2510
2511 if (wpa_parse_kde_ies(key_data, key_data_length, &kde) < 0) {
2512 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_INFO,
2513 "received EAPOL-Key msg 2/4 with invalid Key Data contents");
2514 return;
2515 }
2516 if (kde.rsn_ie) {
2517 eapol_key_ie = kde.rsn_ie;
2518 eapol_key_ie_len = kde.rsn_ie_len;
2519 } else if (kde.osen) {
2520 eapol_key_ie = kde.osen;
2521 eapol_key_ie_len = kde.osen_len;
2522 } else {
2523 eapol_key_ie = kde.wpa_ie;
2524 eapol_key_ie_len = kde.wpa_ie_len;
2525 }
2526 ft = sm->wpa == WPA_VERSION_WPA2 && wpa_key_mgmt_ft(sm->wpa_key_mgmt);
2527 if (sm->wpa_ie == NULL ||
2528 wpa_compare_rsn_ie(ft, sm->wpa_ie, sm->wpa_ie_len,
2529 eapol_key_ie, eapol_key_ie_len)) {
2530 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
2531 "WPA IE from (Re)AssocReq did not match with msg 2/4");
2532 if (sm->wpa_ie) {
2533 wpa_hexdump(MSG_DEBUG, "WPA IE in AssocReq",
2534 sm->wpa_ie, sm->wpa_ie_len);
2535 }
2536 wpa_hexdump(MSG_DEBUG, "WPA IE in msg 2/4",
2537 eapol_key_ie, eapol_key_ie_len);
2538 /* MLME-DEAUTHENTICATE.request */
2539 wpa_sta_disconnect(wpa_auth, sm->addr);
2540 return;
2541 }
2542#ifdef CONFIG_IEEE80211R_AP
2543 if (ft && ft_check_msg_2_of_4(wpa_auth, sm, &kde) < 0) {
2544 wpa_sta_disconnect(wpa_auth, sm->addr);
2545 return;
2546 }
2547#endif /* CONFIG_IEEE80211R_AP */
2548#ifdef CONFIG_P2P
2549 if (kde.ip_addr_req && kde.ip_addr_req[0] &&
2550 wpa_auth->ip_pool && WPA_GET_BE32(sm->ip_addr) == 0) {
2551 int idx;
2552 wpa_printf(MSG_DEBUG,
2553 "P2P: IP address requested in EAPOL-Key exchange");
2554 idx = bitfield_get_first_zero(wpa_auth->ip_pool);
2555 if (idx >= 0) {
2556 u32 start = WPA_GET_BE32(wpa_auth->conf.ip_addr_start);
2557 bitfield_set(wpa_auth->ip_pool, idx);
2558 WPA_PUT_BE32(sm->ip_addr, start + idx);
2559 wpa_printf(MSG_DEBUG,
2560 "P2P: Assigned IP address %u.%u.%u.%u to "
2561 MACSTR, sm->ip_addr[0], sm->ip_addr[1],
2562 sm->ip_addr[2], sm->ip_addr[3],
2563 MAC2STR(sm->addr));
2564 }
2565 }
2566#endif /* CONFIG_P2P */
2567
2568#ifdef CONFIG_IEEE80211R_AP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002569 if (sm->wpa == WPA_VERSION_WPA2 && wpa_key_mgmt_ft(sm->wpa_key_mgmt)) {
2570 /*
2571 * Verify that PMKR1Name from EAPOL-Key message 2/4 matches
2572 * with the value we derived.
2573 */
Dmitry Shmidtc2817022014-07-02 10:32:10 -07002574 if (os_memcmp_const(sm->sup_pmk_r1_name, sm->pmk_r1_name,
2575 WPA_PMK_NAME_LEN) != 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002576 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
2577 "PMKR1Name mismatch in FT 4-way "
2578 "handshake");
2579 wpa_hexdump(MSG_DEBUG, "FT: PMKR1Name from "
2580 "Supplicant",
2581 sm->sup_pmk_r1_name, WPA_PMK_NAME_LEN);
2582 wpa_hexdump(MSG_DEBUG, "FT: Derived PMKR1Name",
2583 sm->pmk_r1_name, WPA_PMK_NAME_LEN);
2584 return;
2585 }
2586 }
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002587#endif /* CONFIG_IEEE80211R_AP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002588
2589 sm->pending_1_of_4_timeout = 0;
2590 eloop_cancel_timeout(wpa_send_eapol_timeout, sm->wpa_auth, sm);
2591
2592 if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt)) {
2593 /* PSK may have changed from the previous choice, so update
2594 * state machine data based on whatever PSK was selected here.
2595 */
2596 os_memcpy(sm->PMK, pmk, PMK_LEN);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08002597 sm->pmk_len = PMK_LEN;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002598 }
2599
2600 sm->MICVerified = TRUE;
2601
2602 os_memcpy(&sm->PTK, &PTK, sizeof(PTK));
2603 sm->PTK_valid = TRUE;
2604}
2605
2606
2607SM_STATE(WPA_PTK, PTKCALCNEGOTIATING2)
2608{
2609 SM_ENTRY_MA(WPA_PTK, PTKCALCNEGOTIATING2, wpa_ptk);
2610 sm->TimeoutCtr = 0;
2611}
2612
2613
2614#ifdef CONFIG_IEEE80211W
2615
2616static int ieee80211w_kde_len(struct wpa_state_machine *sm)
2617{
2618 if (sm->mgmt_frame_prot) {
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002619 size_t len;
2620 len = wpa_cipher_key_len(sm->wpa_auth->conf.group_mgmt_cipher);
2621 return 2 + RSN_SELECTOR_LEN + WPA_IGTK_KDE_PREFIX_LEN + len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002622 }
2623
2624 return 0;
2625}
2626
2627
2628static u8 * ieee80211w_kde_add(struct wpa_state_machine *sm, u8 *pos)
2629{
2630 struct wpa_igtk_kde igtk;
2631 struct wpa_group *gsm = sm->group;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002632 u8 rsc[WPA_KEY_RSC_LEN];
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002633 size_t len = wpa_cipher_key_len(sm->wpa_auth->conf.group_mgmt_cipher);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002634
2635 if (!sm->mgmt_frame_prot)
2636 return pos;
2637
2638 igtk.keyid[0] = gsm->GN_igtk;
2639 igtk.keyid[1] = 0;
2640 if (gsm->wpa_group_state != WPA_GROUP_SETKEYSDONE ||
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002641 wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN_igtk, rsc) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002642 os_memset(igtk.pn, 0, sizeof(igtk.pn));
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002643 else
2644 os_memcpy(igtk.pn, rsc, sizeof(igtk.pn));
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002645 os_memcpy(igtk.igtk, gsm->IGTK[gsm->GN_igtk - 4], len);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002646 if (sm->wpa_auth->conf.disable_gtk) {
2647 /*
2648 * Provide unique random IGTK to each STA to prevent use of
2649 * IGTK in the BSS.
2650 */
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002651 if (random_get_bytes(igtk.igtk, len) < 0)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002652 return pos;
2653 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002654 pos = wpa_add_kde(pos, RSN_KEY_DATA_IGTK,
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002655 (const u8 *) &igtk, WPA_IGTK_KDE_PREFIX_LEN + len,
2656 NULL, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002657
2658 return pos;
2659}
2660
2661#else /* CONFIG_IEEE80211W */
2662
2663static int ieee80211w_kde_len(struct wpa_state_machine *sm)
2664{
2665 return 0;
2666}
2667
2668
2669static u8 * ieee80211w_kde_add(struct wpa_state_machine *sm, u8 *pos)
2670{
2671 return pos;
2672}
2673
2674#endif /* CONFIG_IEEE80211W */
2675
2676
2677SM_STATE(WPA_PTK, PTKINITNEGOTIATING)
2678{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002679 u8 rsc[WPA_KEY_RSC_LEN], *_rsc, *gtk, *kde, *pos, dummy_gtk[32];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002680 size_t gtk_len, kde_len;
2681 struct wpa_group *gsm = sm->group;
2682 u8 *wpa_ie;
2683 int wpa_ie_len, secure, keyidx, encr = 0;
2684
2685 SM_ENTRY_MA(WPA_PTK, PTKINITNEGOTIATING, wpa_ptk);
2686 sm->TimeoutEvt = FALSE;
2687
2688 sm->TimeoutCtr++;
2689 if (sm->TimeoutCtr > (int) dot11RSNAConfigPairwiseUpdateCount) {
2690 /* No point in sending the EAPOL-Key - we will disconnect
2691 * immediately following this. */
2692 return;
2693 }
2694
2695 /* Send EAPOL(1, 1, 1, Pair, P, RSC, ANonce, MIC(PTK), RSNIE, [MDIE],
2696 GTK[GN], IGTK, [FTIE], [TIE * 2])
2697 */
2698 os_memset(rsc, 0, WPA_KEY_RSC_LEN);
2699 wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN, rsc);
2700 /* If FT is used, wpa_auth->wpa_ie includes both RSNIE and MDIE */
2701 wpa_ie = sm->wpa_auth->wpa_ie;
2702 wpa_ie_len = sm->wpa_auth->wpa_ie_len;
2703 if (sm->wpa == WPA_VERSION_WPA &&
2704 (sm->wpa_auth->conf.wpa & WPA_PROTO_RSN) &&
2705 wpa_ie_len > wpa_ie[1] + 2 && wpa_ie[0] == WLAN_EID_RSN) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002706 /* WPA-only STA, remove RSN IE and possible MDIE */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002707 wpa_ie = wpa_ie + wpa_ie[1] + 2;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002708 if (wpa_ie[0] == WLAN_EID_MOBILITY_DOMAIN)
2709 wpa_ie = wpa_ie + wpa_ie[1] + 2;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002710 wpa_ie_len = wpa_ie[1] + 2;
2711 }
2712 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
2713 "sending 3/4 msg of 4-Way Handshake");
2714 if (sm->wpa == WPA_VERSION_WPA2) {
2715 /* WPA2 send GTK in the 4-way handshake */
2716 secure = 1;
2717 gtk = gsm->GTK[gsm->GN - 1];
2718 gtk_len = gsm->GTK_len;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002719 if (sm->wpa_auth->conf.disable_gtk) {
2720 /*
2721 * Provide unique random GTK to each STA to prevent use
2722 * of GTK in the BSS.
2723 */
2724 if (random_get_bytes(dummy_gtk, gtk_len) < 0)
2725 return;
2726 gtk = dummy_gtk;
2727 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002728 keyidx = gsm->GN;
2729 _rsc = rsc;
2730 encr = 1;
2731 } else {
2732 /* WPA does not include GTK in msg 3/4 */
2733 secure = 0;
2734 gtk = NULL;
2735 gtk_len = 0;
2736 keyidx = 0;
2737 _rsc = NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002738 if (sm->rx_eapol_key_secure) {
2739 /*
2740 * It looks like Windows 7 supplicant tries to use
2741 * Secure bit in msg 2/4 after having reported Michael
2742 * MIC failure and it then rejects the 4-way handshake
2743 * if msg 3/4 does not set Secure bit. Work around this
2744 * by setting the Secure bit here even in the case of
2745 * WPA if the supplicant used it first.
2746 */
2747 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
2748 "STA used Secure bit in WPA msg 2/4 - "
2749 "set Secure for 3/4 as workaround");
2750 secure = 1;
2751 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002752 }
2753
2754 kde_len = wpa_ie_len + ieee80211w_kde_len(sm);
2755 if (gtk)
2756 kde_len += 2 + RSN_SELECTOR_LEN + 2 + gtk_len;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002757#ifdef CONFIG_IEEE80211R_AP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002758 if (wpa_key_mgmt_ft(sm->wpa_key_mgmt)) {
2759 kde_len += 2 + PMKID_LEN; /* PMKR1Name into RSN IE */
2760 kde_len += 300; /* FTIE + 2 * TIE */
2761 }
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002762#endif /* CONFIG_IEEE80211R_AP */
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002763#ifdef CONFIG_P2P
2764 if (WPA_GET_BE32(sm->ip_addr) > 0)
2765 kde_len += 2 + RSN_SELECTOR_LEN + 3 * 4;
2766#endif /* CONFIG_P2P */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002767 kde = os_malloc(kde_len);
2768 if (kde == NULL)
2769 return;
2770
2771 pos = kde;
2772 os_memcpy(pos, wpa_ie, wpa_ie_len);
2773 pos += wpa_ie_len;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002774#ifdef CONFIG_IEEE80211R_AP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002775 if (wpa_key_mgmt_ft(sm->wpa_key_mgmt)) {
Dmitry Shmidt55840ad2015-12-14 12:45:46 -08002776 int res;
2777 size_t elen;
2778
2779 elen = pos - kde;
2780 res = wpa_insert_pmkid(kde, &elen, sm->pmk_r1_name);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002781 if (res < 0) {
2782 wpa_printf(MSG_ERROR, "FT: Failed to insert "
2783 "PMKR1Name into RSN IE in EAPOL-Key data");
2784 os_free(kde);
2785 return;
2786 }
Dmitry Shmidt55840ad2015-12-14 12:45:46 -08002787 pos -= wpa_ie_len;
2788 pos += elen;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002789 }
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002790#endif /* CONFIG_IEEE80211R_AP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002791 if (gtk) {
2792 u8 hdr[2];
2793 hdr[0] = keyidx & 0x03;
2794 hdr[1] = 0;
2795 pos = wpa_add_kde(pos, RSN_KEY_DATA_GROUPKEY, hdr, 2,
2796 gtk, gtk_len);
2797 }
2798 pos = ieee80211w_kde_add(sm, pos);
2799
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002800#ifdef CONFIG_IEEE80211R_AP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002801 if (wpa_key_mgmt_ft(sm->wpa_key_mgmt)) {
2802 int res;
2803 struct wpa_auth_config *conf;
2804
2805 conf = &sm->wpa_auth->conf;
Dmitry Shmidt55840ad2015-12-14 12:45:46 -08002806 if (sm->assoc_resp_ftie &&
2807 kde + kde_len - pos >= 2 + sm->assoc_resp_ftie[1]) {
2808 os_memcpy(pos, sm->assoc_resp_ftie,
2809 2 + sm->assoc_resp_ftie[1]);
2810 res = 2 + sm->assoc_resp_ftie[1];
2811 } else {
2812 res = wpa_write_ftie(conf, conf->r0_key_holder,
2813 conf->r0_key_holder_len,
2814 NULL, NULL, pos,
2815 kde + kde_len - pos,
2816 NULL, 0);
2817 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002818 if (res < 0) {
2819 wpa_printf(MSG_ERROR, "FT: Failed to insert FTIE "
2820 "into EAPOL-Key Key Data");
2821 os_free(kde);
2822 return;
2823 }
2824 pos += res;
2825
2826 /* TIE[ReassociationDeadline] (TU) */
2827 *pos++ = WLAN_EID_TIMEOUT_INTERVAL;
2828 *pos++ = 5;
2829 *pos++ = WLAN_TIMEOUT_REASSOC_DEADLINE;
2830 WPA_PUT_LE32(pos, conf->reassociation_deadline);
2831 pos += 4;
2832
2833 /* TIE[KeyLifetime] (seconds) */
2834 *pos++ = WLAN_EID_TIMEOUT_INTERVAL;
2835 *pos++ = 5;
2836 *pos++ = WLAN_TIMEOUT_KEY_LIFETIME;
2837 WPA_PUT_LE32(pos, conf->r0_key_lifetime * 60);
2838 pos += 4;
2839 }
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002840#endif /* CONFIG_IEEE80211R_AP */
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002841#ifdef CONFIG_P2P
2842 if (WPA_GET_BE32(sm->ip_addr) > 0) {
2843 u8 addr[3 * 4];
2844 os_memcpy(addr, sm->ip_addr, 4);
2845 os_memcpy(addr + 4, sm->wpa_auth->conf.ip_addr_mask, 4);
2846 os_memcpy(addr + 8, sm->wpa_auth->conf.ip_addr_go, 4);
2847 pos = wpa_add_kde(pos, WFA_KEY_DATA_IP_ADDR_ALLOC,
2848 addr, sizeof(addr), NULL, 0);
2849 }
2850#endif /* CONFIG_P2P */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002851
2852 wpa_send_eapol(sm->wpa_auth, sm,
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002853 (secure ? WPA_KEY_INFO_SECURE : 0) |
2854 (wpa_mic_len(sm->wpa_key_mgmt) ? WPA_KEY_INFO_MIC : 0) |
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002855 WPA_KEY_INFO_ACK | WPA_KEY_INFO_INSTALL |
2856 WPA_KEY_INFO_KEY_TYPE,
2857 _rsc, sm->ANonce, kde, pos - kde, keyidx, encr);
2858 os_free(kde);
2859}
2860
2861
2862SM_STATE(WPA_PTK, PTKINITDONE)
2863{
2864 SM_ENTRY_MA(WPA_PTK, PTKINITDONE, wpa_ptk);
2865 sm->EAPOLKeyReceived = FALSE;
2866 if (sm->Pair) {
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07002867 enum wpa_alg alg = wpa_cipher_to_alg(sm->pairwise);
2868 int klen = wpa_cipher_key_len(sm->pairwise);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002869 if (wpa_auth_set_key(sm->wpa_auth, 0, alg, sm->addr, 0,
Dmitry Shmidt807291d2015-01-27 13:40:23 -08002870 sm->PTK.tk, klen)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002871 wpa_sta_disconnect(sm->wpa_auth, sm->addr);
2872 return;
2873 }
2874 /* FIX: MLME-SetProtection.Request(TA, Tx_Rx) */
2875 sm->pairwise_set = TRUE;
2876
2877 if (sm->wpa_auth->conf.wpa_ptk_rekey) {
2878 eloop_cancel_timeout(wpa_rekey_ptk, sm->wpa_auth, sm);
2879 eloop_register_timeout(sm->wpa_auth->conf.
2880 wpa_ptk_rekey, 0, wpa_rekey_ptk,
2881 sm->wpa_auth, sm);
2882 }
2883
2884 if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt)) {
2885 wpa_auth_set_eapol(sm->wpa_auth, sm->addr,
2886 WPA_EAPOL_authorized, 1);
2887 }
2888 }
2889
2890 if (0 /* IBSS == TRUE */) {
2891 sm->keycount++;
2892 if (sm->keycount == 2) {
2893 wpa_auth_set_eapol(sm->wpa_auth, sm->addr,
2894 WPA_EAPOL_portValid, 1);
2895 }
2896 } else {
2897 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portValid,
2898 1);
2899 }
2900 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_keyAvailable, 0);
2901 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_keyDone, 1);
2902 if (sm->wpa == WPA_VERSION_WPA)
2903 sm->PInitAKeys = TRUE;
2904 else
2905 sm->has_GTK = TRUE;
2906 wpa_auth_vlogger(sm->wpa_auth, sm->addr, LOGGER_INFO,
2907 "pairwise key handshake completed (%s)",
2908 sm->wpa == WPA_VERSION_WPA ? "WPA" : "RSN");
2909
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002910#ifdef CONFIG_IEEE80211R_AP
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002911 wpa_ft_push_pmk_r1(sm->wpa_auth, sm->addr);
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08002912#endif /* CONFIG_IEEE80211R_AP */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002913}
2914
2915
2916SM_STEP(WPA_PTK)
2917{
2918 struct wpa_authenticator *wpa_auth = sm->wpa_auth;
2919
2920 if (sm->Init)
2921 SM_ENTER(WPA_PTK, INITIALIZE);
2922 else if (sm->Disconnect
2923 /* || FIX: dot11RSNAConfigSALifetime timeout */) {
2924 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG,
2925 "WPA_PTK: sm->Disconnect");
2926 SM_ENTER(WPA_PTK, DISCONNECT);
2927 }
2928 else if (sm->DeauthenticationRequest)
2929 SM_ENTER(WPA_PTK, DISCONNECTED);
2930 else if (sm->AuthenticationRequest)
2931 SM_ENTER(WPA_PTK, AUTHENTICATION);
2932 else if (sm->ReAuthenticationRequest)
2933 SM_ENTER(WPA_PTK, AUTHENTICATION2);
2934 else if (sm->PTKRequest)
2935 SM_ENTER(WPA_PTK, PTKSTART);
2936 else switch (sm->wpa_ptk_state) {
2937 case WPA_PTK_INITIALIZE:
2938 break;
2939 case WPA_PTK_DISCONNECT:
2940 SM_ENTER(WPA_PTK, DISCONNECTED);
2941 break;
2942 case WPA_PTK_DISCONNECTED:
2943 SM_ENTER(WPA_PTK, INITIALIZE);
2944 break;
2945 case WPA_PTK_AUTHENTICATION:
2946 SM_ENTER(WPA_PTK, AUTHENTICATION2);
2947 break;
2948 case WPA_PTK_AUTHENTICATION2:
2949 if (wpa_key_mgmt_wpa_ieee8021x(sm->wpa_key_mgmt) &&
2950 wpa_auth_get_eapol(sm->wpa_auth, sm->addr,
2951 WPA_EAPOL_keyRun) > 0)
2952 SM_ENTER(WPA_PTK, INITPMK);
2953 else if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt)
2954 /* FIX: && 802.1X::keyRun */)
2955 SM_ENTER(WPA_PTK, INITPSK);
2956 break;
2957 case WPA_PTK_INITPMK:
2958 if (wpa_auth_get_eapol(sm->wpa_auth, sm->addr,
2959 WPA_EAPOL_keyAvailable) > 0)
2960 SM_ENTER(WPA_PTK, PTKSTART);
2961 else {
2962 wpa_auth->dot11RSNA4WayHandshakeFailures++;
2963 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_INFO,
2964 "INITPMK - keyAvailable = false");
2965 SM_ENTER(WPA_PTK, DISCONNECT);
2966 }
2967 break;
2968 case WPA_PTK_INITPSK:
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07002969 if (wpa_auth_get_psk(sm->wpa_auth, sm->addr, sm->p2p_dev_addr,
2970 NULL))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002971 SM_ENTER(WPA_PTK, PTKSTART);
2972 else {
2973 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_INFO,
2974 "no PSK configured for the STA");
2975 wpa_auth->dot11RSNA4WayHandshakeFailures++;
2976 SM_ENTER(WPA_PTK, DISCONNECT);
2977 }
2978 break;
2979 case WPA_PTK_PTKSTART:
2980 if (sm->EAPOLKeyReceived && !sm->EAPOLKeyRequest &&
2981 sm->EAPOLKeyPairwise)
2982 SM_ENTER(WPA_PTK, PTKCALCNEGOTIATING);
2983 else if (sm->TimeoutCtr >
2984 (int) dot11RSNAConfigPairwiseUpdateCount) {
2985 wpa_auth->dot11RSNA4WayHandshakeFailures++;
2986 wpa_auth_vlogger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
2987 "PTKSTART: Retry limit %d reached",
2988 dot11RSNAConfigPairwiseUpdateCount);
2989 SM_ENTER(WPA_PTK, DISCONNECT);
2990 } else if (sm->TimeoutEvt)
2991 SM_ENTER(WPA_PTK, PTKSTART);
2992 break;
2993 case WPA_PTK_PTKCALCNEGOTIATING:
2994 if (sm->MICVerified)
2995 SM_ENTER(WPA_PTK, PTKCALCNEGOTIATING2);
2996 else if (sm->EAPOLKeyReceived && !sm->EAPOLKeyRequest &&
2997 sm->EAPOLKeyPairwise)
2998 SM_ENTER(WPA_PTK, PTKCALCNEGOTIATING);
2999 else if (sm->TimeoutEvt)
3000 SM_ENTER(WPA_PTK, PTKSTART);
3001 break;
3002 case WPA_PTK_PTKCALCNEGOTIATING2:
3003 SM_ENTER(WPA_PTK, PTKINITNEGOTIATING);
3004 break;
3005 case WPA_PTK_PTKINITNEGOTIATING:
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003006 if (sm->update_snonce)
3007 SM_ENTER(WPA_PTK, PTKCALCNEGOTIATING);
3008 else if (sm->EAPOLKeyReceived && !sm->EAPOLKeyRequest &&
3009 sm->EAPOLKeyPairwise && sm->MICVerified)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003010 SM_ENTER(WPA_PTK, PTKINITDONE);
3011 else if (sm->TimeoutCtr >
3012 (int) dot11RSNAConfigPairwiseUpdateCount) {
3013 wpa_auth->dot11RSNA4WayHandshakeFailures++;
3014 wpa_auth_vlogger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
3015 "PTKINITNEGOTIATING: Retry limit %d "
3016 "reached",
3017 dot11RSNAConfigPairwiseUpdateCount);
3018 SM_ENTER(WPA_PTK, DISCONNECT);
3019 } else if (sm->TimeoutEvt)
3020 SM_ENTER(WPA_PTK, PTKINITNEGOTIATING);
3021 break;
3022 case WPA_PTK_PTKINITDONE:
3023 break;
3024 }
3025}
3026
3027
3028SM_STATE(WPA_PTK_GROUP, IDLE)
3029{
3030 SM_ENTRY_MA(WPA_PTK_GROUP, IDLE, wpa_ptk_group);
3031 if (sm->Init) {
3032 /* Init flag is not cleared here, so avoid busy
3033 * loop by claiming nothing changed. */
3034 sm->changed = FALSE;
3035 }
3036 sm->GTimeoutCtr = 0;
3037}
3038
3039
3040SM_STATE(WPA_PTK_GROUP, REKEYNEGOTIATING)
3041{
3042 u8 rsc[WPA_KEY_RSC_LEN];
3043 struct wpa_group *gsm = sm->group;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003044 const u8 *kde;
3045 u8 *kde_buf = NULL, *pos, hdr[2];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003046 size_t kde_len;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003047 u8 *gtk, dummy_gtk[32];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003048
3049 SM_ENTRY_MA(WPA_PTK_GROUP, REKEYNEGOTIATING, wpa_ptk_group);
3050
3051 sm->GTimeoutCtr++;
3052 if (sm->GTimeoutCtr > (int) dot11RSNAConfigGroupUpdateCount) {
3053 /* No point in sending the EAPOL-Key - we will disconnect
3054 * immediately following this. */
3055 return;
3056 }
3057
3058 if (sm->wpa == WPA_VERSION_WPA)
3059 sm->PInitAKeys = FALSE;
3060 sm->TimeoutEvt = FALSE;
3061 /* Send EAPOL(1, 1, 1, !Pair, G, RSC, GNonce, MIC(PTK), GTK[GN]) */
3062 os_memset(rsc, 0, WPA_KEY_RSC_LEN);
3063 if (gsm->wpa_group_state == WPA_GROUP_SETKEYSDONE)
3064 wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN, rsc);
3065 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
3066 "sending 1/2 msg of Group Key Handshake");
3067
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003068 gtk = gsm->GTK[gsm->GN - 1];
3069 if (sm->wpa_auth->conf.disable_gtk) {
3070 /*
3071 * Provide unique random GTK to each STA to prevent use
3072 * of GTK in the BSS.
3073 */
3074 if (random_get_bytes(dummy_gtk, gsm->GTK_len) < 0)
3075 return;
3076 gtk = dummy_gtk;
3077 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003078 if (sm->wpa == WPA_VERSION_WPA2) {
3079 kde_len = 2 + RSN_SELECTOR_LEN + 2 + gsm->GTK_len +
3080 ieee80211w_kde_len(sm);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003081 kde_buf = os_malloc(kde_len);
3082 if (kde_buf == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003083 return;
3084
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003085 kde = pos = kde_buf;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003086 hdr[0] = gsm->GN & 0x03;
3087 hdr[1] = 0;
3088 pos = wpa_add_kde(pos, RSN_KEY_DATA_GROUPKEY, hdr, 2,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003089 gtk, gsm->GTK_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003090 pos = ieee80211w_kde_add(sm, pos);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003091 kde_len = pos - kde;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003092 } else {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003093 kde = gtk;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003094 kde_len = gsm->GTK_len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003095 }
3096
3097 wpa_send_eapol(sm->wpa_auth, sm,
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003098 WPA_KEY_INFO_SECURE |
3099 (wpa_mic_len(sm->wpa_key_mgmt) ? WPA_KEY_INFO_MIC : 0) |
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003100 WPA_KEY_INFO_ACK |
3101 (!sm->Pair ? WPA_KEY_INFO_INSTALL : 0),
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003102 rsc, gsm->GNonce, kde, kde_len, gsm->GN, 1);
3103
3104 os_free(kde_buf);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003105}
3106
3107
3108SM_STATE(WPA_PTK_GROUP, REKEYESTABLISHED)
3109{
3110 SM_ENTRY_MA(WPA_PTK_GROUP, REKEYESTABLISHED, wpa_ptk_group);
3111 sm->EAPOLKeyReceived = FALSE;
3112 if (sm->GUpdateStationKeys)
3113 sm->group->GKeyDoneStations--;
3114 sm->GUpdateStationKeys = FALSE;
3115 sm->GTimeoutCtr = 0;
3116 /* FIX: MLME.SetProtection.Request(TA, Tx_Rx) */
3117 wpa_auth_vlogger(sm->wpa_auth, sm->addr, LOGGER_INFO,
3118 "group key handshake completed (%s)",
3119 sm->wpa == WPA_VERSION_WPA ? "WPA" : "RSN");
3120 sm->has_GTK = TRUE;
3121}
3122
3123
3124SM_STATE(WPA_PTK_GROUP, KEYERROR)
3125{
3126 SM_ENTRY_MA(WPA_PTK_GROUP, KEYERROR, wpa_ptk_group);
3127 if (sm->GUpdateStationKeys)
3128 sm->group->GKeyDoneStations--;
3129 sm->GUpdateStationKeys = FALSE;
3130 sm->Disconnect = TRUE;
3131}
3132
3133
3134SM_STEP(WPA_PTK_GROUP)
3135{
3136 if (sm->Init || sm->PtkGroupInit) {
3137 SM_ENTER(WPA_PTK_GROUP, IDLE);
3138 sm->PtkGroupInit = FALSE;
3139 } else switch (sm->wpa_ptk_group_state) {
3140 case WPA_PTK_GROUP_IDLE:
3141 if (sm->GUpdateStationKeys ||
3142 (sm->wpa == WPA_VERSION_WPA && sm->PInitAKeys))
3143 SM_ENTER(WPA_PTK_GROUP, REKEYNEGOTIATING);
3144 break;
3145 case WPA_PTK_GROUP_REKEYNEGOTIATING:
3146 if (sm->EAPOLKeyReceived && !sm->EAPOLKeyRequest &&
3147 !sm->EAPOLKeyPairwise && sm->MICVerified)
3148 SM_ENTER(WPA_PTK_GROUP, REKEYESTABLISHED);
3149 else if (sm->GTimeoutCtr >
3150 (int) dot11RSNAConfigGroupUpdateCount)
3151 SM_ENTER(WPA_PTK_GROUP, KEYERROR);
3152 else if (sm->TimeoutEvt)
3153 SM_ENTER(WPA_PTK_GROUP, REKEYNEGOTIATING);
3154 break;
3155 case WPA_PTK_GROUP_KEYERROR:
3156 SM_ENTER(WPA_PTK_GROUP, IDLE);
3157 break;
3158 case WPA_PTK_GROUP_REKEYESTABLISHED:
3159 SM_ENTER(WPA_PTK_GROUP, IDLE);
3160 break;
3161 }
3162}
3163
3164
3165static int wpa_gtk_update(struct wpa_authenticator *wpa_auth,
3166 struct wpa_group *group)
3167{
3168 int ret = 0;
3169
3170 os_memcpy(group->GNonce, group->Counter, WPA_NONCE_LEN);
3171 inc_byte_array(group->Counter, WPA_NONCE_LEN);
3172 if (wpa_gmk_to_gtk(group->GMK, "Group key expansion",
3173 wpa_auth->addr, group->GNonce,
3174 group->GTK[group->GN - 1], group->GTK_len) < 0)
3175 ret = -1;
3176 wpa_hexdump_key(MSG_DEBUG, "GTK",
3177 group->GTK[group->GN - 1], group->GTK_len);
3178
3179#ifdef CONFIG_IEEE80211W
3180 if (wpa_auth->conf.ieee80211w != NO_MGMT_FRAME_PROTECTION) {
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07003181 size_t len;
3182 len = wpa_cipher_key_len(wpa_auth->conf.group_mgmt_cipher);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003183 os_memcpy(group->GNonce, group->Counter, WPA_NONCE_LEN);
3184 inc_byte_array(group->Counter, WPA_NONCE_LEN);
3185 if (wpa_gmk_to_gtk(group->GMK, "IGTK key expansion",
3186 wpa_auth->addr, group->GNonce,
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07003187 group->IGTK[group->GN_igtk - 4], len) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003188 ret = -1;
3189 wpa_hexdump_key(MSG_DEBUG, "IGTK",
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07003190 group->IGTK[group->GN_igtk - 4], len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003191 }
3192#endif /* CONFIG_IEEE80211W */
3193
3194 return ret;
3195}
3196
3197
3198static void wpa_group_gtk_init(struct wpa_authenticator *wpa_auth,
3199 struct wpa_group *group)
3200{
3201 wpa_printf(MSG_DEBUG, "WPA: group state machine entering state "
3202 "GTK_INIT (VLAN-ID %d)", group->vlan_id);
3203 group->changed = FALSE; /* GInit is not cleared here; avoid loop */
3204 group->wpa_group_state = WPA_GROUP_GTK_INIT;
3205
3206 /* GTK[0..N] = 0 */
3207 os_memset(group->GTK, 0, sizeof(group->GTK));
3208 group->GN = 1;
3209 group->GM = 2;
3210#ifdef CONFIG_IEEE80211W
3211 group->GN_igtk = 4;
3212 group->GM_igtk = 5;
3213#endif /* CONFIG_IEEE80211W */
3214 /* GTK[GN] = CalcGTK() */
3215 wpa_gtk_update(wpa_auth, group);
3216}
3217
3218
3219static int wpa_group_update_sta(struct wpa_state_machine *sm, void *ctx)
3220{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003221 if (ctx != NULL && ctx != sm->group)
3222 return 0;
3223
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003224 if (sm->wpa_ptk_state != WPA_PTK_PTKINITDONE) {
3225 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
3226 "Not in PTKINITDONE; skip Group Key update");
3227 sm->GUpdateStationKeys = FALSE;
3228 return 0;
3229 }
3230 if (sm->GUpdateStationKeys) {
3231 /*
3232 * This should not really happen, so add a debug log entry.
3233 * Since we clear the GKeyDoneStations before the loop, the
3234 * station needs to be counted here anyway.
3235 */
3236 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
3237 "GUpdateStationKeys was already set when "
3238 "marking station for GTK rekeying");
3239 }
3240
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003241 /* Do not rekey GTK/IGTK when STA is in WNM-Sleep Mode */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003242 if (sm->is_wnmsleep)
3243 return 0;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003244
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003245 sm->group->GKeyDoneStations++;
3246 sm->GUpdateStationKeys = TRUE;
3247
3248 wpa_sm_step(sm);
3249 return 0;
3250}
3251
3252
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003253#ifdef CONFIG_WNM
3254/* update GTK when exiting WNM-Sleep Mode */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003255void wpa_wnmsleep_rekey_gtk(struct wpa_state_machine *sm)
3256{
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003257 if (sm == NULL || sm->is_wnmsleep)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003258 return;
3259
3260 wpa_group_update_sta(sm, NULL);
3261}
3262
3263
3264void wpa_set_wnmsleep(struct wpa_state_machine *sm, int flag)
3265{
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003266 if (sm)
3267 sm->is_wnmsleep = !!flag;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003268}
3269
3270
3271int wpa_wnmsleep_gtk_subelem(struct wpa_state_machine *sm, u8 *pos)
3272{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003273 struct wpa_group *gsm = sm->group;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003274 u8 *start = pos;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003275
3276 /*
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003277 * GTK subelement:
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003278 * Sub-elem ID[1] | Length[1] | Key Info[2] | Key Length[1] | RSC[8] |
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003279 * Key[5..32]
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003280 */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003281 *pos++ = WNM_SLEEP_SUBELEM_GTK;
3282 *pos++ = 11 + gsm->GTK_len;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003283 /* Key ID in B0-B1 of Key Info */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003284 WPA_PUT_LE16(pos, gsm->GN & 0x03);
3285 pos += 2;
3286 *pos++ = gsm->GTK_len;
3287 if (wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN, pos) != 0)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003288 return 0;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003289 pos += 8;
3290 os_memcpy(pos, gsm->GTK[gsm->GN - 1], gsm->GTK_len);
3291 pos += gsm->GTK_len;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003292
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003293 wpa_printf(MSG_DEBUG, "WNM: GTK Key ID %u in WNM-Sleep Mode exit",
3294 gsm->GN);
3295 wpa_hexdump_key(MSG_DEBUG, "WNM: GTK in WNM-Sleep Mode exit",
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003296 gsm->GTK[gsm->GN - 1], gsm->GTK_len);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003297
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003298 return pos - start;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003299}
3300
3301
3302#ifdef CONFIG_IEEE80211W
3303int wpa_wnmsleep_igtk_subelem(struct wpa_state_machine *sm, u8 *pos)
3304{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003305 struct wpa_group *gsm = sm->group;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003306 u8 *start = pos;
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07003307 size_t len = wpa_cipher_key_len(sm->wpa_auth->conf.group_mgmt_cipher);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003308
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003309 /*
3310 * IGTK subelement:
3311 * Sub-elem ID[1] | Length[1] | KeyID[2] | PN[6] | Key[16]
3312 */
3313 *pos++ = WNM_SLEEP_SUBELEM_IGTK;
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07003314 *pos++ = 2 + 6 + len;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003315 WPA_PUT_LE16(pos, gsm->GN_igtk);
3316 pos += 2;
3317 if (wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN_igtk, pos) != 0)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003318 return 0;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003319 pos += 6;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003320
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07003321 os_memcpy(pos, gsm->IGTK[gsm->GN_igtk - 4], len);
3322 pos += len;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003323
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003324 wpa_printf(MSG_DEBUG, "WNM: IGTK Key ID %u in WNM-Sleep Mode exit",
3325 gsm->GN_igtk);
3326 wpa_hexdump_key(MSG_DEBUG, "WNM: IGTK in WNM-Sleep Mode exit",
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07003327 gsm->IGTK[gsm->GN_igtk - 4], len);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003328
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003329 return pos - start;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003330}
3331#endif /* CONFIG_IEEE80211W */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08003332#endif /* CONFIG_WNM */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003333
3334
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003335static void wpa_group_setkeys(struct wpa_authenticator *wpa_auth,
3336 struct wpa_group *group)
3337{
3338 int tmp;
3339
3340 wpa_printf(MSG_DEBUG, "WPA: group state machine entering state "
3341 "SETKEYS (VLAN-ID %d)", group->vlan_id);
3342 group->changed = TRUE;
3343 group->wpa_group_state = WPA_GROUP_SETKEYS;
3344 group->GTKReKey = FALSE;
3345 tmp = group->GM;
3346 group->GM = group->GN;
3347 group->GN = tmp;
3348#ifdef CONFIG_IEEE80211W
3349 tmp = group->GM_igtk;
3350 group->GM_igtk = group->GN_igtk;
3351 group->GN_igtk = tmp;
3352#endif /* CONFIG_IEEE80211W */
3353 /* "GKeyDoneStations = GNoStations" is done in more robust way by
3354 * counting the STAs that are marked with GUpdateStationKeys instead of
3355 * including all STAs that could be in not-yet-completed state. */
3356 wpa_gtk_update(wpa_auth, group);
3357
3358 if (group->GKeyDoneStations) {
3359 wpa_printf(MSG_DEBUG, "wpa_group_setkeys: Unexpected "
3360 "GKeyDoneStations=%d when starting new GTK rekey",
3361 group->GKeyDoneStations);
3362 group->GKeyDoneStations = 0;
3363 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003364 wpa_auth_for_each_sta(wpa_auth, wpa_group_update_sta, group);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003365 wpa_printf(MSG_DEBUG, "wpa_group_setkeys: GKeyDoneStations=%d",
3366 group->GKeyDoneStations);
3367}
3368
3369
3370static int wpa_group_config_group_keys(struct wpa_authenticator *wpa_auth,
3371 struct wpa_group *group)
3372{
3373 int ret = 0;
3374
3375 if (wpa_auth_set_key(wpa_auth, group->vlan_id,
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07003376 wpa_cipher_to_alg(wpa_auth->conf.wpa_group),
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003377 broadcast_ether_addr, group->GN,
3378 group->GTK[group->GN - 1], group->GTK_len) < 0)
3379 ret = -1;
3380
3381#ifdef CONFIG_IEEE80211W
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07003382 if (wpa_auth->conf.ieee80211w != NO_MGMT_FRAME_PROTECTION) {
3383 enum wpa_alg alg;
3384 size_t len;
3385
3386 alg = wpa_cipher_to_alg(wpa_auth->conf.group_mgmt_cipher);
3387 len = wpa_cipher_key_len(wpa_auth->conf.group_mgmt_cipher);
3388
3389 if (ret == 0 &&
3390 wpa_auth_set_key(wpa_auth, group->vlan_id, alg,
3391 broadcast_ether_addr, group->GN_igtk,
3392 group->IGTK[group->GN_igtk - 4], len) < 0)
3393 ret = -1;
3394 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003395#endif /* CONFIG_IEEE80211W */
3396
3397 return ret;
3398}
3399
3400
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003401static int wpa_group_disconnect_cb(struct wpa_state_machine *sm, void *ctx)
3402{
3403 if (sm->group == ctx) {
3404 wpa_printf(MSG_DEBUG, "WPA: Mark STA " MACSTR
3405 " for discconnection due to fatal failure",
3406 MAC2STR(sm->addr));
3407 sm->Disconnect = TRUE;
3408 }
3409
3410 return 0;
3411}
3412
3413
3414static void wpa_group_fatal_failure(struct wpa_authenticator *wpa_auth,
3415 struct wpa_group *group)
3416{
3417 wpa_printf(MSG_DEBUG, "WPA: group state machine entering state FATAL_FAILURE");
3418 group->changed = TRUE;
3419 group->wpa_group_state = WPA_GROUP_FATAL_FAILURE;
3420 wpa_auth_for_each_sta(wpa_auth, wpa_group_disconnect_cb, group);
3421}
3422
3423
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003424static int wpa_group_setkeysdone(struct wpa_authenticator *wpa_auth,
3425 struct wpa_group *group)
3426{
3427 wpa_printf(MSG_DEBUG, "WPA: group state machine entering state "
3428 "SETKEYSDONE (VLAN-ID %d)", group->vlan_id);
3429 group->changed = TRUE;
3430 group->wpa_group_state = WPA_GROUP_SETKEYSDONE;
3431
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003432 if (wpa_group_config_group_keys(wpa_auth, group) < 0) {
3433 wpa_group_fatal_failure(wpa_auth, group);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003434 return -1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003435 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003436
3437 return 0;
3438}
3439
3440
3441static void wpa_group_sm_step(struct wpa_authenticator *wpa_auth,
3442 struct wpa_group *group)
3443{
3444 if (group->GInit) {
3445 wpa_group_gtk_init(wpa_auth, group);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003446 } else if (group->wpa_group_state == WPA_GROUP_FATAL_FAILURE) {
3447 /* Do not allow group operations */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003448 } else if (group->wpa_group_state == WPA_GROUP_GTK_INIT &&
3449 group->GTKAuthenticator) {
3450 wpa_group_setkeysdone(wpa_auth, group);
3451 } else if (group->wpa_group_state == WPA_GROUP_SETKEYSDONE &&
3452 group->GTKReKey) {
3453 wpa_group_setkeys(wpa_auth, group);
3454 } else if (group->wpa_group_state == WPA_GROUP_SETKEYS) {
3455 if (group->GKeyDoneStations == 0)
3456 wpa_group_setkeysdone(wpa_auth, group);
3457 else if (group->GTKReKey)
3458 wpa_group_setkeys(wpa_auth, group);
3459 }
3460}
3461
3462
3463static int wpa_sm_step(struct wpa_state_machine *sm)
3464{
3465 if (sm == NULL)
3466 return 0;
3467
3468 if (sm->in_step_loop) {
3469 /* This should not happen, but if it does, make sure we do not
3470 * end up freeing the state machine too early by exiting the
3471 * recursive call. */
3472 wpa_printf(MSG_ERROR, "WPA: wpa_sm_step() called recursively");
3473 return 0;
3474 }
3475
3476 sm->in_step_loop = 1;
3477 do {
3478 if (sm->pending_deinit)
3479 break;
3480
3481 sm->changed = FALSE;
3482 sm->wpa_auth->group->changed = FALSE;
3483
3484 SM_STEP_RUN(WPA_PTK);
3485 if (sm->pending_deinit)
3486 break;
3487 SM_STEP_RUN(WPA_PTK_GROUP);
3488 if (sm->pending_deinit)
3489 break;
3490 wpa_group_sm_step(sm->wpa_auth, sm->group);
3491 } while (sm->changed || sm->wpa_auth->group->changed);
3492 sm->in_step_loop = 0;
3493
3494 if (sm->pending_deinit) {
3495 wpa_printf(MSG_DEBUG, "WPA: Completing pending STA state "
3496 "machine deinit for " MACSTR, MAC2STR(sm->addr));
3497 wpa_free_sta_sm(sm);
3498 return 1;
3499 }
3500 return 0;
3501}
3502
3503
3504static void wpa_sm_call_step(void *eloop_ctx, void *timeout_ctx)
3505{
3506 struct wpa_state_machine *sm = eloop_ctx;
3507 wpa_sm_step(sm);
3508}
3509
3510
3511void wpa_auth_sm_notify(struct wpa_state_machine *sm)
3512{
3513 if (sm == NULL)
3514 return;
3515 eloop_register_timeout(0, 0, wpa_sm_call_step, sm, NULL);
3516}
3517
3518
3519void wpa_gtk_rekey(struct wpa_authenticator *wpa_auth)
3520{
3521 int tmp, i;
3522 struct wpa_group *group;
3523
3524 if (wpa_auth == NULL)
3525 return;
3526
3527 group = wpa_auth->group;
3528
3529 for (i = 0; i < 2; i++) {
3530 tmp = group->GM;
3531 group->GM = group->GN;
3532 group->GN = tmp;
3533#ifdef CONFIG_IEEE80211W
3534 tmp = group->GM_igtk;
3535 group->GM_igtk = group->GN_igtk;
3536 group->GN_igtk = tmp;
3537#endif /* CONFIG_IEEE80211W */
3538 wpa_gtk_update(wpa_auth, group);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003539 wpa_group_config_group_keys(wpa_auth, group);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003540 }
3541}
3542
3543
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07003544static const char * wpa_bool_txt(int val)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003545{
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07003546 return val ? "TRUE" : "FALSE";
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003547}
3548
3549
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003550#define RSN_SUITE "%02x-%02x-%02x-%d"
3551#define RSN_SUITE_ARG(s) \
3552((s) >> 24) & 0xff, ((s) >> 16) & 0xff, ((s) >> 8) & 0xff, (s) & 0xff
3553
3554int wpa_get_mib(struct wpa_authenticator *wpa_auth, char *buf, size_t buflen)
3555{
3556 int len = 0, ret;
3557 char pmkid_txt[PMKID_LEN * 2 + 1];
3558#ifdef CONFIG_RSN_PREAUTH
3559 const int preauth = 1;
3560#else /* CONFIG_RSN_PREAUTH */
3561 const int preauth = 0;
3562#endif /* CONFIG_RSN_PREAUTH */
3563
3564 if (wpa_auth == NULL)
3565 return len;
3566
3567 ret = os_snprintf(buf + len, buflen - len,
3568 "dot11RSNAOptionImplemented=TRUE\n"
3569 "dot11RSNAPreauthenticationImplemented=%s\n"
3570 "dot11RSNAEnabled=%s\n"
3571 "dot11RSNAPreauthenticationEnabled=%s\n",
3572 wpa_bool_txt(preauth),
3573 wpa_bool_txt(wpa_auth->conf.wpa & WPA_PROTO_RSN),
3574 wpa_bool_txt(wpa_auth->conf.rsn_preauth));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003575 if (os_snprintf_error(buflen - len, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003576 return len;
3577 len += ret;
3578
3579 wpa_snprintf_hex(pmkid_txt, sizeof(pmkid_txt),
3580 wpa_auth->dot11RSNAPMKIDUsed, PMKID_LEN);
3581
3582 ret = os_snprintf(
3583 buf + len, buflen - len,
3584 "dot11RSNAConfigVersion=%u\n"
3585 "dot11RSNAConfigPairwiseKeysSupported=9999\n"
3586 /* FIX: dot11RSNAConfigGroupCipher */
3587 /* FIX: dot11RSNAConfigGroupRekeyMethod */
3588 /* FIX: dot11RSNAConfigGroupRekeyTime */
3589 /* FIX: dot11RSNAConfigGroupRekeyPackets */
3590 "dot11RSNAConfigGroupRekeyStrict=%u\n"
3591 "dot11RSNAConfigGroupUpdateCount=%u\n"
3592 "dot11RSNAConfigPairwiseUpdateCount=%u\n"
3593 "dot11RSNAConfigGroupCipherSize=%u\n"
3594 "dot11RSNAConfigPMKLifetime=%u\n"
3595 "dot11RSNAConfigPMKReauthThreshold=%u\n"
3596 "dot11RSNAConfigNumberOfPTKSAReplayCounters=0\n"
3597 "dot11RSNAConfigSATimeout=%u\n"
3598 "dot11RSNAAuthenticationSuiteSelected=" RSN_SUITE "\n"
3599 "dot11RSNAPairwiseCipherSelected=" RSN_SUITE "\n"
3600 "dot11RSNAGroupCipherSelected=" RSN_SUITE "\n"
3601 "dot11RSNAPMKIDUsed=%s\n"
3602 "dot11RSNAAuthenticationSuiteRequested=" RSN_SUITE "\n"
3603 "dot11RSNAPairwiseCipherRequested=" RSN_SUITE "\n"
3604 "dot11RSNAGroupCipherRequested=" RSN_SUITE "\n"
3605 "dot11RSNATKIPCounterMeasuresInvoked=%u\n"
3606 "dot11RSNA4WayHandshakeFailures=%u\n"
3607 "dot11RSNAConfigNumberOfGTKSAReplayCounters=0\n",
3608 RSN_VERSION,
3609 !!wpa_auth->conf.wpa_strict_rekey,
3610 dot11RSNAConfigGroupUpdateCount,
3611 dot11RSNAConfigPairwiseUpdateCount,
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07003612 wpa_cipher_key_len(wpa_auth->conf.wpa_group) * 8,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003613 dot11RSNAConfigPMKLifetime,
3614 dot11RSNAConfigPMKReauthThreshold,
3615 dot11RSNAConfigSATimeout,
3616 RSN_SUITE_ARG(wpa_auth->dot11RSNAAuthenticationSuiteSelected),
3617 RSN_SUITE_ARG(wpa_auth->dot11RSNAPairwiseCipherSelected),
3618 RSN_SUITE_ARG(wpa_auth->dot11RSNAGroupCipherSelected),
3619 pmkid_txt,
3620 RSN_SUITE_ARG(wpa_auth->dot11RSNAAuthenticationSuiteRequested),
3621 RSN_SUITE_ARG(wpa_auth->dot11RSNAPairwiseCipherRequested),
3622 RSN_SUITE_ARG(wpa_auth->dot11RSNAGroupCipherRequested),
3623 wpa_auth->dot11RSNATKIPCounterMeasuresInvoked,
3624 wpa_auth->dot11RSNA4WayHandshakeFailures);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003625 if (os_snprintf_error(buflen - len, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003626 return len;
3627 len += ret;
3628
3629 /* TODO: dot11RSNAConfigPairwiseCiphersTable */
3630 /* TODO: dot11RSNAConfigAuthenticationSuitesTable */
3631
3632 /* Private MIB */
3633 ret = os_snprintf(buf + len, buflen - len, "hostapdWPAGroupState=%d\n",
3634 wpa_auth->group->wpa_group_state);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003635 if (os_snprintf_error(buflen - len, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003636 return len;
3637 len += ret;
3638
3639 return len;
3640}
3641
3642
3643int wpa_get_mib_sta(struct wpa_state_machine *sm, char *buf, size_t buflen)
3644{
3645 int len = 0, ret;
3646 u32 pairwise = 0;
3647
3648 if (sm == NULL)
3649 return 0;
3650
3651 /* TODO: FF-FF-FF-FF-FF-FF entry for broadcast/multicast stats */
3652
3653 /* dot11RSNAStatsEntry */
3654
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07003655 pairwise = wpa_cipher_to_suite(sm->wpa == WPA_VERSION_WPA2 ?
3656 WPA_PROTO_RSN : WPA_PROTO_WPA,
3657 sm->pairwise);
3658 if (pairwise == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003659 return 0;
3660
3661 ret = os_snprintf(
3662 buf + len, buflen - len,
3663 /* TODO: dot11RSNAStatsIndex */
3664 "dot11RSNAStatsSTAAddress=" MACSTR "\n"
3665 "dot11RSNAStatsVersion=1\n"
3666 "dot11RSNAStatsSelectedPairwiseCipher=" RSN_SUITE "\n"
3667 /* TODO: dot11RSNAStatsTKIPICVErrors */
3668 "dot11RSNAStatsTKIPLocalMICFailures=%u\n"
3669 "dot11RSNAStatsTKIPRemoteMICFailures=%u\n"
3670 /* TODO: dot11RSNAStatsCCMPReplays */
3671 /* TODO: dot11RSNAStatsCCMPDecryptErrors */
3672 /* TODO: dot11RSNAStatsTKIPReplays */,
3673 MAC2STR(sm->addr),
3674 RSN_SUITE_ARG(pairwise),
3675 sm->dot11RSNAStatsTKIPLocalMICFailures,
3676 sm->dot11RSNAStatsTKIPRemoteMICFailures);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003677 if (os_snprintf_error(buflen - len, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003678 return len;
3679 len += ret;
3680
3681 /* Private MIB */
3682 ret = os_snprintf(buf + len, buflen - len,
3683 "hostapdWPAPTKState=%d\n"
3684 "hostapdWPAPTKGroupState=%d\n",
3685 sm->wpa_ptk_state,
3686 sm->wpa_ptk_group_state);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003687 if (os_snprintf_error(buflen - len, ret))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003688 return len;
3689 len += ret;
3690
3691 return len;
3692}
3693
3694
3695void wpa_auth_countermeasures_start(struct wpa_authenticator *wpa_auth)
3696{
3697 if (wpa_auth)
3698 wpa_auth->dot11RSNATKIPCounterMeasuresInvoked++;
3699}
3700
3701
3702int wpa_auth_pairwise_set(struct wpa_state_machine *sm)
3703{
3704 return sm && sm->pairwise_set;
3705}
3706
3707
3708int wpa_auth_get_pairwise(struct wpa_state_machine *sm)
3709{
3710 return sm->pairwise;
3711}
3712
3713
3714int wpa_auth_sta_key_mgmt(struct wpa_state_machine *sm)
3715{
3716 if (sm == NULL)
3717 return -1;
3718 return sm->wpa_key_mgmt;
3719}
3720
3721
3722int wpa_auth_sta_wpa_version(struct wpa_state_machine *sm)
3723{
3724 if (sm == NULL)
3725 return 0;
3726 return sm->wpa;
3727}
3728
3729
3730int wpa_auth_sta_clear_pmksa(struct wpa_state_machine *sm,
3731 struct rsn_pmksa_cache_entry *entry)
3732{
3733 if (sm == NULL || sm->pmksa != entry)
3734 return -1;
3735 sm->pmksa = NULL;
3736 return 0;
3737}
3738
3739
3740struct rsn_pmksa_cache_entry *
3741wpa_auth_sta_get_pmksa(struct wpa_state_machine *sm)
3742{
3743 return sm ? sm->pmksa : NULL;
3744}
3745
3746
3747void wpa_auth_sta_local_mic_failure_report(struct wpa_state_machine *sm)
3748{
3749 if (sm)
3750 sm->dot11RSNAStatsTKIPLocalMICFailures++;
3751}
3752
3753
3754const u8 * wpa_auth_get_wpa_ie(struct wpa_authenticator *wpa_auth, size_t *len)
3755{
3756 if (wpa_auth == NULL)
3757 return NULL;
3758 *len = wpa_auth->wpa_ie_len;
3759 return wpa_auth->wpa_ie;
3760}
3761
3762
3763int wpa_auth_pmksa_add(struct wpa_state_machine *sm, const u8 *pmk,
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003764 unsigned int pmk_len,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003765 int session_timeout, struct eapol_state_machine *eapol)
3766{
Dmitry Shmidtc55524a2011-07-07 11:18:38 -07003767 if (sm == NULL || sm->wpa != WPA_VERSION_WPA2 ||
3768 sm->wpa_auth->conf.disable_pmksa_caching)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003769 return -1;
3770
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08003771 if (sm->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X_SUITE_B_192) {
3772 if (pmk_len > PMK_LEN_SUITE_B_192)
3773 pmk_len = PMK_LEN_SUITE_B_192;
3774 } else if (pmk_len > PMK_LEN) {
3775 pmk_len = PMK_LEN;
3776 }
3777
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08003778 if (pmksa_cache_auth_add(sm->wpa_auth->pmksa, pmk, pmk_len, NULL,
Dmitry Shmidt807291d2015-01-27 13:40:23 -08003779 sm->PTK.kck, sm->PTK.kck_len,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003780 sm->wpa_auth->addr, sm->addr, session_timeout,
3781 eapol, sm->wpa_key_mgmt))
3782 return 0;
3783
3784 return -1;
3785}
3786
3787
3788int wpa_auth_pmksa_add_preauth(struct wpa_authenticator *wpa_auth,
3789 const u8 *pmk, size_t len, const u8 *sta_addr,
3790 int session_timeout,
3791 struct eapol_state_machine *eapol)
3792{
3793 if (wpa_auth == NULL)
3794 return -1;
3795
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08003796 if (pmksa_cache_auth_add(wpa_auth->pmksa, pmk, len, NULL,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003797 NULL, 0,
3798 wpa_auth->addr,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003799 sta_addr, session_timeout, eapol,
3800 WPA_KEY_MGMT_IEEE8021X))
3801 return 0;
3802
3803 return -1;
3804}
3805
3806
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003807int wpa_auth_pmksa_add_sae(struct wpa_authenticator *wpa_auth, const u8 *addr,
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08003808 const u8 *pmk, const u8 *pmkid)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003809{
3810 if (wpa_auth->conf.disable_pmksa_caching)
3811 return -1;
3812
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08003813 if (pmksa_cache_auth_add(wpa_auth->pmksa, pmk, PMK_LEN, pmkid,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003814 NULL, 0,
3815 wpa_auth->addr, addr, 0, NULL,
3816 WPA_KEY_MGMT_SAE))
3817 return 0;
3818
3819 return -1;
3820}
3821
3822
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07003823void wpa_auth_pmksa_remove(struct wpa_authenticator *wpa_auth,
3824 const u8 *sta_addr)
3825{
3826 struct rsn_pmksa_cache_entry *pmksa;
3827
3828 if (wpa_auth == NULL || wpa_auth->pmksa == NULL)
3829 return;
3830 pmksa = pmksa_cache_auth_get(wpa_auth->pmksa, sta_addr, NULL);
3831 if (pmksa) {
3832 wpa_printf(MSG_DEBUG, "WPA: Remove PMKSA cache entry for "
3833 MACSTR " based on request", MAC2STR(sta_addr));
3834 pmksa_cache_free_entry(wpa_auth->pmksa, pmksa);
3835 }
3836}
3837
3838
Dmitry Shmidte4663042016-04-04 10:07:49 -07003839int wpa_auth_pmksa_list(struct wpa_authenticator *wpa_auth, char *buf,
3840 size_t len)
3841{
3842 if (!wpa_auth || !wpa_auth->pmksa)
3843 return 0;
3844 return pmksa_cache_auth_list(wpa_auth->pmksa, buf, len);
3845}
3846
3847
3848void wpa_auth_pmksa_flush(struct wpa_authenticator *wpa_auth)
3849{
3850 if (wpa_auth && wpa_auth->pmksa)
3851 pmksa_cache_auth_flush(wpa_auth->pmksa);
3852}
3853
3854
Paul Stewart092955c2017-02-06 09:13:09 -08003855#ifdef CONFIG_PMKSA_CACHE_EXTERNAL
3856#ifdef CONFIG_MESH
3857
3858int wpa_auth_pmksa_list_mesh(struct wpa_authenticator *wpa_auth, const u8 *addr,
3859 char *buf, size_t len)
3860{
3861 if (!wpa_auth || !wpa_auth->pmksa)
3862 return 0;
3863
3864 return pmksa_cache_auth_list_mesh(wpa_auth->pmksa, addr, buf, len);
3865}
3866
3867
3868struct rsn_pmksa_cache_entry *
3869wpa_auth_pmksa_create_entry(const u8 *aa, const u8 *spa, const u8 *pmk,
3870 const u8 *pmkid, int expiration)
3871{
3872 struct rsn_pmksa_cache_entry *entry;
3873 struct os_reltime now;
3874
3875 entry = pmksa_cache_auth_create_entry(pmk, PMK_LEN, pmkid, NULL, 0, aa,
3876 spa, 0, NULL, WPA_KEY_MGMT_SAE);
3877 if (!entry)
3878 return NULL;
3879
3880 os_get_reltime(&now);
3881 entry->expiration = now.sec + expiration;
3882 return entry;
3883}
3884
3885
3886int wpa_auth_pmksa_add_entry(struct wpa_authenticator *wpa_auth,
3887 struct rsn_pmksa_cache_entry *entry)
3888{
3889 int ret;
3890
3891 if (!wpa_auth || !wpa_auth->pmksa)
3892 return -1;
3893
3894 ret = pmksa_cache_auth_add_entry(wpa_auth->pmksa, entry);
3895 if (ret < 0)
3896 wpa_printf(MSG_DEBUG,
3897 "RSN: Failed to store external PMKSA cache for "
3898 MACSTR, MAC2STR(entry->spa));
3899
3900 return ret;
3901}
3902
3903#endif /* CONFIG_MESH */
3904#endif /* CONFIG_PMKSA_CACHE_EXTERNAL */
3905
3906
Dmitry Shmidte4663042016-04-04 10:07:49 -07003907struct rsn_pmksa_cache_entry *
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003908wpa_auth_pmksa_get(struct wpa_authenticator *wpa_auth, const u8 *sta_addr,
3909 const u8 *pmkid)
Dmitry Shmidte4663042016-04-04 10:07:49 -07003910{
3911 if (!wpa_auth || !wpa_auth->pmksa)
3912 return NULL;
Dmitry Shmidt9839ecd2016-11-07 11:05:47 -08003913 return pmksa_cache_auth_get(wpa_auth->pmksa, sta_addr, pmkid);
Dmitry Shmidte4663042016-04-04 10:07:49 -07003914}
3915
3916
3917void wpa_auth_pmksa_set_to_sm(struct rsn_pmksa_cache_entry *pmksa,
3918 struct wpa_state_machine *sm,
3919 struct wpa_authenticator *wpa_auth,
3920 u8 *pmkid, u8 *pmk)
3921{
3922 if (!sm)
3923 return;
3924
3925 sm->pmksa = pmksa;
3926 os_memcpy(pmk, pmksa->pmk, PMK_LEN);
3927 os_memcpy(pmkid, pmksa->pmkid, PMKID_LEN);
3928 os_memcpy(wpa_auth->dot11RSNAPMKIDUsed, pmksa->pmkid, PMKID_LEN);
3929}
3930
3931
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07003932/*
3933 * Remove and free the group from wpa_authenticator. This is triggered by a
3934 * callback to make sure nobody is currently iterating the group list while it
3935 * gets modified.
3936 */
3937static void wpa_group_free(struct wpa_authenticator *wpa_auth,
3938 struct wpa_group *group)
3939{
3940 struct wpa_group *prev = wpa_auth->group;
3941
3942 wpa_printf(MSG_DEBUG, "WPA: Remove group state machine for VLAN-ID %d",
3943 group->vlan_id);
3944
3945 while (prev) {
3946 if (prev->next == group) {
3947 /* This never frees the special first group as needed */
3948 prev->next = group->next;
3949 os_free(group);
3950 break;
3951 }
3952 prev = prev->next;
3953 }
3954
3955}
3956
3957
3958/* Increase the reference counter for group */
3959static void wpa_group_get(struct wpa_authenticator *wpa_auth,
3960 struct wpa_group *group)
3961{
3962 /* Skip the special first group */
3963 if (wpa_auth->group == group)
3964 return;
3965
3966 group->references++;
3967}
3968
3969
3970/* Decrease the reference counter and maybe free the group */
3971static void wpa_group_put(struct wpa_authenticator *wpa_auth,
3972 struct wpa_group *group)
3973{
3974 /* Skip the special first group */
3975 if (wpa_auth->group == group)
3976 return;
3977
3978 group->references--;
3979 if (group->references)
3980 return;
3981 wpa_group_free(wpa_auth, group);
3982}
3983
3984
3985/*
3986 * Add a group that has its references counter set to zero. Caller needs to
3987 * call wpa_group_get() on the return value to mark the entry in use.
3988 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003989static struct wpa_group *
3990wpa_auth_add_group(struct wpa_authenticator *wpa_auth, int vlan_id)
3991{
3992 struct wpa_group *group;
3993
3994 if (wpa_auth == NULL || wpa_auth->group == NULL)
3995 return NULL;
3996
3997 wpa_printf(MSG_DEBUG, "WPA: Add group state machine for VLAN-ID %d",
3998 vlan_id);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003999 group = wpa_group_init(wpa_auth, vlan_id, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004000 if (group == NULL)
4001 return NULL;
4002
4003 group->next = wpa_auth->group->next;
4004 wpa_auth->group->next = group;
4005
4006 return group;
4007}
4008
4009
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08004010/*
4011 * Enforce that the group state machine for the VLAN is running, increase
4012 * reference counter as interface is up. References might have been increased
4013 * even if a negative value is returned.
4014 * Returns: -1 on error (group missing, group already failed); otherwise, 0
4015 */
4016int wpa_auth_ensure_group(struct wpa_authenticator *wpa_auth, int vlan_id)
4017{
4018 struct wpa_group *group;
4019
4020 if (wpa_auth == NULL)
4021 return 0;
4022
4023 group = wpa_auth->group;
4024 while (group) {
4025 if (group->vlan_id == vlan_id)
4026 break;
4027 group = group->next;
4028 }
4029
4030 if (group == NULL) {
4031 group = wpa_auth_add_group(wpa_auth, vlan_id);
4032 if (group == NULL)
4033 return -1;
4034 }
4035
4036 wpa_printf(MSG_DEBUG,
4037 "WPA: Ensure group state machine running for VLAN ID %d",
4038 vlan_id);
4039
4040 wpa_group_get(wpa_auth, group);
4041 group->num_setup_iface++;
4042
4043 if (group->wpa_group_state == WPA_GROUP_FATAL_FAILURE)
4044 return -1;
4045
4046 return 0;
4047}
4048
4049
4050/*
4051 * Decrease reference counter, expected to be zero afterwards.
4052 * returns: -1 on error (group not found, group in fail state)
4053 * -2 if wpa_group is still referenced
4054 * 0 else
4055 */
4056int wpa_auth_release_group(struct wpa_authenticator *wpa_auth, int vlan_id)
4057{
4058 struct wpa_group *group;
4059 int ret = 0;
4060
4061 if (wpa_auth == NULL)
4062 return 0;
4063
4064 group = wpa_auth->group;
4065 while (group) {
4066 if (group->vlan_id == vlan_id)
4067 break;
4068 group = group->next;
4069 }
4070
4071 if (group == NULL)
4072 return -1;
4073
4074 wpa_printf(MSG_DEBUG,
4075 "WPA: Try stopping group state machine for VLAN ID %d",
4076 vlan_id);
4077
4078 if (group->num_setup_iface <= 0) {
4079 wpa_printf(MSG_ERROR,
4080 "WPA: wpa_auth_release_group called more often than wpa_auth_ensure_group for VLAN ID %d, skipping.",
4081 vlan_id);
4082 return -1;
4083 }
4084 group->num_setup_iface--;
4085
4086 if (group->wpa_group_state == WPA_GROUP_FATAL_FAILURE)
4087 ret = -1;
4088
4089 if (group->references > 1) {
4090 wpa_printf(MSG_DEBUG,
4091 "WPA: Cannot stop group state machine for VLAN ID %d as references are still hold",
4092 vlan_id);
4093 ret = -2;
4094 }
4095
4096 wpa_group_put(wpa_auth, group);
4097
4098 return ret;
4099}
4100
4101
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004102int wpa_auth_sta_set_vlan(struct wpa_state_machine *sm, int vlan_id)
4103{
4104 struct wpa_group *group;
4105
4106 if (sm == NULL || sm->wpa_auth == NULL)
4107 return 0;
4108
4109 group = sm->wpa_auth->group;
4110 while (group) {
4111 if (group->vlan_id == vlan_id)
4112 break;
4113 group = group->next;
4114 }
4115
4116 if (group == NULL) {
4117 group = wpa_auth_add_group(sm->wpa_auth, vlan_id);
4118 if (group == NULL)
4119 return -1;
4120 }
4121
4122 if (sm->group == group)
4123 return 0;
4124
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004125 if (group->wpa_group_state == WPA_GROUP_FATAL_FAILURE)
4126 return -1;
4127
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004128 wpa_printf(MSG_DEBUG, "WPA: Moving STA " MACSTR " to use group state "
4129 "machine for VLAN ID %d", MAC2STR(sm->addr), vlan_id);
4130
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07004131 wpa_group_get(sm->wpa_auth, group);
4132 wpa_group_put(sm->wpa_auth, sm->group);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004133 sm->group = group;
Dmitry Shmidt1d755d02015-04-28 10:34:29 -07004134
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004135 return 0;
4136}
4137
4138
4139void wpa_auth_eapol_key_tx_status(struct wpa_authenticator *wpa_auth,
4140 struct wpa_state_machine *sm, int ack)
4141{
4142 if (wpa_auth == NULL || sm == NULL)
4143 return;
4144 wpa_printf(MSG_DEBUG, "WPA: EAPOL-Key TX status for STA " MACSTR
4145 " ack=%d", MAC2STR(sm->addr), ack);
4146 if (sm->pending_1_of_4_timeout && ack) {
4147 /*
4148 * Some deployed supplicant implementations update their SNonce
4149 * for each EAPOL-Key 2/4 message even within the same 4-way
4150 * handshake and then fail to use the first SNonce when
4151 * deriving the PTK. This results in unsuccessful 4-way
4152 * handshake whenever the relatively short initial timeout is
4153 * reached and EAPOL-Key 1/4 is retransmitted. Try to work
4154 * around this by increasing the timeout now that we know that
4155 * the station has received the frame.
4156 */
4157 int timeout_ms = eapol_key_timeout_subseq;
4158 wpa_printf(MSG_DEBUG, "WPA: Increase initial EAPOL-Key 1/4 "
4159 "timeout by %u ms because of acknowledged frame",
4160 timeout_ms);
4161 eloop_cancel_timeout(wpa_send_eapol_timeout, wpa_auth, sm);
4162 eloop_register_timeout(timeout_ms / 1000,
4163 (timeout_ms % 1000) * 1000,
4164 wpa_send_eapol_timeout, wpa_auth, sm);
4165 }
4166}
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004167
4168
4169int wpa_auth_uses_sae(struct wpa_state_machine *sm)
4170{
4171 if (sm == NULL)
4172 return 0;
4173 return wpa_key_mgmt_sae(sm->wpa_key_mgmt);
4174}
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004175
4176
4177int wpa_auth_uses_ft_sae(struct wpa_state_machine *sm)
4178{
4179 if (sm == NULL)
4180 return 0;
4181 return sm->wpa_key_mgmt == WPA_KEY_MGMT_FT_SAE;
4182}
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004183
4184
4185#ifdef CONFIG_P2P
4186int wpa_auth_get_ip_addr(struct wpa_state_machine *sm, u8 *addr)
4187{
4188 if (sm == NULL || WPA_GET_BE32(sm->ip_addr) == 0)
4189 return -1;
4190 os_memcpy(addr, sm->ip_addr, 4);
4191 return 0;
4192}
4193#endif /* CONFIG_P2P */
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08004194
4195
4196int wpa_auth_radius_das_disconnect_pmksa(struct wpa_authenticator *wpa_auth,
4197 struct radius_das_attrs *attr)
4198{
4199 return pmksa_cache_auth_radius_das_disconnect(wpa_auth->pmksa, attr);
4200}
Dmitry Shmidt7f656022015-02-25 14:36:37 -08004201
4202
4203void wpa_auth_reconfig_group_keys(struct wpa_authenticator *wpa_auth)
4204{
4205 struct wpa_group *group;
4206
4207 if (!wpa_auth)
4208 return;
4209 for (group = wpa_auth->group; group; group = group->next)
4210 wpa_group_config_group_keys(wpa_auth, group);
4211}