1 /* 2 * hostapd - WPA/RSN IE and KDE definitions 3 * Copyright (c) 2004-2015, 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 "common/ieee802_11_defs.h" 13 #include "eapol_auth/eapol_auth_sm.h" 14 #include "ap_config.h" 15 #include "ieee802_11.h" 16 #include "wpa_auth.h" 17 #include "pmksa_cache_auth.h" 18 #include "wpa_auth_ie.h" 19 #include "wpa_auth_i.h" 20 21 22 #ifdef CONFIG_RSN_TESTING 23 int rsn_testing = 0; 24 #endif /* CONFIG_RSN_TESTING */ 25 26 27 static int wpa_write_wpa_ie(struct wpa_auth_config *conf, u8 *buf, size_t len) 28 { 29 struct wpa_ie_hdr *hdr; 30 int num_suites; 31 u8 *pos, *count; 32 u32 suite; 33 34 hdr = (struct wpa_ie_hdr *) buf; 35 hdr->elem_id = WLAN_EID_VENDOR_SPECIFIC; 36 RSN_SELECTOR_PUT(hdr->oui, WPA_OUI_TYPE); 37 WPA_PUT_LE16(hdr->version, WPA_VERSION); 38 pos = (u8 *) (hdr + 1); 39 40 suite = wpa_cipher_to_suite(WPA_PROTO_WPA, conf->wpa_group); 41 if (suite == 0) { 42 wpa_printf(MSG_DEBUG, "Invalid group cipher (%d).", 43 conf->wpa_group); 44 return -1; 45 } 46 RSN_SELECTOR_PUT(pos, suite); 47 pos += WPA_SELECTOR_LEN; 48 49 count = pos; 50 pos += 2; 51 52 num_suites = wpa_cipher_put_suites(pos, conf->wpa_pairwise); 53 if (num_suites == 0) { 54 wpa_printf(MSG_DEBUG, "Invalid pairwise cipher (%d).", 55 conf->wpa_pairwise); 56 return -1; 57 } 58 pos += num_suites * WPA_SELECTOR_LEN; 59 WPA_PUT_LE16(count, num_suites); 60 61 num_suites = 0; 62 count = pos; 63 pos += 2; 64 65 if (conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X) { 66 RSN_SELECTOR_PUT(pos, WPA_AUTH_KEY_MGMT_UNSPEC_802_1X); 67 pos += WPA_SELECTOR_LEN; 68 num_suites++; 69 } 70 if (conf->wpa_key_mgmt & WPA_KEY_MGMT_PSK) { 71 RSN_SELECTOR_PUT(pos, WPA_AUTH_KEY_MGMT_PSK_OVER_802_1X); 72 pos += WPA_SELECTOR_LEN; 73 num_suites++; 74 } 75 76 if (num_suites == 0) { 77 wpa_printf(MSG_DEBUG, "Invalid key management type (%d).", 78 conf->wpa_key_mgmt); 79 return -1; 80 } 81 WPA_PUT_LE16(count, num_suites); 82 83 /* WPA Capabilities; use defaults, so no need to include it */ 84 85 hdr->len = (pos - buf) - 2; 86 87 return pos - buf; 88 } 89 90 91 int wpa_write_rsn_ie(struct wpa_auth_config *conf, u8 *buf, size_t len, 92 const u8 *pmkid) 93 { 94 struct rsn_ie_hdr *hdr; 95 int num_suites, res; 96 u8 *pos, *count; 97 u16 capab; 98 u32 suite; 99 100 hdr = (struct rsn_ie_hdr *) buf; 101 hdr->elem_id = WLAN_EID_RSN; 102 WPA_PUT_LE16(hdr->version, RSN_VERSION); 103 pos = (u8 *) (hdr + 1); 104 105 suite = wpa_cipher_to_suite(WPA_PROTO_RSN, conf->wpa_group); 106 if (suite == 0) { 107 wpa_printf(MSG_DEBUG, "Invalid group cipher (%d).", 108 conf->wpa_group); 109 return -1; 110 } 111 RSN_SELECTOR_PUT(pos, suite); 112 pos += RSN_SELECTOR_LEN; 113 114 num_suites = 0; 115 count = pos; 116 pos += 2; 117 118 #ifdef CONFIG_RSN_TESTING 119 if (rsn_testing) { 120 RSN_SELECTOR_PUT(pos, RSN_SELECTOR(0x12, 0x34, 0x56, 1)); 121 pos += RSN_SELECTOR_LEN; 122 num_suites++; 123 } 124 #endif /* CONFIG_RSN_TESTING */ 125 126 res = rsn_cipher_put_suites(pos, conf->rsn_pairwise); 127 num_suites += res; 128 pos += res * RSN_SELECTOR_LEN; 129 130 #ifdef CONFIG_RSN_TESTING 131 if (rsn_testing) { 132 RSN_SELECTOR_PUT(pos, RSN_SELECTOR(0x12, 0x34, 0x56, 2)); 133 pos += RSN_SELECTOR_LEN; 134 num_suites++; 135 } 136 #endif /* CONFIG_RSN_TESTING */ 137 138 if (num_suites == 0) { 139 wpa_printf(MSG_DEBUG, "Invalid pairwise cipher (%d).", 140 conf->rsn_pairwise); 141 return -1; 142 } 143 WPA_PUT_LE16(count, num_suites); 144 145 num_suites = 0; 146 count = pos; 147 pos += 2; 148 149 #ifdef CONFIG_RSN_TESTING 150 if (rsn_testing) { 151 RSN_SELECTOR_PUT(pos, RSN_SELECTOR(0x12, 0x34, 0x56, 1)); 152 pos += RSN_SELECTOR_LEN; 153 num_suites++; 154 } 155 #endif /* CONFIG_RSN_TESTING */ 156 157 if (conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X) { 158 RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_UNSPEC_802_1X); 159 pos += RSN_SELECTOR_LEN; 160 num_suites++; 161 } 162 if (conf->wpa_key_mgmt & WPA_KEY_MGMT_PSK) { 163 RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_PSK_OVER_802_1X); 164 pos += RSN_SELECTOR_LEN; 165 num_suites++; 166 } 167 #ifdef CONFIG_IEEE80211R_AP 168 if (conf->wpa_key_mgmt & WPA_KEY_MGMT_FT_IEEE8021X) { 169 RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_FT_802_1X); 170 pos += RSN_SELECTOR_LEN; 171 num_suites++; 172 } 173 if (conf->wpa_key_mgmt & WPA_KEY_MGMT_FT_PSK) { 174 RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_FT_PSK); 175 pos += RSN_SELECTOR_LEN; 176 num_suites++; 177 } 178 #endif /* CONFIG_IEEE80211R_AP */ 179 #ifdef CONFIG_IEEE80211W 180 if (conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X_SHA256) { 181 RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_802_1X_SHA256); 182 pos += RSN_SELECTOR_LEN; 183 num_suites++; 184 } 185 if (conf->wpa_key_mgmt & WPA_KEY_MGMT_PSK_SHA256) { 186 RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_PSK_SHA256); 187 pos += RSN_SELECTOR_LEN; 188 num_suites++; 189 } 190 #endif /* CONFIG_IEEE80211W */ 191 #ifdef CONFIG_SAE 192 if (conf->wpa_key_mgmt & WPA_KEY_MGMT_SAE) { 193 RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_SAE); 194 pos += RSN_SELECTOR_LEN; 195 num_suites++; 196 } 197 if (conf->wpa_key_mgmt & WPA_KEY_MGMT_FT_SAE) { 198 RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_FT_SAE); 199 pos += RSN_SELECTOR_LEN; 200 num_suites++; 201 } 202 #endif /* CONFIG_SAE */ 203 if (conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X_SUITE_B) { 204 RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_802_1X_SUITE_B); 205 pos += RSN_SELECTOR_LEN; 206 num_suites++; 207 } 208 if (conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X_SUITE_B_192) { 209 RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_802_1X_SUITE_B_192); 210 pos += RSN_SELECTOR_LEN; 211 num_suites++; 212 } 213 #ifdef CONFIG_FILS 214 if (conf->wpa_key_mgmt & WPA_KEY_MGMT_FILS_SHA256) { 215 RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_FILS_SHA256); 216 pos += RSN_SELECTOR_LEN; 217 num_suites++; 218 } 219 if (conf->wpa_key_mgmt & WPA_KEY_MGMT_FILS_SHA384) { 220 RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_FILS_SHA384); 221 pos += RSN_SELECTOR_LEN; 222 num_suites++; 223 } 224 #ifdef CONFIG_IEEE80211R_AP 225 if (conf->wpa_key_mgmt & WPA_KEY_MGMT_FT_FILS_SHA256) { 226 RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_FT_FILS_SHA256); 227 pos += RSN_SELECTOR_LEN; 228 num_suites++; 229 } 230 if (conf->wpa_key_mgmt & WPA_KEY_MGMT_FT_FILS_SHA384) { 231 RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_FT_FILS_SHA384); 232 pos += RSN_SELECTOR_LEN; 233 num_suites++; 234 } 235 #endif /* CONFIG_IEEE80211R_AP */ 236 #endif /* CONFIG_FILS */ 237 238 #ifdef CONFIG_RSN_TESTING 239 if (rsn_testing) { 240 RSN_SELECTOR_PUT(pos, RSN_SELECTOR(0x12, 0x34, 0x56, 2)); 241 pos += RSN_SELECTOR_LEN; 242 num_suites++; 243 } 244 #endif /* CONFIG_RSN_TESTING */ 245 246 if (num_suites == 0) { 247 wpa_printf(MSG_DEBUG, "Invalid key management type (%d).", 248 conf->wpa_key_mgmt); 249 return -1; 250 } 251 WPA_PUT_LE16(count, num_suites); 252 253 /* RSN Capabilities */ 254 capab = 0; 255 if (conf->rsn_preauth) 256 capab |= WPA_CAPABILITY_PREAUTH; 257 if (conf->peerkey) 258 capab |= WPA_CAPABILITY_PEERKEY_ENABLED; 259 if (conf->wmm_enabled) { 260 /* 4 PTKSA replay counters when using WMM */ 261 capab |= (RSN_NUM_REPLAY_COUNTERS_16 << 2); 262 } 263 #ifdef CONFIG_IEEE80211W 264 if (conf->ieee80211w != NO_MGMT_FRAME_PROTECTION) { 265 capab |= WPA_CAPABILITY_MFPC; 266 if (conf->ieee80211w == MGMT_FRAME_PROTECTION_REQUIRED) 267 capab |= WPA_CAPABILITY_MFPR; 268 } 269 #endif /* CONFIG_IEEE80211W */ 270 #ifdef CONFIG_RSN_TESTING 271 if (rsn_testing) 272 capab |= BIT(8) | BIT(14) | BIT(15); 273 #endif /* CONFIG_RSN_TESTING */ 274 WPA_PUT_LE16(pos, capab); 275 pos += 2; 276 277 if (pmkid) { 278 if (2 + PMKID_LEN > buf + len - pos) 279 return -1; 280 /* PMKID Count */ 281 WPA_PUT_LE16(pos, 1); 282 pos += 2; 283 os_memcpy(pos, pmkid, PMKID_LEN); 284 pos += PMKID_LEN; 285 } 286 287 #ifdef CONFIG_IEEE80211W 288 if (conf->ieee80211w != NO_MGMT_FRAME_PROTECTION && 289 conf->group_mgmt_cipher != WPA_CIPHER_AES_128_CMAC) { 290 if (2 + 4 > buf + len - pos) 291 return -1; 292 if (pmkid == NULL) { 293 /* PMKID Count */ 294 WPA_PUT_LE16(pos, 0); 295 pos += 2; 296 } 297 298 /* Management Group Cipher Suite */ 299 switch (conf->group_mgmt_cipher) { 300 case WPA_CIPHER_AES_128_CMAC: 301 RSN_SELECTOR_PUT(pos, RSN_CIPHER_SUITE_AES_128_CMAC); 302 break; 303 case WPA_CIPHER_BIP_GMAC_128: 304 RSN_SELECTOR_PUT(pos, RSN_CIPHER_SUITE_BIP_GMAC_128); 305 break; 306 case WPA_CIPHER_BIP_GMAC_256: 307 RSN_SELECTOR_PUT(pos, RSN_CIPHER_SUITE_BIP_GMAC_256); 308 break; 309 case WPA_CIPHER_BIP_CMAC_256: 310 RSN_SELECTOR_PUT(pos, RSN_CIPHER_SUITE_BIP_CMAC_256); 311 break; 312 default: 313 wpa_printf(MSG_DEBUG, 314 "Invalid group management cipher (0x%x)", 315 conf->group_mgmt_cipher); 316 return -1; 317 } 318 pos += RSN_SELECTOR_LEN; 319 } 320 #endif /* CONFIG_IEEE80211W */ 321 322 #ifdef CONFIG_RSN_TESTING 323 if (rsn_testing) { 324 /* 325 * Fill in any defined fields and add extra data to the end of 326 * the element. 327 */ 328 int pmkid_count_set = pmkid != NULL; 329 if (conf->ieee80211w != NO_MGMT_FRAME_PROTECTION) 330 pmkid_count_set = 1; 331 /* PMKID Count */ 332 WPA_PUT_LE16(pos, 0); 333 pos += 2; 334 if (conf->ieee80211w == NO_MGMT_FRAME_PROTECTION) { 335 /* Management Group Cipher Suite */ 336 RSN_SELECTOR_PUT(pos, RSN_CIPHER_SUITE_AES_128_CMAC); 337 pos += RSN_SELECTOR_LEN; 338 } 339 340 os_memset(pos, 0x12, 17); 341 pos += 17; 342 } 343 #endif /* CONFIG_RSN_TESTING */ 344 345 hdr->len = (pos - buf) - 2; 346 347 return pos - buf; 348 } 349 350 351 static u8 * wpa_write_osen(struct wpa_auth_config *conf, u8 *eid) 352 { 353 u8 *len; 354 u16 capab; 355 356 *eid++ = WLAN_EID_VENDOR_SPECIFIC; 357 len = eid++; /* to be filled */ 358 WPA_PUT_BE24(eid, OUI_WFA); 359 eid += 3; 360 *eid++ = HS20_OSEN_OUI_TYPE; 361 362 /* Group Data Cipher Suite */ 363 RSN_SELECTOR_PUT(eid, RSN_CIPHER_SUITE_NO_GROUP_ADDRESSED); 364 eid += RSN_SELECTOR_LEN; 365 366 /* Pairwise Cipher Suite Count and List */ 367 WPA_PUT_LE16(eid, 1); 368 eid += 2; 369 RSN_SELECTOR_PUT(eid, RSN_CIPHER_SUITE_CCMP); 370 eid += RSN_SELECTOR_LEN; 371 372 /* AKM Suite Count and List */ 373 WPA_PUT_LE16(eid, 1); 374 eid += 2; 375 RSN_SELECTOR_PUT(eid, RSN_AUTH_KEY_MGMT_OSEN); 376 eid += RSN_SELECTOR_LEN; 377 378 /* RSN Capabilities */ 379 capab = 0; 380 if (conf->wmm_enabled) { 381 /* 4 PTKSA replay counters when using WMM */ 382 capab |= (RSN_NUM_REPLAY_COUNTERS_16 << 2); 383 } 384 #ifdef CONFIG_IEEE80211W 385 if (conf->ieee80211w != NO_MGMT_FRAME_PROTECTION) { 386 capab |= WPA_CAPABILITY_MFPC; 387 if (conf->ieee80211w == MGMT_FRAME_PROTECTION_REQUIRED) 388 capab |= WPA_CAPABILITY_MFPR; 389 } 390 #endif /* CONFIG_IEEE80211W */ 391 WPA_PUT_LE16(eid, capab); 392 eid += 2; 393 394 *len = eid - len - 1; 395 396 return eid; 397 } 398 399 400 int wpa_auth_gen_wpa_ie(struct wpa_authenticator *wpa_auth) 401 { 402 u8 *pos, buf[128]; 403 int res; 404 405 #ifdef CONFIG_TESTING_OPTIONS 406 if (wpa_auth->conf.own_ie_override_len) { 407 wpa_hexdump(MSG_DEBUG, "WPA: Forced own IE(s) for testing", 408 wpa_auth->conf.own_ie_override, 409 wpa_auth->conf.own_ie_override_len); 410 os_free(wpa_auth->wpa_ie); 411 wpa_auth->wpa_ie = 412 os_malloc(wpa_auth->conf.own_ie_override_len); 413 if (wpa_auth->wpa_ie == NULL) 414 return -1; 415 os_memcpy(wpa_auth->wpa_ie, wpa_auth->conf.own_ie_override, 416 wpa_auth->conf.own_ie_override_len); 417 wpa_auth->wpa_ie_len = wpa_auth->conf.own_ie_override_len; 418 return 0; 419 } 420 #endif /* CONFIG_TESTING_OPTIONS */ 421 422 pos = buf; 423 424 if (wpa_auth->conf.wpa == WPA_PROTO_OSEN) { 425 pos = wpa_write_osen(&wpa_auth->conf, pos); 426 } 427 if (wpa_auth->conf.wpa & WPA_PROTO_RSN) { 428 res = wpa_write_rsn_ie(&wpa_auth->conf, 429 pos, buf + sizeof(buf) - pos, NULL); 430 if (res < 0) 431 return res; 432 pos += res; 433 } 434 #ifdef CONFIG_IEEE80211R_AP 435 if (wpa_key_mgmt_ft(wpa_auth->conf.wpa_key_mgmt)) { 436 res = wpa_write_mdie(&wpa_auth->conf, pos, 437 buf + sizeof(buf) - pos); 438 if (res < 0) 439 return res; 440 pos += res; 441 } 442 #endif /* CONFIG_IEEE80211R_AP */ 443 if (wpa_auth->conf.wpa & WPA_PROTO_WPA) { 444 res = wpa_write_wpa_ie(&wpa_auth->conf, 445 pos, buf + sizeof(buf) - pos); 446 if (res < 0) 447 return res; 448 pos += res; 449 } 450 451 os_free(wpa_auth->wpa_ie); 452 wpa_auth->wpa_ie = os_malloc(pos - buf); 453 if (wpa_auth->wpa_ie == NULL) 454 return -1; 455 os_memcpy(wpa_auth->wpa_ie, buf, pos - buf); 456 wpa_auth->wpa_ie_len = pos - buf; 457 458 return 0; 459 } 460 461 462 u8 * wpa_add_kde(u8 *pos, u32 kde, const u8 *data, size_t data_len, 463 const u8 *data2, size_t data2_len) 464 { 465 *pos++ = WLAN_EID_VENDOR_SPECIFIC; 466 *pos++ = RSN_SELECTOR_LEN + data_len + data2_len; 467 RSN_SELECTOR_PUT(pos, kde); 468 pos += RSN_SELECTOR_LEN; 469 os_memcpy(pos, data, data_len); 470 pos += data_len; 471 if (data2) { 472 os_memcpy(pos, data2, data2_len); 473 pos += data2_len; 474 } 475 return pos; 476 } 477 478 479 struct wpa_auth_okc_iter_data { 480 struct rsn_pmksa_cache_entry *pmksa; 481 const u8 *aa; 482 const u8 *spa; 483 const u8 *pmkid; 484 }; 485 486 487 static int wpa_auth_okc_iter(struct wpa_authenticator *a, void *ctx) 488 { 489 struct wpa_auth_okc_iter_data *data = ctx; 490 data->pmksa = pmksa_cache_get_okc(a->pmksa, data->aa, data->spa, 491 data->pmkid); 492 if (data->pmksa) 493 return 1; 494 return 0; 495 } 496 497 498 int wpa_validate_wpa_ie(struct wpa_authenticator *wpa_auth, 499 struct wpa_state_machine *sm, 500 const u8 *wpa_ie, size_t wpa_ie_len, 501 const u8 *mdie, size_t mdie_len) 502 { 503 struct wpa_ie_data data; 504 int ciphers, key_mgmt, res, version; 505 u32 selector; 506 size_t i; 507 const u8 *pmkid = NULL; 508 509 if (wpa_auth == NULL || sm == NULL) 510 return WPA_NOT_ENABLED; 511 512 if (wpa_ie == NULL || wpa_ie_len < 1) 513 return WPA_INVALID_IE; 514 515 if (wpa_ie[0] == WLAN_EID_RSN) 516 version = WPA_PROTO_RSN; 517 else 518 version = WPA_PROTO_WPA; 519 520 if (!(wpa_auth->conf.wpa & version)) { 521 wpa_printf(MSG_DEBUG, "Invalid WPA proto (%d) from " MACSTR, 522 version, MAC2STR(sm->addr)); 523 return WPA_INVALID_PROTO; 524 } 525 526 if (version == WPA_PROTO_RSN) { 527 res = wpa_parse_wpa_ie_rsn(wpa_ie, wpa_ie_len, &data); 528 529 selector = RSN_AUTH_KEY_MGMT_UNSPEC_802_1X; 530 if (0) { 531 } 532 else if (data.key_mgmt & WPA_KEY_MGMT_IEEE8021X_SUITE_B_192) 533 selector = RSN_AUTH_KEY_MGMT_802_1X_SUITE_B_192; 534 else if (data.key_mgmt & WPA_KEY_MGMT_IEEE8021X_SUITE_B) 535 selector = RSN_AUTH_KEY_MGMT_802_1X_SUITE_B; 536 #ifdef CONFIG_FILS 537 #ifdef CONFIG_IEEE80211R_AP 538 else if (data.key_mgmt & WPA_KEY_MGMT_FT_FILS_SHA384) 539 selector = RSN_AUTH_KEY_MGMT_FT_FILS_SHA384; 540 else if (data.key_mgmt & WPA_KEY_MGMT_FT_FILS_SHA256) 541 selector = RSN_AUTH_KEY_MGMT_FT_FILS_SHA256; 542 #endif /* CONFIG_IEEE80211R_AP */ 543 else if (data.key_mgmt & WPA_KEY_MGMT_FILS_SHA384) 544 selector = RSN_AUTH_KEY_MGMT_FILS_SHA384; 545 else if (data.key_mgmt & WPA_KEY_MGMT_FILS_SHA256) 546 selector = RSN_AUTH_KEY_MGMT_FILS_SHA256; 547 #endif /* CONFIG_FILS */ 548 #ifdef CONFIG_IEEE80211R_AP 549 else if (data.key_mgmt & WPA_KEY_MGMT_FT_IEEE8021X) 550 selector = RSN_AUTH_KEY_MGMT_FT_802_1X; 551 else if (data.key_mgmt & WPA_KEY_MGMT_FT_PSK) 552 selector = RSN_AUTH_KEY_MGMT_FT_PSK; 553 #endif /* CONFIG_IEEE80211R_AP */ 554 #ifdef CONFIG_IEEE80211W 555 else if (data.key_mgmt & WPA_KEY_MGMT_IEEE8021X_SHA256) 556 selector = RSN_AUTH_KEY_MGMT_802_1X_SHA256; 557 else if (data.key_mgmt & WPA_KEY_MGMT_PSK_SHA256) 558 selector = RSN_AUTH_KEY_MGMT_PSK_SHA256; 559 #endif /* CONFIG_IEEE80211W */ 560 #ifdef CONFIG_SAE 561 else if (data.key_mgmt & WPA_KEY_MGMT_SAE) 562 selector = RSN_AUTH_KEY_MGMT_SAE; 563 else if (data.key_mgmt & WPA_KEY_MGMT_FT_SAE) 564 selector = RSN_AUTH_KEY_MGMT_FT_SAE; 565 #endif /* CONFIG_SAE */ 566 else if (data.key_mgmt & WPA_KEY_MGMT_IEEE8021X) 567 selector = RSN_AUTH_KEY_MGMT_UNSPEC_802_1X; 568 else if (data.key_mgmt & WPA_KEY_MGMT_PSK) 569 selector = RSN_AUTH_KEY_MGMT_PSK_OVER_802_1X; 570 wpa_auth->dot11RSNAAuthenticationSuiteSelected = selector; 571 572 selector = wpa_cipher_to_suite(WPA_PROTO_RSN, 573 data.pairwise_cipher); 574 if (!selector) 575 selector = RSN_CIPHER_SUITE_CCMP; 576 wpa_auth->dot11RSNAPairwiseCipherSelected = selector; 577 578 selector = wpa_cipher_to_suite(WPA_PROTO_RSN, 579 data.group_cipher); 580 if (!selector) 581 selector = RSN_CIPHER_SUITE_CCMP; 582 wpa_auth->dot11RSNAGroupCipherSelected = selector; 583 } else { 584 res = wpa_parse_wpa_ie_wpa(wpa_ie, wpa_ie_len, &data); 585 586 selector = WPA_AUTH_KEY_MGMT_UNSPEC_802_1X; 587 if (data.key_mgmt & WPA_KEY_MGMT_IEEE8021X) 588 selector = WPA_AUTH_KEY_MGMT_UNSPEC_802_1X; 589 else if (data.key_mgmt & WPA_KEY_MGMT_PSK) 590 selector = WPA_AUTH_KEY_MGMT_PSK_OVER_802_1X; 591 wpa_auth->dot11RSNAAuthenticationSuiteSelected = selector; 592 593 selector = wpa_cipher_to_suite(WPA_PROTO_WPA, 594 data.pairwise_cipher); 595 if (!selector) 596 selector = RSN_CIPHER_SUITE_TKIP; 597 wpa_auth->dot11RSNAPairwiseCipherSelected = selector; 598 599 selector = wpa_cipher_to_suite(WPA_PROTO_WPA, 600 data.group_cipher); 601 if (!selector) 602 selector = WPA_CIPHER_SUITE_TKIP; 603 wpa_auth->dot11RSNAGroupCipherSelected = selector; 604 } 605 if (res) { 606 wpa_printf(MSG_DEBUG, "Failed to parse WPA/RSN IE from " 607 MACSTR " (res=%d)", MAC2STR(sm->addr), res); 608 wpa_hexdump(MSG_DEBUG, "WPA/RSN IE", wpa_ie, wpa_ie_len); 609 return WPA_INVALID_IE; 610 } 611 612 if (data.group_cipher != wpa_auth->conf.wpa_group) { 613 wpa_printf(MSG_DEBUG, "Invalid WPA group cipher (0x%x) from " 614 MACSTR, data.group_cipher, MAC2STR(sm->addr)); 615 return WPA_INVALID_GROUP; 616 } 617 618 key_mgmt = data.key_mgmt & wpa_auth->conf.wpa_key_mgmt; 619 if (!key_mgmt) { 620 wpa_printf(MSG_DEBUG, "Invalid WPA key mgmt (0x%x) from " 621 MACSTR, data.key_mgmt, MAC2STR(sm->addr)); 622 return WPA_INVALID_AKMP; 623 } 624 if (0) { 625 } 626 else if (key_mgmt & WPA_KEY_MGMT_IEEE8021X_SUITE_B_192) 627 sm->wpa_key_mgmt = WPA_KEY_MGMT_IEEE8021X_SUITE_B_192; 628 else if (key_mgmt & WPA_KEY_MGMT_IEEE8021X_SUITE_B) 629 sm->wpa_key_mgmt = WPA_KEY_MGMT_IEEE8021X_SUITE_B; 630 #ifdef CONFIG_FILS 631 #ifdef CONFIG_IEEE80211R_AP 632 else if (key_mgmt & WPA_KEY_MGMT_FT_FILS_SHA384) 633 sm->wpa_key_mgmt = WPA_KEY_MGMT_FT_FILS_SHA384; 634 else if (data.key_mgmt & WPA_KEY_MGMT_FT_FILS_SHA256) 635 sm->wpa_key_mgmt = WPA_KEY_MGMT_FT_FILS_SHA256; 636 #endif /* CONFIG_IEEE80211R_AP */ 637 else if (key_mgmt & WPA_KEY_MGMT_FILS_SHA384) 638 sm->wpa_key_mgmt = WPA_KEY_MGMT_FILS_SHA384; 639 else if (key_mgmt & WPA_KEY_MGMT_FILS_SHA256) 640 sm->wpa_key_mgmt = WPA_KEY_MGMT_FILS_SHA256; 641 #endif /* CONFIG_FILS */ 642 #ifdef CONFIG_IEEE80211R_AP 643 else if (key_mgmt & WPA_KEY_MGMT_FT_IEEE8021X) 644 sm->wpa_key_mgmt = WPA_KEY_MGMT_FT_IEEE8021X; 645 else if (key_mgmt & WPA_KEY_MGMT_FT_PSK) 646 sm->wpa_key_mgmt = WPA_KEY_MGMT_FT_PSK; 647 #endif /* CONFIG_IEEE80211R_AP */ 648 #ifdef CONFIG_IEEE80211W 649 else if (key_mgmt & WPA_KEY_MGMT_IEEE8021X_SHA256) 650 sm->wpa_key_mgmt = WPA_KEY_MGMT_IEEE8021X_SHA256; 651 else if (key_mgmt & WPA_KEY_MGMT_PSK_SHA256) 652 sm->wpa_key_mgmt = WPA_KEY_MGMT_PSK_SHA256; 653 #endif /* CONFIG_IEEE80211W */ 654 #ifdef CONFIG_SAE 655 else if (key_mgmt & WPA_KEY_MGMT_SAE) 656 sm->wpa_key_mgmt = WPA_KEY_MGMT_SAE; 657 else if (key_mgmt & WPA_KEY_MGMT_FT_SAE) 658 sm->wpa_key_mgmt = WPA_KEY_MGMT_FT_SAE; 659 #endif /* CONFIG_SAE */ 660 else if (key_mgmt & WPA_KEY_MGMT_IEEE8021X) 661 sm->wpa_key_mgmt = WPA_KEY_MGMT_IEEE8021X; 662 else 663 sm->wpa_key_mgmt = WPA_KEY_MGMT_PSK; 664 665 if (version == WPA_PROTO_RSN) 666 ciphers = data.pairwise_cipher & wpa_auth->conf.rsn_pairwise; 667 else 668 ciphers = data.pairwise_cipher & wpa_auth->conf.wpa_pairwise; 669 if (!ciphers) { 670 wpa_printf(MSG_DEBUG, "Invalid %s pairwise cipher (0x%x) " 671 "from " MACSTR, 672 version == WPA_PROTO_RSN ? "RSN" : "WPA", 673 data.pairwise_cipher, MAC2STR(sm->addr)); 674 return WPA_INVALID_PAIRWISE; 675 } 676 677 #ifdef CONFIG_IEEE80211W 678 if (wpa_auth->conf.ieee80211w == MGMT_FRAME_PROTECTION_REQUIRED) { 679 if (!(data.capabilities & WPA_CAPABILITY_MFPC)) { 680 wpa_printf(MSG_DEBUG, "Management frame protection " 681 "required, but client did not enable it"); 682 return WPA_MGMT_FRAME_PROTECTION_VIOLATION; 683 } 684 685 if (ciphers & WPA_CIPHER_TKIP) { 686 wpa_printf(MSG_DEBUG, "Management frame protection " 687 "cannot use TKIP"); 688 return WPA_MGMT_FRAME_PROTECTION_VIOLATION; 689 } 690 691 if (data.mgmt_group_cipher != wpa_auth->conf.group_mgmt_cipher) 692 { 693 wpa_printf(MSG_DEBUG, "Unsupported management group " 694 "cipher %d", data.mgmt_group_cipher); 695 return WPA_INVALID_MGMT_GROUP_CIPHER; 696 } 697 } 698 699 if (wpa_auth->conf.ieee80211w == NO_MGMT_FRAME_PROTECTION || 700 !(data.capabilities & WPA_CAPABILITY_MFPC)) 701 sm->mgmt_frame_prot = 0; 702 else 703 sm->mgmt_frame_prot = 1; 704 #endif /* CONFIG_IEEE80211W */ 705 706 #ifdef CONFIG_IEEE80211R_AP 707 if (wpa_key_mgmt_ft(sm->wpa_key_mgmt)) { 708 if (mdie == NULL || mdie_len < MOBILITY_DOMAIN_ID_LEN + 1) { 709 wpa_printf(MSG_DEBUG, "RSN: Trying to use FT, but " 710 "MDIE not included"); 711 return WPA_INVALID_MDIE; 712 } 713 if (os_memcmp(mdie, wpa_auth->conf.mobility_domain, 714 MOBILITY_DOMAIN_ID_LEN) != 0) { 715 wpa_hexdump(MSG_DEBUG, "RSN: Attempted to use unknown " 716 "MDIE", mdie, MOBILITY_DOMAIN_ID_LEN); 717 return WPA_INVALID_MDIE; 718 } 719 } else if (mdie != NULL) { 720 wpa_printf(MSG_DEBUG, 721 "RSN: Trying to use non-FT AKM suite, but MDIE included"); 722 return WPA_INVALID_AKMP; 723 } 724 #endif /* CONFIG_IEEE80211R_AP */ 725 726 sm->pairwise = wpa_pick_pairwise_cipher(ciphers, 0); 727 if (sm->pairwise < 0) 728 return WPA_INVALID_PAIRWISE; 729 730 /* TODO: clear WPA/WPA2 state if STA changes from one to another */ 731 if (wpa_ie[0] == WLAN_EID_RSN) 732 sm->wpa = WPA_VERSION_WPA2; 733 else 734 sm->wpa = WPA_VERSION_WPA; 735 736 sm->pmksa = NULL; 737 for (i = 0; i < data.num_pmkid; i++) { 738 wpa_hexdump(MSG_DEBUG, "RSN IE: STA PMKID", 739 &data.pmkid[i * PMKID_LEN], PMKID_LEN); 740 sm->pmksa = pmksa_cache_auth_get(wpa_auth->pmksa, sm->addr, 741 &data.pmkid[i * PMKID_LEN]); 742 if (sm->pmksa) { 743 pmkid = sm->pmksa->pmkid; 744 break; 745 } 746 } 747 for (i = 0; sm->pmksa == NULL && wpa_auth->conf.okc && 748 i < data.num_pmkid; i++) { 749 struct wpa_auth_okc_iter_data idata; 750 idata.pmksa = NULL; 751 idata.aa = wpa_auth->addr; 752 idata.spa = sm->addr; 753 idata.pmkid = &data.pmkid[i * PMKID_LEN]; 754 wpa_auth_for_each_auth(wpa_auth, wpa_auth_okc_iter, &idata); 755 if (idata.pmksa) { 756 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_DEBUG, 757 "OKC match for PMKID"); 758 sm->pmksa = pmksa_cache_add_okc(wpa_auth->pmksa, 759 idata.pmksa, 760 wpa_auth->addr, 761 idata.pmkid); 762 pmkid = idata.pmkid; 763 break; 764 } 765 } 766 if (sm->pmksa && pmkid) { 767 struct vlan_description *vlan; 768 769 vlan = sm->pmksa->vlan_desc; 770 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_DEBUG, 771 "PMKID found from PMKSA cache eap_type=%d vlan=%d%s", 772 sm->pmksa->eap_type_authsrv, 773 vlan ? vlan->untagged : 0, 774 (vlan && vlan->tagged[0]) ? "+" : ""); 775 os_memcpy(wpa_auth->dot11RSNAPMKIDUsed, pmkid, PMKID_LEN); 776 } 777 778 if (sm->wpa_ie == NULL || sm->wpa_ie_len < wpa_ie_len) { 779 os_free(sm->wpa_ie); 780 sm->wpa_ie = os_malloc(wpa_ie_len); 781 if (sm->wpa_ie == NULL) 782 return WPA_ALLOC_FAIL; 783 } 784 os_memcpy(sm->wpa_ie, wpa_ie, wpa_ie_len); 785 sm->wpa_ie_len = wpa_ie_len; 786 787 return WPA_IE_OK; 788 } 789 790 791 #ifdef CONFIG_HS20 792 int wpa_validate_osen(struct wpa_authenticator *wpa_auth, 793 struct wpa_state_machine *sm, 794 const u8 *osen_ie, size_t osen_ie_len) 795 { 796 if (wpa_auth == NULL || sm == NULL) 797 return -1; 798 799 /* TODO: parse OSEN element */ 800 sm->wpa_key_mgmt = WPA_KEY_MGMT_OSEN; 801 sm->mgmt_frame_prot = 1; 802 sm->pairwise = WPA_CIPHER_CCMP; 803 sm->wpa = WPA_VERSION_WPA2; 804 805 if (sm->wpa_ie == NULL || sm->wpa_ie_len < osen_ie_len) { 806 os_free(sm->wpa_ie); 807 sm->wpa_ie = os_malloc(osen_ie_len); 808 if (sm->wpa_ie == NULL) 809 return -1; 810 } 811 812 os_memcpy(sm->wpa_ie, osen_ie, osen_ie_len); 813 sm->wpa_ie_len = osen_ie_len; 814 815 return 0; 816 } 817 818 #endif /* CONFIG_HS20 */ 819 820 821 /** 822 * wpa_parse_generic - Parse EAPOL-Key Key Data Generic IEs 823 * @pos: Pointer to the IE header 824 * @end: Pointer to the end of the Key Data buffer 825 * @ie: Pointer to parsed IE data 826 * Returns: 0 on success, 1 if end mark is found, -1 on failure 827 */ 828 static int wpa_parse_generic(const u8 *pos, const u8 *end, 829 struct wpa_eapol_ie_parse *ie) 830 { 831 if (pos[1] == 0) 832 return 1; 833 834 if (pos[1] >= 6 && 835 RSN_SELECTOR_GET(pos + 2) == WPA_OUI_TYPE && 836 pos[2 + WPA_SELECTOR_LEN] == 1 && 837 pos[2 + WPA_SELECTOR_LEN + 1] == 0) { 838 ie->wpa_ie = pos; 839 ie->wpa_ie_len = pos[1] + 2; 840 return 0; 841 } 842 843 if (pos[1] >= 4 && WPA_GET_BE32(pos + 2) == OSEN_IE_VENDOR_TYPE) { 844 ie->osen = pos; 845 ie->osen_len = pos[1] + 2; 846 return 0; 847 } 848 849 if (1 + RSN_SELECTOR_LEN < end - pos && 850 pos[1] >= RSN_SELECTOR_LEN + PMKID_LEN && 851 RSN_SELECTOR_GET(pos + 2) == RSN_KEY_DATA_PMKID) { 852 ie->pmkid = pos + 2 + RSN_SELECTOR_LEN; 853 return 0; 854 } 855 856 if (pos[1] > RSN_SELECTOR_LEN + 2 && 857 RSN_SELECTOR_GET(pos + 2) == RSN_KEY_DATA_GROUPKEY) { 858 ie->gtk = pos + 2 + RSN_SELECTOR_LEN; 859 ie->gtk_len = pos[1] - RSN_SELECTOR_LEN; 860 return 0; 861 } 862 863 if (pos[1] > RSN_SELECTOR_LEN + 2 && 864 RSN_SELECTOR_GET(pos + 2) == RSN_KEY_DATA_MAC_ADDR) { 865 ie->mac_addr = pos + 2 + RSN_SELECTOR_LEN; 866 ie->mac_addr_len = pos[1] - RSN_SELECTOR_LEN; 867 return 0; 868 } 869 870 #ifdef CONFIG_PEERKEY 871 if (pos[1] > RSN_SELECTOR_LEN + 2 && 872 RSN_SELECTOR_GET(pos + 2) == RSN_KEY_DATA_SMK) { 873 ie->smk = pos + 2 + RSN_SELECTOR_LEN; 874 ie->smk_len = pos[1] - RSN_SELECTOR_LEN; 875 return 0; 876 } 877 878 if (pos[1] > RSN_SELECTOR_LEN + 2 && 879 RSN_SELECTOR_GET(pos + 2) == RSN_KEY_DATA_NONCE) { 880 ie->nonce = pos + 2 + RSN_SELECTOR_LEN; 881 ie->nonce_len = pos[1] - RSN_SELECTOR_LEN; 882 return 0; 883 } 884 885 if (pos[1] > RSN_SELECTOR_LEN + 2 && 886 RSN_SELECTOR_GET(pos + 2) == RSN_KEY_DATA_LIFETIME) { 887 ie->lifetime = pos + 2 + RSN_SELECTOR_LEN; 888 ie->lifetime_len = pos[1] - RSN_SELECTOR_LEN; 889 return 0; 890 } 891 892 if (pos[1] > RSN_SELECTOR_LEN + 2 && 893 RSN_SELECTOR_GET(pos + 2) == RSN_KEY_DATA_ERROR) { 894 ie->error = pos + 2 + RSN_SELECTOR_LEN; 895 ie->error_len = pos[1] - RSN_SELECTOR_LEN; 896 return 0; 897 } 898 #endif /* CONFIG_PEERKEY */ 899 900 #ifdef CONFIG_IEEE80211W 901 if (pos[1] > RSN_SELECTOR_LEN + 2 && 902 RSN_SELECTOR_GET(pos + 2) == RSN_KEY_DATA_IGTK) { 903 ie->igtk = pos + 2 + RSN_SELECTOR_LEN; 904 ie->igtk_len = pos[1] - RSN_SELECTOR_LEN; 905 return 0; 906 } 907 #endif /* CONFIG_IEEE80211W */ 908 909 #ifdef CONFIG_P2P 910 if (pos[1] >= RSN_SELECTOR_LEN + 1 && 911 RSN_SELECTOR_GET(pos + 2) == WFA_KEY_DATA_IP_ADDR_REQ) { 912 ie->ip_addr_req = pos + 2 + RSN_SELECTOR_LEN; 913 wpa_hexdump(MSG_DEBUG, "WPA: IP Address Request in EAPOL-Key", 914 ie->ip_addr_req, pos[1] - RSN_SELECTOR_LEN); 915 return 0; 916 } 917 918 if (pos[1] >= RSN_SELECTOR_LEN + 3 * 4 && 919 RSN_SELECTOR_GET(pos + 2) == WFA_KEY_DATA_IP_ADDR_ALLOC) { 920 ie->ip_addr_alloc = pos + 2 + RSN_SELECTOR_LEN; 921 wpa_hexdump(MSG_DEBUG, 922 "WPA: IP Address Allocation in EAPOL-Key", 923 ie->ip_addr_alloc, pos[1] - RSN_SELECTOR_LEN); 924 return 0; 925 } 926 #endif /* CONFIG_P2P */ 927 928 return 0; 929 } 930 931 932 /** 933 * wpa_parse_kde_ies - Parse EAPOL-Key Key Data IEs 934 * @buf: Pointer to the Key Data buffer 935 * @len: Key Data Length 936 * @ie: Pointer to parsed IE data 937 * Returns: 0 on success, -1 on failure 938 */ 939 int wpa_parse_kde_ies(const u8 *buf, size_t len, struct wpa_eapol_ie_parse *ie) 940 { 941 const u8 *pos, *end; 942 int ret = 0; 943 944 os_memset(ie, 0, sizeof(*ie)); 945 for (pos = buf, end = pos + len; end - pos > 1; pos += 2 + pos[1]) { 946 if (pos[0] == 0xdd && 947 ((pos == buf + len - 1) || pos[1] == 0)) { 948 /* Ignore padding */ 949 break; 950 } 951 if (2 + pos[1] > end - pos) { 952 wpa_printf(MSG_DEBUG, "WPA: EAPOL-Key Key Data " 953 "underflow (ie=%d len=%d pos=%d)", 954 pos[0], pos[1], (int) (pos - buf)); 955 wpa_hexdump_key(MSG_DEBUG, "WPA: Key Data", 956 buf, len); 957 ret = -1; 958 break; 959 } 960 if (*pos == WLAN_EID_RSN) { 961 ie->rsn_ie = pos; 962 ie->rsn_ie_len = pos[1] + 2; 963 #ifdef CONFIG_IEEE80211R_AP 964 } else if (*pos == WLAN_EID_MOBILITY_DOMAIN) { 965 ie->mdie = pos; 966 ie->mdie_len = pos[1] + 2; 967 } else if (*pos == WLAN_EID_FAST_BSS_TRANSITION) { 968 ie->ftie = pos; 969 ie->ftie_len = pos[1] + 2; 970 #endif /* CONFIG_IEEE80211R_AP */ 971 } else if (*pos == WLAN_EID_VENDOR_SPECIFIC) { 972 ret = wpa_parse_generic(pos, end, ie); 973 if (ret < 0) 974 break; 975 if (ret > 0) { 976 ret = 0; 977 break; 978 } 979 } else { 980 wpa_hexdump(MSG_DEBUG, "WPA: Unrecognized EAPOL-Key " 981 "Key Data IE", pos, 2 + pos[1]); 982 } 983 } 984 985 return ret; 986 } 987 988 989 int wpa_auth_uses_mfp(struct wpa_state_machine *sm) 990 { 991 return sm ? sm->mgmt_frame_prot : 0; 992 } 993