]>
Commit | Line | Data |
---|---|---|
6fc6879b | 1 | /* |
00338a4f | 2 | * IEEE 802.11 RSN / WPA Authenticator |
bb05d036 | 3 | * Copyright (c) 2004-2019, 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" |
17c2559c | 16 | #include "common/ocv.h" |
3b5b7aa8 | 17 | #include "crypto/aes.h" |
03da66bd | 18 | #include "crypto/aes_wrap.h" |
3b5b7aa8 | 19 | #include "crypto/aes_siv.h" |
03da66bd JM |
20 | #include "crypto/crypto.h" |
21 | #include "crypto/sha1.h" | |
22 | #include "crypto/sha256.h" | |
c6eb3e34 | 23 | #include "crypto/sha384.h" |
3642c431 | 24 | #include "crypto/random.h" |
281c950b | 25 | #include "eapol_auth/eapol_auth_sm.h" |
17c2559c | 26 | #include "drivers/driver.h" |
6226e38d | 27 | #include "ap_config.h" |
6fc6879b | 28 | #include "ieee802_11.h" |
6226e38d JM |
29 | #include "wpa_auth.h" |
30 | #include "pmksa_cache_auth.h" | |
6fc6879b JM |
31 | #include "wpa_auth_i.h" |
32 | #include "wpa_auth_ie.h" | |
33 | ||
34 | #define STATE_MACHINE_DATA struct wpa_state_machine | |
35 | #define STATE_MACHINE_DEBUG_PREFIX "WPA" | |
36 | #define STATE_MACHINE_ADDR sm->addr | |
37 | ||
38 | ||
39 | static void wpa_send_eapol_timeout(void *eloop_ctx, void *timeout_ctx); | |
e4a6ea1d | 40 | static int wpa_sm_step(struct wpa_state_machine *sm); |
567da5bb JM |
41 | static int wpa_verify_key_mic(int akmp, size_t pmk_len, struct wpa_ptk *PTK, |
42 | u8 *data, size_t data_len); | |
75c8563e JM |
43 | #ifdef CONFIG_FILS |
44 | static int wpa_aead_decrypt(struct wpa_state_machine *sm, struct wpa_ptk *ptk, | |
45 | u8 *buf, size_t buf_len, u16 *_key_data_len); | |
9392859d JM |
46 | static struct wpabuf * fils_prepare_plainbuf(struct wpa_state_machine *sm, |
47 | const struct wpabuf *hlp); | |
75c8563e | 48 | #endif /* CONFIG_FILS */ |
6fc6879b JM |
49 | static void wpa_sm_call_step(void *eloop_ctx, void *timeout_ctx); |
50 | static void wpa_group_sm_step(struct wpa_authenticator *wpa_auth, | |
51 | struct wpa_group *group); | |
581a8cde | 52 | static void wpa_request_new_ptk(struct wpa_state_machine *sm); |
e6965d4e JM |
53 | static int wpa_gtk_update(struct wpa_authenticator *wpa_auth, |
54 | struct wpa_group *group); | |
1bdb7ab3 JM |
55 | static int wpa_group_config_group_keys(struct wpa_authenticator *wpa_auth, |
56 | struct wpa_group *group); | |
f23c5b17 | 57 | static int wpa_derive_ptk(struct wpa_state_machine *sm, const u8 *snonce, |
207976f0 | 58 | const u8 *pmk, unsigned int pmk_len, |
65a44e84 | 59 | struct wpa_ptk *ptk, int force_sha256); |
a0ad9e8c MB |
60 | static void wpa_group_free(struct wpa_authenticator *wpa_auth, |
61 | struct wpa_group *group); | |
62 | static void wpa_group_get(struct wpa_authenticator *wpa_auth, | |
63 | struct wpa_group *group); | |
64 | static void wpa_group_put(struct wpa_authenticator *wpa_auth, | |
65 | struct wpa_group *group); | |
16889aff | 66 | static int ieee80211w_kde_len(struct wpa_state_machine *sm); |
e73ffa09 | 67 | static u8 * ieee80211w_kde_add(struct wpa_state_machine *sm, u8 *pos); |
6fc6879b | 68 | |
bae61562 JM |
69 | static const u32 eapol_key_timeout_first = 100; /* ms */ |
70 | static const u32 eapol_key_timeout_subseq = 1000; /* ms */ | |
00338a4f | 71 | static const u32 eapol_key_timeout_first_group = 500; /* ms */ |
6f234c1e | 72 | static const u32 eapol_key_timeout_no_retrans = 4000; /* ms */ |
6fc6879b JM |
73 | |
74 | /* TODO: make these configurable */ | |
75 | static const int dot11RSNAConfigPMKLifetime = 43200; | |
76 | static const int dot11RSNAConfigPMKReauthThreshold = 70; | |
77 | static const int dot11RSNAConfigSATimeout = 60; | |
78 | ||
79 | ||
c772d054 | 80 | static inline int wpa_auth_mic_failure_report( |
6fc6879b JM |
81 | struct wpa_authenticator *wpa_auth, const u8 *addr) |
82 | { | |
cef8fac0 JB |
83 | if (wpa_auth->cb->mic_failure_report) |
84 | return wpa_auth->cb->mic_failure_report(wpa_auth->cb_ctx, addr); | |
c772d054 | 85 | return 0; |
6fc6879b JM |
86 | } |
87 | ||
88 | ||
2c502460 JM |
89 | static inline void wpa_auth_psk_failure_report( |
90 | struct wpa_authenticator *wpa_auth, const u8 *addr) | |
91 | { | |
cef8fac0 JB |
92 | if (wpa_auth->cb->psk_failure_report) |
93 | wpa_auth->cb->psk_failure_report(wpa_auth->cb_ctx, addr); | |
2c502460 JM |
94 | } |
95 | ||
96 | ||
6fc6879b JM |
97 | static inline void wpa_auth_set_eapol(struct wpa_authenticator *wpa_auth, |
98 | const u8 *addr, wpa_eapol_variable var, | |
99 | int value) | |
100 | { | |
cef8fac0 JB |
101 | if (wpa_auth->cb->set_eapol) |
102 | wpa_auth->cb->set_eapol(wpa_auth->cb_ctx, addr, var, value); | |
6fc6879b JM |
103 | } |
104 | ||
105 | ||
106 | static inline int wpa_auth_get_eapol(struct wpa_authenticator *wpa_auth, | |
107 | const u8 *addr, wpa_eapol_variable var) | |
108 | { | |
cef8fac0 | 109 | if (wpa_auth->cb->get_eapol == NULL) |
6fc6879b | 110 | return -1; |
cef8fac0 | 111 | return wpa_auth->cb->get_eapol(wpa_auth->cb_ctx, addr, var); |
6fc6879b JM |
112 | } |
113 | ||
114 | ||
115 | static inline const u8 * wpa_auth_get_psk(struct wpa_authenticator *wpa_auth, | |
759fd76b JM |
116 | const u8 *addr, |
117 | const u8 *p2p_dev_addr, | |
dbfa691d JM |
118 | const u8 *prev_psk, size_t *psk_len, |
119 | int *vlan_id) | |
6fc6879b | 120 | { |
cef8fac0 | 121 | if (wpa_auth->cb->get_psk == NULL) |
6fc6879b | 122 | return NULL; |
cef8fac0 | 123 | return wpa_auth->cb->get_psk(wpa_auth->cb_ctx, addr, p2p_dev_addr, |
dbfa691d | 124 | prev_psk, psk_len, vlan_id); |
6fc6879b JM |
125 | } |
126 | ||
127 | ||
128 | static inline int wpa_auth_get_msk(struct wpa_authenticator *wpa_auth, | |
129 | const u8 *addr, u8 *msk, size_t *len) | |
130 | { | |
cef8fac0 | 131 | if (wpa_auth->cb->get_msk == NULL) |
6fc6879b | 132 | return -1; |
cef8fac0 | 133 | return wpa_auth->cb->get_msk(wpa_auth->cb_ctx, addr, msk, len); |
6fc6879b JM |
134 | } |
135 | ||
136 | ||
137 | static inline int wpa_auth_set_key(struct wpa_authenticator *wpa_auth, | |
138 | int vlan_id, | |
71934751 | 139 | enum wpa_alg alg, const u8 *addr, int idx, |
a919a260 AW |
140 | u8 *key, size_t key_len, |
141 | enum key_flag key_flag) | |
6fc6879b | 142 | { |
cef8fac0 | 143 | if (wpa_auth->cb->set_key == NULL) |
6fc6879b | 144 | return -1; |
cef8fac0 | 145 | return wpa_auth->cb->set_key(wpa_auth->cb_ctx, vlan_id, alg, addr, idx, |
a919a260 | 146 | key, key_len, key_flag); |
6fc6879b JM |
147 | } |
148 | ||
149 | ||
150 | static inline int wpa_auth_get_seqnum(struct wpa_authenticator *wpa_auth, | |
151 | const u8 *addr, int idx, u8 *seq) | |
152 | { | |
8d84c75f JM |
153 | int res; |
154 | ||
cef8fac0 | 155 | if (wpa_auth->cb->get_seqnum == NULL) |
6fc6879b | 156 | return -1; |
8d84c75f JM |
157 | res = wpa_auth->cb->get_seqnum(wpa_auth->cb_ctx, addr, idx, seq); |
158 | #ifdef CONFIG_TESTING_OPTIONS | |
159 | if (!addr && idx < 4 && wpa_auth->conf.gtk_rsc_override_set) { | |
160 | wpa_printf(MSG_DEBUG, | |
161 | "TESTING: Override GTK RSC %016llx --> %016llx", | |
162 | (long long unsigned) WPA_GET_LE64(seq), | |
163 | (long long unsigned) | |
164 | WPA_GET_LE64(wpa_auth->conf.gtk_rsc_override)); | |
165 | os_memcpy(seq, wpa_auth->conf.gtk_rsc_override, | |
166 | WPA_KEY_RSC_LEN); | |
167 | } | |
168 | if (!addr && idx >= 4 && idx <= 5 && | |
169 | wpa_auth->conf.igtk_rsc_override_set) { | |
170 | wpa_printf(MSG_DEBUG, | |
171 | "TESTING: Override IGTK RSC %016llx --> %016llx", | |
172 | (long long unsigned) WPA_GET_LE64(seq), | |
173 | (long long unsigned) | |
174 | WPA_GET_LE64(wpa_auth->conf.igtk_rsc_override)); | |
175 | os_memcpy(seq, wpa_auth->conf.igtk_rsc_override, | |
176 | WPA_KEY_RSC_LEN); | |
177 | } | |
178 | #endif /* CONFIG_TESTING_OPTIONS */ | |
179 | return res; | |
6fc6879b JM |
180 | } |
181 | ||
182 | ||
6fc6879b JM |
183 | static inline int |
184 | wpa_auth_send_eapol(struct wpa_authenticator *wpa_auth, const u8 *addr, | |
185 | const u8 *data, size_t data_len, int encrypt) | |
186 | { | |
cef8fac0 | 187 | if (wpa_auth->cb->send_eapol == NULL) |
6fc6879b | 188 | return -1; |
cef8fac0 JB |
189 | return wpa_auth->cb->send_eapol(wpa_auth->cb_ctx, addr, data, data_len, |
190 | encrypt); | |
6fc6879b JM |
191 | } |
192 | ||
193 | ||
c50d94f1 BC |
194 | #ifdef CONFIG_MESH |
195 | static inline int wpa_auth_start_ampe(struct wpa_authenticator *wpa_auth, | |
196 | const u8 *addr) | |
197 | { | |
cef8fac0 | 198 | if (wpa_auth->cb->start_ampe == NULL) |
c50d94f1 | 199 | return -1; |
cef8fac0 | 200 | return wpa_auth->cb->start_ampe(wpa_auth->cb_ctx, addr); |
c50d94f1 BC |
201 | } |
202 | #endif /* CONFIG_MESH */ | |
203 | ||
204 | ||
6fc6879b JM |
205 | int wpa_auth_for_each_sta(struct wpa_authenticator *wpa_auth, |
206 | int (*cb)(struct wpa_state_machine *sm, void *ctx), | |
207 | void *cb_ctx) | |
208 | { | |
cef8fac0 | 209 | if (wpa_auth->cb->for_each_sta == NULL) |
6fc6879b | 210 | return 0; |
cef8fac0 | 211 | return wpa_auth->cb->for_each_sta(wpa_auth->cb_ctx, cb, cb_ctx); |
6fc6879b JM |
212 | } |
213 | ||
214 | ||
bf98f7f3 JM |
215 | int wpa_auth_for_each_auth(struct wpa_authenticator *wpa_auth, |
216 | int (*cb)(struct wpa_authenticator *a, void *ctx), | |
217 | void *cb_ctx) | |
218 | { | |
cef8fac0 | 219 | if (wpa_auth->cb->for_each_auth == NULL) |
bf98f7f3 | 220 | return 0; |
cef8fac0 | 221 | return wpa_auth->cb->for_each_auth(wpa_auth->cb_ctx, cb, cb_ctx); |
bf98f7f3 JM |
222 | } |
223 | ||
224 | ||
6fc6879b JM |
225 | void wpa_auth_logger(struct wpa_authenticator *wpa_auth, const u8 *addr, |
226 | logger_level level, const char *txt) | |
227 | { | |
cef8fac0 | 228 | if (wpa_auth->cb->logger == NULL) |
6fc6879b | 229 | return; |
cef8fac0 | 230 | wpa_auth->cb->logger(wpa_auth->cb_ctx, addr, level, txt); |
6fc6879b JM |
231 | } |
232 | ||
233 | ||
234 | void wpa_auth_vlogger(struct wpa_authenticator *wpa_auth, const u8 *addr, | |
235 | logger_level level, const char *fmt, ...) | |
236 | { | |
237 | char *format; | |
238 | int maxlen; | |
239 | va_list ap; | |
240 | ||
cef8fac0 | 241 | if (wpa_auth->cb->logger == NULL) |
6fc6879b JM |
242 | return; |
243 | ||
244 | maxlen = os_strlen(fmt) + 100; | |
245 | format = os_malloc(maxlen); | |
246 | if (!format) | |
247 | return; | |
248 | ||
249 | va_start(ap, fmt); | |
250 | vsnprintf(format, maxlen, fmt, ap); | |
251 | va_end(ap); | |
252 | ||
253 | wpa_auth_logger(wpa_auth, addr, level, format); | |
254 | ||
255 | os_free(format); | |
256 | } | |
257 | ||
258 | ||
259 | static void wpa_sta_disconnect(struct wpa_authenticator *wpa_auth, | |
567da5bb | 260 | const u8 *addr, u16 reason) |
6fc6879b | 261 | { |
cef8fac0 | 262 | if (wpa_auth->cb->disconnect == NULL) |
6fc6879b | 263 | return; |
567da5bb JM |
264 | wpa_printf(MSG_DEBUG, "wpa_sta_disconnect STA " MACSTR " (reason %u)", |
265 | MAC2STR(addr), reason); | |
266 | wpa_auth->cb->disconnect(wpa_auth->cb_ctx, addr, reason); | |
6fc6879b JM |
267 | } |
268 | ||
269 | ||
17c2559c MV |
270 | #ifdef CONFIG_OCV |
271 | static int wpa_channel_info(struct wpa_authenticator *wpa_auth, | |
272 | struct wpa_channel_info *ci) | |
273 | { | |
274 | if (!wpa_auth->cb->channel_info) | |
275 | return -1; | |
276 | return wpa_auth->cb->channel_info(wpa_auth->cb_ctx, ci); | |
277 | } | |
278 | #endif /* CONFIG_OCV */ | |
279 | ||
280 | ||
dbfa691d JM |
281 | static int wpa_auth_update_vlan(struct wpa_authenticator *wpa_auth, |
282 | const u8 *addr, int vlan_id) | |
283 | { | |
284 | if (!wpa_auth->cb->update_vlan) | |
285 | return -1; | |
286 | return wpa_auth->cb->update_vlan(wpa_auth->cb_ctx, addr, vlan_id); | |
287 | } | |
288 | ||
289 | ||
6fc6879b JM |
290 | static void wpa_rekey_gmk(void *eloop_ctx, void *timeout_ctx) |
291 | { | |
292 | struct wpa_authenticator *wpa_auth = eloop_ctx; | |
293 | ||
3642c431 | 294 | if (random_get_bytes(wpa_auth->group->GMK, WPA_GMK_LEN)) { |
6fc6879b JM |
295 | wpa_printf(MSG_ERROR, "Failed to get random data for WPA " |
296 | "initialization."); | |
297 | } else { | |
298 | wpa_auth_logger(wpa_auth, NULL, LOGGER_DEBUG, "GMK rekeyd"); | |
5cb9d5c3 JM |
299 | wpa_hexdump_key(MSG_DEBUG, "GMK", |
300 | wpa_auth->group->GMK, WPA_GMK_LEN); | |
6fc6879b JM |
301 | } |
302 | ||
303 | if (wpa_auth->conf.wpa_gmk_rekey) { | |
304 | eloop_register_timeout(wpa_auth->conf.wpa_gmk_rekey, 0, | |
305 | wpa_rekey_gmk, wpa_auth, NULL); | |
306 | } | |
307 | } | |
308 | ||
309 | ||
310 | static void wpa_rekey_gtk(void *eloop_ctx, void *timeout_ctx) | |
311 | { | |
312 | struct wpa_authenticator *wpa_auth = eloop_ctx; | |
a0ad9e8c | 313 | struct wpa_group *group, *next; |
6fc6879b JM |
314 | |
315 | wpa_auth_logger(wpa_auth, NULL, LOGGER_DEBUG, "rekeying GTK"); | |
a0ad9e8c MB |
316 | group = wpa_auth->group; |
317 | while (group) { | |
318 | wpa_group_get(wpa_auth, group); | |
319 | ||
6fc6879b JM |
320 | group->GTKReKey = TRUE; |
321 | do { | |
322 | group->changed = FALSE; | |
323 | wpa_group_sm_step(wpa_auth, group); | |
324 | } while (group->changed); | |
a0ad9e8c MB |
325 | |
326 | next = group->next; | |
327 | wpa_group_put(wpa_auth, group); | |
328 | group = next; | |
6fc6879b JM |
329 | } |
330 | ||
331 | if (wpa_auth->conf.wpa_group_rekey) { | |
332 | eloop_register_timeout(wpa_auth->conf.wpa_group_rekey, | |
333 | 0, wpa_rekey_gtk, wpa_auth, NULL); | |
334 | } | |
335 | } | |
336 | ||
337 | ||
581a8cde JM |
338 | static void wpa_rekey_ptk(void *eloop_ctx, void *timeout_ctx) |
339 | { | |
340 | struct wpa_authenticator *wpa_auth = eloop_ctx; | |
341 | struct wpa_state_machine *sm = timeout_ctx; | |
342 | ||
343 | wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG, "rekeying PTK"); | |
344 | wpa_request_new_ptk(sm); | |
345 | wpa_sm_step(sm); | |
346 | } | |
347 | ||
348 | ||
a40bd06e JM |
349 | void wpa_auth_set_ptk_rekey_timer(struct wpa_state_machine *sm) |
350 | { | |
351 | if (sm && sm->wpa_auth->conf.wpa_ptk_rekey) { | |
352 | wpa_printf(MSG_DEBUG, "WPA: Start PTK rekeying timer for " | |
353 | MACSTR " (%d seconds)", MAC2STR(sm->addr), | |
354 | sm->wpa_auth->conf.wpa_ptk_rekey); | |
355 | eloop_cancel_timeout(wpa_rekey_ptk, sm->wpa_auth, sm); | |
356 | eloop_register_timeout(sm->wpa_auth->conf.wpa_ptk_rekey, 0, | |
357 | wpa_rekey_ptk, sm->wpa_auth, sm); | |
358 | } | |
359 | } | |
360 | ||
361 | ||
6fc6879b JM |
362 | static int wpa_auth_pmksa_clear_cb(struct wpa_state_machine *sm, void *ctx) |
363 | { | |
364 | if (sm->pmksa == ctx) | |
365 | sm->pmksa = NULL; | |
366 | return 0; | |
367 | } | |
368 | ||
369 | ||
370 | static void wpa_auth_pmksa_free_cb(struct rsn_pmksa_cache_entry *entry, | |
371 | void *ctx) | |
372 | { | |
373 | struct wpa_authenticator *wpa_auth = ctx; | |
374 | wpa_auth_for_each_sta(wpa_auth, wpa_auth_pmksa_clear_cb, entry); | |
375 | } | |
376 | ||
377 | ||
1bdb7ab3 JM |
378 | static int wpa_group_init_gmk_and_counter(struct wpa_authenticator *wpa_auth, |
379 | struct wpa_group *group) | |
380 | { | |
559cdabb | 381 | u8 buf[ETH_ALEN + 8 + sizeof(unsigned long)]; |
1bdb7ab3 | 382 | u8 rkey[32]; |
559cdabb | 383 | unsigned long ptr; |
1bdb7ab3 | 384 | |
3642c431 | 385 | if (random_get_bytes(group->GMK, WPA_GMK_LEN) < 0) |
1bdb7ab3 JM |
386 | return -1; |
387 | wpa_hexdump_key(MSG_DEBUG, "GMK", group->GMK, WPA_GMK_LEN); | |
388 | ||
389 | /* | |
390 | * Counter = PRF-256(Random number, "Init Counter", | |
391 | * Local MAC Address || Time) | |
392 | */ | |
393 | os_memcpy(buf, wpa_auth->addr, ETH_ALEN); | |
394 | wpa_get_ntp_timestamp(buf + ETH_ALEN); | |
559cdabb JM |
395 | ptr = (unsigned long) group; |
396 | os_memcpy(buf + ETH_ALEN + 8, &ptr, sizeof(ptr)); | |
525923b1 JM |
397 | #ifdef TEST_FUZZ |
398 | os_memset(buf + ETH_ALEN, 0xab, 8); | |
399 | os_memset(buf + ETH_ALEN + 8, 0xcd, sizeof(ptr)); | |
400 | #endif /* TEST_FUZZ */ | |
3642c431 | 401 | if (random_get_bytes(rkey, sizeof(rkey)) < 0) |
1bdb7ab3 JM |
402 | return -1; |
403 | ||
404 | if (sha1_prf(rkey, sizeof(rkey), "Init Counter", buf, sizeof(buf), | |
405 | group->Counter, WPA_NONCE_LEN) < 0) | |
406 | return -1; | |
407 | wpa_hexdump_key(MSG_DEBUG, "Key Counter", | |
408 | group->Counter, WPA_NONCE_LEN); | |
409 | ||
410 | return 0; | |
411 | } | |
412 | ||
413 | ||
e6965d4e | 414 | static struct wpa_group * wpa_group_init(struct wpa_authenticator *wpa_auth, |
bdffdc5d | 415 | int vlan_id, int delay_init) |
e6965d4e JM |
416 | { |
417 | struct wpa_group *group; | |
e6965d4e JM |
418 | |
419 | group = os_zalloc(sizeof(struct wpa_group)); | |
420 | if (group == NULL) | |
421 | return NULL; | |
422 | ||
423 | group->GTKAuthenticator = TRUE; | |
424 | group->vlan_id = vlan_id; | |
c3550295 | 425 | group->GTK_len = wpa_cipher_key_len(wpa_auth->conf.wpa_group); |
6fc6879b | 426 | |
08704cd8 JM |
427 | if (random_pool_ready() != 1) { |
428 | wpa_printf(MSG_INFO, "WPA: Not enough entropy in random pool " | |
429 | "for secure operations - update keys later when " | |
430 | "the first station connects"); | |
431 | } | |
432 | ||
1bdb7ab3 JM |
433 | /* |
434 | * Set initial GMK/Counter value here. The actual values that will be | |
435 | * used in negotiations will be set once the first station tries to | |
436 | * connect. This allows more time for collecting additional randomness | |
437 | * on embedded devices. | |
6fc6879b | 438 | */ |
1bdb7ab3 | 439 | if (wpa_group_init_gmk_and_counter(wpa_auth, group) < 0) { |
6fc6879b JM |
440 | wpa_printf(MSG_ERROR, "Failed to get random data for WPA " |
441 | "initialization."); | |
442 | os_free(group); | |
443 | return NULL; | |
444 | } | |
6fc6879b JM |
445 | |
446 | group->GInit = TRUE; | |
bdffdc5d JM |
447 | if (delay_init) { |
448 | wpa_printf(MSG_DEBUG, "WPA: Delay group state machine start " | |
449 | "until Beacon frames have been configured"); | |
450 | /* Initialization is completed in wpa_init_keys(). */ | |
451 | } else { | |
452 | wpa_group_sm_step(wpa_auth, group); | |
453 | group->GInit = FALSE; | |
454 | wpa_group_sm_step(wpa_auth, group); | |
455 | } | |
6fc6879b JM |
456 | |
457 | return group; | |
458 | } | |
459 | ||
460 | ||
461 | /** | |
462 | * wpa_init - Initialize WPA authenticator | |
463 | * @addr: Authenticator address | |
464 | * @conf: Configuration for WPA authenticator | |
a17df5fb | 465 | * @cb: Callback functions for WPA authenticator |
6fc6879b JM |
466 | * Returns: Pointer to WPA authenticator data or %NULL on failure |
467 | */ | |
468 | struct wpa_authenticator * wpa_init(const u8 *addr, | |
469 | struct wpa_auth_config *conf, | |
cef8fac0 JB |
470 | const struct wpa_auth_callbacks *cb, |
471 | void *cb_ctx) | |
6fc6879b JM |
472 | { |
473 | struct wpa_authenticator *wpa_auth; | |
474 | ||
475 | wpa_auth = os_zalloc(sizeof(struct wpa_authenticator)); | |
476 | if (wpa_auth == NULL) | |
477 | return NULL; | |
478 | os_memcpy(wpa_auth->addr, addr, ETH_ALEN); | |
479 | os_memcpy(&wpa_auth->conf, conf, sizeof(*conf)); | |
cef8fac0 JB |
480 | wpa_auth->cb = cb; |
481 | wpa_auth->cb_ctx = cb_ctx; | |
6fc6879b JM |
482 | |
483 | if (wpa_auth_gen_wpa_ie(wpa_auth)) { | |
484 | wpa_printf(MSG_ERROR, "Could not generate WPA IE."); | |
485 | os_free(wpa_auth); | |
486 | return NULL; | |
487 | } | |
488 | ||
bdffdc5d | 489 | wpa_auth->group = wpa_group_init(wpa_auth, 0, 1); |
6fc6879b JM |
490 | if (wpa_auth->group == NULL) { |
491 | os_free(wpa_auth->wpa_ie); | |
492 | os_free(wpa_auth); | |
493 | return NULL; | |
494 | } | |
495 | ||
4bb081f1 JM |
496 | wpa_auth->pmksa = pmksa_cache_auth_init(wpa_auth_pmksa_free_cb, |
497 | wpa_auth); | |
6fc6879b JM |
498 | if (wpa_auth->pmksa == NULL) { |
499 | wpa_printf(MSG_ERROR, "PMKSA cache initialization failed."); | |
27d9701d | 500 | os_free(wpa_auth->group); |
6fc6879b JM |
501 | os_free(wpa_auth->wpa_ie); |
502 | os_free(wpa_auth); | |
503 | return NULL; | |
504 | } | |
505 | ||
4ec1fd8e | 506 | #ifdef CONFIG_IEEE80211R_AP |
6fc6879b JM |
507 | wpa_auth->ft_pmk_cache = wpa_ft_pmk_cache_init(); |
508 | if (wpa_auth->ft_pmk_cache == NULL) { | |
509 | wpa_printf(MSG_ERROR, "FT PMK cache initialization failed."); | |
27d9701d | 510 | os_free(wpa_auth->group); |
6fc6879b | 511 | os_free(wpa_auth->wpa_ie); |
4bb081f1 | 512 | pmksa_cache_auth_deinit(wpa_auth->pmksa); |
6fc6879b JM |
513 | os_free(wpa_auth); |
514 | return NULL; | |
515 | } | |
4ec1fd8e | 516 | #endif /* CONFIG_IEEE80211R_AP */ |
6fc6879b JM |
517 | |
518 | if (wpa_auth->conf.wpa_gmk_rekey) { | |
519 | eloop_register_timeout(wpa_auth->conf.wpa_gmk_rekey, 0, | |
520 | wpa_rekey_gmk, wpa_auth, NULL); | |
521 | } | |
522 | ||
523 | if (wpa_auth->conf.wpa_group_rekey) { | |
524 | eloop_register_timeout(wpa_auth->conf.wpa_group_rekey, 0, | |
525 | wpa_rekey_gtk, wpa_auth, NULL); | |
526 | } | |
527 | ||
25ef8529 JM |
528 | #ifdef CONFIG_P2P |
529 | if (WPA_GET_BE32(conf->ip_addr_start)) { | |
530 | int count = WPA_GET_BE32(conf->ip_addr_end) - | |
531 | WPA_GET_BE32(conf->ip_addr_start) + 1; | |
532 | if (count > 1000) | |
533 | count = 1000; | |
534 | if (count > 0) | |
535 | wpa_auth->ip_pool = bitfield_alloc(count); | |
536 | } | |
537 | #endif /* CONFIG_P2P */ | |
538 | ||
6fc6879b JM |
539 | return wpa_auth; |
540 | } | |
541 | ||
542 | ||
bdffdc5d JM |
543 | int wpa_init_keys(struct wpa_authenticator *wpa_auth) |
544 | { | |
545 | struct wpa_group *group = wpa_auth->group; | |
546 | ||
547 | wpa_printf(MSG_DEBUG, "WPA: Start group state machine to set initial " | |
548 | "keys"); | |
549 | wpa_group_sm_step(wpa_auth, group); | |
550 | group->GInit = FALSE; | |
551 | wpa_group_sm_step(wpa_auth, group); | |
7d7f7be2 JM |
552 | if (group->wpa_group_state == WPA_GROUP_FATAL_FAILURE) |
553 | return -1; | |
bdffdc5d JM |
554 | return 0; |
555 | } | |
556 | ||
557 | ||
6fc6879b JM |
558 | /** |
559 | * wpa_deinit - Deinitialize WPA authenticator | |
560 | * @wpa_auth: Pointer to WPA authenticator data from wpa_init() | |
561 | */ | |
562 | void wpa_deinit(struct wpa_authenticator *wpa_auth) | |
563 | { | |
564 | struct wpa_group *group, *prev; | |
565 | ||
566 | eloop_cancel_timeout(wpa_rekey_gmk, wpa_auth, NULL); | |
567 | eloop_cancel_timeout(wpa_rekey_gtk, wpa_auth, NULL); | |
568 | ||
4bb081f1 | 569 | pmksa_cache_auth_deinit(wpa_auth->pmksa); |
6fc6879b | 570 | |
4ec1fd8e | 571 | #ifdef CONFIG_IEEE80211R_AP |
6fc6879b JM |
572 | wpa_ft_pmk_cache_deinit(wpa_auth->ft_pmk_cache); |
573 | wpa_auth->ft_pmk_cache = NULL; | |
eefe8630 | 574 | wpa_ft_deinit(wpa_auth); |
4ec1fd8e | 575 | #endif /* CONFIG_IEEE80211R_AP */ |
6fc6879b | 576 | |
25ef8529 JM |
577 | #ifdef CONFIG_P2P |
578 | bitfield_free(wpa_auth->ip_pool); | |
579 | #endif /* CONFIG_P2P */ | |
580 | ||
581 | ||
6fc6879b JM |
582 | os_free(wpa_auth->wpa_ie); |
583 | ||
584 | group = wpa_auth->group; | |
585 | while (group) { | |
586 | prev = group; | |
587 | group = group->next; | |
588 | os_free(prev); | |
589 | } | |
590 | ||
591 | os_free(wpa_auth); | |
592 | } | |
593 | ||
594 | ||
595 | /** | |
596 | * wpa_reconfig - Update WPA authenticator configuration | |
597 | * @wpa_auth: Pointer to WPA authenticator data from wpa_init() | |
598 | * @conf: Configuration for WPA authenticator | |
599 | */ | |
600 | int wpa_reconfig(struct wpa_authenticator *wpa_auth, | |
601 | struct wpa_auth_config *conf) | |
602 | { | |
e6965d4e | 603 | struct wpa_group *group; |
6fc6879b JM |
604 | if (wpa_auth == NULL) |
605 | return 0; | |
606 | ||
607 | os_memcpy(&wpa_auth->conf, conf, sizeof(*conf)); | |
ad08c363 JM |
608 | if (wpa_auth_gen_wpa_ie(wpa_auth)) { |
609 | wpa_printf(MSG_ERROR, "Could not generate WPA IE."); | |
610 | return -1; | |
611 | } | |
612 | ||
e6965d4e JM |
613 | /* |
614 | * Reinitialize GTK to make sure it is suitable for the new | |
615 | * configuration. | |
616 | */ | |
617 | group = wpa_auth->group; | |
c3550295 | 618 | group->GTK_len = wpa_cipher_key_len(wpa_auth->conf.wpa_group); |
e6965d4e JM |
619 | group->GInit = TRUE; |
620 | wpa_group_sm_step(wpa_auth, group); | |
621 | group->GInit = FALSE; | |
622 | wpa_group_sm_step(wpa_auth, group); | |
623 | ||
6fc6879b JM |
624 | return 0; |
625 | } | |
626 | ||
627 | ||
628 | struct wpa_state_machine * | |
94ddef3e JM |
629 | wpa_auth_sta_init(struct wpa_authenticator *wpa_auth, const u8 *addr, |
630 | const u8 *p2p_dev_addr) | |
6fc6879b JM |
631 | { |
632 | struct wpa_state_machine *sm; | |
633 | ||
7d7f7be2 JM |
634 | if (wpa_auth->group->wpa_group_state == WPA_GROUP_FATAL_FAILURE) |
635 | return NULL; | |
636 | ||
6fc6879b JM |
637 | sm = os_zalloc(sizeof(struct wpa_state_machine)); |
638 | if (sm == NULL) | |
639 | return NULL; | |
640 | os_memcpy(sm->addr, addr, ETH_ALEN); | |
94ddef3e JM |
641 | if (p2p_dev_addr) |
642 | os_memcpy(sm->p2p_dev_addr, p2p_dev_addr, ETH_ALEN); | |
6fc6879b JM |
643 | |
644 | sm->wpa_auth = wpa_auth; | |
645 | sm->group = wpa_auth->group; | |
a0ad9e8c | 646 | wpa_group_get(sm->wpa_auth, sm->group); |
6fc6879b JM |
647 | |
648 | return sm; | |
649 | } | |
650 | ||
651 | ||
6f9b5d16 JM |
652 | int wpa_auth_sta_associated(struct wpa_authenticator *wpa_auth, |
653 | struct wpa_state_machine *sm) | |
6fc6879b JM |
654 | { |
655 | if (wpa_auth == NULL || !wpa_auth->conf.wpa || sm == NULL) | |
6f9b5d16 | 656 | return -1; |
6fc6879b | 657 | |
4ec1fd8e | 658 | #ifdef CONFIG_IEEE80211R_AP |
6fc6879b JM |
659 | if (sm->ft_completed) { |
660 | wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG, | |
661 | "FT authentication already completed - do not " | |
662 | "start 4-way handshake"); | |
3d4d2348 JM |
663 | /* Go to PTKINITDONE state to allow GTK rekeying */ |
664 | sm->wpa_ptk_state = WPA_PTK_PTKINITDONE; | |
3bbc4705 | 665 | sm->Pair = TRUE; |
6f9b5d16 | 666 | return 0; |
6fc6879b | 667 | } |
4ec1fd8e | 668 | #endif /* CONFIG_IEEE80211R_AP */ |
6fc6879b | 669 | |
07e0117d JM |
670 | #ifdef CONFIG_FILS |
671 | if (sm->fils_completed) { | |
672 | wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG, | |
673 | "FILS authentication already completed - do not start 4-way handshake"); | |
674 | /* Go to PTKINITDONE state to allow GTK rekeying */ | |
675 | sm->wpa_ptk_state = WPA_PTK_PTKINITDONE; | |
3bbc4705 | 676 | sm->Pair = TRUE; |
07e0117d JM |
677 | return 0; |
678 | } | |
679 | #endif /* CONFIG_FILS */ | |
680 | ||
6fc6879b | 681 | if (sm->started) { |
22a299ee | 682 | os_memset(&sm->key_replay, 0, sizeof(sm->key_replay)); |
6fc6879b | 683 | sm->ReAuthenticationRequest = TRUE; |
6f9b5d16 | 684 | return wpa_sm_step(sm); |
6fc6879b JM |
685 | } |
686 | ||
687 | wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG, | |
688 | "start authentication"); | |
689 | sm->started = 1; | |
690 | ||
691 | sm->Init = TRUE; | |
e4a6ea1d | 692 | if (wpa_sm_step(sm) == 1) |
6f9b5d16 | 693 | return 1; /* should not really happen */ |
6fc6879b JM |
694 | sm->Init = FALSE; |
695 | sm->AuthenticationRequest = TRUE; | |
6f9b5d16 | 696 | return wpa_sm_step(sm); |
6fc6879b JM |
697 | } |
698 | ||
699 | ||
a8d05fca JM |
700 | void wpa_auth_sta_no_wpa(struct wpa_state_machine *sm) |
701 | { | |
702 | /* WPA/RSN was not used - clear WPA state. This is needed if the STA | |
703 | * reassociates back to the same AP while the previous entry for the | |
704 | * STA has not yet been removed. */ | |
705 | if (sm == NULL) | |
706 | return; | |
707 | ||
708 | sm->wpa_key_mgmt = 0; | |
709 | } | |
710 | ||
711 | ||
6fc6879b JM |
712 | static void wpa_free_sta_sm(struct wpa_state_machine *sm) |
713 | { | |
25ef8529 JM |
714 | #ifdef CONFIG_P2P |
715 | if (WPA_GET_BE32(sm->ip_addr)) { | |
716 | u32 start; | |
717 | wpa_printf(MSG_DEBUG, "P2P: Free assigned IP " | |
718 | "address %u.%u.%u.%u from " MACSTR, | |
719 | sm->ip_addr[0], sm->ip_addr[1], | |
720 | sm->ip_addr[2], sm->ip_addr[3], | |
721 | MAC2STR(sm->addr)); | |
722 | start = WPA_GET_BE32(sm->wpa_auth->conf.ip_addr_start); | |
723 | bitfield_clear(sm->wpa_auth->ip_pool, | |
724 | WPA_GET_BE32(sm->ip_addr) - start); | |
725 | } | |
726 | #endif /* CONFIG_P2P */ | |
2ade8ef2 JM |
727 | if (sm->GUpdateStationKeys) { |
728 | sm->group->GKeyDoneStations--; | |
729 | sm->GUpdateStationKeys = FALSE; | |
730 | } | |
4ec1fd8e | 731 | #ifdef CONFIG_IEEE80211R_AP |
0f857f43 | 732 | os_free(sm->assoc_resp_ftie); |
692ec305 | 733 | wpabuf_free(sm->ft_pending_req_ies); |
4ec1fd8e | 734 | #endif /* CONFIG_IEEE80211R_AP */ |
6fc6879b JM |
735 | os_free(sm->last_rx_eapol_key); |
736 | os_free(sm->wpa_ie); | |
d3516cad | 737 | os_free(sm->rsnxe); |
a0ad9e8c | 738 | wpa_group_put(sm->wpa_auth, sm->group); |
10ec6a5f JM |
739 | #ifdef CONFIG_DPP2 |
740 | wpabuf_clear_free(sm->dpp_z); | |
741 | #endif /* CONFIG_DPP2 */ | |
09ab81b9 | 742 | bin_clear_free(sm, sizeof(*sm)); |
6fc6879b JM |
743 | } |
744 | ||
745 | ||
746 | void wpa_auth_sta_deinit(struct wpa_state_machine *sm) | |
747 | { | |
62e12afc JM |
748 | struct wpa_authenticator *wpa_auth; |
749 | ||
6fc6879b JM |
750 | if (sm == NULL) |
751 | return; | |
752 | ||
62e12afc JM |
753 | wpa_auth = sm->wpa_auth; |
754 | if (wpa_auth->conf.wpa_strict_rekey && sm->has_GTK) { | |
755 | wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG, | |
6fc6879b JM |
756 | "strict rekeying - force GTK rekey since STA " |
757 | "is leaving"); | |
3f5a1860 | 758 | if (eloop_deplete_timeout(0, 500000, wpa_rekey_gtk, |
62e12afc JM |
759 | wpa_auth, NULL) == -1) |
760 | eloop_register_timeout(0, 500000, wpa_rekey_gtk, | |
761 | wpa_auth, NULL); | |
6fc6879b JM |
762 | } |
763 | ||
62e12afc | 764 | eloop_cancel_timeout(wpa_send_eapol_timeout, wpa_auth, sm); |
e4bf4db9 | 765 | sm->pending_1_of_4_timeout = 0; |
6fc6879b | 766 | eloop_cancel_timeout(wpa_sm_call_step, sm, NULL); |
62e12afc | 767 | eloop_cancel_timeout(wpa_rekey_ptk, wpa_auth, sm); |
3a46cf93 MB |
768 | #ifdef CONFIG_IEEE80211R_AP |
769 | wpa_ft_sta_deinit(sm); | |
770 | #endif /* CONFIG_IEEE80211R_AP */ | |
6fc6879b JM |
771 | if (sm->in_step_loop) { |
772 | /* Must not free state machine while wpa_sm_step() is running. | |
773 | * Freeing will be completed in the end of wpa_sm_step(). */ | |
774 | wpa_printf(MSG_DEBUG, "WPA: Registering pending STA state " | |
775 | "machine deinit for " MACSTR, MAC2STR(sm->addr)); | |
776 | sm->pending_deinit = 1; | |
777 | } else | |
778 | wpa_free_sta_sm(sm); | |
779 | } | |
780 | ||
781 | ||
782 | static void wpa_request_new_ptk(struct wpa_state_machine *sm) | |
783 | { | |
784 | if (sm == NULL) | |
785 | return; | |
786 | ||
862aac1f | 787 | if (!sm->use_ext_key_id && sm->wpa_auth->conf.wpa_deny_ptk0_rekey) { |
1a7963e3 AW |
788 | wpa_printf(MSG_INFO, |
789 | "WPA: PTK0 rekey not allowed, disconnect " MACSTR, | |
790 | MAC2STR(sm->addr)); | |
791 | sm->Disconnect = TRUE; | |
792 | /* Try to encourage the STA to reconnect */ | |
793 | sm->disconnect_reason = | |
794 | WLAN_REASON_CLASS3_FRAME_FROM_NONASSOC_STA; | |
795 | } else { | |
862aac1f AW |
796 | if (sm->use_ext_key_id) |
797 | sm->keyidx_active ^= 1; /* flip Key ID */ | |
1a7963e3 AW |
798 | sm->PTKRequest = TRUE; |
799 | sm->PTK_valid = 0; | |
800 | } | |
6fc6879b JM |
801 | } |
802 | ||
803 | ||
68921e24 | 804 | static int wpa_replay_counter_valid(struct wpa_key_replay_counter *ctr, |
22a299ee JM |
805 | const u8 *replay_counter) |
806 | { | |
807 | int i; | |
808 | for (i = 0; i < RSNA_MAX_EAPOL_RETRIES; i++) { | |
68921e24 | 809 | if (!ctr[i].valid) |
22a299ee | 810 | break; |
68921e24 | 811 | if (os_memcmp(replay_counter, ctr[i].counter, |
22a299ee JM |
812 | WPA_REPLAY_COUNTER_LEN) == 0) |
813 | return 1; | |
814 | } | |
815 | return 0; | |
816 | } | |
817 | ||
818 | ||
68921e24 JM |
819 | static void wpa_replay_counter_mark_invalid(struct wpa_key_replay_counter *ctr, |
820 | const u8 *replay_counter) | |
821 | { | |
822 | int i; | |
823 | for (i = 0; i < RSNA_MAX_EAPOL_RETRIES; i++) { | |
824 | if (ctr[i].valid && | |
825 | (replay_counter == NULL || | |
826 | os_memcmp(replay_counter, ctr[i].counter, | |
827 | WPA_REPLAY_COUNTER_LEN) == 0)) | |
828 | ctr[i].valid = FALSE; | |
829 | } | |
830 | } | |
831 | ||
832 | ||
4ec1fd8e | 833 | #ifdef CONFIG_IEEE80211R_AP |
0f857f43 JM |
834 | static int ft_check_msg_2_of_4(struct wpa_authenticator *wpa_auth, |
835 | struct wpa_state_machine *sm, | |
836 | struct wpa_eapol_ie_parse *kde) | |
837 | { | |
838 | struct wpa_ie_data ie; | |
839 | struct rsn_mdie *mdie; | |
840 | ||
841 | if (wpa_parse_wpa_ie_rsn(kde->rsn_ie, kde->rsn_ie_len, &ie) < 0 || | |
842 | ie.num_pmkid != 1 || ie.pmkid == NULL) { | |
843 | wpa_printf(MSG_DEBUG, "FT: No PMKR1Name in " | |
844 | "FT 4-way handshake message 2/4"); | |
845 | return -1; | |
846 | } | |
847 | ||
848 | os_memcpy(sm->sup_pmk_r1_name, ie.pmkid, PMKID_LEN); | |
849 | wpa_hexdump(MSG_DEBUG, "FT: PMKR1Name from Supplicant", | |
850 | sm->sup_pmk_r1_name, PMKID_LEN); | |
851 | ||
852 | if (!kde->mdie || !kde->ftie) { | |
853 | wpa_printf(MSG_DEBUG, "FT: No %s in FT 4-way handshake " | |
854 | "message 2/4", kde->mdie ? "FTIE" : "MDIE"); | |
855 | return -1; | |
856 | } | |
857 | ||
858 | mdie = (struct rsn_mdie *) (kde->mdie + 2); | |
859 | if (kde->mdie[1] < sizeof(struct rsn_mdie) || | |
860 | os_memcmp(wpa_auth->conf.mobility_domain, mdie->mobility_domain, | |
861 | MOBILITY_DOMAIN_ID_LEN) != 0) { | |
862 | wpa_printf(MSG_DEBUG, "FT: MDIE mismatch"); | |
863 | return -1; | |
864 | } | |
865 | ||
866 | if (sm->assoc_resp_ftie && | |
867 | (kde->ftie[1] != sm->assoc_resp_ftie[1] || | |
868 | os_memcmp(kde->ftie, sm->assoc_resp_ftie, | |
869 | 2 + sm->assoc_resp_ftie[1]) != 0)) { | |
870 | wpa_printf(MSG_DEBUG, "FT: FTIE mismatch"); | |
871 | wpa_hexdump(MSG_DEBUG, "FT: FTIE in EAPOL-Key msg 2/4", | |
872 | kde->ftie, kde->ftie_len); | |
873 | wpa_hexdump(MSG_DEBUG, "FT: FTIE in (Re)AssocResp", | |
874 | sm->assoc_resp_ftie, 2 + sm->assoc_resp_ftie[1]); | |
875 | return -1; | |
876 | } | |
877 | ||
878 | return 0; | |
879 | } | |
4ec1fd8e | 880 | #endif /* CONFIG_IEEE80211R_AP */ |
0f857f43 JM |
881 | |
882 | ||
c772d054 JM |
883 | static int wpa_receive_error_report(struct wpa_authenticator *wpa_auth, |
884 | struct wpa_state_machine *sm, int group) | |
ec027805 JM |
885 | { |
886 | /* Supplicant reported a Michael MIC error */ | |
887 | wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_INFO, | |
888 | "received EAPOL-Key Error Request " | |
889 | "(STA detected Michael MIC failure (group=%d))", | |
890 | group); | |
fbc72d32 JM |
891 | |
892 | if (group && wpa_auth->conf.wpa_group != WPA_CIPHER_TKIP) { | |
893 | wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO, | |
894 | "ignore Michael MIC failure report since " | |
895 | "group cipher is not TKIP"); | |
896 | } else if (!group && sm->pairwise != WPA_CIPHER_TKIP) { | |
897 | wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO, | |
898 | "ignore Michael MIC failure report since " | |
899 | "pairwise cipher is not TKIP"); | |
900 | } else { | |
c772d054 JM |
901 | if (wpa_auth_mic_failure_report(wpa_auth, sm->addr) > 0) |
902 | return 1; /* STA entry was removed */ | |
fbc72d32 JM |
903 | sm->dot11RSNAStatsTKIPRemoteMICFailures++; |
904 | wpa_auth->dot11RSNAStatsTKIPRemoteMICFailures++; | |
905 | } | |
ec027805 JM |
906 | |
907 | /* | |
908 | * Error report is not a request for a new key handshake, but since | |
909 | * Authenticator may do it, let's change the keys now anyway. | |
910 | */ | |
911 | wpa_request_new_ptk(sm); | |
c772d054 | 912 | return 0; |
ec027805 JM |
913 | } |
914 | ||
915 | ||
f23c5b17 JM |
916 | static int wpa_try_alt_snonce(struct wpa_state_machine *sm, u8 *data, |
917 | size_t data_len) | |
918 | { | |
919 | struct wpa_ptk PTK; | |
920 | int ok = 0; | |
921 | const u8 *pmk = NULL; | |
7a12edd1 | 922 | size_t pmk_len; |
dbfa691d | 923 | int vlan_id = 0; |
f23c5b17 | 924 | |
b729fd8d | 925 | os_memset(&PTK, 0, sizeof(PTK)); |
f23c5b17 | 926 | for (;;) { |
e61fea6b JM |
927 | if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt) && |
928 | !wpa_key_mgmt_sae(sm->wpa_key_mgmt)) { | |
f23c5b17 | 929 | pmk = wpa_auth_get_psk(sm->wpa_auth, sm->addr, |
dbfa691d JM |
930 | sm->p2p_dev_addr, pmk, &pmk_len, |
931 | &vlan_id); | |
f23c5b17 JM |
932 | if (pmk == NULL) |
933 | break; | |
f9854c18 MK |
934 | #ifdef CONFIG_IEEE80211R_AP |
935 | if (wpa_key_mgmt_ft_psk(sm->wpa_key_mgmt)) { | |
936 | os_memcpy(sm->xxkey, pmk, pmk_len); | |
937 | sm->xxkey_len = pmk_len; | |
938 | } | |
939 | #endif /* CONFIG_IEEE80211R_AP */ | |
207976f0 | 940 | } else { |
f23c5b17 | 941 | pmk = sm->PMK; |
207976f0 JM |
942 | pmk_len = sm->pmk_len; |
943 | } | |
f23c5b17 | 944 | |
65a44e84 JM |
945 | if (wpa_derive_ptk(sm, sm->alt_SNonce, pmk, pmk_len, &PTK, 0) < |
946 | 0) | |
364c064a | 947 | break; |
f23c5b17 | 948 | |
567da5bb JM |
949 | if (wpa_verify_key_mic(sm->wpa_key_mgmt, pmk_len, &PTK, |
950 | data, data_len) == 0) { | |
08dc8efd JM |
951 | if (sm->PMK != pmk) { |
952 | os_memcpy(sm->PMK, pmk, pmk_len); | |
953 | sm->pmk_len = pmk_len; | |
954 | } | |
f23c5b17 JM |
955 | ok = 1; |
956 | break; | |
957 | } | |
958 | ||
f5e0a332 JM |
959 | if (!wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt) || |
960 | wpa_key_mgmt_sae(sm->wpa_key_mgmt)) | |
f23c5b17 JM |
961 | break; |
962 | } | |
963 | ||
964 | if (!ok) { | |
965 | wpa_printf(MSG_DEBUG, | |
966 | "WPA: Earlier SNonce did not result in matching MIC"); | |
967 | return -1; | |
968 | } | |
969 | ||
970 | wpa_printf(MSG_DEBUG, | |
971 | "WPA: Earlier SNonce resulted in matching MIC"); | |
972 | sm->alt_snonce_valid = 0; | |
dbfa691d JM |
973 | |
974 | if (vlan_id && wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt) && | |
975 | wpa_auth_update_vlan(sm->wpa_auth, sm->addr, vlan_id) < 0) | |
976 | return -1; | |
977 | ||
f23c5b17 JM |
978 | os_memcpy(sm->SNonce, sm->alt_SNonce, WPA_NONCE_LEN); |
979 | os_memcpy(&sm->PTK, &PTK, sizeof(PTK)); | |
31bc66e4 | 980 | forced_memzero(&PTK, sizeof(PTK)); |
f23c5b17 JM |
981 | sm->PTK_valid = TRUE; |
982 | ||
983 | return 0; | |
984 | } | |
985 | ||
986 | ||
6fc6879b JM |
987 | void wpa_receive(struct wpa_authenticator *wpa_auth, |
988 | struct wpa_state_machine *sm, | |
989 | u8 *data, size_t data_len) | |
990 | { | |
991 | struct ieee802_1x_hdr *hdr; | |
992 | struct wpa_eapol_key *key; | |
993 | u16 key_info, key_data_length; | |
a0bf1b68 | 994 | enum { PAIRWISE_2, PAIRWISE_4, GROUP_2, REQUEST } msg; |
6fc6879b JM |
995 | char *msgtxt; |
996 | struct wpa_eapol_ie_parse kde; | |
3b5b7aa8 JM |
997 | const u8 *key_data; |
998 | size_t keyhdrlen, mic_len; | |
6d014ffc | 999 | u8 *mic; |
6fc6879b JM |
1000 | |
1001 | if (wpa_auth == NULL || !wpa_auth->conf.wpa || sm == NULL) | |
1002 | return; | |
dc5bad48 | 1003 | wpa_hexdump(MSG_MSGDUMP, "WPA: RX EAPOL data", data, data_len); |
6fc6879b | 1004 | |
567da5bb | 1005 | mic_len = wpa_mic_len(sm->wpa_key_mgmt, sm->pmk_len); |
6d014ffc | 1006 | keyhdrlen = sizeof(*key) + mic_len + 2; |
5e3b5197 | 1007 | |
dc5bad48 JM |
1008 | if (data_len < sizeof(*hdr) + keyhdrlen) { |
1009 | wpa_printf(MSG_DEBUG, "WPA: Ignore too short EAPOL-Key frame"); | |
6fc6879b | 1010 | return; |
dc5bad48 | 1011 | } |
6fc6879b JM |
1012 | |
1013 | hdr = (struct ieee802_1x_hdr *) data; | |
1014 | key = (struct wpa_eapol_key *) (hdr + 1); | |
6d014ffc | 1015 | mic = (u8 *) (key + 1); |
6fc6879b | 1016 | key_info = WPA_GET_BE16(key->key_info); |
6d014ffc JM |
1017 | key_data = mic + mic_len + 2; |
1018 | key_data_length = WPA_GET_BE16(mic + mic_len); | |
0d442aff | 1019 | wpa_printf(MSG_DEBUG, "WPA: Received EAPOL-Key from " MACSTR |
dc5bad48 JM |
1020 | " key_info=0x%x type=%u mic_len=%u key_data_length=%u", |
1021 | MAC2STR(sm->addr), key_info, key->type, | |
1022 | (unsigned int) mic_len, key_data_length); | |
1023 | wpa_hexdump(MSG_MSGDUMP, | |
1024 | "WPA: EAPOL-Key header (ending before Key MIC)", | |
1025 | key, sizeof(*key)); | |
1026 | wpa_hexdump(MSG_MSGDUMP, "WPA: EAPOL-Key Key MIC", | |
1027 | mic, mic_len); | |
5e3b5197 | 1028 | if (key_data_length > data_len - sizeof(*hdr) - keyhdrlen) { |
6fc6879b JM |
1029 | wpa_printf(MSG_INFO, "WPA: Invalid EAPOL-Key frame - " |
1030 | "key_data overflow (%d > %lu)", | |
1031 | key_data_length, | |
1032 | (unsigned long) (data_len - sizeof(*hdr) - | |
5e3b5197 | 1033 | keyhdrlen)); |
6fc6879b JM |
1034 | return; |
1035 | } | |
1036 | ||
f8e96eb6 | 1037 | if (sm->wpa == WPA_VERSION_WPA2) { |
74590e71 JM |
1038 | if (key->type == EAPOL_KEY_TYPE_WPA) { |
1039 | /* | |
1040 | * Some deployed station implementations seem to send | |
1041 | * msg 4/4 with incorrect type value in WPA2 mode. | |
1042 | */ | |
1043 | wpa_printf(MSG_DEBUG, "Workaround: Allow EAPOL-Key " | |
1044 | "with unexpected WPA type in RSN mode"); | |
1045 | } else if (key->type != EAPOL_KEY_TYPE_RSN) { | |
f8e96eb6 JM |
1046 | wpa_printf(MSG_DEBUG, "Ignore EAPOL-Key with " |
1047 | "unexpected type %d in RSN mode", | |
1048 | key->type); | |
1049 | return; | |
1050 | } | |
1051 | } else { | |
1052 | if (key->type != EAPOL_KEY_TYPE_WPA) { | |
1053 | wpa_printf(MSG_DEBUG, "Ignore EAPOL-Key with " | |
1054 | "unexpected type %d in WPA mode", | |
1055 | key->type); | |
1056 | return; | |
1057 | } | |
1058 | } | |
1059 | ||
bc8318ac JM |
1060 | wpa_hexdump(MSG_DEBUG, "WPA: Received Key Nonce", key->key_nonce, |
1061 | WPA_NONCE_LEN); | |
1062 | wpa_hexdump(MSG_DEBUG, "WPA: Received Replay Counter", | |
1063 | key->replay_counter, WPA_REPLAY_COUNTER_LEN); | |
1064 | ||
6fc6879b JM |
1065 | /* FIX: verify that the EAPOL-Key frame was encrypted if pairwise keys |
1066 | * are set */ | |
1067 | ||
a0bf1b68 JM |
1068 | if (key_info & WPA_KEY_INFO_SMK_MESSAGE) { |
1069 | wpa_printf(MSG_DEBUG, "WPA: Ignore SMK message"); | |
1070 | return; | |
1071 | } | |
1072 | ||
1073 | if (key_info & WPA_KEY_INFO_REQUEST) { | |
6fc6879b JM |
1074 | msg = REQUEST; |
1075 | msgtxt = "Request"; | |
1076 | } else if (!(key_info & WPA_KEY_INFO_KEY_TYPE)) { | |
1077 | msg = GROUP_2; | |
1078 | msgtxt = "2/2 Group"; | |
3b5b7aa8 JM |
1079 | } else if (key_data_length == 0 || |
1080 | (mic_len == 0 && (key_info & WPA_KEY_INFO_ENCR_KEY_DATA) && | |
1081 | key_data_length == AES_BLOCK_SIZE)) { | |
6fc6879b JM |
1082 | msg = PAIRWISE_4; |
1083 | msgtxt = "4/4 Pairwise"; | |
1084 | } else { | |
1085 | msg = PAIRWISE_2; | |
1086 | msgtxt = "2/4 Pairwise"; | |
1087 | } | |
1088 | ||
6fc6879b JM |
1089 | if (msg == REQUEST || msg == PAIRWISE_2 || msg == PAIRWISE_4 || |
1090 | msg == GROUP_2) { | |
1091 | u16 ver = key_info & WPA_KEY_INFO_TYPE_MASK; | |
eb7719ff JM |
1092 | if (sm->pairwise == WPA_CIPHER_CCMP || |
1093 | sm->pairwise == WPA_CIPHER_GCMP) { | |
4bc801ab JM |
1094 | if (wpa_use_cmac(sm->wpa_key_mgmt) && |
1095 | !wpa_use_akm_defined(sm->wpa_key_mgmt) && | |
6fc6879b JM |
1096 | ver != WPA_KEY_INFO_TYPE_AES_128_CMAC) { |
1097 | wpa_auth_logger(wpa_auth, sm->addr, | |
1098 | LOGGER_WARNING, | |
1099 | "advertised support for " | |
1100 | "AES-128-CMAC, but did not " | |
1101 | "use it"); | |
1102 | return; | |
1103 | } | |
1104 | ||
4bc801ab JM |
1105 | if (!wpa_use_cmac(sm->wpa_key_mgmt) && |
1106 | !wpa_use_akm_defined(sm->wpa_key_mgmt) && | |
6fc6879b JM |
1107 | ver != WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) { |
1108 | wpa_auth_logger(wpa_auth, sm->addr, | |
1109 | LOGGER_WARNING, | |
1110 | "did not use HMAC-SHA1-AES " | |
eb7719ff | 1111 | "with CCMP/GCMP"); |
6fc6879b JM |
1112 | return; |
1113 | } | |
1114 | } | |
929a2ea5 | 1115 | |
4bc801ab | 1116 | if (wpa_use_akm_defined(sm->wpa_key_mgmt) && |
929a2ea5 JM |
1117 | ver != WPA_KEY_INFO_TYPE_AKM_DEFINED) { |
1118 | wpa_auth_logger(wpa_auth, sm->addr, LOGGER_WARNING, | |
1119 | "did not use EAPOL-Key descriptor version 0 as required for AKM-defined cases"); | |
1120 | return; | |
1121 | } | |
6fc6879b JM |
1122 | } |
1123 | ||
1124 | if (key_info & WPA_KEY_INFO_REQUEST) { | |
1125 | if (sm->req_replay_counter_used && | |
1126 | os_memcmp(key->replay_counter, sm->req_replay_counter, | |
1127 | WPA_REPLAY_COUNTER_LEN) <= 0) { | |
1128 | wpa_auth_logger(wpa_auth, sm->addr, LOGGER_WARNING, | |
1129 | "received EAPOL-Key request with " | |
1130 | "replayed counter"); | |
1131 | return; | |
1132 | } | |
1133 | } | |
1134 | ||
1135 | if (!(key_info & WPA_KEY_INFO_REQUEST) && | |
68921e24 | 1136 | !wpa_replay_counter_valid(sm->key_replay, key->replay_counter)) { |
22a299ee | 1137 | int i; |
68921e24 JM |
1138 | |
1139 | if (msg == PAIRWISE_2 && | |
1140 | wpa_replay_counter_valid(sm->prev_key_replay, | |
1141 | key->replay_counter) && | |
1142 | sm->wpa_ptk_state == WPA_PTK_PTKINITNEGOTIATING && | |
1143 | os_memcmp(sm->SNonce, key->key_nonce, WPA_NONCE_LEN) != 0) | |
1144 | { | |
1145 | /* | |
1146 | * Some supplicant implementations (e.g., Windows XP | |
1147 | * WZC) update SNonce for each EAPOL-Key 2/4. This | |
1148 | * breaks the workaround on accepting any of the | |
1149 | * pending requests, so allow the SNonce to be updated | |
1150 | * even if we have already sent out EAPOL-Key 3/4. | |
1151 | */ | |
1152 | wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_DEBUG, | |
1153 | "Process SNonce update from STA " | |
1154 | "based on retransmitted EAPOL-Key " | |
1155 | "1/4"); | |
1156 | sm->update_snonce = 1; | |
f23c5b17 JM |
1157 | os_memcpy(sm->alt_SNonce, sm->SNonce, WPA_NONCE_LEN); |
1158 | sm->alt_snonce_valid = TRUE; | |
1159 | os_memcpy(sm->alt_replay_counter, | |
1160 | sm->key_replay[0].counter, | |
1161 | WPA_REPLAY_COUNTER_LEN); | |
1162 | goto continue_processing; | |
1163 | } | |
1164 | ||
1165 | if (msg == PAIRWISE_4 && sm->alt_snonce_valid && | |
1166 | sm->wpa_ptk_state == WPA_PTK_PTKINITNEGOTIATING && | |
1167 | os_memcmp(key->replay_counter, sm->alt_replay_counter, | |
1168 | WPA_REPLAY_COUNTER_LEN) == 0) { | |
1169 | /* | |
1170 | * Supplicant may still be using the old SNonce since | |
1171 | * there was two EAPOL-Key 2/4 messages and they had | |
1172 | * different SNonce values. | |
1173 | */ | |
1174 | wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_DEBUG, | |
1175 | "Try to process received EAPOL-Key 4/4 based on old Replay Counter and SNonce from an earlier EAPOL-Key 1/4"); | |
68921e24 JM |
1176 | goto continue_processing; |
1177 | } | |
1178 | ||
1179 | if (msg == PAIRWISE_2 && | |
1180 | wpa_replay_counter_valid(sm->prev_key_replay, | |
1181 | key->replay_counter) && | |
1182 | sm->wpa_ptk_state == WPA_PTK_PTKINITNEGOTIATING) { | |
1183 | wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_DEBUG, | |
1184 | "ignore retransmitted EAPOL-Key %s - " | |
1185 | "SNonce did not change", msgtxt); | |
1186 | } else { | |
1187 | wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_DEBUG, | |
1188 | "received EAPOL-Key %s with " | |
1189 | "unexpected replay counter", msgtxt); | |
1190 | } | |
22a299ee JM |
1191 | for (i = 0; i < RSNA_MAX_EAPOL_RETRIES; i++) { |
1192 | if (!sm->key_replay[i].valid) | |
1193 | break; | |
1194 | wpa_hexdump(MSG_DEBUG, "pending replay counter", | |
1195 | sm->key_replay[i].counter, | |
1196 | WPA_REPLAY_COUNTER_LEN); | |
1197 | } | |
6fc6879b JM |
1198 | wpa_hexdump(MSG_DEBUG, "received replay counter", |
1199 | key->replay_counter, WPA_REPLAY_COUNTER_LEN); | |
1200 | return; | |
1201 | } | |
1202 | ||
68921e24 | 1203 | continue_processing: |
3b5b7aa8 JM |
1204 | #ifdef CONFIG_FILS |
1205 | if (sm->wpa == WPA_VERSION_WPA2 && mic_len == 0 && | |
1206 | !(key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) { | |
1207 | wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_DEBUG, | |
1208 | "WPA: Encr Key Data bit not set even though AEAD cipher is supposed to be used - drop frame"); | |
1209 | return; | |
1210 | } | |
1211 | #endif /* CONFIG_FILS */ | |
1212 | ||
6fc6879b JM |
1213 | switch (msg) { |
1214 | case PAIRWISE_2: | |
1215 | if (sm->wpa_ptk_state != WPA_PTK_PTKSTART && | |
68921e24 JM |
1216 | sm->wpa_ptk_state != WPA_PTK_PTKCALCNEGOTIATING && |
1217 | (!sm->update_snonce || | |
1218 | sm->wpa_ptk_state != WPA_PTK_PTKINITNEGOTIATING)) { | |
6fc6879b JM |
1219 | wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_INFO, |
1220 | "received EAPOL-Key msg 2/4 in " | |
1221 | "invalid state (%d) - dropped", | |
1222 | sm->wpa_ptk_state); | |
1223 | return; | |
1224 | } | |
bbb921da | 1225 | random_add_randomness(key->key_nonce, WPA_NONCE_LEN); |
08704cd8 JM |
1226 | if (sm->group->reject_4way_hs_for_entropy) { |
1227 | /* | |
1228 | * The system did not have enough entropy to generate | |
1229 | * strong random numbers. Reject the first 4-way | |
1230 | * handshake(s) and collect some entropy based on the | |
1231 | * information from it. Once enough entropy is | |
1232 | * available, the next atempt will trigger GMK/Key | |
1233 | * Counter update and the station will be allowed to | |
1234 | * continue. | |
1235 | */ | |
1236 | wpa_printf(MSG_DEBUG, "WPA: Reject 4-way handshake to " | |
1237 | "collect more entropy for random number " | |
1238 | "generation"); | |
08704cd8 | 1239 | random_mark_pool_ready(); |
567da5bb JM |
1240 | wpa_sta_disconnect(wpa_auth, sm->addr, |
1241 | WLAN_REASON_PREV_AUTH_NOT_VALID); | |
08704cd8 JM |
1242 | return; |
1243 | } | |
6fc6879b JM |
1244 | break; |
1245 | case PAIRWISE_4: | |
1246 | if (sm->wpa_ptk_state != WPA_PTK_PTKINITNEGOTIATING || | |
1247 | !sm->PTK_valid) { | |
1248 | wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_INFO, | |
1249 | "received EAPOL-Key msg 4/4 in " | |
1250 | "invalid state (%d) - dropped", | |
1251 | sm->wpa_ptk_state); | |
1252 | return; | |
1253 | } | |
1254 | break; | |
1255 | case GROUP_2: | |
1256 | if (sm->wpa_ptk_group_state != WPA_PTK_GROUP_REKEYNEGOTIATING | |
1257 | || !sm->PTK_valid) { | |
1258 | wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_INFO, | |
1259 | "received EAPOL-Key msg 2/2 in " | |
1260 | "invalid state (%d) - dropped", | |
1261 | sm->wpa_ptk_group_state); | |
1262 | return; | |
1263 | } | |
1264 | break; | |
6fc6879b JM |
1265 | case REQUEST: |
1266 | break; | |
1267 | } | |
1268 | ||
1269 | wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_DEBUG, | |
1270 | "received EAPOL-Key frame (%s)", msgtxt); | |
1271 | ||
1272 | if (key_info & WPA_KEY_INFO_ACK) { | |
1273 | wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO, | |
1274 | "received invalid EAPOL-Key: Key Ack set"); | |
1275 | return; | |
1276 | } | |
1277 | ||
b9866483 JM |
1278 | if (!wpa_key_mgmt_fils(sm->wpa_key_mgmt) && |
1279 | !(key_info & WPA_KEY_INFO_MIC)) { | |
6fc6879b JM |
1280 | wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO, |
1281 | "received invalid EAPOL-Key: Key MIC not set"); | |
1282 | return; | |
1283 | } | |
1284 | ||
b9866483 JM |
1285 | #ifdef CONFIG_FILS |
1286 | if (wpa_key_mgmt_fils(sm->wpa_key_mgmt) && | |
1287 | (key_info & WPA_KEY_INFO_MIC)) { | |
1288 | wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO, | |
1289 | "received invalid EAPOL-Key: Key MIC set"); | |
1290 | return; | |
1291 | } | |
1292 | #endif /* CONFIG_FILS */ | |
1293 | ||
6fc6879b | 1294 | sm->MICVerified = FALSE; |
68921e24 | 1295 | if (sm->PTK_valid && !sm->update_snonce) { |
3b5b7aa8 | 1296 | if (mic_len && |
567da5bb JM |
1297 | wpa_verify_key_mic(sm->wpa_key_mgmt, sm->pmk_len, &sm->PTK, |
1298 | data, data_len) && | |
f23c5b17 JM |
1299 | (msg != PAIRWISE_4 || !sm->alt_snonce_valid || |
1300 | wpa_try_alt_snonce(sm, data, data_len))) { | |
6fc6879b JM |
1301 | wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO, |
1302 | "received EAPOL-Key with invalid MIC"); | |
525923b1 JM |
1303 | #ifdef TEST_FUZZ |
1304 | wpa_printf(MSG_INFO, | |
1305 | "TEST: Ignore Key MIC failure for fuzz testing"); | |
1306 | goto continue_fuzz; | |
1307 | #endif /* TEST_FUZZ */ | |
6fc6879b JM |
1308 | return; |
1309 | } | |
75c8563e JM |
1310 | #ifdef CONFIG_FILS |
1311 | if (!mic_len && | |
1312 | wpa_aead_decrypt(sm, &sm->PTK, data, data_len, | |
1313 | &key_data_length) < 0) { | |
1314 | wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO, | |
1315 | "received EAPOL-Key with invalid MIC"); | |
525923b1 JM |
1316 | #ifdef TEST_FUZZ |
1317 | wpa_printf(MSG_INFO, | |
1318 | "TEST: Ignore Key MIC failure for fuzz testing"); | |
1319 | goto continue_fuzz; | |
1320 | #endif /* TEST_FUZZ */ | |
75c8563e JM |
1321 | return; |
1322 | } | |
1323 | #endif /* CONFIG_FILS */ | |
525923b1 JM |
1324 | #ifdef TEST_FUZZ |
1325 | continue_fuzz: | |
1326 | #endif /* TEST_FUZZ */ | |
6fc6879b JM |
1327 | sm->MICVerified = TRUE; |
1328 | eloop_cancel_timeout(wpa_send_eapol_timeout, wpa_auth, sm); | |
e4bf4db9 | 1329 | sm->pending_1_of_4_timeout = 0; |
6fc6879b JM |
1330 | } |
1331 | ||
1332 | if (key_info & WPA_KEY_INFO_REQUEST) { | |
1333 | if (sm->MICVerified) { | |
1334 | sm->req_replay_counter_used = 1; | |
1335 | os_memcpy(sm->req_replay_counter, key->replay_counter, | |
1336 | WPA_REPLAY_COUNTER_LEN); | |
1337 | } else { | |
1338 | wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO, | |
1339 | "received EAPOL-Key request with " | |
1340 | "invalid MIC"); | |
1341 | return; | |
1342 | } | |
1343 | ||
1344 | /* | |
1345 | * TODO: should decrypt key data field if encryption was used; | |
1346 | * even though MAC address KDE is not normally encrypted, | |
1347 | * supplicant is allowed to encrypt it. | |
1348 | */ | |
a0bf1b68 | 1349 | if (key_info & WPA_KEY_INFO_ERROR) { |
c772d054 JM |
1350 | if (wpa_receive_error_report( |
1351 | wpa_auth, sm, | |
1352 | !(key_info & WPA_KEY_INFO_KEY_TYPE)) > 0) | |
1353 | return; /* STA entry was removed */ | |
6fc6879b JM |
1354 | } else if (key_info & WPA_KEY_INFO_KEY_TYPE) { |
1355 | wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO, | |
1356 | "received EAPOL-Key Request for new " | |
1357 | "4-Way Handshake"); | |
1358 | wpa_request_new_ptk(sm); | |
6fc6879b | 1359 | } else if (key_data_length > 0 && |
5e3b5197 JM |
1360 | wpa_parse_kde_ies(key_data, key_data_length, |
1361 | &kde) == 0 && | |
6fc6879b JM |
1362 | kde.mac_addr) { |
1363 | } else { | |
1364 | wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO, | |
1365 | "received EAPOL-Key Request for GTK " | |
1366 | "rekeying"); | |
6fc6879b JM |
1367 | eloop_cancel_timeout(wpa_rekey_gtk, wpa_auth, NULL); |
1368 | wpa_rekey_gtk(wpa_auth, NULL); | |
1369 | } | |
1370 | } else { | |
68921e24 JM |
1371 | /* Do not allow the same key replay counter to be reused. */ |
1372 | wpa_replay_counter_mark_invalid(sm->key_replay, | |
1373 | key->replay_counter); | |
1374 | ||
1375 | if (msg == PAIRWISE_2) { | |
1376 | /* | |
1377 | * Maintain a copy of the pending EAPOL-Key frames in | |
1378 | * case the EAPOL-Key frame was retransmitted. This is | |
1379 | * needed to allow EAPOL-Key msg 2/4 reply to another | |
1380 | * pending msg 1/4 to update the SNonce to work around | |
1381 | * unexpected supplicant behavior. | |
1382 | */ | |
1383 | os_memcpy(sm->prev_key_replay, sm->key_replay, | |
1384 | sizeof(sm->key_replay)); | |
1385 | } else { | |
1386 | os_memset(sm->prev_key_replay, 0, | |
1387 | sizeof(sm->prev_key_replay)); | |
1388 | } | |
1389 | ||
1390 | /* | |
1391 | * Make sure old valid counters are not accepted anymore and | |
1392 | * do not get copied again. | |
1393 | */ | |
1394 | wpa_replay_counter_mark_invalid(sm->key_replay, NULL); | |
6fc6879b JM |
1395 | } |
1396 | ||
6fc6879b | 1397 | os_free(sm->last_rx_eapol_key); |
a1f11e34 | 1398 | sm->last_rx_eapol_key = os_memdup(data, data_len); |
6fc6879b JM |
1399 | if (sm->last_rx_eapol_key == NULL) |
1400 | return; | |
6fc6879b JM |
1401 | sm->last_rx_eapol_key_len = data_len; |
1402 | ||
0d442aff | 1403 | sm->rx_eapol_key_secure = !!(key_info & WPA_KEY_INFO_SECURE); |
6fc6879b JM |
1404 | sm->EAPOLKeyReceived = TRUE; |
1405 | sm->EAPOLKeyPairwise = !!(key_info & WPA_KEY_INFO_KEY_TYPE); | |
1406 | sm->EAPOLKeyRequest = !!(key_info & WPA_KEY_INFO_REQUEST); | |
1407 | os_memcpy(sm->SNonce, key->key_nonce, WPA_NONCE_LEN); | |
1408 | wpa_sm_step(sm); | |
1409 | } | |
1410 | ||
1411 | ||
3c7302c2 JM |
1412 | static int wpa_gmk_to_gtk(const u8 *gmk, const char *label, const u8 *addr, |
1413 | const u8 *gnonce, u8 *gtk, size_t gtk_len) | |
6fc6879b | 1414 | { |
c6eb3e34 | 1415 | u8 data[ETH_ALEN + WPA_NONCE_LEN + 8 + WPA_GTK_MAX_LEN]; |
5cb9d5c3 | 1416 | u8 *pos; |
3c7302c2 | 1417 | int ret = 0; |
6fc6879b | 1418 | |
5cb9d5c3 JM |
1419 | /* GTK = PRF-X(GMK, "Group key expansion", |
1420 | * AA || GNonce || Time || random data) | |
1421 | * The example described in the IEEE 802.11 standard uses only AA and | |
1422 | * GNonce as inputs here. Add some more entropy since this derivation | |
1423 | * is done only at the Authenticator and as such, does not need to be | |
1424 | * exactly same. | |
1425 | */ | |
c6eb3e34 | 1426 | os_memset(data, 0, sizeof(data)); |
6fc6879b JM |
1427 | os_memcpy(data, addr, ETH_ALEN); |
1428 | os_memcpy(data + ETH_ALEN, gnonce, WPA_NONCE_LEN); | |
5cb9d5c3 JM |
1429 | pos = data + ETH_ALEN + WPA_NONCE_LEN; |
1430 | wpa_get_ntp_timestamp(pos); | |
525923b1 JM |
1431 | #ifdef TEST_FUZZ |
1432 | os_memset(pos, 0xef, 8); | |
1433 | #endif /* TEST_FUZZ */ | |
5cb9d5c3 | 1434 | pos += 8; |
c6eb3e34 | 1435 | if (random_get_bytes(pos, gtk_len) < 0) |
3c7302c2 | 1436 | ret = -1; |
6fc6879b | 1437 | |
c6eb3e34 JM |
1438 | #ifdef CONFIG_SHA384 |
1439 | if (sha384_prf(gmk, WPA_GMK_LEN, label, data, sizeof(data), | |
1440 | gtk, gtk_len) < 0) | |
3c7302c2 | 1441 | ret = -1; |
c6eb3e34 JM |
1442 | #else /* CONFIG_SHA384 */ |
1443 | #ifdef CONFIG_SHA256 | |
1444 | if (sha256_prf(gmk, WPA_GMK_LEN, label, data, sizeof(data), | |
1445 | gtk, gtk_len) < 0) | |
1446 | ret = -1; | |
1447 | #else /* CONFIG_SHA256 */ | |
1448 | if (sha1_prf(gmk, WPA_GMK_LEN, label, data, sizeof(data), | |
1449 | gtk, gtk_len) < 0) | |
1450 | ret = -1; | |
1451 | #endif /* CONFIG_SHA256 */ | |
1452 | #endif /* CONFIG_SHA384 */ | |
3c7302c2 | 1453 | |
31bc66e4 JM |
1454 | forced_memzero(data, sizeof(data)); |
1455 | ||
3c7302c2 | 1456 | return ret; |
6fc6879b JM |
1457 | } |
1458 | ||
1459 | ||
1460 | static void wpa_send_eapol_timeout(void *eloop_ctx, void *timeout_ctx) | |
1461 | { | |
1462 | struct wpa_authenticator *wpa_auth = eloop_ctx; | |
1463 | struct wpa_state_machine *sm = timeout_ctx; | |
1464 | ||
e4bf4db9 | 1465 | sm->pending_1_of_4_timeout = 0; |
6fc6879b JM |
1466 | wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG, "EAPOL-Key timeout"); |
1467 | sm->TimeoutEvt = TRUE; | |
1468 | wpa_sm_step(sm); | |
1469 | } | |
1470 | ||
1471 | ||
1472 | void __wpa_send_eapol(struct wpa_authenticator *wpa_auth, | |
1473 | struct wpa_state_machine *sm, int key_info, | |
1474 | const u8 *key_rsc, const u8 *nonce, | |
1475 | const u8 *kde, size_t kde_len, | |
1476 | int keyidx, int encr, int force_version) | |
1477 | { | |
62e12afc | 1478 | struct wpa_auth_config *conf = &wpa_auth->conf; |
6fc6879b JM |
1479 | struct ieee802_1x_hdr *hdr; |
1480 | struct wpa_eapol_key *key; | |
5e3b5197 | 1481 | size_t len, mic_len, keyhdrlen; |
6fc6879b JM |
1482 | int alg; |
1483 | int key_data_len, pad_len = 0; | |
1484 | u8 *buf, *pos; | |
1485 | int version, pairwise; | |
22a299ee | 1486 | int i; |
6d014ffc | 1487 | u8 *key_mic, *key_data; |
6fc6879b | 1488 | |
567da5bb | 1489 | mic_len = wpa_mic_len(sm->wpa_key_mgmt, sm->pmk_len); |
6d014ffc | 1490 | keyhdrlen = sizeof(*key) + mic_len + 2; |
5e3b5197 JM |
1491 | |
1492 | len = sizeof(struct ieee802_1x_hdr) + keyhdrlen; | |
6fc6879b JM |
1493 | |
1494 | if (force_version) | |
1495 | version = force_version; | |
4bc801ab | 1496 | else if (wpa_use_akm_defined(sm->wpa_key_mgmt)) |
a14896e8 | 1497 | version = WPA_KEY_INFO_TYPE_AKM_DEFINED; |
4bc801ab | 1498 | else if (wpa_use_cmac(sm->wpa_key_mgmt)) |
6fc6879b | 1499 | version = WPA_KEY_INFO_TYPE_AES_128_CMAC; |
eb7719ff | 1500 | else if (sm->pairwise != WPA_CIPHER_TKIP) |
6fc6879b JM |
1501 | version = WPA_KEY_INFO_TYPE_HMAC_SHA1_AES; |
1502 | else | |
1503 | version = WPA_KEY_INFO_TYPE_HMAC_MD5_RC4; | |
1504 | ||
8543ed8a | 1505 | pairwise = !!(key_info & WPA_KEY_INFO_KEY_TYPE); |
6fc6879b JM |
1506 | |
1507 | wpa_printf(MSG_DEBUG, "WPA: Send EAPOL(version=%d secure=%d mic=%d " | |
1508 | "ack=%d install=%d pairwise=%d kde_len=%lu keyidx=%d " | |
1509 | "encr=%d)", | |
1510 | version, | |
1511 | (key_info & WPA_KEY_INFO_SECURE) ? 1 : 0, | |
1512 | (key_info & WPA_KEY_INFO_MIC) ? 1 : 0, | |
1513 | (key_info & WPA_KEY_INFO_ACK) ? 1 : 0, | |
1514 | (key_info & WPA_KEY_INFO_INSTALL) ? 1 : 0, | |
1515 | pairwise, (unsigned long) kde_len, keyidx, encr); | |
1516 | ||
1517 | key_data_len = kde_len; | |
1518 | ||
1519 | if ((version == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES || | |
4bc801ab | 1520 | wpa_use_aes_key_wrap(sm->wpa_key_mgmt) || |
6fc6879b JM |
1521 | version == WPA_KEY_INFO_TYPE_AES_128_CMAC) && encr) { |
1522 | pad_len = key_data_len % 8; | |
1523 | if (pad_len) | |
1524 | pad_len = 8 - pad_len; | |
1525 | key_data_len += pad_len + 8; | |
1526 | } | |
1527 | ||
1528 | len += key_data_len; | |
b729fd8d JM |
1529 | if (!mic_len && encr) |
1530 | len += AES_BLOCK_SIZE; | |
6fc6879b JM |
1531 | |
1532 | hdr = os_zalloc(len); | |
1533 | if (hdr == NULL) | |
1534 | return; | |
62e12afc | 1535 | hdr->version = conf->eapol_version; |
6fc6879b JM |
1536 | hdr->type = IEEE802_1X_TYPE_EAPOL_KEY; |
1537 | hdr->length = host_to_be16(len - sizeof(*hdr)); | |
1538 | key = (struct wpa_eapol_key *) (hdr + 1); | |
6d014ffc | 1539 | key_mic = (u8 *) (key + 1); |
5e3b5197 | 1540 | key_data = ((u8 *) (hdr + 1)) + keyhdrlen; |
6fc6879b JM |
1541 | |
1542 | key->type = sm->wpa == WPA_VERSION_WPA2 ? | |
1543 | EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA; | |
1544 | key_info |= version; | |
1545 | if (encr && sm->wpa == WPA_VERSION_WPA2) | |
1546 | key_info |= WPA_KEY_INFO_ENCR_KEY_DATA; | |
1547 | if (sm->wpa != WPA_VERSION_WPA2) | |
1548 | key_info |= keyidx << WPA_KEY_INFO_KEY_INDEX_SHIFT; | |
1549 | WPA_PUT_BE16(key->key_info, key_info); | |
1550 | ||
62e12afc | 1551 | alg = pairwise ? sm->pairwise : conf->wpa_group; |
a0bf1b68 | 1552 | if (sm->wpa == WPA_VERSION_WPA2 && !pairwise) |
6fc6879b | 1553 | WPA_PUT_BE16(key->key_length, 0); |
caab23f1 JM |
1554 | else |
1555 | WPA_PUT_BE16(key->key_length, wpa_cipher_key_len(alg)); | |
6fc6879b | 1556 | |
22a299ee JM |
1557 | for (i = RSNA_MAX_EAPOL_RETRIES - 1; i > 0; i--) { |
1558 | sm->key_replay[i].valid = sm->key_replay[i - 1].valid; | |
1559 | os_memcpy(sm->key_replay[i].counter, | |
1560 | sm->key_replay[i - 1].counter, | |
1561 | WPA_REPLAY_COUNTER_LEN); | |
1562 | } | |
1563 | inc_byte_array(sm->key_replay[0].counter, WPA_REPLAY_COUNTER_LEN); | |
1564 | os_memcpy(key->replay_counter, sm->key_replay[0].counter, | |
6fc6879b | 1565 | WPA_REPLAY_COUNTER_LEN); |
db099951 JM |
1566 | wpa_hexdump(MSG_DEBUG, "WPA: Replay Counter", |
1567 | key->replay_counter, WPA_REPLAY_COUNTER_LEN); | |
22a299ee | 1568 | sm->key_replay[0].valid = TRUE; |
6fc6879b JM |
1569 | |
1570 | if (nonce) | |
1571 | os_memcpy(key->key_nonce, nonce, WPA_NONCE_LEN); | |
1572 | ||
1573 | if (key_rsc) | |
1574 | os_memcpy(key->key_rsc, key_rsc, WPA_KEY_RSC_LEN); | |
1575 | ||
1576 | if (kde && !encr) { | |
5e3b5197 | 1577 | os_memcpy(key_data, kde, kde_len); |
6d014ffc | 1578 | WPA_PUT_BE16(key_mic + mic_len, kde_len); |
b729fd8d | 1579 | #ifdef CONFIG_FILS |
c05f96a3 | 1580 | } else if (!mic_len && kde) { |
b729fd8d JM |
1581 | const u8 *aad[1]; |
1582 | size_t aad_len[1]; | |
1583 | ||
1584 | WPA_PUT_BE16(key_mic, AES_BLOCK_SIZE + kde_len); | |
1585 | wpa_hexdump_key(MSG_DEBUG, "Plaintext EAPOL-Key Key Data", | |
1586 | kde, kde_len); | |
1587 | ||
1588 | wpa_hexdump_key(MSG_DEBUG, "WPA: KEK", | |
1589 | sm->PTK.kek, sm->PTK.kek_len); | |
1590 | /* AES-SIV AAD from EAPOL protocol version field (inclusive) to | |
1591 | * to Key Data (exclusive). */ | |
1592 | aad[0] = (u8 *) hdr; | |
1593 | aad_len[0] = key_mic + 2 - (u8 *) hdr; | |
1594 | if (aes_siv_encrypt(sm->PTK.kek, sm->PTK.kek_len, kde, kde_len, | |
1595 | 1, aad, aad_len, key_mic + 2) < 0) { | |
1596 | wpa_printf(MSG_DEBUG, "WPA: AES-SIV encryption failed"); | |
1597 | return; | |
1598 | } | |
1599 | ||
1600 | wpa_hexdump(MSG_DEBUG, "WPA: Encrypted Key Data from SIV", | |
1601 | key_mic + 2, AES_BLOCK_SIZE + kde_len); | |
1602 | #endif /* CONFIG_FILS */ | |
6fc6879b JM |
1603 | } else if (encr && kde) { |
1604 | buf = os_zalloc(key_data_len); | |
1605 | if (buf == NULL) { | |
1606 | os_free(hdr); | |
1607 | return; | |
1608 | } | |
1609 | pos = buf; | |
1610 | os_memcpy(pos, kde, kde_len); | |
1611 | pos += kde_len; | |
1612 | ||
1613 | if (pad_len) | |
1614 | *pos++ = 0xdd; | |
1615 | ||
1616 | wpa_hexdump_key(MSG_DEBUG, "Plaintext EAPOL-Key Key Data", | |
1617 | buf, key_data_len); | |
1618 | if (version == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES || | |
4bc801ab | 1619 | wpa_use_aes_key_wrap(sm->wpa_key_mgmt) || |
6fc6879b | 1620 | version == WPA_KEY_INFO_TYPE_AES_128_CMAC) { |
ef9627cb JM |
1621 | wpa_printf(MSG_DEBUG, |
1622 | "WPA: Encrypt Key Data using AES-WRAP (KEK length %u)", | |
1623 | (unsigned int) sm->PTK.kek_len); | |
98cd3d1c | 1624 | if (aes_wrap(sm->PTK.kek, sm->PTK.kek_len, |
5e3b5197 | 1625 | (key_data_len - 8) / 8, buf, key_data)) { |
6fc6879b JM |
1626 | os_free(hdr); |
1627 | os_free(buf); | |
1628 | return; | |
1629 | } | |
6d014ffc | 1630 | WPA_PUT_BE16(key_mic + mic_len, key_data_len); |
7cb53ded | 1631 | #ifndef CONFIG_NO_RC4 |
98cd3d1c | 1632 | } else if (sm->PTK.kek_len == 16) { |
6fc6879b | 1633 | u8 ek[32]; |
ef9627cb JM |
1634 | |
1635 | wpa_printf(MSG_DEBUG, | |
1636 | "WPA: Encrypt Key Data using RC4"); | |
6fc6879b JM |
1637 | os_memcpy(key->key_iv, |
1638 | sm->group->Counter + WPA_NONCE_LEN - 16, 16); | |
1639 | inc_byte_array(sm->group->Counter, WPA_NONCE_LEN); | |
1640 | os_memcpy(ek, key->key_iv, 16); | |
98cd3d1c | 1641 | os_memcpy(ek + 16, sm->PTK.kek, sm->PTK.kek_len); |
5e3b5197 JM |
1642 | os_memcpy(key_data, buf, key_data_len); |
1643 | rc4_skip(ek, 32, 256, key_data, key_data_len); | |
6d014ffc | 1644 | WPA_PUT_BE16(key_mic + mic_len, key_data_len); |
7cb53ded | 1645 | #endif /* CONFIG_NO_RC4 */ |
98cd3d1c JM |
1646 | } else { |
1647 | os_free(hdr); | |
1648 | os_free(buf); | |
1649 | return; | |
6fc6879b JM |
1650 | } |
1651 | os_free(buf); | |
1652 | } | |
1653 | ||
1654 | if (key_info & WPA_KEY_INFO_MIC) { | |
b729fd8d | 1655 | if (!sm->PTK_valid || !mic_len) { |
6fc6879b JM |
1656 | wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG, |
1657 | "PTK not valid when sending EAPOL-Key " | |
1658 | "frame"); | |
1659 | os_free(hdr); | |
1660 | return; | |
1661 | } | |
5e3b5197 | 1662 | |
beae255a JM |
1663 | if (wpa_eapol_key_mic(sm->PTK.kck, sm->PTK.kck_len, |
1664 | sm->wpa_key_mgmt, version, | |
1665 | (u8 *) hdr, len, key_mic) < 0) { | |
1666 | os_free(hdr); | |
1667 | return; | |
1668 | } | |
7af092a0 JB |
1669 | #ifdef CONFIG_TESTING_OPTIONS |
1670 | if (!pairwise && | |
62e12afc JM |
1671 | conf->corrupt_gtk_rekey_mic_probability > 0.0 && |
1672 | drand48() < conf->corrupt_gtk_rekey_mic_probability) { | |
7af092a0 JB |
1673 | wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO, |
1674 | "Corrupting group EAPOL-Key Key MIC"); | |
5e3b5197 | 1675 | key_mic[0]++; |
7af092a0 JB |
1676 | } |
1677 | #endif /* CONFIG_TESTING_OPTIONS */ | |
6fc6879b JM |
1678 | } |
1679 | ||
62e12afc | 1680 | wpa_auth_set_eapol(wpa_auth, sm->addr, WPA_EAPOL_inc_EapolFramesTx, 1); |
6fc6879b JM |
1681 | wpa_auth_send_eapol(wpa_auth, sm->addr, (u8 *) hdr, len, |
1682 | sm->pairwise_set); | |
1683 | os_free(hdr); | |
1684 | } | |
1685 | ||
1686 | ||
1687 | static void wpa_send_eapol(struct wpa_authenticator *wpa_auth, | |
1688 | struct wpa_state_machine *sm, int key_info, | |
1689 | const u8 *key_rsc, const u8 *nonce, | |
1690 | const u8 *kde, size_t kde_len, | |
1691 | int keyidx, int encr) | |
1692 | { | |
1693 | int timeout_ms; | |
1694 | int pairwise = key_info & WPA_KEY_INFO_KEY_TYPE; | |
41f140d3 | 1695 | u32 ctr; |
6fc6879b JM |
1696 | |
1697 | if (sm == NULL) | |
1698 | return; | |
1699 | ||
1700 | __wpa_send_eapol(wpa_auth, sm, key_info, key_rsc, nonce, kde, kde_len, | |
1701 | keyidx, encr, 0); | |
1702 | ||
bae61562 | 1703 | ctr = pairwise ? sm->TimeoutCtr : sm->GTimeoutCtr; |
e4bf4db9 | 1704 | if (ctr == 1 && wpa_auth->conf.tx_status) |
00338a4f JM |
1705 | timeout_ms = pairwise ? eapol_key_timeout_first : |
1706 | eapol_key_timeout_first_group; | |
bae61562 JM |
1707 | else |
1708 | timeout_ms = eapol_key_timeout_subseq; | |
6f234c1e JM |
1709 | if (wpa_auth->conf.wpa_disable_eapol_key_retries && |
1710 | (!pairwise || (key_info & WPA_KEY_INFO_MIC))) | |
1711 | timeout_ms = eapol_key_timeout_no_retrans; | |
e4bf4db9 JM |
1712 | if (pairwise && ctr == 1 && !(key_info & WPA_KEY_INFO_MIC)) |
1713 | sm->pending_1_of_4_timeout = 1; | |
525923b1 JM |
1714 | #ifdef TEST_FUZZ |
1715 | timeout_ms = 1; | |
1716 | #endif /* TEST_FUZZ */ | |
e4bf4db9 | 1717 | wpa_printf(MSG_DEBUG, "WPA: Use EAPOL-Key timeout of %u ms (retry " |
41f140d3 | 1718 | "counter %u)", timeout_ms, ctr); |
6fc6879b JM |
1719 | eloop_register_timeout(timeout_ms / 1000, (timeout_ms % 1000) * 1000, |
1720 | wpa_send_eapol_timeout, wpa_auth, sm); | |
1721 | } | |
1722 | ||
1723 | ||
567da5bb JM |
1724 | static int wpa_verify_key_mic(int akmp, size_t pmk_len, struct wpa_ptk *PTK, |
1725 | u8 *data, size_t data_len) | |
6fc6879b JM |
1726 | { |
1727 | struct ieee802_1x_hdr *hdr; | |
1728 | struct wpa_eapol_key *key; | |
1729 | u16 key_info; | |
1730 | int ret = 0; | |
6d014ffc | 1731 | u8 mic[WPA_EAPOL_KEY_MIC_MAX_LEN], *mic_pos; |
567da5bb | 1732 | size_t mic_len = wpa_mic_len(akmp, pmk_len); |
6fc6879b JM |
1733 | |
1734 | if (data_len < sizeof(*hdr) + sizeof(*key)) | |
1735 | return -1; | |
1736 | ||
1737 | hdr = (struct ieee802_1x_hdr *) data; | |
1738 | key = (struct wpa_eapol_key *) (hdr + 1); | |
6d014ffc | 1739 | mic_pos = (u8 *) (key + 1); |
6fc6879b | 1740 | key_info = WPA_GET_BE16(key->key_info); |
6d014ffc JM |
1741 | os_memcpy(mic, mic_pos, mic_len); |
1742 | os_memset(mic_pos, 0, mic_len); | |
98cd3d1c JM |
1743 | if (wpa_eapol_key_mic(PTK->kck, PTK->kck_len, akmp, |
1744 | key_info & WPA_KEY_INFO_TYPE_MASK, | |
6d014ffc JM |
1745 | data, data_len, mic_pos) || |
1746 | os_memcmp_const(mic, mic_pos, mic_len) != 0) | |
6fc6879b | 1747 | ret = -1; |
6d014ffc | 1748 | os_memcpy(mic_pos, mic, mic_len); |
6fc6879b JM |
1749 | return ret; |
1750 | } | |
1751 | ||
1752 | ||
1753 | void wpa_remove_ptk(struct wpa_state_machine *sm) | |
1754 | { | |
1755 | sm->PTK_valid = FALSE; | |
1756 | os_memset(&sm->PTK, 0, sizeof(sm->PTK)); | |
2a0b86d3 | 1757 | if (wpa_auth_set_key(sm->wpa_auth, 0, WPA_ALG_NONE, sm->addr, 0, NULL, |
a919a260 | 1758 | 0, KEY_FLAG_PAIRWISE)) |
2a0b86d3 JM |
1759 | wpa_printf(MSG_DEBUG, |
1760 | "RSN: PTK removal from the driver failed"); | |
862aac1f AW |
1761 | if (sm->wpa_auth->conf.extended_key_id && sm->use_ext_key_id && |
1762 | wpa_auth_set_key(sm->wpa_auth, 0, WPA_ALG_NONE, sm->addr, 1, NULL, | |
1763 | 0, KEY_FLAG_PAIRWISE)) | |
1764 | wpa_printf(MSG_DEBUG, | |
1765 | "RSN: PTK Key ID 1 removal from the driver failed"); | |
6fc6879b | 1766 | sm->pairwise_set = FALSE; |
581a8cde | 1767 | eloop_cancel_timeout(wpa_rekey_ptk, sm->wpa_auth, sm); |
6fc6879b JM |
1768 | } |
1769 | ||
1770 | ||
bb119228 | 1771 | int wpa_auth_sm_event(struct wpa_state_machine *sm, enum wpa_event event) |
6fc6879b | 1772 | { |
5d22a1d5 JM |
1773 | int remove_ptk = 1; |
1774 | ||
6fc6879b | 1775 | if (sm == NULL) |
6f9b5d16 | 1776 | return -1; |
6fc6879b JM |
1777 | |
1778 | wpa_auth_vlogger(sm->wpa_auth, sm->addr, LOGGER_DEBUG, | |
1779 | "event %d notification", event); | |
1780 | ||
1781 | switch (event) { | |
1782 | case WPA_AUTH: | |
c50d94f1 BC |
1783 | #ifdef CONFIG_MESH |
1784 | /* PTKs are derived through AMPE */ | |
1785 | if (wpa_auth_start_ampe(sm->wpa_auth, sm->addr)) { | |
1786 | /* not mesh */ | |
1787 | break; | |
1788 | } | |
1789 | return 0; | |
1790 | #endif /* CONFIG_MESH */ | |
6fc6879b JM |
1791 | case WPA_ASSOC: |
1792 | break; | |
1793 | case WPA_DEAUTH: | |
1794 | case WPA_DISASSOC: | |
1795 | sm->DeauthenticationRequest = TRUE; | |
09ab81b9 JM |
1796 | #ifdef CONFIG_IEEE80211R_AP |
1797 | os_memset(sm->PMK, 0, sizeof(sm->PMK)); | |
1798 | sm->pmk_len = 0; | |
1799 | os_memset(sm->xxkey, 0, sizeof(sm->xxkey)); | |
1800 | sm->xxkey_len = 0; | |
8a576f5e JM |
1801 | os_memset(sm->pmk_r1, 0, sizeof(sm->pmk_r1)); |
1802 | sm->pmk_r1_len = 0; | |
09ab81b9 | 1803 | #endif /* CONFIG_IEEE80211R_AP */ |
6fc6879b JM |
1804 | break; |
1805 | case WPA_REAUTH: | |
1806 | case WPA_REAUTH_EAPOL: | |
3ab72b62 JM |
1807 | if (!sm->started) { |
1808 | /* | |
1809 | * When using WPS, we may end up here if the STA | |
1810 | * manages to re-associate without the previous STA | |
1811 | * entry getting removed. Consequently, we need to make | |
1812 | * sure that the WPA state machines gets initialized | |
1813 | * properly at this point. | |
1814 | */ | |
1815 | wpa_printf(MSG_DEBUG, "WPA state machine had not been " | |
1816 | "started - initialize now"); | |
1817 | sm->started = 1; | |
1818 | sm->Init = TRUE; | |
1819 | if (wpa_sm_step(sm) == 1) | |
1820 | return 1; /* should not really happen */ | |
1821 | sm->Init = FALSE; | |
1822 | sm->AuthenticationRequest = TRUE; | |
1823 | break; | |
862aac1f AW |
1824 | } |
1825 | ||
1826 | if (!sm->use_ext_key_id && | |
1827 | sm->wpa_auth->conf.wpa_deny_ptk0_rekey) { | |
1a7963e3 AW |
1828 | wpa_printf(MSG_INFO, |
1829 | "WPA: PTK0 rekey not allowed, disconnect " | |
1830 | MACSTR, MAC2STR(sm->addr)); | |
1831 | sm->Disconnect = TRUE; | |
862aac1f | 1832 | /* Try to encourage the STA to reconnect */ |
1a7963e3 AW |
1833 | sm->disconnect_reason = |
1834 | WLAN_REASON_CLASS3_FRAME_FROM_NONASSOC_STA; | |
1835 | break; | |
3ab72b62 | 1836 | } |
862aac1f AW |
1837 | |
1838 | if (sm->use_ext_key_id) | |
1839 | sm->keyidx_active ^= 1; /* flip Key ID */ | |
1840 | ||
9663596f JM |
1841 | if (sm->GUpdateStationKeys) { |
1842 | /* | |
1843 | * Reauthentication cancels the pending group key | |
1844 | * update for this STA. | |
1845 | */ | |
1846 | sm->group->GKeyDoneStations--; | |
1847 | sm->GUpdateStationKeys = FALSE; | |
1848 | sm->PtkGroupInit = TRUE; | |
1849 | } | |
6fc6879b JM |
1850 | sm->ReAuthenticationRequest = TRUE; |
1851 | break; | |
1852 | case WPA_ASSOC_FT: | |
4ec1fd8e | 1853 | #ifdef CONFIG_IEEE80211R_AP |
81a658d7 JM |
1854 | wpa_printf(MSG_DEBUG, "FT: Retry PTK configuration " |
1855 | "after association"); | |
1856 | wpa_ft_install_ptk(sm); | |
1857 | ||
6fc6879b JM |
1858 | /* Using FT protocol, not WPA auth state machine */ |
1859 | sm->ft_completed = 1; | |
d178ab0d | 1860 | wpa_auth_set_ptk_rekey_timer(sm); |
6f9b5d16 | 1861 | return 0; |
4ec1fd8e | 1862 | #else /* CONFIG_IEEE80211R_AP */ |
6fc6879b | 1863 | break; |
4ec1fd8e | 1864 | #endif /* CONFIG_IEEE80211R_AP */ |
957bff83 JM |
1865 | case WPA_ASSOC_FILS: |
1866 | #ifdef CONFIG_FILS | |
1867 | wpa_printf(MSG_DEBUG, | |
1868 | "FILS: TK configuration after association"); | |
1869 | fils_set_tk(sm); | |
1870 | sm->fils_completed = 1; | |
1871 | return 0; | |
1872 | #else /* CONFIG_FILS */ | |
1873 | break; | |
1874 | #endif /* CONFIG_FILS */ | |
0e3bd7ac MV |
1875 | case WPA_DRV_STA_REMOVED: |
1876 | sm->tk_already_set = FALSE; | |
1877 | return 0; | |
6fc6879b JM |
1878 | } |
1879 | ||
4ec1fd8e | 1880 | #ifdef CONFIG_IEEE80211R_AP |
6fc6879b | 1881 | sm->ft_completed = 0; |
4ec1fd8e | 1882 | #endif /* CONFIG_IEEE80211R_AP */ |
6fc6879b | 1883 | |
5d22a1d5 JM |
1884 | if (sm->mgmt_frame_prot && event == WPA_AUTH) |
1885 | remove_ptk = 0; | |
78815f3d | 1886 | #ifdef CONFIG_FILS |
da24c5aa JM |
1887 | if (wpa_key_mgmt_fils(sm->wpa_key_mgmt) && |
1888 | (event == WPA_AUTH || event == WPA_ASSOC)) | |
78815f3d JM |
1889 | remove_ptk = 0; |
1890 | #endif /* CONFIG_FILS */ | |
6fc6879b | 1891 | |
5d22a1d5 JM |
1892 | if (remove_ptk) { |
1893 | sm->PTK_valid = FALSE; | |
1894 | os_memset(&sm->PTK, 0, sizeof(sm->PTK)); | |
1895 | ||
1896 | if (event != WPA_REAUTH_EAPOL) | |
1897 | wpa_remove_ptk(sm); | |
1898 | } | |
6fc6879b | 1899 | |
43f49c37 JM |
1900 | if (sm->in_step_loop) { |
1901 | /* | |
1902 | * wpa_sm_step() is already running - avoid recursive call to | |
1903 | * it by making the existing loop process the new update. | |
1904 | */ | |
1905 | sm->changed = TRUE; | |
1906 | return 0; | |
1907 | } | |
6f9b5d16 | 1908 | return wpa_sm_step(sm); |
6fc6879b JM |
1909 | } |
1910 | ||
1911 | ||
6fc6879b JM |
1912 | SM_STATE(WPA_PTK, INITIALIZE) |
1913 | { | |
1914 | SM_ENTRY_MA(WPA_PTK, INITIALIZE, wpa_ptk); | |
1915 | if (sm->Init) { | |
1916 | /* Init flag is not cleared here, so avoid busy | |
1917 | * loop by claiming nothing changed. */ | |
1918 | sm->changed = FALSE; | |
1919 | } | |
1920 | ||
1921 | sm->keycount = 0; | |
1922 | if (sm->GUpdateStationKeys) | |
1923 | sm->group->GKeyDoneStations--; | |
1924 | sm->GUpdateStationKeys = FALSE; | |
1925 | if (sm->wpa == WPA_VERSION_WPA) | |
1926 | sm->PInitAKeys = FALSE; | |
1927 | if (1 /* Unicast cipher supported AND (ESS OR ((IBSS or WDS) and | |
1928 | * Local AA > Remote AA)) */) { | |
1929 | sm->Pair = TRUE; | |
1930 | } | |
1931 | wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portEnabled, 0); | |
1932 | wpa_remove_ptk(sm); | |
1933 | wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portValid, 0); | |
1934 | sm->TimeoutCtr = 0; | |
a1ea1b45 | 1935 | if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt) || |
567da5bb | 1936 | sm->wpa_key_mgmt == WPA_KEY_MGMT_DPP || |
a1ea1b45 | 1937 | sm->wpa_key_mgmt == WPA_KEY_MGMT_OWE) { |
6fc6879b JM |
1938 | wpa_auth_set_eapol(sm->wpa_auth, sm->addr, |
1939 | WPA_EAPOL_authorized, 0); | |
1940 | } | |
1941 | } | |
1942 | ||
1943 | ||
1944 | SM_STATE(WPA_PTK, DISCONNECT) | |
1945 | { | |
567da5bb JM |
1946 | u16 reason = sm->disconnect_reason; |
1947 | ||
6fc6879b JM |
1948 | SM_ENTRY_MA(WPA_PTK, DISCONNECT, wpa_ptk); |
1949 | sm->Disconnect = FALSE; | |
567da5bb JM |
1950 | sm->disconnect_reason = 0; |
1951 | if (!reason) | |
1952 | reason = WLAN_REASON_PREV_AUTH_NOT_VALID; | |
1953 | wpa_sta_disconnect(sm->wpa_auth, sm->addr, reason); | |
6fc6879b JM |
1954 | } |
1955 | ||
1956 | ||
1957 | SM_STATE(WPA_PTK, DISCONNECTED) | |
1958 | { | |
1959 | SM_ENTRY_MA(WPA_PTK, DISCONNECTED, wpa_ptk); | |
1960 | sm->DeauthenticationRequest = FALSE; | |
1961 | } | |
1962 | ||
1963 | ||
1964 | SM_STATE(WPA_PTK, AUTHENTICATION) | |
1965 | { | |
1966 | SM_ENTRY_MA(WPA_PTK, AUTHENTICATION, wpa_ptk); | |
1967 | os_memset(&sm->PTK, 0, sizeof(sm->PTK)); | |
1968 | sm->PTK_valid = FALSE; | |
1969 | wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portControl_Auto, | |
1970 | 1); | |
1971 | wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portEnabled, 1); | |
1972 | sm->AuthenticationRequest = FALSE; | |
1973 | } | |
1974 | ||
1975 | ||
40d00d2b NC |
1976 | static void wpa_group_ensure_init(struct wpa_authenticator *wpa_auth, |
1977 | struct wpa_group *group) | |
1bdb7ab3 | 1978 | { |
40d00d2b NC |
1979 | if (group->first_sta_seen) |
1980 | return; | |
1bdb7ab3 JM |
1981 | /* |
1982 | * System has run bit further than at the time hostapd was started | |
1983 | * potentially very early during boot up. This provides better chances | |
1984 | * of collecting more randomness on embedded systems. Re-initialize the | |
1985 | * GMK and Counter here to improve their strength if there was not | |
1986 | * enough entropy available immediately after system startup. | |
1987 | */ | |
1988 | wpa_printf(MSG_DEBUG, "WPA: Re-initialize GMK/Counter on first " | |
1989 | "station"); | |
08704cd8 JM |
1990 | if (random_pool_ready() != 1) { |
1991 | wpa_printf(MSG_INFO, "WPA: Not enough entropy in random pool " | |
1992 | "to proceed - reject first 4-way handshake"); | |
1993 | group->reject_4way_hs_for_entropy = TRUE; | |
40d00d2b NC |
1994 | } else { |
1995 | group->first_sta_seen = TRUE; | |
1996 | group->reject_4way_hs_for_entropy = FALSE; | |
08704cd8 | 1997 | } |
40d00d2b | 1998 | |
aac1efec JM |
1999 | if (wpa_group_init_gmk_and_counter(wpa_auth, group) < 0 || |
2000 | wpa_gtk_update(wpa_auth, group) < 0 || | |
2001 | wpa_group_config_group_keys(wpa_auth, group) < 0) { | |
2002 | wpa_printf(MSG_INFO, "WPA: GMK/GTK setup failed"); | |
2003 | group->first_sta_seen = FALSE; | |
2004 | group->reject_4way_hs_for_entropy = TRUE; | |
2005 | } | |
1bdb7ab3 JM |
2006 | } |
2007 | ||
2008 | ||
6fc6879b JM |
2009 | SM_STATE(WPA_PTK, AUTHENTICATION2) |
2010 | { | |
2011 | SM_ENTRY_MA(WPA_PTK, AUTHENTICATION2, wpa_ptk); | |
1bdb7ab3 | 2012 | |
40d00d2b | 2013 | wpa_group_ensure_init(sm->wpa_auth, sm->group); |
65a32cdb | 2014 | sm->ReAuthenticationRequest = FALSE; |
1bdb7ab3 | 2015 | |
3825a19b JM |
2016 | /* |
2017 | * Definition of ANonce selection in IEEE Std 802.11i-2004 is somewhat | |
2018 | * ambiguous. The Authenticator state machine uses a counter that is | |
2019 | * incremented by one for each 4-way handshake. However, the security | |
2020 | * analysis of 4-way handshake points out that unpredictable nonces | |
2021 | * help in preventing precomputation attacks. Instead of the state | |
2022 | * machine definition, use an unpredictable nonce value here to provide | |
2023 | * stronger protection against potential precomputation attacks. | |
2024 | */ | |
2025 | if (random_get_bytes(sm->ANonce, WPA_NONCE_LEN)) { | |
2026 | wpa_printf(MSG_ERROR, "WPA: Failed to get random data for " | |
2027 | "ANonce."); | |
65a32cdb | 2028 | sm->Disconnect = TRUE; |
3825a19b JM |
2029 | return; |
2030 | } | |
bc8318ac JM |
2031 | wpa_hexdump(MSG_DEBUG, "WPA: Assign ANonce", sm->ANonce, |
2032 | WPA_NONCE_LEN); | |
6fc6879b JM |
2033 | /* IEEE 802.11i does not clear TimeoutCtr here, but this is more |
2034 | * logical place than INITIALIZE since AUTHENTICATION2 can be | |
2035 | * re-entered on ReAuthenticationRequest without going through | |
2036 | * INITIALIZE. */ | |
2037 | sm->TimeoutCtr = 0; | |
2038 | } | |
2039 | ||
2040 | ||
0adc9b28 JM |
2041 | static int wpa_auth_sm_ptk_update(struct wpa_state_machine *sm) |
2042 | { | |
2043 | if (random_get_bytes(sm->ANonce, WPA_NONCE_LEN)) { | |
2044 | wpa_printf(MSG_ERROR, | |
2045 | "WPA: Failed to get random data for ANonce"); | |
2046 | sm->Disconnect = TRUE; | |
2047 | return -1; | |
2048 | } | |
2049 | wpa_hexdump(MSG_DEBUG, "WPA: Assign new ANonce", sm->ANonce, | |
2050 | WPA_NONCE_LEN); | |
2051 | sm->TimeoutCtr = 0; | |
2052 | return 0; | |
2053 | } | |
2054 | ||
2055 | ||
6fc6879b JM |
2056 | SM_STATE(WPA_PTK, INITPMK) |
2057 | { | |
2058 | u8 msk[2 * PMK_LEN]; | |
2059 | size_t len = 2 * PMK_LEN; | |
2060 | ||
2061 | SM_ENTRY_MA(WPA_PTK, INITPMK, wpa_ptk); | |
4ec1fd8e | 2062 | #ifdef CONFIG_IEEE80211R_AP |
6fc6879b | 2063 | sm->xxkey_len = 0; |
4ec1fd8e | 2064 | #endif /* CONFIG_IEEE80211R_AP */ |
6fc6879b JM |
2065 | if (sm->pmksa) { |
2066 | wpa_printf(MSG_DEBUG, "WPA: PMK from PMKSA cache"); | |
207976f0 JM |
2067 | os_memcpy(sm->PMK, sm->pmksa->pmk, sm->pmksa->pmk_len); |
2068 | sm->pmk_len = sm->pmksa->pmk_len; | |
567da5bb JM |
2069 | #ifdef CONFIG_DPP |
2070 | } else if (sm->wpa_key_mgmt == WPA_KEY_MGMT_DPP) { | |
2071 | wpa_printf(MSG_DEBUG, | |
2072 | "DPP: No PMKSA cache entry for STA - reject connection"); | |
2073 | sm->Disconnect = TRUE; | |
2074 | sm->disconnect_reason = WLAN_REASON_INVALID_PMKID; | |
2075 | return; | |
2076 | #endif /* CONFIG_DPP */ | |
6fc6879b | 2077 | } else if (wpa_auth_get_msk(sm->wpa_auth, sm->addr, msk, &len) == 0) { |
207976f0 JM |
2078 | unsigned int pmk_len; |
2079 | ||
834c5d68 | 2080 | if (wpa_key_mgmt_sha384(sm->wpa_key_mgmt)) |
207976f0 JM |
2081 | pmk_len = PMK_LEN_SUITE_B_192; |
2082 | else | |
2083 | pmk_len = PMK_LEN; | |
6fc6879b | 2084 | wpa_printf(MSG_DEBUG, "WPA: PMK from EAPOL state machine " |
207976f0 JM |
2085 | "(MSK len=%lu PMK len=%u)", (unsigned long) len, |
2086 | pmk_len); | |
2087 | if (len < pmk_len) { | |
2088 | wpa_printf(MSG_DEBUG, | |
2089 | "WPA: MSK not long enough (%u) to create PMK (%u)", | |
2090 | (unsigned int) len, (unsigned int) pmk_len); | |
2091 | sm->Disconnect = TRUE; | |
2092 | return; | |
2093 | } | |
2094 | os_memcpy(sm->PMK, msk, pmk_len); | |
2095 | sm->pmk_len = pmk_len; | |
4ec1fd8e | 2096 | #ifdef CONFIG_IEEE80211R_AP |
6fc6879b | 2097 | if (len >= 2 * PMK_LEN) { |
9f12271b JM |
2098 | if (wpa_key_mgmt_sha384(sm->wpa_key_mgmt)) { |
2099 | os_memcpy(sm->xxkey, msk, SHA384_MAC_LEN); | |
2100 | sm->xxkey_len = SHA384_MAC_LEN; | |
2101 | } else { | |
2102 | os_memcpy(sm->xxkey, msk + PMK_LEN, PMK_LEN); | |
2103 | sm->xxkey_len = PMK_LEN; | |
2104 | } | |
6fc6879b | 2105 | } |
4ec1fd8e | 2106 | #endif /* CONFIG_IEEE80211R_AP */ |
6fc6879b | 2107 | } else { |
400de9b1 | 2108 | wpa_printf(MSG_DEBUG, "WPA: Could not get PMK, get_msk: %p", |
cef8fac0 | 2109 | sm->wpa_auth->cb->get_msk); |
1180dd66 JM |
2110 | sm->Disconnect = TRUE; |
2111 | return; | |
6fc6879b | 2112 | } |
31bc66e4 | 2113 | forced_memzero(msk, sizeof(msk)); |
6fc6879b JM |
2114 | |
2115 | sm->req_replay_counter_used = 0; | |
2116 | /* IEEE 802.11i does not set keyRun to FALSE, but not doing this | |
2117 | * will break reauthentication since EAPOL state machines may not be | |
2118 | * get into AUTHENTICATING state that clears keyRun before WPA state | |
2119 | * machine enters AUTHENTICATION2 state and goes immediately to INITPMK | |
2120 | * state and takes PMK from the previously used AAA Key. This will | |
2121 | * eventually fail in 4-Way Handshake because Supplicant uses PMK | |
2122 | * derived from the new AAA Key. Setting keyRun = FALSE here seems to | |
2123 | * be good workaround for this issue. */ | |
2124 | wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_keyRun, 0); | |
2125 | } | |
2126 | ||
2127 | ||
2128 | SM_STATE(WPA_PTK, INITPSK) | |
2129 | { | |
2130 | const u8 *psk; | |
7a12edd1 JM |
2131 | size_t psk_len; |
2132 | ||
6fc6879b | 2133 | SM_ENTRY_MA(WPA_PTK, INITPSK, wpa_ptk); |
7a12edd1 | 2134 | psk = wpa_auth_get_psk(sm->wpa_auth, sm->addr, sm->p2p_dev_addr, NULL, |
dbfa691d | 2135 | &psk_len, NULL); |
6fc6879b | 2136 | if (psk) { |
7a12edd1 JM |
2137 | os_memcpy(sm->PMK, psk, psk_len); |
2138 | sm->pmk_len = psk_len; | |
4ec1fd8e | 2139 | #ifdef CONFIG_IEEE80211R_AP |
6fc6879b JM |
2140 | os_memcpy(sm->xxkey, psk, PMK_LEN); |
2141 | sm->xxkey_len = PMK_LEN; | |
4ec1fd8e | 2142 | #endif /* CONFIG_IEEE80211R_AP */ |
6fc6879b | 2143 | } |
e61fea6b JM |
2144 | #ifdef CONFIG_SAE |
2145 | if (wpa_auth_uses_sae(sm) && sm->pmksa) { | |
2146 | wpa_printf(MSG_DEBUG, "SAE: PMK from PMKSA cache"); | |
2147 | os_memcpy(sm->PMK, sm->pmksa->pmk, sm->pmksa->pmk_len); | |
2148 | sm->pmk_len = sm->pmksa->pmk_len; | |
6fe3f0f7 | 2149 | #ifdef CONFIG_IEEE80211R_AP |
2150 | os_memcpy(sm->xxkey, sm->pmksa->pmk, sm->pmksa->pmk_len); | |
2151 | sm->xxkey_len = sm->pmksa->pmk_len; | |
2152 | #endif /* CONFIG_IEEE80211R_AP */ | |
e61fea6b JM |
2153 | } |
2154 | #endif /* CONFIG_SAE */ | |
6fc6879b JM |
2155 | sm->req_replay_counter_used = 0; |
2156 | } | |
2157 | ||
2158 | ||
2159 | SM_STATE(WPA_PTK, PTKSTART) | |
2160 | { | |
2161 | u8 buf[2 + RSN_SELECTOR_LEN + PMKID_LEN], *pmkid = NULL; | |
2162 | size_t pmkid_len = 0; | |
2163 | ||
2164 | SM_ENTRY_MA(WPA_PTK, PTKSTART, wpa_ptk); | |
2165 | sm->PTKRequest = FALSE; | |
2166 | sm->TimeoutEvt = FALSE; | |
f23c5b17 | 2167 | sm->alt_snonce_valid = FALSE; |
bae61562 JM |
2168 | |
2169 | sm->TimeoutCtr++; | |
41f140d3 | 2170 | if (sm->TimeoutCtr > sm->wpa_auth->conf.wpa_pairwise_update_count) { |
bae61562 JM |
2171 | /* No point in sending the EAPOL-Key - we will disconnect |
2172 | * immediately following this. */ | |
2173 | return; | |
2174 | } | |
2175 | ||
6fc6879b JM |
2176 | wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG, |
2177 | "sending 1/4 msg of 4-Way Handshake"); | |
2178 | /* | |
c6e1a9a0 JM |
2179 | * For infrastructure BSS cases, it is better for the AP not to include |
2180 | * the PMKID KDE in EAPOL-Key msg 1/4 since it could be used to initiate | |
2181 | * offline search for the passphrase/PSK without having to be able to | |
2182 | * capture a 4-way handshake from a STA that has access to the network. | |
2183 | * | |
2184 | * For IBSS cases, addition of PMKID KDE could be considered even with | |
2185 | * WPA2-PSK cases that use multiple PSKs, but only if there is a single | |
2186 | * possible PSK for this STA. However, this should not be done unless | |
2187 | * there is support for using that information on the supplicant side. | |
2188 | * The concern about exposing PMKID unnecessarily in infrastructure BSS | |
2189 | * cases would also apply here, but at least in the IBSS case, this | |
2190 | * would cover a potential real use case. | |
6fc6879b JM |
2191 | */ |
2192 | if (sm->wpa == WPA_VERSION_WPA2 && | |
e61fea6b | 2193 | (wpa_key_mgmt_wpa_ieee8021x(sm->wpa_key_mgmt) || |
d90f10fa | 2194 | (sm->wpa_key_mgmt == WPA_KEY_MGMT_OWE && sm->pmksa) || |
e61fea6b | 2195 | wpa_key_mgmt_sae(sm->wpa_key_mgmt)) && |
a14896e8 | 2196 | sm->wpa_key_mgmt != WPA_KEY_MGMT_OSEN) { |
6fc6879b JM |
2197 | pmkid = buf; |
2198 | pmkid_len = 2 + RSN_SELECTOR_LEN + PMKID_LEN; | |
2199 | pmkid[0] = WLAN_EID_VENDOR_SPECIFIC; | |
2200 | pmkid[1] = RSN_SELECTOR_LEN + PMKID_LEN; | |
2201 | RSN_SELECTOR_PUT(&pmkid[2], RSN_KEY_DATA_PMKID); | |
929a2ea5 | 2202 | if (sm->pmksa) { |
9d94e4bb JM |
2203 | wpa_hexdump(MSG_DEBUG, |
2204 | "RSN: Message 1/4 PMKID from PMKSA entry", | |
2205 | sm->pmksa->pmkid, PMKID_LEN); | |
6fc6879b JM |
2206 | os_memcpy(&pmkid[2 + RSN_SELECTOR_LEN], |
2207 | sm->pmksa->pmkid, PMKID_LEN); | |
929a2ea5 JM |
2208 | } else if (wpa_key_mgmt_suite_b(sm->wpa_key_mgmt)) { |
2209 | /* No KCK available to derive PMKID */ | |
9d94e4bb JM |
2210 | wpa_printf(MSG_DEBUG, |
2211 | "RSN: No KCK available to derive PMKID for message 1/4"); | |
929a2ea5 | 2212 | pmkid = NULL; |
a40bd06e JM |
2213 | #ifdef CONFIG_FILS |
2214 | } else if (wpa_key_mgmt_fils(sm->wpa_key_mgmt)) { | |
2215 | if (sm->pmkid_set) { | |
2216 | wpa_hexdump(MSG_DEBUG, | |
2217 | "RSN: Message 1/4 PMKID from FILS/ERP", | |
2218 | sm->pmkid, PMKID_LEN); | |
2219 | os_memcpy(&pmkid[2 + RSN_SELECTOR_LEN], | |
2220 | sm->pmkid, PMKID_LEN); | |
2221 | } else { | |
2222 | /* No PMKID available */ | |
2223 | wpa_printf(MSG_DEBUG, | |
2224 | "RSN: No FILS/ERP PMKID available for message 1/4"); | |
2225 | pmkid = NULL; | |
2226 | } | |
2227 | #endif /* CONFIG_FILS */ | |
fb1dee22 JM |
2228 | #ifdef CONFIG_IEEE80211R_AP |
2229 | } else if (wpa_key_mgmt_ft(sm->wpa_key_mgmt) && | |
2230 | sm->ft_completed) { | |
2231 | wpa_printf(MSG_DEBUG, | |
2232 | "FT: No PMKID in message 1/4 when using FT protocol"); | |
2233 | pmkid = NULL; | |
fb1dee22 | 2234 | #endif /* CONFIG_IEEE80211R_AP */ |
9d94e4bb JM |
2235 | #ifdef CONFIG_SAE |
2236 | } else if (wpa_key_mgmt_sae(sm->wpa_key_mgmt)) { | |
2237 | if (sm->pmkid_set) { | |
2238 | wpa_hexdump(MSG_DEBUG, | |
2239 | "RSN: Message 1/4 PMKID from SAE", | |
2240 | sm->pmkid, PMKID_LEN); | |
2241 | os_memcpy(&pmkid[2 + RSN_SELECTOR_LEN], | |
2242 | sm->pmkid, PMKID_LEN); | |
2243 | } else { | |
2244 | /* No PMKID available */ | |
2245 | wpa_printf(MSG_DEBUG, | |
2246 | "RSN: No SAE PMKID available for message 1/4"); | |
2247 | pmkid = NULL; | |
2248 | } | |
2249 | #endif /* CONFIG_SAE */ | |
929a2ea5 | 2250 | } else { |
6fc6879b JM |
2251 | /* |
2252 | * Calculate PMKID since no PMKSA cache entry was | |
2253 | * available with pre-calculated PMKID. | |
2254 | */ | |
207976f0 | 2255 | rsn_pmkid(sm->PMK, sm->pmk_len, sm->wpa_auth->addr, |
56586197 | 2256 | sm->addr, &pmkid[2 + RSN_SELECTOR_LEN], |
41b81914 | 2257 | sm->wpa_key_mgmt); |
9d94e4bb JM |
2258 | wpa_hexdump(MSG_DEBUG, |
2259 | "RSN: Message 1/4 PMKID derived from PMK", | |
2260 | &pmkid[2 + RSN_SELECTOR_LEN], PMKID_LEN); | |
6fc6879b JM |
2261 | } |
2262 | } | |
307cfc32 MK |
2263 | if (!pmkid) |
2264 | pmkid_len = 0; | |
6fc6879b JM |
2265 | wpa_send_eapol(sm->wpa_auth, sm, |
2266 | WPA_KEY_INFO_ACK | WPA_KEY_INFO_KEY_TYPE, NULL, | |
2267 | sm->ANonce, pmkid, pmkid_len, 0, 0); | |
6fc6879b JM |
2268 | } |
2269 | ||
2270 | ||
f23c5b17 | 2271 | static int wpa_derive_ptk(struct wpa_state_machine *sm, const u8 *snonce, |
207976f0 | 2272 | const u8 *pmk, unsigned int pmk_len, |
65a44e84 | 2273 | struct wpa_ptk *ptk, int force_sha256) |
6fc6879b | 2274 | { |
10ec6a5f JM |
2275 | const u8 *z = NULL; |
2276 | size_t z_len = 0; | |
65a44e84 | 2277 | int akmp; |
10ec6a5f | 2278 | |
4ec1fd8e | 2279 | #ifdef CONFIG_IEEE80211R_AP |
b583ed38 JM |
2280 | if (wpa_key_mgmt_ft(sm->wpa_key_mgmt)) { |
2281 | if (sm->ft_completed) { | |
2282 | u8 ptk_name[WPA_PMK_NAME_LEN]; | |
2283 | ||
2284 | return wpa_pmk_r1_to_ptk(sm->pmk_r1, sm->pmk_r1_len, | |
2285 | sm->SNonce, sm->ANonce, | |
2286 | sm->addr, sm->wpa_auth->addr, | |
2287 | sm->pmk_r1_name, | |
2288 | ptk, ptk_name, | |
2289 | sm->wpa_key_mgmt, | |
2290 | sm->pairwise); | |
2291 | } | |
267c366f | 2292 | return wpa_auth_derive_ptk_ft(sm, ptk); |
b583ed38 | 2293 | } |
4ec1fd8e | 2294 | #endif /* CONFIG_IEEE80211R_AP */ |
6fc6879b | 2295 | |
10ec6a5f JM |
2296 | #ifdef CONFIG_DPP2 |
2297 | if (sm->wpa_key_mgmt == WPA_KEY_MGMT_DPP && sm->dpp_z) { | |
2298 | z = wpabuf_head(sm->dpp_z); | |
2299 | z_len = wpabuf_len(sm->dpp_z); | |
2300 | } | |
2301 | #endif /* CONFIG_DPP2 */ | |
2302 | ||
65a44e84 JM |
2303 | akmp = sm->wpa_key_mgmt; |
2304 | if (force_sha256) | |
0a76a0b9 | 2305 | akmp |= WPA_KEY_MGMT_PSK_SHA256; |
207976f0 | 2306 | return wpa_pmk_to_ptk(pmk, pmk_len, "Pairwise key expansion", |
98cd3d1c | 2307 | sm->wpa_auth->addr, sm->addr, sm->ANonce, snonce, |
65a44e84 | 2308 | ptk, akmp, sm->pairwise, z, z_len); |
6fc6879b JM |
2309 | } |
2310 | ||
2311 | ||
3b5b7aa8 | 2312 | #ifdef CONFIG_FILS |
c4fd6d8a JM |
2313 | |
2314 | int fils_auth_pmk_to_ptk(struct wpa_state_machine *sm, const u8 *pmk, | |
80ddf5d9 | 2315 | size_t pmk_len, const u8 *snonce, const u8 *anonce, |
4cada9dc | 2316 | const u8 *dhss, size_t dhss_len, |
80ddf5d9 | 2317 | struct wpabuf *g_sta, struct wpabuf *g_ap) |
c4fd6d8a JM |
2318 | { |
2319 | u8 ick[FILS_ICK_MAX_LEN]; | |
2320 | size_t ick_len; | |
2321 | int res; | |
8fed47e0 JM |
2322 | u8 fils_ft[FILS_FT_MAX_LEN]; |
2323 | size_t fils_ft_len = 0; | |
c4fd6d8a JM |
2324 | |
2325 | res = fils_pmk_to_ptk(pmk, pmk_len, sm->addr, sm->wpa_auth->addr, | |
4cada9dc JM |
2326 | snonce, anonce, dhss, dhss_len, |
2327 | &sm->PTK, ick, &ick_len, | |
8fed47e0 JM |
2328 | sm->wpa_key_mgmt, sm->pairwise, |
2329 | fils_ft, &fils_ft_len); | |
c4fd6d8a JM |
2330 | if (res < 0) |
2331 | return res; | |
2332 | sm->PTK_valid = TRUE; | |
2f1357fb | 2333 | sm->tk_already_set = FALSE; |
c4fd6d8a | 2334 | |
8fed47e0 JM |
2335 | #ifdef CONFIG_IEEE80211R_AP |
2336 | if (fils_ft_len) { | |
2337 | struct wpa_authenticator *wpa_auth = sm->wpa_auth; | |
2338 | struct wpa_auth_config *conf = &wpa_auth->conf; | |
994eac7e JM |
2339 | u8 pmk_r0[PMK_LEN_MAX], pmk_r0_name[WPA_PMK_NAME_LEN]; |
2340 | int use_sha384 = wpa_key_mgmt_sha384(sm->wpa_key_mgmt); | |
2341 | size_t pmk_r0_len = use_sha384 ? SHA384_MAC_LEN : PMK_LEN; | |
8fed47e0 JM |
2342 | |
2343 | if (wpa_derive_pmk_r0(fils_ft, fils_ft_len, | |
2344 | conf->ssid, conf->ssid_len, | |
2345 | conf->mobility_domain, | |
2346 | conf->r0_key_holder, | |
2347 | conf->r0_key_holder_len, | |
994eac7e JM |
2348 | sm->addr, pmk_r0, pmk_r0_name, |
2349 | use_sha384) < 0) | |
8fed47e0 JM |
2350 | return -1; |
2351 | ||
994eac7e JM |
2352 | wpa_hexdump_key(MSG_DEBUG, "FILS+FT: PMK-R0", |
2353 | pmk_r0, pmk_r0_len); | |
8fed47e0 JM |
2354 | wpa_hexdump(MSG_DEBUG, "FILS+FT: PMKR0Name", |
2355 | pmk_r0_name, WPA_PMK_NAME_LEN); | |
89dea17e | 2356 | wpa_ft_store_pmk_fils(sm, pmk_r0, pmk_r0_name); |
31bc66e4 | 2357 | forced_memzero(fils_ft, sizeof(fils_ft)); |
36536639 JM |
2358 | |
2359 | res = wpa_derive_pmk_r1_name(pmk_r0_name, conf->r1_key_holder, | |
2360 | sm->addr, sm->pmk_r1_name, | |
2361 | use_sha384); | |
31bc66e4 | 2362 | forced_memzero(pmk_r0, PMK_LEN_MAX); |
36536639 JM |
2363 | if (res < 0) |
2364 | return -1; | |
2365 | wpa_hexdump(MSG_DEBUG, "FILS+FT: PMKR1Name", sm->pmk_r1_name, | |
2366 | WPA_PMK_NAME_LEN); | |
2367 | sm->pmk_r1_name_valid = 1; | |
8fed47e0 JM |
2368 | } |
2369 | #endif /* CONFIG_IEEE80211R_AP */ | |
2370 | ||
c4fd6d8a JM |
2371 | res = fils_key_auth_sk(ick, ick_len, snonce, anonce, |
2372 | sm->addr, sm->wpa_auth->addr, | |
80ddf5d9 JM |
2373 | g_sta ? wpabuf_head(g_sta) : NULL, |
2374 | g_sta ? wpabuf_len(g_sta) : 0, | |
2375 | g_ap ? wpabuf_head(g_ap) : NULL, | |
2376 | g_ap ? wpabuf_len(g_ap) : 0, | |
c4fd6d8a JM |
2377 | sm->wpa_key_mgmt, sm->fils_key_auth_sta, |
2378 | sm->fils_key_auth_ap, | |
2379 | &sm->fils_key_auth_len); | |
31bc66e4 | 2380 | forced_memzero(ick, sizeof(ick)); |
78815f3d JM |
2381 | |
2382 | /* Store nonces for (Re)Association Request/Response frame processing */ | |
2383 | os_memcpy(sm->SNonce, snonce, FILS_NONCE_LEN); | |
2384 | os_memcpy(sm->ANonce, anonce, FILS_NONCE_LEN); | |
2385 | ||
c4fd6d8a JM |
2386 | return res; |
2387 | } | |
2388 | ||
2389 | ||
3b5b7aa8 | 2390 | static int wpa_aead_decrypt(struct wpa_state_machine *sm, struct wpa_ptk *ptk, |
75c8563e | 2391 | u8 *buf, size_t buf_len, u16 *_key_data_len) |
3b5b7aa8 JM |
2392 | { |
2393 | struct ieee802_1x_hdr *hdr; | |
2394 | struct wpa_eapol_key *key; | |
2395 | u8 *pos; | |
2396 | u16 key_data_len; | |
2397 | u8 *tmp; | |
2398 | const u8 *aad[1]; | |
2399 | size_t aad_len[1]; | |
2400 | ||
75c8563e | 2401 | hdr = (struct ieee802_1x_hdr *) buf; |
3b5b7aa8 JM |
2402 | key = (struct wpa_eapol_key *) (hdr + 1); |
2403 | pos = (u8 *) (key + 1); | |
2404 | key_data_len = WPA_GET_BE16(pos); | |
2405 | if (key_data_len < AES_BLOCK_SIZE || | |
2406 | key_data_len > buf_len - sizeof(*hdr) - sizeof(*key) - 2) { | |
2407 | wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_INFO, | |
2408 | "No room for AES-SIV data in the frame"); | |
2409 | return -1; | |
2410 | } | |
2411 | pos += 2; /* Pointing at the Encrypted Key Data field */ | |
2412 | ||
2413 | tmp = os_malloc(key_data_len); | |
2414 | if (!tmp) | |
2415 | return -1; | |
2416 | ||
2417 | /* AES-SIV AAD from EAPOL protocol version field (inclusive) to | |
2418 | * to Key Data (exclusive). */ | |
75c8563e JM |
2419 | aad[0] = buf; |
2420 | aad_len[0] = pos - buf; | |
3b5b7aa8 JM |
2421 | if (aes_siv_decrypt(ptk->kek, ptk->kek_len, pos, key_data_len, |
2422 | 1, aad, aad_len, tmp) < 0) { | |
2423 | wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_INFO, | |
2424 | "Invalid AES-SIV data in the frame"); | |
2425 | bin_clear_free(tmp, key_data_len); | |
2426 | return -1; | |
2427 | } | |
2428 | ||
2429 | /* AEAD decryption and validation completed successfully */ | |
2430 | key_data_len -= AES_BLOCK_SIZE; | |
2431 | wpa_hexdump_key(MSG_DEBUG, "WPA: Decrypted Key Data", | |
2432 | tmp, key_data_len); | |
2433 | ||
2434 | /* Replace Key Data field with the decrypted version */ | |
2435 | os_memcpy(pos, tmp, key_data_len); | |
2436 | pos -= 2; /* Key Data Length field */ | |
2437 | WPA_PUT_BE16(pos, key_data_len); | |
2438 | bin_clear_free(tmp, key_data_len); | |
2439 | if (_key_data_len) | |
2440 | *_key_data_len = key_data_len; | |
2441 | return 0; | |
2442 | } | |
78815f3d JM |
2443 | |
2444 | ||
087631b9 JM |
2445 | const u8 * wpa_fils_validate_fils_session(struct wpa_state_machine *sm, |
2446 | const u8 *ies, size_t ies_len, | |
2447 | const u8 *fils_session) | |
2448 | { | |
2449 | const u8 *ie, *end; | |
2450 | const u8 *session = NULL; | |
2451 | ||
2452 | if (!wpa_key_mgmt_fils(sm->wpa_key_mgmt)) { | |
2453 | wpa_printf(MSG_DEBUG, | |
2454 | "FILS: Not a FILS AKM - reject association"); | |
2455 | return NULL; | |
2456 | } | |
2457 | ||
2458 | /* Verify Session element */ | |
2459 | ie = ies; | |
2460 | end = ((const u8 *) ie) + ies_len; | |
2461 | while (ie + 1 < end) { | |
2462 | if (ie + 2 + ie[1] > end) | |
2463 | break; | |
2464 | if (ie[0] == WLAN_EID_EXTENSION && | |
2465 | ie[1] >= 1 + FILS_SESSION_LEN && | |
2466 | ie[2] == WLAN_EID_EXT_FILS_SESSION) { | |
2467 | session = ie; | |
2468 | break; | |
2469 | } | |
2470 | ie += 2 + ie[1]; | |
2471 | } | |
2472 | ||
2473 | if (!session) { | |
2474 | wpa_printf(MSG_DEBUG, | |
2475 | "FILS: %s: Could not find FILS Session element in Assoc Req - reject", | |
2476 | __func__); | |
2477 | return NULL; | |
2478 | } | |
2479 | ||
2480 | if (!fils_session) { | |
2481 | wpa_printf(MSG_DEBUG, | |
2482 | "FILS: %s: Could not find FILS Session element in STA entry - reject", | |
2483 | __func__); | |
2484 | return NULL; | |
2485 | } | |
2486 | ||
2487 | if (os_memcmp(fils_session, session + 3, FILS_SESSION_LEN) != 0) { | |
2488 | wpa_printf(MSG_DEBUG, "FILS: Session mismatch"); | |
2489 | wpa_hexdump(MSG_DEBUG, "FILS: Expected FILS Session", | |
2490 | fils_session, FILS_SESSION_LEN); | |
2491 | wpa_hexdump(MSG_DEBUG, "FILS: Received FILS Session", | |
2492 | session + 3, FILS_SESSION_LEN); | |
2493 | return NULL; | |
2494 | } | |
2495 | return session; | |
2496 | } | |
2497 | ||
2498 | ||
bd599353 JM |
2499 | int wpa_fils_validate_key_confirm(struct wpa_state_machine *sm, const u8 *ies, |
2500 | size_t ies_len) | |
2501 | { | |
2502 | struct ieee802_11_elems elems; | |
2503 | ||
2504 | if (ieee802_11_parse_elems(ies, ies_len, &elems, 1) == ParseFailed) { | |
2505 | wpa_printf(MSG_DEBUG, | |
2506 | "FILS: Failed to parse decrypted elements"); | |
2507 | return -1; | |
2508 | } | |
2509 | ||
8b5ddda5 JM |
2510 | if (!elems.fils_session) { |
2511 | wpa_printf(MSG_DEBUG, "FILS: No FILS Session element"); | |
2512 | return -1; | |
2513 | } | |
2514 | ||
bd599353 JM |
2515 | if (!elems.fils_key_confirm) { |
2516 | wpa_printf(MSG_DEBUG, "FILS: No FILS Key Confirm element"); | |
2517 | return -1; | |
2518 | } | |
2519 | ||
2520 | if (elems.fils_key_confirm_len != sm->fils_key_auth_len) { | |
2521 | wpa_printf(MSG_DEBUG, | |
2522 | "FILS: Unexpected Key-Auth length %d (expected %d)", | |
2523 | elems.fils_key_confirm_len, | |
2524 | (int) sm->fils_key_auth_len); | |
2525 | return -1; | |
2526 | } | |
2527 | ||
2528 | if (os_memcmp(elems.fils_key_confirm, sm->fils_key_auth_sta, | |
2529 | sm->fils_key_auth_len) != 0) { | |
2530 | wpa_printf(MSG_DEBUG, "FILS: Key-Auth mismatch"); | |
2531 | wpa_hexdump(MSG_DEBUG, "FILS: Received Key-Auth", | |
2532 | elems.fils_key_confirm, elems.fils_key_confirm_len); | |
2533 | wpa_hexdump(MSG_DEBUG, "FILS: Expected Key-Auth", | |
2534 | sm->fils_key_auth_sta, sm->fils_key_auth_len); | |
2535 | return -1; | |
2536 | } | |
2537 | ||
2538 | return 0; | |
2539 | } | |
2540 | ||
2541 | ||
78815f3d JM |
2542 | int fils_decrypt_assoc(struct wpa_state_machine *sm, const u8 *fils_session, |
2543 | const struct ieee80211_mgmt *mgmt, size_t frame_len, | |
2544 | u8 *pos, size_t left) | |
2545 | { | |
2546 | u16 fc, stype; | |
2547 | const u8 *end, *ie_start, *ie, *session, *crypt; | |
78815f3d JM |
2548 | const u8 *aad[5]; |
2549 | size_t aad_len[5]; | |
2550 | ||
2551 | if (!sm || !sm->PTK_valid) { | |
2552 | wpa_printf(MSG_DEBUG, | |
2553 | "FILS: No KEK to decrypt Assocication Request frame"); | |
2554 | return -1; | |
2555 | } | |
2556 | ||
2557 | if (!wpa_key_mgmt_fils(sm->wpa_key_mgmt)) { | |
2558 | wpa_printf(MSG_DEBUG, | |
2559 | "FILS: Not a FILS AKM - reject association"); | |
2560 | return -1; | |
2561 | } | |
2562 | ||
2563 | end = ((const u8 *) mgmt) + frame_len; | |
2564 | fc = le_to_host16(mgmt->frame_control); | |
2565 | stype = WLAN_FC_GET_STYPE(fc); | |
2566 | if (stype == WLAN_FC_STYPE_REASSOC_REQ) | |
2567 | ie_start = mgmt->u.reassoc_req.variable; | |
2568 | else | |
2569 | ie_start = mgmt->u.assoc_req.variable; | |
2570 | ie = ie_start; | |
2571 | ||
2572 | /* | |
2573 | * Find FILS Session element which is the last unencrypted element in | |
2574 | * the frame. | |
2575 | */ | |
087631b9 JM |
2576 | session = wpa_fils_validate_fils_session(sm, ie, end - ie, |
2577 | fils_session); | |
78815f3d | 2578 | if (!session) { |
087631b9 | 2579 | wpa_printf(MSG_DEBUG, "FILS: Session validation failed"); |
78815f3d JM |
2580 | return -1; |
2581 | } | |
087631b9 | 2582 | |
78815f3d JM |
2583 | crypt = session + 2 + session[1]; |
2584 | ||
2585 | if (end - crypt < AES_BLOCK_SIZE) { | |
2586 | wpa_printf(MSG_DEBUG, | |
2587 | "FILS: Too short frame to include AES-SIV data"); | |
2588 | return -1; | |
2589 | } | |
2590 | ||
2591 | /* AES-SIV AAD vectors */ | |
2592 | ||
2593 | /* The STA's MAC address */ | |
2594 | aad[0] = mgmt->sa; | |
2595 | aad_len[0] = ETH_ALEN; | |
2596 | /* The AP's BSSID */ | |
2597 | aad[1] = mgmt->da; | |
2598 | aad_len[1] = ETH_ALEN; | |
2599 | /* The STA's nonce */ | |
2600 | aad[2] = sm->SNonce; | |
2601 | aad_len[2] = FILS_NONCE_LEN; | |
2602 | /* The AP's nonce */ | |
2603 | aad[3] = sm->ANonce; | |
2604 | aad_len[3] = FILS_NONCE_LEN; | |
2605 | /* | |
2606 | * The (Re)Association Request frame from the Capability Information | |
2607 | * field to the FILS Session element (both inclusive). | |
2608 | */ | |
2609 | aad[4] = (const u8 *) &mgmt->u.assoc_req.capab_info; | |
d77f3304 | 2610 | aad_len[4] = crypt - aad[4]; |
78815f3d JM |
2611 | |
2612 | if (aes_siv_decrypt(sm->PTK.kek, sm->PTK.kek_len, crypt, end - crypt, | |
d77f3304 | 2613 | 5, aad, aad_len, pos + (crypt - ie_start)) < 0) { |
78815f3d JM |
2614 | wpa_printf(MSG_DEBUG, |
2615 | "FILS: Invalid AES-SIV data in the frame"); | |
2616 | return -1; | |
2617 | } | |
2618 | wpa_hexdump(MSG_DEBUG, "FILS: Decrypted Association Request elements", | |
2619 | pos, left - AES_BLOCK_SIZE); | |
2620 | ||
bd599353 JM |
2621 | if (wpa_fils_validate_key_confirm(sm, pos, left - AES_BLOCK_SIZE) < 0) { |
2622 | wpa_printf(MSG_DEBUG, "FILS: Key Confirm validation failed"); | |
78815f3d JM |
2623 | return -1; |
2624 | } | |
2625 | ||
2626 | return left - AES_BLOCK_SIZE; | |
2627 | } | |
2628 | ||
e73ffa09 JM |
2629 | |
2630 | int fils_encrypt_assoc(struct wpa_state_machine *sm, u8 *buf, | |
91d91abf JM |
2631 | size_t current_len, size_t max_len, |
2632 | const struct wpabuf *hlp) | |
e73ffa09 JM |
2633 | { |
2634 | u8 *end = buf + max_len; | |
2635 | u8 *pos = buf + current_len; | |
2636 | struct ieee80211_mgmt *mgmt; | |
2637 | struct wpabuf *plain; | |
e73ffa09 JM |
2638 | const u8 *aad[5]; |
2639 | size_t aad_len[5]; | |
2640 | ||
2641 | if (!sm || !sm->PTK_valid) | |
2642 | return -1; | |
2643 | ||
2644 | wpa_hexdump(MSG_DEBUG, | |
2645 | "FILS: Association Response frame before FILS processing", | |
2646 | buf, current_len); | |
2647 | ||
2648 | mgmt = (struct ieee80211_mgmt *) buf; | |
2649 | ||
2650 | /* AES-SIV AAD vectors */ | |
2651 | ||
2652 | /* The AP's BSSID */ | |
2653 | aad[0] = mgmt->sa; | |
2654 | aad_len[0] = ETH_ALEN; | |
2655 | /* The STA's MAC address */ | |
2656 | aad[1] = mgmt->da; | |
2657 | aad_len[1] = ETH_ALEN; | |
2658 | /* The AP's nonce */ | |
2659 | aad[2] = sm->ANonce; | |
2660 | aad_len[2] = FILS_NONCE_LEN; | |
2661 | /* The STA's nonce */ | |
2662 | aad[3] = sm->SNonce; | |
2663 | aad_len[3] = FILS_NONCE_LEN; | |
2664 | /* | |
2665 | * The (Re)Association Response frame from the Capability Information | |
2666 | * field (the same offset in both Association and Reassociation | |
2667 | * Response frames) to the FILS Session element (both inclusive). | |
2668 | */ | |
2669 | aad[4] = (const u8 *) &mgmt->u.assoc_resp.capab_info; | |
2670 | aad_len[4] = pos - aad[4]; | |
2671 | ||
2672 | /* The following elements will be encrypted with AES-SIV */ | |
9392859d JM |
2673 | plain = fils_prepare_plainbuf(sm, hlp); |
2674 | if (!plain) { | |
2675 | wpa_printf(MSG_DEBUG, "FILS: Plain buffer prep failed"); | |
2676 | return -1; | |
2677 | } | |
2678 | ||
2679 | if (pos + wpabuf_len(plain) + AES_BLOCK_SIZE > end) { | |
2680 | wpa_printf(MSG_DEBUG, | |
2681 | "FILS: Not enough room for FILS elements"); | |
ce362f88 | 2682 | wpabuf_clear_free(plain); |
9392859d JM |
2683 | return -1; |
2684 | } | |
2685 | ||
2686 | wpa_hexdump_buf_key(MSG_DEBUG, "FILS: Association Response plaintext", | |
2687 | plain); | |
2688 | ||
2689 | if (aes_siv_encrypt(sm->PTK.kek, sm->PTK.kek_len, | |
2690 | wpabuf_head(plain), wpabuf_len(plain), | |
2691 | 5, aad, aad_len, pos) < 0) { | |
ce362f88 | 2692 | wpabuf_clear_free(plain); |
9392859d JM |
2693 | return -1; |
2694 | } | |
2695 | ||
2696 | wpa_hexdump(MSG_DEBUG, | |
2697 | "FILS: Encrypted Association Response elements", | |
2698 | pos, AES_BLOCK_SIZE + wpabuf_len(plain)); | |
2699 | current_len += wpabuf_len(plain) + AES_BLOCK_SIZE; | |
ce362f88 | 2700 | wpabuf_clear_free(plain); |
9392859d JM |
2701 | |
2702 | sm->fils_completed = 1; | |
2703 | ||
2704 | return current_len; | |
2705 | } | |
2706 | ||
2707 | ||
2708 | static struct wpabuf * fils_prepare_plainbuf(struct wpa_state_machine *sm, | |
2709 | const struct wpabuf *hlp) | |
2710 | { | |
2711 | struct wpabuf *plain; | |
2712 | u8 *len, *tmp, *tmp2; | |
2713 | u8 hdr[2]; | |
2714 | u8 *gtk, dummy_gtk[32]; | |
2715 | size_t gtk_len; | |
2716 | struct wpa_group *gsm; | |
82cc0b0c JM |
2717 | size_t plain_len; |
2718 | struct wpa_auth_config *conf = &sm->wpa_auth->conf; | |
e73ffa09 | 2719 | |
82cc0b0c JM |
2720 | plain_len = 1000 + ieee80211w_kde_len(sm); |
2721 | if (conf->transition_disable) | |
2722 | plain_len += 2 + RSN_SELECTOR_LEN + 1; | |
2723 | plain = wpabuf_alloc(plain_len); | |
e73ffa09 | 2724 | if (!plain) |
9392859d | 2725 | return NULL; |
e73ffa09 JM |
2726 | |
2727 | /* TODO: FILS Public Key */ | |
2728 | ||
2729 | /* FILS Key Confirmation */ | |
2730 | wpabuf_put_u8(plain, WLAN_EID_EXTENSION); /* Element ID */ | |
2731 | wpabuf_put_u8(plain, 1 + sm->fils_key_auth_len); /* Length */ | |
2732 | /* Element ID Extension */ | |
2733 | wpabuf_put_u8(plain, WLAN_EID_EXT_FILS_KEY_CONFIRM); | |
2734 | wpabuf_put_data(plain, sm->fils_key_auth_ap, sm->fils_key_auth_len); | |
2735 | ||
91d91abf JM |
2736 | /* FILS HLP Container */ |
2737 | if (hlp) | |
2738 | wpabuf_put_buf(plain, hlp); | |
e73ffa09 JM |
2739 | |
2740 | /* TODO: FILS IP Address Assignment */ | |
2741 | ||
2742 | /* Key Delivery */ | |
2743 | gsm = sm->group; | |
2744 | wpabuf_put_u8(plain, WLAN_EID_EXTENSION); /* Element ID */ | |
2745 | len = wpabuf_put(plain, 1); | |
2746 | wpabuf_put_u8(plain, WLAN_EID_EXT_KEY_DELIVERY); | |
2747 | wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN, | |
2748 | wpabuf_put(plain, WPA_KEY_RSC_LEN)); | |
2749 | /* GTK KDE */ | |
2750 | gtk = gsm->GTK[gsm->GN - 1]; | |
2751 | gtk_len = gsm->GTK_len; | |
62e12afc | 2752 | if (conf->disable_gtk || sm->wpa_key_mgmt == WPA_KEY_MGMT_OSEN) { |
e73ffa09 JM |
2753 | /* |
2754 | * Provide unique random GTK to each STA to prevent use | |
2755 | * of GTK in the BSS. | |
2756 | */ | |
2757 | if (random_get_bytes(dummy_gtk, gtk_len) < 0) { | |
ce362f88 | 2758 | wpabuf_clear_free(plain); |
9392859d | 2759 | return NULL; |
e73ffa09 JM |
2760 | } |
2761 | gtk = dummy_gtk; | |
2762 | } | |
2763 | hdr[0] = gsm->GN & 0x03; | |
2764 | hdr[1] = 0; | |
2765 | tmp = wpabuf_put(plain, 0); | |
2766 | tmp2 = wpa_add_kde(tmp, RSN_KEY_DATA_GROUPKEY, hdr, 2, | |
2767 | gtk, gtk_len); | |
2768 | wpabuf_put(plain, tmp2 - tmp); | |
2769 | ||
16889aff | 2770 | /* IGTK KDE and BIGTK KDE */ |
e73ffa09 JM |
2771 | tmp = wpabuf_put(plain, 0); |
2772 | tmp2 = ieee80211w_kde_add(sm, tmp); | |
2773 | wpabuf_put(plain, tmp2 - tmp); | |
2774 | ||
82cc0b0c JM |
2775 | if (conf->transition_disable) { |
2776 | tmp = wpabuf_put(plain, 0); | |
2777 | tmp2 = wpa_add_kde(tmp, WFA_KEY_DATA_TRANSITION_DISABLE, | |
2778 | &conf->transition_disable, 1, NULL, 0); | |
2779 | wpabuf_put(plain, tmp2 - tmp); | |
2780 | } | |
2781 | ||
e73ffa09 | 2782 | *len = (u8 *) wpabuf_put(plain, 0) - len - 1; |
99621dc1 MV |
2783 | |
2784 | #ifdef CONFIG_OCV | |
2785 | if (wpa_auth_uses_ocv(sm)) { | |
2786 | struct wpa_channel_info ci; | |
2787 | u8 *pos; | |
2788 | ||
2789 | if (wpa_channel_info(sm->wpa_auth, &ci) != 0) { | |
2790 | wpa_printf(MSG_WARNING, | |
2791 | "FILS: Failed to get channel info for OCI element"); | |
ce362f88 | 2792 | wpabuf_clear_free(plain); |
99621dc1 MV |
2793 | return NULL; |
2794 | } | |
2795 | ||
2796 | pos = wpabuf_put(plain, OCV_OCI_EXTENDED_LEN); | |
2797 | if (ocv_insert_extended_oci(&ci, pos) < 0) { | |
ce362f88 | 2798 | wpabuf_clear_free(plain); |
99621dc1 MV |
2799 | return NULL; |
2800 | } | |
2801 | } | |
2802 | #endif /* CONFIG_OCV */ | |
2803 | ||
9392859d | 2804 | return plain; |
e73ffa09 JM |
2805 | } |
2806 | ||
da24c5aa JM |
2807 | |
2808 | int fils_set_tk(struct wpa_state_machine *sm) | |
2809 | { | |
2810 | enum wpa_alg alg; | |
2811 | int klen; | |
2812 | ||
2f1357fb JM |
2813 | if (!sm || !sm->PTK_valid) { |
2814 | wpa_printf(MSG_DEBUG, "FILS: No valid PTK available to set TK"); | |
2815 | return -1; | |
2816 | } | |
2817 | if (sm->tk_already_set) { | |
2818 | wpa_printf(MSG_DEBUG, "FILS: TK already set to the driver"); | |
da24c5aa | 2819 | return -1; |
2f1357fb | 2820 | } |
da24c5aa JM |
2821 | |
2822 | alg = wpa_cipher_to_alg(sm->pairwise); | |
2823 | klen = wpa_cipher_key_len(sm->pairwise); | |
2824 | ||
2825 | wpa_printf(MSG_DEBUG, "FILS: Configure TK to the driver"); | |
2826 | if (wpa_auth_set_key(sm->wpa_auth, 0, alg, sm->addr, 0, | |
a919a260 | 2827 | sm->PTK.tk, klen, KEY_FLAG_PAIRWISE_RX_TX)) { |
da24c5aa JM |
2828 | wpa_printf(MSG_DEBUG, "FILS: Failed to set TK to the driver"); |
2829 | return -1; | |
2830 | } | |
2f1357fb | 2831 | sm->tk_already_set = TRUE; |
da24c5aa JM |
2832 | |
2833 | return 0; | |
2834 | } | |
2835 | ||
fa61bff6 JM |
2836 | |
2837 | u8 * hostapd_eid_assoc_fils_session(struct wpa_state_machine *sm, u8 *buf, | |
8b5ddda5 | 2838 | const u8 *fils_session, struct wpabuf *hlp) |
fa61bff6 JM |
2839 | { |
2840 | struct wpabuf *plain; | |
2841 | u8 *pos = buf; | |
2842 | ||
2843 | /* FILS Session */ | |
2844 | *pos++ = WLAN_EID_EXTENSION; /* Element ID */ | |
2845 | *pos++ = 1 + FILS_SESSION_LEN; /* Length */ | |
2846 | *pos++ = WLAN_EID_EXT_FILS_SESSION; /* Element ID Extension */ | |
2847 | os_memcpy(pos, fils_session, FILS_SESSION_LEN); | |
2848 | pos += FILS_SESSION_LEN; | |
2849 | ||
8b5ddda5 | 2850 | plain = fils_prepare_plainbuf(sm, hlp); |
fa61bff6 JM |
2851 | if (!plain) { |
2852 | wpa_printf(MSG_DEBUG, "FILS: Plain buffer prep failed"); | |
2853 | return NULL; | |
2854 | } | |
2855 | ||
2856 | os_memcpy(pos, wpabuf_head(plain), wpabuf_len(plain)); | |
2857 | pos += wpabuf_len(plain); | |
2858 | ||
2859 | wpa_printf(MSG_DEBUG, "%s: plain buf_len: %u", __func__, | |
2860 | (unsigned int) wpabuf_len(plain)); | |
ce362f88 | 2861 | wpabuf_clear_free(plain); |
fa61bff6 JM |
2862 | sm->fils_completed = 1; |
2863 | return pos; | |
2864 | } | |
2865 | ||
3b5b7aa8 JM |
2866 | #endif /* CONFIG_FILS */ |
2867 | ||
2868 | ||
1034f67b MV |
2869 | #ifdef CONFIG_OCV |
2870 | int get_sta_tx_parameters(struct wpa_state_machine *sm, int ap_max_chanwidth, | |
2871 | int ap_seg1_idx, int *bandwidth, int *seg1_idx) | |
2872 | { | |
2873 | struct wpa_authenticator *wpa_auth = sm->wpa_auth; | |
2874 | ||
2875 | if (!wpa_auth->cb->get_sta_tx_params) | |
2876 | return -1; | |
2877 | return wpa_auth->cb->get_sta_tx_params(wpa_auth->cb_ctx, sm->addr, | |
2878 | ap_max_chanwidth, ap_seg1_idx, | |
2879 | bandwidth, seg1_idx); | |
2880 | } | |
2881 | #endif /* CONFIG_OCV */ | |
2882 | ||
2883 | ||
6fc6879b JM |
2884 | SM_STATE(WPA_PTK, PTKCALCNEGOTIATING) |
2885 | { | |
3b5b7aa8 | 2886 | struct wpa_authenticator *wpa_auth = sm->wpa_auth; |
6fc6879b | 2887 | struct wpa_ptk PTK; |
2c502460 | 2888 | int ok = 0, psk_found = 0; |
6fc6879b | 2889 | const u8 *pmk = NULL; |
7a12edd1 | 2890 | size_t pmk_len; |
3b5b7aa8 JM |
2891 | int ft; |
2892 | const u8 *eapol_key_ie, *key_data, *mic; | |
2893 | u16 key_data_length; | |
2894 | size_t mic_len, eapol_key_ie_len; | |
2895 | struct ieee802_1x_hdr *hdr; | |
2896 | struct wpa_eapol_key *key; | |
2897 | struct wpa_eapol_ie_parse kde; | |
bb05d036 | 2898 | int vlan_id = 0; |
65a44e84 | 2899 | int owe_ptk_workaround = !!wpa_auth->conf.owe_ptk_workaround; |
6fc6879b JM |
2900 | |
2901 | SM_ENTRY_MA(WPA_PTK, PTKCALCNEGOTIATING, wpa_ptk); | |
2902 | sm->EAPOLKeyReceived = FALSE; | |
68921e24 | 2903 | sm->update_snonce = FALSE; |
b729fd8d | 2904 | os_memset(&PTK, 0, sizeof(PTK)); |
6fc6879b | 2905 | |
567da5bb | 2906 | mic_len = wpa_mic_len(sm->wpa_key_mgmt, sm->pmk_len); |
3b5b7aa8 | 2907 | |
6fc6879b JM |
2908 | /* WPA with IEEE 802.1X: use the derived PMK from EAP |
2909 | * WPA-PSK: iterate through possible PSKs and select the one matching | |
2910 | * the packet */ | |
2911 | for (;;) { | |
e61fea6b JM |
2912 | if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt) && |
2913 | !wpa_key_mgmt_sae(sm->wpa_key_mgmt)) { | |
759fd76b | 2914 | pmk = wpa_auth_get_psk(sm->wpa_auth, sm->addr, |
dbfa691d JM |
2915 | sm->p2p_dev_addr, pmk, &pmk_len, |
2916 | &vlan_id); | |
6fc6879b JM |
2917 | if (pmk == NULL) |
2918 | break; | |
2c502460 | 2919 | psk_found = 1; |
f9854c18 MK |
2920 | #ifdef CONFIG_IEEE80211R_AP |
2921 | if (wpa_key_mgmt_ft_psk(sm->wpa_key_mgmt)) { | |
2922 | os_memcpy(sm->xxkey, pmk, pmk_len); | |
2923 | sm->xxkey_len = pmk_len; | |
2924 | } | |
2925 | #endif /* CONFIG_IEEE80211R_AP */ | |
207976f0 | 2926 | } else { |
6fc6879b | 2927 | pmk = sm->PMK; |
207976f0 JM |
2928 | pmk_len = sm->pmk_len; |
2929 | } | |
6fc6879b | 2930 | |
a40bd06e JM |
2931 | if ((!pmk || !pmk_len) && sm->pmksa) { |
2932 | wpa_printf(MSG_DEBUG, "WPA: Use PMK from PMKSA cache"); | |
2933 | pmk = sm->pmksa->pmk; | |
2934 | pmk_len = sm->pmksa->pmk_len; | |
2935 | } | |
2936 | ||
65a44e84 JM |
2937 | if (wpa_derive_ptk(sm, sm->SNonce, pmk, pmk_len, &PTK, |
2938 | owe_ptk_workaround == 2) < 0) | |
364c064a | 2939 | break; |
6fc6879b | 2940 | |
3b5b7aa8 | 2941 | if (mic_len && |
567da5bb | 2942 | wpa_verify_key_mic(sm->wpa_key_mgmt, pmk_len, &PTK, |
929a2ea5 | 2943 | sm->last_rx_eapol_key, |
6fc6879b | 2944 | sm->last_rx_eapol_key_len) == 0) { |
08dc8efd JM |
2945 | if (sm->PMK != pmk) { |
2946 | os_memcpy(sm->PMK, pmk, pmk_len); | |
2947 | sm->pmk_len = pmk_len; | |
2948 | } | |
6fc6879b JM |
2949 | ok = 1; |
2950 | break; | |
2951 | } | |
2952 | ||
3b5b7aa8 | 2953 | #ifdef CONFIG_FILS |
75c8563e JM |
2954 | if (!mic_len && |
2955 | wpa_aead_decrypt(sm, &PTK, sm->last_rx_eapol_key, | |
2956 | sm->last_rx_eapol_key_len, NULL) == 0) { | |
3b5b7aa8 JM |
2957 | ok = 1; |
2958 | break; | |
2959 | } | |
2960 | #endif /* CONFIG_FILS */ | |
2961 | ||
65a44e84 JM |
2962 | #ifdef CONFIG_OWE |
2963 | if (sm->wpa_key_mgmt == WPA_KEY_MGMT_OWE && pmk_len > 32 && | |
2964 | owe_ptk_workaround == 1) { | |
2965 | wpa_printf(MSG_DEBUG, | |
2966 | "OWE: Try PTK derivation workaround with SHA256"); | |
2967 | owe_ptk_workaround = 2; | |
2968 | continue; | |
2969 | } | |
2970 | #endif /* CONFIG_OWE */ | |
2971 | ||
f5e0a332 JM |
2972 | if (!wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt) || |
2973 | wpa_key_mgmt_sae(sm->wpa_key_mgmt)) | |
6fc6879b JM |
2974 | break; |
2975 | } | |
2976 | ||
2977 | if (!ok) { | |
2978 | wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG, | |
2979 | "invalid MIC in msg 2/4 of 4-Way Handshake"); | |
2c502460 JM |
2980 | if (psk_found) |
2981 | wpa_auth_psk_failure_report(sm->wpa_auth, sm->addr); | |
6fc6879b JM |
2982 | return; |
2983 | } | |
2984 | ||
3b5b7aa8 JM |
2985 | /* |
2986 | * Note: last_rx_eapol_key length fields have already been validated in | |
2987 | * wpa_receive(). | |
2988 | */ | |
2989 | hdr = (struct ieee802_1x_hdr *) sm->last_rx_eapol_key; | |
2990 | key = (struct wpa_eapol_key *) (hdr + 1); | |
2991 | mic = (u8 *) (key + 1); | |
2992 | key_data = mic + mic_len + 2; | |
2993 | key_data_length = WPA_GET_BE16(mic + mic_len); | |
2994 | if (key_data_length > sm->last_rx_eapol_key_len - sizeof(*hdr) - | |
2995 | sizeof(*key) - mic_len - 2) | |
2996 | return; | |
2997 | ||
2998 | if (wpa_parse_kde_ies(key_data, key_data_length, &kde) < 0) { | |
2999 | wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_INFO, | |
3000 | "received EAPOL-Key msg 2/4 with invalid Key Data contents"); | |
3001 | return; | |
3002 | } | |
3003 | if (kde.rsn_ie) { | |
3004 | eapol_key_ie = kde.rsn_ie; | |
3005 | eapol_key_ie_len = kde.rsn_ie_len; | |
3006 | } else if (kde.osen) { | |
3007 | eapol_key_ie = kde.osen; | |
3008 | eapol_key_ie_len = kde.osen_len; | |
3009 | } else { | |
3010 | eapol_key_ie = kde.wpa_ie; | |
3011 | eapol_key_ie_len = kde.wpa_ie_len; | |
3012 | } | |
3013 | ft = sm->wpa == WPA_VERSION_WPA2 && wpa_key_mgmt_ft(sm->wpa_key_mgmt); | |
3014 | if (sm->wpa_ie == NULL || | |
3015 | wpa_compare_rsn_ie(ft, sm->wpa_ie, sm->wpa_ie_len, | |
3016 | eapol_key_ie, eapol_key_ie_len)) { | |
3017 | wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO, | |
3018 | "WPA IE from (Re)AssocReq did not match with msg 2/4"); | |
3019 | if (sm->wpa_ie) { | |
3020 | wpa_hexdump(MSG_DEBUG, "WPA IE in AssocReq", | |
3021 | sm->wpa_ie, sm->wpa_ie_len); | |
3022 | } | |
3023 | wpa_hexdump(MSG_DEBUG, "WPA IE in msg 2/4", | |
3024 | eapol_key_ie, eapol_key_ie_len); | |
3025 | /* MLME-DEAUTHENTICATE.request */ | |
567da5bb JM |
3026 | wpa_sta_disconnect(wpa_auth, sm->addr, |
3027 | WLAN_REASON_PREV_AUTH_NOT_VALID); | |
3b5b7aa8 | 3028 | return; |
74866f53 JM |
3029 | } |
3030 | if ((!sm->rsnxe && kde.rsnxe) || | |
3031 | (sm->rsnxe && !kde.rsnxe) || | |
3032 | (sm->rsnxe && kde.rsnxe && | |
3033 | (sm->rsnxe_len != kde.rsnxe_len || | |
3034 | os_memcmp(sm->rsnxe, kde.rsnxe, sm->rsnxe_len) != 0))) { | |
3035 | wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO, | |
3036 | "RSNXE from (Re)AssocReq did not match the one in EAPOL-Key msg 2/4"); | |
3037 | wpa_hexdump(MSG_DEBUG, "RSNXE in AssocReq", | |
3038 | sm->rsnxe, sm->rsnxe_len); | |
3039 | wpa_hexdump(MSG_DEBUG, "RSNXE in EAPOL-Key msg 2/4", | |
3040 | kde.rsnxe, kde.rsnxe_len); | |
3041 | /* MLME-DEAUTHENTICATE.request */ | |
3042 | wpa_sta_disconnect(wpa_auth, sm->addr, | |
3043 | WLAN_REASON_PREV_AUTH_NOT_VALID); | |
3044 | return; | |
3b5b7aa8 | 3045 | } |
aed61c4e MV |
3046 | #ifdef CONFIG_OCV |
3047 | if (wpa_auth_uses_ocv(sm)) { | |
3048 | struct wpa_channel_info ci; | |
3049 | int tx_chanwidth; | |
3050 | int tx_seg1_idx; | |
3051 | ||
3052 | if (wpa_channel_info(wpa_auth, &ci) != 0) { | |
3053 | wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO, | |
3054 | "Failed to get channel info to validate received OCI in EAPOL-Key 2/4"); | |
3055 | return; | |
3056 | } | |
3057 | ||
3058 | if (get_sta_tx_parameters(sm, | |
3059 | channel_width_to_int(ci.chanwidth), | |
3060 | ci.seg1_idx, &tx_chanwidth, | |
3061 | &tx_seg1_idx) < 0) | |
3062 | return; | |
3063 | ||
3064 | if (ocv_verify_tx_params(kde.oci, kde.oci_len, &ci, | |
3065 | tx_chanwidth, tx_seg1_idx) != 0) { | |
3066 | wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO, | |
3067 | ocv_errorstr); | |
3068 | return; | |
3069 | } | |
3070 | } | |
3071 | #endif /* CONFIG_OCV */ | |
4ec1fd8e | 3072 | #ifdef CONFIG_IEEE80211R_AP |
3b5b7aa8 | 3073 | if (ft && ft_check_msg_2_of_4(wpa_auth, sm, &kde) < 0) { |
567da5bb JM |
3074 | wpa_sta_disconnect(wpa_auth, sm->addr, |
3075 | WLAN_REASON_PREV_AUTH_NOT_VALID); | |
3b5b7aa8 JM |
3076 | return; |
3077 | } | |
4ec1fd8e | 3078 | #endif /* CONFIG_IEEE80211R_AP */ |
3b5b7aa8 JM |
3079 | #ifdef CONFIG_P2P |
3080 | if (kde.ip_addr_req && kde.ip_addr_req[0] && | |
3081 | wpa_auth->ip_pool && WPA_GET_BE32(sm->ip_addr) == 0) { | |
3082 | int idx; | |
3083 | wpa_printf(MSG_DEBUG, | |
3084 | "P2P: IP address requested in EAPOL-Key exchange"); | |
3085 | idx = bitfield_get_first_zero(wpa_auth->ip_pool); | |
3086 | if (idx >= 0) { | |
3087 | u32 start = WPA_GET_BE32(wpa_auth->conf.ip_addr_start); | |
3088 | bitfield_set(wpa_auth->ip_pool, idx); | |
3089 | WPA_PUT_BE32(sm->ip_addr, start + idx); | |
3090 | wpa_printf(MSG_DEBUG, | |
3091 | "P2P: Assigned IP address %u.%u.%u.%u to " | |
3092 | MACSTR, sm->ip_addr[0], sm->ip_addr[1], | |
3093 | sm->ip_addr[2], sm->ip_addr[3], | |
3094 | MAC2STR(sm->addr)); | |
3095 | } | |
3096 | } | |
3097 | #endif /* CONFIG_P2P */ | |
3098 | ||
4ec1fd8e | 3099 | #ifdef CONFIG_IEEE80211R_AP |
26e23750 JM |
3100 | if (sm->wpa == WPA_VERSION_WPA2 && wpa_key_mgmt_ft(sm->wpa_key_mgmt)) { |
3101 | /* | |
3102 | * Verify that PMKR1Name from EAPOL-Key message 2/4 matches | |
3103 | * with the value we derived. | |
3104 | */ | |
870834a1 JM |
3105 | if (os_memcmp_const(sm->sup_pmk_r1_name, sm->pmk_r1_name, |
3106 | WPA_PMK_NAME_LEN) != 0) { | |
26e23750 JM |
3107 | wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG, |
3108 | "PMKR1Name mismatch in FT 4-way " | |
3109 | "handshake"); | |
3110 | wpa_hexdump(MSG_DEBUG, "FT: PMKR1Name from " | |
3111 | "Supplicant", | |
3112 | sm->sup_pmk_r1_name, WPA_PMK_NAME_LEN); | |
3113 | wpa_hexdump(MSG_DEBUG, "FT: Derived PMKR1Name", | |
3114 | sm->pmk_r1_name, WPA_PMK_NAME_LEN); | |
3115 | return; | |
3116 | } | |
3117 | } | |
4ec1fd8e | 3118 | #endif /* CONFIG_IEEE80211R_AP */ |
26e23750 | 3119 | |
dbfa691d JM |
3120 | if (vlan_id && wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt) && |
3121 | wpa_auth_update_vlan(wpa_auth, sm->addr, vlan_id) < 0) { | |
3122 | wpa_sta_disconnect(wpa_auth, sm->addr, | |
3123 | WLAN_REASON_PREV_AUTH_NOT_VALID); | |
3124 | return; | |
3125 | } | |
3126 | ||
e4bf4db9 | 3127 | sm->pending_1_of_4_timeout = 0; |
6fc6879b JM |
3128 | eloop_cancel_timeout(wpa_send_eapol_timeout, sm->wpa_auth, sm); |
3129 | ||
56586197 | 3130 | if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt)) { |
6fc6879b JM |
3131 | /* PSK may have changed from the previous choice, so update |
3132 | * state machine data based on whatever PSK was selected here. | |
3133 | */ | |
3134 | os_memcpy(sm->PMK, pmk, PMK_LEN); | |
207976f0 | 3135 | sm->pmk_len = PMK_LEN; |
6fc6879b JM |
3136 | } |
3137 | ||
3138 | sm->MICVerified = TRUE; | |
3139 | ||
3140 | os_memcpy(&sm->PTK, &PTK, sizeof(PTK)); | |
31bc66e4 | 3141 | forced_memzero(&PTK, sizeof(PTK)); |
6fc6879b JM |
3142 | sm->PTK_valid = TRUE; |
3143 | } | |
3144 | ||
3145 | ||
3146 | SM_STATE(WPA_PTK, PTKCALCNEGOTIATING2) | |
3147 | { | |
3148 | SM_ENTRY_MA(WPA_PTK, PTKCALCNEGOTIATING2, wpa_ptk); | |
3149 | sm->TimeoutCtr = 0; | |
3150 | } | |
3151 | ||
3152 | ||
6fc6879b JM |
3153 | static int ieee80211w_kde_len(struct wpa_state_machine *sm) |
3154 | { | |
16889aff JM |
3155 | size_t len = 0; |
3156 | ||
6fc6879b | 3157 | if (sm->mgmt_frame_prot) { |
16889aff JM |
3158 | len += 2 + RSN_SELECTOR_LEN + WPA_IGTK_KDE_PREFIX_LEN; |
3159 | len += wpa_cipher_key_len(sm->wpa_auth->conf.group_mgmt_cipher); | |
3160 | } | |
3161 | if (sm->mgmt_frame_prot && sm->wpa_auth->conf.beacon_prot) { | |
3162 | len += 2 + RSN_SELECTOR_LEN + WPA_BIGTK_KDE_PREFIX_LEN; | |
3163 | len += wpa_cipher_key_len(sm->wpa_auth->conf.group_mgmt_cipher); | |
6fc6879b JM |
3164 | } |
3165 | ||
16889aff | 3166 | return len; |
6fc6879b JM |
3167 | } |
3168 | ||
3169 | ||
3170 | static u8 * ieee80211w_kde_add(struct wpa_state_machine *sm, u8 *pos) | |
3171 | { | |
3172 | struct wpa_igtk_kde igtk; | |
16889aff | 3173 | struct wpa_bigtk_kde bigtk; |
6fc6879b | 3174 | struct wpa_group *gsm = sm->group; |
03610ad2 | 3175 | u8 rsc[WPA_KEY_RSC_LEN]; |
62e12afc JM |
3176 | struct wpa_auth_config *conf = &sm->wpa_auth->conf; |
3177 | size_t len = wpa_cipher_key_len(conf->group_mgmt_cipher); | |
6fc6879b JM |
3178 | |
3179 | if (!sm->mgmt_frame_prot) | |
3180 | return pos; | |
3181 | ||
3182 | igtk.keyid[0] = gsm->GN_igtk; | |
3183 | igtk.keyid[1] = 0; | |
7b1080da | 3184 | if (gsm->wpa_group_state != WPA_GROUP_SETKEYSDONE || |
03610ad2 | 3185 | wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN_igtk, rsc) < 0) |
6fc6879b | 3186 | os_memset(igtk.pn, 0, sizeof(igtk.pn)); |
03610ad2 JM |
3187 | else |
3188 | os_memcpy(igtk.pn, rsc, sizeof(igtk.pn)); | |
8dd9f9cd | 3189 | os_memcpy(igtk.igtk, gsm->IGTK[gsm->GN_igtk - 4], len); |
62e12afc | 3190 | if (conf->disable_gtk || sm->wpa_key_mgmt == WPA_KEY_MGMT_OSEN) { |
83421850 JM |
3191 | /* |
3192 | * Provide unique random IGTK to each STA to prevent use of | |
3193 | * IGTK in the BSS. | |
3194 | */ | |
8dd9f9cd | 3195 | if (random_get_bytes(igtk.igtk, len) < 0) |
83421850 JM |
3196 | return pos; |
3197 | } | |
6fc6879b | 3198 | pos = wpa_add_kde(pos, RSN_KEY_DATA_IGTK, |
8dd9f9cd JM |
3199 | (const u8 *) &igtk, WPA_IGTK_KDE_PREFIX_LEN + len, |
3200 | NULL, 0); | |
6fc6879b | 3201 | |
62e12afc | 3202 | if (!conf->beacon_prot) |
16889aff JM |
3203 | return pos; |
3204 | ||
3205 | bigtk.keyid[0] = gsm->GN_bigtk; | |
3206 | bigtk.keyid[1] = 0; | |
3207 | if (gsm->wpa_group_state != WPA_GROUP_SETKEYSDONE || | |
3208 | wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN_bigtk, rsc) < 0) | |
3209 | os_memset(bigtk.pn, 0, sizeof(bigtk.pn)); | |
3210 | else | |
3211 | os_memcpy(bigtk.pn, rsc, sizeof(bigtk.pn)); | |
3212 | os_memcpy(bigtk.bigtk, gsm->BIGTK[gsm->GN_bigtk - 6], len); | |
3213 | pos = wpa_add_kde(pos, RSN_KEY_DATA_BIGTK, | |
3214 | (const u8 *) &bigtk, WPA_BIGTK_KDE_PREFIX_LEN + len, | |
3215 | NULL, 0); | |
3216 | ||
6fc6879b JM |
3217 | return pos; |
3218 | } | |
3219 | ||
6fc6879b | 3220 | |
17c2559c MV |
3221 | static int ocv_oci_len(struct wpa_state_machine *sm) |
3222 | { | |
3223 | #ifdef CONFIG_OCV | |
3224 | if (wpa_auth_uses_ocv(sm)) | |
3225 | return OCV_OCI_KDE_LEN; | |
3226 | #endif /* CONFIG_OCV */ | |
3227 | return 0; | |
3228 | } | |
3229 | ||
3230 | static int ocv_oci_add(struct wpa_state_machine *sm, u8 **argpos) | |
3231 | { | |
3232 | #ifdef CONFIG_OCV | |
3233 | struct wpa_channel_info ci; | |
3234 | ||
3235 | if (!wpa_auth_uses_ocv(sm)) | |
3236 | return 0; | |
3237 | ||
3238 | if (wpa_channel_info(sm->wpa_auth, &ci) != 0) { | |
3239 | wpa_printf(MSG_WARNING, | |
3240 | "Failed to get channel info for OCI element"); | |
3241 | return -1; | |
3242 | } | |
3243 | ||
3244 | return ocv_insert_oci_kde(&ci, argpos); | |
3245 | #else /* CONFIG_OCV */ | |
3246 | return 0; | |
3247 | #endif /* CONFIG_OCV */ | |
3248 | } | |
3249 | ||
3250 | ||
9128b672 JM |
3251 | #ifdef CONFIG_TESTING_OPTIONS |
3252 | static u8 * replace_ie(const char *name, const u8 *old_buf, size_t *len, u8 eid, | |
3253 | const u8 *ie, size_t ie_len) | |
3254 | { | |
3255 | const u8 *elem; | |
3256 | u8 *buf; | |
3257 | ||
3258 | wpa_printf(MSG_DEBUG, "TESTING: %s EAPOL override", name); | |
3259 | wpa_hexdump(MSG_DEBUG, "TESTING: wpa_ie before override", | |
3260 | old_buf, *len); | |
3261 | buf = os_malloc(*len + ie_len); | |
3262 | if (!buf) | |
3263 | return NULL; | |
3264 | os_memcpy(buf, old_buf, *len); | |
3265 | elem = get_ie(buf, *len, eid); | |
3266 | if (elem) { | |
3267 | u8 elem_len = 2 + elem[1]; | |
3268 | ||
3269 | os_memmove((void *) elem, elem + elem_len, | |
3270 | *len - (elem - buf) - elem_len); | |
3271 | *len -= elem_len; | |
3272 | } | |
3273 | os_memcpy(buf + *len, ie, ie_len); | |
3274 | *len += ie_len; | |
3275 | wpa_hexdump(MSG_DEBUG, "TESTING: wpa_ie after EAPOL override", | |
3276 | buf, *len); | |
3277 | ||
3278 | return buf; | |
3279 | } | |
3280 | #endif /* CONFIG_TESTING_OPTIONS */ | |
3281 | ||
3282 | ||
6fc6879b JM |
3283 | SM_STATE(WPA_PTK, PTKINITNEGOTIATING) |
3284 | { | |
f2c4b44b | 3285 | u8 rsc[WPA_KEY_RSC_LEN], *_rsc, *gtk, *kde = NULL, *pos, dummy_gtk[32]; |
9128b672 | 3286 | size_t gtk_len, kde_len, wpa_ie_len; |
6fc6879b JM |
3287 | struct wpa_group *gsm = sm->group; |
3288 | u8 *wpa_ie; | |
9128b672 | 3289 | int secure, gtkidx, encr = 0; |
4d64fd37 | 3290 | u8 *wpa_ie_buf = NULL, *wpa_ie_buf2 = NULL; |
862aac1f | 3291 | u8 hdr[2]; |
82cc0b0c | 3292 | struct wpa_auth_config *conf = &sm->wpa_auth->conf; |
6fc6879b JM |
3293 | |
3294 | SM_ENTRY_MA(WPA_PTK, PTKINITNEGOTIATING, wpa_ptk); | |
3295 | sm->TimeoutEvt = FALSE; | |
bae61562 JM |
3296 | |
3297 | sm->TimeoutCtr++; | |
62e12afc | 3298 | if (conf->wpa_disable_eapol_key_retries && sm->TimeoutCtr > 1) { |
6f234c1e JM |
3299 | /* Do not allow retransmission of EAPOL-Key msg 3/4 */ |
3300 | return; | |
3301 | } | |
62e12afc | 3302 | if (sm->TimeoutCtr > conf->wpa_pairwise_update_count) { |
bae61562 JM |
3303 | /* No point in sending the EAPOL-Key - we will disconnect |
3304 | * immediately following this. */ | |
3305 | return; | |
3306 | } | |
3307 | ||
86dfabb8 | 3308 | /* Send EAPOL(1, 1, 1, Pair, P, RSC, ANonce, MIC(PTK), RSNIE, [MDIE], |
16889aff | 3309 | GTK[GN], IGTK, [BIGTK], [FTIE], [TIE * 2]) |
6fc6879b JM |
3310 | */ |
3311 | os_memset(rsc, 0, WPA_KEY_RSC_LEN); | |
3312 | wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN, rsc); | |
86dfabb8 | 3313 | /* If FT is used, wpa_auth->wpa_ie includes both RSNIE and MDIE */ |
6fc6879b JM |
3314 | wpa_ie = sm->wpa_auth->wpa_ie; |
3315 | wpa_ie_len = sm->wpa_auth->wpa_ie_len; | |
62e12afc | 3316 | if (sm->wpa == WPA_VERSION_WPA && (conf->wpa & WPA_PROTO_RSN) && |
9128b672 | 3317 | wpa_ie_len > wpa_ie[1] + 2U && wpa_ie[0] == WLAN_EID_RSN) { |
774d4145 | 3318 | /* WPA-only STA, remove RSN IE and possible MDIE */ |
6fc6879b | 3319 | wpa_ie = wpa_ie + wpa_ie[1] + 2; |
774d4145 JM |
3320 | if (wpa_ie[0] == WLAN_EID_MOBILITY_DOMAIN) |
3321 | wpa_ie = wpa_ie + wpa_ie[1] + 2; | |
6fc6879b JM |
3322 | wpa_ie_len = wpa_ie[1] + 2; |
3323 | } | |
f2c4b44b | 3324 | #ifdef CONFIG_TESTING_OPTIONS |
62e12afc | 3325 | if (conf->rsne_override_eapol_set) { |
4d64fd37 JM |
3326 | wpa_ie_buf2 = replace_ie( |
3327 | "RSNE", wpa_ie, &wpa_ie_len, WLAN_EID_RSN, | |
62e12afc JM |
3328 | conf->rsne_override_eapol, |
3329 | conf->rsne_override_eapol_len); | |
4d64fd37 JM |
3330 | if (!wpa_ie_buf2) |
3331 | goto done; | |
3332 | wpa_ie = wpa_ie_buf2; | |
3333 | } | |
62e12afc | 3334 | if (conf->rsnxe_override_eapol_set) { |
9128b672 JM |
3335 | wpa_ie_buf = replace_ie( |
3336 | "RSNXE", wpa_ie, &wpa_ie_len, WLAN_EID_RSNX, | |
62e12afc JM |
3337 | conf->rsnxe_override_eapol, |
3338 | conf->rsnxe_override_eapol_len); | |
f2c4b44b | 3339 | if (!wpa_ie_buf) |
9128b672 | 3340 | goto done; |
f2c4b44b | 3341 | wpa_ie = wpa_ie_buf; |
f2c4b44b JM |
3342 | } |
3343 | #endif /* CONFIG_TESTING_OPTIONS */ | |
6fc6879b JM |
3344 | wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG, |
3345 | "sending 3/4 msg of 4-Way Handshake"); | |
3346 | if (sm->wpa == WPA_VERSION_WPA2) { | |
862aac1f AW |
3347 | if (sm->use_ext_key_id && sm->TimeoutCtr == 1 && |
3348 | wpa_auth_set_key(sm->wpa_auth, 0, | |
3349 | wpa_cipher_to_alg(sm->pairwise), | |
3350 | sm->addr, | |
3351 | sm->keyidx_active, sm->PTK.tk, | |
3352 | wpa_cipher_key_len(sm->pairwise), | |
3353 | KEY_FLAG_PAIRWISE_RX)) { | |
3354 | wpa_sta_disconnect(sm->wpa_auth, sm->addr, | |
3355 | WLAN_REASON_PREV_AUTH_NOT_VALID); | |
3356 | return; | |
3357 | } | |
3358 | ||
6fc6879b JM |
3359 | /* WPA2 send GTK in the 4-way handshake */ |
3360 | secure = 1; | |
3361 | gtk = gsm->GTK[gsm->GN - 1]; | |
3362 | gtk_len = gsm->GTK_len; | |
62e12afc | 3363 | if (conf->disable_gtk || |
8d660a4b | 3364 | sm->wpa_key_mgmt == WPA_KEY_MGMT_OSEN) { |
83421850 JM |
3365 | /* |
3366 | * Provide unique random GTK to each STA to prevent use | |
3367 | * of GTK in the BSS. | |
3368 | */ | |
3369 | if (random_get_bytes(dummy_gtk, gtk_len) < 0) | |
f2c4b44b | 3370 | goto done; |
83421850 JM |
3371 | gtk = dummy_gtk; |
3372 | } | |
7a4b01c8 | 3373 | gtkidx = gsm->GN; |
6fc6879b JM |
3374 | _rsc = rsc; |
3375 | encr = 1; | |
3376 | } else { | |
3377 | /* WPA does not include GTK in msg 3/4 */ | |
3378 | secure = 0; | |
3379 | gtk = NULL; | |
3380 | gtk_len = 0; | |
6fc6879b | 3381 | _rsc = NULL; |
0d442aff JM |
3382 | if (sm->rx_eapol_key_secure) { |
3383 | /* | |
3384 | * It looks like Windows 7 supplicant tries to use | |
3385 | * Secure bit in msg 2/4 after having reported Michael | |
3386 | * MIC failure and it then rejects the 4-way handshake | |
3387 | * if msg 3/4 does not set Secure bit. Work around this | |
3388 | * by setting the Secure bit here even in the case of | |
3389 | * WPA if the supplicant used it first. | |
3390 | */ | |
3391 | wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG, | |
4740d5b9 JM |
3392 | "STA used Secure bit in WPA msg 2/4 - " |
3393 | "set Secure for 3/4 as workaround"); | |
0d442aff JM |
3394 | secure = 1; |
3395 | } | |
6fc6879b JM |
3396 | } |
3397 | ||
17c2559c | 3398 | kde_len = wpa_ie_len + ieee80211w_kde_len(sm) + ocv_oci_len(sm); |
862aac1f AW |
3399 | |
3400 | if (sm->use_ext_key_id) | |
3401 | kde_len += 2 + RSN_SELECTOR_LEN + 2; | |
3402 | ||
6fc6879b JM |
3403 | if (gtk) |
3404 | kde_len += 2 + RSN_SELECTOR_LEN + 2 + gtk_len; | |
4ec1fd8e | 3405 | #ifdef CONFIG_IEEE80211R_AP |
86dfabb8 JM |
3406 | if (wpa_key_mgmt_ft(sm->wpa_key_mgmt)) { |
3407 | kde_len += 2 + PMKID_LEN; /* PMKR1Name into RSN IE */ | |
3408 | kde_len += 300; /* FTIE + 2 * TIE */ | |
3409 | } | |
4ec1fd8e | 3410 | #endif /* CONFIG_IEEE80211R_AP */ |
25ef8529 JM |
3411 | #ifdef CONFIG_P2P |
3412 | if (WPA_GET_BE32(sm->ip_addr) > 0) | |
3413 | kde_len += 2 + RSN_SELECTOR_LEN + 3 * 4; | |
3414 | #endif /* CONFIG_P2P */ | |
82cc0b0c JM |
3415 | |
3416 | if (conf->transition_disable) | |
3417 | kde_len += 2 + RSN_SELECTOR_LEN + 1; | |
3418 | ||
6fc6879b JM |
3419 | kde = os_malloc(kde_len); |
3420 | if (kde == NULL) | |
f2c4b44b | 3421 | goto done; |
6fc6879b JM |
3422 | |
3423 | pos = kde; | |
3424 | os_memcpy(pos, wpa_ie, wpa_ie_len); | |
3425 | pos += wpa_ie_len; | |
4ec1fd8e | 3426 | #ifdef CONFIG_IEEE80211R_AP |
26e23750 | 3427 | if (wpa_key_mgmt_ft(sm->wpa_key_mgmt)) { |
59e78c24 JM |
3428 | int res; |
3429 | size_t elen; | |
3430 | ||
3431 | elen = pos - kde; | |
3432 | res = wpa_insert_pmkid(kde, &elen, sm->pmk_r1_name); | |
26e23750 JM |
3433 | if (res < 0) { |
3434 | wpa_printf(MSG_ERROR, "FT: Failed to insert " | |
3435 | "PMKR1Name into RSN IE in EAPOL-Key data"); | |
f2c4b44b | 3436 | goto done; |
26e23750 | 3437 | } |
59e78c24 JM |
3438 | pos -= wpa_ie_len; |
3439 | pos += elen; | |
26e23750 | 3440 | } |
4ec1fd8e | 3441 | #endif /* CONFIG_IEEE80211R_AP */ |
862aac1f AW |
3442 | hdr[1] = 0; |
3443 | ||
3444 | if (sm->use_ext_key_id) { | |
3445 | hdr[0] = sm->keyidx_active & 0x01; | |
3446 | pos = wpa_add_kde(pos, RSN_KEY_DATA_KEYID, hdr, 2, NULL, 0); | |
3447 | } | |
3448 | ||
6fc6879b | 3449 | if (gtk) { |
7a4b01c8 | 3450 | hdr[0] = gtkidx & 0x03; |
6fc6879b JM |
3451 | pos = wpa_add_kde(pos, RSN_KEY_DATA_GROUPKEY, hdr, 2, |
3452 | gtk, gtk_len); | |
3453 | } | |
3454 | pos = ieee80211w_kde_add(sm, pos); | |
f2c4b44b JM |
3455 | if (ocv_oci_add(sm, &pos) < 0) |
3456 | goto done; | |
6fc6879b | 3457 | |
4ec1fd8e | 3458 | #ifdef CONFIG_IEEE80211R_AP |
86dfabb8 JM |
3459 | if (wpa_key_mgmt_ft(sm->wpa_key_mgmt)) { |
3460 | int res; | |
86dfabb8 | 3461 | |
9257610a JM |
3462 | if (sm->assoc_resp_ftie && |
3463 | kde + kde_len - pos >= 2 + sm->assoc_resp_ftie[1]) { | |
3464 | os_memcpy(pos, sm->assoc_resp_ftie, | |
3465 | 2 + sm->assoc_resp_ftie[1]); | |
3466 | res = 2 + sm->assoc_resp_ftie[1]; | |
3467 | } else { | |
a7968ea5 JM |
3468 | int use_sha384 = wpa_key_mgmt_sha384(sm->wpa_key_mgmt); |
3469 | ||
3470 | res = wpa_write_ftie(conf, use_sha384, | |
3471 | conf->r0_key_holder, | |
9257610a JM |
3472 | conf->r0_key_holder_len, |
3473 | NULL, NULL, pos, | |
3474 | kde + kde_len - pos, | |
51d1924b | 3475 | NULL, 0, 0); |
9257610a | 3476 | } |
86dfabb8 JM |
3477 | if (res < 0) { |
3478 | wpa_printf(MSG_ERROR, "FT: Failed to insert FTIE " | |
3479 | "into EAPOL-Key Key Data"); | |
f2c4b44b | 3480 | goto done; |
86dfabb8 JM |
3481 | } |
3482 | pos += res; | |
3483 | ||
3484 | /* TIE[ReassociationDeadline] (TU) */ | |
3485 | *pos++ = WLAN_EID_TIMEOUT_INTERVAL; | |
3486 | *pos++ = 5; | |
3487 | *pos++ = WLAN_TIMEOUT_REASSOC_DEADLINE; | |
3488 | WPA_PUT_LE32(pos, conf->reassociation_deadline); | |
3489 | pos += 4; | |
3490 | ||
3491 | /* TIE[KeyLifetime] (seconds) */ | |
3492 | *pos++ = WLAN_EID_TIMEOUT_INTERVAL; | |
3493 | *pos++ = 5; | |
3494 | *pos++ = WLAN_TIMEOUT_KEY_LIFETIME; | |
83fe4bd3 | 3495 | WPA_PUT_LE32(pos, conf->r0_key_lifetime); |
86dfabb8 JM |
3496 | pos += 4; |
3497 | } | |
4ec1fd8e | 3498 | #endif /* CONFIG_IEEE80211R_AP */ |
25ef8529 JM |
3499 | #ifdef CONFIG_P2P |
3500 | if (WPA_GET_BE32(sm->ip_addr) > 0) { | |
3501 | u8 addr[3 * 4]; | |
3502 | os_memcpy(addr, sm->ip_addr, 4); | |
62e12afc JM |
3503 | os_memcpy(addr + 4, conf->ip_addr_mask, 4); |
3504 | os_memcpy(addr + 8, conf->ip_addr_go, 4); | |
25ef8529 JM |
3505 | pos = wpa_add_kde(pos, WFA_KEY_DATA_IP_ADDR_ALLOC, |
3506 | addr, sizeof(addr), NULL, 0); | |
3507 | } | |
3508 | #endif /* CONFIG_P2P */ | |
86dfabb8 | 3509 | |
82cc0b0c JM |
3510 | if (conf->transition_disable) |
3511 | pos = wpa_add_kde(pos, WFA_KEY_DATA_TRANSITION_DISABLE, | |
3512 | &conf->transition_disable, 1, NULL, 0); | |
3513 | ||
6fc6879b | 3514 | wpa_send_eapol(sm->wpa_auth, sm, |
b729fd8d | 3515 | (secure ? WPA_KEY_INFO_SECURE : 0) | |
567da5bb JM |
3516 | (wpa_mic_len(sm->wpa_key_mgmt, sm->pmk_len) ? |
3517 | WPA_KEY_INFO_MIC : 0) | | |
6fc6879b JM |
3518 | WPA_KEY_INFO_ACK | WPA_KEY_INFO_INSTALL | |
3519 | WPA_KEY_INFO_KEY_TYPE, | |
7a4b01c8 | 3520 | _rsc, sm->ANonce, kde, pos - kde, 0, encr); |
f2c4b44b | 3521 | done: |
6fc6879b | 3522 | os_free(kde); |
f2c4b44b | 3523 | os_free(wpa_ie_buf); |
4d64fd37 | 3524 | os_free(wpa_ie_buf2); |
6fc6879b JM |
3525 | } |
3526 | ||
3527 | ||
3528 | SM_STATE(WPA_PTK, PTKINITDONE) | |
3529 | { | |
3530 | SM_ENTRY_MA(WPA_PTK, PTKINITDONE, wpa_ptk); | |
3531 | sm->EAPOLKeyReceived = FALSE; | |
3532 | if (sm->Pair) { | |
c3550295 JM |
3533 | enum wpa_alg alg = wpa_cipher_to_alg(sm->pairwise); |
3534 | int klen = wpa_cipher_key_len(sm->pairwise); | |
862aac1f AW |
3535 | int res; |
3536 | ||
3537 | if (sm->use_ext_key_id) | |
3538 | res = wpa_auth_set_key(sm->wpa_auth, 0, 0, sm->addr, | |
3539 | sm->keyidx_active, NULL, 0, | |
3540 | KEY_FLAG_PAIRWISE_RX_TX_MODIFY); | |
3541 | else | |
3542 | res = wpa_auth_set_key(sm->wpa_auth, 0, alg, sm->addr, | |
3543 | 0, sm->PTK.tk, klen, | |
3544 | KEY_FLAG_PAIRWISE_RX_TX); | |
3545 | if (res) { | |
567da5bb JM |
3546 | wpa_sta_disconnect(sm->wpa_auth, sm->addr, |
3547 | WLAN_REASON_PREV_AUTH_NOT_VALID); | |
6fc6879b JM |
3548 | return; |
3549 | } | |
3550 | /* FIX: MLME-SetProtection.Request(TA, Tx_Rx) */ | |
3551 | sm->pairwise_set = TRUE; | |
3552 | ||
a40bd06e | 3553 | wpa_auth_set_ptk_rekey_timer(sm); |
581a8cde | 3554 | |
a1ea1b45 | 3555 | if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt) || |
567da5bb | 3556 | sm->wpa_key_mgmt == WPA_KEY_MGMT_DPP || |
a1ea1b45 | 3557 | sm->wpa_key_mgmt == WPA_KEY_MGMT_OWE) { |
6fc6879b JM |
3558 | wpa_auth_set_eapol(sm->wpa_auth, sm->addr, |
3559 | WPA_EAPOL_authorized, 1); | |
3560 | } | |
3561 | } | |
3562 | ||
3563 | if (0 /* IBSS == TRUE */) { | |
3564 | sm->keycount++; | |
3565 | if (sm->keycount == 2) { | |
3566 | wpa_auth_set_eapol(sm->wpa_auth, sm->addr, | |
3567 | WPA_EAPOL_portValid, 1); | |
3568 | } | |
3569 | } else { | |
3570 | wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portValid, | |
3571 | 1); | |
3572 | } | |
3573 | wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_keyAvailable, 0); | |
3574 | wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_keyDone, 1); | |
3575 | if (sm->wpa == WPA_VERSION_WPA) | |
3576 | sm->PInitAKeys = TRUE; | |
3577 | else | |
3578 | sm->has_GTK = TRUE; | |
3579 | wpa_auth_vlogger(sm->wpa_auth, sm->addr, LOGGER_INFO, | |
3580 | "pairwise key handshake completed (%s)", | |
3581 | sm->wpa == WPA_VERSION_WPA ? "WPA" : "RSN"); | |
3582 | ||
4ec1fd8e | 3583 | #ifdef CONFIG_IEEE80211R_AP |
6fc6879b | 3584 | wpa_ft_push_pmk_r1(sm->wpa_auth, sm->addr); |
4ec1fd8e | 3585 | #endif /* CONFIG_IEEE80211R_AP */ |
6fc6879b JM |
3586 | } |
3587 | ||
3588 | ||
3589 | SM_STEP(WPA_PTK) | |
3590 | { | |
3591 | struct wpa_authenticator *wpa_auth = sm->wpa_auth; | |
62e12afc | 3592 | struct wpa_auth_config *conf = &wpa_auth->conf; |
6fc6879b JM |
3593 | |
3594 | if (sm->Init) | |
3595 | SM_ENTER(WPA_PTK, INITIALIZE); | |
3596 | else if (sm->Disconnect | |
1aae01fc BG |
3597 | /* || FIX: dot11RSNAConfigSALifetime timeout */) { |
3598 | wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG, | |
3599 | "WPA_PTK: sm->Disconnect"); | |
6fc6879b | 3600 | SM_ENTER(WPA_PTK, DISCONNECT); |
1aae01fc | 3601 | } |
6fc6879b JM |
3602 | else if (sm->DeauthenticationRequest) |
3603 | SM_ENTER(WPA_PTK, DISCONNECTED); | |
3604 | else if (sm->AuthenticationRequest) | |
3605 | SM_ENTER(WPA_PTK, AUTHENTICATION); | |
3606 | else if (sm->ReAuthenticationRequest) | |
3607 | SM_ENTER(WPA_PTK, AUTHENTICATION2); | |
0adc9b28 JM |
3608 | else if (sm->PTKRequest) { |
3609 | if (wpa_auth_sm_ptk_update(sm) < 0) | |
3610 | SM_ENTER(WPA_PTK, DISCONNECTED); | |
3611 | else | |
3612 | SM_ENTER(WPA_PTK, PTKSTART); | |
3613 | } else switch (sm->wpa_ptk_state) { | |
6fc6879b JM |
3614 | case WPA_PTK_INITIALIZE: |
3615 | break; | |
3616 | case WPA_PTK_DISCONNECT: | |
3617 | SM_ENTER(WPA_PTK, DISCONNECTED); | |
3618 | break; | |
3619 | case WPA_PTK_DISCONNECTED: | |
3620 | SM_ENTER(WPA_PTK, INITIALIZE); | |
3621 | break; | |
3622 | case WPA_PTK_AUTHENTICATION: | |
3623 | SM_ENTER(WPA_PTK, AUTHENTICATION2); | |
3624 | break; | |
3625 | case WPA_PTK_AUTHENTICATION2: | |
56586197 | 3626 | if (wpa_key_mgmt_wpa_ieee8021x(sm->wpa_key_mgmt) && |
62e12afc | 3627 | wpa_auth_get_eapol(wpa_auth, sm->addr, |
6fc6879b JM |
3628 | WPA_EAPOL_keyRun) > 0) |
3629 | SM_ENTER(WPA_PTK, INITPMK); | |
a1ea1b45 JM |
3630 | else if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt) || |
3631 | sm->wpa_key_mgmt == WPA_KEY_MGMT_OWE | |
6fc6879b JM |
3632 | /* FIX: && 802.1X::keyRun */) |
3633 | SM_ENTER(WPA_PTK, INITPSK); | |
567da5bb JM |
3634 | else if (sm->wpa_key_mgmt == WPA_KEY_MGMT_DPP) |
3635 | SM_ENTER(WPA_PTK, INITPMK); | |
6fc6879b JM |
3636 | break; |
3637 | case WPA_PTK_INITPMK: | |
62e12afc | 3638 | if (wpa_auth_get_eapol(wpa_auth, sm->addr, |
567da5bb | 3639 | WPA_EAPOL_keyAvailable) > 0) { |
6fc6879b | 3640 | SM_ENTER(WPA_PTK, PTKSTART); |
567da5bb JM |
3641 | #ifdef CONFIG_DPP |
3642 | } else if (sm->wpa_key_mgmt == WPA_KEY_MGMT_DPP && sm->pmksa) { | |
3643 | SM_ENTER(WPA_PTK, PTKSTART); | |
3644 | #endif /* CONFIG_DPP */ | |
3645 | } else { | |
6fc6879b | 3646 | wpa_auth->dot11RSNA4WayHandshakeFailures++; |
62e12afc | 3647 | wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO, |
1aae01fc | 3648 | "INITPMK - keyAvailable = false"); |
6fc6879b JM |
3649 | SM_ENTER(WPA_PTK, DISCONNECT); |
3650 | } | |
3651 | break; | |
3652 | case WPA_PTK_INITPSK: | |
62e12afc | 3653 | if (wpa_auth_get_psk(wpa_auth, sm->addr, sm->p2p_dev_addr, |
dbfa691d | 3654 | NULL, NULL, NULL)) { |
6fc6879b | 3655 | SM_ENTER(WPA_PTK, PTKSTART); |
e61fea6b JM |
3656 | #ifdef CONFIG_SAE |
3657 | } else if (wpa_auth_uses_sae(sm) && sm->pmksa) { | |
3658 | SM_ENTER(WPA_PTK, PTKSTART); | |
3659 | #endif /* CONFIG_SAE */ | |
3660 | } else { | |
62e12afc | 3661 | wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO, |
6fc6879b JM |
3662 | "no PSK configured for the STA"); |
3663 | wpa_auth->dot11RSNA4WayHandshakeFailures++; | |
3664 | SM_ENTER(WPA_PTK, DISCONNECT); | |
3665 | } | |
3666 | break; | |
3667 | case WPA_PTK_PTKSTART: | |
3668 | if (sm->EAPOLKeyReceived && !sm->EAPOLKeyRequest && | |
3669 | sm->EAPOLKeyPairwise) | |
3670 | SM_ENTER(WPA_PTK, PTKCALCNEGOTIATING); | |
62e12afc | 3671 | else if (sm->TimeoutCtr > conf->wpa_pairwise_update_count) { |
6fc6879b | 3672 | wpa_auth->dot11RSNA4WayHandshakeFailures++; |
62e12afc JM |
3673 | wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_DEBUG, |
3674 | "PTKSTART: Retry limit %u reached", | |
3675 | conf->wpa_pairwise_update_count); | |
6fc6879b JM |
3676 | SM_ENTER(WPA_PTK, DISCONNECT); |
3677 | } else if (sm->TimeoutEvt) | |
3678 | SM_ENTER(WPA_PTK, PTKSTART); | |
3679 | break; | |
3680 | case WPA_PTK_PTKCALCNEGOTIATING: | |
3681 | if (sm->MICVerified) | |
3682 | SM_ENTER(WPA_PTK, PTKCALCNEGOTIATING2); | |
3683 | else if (sm->EAPOLKeyReceived && !sm->EAPOLKeyRequest && | |
3684 | sm->EAPOLKeyPairwise) | |
3685 | SM_ENTER(WPA_PTK, PTKCALCNEGOTIATING); | |
3686 | else if (sm->TimeoutEvt) | |
3687 | SM_ENTER(WPA_PTK, PTKSTART); | |
3688 | break; | |
3689 | case WPA_PTK_PTKCALCNEGOTIATING2: | |
3690 | SM_ENTER(WPA_PTK, PTKINITNEGOTIATING); | |
3691 | break; | |
3692 | case WPA_PTK_PTKINITNEGOTIATING: | |
68921e24 JM |
3693 | if (sm->update_snonce) |
3694 | SM_ENTER(WPA_PTK, PTKCALCNEGOTIATING); | |
3695 | else if (sm->EAPOLKeyReceived && !sm->EAPOLKeyRequest && | |
3696 | sm->EAPOLKeyPairwise && sm->MICVerified) | |
6fc6879b JM |
3697 | SM_ENTER(WPA_PTK, PTKINITDONE); |
3698 | else if (sm->TimeoutCtr > | |
62e12afc JM |
3699 | conf->wpa_pairwise_update_count || |
3700 | (conf->wpa_disable_eapol_key_retries && | |
6f234c1e | 3701 | sm->TimeoutCtr > 1)) { |
6fc6879b | 3702 | wpa_auth->dot11RSNA4WayHandshakeFailures++; |
62e12afc JM |
3703 | wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_DEBUG, |
3704 | "PTKINITNEGOTIATING: Retry limit %u reached", | |
3705 | conf->wpa_pairwise_update_count); | |
6fc6879b JM |
3706 | SM_ENTER(WPA_PTK, DISCONNECT); |
3707 | } else if (sm->TimeoutEvt) | |
3708 | SM_ENTER(WPA_PTK, PTKINITNEGOTIATING); | |
3709 | break; | |
3710 | case WPA_PTK_PTKINITDONE: | |
3711 | break; | |
3712 | } | |
3713 | } | |
3714 | ||
3715 | ||
3716 | SM_STATE(WPA_PTK_GROUP, IDLE) | |
3717 | { | |
3718 | SM_ENTRY_MA(WPA_PTK_GROUP, IDLE, wpa_ptk_group); | |
3719 | if (sm->Init) { | |
3720 | /* Init flag is not cleared here, so avoid busy | |
3721 | * loop by claiming nothing changed. */ | |
3722 | sm->changed = FALSE; | |
3723 | } | |
3724 | sm->GTimeoutCtr = 0; | |
3725 | } | |
3726 | ||
3727 | ||
3728 | SM_STATE(WPA_PTK_GROUP, REKEYNEGOTIATING) | |
3729 | { | |
3730 | u8 rsc[WPA_KEY_RSC_LEN]; | |
3731 | struct wpa_group *gsm = sm->group; | |
e41e4f9e JM |
3732 | const u8 *kde; |
3733 | u8 *kde_buf = NULL, *pos, hdr[2]; | |
6fc6879b | 3734 | size_t kde_len; |
83421850 | 3735 | u8 *gtk, dummy_gtk[32]; |
62e12afc | 3736 | struct wpa_auth_config *conf = &sm->wpa_auth->conf; |
6fc6879b JM |
3737 | |
3738 | SM_ENTRY_MA(WPA_PTK_GROUP, REKEYNEGOTIATING, wpa_ptk_group); | |
bae61562 JM |
3739 | |
3740 | sm->GTimeoutCtr++; | |
62e12afc | 3741 | if (conf->wpa_disable_eapol_key_retries && sm->GTimeoutCtr > 1) { |
6f234c1e JM |
3742 | /* Do not allow retransmission of EAPOL-Key group msg 1/2 */ |
3743 | return; | |
3744 | } | |
62e12afc | 3745 | if (sm->GTimeoutCtr > conf->wpa_group_update_count) { |
bae61562 JM |
3746 | /* No point in sending the EAPOL-Key - we will disconnect |
3747 | * immediately following this. */ | |
3748 | return; | |
3749 | } | |
3750 | ||
6fc6879b JM |
3751 | if (sm->wpa == WPA_VERSION_WPA) |
3752 | sm->PInitAKeys = FALSE; | |
3753 | sm->TimeoutEvt = FALSE; | |
3754 | /* Send EAPOL(1, 1, 1, !Pair, G, RSC, GNonce, MIC(PTK), GTK[GN]) */ | |
3755 | os_memset(rsc, 0, WPA_KEY_RSC_LEN); | |
3756 | if (gsm->wpa_group_state == WPA_GROUP_SETKEYSDONE) | |
3757 | wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN, rsc); | |
3758 | wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG, | |
3759 | "sending 1/2 msg of Group Key Handshake"); | |
3760 | ||
83421850 | 3761 | gtk = gsm->GTK[gsm->GN - 1]; |
62e12afc | 3762 | if (conf->disable_gtk || sm->wpa_key_mgmt == WPA_KEY_MGMT_OSEN) { |
83421850 JM |
3763 | /* |
3764 | * Provide unique random GTK to each STA to prevent use | |
3765 | * of GTK in the BSS. | |
3766 | */ | |
3767 | if (random_get_bytes(dummy_gtk, gsm->GTK_len) < 0) | |
3768 | return; | |
3769 | gtk = dummy_gtk; | |
3770 | } | |
6fc6879b JM |
3771 | if (sm->wpa == WPA_VERSION_WPA2) { |
3772 | kde_len = 2 + RSN_SELECTOR_LEN + 2 + gsm->GTK_len + | |
17c2559c | 3773 | ieee80211w_kde_len(sm) + ocv_oci_len(sm); |
e41e4f9e JM |
3774 | kde_buf = os_malloc(kde_len); |
3775 | if (kde_buf == NULL) | |
6fc6879b JM |
3776 | return; |
3777 | ||
e41e4f9e | 3778 | kde = pos = kde_buf; |
6fc6879b JM |
3779 | hdr[0] = gsm->GN & 0x03; |
3780 | hdr[1] = 0; | |
3781 | pos = wpa_add_kde(pos, RSN_KEY_DATA_GROUPKEY, hdr, 2, | |
83421850 | 3782 | gtk, gsm->GTK_len); |
6fc6879b | 3783 | pos = ieee80211w_kde_add(sm, pos); |
17c2559c MV |
3784 | if (ocv_oci_add(sm, &pos) < 0) { |
3785 | os_free(kde_buf); | |
3786 | return; | |
3787 | } | |
e41e4f9e | 3788 | kde_len = pos - kde; |
6fc6879b | 3789 | } else { |
83421850 | 3790 | kde = gtk; |
e41e4f9e | 3791 | kde_len = gsm->GTK_len; |
6fc6879b JM |
3792 | } |
3793 | ||
3794 | wpa_send_eapol(sm->wpa_auth, sm, | |
b729fd8d | 3795 | WPA_KEY_INFO_SECURE | |
567da5bb JM |
3796 | (wpa_mic_len(sm->wpa_key_mgmt, sm->pmk_len) ? |
3797 | WPA_KEY_INFO_MIC : 0) | | |
6fc6879b JM |
3798 | WPA_KEY_INFO_ACK | |
3799 | (!sm->Pair ? WPA_KEY_INFO_INSTALL : 0), | |
b0fb2be7 | 3800 | rsc, NULL, kde, kde_len, gsm->GN, 1); |
e41e4f9e JM |
3801 | |
3802 | os_free(kde_buf); | |
6fc6879b JM |
3803 | } |
3804 | ||
3805 | ||
3806 | SM_STATE(WPA_PTK_GROUP, REKEYESTABLISHED) | |
3807 | { | |
aed61c4e | 3808 | struct wpa_authenticator *wpa_auth = sm->wpa_auth; |
62e12afc | 3809 | #ifdef CONFIG_OCV |
aed61c4e MV |
3810 | const u8 *key_data, *mic; |
3811 | struct ieee802_1x_hdr *hdr; | |
3812 | struct wpa_eapol_key *key; | |
3813 | struct wpa_eapol_ie_parse kde; | |
3814 | size_t mic_len; | |
3815 | u16 key_data_length; | |
3816 | #endif /* CONFIG_OCV */ | |
3817 | ||
6fc6879b JM |
3818 | SM_ENTRY_MA(WPA_PTK_GROUP, REKEYESTABLISHED, wpa_ptk_group); |
3819 | sm->EAPOLKeyReceived = FALSE; | |
aed61c4e MV |
3820 | |
3821 | #ifdef CONFIG_OCV | |
3822 | mic_len = wpa_mic_len(sm->wpa_key_mgmt, sm->pmk_len); | |
3823 | ||
3824 | /* | |
3825 | * Note: last_rx_eapol_key length fields have already been validated in | |
3826 | * wpa_receive(). | |
3827 | */ | |
3828 | hdr = (struct ieee802_1x_hdr *) sm->last_rx_eapol_key; | |
3829 | key = (struct wpa_eapol_key *) (hdr + 1); | |
3830 | mic = (u8 *) (key + 1); | |
3831 | key_data = mic + mic_len + 2; | |
3832 | key_data_length = WPA_GET_BE16(mic + mic_len); | |
3833 | if (key_data_length > sm->last_rx_eapol_key_len - sizeof(*hdr) - | |
3834 | sizeof(*key) - mic_len - 2) | |
3835 | return; | |
3836 | ||
3837 | if (wpa_parse_kde_ies(key_data, key_data_length, &kde) < 0) { | |
3838 | wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_INFO, | |
3839 | "received EAPOL-Key group msg 2/2 with invalid Key Data contents"); | |
3840 | return; | |
3841 | } | |
3842 | ||
3843 | if (wpa_auth_uses_ocv(sm)) { | |
3844 | struct wpa_channel_info ci; | |
3845 | int tx_chanwidth; | |
3846 | int tx_seg1_idx; | |
3847 | ||
3848 | if (wpa_channel_info(wpa_auth, &ci) != 0) { | |
3849 | wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO, | |
3850 | "Failed to get channel info to validate received OCI in EAPOL-Key group 1/2"); | |
3851 | return; | |
3852 | } | |
3853 | ||
3854 | if (get_sta_tx_parameters(sm, | |
3855 | channel_width_to_int(ci.chanwidth), | |
3856 | ci.seg1_idx, &tx_chanwidth, | |
3857 | &tx_seg1_idx) < 0) | |
3858 | return; | |
3859 | ||
3860 | if (ocv_verify_tx_params(kde.oci, kde.oci_len, &ci, | |
3861 | tx_chanwidth, tx_seg1_idx) != 0) { | |
3862 | wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO, | |
3863 | ocv_errorstr); | |
3864 | return; | |
3865 | } | |
3866 | } | |
3867 | #endif /* CONFIG_OCV */ | |
3868 | ||
6fc6879b JM |
3869 | if (sm->GUpdateStationKeys) |
3870 | sm->group->GKeyDoneStations--; | |
3871 | sm->GUpdateStationKeys = FALSE; | |
3872 | sm->GTimeoutCtr = 0; | |
3873 | /* FIX: MLME.SetProtection.Request(TA, Tx_Rx) */ | |
62e12afc | 3874 | wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_INFO, |
6fc6879b JM |
3875 | "group key handshake completed (%s)", |
3876 | sm->wpa == WPA_VERSION_WPA ? "WPA" : "RSN"); | |
3877 | sm->has_GTK = TRUE; | |
3878 | } | |
3879 | ||
3880 | ||
3881 | SM_STATE(WPA_PTK_GROUP, KEYERROR) | |
3882 | { | |
3883 | SM_ENTRY_MA(WPA_PTK_GROUP, KEYERROR, wpa_ptk_group); | |
3884 | if (sm->GUpdateStationKeys) | |
3885 | sm->group->GKeyDoneStations--; | |
3886 | sm->GUpdateStationKeys = FALSE; | |
3887 | sm->Disconnect = TRUE; | |
4bb9b674 GK |
3888 | wpa_auth_vlogger(sm->wpa_auth, sm->addr, LOGGER_INFO, |
3889 | "group key handshake failed (%s) after %u tries", | |
3890 | sm->wpa == WPA_VERSION_WPA ? "WPA" : "RSN", | |
3891 | sm->wpa_auth->conf.wpa_group_update_count); | |
6fc6879b JM |
3892 | } |
3893 | ||
3894 | ||
3895 | SM_STEP(WPA_PTK_GROUP) | |
3896 | { | |
9663596f | 3897 | if (sm->Init || sm->PtkGroupInit) { |
6fc6879b | 3898 | SM_ENTER(WPA_PTK_GROUP, IDLE); |
9663596f JM |
3899 | sm->PtkGroupInit = FALSE; |
3900 | } else switch (sm->wpa_ptk_group_state) { | |
6fc6879b JM |
3901 | case WPA_PTK_GROUP_IDLE: |
3902 | if (sm->GUpdateStationKeys || | |
3903 | (sm->wpa == WPA_VERSION_WPA && sm->PInitAKeys)) | |
3904 | SM_ENTER(WPA_PTK_GROUP, REKEYNEGOTIATING); | |
3905 | break; | |
3906 | case WPA_PTK_GROUP_REKEYNEGOTIATING: | |
3907 | if (sm->EAPOLKeyReceived && !sm->EAPOLKeyRequest && | |
3908 | !sm->EAPOLKeyPairwise && sm->MICVerified) | |
3909 | SM_ENTER(WPA_PTK_GROUP, REKEYESTABLISHED); | |
3910 | else if (sm->GTimeoutCtr > | |
6f234c1e JM |
3911 | sm->wpa_auth->conf.wpa_group_update_count || |
3912 | (sm->wpa_auth->conf.wpa_disable_eapol_key_retries && | |
3913 | sm->GTimeoutCtr > 1)) | |
6fc6879b JM |
3914 | SM_ENTER(WPA_PTK_GROUP, KEYERROR); |
3915 | else if (sm->TimeoutEvt) | |
3916 | SM_ENTER(WPA_PTK_GROUP, REKEYNEGOTIATING); | |
3917 | break; | |
3918 | case WPA_PTK_GROUP_KEYERROR: | |
3919 | SM_ENTER(WPA_PTK_GROUP, IDLE); | |
3920 | break; | |
3921 | case WPA_PTK_GROUP_REKEYESTABLISHED: | |
3922 | SM_ENTER(WPA_PTK_GROUP, IDLE); | |
3923 | break; | |
3924 | } | |
3925 | } | |
3926 | ||
3927 | ||
3928 | static int wpa_gtk_update(struct wpa_authenticator *wpa_auth, | |
3929 | struct wpa_group *group) | |
3930 | { | |
62e12afc | 3931 | struct wpa_auth_config *conf = &wpa_auth->conf; |
6fc6879b | 3932 | int ret = 0; |
555dcd75 | 3933 | size_t len; |
6fc6879b | 3934 | |
6fc6879b JM |
3935 | os_memcpy(group->GNonce, group->Counter, WPA_NONCE_LEN); |
3936 | inc_byte_array(group->Counter, WPA_NONCE_LEN); | |
3c7302c2 JM |
3937 | if (wpa_gmk_to_gtk(group->GMK, "Group key expansion", |
3938 | wpa_auth->addr, group->GNonce, | |
3939 | group->GTK[group->GN - 1], group->GTK_len) < 0) | |
3940 | ret = -1; | |
5cb9d5c3 JM |
3941 | wpa_hexdump_key(MSG_DEBUG, "GTK", |
3942 | group->GTK[group->GN - 1], group->GTK_len); | |
6fc6879b | 3943 | |
62e12afc JM |
3944 | if (conf->ieee80211w != NO_MGMT_FRAME_PROTECTION) { |
3945 | len = wpa_cipher_key_len(conf->group_mgmt_cipher); | |
5cb9d5c3 JM |
3946 | os_memcpy(group->GNonce, group->Counter, WPA_NONCE_LEN); |
3947 | inc_byte_array(group->Counter, WPA_NONCE_LEN); | |
3c7302c2 JM |
3948 | if (wpa_gmk_to_gtk(group->GMK, "IGTK key expansion", |
3949 | wpa_auth->addr, group->GNonce, | |
8dd9f9cd | 3950 | group->IGTK[group->GN_igtk - 4], len) < 0) |
3c7302c2 | 3951 | ret = -1; |
6fc6879b | 3952 | wpa_hexdump_key(MSG_DEBUG, "IGTK", |
8dd9f9cd | 3953 | group->IGTK[group->GN_igtk - 4], len); |
6fc6879b | 3954 | } |
6fc6879b | 3955 | |
62e12afc JM |
3956 | if (conf->ieee80211w != NO_MGMT_FRAME_PROTECTION && |
3957 | conf->beacon_prot) { | |
3958 | len = wpa_cipher_key_len(conf->group_mgmt_cipher); | |
555dcd75 JM |
3959 | os_memcpy(group->GNonce, group->Counter, WPA_NONCE_LEN); |
3960 | inc_byte_array(group->Counter, WPA_NONCE_LEN); | |
3961 | if (wpa_gmk_to_gtk(group->GMK, "BIGTK key expansion", | |
3962 | wpa_auth->addr, group->GNonce, | |
3963 | group->BIGTK[group->GN_bigtk - 6], len) < 0) | |
3964 | ret = -1; | |
3965 | wpa_hexdump_key(MSG_DEBUG, "BIGTK", | |
3966 | group->BIGTK[group->GN_bigtk - 6], len); | |
3967 | } | |
3968 | ||
6fc6879b JM |
3969 | return ret; |
3970 | } | |
3971 | ||
3972 | ||
3973 | static void wpa_group_gtk_init(struct wpa_authenticator *wpa_auth, | |
3974 | struct wpa_group *group) | |
3975 | { | |
3976 | wpa_printf(MSG_DEBUG, "WPA: group state machine entering state " | |
3977 | "GTK_INIT (VLAN-ID %d)", group->vlan_id); | |
3978 | group->changed = FALSE; /* GInit is not cleared here; avoid loop */ | |
3979 | group->wpa_group_state = WPA_GROUP_GTK_INIT; | |
3980 | ||
3981 | /* GTK[0..N] = 0 */ | |
3982 | os_memset(group->GTK, 0, sizeof(group->GTK)); | |
3983 | group->GN = 1; | |
3984 | group->GM = 2; | |
6fc6879b JM |
3985 | group->GN_igtk = 4; |
3986 | group->GM_igtk = 5; | |
555dcd75 JM |
3987 | group->GN_bigtk = 6; |
3988 | group->GM_bigtk = 7; | |
6fc6879b JM |
3989 | /* GTK[GN] = CalcGTK() */ |
3990 | wpa_gtk_update(wpa_auth, group); | |
3991 | } | |
3992 | ||
3993 | ||
3994 | static int wpa_group_update_sta(struct wpa_state_machine *sm, void *ctx) | |
3995 | { | |
473b6f22 MB |
3996 | if (ctx != NULL && ctx != sm->group) |
3997 | return 0; | |
3998 | ||
6fc6879b JM |
3999 | if (sm->wpa_ptk_state != WPA_PTK_PTKINITDONE) { |
4000 | wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG, | |
4001 | "Not in PTKINITDONE; skip Group Key update"); | |
3c183894 | 4002 | sm->GUpdateStationKeys = FALSE; |
6fc6879b JM |
4003 | return 0; |
4004 | } | |
9663596f JM |
4005 | if (sm->GUpdateStationKeys) { |
4006 | /* | |
3c183894 JM |
4007 | * This should not really happen, so add a debug log entry. |
4008 | * Since we clear the GKeyDoneStations before the loop, the | |
4009 | * station needs to be counted here anyway. | |
9663596f JM |
4010 | */ |
4011 | wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG, | |
3c183894 JM |
4012 | "GUpdateStationKeys was already set when " |
4013 | "marking station for GTK rekeying"); | |
9663596f | 4014 | } |
3c183894 | 4015 | |
ad3872a3 | 4016 | /* Do not rekey GTK/IGTK when STA is in WNM-Sleep Mode */ |
d32d94db XC |
4017 | if (sm->is_wnmsleep) |
4018 | return 0; | |
d32d94db | 4019 | |
3c183894 JM |
4020 | sm->group->GKeyDoneStations++; |
4021 | sm->GUpdateStationKeys = TRUE; | |
4022 | ||
6fc6879b JM |
4023 | wpa_sm_step(sm); |
4024 | return 0; | |
4025 | } | |
4026 | ||
4027 | ||
b5bf84ba | 4028 | #ifdef CONFIG_WNM_AP |
ad3872a3 | 4029 | /* update GTK when exiting WNM-Sleep Mode */ |
d32d94db XC |
4030 | void wpa_wnmsleep_rekey_gtk(struct wpa_state_machine *sm) |
4031 | { | |
629edfef | 4032 | if (sm == NULL || sm->is_wnmsleep) |
d32d94db XC |
4033 | return; |
4034 | ||
4035 | wpa_group_update_sta(sm, NULL); | |
4036 | } | |
4037 | ||
4038 | ||
4039 | void wpa_set_wnmsleep(struct wpa_state_machine *sm, int flag) | |
4040 | { | |
629edfef JM |
4041 | if (sm) |
4042 | sm->is_wnmsleep = !!flag; | |
d32d94db XC |
4043 | } |
4044 | ||
4045 | ||
4046 | int wpa_wnmsleep_gtk_subelem(struct wpa_state_machine *sm, u8 *pos) | |
4047 | { | |
d32d94db | 4048 | struct wpa_group *gsm = sm->group; |
835822d4 | 4049 | u8 *start = pos; |
d32d94db XC |
4050 | |
4051 | /* | |
835822d4 | 4052 | * GTK subelement: |
d32d94db | 4053 | * Sub-elem ID[1] | Length[1] | Key Info[2] | Key Length[1] | RSC[8] | |
835822d4 | 4054 | * Key[5..32] |
d32d94db | 4055 | */ |
835822d4 JM |
4056 | *pos++ = WNM_SLEEP_SUBELEM_GTK; |
4057 | *pos++ = 11 + gsm->GTK_len; | |
d32d94db | 4058 | /* Key ID in B0-B1 of Key Info */ |
835822d4 JM |
4059 | WPA_PUT_LE16(pos, gsm->GN & 0x03); |
4060 | pos += 2; | |
4061 | *pos++ = gsm->GTK_len; | |
4062 | if (wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN, pos) != 0) | |
d32d94db | 4063 | return 0; |
835822d4 JM |
4064 | pos += 8; |
4065 | os_memcpy(pos, gsm->GTK[gsm->GN - 1], gsm->GTK_len); | |
4066 | pos += gsm->GTK_len; | |
d32d94db | 4067 | |
835822d4 JM |
4068 | wpa_printf(MSG_DEBUG, "WNM: GTK Key ID %u in WNM-Sleep Mode exit", |
4069 | gsm->GN); | |
4070 | wpa_hexdump_key(MSG_DEBUG, "WNM: GTK in WNM-Sleep Mode exit", | |
d32d94db | 4071 | gsm->GTK[gsm->GN - 1], gsm->GTK_len); |
d32d94db | 4072 | |
835822d4 | 4073 | return pos - start; |
d32d94db XC |
4074 | } |
4075 | ||
4076 | ||
d32d94db XC |
4077 | int wpa_wnmsleep_igtk_subelem(struct wpa_state_machine *sm, u8 *pos) |
4078 | { | |
d32d94db | 4079 | struct wpa_group *gsm = sm->group; |
835822d4 | 4080 | u8 *start = pos; |
8dd9f9cd | 4081 | size_t len = wpa_cipher_key_len(sm->wpa_auth->conf.group_mgmt_cipher); |
d32d94db | 4082 | |
835822d4 JM |
4083 | /* |
4084 | * IGTK subelement: | |
4085 | * Sub-elem ID[1] | Length[1] | KeyID[2] | PN[6] | Key[16] | |
4086 | */ | |
4087 | *pos++ = WNM_SLEEP_SUBELEM_IGTK; | |
8dd9f9cd | 4088 | *pos++ = 2 + 6 + len; |
835822d4 JM |
4089 | WPA_PUT_LE16(pos, gsm->GN_igtk); |
4090 | pos += 2; | |
4091 | if (wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN_igtk, pos) != 0) | |
d32d94db | 4092 | return 0; |
835822d4 | 4093 | pos += 6; |
d32d94db | 4094 | |
8dd9f9cd JM |
4095 | os_memcpy(pos, gsm->IGTK[gsm->GN_igtk - 4], len); |
4096 | pos += len; | |
d32d94db | 4097 | |
835822d4 JM |
4098 | wpa_printf(MSG_DEBUG, "WNM: IGTK Key ID %u in WNM-Sleep Mode exit", |
4099 | gsm->GN_igtk); | |
4100 | wpa_hexdump_key(MSG_DEBUG, "WNM: IGTK in WNM-Sleep Mode exit", | |
8dd9f9cd | 4101 | gsm->IGTK[gsm->GN_igtk - 4], len); |
d32d94db | 4102 | |
835822d4 | 4103 | return pos - start; |
d32d94db | 4104 | } |
7d2ed8ba | 4105 | |
16889aff JM |
4106 | |
4107 | int wpa_wnmsleep_bigtk_subelem(struct wpa_state_machine *sm, u8 *pos) | |
4108 | { | |
4109 | struct wpa_group *gsm = sm->group; | |
4110 | u8 *start = pos; | |
4111 | size_t len = wpa_cipher_key_len(sm->wpa_auth->conf.group_mgmt_cipher); | |
4112 | ||
4113 | /* | |
4114 | * BIGTK subelement: | |
4115 | * Sub-elem ID[1] | Length[1] | KeyID[2] | PN[6] | Key[16] | |
4116 | */ | |
4117 | *pos++ = WNM_SLEEP_SUBELEM_BIGTK; | |
4118 | *pos++ = 2 + 6 + len; | |
4119 | WPA_PUT_LE16(pos, gsm->GN_bigtk); | |
4120 | pos += 2; | |
4121 | if (wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN_bigtk, pos) != 0) | |
4122 | return 0; | |
4123 | pos += 6; | |
4124 | ||
4125 | os_memcpy(pos, gsm->BIGTK[gsm->GN_bigtk - 6], len); | |
4126 | pos += len; | |
4127 | ||
4128 | wpa_printf(MSG_DEBUG, "WNM: BIGTK Key ID %u in WNM-Sleep Mode exit", | |
4129 | gsm->GN_bigtk); | |
4130 | wpa_hexdump_key(MSG_DEBUG, "WNM: BIGTK in WNM-Sleep Mode exit", | |
4131 | gsm->IGTK[gsm->GN_bigtk - 6], len); | |
4132 | ||
4133 | return pos - start; | |
4134 | } | |
4135 | ||
b5bf84ba | 4136 | #endif /* CONFIG_WNM_AP */ |
d32d94db XC |
4137 | |
4138 | ||
6fc6879b JM |
4139 | static void wpa_group_setkeys(struct wpa_authenticator *wpa_auth, |
4140 | struct wpa_group *group) | |
4141 | { | |
4142 | int tmp; | |
4143 | ||
4144 | wpa_printf(MSG_DEBUG, "WPA: group state machine entering state " | |
4145 | "SETKEYS (VLAN-ID %d)", group->vlan_id); | |
4146 | group->changed = TRUE; | |
4147 | group->wpa_group_state = WPA_GROUP_SETKEYS; | |
4148 | group->GTKReKey = FALSE; | |
4149 | tmp = group->GM; | |
4150 | group->GM = group->GN; | |
4151 | group->GN = tmp; | |
6fc6879b JM |
4152 | tmp = group->GM_igtk; |
4153 | group->GM_igtk = group->GN_igtk; | |
4154 | group->GN_igtk = tmp; | |
555dcd75 JM |
4155 | tmp = group->GM_bigtk; |
4156 | group->GM_bigtk = group->GN_bigtk; | |
4157 | group->GN_bigtk = tmp; | |
6fc6879b JM |
4158 | /* "GKeyDoneStations = GNoStations" is done in more robust way by |
4159 | * counting the STAs that are marked with GUpdateStationKeys instead of | |
4160 | * including all STAs that could be in not-yet-completed state. */ | |
4161 | wpa_gtk_update(wpa_auth, group); | |
4162 | ||
3c183894 JM |
4163 | if (group->GKeyDoneStations) { |
4164 | wpa_printf(MSG_DEBUG, "wpa_group_setkeys: Unexpected " | |
4165 | "GKeyDoneStations=%d when starting new GTK rekey", | |
4166 | group->GKeyDoneStations); | |
4167 | group->GKeyDoneStations = 0; | |
4168 | } | |
473b6f22 | 4169 | wpa_auth_for_each_sta(wpa_auth, wpa_group_update_sta, group); |
6fc6879b JM |
4170 | wpa_printf(MSG_DEBUG, "wpa_group_setkeys: GKeyDoneStations=%d", |
4171 | group->GKeyDoneStations); | |
4172 | } | |
4173 | ||
4174 | ||
1bdb7ab3 JM |
4175 | static int wpa_group_config_group_keys(struct wpa_authenticator *wpa_auth, |
4176 | struct wpa_group *group) | |
6fc6879b | 4177 | { |
62e12afc | 4178 | struct wpa_auth_config *conf = &wpa_auth->conf; |
3c7302c2 JM |
4179 | int ret = 0; |
4180 | ||
3c7302c2 | 4181 | if (wpa_auth_set_key(wpa_auth, group->vlan_id, |
62e12afc | 4182 | wpa_cipher_to_alg(conf->wpa_group), |
0382097e | 4183 | broadcast_ether_addr, group->GN, |
a919a260 AW |
4184 | group->GTK[group->GN - 1], group->GTK_len, |
4185 | KEY_FLAG_GROUP_TX_DEFAULT) < 0) | |
3c7302c2 | 4186 | ret = -1; |
6fc6879b | 4187 | |
62e12afc | 4188 | if (conf->ieee80211w != NO_MGMT_FRAME_PROTECTION) { |
8dd9f9cd JM |
4189 | enum wpa_alg alg; |
4190 | size_t len; | |
4191 | ||
62e12afc JM |
4192 | alg = wpa_cipher_to_alg(conf->group_mgmt_cipher); |
4193 | len = wpa_cipher_key_len(conf->group_mgmt_cipher); | |
8dd9f9cd JM |
4194 | |
4195 | if (ret == 0 && | |
4196 | wpa_auth_set_key(wpa_auth, group->vlan_id, alg, | |
4197 | broadcast_ether_addr, group->GN_igtk, | |
a919a260 AW |
4198 | group->IGTK[group->GN_igtk - 4], len, |
4199 | KEY_FLAG_GROUP_TX_DEFAULT) < 0) | |
8dd9f9cd | 4200 | ret = -1; |
555dcd75 | 4201 | |
62e12afc | 4202 | if (ret == 0 && conf->beacon_prot && |
555dcd75 JM |
4203 | wpa_auth_set_key(wpa_auth, group->vlan_id, alg, |
4204 | broadcast_ether_addr, group->GN_bigtk, | |
4205 | group->BIGTK[group->GN_bigtk - 6], len, | |
4206 | KEY_FLAG_GROUP_TX_DEFAULT) < 0) | |
4207 | ret = -1; | |
8dd9f9cd | 4208 | } |
3c7302c2 JM |
4209 | |
4210 | return ret; | |
6fc6879b JM |
4211 | } |
4212 | ||
4213 | ||
7d7f7be2 JM |
4214 | static int wpa_group_disconnect_cb(struct wpa_state_machine *sm, void *ctx) |
4215 | { | |
4216 | if (sm->group == ctx) { | |
4217 | wpa_printf(MSG_DEBUG, "WPA: Mark STA " MACSTR | |
4218 | " for discconnection due to fatal failure", | |
4219 | MAC2STR(sm->addr)); | |
4220 | sm->Disconnect = TRUE; | |
4221 | } | |
4222 | ||
4223 | return 0; | |
4224 | } | |
4225 | ||
4226 | ||
4227 | static void wpa_group_fatal_failure(struct wpa_authenticator *wpa_auth, | |
4228 | struct wpa_group *group) | |
4229 | { | |
4230 | wpa_printf(MSG_DEBUG, "WPA: group state machine entering state FATAL_FAILURE"); | |
4231 | group->changed = TRUE; | |
4232 | group->wpa_group_state = WPA_GROUP_FATAL_FAILURE; | |
4233 | wpa_auth_for_each_sta(wpa_auth, wpa_group_disconnect_cb, group); | |
4234 | } | |
4235 | ||
4236 | ||
1bdb7ab3 JM |
4237 | static int wpa_group_setkeysdone(struct wpa_authenticator *wpa_auth, |
4238 | struct wpa_group *group) | |
4239 | { | |
4240 | wpa_printf(MSG_DEBUG, "WPA: group state machine entering state " | |
4241 | "SETKEYSDONE (VLAN-ID %d)", group->vlan_id); | |
4242 | group->changed = TRUE; | |
4243 | group->wpa_group_state = WPA_GROUP_SETKEYSDONE; | |
4244 | ||
7d7f7be2 JM |
4245 | if (wpa_group_config_group_keys(wpa_auth, group) < 0) { |
4246 | wpa_group_fatal_failure(wpa_auth, group); | |
1bdb7ab3 | 4247 | return -1; |
7d7f7be2 | 4248 | } |
1bdb7ab3 JM |
4249 | |
4250 | return 0; | |
4251 | } | |
4252 | ||
4253 | ||
6fc6879b JM |
4254 | static void wpa_group_sm_step(struct wpa_authenticator *wpa_auth, |
4255 | struct wpa_group *group) | |
4256 | { | |
4257 | if (group->GInit) { | |
4258 | wpa_group_gtk_init(wpa_auth, group); | |
7d7f7be2 JM |
4259 | } else if (group->wpa_group_state == WPA_GROUP_FATAL_FAILURE) { |
4260 | /* Do not allow group operations */ | |
6fc6879b JM |
4261 | } else if (group->wpa_group_state == WPA_GROUP_GTK_INIT && |
4262 | group->GTKAuthenticator) { | |
4263 | wpa_group_setkeysdone(wpa_auth, group); | |
4264 | } else if (group->wpa_group_state == WPA_GROUP_SETKEYSDONE && | |
4265 | group->GTKReKey) { | |
4266 | wpa_group_setkeys(wpa_auth, group); | |
4267 | } else if (group->wpa_group_state == WPA_GROUP_SETKEYS) { | |
4268 | if (group->GKeyDoneStations == 0) | |
4269 | wpa_group_setkeysdone(wpa_auth, group); | |
4270 | else if (group->GTKReKey) | |
4271 | wpa_group_setkeys(wpa_auth, group); | |
4272 | } | |
4273 | } | |
4274 | ||
4275 | ||
e4a6ea1d | 4276 | static int wpa_sm_step(struct wpa_state_machine *sm) |
6fc6879b JM |
4277 | { |
4278 | if (sm == NULL) | |
e4a6ea1d | 4279 | return 0; |
6fc6879b JM |
4280 | |
4281 | if (sm->in_step_loop) { | |
4282 | /* This should not happen, but if it does, make sure we do not | |
4283 | * end up freeing the state machine too early by exiting the | |
4284 | * recursive call. */ | |
4285 | wpa_printf(MSG_ERROR, "WPA: wpa_sm_step() called recursively"); | |
e4a6ea1d | 4286 | return 0; |
6fc6879b JM |
4287 | } |
4288 | ||
4289 | sm->in_step_loop = 1; | |
4290 | do { | |
4291 | if (sm->pending_deinit) | |
4292 | break; | |
4293 | ||
4294 | sm->changed = FALSE; | |
4295 | sm->wpa_auth->group->changed = FALSE; | |
4296 | ||
4297 | SM_STEP_RUN(WPA_PTK); | |
4298 | if (sm->pending_deinit) | |
4299 | break; | |
4300 | SM_STEP_RUN(WPA_PTK_GROUP); | |
4301 | if (sm->pending_deinit) | |
4302 | break; | |
4303 | wpa_group_sm_step(sm->wpa_auth, sm->group); | |
4304 | } while (sm->changed || sm->wpa_auth->group->changed); | |
4305 | sm->in_step_loop = 0; | |
4306 | ||
4307 | if (sm->pending_deinit) { | |
4308 | wpa_printf(MSG_DEBUG, "WPA: Completing pending STA state " | |
4309 | "machine deinit for " MACSTR, MAC2STR(sm->addr)); | |
4310 | wpa_free_sta_sm(sm); | |
e4a6ea1d | 4311 | return 1; |
6fc6879b | 4312 | } |
e4a6ea1d | 4313 | return 0; |
6fc6879b JM |
4314 | } |
4315 | ||
4316 | ||
4317 | static void wpa_sm_call_step(void *eloop_ctx, void *timeout_ctx) | |
4318 | { | |
4319 | struct wpa_state_machine *sm = eloop_ctx; | |
4320 | wpa_sm_step(sm); | |
4321 | } | |
4322 | ||
4323 | ||
4324 | void wpa_auth_sm_notify(struct wpa_state_machine *sm) | |
4325 | { | |
4326 | if (sm == NULL) | |
4327 | return; | |
4328 | eloop_register_timeout(0, 0, wpa_sm_call_step, sm, NULL); | |
4329 | } | |
4330 | ||
4331 | ||
4332 | void wpa_gtk_rekey(struct wpa_authenticator *wpa_auth) | |
4333 | { | |
4334 | int tmp, i; | |
4335 | struct wpa_group *group; | |
4336 | ||
4337 | if (wpa_auth == NULL) | |
4338 | return; | |
4339 | ||
4340 | group = wpa_auth->group; | |
4341 | ||
4342 | for (i = 0; i < 2; i++) { | |
4343 | tmp = group->GM; | |
4344 | group->GM = group->GN; | |
4345 | group->GN = tmp; | |
6fc6879b JM |
4346 | tmp = group->GM_igtk; |
4347 | group->GM_igtk = group->GN_igtk; | |
4348 | group->GN_igtk = tmp; | |
555dcd75 JM |
4349 | tmp = group->GM_bigtk; |
4350 | group->GM_bigtk = group->GN_bigtk; | |
4351 | group->GN_bigtk = tmp; | |
6fc6879b | 4352 | wpa_gtk_update(wpa_auth, group); |
9354e594 | 4353 | wpa_group_config_group_keys(wpa_auth, group); |
6fc6879b JM |
4354 | } |
4355 | } | |
4356 | ||
4357 | ||
a6da824b | 4358 | static const char * wpa_bool_txt(int val) |
6fc6879b | 4359 | { |
a6da824b | 4360 | return val ? "TRUE" : "FALSE"; |
6fc6879b JM |
4361 | } |
4362 | ||
4363 | ||
6fc6879b JM |
4364 | #define RSN_SUITE "%02x-%02x-%02x-%d" |
4365 | #define RSN_SUITE_ARG(s) \ | |
4366 | ((s) >> 24) & 0xff, ((s) >> 16) & 0xff, ((s) >> 8) & 0xff, (s) & 0xff | |
4367 | ||
4368 | int wpa_get_mib(struct wpa_authenticator *wpa_auth, char *buf, size_t buflen) | |
4369 | { | |
62e12afc | 4370 | struct wpa_auth_config *conf; |
6fc6879b JM |
4371 | int len = 0, ret; |
4372 | char pmkid_txt[PMKID_LEN * 2 + 1]; | |
8ce58ceb IH |
4373 | #ifdef CONFIG_RSN_PREAUTH |
4374 | const int preauth = 1; | |
4375 | #else /* CONFIG_RSN_PREAUTH */ | |
4376 | const int preauth = 0; | |
4377 | #endif /* CONFIG_RSN_PREAUTH */ | |
6fc6879b JM |
4378 | |
4379 | if (wpa_auth == NULL) | |
4380 | return len; | |
62e12afc | 4381 | conf = &wpa_auth->conf; |
6fc6879b JM |
4382 | |
4383 | ret = os_snprintf(buf + len, buflen - len, | |
4384 | "dot11RSNAOptionImplemented=TRUE\n" | |
8ce58ceb | 4385 | "dot11RSNAPreauthenticationImplemented=%s\n" |
6fc6879b JM |
4386 | "dot11RSNAEnabled=%s\n" |
4387 | "dot11RSNAPreauthenticationEnabled=%s\n", | |
8ce58ceb | 4388 | wpa_bool_txt(preauth), |
62e12afc JM |
4389 | wpa_bool_txt(conf->wpa & WPA_PROTO_RSN), |
4390 | wpa_bool_txt(conf->rsn_preauth)); | |
d85e1fc8 | 4391 | if (os_snprintf_error(buflen - len, ret)) |
6fc6879b JM |
4392 | return len; |
4393 | len += ret; | |
4394 | ||
4395 | wpa_snprintf_hex(pmkid_txt, sizeof(pmkid_txt), | |
4396 | wpa_auth->dot11RSNAPMKIDUsed, PMKID_LEN); | |
4397 | ||
4398 | ret = os_snprintf( | |
4399 | buf + len, buflen - len, | |
4400 | "dot11RSNAConfigVersion=%u\n" | |
4401 | "dot11RSNAConfigPairwiseKeysSupported=9999\n" | |
4402 | /* FIX: dot11RSNAConfigGroupCipher */ | |
4403 | /* FIX: dot11RSNAConfigGroupRekeyMethod */ | |
4404 | /* FIX: dot11RSNAConfigGroupRekeyTime */ | |
4405 | /* FIX: dot11RSNAConfigGroupRekeyPackets */ | |
4406 | "dot11RSNAConfigGroupRekeyStrict=%u\n" | |
4407 | "dot11RSNAConfigGroupUpdateCount=%u\n" | |
4408 | "dot11RSNAConfigPairwiseUpdateCount=%u\n" | |
4409 | "dot11RSNAConfigGroupCipherSize=%u\n" | |
4410 | "dot11RSNAConfigPMKLifetime=%u\n" | |
4411 | "dot11RSNAConfigPMKReauthThreshold=%u\n" | |
4412 | "dot11RSNAConfigNumberOfPTKSAReplayCounters=0\n" | |
4413 | "dot11RSNAConfigSATimeout=%u\n" | |
4414 | "dot11RSNAAuthenticationSuiteSelected=" RSN_SUITE "\n" | |
4415 | "dot11RSNAPairwiseCipherSelected=" RSN_SUITE "\n" | |
4416 | "dot11RSNAGroupCipherSelected=" RSN_SUITE "\n" | |
4417 | "dot11RSNAPMKIDUsed=%s\n" | |
4418 | "dot11RSNAAuthenticationSuiteRequested=" RSN_SUITE "\n" | |
4419 | "dot11RSNAPairwiseCipherRequested=" RSN_SUITE "\n" | |
4420 | "dot11RSNAGroupCipherRequested=" RSN_SUITE "\n" | |
4421 | "dot11RSNATKIPCounterMeasuresInvoked=%u\n" | |
4422 | "dot11RSNA4WayHandshakeFailures=%u\n" | |
4423 | "dot11RSNAConfigNumberOfGTKSAReplayCounters=0\n", | |
4424 | RSN_VERSION, | |
62e12afc JM |
4425 | !!conf->wpa_strict_rekey, |
4426 | conf->wpa_group_update_count, | |
4427 | conf->wpa_pairwise_update_count, | |
4428 | wpa_cipher_key_len(conf->wpa_group) * 8, | |
6fc6879b JM |
4429 | dot11RSNAConfigPMKLifetime, |
4430 | dot11RSNAConfigPMKReauthThreshold, | |
4431 | dot11RSNAConfigSATimeout, | |
4432 | RSN_SUITE_ARG(wpa_auth->dot11RSNAAuthenticationSuiteSelected), | |
4433 | RSN_SUITE_ARG(wpa_auth->dot11RSNAPairwiseCipherSelected), | |
4434 | RSN_SUITE_ARG(wpa_auth->dot11RSNAGroupCipherSelected), | |
4435 | pmkid_txt, | |
4436 | RSN_SUITE_ARG(wpa_auth->dot11RSNAAuthenticationSuiteRequested), | |
4437 | RSN_SUITE_ARG(wpa_auth->dot11RSNAPairwiseCipherRequested), | |
4438 | RSN_SUITE_ARG(wpa_auth->dot11RSNAGroupCipherRequested), | |
4439 | wpa_auth->dot11RSNATKIPCounterMeasuresInvoked, | |
4440 | wpa_auth->dot11RSNA4WayHandshakeFailures); | |
d85e1fc8 | 4441 | if (os_snprintf_error(buflen - len, ret)) |
6fc6879b JM |
4442 | return len; |
4443 | len += ret; | |
4444 | ||
4445 | /* TODO: dot11RSNAConfigPairwiseCiphersTable */ | |
4446 | /* TODO: dot11RSNAConfigAuthenticationSuitesTable */ | |
4447 | ||
4448 | /* Private MIB */ | |
4449 | ret = os_snprintf(buf + len, buflen - len, "hostapdWPAGroupState=%d\n", | |
4450 | wpa_auth->group->wpa_group_state); | |
d85e1fc8 | 4451 | if (os_snprintf_error(buflen - len, ret)) |
6fc6879b JM |
4452 | return len; |
4453 | len += ret; | |
4454 | ||
4455 | return len; | |
4456 | } | |
4457 | ||
4458 | ||
4459 | int wpa_get_mib_sta(struct wpa_state_machine *sm, char *buf, size_t buflen) | |
4460 | { | |
4461 | int len = 0, ret; | |
4462 | u32 pairwise = 0; | |
4463 | ||
4464 | if (sm == NULL) | |
4465 | return 0; | |
4466 | ||
4467 | /* TODO: FF-FF-FF-FF-FF-FF entry for broadcast/multicast stats */ | |
4468 | ||
4469 | /* dot11RSNAStatsEntry */ | |
4470 | ||
c3550295 JM |
4471 | pairwise = wpa_cipher_to_suite(sm->wpa == WPA_VERSION_WPA2 ? |
4472 | WPA_PROTO_RSN : WPA_PROTO_WPA, | |
4473 | sm->pairwise); | |
4474 | if (pairwise == 0) | |
6fc6879b JM |
4475 | return 0; |
4476 | ||
4477 | ret = os_snprintf( | |
4478 | buf + len, buflen - len, | |
4479 | /* TODO: dot11RSNAStatsIndex */ | |
4480 | "dot11RSNAStatsSTAAddress=" MACSTR "\n" | |
4481 | "dot11RSNAStatsVersion=1\n" | |
4482 | "dot11RSNAStatsSelectedPairwiseCipher=" RSN_SUITE "\n" | |
4483 | /* TODO: dot11RSNAStatsTKIPICVErrors */ | |
4484 | "dot11RSNAStatsTKIPLocalMICFailures=%u\n" | |
d9040cdb | 4485 | "dot11RSNAStatsTKIPRemoteMICFailures=%u\n" |
6fc6879b JM |
4486 | /* TODO: dot11RSNAStatsCCMPReplays */ |
4487 | /* TODO: dot11RSNAStatsCCMPDecryptErrors */ | |
4488 | /* TODO: dot11RSNAStatsTKIPReplays */, | |
4489 | MAC2STR(sm->addr), | |
4490 | RSN_SUITE_ARG(pairwise), | |
4491 | sm->dot11RSNAStatsTKIPLocalMICFailures, | |
4492 | sm->dot11RSNAStatsTKIPRemoteMICFailures); | |
d85e1fc8 | 4493 | if (os_snprintf_error(buflen - len, ret)) |
6fc6879b JM |
4494 | return len; |
4495 | len += ret; | |
4496 | ||
4497 | /* Private MIB */ | |
4498 | ret = os_snprintf(buf + len, buflen - len, | |
d1b1f9fa JM |
4499 | "wpa=%d\n" |
4500 | "AKMSuiteSelector=" RSN_SUITE "\n" | |
6fc6879b JM |
4501 | "hostapdWPAPTKState=%d\n" |
4502 | "hostapdWPAPTKGroupState=%d\n", | |
d1b1f9fa JM |
4503 | sm->wpa, |
4504 | RSN_SUITE_ARG(wpa_akm_to_suite(sm->wpa_key_mgmt)), | |
6fc6879b JM |
4505 | sm->wpa_ptk_state, |
4506 | sm->wpa_ptk_group_state); | |
d85e1fc8 | 4507 | if (os_snprintf_error(buflen - len, ret)) |
6fc6879b JM |
4508 | return len; |
4509 | len += ret; | |
4510 | ||
4511 | return len; | |
4512 | } | |
4513 | ||
4514 | ||
4515 | void wpa_auth_countermeasures_start(struct wpa_authenticator *wpa_auth) | |
4516 | { | |
4517 | if (wpa_auth) | |
4518 | wpa_auth->dot11RSNATKIPCounterMeasuresInvoked++; | |
4519 | } | |
4520 | ||
4521 | ||
4522 | int wpa_auth_pairwise_set(struct wpa_state_machine *sm) | |
4523 | { | |
4524 | return sm && sm->pairwise_set; | |
4525 | } | |
4526 | ||
4527 | ||
ff36ff00 JM |
4528 | int wpa_auth_get_pairwise(struct wpa_state_machine *sm) |
4529 | { | |
4530 | return sm->pairwise; | |
4531 | } | |
4532 | ||
4533 | ||
b08c9ad0 MK |
4534 | const u8 * wpa_auth_get_pmk(struct wpa_state_machine *sm, int *len) |
4535 | { | |
4536 | if (!sm) | |
4537 | return NULL; | |
4538 | *len = sm->pmk_len; | |
4539 | return sm->PMK; | |
4540 | } | |
4541 | ||
4542 | ||
6fc6879b JM |
4543 | int wpa_auth_sta_key_mgmt(struct wpa_state_machine *sm) |
4544 | { | |
4545 | if (sm == NULL) | |
4546 | return -1; | |
4547 | return sm->wpa_key_mgmt; | |
4548 | } | |
4549 | ||
4550 | ||
4551 | int wpa_auth_sta_wpa_version(struct wpa_state_machine *sm) | |
4552 | { | |
4553 | if (sm == NULL) | |
4554 | return 0; | |
4555 | return sm->wpa; | |
4556 | } | |
4557 | ||
4558 | ||
0e3bd7ac MV |
4559 | int wpa_auth_sta_ft_tk_already_set(struct wpa_state_machine *sm) |
4560 | { | |
4561 | if (!sm || !wpa_key_mgmt_ft(sm->wpa_key_mgmt)) | |
4562 | return 0; | |
4563 | return sm->tk_already_set; | |
4564 | } | |
4565 | ||
4566 | ||
2f1357fb JM |
4567 | int wpa_auth_sta_fils_tk_already_set(struct wpa_state_machine *sm) |
4568 | { | |
4569 | if (!sm || !wpa_key_mgmt_fils(sm->wpa_key_mgmt)) | |
4570 | return 0; | |
4571 | return sm->tk_already_set; | |
4572 | } | |
4573 | ||
4574 | ||
6fc6879b JM |
4575 | int wpa_auth_sta_clear_pmksa(struct wpa_state_machine *sm, |
4576 | struct rsn_pmksa_cache_entry *entry) | |
4577 | { | |
4578 | if (sm == NULL || sm->pmksa != entry) | |
4579 | return -1; | |
4580 | sm->pmksa = NULL; | |
4581 | return 0; | |
4582 | } | |
4583 | ||
4584 | ||
4585 | struct rsn_pmksa_cache_entry * | |
4586 | wpa_auth_sta_get_pmksa(struct wpa_state_machine *sm) | |
4587 | { | |
4588 | return sm ? sm->pmksa : NULL; | |
4589 | } | |
4590 | ||
4591 | ||
4592 | void wpa_auth_sta_local_mic_failure_report(struct wpa_state_machine *sm) | |
4593 | { | |
4594 | if (sm) | |
4595 | sm->dot11RSNAStatsTKIPLocalMICFailures++; | |
4596 | } | |
4597 | ||
4598 | ||
4599 | const u8 * wpa_auth_get_wpa_ie(struct wpa_authenticator *wpa_auth, size_t *len) | |
4600 | { | |
4601 | if (wpa_auth == NULL) | |
4602 | return NULL; | |
4603 | *len = wpa_auth->wpa_ie_len; | |
4604 | return wpa_auth->wpa_ie; | |
4605 | } | |
4606 | ||
4607 | ||
4608 | int wpa_auth_pmksa_add(struct wpa_state_machine *sm, const u8 *pmk, | |
207976f0 | 4609 | unsigned int pmk_len, |
6fc6879b JM |
4610 | int session_timeout, struct eapol_state_machine *eapol) |
4611 | { | |
cb465555 JM |
4612 | if (sm == NULL || sm->wpa != WPA_VERSION_WPA2 || |
4613 | sm->wpa_auth->conf.disable_pmksa_caching) | |
6fc6879b JM |
4614 | return -1; |
4615 | ||
64f48093 JM |
4616 | #ifdef CONFIG_IEEE80211R_AP |
4617 | if (pmk_len >= 2 * PMK_LEN && wpa_key_mgmt_ft(sm->wpa_key_mgmt) && | |
4618 | wpa_key_mgmt_wpa_ieee8021x(sm->wpa_key_mgmt) && | |
4619 | !wpa_key_mgmt_sha384(sm->wpa_key_mgmt)) { | |
4620 | /* Cache MPMK/XXKey instead of initial part from MSK */ | |
4621 | pmk = pmk + PMK_LEN; | |
4622 | pmk_len = PMK_LEN; | |
4623 | } else | |
4624 | #endif /* CONFIG_IEEE80211R_AP */ | |
834c5d68 | 4625 | if (wpa_key_mgmt_sha384(sm->wpa_key_mgmt)) { |
207976f0 JM |
4626 | if (pmk_len > PMK_LEN_SUITE_B_192) |
4627 | pmk_len = PMK_LEN_SUITE_B_192; | |
4628 | } else if (pmk_len > PMK_LEN) { | |
4629 | pmk_len = PMK_LEN; | |
4630 | } | |
4631 | ||
64f48093 | 4632 | wpa_hexdump_key(MSG_DEBUG, "RSN: Cache PMK", pmk, pmk_len); |
70c93963 | 4633 | if (pmksa_cache_auth_add(sm->wpa_auth->pmksa, pmk, pmk_len, NULL, |
98cd3d1c | 4634 | sm->PTK.kck, sm->PTK.kck_len, |
4bb081f1 JM |
4635 | sm->wpa_auth->addr, sm->addr, session_timeout, |
4636 | eapol, sm->wpa_key_mgmt)) | |
6fc6879b JM |
4637 | return 0; |
4638 | ||
4639 | return -1; | |
4640 | } | |
4641 | ||
4642 | ||
4643 | int wpa_auth_pmksa_add_preauth(struct wpa_authenticator *wpa_auth, | |
4644 | const u8 *pmk, size_t len, const u8 *sta_addr, | |
4645 | int session_timeout, | |
4646 | struct eapol_state_machine *eapol) | |
4647 | { | |
4648 | if (wpa_auth == NULL) | |
4649 | return -1; | |
4650 | ||
64f48093 | 4651 | wpa_hexdump_key(MSG_DEBUG, "RSN: Cache PMK from preauth", pmk, len); |
70c93963 | 4652 | if (pmksa_cache_auth_add(wpa_auth->pmksa, pmk, len, NULL, |
087a1f4e JM |
4653 | NULL, 0, |
4654 | wpa_auth->addr, | |
4bb081f1 JM |
4655 | sta_addr, session_timeout, eapol, |
4656 | WPA_KEY_MGMT_IEEE8021X)) | |
6fc6879b JM |
4657 | return 0; |
4658 | ||
4659 | return -1; | |
4660 | } | |
4661 | ||
4662 | ||
f2991170 | 4663 | int wpa_auth_pmksa_add_sae(struct wpa_authenticator *wpa_auth, const u8 *addr, |
70c93963 | 4664 | const u8 *pmk, const u8 *pmkid) |
f2991170 JM |
4665 | { |
4666 | if (wpa_auth->conf.disable_pmksa_caching) | |
4667 | return -1; | |
4668 | ||
64f48093 | 4669 | wpa_hexdump_key(MSG_DEBUG, "RSN: Cache PMK from SAE", pmk, PMK_LEN); |
70c93963 | 4670 | if (pmksa_cache_auth_add(wpa_auth->pmksa, pmk, PMK_LEN, pmkid, |
087a1f4e | 4671 | NULL, 0, |
f2991170 JM |
4672 | wpa_auth->addr, addr, 0, NULL, |
4673 | WPA_KEY_MGMT_SAE)) | |
4674 | return 0; | |
4675 | ||
4676 | return -1; | |
4677 | } | |
4678 | ||
4679 | ||
9d94e4bb JM |
4680 | void wpa_auth_add_sae_pmkid(struct wpa_state_machine *sm, const u8 *pmkid) |
4681 | { | |
4682 | os_memcpy(sm->pmkid, pmkid, PMKID_LEN); | |
4683 | sm->pmkid_set = 1; | |
4684 | } | |
4685 | ||
4686 | ||
0c52953b JM |
4687 | int wpa_auth_pmksa_add2(struct wpa_authenticator *wpa_auth, const u8 *addr, |
4688 | const u8 *pmk, size_t pmk_len, const u8 *pmkid, | |
4689 | int session_timeout, int akmp) | |
4690 | { | |
4691 | if (wpa_auth->conf.disable_pmksa_caching) | |
4692 | return -1; | |
4693 | ||
64f48093 | 4694 | wpa_hexdump_key(MSG_DEBUG, "RSN: Cache PMK (2)", pmk, PMK_LEN); |
0c52953b JM |
4695 | if (pmksa_cache_auth_add(wpa_auth->pmksa, pmk, pmk_len, pmkid, |
4696 | NULL, 0, wpa_auth->addr, addr, session_timeout, | |
4697 | NULL, akmp)) | |
4698 | return 0; | |
4699 | ||
4700 | return -1; | |
4701 | } | |
4702 | ||
4703 | ||
901d1fe1 JM |
4704 | void wpa_auth_pmksa_remove(struct wpa_authenticator *wpa_auth, |
4705 | const u8 *sta_addr) | |
4706 | { | |
4707 | struct rsn_pmksa_cache_entry *pmksa; | |
4708 | ||
4709 | if (wpa_auth == NULL || wpa_auth->pmksa == NULL) | |
4710 | return; | |
4711 | pmksa = pmksa_cache_auth_get(wpa_auth->pmksa, sta_addr, NULL); | |
4712 | if (pmksa) { | |
4713 | wpa_printf(MSG_DEBUG, "WPA: Remove PMKSA cache entry for " | |
4714 | MACSTR " based on request", MAC2STR(sta_addr)); | |
4715 | pmksa_cache_free_entry(wpa_auth->pmksa, pmksa); | |
4716 | } | |
4717 | } | |
4718 | ||
4719 | ||
b8daac18 MH |
4720 | int wpa_auth_pmksa_list(struct wpa_authenticator *wpa_auth, char *buf, |
4721 | size_t len) | |
4722 | { | |
4723 | if (!wpa_auth || !wpa_auth->pmksa) | |
4724 | return 0; | |
4725 | return pmksa_cache_auth_list(wpa_auth->pmksa, buf, len); | |
4726 | } | |
4727 | ||
4728 | ||
4c522c77 MH |
4729 | void wpa_auth_pmksa_flush(struct wpa_authenticator *wpa_auth) |
4730 | { | |
4731 | if (wpa_auth && wpa_auth->pmksa) | |
4732 | pmksa_cache_auth_flush(wpa_auth->pmksa); | |
4733 | } | |
4734 | ||
4735 | ||
4d77d80e MH |
4736 | #ifdef CONFIG_PMKSA_CACHE_EXTERNAL |
4737 | #ifdef CONFIG_MESH | |
4738 | ||
4739 | int wpa_auth_pmksa_list_mesh(struct wpa_authenticator *wpa_auth, const u8 *addr, | |
4740 | char *buf, size_t len) | |
4741 | { | |
4742 | if (!wpa_auth || !wpa_auth->pmksa) | |
4743 | return 0; | |
4744 | ||
4745 | return pmksa_cache_auth_list_mesh(wpa_auth->pmksa, addr, buf, len); | |
4746 | } | |
4747 | ||
4748 | ||
4749 | struct rsn_pmksa_cache_entry * | |
4750 | wpa_auth_pmksa_create_entry(const u8 *aa, const u8 *spa, const u8 *pmk, | |
4751 | const u8 *pmkid, int expiration) | |
4752 | { | |
4753 | struct rsn_pmksa_cache_entry *entry; | |
4754 | struct os_reltime now; | |
4755 | ||
4756 | entry = pmksa_cache_auth_create_entry(pmk, PMK_LEN, pmkid, NULL, 0, aa, | |
4757 | spa, 0, NULL, WPA_KEY_MGMT_SAE); | |
4758 | if (!entry) | |
4759 | return NULL; | |
4760 | ||
4761 | os_get_reltime(&now); | |
4762 | entry->expiration = now.sec + expiration; | |
4763 | return entry; | |
4764 | } | |
4765 | ||
4766 | ||
4767 | int wpa_auth_pmksa_add_entry(struct wpa_authenticator *wpa_auth, | |
4768 | struct rsn_pmksa_cache_entry *entry) | |
4769 | { | |
4770 | int ret; | |
4771 | ||
4772 | if (!wpa_auth || !wpa_auth->pmksa) | |
4773 | return -1; | |
4774 | ||
4775 | ret = pmksa_cache_auth_add_entry(wpa_auth->pmksa, entry); | |
4776 | if (ret < 0) | |
4777 | wpa_printf(MSG_DEBUG, | |
4778 | "RSN: Failed to store external PMKSA cache for " | |
4779 | MACSTR, MAC2STR(entry->spa)); | |
4780 | ||
4781 | return ret; | |
4782 | } | |
4783 | ||
4784 | #endif /* CONFIG_MESH */ | |
4785 | #endif /* CONFIG_PMKSA_CACHE_EXTERNAL */ | |
4786 | ||
4787 | ||
9f2cf23e | 4788 | struct rsn_pmksa_cache_entry * |
c1bd4bac JM |
4789 | wpa_auth_pmksa_get(struct wpa_authenticator *wpa_auth, const u8 *sta_addr, |
4790 | const u8 *pmkid) | |
9f2cf23e MH |
4791 | { |
4792 | if (!wpa_auth || !wpa_auth->pmksa) | |
4793 | return NULL; | |
c1bd4bac | 4794 | return pmksa_cache_auth_get(wpa_auth->pmksa, sta_addr, pmkid); |
9f2cf23e MH |
4795 | } |
4796 | ||
4797 | ||
4798 | void wpa_auth_pmksa_set_to_sm(struct rsn_pmksa_cache_entry *pmksa, | |
4799 | struct wpa_state_machine *sm, | |
4800 | struct wpa_authenticator *wpa_auth, | |
4801 | u8 *pmkid, u8 *pmk) | |
4802 | { | |
4803 | if (!sm) | |
4804 | return; | |
4805 | ||
4806 | sm->pmksa = pmksa; | |
8854f90b JM |
4807 | os_memcpy(pmk, pmksa->pmk, PMK_LEN); |
4808 | os_memcpy(pmkid, pmksa->pmkid, PMKID_LEN); | |
4809 | os_memcpy(wpa_auth->dot11RSNAPMKIDUsed, pmksa->pmkid, PMKID_LEN); | |
9f2cf23e MH |
4810 | } |
4811 | ||
4812 | ||
a0ad9e8c MB |
4813 | /* |
4814 | * Remove and free the group from wpa_authenticator. This is triggered by a | |
4815 | * callback to make sure nobody is currently iterating the group list while it | |
4816 | * gets modified. | |
4817 | */ | |
4818 | static void wpa_group_free(struct wpa_authenticator *wpa_auth, | |
4819 | struct wpa_group *group) | |
4820 | { | |
4821 | struct wpa_group *prev = wpa_auth->group; | |
4822 | ||
4823 | wpa_printf(MSG_DEBUG, "WPA: Remove group state machine for VLAN-ID %d", | |
4824 | group->vlan_id); | |
4825 | ||
4826 | while (prev) { | |
4827 | if (prev->next == group) { | |
4828 | /* This never frees the special first group as needed */ | |
4829 | prev->next = group->next; | |
4830 | os_free(group); | |
4831 | break; | |
4832 | } | |
4833 | prev = prev->next; | |
4834 | } | |
4835 | ||
4836 | } | |
4837 | ||
4838 | ||
4839 | /* Increase the reference counter for group */ | |
4840 | static void wpa_group_get(struct wpa_authenticator *wpa_auth, | |
4841 | struct wpa_group *group) | |
4842 | { | |
4843 | /* Skip the special first group */ | |
4844 | if (wpa_auth->group == group) | |
4845 | return; | |
4846 | ||
4847 | group->references++; | |
4848 | } | |
4849 | ||
4850 | ||
4851 | /* Decrease the reference counter and maybe free the group */ | |
4852 | static void wpa_group_put(struct wpa_authenticator *wpa_auth, | |
4853 | struct wpa_group *group) | |
4854 | { | |
4855 | /* Skip the special first group */ | |
4856 | if (wpa_auth->group == group) | |
4857 | return; | |
4858 | ||
4859 | group->references--; | |
4860 | if (group->references) | |
4861 | return; | |
4862 | wpa_group_free(wpa_auth, group); | |
4863 | } | |
4864 | ||
4865 | ||
4866 | /* | |
4867 | * Add a group that has its references counter set to zero. Caller needs to | |
4868 | * call wpa_group_get() on the return value to mark the entry in use. | |
4869 | */ | |
6fc6879b JM |
4870 | static struct wpa_group * |
4871 | wpa_auth_add_group(struct wpa_authenticator *wpa_auth, int vlan_id) | |
4872 | { | |
4873 | struct wpa_group *group; | |
4874 | ||
4875 | if (wpa_auth == NULL || wpa_auth->group == NULL) | |
4876 | return NULL; | |
4877 | ||
4878 | wpa_printf(MSG_DEBUG, "WPA: Add group state machine for VLAN-ID %d", | |
4879 | vlan_id); | |
bdffdc5d | 4880 | group = wpa_group_init(wpa_auth, vlan_id, 0); |
6fc6879b JM |
4881 | if (group == NULL) |
4882 | return NULL; | |
4883 | ||
4884 | group->next = wpa_auth->group->next; | |
4885 | wpa_auth->group->next = group; | |
4886 | ||
4887 | return group; | |
4888 | } | |
4889 | ||
4890 | ||
7cebc8e2 MB |
4891 | /* |
4892 | * Enforce that the group state machine for the VLAN is running, increase | |
4893 | * reference counter as interface is up. References might have been increased | |
4894 | * even if a negative value is returned. | |
4895 | * Returns: -1 on error (group missing, group already failed); otherwise, 0 | |
4896 | */ | |
4897 | int wpa_auth_ensure_group(struct wpa_authenticator *wpa_auth, int vlan_id) | |
4898 | { | |
4899 | struct wpa_group *group; | |
4900 | ||
4901 | if (wpa_auth == NULL) | |
4902 | return 0; | |
4903 | ||
4904 | group = wpa_auth->group; | |
4905 | while (group) { | |
4906 | if (group->vlan_id == vlan_id) | |
4907 | break; | |
4908 | group = group->next; | |
4909 | } | |
4910 | ||
4911 | if (group == NULL) { | |
4912 | group = wpa_auth_add_group(wpa_auth, vlan_id); | |
4913 | if (group == NULL) | |
4914 | return -1; | |
4915 | } | |
4916 | ||
4917 | wpa_printf(MSG_DEBUG, | |
4918 | "WPA: Ensure group state machine running for VLAN ID %d", | |
4919 | vlan_id); | |
4920 | ||
4921 | wpa_group_get(wpa_auth, group); | |
4922 | group->num_setup_iface++; | |
4923 | ||
4924 | if (group->wpa_group_state == WPA_GROUP_FATAL_FAILURE) | |
4925 | return -1; | |
4926 | ||
4927 | return 0; | |
4928 | } | |
4929 | ||
4930 | ||
4931 | /* | |
4932 | * Decrease reference counter, expected to be zero afterwards. | |
4933 | * returns: -1 on error (group not found, group in fail state) | |
4934 | * -2 if wpa_group is still referenced | |
4935 | * 0 else | |
4936 | */ | |
4937 | int wpa_auth_release_group(struct wpa_authenticator *wpa_auth, int vlan_id) | |
4938 | { | |
4939 | struct wpa_group *group; | |
4940 | int ret = 0; | |
4941 | ||
4942 | if (wpa_auth == NULL) | |
4943 | return 0; | |
4944 | ||
4945 | group = wpa_auth->group; | |
4946 | while (group) { | |
4947 | if (group->vlan_id == vlan_id) | |
4948 | break; | |
4949 | group = group->next; | |
4950 | } | |
4951 | ||
4952 | if (group == NULL) | |
4953 | return -1; | |
4954 | ||
4955 | wpa_printf(MSG_DEBUG, | |
4956 | "WPA: Try stopping group state machine for VLAN ID %d", | |
4957 | vlan_id); | |
4958 | ||
4959 | if (group->num_setup_iface <= 0) { | |
4960 | wpa_printf(MSG_ERROR, | |
4961 | "WPA: wpa_auth_release_group called more often than wpa_auth_ensure_group for VLAN ID %d, skipping.", | |
4962 | vlan_id); | |
4963 | return -1; | |
4964 | } | |
4965 | group->num_setup_iface--; | |
4966 | ||
4967 | if (group->wpa_group_state == WPA_GROUP_FATAL_FAILURE) | |
4968 | ret = -1; | |
4969 | ||
4970 | if (group->references > 1) { | |
4971 | wpa_printf(MSG_DEBUG, | |
4972 | "WPA: Cannot stop group state machine for VLAN ID %d as references are still hold", | |
4973 | vlan_id); | |
4974 | ret = -2; | |
4975 | } | |
4976 | ||
4977 | wpa_group_put(wpa_auth, group); | |
4978 | ||
4979 | return ret; | |
4980 | } | |
4981 | ||
4982 | ||
6fc6879b JM |
4983 | int wpa_auth_sta_set_vlan(struct wpa_state_machine *sm, int vlan_id) |
4984 | { | |
4985 | struct wpa_group *group; | |
4986 | ||
4987 | if (sm == NULL || sm->wpa_auth == NULL) | |
4988 | return 0; | |
4989 | ||
4990 | group = sm->wpa_auth->group; | |
4991 | while (group) { | |
4992 | if (group->vlan_id == vlan_id) | |
4993 | break; | |
4994 | group = group->next; | |
4995 | } | |
4996 | ||
4997 | if (group == NULL) { | |
4998 | group = wpa_auth_add_group(sm->wpa_auth, vlan_id); | |
4999 | if (group == NULL) | |
5000 | return -1; | |
5001 | } | |
5002 | ||
5003 | if (sm->group == group) | |
5004 | return 0; | |
5005 | ||
7d7f7be2 JM |
5006 | if (group->wpa_group_state == WPA_GROUP_FATAL_FAILURE) |
5007 | return -1; | |
5008 | ||
6fc6879b JM |
5009 | wpa_printf(MSG_DEBUG, "WPA: Moving STA " MACSTR " to use group state " |
5010 | "machine for VLAN ID %d", MAC2STR(sm->addr), vlan_id); | |
5011 | ||
a0ad9e8c MB |
5012 | wpa_group_get(sm->wpa_auth, group); |
5013 | wpa_group_put(sm->wpa_auth, sm->group); | |
6fc6879b | 5014 | sm->group = group; |
a0ad9e8c | 5015 | |
6fc6879b JM |
5016 | return 0; |
5017 | } | |
e4bf4db9 JM |
5018 | |
5019 | ||
5020 | void wpa_auth_eapol_key_tx_status(struct wpa_authenticator *wpa_auth, | |
5021 | struct wpa_state_machine *sm, int ack) | |
5022 | { | |
5023 | if (wpa_auth == NULL || sm == NULL) | |
5024 | return; | |
5025 | wpa_printf(MSG_DEBUG, "WPA: EAPOL-Key TX status for STA " MACSTR | |
5026 | " ack=%d", MAC2STR(sm->addr), ack); | |
5027 | if (sm->pending_1_of_4_timeout && ack) { | |
5028 | /* | |
5029 | * Some deployed supplicant implementations update their SNonce | |
5030 | * for each EAPOL-Key 2/4 message even within the same 4-way | |
5031 | * handshake and then fail to use the first SNonce when | |
5032 | * deriving the PTK. This results in unsuccessful 4-way | |
5033 | * handshake whenever the relatively short initial timeout is | |
5034 | * reached and EAPOL-Key 1/4 is retransmitted. Try to work | |
5035 | * around this by increasing the timeout now that we know that | |
5036 | * the station has received the frame. | |
5037 | */ | |
5038 | int timeout_ms = eapol_key_timeout_subseq; | |
5039 | wpa_printf(MSG_DEBUG, "WPA: Increase initial EAPOL-Key 1/4 " | |
5040 | "timeout by %u ms because of acknowledged frame", | |
5041 | timeout_ms); | |
5042 | eloop_cancel_timeout(wpa_send_eapol_timeout, wpa_auth, sm); | |
5043 | eloop_register_timeout(timeout_ms / 1000, | |
5044 | (timeout_ms % 1000) * 1000, | |
5045 | wpa_send_eapol_timeout, wpa_auth, sm); | |
5046 | } | |
3d695328 JM |
5047 | |
5048 | #ifdef CONFIG_TESTING_OPTIONS | |
5049 | if (sm->eapol_status_cb) { | |
5050 | sm->eapol_status_cb(sm->eapol_status_cb_ctx1, | |
5051 | sm->eapol_status_cb_ctx2); | |
5052 | sm->eapol_status_cb = NULL; | |
5053 | } | |
5054 | #endif /* CONFIG_TESTING_OPTIONS */ | |
e4bf4db9 | 5055 | } |
c10347f2 JM |
5056 | |
5057 | ||
5058 | int wpa_auth_uses_sae(struct wpa_state_machine *sm) | |
5059 | { | |
5060 | if (sm == NULL) | |
5061 | return 0; | |
5062 | return wpa_key_mgmt_sae(sm->wpa_key_mgmt); | |
5063 | } | |
aa189ac9 JM |
5064 | |
5065 | ||
5066 | int wpa_auth_uses_ft_sae(struct wpa_state_machine *sm) | |
5067 | { | |
5068 | if (sm == NULL) | |
5069 | return 0; | |
5070 | return sm->wpa_key_mgmt == WPA_KEY_MGMT_FT_SAE; | |
5071 | } | |
25ef8529 JM |
5072 | |
5073 | ||
5074 | #ifdef CONFIG_P2P | |
5075 | int wpa_auth_get_ip_addr(struct wpa_state_machine *sm, u8 *addr) | |
5076 | { | |
5077 | if (sm == NULL || WPA_GET_BE32(sm->ip_addr) == 0) | |
5078 | return -1; | |
5079 | os_memcpy(addr, sm->ip_addr, 4); | |
5080 | return 0; | |
5081 | } | |
5082 | #endif /* CONFIG_P2P */ | |
cbc210de JM |
5083 | |
5084 | ||
5085 | int wpa_auth_radius_das_disconnect_pmksa(struct wpa_authenticator *wpa_auth, | |
5086 | struct radius_das_attrs *attr) | |
5087 | { | |
5088 | return pmksa_cache_auth_radius_das_disconnect(wpa_auth->pmksa, attr); | |
5089 | } | |
34782730 JM |
5090 | |
5091 | ||
5092 | void wpa_auth_reconfig_group_keys(struct wpa_authenticator *wpa_auth) | |
5093 | { | |
5094 | struct wpa_group *group; | |
5095 | ||
5096 | if (!wpa_auth) | |
5097 | return; | |
5098 | for (group = wpa_auth->group; group; group = group->next) | |
5099 | wpa_group_config_group_keys(wpa_auth, group); | |
5100 | } | |
7eace378 JM |
5101 | |
5102 | ||
5103 | #ifdef CONFIG_FILS | |
5104 | ||
5105 | struct wpa_auth_fils_iter_data { | |
5106 | struct wpa_authenticator *auth; | |
5107 | const u8 *cache_id; | |
5108 | struct rsn_pmksa_cache_entry *pmksa; | |
5109 | const u8 *spa; | |
5110 | const u8 *pmkid; | |
5111 | }; | |
5112 | ||
5113 | ||
5114 | static int wpa_auth_fils_iter(struct wpa_authenticator *a, void *ctx) | |
5115 | { | |
5116 | struct wpa_auth_fils_iter_data *data = ctx; | |
5117 | ||
4cc6574d | 5118 | if (a == data->auth || !a->conf.fils_cache_id_set || |
7eace378 JM |
5119 | os_memcmp(a->conf.fils_cache_id, data->cache_id, |
5120 | FILS_CACHE_ID_LEN) != 0) | |
5121 | return 0; | |
5122 | data->pmksa = pmksa_cache_auth_get(a->pmksa, data->spa, data->pmkid); | |
5123 | return data->pmksa != NULL; | |
5124 | } | |
5125 | ||
5126 | ||
5127 | struct rsn_pmksa_cache_entry * | |
5128 | wpa_auth_pmksa_get_fils_cache_id(struct wpa_authenticator *wpa_auth, | |
5129 | const u8 *sta_addr, const u8 *pmkid) | |
5130 | { | |
5131 | struct wpa_auth_fils_iter_data idata; | |
5132 | ||
5133 | if (!wpa_auth->conf.fils_cache_id_set) | |
5134 | return NULL; | |
5135 | idata.auth = wpa_auth; | |
5136 | idata.cache_id = wpa_auth->conf.fils_cache_id; | |
5137 | idata.pmksa = NULL; | |
5138 | idata.spa = sta_addr; | |
5139 | idata.pmkid = pmkid; | |
5140 | wpa_auth_for_each_auth(wpa_auth, wpa_auth_fils_iter, &idata); | |
5141 | return idata.pmksa; | |
5142 | } | |
5143 | ||
5db997e3 JM |
5144 | |
5145 | #ifdef CONFIG_IEEE80211R_AP | |
a7968ea5 JM |
5146 | int wpa_auth_write_fte(struct wpa_authenticator *wpa_auth, int use_sha384, |
5147 | u8 *buf, size_t len) | |
5db997e3 JM |
5148 | { |
5149 | struct wpa_auth_config *conf = &wpa_auth->conf; | |
5150 | ||
a7968ea5 | 5151 | return wpa_write_ftie(conf, use_sha384, conf->r0_key_holder, |
5db997e3 | 5152 | conf->r0_key_holder_len, |
51d1924b | 5153 | NULL, NULL, buf, len, NULL, 0, 0); |
5db997e3 JM |
5154 | } |
5155 | #endif /* CONFIG_IEEE80211R_AP */ | |
5156 | ||
8acbf85f JM |
5157 | |
5158 | void wpa_auth_get_fils_aead_params(struct wpa_state_machine *sm, | |
5159 | u8 *fils_anonce, u8 *fils_snonce, | |
5160 | u8 *fils_kek, size_t *fils_kek_len) | |
5161 | { | |
5162 | os_memcpy(fils_anonce, sm->ANonce, WPA_NONCE_LEN); | |
5163 | os_memcpy(fils_snonce, sm->SNonce, WPA_NONCE_LEN); | |
5164 | os_memcpy(fils_kek, sm->PTK.kek, WPA_KEK_MAX_LEN); | |
5165 | *fils_kek_len = sm->PTK.kek_len; | |
5166 | } | |
5167 | ||
a40bd06e JM |
5168 | |
5169 | void wpa_auth_add_fils_pmk_pmkid(struct wpa_state_machine *sm, const u8 *pmk, | |
5170 | size_t pmk_len, const u8 *pmkid) | |
5171 | { | |
5172 | os_memcpy(sm->PMK, pmk, pmk_len); | |
5173 | sm->pmk_len = pmk_len; | |
5174 | os_memcpy(sm->pmkid, pmkid, PMKID_LEN); | |
5175 | sm->pmkid_set = 1; | |
5176 | } | |
5177 | ||
7eace378 | 5178 | #endif /* CONFIG_FILS */ |
6bc2f00f JM |
5179 | |
5180 | ||
36536639 JM |
5181 | void wpa_auth_set_auth_alg(struct wpa_state_machine *sm, u16 auth_alg) |
5182 | { | |
5183 | if (sm) | |
5184 | sm->auth_alg = auth_alg; | |
5185 | } | |
5186 | ||
5187 | ||
10ec6a5f JM |
5188 | #ifdef CONFIG_DPP2 |
5189 | void wpa_auth_set_dpp_z(struct wpa_state_machine *sm, const struct wpabuf *z) | |
5190 | { | |
5191 | if (sm) { | |
5192 | wpabuf_clear_free(sm->dpp_z); | |
5193 | sm->dpp_z = z ? wpabuf_dup(z) : NULL; | |
5194 | } | |
5195 | } | |
5196 | #endif /* CONFIG_DPP2 */ | |
5197 | ||
5198 | ||
82424732 | 5199 | #ifdef CONFIG_TESTING_OPTIONS |
d8afdb21 | 5200 | |
3d695328 JM |
5201 | int wpa_auth_resend_m1(struct wpa_state_machine *sm, int change_anonce, |
5202 | void (*cb)(void *ctx1, void *ctx2), | |
5203 | void *ctx1, void *ctx2) | |
d8afdb21 JM |
5204 | { |
5205 | const u8 *anonce = sm->ANonce; | |
5206 | u8 anonce_buf[WPA_NONCE_LEN]; | |
5207 | ||
5208 | if (change_anonce) { | |
5209 | if (random_get_bytes(anonce_buf, WPA_NONCE_LEN)) | |
5210 | return -1; | |
5211 | anonce = anonce_buf; | |
5212 | } | |
5213 | ||
5214 | wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG, | |
5215 | "sending 1/4 msg of 4-Way Handshake (TESTING)"); | |
5216 | wpa_send_eapol(sm->wpa_auth, sm, | |
5217 | WPA_KEY_INFO_ACK | WPA_KEY_INFO_KEY_TYPE, NULL, | |
5218 | anonce, NULL, 0, 0, 0); | |
5219 | return 0; | |
5220 | } | |
5221 | ||
5222 | ||
3d695328 JM |
5223 | int wpa_auth_resend_m3(struct wpa_state_machine *sm, |
5224 | void (*cb)(void *ctx1, void *ctx2), | |
5225 | void *ctx1, void *ctx2) | |
d8afdb21 | 5226 | { |
0c3bc1be | 5227 | u8 rsc[WPA_KEY_RSC_LEN], *_rsc, *gtk, *kde, *pos; |
0c3bc1be | 5228 | u8 *opos; |
d8afdb21 JM |
5229 | size_t gtk_len, kde_len; |
5230 | struct wpa_group *gsm = sm->group; | |
5231 | u8 *wpa_ie; | |
7a4b01c8 | 5232 | int wpa_ie_len, secure, gtkidx, encr = 0; |
862aac1f | 5233 | u8 hdr[2]; |
d8afdb21 JM |
5234 | |
5235 | /* Send EAPOL(1, 1, 1, Pair, P, RSC, ANonce, MIC(PTK), RSNIE, [MDIE], | |
16889aff | 5236 | GTK[GN], IGTK, [BIGTK], [FTIE], [TIE * 2]) |
d8afdb21 JM |
5237 | */ |
5238 | ||
5239 | /* Use 0 RSC */ | |
5240 | os_memset(rsc, 0, WPA_KEY_RSC_LEN); | |
5241 | /* If FT is used, wpa_auth->wpa_ie includes both RSNIE and MDIE */ | |
5242 | wpa_ie = sm->wpa_auth->wpa_ie; | |
5243 | wpa_ie_len = sm->wpa_auth->wpa_ie_len; | |
5244 | if (sm->wpa == WPA_VERSION_WPA && | |
5245 | (sm->wpa_auth->conf.wpa & WPA_PROTO_RSN) && | |
5246 | wpa_ie_len > wpa_ie[1] + 2 && wpa_ie[0] == WLAN_EID_RSN) { | |
5247 | /* WPA-only STA, remove RSN IE and possible MDIE */ | |
5248 | wpa_ie = wpa_ie + wpa_ie[1] + 2; | |
5249 | if (wpa_ie[0] == WLAN_EID_MOBILITY_DOMAIN) | |
5250 | wpa_ie = wpa_ie + wpa_ie[1] + 2; | |
5251 | wpa_ie_len = wpa_ie[1] + 2; | |
5252 | } | |
5253 | wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG, | |
5254 | "sending 3/4 msg of 4-Way Handshake (TESTING)"); | |
5255 | if (sm->wpa == WPA_VERSION_WPA2) { | |
5256 | /* WPA2 send GTK in the 4-way handshake */ | |
5257 | secure = 1; | |
5258 | gtk = gsm->GTK[gsm->GN - 1]; | |
5259 | gtk_len = gsm->GTK_len; | |
7a4b01c8 | 5260 | gtkidx = gsm->GN; |
d8afdb21 JM |
5261 | _rsc = rsc; |
5262 | encr = 1; | |
5263 | } else { | |
5264 | /* WPA does not include GTK in msg 3/4 */ | |
5265 | secure = 0; | |
5266 | gtk = NULL; | |
5267 | gtk_len = 0; | |
d8afdb21 JM |
5268 | _rsc = NULL; |
5269 | if (sm->rx_eapol_key_secure) { | |
5270 | /* | |
5271 | * It looks like Windows 7 supplicant tries to use | |
5272 | * Secure bit in msg 2/4 after having reported Michael | |
5273 | * MIC failure and it then rejects the 4-way handshake | |
5274 | * if msg 3/4 does not set Secure bit. Work around this | |
5275 | * by setting the Secure bit here even in the case of | |
5276 | * WPA if the supplicant used it first. | |
5277 | */ | |
5278 | wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG, | |
5279 | "STA used Secure bit in WPA msg 2/4 - " | |
5280 | "set Secure for 3/4 as workaround"); | |
5281 | secure = 1; | |
5282 | } | |
5283 | } | |
5284 | ||
17c2559c | 5285 | kde_len = wpa_ie_len + ieee80211w_kde_len(sm) + ocv_oci_len(sm); |
862aac1f AW |
5286 | |
5287 | if (sm->use_ext_key_id) | |
5288 | kde_len += 2 + RSN_SELECTOR_LEN + 2; | |
5289 | ||
d8afdb21 JM |
5290 | if (gtk) |
5291 | kde_len += 2 + RSN_SELECTOR_LEN + 2 + gtk_len; | |
5292 | #ifdef CONFIG_IEEE80211R_AP | |
5293 | if (wpa_key_mgmt_ft(sm->wpa_key_mgmt)) { | |
5294 | kde_len += 2 + PMKID_LEN; /* PMKR1Name into RSN IE */ | |
5295 | kde_len += 300; /* FTIE + 2 * TIE */ | |
5296 | } | |
5297 | #endif /* CONFIG_IEEE80211R_AP */ | |
5298 | kde = os_malloc(kde_len); | |
5299 | if (kde == NULL) | |
5300 | return -1; | |
5301 | ||
5302 | pos = kde; | |
5303 | os_memcpy(pos, wpa_ie, wpa_ie_len); | |
5304 | pos += wpa_ie_len; | |
5305 | #ifdef CONFIG_IEEE80211R_AP | |
5306 | if (wpa_key_mgmt_ft(sm->wpa_key_mgmt)) { | |
5307 | int res; | |
5308 | size_t elen; | |
5309 | ||
5310 | elen = pos - kde; | |
5311 | res = wpa_insert_pmkid(kde, &elen, sm->pmk_r1_name); | |
5312 | if (res < 0) { | |
5313 | wpa_printf(MSG_ERROR, "FT: Failed to insert " | |
5314 | "PMKR1Name into RSN IE in EAPOL-Key data"); | |
5315 | os_free(kde); | |
5316 | return -1; | |
5317 | } | |
5318 | pos -= wpa_ie_len; | |
5319 | pos += elen; | |
5320 | } | |
5321 | #endif /* CONFIG_IEEE80211R_AP */ | |
862aac1f AW |
5322 | hdr[1] = 0; |
5323 | ||
5324 | if (sm->use_ext_key_id) { | |
5325 | hdr[0] = sm->keyidx_active & 0x01; | |
5326 | pos = wpa_add_kde(pos, RSN_KEY_DATA_KEYID, hdr, 2, NULL, 0); | |
5327 | } | |
5328 | ||
d8afdb21 | 5329 | if (gtk) { |
7a4b01c8 | 5330 | hdr[0] = gtkidx & 0x03; |
d8afdb21 JM |
5331 | pos = wpa_add_kde(pos, RSN_KEY_DATA_GROUPKEY, hdr, 2, |
5332 | gtk, gtk_len); | |
5333 | } | |
5334 | opos = pos; | |
5335 | pos = ieee80211w_kde_add(sm, pos); | |
9f917339 MV |
5336 | if (pos - opos >= 2 + RSN_SELECTOR_LEN + WPA_IGTK_KDE_PREFIX_LEN) { |
5337 | /* skip KDE header and keyid */ | |
5338 | opos += 2 + RSN_SELECTOR_LEN + 2; | |
d8afdb21 JM |
5339 | os_memset(opos, 0, 6); /* clear PN */ |
5340 | } | |
17c2559c MV |
5341 | if (ocv_oci_add(sm, &pos) < 0) { |
5342 | os_free(kde); | |
5343 | return -1; | |
5344 | } | |
d8afdb21 JM |
5345 | |
5346 | #ifdef CONFIG_IEEE80211R_AP | |
5347 | if (wpa_key_mgmt_ft(sm->wpa_key_mgmt)) { | |
5348 | int res; | |
5349 | struct wpa_auth_config *conf; | |
5350 | ||
5351 | conf = &sm->wpa_auth->conf; | |
5352 | if (sm->assoc_resp_ftie && | |
5353 | kde + kde_len - pos >= 2 + sm->assoc_resp_ftie[1]) { | |
5354 | os_memcpy(pos, sm->assoc_resp_ftie, | |
5355 | 2 + sm->assoc_resp_ftie[1]); | |
5356 | res = 2 + sm->assoc_resp_ftie[1]; | |
5357 | } else { | |
a7968ea5 JM |
5358 | int use_sha384 = wpa_key_mgmt_sha384(sm->wpa_key_mgmt); |
5359 | ||
5360 | res = wpa_write_ftie(conf, use_sha384, | |
5361 | conf->r0_key_holder, | |
d8afdb21 JM |
5362 | conf->r0_key_holder_len, |
5363 | NULL, NULL, pos, | |
5364 | kde + kde_len - pos, | |
51d1924b | 5365 | NULL, 0, 0); |
d8afdb21 JM |
5366 | } |
5367 | if (res < 0) { | |
5368 | wpa_printf(MSG_ERROR, "FT: Failed to insert FTIE " | |
5369 | "into EAPOL-Key Key Data"); | |
5370 | os_free(kde); | |
5371 | return -1; | |
5372 | } | |
5373 | pos += res; | |
5374 | ||
5375 | /* TIE[ReassociationDeadline] (TU) */ | |
5376 | *pos++ = WLAN_EID_TIMEOUT_INTERVAL; | |
5377 | *pos++ = 5; | |
5378 | *pos++ = WLAN_TIMEOUT_REASSOC_DEADLINE; | |
5379 | WPA_PUT_LE32(pos, conf->reassociation_deadline); | |
5380 | pos += 4; | |
5381 | ||
5382 | /* TIE[KeyLifetime] (seconds) */ | |
5383 | *pos++ = WLAN_EID_TIMEOUT_INTERVAL; | |
5384 | *pos++ = 5; | |
5385 | *pos++ = WLAN_TIMEOUT_KEY_LIFETIME; | |
83fe4bd3 | 5386 | WPA_PUT_LE32(pos, conf->r0_key_lifetime); |
d8afdb21 JM |
5387 | pos += 4; |
5388 | } | |
5389 | #endif /* CONFIG_IEEE80211R_AP */ | |
5390 | ||
5391 | wpa_send_eapol(sm->wpa_auth, sm, | |
5392 | (secure ? WPA_KEY_INFO_SECURE : 0) | | |
5393 | (wpa_mic_len(sm->wpa_key_mgmt, sm->pmk_len) ? | |
5394 | WPA_KEY_INFO_MIC : 0) | | |
5395 | WPA_KEY_INFO_ACK | WPA_KEY_INFO_INSTALL | | |
5396 | WPA_KEY_INFO_KEY_TYPE, | |
7a4b01c8 | 5397 | _rsc, sm->ANonce, kde, pos - kde, 0, encr); |
d8afdb21 JM |
5398 | os_free(kde); |
5399 | return 0; | |
5400 | } | |
5401 | ||
5402 | ||
3d695328 JM |
5403 | int wpa_auth_resend_group_m1(struct wpa_state_machine *sm, |
5404 | void (*cb)(void *ctx1, void *ctx2), | |
5405 | void *ctx1, void *ctx2) | |
6bc2f00f JM |
5406 | { |
5407 | u8 rsc[WPA_KEY_RSC_LEN]; | |
5408 | struct wpa_group *gsm = sm->group; | |
5409 | const u8 *kde; | |
0c3bc1be | 5410 | u8 *kde_buf = NULL, *pos, hdr[2]; |
0c3bc1be | 5411 | u8 *opos; |
6bc2f00f JM |
5412 | size_t kde_len; |
5413 | u8 *gtk; | |
5414 | ||
5415 | /* Send EAPOL(1, 1, 1, !Pair, G, RSC, GNonce, MIC(PTK), GTK[GN]) */ | |
5416 | os_memset(rsc, 0, WPA_KEY_RSC_LEN); | |
5417 | /* Use 0 RSC */ | |
5418 | wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG, | |
5419 | "sending 1/2 msg of Group Key Handshake (TESTING)"); | |
5420 | ||
5421 | gtk = gsm->GTK[gsm->GN - 1]; | |
5422 | if (sm->wpa == WPA_VERSION_WPA2) { | |
5423 | kde_len = 2 + RSN_SELECTOR_LEN + 2 + gsm->GTK_len + | |
17c2559c | 5424 | ieee80211w_kde_len(sm) + ocv_oci_len(sm); |
6bc2f00f JM |
5425 | kde_buf = os_malloc(kde_len); |
5426 | if (kde_buf == NULL) | |
5427 | return -1; | |
5428 | ||
5429 | kde = pos = kde_buf; | |
5430 | hdr[0] = gsm->GN & 0x03; | |
5431 | hdr[1] = 0; | |
5432 | pos = wpa_add_kde(pos, RSN_KEY_DATA_GROUPKEY, hdr, 2, | |
5433 | gtk, gsm->GTK_len); | |
5434 | opos = pos; | |
5435 | pos = ieee80211w_kde_add(sm, pos); | |
9f917339 MV |
5436 | if (pos - opos >= |
5437 | 2 + RSN_SELECTOR_LEN + WPA_IGTK_KDE_PREFIX_LEN) { | |
5438 | /* skip KDE header and keyid */ | |
5439 | opos += 2 + RSN_SELECTOR_LEN + 2; | |
6bc2f00f JM |
5440 | os_memset(opos, 0, 6); /* clear PN */ |
5441 | } | |
17c2559c MV |
5442 | if (ocv_oci_add(sm, &pos) < 0) { |
5443 | os_free(kde_buf); | |
5444 | return -1; | |
5445 | } | |
6bc2f00f JM |
5446 | kde_len = pos - kde; |
5447 | } else { | |
5448 | kde = gtk; | |
5449 | kde_len = gsm->GTK_len; | |
5450 | } | |
5451 | ||
3d695328 JM |
5452 | sm->eapol_status_cb = cb; |
5453 | sm->eapol_status_cb_ctx1 = ctx1; | |
5454 | sm->eapol_status_cb_ctx2 = ctx2; | |
5455 | ||
6bc2f00f JM |
5456 | wpa_send_eapol(sm->wpa_auth, sm, |
5457 | WPA_KEY_INFO_SECURE | | |
5458 | (wpa_mic_len(sm->wpa_key_mgmt, sm->pmk_len) ? | |
5459 | WPA_KEY_INFO_MIC : 0) | | |
5460 | WPA_KEY_INFO_ACK | | |
5461 | (!sm->Pair ? WPA_KEY_INFO_INSTALL : 0), | |
5462 | rsc, NULL, kde, kde_len, gsm->GN, 1); | |
5463 | ||
5464 | os_free(kde_buf); | |
5465 | return 0; | |
5466 | } | |
d8afdb21 | 5467 | |
92662fb2 JB |
5468 | |
5469 | int wpa_auth_rekey_gtk(struct wpa_authenticator *wpa_auth) | |
5470 | { | |
5471 | if (!wpa_auth) | |
5472 | return -1; | |
5473 | eloop_cancel_timeout(wpa_rekey_gtk, wpa_auth, NULL); | |
5474 | return eloop_register_timeout(0, 0, wpa_rekey_gtk, wpa_auth, NULL); | |
5475 | } | |
5476 | ||
6bc2f00f | 5477 | #endif /* CONFIG_TESTING_OPTIONS */ |