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