]>
Commit | Line | Data |
---|---|---|
6fc6879b | 1 | /* |
00338a4f | 2 | * IEEE 802.11 RSN / WPA Authenticator |
98cd3d1c | 3 | * Copyright (c) 2004-2015, Jouni Malinen <j@w1.fi> |
6fc6879b | 4 | * |
0f3d578e JM |
5 | * This software may be distributed under the terms of the BSD license. |
6 | * See README for more details. | |
6fc6879b JM |
7 | */ |
8 | ||
6226e38d | 9 | #include "utils/includes.h" |
6fc6879b | 10 | |
6226e38d JM |
11 | #include "utils/common.h" |
12 | #include "utils/eloop.h" | |
13 | #include "utils/state_machine.h" | |
25ef8529 | 14 | #include "utils/bitfield.h" |
81f4f619 | 15 | #include "common/ieee802_11_defs.h" |
3b5b7aa8 | 16 | #include "crypto/aes.h" |
03da66bd | 17 | #include "crypto/aes_wrap.h" |
3b5b7aa8 | 18 | #include "crypto/aes_siv.h" |
03da66bd JM |
19 | #include "crypto/crypto.h" |
20 | #include "crypto/sha1.h" | |
21 | #include "crypto/sha256.h" | |
3642c431 | 22 | #include "crypto/random.h" |
281c950b | 23 | #include "eapol_auth/eapol_auth_sm.h" |
6226e38d | 24 | #include "ap_config.h" |
6fc6879b | 25 | #include "ieee802_11.h" |
6226e38d JM |
26 | #include "wpa_auth.h" |
27 | #include "pmksa_cache_auth.h" | |
6fc6879b JM |
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 | ||
36 | static void wpa_send_eapol_timeout(void *eloop_ctx, void *timeout_ctx); | |
e4a6ea1d | 37 | static int wpa_sm_step(struct wpa_state_machine *sm); |
929a2ea5 JM |
38 | static int wpa_verify_key_mic(int akmp, struct wpa_ptk *PTK, u8 *data, |
39 | size_t data_len); | |
75c8563e JM |
40 | #ifdef CONFIG_FILS |
41 | static 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 */ | |
6fc6879b JM |
44 | static void wpa_sm_call_step(void *eloop_ctx, void *timeout_ctx); |
45 | static void wpa_group_sm_step(struct wpa_authenticator *wpa_auth, | |
46 | struct wpa_group *group); | |
581a8cde | 47 | static void wpa_request_new_ptk(struct wpa_state_machine *sm); |
e6965d4e JM |
48 | static int wpa_gtk_update(struct wpa_authenticator *wpa_auth, |
49 | struct wpa_group *group); | |
1bdb7ab3 JM |
50 | static int wpa_group_config_group_keys(struct wpa_authenticator *wpa_auth, |
51 | struct wpa_group *group); | |
f23c5b17 | 52 | static int wpa_derive_ptk(struct wpa_state_machine *sm, const u8 *snonce, |
207976f0 JM |
53 | const u8 *pmk, unsigned int pmk_len, |
54 | struct wpa_ptk *ptk); | |
a0ad9e8c MB |
55 | static void wpa_group_free(struct wpa_authenticator *wpa_auth, |
56 | struct wpa_group *group); | |
57 | static void wpa_group_get(struct wpa_authenticator *wpa_auth, | |
58 | struct wpa_group *group); | |
59 | static void wpa_group_put(struct wpa_authenticator *wpa_auth, | |
60 | struct wpa_group *group); | |
e73ffa09 | 61 | static u8 * ieee80211w_kde_add(struct wpa_state_machine *sm, u8 *pos); |
6fc6879b | 62 | |
bae61562 JM |
63 | static const u32 eapol_key_timeout_first = 100; /* ms */ |
64 | static const u32 eapol_key_timeout_subseq = 1000; /* ms */ | |
00338a4f | 65 | static const u32 eapol_key_timeout_first_group = 500; /* ms */ |
6fc6879b JM |
66 | |
67 | /* TODO: make these configurable */ | |
68 | static const int dot11RSNAConfigPMKLifetime = 43200; | |
69 | static const int dot11RSNAConfigPMKReauthThreshold = 70; | |
70 | static const int dot11RSNAConfigSATimeout = 60; | |
71 | ||
72 | ||
c772d054 | 73 | static inline int wpa_auth_mic_failure_report( |
6fc6879b JM |
74 | struct wpa_authenticator *wpa_auth, const u8 *addr) |
75 | { | |
cef8fac0 JB |
76 | if (wpa_auth->cb->mic_failure_report) |
77 | return wpa_auth->cb->mic_failure_report(wpa_auth->cb_ctx, addr); | |
c772d054 | 78 | return 0; |
6fc6879b JM |
79 | } |
80 | ||
81 | ||
2c502460 JM |
82 | static inline void wpa_auth_psk_failure_report( |
83 | struct wpa_authenticator *wpa_auth, const u8 *addr) | |
84 | { | |
cef8fac0 JB |
85 | if (wpa_auth->cb->psk_failure_report) |
86 | wpa_auth->cb->psk_failure_report(wpa_auth->cb_ctx, addr); | |
2c502460 JM |
87 | } |
88 | ||
89 | ||
6fc6879b JM |
90 | static inline void wpa_auth_set_eapol(struct wpa_authenticator *wpa_auth, |
91 | const u8 *addr, wpa_eapol_variable var, | |
92 | int value) | |
93 | { | |
cef8fac0 JB |
94 | if (wpa_auth->cb->set_eapol) |
95 | wpa_auth->cb->set_eapol(wpa_auth->cb_ctx, addr, var, value); | |
6fc6879b JM |
96 | } |
97 | ||
98 | ||
99 | static inline int wpa_auth_get_eapol(struct wpa_authenticator *wpa_auth, | |
100 | const u8 *addr, wpa_eapol_variable var) | |
101 | { | |
cef8fac0 | 102 | if (wpa_auth->cb->get_eapol == NULL) |
6fc6879b | 103 | return -1; |
cef8fac0 | 104 | return wpa_auth->cb->get_eapol(wpa_auth->cb_ctx, addr, var); |
6fc6879b JM |
105 | } |
106 | ||
107 | ||
108 | static inline const u8 * wpa_auth_get_psk(struct wpa_authenticator *wpa_auth, | |
759fd76b JM |
109 | const u8 *addr, |
110 | const u8 *p2p_dev_addr, | |
111 | const u8 *prev_psk) | |
6fc6879b | 112 | { |
cef8fac0 | 113 | if (wpa_auth->cb->get_psk == NULL) |
6fc6879b | 114 | return NULL; |
cef8fac0 JB |
115 | return wpa_auth->cb->get_psk(wpa_auth->cb_ctx, addr, p2p_dev_addr, |
116 | prev_psk); | |
6fc6879b JM |
117 | } |
118 | ||
119 | ||
120 | static inline int wpa_auth_get_msk(struct wpa_authenticator *wpa_auth, | |
121 | const u8 *addr, u8 *msk, size_t *len) | |
122 | { | |
cef8fac0 | 123 | if (wpa_auth->cb->get_msk == NULL) |
6fc6879b | 124 | return -1; |
cef8fac0 | 125 | return wpa_auth->cb->get_msk(wpa_auth->cb_ctx, addr, msk, len); |
6fc6879b JM |
126 | } |
127 | ||
128 | ||
129 | static inline int wpa_auth_set_key(struct wpa_authenticator *wpa_auth, | |
130 | int vlan_id, | |
71934751 | 131 | enum wpa_alg alg, const u8 *addr, int idx, |
6fc6879b JM |
132 | u8 *key, size_t key_len) |
133 | { | |
cef8fac0 | 134 | if (wpa_auth->cb->set_key == NULL) |
6fc6879b | 135 | return -1; |
cef8fac0 JB |
136 | return wpa_auth->cb->set_key(wpa_auth->cb_ctx, vlan_id, alg, addr, idx, |
137 | key, key_len); | |
6fc6879b JM |
138 | } |
139 | ||
140 | ||
141 | static inline int wpa_auth_get_seqnum(struct wpa_authenticator *wpa_auth, | |
142 | const u8 *addr, int idx, u8 *seq) | |
143 | { | |
cef8fac0 | 144 | if (wpa_auth->cb->get_seqnum == NULL) |
6fc6879b | 145 | return -1; |
cef8fac0 | 146 | return wpa_auth->cb->get_seqnum(wpa_auth->cb_ctx, addr, idx, seq); |
6fc6879b JM |
147 | } |
148 | ||
149 | ||
6fc6879b JM |
150 | static inline int |
151 | wpa_auth_send_eapol(struct wpa_authenticator *wpa_auth, const u8 *addr, | |
152 | const u8 *data, size_t data_len, int encrypt) | |
153 | { | |
cef8fac0 | 154 | if (wpa_auth->cb->send_eapol == NULL) |
6fc6879b | 155 | return -1; |
cef8fac0 JB |
156 | return wpa_auth->cb->send_eapol(wpa_auth->cb_ctx, addr, data, data_len, |
157 | encrypt); | |
6fc6879b JM |
158 | } |
159 | ||
160 | ||
c50d94f1 BC |
161 | #ifdef CONFIG_MESH |
162 | static inline int wpa_auth_start_ampe(struct wpa_authenticator *wpa_auth, | |
163 | const u8 *addr) | |
164 | { | |
cef8fac0 | 165 | if (wpa_auth->cb->start_ampe == NULL) |
c50d94f1 | 166 | return -1; |
cef8fac0 | 167 | return wpa_auth->cb->start_ampe(wpa_auth->cb_ctx, addr); |
c50d94f1 BC |
168 | } |
169 | #endif /* CONFIG_MESH */ | |
170 | ||
171 | ||
6fc6879b JM |
172 | int wpa_auth_for_each_sta(struct wpa_authenticator *wpa_auth, |
173 | int (*cb)(struct wpa_state_machine *sm, void *ctx), | |
174 | void *cb_ctx) | |
175 | { | |
cef8fac0 | 176 | if (wpa_auth->cb->for_each_sta == NULL) |
6fc6879b | 177 | return 0; |
cef8fac0 | 178 | return wpa_auth->cb->for_each_sta(wpa_auth->cb_ctx, cb, cb_ctx); |
6fc6879b JM |
179 | } |
180 | ||
181 | ||
bf98f7f3 JM |
182 | int wpa_auth_for_each_auth(struct wpa_authenticator *wpa_auth, |
183 | int (*cb)(struct wpa_authenticator *a, void *ctx), | |
184 | void *cb_ctx) | |
185 | { | |
cef8fac0 | 186 | if (wpa_auth->cb->for_each_auth == NULL) |
bf98f7f3 | 187 | return 0; |
cef8fac0 | 188 | return wpa_auth->cb->for_each_auth(wpa_auth->cb_ctx, cb, cb_ctx); |
bf98f7f3 JM |
189 | } |
190 | ||
191 | ||
6fc6879b JM |
192 | void wpa_auth_logger(struct wpa_authenticator *wpa_auth, const u8 *addr, |
193 | logger_level level, const char *txt) | |
194 | { | |
cef8fac0 | 195 | if (wpa_auth->cb->logger == NULL) |
6fc6879b | 196 | return; |
cef8fac0 | 197 | wpa_auth->cb->logger(wpa_auth->cb_ctx, addr, level, txt); |
6fc6879b JM |
198 | } |
199 | ||
200 | ||
201 | void wpa_auth_vlogger(struct wpa_authenticator *wpa_auth, const u8 *addr, | |
202 | logger_level level, const char *fmt, ...) | |
203 | { | |
204 | char *format; | |
205 | int maxlen; | |
206 | va_list ap; | |
207 | ||
cef8fac0 | 208 | if (wpa_auth->cb->logger == NULL) |
6fc6879b JM |
209 | return; |
210 | ||
211 | maxlen = os_strlen(fmt) + 100; | |
212 | format = os_malloc(maxlen); | |
213 | if (!format) | |
214 | return; | |
215 | ||
216 | va_start(ap, fmt); | |
217 | vsnprintf(format, maxlen, fmt, ap); | |
218 | va_end(ap); | |
219 | ||
220 | wpa_auth_logger(wpa_auth, addr, level, format); | |
221 | ||
222 | os_free(format); | |
223 | } | |
224 | ||
225 | ||
226 | static void wpa_sta_disconnect(struct wpa_authenticator *wpa_auth, | |
227 | const u8 *addr) | |
228 | { | |
cef8fac0 | 229 | if (wpa_auth->cb->disconnect == NULL) |
6fc6879b | 230 | return; |
afcc9ea1 | 231 | wpa_printf(MSG_DEBUG, "wpa_sta_disconnect STA " MACSTR, MAC2STR(addr)); |
cef8fac0 JB |
232 | wpa_auth->cb->disconnect(wpa_auth->cb_ctx, addr, |
233 | WLAN_REASON_PREV_AUTH_NOT_VALID); | |
6fc6879b JM |
234 | } |
235 | ||
236 | ||
237 | static int wpa_use_aes_cmac(struct wpa_state_machine *sm) | |
238 | { | |
56586197 | 239 | int ret = 0; |
4ec1fd8e | 240 | #ifdef CONFIG_IEEE80211R_AP |
56586197 JM |
241 | if (wpa_key_mgmt_ft(sm->wpa_key_mgmt)) |
242 | ret = 1; | |
4ec1fd8e | 243 | #endif /* CONFIG_IEEE80211R_AP */ |
56586197 JM |
244 | #ifdef CONFIG_IEEE80211W |
245 | if (wpa_key_mgmt_sha256(sm->wpa_key_mgmt)) | |
246 | ret = 1; | |
247 | #endif /* CONFIG_IEEE80211W */ | |
a14896e8 JM |
248 | if (sm->wpa_key_mgmt == WPA_KEY_MGMT_OSEN) |
249 | ret = 1; | |
56586197 | 250 | return ret; |
6fc6879b JM |
251 | } |
252 | ||
253 | ||
254 | static void wpa_rekey_gmk(void *eloop_ctx, void *timeout_ctx) | |
255 | { | |
256 | struct wpa_authenticator *wpa_auth = eloop_ctx; | |
257 | ||
3642c431 | 258 | if (random_get_bytes(wpa_auth->group->GMK, WPA_GMK_LEN)) { |
6fc6879b JM |
259 | wpa_printf(MSG_ERROR, "Failed to get random data for WPA " |
260 | "initialization."); | |
261 | } else { | |
262 | wpa_auth_logger(wpa_auth, NULL, LOGGER_DEBUG, "GMK rekeyd"); | |
5cb9d5c3 JM |
263 | wpa_hexdump_key(MSG_DEBUG, "GMK", |
264 | wpa_auth->group->GMK, WPA_GMK_LEN); | |
6fc6879b JM |
265 | } |
266 | ||
267 | if (wpa_auth->conf.wpa_gmk_rekey) { | |
268 | eloop_register_timeout(wpa_auth->conf.wpa_gmk_rekey, 0, | |
269 | wpa_rekey_gmk, wpa_auth, NULL); | |
270 | } | |
271 | } | |
272 | ||
273 | ||
274 | static void wpa_rekey_gtk(void *eloop_ctx, void *timeout_ctx) | |
275 | { | |
276 | struct wpa_authenticator *wpa_auth = eloop_ctx; | |
a0ad9e8c | 277 | struct wpa_group *group, *next; |
6fc6879b JM |
278 | |
279 | wpa_auth_logger(wpa_auth, NULL, LOGGER_DEBUG, "rekeying GTK"); | |
a0ad9e8c MB |
280 | group = wpa_auth->group; |
281 | while (group) { | |
282 | wpa_group_get(wpa_auth, group); | |
283 | ||
6fc6879b JM |
284 | group->GTKReKey = TRUE; |
285 | do { | |
286 | group->changed = FALSE; | |
287 | wpa_group_sm_step(wpa_auth, group); | |
288 | } while (group->changed); | |
a0ad9e8c MB |
289 | |
290 | next = group->next; | |
291 | wpa_group_put(wpa_auth, group); | |
292 | group = next; | |
6fc6879b JM |
293 | } |
294 | ||
295 | if (wpa_auth->conf.wpa_group_rekey) { | |
296 | eloop_register_timeout(wpa_auth->conf.wpa_group_rekey, | |
297 | 0, wpa_rekey_gtk, wpa_auth, NULL); | |
298 | } | |
299 | } | |
300 | ||
301 | ||
581a8cde JM |
302 | static void wpa_rekey_ptk(void *eloop_ctx, void *timeout_ctx) |
303 | { | |
304 | struct wpa_authenticator *wpa_auth = eloop_ctx; | |
305 | struct wpa_state_machine *sm = timeout_ctx; | |
306 | ||
307 | wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG, "rekeying PTK"); | |
308 | wpa_request_new_ptk(sm); | |
309 | wpa_sm_step(sm); | |
310 | } | |
311 | ||
312 | ||
6fc6879b JM |
313 | static int wpa_auth_pmksa_clear_cb(struct wpa_state_machine *sm, void *ctx) |
314 | { | |
315 | if (sm->pmksa == ctx) | |
316 | sm->pmksa = NULL; | |
317 | return 0; | |
318 | } | |
319 | ||
320 | ||
321 | static void wpa_auth_pmksa_free_cb(struct rsn_pmksa_cache_entry *entry, | |
322 | void *ctx) | |
323 | { | |
324 | struct wpa_authenticator *wpa_auth = ctx; | |
325 | wpa_auth_for_each_sta(wpa_auth, wpa_auth_pmksa_clear_cb, entry); | |
326 | } | |
327 | ||
328 | ||
1bdb7ab3 JM |
329 | static int wpa_group_init_gmk_and_counter(struct wpa_authenticator *wpa_auth, |
330 | struct wpa_group *group) | |
331 | { | |
559cdabb | 332 | u8 buf[ETH_ALEN + 8 + sizeof(unsigned long)]; |
1bdb7ab3 | 333 | u8 rkey[32]; |
559cdabb | 334 | unsigned long ptr; |
1bdb7ab3 | 335 | |
3642c431 | 336 | if (random_get_bytes(group->GMK, WPA_GMK_LEN) < 0) |
1bdb7ab3 JM |
337 | return -1; |
338 | wpa_hexdump_key(MSG_DEBUG, "GMK", group->GMK, WPA_GMK_LEN); | |
339 | ||
340 | /* | |
341 | * Counter = PRF-256(Random number, "Init Counter", | |
342 | * Local MAC Address || Time) | |
343 | */ | |
344 | os_memcpy(buf, wpa_auth->addr, ETH_ALEN); | |
345 | wpa_get_ntp_timestamp(buf + ETH_ALEN); | |
559cdabb JM |
346 | ptr = (unsigned long) group; |
347 | os_memcpy(buf + ETH_ALEN + 8, &ptr, sizeof(ptr)); | |
3642c431 | 348 | if (random_get_bytes(rkey, sizeof(rkey)) < 0) |
1bdb7ab3 JM |
349 | return -1; |
350 | ||
351 | if (sha1_prf(rkey, sizeof(rkey), "Init Counter", buf, sizeof(buf), | |
352 | group->Counter, WPA_NONCE_LEN) < 0) | |
353 | return -1; | |
354 | wpa_hexdump_key(MSG_DEBUG, "Key Counter", | |
355 | group->Counter, WPA_NONCE_LEN); | |
356 | ||
357 | return 0; | |
358 | } | |
359 | ||
360 | ||
e6965d4e | 361 | static struct wpa_group * wpa_group_init(struct wpa_authenticator *wpa_auth, |
bdffdc5d | 362 | int vlan_id, int delay_init) |
e6965d4e JM |
363 | { |
364 | struct wpa_group *group; | |
e6965d4e JM |
365 | |
366 | group = os_zalloc(sizeof(struct wpa_group)); | |
367 | if (group == NULL) | |
368 | return NULL; | |
369 | ||
370 | group->GTKAuthenticator = TRUE; | |
371 | group->vlan_id = vlan_id; | |
c3550295 | 372 | group->GTK_len = wpa_cipher_key_len(wpa_auth->conf.wpa_group); |
6fc6879b | 373 | |
08704cd8 JM |
374 | if (random_pool_ready() != 1) { |
375 | wpa_printf(MSG_INFO, "WPA: Not enough entropy in random pool " | |
376 | "for secure operations - update keys later when " | |
377 | "the first station connects"); | |
378 | } | |
379 | ||
1bdb7ab3 JM |
380 | /* |
381 | * Set initial GMK/Counter value here. The actual values that will be | |
382 | * used in negotiations will be set once the first station tries to | |
383 | * connect. This allows more time for collecting additional randomness | |
384 | * on embedded devices. | |
6fc6879b | 385 | */ |
1bdb7ab3 | 386 | if (wpa_group_init_gmk_and_counter(wpa_auth, group) < 0) { |
6fc6879b JM |
387 | wpa_printf(MSG_ERROR, "Failed to get random data for WPA " |
388 | "initialization."); | |
389 | os_free(group); | |
390 | return NULL; | |
391 | } | |
6fc6879b JM |
392 | |
393 | group->GInit = TRUE; | |
bdffdc5d JM |
394 | if (delay_init) { |
395 | wpa_printf(MSG_DEBUG, "WPA: Delay group state machine start " | |
396 | "until Beacon frames have been configured"); | |
397 | /* Initialization is completed in wpa_init_keys(). */ | |
398 | } else { | |
399 | wpa_group_sm_step(wpa_auth, group); | |
400 | group->GInit = FALSE; | |
401 | wpa_group_sm_step(wpa_auth, group); | |
402 | } | |
6fc6879b JM |
403 | |
404 | return group; | |
405 | } | |
406 | ||
407 | ||
408 | /** | |
409 | * wpa_init - Initialize WPA authenticator | |
410 | * @addr: Authenticator address | |
411 | * @conf: Configuration for WPA authenticator | |
a17df5fb | 412 | * @cb: Callback functions for WPA authenticator |
6fc6879b JM |
413 | * Returns: Pointer to WPA authenticator data or %NULL on failure |
414 | */ | |
415 | struct wpa_authenticator * wpa_init(const u8 *addr, | |
416 | struct wpa_auth_config *conf, | |
cef8fac0 JB |
417 | const struct wpa_auth_callbacks *cb, |
418 | void *cb_ctx) | |
6fc6879b JM |
419 | { |
420 | struct wpa_authenticator *wpa_auth; | |
421 | ||
422 | wpa_auth = os_zalloc(sizeof(struct wpa_authenticator)); | |
423 | if (wpa_auth == NULL) | |
424 | return NULL; | |
425 | os_memcpy(wpa_auth->addr, addr, ETH_ALEN); | |
426 | os_memcpy(&wpa_auth->conf, conf, sizeof(*conf)); | |
cef8fac0 JB |
427 | wpa_auth->cb = cb; |
428 | wpa_auth->cb_ctx = cb_ctx; | |
6fc6879b JM |
429 | |
430 | if (wpa_auth_gen_wpa_ie(wpa_auth)) { | |
431 | wpa_printf(MSG_ERROR, "Could not generate WPA IE."); | |
432 | os_free(wpa_auth); | |
433 | return NULL; | |
434 | } | |
435 | ||
bdffdc5d | 436 | wpa_auth->group = wpa_group_init(wpa_auth, 0, 1); |
6fc6879b JM |
437 | if (wpa_auth->group == NULL) { |
438 | os_free(wpa_auth->wpa_ie); | |
439 | os_free(wpa_auth); | |
440 | return NULL; | |
441 | } | |
442 | ||
4bb081f1 JM |
443 | wpa_auth->pmksa = pmksa_cache_auth_init(wpa_auth_pmksa_free_cb, |
444 | wpa_auth); | |
6fc6879b JM |
445 | if (wpa_auth->pmksa == NULL) { |
446 | wpa_printf(MSG_ERROR, "PMKSA cache initialization failed."); | |
27d9701d | 447 | os_free(wpa_auth->group); |
6fc6879b JM |
448 | os_free(wpa_auth->wpa_ie); |
449 | os_free(wpa_auth); | |
450 | return NULL; | |
451 | } | |
452 | ||
4ec1fd8e | 453 | #ifdef CONFIG_IEEE80211R_AP |
6fc6879b JM |
454 | wpa_auth->ft_pmk_cache = wpa_ft_pmk_cache_init(); |
455 | if (wpa_auth->ft_pmk_cache == NULL) { | |
456 | wpa_printf(MSG_ERROR, "FT PMK cache initialization failed."); | |
27d9701d | 457 | os_free(wpa_auth->group); |
6fc6879b | 458 | os_free(wpa_auth->wpa_ie); |
4bb081f1 | 459 | pmksa_cache_auth_deinit(wpa_auth->pmksa); |
6fc6879b JM |
460 | os_free(wpa_auth); |
461 | return NULL; | |
462 | } | |
4ec1fd8e | 463 | #endif /* CONFIG_IEEE80211R_AP */ |
6fc6879b JM |
464 | |
465 | if (wpa_auth->conf.wpa_gmk_rekey) { | |
466 | eloop_register_timeout(wpa_auth->conf.wpa_gmk_rekey, 0, | |
467 | wpa_rekey_gmk, wpa_auth, NULL); | |
468 | } | |
469 | ||
470 | if (wpa_auth->conf.wpa_group_rekey) { | |
471 | eloop_register_timeout(wpa_auth->conf.wpa_group_rekey, 0, | |
472 | wpa_rekey_gtk, wpa_auth, NULL); | |
473 | } | |
474 | ||
25ef8529 JM |
475 | #ifdef CONFIG_P2P |
476 | if (WPA_GET_BE32(conf->ip_addr_start)) { | |
477 | int count = WPA_GET_BE32(conf->ip_addr_end) - | |
478 | WPA_GET_BE32(conf->ip_addr_start) + 1; | |
479 | if (count > 1000) | |
480 | count = 1000; | |
481 | if (count > 0) | |
482 | wpa_auth->ip_pool = bitfield_alloc(count); | |
483 | } | |
484 | #endif /* CONFIG_P2P */ | |
485 | ||
6fc6879b JM |
486 | return wpa_auth; |
487 | } | |
488 | ||
489 | ||
bdffdc5d JM |
490 | int wpa_init_keys(struct wpa_authenticator *wpa_auth) |
491 | { | |
492 | struct wpa_group *group = wpa_auth->group; | |
493 | ||
494 | wpa_printf(MSG_DEBUG, "WPA: Start group state machine to set initial " | |
495 | "keys"); | |
496 | wpa_group_sm_step(wpa_auth, group); | |
497 | group->GInit = FALSE; | |
498 | wpa_group_sm_step(wpa_auth, group); | |
7d7f7be2 JM |
499 | if (group->wpa_group_state == WPA_GROUP_FATAL_FAILURE) |
500 | return -1; | |
bdffdc5d JM |
501 | return 0; |
502 | } | |
503 | ||
504 | ||
6fc6879b JM |
505 | /** |
506 | * wpa_deinit - Deinitialize WPA authenticator | |
507 | * @wpa_auth: Pointer to WPA authenticator data from wpa_init() | |
508 | */ | |
509 | void wpa_deinit(struct wpa_authenticator *wpa_auth) | |
510 | { | |
511 | struct wpa_group *group, *prev; | |
512 | ||
513 | eloop_cancel_timeout(wpa_rekey_gmk, wpa_auth, NULL); | |
514 | eloop_cancel_timeout(wpa_rekey_gtk, wpa_auth, NULL); | |
515 | ||
4bb081f1 | 516 | pmksa_cache_auth_deinit(wpa_auth->pmksa); |
6fc6879b | 517 | |
4ec1fd8e | 518 | #ifdef CONFIG_IEEE80211R_AP |
6fc6879b JM |
519 | wpa_ft_pmk_cache_deinit(wpa_auth->ft_pmk_cache); |
520 | wpa_auth->ft_pmk_cache = NULL; | |
4ec1fd8e | 521 | #endif /* CONFIG_IEEE80211R_AP */ |
6fc6879b | 522 | |
25ef8529 JM |
523 | #ifdef CONFIG_P2P |
524 | bitfield_free(wpa_auth->ip_pool); | |
525 | #endif /* CONFIG_P2P */ | |
526 | ||
527 | ||
6fc6879b JM |
528 | os_free(wpa_auth->wpa_ie); |
529 | ||
530 | group = wpa_auth->group; | |
531 | while (group) { | |
532 | prev = group; | |
533 | group = group->next; | |
534 | os_free(prev); | |
535 | } | |
536 | ||
537 | os_free(wpa_auth); | |
538 | } | |
539 | ||
540 | ||
541 | /** | |
542 | * wpa_reconfig - Update WPA authenticator configuration | |
543 | * @wpa_auth: Pointer to WPA authenticator data from wpa_init() | |
544 | * @conf: Configuration for WPA authenticator | |
545 | */ | |
546 | int wpa_reconfig(struct wpa_authenticator *wpa_auth, | |
547 | struct wpa_auth_config *conf) | |
548 | { | |
e6965d4e | 549 | struct wpa_group *group; |
6fc6879b JM |
550 | if (wpa_auth == NULL) |
551 | return 0; | |
552 | ||
553 | os_memcpy(&wpa_auth->conf, conf, sizeof(*conf)); | |
ad08c363 JM |
554 | if (wpa_auth_gen_wpa_ie(wpa_auth)) { |
555 | wpa_printf(MSG_ERROR, "Could not generate WPA IE."); | |
556 | return -1; | |
557 | } | |
558 | ||
e6965d4e JM |
559 | /* |
560 | * Reinitialize GTK to make sure it is suitable for the new | |
561 | * configuration. | |
562 | */ | |
563 | group = wpa_auth->group; | |
c3550295 | 564 | group->GTK_len = wpa_cipher_key_len(wpa_auth->conf.wpa_group); |
e6965d4e JM |
565 | group->GInit = TRUE; |
566 | wpa_group_sm_step(wpa_auth, group); | |
567 | group->GInit = FALSE; | |
568 | wpa_group_sm_step(wpa_auth, group); | |
569 | ||
6fc6879b JM |
570 | return 0; |
571 | } | |
572 | ||
573 | ||
574 | struct wpa_state_machine * | |
94ddef3e JM |
575 | wpa_auth_sta_init(struct wpa_authenticator *wpa_auth, const u8 *addr, |
576 | const u8 *p2p_dev_addr) | |
6fc6879b JM |
577 | { |
578 | struct wpa_state_machine *sm; | |
579 | ||
7d7f7be2 JM |
580 | if (wpa_auth->group->wpa_group_state == WPA_GROUP_FATAL_FAILURE) |
581 | return NULL; | |
582 | ||
6fc6879b JM |
583 | sm = os_zalloc(sizeof(struct wpa_state_machine)); |
584 | if (sm == NULL) | |
585 | return NULL; | |
586 | os_memcpy(sm->addr, addr, ETH_ALEN); | |
94ddef3e JM |
587 | if (p2p_dev_addr) |
588 | os_memcpy(sm->p2p_dev_addr, p2p_dev_addr, ETH_ALEN); | |
6fc6879b JM |
589 | |
590 | sm->wpa_auth = wpa_auth; | |
591 | sm->group = wpa_auth->group; | |
a0ad9e8c | 592 | wpa_group_get(sm->wpa_auth, sm->group); |
6fc6879b JM |
593 | |
594 | return sm; | |
595 | } | |
596 | ||
597 | ||
6f9b5d16 JM |
598 | int wpa_auth_sta_associated(struct wpa_authenticator *wpa_auth, |
599 | struct wpa_state_machine *sm) | |
6fc6879b JM |
600 | { |
601 | if (wpa_auth == NULL || !wpa_auth->conf.wpa || sm == NULL) | |
6f9b5d16 | 602 | return -1; |
6fc6879b | 603 | |
4ec1fd8e | 604 | #ifdef CONFIG_IEEE80211R_AP |
6fc6879b JM |
605 | if (sm->ft_completed) { |
606 | wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG, | |
607 | "FT authentication already completed - do not " | |
608 | "start 4-way handshake"); | |
3d4d2348 JM |
609 | /* Go to PTKINITDONE state to allow GTK rekeying */ |
610 | sm->wpa_ptk_state = WPA_PTK_PTKINITDONE; | |
3bbc4705 | 611 | sm->Pair = TRUE; |
6f9b5d16 | 612 | return 0; |
6fc6879b | 613 | } |
4ec1fd8e | 614 | #endif /* CONFIG_IEEE80211R_AP */ |
6fc6879b | 615 | |
07e0117d JM |
616 | #ifdef CONFIG_FILS |
617 | if (sm->fils_completed) { | |
618 | wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG, | |
619 | "FILS authentication already completed - do not start 4-way handshake"); | |
620 | /* Go to PTKINITDONE state to allow GTK rekeying */ | |
621 | sm->wpa_ptk_state = WPA_PTK_PTKINITDONE; | |
3bbc4705 | 622 | sm->Pair = TRUE; |
07e0117d JM |
623 | return 0; |
624 | } | |
625 | #endif /* CONFIG_FILS */ | |
626 | ||
6fc6879b | 627 | if (sm->started) { |
22a299ee | 628 | os_memset(&sm->key_replay, 0, sizeof(sm->key_replay)); |
6fc6879b | 629 | sm->ReAuthenticationRequest = TRUE; |
6f9b5d16 | 630 | return wpa_sm_step(sm); |
6fc6879b JM |
631 | } |
632 | ||
633 | wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG, | |
634 | "start authentication"); | |
635 | sm->started = 1; | |
636 | ||
637 | sm->Init = TRUE; | |
e4a6ea1d | 638 | if (wpa_sm_step(sm) == 1) |
6f9b5d16 | 639 | return 1; /* should not really happen */ |
6fc6879b JM |
640 | sm->Init = FALSE; |
641 | sm->AuthenticationRequest = TRUE; | |
6f9b5d16 | 642 | return wpa_sm_step(sm); |
6fc6879b JM |
643 | } |
644 | ||
645 | ||
a8d05fca JM |
646 | void wpa_auth_sta_no_wpa(struct wpa_state_machine *sm) |
647 | { | |
648 | /* WPA/RSN was not used - clear WPA state. This is needed if the STA | |
649 | * reassociates back to the same AP while the previous entry for the | |
650 | * STA has not yet been removed. */ | |
651 | if (sm == NULL) | |
652 | return; | |
653 | ||
654 | sm->wpa_key_mgmt = 0; | |
655 | } | |
656 | ||
657 | ||
6fc6879b JM |
658 | static void wpa_free_sta_sm(struct wpa_state_machine *sm) |
659 | { | |
25ef8529 JM |
660 | #ifdef CONFIG_P2P |
661 | if (WPA_GET_BE32(sm->ip_addr)) { | |
662 | u32 start; | |
663 | wpa_printf(MSG_DEBUG, "P2P: Free assigned IP " | |
664 | "address %u.%u.%u.%u from " MACSTR, | |
665 | sm->ip_addr[0], sm->ip_addr[1], | |
666 | sm->ip_addr[2], sm->ip_addr[3], | |
667 | MAC2STR(sm->addr)); | |
668 | start = WPA_GET_BE32(sm->wpa_auth->conf.ip_addr_start); | |
669 | bitfield_clear(sm->wpa_auth->ip_pool, | |
670 | WPA_GET_BE32(sm->ip_addr) - start); | |
671 | } | |
672 | #endif /* CONFIG_P2P */ | |
2ade8ef2 JM |
673 | if (sm->GUpdateStationKeys) { |
674 | sm->group->GKeyDoneStations--; | |
675 | sm->GUpdateStationKeys = FALSE; | |
676 | } | |
4ec1fd8e | 677 | #ifdef CONFIG_IEEE80211R_AP |
0f857f43 | 678 | os_free(sm->assoc_resp_ftie); |
692ec305 | 679 | wpabuf_free(sm->ft_pending_req_ies); |
4ec1fd8e | 680 | #endif /* CONFIG_IEEE80211R_AP */ |
6fc6879b JM |
681 | os_free(sm->last_rx_eapol_key); |
682 | os_free(sm->wpa_ie); | |
a0ad9e8c | 683 | wpa_group_put(sm->wpa_auth, sm->group); |
6fc6879b JM |
684 | os_free(sm); |
685 | } | |
686 | ||
687 | ||
688 | void wpa_auth_sta_deinit(struct wpa_state_machine *sm) | |
689 | { | |
690 | if (sm == NULL) | |
691 | return; | |
692 | ||
693 | if (sm->wpa_auth->conf.wpa_strict_rekey && sm->has_GTK) { | |
694 | wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG, | |
695 | "strict rekeying - force GTK rekey since STA " | |
696 | "is leaving"); | |
697 | eloop_cancel_timeout(wpa_rekey_gtk, sm->wpa_auth, NULL); | |
698 | eloop_register_timeout(0, 500000, wpa_rekey_gtk, sm->wpa_auth, | |
699 | NULL); | |
700 | } | |
701 | ||
702 | eloop_cancel_timeout(wpa_send_eapol_timeout, sm->wpa_auth, sm); | |
e4bf4db9 | 703 | sm->pending_1_of_4_timeout = 0; |
6fc6879b | 704 | eloop_cancel_timeout(wpa_sm_call_step, sm, NULL); |
581a8cde | 705 | eloop_cancel_timeout(wpa_rekey_ptk, sm->wpa_auth, sm); |
6fc6879b JM |
706 | if (sm->in_step_loop) { |
707 | /* Must not free state machine while wpa_sm_step() is running. | |
708 | * Freeing will be completed in the end of wpa_sm_step(). */ | |
709 | wpa_printf(MSG_DEBUG, "WPA: Registering pending STA state " | |
710 | "machine deinit for " MACSTR, MAC2STR(sm->addr)); | |
711 | sm->pending_deinit = 1; | |
712 | } else | |
713 | wpa_free_sta_sm(sm); | |
714 | } | |
715 | ||
716 | ||
717 | static void wpa_request_new_ptk(struct wpa_state_machine *sm) | |
718 | { | |
719 | if (sm == NULL) | |
720 | return; | |
721 | ||
722 | sm->PTKRequest = TRUE; | |
723 | sm->PTK_valid = 0; | |
724 | } | |
725 | ||
726 | ||
68921e24 | 727 | static int wpa_replay_counter_valid(struct wpa_key_replay_counter *ctr, |
22a299ee JM |
728 | const u8 *replay_counter) |
729 | { | |
730 | int i; | |
731 | for (i = 0; i < RSNA_MAX_EAPOL_RETRIES; i++) { | |
68921e24 | 732 | if (!ctr[i].valid) |
22a299ee | 733 | break; |
68921e24 | 734 | if (os_memcmp(replay_counter, ctr[i].counter, |
22a299ee JM |
735 | WPA_REPLAY_COUNTER_LEN) == 0) |
736 | return 1; | |
737 | } | |
738 | return 0; | |
739 | } | |
740 | ||
741 | ||
68921e24 JM |
742 | static void wpa_replay_counter_mark_invalid(struct wpa_key_replay_counter *ctr, |
743 | const u8 *replay_counter) | |
744 | { | |
745 | int i; | |
746 | for (i = 0; i < RSNA_MAX_EAPOL_RETRIES; i++) { | |
747 | if (ctr[i].valid && | |
748 | (replay_counter == NULL || | |
749 | os_memcmp(replay_counter, ctr[i].counter, | |
750 | WPA_REPLAY_COUNTER_LEN) == 0)) | |
751 | ctr[i].valid = FALSE; | |
752 | } | |
753 | } | |
754 | ||
755 | ||
4ec1fd8e | 756 | #ifdef CONFIG_IEEE80211R_AP |
0f857f43 JM |
757 | static int ft_check_msg_2_of_4(struct wpa_authenticator *wpa_auth, |
758 | struct wpa_state_machine *sm, | |
759 | struct wpa_eapol_ie_parse *kde) | |
760 | { | |
761 | struct wpa_ie_data ie; | |
762 | struct rsn_mdie *mdie; | |
763 | ||
764 | if (wpa_parse_wpa_ie_rsn(kde->rsn_ie, kde->rsn_ie_len, &ie) < 0 || | |
765 | ie.num_pmkid != 1 || ie.pmkid == NULL) { | |
766 | wpa_printf(MSG_DEBUG, "FT: No PMKR1Name in " | |
767 | "FT 4-way handshake message 2/4"); | |
768 | return -1; | |
769 | } | |
770 | ||
771 | os_memcpy(sm->sup_pmk_r1_name, ie.pmkid, PMKID_LEN); | |
772 | wpa_hexdump(MSG_DEBUG, "FT: PMKR1Name from Supplicant", | |
773 | sm->sup_pmk_r1_name, PMKID_LEN); | |
774 | ||
775 | if (!kde->mdie || !kde->ftie) { | |
776 | wpa_printf(MSG_DEBUG, "FT: No %s in FT 4-way handshake " | |
777 | "message 2/4", kde->mdie ? "FTIE" : "MDIE"); | |
778 | return -1; | |
779 | } | |
780 | ||
781 | mdie = (struct rsn_mdie *) (kde->mdie + 2); | |
782 | if (kde->mdie[1] < sizeof(struct rsn_mdie) || | |
783 | os_memcmp(wpa_auth->conf.mobility_domain, mdie->mobility_domain, | |
784 | MOBILITY_DOMAIN_ID_LEN) != 0) { | |
785 | wpa_printf(MSG_DEBUG, "FT: MDIE mismatch"); | |
786 | return -1; | |
787 | } | |
788 | ||
789 | if (sm->assoc_resp_ftie && | |
790 | (kde->ftie[1] != sm->assoc_resp_ftie[1] || | |
791 | os_memcmp(kde->ftie, sm->assoc_resp_ftie, | |
792 | 2 + sm->assoc_resp_ftie[1]) != 0)) { | |
793 | wpa_printf(MSG_DEBUG, "FT: FTIE mismatch"); | |
794 | wpa_hexdump(MSG_DEBUG, "FT: FTIE in EAPOL-Key msg 2/4", | |
795 | kde->ftie, kde->ftie_len); | |
796 | wpa_hexdump(MSG_DEBUG, "FT: FTIE in (Re)AssocResp", | |
797 | sm->assoc_resp_ftie, 2 + sm->assoc_resp_ftie[1]); | |
798 | return -1; | |
799 | } | |
800 | ||
801 | return 0; | |
802 | } | |
4ec1fd8e | 803 | #endif /* CONFIG_IEEE80211R_AP */ |
0f857f43 JM |
804 | |
805 | ||
c772d054 JM |
806 | static int wpa_receive_error_report(struct wpa_authenticator *wpa_auth, |
807 | struct wpa_state_machine *sm, int group) | |
ec027805 JM |
808 | { |
809 | /* Supplicant reported a Michael MIC error */ | |
810 | wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_INFO, | |
811 | "received EAPOL-Key Error Request " | |
812 | "(STA detected Michael MIC failure (group=%d))", | |
813 | group); | |
fbc72d32 JM |
814 | |
815 | if (group && wpa_auth->conf.wpa_group != WPA_CIPHER_TKIP) { | |
816 | wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO, | |
817 | "ignore Michael MIC failure report since " | |
818 | "group cipher is not TKIP"); | |
819 | } else if (!group && sm->pairwise != WPA_CIPHER_TKIP) { | |
820 | wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO, | |
821 | "ignore Michael MIC failure report since " | |
822 | "pairwise cipher is not TKIP"); | |
823 | } else { | |
c772d054 JM |
824 | if (wpa_auth_mic_failure_report(wpa_auth, sm->addr) > 0) |
825 | return 1; /* STA entry was removed */ | |
fbc72d32 JM |
826 | sm->dot11RSNAStatsTKIPRemoteMICFailures++; |
827 | wpa_auth->dot11RSNAStatsTKIPRemoteMICFailures++; | |
828 | } | |
ec027805 JM |
829 | |
830 | /* | |
831 | * Error report is not a request for a new key handshake, but since | |
832 | * Authenticator may do it, let's change the keys now anyway. | |
833 | */ | |
834 | wpa_request_new_ptk(sm); | |
c772d054 | 835 | return 0; |
ec027805 JM |
836 | } |
837 | ||
838 | ||
f23c5b17 JM |
839 | static int wpa_try_alt_snonce(struct wpa_state_machine *sm, u8 *data, |
840 | size_t data_len) | |
841 | { | |
842 | struct wpa_ptk PTK; | |
843 | int ok = 0; | |
844 | const u8 *pmk = NULL; | |
207976f0 | 845 | unsigned int pmk_len; |
f23c5b17 | 846 | |
b729fd8d | 847 | os_memset(&PTK, 0, sizeof(PTK)); |
f23c5b17 JM |
848 | for (;;) { |
849 | if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt)) { | |
850 | pmk = wpa_auth_get_psk(sm->wpa_auth, sm->addr, | |
851 | sm->p2p_dev_addr, pmk); | |
852 | if (pmk == NULL) | |
853 | break; | |
207976f0 JM |
854 | pmk_len = PMK_LEN; |
855 | } else { | |
f23c5b17 | 856 | pmk = sm->PMK; |
207976f0 JM |
857 | pmk_len = sm->pmk_len; |
858 | } | |
f23c5b17 | 859 | |
364c064a JM |
860 | if (wpa_derive_ptk(sm, sm->alt_SNonce, pmk, pmk_len, &PTK) < 0) |
861 | break; | |
f23c5b17 JM |
862 | |
863 | if (wpa_verify_key_mic(sm->wpa_key_mgmt, &PTK, data, data_len) | |
864 | == 0) { | |
865 | ok = 1; | |
866 | break; | |
867 | } | |
868 | ||
869 | if (!wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt)) | |
870 | break; | |
871 | } | |
872 | ||
873 | if (!ok) { | |
874 | wpa_printf(MSG_DEBUG, | |
875 | "WPA: Earlier SNonce did not result in matching MIC"); | |
876 | return -1; | |
877 | } | |
878 | ||
879 | wpa_printf(MSG_DEBUG, | |
880 | "WPA: Earlier SNonce resulted in matching MIC"); | |
881 | sm->alt_snonce_valid = 0; | |
882 | os_memcpy(sm->SNonce, sm->alt_SNonce, WPA_NONCE_LEN); | |
883 | os_memcpy(&sm->PTK, &PTK, sizeof(PTK)); | |
884 | sm->PTK_valid = TRUE; | |
885 | ||
886 | return 0; | |
887 | } | |
888 | ||
889 | ||
6fc6879b JM |
890 | void wpa_receive(struct wpa_authenticator *wpa_auth, |
891 | struct wpa_state_machine *sm, | |
892 | u8 *data, size_t data_len) | |
893 | { | |
894 | struct ieee802_1x_hdr *hdr; | |
895 | struct wpa_eapol_key *key; | |
896 | u16 key_info, key_data_length; | |
897 | enum { PAIRWISE_2, PAIRWISE_4, GROUP_2, REQUEST, | |
898 | SMK_M1, SMK_M3, SMK_ERROR } msg; | |
899 | char *msgtxt; | |
900 | struct wpa_eapol_ie_parse kde; | |
3b5b7aa8 JM |
901 | const u8 *key_data; |
902 | size_t keyhdrlen, mic_len; | |
6d014ffc | 903 | u8 *mic; |
6fc6879b JM |
904 | |
905 | if (wpa_auth == NULL || !wpa_auth->conf.wpa || sm == NULL) | |
906 | return; | |
dc5bad48 | 907 | wpa_hexdump(MSG_MSGDUMP, "WPA: RX EAPOL data", data, data_len); |
6fc6879b | 908 | |
5e3b5197 | 909 | mic_len = wpa_mic_len(sm->wpa_key_mgmt); |
6d014ffc | 910 | keyhdrlen = sizeof(*key) + mic_len + 2; |
5e3b5197 | 911 | |
dc5bad48 JM |
912 | if (data_len < sizeof(*hdr) + keyhdrlen) { |
913 | wpa_printf(MSG_DEBUG, "WPA: Ignore too short EAPOL-Key frame"); | |
6fc6879b | 914 | return; |
dc5bad48 | 915 | } |
6fc6879b JM |
916 | |
917 | hdr = (struct ieee802_1x_hdr *) data; | |
918 | key = (struct wpa_eapol_key *) (hdr + 1); | |
6d014ffc | 919 | mic = (u8 *) (key + 1); |
6fc6879b | 920 | key_info = WPA_GET_BE16(key->key_info); |
6d014ffc JM |
921 | key_data = mic + mic_len + 2; |
922 | key_data_length = WPA_GET_BE16(mic + mic_len); | |
0d442aff | 923 | wpa_printf(MSG_DEBUG, "WPA: Received EAPOL-Key from " MACSTR |
dc5bad48 JM |
924 | " key_info=0x%x type=%u mic_len=%u key_data_length=%u", |
925 | MAC2STR(sm->addr), key_info, key->type, | |
926 | (unsigned int) mic_len, key_data_length); | |
927 | wpa_hexdump(MSG_MSGDUMP, | |
928 | "WPA: EAPOL-Key header (ending before Key MIC)", | |
929 | key, sizeof(*key)); | |
930 | wpa_hexdump(MSG_MSGDUMP, "WPA: EAPOL-Key Key MIC", | |
931 | mic, mic_len); | |
5e3b5197 | 932 | if (key_data_length > data_len - sizeof(*hdr) - keyhdrlen) { |
6fc6879b JM |
933 | wpa_printf(MSG_INFO, "WPA: Invalid EAPOL-Key frame - " |
934 | "key_data overflow (%d > %lu)", | |
935 | key_data_length, | |
936 | (unsigned long) (data_len - sizeof(*hdr) - | |
5e3b5197 | 937 | keyhdrlen)); |
6fc6879b JM |
938 | return; |
939 | } | |
940 | ||
f8e96eb6 | 941 | if (sm->wpa == WPA_VERSION_WPA2) { |
74590e71 JM |
942 | if (key->type == EAPOL_KEY_TYPE_WPA) { |
943 | /* | |
944 | * Some deployed station implementations seem to send | |
945 | * msg 4/4 with incorrect type value in WPA2 mode. | |
946 | */ | |
947 | wpa_printf(MSG_DEBUG, "Workaround: Allow EAPOL-Key " | |
948 | "with unexpected WPA type in RSN mode"); | |
949 | } else if (key->type != EAPOL_KEY_TYPE_RSN) { | |
f8e96eb6 JM |
950 | wpa_printf(MSG_DEBUG, "Ignore EAPOL-Key with " |
951 | "unexpected type %d in RSN mode", | |
952 | key->type); | |
953 | return; | |
954 | } | |
955 | } else { | |
956 | if (key->type != EAPOL_KEY_TYPE_WPA) { | |
957 | wpa_printf(MSG_DEBUG, "Ignore EAPOL-Key with " | |
958 | "unexpected type %d in WPA mode", | |
959 | key->type); | |
960 | return; | |
961 | } | |
962 | } | |
963 | ||
bc8318ac JM |
964 | wpa_hexdump(MSG_DEBUG, "WPA: Received Key Nonce", key->key_nonce, |
965 | WPA_NONCE_LEN); | |
966 | wpa_hexdump(MSG_DEBUG, "WPA: Received Replay Counter", | |
967 | key->replay_counter, WPA_REPLAY_COUNTER_LEN); | |
968 | ||
6fc6879b JM |
969 | /* FIX: verify that the EAPOL-Key frame was encrypted if pairwise keys |
970 | * are set */ | |
971 | ||
972 | if ((key_info & (WPA_KEY_INFO_SMK_MESSAGE | WPA_KEY_INFO_REQUEST)) == | |
973 | (WPA_KEY_INFO_SMK_MESSAGE | WPA_KEY_INFO_REQUEST)) { | |
974 | if (key_info & WPA_KEY_INFO_ERROR) { | |
975 | msg = SMK_ERROR; | |
976 | msgtxt = "SMK Error"; | |
977 | } else { | |
978 | msg = SMK_M1; | |
979 | msgtxt = "SMK M1"; | |
980 | } | |
981 | } else if (key_info & WPA_KEY_INFO_SMK_MESSAGE) { | |
982 | msg = SMK_M3; | |
983 | msgtxt = "SMK M3"; | |
984 | } else if (key_info & WPA_KEY_INFO_REQUEST) { | |
985 | msg = REQUEST; | |
986 | msgtxt = "Request"; | |
987 | } else if (!(key_info & WPA_KEY_INFO_KEY_TYPE)) { | |
988 | msg = GROUP_2; | |
989 | msgtxt = "2/2 Group"; | |
3b5b7aa8 JM |
990 | } else if (key_data_length == 0 || |
991 | (mic_len == 0 && (key_info & WPA_KEY_INFO_ENCR_KEY_DATA) && | |
992 | key_data_length == AES_BLOCK_SIZE)) { | |
6fc6879b JM |
993 | msg = PAIRWISE_4; |
994 | msgtxt = "4/4 Pairwise"; | |
995 | } else { | |
996 | msg = PAIRWISE_2; | |
997 | msgtxt = "2/4 Pairwise"; | |
998 | } | |
999 | ||
1000 | /* TODO: key_info type validation for PeerKey */ | |
1001 | if (msg == REQUEST || msg == PAIRWISE_2 || msg == PAIRWISE_4 || | |
1002 | msg == GROUP_2) { | |
1003 | u16 ver = key_info & WPA_KEY_INFO_TYPE_MASK; | |
eb7719ff JM |
1004 | if (sm->pairwise == WPA_CIPHER_CCMP || |
1005 | sm->pairwise == WPA_CIPHER_GCMP) { | |
6fc6879b | 1006 | if (wpa_use_aes_cmac(sm) && |
a14896e8 | 1007 | sm->wpa_key_mgmt != WPA_KEY_MGMT_OSEN && |
929a2ea5 | 1008 | !wpa_key_mgmt_suite_b(sm->wpa_key_mgmt) && |
b9866483 | 1009 | !wpa_key_mgmt_fils(sm->wpa_key_mgmt) && |
6fc6879b JM |
1010 | ver != WPA_KEY_INFO_TYPE_AES_128_CMAC) { |
1011 | wpa_auth_logger(wpa_auth, sm->addr, | |
1012 | LOGGER_WARNING, | |
1013 | "advertised support for " | |
1014 | "AES-128-CMAC, but did not " | |
1015 | "use it"); | |
1016 | return; | |
1017 | } | |
1018 | ||
1019 | if (!wpa_use_aes_cmac(sm) && | |
2449791b | 1020 | !wpa_key_mgmt_fils(sm->wpa_key_mgmt) && |
07a5fe82 | 1021 | sm->wpa_key_mgmt != WPA_KEY_MGMT_OWE && |
6fc6879b JM |
1022 | ver != WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) { |
1023 | wpa_auth_logger(wpa_auth, sm->addr, | |
1024 | LOGGER_WARNING, | |
1025 | "did not use HMAC-SHA1-AES " | |
eb7719ff | 1026 | "with CCMP/GCMP"); |
6fc6879b JM |
1027 | return; |
1028 | } | |
1029 | } | |
929a2ea5 | 1030 | |
2449791b | 1031 | if ((wpa_key_mgmt_suite_b(sm->wpa_key_mgmt) || |
07a5fe82 JM |
1032 | wpa_key_mgmt_fils(sm->wpa_key_mgmt) || |
1033 | sm->wpa_key_mgmt == WPA_KEY_MGMT_OWE) && | |
929a2ea5 JM |
1034 | ver != WPA_KEY_INFO_TYPE_AKM_DEFINED) { |
1035 | wpa_auth_logger(wpa_auth, sm->addr, LOGGER_WARNING, | |
1036 | "did not use EAPOL-Key descriptor version 0 as required for AKM-defined cases"); | |
1037 | return; | |
1038 | } | |
6fc6879b JM |
1039 | } |
1040 | ||
1041 | if (key_info & WPA_KEY_INFO_REQUEST) { | |
1042 | if (sm->req_replay_counter_used && | |
1043 | os_memcmp(key->replay_counter, sm->req_replay_counter, | |
1044 | WPA_REPLAY_COUNTER_LEN) <= 0) { | |
1045 | wpa_auth_logger(wpa_auth, sm->addr, LOGGER_WARNING, | |
1046 | "received EAPOL-Key request with " | |
1047 | "replayed counter"); | |
1048 | return; | |
1049 | } | |
1050 | } | |
1051 | ||
1052 | if (!(key_info & WPA_KEY_INFO_REQUEST) && | |
68921e24 | 1053 | !wpa_replay_counter_valid(sm->key_replay, key->replay_counter)) { |
22a299ee | 1054 | int i; |
68921e24 JM |
1055 | |
1056 | if (msg == PAIRWISE_2 && | |
1057 | wpa_replay_counter_valid(sm->prev_key_replay, | |
1058 | key->replay_counter) && | |
1059 | sm->wpa_ptk_state == WPA_PTK_PTKINITNEGOTIATING && | |
1060 | os_memcmp(sm->SNonce, key->key_nonce, WPA_NONCE_LEN) != 0) | |
1061 | { | |
1062 | /* | |
1063 | * Some supplicant implementations (e.g., Windows XP | |
1064 | * WZC) update SNonce for each EAPOL-Key 2/4. This | |
1065 | * breaks the workaround on accepting any of the | |
1066 | * pending requests, so allow the SNonce to be updated | |
1067 | * even if we have already sent out EAPOL-Key 3/4. | |
1068 | */ | |
1069 | wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_DEBUG, | |
1070 | "Process SNonce update from STA " | |
1071 | "based on retransmitted EAPOL-Key " | |
1072 | "1/4"); | |
1073 | sm->update_snonce = 1; | |
f23c5b17 JM |
1074 | os_memcpy(sm->alt_SNonce, sm->SNonce, WPA_NONCE_LEN); |
1075 | sm->alt_snonce_valid = TRUE; | |
1076 | os_memcpy(sm->alt_replay_counter, | |
1077 | sm->key_replay[0].counter, | |
1078 | WPA_REPLAY_COUNTER_LEN); | |
1079 | goto continue_processing; | |
1080 | } | |
1081 | ||
1082 | if (msg == PAIRWISE_4 && sm->alt_snonce_valid && | |
1083 | sm->wpa_ptk_state == WPA_PTK_PTKINITNEGOTIATING && | |
1084 | os_memcmp(key->replay_counter, sm->alt_replay_counter, | |
1085 | WPA_REPLAY_COUNTER_LEN) == 0) { | |
1086 | /* | |
1087 | * Supplicant may still be using the old SNonce since | |
1088 | * there was two EAPOL-Key 2/4 messages and they had | |
1089 | * different SNonce values. | |
1090 | */ | |
1091 | wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_DEBUG, | |
1092 | "Try to process received EAPOL-Key 4/4 based on old Replay Counter and SNonce from an earlier EAPOL-Key 1/4"); | |
68921e24 JM |
1093 | goto continue_processing; |
1094 | } | |
1095 | ||
1096 | if (msg == PAIRWISE_2 && | |
1097 | wpa_replay_counter_valid(sm->prev_key_replay, | |
1098 | key->replay_counter) && | |
1099 | sm->wpa_ptk_state == WPA_PTK_PTKINITNEGOTIATING) { | |
1100 | wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_DEBUG, | |
1101 | "ignore retransmitted EAPOL-Key %s - " | |
1102 | "SNonce did not change", msgtxt); | |
1103 | } else { | |
1104 | wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_DEBUG, | |
1105 | "received EAPOL-Key %s with " | |
1106 | "unexpected replay counter", msgtxt); | |
1107 | } | |
22a299ee JM |
1108 | for (i = 0; i < RSNA_MAX_EAPOL_RETRIES; i++) { |
1109 | if (!sm->key_replay[i].valid) | |
1110 | break; | |
1111 | wpa_hexdump(MSG_DEBUG, "pending replay counter", | |
1112 | sm->key_replay[i].counter, | |
1113 | WPA_REPLAY_COUNTER_LEN); | |
1114 | } | |
6fc6879b JM |
1115 | wpa_hexdump(MSG_DEBUG, "received replay counter", |
1116 | key->replay_counter, WPA_REPLAY_COUNTER_LEN); | |
1117 | return; | |
1118 | } | |
1119 | ||
68921e24 | 1120 | continue_processing: |
3b5b7aa8 JM |
1121 | #ifdef CONFIG_FILS |
1122 | if (sm->wpa == WPA_VERSION_WPA2 && mic_len == 0 && | |
1123 | !(key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) { | |
1124 | wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_DEBUG, | |
1125 | "WPA: Encr Key Data bit not set even though AEAD cipher is supposed to be used - drop frame"); | |
1126 | return; | |
1127 | } | |
1128 | #endif /* CONFIG_FILS */ | |
1129 | ||
6fc6879b JM |
1130 | switch (msg) { |
1131 | case PAIRWISE_2: | |
1132 | if (sm->wpa_ptk_state != WPA_PTK_PTKSTART && | |
68921e24 JM |
1133 | sm->wpa_ptk_state != WPA_PTK_PTKCALCNEGOTIATING && |
1134 | (!sm->update_snonce || | |
1135 | sm->wpa_ptk_state != WPA_PTK_PTKINITNEGOTIATING)) { | |
6fc6879b JM |
1136 | wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_INFO, |
1137 | "received EAPOL-Key msg 2/4 in " | |
1138 | "invalid state (%d) - dropped", | |
1139 | sm->wpa_ptk_state); | |
1140 | return; | |
1141 | } | |
bbb921da | 1142 | random_add_randomness(key->key_nonce, WPA_NONCE_LEN); |
08704cd8 JM |
1143 | if (sm->group->reject_4way_hs_for_entropy) { |
1144 | /* | |
1145 | * The system did not have enough entropy to generate | |
1146 | * strong random numbers. Reject the first 4-way | |
1147 | * handshake(s) and collect some entropy based on the | |
1148 | * information from it. Once enough entropy is | |
1149 | * available, the next atempt will trigger GMK/Key | |
1150 | * Counter update and the station will be allowed to | |
1151 | * continue. | |
1152 | */ | |
1153 | wpa_printf(MSG_DEBUG, "WPA: Reject 4-way handshake to " | |
1154 | "collect more entropy for random number " | |
1155 | "generation"); | |
08704cd8 | 1156 | random_mark_pool_ready(); |
08704cd8 JM |
1157 | wpa_sta_disconnect(wpa_auth, sm->addr); |
1158 | return; | |
1159 | } | |
6fc6879b JM |
1160 | break; |
1161 | case PAIRWISE_4: | |
1162 | if (sm->wpa_ptk_state != WPA_PTK_PTKINITNEGOTIATING || | |
1163 | !sm->PTK_valid) { | |
1164 | wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_INFO, | |
1165 | "received EAPOL-Key msg 4/4 in " | |
1166 | "invalid state (%d) - dropped", | |
1167 | sm->wpa_ptk_state); | |
1168 | return; | |
1169 | } | |
1170 | break; | |
1171 | case GROUP_2: | |
1172 | if (sm->wpa_ptk_group_state != WPA_PTK_GROUP_REKEYNEGOTIATING | |
1173 | || !sm->PTK_valid) { | |
1174 | wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_INFO, | |
1175 | "received EAPOL-Key msg 2/2 in " | |
1176 | "invalid state (%d) - dropped", | |
1177 | sm->wpa_ptk_group_state); | |
1178 | return; | |
1179 | } | |
1180 | break; | |
1181 | #ifdef CONFIG_PEERKEY | |
1182 | case SMK_M1: | |
1183 | case SMK_M3: | |
1184 | case SMK_ERROR: | |
1185 | if (!wpa_auth->conf.peerkey) { | |
1186 | wpa_printf(MSG_DEBUG, "RSN: SMK M1/M3/Error, but " | |
1187 | "PeerKey use disabled - ignoring message"); | |
1188 | return; | |
1189 | } | |
1190 | if (!sm->PTK_valid) { | |
1191 | wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO, | |
1192 | "received EAPOL-Key msg SMK in " | |
1193 | "invalid state - dropped"); | |
1194 | return; | |
1195 | } | |
1196 | break; | |
1197 | #else /* CONFIG_PEERKEY */ | |
1198 | case SMK_M1: | |
1199 | case SMK_M3: | |
1200 | case SMK_ERROR: | |
1201 | return; /* STSL disabled - ignore SMK messages */ | |
1202 | #endif /* CONFIG_PEERKEY */ | |
1203 | case REQUEST: | |
1204 | break; | |
1205 | } | |
1206 | ||
1207 | wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_DEBUG, | |
1208 | "received EAPOL-Key frame (%s)", msgtxt); | |
1209 | ||
1210 | if (key_info & WPA_KEY_INFO_ACK) { | |
1211 | wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO, | |
1212 | "received invalid EAPOL-Key: Key Ack set"); | |
1213 | return; | |
1214 | } | |
1215 | ||
b9866483 JM |
1216 | if (!wpa_key_mgmt_fils(sm->wpa_key_mgmt) && |
1217 | !(key_info & WPA_KEY_INFO_MIC)) { | |
6fc6879b JM |
1218 | wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO, |
1219 | "received invalid EAPOL-Key: Key MIC not set"); | |
1220 | return; | |
1221 | } | |
1222 | ||
b9866483 JM |
1223 | #ifdef CONFIG_FILS |
1224 | if (wpa_key_mgmt_fils(sm->wpa_key_mgmt) && | |
1225 | (key_info & WPA_KEY_INFO_MIC)) { | |
1226 | wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO, | |
1227 | "received invalid EAPOL-Key: Key MIC set"); | |
1228 | return; | |
1229 | } | |
1230 | #endif /* CONFIG_FILS */ | |
1231 | ||
6fc6879b | 1232 | sm->MICVerified = FALSE; |
68921e24 | 1233 | if (sm->PTK_valid && !sm->update_snonce) { |
3b5b7aa8 JM |
1234 | if (mic_len && |
1235 | wpa_verify_key_mic(sm->wpa_key_mgmt, &sm->PTK, data, | |
f23c5b17 JM |
1236 | data_len) && |
1237 | (msg != PAIRWISE_4 || !sm->alt_snonce_valid || | |
1238 | wpa_try_alt_snonce(sm, data, data_len))) { | |
6fc6879b JM |
1239 | wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO, |
1240 | "received EAPOL-Key with invalid MIC"); | |
1241 | return; | |
1242 | } | |
75c8563e JM |
1243 | #ifdef CONFIG_FILS |
1244 | if (!mic_len && | |
1245 | wpa_aead_decrypt(sm, &sm->PTK, data, data_len, | |
1246 | &key_data_length) < 0) { | |
1247 | wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO, | |
1248 | "received EAPOL-Key with invalid MIC"); | |
1249 | return; | |
1250 | } | |
1251 | #endif /* CONFIG_FILS */ | |
6fc6879b JM |
1252 | sm->MICVerified = TRUE; |
1253 | eloop_cancel_timeout(wpa_send_eapol_timeout, wpa_auth, sm); | |
e4bf4db9 | 1254 | sm->pending_1_of_4_timeout = 0; |
6fc6879b JM |
1255 | } |
1256 | ||
1257 | if (key_info & WPA_KEY_INFO_REQUEST) { | |
1258 | if (sm->MICVerified) { | |
1259 | sm->req_replay_counter_used = 1; | |
1260 | os_memcpy(sm->req_replay_counter, key->replay_counter, | |
1261 | WPA_REPLAY_COUNTER_LEN); | |
1262 | } else { | |
1263 | wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO, | |
1264 | "received EAPOL-Key request with " | |
1265 | "invalid MIC"); | |
1266 | return; | |
1267 | } | |
1268 | ||
1269 | /* | |
1270 | * TODO: should decrypt key data field if encryption was used; | |
1271 | * even though MAC address KDE is not normally encrypted, | |
1272 | * supplicant is allowed to encrypt it. | |
1273 | */ | |
1274 | if (msg == SMK_ERROR) { | |
1275 | #ifdef CONFIG_PEERKEY | |
5e3b5197 | 1276 | wpa_smk_error(wpa_auth, sm, key_data, key_data_length); |
6fc6879b JM |
1277 | #endif /* CONFIG_PEERKEY */ |
1278 | return; | |
1279 | } else if (key_info & WPA_KEY_INFO_ERROR) { | |
c772d054 JM |
1280 | if (wpa_receive_error_report( |
1281 | wpa_auth, sm, | |
1282 | !(key_info & WPA_KEY_INFO_KEY_TYPE)) > 0) | |
1283 | return; /* STA entry was removed */ | |
6fc6879b JM |
1284 | } else if (key_info & WPA_KEY_INFO_KEY_TYPE) { |
1285 | wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO, | |
1286 | "received EAPOL-Key Request for new " | |
1287 | "4-Way Handshake"); | |
1288 | wpa_request_new_ptk(sm); | |
1289 | #ifdef CONFIG_PEERKEY | |
1290 | } else if (msg == SMK_M1) { | |
5e3b5197 | 1291 | wpa_smk_m1(wpa_auth, sm, key, key_data, |
5c6787a6 | 1292 | key_data_length); |
6fc6879b JM |
1293 | #endif /* CONFIG_PEERKEY */ |
1294 | } else if (key_data_length > 0 && | |
5e3b5197 JM |
1295 | wpa_parse_kde_ies(key_data, key_data_length, |
1296 | &kde) == 0 && | |
6fc6879b JM |
1297 | kde.mac_addr) { |
1298 | } else { | |
1299 | wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO, | |
1300 | "received EAPOL-Key Request for GTK " | |
1301 | "rekeying"); | |
6fc6879b JM |
1302 | eloop_cancel_timeout(wpa_rekey_gtk, wpa_auth, NULL); |
1303 | wpa_rekey_gtk(wpa_auth, NULL); | |
1304 | } | |
1305 | } else { | |
68921e24 JM |
1306 | /* Do not allow the same key replay counter to be reused. */ |
1307 | wpa_replay_counter_mark_invalid(sm->key_replay, | |
1308 | key->replay_counter); | |
1309 | ||
1310 | if (msg == PAIRWISE_2) { | |
1311 | /* | |
1312 | * Maintain a copy of the pending EAPOL-Key frames in | |
1313 | * case the EAPOL-Key frame was retransmitted. This is | |
1314 | * needed to allow EAPOL-Key msg 2/4 reply to another | |
1315 | * pending msg 1/4 to update the SNonce to work around | |
1316 | * unexpected supplicant behavior. | |
1317 | */ | |
1318 | os_memcpy(sm->prev_key_replay, sm->key_replay, | |
1319 | sizeof(sm->key_replay)); | |
1320 | } else { | |
1321 | os_memset(sm->prev_key_replay, 0, | |
1322 | sizeof(sm->prev_key_replay)); | |
1323 | } | |
1324 | ||
1325 | /* | |
1326 | * Make sure old valid counters are not accepted anymore and | |
1327 | * do not get copied again. | |
1328 | */ | |
1329 | wpa_replay_counter_mark_invalid(sm->key_replay, NULL); | |
6fc6879b JM |
1330 | } |
1331 | ||
1332 | #ifdef CONFIG_PEERKEY | |
1333 | if (msg == SMK_M3) { | |
5e3b5197 | 1334 | wpa_smk_m3(wpa_auth, sm, key, key_data, key_data_length); |
6fc6879b JM |
1335 | return; |
1336 | } | |
1337 | #endif /* CONFIG_PEERKEY */ | |
1338 | ||
1339 | os_free(sm->last_rx_eapol_key); | |
a1f11e34 | 1340 | sm->last_rx_eapol_key = os_memdup(data, data_len); |
6fc6879b JM |
1341 | if (sm->last_rx_eapol_key == NULL) |
1342 | return; | |
6fc6879b JM |
1343 | sm->last_rx_eapol_key_len = data_len; |
1344 | ||
0d442aff | 1345 | sm->rx_eapol_key_secure = !!(key_info & WPA_KEY_INFO_SECURE); |
6fc6879b JM |
1346 | sm->EAPOLKeyReceived = TRUE; |
1347 | sm->EAPOLKeyPairwise = !!(key_info & WPA_KEY_INFO_KEY_TYPE); | |
1348 | sm->EAPOLKeyRequest = !!(key_info & WPA_KEY_INFO_REQUEST); | |
1349 | os_memcpy(sm->SNonce, key->key_nonce, WPA_NONCE_LEN); | |
1350 | wpa_sm_step(sm); | |
1351 | } | |
1352 | ||
1353 | ||
3c7302c2 JM |
1354 | static int wpa_gmk_to_gtk(const u8 *gmk, const char *label, const u8 *addr, |
1355 | const u8 *gnonce, u8 *gtk, size_t gtk_len) | |
6fc6879b | 1356 | { |
5cb9d5c3 JM |
1357 | u8 data[ETH_ALEN + WPA_NONCE_LEN + 8 + 16]; |
1358 | u8 *pos; | |
3c7302c2 | 1359 | int ret = 0; |
6fc6879b | 1360 | |
5cb9d5c3 JM |
1361 | /* GTK = PRF-X(GMK, "Group key expansion", |
1362 | * AA || GNonce || Time || random data) | |
1363 | * The example described in the IEEE 802.11 standard uses only AA and | |
1364 | * GNonce as inputs here. Add some more entropy since this derivation | |
1365 | * is done only at the Authenticator and as such, does not need to be | |
1366 | * exactly same. | |
1367 | */ | |
6fc6879b JM |
1368 | os_memcpy(data, addr, ETH_ALEN); |
1369 | os_memcpy(data + ETH_ALEN, gnonce, WPA_NONCE_LEN); | |
5cb9d5c3 JM |
1370 | pos = data + ETH_ALEN + WPA_NONCE_LEN; |
1371 | wpa_get_ntp_timestamp(pos); | |
1372 | pos += 8; | |
3642c431 | 1373 | if (random_get_bytes(pos, 16) < 0) |
3c7302c2 | 1374 | ret = -1; |
6fc6879b | 1375 | |
56586197 | 1376 | #ifdef CONFIG_IEEE80211W |
5cb9d5c3 | 1377 | sha256_prf(gmk, WPA_GMK_LEN, label, data, sizeof(data), gtk, gtk_len); |
56586197 | 1378 | #else /* CONFIG_IEEE80211W */ |
3c7302c2 JM |
1379 | if (sha1_prf(gmk, WPA_GMK_LEN, label, data, sizeof(data), gtk, gtk_len) |
1380 | < 0) | |
1381 | ret = -1; | |
56586197 | 1382 | #endif /* CONFIG_IEEE80211W */ |
3c7302c2 JM |
1383 | |
1384 | return ret; | |
6fc6879b JM |
1385 | } |
1386 | ||
1387 | ||
1388 | static void wpa_send_eapol_timeout(void *eloop_ctx, void *timeout_ctx) | |
1389 | { | |
1390 | struct wpa_authenticator *wpa_auth = eloop_ctx; | |
1391 | struct wpa_state_machine *sm = timeout_ctx; | |
1392 | ||
e4bf4db9 | 1393 | sm->pending_1_of_4_timeout = 0; |
6fc6879b JM |
1394 | wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG, "EAPOL-Key timeout"); |
1395 | sm->TimeoutEvt = TRUE; | |
1396 | wpa_sm_step(sm); | |
1397 | } | |
1398 | ||
1399 | ||
1400 | void __wpa_send_eapol(struct wpa_authenticator *wpa_auth, | |
1401 | struct wpa_state_machine *sm, int key_info, | |
1402 | const u8 *key_rsc, const u8 *nonce, | |
1403 | const u8 *kde, size_t kde_len, | |
1404 | int keyidx, int encr, int force_version) | |
1405 | { | |
1406 | struct ieee802_1x_hdr *hdr; | |
1407 | struct wpa_eapol_key *key; | |
5e3b5197 | 1408 | size_t len, mic_len, keyhdrlen; |
6fc6879b JM |
1409 | int alg; |
1410 | int key_data_len, pad_len = 0; | |
1411 | u8 *buf, *pos; | |
1412 | int version, pairwise; | |
22a299ee | 1413 | int i; |
6d014ffc | 1414 | u8 *key_mic, *key_data; |
6fc6879b | 1415 | |
5e3b5197 | 1416 | mic_len = wpa_mic_len(sm->wpa_key_mgmt); |
6d014ffc | 1417 | keyhdrlen = sizeof(*key) + mic_len + 2; |
5e3b5197 JM |
1418 | |
1419 | len = sizeof(struct ieee802_1x_hdr) + keyhdrlen; | |
6fc6879b JM |
1420 | |
1421 | if (force_version) | |
1422 | version = force_version; | |
929a2ea5 | 1423 | else if (sm->wpa_key_mgmt == WPA_KEY_MGMT_OSEN || |
07a5fe82 | 1424 | sm->wpa_key_mgmt == WPA_KEY_MGMT_OWE || |
36a50fd4 JM |
1425 | wpa_key_mgmt_suite_b(sm->wpa_key_mgmt) || |
1426 | wpa_key_mgmt_fils(sm->wpa_key_mgmt)) | |
a14896e8 | 1427 | version = WPA_KEY_INFO_TYPE_AKM_DEFINED; |
6fc6879b JM |
1428 | else if (wpa_use_aes_cmac(sm)) |
1429 | version = WPA_KEY_INFO_TYPE_AES_128_CMAC; | |
eb7719ff | 1430 | else if (sm->pairwise != WPA_CIPHER_TKIP) |
6fc6879b JM |
1431 | version = WPA_KEY_INFO_TYPE_HMAC_SHA1_AES; |
1432 | else | |
1433 | version = WPA_KEY_INFO_TYPE_HMAC_MD5_RC4; | |
1434 | ||
8543ed8a | 1435 | pairwise = !!(key_info & WPA_KEY_INFO_KEY_TYPE); |
6fc6879b JM |
1436 | |
1437 | wpa_printf(MSG_DEBUG, "WPA: Send EAPOL(version=%d secure=%d mic=%d " | |
1438 | "ack=%d install=%d pairwise=%d kde_len=%lu keyidx=%d " | |
1439 | "encr=%d)", | |
1440 | version, | |
1441 | (key_info & WPA_KEY_INFO_SECURE) ? 1 : 0, | |
1442 | (key_info & WPA_KEY_INFO_MIC) ? 1 : 0, | |
1443 | (key_info & WPA_KEY_INFO_ACK) ? 1 : 0, | |
1444 | (key_info & WPA_KEY_INFO_INSTALL) ? 1 : 0, | |
1445 | pairwise, (unsigned long) kde_len, keyidx, encr); | |
1446 | ||
1447 | key_data_len = kde_len; | |
1448 | ||
1449 | if ((version == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES || | |
07a5fe82 | 1450 | sm->wpa_key_mgmt == WPA_KEY_MGMT_OWE || |
a14896e8 | 1451 | sm->wpa_key_mgmt == WPA_KEY_MGMT_OSEN || |
929a2ea5 | 1452 | wpa_key_mgmt_suite_b(sm->wpa_key_mgmt) || |
6fc6879b JM |
1453 | version == WPA_KEY_INFO_TYPE_AES_128_CMAC) && encr) { |
1454 | pad_len = key_data_len % 8; | |
1455 | if (pad_len) | |
1456 | pad_len = 8 - pad_len; | |
1457 | key_data_len += pad_len + 8; | |
1458 | } | |
1459 | ||
1460 | len += key_data_len; | |
b729fd8d JM |
1461 | if (!mic_len && encr) |
1462 | len += AES_BLOCK_SIZE; | |
6fc6879b JM |
1463 | |
1464 | hdr = os_zalloc(len); | |
1465 | if (hdr == NULL) | |
1466 | return; | |
1467 | hdr->version = wpa_auth->conf.eapol_version; | |
1468 | hdr->type = IEEE802_1X_TYPE_EAPOL_KEY; | |
1469 | hdr->length = host_to_be16(len - sizeof(*hdr)); | |
1470 | key = (struct wpa_eapol_key *) (hdr + 1); | |
6d014ffc | 1471 | key_mic = (u8 *) (key + 1); |
5e3b5197 | 1472 | key_data = ((u8 *) (hdr + 1)) + keyhdrlen; |
6fc6879b JM |
1473 | |
1474 | key->type = sm->wpa == WPA_VERSION_WPA2 ? | |
1475 | EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA; | |
1476 | key_info |= version; | |
1477 | if (encr && sm->wpa == WPA_VERSION_WPA2) | |
1478 | key_info |= WPA_KEY_INFO_ENCR_KEY_DATA; | |
1479 | if (sm->wpa != WPA_VERSION_WPA2) | |
1480 | key_info |= keyidx << WPA_KEY_INFO_KEY_INDEX_SHIFT; | |
1481 | WPA_PUT_BE16(key->key_info, key_info); | |
1482 | ||
1483 | alg = pairwise ? sm->pairwise : wpa_auth->conf.wpa_group; | |
caab23f1 JM |
1484 | if ((key_info & WPA_KEY_INFO_SMK_MESSAGE) || |
1485 | (sm->wpa == WPA_VERSION_WPA2 && !pairwise)) | |
6fc6879b | 1486 | WPA_PUT_BE16(key->key_length, 0); |
caab23f1 JM |
1487 | else |
1488 | WPA_PUT_BE16(key->key_length, wpa_cipher_key_len(alg)); | |
6fc6879b JM |
1489 | |
1490 | /* FIX: STSL: what to use as key_replay_counter? */ | |
22a299ee JM |
1491 | for (i = RSNA_MAX_EAPOL_RETRIES - 1; i > 0; i--) { |
1492 | sm->key_replay[i].valid = sm->key_replay[i - 1].valid; | |
1493 | os_memcpy(sm->key_replay[i].counter, | |
1494 | sm->key_replay[i - 1].counter, | |
1495 | WPA_REPLAY_COUNTER_LEN); | |
1496 | } | |
1497 | inc_byte_array(sm->key_replay[0].counter, WPA_REPLAY_COUNTER_LEN); | |
1498 | os_memcpy(key->replay_counter, sm->key_replay[0].counter, | |
6fc6879b | 1499 | WPA_REPLAY_COUNTER_LEN); |
db099951 JM |
1500 | wpa_hexdump(MSG_DEBUG, "WPA: Replay Counter", |
1501 | key->replay_counter, WPA_REPLAY_COUNTER_LEN); | |
22a299ee | 1502 | sm->key_replay[0].valid = TRUE; |
6fc6879b JM |
1503 | |
1504 | if (nonce) | |
1505 | os_memcpy(key->key_nonce, nonce, WPA_NONCE_LEN); | |
1506 | ||
1507 | if (key_rsc) | |
1508 | os_memcpy(key->key_rsc, key_rsc, WPA_KEY_RSC_LEN); | |
1509 | ||
1510 | if (kde && !encr) { | |
5e3b5197 | 1511 | os_memcpy(key_data, kde, kde_len); |
6d014ffc | 1512 | WPA_PUT_BE16(key_mic + mic_len, kde_len); |
b729fd8d JM |
1513 | #ifdef CONFIG_FILS |
1514 | } else if (!mic_len) { | |
1515 | const u8 *aad[1]; | |
1516 | size_t aad_len[1]; | |
1517 | ||
1518 | WPA_PUT_BE16(key_mic, AES_BLOCK_SIZE + kde_len); | |
1519 | wpa_hexdump_key(MSG_DEBUG, "Plaintext EAPOL-Key Key Data", | |
1520 | kde, kde_len); | |
1521 | ||
1522 | wpa_hexdump_key(MSG_DEBUG, "WPA: KEK", | |
1523 | sm->PTK.kek, sm->PTK.kek_len); | |
1524 | /* AES-SIV AAD from EAPOL protocol version field (inclusive) to | |
1525 | * to Key Data (exclusive). */ | |
1526 | aad[0] = (u8 *) hdr; | |
1527 | aad_len[0] = key_mic + 2 - (u8 *) hdr; | |
1528 | if (aes_siv_encrypt(sm->PTK.kek, sm->PTK.kek_len, kde, kde_len, | |
1529 | 1, aad, aad_len, key_mic + 2) < 0) { | |
1530 | wpa_printf(MSG_DEBUG, "WPA: AES-SIV encryption failed"); | |
1531 | return; | |
1532 | } | |
1533 | ||
1534 | wpa_hexdump(MSG_DEBUG, "WPA: Encrypted Key Data from SIV", | |
1535 | key_mic + 2, AES_BLOCK_SIZE + kde_len); | |
1536 | #endif /* CONFIG_FILS */ | |
6fc6879b JM |
1537 | } else if (encr && kde) { |
1538 | buf = os_zalloc(key_data_len); | |
1539 | if (buf == NULL) { | |
1540 | os_free(hdr); | |
1541 | return; | |
1542 | } | |
1543 | pos = buf; | |
1544 | os_memcpy(pos, kde, kde_len); | |
1545 | pos += kde_len; | |
1546 | ||
1547 | if (pad_len) | |
1548 | *pos++ = 0xdd; | |
1549 | ||
1550 | wpa_hexdump_key(MSG_DEBUG, "Plaintext EAPOL-Key Key Data", | |
1551 | buf, key_data_len); | |
1552 | if (version == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES || | |
07a5fe82 | 1553 | sm->wpa_key_mgmt == WPA_KEY_MGMT_OWE || |
a14896e8 | 1554 | sm->wpa_key_mgmt == WPA_KEY_MGMT_OSEN || |
929a2ea5 | 1555 | wpa_key_mgmt_suite_b(sm->wpa_key_mgmt) || |
6fc6879b | 1556 | version == WPA_KEY_INFO_TYPE_AES_128_CMAC) { |
ef9627cb JM |
1557 | wpa_printf(MSG_DEBUG, |
1558 | "WPA: Encrypt Key Data using AES-WRAP (KEK length %u)", | |
1559 | (unsigned int) sm->PTK.kek_len); | |
98cd3d1c | 1560 | if (aes_wrap(sm->PTK.kek, sm->PTK.kek_len, |
5e3b5197 | 1561 | (key_data_len - 8) / 8, buf, key_data)) { |
6fc6879b JM |
1562 | os_free(hdr); |
1563 | os_free(buf); | |
1564 | return; | |
1565 | } | |
6d014ffc | 1566 | WPA_PUT_BE16(key_mic + mic_len, key_data_len); |
7cb53ded | 1567 | #ifndef CONFIG_NO_RC4 |
98cd3d1c | 1568 | } else if (sm->PTK.kek_len == 16) { |
6fc6879b | 1569 | u8 ek[32]; |
ef9627cb JM |
1570 | |
1571 | wpa_printf(MSG_DEBUG, | |
1572 | "WPA: Encrypt Key Data using RC4"); | |
6fc6879b JM |
1573 | os_memcpy(key->key_iv, |
1574 | sm->group->Counter + WPA_NONCE_LEN - 16, 16); | |
1575 | inc_byte_array(sm->group->Counter, WPA_NONCE_LEN); | |
1576 | os_memcpy(ek, key->key_iv, 16); | |
98cd3d1c | 1577 | os_memcpy(ek + 16, sm->PTK.kek, sm->PTK.kek_len); |
5e3b5197 JM |
1578 | os_memcpy(key_data, buf, key_data_len); |
1579 | rc4_skip(ek, 32, 256, key_data, key_data_len); | |
6d014ffc | 1580 | WPA_PUT_BE16(key_mic + mic_len, key_data_len); |
7cb53ded | 1581 | #endif /* CONFIG_NO_RC4 */ |
98cd3d1c JM |
1582 | } else { |
1583 | os_free(hdr); | |
1584 | os_free(buf); | |
1585 | return; | |
6fc6879b JM |
1586 | } |
1587 | os_free(buf); | |
1588 | } | |
1589 | ||
1590 | if (key_info & WPA_KEY_INFO_MIC) { | |
b729fd8d | 1591 | if (!sm->PTK_valid || !mic_len) { |
6fc6879b JM |
1592 | wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG, |
1593 | "PTK not valid when sending EAPOL-Key " | |
1594 | "frame"); | |
1595 | os_free(hdr); | |
1596 | return; | |
1597 | } | |
5e3b5197 | 1598 | |
98cd3d1c JM |
1599 | wpa_eapol_key_mic(sm->PTK.kck, sm->PTK.kck_len, |
1600 | sm->wpa_key_mgmt, version, | |
5e3b5197 | 1601 | (u8 *) hdr, len, key_mic); |
7af092a0 JB |
1602 | #ifdef CONFIG_TESTING_OPTIONS |
1603 | if (!pairwise && | |
06df2aa6 | 1604 | wpa_auth->conf.corrupt_gtk_rekey_mic_probability > 0.0 && |
7af092a0 JB |
1605 | drand48() < |
1606 | wpa_auth->conf.corrupt_gtk_rekey_mic_probability) { | |
1607 | wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO, | |
1608 | "Corrupting group EAPOL-Key Key MIC"); | |
5e3b5197 | 1609 | key_mic[0]++; |
7af092a0 JB |
1610 | } |
1611 | #endif /* CONFIG_TESTING_OPTIONS */ | |
6fc6879b JM |
1612 | } |
1613 | ||
1614 | wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_inc_EapolFramesTx, | |
1615 | 1); | |
1616 | wpa_auth_send_eapol(wpa_auth, sm->addr, (u8 *) hdr, len, | |
1617 | sm->pairwise_set); | |
1618 | os_free(hdr); | |
1619 | } | |
1620 | ||
1621 | ||
1622 | static void wpa_send_eapol(struct wpa_authenticator *wpa_auth, | |
1623 | struct wpa_state_machine *sm, int key_info, | |
1624 | const u8 *key_rsc, const u8 *nonce, | |
1625 | const u8 *kde, size_t kde_len, | |
1626 | int keyidx, int encr) | |
1627 | { | |
1628 | int timeout_ms; | |
1629 | int pairwise = key_info & WPA_KEY_INFO_KEY_TYPE; | |
41f140d3 | 1630 | u32 ctr; |
6fc6879b JM |
1631 | |
1632 | if (sm == NULL) | |
1633 | return; | |
1634 | ||
1635 | __wpa_send_eapol(wpa_auth, sm, key_info, key_rsc, nonce, kde, kde_len, | |
1636 | keyidx, encr, 0); | |
1637 | ||
bae61562 | 1638 | ctr = pairwise ? sm->TimeoutCtr : sm->GTimeoutCtr; |
e4bf4db9 | 1639 | if (ctr == 1 && wpa_auth->conf.tx_status) |
00338a4f JM |
1640 | timeout_ms = pairwise ? eapol_key_timeout_first : |
1641 | eapol_key_timeout_first_group; | |
bae61562 JM |
1642 | else |
1643 | timeout_ms = eapol_key_timeout_subseq; | |
e4bf4db9 JM |
1644 | if (pairwise && ctr == 1 && !(key_info & WPA_KEY_INFO_MIC)) |
1645 | sm->pending_1_of_4_timeout = 1; | |
1646 | wpa_printf(MSG_DEBUG, "WPA: Use EAPOL-Key timeout of %u ms (retry " | |
41f140d3 | 1647 | "counter %u)", timeout_ms, ctr); |
6fc6879b JM |
1648 | eloop_register_timeout(timeout_ms / 1000, (timeout_ms % 1000) * 1000, |
1649 | wpa_send_eapol_timeout, wpa_auth, sm); | |
1650 | } | |
1651 | ||
1652 | ||
929a2ea5 JM |
1653 | static int wpa_verify_key_mic(int akmp, struct wpa_ptk *PTK, u8 *data, |
1654 | size_t data_len) | |
6fc6879b JM |
1655 | { |
1656 | struct ieee802_1x_hdr *hdr; | |
1657 | struct wpa_eapol_key *key; | |
1658 | u16 key_info; | |
1659 | int ret = 0; | |
6d014ffc | 1660 | u8 mic[WPA_EAPOL_KEY_MIC_MAX_LEN], *mic_pos; |
5e3b5197 | 1661 | size_t mic_len = wpa_mic_len(akmp); |
6fc6879b JM |
1662 | |
1663 | if (data_len < sizeof(*hdr) + sizeof(*key)) | |
1664 | return -1; | |
1665 | ||
1666 | hdr = (struct ieee802_1x_hdr *) data; | |
1667 | key = (struct wpa_eapol_key *) (hdr + 1); | |
6d014ffc | 1668 | mic_pos = (u8 *) (key + 1); |
6fc6879b | 1669 | key_info = WPA_GET_BE16(key->key_info); |
6d014ffc JM |
1670 | os_memcpy(mic, mic_pos, mic_len); |
1671 | os_memset(mic_pos, 0, mic_len); | |
98cd3d1c JM |
1672 | if (wpa_eapol_key_mic(PTK->kck, PTK->kck_len, akmp, |
1673 | key_info & WPA_KEY_INFO_TYPE_MASK, | |
6d014ffc JM |
1674 | data, data_len, mic_pos) || |
1675 | os_memcmp_const(mic, mic_pos, mic_len) != 0) | |
6fc6879b | 1676 | ret = -1; |
6d014ffc | 1677 | os_memcpy(mic_pos, mic, mic_len); |
6fc6879b JM |
1678 | return ret; |
1679 | } | |
1680 | ||
1681 | ||
1682 | void wpa_remove_ptk(struct wpa_state_machine *sm) | |
1683 | { | |
1684 | sm->PTK_valid = FALSE; | |
1685 | os_memset(&sm->PTK, 0, sizeof(sm->PTK)); | |
2a0b86d3 JM |
1686 | if (wpa_auth_set_key(sm->wpa_auth, 0, WPA_ALG_NONE, sm->addr, 0, NULL, |
1687 | 0)) | |
1688 | wpa_printf(MSG_DEBUG, | |
1689 | "RSN: PTK removal from the driver failed"); | |
6fc6879b | 1690 | sm->pairwise_set = FALSE; |
581a8cde | 1691 | eloop_cancel_timeout(wpa_rekey_ptk, sm->wpa_auth, sm); |
6fc6879b JM |
1692 | } |
1693 | ||
1694 | ||
bb119228 | 1695 | int wpa_auth_sm_event(struct wpa_state_machine *sm, enum wpa_event event) |
6fc6879b | 1696 | { |
5d22a1d5 JM |
1697 | int remove_ptk = 1; |
1698 | ||
6fc6879b | 1699 | if (sm == NULL) |
6f9b5d16 | 1700 | return -1; |
6fc6879b JM |
1701 | |
1702 | wpa_auth_vlogger(sm->wpa_auth, sm->addr, LOGGER_DEBUG, | |
1703 | "event %d notification", event); | |
1704 | ||
1705 | switch (event) { | |
1706 | case WPA_AUTH: | |
c50d94f1 BC |
1707 | #ifdef CONFIG_MESH |
1708 | /* PTKs are derived through AMPE */ | |
1709 | if (wpa_auth_start_ampe(sm->wpa_auth, sm->addr)) { | |
1710 | /* not mesh */ | |
1711 | break; | |
1712 | } | |
1713 | return 0; | |
1714 | #endif /* CONFIG_MESH */ | |
6fc6879b JM |
1715 | case WPA_ASSOC: |
1716 | break; | |
1717 | case WPA_DEAUTH: | |
1718 | case WPA_DISASSOC: | |
1719 | sm->DeauthenticationRequest = TRUE; | |
1720 | break; | |
1721 | case WPA_REAUTH: | |
1722 | case WPA_REAUTH_EAPOL: | |
3ab72b62 JM |
1723 | if (!sm->started) { |
1724 | /* | |
1725 | * When using WPS, we may end up here if the STA | |
1726 | * manages to re-associate without the previous STA | |
1727 | * entry getting removed. Consequently, we need to make | |
1728 | * sure that the WPA state machines gets initialized | |
1729 | * properly at this point. | |
1730 | */ | |
1731 | wpa_printf(MSG_DEBUG, "WPA state machine had not been " | |
1732 | "started - initialize now"); | |
1733 | sm->started = 1; | |
1734 | sm->Init = TRUE; | |
1735 | if (wpa_sm_step(sm) == 1) | |
1736 | return 1; /* should not really happen */ | |
1737 | sm->Init = FALSE; | |
1738 | sm->AuthenticationRequest = TRUE; | |
1739 | break; | |
1740 | } | |
9663596f JM |
1741 | if (sm->GUpdateStationKeys) { |
1742 | /* | |
1743 | * Reauthentication cancels the pending group key | |
1744 | * update for this STA. | |
1745 | */ | |
1746 | sm->group->GKeyDoneStations--; | |
1747 | sm->GUpdateStationKeys = FALSE; | |
1748 | sm->PtkGroupInit = TRUE; | |
1749 | } | |
6fc6879b JM |
1750 | sm->ReAuthenticationRequest = TRUE; |
1751 | break; | |
1752 | case WPA_ASSOC_FT: | |
4ec1fd8e | 1753 | #ifdef CONFIG_IEEE80211R_AP |
81a658d7 JM |
1754 | wpa_printf(MSG_DEBUG, "FT: Retry PTK configuration " |
1755 | "after association"); | |
1756 | wpa_ft_install_ptk(sm); | |
1757 | ||
6fc6879b JM |
1758 | /* Using FT protocol, not WPA auth state machine */ |
1759 | sm->ft_completed = 1; | |
6f9b5d16 | 1760 | return 0; |
4ec1fd8e | 1761 | #else /* CONFIG_IEEE80211R_AP */ |
6fc6879b | 1762 | break; |
4ec1fd8e | 1763 | #endif /* CONFIG_IEEE80211R_AP */ |
957bff83 JM |
1764 | case WPA_ASSOC_FILS: |
1765 | #ifdef CONFIG_FILS | |
1766 | wpa_printf(MSG_DEBUG, | |
1767 | "FILS: TK configuration after association"); | |
1768 | fils_set_tk(sm); | |
1769 | sm->fils_completed = 1; | |
1770 | return 0; | |
1771 | #else /* CONFIG_FILS */ | |
1772 | break; | |
1773 | #endif /* CONFIG_FILS */ | |
6fc6879b JM |
1774 | } |
1775 | ||
4ec1fd8e | 1776 | #ifdef CONFIG_IEEE80211R_AP |
6fc6879b | 1777 | sm->ft_completed = 0; |
4ec1fd8e | 1778 | #endif /* CONFIG_IEEE80211R_AP */ |
6fc6879b | 1779 | |
5d22a1d5 JM |
1780 | #ifdef CONFIG_IEEE80211W |
1781 | if (sm->mgmt_frame_prot && event == WPA_AUTH) | |
1782 | remove_ptk = 0; | |
1783 | #endif /* CONFIG_IEEE80211W */ | |
78815f3d | 1784 | #ifdef CONFIG_FILS |
da24c5aa JM |
1785 | if (wpa_key_mgmt_fils(sm->wpa_key_mgmt) && |
1786 | (event == WPA_AUTH || event == WPA_ASSOC)) | |
78815f3d JM |
1787 | remove_ptk = 0; |
1788 | #endif /* CONFIG_FILS */ | |
6fc6879b | 1789 | |
5d22a1d5 JM |
1790 | if (remove_ptk) { |
1791 | sm->PTK_valid = FALSE; | |
1792 | os_memset(&sm->PTK, 0, sizeof(sm->PTK)); | |
1793 | ||
1794 | if (event != WPA_REAUTH_EAPOL) | |
1795 | wpa_remove_ptk(sm); | |
1796 | } | |
6fc6879b | 1797 | |
43f49c37 JM |
1798 | if (sm->in_step_loop) { |
1799 | /* | |
1800 | * wpa_sm_step() is already running - avoid recursive call to | |
1801 | * it by making the existing loop process the new update. | |
1802 | */ | |
1803 | sm->changed = TRUE; | |
1804 | return 0; | |
1805 | } | |
6f9b5d16 | 1806 | return wpa_sm_step(sm); |
6fc6879b JM |
1807 | } |
1808 | ||
1809 | ||
6fc6879b JM |
1810 | SM_STATE(WPA_PTK, INITIALIZE) |
1811 | { | |
1812 | SM_ENTRY_MA(WPA_PTK, INITIALIZE, wpa_ptk); | |
1813 | if (sm->Init) { | |
1814 | /* Init flag is not cleared here, so avoid busy | |
1815 | * loop by claiming nothing changed. */ | |
1816 | sm->changed = FALSE; | |
1817 | } | |
1818 | ||
1819 | sm->keycount = 0; | |
1820 | if (sm->GUpdateStationKeys) | |
1821 | sm->group->GKeyDoneStations--; | |
1822 | sm->GUpdateStationKeys = FALSE; | |
1823 | if (sm->wpa == WPA_VERSION_WPA) | |
1824 | sm->PInitAKeys = FALSE; | |
1825 | if (1 /* Unicast cipher supported AND (ESS OR ((IBSS or WDS) and | |
1826 | * Local AA > Remote AA)) */) { | |
1827 | sm->Pair = TRUE; | |
1828 | } | |
1829 | wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portEnabled, 0); | |
1830 | wpa_remove_ptk(sm); | |
1831 | wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portValid, 0); | |
1832 | sm->TimeoutCtr = 0; | |
a1ea1b45 JM |
1833 | if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt) || |
1834 | sm->wpa_key_mgmt == WPA_KEY_MGMT_OWE) { | |
6fc6879b JM |
1835 | wpa_auth_set_eapol(sm->wpa_auth, sm->addr, |
1836 | WPA_EAPOL_authorized, 0); | |
1837 | } | |
1838 | } | |
1839 | ||
1840 | ||
1841 | SM_STATE(WPA_PTK, DISCONNECT) | |
1842 | { | |
1843 | SM_ENTRY_MA(WPA_PTK, DISCONNECT, wpa_ptk); | |
1844 | sm->Disconnect = FALSE; | |
1845 | wpa_sta_disconnect(sm->wpa_auth, sm->addr); | |
1846 | } | |
1847 | ||
1848 | ||
1849 | SM_STATE(WPA_PTK, DISCONNECTED) | |
1850 | { | |
1851 | SM_ENTRY_MA(WPA_PTK, DISCONNECTED, wpa_ptk); | |
1852 | sm->DeauthenticationRequest = FALSE; | |
1853 | } | |
1854 | ||
1855 | ||
1856 | SM_STATE(WPA_PTK, AUTHENTICATION) | |
1857 | { | |
1858 | SM_ENTRY_MA(WPA_PTK, AUTHENTICATION, wpa_ptk); | |
1859 | os_memset(&sm->PTK, 0, sizeof(sm->PTK)); | |
1860 | sm->PTK_valid = FALSE; | |
1861 | wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portControl_Auto, | |
1862 | 1); | |
1863 | wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portEnabled, 1); | |
1864 | sm->AuthenticationRequest = FALSE; | |
1865 | } | |
1866 | ||
1867 | ||
40d00d2b NC |
1868 | static void wpa_group_ensure_init(struct wpa_authenticator *wpa_auth, |
1869 | struct wpa_group *group) | |
1bdb7ab3 | 1870 | { |
40d00d2b NC |
1871 | if (group->first_sta_seen) |
1872 | return; | |
1bdb7ab3 JM |
1873 | /* |
1874 | * System has run bit further than at the time hostapd was started | |
1875 | * potentially very early during boot up. This provides better chances | |
1876 | * of collecting more randomness on embedded systems. Re-initialize the | |
1877 | * GMK and Counter here to improve their strength if there was not | |
1878 | * enough entropy available immediately after system startup. | |
1879 | */ | |
1880 | wpa_printf(MSG_DEBUG, "WPA: Re-initialize GMK/Counter on first " | |
1881 | "station"); | |
08704cd8 JM |
1882 | if (random_pool_ready() != 1) { |
1883 | wpa_printf(MSG_INFO, "WPA: Not enough entropy in random pool " | |
1884 | "to proceed - reject first 4-way handshake"); | |
1885 | group->reject_4way_hs_for_entropy = TRUE; | |
40d00d2b NC |
1886 | } else { |
1887 | group->first_sta_seen = TRUE; | |
1888 | group->reject_4way_hs_for_entropy = FALSE; | |
08704cd8 | 1889 | } |
40d00d2b | 1890 | |
aac1efec JM |
1891 | if (wpa_group_init_gmk_and_counter(wpa_auth, group) < 0 || |
1892 | wpa_gtk_update(wpa_auth, group) < 0 || | |
1893 | wpa_group_config_group_keys(wpa_auth, group) < 0) { | |
1894 | wpa_printf(MSG_INFO, "WPA: GMK/GTK setup failed"); | |
1895 | group->first_sta_seen = FALSE; | |
1896 | group->reject_4way_hs_for_entropy = TRUE; | |
1897 | } | |
1bdb7ab3 JM |
1898 | } |
1899 | ||
1900 | ||
6fc6879b JM |
1901 | SM_STATE(WPA_PTK, AUTHENTICATION2) |
1902 | { | |
1903 | SM_ENTRY_MA(WPA_PTK, AUTHENTICATION2, wpa_ptk); | |
1bdb7ab3 | 1904 | |
40d00d2b | 1905 | wpa_group_ensure_init(sm->wpa_auth, sm->group); |
65a32cdb | 1906 | sm->ReAuthenticationRequest = FALSE; |
1bdb7ab3 | 1907 | |
3825a19b JM |
1908 | /* |
1909 | * Definition of ANonce selection in IEEE Std 802.11i-2004 is somewhat | |
1910 | * ambiguous. The Authenticator state machine uses a counter that is | |
1911 | * incremented by one for each 4-way handshake. However, the security | |
1912 | * analysis of 4-way handshake points out that unpredictable nonces | |
1913 | * help in preventing precomputation attacks. Instead of the state | |
1914 | * machine definition, use an unpredictable nonce value here to provide | |
1915 | * stronger protection against potential precomputation attacks. | |
1916 | */ | |
1917 | if (random_get_bytes(sm->ANonce, WPA_NONCE_LEN)) { | |
1918 | wpa_printf(MSG_ERROR, "WPA: Failed to get random data for " | |
1919 | "ANonce."); | |
65a32cdb | 1920 | sm->Disconnect = TRUE; |
3825a19b JM |
1921 | return; |
1922 | } | |
bc8318ac JM |
1923 | wpa_hexdump(MSG_DEBUG, "WPA: Assign ANonce", sm->ANonce, |
1924 | WPA_NONCE_LEN); | |
6fc6879b JM |
1925 | /* IEEE 802.11i does not clear TimeoutCtr here, but this is more |
1926 | * logical place than INITIALIZE since AUTHENTICATION2 can be | |
1927 | * re-entered on ReAuthenticationRequest without going through | |
1928 | * INITIALIZE. */ | |
1929 | sm->TimeoutCtr = 0; | |
1930 | } | |
1931 | ||
1932 | ||
1933 | SM_STATE(WPA_PTK, INITPMK) | |
1934 | { | |
1935 | u8 msk[2 * PMK_LEN]; | |
1936 | size_t len = 2 * PMK_LEN; | |
1937 | ||
1938 | SM_ENTRY_MA(WPA_PTK, INITPMK, wpa_ptk); | |
4ec1fd8e | 1939 | #ifdef CONFIG_IEEE80211R_AP |
6fc6879b | 1940 | sm->xxkey_len = 0; |
4ec1fd8e | 1941 | #endif /* CONFIG_IEEE80211R_AP */ |
6fc6879b JM |
1942 | if (sm->pmksa) { |
1943 | wpa_printf(MSG_DEBUG, "WPA: PMK from PMKSA cache"); | |
207976f0 JM |
1944 | os_memcpy(sm->PMK, sm->pmksa->pmk, sm->pmksa->pmk_len); |
1945 | sm->pmk_len = sm->pmksa->pmk_len; | |
6fc6879b | 1946 | } else if (wpa_auth_get_msk(sm->wpa_auth, sm->addr, msk, &len) == 0) { |
207976f0 JM |
1947 | unsigned int pmk_len; |
1948 | ||
834c5d68 | 1949 | if (wpa_key_mgmt_sha384(sm->wpa_key_mgmt)) |
207976f0 JM |
1950 | pmk_len = PMK_LEN_SUITE_B_192; |
1951 | else | |
1952 | pmk_len = PMK_LEN; | |
6fc6879b | 1953 | wpa_printf(MSG_DEBUG, "WPA: PMK from EAPOL state machine " |
207976f0 JM |
1954 | "(MSK len=%lu PMK len=%u)", (unsigned long) len, |
1955 | pmk_len); | |
1956 | if (len < pmk_len) { | |
1957 | wpa_printf(MSG_DEBUG, | |
1958 | "WPA: MSK not long enough (%u) to create PMK (%u)", | |
1959 | (unsigned int) len, (unsigned int) pmk_len); | |
1960 | sm->Disconnect = TRUE; | |
1961 | return; | |
1962 | } | |
1963 | os_memcpy(sm->PMK, msk, pmk_len); | |
1964 | sm->pmk_len = pmk_len; | |
4ec1fd8e | 1965 | #ifdef CONFIG_IEEE80211R_AP |
6fc6879b JM |
1966 | if (len >= 2 * PMK_LEN) { |
1967 | os_memcpy(sm->xxkey, msk + PMK_LEN, PMK_LEN); | |
1968 | sm->xxkey_len = PMK_LEN; | |
1969 | } | |
4ec1fd8e | 1970 | #endif /* CONFIG_IEEE80211R_AP */ |
6fc6879b | 1971 | } else { |
400de9b1 | 1972 | wpa_printf(MSG_DEBUG, "WPA: Could not get PMK, get_msk: %p", |
cef8fac0 | 1973 | sm->wpa_auth->cb->get_msk); |
1180dd66 JM |
1974 | sm->Disconnect = TRUE; |
1975 | return; | |
6fc6879b | 1976 | } |
40aaa64f | 1977 | os_memset(msk, 0, sizeof(msk)); |
6fc6879b JM |
1978 | |
1979 | sm->req_replay_counter_used = 0; | |
1980 | /* IEEE 802.11i does not set keyRun to FALSE, but not doing this | |
1981 | * will break reauthentication since EAPOL state machines may not be | |
1982 | * get into AUTHENTICATING state that clears keyRun before WPA state | |
1983 | * machine enters AUTHENTICATION2 state and goes immediately to INITPMK | |
1984 | * state and takes PMK from the previously used AAA Key. This will | |
1985 | * eventually fail in 4-Way Handshake because Supplicant uses PMK | |
1986 | * derived from the new AAA Key. Setting keyRun = FALSE here seems to | |
1987 | * be good workaround for this issue. */ | |
1988 | wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_keyRun, 0); | |
1989 | } | |
1990 | ||
1991 | ||
1992 | SM_STATE(WPA_PTK, INITPSK) | |
1993 | { | |
1994 | const u8 *psk; | |
1995 | SM_ENTRY_MA(WPA_PTK, INITPSK, wpa_ptk); | |
759fd76b | 1996 | psk = wpa_auth_get_psk(sm->wpa_auth, sm->addr, sm->p2p_dev_addr, NULL); |
6fc6879b JM |
1997 | if (psk) { |
1998 | os_memcpy(sm->PMK, psk, PMK_LEN); | |
207976f0 | 1999 | sm->pmk_len = PMK_LEN; |
4ec1fd8e | 2000 | #ifdef CONFIG_IEEE80211R_AP |
6fc6879b JM |
2001 | os_memcpy(sm->xxkey, psk, PMK_LEN); |
2002 | sm->xxkey_len = PMK_LEN; | |
4ec1fd8e | 2003 | #endif /* CONFIG_IEEE80211R_AP */ |
6fc6879b JM |
2004 | } |
2005 | sm->req_replay_counter_used = 0; | |
2006 | } | |
2007 | ||
2008 | ||
2009 | SM_STATE(WPA_PTK, PTKSTART) | |
2010 | { | |
2011 | u8 buf[2 + RSN_SELECTOR_LEN + PMKID_LEN], *pmkid = NULL; | |
2012 | size_t pmkid_len = 0; | |
2013 | ||
2014 | SM_ENTRY_MA(WPA_PTK, PTKSTART, wpa_ptk); | |
2015 | sm->PTKRequest = FALSE; | |
2016 | sm->TimeoutEvt = FALSE; | |
f23c5b17 | 2017 | sm->alt_snonce_valid = FALSE; |
bae61562 JM |
2018 | |
2019 | sm->TimeoutCtr++; | |
41f140d3 | 2020 | if (sm->TimeoutCtr > sm->wpa_auth->conf.wpa_pairwise_update_count) { |
bae61562 JM |
2021 | /* No point in sending the EAPOL-Key - we will disconnect |
2022 | * immediately following this. */ | |
2023 | return; | |
2024 | } | |
2025 | ||
6fc6879b JM |
2026 | wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG, |
2027 | "sending 1/4 msg of 4-Way Handshake"); | |
2028 | /* | |
2029 | * TODO: Could add PMKID even with WPA2-PSK, but only if there is only | |
2030 | * one possible PSK for this STA. | |
2031 | */ | |
2032 | if (sm->wpa == WPA_VERSION_WPA2 && | |
a14896e8 JM |
2033 | wpa_key_mgmt_wpa_ieee8021x(sm->wpa_key_mgmt) && |
2034 | sm->wpa_key_mgmt != WPA_KEY_MGMT_OSEN) { | |
6fc6879b JM |
2035 | pmkid = buf; |
2036 | pmkid_len = 2 + RSN_SELECTOR_LEN + PMKID_LEN; | |
2037 | pmkid[0] = WLAN_EID_VENDOR_SPECIFIC; | |
2038 | pmkid[1] = RSN_SELECTOR_LEN + PMKID_LEN; | |
2039 | RSN_SELECTOR_PUT(&pmkid[2], RSN_KEY_DATA_PMKID); | |
929a2ea5 | 2040 | if (sm->pmksa) { |
6fc6879b JM |
2041 | os_memcpy(&pmkid[2 + RSN_SELECTOR_LEN], |
2042 | sm->pmksa->pmkid, PMKID_LEN); | |
929a2ea5 JM |
2043 | } else if (wpa_key_mgmt_suite_b(sm->wpa_key_mgmt)) { |
2044 | /* No KCK available to derive PMKID */ | |
2045 | pmkid = NULL; | |
2046 | } else { | |
6fc6879b JM |
2047 | /* |
2048 | * Calculate PMKID since no PMKSA cache entry was | |
2049 | * available with pre-calculated PMKID. | |
2050 | */ | |
207976f0 | 2051 | rsn_pmkid(sm->PMK, sm->pmk_len, sm->wpa_auth->addr, |
56586197 JM |
2052 | sm->addr, &pmkid[2 + RSN_SELECTOR_LEN], |
2053 | wpa_key_mgmt_sha256(sm->wpa_key_mgmt)); | |
6fc6879b JM |
2054 | } |
2055 | } | |
2056 | wpa_send_eapol(sm->wpa_auth, sm, | |
2057 | WPA_KEY_INFO_ACK | WPA_KEY_INFO_KEY_TYPE, NULL, | |
2058 | sm->ANonce, pmkid, pmkid_len, 0, 0); | |
6fc6879b JM |
2059 | } |
2060 | ||
2061 | ||
f23c5b17 | 2062 | static int wpa_derive_ptk(struct wpa_state_machine *sm, const u8 *snonce, |
207976f0 JM |
2063 | const u8 *pmk, unsigned int pmk_len, |
2064 | struct wpa_ptk *ptk) | |
6fc6879b | 2065 | { |
4ec1fd8e | 2066 | #ifdef CONFIG_IEEE80211R_AP |
56586197 | 2067 | if (wpa_key_mgmt_ft(sm->wpa_key_mgmt)) |
98cd3d1c | 2068 | return wpa_auth_derive_ptk_ft(sm, pmk, ptk); |
4ec1fd8e | 2069 | #endif /* CONFIG_IEEE80211R_AP */ |
6fc6879b | 2070 | |
207976f0 | 2071 | return wpa_pmk_to_ptk(pmk, pmk_len, "Pairwise key expansion", |
98cd3d1c JM |
2072 | sm->wpa_auth->addr, sm->addr, sm->ANonce, snonce, |
2073 | ptk, sm->wpa_key_mgmt, sm->pairwise); | |
6fc6879b JM |
2074 | } |
2075 | ||
2076 | ||
3b5b7aa8 | 2077 | #ifdef CONFIG_FILS |
c4fd6d8a JM |
2078 | |
2079 | int fils_auth_pmk_to_ptk(struct wpa_state_machine *sm, const u8 *pmk, | |
2080 | size_t pmk_len, const u8 *snonce, const u8 *anonce) | |
2081 | { | |
2082 | u8 ick[FILS_ICK_MAX_LEN]; | |
2083 | size_t ick_len; | |
2084 | int res; | |
2085 | ||
2086 | res = fils_pmk_to_ptk(pmk, pmk_len, sm->addr, sm->wpa_auth->addr, | |
2087 | snonce, anonce, &sm->PTK, ick, &ick_len, | |
2088 | sm->wpa_key_mgmt, sm->pairwise); | |
2089 | if (res < 0) | |
2090 | return res; | |
2091 | sm->PTK_valid = TRUE; | |
2092 | ||
2093 | res = fils_key_auth_sk(ick, ick_len, snonce, anonce, | |
2094 | sm->addr, sm->wpa_auth->addr, | |
2095 | NULL, 0, NULL, 0, /* TODO: SK+PFS */ | |
2096 | sm->wpa_key_mgmt, sm->fils_key_auth_sta, | |
2097 | sm->fils_key_auth_ap, | |
2098 | &sm->fils_key_auth_len); | |
2099 | os_memset(ick, 0, sizeof(ick)); | |
78815f3d JM |
2100 | |
2101 | /* Store nonces for (Re)Association Request/Response frame processing */ | |
2102 | os_memcpy(sm->SNonce, snonce, FILS_NONCE_LEN); | |
2103 | os_memcpy(sm->ANonce, anonce, FILS_NONCE_LEN); | |
2104 | ||
c4fd6d8a JM |
2105 | return res; |
2106 | } | |
2107 | ||
2108 | ||
3b5b7aa8 | 2109 | static int wpa_aead_decrypt(struct wpa_state_machine *sm, struct wpa_ptk *ptk, |
75c8563e | 2110 | u8 *buf, size_t buf_len, u16 *_key_data_len) |
3b5b7aa8 JM |
2111 | { |
2112 | struct ieee802_1x_hdr *hdr; | |
2113 | struct wpa_eapol_key *key; | |
2114 | u8 *pos; | |
2115 | u16 key_data_len; | |
2116 | u8 *tmp; | |
2117 | const u8 *aad[1]; | |
2118 | size_t aad_len[1]; | |
2119 | ||
75c8563e | 2120 | hdr = (struct ieee802_1x_hdr *) buf; |
3b5b7aa8 JM |
2121 | key = (struct wpa_eapol_key *) (hdr + 1); |
2122 | pos = (u8 *) (key + 1); | |
2123 | key_data_len = WPA_GET_BE16(pos); | |
2124 | if (key_data_len < AES_BLOCK_SIZE || | |
2125 | key_data_len > buf_len - sizeof(*hdr) - sizeof(*key) - 2) { | |
2126 | wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_INFO, | |
2127 | "No room for AES-SIV data in the frame"); | |
2128 | return -1; | |
2129 | } | |
2130 | pos += 2; /* Pointing at the Encrypted Key Data field */ | |
2131 | ||
2132 | tmp = os_malloc(key_data_len); | |
2133 | if (!tmp) | |
2134 | return -1; | |
2135 | ||
2136 | /* AES-SIV AAD from EAPOL protocol version field (inclusive) to | |
2137 | * to Key Data (exclusive). */ | |
75c8563e JM |
2138 | aad[0] = buf; |
2139 | aad_len[0] = pos - buf; | |
3b5b7aa8 JM |
2140 | if (aes_siv_decrypt(ptk->kek, ptk->kek_len, pos, key_data_len, |
2141 | 1, aad, aad_len, tmp) < 0) { | |
2142 | wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_INFO, | |
2143 | "Invalid AES-SIV data in the frame"); | |
2144 | bin_clear_free(tmp, key_data_len); | |
2145 | return -1; | |
2146 | } | |
2147 | ||
2148 | /* AEAD decryption and validation completed successfully */ | |
2149 | key_data_len -= AES_BLOCK_SIZE; | |
2150 | wpa_hexdump_key(MSG_DEBUG, "WPA: Decrypted Key Data", | |
2151 | tmp, key_data_len); | |
2152 | ||
2153 | /* Replace Key Data field with the decrypted version */ | |
2154 | os_memcpy(pos, tmp, key_data_len); | |
2155 | pos -= 2; /* Key Data Length field */ | |
2156 | WPA_PUT_BE16(pos, key_data_len); | |
2157 | bin_clear_free(tmp, key_data_len); | |
2158 | if (_key_data_len) | |
2159 | *_key_data_len = key_data_len; | |
2160 | return 0; | |
2161 | } | |
78815f3d JM |
2162 | |
2163 | ||
087631b9 JM |
2164 | const u8 * wpa_fils_validate_fils_session(struct wpa_state_machine *sm, |
2165 | const u8 *ies, size_t ies_len, | |
2166 | const u8 *fils_session) | |
2167 | { | |
2168 | const u8 *ie, *end; | |
2169 | const u8 *session = NULL; | |
2170 | ||
2171 | if (!wpa_key_mgmt_fils(sm->wpa_key_mgmt)) { | |
2172 | wpa_printf(MSG_DEBUG, | |
2173 | "FILS: Not a FILS AKM - reject association"); | |
2174 | return NULL; | |
2175 | } | |
2176 | ||
2177 | /* Verify Session element */ | |
2178 | ie = ies; | |
2179 | end = ((const u8 *) ie) + ies_len; | |
2180 | while (ie + 1 < end) { | |
2181 | if (ie + 2 + ie[1] > end) | |
2182 | break; | |
2183 | if (ie[0] == WLAN_EID_EXTENSION && | |
2184 | ie[1] >= 1 + FILS_SESSION_LEN && | |
2185 | ie[2] == WLAN_EID_EXT_FILS_SESSION) { | |
2186 | session = ie; | |
2187 | break; | |
2188 | } | |
2189 | ie += 2 + ie[1]; | |
2190 | } | |
2191 | ||
2192 | if (!session) { | |
2193 | wpa_printf(MSG_DEBUG, | |
2194 | "FILS: %s: Could not find FILS Session element in Assoc Req - reject", | |
2195 | __func__); | |
2196 | return NULL; | |
2197 | } | |
2198 | ||
2199 | if (!fils_session) { | |
2200 | wpa_printf(MSG_DEBUG, | |
2201 | "FILS: %s: Could not find FILS Session element in STA entry - reject", | |
2202 | __func__); | |
2203 | return NULL; | |
2204 | } | |
2205 | ||
2206 | if (os_memcmp(fils_session, session + 3, FILS_SESSION_LEN) != 0) { | |
2207 | wpa_printf(MSG_DEBUG, "FILS: Session mismatch"); | |
2208 | wpa_hexdump(MSG_DEBUG, "FILS: Expected FILS Session", | |
2209 | fils_session, FILS_SESSION_LEN); | |
2210 | wpa_hexdump(MSG_DEBUG, "FILS: Received FILS Session", | |
2211 | session + 3, FILS_SESSION_LEN); | |
2212 | return NULL; | |
2213 | } | |
2214 | return session; | |
2215 | } | |
2216 | ||
2217 | ||
78815f3d JM |
2218 | int fils_decrypt_assoc(struct wpa_state_machine *sm, const u8 *fils_session, |
2219 | const struct ieee80211_mgmt *mgmt, size_t frame_len, | |
2220 | u8 *pos, size_t left) | |
2221 | { | |
2222 | u16 fc, stype; | |
2223 | const u8 *end, *ie_start, *ie, *session, *crypt; | |
2224 | struct ieee802_11_elems elems; | |
2225 | const u8 *aad[5]; | |
2226 | size_t aad_len[5]; | |
2227 | ||
2228 | if (!sm || !sm->PTK_valid) { | |
2229 | wpa_printf(MSG_DEBUG, | |
2230 | "FILS: No KEK to decrypt Assocication Request frame"); | |
2231 | return -1; | |
2232 | } | |
2233 | ||
2234 | if (!wpa_key_mgmt_fils(sm->wpa_key_mgmt)) { | |
2235 | wpa_printf(MSG_DEBUG, | |
2236 | "FILS: Not a FILS AKM - reject association"); | |
2237 | return -1; | |
2238 | } | |
2239 | ||
2240 | end = ((const u8 *) mgmt) + frame_len; | |
2241 | fc = le_to_host16(mgmt->frame_control); | |
2242 | stype = WLAN_FC_GET_STYPE(fc); | |
2243 | if (stype == WLAN_FC_STYPE_REASSOC_REQ) | |
2244 | ie_start = mgmt->u.reassoc_req.variable; | |
2245 | else | |
2246 | ie_start = mgmt->u.assoc_req.variable; | |
2247 | ie = ie_start; | |
2248 | ||
2249 | /* | |
2250 | * Find FILS Session element which is the last unencrypted element in | |
2251 | * the frame. | |
2252 | */ | |
087631b9 JM |
2253 | session = wpa_fils_validate_fils_session(sm, ie, end - ie, |
2254 | fils_session); | |
78815f3d | 2255 | if (!session) { |
087631b9 | 2256 | wpa_printf(MSG_DEBUG, "FILS: Session validation failed"); |
78815f3d JM |
2257 | return -1; |
2258 | } | |
087631b9 | 2259 | |
78815f3d JM |
2260 | crypt = session + 2 + session[1]; |
2261 | ||
2262 | if (end - crypt < AES_BLOCK_SIZE) { | |
2263 | wpa_printf(MSG_DEBUG, | |
2264 | "FILS: Too short frame to include AES-SIV data"); | |
2265 | return -1; | |
2266 | } | |
2267 | ||
2268 | /* AES-SIV AAD vectors */ | |
2269 | ||
2270 | /* The STA's MAC address */ | |
2271 | aad[0] = mgmt->sa; | |
2272 | aad_len[0] = ETH_ALEN; | |
2273 | /* The AP's BSSID */ | |
2274 | aad[1] = mgmt->da; | |
2275 | aad_len[1] = ETH_ALEN; | |
2276 | /* The STA's nonce */ | |
2277 | aad[2] = sm->SNonce; | |
2278 | aad_len[2] = FILS_NONCE_LEN; | |
2279 | /* The AP's nonce */ | |
2280 | aad[3] = sm->ANonce; | |
2281 | aad_len[3] = FILS_NONCE_LEN; | |
2282 | /* | |
2283 | * The (Re)Association Request frame from the Capability Information | |
2284 | * field to the FILS Session element (both inclusive). | |
2285 | */ | |
2286 | aad[4] = (const u8 *) &mgmt->u.assoc_req.capab_info; | |
d77f3304 | 2287 | aad_len[4] = crypt - aad[4]; |
78815f3d JM |
2288 | |
2289 | if (aes_siv_decrypt(sm->PTK.kek, sm->PTK.kek_len, crypt, end - crypt, | |
d77f3304 | 2290 | 5, aad, aad_len, pos + (crypt - ie_start)) < 0) { |
78815f3d JM |
2291 | wpa_printf(MSG_DEBUG, |
2292 | "FILS: Invalid AES-SIV data in the frame"); | |
2293 | return -1; | |
2294 | } | |
2295 | wpa_hexdump(MSG_DEBUG, "FILS: Decrypted Association Request elements", | |
2296 | pos, left - AES_BLOCK_SIZE); | |
2297 | ||
2298 | if (ieee802_11_parse_elems(pos, left - AES_BLOCK_SIZE, &elems, 1) == | |
2299 | ParseFailed) { | |
2300 | wpa_printf(MSG_DEBUG, | |
2301 | "FILS: Failed to parse decrypted elements"); | |
2302 | return -1; | |
2303 | } | |
2304 | if (!elems.fils_key_confirm) { | |
2305 | wpa_printf(MSG_DEBUG, "FILS: No FILS Key Confirm element"); | |
2306 | return -1; | |
2307 | } | |
2308 | if (elems.fils_key_confirm_len != sm->fils_key_auth_len) { | |
2309 | wpa_printf(MSG_DEBUG, | |
2310 | "FILS: Unexpected Key-Auth length %d (expected %d)", | |
2311 | elems.fils_key_confirm_len, | |
2312 | (int) sm->fils_key_auth_len); | |
2313 | return -1; | |
2314 | } | |
2315 | if (os_memcmp(elems.fils_key_confirm, sm->fils_key_auth_sta, | |
2316 | sm->fils_key_auth_len) != 0) { | |
2317 | wpa_printf(MSG_DEBUG, "FILS: Key-Auth mismatch"); | |
2318 | wpa_hexdump(MSG_DEBUG, "FILS: Received Key-Auth", | |
2319 | elems.fils_key_confirm, | |
2320 | elems.fils_key_confirm_len); | |
2321 | wpa_hexdump(MSG_DEBUG, "FILS: Expected Key-Auth", | |
2322 | sm->fils_key_auth_sta, sm->fils_key_auth_len); | |
2323 | return -1; | |
2324 | } | |
2325 | ||
2326 | return left - AES_BLOCK_SIZE; | |
2327 | } | |
2328 | ||
e73ffa09 JM |
2329 | |
2330 | int fils_encrypt_assoc(struct wpa_state_machine *sm, u8 *buf, | |
91d91abf JM |
2331 | size_t current_len, size_t max_len, |
2332 | const struct wpabuf *hlp) | |
e73ffa09 JM |
2333 | { |
2334 | u8 *end = buf + max_len; | |
2335 | u8 *pos = buf + current_len; | |
2336 | struct ieee80211_mgmt *mgmt; | |
2337 | struct wpabuf *plain; | |
2338 | u8 *len, *tmp, *tmp2; | |
2339 | u8 hdr[2]; | |
2340 | u8 *gtk, dummy_gtk[32]; | |
2341 | size_t gtk_len; | |
2342 | struct wpa_group *gsm; | |
2343 | const u8 *aad[5]; | |
2344 | size_t aad_len[5]; | |
2345 | ||
2346 | if (!sm || !sm->PTK_valid) | |
2347 | return -1; | |
2348 | ||
2349 | wpa_hexdump(MSG_DEBUG, | |
2350 | "FILS: Association Response frame before FILS processing", | |
2351 | buf, current_len); | |
2352 | ||
2353 | mgmt = (struct ieee80211_mgmt *) buf; | |
2354 | ||
2355 | /* AES-SIV AAD vectors */ | |
2356 | ||
2357 | /* The AP's BSSID */ | |
2358 | aad[0] = mgmt->sa; | |
2359 | aad_len[0] = ETH_ALEN; | |
2360 | /* The STA's MAC address */ | |
2361 | aad[1] = mgmt->da; | |
2362 | aad_len[1] = ETH_ALEN; | |
2363 | /* The AP's nonce */ | |
2364 | aad[2] = sm->ANonce; | |
2365 | aad_len[2] = FILS_NONCE_LEN; | |
2366 | /* The STA's nonce */ | |
2367 | aad[3] = sm->SNonce; | |
2368 | aad_len[3] = FILS_NONCE_LEN; | |
2369 | /* | |
2370 | * The (Re)Association Response frame from the Capability Information | |
2371 | * field (the same offset in both Association and Reassociation | |
2372 | * Response frames) to the FILS Session element (both inclusive). | |
2373 | */ | |
2374 | aad[4] = (const u8 *) &mgmt->u.assoc_resp.capab_info; | |
2375 | aad_len[4] = pos - aad[4]; | |
2376 | ||
2377 | /* The following elements will be encrypted with AES-SIV */ | |
2378 | ||
2379 | plain = wpabuf_alloc(1000); | |
2380 | if (!plain) | |
2381 | return -1; | |
2382 | ||
2383 | /* TODO: FILS Public Key */ | |
2384 | ||
2385 | /* FILS Key Confirmation */ | |
2386 | wpabuf_put_u8(plain, WLAN_EID_EXTENSION); /* Element ID */ | |
2387 | wpabuf_put_u8(plain, 1 + sm->fils_key_auth_len); /* Length */ | |
2388 | /* Element ID Extension */ | |
2389 | wpabuf_put_u8(plain, WLAN_EID_EXT_FILS_KEY_CONFIRM); | |
2390 | wpabuf_put_data(plain, sm->fils_key_auth_ap, sm->fils_key_auth_len); | |
2391 | ||
91d91abf JM |
2392 | /* FILS HLP Container */ |
2393 | if (hlp) | |
2394 | wpabuf_put_buf(plain, hlp); | |
e73ffa09 JM |
2395 | |
2396 | /* TODO: FILS IP Address Assignment */ | |
2397 | ||
2398 | /* Key Delivery */ | |
2399 | gsm = sm->group; | |
2400 | wpabuf_put_u8(plain, WLAN_EID_EXTENSION); /* Element ID */ | |
2401 | len = wpabuf_put(plain, 1); | |
2402 | wpabuf_put_u8(plain, WLAN_EID_EXT_KEY_DELIVERY); | |
2403 | wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN, | |
2404 | wpabuf_put(plain, WPA_KEY_RSC_LEN)); | |
2405 | /* GTK KDE */ | |
2406 | gtk = gsm->GTK[gsm->GN - 1]; | |
2407 | gtk_len = gsm->GTK_len; | |
2408 | if (sm->wpa_auth->conf.disable_gtk) { | |
2409 | /* | |
2410 | * Provide unique random GTK to each STA to prevent use | |
2411 | * of GTK in the BSS. | |
2412 | */ | |
2413 | if (random_get_bytes(dummy_gtk, gtk_len) < 0) { | |
2414 | wpabuf_free(plain); | |
2415 | return -1; | |
2416 | } | |
2417 | gtk = dummy_gtk; | |
2418 | } | |
2419 | hdr[0] = gsm->GN & 0x03; | |
2420 | hdr[1] = 0; | |
2421 | tmp = wpabuf_put(plain, 0); | |
2422 | tmp2 = wpa_add_kde(tmp, RSN_KEY_DATA_GROUPKEY, hdr, 2, | |
2423 | gtk, gtk_len); | |
2424 | wpabuf_put(plain, tmp2 - tmp); | |
2425 | ||
2426 | /* IGTK KDE */ | |
2427 | tmp = wpabuf_put(plain, 0); | |
2428 | tmp2 = ieee80211w_kde_add(sm, tmp); | |
2429 | wpabuf_put(plain, tmp2 - tmp); | |
2430 | ||
2431 | *len = (u8 *) wpabuf_put(plain, 0) - len - 1; | |
2432 | ||
2433 | if (pos + wpabuf_len(plain) + AES_BLOCK_SIZE > end) { | |
2434 | wpa_printf(MSG_DEBUG, | |
2435 | "FILS: Not enough room for FILS elements"); | |
2436 | wpabuf_free(plain); | |
2437 | return -1; | |
2438 | } | |
2439 | ||
2440 | wpa_hexdump_buf_key(MSG_DEBUG, "FILS: Association Response plaintext", | |
2441 | plain); | |
2442 | ||
2443 | if (aes_siv_encrypt(sm->PTK.kek, sm->PTK.kek_len, | |
2444 | wpabuf_head(plain), wpabuf_len(plain), | |
2445 | 5, aad, aad_len, pos) < 0) { | |
2446 | wpabuf_free(plain); | |
2447 | return -1; | |
2448 | } | |
2449 | ||
2450 | wpa_hexdump(MSG_DEBUG, | |
2451 | "FILS: Encrypted Association Response elements", | |
2452 | pos, AES_BLOCK_SIZE + wpabuf_len(plain)); | |
2453 | current_len += wpabuf_len(plain) + AES_BLOCK_SIZE; | |
2454 | wpabuf_free(plain); | |
2455 | ||
07e0117d JM |
2456 | sm->fils_completed = 1; |
2457 | ||
e73ffa09 JM |
2458 | return current_len; |
2459 | } | |
2460 | ||
da24c5aa JM |
2461 | |
2462 | int fils_set_tk(struct wpa_state_machine *sm) | |
2463 | { | |
2464 | enum wpa_alg alg; | |
2465 | int klen; | |
2466 | ||
2467 | if (!sm || !sm->PTK_valid) | |
2468 | return -1; | |
2469 | ||
2470 | alg = wpa_cipher_to_alg(sm->pairwise); | |
2471 | klen = wpa_cipher_key_len(sm->pairwise); | |
2472 | ||
2473 | wpa_printf(MSG_DEBUG, "FILS: Configure TK to the driver"); | |
2474 | if (wpa_auth_set_key(sm->wpa_auth, 0, alg, sm->addr, 0, | |
2475 | sm->PTK.tk, klen)) { | |
2476 | wpa_printf(MSG_DEBUG, "FILS: Failed to set TK to the driver"); | |
2477 | return -1; | |
2478 | } | |
2479 | ||
2480 | return 0; | |
2481 | } | |
2482 | ||
3b5b7aa8 JM |
2483 | #endif /* CONFIG_FILS */ |
2484 | ||
2485 | ||
6fc6879b JM |
2486 | SM_STATE(WPA_PTK, PTKCALCNEGOTIATING) |
2487 | { | |
3b5b7aa8 | 2488 | struct wpa_authenticator *wpa_auth = sm->wpa_auth; |
6fc6879b | 2489 | struct wpa_ptk PTK; |
2c502460 | 2490 | int ok = 0, psk_found = 0; |
6fc6879b | 2491 | const u8 *pmk = NULL; |
207976f0 | 2492 | unsigned int pmk_len; |
3b5b7aa8 JM |
2493 | int ft; |
2494 | const u8 *eapol_key_ie, *key_data, *mic; | |
2495 | u16 key_data_length; | |
2496 | size_t mic_len, eapol_key_ie_len; | |
2497 | struct ieee802_1x_hdr *hdr; | |
2498 | struct wpa_eapol_key *key; | |
2499 | struct wpa_eapol_ie_parse kde; | |
6fc6879b JM |
2500 | |
2501 | SM_ENTRY_MA(WPA_PTK, PTKCALCNEGOTIATING, wpa_ptk); | |
2502 | sm->EAPOLKeyReceived = FALSE; | |
68921e24 | 2503 | sm->update_snonce = FALSE; |
b729fd8d | 2504 | os_memset(&PTK, 0, sizeof(PTK)); |
6fc6879b | 2505 | |
3b5b7aa8 JM |
2506 | mic_len = wpa_mic_len(sm->wpa_key_mgmt); |
2507 | ||
6fc6879b JM |
2508 | /* WPA with IEEE 802.1X: use the derived PMK from EAP |
2509 | * WPA-PSK: iterate through possible PSKs and select the one matching | |
2510 | * the packet */ | |
2511 | for (;;) { | |
56586197 | 2512 | if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt)) { |
759fd76b JM |
2513 | pmk = wpa_auth_get_psk(sm->wpa_auth, sm->addr, |
2514 | sm->p2p_dev_addr, pmk); | |
6fc6879b JM |
2515 | if (pmk == NULL) |
2516 | break; | |
2c502460 | 2517 | psk_found = 1; |
207976f0 JM |
2518 | pmk_len = PMK_LEN; |
2519 | } else { | |
6fc6879b | 2520 | pmk = sm->PMK; |
207976f0 JM |
2521 | pmk_len = sm->pmk_len; |
2522 | } | |
6fc6879b | 2523 | |
364c064a JM |
2524 | if (wpa_derive_ptk(sm, sm->SNonce, pmk, pmk_len, &PTK) < 0) |
2525 | break; | |
6fc6879b | 2526 | |
3b5b7aa8 JM |
2527 | if (mic_len && |
2528 | wpa_verify_key_mic(sm->wpa_key_mgmt, &PTK, | |
929a2ea5 | 2529 | sm->last_rx_eapol_key, |
6fc6879b JM |
2530 | sm->last_rx_eapol_key_len) == 0) { |
2531 | ok = 1; | |
2532 | break; | |
2533 | } | |
2534 | ||
3b5b7aa8 | 2535 | #ifdef CONFIG_FILS |
75c8563e JM |
2536 | if (!mic_len && |
2537 | wpa_aead_decrypt(sm, &PTK, sm->last_rx_eapol_key, | |
2538 | sm->last_rx_eapol_key_len, NULL) == 0) { | |
3b5b7aa8 JM |
2539 | ok = 1; |
2540 | break; | |
2541 | } | |
2542 | #endif /* CONFIG_FILS */ | |
2543 | ||
56586197 | 2544 | if (!wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt)) |
6fc6879b JM |
2545 | break; |
2546 | } | |
2547 | ||
2548 | if (!ok) { | |
2549 | wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG, | |
2550 | "invalid MIC in msg 2/4 of 4-Way Handshake"); | |
2c502460 JM |
2551 | if (psk_found) |
2552 | wpa_auth_psk_failure_report(sm->wpa_auth, sm->addr); | |
6fc6879b JM |
2553 | return; |
2554 | } | |
2555 | ||
3b5b7aa8 JM |
2556 | /* |
2557 | * Note: last_rx_eapol_key length fields have already been validated in | |
2558 | * wpa_receive(). | |
2559 | */ | |
2560 | hdr = (struct ieee802_1x_hdr *) sm->last_rx_eapol_key; | |
2561 | key = (struct wpa_eapol_key *) (hdr + 1); | |
2562 | mic = (u8 *) (key + 1); | |
2563 | key_data = mic + mic_len + 2; | |
2564 | key_data_length = WPA_GET_BE16(mic + mic_len); | |
2565 | if (key_data_length > sm->last_rx_eapol_key_len - sizeof(*hdr) - | |
2566 | sizeof(*key) - mic_len - 2) | |
2567 | return; | |
2568 | ||
2569 | if (wpa_parse_kde_ies(key_data, key_data_length, &kde) < 0) { | |
2570 | wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_INFO, | |
2571 | "received EAPOL-Key msg 2/4 with invalid Key Data contents"); | |
2572 | return; | |
2573 | } | |
2574 | if (kde.rsn_ie) { | |
2575 | eapol_key_ie = kde.rsn_ie; | |
2576 | eapol_key_ie_len = kde.rsn_ie_len; | |
2577 | } else if (kde.osen) { | |
2578 | eapol_key_ie = kde.osen; | |
2579 | eapol_key_ie_len = kde.osen_len; | |
2580 | } else { | |
2581 | eapol_key_ie = kde.wpa_ie; | |
2582 | eapol_key_ie_len = kde.wpa_ie_len; | |
2583 | } | |
2584 | ft = sm->wpa == WPA_VERSION_WPA2 && wpa_key_mgmt_ft(sm->wpa_key_mgmt); | |
2585 | if (sm->wpa_ie == NULL || | |
2586 | wpa_compare_rsn_ie(ft, sm->wpa_ie, sm->wpa_ie_len, | |
2587 | eapol_key_ie, eapol_key_ie_len)) { | |
2588 | wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO, | |
2589 | "WPA IE from (Re)AssocReq did not match with msg 2/4"); | |
2590 | if (sm->wpa_ie) { | |
2591 | wpa_hexdump(MSG_DEBUG, "WPA IE in AssocReq", | |
2592 | sm->wpa_ie, sm->wpa_ie_len); | |
2593 | } | |
2594 | wpa_hexdump(MSG_DEBUG, "WPA IE in msg 2/4", | |
2595 | eapol_key_ie, eapol_key_ie_len); | |
2596 | /* MLME-DEAUTHENTICATE.request */ | |
2597 | wpa_sta_disconnect(wpa_auth, sm->addr); | |
2598 | return; | |
2599 | } | |
4ec1fd8e | 2600 | #ifdef CONFIG_IEEE80211R_AP |
3b5b7aa8 JM |
2601 | if (ft && ft_check_msg_2_of_4(wpa_auth, sm, &kde) < 0) { |
2602 | wpa_sta_disconnect(wpa_auth, sm->addr); | |
2603 | return; | |
2604 | } | |
4ec1fd8e | 2605 | #endif /* CONFIG_IEEE80211R_AP */ |
3b5b7aa8 JM |
2606 | #ifdef CONFIG_P2P |
2607 | if (kde.ip_addr_req && kde.ip_addr_req[0] && | |
2608 | wpa_auth->ip_pool && WPA_GET_BE32(sm->ip_addr) == 0) { | |
2609 | int idx; | |
2610 | wpa_printf(MSG_DEBUG, | |
2611 | "P2P: IP address requested in EAPOL-Key exchange"); | |
2612 | idx = bitfield_get_first_zero(wpa_auth->ip_pool); | |
2613 | if (idx >= 0) { | |
2614 | u32 start = WPA_GET_BE32(wpa_auth->conf.ip_addr_start); | |
2615 | bitfield_set(wpa_auth->ip_pool, idx); | |
2616 | WPA_PUT_BE32(sm->ip_addr, start + idx); | |
2617 | wpa_printf(MSG_DEBUG, | |
2618 | "P2P: Assigned IP address %u.%u.%u.%u to " | |
2619 | MACSTR, sm->ip_addr[0], sm->ip_addr[1], | |
2620 | sm->ip_addr[2], sm->ip_addr[3], | |
2621 | MAC2STR(sm->addr)); | |
2622 | } | |
2623 | } | |
2624 | #endif /* CONFIG_P2P */ | |
2625 | ||
4ec1fd8e | 2626 | #ifdef CONFIG_IEEE80211R_AP |
26e23750 JM |
2627 | if (sm->wpa == WPA_VERSION_WPA2 && wpa_key_mgmt_ft(sm->wpa_key_mgmt)) { |
2628 | /* | |
2629 | * Verify that PMKR1Name from EAPOL-Key message 2/4 matches | |
2630 | * with the value we derived. | |
2631 | */ | |
870834a1 JM |
2632 | if (os_memcmp_const(sm->sup_pmk_r1_name, sm->pmk_r1_name, |
2633 | WPA_PMK_NAME_LEN) != 0) { | |
26e23750 JM |
2634 | wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG, |
2635 | "PMKR1Name mismatch in FT 4-way " | |
2636 | "handshake"); | |
2637 | wpa_hexdump(MSG_DEBUG, "FT: PMKR1Name from " | |
2638 | "Supplicant", | |
2639 | sm->sup_pmk_r1_name, WPA_PMK_NAME_LEN); | |
2640 | wpa_hexdump(MSG_DEBUG, "FT: Derived PMKR1Name", | |
2641 | sm->pmk_r1_name, WPA_PMK_NAME_LEN); | |
2642 | return; | |
2643 | } | |
2644 | } | |
4ec1fd8e | 2645 | #endif /* CONFIG_IEEE80211R_AP */ |
26e23750 | 2646 | |
e4bf4db9 | 2647 | sm->pending_1_of_4_timeout = 0; |
6fc6879b JM |
2648 | eloop_cancel_timeout(wpa_send_eapol_timeout, sm->wpa_auth, sm); |
2649 | ||
56586197 | 2650 | if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt)) { |
6fc6879b JM |
2651 | /* PSK may have changed from the previous choice, so update |
2652 | * state machine data based on whatever PSK was selected here. | |
2653 | */ | |
2654 | os_memcpy(sm->PMK, pmk, PMK_LEN); | |
207976f0 | 2655 | sm->pmk_len = PMK_LEN; |
6fc6879b JM |
2656 | } |
2657 | ||
2658 | sm->MICVerified = TRUE; | |
2659 | ||
2660 | os_memcpy(&sm->PTK, &PTK, sizeof(PTK)); | |
2661 | sm->PTK_valid = TRUE; | |
2662 | } | |
2663 | ||
2664 | ||
2665 | SM_STATE(WPA_PTK, PTKCALCNEGOTIATING2) | |
2666 | { | |
2667 | SM_ENTRY_MA(WPA_PTK, PTKCALCNEGOTIATING2, wpa_ptk); | |
2668 | sm->TimeoutCtr = 0; | |
2669 | } | |
2670 | ||
2671 | ||
2672 | #ifdef CONFIG_IEEE80211W | |
2673 | ||
2674 | static int ieee80211w_kde_len(struct wpa_state_machine *sm) | |
2675 | { | |
2676 | if (sm->mgmt_frame_prot) { | |
8dd9f9cd JM |
2677 | size_t len; |
2678 | len = wpa_cipher_key_len(sm->wpa_auth->conf.group_mgmt_cipher); | |
2679 | return 2 + RSN_SELECTOR_LEN + WPA_IGTK_KDE_PREFIX_LEN + len; | |
6fc6879b JM |
2680 | } |
2681 | ||
2682 | return 0; | |
2683 | } | |
2684 | ||
2685 | ||
2686 | static u8 * ieee80211w_kde_add(struct wpa_state_machine *sm, u8 *pos) | |
2687 | { | |
2688 | struct wpa_igtk_kde igtk; | |
2689 | struct wpa_group *gsm = sm->group; | |
03610ad2 | 2690 | u8 rsc[WPA_KEY_RSC_LEN]; |
8dd9f9cd | 2691 | size_t len = wpa_cipher_key_len(sm->wpa_auth->conf.group_mgmt_cipher); |
6fc6879b JM |
2692 | |
2693 | if (!sm->mgmt_frame_prot) | |
2694 | return pos; | |
2695 | ||
2696 | igtk.keyid[0] = gsm->GN_igtk; | |
2697 | igtk.keyid[1] = 0; | |
7b1080da | 2698 | if (gsm->wpa_group_state != WPA_GROUP_SETKEYSDONE || |
03610ad2 | 2699 | wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN_igtk, rsc) < 0) |
6fc6879b | 2700 | os_memset(igtk.pn, 0, sizeof(igtk.pn)); |
03610ad2 JM |
2701 | else |
2702 | os_memcpy(igtk.pn, rsc, sizeof(igtk.pn)); | |
8dd9f9cd | 2703 | os_memcpy(igtk.igtk, gsm->IGTK[gsm->GN_igtk - 4], len); |
83421850 JM |
2704 | if (sm->wpa_auth->conf.disable_gtk) { |
2705 | /* | |
2706 | * Provide unique random IGTK to each STA to prevent use of | |
2707 | * IGTK in the BSS. | |
2708 | */ | |
8dd9f9cd | 2709 | if (random_get_bytes(igtk.igtk, len) < 0) |
83421850 JM |
2710 | return pos; |
2711 | } | |
6fc6879b | 2712 | pos = wpa_add_kde(pos, RSN_KEY_DATA_IGTK, |
8dd9f9cd JM |
2713 | (const u8 *) &igtk, WPA_IGTK_KDE_PREFIX_LEN + len, |
2714 | NULL, 0); | |
6fc6879b JM |
2715 | |
2716 | return pos; | |
2717 | } | |
2718 | ||
2719 | #else /* CONFIG_IEEE80211W */ | |
2720 | ||
2721 | static int ieee80211w_kde_len(struct wpa_state_machine *sm) | |
2722 | { | |
2723 | return 0; | |
2724 | } | |
2725 | ||
2726 | ||
2727 | static u8 * ieee80211w_kde_add(struct wpa_state_machine *sm, u8 *pos) | |
2728 | { | |
2729 | return pos; | |
2730 | } | |
2731 | ||
2732 | #endif /* CONFIG_IEEE80211W */ | |
2733 | ||
2734 | ||
2735 | SM_STATE(WPA_PTK, PTKINITNEGOTIATING) | |
2736 | { | |
83421850 | 2737 | u8 rsc[WPA_KEY_RSC_LEN], *_rsc, *gtk, *kde, *pos, dummy_gtk[32]; |
6fc6879b JM |
2738 | size_t gtk_len, kde_len; |
2739 | struct wpa_group *gsm = sm->group; | |
2740 | u8 *wpa_ie; | |
2741 | int wpa_ie_len, secure, keyidx, encr = 0; | |
2742 | ||
2743 | SM_ENTRY_MA(WPA_PTK, PTKINITNEGOTIATING, wpa_ptk); | |
2744 | sm->TimeoutEvt = FALSE; | |
bae61562 JM |
2745 | |
2746 | sm->TimeoutCtr++; | |
41f140d3 | 2747 | if (sm->TimeoutCtr > sm->wpa_auth->conf.wpa_pairwise_update_count) { |
bae61562 JM |
2748 | /* No point in sending the EAPOL-Key - we will disconnect |
2749 | * immediately following this. */ | |
2750 | return; | |
2751 | } | |
2752 | ||
86dfabb8 JM |
2753 | /* Send EAPOL(1, 1, 1, Pair, P, RSC, ANonce, MIC(PTK), RSNIE, [MDIE], |
2754 | GTK[GN], IGTK, [FTIE], [TIE * 2]) | |
6fc6879b JM |
2755 | */ |
2756 | os_memset(rsc, 0, WPA_KEY_RSC_LEN); | |
2757 | wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN, rsc); | |
86dfabb8 | 2758 | /* If FT is used, wpa_auth->wpa_ie includes both RSNIE and MDIE */ |
6fc6879b JM |
2759 | wpa_ie = sm->wpa_auth->wpa_ie; |
2760 | wpa_ie_len = sm->wpa_auth->wpa_ie_len; | |
2761 | if (sm->wpa == WPA_VERSION_WPA && | |
2762 | (sm->wpa_auth->conf.wpa & WPA_PROTO_RSN) && | |
2763 | wpa_ie_len > wpa_ie[1] + 2 && wpa_ie[0] == WLAN_EID_RSN) { | |
774d4145 | 2764 | /* WPA-only STA, remove RSN IE and possible MDIE */ |
6fc6879b | 2765 | wpa_ie = wpa_ie + wpa_ie[1] + 2; |
774d4145 JM |
2766 | if (wpa_ie[0] == WLAN_EID_MOBILITY_DOMAIN) |
2767 | wpa_ie = wpa_ie + wpa_ie[1] + 2; | |
6fc6879b JM |
2768 | wpa_ie_len = wpa_ie[1] + 2; |
2769 | } | |
2770 | wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG, | |
2771 | "sending 3/4 msg of 4-Way Handshake"); | |
2772 | if (sm->wpa == WPA_VERSION_WPA2) { | |
2773 | /* WPA2 send GTK in the 4-way handshake */ | |
2774 | secure = 1; | |
2775 | gtk = gsm->GTK[gsm->GN - 1]; | |
2776 | gtk_len = gsm->GTK_len; | |
83421850 JM |
2777 | if (sm->wpa_auth->conf.disable_gtk) { |
2778 | /* | |
2779 | * Provide unique random GTK to each STA to prevent use | |
2780 | * of GTK in the BSS. | |
2781 | */ | |
2782 | if (random_get_bytes(dummy_gtk, gtk_len) < 0) | |
2783 | return; | |
2784 | gtk = dummy_gtk; | |
2785 | } | |
6fc6879b JM |
2786 | keyidx = gsm->GN; |
2787 | _rsc = rsc; | |
2788 | encr = 1; | |
2789 | } else { | |
2790 | /* WPA does not include GTK in msg 3/4 */ | |
2791 | secure = 0; | |
2792 | gtk = NULL; | |
2793 | gtk_len = 0; | |
2794 | keyidx = 0; | |
2795 | _rsc = NULL; | |
0d442aff JM |
2796 | if (sm->rx_eapol_key_secure) { |
2797 | /* | |
2798 | * It looks like Windows 7 supplicant tries to use | |
2799 | * Secure bit in msg 2/4 after having reported Michael | |
2800 | * MIC failure and it then rejects the 4-way handshake | |
2801 | * if msg 3/4 does not set Secure bit. Work around this | |
2802 | * by setting the Secure bit here even in the case of | |
2803 | * WPA if the supplicant used it first. | |
2804 | */ | |
2805 | wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG, | |
4740d5b9 JM |
2806 | "STA used Secure bit in WPA msg 2/4 - " |
2807 | "set Secure for 3/4 as workaround"); | |
0d442aff JM |
2808 | secure = 1; |
2809 | } | |
6fc6879b JM |
2810 | } |
2811 | ||
2812 | kde_len = wpa_ie_len + ieee80211w_kde_len(sm); | |
2813 | if (gtk) | |
2814 | kde_len += 2 + RSN_SELECTOR_LEN + 2 + gtk_len; | |
4ec1fd8e | 2815 | #ifdef CONFIG_IEEE80211R_AP |
86dfabb8 JM |
2816 | if (wpa_key_mgmt_ft(sm->wpa_key_mgmt)) { |
2817 | kde_len += 2 + PMKID_LEN; /* PMKR1Name into RSN IE */ | |
2818 | kde_len += 300; /* FTIE + 2 * TIE */ | |
2819 | } | |
4ec1fd8e | 2820 | #endif /* CONFIG_IEEE80211R_AP */ |
25ef8529 JM |
2821 | #ifdef CONFIG_P2P |
2822 | if (WPA_GET_BE32(sm->ip_addr) > 0) | |
2823 | kde_len += 2 + RSN_SELECTOR_LEN + 3 * 4; | |
2824 | #endif /* CONFIG_P2P */ | |
6fc6879b JM |
2825 | kde = os_malloc(kde_len); |
2826 | if (kde == NULL) | |
2827 | return; | |
2828 | ||
2829 | pos = kde; | |
2830 | os_memcpy(pos, wpa_ie, wpa_ie_len); | |
2831 | pos += wpa_ie_len; | |
4ec1fd8e | 2832 | #ifdef CONFIG_IEEE80211R_AP |
26e23750 | 2833 | if (wpa_key_mgmt_ft(sm->wpa_key_mgmt)) { |
59e78c24 JM |
2834 | int res; |
2835 | size_t elen; | |
2836 | ||
2837 | elen = pos - kde; | |
2838 | res = wpa_insert_pmkid(kde, &elen, sm->pmk_r1_name); | |
26e23750 JM |
2839 | if (res < 0) { |
2840 | wpa_printf(MSG_ERROR, "FT: Failed to insert " | |
2841 | "PMKR1Name into RSN IE in EAPOL-Key data"); | |
2842 | os_free(kde); | |
2843 | return; | |
2844 | } | |
59e78c24 JM |
2845 | pos -= wpa_ie_len; |
2846 | pos += elen; | |
26e23750 | 2847 | } |
4ec1fd8e | 2848 | #endif /* CONFIG_IEEE80211R_AP */ |
6fc6879b JM |
2849 | if (gtk) { |
2850 | u8 hdr[2]; | |
2851 | hdr[0] = keyidx & 0x03; | |
2852 | hdr[1] = 0; | |
2853 | pos = wpa_add_kde(pos, RSN_KEY_DATA_GROUPKEY, hdr, 2, | |
2854 | gtk, gtk_len); | |
2855 | } | |
2856 | pos = ieee80211w_kde_add(sm, pos); | |
2857 | ||
4ec1fd8e | 2858 | #ifdef CONFIG_IEEE80211R_AP |
86dfabb8 JM |
2859 | if (wpa_key_mgmt_ft(sm->wpa_key_mgmt)) { |
2860 | int res; | |
2861 | struct wpa_auth_config *conf; | |
2862 | ||
2863 | conf = &sm->wpa_auth->conf; | |
9257610a JM |
2864 | if (sm->assoc_resp_ftie && |
2865 | kde + kde_len - pos >= 2 + sm->assoc_resp_ftie[1]) { | |
2866 | os_memcpy(pos, sm->assoc_resp_ftie, | |
2867 | 2 + sm->assoc_resp_ftie[1]); | |
2868 | res = 2 + sm->assoc_resp_ftie[1]; | |
2869 | } else { | |
2870 | res = wpa_write_ftie(conf, conf->r0_key_holder, | |
2871 | conf->r0_key_holder_len, | |
2872 | NULL, NULL, pos, | |
2873 | kde + kde_len - pos, | |
2874 | NULL, 0); | |
2875 | } | |
86dfabb8 JM |
2876 | if (res < 0) { |
2877 | wpa_printf(MSG_ERROR, "FT: Failed to insert FTIE " | |
2878 | "into EAPOL-Key Key Data"); | |
2879 | os_free(kde); | |
2880 | return; | |
2881 | } | |
2882 | pos += res; | |
2883 | ||
2884 | /* TIE[ReassociationDeadline] (TU) */ | |
2885 | *pos++ = WLAN_EID_TIMEOUT_INTERVAL; | |
2886 | *pos++ = 5; | |
2887 | *pos++ = WLAN_TIMEOUT_REASSOC_DEADLINE; | |
2888 | WPA_PUT_LE32(pos, conf->reassociation_deadline); | |
2889 | pos += 4; | |
2890 | ||
2891 | /* TIE[KeyLifetime] (seconds) */ | |
2892 | *pos++ = WLAN_EID_TIMEOUT_INTERVAL; | |
2893 | *pos++ = 5; | |
2894 | *pos++ = WLAN_TIMEOUT_KEY_LIFETIME; | |
2895 | WPA_PUT_LE32(pos, conf->r0_key_lifetime * 60); | |
2896 | pos += 4; | |
2897 | } | |
4ec1fd8e | 2898 | #endif /* CONFIG_IEEE80211R_AP */ |
25ef8529 JM |
2899 | #ifdef CONFIG_P2P |
2900 | if (WPA_GET_BE32(sm->ip_addr) > 0) { | |
2901 | u8 addr[3 * 4]; | |
2902 | os_memcpy(addr, sm->ip_addr, 4); | |
2903 | os_memcpy(addr + 4, sm->wpa_auth->conf.ip_addr_mask, 4); | |
2904 | os_memcpy(addr + 8, sm->wpa_auth->conf.ip_addr_go, 4); | |
2905 | pos = wpa_add_kde(pos, WFA_KEY_DATA_IP_ADDR_ALLOC, | |
2906 | addr, sizeof(addr), NULL, 0); | |
2907 | } | |
2908 | #endif /* CONFIG_P2P */ | |
86dfabb8 | 2909 | |
6fc6879b | 2910 | wpa_send_eapol(sm->wpa_auth, sm, |
b729fd8d JM |
2911 | (secure ? WPA_KEY_INFO_SECURE : 0) | |
2912 | (wpa_mic_len(sm->wpa_key_mgmt) ? WPA_KEY_INFO_MIC : 0) | | |
6fc6879b JM |
2913 | WPA_KEY_INFO_ACK | WPA_KEY_INFO_INSTALL | |
2914 | WPA_KEY_INFO_KEY_TYPE, | |
2915 | _rsc, sm->ANonce, kde, pos - kde, keyidx, encr); | |
2916 | os_free(kde); | |
6fc6879b JM |
2917 | } |
2918 | ||
2919 | ||
2920 | SM_STATE(WPA_PTK, PTKINITDONE) | |
2921 | { | |
2922 | SM_ENTRY_MA(WPA_PTK, PTKINITDONE, wpa_ptk); | |
2923 | sm->EAPOLKeyReceived = FALSE; | |
2924 | if (sm->Pair) { | |
c3550295 JM |
2925 | enum wpa_alg alg = wpa_cipher_to_alg(sm->pairwise); |
2926 | int klen = wpa_cipher_key_len(sm->pairwise); | |
6fc6879b | 2927 | if (wpa_auth_set_key(sm->wpa_auth, 0, alg, sm->addr, 0, |
98cd3d1c | 2928 | sm->PTK.tk, klen)) { |
6fc6879b JM |
2929 | wpa_sta_disconnect(sm->wpa_auth, sm->addr); |
2930 | return; | |
2931 | } | |
2932 | /* FIX: MLME-SetProtection.Request(TA, Tx_Rx) */ | |
2933 | sm->pairwise_set = TRUE; | |
2934 | ||
581a8cde JM |
2935 | if (sm->wpa_auth->conf.wpa_ptk_rekey) { |
2936 | eloop_cancel_timeout(wpa_rekey_ptk, sm->wpa_auth, sm); | |
2937 | eloop_register_timeout(sm->wpa_auth->conf. | |
2938 | wpa_ptk_rekey, 0, wpa_rekey_ptk, | |
2939 | sm->wpa_auth, sm); | |
2940 | } | |
2941 | ||
a1ea1b45 JM |
2942 | if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt) || |
2943 | sm->wpa_key_mgmt == WPA_KEY_MGMT_OWE) { | |
6fc6879b JM |
2944 | wpa_auth_set_eapol(sm->wpa_auth, sm->addr, |
2945 | WPA_EAPOL_authorized, 1); | |
2946 | } | |
2947 | } | |
2948 | ||
2949 | if (0 /* IBSS == TRUE */) { | |
2950 | sm->keycount++; | |
2951 | if (sm->keycount == 2) { | |
2952 | wpa_auth_set_eapol(sm->wpa_auth, sm->addr, | |
2953 | WPA_EAPOL_portValid, 1); | |
2954 | } | |
2955 | } else { | |
2956 | wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portValid, | |
2957 | 1); | |
2958 | } | |
2959 | wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_keyAvailable, 0); | |
2960 | wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_keyDone, 1); | |
2961 | if (sm->wpa == WPA_VERSION_WPA) | |
2962 | sm->PInitAKeys = TRUE; | |
2963 | else | |
2964 | sm->has_GTK = TRUE; | |
2965 | wpa_auth_vlogger(sm->wpa_auth, sm->addr, LOGGER_INFO, | |
2966 | "pairwise key handshake completed (%s)", | |
2967 | sm->wpa == WPA_VERSION_WPA ? "WPA" : "RSN"); | |
2968 | ||
4ec1fd8e | 2969 | #ifdef CONFIG_IEEE80211R_AP |
6fc6879b | 2970 | wpa_ft_push_pmk_r1(sm->wpa_auth, sm->addr); |
4ec1fd8e | 2971 | #endif /* CONFIG_IEEE80211R_AP */ |
6fc6879b JM |
2972 | } |
2973 | ||
2974 | ||
2975 | SM_STEP(WPA_PTK) | |
2976 | { | |
2977 | struct wpa_authenticator *wpa_auth = sm->wpa_auth; | |
2978 | ||
2979 | if (sm->Init) | |
2980 | SM_ENTER(WPA_PTK, INITIALIZE); | |
2981 | else if (sm->Disconnect | |
1aae01fc BG |
2982 | /* || FIX: dot11RSNAConfigSALifetime timeout */) { |
2983 | wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG, | |
2984 | "WPA_PTK: sm->Disconnect"); | |
6fc6879b | 2985 | SM_ENTER(WPA_PTK, DISCONNECT); |
1aae01fc | 2986 | } |
6fc6879b JM |
2987 | else if (sm->DeauthenticationRequest) |
2988 | SM_ENTER(WPA_PTK, DISCONNECTED); | |
2989 | else if (sm->AuthenticationRequest) | |
2990 | SM_ENTER(WPA_PTK, AUTHENTICATION); | |
2991 | else if (sm->ReAuthenticationRequest) | |
2992 | SM_ENTER(WPA_PTK, AUTHENTICATION2); | |
2993 | else if (sm->PTKRequest) | |
2994 | SM_ENTER(WPA_PTK, PTKSTART); | |
2995 | else switch (sm->wpa_ptk_state) { | |
2996 | case WPA_PTK_INITIALIZE: | |
2997 | break; | |
2998 | case WPA_PTK_DISCONNECT: | |
2999 | SM_ENTER(WPA_PTK, DISCONNECTED); | |
3000 | break; | |
3001 | case WPA_PTK_DISCONNECTED: | |
3002 | SM_ENTER(WPA_PTK, INITIALIZE); | |
3003 | break; | |
3004 | case WPA_PTK_AUTHENTICATION: | |
3005 | SM_ENTER(WPA_PTK, AUTHENTICATION2); | |
3006 | break; | |
3007 | case WPA_PTK_AUTHENTICATION2: | |
56586197 | 3008 | if (wpa_key_mgmt_wpa_ieee8021x(sm->wpa_key_mgmt) && |
6fc6879b JM |
3009 | wpa_auth_get_eapol(sm->wpa_auth, sm->addr, |
3010 | WPA_EAPOL_keyRun) > 0) | |
3011 | SM_ENTER(WPA_PTK, INITPMK); | |
a1ea1b45 JM |
3012 | else if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt) || |
3013 | sm->wpa_key_mgmt == WPA_KEY_MGMT_OWE | |
6fc6879b JM |
3014 | /* FIX: && 802.1X::keyRun */) |
3015 | SM_ENTER(WPA_PTK, INITPSK); | |
3016 | break; | |
3017 | case WPA_PTK_INITPMK: | |
3018 | if (wpa_auth_get_eapol(sm->wpa_auth, sm->addr, | |
3019 | WPA_EAPOL_keyAvailable) > 0) | |
3020 | SM_ENTER(WPA_PTK, PTKSTART); | |
3021 | else { | |
3022 | wpa_auth->dot11RSNA4WayHandshakeFailures++; | |
1aae01fc BG |
3023 | wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_INFO, |
3024 | "INITPMK - keyAvailable = false"); | |
6fc6879b JM |
3025 | SM_ENTER(WPA_PTK, DISCONNECT); |
3026 | } | |
3027 | break; | |
3028 | case WPA_PTK_INITPSK: | |
759fd76b JM |
3029 | if (wpa_auth_get_psk(sm->wpa_auth, sm->addr, sm->p2p_dev_addr, |
3030 | NULL)) | |
6fc6879b JM |
3031 | SM_ENTER(WPA_PTK, PTKSTART); |
3032 | else { | |
3033 | wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_INFO, | |
3034 | "no PSK configured for the STA"); | |
3035 | wpa_auth->dot11RSNA4WayHandshakeFailures++; | |
3036 | SM_ENTER(WPA_PTK, DISCONNECT); | |
3037 | } | |
3038 | break; | |
3039 | case WPA_PTK_PTKSTART: | |
3040 | if (sm->EAPOLKeyReceived && !sm->EAPOLKeyRequest && | |
3041 | sm->EAPOLKeyPairwise) | |
3042 | SM_ENTER(WPA_PTK, PTKCALCNEGOTIATING); | |
3043 | else if (sm->TimeoutCtr > | |
41f140d3 | 3044 | sm->wpa_auth->conf.wpa_pairwise_update_count) { |
6fc6879b | 3045 | wpa_auth->dot11RSNA4WayHandshakeFailures++; |
41f140d3 GK |
3046 | wpa_auth_vlogger( |
3047 | sm->wpa_auth, sm->addr, LOGGER_DEBUG, | |
3048 | "PTKSTART: Retry limit %u reached", | |
3049 | sm->wpa_auth->conf.wpa_pairwise_update_count); | |
6fc6879b JM |
3050 | SM_ENTER(WPA_PTK, DISCONNECT); |
3051 | } else if (sm->TimeoutEvt) | |
3052 | SM_ENTER(WPA_PTK, PTKSTART); | |
3053 | break; | |
3054 | case WPA_PTK_PTKCALCNEGOTIATING: | |
3055 | if (sm->MICVerified) | |
3056 | SM_ENTER(WPA_PTK, PTKCALCNEGOTIATING2); | |
3057 | else if (sm->EAPOLKeyReceived && !sm->EAPOLKeyRequest && | |
3058 | sm->EAPOLKeyPairwise) | |
3059 | SM_ENTER(WPA_PTK, PTKCALCNEGOTIATING); | |
3060 | else if (sm->TimeoutEvt) | |
3061 | SM_ENTER(WPA_PTK, PTKSTART); | |
3062 | break; | |
3063 | case WPA_PTK_PTKCALCNEGOTIATING2: | |
3064 | SM_ENTER(WPA_PTK, PTKINITNEGOTIATING); | |
3065 | break; | |
3066 | case WPA_PTK_PTKINITNEGOTIATING: | |
68921e24 JM |
3067 | if (sm->update_snonce) |
3068 | SM_ENTER(WPA_PTK, PTKCALCNEGOTIATING); | |
3069 | else if (sm->EAPOLKeyReceived && !sm->EAPOLKeyRequest && | |
3070 | sm->EAPOLKeyPairwise && sm->MICVerified) | |
6fc6879b JM |
3071 | SM_ENTER(WPA_PTK, PTKINITDONE); |
3072 | else if (sm->TimeoutCtr > | |
41f140d3 | 3073 | sm->wpa_auth->conf.wpa_pairwise_update_count) { |
6fc6879b | 3074 | wpa_auth->dot11RSNA4WayHandshakeFailures++; |
41f140d3 GK |
3075 | wpa_auth_vlogger( |
3076 | sm->wpa_auth, sm->addr, LOGGER_DEBUG, | |
3077 | "PTKINITNEGOTIATING: Retry limit %u reached", | |
3078 | sm->wpa_auth->conf.wpa_pairwise_update_count); | |
6fc6879b JM |
3079 | SM_ENTER(WPA_PTK, DISCONNECT); |
3080 | } else if (sm->TimeoutEvt) | |
3081 | SM_ENTER(WPA_PTK, PTKINITNEGOTIATING); | |
3082 | break; | |
3083 | case WPA_PTK_PTKINITDONE: | |
3084 | break; | |
3085 | } | |
3086 | } | |
3087 | ||
3088 | ||
3089 | SM_STATE(WPA_PTK_GROUP, IDLE) | |
3090 | { | |
3091 | SM_ENTRY_MA(WPA_PTK_GROUP, IDLE, wpa_ptk_group); | |
3092 | if (sm->Init) { | |
3093 | /* Init flag is not cleared here, so avoid busy | |
3094 | * loop by claiming nothing changed. */ | |
3095 | sm->changed = FALSE; | |
3096 | } | |
3097 | sm->GTimeoutCtr = 0; | |
3098 | } | |
3099 | ||
3100 | ||
3101 | SM_STATE(WPA_PTK_GROUP, REKEYNEGOTIATING) | |
3102 | { | |
3103 | u8 rsc[WPA_KEY_RSC_LEN]; | |
3104 | struct wpa_group *gsm = sm->group; | |
e41e4f9e JM |
3105 | const u8 *kde; |
3106 | u8 *kde_buf = NULL, *pos, hdr[2]; | |
6fc6879b | 3107 | size_t kde_len; |
83421850 | 3108 | u8 *gtk, dummy_gtk[32]; |
6fc6879b JM |
3109 | |
3110 | SM_ENTRY_MA(WPA_PTK_GROUP, REKEYNEGOTIATING, wpa_ptk_group); | |
bae61562 JM |
3111 | |
3112 | sm->GTimeoutCtr++; | |
41f140d3 | 3113 | if (sm->GTimeoutCtr > sm->wpa_auth->conf.wpa_group_update_count) { |
bae61562 JM |
3114 | /* No point in sending the EAPOL-Key - we will disconnect |
3115 | * immediately following this. */ | |
3116 | return; | |
3117 | } | |
3118 | ||
6fc6879b JM |
3119 | if (sm->wpa == WPA_VERSION_WPA) |
3120 | sm->PInitAKeys = FALSE; | |
3121 | sm->TimeoutEvt = FALSE; | |
3122 | /* Send EAPOL(1, 1, 1, !Pair, G, RSC, GNonce, MIC(PTK), GTK[GN]) */ | |
3123 | os_memset(rsc, 0, WPA_KEY_RSC_LEN); | |
3124 | if (gsm->wpa_group_state == WPA_GROUP_SETKEYSDONE) | |
3125 | wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN, rsc); | |
3126 | wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG, | |
3127 | "sending 1/2 msg of Group Key Handshake"); | |
3128 | ||
83421850 JM |
3129 | gtk = gsm->GTK[gsm->GN - 1]; |
3130 | if (sm->wpa_auth->conf.disable_gtk) { | |
3131 | /* | |
3132 | * Provide unique random GTK to each STA to prevent use | |
3133 | * of GTK in the BSS. | |
3134 | */ | |
3135 | if (random_get_bytes(dummy_gtk, gsm->GTK_len) < 0) | |
3136 | return; | |
3137 | gtk = dummy_gtk; | |
3138 | } | |
6fc6879b JM |
3139 | if (sm->wpa == WPA_VERSION_WPA2) { |
3140 | kde_len = 2 + RSN_SELECTOR_LEN + 2 + gsm->GTK_len + | |
3141 | ieee80211w_kde_len(sm); | |
e41e4f9e JM |
3142 | kde_buf = os_malloc(kde_len); |
3143 | if (kde_buf == NULL) | |
6fc6879b JM |
3144 | return; |
3145 | ||
e41e4f9e | 3146 | kde = pos = kde_buf; |
6fc6879b JM |
3147 | hdr[0] = gsm->GN & 0x03; |
3148 | hdr[1] = 0; | |
3149 | pos = wpa_add_kde(pos, RSN_KEY_DATA_GROUPKEY, hdr, 2, | |
83421850 | 3150 | gtk, gsm->GTK_len); |
6fc6879b | 3151 | pos = ieee80211w_kde_add(sm, pos); |
e41e4f9e | 3152 | kde_len = pos - kde; |
6fc6879b | 3153 | } else { |
83421850 | 3154 | kde = gtk; |
e41e4f9e | 3155 | kde_len = gsm->GTK_len; |
6fc6879b JM |
3156 | } |
3157 | ||
3158 | wpa_send_eapol(sm->wpa_auth, sm, | |
b729fd8d JM |
3159 | WPA_KEY_INFO_SECURE | |
3160 | (wpa_mic_len(sm->wpa_key_mgmt) ? WPA_KEY_INFO_MIC : 0) | | |
6fc6879b JM |
3161 | WPA_KEY_INFO_ACK | |
3162 | (!sm->Pair ? WPA_KEY_INFO_INSTALL : 0), | |
b0fb2be7 | 3163 | rsc, NULL, kde, kde_len, gsm->GN, 1); |
e41e4f9e JM |
3164 | |
3165 | os_free(kde_buf); | |
6fc6879b JM |
3166 | } |
3167 | ||
3168 | ||
3169 | SM_STATE(WPA_PTK_GROUP, REKEYESTABLISHED) | |
3170 | { | |
3171 | SM_ENTRY_MA(WPA_PTK_GROUP, REKEYESTABLISHED, wpa_ptk_group); | |
3172 | sm->EAPOLKeyReceived = FALSE; | |
3173 | if (sm->GUpdateStationKeys) | |
3174 | sm->group->GKeyDoneStations--; | |
3175 | sm->GUpdateStationKeys = FALSE; | |
3176 | sm->GTimeoutCtr = 0; | |
3177 | /* FIX: MLME.SetProtection.Request(TA, Tx_Rx) */ | |
3178 | wpa_auth_vlogger(sm->wpa_auth, sm->addr, LOGGER_INFO, | |
3179 | "group key handshake completed (%s)", | |
3180 | sm->wpa == WPA_VERSION_WPA ? "WPA" : "RSN"); | |
3181 | sm->has_GTK = TRUE; | |
3182 | } | |
3183 | ||
3184 | ||
3185 | SM_STATE(WPA_PTK_GROUP, KEYERROR) | |
3186 | { | |
3187 | SM_ENTRY_MA(WPA_PTK_GROUP, KEYERROR, wpa_ptk_group); | |
3188 | if (sm->GUpdateStationKeys) | |
3189 | sm->group->GKeyDoneStations--; | |
3190 | sm->GUpdateStationKeys = FALSE; | |
3191 | sm->Disconnect = TRUE; | |
4bb9b674 GK |
3192 | wpa_auth_vlogger(sm->wpa_auth, sm->addr, LOGGER_INFO, |
3193 | "group key handshake failed (%s) after %u tries", | |
3194 | sm->wpa == WPA_VERSION_WPA ? "WPA" : "RSN", | |
3195 | sm->wpa_auth->conf.wpa_group_update_count); | |
6fc6879b JM |
3196 | } |
3197 | ||
3198 | ||
3199 | SM_STEP(WPA_PTK_GROUP) | |
3200 | { | |
9663596f | 3201 | if (sm->Init || sm->PtkGroupInit) { |
6fc6879b | 3202 | SM_ENTER(WPA_PTK_GROUP, IDLE); |
9663596f JM |
3203 | sm->PtkGroupInit = FALSE; |
3204 | } else switch (sm->wpa_ptk_group_state) { | |
6fc6879b JM |
3205 | case WPA_PTK_GROUP_IDLE: |
3206 | if (sm->GUpdateStationKeys || | |
3207 | (sm->wpa == WPA_VERSION_WPA && sm->PInitAKeys)) | |
3208 | SM_ENTER(WPA_PTK_GROUP, REKEYNEGOTIATING); | |
3209 | break; | |
3210 | case WPA_PTK_GROUP_REKEYNEGOTIATING: | |
3211 | if (sm->EAPOLKeyReceived && !sm->EAPOLKeyRequest && | |
3212 | !sm->EAPOLKeyPairwise && sm->MICVerified) | |
3213 | SM_ENTER(WPA_PTK_GROUP, REKEYESTABLISHED); | |
3214 | else if (sm->GTimeoutCtr > | |
41f140d3 | 3215 | sm->wpa_auth->conf.wpa_group_update_count) |
6fc6879b JM |
3216 | SM_ENTER(WPA_PTK_GROUP, KEYERROR); |
3217 | else if (sm->TimeoutEvt) | |
3218 | SM_ENTER(WPA_PTK_GROUP, REKEYNEGOTIATING); | |
3219 | break; | |
3220 | case WPA_PTK_GROUP_KEYERROR: | |
3221 | SM_ENTER(WPA_PTK_GROUP, IDLE); | |
3222 | break; | |
3223 | case WPA_PTK_GROUP_REKEYESTABLISHED: | |
3224 | SM_ENTER(WPA_PTK_GROUP, IDLE); | |
3225 | break; | |
3226 | } | |
3227 | } | |
3228 | ||
3229 | ||
3230 | static int wpa_gtk_update(struct wpa_authenticator *wpa_auth, | |
3231 | struct wpa_group *group) | |
3232 | { | |
3233 | int ret = 0; | |
3234 | ||
6fc6879b JM |
3235 | os_memcpy(group->GNonce, group->Counter, WPA_NONCE_LEN); |
3236 | inc_byte_array(group->Counter, WPA_NONCE_LEN); | |
3c7302c2 JM |
3237 | if (wpa_gmk_to_gtk(group->GMK, "Group key expansion", |
3238 | wpa_auth->addr, group->GNonce, | |
3239 | group->GTK[group->GN - 1], group->GTK_len) < 0) | |
3240 | ret = -1; | |
5cb9d5c3 JM |
3241 | wpa_hexdump_key(MSG_DEBUG, "GTK", |
3242 | group->GTK[group->GN - 1], group->GTK_len); | |
6fc6879b JM |
3243 | |
3244 | #ifdef CONFIG_IEEE80211W | |
70f8cc8e | 3245 | if (wpa_auth->conf.ieee80211w != NO_MGMT_FRAME_PROTECTION) { |
8dd9f9cd JM |
3246 | size_t len; |
3247 | len = wpa_cipher_key_len(wpa_auth->conf.group_mgmt_cipher); | |
5cb9d5c3 JM |
3248 | os_memcpy(group->GNonce, group->Counter, WPA_NONCE_LEN); |
3249 | inc_byte_array(group->Counter, WPA_NONCE_LEN); | |
3c7302c2 JM |
3250 | if (wpa_gmk_to_gtk(group->GMK, "IGTK key expansion", |
3251 | wpa_auth->addr, group->GNonce, | |
8dd9f9cd | 3252 | group->IGTK[group->GN_igtk - 4], len) < 0) |
3c7302c2 | 3253 | ret = -1; |
6fc6879b | 3254 | wpa_hexdump_key(MSG_DEBUG, "IGTK", |
8dd9f9cd | 3255 | group->IGTK[group->GN_igtk - 4], len); |
6fc6879b JM |
3256 | } |
3257 | #endif /* CONFIG_IEEE80211W */ | |
3258 | ||
3259 | return ret; | |
3260 | } | |
3261 | ||
3262 | ||
3263 | static void wpa_group_gtk_init(struct wpa_authenticator *wpa_auth, | |
3264 | struct wpa_group *group) | |
3265 | { | |
3266 | wpa_printf(MSG_DEBUG, "WPA: group state machine entering state " | |
3267 | "GTK_INIT (VLAN-ID %d)", group->vlan_id); | |
3268 | group->changed = FALSE; /* GInit is not cleared here; avoid loop */ | |
3269 | group->wpa_group_state = WPA_GROUP_GTK_INIT; | |
3270 | ||
3271 | /* GTK[0..N] = 0 */ | |
3272 | os_memset(group->GTK, 0, sizeof(group->GTK)); | |
3273 | group->GN = 1; | |
3274 | group->GM = 2; | |
3275 | #ifdef CONFIG_IEEE80211W | |
3276 | group->GN_igtk = 4; | |
3277 | group->GM_igtk = 5; | |
3278 | #endif /* CONFIG_IEEE80211W */ | |
3279 | /* GTK[GN] = CalcGTK() */ | |
3280 | wpa_gtk_update(wpa_auth, group); | |
3281 | } | |
3282 | ||
3283 | ||
3284 | static int wpa_group_update_sta(struct wpa_state_machine *sm, void *ctx) | |
3285 | { | |
473b6f22 MB |
3286 | if (ctx != NULL && ctx != sm->group) |
3287 | return 0; | |
3288 | ||
6fc6879b JM |
3289 | if (sm->wpa_ptk_state != WPA_PTK_PTKINITDONE) { |
3290 | wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG, | |
3291 | "Not in PTKINITDONE; skip Group Key update"); | |
3c183894 | 3292 | sm->GUpdateStationKeys = FALSE; |
6fc6879b JM |
3293 | return 0; |
3294 | } | |
9663596f JM |
3295 | if (sm->GUpdateStationKeys) { |
3296 | /* | |
3c183894 JM |
3297 | * This should not really happen, so add a debug log entry. |
3298 | * Since we clear the GKeyDoneStations before the loop, the | |
3299 | * station needs to be counted here anyway. | |
9663596f JM |
3300 | */ |
3301 | wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG, | |
3c183894 JM |
3302 | "GUpdateStationKeys was already set when " |
3303 | "marking station for GTK rekeying"); | |
9663596f | 3304 | } |
3c183894 | 3305 | |
ad3872a3 | 3306 | /* Do not rekey GTK/IGTK when STA is in WNM-Sleep Mode */ |
d32d94db XC |
3307 | if (sm->is_wnmsleep) |
3308 | return 0; | |
d32d94db | 3309 | |
3c183894 JM |
3310 | sm->group->GKeyDoneStations++; |
3311 | sm->GUpdateStationKeys = TRUE; | |
3312 | ||
6fc6879b JM |
3313 | wpa_sm_step(sm); |
3314 | return 0; | |
3315 | } | |
3316 | ||
3317 | ||
ad3872a3 JM |
3318 | #ifdef CONFIG_WNM |
3319 | /* update GTK when exiting WNM-Sleep Mode */ | |
d32d94db XC |
3320 | void wpa_wnmsleep_rekey_gtk(struct wpa_state_machine *sm) |
3321 | { | |
629edfef | 3322 | if (sm == NULL || sm->is_wnmsleep) |
d32d94db XC |
3323 | return; |
3324 | ||
3325 | wpa_group_update_sta(sm, NULL); | |
3326 | } | |
3327 | ||
3328 | ||
3329 | void wpa_set_wnmsleep(struct wpa_state_machine *sm, int flag) | |
3330 | { | |
629edfef JM |
3331 | if (sm) |
3332 | sm->is_wnmsleep = !!flag; | |
d32d94db XC |
3333 | } |
3334 | ||
3335 | ||
3336 | int wpa_wnmsleep_gtk_subelem(struct wpa_state_machine *sm, u8 *pos) | |
3337 | { | |
d32d94db | 3338 | struct wpa_group *gsm = sm->group; |
835822d4 | 3339 | u8 *start = pos; |
d32d94db XC |
3340 | |
3341 | /* | |
835822d4 | 3342 | * GTK subelement: |
d32d94db | 3343 | * Sub-elem ID[1] | Length[1] | Key Info[2] | Key Length[1] | RSC[8] | |
835822d4 | 3344 | * Key[5..32] |
d32d94db | 3345 | */ |
835822d4 JM |
3346 | *pos++ = WNM_SLEEP_SUBELEM_GTK; |
3347 | *pos++ = 11 + gsm->GTK_len; | |
d32d94db | 3348 | /* Key ID in B0-B1 of Key Info */ |
835822d4 JM |
3349 | WPA_PUT_LE16(pos, gsm->GN & 0x03); |
3350 | pos += 2; | |
3351 | *pos++ = gsm->GTK_len; | |
3352 | if (wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN, pos) != 0) | |
d32d94db | 3353 | return 0; |
835822d4 JM |
3354 | pos += 8; |
3355 | os_memcpy(pos, gsm->GTK[gsm->GN - 1], gsm->GTK_len); | |
3356 | pos += gsm->GTK_len; | |
d32d94db | 3357 | |
835822d4 JM |
3358 | wpa_printf(MSG_DEBUG, "WNM: GTK Key ID %u in WNM-Sleep Mode exit", |
3359 | gsm->GN); | |
3360 | wpa_hexdump_key(MSG_DEBUG, "WNM: GTK in WNM-Sleep Mode exit", | |
d32d94db | 3361 | gsm->GTK[gsm->GN - 1], gsm->GTK_len); |
d32d94db | 3362 | |
835822d4 | 3363 | return pos - start; |
d32d94db XC |
3364 | } |
3365 | ||
3366 | ||
3367 | #ifdef CONFIG_IEEE80211W | |
3368 | int wpa_wnmsleep_igtk_subelem(struct wpa_state_machine *sm, u8 *pos) | |
3369 | { | |
d32d94db | 3370 | struct wpa_group *gsm = sm->group; |
835822d4 | 3371 | u8 *start = pos; |
8dd9f9cd | 3372 | size_t len = wpa_cipher_key_len(sm->wpa_auth->conf.group_mgmt_cipher); |
d32d94db | 3373 | |
835822d4 JM |
3374 | /* |
3375 | * IGTK subelement: | |
3376 | * Sub-elem ID[1] | Length[1] | KeyID[2] | PN[6] | Key[16] | |
3377 | */ | |
3378 | *pos++ = WNM_SLEEP_SUBELEM_IGTK; | |
8dd9f9cd | 3379 | *pos++ = 2 + 6 + len; |
835822d4 JM |
3380 | WPA_PUT_LE16(pos, gsm->GN_igtk); |
3381 | pos += 2; | |
3382 | if (wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN_igtk, pos) != 0) | |
d32d94db | 3383 | return 0; |
835822d4 | 3384 | pos += 6; |
d32d94db | 3385 | |
8dd9f9cd JM |
3386 | os_memcpy(pos, gsm->IGTK[gsm->GN_igtk - 4], len); |
3387 | pos += len; | |
d32d94db | 3388 | |
835822d4 JM |
3389 | wpa_printf(MSG_DEBUG, "WNM: IGTK Key ID %u in WNM-Sleep Mode exit", |
3390 | gsm->GN_igtk); | |
3391 | wpa_hexdump_key(MSG_DEBUG, "WNM: IGTK in WNM-Sleep Mode exit", | |
8dd9f9cd | 3392 | gsm->IGTK[gsm->GN_igtk - 4], len); |
d32d94db | 3393 | |
835822d4 | 3394 | return pos - start; |
d32d94db XC |
3395 | } |
3396 | #endif /* CONFIG_IEEE80211W */ | |
ad3872a3 | 3397 | #endif /* CONFIG_WNM */ |
d32d94db XC |
3398 | |
3399 | ||
6fc6879b JM |
3400 | static void wpa_group_setkeys(struct wpa_authenticator *wpa_auth, |
3401 | struct wpa_group *group) | |
3402 | { | |
3403 | int tmp; | |
3404 | ||
3405 | wpa_printf(MSG_DEBUG, "WPA: group state machine entering state " | |
3406 | "SETKEYS (VLAN-ID %d)", group->vlan_id); | |
3407 | group->changed = TRUE; | |
3408 | group->wpa_group_state = WPA_GROUP_SETKEYS; | |
3409 | group->GTKReKey = FALSE; | |
3410 | tmp = group->GM; | |
3411 | group->GM = group->GN; | |
3412 | group->GN = tmp; | |
3413 | #ifdef CONFIG_IEEE80211W | |
3414 | tmp = group->GM_igtk; | |
3415 | group->GM_igtk = group->GN_igtk; | |
3416 | group->GN_igtk = tmp; | |
3417 | #endif /* CONFIG_IEEE80211W */ | |
3418 | /* "GKeyDoneStations = GNoStations" is done in more robust way by | |
3419 | * counting the STAs that are marked with GUpdateStationKeys instead of | |
3420 | * including all STAs that could be in not-yet-completed state. */ | |
3421 | wpa_gtk_update(wpa_auth, group); | |
3422 | ||
3c183894 JM |
3423 | if (group->GKeyDoneStations) { |
3424 | wpa_printf(MSG_DEBUG, "wpa_group_setkeys: Unexpected " | |
3425 | "GKeyDoneStations=%d when starting new GTK rekey", | |
3426 | group->GKeyDoneStations); | |
3427 | group->GKeyDoneStations = 0; | |
3428 | } | |
473b6f22 | 3429 | wpa_auth_for_each_sta(wpa_auth, wpa_group_update_sta, group); |
6fc6879b JM |
3430 | wpa_printf(MSG_DEBUG, "wpa_group_setkeys: GKeyDoneStations=%d", |
3431 | group->GKeyDoneStations); | |
3432 | } | |
3433 | ||
3434 | ||
1bdb7ab3 JM |
3435 | static int wpa_group_config_group_keys(struct wpa_authenticator *wpa_auth, |
3436 | struct wpa_group *group) | |
6fc6879b | 3437 | { |
3c7302c2 JM |
3438 | int ret = 0; |
3439 | ||
3c7302c2 | 3440 | if (wpa_auth_set_key(wpa_auth, group->vlan_id, |
c3550295 | 3441 | wpa_cipher_to_alg(wpa_auth->conf.wpa_group), |
0382097e JM |
3442 | broadcast_ether_addr, group->GN, |
3443 | group->GTK[group->GN - 1], group->GTK_len) < 0) | |
3c7302c2 | 3444 | ret = -1; |
6fc6879b JM |
3445 | |
3446 | #ifdef CONFIG_IEEE80211W | |
8dd9f9cd JM |
3447 | if (wpa_auth->conf.ieee80211w != NO_MGMT_FRAME_PROTECTION) { |
3448 | enum wpa_alg alg; | |
3449 | size_t len; | |
3450 | ||
3451 | alg = wpa_cipher_to_alg(wpa_auth->conf.group_mgmt_cipher); | |
3452 | len = wpa_cipher_key_len(wpa_auth->conf.group_mgmt_cipher); | |
3453 | ||
3454 | if (ret == 0 && | |
3455 | wpa_auth_set_key(wpa_auth, group->vlan_id, alg, | |
3456 | broadcast_ether_addr, group->GN_igtk, | |
3457 | group->IGTK[group->GN_igtk - 4], len) < 0) | |
3458 | ret = -1; | |
3459 | } | |
6fc6879b | 3460 | #endif /* CONFIG_IEEE80211W */ |
3c7302c2 JM |
3461 | |
3462 | return ret; | |
6fc6879b JM |
3463 | } |
3464 | ||
3465 | ||
7d7f7be2 JM |
3466 | static int wpa_group_disconnect_cb(struct wpa_state_machine *sm, void *ctx) |
3467 | { | |
3468 | if (sm->group == ctx) { | |
3469 | wpa_printf(MSG_DEBUG, "WPA: Mark STA " MACSTR | |
3470 | " for discconnection due to fatal failure", | |
3471 | MAC2STR(sm->addr)); | |
3472 | sm->Disconnect = TRUE; | |
3473 | } | |
3474 | ||
3475 | return 0; | |
3476 | } | |
3477 | ||
3478 | ||
3479 | static void wpa_group_fatal_failure(struct wpa_authenticator *wpa_auth, | |
3480 | struct wpa_group *group) | |
3481 | { | |
3482 | wpa_printf(MSG_DEBUG, "WPA: group state machine entering state FATAL_FAILURE"); | |
3483 | group->changed = TRUE; | |
3484 | group->wpa_group_state = WPA_GROUP_FATAL_FAILURE; | |
3485 | wpa_auth_for_each_sta(wpa_auth, wpa_group_disconnect_cb, group); | |
3486 | } | |
3487 | ||
3488 | ||
1bdb7ab3 JM |
3489 | static int wpa_group_setkeysdone(struct wpa_authenticator *wpa_auth, |
3490 | struct wpa_group *group) | |
3491 | { | |
3492 | wpa_printf(MSG_DEBUG, "WPA: group state machine entering state " | |
3493 | "SETKEYSDONE (VLAN-ID %d)", group->vlan_id); | |
3494 | group->changed = TRUE; | |
3495 | group->wpa_group_state = WPA_GROUP_SETKEYSDONE; | |
3496 | ||
7d7f7be2 JM |
3497 | if (wpa_group_config_group_keys(wpa_auth, group) < 0) { |
3498 | wpa_group_fatal_failure(wpa_auth, group); | |
1bdb7ab3 | 3499 | return -1; |
7d7f7be2 | 3500 | } |
1bdb7ab3 JM |
3501 | |
3502 | return 0; | |
3503 | } | |
3504 | ||
3505 | ||
6fc6879b JM |
3506 | static void wpa_group_sm_step(struct wpa_authenticator *wpa_auth, |
3507 | struct wpa_group *group) | |
3508 | { | |
3509 | if (group->GInit) { | |
3510 | wpa_group_gtk_init(wpa_auth, group); | |
7d7f7be2 JM |
3511 | } else if (group->wpa_group_state == WPA_GROUP_FATAL_FAILURE) { |
3512 | /* Do not allow group operations */ | |
6fc6879b JM |
3513 | } else if (group->wpa_group_state == WPA_GROUP_GTK_INIT && |
3514 | group->GTKAuthenticator) { | |
3515 | wpa_group_setkeysdone(wpa_auth, group); | |
3516 | } else if (group->wpa_group_state == WPA_GROUP_SETKEYSDONE && | |
3517 | group->GTKReKey) { | |
3518 | wpa_group_setkeys(wpa_auth, group); | |
3519 | } else if (group->wpa_group_state == WPA_GROUP_SETKEYS) { | |
3520 | if (group->GKeyDoneStations == 0) | |
3521 | wpa_group_setkeysdone(wpa_auth, group); | |
3522 | else if (group->GTKReKey) | |
3523 | wpa_group_setkeys(wpa_auth, group); | |
3524 | } | |
3525 | } | |
3526 | ||
3527 | ||
e4a6ea1d | 3528 | static int wpa_sm_step(struct wpa_state_machine *sm) |
6fc6879b JM |
3529 | { |
3530 | if (sm == NULL) | |
e4a6ea1d | 3531 | return 0; |
6fc6879b JM |
3532 | |
3533 | if (sm->in_step_loop) { | |
3534 | /* This should not happen, but if it does, make sure we do not | |
3535 | * end up freeing the state machine too early by exiting the | |
3536 | * recursive call. */ | |
3537 | wpa_printf(MSG_ERROR, "WPA: wpa_sm_step() called recursively"); | |
e4a6ea1d | 3538 | return 0; |
6fc6879b JM |
3539 | } |
3540 | ||
3541 | sm->in_step_loop = 1; | |
3542 | do { | |
3543 | if (sm->pending_deinit) | |
3544 | break; | |
3545 | ||
3546 | sm->changed = FALSE; | |
3547 | sm->wpa_auth->group->changed = FALSE; | |
3548 | ||
3549 | SM_STEP_RUN(WPA_PTK); | |
3550 | if (sm->pending_deinit) | |
3551 | break; | |
3552 | SM_STEP_RUN(WPA_PTK_GROUP); | |
3553 | if (sm->pending_deinit) | |
3554 | break; | |
3555 | wpa_group_sm_step(sm->wpa_auth, sm->group); | |
3556 | } while (sm->changed || sm->wpa_auth->group->changed); | |
3557 | sm->in_step_loop = 0; | |
3558 | ||
3559 | if (sm->pending_deinit) { | |
3560 | wpa_printf(MSG_DEBUG, "WPA: Completing pending STA state " | |
3561 | "machine deinit for " MACSTR, MAC2STR(sm->addr)); | |
3562 | wpa_free_sta_sm(sm); | |
e4a6ea1d | 3563 | return 1; |
6fc6879b | 3564 | } |
e4a6ea1d | 3565 | return 0; |
6fc6879b JM |
3566 | } |
3567 | ||
3568 | ||
3569 | static void wpa_sm_call_step(void *eloop_ctx, void *timeout_ctx) | |
3570 | { | |
3571 | struct wpa_state_machine *sm = eloop_ctx; | |
3572 | wpa_sm_step(sm); | |
3573 | } | |
3574 | ||
3575 | ||
3576 | void wpa_auth_sm_notify(struct wpa_state_machine *sm) | |
3577 | { | |
3578 | if (sm == NULL) | |
3579 | return; | |
3580 | eloop_register_timeout(0, 0, wpa_sm_call_step, sm, NULL); | |
3581 | } | |
3582 | ||
3583 | ||
3584 | void wpa_gtk_rekey(struct wpa_authenticator *wpa_auth) | |
3585 | { | |
3586 | int tmp, i; | |
3587 | struct wpa_group *group; | |
3588 | ||
3589 | if (wpa_auth == NULL) | |
3590 | return; | |
3591 | ||
3592 | group = wpa_auth->group; | |
3593 | ||
3594 | for (i = 0; i < 2; i++) { | |
3595 | tmp = group->GM; | |
3596 | group->GM = group->GN; | |
3597 | group->GN = tmp; | |
3598 | #ifdef CONFIG_IEEE80211W | |
3599 | tmp = group->GM_igtk; | |
3600 | group->GM_igtk = group->GN_igtk; | |
3601 | group->GN_igtk = tmp; | |
3602 | #endif /* CONFIG_IEEE80211W */ | |
3603 | wpa_gtk_update(wpa_auth, group); | |
9354e594 | 3604 | wpa_group_config_group_keys(wpa_auth, group); |
6fc6879b JM |
3605 | } |
3606 | } | |
3607 | ||
3608 | ||
a6da824b | 3609 | static const char * wpa_bool_txt(int val) |
6fc6879b | 3610 | { |
a6da824b | 3611 | return val ? "TRUE" : "FALSE"; |
6fc6879b JM |
3612 | } |
3613 | ||
3614 | ||
6fc6879b JM |
3615 | #define RSN_SUITE "%02x-%02x-%02x-%d" |
3616 | #define RSN_SUITE_ARG(s) \ | |
3617 | ((s) >> 24) & 0xff, ((s) >> 16) & 0xff, ((s) >> 8) & 0xff, (s) & 0xff | |
3618 | ||
3619 | int wpa_get_mib(struct wpa_authenticator *wpa_auth, char *buf, size_t buflen) | |
3620 | { | |
3621 | int len = 0, ret; | |
3622 | char pmkid_txt[PMKID_LEN * 2 + 1]; | |
8ce58ceb IH |
3623 | #ifdef CONFIG_RSN_PREAUTH |
3624 | const int preauth = 1; | |
3625 | #else /* CONFIG_RSN_PREAUTH */ | |
3626 | const int preauth = 0; | |
3627 | #endif /* CONFIG_RSN_PREAUTH */ | |
6fc6879b JM |
3628 | |
3629 | if (wpa_auth == NULL) | |
3630 | return len; | |
3631 | ||
3632 | ret = os_snprintf(buf + len, buflen - len, | |
3633 | "dot11RSNAOptionImplemented=TRUE\n" | |
8ce58ceb | 3634 | "dot11RSNAPreauthenticationImplemented=%s\n" |
6fc6879b JM |
3635 | "dot11RSNAEnabled=%s\n" |
3636 | "dot11RSNAPreauthenticationEnabled=%s\n", | |
8ce58ceb | 3637 | wpa_bool_txt(preauth), |
6fc6879b JM |
3638 | wpa_bool_txt(wpa_auth->conf.wpa & WPA_PROTO_RSN), |
3639 | wpa_bool_txt(wpa_auth->conf.rsn_preauth)); | |
d85e1fc8 | 3640 | if (os_snprintf_error(buflen - len, ret)) |
6fc6879b JM |
3641 | return len; |
3642 | len += ret; | |
3643 | ||
3644 | wpa_snprintf_hex(pmkid_txt, sizeof(pmkid_txt), | |
3645 | wpa_auth->dot11RSNAPMKIDUsed, PMKID_LEN); | |
3646 | ||
3647 | ret = os_snprintf( | |
3648 | buf + len, buflen - len, | |
3649 | "dot11RSNAConfigVersion=%u\n" | |
3650 | "dot11RSNAConfigPairwiseKeysSupported=9999\n" | |
3651 | /* FIX: dot11RSNAConfigGroupCipher */ | |
3652 | /* FIX: dot11RSNAConfigGroupRekeyMethod */ | |
3653 | /* FIX: dot11RSNAConfigGroupRekeyTime */ | |
3654 | /* FIX: dot11RSNAConfigGroupRekeyPackets */ | |
3655 | "dot11RSNAConfigGroupRekeyStrict=%u\n" | |
3656 | "dot11RSNAConfigGroupUpdateCount=%u\n" | |
3657 | "dot11RSNAConfigPairwiseUpdateCount=%u\n" | |
3658 | "dot11RSNAConfigGroupCipherSize=%u\n" | |
3659 | "dot11RSNAConfigPMKLifetime=%u\n" | |
3660 | "dot11RSNAConfigPMKReauthThreshold=%u\n" | |
3661 | "dot11RSNAConfigNumberOfPTKSAReplayCounters=0\n" | |
3662 | "dot11RSNAConfigSATimeout=%u\n" | |
3663 | "dot11RSNAAuthenticationSuiteSelected=" RSN_SUITE "\n" | |
3664 | "dot11RSNAPairwiseCipherSelected=" RSN_SUITE "\n" | |
3665 | "dot11RSNAGroupCipherSelected=" RSN_SUITE "\n" | |
3666 | "dot11RSNAPMKIDUsed=%s\n" | |
3667 | "dot11RSNAAuthenticationSuiteRequested=" RSN_SUITE "\n" | |
3668 | "dot11RSNAPairwiseCipherRequested=" RSN_SUITE "\n" | |
3669 | "dot11RSNAGroupCipherRequested=" RSN_SUITE "\n" | |
3670 | "dot11RSNATKIPCounterMeasuresInvoked=%u\n" | |
3671 | "dot11RSNA4WayHandshakeFailures=%u\n" | |
3672 | "dot11RSNAConfigNumberOfGTKSAReplayCounters=0\n", | |
3673 | RSN_VERSION, | |
3674 | !!wpa_auth->conf.wpa_strict_rekey, | |
41f140d3 GK |
3675 | wpa_auth->conf.wpa_group_update_count, |
3676 | wpa_auth->conf.wpa_pairwise_update_count, | |
c3550295 | 3677 | wpa_cipher_key_len(wpa_auth->conf.wpa_group) * 8, |
6fc6879b JM |
3678 | dot11RSNAConfigPMKLifetime, |
3679 | dot11RSNAConfigPMKReauthThreshold, | |
3680 | dot11RSNAConfigSATimeout, | |
3681 | RSN_SUITE_ARG(wpa_auth->dot11RSNAAuthenticationSuiteSelected), | |
3682 | RSN_SUITE_ARG(wpa_auth->dot11RSNAPairwiseCipherSelected), | |
3683 | RSN_SUITE_ARG(wpa_auth->dot11RSNAGroupCipherSelected), | |
3684 | pmkid_txt, | |
3685 | RSN_SUITE_ARG(wpa_auth->dot11RSNAAuthenticationSuiteRequested), | |
3686 | RSN_SUITE_ARG(wpa_auth->dot11RSNAPairwiseCipherRequested), | |
3687 | RSN_SUITE_ARG(wpa_auth->dot11RSNAGroupCipherRequested), | |
3688 | wpa_auth->dot11RSNATKIPCounterMeasuresInvoked, | |
3689 | wpa_auth->dot11RSNA4WayHandshakeFailures); | |
d85e1fc8 | 3690 | if (os_snprintf_error(buflen - len, ret)) |
6fc6879b JM |
3691 | return len; |
3692 | len += ret; | |
3693 | ||
3694 | /* TODO: dot11RSNAConfigPairwiseCiphersTable */ | |
3695 | /* TODO: dot11RSNAConfigAuthenticationSuitesTable */ | |
3696 | ||
3697 | /* Private MIB */ | |
3698 | ret = os_snprintf(buf + len, buflen - len, "hostapdWPAGroupState=%d\n", | |
3699 | wpa_auth->group->wpa_group_state); | |
d85e1fc8 | 3700 | if (os_snprintf_error(buflen - len, ret)) |
6fc6879b JM |
3701 | return len; |
3702 | len += ret; | |
3703 | ||
3704 | return len; | |
3705 | } | |
3706 | ||
3707 | ||
3708 | int wpa_get_mib_sta(struct wpa_state_machine *sm, char *buf, size_t buflen) | |
3709 | { | |
3710 | int len = 0, ret; | |
3711 | u32 pairwise = 0; | |
3712 | ||
3713 | if (sm == NULL) | |
3714 | return 0; | |
3715 | ||
3716 | /* TODO: FF-FF-FF-FF-FF-FF entry for broadcast/multicast stats */ | |
3717 | ||
3718 | /* dot11RSNAStatsEntry */ | |
3719 | ||
c3550295 JM |
3720 | pairwise = wpa_cipher_to_suite(sm->wpa == WPA_VERSION_WPA2 ? |
3721 | WPA_PROTO_RSN : WPA_PROTO_WPA, | |
3722 | sm->pairwise); | |
3723 | if (pairwise == 0) | |
6fc6879b JM |
3724 | return 0; |
3725 | ||
3726 | ret = os_snprintf( | |
3727 | buf + len, buflen - len, | |
3728 | /* TODO: dot11RSNAStatsIndex */ | |
3729 | "dot11RSNAStatsSTAAddress=" MACSTR "\n" | |
3730 | "dot11RSNAStatsVersion=1\n" | |
3731 | "dot11RSNAStatsSelectedPairwiseCipher=" RSN_SUITE "\n" | |
3732 | /* TODO: dot11RSNAStatsTKIPICVErrors */ | |
3733 | "dot11RSNAStatsTKIPLocalMICFailures=%u\n" | |
d9040cdb | 3734 | "dot11RSNAStatsTKIPRemoteMICFailures=%u\n" |
6fc6879b JM |
3735 | /* TODO: dot11RSNAStatsCCMPReplays */ |
3736 | /* TODO: dot11RSNAStatsCCMPDecryptErrors */ | |
3737 | /* TODO: dot11RSNAStatsTKIPReplays */, | |
3738 | MAC2STR(sm->addr), | |
3739 | RSN_SUITE_ARG(pairwise), | |
3740 | sm->dot11RSNAStatsTKIPLocalMICFailures, | |
3741 | sm->dot11RSNAStatsTKIPRemoteMICFailures); | |
d85e1fc8 | 3742 | if (os_snprintf_error(buflen - len, ret)) |
6fc6879b JM |
3743 | return len; |
3744 | len += ret; | |
3745 | ||
3746 | /* Private MIB */ | |
3747 | ret = os_snprintf(buf + len, buflen - len, | |
3748 | "hostapdWPAPTKState=%d\n" | |
3749 | "hostapdWPAPTKGroupState=%d\n", | |
3750 | sm->wpa_ptk_state, | |
3751 | sm->wpa_ptk_group_state); | |
d85e1fc8 | 3752 | if (os_snprintf_error(buflen - len, ret)) |
6fc6879b JM |
3753 | return len; |
3754 | len += ret; | |
3755 | ||
3756 | return len; | |
3757 | } | |
3758 | ||
3759 | ||
3760 | void wpa_auth_countermeasures_start(struct wpa_authenticator *wpa_auth) | |
3761 | { | |
3762 | if (wpa_auth) | |
3763 | wpa_auth->dot11RSNATKIPCounterMeasuresInvoked++; | |
3764 | } | |
3765 | ||
3766 | ||
3767 | int wpa_auth_pairwise_set(struct wpa_state_machine *sm) | |
3768 | { | |
3769 | return sm && sm->pairwise_set; | |
3770 | } | |
3771 | ||
3772 | ||
ff36ff00 JM |
3773 | int wpa_auth_get_pairwise(struct wpa_state_machine *sm) |
3774 | { | |
3775 | return sm->pairwise; | |
3776 | } | |
3777 | ||
3778 | ||
6fc6879b JM |
3779 | int wpa_auth_sta_key_mgmt(struct wpa_state_machine *sm) |
3780 | { | |
3781 | if (sm == NULL) | |
3782 | return -1; | |
3783 | return sm->wpa_key_mgmt; | |
3784 | } | |
3785 | ||
3786 | ||
3787 | int wpa_auth_sta_wpa_version(struct wpa_state_machine *sm) | |
3788 | { | |
3789 | if (sm == NULL) | |
3790 | return 0; | |
3791 | return sm->wpa; | |
3792 | } | |
3793 | ||
3794 | ||
3795 | int wpa_auth_sta_clear_pmksa(struct wpa_state_machine *sm, | |
3796 | struct rsn_pmksa_cache_entry *entry) | |
3797 | { | |
3798 | if (sm == NULL || sm->pmksa != entry) | |
3799 | return -1; | |
3800 | sm->pmksa = NULL; | |
3801 | return 0; | |
3802 | } | |
3803 | ||
3804 | ||
3805 | struct rsn_pmksa_cache_entry * | |
3806 | wpa_auth_sta_get_pmksa(struct wpa_state_machine *sm) | |
3807 | { | |
3808 | return sm ? sm->pmksa : NULL; | |
3809 | } | |
3810 | ||
3811 | ||
3812 | void wpa_auth_sta_local_mic_failure_report(struct wpa_state_machine *sm) | |
3813 | { | |
3814 | if (sm) | |
3815 | sm->dot11RSNAStatsTKIPLocalMICFailures++; | |
3816 | } | |
3817 | ||
3818 | ||
3819 | const u8 * wpa_auth_get_wpa_ie(struct wpa_authenticator *wpa_auth, size_t *len) | |
3820 | { | |
3821 | if (wpa_auth == NULL) | |
3822 | return NULL; | |
3823 | *len = wpa_auth->wpa_ie_len; | |
3824 | return wpa_auth->wpa_ie; | |
3825 | } | |
3826 | ||
3827 | ||
3828 | int wpa_auth_pmksa_add(struct wpa_state_machine *sm, const u8 *pmk, | |
207976f0 | 3829 | unsigned int pmk_len, |
6fc6879b JM |
3830 | int session_timeout, struct eapol_state_machine *eapol) |
3831 | { | |
cb465555 JM |
3832 | if (sm == NULL || sm->wpa != WPA_VERSION_WPA2 || |
3833 | sm->wpa_auth->conf.disable_pmksa_caching) | |
6fc6879b JM |
3834 | return -1; |
3835 | ||
834c5d68 | 3836 | if (wpa_key_mgmt_sha384(sm->wpa_key_mgmt)) { |
207976f0 JM |
3837 | if (pmk_len > PMK_LEN_SUITE_B_192) |
3838 | pmk_len = PMK_LEN_SUITE_B_192; | |
3839 | } else if (pmk_len > PMK_LEN) { | |
3840 | pmk_len = PMK_LEN; | |
3841 | } | |
3842 | ||
70c93963 | 3843 | if (pmksa_cache_auth_add(sm->wpa_auth->pmksa, pmk, pmk_len, NULL, |
98cd3d1c | 3844 | sm->PTK.kck, sm->PTK.kck_len, |
4bb081f1 JM |
3845 | sm->wpa_auth->addr, sm->addr, session_timeout, |
3846 | eapol, sm->wpa_key_mgmt)) | |
6fc6879b JM |
3847 | return 0; |
3848 | ||
3849 | return -1; | |
3850 | } | |
3851 | ||
3852 | ||
3853 | int wpa_auth_pmksa_add_preauth(struct wpa_authenticator *wpa_auth, | |
3854 | const u8 *pmk, size_t len, const u8 *sta_addr, | |
3855 | int session_timeout, | |
3856 | struct eapol_state_machine *eapol) | |
3857 | { | |
3858 | if (wpa_auth == NULL) | |
3859 | return -1; | |
3860 | ||
70c93963 | 3861 | if (pmksa_cache_auth_add(wpa_auth->pmksa, pmk, len, NULL, |
087a1f4e JM |
3862 | NULL, 0, |
3863 | wpa_auth->addr, | |
4bb081f1 JM |
3864 | sta_addr, session_timeout, eapol, |
3865 | WPA_KEY_MGMT_IEEE8021X)) | |
6fc6879b JM |
3866 | return 0; |
3867 | ||
3868 | return -1; | |
3869 | } | |
3870 | ||
3871 | ||
f2991170 | 3872 | int wpa_auth_pmksa_add_sae(struct wpa_authenticator *wpa_auth, const u8 *addr, |
70c93963 | 3873 | const u8 *pmk, const u8 *pmkid) |
f2991170 JM |
3874 | { |
3875 | if (wpa_auth->conf.disable_pmksa_caching) | |
3876 | return -1; | |
3877 | ||
70c93963 | 3878 | if (pmksa_cache_auth_add(wpa_auth->pmksa, pmk, PMK_LEN, pmkid, |
087a1f4e | 3879 | NULL, 0, |
f2991170 JM |
3880 | wpa_auth->addr, addr, 0, NULL, |
3881 | WPA_KEY_MGMT_SAE)) | |
3882 | return 0; | |
3883 | ||
3884 | return -1; | |
3885 | } | |
3886 | ||
3887 | ||
901d1fe1 JM |
3888 | void wpa_auth_pmksa_remove(struct wpa_authenticator *wpa_auth, |
3889 | const u8 *sta_addr) | |
3890 | { | |
3891 | struct rsn_pmksa_cache_entry *pmksa; | |
3892 | ||
3893 | if (wpa_auth == NULL || wpa_auth->pmksa == NULL) | |
3894 | return; | |
3895 | pmksa = pmksa_cache_auth_get(wpa_auth->pmksa, sta_addr, NULL); | |
3896 | if (pmksa) { | |
3897 | wpa_printf(MSG_DEBUG, "WPA: Remove PMKSA cache entry for " | |
3898 | MACSTR " based on request", MAC2STR(sta_addr)); | |
3899 | pmksa_cache_free_entry(wpa_auth->pmksa, pmksa); | |
3900 | } | |
3901 | } | |
3902 | ||
3903 | ||
b8daac18 MH |
3904 | int wpa_auth_pmksa_list(struct wpa_authenticator *wpa_auth, char *buf, |
3905 | size_t len) | |
3906 | { | |
3907 | if (!wpa_auth || !wpa_auth->pmksa) | |
3908 | return 0; | |
3909 | return pmksa_cache_auth_list(wpa_auth->pmksa, buf, len); | |
3910 | } | |
3911 | ||
3912 | ||
4c522c77 MH |
3913 | void wpa_auth_pmksa_flush(struct wpa_authenticator *wpa_auth) |
3914 | { | |
3915 | if (wpa_auth && wpa_auth->pmksa) | |
3916 | pmksa_cache_auth_flush(wpa_auth->pmksa); | |
3917 | } | |
3918 | ||
3919 | ||
4d77d80e MH |
3920 | #ifdef CONFIG_PMKSA_CACHE_EXTERNAL |
3921 | #ifdef CONFIG_MESH | |
3922 | ||
3923 | int wpa_auth_pmksa_list_mesh(struct wpa_authenticator *wpa_auth, const u8 *addr, | |
3924 | char *buf, size_t len) | |
3925 | { | |
3926 | if (!wpa_auth || !wpa_auth->pmksa) | |
3927 | return 0; | |
3928 | ||
3929 | return pmksa_cache_auth_list_mesh(wpa_auth->pmksa, addr, buf, len); | |
3930 | } | |
3931 | ||
3932 | ||
3933 | struct rsn_pmksa_cache_entry * | |
3934 | wpa_auth_pmksa_create_entry(const u8 *aa, const u8 *spa, const u8 *pmk, | |
3935 | const u8 *pmkid, int expiration) | |
3936 | { | |
3937 | struct rsn_pmksa_cache_entry *entry; | |
3938 | struct os_reltime now; | |
3939 | ||
3940 | entry = pmksa_cache_auth_create_entry(pmk, PMK_LEN, pmkid, NULL, 0, aa, | |
3941 | spa, 0, NULL, WPA_KEY_MGMT_SAE); | |
3942 | if (!entry) | |
3943 | return NULL; | |
3944 | ||
3945 | os_get_reltime(&now); | |
3946 | entry->expiration = now.sec + expiration; | |
3947 | return entry; | |
3948 | } | |
3949 | ||
3950 | ||
3951 | int wpa_auth_pmksa_add_entry(struct wpa_authenticator *wpa_auth, | |
3952 | struct rsn_pmksa_cache_entry *entry) | |
3953 | { | |
3954 | int ret; | |
3955 | ||
3956 | if (!wpa_auth || !wpa_auth->pmksa) | |
3957 | return -1; | |
3958 | ||
3959 | ret = pmksa_cache_auth_add_entry(wpa_auth->pmksa, entry); | |
3960 | if (ret < 0) | |
3961 | wpa_printf(MSG_DEBUG, | |
3962 | "RSN: Failed to store external PMKSA cache for " | |
3963 | MACSTR, MAC2STR(entry->spa)); | |
3964 | ||
3965 | return ret; | |
3966 | } | |
3967 | ||
3968 | #endif /* CONFIG_MESH */ | |
3969 | #endif /* CONFIG_PMKSA_CACHE_EXTERNAL */ | |
3970 | ||
3971 | ||
9f2cf23e | 3972 | struct rsn_pmksa_cache_entry * |
c1bd4bac JM |
3973 | wpa_auth_pmksa_get(struct wpa_authenticator *wpa_auth, const u8 *sta_addr, |
3974 | const u8 *pmkid) | |
9f2cf23e MH |
3975 | { |
3976 | if (!wpa_auth || !wpa_auth->pmksa) | |
3977 | return NULL; | |
c1bd4bac | 3978 | return pmksa_cache_auth_get(wpa_auth->pmksa, sta_addr, pmkid); |
9f2cf23e MH |
3979 | } |
3980 | ||
3981 | ||
3982 | void wpa_auth_pmksa_set_to_sm(struct rsn_pmksa_cache_entry *pmksa, | |
3983 | struct wpa_state_machine *sm, | |
3984 | struct wpa_authenticator *wpa_auth, | |
3985 | u8 *pmkid, u8 *pmk) | |
3986 | { | |
3987 | if (!sm) | |
3988 | return; | |
3989 | ||
3990 | sm->pmksa = pmksa; | |
8854f90b JM |
3991 | os_memcpy(pmk, pmksa->pmk, PMK_LEN); |
3992 | os_memcpy(pmkid, pmksa->pmkid, PMKID_LEN); | |
3993 | os_memcpy(wpa_auth->dot11RSNAPMKIDUsed, pmksa->pmkid, PMKID_LEN); | |
9f2cf23e MH |
3994 | } |
3995 | ||
3996 | ||
a0ad9e8c MB |
3997 | /* |
3998 | * Remove and free the group from wpa_authenticator. This is triggered by a | |
3999 | * callback to make sure nobody is currently iterating the group list while it | |
4000 | * gets modified. | |
4001 | */ | |
4002 | static void wpa_group_free(struct wpa_authenticator *wpa_auth, | |
4003 | struct wpa_group *group) | |
4004 | { | |
4005 | struct wpa_group *prev = wpa_auth->group; | |
4006 | ||
4007 | wpa_printf(MSG_DEBUG, "WPA: Remove group state machine for VLAN-ID %d", | |
4008 | group->vlan_id); | |
4009 | ||
4010 | while (prev) { | |
4011 | if (prev->next == group) { | |
4012 | /* This never frees the special first group as needed */ | |
4013 | prev->next = group->next; | |
4014 | os_free(group); | |
4015 | break; | |
4016 | } | |
4017 | prev = prev->next; | |
4018 | } | |
4019 | ||
4020 | } | |
4021 | ||
4022 | ||
4023 | /* Increase the reference counter for group */ | |
4024 | static void wpa_group_get(struct wpa_authenticator *wpa_auth, | |
4025 | struct wpa_group *group) | |
4026 | { | |
4027 | /* Skip the special first group */ | |
4028 | if (wpa_auth->group == group) | |
4029 | return; | |
4030 | ||
4031 | group->references++; | |
4032 | } | |
4033 | ||
4034 | ||
4035 | /* Decrease the reference counter and maybe free the group */ | |
4036 | static void wpa_group_put(struct wpa_authenticator *wpa_auth, | |
4037 | struct wpa_group *group) | |
4038 | { | |
4039 | /* Skip the special first group */ | |
4040 | if (wpa_auth->group == group) | |
4041 | return; | |
4042 | ||
4043 | group->references--; | |
4044 | if (group->references) | |
4045 | return; | |
4046 | wpa_group_free(wpa_auth, group); | |
4047 | } | |
4048 | ||
4049 | ||
4050 | /* | |
4051 | * Add a group that has its references counter set to zero. Caller needs to | |
4052 | * call wpa_group_get() on the return value to mark the entry in use. | |
4053 | */ | |
6fc6879b JM |
4054 | static struct wpa_group * |
4055 | wpa_auth_add_group(struct wpa_authenticator *wpa_auth, int vlan_id) | |
4056 | { | |
4057 | struct wpa_group *group; | |
4058 | ||
4059 | if (wpa_auth == NULL || wpa_auth->group == NULL) | |
4060 | return NULL; | |
4061 | ||
4062 | wpa_printf(MSG_DEBUG, "WPA: Add group state machine for VLAN-ID %d", | |
4063 | vlan_id); | |
bdffdc5d | 4064 | group = wpa_group_init(wpa_auth, vlan_id, 0); |
6fc6879b JM |
4065 | if (group == NULL) |
4066 | return NULL; | |
4067 | ||
4068 | group->next = wpa_auth->group->next; | |
4069 | wpa_auth->group->next = group; | |
4070 | ||
4071 | return group; | |
4072 | } | |
4073 | ||
4074 | ||
7cebc8e2 MB |
4075 | /* |
4076 | * Enforce that the group state machine for the VLAN is running, increase | |
4077 | * reference counter as interface is up. References might have been increased | |
4078 | * even if a negative value is returned. | |
4079 | * Returns: -1 on error (group missing, group already failed); otherwise, 0 | |
4080 | */ | |
4081 | int wpa_auth_ensure_group(struct wpa_authenticator *wpa_auth, int vlan_id) | |
4082 | { | |
4083 | struct wpa_group *group; | |
4084 | ||
4085 | if (wpa_auth == NULL) | |
4086 | return 0; | |
4087 | ||
4088 | group = wpa_auth->group; | |
4089 | while (group) { | |
4090 | if (group->vlan_id == vlan_id) | |
4091 | break; | |
4092 | group = group->next; | |
4093 | } | |
4094 | ||
4095 | if (group == NULL) { | |
4096 | group = wpa_auth_add_group(wpa_auth, vlan_id); | |
4097 | if (group == NULL) | |
4098 | return -1; | |
4099 | } | |
4100 | ||
4101 | wpa_printf(MSG_DEBUG, | |
4102 | "WPA: Ensure group state machine running for VLAN ID %d", | |
4103 | vlan_id); | |
4104 | ||
4105 | wpa_group_get(wpa_auth, group); | |
4106 | group->num_setup_iface++; | |
4107 | ||
4108 | if (group->wpa_group_state == WPA_GROUP_FATAL_FAILURE) | |
4109 | return -1; | |
4110 | ||
4111 | return 0; | |
4112 | } | |
4113 | ||
4114 | ||
4115 | /* | |
4116 | * Decrease reference counter, expected to be zero afterwards. | |
4117 | * returns: -1 on error (group not found, group in fail state) | |
4118 | * -2 if wpa_group is still referenced | |
4119 | * 0 else | |
4120 | */ | |
4121 | int wpa_auth_release_group(struct wpa_authenticator *wpa_auth, int vlan_id) | |
4122 | { | |
4123 | struct wpa_group *group; | |
4124 | int ret = 0; | |
4125 | ||
4126 | if (wpa_auth == NULL) | |
4127 | return 0; | |
4128 | ||
4129 | group = wpa_auth->group; | |
4130 | while (group) { | |
4131 | if (group->vlan_id == vlan_id) | |
4132 | break; | |
4133 | group = group->next; | |
4134 | } | |
4135 | ||
4136 | if (group == NULL) | |
4137 | return -1; | |
4138 | ||
4139 | wpa_printf(MSG_DEBUG, | |
4140 | "WPA: Try stopping group state machine for VLAN ID %d", | |
4141 | vlan_id); | |
4142 | ||
4143 | if (group->num_setup_iface <= 0) { | |
4144 | wpa_printf(MSG_ERROR, | |
4145 | "WPA: wpa_auth_release_group called more often than wpa_auth_ensure_group for VLAN ID %d, skipping.", | |
4146 | vlan_id); | |
4147 | return -1; | |
4148 | } | |
4149 | group->num_setup_iface--; | |
4150 | ||
4151 | if (group->wpa_group_state == WPA_GROUP_FATAL_FAILURE) | |
4152 | ret = -1; | |
4153 | ||
4154 | if (group->references > 1) { | |
4155 | wpa_printf(MSG_DEBUG, | |
4156 | "WPA: Cannot stop group state machine for VLAN ID %d as references are still hold", | |
4157 | vlan_id); | |
4158 | ret = -2; | |
4159 | } | |
4160 | ||
4161 | wpa_group_put(wpa_auth, group); | |
4162 | ||
4163 | return ret; | |
4164 | } | |
4165 | ||
4166 | ||
6fc6879b JM |
4167 | int wpa_auth_sta_set_vlan(struct wpa_state_machine *sm, int vlan_id) |
4168 | { | |
4169 | struct wpa_group *group; | |
4170 | ||
4171 | if (sm == NULL || sm->wpa_auth == NULL) | |
4172 | return 0; | |
4173 | ||
4174 | group = sm->wpa_auth->group; | |
4175 | while (group) { | |
4176 | if (group->vlan_id == vlan_id) | |
4177 | break; | |
4178 | group = group->next; | |
4179 | } | |
4180 | ||
4181 | if (group == NULL) { | |
4182 | group = wpa_auth_add_group(sm->wpa_auth, vlan_id); | |
4183 | if (group == NULL) | |
4184 | return -1; | |
4185 | } | |
4186 | ||
4187 | if (sm->group == group) | |
4188 | return 0; | |
4189 | ||
7d7f7be2 JM |
4190 | if (group->wpa_group_state == WPA_GROUP_FATAL_FAILURE) |
4191 | return -1; | |
4192 | ||
6fc6879b JM |
4193 | wpa_printf(MSG_DEBUG, "WPA: Moving STA " MACSTR " to use group state " |
4194 | "machine for VLAN ID %d", MAC2STR(sm->addr), vlan_id); | |
4195 | ||
a0ad9e8c MB |
4196 | wpa_group_get(sm->wpa_auth, group); |
4197 | wpa_group_put(sm->wpa_auth, sm->group); | |
6fc6879b | 4198 | sm->group = group; |
a0ad9e8c | 4199 | |
6fc6879b JM |
4200 | return 0; |
4201 | } | |
e4bf4db9 JM |
4202 | |
4203 | ||
4204 | void wpa_auth_eapol_key_tx_status(struct wpa_authenticator *wpa_auth, | |
4205 | struct wpa_state_machine *sm, int ack) | |
4206 | { | |
4207 | if (wpa_auth == NULL || sm == NULL) | |
4208 | return; | |
4209 | wpa_printf(MSG_DEBUG, "WPA: EAPOL-Key TX status for STA " MACSTR | |
4210 | " ack=%d", MAC2STR(sm->addr), ack); | |
4211 | if (sm->pending_1_of_4_timeout && ack) { | |
4212 | /* | |
4213 | * Some deployed supplicant implementations update their SNonce | |
4214 | * for each EAPOL-Key 2/4 message even within the same 4-way | |
4215 | * handshake and then fail to use the first SNonce when | |
4216 | * deriving the PTK. This results in unsuccessful 4-way | |
4217 | * handshake whenever the relatively short initial timeout is | |
4218 | * reached and EAPOL-Key 1/4 is retransmitted. Try to work | |
4219 | * around this by increasing the timeout now that we know that | |
4220 | * the station has received the frame. | |
4221 | */ | |
4222 | int timeout_ms = eapol_key_timeout_subseq; | |
4223 | wpa_printf(MSG_DEBUG, "WPA: Increase initial EAPOL-Key 1/4 " | |
4224 | "timeout by %u ms because of acknowledged frame", | |
4225 | timeout_ms); | |
4226 | eloop_cancel_timeout(wpa_send_eapol_timeout, wpa_auth, sm); | |
4227 | eloop_register_timeout(timeout_ms / 1000, | |
4228 | (timeout_ms % 1000) * 1000, | |
4229 | wpa_send_eapol_timeout, wpa_auth, sm); | |
4230 | } | |
4231 | } | |
c10347f2 JM |
4232 | |
4233 | ||
4234 | int wpa_auth_uses_sae(struct wpa_state_machine *sm) | |
4235 | { | |
4236 | if (sm == NULL) | |
4237 | return 0; | |
4238 | return wpa_key_mgmt_sae(sm->wpa_key_mgmt); | |
4239 | } | |
aa189ac9 JM |
4240 | |
4241 | ||
4242 | int wpa_auth_uses_ft_sae(struct wpa_state_machine *sm) | |
4243 | { | |
4244 | if (sm == NULL) | |
4245 | return 0; | |
4246 | return sm->wpa_key_mgmt == WPA_KEY_MGMT_FT_SAE; | |
4247 | } | |
25ef8529 JM |
4248 | |
4249 | ||
4250 | #ifdef CONFIG_P2P | |
4251 | int wpa_auth_get_ip_addr(struct wpa_state_machine *sm, u8 *addr) | |
4252 | { | |
4253 | if (sm == NULL || WPA_GET_BE32(sm->ip_addr) == 0) | |
4254 | return -1; | |
4255 | os_memcpy(addr, sm->ip_addr, 4); | |
4256 | return 0; | |
4257 | } | |
4258 | #endif /* CONFIG_P2P */ | |
cbc210de JM |
4259 | |
4260 | ||
4261 | int wpa_auth_radius_das_disconnect_pmksa(struct wpa_authenticator *wpa_auth, | |
4262 | struct radius_das_attrs *attr) | |
4263 | { | |
4264 | return pmksa_cache_auth_radius_das_disconnect(wpa_auth->pmksa, attr); | |
4265 | } | |
34782730 JM |
4266 | |
4267 | ||
4268 | void wpa_auth_reconfig_group_keys(struct wpa_authenticator *wpa_auth) | |
4269 | { | |
4270 | struct wpa_group *group; | |
4271 | ||
4272 | if (!wpa_auth) | |
4273 | return; | |
4274 | for (group = wpa_auth->group; group; group = group->next) | |
4275 | wpa_group_config_group_keys(wpa_auth, group); | |
4276 | } | |
7eace378 JM |
4277 | |
4278 | ||
4279 | #ifdef CONFIG_FILS | |
4280 | ||
4281 | struct wpa_auth_fils_iter_data { | |
4282 | struct wpa_authenticator *auth; | |
4283 | const u8 *cache_id; | |
4284 | struct rsn_pmksa_cache_entry *pmksa; | |
4285 | const u8 *spa; | |
4286 | const u8 *pmkid; | |
4287 | }; | |
4288 | ||
4289 | ||
4290 | static int wpa_auth_fils_iter(struct wpa_authenticator *a, void *ctx) | |
4291 | { | |
4292 | struct wpa_auth_fils_iter_data *data = ctx; | |
4293 | ||
4cc6574d | 4294 | if (a == data->auth || !a->conf.fils_cache_id_set || |
7eace378 JM |
4295 | os_memcmp(a->conf.fils_cache_id, data->cache_id, |
4296 | FILS_CACHE_ID_LEN) != 0) | |
4297 | return 0; | |
4298 | data->pmksa = pmksa_cache_auth_get(a->pmksa, data->spa, data->pmkid); | |
4299 | return data->pmksa != NULL; | |
4300 | } | |
4301 | ||
4302 | ||
4303 | struct rsn_pmksa_cache_entry * | |
4304 | wpa_auth_pmksa_get_fils_cache_id(struct wpa_authenticator *wpa_auth, | |
4305 | const u8 *sta_addr, const u8 *pmkid) | |
4306 | { | |
4307 | struct wpa_auth_fils_iter_data idata; | |
4308 | ||
4309 | if (!wpa_auth->conf.fils_cache_id_set) | |
4310 | return NULL; | |
4311 | idata.auth = wpa_auth; | |
4312 | idata.cache_id = wpa_auth->conf.fils_cache_id; | |
4313 | idata.pmksa = NULL; | |
4314 | idata.spa = sta_addr; | |
4315 | idata.pmkid = pmkid; | |
4316 | wpa_auth_for_each_auth(wpa_auth, wpa_auth_fils_iter, &idata); | |
4317 | return idata.pmksa; | |
4318 | } | |
4319 | ||
5db997e3 JM |
4320 | |
4321 | #ifdef CONFIG_IEEE80211R_AP | |
4322 | int wpa_auth_write_fte(struct wpa_authenticator *wpa_auth, u8 *buf, size_t len) | |
4323 | { | |
4324 | struct wpa_auth_config *conf = &wpa_auth->conf; | |
4325 | ||
4326 | return wpa_write_ftie(conf, conf->r0_key_holder, | |
4327 | conf->r0_key_holder_len, | |
4328 | NULL, NULL, buf, len, NULL, 0); | |
4329 | } | |
4330 | #endif /* CONFIG_IEEE80211R_AP */ | |
4331 | ||
8acbf85f JM |
4332 | |
4333 | void wpa_auth_get_fils_aead_params(struct wpa_state_machine *sm, | |
4334 | u8 *fils_anonce, u8 *fils_snonce, | |
4335 | u8 *fils_kek, size_t *fils_kek_len) | |
4336 | { | |
4337 | os_memcpy(fils_anonce, sm->ANonce, WPA_NONCE_LEN); | |
4338 | os_memcpy(fils_snonce, sm->SNonce, WPA_NONCE_LEN); | |
4339 | os_memcpy(fils_kek, sm->PTK.kek, WPA_KEK_MAX_LEN); | |
4340 | *fils_kek_len = sm->PTK.kek_len; | |
4341 | } | |
4342 | ||
7eace378 | 4343 | #endif /* CONFIG_FILS */ |