Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1 | /* |
| 2 | * WPA Supplicant - WPA state machine and EAPOL-Key processing |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame] | 3 | * Copyright (c) 2003-2018, Jouni Malinen <j@w1.fi> |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 4 | * Copyright(c) 2015 Intel Deutschland GmbH |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 5 | * |
Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 6 | * This software may be distributed under the terms of the BSD license. |
| 7 | * See README for more details. |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 8 | */ |
| 9 | |
| 10 | #include "includes.h" |
| 11 | |
| 12 | #include "common.h" |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 13 | #include "crypto/aes.h" |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 14 | #include "crypto/aes_wrap.h" |
| 15 | #include "crypto/crypto.h" |
| 16 | #include "crypto/random.h" |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 17 | #include "crypto/aes_siv.h" |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 18 | #include "crypto/sha256.h" |
| 19 | #include "crypto/sha384.h" |
| 20 | #include "crypto/sha512.h" |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 21 | #include "common/ieee802_11_defs.h" |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 22 | #include "common/ieee802_11_common.h" |
Hai Shalom | 74f70d4 | 2019-02-11 14:42:39 -0800 | [diff] [blame] | 23 | #include "common/ocv.h" |
Hai Shalom | 4fbc08f | 2020-05-18 12:37:00 -0700 | [diff] [blame] | 24 | #include "common/dpp.h" |
Hai Shalom | 899fcc7 | 2020-10-19 14:38:18 -0700 | [diff] [blame] | 25 | #include "common/wpa_ctrl.h" |
Paul Stewart | 092955c | 2017-02-06 09:13:09 -0800 | [diff] [blame] | 26 | #include "eap_common/eap_defs.h" |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 27 | #include "eapol_supp/eapol_supp_sm.h" |
Hai Shalom | 74f70d4 | 2019-02-11 14:42:39 -0800 | [diff] [blame] | 28 | #include "drivers/driver.h" |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 29 | #include "wpa.h" |
| 30 | #include "eloop.h" |
| 31 | #include "preauth.h" |
| 32 | #include "pmksa_cache.h" |
| 33 | #include "wpa_i.h" |
| 34 | #include "wpa_ie.h" |
Mir Ali | eaaf04e | 2021-06-07 12:17:29 +0530 | [diff] [blame] | 35 | #include "wpa_supplicant_i.h" |
| 36 | #include "driver_i.h" |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 37 | |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 38 | static const u8 null_rsc[8] = { 0, 0, 0, 0, 0, 0, 0, 0 }; |
| 39 | |
| 40 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 41 | /** |
| 42 | * wpa_eapol_key_send - Send WPA/RSN EAPOL-Key message |
| 43 | * @sm: Pointer to WPA state machine data from wpa_sm_init() |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 44 | * @ptk: PTK for Key Confirmation/Encryption Key |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 45 | * @ver: Version field from Key Info |
| 46 | * @dest: Destination address for the frame |
| 47 | * @proto: Ethertype (usually ETH_P_EAPOL) |
| 48 | * @msg: EAPOL-Key message |
| 49 | * @msg_len: Length of message |
| 50 | * @key_mic: Pointer to the buffer to which the EAPOL-Key MIC is written |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 51 | * Returns: >= 0 on success, < 0 on failure |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 52 | */ |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 53 | int wpa_eapol_key_send(struct wpa_sm *sm, struct wpa_ptk *ptk, |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 54 | int ver, const u8 *dest, u16 proto, |
| 55 | u8 *msg, size_t msg_len, u8 *key_mic) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 56 | { |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 57 | int ret = -1; |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 58 | size_t mic_len = wpa_mic_len(sm->key_mgmt, sm->pmk_len); |
Dmitry Shmidt | 807291d | 2015-01-27 13:40:23 -0800 | [diff] [blame] | 59 | |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 60 | wpa_printf(MSG_DEBUG, "WPA: Send EAPOL-Key frame to " MACSTR |
| 61 | " ver=%d mic_len=%d key_mgmt=0x%x", |
| 62 | MAC2STR(dest), ver, (int) mic_len, sm->key_mgmt); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 63 | if (is_zero_ether_addr(dest) && is_zero_ether_addr(sm->bssid)) { |
| 64 | /* |
| 65 | * Association event was not yet received; try to fetch |
| 66 | * BSSID from the driver. |
| 67 | */ |
| 68 | if (wpa_sm_get_bssid(sm, sm->bssid) < 0) { |
| 69 | wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, |
| 70 | "WPA: Failed to read BSSID for " |
| 71 | "EAPOL-Key destination address"); |
| 72 | } else { |
| 73 | dest = sm->bssid; |
| 74 | wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, |
| 75 | "WPA: Use BSSID (" MACSTR |
| 76 | ") as the destination for EAPOL-Key", |
| 77 | MAC2STR(dest)); |
| 78 | } |
| 79 | } |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 80 | |
| 81 | if (mic_len) { |
| 82 | if (key_mic && (!ptk || !ptk->kck_len)) |
| 83 | goto out; |
| 84 | |
| 85 | if (key_mic && |
| 86 | wpa_eapol_key_mic(ptk->kck, ptk->kck_len, sm->key_mgmt, ver, |
| 87 | msg, msg_len, key_mic)) { |
| 88 | wpa_msg(sm->ctx->msg_ctx, MSG_ERROR, |
| 89 | "WPA: Failed to generate EAPOL-Key version %d key_mgmt 0x%x MIC", |
| 90 | ver, sm->key_mgmt); |
| 91 | goto out; |
| 92 | } |
Dmitry Shmidt | 2933359 | 2017-01-09 12:27:11 -0800 | [diff] [blame] | 93 | if (ptk) |
| 94 | wpa_hexdump_key(MSG_DEBUG, "WPA: KCK", |
| 95 | ptk->kck, ptk->kck_len); |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 96 | wpa_hexdump(MSG_DEBUG, "WPA: Derived Key MIC", |
| 97 | key_mic, mic_len); |
| 98 | } else { |
| 99 | #ifdef CONFIG_FILS |
| 100 | /* AEAD cipher - Key MIC field not used */ |
| 101 | struct ieee802_1x_hdr *s_hdr, *hdr; |
| 102 | struct wpa_eapol_key *s_key, *key; |
| 103 | u8 *buf, *s_key_data, *key_data; |
| 104 | size_t buf_len = msg_len + AES_BLOCK_SIZE; |
| 105 | size_t key_data_len; |
| 106 | u16 eapol_len; |
| 107 | const u8 *aad[1]; |
| 108 | size_t aad_len[1]; |
| 109 | |
| 110 | if (!ptk || !ptk->kek_len) |
| 111 | goto out; |
| 112 | |
| 113 | key_data_len = msg_len - sizeof(struct ieee802_1x_hdr) - |
| 114 | sizeof(struct wpa_eapol_key) - 2; |
| 115 | |
| 116 | buf = os_malloc(buf_len); |
| 117 | if (!buf) |
| 118 | goto out; |
| 119 | |
| 120 | os_memcpy(buf, msg, msg_len); |
| 121 | hdr = (struct ieee802_1x_hdr *) buf; |
| 122 | key = (struct wpa_eapol_key *) (hdr + 1); |
| 123 | key_data = ((u8 *) (key + 1)) + 2; |
| 124 | |
| 125 | /* Update EAPOL header to include AES-SIV overhead */ |
| 126 | eapol_len = be_to_host16(hdr->length); |
| 127 | eapol_len += AES_BLOCK_SIZE; |
| 128 | hdr->length = host_to_be16(eapol_len); |
| 129 | |
| 130 | /* Update Key Data Length field to include AES-SIV overhead */ |
| 131 | WPA_PUT_BE16((u8 *) (key + 1), AES_BLOCK_SIZE + key_data_len); |
| 132 | |
| 133 | s_hdr = (struct ieee802_1x_hdr *) msg; |
| 134 | s_key = (struct wpa_eapol_key *) (s_hdr + 1); |
| 135 | s_key_data = ((u8 *) (s_key + 1)) + 2; |
| 136 | |
| 137 | wpa_hexdump_key(MSG_DEBUG, "WPA: Plaintext Key Data", |
| 138 | s_key_data, key_data_len); |
| 139 | |
| 140 | wpa_hexdump_key(MSG_DEBUG, "WPA: KEK", ptk->kek, ptk->kek_len); |
| 141 | /* AES-SIV AAD from EAPOL protocol version field (inclusive) to |
| 142 | * to Key Data (exclusive). */ |
| 143 | aad[0] = buf; |
| 144 | aad_len[0] = key_data - buf; |
| 145 | if (aes_siv_encrypt(ptk->kek, ptk->kek_len, |
| 146 | s_key_data, key_data_len, |
| 147 | 1, aad, aad_len, key_data) < 0) { |
| 148 | os_free(buf); |
| 149 | goto out; |
| 150 | } |
| 151 | |
| 152 | wpa_hexdump(MSG_DEBUG, "WPA: Encrypted Key Data from SIV", |
| 153 | key_data, AES_BLOCK_SIZE + key_data_len); |
| 154 | |
| 155 | os_free(msg); |
| 156 | msg = buf; |
| 157 | msg_len = buf_len; |
| 158 | #else /* CONFIG_FILS */ |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 159 | goto out; |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 160 | #endif /* CONFIG_FILS */ |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 161 | } |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 162 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 163 | wpa_hexdump(MSG_MSGDUMP, "WPA: TX EAPOL-Key", msg, msg_len); |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 164 | ret = wpa_sm_ether_send(sm, dest, proto, msg, msg_len); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 165 | eapol_sm_notify_tx_eapol_key(sm->eapol); |
| 166 | out: |
| 167 | os_free(msg); |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 168 | return ret; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | |
| 172 | /** |
| 173 | * wpa_sm_key_request - Send EAPOL-Key Request |
| 174 | * @sm: Pointer to WPA state machine data from wpa_sm_init() |
| 175 | * @error: Indicate whether this is an Michael MIC error report |
| 176 | * @pairwise: 1 = error report for pairwise packet, 0 = for group packet |
| 177 | * |
| 178 | * Send an EAPOL-Key Request to the current authenticator. This function is |
| 179 | * used to request rekeying and it is usually called when a local Michael MIC |
| 180 | * failure is detected. |
| 181 | */ |
| 182 | void wpa_sm_key_request(struct wpa_sm *sm, int error, int pairwise) |
| 183 | { |
Dmitry Shmidt | 807291d | 2015-01-27 13:40:23 -0800 | [diff] [blame] | 184 | size_t mic_len, hdrlen, rlen; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 185 | struct wpa_eapol_key *reply; |
| 186 | int key_info, ver; |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 187 | u8 bssid[ETH_ALEN], *rbuf, *key_mic, *mic; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 188 | |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 189 | if (pairwise && sm->wpa_deny_ptk0_rekey && !sm->use_ext_key_id && |
| 190 | wpa_sm_get_state(sm) == WPA_COMPLETED) { |
| 191 | wpa_msg(sm->ctx->msg_ctx, MSG_INFO, |
| 192 | "WPA: PTK0 rekey not allowed, reconnecting"); |
| 193 | wpa_sm_reconnect(sm); |
| 194 | return; |
| 195 | } |
| 196 | |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame] | 197 | if (wpa_use_akm_defined(sm->key_mgmt)) |
Dmitry Shmidt | f21452a | 2014-02-26 10:55:25 -0800 | [diff] [blame] | 198 | ver = WPA_KEY_INFO_TYPE_AKM_DEFINED; |
| 199 | else if (wpa_key_mgmt_ft(sm->key_mgmt) || |
| 200 | wpa_key_mgmt_sha256(sm->key_mgmt)) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 201 | ver = WPA_KEY_INFO_TYPE_AES_128_CMAC; |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 202 | else if (sm->pairwise_cipher != WPA_CIPHER_TKIP) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 203 | ver = WPA_KEY_INFO_TYPE_HMAC_SHA1_AES; |
| 204 | else |
| 205 | ver = WPA_KEY_INFO_TYPE_HMAC_MD5_RC4; |
| 206 | |
| 207 | if (wpa_sm_get_bssid(sm, bssid) < 0) { |
| 208 | wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, |
| 209 | "Failed to read BSSID for EAPOL-Key request"); |
| 210 | return; |
| 211 | } |
| 212 | |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 213 | mic_len = wpa_mic_len(sm->key_mgmt, sm->pmk_len); |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 214 | hdrlen = sizeof(*reply) + mic_len + 2; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 215 | rbuf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_KEY, NULL, |
Dmitry Shmidt | 807291d | 2015-01-27 13:40:23 -0800 | [diff] [blame] | 216 | hdrlen, &rlen, (void *) &reply); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 217 | if (rbuf == NULL) |
| 218 | return; |
| 219 | |
Dmitry Shmidt | f21452a | 2014-02-26 10:55:25 -0800 | [diff] [blame] | 220 | reply->type = (sm->proto == WPA_PROTO_RSN || |
| 221 | sm->proto == WPA_PROTO_OSEN) ? |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 222 | EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA; |
| 223 | key_info = WPA_KEY_INFO_REQUEST | ver; |
| 224 | if (sm->ptk_set) |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 225 | key_info |= WPA_KEY_INFO_SECURE; |
| 226 | if (sm->ptk_set && mic_len) |
| 227 | key_info |= WPA_KEY_INFO_MIC; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 228 | if (error) |
| 229 | key_info |= WPA_KEY_INFO_ERROR; |
| 230 | if (pairwise) |
| 231 | key_info |= WPA_KEY_INFO_KEY_TYPE; |
| 232 | WPA_PUT_BE16(reply->key_info, key_info); |
| 233 | WPA_PUT_BE16(reply->key_length, 0); |
| 234 | os_memcpy(reply->replay_counter, sm->request_counter, |
| 235 | WPA_REPLAY_COUNTER_LEN); |
| 236 | inc_byte_array(sm->request_counter, WPA_REPLAY_COUNTER_LEN); |
| 237 | |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 238 | mic = (u8 *) (reply + 1); |
| 239 | WPA_PUT_BE16(mic + mic_len, 0); |
Dmitry Shmidt | 807291d | 2015-01-27 13:40:23 -0800 | [diff] [blame] | 240 | if (!(key_info & WPA_KEY_INFO_MIC)) |
| 241 | key_mic = NULL; |
| 242 | else |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 243 | key_mic = mic; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 244 | |
| 245 | wpa_msg(sm->ctx->msg_ctx, MSG_INFO, |
| 246 | "WPA: Sending EAPOL-Key Request (error=%d " |
| 247 | "pairwise=%d ptk_set=%d len=%lu)", |
| 248 | error, pairwise, sm->ptk_set, (unsigned long) rlen); |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 249 | wpa_eapol_key_send(sm, &sm->ptk, ver, bssid, ETH_P_EAPOL, rbuf, rlen, |
| 250 | key_mic); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 254 | static void wpa_supplicant_key_mgmt_set_pmk(struct wpa_sm *sm) |
| 255 | { |
| 256 | #ifdef CONFIG_IEEE80211R |
| 257 | if (sm->key_mgmt == WPA_KEY_MGMT_FT_IEEE8021X) { |
| 258 | if (wpa_sm_key_mgmt_set_pmk(sm, sm->xxkey, sm->xxkey_len)) |
| 259 | wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, |
| 260 | "RSN: Cannot set low order 256 bits of MSK for key management offload"); |
| 261 | } else { |
| 262 | #endif /* CONFIG_IEEE80211R */ |
| 263 | if (wpa_sm_key_mgmt_set_pmk(sm, sm->pmk, sm->pmk_len)) |
| 264 | wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, |
| 265 | "RSN: Cannot set PMK for key management offload"); |
| 266 | #ifdef CONFIG_IEEE80211R |
| 267 | } |
| 268 | #endif /* CONFIG_IEEE80211R */ |
| 269 | } |
| 270 | |
| 271 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 272 | static int wpa_supplicant_get_pmk(struct wpa_sm *sm, |
| 273 | const unsigned char *src_addr, |
| 274 | const u8 *pmkid) |
| 275 | { |
| 276 | int abort_cached = 0; |
| 277 | |
| 278 | if (pmkid && !sm->cur_pmksa) { |
| 279 | /* When using drivers that generate RSN IE, wpa_supplicant may |
| 280 | * not have enough time to get the association information |
| 281 | * event before receiving this 1/4 message, so try to find a |
| 282 | * matching PMKSA cache entry here. */ |
Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 283 | sm->cur_pmksa = pmksa_cache_get(sm->pmksa, src_addr, pmkid, |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame] | 284 | NULL, 0); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 285 | if (sm->cur_pmksa) { |
| 286 | wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, |
| 287 | "RSN: found matching PMKID from PMKSA cache"); |
| 288 | } else { |
| 289 | wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, |
| 290 | "RSN: no matching PMKID found"); |
| 291 | abort_cached = 1; |
| 292 | } |
| 293 | } |
| 294 | |
| 295 | if (pmkid && sm->cur_pmksa && |
Dmitry Shmidt | c281702 | 2014-07-02 10:32:10 -0700 | [diff] [blame] | 296 | os_memcmp_const(pmkid, sm->cur_pmksa->pmkid, PMKID_LEN) == 0) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 297 | wpa_hexdump(MSG_DEBUG, "RSN: matched PMKID", pmkid, PMKID_LEN); |
| 298 | wpa_sm_set_pmk_from_pmksa(sm); |
| 299 | wpa_hexdump_key(MSG_DEBUG, "RSN: PMK from PMKSA cache", |
| 300 | sm->pmk, sm->pmk_len); |
| 301 | eapol_sm_notify_cached(sm->eapol); |
| 302 | #ifdef CONFIG_IEEE80211R |
| 303 | sm->xxkey_len = 0; |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame] | 304 | #ifdef CONFIG_SAE |
| 305 | if (sm->key_mgmt == WPA_KEY_MGMT_FT_SAE && |
| 306 | sm->pmk_len == PMK_LEN) { |
| 307 | /* Need to allow FT key derivation to proceed with |
| 308 | * PMK from SAE being used as the XXKey in cases where |
| 309 | * the PMKID in msg 1/4 matches the PMKSA entry that was |
| 310 | * just added based on SAE authentication for the |
| 311 | * initial mobility domain association. */ |
| 312 | os_memcpy(sm->xxkey, sm->pmk, sm->pmk_len); |
| 313 | sm->xxkey_len = sm->pmk_len; |
| 314 | } |
| 315 | #endif /* CONFIG_SAE */ |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 316 | #endif /* CONFIG_IEEE80211R */ |
| 317 | } else if (wpa_key_mgmt_wpa_ieee8021x(sm->key_mgmt) && sm->eapol) { |
| 318 | int res, pmk_len; |
Hai Shalom | 81f62d8 | 2019-07-22 12:10:00 -0700 | [diff] [blame] | 319 | #ifdef CONFIG_IEEE80211R |
| 320 | u8 buf[2 * PMK_LEN]; |
| 321 | #endif /* CONFIG_IEEE80211R */ |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 322 | |
Dmitry Shmidt | ebd93af | 2017-02-21 13:40:44 -0800 | [diff] [blame] | 323 | if (wpa_key_mgmt_sha384(sm->key_mgmt)) |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 324 | pmk_len = PMK_LEN_SUITE_B_192; |
| 325 | else |
| 326 | pmk_len = PMK_LEN; |
| 327 | res = eapol_sm_get_key(sm->eapol, sm->pmk, pmk_len); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 328 | if (res) { |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 329 | if (pmk_len == PMK_LEN) { |
| 330 | /* |
| 331 | * EAP-LEAP is an exception from other EAP |
| 332 | * methods: it uses only 16-byte PMK. |
| 333 | */ |
| 334 | res = eapol_sm_get_key(sm->eapol, sm->pmk, 16); |
| 335 | pmk_len = 16; |
| 336 | } |
Hai Shalom | f1c9764 | 2019-07-19 23:42:07 +0000 | [diff] [blame] | 337 | } |
Hai Shalom | 81f62d8 | 2019-07-22 12:10:00 -0700 | [diff] [blame] | 338 | #ifdef CONFIG_IEEE80211R |
| 339 | if (res == 0 && |
| 340 | eapol_sm_get_key(sm->eapol, buf, 2 * PMK_LEN) == 0) { |
| 341 | if (wpa_key_mgmt_sha384(sm->key_mgmt)) { |
| 342 | os_memcpy(sm->xxkey, buf, SHA384_MAC_LEN); |
| 343 | sm->xxkey_len = SHA384_MAC_LEN; |
| 344 | } else { |
| 345 | os_memcpy(sm->xxkey, buf + PMK_LEN, PMK_LEN); |
| 346 | sm->xxkey_len = PMK_LEN; |
| 347 | } |
| 348 | forced_memzero(buf, sizeof(buf)); |
| 349 | if (sm->proto == WPA_PROTO_RSN && |
| 350 | wpa_key_mgmt_ft(sm->key_mgmt)) { |
| 351 | struct rsn_pmksa_cache_entry *sa = NULL; |
| 352 | const u8 *fils_cache_id = NULL; |
| 353 | |
| 354 | #ifdef CONFIG_FILS |
| 355 | if (sm->fils_cache_id_set) |
| 356 | fils_cache_id = sm->fils_cache_id; |
| 357 | #endif /* CONFIG_FILS */ |
| 358 | wpa_hexdump_key(MSG_DEBUG, |
| 359 | "FT: Cache XXKey/MPMK", |
| 360 | sm->xxkey, sm->xxkey_len); |
| 361 | sa = pmksa_cache_add(sm->pmksa, |
| 362 | sm->xxkey, sm->xxkey_len, |
| 363 | NULL, NULL, 0, |
| 364 | src_addr, sm->own_addr, |
| 365 | sm->network_ctx, |
| 366 | sm->key_mgmt, |
| 367 | fils_cache_id); |
| 368 | if (!sm->cur_pmksa) |
| 369 | sm->cur_pmksa = sa; |
| 370 | } |
| 371 | } |
| 372 | #endif /* CONFIG_IEEE80211R */ |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 373 | if (res == 0) { |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 374 | struct rsn_pmksa_cache_entry *sa = NULL; |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 375 | const u8 *fils_cache_id = NULL; |
| 376 | |
| 377 | #ifdef CONFIG_FILS |
| 378 | if (sm->fils_cache_id_set) |
| 379 | fils_cache_id = sm->fils_cache_id; |
| 380 | #endif /* CONFIG_FILS */ |
| 381 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 382 | wpa_hexdump_key(MSG_DEBUG, "WPA: PMK from EAPOL state " |
| 383 | "machines", sm->pmk, pmk_len); |
| 384 | sm->pmk_len = pmk_len; |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 385 | wpa_supplicant_key_mgmt_set_pmk(sm); |
Dmitry Shmidt | c55524a | 2011-07-07 11:18:38 -0700 | [diff] [blame] | 386 | if (sm->proto == WPA_PROTO_RSN && |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 387 | !wpa_key_mgmt_suite_b(sm->key_mgmt) && |
Dmitry Shmidt | c55524a | 2011-07-07 11:18:38 -0700 | [diff] [blame] | 388 | !wpa_key_mgmt_ft(sm->key_mgmt)) { |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 389 | sa = pmksa_cache_add(sm->pmksa, |
Dmitry Shmidt | 57c2d39 | 2016-02-23 13:40:19 -0800 | [diff] [blame] | 390 | sm->pmk, pmk_len, NULL, |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 391 | NULL, 0, |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 392 | src_addr, sm->own_addr, |
| 393 | sm->network_ctx, |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 394 | sm->key_mgmt, |
| 395 | fils_cache_id); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 396 | } |
| 397 | if (!sm->cur_pmksa && pmkid && |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame] | 398 | pmksa_cache_get(sm->pmksa, src_addr, pmkid, NULL, |
| 399 | 0)) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 400 | wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, |
| 401 | "RSN: the new PMK matches with the " |
| 402 | "PMKID"); |
| 403 | abort_cached = 0; |
Jouni Malinen | 6ec3038 | 2015-07-08 20:48:18 +0300 | [diff] [blame] | 404 | } else if (sa && !sm->cur_pmksa && pmkid) { |
| 405 | /* |
| 406 | * It looks like the authentication server |
| 407 | * derived mismatching MSK. This should not |
| 408 | * really happen, but bugs happen.. There is not |
| 409 | * much we can do here without knowing what |
| 410 | * exactly caused the server to misbehave. |
| 411 | */ |
Dmitry Shmidt | 014a3ff | 2015-12-28 13:27:49 -0800 | [diff] [blame] | 412 | wpa_msg(sm->ctx->msg_ctx, MSG_INFO, |
Jouni Malinen | 6ec3038 | 2015-07-08 20:48:18 +0300 | [diff] [blame] | 413 | "RSN: PMKID mismatch - authentication server may have derived different MSK?!"); |
| 414 | return -1; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 415 | } |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 416 | |
| 417 | if (!sm->cur_pmksa) |
| 418 | sm->cur_pmksa = sa; |
Hai Shalom | 81f62d8 | 2019-07-22 12:10:00 -0700 | [diff] [blame] | 419 | #ifdef CONFIG_IEEE80211R |
| 420 | } else if (wpa_key_mgmt_ft(sm->key_mgmt) && sm->ft_protocol) { |
| 421 | wpa_printf(MSG_DEBUG, |
| 422 | "FT: Continue 4-way handshake without PMK/PMKID for association using FT protocol"); |
| 423 | #endif /* CONFIG_IEEE80211R */ |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 424 | } else { |
| 425 | wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, |
| 426 | "WPA: Failed to get master session key from " |
| 427 | "EAPOL state machines - key handshake " |
| 428 | "aborted"); |
| 429 | if (sm->cur_pmksa) { |
| 430 | wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, |
| 431 | "RSN: Cancelled PMKSA caching " |
| 432 | "attempt"); |
| 433 | sm->cur_pmksa = NULL; |
| 434 | abort_cached = 1; |
| 435 | } else if (!abort_cached) { |
| 436 | return -1; |
| 437 | } |
| 438 | } |
| 439 | } |
| 440 | |
Dmitry Shmidt | c55524a | 2011-07-07 11:18:38 -0700 | [diff] [blame] | 441 | if (abort_cached && wpa_key_mgmt_wpa_ieee8021x(sm->key_mgmt) && |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 442 | !wpa_key_mgmt_suite_b(sm->key_mgmt) && |
Dmitry Shmidt | f21452a | 2014-02-26 10:55:25 -0800 | [diff] [blame] | 443 | !wpa_key_mgmt_ft(sm->key_mgmt) && sm->key_mgmt != WPA_KEY_MGMT_OSEN) |
| 444 | { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 445 | /* Send EAPOL-Start to trigger full EAP authentication. */ |
| 446 | u8 *buf; |
| 447 | size_t buflen; |
| 448 | |
| 449 | wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, |
| 450 | "RSN: no PMKSA entry found - trigger " |
| 451 | "full EAP authentication"); |
| 452 | buf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_START, |
| 453 | NULL, 0, &buflen, NULL); |
| 454 | if (buf) { |
Hai Shalom | c1a2144 | 2022-02-04 13:43:00 -0800 | [diff] [blame] | 455 | /* Set and reset eapFail to allow EAP state machine to |
| 456 | * proceed with new authentication. */ |
| 457 | eapol_sm_notify_eap_fail(sm->eapol, true); |
| 458 | eapol_sm_notify_eap_fail(sm->eapol, false); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 459 | wpa_sm_ether_send(sm, sm->bssid, ETH_P_EAPOL, |
| 460 | buf, buflen); |
| 461 | os_free(buf); |
| 462 | return -2; |
| 463 | } |
| 464 | |
| 465 | return -1; |
| 466 | } |
| 467 | |
| 468 | return 0; |
| 469 | } |
| 470 | |
| 471 | |
| 472 | /** |
| 473 | * wpa_supplicant_send_2_of_4 - Send message 2 of WPA/RSN 4-Way Handshake |
| 474 | * @sm: Pointer to WPA state machine data from wpa_sm_init() |
| 475 | * @dst: Destination address for the frame |
| 476 | * @key: Pointer to the EAPOL-Key frame header |
| 477 | * @ver: Version bits from EAPOL-Key Key Info |
| 478 | * @nonce: Nonce value for the EAPOL-Key frame |
| 479 | * @wpa_ie: WPA/RSN IE |
| 480 | * @wpa_ie_len: Length of the WPA/RSN IE |
| 481 | * @ptk: PTK to use for keyed hash and encryption |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 482 | * Returns: >= 0 on success, < 0 on failure |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 483 | */ |
| 484 | int wpa_supplicant_send_2_of_4(struct wpa_sm *sm, const unsigned char *dst, |
| 485 | const struct wpa_eapol_key *key, |
| 486 | int ver, const u8 *nonce, |
| 487 | const u8 *wpa_ie, size_t wpa_ie_len, |
| 488 | struct wpa_ptk *ptk) |
| 489 | { |
Dmitry Shmidt | 807291d | 2015-01-27 13:40:23 -0800 | [diff] [blame] | 490 | size_t mic_len, hdrlen, rlen; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 491 | struct wpa_eapol_key *reply; |
Dmitry Shmidt | 807291d | 2015-01-27 13:40:23 -0800 | [diff] [blame] | 492 | u8 *rbuf, *key_mic; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 493 | u8 *rsn_ie_buf = NULL; |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 494 | u16 key_info; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 495 | |
| 496 | if (wpa_ie == NULL) { |
| 497 | wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, "WPA: No wpa_ie set - " |
| 498 | "cannot generate msg 2/4"); |
| 499 | return -1; |
| 500 | } |
| 501 | |
| 502 | #ifdef CONFIG_IEEE80211R |
| 503 | if (wpa_key_mgmt_ft(sm->key_mgmt)) { |
| 504 | int res; |
| 505 | |
Ahmed ElArabawy | 0ff61c5 | 2019-12-26 12:38:39 -0800 | [diff] [blame] | 506 | wpa_hexdump(MSG_DEBUG, "WPA: WPA IE before FT processing", |
| 507 | wpa_ie, wpa_ie_len); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 508 | /* |
| 509 | * Add PMKR1Name into RSN IE (PMKID-List) and add MDIE and |
| 510 | * FTIE from (Re)Association Response. |
| 511 | */ |
| 512 | rsn_ie_buf = os_malloc(wpa_ie_len + 2 + 2 + PMKID_LEN + |
| 513 | sm->assoc_resp_ies_len); |
| 514 | if (rsn_ie_buf == NULL) |
| 515 | return -1; |
| 516 | os_memcpy(rsn_ie_buf, wpa_ie, wpa_ie_len); |
Dmitry Shmidt | 55840ad | 2015-12-14 12:45:46 -0800 | [diff] [blame] | 517 | res = wpa_insert_pmkid(rsn_ie_buf, &wpa_ie_len, |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 518 | sm->pmk_r1_name); |
| 519 | if (res < 0) { |
| 520 | os_free(rsn_ie_buf); |
| 521 | return -1; |
| 522 | } |
Ahmed ElArabawy | 0ff61c5 | 2019-12-26 12:38:39 -0800 | [diff] [blame] | 523 | wpa_hexdump(MSG_DEBUG, |
| 524 | "WPA: WPA IE after PMKID[PMKR1Name] addition into RSNE", |
| 525 | rsn_ie_buf, wpa_ie_len); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 526 | |
| 527 | if (sm->assoc_resp_ies) { |
Ahmed ElArabawy | 0ff61c5 | 2019-12-26 12:38:39 -0800 | [diff] [blame] | 528 | wpa_hexdump(MSG_DEBUG, "WPA: Add assoc_resp_ies", |
| 529 | sm->assoc_resp_ies, |
| 530 | sm->assoc_resp_ies_len); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 531 | os_memcpy(rsn_ie_buf + wpa_ie_len, sm->assoc_resp_ies, |
| 532 | sm->assoc_resp_ies_len); |
| 533 | wpa_ie_len += sm->assoc_resp_ies_len; |
| 534 | } |
| 535 | |
| 536 | wpa_ie = rsn_ie_buf; |
| 537 | } |
| 538 | #endif /* CONFIG_IEEE80211R */ |
| 539 | |
| 540 | wpa_hexdump(MSG_DEBUG, "WPA: WPA IE for msg 2/4", wpa_ie, wpa_ie_len); |
| 541 | |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 542 | mic_len = wpa_mic_len(sm->key_mgmt, sm->pmk_len); |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 543 | hdrlen = sizeof(*reply) + mic_len + 2; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 544 | rbuf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_KEY, |
Dmitry Shmidt | 807291d | 2015-01-27 13:40:23 -0800 | [diff] [blame] | 545 | NULL, hdrlen + wpa_ie_len, |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 546 | &rlen, (void *) &reply); |
| 547 | if (rbuf == NULL) { |
| 548 | os_free(rsn_ie_buf); |
| 549 | return -1; |
| 550 | } |
| 551 | |
Dmitry Shmidt | f21452a | 2014-02-26 10:55:25 -0800 | [diff] [blame] | 552 | reply->type = (sm->proto == WPA_PROTO_RSN || |
| 553 | sm->proto == WPA_PROTO_OSEN) ? |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 554 | EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA; |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 555 | key_info = ver | WPA_KEY_INFO_KEY_TYPE; |
| 556 | if (mic_len) |
| 557 | key_info |= WPA_KEY_INFO_MIC; |
| 558 | else |
| 559 | key_info |= WPA_KEY_INFO_ENCR_KEY_DATA; |
| 560 | WPA_PUT_BE16(reply->key_info, key_info); |
Dmitry Shmidt | f21452a | 2014-02-26 10:55:25 -0800 | [diff] [blame] | 561 | if (sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 562 | WPA_PUT_BE16(reply->key_length, 0); |
| 563 | else |
| 564 | os_memcpy(reply->key_length, key->key_length, 2); |
| 565 | os_memcpy(reply->replay_counter, key->replay_counter, |
| 566 | WPA_REPLAY_COUNTER_LEN); |
| 567 | wpa_hexdump(MSG_DEBUG, "WPA: Replay Counter", reply->replay_counter, |
| 568 | WPA_REPLAY_COUNTER_LEN); |
| 569 | |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 570 | key_mic = (u8 *) (reply + 1); |
| 571 | WPA_PUT_BE16(key_mic + mic_len, wpa_ie_len); /* Key Data Length */ |
| 572 | os_memcpy(key_mic + mic_len + 2, wpa_ie, wpa_ie_len); /* Key Data */ |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 573 | os_free(rsn_ie_buf); |
| 574 | |
| 575 | os_memcpy(reply->key_nonce, nonce, WPA_NONCE_LEN); |
| 576 | |
Roshan Pius | 5e7db94 | 2018-04-12 12:27:41 -0700 | [diff] [blame] | 577 | wpa_dbg(sm->ctx->msg_ctx, MSG_INFO, "WPA: Sending EAPOL-Key 2/4"); |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 578 | return wpa_eapol_key_send(sm, ptk, ver, dst, ETH_P_EAPOL, rbuf, rlen, |
| 579 | key_mic); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 580 | } |
| 581 | |
| 582 | |
| 583 | static int wpa_derive_ptk(struct wpa_sm *sm, const unsigned char *src_addr, |
Dmitry Shmidt | 807291d | 2015-01-27 13:40:23 -0800 | [diff] [blame] | 584 | const struct wpa_eapol_key *key, struct wpa_ptk *ptk) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 585 | { |
Hai Shalom | 021b0b5 | 2019-04-10 11:17:58 -0700 | [diff] [blame] | 586 | const u8 *z = NULL; |
Hai Shalom | 6084025 | 2021-02-19 19:02:11 -0800 | [diff] [blame] | 587 | size_t z_len = 0, kdk_len; |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 588 | int akmp; |
Hai Shalom | 021b0b5 | 2019-04-10 11:17:58 -0700 | [diff] [blame] | 589 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 590 | #ifdef CONFIG_IEEE80211R |
| 591 | if (wpa_key_mgmt_ft(sm->key_mgmt)) |
Dmitry Shmidt | 807291d | 2015-01-27 13:40:23 -0800 | [diff] [blame] | 592 | return wpa_derive_ptk_ft(sm, src_addr, key, ptk); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 593 | #endif /* CONFIG_IEEE80211R */ |
| 594 | |
Hai Shalom | 021b0b5 | 2019-04-10 11:17:58 -0700 | [diff] [blame] | 595 | #ifdef CONFIG_DPP2 |
| 596 | if (sm->key_mgmt == WPA_KEY_MGMT_DPP && sm->dpp_z) { |
| 597 | z = wpabuf_head(sm->dpp_z); |
| 598 | z_len = wpabuf_len(sm->dpp_z); |
| 599 | } |
| 600 | #endif /* CONFIG_DPP2 */ |
| 601 | |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 602 | akmp = sm->key_mgmt; |
| 603 | #ifdef CONFIG_OWE |
| 604 | if (sm->owe_ptk_workaround && akmp == WPA_KEY_MGMT_OWE && |
| 605 | sm->pmk_len > 32) { |
| 606 | wpa_printf(MSG_DEBUG, |
| 607 | "OWE: Force SHA256 for PTK derivation"); |
| 608 | akmp |= WPA_KEY_MGMT_PSK_SHA256; |
| 609 | } |
| 610 | #endif /* CONFIG_OWE */ |
Hai Shalom | 6084025 | 2021-02-19 19:02:11 -0800 | [diff] [blame] | 611 | |
| 612 | if (sm->force_kdk_derivation || |
Hai Shalom | c1a2144 | 2022-02-04 13:43:00 -0800 | [diff] [blame] | 613 | (sm->secure_ltf && |
| 614 | ieee802_11_rsnx_capab(sm->ap_rsnxe, WLAN_RSNX_CAPAB_SECURE_LTF))) |
Hai Shalom | 6084025 | 2021-02-19 19:02:11 -0800 | [diff] [blame] | 615 | kdk_len = WPA_KDK_MAX_LEN; |
| 616 | else |
| 617 | kdk_len = 0; |
| 618 | |
Dmitry Shmidt | 807291d | 2015-01-27 13:40:23 -0800 | [diff] [blame] | 619 | return wpa_pmk_to_ptk(sm->pmk, sm->pmk_len, "Pairwise key expansion", |
| 620 | sm->own_addr, sm->bssid, sm->snonce, |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 621 | key->key_nonce, ptk, akmp, |
Hai Shalom | 6084025 | 2021-02-19 19:02:11 -0800 | [diff] [blame] | 622 | sm->pairwise_cipher, z, z_len, |
| 623 | kdk_len); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 624 | } |
| 625 | |
| 626 | |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 627 | static int wpa_handle_ext_key_id(struct wpa_sm *sm, |
| 628 | struct wpa_eapol_ie_parse *kde) |
| 629 | { |
| 630 | if (sm->ext_key_id) { |
| 631 | u16 key_id; |
| 632 | |
| 633 | if (!kde->key_id) { |
| 634 | wpa_msg(sm->ctx->msg_ctx, |
| 635 | sm->use_ext_key_id ? MSG_INFO : MSG_DEBUG, |
| 636 | "RSN: No Key ID in Extended Key ID handshake"); |
| 637 | sm->keyidx_active = 0; |
| 638 | return sm->use_ext_key_id ? -1 : 0; |
| 639 | } |
| 640 | |
| 641 | key_id = kde->key_id[0] & 0x03; |
| 642 | if (key_id > 1) { |
| 643 | wpa_msg(sm->ctx->msg_ctx, MSG_INFO, |
| 644 | "RSN: Invalid Extended Key ID: %d", key_id); |
| 645 | return -1; |
| 646 | } |
| 647 | wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, |
| 648 | "RSN: Using Extended Key ID %d", key_id); |
| 649 | sm->keyidx_active = key_id; |
| 650 | sm->use_ext_key_id = 1; |
| 651 | } else { |
| 652 | if (kde->key_id && (kde->key_id[0] & 0x03)) { |
| 653 | wpa_msg(sm->ctx->msg_ctx, MSG_INFO, |
| 654 | "RSN: Non-zero Extended Key ID Key ID in PTK0 handshake"); |
| 655 | return -1; |
| 656 | } |
| 657 | |
| 658 | if (kde->key_id) { |
| 659 | /* This is not supposed to be included here, but ignore |
| 660 | * the case of matching Key ID 0 just in case. */ |
| 661 | wpa_msg(sm->ctx->msg_ctx, MSG_DEBUG, |
| 662 | "RSN: Extended Key ID Key ID 0 in PTK0 handshake"); |
| 663 | } |
| 664 | sm->keyidx_active = 0; |
| 665 | sm->use_ext_key_id = 0; |
| 666 | } |
| 667 | |
| 668 | return 0; |
| 669 | } |
| 670 | |
| 671 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 672 | static void wpa_supplicant_process_1_of_4(struct wpa_sm *sm, |
| 673 | const unsigned char *src_addr, |
| 674 | const struct wpa_eapol_key *key, |
Dmitry Shmidt | 43cb578 | 2014-06-16 16:23:22 -0700 | [diff] [blame] | 675 | u16 ver, const u8 *key_data, |
| 676 | size_t key_data_len) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 677 | { |
| 678 | struct wpa_eapol_ie_parse ie; |
| 679 | struct wpa_ptk *ptk; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 680 | int res; |
Dmitry Shmidt | cf32e60 | 2014-01-28 10:57:39 -0800 | [diff] [blame] | 681 | u8 *kde, *kde_buf = NULL; |
| 682 | size_t kde_len; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 683 | |
| 684 | if (wpa_sm_get_network_ctx(sm) == NULL) { |
| 685 | wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, "WPA: No SSID info " |
| 686 | "found (msg 1 of 4)"); |
| 687 | return; |
| 688 | } |
| 689 | |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 690 | if (sm->wpa_deny_ptk0_rekey && !sm->use_ext_key_id && |
| 691 | wpa_sm_get_state(sm) == WPA_COMPLETED) { |
| 692 | wpa_msg(sm->ctx->msg_ctx, MSG_INFO, |
| 693 | "WPA: PTK0 rekey not allowed, reconnecting"); |
| 694 | wpa_sm_reconnect(sm); |
| 695 | return; |
| 696 | } |
| 697 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 698 | wpa_sm_set_state(sm, WPA_4WAY_HANDSHAKE); |
Roshan Pius | 5e7db94 | 2018-04-12 12:27:41 -0700 | [diff] [blame] | 699 | wpa_dbg(sm->ctx->msg_ctx, MSG_INFO, "WPA: RX message 1 of 4-Way " |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 700 | "Handshake from " MACSTR " (ver=%d)", MAC2STR(src_addr), ver); |
| 701 | |
| 702 | os_memset(&ie, 0, sizeof(ie)); |
| 703 | |
Dmitry Shmidt | f21452a | 2014-02-26 10:55:25 -0800 | [diff] [blame] | 704 | if (sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 705 | /* RSN: msg 1/4 should contain PMKID for the selected PMK */ |
Dmitry Shmidt | 43cb578 | 2014-06-16 16:23:22 -0700 | [diff] [blame] | 706 | wpa_hexdump(MSG_DEBUG, "RSN: msg 1/4 key data", |
| 707 | key_data, key_data_len); |
| 708 | if (wpa_supplicant_parse_ies(key_data, key_data_len, &ie) < 0) |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 709 | goto failed; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 710 | if (ie.pmkid) { |
| 711 | wpa_hexdump(MSG_DEBUG, "RSN: PMKID from " |
| 712 | "Authenticator", ie.pmkid, PMKID_LEN); |
| 713 | } |
| 714 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 715 | |
| 716 | res = wpa_supplicant_get_pmk(sm, src_addr, ie.pmkid); |
| 717 | if (res == -2) { |
| 718 | wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "RSN: Do not reply to " |
| 719 | "msg 1/4 - requesting full EAP authentication"); |
| 720 | return; |
| 721 | } |
| 722 | if (res) |
| 723 | goto failed; |
| 724 | |
| 725 | if (sm->renew_snonce) { |
| 726 | if (random_get_bytes(sm->snonce, WPA_NONCE_LEN)) { |
| 727 | wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, |
| 728 | "WPA: Failed to get random data for SNonce"); |
| 729 | goto failed; |
| 730 | } |
| 731 | sm->renew_snonce = 0; |
| 732 | wpa_hexdump(MSG_DEBUG, "WPA: Renewed SNonce", |
| 733 | sm->snonce, WPA_NONCE_LEN); |
| 734 | } |
| 735 | |
| 736 | /* Calculate PTK which will be stored as a temporary PTK until it has |
| 737 | * been verified when processing message 3/4. */ |
| 738 | ptk = &sm->tptk; |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 739 | if (wpa_derive_ptk(sm, src_addr, key, ptk) < 0) |
| 740 | goto failed; |
Dmitry Shmidt | 9866086 | 2014-03-11 17:26:21 -0700 | [diff] [blame] | 741 | if (sm->pairwise_cipher == WPA_CIPHER_TKIP) { |
Dmitry Shmidt | 61593f0 | 2014-04-21 16:27:35 -0700 | [diff] [blame] | 742 | u8 buf[8]; |
Dmitry Shmidt | 9866086 | 2014-03-11 17:26:21 -0700 | [diff] [blame] | 743 | /* Supplicant: swap tx/rx Mic keys */ |
Dmitry Shmidt | 807291d | 2015-01-27 13:40:23 -0800 | [diff] [blame] | 744 | os_memcpy(buf, &ptk->tk[16], 8); |
| 745 | os_memcpy(&ptk->tk[16], &ptk->tk[24], 8); |
| 746 | os_memcpy(&ptk->tk[24], buf, 8); |
Hai Shalom | 81f62d8 | 2019-07-22 12:10:00 -0700 | [diff] [blame] | 747 | forced_memzero(buf, sizeof(buf)); |
Dmitry Shmidt | 9866086 | 2014-03-11 17:26:21 -0700 | [diff] [blame] | 748 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 749 | sm->tptk_set = 1; |
| 750 | |
Dmitry Shmidt | cf32e60 | 2014-01-28 10:57:39 -0800 | [diff] [blame] | 751 | kde = sm->assoc_wpa_ie; |
| 752 | kde_len = sm->assoc_wpa_ie_len; |
Hai Shalom | c356592 | 2019-10-28 11:58:20 -0700 | [diff] [blame] | 753 | kde_buf = os_malloc(kde_len + |
| 754 | 2 + RSN_SELECTOR_LEN + 3 + |
| 755 | sm->assoc_rsnxe_len + |
Hai Shalom | 4fbc08f | 2020-05-18 12:37:00 -0700 | [diff] [blame] | 756 | 2 + RSN_SELECTOR_LEN + 1 + |
| 757 | 2 + RSN_SELECTOR_LEN + 2); |
Hai Shalom | c356592 | 2019-10-28 11:58:20 -0700 | [diff] [blame] | 758 | if (!kde_buf) |
| 759 | goto failed; |
| 760 | os_memcpy(kde_buf, kde, kde_len); |
| 761 | kde = kde_buf; |
Dmitry Shmidt | cf32e60 | 2014-01-28 10:57:39 -0800 | [diff] [blame] | 762 | |
Hai Shalom | 74f70d4 | 2019-02-11 14:42:39 -0800 | [diff] [blame] | 763 | #ifdef CONFIG_OCV |
| 764 | if (wpa_sm_ocv_enabled(sm)) { |
| 765 | struct wpa_channel_info ci; |
| 766 | u8 *pos; |
| 767 | |
Hai Shalom | c356592 | 2019-10-28 11:58:20 -0700 | [diff] [blame] | 768 | pos = kde + kde_len; |
Hai Shalom | 74f70d4 | 2019-02-11 14:42:39 -0800 | [diff] [blame] | 769 | if (wpa_sm_channel_info(sm, &ci) != 0) { |
| 770 | wpa_printf(MSG_WARNING, |
| 771 | "Failed to get channel info for OCI element in EAPOL-Key 2/4"); |
| 772 | goto failed; |
| 773 | } |
Hai Shalom | 899fcc7 | 2020-10-19 14:38:18 -0700 | [diff] [blame] | 774 | #ifdef CONFIG_TESTING_OPTIONS |
| 775 | if (sm->oci_freq_override_eapol) { |
| 776 | wpa_printf(MSG_INFO, |
| 777 | "TEST: Override OCI KDE frequency %d -> %d MHz", |
| 778 | ci.frequency, sm->oci_freq_override_eapol); |
| 779 | ci.frequency = sm->oci_freq_override_eapol; |
| 780 | } |
| 781 | #endif /* CONFIG_TESTING_OPTIONS */ |
Hai Shalom | 74f70d4 | 2019-02-11 14:42:39 -0800 | [diff] [blame] | 782 | |
Hai Shalom | 74f70d4 | 2019-02-11 14:42:39 -0800 | [diff] [blame] | 783 | if (ocv_insert_oci_kde(&ci, &pos) < 0) |
| 784 | goto failed; |
| 785 | kde_len = pos - kde; |
| 786 | } |
| 787 | #endif /* CONFIG_OCV */ |
| 788 | |
Hai Shalom | c356592 | 2019-10-28 11:58:20 -0700 | [diff] [blame] | 789 | if (sm->assoc_rsnxe && sm->assoc_rsnxe_len) { |
| 790 | os_memcpy(kde + kde_len, sm->assoc_rsnxe, sm->assoc_rsnxe_len); |
| 791 | kde_len += sm->assoc_rsnxe_len; |
| 792 | } |
| 793 | |
Dmitry Shmidt | cf32e60 | 2014-01-28 10:57:39 -0800 | [diff] [blame] | 794 | #ifdef CONFIG_P2P |
| 795 | if (sm->p2p) { |
Hai Shalom | c356592 | 2019-10-28 11:58:20 -0700 | [diff] [blame] | 796 | u8 *pos; |
| 797 | |
| 798 | wpa_printf(MSG_DEBUG, |
| 799 | "P2P: Add IP Address Request KDE into EAPOL-Key 2/4"); |
| 800 | pos = kde + kde_len; |
| 801 | *pos++ = WLAN_EID_VENDOR_SPECIFIC; |
| 802 | *pos++ = RSN_SELECTOR_LEN + 1; |
| 803 | RSN_SELECTOR_PUT(pos, WFA_KEY_DATA_IP_ADDR_REQ); |
| 804 | pos += RSN_SELECTOR_LEN; |
| 805 | *pos++ = 0x01; |
| 806 | kde_len = pos - kde; |
Dmitry Shmidt | cf32e60 | 2014-01-28 10:57:39 -0800 | [diff] [blame] | 807 | } |
| 808 | #endif /* CONFIG_P2P */ |
| 809 | |
Hai Shalom | 4fbc08f | 2020-05-18 12:37:00 -0700 | [diff] [blame] | 810 | #ifdef CONFIG_DPP2 |
| 811 | if (DPP_VERSION > 1 && sm->key_mgmt == WPA_KEY_MGMT_DPP) { |
| 812 | u8 *pos; |
| 813 | |
| 814 | wpa_printf(MSG_DEBUG, "DPP: Add DPP KDE into EAPOL-Key 2/4"); |
| 815 | pos = kde + kde_len; |
| 816 | *pos++ = WLAN_EID_VENDOR_SPECIFIC; |
| 817 | *pos++ = RSN_SELECTOR_LEN + 2; |
| 818 | RSN_SELECTOR_PUT(pos, WFA_KEY_DATA_DPP); |
| 819 | pos += RSN_SELECTOR_LEN; |
| 820 | *pos++ = DPP_VERSION; /* Protocol Version */ |
| 821 | *pos = 0; /* Flags */ |
| 822 | if (sm->dpp_pfs == 0) |
| 823 | *pos |= DPP_KDE_PFS_ALLOWED; |
| 824 | else if (sm->dpp_pfs == 1) |
| 825 | *pos |= DPP_KDE_PFS_ALLOWED | DPP_KDE_PFS_REQUIRED; |
| 826 | pos++; |
| 827 | kde_len = pos - kde; |
| 828 | } |
| 829 | #endif /* CONFIG_DPP2 */ |
| 830 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 831 | if (wpa_supplicant_send_2_of_4(sm, sm->bssid, key, ver, sm->snonce, |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 832 | kde, kde_len, ptk) < 0) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 833 | goto failed; |
| 834 | |
Dmitry Shmidt | cf32e60 | 2014-01-28 10:57:39 -0800 | [diff] [blame] | 835 | os_free(kde_buf); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 836 | os_memcpy(sm->anonce, key->key_nonce, WPA_NONCE_LEN); |
| 837 | return; |
| 838 | |
| 839 | failed: |
Dmitry Shmidt | cf32e60 | 2014-01-28 10:57:39 -0800 | [diff] [blame] | 840 | os_free(kde_buf); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 841 | wpa_sm_deauthenticate(sm, WLAN_REASON_UNSPECIFIED); |
| 842 | } |
| 843 | |
| 844 | |
| 845 | static void wpa_sm_start_preauth(void *eloop_ctx, void *timeout_ctx) |
| 846 | { |
| 847 | struct wpa_sm *sm = eloop_ctx; |
| 848 | rsn_preauth_candidate_process(sm); |
| 849 | } |
| 850 | |
| 851 | |
| 852 | static void wpa_supplicant_key_neg_complete(struct wpa_sm *sm, |
| 853 | const u8 *addr, int secure) |
| 854 | { |
| 855 | wpa_msg(sm->ctx->msg_ctx, MSG_INFO, |
| 856 | "WPA: Key negotiation completed with " |
| 857 | MACSTR " [PTK=%s GTK=%s]", MAC2STR(addr), |
| 858 | wpa_cipher_txt(sm->pairwise_cipher), |
| 859 | wpa_cipher_txt(sm->group_cipher)); |
| 860 | wpa_sm_cancel_auth_timeout(sm); |
| 861 | wpa_sm_set_state(sm, WPA_COMPLETED); |
| 862 | |
| 863 | if (secure) { |
| 864 | wpa_sm_mlme_setprotection( |
| 865 | sm, addr, MLME_SETPROTECTION_PROTECT_TYPE_RX_TX, |
| 866 | MLME_SETPROTECTION_KEY_TYPE_PAIRWISE); |
Hai Shalom | e21d4e8 | 2020-04-29 16:34:06 -0700 | [diff] [blame] | 867 | eapol_sm_notify_portValid(sm->eapol, true); |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 868 | if (wpa_key_mgmt_wpa_psk(sm->key_mgmt) || |
| 869 | sm->key_mgmt == WPA_KEY_MGMT_DPP || |
| 870 | sm->key_mgmt == WPA_KEY_MGMT_OWE) |
Hai Shalom | e21d4e8 | 2020-04-29 16:34:06 -0700 | [diff] [blame] | 871 | eapol_sm_notify_eap_success(sm->eapol, true); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 872 | /* |
| 873 | * Start preauthentication after a short wait to avoid a |
| 874 | * possible race condition between the data receive and key |
| 875 | * configuration after the 4-Way Handshake. This increases the |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 876 | * likelihood of the first preauth EAPOL-Start frame getting to |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 877 | * the target AP. |
| 878 | */ |
Hai Shalom | 74f70d4 | 2019-02-11 14:42:39 -0800 | [diff] [blame] | 879 | if (!dl_list_empty(&sm->pmksa_candidates)) |
| 880 | eloop_register_timeout(1, 0, wpa_sm_start_preauth, |
| 881 | sm, NULL); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 882 | } |
| 883 | |
| 884 | if (sm->cur_pmksa && sm->cur_pmksa->opportunistic) { |
| 885 | wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, |
| 886 | "RSN: Authenticator accepted " |
| 887 | "opportunistic PMKSA entry - marking it valid"); |
| 888 | sm->cur_pmksa->opportunistic = 0; |
| 889 | } |
| 890 | |
| 891 | #ifdef CONFIG_IEEE80211R |
| 892 | if (wpa_key_mgmt_ft(sm->key_mgmt)) { |
| 893 | /* Prepare for the next transition */ |
| 894 | wpa_ft_prepare_auth_request(sm, NULL); |
| 895 | } |
| 896 | #endif /* CONFIG_IEEE80211R */ |
| 897 | } |
| 898 | |
| 899 | |
| 900 | static void wpa_sm_rekey_ptk(void *eloop_ctx, void *timeout_ctx) |
| 901 | { |
| 902 | struct wpa_sm *sm = eloop_ctx; |
| 903 | wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Request PTK rekeying"); |
| 904 | wpa_sm_key_request(sm, 0, 1); |
| 905 | } |
| 906 | |
| 907 | |
| 908 | static int wpa_supplicant_install_ptk(struct wpa_sm *sm, |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 909 | const struct wpa_eapol_key *key, |
| 910 | enum key_flag key_flag) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 911 | { |
| 912 | int keylen, rsclen; |
| 913 | enum wpa_alg alg; |
| 914 | const u8 *key_rsc; |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 915 | |
Mathy Vanhoef | c66556c | 2017-09-29 04:22:51 +0200 | [diff] [blame] | 916 | if (sm->ptk.installed) { |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 917 | wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, |
| 918 | "WPA: Do not re-install same PTK to the driver"); |
| 919 | return 0; |
| 920 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 921 | |
| 922 | wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, |
| 923 | "WPA: Installing PTK to the driver"); |
| 924 | |
Dmitry Shmidt | 4530cfd | 2012-09-09 15:20:40 -0700 | [diff] [blame] | 925 | if (sm->pairwise_cipher == WPA_CIPHER_NONE) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 926 | wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Pairwise Cipher " |
| 927 | "Suite: NONE - do not use pairwise keys"); |
| 928 | return 0; |
Dmitry Shmidt | 4530cfd | 2012-09-09 15:20:40 -0700 | [diff] [blame] | 929 | } |
| 930 | |
| 931 | if (!wpa_cipher_valid_pairwise(sm->pairwise_cipher)) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 932 | wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, |
| 933 | "WPA: Unsupported pairwise cipher %d", |
| 934 | sm->pairwise_cipher); |
| 935 | return -1; |
| 936 | } |
| 937 | |
Dmitry Shmidt | 4530cfd | 2012-09-09 15:20:40 -0700 | [diff] [blame] | 938 | alg = wpa_cipher_to_alg(sm->pairwise_cipher); |
| 939 | keylen = wpa_cipher_key_len(sm->pairwise_cipher); |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 940 | if (keylen <= 0 || (unsigned int) keylen != sm->ptk.tk_len) { |
| 941 | wpa_printf(MSG_DEBUG, "WPA: TK length mismatch: %d != %lu", |
| 942 | keylen, (long unsigned int) sm->ptk.tk_len); |
| 943 | return -1; |
| 944 | } |
Dmitry Shmidt | 4530cfd | 2012-09-09 15:20:40 -0700 | [diff] [blame] | 945 | rsclen = wpa_cipher_rsc_len(sm->pairwise_cipher); |
| 946 | |
Dmitry Shmidt | f21452a | 2014-02-26 10:55:25 -0800 | [diff] [blame] | 947 | if (sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 948 | key_rsc = null_rsc; |
| 949 | } else { |
| 950 | key_rsc = key->key_rsc; |
| 951 | wpa_hexdump(MSG_DEBUG, "WPA: RSC", key_rsc, rsclen); |
| 952 | } |
| 953 | |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 954 | if (wpa_sm_set_key(sm, alg, sm->bssid, sm->keyidx_active, 1, key_rsc, |
| 955 | rsclen, sm->ptk.tk, keylen, |
| 956 | KEY_FLAG_PAIRWISE | key_flag) < 0) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 957 | wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 958 | "WPA: Failed to set PTK to the driver (alg=%d keylen=%d bssid=" |
| 959 | MACSTR " idx=%d key_flag=0x%x)", |
| 960 | alg, keylen, MAC2STR(sm->bssid), |
| 961 | sm->keyidx_active, key_flag); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 962 | return -1; |
| 963 | } |
| 964 | |
Hai Shalom | 6084025 | 2021-02-19 19:02:11 -0800 | [diff] [blame] | 965 | wpa_sm_store_ptk(sm, sm->bssid, sm->pairwise_cipher, |
| 966 | sm->dot11RSNAConfigPMKLifetime, &sm->ptk); |
| 967 | |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 968 | /* TK is not needed anymore in supplicant */ |
Dmitry Shmidt | 807291d | 2015-01-27 13:40:23 -0800 | [diff] [blame] | 969 | os_memset(sm->ptk.tk, 0, WPA_TK_MAX_LEN); |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 970 | sm->ptk.tk_len = 0; |
Mathy Vanhoef | c66556c | 2017-09-29 04:22:51 +0200 | [diff] [blame] | 971 | sm->ptk.installed = 1; |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 972 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 973 | if (sm->wpa_ptk_rekey) { |
| 974 | eloop_cancel_timeout(wpa_sm_rekey_ptk, sm, NULL); |
| 975 | eloop_register_timeout(sm->wpa_ptk_rekey, 0, wpa_sm_rekey_ptk, |
| 976 | sm, NULL); |
| 977 | } |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 978 | return 0; |
| 979 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 980 | |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 981 | |
| 982 | static int wpa_supplicant_activate_ptk(struct wpa_sm *sm) |
| 983 | { |
| 984 | wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, |
| 985 | "WPA: Activate PTK (idx=%d bssid=" MACSTR ")", |
| 986 | sm->keyidx_active, MAC2STR(sm->bssid)); |
| 987 | |
| 988 | if (wpa_sm_set_key(sm, 0, sm->bssid, sm->keyidx_active, 0, NULL, 0, |
| 989 | NULL, 0, KEY_FLAG_PAIRWISE_RX_TX_MODIFY) < 0) { |
| 990 | wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, |
| 991 | "WPA: Failed to activate PTK for TX (idx=%d bssid=" |
| 992 | MACSTR ")", sm->keyidx_active, MAC2STR(sm->bssid)); |
| 993 | return -1; |
| 994 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 995 | return 0; |
| 996 | } |
| 997 | |
| 998 | |
| 999 | static int wpa_supplicant_check_group_cipher(struct wpa_sm *sm, |
| 1000 | int group_cipher, |
| 1001 | int keylen, int maxkeylen, |
| 1002 | int *key_rsc_len, |
| 1003 | enum wpa_alg *alg) |
| 1004 | { |
Dmitry Shmidt | 4530cfd | 2012-09-09 15:20:40 -0700 | [diff] [blame] | 1005 | int klen; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1006 | |
Dmitry Shmidt | 4530cfd | 2012-09-09 15:20:40 -0700 | [diff] [blame] | 1007 | *alg = wpa_cipher_to_alg(group_cipher); |
| 1008 | if (*alg == WPA_ALG_NONE) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1009 | wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, |
| 1010 | "WPA: Unsupported Group Cipher %d", |
| 1011 | group_cipher); |
| 1012 | return -1; |
| 1013 | } |
Dmitry Shmidt | 4530cfd | 2012-09-09 15:20:40 -0700 | [diff] [blame] | 1014 | *key_rsc_len = wpa_cipher_rsc_len(group_cipher); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1015 | |
Dmitry Shmidt | 4530cfd | 2012-09-09 15:20:40 -0700 | [diff] [blame] | 1016 | klen = wpa_cipher_key_len(group_cipher); |
| 1017 | if (keylen != klen || maxkeylen < klen) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1018 | wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, |
| 1019 | "WPA: Unsupported %s Group Cipher key length %d (%d)", |
| 1020 | wpa_cipher_txt(group_cipher), keylen, maxkeylen); |
Dmitry Shmidt | 4530cfd | 2012-09-09 15:20:40 -0700 | [diff] [blame] | 1021 | return -1; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1022 | } |
Dmitry Shmidt | 4530cfd | 2012-09-09 15:20:40 -0700 | [diff] [blame] | 1023 | return 0; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1024 | } |
| 1025 | |
| 1026 | |
| 1027 | struct wpa_gtk_data { |
| 1028 | enum wpa_alg alg; |
| 1029 | int tx, key_rsc_len, keyidx; |
| 1030 | u8 gtk[32]; |
| 1031 | int gtk_len; |
| 1032 | }; |
| 1033 | |
| 1034 | |
| 1035 | static int wpa_supplicant_install_gtk(struct wpa_sm *sm, |
| 1036 | const struct wpa_gtk_data *gd, |
Jouni Malinen | 58c0e96 | 2017-10-01 12:12:24 +0300 | [diff] [blame] | 1037 | const u8 *key_rsc, int wnm_sleep) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1038 | { |
| 1039 | const u8 *_gtk = gd->gtk; |
| 1040 | u8 gtk_buf[32]; |
| 1041 | |
Mathy Vanhoef | 10bfd64 | 2017-07-12 16:03:24 +0200 | [diff] [blame] | 1042 | /* Detect possible key reinstallation */ |
Jouni Malinen | 58c0e96 | 2017-10-01 12:12:24 +0300 | [diff] [blame] | 1043 | if ((sm->gtk.gtk_len == (size_t) gd->gtk_len && |
| 1044 | os_memcmp(sm->gtk.gtk, gd->gtk, sm->gtk.gtk_len) == 0) || |
| 1045 | (sm->gtk_wnm_sleep.gtk_len == (size_t) gd->gtk_len && |
| 1046 | os_memcmp(sm->gtk_wnm_sleep.gtk, gd->gtk, |
| 1047 | sm->gtk_wnm_sleep.gtk_len) == 0)) { |
Mathy Vanhoef | 10bfd64 | 2017-07-12 16:03:24 +0200 | [diff] [blame] | 1048 | wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, |
| 1049 | "WPA: Not reinstalling already in-use GTK to the driver (keyidx=%d tx=%d len=%d)", |
| 1050 | gd->keyidx, gd->tx, gd->gtk_len); |
| 1051 | return 0; |
| 1052 | } |
| 1053 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1054 | wpa_hexdump_key(MSG_DEBUG, "WPA: Group Key", gd->gtk, gd->gtk_len); |
| 1055 | wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, |
| 1056 | "WPA: Installing GTK to the driver (keyidx=%d tx=%d len=%d)", |
| 1057 | gd->keyidx, gd->tx, gd->gtk_len); |
| 1058 | wpa_hexdump(MSG_DEBUG, "WPA: RSC", key_rsc, gd->key_rsc_len); |
| 1059 | if (sm->group_cipher == WPA_CIPHER_TKIP) { |
| 1060 | /* Swap Tx/Rx keys for Michael MIC */ |
| 1061 | os_memcpy(gtk_buf, gd->gtk, 16); |
| 1062 | os_memcpy(gtk_buf + 16, gd->gtk + 24, 8); |
| 1063 | os_memcpy(gtk_buf + 24, gd->gtk + 16, 8); |
| 1064 | _gtk = gtk_buf; |
| 1065 | } |
| 1066 | if (sm->pairwise_cipher == WPA_CIPHER_NONE) { |
| 1067 | if (wpa_sm_set_key(sm, gd->alg, NULL, |
| 1068 | gd->keyidx, 1, key_rsc, gd->key_rsc_len, |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 1069 | _gtk, gd->gtk_len, |
| 1070 | KEY_FLAG_GROUP_RX_TX_DEFAULT) < 0) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1071 | wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, |
| 1072 | "WPA: Failed to set GTK to the driver " |
| 1073 | "(Group only)"); |
Hai Shalom | 81f62d8 | 2019-07-22 12:10:00 -0700 | [diff] [blame] | 1074 | forced_memzero(gtk_buf, sizeof(gtk_buf)); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1075 | return -1; |
| 1076 | } |
| 1077 | } else if (wpa_sm_set_key(sm, gd->alg, broadcast_ether_addr, |
| 1078 | gd->keyidx, gd->tx, key_rsc, gd->key_rsc_len, |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 1079 | _gtk, gd->gtk_len, KEY_FLAG_GROUP_RX) < 0) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1080 | wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, |
| 1081 | "WPA: Failed to set GTK to " |
| 1082 | "the driver (alg=%d keylen=%d keyidx=%d)", |
| 1083 | gd->alg, gd->gtk_len, gd->keyidx); |
Hai Shalom | 81f62d8 | 2019-07-22 12:10:00 -0700 | [diff] [blame] | 1084 | forced_memzero(gtk_buf, sizeof(gtk_buf)); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1085 | return -1; |
| 1086 | } |
Hai Shalom | 81f62d8 | 2019-07-22 12:10:00 -0700 | [diff] [blame] | 1087 | forced_memzero(gtk_buf, sizeof(gtk_buf)); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1088 | |
Jouni Malinen | 58c0e96 | 2017-10-01 12:12:24 +0300 | [diff] [blame] | 1089 | if (wnm_sleep) { |
| 1090 | sm->gtk_wnm_sleep.gtk_len = gd->gtk_len; |
| 1091 | os_memcpy(sm->gtk_wnm_sleep.gtk, gd->gtk, |
| 1092 | sm->gtk_wnm_sleep.gtk_len); |
| 1093 | } else { |
| 1094 | sm->gtk.gtk_len = gd->gtk_len; |
| 1095 | os_memcpy(sm->gtk.gtk, gd->gtk, sm->gtk.gtk_len); |
| 1096 | } |
Mathy Vanhoef | 10bfd64 | 2017-07-12 16:03:24 +0200 | [diff] [blame] | 1097 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1098 | return 0; |
| 1099 | } |
| 1100 | |
| 1101 | |
| 1102 | static int wpa_supplicant_gtk_tx_bit_workaround(const struct wpa_sm *sm, |
| 1103 | int tx) |
| 1104 | { |
| 1105 | if (tx && sm->pairwise_cipher != WPA_CIPHER_NONE) { |
| 1106 | /* Ignore Tx bit for GTK if a pairwise key is used. One AP |
| 1107 | * seemed to set this bit (incorrectly, since Tx is only when |
| 1108 | * doing Group Key only APs) and without this workaround, the |
| 1109 | * data connection does not work because wpa_supplicant |
| 1110 | * configured non-zero keyidx to be used for unicast. */ |
| 1111 | wpa_msg(sm->ctx->msg_ctx, MSG_INFO, |
| 1112 | "WPA: Tx bit set for GTK, but pairwise " |
| 1113 | "keys are used - ignore Tx bit"); |
| 1114 | return 0; |
| 1115 | } |
| 1116 | return tx; |
| 1117 | } |
| 1118 | |
| 1119 | |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 1120 | static int wpa_supplicant_rsc_relaxation(const struct wpa_sm *sm, |
| 1121 | const u8 *rsc) |
| 1122 | { |
| 1123 | int rsclen; |
| 1124 | |
| 1125 | if (!sm->wpa_rsc_relaxation) |
| 1126 | return 0; |
| 1127 | |
| 1128 | rsclen = wpa_cipher_rsc_len(sm->group_cipher); |
| 1129 | |
| 1130 | /* |
| 1131 | * Try to detect RSC (endian) corruption issue where the AP sends |
| 1132 | * the RSC bytes in EAPOL-Key message in the wrong order, both if |
| 1133 | * it's actually a 6-byte field (as it should be) and if it treats |
| 1134 | * it as an 8-byte field. |
| 1135 | * An AP model known to have this bug is the Sapido RB-1632. |
| 1136 | */ |
| 1137 | if (rsclen == 6 && ((rsc[5] && !rsc[0]) || rsc[6] || rsc[7])) { |
| 1138 | wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, |
| 1139 | "RSC %02x%02x%02x%02x%02x%02x%02x%02x is likely bogus, using 0", |
| 1140 | rsc[0], rsc[1], rsc[2], rsc[3], |
| 1141 | rsc[4], rsc[5], rsc[6], rsc[7]); |
| 1142 | |
| 1143 | return 1; |
| 1144 | } |
| 1145 | |
| 1146 | return 0; |
| 1147 | } |
| 1148 | |
| 1149 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1150 | static int wpa_supplicant_pairwise_gtk(struct wpa_sm *sm, |
| 1151 | const struct wpa_eapol_key *key, |
| 1152 | const u8 *gtk, size_t gtk_len, |
| 1153 | int key_info) |
| 1154 | { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1155 | struct wpa_gtk_data gd; |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 1156 | const u8 *key_rsc; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1157 | |
| 1158 | /* |
| 1159 | * IEEE Std 802.11i-2004 - 8.5.2 EAPOL-Key frames - Figure 43x |
| 1160 | * GTK KDE format: |
| 1161 | * KeyID[bits 0-1], Tx [bit 2], Reserved [bits 3-7] |
| 1162 | * Reserved [bits 0-7] |
| 1163 | * GTK |
| 1164 | */ |
| 1165 | |
| 1166 | os_memset(&gd, 0, sizeof(gd)); |
| 1167 | wpa_hexdump_key(MSG_DEBUG, "RSN: received GTK in pairwise handshake", |
| 1168 | gtk, gtk_len); |
| 1169 | |
| 1170 | if (gtk_len < 2 || gtk_len - 2 > sizeof(gd.gtk)) |
| 1171 | return -1; |
| 1172 | |
| 1173 | gd.keyidx = gtk[0] & 0x3; |
| 1174 | gd.tx = wpa_supplicant_gtk_tx_bit_workaround(sm, |
| 1175 | !!(gtk[0] & BIT(2))); |
| 1176 | gtk += 2; |
| 1177 | gtk_len -= 2; |
| 1178 | |
| 1179 | os_memcpy(gd.gtk, gtk, gtk_len); |
| 1180 | gd.gtk_len = gtk_len; |
| 1181 | |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 1182 | key_rsc = key->key_rsc; |
| 1183 | if (wpa_supplicant_rsc_relaxation(sm, key->key_rsc)) |
| 1184 | key_rsc = null_rsc; |
| 1185 | |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 1186 | if (sm->group_cipher != WPA_CIPHER_GTK_NOT_USED && |
| 1187 | (wpa_supplicant_check_group_cipher(sm, sm->group_cipher, |
| 1188 | gtk_len, gtk_len, |
| 1189 | &gd.key_rsc_len, &gd.alg) || |
Jouni Malinen | 58c0e96 | 2017-10-01 12:12:24 +0300 | [diff] [blame] | 1190 | wpa_supplicant_install_gtk(sm, &gd, key_rsc, 0))) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1191 | wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, |
| 1192 | "RSN: Failed to install GTK"); |
Hai Shalom | 81f62d8 | 2019-07-22 12:10:00 -0700 | [diff] [blame] | 1193 | forced_memzero(&gd, sizeof(gd)); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1194 | return -1; |
| 1195 | } |
Hai Shalom | 81f62d8 | 2019-07-22 12:10:00 -0700 | [diff] [blame] | 1196 | forced_memzero(&gd, sizeof(gd)); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1197 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1198 | return 0; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1199 | } |
| 1200 | |
| 1201 | |
Mathy Vanhoef | 10bfd64 | 2017-07-12 16:03:24 +0200 | [diff] [blame] | 1202 | static int wpa_supplicant_install_igtk(struct wpa_sm *sm, |
Jouni Malinen | 58c0e96 | 2017-10-01 12:12:24 +0300 | [diff] [blame] | 1203 | const struct wpa_igtk_kde *igtk, |
| 1204 | int wnm_sleep) |
Mathy Vanhoef | 10bfd64 | 2017-07-12 16:03:24 +0200 | [diff] [blame] | 1205 | { |
| 1206 | size_t len = wpa_cipher_key_len(sm->mgmt_group_cipher); |
| 1207 | u16 keyidx = WPA_GET_LE16(igtk->keyid); |
| 1208 | |
| 1209 | /* Detect possible key reinstallation */ |
Jouni Malinen | 58c0e96 | 2017-10-01 12:12:24 +0300 | [diff] [blame] | 1210 | if ((sm->igtk.igtk_len == len && |
| 1211 | os_memcmp(sm->igtk.igtk, igtk->igtk, sm->igtk.igtk_len) == 0) || |
| 1212 | (sm->igtk_wnm_sleep.igtk_len == len && |
| 1213 | os_memcmp(sm->igtk_wnm_sleep.igtk, igtk->igtk, |
| 1214 | sm->igtk_wnm_sleep.igtk_len) == 0)) { |
Mathy Vanhoef | 10bfd64 | 2017-07-12 16:03:24 +0200 | [diff] [blame] | 1215 | wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, |
| 1216 | "WPA: Not reinstalling already in-use IGTK to the driver (keyidx=%d)", |
| 1217 | keyidx); |
| 1218 | return 0; |
| 1219 | } |
| 1220 | |
| 1221 | wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, |
Hai Shalom | 39ba6fc | 2019-01-22 12:40:38 -0800 | [diff] [blame] | 1222 | "WPA: IGTK keyid %d pn " COMPACT_MACSTR, |
Mathy Vanhoef | 10bfd64 | 2017-07-12 16:03:24 +0200 | [diff] [blame] | 1223 | keyidx, MAC2STR(igtk->pn)); |
| 1224 | wpa_hexdump_key(MSG_DEBUG, "WPA: IGTK", igtk->igtk, len); |
| 1225 | if (keyidx > 4095) { |
| 1226 | wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, |
| 1227 | "WPA: Invalid IGTK KeyID %d", keyidx); |
| 1228 | return -1; |
| 1229 | } |
| 1230 | if (wpa_sm_set_key(sm, wpa_cipher_to_alg(sm->mgmt_group_cipher), |
| 1231 | broadcast_ether_addr, |
| 1232 | keyidx, 0, igtk->pn, sizeof(igtk->pn), |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 1233 | igtk->igtk, len, KEY_FLAG_GROUP_RX) < 0) { |
Hai Shalom | 5f92bc9 | 2019-04-18 11:54:11 -0700 | [diff] [blame] | 1234 | if (keyidx == 0x0400 || keyidx == 0x0500) { |
| 1235 | /* Assume the AP has broken PMF implementation since it |
| 1236 | * seems to have swapped the KeyID bytes. The AP cannot |
| 1237 | * be trusted to implement BIP correctly or provide a |
| 1238 | * valid IGTK, so do not try to configure this key with |
| 1239 | * swapped KeyID bytes. Instead, continue without |
| 1240 | * configuring the IGTK so that the driver can drop any |
| 1241 | * received group-addressed robust management frames due |
| 1242 | * to missing keys. |
| 1243 | * |
| 1244 | * Normally, this error behavior would result in us |
| 1245 | * disconnecting, but there are number of deployed APs |
| 1246 | * with this broken behavior, so as an interoperability |
| 1247 | * workaround, allow the connection to proceed. */ |
| 1248 | wpa_msg(sm->ctx->msg_ctx, MSG_INFO, |
| 1249 | "WPA: Ignore IGTK configuration error due to invalid IGTK KeyID byte order"); |
| 1250 | } else { |
| 1251 | wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, |
| 1252 | "WPA: Failed to configure IGTK to the driver"); |
| 1253 | return -1; |
| 1254 | } |
Mathy Vanhoef | 10bfd64 | 2017-07-12 16:03:24 +0200 | [diff] [blame] | 1255 | } |
| 1256 | |
Jouni Malinen | 58c0e96 | 2017-10-01 12:12:24 +0300 | [diff] [blame] | 1257 | if (wnm_sleep) { |
| 1258 | sm->igtk_wnm_sleep.igtk_len = len; |
| 1259 | os_memcpy(sm->igtk_wnm_sleep.igtk, igtk->igtk, |
| 1260 | sm->igtk_wnm_sleep.igtk_len); |
| 1261 | } else { |
| 1262 | sm->igtk.igtk_len = len; |
| 1263 | os_memcpy(sm->igtk.igtk, igtk->igtk, sm->igtk.igtk_len); |
| 1264 | } |
Mathy Vanhoef | 10bfd64 | 2017-07-12 16:03:24 +0200 | [diff] [blame] | 1265 | |
| 1266 | return 0; |
| 1267 | } |
Mathy Vanhoef | 10bfd64 | 2017-07-12 16:03:24 +0200 | [diff] [blame] | 1268 | |
| 1269 | |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 1270 | static int wpa_supplicant_install_bigtk(struct wpa_sm *sm, |
| 1271 | const struct wpa_bigtk_kde *bigtk, |
| 1272 | int wnm_sleep) |
| 1273 | { |
| 1274 | size_t len = wpa_cipher_key_len(sm->mgmt_group_cipher); |
| 1275 | u16 keyidx = WPA_GET_LE16(bigtk->keyid); |
| 1276 | |
| 1277 | /* Detect possible key reinstallation */ |
| 1278 | if ((sm->bigtk.bigtk_len == len && |
| 1279 | os_memcmp(sm->bigtk.bigtk, bigtk->bigtk, |
| 1280 | sm->bigtk.bigtk_len) == 0) || |
| 1281 | (sm->bigtk_wnm_sleep.bigtk_len == len && |
| 1282 | os_memcmp(sm->bigtk_wnm_sleep.bigtk, bigtk->bigtk, |
| 1283 | sm->bigtk_wnm_sleep.bigtk_len) == 0)) { |
| 1284 | wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, |
| 1285 | "WPA: Not reinstalling already in-use BIGTK to the driver (keyidx=%d)", |
| 1286 | keyidx); |
| 1287 | return 0; |
| 1288 | } |
| 1289 | |
| 1290 | wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, |
| 1291 | "WPA: BIGTK keyid %d pn " COMPACT_MACSTR, |
| 1292 | keyidx, MAC2STR(bigtk->pn)); |
| 1293 | wpa_hexdump_key(MSG_DEBUG, "WPA: BIGTK", bigtk->bigtk, len); |
| 1294 | if (keyidx < 6 || keyidx > 7) { |
| 1295 | wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, |
| 1296 | "WPA: Invalid BIGTK KeyID %d", keyidx); |
| 1297 | return -1; |
| 1298 | } |
| 1299 | if (wpa_sm_set_key(sm, wpa_cipher_to_alg(sm->mgmt_group_cipher), |
| 1300 | broadcast_ether_addr, |
| 1301 | keyidx, 0, bigtk->pn, sizeof(bigtk->pn), |
| 1302 | bigtk->bigtk, len, KEY_FLAG_GROUP_RX) < 0) { |
| 1303 | wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, |
| 1304 | "WPA: Failed to configure BIGTK to the driver"); |
| 1305 | return -1; |
| 1306 | } |
| 1307 | |
| 1308 | if (wnm_sleep) { |
| 1309 | sm->bigtk_wnm_sleep.bigtk_len = len; |
| 1310 | os_memcpy(sm->bigtk_wnm_sleep.bigtk, bigtk->bigtk, |
| 1311 | sm->bigtk_wnm_sleep.bigtk_len); |
| 1312 | } else { |
| 1313 | sm->bigtk.bigtk_len = len; |
| 1314 | os_memcpy(sm->bigtk.bigtk, bigtk->bigtk, sm->bigtk.bigtk_len); |
| 1315 | } |
| 1316 | |
| 1317 | return 0; |
| 1318 | } |
| 1319 | |
| 1320 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1321 | static int ieee80211w_set_keys(struct wpa_sm *sm, |
| 1322 | struct wpa_eapol_ie_parse *ie) |
| 1323 | { |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 1324 | size_t len; |
| 1325 | |
Hai Shalom | 6084025 | 2021-02-19 19:02:11 -0800 | [diff] [blame] | 1326 | if (!wpa_cipher_valid_mgmt_group(sm->mgmt_group_cipher) || |
| 1327 | sm->mgmt_group_cipher == WPA_CIPHER_GTK_NOT_USED) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1328 | return 0; |
| 1329 | |
| 1330 | if (ie->igtk) { |
| 1331 | const struct wpa_igtk_kde *igtk; |
Mathy Vanhoef | 10bfd64 | 2017-07-12 16:03:24 +0200 | [diff] [blame] | 1332 | |
Dmitry Shmidt | b36ed7c | 2014-03-17 10:57:26 -0700 | [diff] [blame] | 1333 | len = wpa_cipher_key_len(sm->mgmt_group_cipher); |
| 1334 | if (ie->igtk_len != WPA_IGTK_KDE_PREFIX_LEN + len) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1335 | return -1; |
Mathy Vanhoef | 10bfd64 | 2017-07-12 16:03:24 +0200 | [diff] [blame] | 1336 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1337 | igtk = (const struct wpa_igtk_kde *) ie->igtk; |
Jouni Malinen | 58c0e96 | 2017-10-01 12:12:24 +0300 | [diff] [blame] | 1338 | if (wpa_supplicant_install_igtk(sm, igtk, 0) < 0) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1339 | return -1; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1340 | } |
| 1341 | |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 1342 | if (ie->bigtk && sm->beacon_prot) { |
| 1343 | const struct wpa_bigtk_kde *bigtk; |
| 1344 | |
| 1345 | len = wpa_cipher_key_len(sm->mgmt_group_cipher); |
| 1346 | if (ie->bigtk_len != WPA_BIGTK_KDE_PREFIX_LEN + len) |
| 1347 | return -1; |
| 1348 | |
| 1349 | bigtk = (const struct wpa_bigtk_kde *) ie->bigtk; |
| 1350 | if (wpa_supplicant_install_bigtk(sm, bigtk, 0) < 0) |
| 1351 | return -1; |
| 1352 | } |
| 1353 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1354 | return 0; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1355 | } |
| 1356 | |
| 1357 | |
| 1358 | static void wpa_report_ie_mismatch(struct wpa_sm *sm, |
| 1359 | const char *reason, const u8 *src_addr, |
| 1360 | const u8 *wpa_ie, size_t wpa_ie_len, |
| 1361 | const u8 *rsn_ie, size_t rsn_ie_len) |
| 1362 | { |
| 1363 | wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, "WPA: %s (src=" MACSTR ")", |
| 1364 | reason, MAC2STR(src_addr)); |
| 1365 | |
| 1366 | if (sm->ap_wpa_ie) { |
| 1367 | wpa_hexdump(MSG_INFO, "WPA: WPA IE in Beacon/ProbeResp", |
| 1368 | sm->ap_wpa_ie, sm->ap_wpa_ie_len); |
| 1369 | } |
| 1370 | if (wpa_ie) { |
| 1371 | if (!sm->ap_wpa_ie) { |
| 1372 | wpa_msg(sm->ctx->msg_ctx, MSG_INFO, |
| 1373 | "WPA: No WPA IE in Beacon/ProbeResp"); |
| 1374 | } |
| 1375 | wpa_hexdump(MSG_INFO, "WPA: WPA IE in 3/4 msg", |
| 1376 | wpa_ie, wpa_ie_len); |
| 1377 | } |
| 1378 | |
| 1379 | if (sm->ap_rsn_ie) { |
| 1380 | wpa_hexdump(MSG_INFO, "WPA: RSN IE in Beacon/ProbeResp", |
| 1381 | sm->ap_rsn_ie, sm->ap_rsn_ie_len); |
| 1382 | } |
| 1383 | if (rsn_ie) { |
| 1384 | if (!sm->ap_rsn_ie) { |
| 1385 | wpa_msg(sm->ctx->msg_ctx, MSG_INFO, |
| 1386 | "WPA: No RSN IE in Beacon/ProbeResp"); |
| 1387 | } |
| 1388 | wpa_hexdump(MSG_INFO, "WPA: RSN IE in 3/4 msg", |
| 1389 | rsn_ie, rsn_ie_len); |
| 1390 | } |
| 1391 | |
Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 1392 | wpa_sm_deauthenticate(sm, WLAN_REASON_IE_IN_4WAY_DIFFERS); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1393 | } |
| 1394 | |
| 1395 | |
| 1396 | #ifdef CONFIG_IEEE80211R |
| 1397 | |
| 1398 | static int ft_validate_mdie(struct wpa_sm *sm, |
| 1399 | const unsigned char *src_addr, |
| 1400 | struct wpa_eapol_ie_parse *ie, |
| 1401 | const u8 *assoc_resp_mdie) |
| 1402 | { |
| 1403 | struct rsn_mdie *mdie; |
| 1404 | |
| 1405 | mdie = (struct rsn_mdie *) (ie->mdie + 2); |
| 1406 | if (ie->mdie == NULL || ie->mdie_len < 2 + sizeof(*mdie) || |
| 1407 | os_memcmp(mdie->mobility_domain, sm->mobility_domain, |
| 1408 | MOBILITY_DOMAIN_ID_LEN) != 0) { |
| 1409 | wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "FT: MDIE in msg 3/4 did " |
| 1410 | "not match with the current mobility domain"); |
| 1411 | return -1; |
| 1412 | } |
| 1413 | |
| 1414 | if (assoc_resp_mdie && |
| 1415 | (assoc_resp_mdie[1] != ie->mdie[1] || |
| 1416 | os_memcmp(assoc_resp_mdie, ie->mdie, 2 + ie->mdie[1]) != 0)) { |
| 1417 | wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "FT: MDIE mismatch"); |
| 1418 | wpa_hexdump(MSG_DEBUG, "FT: MDIE in EAPOL-Key msg 3/4", |
| 1419 | ie->mdie, 2 + ie->mdie[1]); |
| 1420 | wpa_hexdump(MSG_DEBUG, "FT: MDIE in (Re)Association Response", |
| 1421 | assoc_resp_mdie, 2 + assoc_resp_mdie[1]); |
| 1422 | return -1; |
| 1423 | } |
| 1424 | |
| 1425 | return 0; |
| 1426 | } |
| 1427 | |
| 1428 | |
| 1429 | static int ft_validate_ftie(struct wpa_sm *sm, |
| 1430 | const unsigned char *src_addr, |
| 1431 | struct wpa_eapol_ie_parse *ie, |
| 1432 | const u8 *assoc_resp_ftie) |
| 1433 | { |
| 1434 | if (ie->ftie == NULL) { |
| 1435 | wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, |
| 1436 | "FT: No FTIE in EAPOL-Key msg 3/4"); |
| 1437 | return -1; |
| 1438 | } |
| 1439 | |
| 1440 | if (assoc_resp_ftie == NULL) |
| 1441 | return 0; |
| 1442 | |
| 1443 | if (assoc_resp_ftie[1] != ie->ftie[1] || |
| 1444 | os_memcmp(assoc_resp_ftie, ie->ftie, 2 + ie->ftie[1]) != 0) { |
| 1445 | wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "FT: FTIE mismatch"); |
| 1446 | wpa_hexdump(MSG_DEBUG, "FT: FTIE in EAPOL-Key msg 3/4", |
| 1447 | ie->ftie, 2 + ie->ftie[1]); |
| 1448 | wpa_hexdump(MSG_DEBUG, "FT: FTIE in (Re)Association Response", |
| 1449 | assoc_resp_ftie, 2 + assoc_resp_ftie[1]); |
| 1450 | return -1; |
| 1451 | } |
| 1452 | |
| 1453 | return 0; |
| 1454 | } |
| 1455 | |
| 1456 | |
| 1457 | static int ft_validate_rsnie(struct wpa_sm *sm, |
| 1458 | const unsigned char *src_addr, |
| 1459 | struct wpa_eapol_ie_parse *ie) |
| 1460 | { |
| 1461 | struct wpa_ie_data rsn; |
| 1462 | |
| 1463 | if (!ie->rsn_ie) |
| 1464 | return 0; |
| 1465 | |
| 1466 | /* |
| 1467 | * Verify that PMKR1Name from EAPOL-Key message 3/4 |
| 1468 | * matches with the value we derived. |
| 1469 | */ |
| 1470 | if (wpa_parse_wpa_ie_rsn(ie->rsn_ie, ie->rsn_ie_len, &rsn) < 0 || |
| 1471 | rsn.num_pmkid != 1 || rsn.pmkid == NULL) { |
| 1472 | wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "FT: No PMKR1Name in " |
| 1473 | "FT 4-way handshake message 3/4"); |
| 1474 | return -1; |
| 1475 | } |
| 1476 | |
Dmitry Shmidt | c281702 | 2014-07-02 10:32:10 -0700 | [diff] [blame] | 1477 | if (os_memcmp_const(rsn.pmkid, sm->pmk_r1_name, WPA_PMK_NAME_LEN) != 0) |
| 1478 | { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1479 | wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, |
| 1480 | "FT: PMKR1Name mismatch in " |
| 1481 | "FT 4-way handshake message 3/4"); |
| 1482 | wpa_hexdump(MSG_DEBUG, "FT: PMKR1Name from Authenticator", |
| 1483 | rsn.pmkid, WPA_PMK_NAME_LEN); |
| 1484 | wpa_hexdump(MSG_DEBUG, "FT: Derived PMKR1Name", |
| 1485 | sm->pmk_r1_name, WPA_PMK_NAME_LEN); |
| 1486 | return -1; |
| 1487 | } |
| 1488 | |
| 1489 | return 0; |
| 1490 | } |
| 1491 | |
| 1492 | |
| 1493 | static int wpa_supplicant_validate_ie_ft(struct wpa_sm *sm, |
| 1494 | const unsigned char *src_addr, |
| 1495 | struct wpa_eapol_ie_parse *ie) |
| 1496 | { |
| 1497 | const u8 *pos, *end, *mdie = NULL, *ftie = NULL; |
| 1498 | |
| 1499 | if (sm->assoc_resp_ies) { |
| 1500 | pos = sm->assoc_resp_ies; |
| 1501 | end = pos + sm->assoc_resp_ies_len; |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 1502 | while (end - pos > 2) { |
| 1503 | if (2 + pos[1] > end - pos) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1504 | break; |
| 1505 | switch (*pos) { |
| 1506 | case WLAN_EID_MOBILITY_DOMAIN: |
| 1507 | mdie = pos; |
| 1508 | break; |
| 1509 | case WLAN_EID_FAST_BSS_TRANSITION: |
| 1510 | ftie = pos; |
| 1511 | break; |
| 1512 | } |
| 1513 | pos += 2 + pos[1]; |
| 1514 | } |
| 1515 | } |
| 1516 | |
| 1517 | if (ft_validate_mdie(sm, src_addr, ie, mdie) < 0 || |
| 1518 | ft_validate_ftie(sm, src_addr, ie, ftie) < 0 || |
| 1519 | ft_validate_rsnie(sm, src_addr, ie) < 0) |
| 1520 | return -1; |
| 1521 | |
| 1522 | return 0; |
| 1523 | } |
| 1524 | |
| 1525 | #endif /* CONFIG_IEEE80211R */ |
| 1526 | |
| 1527 | |
| 1528 | static int wpa_supplicant_validate_ie(struct wpa_sm *sm, |
| 1529 | const unsigned char *src_addr, |
| 1530 | struct wpa_eapol_ie_parse *ie) |
| 1531 | { |
| 1532 | if (sm->ap_wpa_ie == NULL && sm->ap_rsn_ie == NULL) { |
| 1533 | wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, |
| 1534 | "WPA: No WPA/RSN IE for this AP known. " |
| 1535 | "Trying to get from scan results"); |
| 1536 | if (wpa_sm_get_beacon_ie(sm) < 0) { |
| 1537 | wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, |
| 1538 | "WPA: Could not find AP from " |
| 1539 | "the scan results"); |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 1540 | return -1; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1541 | } |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 1542 | wpa_msg(sm->ctx->msg_ctx, MSG_DEBUG, |
| 1543 | "WPA: Found the current AP from updated scan results"); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1544 | } |
| 1545 | |
| 1546 | if (ie->wpa_ie == NULL && ie->rsn_ie == NULL && |
| 1547 | (sm->ap_wpa_ie || sm->ap_rsn_ie)) { |
| 1548 | wpa_report_ie_mismatch(sm, "IE in 3/4 msg does not match " |
| 1549 | "with IE in Beacon/ProbeResp (no IE?)", |
| 1550 | src_addr, ie->wpa_ie, ie->wpa_ie_len, |
| 1551 | ie->rsn_ie, ie->rsn_ie_len); |
| 1552 | return -1; |
| 1553 | } |
| 1554 | |
| 1555 | if ((ie->wpa_ie && sm->ap_wpa_ie && |
| 1556 | (ie->wpa_ie_len != sm->ap_wpa_ie_len || |
| 1557 | os_memcmp(ie->wpa_ie, sm->ap_wpa_ie, ie->wpa_ie_len) != 0)) || |
| 1558 | (ie->rsn_ie && sm->ap_rsn_ie && |
| 1559 | wpa_compare_rsn_ie(wpa_key_mgmt_ft(sm->key_mgmt), |
| 1560 | sm->ap_rsn_ie, sm->ap_rsn_ie_len, |
| 1561 | ie->rsn_ie, ie->rsn_ie_len))) { |
| 1562 | wpa_report_ie_mismatch(sm, "IE in 3/4 msg does not match " |
| 1563 | "with IE in Beacon/ProbeResp", |
| 1564 | src_addr, ie->wpa_ie, ie->wpa_ie_len, |
| 1565 | ie->rsn_ie, ie->rsn_ie_len); |
| 1566 | return -1; |
| 1567 | } |
| 1568 | |
| 1569 | if (sm->proto == WPA_PROTO_WPA && |
| 1570 | ie->rsn_ie && sm->ap_rsn_ie == NULL && sm->rsn_enabled) { |
| 1571 | wpa_report_ie_mismatch(sm, "Possible downgrade attack " |
| 1572 | "detected - RSN was enabled and RSN IE " |
| 1573 | "was in msg 3/4, but not in " |
| 1574 | "Beacon/ProbeResp", |
| 1575 | src_addr, ie->wpa_ie, ie->wpa_ie_len, |
| 1576 | ie->rsn_ie, ie->rsn_ie_len); |
| 1577 | return -1; |
| 1578 | } |
| 1579 | |
Hai Shalom | 6084025 | 2021-02-19 19:02:11 -0800 | [diff] [blame] | 1580 | if (sm->proto == WPA_PROTO_RSN && |
| 1581 | ((sm->ap_rsnxe && !ie->rsnxe) || |
| 1582 | (!sm->ap_rsnxe && ie->rsnxe) || |
| 1583 | (sm->ap_rsnxe && ie->rsnxe && |
| 1584 | (sm->ap_rsnxe_len != ie->rsnxe_len || |
| 1585 | os_memcmp(sm->ap_rsnxe, ie->rsnxe, sm->ap_rsnxe_len) != 0)))) { |
Hai Shalom | c356592 | 2019-10-28 11:58:20 -0700 | [diff] [blame] | 1586 | wpa_msg(sm->ctx->msg_ctx, MSG_INFO, |
| 1587 | "WPA: RSNXE mismatch between Beacon/ProbeResp and EAPOL-Key msg 3/4"); |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 1588 | wpa_hexdump(MSG_INFO, "RSNXE in Beacon/ProbeResp", |
| 1589 | sm->ap_rsnxe, sm->ap_rsnxe_len); |
| 1590 | wpa_hexdump(MSG_INFO, "RSNXE in EAPOL-Key msg 3/4", |
| 1591 | ie->rsnxe, ie->rsnxe_len); |
| 1592 | wpa_sm_deauthenticate(sm, WLAN_REASON_IE_IN_4WAY_DIFFERS); |
Hai Shalom | c356592 | 2019-10-28 11:58:20 -0700 | [diff] [blame] | 1593 | return -1; |
| 1594 | } |
| 1595 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1596 | #ifdef CONFIG_IEEE80211R |
| 1597 | if (wpa_key_mgmt_ft(sm->key_mgmt) && |
| 1598 | wpa_supplicant_validate_ie_ft(sm, src_addr, ie) < 0) |
| 1599 | return -1; |
| 1600 | #endif /* CONFIG_IEEE80211R */ |
| 1601 | |
| 1602 | return 0; |
| 1603 | } |
| 1604 | |
| 1605 | |
| 1606 | /** |
| 1607 | * wpa_supplicant_send_4_of_4 - Send message 4 of WPA/RSN 4-Way Handshake |
| 1608 | * @sm: Pointer to WPA state machine data from wpa_sm_init() |
| 1609 | * @dst: Destination address for the frame |
| 1610 | * @key: Pointer to the EAPOL-Key frame header |
| 1611 | * @ver: Version bits from EAPOL-Key Key Info |
| 1612 | * @key_info: Key Info |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1613 | * @ptk: PTK to use for keyed hash and encryption |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 1614 | * Returns: >= 0 on success, < 0 on failure |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1615 | */ |
| 1616 | int wpa_supplicant_send_4_of_4(struct wpa_sm *sm, const unsigned char *dst, |
| 1617 | const struct wpa_eapol_key *key, |
| 1618 | u16 ver, u16 key_info, |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1619 | struct wpa_ptk *ptk) |
| 1620 | { |
Dmitry Shmidt | 807291d | 2015-01-27 13:40:23 -0800 | [diff] [blame] | 1621 | size_t mic_len, hdrlen, rlen; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1622 | struct wpa_eapol_key *reply; |
Dmitry Shmidt | 807291d | 2015-01-27 13:40:23 -0800 | [diff] [blame] | 1623 | u8 *rbuf, *key_mic; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1624 | |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 1625 | mic_len = wpa_mic_len(sm->key_mgmt, sm->pmk_len); |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 1626 | hdrlen = sizeof(*reply) + mic_len + 2; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1627 | rbuf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_KEY, NULL, |
Dmitry Shmidt | 807291d | 2015-01-27 13:40:23 -0800 | [diff] [blame] | 1628 | hdrlen, &rlen, (void *) &reply); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1629 | if (rbuf == NULL) |
| 1630 | return -1; |
| 1631 | |
Dmitry Shmidt | f21452a | 2014-02-26 10:55:25 -0800 | [diff] [blame] | 1632 | reply->type = (sm->proto == WPA_PROTO_RSN || |
| 1633 | sm->proto == WPA_PROTO_OSEN) ? |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1634 | EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA; |
| 1635 | key_info &= WPA_KEY_INFO_SECURE; |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 1636 | key_info |= ver | WPA_KEY_INFO_KEY_TYPE; |
| 1637 | if (mic_len) |
| 1638 | key_info |= WPA_KEY_INFO_MIC; |
| 1639 | else |
| 1640 | key_info |= WPA_KEY_INFO_ENCR_KEY_DATA; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1641 | WPA_PUT_BE16(reply->key_info, key_info); |
Dmitry Shmidt | f21452a | 2014-02-26 10:55:25 -0800 | [diff] [blame] | 1642 | if (sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1643 | WPA_PUT_BE16(reply->key_length, 0); |
| 1644 | else |
| 1645 | os_memcpy(reply->key_length, key->key_length, 2); |
| 1646 | os_memcpy(reply->replay_counter, key->replay_counter, |
| 1647 | WPA_REPLAY_COUNTER_LEN); |
| 1648 | |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 1649 | key_mic = (u8 *) (reply + 1); |
| 1650 | WPA_PUT_BE16(key_mic + mic_len, 0); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1651 | |
Roshan Pius | 5e7db94 | 2018-04-12 12:27:41 -0700 | [diff] [blame] | 1652 | wpa_dbg(sm->ctx->msg_ctx, MSG_INFO, "WPA: Sending EAPOL-Key 4/4"); |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 1653 | return wpa_eapol_key_send(sm, ptk, ver, dst, ETH_P_EAPOL, rbuf, rlen, |
| 1654 | key_mic); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1655 | } |
| 1656 | |
| 1657 | |
| 1658 | static void wpa_supplicant_process_3_of_4(struct wpa_sm *sm, |
| 1659 | const struct wpa_eapol_key *key, |
Dmitry Shmidt | 43cb578 | 2014-06-16 16:23:22 -0700 | [diff] [blame] | 1660 | u16 ver, const u8 *key_data, |
| 1661 | size_t key_data_len) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1662 | { |
Dmitry Shmidt | 43cb578 | 2014-06-16 16:23:22 -0700 | [diff] [blame] | 1663 | u16 key_info, keylen; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1664 | struct wpa_eapol_ie_parse ie; |
| 1665 | |
| 1666 | wpa_sm_set_state(sm, WPA_4WAY_HANDSHAKE); |
Roshan Pius | 5e7db94 | 2018-04-12 12:27:41 -0700 | [diff] [blame] | 1667 | wpa_dbg(sm->ctx->msg_ctx, MSG_INFO, "WPA: RX message 3 of 4-Way " |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1668 | "Handshake from " MACSTR " (ver=%d)", MAC2STR(sm->bssid), ver); |
| 1669 | |
| 1670 | key_info = WPA_GET_BE16(key->key_info); |
| 1671 | |
Dmitry Shmidt | 43cb578 | 2014-06-16 16:23:22 -0700 | [diff] [blame] | 1672 | wpa_hexdump(MSG_DEBUG, "WPA: IE KeyData", key_data, key_data_len); |
| 1673 | if (wpa_supplicant_parse_ies(key_data, key_data_len, &ie) < 0) |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 1674 | goto failed; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1675 | if (ie.gtk && !(key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) { |
| 1676 | wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, |
| 1677 | "WPA: GTK IE in unencrypted key data"); |
| 1678 | goto failed; |
| 1679 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1680 | if (ie.igtk && !(key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) { |
| 1681 | wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, |
| 1682 | "WPA: IGTK KDE in unencrypted key data"); |
| 1683 | goto failed; |
| 1684 | } |
| 1685 | |
Dmitry Shmidt | b36ed7c | 2014-03-17 10:57:26 -0700 | [diff] [blame] | 1686 | if (ie.igtk && |
Hai Shalom | 6084025 | 2021-02-19 19:02:11 -0800 | [diff] [blame] | 1687 | sm->mgmt_group_cipher != WPA_CIPHER_GTK_NOT_USED && |
Dmitry Shmidt | b36ed7c | 2014-03-17 10:57:26 -0700 | [diff] [blame] | 1688 | wpa_cipher_valid_mgmt_group(sm->mgmt_group_cipher) && |
| 1689 | ie.igtk_len != WPA_IGTK_KDE_PREFIX_LEN + |
| 1690 | (unsigned int) wpa_cipher_key_len(sm->mgmt_group_cipher)) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1691 | wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, |
| 1692 | "WPA: Invalid IGTK KDE length %lu", |
| 1693 | (unsigned long) ie.igtk_len); |
| 1694 | goto failed; |
| 1695 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1696 | |
| 1697 | if (wpa_supplicant_validate_ie(sm, sm->bssid, &ie) < 0) |
| 1698 | goto failed; |
| 1699 | |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 1700 | if (wpa_handle_ext_key_id(sm, &ie)) |
| 1701 | goto failed; |
| 1702 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1703 | if (os_memcmp(sm->anonce, key->key_nonce, WPA_NONCE_LEN) != 0) { |
| 1704 | wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, |
| 1705 | "WPA: ANonce from message 1 of 4-Way Handshake " |
| 1706 | "differs from 3 of 4-Way Handshake - drop packet (src=" |
| 1707 | MACSTR ")", MAC2STR(sm->bssid)); |
| 1708 | goto failed; |
| 1709 | } |
| 1710 | |
| 1711 | keylen = WPA_GET_BE16(key->key_length); |
Dmitry Shmidt | 4530cfd | 2012-09-09 15:20:40 -0700 | [diff] [blame] | 1712 | if (keylen != wpa_cipher_key_len(sm->pairwise_cipher)) { |
| 1713 | wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, |
| 1714 | "WPA: Invalid %s key length %d (src=" MACSTR |
| 1715 | ")", wpa_cipher_txt(sm->pairwise_cipher), keylen, |
| 1716 | MAC2STR(sm->bssid)); |
| 1717 | goto failed; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1718 | } |
| 1719 | |
Dmitry Shmidt | cf32e60 | 2014-01-28 10:57:39 -0800 | [diff] [blame] | 1720 | #ifdef CONFIG_P2P |
| 1721 | if (ie.ip_addr_alloc) { |
| 1722 | os_memcpy(sm->p2p_ip_addr, ie.ip_addr_alloc, 3 * 4); |
| 1723 | wpa_hexdump(MSG_DEBUG, "P2P: IP address info", |
| 1724 | sm->p2p_ip_addr, sizeof(sm->p2p_ip_addr)); |
| 1725 | } |
| 1726 | #endif /* CONFIG_P2P */ |
| 1727 | |
Hai Shalom | 74f70d4 | 2019-02-11 14:42:39 -0800 | [diff] [blame] | 1728 | #ifdef CONFIG_OCV |
| 1729 | if (wpa_sm_ocv_enabled(sm)) { |
| 1730 | struct wpa_channel_info ci; |
| 1731 | |
| 1732 | if (wpa_sm_channel_info(sm, &ci) != 0) { |
| 1733 | wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, |
| 1734 | "Failed to get channel info to validate received OCI in EAPOL-Key 3/4"); |
| 1735 | return; |
| 1736 | } |
| 1737 | |
| 1738 | if (ocv_verify_tx_params(ie.oci, ie.oci_len, &ci, |
| 1739 | channel_width_to_int(ci.chanwidth), |
Hai Shalom | 899fcc7 | 2020-10-19 14:38:18 -0700 | [diff] [blame] | 1740 | ci.seg1_idx) != OCI_SUCCESS) { |
| 1741 | wpa_msg(sm->ctx->msg_ctx, MSG_INFO, OCV_FAILURE |
| 1742 | "addr=" MACSTR " frame=eapol-key-m3 error=%s", |
| 1743 | MAC2STR(sm->bssid), ocv_errorstr); |
Hai Shalom | 74f70d4 | 2019-02-11 14:42:39 -0800 | [diff] [blame] | 1744 | return; |
| 1745 | } |
| 1746 | } |
| 1747 | #endif /* CONFIG_OCV */ |
| 1748 | |
Hai Shalom | 4fbc08f | 2020-05-18 12:37:00 -0700 | [diff] [blame] | 1749 | #ifdef CONFIG_DPP2 |
| 1750 | if (DPP_VERSION > 1 && ie.dpp_kde) { |
| 1751 | wpa_printf(MSG_DEBUG, |
| 1752 | "DPP: peer Protocol Version %u Flags 0x%x", |
| 1753 | ie.dpp_kde[0], ie.dpp_kde[1]); |
| 1754 | if (sm->key_mgmt == WPA_KEY_MGMT_DPP && sm->dpp_pfs != 2 && |
| 1755 | (ie.dpp_kde[1] & DPP_KDE_PFS_ALLOWED) && !sm->dpp_z) { |
| 1756 | wpa_printf(MSG_INFO, |
| 1757 | "DPP: Peer indicated it supports PFS and local configuration allows this, but PFS was not negotiated for the association"); |
| 1758 | goto failed; |
| 1759 | } |
| 1760 | } |
| 1761 | #endif /* CONFIG_DPP2 */ |
| 1762 | |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 1763 | if (sm->use_ext_key_id && |
| 1764 | wpa_supplicant_install_ptk(sm, key, KEY_FLAG_RX)) |
| 1765 | goto failed; |
| 1766 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1767 | if (wpa_supplicant_send_4_of_4(sm, sm->bssid, key, ver, key_info, |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 1768 | &sm->ptk) < 0) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1769 | goto failed; |
| 1770 | } |
| 1771 | |
| 1772 | /* SNonce was successfully used in msg 3/4, so mark it to be renewed |
| 1773 | * for the next 4-Way Handshake. If msg 3 is received again, the old |
| 1774 | * SNonce will still be used to avoid changing PTK. */ |
| 1775 | sm->renew_snonce = 1; |
| 1776 | |
| 1777 | if (key_info & WPA_KEY_INFO_INSTALL) { |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 1778 | int res; |
| 1779 | |
| 1780 | if (sm->use_ext_key_id) |
| 1781 | res = wpa_supplicant_activate_ptk(sm); |
| 1782 | else |
| 1783 | res = wpa_supplicant_install_ptk(sm, key, |
| 1784 | KEY_FLAG_RX_TX); |
| 1785 | if (res) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1786 | goto failed; |
| 1787 | } |
| 1788 | |
| 1789 | if (key_info & WPA_KEY_INFO_SECURE) { |
| 1790 | wpa_sm_mlme_setprotection( |
| 1791 | sm, sm->bssid, MLME_SETPROTECTION_PROTECT_TYPE_RX, |
| 1792 | MLME_SETPROTECTION_KEY_TYPE_PAIRWISE); |
Hai Shalom | e21d4e8 | 2020-04-29 16:34:06 -0700 | [diff] [blame] | 1793 | eapol_sm_notify_portValid(sm->eapol, true); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1794 | } |
| 1795 | wpa_sm_set_state(sm, WPA_GROUP_HANDSHAKE); |
| 1796 | |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 1797 | if (sm->group_cipher == WPA_CIPHER_GTK_NOT_USED) { |
Hai Shalom | 5f92bc9 | 2019-04-18 11:54:11 -0700 | [diff] [blame] | 1798 | /* No GTK to be set to the driver */ |
| 1799 | } else if (!ie.gtk && sm->proto == WPA_PROTO_RSN) { |
| 1800 | wpa_msg(sm->ctx->msg_ctx, MSG_INFO, |
| 1801 | "RSN: No GTK KDE included in EAPOL-Key msg 3/4"); |
| 1802 | goto failed; |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 1803 | } else if (ie.gtk && |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1804 | wpa_supplicant_pairwise_gtk(sm, key, |
| 1805 | ie.gtk, ie.gtk_len, key_info) < 0) { |
| 1806 | wpa_msg(sm->ctx->msg_ctx, MSG_INFO, |
| 1807 | "RSN: Failed to configure GTK"); |
| 1808 | goto failed; |
| 1809 | } |
| 1810 | |
| 1811 | if (ieee80211w_set_keys(sm, &ie) < 0) { |
| 1812 | wpa_msg(sm->ctx->msg_ctx, MSG_INFO, |
| 1813 | "RSN: Failed to configure IGTK"); |
| 1814 | goto failed; |
| 1815 | } |
| 1816 | |
Hai Shalom | 5f92bc9 | 2019-04-18 11:54:11 -0700 | [diff] [blame] | 1817 | if (sm->group_cipher == WPA_CIPHER_GTK_NOT_USED || ie.gtk) |
| 1818 | wpa_supplicant_key_neg_complete(sm, sm->bssid, |
| 1819 | key_info & WPA_KEY_INFO_SECURE); |
| 1820 | |
Dmitry Shmidt | cce0666 | 2013-11-04 18:44:24 -0800 | [diff] [blame] | 1821 | if (ie.gtk) |
| 1822 | wpa_sm_set_rekey_offload(sm); |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 1823 | |
Hai Shalom | 39ba6fc | 2019-01-22 12:40:38 -0800 | [diff] [blame] | 1824 | /* Add PMKSA cache entry for Suite B AKMs here since PMKID can be |
| 1825 | * calculated only after KCK has been derived. Though, do not replace an |
| 1826 | * existing PMKSA entry after each 4-way handshake (i.e., new KCK/PMKID) |
| 1827 | * to avoid unnecessary changes of PMKID while continuing to use the |
| 1828 | * same PMK. */ |
| 1829 | if (sm->proto == WPA_PROTO_RSN && wpa_key_mgmt_suite_b(sm->key_mgmt) && |
| 1830 | !sm->cur_pmksa) { |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 1831 | struct rsn_pmksa_cache_entry *sa; |
| 1832 | |
Dmitry Shmidt | 57c2d39 | 2016-02-23 13:40:19 -0800 | [diff] [blame] | 1833 | sa = pmksa_cache_add(sm->pmksa, sm->pmk, sm->pmk_len, NULL, |
Dmitry Shmidt | 807291d | 2015-01-27 13:40:23 -0800 | [diff] [blame] | 1834 | sm->ptk.kck, sm->ptk.kck_len, |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 1835 | sm->bssid, sm->own_addr, |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 1836 | sm->network_ctx, sm->key_mgmt, NULL); |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 1837 | if (!sm->cur_pmksa) |
| 1838 | sm->cur_pmksa = sa; |
| 1839 | } |
| 1840 | |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 1841 | if (ie.transition_disable) |
| 1842 | wpa_sm_transition_disable(sm, ie.transition_disable[0]); |
Dmitry Shmidt | 4dd28dc | 2015-03-10 11:21:43 -0700 | [diff] [blame] | 1843 | sm->msg_3_of_4_ok = 1; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1844 | return; |
| 1845 | |
| 1846 | failed: |
| 1847 | wpa_sm_deauthenticate(sm, WLAN_REASON_UNSPECIFIED); |
| 1848 | } |
| 1849 | |
| 1850 | |
| 1851 | static int wpa_supplicant_process_1_of_2_rsn(struct wpa_sm *sm, |
| 1852 | const u8 *keydata, |
| 1853 | size_t keydatalen, |
| 1854 | u16 key_info, |
| 1855 | struct wpa_gtk_data *gd) |
| 1856 | { |
| 1857 | int maxkeylen; |
| 1858 | struct wpa_eapol_ie_parse ie; |
Hai Shalom | 6084025 | 2021-02-19 19:02:11 -0800 | [diff] [blame] | 1859 | u16 gtk_len; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1860 | |
Dmitry Shmidt | ebd93af | 2017-02-21 13:40:44 -0800 | [diff] [blame] | 1861 | wpa_hexdump_key(MSG_DEBUG, "RSN: msg 1/2 key data", |
| 1862 | keydata, keydatalen); |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 1863 | if (wpa_supplicant_parse_ies(keydata, keydatalen, &ie) < 0) |
| 1864 | return -1; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1865 | if (ie.gtk && !(key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) { |
| 1866 | wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, |
| 1867 | "WPA: GTK IE in unencrypted key data"); |
| 1868 | return -1; |
| 1869 | } |
| 1870 | if (ie.gtk == NULL) { |
| 1871 | wpa_msg(sm->ctx->msg_ctx, MSG_INFO, |
| 1872 | "WPA: No GTK IE in Group Key msg 1/2"); |
| 1873 | return -1; |
| 1874 | } |
Hai Shalom | 6084025 | 2021-02-19 19:02:11 -0800 | [diff] [blame] | 1875 | gtk_len = ie.gtk_len; |
| 1876 | if (gtk_len < 2) { |
| 1877 | wpa_msg(sm->ctx->msg_ctx, MSG_INFO, |
| 1878 | "RSN: Invalid GTK KDE length (%u) in Group Key msg 1/2", |
| 1879 | gtk_len); |
| 1880 | return -1; |
| 1881 | } |
| 1882 | gtk_len -= 2; |
| 1883 | if (gtk_len > sizeof(gd->gtk)) { |
| 1884 | wpa_msg(sm->ctx->msg_ctx, MSG_INFO, |
| 1885 | "RSN: Too long GTK in GTK KDE (len=%u)", gtk_len); |
| 1886 | return -1; |
| 1887 | } |
| 1888 | maxkeylen = gd->gtk_len = gtk_len; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1889 | |
Hai Shalom | 74f70d4 | 2019-02-11 14:42:39 -0800 | [diff] [blame] | 1890 | #ifdef CONFIG_OCV |
| 1891 | if (wpa_sm_ocv_enabled(sm)) { |
| 1892 | struct wpa_channel_info ci; |
| 1893 | |
| 1894 | if (wpa_sm_channel_info(sm, &ci) != 0) { |
| 1895 | wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, |
| 1896 | "Failed to get channel info to validate received OCI in EAPOL-Key group msg 1/2"); |
| 1897 | return -1; |
| 1898 | } |
| 1899 | |
| 1900 | if (ocv_verify_tx_params(ie.oci, ie.oci_len, &ci, |
| 1901 | channel_width_to_int(ci.chanwidth), |
Hai Shalom | 899fcc7 | 2020-10-19 14:38:18 -0700 | [diff] [blame] | 1902 | ci.seg1_idx) != OCI_SUCCESS) { |
| 1903 | wpa_msg(sm->ctx->msg_ctx, MSG_INFO, OCV_FAILURE |
| 1904 | "addr=" MACSTR " frame=eapol-key-g1 error=%s", |
| 1905 | MAC2STR(sm->bssid), ocv_errorstr); |
Hai Shalom | 74f70d4 | 2019-02-11 14:42:39 -0800 | [diff] [blame] | 1906 | return -1; |
| 1907 | } |
| 1908 | } |
| 1909 | #endif /* CONFIG_OCV */ |
| 1910 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1911 | if (wpa_supplicant_check_group_cipher(sm, sm->group_cipher, |
Hai Shalom | 6084025 | 2021-02-19 19:02:11 -0800 | [diff] [blame] | 1912 | gtk_len, maxkeylen, |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1913 | &gd->key_rsc_len, &gd->alg)) |
| 1914 | return -1; |
| 1915 | |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 1916 | wpa_hexdump_key(MSG_DEBUG, "RSN: received GTK in group key handshake", |
Hai Shalom | 6084025 | 2021-02-19 19:02:11 -0800 | [diff] [blame] | 1917 | ie.gtk, 2 + gtk_len); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1918 | gd->keyidx = ie.gtk[0] & 0x3; |
| 1919 | gd->tx = wpa_supplicant_gtk_tx_bit_workaround(sm, |
| 1920 | !!(ie.gtk[0] & BIT(2))); |
Hai Shalom | 6084025 | 2021-02-19 19:02:11 -0800 | [diff] [blame] | 1921 | os_memcpy(gd->gtk, ie.gtk + 2, gtk_len); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1922 | |
| 1923 | if (ieee80211w_set_keys(sm, &ie) < 0) |
| 1924 | wpa_msg(sm->ctx->msg_ctx, MSG_INFO, |
| 1925 | "RSN: Failed to configure IGTK"); |
| 1926 | |
| 1927 | return 0; |
| 1928 | } |
| 1929 | |
| 1930 | |
| 1931 | static int wpa_supplicant_process_1_of_2_wpa(struct wpa_sm *sm, |
| 1932 | const struct wpa_eapol_key *key, |
Dmitry Shmidt | 43cb578 | 2014-06-16 16:23:22 -0700 | [diff] [blame] | 1933 | const u8 *key_data, |
| 1934 | size_t key_data_len, u16 key_info, |
| 1935 | u16 ver, struct wpa_gtk_data *gd) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1936 | { |
| 1937 | size_t maxkeylen; |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 1938 | u16 gtk_len; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1939 | |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 1940 | gtk_len = WPA_GET_BE16(key->key_length); |
Dmitry Shmidt | 43cb578 | 2014-06-16 16:23:22 -0700 | [diff] [blame] | 1941 | maxkeylen = key_data_len; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1942 | if (ver == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) { |
| 1943 | if (maxkeylen < 8) { |
| 1944 | wpa_msg(sm->ctx->msg_ctx, MSG_INFO, |
| 1945 | "WPA: Too short maxkeylen (%lu)", |
| 1946 | (unsigned long) maxkeylen); |
| 1947 | return -1; |
| 1948 | } |
| 1949 | maxkeylen -= 8; |
| 1950 | } |
| 1951 | |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 1952 | if (gtk_len > maxkeylen || |
| 1953 | wpa_supplicant_check_group_cipher(sm, sm->group_cipher, |
| 1954 | gtk_len, maxkeylen, |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1955 | &gd->key_rsc_len, &gd->alg)) |
| 1956 | return -1; |
| 1957 | |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 1958 | gd->gtk_len = gtk_len; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1959 | gd->keyidx = (key_info & WPA_KEY_INFO_KEY_INDEX_MASK) >> |
| 1960 | WPA_KEY_INFO_KEY_INDEX_SHIFT; |
Dmitry Shmidt | 807291d | 2015-01-27 13:40:23 -0800 | [diff] [blame] | 1961 | if (ver == WPA_KEY_INFO_TYPE_HMAC_MD5_RC4 && sm->ptk.kek_len == 16) { |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 1962 | #ifdef CONFIG_NO_RC4 |
| 1963 | wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, |
| 1964 | "WPA: RC4 not supported in the build"); |
| 1965 | return -1; |
| 1966 | #else /* CONFIG_NO_RC4 */ |
Dmitry Shmidt | 61593f0 | 2014-04-21 16:27:35 -0700 | [diff] [blame] | 1967 | u8 ek[32]; |
Dmitry Shmidt | 43cb578 | 2014-06-16 16:23:22 -0700 | [diff] [blame] | 1968 | if (key_data_len > sizeof(gd->gtk)) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1969 | wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, |
| 1970 | "WPA: RC4 key data too long (%lu)", |
Dmitry Shmidt | 43cb578 | 2014-06-16 16:23:22 -0700 | [diff] [blame] | 1971 | (unsigned long) key_data_len); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1972 | return -1; |
| 1973 | } |
Dmitry Shmidt | 61593f0 | 2014-04-21 16:27:35 -0700 | [diff] [blame] | 1974 | os_memcpy(ek, key->key_iv, 16); |
Dmitry Shmidt | 807291d | 2015-01-27 13:40:23 -0800 | [diff] [blame] | 1975 | os_memcpy(ek + 16, sm->ptk.kek, sm->ptk.kek_len); |
Dmitry Shmidt | 43cb578 | 2014-06-16 16:23:22 -0700 | [diff] [blame] | 1976 | os_memcpy(gd->gtk, key_data, key_data_len); |
| 1977 | if (rc4_skip(ek, 32, 256, gd->gtk, key_data_len)) { |
Hai Shalom | 81f62d8 | 2019-07-22 12:10:00 -0700 | [diff] [blame] | 1978 | forced_memzero(ek, sizeof(ek)); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1979 | wpa_msg(sm->ctx->msg_ctx, MSG_ERROR, |
| 1980 | "WPA: RC4 failed"); |
| 1981 | return -1; |
| 1982 | } |
Hai Shalom | 81f62d8 | 2019-07-22 12:10:00 -0700 | [diff] [blame] | 1983 | forced_memzero(ek, sizeof(ek)); |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 1984 | #endif /* CONFIG_NO_RC4 */ |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1985 | } else if (ver == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) { |
Dmitry Shmidt | 43cb578 | 2014-06-16 16:23:22 -0700 | [diff] [blame] | 1986 | if (maxkeylen % 8) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1987 | wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, |
| 1988 | "WPA: Unsupported AES-WRAP len %lu", |
Dmitry Shmidt | 43cb578 | 2014-06-16 16:23:22 -0700 | [diff] [blame] | 1989 | (unsigned long) maxkeylen); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1990 | return -1; |
| 1991 | } |
| 1992 | if (maxkeylen > sizeof(gd->gtk)) { |
| 1993 | wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, |
| 1994 | "WPA: AES-WRAP key data " |
| 1995 | "too long (keydatalen=%lu maxkeylen=%lu)", |
Dmitry Shmidt | 43cb578 | 2014-06-16 16:23:22 -0700 | [diff] [blame] | 1996 | (unsigned long) key_data_len, |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1997 | (unsigned long) maxkeylen); |
| 1998 | return -1; |
| 1999 | } |
Dmitry Shmidt | 807291d | 2015-01-27 13:40:23 -0800 | [diff] [blame] | 2000 | if (aes_unwrap(sm->ptk.kek, sm->ptk.kek_len, maxkeylen / 8, |
| 2001 | key_data, gd->gtk)) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2002 | wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, |
| 2003 | "WPA: AES unwrap failed - could not decrypt " |
| 2004 | "GTK"); |
| 2005 | return -1; |
| 2006 | } |
| 2007 | } else { |
| 2008 | wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, |
| 2009 | "WPA: Unsupported key_info type %d", ver); |
| 2010 | return -1; |
| 2011 | } |
| 2012 | gd->tx = wpa_supplicant_gtk_tx_bit_workaround( |
| 2013 | sm, !!(key_info & WPA_KEY_INFO_TXRX)); |
| 2014 | return 0; |
| 2015 | } |
| 2016 | |
| 2017 | |
| 2018 | static int wpa_supplicant_send_2_of_2(struct wpa_sm *sm, |
| 2019 | const struct wpa_eapol_key *key, |
| 2020 | int ver, u16 key_info) |
| 2021 | { |
Dmitry Shmidt | 807291d | 2015-01-27 13:40:23 -0800 | [diff] [blame] | 2022 | size_t mic_len, hdrlen, rlen; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2023 | struct wpa_eapol_key *reply; |
Dmitry Shmidt | 807291d | 2015-01-27 13:40:23 -0800 | [diff] [blame] | 2024 | u8 *rbuf, *key_mic; |
Hai Shalom | 74f70d4 | 2019-02-11 14:42:39 -0800 | [diff] [blame] | 2025 | size_t kde_len = 0; |
| 2026 | |
| 2027 | #ifdef CONFIG_OCV |
| 2028 | if (wpa_sm_ocv_enabled(sm)) |
| 2029 | kde_len = OCV_OCI_KDE_LEN; |
| 2030 | #endif /* CONFIG_OCV */ |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2031 | |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 2032 | mic_len = wpa_mic_len(sm->key_mgmt, sm->pmk_len); |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 2033 | hdrlen = sizeof(*reply) + mic_len + 2; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2034 | rbuf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_KEY, NULL, |
Hai Shalom | 74f70d4 | 2019-02-11 14:42:39 -0800 | [diff] [blame] | 2035 | hdrlen + kde_len, &rlen, (void *) &reply); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2036 | if (rbuf == NULL) |
| 2037 | return -1; |
| 2038 | |
Dmitry Shmidt | f21452a | 2014-02-26 10:55:25 -0800 | [diff] [blame] | 2039 | reply->type = (sm->proto == WPA_PROTO_RSN || |
| 2040 | sm->proto == WPA_PROTO_OSEN) ? |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2041 | EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA; |
| 2042 | key_info &= WPA_KEY_INFO_KEY_INDEX_MASK; |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 2043 | key_info |= ver | WPA_KEY_INFO_SECURE; |
| 2044 | if (mic_len) |
| 2045 | key_info |= WPA_KEY_INFO_MIC; |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 2046 | else |
| 2047 | key_info |= WPA_KEY_INFO_ENCR_KEY_DATA; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2048 | WPA_PUT_BE16(reply->key_info, key_info); |
Dmitry Shmidt | f21452a | 2014-02-26 10:55:25 -0800 | [diff] [blame] | 2049 | if (sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2050 | WPA_PUT_BE16(reply->key_length, 0); |
| 2051 | else |
| 2052 | os_memcpy(reply->key_length, key->key_length, 2); |
| 2053 | os_memcpy(reply->replay_counter, key->replay_counter, |
| 2054 | WPA_REPLAY_COUNTER_LEN); |
| 2055 | |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 2056 | key_mic = (u8 *) (reply + 1); |
Hai Shalom | 74f70d4 | 2019-02-11 14:42:39 -0800 | [diff] [blame] | 2057 | WPA_PUT_BE16(key_mic + mic_len, kde_len); /* Key Data Length */ |
| 2058 | |
| 2059 | #ifdef CONFIG_OCV |
| 2060 | if (wpa_sm_ocv_enabled(sm)) { |
| 2061 | struct wpa_channel_info ci; |
| 2062 | u8 *pos; |
| 2063 | |
| 2064 | if (wpa_sm_channel_info(sm, &ci) != 0) { |
| 2065 | wpa_printf(MSG_WARNING, |
| 2066 | "Failed to get channel info for OCI element in EAPOL-Key 2/2"); |
| 2067 | os_free(rbuf); |
| 2068 | return -1; |
| 2069 | } |
Hai Shalom | 899fcc7 | 2020-10-19 14:38:18 -0700 | [diff] [blame] | 2070 | #ifdef CONFIG_TESTING_OPTIONS |
| 2071 | if (sm->oci_freq_override_eapol_g2) { |
| 2072 | wpa_printf(MSG_INFO, |
| 2073 | "TEST: Override OCI KDE frequency %d -> %d MHz", |
| 2074 | ci.frequency, |
| 2075 | sm->oci_freq_override_eapol_g2); |
| 2076 | ci.frequency = sm->oci_freq_override_eapol_g2; |
| 2077 | } |
| 2078 | #endif /* CONFIG_TESTING_OPTIONS */ |
Hai Shalom | 74f70d4 | 2019-02-11 14:42:39 -0800 | [diff] [blame] | 2079 | |
| 2080 | pos = key_mic + mic_len + 2; /* Key Data */ |
| 2081 | if (ocv_insert_oci_kde(&ci, &pos) < 0) { |
| 2082 | os_free(rbuf); |
| 2083 | return -1; |
| 2084 | } |
| 2085 | } |
| 2086 | #endif /* CONFIG_OCV */ |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2087 | |
| 2088 | wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Sending EAPOL-Key 2/2"); |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 2089 | return wpa_eapol_key_send(sm, &sm->ptk, ver, sm->bssid, ETH_P_EAPOL, |
| 2090 | rbuf, rlen, key_mic); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2091 | } |
| 2092 | |
| 2093 | |
| 2094 | static void wpa_supplicant_process_1_of_2(struct wpa_sm *sm, |
| 2095 | const unsigned char *src_addr, |
| 2096 | const struct wpa_eapol_key *key, |
Dmitry Shmidt | 43cb578 | 2014-06-16 16:23:22 -0700 | [diff] [blame] | 2097 | const u8 *key_data, |
| 2098 | size_t key_data_len, u16 ver) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2099 | { |
Dmitry Shmidt | 43cb578 | 2014-06-16 16:23:22 -0700 | [diff] [blame] | 2100 | u16 key_info; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2101 | int rekey, ret; |
| 2102 | struct wpa_gtk_data gd; |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 2103 | const u8 *key_rsc; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2104 | |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 2105 | if (!sm->msg_3_of_4_ok && !wpa_fils_is_completed(sm)) { |
Dmitry Shmidt | 4dd28dc | 2015-03-10 11:21:43 -0700 | [diff] [blame] | 2106 | wpa_msg(sm->ctx->msg_ctx, MSG_INFO, |
| 2107 | "WPA: Group Key Handshake started prior to completion of 4-way handshake"); |
| 2108 | goto failed; |
| 2109 | } |
| 2110 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2111 | os_memset(&gd, 0, sizeof(gd)); |
| 2112 | |
| 2113 | rekey = wpa_sm_get_state(sm) == WPA_COMPLETED; |
| 2114 | wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: RX message 1 of Group Key " |
| 2115 | "Handshake from " MACSTR " (ver=%d)", MAC2STR(src_addr), ver); |
| 2116 | |
| 2117 | key_info = WPA_GET_BE16(key->key_info); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2118 | |
Dmitry Shmidt | f21452a | 2014-02-26 10:55:25 -0800 | [diff] [blame] | 2119 | if (sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN) { |
Dmitry Shmidt | 43cb578 | 2014-06-16 16:23:22 -0700 | [diff] [blame] | 2120 | ret = wpa_supplicant_process_1_of_2_rsn(sm, key_data, |
| 2121 | key_data_len, key_info, |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2122 | &gd); |
| 2123 | } else { |
Dmitry Shmidt | 43cb578 | 2014-06-16 16:23:22 -0700 | [diff] [blame] | 2124 | ret = wpa_supplicant_process_1_of_2_wpa(sm, key, key_data, |
| 2125 | key_data_len, |
| 2126 | key_info, ver, &gd); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2127 | } |
| 2128 | |
| 2129 | wpa_sm_set_state(sm, WPA_GROUP_HANDSHAKE); |
| 2130 | |
| 2131 | if (ret) |
| 2132 | goto failed; |
| 2133 | |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 2134 | key_rsc = key->key_rsc; |
| 2135 | if (wpa_supplicant_rsc_relaxation(sm, key->key_rsc)) |
| 2136 | key_rsc = null_rsc; |
| 2137 | |
Jouni Malinen | 58c0e96 | 2017-10-01 12:12:24 +0300 | [diff] [blame] | 2138 | if (wpa_supplicant_install_gtk(sm, &gd, key_rsc, 0) || |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 2139 | wpa_supplicant_send_2_of_2(sm, key, ver, key_info) < 0) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2140 | goto failed; |
Hai Shalom | 81f62d8 | 2019-07-22 12:10:00 -0700 | [diff] [blame] | 2141 | forced_memzero(&gd, sizeof(gd)); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2142 | |
| 2143 | if (rekey) { |
| 2144 | wpa_msg(sm->ctx->msg_ctx, MSG_INFO, "WPA: Group rekeying " |
| 2145 | "completed with " MACSTR " [GTK=%s]", |
| 2146 | MAC2STR(sm->bssid), wpa_cipher_txt(sm->group_cipher)); |
| 2147 | wpa_sm_cancel_auth_timeout(sm); |
| 2148 | wpa_sm_set_state(sm, WPA_COMPLETED); |
| 2149 | } else { |
| 2150 | wpa_supplicant_key_neg_complete(sm, sm->bssid, |
| 2151 | key_info & |
| 2152 | WPA_KEY_INFO_SECURE); |
| 2153 | } |
Dmitry Shmidt | cce0666 | 2013-11-04 18:44:24 -0800 | [diff] [blame] | 2154 | |
| 2155 | wpa_sm_set_rekey_offload(sm); |
| 2156 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2157 | return; |
| 2158 | |
| 2159 | failed: |
Hai Shalom | 81f62d8 | 2019-07-22 12:10:00 -0700 | [diff] [blame] | 2160 | forced_memzero(&gd, sizeof(gd)); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2161 | wpa_sm_deauthenticate(sm, WLAN_REASON_UNSPECIFIED); |
| 2162 | } |
| 2163 | |
| 2164 | |
| 2165 | static int wpa_supplicant_verify_eapol_key_mic(struct wpa_sm *sm, |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 2166 | struct wpa_eapol_key *key, |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2167 | u16 ver, |
| 2168 | const u8 *buf, size_t len) |
| 2169 | { |
Dmitry Shmidt | 807291d | 2015-01-27 13:40:23 -0800 | [diff] [blame] | 2170 | u8 mic[WPA_EAPOL_KEY_MIC_MAX_LEN]; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2171 | int ok = 0; |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 2172 | size_t mic_len = wpa_mic_len(sm->key_mgmt, sm->pmk_len); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2173 | |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 2174 | os_memcpy(mic, key + 1, mic_len); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2175 | if (sm->tptk_set) { |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 2176 | os_memset(key + 1, 0, mic_len); |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 2177 | if (wpa_eapol_key_mic(sm->tptk.kck, sm->tptk.kck_len, |
| 2178 | sm->key_mgmt, |
| 2179 | ver, buf, len, (u8 *) (key + 1)) < 0 || |
| 2180 | os_memcmp_const(mic, key + 1, mic_len) != 0) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2181 | wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, |
| 2182 | "WPA: Invalid EAPOL-Key MIC " |
| 2183 | "when using TPTK - ignoring TPTK"); |
Hai Shalom | 74f70d4 | 2019-02-11 14:42:39 -0800 | [diff] [blame] | 2184 | #ifdef TEST_FUZZ |
| 2185 | wpa_printf(MSG_INFO, |
| 2186 | "TEST: Ignore Key MIC failure for fuzz testing"); |
| 2187 | goto continue_fuzz; |
| 2188 | #endif /* TEST_FUZZ */ |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2189 | } else { |
Hai Shalom | 74f70d4 | 2019-02-11 14:42:39 -0800 | [diff] [blame] | 2190 | #ifdef TEST_FUZZ |
| 2191 | continue_fuzz: |
| 2192 | #endif /* TEST_FUZZ */ |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2193 | ok = 1; |
| 2194 | sm->tptk_set = 0; |
| 2195 | sm->ptk_set = 1; |
| 2196 | os_memcpy(&sm->ptk, &sm->tptk, sizeof(sm->ptk)); |
Dmitry Shmidt | 61593f0 | 2014-04-21 16:27:35 -0700 | [diff] [blame] | 2197 | os_memset(&sm->tptk, 0, sizeof(sm->tptk)); |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 2198 | /* |
| 2199 | * This assures the same TPTK in sm->tptk can never be |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame] | 2200 | * copied twice to sm->ptk as the new PTK. In |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 2201 | * combination with the installed flag in the wpa_ptk |
| 2202 | * struct, this assures the same PTK is only installed |
| 2203 | * once. |
| 2204 | */ |
| 2205 | sm->renew_snonce = 1; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2206 | } |
| 2207 | } |
| 2208 | |
| 2209 | if (!ok && sm->ptk_set) { |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 2210 | os_memset(key + 1, 0, mic_len); |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 2211 | if (wpa_eapol_key_mic(sm->ptk.kck, sm->ptk.kck_len, |
| 2212 | sm->key_mgmt, |
| 2213 | ver, buf, len, (u8 *) (key + 1)) < 0 || |
| 2214 | os_memcmp_const(mic, key + 1, mic_len) != 0) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2215 | wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, |
| 2216 | "WPA: Invalid EAPOL-Key MIC - " |
| 2217 | "dropping packet"); |
Hai Shalom | 74f70d4 | 2019-02-11 14:42:39 -0800 | [diff] [blame] | 2218 | #ifdef TEST_FUZZ |
| 2219 | wpa_printf(MSG_INFO, |
| 2220 | "TEST: Ignore Key MIC failure for fuzz testing"); |
| 2221 | goto continue_fuzz2; |
| 2222 | #endif /* TEST_FUZZ */ |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2223 | return -1; |
| 2224 | } |
Hai Shalom | 74f70d4 | 2019-02-11 14:42:39 -0800 | [diff] [blame] | 2225 | #ifdef TEST_FUZZ |
| 2226 | continue_fuzz2: |
| 2227 | #endif /* TEST_FUZZ */ |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2228 | ok = 1; |
| 2229 | } |
| 2230 | |
| 2231 | if (!ok) { |
| 2232 | wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, |
| 2233 | "WPA: Could not verify EAPOL-Key MIC - " |
| 2234 | "dropping packet"); |
| 2235 | return -1; |
| 2236 | } |
| 2237 | |
| 2238 | os_memcpy(sm->rx_replay_counter, key->replay_counter, |
| 2239 | WPA_REPLAY_COUNTER_LEN); |
| 2240 | sm->rx_replay_counter_set = 1; |
| 2241 | return 0; |
| 2242 | } |
| 2243 | |
| 2244 | |
| 2245 | /* Decrypt RSN EAPOL-Key key data (RC4 or AES-WRAP) */ |
| 2246 | static int wpa_supplicant_decrypt_key_data(struct wpa_sm *sm, |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 2247 | struct wpa_eapol_key *key, |
| 2248 | size_t mic_len, u16 ver, |
Dmitry Shmidt | 43cb578 | 2014-06-16 16:23:22 -0700 | [diff] [blame] | 2249 | u8 *key_data, size_t *key_data_len) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2250 | { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2251 | wpa_hexdump(MSG_DEBUG, "RSN: encrypted key data", |
Dmitry Shmidt | 43cb578 | 2014-06-16 16:23:22 -0700 | [diff] [blame] | 2252 | key_data, *key_data_len); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2253 | if (!sm->ptk_set) { |
| 2254 | wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, |
| 2255 | "WPA: PTK not available, cannot decrypt EAPOL-Key Key " |
| 2256 | "Data"); |
| 2257 | return -1; |
| 2258 | } |
| 2259 | |
| 2260 | /* Decrypt key data here so that this operation does not need |
| 2261 | * to be implemented separately for each message type. */ |
Dmitry Shmidt | 807291d | 2015-01-27 13:40:23 -0800 | [diff] [blame] | 2262 | if (ver == WPA_KEY_INFO_TYPE_HMAC_MD5_RC4 && sm->ptk.kek_len == 16) { |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 2263 | #ifdef CONFIG_NO_RC4 |
| 2264 | wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, |
| 2265 | "WPA: RC4 not supported in the build"); |
| 2266 | return -1; |
| 2267 | #else /* CONFIG_NO_RC4 */ |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2268 | u8 ek[32]; |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 2269 | |
| 2270 | wpa_printf(MSG_DEBUG, "WPA: Decrypt Key Data using RC4"); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2271 | os_memcpy(ek, key->key_iv, 16); |
Dmitry Shmidt | 807291d | 2015-01-27 13:40:23 -0800 | [diff] [blame] | 2272 | os_memcpy(ek + 16, sm->ptk.kek, sm->ptk.kek_len); |
Dmitry Shmidt | 43cb578 | 2014-06-16 16:23:22 -0700 | [diff] [blame] | 2273 | if (rc4_skip(ek, 32, 256, key_data, *key_data_len)) { |
Hai Shalom | 81f62d8 | 2019-07-22 12:10:00 -0700 | [diff] [blame] | 2274 | forced_memzero(ek, sizeof(ek)); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2275 | wpa_msg(sm->ctx->msg_ctx, MSG_ERROR, |
| 2276 | "WPA: RC4 failed"); |
| 2277 | return -1; |
| 2278 | } |
Hai Shalom | 81f62d8 | 2019-07-22 12:10:00 -0700 | [diff] [blame] | 2279 | forced_memzero(ek, sizeof(ek)); |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 2280 | #endif /* CONFIG_NO_RC4 */ |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2281 | } else if (ver == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES || |
Dmitry Shmidt | f21452a | 2014-02-26 10:55:25 -0800 | [diff] [blame] | 2282 | ver == WPA_KEY_INFO_TYPE_AES_128_CMAC || |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame] | 2283 | wpa_use_aes_key_wrap(sm->key_mgmt)) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2284 | u8 *buf; |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 2285 | |
| 2286 | wpa_printf(MSG_DEBUG, |
| 2287 | "WPA: Decrypt Key Data using AES-UNWRAP (KEK length %u)", |
| 2288 | (unsigned int) sm->ptk.kek_len); |
Dmitry Shmidt | 43cb578 | 2014-06-16 16:23:22 -0700 | [diff] [blame] | 2289 | if (*key_data_len < 8 || *key_data_len % 8) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2290 | wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, |
Dmitry Shmidt | 43cb578 | 2014-06-16 16:23:22 -0700 | [diff] [blame] | 2291 | "WPA: Unsupported AES-WRAP len %u", |
| 2292 | (unsigned int) *key_data_len); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2293 | return -1; |
| 2294 | } |
Dmitry Shmidt | 43cb578 | 2014-06-16 16:23:22 -0700 | [diff] [blame] | 2295 | *key_data_len -= 8; /* AES-WRAP adds 8 bytes */ |
| 2296 | buf = os_malloc(*key_data_len); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2297 | if (buf == NULL) { |
| 2298 | wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, |
| 2299 | "WPA: No memory for AES-UNWRAP buffer"); |
| 2300 | return -1; |
| 2301 | } |
Hai Shalom | 74f70d4 | 2019-02-11 14:42:39 -0800 | [diff] [blame] | 2302 | #ifdef TEST_FUZZ |
| 2303 | os_memset(buf, 0x11, *key_data_len); |
| 2304 | #endif /* TEST_FUZZ */ |
Dmitry Shmidt | 807291d | 2015-01-27 13:40:23 -0800 | [diff] [blame] | 2305 | if (aes_unwrap(sm->ptk.kek, sm->ptk.kek_len, *key_data_len / 8, |
Dmitry Shmidt | 43cb578 | 2014-06-16 16:23:22 -0700 | [diff] [blame] | 2306 | key_data, buf)) { |
Hai Shalom | 74f70d4 | 2019-02-11 14:42:39 -0800 | [diff] [blame] | 2307 | #ifdef TEST_FUZZ |
| 2308 | wpa_printf(MSG_INFO, |
| 2309 | "TEST: Ignore AES unwrap failure for fuzz testing"); |
| 2310 | goto continue_fuzz; |
| 2311 | #endif /* TEST_FUZZ */ |
Dmitry Shmidt | 7d56b75 | 2015-12-22 10:59:44 -0800 | [diff] [blame] | 2312 | bin_clear_free(buf, *key_data_len); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2313 | wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, |
| 2314 | "WPA: AES unwrap failed - " |
| 2315 | "could not decrypt EAPOL-Key key data"); |
| 2316 | return -1; |
| 2317 | } |
Hai Shalom | 74f70d4 | 2019-02-11 14:42:39 -0800 | [diff] [blame] | 2318 | #ifdef TEST_FUZZ |
| 2319 | continue_fuzz: |
| 2320 | #endif /* TEST_FUZZ */ |
Dmitry Shmidt | 43cb578 | 2014-06-16 16:23:22 -0700 | [diff] [blame] | 2321 | os_memcpy(key_data, buf, *key_data_len); |
Dmitry Shmidt | 7d56b75 | 2015-12-22 10:59:44 -0800 | [diff] [blame] | 2322 | bin_clear_free(buf, *key_data_len); |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 2323 | WPA_PUT_BE16(((u8 *) (key + 1)) + mic_len, *key_data_len); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2324 | } else { |
| 2325 | wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, |
| 2326 | "WPA: Unsupported key_info type %d", ver); |
| 2327 | return -1; |
| 2328 | } |
| 2329 | wpa_hexdump_key(MSG_DEBUG, "WPA: decrypted EAPOL-Key key data", |
Dmitry Shmidt | 43cb578 | 2014-06-16 16:23:22 -0700 | [diff] [blame] | 2330 | key_data, *key_data_len); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2331 | return 0; |
| 2332 | } |
| 2333 | |
| 2334 | |
| 2335 | /** |
| 2336 | * wpa_sm_aborted_cached - Notify WPA that PMKSA caching was aborted |
| 2337 | * @sm: Pointer to WPA state machine data from wpa_sm_init() |
| 2338 | */ |
| 2339 | void wpa_sm_aborted_cached(struct wpa_sm *sm) |
| 2340 | { |
| 2341 | if (sm && sm->cur_pmksa) { |
| 2342 | wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, |
| 2343 | "RSN: Cancelling PMKSA caching attempt"); |
| 2344 | sm->cur_pmksa = NULL; |
| 2345 | } |
| 2346 | } |
| 2347 | |
| 2348 | |
Hai Shalom | c1a2144 | 2022-02-04 13:43:00 -0800 | [diff] [blame] | 2349 | void wpa_sm_aborted_external_cached(struct wpa_sm *sm) |
| 2350 | { |
| 2351 | if (sm && sm->cur_pmksa && sm->cur_pmksa->external) { |
| 2352 | wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, |
| 2353 | "RSN: Cancelling external PMKSA caching attempt"); |
| 2354 | sm->cur_pmksa = NULL; |
| 2355 | } |
| 2356 | } |
| 2357 | |
| 2358 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2359 | static void wpa_eapol_key_dump(struct wpa_sm *sm, |
Dmitry Shmidt | 807291d | 2015-01-27 13:40:23 -0800 | [diff] [blame] | 2360 | const struct wpa_eapol_key *key, |
| 2361 | unsigned int key_data_len, |
| 2362 | const u8 *mic, unsigned int mic_len) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2363 | { |
| 2364 | #ifndef CONFIG_NO_STDOUT_DEBUG |
| 2365 | u16 key_info = WPA_GET_BE16(key->key_info); |
| 2366 | |
| 2367 | wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, " EAPOL-Key type=%d", key->type); |
| 2368 | wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, |
| 2369 | " key_info 0x%x (ver=%d keyidx=%d rsvd=%d %s%s%s%s%s%s%s%s)", |
| 2370 | key_info, key_info & WPA_KEY_INFO_TYPE_MASK, |
| 2371 | (key_info & WPA_KEY_INFO_KEY_INDEX_MASK) >> |
| 2372 | WPA_KEY_INFO_KEY_INDEX_SHIFT, |
| 2373 | (key_info & (BIT(13) | BIT(14) | BIT(15))) >> 13, |
| 2374 | key_info & WPA_KEY_INFO_KEY_TYPE ? "Pairwise" : "Group", |
| 2375 | key_info & WPA_KEY_INFO_INSTALL ? " Install" : "", |
| 2376 | key_info & WPA_KEY_INFO_ACK ? " Ack" : "", |
| 2377 | key_info & WPA_KEY_INFO_MIC ? " MIC" : "", |
| 2378 | key_info & WPA_KEY_INFO_SECURE ? " Secure" : "", |
| 2379 | key_info & WPA_KEY_INFO_ERROR ? " Error" : "", |
| 2380 | key_info & WPA_KEY_INFO_REQUEST ? " Request" : "", |
| 2381 | key_info & WPA_KEY_INFO_ENCR_KEY_DATA ? " Encr" : ""); |
| 2382 | wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, |
| 2383 | " key_length=%u key_data_length=%u", |
Dmitry Shmidt | 807291d | 2015-01-27 13:40:23 -0800 | [diff] [blame] | 2384 | WPA_GET_BE16(key->key_length), key_data_len); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2385 | wpa_hexdump(MSG_DEBUG, " replay_counter", |
| 2386 | key->replay_counter, WPA_REPLAY_COUNTER_LEN); |
| 2387 | wpa_hexdump(MSG_DEBUG, " key_nonce", key->key_nonce, WPA_NONCE_LEN); |
| 2388 | wpa_hexdump(MSG_DEBUG, " key_iv", key->key_iv, 16); |
| 2389 | wpa_hexdump(MSG_DEBUG, " key_rsc", key->key_rsc, 8); |
| 2390 | wpa_hexdump(MSG_DEBUG, " key_id (reserved)", key->key_id, 8); |
Dmitry Shmidt | 807291d | 2015-01-27 13:40:23 -0800 | [diff] [blame] | 2391 | wpa_hexdump(MSG_DEBUG, " key_mic", mic, mic_len); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2392 | #endif /* CONFIG_NO_STDOUT_DEBUG */ |
| 2393 | } |
| 2394 | |
| 2395 | |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 2396 | #ifdef CONFIG_FILS |
| 2397 | static int wpa_supp_aead_decrypt(struct wpa_sm *sm, u8 *buf, size_t buf_len, |
| 2398 | size_t *key_data_len) |
| 2399 | { |
| 2400 | struct wpa_ptk *ptk; |
| 2401 | struct ieee802_1x_hdr *hdr; |
| 2402 | struct wpa_eapol_key *key; |
| 2403 | u8 *pos, *tmp; |
| 2404 | const u8 *aad[1]; |
| 2405 | size_t aad_len[1]; |
| 2406 | |
| 2407 | if (*key_data_len < AES_BLOCK_SIZE) { |
| 2408 | wpa_printf(MSG_INFO, "No room for AES-SIV data in the frame"); |
| 2409 | return -1; |
| 2410 | } |
| 2411 | |
| 2412 | if (sm->tptk_set) |
| 2413 | ptk = &sm->tptk; |
| 2414 | else if (sm->ptk_set) |
| 2415 | ptk = &sm->ptk; |
| 2416 | else |
| 2417 | return -1; |
| 2418 | |
| 2419 | hdr = (struct ieee802_1x_hdr *) buf; |
| 2420 | key = (struct wpa_eapol_key *) (hdr + 1); |
| 2421 | pos = (u8 *) (key + 1); |
| 2422 | pos += 2; /* Pointing at the Encrypted Key Data field */ |
| 2423 | |
| 2424 | tmp = os_malloc(*key_data_len); |
| 2425 | if (!tmp) |
| 2426 | return -1; |
| 2427 | |
| 2428 | /* AES-SIV AAD from EAPOL protocol version field (inclusive) to |
| 2429 | * to Key Data (exclusive). */ |
| 2430 | aad[0] = buf; |
| 2431 | aad_len[0] = pos - buf; |
| 2432 | if (aes_siv_decrypt(ptk->kek, ptk->kek_len, pos, *key_data_len, |
| 2433 | 1, aad, aad_len, tmp) < 0) { |
| 2434 | wpa_printf(MSG_INFO, "Invalid AES-SIV data in the frame"); |
| 2435 | bin_clear_free(tmp, *key_data_len); |
| 2436 | return -1; |
| 2437 | } |
| 2438 | |
| 2439 | /* AEAD decryption and validation completed successfully */ |
| 2440 | (*key_data_len) -= AES_BLOCK_SIZE; |
| 2441 | wpa_hexdump_key(MSG_DEBUG, "WPA: Decrypted Key Data", |
| 2442 | tmp, *key_data_len); |
| 2443 | |
| 2444 | /* Replace Key Data field with the decrypted version */ |
| 2445 | os_memcpy(pos, tmp, *key_data_len); |
| 2446 | pos -= 2; /* Key Data Length field */ |
| 2447 | WPA_PUT_BE16(pos, *key_data_len); |
| 2448 | bin_clear_free(tmp, *key_data_len); |
| 2449 | |
| 2450 | if (sm->tptk_set) { |
| 2451 | sm->tptk_set = 0; |
| 2452 | sm->ptk_set = 1; |
| 2453 | os_memcpy(&sm->ptk, &sm->tptk, sizeof(sm->ptk)); |
| 2454 | os_memset(&sm->tptk, 0, sizeof(sm->tptk)); |
| 2455 | } |
| 2456 | |
| 2457 | os_memcpy(sm->rx_replay_counter, key->replay_counter, |
| 2458 | WPA_REPLAY_COUNTER_LEN); |
| 2459 | sm->rx_replay_counter_set = 1; |
| 2460 | |
| 2461 | return 0; |
| 2462 | } |
| 2463 | #endif /* CONFIG_FILS */ |
| 2464 | |
| 2465 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2466 | /** |
| 2467 | * wpa_sm_rx_eapol - Process received WPA EAPOL frames |
| 2468 | * @sm: Pointer to WPA state machine data from wpa_sm_init() |
| 2469 | * @src_addr: Source MAC address of the EAPOL packet |
| 2470 | * @buf: Pointer to the beginning of the EAPOL data (EAPOL header) |
| 2471 | * @len: Length of the EAPOL frame |
| 2472 | * Returns: 1 = WPA EAPOL-Key processed, 0 = not a WPA EAPOL-Key, -1 failure |
| 2473 | * |
| 2474 | * This function is called for each received EAPOL frame. Other than EAPOL-Key |
| 2475 | * frames can be skipped if filtering is done elsewhere. wpa_sm_rx_eapol() is |
| 2476 | * only processing WPA and WPA2 EAPOL-Key frames. |
| 2477 | * |
| 2478 | * The received EAPOL-Key packets are validated and valid packets are replied |
| 2479 | * to. In addition, key material (PTK, GTK) is configured at the end of a |
| 2480 | * successful key handshake. |
| 2481 | */ |
| 2482 | int wpa_sm_rx_eapol(struct wpa_sm *sm, const u8 *src_addr, |
| 2483 | const u8 *buf, size_t len) |
| 2484 | { |
Dmitry Shmidt | 43cb578 | 2014-06-16 16:23:22 -0700 | [diff] [blame] | 2485 | size_t plen, data_len, key_data_len; |
| 2486 | const struct ieee802_1x_hdr *hdr; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2487 | struct wpa_eapol_key *key; |
| 2488 | u16 key_info, ver; |
Dmitry Shmidt | 43cb578 | 2014-06-16 16:23:22 -0700 | [diff] [blame] | 2489 | u8 *tmp = NULL; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2490 | int ret = -1; |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 2491 | u8 *mic, *key_data; |
Hai Shalom | 899fcc7 | 2020-10-19 14:38:18 -0700 | [diff] [blame] | 2492 | size_t mic_len, keyhdrlen, pmk_len; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2493 | |
| 2494 | #ifdef CONFIG_IEEE80211R |
| 2495 | sm->ft_completed = 0; |
| 2496 | #endif /* CONFIG_IEEE80211R */ |
| 2497 | |
Hai Shalom | 899fcc7 | 2020-10-19 14:38:18 -0700 | [diff] [blame] | 2498 | pmk_len = sm->pmk_len; |
| 2499 | if (!pmk_len && sm->cur_pmksa) |
| 2500 | pmk_len = sm->cur_pmksa->pmk_len; |
| 2501 | mic_len = wpa_mic_len(sm->key_mgmt, pmk_len); |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 2502 | keyhdrlen = sizeof(*key) + mic_len + 2; |
Dmitry Shmidt | 807291d | 2015-01-27 13:40:23 -0800 | [diff] [blame] | 2503 | |
| 2504 | if (len < sizeof(*hdr) + keyhdrlen) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2505 | wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, |
| 2506 | "WPA: EAPOL frame too short to be a WPA " |
| 2507 | "EAPOL-Key (len %lu, expecting at least %lu)", |
| 2508 | (unsigned long) len, |
Dmitry Shmidt | 807291d | 2015-01-27 13:40:23 -0800 | [diff] [blame] | 2509 | (unsigned long) sizeof(*hdr) + keyhdrlen); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2510 | return 0; |
| 2511 | } |
| 2512 | |
Dmitry Shmidt | 43cb578 | 2014-06-16 16:23:22 -0700 | [diff] [blame] | 2513 | hdr = (const struct ieee802_1x_hdr *) buf; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2514 | plen = be_to_host16(hdr->length); |
| 2515 | data_len = plen + sizeof(*hdr); |
| 2516 | wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, |
| 2517 | "IEEE 802.1X RX: version=%d type=%d length=%lu", |
| 2518 | hdr->version, hdr->type, (unsigned long) plen); |
| 2519 | |
| 2520 | if (hdr->version < EAPOL_VERSION) { |
| 2521 | /* TODO: backwards compatibility */ |
| 2522 | } |
| 2523 | if (hdr->type != IEEE802_1X_TYPE_EAPOL_KEY) { |
| 2524 | wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, |
| 2525 | "WPA: EAPOL frame (type %u) discarded, " |
| 2526 | "not a Key frame", hdr->type); |
| 2527 | ret = 0; |
| 2528 | goto out; |
| 2529 | } |
Dmitry Shmidt | 43cb578 | 2014-06-16 16:23:22 -0700 | [diff] [blame] | 2530 | wpa_hexdump(MSG_MSGDUMP, "WPA: RX EAPOL-Key", buf, len); |
Dmitry Shmidt | 807291d | 2015-01-27 13:40:23 -0800 | [diff] [blame] | 2531 | if (plen > len - sizeof(*hdr) || plen < keyhdrlen) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2532 | wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, |
| 2533 | "WPA: EAPOL frame payload size %lu " |
| 2534 | "invalid (frame size %lu)", |
| 2535 | (unsigned long) plen, (unsigned long) len); |
| 2536 | ret = 0; |
| 2537 | goto out; |
| 2538 | } |
Dmitry Shmidt | 43cb578 | 2014-06-16 16:23:22 -0700 | [diff] [blame] | 2539 | if (data_len < len) { |
| 2540 | wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, |
| 2541 | "WPA: ignoring %lu bytes after the IEEE 802.1X data", |
| 2542 | (unsigned long) len - data_len); |
| 2543 | } |
| 2544 | |
| 2545 | /* |
| 2546 | * Make a copy of the frame since we need to modify the buffer during |
| 2547 | * MAC validation and Key Data decryption. |
| 2548 | */ |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 2549 | tmp = os_memdup(buf, data_len); |
Dmitry Shmidt | 43cb578 | 2014-06-16 16:23:22 -0700 | [diff] [blame] | 2550 | if (tmp == NULL) |
| 2551 | goto out; |
Dmitry Shmidt | 43cb578 | 2014-06-16 16:23:22 -0700 | [diff] [blame] | 2552 | key = (struct wpa_eapol_key *) (tmp + sizeof(struct ieee802_1x_hdr)); |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 2553 | mic = (u8 *) (key + 1); |
| 2554 | key_data = mic + mic_len + 2; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2555 | |
| 2556 | if (key->type != EAPOL_KEY_TYPE_WPA && key->type != EAPOL_KEY_TYPE_RSN) |
| 2557 | { |
| 2558 | wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, |
| 2559 | "WPA: EAPOL-Key type (%d) unknown, discarded", |
| 2560 | key->type); |
| 2561 | ret = 0; |
| 2562 | goto out; |
| 2563 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2564 | |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 2565 | key_data_len = WPA_GET_BE16(mic + mic_len); |
| 2566 | wpa_eapol_key_dump(sm, key, key_data_len, mic, mic_len); |
Dmitry Shmidt | 807291d | 2015-01-27 13:40:23 -0800 | [diff] [blame] | 2567 | |
| 2568 | if (key_data_len > plen - keyhdrlen) { |
Dmitry Shmidt | 43cb578 | 2014-06-16 16:23:22 -0700 | [diff] [blame] | 2569 | wpa_msg(sm->ctx->msg_ctx, MSG_INFO, "WPA: Invalid EAPOL-Key " |
| 2570 | "frame - key_data overflow (%u > %u)", |
| 2571 | (unsigned int) key_data_len, |
Dmitry Shmidt | 807291d | 2015-01-27 13:40:23 -0800 | [diff] [blame] | 2572 | (unsigned int) (plen - keyhdrlen)); |
Dmitry Shmidt | 43cb578 | 2014-06-16 16:23:22 -0700 | [diff] [blame] | 2573 | goto out; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2574 | } |
Dmitry Shmidt | 43cb578 | 2014-06-16 16:23:22 -0700 | [diff] [blame] | 2575 | |
| 2576 | eapol_sm_notify_lower_layer_success(sm->eapol, 0); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2577 | key_info = WPA_GET_BE16(key->key_info); |
| 2578 | ver = key_info & WPA_KEY_INFO_TYPE_MASK; |
| 2579 | if (ver != WPA_KEY_INFO_TYPE_HMAC_MD5_RC4 && |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2580 | ver != WPA_KEY_INFO_TYPE_AES_128_CMAC && |
Dmitry Shmidt | f21452a | 2014-02-26 10:55:25 -0800 | [diff] [blame] | 2581 | ver != WPA_KEY_INFO_TYPE_HMAC_SHA1_AES && |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame] | 2582 | !wpa_use_akm_defined(sm->key_mgmt)) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2583 | wpa_msg(sm->ctx->msg_ctx, MSG_INFO, |
| 2584 | "WPA: Unsupported EAPOL-Key descriptor version %d", |
| 2585 | ver); |
| 2586 | goto out; |
| 2587 | } |
| 2588 | |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame] | 2589 | if (wpa_use_akm_defined(sm->key_mgmt) && |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 2590 | ver != WPA_KEY_INFO_TYPE_AKM_DEFINED) { |
| 2591 | wpa_msg(sm->ctx->msg_ctx, MSG_INFO, |
| 2592 | "RSN: Unsupported EAPOL-Key descriptor version %d (expected AKM defined = 0)", |
| 2593 | ver); |
| 2594 | goto out; |
| 2595 | } |
| 2596 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2597 | #ifdef CONFIG_IEEE80211R |
| 2598 | if (wpa_key_mgmt_ft(sm->key_mgmt)) { |
| 2599 | /* IEEE 802.11r uses a new key_info type (AES-128-CMAC). */ |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame] | 2600 | if (ver != WPA_KEY_INFO_TYPE_AES_128_CMAC && |
| 2601 | !wpa_use_akm_defined(sm->key_mgmt)) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2602 | wpa_msg(sm->ctx->msg_ctx, MSG_INFO, |
| 2603 | "FT: AP did not use AES-128-CMAC"); |
| 2604 | goto out; |
| 2605 | } |
| 2606 | } else |
| 2607 | #endif /* CONFIG_IEEE80211R */ |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2608 | if (wpa_key_mgmt_sha256(sm->key_mgmt)) { |
Dmitry Shmidt | f21452a | 2014-02-26 10:55:25 -0800 | [diff] [blame] | 2609 | if (ver != WPA_KEY_INFO_TYPE_AES_128_CMAC && |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame] | 2610 | !wpa_use_akm_defined(sm->key_mgmt)) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2611 | wpa_msg(sm->ctx->msg_ctx, MSG_INFO, |
| 2612 | "WPA: AP did not use the " |
| 2613 | "negotiated AES-128-CMAC"); |
| 2614 | goto out; |
| 2615 | } |
Hai Shalom | c356592 | 2019-10-28 11:58:20 -0700 | [diff] [blame] | 2616 | } else if (sm->pairwise_cipher == WPA_CIPHER_CCMP && |
| 2617 | !wpa_use_akm_defined(sm->key_mgmt) && |
| 2618 | ver != WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2619 | wpa_msg(sm->ctx->msg_ctx, MSG_INFO, |
| 2620 | "WPA: CCMP is used, but EAPOL-Key " |
| 2621 | "descriptor version (%d) is not 2", ver); |
| 2622 | if (sm->group_cipher != WPA_CIPHER_CCMP && |
| 2623 | !(key_info & WPA_KEY_INFO_KEY_TYPE)) { |
| 2624 | /* Earlier versions of IEEE 802.11i did not explicitly |
| 2625 | * require version 2 descriptor for all EAPOL-Key |
| 2626 | * packets, so allow group keys to use version 1 if |
| 2627 | * CCMP is not used for them. */ |
| 2628 | wpa_msg(sm->ctx->msg_ctx, MSG_INFO, |
| 2629 | "WPA: Backwards compatibility: allow invalid " |
| 2630 | "version for non-CCMP group keys"); |
Jouni Malinen | 658fb4a | 2014-11-14 20:57:05 +0200 | [diff] [blame] | 2631 | } else if (ver == WPA_KEY_INFO_TYPE_AES_128_CMAC) { |
| 2632 | wpa_msg(sm->ctx->msg_ctx, MSG_INFO, |
| 2633 | "WPA: Interoperability workaround: allow incorrect (should have been HMAC-SHA1), but stronger (is AES-128-CMAC), descriptor version to be used"); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2634 | } else |
| 2635 | goto out; |
Dmitry Shmidt | 7175743 | 2014-06-02 13:50:35 -0700 | [diff] [blame] | 2636 | } else if (sm->pairwise_cipher == WPA_CIPHER_GCMP && |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame] | 2637 | !wpa_use_akm_defined(sm->key_mgmt) && |
Dmitry Shmidt | 7175743 | 2014-06-02 13:50:35 -0700 | [diff] [blame] | 2638 | ver != WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) { |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 2639 | wpa_msg(sm->ctx->msg_ctx, MSG_INFO, |
| 2640 | "WPA: GCMP is used, but EAPOL-Key " |
| 2641 | "descriptor version (%d) is not 2", ver); |
| 2642 | goto out; |
| 2643 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2644 | |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 2645 | if (sm->rx_replay_counter_set && |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2646 | os_memcmp(key->replay_counter, sm->rx_replay_counter, |
| 2647 | WPA_REPLAY_COUNTER_LEN) <= 0) { |
| 2648 | wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, |
| 2649 | "WPA: EAPOL-Key Replay Counter did not increase - " |
| 2650 | "dropping packet"); |
| 2651 | goto out; |
| 2652 | } |
| 2653 | |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 2654 | if (key_info & WPA_KEY_INFO_SMK_MESSAGE) { |
| 2655 | wpa_msg(sm->ctx->msg_ctx, MSG_INFO, |
| 2656 | "WPA: Unsupported SMK bit in key_info"); |
| 2657 | goto out; |
| 2658 | } |
| 2659 | |
| 2660 | if (!(key_info & WPA_KEY_INFO_ACK)) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2661 | wpa_msg(sm->ctx->msg_ctx, MSG_INFO, |
| 2662 | "WPA: No Ack bit in key_info"); |
| 2663 | goto out; |
| 2664 | } |
| 2665 | |
| 2666 | if (key_info & WPA_KEY_INFO_REQUEST) { |
| 2667 | wpa_msg(sm->ctx->msg_ctx, MSG_INFO, |
| 2668 | "WPA: EAPOL-Key with Request bit - dropped"); |
| 2669 | goto out; |
| 2670 | } |
| 2671 | |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 2672 | if ((key_info & WPA_KEY_INFO_MIC) && |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 2673 | wpa_supplicant_verify_eapol_key_mic(sm, key, ver, tmp, data_len)) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2674 | goto out; |
| 2675 | |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 2676 | #ifdef CONFIG_FILS |
| 2677 | if (!mic_len && (key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) { |
| 2678 | if (wpa_supp_aead_decrypt(sm, tmp, data_len, &key_data_len)) |
| 2679 | goto out; |
| 2680 | } |
| 2681 | #endif /* CONFIG_FILS */ |
| 2682 | |
Dmitry Shmidt | f21452a | 2014-02-26 10:55:25 -0800 | [diff] [blame] | 2683 | if ((sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN) && |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 2684 | (key_info & WPA_KEY_INFO_ENCR_KEY_DATA) && mic_len) { |
Hai Shalom | ce48b4a | 2018-09-05 11:41:35 -0700 | [diff] [blame] | 2685 | /* |
| 2686 | * Only decrypt the Key Data field if the frame's authenticity |
| 2687 | * was verified. When using AES-SIV (FILS), the MIC flag is not |
| 2688 | * set, so this check should only be performed if mic_len != 0 |
| 2689 | * which is the case in this code branch. |
| 2690 | */ |
| 2691 | if (!(key_info & WPA_KEY_INFO_MIC)) { |
| 2692 | wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, |
| 2693 | "WPA: Ignore EAPOL-Key with encrypted but unauthenticated data"); |
| 2694 | goto out; |
| 2695 | } |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 2696 | if (wpa_supplicant_decrypt_key_data(sm, key, mic_len, |
| 2697 | ver, key_data, |
Dmitry Shmidt | 43cb578 | 2014-06-16 16:23:22 -0700 | [diff] [blame] | 2698 | &key_data_len)) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2699 | goto out; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2700 | } |
| 2701 | |
| 2702 | if (key_info & WPA_KEY_INFO_KEY_TYPE) { |
| 2703 | if (key_info & WPA_KEY_INFO_KEY_INDEX_MASK) { |
| 2704 | wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, |
| 2705 | "WPA: Ignored EAPOL-Key (Pairwise) with " |
| 2706 | "non-zero key index"); |
| 2707 | goto out; |
| 2708 | } |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 2709 | if (key_info & (WPA_KEY_INFO_MIC | |
| 2710 | WPA_KEY_INFO_ENCR_KEY_DATA)) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2711 | /* 3/4 4-Way Handshake */ |
Dmitry Shmidt | 43cb578 | 2014-06-16 16:23:22 -0700 | [diff] [blame] | 2712 | wpa_supplicant_process_3_of_4(sm, key, ver, key_data, |
| 2713 | key_data_len); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2714 | } else { |
| 2715 | /* 1/4 4-Way Handshake */ |
| 2716 | wpa_supplicant_process_1_of_4(sm, src_addr, key, |
Dmitry Shmidt | 43cb578 | 2014-06-16 16:23:22 -0700 | [diff] [blame] | 2717 | ver, key_data, |
| 2718 | key_data_len); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2719 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2720 | } else { |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 2721 | if ((mic_len && (key_info & WPA_KEY_INFO_MIC)) || |
| 2722 | (!mic_len && (key_info & WPA_KEY_INFO_ENCR_KEY_DATA))) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2723 | /* 1/2 Group Key Handshake */ |
| 2724 | wpa_supplicant_process_1_of_2(sm, src_addr, key, |
Dmitry Shmidt | 43cb578 | 2014-06-16 16:23:22 -0700 | [diff] [blame] | 2725 | key_data, key_data_len, |
| 2726 | ver); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2727 | } else { |
| 2728 | wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 2729 | "WPA: EAPOL-Key (Group) without Mic/Encr bit - " |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2730 | "dropped"); |
| 2731 | } |
| 2732 | } |
| 2733 | |
| 2734 | ret = 1; |
| 2735 | |
| 2736 | out: |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 2737 | bin_clear_free(tmp, data_len); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2738 | return ret; |
| 2739 | } |
| 2740 | |
| 2741 | |
| 2742 | #ifdef CONFIG_CTRL_IFACE |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2743 | static u32 wpa_key_mgmt_suite(struct wpa_sm *sm) |
| 2744 | { |
| 2745 | switch (sm->key_mgmt) { |
| 2746 | case WPA_KEY_MGMT_IEEE8021X: |
Dmitry Shmidt | f21452a | 2014-02-26 10:55:25 -0800 | [diff] [blame] | 2747 | return ((sm->proto == WPA_PROTO_RSN || |
| 2748 | sm->proto == WPA_PROTO_OSEN) ? |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2749 | RSN_AUTH_KEY_MGMT_UNSPEC_802_1X : |
| 2750 | WPA_AUTH_KEY_MGMT_UNSPEC_802_1X); |
| 2751 | case WPA_KEY_MGMT_PSK: |
| 2752 | return (sm->proto == WPA_PROTO_RSN ? |
| 2753 | RSN_AUTH_KEY_MGMT_PSK_OVER_802_1X : |
| 2754 | WPA_AUTH_KEY_MGMT_PSK_OVER_802_1X); |
| 2755 | #ifdef CONFIG_IEEE80211R |
| 2756 | case WPA_KEY_MGMT_FT_IEEE8021X: |
| 2757 | return RSN_AUTH_KEY_MGMT_FT_802_1X; |
| 2758 | case WPA_KEY_MGMT_FT_PSK: |
| 2759 | return RSN_AUTH_KEY_MGMT_FT_PSK; |
| 2760 | #endif /* CONFIG_IEEE80211R */ |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2761 | case WPA_KEY_MGMT_IEEE8021X_SHA256: |
| 2762 | return RSN_AUTH_KEY_MGMT_802_1X_SHA256; |
| 2763 | case WPA_KEY_MGMT_PSK_SHA256: |
| 2764 | return RSN_AUTH_KEY_MGMT_PSK_SHA256; |
Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 2765 | case WPA_KEY_MGMT_CCKM: |
| 2766 | return (sm->proto == WPA_PROTO_RSN ? |
| 2767 | RSN_AUTH_KEY_MGMT_CCKM: |
| 2768 | WPA_AUTH_KEY_MGMT_CCKM); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2769 | case WPA_KEY_MGMT_WPA_NONE: |
| 2770 | return WPA_AUTH_KEY_MGMT_NONE; |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 2771 | case WPA_KEY_MGMT_IEEE8021X_SUITE_B: |
| 2772 | return RSN_AUTH_KEY_MGMT_802_1X_SUITE_B; |
Dmitry Shmidt | 807291d | 2015-01-27 13:40:23 -0800 | [diff] [blame] | 2773 | case WPA_KEY_MGMT_IEEE8021X_SUITE_B_192: |
| 2774 | return RSN_AUTH_KEY_MGMT_802_1X_SUITE_B_192; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2775 | default: |
| 2776 | return 0; |
| 2777 | } |
| 2778 | } |
| 2779 | |
| 2780 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2781 | #define RSN_SUITE "%02x-%02x-%02x-%d" |
| 2782 | #define RSN_SUITE_ARG(s) \ |
| 2783 | ((s) >> 24) & 0xff, ((s) >> 16) & 0xff, ((s) >> 8) & 0xff, (s) & 0xff |
| 2784 | |
| 2785 | /** |
| 2786 | * wpa_sm_get_mib - Dump text list of MIB entries |
| 2787 | * @sm: Pointer to WPA state machine data from wpa_sm_init() |
| 2788 | * @buf: Buffer for the list |
| 2789 | * @buflen: Length of the buffer |
| 2790 | * Returns: Number of bytes written to buffer |
| 2791 | * |
| 2792 | * This function is used fetch dot11 MIB variables. |
| 2793 | */ |
| 2794 | int wpa_sm_get_mib(struct wpa_sm *sm, char *buf, size_t buflen) |
| 2795 | { |
| 2796 | char pmkid_txt[PMKID_LEN * 2 + 1]; |
Hai Shalom | e21d4e8 | 2020-04-29 16:34:06 -0700 | [diff] [blame] | 2797 | bool rsna; |
| 2798 | int ret; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2799 | size_t len; |
| 2800 | |
| 2801 | if (sm->cur_pmksa) { |
| 2802 | wpa_snprintf_hex(pmkid_txt, sizeof(pmkid_txt), |
| 2803 | sm->cur_pmksa->pmkid, PMKID_LEN); |
| 2804 | } else |
| 2805 | pmkid_txt[0] = '\0'; |
| 2806 | |
Hai Shalom | e21d4e8 | 2020-04-29 16:34:06 -0700 | [diff] [blame] | 2807 | rsna = (wpa_key_mgmt_wpa_psk(sm->key_mgmt) || |
| 2808 | wpa_key_mgmt_wpa_ieee8021x(sm->key_mgmt)) && |
| 2809 | sm->proto == WPA_PROTO_RSN; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2810 | |
| 2811 | ret = os_snprintf(buf, buflen, |
| 2812 | "dot11RSNAOptionImplemented=TRUE\n" |
| 2813 | "dot11RSNAPreauthenticationImplemented=TRUE\n" |
| 2814 | "dot11RSNAEnabled=%s\n" |
| 2815 | "dot11RSNAPreauthenticationEnabled=%s\n" |
| 2816 | "dot11RSNAConfigVersion=%d\n" |
| 2817 | "dot11RSNAConfigPairwiseKeysSupported=5\n" |
| 2818 | "dot11RSNAConfigGroupCipherSize=%d\n" |
| 2819 | "dot11RSNAConfigPMKLifetime=%d\n" |
| 2820 | "dot11RSNAConfigPMKReauthThreshold=%d\n" |
| 2821 | "dot11RSNAConfigNumberOfPTKSAReplayCounters=1\n" |
| 2822 | "dot11RSNAConfigSATimeout=%d\n", |
| 2823 | rsna ? "TRUE" : "FALSE", |
| 2824 | rsna ? "TRUE" : "FALSE", |
| 2825 | RSN_VERSION, |
Dmitry Shmidt | 4530cfd | 2012-09-09 15:20:40 -0700 | [diff] [blame] | 2826 | wpa_cipher_key_len(sm->group_cipher) * 8, |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2827 | sm->dot11RSNAConfigPMKLifetime, |
| 2828 | sm->dot11RSNAConfigPMKReauthThreshold, |
| 2829 | sm->dot11RSNAConfigSATimeout); |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 2830 | if (os_snprintf_error(buflen, ret)) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2831 | return 0; |
| 2832 | len = ret; |
| 2833 | |
| 2834 | ret = os_snprintf( |
| 2835 | buf + len, buflen - len, |
| 2836 | "dot11RSNAAuthenticationSuiteSelected=" RSN_SUITE "\n" |
| 2837 | "dot11RSNAPairwiseCipherSelected=" RSN_SUITE "\n" |
| 2838 | "dot11RSNAGroupCipherSelected=" RSN_SUITE "\n" |
| 2839 | "dot11RSNAPMKIDUsed=%s\n" |
| 2840 | "dot11RSNAAuthenticationSuiteRequested=" RSN_SUITE "\n" |
| 2841 | "dot11RSNAPairwiseCipherRequested=" RSN_SUITE "\n" |
| 2842 | "dot11RSNAGroupCipherRequested=" RSN_SUITE "\n" |
| 2843 | "dot11RSNAConfigNumberOfGTKSAReplayCounters=0\n" |
| 2844 | "dot11RSNA4WayHandshakeFailures=%u\n", |
| 2845 | RSN_SUITE_ARG(wpa_key_mgmt_suite(sm)), |
Dmitry Shmidt | 4530cfd | 2012-09-09 15:20:40 -0700 | [diff] [blame] | 2846 | RSN_SUITE_ARG(wpa_cipher_to_suite(sm->proto, |
| 2847 | sm->pairwise_cipher)), |
| 2848 | RSN_SUITE_ARG(wpa_cipher_to_suite(sm->proto, |
| 2849 | sm->group_cipher)), |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2850 | pmkid_txt, |
| 2851 | RSN_SUITE_ARG(wpa_key_mgmt_suite(sm)), |
Dmitry Shmidt | 4530cfd | 2012-09-09 15:20:40 -0700 | [diff] [blame] | 2852 | RSN_SUITE_ARG(wpa_cipher_to_suite(sm->proto, |
| 2853 | sm->pairwise_cipher)), |
| 2854 | RSN_SUITE_ARG(wpa_cipher_to_suite(sm->proto, |
| 2855 | sm->group_cipher)), |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2856 | sm->dot11RSNA4WayHandshakeFailures); |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 2857 | if (!os_snprintf_error(buflen - len, ret)) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2858 | len += ret; |
| 2859 | |
| 2860 | return (int) len; |
| 2861 | } |
| 2862 | #endif /* CONFIG_CTRL_IFACE */ |
| 2863 | |
| 2864 | |
| 2865 | static void wpa_sm_pmksa_free_cb(struct rsn_pmksa_cache_entry *entry, |
Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 2866 | void *ctx, enum pmksa_free_reason reason) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2867 | { |
| 2868 | struct wpa_sm *sm = ctx; |
Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 2869 | int deauth = 0; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2870 | |
Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 2871 | wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "RSN: PMKSA cache entry free_cb: " |
| 2872 | MACSTR " reason=%d", MAC2STR(entry->aa), reason); |
| 2873 | |
| 2874 | if (sm->cur_pmksa == entry) { |
| 2875 | wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, |
| 2876 | "RSN: %s current PMKSA entry", |
| 2877 | reason == PMKSA_REPLACE ? "replaced" : "removed"); |
| 2878 | pmksa_cache_clear_current(sm); |
| 2879 | |
| 2880 | /* |
| 2881 | * If an entry is simply being replaced, there's no need to |
| 2882 | * deauthenticate because it will be immediately re-added. |
| 2883 | * This happens when EAP authentication is completed again |
| 2884 | * (reauth or failed PMKSA caching attempt). |
| 2885 | */ |
| 2886 | if (reason != PMKSA_REPLACE) |
| 2887 | deauth = 1; |
| 2888 | } |
| 2889 | |
| 2890 | if (reason == PMKSA_EXPIRE && |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2891 | (sm->pmk_len == entry->pmk_len && |
| 2892 | os_memcmp(sm->pmk, entry->pmk, sm->pmk_len) == 0)) { |
| 2893 | wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, |
Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 2894 | "RSN: deauthenticating due to expired PMK"); |
| 2895 | pmksa_cache_clear_current(sm); |
| 2896 | deauth = 1; |
| 2897 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2898 | |
Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 2899 | if (deauth) { |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame] | 2900 | sm->pmk_len = 0; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2901 | os_memset(sm->pmk, 0, sizeof(sm->pmk)); |
| 2902 | wpa_sm_deauthenticate(sm, WLAN_REASON_UNSPECIFIED); |
| 2903 | } |
| 2904 | } |
| 2905 | |
| 2906 | |
Hai Shalom | c1a2144 | 2022-02-04 13:43:00 -0800 | [diff] [blame] | 2907 | static bool wpa_sm_pmksa_is_current_cb(struct rsn_pmksa_cache_entry *entry, |
| 2908 | void *ctx) |
| 2909 | { |
| 2910 | struct wpa_sm *sm = ctx; |
| 2911 | |
| 2912 | return sm->cur_pmksa == entry; |
| 2913 | } |
| 2914 | |
| 2915 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2916 | /** |
| 2917 | * wpa_sm_init - Initialize WPA state machine |
| 2918 | * @ctx: Context pointer for callbacks; this needs to be an allocated buffer |
| 2919 | * Returns: Pointer to the allocated WPA state machine data |
| 2920 | * |
| 2921 | * This function is used to allocate a new WPA state machine and the returned |
| 2922 | * value is passed to all WPA state machine calls. |
| 2923 | */ |
| 2924 | struct wpa_sm * wpa_sm_init(struct wpa_sm_ctx *ctx) |
| 2925 | { |
| 2926 | struct wpa_sm *sm; |
| 2927 | |
| 2928 | sm = os_zalloc(sizeof(*sm)); |
| 2929 | if (sm == NULL) |
| 2930 | return NULL; |
| 2931 | dl_list_init(&sm->pmksa_candidates); |
| 2932 | sm->renew_snonce = 1; |
| 2933 | sm->ctx = ctx; |
| 2934 | |
| 2935 | sm->dot11RSNAConfigPMKLifetime = 43200; |
| 2936 | sm->dot11RSNAConfigPMKReauthThreshold = 70; |
| 2937 | sm->dot11RSNAConfigSATimeout = 60; |
| 2938 | |
Hai Shalom | c1a2144 | 2022-02-04 13:43:00 -0800 | [diff] [blame] | 2939 | sm->pmksa = pmksa_cache_init(wpa_sm_pmksa_free_cb, |
| 2940 | wpa_sm_pmksa_is_current_cb, sm, sm); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2941 | if (sm->pmksa == NULL) { |
| 2942 | wpa_msg(sm->ctx->msg_ctx, MSG_ERROR, |
| 2943 | "RSN: PMKSA cache initialization failed"); |
| 2944 | os_free(sm); |
| 2945 | return NULL; |
| 2946 | } |
| 2947 | |
| 2948 | return sm; |
| 2949 | } |
| 2950 | |
| 2951 | |
| 2952 | /** |
| 2953 | * wpa_sm_deinit - Deinitialize WPA state machine |
| 2954 | * @sm: Pointer to WPA state machine data from wpa_sm_init() |
| 2955 | */ |
| 2956 | void wpa_sm_deinit(struct wpa_sm *sm) |
| 2957 | { |
| 2958 | if (sm == NULL) |
| 2959 | return; |
| 2960 | pmksa_cache_deinit(sm->pmksa); |
| 2961 | eloop_cancel_timeout(wpa_sm_start_preauth, sm, NULL); |
| 2962 | eloop_cancel_timeout(wpa_sm_rekey_ptk, sm, NULL); |
| 2963 | os_free(sm->assoc_wpa_ie); |
Hai Shalom | c356592 | 2019-10-28 11:58:20 -0700 | [diff] [blame] | 2964 | os_free(sm->assoc_rsnxe); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2965 | os_free(sm->ap_wpa_ie); |
| 2966 | os_free(sm->ap_rsn_ie); |
Hai Shalom | c356592 | 2019-10-28 11:58:20 -0700 | [diff] [blame] | 2967 | os_free(sm->ap_rsnxe); |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 2968 | wpa_sm_drop_sa(sm); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2969 | os_free(sm->ctx); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2970 | #ifdef CONFIG_IEEE80211R |
| 2971 | os_free(sm->assoc_resp_ies); |
| 2972 | #endif /* CONFIG_IEEE80211R */ |
Dmitry Shmidt | 55840ad | 2015-12-14 12:45:46 -0800 | [diff] [blame] | 2973 | #ifdef CONFIG_TESTING_OPTIONS |
| 2974 | wpabuf_free(sm->test_assoc_ie); |
| 2975 | #endif /* CONFIG_TESTING_OPTIONS */ |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 2976 | #ifdef CONFIG_FILS_SK_PFS |
| 2977 | crypto_ecdh_deinit(sm->fils_ecdh); |
| 2978 | #endif /* CONFIG_FILS_SK_PFS */ |
| 2979 | #ifdef CONFIG_FILS |
| 2980 | wpabuf_free(sm->fils_ft_ies); |
| 2981 | #endif /* CONFIG_FILS */ |
| 2982 | #ifdef CONFIG_OWE |
| 2983 | crypto_ecdh_deinit(sm->owe_ecdh); |
| 2984 | #endif /* CONFIG_OWE */ |
Hai Shalom | 021b0b5 | 2019-04-10 11:17:58 -0700 | [diff] [blame] | 2985 | #ifdef CONFIG_DPP2 |
| 2986 | wpabuf_clear_free(sm->dpp_z); |
| 2987 | #endif /* CONFIG_DPP2 */ |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2988 | os_free(sm); |
| 2989 | } |
| 2990 | |
| 2991 | |
| 2992 | /** |
| 2993 | * wpa_sm_notify_assoc - Notify WPA state machine about association |
| 2994 | * @sm: Pointer to WPA state machine data from wpa_sm_init() |
| 2995 | * @bssid: The BSSID of the new association |
| 2996 | * |
| 2997 | * This function is called to let WPA state machine know that the connection |
| 2998 | * was established. |
| 2999 | */ |
| 3000 | void wpa_sm_notify_assoc(struct wpa_sm *sm, const u8 *bssid) |
| 3001 | { |
Mathy Vanhoef | 10bfd64 | 2017-07-12 16:03:24 +0200 | [diff] [blame] | 3002 | int clear_keys = 1; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3003 | |
| 3004 | if (sm == NULL) |
| 3005 | return; |
| 3006 | |
| 3007 | wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, |
| 3008 | "WPA: Association event - clear replay counter"); |
| 3009 | os_memcpy(sm->bssid, bssid, ETH_ALEN); |
| 3010 | os_memset(sm->rx_replay_counter, 0, WPA_REPLAY_COUNTER_LEN); |
| 3011 | sm->rx_replay_counter_set = 0; |
| 3012 | sm->renew_snonce = 1; |
| 3013 | if (os_memcmp(sm->preauth_bssid, bssid, ETH_ALEN) == 0) |
| 3014 | rsn_preauth_deinit(sm); |
| 3015 | |
| 3016 | #ifdef CONFIG_IEEE80211R |
| 3017 | if (wpa_ft_is_completed(sm)) { |
| 3018 | /* |
| 3019 | * Clear portValid to kick EAPOL state machine to re-enter |
| 3020 | * AUTHENTICATED state to get the EAPOL port Authorized. |
| 3021 | */ |
Hai Shalom | e21d4e8 | 2020-04-29 16:34:06 -0700 | [diff] [blame] | 3022 | eapol_sm_notify_portValid(sm->eapol, false); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3023 | wpa_supplicant_key_neg_complete(sm, sm->bssid, 1); |
| 3024 | |
| 3025 | /* Prepare for the next transition */ |
| 3026 | wpa_ft_prepare_auth_request(sm, NULL); |
| 3027 | |
Mathy Vanhoef | 10bfd64 | 2017-07-12 16:03:24 +0200 | [diff] [blame] | 3028 | clear_keys = 0; |
Hai Shalom | 81f62d8 | 2019-07-22 12:10:00 -0700 | [diff] [blame] | 3029 | sm->ft_protocol = 1; |
| 3030 | } else { |
| 3031 | sm->ft_protocol = 0; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3032 | } |
| 3033 | #endif /* CONFIG_IEEE80211R */ |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 3034 | #ifdef CONFIG_FILS |
| 3035 | if (sm->fils_completed) { |
| 3036 | /* |
| 3037 | * Clear portValid to kick EAPOL state machine to re-enter |
| 3038 | * AUTHENTICATED state to get the EAPOL port Authorized. |
| 3039 | */ |
| 3040 | wpa_supplicant_key_neg_complete(sm, sm->bssid, 1); |
Mathy Vanhoef | 10bfd64 | 2017-07-12 16:03:24 +0200 | [diff] [blame] | 3041 | clear_keys = 0; |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 3042 | } |
| 3043 | #endif /* CONFIG_FILS */ |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3044 | |
Mathy Vanhoef | 10bfd64 | 2017-07-12 16:03:24 +0200 | [diff] [blame] | 3045 | if (clear_keys) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3046 | /* |
| 3047 | * IEEE 802.11, 8.4.10: Delete PTK SA on (re)association if |
| 3048 | * this is not part of a Fast BSS Transition. |
| 3049 | */ |
| 3050 | wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Clear old PTK"); |
| 3051 | sm->ptk_set = 0; |
Dmitry Shmidt | 61593f0 | 2014-04-21 16:27:35 -0700 | [diff] [blame] | 3052 | os_memset(&sm->ptk, 0, sizeof(sm->ptk)); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3053 | sm->tptk_set = 0; |
Dmitry Shmidt | 61593f0 | 2014-04-21 16:27:35 -0700 | [diff] [blame] | 3054 | os_memset(&sm->tptk, 0, sizeof(sm->tptk)); |
Mathy Vanhoef | 10bfd64 | 2017-07-12 16:03:24 +0200 | [diff] [blame] | 3055 | os_memset(&sm->gtk, 0, sizeof(sm->gtk)); |
Jouni Malinen | 58c0e96 | 2017-10-01 12:12:24 +0300 | [diff] [blame] | 3056 | os_memset(&sm->gtk_wnm_sleep, 0, sizeof(sm->gtk_wnm_sleep)); |
Mathy Vanhoef | 10bfd64 | 2017-07-12 16:03:24 +0200 | [diff] [blame] | 3057 | os_memset(&sm->igtk, 0, sizeof(sm->igtk)); |
Jouni Malinen | 58c0e96 | 2017-10-01 12:12:24 +0300 | [diff] [blame] | 3058 | os_memset(&sm->igtk_wnm_sleep, 0, sizeof(sm->igtk_wnm_sleep)); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3059 | } |
| 3060 | |
| 3061 | #ifdef CONFIG_TDLS |
| 3062 | wpa_tdls_assoc(sm); |
| 3063 | #endif /* CONFIG_TDLS */ |
Dmitry Shmidt | cf32e60 | 2014-01-28 10:57:39 -0800 | [diff] [blame] | 3064 | |
| 3065 | #ifdef CONFIG_P2P |
| 3066 | os_memset(sm->p2p_ip_addr, 0, sizeof(sm->p2p_ip_addr)); |
| 3067 | #endif /* CONFIG_P2P */ |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 3068 | |
| 3069 | sm->keyidx_active = 0; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3070 | } |
| 3071 | |
| 3072 | |
| 3073 | /** |
| 3074 | * wpa_sm_notify_disassoc - Notify WPA state machine about disassociation |
| 3075 | * @sm: Pointer to WPA state machine data from wpa_sm_init() |
| 3076 | * |
| 3077 | * This function is called to let WPA state machine know that the connection |
| 3078 | * was lost. This will abort any existing pre-authentication session. |
| 3079 | */ |
| 3080 | void wpa_sm_notify_disassoc(struct wpa_sm *sm) |
| 3081 | { |
Dmitry Shmidt | 4dd28dc | 2015-03-10 11:21:43 -0700 | [diff] [blame] | 3082 | eloop_cancel_timeout(wpa_sm_start_preauth, sm, NULL); |
| 3083 | eloop_cancel_timeout(wpa_sm_rekey_ptk, sm, NULL); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3084 | rsn_preauth_deinit(sm); |
Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 3085 | pmksa_cache_clear_current(sm); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3086 | if (wpa_sm_get_state(sm) == WPA_4WAY_HANDSHAKE) |
| 3087 | sm->dot11RSNA4WayHandshakeFailures++; |
| 3088 | #ifdef CONFIG_TDLS |
| 3089 | wpa_tdls_disassoc(sm); |
| 3090 | #endif /* CONFIG_TDLS */ |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 3091 | #ifdef CONFIG_FILS |
| 3092 | sm->fils_completed = 0; |
| 3093 | #endif /* CONFIG_FILS */ |
Jouni Malinen | 4283f9e | 2017-09-22 12:06:37 +0300 | [diff] [blame] | 3094 | #ifdef CONFIG_IEEE80211R |
| 3095 | sm->ft_reassoc_completed = 0; |
Hai Shalom | 81f62d8 | 2019-07-22 12:10:00 -0700 | [diff] [blame] | 3096 | sm->ft_protocol = 0; |
Jouni Malinen | 4283f9e | 2017-09-22 12:06:37 +0300 | [diff] [blame] | 3097 | #endif /* CONFIG_IEEE80211R */ |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 3098 | |
| 3099 | /* Keys are not needed in the WPA state machine anymore */ |
| 3100 | wpa_sm_drop_sa(sm); |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 3101 | sm->keyidx_active = 0; |
Dmitry Shmidt | 4dd28dc | 2015-03-10 11:21:43 -0700 | [diff] [blame] | 3102 | |
| 3103 | sm->msg_3_of_4_ok = 0; |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 3104 | os_memset(sm->bssid, 0, ETH_ALEN); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3105 | } |
| 3106 | |
| 3107 | |
| 3108 | /** |
| 3109 | * wpa_sm_set_pmk - Set PMK |
| 3110 | * @sm: Pointer to WPA state machine data from wpa_sm_init() |
| 3111 | * @pmk: The new PMK |
| 3112 | * @pmk_len: The length of the new PMK in bytes |
Dmitry Shmidt | 57c2d39 | 2016-02-23 13:40:19 -0800 | [diff] [blame] | 3113 | * @pmkid: Calculated PMKID |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 3114 | * @bssid: AA to add into PMKSA cache or %NULL to not cache the PMK |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3115 | * |
| 3116 | * Configure the PMK for WPA state machine. |
| 3117 | */ |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 3118 | void wpa_sm_set_pmk(struct wpa_sm *sm, const u8 *pmk, size_t pmk_len, |
Dmitry Shmidt | 57c2d39 | 2016-02-23 13:40:19 -0800 | [diff] [blame] | 3119 | const u8 *pmkid, const u8 *bssid) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3120 | { |
| 3121 | if (sm == NULL) |
| 3122 | return; |
| 3123 | |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 3124 | wpa_hexdump_key(MSG_DEBUG, "WPA: Set PMK based on external data", |
| 3125 | pmk, pmk_len); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3126 | sm->pmk_len = pmk_len; |
| 3127 | os_memcpy(sm->pmk, pmk, pmk_len); |
| 3128 | |
| 3129 | #ifdef CONFIG_IEEE80211R |
| 3130 | /* Set XXKey to be PSK for FT key derivation */ |
| 3131 | sm->xxkey_len = pmk_len; |
| 3132 | os_memcpy(sm->xxkey, pmk, pmk_len); |
| 3133 | #endif /* CONFIG_IEEE80211R */ |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 3134 | |
| 3135 | if (bssid) { |
Hai Shalom | c1a2144 | 2022-02-04 13:43:00 -0800 | [diff] [blame] | 3136 | sm->cur_pmksa = pmksa_cache_add(sm->pmksa, pmk, pmk_len, |
| 3137 | pmkid, NULL, 0, bssid, |
| 3138 | sm->own_addr, |
| 3139 | sm->network_ctx, sm->key_mgmt, |
| 3140 | NULL); |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 3141 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3142 | } |
| 3143 | |
| 3144 | |
| 3145 | /** |
| 3146 | * wpa_sm_set_pmk_from_pmksa - Set PMK based on the current PMKSA |
| 3147 | * @sm: Pointer to WPA state machine data from wpa_sm_init() |
| 3148 | * |
| 3149 | * Take the PMK from the current PMKSA into use. If no PMKSA is active, the PMK |
| 3150 | * will be cleared. |
| 3151 | */ |
| 3152 | void wpa_sm_set_pmk_from_pmksa(struct wpa_sm *sm) |
| 3153 | { |
| 3154 | if (sm == NULL) |
| 3155 | return; |
| 3156 | |
| 3157 | if (sm->cur_pmksa) { |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 3158 | wpa_hexdump_key(MSG_DEBUG, |
| 3159 | "WPA: Set PMK based on current PMKSA", |
| 3160 | sm->cur_pmksa->pmk, sm->cur_pmksa->pmk_len); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3161 | sm->pmk_len = sm->cur_pmksa->pmk_len; |
| 3162 | os_memcpy(sm->pmk, sm->cur_pmksa->pmk, sm->pmk_len); |
| 3163 | } else { |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 3164 | wpa_printf(MSG_DEBUG, "WPA: No current PMKSA - clear PMK"); |
| 3165 | sm->pmk_len = 0; |
| 3166 | os_memset(sm->pmk, 0, PMK_LEN_MAX); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3167 | } |
| 3168 | } |
| 3169 | |
| 3170 | |
| 3171 | /** |
| 3172 | * wpa_sm_set_fast_reauth - Set fast reauthentication (EAP) enabled/disabled |
| 3173 | * @sm: Pointer to WPA state machine data from wpa_sm_init() |
| 3174 | * @fast_reauth: Whether fast reauthentication (EAP) is allowed |
| 3175 | */ |
| 3176 | void wpa_sm_set_fast_reauth(struct wpa_sm *sm, int fast_reauth) |
| 3177 | { |
| 3178 | if (sm) |
| 3179 | sm->fast_reauth = fast_reauth; |
| 3180 | } |
| 3181 | |
| 3182 | |
| 3183 | /** |
| 3184 | * wpa_sm_set_scard_ctx - Set context pointer for smartcard callbacks |
| 3185 | * @sm: Pointer to WPA state machine data from wpa_sm_init() |
| 3186 | * @scard_ctx: Context pointer for smartcard related callback functions |
| 3187 | */ |
| 3188 | void wpa_sm_set_scard_ctx(struct wpa_sm *sm, void *scard_ctx) |
| 3189 | { |
| 3190 | if (sm == NULL) |
| 3191 | return; |
| 3192 | sm->scard_ctx = scard_ctx; |
| 3193 | if (sm->preauth_eapol) |
| 3194 | eapol_sm_register_scard_ctx(sm->preauth_eapol, scard_ctx); |
| 3195 | } |
| 3196 | |
| 3197 | |
| 3198 | /** |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 3199 | * wpa_sm_set_config - Notification of current configuration change |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3200 | * @sm: Pointer to WPA state machine data from wpa_sm_init() |
| 3201 | * @config: Pointer to current network configuration |
| 3202 | * |
| 3203 | * Notify WPA state machine that configuration has changed. config will be |
| 3204 | * stored as a backpointer to network configuration. This can be %NULL to clear |
| 3205 | * the stored pointed. |
| 3206 | */ |
| 3207 | void wpa_sm_set_config(struct wpa_sm *sm, struct rsn_supp_config *config) |
| 3208 | { |
| 3209 | if (!sm) |
| 3210 | return; |
| 3211 | |
| 3212 | if (config) { |
| 3213 | sm->network_ctx = config->network_ctx; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3214 | sm->allowed_pairwise_cipher = config->allowed_pairwise_cipher; |
| 3215 | sm->proactive_key_caching = config->proactive_key_caching; |
| 3216 | sm->eap_workaround = config->eap_workaround; |
| 3217 | sm->eap_conf_ctx = config->eap_conf_ctx; |
| 3218 | if (config->ssid) { |
| 3219 | os_memcpy(sm->ssid, config->ssid, config->ssid_len); |
| 3220 | sm->ssid_len = config->ssid_len; |
| 3221 | } else |
| 3222 | sm->ssid_len = 0; |
| 3223 | sm->wpa_ptk_rekey = config->wpa_ptk_rekey; |
Dmitry Shmidt | cf32e60 | 2014-01-28 10:57:39 -0800 | [diff] [blame] | 3224 | sm->p2p = config->p2p; |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 3225 | sm->wpa_rsc_relaxation = config->wpa_rsc_relaxation; |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 3226 | sm->owe_ptk_workaround = config->owe_ptk_workaround; |
Hai Shalom | 6084025 | 2021-02-19 19:02:11 -0800 | [diff] [blame] | 3227 | sm->force_kdk_derivation = config->force_kdk_derivation; |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 3228 | #ifdef CONFIG_FILS |
| 3229 | if (config->fils_cache_id) { |
| 3230 | sm->fils_cache_id_set = 1; |
| 3231 | os_memcpy(sm->fils_cache_id, config->fils_cache_id, |
| 3232 | FILS_CACHE_ID_LEN); |
| 3233 | } else { |
| 3234 | sm->fils_cache_id_set = 0; |
| 3235 | } |
| 3236 | #endif /* CONFIG_FILS */ |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 3237 | sm->beacon_prot = config->beacon_prot; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3238 | } else { |
| 3239 | sm->network_ctx = NULL; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3240 | sm->allowed_pairwise_cipher = 0; |
| 3241 | sm->proactive_key_caching = 0; |
| 3242 | sm->eap_workaround = 0; |
| 3243 | sm->eap_conf_ctx = NULL; |
| 3244 | sm->ssid_len = 0; |
| 3245 | sm->wpa_ptk_rekey = 0; |
Dmitry Shmidt | cf32e60 | 2014-01-28 10:57:39 -0800 | [diff] [blame] | 3246 | sm->p2p = 0; |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 3247 | sm->wpa_rsc_relaxation = 0; |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 3248 | sm->owe_ptk_workaround = 0; |
| 3249 | sm->beacon_prot = 0; |
Hai Shalom | 6084025 | 2021-02-19 19:02:11 -0800 | [diff] [blame] | 3250 | sm->force_kdk_derivation = false; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3251 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3252 | } |
| 3253 | |
| 3254 | |
| 3255 | /** |
| 3256 | * wpa_sm_set_own_addr - Set own MAC address |
| 3257 | * @sm: Pointer to WPA state machine data from wpa_sm_init() |
| 3258 | * @addr: Own MAC address |
| 3259 | */ |
| 3260 | void wpa_sm_set_own_addr(struct wpa_sm *sm, const u8 *addr) |
| 3261 | { |
| 3262 | if (sm) |
| 3263 | os_memcpy(sm->own_addr, addr, ETH_ALEN); |
| 3264 | } |
| 3265 | |
| 3266 | |
| 3267 | /** |
| 3268 | * wpa_sm_set_ifname - Set network interface name |
| 3269 | * @sm: Pointer to WPA state machine data from wpa_sm_init() |
| 3270 | * @ifname: Interface name |
| 3271 | * @bridge_ifname: Optional bridge interface name (for pre-auth) |
| 3272 | */ |
| 3273 | void wpa_sm_set_ifname(struct wpa_sm *sm, const char *ifname, |
| 3274 | const char *bridge_ifname) |
| 3275 | { |
| 3276 | if (sm) { |
| 3277 | sm->ifname = ifname; |
| 3278 | sm->bridge_ifname = bridge_ifname; |
| 3279 | } |
| 3280 | } |
| 3281 | |
| 3282 | |
| 3283 | /** |
| 3284 | * wpa_sm_set_eapol - Set EAPOL state machine pointer |
| 3285 | * @sm: Pointer to WPA state machine data from wpa_sm_init() |
| 3286 | * @eapol: Pointer to EAPOL state machine allocated with eapol_sm_init() |
| 3287 | */ |
| 3288 | void wpa_sm_set_eapol(struct wpa_sm *sm, struct eapol_sm *eapol) |
| 3289 | { |
| 3290 | if (sm) |
| 3291 | sm->eapol = eapol; |
| 3292 | } |
| 3293 | |
| 3294 | |
| 3295 | /** |
| 3296 | * wpa_sm_set_param - Set WPA state machine parameters |
| 3297 | * @sm: Pointer to WPA state machine data from wpa_sm_init() |
| 3298 | * @param: Parameter field |
| 3299 | * @value: Parameter value |
| 3300 | * Returns: 0 on success, -1 on failure |
| 3301 | */ |
| 3302 | int wpa_sm_set_param(struct wpa_sm *sm, enum wpa_sm_conf_params param, |
| 3303 | unsigned int value) |
| 3304 | { |
| 3305 | int ret = 0; |
| 3306 | |
| 3307 | if (sm == NULL) |
| 3308 | return -1; |
| 3309 | |
| 3310 | switch (param) { |
| 3311 | case RSNA_PMK_LIFETIME: |
| 3312 | if (value > 0) |
| 3313 | sm->dot11RSNAConfigPMKLifetime = value; |
| 3314 | else |
| 3315 | ret = -1; |
| 3316 | break; |
| 3317 | case RSNA_PMK_REAUTH_THRESHOLD: |
| 3318 | if (value > 0 && value <= 100) |
| 3319 | sm->dot11RSNAConfigPMKReauthThreshold = value; |
| 3320 | else |
| 3321 | ret = -1; |
| 3322 | break; |
| 3323 | case RSNA_SA_TIMEOUT: |
| 3324 | if (value > 0) |
| 3325 | sm->dot11RSNAConfigSATimeout = value; |
| 3326 | else |
| 3327 | ret = -1; |
| 3328 | break; |
| 3329 | case WPA_PARAM_PROTO: |
| 3330 | sm->proto = value; |
| 3331 | break; |
| 3332 | case WPA_PARAM_PAIRWISE: |
| 3333 | sm->pairwise_cipher = value; |
| 3334 | break; |
| 3335 | case WPA_PARAM_GROUP: |
| 3336 | sm->group_cipher = value; |
| 3337 | break; |
| 3338 | case WPA_PARAM_KEY_MGMT: |
| 3339 | sm->key_mgmt = value; |
| 3340 | break; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3341 | case WPA_PARAM_MGMT_GROUP: |
| 3342 | sm->mgmt_group_cipher = value; |
| 3343 | break; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3344 | case WPA_PARAM_RSN_ENABLED: |
| 3345 | sm->rsn_enabled = value; |
| 3346 | break; |
| 3347 | case WPA_PARAM_MFP: |
| 3348 | sm->mfp = value; |
| 3349 | break; |
Hai Shalom | 74f70d4 | 2019-02-11 14:42:39 -0800 | [diff] [blame] | 3350 | case WPA_PARAM_OCV: |
| 3351 | sm->ocv = value; |
| 3352 | break; |
Hai Shalom | c356592 | 2019-10-28 11:58:20 -0700 | [diff] [blame] | 3353 | case WPA_PARAM_SAE_PWE: |
| 3354 | sm->sae_pwe = value; |
| 3355 | break; |
Hai Shalom | 899fcc7 | 2020-10-19 14:38:18 -0700 | [diff] [blame] | 3356 | case WPA_PARAM_SAE_PK: |
| 3357 | sm->sae_pk = value; |
| 3358 | break; |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 3359 | case WPA_PARAM_DENY_PTK0_REKEY: |
| 3360 | sm->wpa_deny_ptk0_rekey = value; |
| 3361 | break; |
| 3362 | case WPA_PARAM_EXT_KEY_ID: |
| 3363 | sm->ext_key_id = value; |
| 3364 | break; |
| 3365 | case WPA_PARAM_USE_EXT_KEY_ID: |
| 3366 | sm->use_ext_key_id = value; |
| 3367 | break; |
Hai Shalom | b755a2a | 2020-04-23 21:49:02 -0700 | [diff] [blame] | 3368 | #ifdef CONFIG_TESTING_OPTIONS |
| 3369 | case WPA_PARAM_FT_RSNXE_USED: |
| 3370 | sm->ft_rsnxe_used = value; |
| 3371 | break; |
Hai Shalom | 899fcc7 | 2020-10-19 14:38:18 -0700 | [diff] [blame] | 3372 | case WPA_PARAM_OCI_FREQ_EAPOL: |
| 3373 | sm->oci_freq_override_eapol = value; |
| 3374 | break; |
| 3375 | case WPA_PARAM_OCI_FREQ_EAPOL_G2: |
| 3376 | sm->oci_freq_override_eapol_g2 = value; |
| 3377 | break; |
| 3378 | case WPA_PARAM_OCI_FREQ_FT_ASSOC: |
| 3379 | sm->oci_freq_override_ft_assoc = value; |
| 3380 | break; |
| 3381 | case WPA_PARAM_OCI_FREQ_FILS_ASSOC: |
| 3382 | sm->oci_freq_override_fils_assoc = value; |
| 3383 | break; |
Hai Shalom | b755a2a | 2020-04-23 21:49:02 -0700 | [diff] [blame] | 3384 | #endif /* CONFIG_TESTING_OPTIONS */ |
Hai Shalom | 4fbc08f | 2020-05-18 12:37:00 -0700 | [diff] [blame] | 3385 | #ifdef CONFIG_DPP2 |
| 3386 | case WPA_PARAM_DPP_PFS: |
| 3387 | sm->dpp_pfs = value; |
| 3388 | break; |
| 3389 | #endif /* CONFIG_DPP2 */ |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3390 | default: |
| 3391 | break; |
| 3392 | } |
| 3393 | |
| 3394 | return ret; |
| 3395 | } |
| 3396 | |
| 3397 | |
| 3398 | /** |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3399 | * wpa_sm_get_status - Get WPA state machine |
| 3400 | * @sm: Pointer to WPA state machine data from wpa_sm_init() |
| 3401 | * @buf: Buffer for status information |
| 3402 | * @buflen: Maximum buffer length |
| 3403 | * @verbose: Whether to include verbose status information |
| 3404 | * Returns: Number of bytes written to buf. |
| 3405 | * |
| 3406 | * Query WPA state machine for status information. This function fills in |
| 3407 | * a text area with current status information. If the buffer (buf) is not |
| 3408 | * large enough, status information will be truncated to fit the buffer. |
| 3409 | */ |
| 3410 | int wpa_sm_get_status(struct wpa_sm *sm, char *buf, size_t buflen, |
| 3411 | int verbose) |
| 3412 | { |
| 3413 | char *pos = buf, *end = buf + buflen; |
| 3414 | int ret; |
| 3415 | |
| 3416 | ret = os_snprintf(pos, end - pos, |
| 3417 | "pairwise_cipher=%s\n" |
| 3418 | "group_cipher=%s\n" |
| 3419 | "key_mgmt=%s\n", |
| 3420 | wpa_cipher_txt(sm->pairwise_cipher), |
| 3421 | wpa_cipher_txt(sm->group_cipher), |
| 3422 | wpa_key_mgmt_txt(sm->key_mgmt, sm->proto)); |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 3423 | if (os_snprintf_error(end - pos, ret)) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3424 | return pos - buf; |
| 3425 | pos += ret; |
Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 3426 | |
Hai Shalom | 4fbc08f | 2020-05-18 12:37:00 -0700 | [diff] [blame] | 3427 | #ifdef CONFIG_DPP2 |
| 3428 | if (sm->key_mgmt == WPA_KEY_MGMT_DPP && sm->dpp_z) { |
| 3429 | ret = os_snprintf(pos, end - pos, "dpp_pfs=1\n"); |
| 3430 | if (os_snprintf_error(end - pos, ret)) |
| 3431 | return pos - buf; |
| 3432 | pos += ret; |
| 3433 | } |
| 3434 | #endif /* CONFIG_DPP2 */ |
| 3435 | |
Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 3436 | if (sm->mfp != NO_MGMT_FRAME_PROTECTION && sm->ap_rsn_ie) { |
| 3437 | struct wpa_ie_data rsn; |
| 3438 | if (wpa_parse_wpa_ie_rsn(sm->ap_rsn_ie, sm->ap_rsn_ie_len, &rsn) |
| 3439 | >= 0 && |
| 3440 | rsn.capabilities & (WPA_CAPABILITY_MFPR | |
| 3441 | WPA_CAPABILITY_MFPC)) { |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 3442 | ret = os_snprintf(pos, end - pos, "pmf=%d\n" |
| 3443 | "mgmt_group_cipher=%s\n", |
Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 3444 | (rsn.capabilities & |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 3445 | WPA_CAPABILITY_MFPR) ? 2 : 1, |
| 3446 | wpa_cipher_txt( |
| 3447 | sm->mgmt_group_cipher)); |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 3448 | if (os_snprintf_error(end - pos, ret)) |
Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 3449 | return pos - buf; |
| 3450 | pos += ret; |
| 3451 | } |
| 3452 | } |
| 3453 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3454 | return pos - buf; |
| 3455 | } |
| 3456 | |
| 3457 | |
Dmitry Shmidt | f7e0a99 | 2013-05-23 11:03:10 -0700 | [diff] [blame] | 3458 | int wpa_sm_pmf_enabled(struct wpa_sm *sm) |
| 3459 | { |
| 3460 | struct wpa_ie_data rsn; |
| 3461 | |
| 3462 | if (sm->mfp == NO_MGMT_FRAME_PROTECTION || !sm->ap_rsn_ie) |
| 3463 | return 0; |
| 3464 | |
| 3465 | if (wpa_parse_wpa_ie_rsn(sm->ap_rsn_ie, sm->ap_rsn_ie_len, &rsn) >= 0 && |
| 3466 | rsn.capabilities & (WPA_CAPABILITY_MFPR | WPA_CAPABILITY_MFPC)) |
| 3467 | return 1; |
| 3468 | |
| 3469 | return 0; |
| 3470 | } |
| 3471 | |
| 3472 | |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 3473 | int wpa_sm_ext_key_id(struct wpa_sm *sm) |
| 3474 | { |
| 3475 | return sm ? sm->ext_key_id : 0; |
| 3476 | } |
| 3477 | |
| 3478 | |
| 3479 | int wpa_sm_ext_key_id_active(struct wpa_sm *sm) |
| 3480 | { |
| 3481 | return sm ? sm->use_ext_key_id : 0; |
| 3482 | } |
| 3483 | |
| 3484 | |
Hai Shalom | 74f70d4 | 2019-02-11 14:42:39 -0800 | [diff] [blame] | 3485 | int wpa_sm_ocv_enabled(struct wpa_sm *sm) |
| 3486 | { |
| 3487 | struct wpa_ie_data rsn; |
| 3488 | |
| 3489 | if (!sm->ocv || !sm->ap_rsn_ie) |
| 3490 | return 0; |
| 3491 | |
| 3492 | return wpa_parse_wpa_ie_rsn(sm->ap_rsn_ie, sm->ap_rsn_ie_len, |
| 3493 | &rsn) >= 0 && |
| 3494 | (rsn.capabilities & WPA_CAPABILITY_OCVC); |
| 3495 | } |
| 3496 | |
| 3497 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3498 | /** |
| 3499 | * wpa_sm_set_assoc_wpa_ie_default - Generate own WPA/RSN IE from configuration |
| 3500 | * @sm: Pointer to WPA state machine data from wpa_sm_init() |
| 3501 | * @wpa_ie: Pointer to buffer for WPA/RSN IE |
| 3502 | * @wpa_ie_len: Pointer to the length of the wpa_ie buffer |
| 3503 | * Returns: 0 on success, -1 on failure |
| 3504 | */ |
| 3505 | int wpa_sm_set_assoc_wpa_ie_default(struct wpa_sm *sm, u8 *wpa_ie, |
| 3506 | size_t *wpa_ie_len) |
| 3507 | { |
| 3508 | int res; |
| 3509 | |
| 3510 | if (sm == NULL) |
| 3511 | return -1; |
| 3512 | |
Dmitry Shmidt | 55840ad | 2015-12-14 12:45:46 -0800 | [diff] [blame] | 3513 | #ifdef CONFIG_TESTING_OPTIONS |
| 3514 | if (sm->test_assoc_ie) { |
| 3515 | wpa_printf(MSG_DEBUG, |
| 3516 | "TESTING: Replace association WPA/RSN IE"); |
| 3517 | if (*wpa_ie_len < wpabuf_len(sm->test_assoc_ie)) |
| 3518 | return -1; |
| 3519 | os_memcpy(wpa_ie, wpabuf_head(sm->test_assoc_ie), |
| 3520 | wpabuf_len(sm->test_assoc_ie)); |
| 3521 | res = wpabuf_len(sm->test_assoc_ie); |
| 3522 | } else |
| 3523 | #endif /* CONFIG_TESTING_OPTIONS */ |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3524 | res = wpa_gen_wpa_ie(sm, wpa_ie, *wpa_ie_len); |
| 3525 | if (res < 0) |
| 3526 | return -1; |
| 3527 | *wpa_ie_len = res; |
| 3528 | |
| 3529 | wpa_hexdump(MSG_DEBUG, "WPA: Set own WPA IE default", |
| 3530 | wpa_ie, *wpa_ie_len); |
| 3531 | |
| 3532 | if (sm->assoc_wpa_ie == NULL) { |
| 3533 | /* |
| 3534 | * Make a copy of the WPA/RSN IE so that 4-Way Handshake gets |
| 3535 | * the correct version of the IE even if PMKSA caching is |
| 3536 | * aborted (which would remove PMKID from IE generation). |
| 3537 | */ |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 3538 | sm->assoc_wpa_ie = os_memdup(wpa_ie, *wpa_ie_len); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3539 | if (sm->assoc_wpa_ie == NULL) |
| 3540 | return -1; |
| 3541 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3542 | sm->assoc_wpa_ie_len = *wpa_ie_len; |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 3543 | } else { |
| 3544 | wpa_hexdump(MSG_DEBUG, |
| 3545 | "WPA: Leave previously set WPA IE default", |
| 3546 | sm->assoc_wpa_ie, sm->assoc_wpa_ie_len); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3547 | } |
| 3548 | |
| 3549 | return 0; |
| 3550 | } |
| 3551 | |
| 3552 | |
| 3553 | /** |
| 3554 | * wpa_sm_set_assoc_wpa_ie - Set own WPA/RSN IE from (Re)AssocReq |
| 3555 | * @sm: Pointer to WPA state machine data from wpa_sm_init() |
| 3556 | * @ie: Pointer to IE data (starting from id) |
| 3557 | * @len: IE length |
| 3558 | * Returns: 0 on success, -1 on failure |
| 3559 | * |
| 3560 | * Inform WPA state machine about the WPA/RSN IE used in (Re)Association |
| 3561 | * Request frame. The IE will be used to override the default value generated |
| 3562 | * with wpa_sm_set_assoc_wpa_ie_default(). |
| 3563 | */ |
| 3564 | int wpa_sm_set_assoc_wpa_ie(struct wpa_sm *sm, const u8 *ie, size_t len) |
| 3565 | { |
| 3566 | if (sm == NULL) |
| 3567 | return -1; |
| 3568 | |
| 3569 | os_free(sm->assoc_wpa_ie); |
| 3570 | if (ie == NULL || len == 0) { |
| 3571 | wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, |
| 3572 | "WPA: clearing own WPA/RSN IE"); |
| 3573 | sm->assoc_wpa_ie = NULL; |
| 3574 | sm->assoc_wpa_ie_len = 0; |
| 3575 | } else { |
| 3576 | wpa_hexdump(MSG_DEBUG, "WPA: set own WPA/RSN IE", ie, len); |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 3577 | sm->assoc_wpa_ie = os_memdup(ie, len); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3578 | if (sm->assoc_wpa_ie == NULL) |
| 3579 | return -1; |
| 3580 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3581 | sm->assoc_wpa_ie_len = len; |
| 3582 | } |
| 3583 | |
| 3584 | return 0; |
| 3585 | } |
| 3586 | |
| 3587 | |
| 3588 | /** |
Hai Shalom | c356592 | 2019-10-28 11:58:20 -0700 | [diff] [blame] | 3589 | * wpa_sm_set_assoc_rsnxe_default - Generate own RSNXE from configuration |
| 3590 | * @sm: Pointer to WPA state machine data from wpa_sm_init() |
| 3591 | * @rsnxe: Pointer to buffer for RSNXE |
| 3592 | * @rsnxe_len: Pointer to the length of the rsne buffer |
| 3593 | * Returns: 0 on success, -1 on failure |
| 3594 | */ |
| 3595 | int wpa_sm_set_assoc_rsnxe_default(struct wpa_sm *sm, u8 *rsnxe, |
| 3596 | size_t *rsnxe_len) |
| 3597 | { |
| 3598 | int res; |
| 3599 | |
| 3600 | if (!sm) |
| 3601 | return -1; |
| 3602 | |
| 3603 | res = wpa_gen_rsnxe(sm, rsnxe, *rsnxe_len); |
| 3604 | if (res < 0) |
| 3605 | return -1; |
| 3606 | *rsnxe_len = res; |
| 3607 | |
| 3608 | wpa_hexdump(MSG_DEBUG, "RSN: Set own RSNXE default", rsnxe, *rsnxe_len); |
| 3609 | |
| 3610 | if (sm->assoc_rsnxe) { |
| 3611 | wpa_hexdump(MSG_DEBUG, |
| 3612 | "RSN: Leave previously set RSNXE default", |
| 3613 | sm->assoc_rsnxe, sm->assoc_rsnxe_len); |
| 3614 | } else if (*rsnxe_len > 0) { |
| 3615 | /* |
| 3616 | * Make a copy of the RSNXE so that 4-Way Handshake gets the |
| 3617 | * correct version of the IE even if it gets changed. |
| 3618 | */ |
| 3619 | sm->assoc_rsnxe = os_memdup(rsnxe, *rsnxe_len); |
| 3620 | if (!sm->assoc_rsnxe) |
| 3621 | return -1; |
| 3622 | |
| 3623 | sm->assoc_rsnxe_len = *rsnxe_len; |
| 3624 | } |
| 3625 | |
| 3626 | return 0; |
| 3627 | } |
| 3628 | |
| 3629 | |
| 3630 | /** |
| 3631 | * wpa_sm_set_assoc_rsnxe - Set own RSNXE from (Re)AssocReq |
| 3632 | * @sm: Pointer to WPA state machine data from wpa_sm_init() |
| 3633 | * @ie: Pointer to IE data (starting from id) |
| 3634 | * @len: IE length |
| 3635 | * Returns: 0 on success, -1 on failure |
| 3636 | * |
| 3637 | * Inform WPA state machine about the RSNXE used in (Re)Association Request |
| 3638 | * frame. The IE will be used to override the default value generated |
| 3639 | * with wpa_sm_set_assoc_rsnxe_default(). |
| 3640 | */ |
| 3641 | int wpa_sm_set_assoc_rsnxe(struct wpa_sm *sm, const u8 *ie, size_t len) |
| 3642 | { |
| 3643 | if (!sm) |
| 3644 | return -1; |
| 3645 | |
| 3646 | os_free(sm->assoc_rsnxe); |
| 3647 | if (!ie || len == 0) { |
| 3648 | wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, |
| 3649 | "RSN: clearing own RSNXE"); |
| 3650 | sm->assoc_rsnxe = NULL; |
| 3651 | sm->assoc_rsnxe_len = 0; |
| 3652 | } else { |
| 3653 | wpa_hexdump(MSG_DEBUG, "RSN: set own RSNXE", ie, len); |
| 3654 | sm->assoc_rsnxe = os_memdup(ie, len); |
| 3655 | if (!sm->assoc_rsnxe) |
| 3656 | return -1; |
| 3657 | |
| 3658 | sm->assoc_rsnxe_len = len; |
| 3659 | } |
| 3660 | |
| 3661 | return 0; |
| 3662 | } |
| 3663 | |
| 3664 | |
| 3665 | /** |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3666 | * wpa_sm_set_ap_wpa_ie - Set AP WPA IE from Beacon/ProbeResp |
| 3667 | * @sm: Pointer to WPA state machine data from wpa_sm_init() |
| 3668 | * @ie: Pointer to IE data (starting from id) |
| 3669 | * @len: IE length |
| 3670 | * Returns: 0 on success, -1 on failure |
| 3671 | * |
| 3672 | * Inform WPA state machine about the WPA IE used in Beacon / Probe Response |
| 3673 | * frame. |
| 3674 | */ |
| 3675 | int wpa_sm_set_ap_wpa_ie(struct wpa_sm *sm, const u8 *ie, size_t len) |
| 3676 | { |
| 3677 | if (sm == NULL) |
| 3678 | return -1; |
| 3679 | |
| 3680 | os_free(sm->ap_wpa_ie); |
| 3681 | if (ie == NULL || len == 0) { |
| 3682 | wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, |
| 3683 | "WPA: clearing AP WPA IE"); |
| 3684 | sm->ap_wpa_ie = NULL; |
| 3685 | sm->ap_wpa_ie_len = 0; |
| 3686 | } else { |
| 3687 | wpa_hexdump(MSG_DEBUG, "WPA: set AP WPA IE", ie, len); |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 3688 | sm->ap_wpa_ie = os_memdup(ie, len); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3689 | if (sm->ap_wpa_ie == NULL) |
| 3690 | return -1; |
| 3691 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3692 | sm->ap_wpa_ie_len = len; |
| 3693 | } |
| 3694 | |
| 3695 | return 0; |
| 3696 | } |
| 3697 | |
| 3698 | |
| 3699 | /** |
| 3700 | * wpa_sm_set_ap_rsn_ie - Set AP RSN IE from Beacon/ProbeResp |
| 3701 | * @sm: Pointer to WPA state machine data from wpa_sm_init() |
| 3702 | * @ie: Pointer to IE data (starting from id) |
| 3703 | * @len: IE length |
| 3704 | * Returns: 0 on success, -1 on failure |
| 3705 | * |
| 3706 | * Inform WPA state machine about the RSN IE used in Beacon / Probe Response |
| 3707 | * frame. |
| 3708 | */ |
| 3709 | int wpa_sm_set_ap_rsn_ie(struct wpa_sm *sm, const u8 *ie, size_t len) |
| 3710 | { |
| 3711 | if (sm == NULL) |
| 3712 | return -1; |
| 3713 | |
| 3714 | os_free(sm->ap_rsn_ie); |
| 3715 | if (ie == NULL || len == 0) { |
| 3716 | wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, |
| 3717 | "WPA: clearing AP RSN IE"); |
| 3718 | sm->ap_rsn_ie = NULL; |
| 3719 | sm->ap_rsn_ie_len = 0; |
| 3720 | } else { |
| 3721 | wpa_hexdump(MSG_DEBUG, "WPA: set AP RSN IE", ie, len); |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 3722 | sm->ap_rsn_ie = os_memdup(ie, len); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3723 | if (sm->ap_rsn_ie == NULL) |
| 3724 | return -1; |
| 3725 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3726 | sm->ap_rsn_ie_len = len; |
| 3727 | } |
| 3728 | |
| 3729 | return 0; |
| 3730 | } |
| 3731 | |
| 3732 | |
| 3733 | /** |
Hai Shalom | c356592 | 2019-10-28 11:58:20 -0700 | [diff] [blame] | 3734 | * wpa_sm_set_ap_rsnxe - Set AP RSNXE from Beacon/ProbeResp |
| 3735 | * @sm: Pointer to WPA state machine data from wpa_sm_init() |
| 3736 | * @ie: Pointer to IE data (starting from id) |
| 3737 | * @len: IE length |
| 3738 | * Returns: 0 on success, -1 on failure |
| 3739 | * |
| 3740 | * Inform WPA state machine about the RSNXE used in Beacon / Probe Response |
| 3741 | * frame. |
| 3742 | */ |
| 3743 | int wpa_sm_set_ap_rsnxe(struct wpa_sm *sm, const u8 *ie, size_t len) |
| 3744 | { |
| 3745 | if (!sm) |
| 3746 | return -1; |
| 3747 | |
| 3748 | os_free(sm->ap_rsnxe); |
| 3749 | if (!ie || len == 0) { |
| 3750 | wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: clearing AP RSNXE"); |
| 3751 | sm->ap_rsnxe = NULL; |
| 3752 | sm->ap_rsnxe_len = 0; |
| 3753 | } else { |
| 3754 | wpa_hexdump(MSG_DEBUG, "WPA: set AP RSNXE", ie, len); |
| 3755 | sm->ap_rsnxe = os_memdup(ie, len); |
| 3756 | if (!sm->ap_rsnxe) |
| 3757 | return -1; |
| 3758 | |
| 3759 | sm->ap_rsnxe_len = len; |
| 3760 | } |
| 3761 | |
| 3762 | return 0; |
| 3763 | } |
| 3764 | |
| 3765 | |
| 3766 | /** |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3767 | * wpa_sm_parse_own_wpa_ie - Parse own WPA/RSN IE |
| 3768 | * @sm: Pointer to WPA state machine data from wpa_sm_init() |
| 3769 | * @data: Pointer to data area for parsing results |
| 3770 | * Returns: 0 on success, -1 if IE is not known, or -2 on parsing failure |
| 3771 | * |
| 3772 | * Parse the contents of the own WPA or RSN IE from (Re)AssocReq and write the |
| 3773 | * parsed data into data. |
| 3774 | */ |
| 3775 | int wpa_sm_parse_own_wpa_ie(struct wpa_sm *sm, struct wpa_ie_data *data) |
| 3776 | { |
| 3777 | if (sm == NULL) |
| 3778 | return -1; |
| 3779 | |
| 3780 | if (sm->assoc_wpa_ie == NULL) { |
| 3781 | wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, |
| 3782 | "WPA: No WPA/RSN IE available from association info"); |
| 3783 | return -1; |
| 3784 | } |
| 3785 | if (wpa_parse_wpa_ie(sm->assoc_wpa_ie, sm->assoc_wpa_ie_len, data)) |
| 3786 | return -2; |
| 3787 | return 0; |
| 3788 | } |
| 3789 | |
| 3790 | |
| 3791 | int wpa_sm_pmksa_cache_list(struct wpa_sm *sm, char *buf, size_t len) |
| 3792 | { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3793 | return pmksa_cache_list(sm->pmksa, buf, len); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3794 | } |
| 3795 | |
| 3796 | |
Dmitry Shmidt | 2933359 | 2017-01-09 12:27:11 -0800 | [diff] [blame] | 3797 | struct rsn_pmksa_cache_entry * wpa_sm_pmksa_cache_head(struct wpa_sm *sm) |
| 3798 | { |
| 3799 | return pmksa_cache_head(sm->pmksa); |
| 3800 | } |
| 3801 | |
| 3802 | |
| 3803 | struct rsn_pmksa_cache_entry * |
| 3804 | wpa_sm_pmksa_cache_add_entry(struct wpa_sm *sm, |
| 3805 | struct rsn_pmksa_cache_entry * entry) |
| 3806 | { |
| 3807 | return pmksa_cache_add_entry(sm->pmksa, entry); |
| 3808 | } |
| 3809 | |
| 3810 | |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 3811 | void wpa_sm_pmksa_cache_add(struct wpa_sm *sm, const u8 *pmk, size_t pmk_len, |
| 3812 | const u8 *pmkid, const u8 *bssid, |
| 3813 | const u8 *fils_cache_id) |
| 3814 | { |
| 3815 | sm->cur_pmksa = pmksa_cache_add(sm->pmksa, pmk, pmk_len, pmkid, NULL, 0, |
| 3816 | bssid, sm->own_addr, sm->network_ctx, |
| 3817 | sm->key_mgmt, fils_cache_id); |
| 3818 | } |
| 3819 | |
| 3820 | |
| 3821 | int wpa_sm_pmksa_exists(struct wpa_sm *sm, const u8 *bssid, |
| 3822 | const void *network_ctx) |
| 3823 | { |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame] | 3824 | return pmksa_cache_get(sm->pmksa, bssid, NULL, network_ctx, 0) != NULL; |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 3825 | } |
| 3826 | |
| 3827 | |
Hai Shalom | 6084025 | 2021-02-19 19:02:11 -0800 | [diff] [blame] | 3828 | struct rsn_pmksa_cache_entry * wpa_sm_pmksa_cache_get(struct wpa_sm *sm, |
| 3829 | const u8 *aa, |
| 3830 | const u8 *pmkid, |
| 3831 | const void *network_ctx, |
| 3832 | int akmp) |
| 3833 | { |
| 3834 | return pmksa_cache_get(sm->pmksa, aa, pmkid, network_ctx, akmp); |
| 3835 | } |
| 3836 | |
| 3837 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3838 | void wpa_sm_drop_sa(struct wpa_sm *sm) |
| 3839 | { |
| 3840 | wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Clear old PMK and PTK"); |
| 3841 | sm->ptk_set = 0; |
| 3842 | sm->tptk_set = 0; |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame] | 3843 | sm->pmk_len = 0; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3844 | os_memset(sm->pmk, 0, sizeof(sm->pmk)); |
| 3845 | os_memset(&sm->ptk, 0, sizeof(sm->ptk)); |
| 3846 | os_memset(&sm->tptk, 0, sizeof(sm->tptk)); |
Mathy Vanhoef | 10bfd64 | 2017-07-12 16:03:24 +0200 | [diff] [blame] | 3847 | os_memset(&sm->gtk, 0, sizeof(sm->gtk)); |
Jouni Malinen | 58c0e96 | 2017-10-01 12:12:24 +0300 | [diff] [blame] | 3848 | os_memset(&sm->gtk_wnm_sleep, 0, sizeof(sm->gtk_wnm_sleep)); |
Mathy Vanhoef | 10bfd64 | 2017-07-12 16:03:24 +0200 | [diff] [blame] | 3849 | os_memset(&sm->igtk, 0, sizeof(sm->igtk)); |
Jouni Malinen | 58c0e96 | 2017-10-01 12:12:24 +0300 | [diff] [blame] | 3850 | os_memset(&sm->igtk_wnm_sleep, 0, sizeof(sm->igtk_wnm_sleep)); |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 3851 | #ifdef CONFIG_IEEE80211R |
| 3852 | os_memset(sm->xxkey, 0, sizeof(sm->xxkey)); |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame] | 3853 | sm->xxkey_len = 0; |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 3854 | os_memset(sm->pmk_r0, 0, sizeof(sm->pmk_r0)); |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame] | 3855 | sm->pmk_r0_len = 0; |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 3856 | os_memset(sm->pmk_r1, 0, sizeof(sm->pmk_r1)); |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame] | 3857 | sm->pmk_r1_len = 0; |
Hai Shalom | 6084025 | 2021-02-19 19:02:11 -0800 | [diff] [blame] | 3858 | #ifdef CONFIG_PASN |
| 3859 | os_free(sm->pasn_r1kh); |
| 3860 | sm->pasn_r1kh = NULL; |
| 3861 | sm->n_pasn_r1kh = 0; |
| 3862 | #endif /* CONFIG_PASN */ |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 3863 | #endif /* CONFIG_IEEE80211R */ |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3864 | } |
| 3865 | |
| 3866 | |
| 3867 | int wpa_sm_has_ptk(struct wpa_sm *sm) |
| 3868 | { |
| 3869 | if (sm == NULL) |
| 3870 | return 0; |
| 3871 | return sm->ptk_set; |
| 3872 | } |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 3873 | |
| 3874 | |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 3875 | int wpa_sm_has_ptk_installed(struct wpa_sm *sm) |
| 3876 | { |
| 3877 | if (!sm) |
| 3878 | return 0; |
| 3879 | return sm->ptk.installed; |
| 3880 | } |
| 3881 | |
| 3882 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 3883 | void wpa_sm_update_replay_ctr(struct wpa_sm *sm, const u8 *replay_ctr) |
| 3884 | { |
| 3885 | os_memcpy(sm->rx_replay_counter, replay_ctr, WPA_REPLAY_COUNTER_LEN); |
| 3886 | } |
| 3887 | |
| 3888 | |
| 3889 | void wpa_sm_pmksa_cache_flush(struct wpa_sm *sm, void *network_ctx) |
| 3890 | { |
Hai Shalom | c1a2144 | 2022-02-04 13:43:00 -0800 | [diff] [blame] | 3891 | pmksa_cache_flush(sm->pmksa, network_ctx, NULL, 0, false); |
| 3892 | } |
| 3893 | |
| 3894 | |
| 3895 | void wpa_sm_external_pmksa_cache_flush(struct wpa_sm *sm, void *network_ctx) |
| 3896 | { |
| 3897 | pmksa_cache_flush(sm->pmksa, network_ctx, NULL, 0, true); |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 3898 | } |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 3899 | |
Andy Kuo | aba17c1 | 2022-04-14 16:05:31 +0800 | [diff] [blame^] | 3900 | #if defined(CONFIG_DRIVER_NL80211_BRCM) || defined(CONFIG_DRIVER_NL80211_SYNA) |
Mir Ali | 677e748 | 2020-11-12 19:49:02 +0530 | [diff] [blame] | 3901 | void wpa_sm_install_pmk(struct wpa_sm *sm) |
| 3902 | { |
| 3903 | /* In case the driver wants to handle re-assocs, pass it down the PMK. */ |
| 3904 | if (wpa_sm_set_key(sm, wpa_cipher_to_alg(sm->pairwise_cipher), NULL, 0, 0, NULL, 0, |
| 3905 | (u8*)sm->pmk, sm->pmk_len, KEY_FLAG_PMK) < 0) { |
| 3906 | wpa_hexdump(MSG_DEBUG, "PSK: Install PMK to the driver for driver reassociations", |
| 3907 | (u8*)sm->pmk, sm->pmk_len); |
| 3908 | /* No harm if the driver doesn't support. */ |
| 3909 | wpa_msg(sm->ctx->msg_ctx, MSG_DEBUG, |
| 3910 | "WPA: Failed to set PMK to the driver"); |
| 3911 | } |
| 3912 | } |
Mir Ali | eaaf04e | 2021-06-07 12:17:29 +0530 | [diff] [blame] | 3913 | |
| 3914 | void wpa_sm_notify_brcm_ft_reassoc(struct wpa_sm *sm, const u8 *bssid) |
| 3915 | { |
| 3916 | u8 buf[256]; |
| 3917 | struct wpa_supplicant *wpa_s = sm->ctx->ctx; |
| 3918 | |
| 3919 | wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, |
| 3920 | "WPA: BRCM FT Reassociation event - clear replay counter"); |
| 3921 | os_memcpy(sm->bssid, bssid, ETH_ALEN); |
| 3922 | os_memset(sm->rx_replay_counter, 0, WPA_REPLAY_COUNTER_LEN); |
| 3923 | sm->rx_replay_counter_set = 0; |
| 3924 | |
| 3925 | if (wpa_drv_driver_cmd(wpa_s, "GET_FTKEY", (char *)buf, sizeof(buf)) < 0) { |
| 3926 | wpa_msg(sm->ctx->msg_ctx, MSG_ERROR, |
| 3927 | "WPA: Failed to get FT KEY information"); |
| 3928 | wpa_supplicant_deauthenticate( |
| 3929 | wpa_s, WLAN_REASON_DEAUTH_LEAVING); |
| 3930 | |
| 3931 | } else { |
| 3932 | /* update kck and kek */ |
| 3933 | os_memcpy(sm->ptk.kck, buf, 16); |
| 3934 | os_memcpy(sm->ptk.kek, buf + 16, 16); |
| 3935 | wpa_msg(sm->ctx->msg_ctx, MSG_INFO, |
| 3936 | "WPA: Updated KCK and KEK after FT reassoc"); |
| 3937 | } |
| 3938 | } |
Andy Kuo | aba17c1 | 2022-04-14 16:05:31 +0800 | [diff] [blame^] | 3939 | #endif /* CONFIG_DRIVER_NL80211_BRCM || CONFIG_DRIVER_NL80211_SYNA */ |
| 3940 | |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 3941 | |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 3942 | #ifdef CONFIG_WNM |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 3943 | int wpa_wnmsleep_install_key(struct wpa_sm *sm, u8 subelem_id, u8 *buf) |
| 3944 | { |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 3945 | u16 keyinfo; |
| 3946 | u8 keylen; /* plaintext key len */ |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 3947 | u8 *key_rsc; |
| 3948 | |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 3949 | if (subelem_id == WNM_SLEEP_SUBELEM_GTK) { |
Dmitry Shmidt | 61593f0 | 2014-04-21 16:27:35 -0700 | [diff] [blame] | 3950 | struct wpa_gtk_data gd; |
| 3951 | |
| 3952 | os_memset(&gd, 0, sizeof(gd)); |
| 3953 | keylen = wpa_cipher_key_len(sm->group_cipher); |
| 3954 | gd.key_rsc_len = wpa_cipher_rsc_len(sm->group_cipher); |
| 3955 | gd.alg = wpa_cipher_to_alg(sm->group_cipher); |
| 3956 | if (gd.alg == WPA_ALG_NONE) { |
| 3957 | wpa_printf(MSG_DEBUG, "Unsupported group cipher suite"); |
| 3958 | return -1; |
| 3959 | } |
| 3960 | |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 3961 | key_rsc = buf + 5; |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 3962 | keyinfo = WPA_GET_LE16(buf + 2); |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 3963 | gd.gtk_len = keylen; |
| 3964 | if (gd.gtk_len != buf[4]) { |
| 3965 | wpa_printf(MSG_DEBUG, "GTK len mismatch len %d vs %d", |
| 3966 | gd.gtk_len, buf[4]); |
| 3967 | return -1; |
| 3968 | } |
| 3969 | gd.keyidx = keyinfo & 0x03; /* B0 - B1 */ |
| 3970 | gd.tx = wpa_supplicant_gtk_tx_bit_workaround( |
| 3971 | sm, !!(keyinfo & WPA_KEY_INFO_TXRX)); |
| 3972 | |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 3973 | os_memcpy(gd.gtk, buf + 13, gd.gtk_len); |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 3974 | |
| 3975 | wpa_hexdump_key(MSG_DEBUG, "Install GTK (WNM SLEEP)", |
| 3976 | gd.gtk, gd.gtk_len); |
Jouni Malinen | 58c0e96 | 2017-10-01 12:12:24 +0300 | [diff] [blame] | 3977 | if (wpa_supplicant_install_gtk(sm, &gd, key_rsc, 1)) { |
Hai Shalom | 81f62d8 | 2019-07-22 12:10:00 -0700 | [diff] [blame] | 3978 | forced_memzero(&gd, sizeof(gd)); |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 3979 | wpa_printf(MSG_DEBUG, "Failed to install the GTK in " |
| 3980 | "WNM mode"); |
| 3981 | return -1; |
| 3982 | } |
Hai Shalom | 81f62d8 | 2019-07-22 12:10:00 -0700 | [diff] [blame] | 3983 | forced_memzero(&gd, sizeof(gd)); |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 3984 | } else if (subelem_id == WNM_SLEEP_SUBELEM_IGTK) { |
Mathy Vanhoef | 10bfd64 | 2017-07-12 16:03:24 +0200 | [diff] [blame] | 3985 | const struct wpa_igtk_kde *igtk; |
Dmitry Shmidt | 61593f0 | 2014-04-21 16:27:35 -0700 | [diff] [blame] | 3986 | |
Mathy Vanhoef | 10bfd64 | 2017-07-12 16:03:24 +0200 | [diff] [blame] | 3987 | igtk = (const struct wpa_igtk_kde *) (buf + 2); |
Jouni Malinen | 58c0e96 | 2017-10-01 12:12:24 +0300 | [diff] [blame] | 3988 | if (wpa_supplicant_install_igtk(sm, igtk, 1) < 0) |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 3989 | return -1; |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 3990 | } else if (subelem_id == WNM_SLEEP_SUBELEM_BIGTK) { |
| 3991 | const struct wpa_bigtk_kde *bigtk; |
| 3992 | |
| 3993 | bigtk = (const struct wpa_bigtk_kde *) (buf + 2); |
| 3994 | if (sm->beacon_prot && |
| 3995 | wpa_supplicant_install_bigtk(sm, bigtk, 1) < 0) |
| 3996 | return -1; |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 3997 | } else { |
| 3998 | wpa_printf(MSG_DEBUG, "Unknown element id"); |
| 3999 | return -1; |
| 4000 | } |
| 4001 | |
| 4002 | return 0; |
| 4003 | } |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 4004 | #endif /* CONFIG_WNM */ |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 4005 | |
| 4006 | |
Dmitry Shmidt | cf32e60 | 2014-01-28 10:57:39 -0800 | [diff] [blame] | 4007 | #ifdef CONFIG_P2P |
| 4008 | |
| 4009 | int wpa_sm_get_p2p_ip_addr(struct wpa_sm *sm, u8 *buf) |
| 4010 | { |
| 4011 | if (sm == NULL || WPA_GET_BE32(sm->p2p_ip_addr) == 0) |
| 4012 | return -1; |
| 4013 | os_memcpy(buf, sm->p2p_ip_addr, 3 * 4); |
| 4014 | return 0; |
| 4015 | } |
| 4016 | |
| 4017 | #endif /* CONFIG_P2P */ |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 4018 | |
| 4019 | |
| 4020 | void wpa_sm_set_rx_replay_ctr(struct wpa_sm *sm, const u8 *rx_replay_counter) |
| 4021 | { |
| 4022 | if (rx_replay_counter == NULL) |
| 4023 | return; |
| 4024 | |
| 4025 | os_memcpy(sm->rx_replay_counter, rx_replay_counter, |
| 4026 | WPA_REPLAY_COUNTER_LEN); |
| 4027 | sm->rx_replay_counter_set = 1; |
| 4028 | wpa_printf(MSG_DEBUG, "Updated key replay counter"); |
| 4029 | } |
| 4030 | |
| 4031 | |
Dmitry Shmidt | 807291d | 2015-01-27 13:40:23 -0800 | [diff] [blame] | 4032 | void wpa_sm_set_ptk_kck_kek(struct wpa_sm *sm, |
| 4033 | const u8 *ptk_kck, size_t ptk_kck_len, |
| 4034 | const u8 *ptk_kek, size_t ptk_kek_len) |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 4035 | { |
Dmitry Shmidt | 807291d | 2015-01-27 13:40:23 -0800 | [diff] [blame] | 4036 | if (ptk_kck && ptk_kck_len <= WPA_KCK_MAX_LEN) { |
| 4037 | os_memcpy(sm->ptk.kck, ptk_kck, ptk_kck_len); |
| 4038 | sm->ptk.kck_len = ptk_kck_len; |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 4039 | wpa_printf(MSG_DEBUG, "Updated PTK KCK"); |
| 4040 | } |
Dmitry Shmidt | 807291d | 2015-01-27 13:40:23 -0800 | [diff] [blame] | 4041 | if (ptk_kek && ptk_kek_len <= WPA_KEK_MAX_LEN) { |
| 4042 | os_memcpy(sm->ptk.kek, ptk_kek, ptk_kek_len); |
| 4043 | sm->ptk.kek_len = ptk_kek_len; |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 4044 | wpa_printf(MSG_DEBUG, "Updated PTK KEK"); |
| 4045 | } |
| 4046 | sm->ptk_set = 1; |
| 4047 | } |
Dmitry Shmidt | 55840ad | 2015-12-14 12:45:46 -0800 | [diff] [blame] | 4048 | |
| 4049 | |
| 4050 | #ifdef CONFIG_TESTING_OPTIONS |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 4051 | |
Dmitry Shmidt | 55840ad | 2015-12-14 12:45:46 -0800 | [diff] [blame] | 4052 | void wpa_sm_set_test_assoc_ie(struct wpa_sm *sm, struct wpabuf *buf) |
| 4053 | { |
| 4054 | wpabuf_free(sm->test_assoc_ie); |
| 4055 | sm->test_assoc_ie = buf; |
| 4056 | } |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 4057 | |
| 4058 | |
| 4059 | const u8 * wpa_sm_get_anonce(struct wpa_sm *sm) |
| 4060 | { |
| 4061 | return sm->anonce; |
| 4062 | } |
| 4063 | |
Dmitry Shmidt | 55840ad | 2015-12-14 12:45:46 -0800 | [diff] [blame] | 4064 | #endif /* CONFIG_TESTING_OPTIONS */ |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 4065 | |
| 4066 | |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame] | 4067 | unsigned int wpa_sm_get_key_mgmt(struct wpa_sm *sm) |
| 4068 | { |
| 4069 | return sm->key_mgmt; |
| 4070 | } |
| 4071 | |
| 4072 | |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 4073 | #ifdef CONFIG_FILS |
| 4074 | |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 4075 | struct wpabuf * fils_build_auth(struct wpa_sm *sm, int dh_group, const u8 *md) |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 4076 | { |
| 4077 | struct wpabuf *buf = NULL; |
| 4078 | struct wpabuf *erp_msg; |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 4079 | struct wpabuf *pub = NULL; |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 4080 | |
| 4081 | erp_msg = eapol_sm_build_erp_reauth_start(sm->eapol); |
| 4082 | if (!erp_msg && !sm->cur_pmksa) { |
| 4083 | wpa_printf(MSG_DEBUG, |
| 4084 | "FILS: Neither ERP EAP-Initiate/Re-auth nor PMKSA cache entry is available - skip FILS"); |
| 4085 | goto fail; |
| 4086 | } |
| 4087 | |
| 4088 | wpa_printf(MSG_DEBUG, "FILS: Try to use FILS (erp=%d pmksa_cache=%d)", |
| 4089 | erp_msg != NULL, sm->cur_pmksa != NULL); |
| 4090 | |
| 4091 | sm->fils_completed = 0; |
| 4092 | |
| 4093 | if (!sm->assoc_wpa_ie) { |
| 4094 | wpa_printf(MSG_INFO, "FILS: No own RSN IE set for FILS"); |
| 4095 | goto fail; |
| 4096 | } |
| 4097 | |
| 4098 | if (random_get_bytes(sm->fils_nonce, FILS_NONCE_LEN) < 0 || |
| 4099 | random_get_bytes(sm->fils_session, FILS_SESSION_LEN) < 0) |
| 4100 | goto fail; |
| 4101 | |
| 4102 | wpa_hexdump(MSG_DEBUG, "FILS: Generated FILS Nonce", |
| 4103 | sm->fils_nonce, FILS_NONCE_LEN); |
| 4104 | wpa_hexdump(MSG_DEBUG, "FILS: Generated FILS Session", |
| 4105 | sm->fils_session, FILS_SESSION_LEN); |
| 4106 | |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 4107 | #ifdef CONFIG_FILS_SK_PFS |
| 4108 | sm->fils_dh_group = dh_group; |
| 4109 | if (dh_group) { |
| 4110 | crypto_ecdh_deinit(sm->fils_ecdh); |
| 4111 | sm->fils_ecdh = crypto_ecdh_init(dh_group); |
| 4112 | if (!sm->fils_ecdh) { |
| 4113 | wpa_printf(MSG_INFO, |
| 4114 | "FILS: Could not initialize ECDH with group %d", |
| 4115 | dh_group); |
| 4116 | goto fail; |
| 4117 | } |
| 4118 | pub = crypto_ecdh_get_pubkey(sm->fils_ecdh, 1); |
| 4119 | if (!pub) |
| 4120 | goto fail; |
| 4121 | wpa_hexdump_buf(MSG_DEBUG, "FILS: Element (DH public key)", |
| 4122 | pub); |
| 4123 | sm->fils_dh_elem_len = wpabuf_len(pub); |
| 4124 | } |
| 4125 | #endif /* CONFIG_FILS_SK_PFS */ |
| 4126 | |
| 4127 | buf = wpabuf_alloc(1000 + sm->assoc_wpa_ie_len + |
| 4128 | (pub ? wpabuf_len(pub) : 0)); |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 4129 | if (!buf) |
| 4130 | goto fail; |
| 4131 | |
| 4132 | /* Fields following the Authentication algorithm number field */ |
| 4133 | |
| 4134 | /* Authentication Transaction seq# */ |
| 4135 | wpabuf_put_le16(buf, 1); |
| 4136 | |
| 4137 | /* Status Code */ |
| 4138 | wpabuf_put_le16(buf, WLAN_STATUS_SUCCESS); |
| 4139 | |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 4140 | /* TODO: FILS PK */ |
| 4141 | #ifdef CONFIG_FILS_SK_PFS |
| 4142 | if (dh_group) { |
| 4143 | /* Finite Cyclic Group */ |
| 4144 | wpabuf_put_le16(buf, dh_group); |
| 4145 | /* Element */ |
| 4146 | wpabuf_put_buf(buf, pub); |
| 4147 | } |
| 4148 | #endif /* CONFIG_FILS_SK_PFS */ |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 4149 | |
| 4150 | /* RSNE */ |
| 4151 | wpa_hexdump(MSG_DEBUG, "FILS: RSNE in FILS Authentication frame", |
| 4152 | sm->assoc_wpa_ie, sm->assoc_wpa_ie_len); |
| 4153 | wpabuf_put_data(buf, sm->assoc_wpa_ie, sm->assoc_wpa_ie_len); |
| 4154 | |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 4155 | if (md) { |
| 4156 | /* MDE when using FILS for FT initial association */ |
| 4157 | struct rsn_mdie *mdie; |
| 4158 | |
| 4159 | wpabuf_put_u8(buf, WLAN_EID_MOBILITY_DOMAIN); |
| 4160 | wpabuf_put_u8(buf, sizeof(*mdie)); |
| 4161 | mdie = wpabuf_put(buf, sizeof(*mdie)); |
| 4162 | os_memcpy(mdie->mobility_domain, md, MOBILITY_DOMAIN_ID_LEN); |
| 4163 | mdie->ft_capab = 0; |
| 4164 | } |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 4165 | |
| 4166 | /* FILS Nonce */ |
| 4167 | wpabuf_put_u8(buf, WLAN_EID_EXTENSION); /* Element ID */ |
| 4168 | wpabuf_put_u8(buf, 1 + FILS_NONCE_LEN); /* Length */ |
| 4169 | /* Element ID Extension */ |
| 4170 | wpabuf_put_u8(buf, WLAN_EID_EXT_FILS_NONCE); |
| 4171 | wpabuf_put_data(buf, sm->fils_nonce, FILS_NONCE_LEN); |
| 4172 | |
| 4173 | /* FILS Session */ |
| 4174 | wpabuf_put_u8(buf, WLAN_EID_EXTENSION); /* Element ID */ |
| 4175 | wpabuf_put_u8(buf, 1 + FILS_SESSION_LEN); /* Length */ |
| 4176 | /* Element ID Extension */ |
| 4177 | wpabuf_put_u8(buf, WLAN_EID_EXT_FILS_SESSION); |
| 4178 | wpabuf_put_data(buf, sm->fils_session, FILS_SESSION_LEN); |
| 4179 | |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 4180 | /* Wrapped Data */ |
Paul Stewart | 092955c | 2017-02-06 09:13:09 -0800 | [diff] [blame] | 4181 | sm->fils_erp_pmkid_set = 0; |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 4182 | if (erp_msg) { |
| 4183 | wpabuf_put_u8(buf, WLAN_EID_EXTENSION); /* Element ID */ |
| 4184 | wpabuf_put_u8(buf, 1 + wpabuf_len(erp_msg)); /* Length */ |
| 4185 | /* Element ID Extension */ |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 4186 | wpabuf_put_u8(buf, WLAN_EID_EXT_WRAPPED_DATA); |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 4187 | wpabuf_put_buf(buf, erp_msg); |
Paul Stewart | 092955c | 2017-02-06 09:13:09 -0800 | [diff] [blame] | 4188 | /* Calculate pending PMKID here so that we do not need to |
| 4189 | * maintain a copy of the EAP-Initiate/Reauth message. */ |
| 4190 | if (fils_pmkid_erp(sm->key_mgmt, wpabuf_head(erp_msg), |
| 4191 | wpabuf_len(erp_msg), |
| 4192 | sm->fils_erp_pmkid) == 0) |
| 4193 | sm->fils_erp_pmkid_set = 1; |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 4194 | } |
| 4195 | |
| 4196 | wpa_hexdump_buf(MSG_DEBUG, "RSN: FILS fields for Authentication frame", |
| 4197 | buf); |
| 4198 | |
| 4199 | fail: |
| 4200 | wpabuf_free(erp_msg); |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 4201 | wpabuf_free(pub); |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 4202 | return buf; |
| 4203 | } |
| 4204 | |
| 4205 | |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 4206 | int fils_process_auth(struct wpa_sm *sm, const u8 *bssid, const u8 *data, |
| 4207 | size_t len) |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 4208 | { |
| 4209 | const u8 *pos, *end; |
| 4210 | struct ieee802_11_elems elems; |
| 4211 | struct wpa_ie_data rsn; |
| 4212 | int pmkid_match = 0; |
| 4213 | u8 ick[FILS_ICK_MAX_LEN]; |
| 4214 | size_t ick_len; |
| 4215 | int res; |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 4216 | struct wpabuf *dh_ss = NULL; |
| 4217 | const u8 *g_sta = NULL; |
| 4218 | size_t g_sta_len = 0; |
| 4219 | const u8 *g_ap = NULL; |
Hai Shalom | 6084025 | 2021-02-19 19:02:11 -0800 | [diff] [blame] | 4220 | size_t g_ap_len = 0, kdk_len; |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 4221 | struct wpabuf *pub = NULL; |
| 4222 | |
| 4223 | os_memcpy(sm->bssid, bssid, ETH_ALEN); |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 4224 | |
| 4225 | wpa_hexdump(MSG_DEBUG, "FILS: Authentication frame fields", |
| 4226 | data, len); |
| 4227 | pos = data; |
| 4228 | end = data + len; |
| 4229 | |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 4230 | /* TODO: FILS PK */ |
| 4231 | #ifdef CONFIG_FILS_SK_PFS |
| 4232 | if (sm->fils_dh_group) { |
| 4233 | u16 group; |
| 4234 | |
| 4235 | /* Using FILS PFS */ |
| 4236 | |
| 4237 | /* Finite Cyclic Group */ |
| 4238 | if (end - pos < 2) { |
| 4239 | wpa_printf(MSG_DEBUG, |
| 4240 | "FILS: No room for Finite Cyclic Group"); |
| 4241 | goto fail; |
| 4242 | } |
| 4243 | group = WPA_GET_LE16(pos); |
| 4244 | pos += 2; |
| 4245 | if (group != sm->fils_dh_group) { |
| 4246 | wpa_printf(MSG_DEBUG, |
| 4247 | "FILS: Unexpected change in Finite Cyclic Group: %u (expected %u)", |
| 4248 | group, sm->fils_dh_group); |
| 4249 | goto fail; |
| 4250 | } |
| 4251 | |
| 4252 | /* Element */ |
| 4253 | if ((size_t) (end - pos) < sm->fils_dh_elem_len) { |
| 4254 | wpa_printf(MSG_DEBUG, "FILS: No room for Element"); |
| 4255 | goto fail; |
| 4256 | } |
| 4257 | |
| 4258 | if (!sm->fils_ecdh) { |
| 4259 | wpa_printf(MSG_DEBUG, "FILS: No ECDH state available"); |
| 4260 | goto fail; |
| 4261 | } |
| 4262 | dh_ss = crypto_ecdh_set_peerkey(sm->fils_ecdh, 1, pos, |
| 4263 | sm->fils_dh_elem_len); |
| 4264 | if (!dh_ss) { |
| 4265 | wpa_printf(MSG_DEBUG, "FILS: ECDH operation failed"); |
| 4266 | goto fail; |
| 4267 | } |
| 4268 | wpa_hexdump_buf_key(MSG_DEBUG, "FILS: DH_SS", dh_ss); |
| 4269 | g_ap = pos; |
| 4270 | g_ap_len = sm->fils_dh_elem_len; |
| 4271 | pos += sm->fils_dh_elem_len; |
| 4272 | } |
| 4273 | #endif /* CONFIG_FILS_SK_PFS */ |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 4274 | |
| 4275 | wpa_hexdump(MSG_DEBUG, "FILS: Remaining IEs", pos, end - pos); |
| 4276 | if (ieee802_11_parse_elems(pos, end - pos, &elems, 1) == ParseFailed) { |
| 4277 | wpa_printf(MSG_DEBUG, "FILS: Could not parse elements"); |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 4278 | goto fail; |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 4279 | } |
| 4280 | |
| 4281 | /* RSNE */ |
| 4282 | wpa_hexdump(MSG_DEBUG, "FILS: RSN element", elems.rsn_ie, |
| 4283 | elems.rsn_ie_len); |
| 4284 | if (!elems.rsn_ie || |
| 4285 | wpa_parse_wpa_ie_rsn(elems.rsn_ie - 2, elems.rsn_ie_len + 2, |
| 4286 | &rsn) < 0) { |
| 4287 | wpa_printf(MSG_DEBUG, "FILS: No RSN element"); |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 4288 | goto fail; |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 4289 | } |
| 4290 | |
| 4291 | if (!elems.fils_nonce) { |
| 4292 | wpa_printf(MSG_DEBUG, "FILS: No FILS Nonce field"); |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 4293 | goto fail; |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 4294 | } |
| 4295 | os_memcpy(sm->fils_anonce, elems.fils_nonce, FILS_NONCE_LEN); |
| 4296 | wpa_hexdump(MSG_DEBUG, "FILS: ANonce", sm->fils_anonce, FILS_NONCE_LEN); |
| 4297 | |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame] | 4298 | #ifdef CONFIG_IEEE80211R |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 4299 | if (wpa_key_mgmt_ft(sm->key_mgmt)) { |
| 4300 | struct wpa_ft_ies parse; |
| 4301 | |
| 4302 | if (!elems.mdie || !elems.ftie) { |
| 4303 | wpa_printf(MSG_DEBUG, "FILS+FT: No MDE or FTE"); |
| 4304 | goto fail; |
| 4305 | } |
| 4306 | |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame] | 4307 | if (wpa_ft_parse_ies(pos, end - pos, &parse, |
| 4308 | wpa_key_mgmt_sha384(sm->key_mgmt)) < 0) { |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 4309 | wpa_printf(MSG_DEBUG, "FILS+FT: Failed to parse IEs"); |
| 4310 | goto fail; |
| 4311 | } |
| 4312 | |
| 4313 | if (!parse.r0kh_id) { |
| 4314 | wpa_printf(MSG_DEBUG, |
| 4315 | "FILS+FT: No R0KH-ID subelem in FTE"); |
| 4316 | goto fail; |
| 4317 | } |
| 4318 | os_memcpy(sm->r0kh_id, parse.r0kh_id, parse.r0kh_id_len); |
| 4319 | sm->r0kh_id_len = parse.r0kh_id_len; |
| 4320 | wpa_hexdump_ascii(MSG_DEBUG, "FILS+FT: R0KH-ID", |
| 4321 | sm->r0kh_id, sm->r0kh_id_len); |
| 4322 | |
| 4323 | if (!parse.r1kh_id) { |
| 4324 | wpa_printf(MSG_DEBUG, |
| 4325 | "FILS+FT: No R1KH-ID subelem in FTE"); |
| 4326 | goto fail; |
| 4327 | } |
| 4328 | os_memcpy(sm->r1kh_id, parse.r1kh_id, FT_R1KH_ID_LEN); |
| 4329 | wpa_hexdump(MSG_DEBUG, "FILS+FT: R1KH-ID", |
| 4330 | sm->r1kh_id, FT_R1KH_ID_LEN); |
| 4331 | |
| 4332 | /* TODO: Check MDE and FTE payload */ |
| 4333 | |
| 4334 | wpabuf_free(sm->fils_ft_ies); |
| 4335 | sm->fils_ft_ies = wpabuf_alloc(2 + elems.mdie_len + |
| 4336 | 2 + elems.ftie_len); |
| 4337 | if (!sm->fils_ft_ies) |
| 4338 | goto fail; |
| 4339 | wpabuf_put_data(sm->fils_ft_ies, elems.mdie - 2, |
| 4340 | 2 + elems.mdie_len); |
| 4341 | wpabuf_put_data(sm->fils_ft_ies, elems.ftie - 2, |
| 4342 | 2 + elems.ftie_len); |
| 4343 | } else { |
| 4344 | wpabuf_free(sm->fils_ft_ies); |
| 4345 | sm->fils_ft_ies = NULL; |
| 4346 | } |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame] | 4347 | #endif /* CONFIG_IEEE80211R */ |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 4348 | |
| 4349 | /* PMKID List */ |
| 4350 | if (rsn.pmkid && rsn.num_pmkid > 0) { |
| 4351 | wpa_hexdump(MSG_DEBUG, "FILS: PMKID List", |
| 4352 | rsn.pmkid, rsn.num_pmkid * PMKID_LEN); |
| 4353 | |
| 4354 | if (rsn.num_pmkid != 1) { |
| 4355 | wpa_printf(MSG_DEBUG, "FILS: Invalid PMKID selection"); |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 4356 | goto fail; |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 4357 | } |
| 4358 | wpa_hexdump(MSG_DEBUG, "FILS: PMKID", rsn.pmkid, PMKID_LEN); |
| 4359 | if (os_memcmp(sm->cur_pmksa->pmkid, rsn.pmkid, PMKID_LEN) != 0) |
| 4360 | { |
| 4361 | wpa_printf(MSG_DEBUG, "FILS: PMKID mismatch"); |
| 4362 | wpa_hexdump(MSG_DEBUG, "FILS: Expected PMKID", |
| 4363 | sm->cur_pmksa->pmkid, PMKID_LEN); |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 4364 | goto fail; |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 4365 | } |
| 4366 | wpa_printf(MSG_DEBUG, |
| 4367 | "FILS: Matching PMKID - continue using PMKSA caching"); |
| 4368 | pmkid_match = 1; |
| 4369 | } |
| 4370 | if (!pmkid_match && sm->cur_pmksa) { |
| 4371 | wpa_printf(MSG_DEBUG, |
| 4372 | "FILS: No PMKID match - cannot use cached PMKSA entry"); |
| 4373 | sm->cur_pmksa = NULL; |
| 4374 | } |
| 4375 | |
| 4376 | /* FILS Session */ |
| 4377 | if (!elems.fils_session) { |
| 4378 | wpa_printf(MSG_DEBUG, "FILS: No FILS Session element"); |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 4379 | goto fail; |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 4380 | } |
| 4381 | wpa_hexdump(MSG_DEBUG, "FILS: FILS Session", elems.fils_session, |
| 4382 | FILS_SESSION_LEN); |
| 4383 | if (os_memcmp(sm->fils_session, elems.fils_session, FILS_SESSION_LEN) |
| 4384 | != 0) { |
| 4385 | wpa_printf(MSG_DEBUG, "FILS: Session mismatch"); |
| 4386 | wpa_hexdump(MSG_DEBUG, "FILS: Expected FILS Session", |
| 4387 | sm->fils_session, FILS_SESSION_LEN); |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 4388 | goto fail; |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 4389 | } |
| 4390 | |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 4391 | /* Wrapped Data */ |
| 4392 | if (!sm->cur_pmksa && elems.wrapped_data) { |
Paul Stewart | 092955c | 2017-02-06 09:13:09 -0800 | [diff] [blame] | 4393 | u8 rmsk[ERP_MAX_KEY_LEN]; |
| 4394 | size_t rmsk_len; |
| 4395 | |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 4396 | wpa_hexdump(MSG_DEBUG, "FILS: Wrapped Data", |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 4397 | elems.wrapped_data, |
| 4398 | elems.wrapped_data_len); |
| 4399 | eapol_sm_process_erp_finish(sm->eapol, elems.wrapped_data, |
| 4400 | elems.wrapped_data_len); |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 4401 | if (eapol_sm_failed(sm->eapol)) |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 4402 | goto fail; |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 4403 | |
Paul Stewart | 092955c | 2017-02-06 09:13:09 -0800 | [diff] [blame] | 4404 | rmsk_len = ERP_MAX_KEY_LEN; |
| 4405 | res = eapol_sm_get_key(sm->eapol, rmsk, rmsk_len); |
| 4406 | if (res == PMK_LEN) { |
| 4407 | rmsk_len = PMK_LEN; |
| 4408 | res = eapol_sm_get_key(sm->eapol, rmsk, rmsk_len); |
| 4409 | } |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 4410 | if (res) |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 4411 | goto fail; |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 4412 | |
Paul Stewart | 092955c | 2017-02-06 09:13:09 -0800 | [diff] [blame] | 4413 | res = fils_rmsk_to_pmk(sm->key_mgmt, rmsk, rmsk_len, |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 4414 | sm->fils_nonce, sm->fils_anonce, |
| 4415 | dh_ss ? wpabuf_head(dh_ss) : NULL, |
| 4416 | dh_ss ? wpabuf_len(dh_ss) : 0, |
Paul Stewart | 092955c | 2017-02-06 09:13:09 -0800 | [diff] [blame] | 4417 | sm->pmk, &sm->pmk_len); |
Hai Shalom | 81f62d8 | 2019-07-22 12:10:00 -0700 | [diff] [blame] | 4418 | forced_memzero(rmsk, sizeof(rmsk)); |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 4419 | |
| 4420 | /* Don't use DHss in PTK derivation if PMKSA caching is not |
| 4421 | * used. */ |
| 4422 | wpabuf_clear_free(dh_ss); |
| 4423 | dh_ss = NULL; |
| 4424 | |
Dmitry Shmidt | ebd93af | 2017-02-21 13:40:44 -0800 | [diff] [blame] | 4425 | if (res) |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 4426 | goto fail; |
Paul Stewart | 092955c | 2017-02-06 09:13:09 -0800 | [diff] [blame] | 4427 | |
| 4428 | if (!sm->fils_erp_pmkid_set) { |
| 4429 | wpa_printf(MSG_DEBUG, "FILS: PMKID not available"); |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 4430 | goto fail; |
Paul Stewart | 092955c | 2017-02-06 09:13:09 -0800 | [diff] [blame] | 4431 | } |
| 4432 | wpa_hexdump(MSG_DEBUG, "FILS: PMKID", sm->fils_erp_pmkid, |
| 4433 | PMKID_LEN); |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 4434 | wpa_printf(MSG_DEBUG, "FILS: ERP processing succeeded - add PMKSA cache entry for the result"); |
Paul Stewart | 092955c | 2017-02-06 09:13:09 -0800 | [diff] [blame] | 4435 | sm->cur_pmksa = pmksa_cache_add(sm->pmksa, sm->pmk, sm->pmk_len, |
| 4436 | sm->fils_erp_pmkid, NULL, 0, |
| 4437 | sm->bssid, sm->own_addr, |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 4438 | sm->network_ctx, sm->key_mgmt, |
| 4439 | NULL); |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 4440 | } |
| 4441 | |
| 4442 | if (!sm->cur_pmksa) { |
| 4443 | wpa_printf(MSG_DEBUG, |
| 4444 | "FILS: No remaining options to continue FILS authentication"); |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 4445 | goto fail; |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 4446 | } |
| 4447 | |
Hai Shalom | 6084025 | 2021-02-19 19:02:11 -0800 | [diff] [blame] | 4448 | if (sm->force_kdk_derivation || |
Hai Shalom | c1a2144 | 2022-02-04 13:43:00 -0800 | [diff] [blame] | 4449 | (sm->secure_ltf && |
| 4450 | ieee802_11_rsnx_capab(sm->ap_rsnxe, WLAN_RSNX_CAPAB_SECURE_LTF))) |
Hai Shalom | 6084025 | 2021-02-19 19:02:11 -0800 | [diff] [blame] | 4451 | kdk_len = WPA_KDK_MAX_LEN; |
| 4452 | else |
| 4453 | kdk_len = 0; |
| 4454 | |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 4455 | if (fils_pmk_to_ptk(sm->pmk, sm->pmk_len, sm->own_addr, sm->bssid, |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 4456 | sm->fils_nonce, sm->fils_anonce, |
| 4457 | dh_ss ? wpabuf_head(dh_ss) : NULL, |
| 4458 | dh_ss ? wpabuf_len(dh_ss) : 0, |
| 4459 | &sm->ptk, ick, &ick_len, |
| 4460 | sm->key_mgmt, sm->pairwise_cipher, |
Hai Shalom | 6084025 | 2021-02-19 19:02:11 -0800 | [diff] [blame] | 4461 | sm->fils_ft, &sm->fils_ft_len, |
| 4462 | kdk_len) < 0) { |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 4463 | wpa_printf(MSG_DEBUG, "FILS: Failed to derive PTK"); |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 4464 | goto fail; |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 4465 | } |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 4466 | |
| 4467 | wpabuf_clear_free(dh_ss); |
| 4468 | dh_ss = NULL; |
| 4469 | |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 4470 | sm->ptk_set = 1; |
| 4471 | sm->tptk_set = 0; |
| 4472 | os_memset(&sm->tptk, 0, sizeof(sm->tptk)); |
| 4473 | |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 4474 | #ifdef CONFIG_FILS_SK_PFS |
| 4475 | if (sm->fils_dh_group) { |
| 4476 | if (!sm->fils_ecdh) { |
| 4477 | wpa_printf(MSG_INFO, "FILS: ECDH not initialized"); |
| 4478 | goto fail; |
| 4479 | } |
| 4480 | pub = crypto_ecdh_get_pubkey(sm->fils_ecdh, 1); |
| 4481 | if (!pub) |
| 4482 | goto fail; |
| 4483 | wpa_hexdump_buf(MSG_DEBUG, "FILS: gSTA", pub); |
| 4484 | g_sta = wpabuf_head(pub); |
| 4485 | g_sta_len = wpabuf_len(pub); |
| 4486 | if (!g_ap) { |
| 4487 | wpa_printf(MSG_INFO, "FILS: gAP not available"); |
| 4488 | goto fail; |
| 4489 | } |
| 4490 | wpa_hexdump(MSG_DEBUG, "FILS: gAP", g_ap, g_ap_len); |
| 4491 | } |
| 4492 | #endif /* CONFIG_FILS_SK_PFS */ |
| 4493 | |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 4494 | res = fils_key_auth_sk(ick, ick_len, sm->fils_nonce, |
| 4495 | sm->fils_anonce, sm->own_addr, sm->bssid, |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 4496 | g_sta, g_sta_len, g_ap, g_ap_len, |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 4497 | sm->key_mgmt, sm->fils_key_auth_sta, |
| 4498 | sm->fils_key_auth_ap, |
| 4499 | &sm->fils_key_auth_len); |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 4500 | wpabuf_free(pub); |
Hai Shalom | 81f62d8 | 2019-07-22 12:10:00 -0700 | [diff] [blame] | 4501 | forced_memzero(ick, sizeof(ick)); |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 4502 | return res; |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 4503 | fail: |
| 4504 | wpabuf_free(pub); |
| 4505 | wpabuf_clear_free(dh_ss); |
| 4506 | return -1; |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 4507 | } |
| 4508 | |
| 4509 | |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 4510 | #ifdef CONFIG_IEEE80211R |
| 4511 | static int fils_ft_build_assoc_req_rsne(struct wpa_sm *sm, struct wpabuf *buf) |
| 4512 | { |
| 4513 | struct rsn_ie_hdr *rsnie; |
| 4514 | u16 capab; |
| 4515 | u8 *pos; |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame] | 4516 | int use_sha384 = wpa_key_mgmt_sha384(sm->key_mgmt); |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 4517 | |
| 4518 | /* RSNIE[PMKR0Name/PMKR1Name] */ |
| 4519 | rsnie = wpabuf_put(buf, sizeof(*rsnie)); |
| 4520 | rsnie->elem_id = WLAN_EID_RSN; |
| 4521 | WPA_PUT_LE16(rsnie->version, RSN_VERSION); |
| 4522 | |
| 4523 | /* Group Suite Selector */ |
| 4524 | if (!wpa_cipher_valid_group(sm->group_cipher)) { |
| 4525 | wpa_printf(MSG_WARNING, "FT: Invalid group cipher (%d)", |
| 4526 | sm->group_cipher); |
| 4527 | return -1; |
| 4528 | } |
| 4529 | pos = wpabuf_put(buf, RSN_SELECTOR_LEN); |
| 4530 | RSN_SELECTOR_PUT(pos, wpa_cipher_to_suite(WPA_PROTO_RSN, |
| 4531 | sm->group_cipher)); |
| 4532 | |
| 4533 | /* Pairwise Suite Count */ |
| 4534 | wpabuf_put_le16(buf, 1); |
| 4535 | |
| 4536 | /* Pairwise Suite List */ |
| 4537 | if (!wpa_cipher_valid_pairwise(sm->pairwise_cipher)) { |
| 4538 | wpa_printf(MSG_WARNING, "FT: Invalid pairwise cipher (%d)", |
| 4539 | sm->pairwise_cipher); |
| 4540 | return -1; |
| 4541 | } |
| 4542 | pos = wpabuf_put(buf, RSN_SELECTOR_LEN); |
| 4543 | RSN_SELECTOR_PUT(pos, wpa_cipher_to_suite(WPA_PROTO_RSN, |
| 4544 | sm->pairwise_cipher)); |
| 4545 | |
| 4546 | /* Authenticated Key Management Suite Count */ |
| 4547 | wpabuf_put_le16(buf, 1); |
| 4548 | |
| 4549 | /* Authenticated Key Management Suite List */ |
| 4550 | pos = wpabuf_put(buf, RSN_SELECTOR_LEN); |
| 4551 | if (sm->key_mgmt == WPA_KEY_MGMT_FT_FILS_SHA256) |
| 4552 | RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_FT_FILS_SHA256); |
| 4553 | else if (sm->key_mgmt == WPA_KEY_MGMT_FT_FILS_SHA384) |
| 4554 | RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_FT_FILS_SHA384); |
| 4555 | else { |
| 4556 | wpa_printf(MSG_WARNING, |
| 4557 | "FILS+FT: Invalid key management type (%d)", |
| 4558 | sm->key_mgmt); |
| 4559 | return -1; |
| 4560 | } |
| 4561 | |
| 4562 | /* RSN Capabilities */ |
| 4563 | capab = 0; |
Hai Shalom | c356592 | 2019-10-28 11:58:20 -0700 | [diff] [blame] | 4564 | if (sm->mfp) |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 4565 | capab |= WPA_CAPABILITY_MFPC; |
Hai Shalom | c356592 | 2019-10-28 11:58:20 -0700 | [diff] [blame] | 4566 | if (sm->mfp == 2) |
| 4567 | capab |= WPA_CAPABILITY_MFPR; |
Hai Shalom | 74f70d4 | 2019-02-11 14:42:39 -0800 | [diff] [blame] | 4568 | if (sm->ocv) |
| 4569 | capab |= WPA_CAPABILITY_OCVC; |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 4570 | if (sm->ext_key_id) |
| 4571 | capab |= WPA_CAPABILITY_EXT_KEY_ID_FOR_UNICAST; |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 4572 | wpabuf_put_le16(buf, capab); |
| 4573 | |
| 4574 | /* PMKID Count */ |
| 4575 | wpabuf_put_le16(buf, 1); |
| 4576 | |
| 4577 | /* PMKID List [PMKR1Name] */ |
| 4578 | wpa_hexdump_key(MSG_DEBUG, "FILS+FT: XXKey (FILS-FT)", |
| 4579 | sm->fils_ft, sm->fils_ft_len); |
| 4580 | wpa_hexdump_ascii(MSG_DEBUG, "FILS+FT: SSID", sm->ssid, sm->ssid_len); |
| 4581 | wpa_hexdump(MSG_DEBUG, "FILS+FT: MDID", |
| 4582 | sm->mobility_domain, MOBILITY_DOMAIN_ID_LEN); |
| 4583 | wpa_hexdump_ascii(MSG_DEBUG, "FILS+FT: R0KH-ID", |
| 4584 | sm->r0kh_id, sm->r0kh_id_len); |
| 4585 | if (wpa_derive_pmk_r0(sm->fils_ft, sm->fils_ft_len, sm->ssid, |
| 4586 | sm->ssid_len, sm->mobility_domain, |
| 4587 | sm->r0kh_id, sm->r0kh_id_len, sm->own_addr, |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame] | 4588 | sm->pmk_r0, sm->pmk_r0_name, use_sha384) < 0) { |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 4589 | wpa_printf(MSG_WARNING, "FILS+FT: Could not derive PMK-R0"); |
| 4590 | return -1; |
| 4591 | } |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame] | 4592 | sm->pmk_r0_len = use_sha384 ? SHA384_MAC_LEN : PMK_LEN; |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 4593 | wpa_printf(MSG_DEBUG, "FILS+FT: R1KH-ID: " MACSTR, |
| 4594 | MAC2STR(sm->r1kh_id)); |
| 4595 | pos = wpabuf_put(buf, WPA_PMK_NAME_LEN); |
| 4596 | if (wpa_derive_pmk_r1_name(sm->pmk_r0_name, sm->r1kh_id, sm->own_addr, |
Hai Shalom | 021b0b5 | 2019-04-10 11:17:58 -0700 | [diff] [blame] | 4597 | sm->pmk_r1_name, use_sha384) < 0) { |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 4598 | wpa_printf(MSG_WARNING, "FILS+FT: Could not derive PMKR1Name"); |
| 4599 | return -1; |
| 4600 | } |
Hai Shalom | 021b0b5 | 2019-04-10 11:17:58 -0700 | [diff] [blame] | 4601 | os_memcpy(pos, sm->pmk_r1_name, WPA_PMK_NAME_LEN); |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 4602 | |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 4603 | if (sm->mgmt_group_cipher == WPA_CIPHER_AES_128_CMAC) { |
| 4604 | /* Management Group Cipher Suite */ |
| 4605 | pos = wpabuf_put(buf, RSN_SELECTOR_LEN); |
| 4606 | RSN_SELECTOR_PUT(pos, RSN_CIPHER_SUITE_AES_128_CMAC); |
| 4607 | } |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 4608 | |
| 4609 | rsnie->len = ((u8 *) wpabuf_put(buf, 0) - (u8 *) rsnie) - 2; |
| 4610 | return 0; |
| 4611 | } |
| 4612 | #endif /* CONFIG_IEEE80211R */ |
| 4613 | |
| 4614 | |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 4615 | struct wpabuf * fils_build_assoc_req(struct wpa_sm *sm, const u8 **kek, |
| 4616 | size_t *kek_len, const u8 **snonce, |
Paul Stewart | 092955c | 2017-02-06 09:13:09 -0800 | [diff] [blame] | 4617 | const u8 **anonce, |
| 4618 | const struct wpabuf **hlp, |
| 4619 | unsigned int num_hlp) |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 4620 | { |
| 4621 | struct wpabuf *buf; |
Paul Stewart | 092955c | 2017-02-06 09:13:09 -0800 | [diff] [blame] | 4622 | size_t len; |
| 4623 | unsigned int i; |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 4624 | |
Paul Stewart | 092955c | 2017-02-06 09:13:09 -0800 | [diff] [blame] | 4625 | len = 1000; |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 4626 | #ifdef CONFIG_IEEE80211R |
| 4627 | if (sm->fils_ft_ies) |
| 4628 | len += wpabuf_len(sm->fils_ft_ies); |
| 4629 | if (wpa_key_mgmt_ft(sm->key_mgmt)) |
| 4630 | len += 256; |
| 4631 | #endif /* CONFIG_IEEE80211R */ |
Paul Stewart | 092955c | 2017-02-06 09:13:09 -0800 | [diff] [blame] | 4632 | for (i = 0; hlp && i < num_hlp; i++) |
| 4633 | len += 10 + wpabuf_len(hlp[i]); |
| 4634 | buf = wpabuf_alloc(len); |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 4635 | if (!buf) |
| 4636 | return NULL; |
| 4637 | |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 4638 | #ifdef CONFIG_IEEE80211R |
| 4639 | if (wpa_key_mgmt_ft(sm->key_mgmt) && sm->fils_ft_ies) { |
| 4640 | /* MDE and FTE when using FILS+FT */ |
| 4641 | wpabuf_put_buf(buf, sm->fils_ft_ies); |
| 4642 | /* RSNE with PMKR1Name in PMKID field */ |
| 4643 | if (fils_ft_build_assoc_req_rsne(sm, buf) < 0) { |
| 4644 | wpabuf_free(buf); |
| 4645 | return NULL; |
| 4646 | } |
| 4647 | } |
| 4648 | #endif /* CONFIG_IEEE80211R */ |
| 4649 | |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 4650 | /* FILS Session */ |
| 4651 | wpabuf_put_u8(buf, WLAN_EID_EXTENSION); /* Element ID */ |
| 4652 | wpabuf_put_u8(buf, 1 + FILS_SESSION_LEN); /* Length */ |
| 4653 | /* Element ID Extension */ |
| 4654 | wpabuf_put_u8(buf, WLAN_EID_EXT_FILS_SESSION); |
| 4655 | wpabuf_put_data(buf, sm->fils_session, FILS_SESSION_LEN); |
| 4656 | |
| 4657 | /* Everything after FILS Session element gets encrypted in the driver |
| 4658 | * with KEK. The buffer returned from here is the plaintext version. */ |
| 4659 | |
| 4660 | /* TODO: FILS Public Key */ |
| 4661 | |
| 4662 | /* FILS Key Confirm */ |
| 4663 | wpabuf_put_u8(buf, WLAN_EID_EXTENSION); /* Element ID */ |
| 4664 | wpabuf_put_u8(buf, 1 + sm->fils_key_auth_len); /* Length */ |
| 4665 | /* Element ID Extension */ |
| 4666 | wpabuf_put_u8(buf, WLAN_EID_EXT_FILS_KEY_CONFIRM); |
| 4667 | wpabuf_put_data(buf, sm->fils_key_auth_sta, sm->fils_key_auth_len); |
| 4668 | |
Paul Stewart | 092955c | 2017-02-06 09:13:09 -0800 | [diff] [blame] | 4669 | /* FILS HLP Container */ |
| 4670 | for (i = 0; hlp && i < num_hlp; i++) { |
| 4671 | const u8 *pos = wpabuf_head(hlp[i]); |
| 4672 | size_t left = wpabuf_len(hlp[i]); |
| 4673 | |
| 4674 | wpabuf_put_u8(buf, WLAN_EID_EXTENSION); /* Element ID */ |
| 4675 | if (left <= 254) |
| 4676 | len = 1 + left; |
| 4677 | else |
| 4678 | len = 255; |
| 4679 | wpabuf_put_u8(buf, len); /* Length */ |
| 4680 | /* Element ID Extension */ |
| 4681 | wpabuf_put_u8(buf, WLAN_EID_EXT_FILS_HLP_CONTAINER); |
| 4682 | /* Destination MAC Address, Source MAC Address, HLP Packet. |
| 4683 | * HLP Packet is in MSDU format (i.e., included the LLC/SNAP |
| 4684 | * header when LPD is used). */ |
| 4685 | wpabuf_put_data(buf, pos, len - 1); |
| 4686 | pos += len - 1; |
| 4687 | left -= len - 1; |
| 4688 | while (left) { |
| 4689 | wpabuf_put_u8(buf, WLAN_EID_FRAGMENT); |
| 4690 | len = left > 255 ? 255 : left; |
| 4691 | wpabuf_put_u8(buf, len); |
| 4692 | wpabuf_put_data(buf, pos, len); |
| 4693 | pos += len; |
| 4694 | left -= len; |
| 4695 | } |
| 4696 | } |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 4697 | |
| 4698 | /* TODO: FILS IP Address Assignment */ |
| 4699 | |
Hai Shalom | 74f70d4 | 2019-02-11 14:42:39 -0800 | [diff] [blame] | 4700 | #ifdef CONFIG_OCV |
| 4701 | if (wpa_sm_ocv_enabled(sm)) { |
| 4702 | struct wpa_channel_info ci; |
| 4703 | u8 *pos; |
| 4704 | |
| 4705 | if (wpa_sm_channel_info(sm, &ci) != 0) { |
| 4706 | wpa_printf(MSG_WARNING, |
| 4707 | "FILS: Failed to get channel info for OCI element"); |
| 4708 | wpabuf_free(buf); |
| 4709 | return NULL; |
| 4710 | } |
Hai Shalom | 899fcc7 | 2020-10-19 14:38:18 -0700 | [diff] [blame] | 4711 | #ifdef CONFIG_TESTING_OPTIONS |
| 4712 | if (sm->oci_freq_override_fils_assoc) { |
| 4713 | wpa_printf(MSG_INFO, |
| 4714 | "TEST: Override OCI KDE frequency %d -> %d MHz", |
| 4715 | ci.frequency, |
| 4716 | sm->oci_freq_override_fils_assoc); |
| 4717 | ci.frequency = sm->oci_freq_override_fils_assoc; |
| 4718 | } |
| 4719 | #endif /* CONFIG_TESTING_OPTIONS */ |
Hai Shalom | 74f70d4 | 2019-02-11 14:42:39 -0800 | [diff] [blame] | 4720 | |
| 4721 | pos = wpabuf_put(buf, OCV_OCI_EXTENDED_LEN); |
| 4722 | if (ocv_insert_extended_oci(&ci, pos) < 0) { |
| 4723 | wpabuf_free(buf); |
| 4724 | return NULL; |
| 4725 | } |
| 4726 | } |
| 4727 | #endif /* CONFIG_OCV */ |
| 4728 | |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 4729 | wpa_hexdump_buf(MSG_DEBUG, "FILS: Association Request plaintext", buf); |
| 4730 | |
| 4731 | *kek = sm->ptk.kek; |
| 4732 | *kek_len = sm->ptk.kek_len; |
| 4733 | wpa_hexdump_key(MSG_DEBUG, "FILS: KEK for AEAD", *kek, *kek_len); |
| 4734 | *snonce = sm->fils_nonce; |
| 4735 | wpa_hexdump(MSG_DEBUG, "FILS: SNonce for AEAD AAD", |
| 4736 | *snonce, FILS_NONCE_LEN); |
| 4737 | *anonce = sm->fils_anonce; |
| 4738 | wpa_hexdump(MSG_DEBUG, "FILS: ANonce for AEAD AAD", |
| 4739 | *anonce, FILS_NONCE_LEN); |
| 4740 | |
| 4741 | return buf; |
| 4742 | } |
| 4743 | |
| 4744 | |
Dmitry Shmidt | ebd93af | 2017-02-21 13:40:44 -0800 | [diff] [blame] | 4745 | static void fils_process_hlp_resp(struct wpa_sm *sm, const u8 *resp, size_t len) |
| 4746 | { |
| 4747 | const u8 *pos, *end; |
| 4748 | |
| 4749 | wpa_hexdump(MSG_MSGDUMP, "FILS: HLP response", resp, len); |
| 4750 | if (len < 2 * ETH_ALEN) |
| 4751 | return; |
| 4752 | pos = resp + 2 * ETH_ALEN; |
| 4753 | end = resp + len; |
| 4754 | if (end - pos >= 6 && |
| 4755 | os_memcmp(pos, "\xaa\xaa\x03\x00\x00\x00", 6) == 0) |
| 4756 | pos += 6; /* Remove SNAP/LLC header */ |
| 4757 | wpa_sm_fils_hlp_rx(sm, resp, resp + ETH_ALEN, pos, end - pos); |
| 4758 | } |
| 4759 | |
| 4760 | |
| 4761 | static void fils_process_hlp_container(struct wpa_sm *sm, const u8 *pos, |
| 4762 | size_t len) |
| 4763 | { |
| 4764 | const u8 *end = pos + len; |
| 4765 | u8 *tmp, *tmp_pos; |
| 4766 | |
| 4767 | /* Check if there are any FILS HLP Container elements */ |
| 4768 | while (end - pos >= 2) { |
| 4769 | if (2 + pos[1] > end - pos) |
| 4770 | return; |
| 4771 | if (pos[0] == WLAN_EID_EXTENSION && |
| 4772 | pos[1] >= 1 + 2 * ETH_ALEN && |
| 4773 | pos[2] == WLAN_EID_EXT_FILS_HLP_CONTAINER) |
| 4774 | break; |
| 4775 | pos += 2 + pos[1]; |
| 4776 | } |
| 4777 | if (end - pos < 2) |
| 4778 | return; /* No FILS HLP Container elements */ |
| 4779 | |
| 4780 | tmp = os_malloc(end - pos); |
| 4781 | if (!tmp) |
| 4782 | return; |
| 4783 | |
| 4784 | while (end - pos >= 2) { |
| 4785 | if (2 + pos[1] > end - pos || |
| 4786 | pos[0] != WLAN_EID_EXTENSION || |
| 4787 | pos[1] < 1 + 2 * ETH_ALEN || |
| 4788 | pos[2] != WLAN_EID_EXT_FILS_HLP_CONTAINER) |
| 4789 | break; |
| 4790 | tmp_pos = tmp; |
| 4791 | os_memcpy(tmp_pos, pos + 3, pos[1] - 1); |
| 4792 | tmp_pos += pos[1] - 1; |
| 4793 | pos += 2 + pos[1]; |
| 4794 | |
| 4795 | /* Add possible fragments */ |
| 4796 | while (end - pos >= 2 && pos[0] == WLAN_EID_FRAGMENT && |
| 4797 | 2 + pos[1] <= end - pos) { |
| 4798 | os_memcpy(tmp_pos, pos + 2, pos[1]); |
| 4799 | tmp_pos += pos[1]; |
| 4800 | pos += 2 + pos[1]; |
| 4801 | } |
| 4802 | |
| 4803 | fils_process_hlp_resp(sm, tmp, tmp_pos - tmp); |
| 4804 | } |
| 4805 | |
| 4806 | os_free(tmp); |
| 4807 | } |
| 4808 | |
| 4809 | |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 4810 | int fils_process_assoc_resp(struct wpa_sm *sm, const u8 *resp, size_t len) |
| 4811 | { |
| 4812 | const struct ieee80211_mgmt *mgmt; |
| 4813 | const u8 *end, *ie_start; |
| 4814 | struct ieee802_11_elems elems; |
| 4815 | int keylen, rsclen; |
| 4816 | enum wpa_alg alg; |
| 4817 | struct wpa_gtk_data gd; |
| 4818 | int maxkeylen; |
| 4819 | struct wpa_eapol_ie_parse kde; |
| 4820 | |
| 4821 | if (!sm || !sm->ptk_set) { |
| 4822 | wpa_printf(MSG_DEBUG, "FILS: No KEK available"); |
| 4823 | return -1; |
| 4824 | } |
| 4825 | |
| 4826 | if (!wpa_key_mgmt_fils(sm->key_mgmt)) { |
| 4827 | wpa_printf(MSG_DEBUG, "FILS: Not a FILS AKM"); |
| 4828 | return -1; |
| 4829 | } |
| 4830 | |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 4831 | if (sm->fils_completed) { |
| 4832 | wpa_printf(MSG_DEBUG, |
| 4833 | "FILS: Association has already been completed for this FILS authentication - ignore unexpected retransmission"); |
| 4834 | return -1; |
| 4835 | } |
| 4836 | |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 4837 | wpa_hexdump(MSG_DEBUG, "FILS: (Re)Association Response frame", |
| 4838 | resp, len); |
| 4839 | |
| 4840 | mgmt = (const struct ieee80211_mgmt *) resp; |
| 4841 | if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.assoc_resp)) |
| 4842 | return -1; |
| 4843 | |
| 4844 | end = resp + len; |
| 4845 | /* Same offset for Association Response and Reassociation Response */ |
| 4846 | ie_start = mgmt->u.assoc_resp.variable; |
| 4847 | |
| 4848 | if (ieee802_11_parse_elems(ie_start, end - ie_start, &elems, 1) == |
| 4849 | ParseFailed) { |
| 4850 | wpa_printf(MSG_DEBUG, |
| 4851 | "FILS: Failed to parse decrypted elements"); |
| 4852 | goto fail; |
| 4853 | } |
| 4854 | |
| 4855 | if (!elems.fils_session) { |
| 4856 | wpa_printf(MSG_DEBUG, "FILS: No FILS Session element"); |
| 4857 | return -1; |
| 4858 | } |
| 4859 | if (os_memcmp(elems.fils_session, sm->fils_session, |
| 4860 | FILS_SESSION_LEN) != 0) { |
| 4861 | wpa_printf(MSG_DEBUG, "FILS: FILS Session mismatch"); |
| 4862 | wpa_hexdump(MSG_DEBUG, "FILS: Received FILS Session", |
| 4863 | elems.fils_session, FILS_SESSION_LEN); |
| 4864 | wpa_hexdump(MSG_DEBUG, "FILS: Expected FILS Session", |
| 4865 | sm->fils_session, FILS_SESSION_LEN); |
| 4866 | } |
| 4867 | |
Hai Shalom | 81f62d8 | 2019-07-22 12:10:00 -0700 | [diff] [blame] | 4868 | if (!elems.rsn_ie) { |
| 4869 | wpa_printf(MSG_DEBUG, |
| 4870 | "FILS: No RSNE in (Re)Association Response"); |
| 4871 | /* As an interop workaround, allow this for now since IEEE Std |
| 4872 | * 802.11ai-2016 did not include all the needed changes to make |
| 4873 | * a FILS AP include RSNE in the frame. This workaround might |
| 4874 | * eventually be removed and replaced with rejection (goto fail) |
| 4875 | * to follow a strict interpretation of the standard. */ |
| 4876 | } else if (wpa_compare_rsn_ie(wpa_key_mgmt_ft(sm->key_mgmt), |
| 4877 | sm->ap_rsn_ie, sm->ap_rsn_ie_len, |
| 4878 | elems.rsn_ie - 2, elems.rsn_ie_len + 2)) { |
| 4879 | wpa_msg(sm->ctx->msg_ctx, MSG_INFO, |
| 4880 | "FILS: RSNE mismatch between Beacon/Probe Response and (Re)Association Response"); |
| 4881 | wpa_hexdump(MSG_DEBUG, "FILS: RSNE in Beacon/Probe Response", |
| 4882 | sm->ap_rsn_ie, sm->ap_rsn_ie_len); |
| 4883 | wpa_hexdump(MSG_DEBUG, "FILS: RSNE in (Re)Association Response", |
| 4884 | elems.rsn_ie, elems.rsn_ie_len); |
| 4885 | goto fail; |
| 4886 | } |
| 4887 | |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 4888 | /* TODO: FILS Public Key */ |
| 4889 | |
| 4890 | if (!elems.fils_key_confirm) { |
| 4891 | wpa_printf(MSG_DEBUG, "FILS: No FILS Key Confirm element"); |
| 4892 | goto fail; |
| 4893 | } |
| 4894 | if (elems.fils_key_confirm_len != sm->fils_key_auth_len) { |
| 4895 | wpa_printf(MSG_DEBUG, |
| 4896 | "FILS: Unexpected Key-Auth length %d (expected %d)", |
| 4897 | elems.fils_key_confirm_len, |
| 4898 | (int) sm->fils_key_auth_len); |
| 4899 | goto fail; |
| 4900 | } |
| 4901 | if (os_memcmp(elems.fils_key_confirm, sm->fils_key_auth_ap, |
| 4902 | sm->fils_key_auth_len) != 0) { |
| 4903 | wpa_printf(MSG_DEBUG, "FILS: Key-Auth mismatch"); |
| 4904 | wpa_hexdump(MSG_DEBUG, "FILS: Received Key-Auth", |
| 4905 | elems.fils_key_confirm, |
| 4906 | elems.fils_key_confirm_len); |
| 4907 | wpa_hexdump(MSG_DEBUG, "FILS: Expected Key-Auth", |
| 4908 | sm->fils_key_auth_ap, sm->fils_key_auth_len); |
| 4909 | goto fail; |
| 4910 | } |
| 4911 | |
Hai Shalom | 74f70d4 | 2019-02-11 14:42:39 -0800 | [diff] [blame] | 4912 | #ifdef CONFIG_OCV |
| 4913 | if (wpa_sm_ocv_enabled(sm)) { |
| 4914 | struct wpa_channel_info ci; |
| 4915 | |
| 4916 | if (wpa_sm_channel_info(sm, &ci) != 0) { |
| 4917 | wpa_printf(MSG_WARNING, |
| 4918 | "Failed to get channel info to validate received OCI in FILS (Re)Association Response frame"); |
| 4919 | goto fail; |
| 4920 | } |
| 4921 | |
| 4922 | if (ocv_verify_tx_params(elems.oci, elems.oci_len, &ci, |
| 4923 | channel_width_to_int(ci.chanwidth), |
Hai Shalom | 899fcc7 | 2020-10-19 14:38:18 -0700 | [diff] [blame] | 4924 | ci.seg1_idx) != OCI_SUCCESS) { |
| 4925 | wpa_msg(sm->ctx->msg_ctx, MSG_INFO, OCV_FAILURE |
| 4926 | "addr=" MACSTR " frame=fils-assoc error=%s", |
| 4927 | MAC2STR(sm->bssid), ocv_errorstr); |
Hai Shalom | 74f70d4 | 2019-02-11 14:42:39 -0800 | [diff] [blame] | 4928 | goto fail; |
| 4929 | } |
| 4930 | } |
| 4931 | #endif /* CONFIG_OCV */ |
| 4932 | |
Hai Shalom | 021b0b5 | 2019-04-10 11:17:58 -0700 | [diff] [blame] | 4933 | #ifdef CONFIG_IEEE80211R |
| 4934 | if (wpa_key_mgmt_ft(sm->key_mgmt) && sm->fils_ft_ies) { |
| 4935 | struct wpa_ie_data rsn; |
| 4936 | |
| 4937 | /* Check that PMKR1Name derived by the AP matches */ |
| 4938 | if (!elems.rsn_ie || |
| 4939 | wpa_parse_wpa_ie_rsn(elems.rsn_ie - 2, elems.rsn_ie_len + 2, |
| 4940 | &rsn) < 0 || |
| 4941 | !rsn.pmkid || rsn.num_pmkid != 1 || |
| 4942 | os_memcmp(rsn.pmkid, sm->pmk_r1_name, |
| 4943 | WPA_PMK_NAME_LEN) != 0) { |
| 4944 | wpa_printf(MSG_DEBUG, |
| 4945 | "FILS+FT: No RSNE[PMKR1Name] match in AssocResp"); |
| 4946 | goto fail; |
| 4947 | } |
| 4948 | } |
| 4949 | #endif /* CONFIG_IEEE80211R */ |
| 4950 | |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 4951 | /* Key Delivery */ |
| 4952 | if (!elems.key_delivery) { |
| 4953 | wpa_printf(MSG_DEBUG, "FILS: No Key Delivery element"); |
| 4954 | goto fail; |
| 4955 | } |
| 4956 | |
| 4957 | /* Parse GTK and set the key to the driver */ |
| 4958 | os_memset(&gd, 0, sizeof(gd)); |
| 4959 | if (wpa_supplicant_parse_ies(elems.key_delivery + WPA_KEY_RSC_LEN, |
| 4960 | elems.key_delivery_len - WPA_KEY_RSC_LEN, |
| 4961 | &kde) < 0) { |
| 4962 | wpa_printf(MSG_DEBUG, "FILS: Failed to parse KDEs"); |
| 4963 | goto fail; |
| 4964 | } |
| 4965 | if (!kde.gtk) { |
| 4966 | wpa_printf(MSG_DEBUG, "FILS: No GTK KDE"); |
| 4967 | goto fail; |
| 4968 | } |
| 4969 | maxkeylen = gd.gtk_len = kde.gtk_len - 2; |
| 4970 | if (wpa_supplicant_check_group_cipher(sm, sm->group_cipher, |
| 4971 | gd.gtk_len, maxkeylen, |
| 4972 | &gd.key_rsc_len, &gd.alg)) |
| 4973 | goto fail; |
| 4974 | |
| 4975 | wpa_hexdump_key(MSG_DEBUG, "FILS: Received GTK", kde.gtk, kde.gtk_len); |
| 4976 | gd.keyidx = kde.gtk[0] & 0x3; |
| 4977 | gd.tx = wpa_supplicant_gtk_tx_bit_workaround(sm, |
| 4978 | !!(kde.gtk[0] & BIT(2))); |
| 4979 | if (kde.gtk_len - 2 > sizeof(gd.gtk)) { |
| 4980 | wpa_printf(MSG_DEBUG, "FILS: Too long GTK in GTK KDE (len=%lu)", |
| 4981 | (unsigned long) kde.gtk_len - 2); |
| 4982 | goto fail; |
| 4983 | } |
| 4984 | os_memcpy(gd.gtk, kde.gtk + 2, kde.gtk_len - 2); |
| 4985 | |
| 4986 | wpa_printf(MSG_DEBUG, "FILS: Set GTK to driver"); |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 4987 | if (wpa_supplicant_install_gtk(sm, &gd, elems.key_delivery, 0) < 0) { |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 4988 | wpa_printf(MSG_DEBUG, "FILS: Failed to set GTK"); |
| 4989 | goto fail; |
| 4990 | } |
| 4991 | |
| 4992 | if (ieee80211w_set_keys(sm, &kde) < 0) { |
| 4993 | wpa_printf(MSG_DEBUG, "FILS: Failed to set IGTK"); |
| 4994 | goto fail; |
| 4995 | } |
| 4996 | |
| 4997 | alg = wpa_cipher_to_alg(sm->pairwise_cipher); |
| 4998 | keylen = wpa_cipher_key_len(sm->pairwise_cipher); |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 4999 | if (keylen <= 0 || (unsigned int) keylen != sm->ptk.tk_len) { |
| 5000 | wpa_printf(MSG_DEBUG, "FILS: TK length mismatch: %u != %lu", |
| 5001 | keylen, (long unsigned int) sm->ptk.tk_len); |
| 5002 | goto fail; |
| 5003 | } |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 5004 | |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 5005 | rsclen = wpa_cipher_rsc_len(sm->pairwise_cipher); |
| 5006 | wpa_hexdump_key(MSG_DEBUG, "FILS: Set TK to driver", |
| 5007 | sm->ptk.tk, keylen); |
| 5008 | if (wpa_sm_set_key(sm, alg, sm->bssid, 0, 1, null_rsc, rsclen, |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 5009 | sm->ptk.tk, keylen, KEY_FLAG_PAIRWISE_RX_TX) < 0) { |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 5010 | wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, |
| 5011 | "FILS: Failed to set PTK to the driver (alg=%d keylen=%d bssid=" |
| 5012 | MACSTR ")", |
| 5013 | alg, keylen, MAC2STR(sm->bssid)); |
| 5014 | goto fail; |
| 5015 | } |
| 5016 | |
Hai Shalom | 6084025 | 2021-02-19 19:02:11 -0800 | [diff] [blame] | 5017 | wpa_sm_store_ptk(sm, sm->bssid, sm->pairwise_cipher, |
| 5018 | sm->dot11RSNAConfigPMKLifetime, &sm->ptk); |
| 5019 | |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 5020 | /* TODO: TK could be cleared after auth frame exchange now that driver |
| 5021 | * takes care of association frame encryption/decryption. */ |
| 5022 | /* TK is not needed anymore in supplicant */ |
| 5023 | os_memset(sm->ptk.tk, 0, WPA_TK_MAX_LEN); |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 5024 | sm->ptk.tk_len = 0; |
Mathy Vanhoef | c66556c | 2017-09-29 04:22:51 +0200 | [diff] [blame] | 5025 | sm->ptk.installed = 1; |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 5026 | |
Dmitry Shmidt | ebd93af | 2017-02-21 13:40:44 -0800 | [diff] [blame] | 5027 | /* FILS HLP Container */ |
| 5028 | fils_process_hlp_container(sm, ie_start, end - ie_start); |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 5029 | |
| 5030 | /* TODO: FILS IP Address Assignment */ |
| 5031 | |
| 5032 | wpa_printf(MSG_DEBUG, "FILS: Auth+Assoc completed successfully"); |
| 5033 | sm->fils_completed = 1; |
Hai Shalom | 81f62d8 | 2019-07-22 12:10:00 -0700 | [diff] [blame] | 5034 | forced_memzero(&gd, sizeof(gd)); |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 5035 | |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 5036 | if (kde.transition_disable) |
| 5037 | wpa_sm_transition_disable(sm, kde.transition_disable[0]); |
| 5038 | |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 5039 | return 0; |
| 5040 | fail: |
Hai Shalom | 81f62d8 | 2019-07-22 12:10:00 -0700 | [diff] [blame] | 5041 | forced_memzero(&gd, sizeof(gd)); |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 5042 | return -1; |
| 5043 | } |
| 5044 | |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 5045 | |
| 5046 | void wpa_sm_set_reset_fils_completed(struct wpa_sm *sm, int set) |
| 5047 | { |
| 5048 | if (sm) |
| 5049 | sm->fils_completed = !!set; |
| 5050 | } |
| 5051 | |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 5052 | #endif /* CONFIG_FILS */ |
| 5053 | |
| 5054 | |
| 5055 | int wpa_fils_is_completed(struct wpa_sm *sm) |
| 5056 | { |
| 5057 | #ifdef CONFIG_FILS |
| 5058 | return sm && sm->fils_completed; |
| 5059 | #else /* CONFIG_FILS */ |
| 5060 | return 0; |
| 5061 | #endif /* CONFIG_FILS */ |
| 5062 | } |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 5063 | |
| 5064 | |
| 5065 | #ifdef CONFIG_OWE |
| 5066 | |
| 5067 | struct wpabuf * owe_build_assoc_req(struct wpa_sm *sm, u16 group) |
| 5068 | { |
| 5069 | struct wpabuf *ie = NULL, *pub = NULL; |
| 5070 | size_t prime_len; |
| 5071 | |
| 5072 | if (group == 19) |
| 5073 | prime_len = 32; |
| 5074 | else if (group == 20) |
| 5075 | prime_len = 48; |
| 5076 | else if (group == 21) |
| 5077 | prime_len = 66; |
| 5078 | else |
| 5079 | return NULL; |
| 5080 | |
| 5081 | crypto_ecdh_deinit(sm->owe_ecdh); |
| 5082 | sm->owe_ecdh = crypto_ecdh_init(group); |
| 5083 | if (!sm->owe_ecdh) |
| 5084 | goto fail; |
| 5085 | sm->owe_group = group; |
| 5086 | pub = crypto_ecdh_get_pubkey(sm->owe_ecdh, 0); |
| 5087 | pub = wpabuf_zeropad(pub, prime_len); |
| 5088 | if (!pub) |
| 5089 | goto fail; |
| 5090 | |
| 5091 | ie = wpabuf_alloc(5 + wpabuf_len(pub)); |
| 5092 | if (!ie) |
| 5093 | goto fail; |
| 5094 | wpabuf_put_u8(ie, WLAN_EID_EXTENSION); |
| 5095 | wpabuf_put_u8(ie, 1 + 2 + wpabuf_len(pub)); |
| 5096 | wpabuf_put_u8(ie, WLAN_EID_EXT_OWE_DH_PARAM); |
| 5097 | wpabuf_put_le16(ie, group); |
| 5098 | wpabuf_put_buf(ie, pub); |
| 5099 | wpabuf_free(pub); |
| 5100 | wpa_hexdump_buf(MSG_DEBUG, "OWE: Diffie-Hellman Parameter element", |
| 5101 | ie); |
| 5102 | |
| 5103 | return ie; |
| 5104 | fail: |
| 5105 | wpabuf_free(pub); |
| 5106 | crypto_ecdh_deinit(sm->owe_ecdh); |
| 5107 | sm->owe_ecdh = NULL; |
| 5108 | return NULL; |
| 5109 | } |
| 5110 | |
| 5111 | |
| 5112 | int owe_process_assoc_resp(struct wpa_sm *sm, const u8 *bssid, |
| 5113 | const u8 *resp_ies, size_t resp_ies_len) |
| 5114 | { |
| 5115 | struct ieee802_11_elems elems; |
| 5116 | u16 group; |
| 5117 | struct wpabuf *secret, *pub, *hkey; |
| 5118 | int res; |
| 5119 | u8 prk[SHA512_MAC_LEN], pmkid[SHA512_MAC_LEN]; |
| 5120 | const char *info = "OWE Key Generation"; |
| 5121 | const u8 *addr[2]; |
| 5122 | size_t len[2]; |
| 5123 | size_t hash_len, prime_len; |
| 5124 | struct wpa_ie_data data; |
| 5125 | |
| 5126 | if (!resp_ies || |
| 5127 | ieee802_11_parse_elems(resp_ies, resp_ies_len, &elems, 1) == |
| 5128 | ParseFailed) { |
| 5129 | wpa_printf(MSG_INFO, |
| 5130 | "OWE: Could not parse Association Response frame elements"); |
| 5131 | return -1; |
| 5132 | } |
| 5133 | |
| 5134 | if (sm->cur_pmksa && elems.rsn_ie && |
| 5135 | wpa_parse_wpa_ie_rsn(elems.rsn_ie - 2, 2 + elems.rsn_ie_len, |
| 5136 | &data) == 0 && |
| 5137 | data.num_pmkid == 1 && data.pmkid && |
| 5138 | os_memcmp(sm->cur_pmksa->pmkid, data.pmkid, PMKID_LEN) == 0) { |
| 5139 | wpa_printf(MSG_DEBUG, "OWE: Use PMKSA caching"); |
| 5140 | wpa_sm_set_pmk_from_pmksa(sm); |
| 5141 | return 0; |
| 5142 | } |
| 5143 | |
| 5144 | if (!elems.owe_dh) { |
| 5145 | wpa_printf(MSG_INFO, |
| 5146 | "OWE: No Diffie-Hellman Parameter element found in Association Response frame"); |
| 5147 | return -1; |
| 5148 | } |
| 5149 | |
| 5150 | group = WPA_GET_LE16(elems.owe_dh); |
| 5151 | if (group != sm->owe_group) { |
| 5152 | wpa_printf(MSG_INFO, |
| 5153 | "OWE: Unexpected Diffie-Hellman group in response: %u", |
| 5154 | group); |
| 5155 | return -1; |
| 5156 | } |
| 5157 | |
| 5158 | if (!sm->owe_ecdh) { |
| 5159 | wpa_printf(MSG_INFO, "OWE: No ECDH state available"); |
| 5160 | return -1; |
| 5161 | } |
| 5162 | |
| 5163 | if (group == 19) |
| 5164 | prime_len = 32; |
| 5165 | else if (group == 20) |
| 5166 | prime_len = 48; |
| 5167 | else if (group == 21) |
| 5168 | prime_len = 66; |
| 5169 | else |
| 5170 | return -1; |
| 5171 | |
| 5172 | secret = crypto_ecdh_set_peerkey(sm->owe_ecdh, 0, |
| 5173 | elems.owe_dh + 2, |
| 5174 | elems.owe_dh_len - 2); |
| 5175 | secret = wpabuf_zeropad(secret, prime_len); |
| 5176 | if (!secret) { |
| 5177 | wpa_printf(MSG_DEBUG, "OWE: Invalid peer DH public key"); |
| 5178 | return -1; |
| 5179 | } |
| 5180 | wpa_hexdump_buf_key(MSG_DEBUG, "OWE: DH shared secret", secret); |
| 5181 | |
| 5182 | /* prk = HKDF-extract(C | A | group, z) */ |
| 5183 | |
| 5184 | pub = crypto_ecdh_get_pubkey(sm->owe_ecdh, 0); |
| 5185 | if (!pub) { |
| 5186 | wpabuf_clear_free(secret); |
| 5187 | return -1; |
| 5188 | } |
| 5189 | |
| 5190 | /* PMKID = Truncate-128(Hash(C | A)) */ |
| 5191 | addr[0] = wpabuf_head(pub); |
| 5192 | len[0] = wpabuf_len(pub); |
| 5193 | addr[1] = elems.owe_dh + 2; |
| 5194 | len[1] = elems.owe_dh_len - 2; |
| 5195 | if (group == 19) { |
| 5196 | res = sha256_vector(2, addr, len, pmkid); |
| 5197 | hash_len = SHA256_MAC_LEN; |
| 5198 | } else if (group == 20) { |
| 5199 | res = sha384_vector(2, addr, len, pmkid); |
| 5200 | hash_len = SHA384_MAC_LEN; |
| 5201 | } else if (group == 21) { |
| 5202 | res = sha512_vector(2, addr, len, pmkid); |
| 5203 | hash_len = SHA512_MAC_LEN; |
| 5204 | } else { |
| 5205 | res = -1; |
| 5206 | hash_len = 0; |
| 5207 | } |
| 5208 | pub = wpabuf_zeropad(pub, prime_len); |
| 5209 | if (res < 0 || !pub) { |
| 5210 | wpabuf_free(pub); |
| 5211 | wpabuf_clear_free(secret); |
| 5212 | return -1; |
| 5213 | } |
| 5214 | |
| 5215 | hkey = wpabuf_alloc(wpabuf_len(pub) + elems.owe_dh_len - 2 + 2); |
| 5216 | if (!hkey) { |
| 5217 | wpabuf_free(pub); |
| 5218 | wpabuf_clear_free(secret); |
| 5219 | return -1; |
| 5220 | } |
| 5221 | |
| 5222 | wpabuf_put_buf(hkey, pub); /* C */ |
| 5223 | wpabuf_free(pub); |
| 5224 | wpabuf_put_data(hkey, elems.owe_dh + 2, elems.owe_dh_len - 2); /* A */ |
| 5225 | wpabuf_put_le16(hkey, sm->owe_group); /* group */ |
| 5226 | if (group == 19) |
| 5227 | res = hmac_sha256(wpabuf_head(hkey), wpabuf_len(hkey), |
| 5228 | wpabuf_head(secret), wpabuf_len(secret), prk); |
| 5229 | else if (group == 20) |
| 5230 | res = hmac_sha384(wpabuf_head(hkey), wpabuf_len(hkey), |
| 5231 | wpabuf_head(secret), wpabuf_len(secret), prk); |
| 5232 | else if (group == 21) |
| 5233 | res = hmac_sha512(wpabuf_head(hkey), wpabuf_len(hkey), |
| 5234 | wpabuf_head(secret), wpabuf_len(secret), prk); |
| 5235 | wpabuf_clear_free(hkey); |
| 5236 | wpabuf_clear_free(secret); |
| 5237 | if (res < 0) |
| 5238 | return -1; |
| 5239 | |
| 5240 | wpa_hexdump_key(MSG_DEBUG, "OWE: prk", prk, hash_len); |
| 5241 | |
| 5242 | /* PMK = HKDF-expand(prk, "OWE Key Generation", n) */ |
| 5243 | |
| 5244 | if (group == 19) |
| 5245 | res = hmac_sha256_kdf(prk, hash_len, NULL, (const u8 *) info, |
| 5246 | os_strlen(info), sm->pmk, hash_len); |
| 5247 | else if (group == 20) |
| 5248 | res = hmac_sha384_kdf(prk, hash_len, NULL, (const u8 *) info, |
| 5249 | os_strlen(info), sm->pmk, hash_len); |
| 5250 | else if (group == 21) |
| 5251 | res = hmac_sha512_kdf(prk, hash_len, NULL, (const u8 *) info, |
| 5252 | os_strlen(info), sm->pmk, hash_len); |
Hai Shalom | 81f62d8 | 2019-07-22 12:10:00 -0700 | [diff] [blame] | 5253 | forced_memzero(prk, SHA512_MAC_LEN); |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame] | 5254 | if (res < 0) { |
| 5255 | sm->pmk_len = 0; |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 5256 | return -1; |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame] | 5257 | } |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 5258 | sm->pmk_len = hash_len; |
| 5259 | |
| 5260 | wpa_hexdump_key(MSG_DEBUG, "OWE: PMK", sm->pmk, sm->pmk_len); |
| 5261 | wpa_hexdump(MSG_DEBUG, "OWE: PMKID", pmkid, PMKID_LEN); |
| 5262 | pmksa_cache_add(sm->pmksa, sm->pmk, sm->pmk_len, pmkid, NULL, 0, |
| 5263 | bssid, sm->own_addr, sm->network_ctx, sm->key_mgmt, |
| 5264 | NULL); |
| 5265 | |
| 5266 | return 0; |
| 5267 | } |
| 5268 | |
| 5269 | #endif /* CONFIG_OWE */ |
| 5270 | |
| 5271 | |
| 5272 | void wpa_sm_set_fils_cache_id(struct wpa_sm *sm, const u8 *fils_cache_id) |
| 5273 | { |
| 5274 | #ifdef CONFIG_FILS |
| 5275 | if (sm && fils_cache_id) { |
| 5276 | sm->fils_cache_id_set = 1; |
| 5277 | os_memcpy(sm->fils_cache_id, fils_cache_id, FILS_CACHE_ID_LEN); |
| 5278 | } |
| 5279 | #endif /* CONFIG_FILS */ |
| 5280 | } |
Hai Shalom | 021b0b5 | 2019-04-10 11:17:58 -0700 | [diff] [blame] | 5281 | |
| 5282 | |
| 5283 | #ifdef CONFIG_DPP2 |
| 5284 | void wpa_sm_set_dpp_z(struct wpa_sm *sm, const struct wpabuf *z) |
| 5285 | { |
| 5286 | if (sm) { |
| 5287 | wpabuf_clear_free(sm->dpp_z); |
| 5288 | sm->dpp_z = z ? wpabuf_dup(z) : NULL; |
| 5289 | } |
| 5290 | } |
| 5291 | #endif /* CONFIG_DPP2 */ |
Hai Shalom | 6084025 | 2021-02-19 19:02:11 -0800 | [diff] [blame] | 5292 | |
| 5293 | |
| 5294 | #ifdef CONFIG_PASN |
| 5295 | void wpa_pasn_pmksa_cache_add(struct wpa_sm *sm, const u8 *pmk, size_t pmk_len, |
| 5296 | const u8 *pmkid, const u8 *bssid, int key_mgmt) |
| 5297 | { |
| 5298 | sm->cur_pmksa = pmksa_cache_add(sm->pmksa, pmk, pmk_len, pmkid, NULL, 0, |
| 5299 | bssid, sm->own_addr, NULL, |
| 5300 | key_mgmt, 0); |
| 5301 | } |
| 5302 | #endif /* CONFIG_PASN */ |
Hai Shalom | c1a2144 | 2022-02-04 13:43:00 -0800 | [diff] [blame] | 5303 | |
| 5304 | |
| 5305 | void wpa_sm_pmksa_cache_reconfig(struct wpa_sm *sm) |
| 5306 | { |
| 5307 | if (sm) |
| 5308 | pmksa_cache_reconfig(sm->pmksa); |
| 5309 | } |