preauth_test.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. /*
  2. * WPA Supplicant - test code for pre-authentication
  3. * Copyright (c) 2003-2007, Jouni Malinen <j@w1.fi>
  4. *
  5. * This software may be distributed under the terms of the BSD license.
  6. * See README for more details.
  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. #include "includes.h"
  12. #include <assert.h>
  13. #include "common.h"
  14. #include "config.h"
  15. #include "eapol_supp/eapol_supp_sm.h"
  16. #include "eloop.h"
  17. #include "rsn_supp/wpa.h"
  18. #include "eap_peer/eap.h"
  19. #include "wpa_supplicant_i.h"
  20. #include "l2_packet/l2_packet.h"
  21. #include "ctrl_iface.h"
  22. #include "pcsc_funcs.h"
  23. #include "rsn_supp/preauth.h"
  24. #include "rsn_supp/pmksa_cache.h"
  25. #include "drivers/driver.h"
  26. const struct wpa_driver_ops *const wpa_drivers[] = { NULL };
  27. struct preauth_test_data {
  28. int auth_timed_out;
  29. };
  30. static void _wpa_supplicant_deauthenticate(void *wpa_s, int reason_code)
  31. {
  32. wpa_supplicant_deauthenticate(wpa_s, reason_code);
  33. }
  34. static u8 * wpa_alloc_eapol(const struct wpa_supplicant *wpa_s, u8 type,
  35. const void *data, u16 data_len,
  36. size_t *msg_len, void **data_pos)
  37. {
  38. struct ieee802_1x_hdr *hdr;
  39. *msg_len = sizeof(*hdr) + data_len;
  40. hdr = os_malloc(*msg_len);
  41. if (hdr == NULL)
  42. return NULL;
  43. hdr->version = wpa_s->conf->eapol_version;
  44. hdr->type = type;
  45. hdr->length = htons(data_len);
  46. if (data)
  47. os_memcpy(hdr + 1, data, data_len);
  48. else
  49. os_memset(hdr + 1, 0, data_len);
  50. if (data_pos)
  51. *data_pos = hdr + 1;
  52. return (u8 *) hdr;
  53. }
  54. static u8 * _wpa_alloc_eapol(void *wpa_s, u8 type,
  55. const void *data, u16 data_len,
  56. size_t *msg_len, void **data_pos)
  57. {
  58. return wpa_alloc_eapol(wpa_s, type, data, data_len, msg_len, data_pos);
  59. }
  60. static void _wpa_supplicant_set_state(void *ctx, enum wpa_states state)
  61. {
  62. struct wpa_supplicant *wpa_s = ctx;
  63. wpa_s->wpa_state = state;
  64. }
  65. static enum wpa_states _wpa_supplicant_get_state(void *ctx)
  66. {
  67. struct wpa_supplicant *wpa_s = ctx;
  68. return wpa_s->wpa_state;
  69. }
  70. static int wpa_ether_send(void *wpa_s, const u8 *dest, u16 proto,
  71. const u8 *buf, size_t len)
  72. {
  73. printf("%s - not implemented\n", __func__);
  74. return -1;
  75. }
  76. static void * wpa_supplicant_get_network_ctx(void *wpa_s)
  77. {
  78. return wpa_supplicant_get_ssid(wpa_s);
  79. }
  80. static void _wpa_supplicant_cancel_auth_timeout(void *wpa_s)
  81. {
  82. wpa_supplicant_cancel_auth_timeout(wpa_s);
  83. }
  84. static int wpa_supplicant_get_beacon_ie(void *wpa_s)
  85. {
  86. printf("%s - not implemented\n", __func__);
  87. return -1;
  88. }
  89. static int wpa_supplicant_get_bssid(void *wpa_s, u8 *bssid)
  90. {
  91. printf("%s - not implemented\n", __func__);
  92. return -1;
  93. }
  94. static int wpa_supplicant_set_key(void *wpa_s, enum wpa_alg alg,
  95. const u8 *addr, int key_idx, int set_tx,
  96. const u8 *seq, size_t seq_len,
  97. const u8 *key, size_t key_len)
  98. {
  99. printf("%s - not implemented\n", __func__);
  100. return -1;
  101. }
  102. static int wpa_supplicant_mlme_setprotection(void *wpa_s, const u8 *addr,
  103. int protection_type,
  104. int key_type)
  105. {
  106. printf("%s - not implemented\n", __func__);
  107. return -1;
  108. }
  109. static int wpa_supplicant_add_pmkid(void *wpa_s, void *network_ctx,
  110. const u8 *bssid, const u8 *pmkid,
  111. const u8 *fils_cache_id,
  112. const u8 *pmk, size_t pmk_len)
  113. {
  114. printf("%s - not implemented\n", __func__);
  115. return -1;
  116. }
  117. static int wpa_supplicant_remove_pmkid(void *wpa_s, void *network_ctx,
  118. const u8 *bssid, const u8 *pmkid,
  119. const u8 *fils_cache_id)
  120. {
  121. printf("%s - not implemented\n", __func__);
  122. return -1;
  123. }
  124. static void wpa_supplicant_set_config_blob(void *ctx,
  125. struct wpa_config_blob *blob)
  126. {
  127. struct wpa_supplicant *wpa_s = ctx;
  128. wpa_config_set_blob(wpa_s->conf, blob);
  129. }
  130. static const struct wpa_config_blob *
  131. wpa_supplicant_get_config_blob(void *ctx, const char *name)
  132. {
  133. struct wpa_supplicant *wpa_s = ctx;
  134. return wpa_config_get_blob(wpa_s->conf, name);
  135. }
  136. static void test_eapol_clean(struct wpa_supplicant *wpa_s)
  137. {
  138. rsn_preauth_deinit(wpa_s->wpa);
  139. pmksa_candidate_free(wpa_s->wpa);
  140. wpa_sm_deinit(wpa_s->wpa);
  141. scard_deinit(wpa_s->scard);
  142. if (wpa_s->ctrl_iface) {
  143. wpa_supplicant_ctrl_iface_deinit(wpa_s->ctrl_iface);
  144. wpa_s->ctrl_iface = NULL;
  145. }
  146. wpa_config_free(wpa_s->conf);
  147. }
  148. static void eapol_test_timeout(void *eloop_ctx, void *timeout_ctx)
  149. {
  150. struct preauth_test_data *p = eloop_ctx;
  151. printf("EAPOL test timed out\n");
  152. p->auth_timed_out = 1;
  153. eloop_terminate();
  154. }
  155. static void eapol_test_poll(void *eloop_ctx, void *timeout_ctx)
  156. {
  157. struct wpa_supplicant *wpa_s = eloop_ctx;
  158. if (!rsn_preauth_in_progress(wpa_s->wpa))
  159. eloop_terminate();
  160. else {
  161. eloop_register_timeout(0, 100000, eapol_test_poll, eloop_ctx,
  162. timeout_ctx);
  163. }
  164. }
  165. static struct wpa_driver_ops dummy_driver;
  166. static void wpa_init_conf(struct wpa_supplicant *wpa_s, const char *ifname)
  167. {
  168. struct l2_packet_data *l2;
  169. struct wpa_sm_ctx *ctx;
  170. os_memset(&dummy_driver, 0, sizeof(dummy_driver));
  171. wpa_s->driver = &dummy_driver;
  172. ctx = os_zalloc(sizeof(*ctx));
  173. assert(ctx != NULL);
  174. ctx->ctx = wpa_s;
  175. ctx->msg_ctx = wpa_s;
  176. ctx->set_state = _wpa_supplicant_set_state;
  177. ctx->get_state = _wpa_supplicant_get_state;
  178. ctx->deauthenticate = _wpa_supplicant_deauthenticate;
  179. ctx->set_key = wpa_supplicant_set_key;
  180. ctx->get_network_ctx = wpa_supplicant_get_network_ctx;
  181. ctx->get_bssid = wpa_supplicant_get_bssid;
  182. ctx->ether_send = wpa_ether_send;
  183. ctx->get_beacon_ie = wpa_supplicant_get_beacon_ie;
  184. ctx->alloc_eapol = _wpa_alloc_eapol;
  185. ctx->cancel_auth_timeout = _wpa_supplicant_cancel_auth_timeout;
  186. ctx->add_pmkid = wpa_supplicant_add_pmkid;
  187. ctx->remove_pmkid = wpa_supplicant_remove_pmkid;
  188. ctx->set_config_blob = wpa_supplicant_set_config_blob;
  189. ctx->get_config_blob = wpa_supplicant_get_config_blob;
  190. ctx->mlme_setprotection = wpa_supplicant_mlme_setprotection;
  191. wpa_s->wpa = wpa_sm_init(ctx);
  192. assert(wpa_s->wpa != NULL);
  193. wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_PROTO, WPA_PROTO_RSN);
  194. os_strlcpy(wpa_s->ifname, ifname, sizeof(wpa_s->ifname));
  195. wpa_sm_set_ifname(wpa_s->wpa, wpa_s->ifname, NULL);
  196. l2 = l2_packet_init(wpa_s->ifname, NULL, ETH_P_RSN_PREAUTH, NULL,
  197. NULL, 0);
  198. assert(l2 != NULL);
  199. if (l2_packet_get_own_addr(l2, wpa_s->own_addr)) {
  200. wpa_printf(MSG_WARNING, "Failed to get own L2 address\n");
  201. exit(-1);
  202. }
  203. l2_packet_deinit(l2);
  204. wpa_sm_set_own_addr(wpa_s->wpa, wpa_s->own_addr);
  205. }
  206. static void eapol_test_terminate(int sig, void *signal_ctx)
  207. {
  208. struct wpa_supplicant *wpa_s = signal_ctx;
  209. wpa_msg(wpa_s, MSG_INFO, "Signal %d received - terminating", sig);
  210. eloop_terminate();
  211. }
  212. int main(int argc, char *argv[])
  213. {
  214. struct wpa_supplicant wpa_s;
  215. int ret = 1;
  216. u8 bssid[ETH_ALEN];
  217. struct preauth_test_data preauth_test;
  218. if (os_program_init())
  219. return -1;
  220. os_memset(&preauth_test, 0, sizeof(preauth_test));
  221. wpa_debug_level = 0;
  222. wpa_debug_show_keys = 1;
  223. if (argc != 4) {
  224. printf("usage: preauth_test <conf> <target MAC address> "
  225. "<ifname>\n");
  226. return -1;
  227. }
  228. if (hwaddr_aton(argv[2], bssid)) {
  229. printf("Failed to parse target address '%s'.\n", argv[2]);
  230. return -1;
  231. }
  232. if (eap_register_methods()) {
  233. wpa_printf(MSG_ERROR, "Failed to register EAP methods");
  234. return -1;
  235. }
  236. if (eloop_init()) {
  237. wpa_printf(MSG_ERROR, "Failed to initialize event loop");
  238. return -1;
  239. }
  240. os_memset(&wpa_s, 0, sizeof(wpa_s));
  241. wpa_s.conf = wpa_config_read(argv[1], NULL);
  242. if (wpa_s.conf == NULL) {
  243. printf("Failed to parse configuration file '%s'.\n", argv[1]);
  244. return -1;
  245. }
  246. if (wpa_s.conf->ssid == NULL) {
  247. printf("No networks defined.\n");
  248. return -1;
  249. }
  250. wpa_init_conf(&wpa_s, argv[3]);
  251. wpa_s.ctrl_iface = wpa_supplicant_ctrl_iface_init(&wpa_s);
  252. if (wpa_s.ctrl_iface == NULL) {
  253. printf("Failed to initialize control interface '%s'.\n"
  254. "You may have another preauth_test process already "
  255. "running or the file was\n"
  256. "left by an unclean termination of preauth_test in "
  257. "which case you will need\n"
  258. "to manually remove this file before starting "
  259. "preauth_test again.\n",
  260. wpa_s.conf->ctrl_interface);
  261. return -1;
  262. }
  263. if (wpa_supplicant_scard_init(&wpa_s, wpa_s.conf->ssid))
  264. return -1;
  265. if (rsn_preauth_init(wpa_s.wpa, bssid, &wpa_s.conf->ssid->eap))
  266. return -1;
  267. eloop_register_timeout(30, 0, eapol_test_timeout, &preauth_test, NULL);
  268. eloop_register_timeout(0, 100000, eapol_test_poll, &wpa_s, NULL);
  269. eloop_register_signal_terminate(eapol_test_terminate, &wpa_s);
  270. eloop_register_signal_reconfig(eapol_test_terminate, &wpa_s);
  271. eloop_run();
  272. if (preauth_test.auth_timed_out)
  273. ret = -2;
  274. else {
  275. ret = pmksa_cache_set_current(wpa_s.wpa, NULL, bssid, NULL, 0,
  276. NULL) ? 0 : -3;
  277. }
  278. test_eapol_clean(&wpa_s);
  279. eap_peer_unregister_methods();
  280. eloop_destroy();
  281. os_program_deinit();
  282. return ret;
  283. }