1 /* 2 * WPA/RSN - Shared functions for supplicant and authenticator 3 * Copyright (c) 2002-2008, Jouni Malinen <j (at) w1.fi> 4 * 5 * This program is free software; you can redistribute it and/or modify 6 * it under the terms of the GNU General Public License version 2 as 7 * published by the Free Software Foundation. 8 * 9 * Alternatively, this software may be distributed under the terms of BSD 10 * license. 11 * 12 * See README and COPYING for more details. 13 */ 14 15 #include "includes.h" 16 17 #include "common.h" 18 #include "crypto/md5.h" 19 #include "crypto/sha1.h" 20 #include "crypto/sha256.h" 21 #include "crypto/aes_wrap.h" 22 #include "crypto/crypto.h" 23 #include "ieee802_11_defs.h" 24 #include "defs.h" 25 #include "wpa_common.h" 26 27 28 /** 29 * wpa_eapol_key_mic - Calculate EAPOL-Key MIC 30 * @key: EAPOL-Key Key Confirmation Key (KCK) 31 * @ver: Key descriptor version (WPA_KEY_INFO_TYPE_*) 32 * @buf: Pointer to the beginning of the EAPOL header (version field) 33 * @len: Length of the EAPOL frame (from EAPOL header to the end of the frame) 34 * @mic: Pointer to the buffer to which the EAPOL-Key MIC is written 35 * Returns: 0 on success, -1 on failure 36 * 37 * Calculate EAPOL-Key MIC for an EAPOL-Key packet. The EAPOL-Key MIC field has 38 * to be cleared (all zeroes) when calling this function. 39 * 40 * Note: 'IEEE Std 802.11i-2004 - 8.5.2 EAPOL-Key frames' has an error in the 41 * description of the Key MIC calculation. It includes packet data from the 42 * beginning of the EAPOL-Key header, not EAPOL header. This incorrect change 43 * happened during final editing of the standard and the correct behavior is 44 * defined in the last draft (IEEE 802.11i/D10). 45 */ 46 int wpa_eapol_key_mic(const u8 *key, int ver, const u8 *buf, size_t len, 47 u8 *mic) 48 { 49 u8 hash[SHA1_MAC_LEN]; 50 51 switch (ver) { 52 case WPA_KEY_INFO_TYPE_HMAC_MD5_RC4: 53 return hmac_md5(key, 16, buf, len, mic); 54 case WPA_KEY_INFO_TYPE_HMAC_SHA1_AES: 55 if (hmac_sha1(key, 16, buf, len, hash)) 56 return -1; 57 os_memcpy(mic, hash, MD5_MAC_LEN); 58 break; 59 #if defined(CONFIG_IEEE80211R) || defined(CONFIG_IEEE80211W) 60 case WPA_KEY_INFO_TYPE_AES_128_CMAC: 61 return omac1_aes_128(key, buf, len, mic); 62 #endif /* CONFIG_IEEE80211R || CONFIG_IEEE80211W */ 63 default: 64 return -1; 65 } 66 67 return 0; 68 } 69 70 71 /** 72 * wpa_pmk_to_ptk - Calculate PTK from PMK, addresses, and nonces 73 * @pmk: Pairwise master key 74 * @pmk_len: Length of PMK 75 * @label: Label to use in derivation 76 * @addr1: AA or SA 77 * @addr2: SA or AA 78 * @nonce1: ANonce or SNonce 79 * @nonce2: SNonce or ANonce 80 * @ptk: Buffer for pairwise transient key 81 * @ptk_len: Length of PTK 82 * @use_sha256: Whether to use SHA256-based KDF 83 * 84 * IEEE Std 802.11i-2004 - 8.5.1.2 Pairwise key hierarchy 85 * PTK = PRF-X(PMK, "Pairwise key expansion", 86 * Min(AA, SA) || Max(AA, SA) || 87 * Min(ANonce, SNonce) || Max(ANonce, SNonce)) 88 * 89 * STK = PRF-X(SMK, "Peer key expansion", 90 * Min(MAC_I, MAC_P) || Max(MAC_I, MAC_P) || 91 * Min(INonce, PNonce) || Max(INonce, PNonce)) 92 */ 93 void wpa_pmk_to_ptk(const u8 *pmk, size_t pmk_len, const char *label, 94 const u8 *addr1, const u8 *addr2, 95 const u8 *nonce1, const u8 *nonce2, 96 u8 *ptk, size_t ptk_len, int use_sha256) 97 { 98 u8 data[2 * ETH_ALEN + 2 * WPA_NONCE_LEN]; 99 100 if (os_memcmp(addr1, addr2, ETH_ALEN) < 0) { 101 os_memcpy(data, addr1, ETH_ALEN); 102 os_memcpy(data + ETH_ALEN, addr2, ETH_ALEN); 103 } else { 104 os_memcpy(data, addr2, ETH_ALEN); 105 os_memcpy(data + ETH_ALEN, addr1, ETH_ALEN); 106 } 107 108 if (os_memcmp(nonce1, nonce2, WPA_NONCE_LEN) < 0) { 109 os_memcpy(data + 2 * ETH_ALEN, nonce1, WPA_NONCE_LEN); 110 os_memcpy(data + 2 * ETH_ALEN + WPA_NONCE_LEN, nonce2, 111 WPA_NONCE_LEN); 112 } else { 113 os_memcpy(data + 2 * ETH_ALEN, nonce2, WPA_NONCE_LEN); 114 os_memcpy(data + 2 * ETH_ALEN + WPA_NONCE_LEN, nonce1, 115 WPA_NONCE_LEN); 116 } 117 118 #ifdef CONFIG_IEEE80211W 119 if (use_sha256) 120 sha256_prf(pmk, pmk_len, label, data, sizeof(data), 121 ptk, ptk_len); 122 else 123 #endif /* CONFIG_IEEE80211W */ 124 sha1_prf(pmk, pmk_len, label, data, sizeof(data), ptk, 125 ptk_len); 126 127 wpa_printf(MSG_DEBUG, "WPA: PTK derivation - A1=" MACSTR " A2=" MACSTR, 128 MAC2STR(addr1), MAC2STR(addr2)); 129 wpa_hexdump(MSG_DEBUG, "WPA: Nonce1", nonce1, WPA_NONCE_LEN); 130 wpa_hexdump(MSG_DEBUG, "WPA: Nonce2", nonce2, WPA_NONCE_LEN); 131 wpa_hexdump_key(MSG_DEBUG, "WPA: PMK", pmk, pmk_len); 132 wpa_hexdump_key(MSG_DEBUG, "WPA: PTK", ptk, ptk_len); 133 } 134 135 136 #ifdef CONFIG_IEEE80211R 137 int wpa_ft_mic(const u8 *kck, const u8 *sta_addr, const u8 *ap_addr, 138 u8 transaction_seqnum, const u8 *mdie, size_t mdie_len, 139 const u8 *ftie, size_t ftie_len, 140 const u8 *rsnie, size_t rsnie_len, 141 const u8 *ric, size_t ric_len, u8 *mic) 142 { 143 u8 *buf, *pos; 144 size_t buf_len; 145 146 buf_len = 2 * ETH_ALEN + 1 + mdie_len + ftie_len + rsnie_len + ric_len; 147 buf = os_malloc(buf_len); 148 if (buf == NULL) 149 return -1; 150 151 pos = buf; 152 os_memcpy(pos, sta_addr, ETH_ALEN); 153 pos += ETH_ALEN; 154 os_memcpy(pos, ap_addr, ETH_ALEN); 155 pos += ETH_ALEN; 156 *pos++ = transaction_seqnum; 157 if (rsnie) { 158 os_memcpy(pos, rsnie, rsnie_len); 159 pos += rsnie_len; 160 } 161 if (mdie) { 162 os_memcpy(pos, mdie, mdie_len); 163 pos += mdie_len; 164 } 165 if (ftie) { 166 struct rsn_ftie *_ftie; 167 os_memcpy(pos, ftie, ftie_len); 168 if (ftie_len < 2 + sizeof(*_ftie)) { 169 os_free(buf); 170 return -1; 171 } 172 _ftie = (struct rsn_ftie *) (pos + 2); 173 os_memset(_ftie->mic, 0, sizeof(_ftie->mic)); 174 pos += ftie_len; 175 } 176 if (ric) { 177 os_memcpy(pos, ric, ric_len); 178 pos += ric_len; 179 } 180 181 wpa_hexdump(MSG_MSGDUMP, "FT: MIC data", buf, pos - buf); 182 if (omac1_aes_128(kck, buf, pos - buf, mic)) { 183 os_free(buf); 184 return -1; 185 } 186 187 os_free(buf); 188 189 return 0; 190 } 191 #endif /* CONFIG_IEEE80211R */ 192 193 194 #ifndef CONFIG_NO_WPA2 195 static int rsn_selector_to_bitfield(const u8 *s) 196 { 197 if (RSN_SELECTOR_GET(s) == RSN_CIPHER_SUITE_NONE) 198 return WPA_CIPHER_NONE; 199 if (RSN_SELECTOR_GET(s) == RSN_CIPHER_SUITE_WEP40) 200 return WPA_CIPHER_WEP40; 201 if (RSN_SELECTOR_GET(s) == RSN_CIPHER_SUITE_TKIP) 202 return WPA_CIPHER_TKIP; 203 if (RSN_SELECTOR_GET(s) == RSN_CIPHER_SUITE_CCMP) 204 return WPA_CIPHER_CCMP; 205 if (RSN_SELECTOR_GET(s) == RSN_CIPHER_SUITE_WEP104) 206 return WPA_CIPHER_WEP104; 207 #ifdef CONFIG_IEEE80211W 208 if (RSN_SELECTOR_GET(s) == RSN_CIPHER_SUITE_AES_128_CMAC) 209 return WPA_CIPHER_AES_128_CMAC; 210 #endif /* CONFIG_IEEE80211W */ 211 return 0; 212 } 213 214 215 static int rsn_key_mgmt_to_bitfield(const u8 *s) 216 { 217 if (RSN_SELECTOR_GET(s) == RSN_AUTH_KEY_MGMT_UNSPEC_802_1X) 218 return WPA_KEY_MGMT_IEEE8021X; 219 if (RSN_SELECTOR_GET(s) == RSN_AUTH_KEY_MGMT_PSK_OVER_802_1X) 220 return WPA_KEY_MGMT_PSK; 221 #ifdef CONFIG_IEEE80211R 222 if (RSN_SELECTOR_GET(s) == RSN_AUTH_KEY_MGMT_FT_802_1X) 223 return WPA_KEY_MGMT_FT_IEEE8021X; 224 if (RSN_SELECTOR_GET(s) == RSN_AUTH_KEY_MGMT_FT_PSK) 225 return WPA_KEY_MGMT_FT_PSK; 226 #endif /* CONFIG_IEEE80211R */ 227 #ifdef CONFIG_IEEE80211W 228 if (RSN_SELECTOR_GET(s) == RSN_AUTH_KEY_MGMT_802_1X_SHA256) 229 return WPA_KEY_MGMT_IEEE8021X_SHA256; 230 if (RSN_SELECTOR_GET(s) == RSN_AUTH_KEY_MGMT_PSK_SHA256) 231 return WPA_KEY_MGMT_PSK_SHA256; 232 #endif /* CONFIG_IEEE80211W */ 233 return 0; 234 } 235 #endif /* CONFIG_NO_WPA2 */ 236 237 238 /** 239 * wpa_parse_wpa_ie_rsn - Parse RSN IE 240 * @rsn_ie: Buffer containing RSN IE 241 * @rsn_ie_len: RSN IE buffer length (including IE number and length octets) 242 * @data: Pointer to structure that will be filled in with parsed data 243 * Returns: 0 on success, <0 on failure 244 */ 245 int wpa_parse_wpa_ie_rsn(const u8 *rsn_ie, size_t rsn_ie_len, 246 struct wpa_ie_data *data) 247 { 248 #ifndef CONFIG_NO_WPA2 249 const struct rsn_ie_hdr *hdr; 250 const u8 *pos; 251 int left; 252 int i, count; 253 254 os_memset(data, 0, sizeof(*data)); 255 data->proto = WPA_PROTO_RSN; 256 data->pairwise_cipher = WPA_CIPHER_CCMP; 257 data->group_cipher = WPA_CIPHER_CCMP; 258 data->key_mgmt = WPA_KEY_MGMT_IEEE8021X; 259 data->capabilities = 0; 260 data->pmkid = NULL; 261 data->num_pmkid = 0; 262 #ifdef CONFIG_IEEE80211W 263 data->mgmt_group_cipher = WPA_CIPHER_AES_128_CMAC; 264 #else /* CONFIG_IEEE80211W */ 265 data->mgmt_group_cipher = 0; 266 #endif /* CONFIG_IEEE80211W */ 267 268 if (rsn_ie_len == 0) { 269 /* No RSN IE - fail silently */ 270 return -1; 271 } 272 273 if (rsn_ie_len < sizeof(struct rsn_ie_hdr)) { 274 wpa_printf(MSG_DEBUG, "%s: ie len too short %lu", 275 __func__, (unsigned long) rsn_ie_len); 276 return -1; 277 } 278 279 hdr = (const struct rsn_ie_hdr *) rsn_ie; 280 281 if (hdr->elem_id != WLAN_EID_RSN || 282 hdr->len != rsn_ie_len - 2 || 283 WPA_GET_LE16(hdr->version) != RSN_VERSION) { 284 wpa_printf(MSG_DEBUG, "%s: malformed ie or unknown version", 285 __func__); 286 return -2; 287 } 288 289 pos = (const u8 *) (hdr + 1); 290 left = rsn_ie_len - sizeof(*hdr); 291 292 if (left >= RSN_SELECTOR_LEN) { 293 data->group_cipher = rsn_selector_to_bitfield(pos); 294 #ifdef CONFIG_IEEE80211W 295 if (data->group_cipher == WPA_CIPHER_AES_128_CMAC) { 296 wpa_printf(MSG_DEBUG, "%s: AES-128-CMAC used as group " 297 "cipher", __func__); 298 return -1; 299 } 300 #endif /* CONFIG_IEEE80211W */ 301 pos += RSN_SELECTOR_LEN; 302 left -= RSN_SELECTOR_LEN; 303 } else if (left > 0) { 304 wpa_printf(MSG_DEBUG, "%s: ie length mismatch, %u too much", 305 __func__, left); 306 return -3; 307 } 308 309 if (left >= 2) { 310 data->pairwise_cipher = 0; 311 count = WPA_GET_LE16(pos); 312 pos += 2; 313 left -= 2; 314 if (count == 0 || left < count * RSN_SELECTOR_LEN) { 315 wpa_printf(MSG_DEBUG, "%s: ie count botch (pairwise), " 316 "count %u left %u", __func__, count, left); 317 return -4; 318 } 319 for (i = 0; i < count; i++) { 320 data->pairwise_cipher |= rsn_selector_to_bitfield(pos); 321 pos += RSN_SELECTOR_LEN; 322 left -= RSN_SELECTOR_LEN; 323 } 324 #ifdef CONFIG_IEEE80211W 325 if (data->pairwise_cipher & WPA_CIPHER_AES_128_CMAC) { 326 wpa_printf(MSG_DEBUG, "%s: AES-128-CMAC used as " 327 "pairwise cipher", __func__); 328 return -1; 329 } 330 #endif /* CONFIG_IEEE80211W */ 331 } else if (left == 1) { 332 wpa_printf(MSG_DEBUG, "%s: ie too short (for key mgmt)", 333 __func__); 334 return -5; 335 } 336 337 if (left >= 2) { 338 data->key_mgmt = 0; 339 count = WPA_GET_LE16(pos); 340 pos += 2; 341 left -= 2; 342 if (count == 0 || left < count * RSN_SELECTOR_LEN) { 343 wpa_printf(MSG_DEBUG, "%s: ie count botch (key mgmt), " 344 "count %u left %u", __func__, count, left); 345 return -6; 346 } 347 for (i = 0; i < count; i++) { 348 data->key_mgmt |= rsn_key_mgmt_to_bitfield(pos); 349 pos += RSN_SELECTOR_LEN; 350 left -= RSN_SELECTOR_LEN; 351 } 352 } else if (left == 1) { 353 wpa_printf(MSG_DEBUG, "%s: ie too short (for capabilities)", 354 __func__); 355 return -7; 356 } 357 358 if (left >= 2) { 359 data->capabilities = WPA_GET_LE16(pos); 360 pos += 2; 361 left -= 2; 362 } 363 364 if (left >= 2) { 365 data->num_pmkid = WPA_GET_LE16(pos); 366 pos += 2; 367 left -= 2; 368 if (left < (int) data->num_pmkid * PMKID_LEN) { 369 wpa_printf(MSG_DEBUG, "%s: PMKID underflow " 370 "(num_pmkid=%lu left=%d)", 371 __func__, (unsigned long) data->num_pmkid, 372 left); 373 data->num_pmkid = 0; 374 return -9; 375 } else { 376 data->pmkid = pos; 377 pos += data->num_pmkid * PMKID_LEN; 378 left -= data->num_pmkid * PMKID_LEN; 379 } 380 } 381 382 #ifdef CONFIG_IEEE80211W 383 if (left >= 4) { 384 data->mgmt_group_cipher = rsn_selector_to_bitfield(pos); 385 if (data->mgmt_group_cipher != WPA_CIPHER_AES_128_CMAC) { 386 wpa_printf(MSG_DEBUG, "%s: Unsupported management " 387 "group cipher 0x%x", __func__, 388 data->mgmt_group_cipher); 389 return -10; 390 } 391 pos += RSN_SELECTOR_LEN; 392 left -= RSN_SELECTOR_LEN; 393 } 394 #endif /* CONFIG_IEEE80211W */ 395 396 if (left > 0) { 397 wpa_printf(MSG_DEBUG, "%s: ie has %u trailing bytes - ignored", 398 __func__, left); 399 } 400 401 return 0; 402 #else /* CONFIG_NO_WPA2 */ 403 return -1; 404 #endif /* CONFIG_NO_WPA2 */ 405 } 406 407 408 static int wpa_selector_to_bitfield(const u8 *s) 409 { 410 if (RSN_SELECTOR_GET(s) == WPA_CIPHER_SUITE_NONE) 411 return WPA_CIPHER_NONE; 412 if (RSN_SELECTOR_GET(s) == WPA_CIPHER_SUITE_WEP40) 413 return WPA_CIPHER_WEP40; 414 if (RSN_SELECTOR_GET(s) == WPA_CIPHER_SUITE_TKIP) 415 return WPA_CIPHER_TKIP; 416 if (RSN_SELECTOR_GET(s) == WPA_CIPHER_SUITE_CCMP) 417 return WPA_CIPHER_CCMP; 418 if (RSN_SELECTOR_GET(s) == WPA_CIPHER_SUITE_WEP104) 419 return WPA_CIPHER_WEP104; 420 return 0; 421 } 422 423 424 static int wpa_key_mgmt_to_bitfield(const u8 *s) 425 { 426 if (RSN_SELECTOR_GET(s) == WPA_AUTH_KEY_MGMT_UNSPEC_802_1X) 427 return WPA_KEY_MGMT_IEEE8021X; 428 if (RSN_SELECTOR_GET(s) == WPA_AUTH_KEY_MGMT_PSK_OVER_802_1X) 429 return WPA_KEY_MGMT_PSK; 430 if (RSN_SELECTOR_GET(s) == WPA_AUTH_KEY_MGMT_NONE) 431 return WPA_KEY_MGMT_WPA_NONE; 432 return 0; 433 } 434 435 436 int wpa_parse_wpa_ie_wpa(const u8 *wpa_ie, size_t wpa_ie_len, 437 struct wpa_ie_data *data) 438 { 439 const struct wpa_ie_hdr *hdr; 440 const u8 *pos; 441 int left; 442 int i, count; 443 444 os_memset(data, 0, sizeof(*data)); 445 data->proto = WPA_PROTO_WPA; 446 data->pairwise_cipher = WPA_CIPHER_TKIP; 447 data->group_cipher = WPA_CIPHER_TKIP; 448 data->key_mgmt = WPA_KEY_MGMT_IEEE8021X; 449 data->capabilities = 0; 450 data->pmkid = NULL; 451 data->num_pmkid = 0; 452 data->mgmt_group_cipher = 0; 453 454 if (wpa_ie_len == 0) { 455 /* No WPA IE - fail silently */ 456 return -1; 457 } 458 459 if (wpa_ie_len < sizeof(struct wpa_ie_hdr)) { 460 wpa_printf(MSG_DEBUG, "%s: ie len too short %lu", 461 __func__, (unsigned long) wpa_ie_len); 462 return -1; 463 } 464 465 hdr = (const struct wpa_ie_hdr *) wpa_ie; 466 467 if (hdr->elem_id != WLAN_EID_VENDOR_SPECIFIC || 468 hdr->len != wpa_ie_len - 2 || 469 RSN_SELECTOR_GET(hdr->oui) != WPA_OUI_TYPE || 470 WPA_GET_LE16(hdr->version) != WPA_VERSION) { 471 wpa_printf(MSG_DEBUG, "%s: malformed ie or unknown version", 472 __func__); 473 return -2; 474 } 475 476 pos = (const u8 *) (hdr + 1); 477 left = wpa_ie_len - sizeof(*hdr); 478 479 if (left >= WPA_SELECTOR_LEN) { 480 data->group_cipher = wpa_selector_to_bitfield(pos); 481 pos += WPA_SELECTOR_LEN; 482 left -= WPA_SELECTOR_LEN; 483 } else if (left > 0) { 484 wpa_printf(MSG_DEBUG, "%s: ie length mismatch, %u too much", 485 __func__, left); 486 return -3; 487 } 488 489 if (left >= 2) { 490 data->pairwise_cipher = 0; 491 count = WPA_GET_LE16(pos); 492 pos += 2; 493 left -= 2; 494 if (count == 0 || left < count * WPA_SELECTOR_LEN) { 495 wpa_printf(MSG_DEBUG, "%s: ie count botch (pairwise), " 496 "count %u left %u", __func__, count, left); 497 return -4; 498 } 499 for (i = 0; i < count; i++) { 500 data->pairwise_cipher |= wpa_selector_to_bitfield(pos); 501 pos += WPA_SELECTOR_LEN; 502 left -= WPA_SELECTOR_LEN; 503 } 504 } else if (left == 1) { 505 wpa_printf(MSG_DEBUG, "%s: ie too short (for key mgmt)", 506 __func__); 507 return -5; 508 } 509 510 if (left >= 2) { 511 data->key_mgmt = 0; 512 count = WPA_GET_LE16(pos); 513 pos += 2; 514 left -= 2; 515 if (count == 0 || left < count * WPA_SELECTOR_LEN) { 516 wpa_printf(MSG_DEBUG, "%s: ie count botch (key mgmt), " 517 "count %u left %u", __func__, count, left); 518 return -6; 519 } 520 for (i = 0; i < count; i++) { 521 data->key_mgmt |= wpa_key_mgmt_to_bitfield(pos); 522 pos += WPA_SELECTOR_LEN; 523 left -= WPA_SELECTOR_LEN; 524 } 525 } else if (left == 1) { 526 wpa_printf(MSG_DEBUG, "%s: ie too short (for capabilities)", 527 __func__); 528 return -7; 529 } 530 531 if (left >= 2) { 532 data->capabilities = WPA_GET_LE16(pos); 533 pos += 2; 534 left -= 2; 535 } 536 537 if (left > 0) { 538 wpa_printf(MSG_DEBUG, "%s: ie has %u trailing bytes - ignored", 539 __func__, left); 540 } 541 542 return 0; 543 } 544 545 546 #ifdef CONFIG_IEEE80211R 547 548 /** 549 * wpa_derive_pmk_r0 - Derive PMK-R0 and PMKR0Name 550 * 551 * IEEE Std 802.11r-2008 - 8.5.1.5.3 552 */ 553 void wpa_derive_pmk_r0(const u8 *xxkey, size_t xxkey_len, 554 const u8 *ssid, size_t ssid_len, 555 const u8 *mdid, const u8 *r0kh_id, size_t r0kh_id_len, 556 const u8 *s0kh_id, u8 *pmk_r0, u8 *pmk_r0_name) 557 { 558 u8 buf[1 + WPA_MAX_SSID_LEN + MOBILITY_DOMAIN_ID_LEN + 1 + 559 FT_R0KH_ID_MAX_LEN + ETH_ALEN]; 560 u8 *pos, r0_key_data[48], hash[32]; 561 const u8 *addr[2]; 562 size_t len[2]; 563 564 /* 565 * R0-Key-Data = KDF-384(XXKey, "FT-R0", 566 * SSIDlength || SSID || MDID || R0KHlength || 567 * R0KH-ID || S0KH-ID) 568 * XXKey is either the second 256 bits of MSK or PSK. 569 * PMK-R0 = L(R0-Key-Data, 0, 256) 570 * PMK-R0Name-Salt = L(R0-Key-Data, 256, 128) 571 */ 572 if (ssid_len > WPA_MAX_SSID_LEN || r0kh_id_len > FT_R0KH_ID_MAX_LEN) 573 return; 574 pos = buf; 575 *pos++ = ssid_len; 576 os_memcpy(pos, ssid, ssid_len); 577 pos += ssid_len; 578 os_memcpy(pos, mdid, MOBILITY_DOMAIN_ID_LEN); 579 pos += MOBILITY_DOMAIN_ID_LEN; 580 *pos++ = r0kh_id_len; 581 os_memcpy(pos, r0kh_id, r0kh_id_len); 582 pos += r0kh_id_len; 583 os_memcpy(pos, s0kh_id, ETH_ALEN); 584 pos += ETH_ALEN; 585 586 sha256_prf(xxkey, xxkey_len, "FT-R0", buf, pos - buf, 587 r0_key_data, sizeof(r0_key_data)); 588 os_memcpy(pmk_r0, r0_key_data, PMK_LEN); 589 590 /* 591 * PMKR0Name = Truncate-128(SHA-256("FT-R0N" || PMK-R0Name-Salt) 592 */ 593 addr[0] = (const u8 *) "FT-R0N"; 594 len[0] = 6; 595 addr[1] = r0_key_data + PMK_LEN; 596 len[1] = 16; 597 598 sha256_vector(2, addr, len, hash); 599 os_memcpy(pmk_r0_name, hash, WPA_PMK_NAME_LEN); 600 } 601 602 603 /** 604 * wpa_derive_pmk_r1_name - Derive PMKR1Name 605 * 606 * IEEE Std 802.11r-2008 - 8.5.1.5.4 607 */ 608 void wpa_derive_pmk_r1_name(const u8 *pmk_r0_name, const u8 *r1kh_id, 609 const u8 *s1kh_id, u8 *pmk_r1_name) 610 { 611 u8 hash[32]; 612 const u8 *addr[4]; 613 size_t len[4]; 614 615 /* 616 * PMKR1Name = Truncate-128(SHA-256("FT-R1N" || PMKR0Name || 617 * R1KH-ID || S1KH-ID)) 618 */ 619 addr[0] = (const u8 *) "FT-R1N"; 620 len[0] = 6; 621 addr[1] = pmk_r0_name; 622 len[1] = WPA_PMK_NAME_LEN; 623 addr[2] = r1kh_id; 624 len[2] = FT_R1KH_ID_LEN; 625 addr[3] = s1kh_id; 626 len[3] = ETH_ALEN; 627 628 sha256_vector(4, addr, len, hash); 629 os_memcpy(pmk_r1_name, hash, WPA_PMK_NAME_LEN); 630 } 631 632 633 /** 634 * wpa_derive_pmk_r1 - Derive PMK-R1 and PMKR1Name from PMK-R0 635 * 636 * IEEE Std 802.11r-2008 - 8.5.1.5.4 637 */ 638 void wpa_derive_pmk_r1(const u8 *pmk_r0, const u8 *pmk_r0_name, 639 const u8 *r1kh_id, const u8 *s1kh_id, 640 u8 *pmk_r1, u8 *pmk_r1_name) 641 { 642 u8 buf[FT_R1KH_ID_LEN + ETH_ALEN]; 643 u8 *pos; 644 645 /* PMK-R1 = KDF-256(PMK-R0, "FT-R1", R1KH-ID || S1KH-ID) */ 646 pos = buf; 647 os_memcpy(pos, r1kh_id, FT_R1KH_ID_LEN); 648 pos += FT_R1KH_ID_LEN; 649 os_memcpy(pos, s1kh_id, ETH_ALEN); 650 pos += ETH_ALEN; 651 652 sha256_prf(pmk_r0, PMK_LEN, "FT-R1", buf, pos - buf, pmk_r1, PMK_LEN); 653 654 wpa_derive_pmk_r1_name(pmk_r0_name, r1kh_id, s1kh_id, pmk_r1_name); 655 } 656 657 658 /** 659 * wpa_pmk_r1_to_ptk - Derive PTK and PTKName from PMK-R1 660 * 661 * IEEE Std 802.11r-2008 - 8.5.1.5.5 662 */ 663 void wpa_pmk_r1_to_ptk(const u8 *pmk_r1, const u8 *snonce, const u8 *anonce, 664 const u8 *sta_addr, const u8 *bssid, 665 const u8 *pmk_r1_name, 666 u8 *ptk, size_t ptk_len, u8 *ptk_name) 667 { 668 u8 buf[2 * WPA_NONCE_LEN + 2 * ETH_ALEN]; 669 u8 *pos, hash[32]; 670 const u8 *addr[6]; 671 size_t len[6]; 672 673 /* 674 * PTK = KDF-PTKLen(PMK-R1, "FT-PTK", SNonce || ANonce || 675 * BSSID || STA-ADDR) 676 */ 677 pos = buf; 678 os_memcpy(pos, snonce, WPA_NONCE_LEN); 679 pos += WPA_NONCE_LEN; 680 os_memcpy(pos, anonce, WPA_NONCE_LEN); 681 pos += WPA_NONCE_LEN; 682 os_memcpy(pos, bssid, ETH_ALEN); 683 pos += ETH_ALEN; 684 os_memcpy(pos, sta_addr, ETH_ALEN); 685 pos += ETH_ALEN; 686 687 sha256_prf(pmk_r1, PMK_LEN, "FT-PTK", buf, pos - buf, ptk, ptk_len); 688 689 /* 690 * PTKName = Truncate-128(SHA-256(PMKR1Name || "FT-PTKN" || SNonce || 691 * ANonce || BSSID || STA-ADDR)) 692 */ 693 addr[0] = pmk_r1_name; 694 len[0] = WPA_PMK_NAME_LEN; 695 addr[1] = (const u8 *) "FT-PTKN"; 696 len[1] = 7; 697 addr[2] = snonce; 698 len[2] = WPA_NONCE_LEN; 699 addr[3] = anonce; 700 len[3] = WPA_NONCE_LEN; 701 addr[4] = bssid; 702 len[4] = ETH_ALEN; 703 addr[5] = sta_addr; 704 len[5] = ETH_ALEN; 705 706 sha256_vector(6, addr, len, hash); 707 os_memcpy(ptk_name, hash, WPA_PMK_NAME_LEN); 708 } 709 710 #endif /* CONFIG_IEEE80211R */ 711 712 713 /** 714 * rsn_pmkid - Calculate PMK identifier 715 * @pmk: Pairwise master key 716 * @pmk_len: Length of pmk in bytes 717 * @aa: Authenticator address 718 * @spa: Supplicant address 719 * @pmkid: Buffer for PMKID 720 * @use_sha256: Whether to use SHA256-based KDF 721 * 722 * IEEE Std 802.11i-2004 - 8.5.1.2 Pairwise key hierarchy 723 * PMKID = HMAC-SHA1-128(PMK, "PMK Name" || AA || SPA) 724 */ 725 void rsn_pmkid(const u8 *pmk, size_t pmk_len, const u8 *aa, const u8 *spa, 726 u8 *pmkid, int use_sha256) 727 { 728 char *title = "PMK Name"; 729 const u8 *addr[3]; 730 const size_t len[3] = { 8, ETH_ALEN, ETH_ALEN }; 731 unsigned char hash[SHA256_MAC_LEN]; 732 733 addr[0] = (u8 *) title; 734 addr[1] = aa; 735 addr[2] = spa; 736 737 #ifdef CONFIG_IEEE80211W 738 if (use_sha256) 739 hmac_sha256_vector(pmk, pmk_len, 3, addr, len, hash); 740 else 741 #endif /* CONFIG_IEEE80211W */ 742 hmac_sha1_vector(pmk, pmk_len, 3, addr, len, hash); 743 os_memcpy(pmkid, hash, PMKID_LEN); 744 } 745 746 747 /** 748 * wpa_cipher_txt - Convert cipher suite to a text string 749 * @cipher: Cipher suite (WPA_CIPHER_* enum) 750 * Returns: Pointer to a text string of the cipher suite name 751 */ 752 const char * wpa_cipher_txt(int cipher) 753 { 754 switch (cipher) { 755 case WPA_CIPHER_NONE: 756 return "NONE"; 757 case WPA_CIPHER_WEP40: 758 return "WEP-40"; 759 case WPA_CIPHER_WEP104: 760 return "WEP-104"; 761 case WPA_CIPHER_TKIP: 762 return "TKIP"; 763 case WPA_CIPHER_CCMP: 764 return "CCMP"; 765 case WPA_CIPHER_CCMP | WPA_CIPHER_TKIP: 766 return "CCMP+TKIP"; 767 default: 768 return "UNKNOWN"; 769 } 770 } 771 772 773 /** 774 * wpa_key_mgmt_txt - Convert key management suite to a text string 775 * @key_mgmt: Key management suite (WPA_KEY_MGMT_* enum) 776 * @proto: WPA/WPA2 version (WPA_PROTO_*) 777 * Returns: Pointer to a text string of the key management suite name 778 */ 779 const char * wpa_key_mgmt_txt(int key_mgmt, int proto) 780 { 781 switch (key_mgmt) { 782 case WPA_KEY_MGMT_IEEE8021X: 783 if (proto == (WPA_PROTO_RSN | WPA_PROTO_WPA)) 784 return "WPA2+WPA/IEEE 802.1X/EAP"; 785 return proto == WPA_PROTO_RSN ? 786 "WPA2/IEEE 802.1X/EAP" : "WPA/IEEE 802.1X/EAP"; 787 case WPA_KEY_MGMT_PSK: 788 if (proto == (WPA_PROTO_RSN | WPA_PROTO_WPA)) 789 return "WPA2-PSK+WPA-PSK"; 790 return proto == WPA_PROTO_RSN ? 791 "WPA2-PSK" : "WPA-PSK"; 792 case WPA_KEY_MGMT_NONE: 793 return "NONE"; 794 case WPA_KEY_MGMT_IEEE8021X_NO_WPA: 795 return "IEEE 802.1X (no WPA)"; 796 #ifdef CONFIG_IEEE80211R 797 case WPA_KEY_MGMT_FT_IEEE8021X: 798 return "FT-EAP"; 799 case WPA_KEY_MGMT_FT_PSK: 800 return "FT-PSK"; 801 #endif /* CONFIG_IEEE80211R */ 802 #ifdef CONFIG_IEEE80211W 803 case WPA_KEY_MGMT_IEEE8021X_SHA256: 804 return "WPA2-EAP-SHA256"; 805 case WPA_KEY_MGMT_PSK_SHA256: 806 return "WPA2-PSK-SHA256"; 807 #endif /* CONFIG_IEEE80211W */ 808 default: 809 return "UNKNOWN"; 810 } 811 } 812 813 814 int wpa_compare_rsn_ie(int ft_initial_assoc, 815 const u8 *ie1, size_t ie1len, 816 const u8 *ie2, size_t ie2len) 817 { 818 if (ie1 == NULL || ie2 == NULL) 819 return -1; 820 821 if (ie1len == ie2len && os_memcmp(ie1, ie2, ie1len) == 0) 822 return 0; /* identical IEs */ 823 824 #ifdef CONFIG_IEEE80211R 825 if (ft_initial_assoc) { 826 struct wpa_ie_data ie1d, ie2d; 827 /* 828 * The PMKID-List in RSN IE is different between Beacon/Probe 829 * Response/(Re)Association Request frames and EAPOL-Key 830 * messages in FT initial mobility domain association. Allow 831 * for this, but verify that other parts of the RSN IEs are 832 * identical. 833 */ 834 if (wpa_parse_wpa_ie_rsn(ie1, ie1len, &ie1d) < 0 || 835 wpa_parse_wpa_ie_rsn(ie2, ie2len, &ie2d) < 0) 836 return -1; 837 if (ie1d.proto == ie2d.proto && 838 ie1d.pairwise_cipher == ie2d.pairwise_cipher && 839 ie1d.group_cipher == ie2d.group_cipher && 840 ie1d.key_mgmt == ie2d.key_mgmt && 841 ie1d.capabilities == ie2d.capabilities && 842 ie1d.mgmt_group_cipher == ie2d.mgmt_group_cipher) 843 return 0; 844 } 845 #endif /* CONFIG_IEEE80211R */ 846 847 return -1; 848 } 849 850 851 #ifdef CONFIG_IEEE80211R 852 int wpa_insert_pmkid(u8 *ies, size_t ies_len, const u8 *pmkid) 853 { 854 u8 *start, *end, *rpos, *rend; 855 int added = 0; 856 857 start = ies; 858 end = ies + ies_len; 859 860 while (start < end) { 861 if (*start == WLAN_EID_RSN) 862 break; 863 start += 2 + start[1]; 864 } 865 if (start >= end) { 866 wpa_printf(MSG_ERROR, "FT: Could not find RSN IE in " 867 "IEs data"); 868 return -1; 869 } 870 wpa_hexdump(MSG_DEBUG, "FT: RSN IE before modification", 871 start, 2 + start[1]); 872 873 /* Find start of PMKID-Count */ 874 rpos = start + 2; 875 rend = rpos + start[1]; 876 877 /* Skip Version and Group Data Cipher Suite */ 878 rpos += 2 + 4; 879 /* Skip Pairwise Cipher Suite Count and List */ 880 rpos += 2 + WPA_GET_LE16(rpos) * RSN_SELECTOR_LEN; 881 /* Skip AKM Suite Count and List */ 882 rpos += 2 + WPA_GET_LE16(rpos) * RSN_SELECTOR_LEN; 883 884 if (rpos == rend) { 885 /* Add RSN Capabilities */ 886 os_memmove(rpos + 2, rpos, end - rpos); 887 *rpos++ = 0; 888 *rpos++ = 0; 889 } else { 890 /* Skip RSN Capabilities */ 891 rpos += 2; 892 if (rpos > rend) { 893 wpa_printf(MSG_ERROR, "FT: Could not parse RSN IE in " 894 "IEs data"); 895 return -1; 896 } 897 } 898 899 if (rpos == rend) { 900 /* No PMKID-Count field included; add it */ 901 os_memmove(rpos + 2 + PMKID_LEN, rpos, end - rpos); 902 WPA_PUT_LE16(rpos, 1); 903 rpos += 2; 904 os_memcpy(rpos, pmkid, PMKID_LEN); 905 added += 2 + PMKID_LEN; 906 start[1] += 2 + PMKID_LEN; 907 } else { 908 /* PMKID-Count was included; use it */ 909 if (WPA_GET_LE16(rpos) != 0) { 910 wpa_printf(MSG_ERROR, "FT: Unexpected PMKID " 911 "in RSN IE in EAPOL-Key data"); 912 return -1; 913 } 914 WPA_PUT_LE16(rpos, 1); 915 rpos += 2; 916 os_memmove(rpos + PMKID_LEN, rpos, end - rpos); 917 os_memcpy(rpos, pmkid, PMKID_LEN); 918 added += PMKID_LEN; 919 start[1] += PMKID_LEN; 920 } 921 922 wpa_hexdump(MSG_DEBUG, "FT: RSN IE after modification " 923 "(PMKID inserted)", start, 2 + start[1]); 924 925 return added; 926 } 927 #endif /* CONFIG_IEEE80211R */ 928