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