Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1 | /* |
| 2 | * WPA/RSN - Shared functions for supplicant and authenticator |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 3 | * Copyright (c) 2002-2013, Jouni Malinen <j@w1.fi> |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4 | * |
Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 5 | * This software may be distributed under the terms of the BSD license. |
| 6 | * See README for more details. |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | #include "includes.h" |
| 10 | |
| 11 | #include "common.h" |
| 12 | #include "crypto/md5.h" |
| 13 | #include "crypto/sha1.h" |
| 14 | #include "crypto/sha256.h" |
| 15 | #include "crypto/aes_wrap.h" |
| 16 | #include "crypto/crypto.h" |
| 17 | #include "ieee802_11_defs.h" |
| 18 | #include "defs.h" |
| 19 | #include "wpa_common.h" |
| 20 | |
| 21 | |
| 22 | /** |
| 23 | * wpa_eapol_key_mic - Calculate EAPOL-Key MIC |
| 24 | * @key: EAPOL-Key Key Confirmation Key (KCK) |
Dmitry Shmidt | fb45fd5 | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 25 | * @akmp: WPA_KEY_MGMT_* used in key derivation |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 26 | * @ver: Key descriptor version (WPA_KEY_INFO_TYPE_*) |
| 27 | * @buf: Pointer to the beginning of the EAPOL header (version field) |
| 28 | * @len: Length of the EAPOL frame (from EAPOL header to the end of the frame) |
| 29 | * @mic: Pointer to the buffer to which the EAPOL-Key MIC is written |
| 30 | * Returns: 0 on success, -1 on failure |
| 31 | * |
| 32 | * Calculate EAPOL-Key MIC for an EAPOL-Key packet. The EAPOL-Key MIC field has |
| 33 | * to be cleared (all zeroes) when calling this function. |
| 34 | * |
| 35 | * Note: 'IEEE Std 802.11i-2004 - 8.5.2 EAPOL-Key frames' has an error in the |
| 36 | * description of the Key MIC calculation. It includes packet data from the |
| 37 | * beginning of the EAPOL-Key header, not EAPOL header. This incorrect change |
| 38 | * happened during final editing of the standard and the correct behavior is |
| 39 | * defined in the last draft (IEEE 802.11i/D10). |
| 40 | */ |
Dmitry Shmidt | fb45fd5 | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 41 | int wpa_eapol_key_mic(const u8 *key, int akmp, int ver, const u8 *buf, |
| 42 | size_t len, u8 *mic) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 43 | { |
Dmitry Shmidt | fb45fd5 | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 44 | u8 hash[SHA256_MAC_LEN]; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 45 | |
| 46 | switch (ver) { |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 47 | #ifndef CONFIG_FIPS |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 48 | case WPA_KEY_INFO_TYPE_HMAC_MD5_RC4: |
| 49 | return hmac_md5(key, 16, buf, len, mic); |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 50 | #endif /* CONFIG_FIPS */ |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 51 | case WPA_KEY_INFO_TYPE_HMAC_SHA1_AES: |
| 52 | if (hmac_sha1(key, 16, buf, len, hash)) |
| 53 | return -1; |
| 54 | os_memcpy(mic, hash, MD5_MAC_LEN); |
| 55 | break; |
| 56 | #if defined(CONFIG_IEEE80211R) || defined(CONFIG_IEEE80211W) |
| 57 | case WPA_KEY_INFO_TYPE_AES_128_CMAC: |
| 58 | return omac1_aes_128(key, buf, len, mic); |
| 59 | #endif /* CONFIG_IEEE80211R || CONFIG_IEEE80211W */ |
Dmitry Shmidt | f21452a | 2014-02-26 10:55:25 -0800 | [diff] [blame] | 60 | case WPA_KEY_INFO_TYPE_AKM_DEFINED: |
Dmitry Shmidt | fb45fd5 | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 61 | switch (akmp) { |
| 62 | #ifdef CONFIG_HS20 |
| 63 | case WPA_KEY_MGMT_OSEN: |
| 64 | return omac1_aes_128(key, buf, len, mic); |
Dmitry Shmidt | f21452a | 2014-02-26 10:55:25 -0800 | [diff] [blame] | 65 | #endif /* CONFIG_HS20 */ |
Dmitry Shmidt | fb45fd5 | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 66 | #ifdef CONFIG_SUITEB |
| 67 | case WPA_KEY_MGMT_IEEE8021X_SUITE_B: |
| 68 | if (hmac_sha256(key, 16, buf, len, hash)) |
| 69 | return -1; |
| 70 | os_memcpy(mic, hash, MD5_MAC_LEN); |
| 71 | break; |
| 72 | #endif /* CONFIG_SUITEB */ |
| 73 | default: |
| 74 | return -1; |
| 75 | } |
| 76 | break; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 77 | default: |
| 78 | return -1; |
| 79 | } |
| 80 | |
| 81 | return 0; |
| 82 | } |
| 83 | |
| 84 | |
| 85 | /** |
| 86 | * wpa_pmk_to_ptk - Calculate PTK from PMK, addresses, and nonces |
| 87 | * @pmk: Pairwise master key |
| 88 | * @pmk_len: Length of PMK |
| 89 | * @label: Label to use in derivation |
| 90 | * @addr1: AA or SA |
| 91 | * @addr2: SA or AA |
| 92 | * @nonce1: ANonce or SNonce |
| 93 | * @nonce2: SNonce or ANonce |
| 94 | * @ptk: Buffer for pairwise transient key |
| 95 | * @ptk_len: Length of PTK |
| 96 | * @use_sha256: Whether to use SHA256-based KDF |
| 97 | * |
| 98 | * IEEE Std 802.11i-2004 - 8.5.1.2 Pairwise key hierarchy |
| 99 | * PTK = PRF-X(PMK, "Pairwise key expansion", |
| 100 | * Min(AA, SA) || Max(AA, SA) || |
| 101 | * Min(ANonce, SNonce) || Max(ANonce, SNonce)) |
| 102 | * |
| 103 | * STK = PRF-X(SMK, "Peer key expansion", |
| 104 | * Min(MAC_I, MAC_P) || Max(MAC_I, MAC_P) || |
| 105 | * Min(INonce, PNonce) || Max(INonce, PNonce)) |
| 106 | */ |
| 107 | void wpa_pmk_to_ptk(const u8 *pmk, size_t pmk_len, const char *label, |
| 108 | const u8 *addr1, const u8 *addr2, |
| 109 | const u8 *nonce1, const u8 *nonce2, |
| 110 | u8 *ptk, size_t ptk_len, int use_sha256) |
| 111 | { |
| 112 | u8 data[2 * ETH_ALEN + 2 * WPA_NONCE_LEN]; |
| 113 | |
| 114 | if (os_memcmp(addr1, addr2, ETH_ALEN) < 0) { |
| 115 | os_memcpy(data, addr1, ETH_ALEN); |
| 116 | os_memcpy(data + ETH_ALEN, addr2, ETH_ALEN); |
| 117 | } else { |
| 118 | os_memcpy(data, addr2, ETH_ALEN); |
| 119 | os_memcpy(data + ETH_ALEN, addr1, ETH_ALEN); |
| 120 | } |
| 121 | |
| 122 | if (os_memcmp(nonce1, nonce2, WPA_NONCE_LEN) < 0) { |
| 123 | os_memcpy(data + 2 * ETH_ALEN, nonce1, WPA_NONCE_LEN); |
| 124 | os_memcpy(data + 2 * ETH_ALEN + WPA_NONCE_LEN, nonce2, |
| 125 | WPA_NONCE_LEN); |
| 126 | } else { |
| 127 | os_memcpy(data + 2 * ETH_ALEN, nonce2, WPA_NONCE_LEN); |
| 128 | os_memcpy(data + 2 * ETH_ALEN + WPA_NONCE_LEN, nonce1, |
| 129 | WPA_NONCE_LEN); |
| 130 | } |
| 131 | |
| 132 | #ifdef CONFIG_IEEE80211W |
| 133 | if (use_sha256) |
| 134 | sha256_prf(pmk, pmk_len, label, data, sizeof(data), |
| 135 | ptk, ptk_len); |
| 136 | else |
| 137 | #endif /* CONFIG_IEEE80211W */ |
| 138 | sha1_prf(pmk, pmk_len, label, data, sizeof(data), ptk, |
| 139 | ptk_len); |
| 140 | |
| 141 | wpa_printf(MSG_DEBUG, "WPA: PTK derivation - A1=" MACSTR " A2=" MACSTR, |
| 142 | MAC2STR(addr1), MAC2STR(addr2)); |
| 143 | wpa_hexdump(MSG_DEBUG, "WPA: Nonce1", nonce1, WPA_NONCE_LEN); |
| 144 | wpa_hexdump(MSG_DEBUG, "WPA: Nonce2", nonce2, WPA_NONCE_LEN); |
| 145 | wpa_hexdump_key(MSG_DEBUG, "WPA: PMK", pmk, pmk_len); |
| 146 | wpa_hexdump_key(MSG_DEBUG, "WPA: PTK", ptk, ptk_len); |
| 147 | } |
| 148 | |
| 149 | |
| 150 | #ifdef CONFIG_IEEE80211R |
| 151 | int wpa_ft_mic(const u8 *kck, const u8 *sta_addr, const u8 *ap_addr, |
| 152 | u8 transaction_seqnum, const u8 *mdie, size_t mdie_len, |
| 153 | const u8 *ftie, size_t ftie_len, |
| 154 | const u8 *rsnie, size_t rsnie_len, |
| 155 | const u8 *ric, size_t ric_len, u8 *mic) |
| 156 | { |
| 157 | u8 *buf, *pos; |
| 158 | size_t buf_len; |
| 159 | |
| 160 | buf_len = 2 * ETH_ALEN + 1 + mdie_len + ftie_len + rsnie_len + ric_len; |
| 161 | buf = os_malloc(buf_len); |
| 162 | if (buf == NULL) |
| 163 | return -1; |
| 164 | |
| 165 | pos = buf; |
| 166 | os_memcpy(pos, sta_addr, ETH_ALEN); |
| 167 | pos += ETH_ALEN; |
| 168 | os_memcpy(pos, ap_addr, ETH_ALEN); |
| 169 | pos += ETH_ALEN; |
| 170 | *pos++ = transaction_seqnum; |
| 171 | if (rsnie) { |
| 172 | os_memcpy(pos, rsnie, rsnie_len); |
| 173 | pos += rsnie_len; |
| 174 | } |
| 175 | if (mdie) { |
| 176 | os_memcpy(pos, mdie, mdie_len); |
| 177 | pos += mdie_len; |
| 178 | } |
| 179 | if (ftie) { |
| 180 | struct rsn_ftie *_ftie; |
| 181 | os_memcpy(pos, ftie, ftie_len); |
| 182 | if (ftie_len < 2 + sizeof(*_ftie)) { |
| 183 | os_free(buf); |
| 184 | return -1; |
| 185 | } |
| 186 | _ftie = (struct rsn_ftie *) (pos + 2); |
| 187 | os_memset(_ftie->mic, 0, sizeof(_ftie->mic)); |
| 188 | pos += ftie_len; |
| 189 | } |
| 190 | if (ric) { |
| 191 | os_memcpy(pos, ric, ric_len); |
| 192 | pos += ric_len; |
| 193 | } |
| 194 | |
| 195 | wpa_hexdump(MSG_MSGDUMP, "FT: MIC data", buf, pos - buf); |
| 196 | if (omac1_aes_128(kck, buf, pos - buf, mic)) { |
| 197 | os_free(buf); |
| 198 | return -1; |
| 199 | } |
| 200 | |
| 201 | os_free(buf); |
| 202 | |
| 203 | return 0; |
| 204 | } |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 205 | |
| 206 | |
| 207 | static int wpa_ft_parse_ftie(const u8 *ie, size_t ie_len, |
| 208 | struct wpa_ft_ies *parse) |
| 209 | { |
| 210 | const u8 *end, *pos; |
| 211 | |
| 212 | parse->ftie = ie; |
| 213 | parse->ftie_len = ie_len; |
| 214 | |
| 215 | pos = ie + sizeof(struct rsn_ftie); |
| 216 | end = ie + ie_len; |
| 217 | |
| 218 | while (pos + 2 <= end && pos + 2 + pos[1] <= end) { |
| 219 | switch (pos[0]) { |
| 220 | case FTIE_SUBELEM_R1KH_ID: |
| 221 | if (pos[1] != FT_R1KH_ID_LEN) { |
| 222 | wpa_printf(MSG_DEBUG, "FT: Invalid R1KH-ID " |
| 223 | "length in FTIE: %d", pos[1]); |
| 224 | return -1; |
| 225 | } |
| 226 | parse->r1kh_id = pos + 2; |
| 227 | break; |
| 228 | case FTIE_SUBELEM_GTK: |
| 229 | parse->gtk = pos + 2; |
| 230 | parse->gtk_len = pos[1]; |
| 231 | break; |
| 232 | case FTIE_SUBELEM_R0KH_ID: |
| 233 | if (pos[1] < 1 || pos[1] > FT_R0KH_ID_MAX_LEN) { |
| 234 | wpa_printf(MSG_DEBUG, "FT: Invalid R0KH-ID " |
| 235 | "length in FTIE: %d", pos[1]); |
| 236 | return -1; |
| 237 | } |
| 238 | parse->r0kh_id = pos + 2; |
| 239 | parse->r0kh_id_len = pos[1]; |
| 240 | break; |
| 241 | #ifdef CONFIG_IEEE80211W |
| 242 | case FTIE_SUBELEM_IGTK: |
| 243 | parse->igtk = pos + 2; |
| 244 | parse->igtk_len = pos[1]; |
| 245 | break; |
| 246 | #endif /* CONFIG_IEEE80211W */ |
| 247 | } |
| 248 | |
| 249 | pos += 2 + pos[1]; |
| 250 | } |
| 251 | |
| 252 | return 0; |
| 253 | } |
| 254 | |
| 255 | |
| 256 | int wpa_ft_parse_ies(const u8 *ies, size_t ies_len, |
| 257 | struct wpa_ft_ies *parse) |
| 258 | { |
| 259 | const u8 *end, *pos; |
| 260 | struct wpa_ie_data data; |
| 261 | int ret; |
| 262 | const struct rsn_ftie *ftie; |
| 263 | int prot_ie_count = 0; |
| 264 | |
| 265 | os_memset(parse, 0, sizeof(*parse)); |
| 266 | if (ies == NULL) |
| 267 | return 0; |
| 268 | |
| 269 | pos = ies; |
| 270 | end = ies + ies_len; |
| 271 | while (pos + 2 <= end && pos + 2 + pos[1] <= end) { |
| 272 | switch (pos[0]) { |
| 273 | case WLAN_EID_RSN: |
| 274 | parse->rsn = pos + 2; |
| 275 | parse->rsn_len = pos[1]; |
| 276 | ret = wpa_parse_wpa_ie_rsn(parse->rsn - 2, |
| 277 | parse->rsn_len + 2, |
| 278 | &data); |
| 279 | if (ret < 0) { |
| 280 | wpa_printf(MSG_DEBUG, "FT: Failed to parse " |
| 281 | "RSN IE: %d", ret); |
| 282 | return -1; |
| 283 | } |
| 284 | if (data.num_pmkid == 1 && data.pmkid) |
| 285 | parse->rsn_pmkid = data.pmkid; |
| 286 | break; |
| 287 | case WLAN_EID_MOBILITY_DOMAIN: |
| 288 | parse->mdie = pos + 2; |
| 289 | parse->mdie_len = pos[1]; |
| 290 | break; |
| 291 | case WLAN_EID_FAST_BSS_TRANSITION: |
| 292 | if (pos[1] < sizeof(*ftie)) |
| 293 | return -1; |
| 294 | ftie = (const struct rsn_ftie *) (pos + 2); |
| 295 | prot_ie_count = ftie->mic_control[1]; |
| 296 | if (wpa_ft_parse_ftie(pos + 2, pos[1], parse) < 0) |
| 297 | return -1; |
| 298 | break; |
| 299 | case WLAN_EID_TIMEOUT_INTERVAL: |
| 300 | parse->tie = pos + 2; |
| 301 | parse->tie_len = pos[1]; |
| 302 | break; |
| 303 | case WLAN_EID_RIC_DATA: |
| 304 | if (parse->ric == NULL) |
| 305 | parse->ric = pos; |
| 306 | break; |
| 307 | } |
| 308 | |
| 309 | pos += 2 + pos[1]; |
| 310 | } |
| 311 | |
| 312 | if (prot_ie_count == 0) |
| 313 | return 0; /* no MIC */ |
| 314 | |
| 315 | /* |
| 316 | * Check that the protected IE count matches with IEs included in the |
| 317 | * frame. |
| 318 | */ |
| 319 | if (parse->rsn) |
| 320 | prot_ie_count--; |
| 321 | if (parse->mdie) |
| 322 | prot_ie_count--; |
| 323 | if (parse->ftie) |
| 324 | prot_ie_count--; |
| 325 | if (prot_ie_count < 0) { |
| 326 | wpa_printf(MSG_DEBUG, "FT: Some required IEs not included in " |
| 327 | "the protected IE count"); |
| 328 | return -1; |
| 329 | } |
| 330 | |
| 331 | if (prot_ie_count == 0 && parse->ric) { |
| 332 | wpa_printf(MSG_DEBUG, "FT: RIC IE(s) in the frame, but not " |
| 333 | "included in protected IE count"); |
| 334 | return -1; |
| 335 | } |
| 336 | |
| 337 | /* Determine the end of the RIC IE(s) */ |
| 338 | pos = parse->ric; |
| 339 | while (pos && pos + 2 <= end && pos + 2 + pos[1] <= end && |
| 340 | prot_ie_count) { |
| 341 | prot_ie_count--; |
| 342 | pos += 2 + pos[1]; |
| 343 | } |
| 344 | parse->ric_len = pos - parse->ric; |
| 345 | if (prot_ie_count) { |
| 346 | wpa_printf(MSG_DEBUG, "FT: %d protected IEs missing from " |
| 347 | "frame", (int) prot_ie_count); |
| 348 | return -1; |
| 349 | } |
| 350 | |
| 351 | return 0; |
| 352 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 353 | #endif /* CONFIG_IEEE80211R */ |
| 354 | |
| 355 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 356 | static int rsn_selector_to_bitfield(const u8 *s) |
| 357 | { |
| 358 | if (RSN_SELECTOR_GET(s) == RSN_CIPHER_SUITE_NONE) |
| 359 | return WPA_CIPHER_NONE; |
| 360 | if (RSN_SELECTOR_GET(s) == RSN_CIPHER_SUITE_WEP40) |
| 361 | return WPA_CIPHER_WEP40; |
| 362 | if (RSN_SELECTOR_GET(s) == RSN_CIPHER_SUITE_TKIP) |
| 363 | return WPA_CIPHER_TKIP; |
| 364 | if (RSN_SELECTOR_GET(s) == RSN_CIPHER_SUITE_CCMP) |
| 365 | return WPA_CIPHER_CCMP; |
| 366 | if (RSN_SELECTOR_GET(s) == RSN_CIPHER_SUITE_WEP104) |
| 367 | return WPA_CIPHER_WEP104; |
| 368 | #ifdef CONFIG_IEEE80211W |
| 369 | if (RSN_SELECTOR_GET(s) == RSN_CIPHER_SUITE_AES_128_CMAC) |
| 370 | return WPA_CIPHER_AES_128_CMAC; |
| 371 | #endif /* CONFIG_IEEE80211W */ |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 372 | if (RSN_SELECTOR_GET(s) == RSN_CIPHER_SUITE_GCMP) |
| 373 | return WPA_CIPHER_GCMP; |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 374 | if (RSN_SELECTOR_GET(s) == RSN_CIPHER_SUITE_CCMP_256) |
| 375 | return WPA_CIPHER_CCMP_256; |
| 376 | if (RSN_SELECTOR_GET(s) == RSN_CIPHER_SUITE_GCMP_256) |
| 377 | return WPA_CIPHER_GCMP_256; |
| 378 | if (RSN_SELECTOR_GET(s) == RSN_CIPHER_SUITE_BIP_GMAC_128) |
| 379 | return WPA_CIPHER_BIP_GMAC_128; |
| 380 | if (RSN_SELECTOR_GET(s) == RSN_CIPHER_SUITE_BIP_GMAC_256) |
| 381 | return WPA_CIPHER_BIP_GMAC_256; |
| 382 | if (RSN_SELECTOR_GET(s) == RSN_CIPHER_SUITE_BIP_CMAC_256) |
| 383 | return WPA_CIPHER_BIP_CMAC_256; |
Dmitry Shmidt | b36ed7c | 2014-03-17 10:57:26 -0700 | [diff] [blame] | 384 | if (RSN_SELECTOR_GET(s) == RSN_CIPHER_SUITE_NO_GROUP_ADDRESSED) |
| 385 | return WPA_CIPHER_GTK_NOT_USED; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 386 | return 0; |
| 387 | } |
| 388 | |
| 389 | |
| 390 | static int rsn_key_mgmt_to_bitfield(const u8 *s) |
| 391 | { |
| 392 | if (RSN_SELECTOR_GET(s) == RSN_AUTH_KEY_MGMT_UNSPEC_802_1X) |
| 393 | return WPA_KEY_MGMT_IEEE8021X; |
| 394 | if (RSN_SELECTOR_GET(s) == RSN_AUTH_KEY_MGMT_PSK_OVER_802_1X) |
| 395 | return WPA_KEY_MGMT_PSK; |
| 396 | #ifdef CONFIG_IEEE80211R |
| 397 | if (RSN_SELECTOR_GET(s) == RSN_AUTH_KEY_MGMT_FT_802_1X) |
| 398 | return WPA_KEY_MGMT_FT_IEEE8021X; |
| 399 | if (RSN_SELECTOR_GET(s) == RSN_AUTH_KEY_MGMT_FT_PSK) |
| 400 | return WPA_KEY_MGMT_FT_PSK; |
| 401 | #endif /* CONFIG_IEEE80211R */ |
| 402 | #ifdef CONFIG_IEEE80211W |
| 403 | if (RSN_SELECTOR_GET(s) == RSN_AUTH_KEY_MGMT_802_1X_SHA256) |
| 404 | return WPA_KEY_MGMT_IEEE8021X_SHA256; |
| 405 | if (RSN_SELECTOR_GET(s) == RSN_AUTH_KEY_MGMT_PSK_SHA256) |
| 406 | return WPA_KEY_MGMT_PSK_SHA256; |
| 407 | #endif /* CONFIG_IEEE80211W */ |
Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 408 | #ifdef CONFIG_SAE |
| 409 | if (RSN_SELECTOR_GET(s) == RSN_AUTH_KEY_MGMT_SAE) |
| 410 | return WPA_KEY_MGMT_SAE; |
| 411 | if (RSN_SELECTOR_GET(s) == RSN_AUTH_KEY_MGMT_FT_SAE) |
| 412 | return WPA_KEY_MGMT_FT_SAE; |
| 413 | #endif /* CONFIG_SAE */ |
Dmitry Shmidt | fb45fd5 | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 414 | if (RSN_SELECTOR_GET(s) == RSN_AUTH_KEY_MGMT_802_1X_SUITE_B) |
| 415 | return WPA_KEY_MGMT_IEEE8021X_SUITE_B; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 416 | return 0; |
| 417 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 418 | |
| 419 | |
Dmitry Shmidt | b36ed7c | 2014-03-17 10:57:26 -0700 | [diff] [blame] | 420 | static int wpa_cipher_valid_group(int cipher) |
| 421 | { |
| 422 | return wpa_cipher_valid_pairwise(cipher) || |
| 423 | cipher == WPA_CIPHER_WEP104 || |
| 424 | cipher == WPA_CIPHER_WEP40 || |
| 425 | cipher == WPA_CIPHER_GTK_NOT_USED; |
| 426 | } |
| 427 | |
| 428 | |
| 429 | #ifdef CONFIG_IEEE80211W |
| 430 | int wpa_cipher_valid_mgmt_group(int cipher) |
| 431 | { |
| 432 | return cipher == WPA_CIPHER_AES_128_CMAC || |
| 433 | cipher == WPA_CIPHER_BIP_GMAC_128 || |
| 434 | cipher == WPA_CIPHER_BIP_GMAC_256 || |
| 435 | cipher == WPA_CIPHER_BIP_CMAC_256; |
| 436 | } |
| 437 | #endif /* CONFIG_IEEE80211W */ |
| 438 | |
| 439 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 440 | /** |
| 441 | * wpa_parse_wpa_ie_rsn - Parse RSN IE |
| 442 | * @rsn_ie: Buffer containing RSN IE |
| 443 | * @rsn_ie_len: RSN IE buffer length (including IE number and length octets) |
| 444 | * @data: Pointer to structure that will be filled in with parsed data |
| 445 | * Returns: 0 on success, <0 on failure |
| 446 | */ |
| 447 | int wpa_parse_wpa_ie_rsn(const u8 *rsn_ie, size_t rsn_ie_len, |
| 448 | struct wpa_ie_data *data) |
| 449 | { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 450 | const struct rsn_ie_hdr *hdr; |
| 451 | const u8 *pos; |
| 452 | int left; |
| 453 | int i, count; |
| 454 | |
| 455 | os_memset(data, 0, sizeof(*data)); |
| 456 | data->proto = WPA_PROTO_RSN; |
| 457 | data->pairwise_cipher = WPA_CIPHER_CCMP; |
| 458 | data->group_cipher = WPA_CIPHER_CCMP; |
| 459 | data->key_mgmt = WPA_KEY_MGMT_IEEE8021X; |
| 460 | data->capabilities = 0; |
| 461 | data->pmkid = NULL; |
| 462 | data->num_pmkid = 0; |
| 463 | #ifdef CONFIG_IEEE80211W |
| 464 | data->mgmt_group_cipher = WPA_CIPHER_AES_128_CMAC; |
| 465 | #else /* CONFIG_IEEE80211W */ |
| 466 | data->mgmt_group_cipher = 0; |
| 467 | #endif /* CONFIG_IEEE80211W */ |
| 468 | |
| 469 | if (rsn_ie_len == 0) { |
| 470 | /* No RSN IE - fail silently */ |
| 471 | return -1; |
| 472 | } |
| 473 | |
| 474 | if (rsn_ie_len < sizeof(struct rsn_ie_hdr)) { |
| 475 | wpa_printf(MSG_DEBUG, "%s: ie len too short %lu", |
| 476 | __func__, (unsigned long) rsn_ie_len); |
| 477 | return -1; |
| 478 | } |
| 479 | |
| 480 | hdr = (const struct rsn_ie_hdr *) rsn_ie; |
| 481 | |
| 482 | if (hdr->elem_id != WLAN_EID_RSN || |
| 483 | hdr->len != rsn_ie_len - 2 || |
| 484 | WPA_GET_LE16(hdr->version) != RSN_VERSION) { |
| 485 | wpa_printf(MSG_DEBUG, "%s: malformed ie or unknown version", |
| 486 | __func__); |
| 487 | return -2; |
| 488 | } |
| 489 | |
| 490 | pos = (const u8 *) (hdr + 1); |
| 491 | left = rsn_ie_len - sizeof(*hdr); |
| 492 | |
| 493 | if (left >= RSN_SELECTOR_LEN) { |
| 494 | data->group_cipher = rsn_selector_to_bitfield(pos); |
Dmitry Shmidt | b36ed7c | 2014-03-17 10:57:26 -0700 | [diff] [blame] | 495 | if (!wpa_cipher_valid_group(data->group_cipher)) { |
| 496 | wpa_printf(MSG_DEBUG, "%s: invalid group cipher 0x%x", |
| 497 | __func__, data->group_cipher); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 498 | return -1; |
| 499 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 500 | pos += RSN_SELECTOR_LEN; |
| 501 | left -= RSN_SELECTOR_LEN; |
| 502 | } else if (left > 0) { |
| 503 | wpa_printf(MSG_DEBUG, "%s: ie length mismatch, %u too much", |
| 504 | __func__, left); |
| 505 | return -3; |
| 506 | } |
| 507 | |
| 508 | if (left >= 2) { |
| 509 | data->pairwise_cipher = 0; |
| 510 | count = WPA_GET_LE16(pos); |
| 511 | pos += 2; |
| 512 | left -= 2; |
Dmitry Shmidt | fb45fd5 | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 513 | if (count == 0 || count > left / RSN_SELECTOR_LEN) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 514 | wpa_printf(MSG_DEBUG, "%s: ie count botch (pairwise), " |
| 515 | "count %u left %u", __func__, count, left); |
| 516 | return -4; |
| 517 | } |
| 518 | for (i = 0; i < count; i++) { |
| 519 | data->pairwise_cipher |= rsn_selector_to_bitfield(pos); |
| 520 | pos += RSN_SELECTOR_LEN; |
| 521 | left -= RSN_SELECTOR_LEN; |
| 522 | } |
| 523 | #ifdef CONFIG_IEEE80211W |
| 524 | if (data->pairwise_cipher & WPA_CIPHER_AES_128_CMAC) { |
| 525 | wpa_printf(MSG_DEBUG, "%s: AES-128-CMAC used as " |
| 526 | "pairwise cipher", __func__); |
| 527 | return -1; |
| 528 | } |
| 529 | #endif /* CONFIG_IEEE80211W */ |
| 530 | } else if (left == 1) { |
| 531 | wpa_printf(MSG_DEBUG, "%s: ie too short (for key mgmt)", |
| 532 | __func__); |
| 533 | return -5; |
| 534 | } |
| 535 | |
| 536 | if (left >= 2) { |
| 537 | data->key_mgmt = 0; |
| 538 | count = WPA_GET_LE16(pos); |
| 539 | pos += 2; |
| 540 | left -= 2; |
Dmitry Shmidt | fb45fd5 | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 541 | if (count == 0 || count > left / RSN_SELECTOR_LEN) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 542 | wpa_printf(MSG_DEBUG, "%s: ie count botch (key mgmt), " |
| 543 | "count %u left %u", __func__, count, left); |
| 544 | return -6; |
| 545 | } |
| 546 | for (i = 0; i < count; i++) { |
| 547 | data->key_mgmt |= rsn_key_mgmt_to_bitfield(pos); |
| 548 | pos += RSN_SELECTOR_LEN; |
| 549 | left -= RSN_SELECTOR_LEN; |
| 550 | } |
| 551 | } else if (left == 1) { |
| 552 | wpa_printf(MSG_DEBUG, "%s: ie too short (for capabilities)", |
| 553 | __func__); |
| 554 | return -7; |
| 555 | } |
| 556 | |
| 557 | if (left >= 2) { |
| 558 | data->capabilities = WPA_GET_LE16(pos); |
| 559 | pos += 2; |
| 560 | left -= 2; |
| 561 | } |
| 562 | |
| 563 | if (left >= 2) { |
Dmitry Shmidt | fb45fd5 | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 564 | u16 num_pmkid = WPA_GET_LE16(pos); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 565 | pos += 2; |
| 566 | left -= 2; |
Dmitry Shmidt | fb45fd5 | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 567 | if (num_pmkid > (unsigned int) left / PMKID_LEN) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 568 | wpa_printf(MSG_DEBUG, "%s: PMKID underflow " |
Dmitry Shmidt | fb45fd5 | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 569 | "(num_pmkid=%u left=%d)", |
| 570 | __func__, num_pmkid, left); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 571 | data->num_pmkid = 0; |
| 572 | return -9; |
| 573 | } else { |
Dmitry Shmidt | fb45fd5 | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 574 | data->num_pmkid = num_pmkid; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 575 | data->pmkid = pos; |
| 576 | pos += data->num_pmkid * PMKID_LEN; |
| 577 | left -= data->num_pmkid * PMKID_LEN; |
| 578 | } |
| 579 | } |
| 580 | |
| 581 | #ifdef CONFIG_IEEE80211W |
| 582 | if (left >= 4) { |
| 583 | data->mgmt_group_cipher = rsn_selector_to_bitfield(pos); |
Dmitry Shmidt | b36ed7c | 2014-03-17 10:57:26 -0700 | [diff] [blame] | 584 | if (!wpa_cipher_valid_mgmt_group(data->mgmt_group_cipher)) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 585 | wpa_printf(MSG_DEBUG, "%s: Unsupported management " |
| 586 | "group cipher 0x%x", __func__, |
| 587 | data->mgmt_group_cipher); |
| 588 | return -10; |
| 589 | } |
| 590 | pos += RSN_SELECTOR_LEN; |
| 591 | left -= RSN_SELECTOR_LEN; |
| 592 | } |
| 593 | #endif /* CONFIG_IEEE80211W */ |
| 594 | |
| 595 | if (left > 0) { |
Dmitry Shmidt | 7d5c8f2 | 2014-03-03 13:53:28 -0800 | [diff] [blame] | 596 | wpa_hexdump(MSG_DEBUG, |
| 597 | "wpa_parse_wpa_ie_rsn: ignore trailing bytes", |
| 598 | pos, left); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 599 | } |
| 600 | |
| 601 | return 0; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 602 | } |
| 603 | |
| 604 | |
| 605 | static int wpa_selector_to_bitfield(const u8 *s) |
| 606 | { |
| 607 | if (RSN_SELECTOR_GET(s) == WPA_CIPHER_SUITE_NONE) |
| 608 | return WPA_CIPHER_NONE; |
| 609 | if (RSN_SELECTOR_GET(s) == WPA_CIPHER_SUITE_WEP40) |
| 610 | return WPA_CIPHER_WEP40; |
| 611 | if (RSN_SELECTOR_GET(s) == WPA_CIPHER_SUITE_TKIP) |
| 612 | return WPA_CIPHER_TKIP; |
| 613 | if (RSN_SELECTOR_GET(s) == WPA_CIPHER_SUITE_CCMP) |
| 614 | return WPA_CIPHER_CCMP; |
| 615 | if (RSN_SELECTOR_GET(s) == WPA_CIPHER_SUITE_WEP104) |
| 616 | return WPA_CIPHER_WEP104; |
| 617 | return 0; |
| 618 | } |
| 619 | |
| 620 | |
| 621 | static int wpa_key_mgmt_to_bitfield(const u8 *s) |
| 622 | { |
| 623 | if (RSN_SELECTOR_GET(s) == WPA_AUTH_KEY_MGMT_UNSPEC_802_1X) |
| 624 | return WPA_KEY_MGMT_IEEE8021X; |
| 625 | if (RSN_SELECTOR_GET(s) == WPA_AUTH_KEY_MGMT_PSK_OVER_802_1X) |
| 626 | return WPA_KEY_MGMT_PSK; |
| 627 | if (RSN_SELECTOR_GET(s) == WPA_AUTH_KEY_MGMT_NONE) |
| 628 | return WPA_KEY_MGMT_WPA_NONE; |
| 629 | return 0; |
| 630 | } |
| 631 | |
| 632 | |
| 633 | int wpa_parse_wpa_ie_wpa(const u8 *wpa_ie, size_t wpa_ie_len, |
| 634 | struct wpa_ie_data *data) |
| 635 | { |
| 636 | const struct wpa_ie_hdr *hdr; |
| 637 | const u8 *pos; |
| 638 | int left; |
| 639 | int i, count; |
| 640 | |
| 641 | os_memset(data, 0, sizeof(*data)); |
| 642 | data->proto = WPA_PROTO_WPA; |
| 643 | data->pairwise_cipher = WPA_CIPHER_TKIP; |
| 644 | data->group_cipher = WPA_CIPHER_TKIP; |
| 645 | data->key_mgmt = WPA_KEY_MGMT_IEEE8021X; |
| 646 | data->capabilities = 0; |
| 647 | data->pmkid = NULL; |
| 648 | data->num_pmkid = 0; |
| 649 | data->mgmt_group_cipher = 0; |
| 650 | |
| 651 | if (wpa_ie_len == 0) { |
| 652 | /* No WPA IE - fail silently */ |
| 653 | return -1; |
| 654 | } |
| 655 | |
| 656 | if (wpa_ie_len < sizeof(struct wpa_ie_hdr)) { |
| 657 | wpa_printf(MSG_DEBUG, "%s: ie len too short %lu", |
| 658 | __func__, (unsigned long) wpa_ie_len); |
| 659 | return -1; |
| 660 | } |
| 661 | |
| 662 | hdr = (const struct wpa_ie_hdr *) wpa_ie; |
| 663 | |
| 664 | if (hdr->elem_id != WLAN_EID_VENDOR_SPECIFIC || |
| 665 | hdr->len != wpa_ie_len - 2 || |
| 666 | RSN_SELECTOR_GET(hdr->oui) != WPA_OUI_TYPE || |
| 667 | WPA_GET_LE16(hdr->version) != WPA_VERSION) { |
| 668 | wpa_printf(MSG_DEBUG, "%s: malformed ie or unknown version", |
| 669 | __func__); |
| 670 | return -2; |
| 671 | } |
| 672 | |
| 673 | pos = (const u8 *) (hdr + 1); |
| 674 | left = wpa_ie_len - sizeof(*hdr); |
| 675 | |
| 676 | if (left >= WPA_SELECTOR_LEN) { |
| 677 | data->group_cipher = wpa_selector_to_bitfield(pos); |
| 678 | pos += WPA_SELECTOR_LEN; |
| 679 | left -= WPA_SELECTOR_LEN; |
| 680 | } else if (left > 0) { |
| 681 | wpa_printf(MSG_DEBUG, "%s: ie length mismatch, %u too much", |
| 682 | __func__, left); |
| 683 | return -3; |
| 684 | } |
| 685 | |
| 686 | if (left >= 2) { |
| 687 | data->pairwise_cipher = 0; |
| 688 | count = WPA_GET_LE16(pos); |
| 689 | pos += 2; |
| 690 | left -= 2; |
Dmitry Shmidt | fb45fd5 | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 691 | if (count == 0 || count > left / WPA_SELECTOR_LEN) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 692 | wpa_printf(MSG_DEBUG, "%s: ie count botch (pairwise), " |
| 693 | "count %u left %u", __func__, count, left); |
| 694 | return -4; |
| 695 | } |
| 696 | for (i = 0; i < count; i++) { |
| 697 | data->pairwise_cipher |= wpa_selector_to_bitfield(pos); |
| 698 | pos += WPA_SELECTOR_LEN; |
| 699 | left -= WPA_SELECTOR_LEN; |
| 700 | } |
| 701 | } else if (left == 1) { |
| 702 | wpa_printf(MSG_DEBUG, "%s: ie too short (for key mgmt)", |
| 703 | __func__); |
| 704 | return -5; |
| 705 | } |
| 706 | |
| 707 | if (left >= 2) { |
| 708 | data->key_mgmt = 0; |
| 709 | count = WPA_GET_LE16(pos); |
| 710 | pos += 2; |
| 711 | left -= 2; |
Dmitry Shmidt | fb45fd5 | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 712 | if (count == 0 || count > left / WPA_SELECTOR_LEN) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 713 | wpa_printf(MSG_DEBUG, "%s: ie count botch (key mgmt), " |
| 714 | "count %u left %u", __func__, count, left); |
| 715 | return -6; |
| 716 | } |
| 717 | for (i = 0; i < count; i++) { |
| 718 | data->key_mgmt |= wpa_key_mgmt_to_bitfield(pos); |
| 719 | pos += WPA_SELECTOR_LEN; |
| 720 | left -= WPA_SELECTOR_LEN; |
| 721 | } |
| 722 | } else if (left == 1) { |
| 723 | wpa_printf(MSG_DEBUG, "%s: ie too short (for capabilities)", |
| 724 | __func__); |
| 725 | return -7; |
| 726 | } |
| 727 | |
| 728 | if (left >= 2) { |
| 729 | data->capabilities = WPA_GET_LE16(pos); |
| 730 | pos += 2; |
| 731 | left -= 2; |
| 732 | } |
| 733 | |
| 734 | if (left > 0) { |
Dmitry Shmidt | 7d5c8f2 | 2014-03-03 13:53:28 -0800 | [diff] [blame] | 735 | wpa_hexdump(MSG_DEBUG, |
| 736 | "wpa_parse_wpa_ie_wpa: ignore trailing bytes", |
| 737 | pos, left); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 738 | } |
| 739 | |
| 740 | return 0; |
| 741 | } |
| 742 | |
| 743 | |
| 744 | #ifdef CONFIG_IEEE80211R |
| 745 | |
| 746 | /** |
| 747 | * wpa_derive_pmk_r0 - Derive PMK-R0 and PMKR0Name |
| 748 | * |
| 749 | * IEEE Std 802.11r-2008 - 8.5.1.5.3 |
| 750 | */ |
| 751 | void wpa_derive_pmk_r0(const u8 *xxkey, size_t xxkey_len, |
| 752 | const u8 *ssid, size_t ssid_len, |
| 753 | const u8 *mdid, const u8 *r0kh_id, size_t r0kh_id_len, |
| 754 | const u8 *s0kh_id, u8 *pmk_r0, u8 *pmk_r0_name) |
| 755 | { |
| 756 | u8 buf[1 + WPA_MAX_SSID_LEN + MOBILITY_DOMAIN_ID_LEN + 1 + |
| 757 | FT_R0KH_ID_MAX_LEN + ETH_ALEN]; |
| 758 | u8 *pos, r0_key_data[48], hash[32]; |
| 759 | const u8 *addr[2]; |
| 760 | size_t len[2]; |
| 761 | |
| 762 | /* |
| 763 | * R0-Key-Data = KDF-384(XXKey, "FT-R0", |
| 764 | * SSIDlength || SSID || MDID || R0KHlength || |
| 765 | * R0KH-ID || S0KH-ID) |
| 766 | * XXKey is either the second 256 bits of MSK or PSK. |
| 767 | * PMK-R0 = L(R0-Key-Data, 0, 256) |
| 768 | * PMK-R0Name-Salt = L(R0-Key-Data, 256, 128) |
| 769 | */ |
| 770 | if (ssid_len > WPA_MAX_SSID_LEN || r0kh_id_len > FT_R0KH_ID_MAX_LEN) |
| 771 | return; |
| 772 | pos = buf; |
| 773 | *pos++ = ssid_len; |
| 774 | os_memcpy(pos, ssid, ssid_len); |
| 775 | pos += ssid_len; |
| 776 | os_memcpy(pos, mdid, MOBILITY_DOMAIN_ID_LEN); |
| 777 | pos += MOBILITY_DOMAIN_ID_LEN; |
| 778 | *pos++ = r0kh_id_len; |
| 779 | os_memcpy(pos, r0kh_id, r0kh_id_len); |
| 780 | pos += r0kh_id_len; |
| 781 | os_memcpy(pos, s0kh_id, ETH_ALEN); |
| 782 | pos += ETH_ALEN; |
| 783 | |
| 784 | sha256_prf(xxkey, xxkey_len, "FT-R0", buf, pos - buf, |
| 785 | r0_key_data, sizeof(r0_key_data)); |
| 786 | os_memcpy(pmk_r0, r0_key_data, PMK_LEN); |
| 787 | |
| 788 | /* |
| 789 | * PMKR0Name = Truncate-128(SHA-256("FT-R0N" || PMK-R0Name-Salt) |
| 790 | */ |
| 791 | addr[0] = (const u8 *) "FT-R0N"; |
| 792 | len[0] = 6; |
| 793 | addr[1] = r0_key_data + PMK_LEN; |
| 794 | len[1] = 16; |
| 795 | |
| 796 | sha256_vector(2, addr, len, hash); |
| 797 | os_memcpy(pmk_r0_name, hash, WPA_PMK_NAME_LEN); |
| 798 | } |
| 799 | |
| 800 | |
| 801 | /** |
| 802 | * wpa_derive_pmk_r1_name - Derive PMKR1Name |
| 803 | * |
| 804 | * IEEE Std 802.11r-2008 - 8.5.1.5.4 |
| 805 | */ |
| 806 | void wpa_derive_pmk_r1_name(const u8 *pmk_r0_name, const u8 *r1kh_id, |
| 807 | const u8 *s1kh_id, u8 *pmk_r1_name) |
| 808 | { |
| 809 | u8 hash[32]; |
| 810 | const u8 *addr[4]; |
| 811 | size_t len[4]; |
| 812 | |
| 813 | /* |
| 814 | * PMKR1Name = Truncate-128(SHA-256("FT-R1N" || PMKR0Name || |
| 815 | * R1KH-ID || S1KH-ID)) |
| 816 | */ |
| 817 | addr[0] = (const u8 *) "FT-R1N"; |
| 818 | len[0] = 6; |
| 819 | addr[1] = pmk_r0_name; |
| 820 | len[1] = WPA_PMK_NAME_LEN; |
| 821 | addr[2] = r1kh_id; |
| 822 | len[2] = FT_R1KH_ID_LEN; |
| 823 | addr[3] = s1kh_id; |
| 824 | len[3] = ETH_ALEN; |
| 825 | |
| 826 | sha256_vector(4, addr, len, hash); |
| 827 | os_memcpy(pmk_r1_name, hash, WPA_PMK_NAME_LEN); |
| 828 | } |
| 829 | |
| 830 | |
| 831 | /** |
| 832 | * wpa_derive_pmk_r1 - Derive PMK-R1 and PMKR1Name from PMK-R0 |
| 833 | * |
| 834 | * IEEE Std 802.11r-2008 - 8.5.1.5.4 |
| 835 | */ |
| 836 | void wpa_derive_pmk_r1(const u8 *pmk_r0, const u8 *pmk_r0_name, |
| 837 | const u8 *r1kh_id, const u8 *s1kh_id, |
| 838 | u8 *pmk_r1, u8 *pmk_r1_name) |
| 839 | { |
| 840 | u8 buf[FT_R1KH_ID_LEN + ETH_ALEN]; |
| 841 | u8 *pos; |
| 842 | |
| 843 | /* PMK-R1 = KDF-256(PMK-R0, "FT-R1", R1KH-ID || S1KH-ID) */ |
| 844 | pos = buf; |
| 845 | os_memcpy(pos, r1kh_id, FT_R1KH_ID_LEN); |
| 846 | pos += FT_R1KH_ID_LEN; |
| 847 | os_memcpy(pos, s1kh_id, ETH_ALEN); |
| 848 | pos += ETH_ALEN; |
| 849 | |
| 850 | sha256_prf(pmk_r0, PMK_LEN, "FT-R1", buf, pos - buf, pmk_r1, PMK_LEN); |
| 851 | |
| 852 | wpa_derive_pmk_r1_name(pmk_r0_name, r1kh_id, s1kh_id, pmk_r1_name); |
| 853 | } |
| 854 | |
| 855 | |
| 856 | /** |
| 857 | * wpa_pmk_r1_to_ptk - Derive PTK and PTKName from PMK-R1 |
| 858 | * |
| 859 | * IEEE Std 802.11r-2008 - 8.5.1.5.5 |
| 860 | */ |
| 861 | void wpa_pmk_r1_to_ptk(const u8 *pmk_r1, const u8 *snonce, const u8 *anonce, |
| 862 | const u8 *sta_addr, const u8 *bssid, |
| 863 | const u8 *pmk_r1_name, |
| 864 | u8 *ptk, size_t ptk_len, u8 *ptk_name) |
| 865 | { |
| 866 | u8 buf[2 * WPA_NONCE_LEN + 2 * ETH_ALEN]; |
| 867 | u8 *pos, hash[32]; |
| 868 | const u8 *addr[6]; |
| 869 | size_t len[6]; |
| 870 | |
| 871 | /* |
| 872 | * PTK = KDF-PTKLen(PMK-R1, "FT-PTK", SNonce || ANonce || |
| 873 | * BSSID || STA-ADDR) |
| 874 | */ |
| 875 | pos = buf; |
| 876 | os_memcpy(pos, snonce, WPA_NONCE_LEN); |
| 877 | pos += WPA_NONCE_LEN; |
| 878 | os_memcpy(pos, anonce, WPA_NONCE_LEN); |
| 879 | pos += WPA_NONCE_LEN; |
| 880 | os_memcpy(pos, bssid, ETH_ALEN); |
| 881 | pos += ETH_ALEN; |
| 882 | os_memcpy(pos, sta_addr, ETH_ALEN); |
| 883 | pos += ETH_ALEN; |
| 884 | |
| 885 | sha256_prf(pmk_r1, PMK_LEN, "FT-PTK", buf, pos - buf, ptk, ptk_len); |
| 886 | |
| 887 | /* |
| 888 | * PTKName = Truncate-128(SHA-256(PMKR1Name || "FT-PTKN" || SNonce || |
| 889 | * ANonce || BSSID || STA-ADDR)) |
| 890 | */ |
| 891 | addr[0] = pmk_r1_name; |
| 892 | len[0] = WPA_PMK_NAME_LEN; |
| 893 | addr[1] = (const u8 *) "FT-PTKN"; |
| 894 | len[1] = 7; |
| 895 | addr[2] = snonce; |
| 896 | len[2] = WPA_NONCE_LEN; |
| 897 | addr[3] = anonce; |
| 898 | len[3] = WPA_NONCE_LEN; |
| 899 | addr[4] = bssid; |
| 900 | len[4] = ETH_ALEN; |
| 901 | addr[5] = sta_addr; |
| 902 | len[5] = ETH_ALEN; |
| 903 | |
| 904 | sha256_vector(6, addr, len, hash); |
| 905 | os_memcpy(ptk_name, hash, WPA_PMK_NAME_LEN); |
| 906 | } |
| 907 | |
| 908 | #endif /* CONFIG_IEEE80211R */ |
| 909 | |
| 910 | |
| 911 | /** |
| 912 | * rsn_pmkid - Calculate PMK identifier |
| 913 | * @pmk: Pairwise master key |
| 914 | * @pmk_len: Length of pmk in bytes |
| 915 | * @aa: Authenticator address |
| 916 | * @spa: Supplicant address |
| 917 | * @pmkid: Buffer for PMKID |
| 918 | * @use_sha256: Whether to use SHA256-based KDF |
| 919 | * |
| 920 | * IEEE Std 802.11i-2004 - 8.5.1.2 Pairwise key hierarchy |
| 921 | * PMKID = HMAC-SHA1-128(PMK, "PMK Name" || AA || SPA) |
| 922 | */ |
| 923 | void rsn_pmkid(const u8 *pmk, size_t pmk_len, const u8 *aa, const u8 *spa, |
| 924 | u8 *pmkid, int use_sha256) |
| 925 | { |
| 926 | char *title = "PMK Name"; |
| 927 | const u8 *addr[3]; |
| 928 | const size_t len[3] = { 8, ETH_ALEN, ETH_ALEN }; |
| 929 | unsigned char hash[SHA256_MAC_LEN]; |
| 930 | |
| 931 | addr[0] = (u8 *) title; |
| 932 | addr[1] = aa; |
| 933 | addr[2] = spa; |
| 934 | |
| 935 | #ifdef CONFIG_IEEE80211W |
| 936 | if (use_sha256) |
| 937 | hmac_sha256_vector(pmk, pmk_len, 3, addr, len, hash); |
| 938 | else |
| 939 | #endif /* CONFIG_IEEE80211W */ |
| 940 | hmac_sha1_vector(pmk, pmk_len, 3, addr, len, hash); |
| 941 | os_memcpy(pmkid, hash, PMKID_LEN); |
| 942 | } |
| 943 | |
| 944 | |
Dmitry Shmidt | fb45fd5 | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 945 | #ifdef CONFIG_SUITEB |
| 946 | /** |
| 947 | * rsn_pmkid_suite_b - Calculate PMK identifier for Suite B AKM |
| 948 | * @kck: Key confirmation key |
| 949 | * @kck_len: Length of kck in bytes |
| 950 | * @aa: Authenticator address |
| 951 | * @spa: Supplicant address |
| 952 | * @pmkid: Buffer for PMKID |
| 953 | * Returns: 0 on success, -1 on failure |
| 954 | * |
| 955 | * IEEE Std 802.11ac-2013 - 11.6.1.3 Pairwise key hierarchy |
| 956 | * PMKID = Truncate(HMAC-SHA-256(KCK, "PMK Name" || AA || SPA)) |
| 957 | */ |
| 958 | int rsn_pmkid_suite_b(const u8 *kck, size_t kck_len, const u8 *aa, |
| 959 | const u8 *spa, u8 *pmkid) |
| 960 | { |
| 961 | char *title = "PMK Name"; |
| 962 | const u8 *addr[3]; |
| 963 | const size_t len[3] = { 8, ETH_ALEN, ETH_ALEN }; |
| 964 | unsigned char hash[SHA256_MAC_LEN]; |
| 965 | |
| 966 | addr[0] = (u8 *) title; |
| 967 | addr[1] = aa; |
| 968 | addr[2] = spa; |
| 969 | |
| 970 | if (hmac_sha256_vector(kck, kck_len, 3, addr, len, hash) < 0) |
| 971 | return -1; |
| 972 | os_memcpy(pmkid, hash, PMKID_LEN); |
| 973 | return 0; |
| 974 | } |
| 975 | #endif /* CONFIG_SUITEB */ |
| 976 | |
| 977 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 978 | /** |
| 979 | * wpa_cipher_txt - Convert cipher suite to a text string |
| 980 | * @cipher: Cipher suite (WPA_CIPHER_* enum) |
| 981 | * Returns: Pointer to a text string of the cipher suite name |
| 982 | */ |
| 983 | const char * wpa_cipher_txt(int cipher) |
| 984 | { |
| 985 | switch (cipher) { |
| 986 | case WPA_CIPHER_NONE: |
| 987 | return "NONE"; |
| 988 | case WPA_CIPHER_WEP40: |
| 989 | return "WEP-40"; |
| 990 | case WPA_CIPHER_WEP104: |
| 991 | return "WEP-104"; |
| 992 | case WPA_CIPHER_TKIP: |
| 993 | return "TKIP"; |
| 994 | case WPA_CIPHER_CCMP: |
| 995 | return "CCMP"; |
| 996 | case WPA_CIPHER_CCMP | WPA_CIPHER_TKIP: |
| 997 | return "CCMP+TKIP"; |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 998 | case WPA_CIPHER_GCMP: |
| 999 | return "GCMP"; |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 1000 | case WPA_CIPHER_GCMP_256: |
| 1001 | return "GCMP-256"; |
| 1002 | case WPA_CIPHER_CCMP_256: |
| 1003 | return "CCMP-256"; |
| 1004 | case WPA_CIPHER_GTK_NOT_USED: |
| 1005 | return "GTK_NOT_USED"; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1006 | default: |
| 1007 | return "UNKNOWN"; |
| 1008 | } |
| 1009 | } |
| 1010 | |
| 1011 | |
| 1012 | /** |
| 1013 | * wpa_key_mgmt_txt - Convert key management suite to a text string |
| 1014 | * @key_mgmt: Key management suite (WPA_KEY_MGMT_* enum) |
| 1015 | * @proto: WPA/WPA2 version (WPA_PROTO_*) |
| 1016 | * Returns: Pointer to a text string of the key management suite name |
| 1017 | */ |
| 1018 | const char * wpa_key_mgmt_txt(int key_mgmt, int proto) |
| 1019 | { |
| 1020 | switch (key_mgmt) { |
| 1021 | case WPA_KEY_MGMT_IEEE8021X: |
| 1022 | if (proto == (WPA_PROTO_RSN | WPA_PROTO_WPA)) |
| 1023 | return "WPA2+WPA/IEEE 802.1X/EAP"; |
| 1024 | return proto == WPA_PROTO_RSN ? |
| 1025 | "WPA2/IEEE 802.1X/EAP" : "WPA/IEEE 802.1X/EAP"; |
| 1026 | case WPA_KEY_MGMT_PSK: |
| 1027 | if (proto == (WPA_PROTO_RSN | WPA_PROTO_WPA)) |
| 1028 | return "WPA2-PSK+WPA-PSK"; |
| 1029 | return proto == WPA_PROTO_RSN ? |
| 1030 | "WPA2-PSK" : "WPA-PSK"; |
| 1031 | case WPA_KEY_MGMT_NONE: |
| 1032 | return "NONE"; |
| 1033 | case WPA_KEY_MGMT_IEEE8021X_NO_WPA: |
| 1034 | return "IEEE 802.1X (no WPA)"; |
| 1035 | #ifdef CONFIG_IEEE80211R |
| 1036 | case WPA_KEY_MGMT_FT_IEEE8021X: |
| 1037 | return "FT-EAP"; |
| 1038 | case WPA_KEY_MGMT_FT_PSK: |
| 1039 | return "FT-PSK"; |
| 1040 | #endif /* CONFIG_IEEE80211R */ |
| 1041 | #ifdef CONFIG_IEEE80211W |
| 1042 | case WPA_KEY_MGMT_IEEE8021X_SHA256: |
| 1043 | return "WPA2-EAP-SHA256"; |
| 1044 | case WPA_KEY_MGMT_PSK_SHA256: |
| 1045 | return "WPA2-PSK-SHA256"; |
| 1046 | #endif /* CONFIG_IEEE80211W */ |
Dmitry Shmidt | fb45fd5 | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 1047 | case WPA_KEY_MGMT_WPS: |
| 1048 | return "WPS"; |
| 1049 | case WPA_KEY_MGMT_SAE: |
| 1050 | return "SAE"; |
| 1051 | case WPA_KEY_MGMT_FT_SAE: |
| 1052 | return "FT-SAE"; |
| 1053 | case WPA_KEY_MGMT_OSEN: |
| 1054 | return "OSEN"; |
| 1055 | case WPA_KEY_MGMT_IEEE8021X_SUITE_B: |
| 1056 | return "WPA2-EAP-SUITE-B"; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1057 | default: |
| 1058 | return "UNKNOWN"; |
| 1059 | } |
| 1060 | } |
| 1061 | |
| 1062 | |
Dmitry Shmidt | 0365883 | 2014-08-13 11:03:49 -0700 | [diff] [blame] | 1063 | u32 wpa_akm_to_suite(int akm) |
| 1064 | { |
| 1065 | if (akm & WPA_KEY_MGMT_FT_IEEE8021X) |
| 1066 | return WLAN_AKM_SUITE_FT_8021X; |
| 1067 | if (akm & WPA_KEY_MGMT_FT_PSK) |
| 1068 | return WLAN_AKM_SUITE_FT_PSK; |
| 1069 | if (akm & WPA_KEY_MGMT_IEEE8021X) |
| 1070 | return WLAN_AKM_SUITE_8021X; |
| 1071 | if (akm & WPA_KEY_MGMT_IEEE8021X_SHA256) |
| 1072 | return WLAN_AKM_SUITE_8021X_SHA256; |
| 1073 | if (akm & WPA_KEY_MGMT_IEEE8021X) |
| 1074 | return WLAN_AKM_SUITE_8021X; |
| 1075 | if (akm & WPA_KEY_MGMT_PSK_SHA256) |
| 1076 | return WLAN_AKM_SUITE_PSK_SHA256; |
| 1077 | if (akm & WPA_KEY_MGMT_PSK) |
| 1078 | return WLAN_AKM_SUITE_PSK; |
| 1079 | if (akm & WPA_KEY_MGMT_CCKM) |
| 1080 | return WLAN_AKM_SUITE_CCKM; |
| 1081 | if (akm & WPA_KEY_MGMT_OSEN) |
| 1082 | return WLAN_AKM_SUITE_OSEN; |
Dmitry Shmidt | fb45fd5 | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 1083 | if (akm & WPA_KEY_MGMT_IEEE8021X_SUITE_B) |
| 1084 | return WLAN_AKM_SUITE_8021X_SUITE_B; |
Dmitry Shmidt | 0365883 | 2014-08-13 11:03:49 -0700 | [diff] [blame] | 1085 | return 0; |
| 1086 | } |
| 1087 | |
| 1088 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1089 | int wpa_compare_rsn_ie(int ft_initial_assoc, |
| 1090 | const u8 *ie1, size_t ie1len, |
| 1091 | const u8 *ie2, size_t ie2len) |
| 1092 | { |
| 1093 | if (ie1 == NULL || ie2 == NULL) |
| 1094 | return -1; |
| 1095 | |
| 1096 | if (ie1len == ie2len && os_memcmp(ie1, ie2, ie1len) == 0) |
| 1097 | return 0; /* identical IEs */ |
| 1098 | |
| 1099 | #ifdef CONFIG_IEEE80211R |
| 1100 | if (ft_initial_assoc) { |
| 1101 | struct wpa_ie_data ie1d, ie2d; |
| 1102 | /* |
| 1103 | * The PMKID-List in RSN IE is different between Beacon/Probe |
| 1104 | * Response/(Re)Association Request frames and EAPOL-Key |
| 1105 | * messages in FT initial mobility domain association. Allow |
| 1106 | * for this, but verify that other parts of the RSN IEs are |
| 1107 | * identical. |
| 1108 | */ |
| 1109 | if (wpa_parse_wpa_ie_rsn(ie1, ie1len, &ie1d) < 0 || |
| 1110 | wpa_parse_wpa_ie_rsn(ie2, ie2len, &ie2d) < 0) |
| 1111 | return -1; |
| 1112 | if (ie1d.proto == ie2d.proto && |
| 1113 | ie1d.pairwise_cipher == ie2d.pairwise_cipher && |
| 1114 | ie1d.group_cipher == ie2d.group_cipher && |
| 1115 | ie1d.key_mgmt == ie2d.key_mgmt && |
| 1116 | ie1d.capabilities == ie2d.capabilities && |
| 1117 | ie1d.mgmt_group_cipher == ie2d.mgmt_group_cipher) |
| 1118 | return 0; |
| 1119 | } |
| 1120 | #endif /* CONFIG_IEEE80211R */ |
| 1121 | |
| 1122 | return -1; |
| 1123 | } |
| 1124 | |
| 1125 | |
| 1126 | #ifdef CONFIG_IEEE80211R |
| 1127 | int wpa_insert_pmkid(u8 *ies, size_t ies_len, const u8 *pmkid) |
| 1128 | { |
| 1129 | u8 *start, *end, *rpos, *rend; |
| 1130 | int added = 0; |
| 1131 | |
| 1132 | start = ies; |
| 1133 | end = ies + ies_len; |
| 1134 | |
| 1135 | while (start < end) { |
| 1136 | if (*start == WLAN_EID_RSN) |
| 1137 | break; |
| 1138 | start += 2 + start[1]; |
| 1139 | } |
| 1140 | if (start >= end) { |
| 1141 | wpa_printf(MSG_ERROR, "FT: Could not find RSN IE in " |
| 1142 | "IEs data"); |
| 1143 | return -1; |
| 1144 | } |
| 1145 | wpa_hexdump(MSG_DEBUG, "FT: RSN IE before modification", |
| 1146 | start, 2 + start[1]); |
| 1147 | |
| 1148 | /* Find start of PMKID-Count */ |
| 1149 | rpos = start + 2; |
| 1150 | rend = rpos + start[1]; |
| 1151 | |
| 1152 | /* Skip Version and Group Data Cipher Suite */ |
| 1153 | rpos += 2 + 4; |
| 1154 | /* Skip Pairwise Cipher Suite Count and List */ |
| 1155 | rpos += 2 + WPA_GET_LE16(rpos) * RSN_SELECTOR_LEN; |
| 1156 | /* Skip AKM Suite Count and List */ |
| 1157 | rpos += 2 + WPA_GET_LE16(rpos) * RSN_SELECTOR_LEN; |
| 1158 | |
| 1159 | if (rpos == rend) { |
| 1160 | /* Add RSN Capabilities */ |
| 1161 | os_memmove(rpos + 2, rpos, end - rpos); |
| 1162 | *rpos++ = 0; |
| 1163 | *rpos++ = 0; |
| 1164 | } else { |
| 1165 | /* Skip RSN Capabilities */ |
| 1166 | rpos += 2; |
| 1167 | if (rpos > rend) { |
| 1168 | wpa_printf(MSG_ERROR, "FT: Could not parse RSN IE in " |
| 1169 | "IEs data"); |
| 1170 | return -1; |
| 1171 | } |
| 1172 | } |
| 1173 | |
| 1174 | if (rpos == rend) { |
| 1175 | /* No PMKID-Count field included; add it */ |
| 1176 | os_memmove(rpos + 2 + PMKID_LEN, rpos, end - rpos); |
| 1177 | WPA_PUT_LE16(rpos, 1); |
| 1178 | rpos += 2; |
| 1179 | os_memcpy(rpos, pmkid, PMKID_LEN); |
| 1180 | added += 2 + PMKID_LEN; |
| 1181 | start[1] += 2 + PMKID_LEN; |
| 1182 | } else { |
| 1183 | /* PMKID-Count was included; use it */ |
| 1184 | if (WPA_GET_LE16(rpos) != 0) { |
| 1185 | wpa_printf(MSG_ERROR, "FT: Unexpected PMKID " |
| 1186 | "in RSN IE in EAPOL-Key data"); |
| 1187 | return -1; |
| 1188 | } |
| 1189 | WPA_PUT_LE16(rpos, 1); |
| 1190 | rpos += 2; |
| 1191 | os_memmove(rpos + PMKID_LEN, rpos, end - rpos); |
| 1192 | os_memcpy(rpos, pmkid, PMKID_LEN); |
| 1193 | added += PMKID_LEN; |
| 1194 | start[1] += PMKID_LEN; |
| 1195 | } |
| 1196 | |
| 1197 | wpa_hexdump(MSG_DEBUG, "FT: RSN IE after modification " |
| 1198 | "(PMKID inserted)", start, 2 + start[1]); |
| 1199 | |
| 1200 | return added; |
| 1201 | } |
| 1202 | #endif /* CONFIG_IEEE80211R */ |
Dmitry Shmidt | 4530cfd | 2012-09-09 15:20:40 -0700 | [diff] [blame] | 1203 | |
| 1204 | |
| 1205 | int wpa_cipher_key_len(int cipher) |
| 1206 | { |
| 1207 | switch (cipher) { |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 1208 | case WPA_CIPHER_CCMP_256: |
| 1209 | case WPA_CIPHER_GCMP_256: |
Dmitry Shmidt | b36ed7c | 2014-03-17 10:57:26 -0700 | [diff] [blame] | 1210 | case WPA_CIPHER_BIP_GMAC_256: |
| 1211 | case WPA_CIPHER_BIP_CMAC_256: |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 1212 | return 32; |
Dmitry Shmidt | 4530cfd | 2012-09-09 15:20:40 -0700 | [diff] [blame] | 1213 | case WPA_CIPHER_CCMP: |
| 1214 | case WPA_CIPHER_GCMP: |
Dmitry Shmidt | b36ed7c | 2014-03-17 10:57:26 -0700 | [diff] [blame] | 1215 | case WPA_CIPHER_AES_128_CMAC: |
| 1216 | case WPA_CIPHER_BIP_GMAC_128: |
Dmitry Shmidt | 4530cfd | 2012-09-09 15:20:40 -0700 | [diff] [blame] | 1217 | return 16; |
| 1218 | case WPA_CIPHER_TKIP: |
| 1219 | return 32; |
| 1220 | case WPA_CIPHER_WEP104: |
| 1221 | return 13; |
| 1222 | case WPA_CIPHER_WEP40: |
| 1223 | return 5; |
| 1224 | } |
| 1225 | |
| 1226 | return 0; |
| 1227 | } |
| 1228 | |
| 1229 | |
| 1230 | int wpa_cipher_rsc_len(int cipher) |
| 1231 | { |
| 1232 | switch (cipher) { |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 1233 | case WPA_CIPHER_CCMP_256: |
| 1234 | case WPA_CIPHER_GCMP_256: |
Dmitry Shmidt | 4530cfd | 2012-09-09 15:20:40 -0700 | [diff] [blame] | 1235 | case WPA_CIPHER_CCMP: |
| 1236 | case WPA_CIPHER_GCMP: |
| 1237 | case WPA_CIPHER_TKIP: |
| 1238 | return 6; |
| 1239 | case WPA_CIPHER_WEP104: |
| 1240 | case WPA_CIPHER_WEP40: |
| 1241 | return 0; |
| 1242 | } |
| 1243 | |
| 1244 | return 0; |
| 1245 | } |
| 1246 | |
| 1247 | |
| 1248 | int wpa_cipher_to_alg(int cipher) |
| 1249 | { |
| 1250 | switch (cipher) { |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 1251 | case WPA_CIPHER_CCMP_256: |
| 1252 | return WPA_ALG_CCMP_256; |
| 1253 | case WPA_CIPHER_GCMP_256: |
| 1254 | return WPA_ALG_GCMP_256; |
Dmitry Shmidt | 4530cfd | 2012-09-09 15:20:40 -0700 | [diff] [blame] | 1255 | case WPA_CIPHER_CCMP: |
| 1256 | return WPA_ALG_CCMP; |
| 1257 | case WPA_CIPHER_GCMP: |
| 1258 | return WPA_ALG_GCMP; |
| 1259 | case WPA_CIPHER_TKIP: |
| 1260 | return WPA_ALG_TKIP; |
| 1261 | case WPA_CIPHER_WEP104: |
| 1262 | case WPA_CIPHER_WEP40: |
| 1263 | return WPA_ALG_WEP; |
Dmitry Shmidt | b36ed7c | 2014-03-17 10:57:26 -0700 | [diff] [blame] | 1264 | case WPA_CIPHER_AES_128_CMAC: |
| 1265 | return WPA_ALG_IGTK; |
| 1266 | case WPA_CIPHER_BIP_GMAC_128: |
| 1267 | return WPA_ALG_BIP_GMAC_128; |
| 1268 | case WPA_CIPHER_BIP_GMAC_256: |
| 1269 | return WPA_ALG_BIP_GMAC_256; |
| 1270 | case WPA_CIPHER_BIP_CMAC_256: |
| 1271 | return WPA_ALG_BIP_CMAC_256; |
Dmitry Shmidt | 4530cfd | 2012-09-09 15:20:40 -0700 | [diff] [blame] | 1272 | } |
| 1273 | return WPA_ALG_NONE; |
| 1274 | } |
| 1275 | |
| 1276 | |
| 1277 | int wpa_cipher_valid_pairwise(int cipher) |
| 1278 | { |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 1279 | return cipher == WPA_CIPHER_CCMP_256 || |
| 1280 | cipher == WPA_CIPHER_GCMP_256 || |
| 1281 | cipher == WPA_CIPHER_CCMP || |
Dmitry Shmidt | 4530cfd | 2012-09-09 15:20:40 -0700 | [diff] [blame] | 1282 | cipher == WPA_CIPHER_GCMP || |
| 1283 | cipher == WPA_CIPHER_TKIP; |
| 1284 | } |
| 1285 | |
| 1286 | |
| 1287 | u32 wpa_cipher_to_suite(int proto, int cipher) |
| 1288 | { |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 1289 | if (cipher & WPA_CIPHER_CCMP_256) |
| 1290 | return RSN_CIPHER_SUITE_CCMP_256; |
| 1291 | if (cipher & WPA_CIPHER_GCMP_256) |
| 1292 | return RSN_CIPHER_SUITE_GCMP_256; |
Dmitry Shmidt | 4530cfd | 2012-09-09 15:20:40 -0700 | [diff] [blame] | 1293 | if (cipher & WPA_CIPHER_CCMP) |
| 1294 | return (proto == WPA_PROTO_RSN ? |
| 1295 | RSN_CIPHER_SUITE_CCMP : WPA_CIPHER_SUITE_CCMP); |
| 1296 | if (cipher & WPA_CIPHER_GCMP) |
| 1297 | return RSN_CIPHER_SUITE_GCMP; |
| 1298 | if (cipher & WPA_CIPHER_TKIP) |
| 1299 | return (proto == WPA_PROTO_RSN ? |
| 1300 | RSN_CIPHER_SUITE_TKIP : WPA_CIPHER_SUITE_TKIP); |
| 1301 | if (cipher & WPA_CIPHER_WEP104) |
| 1302 | return (proto == WPA_PROTO_RSN ? |
| 1303 | RSN_CIPHER_SUITE_WEP104 : WPA_CIPHER_SUITE_WEP104); |
| 1304 | if (cipher & WPA_CIPHER_WEP40) |
| 1305 | return (proto == WPA_PROTO_RSN ? |
| 1306 | RSN_CIPHER_SUITE_WEP40 : WPA_CIPHER_SUITE_WEP40); |
| 1307 | if (cipher & WPA_CIPHER_NONE) |
| 1308 | return (proto == WPA_PROTO_RSN ? |
| 1309 | RSN_CIPHER_SUITE_NONE : WPA_CIPHER_SUITE_NONE); |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 1310 | if (cipher & WPA_CIPHER_GTK_NOT_USED) |
| 1311 | return RSN_CIPHER_SUITE_NO_GROUP_ADDRESSED; |
Dmitry Shmidt | b36ed7c | 2014-03-17 10:57:26 -0700 | [diff] [blame] | 1312 | if (cipher & WPA_CIPHER_AES_128_CMAC) |
| 1313 | return RSN_CIPHER_SUITE_AES_128_CMAC; |
| 1314 | if (cipher & WPA_CIPHER_BIP_GMAC_128) |
| 1315 | return RSN_CIPHER_SUITE_BIP_GMAC_128; |
| 1316 | if (cipher & WPA_CIPHER_BIP_GMAC_256) |
| 1317 | return RSN_CIPHER_SUITE_BIP_GMAC_256; |
| 1318 | if (cipher & WPA_CIPHER_BIP_CMAC_256) |
| 1319 | return RSN_CIPHER_SUITE_BIP_CMAC_256; |
Dmitry Shmidt | 4530cfd | 2012-09-09 15:20:40 -0700 | [diff] [blame] | 1320 | return 0; |
| 1321 | } |
| 1322 | |
| 1323 | |
Dmitry Shmidt | 7d5c8f2 | 2014-03-03 13:53:28 -0800 | [diff] [blame] | 1324 | int rsn_cipher_put_suites(u8 *start, int ciphers) |
Dmitry Shmidt | 4530cfd | 2012-09-09 15:20:40 -0700 | [diff] [blame] | 1325 | { |
Dmitry Shmidt | 7d5c8f2 | 2014-03-03 13:53:28 -0800 | [diff] [blame] | 1326 | u8 *pos = start; |
Dmitry Shmidt | 4530cfd | 2012-09-09 15:20:40 -0700 | [diff] [blame] | 1327 | |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 1328 | if (ciphers & WPA_CIPHER_CCMP_256) { |
| 1329 | RSN_SELECTOR_PUT(pos, RSN_CIPHER_SUITE_CCMP_256); |
| 1330 | pos += RSN_SELECTOR_LEN; |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 1331 | } |
| 1332 | if (ciphers & WPA_CIPHER_GCMP_256) { |
| 1333 | RSN_SELECTOR_PUT(pos, RSN_CIPHER_SUITE_GCMP_256); |
| 1334 | pos += RSN_SELECTOR_LEN; |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 1335 | } |
Dmitry Shmidt | 4530cfd | 2012-09-09 15:20:40 -0700 | [diff] [blame] | 1336 | if (ciphers & WPA_CIPHER_CCMP) { |
| 1337 | RSN_SELECTOR_PUT(pos, RSN_CIPHER_SUITE_CCMP); |
| 1338 | pos += RSN_SELECTOR_LEN; |
Dmitry Shmidt | 4530cfd | 2012-09-09 15:20:40 -0700 | [diff] [blame] | 1339 | } |
| 1340 | if (ciphers & WPA_CIPHER_GCMP) { |
| 1341 | RSN_SELECTOR_PUT(pos, RSN_CIPHER_SUITE_GCMP); |
| 1342 | pos += RSN_SELECTOR_LEN; |
Dmitry Shmidt | 4530cfd | 2012-09-09 15:20:40 -0700 | [diff] [blame] | 1343 | } |
| 1344 | if (ciphers & WPA_CIPHER_TKIP) { |
| 1345 | RSN_SELECTOR_PUT(pos, RSN_CIPHER_SUITE_TKIP); |
| 1346 | pos += RSN_SELECTOR_LEN; |
Dmitry Shmidt | 4530cfd | 2012-09-09 15:20:40 -0700 | [diff] [blame] | 1347 | } |
| 1348 | if (ciphers & WPA_CIPHER_NONE) { |
| 1349 | RSN_SELECTOR_PUT(pos, RSN_CIPHER_SUITE_NONE); |
| 1350 | pos += RSN_SELECTOR_LEN; |
Dmitry Shmidt | 4530cfd | 2012-09-09 15:20:40 -0700 | [diff] [blame] | 1351 | } |
| 1352 | |
Dmitry Shmidt | 7d5c8f2 | 2014-03-03 13:53:28 -0800 | [diff] [blame] | 1353 | return (pos - start) / RSN_SELECTOR_LEN; |
Dmitry Shmidt | 4530cfd | 2012-09-09 15:20:40 -0700 | [diff] [blame] | 1354 | } |
| 1355 | |
| 1356 | |
Dmitry Shmidt | 7d5c8f2 | 2014-03-03 13:53:28 -0800 | [diff] [blame] | 1357 | int wpa_cipher_put_suites(u8 *start, int ciphers) |
Dmitry Shmidt | 4530cfd | 2012-09-09 15:20:40 -0700 | [diff] [blame] | 1358 | { |
Dmitry Shmidt | 7d5c8f2 | 2014-03-03 13:53:28 -0800 | [diff] [blame] | 1359 | u8 *pos = start; |
Dmitry Shmidt | 4530cfd | 2012-09-09 15:20:40 -0700 | [diff] [blame] | 1360 | |
| 1361 | if (ciphers & WPA_CIPHER_CCMP) { |
| 1362 | RSN_SELECTOR_PUT(pos, WPA_CIPHER_SUITE_CCMP); |
| 1363 | pos += WPA_SELECTOR_LEN; |
Dmitry Shmidt | 4530cfd | 2012-09-09 15:20:40 -0700 | [diff] [blame] | 1364 | } |
| 1365 | if (ciphers & WPA_CIPHER_TKIP) { |
| 1366 | RSN_SELECTOR_PUT(pos, WPA_CIPHER_SUITE_TKIP); |
| 1367 | pos += WPA_SELECTOR_LEN; |
Dmitry Shmidt | 4530cfd | 2012-09-09 15:20:40 -0700 | [diff] [blame] | 1368 | } |
| 1369 | if (ciphers & WPA_CIPHER_NONE) { |
| 1370 | RSN_SELECTOR_PUT(pos, WPA_CIPHER_SUITE_NONE); |
| 1371 | pos += WPA_SELECTOR_LEN; |
Dmitry Shmidt | 4530cfd | 2012-09-09 15:20:40 -0700 | [diff] [blame] | 1372 | } |
| 1373 | |
Dmitry Shmidt | 7d5c8f2 | 2014-03-03 13:53:28 -0800 | [diff] [blame] | 1374 | return (pos - start) / RSN_SELECTOR_LEN; |
Dmitry Shmidt | 4530cfd | 2012-09-09 15:20:40 -0700 | [diff] [blame] | 1375 | } |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 1376 | |
| 1377 | |
| 1378 | int wpa_pick_pairwise_cipher(int ciphers, int none_allowed) |
| 1379 | { |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 1380 | if (ciphers & WPA_CIPHER_CCMP_256) |
| 1381 | return WPA_CIPHER_CCMP_256; |
| 1382 | if (ciphers & WPA_CIPHER_GCMP_256) |
| 1383 | return WPA_CIPHER_GCMP_256; |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 1384 | if (ciphers & WPA_CIPHER_CCMP) |
| 1385 | return WPA_CIPHER_CCMP; |
| 1386 | if (ciphers & WPA_CIPHER_GCMP) |
| 1387 | return WPA_CIPHER_GCMP; |
| 1388 | if (ciphers & WPA_CIPHER_TKIP) |
| 1389 | return WPA_CIPHER_TKIP; |
| 1390 | if (none_allowed && (ciphers & WPA_CIPHER_NONE)) |
| 1391 | return WPA_CIPHER_NONE; |
| 1392 | return -1; |
| 1393 | } |
| 1394 | |
| 1395 | |
| 1396 | int wpa_pick_group_cipher(int ciphers) |
| 1397 | { |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 1398 | if (ciphers & WPA_CIPHER_CCMP_256) |
| 1399 | return WPA_CIPHER_CCMP_256; |
| 1400 | if (ciphers & WPA_CIPHER_GCMP_256) |
| 1401 | return WPA_CIPHER_GCMP_256; |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 1402 | if (ciphers & WPA_CIPHER_CCMP) |
| 1403 | return WPA_CIPHER_CCMP; |
| 1404 | if (ciphers & WPA_CIPHER_GCMP) |
| 1405 | return WPA_CIPHER_GCMP; |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 1406 | if (ciphers & WPA_CIPHER_GTK_NOT_USED) |
| 1407 | return WPA_CIPHER_GTK_NOT_USED; |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 1408 | if (ciphers & WPA_CIPHER_TKIP) |
| 1409 | return WPA_CIPHER_TKIP; |
| 1410 | if (ciphers & WPA_CIPHER_WEP104) |
| 1411 | return WPA_CIPHER_WEP104; |
| 1412 | if (ciphers & WPA_CIPHER_WEP40) |
| 1413 | return WPA_CIPHER_WEP40; |
| 1414 | return -1; |
| 1415 | } |
| 1416 | |
| 1417 | |
| 1418 | int wpa_parse_cipher(const char *value) |
| 1419 | { |
| 1420 | int val = 0, last; |
| 1421 | char *start, *end, *buf; |
| 1422 | |
| 1423 | buf = os_strdup(value); |
| 1424 | if (buf == NULL) |
| 1425 | return -1; |
| 1426 | start = buf; |
| 1427 | |
| 1428 | while (*start != '\0') { |
| 1429 | while (*start == ' ' || *start == '\t') |
| 1430 | start++; |
| 1431 | if (*start == '\0') |
| 1432 | break; |
| 1433 | end = start; |
| 1434 | while (*end != ' ' && *end != '\t' && *end != '\0') |
| 1435 | end++; |
| 1436 | last = *end == '\0'; |
| 1437 | *end = '\0'; |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 1438 | if (os_strcmp(start, "CCMP-256") == 0) |
| 1439 | val |= WPA_CIPHER_CCMP_256; |
| 1440 | else if (os_strcmp(start, "GCMP-256") == 0) |
| 1441 | val |= WPA_CIPHER_GCMP_256; |
| 1442 | else if (os_strcmp(start, "CCMP") == 0) |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 1443 | val |= WPA_CIPHER_CCMP; |
| 1444 | else if (os_strcmp(start, "GCMP") == 0) |
| 1445 | val |= WPA_CIPHER_GCMP; |
| 1446 | else if (os_strcmp(start, "TKIP") == 0) |
| 1447 | val |= WPA_CIPHER_TKIP; |
| 1448 | else if (os_strcmp(start, "WEP104") == 0) |
| 1449 | val |= WPA_CIPHER_WEP104; |
| 1450 | else if (os_strcmp(start, "WEP40") == 0) |
| 1451 | val |= WPA_CIPHER_WEP40; |
| 1452 | else if (os_strcmp(start, "NONE") == 0) |
| 1453 | val |= WPA_CIPHER_NONE; |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 1454 | else if (os_strcmp(start, "GTK_NOT_USED") == 0) |
| 1455 | val |= WPA_CIPHER_GTK_NOT_USED; |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 1456 | else { |
| 1457 | os_free(buf); |
| 1458 | return -1; |
| 1459 | } |
| 1460 | |
| 1461 | if (last) |
| 1462 | break; |
| 1463 | start = end + 1; |
| 1464 | } |
| 1465 | os_free(buf); |
| 1466 | |
| 1467 | return val; |
| 1468 | } |
| 1469 | |
| 1470 | |
| 1471 | int wpa_write_ciphers(char *start, char *end, int ciphers, const char *delim) |
| 1472 | { |
| 1473 | char *pos = start; |
| 1474 | int ret; |
| 1475 | |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 1476 | if (ciphers & WPA_CIPHER_CCMP_256) { |
| 1477 | ret = os_snprintf(pos, end - pos, "%sCCMP-256", |
| 1478 | pos == start ? "" : delim); |
Dmitry Shmidt | fb45fd5 | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 1479 | if (os_snprintf_error(end - pos, ret)) |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 1480 | return -1; |
| 1481 | pos += ret; |
| 1482 | } |
| 1483 | if (ciphers & WPA_CIPHER_GCMP_256) { |
| 1484 | ret = os_snprintf(pos, end - pos, "%sGCMP-256", |
| 1485 | pos == start ? "" : delim); |
Dmitry Shmidt | fb45fd5 | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 1486 | if (os_snprintf_error(end - pos, ret)) |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 1487 | return -1; |
| 1488 | pos += ret; |
| 1489 | } |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 1490 | if (ciphers & WPA_CIPHER_CCMP) { |
| 1491 | ret = os_snprintf(pos, end - pos, "%sCCMP", |
| 1492 | pos == start ? "" : delim); |
Dmitry Shmidt | fb45fd5 | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 1493 | if (os_snprintf_error(end - pos, ret)) |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 1494 | return -1; |
| 1495 | pos += ret; |
| 1496 | } |
| 1497 | if (ciphers & WPA_CIPHER_GCMP) { |
| 1498 | ret = os_snprintf(pos, end - pos, "%sGCMP", |
| 1499 | pos == start ? "" : delim); |
Dmitry Shmidt | fb45fd5 | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 1500 | if (os_snprintf_error(end - pos, ret)) |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 1501 | return -1; |
| 1502 | pos += ret; |
| 1503 | } |
| 1504 | if (ciphers & WPA_CIPHER_TKIP) { |
| 1505 | ret = os_snprintf(pos, end - pos, "%sTKIP", |
| 1506 | pos == start ? "" : delim); |
Dmitry Shmidt | fb45fd5 | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 1507 | if (os_snprintf_error(end - pos, ret)) |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 1508 | return -1; |
| 1509 | pos += ret; |
| 1510 | } |
| 1511 | if (ciphers & WPA_CIPHER_WEP104) { |
| 1512 | ret = os_snprintf(pos, end - pos, "%sWEP104", |
| 1513 | pos == start ? "" : delim); |
Dmitry Shmidt | fb45fd5 | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 1514 | if (os_snprintf_error(end - pos, ret)) |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 1515 | return -1; |
| 1516 | pos += ret; |
| 1517 | } |
| 1518 | if (ciphers & WPA_CIPHER_WEP40) { |
| 1519 | ret = os_snprintf(pos, end - pos, "%sWEP40", |
| 1520 | pos == start ? "" : delim); |
Dmitry Shmidt | fb45fd5 | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 1521 | if (os_snprintf_error(end - pos, ret)) |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 1522 | return -1; |
| 1523 | pos += ret; |
| 1524 | } |
| 1525 | if (ciphers & WPA_CIPHER_NONE) { |
| 1526 | ret = os_snprintf(pos, end - pos, "%sNONE", |
| 1527 | pos == start ? "" : delim); |
Dmitry Shmidt | fb45fd5 | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 1528 | if (os_snprintf_error(end - pos, ret)) |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 1529 | return -1; |
| 1530 | pos += ret; |
| 1531 | } |
| 1532 | |
| 1533 | return pos - start; |
| 1534 | } |
| 1535 | |
| 1536 | |
| 1537 | int wpa_select_ap_group_cipher(int wpa, int wpa_pairwise, int rsn_pairwise) |
| 1538 | { |
| 1539 | int pairwise = 0; |
| 1540 | |
| 1541 | /* Select group cipher based on the enabled pairwise cipher suites */ |
| 1542 | if (wpa & 1) |
| 1543 | pairwise |= wpa_pairwise; |
| 1544 | if (wpa & 2) |
| 1545 | pairwise |= rsn_pairwise; |
| 1546 | |
| 1547 | if (pairwise & WPA_CIPHER_TKIP) |
| 1548 | return WPA_CIPHER_TKIP; |
| 1549 | if ((pairwise & (WPA_CIPHER_CCMP | WPA_CIPHER_GCMP)) == WPA_CIPHER_GCMP) |
| 1550 | return WPA_CIPHER_GCMP; |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 1551 | if ((pairwise & (WPA_CIPHER_GCMP_256 | WPA_CIPHER_CCMP | |
| 1552 | WPA_CIPHER_GCMP)) == WPA_CIPHER_GCMP_256) |
| 1553 | return WPA_CIPHER_GCMP_256; |
| 1554 | if ((pairwise & (WPA_CIPHER_CCMP_256 | WPA_CIPHER_CCMP | |
| 1555 | WPA_CIPHER_GCMP)) == WPA_CIPHER_CCMP_256) |
| 1556 | return WPA_CIPHER_CCMP_256; |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 1557 | return WPA_CIPHER_CCMP; |
| 1558 | } |