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