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