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