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