handshake.h (11789B)
1 /* 2 * 3 * Wireless daemon for Linux 4 * 5 * Copyright (C) 2013-2019 Intel Corporation. All rights reserved. 6 * 7 * This library is free software; you can redistribute it and/or 8 * modify it under the terms of the GNU Lesser General Public 9 * License as published by the Free Software Foundation; either 10 * version 2.1 of the License, or (at your option) any later version. 11 * 12 * This library is distributed in the hope that it will be useful, 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 15 * Lesser General Public License for more details. 16 * 17 * You should have received a copy of the GNU Lesser General Public 18 * License along with this library; if not, write to the Free Software 19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 20 * 21 */ 22 23 #include <stdint.h> 24 #include <stdbool.h> 25 #include <asm/byteorder.h> 26 #include <linux/types.h> 27 #include <ell/cleanup.h> 28 29 #include "src/vendor_quirks.h" 30 31 struct handshake_state; 32 enum crypto_cipher; 33 struct eapol_frame; 34 struct pmksa; 35 36 enum handshake_kde { 37 /* 802.11-2020 Table 12-9 in section 12.7.2 */ 38 HANDSHAKE_KDE_GTK = 0x000fac01, 39 HANDSHAKE_KDE_MAC_ADDRESS = 0x000fac03, 40 HANDSHAKE_KDE_PMKID = 0x000fac04, 41 HANDSHAKE_KDE_NONCE = 0x000fac06, 42 HANDSHAKE_KDE_LIFETIME = 0x000fac07, 43 HANDSHAKE_KDE_ERROR = 0x000fac08, 44 HANDSHAKE_KDE_IGTK = 0x000fac09, 45 HANDSHAKE_KDE_KEY_ID = 0x000fac0a, 46 HANDSHAKE_KDE_MULTIBAND_GTK = 0x000fac0b, 47 HANDSHAKE_KDE_MULTIBAND_KEY_ID = 0x000fac0c, 48 HANDSHAKE_KDE_OCI = 0x000fac0d, 49 HANDSHAKE_KDE_BIGTK = 0x000fac0e, 50 /* Wi-Fi P2P Technical Specification v1.7 4.2.8 */ 51 HANDSHAKE_KDE_IP_ADDRESS_REQ = 0x506f9a04, 52 HANDSHAKE_KDE_IP_ADDRESS_ALLOC = 0x506f9a05, 53 /* Wi-Fi WPA3 Specification v3.0 Table 4 */ 54 HANDSHAKE_KDE_TRANSITION_DISABLE = 0x506f9a20, 55 }; 56 57 enum handshake_event { 58 HANDSHAKE_EVENT_STARTED, 59 HANDSHAKE_EVENT_SETTING_KEYS, 60 HANDSHAKE_EVENT_SETTING_KEYS_FAILED, 61 HANDSHAKE_EVENT_COMPLETE, 62 HANDSHAKE_EVENT_FAILED, 63 HANDSHAKE_EVENT_REKEY_FAILED, 64 HANDSHAKE_EVENT_EAP_NOTIFY, 65 HANDSHAKE_EVENT_TRANSITION_DISABLE, 66 HANDSHAKE_EVENT_P2P_IP_REQUEST, 67 HANDSHAKE_EVENT_REKEY_COMPLETE, 68 }; 69 70 typedef void (*handshake_event_func_t)(struct handshake_state *hs, 71 enum handshake_event event, 72 void *user_data, ...); 73 74 typedef bool (*handshake_get_nonce_func_t)(uint8_t nonce[]); 75 typedef void (*handshake_install_tk_func_t)(struct handshake_state *hs, 76 uint8_t key_index, 77 const uint8_t *tk, uint32_t cipher); 78 typedef void (*handshake_install_gtk_func_t)(struct handshake_state *hs, 79 uint16_t key_index, 80 const uint8_t *gtk, uint8_t gtk_len, 81 const uint8_t *rsc, uint8_t rsc_len, 82 uint32_t cipher); 83 typedef void (*handshake_install_igtk_func_t)(struct handshake_state *hs, 84 uint16_t key_index, 85 const uint8_t *igtk, uint8_t igtk_len, 86 const uint8_t *ipn, uint8_t ipn_len, 87 uint32_t cipher); 88 typedef void (*handshake_install_ext_tk_func_t)(struct handshake_state *hs, 89 uint8_t key_idx, const uint8_t *tk, 90 uint32_t cipher, 91 const struct eapol_frame *step4, 92 uint16_t proto, bool noencrypt); 93 94 void __handshake_set_get_nonce_func(handshake_get_nonce_func_t func); 95 void __handshake_set_install_tk_func(handshake_install_tk_func_t func); 96 void __handshake_set_install_gtk_func(handshake_install_gtk_func_t func); 97 void __handshake_set_install_igtk_func(handshake_install_igtk_func_t func); 98 void __handshake_set_install_ext_tk_func(handshake_install_ext_tk_func_t func); 99 100 struct handshake_state { 101 uint32_t ifindex; 102 uint8_t spa[6]; 103 uint8_t aa[6]; 104 uint8_t *authenticator_ie; 105 uint8_t *supplicant_ie; 106 uint8_t *authenticator_rsnxe; 107 uint8_t *supplicant_rsnxe; 108 uint8_t *mde; 109 uint8_t *authenticator_fte; 110 uint8_t *supplicant_fte; 111 uint8_t *vendor_ies; 112 struct vendor_quirk vendor_quirks; 113 size_t vendor_ies_len; 114 enum ie_rsn_cipher_suite pairwise_cipher; 115 enum ie_rsn_cipher_suite group_cipher; 116 enum ie_rsn_cipher_suite group_management_cipher; 117 enum ie_rsn_akm_suite akm_suite; 118 uint8_t pmk[64]; 119 size_t pmk_len; 120 uint8_t snonce[32]; 121 uint8_t anonce[32]; 122 uint8_t ptk[136]; 123 uint8_t pmk_r0[48]; 124 uint8_t pmk_r0_name[16]; 125 uint8_t pmk_r1[48]; 126 uint8_t pmk_r1_name[16]; 127 uint8_t pmkid[16]; 128 uint8_t fils_ft[48]; 129 uint8_t fils_ft_len; 130 struct l_settings *settings_8021x; 131 struct l_ecc_point **ecc_sae_pts; 132 bool have_snonce : 1; 133 bool ptk_complete : 1; 134 bool wpa_ie : 1; 135 bool osen_ie : 1; 136 bool have_pmk : 1; 137 bool mfp : 1; 138 bool have_anonce : 1; 139 bool have_pmkid : 1; 140 bool authenticator : 1; 141 bool wait_for_gtk : 1; 142 bool no_rekey : 1; 143 bool support_fils : 1; 144 bool authenticator_ocvc : 1; 145 bool supplicant_ocvc : 1; 146 bool ext_key_id_capable : 1; 147 bool force_default_ecc_group : 1; 148 bool have_pmksa : 1; 149 union { 150 struct pmksa *pmksa; 151 uint64_t expiration; 152 }; 153 uint8_t ssid[SSID_MAX_SIZE]; 154 size_t ssid_len; 155 char *passphrase; 156 char *password_identifier; 157 uint8_t r0khid[48]; 158 size_t r0khid_len; 159 uint8_t r1khid[6]; 160 uint8_t gtk[32]; 161 uint8_t gtk_rsc[6]; 162 uint8_t igtk[32]; 163 uint8_t igtk_rsc[6]; 164 uint8_t proto_version : 2; 165 unsigned int gtk_index; 166 unsigned int igtk_index; 167 uint8_t active_tk_index; 168 struct erp_cache_entry *erp_cache; 169 bool support_ip_allocation : 1; 170 uint32_t client_ip_addr; 171 uint32_t subnet_mask; 172 uint32_t go_ip_addr; 173 uint8_t *fils_ip_req_ie; 174 uint8_t *fils_ip_resp_ie; 175 struct band_chandef *chandef; 176 void *user_data; 177 178 void (*free)(struct handshake_state *s); 179 bool in_event; 180 181 handshake_event_func_t event_func; 182 183 int refcount; 184 }; 185 186 #define HSID(x) UNIQUE_ID(handshake_, x) 187 188 #define handshake_event(_hs, event, ...) \ 189 ({ \ 190 bool HSID(freed) = false; \ 191 typeof(_hs) HSID(hs) = (_hs); \ 192 \ 193 if (HSID(hs)->event_func && !HSID(hs)->in_event) { \ 194 HSID(hs)->in_event = true; \ 195 HSID(hs)->event_func(HSID(hs), (event), \ 196 HSID(hs)->user_data, \ 197 ##__VA_ARGS__); \ 198 \ 199 if (!HSID(hs)->in_event) { \ 200 handshake_state_unref(HSID(hs)); \ 201 HSID(freed) = true; \ 202 } else \ 203 HSID(hs)->in_event = false; \ 204 } \ 205 HSID(freed); \ 206 }) 207 208 struct handshake_state *handshake_state_ref(struct handshake_state *s); 209 void handshake_state_unref(struct handshake_state *s); 210 211 void handshake_state_set_supplicant_address(struct handshake_state *s, 212 const uint8_t *spa); 213 void handshake_state_set_authenticator_address(struct handshake_state *s, 214 const uint8_t *aa); 215 void handshake_state_set_authenticator(struct handshake_state *s, bool auth); 216 void handshake_state_set_pmk(struct handshake_state *s, const uint8_t *pmk, 217 size_t pmk_len); 218 void handshake_state_set_ptk(struct handshake_state *s, const uint8_t *ptk, 219 size_t ptk_len); 220 void handshake_state_set_8021x_config(struct handshake_state *s, 221 struct l_settings *settings); 222 bool handshake_state_set_authenticator_ie(struct handshake_state *s, 223 const uint8_t *ie); 224 bool handshake_state_set_supplicant_ie(struct handshake_state *s, 225 const uint8_t *ie); 226 void handshake_state_set_authenticator_rsnxe(struct handshake_state *s, 227 const uint8_t *ie); 228 void handshake_state_set_supplicant_rsnxe(struct handshake_state *s, 229 const uint8_t *ie); 230 void handshake_state_set_ssid(struct handshake_state *s, 231 const uint8_t *ssid, size_t ssid_len); 232 void handshake_state_set_mde(struct handshake_state *s, 233 const uint8_t *mde); 234 void handshake_state_set_authenticator_fte(struct handshake_state *s, 235 const uint8_t *fte); 236 void handshake_state_set_supplicant_fte(struct handshake_state *s, 237 const uint8_t *fte); 238 239 void handshake_state_set_vendor_ies(struct handshake_state *s, 240 const struct iovec *iov, 241 size_t n_iovs); 242 243 void handshake_state_set_vendor_quirks(struct handshake_state *s, 244 struct vendor_quirk quirks); 245 246 void handshake_state_set_kh_ids(struct handshake_state *s, 247 const uint8_t *r0khid, size_t r0khid_len, 248 const uint8_t *r1khid); 249 250 void handshake_state_set_event_func(struct handshake_state *s, 251 handshake_event_func_t func, 252 void *user_data); 253 void handshake_state_set_passphrase(struct handshake_state *s, 254 const char *passphrase); 255 void handshake_state_set_password_identifier(struct handshake_state *s, 256 const char *id); 257 bool handshake_state_add_ecc_sae_pt(struct handshake_state *s, 258 const struct l_ecc_point *pt); 259 void handshake_state_set_no_rekey(struct handshake_state *s, bool no_rekey); 260 261 void handshake_state_set_fils_ft(struct handshake_state *s, 262 const uint8_t *fils_ft, 263 size_t fils_ft_len); 264 265 void handshake_state_set_protocol_version(struct handshake_state *s, 266 uint8_t proto_version); 267 268 void handshake_state_new_snonce(struct handshake_state *s); 269 void handshake_state_new_anonce(struct handshake_state *s); 270 void handshake_state_set_anonce(struct handshake_state *s, 271 const uint8_t *anonce); 272 void handshake_state_set_pmkid(struct handshake_state *s, const uint8_t *pmkid); 273 bool handshake_state_derive_ptk(struct handshake_state *s); 274 size_t handshake_state_get_ptk_size(struct handshake_state *s); 275 size_t handshake_state_get_kck_len(struct handshake_state *s); 276 const uint8_t *handshake_state_get_kck(struct handshake_state *s); 277 size_t handshake_state_get_kek_len(struct handshake_state *s); 278 const uint8_t *handshake_state_get_kek(struct handshake_state *s); 279 void handshake_state_install_ptk(struct handshake_state *s); 280 281 void handshake_state_install_ext_ptk(struct handshake_state *s, 282 uint8_t key_idx, 283 struct eapol_frame *ek, uint16_t proto, 284 bool noencrypt); 285 286 void handshake_state_install_gtk(struct handshake_state *s, 287 uint16_t gtk_key_index, 288 const uint8_t *gtk, size_t gtk_len, 289 const uint8_t *rsc, uint8_t rsc_len); 290 291 void handshake_state_install_igtk(struct handshake_state *s, 292 uint16_t igtk_key_index, 293 const uint8_t *igtk, size_t igtk_len, 294 const uint8_t *ipn); 295 296 void handshake_state_override_pairwise_cipher(struct handshake_state *s, 297 enum ie_rsn_cipher_suite pairwise); 298 299 bool handshake_state_get_pmkid(struct handshake_state *s, uint8_t *out_pmkid, 300 enum l_checksum_type sha); 301 bool handshake_state_pmkid_matches(struct handshake_state *s, 302 const uint8_t *check); 303 bool handshake_decode_fte_key(struct handshake_state *s, const uint8_t *wrapped, 304 size_t key_len, uint8_t *key_out); 305 306 void handshake_state_set_gtk(struct handshake_state *s, const uint8_t *key, 307 unsigned int key_index, const uint8_t *rsc); 308 309 void handshake_state_set_igtk(struct handshake_state *s, const uint8_t *key, 310 unsigned int key_index, const uint8_t *rsc); 311 312 void handshake_state_set_chandef(struct handshake_state *s, 313 struct band_chandef *chandef); 314 int handshake_state_verify_oci(struct handshake_state *s, const uint8_t *oci, 315 size_t oci_len); 316 317 bool handshake_state_set_pmksa(struct handshake_state *s, struct pmksa *pmksa); 318 void handshake_state_cache_pmksa(struct handshake_state *s); 319 bool handshake_state_remove_pmksa(struct handshake_state *s); 320 321 bool handshake_util_ap_ie_matches(struct handshake_state *s, 322 const struct ie_rsn_info *msg_info, 323 const uint8_t *scan_ie, bool is_wpa); 324 325 const uint8_t *handshake_util_find_kde(enum handshake_kde selector, 326 const uint8_t *data, size_t data_len, 327 size_t *out_kde_len); 328 const uint8_t *handshake_util_find_gtk_kde(const uint8_t *data, size_t data_len, 329 size_t *out_gtk_len); 330 const uint8_t *handshake_util_find_igtk_kde(const uint8_t *data, 331 size_t data_len, size_t *out_igtk_len); 332 const uint8_t *handshake_util_find_pmkid_kde(const uint8_t *data, 333 size_t data_len); 334 void handshake_util_build_gtk_kde(enum crypto_cipher cipher, const uint8_t *key, 335 unsigned int key_index, uint8_t *to); 336 void handshake_util_build_igtk_kde(enum crypto_cipher cipher, const uint8_t *key, 337 unsigned int key_index, uint8_t *to); 338 339 DEFINE_CLEANUP_FUNC(handshake_state_unref);