Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1 | /* |
| 2 | * WPA Supplicant - test code for pre-authentication |
| 3 | * Copyright (c) 2003-2007, Jouni Malinen <j@w1.fi> |
| 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 | * IEEE 802.1X Supplicant test code (to be used in place of wpa_supplicant.c. |
| 9 | * Not used in production version. |
| 10 | */ |
| 11 | |
| 12 | #include "includes.h" |
| 13 | #include <assert.h> |
| 14 | |
| 15 | #include "common.h" |
Sunil Ravi | a04bd25 | 2022-05-02 22:54:18 -0700 | [diff] [blame] | 16 | #include "crypto/crypto.h" |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 17 | #include "config.h" |
| 18 | #include "eapol_supp/eapol_supp_sm.h" |
| 19 | #include "eloop.h" |
| 20 | #include "rsn_supp/wpa.h" |
| 21 | #include "eap_peer/eap.h" |
| 22 | #include "wpa_supplicant_i.h" |
| 23 | #include "l2_packet/l2_packet.h" |
| 24 | #include "ctrl_iface.h" |
| 25 | #include "pcsc_funcs.h" |
| 26 | #include "rsn_supp/preauth.h" |
| 27 | #include "rsn_supp/pmksa_cache.h" |
| 28 | #include "drivers/driver.h" |
| 29 | |
| 30 | |
Dmitry Shmidt | 1d755d0 | 2015-04-28 10:34:29 -0700 | [diff] [blame] | 31 | const struct wpa_driver_ops *const wpa_drivers[] = { NULL }; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 32 | |
| 33 | |
| 34 | struct preauth_test_data { |
| 35 | int auth_timed_out; |
| 36 | }; |
| 37 | |
| 38 | |
Hai Shalom | 81f62d8 | 2019-07-22 12:10:00 -0700 | [diff] [blame] | 39 | static void _wpa_supplicant_deauthenticate(void *wpa_s, u16 reason_code) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 40 | { |
| 41 | wpa_supplicant_deauthenticate(wpa_s, reason_code); |
| 42 | } |
| 43 | |
| 44 | |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 45 | static void _wpa_supplicant_reconnect(void *wpa_s) |
| 46 | { |
| 47 | wpa_supplicant_reconnect(wpa_s); |
| 48 | } |
| 49 | |
| 50 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 51 | static u8 * wpa_alloc_eapol(const struct wpa_supplicant *wpa_s, u8 type, |
| 52 | const void *data, u16 data_len, |
| 53 | size_t *msg_len, void **data_pos) |
| 54 | { |
| 55 | struct ieee802_1x_hdr *hdr; |
| 56 | |
| 57 | *msg_len = sizeof(*hdr) + data_len; |
| 58 | hdr = os_malloc(*msg_len); |
| 59 | if (hdr == NULL) |
| 60 | return NULL; |
| 61 | |
| 62 | hdr->version = wpa_s->conf->eapol_version; |
| 63 | hdr->type = type; |
| 64 | hdr->length = htons(data_len); |
| 65 | |
| 66 | if (data) |
| 67 | os_memcpy(hdr + 1, data, data_len); |
| 68 | else |
| 69 | os_memset(hdr + 1, 0, data_len); |
| 70 | |
| 71 | if (data_pos) |
| 72 | *data_pos = hdr + 1; |
| 73 | |
| 74 | return (u8 *) hdr; |
| 75 | } |
| 76 | |
| 77 | |
| 78 | static u8 * _wpa_alloc_eapol(void *wpa_s, u8 type, |
| 79 | const void *data, u16 data_len, |
| 80 | size_t *msg_len, void **data_pos) |
| 81 | { |
| 82 | return wpa_alloc_eapol(wpa_s, type, data, data_len, msg_len, data_pos); |
| 83 | } |
| 84 | |
| 85 | |
| 86 | static void _wpa_supplicant_set_state(void *ctx, enum wpa_states state) |
| 87 | { |
| 88 | struct wpa_supplicant *wpa_s = ctx; |
| 89 | wpa_s->wpa_state = state; |
| 90 | } |
| 91 | |
| 92 | |
| 93 | static enum wpa_states _wpa_supplicant_get_state(void *ctx) |
| 94 | { |
| 95 | struct wpa_supplicant *wpa_s = ctx; |
| 96 | return wpa_s->wpa_state; |
| 97 | } |
| 98 | |
| 99 | |
| 100 | static int wpa_ether_send(void *wpa_s, const u8 *dest, u16 proto, |
| 101 | const u8 *buf, size_t len) |
| 102 | { |
| 103 | printf("%s - not implemented\n", __func__); |
| 104 | return -1; |
| 105 | } |
| 106 | |
| 107 | |
| 108 | static void * wpa_supplicant_get_network_ctx(void *wpa_s) |
| 109 | { |
| 110 | return wpa_supplicant_get_ssid(wpa_s); |
| 111 | } |
| 112 | |
| 113 | |
| 114 | static void _wpa_supplicant_cancel_auth_timeout(void *wpa_s) |
| 115 | { |
| 116 | wpa_supplicant_cancel_auth_timeout(wpa_s); |
| 117 | } |
| 118 | |
| 119 | |
| 120 | static int wpa_supplicant_get_beacon_ie(void *wpa_s) |
| 121 | { |
| 122 | printf("%s - not implemented\n", __func__); |
| 123 | return -1; |
| 124 | } |
| 125 | |
| 126 | |
| 127 | static int wpa_supplicant_get_bssid(void *wpa_s, u8 *bssid) |
| 128 | { |
| 129 | printf("%s - not implemented\n", __func__); |
| 130 | return -1; |
| 131 | } |
| 132 | |
| 133 | |
Sunil Ravi | 38ad1ed | 2023-01-17 23:58:31 +0000 | [diff] [blame] | 134 | static int wpa_supplicant_set_key(void *wpa_s, int link_id, enum wpa_alg alg, |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 135 | const u8 *addr, int key_idx, int set_tx, |
| 136 | const u8 *seq, size_t seq_len, |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 137 | const u8 *key, size_t key_len, |
| 138 | enum key_flag key_flag) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 139 | { |
| 140 | printf("%s - not implemented\n", __func__); |
| 141 | return -1; |
| 142 | } |
| 143 | |
| 144 | |
| 145 | static int wpa_supplicant_mlme_setprotection(void *wpa_s, const u8 *addr, |
| 146 | int protection_type, |
| 147 | int key_type) |
| 148 | { |
| 149 | printf("%s - not implemented\n", __func__); |
| 150 | return -1; |
| 151 | } |
| 152 | |
| 153 | |
Dmitry Shmidt | 2933359 | 2017-01-09 12:27:11 -0800 | [diff] [blame] | 154 | static int wpa_supplicant_add_pmkid(void *wpa_s, void *network_ctx, |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 155 | const u8 *bssid, const u8 *pmkid, |
| 156 | const u8 *fils_cache_id, |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 157 | const u8 *pmk, size_t pmk_len, |
Hai Shalom | 899fcc7 | 2020-10-19 14:38:18 -0700 | [diff] [blame] | 158 | u32 pmk_lifetime, u8 pmk_reauth_threshold, |
| 159 | int akmp) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 160 | { |
| 161 | printf("%s - not implemented\n", __func__); |
| 162 | return -1; |
| 163 | } |
| 164 | |
| 165 | |
Dmitry Shmidt | 2933359 | 2017-01-09 12:27:11 -0800 | [diff] [blame] | 166 | static int wpa_supplicant_remove_pmkid(void *wpa_s, void *network_ctx, |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 167 | const u8 *bssid, const u8 *pmkid, |
| 168 | const u8 *fils_cache_id) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 169 | { |
| 170 | printf("%s - not implemented\n", __func__); |
| 171 | return -1; |
| 172 | } |
| 173 | |
| 174 | |
| 175 | static void wpa_supplicant_set_config_blob(void *ctx, |
| 176 | struct wpa_config_blob *blob) |
| 177 | { |
| 178 | struct wpa_supplicant *wpa_s = ctx; |
| 179 | wpa_config_set_blob(wpa_s->conf, blob); |
| 180 | } |
| 181 | |
| 182 | |
| 183 | static const struct wpa_config_blob * |
| 184 | wpa_supplicant_get_config_blob(void *ctx, const char *name) |
| 185 | { |
| 186 | struct wpa_supplicant *wpa_s = ctx; |
| 187 | return wpa_config_get_blob(wpa_s->conf, name); |
| 188 | } |
| 189 | |
| 190 | |
| 191 | static void test_eapol_clean(struct wpa_supplicant *wpa_s) |
| 192 | { |
| 193 | rsn_preauth_deinit(wpa_s->wpa); |
| 194 | pmksa_candidate_free(wpa_s->wpa); |
| 195 | wpa_sm_deinit(wpa_s->wpa); |
| 196 | scard_deinit(wpa_s->scard); |
Jouni Malinen | f3f8d3c | 2021-02-05 00:28:17 +0200 | [diff] [blame] | 197 | wpa_supplicant_ctrl_iface_deinit(wpa_s, wpa_s->ctrl_iface); |
| 198 | wpa_s->ctrl_iface = NULL; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 199 | wpa_config_free(wpa_s->conf); |
| 200 | } |
| 201 | |
| 202 | |
| 203 | static void eapol_test_timeout(void *eloop_ctx, void *timeout_ctx) |
| 204 | { |
| 205 | struct preauth_test_data *p = eloop_ctx; |
| 206 | printf("EAPOL test timed out\n"); |
| 207 | p->auth_timed_out = 1; |
| 208 | eloop_terminate(); |
| 209 | } |
| 210 | |
| 211 | |
| 212 | static void eapol_test_poll(void *eloop_ctx, void *timeout_ctx) |
| 213 | { |
| 214 | struct wpa_supplicant *wpa_s = eloop_ctx; |
| 215 | if (!rsn_preauth_in_progress(wpa_s->wpa)) |
| 216 | eloop_terminate(); |
| 217 | else { |
| 218 | eloop_register_timeout(0, 100000, eapol_test_poll, eloop_ctx, |
| 219 | timeout_ctx); |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | |
Hai Shalom | a20dcd7 | 2022-02-04 13:43:00 -0800 | [diff] [blame] | 224 | static struct wpa_driver_ops stub_driver; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 225 | |
| 226 | |
| 227 | static void wpa_init_conf(struct wpa_supplicant *wpa_s, const char *ifname) |
| 228 | { |
| 229 | struct l2_packet_data *l2; |
| 230 | struct wpa_sm_ctx *ctx; |
| 231 | |
Hai Shalom | a20dcd7 | 2022-02-04 13:43:00 -0800 | [diff] [blame] | 232 | os_memset(&stub_driver, 0, sizeof(stub_driver)); |
| 233 | wpa_s->driver = &stub_driver; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 234 | |
| 235 | ctx = os_zalloc(sizeof(*ctx)); |
| 236 | assert(ctx != NULL); |
| 237 | |
| 238 | ctx->ctx = wpa_s; |
| 239 | ctx->msg_ctx = wpa_s; |
| 240 | ctx->set_state = _wpa_supplicant_set_state; |
| 241 | ctx->get_state = _wpa_supplicant_get_state; |
| 242 | ctx->deauthenticate = _wpa_supplicant_deauthenticate; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 243 | ctx->set_key = wpa_supplicant_set_key; |
| 244 | ctx->get_network_ctx = wpa_supplicant_get_network_ctx; |
| 245 | ctx->get_bssid = wpa_supplicant_get_bssid; |
| 246 | ctx->ether_send = wpa_ether_send; |
| 247 | ctx->get_beacon_ie = wpa_supplicant_get_beacon_ie; |
| 248 | ctx->alloc_eapol = _wpa_alloc_eapol; |
| 249 | ctx->cancel_auth_timeout = _wpa_supplicant_cancel_auth_timeout; |
| 250 | ctx->add_pmkid = wpa_supplicant_add_pmkid; |
| 251 | ctx->remove_pmkid = wpa_supplicant_remove_pmkid; |
| 252 | ctx->set_config_blob = wpa_supplicant_set_config_blob; |
| 253 | ctx->get_config_blob = wpa_supplicant_get_config_blob; |
| 254 | ctx->mlme_setprotection = wpa_supplicant_mlme_setprotection; |
Hai Shalom | fdcde76 | 2020-04-02 11:19:20 -0700 | [diff] [blame] | 255 | ctx->reconnect = _wpa_supplicant_reconnect; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 256 | |
| 257 | wpa_s->wpa = wpa_sm_init(ctx); |
| 258 | assert(wpa_s->wpa != NULL); |
| 259 | wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_PROTO, WPA_PROTO_RSN); |
| 260 | |
| 261 | os_strlcpy(wpa_s->ifname, ifname, sizeof(wpa_s->ifname)); |
| 262 | wpa_sm_set_ifname(wpa_s->wpa, wpa_s->ifname, NULL); |
| 263 | |
| 264 | l2 = l2_packet_init(wpa_s->ifname, NULL, ETH_P_RSN_PREAUTH, NULL, |
| 265 | NULL, 0); |
| 266 | assert(l2 != NULL); |
| 267 | if (l2_packet_get_own_addr(l2, wpa_s->own_addr)) { |
| 268 | wpa_printf(MSG_WARNING, "Failed to get own L2 address\n"); |
| 269 | exit(-1); |
| 270 | } |
| 271 | l2_packet_deinit(l2); |
| 272 | wpa_sm_set_own_addr(wpa_s->wpa, wpa_s->own_addr); |
| 273 | } |
| 274 | |
| 275 | |
| 276 | static void eapol_test_terminate(int sig, void *signal_ctx) |
| 277 | { |
| 278 | struct wpa_supplicant *wpa_s = signal_ctx; |
| 279 | wpa_msg(wpa_s, MSG_INFO, "Signal %d received - terminating", sig); |
| 280 | eloop_terminate(); |
| 281 | } |
| 282 | |
| 283 | |
| 284 | int main(int argc, char *argv[]) |
| 285 | { |
| 286 | struct wpa_supplicant wpa_s; |
| 287 | int ret = 1; |
| 288 | u8 bssid[ETH_ALEN]; |
| 289 | struct preauth_test_data preauth_test; |
| 290 | |
| 291 | if (os_program_init()) |
| 292 | return -1; |
| 293 | |
| 294 | os_memset(&preauth_test, 0, sizeof(preauth_test)); |
| 295 | |
| 296 | wpa_debug_level = 0; |
| 297 | wpa_debug_show_keys = 1; |
| 298 | |
| 299 | if (argc != 4) { |
| 300 | printf("usage: preauth_test <conf> <target MAC address> " |
| 301 | "<ifname>\n"); |
| 302 | return -1; |
| 303 | } |
| 304 | |
| 305 | if (hwaddr_aton(argv[2], bssid)) { |
| 306 | printf("Failed to parse target address '%s'.\n", argv[2]); |
| 307 | return -1; |
| 308 | } |
| 309 | |
| 310 | if (eap_register_methods()) { |
| 311 | wpa_printf(MSG_ERROR, "Failed to register EAP methods"); |
| 312 | return -1; |
| 313 | } |
| 314 | |
| 315 | if (eloop_init()) { |
| 316 | wpa_printf(MSG_ERROR, "Failed to initialize event loop"); |
| 317 | return -1; |
| 318 | } |
| 319 | |
| 320 | os_memset(&wpa_s, 0, sizeof(wpa_s)); |
Sunil Ravi | 38ad1ed | 2023-01-17 23:58:31 +0000 | [diff] [blame] | 321 | wpa_s.conf = wpa_config_read(argv[1], NULL, false); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 322 | if (wpa_s.conf == NULL) { |
| 323 | printf("Failed to parse configuration file '%s'.\n", argv[1]); |
| 324 | return -1; |
| 325 | } |
| 326 | if (wpa_s.conf->ssid == NULL) { |
| 327 | printf("No networks defined.\n"); |
| 328 | return -1; |
| 329 | } |
| 330 | |
| 331 | wpa_init_conf(&wpa_s, argv[3]); |
| 332 | wpa_s.ctrl_iface = wpa_supplicant_ctrl_iface_init(&wpa_s); |
| 333 | if (wpa_s.ctrl_iface == NULL) { |
| 334 | printf("Failed to initialize control interface '%s'.\n" |
| 335 | "You may have another preauth_test process already " |
| 336 | "running or the file was\n" |
| 337 | "left by an unclean termination of preauth_test in " |
| 338 | "which case you will need\n" |
| 339 | "to manually remove this file before starting " |
| 340 | "preauth_test again.\n", |
| 341 | wpa_s.conf->ctrl_interface); |
| 342 | return -1; |
| 343 | } |
| 344 | if (wpa_supplicant_scard_init(&wpa_s, wpa_s.conf->ssid)) |
| 345 | return -1; |
| 346 | |
| 347 | if (rsn_preauth_init(wpa_s.wpa, bssid, &wpa_s.conf->ssid->eap)) |
| 348 | return -1; |
| 349 | |
| 350 | eloop_register_timeout(30, 0, eapol_test_timeout, &preauth_test, NULL); |
| 351 | eloop_register_timeout(0, 100000, eapol_test_poll, &wpa_s, NULL); |
| 352 | eloop_register_signal_terminate(eapol_test_terminate, &wpa_s); |
| 353 | eloop_register_signal_reconfig(eapol_test_terminate, &wpa_s); |
| 354 | eloop_run(); |
| 355 | |
| 356 | if (preauth_test.auth_timed_out) |
| 357 | ret = -2; |
| 358 | else { |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 359 | ret = pmksa_cache_set_current(wpa_s.wpa, NULL, bssid, NULL, 0, |
Sunil Ravi | 2a14cf1 | 2023-11-21 00:54:38 +0000 | [diff] [blame] | 360 | NULL, 0, false) ? 0 : -3; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 361 | } |
| 362 | |
| 363 | test_eapol_clean(&wpa_s); |
| 364 | |
| 365 | eap_peer_unregister_methods(); |
| 366 | |
| 367 | eloop_destroy(); |
| 368 | |
Sunil Ravi | a04bd25 | 2022-05-02 22:54:18 -0700 | [diff] [blame] | 369 | crypto_unload(); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 370 | os_program_deinit(); |
| 371 | |
| 372 | return ret; |
| 373 | } |