preauth_test.c 9.1 KB

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