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 |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame^] | 3 | * Copyright (c) 2004-2018, 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" |
Dmitry Shmidt | cf32e60 | 2014-01-28 10:57:39 -0800 | [diff] [blame] | 14 | #include "utils/bitfield.h" |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 15 | #include "common/ieee802_11_defs.h" |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 16 | #include "crypto/aes.h" |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 17 | #include "crypto/aes_wrap.h" |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 18 | #include "crypto/aes_siv.h" |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 19 | #include "crypto/crypto.h" |
| 20 | #include "crypto/sha1.h" |
| 21 | #include "crypto/sha256.h" |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame^] | 22 | #include "crypto/sha384.h" |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 23 | #include "crypto/random.h" |
| 24 | #include "eapol_auth/eapol_auth_sm.h" |
| 25 | #include "ap_config.h" |
| 26 | #include "ieee802_11.h" |
| 27 | #include "wpa_auth.h" |
| 28 | #include "pmksa_cache_auth.h" |
| 29 | #include "wpa_auth_i.h" |
| 30 | #include "wpa_auth_ie.h" |
| 31 | |
| 32 | #define STATE_MACHINE_DATA struct wpa_state_machine |
| 33 | #define STATE_MACHINE_DEBUG_PREFIX "WPA" |
| 34 | #define STATE_MACHINE_ADDR sm->addr |
| 35 | |
| 36 | |
| 37 | static void wpa_send_eapol_timeout(void *eloop_ctx, void *timeout_ctx); |
| 38 | static int wpa_sm_step(struct wpa_state_machine *sm); |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 39 | static int wpa_verify_key_mic(int akmp, size_t pmk_len, struct wpa_ptk *PTK, |
| 40 | u8 *data, size_t data_len); |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 41 | #ifdef CONFIG_FILS |
| 42 | static int wpa_aead_decrypt(struct wpa_state_machine *sm, struct wpa_ptk *ptk, |
| 43 | u8 *buf, size_t buf_len, u16 *_key_data_len); |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 44 | static struct wpabuf * fils_prepare_plainbuf(struct wpa_state_machine *sm, |
| 45 | const struct wpabuf *hlp); |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 46 | #endif /* CONFIG_FILS */ |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 47 | static void wpa_sm_call_step(void *eloop_ctx, void *timeout_ctx); |
| 48 | static void wpa_group_sm_step(struct wpa_authenticator *wpa_auth, |
| 49 | struct wpa_group *group); |
| 50 | static void wpa_request_new_ptk(struct wpa_state_machine *sm); |
| 51 | static int wpa_gtk_update(struct wpa_authenticator *wpa_auth, |
| 52 | struct wpa_group *group); |
| 53 | static int wpa_group_config_group_keys(struct wpa_authenticator *wpa_auth, |
| 54 | struct wpa_group *group); |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 55 | static int wpa_derive_ptk(struct wpa_state_machine *sm, const u8 *snonce, |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 56 | const u8 *pmk, unsigned int pmk_len, |
| 57 | struct wpa_ptk *ptk); |
Dmitry Shmidt | 1d755d0 | 2015-04-28 10:34:29 -0700 | [diff] [blame] | 58 | static void wpa_group_free(struct wpa_authenticator *wpa_auth, |
| 59 | struct wpa_group *group); |
| 60 | static void wpa_group_get(struct wpa_authenticator *wpa_auth, |
| 61 | struct wpa_group *group); |
| 62 | static void wpa_group_put(struct wpa_authenticator *wpa_auth, |
| 63 | struct wpa_group *group); |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 64 | static u8 * ieee80211w_kde_add(struct wpa_state_machine *sm, u8 *pos); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 65 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 66 | static const u32 eapol_key_timeout_first = 100; /* ms */ |
| 67 | static const u32 eapol_key_timeout_subseq = 1000; /* ms */ |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 68 | static const u32 eapol_key_timeout_first_group = 500; /* ms */ |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 69 | static const u32 eapol_key_timeout_no_retrans = 4000; /* ms */ |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 70 | |
| 71 | /* TODO: make these configurable */ |
| 72 | static const int dot11RSNAConfigPMKLifetime = 43200; |
| 73 | static const int dot11RSNAConfigPMKReauthThreshold = 70; |
| 74 | static const int dot11RSNAConfigSATimeout = 60; |
| 75 | |
| 76 | |
Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 77 | static inline int wpa_auth_mic_failure_report( |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 78 | struct wpa_authenticator *wpa_auth, const u8 *addr) |
| 79 | { |
Paul Stewart | 092955c | 2017-02-06 09:13:09 -0800 | [diff] [blame] | 80 | if (wpa_auth->cb->mic_failure_report) |
| 81 | return wpa_auth->cb->mic_failure_report(wpa_auth->cb_ctx, addr); |
Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 82 | return 0; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | |
Dmitry Shmidt | dda10c2 | 2015-03-24 16:05:01 -0700 | [diff] [blame] | 86 | static inline void wpa_auth_psk_failure_report( |
| 87 | struct wpa_authenticator *wpa_auth, const u8 *addr) |
| 88 | { |
Paul Stewart | 092955c | 2017-02-06 09:13:09 -0800 | [diff] [blame] | 89 | if (wpa_auth->cb->psk_failure_report) |
| 90 | wpa_auth->cb->psk_failure_report(wpa_auth->cb_ctx, addr); |
Dmitry Shmidt | dda10c2 | 2015-03-24 16:05:01 -0700 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 94 | static inline void wpa_auth_set_eapol(struct wpa_authenticator *wpa_auth, |
| 95 | const u8 *addr, wpa_eapol_variable var, |
| 96 | int value) |
| 97 | { |
Paul Stewart | 092955c | 2017-02-06 09:13:09 -0800 | [diff] [blame] | 98 | if (wpa_auth->cb->set_eapol) |
| 99 | wpa_auth->cb->set_eapol(wpa_auth->cb_ctx, addr, var, value); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | |
| 103 | static inline int wpa_auth_get_eapol(struct wpa_authenticator *wpa_auth, |
| 104 | const u8 *addr, wpa_eapol_variable var) |
| 105 | { |
Paul Stewart | 092955c | 2017-02-06 09:13:09 -0800 | [diff] [blame] | 106 | if (wpa_auth->cb->get_eapol == NULL) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 107 | return -1; |
Paul Stewart | 092955c | 2017-02-06 09:13:09 -0800 | [diff] [blame] | 108 | return wpa_auth->cb->get_eapol(wpa_auth->cb_ctx, addr, var); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | |
| 112 | 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] | 113 | const u8 *addr, |
| 114 | const u8 *p2p_dev_addr, |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 115 | const u8 *prev_psk, size_t *psk_len) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 116 | { |
Paul Stewart | 092955c | 2017-02-06 09:13:09 -0800 | [diff] [blame] | 117 | if (wpa_auth->cb->get_psk == NULL) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 118 | return NULL; |
Paul Stewart | 092955c | 2017-02-06 09:13:09 -0800 | [diff] [blame] | 119 | return wpa_auth->cb->get_psk(wpa_auth->cb_ctx, addr, p2p_dev_addr, |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 120 | prev_psk, psk_len); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | |
| 124 | static inline int wpa_auth_get_msk(struct wpa_authenticator *wpa_auth, |
| 125 | const u8 *addr, u8 *msk, size_t *len) |
| 126 | { |
Paul Stewart | 092955c | 2017-02-06 09:13:09 -0800 | [diff] [blame] | 127 | if (wpa_auth->cb->get_msk == NULL) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 128 | return -1; |
Paul Stewart | 092955c | 2017-02-06 09:13:09 -0800 | [diff] [blame] | 129 | return wpa_auth->cb->get_msk(wpa_auth->cb_ctx, addr, msk, len); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | |
| 133 | static inline int wpa_auth_set_key(struct wpa_authenticator *wpa_auth, |
| 134 | int vlan_id, |
| 135 | enum wpa_alg alg, const u8 *addr, int idx, |
| 136 | u8 *key, size_t key_len) |
| 137 | { |
Paul Stewart | 092955c | 2017-02-06 09:13:09 -0800 | [diff] [blame] | 138 | if (wpa_auth->cb->set_key == NULL) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 139 | return -1; |
Paul Stewart | 092955c | 2017-02-06 09:13:09 -0800 | [diff] [blame] | 140 | return wpa_auth->cb->set_key(wpa_auth->cb_ctx, vlan_id, alg, addr, idx, |
| 141 | key, key_len); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | |
| 145 | static inline int wpa_auth_get_seqnum(struct wpa_authenticator *wpa_auth, |
| 146 | const u8 *addr, int idx, u8 *seq) |
| 147 | { |
Paul Stewart | 092955c | 2017-02-06 09:13:09 -0800 | [diff] [blame] | 148 | if (wpa_auth->cb->get_seqnum == NULL) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 149 | return -1; |
Paul Stewart | 092955c | 2017-02-06 09:13:09 -0800 | [diff] [blame] | 150 | return wpa_auth->cb->get_seqnum(wpa_auth->cb_ctx, addr, idx, seq); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | |
| 154 | static inline int |
| 155 | wpa_auth_send_eapol(struct wpa_authenticator *wpa_auth, const u8 *addr, |
| 156 | const u8 *data, size_t data_len, int encrypt) |
| 157 | { |
Paul Stewart | 092955c | 2017-02-06 09:13:09 -0800 | [diff] [blame] | 158 | if (wpa_auth->cb->send_eapol == NULL) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 159 | return -1; |
Paul Stewart | 092955c | 2017-02-06 09:13:09 -0800 | [diff] [blame] | 160 | return wpa_auth->cb->send_eapol(wpa_auth->cb_ctx, addr, data, data_len, |
| 161 | encrypt); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 165 | #ifdef CONFIG_MESH |
| 166 | static inline int wpa_auth_start_ampe(struct wpa_authenticator *wpa_auth, |
| 167 | const u8 *addr) |
| 168 | { |
Paul Stewart | 092955c | 2017-02-06 09:13:09 -0800 | [diff] [blame] | 169 | if (wpa_auth->cb->start_ampe == NULL) |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 170 | return -1; |
Paul Stewart | 092955c | 2017-02-06 09:13:09 -0800 | [diff] [blame] | 171 | return wpa_auth->cb->start_ampe(wpa_auth->cb_ctx, addr); |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 172 | } |
| 173 | #endif /* CONFIG_MESH */ |
| 174 | |
| 175 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 176 | int wpa_auth_for_each_sta(struct wpa_authenticator *wpa_auth, |
| 177 | int (*cb)(struct wpa_state_machine *sm, void *ctx), |
| 178 | void *cb_ctx) |
| 179 | { |
Paul Stewart | 092955c | 2017-02-06 09:13:09 -0800 | [diff] [blame] | 180 | if (wpa_auth->cb->for_each_sta == NULL) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 181 | return 0; |
Paul Stewart | 092955c | 2017-02-06 09:13:09 -0800 | [diff] [blame] | 182 | return wpa_auth->cb->for_each_sta(wpa_auth->cb_ctx, cb, cb_ctx); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | |
| 186 | int wpa_auth_for_each_auth(struct wpa_authenticator *wpa_auth, |
| 187 | int (*cb)(struct wpa_authenticator *a, void *ctx), |
| 188 | void *cb_ctx) |
| 189 | { |
Paul Stewart | 092955c | 2017-02-06 09:13:09 -0800 | [diff] [blame] | 190 | if (wpa_auth->cb->for_each_auth == NULL) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 191 | return 0; |
Paul Stewart | 092955c | 2017-02-06 09:13:09 -0800 | [diff] [blame] | 192 | return wpa_auth->cb->for_each_auth(wpa_auth->cb_ctx, cb, cb_ctx); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | |
| 196 | void wpa_auth_logger(struct wpa_authenticator *wpa_auth, const u8 *addr, |
| 197 | logger_level level, const char *txt) |
| 198 | { |
Paul Stewart | 092955c | 2017-02-06 09:13:09 -0800 | [diff] [blame] | 199 | if (wpa_auth->cb->logger == NULL) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 200 | return; |
Paul Stewart | 092955c | 2017-02-06 09:13:09 -0800 | [diff] [blame] | 201 | wpa_auth->cb->logger(wpa_auth->cb_ctx, addr, level, txt); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | |
| 205 | void wpa_auth_vlogger(struct wpa_authenticator *wpa_auth, const u8 *addr, |
| 206 | logger_level level, const char *fmt, ...) |
| 207 | { |
| 208 | char *format; |
| 209 | int maxlen; |
| 210 | va_list ap; |
| 211 | |
Paul Stewart | 092955c | 2017-02-06 09:13:09 -0800 | [diff] [blame] | 212 | if (wpa_auth->cb->logger == NULL) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 213 | return; |
| 214 | |
| 215 | maxlen = os_strlen(fmt) + 100; |
| 216 | format = os_malloc(maxlen); |
| 217 | if (!format) |
| 218 | return; |
| 219 | |
| 220 | va_start(ap, fmt); |
| 221 | vsnprintf(format, maxlen, fmt, ap); |
| 222 | va_end(ap); |
| 223 | |
| 224 | wpa_auth_logger(wpa_auth, addr, level, format); |
| 225 | |
| 226 | os_free(format); |
| 227 | } |
| 228 | |
| 229 | |
| 230 | static void wpa_sta_disconnect(struct wpa_authenticator *wpa_auth, |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 231 | const u8 *addr, u16 reason) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 232 | { |
Paul Stewart | 092955c | 2017-02-06 09:13:09 -0800 | [diff] [blame] | 233 | if (wpa_auth->cb->disconnect == NULL) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 234 | return; |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 235 | wpa_printf(MSG_DEBUG, "wpa_sta_disconnect STA " MACSTR " (reason %u)", |
| 236 | MAC2STR(addr), reason); |
| 237 | wpa_auth->cb->disconnect(wpa_auth->cb_ctx, addr, reason); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 241 | static void wpa_rekey_gmk(void *eloop_ctx, void *timeout_ctx) |
| 242 | { |
| 243 | struct wpa_authenticator *wpa_auth = eloop_ctx; |
| 244 | |
| 245 | if (random_get_bytes(wpa_auth->group->GMK, WPA_GMK_LEN)) { |
| 246 | wpa_printf(MSG_ERROR, "Failed to get random data for WPA " |
| 247 | "initialization."); |
| 248 | } else { |
| 249 | wpa_auth_logger(wpa_auth, NULL, LOGGER_DEBUG, "GMK rekeyd"); |
| 250 | wpa_hexdump_key(MSG_DEBUG, "GMK", |
| 251 | wpa_auth->group->GMK, WPA_GMK_LEN); |
| 252 | } |
| 253 | |
| 254 | if (wpa_auth->conf.wpa_gmk_rekey) { |
| 255 | eloop_register_timeout(wpa_auth->conf.wpa_gmk_rekey, 0, |
| 256 | wpa_rekey_gmk, wpa_auth, NULL); |
| 257 | } |
| 258 | } |
| 259 | |
| 260 | |
| 261 | static void wpa_rekey_gtk(void *eloop_ctx, void *timeout_ctx) |
| 262 | { |
| 263 | struct wpa_authenticator *wpa_auth = eloop_ctx; |
Dmitry Shmidt | 1d755d0 | 2015-04-28 10:34:29 -0700 | [diff] [blame] | 264 | struct wpa_group *group, *next; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 265 | |
| 266 | wpa_auth_logger(wpa_auth, NULL, LOGGER_DEBUG, "rekeying GTK"); |
Dmitry Shmidt | 1d755d0 | 2015-04-28 10:34:29 -0700 | [diff] [blame] | 267 | group = wpa_auth->group; |
| 268 | while (group) { |
| 269 | wpa_group_get(wpa_auth, group); |
| 270 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 271 | group->GTKReKey = TRUE; |
| 272 | do { |
| 273 | group->changed = FALSE; |
| 274 | wpa_group_sm_step(wpa_auth, group); |
| 275 | } while (group->changed); |
Dmitry Shmidt | 1d755d0 | 2015-04-28 10:34:29 -0700 | [diff] [blame] | 276 | |
| 277 | next = group->next; |
| 278 | wpa_group_put(wpa_auth, group); |
| 279 | group = next; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 280 | } |
| 281 | |
| 282 | if (wpa_auth->conf.wpa_group_rekey) { |
| 283 | eloop_register_timeout(wpa_auth->conf.wpa_group_rekey, |
| 284 | 0, wpa_rekey_gtk, wpa_auth, NULL); |
| 285 | } |
| 286 | } |
| 287 | |
| 288 | |
| 289 | static void wpa_rekey_ptk(void *eloop_ctx, void *timeout_ctx) |
| 290 | { |
| 291 | struct wpa_authenticator *wpa_auth = eloop_ctx; |
| 292 | struct wpa_state_machine *sm = timeout_ctx; |
| 293 | |
| 294 | wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG, "rekeying PTK"); |
| 295 | wpa_request_new_ptk(sm); |
| 296 | wpa_sm_step(sm); |
| 297 | } |
| 298 | |
| 299 | |
| 300 | static int wpa_auth_pmksa_clear_cb(struct wpa_state_machine *sm, void *ctx) |
| 301 | { |
| 302 | if (sm->pmksa == ctx) |
| 303 | sm->pmksa = NULL; |
| 304 | return 0; |
| 305 | } |
| 306 | |
| 307 | |
| 308 | static void wpa_auth_pmksa_free_cb(struct rsn_pmksa_cache_entry *entry, |
| 309 | void *ctx) |
| 310 | { |
| 311 | struct wpa_authenticator *wpa_auth = ctx; |
| 312 | wpa_auth_for_each_sta(wpa_auth, wpa_auth_pmksa_clear_cb, entry); |
| 313 | } |
| 314 | |
| 315 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 316 | static int wpa_group_init_gmk_and_counter(struct wpa_authenticator *wpa_auth, |
| 317 | struct wpa_group *group) |
| 318 | { |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 319 | u8 buf[ETH_ALEN + 8 + sizeof(unsigned long)]; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 320 | u8 rkey[32]; |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 321 | unsigned long ptr; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 322 | |
| 323 | if (random_get_bytes(group->GMK, WPA_GMK_LEN) < 0) |
| 324 | return -1; |
| 325 | wpa_hexdump_key(MSG_DEBUG, "GMK", group->GMK, WPA_GMK_LEN); |
| 326 | |
| 327 | /* |
| 328 | * Counter = PRF-256(Random number, "Init Counter", |
| 329 | * Local MAC Address || Time) |
| 330 | */ |
| 331 | os_memcpy(buf, wpa_auth->addr, ETH_ALEN); |
| 332 | wpa_get_ntp_timestamp(buf + ETH_ALEN); |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 333 | ptr = (unsigned long) group; |
| 334 | os_memcpy(buf + ETH_ALEN + 8, &ptr, sizeof(ptr)); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 335 | if (random_get_bytes(rkey, sizeof(rkey)) < 0) |
| 336 | return -1; |
| 337 | |
| 338 | if (sha1_prf(rkey, sizeof(rkey), "Init Counter", buf, sizeof(buf), |
| 339 | group->Counter, WPA_NONCE_LEN) < 0) |
| 340 | return -1; |
| 341 | wpa_hexdump_key(MSG_DEBUG, "Key Counter", |
| 342 | group->Counter, WPA_NONCE_LEN); |
| 343 | |
| 344 | return 0; |
| 345 | } |
| 346 | |
| 347 | |
| 348 | static struct wpa_group * wpa_group_init(struct wpa_authenticator *wpa_auth, |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 349 | int vlan_id, int delay_init) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 350 | { |
| 351 | struct wpa_group *group; |
| 352 | |
| 353 | group = os_zalloc(sizeof(struct wpa_group)); |
| 354 | if (group == NULL) |
| 355 | return NULL; |
| 356 | |
| 357 | group->GTKAuthenticator = TRUE; |
| 358 | group->vlan_id = vlan_id; |
Dmitry Shmidt | 4530cfd | 2012-09-09 15:20:40 -0700 | [diff] [blame] | 359 | group->GTK_len = wpa_cipher_key_len(wpa_auth->conf.wpa_group); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 360 | |
| 361 | if (random_pool_ready() != 1) { |
| 362 | wpa_printf(MSG_INFO, "WPA: Not enough entropy in random pool " |
| 363 | "for secure operations - update keys later when " |
| 364 | "the first station connects"); |
| 365 | } |
| 366 | |
| 367 | /* |
| 368 | * Set initial GMK/Counter value here. The actual values that will be |
| 369 | * used in negotiations will be set once the first station tries to |
| 370 | * connect. This allows more time for collecting additional randomness |
| 371 | * on embedded devices. |
| 372 | */ |
| 373 | if (wpa_group_init_gmk_and_counter(wpa_auth, group) < 0) { |
| 374 | wpa_printf(MSG_ERROR, "Failed to get random data for WPA " |
| 375 | "initialization."); |
| 376 | os_free(group); |
| 377 | return NULL; |
| 378 | } |
| 379 | |
| 380 | group->GInit = TRUE; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 381 | if (delay_init) { |
| 382 | wpa_printf(MSG_DEBUG, "WPA: Delay group state machine start " |
| 383 | "until Beacon frames have been configured"); |
| 384 | /* Initialization is completed in wpa_init_keys(). */ |
| 385 | } else { |
| 386 | wpa_group_sm_step(wpa_auth, group); |
| 387 | group->GInit = FALSE; |
| 388 | wpa_group_sm_step(wpa_auth, group); |
| 389 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 390 | |
| 391 | return group; |
| 392 | } |
| 393 | |
| 394 | |
| 395 | /** |
| 396 | * wpa_init - Initialize WPA authenticator |
| 397 | * @addr: Authenticator address |
| 398 | * @conf: Configuration for WPA authenticator |
| 399 | * @cb: Callback functions for WPA authenticator |
| 400 | * Returns: Pointer to WPA authenticator data or %NULL on failure |
| 401 | */ |
| 402 | struct wpa_authenticator * wpa_init(const u8 *addr, |
| 403 | struct wpa_auth_config *conf, |
Paul Stewart | 092955c | 2017-02-06 09:13:09 -0800 | [diff] [blame] | 404 | const struct wpa_auth_callbacks *cb, |
| 405 | void *cb_ctx) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 406 | { |
| 407 | struct wpa_authenticator *wpa_auth; |
| 408 | |
| 409 | wpa_auth = os_zalloc(sizeof(struct wpa_authenticator)); |
| 410 | if (wpa_auth == NULL) |
| 411 | return NULL; |
| 412 | os_memcpy(wpa_auth->addr, addr, ETH_ALEN); |
| 413 | os_memcpy(&wpa_auth->conf, conf, sizeof(*conf)); |
Paul Stewart | 092955c | 2017-02-06 09:13:09 -0800 | [diff] [blame] | 414 | wpa_auth->cb = cb; |
| 415 | wpa_auth->cb_ctx = cb_ctx; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 416 | |
| 417 | if (wpa_auth_gen_wpa_ie(wpa_auth)) { |
| 418 | wpa_printf(MSG_ERROR, "Could not generate WPA IE."); |
| 419 | os_free(wpa_auth); |
| 420 | return NULL; |
| 421 | } |
| 422 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 423 | wpa_auth->group = wpa_group_init(wpa_auth, 0, 1); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 424 | if (wpa_auth->group == NULL) { |
| 425 | os_free(wpa_auth->wpa_ie); |
| 426 | os_free(wpa_auth); |
| 427 | return NULL; |
| 428 | } |
| 429 | |
| 430 | wpa_auth->pmksa = pmksa_cache_auth_init(wpa_auth_pmksa_free_cb, |
| 431 | wpa_auth); |
| 432 | if (wpa_auth->pmksa == NULL) { |
| 433 | wpa_printf(MSG_ERROR, "PMKSA cache initialization failed."); |
Dmitry Shmidt | ff787d5 | 2015-01-12 13:01:47 -0800 | [diff] [blame] | 434 | os_free(wpa_auth->group); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 435 | os_free(wpa_auth->wpa_ie); |
| 436 | os_free(wpa_auth); |
| 437 | return NULL; |
| 438 | } |
| 439 | |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 440 | #ifdef CONFIG_IEEE80211R_AP |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 441 | wpa_auth->ft_pmk_cache = wpa_ft_pmk_cache_init(); |
| 442 | if (wpa_auth->ft_pmk_cache == NULL) { |
| 443 | wpa_printf(MSG_ERROR, "FT PMK cache initialization failed."); |
Dmitry Shmidt | ff787d5 | 2015-01-12 13:01:47 -0800 | [diff] [blame] | 444 | os_free(wpa_auth->group); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 445 | os_free(wpa_auth->wpa_ie); |
| 446 | pmksa_cache_auth_deinit(wpa_auth->pmksa); |
| 447 | os_free(wpa_auth); |
| 448 | return NULL; |
| 449 | } |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 450 | #endif /* CONFIG_IEEE80211R_AP */ |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 451 | |
| 452 | if (wpa_auth->conf.wpa_gmk_rekey) { |
| 453 | eloop_register_timeout(wpa_auth->conf.wpa_gmk_rekey, 0, |
| 454 | wpa_rekey_gmk, wpa_auth, NULL); |
| 455 | } |
| 456 | |
| 457 | if (wpa_auth->conf.wpa_group_rekey) { |
| 458 | eloop_register_timeout(wpa_auth->conf.wpa_group_rekey, 0, |
| 459 | wpa_rekey_gtk, wpa_auth, NULL); |
| 460 | } |
| 461 | |
Dmitry Shmidt | cf32e60 | 2014-01-28 10:57:39 -0800 | [diff] [blame] | 462 | #ifdef CONFIG_P2P |
| 463 | if (WPA_GET_BE32(conf->ip_addr_start)) { |
| 464 | int count = WPA_GET_BE32(conf->ip_addr_end) - |
| 465 | WPA_GET_BE32(conf->ip_addr_start) + 1; |
| 466 | if (count > 1000) |
| 467 | count = 1000; |
| 468 | if (count > 0) |
| 469 | wpa_auth->ip_pool = bitfield_alloc(count); |
| 470 | } |
| 471 | #endif /* CONFIG_P2P */ |
| 472 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 473 | return wpa_auth; |
| 474 | } |
| 475 | |
| 476 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 477 | int wpa_init_keys(struct wpa_authenticator *wpa_auth) |
| 478 | { |
| 479 | struct wpa_group *group = wpa_auth->group; |
| 480 | |
| 481 | wpa_printf(MSG_DEBUG, "WPA: Start group state machine to set initial " |
| 482 | "keys"); |
| 483 | wpa_group_sm_step(wpa_auth, group); |
| 484 | group->GInit = FALSE; |
| 485 | wpa_group_sm_step(wpa_auth, group); |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 486 | if (group->wpa_group_state == WPA_GROUP_FATAL_FAILURE) |
| 487 | return -1; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 488 | return 0; |
| 489 | } |
| 490 | |
| 491 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 492 | /** |
| 493 | * wpa_deinit - Deinitialize WPA authenticator |
| 494 | * @wpa_auth: Pointer to WPA authenticator data from wpa_init() |
| 495 | */ |
| 496 | void wpa_deinit(struct wpa_authenticator *wpa_auth) |
| 497 | { |
| 498 | struct wpa_group *group, *prev; |
| 499 | |
| 500 | eloop_cancel_timeout(wpa_rekey_gmk, wpa_auth, NULL); |
| 501 | eloop_cancel_timeout(wpa_rekey_gtk, wpa_auth, NULL); |
| 502 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 503 | pmksa_cache_auth_deinit(wpa_auth->pmksa); |
| 504 | |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 505 | #ifdef CONFIG_IEEE80211R_AP |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 506 | wpa_ft_pmk_cache_deinit(wpa_auth->ft_pmk_cache); |
| 507 | wpa_auth->ft_pmk_cache = NULL; |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 508 | wpa_ft_deinit(wpa_auth); |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 509 | #endif /* CONFIG_IEEE80211R_AP */ |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 510 | |
Dmitry Shmidt | cf32e60 | 2014-01-28 10:57:39 -0800 | [diff] [blame] | 511 | #ifdef CONFIG_P2P |
| 512 | bitfield_free(wpa_auth->ip_pool); |
| 513 | #endif /* CONFIG_P2P */ |
| 514 | |
| 515 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 516 | os_free(wpa_auth->wpa_ie); |
| 517 | |
| 518 | group = wpa_auth->group; |
| 519 | while (group) { |
| 520 | prev = group; |
| 521 | group = group->next; |
| 522 | os_free(prev); |
| 523 | } |
| 524 | |
| 525 | os_free(wpa_auth); |
| 526 | } |
| 527 | |
| 528 | |
| 529 | /** |
| 530 | * wpa_reconfig - Update WPA authenticator configuration |
| 531 | * @wpa_auth: Pointer to WPA authenticator data from wpa_init() |
| 532 | * @conf: Configuration for WPA authenticator |
| 533 | */ |
| 534 | int wpa_reconfig(struct wpa_authenticator *wpa_auth, |
| 535 | struct wpa_auth_config *conf) |
| 536 | { |
| 537 | struct wpa_group *group; |
| 538 | if (wpa_auth == NULL) |
| 539 | return 0; |
| 540 | |
| 541 | os_memcpy(&wpa_auth->conf, conf, sizeof(*conf)); |
| 542 | if (wpa_auth_gen_wpa_ie(wpa_auth)) { |
| 543 | wpa_printf(MSG_ERROR, "Could not generate WPA IE."); |
| 544 | return -1; |
| 545 | } |
| 546 | |
| 547 | /* |
| 548 | * Reinitialize GTK to make sure it is suitable for the new |
| 549 | * configuration. |
| 550 | */ |
| 551 | group = wpa_auth->group; |
Dmitry Shmidt | 4530cfd | 2012-09-09 15:20:40 -0700 | [diff] [blame] | 552 | group->GTK_len = wpa_cipher_key_len(wpa_auth->conf.wpa_group); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 553 | group->GInit = TRUE; |
| 554 | wpa_group_sm_step(wpa_auth, group); |
| 555 | group->GInit = FALSE; |
| 556 | wpa_group_sm_step(wpa_auth, group); |
| 557 | |
| 558 | return 0; |
| 559 | } |
| 560 | |
| 561 | |
| 562 | struct wpa_state_machine * |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 563 | wpa_auth_sta_init(struct wpa_authenticator *wpa_auth, const u8 *addr, |
| 564 | const u8 *p2p_dev_addr) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 565 | { |
| 566 | struct wpa_state_machine *sm; |
| 567 | |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 568 | if (wpa_auth->group->wpa_group_state == WPA_GROUP_FATAL_FAILURE) |
| 569 | return NULL; |
| 570 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 571 | sm = os_zalloc(sizeof(struct wpa_state_machine)); |
| 572 | if (sm == NULL) |
| 573 | return NULL; |
| 574 | os_memcpy(sm->addr, addr, ETH_ALEN); |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 575 | if (p2p_dev_addr) |
| 576 | os_memcpy(sm->p2p_dev_addr, p2p_dev_addr, ETH_ALEN); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 577 | |
| 578 | sm->wpa_auth = wpa_auth; |
| 579 | sm->group = wpa_auth->group; |
Dmitry Shmidt | 1d755d0 | 2015-04-28 10:34:29 -0700 | [diff] [blame] | 580 | wpa_group_get(sm->wpa_auth, sm->group); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 581 | |
| 582 | return sm; |
| 583 | } |
| 584 | |
| 585 | |
| 586 | int wpa_auth_sta_associated(struct wpa_authenticator *wpa_auth, |
| 587 | struct wpa_state_machine *sm) |
| 588 | { |
| 589 | if (wpa_auth == NULL || !wpa_auth->conf.wpa || sm == NULL) |
| 590 | return -1; |
| 591 | |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 592 | #ifdef CONFIG_IEEE80211R_AP |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 593 | if (sm->ft_completed) { |
| 594 | wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG, |
| 595 | "FT authentication already completed - do not " |
| 596 | "start 4-way handshake"); |
Dmitry Shmidt | 7175743 | 2014-06-02 13:50:35 -0700 | [diff] [blame] | 597 | /* Go to PTKINITDONE state to allow GTK rekeying */ |
| 598 | sm->wpa_ptk_state = WPA_PTK_PTKINITDONE; |
Dmitry Shmidt | ebd93af | 2017-02-21 13:40:44 -0800 | [diff] [blame] | 599 | sm->Pair = TRUE; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 600 | return 0; |
| 601 | } |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 602 | #endif /* CONFIG_IEEE80211R_AP */ |
| 603 | |
| 604 | #ifdef CONFIG_FILS |
| 605 | if (sm->fils_completed) { |
| 606 | wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG, |
| 607 | "FILS authentication already completed - do not start 4-way handshake"); |
| 608 | /* Go to PTKINITDONE state to allow GTK rekeying */ |
| 609 | sm->wpa_ptk_state = WPA_PTK_PTKINITDONE; |
Dmitry Shmidt | ebd93af | 2017-02-21 13:40:44 -0800 | [diff] [blame] | 610 | sm->Pair = TRUE; |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 611 | return 0; |
| 612 | } |
| 613 | #endif /* CONFIG_FILS */ |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 614 | |
| 615 | if (sm->started) { |
| 616 | os_memset(&sm->key_replay, 0, sizeof(sm->key_replay)); |
| 617 | sm->ReAuthenticationRequest = TRUE; |
| 618 | return wpa_sm_step(sm); |
| 619 | } |
| 620 | |
| 621 | wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG, |
| 622 | "start authentication"); |
| 623 | sm->started = 1; |
| 624 | |
| 625 | sm->Init = TRUE; |
| 626 | if (wpa_sm_step(sm) == 1) |
| 627 | return 1; /* should not really happen */ |
| 628 | sm->Init = FALSE; |
| 629 | sm->AuthenticationRequest = TRUE; |
| 630 | return wpa_sm_step(sm); |
| 631 | } |
| 632 | |
| 633 | |
| 634 | void wpa_auth_sta_no_wpa(struct wpa_state_machine *sm) |
| 635 | { |
| 636 | /* WPA/RSN was not used - clear WPA state. This is needed if the STA |
| 637 | * reassociates back to the same AP while the previous entry for the |
| 638 | * STA has not yet been removed. */ |
| 639 | if (sm == NULL) |
| 640 | return; |
| 641 | |
| 642 | sm->wpa_key_mgmt = 0; |
| 643 | } |
| 644 | |
| 645 | |
| 646 | static void wpa_free_sta_sm(struct wpa_state_machine *sm) |
| 647 | { |
Dmitry Shmidt | cf32e60 | 2014-01-28 10:57:39 -0800 | [diff] [blame] | 648 | #ifdef CONFIG_P2P |
| 649 | if (WPA_GET_BE32(sm->ip_addr)) { |
| 650 | u32 start; |
| 651 | wpa_printf(MSG_DEBUG, "P2P: Free assigned IP " |
| 652 | "address %u.%u.%u.%u from " MACSTR, |
| 653 | sm->ip_addr[0], sm->ip_addr[1], |
| 654 | sm->ip_addr[2], sm->ip_addr[3], |
| 655 | MAC2STR(sm->addr)); |
| 656 | start = WPA_GET_BE32(sm->wpa_auth->conf.ip_addr_start); |
| 657 | bitfield_clear(sm->wpa_auth->ip_pool, |
| 658 | WPA_GET_BE32(sm->ip_addr) - start); |
| 659 | } |
| 660 | #endif /* CONFIG_P2P */ |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 661 | if (sm->GUpdateStationKeys) { |
| 662 | sm->group->GKeyDoneStations--; |
| 663 | sm->GUpdateStationKeys = FALSE; |
| 664 | } |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 665 | #ifdef CONFIG_IEEE80211R_AP |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 666 | os_free(sm->assoc_resp_ftie); |
Dmitry Shmidt | d11f019 | 2014-03-24 12:09:47 -0700 | [diff] [blame] | 667 | wpabuf_free(sm->ft_pending_req_ies); |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 668 | #endif /* CONFIG_IEEE80211R_AP */ |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 669 | os_free(sm->last_rx_eapol_key); |
| 670 | os_free(sm->wpa_ie); |
Dmitry Shmidt | 1d755d0 | 2015-04-28 10:34:29 -0700 | [diff] [blame] | 671 | wpa_group_put(sm->wpa_auth, sm->group); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 672 | os_free(sm); |
| 673 | } |
| 674 | |
| 675 | |
| 676 | void wpa_auth_sta_deinit(struct wpa_state_machine *sm) |
| 677 | { |
| 678 | if (sm == NULL) |
| 679 | return; |
| 680 | |
| 681 | if (sm->wpa_auth->conf.wpa_strict_rekey && sm->has_GTK) { |
| 682 | wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG, |
| 683 | "strict rekeying - force GTK rekey since STA " |
| 684 | "is leaving"); |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame^] | 685 | if (eloop_deplete_timeout(0, 500000, wpa_rekey_gtk, |
| 686 | sm->wpa_auth, NULL) == -1) |
| 687 | eloop_register_timeout(0, 500000, wpa_rekey_gtk, sm->wpa_auth, |
| 688 | NULL); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 689 | } |
| 690 | |
| 691 | eloop_cancel_timeout(wpa_send_eapol_timeout, sm->wpa_auth, sm); |
| 692 | sm->pending_1_of_4_timeout = 0; |
| 693 | eloop_cancel_timeout(wpa_sm_call_step, sm, NULL); |
| 694 | eloop_cancel_timeout(wpa_rekey_ptk, sm->wpa_auth, sm); |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 695 | #ifdef CONFIG_IEEE80211R_AP |
| 696 | wpa_ft_sta_deinit(sm); |
| 697 | #endif /* CONFIG_IEEE80211R_AP */ |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 698 | if (sm->in_step_loop) { |
| 699 | /* Must not free state machine while wpa_sm_step() is running. |
| 700 | * Freeing will be completed in the end of wpa_sm_step(). */ |
| 701 | wpa_printf(MSG_DEBUG, "WPA: Registering pending STA state " |
| 702 | "machine deinit for " MACSTR, MAC2STR(sm->addr)); |
| 703 | sm->pending_deinit = 1; |
| 704 | } else |
| 705 | wpa_free_sta_sm(sm); |
| 706 | } |
| 707 | |
| 708 | |
| 709 | static void wpa_request_new_ptk(struct wpa_state_machine *sm) |
| 710 | { |
| 711 | if (sm == NULL) |
| 712 | return; |
| 713 | |
| 714 | sm->PTKRequest = TRUE; |
| 715 | sm->PTK_valid = 0; |
| 716 | } |
| 717 | |
| 718 | |
Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 719 | static int wpa_replay_counter_valid(struct wpa_key_replay_counter *ctr, |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 720 | const u8 *replay_counter) |
| 721 | { |
| 722 | int i; |
| 723 | for (i = 0; i < RSNA_MAX_EAPOL_RETRIES; i++) { |
Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 724 | if (!ctr[i].valid) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 725 | break; |
Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 726 | if (os_memcmp(replay_counter, ctr[i].counter, |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 727 | WPA_REPLAY_COUNTER_LEN) == 0) |
| 728 | return 1; |
| 729 | } |
| 730 | return 0; |
| 731 | } |
| 732 | |
| 733 | |
Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 734 | static void wpa_replay_counter_mark_invalid(struct wpa_key_replay_counter *ctr, |
| 735 | const u8 *replay_counter) |
| 736 | { |
| 737 | int i; |
| 738 | for (i = 0; i < RSNA_MAX_EAPOL_RETRIES; i++) { |
| 739 | if (ctr[i].valid && |
| 740 | (replay_counter == NULL || |
| 741 | os_memcmp(replay_counter, ctr[i].counter, |
| 742 | WPA_REPLAY_COUNTER_LEN) == 0)) |
| 743 | ctr[i].valid = FALSE; |
| 744 | } |
| 745 | } |
| 746 | |
| 747 | |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 748 | #ifdef CONFIG_IEEE80211R_AP |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 749 | static int ft_check_msg_2_of_4(struct wpa_authenticator *wpa_auth, |
| 750 | struct wpa_state_machine *sm, |
| 751 | struct wpa_eapol_ie_parse *kde) |
| 752 | { |
| 753 | struct wpa_ie_data ie; |
| 754 | struct rsn_mdie *mdie; |
| 755 | |
| 756 | if (wpa_parse_wpa_ie_rsn(kde->rsn_ie, kde->rsn_ie_len, &ie) < 0 || |
| 757 | ie.num_pmkid != 1 || ie.pmkid == NULL) { |
| 758 | wpa_printf(MSG_DEBUG, "FT: No PMKR1Name in " |
| 759 | "FT 4-way handshake message 2/4"); |
| 760 | return -1; |
| 761 | } |
| 762 | |
| 763 | os_memcpy(sm->sup_pmk_r1_name, ie.pmkid, PMKID_LEN); |
| 764 | wpa_hexdump(MSG_DEBUG, "FT: PMKR1Name from Supplicant", |
| 765 | sm->sup_pmk_r1_name, PMKID_LEN); |
| 766 | |
| 767 | if (!kde->mdie || !kde->ftie) { |
| 768 | wpa_printf(MSG_DEBUG, "FT: No %s in FT 4-way handshake " |
| 769 | "message 2/4", kde->mdie ? "FTIE" : "MDIE"); |
| 770 | return -1; |
| 771 | } |
| 772 | |
| 773 | mdie = (struct rsn_mdie *) (kde->mdie + 2); |
| 774 | if (kde->mdie[1] < sizeof(struct rsn_mdie) || |
| 775 | os_memcmp(wpa_auth->conf.mobility_domain, mdie->mobility_domain, |
| 776 | MOBILITY_DOMAIN_ID_LEN) != 0) { |
| 777 | wpa_printf(MSG_DEBUG, "FT: MDIE mismatch"); |
| 778 | return -1; |
| 779 | } |
| 780 | |
| 781 | if (sm->assoc_resp_ftie && |
| 782 | (kde->ftie[1] != sm->assoc_resp_ftie[1] || |
| 783 | os_memcmp(kde->ftie, sm->assoc_resp_ftie, |
| 784 | 2 + sm->assoc_resp_ftie[1]) != 0)) { |
| 785 | wpa_printf(MSG_DEBUG, "FT: FTIE mismatch"); |
| 786 | wpa_hexdump(MSG_DEBUG, "FT: FTIE in EAPOL-Key msg 2/4", |
| 787 | kde->ftie, kde->ftie_len); |
| 788 | wpa_hexdump(MSG_DEBUG, "FT: FTIE in (Re)AssocResp", |
| 789 | sm->assoc_resp_ftie, 2 + sm->assoc_resp_ftie[1]); |
| 790 | return -1; |
| 791 | } |
| 792 | |
| 793 | return 0; |
| 794 | } |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 795 | #endif /* CONFIG_IEEE80211R_AP */ |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 796 | |
| 797 | |
Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 798 | static int wpa_receive_error_report(struct wpa_authenticator *wpa_auth, |
| 799 | struct wpa_state_machine *sm, int group) |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 800 | { |
| 801 | /* Supplicant reported a Michael MIC error */ |
| 802 | wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_INFO, |
| 803 | "received EAPOL-Key Error Request " |
| 804 | "(STA detected Michael MIC failure (group=%d))", |
| 805 | group); |
| 806 | |
| 807 | if (group && wpa_auth->conf.wpa_group != WPA_CIPHER_TKIP) { |
| 808 | wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO, |
| 809 | "ignore Michael MIC failure report since " |
| 810 | "group cipher is not TKIP"); |
| 811 | } else if (!group && sm->pairwise != WPA_CIPHER_TKIP) { |
| 812 | wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO, |
| 813 | "ignore Michael MIC failure report since " |
| 814 | "pairwise cipher is not TKIP"); |
| 815 | } else { |
Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 816 | if (wpa_auth_mic_failure_report(wpa_auth, sm->addr) > 0) |
| 817 | return 1; /* STA entry was removed */ |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 818 | sm->dot11RSNAStatsTKIPRemoteMICFailures++; |
| 819 | wpa_auth->dot11RSNAStatsTKIPRemoteMICFailures++; |
| 820 | } |
| 821 | |
| 822 | /* |
| 823 | * Error report is not a request for a new key handshake, but since |
| 824 | * Authenticator may do it, let's change the keys now anyway. |
| 825 | */ |
| 826 | wpa_request_new_ptk(sm); |
Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 827 | return 0; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 828 | } |
| 829 | |
| 830 | |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 831 | static int wpa_try_alt_snonce(struct wpa_state_machine *sm, u8 *data, |
| 832 | size_t data_len) |
| 833 | { |
| 834 | struct wpa_ptk PTK; |
| 835 | int ok = 0; |
| 836 | const u8 *pmk = NULL; |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 837 | size_t pmk_len; |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 838 | |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 839 | os_memset(&PTK, 0, sizeof(PTK)); |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 840 | for (;;) { |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 841 | if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt) && |
| 842 | !wpa_key_mgmt_sae(sm->wpa_key_mgmt)) { |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 843 | pmk = wpa_auth_get_psk(sm->wpa_auth, sm->addr, |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 844 | sm->p2p_dev_addr, pmk, &pmk_len); |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 845 | if (pmk == NULL) |
| 846 | break; |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame^] | 847 | #ifdef CONFIG_IEEE80211R_AP |
| 848 | if (wpa_key_mgmt_ft_psk(sm->wpa_key_mgmt)) { |
| 849 | os_memcpy(sm->xxkey, pmk, pmk_len); |
| 850 | sm->xxkey_len = pmk_len; |
| 851 | } |
| 852 | #endif /* CONFIG_IEEE80211R_AP */ |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 853 | } else { |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 854 | pmk = sm->PMK; |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 855 | pmk_len = sm->pmk_len; |
| 856 | } |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 857 | |
Dmitry Shmidt | ebd93af | 2017-02-21 13:40:44 -0800 | [diff] [blame] | 858 | if (wpa_derive_ptk(sm, sm->alt_SNonce, pmk, pmk_len, &PTK) < 0) |
| 859 | break; |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 860 | |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 861 | if (wpa_verify_key_mic(sm->wpa_key_mgmt, pmk_len, &PTK, |
| 862 | data, data_len) == 0) { |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 863 | ok = 1; |
| 864 | break; |
| 865 | } |
| 866 | |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame^] | 867 | if (!wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt) || |
| 868 | wpa_key_mgmt_sae(sm->wpa_key_mgmt)) |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 869 | break; |
| 870 | } |
| 871 | |
| 872 | if (!ok) { |
| 873 | wpa_printf(MSG_DEBUG, |
| 874 | "WPA: Earlier SNonce did not result in matching MIC"); |
| 875 | return -1; |
| 876 | } |
| 877 | |
| 878 | wpa_printf(MSG_DEBUG, |
| 879 | "WPA: Earlier SNonce resulted in matching MIC"); |
| 880 | sm->alt_snonce_valid = 0; |
| 881 | os_memcpy(sm->SNonce, sm->alt_SNonce, WPA_NONCE_LEN); |
| 882 | os_memcpy(&sm->PTK, &PTK, sizeof(PTK)); |
| 883 | sm->PTK_valid = TRUE; |
| 884 | |
| 885 | return 0; |
| 886 | } |
| 887 | |
| 888 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 889 | void wpa_receive(struct wpa_authenticator *wpa_auth, |
| 890 | struct wpa_state_machine *sm, |
| 891 | u8 *data, size_t data_len) |
| 892 | { |
| 893 | struct ieee802_1x_hdr *hdr; |
| 894 | struct wpa_eapol_key *key; |
| 895 | u16 key_info, key_data_length; |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 896 | enum { PAIRWISE_2, PAIRWISE_4, GROUP_2, REQUEST } msg; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 897 | char *msgtxt; |
| 898 | struct wpa_eapol_ie_parse kde; |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 899 | const u8 *key_data; |
| 900 | size_t keyhdrlen, mic_len; |
| 901 | u8 *mic; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 902 | |
| 903 | if (wpa_auth == NULL || !wpa_auth->conf.wpa || sm == NULL) |
| 904 | return; |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 905 | wpa_hexdump(MSG_MSGDUMP, "WPA: RX EAPOL data", data, data_len); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 906 | |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 907 | mic_len = wpa_mic_len(sm->wpa_key_mgmt, sm->pmk_len); |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 908 | keyhdrlen = sizeof(*key) + mic_len + 2; |
Dmitry Shmidt | 807291d | 2015-01-27 13:40:23 -0800 | [diff] [blame] | 909 | |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 910 | if (data_len < sizeof(*hdr) + keyhdrlen) { |
| 911 | wpa_printf(MSG_DEBUG, "WPA: Ignore too short EAPOL-Key frame"); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 912 | return; |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 913 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 914 | |
| 915 | hdr = (struct ieee802_1x_hdr *) data; |
| 916 | key = (struct wpa_eapol_key *) (hdr + 1); |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 917 | mic = (u8 *) (key + 1); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 918 | key_info = WPA_GET_BE16(key->key_info); |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 919 | key_data = mic + mic_len + 2; |
| 920 | key_data_length = WPA_GET_BE16(mic + mic_len); |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 921 | wpa_printf(MSG_DEBUG, "WPA: Received EAPOL-Key from " MACSTR |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 922 | " key_info=0x%x type=%u mic_len=%u key_data_length=%u", |
| 923 | MAC2STR(sm->addr), key_info, key->type, |
| 924 | (unsigned int) mic_len, key_data_length); |
| 925 | wpa_hexdump(MSG_MSGDUMP, |
| 926 | "WPA: EAPOL-Key header (ending before Key MIC)", |
| 927 | key, sizeof(*key)); |
| 928 | wpa_hexdump(MSG_MSGDUMP, "WPA: EAPOL-Key Key MIC", |
| 929 | mic, mic_len); |
Dmitry Shmidt | 807291d | 2015-01-27 13:40:23 -0800 | [diff] [blame] | 930 | if (key_data_length > data_len - sizeof(*hdr) - keyhdrlen) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 931 | wpa_printf(MSG_INFO, "WPA: Invalid EAPOL-Key frame - " |
| 932 | "key_data overflow (%d > %lu)", |
| 933 | key_data_length, |
| 934 | (unsigned long) (data_len - sizeof(*hdr) - |
Dmitry Shmidt | 807291d | 2015-01-27 13:40:23 -0800 | [diff] [blame] | 935 | keyhdrlen)); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 936 | return; |
| 937 | } |
| 938 | |
| 939 | if (sm->wpa == WPA_VERSION_WPA2) { |
Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 940 | if (key->type == EAPOL_KEY_TYPE_WPA) { |
| 941 | /* |
| 942 | * Some deployed station implementations seem to send |
| 943 | * msg 4/4 with incorrect type value in WPA2 mode. |
| 944 | */ |
| 945 | wpa_printf(MSG_DEBUG, "Workaround: Allow EAPOL-Key " |
| 946 | "with unexpected WPA type in RSN mode"); |
| 947 | } else if (key->type != EAPOL_KEY_TYPE_RSN) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 948 | wpa_printf(MSG_DEBUG, "Ignore EAPOL-Key with " |
| 949 | "unexpected type %d in RSN mode", |
| 950 | key->type); |
| 951 | return; |
| 952 | } |
| 953 | } else { |
| 954 | if (key->type != EAPOL_KEY_TYPE_WPA) { |
| 955 | wpa_printf(MSG_DEBUG, "Ignore EAPOL-Key with " |
| 956 | "unexpected type %d in WPA mode", |
| 957 | key->type); |
| 958 | return; |
| 959 | } |
| 960 | } |
| 961 | |
| 962 | wpa_hexdump(MSG_DEBUG, "WPA: Received Key Nonce", key->key_nonce, |
| 963 | WPA_NONCE_LEN); |
| 964 | wpa_hexdump(MSG_DEBUG, "WPA: Received Replay Counter", |
| 965 | key->replay_counter, WPA_REPLAY_COUNTER_LEN); |
| 966 | |
| 967 | /* FIX: verify that the EAPOL-Key frame was encrypted if pairwise keys |
| 968 | * are set */ |
| 969 | |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 970 | if (key_info & WPA_KEY_INFO_SMK_MESSAGE) { |
| 971 | wpa_printf(MSG_DEBUG, "WPA: Ignore SMK message"); |
| 972 | return; |
| 973 | } |
| 974 | |
| 975 | if (key_info & WPA_KEY_INFO_REQUEST) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 976 | msg = REQUEST; |
| 977 | msgtxt = "Request"; |
| 978 | } else if (!(key_info & WPA_KEY_INFO_KEY_TYPE)) { |
| 979 | msg = GROUP_2; |
| 980 | msgtxt = "2/2 Group"; |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 981 | } else if (key_data_length == 0 || |
| 982 | (mic_len == 0 && (key_info & WPA_KEY_INFO_ENCR_KEY_DATA) && |
| 983 | key_data_length == AES_BLOCK_SIZE)) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 984 | msg = PAIRWISE_4; |
| 985 | msgtxt = "4/4 Pairwise"; |
| 986 | } else { |
| 987 | msg = PAIRWISE_2; |
| 988 | msgtxt = "2/4 Pairwise"; |
| 989 | } |
| 990 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 991 | if (msg == REQUEST || msg == PAIRWISE_2 || msg == PAIRWISE_4 || |
| 992 | msg == GROUP_2) { |
| 993 | u16 ver = key_info & WPA_KEY_INFO_TYPE_MASK; |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 994 | if (sm->pairwise == WPA_CIPHER_CCMP || |
| 995 | sm->pairwise == WPA_CIPHER_GCMP) { |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame^] | 996 | if (wpa_use_cmac(sm->wpa_key_mgmt) && |
| 997 | !wpa_use_akm_defined(sm->wpa_key_mgmt) && |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 998 | ver != WPA_KEY_INFO_TYPE_AES_128_CMAC) { |
| 999 | wpa_auth_logger(wpa_auth, sm->addr, |
| 1000 | LOGGER_WARNING, |
| 1001 | "advertised support for " |
| 1002 | "AES-128-CMAC, but did not " |
| 1003 | "use it"); |
| 1004 | return; |
| 1005 | } |
| 1006 | |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame^] | 1007 | if (!wpa_use_cmac(sm->wpa_key_mgmt) && |
| 1008 | !wpa_use_akm_defined(sm->wpa_key_mgmt) && |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1009 | ver != WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) { |
| 1010 | wpa_auth_logger(wpa_auth, sm->addr, |
| 1011 | LOGGER_WARNING, |
| 1012 | "did not use HMAC-SHA1-AES " |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 1013 | "with CCMP/GCMP"); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1014 | return; |
| 1015 | } |
| 1016 | } |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 1017 | |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame^] | 1018 | if (wpa_use_akm_defined(sm->wpa_key_mgmt) && |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 1019 | ver != WPA_KEY_INFO_TYPE_AKM_DEFINED) { |
| 1020 | wpa_auth_logger(wpa_auth, sm->addr, LOGGER_WARNING, |
| 1021 | "did not use EAPOL-Key descriptor version 0 as required for AKM-defined cases"); |
| 1022 | return; |
| 1023 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1024 | } |
| 1025 | |
| 1026 | if (key_info & WPA_KEY_INFO_REQUEST) { |
| 1027 | if (sm->req_replay_counter_used && |
| 1028 | os_memcmp(key->replay_counter, sm->req_replay_counter, |
| 1029 | WPA_REPLAY_COUNTER_LEN) <= 0) { |
| 1030 | wpa_auth_logger(wpa_auth, sm->addr, LOGGER_WARNING, |
| 1031 | "received EAPOL-Key request with " |
| 1032 | "replayed counter"); |
| 1033 | return; |
| 1034 | } |
| 1035 | } |
| 1036 | |
| 1037 | if (!(key_info & WPA_KEY_INFO_REQUEST) && |
Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 1038 | !wpa_replay_counter_valid(sm->key_replay, key->replay_counter)) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1039 | int i; |
Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 1040 | |
| 1041 | if (msg == PAIRWISE_2 && |
| 1042 | wpa_replay_counter_valid(sm->prev_key_replay, |
| 1043 | key->replay_counter) && |
| 1044 | sm->wpa_ptk_state == WPA_PTK_PTKINITNEGOTIATING && |
| 1045 | os_memcmp(sm->SNonce, key->key_nonce, WPA_NONCE_LEN) != 0) |
| 1046 | { |
| 1047 | /* |
| 1048 | * Some supplicant implementations (e.g., Windows XP |
| 1049 | * WZC) update SNonce for each EAPOL-Key 2/4. This |
| 1050 | * breaks the workaround on accepting any of the |
| 1051 | * pending requests, so allow the SNonce to be updated |
| 1052 | * even if we have already sent out EAPOL-Key 3/4. |
| 1053 | */ |
| 1054 | wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_DEBUG, |
| 1055 | "Process SNonce update from STA " |
| 1056 | "based on retransmitted EAPOL-Key " |
| 1057 | "1/4"); |
| 1058 | sm->update_snonce = 1; |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 1059 | os_memcpy(sm->alt_SNonce, sm->SNonce, WPA_NONCE_LEN); |
| 1060 | sm->alt_snonce_valid = TRUE; |
| 1061 | os_memcpy(sm->alt_replay_counter, |
| 1062 | sm->key_replay[0].counter, |
| 1063 | WPA_REPLAY_COUNTER_LEN); |
| 1064 | goto continue_processing; |
| 1065 | } |
| 1066 | |
| 1067 | if (msg == PAIRWISE_4 && sm->alt_snonce_valid && |
| 1068 | sm->wpa_ptk_state == WPA_PTK_PTKINITNEGOTIATING && |
| 1069 | os_memcmp(key->replay_counter, sm->alt_replay_counter, |
| 1070 | WPA_REPLAY_COUNTER_LEN) == 0) { |
| 1071 | /* |
| 1072 | * Supplicant may still be using the old SNonce since |
| 1073 | * there was two EAPOL-Key 2/4 messages and they had |
| 1074 | * different SNonce values. |
| 1075 | */ |
| 1076 | wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_DEBUG, |
| 1077 | "Try to process received EAPOL-Key 4/4 based on old Replay Counter and SNonce from an earlier EAPOL-Key 1/4"); |
Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 1078 | goto continue_processing; |
| 1079 | } |
| 1080 | |
| 1081 | if (msg == PAIRWISE_2 && |
| 1082 | wpa_replay_counter_valid(sm->prev_key_replay, |
| 1083 | key->replay_counter) && |
| 1084 | sm->wpa_ptk_state == WPA_PTK_PTKINITNEGOTIATING) { |
| 1085 | wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_DEBUG, |
| 1086 | "ignore retransmitted EAPOL-Key %s - " |
| 1087 | "SNonce did not change", msgtxt); |
| 1088 | } else { |
| 1089 | wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_DEBUG, |
| 1090 | "received EAPOL-Key %s with " |
| 1091 | "unexpected replay counter", msgtxt); |
| 1092 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1093 | for (i = 0; i < RSNA_MAX_EAPOL_RETRIES; i++) { |
| 1094 | if (!sm->key_replay[i].valid) |
| 1095 | break; |
| 1096 | wpa_hexdump(MSG_DEBUG, "pending replay counter", |
| 1097 | sm->key_replay[i].counter, |
| 1098 | WPA_REPLAY_COUNTER_LEN); |
| 1099 | } |
| 1100 | wpa_hexdump(MSG_DEBUG, "received replay counter", |
| 1101 | key->replay_counter, WPA_REPLAY_COUNTER_LEN); |
| 1102 | return; |
| 1103 | } |
| 1104 | |
Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 1105 | continue_processing: |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 1106 | #ifdef CONFIG_FILS |
| 1107 | if (sm->wpa == WPA_VERSION_WPA2 && mic_len == 0 && |
| 1108 | !(key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) { |
| 1109 | wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_DEBUG, |
| 1110 | "WPA: Encr Key Data bit not set even though AEAD cipher is supposed to be used - drop frame"); |
| 1111 | return; |
| 1112 | } |
| 1113 | #endif /* CONFIG_FILS */ |
| 1114 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1115 | switch (msg) { |
| 1116 | case PAIRWISE_2: |
| 1117 | if (sm->wpa_ptk_state != WPA_PTK_PTKSTART && |
Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 1118 | sm->wpa_ptk_state != WPA_PTK_PTKCALCNEGOTIATING && |
| 1119 | (!sm->update_snonce || |
| 1120 | sm->wpa_ptk_state != WPA_PTK_PTKINITNEGOTIATING)) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1121 | wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_INFO, |
| 1122 | "received EAPOL-Key msg 2/4 in " |
| 1123 | "invalid state (%d) - dropped", |
| 1124 | sm->wpa_ptk_state); |
| 1125 | return; |
| 1126 | } |
| 1127 | random_add_randomness(key->key_nonce, WPA_NONCE_LEN); |
| 1128 | if (sm->group->reject_4way_hs_for_entropy) { |
| 1129 | /* |
| 1130 | * The system did not have enough entropy to generate |
| 1131 | * strong random numbers. Reject the first 4-way |
| 1132 | * handshake(s) and collect some entropy based on the |
| 1133 | * information from it. Once enough entropy is |
| 1134 | * available, the next atempt will trigger GMK/Key |
| 1135 | * Counter update and the station will be allowed to |
| 1136 | * continue. |
| 1137 | */ |
| 1138 | wpa_printf(MSG_DEBUG, "WPA: Reject 4-way handshake to " |
| 1139 | "collect more entropy for random number " |
| 1140 | "generation"); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1141 | random_mark_pool_ready(); |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 1142 | wpa_sta_disconnect(wpa_auth, sm->addr, |
| 1143 | WLAN_REASON_PREV_AUTH_NOT_VALID); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1144 | return; |
| 1145 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1146 | break; |
| 1147 | case PAIRWISE_4: |
| 1148 | if (sm->wpa_ptk_state != WPA_PTK_PTKINITNEGOTIATING || |
| 1149 | !sm->PTK_valid) { |
| 1150 | wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_INFO, |
| 1151 | "received EAPOL-Key msg 4/4 in " |
| 1152 | "invalid state (%d) - dropped", |
| 1153 | sm->wpa_ptk_state); |
| 1154 | return; |
| 1155 | } |
| 1156 | break; |
| 1157 | case GROUP_2: |
| 1158 | if (sm->wpa_ptk_group_state != WPA_PTK_GROUP_REKEYNEGOTIATING |
| 1159 | || !sm->PTK_valid) { |
| 1160 | wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_INFO, |
| 1161 | "received EAPOL-Key msg 2/2 in " |
| 1162 | "invalid state (%d) - dropped", |
| 1163 | sm->wpa_ptk_group_state); |
| 1164 | return; |
| 1165 | } |
| 1166 | break; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1167 | case REQUEST: |
| 1168 | break; |
| 1169 | } |
| 1170 | |
| 1171 | wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_DEBUG, |
| 1172 | "received EAPOL-Key frame (%s)", msgtxt); |
| 1173 | |
| 1174 | if (key_info & WPA_KEY_INFO_ACK) { |
| 1175 | wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO, |
| 1176 | "received invalid EAPOL-Key: Key Ack set"); |
| 1177 | return; |
| 1178 | } |
| 1179 | |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 1180 | if (!wpa_key_mgmt_fils(sm->wpa_key_mgmt) && |
| 1181 | !(key_info & WPA_KEY_INFO_MIC)) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1182 | wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO, |
| 1183 | "received invalid EAPOL-Key: Key MIC not set"); |
| 1184 | return; |
| 1185 | } |
| 1186 | |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 1187 | #ifdef CONFIG_FILS |
| 1188 | if (wpa_key_mgmt_fils(sm->wpa_key_mgmt) && |
| 1189 | (key_info & WPA_KEY_INFO_MIC)) { |
| 1190 | wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO, |
| 1191 | "received invalid EAPOL-Key: Key MIC set"); |
| 1192 | return; |
| 1193 | } |
| 1194 | #endif /* CONFIG_FILS */ |
| 1195 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1196 | sm->MICVerified = FALSE; |
Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 1197 | if (sm->PTK_valid && !sm->update_snonce) { |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 1198 | if (mic_len && |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 1199 | wpa_verify_key_mic(sm->wpa_key_mgmt, sm->pmk_len, &sm->PTK, |
| 1200 | data, data_len) && |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 1201 | (msg != PAIRWISE_4 || !sm->alt_snonce_valid || |
| 1202 | wpa_try_alt_snonce(sm, data, data_len))) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1203 | wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO, |
| 1204 | "received EAPOL-Key with invalid MIC"); |
| 1205 | return; |
| 1206 | } |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 1207 | #ifdef CONFIG_FILS |
| 1208 | if (!mic_len && |
| 1209 | wpa_aead_decrypt(sm, &sm->PTK, data, data_len, |
| 1210 | &key_data_length) < 0) { |
| 1211 | wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO, |
| 1212 | "received EAPOL-Key with invalid MIC"); |
| 1213 | return; |
| 1214 | } |
| 1215 | #endif /* CONFIG_FILS */ |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1216 | sm->MICVerified = TRUE; |
| 1217 | eloop_cancel_timeout(wpa_send_eapol_timeout, wpa_auth, sm); |
| 1218 | sm->pending_1_of_4_timeout = 0; |
| 1219 | } |
| 1220 | |
| 1221 | if (key_info & WPA_KEY_INFO_REQUEST) { |
| 1222 | if (sm->MICVerified) { |
| 1223 | sm->req_replay_counter_used = 1; |
| 1224 | os_memcpy(sm->req_replay_counter, key->replay_counter, |
| 1225 | WPA_REPLAY_COUNTER_LEN); |
| 1226 | } else { |
| 1227 | wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO, |
| 1228 | "received EAPOL-Key request with " |
| 1229 | "invalid MIC"); |
| 1230 | return; |
| 1231 | } |
| 1232 | |
| 1233 | /* |
| 1234 | * TODO: should decrypt key data field if encryption was used; |
| 1235 | * even though MAC address KDE is not normally encrypted, |
| 1236 | * supplicant is allowed to encrypt it. |
| 1237 | */ |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 1238 | if (key_info & WPA_KEY_INFO_ERROR) { |
Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 1239 | if (wpa_receive_error_report( |
| 1240 | wpa_auth, sm, |
| 1241 | !(key_info & WPA_KEY_INFO_KEY_TYPE)) > 0) |
| 1242 | return; /* STA entry was removed */ |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1243 | } else if (key_info & WPA_KEY_INFO_KEY_TYPE) { |
| 1244 | wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO, |
| 1245 | "received EAPOL-Key Request for new " |
| 1246 | "4-Way Handshake"); |
| 1247 | wpa_request_new_ptk(sm); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1248 | } else if (key_data_length > 0 && |
Dmitry Shmidt | 807291d | 2015-01-27 13:40:23 -0800 | [diff] [blame] | 1249 | wpa_parse_kde_ies(key_data, key_data_length, |
| 1250 | &kde) == 0 && |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1251 | kde.mac_addr) { |
| 1252 | } else { |
| 1253 | wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO, |
| 1254 | "received EAPOL-Key Request for GTK " |
| 1255 | "rekeying"); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1256 | eloop_cancel_timeout(wpa_rekey_gtk, wpa_auth, NULL); |
| 1257 | wpa_rekey_gtk(wpa_auth, NULL); |
| 1258 | } |
| 1259 | } else { |
Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 1260 | /* Do not allow the same key replay counter to be reused. */ |
| 1261 | wpa_replay_counter_mark_invalid(sm->key_replay, |
| 1262 | key->replay_counter); |
| 1263 | |
| 1264 | if (msg == PAIRWISE_2) { |
| 1265 | /* |
| 1266 | * Maintain a copy of the pending EAPOL-Key frames in |
| 1267 | * case the EAPOL-Key frame was retransmitted. This is |
| 1268 | * needed to allow EAPOL-Key msg 2/4 reply to another |
| 1269 | * pending msg 1/4 to update the SNonce to work around |
| 1270 | * unexpected supplicant behavior. |
| 1271 | */ |
| 1272 | os_memcpy(sm->prev_key_replay, sm->key_replay, |
| 1273 | sizeof(sm->key_replay)); |
| 1274 | } else { |
| 1275 | os_memset(sm->prev_key_replay, 0, |
| 1276 | sizeof(sm->prev_key_replay)); |
| 1277 | } |
| 1278 | |
| 1279 | /* |
| 1280 | * Make sure old valid counters are not accepted anymore and |
| 1281 | * do not get copied again. |
| 1282 | */ |
| 1283 | wpa_replay_counter_mark_invalid(sm->key_replay, NULL); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1284 | } |
| 1285 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1286 | os_free(sm->last_rx_eapol_key); |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 1287 | sm->last_rx_eapol_key = os_memdup(data, data_len); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1288 | if (sm->last_rx_eapol_key == NULL) |
| 1289 | return; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1290 | sm->last_rx_eapol_key_len = data_len; |
| 1291 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 1292 | sm->rx_eapol_key_secure = !!(key_info & WPA_KEY_INFO_SECURE); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1293 | sm->EAPOLKeyReceived = TRUE; |
| 1294 | sm->EAPOLKeyPairwise = !!(key_info & WPA_KEY_INFO_KEY_TYPE); |
| 1295 | sm->EAPOLKeyRequest = !!(key_info & WPA_KEY_INFO_REQUEST); |
| 1296 | os_memcpy(sm->SNonce, key->key_nonce, WPA_NONCE_LEN); |
| 1297 | wpa_sm_step(sm); |
| 1298 | } |
| 1299 | |
| 1300 | |
| 1301 | static int wpa_gmk_to_gtk(const u8 *gmk, const char *label, const u8 *addr, |
| 1302 | const u8 *gnonce, u8 *gtk, size_t gtk_len) |
| 1303 | { |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame^] | 1304 | u8 data[ETH_ALEN + WPA_NONCE_LEN + 8 + WPA_GTK_MAX_LEN]; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1305 | u8 *pos; |
| 1306 | int ret = 0; |
| 1307 | |
| 1308 | /* GTK = PRF-X(GMK, "Group key expansion", |
| 1309 | * AA || GNonce || Time || random data) |
| 1310 | * The example described in the IEEE 802.11 standard uses only AA and |
| 1311 | * GNonce as inputs here. Add some more entropy since this derivation |
| 1312 | * is done only at the Authenticator and as such, does not need to be |
| 1313 | * exactly same. |
| 1314 | */ |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame^] | 1315 | os_memset(data, 0, sizeof(data)); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1316 | os_memcpy(data, addr, ETH_ALEN); |
| 1317 | os_memcpy(data + ETH_ALEN, gnonce, WPA_NONCE_LEN); |
| 1318 | pos = data + ETH_ALEN + WPA_NONCE_LEN; |
| 1319 | wpa_get_ntp_timestamp(pos); |
| 1320 | pos += 8; |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame^] | 1321 | if (random_get_bytes(pos, gtk_len) < 0) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1322 | ret = -1; |
| 1323 | |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame^] | 1324 | #ifdef CONFIG_SHA384 |
| 1325 | if (sha384_prf(gmk, WPA_GMK_LEN, label, data, sizeof(data), |
| 1326 | gtk, gtk_len) < 0) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1327 | ret = -1; |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame^] | 1328 | #else /* CONFIG_SHA384 */ |
| 1329 | #ifdef CONFIG_SHA256 |
| 1330 | if (sha256_prf(gmk, WPA_GMK_LEN, label, data, sizeof(data), |
| 1331 | gtk, gtk_len) < 0) |
| 1332 | ret = -1; |
| 1333 | #else /* CONFIG_SHA256 */ |
| 1334 | if (sha1_prf(gmk, WPA_GMK_LEN, label, data, sizeof(data), |
| 1335 | gtk, gtk_len) < 0) |
| 1336 | ret = -1; |
| 1337 | #endif /* CONFIG_SHA256 */ |
| 1338 | #endif /* CONFIG_SHA384 */ |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1339 | |
| 1340 | return ret; |
| 1341 | } |
| 1342 | |
| 1343 | |
| 1344 | static void wpa_send_eapol_timeout(void *eloop_ctx, void *timeout_ctx) |
| 1345 | { |
| 1346 | struct wpa_authenticator *wpa_auth = eloop_ctx; |
| 1347 | struct wpa_state_machine *sm = timeout_ctx; |
| 1348 | |
| 1349 | sm->pending_1_of_4_timeout = 0; |
| 1350 | wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG, "EAPOL-Key timeout"); |
| 1351 | sm->TimeoutEvt = TRUE; |
| 1352 | wpa_sm_step(sm); |
| 1353 | } |
| 1354 | |
| 1355 | |
| 1356 | void __wpa_send_eapol(struct wpa_authenticator *wpa_auth, |
| 1357 | struct wpa_state_machine *sm, int key_info, |
| 1358 | const u8 *key_rsc, const u8 *nonce, |
| 1359 | const u8 *kde, size_t kde_len, |
| 1360 | int keyidx, int encr, int force_version) |
| 1361 | { |
| 1362 | struct ieee802_1x_hdr *hdr; |
| 1363 | struct wpa_eapol_key *key; |
Dmitry Shmidt | 807291d | 2015-01-27 13:40:23 -0800 | [diff] [blame] | 1364 | size_t len, mic_len, keyhdrlen; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1365 | int alg; |
| 1366 | int key_data_len, pad_len = 0; |
| 1367 | u8 *buf, *pos; |
| 1368 | int version, pairwise; |
| 1369 | int i; |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 1370 | u8 *key_mic, *key_data; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1371 | |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 1372 | mic_len = wpa_mic_len(sm->wpa_key_mgmt, sm->pmk_len); |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 1373 | keyhdrlen = sizeof(*key) + mic_len + 2; |
Dmitry Shmidt | 807291d | 2015-01-27 13:40:23 -0800 | [diff] [blame] | 1374 | |
| 1375 | len = sizeof(struct ieee802_1x_hdr) + keyhdrlen; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1376 | |
| 1377 | if (force_version) |
| 1378 | version = force_version; |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame^] | 1379 | else if (wpa_use_akm_defined(sm->wpa_key_mgmt)) |
Dmitry Shmidt | f21452a | 2014-02-26 10:55:25 -0800 | [diff] [blame] | 1380 | version = WPA_KEY_INFO_TYPE_AKM_DEFINED; |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame^] | 1381 | else if (wpa_use_cmac(sm->wpa_key_mgmt)) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1382 | version = WPA_KEY_INFO_TYPE_AES_128_CMAC; |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 1383 | else if (sm->pairwise != WPA_CIPHER_TKIP) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1384 | version = WPA_KEY_INFO_TYPE_HMAC_SHA1_AES; |
| 1385 | else |
| 1386 | version = WPA_KEY_INFO_TYPE_HMAC_MD5_RC4; |
| 1387 | |
Dmitry Shmidt | 51b6ea8 | 2013-05-08 10:42:09 -0700 | [diff] [blame] | 1388 | pairwise = !!(key_info & WPA_KEY_INFO_KEY_TYPE); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1389 | |
| 1390 | wpa_printf(MSG_DEBUG, "WPA: Send EAPOL(version=%d secure=%d mic=%d " |
| 1391 | "ack=%d install=%d pairwise=%d kde_len=%lu keyidx=%d " |
| 1392 | "encr=%d)", |
| 1393 | version, |
| 1394 | (key_info & WPA_KEY_INFO_SECURE) ? 1 : 0, |
| 1395 | (key_info & WPA_KEY_INFO_MIC) ? 1 : 0, |
| 1396 | (key_info & WPA_KEY_INFO_ACK) ? 1 : 0, |
| 1397 | (key_info & WPA_KEY_INFO_INSTALL) ? 1 : 0, |
| 1398 | pairwise, (unsigned long) kde_len, keyidx, encr); |
| 1399 | |
| 1400 | key_data_len = kde_len; |
| 1401 | |
| 1402 | if ((version == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES || |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame^] | 1403 | wpa_use_aes_key_wrap(sm->wpa_key_mgmt) || |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1404 | version == WPA_KEY_INFO_TYPE_AES_128_CMAC) && encr) { |
| 1405 | pad_len = key_data_len % 8; |
| 1406 | if (pad_len) |
| 1407 | pad_len = 8 - pad_len; |
| 1408 | key_data_len += pad_len + 8; |
| 1409 | } |
| 1410 | |
| 1411 | len += key_data_len; |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 1412 | if (!mic_len && encr) |
| 1413 | len += AES_BLOCK_SIZE; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1414 | |
| 1415 | hdr = os_zalloc(len); |
| 1416 | if (hdr == NULL) |
| 1417 | return; |
| 1418 | hdr->version = wpa_auth->conf.eapol_version; |
| 1419 | hdr->type = IEEE802_1X_TYPE_EAPOL_KEY; |
| 1420 | hdr->length = host_to_be16(len - sizeof(*hdr)); |
| 1421 | key = (struct wpa_eapol_key *) (hdr + 1); |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 1422 | key_mic = (u8 *) (key + 1); |
Dmitry Shmidt | 807291d | 2015-01-27 13:40:23 -0800 | [diff] [blame] | 1423 | key_data = ((u8 *) (hdr + 1)) + keyhdrlen; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1424 | |
| 1425 | key->type = sm->wpa == WPA_VERSION_WPA2 ? |
| 1426 | EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA; |
| 1427 | key_info |= version; |
| 1428 | if (encr && sm->wpa == WPA_VERSION_WPA2) |
| 1429 | key_info |= WPA_KEY_INFO_ENCR_KEY_DATA; |
| 1430 | if (sm->wpa != WPA_VERSION_WPA2) |
| 1431 | key_info |= keyidx << WPA_KEY_INFO_KEY_INDEX_SHIFT; |
| 1432 | WPA_PUT_BE16(key->key_info, key_info); |
| 1433 | |
| 1434 | alg = pairwise ? sm->pairwise : wpa_auth->conf.wpa_group; |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 1435 | if (sm->wpa == WPA_VERSION_WPA2 && !pairwise) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1436 | WPA_PUT_BE16(key->key_length, 0); |
Dmitry Shmidt | ebd93af | 2017-02-21 13:40:44 -0800 | [diff] [blame] | 1437 | else |
| 1438 | WPA_PUT_BE16(key->key_length, wpa_cipher_key_len(alg)); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1439 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1440 | for (i = RSNA_MAX_EAPOL_RETRIES - 1; i > 0; i--) { |
| 1441 | sm->key_replay[i].valid = sm->key_replay[i - 1].valid; |
| 1442 | os_memcpy(sm->key_replay[i].counter, |
| 1443 | sm->key_replay[i - 1].counter, |
| 1444 | WPA_REPLAY_COUNTER_LEN); |
| 1445 | } |
| 1446 | inc_byte_array(sm->key_replay[0].counter, WPA_REPLAY_COUNTER_LEN); |
| 1447 | os_memcpy(key->replay_counter, sm->key_replay[0].counter, |
| 1448 | WPA_REPLAY_COUNTER_LEN); |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 1449 | wpa_hexdump(MSG_DEBUG, "WPA: Replay Counter", |
| 1450 | key->replay_counter, WPA_REPLAY_COUNTER_LEN); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1451 | sm->key_replay[0].valid = TRUE; |
| 1452 | |
| 1453 | if (nonce) |
| 1454 | os_memcpy(key->key_nonce, nonce, WPA_NONCE_LEN); |
| 1455 | |
| 1456 | if (key_rsc) |
| 1457 | os_memcpy(key->key_rsc, key_rsc, WPA_KEY_RSC_LEN); |
| 1458 | |
| 1459 | if (kde && !encr) { |
Dmitry Shmidt | 807291d | 2015-01-27 13:40:23 -0800 | [diff] [blame] | 1460 | os_memcpy(key_data, kde, kde_len); |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 1461 | WPA_PUT_BE16(key_mic + mic_len, kde_len); |
| 1462 | #ifdef CONFIG_FILS |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame^] | 1463 | } else if (!mic_len && kde) { |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 1464 | const u8 *aad[1]; |
| 1465 | size_t aad_len[1]; |
| 1466 | |
| 1467 | WPA_PUT_BE16(key_mic, AES_BLOCK_SIZE + kde_len); |
| 1468 | wpa_hexdump_key(MSG_DEBUG, "Plaintext EAPOL-Key Key Data", |
| 1469 | kde, kde_len); |
| 1470 | |
| 1471 | wpa_hexdump_key(MSG_DEBUG, "WPA: KEK", |
| 1472 | sm->PTK.kek, sm->PTK.kek_len); |
| 1473 | /* AES-SIV AAD from EAPOL protocol version field (inclusive) to |
| 1474 | * to Key Data (exclusive). */ |
| 1475 | aad[0] = (u8 *) hdr; |
| 1476 | aad_len[0] = key_mic + 2 - (u8 *) hdr; |
| 1477 | if (aes_siv_encrypt(sm->PTK.kek, sm->PTK.kek_len, kde, kde_len, |
| 1478 | 1, aad, aad_len, key_mic + 2) < 0) { |
| 1479 | wpa_printf(MSG_DEBUG, "WPA: AES-SIV encryption failed"); |
| 1480 | return; |
| 1481 | } |
| 1482 | |
| 1483 | wpa_hexdump(MSG_DEBUG, "WPA: Encrypted Key Data from SIV", |
| 1484 | key_mic + 2, AES_BLOCK_SIZE + kde_len); |
| 1485 | #endif /* CONFIG_FILS */ |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1486 | } else if (encr && kde) { |
| 1487 | buf = os_zalloc(key_data_len); |
| 1488 | if (buf == NULL) { |
| 1489 | os_free(hdr); |
| 1490 | return; |
| 1491 | } |
| 1492 | pos = buf; |
| 1493 | os_memcpy(pos, kde, kde_len); |
| 1494 | pos += kde_len; |
| 1495 | |
| 1496 | if (pad_len) |
| 1497 | *pos++ = 0xdd; |
| 1498 | |
| 1499 | wpa_hexdump_key(MSG_DEBUG, "Plaintext EAPOL-Key Key Data", |
| 1500 | buf, key_data_len); |
| 1501 | if (version == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES || |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame^] | 1502 | wpa_use_aes_key_wrap(sm->wpa_key_mgmt) || |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1503 | version == WPA_KEY_INFO_TYPE_AES_128_CMAC) { |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 1504 | wpa_printf(MSG_DEBUG, |
| 1505 | "WPA: Encrypt Key Data using AES-WRAP (KEK length %u)", |
| 1506 | (unsigned int) sm->PTK.kek_len); |
Dmitry Shmidt | 807291d | 2015-01-27 13:40:23 -0800 | [diff] [blame] | 1507 | if (aes_wrap(sm->PTK.kek, sm->PTK.kek_len, |
| 1508 | (key_data_len - 8) / 8, buf, key_data)) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1509 | os_free(hdr); |
| 1510 | os_free(buf); |
| 1511 | return; |
| 1512 | } |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 1513 | WPA_PUT_BE16(key_mic + mic_len, key_data_len); |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 1514 | #ifndef CONFIG_NO_RC4 |
Dmitry Shmidt | 807291d | 2015-01-27 13:40:23 -0800 | [diff] [blame] | 1515 | } else if (sm->PTK.kek_len == 16) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1516 | u8 ek[32]; |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 1517 | |
| 1518 | wpa_printf(MSG_DEBUG, |
| 1519 | "WPA: Encrypt Key Data using RC4"); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1520 | os_memcpy(key->key_iv, |
| 1521 | sm->group->Counter + WPA_NONCE_LEN - 16, 16); |
| 1522 | inc_byte_array(sm->group->Counter, WPA_NONCE_LEN); |
| 1523 | os_memcpy(ek, key->key_iv, 16); |
Dmitry Shmidt | 807291d | 2015-01-27 13:40:23 -0800 | [diff] [blame] | 1524 | os_memcpy(ek + 16, sm->PTK.kek, sm->PTK.kek_len); |
| 1525 | os_memcpy(key_data, buf, key_data_len); |
| 1526 | rc4_skip(ek, 32, 256, key_data, key_data_len); |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 1527 | WPA_PUT_BE16(key_mic + mic_len, key_data_len); |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 1528 | #endif /* CONFIG_NO_RC4 */ |
Dmitry Shmidt | 807291d | 2015-01-27 13:40:23 -0800 | [diff] [blame] | 1529 | } else { |
| 1530 | os_free(hdr); |
| 1531 | os_free(buf); |
| 1532 | return; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1533 | } |
| 1534 | os_free(buf); |
| 1535 | } |
| 1536 | |
| 1537 | if (key_info & WPA_KEY_INFO_MIC) { |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 1538 | if (!sm->PTK_valid || !mic_len) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1539 | wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG, |
| 1540 | "PTK not valid when sending EAPOL-Key " |
| 1541 | "frame"); |
| 1542 | os_free(hdr); |
| 1543 | return; |
| 1544 | } |
Dmitry Shmidt | 807291d | 2015-01-27 13:40:23 -0800 | [diff] [blame] | 1545 | |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame^] | 1546 | if (wpa_eapol_key_mic(sm->PTK.kck, sm->PTK.kck_len, |
| 1547 | sm->wpa_key_mgmt, version, |
| 1548 | (u8 *) hdr, len, key_mic) < 0) { |
| 1549 | os_free(hdr); |
| 1550 | return; |
| 1551 | } |
Dmitry Shmidt | 51b6ea8 | 2013-05-08 10:42:09 -0700 | [diff] [blame] | 1552 | #ifdef CONFIG_TESTING_OPTIONS |
| 1553 | if (!pairwise && |
Dmitry Shmidt | 7832adb | 2014-04-29 10:53:02 -0700 | [diff] [blame] | 1554 | wpa_auth->conf.corrupt_gtk_rekey_mic_probability > 0.0 && |
Dmitry Shmidt | 51b6ea8 | 2013-05-08 10:42:09 -0700 | [diff] [blame] | 1555 | drand48() < |
| 1556 | wpa_auth->conf.corrupt_gtk_rekey_mic_probability) { |
| 1557 | wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO, |
| 1558 | "Corrupting group EAPOL-Key Key MIC"); |
Dmitry Shmidt | 807291d | 2015-01-27 13:40:23 -0800 | [diff] [blame] | 1559 | key_mic[0]++; |
Dmitry Shmidt | 51b6ea8 | 2013-05-08 10:42:09 -0700 | [diff] [blame] | 1560 | } |
| 1561 | #endif /* CONFIG_TESTING_OPTIONS */ |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1562 | } |
| 1563 | |
| 1564 | wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_inc_EapolFramesTx, |
| 1565 | 1); |
| 1566 | wpa_auth_send_eapol(wpa_auth, sm->addr, (u8 *) hdr, len, |
| 1567 | sm->pairwise_set); |
| 1568 | os_free(hdr); |
| 1569 | } |
| 1570 | |
| 1571 | |
| 1572 | static void wpa_send_eapol(struct wpa_authenticator *wpa_auth, |
| 1573 | struct wpa_state_machine *sm, int key_info, |
| 1574 | const u8 *key_rsc, const u8 *nonce, |
| 1575 | const u8 *kde, size_t kde_len, |
| 1576 | int keyidx, int encr) |
| 1577 | { |
| 1578 | int timeout_ms; |
| 1579 | int pairwise = key_info & WPA_KEY_INFO_KEY_TYPE; |
Dmitry Shmidt | ebd93af | 2017-02-21 13:40:44 -0800 | [diff] [blame] | 1580 | u32 ctr; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1581 | |
| 1582 | if (sm == NULL) |
| 1583 | return; |
| 1584 | |
| 1585 | __wpa_send_eapol(wpa_auth, sm, key_info, key_rsc, nonce, kde, kde_len, |
| 1586 | keyidx, encr, 0); |
| 1587 | |
| 1588 | ctr = pairwise ? sm->TimeoutCtr : sm->GTimeoutCtr; |
| 1589 | if (ctr == 1 && wpa_auth->conf.tx_status) |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 1590 | timeout_ms = pairwise ? eapol_key_timeout_first : |
| 1591 | eapol_key_timeout_first_group; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1592 | else |
| 1593 | timeout_ms = eapol_key_timeout_subseq; |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 1594 | if (wpa_auth->conf.wpa_disable_eapol_key_retries && |
| 1595 | (!pairwise || (key_info & WPA_KEY_INFO_MIC))) |
| 1596 | timeout_ms = eapol_key_timeout_no_retrans; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1597 | if (pairwise && ctr == 1 && !(key_info & WPA_KEY_INFO_MIC)) |
| 1598 | sm->pending_1_of_4_timeout = 1; |
| 1599 | wpa_printf(MSG_DEBUG, "WPA: Use EAPOL-Key timeout of %u ms (retry " |
Dmitry Shmidt | ebd93af | 2017-02-21 13:40:44 -0800 | [diff] [blame] | 1600 | "counter %u)", timeout_ms, ctr); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1601 | eloop_register_timeout(timeout_ms / 1000, (timeout_ms % 1000) * 1000, |
| 1602 | wpa_send_eapol_timeout, wpa_auth, sm); |
| 1603 | } |
| 1604 | |
| 1605 | |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 1606 | static int wpa_verify_key_mic(int akmp, size_t pmk_len, struct wpa_ptk *PTK, |
| 1607 | u8 *data, size_t data_len) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1608 | { |
| 1609 | struct ieee802_1x_hdr *hdr; |
| 1610 | struct wpa_eapol_key *key; |
| 1611 | u16 key_info; |
| 1612 | int ret = 0; |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 1613 | u8 mic[WPA_EAPOL_KEY_MIC_MAX_LEN], *mic_pos; |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 1614 | size_t mic_len = wpa_mic_len(akmp, pmk_len); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1615 | |
| 1616 | if (data_len < sizeof(*hdr) + sizeof(*key)) |
| 1617 | return -1; |
| 1618 | |
| 1619 | hdr = (struct ieee802_1x_hdr *) data; |
| 1620 | key = (struct wpa_eapol_key *) (hdr + 1); |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 1621 | mic_pos = (u8 *) (key + 1); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1622 | key_info = WPA_GET_BE16(key->key_info); |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 1623 | os_memcpy(mic, mic_pos, mic_len); |
| 1624 | os_memset(mic_pos, 0, mic_len); |
Dmitry Shmidt | 807291d | 2015-01-27 13:40:23 -0800 | [diff] [blame] | 1625 | if (wpa_eapol_key_mic(PTK->kck, PTK->kck_len, akmp, |
| 1626 | key_info & WPA_KEY_INFO_TYPE_MASK, |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 1627 | data, data_len, mic_pos) || |
| 1628 | os_memcmp_const(mic, mic_pos, mic_len) != 0) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1629 | ret = -1; |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 1630 | os_memcpy(mic_pos, mic, mic_len); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1631 | return ret; |
| 1632 | } |
| 1633 | |
| 1634 | |
| 1635 | void wpa_remove_ptk(struct wpa_state_machine *sm) |
| 1636 | { |
| 1637 | sm->PTK_valid = FALSE; |
| 1638 | os_memset(&sm->PTK, 0, sizeof(sm->PTK)); |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 1639 | if (wpa_auth_set_key(sm->wpa_auth, 0, WPA_ALG_NONE, sm->addr, 0, NULL, |
| 1640 | 0)) |
| 1641 | wpa_printf(MSG_DEBUG, |
| 1642 | "RSN: PTK removal from the driver failed"); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1643 | sm->pairwise_set = FALSE; |
| 1644 | eloop_cancel_timeout(wpa_rekey_ptk, sm->wpa_auth, sm); |
| 1645 | } |
| 1646 | |
| 1647 | |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 1648 | int wpa_auth_sm_event(struct wpa_state_machine *sm, enum wpa_event event) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1649 | { |
| 1650 | int remove_ptk = 1; |
| 1651 | |
| 1652 | if (sm == NULL) |
| 1653 | return -1; |
| 1654 | |
| 1655 | wpa_auth_vlogger(sm->wpa_auth, sm->addr, LOGGER_DEBUG, |
| 1656 | "event %d notification", event); |
| 1657 | |
| 1658 | switch (event) { |
| 1659 | case WPA_AUTH: |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 1660 | #ifdef CONFIG_MESH |
| 1661 | /* PTKs are derived through AMPE */ |
| 1662 | if (wpa_auth_start_ampe(sm->wpa_auth, sm->addr)) { |
| 1663 | /* not mesh */ |
| 1664 | break; |
| 1665 | } |
| 1666 | return 0; |
| 1667 | #endif /* CONFIG_MESH */ |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1668 | case WPA_ASSOC: |
| 1669 | break; |
| 1670 | case WPA_DEAUTH: |
| 1671 | case WPA_DISASSOC: |
| 1672 | sm->DeauthenticationRequest = TRUE; |
| 1673 | break; |
| 1674 | case WPA_REAUTH: |
| 1675 | case WPA_REAUTH_EAPOL: |
| 1676 | if (!sm->started) { |
| 1677 | /* |
| 1678 | * When using WPS, we may end up here if the STA |
| 1679 | * manages to re-associate without the previous STA |
| 1680 | * entry getting removed. Consequently, we need to make |
| 1681 | * sure that the WPA state machines gets initialized |
| 1682 | * properly at this point. |
| 1683 | */ |
| 1684 | wpa_printf(MSG_DEBUG, "WPA state machine had not been " |
| 1685 | "started - initialize now"); |
| 1686 | sm->started = 1; |
| 1687 | sm->Init = TRUE; |
| 1688 | if (wpa_sm_step(sm) == 1) |
| 1689 | return 1; /* should not really happen */ |
| 1690 | sm->Init = FALSE; |
| 1691 | sm->AuthenticationRequest = TRUE; |
| 1692 | break; |
| 1693 | } |
| 1694 | if (sm->GUpdateStationKeys) { |
| 1695 | /* |
| 1696 | * Reauthentication cancels the pending group key |
| 1697 | * update for this STA. |
| 1698 | */ |
| 1699 | sm->group->GKeyDoneStations--; |
| 1700 | sm->GUpdateStationKeys = FALSE; |
| 1701 | sm->PtkGroupInit = TRUE; |
| 1702 | } |
| 1703 | sm->ReAuthenticationRequest = TRUE; |
| 1704 | break; |
| 1705 | case WPA_ASSOC_FT: |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 1706 | #ifdef CONFIG_IEEE80211R_AP |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1707 | wpa_printf(MSG_DEBUG, "FT: Retry PTK configuration " |
| 1708 | "after association"); |
| 1709 | wpa_ft_install_ptk(sm); |
| 1710 | |
| 1711 | /* Using FT protocol, not WPA auth state machine */ |
| 1712 | sm->ft_completed = 1; |
| 1713 | return 0; |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 1714 | #else /* CONFIG_IEEE80211R_AP */ |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1715 | break; |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 1716 | #endif /* CONFIG_IEEE80211R_AP */ |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 1717 | case WPA_ASSOC_FILS: |
| 1718 | #ifdef CONFIG_FILS |
| 1719 | wpa_printf(MSG_DEBUG, |
| 1720 | "FILS: TK configuration after association"); |
| 1721 | fils_set_tk(sm); |
| 1722 | sm->fils_completed = 1; |
| 1723 | return 0; |
| 1724 | #else /* CONFIG_FILS */ |
| 1725 | break; |
| 1726 | #endif /* CONFIG_FILS */ |
Mathy Vanhoef | f6e1f66 | 2017-07-14 15:15:35 +0200 | [diff] [blame] | 1727 | case WPA_DRV_STA_REMOVED: |
| 1728 | sm->tk_already_set = FALSE; |
| 1729 | return 0; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1730 | } |
| 1731 | |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 1732 | #ifdef CONFIG_IEEE80211R_AP |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1733 | sm->ft_completed = 0; |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 1734 | #endif /* CONFIG_IEEE80211R_AP */ |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1735 | |
| 1736 | #ifdef CONFIG_IEEE80211W |
| 1737 | if (sm->mgmt_frame_prot && event == WPA_AUTH) |
| 1738 | remove_ptk = 0; |
| 1739 | #endif /* CONFIG_IEEE80211W */ |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 1740 | #ifdef CONFIG_FILS |
| 1741 | if (wpa_key_mgmt_fils(sm->wpa_key_mgmt) && |
| 1742 | (event == WPA_AUTH || event == WPA_ASSOC)) |
| 1743 | remove_ptk = 0; |
| 1744 | #endif /* CONFIG_FILS */ |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1745 | |
| 1746 | if (remove_ptk) { |
| 1747 | sm->PTK_valid = FALSE; |
| 1748 | os_memset(&sm->PTK, 0, sizeof(sm->PTK)); |
| 1749 | |
| 1750 | if (event != WPA_REAUTH_EAPOL) |
| 1751 | wpa_remove_ptk(sm); |
| 1752 | } |
| 1753 | |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 1754 | if (sm->in_step_loop) { |
| 1755 | /* |
| 1756 | * wpa_sm_step() is already running - avoid recursive call to |
| 1757 | * it by making the existing loop process the new update. |
| 1758 | */ |
| 1759 | sm->changed = TRUE; |
| 1760 | return 0; |
| 1761 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1762 | return wpa_sm_step(sm); |
| 1763 | } |
| 1764 | |
| 1765 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1766 | SM_STATE(WPA_PTK, INITIALIZE) |
| 1767 | { |
| 1768 | SM_ENTRY_MA(WPA_PTK, INITIALIZE, wpa_ptk); |
| 1769 | if (sm->Init) { |
| 1770 | /* Init flag is not cleared here, so avoid busy |
| 1771 | * loop by claiming nothing changed. */ |
| 1772 | sm->changed = FALSE; |
| 1773 | } |
| 1774 | |
| 1775 | sm->keycount = 0; |
| 1776 | if (sm->GUpdateStationKeys) |
| 1777 | sm->group->GKeyDoneStations--; |
| 1778 | sm->GUpdateStationKeys = FALSE; |
| 1779 | if (sm->wpa == WPA_VERSION_WPA) |
| 1780 | sm->PInitAKeys = FALSE; |
| 1781 | if (1 /* Unicast cipher supported AND (ESS OR ((IBSS or WDS) and |
| 1782 | * Local AA > Remote AA)) */) { |
| 1783 | sm->Pair = TRUE; |
| 1784 | } |
| 1785 | wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portEnabled, 0); |
| 1786 | wpa_remove_ptk(sm); |
| 1787 | wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portValid, 0); |
| 1788 | sm->TimeoutCtr = 0; |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 1789 | if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt) || |
| 1790 | sm->wpa_key_mgmt == WPA_KEY_MGMT_DPP || |
| 1791 | sm->wpa_key_mgmt == WPA_KEY_MGMT_OWE) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1792 | wpa_auth_set_eapol(sm->wpa_auth, sm->addr, |
| 1793 | WPA_EAPOL_authorized, 0); |
| 1794 | } |
| 1795 | } |
| 1796 | |
| 1797 | |
| 1798 | SM_STATE(WPA_PTK, DISCONNECT) |
| 1799 | { |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 1800 | u16 reason = sm->disconnect_reason; |
| 1801 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1802 | SM_ENTRY_MA(WPA_PTK, DISCONNECT, wpa_ptk); |
| 1803 | sm->Disconnect = FALSE; |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 1804 | sm->disconnect_reason = 0; |
| 1805 | if (!reason) |
| 1806 | reason = WLAN_REASON_PREV_AUTH_NOT_VALID; |
| 1807 | wpa_sta_disconnect(sm->wpa_auth, sm->addr, reason); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1808 | } |
| 1809 | |
| 1810 | |
| 1811 | SM_STATE(WPA_PTK, DISCONNECTED) |
| 1812 | { |
| 1813 | SM_ENTRY_MA(WPA_PTK, DISCONNECTED, wpa_ptk); |
| 1814 | sm->DeauthenticationRequest = FALSE; |
| 1815 | } |
| 1816 | |
| 1817 | |
| 1818 | SM_STATE(WPA_PTK, AUTHENTICATION) |
| 1819 | { |
| 1820 | SM_ENTRY_MA(WPA_PTK, AUTHENTICATION, wpa_ptk); |
| 1821 | os_memset(&sm->PTK, 0, sizeof(sm->PTK)); |
| 1822 | sm->PTK_valid = FALSE; |
| 1823 | wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portControl_Auto, |
| 1824 | 1); |
| 1825 | wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portEnabled, 1); |
| 1826 | sm->AuthenticationRequest = FALSE; |
| 1827 | } |
| 1828 | |
| 1829 | |
Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 1830 | static void wpa_group_ensure_init(struct wpa_authenticator *wpa_auth, |
| 1831 | struct wpa_group *group) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1832 | { |
Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 1833 | if (group->first_sta_seen) |
| 1834 | return; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1835 | /* |
| 1836 | * System has run bit further than at the time hostapd was started |
| 1837 | * potentially very early during boot up. This provides better chances |
| 1838 | * of collecting more randomness on embedded systems. Re-initialize the |
| 1839 | * GMK and Counter here to improve their strength if there was not |
| 1840 | * enough entropy available immediately after system startup. |
| 1841 | */ |
| 1842 | wpa_printf(MSG_DEBUG, "WPA: Re-initialize GMK/Counter on first " |
| 1843 | "station"); |
| 1844 | if (random_pool_ready() != 1) { |
| 1845 | wpa_printf(MSG_INFO, "WPA: Not enough entropy in random pool " |
| 1846 | "to proceed - reject first 4-way handshake"); |
| 1847 | group->reject_4way_hs_for_entropy = TRUE; |
Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 1848 | } else { |
| 1849 | group->first_sta_seen = TRUE; |
| 1850 | group->reject_4way_hs_for_entropy = FALSE; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1851 | } |
Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 1852 | |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 1853 | if (wpa_group_init_gmk_and_counter(wpa_auth, group) < 0 || |
| 1854 | wpa_gtk_update(wpa_auth, group) < 0 || |
| 1855 | wpa_group_config_group_keys(wpa_auth, group) < 0) { |
| 1856 | wpa_printf(MSG_INFO, "WPA: GMK/GTK setup failed"); |
| 1857 | group->first_sta_seen = FALSE; |
| 1858 | group->reject_4way_hs_for_entropy = TRUE; |
| 1859 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1860 | } |
| 1861 | |
| 1862 | |
| 1863 | SM_STATE(WPA_PTK, AUTHENTICATION2) |
| 1864 | { |
| 1865 | SM_ENTRY_MA(WPA_PTK, AUTHENTICATION2, wpa_ptk); |
| 1866 | |
Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 1867 | wpa_group_ensure_init(sm->wpa_auth, sm->group); |
Dmitry Shmidt | 444d567 | 2013-04-01 13:08:44 -0700 | [diff] [blame] | 1868 | sm->ReAuthenticationRequest = FALSE; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1869 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 1870 | /* |
| 1871 | * Definition of ANonce selection in IEEE Std 802.11i-2004 is somewhat |
| 1872 | * ambiguous. The Authenticator state machine uses a counter that is |
| 1873 | * incremented by one for each 4-way handshake. However, the security |
| 1874 | * analysis of 4-way handshake points out that unpredictable nonces |
| 1875 | * help in preventing precomputation attacks. Instead of the state |
| 1876 | * machine definition, use an unpredictable nonce value here to provide |
| 1877 | * stronger protection against potential precomputation attacks. |
| 1878 | */ |
| 1879 | if (random_get_bytes(sm->ANonce, WPA_NONCE_LEN)) { |
| 1880 | wpa_printf(MSG_ERROR, "WPA: Failed to get random data for " |
| 1881 | "ANonce."); |
Dmitry Shmidt | 444d567 | 2013-04-01 13:08:44 -0700 | [diff] [blame] | 1882 | sm->Disconnect = TRUE; |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 1883 | return; |
| 1884 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1885 | wpa_hexdump(MSG_DEBUG, "WPA: Assign ANonce", sm->ANonce, |
| 1886 | WPA_NONCE_LEN); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1887 | /* IEEE 802.11i does not clear TimeoutCtr here, but this is more |
| 1888 | * logical place than INITIALIZE since AUTHENTICATION2 can be |
| 1889 | * re-entered on ReAuthenticationRequest without going through |
| 1890 | * INITIALIZE. */ |
| 1891 | sm->TimeoutCtr = 0; |
| 1892 | } |
| 1893 | |
| 1894 | |
Jouni Malinen | 1420a89 | 2017-10-01 12:32:57 +0300 | [diff] [blame] | 1895 | static int wpa_auth_sm_ptk_update(struct wpa_state_machine *sm) |
| 1896 | { |
| 1897 | if (random_get_bytes(sm->ANonce, WPA_NONCE_LEN)) { |
| 1898 | wpa_printf(MSG_ERROR, |
| 1899 | "WPA: Failed to get random data for ANonce"); |
| 1900 | sm->Disconnect = TRUE; |
| 1901 | return -1; |
| 1902 | } |
| 1903 | wpa_hexdump(MSG_DEBUG, "WPA: Assign new ANonce", sm->ANonce, |
| 1904 | WPA_NONCE_LEN); |
| 1905 | sm->TimeoutCtr = 0; |
| 1906 | return 0; |
| 1907 | } |
| 1908 | |
| 1909 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1910 | SM_STATE(WPA_PTK, INITPMK) |
| 1911 | { |
| 1912 | u8 msk[2 * PMK_LEN]; |
| 1913 | size_t len = 2 * PMK_LEN; |
| 1914 | |
| 1915 | SM_ENTRY_MA(WPA_PTK, INITPMK, wpa_ptk); |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 1916 | #ifdef CONFIG_IEEE80211R_AP |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1917 | sm->xxkey_len = 0; |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 1918 | #endif /* CONFIG_IEEE80211R_AP */ |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1919 | if (sm->pmksa) { |
| 1920 | wpa_printf(MSG_DEBUG, "WPA: PMK from PMKSA cache"); |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 1921 | os_memcpy(sm->PMK, sm->pmksa->pmk, sm->pmksa->pmk_len); |
| 1922 | sm->pmk_len = sm->pmksa->pmk_len; |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 1923 | #ifdef CONFIG_DPP |
| 1924 | } else if (sm->wpa_key_mgmt == WPA_KEY_MGMT_DPP) { |
| 1925 | wpa_printf(MSG_DEBUG, |
| 1926 | "DPP: No PMKSA cache entry for STA - reject connection"); |
| 1927 | sm->Disconnect = TRUE; |
| 1928 | sm->disconnect_reason = WLAN_REASON_INVALID_PMKID; |
| 1929 | return; |
| 1930 | #endif /* CONFIG_DPP */ |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1931 | } else if (wpa_auth_get_msk(sm->wpa_auth, sm->addr, msk, &len) == 0) { |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 1932 | unsigned int pmk_len; |
| 1933 | |
Dmitry Shmidt | ebd93af | 2017-02-21 13:40:44 -0800 | [diff] [blame] | 1934 | if (wpa_key_mgmt_sha384(sm->wpa_key_mgmt)) |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 1935 | pmk_len = PMK_LEN_SUITE_B_192; |
| 1936 | else |
| 1937 | pmk_len = PMK_LEN; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1938 | wpa_printf(MSG_DEBUG, "WPA: PMK from EAPOL state machine " |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 1939 | "(MSK len=%lu PMK len=%u)", (unsigned long) len, |
| 1940 | pmk_len); |
| 1941 | if (len < pmk_len) { |
| 1942 | wpa_printf(MSG_DEBUG, |
| 1943 | "WPA: MSK not long enough (%u) to create PMK (%u)", |
| 1944 | (unsigned int) len, (unsigned int) pmk_len); |
| 1945 | sm->Disconnect = TRUE; |
| 1946 | return; |
| 1947 | } |
| 1948 | os_memcpy(sm->PMK, msk, pmk_len); |
| 1949 | sm->pmk_len = pmk_len; |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 1950 | #ifdef CONFIG_IEEE80211R_AP |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1951 | if (len >= 2 * PMK_LEN) { |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame^] | 1952 | if (wpa_key_mgmt_sha384(sm->wpa_key_mgmt)) { |
| 1953 | os_memcpy(sm->xxkey, msk, SHA384_MAC_LEN); |
| 1954 | sm->xxkey_len = SHA384_MAC_LEN; |
| 1955 | } else { |
| 1956 | os_memcpy(sm->xxkey, msk + PMK_LEN, PMK_LEN); |
| 1957 | sm->xxkey_len = PMK_LEN; |
| 1958 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1959 | } |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 1960 | #endif /* CONFIG_IEEE80211R_AP */ |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1961 | } else { |
Dmitry Shmidt | 807291d | 2015-01-27 13:40:23 -0800 | [diff] [blame] | 1962 | wpa_printf(MSG_DEBUG, "WPA: Could not get PMK, get_msk: %p", |
Paul Stewart | 092955c | 2017-02-06 09:13:09 -0800 | [diff] [blame] | 1963 | sm->wpa_auth->cb->get_msk); |
Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 1964 | sm->Disconnect = TRUE; |
| 1965 | return; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1966 | } |
Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 1967 | os_memset(msk, 0, sizeof(msk)); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1968 | |
| 1969 | sm->req_replay_counter_used = 0; |
| 1970 | /* IEEE 802.11i does not set keyRun to FALSE, but not doing this |
| 1971 | * will break reauthentication since EAPOL state machines may not be |
| 1972 | * get into AUTHENTICATING state that clears keyRun before WPA state |
| 1973 | * machine enters AUTHENTICATION2 state and goes immediately to INITPMK |
| 1974 | * state and takes PMK from the previously used AAA Key. This will |
| 1975 | * eventually fail in 4-Way Handshake because Supplicant uses PMK |
| 1976 | * derived from the new AAA Key. Setting keyRun = FALSE here seems to |
| 1977 | * be good workaround for this issue. */ |
| 1978 | wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_keyRun, 0); |
| 1979 | } |
| 1980 | |
| 1981 | |
| 1982 | SM_STATE(WPA_PTK, INITPSK) |
| 1983 | { |
| 1984 | const u8 *psk; |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 1985 | size_t psk_len; |
| 1986 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1987 | SM_ENTRY_MA(WPA_PTK, INITPSK, wpa_ptk); |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 1988 | psk = wpa_auth_get_psk(sm->wpa_auth, sm->addr, sm->p2p_dev_addr, NULL, |
| 1989 | &psk_len); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1990 | if (psk) { |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 1991 | os_memcpy(sm->PMK, psk, psk_len); |
| 1992 | sm->pmk_len = psk_len; |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 1993 | #ifdef CONFIG_IEEE80211R_AP |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1994 | os_memcpy(sm->xxkey, psk, PMK_LEN); |
| 1995 | sm->xxkey_len = PMK_LEN; |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 1996 | #endif /* CONFIG_IEEE80211R_AP */ |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1997 | } |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 1998 | #ifdef CONFIG_SAE |
| 1999 | if (wpa_auth_uses_sae(sm) && sm->pmksa) { |
| 2000 | wpa_printf(MSG_DEBUG, "SAE: PMK from PMKSA cache"); |
| 2001 | os_memcpy(sm->PMK, sm->pmksa->pmk, sm->pmksa->pmk_len); |
| 2002 | sm->pmk_len = sm->pmksa->pmk_len; |
| 2003 | } |
| 2004 | #endif /* CONFIG_SAE */ |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2005 | sm->req_replay_counter_used = 0; |
| 2006 | } |
| 2007 | |
| 2008 | |
| 2009 | SM_STATE(WPA_PTK, PTKSTART) |
| 2010 | { |
| 2011 | u8 buf[2 + RSN_SELECTOR_LEN + PMKID_LEN], *pmkid = NULL; |
| 2012 | size_t pmkid_len = 0; |
| 2013 | |
| 2014 | SM_ENTRY_MA(WPA_PTK, PTKSTART, wpa_ptk); |
| 2015 | sm->PTKRequest = FALSE; |
| 2016 | sm->TimeoutEvt = FALSE; |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 2017 | sm->alt_snonce_valid = FALSE; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2018 | |
| 2019 | sm->TimeoutCtr++; |
Dmitry Shmidt | ebd93af | 2017-02-21 13:40:44 -0800 | [diff] [blame] | 2020 | if (sm->TimeoutCtr > sm->wpa_auth->conf.wpa_pairwise_update_count) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2021 | /* No point in sending the EAPOL-Key - we will disconnect |
| 2022 | * immediately following this. */ |
| 2023 | return; |
| 2024 | } |
| 2025 | |
| 2026 | wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG, |
| 2027 | "sending 1/4 msg of 4-Way Handshake"); |
| 2028 | /* |
| 2029 | * TODO: Could add PMKID even with WPA2-PSK, but only if there is only |
| 2030 | * one possible PSK for this STA. |
| 2031 | */ |
| 2032 | if (sm->wpa == WPA_VERSION_WPA2 && |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 2033 | (wpa_key_mgmt_wpa_ieee8021x(sm->wpa_key_mgmt) || |
| 2034 | (sm->wpa_key_mgmt == WPA_KEY_MGMT_OWE && sm->pmksa) || |
| 2035 | wpa_key_mgmt_sae(sm->wpa_key_mgmt)) && |
Dmitry Shmidt | f21452a | 2014-02-26 10:55:25 -0800 | [diff] [blame] | 2036 | sm->wpa_key_mgmt != WPA_KEY_MGMT_OSEN) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2037 | pmkid = buf; |
| 2038 | pmkid_len = 2 + RSN_SELECTOR_LEN + PMKID_LEN; |
| 2039 | pmkid[0] = WLAN_EID_VENDOR_SPECIFIC; |
| 2040 | pmkid[1] = RSN_SELECTOR_LEN + PMKID_LEN; |
| 2041 | RSN_SELECTOR_PUT(&pmkid[2], RSN_KEY_DATA_PMKID); |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 2042 | if (sm->pmksa) { |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame^] | 2043 | wpa_hexdump(MSG_DEBUG, |
| 2044 | "RSN: Message 1/4 PMKID from PMKSA entry", |
| 2045 | sm->pmksa->pmkid, PMKID_LEN); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2046 | os_memcpy(&pmkid[2 + RSN_SELECTOR_LEN], |
| 2047 | sm->pmksa->pmkid, PMKID_LEN); |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 2048 | } else if (wpa_key_mgmt_suite_b(sm->wpa_key_mgmt)) { |
| 2049 | /* No KCK available to derive PMKID */ |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame^] | 2050 | wpa_printf(MSG_DEBUG, |
| 2051 | "RSN: No KCK available to derive PMKID for message 1/4"); |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 2052 | pmkid = NULL; |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame^] | 2053 | #ifdef CONFIG_SAE |
| 2054 | } else if (wpa_key_mgmt_sae(sm->wpa_key_mgmt)) { |
| 2055 | if (sm->pmkid_set) { |
| 2056 | wpa_hexdump(MSG_DEBUG, |
| 2057 | "RSN: Message 1/4 PMKID from SAE", |
| 2058 | sm->pmkid, PMKID_LEN); |
| 2059 | os_memcpy(&pmkid[2 + RSN_SELECTOR_LEN], |
| 2060 | sm->pmkid, PMKID_LEN); |
| 2061 | } else { |
| 2062 | /* No PMKID available */ |
| 2063 | wpa_printf(MSG_DEBUG, |
| 2064 | "RSN: No SAE PMKID available for message 1/4"); |
| 2065 | pmkid = NULL; |
| 2066 | } |
| 2067 | #endif /* CONFIG_SAE */ |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 2068 | } else { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2069 | /* |
| 2070 | * Calculate PMKID since no PMKSA cache entry was |
| 2071 | * available with pre-calculated PMKID. |
| 2072 | */ |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 2073 | rsn_pmkid(sm->PMK, sm->pmk_len, sm->wpa_auth->addr, |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2074 | sm->addr, &pmkid[2 + RSN_SELECTOR_LEN], |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 2075 | sm->wpa_key_mgmt); |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame^] | 2076 | wpa_hexdump(MSG_DEBUG, |
| 2077 | "RSN: Message 1/4 PMKID derived from PMK", |
| 2078 | &pmkid[2 + RSN_SELECTOR_LEN], PMKID_LEN); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2079 | } |
| 2080 | } |
| 2081 | wpa_send_eapol(sm->wpa_auth, sm, |
| 2082 | WPA_KEY_INFO_ACK | WPA_KEY_INFO_KEY_TYPE, NULL, |
| 2083 | sm->ANonce, pmkid, pmkid_len, 0, 0); |
| 2084 | } |
| 2085 | |
| 2086 | |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 2087 | static int wpa_derive_ptk(struct wpa_state_machine *sm, const u8 *snonce, |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 2088 | const u8 *pmk, unsigned int pmk_len, |
| 2089 | struct wpa_ptk *ptk) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2090 | { |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 2091 | #ifdef CONFIG_IEEE80211R_AP |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2092 | if (wpa_key_mgmt_ft(sm->wpa_key_mgmt)) |
Dmitry Shmidt | 807291d | 2015-01-27 13:40:23 -0800 | [diff] [blame] | 2093 | return wpa_auth_derive_ptk_ft(sm, pmk, ptk); |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 2094 | #endif /* CONFIG_IEEE80211R_AP */ |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2095 | |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 2096 | return wpa_pmk_to_ptk(pmk, pmk_len, "Pairwise key expansion", |
Dmitry Shmidt | 807291d | 2015-01-27 13:40:23 -0800 | [diff] [blame] | 2097 | sm->wpa_auth->addr, sm->addr, sm->ANonce, snonce, |
| 2098 | ptk, sm->wpa_key_mgmt, sm->pairwise); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2099 | } |
| 2100 | |
| 2101 | |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 2102 | #ifdef CONFIG_FILS |
| 2103 | |
| 2104 | int fils_auth_pmk_to_ptk(struct wpa_state_machine *sm, const u8 *pmk, |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 2105 | size_t pmk_len, const u8 *snonce, const u8 *anonce, |
| 2106 | const u8 *dhss, size_t dhss_len, |
| 2107 | struct wpabuf *g_sta, struct wpabuf *g_ap) |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 2108 | { |
| 2109 | u8 ick[FILS_ICK_MAX_LEN]; |
| 2110 | size_t ick_len; |
| 2111 | int res; |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 2112 | u8 fils_ft[FILS_FT_MAX_LEN]; |
| 2113 | size_t fils_ft_len = 0; |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 2114 | |
| 2115 | res = fils_pmk_to_ptk(pmk, pmk_len, sm->addr, sm->wpa_auth->addr, |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 2116 | snonce, anonce, dhss, dhss_len, |
| 2117 | &sm->PTK, ick, &ick_len, |
| 2118 | sm->wpa_key_mgmt, sm->pairwise, |
| 2119 | fils_ft, &fils_ft_len); |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 2120 | if (res < 0) |
| 2121 | return res; |
| 2122 | sm->PTK_valid = TRUE; |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 2123 | sm->tk_already_set = FALSE; |
| 2124 | |
| 2125 | #ifdef CONFIG_IEEE80211R_AP |
| 2126 | if (fils_ft_len) { |
| 2127 | struct wpa_authenticator *wpa_auth = sm->wpa_auth; |
| 2128 | struct wpa_auth_config *conf = &wpa_auth->conf; |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame^] | 2129 | u8 pmk_r0[PMK_LEN_MAX], pmk_r0_name[WPA_PMK_NAME_LEN]; |
| 2130 | int use_sha384 = wpa_key_mgmt_sha384(sm->wpa_key_mgmt); |
| 2131 | size_t pmk_r0_len = use_sha384 ? SHA384_MAC_LEN : PMK_LEN; |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 2132 | |
| 2133 | if (wpa_derive_pmk_r0(fils_ft, fils_ft_len, |
| 2134 | conf->ssid, conf->ssid_len, |
| 2135 | conf->mobility_domain, |
| 2136 | conf->r0_key_holder, |
| 2137 | conf->r0_key_holder_len, |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame^] | 2138 | sm->addr, pmk_r0, pmk_r0_name, |
| 2139 | use_sha384) < 0) |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 2140 | return -1; |
| 2141 | |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame^] | 2142 | wpa_hexdump_key(MSG_DEBUG, "FILS+FT: PMK-R0", |
| 2143 | pmk_r0, pmk_r0_len); |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 2144 | wpa_hexdump(MSG_DEBUG, "FILS+FT: PMKR0Name", |
| 2145 | pmk_r0_name, WPA_PMK_NAME_LEN); |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame^] | 2146 | wpa_ft_store_pmk_fils(sm, pmk_r0, pmk_r0_name); |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 2147 | os_memset(fils_ft, 0, sizeof(fils_ft)); |
| 2148 | } |
| 2149 | #endif /* CONFIG_IEEE80211R_AP */ |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 2150 | |
| 2151 | res = fils_key_auth_sk(ick, ick_len, snonce, anonce, |
| 2152 | sm->addr, sm->wpa_auth->addr, |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 2153 | g_sta ? wpabuf_head(g_sta) : NULL, |
| 2154 | g_sta ? wpabuf_len(g_sta) : 0, |
| 2155 | g_ap ? wpabuf_head(g_ap) : NULL, |
| 2156 | g_ap ? wpabuf_len(g_ap) : 0, |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 2157 | sm->wpa_key_mgmt, sm->fils_key_auth_sta, |
| 2158 | sm->fils_key_auth_ap, |
| 2159 | &sm->fils_key_auth_len); |
| 2160 | os_memset(ick, 0, sizeof(ick)); |
| 2161 | |
| 2162 | /* Store nonces for (Re)Association Request/Response frame processing */ |
| 2163 | os_memcpy(sm->SNonce, snonce, FILS_NONCE_LEN); |
| 2164 | os_memcpy(sm->ANonce, anonce, FILS_NONCE_LEN); |
| 2165 | |
| 2166 | return res; |
| 2167 | } |
| 2168 | |
| 2169 | |
| 2170 | static int wpa_aead_decrypt(struct wpa_state_machine *sm, struct wpa_ptk *ptk, |
| 2171 | u8 *buf, size_t buf_len, u16 *_key_data_len) |
| 2172 | { |
| 2173 | struct ieee802_1x_hdr *hdr; |
| 2174 | struct wpa_eapol_key *key; |
| 2175 | u8 *pos; |
| 2176 | u16 key_data_len; |
| 2177 | u8 *tmp; |
| 2178 | const u8 *aad[1]; |
| 2179 | size_t aad_len[1]; |
| 2180 | |
| 2181 | hdr = (struct ieee802_1x_hdr *) buf; |
| 2182 | key = (struct wpa_eapol_key *) (hdr + 1); |
| 2183 | pos = (u8 *) (key + 1); |
| 2184 | key_data_len = WPA_GET_BE16(pos); |
| 2185 | if (key_data_len < AES_BLOCK_SIZE || |
| 2186 | key_data_len > buf_len - sizeof(*hdr) - sizeof(*key) - 2) { |
| 2187 | wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_INFO, |
| 2188 | "No room for AES-SIV data in the frame"); |
| 2189 | return -1; |
| 2190 | } |
| 2191 | pos += 2; /* Pointing at the Encrypted Key Data field */ |
| 2192 | |
| 2193 | tmp = os_malloc(key_data_len); |
| 2194 | if (!tmp) |
| 2195 | return -1; |
| 2196 | |
| 2197 | /* AES-SIV AAD from EAPOL protocol version field (inclusive) to |
| 2198 | * to Key Data (exclusive). */ |
| 2199 | aad[0] = buf; |
| 2200 | aad_len[0] = pos - buf; |
| 2201 | if (aes_siv_decrypt(ptk->kek, ptk->kek_len, pos, key_data_len, |
| 2202 | 1, aad, aad_len, tmp) < 0) { |
| 2203 | wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_INFO, |
| 2204 | "Invalid AES-SIV data in the frame"); |
| 2205 | bin_clear_free(tmp, key_data_len); |
| 2206 | return -1; |
| 2207 | } |
| 2208 | |
| 2209 | /* AEAD decryption and validation completed successfully */ |
| 2210 | key_data_len -= AES_BLOCK_SIZE; |
| 2211 | wpa_hexdump_key(MSG_DEBUG, "WPA: Decrypted Key Data", |
| 2212 | tmp, key_data_len); |
| 2213 | |
| 2214 | /* Replace Key Data field with the decrypted version */ |
| 2215 | os_memcpy(pos, tmp, key_data_len); |
| 2216 | pos -= 2; /* Key Data Length field */ |
| 2217 | WPA_PUT_BE16(pos, key_data_len); |
| 2218 | bin_clear_free(tmp, key_data_len); |
| 2219 | if (_key_data_len) |
| 2220 | *_key_data_len = key_data_len; |
| 2221 | return 0; |
| 2222 | } |
| 2223 | |
| 2224 | |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 2225 | const u8 * wpa_fils_validate_fils_session(struct wpa_state_machine *sm, |
| 2226 | const u8 *ies, size_t ies_len, |
| 2227 | const u8 *fils_session) |
| 2228 | { |
| 2229 | const u8 *ie, *end; |
| 2230 | const u8 *session = NULL; |
| 2231 | |
| 2232 | if (!wpa_key_mgmt_fils(sm->wpa_key_mgmt)) { |
| 2233 | wpa_printf(MSG_DEBUG, |
| 2234 | "FILS: Not a FILS AKM - reject association"); |
| 2235 | return NULL; |
| 2236 | } |
| 2237 | |
| 2238 | /* Verify Session element */ |
| 2239 | ie = ies; |
| 2240 | end = ((const u8 *) ie) + ies_len; |
| 2241 | while (ie + 1 < end) { |
| 2242 | if (ie + 2 + ie[1] > end) |
| 2243 | break; |
| 2244 | if (ie[0] == WLAN_EID_EXTENSION && |
| 2245 | ie[1] >= 1 + FILS_SESSION_LEN && |
| 2246 | ie[2] == WLAN_EID_EXT_FILS_SESSION) { |
| 2247 | session = ie; |
| 2248 | break; |
| 2249 | } |
| 2250 | ie += 2 + ie[1]; |
| 2251 | } |
| 2252 | |
| 2253 | if (!session) { |
| 2254 | wpa_printf(MSG_DEBUG, |
| 2255 | "FILS: %s: Could not find FILS Session element in Assoc Req - reject", |
| 2256 | __func__); |
| 2257 | return NULL; |
| 2258 | } |
| 2259 | |
| 2260 | if (!fils_session) { |
| 2261 | wpa_printf(MSG_DEBUG, |
| 2262 | "FILS: %s: Could not find FILS Session element in STA entry - reject", |
| 2263 | __func__); |
| 2264 | return NULL; |
| 2265 | } |
| 2266 | |
| 2267 | if (os_memcmp(fils_session, session + 3, FILS_SESSION_LEN) != 0) { |
| 2268 | wpa_printf(MSG_DEBUG, "FILS: Session mismatch"); |
| 2269 | wpa_hexdump(MSG_DEBUG, "FILS: Expected FILS Session", |
| 2270 | fils_session, FILS_SESSION_LEN); |
| 2271 | wpa_hexdump(MSG_DEBUG, "FILS: Received FILS Session", |
| 2272 | session + 3, FILS_SESSION_LEN); |
| 2273 | return NULL; |
| 2274 | } |
| 2275 | return session; |
| 2276 | } |
| 2277 | |
| 2278 | |
| 2279 | int wpa_fils_validate_key_confirm(struct wpa_state_machine *sm, const u8 *ies, |
| 2280 | size_t ies_len) |
| 2281 | { |
| 2282 | struct ieee802_11_elems elems; |
| 2283 | |
| 2284 | if (ieee802_11_parse_elems(ies, ies_len, &elems, 1) == ParseFailed) { |
| 2285 | wpa_printf(MSG_DEBUG, |
| 2286 | "FILS: Failed to parse decrypted elements"); |
| 2287 | return -1; |
| 2288 | } |
| 2289 | |
| 2290 | if (!elems.fils_session) { |
| 2291 | wpa_printf(MSG_DEBUG, "FILS: No FILS Session element"); |
| 2292 | return -1; |
| 2293 | } |
| 2294 | |
| 2295 | if (!elems.fils_key_confirm) { |
| 2296 | wpa_printf(MSG_DEBUG, "FILS: No FILS Key Confirm element"); |
| 2297 | return -1; |
| 2298 | } |
| 2299 | |
| 2300 | if (elems.fils_key_confirm_len != sm->fils_key_auth_len) { |
| 2301 | wpa_printf(MSG_DEBUG, |
| 2302 | "FILS: Unexpected Key-Auth length %d (expected %d)", |
| 2303 | elems.fils_key_confirm_len, |
| 2304 | (int) sm->fils_key_auth_len); |
| 2305 | return -1; |
| 2306 | } |
| 2307 | |
| 2308 | if (os_memcmp(elems.fils_key_confirm, sm->fils_key_auth_sta, |
| 2309 | sm->fils_key_auth_len) != 0) { |
| 2310 | wpa_printf(MSG_DEBUG, "FILS: Key-Auth mismatch"); |
| 2311 | wpa_hexdump(MSG_DEBUG, "FILS: Received Key-Auth", |
| 2312 | elems.fils_key_confirm, elems.fils_key_confirm_len); |
| 2313 | wpa_hexdump(MSG_DEBUG, "FILS: Expected Key-Auth", |
| 2314 | sm->fils_key_auth_sta, sm->fils_key_auth_len); |
| 2315 | return -1; |
| 2316 | } |
| 2317 | |
| 2318 | return 0; |
| 2319 | } |
| 2320 | |
| 2321 | |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 2322 | int fils_decrypt_assoc(struct wpa_state_machine *sm, const u8 *fils_session, |
| 2323 | const struct ieee80211_mgmt *mgmt, size_t frame_len, |
| 2324 | u8 *pos, size_t left) |
| 2325 | { |
| 2326 | u16 fc, stype; |
| 2327 | const u8 *end, *ie_start, *ie, *session, *crypt; |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 2328 | const u8 *aad[5]; |
| 2329 | size_t aad_len[5]; |
| 2330 | |
| 2331 | if (!sm || !sm->PTK_valid) { |
| 2332 | wpa_printf(MSG_DEBUG, |
| 2333 | "FILS: No KEK to decrypt Assocication Request frame"); |
| 2334 | return -1; |
| 2335 | } |
| 2336 | |
| 2337 | if (!wpa_key_mgmt_fils(sm->wpa_key_mgmt)) { |
| 2338 | wpa_printf(MSG_DEBUG, |
| 2339 | "FILS: Not a FILS AKM - reject association"); |
| 2340 | return -1; |
| 2341 | } |
| 2342 | |
| 2343 | end = ((const u8 *) mgmt) + frame_len; |
| 2344 | fc = le_to_host16(mgmt->frame_control); |
| 2345 | stype = WLAN_FC_GET_STYPE(fc); |
| 2346 | if (stype == WLAN_FC_STYPE_REASSOC_REQ) |
| 2347 | ie_start = mgmt->u.reassoc_req.variable; |
| 2348 | else |
| 2349 | ie_start = mgmt->u.assoc_req.variable; |
| 2350 | ie = ie_start; |
| 2351 | |
| 2352 | /* |
| 2353 | * Find FILS Session element which is the last unencrypted element in |
| 2354 | * the frame. |
| 2355 | */ |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 2356 | session = wpa_fils_validate_fils_session(sm, ie, end - ie, |
| 2357 | fils_session); |
| 2358 | if (!session) { |
| 2359 | wpa_printf(MSG_DEBUG, "FILS: Session validation failed"); |
| 2360 | return -1; |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 2361 | } |
| 2362 | |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 2363 | crypt = session + 2 + session[1]; |
| 2364 | |
| 2365 | if (end - crypt < AES_BLOCK_SIZE) { |
| 2366 | wpa_printf(MSG_DEBUG, |
| 2367 | "FILS: Too short frame to include AES-SIV data"); |
| 2368 | return -1; |
| 2369 | } |
| 2370 | |
| 2371 | /* AES-SIV AAD vectors */ |
| 2372 | |
| 2373 | /* The STA's MAC address */ |
| 2374 | aad[0] = mgmt->sa; |
| 2375 | aad_len[0] = ETH_ALEN; |
| 2376 | /* The AP's BSSID */ |
| 2377 | aad[1] = mgmt->da; |
| 2378 | aad_len[1] = ETH_ALEN; |
| 2379 | /* The STA's nonce */ |
| 2380 | aad[2] = sm->SNonce; |
| 2381 | aad_len[2] = FILS_NONCE_LEN; |
| 2382 | /* The AP's nonce */ |
| 2383 | aad[3] = sm->ANonce; |
| 2384 | aad_len[3] = FILS_NONCE_LEN; |
| 2385 | /* |
| 2386 | * The (Re)Association Request frame from the Capability Information |
| 2387 | * field to the FILS Session element (both inclusive). |
| 2388 | */ |
| 2389 | aad[4] = (const u8 *) &mgmt->u.assoc_req.capab_info; |
Dmitry Shmidt | ebd93af | 2017-02-21 13:40:44 -0800 | [diff] [blame] | 2390 | aad_len[4] = crypt - aad[4]; |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 2391 | |
| 2392 | if (aes_siv_decrypt(sm->PTK.kek, sm->PTK.kek_len, crypt, end - crypt, |
Dmitry Shmidt | ebd93af | 2017-02-21 13:40:44 -0800 | [diff] [blame] | 2393 | 5, aad, aad_len, pos + (crypt - ie_start)) < 0) { |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 2394 | wpa_printf(MSG_DEBUG, |
| 2395 | "FILS: Invalid AES-SIV data in the frame"); |
| 2396 | return -1; |
| 2397 | } |
| 2398 | wpa_hexdump(MSG_DEBUG, "FILS: Decrypted Association Request elements", |
| 2399 | pos, left - AES_BLOCK_SIZE); |
| 2400 | |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 2401 | if (wpa_fils_validate_key_confirm(sm, pos, left - AES_BLOCK_SIZE) < 0) { |
| 2402 | wpa_printf(MSG_DEBUG, "FILS: Key Confirm validation failed"); |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 2403 | return -1; |
| 2404 | } |
| 2405 | |
| 2406 | return left - AES_BLOCK_SIZE; |
| 2407 | } |
| 2408 | |
| 2409 | |
| 2410 | int fils_encrypt_assoc(struct wpa_state_machine *sm, u8 *buf, |
Dmitry Shmidt | ebd93af | 2017-02-21 13:40:44 -0800 | [diff] [blame] | 2411 | size_t current_len, size_t max_len, |
| 2412 | const struct wpabuf *hlp) |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 2413 | { |
| 2414 | u8 *end = buf + max_len; |
| 2415 | u8 *pos = buf + current_len; |
| 2416 | struct ieee80211_mgmt *mgmt; |
| 2417 | struct wpabuf *plain; |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 2418 | const u8 *aad[5]; |
| 2419 | size_t aad_len[5]; |
| 2420 | |
| 2421 | if (!sm || !sm->PTK_valid) |
| 2422 | return -1; |
| 2423 | |
| 2424 | wpa_hexdump(MSG_DEBUG, |
| 2425 | "FILS: Association Response frame before FILS processing", |
| 2426 | buf, current_len); |
| 2427 | |
| 2428 | mgmt = (struct ieee80211_mgmt *) buf; |
| 2429 | |
| 2430 | /* AES-SIV AAD vectors */ |
| 2431 | |
| 2432 | /* The AP's BSSID */ |
| 2433 | aad[0] = mgmt->sa; |
| 2434 | aad_len[0] = ETH_ALEN; |
| 2435 | /* The STA's MAC address */ |
| 2436 | aad[1] = mgmt->da; |
| 2437 | aad_len[1] = ETH_ALEN; |
| 2438 | /* The AP's nonce */ |
| 2439 | aad[2] = sm->ANonce; |
| 2440 | aad_len[2] = FILS_NONCE_LEN; |
| 2441 | /* The STA's nonce */ |
| 2442 | aad[3] = sm->SNonce; |
| 2443 | aad_len[3] = FILS_NONCE_LEN; |
| 2444 | /* |
| 2445 | * The (Re)Association Response frame from the Capability Information |
| 2446 | * field (the same offset in both Association and Reassociation |
| 2447 | * Response frames) to the FILS Session element (both inclusive). |
| 2448 | */ |
| 2449 | aad[4] = (const u8 *) &mgmt->u.assoc_resp.capab_info; |
| 2450 | aad_len[4] = pos - aad[4]; |
| 2451 | |
| 2452 | /* The following elements will be encrypted with AES-SIV */ |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 2453 | plain = fils_prepare_plainbuf(sm, hlp); |
| 2454 | if (!plain) { |
| 2455 | wpa_printf(MSG_DEBUG, "FILS: Plain buffer prep failed"); |
| 2456 | return -1; |
| 2457 | } |
| 2458 | |
| 2459 | if (pos + wpabuf_len(plain) + AES_BLOCK_SIZE > end) { |
| 2460 | wpa_printf(MSG_DEBUG, |
| 2461 | "FILS: Not enough room for FILS elements"); |
| 2462 | wpabuf_free(plain); |
| 2463 | return -1; |
| 2464 | } |
| 2465 | |
| 2466 | wpa_hexdump_buf_key(MSG_DEBUG, "FILS: Association Response plaintext", |
| 2467 | plain); |
| 2468 | |
| 2469 | if (aes_siv_encrypt(sm->PTK.kek, sm->PTK.kek_len, |
| 2470 | wpabuf_head(plain), wpabuf_len(plain), |
| 2471 | 5, aad, aad_len, pos) < 0) { |
| 2472 | wpabuf_free(plain); |
| 2473 | return -1; |
| 2474 | } |
| 2475 | |
| 2476 | wpa_hexdump(MSG_DEBUG, |
| 2477 | "FILS: Encrypted Association Response elements", |
| 2478 | pos, AES_BLOCK_SIZE + wpabuf_len(plain)); |
| 2479 | current_len += wpabuf_len(plain) + AES_BLOCK_SIZE; |
| 2480 | wpabuf_free(plain); |
| 2481 | |
| 2482 | sm->fils_completed = 1; |
| 2483 | |
| 2484 | return current_len; |
| 2485 | } |
| 2486 | |
| 2487 | |
| 2488 | static struct wpabuf * fils_prepare_plainbuf(struct wpa_state_machine *sm, |
| 2489 | const struct wpabuf *hlp) |
| 2490 | { |
| 2491 | struct wpabuf *plain; |
| 2492 | u8 *len, *tmp, *tmp2; |
| 2493 | u8 hdr[2]; |
| 2494 | u8 *gtk, dummy_gtk[32]; |
| 2495 | size_t gtk_len; |
| 2496 | struct wpa_group *gsm; |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 2497 | |
| 2498 | plain = wpabuf_alloc(1000); |
| 2499 | if (!plain) |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 2500 | return NULL; |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 2501 | |
| 2502 | /* TODO: FILS Public Key */ |
| 2503 | |
| 2504 | /* FILS Key Confirmation */ |
| 2505 | wpabuf_put_u8(plain, WLAN_EID_EXTENSION); /* Element ID */ |
| 2506 | wpabuf_put_u8(plain, 1 + sm->fils_key_auth_len); /* Length */ |
| 2507 | /* Element ID Extension */ |
| 2508 | wpabuf_put_u8(plain, WLAN_EID_EXT_FILS_KEY_CONFIRM); |
| 2509 | wpabuf_put_data(plain, sm->fils_key_auth_ap, sm->fils_key_auth_len); |
| 2510 | |
Dmitry Shmidt | ebd93af | 2017-02-21 13:40:44 -0800 | [diff] [blame] | 2511 | /* FILS HLP Container */ |
| 2512 | if (hlp) |
| 2513 | wpabuf_put_buf(plain, hlp); |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 2514 | |
| 2515 | /* TODO: FILS IP Address Assignment */ |
| 2516 | |
| 2517 | /* Key Delivery */ |
| 2518 | gsm = sm->group; |
| 2519 | wpabuf_put_u8(plain, WLAN_EID_EXTENSION); /* Element ID */ |
| 2520 | len = wpabuf_put(plain, 1); |
| 2521 | wpabuf_put_u8(plain, WLAN_EID_EXT_KEY_DELIVERY); |
| 2522 | wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN, |
| 2523 | wpabuf_put(plain, WPA_KEY_RSC_LEN)); |
| 2524 | /* GTK KDE */ |
| 2525 | gtk = gsm->GTK[gsm->GN - 1]; |
| 2526 | gtk_len = gsm->GTK_len; |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame^] | 2527 | if (sm->wpa_auth->conf.disable_gtk || |
| 2528 | sm->wpa_key_mgmt == WPA_KEY_MGMT_OSEN) { |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 2529 | /* |
| 2530 | * Provide unique random GTK to each STA to prevent use |
| 2531 | * of GTK in the BSS. |
| 2532 | */ |
| 2533 | if (random_get_bytes(dummy_gtk, gtk_len) < 0) { |
| 2534 | wpabuf_free(plain); |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 2535 | return NULL; |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 2536 | } |
| 2537 | gtk = dummy_gtk; |
| 2538 | } |
| 2539 | hdr[0] = gsm->GN & 0x03; |
| 2540 | hdr[1] = 0; |
| 2541 | tmp = wpabuf_put(plain, 0); |
| 2542 | tmp2 = wpa_add_kde(tmp, RSN_KEY_DATA_GROUPKEY, hdr, 2, |
| 2543 | gtk, gtk_len); |
| 2544 | wpabuf_put(plain, tmp2 - tmp); |
| 2545 | |
| 2546 | /* IGTK KDE */ |
| 2547 | tmp = wpabuf_put(plain, 0); |
| 2548 | tmp2 = ieee80211w_kde_add(sm, tmp); |
| 2549 | wpabuf_put(plain, tmp2 - tmp); |
| 2550 | |
| 2551 | *len = (u8 *) wpabuf_put(plain, 0) - len - 1; |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 2552 | return plain; |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 2553 | } |
| 2554 | |
| 2555 | |
| 2556 | int fils_set_tk(struct wpa_state_machine *sm) |
| 2557 | { |
| 2558 | enum wpa_alg alg; |
| 2559 | int klen; |
| 2560 | |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 2561 | if (!sm || !sm->PTK_valid) { |
| 2562 | wpa_printf(MSG_DEBUG, "FILS: No valid PTK available to set TK"); |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 2563 | return -1; |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 2564 | } |
| 2565 | if (sm->tk_already_set) { |
| 2566 | wpa_printf(MSG_DEBUG, "FILS: TK already set to the driver"); |
| 2567 | return -1; |
| 2568 | } |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 2569 | |
| 2570 | alg = wpa_cipher_to_alg(sm->pairwise); |
| 2571 | klen = wpa_cipher_key_len(sm->pairwise); |
| 2572 | |
| 2573 | wpa_printf(MSG_DEBUG, "FILS: Configure TK to the driver"); |
| 2574 | if (wpa_auth_set_key(sm->wpa_auth, 0, alg, sm->addr, 0, |
| 2575 | sm->PTK.tk, klen)) { |
| 2576 | wpa_printf(MSG_DEBUG, "FILS: Failed to set TK to the driver"); |
| 2577 | return -1; |
| 2578 | } |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 2579 | sm->tk_already_set = TRUE; |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 2580 | |
| 2581 | return 0; |
| 2582 | } |
| 2583 | |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 2584 | |
| 2585 | u8 * hostapd_eid_assoc_fils_session(struct wpa_state_machine *sm, u8 *buf, |
| 2586 | const u8 *fils_session, struct wpabuf *hlp) |
| 2587 | { |
| 2588 | struct wpabuf *plain; |
| 2589 | u8 *pos = buf; |
| 2590 | |
| 2591 | /* FILS Session */ |
| 2592 | *pos++ = WLAN_EID_EXTENSION; /* Element ID */ |
| 2593 | *pos++ = 1 + FILS_SESSION_LEN; /* Length */ |
| 2594 | *pos++ = WLAN_EID_EXT_FILS_SESSION; /* Element ID Extension */ |
| 2595 | os_memcpy(pos, fils_session, FILS_SESSION_LEN); |
| 2596 | pos += FILS_SESSION_LEN; |
| 2597 | |
| 2598 | plain = fils_prepare_plainbuf(sm, hlp); |
| 2599 | if (!plain) { |
| 2600 | wpa_printf(MSG_DEBUG, "FILS: Plain buffer prep failed"); |
| 2601 | return NULL; |
| 2602 | } |
| 2603 | |
| 2604 | os_memcpy(pos, wpabuf_head(plain), wpabuf_len(plain)); |
| 2605 | pos += wpabuf_len(plain); |
| 2606 | |
| 2607 | wpa_printf(MSG_DEBUG, "%s: plain buf_len: %u", __func__, |
| 2608 | (unsigned int) wpabuf_len(plain)); |
| 2609 | wpabuf_free(plain); |
| 2610 | sm->fils_completed = 1; |
| 2611 | return pos; |
| 2612 | } |
| 2613 | |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 2614 | #endif /* CONFIG_FILS */ |
| 2615 | |
| 2616 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2617 | SM_STATE(WPA_PTK, PTKCALCNEGOTIATING) |
| 2618 | { |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 2619 | struct wpa_authenticator *wpa_auth = sm->wpa_auth; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2620 | struct wpa_ptk PTK; |
Dmitry Shmidt | dda10c2 | 2015-03-24 16:05:01 -0700 | [diff] [blame] | 2621 | int ok = 0, psk_found = 0; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2622 | const u8 *pmk = NULL; |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 2623 | size_t pmk_len; |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 2624 | int ft; |
| 2625 | const u8 *eapol_key_ie, *key_data, *mic; |
| 2626 | u16 key_data_length; |
| 2627 | size_t mic_len, eapol_key_ie_len; |
| 2628 | struct ieee802_1x_hdr *hdr; |
| 2629 | struct wpa_eapol_key *key; |
| 2630 | struct wpa_eapol_ie_parse kde; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2631 | |
| 2632 | SM_ENTRY_MA(WPA_PTK, PTKCALCNEGOTIATING, wpa_ptk); |
| 2633 | sm->EAPOLKeyReceived = FALSE; |
Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 2634 | sm->update_snonce = FALSE; |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 2635 | os_memset(&PTK, 0, sizeof(PTK)); |
| 2636 | |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 2637 | mic_len = wpa_mic_len(sm->wpa_key_mgmt, sm->pmk_len); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2638 | |
| 2639 | /* WPA with IEEE 802.1X: use the derived PMK from EAP |
| 2640 | * WPA-PSK: iterate through possible PSKs and select the one matching |
| 2641 | * the packet */ |
| 2642 | for (;;) { |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 2643 | if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt) && |
| 2644 | !wpa_key_mgmt_sae(sm->wpa_key_mgmt)) { |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 2645 | pmk = wpa_auth_get_psk(sm->wpa_auth, sm->addr, |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 2646 | sm->p2p_dev_addr, pmk, &pmk_len); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2647 | if (pmk == NULL) |
| 2648 | break; |
Dmitry Shmidt | dda10c2 | 2015-03-24 16:05:01 -0700 | [diff] [blame] | 2649 | psk_found = 1; |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame^] | 2650 | #ifdef CONFIG_IEEE80211R_AP |
| 2651 | if (wpa_key_mgmt_ft_psk(sm->wpa_key_mgmt)) { |
| 2652 | os_memcpy(sm->xxkey, pmk, pmk_len); |
| 2653 | sm->xxkey_len = pmk_len; |
| 2654 | } |
| 2655 | #endif /* CONFIG_IEEE80211R_AP */ |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 2656 | } else { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2657 | pmk = sm->PMK; |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 2658 | pmk_len = sm->pmk_len; |
| 2659 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2660 | |
Dmitry Shmidt | ebd93af | 2017-02-21 13:40:44 -0800 | [diff] [blame] | 2661 | if (wpa_derive_ptk(sm, sm->SNonce, pmk, pmk_len, &PTK) < 0) |
| 2662 | break; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2663 | |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 2664 | if (mic_len && |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 2665 | wpa_verify_key_mic(sm->wpa_key_mgmt, pmk_len, &PTK, |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 2666 | sm->last_rx_eapol_key, |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2667 | sm->last_rx_eapol_key_len) == 0) { |
| 2668 | ok = 1; |
| 2669 | break; |
| 2670 | } |
| 2671 | |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 2672 | #ifdef CONFIG_FILS |
| 2673 | if (!mic_len && |
| 2674 | wpa_aead_decrypt(sm, &PTK, sm->last_rx_eapol_key, |
| 2675 | sm->last_rx_eapol_key_len, NULL) == 0) { |
| 2676 | ok = 1; |
| 2677 | break; |
| 2678 | } |
| 2679 | #endif /* CONFIG_FILS */ |
| 2680 | |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame^] | 2681 | if (!wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt) || |
| 2682 | wpa_key_mgmt_sae(sm->wpa_key_mgmt)) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2683 | break; |
| 2684 | } |
| 2685 | |
| 2686 | if (!ok) { |
| 2687 | wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG, |
| 2688 | "invalid MIC in msg 2/4 of 4-Way Handshake"); |
Dmitry Shmidt | dda10c2 | 2015-03-24 16:05:01 -0700 | [diff] [blame] | 2689 | if (psk_found) |
| 2690 | wpa_auth_psk_failure_report(sm->wpa_auth, sm->addr); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2691 | return; |
| 2692 | } |
| 2693 | |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 2694 | /* |
| 2695 | * Note: last_rx_eapol_key length fields have already been validated in |
| 2696 | * wpa_receive(). |
| 2697 | */ |
| 2698 | hdr = (struct ieee802_1x_hdr *) sm->last_rx_eapol_key; |
| 2699 | key = (struct wpa_eapol_key *) (hdr + 1); |
| 2700 | mic = (u8 *) (key + 1); |
| 2701 | key_data = mic + mic_len + 2; |
| 2702 | key_data_length = WPA_GET_BE16(mic + mic_len); |
| 2703 | if (key_data_length > sm->last_rx_eapol_key_len - sizeof(*hdr) - |
| 2704 | sizeof(*key) - mic_len - 2) |
| 2705 | return; |
| 2706 | |
| 2707 | if (wpa_parse_kde_ies(key_data, key_data_length, &kde) < 0) { |
| 2708 | wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_INFO, |
| 2709 | "received EAPOL-Key msg 2/4 with invalid Key Data contents"); |
| 2710 | return; |
| 2711 | } |
| 2712 | if (kde.rsn_ie) { |
| 2713 | eapol_key_ie = kde.rsn_ie; |
| 2714 | eapol_key_ie_len = kde.rsn_ie_len; |
| 2715 | } else if (kde.osen) { |
| 2716 | eapol_key_ie = kde.osen; |
| 2717 | eapol_key_ie_len = kde.osen_len; |
| 2718 | } else { |
| 2719 | eapol_key_ie = kde.wpa_ie; |
| 2720 | eapol_key_ie_len = kde.wpa_ie_len; |
| 2721 | } |
| 2722 | ft = sm->wpa == WPA_VERSION_WPA2 && wpa_key_mgmt_ft(sm->wpa_key_mgmt); |
| 2723 | if (sm->wpa_ie == NULL || |
| 2724 | wpa_compare_rsn_ie(ft, sm->wpa_ie, sm->wpa_ie_len, |
| 2725 | eapol_key_ie, eapol_key_ie_len)) { |
| 2726 | wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO, |
| 2727 | "WPA IE from (Re)AssocReq did not match with msg 2/4"); |
| 2728 | if (sm->wpa_ie) { |
| 2729 | wpa_hexdump(MSG_DEBUG, "WPA IE in AssocReq", |
| 2730 | sm->wpa_ie, sm->wpa_ie_len); |
| 2731 | } |
| 2732 | wpa_hexdump(MSG_DEBUG, "WPA IE in msg 2/4", |
| 2733 | eapol_key_ie, eapol_key_ie_len); |
| 2734 | /* MLME-DEAUTHENTICATE.request */ |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 2735 | wpa_sta_disconnect(wpa_auth, sm->addr, |
| 2736 | WLAN_REASON_PREV_AUTH_NOT_VALID); |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 2737 | return; |
| 2738 | } |
| 2739 | #ifdef CONFIG_IEEE80211R_AP |
| 2740 | if (ft && ft_check_msg_2_of_4(wpa_auth, sm, &kde) < 0) { |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 2741 | wpa_sta_disconnect(wpa_auth, sm->addr, |
| 2742 | WLAN_REASON_PREV_AUTH_NOT_VALID); |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 2743 | return; |
| 2744 | } |
| 2745 | #endif /* CONFIG_IEEE80211R_AP */ |
| 2746 | #ifdef CONFIG_P2P |
| 2747 | if (kde.ip_addr_req && kde.ip_addr_req[0] && |
| 2748 | wpa_auth->ip_pool && WPA_GET_BE32(sm->ip_addr) == 0) { |
| 2749 | int idx; |
| 2750 | wpa_printf(MSG_DEBUG, |
| 2751 | "P2P: IP address requested in EAPOL-Key exchange"); |
| 2752 | idx = bitfield_get_first_zero(wpa_auth->ip_pool); |
| 2753 | if (idx >= 0) { |
| 2754 | u32 start = WPA_GET_BE32(wpa_auth->conf.ip_addr_start); |
| 2755 | bitfield_set(wpa_auth->ip_pool, idx); |
| 2756 | WPA_PUT_BE32(sm->ip_addr, start + idx); |
| 2757 | wpa_printf(MSG_DEBUG, |
| 2758 | "P2P: Assigned IP address %u.%u.%u.%u to " |
| 2759 | MACSTR, sm->ip_addr[0], sm->ip_addr[1], |
| 2760 | sm->ip_addr[2], sm->ip_addr[3], |
| 2761 | MAC2STR(sm->addr)); |
| 2762 | } |
| 2763 | } |
| 2764 | #endif /* CONFIG_P2P */ |
| 2765 | |
| 2766 | #ifdef CONFIG_IEEE80211R_AP |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2767 | if (sm->wpa == WPA_VERSION_WPA2 && wpa_key_mgmt_ft(sm->wpa_key_mgmt)) { |
| 2768 | /* |
| 2769 | * Verify that PMKR1Name from EAPOL-Key message 2/4 matches |
| 2770 | * with the value we derived. |
| 2771 | */ |
Dmitry Shmidt | c281702 | 2014-07-02 10:32:10 -0700 | [diff] [blame] | 2772 | if (os_memcmp_const(sm->sup_pmk_r1_name, sm->pmk_r1_name, |
| 2773 | WPA_PMK_NAME_LEN) != 0) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2774 | wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG, |
| 2775 | "PMKR1Name mismatch in FT 4-way " |
| 2776 | "handshake"); |
| 2777 | wpa_hexdump(MSG_DEBUG, "FT: PMKR1Name from " |
| 2778 | "Supplicant", |
| 2779 | sm->sup_pmk_r1_name, WPA_PMK_NAME_LEN); |
| 2780 | wpa_hexdump(MSG_DEBUG, "FT: Derived PMKR1Name", |
| 2781 | sm->pmk_r1_name, WPA_PMK_NAME_LEN); |
| 2782 | return; |
| 2783 | } |
| 2784 | } |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 2785 | #endif /* CONFIG_IEEE80211R_AP */ |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2786 | |
| 2787 | sm->pending_1_of_4_timeout = 0; |
| 2788 | eloop_cancel_timeout(wpa_send_eapol_timeout, sm->wpa_auth, sm); |
| 2789 | |
| 2790 | if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt)) { |
| 2791 | /* PSK may have changed from the previous choice, so update |
| 2792 | * state machine data based on whatever PSK was selected here. |
| 2793 | */ |
| 2794 | os_memcpy(sm->PMK, pmk, PMK_LEN); |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 2795 | sm->pmk_len = PMK_LEN; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2796 | } |
| 2797 | |
| 2798 | sm->MICVerified = TRUE; |
| 2799 | |
| 2800 | os_memcpy(&sm->PTK, &PTK, sizeof(PTK)); |
| 2801 | sm->PTK_valid = TRUE; |
| 2802 | } |
| 2803 | |
| 2804 | |
| 2805 | SM_STATE(WPA_PTK, PTKCALCNEGOTIATING2) |
| 2806 | { |
| 2807 | SM_ENTRY_MA(WPA_PTK, PTKCALCNEGOTIATING2, wpa_ptk); |
| 2808 | sm->TimeoutCtr = 0; |
| 2809 | } |
| 2810 | |
| 2811 | |
| 2812 | #ifdef CONFIG_IEEE80211W |
| 2813 | |
| 2814 | static int ieee80211w_kde_len(struct wpa_state_machine *sm) |
| 2815 | { |
| 2816 | if (sm->mgmt_frame_prot) { |
Dmitry Shmidt | b36ed7c | 2014-03-17 10:57:26 -0700 | [diff] [blame] | 2817 | size_t len; |
| 2818 | len = wpa_cipher_key_len(sm->wpa_auth->conf.group_mgmt_cipher); |
| 2819 | return 2 + RSN_SELECTOR_LEN + WPA_IGTK_KDE_PREFIX_LEN + len; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2820 | } |
| 2821 | |
| 2822 | return 0; |
| 2823 | } |
| 2824 | |
| 2825 | |
| 2826 | static u8 * ieee80211w_kde_add(struct wpa_state_machine *sm, u8 *pos) |
| 2827 | { |
| 2828 | struct wpa_igtk_kde igtk; |
| 2829 | struct wpa_group *gsm = sm->group; |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 2830 | u8 rsc[WPA_KEY_RSC_LEN]; |
Dmitry Shmidt | b36ed7c | 2014-03-17 10:57:26 -0700 | [diff] [blame] | 2831 | size_t len = wpa_cipher_key_len(sm->wpa_auth->conf.group_mgmt_cipher); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2832 | |
| 2833 | if (!sm->mgmt_frame_prot) |
| 2834 | return pos; |
| 2835 | |
| 2836 | igtk.keyid[0] = gsm->GN_igtk; |
| 2837 | igtk.keyid[1] = 0; |
| 2838 | if (gsm->wpa_group_state != WPA_GROUP_SETKEYSDONE || |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 2839 | 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] | 2840 | os_memset(igtk.pn, 0, sizeof(igtk.pn)); |
Dmitry Shmidt | 68d0e3e | 2013-10-28 17:59:21 -0700 | [diff] [blame] | 2841 | else |
| 2842 | os_memcpy(igtk.pn, rsc, sizeof(igtk.pn)); |
Dmitry Shmidt | b36ed7c | 2014-03-17 10:57:26 -0700 | [diff] [blame] | 2843 | os_memcpy(igtk.igtk, gsm->IGTK[gsm->GN_igtk - 4], len); |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame^] | 2844 | if (sm->wpa_auth->conf.disable_gtk || |
| 2845 | sm->wpa_key_mgmt == WPA_KEY_MGMT_OSEN) { |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 2846 | /* |
| 2847 | * Provide unique random IGTK to each STA to prevent use of |
| 2848 | * IGTK in the BSS. |
| 2849 | */ |
Dmitry Shmidt | b36ed7c | 2014-03-17 10:57:26 -0700 | [diff] [blame] | 2850 | if (random_get_bytes(igtk.igtk, len) < 0) |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 2851 | return pos; |
| 2852 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2853 | pos = wpa_add_kde(pos, RSN_KEY_DATA_IGTK, |
Dmitry Shmidt | b36ed7c | 2014-03-17 10:57:26 -0700 | [diff] [blame] | 2854 | (const u8 *) &igtk, WPA_IGTK_KDE_PREFIX_LEN + len, |
| 2855 | NULL, 0); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2856 | |
| 2857 | return pos; |
| 2858 | } |
| 2859 | |
| 2860 | #else /* CONFIG_IEEE80211W */ |
| 2861 | |
| 2862 | static int ieee80211w_kde_len(struct wpa_state_machine *sm) |
| 2863 | { |
| 2864 | return 0; |
| 2865 | } |
| 2866 | |
| 2867 | |
| 2868 | static u8 * ieee80211w_kde_add(struct wpa_state_machine *sm, u8 *pos) |
| 2869 | { |
| 2870 | return pos; |
| 2871 | } |
| 2872 | |
| 2873 | #endif /* CONFIG_IEEE80211W */ |
| 2874 | |
| 2875 | |
| 2876 | SM_STATE(WPA_PTK, PTKINITNEGOTIATING) |
| 2877 | { |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 2878 | 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] | 2879 | size_t gtk_len, kde_len; |
| 2880 | struct wpa_group *gsm = sm->group; |
| 2881 | u8 *wpa_ie; |
| 2882 | int wpa_ie_len, secure, keyidx, encr = 0; |
| 2883 | |
| 2884 | SM_ENTRY_MA(WPA_PTK, PTKINITNEGOTIATING, wpa_ptk); |
| 2885 | sm->TimeoutEvt = FALSE; |
| 2886 | |
| 2887 | sm->TimeoutCtr++; |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 2888 | if (sm->wpa_auth->conf.wpa_disable_eapol_key_retries && |
| 2889 | sm->TimeoutCtr > 1) { |
| 2890 | /* Do not allow retransmission of EAPOL-Key msg 3/4 */ |
| 2891 | return; |
| 2892 | } |
Dmitry Shmidt | ebd93af | 2017-02-21 13:40:44 -0800 | [diff] [blame] | 2893 | if (sm->TimeoutCtr > sm->wpa_auth->conf.wpa_pairwise_update_count) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2894 | /* No point in sending the EAPOL-Key - we will disconnect |
| 2895 | * immediately following this. */ |
| 2896 | return; |
| 2897 | } |
| 2898 | |
| 2899 | /* Send EAPOL(1, 1, 1, Pair, P, RSC, ANonce, MIC(PTK), RSNIE, [MDIE], |
| 2900 | GTK[GN], IGTK, [FTIE], [TIE * 2]) |
| 2901 | */ |
| 2902 | os_memset(rsc, 0, WPA_KEY_RSC_LEN); |
| 2903 | wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN, rsc); |
| 2904 | /* If FT is used, wpa_auth->wpa_ie includes both RSNIE and MDIE */ |
| 2905 | wpa_ie = sm->wpa_auth->wpa_ie; |
| 2906 | wpa_ie_len = sm->wpa_auth->wpa_ie_len; |
| 2907 | if (sm->wpa == WPA_VERSION_WPA && |
| 2908 | (sm->wpa_auth->conf.wpa & WPA_PROTO_RSN) && |
| 2909 | wpa_ie_len > wpa_ie[1] + 2 && wpa_ie[0] == WLAN_EID_RSN) { |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 2910 | /* WPA-only STA, remove RSN IE and possible MDIE */ |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2911 | wpa_ie = wpa_ie + wpa_ie[1] + 2; |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 2912 | if (wpa_ie[0] == WLAN_EID_MOBILITY_DOMAIN) |
| 2913 | wpa_ie = wpa_ie + wpa_ie[1] + 2; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2914 | wpa_ie_len = wpa_ie[1] + 2; |
| 2915 | } |
| 2916 | wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG, |
| 2917 | "sending 3/4 msg of 4-Way Handshake"); |
| 2918 | if (sm->wpa == WPA_VERSION_WPA2) { |
| 2919 | /* WPA2 send GTK in the 4-way handshake */ |
| 2920 | secure = 1; |
| 2921 | gtk = gsm->GTK[gsm->GN - 1]; |
| 2922 | gtk_len = gsm->GTK_len; |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame^] | 2923 | if (sm->wpa_auth->conf.disable_gtk || |
| 2924 | sm->wpa_key_mgmt == WPA_KEY_MGMT_OSEN) { |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 2925 | /* |
| 2926 | * Provide unique random GTK to each STA to prevent use |
| 2927 | * of GTK in the BSS. |
| 2928 | */ |
| 2929 | if (random_get_bytes(dummy_gtk, gtk_len) < 0) |
| 2930 | return; |
| 2931 | gtk = dummy_gtk; |
| 2932 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2933 | keyidx = gsm->GN; |
| 2934 | _rsc = rsc; |
| 2935 | encr = 1; |
| 2936 | } else { |
| 2937 | /* WPA does not include GTK in msg 3/4 */ |
| 2938 | secure = 0; |
| 2939 | gtk = NULL; |
| 2940 | gtk_len = 0; |
| 2941 | keyidx = 0; |
| 2942 | _rsc = NULL; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 2943 | if (sm->rx_eapol_key_secure) { |
| 2944 | /* |
| 2945 | * It looks like Windows 7 supplicant tries to use |
| 2946 | * Secure bit in msg 2/4 after having reported Michael |
| 2947 | * MIC failure and it then rejects the 4-way handshake |
| 2948 | * if msg 3/4 does not set Secure bit. Work around this |
| 2949 | * by setting the Secure bit here even in the case of |
| 2950 | * WPA if the supplicant used it first. |
| 2951 | */ |
| 2952 | wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG, |
| 2953 | "STA used Secure bit in WPA msg 2/4 - " |
| 2954 | "set Secure for 3/4 as workaround"); |
| 2955 | secure = 1; |
| 2956 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2957 | } |
| 2958 | |
| 2959 | kde_len = wpa_ie_len + ieee80211w_kde_len(sm); |
| 2960 | if (gtk) |
| 2961 | kde_len += 2 + RSN_SELECTOR_LEN + 2 + gtk_len; |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 2962 | #ifdef CONFIG_IEEE80211R_AP |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2963 | if (wpa_key_mgmt_ft(sm->wpa_key_mgmt)) { |
| 2964 | kde_len += 2 + PMKID_LEN; /* PMKR1Name into RSN IE */ |
| 2965 | kde_len += 300; /* FTIE + 2 * TIE */ |
| 2966 | } |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 2967 | #endif /* CONFIG_IEEE80211R_AP */ |
Dmitry Shmidt | cf32e60 | 2014-01-28 10:57:39 -0800 | [diff] [blame] | 2968 | #ifdef CONFIG_P2P |
| 2969 | if (WPA_GET_BE32(sm->ip_addr) > 0) |
| 2970 | kde_len += 2 + RSN_SELECTOR_LEN + 3 * 4; |
| 2971 | #endif /* CONFIG_P2P */ |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2972 | kde = os_malloc(kde_len); |
| 2973 | if (kde == NULL) |
| 2974 | return; |
| 2975 | |
| 2976 | pos = kde; |
| 2977 | os_memcpy(pos, wpa_ie, wpa_ie_len); |
| 2978 | pos += wpa_ie_len; |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 2979 | #ifdef CONFIG_IEEE80211R_AP |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2980 | if (wpa_key_mgmt_ft(sm->wpa_key_mgmt)) { |
Dmitry Shmidt | 55840ad | 2015-12-14 12:45:46 -0800 | [diff] [blame] | 2981 | int res; |
| 2982 | size_t elen; |
| 2983 | |
| 2984 | elen = pos - kde; |
| 2985 | res = wpa_insert_pmkid(kde, &elen, sm->pmk_r1_name); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2986 | if (res < 0) { |
| 2987 | wpa_printf(MSG_ERROR, "FT: Failed to insert " |
| 2988 | "PMKR1Name into RSN IE in EAPOL-Key data"); |
| 2989 | os_free(kde); |
| 2990 | return; |
| 2991 | } |
Dmitry Shmidt | 55840ad | 2015-12-14 12:45:46 -0800 | [diff] [blame] | 2992 | pos -= wpa_ie_len; |
| 2993 | pos += elen; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2994 | } |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 2995 | #endif /* CONFIG_IEEE80211R_AP */ |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2996 | if (gtk) { |
| 2997 | u8 hdr[2]; |
| 2998 | hdr[0] = keyidx & 0x03; |
| 2999 | hdr[1] = 0; |
| 3000 | pos = wpa_add_kde(pos, RSN_KEY_DATA_GROUPKEY, hdr, 2, |
| 3001 | gtk, gtk_len); |
| 3002 | } |
| 3003 | pos = ieee80211w_kde_add(sm, pos); |
| 3004 | |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 3005 | #ifdef CONFIG_IEEE80211R_AP |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3006 | if (wpa_key_mgmt_ft(sm->wpa_key_mgmt)) { |
| 3007 | int res; |
| 3008 | struct wpa_auth_config *conf; |
| 3009 | |
| 3010 | conf = &sm->wpa_auth->conf; |
Dmitry Shmidt | 55840ad | 2015-12-14 12:45:46 -0800 | [diff] [blame] | 3011 | if (sm->assoc_resp_ftie && |
| 3012 | kde + kde_len - pos >= 2 + sm->assoc_resp_ftie[1]) { |
| 3013 | os_memcpy(pos, sm->assoc_resp_ftie, |
| 3014 | 2 + sm->assoc_resp_ftie[1]); |
| 3015 | res = 2 + sm->assoc_resp_ftie[1]; |
| 3016 | } else { |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame^] | 3017 | int use_sha384 = wpa_key_mgmt_sha384(sm->wpa_key_mgmt); |
| 3018 | |
| 3019 | res = wpa_write_ftie(conf, use_sha384, |
| 3020 | conf->r0_key_holder, |
Dmitry Shmidt | 55840ad | 2015-12-14 12:45:46 -0800 | [diff] [blame] | 3021 | conf->r0_key_holder_len, |
| 3022 | NULL, NULL, pos, |
| 3023 | kde + kde_len - pos, |
| 3024 | NULL, 0); |
| 3025 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3026 | if (res < 0) { |
| 3027 | wpa_printf(MSG_ERROR, "FT: Failed to insert FTIE " |
| 3028 | "into EAPOL-Key Key Data"); |
| 3029 | os_free(kde); |
| 3030 | return; |
| 3031 | } |
| 3032 | pos += res; |
| 3033 | |
| 3034 | /* TIE[ReassociationDeadline] (TU) */ |
| 3035 | *pos++ = WLAN_EID_TIMEOUT_INTERVAL; |
| 3036 | *pos++ = 5; |
| 3037 | *pos++ = WLAN_TIMEOUT_REASSOC_DEADLINE; |
| 3038 | WPA_PUT_LE32(pos, conf->reassociation_deadline); |
| 3039 | pos += 4; |
| 3040 | |
| 3041 | /* TIE[KeyLifetime] (seconds) */ |
| 3042 | *pos++ = WLAN_EID_TIMEOUT_INTERVAL; |
| 3043 | *pos++ = 5; |
| 3044 | *pos++ = WLAN_TIMEOUT_KEY_LIFETIME; |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame^] | 3045 | WPA_PUT_LE32(pos, conf->r0_key_lifetime); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3046 | pos += 4; |
| 3047 | } |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 3048 | #endif /* CONFIG_IEEE80211R_AP */ |
Dmitry Shmidt | cf32e60 | 2014-01-28 10:57:39 -0800 | [diff] [blame] | 3049 | #ifdef CONFIG_P2P |
| 3050 | if (WPA_GET_BE32(sm->ip_addr) > 0) { |
| 3051 | u8 addr[3 * 4]; |
| 3052 | os_memcpy(addr, sm->ip_addr, 4); |
| 3053 | os_memcpy(addr + 4, sm->wpa_auth->conf.ip_addr_mask, 4); |
| 3054 | os_memcpy(addr + 8, sm->wpa_auth->conf.ip_addr_go, 4); |
| 3055 | pos = wpa_add_kde(pos, WFA_KEY_DATA_IP_ADDR_ALLOC, |
| 3056 | addr, sizeof(addr), NULL, 0); |
| 3057 | } |
| 3058 | #endif /* CONFIG_P2P */ |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3059 | |
| 3060 | wpa_send_eapol(sm->wpa_auth, sm, |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 3061 | (secure ? WPA_KEY_INFO_SECURE : 0) | |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 3062 | (wpa_mic_len(sm->wpa_key_mgmt, sm->pmk_len) ? |
| 3063 | WPA_KEY_INFO_MIC : 0) | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3064 | WPA_KEY_INFO_ACK | WPA_KEY_INFO_INSTALL | |
| 3065 | WPA_KEY_INFO_KEY_TYPE, |
| 3066 | _rsc, sm->ANonce, kde, pos - kde, keyidx, encr); |
| 3067 | os_free(kde); |
| 3068 | } |
| 3069 | |
| 3070 | |
| 3071 | SM_STATE(WPA_PTK, PTKINITDONE) |
| 3072 | { |
| 3073 | SM_ENTRY_MA(WPA_PTK, PTKINITDONE, wpa_ptk); |
| 3074 | sm->EAPOLKeyReceived = FALSE; |
| 3075 | if (sm->Pair) { |
Dmitry Shmidt | 4530cfd | 2012-09-09 15:20:40 -0700 | [diff] [blame] | 3076 | enum wpa_alg alg = wpa_cipher_to_alg(sm->pairwise); |
| 3077 | int klen = wpa_cipher_key_len(sm->pairwise); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3078 | if (wpa_auth_set_key(sm->wpa_auth, 0, alg, sm->addr, 0, |
Dmitry Shmidt | 807291d | 2015-01-27 13:40:23 -0800 | [diff] [blame] | 3079 | sm->PTK.tk, klen)) { |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 3080 | wpa_sta_disconnect(sm->wpa_auth, sm->addr, |
| 3081 | WLAN_REASON_PREV_AUTH_NOT_VALID); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3082 | return; |
| 3083 | } |
| 3084 | /* FIX: MLME-SetProtection.Request(TA, Tx_Rx) */ |
| 3085 | sm->pairwise_set = TRUE; |
| 3086 | |
| 3087 | if (sm->wpa_auth->conf.wpa_ptk_rekey) { |
| 3088 | eloop_cancel_timeout(wpa_rekey_ptk, sm->wpa_auth, sm); |
| 3089 | eloop_register_timeout(sm->wpa_auth->conf. |
| 3090 | wpa_ptk_rekey, 0, wpa_rekey_ptk, |
| 3091 | sm->wpa_auth, sm); |
| 3092 | } |
| 3093 | |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 3094 | if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt) || |
| 3095 | sm->wpa_key_mgmt == WPA_KEY_MGMT_DPP || |
| 3096 | sm->wpa_key_mgmt == WPA_KEY_MGMT_OWE) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3097 | wpa_auth_set_eapol(sm->wpa_auth, sm->addr, |
| 3098 | WPA_EAPOL_authorized, 1); |
| 3099 | } |
| 3100 | } |
| 3101 | |
| 3102 | if (0 /* IBSS == TRUE */) { |
| 3103 | sm->keycount++; |
| 3104 | if (sm->keycount == 2) { |
| 3105 | wpa_auth_set_eapol(sm->wpa_auth, sm->addr, |
| 3106 | WPA_EAPOL_portValid, 1); |
| 3107 | } |
| 3108 | } else { |
| 3109 | wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portValid, |
| 3110 | 1); |
| 3111 | } |
| 3112 | wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_keyAvailable, 0); |
| 3113 | wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_keyDone, 1); |
| 3114 | if (sm->wpa == WPA_VERSION_WPA) |
| 3115 | sm->PInitAKeys = TRUE; |
| 3116 | else |
| 3117 | sm->has_GTK = TRUE; |
| 3118 | wpa_auth_vlogger(sm->wpa_auth, sm->addr, LOGGER_INFO, |
| 3119 | "pairwise key handshake completed (%s)", |
| 3120 | sm->wpa == WPA_VERSION_WPA ? "WPA" : "RSN"); |
| 3121 | |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 3122 | #ifdef CONFIG_IEEE80211R_AP |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3123 | wpa_ft_push_pmk_r1(sm->wpa_auth, sm->addr); |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 3124 | #endif /* CONFIG_IEEE80211R_AP */ |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3125 | } |
| 3126 | |
| 3127 | |
| 3128 | SM_STEP(WPA_PTK) |
| 3129 | { |
| 3130 | struct wpa_authenticator *wpa_auth = sm->wpa_auth; |
| 3131 | |
| 3132 | if (sm->Init) |
| 3133 | SM_ENTER(WPA_PTK, INITIALIZE); |
| 3134 | else if (sm->Disconnect |
| 3135 | /* || FIX: dot11RSNAConfigSALifetime timeout */) { |
| 3136 | wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG, |
| 3137 | "WPA_PTK: sm->Disconnect"); |
| 3138 | SM_ENTER(WPA_PTK, DISCONNECT); |
| 3139 | } |
| 3140 | else if (sm->DeauthenticationRequest) |
| 3141 | SM_ENTER(WPA_PTK, DISCONNECTED); |
| 3142 | else if (sm->AuthenticationRequest) |
| 3143 | SM_ENTER(WPA_PTK, AUTHENTICATION); |
| 3144 | else if (sm->ReAuthenticationRequest) |
| 3145 | SM_ENTER(WPA_PTK, AUTHENTICATION2); |
Jouni Malinen | 1420a89 | 2017-10-01 12:32:57 +0300 | [diff] [blame] | 3146 | else if (sm->PTKRequest) { |
| 3147 | if (wpa_auth_sm_ptk_update(sm) < 0) |
| 3148 | SM_ENTER(WPA_PTK, DISCONNECTED); |
| 3149 | else |
| 3150 | SM_ENTER(WPA_PTK, PTKSTART); |
| 3151 | } else switch (sm->wpa_ptk_state) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3152 | case WPA_PTK_INITIALIZE: |
| 3153 | break; |
| 3154 | case WPA_PTK_DISCONNECT: |
| 3155 | SM_ENTER(WPA_PTK, DISCONNECTED); |
| 3156 | break; |
| 3157 | case WPA_PTK_DISCONNECTED: |
| 3158 | SM_ENTER(WPA_PTK, INITIALIZE); |
| 3159 | break; |
| 3160 | case WPA_PTK_AUTHENTICATION: |
| 3161 | SM_ENTER(WPA_PTK, AUTHENTICATION2); |
| 3162 | break; |
| 3163 | case WPA_PTK_AUTHENTICATION2: |
| 3164 | if (wpa_key_mgmt_wpa_ieee8021x(sm->wpa_key_mgmt) && |
| 3165 | wpa_auth_get_eapol(sm->wpa_auth, sm->addr, |
| 3166 | WPA_EAPOL_keyRun) > 0) |
| 3167 | SM_ENTER(WPA_PTK, INITPMK); |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 3168 | else if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt) || |
| 3169 | sm->wpa_key_mgmt == WPA_KEY_MGMT_OWE |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3170 | /* FIX: && 802.1X::keyRun */) |
| 3171 | SM_ENTER(WPA_PTK, INITPSK); |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 3172 | else if (sm->wpa_key_mgmt == WPA_KEY_MGMT_DPP) |
| 3173 | SM_ENTER(WPA_PTK, INITPMK); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3174 | break; |
| 3175 | case WPA_PTK_INITPMK: |
| 3176 | if (wpa_auth_get_eapol(sm->wpa_auth, sm->addr, |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 3177 | WPA_EAPOL_keyAvailable) > 0) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3178 | SM_ENTER(WPA_PTK, PTKSTART); |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 3179 | #ifdef CONFIG_DPP |
| 3180 | } else if (sm->wpa_key_mgmt == WPA_KEY_MGMT_DPP && sm->pmksa) { |
| 3181 | SM_ENTER(WPA_PTK, PTKSTART); |
| 3182 | #endif /* CONFIG_DPP */ |
| 3183 | } else { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3184 | wpa_auth->dot11RSNA4WayHandshakeFailures++; |
| 3185 | wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_INFO, |
| 3186 | "INITPMK - keyAvailable = false"); |
| 3187 | SM_ENTER(WPA_PTK, DISCONNECT); |
| 3188 | } |
| 3189 | break; |
| 3190 | case WPA_PTK_INITPSK: |
Dmitry Shmidt | 391c59f | 2013-09-03 12:16:28 -0700 | [diff] [blame] | 3191 | if (wpa_auth_get_psk(sm->wpa_auth, sm->addr, sm->p2p_dev_addr, |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 3192 | NULL, NULL)) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3193 | SM_ENTER(WPA_PTK, PTKSTART); |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 3194 | #ifdef CONFIG_SAE |
| 3195 | } else if (wpa_auth_uses_sae(sm) && sm->pmksa) { |
| 3196 | SM_ENTER(WPA_PTK, PTKSTART); |
| 3197 | #endif /* CONFIG_SAE */ |
| 3198 | } else { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3199 | wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_INFO, |
| 3200 | "no PSK configured for the STA"); |
| 3201 | wpa_auth->dot11RSNA4WayHandshakeFailures++; |
| 3202 | SM_ENTER(WPA_PTK, DISCONNECT); |
| 3203 | } |
| 3204 | break; |
| 3205 | case WPA_PTK_PTKSTART: |
| 3206 | if (sm->EAPOLKeyReceived && !sm->EAPOLKeyRequest && |
| 3207 | sm->EAPOLKeyPairwise) |
| 3208 | SM_ENTER(WPA_PTK, PTKCALCNEGOTIATING); |
| 3209 | else if (sm->TimeoutCtr > |
Dmitry Shmidt | ebd93af | 2017-02-21 13:40:44 -0800 | [diff] [blame] | 3210 | sm->wpa_auth->conf.wpa_pairwise_update_count) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3211 | wpa_auth->dot11RSNA4WayHandshakeFailures++; |
Dmitry Shmidt | ebd93af | 2017-02-21 13:40:44 -0800 | [diff] [blame] | 3212 | wpa_auth_vlogger( |
| 3213 | sm->wpa_auth, sm->addr, LOGGER_DEBUG, |
| 3214 | "PTKSTART: Retry limit %u reached", |
| 3215 | sm->wpa_auth->conf.wpa_pairwise_update_count); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3216 | SM_ENTER(WPA_PTK, DISCONNECT); |
| 3217 | } else if (sm->TimeoutEvt) |
| 3218 | SM_ENTER(WPA_PTK, PTKSTART); |
| 3219 | break; |
| 3220 | case WPA_PTK_PTKCALCNEGOTIATING: |
| 3221 | if (sm->MICVerified) |
| 3222 | SM_ENTER(WPA_PTK, PTKCALCNEGOTIATING2); |
| 3223 | else if (sm->EAPOLKeyReceived && !sm->EAPOLKeyRequest && |
| 3224 | sm->EAPOLKeyPairwise) |
| 3225 | SM_ENTER(WPA_PTK, PTKCALCNEGOTIATING); |
| 3226 | else if (sm->TimeoutEvt) |
| 3227 | SM_ENTER(WPA_PTK, PTKSTART); |
| 3228 | break; |
| 3229 | case WPA_PTK_PTKCALCNEGOTIATING2: |
| 3230 | SM_ENTER(WPA_PTK, PTKINITNEGOTIATING); |
| 3231 | break; |
| 3232 | case WPA_PTK_PTKINITNEGOTIATING: |
Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 3233 | if (sm->update_snonce) |
| 3234 | SM_ENTER(WPA_PTK, PTKCALCNEGOTIATING); |
| 3235 | else if (sm->EAPOLKeyReceived && !sm->EAPOLKeyRequest && |
| 3236 | sm->EAPOLKeyPairwise && sm->MICVerified) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3237 | SM_ENTER(WPA_PTK, PTKINITDONE); |
| 3238 | else if (sm->TimeoutCtr > |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 3239 | sm->wpa_auth->conf.wpa_pairwise_update_count || |
| 3240 | (sm->wpa_auth->conf.wpa_disable_eapol_key_retries && |
| 3241 | sm->TimeoutCtr > 1)) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3242 | wpa_auth->dot11RSNA4WayHandshakeFailures++; |
Dmitry Shmidt | ebd93af | 2017-02-21 13:40:44 -0800 | [diff] [blame] | 3243 | wpa_auth_vlogger( |
| 3244 | sm->wpa_auth, sm->addr, LOGGER_DEBUG, |
| 3245 | "PTKINITNEGOTIATING: Retry limit %u reached", |
| 3246 | sm->wpa_auth->conf.wpa_pairwise_update_count); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3247 | SM_ENTER(WPA_PTK, DISCONNECT); |
| 3248 | } else if (sm->TimeoutEvt) |
| 3249 | SM_ENTER(WPA_PTK, PTKINITNEGOTIATING); |
| 3250 | break; |
| 3251 | case WPA_PTK_PTKINITDONE: |
| 3252 | break; |
| 3253 | } |
| 3254 | } |
| 3255 | |
| 3256 | |
| 3257 | SM_STATE(WPA_PTK_GROUP, IDLE) |
| 3258 | { |
| 3259 | SM_ENTRY_MA(WPA_PTK_GROUP, IDLE, wpa_ptk_group); |
| 3260 | if (sm->Init) { |
| 3261 | /* Init flag is not cleared here, so avoid busy |
| 3262 | * loop by claiming nothing changed. */ |
| 3263 | sm->changed = FALSE; |
| 3264 | } |
| 3265 | sm->GTimeoutCtr = 0; |
| 3266 | } |
| 3267 | |
| 3268 | |
| 3269 | SM_STATE(WPA_PTK_GROUP, REKEYNEGOTIATING) |
| 3270 | { |
| 3271 | u8 rsc[WPA_KEY_RSC_LEN]; |
| 3272 | struct wpa_group *gsm = sm->group; |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 3273 | const u8 *kde; |
| 3274 | u8 *kde_buf = NULL, *pos, hdr[2]; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3275 | size_t kde_len; |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 3276 | u8 *gtk, dummy_gtk[32]; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3277 | |
| 3278 | SM_ENTRY_MA(WPA_PTK_GROUP, REKEYNEGOTIATING, wpa_ptk_group); |
| 3279 | |
| 3280 | sm->GTimeoutCtr++; |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 3281 | if (sm->wpa_auth->conf.wpa_disable_eapol_key_retries && |
| 3282 | sm->GTimeoutCtr > 1) { |
| 3283 | /* Do not allow retransmission of EAPOL-Key group msg 1/2 */ |
| 3284 | return; |
| 3285 | } |
Dmitry Shmidt | ebd93af | 2017-02-21 13:40:44 -0800 | [diff] [blame] | 3286 | if (sm->GTimeoutCtr > sm->wpa_auth->conf.wpa_group_update_count) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3287 | /* No point in sending the EAPOL-Key - we will disconnect |
| 3288 | * immediately following this. */ |
| 3289 | return; |
| 3290 | } |
| 3291 | |
| 3292 | if (sm->wpa == WPA_VERSION_WPA) |
| 3293 | sm->PInitAKeys = FALSE; |
| 3294 | sm->TimeoutEvt = FALSE; |
| 3295 | /* Send EAPOL(1, 1, 1, !Pair, G, RSC, GNonce, MIC(PTK), GTK[GN]) */ |
| 3296 | os_memset(rsc, 0, WPA_KEY_RSC_LEN); |
| 3297 | if (gsm->wpa_group_state == WPA_GROUP_SETKEYSDONE) |
| 3298 | wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN, rsc); |
| 3299 | wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG, |
| 3300 | "sending 1/2 msg of Group Key Handshake"); |
| 3301 | |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 3302 | gtk = gsm->GTK[gsm->GN - 1]; |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame^] | 3303 | if (sm->wpa_auth->conf.disable_gtk || |
| 3304 | sm->wpa_key_mgmt == WPA_KEY_MGMT_OSEN) { |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 3305 | /* |
| 3306 | * Provide unique random GTK to each STA to prevent use |
| 3307 | * of GTK in the BSS. |
| 3308 | */ |
| 3309 | if (random_get_bytes(dummy_gtk, gsm->GTK_len) < 0) |
| 3310 | return; |
| 3311 | gtk = dummy_gtk; |
| 3312 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3313 | if (sm->wpa == WPA_VERSION_WPA2) { |
| 3314 | kde_len = 2 + RSN_SELECTOR_LEN + 2 + gsm->GTK_len + |
| 3315 | ieee80211w_kde_len(sm); |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 3316 | kde_buf = os_malloc(kde_len); |
| 3317 | if (kde_buf == NULL) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3318 | return; |
| 3319 | |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 3320 | kde = pos = kde_buf; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3321 | hdr[0] = gsm->GN & 0x03; |
| 3322 | hdr[1] = 0; |
| 3323 | pos = wpa_add_kde(pos, RSN_KEY_DATA_GROUPKEY, hdr, 2, |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 3324 | gtk, gsm->GTK_len); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3325 | pos = ieee80211w_kde_add(sm, pos); |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 3326 | kde_len = pos - kde; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3327 | } else { |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 3328 | kde = gtk; |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 3329 | kde_len = gsm->GTK_len; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3330 | } |
| 3331 | |
| 3332 | wpa_send_eapol(sm->wpa_auth, sm, |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 3333 | WPA_KEY_INFO_SECURE | |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 3334 | (wpa_mic_len(sm->wpa_key_mgmt, sm->pmk_len) ? |
| 3335 | WPA_KEY_INFO_MIC : 0) | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3336 | WPA_KEY_INFO_ACK | |
| 3337 | (!sm->Pair ? WPA_KEY_INFO_INSTALL : 0), |
Dmitry Shmidt | ebd93af | 2017-02-21 13:40:44 -0800 | [diff] [blame] | 3338 | rsc, NULL, kde, kde_len, gsm->GN, 1); |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 3339 | |
| 3340 | os_free(kde_buf); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3341 | } |
| 3342 | |
| 3343 | |
| 3344 | SM_STATE(WPA_PTK_GROUP, REKEYESTABLISHED) |
| 3345 | { |
| 3346 | SM_ENTRY_MA(WPA_PTK_GROUP, REKEYESTABLISHED, wpa_ptk_group); |
| 3347 | sm->EAPOLKeyReceived = FALSE; |
| 3348 | if (sm->GUpdateStationKeys) |
| 3349 | sm->group->GKeyDoneStations--; |
| 3350 | sm->GUpdateStationKeys = FALSE; |
| 3351 | sm->GTimeoutCtr = 0; |
| 3352 | /* FIX: MLME.SetProtection.Request(TA, Tx_Rx) */ |
| 3353 | wpa_auth_vlogger(sm->wpa_auth, sm->addr, LOGGER_INFO, |
| 3354 | "group key handshake completed (%s)", |
| 3355 | sm->wpa == WPA_VERSION_WPA ? "WPA" : "RSN"); |
| 3356 | sm->has_GTK = TRUE; |
| 3357 | } |
| 3358 | |
| 3359 | |
| 3360 | SM_STATE(WPA_PTK_GROUP, KEYERROR) |
| 3361 | { |
| 3362 | SM_ENTRY_MA(WPA_PTK_GROUP, KEYERROR, wpa_ptk_group); |
| 3363 | if (sm->GUpdateStationKeys) |
| 3364 | sm->group->GKeyDoneStations--; |
| 3365 | sm->GUpdateStationKeys = FALSE; |
| 3366 | sm->Disconnect = TRUE; |
Dmitry Shmidt | ebd93af | 2017-02-21 13:40:44 -0800 | [diff] [blame] | 3367 | wpa_auth_vlogger(sm->wpa_auth, sm->addr, LOGGER_INFO, |
| 3368 | "group key handshake failed (%s) after %u tries", |
| 3369 | sm->wpa == WPA_VERSION_WPA ? "WPA" : "RSN", |
| 3370 | sm->wpa_auth->conf.wpa_group_update_count); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3371 | } |
| 3372 | |
| 3373 | |
| 3374 | SM_STEP(WPA_PTK_GROUP) |
| 3375 | { |
| 3376 | if (sm->Init || sm->PtkGroupInit) { |
| 3377 | SM_ENTER(WPA_PTK_GROUP, IDLE); |
| 3378 | sm->PtkGroupInit = FALSE; |
| 3379 | } else switch (sm->wpa_ptk_group_state) { |
| 3380 | case WPA_PTK_GROUP_IDLE: |
| 3381 | if (sm->GUpdateStationKeys || |
| 3382 | (sm->wpa == WPA_VERSION_WPA && sm->PInitAKeys)) |
| 3383 | SM_ENTER(WPA_PTK_GROUP, REKEYNEGOTIATING); |
| 3384 | break; |
| 3385 | case WPA_PTK_GROUP_REKEYNEGOTIATING: |
| 3386 | if (sm->EAPOLKeyReceived && !sm->EAPOLKeyRequest && |
| 3387 | !sm->EAPOLKeyPairwise && sm->MICVerified) |
| 3388 | SM_ENTER(WPA_PTK_GROUP, REKEYESTABLISHED); |
| 3389 | else if (sm->GTimeoutCtr > |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 3390 | sm->wpa_auth->conf.wpa_group_update_count || |
| 3391 | (sm->wpa_auth->conf.wpa_disable_eapol_key_retries && |
| 3392 | sm->GTimeoutCtr > 1)) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3393 | SM_ENTER(WPA_PTK_GROUP, KEYERROR); |
| 3394 | else if (sm->TimeoutEvt) |
| 3395 | SM_ENTER(WPA_PTK_GROUP, REKEYNEGOTIATING); |
| 3396 | break; |
| 3397 | case WPA_PTK_GROUP_KEYERROR: |
| 3398 | SM_ENTER(WPA_PTK_GROUP, IDLE); |
| 3399 | break; |
| 3400 | case WPA_PTK_GROUP_REKEYESTABLISHED: |
| 3401 | SM_ENTER(WPA_PTK_GROUP, IDLE); |
| 3402 | break; |
| 3403 | } |
| 3404 | } |
| 3405 | |
| 3406 | |
| 3407 | static int wpa_gtk_update(struct wpa_authenticator *wpa_auth, |
| 3408 | struct wpa_group *group) |
| 3409 | { |
| 3410 | int ret = 0; |
| 3411 | |
| 3412 | os_memcpy(group->GNonce, group->Counter, WPA_NONCE_LEN); |
| 3413 | inc_byte_array(group->Counter, WPA_NONCE_LEN); |
| 3414 | if (wpa_gmk_to_gtk(group->GMK, "Group key expansion", |
| 3415 | wpa_auth->addr, group->GNonce, |
| 3416 | group->GTK[group->GN - 1], group->GTK_len) < 0) |
| 3417 | ret = -1; |
| 3418 | wpa_hexdump_key(MSG_DEBUG, "GTK", |
| 3419 | group->GTK[group->GN - 1], group->GTK_len); |
| 3420 | |
| 3421 | #ifdef CONFIG_IEEE80211W |
| 3422 | if (wpa_auth->conf.ieee80211w != NO_MGMT_FRAME_PROTECTION) { |
Dmitry Shmidt | b36ed7c | 2014-03-17 10:57:26 -0700 | [diff] [blame] | 3423 | size_t len; |
| 3424 | len = wpa_cipher_key_len(wpa_auth->conf.group_mgmt_cipher); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3425 | os_memcpy(group->GNonce, group->Counter, WPA_NONCE_LEN); |
| 3426 | inc_byte_array(group->Counter, WPA_NONCE_LEN); |
| 3427 | if (wpa_gmk_to_gtk(group->GMK, "IGTK key expansion", |
| 3428 | wpa_auth->addr, group->GNonce, |
Dmitry Shmidt | b36ed7c | 2014-03-17 10:57:26 -0700 | [diff] [blame] | 3429 | group->IGTK[group->GN_igtk - 4], len) < 0) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3430 | ret = -1; |
| 3431 | wpa_hexdump_key(MSG_DEBUG, "IGTK", |
Dmitry Shmidt | b36ed7c | 2014-03-17 10:57:26 -0700 | [diff] [blame] | 3432 | group->IGTK[group->GN_igtk - 4], len); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3433 | } |
| 3434 | #endif /* CONFIG_IEEE80211W */ |
| 3435 | |
| 3436 | return ret; |
| 3437 | } |
| 3438 | |
| 3439 | |
| 3440 | static void wpa_group_gtk_init(struct wpa_authenticator *wpa_auth, |
| 3441 | struct wpa_group *group) |
| 3442 | { |
| 3443 | wpa_printf(MSG_DEBUG, "WPA: group state machine entering state " |
| 3444 | "GTK_INIT (VLAN-ID %d)", group->vlan_id); |
| 3445 | group->changed = FALSE; /* GInit is not cleared here; avoid loop */ |
| 3446 | group->wpa_group_state = WPA_GROUP_GTK_INIT; |
| 3447 | |
| 3448 | /* GTK[0..N] = 0 */ |
| 3449 | os_memset(group->GTK, 0, sizeof(group->GTK)); |
| 3450 | group->GN = 1; |
| 3451 | group->GM = 2; |
| 3452 | #ifdef CONFIG_IEEE80211W |
| 3453 | group->GN_igtk = 4; |
| 3454 | group->GM_igtk = 5; |
| 3455 | #endif /* CONFIG_IEEE80211W */ |
| 3456 | /* GTK[GN] = CalcGTK() */ |
| 3457 | wpa_gtk_update(wpa_auth, group); |
| 3458 | } |
| 3459 | |
| 3460 | |
| 3461 | static int wpa_group_update_sta(struct wpa_state_machine *sm, void *ctx) |
| 3462 | { |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 3463 | if (ctx != NULL && ctx != sm->group) |
| 3464 | return 0; |
| 3465 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3466 | if (sm->wpa_ptk_state != WPA_PTK_PTKINITDONE) { |
| 3467 | wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG, |
| 3468 | "Not in PTKINITDONE; skip Group Key update"); |
| 3469 | sm->GUpdateStationKeys = FALSE; |
| 3470 | return 0; |
| 3471 | } |
| 3472 | if (sm->GUpdateStationKeys) { |
| 3473 | /* |
| 3474 | * This should not really happen, so add a debug log entry. |
| 3475 | * Since we clear the GKeyDoneStations before the loop, the |
| 3476 | * station needs to be counted here anyway. |
| 3477 | */ |
| 3478 | wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG, |
| 3479 | "GUpdateStationKeys was already set when " |
| 3480 | "marking station for GTK rekeying"); |
| 3481 | } |
| 3482 | |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 3483 | /* Do not rekey GTK/IGTK when STA is in WNM-Sleep Mode */ |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 3484 | if (sm->is_wnmsleep) |
| 3485 | return 0; |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 3486 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3487 | sm->group->GKeyDoneStations++; |
| 3488 | sm->GUpdateStationKeys = TRUE; |
| 3489 | |
| 3490 | wpa_sm_step(sm); |
| 3491 | return 0; |
| 3492 | } |
| 3493 | |
| 3494 | |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 3495 | #ifdef CONFIG_WNM_AP |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 3496 | /* update GTK when exiting WNM-Sleep Mode */ |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 3497 | void wpa_wnmsleep_rekey_gtk(struct wpa_state_machine *sm) |
| 3498 | { |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 3499 | if (sm == NULL || sm->is_wnmsleep) |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 3500 | return; |
| 3501 | |
| 3502 | wpa_group_update_sta(sm, NULL); |
| 3503 | } |
| 3504 | |
| 3505 | |
| 3506 | void wpa_set_wnmsleep(struct wpa_state_machine *sm, int flag) |
| 3507 | { |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 3508 | if (sm) |
| 3509 | sm->is_wnmsleep = !!flag; |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 3510 | } |
| 3511 | |
| 3512 | |
| 3513 | int wpa_wnmsleep_gtk_subelem(struct wpa_state_machine *sm, u8 *pos) |
| 3514 | { |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 3515 | struct wpa_group *gsm = sm->group; |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 3516 | u8 *start = pos; |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 3517 | |
| 3518 | /* |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 3519 | * GTK subelement: |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 3520 | * 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] | 3521 | * Key[5..32] |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 3522 | */ |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 3523 | *pos++ = WNM_SLEEP_SUBELEM_GTK; |
| 3524 | *pos++ = 11 + gsm->GTK_len; |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 3525 | /* Key ID in B0-B1 of Key Info */ |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 3526 | WPA_PUT_LE16(pos, gsm->GN & 0x03); |
| 3527 | pos += 2; |
| 3528 | *pos++ = gsm->GTK_len; |
| 3529 | 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] | 3530 | return 0; |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 3531 | pos += 8; |
| 3532 | os_memcpy(pos, gsm->GTK[gsm->GN - 1], gsm->GTK_len); |
| 3533 | pos += gsm->GTK_len; |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 3534 | |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 3535 | wpa_printf(MSG_DEBUG, "WNM: GTK Key ID %u in WNM-Sleep Mode exit", |
| 3536 | gsm->GN); |
| 3537 | wpa_hexdump_key(MSG_DEBUG, "WNM: GTK in WNM-Sleep Mode exit", |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 3538 | gsm->GTK[gsm->GN - 1], gsm->GTK_len); |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 3539 | |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 3540 | return pos - start; |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 3541 | } |
| 3542 | |
| 3543 | |
| 3544 | #ifdef CONFIG_IEEE80211W |
| 3545 | int wpa_wnmsleep_igtk_subelem(struct wpa_state_machine *sm, u8 *pos) |
| 3546 | { |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 3547 | struct wpa_group *gsm = sm->group; |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 3548 | u8 *start = pos; |
Dmitry Shmidt | b36ed7c | 2014-03-17 10:57:26 -0700 | [diff] [blame] | 3549 | size_t len = wpa_cipher_key_len(sm->wpa_auth->conf.group_mgmt_cipher); |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 3550 | |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 3551 | /* |
| 3552 | * IGTK subelement: |
| 3553 | * Sub-elem ID[1] | Length[1] | KeyID[2] | PN[6] | Key[16] |
| 3554 | */ |
| 3555 | *pos++ = WNM_SLEEP_SUBELEM_IGTK; |
Dmitry Shmidt | b36ed7c | 2014-03-17 10:57:26 -0700 | [diff] [blame] | 3556 | *pos++ = 2 + 6 + len; |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 3557 | WPA_PUT_LE16(pos, gsm->GN_igtk); |
| 3558 | pos += 2; |
| 3559 | 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] | 3560 | return 0; |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 3561 | pos += 6; |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 3562 | |
Dmitry Shmidt | b36ed7c | 2014-03-17 10:57:26 -0700 | [diff] [blame] | 3563 | os_memcpy(pos, gsm->IGTK[gsm->GN_igtk - 4], len); |
| 3564 | pos += len; |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 3565 | |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 3566 | wpa_printf(MSG_DEBUG, "WNM: IGTK Key ID %u in WNM-Sleep Mode exit", |
| 3567 | gsm->GN_igtk); |
| 3568 | wpa_hexdump_key(MSG_DEBUG, "WNM: IGTK in WNM-Sleep Mode exit", |
Dmitry Shmidt | b36ed7c | 2014-03-17 10:57:26 -0700 | [diff] [blame] | 3569 | gsm->IGTK[gsm->GN_igtk - 4], len); |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 3570 | |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 3571 | return pos - start; |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 3572 | } |
| 3573 | #endif /* CONFIG_IEEE80211W */ |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 3574 | #endif /* CONFIG_WNM_AP */ |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 3575 | |
| 3576 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3577 | static void wpa_group_setkeys(struct wpa_authenticator *wpa_auth, |
| 3578 | struct wpa_group *group) |
| 3579 | { |
| 3580 | int tmp; |
| 3581 | |
| 3582 | wpa_printf(MSG_DEBUG, "WPA: group state machine entering state " |
| 3583 | "SETKEYS (VLAN-ID %d)", group->vlan_id); |
| 3584 | group->changed = TRUE; |
| 3585 | group->wpa_group_state = WPA_GROUP_SETKEYS; |
| 3586 | group->GTKReKey = FALSE; |
| 3587 | tmp = group->GM; |
| 3588 | group->GM = group->GN; |
| 3589 | group->GN = tmp; |
| 3590 | #ifdef CONFIG_IEEE80211W |
| 3591 | tmp = group->GM_igtk; |
| 3592 | group->GM_igtk = group->GN_igtk; |
| 3593 | group->GN_igtk = tmp; |
| 3594 | #endif /* CONFIG_IEEE80211W */ |
| 3595 | /* "GKeyDoneStations = GNoStations" is done in more robust way by |
| 3596 | * counting the STAs that are marked with GUpdateStationKeys instead of |
| 3597 | * including all STAs that could be in not-yet-completed state. */ |
| 3598 | wpa_gtk_update(wpa_auth, group); |
| 3599 | |
| 3600 | if (group->GKeyDoneStations) { |
| 3601 | wpa_printf(MSG_DEBUG, "wpa_group_setkeys: Unexpected " |
| 3602 | "GKeyDoneStations=%d when starting new GTK rekey", |
| 3603 | group->GKeyDoneStations); |
| 3604 | group->GKeyDoneStations = 0; |
| 3605 | } |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 3606 | wpa_auth_for_each_sta(wpa_auth, wpa_group_update_sta, group); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3607 | wpa_printf(MSG_DEBUG, "wpa_group_setkeys: GKeyDoneStations=%d", |
| 3608 | group->GKeyDoneStations); |
| 3609 | } |
| 3610 | |
| 3611 | |
| 3612 | static int wpa_group_config_group_keys(struct wpa_authenticator *wpa_auth, |
| 3613 | struct wpa_group *group) |
| 3614 | { |
| 3615 | int ret = 0; |
| 3616 | |
| 3617 | if (wpa_auth_set_key(wpa_auth, group->vlan_id, |
Dmitry Shmidt | 4530cfd | 2012-09-09 15:20:40 -0700 | [diff] [blame] | 3618 | wpa_cipher_to_alg(wpa_auth->conf.wpa_group), |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3619 | broadcast_ether_addr, group->GN, |
| 3620 | group->GTK[group->GN - 1], group->GTK_len) < 0) |
| 3621 | ret = -1; |
| 3622 | |
| 3623 | #ifdef CONFIG_IEEE80211W |
Dmitry Shmidt | b36ed7c | 2014-03-17 10:57:26 -0700 | [diff] [blame] | 3624 | if (wpa_auth->conf.ieee80211w != NO_MGMT_FRAME_PROTECTION) { |
| 3625 | enum wpa_alg alg; |
| 3626 | size_t len; |
| 3627 | |
| 3628 | alg = wpa_cipher_to_alg(wpa_auth->conf.group_mgmt_cipher); |
| 3629 | len = wpa_cipher_key_len(wpa_auth->conf.group_mgmt_cipher); |
| 3630 | |
| 3631 | if (ret == 0 && |
| 3632 | wpa_auth_set_key(wpa_auth, group->vlan_id, alg, |
| 3633 | broadcast_ether_addr, group->GN_igtk, |
| 3634 | group->IGTK[group->GN_igtk - 4], len) < 0) |
| 3635 | ret = -1; |
| 3636 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3637 | #endif /* CONFIG_IEEE80211W */ |
| 3638 | |
| 3639 | return ret; |
| 3640 | } |
| 3641 | |
| 3642 | |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 3643 | static int wpa_group_disconnect_cb(struct wpa_state_machine *sm, void *ctx) |
| 3644 | { |
| 3645 | if (sm->group == ctx) { |
| 3646 | wpa_printf(MSG_DEBUG, "WPA: Mark STA " MACSTR |
| 3647 | " for discconnection due to fatal failure", |
| 3648 | MAC2STR(sm->addr)); |
| 3649 | sm->Disconnect = TRUE; |
| 3650 | } |
| 3651 | |
| 3652 | return 0; |
| 3653 | } |
| 3654 | |
| 3655 | |
| 3656 | static void wpa_group_fatal_failure(struct wpa_authenticator *wpa_auth, |
| 3657 | struct wpa_group *group) |
| 3658 | { |
| 3659 | wpa_printf(MSG_DEBUG, "WPA: group state machine entering state FATAL_FAILURE"); |
| 3660 | group->changed = TRUE; |
| 3661 | group->wpa_group_state = WPA_GROUP_FATAL_FAILURE; |
| 3662 | wpa_auth_for_each_sta(wpa_auth, wpa_group_disconnect_cb, group); |
| 3663 | } |
| 3664 | |
| 3665 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3666 | static int wpa_group_setkeysdone(struct wpa_authenticator *wpa_auth, |
| 3667 | struct wpa_group *group) |
| 3668 | { |
| 3669 | wpa_printf(MSG_DEBUG, "WPA: group state machine entering state " |
| 3670 | "SETKEYSDONE (VLAN-ID %d)", group->vlan_id); |
| 3671 | group->changed = TRUE; |
| 3672 | group->wpa_group_state = WPA_GROUP_SETKEYSDONE; |
| 3673 | |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 3674 | if (wpa_group_config_group_keys(wpa_auth, group) < 0) { |
| 3675 | wpa_group_fatal_failure(wpa_auth, group); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3676 | return -1; |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 3677 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3678 | |
| 3679 | return 0; |
| 3680 | } |
| 3681 | |
| 3682 | |
| 3683 | static void wpa_group_sm_step(struct wpa_authenticator *wpa_auth, |
| 3684 | struct wpa_group *group) |
| 3685 | { |
| 3686 | if (group->GInit) { |
| 3687 | wpa_group_gtk_init(wpa_auth, group); |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 3688 | } else if (group->wpa_group_state == WPA_GROUP_FATAL_FAILURE) { |
| 3689 | /* Do not allow group operations */ |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3690 | } else if (group->wpa_group_state == WPA_GROUP_GTK_INIT && |
| 3691 | group->GTKAuthenticator) { |
| 3692 | wpa_group_setkeysdone(wpa_auth, group); |
| 3693 | } else if (group->wpa_group_state == WPA_GROUP_SETKEYSDONE && |
| 3694 | group->GTKReKey) { |
| 3695 | wpa_group_setkeys(wpa_auth, group); |
| 3696 | } else if (group->wpa_group_state == WPA_GROUP_SETKEYS) { |
| 3697 | if (group->GKeyDoneStations == 0) |
| 3698 | wpa_group_setkeysdone(wpa_auth, group); |
| 3699 | else if (group->GTKReKey) |
| 3700 | wpa_group_setkeys(wpa_auth, group); |
| 3701 | } |
| 3702 | } |
| 3703 | |
| 3704 | |
| 3705 | static int wpa_sm_step(struct wpa_state_machine *sm) |
| 3706 | { |
| 3707 | if (sm == NULL) |
| 3708 | return 0; |
| 3709 | |
| 3710 | if (sm->in_step_loop) { |
| 3711 | /* This should not happen, but if it does, make sure we do not |
| 3712 | * end up freeing the state machine too early by exiting the |
| 3713 | * recursive call. */ |
| 3714 | wpa_printf(MSG_ERROR, "WPA: wpa_sm_step() called recursively"); |
| 3715 | return 0; |
| 3716 | } |
| 3717 | |
| 3718 | sm->in_step_loop = 1; |
| 3719 | do { |
| 3720 | if (sm->pending_deinit) |
| 3721 | break; |
| 3722 | |
| 3723 | sm->changed = FALSE; |
| 3724 | sm->wpa_auth->group->changed = FALSE; |
| 3725 | |
| 3726 | SM_STEP_RUN(WPA_PTK); |
| 3727 | if (sm->pending_deinit) |
| 3728 | break; |
| 3729 | SM_STEP_RUN(WPA_PTK_GROUP); |
| 3730 | if (sm->pending_deinit) |
| 3731 | break; |
| 3732 | wpa_group_sm_step(sm->wpa_auth, sm->group); |
| 3733 | } while (sm->changed || sm->wpa_auth->group->changed); |
| 3734 | sm->in_step_loop = 0; |
| 3735 | |
| 3736 | if (sm->pending_deinit) { |
| 3737 | wpa_printf(MSG_DEBUG, "WPA: Completing pending STA state " |
| 3738 | "machine deinit for " MACSTR, MAC2STR(sm->addr)); |
| 3739 | wpa_free_sta_sm(sm); |
| 3740 | return 1; |
| 3741 | } |
| 3742 | return 0; |
| 3743 | } |
| 3744 | |
| 3745 | |
| 3746 | static void wpa_sm_call_step(void *eloop_ctx, void *timeout_ctx) |
| 3747 | { |
| 3748 | struct wpa_state_machine *sm = eloop_ctx; |
| 3749 | wpa_sm_step(sm); |
| 3750 | } |
| 3751 | |
| 3752 | |
| 3753 | void wpa_auth_sm_notify(struct wpa_state_machine *sm) |
| 3754 | { |
| 3755 | if (sm == NULL) |
| 3756 | return; |
| 3757 | eloop_register_timeout(0, 0, wpa_sm_call_step, sm, NULL); |
| 3758 | } |
| 3759 | |
| 3760 | |
| 3761 | void wpa_gtk_rekey(struct wpa_authenticator *wpa_auth) |
| 3762 | { |
| 3763 | int tmp, i; |
| 3764 | struct wpa_group *group; |
| 3765 | |
| 3766 | if (wpa_auth == NULL) |
| 3767 | return; |
| 3768 | |
| 3769 | group = wpa_auth->group; |
| 3770 | |
| 3771 | for (i = 0; i < 2; i++) { |
| 3772 | tmp = group->GM; |
| 3773 | group->GM = group->GN; |
| 3774 | group->GN = tmp; |
| 3775 | #ifdef CONFIG_IEEE80211W |
| 3776 | tmp = group->GM_igtk; |
| 3777 | group->GM_igtk = group->GN_igtk; |
| 3778 | group->GN_igtk = tmp; |
| 3779 | #endif /* CONFIG_IEEE80211W */ |
| 3780 | wpa_gtk_update(wpa_auth, group); |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 3781 | wpa_group_config_group_keys(wpa_auth, group); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3782 | } |
| 3783 | } |
| 3784 | |
| 3785 | |
Dmitry Shmidt | 1d755d0 | 2015-04-28 10:34:29 -0700 | [diff] [blame] | 3786 | static const char * wpa_bool_txt(int val) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3787 | { |
Dmitry Shmidt | 1d755d0 | 2015-04-28 10:34:29 -0700 | [diff] [blame] | 3788 | return val ? "TRUE" : "FALSE"; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3789 | } |
| 3790 | |
| 3791 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3792 | #define RSN_SUITE "%02x-%02x-%02x-%d" |
| 3793 | #define RSN_SUITE_ARG(s) \ |
| 3794 | ((s) >> 24) & 0xff, ((s) >> 16) & 0xff, ((s) >> 8) & 0xff, (s) & 0xff |
| 3795 | |
| 3796 | int wpa_get_mib(struct wpa_authenticator *wpa_auth, char *buf, size_t buflen) |
| 3797 | { |
| 3798 | int len = 0, ret; |
| 3799 | char pmkid_txt[PMKID_LEN * 2 + 1]; |
| 3800 | #ifdef CONFIG_RSN_PREAUTH |
| 3801 | const int preauth = 1; |
| 3802 | #else /* CONFIG_RSN_PREAUTH */ |
| 3803 | const int preauth = 0; |
| 3804 | #endif /* CONFIG_RSN_PREAUTH */ |
| 3805 | |
| 3806 | if (wpa_auth == NULL) |
| 3807 | return len; |
| 3808 | |
| 3809 | ret = os_snprintf(buf + len, buflen - len, |
| 3810 | "dot11RSNAOptionImplemented=TRUE\n" |
| 3811 | "dot11RSNAPreauthenticationImplemented=%s\n" |
| 3812 | "dot11RSNAEnabled=%s\n" |
| 3813 | "dot11RSNAPreauthenticationEnabled=%s\n", |
| 3814 | wpa_bool_txt(preauth), |
| 3815 | wpa_bool_txt(wpa_auth->conf.wpa & WPA_PROTO_RSN), |
| 3816 | wpa_bool_txt(wpa_auth->conf.rsn_preauth)); |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 3817 | if (os_snprintf_error(buflen - len, ret)) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3818 | return len; |
| 3819 | len += ret; |
| 3820 | |
| 3821 | wpa_snprintf_hex(pmkid_txt, sizeof(pmkid_txt), |
| 3822 | wpa_auth->dot11RSNAPMKIDUsed, PMKID_LEN); |
| 3823 | |
| 3824 | ret = os_snprintf( |
| 3825 | buf + len, buflen - len, |
| 3826 | "dot11RSNAConfigVersion=%u\n" |
| 3827 | "dot11RSNAConfigPairwiseKeysSupported=9999\n" |
| 3828 | /* FIX: dot11RSNAConfigGroupCipher */ |
| 3829 | /* FIX: dot11RSNAConfigGroupRekeyMethod */ |
| 3830 | /* FIX: dot11RSNAConfigGroupRekeyTime */ |
| 3831 | /* FIX: dot11RSNAConfigGroupRekeyPackets */ |
| 3832 | "dot11RSNAConfigGroupRekeyStrict=%u\n" |
| 3833 | "dot11RSNAConfigGroupUpdateCount=%u\n" |
| 3834 | "dot11RSNAConfigPairwiseUpdateCount=%u\n" |
| 3835 | "dot11RSNAConfigGroupCipherSize=%u\n" |
| 3836 | "dot11RSNAConfigPMKLifetime=%u\n" |
| 3837 | "dot11RSNAConfigPMKReauthThreshold=%u\n" |
| 3838 | "dot11RSNAConfigNumberOfPTKSAReplayCounters=0\n" |
| 3839 | "dot11RSNAConfigSATimeout=%u\n" |
| 3840 | "dot11RSNAAuthenticationSuiteSelected=" RSN_SUITE "\n" |
| 3841 | "dot11RSNAPairwiseCipherSelected=" RSN_SUITE "\n" |
| 3842 | "dot11RSNAGroupCipherSelected=" RSN_SUITE "\n" |
| 3843 | "dot11RSNAPMKIDUsed=%s\n" |
| 3844 | "dot11RSNAAuthenticationSuiteRequested=" RSN_SUITE "\n" |
| 3845 | "dot11RSNAPairwiseCipherRequested=" RSN_SUITE "\n" |
| 3846 | "dot11RSNAGroupCipherRequested=" RSN_SUITE "\n" |
| 3847 | "dot11RSNATKIPCounterMeasuresInvoked=%u\n" |
| 3848 | "dot11RSNA4WayHandshakeFailures=%u\n" |
| 3849 | "dot11RSNAConfigNumberOfGTKSAReplayCounters=0\n", |
| 3850 | RSN_VERSION, |
| 3851 | !!wpa_auth->conf.wpa_strict_rekey, |
Dmitry Shmidt | ebd93af | 2017-02-21 13:40:44 -0800 | [diff] [blame] | 3852 | wpa_auth->conf.wpa_group_update_count, |
| 3853 | wpa_auth->conf.wpa_pairwise_update_count, |
Dmitry Shmidt | 4530cfd | 2012-09-09 15:20:40 -0700 | [diff] [blame] | 3854 | wpa_cipher_key_len(wpa_auth->conf.wpa_group) * 8, |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3855 | dot11RSNAConfigPMKLifetime, |
| 3856 | dot11RSNAConfigPMKReauthThreshold, |
| 3857 | dot11RSNAConfigSATimeout, |
| 3858 | RSN_SUITE_ARG(wpa_auth->dot11RSNAAuthenticationSuiteSelected), |
| 3859 | RSN_SUITE_ARG(wpa_auth->dot11RSNAPairwiseCipherSelected), |
| 3860 | RSN_SUITE_ARG(wpa_auth->dot11RSNAGroupCipherSelected), |
| 3861 | pmkid_txt, |
| 3862 | RSN_SUITE_ARG(wpa_auth->dot11RSNAAuthenticationSuiteRequested), |
| 3863 | RSN_SUITE_ARG(wpa_auth->dot11RSNAPairwiseCipherRequested), |
| 3864 | RSN_SUITE_ARG(wpa_auth->dot11RSNAGroupCipherRequested), |
| 3865 | wpa_auth->dot11RSNATKIPCounterMeasuresInvoked, |
| 3866 | wpa_auth->dot11RSNA4WayHandshakeFailures); |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 3867 | if (os_snprintf_error(buflen - len, ret)) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3868 | return len; |
| 3869 | len += ret; |
| 3870 | |
| 3871 | /* TODO: dot11RSNAConfigPairwiseCiphersTable */ |
| 3872 | /* TODO: dot11RSNAConfigAuthenticationSuitesTable */ |
| 3873 | |
| 3874 | /* Private MIB */ |
| 3875 | ret = os_snprintf(buf + len, buflen - len, "hostapdWPAGroupState=%d\n", |
| 3876 | wpa_auth->group->wpa_group_state); |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 3877 | if (os_snprintf_error(buflen - len, ret)) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3878 | return len; |
| 3879 | len += ret; |
| 3880 | |
| 3881 | return len; |
| 3882 | } |
| 3883 | |
| 3884 | |
| 3885 | int wpa_get_mib_sta(struct wpa_state_machine *sm, char *buf, size_t buflen) |
| 3886 | { |
| 3887 | int len = 0, ret; |
| 3888 | u32 pairwise = 0; |
| 3889 | |
| 3890 | if (sm == NULL) |
| 3891 | return 0; |
| 3892 | |
| 3893 | /* TODO: FF-FF-FF-FF-FF-FF entry for broadcast/multicast stats */ |
| 3894 | |
| 3895 | /* dot11RSNAStatsEntry */ |
| 3896 | |
Dmitry Shmidt | 4530cfd | 2012-09-09 15:20:40 -0700 | [diff] [blame] | 3897 | pairwise = wpa_cipher_to_suite(sm->wpa == WPA_VERSION_WPA2 ? |
| 3898 | WPA_PROTO_RSN : WPA_PROTO_WPA, |
| 3899 | sm->pairwise); |
| 3900 | if (pairwise == 0) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3901 | return 0; |
| 3902 | |
| 3903 | ret = os_snprintf( |
| 3904 | buf + len, buflen - len, |
| 3905 | /* TODO: dot11RSNAStatsIndex */ |
| 3906 | "dot11RSNAStatsSTAAddress=" MACSTR "\n" |
| 3907 | "dot11RSNAStatsVersion=1\n" |
| 3908 | "dot11RSNAStatsSelectedPairwiseCipher=" RSN_SUITE "\n" |
| 3909 | /* TODO: dot11RSNAStatsTKIPICVErrors */ |
| 3910 | "dot11RSNAStatsTKIPLocalMICFailures=%u\n" |
| 3911 | "dot11RSNAStatsTKIPRemoteMICFailures=%u\n" |
| 3912 | /* TODO: dot11RSNAStatsCCMPReplays */ |
| 3913 | /* TODO: dot11RSNAStatsCCMPDecryptErrors */ |
| 3914 | /* TODO: dot11RSNAStatsTKIPReplays */, |
| 3915 | MAC2STR(sm->addr), |
| 3916 | RSN_SUITE_ARG(pairwise), |
| 3917 | sm->dot11RSNAStatsTKIPLocalMICFailures, |
| 3918 | sm->dot11RSNAStatsTKIPRemoteMICFailures); |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 3919 | if (os_snprintf_error(buflen - len, ret)) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3920 | return len; |
| 3921 | len += ret; |
| 3922 | |
| 3923 | /* Private MIB */ |
| 3924 | ret = os_snprintf(buf + len, buflen - len, |
| 3925 | "hostapdWPAPTKState=%d\n" |
| 3926 | "hostapdWPAPTKGroupState=%d\n", |
| 3927 | sm->wpa_ptk_state, |
| 3928 | sm->wpa_ptk_group_state); |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 3929 | if (os_snprintf_error(buflen - len, ret)) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3930 | return len; |
| 3931 | len += ret; |
| 3932 | |
| 3933 | return len; |
| 3934 | } |
| 3935 | |
| 3936 | |
| 3937 | void wpa_auth_countermeasures_start(struct wpa_authenticator *wpa_auth) |
| 3938 | { |
| 3939 | if (wpa_auth) |
| 3940 | wpa_auth->dot11RSNATKIPCounterMeasuresInvoked++; |
| 3941 | } |
| 3942 | |
| 3943 | |
| 3944 | int wpa_auth_pairwise_set(struct wpa_state_machine *sm) |
| 3945 | { |
| 3946 | return sm && sm->pairwise_set; |
| 3947 | } |
| 3948 | |
| 3949 | |
| 3950 | int wpa_auth_get_pairwise(struct wpa_state_machine *sm) |
| 3951 | { |
| 3952 | return sm->pairwise; |
| 3953 | } |
| 3954 | |
| 3955 | |
| 3956 | int wpa_auth_sta_key_mgmt(struct wpa_state_machine *sm) |
| 3957 | { |
| 3958 | if (sm == NULL) |
| 3959 | return -1; |
| 3960 | return sm->wpa_key_mgmt; |
| 3961 | } |
| 3962 | |
| 3963 | |
| 3964 | int wpa_auth_sta_wpa_version(struct wpa_state_machine *sm) |
| 3965 | { |
| 3966 | if (sm == NULL) |
| 3967 | return 0; |
| 3968 | return sm->wpa; |
| 3969 | } |
| 3970 | |
| 3971 | |
Mathy Vanhoef | f6e1f66 | 2017-07-14 15:15:35 +0200 | [diff] [blame] | 3972 | int wpa_auth_sta_ft_tk_already_set(struct wpa_state_machine *sm) |
| 3973 | { |
| 3974 | if (!sm || !wpa_key_mgmt_ft(sm->wpa_key_mgmt)) |
| 3975 | return 0; |
| 3976 | return sm->tk_already_set; |
| 3977 | } |
| 3978 | |
| 3979 | |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 3980 | int wpa_auth_sta_fils_tk_already_set(struct wpa_state_machine *sm) |
| 3981 | { |
| 3982 | if (!sm || !wpa_key_mgmt_fils(sm->wpa_key_mgmt)) |
| 3983 | return 0; |
| 3984 | return sm->tk_already_set; |
| 3985 | } |
| 3986 | |
| 3987 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 3988 | int wpa_auth_sta_clear_pmksa(struct wpa_state_machine *sm, |
| 3989 | struct rsn_pmksa_cache_entry *entry) |
| 3990 | { |
| 3991 | if (sm == NULL || sm->pmksa != entry) |
| 3992 | return -1; |
| 3993 | sm->pmksa = NULL; |
| 3994 | return 0; |
| 3995 | } |
| 3996 | |
| 3997 | |
| 3998 | struct rsn_pmksa_cache_entry * |
| 3999 | wpa_auth_sta_get_pmksa(struct wpa_state_machine *sm) |
| 4000 | { |
| 4001 | return sm ? sm->pmksa : NULL; |
| 4002 | } |
| 4003 | |
| 4004 | |
| 4005 | void wpa_auth_sta_local_mic_failure_report(struct wpa_state_machine *sm) |
| 4006 | { |
| 4007 | if (sm) |
| 4008 | sm->dot11RSNAStatsTKIPLocalMICFailures++; |
| 4009 | } |
| 4010 | |
| 4011 | |
| 4012 | const u8 * wpa_auth_get_wpa_ie(struct wpa_authenticator *wpa_auth, size_t *len) |
| 4013 | { |
| 4014 | if (wpa_auth == NULL) |
| 4015 | return NULL; |
| 4016 | *len = wpa_auth->wpa_ie_len; |
| 4017 | return wpa_auth->wpa_ie; |
| 4018 | } |
| 4019 | |
| 4020 | |
| 4021 | int wpa_auth_pmksa_add(struct wpa_state_machine *sm, const u8 *pmk, |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 4022 | unsigned int pmk_len, |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4023 | int session_timeout, struct eapol_state_machine *eapol) |
| 4024 | { |
Dmitry Shmidt | c55524a | 2011-07-07 11:18:38 -0700 | [diff] [blame] | 4025 | if (sm == NULL || sm->wpa != WPA_VERSION_WPA2 || |
| 4026 | sm->wpa_auth->conf.disable_pmksa_caching) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4027 | return -1; |
| 4028 | |
Dmitry Shmidt | ebd93af | 2017-02-21 13:40:44 -0800 | [diff] [blame] | 4029 | if (wpa_key_mgmt_sha384(sm->wpa_key_mgmt)) { |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 4030 | if (pmk_len > PMK_LEN_SUITE_B_192) |
| 4031 | pmk_len = PMK_LEN_SUITE_B_192; |
| 4032 | } else if (pmk_len > PMK_LEN) { |
| 4033 | pmk_len = PMK_LEN; |
| 4034 | } |
| 4035 | |
Dmitry Shmidt | 57c2d39 | 2016-02-23 13:40:19 -0800 | [diff] [blame] | 4036 | if (pmksa_cache_auth_add(sm->wpa_auth->pmksa, pmk, pmk_len, NULL, |
Dmitry Shmidt | 807291d | 2015-01-27 13:40:23 -0800 | [diff] [blame] | 4037 | sm->PTK.kck, sm->PTK.kck_len, |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4038 | sm->wpa_auth->addr, sm->addr, session_timeout, |
| 4039 | eapol, sm->wpa_key_mgmt)) |
| 4040 | return 0; |
| 4041 | |
| 4042 | return -1; |
| 4043 | } |
| 4044 | |
| 4045 | |
| 4046 | int wpa_auth_pmksa_add_preauth(struct wpa_authenticator *wpa_auth, |
| 4047 | const u8 *pmk, size_t len, const u8 *sta_addr, |
| 4048 | int session_timeout, |
| 4049 | struct eapol_state_machine *eapol) |
| 4050 | { |
| 4051 | if (wpa_auth == NULL) |
| 4052 | return -1; |
| 4053 | |
Dmitry Shmidt | 57c2d39 | 2016-02-23 13:40:19 -0800 | [diff] [blame] | 4054 | if (pmksa_cache_auth_add(wpa_auth->pmksa, pmk, len, NULL, |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 4055 | NULL, 0, |
| 4056 | wpa_auth->addr, |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4057 | sta_addr, session_timeout, eapol, |
| 4058 | WPA_KEY_MGMT_IEEE8021X)) |
| 4059 | return 0; |
| 4060 | |
| 4061 | return -1; |
| 4062 | } |
| 4063 | |
| 4064 | |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 4065 | int wpa_auth_pmksa_add_sae(struct wpa_authenticator *wpa_auth, const u8 *addr, |
Dmitry Shmidt | 57c2d39 | 2016-02-23 13:40:19 -0800 | [diff] [blame] | 4066 | const u8 *pmk, const u8 *pmkid) |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 4067 | { |
| 4068 | if (wpa_auth->conf.disable_pmksa_caching) |
| 4069 | return -1; |
| 4070 | |
Dmitry Shmidt | 57c2d39 | 2016-02-23 13:40:19 -0800 | [diff] [blame] | 4071 | if (pmksa_cache_auth_add(wpa_auth->pmksa, pmk, PMK_LEN, pmkid, |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 4072 | NULL, 0, |
| 4073 | wpa_auth->addr, addr, 0, NULL, |
| 4074 | WPA_KEY_MGMT_SAE)) |
| 4075 | return 0; |
| 4076 | |
| 4077 | return -1; |
| 4078 | } |
| 4079 | |
| 4080 | |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame^] | 4081 | void wpa_auth_add_sae_pmkid(struct wpa_state_machine *sm, const u8 *pmkid) |
| 4082 | { |
| 4083 | os_memcpy(sm->pmkid, pmkid, PMKID_LEN); |
| 4084 | sm->pmkid_set = 1; |
| 4085 | } |
| 4086 | |
| 4087 | |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 4088 | int wpa_auth_pmksa_add2(struct wpa_authenticator *wpa_auth, const u8 *addr, |
| 4089 | const u8 *pmk, size_t pmk_len, const u8 *pmkid, |
| 4090 | int session_timeout, int akmp) |
| 4091 | { |
| 4092 | if (wpa_auth->conf.disable_pmksa_caching) |
| 4093 | return -1; |
| 4094 | |
| 4095 | if (pmksa_cache_auth_add(wpa_auth->pmksa, pmk, pmk_len, pmkid, |
| 4096 | NULL, 0, wpa_auth->addr, addr, session_timeout, |
| 4097 | NULL, akmp)) |
| 4098 | return 0; |
| 4099 | |
| 4100 | return -1; |
| 4101 | } |
| 4102 | |
| 4103 | |
Dmitry Shmidt | f7e0a99 | 2013-05-23 11:03:10 -0700 | [diff] [blame] | 4104 | void wpa_auth_pmksa_remove(struct wpa_authenticator *wpa_auth, |
| 4105 | const u8 *sta_addr) |
| 4106 | { |
| 4107 | struct rsn_pmksa_cache_entry *pmksa; |
| 4108 | |
| 4109 | if (wpa_auth == NULL || wpa_auth->pmksa == NULL) |
| 4110 | return; |
| 4111 | pmksa = pmksa_cache_auth_get(wpa_auth->pmksa, sta_addr, NULL); |
| 4112 | if (pmksa) { |
| 4113 | wpa_printf(MSG_DEBUG, "WPA: Remove PMKSA cache entry for " |
| 4114 | MACSTR " based on request", MAC2STR(sta_addr)); |
| 4115 | pmksa_cache_free_entry(wpa_auth->pmksa, pmksa); |
| 4116 | } |
| 4117 | } |
| 4118 | |
| 4119 | |
Dmitry Shmidt | e466304 | 2016-04-04 10:07:49 -0700 | [diff] [blame] | 4120 | int wpa_auth_pmksa_list(struct wpa_authenticator *wpa_auth, char *buf, |
| 4121 | size_t len) |
| 4122 | { |
| 4123 | if (!wpa_auth || !wpa_auth->pmksa) |
| 4124 | return 0; |
| 4125 | return pmksa_cache_auth_list(wpa_auth->pmksa, buf, len); |
| 4126 | } |
| 4127 | |
| 4128 | |
| 4129 | void wpa_auth_pmksa_flush(struct wpa_authenticator *wpa_auth) |
| 4130 | { |
| 4131 | if (wpa_auth && wpa_auth->pmksa) |
| 4132 | pmksa_cache_auth_flush(wpa_auth->pmksa); |
| 4133 | } |
| 4134 | |
| 4135 | |
Paul Stewart | 092955c | 2017-02-06 09:13:09 -0800 | [diff] [blame] | 4136 | #ifdef CONFIG_PMKSA_CACHE_EXTERNAL |
| 4137 | #ifdef CONFIG_MESH |
| 4138 | |
| 4139 | int wpa_auth_pmksa_list_mesh(struct wpa_authenticator *wpa_auth, const u8 *addr, |
| 4140 | char *buf, size_t len) |
| 4141 | { |
| 4142 | if (!wpa_auth || !wpa_auth->pmksa) |
| 4143 | return 0; |
| 4144 | |
| 4145 | return pmksa_cache_auth_list_mesh(wpa_auth->pmksa, addr, buf, len); |
| 4146 | } |
| 4147 | |
| 4148 | |
| 4149 | struct rsn_pmksa_cache_entry * |
| 4150 | wpa_auth_pmksa_create_entry(const u8 *aa, const u8 *spa, const u8 *pmk, |
| 4151 | const u8 *pmkid, int expiration) |
| 4152 | { |
| 4153 | struct rsn_pmksa_cache_entry *entry; |
| 4154 | struct os_reltime now; |
| 4155 | |
| 4156 | entry = pmksa_cache_auth_create_entry(pmk, PMK_LEN, pmkid, NULL, 0, aa, |
| 4157 | spa, 0, NULL, WPA_KEY_MGMT_SAE); |
| 4158 | if (!entry) |
| 4159 | return NULL; |
| 4160 | |
| 4161 | os_get_reltime(&now); |
| 4162 | entry->expiration = now.sec + expiration; |
| 4163 | return entry; |
| 4164 | } |
| 4165 | |
| 4166 | |
| 4167 | int wpa_auth_pmksa_add_entry(struct wpa_authenticator *wpa_auth, |
| 4168 | struct rsn_pmksa_cache_entry *entry) |
| 4169 | { |
| 4170 | int ret; |
| 4171 | |
| 4172 | if (!wpa_auth || !wpa_auth->pmksa) |
| 4173 | return -1; |
| 4174 | |
| 4175 | ret = pmksa_cache_auth_add_entry(wpa_auth->pmksa, entry); |
| 4176 | if (ret < 0) |
| 4177 | wpa_printf(MSG_DEBUG, |
| 4178 | "RSN: Failed to store external PMKSA cache for " |
| 4179 | MACSTR, MAC2STR(entry->spa)); |
| 4180 | |
| 4181 | return ret; |
| 4182 | } |
| 4183 | |
| 4184 | #endif /* CONFIG_MESH */ |
| 4185 | #endif /* CONFIG_PMKSA_CACHE_EXTERNAL */ |
| 4186 | |
| 4187 | |
Dmitry Shmidt | e466304 | 2016-04-04 10:07:49 -0700 | [diff] [blame] | 4188 | struct rsn_pmksa_cache_entry * |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 4189 | wpa_auth_pmksa_get(struct wpa_authenticator *wpa_auth, const u8 *sta_addr, |
| 4190 | const u8 *pmkid) |
Dmitry Shmidt | e466304 | 2016-04-04 10:07:49 -0700 | [diff] [blame] | 4191 | { |
| 4192 | if (!wpa_auth || !wpa_auth->pmksa) |
| 4193 | return NULL; |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 4194 | return pmksa_cache_auth_get(wpa_auth->pmksa, sta_addr, pmkid); |
Dmitry Shmidt | e466304 | 2016-04-04 10:07:49 -0700 | [diff] [blame] | 4195 | } |
| 4196 | |
| 4197 | |
| 4198 | void wpa_auth_pmksa_set_to_sm(struct rsn_pmksa_cache_entry *pmksa, |
| 4199 | struct wpa_state_machine *sm, |
| 4200 | struct wpa_authenticator *wpa_auth, |
| 4201 | u8 *pmkid, u8 *pmk) |
| 4202 | { |
| 4203 | if (!sm) |
| 4204 | return; |
| 4205 | |
| 4206 | sm->pmksa = pmksa; |
| 4207 | os_memcpy(pmk, pmksa->pmk, PMK_LEN); |
| 4208 | os_memcpy(pmkid, pmksa->pmkid, PMKID_LEN); |
| 4209 | os_memcpy(wpa_auth->dot11RSNAPMKIDUsed, pmksa->pmkid, PMKID_LEN); |
| 4210 | } |
| 4211 | |
| 4212 | |
Dmitry Shmidt | 1d755d0 | 2015-04-28 10:34:29 -0700 | [diff] [blame] | 4213 | /* |
| 4214 | * Remove and free the group from wpa_authenticator. This is triggered by a |
| 4215 | * callback to make sure nobody is currently iterating the group list while it |
| 4216 | * gets modified. |
| 4217 | */ |
| 4218 | static void wpa_group_free(struct wpa_authenticator *wpa_auth, |
| 4219 | struct wpa_group *group) |
| 4220 | { |
| 4221 | struct wpa_group *prev = wpa_auth->group; |
| 4222 | |
| 4223 | wpa_printf(MSG_DEBUG, "WPA: Remove group state machine for VLAN-ID %d", |
| 4224 | group->vlan_id); |
| 4225 | |
| 4226 | while (prev) { |
| 4227 | if (prev->next == group) { |
| 4228 | /* This never frees the special first group as needed */ |
| 4229 | prev->next = group->next; |
| 4230 | os_free(group); |
| 4231 | break; |
| 4232 | } |
| 4233 | prev = prev->next; |
| 4234 | } |
| 4235 | |
| 4236 | } |
| 4237 | |
| 4238 | |
| 4239 | /* Increase the reference counter for group */ |
| 4240 | static void wpa_group_get(struct wpa_authenticator *wpa_auth, |
| 4241 | struct wpa_group *group) |
| 4242 | { |
| 4243 | /* Skip the special first group */ |
| 4244 | if (wpa_auth->group == group) |
| 4245 | return; |
| 4246 | |
| 4247 | group->references++; |
| 4248 | } |
| 4249 | |
| 4250 | |
| 4251 | /* Decrease the reference counter and maybe free the group */ |
| 4252 | static void wpa_group_put(struct wpa_authenticator *wpa_auth, |
| 4253 | struct wpa_group *group) |
| 4254 | { |
| 4255 | /* Skip the special first group */ |
| 4256 | if (wpa_auth->group == group) |
| 4257 | return; |
| 4258 | |
| 4259 | group->references--; |
| 4260 | if (group->references) |
| 4261 | return; |
| 4262 | wpa_group_free(wpa_auth, group); |
| 4263 | } |
| 4264 | |
| 4265 | |
| 4266 | /* |
| 4267 | * Add a group that has its references counter set to zero. Caller needs to |
| 4268 | * call wpa_group_get() on the return value to mark the entry in use. |
| 4269 | */ |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4270 | static struct wpa_group * |
| 4271 | wpa_auth_add_group(struct wpa_authenticator *wpa_auth, int vlan_id) |
| 4272 | { |
| 4273 | struct wpa_group *group; |
| 4274 | |
| 4275 | if (wpa_auth == NULL || wpa_auth->group == NULL) |
| 4276 | return NULL; |
| 4277 | |
| 4278 | wpa_printf(MSG_DEBUG, "WPA: Add group state machine for VLAN-ID %d", |
| 4279 | vlan_id); |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 4280 | group = wpa_group_init(wpa_auth, vlan_id, 0); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4281 | if (group == NULL) |
| 4282 | return NULL; |
| 4283 | |
| 4284 | group->next = wpa_auth->group->next; |
| 4285 | wpa_auth->group->next = group; |
| 4286 | |
| 4287 | return group; |
| 4288 | } |
| 4289 | |
| 4290 | |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 4291 | /* |
| 4292 | * Enforce that the group state machine for the VLAN is running, increase |
| 4293 | * reference counter as interface is up. References might have been increased |
| 4294 | * even if a negative value is returned. |
| 4295 | * Returns: -1 on error (group missing, group already failed); otherwise, 0 |
| 4296 | */ |
| 4297 | int wpa_auth_ensure_group(struct wpa_authenticator *wpa_auth, int vlan_id) |
| 4298 | { |
| 4299 | struct wpa_group *group; |
| 4300 | |
| 4301 | if (wpa_auth == NULL) |
| 4302 | return 0; |
| 4303 | |
| 4304 | group = wpa_auth->group; |
| 4305 | while (group) { |
| 4306 | if (group->vlan_id == vlan_id) |
| 4307 | break; |
| 4308 | group = group->next; |
| 4309 | } |
| 4310 | |
| 4311 | if (group == NULL) { |
| 4312 | group = wpa_auth_add_group(wpa_auth, vlan_id); |
| 4313 | if (group == NULL) |
| 4314 | return -1; |
| 4315 | } |
| 4316 | |
| 4317 | wpa_printf(MSG_DEBUG, |
| 4318 | "WPA: Ensure group state machine running for VLAN ID %d", |
| 4319 | vlan_id); |
| 4320 | |
| 4321 | wpa_group_get(wpa_auth, group); |
| 4322 | group->num_setup_iface++; |
| 4323 | |
| 4324 | if (group->wpa_group_state == WPA_GROUP_FATAL_FAILURE) |
| 4325 | return -1; |
| 4326 | |
| 4327 | return 0; |
| 4328 | } |
| 4329 | |
| 4330 | |
| 4331 | /* |
| 4332 | * Decrease reference counter, expected to be zero afterwards. |
| 4333 | * returns: -1 on error (group not found, group in fail state) |
| 4334 | * -2 if wpa_group is still referenced |
| 4335 | * 0 else |
| 4336 | */ |
| 4337 | int wpa_auth_release_group(struct wpa_authenticator *wpa_auth, int vlan_id) |
| 4338 | { |
| 4339 | struct wpa_group *group; |
| 4340 | int ret = 0; |
| 4341 | |
| 4342 | if (wpa_auth == NULL) |
| 4343 | return 0; |
| 4344 | |
| 4345 | group = wpa_auth->group; |
| 4346 | while (group) { |
| 4347 | if (group->vlan_id == vlan_id) |
| 4348 | break; |
| 4349 | group = group->next; |
| 4350 | } |
| 4351 | |
| 4352 | if (group == NULL) |
| 4353 | return -1; |
| 4354 | |
| 4355 | wpa_printf(MSG_DEBUG, |
| 4356 | "WPA: Try stopping group state machine for VLAN ID %d", |
| 4357 | vlan_id); |
| 4358 | |
| 4359 | if (group->num_setup_iface <= 0) { |
| 4360 | wpa_printf(MSG_ERROR, |
| 4361 | "WPA: wpa_auth_release_group called more often than wpa_auth_ensure_group for VLAN ID %d, skipping.", |
| 4362 | vlan_id); |
| 4363 | return -1; |
| 4364 | } |
| 4365 | group->num_setup_iface--; |
| 4366 | |
| 4367 | if (group->wpa_group_state == WPA_GROUP_FATAL_FAILURE) |
| 4368 | ret = -1; |
| 4369 | |
| 4370 | if (group->references > 1) { |
| 4371 | wpa_printf(MSG_DEBUG, |
| 4372 | "WPA: Cannot stop group state machine for VLAN ID %d as references are still hold", |
| 4373 | vlan_id); |
| 4374 | ret = -2; |
| 4375 | } |
| 4376 | |
| 4377 | wpa_group_put(wpa_auth, group); |
| 4378 | |
| 4379 | return ret; |
| 4380 | } |
| 4381 | |
| 4382 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4383 | int wpa_auth_sta_set_vlan(struct wpa_state_machine *sm, int vlan_id) |
| 4384 | { |
| 4385 | struct wpa_group *group; |
| 4386 | |
| 4387 | if (sm == NULL || sm->wpa_auth == NULL) |
| 4388 | return 0; |
| 4389 | |
| 4390 | group = sm->wpa_auth->group; |
| 4391 | while (group) { |
| 4392 | if (group->vlan_id == vlan_id) |
| 4393 | break; |
| 4394 | group = group->next; |
| 4395 | } |
| 4396 | |
| 4397 | if (group == NULL) { |
| 4398 | group = wpa_auth_add_group(sm->wpa_auth, vlan_id); |
| 4399 | if (group == NULL) |
| 4400 | return -1; |
| 4401 | } |
| 4402 | |
| 4403 | if (sm->group == group) |
| 4404 | return 0; |
| 4405 | |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 4406 | if (group->wpa_group_state == WPA_GROUP_FATAL_FAILURE) |
| 4407 | return -1; |
| 4408 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4409 | wpa_printf(MSG_DEBUG, "WPA: Moving STA " MACSTR " to use group state " |
| 4410 | "machine for VLAN ID %d", MAC2STR(sm->addr), vlan_id); |
| 4411 | |
Dmitry Shmidt | 1d755d0 | 2015-04-28 10:34:29 -0700 | [diff] [blame] | 4412 | wpa_group_get(sm->wpa_auth, group); |
| 4413 | wpa_group_put(sm->wpa_auth, sm->group); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4414 | sm->group = group; |
Dmitry Shmidt | 1d755d0 | 2015-04-28 10:34:29 -0700 | [diff] [blame] | 4415 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4416 | return 0; |
| 4417 | } |
| 4418 | |
| 4419 | |
| 4420 | void wpa_auth_eapol_key_tx_status(struct wpa_authenticator *wpa_auth, |
| 4421 | struct wpa_state_machine *sm, int ack) |
| 4422 | { |
| 4423 | if (wpa_auth == NULL || sm == NULL) |
| 4424 | return; |
| 4425 | wpa_printf(MSG_DEBUG, "WPA: EAPOL-Key TX status for STA " MACSTR |
| 4426 | " ack=%d", MAC2STR(sm->addr), ack); |
| 4427 | if (sm->pending_1_of_4_timeout && ack) { |
| 4428 | /* |
| 4429 | * Some deployed supplicant implementations update their SNonce |
| 4430 | * for each EAPOL-Key 2/4 message even within the same 4-way |
| 4431 | * handshake and then fail to use the first SNonce when |
| 4432 | * deriving the PTK. This results in unsuccessful 4-way |
| 4433 | * handshake whenever the relatively short initial timeout is |
| 4434 | * reached and EAPOL-Key 1/4 is retransmitted. Try to work |
| 4435 | * around this by increasing the timeout now that we know that |
| 4436 | * the station has received the frame. |
| 4437 | */ |
| 4438 | int timeout_ms = eapol_key_timeout_subseq; |
| 4439 | wpa_printf(MSG_DEBUG, "WPA: Increase initial EAPOL-Key 1/4 " |
| 4440 | "timeout by %u ms because of acknowledged frame", |
| 4441 | timeout_ms); |
| 4442 | eloop_cancel_timeout(wpa_send_eapol_timeout, wpa_auth, sm); |
| 4443 | eloop_register_timeout(timeout_ms / 1000, |
| 4444 | (timeout_ms % 1000) * 1000, |
| 4445 | wpa_send_eapol_timeout, wpa_auth, sm); |
| 4446 | } |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 4447 | |
| 4448 | #ifdef CONFIG_TESTING_OPTIONS |
| 4449 | if (sm->eapol_status_cb) { |
| 4450 | sm->eapol_status_cb(sm->eapol_status_cb_ctx1, |
| 4451 | sm->eapol_status_cb_ctx2); |
| 4452 | sm->eapol_status_cb = NULL; |
| 4453 | } |
| 4454 | #endif /* CONFIG_TESTING_OPTIONS */ |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 4455 | } |
Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 4456 | |
| 4457 | |
| 4458 | int wpa_auth_uses_sae(struct wpa_state_machine *sm) |
| 4459 | { |
| 4460 | if (sm == NULL) |
| 4461 | return 0; |
| 4462 | return wpa_key_mgmt_sae(sm->wpa_key_mgmt); |
| 4463 | } |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 4464 | |
| 4465 | |
| 4466 | int wpa_auth_uses_ft_sae(struct wpa_state_machine *sm) |
| 4467 | { |
| 4468 | if (sm == NULL) |
| 4469 | return 0; |
| 4470 | return sm->wpa_key_mgmt == WPA_KEY_MGMT_FT_SAE; |
| 4471 | } |
Dmitry Shmidt | cf32e60 | 2014-01-28 10:57:39 -0800 | [diff] [blame] | 4472 | |
| 4473 | |
| 4474 | #ifdef CONFIG_P2P |
| 4475 | int wpa_auth_get_ip_addr(struct wpa_state_machine *sm, u8 *addr) |
| 4476 | { |
| 4477 | if (sm == NULL || WPA_GET_BE32(sm->ip_addr) == 0) |
| 4478 | return -1; |
| 4479 | os_memcpy(addr, sm->ip_addr, 4); |
| 4480 | return 0; |
| 4481 | } |
| 4482 | #endif /* CONFIG_P2P */ |
Dmitry Shmidt | 2f74e36 | 2015-01-21 13:19:05 -0800 | [diff] [blame] | 4483 | |
| 4484 | |
| 4485 | int wpa_auth_radius_das_disconnect_pmksa(struct wpa_authenticator *wpa_auth, |
| 4486 | struct radius_das_attrs *attr) |
| 4487 | { |
| 4488 | return pmksa_cache_auth_radius_das_disconnect(wpa_auth->pmksa, attr); |
| 4489 | } |
Dmitry Shmidt | 7f65602 | 2015-02-25 14:36:37 -0800 | [diff] [blame] | 4490 | |
| 4491 | |
| 4492 | void wpa_auth_reconfig_group_keys(struct wpa_authenticator *wpa_auth) |
| 4493 | { |
| 4494 | struct wpa_group *group; |
| 4495 | |
| 4496 | if (!wpa_auth) |
| 4497 | return; |
| 4498 | for (group = wpa_auth->group; group; group = group->next) |
| 4499 | wpa_group_config_group_keys(wpa_auth, group); |
| 4500 | } |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 4501 | |
| 4502 | |
| 4503 | #ifdef CONFIG_FILS |
| 4504 | |
| 4505 | struct wpa_auth_fils_iter_data { |
| 4506 | struct wpa_authenticator *auth; |
| 4507 | const u8 *cache_id; |
| 4508 | struct rsn_pmksa_cache_entry *pmksa; |
| 4509 | const u8 *spa; |
| 4510 | const u8 *pmkid; |
| 4511 | }; |
| 4512 | |
| 4513 | |
| 4514 | static int wpa_auth_fils_iter(struct wpa_authenticator *a, void *ctx) |
| 4515 | { |
| 4516 | struct wpa_auth_fils_iter_data *data = ctx; |
| 4517 | |
| 4518 | if (a == data->auth || !a->conf.fils_cache_id_set || |
| 4519 | os_memcmp(a->conf.fils_cache_id, data->cache_id, |
| 4520 | FILS_CACHE_ID_LEN) != 0) |
| 4521 | return 0; |
| 4522 | data->pmksa = pmksa_cache_auth_get(a->pmksa, data->spa, data->pmkid); |
| 4523 | return data->pmksa != NULL; |
| 4524 | } |
| 4525 | |
| 4526 | |
| 4527 | struct rsn_pmksa_cache_entry * |
| 4528 | wpa_auth_pmksa_get_fils_cache_id(struct wpa_authenticator *wpa_auth, |
| 4529 | const u8 *sta_addr, const u8 *pmkid) |
| 4530 | { |
| 4531 | struct wpa_auth_fils_iter_data idata; |
| 4532 | |
| 4533 | if (!wpa_auth->conf.fils_cache_id_set) |
| 4534 | return NULL; |
| 4535 | idata.auth = wpa_auth; |
| 4536 | idata.cache_id = wpa_auth->conf.fils_cache_id; |
| 4537 | idata.pmksa = NULL; |
| 4538 | idata.spa = sta_addr; |
| 4539 | idata.pmkid = pmkid; |
| 4540 | wpa_auth_for_each_auth(wpa_auth, wpa_auth_fils_iter, &idata); |
| 4541 | return idata.pmksa; |
| 4542 | } |
| 4543 | |
| 4544 | |
| 4545 | #ifdef CONFIG_IEEE80211R_AP |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame^] | 4546 | int wpa_auth_write_fte(struct wpa_authenticator *wpa_auth, int use_sha384, |
| 4547 | u8 *buf, size_t len) |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 4548 | { |
| 4549 | struct wpa_auth_config *conf = &wpa_auth->conf; |
| 4550 | |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame^] | 4551 | return wpa_write_ftie(conf, use_sha384, conf->r0_key_holder, |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 4552 | conf->r0_key_holder_len, |
| 4553 | NULL, NULL, buf, len, NULL, 0); |
| 4554 | } |
| 4555 | #endif /* CONFIG_IEEE80211R_AP */ |
| 4556 | |
| 4557 | |
| 4558 | void wpa_auth_get_fils_aead_params(struct wpa_state_machine *sm, |
| 4559 | u8 *fils_anonce, u8 *fils_snonce, |
| 4560 | u8 *fils_kek, size_t *fils_kek_len) |
| 4561 | { |
| 4562 | os_memcpy(fils_anonce, sm->ANonce, WPA_NONCE_LEN); |
| 4563 | os_memcpy(fils_snonce, sm->SNonce, WPA_NONCE_LEN); |
| 4564 | os_memcpy(fils_kek, sm->PTK.kek, WPA_KEK_MAX_LEN); |
| 4565 | *fils_kek_len = sm->PTK.kek_len; |
| 4566 | } |
| 4567 | |
| 4568 | #endif /* CONFIG_FILS */ |
| 4569 | |
| 4570 | |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame^] | 4571 | #ifdef CONFIG_TESTING_OPTIONS |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 4572 | |
| 4573 | int wpa_auth_resend_m1(struct wpa_state_machine *sm, int change_anonce, |
| 4574 | void (*cb)(void *ctx1, void *ctx2), |
| 4575 | void *ctx1, void *ctx2) |
| 4576 | { |
| 4577 | const u8 *anonce = sm->ANonce; |
| 4578 | u8 anonce_buf[WPA_NONCE_LEN]; |
| 4579 | |
| 4580 | if (change_anonce) { |
| 4581 | if (random_get_bytes(anonce_buf, WPA_NONCE_LEN)) |
| 4582 | return -1; |
| 4583 | anonce = anonce_buf; |
| 4584 | } |
| 4585 | |
| 4586 | wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG, |
| 4587 | "sending 1/4 msg of 4-Way Handshake (TESTING)"); |
| 4588 | wpa_send_eapol(sm->wpa_auth, sm, |
| 4589 | WPA_KEY_INFO_ACK | WPA_KEY_INFO_KEY_TYPE, NULL, |
| 4590 | anonce, NULL, 0, 0, 0); |
| 4591 | return 0; |
| 4592 | } |
| 4593 | |
| 4594 | |
| 4595 | int wpa_auth_resend_m3(struct wpa_state_machine *sm, |
| 4596 | void (*cb)(void *ctx1, void *ctx2), |
| 4597 | void *ctx1, void *ctx2) |
| 4598 | { |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame^] | 4599 | u8 rsc[WPA_KEY_RSC_LEN], *_rsc, *gtk, *kde, *pos; |
| 4600 | #ifdef CONFIG_IEEE80211W |
| 4601 | u8 *opos; |
| 4602 | #endif /* CONFIG_IEEE80211W */ |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 4603 | size_t gtk_len, kde_len; |
| 4604 | struct wpa_group *gsm = sm->group; |
| 4605 | u8 *wpa_ie; |
| 4606 | int wpa_ie_len, secure, keyidx, encr = 0; |
| 4607 | |
| 4608 | /* Send EAPOL(1, 1, 1, Pair, P, RSC, ANonce, MIC(PTK), RSNIE, [MDIE], |
| 4609 | GTK[GN], IGTK, [FTIE], [TIE * 2]) |
| 4610 | */ |
| 4611 | |
| 4612 | /* Use 0 RSC */ |
| 4613 | os_memset(rsc, 0, WPA_KEY_RSC_LEN); |
| 4614 | /* If FT is used, wpa_auth->wpa_ie includes both RSNIE and MDIE */ |
| 4615 | wpa_ie = sm->wpa_auth->wpa_ie; |
| 4616 | wpa_ie_len = sm->wpa_auth->wpa_ie_len; |
| 4617 | if (sm->wpa == WPA_VERSION_WPA && |
| 4618 | (sm->wpa_auth->conf.wpa & WPA_PROTO_RSN) && |
| 4619 | wpa_ie_len > wpa_ie[1] + 2 && wpa_ie[0] == WLAN_EID_RSN) { |
| 4620 | /* WPA-only STA, remove RSN IE and possible MDIE */ |
| 4621 | wpa_ie = wpa_ie + wpa_ie[1] + 2; |
| 4622 | if (wpa_ie[0] == WLAN_EID_MOBILITY_DOMAIN) |
| 4623 | wpa_ie = wpa_ie + wpa_ie[1] + 2; |
| 4624 | wpa_ie_len = wpa_ie[1] + 2; |
| 4625 | } |
| 4626 | wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG, |
| 4627 | "sending 3/4 msg of 4-Way Handshake (TESTING)"); |
| 4628 | if (sm->wpa == WPA_VERSION_WPA2) { |
| 4629 | /* WPA2 send GTK in the 4-way handshake */ |
| 4630 | secure = 1; |
| 4631 | gtk = gsm->GTK[gsm->GN - 1]; |
| 4632 | gtk_len = gsm->GTK_len; |
| 4633 | keyidx = gsm->GN; |
| 4634 | _rsc = rsc; |
| 4635 | encr = 1; |
| 4636 | } else { |
| 4637 | /* WPA does not include GTK in msg 3/4 */ |
| 4638 | secure = 0; |
| 4639 | gtk = NULL; |
| 4640 | gtk_len = 0; |
| 4641 | keyidx = 0; |
| 4642 | _rsc = NULL; |
| 4643 | if (sm->rx_eapol_key_secure) { |
| 4644 | /* |
| 4645 | * It looks like Windows 7 supplicant tries to use |
| 4646 | * Secure bit in msg 2/4 after having reported Michael |
| 4647 | * MIC failure and it then rejects the 4-way handshake |
| 4648 | * if msg 3/4 does not set Secure bit. Work around this |
| 4649 | * by setting the Secure bit here even in the case of |
| 4650 | * WPA if the supplicant used it first. |
| 4651 | */ |
| 4652 | wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG, |
| 4653 | "STA used Secure bit in WPA msg 2/4 - " |
| 4654 | "set Secure for 3/4 as workaround"); |
| 4655 | secure = 1; |
| 4656 | } |
| 4657 | } |
| 4658 | |
| 4659 | kde_len = wpa_ie_len + ieee80211w_kde_len(sm); |
| 4660 | if (gtk) |
| 4661 | kde_len += 2 + RSN_SELECTOR_LEN + 2 + gtk_len; |
| 4662 | #ifdef CONFIG_IEEE80211R_AP |
| 4663 | if (wpa_key_mgmt_ft(sm->wpa_key_mgmt)) { |
| 4664 | kde_len += 2 + PMKID_LEN; /* PMKR1Name into RSN IE */ |
| 4665 | kde_len += 300; /* FTIE + 2 * TIE */ |
| 4666 | } |
| 4667 | #endif /* CONFIG_IEEE80211R_AP */ |
| 4668 | kde = os_malloc(kde_len); |
| 4669 | if (kde == NULL) |
| 4670 | return -1; |
| 4671 | |
| 4672 | pos = kde; |
| 4673 | os_memcpy(pos, wpa_ie, wpa_ie_len); |
| 4674 | pos += wpa_ie_len; |
| 4675 | #ifdef CONFIG_IEEE80211R_AP |
| 4676 | if (wpa_key_mgmt_ft(sm->wpa_key_mgmt)) { |
| 4677 | int res; |
| 4678 | size_t elen; |
| 4679 | |
| 4680 | elen = pos - kde; |
| 4681 | res = wpa_insert_pmkid(kde, &elen, sm->pmk_r1_name); |
| 4682 | if (res < 0) { |
| 4683 | wpa_printf(MSG_ERROR, "FT: Failed to insert " |
| 4684 | "PMKR1Name into RSN IE in EAPOL-Key data"); |
| 4685 | os_free(kde); |
| 4686 | return -1; |
| 4687 | } |
| 4688 | pos -= wpa_ie_len; |
| 4689 | pos += elen; |
| 4690 | } |
| 4691 | #endif /* CONFIG_IEEE80211R_AP */ |
| 4692 | if (gtk) { |
| 4693 | u8 hdr[2]; |
| 4694 | hdr[0] = keyidx & 0x03; |
| 4695 | hdr[1] = 0; |
| 4696 | pos = wpa_add_kde(pos, RSN_KEY_DATA_GROUPKEY, hdr, 2, |
| 4697 | gtk, gtk_len); |
| 4698 | } |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame^] | 4699 | #ifdef CONFIG_IEEE80211W |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 4700 | opos = pos; |
| 4701 | pos = ieee80211w_kde_add(sm, pos); |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame^] | 4702 | if (pos - opos >= 2 + RSN_SELECTOR_LEN + WPA_IGTK_KDE_PREFIX_LEN) { |
| 4703 | /* skip KDE header and keyid */ |
| 4704 | opos += 2 + RSN_SELECTOR_LEN + 2; |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 4705 | os_memset(opos, 0, 6); /* clear PN */ |
| 4706 | } |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame^] | 4707 | #endif /* CONFIG_IEEE80211W */ |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 4708 | |
| 4709 | #ifdef CONFIG_IEEE80211R_AP |
| 4710 | if (wpa_key_mgmt_ft(sm->wpa_key_mgmt)) { |
| 4711 | int res; |
| 4712 | struct wpa_auth_config *conf; |
| 4713 | |
| 4714 | conf = &sm->wpa_auth->conf; |
| 4715 | if (sm->assoc_resp_ftie && |
| 4716 | kde + kde_len - pos >= 2 + sm->assoc_resp_ftie[1]) { |
| 4717 | os_memcpy(pos, sm->assoc_resp_ftie, |
| 4718 | 2 + sm->assoc_resp_ftie[1]); |
| 4719 | res = 2 + sm->assoc_resp_ftie[1]; |
| 4720 | } else { |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame^] | 4721 | int use_sha384 = wpa_key_mgmt_sha384(sm->wpa_key_mgmt); |
| 4722 | |
| 4723 | res = wpa_write_ftie(conf, use_sha384, |
| 4724 | conf->r0_key_holder, |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 4725 | conf->r0_key_holder_len, |
| 4726 | NULL, NULL, pos, |
| 4727 | kde + kde_len - pos, |
| 4728 | NULL, 0); |
| 4729 | } |
| 4730 | if (res < 0) { |
| 4731 | wpa_printf(MSG_ERROR, "FT: Failed to insert FTIE " |
| 4732 | "into EAPOL-Key Key Data"); |
| 4733 | os_free(kde); |
| 4734 | return -1; |
| 4735 | } |
| 4736 | pos += res; |
| 4737 | |
| 4738 | /* TIE[ReassociationDeadline] (TU) */ |
| 4739 | *pos++ = WLAN_EID_TIMEOUT_INTERVAL; |
| 4740 | *pos++ = 5; |
| 4741 | *pos++ = WLAN_TIMEOUT_REASSOC_DEADLINE; |
| 4742 | WPA_PUT_LE32(pos, conf->reassociation_deadline); |
| 4743 | pos += 4; |
| 4744 | |
| 4745 | /* TIE[KeyLifetime] (seconds) */ |
| 4746 | *pos++ = WLAN_EID_TIMEOUT_INTERVAL; |
| 4747 | *pos++ = 5; |
| 4748 | *pos++ = WLAN_TIMEOUT_KEY_LIFETIME; |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame^] | 4749 | WPA_PUT_LE32(pos, conf->r0_key_lifetime); |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 4750 | pos += 4; |
| 4751 | } |
| 4752 | #endif /* CONFIG_IEEE80211R_AP */ |
| 4753 | |
| 4754 | wpa_send_eapol(sm->wpa_auth, sm, |
| 4755 | (secure ? WPA_KEY_INFO_SECURE : 0) | |
| 4756 | (wpa_mic_len(sm->wpa_key_mgmt, sm->pmk_len) ? |
| 4757 | WPA_KEY_INFO_MIC : 0) | |
| 4758 | WPA_KEY_INFO_ACK | WPA_KEY_INFO_INSTALL | |
| 4759 | WPA_KEY_INFO_KEY_TYPE, |
| 4760 | _rsc, sm->ANonce, kde, pos - kde, keyidx, encr); |
| 4761 | os_free(kde); |
| 4762 | return 0; |
| 4763 | } |
| 4764 | |
| 4765 | |
| 4766 | int wpa_auth_resend_group_m1(struct wpa_state_machine *sm, |
| 4767 | void (*cb)(void *ctx1, void *ctx2), |
| 4768 | void *ctx1, void *ctx2) |
| 4769 | { |
| 4770 | u8 rsc[WPA_KEY_RSC_LEN]; |
| 4771 | struct wpa_group *gsm = sm->group; |
| 4772 | const u8 *kde; |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame^] | 4773 | u8 *kde_buf = NULL, *pos, hdr[2]; |
| 4774 | #ifdef CONFIG_IEEE80211W |
| 4775 | u8 *opos; |
| 4776 | #endif /* CONFIG_IEEE80211W */ |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 4777 | size_t kde_len; |
| 4778 | u8 *gtk; |
| 4779 | |
| 4780 | /* Send EAPOL(1, 1, 1, !Pair, G, RSC, GNonce, MIC(PTK), GTK[GN]) */ |
| 4781 | os_memset(rsc, 0, WPA_KEY_RSC_LEN); |
| 4782 | /* Use 0 RSC */ |
| 4783 | wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG, |
| 4784 | "sending 1/2 msg of Group Key Handshake (TESTING)"); |
| 4785 | |
| 4786 | gtk = gsm->GTK[gsm->GN - 1]; |
| 4787 | if (sm->wpa == WPA_VERSION_WPA2) { |
| 4788 | kde_len = 2 + RSN_SELECTOR_LEN + 2 + gsm->GTK_len + |
| 4789 | ieee80211w_kde_len(sm); |
| 4790 | kde_buf = os_malloc(kde_len); |
| 4791 | if (kde_buf == NULL) |
| 4792 | return -1; |
| 4793 | |
| 4794 | kde = pos = kde_buf; |
| 4795 | hdr[0] = gsm->GN & 0x03; |
| 4796 | hdr[1] = 0; |
| 4797 | pos = wpa_add_kde(pos, RSN_KEY_DATA_GROUPKEY, hdr, 2, |
| 4798 | gtk, gsm->GTK_len); |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame^] | 4799 | #ifdef CONFIG_IEEE80211W |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 4800 | opos = pos; |
| 4801 | pos = ieee80211w_kde_add(sm, pos); |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame^] | 4802 | if (pos - opos >= |
| 4803 | 2 + RSN_SELECTOR_LEN + WPA_IGTK_KDE_PREFIX_LEN) { |
| 4804 | /* skip KDE header and keyid */ |
| 4805 | opos += 2 + RSN_SELECTOR_LEN + 2; |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 4806 | os_memset(opos, 0, 6); /* clear PN */ |
| 4807 | } |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame^] | 4808 | #endif /* CONFIG_IEEE80211W */ |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 4809 | kde_len = pos - kde; |
| 4810 | } else { |
| 4811 | kde = gtk; |
| 4812 | kde_len = gsm->GTK_len; |
| 4813 | } |
| 4814 | |
| 4815 | sm->eapol_status_cb = cb; |
| 4816 | sm->eapol_status_cb_ctx1 = ctx1; |
| 4817 | sm->eapol_status_cb_ctx2 = ctx2; |
| 4818 | |
| 4819 | wpa_send_eapol(sm->wpa_auth, sm, |
| 4820 | WPA_KEY_INFO_SECURE | |
| 4821 | (wpa_mic_len(sm->wpa_key_mgmt, sm->pmk_len) ? |
| 4822 | WPA_KEY_INFO_MIC : 0) | |
| 4823 | WPA_KEY_INFO_ACK | |
| 4824 | (!sm->Pair ? WPA_KEY_INFO_INSTALL : 0), |
| 4825 | rsc, NULL, kde, kde_len, gsm->GN, 1); |
| 4826 | |
| 4827 | os_free(kde_buf); |
| 4828 | return 0; |
| 4829 | } |
| 4830 | |
Roshan Pius | 3a1667e | 2018-07-03 15:17:14 -0700 | [diff] [blame^] | 4831 | |
| 4832 | int wpa_auth_rekey_gtk(struct wpa_authenticator *wpa_auth) |
| 4833 | { |
| 4834 | if (!wpa_auth) |
| 4835 | return -1; |
| 4836 | eloop_cancel_timeout(wpa_rekey_gtk, wpa_auth, NULL); |
| 4837 | return eloop_register_timeout(0, 0, wpa_rekey_gtk, wpa_auth, NULL); |
| 4838 | } |
| 4839 | |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 4840 | #endif /* CONFIG_TESTING_OPTIONS */ |