Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1 | /* |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 2 | * IEEE 802.11 RSN / WPA Authenticator |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 3 | * Copyright (c) 2004-2013, Jouni Malinen <j@w1.fi> |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4 | * |
Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 5 | * This software may be distributed under the terms of the BSD license. |
| 6 | * See README for more details. |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | #include "utils/includes.h" |
| 10 | |
| 11 | #include "utils/common.h" |
| 12 | #include "utils/eloop.h" |
| 13 | #include "utils/state_machine.h" |
| 14 | #include "common/ieee802_11_defs.h" |
| 15 | #include "crypto/aes_wrap.h" |
| 16 | #include "crypto/crypto.h" |
| 17 | #include "crypto/sha1.h" |
| 18 | #include "crypto/sha256.h" |
| 19 | #include "crypto/random.h" |
| 20 | #include "eapol_auth/eapol_auth_sm.h" |
| 21 | #include "ap_config.h" |
| 22 | #include "ieee802_11.h" |
| 23 | #include "wpa_auth.h" |
| 24 | #include "pmksa_cache_auth.h" |
| 25 | #include "wpa_auth_i.h" |
| 26 | #include "wpa_auth_ie.h" |
| 27 | |
| 28 | #define STATE_MACHINE_DATA struct wpa_state_machine |
| 29 | #define STATE_MACHINE_DEBUG_PREFIX "WPA" |
| 30 | #define STATE_MACHINE_ADDR sm->addr |
| 31 | |
| 32 | |
| 33 | static void wpa_send_eapol_timeout(void *eloop_ctx, void *timeout_ctx); |
| 34 | static int wpa_sm_step(struct wpa_state_machine *sm); |
| 35 | static int wpa_verify_key_mic(struct wpa_ptk *PTK, u8 *data, size_t data_len); |
| 36 | static void wpa_sm_call_step(void *eloop_ctx, void *timeout_ctx); |
| 37 | static void wpa_group_sm_step(struct wpa_authenticator *wpa_auth, |
| 38 | struct wpa_group *group); |
| 39 | static void wpa_request_new_ptk(struct wpa_state_machine *sm); |
| 40 | static int wpa_gtk_update(struct wpa_authenticator *wpa_auth, |
| 41 | struct wpa_group *group); |
| 42 | static int wpa_group_config_group_keys(struct wpa_authenticator *wpa_auth, |
| 43 | struct wpa_group *group); |
| 44 | |
| 45 | static const u32 dot11RSNAConfigGroupUpdateCount = 4; |
| 46 | static const u32 dot11RSNAConfigPairwiseUpdateCount = 4; |
| 47 | static const u32 eapol_key_timeout_first = 100; /* ms */ |
| 48 | static const u32 eapol_key_timeout_subseq = 1000; /* ms */ |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 49 | static const u32 eapol_key_timeout_first_group = 500; /* ms */ |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 50 | |
| 51 | /* TODO: make these configurable */ |
| 52 | static const int dot11RSNAConfigPMKLifetime = 43200; |
| 53 | static const int dot11RSNAConfigPMKReauthThreshold = 70; |
| 54 | static const int dot11RSNAConfigSATimeout = 60; |
| 55 | |
| 56 | |
Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 57 | static inline int wpa_auth_mic_failure_report( |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 58 | struct wpa_authenticator *wpa_auth, const u8 *addr) |
| 59 | { |
| 60 | if (wpa_auth->cb.mic_failure_report) |
Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 61 | return wpa_auth->cb.mic_failure_report(wpa_auth->cb.ctx, addr); |
| 62 | return 0; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | |
| 66 | static inline void wpa_auth_set_eapol(struct wpa_authenticator *wpa_auth, |
| 67 | const u8 *addr, wpa_eapol_variable var, |
| 68 | int value) |
| 69 | { |
| 70 | if (wpa_auth->cb.set_eapol) |
| 71 | wpa_auth->cb.set_eapol(wpa_auth->cb.ctx, addr, var, value); |
| 72 | } |
| 73 | |
| 74 | |
| 75 | static inline int wpa_auth_get_eapol(struct wpa_authenticator *wpa_auth, |
| 76 | const u8 *addr, wpa_eapol_variable var) |
| 77 | { |
| 78 | if (wpa_auth->cb.get_eapol == NULL) |
| 79 | return -1; |
| 80 | return wpa_auth->cb.get_eapol(wpa_auth->cb.ctx, addr, var); |
| 81 | } |
| 82 | |
| 83 | |
| 84 | static inline const u8 * wpa_auth_get_psk(struct wpa_authenticator *wpa_auth, |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 85 | const u8 *addr, |
| 86 | const u8 *p2p_dev_addr, |
| 87 | const u8 *prev_psk) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 88 | { |
| 89 | if (wpa_auth->cb.get_psk == NULL) |
| 90 | return NULL; |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 91 | return wpa_auth->cb.get_psk(wpa_auth->cb.ctx, addr, p2p_dev_addr, |
| 92 | prev_psk); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | |
| 96 | static inline int wpa_auth_get_msk(struct wpa_authenticator *wpa_auth, |
| 97 | const u8 *addr, u8 *msk, size_t *len) |
| 98 | { |
| 99 | if (wpa_auth->cb.get_msk == NULL) |
| 100 | return -1; |
| 101 | return wpa_auth->cb.get_msk(wpa_auth->cb.ctx, addr, msk, len); |
| 102 | } |
| 103 | |
| 104 | |
| 105 | static inline int wpa_auth_set_key(struct wpa_authenticator *wpa_auth, |
| 106 | int vlan_id, |
| 107 | enum wpa_alg alg, const u8 *addr, int idx, |
| 108 | u8 *key, size_t key_len) |
| 109 | { |
| 110 | if (wpa_auth->cb.set_key == NULL) |
| 111 | return -1; |
| 112 | return wpa_auth->cb.set_key(wpa_auth->cb.ctx, vlan_id, alg, addr, idx, |
| 113 | key, key_len); |
| 114 | } |
| 115 | |
| 116 | |
| 117 | static inline int wpa_auth_get_seqnum(struct wpa_authenticator *wpa_auth, |
| 118 | const u8 *addr, int idx, u8 *seq) |
| 119 | { |
| 120 | if (wpa_auth->cb.get_seqnum == NULL) |
| 121 | return -1; |
| 122 | return wpa_auth->cb.get_seqnum(wpa_auth->cb.ctx, addr, idx, seq); |
| 123 | } |
| 124 | |
| 125 | |
| 126 | static inline int |
| 127 | wpa_auth_send_eapol(struct wpa_authenticator *wpa_auth, const u8 *addr, |
| 128 | const u8 *data, size_t data_len, int encrypt) |
| 129 | { |
| 130 | if (wpa_auth->cb.send_eapol == NULL) |
| 131 | return -1; |
| 132 | return wpa_auth->cb.send_eapol(wpa_auth->cb.ctx, addr, data, data_len, |
| 133 | encrypt); |
| 134 | } |
| 135 | |
| 136 | |
| 137 | int wpa_auth_for_each_sta(struct wpa_authenticator *wpa_auth, |
| 138 | int (*cb)(struct wpa_state_machine *sm, void *ctx), |
| 139 | void *cb_ctx) |
| 140 | { |
| 141 | if (wpa_auth->cb.for_each_sta == NULL) |
| 142 | return 0; |
| 143 | return wpa_auth->cb.for_each_sta(wpa_auth->cb.ctx, cb, cb_ctx); |
| 144 | } |
| 145 | |
| 146 | |
| 147 | int wpa_auth_for_each_auth(struct wpa_authenticator *wpa_auth, |
| 148 | int (*cb)(struct wpa_authenticator *a, void *ctx), |
| 149 | void *cb_ctx) |
| 150 | { |
| 151 | if (wpa_auth->cb.for_each_auth == NULL) |
| 152 | return 0; |
| 153 | return wpa_auth->cb.for_each_auth(wpa_auth->cb.ctx, cb, cb_ctx); |
| 154 | } |
| 155 | |
| 156 | |
| 157 | void wpa_auth_logger(struct wpa_authenticator *wpa_auth, const u8 *addr, |
| 158 | logger_level level, const char *txt) |
| 159 | { |
| 160 | if (wpa_auth->cb.logger == NULL) |
| 161 | return; |
| 162 | wpa_auth->cb.logger(wpa_auth->cb.ctx, addr, level, txt); |
| 163 | } |
| 164 | |
| 165 | |
| 166 | void wpa_auth_vlogger(struct wpa_authenticator *wpa_auth, const u8 *addr, |
| 167 | logger_level level, const char *fmt, ...) |
| 168 | { |
| 169 | char *format; |
| 170 | int maxlen; |
| 171 | va_list ap; |
| 172 | |
| 173 | if (wpa_auth->cb.logger == NULL) |
| 174 | return; |
| 175 | |
| 176 | maxlen = os_strlen(fmt) + 100; |
| 177 | format = os_malloc(maxlen); |
| 178 | if (!format) |
| 179 | return; |
| 180 | |
| 181 | va_start(ap, fmt); |
| 182 | vsnprintf(format, maxlen, fmt, ap); |
| 183 | va_end(ap); |
| 184 | |
| 185 | wpa_auth_logger(wpa_auth, addr, level, format); |
| 186 | |
| 187 | os_free(format); |
| 188 | } |
| 189 | |
| 190 | |
| 191 | static void wpa_sta_disconnect(struct wpa_authenticator *wpa_auth, |
| 192 | const u8 *addr) |
| 193 | { |
| 194 | if (wpa_auth->cb.disconnect == NULL) |
| 195 | return; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 196 | wpa_printf(MSG_DEBUG, "wpa_sta_disconnect STA " MACSTR, MAC2STR(addr)); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 197 | wpa_auth->cb.disconnect(wpa_auth->cb.ctx, addr, |
| 198 | WLAN_REASON_PREV_AUTH_NOT_VALID); |
| 199 | } |
| 200 | |
| 201 | |
| 202 | static int wpa_use_aes_cmac(struct wpa_state_machine *sm) |
| 203 | { |
| 204 | int ret = 0; |
| 205 | #ifdef CONFIG_IEEE80211R |
| 206 | if (wpa_key_mgmt_ft(sm->wpa_key_mgmt)) |
| 207 | ret = 1; |
| 208 | #endif /* CONFIG_IEEE80211R */ |
| 209 | #ifdef CONFIG_IEEE80211W |
| 210 | if (wpa_key_mgmt_sha256(sm->wpa_key_mgmt)) |
| 211 | ret = 1; |
| 212 | #endif /* CONFIG_IEEE80211W */ |
| 213 | return ret; |
| 214 | } |
| 215 | |
| 216 | |
| 217 | static void wpa_rekey_gmk(void *eloop_ctx, void *timeout_ctx) |
| 218 | { |
| 219 | struct wpa_authenticator *wpa_auth = eloop_ctx; |
| 220 | |
| 221 | if (random_get_bytes(wpa_auth->group->GMK, WPA_GMK_LEN)) { |
| 222 | wpa_printf(MSG_ERROR, "Failed to get random data for WPA " |
| 223 | "initialization."); |
| 224 | } else { |
| 225 | wpa_auth_logger(wpa_auth, NULL, LOGGER_DEBUG, "GMK rekeyd"); |
| 226 | wpa_hexdump_key(MSG_DEBUG, "GMK", |
| 227 | wpa_auth->group->GMK, WPA_GMK_LEN); |
| 228 | } |
| 229 | |
| 230 | if (wpa_auth->conf.wpa_gmk_rekey) { |
| 231 | eloop_register_timeout(wpa_auth->conf.wpa_gmk_rekey, 0, |
| 232 | wpa_rekey_gmk, wpa_auth, NULL); |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | |
| 237 | static void wpa_rekey_gtk(void *eloop_ctx, void *timeout_ctx) |
| 238 | { |
| 239 | struct wpa_authenticator *wpa_auth = eloop_ctx; |
| 240 | struct wpa_group *group; |
| 241 | |
| 242 | wpa_auth_logger(wpa_auth, NULL, LOGGER_DEBUG, "rekeying GTK"); |
| 243 | for (group = wpa_auth->group; group; group = group->next) { |
| 244 | group->GTKReKey = TRUE; |
| 245 | do { |
| 246 | group->changed = FALSE; |
| 247 | wpa_group_sm_step(wpa_auth, group); |
| 248 | } while (group->changed); |
| 249 | } |
| 250 | |
| 251 | if (wpa_auth->conf.wpa_group_rekey) { |
| 252 | eloop_register_timeout(wpa_auth->conf.wpa_group_rekey, |
| 253 | 0, wpa_rekey_gtk, wpa_auth, NULL); |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | |
| 258 | static void wpa_rekey_ptk(void *eloop_ctx, void *timeout_ctx) |
| 259 | { |
| 260 | struct wpa_authenticator *wpa_auth = eloop_ctx; |
| 261 | struct wpa_state_machine *sm = timeout_ctx; |
| 262 | |
| 263 | wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG, "rekeying PTK"); |
| 264 | wpa_request_new_ptk(sm); |
| 265 | wpa_sm_step(sm); |
| 266 | } |
| 267 | |
| 268 | |
| 269 | static int wpa_auth_pmksa_clear_cb(struct wpa_state_machine *sm, void *ctx) |
| 270 | { |
| 271 | if (sm->pmksa == ctx) |
| 272 | sm->pmksa = NULL; |
| 273 | return 0; |
| 274 | } |
| 275 | |
| 276 | |
| 277 | static void wpa_auth_pmksa_free_cb(struct rsn_pmksa_cache_entry *entry, |
| 278 | void *ctx) |
| 279 | { |
| 280 | struct wpa_authenticator *wpa_auth = ctx; |
| 281 | wpa_auth_for_each_sta(wpa_auth, wpa_auth_pmksa_clear_cb, entry); |
| 282 | } |
| 283 | |
| 284 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 285 | static int wpa_group_init_gmk_and_counter(struct wpa_authenticator *wpa_auth, |
| 286 | struct wpa_group *group) |
| 287 | { |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 288 | u8 buf[ETH_ALEN + 8 + sizeof(unsigned long)]; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 289 | u8 rkey[32]; |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 290 | unsigned long ptr; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 291 | |
| 292 | if (random_get_bytes(group->GMK, WPA_GMK_LEN) < 0) |
| 293 | return -1; |
| 294 | wpa_hexdump_key(MSG_DEBUG, "GMK", group->GMK, WPA_GMK_LEN); |
| 295 | |
| 296 | /* |
| 297 | * Counter = PRF-256(Random number, "Init Counter", |
| 298 | * Local MAC Address || Time) |
| 299 | */ |
| 300 | os_memcpy(buf, wpa_auth->addr, ETH_ALEN); |
| 301 | wpa_get_ntp_timestamp(buf + ETH_ALEN); |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 302 | ptr = (unsigned long) group; |
| 303 | os_memcpy(buf + ETH_ALEN + 8, &ptr, sizeof(ptr)); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 304 | if (random_get_bytes(rkey, sizeof(rkey)) < 0) |
| 305 | return -1; |
| 306 | |
| 307 | if (sha1_prf(rkey, sizeof(rkey), "Init Counter", buf, sizeof(buf), |
| 308 | group->Counter, WPA_NONCE_LEN) < 0) |
| 309 | return -1; |
| 310 | wpa_hexdump_key(MSG_DEBUG, "Key Counter", |
| 311 | group->Counter, WPA_NONCE_LEN); |
| 312 | |
| 313 | return 0; |
| 314 | } |
| 315 | |
| 316 | |
| 317 | static struct wpa_group * wpa_group_init(struct wpa_authenticator *wpa_auth, |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 318 | int vlan_id, int delay_init) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 319 | { |
| 320 | struct wpa_group *group; |
| 321 | |
| 322 | group = os_zalloc(sizeof(struct wpa_group)); |
| 323 | if (group == NULL) |
| 324 | return NULL; |
| 325 | |
| 326 | group->GTKAuthenticator = TRUE; |
| 327 | group->vlan_id = vlan_id; |
Dmitry Shmidt | 4530cfd | 2012-09-09 15:20:40 -0700 | [diff] [blame] | 328 | group->GTK_len = wpa_cipher_key_len(wpa_auth->conf.wpa_group); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 329 | |
| 330 | if (random_pool_ready() != 1) { |
| 331 | wpa_printf(MSG_INFO, "WPA: Not enough entropy in random pool " |
| 332 | "for secure operations - update keys later when " |
| 333 | "the first station connects"); |
| 334 | } |
| 335 | |
| 336 | /* |
| 337 | * Set initial GMK/Counter value here. The actual values that will be |
| 338 | * used in negotiations will be set once the first station tries to |
| 339 | * connect. This allows more time for collecting additional randomness |
| 340 | * on embedded devices. |
| 341 | */ |
| 342 | if (wpa_group_init_gmk_and_counter(wpa_auth, group) < 0) { |
| 343 | wpa_printf(MSG_ERROR, "Failed to get random data for WPA " |
| 344 | "initialization."); |
| 345 | os_free(group); |
| 346 | return NULL; |
| 347 | } |
| 348 | |
| 349 | group->GInit = TRUE; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 350 | if (delay_init) { |
| 351 | wpa_printf(MSG_DEBUG, "WPA: Delay group state machine start " |
| 352 | "until Beacon frames have been configured"); |
| 353 | /* Initialization is completed in wpa_init_keys(). */ |
| 354 | } else { |
| 355 | wpa_group_sm_step(wpa_auth, group); |
| 356 | group->GInit = FALSE; |
| 357 | wpa_group_sm_step(wpa_auth, group); |
| 358 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 359 | |
| 360 | return group; |
| 361 | } |
| 362 | |
| 363 | |
| 364 | /** |
| 365 | * wpa_init - Initialize WPA authenticator |
| 366 | * @addr: Authenticator address |
| 367 | * @conf: Configuration for WPA authenticator |
| 368 | * @cb: Callback functions for WPA authenticator |
| 369 | * Returns: Pointer to WPA authenticator data or %NULL on failure |
| 370 | */ |
| 371 | struct wpa_authenticator * wpa_init(const u8 *addr, |
| 372 | struct wpa_auth_config *conf, |
| 373 | struct wpa_auth_callbacks *cb) |
| 374 | { |
| 375 | struct wpa_authenticator *wpa_auth; |
| 376 | |
| 377 | wpa_auth = os_zalloc(sizeof(struct wpa_authenticator)); |
| 378 | if (wpa_auth == NULL) |
| 379 | return NULL; |
| 380 | os_memcpy(wpa_auth->addr, addr, ETH_ALEN); |
| 381 | os_memcpy(&wpa_auth->conf, conf, sizeof(*conf)); |
| 382 | os_memcpy(&wpa_auth->cb, cb, sizeof(*cb)); |
| 383 | |
| 384 | if (wpa_auth_gen_wpa_ie(wpa_auth)) { |
| 385 | wpa_printf(MSG_ERROR, "Could not generate WPA IE."); |
| 386 | os_free(wpa_auth); |
| 387 | return NULL; |
| 388 | } |
| 389 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 390 | wpa_auth->group = wpa_group_init(wpa_auth, 0, 1); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 391 | if (wpa_auth->group == NULL) { |
| 392 | os_free(wpa_auth->wpa_ie); |
| 393 | os_free(wpa_auth); |
| 394 | return NULL; |
| 395 | } |
| 396 | |
| 397 | wpa_auth->pmksa = pmksa_cache_auth_init(wpa_auth_pmksa_free_cb, |
| 398 | wpa_auth); |
| 399 | if (wpa_auth->pmksa == NULL) { |
| 400 | wpa_printf(MSG_ERROR, "PMKSA cache initialization failed."); |
| 401 | os_free(wpa_auth->wpa_ie); |
| 402 | os_free(wpa_auth); |
| 403 | return NULL; |
| 404 | } |
| 405 | |
| 406 | #ifdef CONFIG_IEEE80211R |
| 407 | wpa_auth->ft_pmk_cache = wpa_ft_pmk_cache_init(); |
| 408 | if (wpa_auth->ft_pmk_cache == NULL) { |
| 409 | wpa_printf(MSG_ERROR, "FT PMK cache initialization failed."); |
| 410 | os_free(wpa_auth->wpa_ie); |
| 411 | pmksa_cache_auth_deinit(wpa_auth->pmksa); |
| 412 | os_free(wpa_auth); |
| 413 | return NULL; |
| 414 | } |
| 415 | #endif /* CONFIG_IEEE80211R */ |
| 416 | |
| 417 | if (wpa_auth->conf.wpa_gmk_rekey) { |
| 418 | eloop_register_timeout(wpa_auth->conf.wpa_gmk_rekey, 0, |
| 419 | wpa_rekey_gmk, wpa_auth, NULL); |
| 420 | } |
| 421 | |
| 422 | if (wpa_auth->conf.wpa_group_rekey) { |
| 423 | eloop_register_timeout(wpa_auth->conf.wpa_group_rekey, 0, |
| 424 | wpa_rekey_gtk, wpa_auth, NULL); |
| 425 | } |
| 426 | |
| 427 | return wpa_auth; |
| 428 | } |
| 429 | |
| 430 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 431 | int wpa_init_keys(struct wpa_authenticator *wpa_auth) |
| 432 | { |
| 433 | struct wpa_group *group = wpa_auth->group; |
| 434 | |
| 435 | wpa_printf(MSG_DEBUG, "WPA: Start group state machine to set initial " |
| 436 | "keys"); |
| 437 | wpa_group_sm_step(wpa_auth, group); |
| 438 | group->GInit = FALSE; |
| 439 | wpa_group_sm_step(wpa_auth, group); |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame^] | 440 | if (group->wpa_group_state == WPA_GROUP_FATAL_FAILURE) |
| 441 | return -1; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 442 | return 0; |
| 443 | } |
| 444 | |
| 445 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 446 | /** |
| 447 | * wpa_deinit - Deinitialize WPA authenticator |
| 448 | * @wpa_auth: Pointer to WPA authenticator data from wpa_init() |
| 449 | */ |
| 450 | void wpa_deinit(struct wpa_authenticator *wpa_auth) |
| 451 | { |
| 452 | struct wpa_group *group, *prev; |
| 453 | |
| 454 | eloop_cancel_timeout(wpa_rekey_gmk, wpa_auth, NULL); |
| 455 | eloop_cancel_timeout(wpa_rekey_gtk, wpa_auth, NULL); |
| 456 | |
| 457 | #ifdef CONFIG_PEERKEY |
| 458 | while (wpa_auth->stsl_negotiations) |
| 459 | wpa_stsl_remove(wpa_auth, wpa_auth->stsl_negotiations); |
| 460 | #endif /* CONFIG_PEERKEY */ |
| 461 | |
| 462 | pmksa_cache_auth_deinit(wpa_auth->pmksa); |
| 463 | |
| 464 | #ifdef CONFIG_IEEE80211R |
| 465 | wpa_ft_pmk_cache_deinit(wpa_auth->ft_pmk_cache); |
| 466 | wpa_auth->ft_pmk_cache = NULL; |
| 467 | #endif /* CONFIG_IEEE80211R */ |
| 468 | |
| 469 | os_free(wpa_auth->wpa_ie); |
| 470 | |
| 471 | group = wpa_auth->group; |
| 472 | while (group) { |
| 473 | prev = group; |
| 474 | group = group->next; |
| 475 | os_free(prev); |
| 476 | } |
| 477 | |
| 478 | os_free(wpa_auth); |
| 479 | } |
| 480 | |
| 481 | |
| 482 | /** |
| 483 | * wpa_reconfig - Update WPA authenticator configuration |
| 484 | * @wpa_auth: Pointer to WPA authenticator data from wpa_init() |
| 485 | * @conf: Configuration for WPA authenticator |
| 486 | */ |
| 487 | int wpa_reconfig(struct wpa_authenticator *wpa_auth, |
| 488 | struct wpa_auth_config *conf) |
| 489 | { |
| 490 | struct wpa_group *group; |
| 491 | if (wpa_auth == NULL) |
| 492 | return 0; |
| 493 | |
| 494 | os_memcpy(&wpa_auth->conf, conf, sizeof(*conf)); |
| 495 | if (wpa_auth_gen_wpa_ie(wpa_auth)) { |
| 496 | wpa_printf(MSG_ERROR, "Could not generate WPA IE."); |
| 497 | return -1; |
| 498 | } |
| 499 | |
| 500 | /* |
| 501 | * Reinitialize GTK to make sure it is suitable for the new |
| 502 | * configuration. |
| 503 | */ |
| 504 | group = wpa_auth->group; |
Dmitry Shmidt | 4530cfd | 2012-09-09 15:20:40 -0700 | [diff] [blame] | 505 | group->GTK_len = wpa_cipher_key_len(wpa_auth->conf.wpa_group); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 506 | group->GInit = TRUE; |
| 507 | wpa_group_sm_step(wpa_auth, group); |
| 508 | group->GInit = FALSE; |
| 509 | wpa_group_sm_step(wpa_auth, group); |
| 510 | |
| 511 | return 0; |
| 512 | } |
| 513 | |
| 514 | |
| 515 | struct wpa_state_machine * |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 516 | wpa_auth_sta_init(struct wpa_authenticator *wpa_auth, const u8 *addr, |
| 517 | const u8 *p2p_dev_addr) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 518 | { |
| 519 | struct wpa_state_machine *sm; |
| 520 | |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame^] | 521 | if (wpa_auth->group->wpa_group_state == WPA_GROUP_FATAL_FAILURE) |
| 522 | return NULL; |
| 523 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 524 | sm = os_zalloc(sizeof(struct wpa_state_machine)); |
| 525 | if (sm == NULL) |
| 526 | return NULL; |
| 527 | os_memcpy(sm->addr, addr, ETH_ALEN); |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 528 | if (p2p_dev_addr) |
| 529 | os_memcpy(sm->p2p_dev_addr, p2p_dev_addr, ETH_ALEN); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 530 | |
| 531 | sm->wpa_auth = wpa_auth; |
| 532 | sm->group = wpa_auth->group; |
| 533 | |
| 534 | return sm; |
| 535 | } |
| 536 | |
| 537 | |
| 538 | int wpa_auth_sta_associated(struct wpa_authenticator *wpa_auth, |
| 539 | struct wpa_state_machine *sm) |
| 540 | { |
| 541 | if (wpa_auth == NULL || !wpa_auth->conf.wpa || sm == NULL) |
| 542 | return -1; |
| 543 | |
| 544 | #ifdef CONFIG_IEEE80211R |
| 545 | if (sm->ft_completed) { |
| 546 | wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG, |
| 547 | "FT authentication already completed - do not " |
| 548 | "start 4-way handshake"); |
| 549 | return 0; |
| 550 | } |
| 551 | #endif /* CONFIG_IEEE80211R */ |
| 552 | |
| 553 | if (sm->started) { |
| 554 | os_memset(&sm->key_replay, 0, sizeof(sm->key_replay)); |
| 555 | sm->ReAuthenticationRequest = TRUE; |
| 556 | return wpa_sm_step(sm); |
| 557 | } |
| 558 | |
| 559 | wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG, |
| 560 | "start authentication"); |
| 561 | sm->started = 1; |
| 562 | |
| 563 | sm->Init = TRUE; |
| 564 | if (wpa_sm_step(sm) == 1) |
| 565 | return 1; /* should not really happen */ |
| 566 | sm->Init = FALSE; |
| 567 | sm->AuthenticationRequest = TRUE; |
| 568 | return wpa_sm_step(sm); |
| 569 | } |
| 570 | |
| 571 | |
| 572 | void wpa_auth_sta_no_wpa(struct wpa_state_machine *sm) |
| 573 | { |
| 574 | /* WPA/RSN was not used - clear WPA state. This is needed if the STA |
| 575 | * reassociates back to the same AP while the previous entry for the |
| 576 | * STA has not yet been removed. */ |
| 577 | if (sm == NULL) |
| 578 | return; |
| 579 | |
| 580 | sm->wpa_key_mgmt = 0; |
| 581 | } |
| 582 | |
| 583 | |
| 584 | static void wpa_free_sta_sm(struct wpa_state_machine *sm) |
| 585 | { |
| 586 | if (sm->GUpdateStationKeys) { |
| 587 | sm->group->GKeyDoneStations--; |
| 588 | sm->GUpdateStationKeys = FALSE; |
| 589 | } |
| 590 | #ifdef CONFIG_IEEE80211R |
| 591 | os_free(sm->assoc_resp_ftie); |
| 592 | #endif /* CONFIG_IEEE80211R */ |
| 593 | os_free(sm->last_rx_eapol_key); |
| 594 | os_free(sm->wpa_ie); |
| 595 | os_free(sm); |
| 596 | } |
| 597 | |
| 598 | |
| 599 | void wpa_auth_sta_deinit(struct wpa_state_machine *sm) |
| 600 | { |
| 601 | if (sm == NULL) |
| 602 | return; |
| 603 | |
| 604 | if (sm->wpa_auth->conf.wpa_strict_rekey && sm->has_GTK) { |
| 605 | wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG, |
| 606 | "strict rekeying - force GTK rekey since STA " |
| 607 | "is leaving"); |
| 608 | eloop_cancel_timeout(wpa_rekey_gtk, sm->wpa_auth, NULL); |
| 609 | eloop_register_timeout(0, 500000, wpa_rekey_gtk, sm->wpa_auth, |
| 610 | NULL); |
| 611 | } |
| 612 | |
| 613 | eloop_cancel_timeout(wpa_send_eapol_timeout, sm->wpa_auth, sm); |
| 614 | sm->pending_1_of_4_timeout = 0; |
| 615 | eloop_cancel_timeout(wpa_sm_call_step, sm, NULL); |
| 616 | eloop_cancel_timeout(wpa_rekey_ptk, sm->wpa_auth, sm); |
| 617 | if (sm->in_step_loop) { |
| 618 | /* Must not free state machine while wpa_sm_step() is running. |
| 619 | * Freeing will be completed in the end of wpa_sm_step(). */ |
| 620 | wpa_printf(MSG_DEBUG, "WPA: Registering pending STA state " |
| 621 | "machine deinit for " MACSTR, MAC2STR(sm->addr)); |
| 622 | sm->pending_deinit = 1; |
| 623 | } else |
| 624 | wpa_free_sta_sm(sm); |
| 625 | } |
| 626 | |
| 627 | |
| 628 | static void wpa_request_new_ptk(struct wpa_state_machine *sm) |
| 629 | { |
| 630 | if (sm == NULL) |
| 631 | return; |
| 632 | |
| 633 | sm->PTKRequest = TRUE; |
| 634 | sm->PTK_valid = 0; |
| 635 | } |
| 636 | |
| 637 | |
Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 638 | static int wpa_replay_counter_valid(struct wpa_key_replay_counter *ctr, |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 639 | const u8 *replay_counter) |
| 640 | { |
| 641 | int i; |
| 642 | for (i = 0; i < RSNA_MAX_EAPOL_RETRIES; i++) { |
Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 643 | if (!ctr[i].valid) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 644 | break; |
Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 645 | if (os_memcmp(replay_counter, ctr[i].counter, |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 646 | WPA_REPLAY_COUNTER_LEN) == 0) |
| 647 | return 1; |
| 648 | } |
| 649 | return 0; |
| 650 | } |
| 651 | |
| 652 | |
Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 653 | static void wpa_replay_counter_mark_invalid(struct wpa_key_replay_counter *ctr, |
| 654 | const u8 *replay_counter) |
| 655 | { |
| 656 | int i; |
| 657 | for (i = 0; i < RSNA_MAX_EAPOL_RETRIES; i++) { |
| 658 | if (ctr[i].valid && |
| 659 | (replay_counter == NULL || |
| 660 | os_memcmp(replay_counter, ctr[i].counter, |
| 661 | WPA_REPLAY_COUNTER_LEN) == 0)) |
| 662 | ctr[i].valid = FALSE; |
| 663 | } |
| 664 | } |
| 665 | |
| 666 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 667 | #ifdef CONFIG_IEEE80211R |
| 668 | static int ft_check_msg_2_of_4(struct wpa_authenticator *wpa_auth, |
| 669 | struct wpa_state_machine *sm, |
| 670 | struct wpa_eapol_ie_parse *kde) |
| 671 | { |
| 672 | struct wpa_ie_data ie; |
| 673 | struct rsn_mdie *mdie; |
| 674 | |
| 675 | if (wpa_parse_wpa_ie_rsn(kde->rsn_ie, kde->rsn_ie_len, &ie) < 0 || |
| 676 | ie.num_pmkid != 1 || ie.pmkid == NULL) { |
| 677 | wpa_printf(MSG_DEBUG, "FT: No PMKR1Name in " |
| 678 | "FT 4-way handshake message 2/4"); |
| 679 | return -1; |
| 680 | } |
| 681 | |
| 682 | os_memcpy(sm->sup_pmk_r1_name, ie.pmkid, PMKID_LEN); |
| 683 | wpa_hexdump(MSG_DEBUG, "FT: PMKR1Name from Supplicant", |
| 684 | sm->sup_pmk_r1_name, PMKID_LEN); |
| 685 | |
| 686 | if (!kde->mdie || !kde->ftie) { |
| 687 | wpa_printf(MSG_DEBUG, "FT: No %s in FT 4-way handshake " |
| 688 | "message 2/4", kde->mdie ? "FTIE" : "MDIE"); |
| 689 | return -1; |
| 690 | } |
| 691 | |
| 692 | mdie = (struct rsn_mdie *) (kde->mdie + 2); |
| 693 | if (kde->mdie[1] < sizeof(struct rsn_mdie) || |
| 694 | os_memcmp(wpa_auth->conf.mobility_domain, mdie->mobility_domain, |
| 695 | MOBILITY_DOMAIN_ID_LEN) != 0) { |
| 696 | wpa_printf(MSG_DEBUG, "FT: MDIE mismatch"); |
| 697 | return -1; |
| 698 | } |
| 699 | |
| 700 | if (sm->assoc_resp_ftie && |
| 701 | (kde->ftie[1] != sm->assoc_resp_ftie[1] || |
| 702 | os_memcmp(kde->ftie, sm->assoc_resp_ftie, |
| 703 | 2 + sm->assoc_resp_ftie[1]) != 0)) { |
| 704 | wpa_printf(MSG_DEBUG, "FT: FTIE mismatch"); |
| 705 | wpa_hexdump(MSG_DEBUG, "FT: FTIE in EAPOL-Key msg 2/4", |
| 706 | kde->ftie, kde->ftie_len); |
| 707 | wpa_hexdump(MSG_DEBUG, "FT: FTIE in (Re)AssocResp", |
| 708 | sm->assoc_resp_ftie, 2 + sm->assoc_resp_ftie[1]); |
| 709 | return -1; |
| 710 | } |
| 711 | |
| 712 | return 0; |
| 713 | } |
| 714 | #endif /* CONFIG_IEEE80211R */ |
| 715 | |
| 716 | |
Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 717 | static int wpa_receive_error_report(struct wpa_authenticator *wpa_auth, |
| 718 | struct wpa_state_machine *sm, int group) |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 719 | { |
| 720 | /* Supplicant reported a Michael MIC error */ |
| 721 | wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_INFO, |
| 722 | "received EAPOL-Key Error Request " |
| 723 | "(STA detected Michael MIC failure (group=%d))", |
| 724 | group); |
| 725 | |
| 726 | if (group && wpa_auth->conf.wpa_group != WPA_CIPHER_TKIP) { |
| 727 | wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO, |
| 728 | "ignore Michael MIC failure report since " |
| 729 | "group cipher is not TKIP"); |
| 730 | } else if (!group && sm->pairwise != WPA_CIPHER_TKIP) { |
| 731 | wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO, |
| 732 | "ignore Michael MIC failure report since " |
| 733 | "pairwise cipher is not TKIP"); |
| 734 | } else { |
Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 735 | if (wpa_auth_mic_failure_report(wpa_auth, sm->addr) > 0) |
| 736 | return 1; /* STA entry was removed */ |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 737 | sm->dot11RSNAStatsTKIPRemoteMICFailures++; |
| 738 | wpa_auth->dot11RSNAStatsTKIPRemoteMICFailures++; |
| 739 | } |
| 740 | |
| 741 | /* |
| 742 | * Error report is not a request for a new key handshake, but since |
| 743 | * Authenticator may do it, let's change the keys now anyway. |
| 744 | */ |
| 745 | wpa_request_new_ptk(sm); |
Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 746 | return 0; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 747 | } |
| 748 | |
| 749 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 750 | void wpa_receive(struct wpa_authenticator *wpa_auth, |
| 751 | struct wpa_state_machine *sm, |
| 752 | u8 *data, size_t data_len) |
| 753 | { |
| 754 | struct ieee802_1x_hdr *hdr; |
| 755 | struct wpa_eapol_key *key; |
| 756 | u16 key_info, key_data_length; |
| 757 | enum { PAIRWISE_2, PAIRWISE_4, GROUP_2, REQUEST, |
| 758 | SMK_M1, SMK_M3, SMK_ERROR } msg; |
| 759 | char *msgtxt; |
| 760 | struct wpa_eapol_ie_parse kde; |
| 761 | int ft; |
| 762 | const u8 *eapol_key_ie; |
| 763 | size_t eapol_key_ie_len; |
| 764 | |
| 765 | if (wpa_auth == NULL || !wpa_auth->conf.wpa || sm == NULL) |
| 766 | return; |
| 767 | |
| 768 | if (data_len < sizeof(*hdr) + sizeof(*key)) |
| 769 | return; |
| 770 | |
| 771 | hdr = (struct ieee802_1x_hdr *) data; |
| 772 | key = (struct wpa_eapol_key *) (hdr + 1); |
| 773 | key_info = WPA_GET_BE16(key->key_info); |
| 774 | key_data_length = WPA_GET_BE16(key->key_data_length); |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 775 | wpa_printf(MSG_DEBUG, "WPA: Received EAPOL-Key from " MACSTR |
| 776 | " key_info=0x%x type=%u key_data_length=%u", |
| 777 | MAC2STR(sm->addr), key_info, key->type, key_data_length); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 778 | if (key_data_length > data_len - sizeof(*hdr) - sizeof(*key)) { |
| 779 | wpa_printf(MSG_INFO, "WPA: Invalid EAPOL-Key frame - " |
| 780 | "key_data overflow (%d > %lu)", |
| 781 | key_data_length, |
| 782 | (unsigned long) (data_len - sizeof(*hdr) - |
| 783 | sizeof(*key))); |
| 784 | return; |
| 785 | } |
| 786 | |
| 787 | if (sm->wpa == WPA_VERSION_WPA2) { |
Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 788 | if (key->type == EAPOL_KEY_TYPE_WPA) { |
| 789 | /* |
| 790 | * Some deployed station implementations seem to send |
| 791 | * msg 4/4 with incorrect type value in WPA2 mode. |
| 792 | */ |
| 793 | wpa_printf(MSG_DEBUG, "Workaround: Allow EAPOL-Key " |
| 794 | "with unexpected WPA type in RSN mode"); |
| 795 | } else if (key->type != EAPOL_KEY_TYPE_RSN) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 796 | wpa_printf(MSG_DEBUG, "Ignore EAPOL-Key with " |
| 797 | "unexpected type %d in RSN mode", |
| 798 | key->type); |
| 799 | return; |
| 800 | } |
| 801 | } else { |
| 802 | if (key->type != EAPOL_KEY_TYPE_WPA) { |
| 803 | wpa_printf(MSG_DEBUG, "Ignore EAPOL-Key with " |
| 804 | "unexpected type %d in WPA mode", |
| 805 | key->type); |
| 806 | return; |
| 807 | } |
| 808 | } |
| 809 | |
| 810 | wpa_hexdump(MSG_DEBUG, "WPA: Received Key Nonce", key->key_nonce, |
| 811 | WPA_NONCE_LEN); |
| 812 | wpa_hexdump(MSG_DEBUG, "WPA: Received Replay Counter", |
| 813 | key->replay_counter, WPA_REPLAY_COUNTER_LEN); |
| 814 | |
| 815 | /* FIX: verify that the EAPOL-Key frame was encrypted if pairwise keys |
| 816 | * are set */ |
| 817 | |
| 818 | if ((key_info & (WPA_KEY_INFO_SMK_MESSAGE | WPA_KEY_INFO_REQUEST)) == |
| 819 | (WPA_KEY_INFO_SMK_MESSAGE | WPA_KEY_INFO_REQUEST)) { |
| 820 | if (key_info & WPA_KEY_INFO_ERROR) { |
| 821 | msg = SMK_ERROR; |
| 822 | msgtxt = "SMK Error"; |
| 823 | } else { |
| 824 | msg = SMK_M1; |
| 825 | msgtxt = "SMK M1"; |
| 826 | } |
| 827 | } else if (key_info & WPA_KEY_INFO_SMK_MESSAGE) { |
| 828 | msg = SMK_M3; |
| 829 | msgtxt = "SMK M3"; |
| 830 | } else if (key_info & WPA_KEY_INFO_REQUEST) { |
| 831 | msg = REQUEST; |
| 832 | msgtxt = "Request"; |
| 833 | } else if (!(key_info & WPA_KEY_INFO_KEY_TYPE)) { |
| 834 | msg = GROUP_2; |
| 835 | msgtxt = "2/2 Group"; |
| 836 | } else if (key_data_length == 0) { |
| 837 | msg = PAIRWISE_4; |
| 838 | msgtxt = "4/4 Pairwise"; |
| 839 | } else { |
| 840 | msg = PAIRWISE_2; |
| 841 | msgtxt = "2/4 Pairwise"; |
| 842 | } |
| 843 | |
| 844 | /* TODO: key_info type validation for PeerKey */ |
| 845 | if (msg == REQUEST || msg == PAIRWISE_2 || msg == PAIRWISE_4 || |
| 846 | msg == GROUP_2) { |
| 847 | u16 ver = key_info & WPA_KEY_INFO_TYPE_MASK; |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 848 | if (sm->pairwise == WPA_CIPHER_CCMP || |
| 849 | sm->pairwise == WPA_CIPHER_GCMP) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 850 | if (wpa_use_aes_cmac(sm) && |
| 851 | ver != WPA_KEY_INFO_TYPE_AES_128_CMAC) { |
| 852 | wpa_auth_logger(wpa_auth, sm->addr, |
| 853 | LOGGER_WARNING, |
| 854 | "advertised support for " |
| 855 | "AES-128-CMAC, but did not " |
| 856 | "use it"); |
| 857 | return; |
| 858 | } |
| 859 | |
| 860 | if (!wpa_use_aes_cmac(sm) && |
| 861 | ver != WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) { |
| 862 | wpa_auth_logger(wpa_auth, sm->addr, |
| 863 | LOGGER_WARNING, |
| 864 | "did not use HMAC-SHA1-AES " |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 865 | "with CCMP/GCMP"); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 866 | return; |
| 867 | } |
| 868 | } |
| 869 | } |
| 870 | |
| 871 | if (key_info & WPA_KEY_INFO_REQUEST) { |
| 872 | if (sm->req_replay_counter_used && |
| 873 | os_memcmp(key->replay_counter, sm->req_replay_counter, |
| 874 | WPA_REPLAY_COUNTER_LEN) <= 0) { |
| 875 | wpa_auth_logger(wpa_auth, sm->addr, LOGGER_WARNING, |
| 876 | "received EAPOL-Key request with " |
| 877 | "replayed counter"); |
| 878 | return; |
| 879 | } |
| 880 | } |
| 881 | |
| 882 | if (!(key_info & WPA_KEY_INFO_REQUEST) && |
Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 883 | !wpa_replay_counter_valid(sm->key_replay, key->replay_counter)) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 884 | int i; |
Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 885 | |
| 886 | if (msg == PAIRWISE_2 && |
| 887 | wpa_replay_counter_valid(sm->prev_key_replay, |
| 888 | key->replay_counter) && |
| 889 | sm->wpa_ptk_state == WPA_PTK_PTKINITNEGOTIATING && |
| 890 | os_memcmp(sm->SNonce, key->key_nonce, WPA_NONCE_LEN) != 0) |
| 891 | { |
| 892 | /* |
| 893 | * Some supplicant implementations (e.g., Windows XP |
| 894 | * WZC) update SNonce for each EAPOL-Key 2/4. This |
| 895 | * breaks the workaround on accepting any of the |
| 896 | * pending requests, so allow the SNonce to be updated |
| 897 | * even if we have already sent out EAPOL-Key 3/4. |
| 898 | */ |
| 899 | wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_DEBUG, |
| 900 | "Process SNonce update from STA " |
| 901 | "based on retransmitted EAPOL-Key " |
| 902 | "1/4"); |
| 903 | sm->update_snonce = 1; |
| 904 | wpa_replay_counter_mark_invalid(sm->prev_key_replay, |
| 905 | key->replay_counter); |
| 906 | goto continue_processing; |
| 907 | } |
| 908 | |
| 909 | if (msg == PAIRWISE_2 && |
| 910 | wpa_replay_counter_valid(sm->prev_key_replay, |
| 911 | key->replay_counter) && |
| 912 | sm->wpa_ptk_state == WPA_PTK_PTKINITNEGOTIATING) { |
| 913 | wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_DEBUG, |
| 914 | "ignore retransmitted EAPOL-Key %s - " |
| 915 | "SNonce did not change", msgtxt); |
| 916 | } else { |
| 917 | wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_DEBUG, |
| 918 | "received EAPOL-Key %s with " |
| 919 | "unexpected replay counter", msgtxt); |
| 920 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 921 | for (i = 0; i < RSNA_MAX_EAPOL_RETRIES; i++) { |
| 922 | if (!sm->key_replay[i].valid) |
| 923 | break; |
| 924 | wpa_hexdump(MSG_DEBUG, "pending replay counter", |
| 925 | sm->key_replay[i].counter, |
| 926 | WPA_REPLAY_COUNTER_LEN); |
| 927 | } |
| 928 | wpa_hexdump(MSG_DEBUG, "received replay counter", |
| 929 | key->replay_counter, WPA_REPLAY_COUNTER_LEN); |
| 930 | return; |
| 931 | } |
| 932 | |
Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 933 | continue_processing: |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 934 | switch (msg) { |
| 935 | case PAIRWISE_2: |
| 936 | if (sm->wpa_ptk_state != WPA_PTK_PTKSTART && |
Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 937 | sm->wpa_ptk_state != WPA_PTK_PTKCALCNEGOTIATING && |
| 938 | (!sm->update_snonce || |
| 939 | sm->wpa_ptk_state != WPA_PTK_PTKINITNEGOTIATING)) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 940 | wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_INFO, |
| 941 | "received EAPOL-Key msg 2/4 in " |
| 942 | "invalid state (%d) - dropped", |
| 943 | sm->wpa_ptk_state); |
| 944 | return; |
| 945 | } |
| 946 | random_add_randomness(key->key_nonce, WPA_NONCE_LEN); |
| 947 | if (sm->group->reject_4way_hs_for_entropy) { |
| 948 | /* |
| 949 | * The system did not have enough entropy to generate |
| 950 | * strong random numbers. Reject the first 4-way |
| 951 | * handshake(s) and collect some entropy based on the |
| 952 | * information from it. Once enough entropy is |
| 953 | * available, the next atempt will trigger GMK/Key |
| 954 | * Counter update and the station will be allowed to |
| 955 | * continue. |
| 956 | */ |
| 957 | wpa_printf(MSG_DEBUG, "WPA: Reject 4-way handshake to " |
| 958 | "collect more entropy for random number " |
| 959 | "generation"); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 960 | random_mark_pool_ready(); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 961 | wpa_sta_disconnect(wpa_auth, sm->addr); |
| 962 | return; |
| 963 | } |
| 964 | if (wpa_parse_kde_ies((u8 *) (key + 1), key_data_length, |
| 965 | &kde) < 0) { |
| 966 | wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_INFO, |
| 967 | "received EAPOL-Key msg 2/4 with " |
| 968 | "invalid Key Data contents"); |
| 969 | return; |
| 970 | } |
| 971 | if (kde.rsn_ie) { |
| 972 | eapol_key_ie = kde.rsn_ie; |
| 973 | eapol_key_ie_len = kde.rsn_ie_len; |
| 974 | } else { |
| 975 | eapol_key_ie = kde.wpa_ie; |
| 976 | eapol_key_ie_len = kde.wpa_ie_len; |
| 977 | } |
| 978 | ft = sm->wpa == WPA_VERSION_WPA2 && |
| 979 | wpa_key_mgmt_ft(sm->wpa_key_mgmt); |
| 980 | if (sm->wpa_ie == NULL || |
| 981 | wpa_compare_rsn_ie(ft, |
| 982 | sm->wpa_ie, sm->wpa_ie_len, |
| 983 | eapol_key_ie, eapol_key_ie_len)) { |
| 984 | wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO, |
| 985 | "WPA IE from (Re)AssocReq did not " |
| 986 | "match with msg 2/4"); |
| 987 | if (sm->wpa_ie) { |
| 988 | wpa_hexdump(MSG_DEBUG, "WPA IE in AssocReq", |
| 989 | sm->wpa_ie, sm->wpa_ie_len); |
| 990 | } |
| 991 | wpa_hexdump(MSG_DEBUG, "WPA IE in msg 2/4", |
| 992 | eapol_key_ie, eapol_key_ie_len); |
| 993 | /* MLME-DEAUTHENTICATE.request */ |
| 994 | wpa_sta_disconnect(wpa_auth, sm->addr); |
| 995 | return; |
| 996 | } |
| 997 | #ifdef CONFIG_IEEE80211R |
| 998 | if (ft && ft_check_msg_2_of_4(wpa_auth, sm, &kde) < 0) { |
| 999 | wpa_sta_disconnect(wpa_auth, sm->addr); |
| 1000 | return; |
| 1001 | } |
| 1002 | #endif /* CONFIG_IEEE80211R */ |
| 1003 | break; |
| 1004 | case PAIRWISE_4: |
| 1005 | if (sm->wpa_ptk_state != WPA_PTK_PTKINITNEGOTIATING || |
| 1006 | !sm->PTK_valid) { |
| 1007 | wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_INFO, |
| 1008 | "received EAPOL-Key msg 4/4 in " |
| 1009 | "invalid state (%d) - dropped", |
| 1010 | sm->wpa_ptk_state); |
| 1011 | return; |
| 1012 | } |
| 1013 | break; |
| 1014 | case GROUP_2: |
| 1015 | if (sm->wpa_ptk_group_state != WPA_PTK_GROUP_REKEYNEGOTIATING |
| 1016 | || !sm->PTK_valid) { |
| 1017 | wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_INFO, |
| 1018 | "received EAPOL-Key msg 2/2 in " |
| 1019 | "invalid state (%d) - dropped", |
| 1020 | sm->wpa_ptk_group_state); |
| 1021 | return; |
| 1022 | } |
| 1023 | break; |
| 1024 | #ifdef CONFIG_PEERKEY |
| 1025 | case SMK_M1: |
| 1026 | case SMK_M3: |
| 1027 | case SMK_ERROR: |
| 1028 | if (!wpa_auth->conf.peerkey) { |
| 1029 | wpa_printf(MSG_DEBUG, "RSN: SMK M1/M3/Error, but " |
| 1030 | "PeerKey use disabled - ignoring message"); |
| 1031 | return; |
| 1032 | } |
| 1033 | if (!sm->PTK_valid) { |
| 1034 | wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO, |
| 1035 | "received EAPOL-Key msg SMK in " |
| 1036 | "invalid state - dropped"); |
| 1037 | return; |
| 1038 | } |
| 1039 | break; |
| 1040 | #else /* CONFIG_PEERKEY */ |
| 1041 | case SMK_M1: |
| 1042 | case SMK_M3: |
| 1043 | case SMK_ERROR: |
| 1044 | return; /* STSL disabled - ignore SMK messages */ |
| 1045 | #endif /* CONFIG_PEERKEY */ |
| 1046 | case REQUEST: |
| 1047 | break; |
| 1048 | } |
| 1049 | |
| 1050 | wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_DEBUG, |
| 1051 | "received EAPOL-Key frame (%s)", msgtxt); |
| 1052 | |
| 1053 | if (key_info & WPA_KEY_INFO_ACK) { |
| 1054 | wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO, |
| 1055 | "received invalid EAPOL-Key: Key Ack set"); |
| 1056 | return; |
| 1057 | } |
| 1058 | |
| 1059 | if (!(key_info & WPA_KEY_INFO_MIC)) { |
| 1060 | wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO, |
| 1061 | "received invalid EAPOL-Key: Key MIC not set"); |
| 1062 | return; |
| 1063 | } |
| 1064 | |
| 1065 | sm->MICVerified = FALSE; |
Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 1066 | if (sm->PTK_valid && !sm->update_snonce) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1067 | if (wpa_verify_key_mic(&sm->PTK, data, data_len)) { |
| 1068 | wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO, |
| 1069 | "received EAPOL-Key with invalid MIC"); |
| 1070 | return; |
| 1071 | } |
| 1072 | sm->MICVerified = TRUE; |
| 1073 | eloop_cancel_timeout(wpa_send_eapol_timeout, wpa_auth, sm); |
| 1074 | sm->pending_1_of_4_timeout = 0; |
| 1075 | } |
| 1076 | |
| 1077 | if (key_info & WPA_KEY_INFO_REQUEST) { |
| 1078 | if (sm->MICVerified) { |
| 1079 | sm->req_replay_counter_used = 1; |
| 1080 | os_memcpy(sm->req_replay_counter, key->replay_counter, |
| 1081 | WPA_REPLAY_COUNTER_LEN); |
| 1082 | } else { |
| 1083 | wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO, |
| 1084 | "received EAPOL-Key request with " |
| 1085 | "invalid MIC"); |
| 1086 | return; |
| 1087 | } |
| 1088 | |
| 1089 | /* |
| 1090 | * TODO: should decrypt key data field if encryption was used; |
| 1091 | * even though MAC address KDE is not normally encrypted, |
| 1092 | * supplicant is allowed to encrypt it. |
| 1093 | */ |
| 1094 | if (msg == SMK_ERROR) { |
| 1095 | #ifdef CONFIG_PEERKEY |
| 1096 | wpa_smk_error(wpa_auth, sm, key); |
| 1097 | #endif /* CONFIG_PEERKEY */ |
| 1098 | return; |
| 1099 | } else if (key_info & WPA_KEY_INFO_ERROR) { |
Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 1100 | if (wpa_receive_error_report( |
| 1101 | wpa_auth, sm, |
| 1102 | !(key_info & WPA_KEY_INFO_KEY_TYPE)) > 0) |
| 1103 | return; /* STA entry was removed */ |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1104 | } else if (key_info & WPA_KEY_INFO_KEY_TYPE) { |
| 1105 | wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO, |
| 1106 | "received EAPOL-Key Request for new " |
| 1107 | "4-Way Handshake"); |
| 1108 | wpa_request_new_ptk(sm); |
| 1109 | #ifdef CONFIG_PEERKEY |
| 1110 | } else if (msg == SMK_M1) { |
| 1111 | wpa_smk_m1(wpa_auth, sm, key); |
| 1112 | #endif /* CONFIG_PEERKEY */ |
| 1113 | } else if (key_data_length > 0 && |
| 1114 | wpa_parse_kde_ies((const u8 *) (key + 1), |
| 1115 | key_data_length, &kde) == 0 && |
| 1116 | kde.mac_addr) { |
| 1117 | } else { |
| 1118 | wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO, |
| 1119 | "received EAPOL-Key Request for GTK " |
| 1120 | "rekeying"); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1121 | eloop_cancel_timeout(wpa_rekey_gtk, wpa_auth, NULL); |
| 1122 | wpa_rekey_gtk(wpa_auth, NULL); |
| 1123 | } |
| 1124 | } else { |
Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 1125 | /* Do not allow the same key replay counter to be reused. */ |
| 1126 | wpa_replay_counter_mark_invalid(sm->key_replay, |
| 1127 | key->replay_counter); |
| 1128 | |
| 1129 | if (msg == PAIRWISE_2) { |
| 1130 | /* |
| 1131 | * Maintain a copy of the pending EAPOL-Key frames in |
| 1132 | * case the EAPOL-Key frame was retransmitted. This is |
| 1133 | * needed to allow EAPOL-Key msg 2/4 reply to another |
| 1134 | * pending msg 1/4 to update the SNonce to work around |
| 1135 | * unexpected supplicant behavior. |
| 1136 | */ |
| 1137 | os_memcpy(sm->prev_key_replay, sm->key_replay, |
| 1138 | sizeof(sm->key_replay)); |
| 1139 | } else { |
| 1140 | os_memset(sm->prev_key_replay, 0, |
| 1141 | sizeof(sm->prev_key_replay)); |
| 1142 | } |
| 1143 | |
| 1144 | /* |
| 1145 | * Make sure old valid counters are not accepted anymore and |
| 1146 | * do not get copied again. |
| 1147 | */ |
| 1148 | wpa_replay_counter_mark_invalid(sm->key_replay, NULL); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1149 | } |
| 1150 | |
| 1151 | #ifdef CONFIG_PEERKEY |
| 1152 | if (msg == SMK_M3) { |
| 1153 | wpa_smk_m3(wpa_auth, sm, key); |
| 1154 | return; |
| 1155 | } |
| 1156 | #endif /* CONFIG_PEERKEY */ |
| 1157 | |
| 1158 | os_free(sm->last_rx_eapol_key); |
| 1159 | sm->last_rx_eapol_key = os_malloc(data_len); |
| 1160 | if (sm->last_rx_eapol_key == NULL) |
| 1161 | return; |
| 1162 | os_memcpy(sm->last_rx_eapol_key, data, data_len); |
| 1163 | sm->last_rx_eapol_key_len = data_len; |
| 1164 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 1165 | sm->rx_eapol_key_secure = !!(key_info & WPA_KEY_INFO_SECURE); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1166 | sm->EAPOLKeyReceived = TRUE; |
| 1167 | sm->EAPOLKeyPairwise = !!(key_info & WPA_KEY_INFO_KEY_TYPE); |
| 1168 | sm->EAPOLKeyRequest = !!(key_info & WPA_KEY_INFO_REQUEST); |
| 1169 | os_memcpy(sm->SNonce, key->key_nonce, WPA_NONCE_LEN); |
| 1170 | wpa_sm_step(sm); |
| 1171 | } |
| 1172 | |
| 1173 | |
| 1174 | static int wpa_gmk_to_gtk(const u8 *gmk, const char *label, const u8 *addr, |
| 1175 | const u8 *gnonce, u8 *gtk, size_t gtk_len) |
| 1176 | { |
| 1177 | u8 data[ETH_ALEN + WPA_NONCE_LEN + 8 + 16]; |
| 1178 | u8 *pos; |
| 1179 | int ret = 0; |
| 1180 | |
| 1181 | /* GTK = PRF-X(GMK, "Group key expansion", |
| 1182 | * AA || GNonce || Time || random data) |
| 1183 | * The example described in the IEEE 802.11 standard uses only AA and |
| 1184 | * GNonce as inputs here. Add some more entropy since this derivation |
| 1185 | * is done only at the Authenticator and as such, does not need to be |
| 1186 | * exactly same. |
| 1187 | */ |
| 1188 | os_memcpy(data, addr, ETH_ALEN); |
| 1189 | os_memcpy(data + ETH_ALEN, gnonce, WPA_NONCE_LEN); |
| 1190 | pos = data + ETH_ALEN + WPA_NONCE_LEN; |
| 1191 | wpa_get_ntp_timestamp(pos); |
| 1192 | pos += 8; |
| 1193 | if (random_get_bytes(pos, 16) < 0) |
| 1194 | ret = -1; |
| 1195 | |
| 1196 | #ifdef CONFIG_IEEE80211W |
| 1197 | sha256_prf(gmk, WPA_GMK_LEN, label, data, sizeof(data), gtk, gtk_len); |
| 1198 | #else /* CONFIG_IEEE80211W */ |
| 1199 | if (sha1_prf(gmk, WPA_GMK_LEN, label, data, sizeof(data), gtk, gtk_len) |
| 1200 | < 0) |
| 1201 | ret = -1; |
| 1202 | #endif /* CONFIG_IEEE80211W */ |
| 1203 | |
| 1204 | return ret; |
| 1205 | } |
| 1206 | |
| 1207 | |
| 1208 | static void wpa_send_eapol_timeout(void *eloop_ctx, void *timeout_ctx) |
| 1209 | { |
| 1210 | struct wpa_authenticator *wpa_auth = eloop_ctx; |
| 1211 | struct wpa_state_machine *sm = timeout_ctx; |
| 1212 | |
| 1213 | sm->pending_1_of_4_timeout = 0; |
| 1214 | wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG, "EAPOL-Key timeout"); |
| 1215 | sm->TimeoutEvt = TRUE; |
| 1216 | wpa_sm_step(sm); |
| 1217 | } |
| 1218 | |
| 1219 | |
| 1220 | void __wpa_send_eapol(struct wpa_authenticator *wpa_auth, |
| 1221 | struct wpa_state_machine *sm, int key_info, |
| 1222 | const u8 *key_rsc, const u8 *nonce, |
| 1223 | const u8 *kde, size_t kde_len, |
| 1224 | int keyidx, int encr, int force_version) |
| 1225 | { |
| 1226 | struct ieee802_1x_hdr *hdr; |
| 1227 | struct wpa_eapol_key *key; |
| 1228 | size_t len; |
| 1229 | int alg; |
| 1230 | int key_data_len, pad_len = 0; |
| 1231 | u8 *buf, *pos; |
| 1232 | int version, pairwise; |
| 1233 | int i; |
| 1234 | |
| 1235 | len = sizeof(struct ieee802_1x_hdr) + sizeof(struct wpa_eapol_key); |
| 1236 | |
| 1237 | if (force_version) |
| 1238 | version = force_version; |
| 1239 | else if (wpa_use_aes_cmac(sm)) |
| 1240 | version = WPA_KEY_INFO_TYPE_AES_128_CMAC; |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 1241 | else if (sm->pairwise != WPA_CIPHER_TKIP) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1242 | version = WPA_KEY_INFO_TYPE_HMAC_SHA1_AES; |
| 1243 | else |
| 1244 | version = WPA_KEY_INFO_TYPE_HMAC_MD5_RC4; |
| 1245 | |
Dmitry Shmidt | 51b6ea8 | 2013-05-08 10:42:09 -0700 | [diff] [blame] | 1246 | pairwise = !!(key_info & WPA_KEY_INFO_KEY_TYPE); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1247 | |
| 1248 | wpa_printf(MSG_DEBUG, "WPA: Send EAPOL(version=%d secure=%d mic=%d " |
| 1249 | "ack=%d install=%d pairwise=%d kde_len=%lu keyidx=%d " |
| 1250 | "encr=%d)", |
| 1251 | version, |
| 1252 | (key_info & WPA_KEY_INFO_SECURE) ? 1 : 0, |
| 1253 | (key_info & WPA_KEY_INFO_MIC) ? 1 : 0, |
| 1254 | (key_info & WPA_KEY_INFO_ACK) ? 1 : 0, |
| 1255 | (key_info & WPA_KEY_INFO_INSTALL) ? 1 : 0, |
| 1256 | pairwise, (unsigned long) kde_len, keyidx, encr); |
| 1257 | |
| 1258 | key_data_len = kde_len; |
| 1259 | |
| 1260 | if ((version == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES || |
| 1261 | version == WPA_KEY_INFO_TYPE_AES_128_CMAC) && encr) { |
| 1262 | pad_len = key_data_len % 8; |
| 1263 | if (pad_len) |
| 1264 | pad_len = 8 - pad_len; |
| 1265 | key_data_len += pad_len + 8; |
| 1266 | } |
| 1267 | |
| 1268 | len += key_data_len; |
| 1269 | |
| 1270 | hdr = os_zalloc(len); |
| 1271 | if (hdr == NULL) |
| 1272 | return; |
| 1273 | hdr->version = wpa_auth->conf.eapol_version; |
| 1274 | hdr->type = IEEE802_1X_TYPE_EAPOL_KEY; |
| 1275 | hdr->length = host_to_be16(len - sizeof(*hdr)); |
| 1276 | key = (struct wpa_eapol_key *) (hdr + 1); |
| 1277 | |
| 1278 | key->type = sm->wpa == WPA_VERSION_WPA2 ? |
| 1279 | EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA; |
| 1280 | key_info |= version; |
| 1281 | if (encr && sm->wpa == WPA_VERSION_WPA2) |
| 1282 | key_info |= WPA_KEY_INFO_ENCR_KEY_DATA; |
| 1283 | if (sm->wpa != WPA_VERSION_WPA2) |
| 1284 | key_info |= keyidx << WPA_KEY_INFO_KEY_INDEX_SHIFT; |
| 1285 | WPA_PUT_BE16(key->key_info, key_info); |
| 1286 | |
| 1287 | alg = pairwise ? sm->pairwise : wpa_auth->conf.wpa_group; |
Dmitry Shmidt | 4530cfd | 2012-09-09 15:20:40 -0700 | [diff] [blame] | 1288 | WPA_PUT_BE16(key->key_length, wpa_cipher_key_len(alg)); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1289 | if (key_info & WPA_KEY_INFO_SMK_MESSAGE) |
| 1290 | WPA_PUT_BE16(key->key_length, 0); |
| 1291 | |
| 1292 | /* FIX: STSL: what to use as key_replay_counter? */ |
| 1293 | for (i = RSNA_MAX_EAPOL_RETRIES - 1; i > 0; i--) { |
| 1294 | sm->key_replay[i].valid = sm->key_replay[i - 1].valid; |
| 1295 | os_memcpy(sm->key_replay[i].counter, |
| 1296 | sm->key_replay[i - 1].counter, |
| 1297 | WPA_REPLAY_COUNTER_LEN); |
| 1298 | } |
| 1299 | inc_byte_array(sm->key_replay[0].counter, WPA_REPLAY_COUNTER_LEN); |
| 1300 | os_memcpy(key->replay_counter, sm->key_replay[0].counter, |
| 1301 | WPA_REPLAY_COUNTER_LEN); |
| 1302 | sm->key_replay[0].valid = TRUE; |
| 1303 | |
| 1304 | if (nonce) |
| 1305 | os_memcpy(key->key_nonce, nonce, WPA_NONCE_LEN); |
| 1306 | |
| 1307 | if (key_rsc) |
| 1308 | os_memcpy(key->key_rsc, key_rsc, WPA_KEY_RSC_LEN); |
| 1309 | |
| 1310 | if (kde && !encr) { |
| 1311 | os_memcpy(key + 1, kde, kde_len); |
| 1312 | WPA_PUT_BE16(key->key_data_length, kde_len); |
| 1313 | } else if (encr && kde) { |
| 1314 | buf = os_zalloc(key_data_len); |
| 1315 | if (buf == NULL) { |
| 1316 | os_free(hdr); |
| 1317 | return; |
| 1318 | } |
| 1319 | pos = buf; |
| 1320 | os_memcpy(pos, kde, kde_len); |
| 1321 | pos += kde_len; |
| 1322 | |
| 1323 | if (pad_len) |
| 1324 | *pos++ = 0xdd; |
| 1325 | |
| 1326 | wpa_hexdump_key(MSG_DEBUG, "Plaintext EAPOL-Key Key Data", |
| 1327 | buf, key_data_len); |
| 1328 | if (version == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES || |
| 1329 | version == WPA_KEY_INFO_TYPE_AES_128_CMAC) { |
| 1330 | if (aes_wrap(sm->PTK.kek, (key_data_len - 8) / 8, buf, |
| 1331 | (u8 *) (key + 1))) { |
| 1332 | os_free(hdr); |
| 1333 | os_free(buf); |
| 1334 | return; |
| 1335 | } |
| 1336 | WPA_PUT_BE16(key->key_data_length, key_data_len); |
| 1337 | } else { |
| 1338 | u8 ek[32]; |
| 1339 | os_memcpy(key->key_iv, |
| 1340 | sm->group->Counter + WPA_NONCE_LEN - 16, 16); |
| 1341 | inc_byte_array(sm->group->Counter, WPA_NONCE_LEN); |
| 1342 | os_memcpy(ek, key->key_iv, 16); |
| 1343 | os_memcpy(ek + 16, sm->PTK.kek, 16); |
| 1344 | os_memcpy(key + 1, buf, key_data_len); |
| 1345 | rc4_skip(ek, 32, 256, (u8 *) (key + 1), key_data_len); |
| 1346 | WPA_PUT_BE16(key->key_data_length, key_data_len); |
| 1347 | } |
| 1348 | os_free(buf); |
| 1349 | } |
| 1350 | |
| 1351 | if (key_info & WPA_KEY_INFO_MIC) { |
| 1352 | if (!sm->PTK_valid) { |
| 1353 | wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG, |
| 1354 | "PTK not valid when sending EAPOL-Key " |
| 1355 | "frame"); |
| 1356 | os_free(hdr); |
| 1357 | return; |
| 1358 | } |
| 1359 | wpa_eapol_key_mic(sm->PTK.kck, version, (u8 *) hdr, len, |
| 1360 | key->key_mic); |
Dmitry Shmidt | 51b6ea8 | 2013-05-08 10:42:09 -0700 | [diff] [blame] | 1361 | #ifdef CONFIG_TESTING_OPTIONS |
| 1362 | if (!pairwise && |
| 1363 | wpa_auth->conf.corrupt_gtk_rekey_mic_probability > 0.0d && |
| 1364 | drand48() < |
| 1365 | wpa_auth->conf.corrupt_gtk_rekey_mic_probability) { |
| 1366 | wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO, |
| 1367 | "Corrupting group EAPOL-Key Key MIC"); |
| 1368 | key->key_mic[0]++; |
| 1369 | } |
| 1370 | #endif /* CONFIG_TESTING_OPTIONS */ |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1371 | } |
| 1372 | |
| 1373 | wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_inc_EapolFramesTx, |
| 1374 | 1); |
| 1375 | wpa_auth_send_eapol(wpa_auth, sm->addr, (u8 *) hdr, len, |
| 1376 | sm->pairwise_set); |
| 1377 | os_free(hdr); |
| 1378 | } |
| 1379 | |
| 1380 | |
| 1381 | static void wpa_send_eapol(struct wpa_authenticator *wpa_auth, |
| 1382 | struct wpa_state_machine *sm, int key_info, |
| 1383 | const u8 *key_rsc, const u8 *nonce, |
| 1384 | const u8 *kde, size_t kde_len, |
| 1385 | int keyidx, int encr) |
| 1386 | { |
| 1387 | int timeout_ms; |
| 1388 | int pairwise = key_info & WPA_KEY_INFO_KEY_TYPE; |
| 1389 | int ctr; |
| 1390 | |
| 1391 | if (sm == NULL) |
| 1392 | return; |
| 1393 | |
| 1394 | __wpa_send_eapol(wpa_auth, sm, key_info, key_rsc, nonce, kde, kde_len, |
| 1395 | keyidx, encr, 0); |
| 1396 | |
| 1397 | ctr = pairwise ? sm->TimeoutCtr : sm->GTimeoutCtr; |
| 1398 | if (ctr == 1 && wpa_auth->conf.tx_status) |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 1399 | timeout_ms = pairwise ? eapol_key_timeout_first : |
| 1400 | eapol_key_timeout_first_group; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1401 | else |
| 1402 | timeout_ms = eapol_key_timeout_subseq; |
| 1403 | if (pairwise && ctr == 1 && !(key_info & WPA_KEY_INFO_MIC)) |
| 1404 | sm->pending_1_of_4_timeout = 1; |
| 1405 | wpa_printf(MSG_DEBUG, "WPA: Use EAPOL-Key timeout of %u ms (retry " |
| 1406 | "counter %d)", timeout_ms, ctr); |
| 1407 | eloop_register_timeout(timeout_ms / 1000, (timeout_ms % 1000) * 1000, |
| 1408 | wpa_send_eapol_timeout, wpa_auth, sm); |
| 1409 | } |
| 1410 | |
| 1411 | |
| 1412 | static int wpa_verify_key_mic(struct wpa_ptk *PTK, u8 *data, size_t data_len) |
| 1413 | { |
| 1414 | struct ieee802_1x_hdr *hdr; |
| 1415 | struct wpa_eapol_key *key; |
| 1416 | u16 key_info; |
| 1417 | int ret = 0; |
| 1418 | u8 mic[16]; |
| 1419 | |
| 1420 | if (data_len < sizeof(*hdr) + sizeof(*key)) |
| 1421 | return -1; |
| 1422 | |
| 1423 | hdr = (struct ieee802_1x_hdr *) data; |
| 1424 | key = (struct wpa_eapol_key *) (hdr + 1); |
| 1425 | key_info = WPA_GET_BE16(key->key_info); |
| 1426 | os_memcpy(mic, key->key_mic, 16); |
| 1427 | os_memset(key->key_mic, 0, 16); |
| 1428 | if (wpa_eapol_key_mic(PTK->kck, key_info & WPA_KEY_INFO_TYPE_MASK, |
| 1429 | data, data_len, key->key_mic) || |
| 1430 | os_memcmp(mic, key->key_mic, 16) != 0) |
| 1431 | ret = -1; |
| 1432 | os_memcpy(key->key_mic, mic, 16); |
| 1433 | return ret; |
| 1434 | } |
| 1435 | |
| 1436 | |
| 1437 | void wpa_remove_ptk(struct wpa_state_machine *sm) |
| 1438 | { |
| 1439 | sm->PTK_valid = FALSE; |
| 1440 | os_memset(&sm->PTK, 0, sizeof(sm->PTK)); |
| 1441 | wpa_auth_set_key(sm->wpa_auth, 0, WPA_ALG_NONE, sm->addr, 0, NULL, 0); |
| 1442 | sm->pairwise_set = FALSE; |
| 1443 | eloop_cancel_timeout(wpa_rekey_ptk, sm->wpa_auth, sm); |
| 1444 | } |
| 1445 | |
| 1446 | |
| 1447 | int wpa_auth_sm_event(struct wpa_state_machine *sm, wpa_event event) |
| 1448 | { |
| 1449 | int remove_ptk = 1; |
| 1450 | |
| 1451 | if (sm == NULL) |
| 1452 | return -1; |
| 1453 | |
| 1454 | wpa_auth_vlogger(sm->wpa_auth, sm->addr, LOGGER_DEBUG, |
| 1455 | "event %d notification", event); |
| 1456 | |
| 1457 | switch (event) { |
| 1458 | case WPA_AUTH: |
| 1459 | case WPA_ASSOC: |
| 1460 | break; |
| 1461 | case WPA_DEAUTH: |
| 1462 | case WPA_DISASSOC: |
| 1463 | sm->DeauthenticationRequest = TRUE; |
| 1464 | break; |
| 1465 | case WPA_REAUTH: |
| 1466 | case WPA_REAUTH_EAPOL: |
| 1467 | if (!sm->started) { |
| 1468 | /* |
| 1469 | * When using WPS, we may end up here if the STA |
| 1470 | * manages to re-associate without the previous STA |
| 1471 | * entry getting removed. Consequently, we need to make |
| 1472 | * sure that the WPA state machines gets initialized |
| 1473 | * properly at this point. |
| 1474 | */ |
| 1475 | wpa_printf(MSG_DEBUG, "WPA state machine had not been " |
| 1476 | "started - initialize now"); |
| 1477 | sm->started = 1; |
| 1478 | sm->Init = TRUE; |
| 1479 | if (wpa_sm_step(sm) == 1) |
| 1480 | return 1; /* should not really happen */ |
| 1481 | sm->Init = FALSE; |
| 1482 | sm->AuthenticationRequest = TRUE; |
| 1483 | break; |
| 1484 | } |
| 1485 | if (sm->GUpdateStationKeys) { |
| 1486 | /* |
| 1487 | * Reauthentication cancels the pending group key |
| 1488 | * update for this STA. |
| 1489 | */ |
| 1490 | sm->group->GKeyDoneStations--; |
| 1491 | sm->GUpdateStationKeys = FALSE; |
| 1492 | sm->PtkGroupInit = TRUE; |
| 1493 | } |
| 1494 | sm->ReAuthenticationRequest = TRUE; |
| 1495 | break; |
| 1496 | case WPA_ASSOC_FT: |
| 1497 | #ifdef CONFIG_IEEE80211R |
| 1498 | wpa_printf(MSG_DEBUG, "FT: Retry PTK configuration " |
| 1499 | "after association"); |
| 1500 | wpa_ft_install_ptk(sm); |
| 1501 | |
| 1502 | /* Using FT protocol, not WPA auth state machine */ |
| 1503 | sm->ft_completed = 1; |
| 1504 | return 0; |
| 1505 | #else /* CONFIG_IEEE80211R */ |
| 1506 | break; |
| 1507 | #endif /* CONFIG_IEEE80211R */ |
| 1508 | } |
| 1509 | |
| 1510 | #ifdef CONFIG_IEEE80211R |
| 1511 | sm->ft_completed = 0; |
| 1512 | #endif /* CONFIG_IEEE80211R */ |
| 1513 | |
| 1514 | #ifdef CONFIG_IEEE80211W |
| 1515 | if (sm->mgmt_frame_prot && event == WPA_AUTH) |
| 1516 | remove_ptk = 0; |
| 1517 | #endif /* CONFIG_IEEE80211W */ |
| 1518 | |
| 1519 | if (remove_ptk) { |
| 1520 | sm->PTK_valid = FALSE; |
| 1521 | os_memset(&sm->PTK, 0, sizeof(sm->PTK)); |
| 1522 | |
| 1523 | if (event != WPA_REAUTH_EAPOL) |
| 1524 | wpa_remove_ptk(sm); |
| 1525 | } |
| 1526 | |
| 1527 | return wpa_sm_step(sm); |
| 1528 | } |
| 1529 | |
| 1530 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1531 | SM_STATE(WPA_PTK, INITIALIZE) |
| 1532 | { |
| 1533 | SM_ENTRY_MA(WPA_PTK, INITIALIZE, wpa_ptk); |
| 1534 | if (sm->Init) { |
| 1535 | /* Init flag is not cleared here, so avoid busy |
| 1536 | * loop by claiming nothing changed. */ |
| 1537 | sm->changed = FALSE; |
| 1538 | } |
| 1539 | |
| 1540 | sm->keycount = 0; |
| 1541 | if (sm->GUpdateStationKeys) |
| 1542 | sm->group->GKeyDoneStations--; |
| 1543 | sm->GUpdateStationKeys = FALSE; |
| 1544 | if (sm->wpa == WPA_VERSION_WPA) |
| 1545 | sm->PInitAKeys = FALSE; |
| 1546 | if (1 /* Unicast cipher supported AND (ESS OR ((IBSS or WDS) and |
| 1547 | * Local AA > Remote AA)) */) { |
| 1548 | sm->Pair = TRUE; |
| 1549 | } |
| 1550 | wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portEnabled, 0); |
| 1551 | wpa_remove_ptk(sm); |
| 1552 | wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portValid, 0); |
| 1553 | sm->TimeoutCtr = 0; |
| 1554 | if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt)) { |
| 1555 | wpa_auth_set_eapol(sm->wpa_auth, sm->addr, |
| 1556 | WPA_EAPOL_authorized, 0); |
| 1557 | } |
| 1558 | } |
| 1559 | |
| 1560 | |
| 1561 | SM_STATE(WPA_PTK, DISCONNECT) |
| 1562 | { |
| 1563 | SM_ENTRY_MA(WPA_PTK, DISCONNECT, wpa_ptk); |
| 1564 | sm->Disconnect = FALSE; |
| 1565 | wpa_sta_disconnect(sm->wpa_auth, sm->addr); |
| 1566 | } |
| 1567 | |
| 1568 | |
| 1569 | SM_STATE(WPA_PTK, DISCONNECTED) |
| 1570 | { |
| 1571 | SM_ENTRY_MA(WPA_PTK, DISCONNECTED, wpa_ptk); |
| 1572 | sm->DeauthenticationRequest = FALSE; |
| 1573 | } |
| 1574 | |
| 1575 | |
| 1576 | SM_STATE(WPA_PTK, AUTHENTICATION) |
| 1577 | { |
| 1578 | SM_ENTRY_MA(WPA_PTK, AUTHENTICATION, wpa_ptk); |
| 1579 | os_memset(&sm->PTK, 0, sizeof(sm->PTK)); |
| 1580 | sm->PTK_valid = FALSE; |
| 1581 | wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portControl_Auto, |
| 1582 | 1); |
| 1583 | wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portEnabled, 1); |
| 1584 | sm->AuthenticationRequest = FALSE; |
| 1585 | } |
| 1586 | |
| 1587 | |
Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 1588 | static void wpa_group_ensure_init(struct wpa_authenticator *wpa_auth, |
| 1589 | struct wpa_group *group) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1590 | { |
Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 1591 | if (group->first_sta_seen) |
| 1592 | return; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1593 | /* |
| 1594 | * System has run bit further than at the time hostapd was started |
| 1595 | * potentially very early during boot up. This provides better chances |
| 1596 | * of collecting more randomness on embedded systems. Re-initialize the |
| 1597 | * GMK and Counter here to improve their strength if there was not |
| 1598 | * enough entropy available immediately after system startup. |
| 1599 | */ |
| 1600 | wpa_printf(MSG_DEBUG, "WPA: Re-initialize GMK/Counter on first " |
| 1601 | "station"); |
| 1602 | if (random_pool_ready() != 1) { |
| 1603 | wpa_printf(MSG_INFO, "WPA: Not enough entropy in random pool " |
| 1604 | "to proceed - reject first 4-way handshake"); |
| 1605 | group->reject_4way_hs_for_entropy = TRUE; |
Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 1606 | } else { |
| 1607 | group->first_sta_seen = TRUE; |
| 1608 | group->reject_4way_hs_for_entropy = FALSE; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1609 | } |
Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 1610 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1611 | wpa_group_init_gmk_and_counter(wpa_auth, group); |
| 1612 | wpa_gtk_update(wpa_auth, group); |
| 1613 | wpa_group_config_group_keys(wpa_auth, group); |
| 1614 | } |
| 1615 | |
| 1616 | |
| 1617 | SM_STATE(WPA_PTK, AUTHENTICATION2) |
| 1618 | { |
| 1619 | SM_ENTRY_MA(WPA_PTK, AUTHENTICATION2, wpa_ptk); |
| 1620 | |
Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 1621 | wpa_group_ensure_init(sm->wpa_auth, sm->group); |
Dmitry Shmidt | 444d567 | 2013-04-01 13:08:44 -0700 | [diff] [blame] | 1622 | sm->ReAuthenticationRequest = FALSE; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1623 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 1624 | /* |
| 1625 | * Definition of ANonce selection in IEEE Std 802.11i-2004 is somewhat |
| 1626 | * ambiguous. The Authenticator state machine uses a counter that is |
| 1627 | * incremented by one for each 4-way handshake. However, the security |
| 1628 | * analysis of 4-way handshake points out that unpredictable nonces |
| 1629 | * help in preventing precomputation attacks. Instead of the state |
| 1630 | * machine definition, use an unpredictable nonce value here to provide |
| 1631 | * stronger protection against potential precomputation attacks. |
| 1632 | */ |
| 1633 | if (random_get_bytes(sm->ANonce, WPA_NONCE_LEN)) { |
| 1634 | wpa_printf(MSG_ERROR, "WPA: Failed to get random data for " |
| 1635 | "ANonce."); |
Dmitry Shmidt | 444d567 | 2013-04-01 13:08:44 -0700 | [diff] [blame] | 1636 | sm->Disconnect = TRUE; |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 1637 | return; |
| 1638 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1639 | wpa_hexdump(MSG_DEBUG, "WPA: Assign ANonce", sm->ANonce, |
| 1640 | WPA_NONCE_LEN); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1641 | /* IEEE 802.11i does not clear TimeoutCtr here, but this is more |
| 1642 | * logical place than INITIALIZE since AUTHENTICATION2 can be |
| 1643 | * re-entered on ReAuthenticationRequest without going through |
| 1644 | * INITIALIZE. */ |
| 1645 | sm->TimeoutCtr = 0; |
| 1646 | } |
| 1647 | |
| 1648 | |
| 1649 | SM_STATE(WPA_PTK, INITPMK) |
| 1650 | { |
| 1651 | u8 msk[2 * PMK_LEN]; |
| 1652 | size_t len = 2 * PMK_LEN; |
| 1653 | |
| 1654 | SM_ENTRY_MA(WPA_PTK, INITPMK, wpa_ptk); |
| 1655 | #ifdef CONFIG_IEEE80211R |
| 1656 | sm->xxkey_len = 0; |
| 1657 | #endif /* CONFIG_IEEE80211R */ |
| 1658 | if (sm->pmksa) { |
| 1659 | wpa_printf(MSG_DEBUG, "WPA: PMK from PMKSA cache"); |
| 1660 | os_memcpy(sm->PMK, sm->pmksa->pmk, PMK_LEN); |
| 1661 | } else if (wpa_auth_get_msk(sm->wpa_auth, sm->addr, msk, &len) == 0) { |
| 1662 | wpa_printf(MSG_DEBUG, "WPA: PMK from EAPOL state machine " |
| 1663 | "(len=%lu)", (unsigned long) len); |
| 1664 | os_memcpy(sm->PMK, msk, PMK_LEN); |
| 1665 | #ifdef CONFIG_IEEE80211R |
| 1666 | if (len >= 2 * PMK_LEN) { |
| 1667 | os_memcpy(sm->xxkey, msk + PMK_LEN, PMK_LEN); |
| 1668 | sm->xxkey_len = PMK_LEN; |
| 1669 | } |
| 1670 | #endif /* CONFIG_IEEE80211R */ |
| 1671 | } else { |
| 1672 | wpa_printf(MSG_DEBUG, "WPA: Could not get PMK"); |
| 1673 | } |
| 1674 | |
| 1675 | sm->req_replay_counter_used = 0; |
| 1676 | /* IEEE 802.11i does not set keyRun to FALSE, but not doing this |
| 1677 | * will break reauthentication since EAPOL state machines may not be |
| 1678 | * get into AUTHENTICATING state that clears keyRun before WPA state |
| 1679 | * machine enters AUTHENTICATION2 state and goes immediately to INITPMK |
| 1680 | * state and takes PMK from the previously used AAA Key. This will |
| 1681 | * eventually fail in 4-Way Handshake because Supplicant uses PMK |
| 1682 | * derived from the new AAA Key. Setting keyRun = FALSE here seems to |
| 1683 | * be good workaround for this issue. */ |
| 1684 | wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_keyRun, 0); |
| 1685 | } |
| 1686 | |
| 1687 | |
| 1688 | SM_STATE(WPA_PTK, INITPSK) |
| 1689 | { |
| 1690 | const u8 *psk; |
| 1691 | SM_ENTRY_MA(WPA_PTK, INITPSK, wpa_ptk); |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 1692 | psk = wpa_auth_get_psk(sm->wpa_auth, sm->addr, sm->p2p_dev_addr, NULL); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1693 | if (psk) { |
| 1694 | os_memcpy(sm->PMK, psk, PMK_LEN); |
| 1695 | #ifdef CONFIG_IEEE80211R |
| 1696 | os_memcpy(sm->xxkey, psk, PMK_LEN); |
| 1697 | sm->xxkey_len = PMK_LEN; |
| 1698 | #endif /* CONFIG_IEEE80211R */ |
| 1699 | } |
| 1700 | sm->req_replay_counter_used = 0; |
| 1701 | } |
| 1702 | |
| 1703 | |
| 1704 | SM_STATE(WPA_PTK, PTKSTART) |
| 1705 | { |
| 1706 | u8 buf[2 + RSN_SELECTOR_LEN + PMKID_LEN], *pmkid = NULL; |
| 1707 | size_t pmkid_len = 0; |
| 1708 | |
| 1709 | SM_ENTRY_MA(WPA_PTK, PTKSTART, wpa_ptk); |
| 1710 | sm->PTKRequest = FALSE; |
| 1711 | sm->TimeoutEvt = FALSE; |
| 1712 | |
| 1713 | sm->TimeoutCtr++; |
| 1714 | if (sm->TimeoutCtr > (int) dot11RSNAConfigPairwiseUpdateCount) { |
| 1715 | /* No point in sending the EAPOL-Key - we will disconnect |
| 1716 | * immediately following this. */ |
| 1717 | return; |
| 1718 | } |
| 1719 | |
| 1720 | wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG, |
| 1721 | "sending 1/4 msg of 4-Way Handshake"); |
| 1722 | /* |
| 1723 | * TODO: Could add PMKID even with WPA2-PSK, but only if there is only |
| 1724 | * one possible PSK for this STA. |
| 1725 | */ |
| 1726 | if (sm->wpa == WPA_VERSION_WPA2 && |
| 1727 | wpa_key_mgmt_wpa_ieee8021x(sm->wpa_key_mgmt)) { |
| 1728 | pmkid = buf; |
| 1729 | pmkid_len = 2 + RSN_SELECTOR_LEN + PMKID_LEN; |
| 1730 | pmkid[0] = WLAN_EID_VENDOR_SPECIFIC; |
| 1731 | pmkid[1] = RSN_SELECTOR_LEN + PMKID_LEN; |
| 1732 | RSN_SELECTOR_PUT(&pmkid[2], RSN_KEY_DATA_PMKID); |
| 1733 | if (sm->pmksa) |
| 1734 | os_memcpy(&pmkid[2 + RSN_SELECTOR_LEN], |
| 1735 | sm->pmksa->pmkid, PMKID_LEN); |
| 1736 | else { |
| 1737 | /* |
| 1738 | * Calculate PMKID since no PMKSA cache entry was |
| 1739 | * available with pre-calculated PMKID. |
| 1740 | */ |
| 1741 | rsn_pmkid(sm->PMK, PMK_LEN, sm->wpa_auth->addr, |
| 1742 | sm->addr, &pmkid[2 + RSN_SELECTOR_LEN], |
| 1743 | wpa_key_mgmt_sha256(sm->wpa_key_mgmt)); |
| 1744 | } |
| 1745 | } |
| 1746 | wpa_send_eapol(sm->wpa_auth, sm, |
| 1747 | WPA_KEY_INFO_ACK | WPA_KEY_INFO_KEY_TYPE, NULL, |
| 1748 | sm->ANonce, pmkid, pmkid_len, 0, 0); |
| 1749 | } |
| 1750 | |
| 1751 | |
| 1752 | static int wpa_derive_ptk(struct wpa_state_machine *sm, const u8 *pmk, |
| 1753 | struct wpa_ptk *ptk) |
| 1754 | { |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 1755 | size_t ptk_len = sm->pairwise != WPA_CIPHER_TKIP ? 48 : 64; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1756 | #ifdef CONFIG_IEEE80211R |
| 1757 | if (wpa_key_mgmt_ft(sm->wpa_key_mgmt)) |
| 1758 | return wpa_auth_derive_ptk_ft(sm, pmk, ptk, ptk_len); |
| 1759 | #endif /* CONFIG_IEEE80211R */ |
| 1760 | |
| 1761 | wpa_pmk_to_ptk(pmk, PMK_LEN, "Pairwise key expansion", |
| 1762 | sm->wpa_auth->addr, sm->addr, sm->ANonce, sm->SNonce, |
| 1763 | (u8 *) ptk, ptk_len, |
| 1764 | wpa_key_mgmt_sha256(sm->wpa_key_mgmt)); |
| 1765 | |
| 1766 | return 0; |
| 1767 | } |
| 1768 | |
| 1769 | |
| 1770 | SM_STATE(WPA_PTK, PTKCALCNEGOTIATING) |
| 1771 | { |
| 1772 | struct wpa_ptk PTK; |
| 1773 | int ok = 0; |
| 1774 | const u8 *pmk = NULL; |
| 1775 | |
| 1776 | SM_ENTRY_MA(WPA_PTK, PTKCALCNEGOTIATING, wpa_ptk); |
| 1777 | sm->EAPOLKeyReceived = FALSE; |
Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 1778 | sm->update_snonce = FALSE; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1779 | |
| 1780 | /* WPA with IEEE 802.1X: use the derived PMK from EAP |
| 1781 | * WPA-PSK: iterate through possible PSKs and select the one matching |
| 1782 | * the packet */ |
| 1783 | for (;;) { |
| 1784 | if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt)) { |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 1785 | pmk = wpa_auth_get_psk(sm->wpa_auth, sm->addr, |
| 1786 | sm->p2p_dev_addr, pmk); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1787 | if (pmk == NULL) |
| 1788 | break; |
| 1789 | } else |
| 1790 | pmk = sm->PMK; |
| 1791 | |
| 1792 | wpa_derive_ptk(sm, pmk, &PTK); |
| 1793 | |
| 1794 | if (wpa_verify_key_mic(&PTK, sm->last_rx_eapol_key, |
| 1795 | sm->last_rx_eapol_key_len) == 0) { |
| 1796 | ok = 1; |
| 1797 | break; |
| 1798 | } |
| 1799 | |
| 1800 | if (!wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt)) |
| 1801 | break; |
| 1802 | } |
| 1803 | |
| 1804 | if (!ok) { |
| 1805 | wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG, |
| 1806 | "invalid MIC in msg 2/4 of 4-Way Handshake"); |
| 1807 | return; |
| 1808 | } |
| 1809 | |
| 1810 | #ifdef CONFIG_IEEE80211R |
| 1811 | if (sm->wpa == WPA_VERSION_WPA2 && wpa_key_mgmt_ft(sm->wpa_key_mgmt)) { |
| 1812 | /* |
| 1813 | * Verify that PMKR1Name from EAPOL-Key message 2/4 matches |
| 1814 | * with the value we derived. |
| 1815 | */ |
| 1816 | if (os_memcmp(sm->sup_pmk_r1_name, sm->pmk_r1_name, |
| 1817 | WPA_PMK_NAME_LEN) != 0) { |
| 1818 | wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG, |
| 1819 | "PMKR1Name mismatch in FT 4-way " |
| 1820 | "handshake"); |
| 1821 | wpa_hexdump(MSG_DEBUG, "FT: PMKR1Name from " |
| 1822 | "Supplicant", |
| 1823 | sm->sup_pmk_r1_name, WPA_PMK_NAME_LEN); |
| 1824 | wpa_hexdump(MSG_DEBUG, "FT: Derived PMKR1Name", |
| 1825 | sm->pmk_r1_name, WPA_PMK_NAME_LEN); |
| 1826 | return; |
| 1827 | } |
| 1828 | } |
| 1829 | #endif /* CONFIG_IEEE80211R */ |
| 1830 | |
| 1831 | sm->pending_1_of_4_timeout = 0; |
| 1832 | eloop_cancel_timeout(wpa_send_eapol_timeout, sm->wpa_auth, sm); |
| 1833 | |
| 1834 | if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt)) { |
| 1835 | /* PSK may have changed from the previous choice, so update |
| 1836 | * state machine data based on whatever PSK was selected here. |
| 1837 | */ |
| 1838 | os_memcpy(sm->PMK, pmk, PMK_LEN); |
| 1839 | } |
| 1840 | |
| 1841 | sm->MICVerified = TRUE; |
| 1842 | |
| 1843 | os_memcpy(&sm->PTK, &PTK, sizeof(PTK)); |
| 1844 | sm->PTK_valid = TRUE; |
| 1845 | } |
| 1846 | |
| 1847 | |
| 1848 | SM_STATE(WPA_PTK, PTKCALCNEGOTIATING2) |
| 1849 | { |
| 1850 | SM_ENTRY_MA(WPA_PTK, PTKCALCNEGOTIATING2, wpa_ptk); |
| 1851 | sm->TimeoutCtr = 0; |
| 1852 | } |
| 1853 | |
| 1854 | |
| 1855 | #ifdef CONFIG_IEEE80211W |
| 1856 | |
| 1857 | static int ieee80211w_kde_len(struct wpa_state_machine *sm) |
| 1858 | { |
| 1859 | if (sm->mgmt_frame_prot) { |
| 1860 | return 2 + RSN_SELECTOR_LEN + sizeof(struct wpa_igtk_kde); |
| 1861 | } |
| 1862 | |
| 1863 | return 0; |
| 1864 | } |
| 1865 | |
| 1866 | |
| 1867 | static u8 * ieee80211w_kde_add(struct wpa_state_machine *sm, u8 *pos) |
| 1868 | { |
| 1869 | struct wpa_igtk_kde igtk; |
| 1870 | struct wpa_group *gsm = sm->group; |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 1871 | u8 rsc[WPA_KEY_RSC_LEN]; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1872 | |
| 1873 | if (!sm->mgmt_frame_prot) |
| 1874 | return pos; |
| 1875 | |
| 1876 | igtk.keyid[0] = gsm->GN_igtk; |
| 1877 | igtk.keyid[1] = 0; |
| 1878 | if (gsm->wpa_group_state != WPA_GROUP_SETKEYSDONE || |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 1879 | wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN_igtk, rsc) < 0) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1880 | os_memset(igtk.pn, 0, sizeof(igtk.pn)); |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 1881 | else |
| 1882 | os_memcpy(igtk.pn, rsc, sizeof(igtk.pn)); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1883 | os_memcpy(igtk.igtk, gsm->IGTK[gsm->GN_igtk - 4], WPA_IGTK_LEN); |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 1884 | if (sm->wpa_auth->conf.disable_gtk) { |
| 1885 | /* |
| 1886 | * Provide unique random IGTK to each STA to prevent use of |
| 1887 | * IGTK in the BSS. |
| 1888 | */ |
| 1889 | if (random_get_bytes(igtk.igtk, WPA_IGTK_LEN) < 0) |
| 1890 | return pos; |
| 1891 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1892 | pos = wpa_add_kde(pos, RSN_KEY_DATA_IGTK, |
| 1893 | (const u8 *) &igtk, sizeof(igtk), NULL, 0); |
| 1894 | |
| 1895 | return pos; |
| 1896 | } |
| 1897 | |
| 1898 | #else /* CONFIG_IEEE80211W */ |
| 1899 | |
| 1900 | static int ieee80211w_kde_len(struct wpa_state_machine *sm) |
| 1901 | { |
| 1902 | return 0; |
| 1903 | } |
| 1904 | |
| 1905 | |
| 1906 | static u8 * ieee80211w_kde_add(struct wpa_state_machine *sm, u8 *pos) |
| 1907 | { |
| 1908 | return pos; |
| 1909 | } |
| 1910 | |
| 1911 | #endif /* CONFIG_IEEE80211W */ |
| 1912 | |
| 1913 | |
| 1914 | SM_STATE(WPA_PTK, PTKINITNEGOTIATING) |
| 1915 | { |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 1916 | u8 rsc[WPA_KEY_RSC_LEN], *_rsc, *gtk, *kde, *pos, dummy_gtk[32]; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1917 | size_t gtk_len, kde_len; |
| 1918 | struct wpa_group *gsm = sm->group; |
| 1919 | u8 *wpa_ie; |
| 1920 | int wpa_ie_len, secure, keyidx, encr = 0; |
| 1921 | |
| 1922 | SM_ENTRY_MA(WPA_PTK, PTKINITNEGOTIATING, wpa_ptk); |
| 1923 | sm->TimeoutEvt = FALSE; |
| 1924 | |
| 1925 | sm->TimeoutCtr++; |
| 1926 | if (sm->TimeoutCtr > (int) dot11RSNAConfigPairwiseUpdateCount) { |
| 1927 | /* No point in sending the EAPOL-Key - we will disconnect |
| 1928 | * immediately following this. */ |
| 1929 | return; |
| 1930 | } |
| 1931 | |
| 1932 | /* Send EAPOL(1, 1, 1, Pair, P, RSC, ANonce, MIC(PTK), RSNIE, [MDIE], |
| 1933 | GTK[GN], IGTK, [FTIE], [TIE * 2]) |
| 1934 | */ |
| 1935 | os_memset(rsc, 0, WPA_KEY_RSC_LEN); |
| 1936 | wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN, rsc); |
| 1937 | /* If FT is used, wpa_auth->wpa_ie includes both RSNIE and MDIE */ |
| 1938 | wpa_ie = sm->wpa_auth->wpa_ie; |
| 1939 | wpa_ie_len = sm->wpa_auth->wpa_ie_len; |
| 1940 | if (sm->wpa == WPA_VERSION_WPA && |
| 1941 | (sm->wpa_auth->conf.wpa & WPA_PROTO_RSN) && |
| 1942 | wpa_ie_len > wpa_ie[1] + 2 && wpa_ie[0] == WLAN_EID_RSN) { |
| 1943 | /* WPA-only STA, remove RSN IE */ |
| 1944 | wpa_ie = wpa_ie + wpa_ie[1] + 2; |
| 1945 | wpa_ie_len = wpa_ie[1] + 2; |
| 1946 | } |
| 1947 | wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG, |
| 1948 | "sending 3/4 msg of 4-Way Handshake"); |
| 1949 | if (sm->wpa == WPA_VERSION_WPA2) { |
| 1950 | /* WPA2 send GTK in the 4-way handshake */ |
| 1951 | secure = 1; |
| 1952 | gtk = gsm->GTK[gsm->GN - 1]; |
| 1953 | gtk_len = gsm->GTK_len; |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 1954 | if (sm->wpa_auth->conf.disable_gtk) { |
| 1955 | /* |
| 1956 | * Provide unique random GTK to each STA to prevent use |
| 1957 | * of GTK in the BSS. |
| 1958 | */ |
| 1959 | if (random_get_bytes(dummy_gtk, gtk_len) < 0) |
| 1960 | return; |
| 1961 | gtk = dummy_gtk; |
| 1962 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1963 | keyidx = gsm->GN; |
| 1964 | _rsc = rsc; |
| 1965 | encr = 1; |
| 1966 | } else { |
| 1967 | /* WPA does not include GTK in msg 3/4 */ |
| 1968 | secure = 0; |
| 1969 | gtk = NULL; |
| 1970 | gtk_len = 0; |
| 1971 | keyidx = 0; |
| 1972 | _rsc = NULL; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 1973 | if (sm->rx_eapol_key_secure) { |
| 1974 | /* |
| 1975 | * It looks like Windows 7 supplicant tries to use |
| 1976 | * Secure bit in msg 2/4 after having reported Michael |
| 1977 | * MIC failure and it then rejects the 4-way handshake |
| 1978 | * if msg 3/4 does not set Secure bit. Work around this |
| 1979 | * by setting the Secure bit here even in the case of |
| 1980 | * WPA if the supplicant used it first. |
| 1981 | */ |
| 1982 | wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG, |
| 1983 | "STA used Secure bit in WPA msg 2/4 - " |
| 1984 | "set Secure for 3/4 as workaround"); |
| 1985 | secure = 1; |
| 1986 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1987 | } |
| 1988 | |
| 1989 | kde_len = wpa_ie_len + ieee80211w_kde_len(sm); |
| 1990 | if (gtk) |
| 1991 | kde_len += 2 + RSN_SELECTOR_LEN + 2 + gtk_len; |
| 1992 | #ifdef CONFIG_IEEE80211R |
| 1993 | if (wpa_key_mgmt_ft(sm->wpa_key_mgmt)) { |
| 1994 | kde_len += 2 + PMKID_LEN; /* PMKR1Name into RSN IE */ |
| 1995 | kde_len += 300; /* FTIE + 2 * TIE */ |
| 1996 | } |
| 1997 | #endif /* CONFIG_IEEE80211R */ |
| 1998 | kde = os_malloc(kde_len); |
| 1999 | if (kde == NULL) |
| 2000 | return; |
| 2001 | |
| 2002 | pos = kde; |
| 2003 | os_memcpy(pos, wpa_ie, wpa_ie_len); |
| 2004 | pos += wpa_ie_len; |
| 2005 | #ifdef CONFIG_IEEE80211R |
| 2006 | if (wpa_key_mgmt_ft(sm->wpa_key_mgmt)) { |
| 2007 | int res = wpa_insert_pmkid(kde, pos - kde, sm->pmk_r1_name); |
| 2008 | if (res < 0) { |
| 2009 | wpa_printf(MSG_ERROR, "FT: Failed to insert " |
| 2010 | "PMKR1Name into RSN IE in EAPOL-Key data"); |
| 2011 | os_free(kde); |
| 2012 | return; |
| 2013 | } |
| 2014 | pos += res; |
| 2015 | } |
| 2016 | #endif /* CONFIG_IEEE80211R */ |
| 2017 | if (gtk) { |
| 2018 | u8 hdr[2]; |
| 2019 | hdr[0] = keyidx & 0x03; |
| 2020 | hdr[1] = 0; |
| 2021 | pos = wpa_add_kde(pos, RSN_KEY_DATA_GROUPKEY, hdr, 2, |
| 2022 | gtk, gtk_len); |
| 2023 | } |
| 2024 | pos = ieee80211w_kde_add(sm, pos); |
| 2025 | |
| 2026 | #ifdef CONFIG_IEEE80211R |
| 2027 | if (wpa_key_mgmt_ft(sm->wpa_key_mgmt)) { |
| 2028 | int res; |
| 2029 | struct wpa_auth_config *conf; |
| 2030 | |
| 2031 | conf = &sm->wpa_auth->conf; |
| 2032 | res = wpa_write_ftie(conf, conf->r0_key_holder, |
| 2033 | conf->r0_key_holder_len, |
| 2034 | NULL, NULL, pos, kde + kde_len - pos, |
| 2035 | NULL, 0); |
| 2036 | if (res < 0) { |
| 2037 | wpa_printf(MSG_ERROR, "FT: Failed to insert FTIE " |
| 2038 | "into EAPOL-Key Key Data"); |
| 2039 | os_free(kde); |
| 2040 | return; |
| 2041 | } |
| 2042 | pos += res; |
| 2043 | |
| 2044 | /* TIE[ReassociationDeadline] (TU) */ |
| 2045 | *pos++ = WLAN_EID_TIMEOUT_INTERVAL; |
| 2046 | *pos++ = 5; |
| 2047 | *pos++ = WLAN_TIMEOUT_REASSOC_DEADLINE; |
| 2048 | WPA_PUT_LE32(pos, conf->reassociation_deadline); |
| 2049 | pos += 4; |
| 2050 | |
| 2051 | /* TIE[KeyLifetime] (seconds) */ |
| 2052 | *pos++ = WLAN_EID_TIMEOUT_INTERVAL; |
| 2053 | *pos++ = 5; |
| 2054 | *pos++ = WLAN_TIMEOUT_KEY_LIFETIME; |
| 2055 | WPA_PUT_LE32(pos, conf->r0_key_lifetime * 60); |
| 2056 | pos += 4; |
| 2057 | } |
| 2058 | #endif /* CONFIG_IEEE80211R */ |
| 2059 | |
| 2060 | wpa_send_eapol(sm->wpa_auth, sm, |
| 2061 | (secure ? WPA_KEY_INFO_SECURE : 0) | WPA_KEY_INFO_MIC | |
| 2062 | WPA_KEY_INFO_ACK | WPA_KEY_INFO_INSTALL | |
| 2063 | WPA_KEY_INFO_KEY_TYPE, |
| 2064 | _rsc, sm->ANonce, kde, pos - kde, keyidx, encr); |
| 2065 | os_free(kde); |
| 2066 | } |
| 2067 | |
| 2068 | |
| 2069 | SM_STATE(WPA_PTK, PTKINITDONE) |
| 2070 | { |
| 2071 | SM_ENTRY_MA(WPA_PTK, PTKINITDONE, wpa_ptk); |
| 2072 | sm->EAPOLKeyReceived = FALSE; |
| 2073 | if (sm->Pair) { |
Dmitry Shmidt | 4530cfd | 2012-09-09 15:20:40 -0700 | [diff] [blame] | 2074 | enum wpa_alg alg = wpa_cipher_to_alg(sm->pairwise); |
| 2075 | int klen = wpa_cipher_key_len(sm->pairwise); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2076 | if (wpa_auth_set_key(sm->wpa_auth, 0, alg, sm->addr, 0, |
| 2077 | sm->PTK.tk1, klen)) { |
| 2078 | wpa_sta_disconnect(sm->wpa_auth, sm->addr); |
| 2079 | return; |
| 2080 | } |
| 2081 | /* FIX: MLME-SetProtection.Request(TA, Tx_Rx) */ |
| 2082 | sm->pairwise_set = TRUE; |
| 2083 | |
| 2084 | if (sm->wpa_auth->conf.wpa_ptk_rekey) { |
| 2085 | eloop_cancel_timeout(wpa_rekey_ptk, sm->wpa_auth, sm); |
| 2086 | eloop_register_timeout(sm->wpa_auth->conf. |
| 2087 | wpa_ptk_rekey, 0, wpa_rekey_ptk, |
| 2088 | sm->wpa_auth, sm); |
| 2089 | } |
| 2090 | |
| 2091 | if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt)) { |
| 2092 | wpa_auth_set_eapol(sm->wpa_auth, sm->addr, |
| 2093 | WPA_EAPOL_authorized, 1); |
| 2094 | } |
| 2095 | } |
| 2096 | |
| 2097 | if (0 /* IBSS == TRUE */) { |
| 2098 | sm->keycount++; |
| 2099 | if (sm->keycount == 2) { |
| 2100 | wpa_auth_set_eapol(sm->wpa_auth, sm->addr, |
| 2101 | WPA_EAPOL_portValid, 1); |
| 2102 | } |
| 2103 | } else { |
| 2104 | wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portValid, |
| 2105 | 1); |
| 2106 | } |
| 2107 | wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_keyAvailable, 0); |
| 2108 | wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_keyDone, 1); |
| 2109 | if (sm->wpa == WPA_VERSION_WPA) |
| 2110 | sm->PInitAKeys = TRUE; |
| 2111 | else |
| 2112 | sm->has_GTK = TRUE; |
| 2113 | wpa_auth_vlogger(sm->wpa_auth, sm->addr, LOGGER_INFO, |
| 2114 | "pairwise key handshake completed (%s)", |
| 2115 | sm->wpa == WPA_VERSION_WPA ? "WPA" : "RSN"); |
| 2116 | |
| 2117 | #ifdef CONFIG_IEEE80211R |
| 2118 | wpa_ft_push_pmk_r1(sm->wpa_auth, sm->addr); |
| 2119 | #endif /* CONFIG_IEEE80211R */ |
| 2120 | } |
| 2121 | |
| 2122 | |
| 2123 | SM_STEP(WPA_PTK) |
| 2124 | { |
| 2125 | struct wpa_authenticator *wpa_auth = sm->wpa_auth; |
| 2126 | |
| 2127 | if (sm->Init) |
| 2128 | SM_ENTER(WPA_PTK, INITIALIZE); |
| 2129 | else if (sm->Disconnect |
| 2130 | /* || FIX: dot11RSNAConfigSALifetime timeout */) { |
| 2131 | wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG, |
| 2132 | "WPA_PTK: sm->Disconnect"); |
| 2133 | SM_ENTER(WPA_PTK, DISCONNECT); |
| 2134 | } |
| 2135 | else if (sm->DeauthenticationRequest) |
| 2136 | SM_ENTER(WPA_PTK, DISCONNECTED); |
| 2137 | else if (sm->AuthenticationRequest) |
| 2138 | SM_ENTER(WPA_PTK, AUTHENTICATION); |
| 2139 | else if (sm->ReAuthenticationRequest) |
| 2140 | SM_ENTER(WPA_PTK, AUTHENTICATION2); |
| 2141 | else if (sm->PTKRequest) |
| 2142 | SM_ENTER(WPA_PTK, PTKSTART); |
| 2143 | else switch (sm->wpa_ptk_state) { |
| 2144 | case WPA_PTK_INITIALIZE: |
| 2145 | break; |
| 2146 | case WPA_PTK_DISCONNECT: |
| 2147 | SM_ENTER(WPA_PTK, DISCONNECTED); |
| 2148 | break; |
| 2149 | case WPA_PTK_DISCONNECTED: |
| 2150 | SM_ENTER(WPA_PTK, INITIALIZE); |
| 2151 | break; |
| 2152 | case WPA_PTK_AUTHENTICATION: |
| 2153 | SM_ENTER(WPA_PTK, AUTHENTICATION2); |
| 2154 | break; |
| 2155 | case WPA_PTK_AUTHENTICATION2: |
| 2156 | if (wpa_key_mgmt_wpa_ieee8021x(sm->wpa_key_mgmt) && |
| 2157 | wpa_auth_get_eapol(sm->wpa_auth, sm->addr, |
| 2158 | WPA_EAPOL_keyRun) > 0) |
| 2159 | SM_ENTER(WPA_PTK, INITPMK); |
| 2160 | else if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt) |
| 2161 | /* FIX: && 802.1X::keyRun */) |
| 2162 | SM_ENTER(WPA_PTK, INITPSK); |
| 2163 | break; |
| 2164 | case WPA_PTK_INITPMK: |
| 2165 | if (wpa_auth_get_eapol(sm->wpa_auth, sm->addr, |
| 2166 | WPA_EAPOL_keyAvailable) > 0) |
| 2167 | SM_ENTER(WPA_PTK, PTKSTART); |
| 2168 | else { |
| 2169 | wpa_auth->dot11RSNA4WayHandshakeFailures++; |
| 2170 | wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_INFO, |
| 2171 | "INITPMK - keyAvailable = false"); |
| 2172 | SM_ENTER(WPA_PTK, DISCONNECT); |
| 2173 | } |
| 2174 | break; |
| 2175 | case WPA_PTK_INITPSK: |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 2176 | if (wpa_auth_get_psk(sm->wpa_auth, sm->addr, sm->p2p_dev_addr, |
| 2177 | NULL)) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2178 | SM_ENTER(WPA_PTK, PTKSTART); |
| 2179 | else { |
| 2180 | wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_INFO, |
| 2181 | "no PSK configured for the STA"); |
| 2182 | wpa_auth->dot11RSNA4WayHandshakeFailures++; |
| 2183 | SM_ENTER(WPA_PTK, DISCONNECT); |
| 2184 | } |
| 2185 | break; |
| 2186 | case WPA_PTK_PTKSTART: |
| 2187 | if (sm->EAPOLKeyReceived && !sm->EAPOLKeyRequest && |
| 2188 | sm->EAPOLKeyPairwise) |
| 2189 | SM_ENTER(WPA_PTK, PTKCALCNEGOTIATING); |
| 2190 | else if (sm->TimeoutCtr > |
| 2191 | (int) dot11RSNAConfigPairwiseUpdateCount) { |
| 2192 | wpa_auth->dot11RSNA4WayHandshakeFailures++; |
| 2193 | wpa_auth_vlogger(sm->wpa_auth, sm->addr, LOGGER_DEBUG, |
| 2194 | "PTKSTART: Retry limit %d reached", |
| 2195 | dot11RSNAConfigPairwiseUpdateCount); |
| 2196 | SM_ENTER(WPA_PTK, DISCONNECT); |
| 2197 | } else if (sm->TimeoutEvt) |
| 2198 | SM_ENTER(WPA_PTK, PTKSTART); |
| 2199 | break; |
| 2200 | case WPA_PTK_PTKCALCNEGOTIATING: |
| 2201 | if (sm->MICVerified) |
| 2202 | SM_ENTER(WPA_PTK, PTKCALCNEGOTIATING2); |
| 2203 | else if (sm->EAPOLKeyReceived && !sm->EAPOLKeyRequest && |
| 2204 | sm->EAPOLKeyPairwise) |
| 2205 | SM_ENTER(WPA_PTK, PTKCALCNEGOTIATING); |
| 2206 | else if (sm->TimeoutEvt) |
| 2207 | SM_ENTER(WPA_PTK, PTKSTART); |
| 2208 | break; |
| 2209 | case WPA_PTK_PTKCALCNEGOTIATING2: |
| 2210 | SM_ENTER(WPA_PTK, PTKINITNEGOTIATING); |
| 2211 | break; |
| 2212 | case WPA_PTK_PTKINITNEGOTIATING: |
Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 2213 | if (sm->update_snonce) |
| 2214 | SM_ENTER(WPA_PTK, PTKCALCNEGOTIATING); |
| 2215 | else if (sm->EAPOLKeyReceived && !sm->EAPOLKeyRequest && |
| 2216 | sm->EAPOLKeyPairwise && sm->MICVerified) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2217 | SM_ENTER(WPA_PTK, PTKINITDONE); |
| 2218 | else if (sm->TimeoutCtr > |
| 2219 | (int) dot11RSNAConfigPairwiseUpdateCount) { |
| 2220 | wpa_auth->dot11RSNA4WayHandshakeFailures++; |
| 2221 | wpa_auth_vlogger(sm->wpa_auth, sm->addr, LOGGER_DEBUG, |
| 2222 | "PTKINITNEGOTIATING: Retry limit %d " |
| 2223 | "reached", |
| 2224 | dot11RSNAConfigPairwiseUpdateCount); |
| 2225 | SM_ENTER(WPA_PTK, DISCONNECT); |
| 2226 | } else if (sm->TimeoutEvt) |
| 2227 | SM_ENTER(WPA_PTK, PTKINITNEGOTIATING); |
| 2228 | break; |
| 2229 | case WPA_PTK_PTKINITDONE: |
| 2230 | break; |
| 2231 | } |
| 2232 | } |
| 2233 | |
| 2234 | |
| 2235 | SM_STATE(WPA_PTK_GROUP, IDLE) |
| 2236 | { |
| 2237 | SM_ENTRY_MA(WPA_PTK_GROUP, IDLE, wpa_ptk_group); |
| 2238 | if (sm->Init) { |
| 2239 | /* Init flag is not cleared here, so avoid busy |
| 2240 | * loop by claiming nothing changed. */ |
| 2241 | sm->changed = FALSE; |
| 2242 | } |
| 2243 | sm->GTimeoutCtr = 0; |
| 2244 | } |
| 2245 | |
| 2246 | |
| 2247 | SM_STATE(WPA_PTK_GROUP, REKEYNEGOTIATING) |
| 2248 | { |
| 2249 | u8 rsc[WPA_KEY_RSC_LEN]; |
| 2250 | struct wpa_group *gsm = sm->group; |
| 2251 | u8 *kde, *pos, hdr[2]; |
| 2252 | size_t kde_len; |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 2253 | u8 *gtk, dummy_gtk[32]; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2254 | |
| 2255 | SM_ENTRY_MA(WPA_PTK_GROUP, REKEYNEGOTIATING, wpa_ptk_group); |
| 2256 | |
| 2257 | sm->GTimeoutCtr++; |
| 2258 | if (sm->GTimeoutCtr > (int) dot11RSNAConfigGroupUpdateCount) { |
| 2259 | /* No point in sending the EAPOL-Key - we will disconnect |
| 2260 | * immediately following this. */ |
| 2261 | return; |
| 2262 | } |
| 2263 | |
| 2264 | if (sm->wpa == WPA_VERSION_WPA) |
| 2265 | sm->PInitAKeys = FALSE; |
| 2266 | sm->TimeoutEvt = FALSE; |
| 2267 | /* Send EAPOL(1, 1, 1, !Pair, G, RSC, GNonce, MIC(PTK), GTK[GN]) */ |
| 2268 | os_memset(rsc, 0, WPA_KEY_RSC_LEN); |
| 2269 | if (gsm->wpa_group_state == WPA_GROUP_SETKEYSDONE) |
| 2270 | wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN, rsc); |
| 2271 | wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG, |
| 2272 | "sending 1/2 msg of Group Key Handshake"); |
| 2273 | |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 2274 | gtk = gsm->GTK[gsm->GN - 1]; |
| 2275 | if (sm->wpa_auth->conf.disable_gtk) { |
| 2276 | /* |
| 2277 | * Provide unique random GTK to each STA to prevent use |
| 2278 | * of GTK in the BSS. |
| 2279 | */ |
| 2280 | if (random_get_bytes(dummy_gtk, gsm->GTK_len) < 0) |
| 2281 | return; |
| 2282 | gtk = dummy_gtk; |
| 2283 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2284 | if (sm->wpa == WPA_VERSION_WPA2) { |
| 2285 | kde_len = 2 + RSN_SELECTOR_LEN + 2 + gsm->GTK_len + |
| 2286 | ieee80211w_kde_len(sm); |
| 2287 | kde = os_malloc(kde_len); |
| 2288 | if (kde == NULL) |
| 2289 | return; |
| 2290 | |
| 2291 | pos = kde; |
| 2292 | hdr[0] = gsm->GN & 0x03; |
| 2293 | hdr[1] = 0; |
| 2294 | pos = wpa_add_kde(pos, RSN_KEY_DATA_GROUPKEY, hdr, 2, |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 2295 | gtk, gsm->GTK_len); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2296 | pos = ieee80211w_kde_add(sm, pos); |
| 2297 | } else { |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 2298 | kde = gtk; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2299 | pos = kde + gsm->GTK_len; |
| 2300 | } |
| 2301 | |
| 2302 | wpa_send_eapol(sm->wpa_auth, sm, |
| 2303 | WPA_KEY_INFO_SECURE | WPA_KEY_INFO_MIC | |
| 2304 | WPA_KEY_INFO_ACK | |
| 2305 | (!sm->Pair ? WPA_KEY_INFO_INSTALL : 0), |
| 2306 | rsc, gsm->GNonce, kde, pos - kde, gsm->GN, 1); |
| 2307 | if (sm->wpa == WPA_VERSION_WPA2) |
| 2308 | os_free(kde); |
| 2309 | } |
| 2310 | |
| 2311 | |
| 2312 | SM_STATE(WPA_PTK_GROUP, REKEYESTABLISHED) |
| 2313 | { |
| 2314 | SM_ENTRY_MA(WPA_PTK_GROUP, REKEYESTABLISHED, wpa_ptk_group); |
| 2315 | sm->EAPOLKeyReceived = FALSE; |
| 2316 | if (sm->GUpdateStationKeys) |
| 2317 | sm->group->GKeyDoneStations--; |
| 2318 | sm->GUpdateStationKeys = FALSE; |
| 2319 | sm->GTimeoutCtr = 0; |
| 2320 | /* FIX: MLME.SetProtection.Request(TA, Tx_Rx) */ |
| 2321 | wpa_auth_vlogger(sm->wpa_auth, sm->addr, LOGGER_INFO, |
| 2322 | "group key handshake completed (%s)", |
| 2323 | sm->wpa == WPA_VERSION_WPA ? "WPA" : "RSN"); |
| 2324 | sm->has_GTK = TRUE; |
| 2325 | } |
| 2326 | |
| 2327 | |
| 2328 | SM_STATE(WPA_PTK_GROUP, KEYERROR) |
| 2329 | { |
| 2330 | SM_ENTRY_MA(WPA_PTK_GROUP, KEYERROR, wpa_ptk_group); |
| 2331 | if (sm->GUpdateStationKeys) |
| 2332 | sm->group->GKeyDoneStations--; |
| 2333 | sm->GUpdateStationKeys = FALSE; |
| 2334 | sm->Disconnect = TRUE; |
| 2335 | } |
| 2336 | |
| 2337 | |
| 2338 | SM_STEP(WPA_PTK_GROUP) |
| 2339 | { |
| 2340 | if (sm->Init || sm->PtkGroupInit) { |
| 2341 | SM_ENTER(WPA_PTK_GROUP, IDLE); |
| 2342 | sm->PtkGroupInit = FALSE; |
| 2343 | } else switch (sm->wpa_ptk_group_state) { |
| 2344 | case WPA_PTK_GROUP_IDLE: |
| 2345 | if (sm->GUpdateStationKeys || |
| 2346 | (sm->wpa == WPA_VERSION_WPA && sm->PInitAKeys)) |
| 2347 | SM_ENTER(WPA_PTK_GROUP, REKEYNEGOTIATING); |
| 2348 | break; |
| 2349 | case WPA_PTK_GROUP_REKEYNEGOTIATING: |
| 2350 | if (sm->EAPOLKeyReceived && !sm->EAPOLKeyRequest && |
| 2351 | !sm->EAPOLKeyPairwise && sm->MICVerified) |
| 2352 | SM_ENTER(WPA_PTK_GROUP, REKEYESTABLISHED); |
| 2353 | else if (sm->GTimeoutCtr > |
| 2354 | (int) dot11RSNAConfigGroupUpdateCount) |
| 2355 | SM_ENTER(WPA_PTK_GROUP, KEYERROR); |
| 2356 | else if (sm->TimeoutEvt) |
| 2357 | SM_ENTER(WPA_PTK_GROUP, REKEYNEGOTIATING); |
| 2358 | break; |
| 2359 | case WPA_PTK_GROUP_KEYERROR: |
| 2360 | SM_ENTER(WPA_PTK_GROUP, IDLE); |
| 2361 | break; |
| 2362 | case WPA_PTK_GROUP_REKEYESTABLISHED: |
| 2363 | SM_ENTER(WPA_PTK_GROUP, IDLE); |
| 2364 | break; |
| 2365 | } |
| 2366 | } |
| 2367 | |
| 2368 | |
| 2369 | static int wpa_gtk_update(struct wpa_authenticator *wpa_auth, |
| 2370 | struct wpa_group *group) |
| 2371 | { |
| 2372 | int ret = 0; |
| 2373 | |
| 2374 | os_memcpy(group->GNonce, group->Counter, WPA_NONCE_LEN); |
| 2375 | inc_byte_array(group->Counter, WPA_NONCE_LEN); |
| 2376 | if (wpa_gmk_to_gtk(group->GMK, "Group key expansion", |
| 2377 | wpa_auth->addr, group->GNonce, |
| 2378 | group->GTK[group->GN - 1], group->GTK_len) < 0) |
| 2379 | ret = -1; |
| 2380 | wpa_hexdump_key(MSG_DEBUG, "GTK", |
| 2381 | group->GTK[group->GN - 1], group->GTK_len); |
| 2382 | |
| 2383 | #ifdef CONFIG_IEEE80211W |
| 2384 | if (wpa_auth->conf.ieee80211w != NO_MGMT_FRAME_PROTECTION) { |
| 2385 | os_memcpy(group->GNonce, group->Counter, WPA_NONCE_LEN); |
| 2386 | inc_byte_array(group->Counter, WPA_NONCE_LEN); |
| 2387 | if (wpa_gmk_to_gtk(group->GMK, "IGTK key expansion", |
| 2388 | wpa_auth->addr, group->GNonce, |
| 2389 | group->IGTK[group->GN_igtk - 4], |
| 2390 | WPA_IGTK_LEN) < 0) |
| 2391 | ret = -1; |
| 2392 | wpa_hexdump_key(MSG_DEBUG, "IGTK", |
| 2393 | group->IGTK[group->GN_igtk - 4], WPA_IGTK_LEN); |
| 2394 | } |
| 2395 | #endif /* CONFIG_IEEE80211W */ |
| 2396 | |
| 2397 | return ret; |
| 2398 | } |
| 2399 | |
| 2400 | |
| 2401 | static void wpa_group_gtk_init(struct wpa_authenticator *wpa_auth, |
| 2402 | struct wpa_group *group) |
| 2403 | { |
| 2404 | wpa_printf(MSG_DEBUG, "WPA: group state machine entering state " |
| 2405 | "GTK_INIT (VLAN-ID %d)", group->vlan_id); |
| 2406 | group->changed = FALSE; /* GInit is not cleared here; avoid loop */ |
| 2407 | group->wpa_group_state = WPA_GROUP_GTK_INIT; |
| 2408 | |
| 2409 | /* GTK[0..N] = 0 */ |
| 2410 | os_memset(group->GTK, 0, sizeof(group->GTK)); |
| 2411 | group->GN = 1; |
| 2412 | group->GM = 2; |
| 2413 | #ifdef CONFIG_IEEE80211W |
| 2414 | group->GN_igtk = 4; |
| 2415 | group->GM_igtk = 5; |
| 2416 | #endif /* CONFIG_IEEE80211W */ |
| 2417 | /* GTK[GN] = CalcGTK() */ |
| 2418 | wpa_gtk_update(wpa_auth, group); |
| 2419 | } |
| 2420 | |
| 2421 | |
| 2422 | static int wpa_group_update_sta(struct wpa_state_machine *sm, void *ctx) |
| 2423 | { |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 2424 | if (ctx != NULL && ctx != sm->group) |
| 2425 | return 0; |
| 2426 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2427 | if (sm->wpa_ptk_state != WPA_PTK_PTKINITDONE) { |
| 2428 | wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG, |
| 2429 | "Not in PTKINITDONE; skip Group Key update"); |
| 2430 | sm->GUpdateStationKeys = FALSE; |
| 2431 | return 0; |
| 2432 | } |
| 2433 | if (sm->GUpdateStationKeys) { |
| 2434 | /* |
| 2435 | * This should not really happen, so add a debug log entry. |
| 2436 | * Since we clear the GKeyDoneStations before the loop, the |
| 2437 | * station needs to be counted here anyway. |
| 2438 | */ |
| 2439 | wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG, |
| 2440 | "GUpdateStationKeys was already set when " |
| 2441 | "marking station for GTK rekeying"); |
| 2442 | } |
| 2443 | |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 2444 | /* Do not rekey GTK/IGTK when STA is in WNM-Sleep Mode */ |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 2445 | if (sm->is_wnmsleep) |
| 2446 | return 0; |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 2447 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2448 | sm->group->GKeyDoneStations++; |
| 2449 | sm->GUpdateStationKeys = TRUE; |
| 2450 | |
| 2451 | wpa_sm_step(sm); |
| 2452 | return 0; |
| 2453 | } |
| 2454 | |
| 2455 | |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 2456 | #ifdef CONFIG_WNM |
| 2457 | /* update GTK when exiting WNM-Sleep Mode */ |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 2458 | void wpa_wnmsleep_rekey_gtk(struct wpa_state_machine *sm) |
| 2459 | { |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame^] | 2460 | if (sm == NULL || sm->is_wnmsleep) |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 2461 | return; |
| 2462 | |
| 2463 | wpa_group_update_sta(sm, NULL); |
| 2464 | } |
| 2465 | |
| 2466 | |
| 2467 | void wpa_set_wnmsleep(struct wpa_state_machine *sm, int flag) |
| 2468 | { |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame^] | 2469 | if (sm) |
| 2470 | sm->is_wnmsleep = !!flag; |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 2471 | } |
| 2472 | |
| 2473 | |
| 2474 | int wpa_wnmsleep_gtk_subelem(struct wpa_state_machine *sm, u8 *pos) |
| 2475 | { |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 2476 | struct wpa_group *gsm = sm->group; |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 2477 | u8 *start = pos; |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 2478 | |
| 2479 | /* |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 2480 | * GTK subelement: |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 2481 | * Sub-elem ID[1] | Length[1] | Key Info[2] | Key Length[1] | RSC[8] | |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 2482 | * Key[5..32] |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 2483 | */ |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 2484 | *pos++ = WNM_SLEEP_SUBELEM_GTK; |
| 2485 | *pos++ = 11 + gsm->GTK_len; |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 2486 | /* Key ID in B0-B1 of Key Info */ |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 2487 | WPA_PUT_LE16(pos, gsm->GN & 0x03); |
| 2488 | pos += 2; |
| 2489 | *pos++ = gsm->GTK_len; |
| 2490 | if (wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN, pos) != 0) |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 2491 | return 0; |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 2492 | pos += 8; |
| 2493 | os_memcpy(pos, gsm->GTK[gsm->GN - 1], gsm->GTK_len); |
| 2494 | pos += gsm->GTK_len; |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 2495 | |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 2496 | wpa_printf(MSG_DEBUG, "WNM: GTK Key ID %u in WNM-Sleep Mode exit", |
| 2497 | gsm->GN); |
| 2498 | wpa_hexdump_key(MSG_DEBUG, "WNM: GTK in WNM-Sleep Mode exit", |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 2499 | gsm->GTK[gsm->GN - 1], gsm->GTK_len); |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 2500 | |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 2501 | return pos - start; |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 2502 | } |
| 2503 | |
| 2504 | |
| 2505 | #ifdef CONFIG_IEEE80211W |
| 2506 | int wpa_wnmsleep_igtk_subelem(struct wpa_state_machine *sm, u8 *pos) |
| 2507 | { |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 2508 | struct wpa_group *gsm = sm->group; |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 2509 | u8 *start = pos; |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 2510 | |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 2511 | /* |
| 2512 | * IGTK subelement: |
| 2513 | * Sub-elem ID[1] | Length[1] | KeyID[2] | PN[6] | Key[16] |
| 2514 | */ |
| 2515 | *pos++ = WNM_SLEEP_SUBELEM_IGTK; |
| 2516 | *pos++ = 2 + 6 + WPA_IGTK_LEN; |
| 2517 | WPA_PUT_LE16(pos, gsm->GN_igtk); |
| 2518 | pos += 2; |
| 2519 | if (wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN_igtk, pos) != 0) |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 2520 | return 0; |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 2521 | pos += 6; |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 2522 | |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 2523 | os_memcpy(pos, gsm->IGTK[gsm->GN_igtk - 4], WPA_IGTK_LEN); |
| 2524 | pos += WPA_IGTK_LEN; |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 2525 | |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 2526 | wpa_printf(MSG_DEBUG, "WNM: IGTK Key ID %u in WNM-Sleep Mode exit", |
| 2527 | gsm->GN_igtk); |
| 2528 | wpa_hexdump_key(MSG_DEBUG, "WNM: IGTK in WNM-Sleep Mode exit", |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 2529 | gsm->IGTK[gsm->GN_igtk - 4], WPA_IGTK_LEN); |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 2530 | |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 2531 | return pos - start; |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 2532 | } |
| 2533 | #endif /* CONFIG_IEEE80211W */ |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 2534 | #endif /* CONFIG_WNM */ |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 2535 | |
| 2536 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2537 | static void wpa_group_setkeys(struct wpa_authenticator *wpa_auth, |
| 2538 | struct wpa_group *group) |
| 2539 | { |
| 2540 | int tmp; |
| 2541 | |
| 2542 | wpa_printf(MSG_DEBUG, "WPA: group state machine entering state " |
| 2543 | "SETKEYS (VLAN-ID %d)", group->vlan_id); |
| 2544 | group->changed = TRUE; |
| 2545 | group->wpa_group_state = WPA_GROUP_SETKEYS; |
| 2546 | group->GTKReKey = FALSE; |
| 2547 | tmp = group->GM; |
| 2548 | group->GM = group->GN; |
| 2549 | group->GN = tmp; |
| 2550 | #ifdef CONFIG_IEEE80211W |
| 2551 | tmp = group->GM_igtk; |
| 2552 | group->GM_igtk = group->GN_igtk; |
| 2553 | group->GN_igtk = tmp; |
| 2554 | #endif /* CONFIG_IEEE80211W */ |
| 2555 | /* "GKeyDoneStations = GNoStations" is done in more robust way by |
| 2556 | * counting the STAs that are marked with GUpdateStationKeys instead of |
| 2557 | * including all STAs that could be in not-yet-completed state. */ |
| 2558 | wpa_gtk_update(wpa_auth, group); |
| 2559 | |
| 2560 | if (group->GKeyDoneStations) { |
| 2561 | wpa_printf(MSG_DEBUG, "wpa_group_setkeys: Unexpected " |
| 2562 | "GKeyDoneStations=%d when starting new GTK rekey", |
| 2563 | group->GKeyDoneStations); |
| 2564 | group->GKeyDoneStations = 0; |
| 2565 | } |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 2566 | wpa_auth_for_each_sta(wpa_auth, wpa_group_update_sta, group); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2567 | wpa_printf(MSG_DEBUG, "wpa_group_setkeys: GKeyDoneStations=%d", |
| 2568 | group->GKeyDoneStations); |
| 2569 | } |
| 2570 | |
| 2571 | |
| 2572 | static int wpa_group_config_group_keys(struct wpa_authenticator *wpa_auth, |
| 2573 | struct wpa_group *group) |
| 2574 | { |
| 2575 | int ret = 0; |
| 2576 | |
| 2577 | if (wpa_auth_set_key(wpa_auth, group->vlan_id, |
Dmitry Shmidt | 4530cfd | 2012-09-09 15:20:40 -0700 | [diff] [blame] | 2578 | wpa_cipher_to_alg(wpa_auth->conf.wpa_group), |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2579 | broadcast_ether_addr, group->GN, |
| 2580 | group->GTK[group->GN - 1], group->GTK_len) < 0) |
| 2581 | ret = -1; |
| 2582 | |
| 2583 | #ifdef CONFIG_IEEE80211W |
| 2584 | if (wpa_auth->conf.ieee80211w != NO_MGMT_FRAME_PROTECTION && |
| 2585 | wpa_auth_set_key(wpa_auth, group->vlan_id, WPA_ALG_IGTK, |
| 2586 | broadcast_ether_addr, group->GN_igtk, |
| 2587 | group->IGTK[group->GN_igtk - 4], |
| 2588 | WPA_IGTK_LEN) < 0) |
| 2589 | ret = -1; |
| 2590 | #endif /* CONFIG_IEEE80211W */ |
| 2591 | |
| 2592 | return ret; |
| 2593 | } |
| 2594 | |
| 2595 | |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame^] | 2596 | static int wpa_group_disconnect_cb(struct wpa_state_machine *sm, void *ctx) |
| 2597 | { |
| 2598 | if (sm->group == ctx) { |
| 2599 | wpa_printf(MSG_DEBUG, "WPA: Mark STA " MACSTR |
| 2600 | " for discconnection due to fatal failure", |
| 2601 | MAC2STR(sm->addr)); |
| 2602 | sm->Disconnect = TRUE; |
| 2603 | } |
| 2604 | |
| 2605 | return 0; |
| 2606 | } |
| 2607 | |
| 2608 | |
| 2609 | static void wpa_group_fatal_failure(struct wpa_authenticator *wpa_auth, |
| 2610 | struct wpa_group *group) |
| 2611 | { |
| 2612 | wpa_printf(MSG_DEBUG, "WPA: group state machine entering state FATAL_FAILURE"); |
| 2613 | group->changed = TRUE; |
| 2614 | group->wpa_group_state = WPA_GROUP_FATAL_FAILURE; |
| 2615 | wpa_auth_for_each_sta(wpa_auth, wpa_group_disconnect_cb, group); |
| 2616 | } |
| 2617 | |
| 2618 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2619 | static int wpa_group_setkeysdone(struct wpa_authenticator *wpa_auth, |
| 2620 | struct wpa_group *group) |
| 2621 | { |
| 2622 | wpa_printf(MSG_DEBUG, "WPA: group state machine entering state " |
| 2623 | "SETKEYSDONE (VLAN-ID %d)", group->vlan_id); |
| 2624 | group->changed = TRUE; |
| 2625 | group->wpa_group_state = WPA_GROUP_SETKEYSDONE; |
| 2626 | |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame^] | 2627 | if (wpa_group_config_group_keys(wpa_auth, group) < 0) { |
| 2628 | wpa_group_fatal_failure(wpa_auth, group); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2629 | return -1; |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame^] | 2630 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2631 | |
| 2632 | return 0; |
| 2633 | } |
| 2634 | |
| 2635 | |
| 2636 | static void wpa_group_sm_step(struct wpa_authenticator *wpa_auth, |
| 2637 | struct wpa_group *group) |
| 2638 | { |
| 2639 | if (group->GInit) { |
| 2640 | wpa_group_gtk_init(wpa_auth, group); |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame^] | 2641 | } else if (group->wpa_group_state == WPA_GROUP_FATAL_FAILURE) { |
| 2642 | /* Do not allow group operations */ |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2643 | } else if (group->wpa_group_state == WPA_GROUP_GTK_INIT && |
| 2644 | group->GTKAuthenticator) { |
| 2645 | wpa_group_setkeysdone(wpa_auth, group); |
| 2646 | } else if (group->wpa_group_state == WPA_GROUP_SETKEYSDONE && |
| 2647 | group->GTKReKey) { |
| 2648 | wpa_group_setkeys(wpa_auth, group); |
| 2649 | } else if (group->wpa_group_state == WPA_GROUP_SETKEYS) { |
| 2650 | if (group->GKeyDoneStations == 0) |
| 2651 | wpa_group_setkeysdone(wpa_auth, group); |
| 2652 | else if (group->GTKReKey) |
| 2653 | wpa_group_setkeys(wpa_auth, group); |
| 2654 | } |
| 2655 | } |
| 2656 | |
| 2657 | |
| 2658 | static int wpa_sm_step(struct wpa_state_machine *sm) |
| 2659 | { |
| 2660 | if (sm == NULL) |
| 2661 | return 0; |
| 2662 | |
| 2663 | if (sm->in_step_loop) { |
| 2664 | /* This should not happen, but if it does, make sure we do not |
| 2665 | * end up freeing the state machine too early by exiting the |
| 2666 | * recursive call. */ |
| 2667 | wpa_printf(MSG_ERROR, "WPA: wpa_sm_step() called recursively"); |
| 2668 | return 0; |
| 2669 | } |
| 2670 | |
| 2671 | sm->in_step_loop = 1; |
| 2672 | do { |
| 2673 | if (sm->pending_deinit) |
| 2674 | break; |
| 2675 | |
| 2676 | sm->changed = FALSE; |
| 2677 | sm->wpa_auth->group->changed = FALSE; |
| 2678 | |
| 2679 | SM_STEP_RUN(WPA_PTK); |
| 2680 | if (sm->pending_deinit) |
| 2681 | break; |
| 2682 | SM_STEP_RUN(WPA_PTK_GROUP); |
| 2683 | if (sm->pending_deinit) |
| 2684 | break; |
| 2685 | wpa_group_sm_step(sm->wpa_auth, sm->group); |
| 2686 | } while (sm->changed || sm->wpa_auth->group->changed); |
| 2687 | sm->in_step_loop = 0; |
| 2688 | |
| 2689 | if (sm->pending_deinit) { |
| 2690 | wpa_printf(MSG_DEBUG, "WPA: Completing pending STA state " |
| 2691 | "machine deinit for " MACSTR, MAC2STR(sm->addr)); |
| 2692 | wpa_free_sta_sm(sm); |
| 2693 | return 1; |
| 2694 | } |
| 2695 | return 0; |
| 2696 | } |
| 2697 | |
| 2698 | |
| 2699 | static void wpa_sm_call_step(void *eloop_ctx, void *timeout_ctx) |
| 2700 | { |
| 2701 | struct wpa_state_machine *sm = eloop_ctx; |
| 2702 | wpa_sm_step(sm); |
| 2703 | } |
| 2704 | |
| 2705 | |
| 2706 | void wpa_auth_sm_notify(struct wpa_state_machine *sm) |
| 2707 | { |
| 2708 | if (sm == NULL) |
| 2709 | return; |
| 2710 | eloop_register_timeout(0, 0, wpa_sm_call_step, sm, NULL); |
| 2711 | } |
| 2712 | |
| 2713 | |
| 2714 | void wpa_gtk_rekey(struct wpa_authenticator *wpa_auth) |
| 2715 | { |
| 2716 | int tmp, i; |
| 2717 | struct wpa_group *group; |
| 2718 | |
| 2719 | if (wpa_auth == NULL) |
| 2720 | return; |
| 2721 | |
| 2722 | group = wpa_auth->group; |
| 2723 | |
| 2724 | for (i = 0; i < 2; i++) { |
| 2725 | tmp = group->GM; |
| 2726 | group->GM = group->GN; |
| 2727 | group->GN = tmp; |
| 2728 | #ifdef CONFIG_IEEE80211W |
| 2729 | tmp = group->GM_igtk; |
| 2730 | group->GM_igtk = group->GN_igtk; |
| 2731 | group->GN_igtk = tmp; |
| 2732 | #endif /* CONFIG_IEEE80211W */ |
| 2733 | wpa_gtk_update(wpa_auth, group); |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 2734 | wpa_group_config_group_keys(wpa_auth, group); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2735 | } |
| 2736 | } |
| 2737 | |
| 2738 | |
| 2739 | static const char * wpa_bool_txt(int bool) |
| 2740 | { |
| 2741 | return bool ? "TRUE" : "FALSE"; |
| 2742 | } |
| 2743 | |
| 2744 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2745 | #define RSN_SUITE "%02x-%02x-%02x-%d" |
| 2746 | #define RSN_SUITE_ARG(s) \ |
| 2747 | ((s) >> 24) & 0xff, ((s) >> 16) & 0xff, ((s) >> 8) & 0xff, (s) & 0xff |
| 2748 | |
| 2749 | int wpa_get_mib(struct wpa_authenticator *wpa_auth, char *buf, size_t buflen) |
| 2750 | { |
| 2751 | int len = 0, ret; |
| 2752 | char pmkid_txt[PMKID_LEN * 2 + 1]; |
| 2753 | #ifdef CONFIG_RSN_PREAUTH |
| 2754 | const int preauth = 1; |
| 2755 | #else /* CONFIG_RSN_PREAUTH */ |
| 2756 | const int preauth = 0; |
| 2757 | #endif /* CONFIG_RSN_PREAUTH */ |
| 2758 | |
| 2759 | if (wpa_auth == NULL) |
| 2760 | return len; |
| 2761 | |
| 2762 | ret = os_snprintf(buf + len, buflen - len, |
| 2763 | "dot11RSNAOptionImplemented=TRUE\n" |
| 2764 | "dot11RSNAPreauthenticationImplemented=%s\n" |
| 2765 | "dot11RSNAEnabled=%s\n" |
| 2766 | "dot11RSNAPreauthenticationEnabled=%s\n", |
| 2767 | wpa_bool_txt(preauth), |
| 2768 | wpa_bool_txt(wpa_auth->conf.wpa & WPA_PROTO_RSN), |
| 2769 | wpa_bool_txt(wpa_auth->conf.rsn_preauth)); |
| 2770 | if (ret < 0 || (size_t) ret >= buflen - len) |
| 2771 | return len; |
| 2772 | len += ret; |
| 2773 | |
| 2774 | wpa_snprintf_hex(pmkid_txt, sizeof(pmkid_txt), |
| 2775 | wpa_auth->dot11RSNAPMKIDUsed, PMKID_LEN); |
| 2776 | |
| 2777 | ret = os_snprintf( |
| 2778 | buf + len, buflen - len, |
| 2779 | "dot11RSNAConfigVersion=%u\n" |
| 2780 | "dot11RSNAConfigPairwiseKeysSupported=9999\n" |
| 2781 | /* FIX: dot11RSNAConfigGroupCipher */ |
| 2782 | /* FIX: dot11RSNAConfigGroupRekeyMethod */ |
| 2783 | /* FIX: dot11RSNAConfigGroupRekeyTime */ |
| 2784 | /* FIX: dot11RSNAConfigGroupRekeyPackets */ |
| 2785 | "dot11RSNAConfigGroupRekeyStrict=%u\n" |
| 2786 | "dot11RSNAConfigGroupUpdateCount=%u\n" |
| 2787 | "dot11RSNAConfigPairwiseUpdateCount=%u\n" |
| 2788 | "dot11RSNAConfigGroupCipherSize=%u\n" |
| 2789 | "dot11RSNAConfigPMKLifetime=%u\n" |
| 2790 | "dot11RSNAConfigPMKReauthThreshold=%u\n" |
| 2791 | "dot11RSNAConfigNumberOfPTKSAReplayCounters=0\n" |
| 2792 | "dot11RSNAConfigSATimeout=%u\n" |
| 2793 | "dot11RSNAAuthenticationSuiteSelected=" RSN_SUITE "\n" |
| 2794 | "dot11RSNAPairwiseCipherSelected=" RSN_SUITE "\n" |
| 2795 | "dot11RSNAGroupCipherSelected=" RSN_SUITE "\n" |
| 2796 | "dot11RSNAPMKIDUsed=%s\n" |
| 2797 | "dot11RSNAAuthenticationSuiteRequested=" RSN_SUITE "\n" |
| 2798 | "dot11RSNAPairwiseCipherRequested=" RSN_SUITE "\n" |
| 2799 | "dot11RSNAGroupCipherRequested=" RSN_SUITE "\n" |
| 2800 | "dot11RSNATKIPCounterMeasuresInvoked=%u\n" |
| 2801 | "dot11RSNA4WayHandshakeFailures=%u\n" |
| 2802 | "dot11RSNAConfigNumberOfGTKSAReplayCounters=0\n", |
| 2803 | RSN_VERSION, |
| 2804 | !!wpa_auth->conf.wpa_strict_rekey, |
| 2805 | dot11RSNAConfigGroupUpdateCount, |
| 2806 | dot11RSNAConfigPairwiseUpdateCount, |
Dmitry Shmidt | 4530cfd | 2012-09-09 15:20:40 -0700 | [diff] [blame] | 2807 | wpa_cipher_key_len(wpa_auth->conf.wpa_group) * 8, |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2808 | dot11RSNAConfigPMKLifetime, |
| 2809 | dot11RSNAConfigPMKReauthThreshold, |
| 2810 | dot11RSNAConfigSATimeout, |
| 2811 | RSN_SUITE_ARG(wpa_auth->dot11RSNAAuthenticationSuiteSelected), |
| 2812 | RSN_SUITE_ARG(wpa_auth->dot11RSNAPairwiseCipherSelected), |
| 2813 | RSN_SUITE_ARG(wpa_auth->dot11RSNAGroupCipherSelected), |
| 2814 | pmkid_txt, |
| 2815 | RSN_SUITE_ARG(wpa_auth->dot11RSNAAuthenticationSuiteRequested), |
| 2816 | RSN_SUITE_ARG(wpa_auth->dot11RSNAPairwiseCipherRequested), |
| 2817 | RSN_SUITE_ARG(wpa_auth->dot11RSNAGroupCipherRequested), |
| 2818 | wpa_auth->dot11RSNATKIPCounterMeasuresInvoked, |
| 2819 | wpa_auth->dot11RSNA4WayHandshakeFailures); |
| 2820 | if (ret < 0 || (size_t) ret >= buflen - len) |
| 2821 | return len; |
| 2822 | len += ret; |
| 2823 | |
| 2824 | /* TODO: dot11RSNAConfigPairwiseCiphersTable */ |
| 2825 | /* TODO: dot11RSNAConfigAuthenticationSuitesTable */ |
| 2826 | |
| 2827 | /* Private MIB */ |
| 2828 | ret = os_snprintf(buf + len, buflen - len, "hostapdWPAGroupState=%d\n", |
| 2829 | wpa_auth->group->wpa_group_state); |
| 2830 | if (ret < 0 || (size_t) ret >= buflen - len) |
| 2831 | return len; |
| 2832 | len += ret; |
| 2833 | |
| 2834 | return len; |
| 2835 | } |
| 2836 | |
| 2837 | |
| 2838 | int wpa_get_mib_sta(struct wpa_state_machine *sm, char *buf, size_t buflen) |
| 2839 | { |
| 2840 | int len = 0, ret; |
| 2841 | u32 pairwise = 0; |
| 2842 | |
| 2843 | if (sm == NULL) |
| 2844 | return 0; |
| 2845 | |
| 2846 | /* TODO: FF-FF-FF-FF-FF-FF entry for broadcast/multicast stats */ |
| 2847 | |
| 2848 | /* dot11RSNAStatsEntry */ |
| 2849 | |
Dmitry Shmidt | 4530cfd | 2012-09-09 15:20:40 -0700 | [diff] [blame] | 2850 | pairwise = wpa_cipher_to_suite(sm->wpa == WPA_VERSION_WPA2 ? |
| 2851 | WPA_PROTO_RSN : WPA_PROTO_WPA, |
| 2852 | sm->pairwise); |
| 2853 | if (pairwise == 0) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2854 | return 0; |
| 2855 | |
| 2856 | ret = os_snprintf( |
| 2857 | buf + len, buflen - len, |
| 2858 | /* TODO: dot11RSNAStatsIndex */ |
| 2859 | "dot11RSNAStatsSTAAddress=" MACSTR "\n" |
| 2860 | "dot11RSNAStatsVersion=1\n" |
| 2861 | "dot11RSNAStatsSelectedPairwiseCipher=" RSN_SUITE "\n" |
| 2862 | /* TODO: dot11RSNAStatsTKIPICVErrors */ |
| 2863 | "dot11RSNAStatsTKIPLocalMICFailures=%u\n" |
| 2864 | "dot11RSNAStatsTKIPRemoteMICFailures=%u\n" |
| 2865 | /* TODO: dot11RSNAStatsCCMPReplays */ |
| 2866 | /* TODO: dot11RSNAStatsCCMPDecryptErrors */ |
| 2867 | /* TODO: dot11RSNAStatsTKIPReplays */, |
| 2868 | MAC2STR(sm->addr), |
| 2869 | RSN_SUITE_ARG(pairwise), |
| 2870 | sm->dot11RSNAStatsTKIPLocalMICFailures, |
| 2871 | sm->dot11RSNAStatsTKIPRemoteMICFailures); |
| 2872 | if (ret < 0 || (size_t) ret >= buflen - len) |
| 2873 | return len; |
| 2874 | len += ret; |
| 2875 | |
| 2876 | /* Private MIB */ |
| 2877 | ret = os_snprintf(buf + len, buflen - len, |
| 2878 | "hostapdWPAPTKState=%d\n" |
| 2879 | "hostapdWPAPTKGroupState=%d\n", |
| 2880 | sm->wpa_ptk_state, |
| 2881 | sm->wpa_ptk_group_state); |
| 2882 | if (ret < 0 || (size_t) ret >= buflen - len) |
| 2883 | return len; |
| 2884 | len += ret; |
| 2885 | |
| 2886 | return len; |
| 2887 | } |
| 2888 | |
| 2889 | |
| 2890 | void wpa_auth_countermeasures_start(struct wpa_authenticator *wpa_auth) |
| 2891 | { |
| 2892 | if (wpa_auth) |
| 2893 | wpa_auth->dot11RSNATKIPCounterMeasuresInvoked++; |
| 2894 | } |
| 2895 | |
| 2896 | |
| 2897 | int wpa_auth_pairwise_set(struct wpa_state_machine *sm) |
| 2898 | { |
| 2899 | return sm && sm->pairwise_set; |
| 2900 | } |
| 2901 | |
| 2902 | |
| 2903 | int wpa_auth_get_pairwise(struct wpa_state_machine *sm) |
| 2904 | { |
| 2905 | return sm->pairwise; |
| 2906 | } |
| 2907 | |
| 2908 | |
| 2909 | int wpa_auth_sta_key_mgmt(struct wpa_state_machine *sm) |
| 2910 | { |
| 2911 | if (sm == NULL) |
| 2912 | return -1; |
| 2913 | return sm->wpa_key_mgmt; |
| 2914 | } |
| 2915 | |
| 2916 | |
| 2917 | int wpa_auth_sta_wpa_version(struct wpa_state_machine *sm) |
| 2918 | { |
| 2919 | if (sm == NULL) |
| 2920 | return 0; |
| 2921 | return sm->wpa; |
| 2922 | } |
| 2923 | |
| 2924 | |
| 2925 | int wpa_auth_sta_clear_pmksa(struct wpa_state_machine *sm, |
| 2926 | struct rsn_pmksa_cache_entry *entry) |
| 2927 | { |
| 2928 | if (sm == NULL || sm->pmksa != entry) |
| 2929 | return -1; |
| 2930 | sm->pmksa = NULL; |
| 2931 | return 0; |
| 2932 | } |
| 2933 | |
| 2934 | |
| 2935 | struct rsn_pmksa_cache_entry * |
| 2936 | wpa_auth_sta_get_pmksa(struct wpa_state_machine *sm) |
| 2937 | { |
| 2938 | return sm ? sm->pmksa : NULL; |
| 2939 | } |
| 2940 | |
| 2941 | |
| 2942 | void wpa_auth_sta_local_mic_failure_report(struct wpa_state_machine *sm) |
| 2943 | { |
| 2944 | if (sm) |
| 2945 | sm->dot11RSNAStatsTKIPLocalMICFailures++; |
| 2946 | } |
| 2947 | |
| 2948 | |
| 2949 | const u8 * wpa_auth_get_wpa_ie(struct wpa_authenticator *wpa_auth, size_t *len) |
| 2950 | { |
| 2951 | if (wpa_auth == NULL) |
| 2952 | return NULL; |
| 2953 | *len = wpa_auth->wpa_ie_len; |
| 2954 | return wpa_auth->wpa_ie; |
| 2955 | } |
| 2956 | |
| 2957 | |
| 2958 | int wpa_auth_pmksa_add(struct wpa_state_machine *sm, const u8 *pmk, |
| 2959 | int session_timeout, struct eapol_state_machine *eapol) |
| 2960 | { |
Dmitry Shmidt | c55524a | 2011-07-07 11:18:38 -0700 | [diff] [blame] | 2961 | if (sm == NULL || sm->wpa != WPA_VERSION_WPA2 || |
| 2962 | sm->wpa_auth->conf.disable_pmksa_caching) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2963 | return -1; |
| 2964 | |
| 2965 | if (pmksa_cache_auth_add(sm->wpa_auth->pmksa, pmk, PMK_LEN, |
| 2966 | sm->wpa_auth->addr, sm->addr, session_timeout, |
| 2967 | eapol, sm->wpa_key_mgmt)) |
| 2968 | return 0; |
| 2969 | |
| 2970 | return -1; |
| 2971 | } |
| 2972 | |
| 2973 | |
| 2974 | int wpa_auth_pmksa_add_preauth(struct wpa_authenticator *wpa_auth, |
| 2975 | const u8 *pmk, size_t len, const u8 *sta_addr, |
| 2976 | int session_timeout, |
| 2977 | struct eapol_state_machine *eapol) |
| 2978 | { |
| 2979 | if (wpa_auth == NULL) |
| 2980 | return -1; |
| 2981 | |
| 2982 | if (pmksa_cache_auth_add(wpa_auth->pmksa, pmk, len, wpa_auth->addr, |
| 2983 | sta_addr, session_timeout, eapol, |
| 2984 | WPA_KEY_MGMT_IEEE8021X)) |
| 2985 | return 0; |
| 2986 | |
| 2987 | return -1; |
| 2988 | } |
| 2989 | |
| 2990 | |
Dmitry Shmidt | f7e0a99 | 2013-05-23 11:03:10 -0700 | [diff] [blame] | 2991 | void wpa_auth_pmksa_remove(struct wpa_authenticator *wpa_auth, |
| 2992 | const u8 *sta_addr) |
| 2993 | { |
| 2994 | struct rsn_pmksa_cache_entry *pmksa; |
| 2995 | |
| 2996 | if (wpa_auth == NULL || wpa_auth->pmksa == NULL) |
| 2997 | return; |
| 2998 | pmksa = pmksa_cache_auth_get(wpa_auth->pmksa, sta_addr, NULL); |
| 2999 | if (pmksa) { |
| 3000 | wpa_printf(MSG_DEBUG, "WPA: Remove PMKSA cache entry for " |
| 3001 | MACSTR " based on request", MAC2STR(sta_addr)); |
| 3002 | pmksa_cache_free_entry(wpa_auth->pmksa, pmksa); |
| 3003 | } |
| 3004 | } |
| 3005 | |
| 3006 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3007 | static struct wpa_group * |
| 3008 | wpa_auth_add_group(struct wpa_authenticator *wpa_auth, int vlan_id) |
| 3009 | { |
| 3010 | struct wpa_group *group; |
| 3011 | |
| 3012 | if (wpa_auth == NULL || wpa_auth->group == NULL) |
| 3013 | return NULL; |
| 3014 | |
| 3015 | wpa_printf(MSG_DEBUG, "WPA: Add group state machine for VLAN-ID %d", |
| 3016 | vlan_id); |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 3017 | group = wpa_group_init(wpa_auth, vlan_id, 0); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3018 | if (group == NULL) |
| 3019 | return NULL; |
| 3020 | |
| 3021 | group->next = wpa_auth->group->next; |
| 3022 | wpa_auth->group->next = group; |
| 3023 | |
| 3024 | return group; |
| 3025 | } |
| 3026 | |
| 3027 | |
| 3028 | int wpa_auth_sta_set_vlan(struct wpa_state_machine *sm, int vlan_id) |
| 3029 | { |
| 3030 | struct wpa_group *group; |
| 3031 | |
| 3032 | if (sm == NULL || sm->wpa_auth == NULL) |
| 3033 | return 0; |
| 3034 | |
| 3035 | group = sm->wpa_auth->group; |
| 3036 | while (group) { |
| 3037 | if (group->vlan_id == vlan_id) |
| 3038 | break; |
| 3039 | group = group->next; |
| 3040 | } |
| 3041 | |
| 3042 | if (group == NULL) { |
| 3043 | group = wpa_auth_add_group(sm->wpa_auth, vlan_id); |
| 3044 | if (group == NULL) |
| 3045 | return -1; |
| 3046 | } |
| 3047 | |
| 3048 | if (sm->group == group) |
| 3049 | return 0; |
| 3050 | |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame^] | 3051 | if (group->wpa_group_state == WPA_GROUP_FATAL_FAILURE) |
| 3052 | return -1; |
| 3053 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3054 | wpa_printf(MSG_DEBUG, "WPA: Moving STA " MACSTR " to use group state " |
| 3055 | "machine for VLAN ID %d", MAC2STR(sm->addr), vlan_id); |
| 3056 | |
| 3057 | sm->group = group; |
| 3058 | return 0; |
| 3059 | } |
| 3060 | |
| 3061 | |
| 3062 | void wpa_auth_eapol_key_tx_status(struct wpa_authenticator *wpa_auth, |
| 3063 | struct wpa_state_machine *sm, int ack) |
| 3064 | { |
| 3065 | if (wpa_auth == NULL || sm == NULL) |
| 3066 | return; |
| 3067 | wpa_printf(MSG_DEBUG, "WPA: EAPOL-Key TX status for STA " MACSTR |
| 3068 | " ack=%d", MAC2STR(sm->addr), ack); |
| 3069 | if (sm->pending_1_of_4_timeout && ack) { |
| 3070 | /* |
| 3071 | * Some deployed supplicant implementations update their SNonce |
| 3072 | * for each EAPOL-Key 2/4 message even within the same 4-way |
| 3073 | * handshake and then fail to use the first SNonce when |
| 3074 | * deriving the PTK. This results in unsuccessful 4-way |
| 3075 | * handshake whenever the relatively short initial timeout is |
| 3076 | * reached and EAPOL-Key 1/4 is retransmitted. Try to work |
| 3077 | * around this by increasing the timeout now that we know that |
| 3078 | * the station has received the frame. |
| 3079 | */ |
| 3080 | int timeout_ms = eapol_key_timeout_subseq; |
| 3081 | wpa_printf(MSG_DEBUG, "WPA: Increase initial EAPOL-Key 1/4 " |
| 3082 | "timeout by %u ms because of acknowledged frame", |
| 3083 | timeout_ms); |
| 3084 | eloop_cancel_timeout(wpa_send_eapol_timeout, wpa_auth, sm); |
| 3085 | eloop_register_timeout(timeout_ms / 1000, |
| 3086 | (timeout_ms % 1000) * 1000, |
| 3087 | wpa_send_eapol_timeout, wpa_auth, sm); |
| 3088 | } |
| 3089 | } |
Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 3090 | |
| 3091 | |
| 3092 | int wpa_auth_uses_sae(struct wpa_state_machine *sm) |
| 3093 | { |
| 3094 | if (sm == NULL) |
| 3095 | return 0; |
| 3096 | return wpa_key_mgmt_sae(sm->wpa_key_mgmt); |
| 3097 | } |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame^] | 3098 | |
| 3099 | |
| 3100 | int wpa_auth_uses_ft_sae(struct wpa_state_machine *sm) |
| 3101 | { |
| 3102 | if (sm == NULL) |
| 3103 | return 0; |
| 3104 | return sm->wpa_key_mgmt == WPA_KEY_MGMT_FT_SAE; |
| 3105 | } |