test_wpa.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. /*
  2. * Test program for combined WPA authenticator/supplicant
  3. * Copyright (c) 2006-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. #include "includes.h"
  15. #include "common.h"
  16. #include "eloop.h"
  17. #include "ieee802_11_defs.h"
  18. #include "config.h"
  19. #include "wpa.h"
  20. #include "wpa_ie.h"
  21. #include "../hostapd/wpa.h"
  22. extern int wpa_debug_level;
  23. extern int wpa_debug_show_keys;
  24. struct wpa {
  25. u8 auth_addr[ETH_ALEN];
  26. u8 supp_addr[ETH_ALEN];
  27. u8 psk[PMK_LEN];
  28. /* from authenticator */
  29. u8 auth_eapol_dst[ETH_ALEN];
  30. u8 *auth_eapol;
  31. size_t auth_eapol_len;
  32. /* from supplicant */
  33. u8 *supp_eapol;
  34. size_t supp_eapol_len;
  35. struct wpa_sm *supp;
  36. struct wpa_authenticator *auth_group;
  37. struct wpa_state_machine *auth;
  38. struct wpa_ssid ssid;
  39. u8 supp_ie[80];
  40. size_t supp_ie_len;
  41. };
  42. static struct wpa_ssid * supp_get_ssid(void *ctx)
  43. {
  44. struct wpa *wpa = ctx;
  45. wpa_printf(MSG_DEBUG, "SUPP: %s", __func__);
  46. return &wpa->ssid;
  47. }
  48. static int supp_get_bssid(void *ctx, u8 *bssid)
  49. {
  50. struct wpa *wpa = ctx;
  51. wpa_printf(MSG_DEBUG, "SUPP: %s", __func__);
  52. os_memcpy(bssid, wpa->auth_addr, ETH_ALEN);
  53. return 0;
  54. }
  55. static void supp_set_state(void *ctx, wpa_states state)
  56. {
  57. wpa_printf(MSG_DEBUG, "SUPP: %s(state=%d)", __func__, state);
  58. }
  59. static void auth_eapol_rx(void *eloop_data, void *user_ctx)
  60. {
  61. struct wpa *wpa = eloop_data;
  62. wpa_printf(MSG_DEBUG, "AUTH: RX EAPOL frame");
  63. wpa_receive(wpa->auth_group, wpa->auth, wpa->supp_eapol,
  64. wpa->supp_eapol_len);
  65. }
  66. static int supp_ether_send(void *ctx, const u8 *dest, u16 proto, const u8 *buf,
  67. size_t len)
  68. {
  69. struct wpa *wpa = ctx;
  70. wpa_printf(MSG_DEBUG, "SUPP: %s(dest=" MACSTR " proto=0x%04x "
  71. "len=%lu)",
  72. __func__, MAC2STR(dest), proto, (unsigned long) len);
  73. os_free(wpa->supp_eapol);
  74. wpa->supp_eapol = os_malloc(len);
  75. if (wpa->supp_eapol == NULL)
  76. return -1;
  77. os_memcpy(wpa->supp_eapol, buf, len);
  78. wpa->supp_eapol_len = len;
  79. eloop_register_timeout(0, 0, auth_eapol_rx, wpa, NULL);
  80. return 0;
  81. }
  82. static u8 * supp_alloc_eapol(void *ctx, u8 type, const void *data,
  83. u16 data_len, size_t *msg_len, void **data_pos)
  84. {
  85. struct ieee802_1x_hdr *hdr;
  86. wpa_printf(MSG_DEBUG, "SUPP: %s(type=%d data_len=%d)",
  87. __func__, type, data_len);
  88. *msg_len = sizeof(*hdr) + data_len;
  89. hdr = os_malloc(*msg_len);
  90. if (hdr == NULL)
  91. return NULL;
  92. hdr->version = 2;
  93. hdr->type = type;
  94. hdr->length = host_to_be16(data_len);
  95. if (data)
  96. os_memcpy(hdr + 1, data, data_len);
  97. else
  98. os_memset(hdr + 1, 0, data_len);
  99. if (data_pos)
  100. *data_pos = hdr + 1;
  101. return (u8 *) hdr;
  102. }
  103. static int supp_get_beacon_ie(void *ctx)
  104. {
  105. struct wpa *wpa = ctx;
  106. const u8 *ie;
  107. size_t ielen;
  108. wpa_printf(MSG_DEBUG, "SUPP: %s", __func__);
  109. ie = wpa_auth_get_wpa_ie(wpa->auth_group, &ielen);
  110. if (ie == NULL || ielen < 1)
  111. return -1;
  112. if (ie[0] == WLAN_EID_RSN)
  113. return wpa_sm_set_ap_rsn_ie(wpa->supp, ie, 2 + ie[1]);
  114. return wpa_sm_set_ap_wpa_ie(wpa->supp, ie, 2 + ie[1]);
  115. }
  116. static int supp_set_key(void *ctx, wpa_alg alg,
  117. const u8 *addr, int key_idx, int set_tx,
  118. const u8 *seq, size_t seq_len,
  119. const u8 *key, size_t key_len)
  120. {
  121. wpa_printf(MSG_DEBUG, "SUPP: %s(alg=%d addr=" MACSTR " key_idx=%d "
  122. "set_tx=%d)",
  123. __func__, alg, MAC2STR(addr), key_idx, set_tx);
  124. wpa_hexdump(MSG_DEBUG, "SUPP: set_key - seq", seq, seq_len);
  125. wpa_hexdump(MSG_DEBUG, "SUPP: set_key - key", key, key_len);
  126. return 0;
  127. }
  128. static int supp_mlme_setprotection(void *ctx, const u8 *addr,
  129. int protection_type, int key_type)
  130. {
  131. wpa_printf(MSG_DEBUG, "SUPP: %s(addr=" MACSTR " protection_type=%d "
  132. "key_type=%d)",
  133. __func__, MAC2STR(addr), protection_type, key_type);
  134. return 0;
  135. }
  136. static void supp_cancel_scan(void *ctx)
  137. {
  138. wpa_printf(MSG_DEBUG, "SUPP: %s", __func__);
  139. }
  140. static void supp_cancel_auth_timeout(void *ctx)
  141. {
  142. wpa_printf(MSG_DEBUG, "SUPP: %s", __func__);
  143. }
  144. static int supp_init(struct wpa *wpa)
  145. {
  146. struct wpa_sm_ctx *ctx = os_zalloc(sizeof(*ctx));
  147. if (ctx == NULL)
  148. return -1;
  149. ctx->ctx = wpa;
  150. ctx->set_state = supp_set_state;
  151. ctx->get_ssid = supp_get_ssid;
  152. ctx->get_bssid = supp_get_bssid;
  153. ctx->ether_send = supp_ether_send;
  154. ctx->get_beacon_ie = supp_get_beacon_ie;
  155. ctx->alloc_eapol = supp_alloc_eapol;
  156. ctx->set_key = supp_set_key;
  157. ctx->mlme_setprotection = supp_mlme_setprotection;
  158. ctx->cancel_scan = supp_cancel_scan;
  159. ctx->cancel_auth_timeout = supp_cancel_auth_timeout;
  160. wpa->supp = wpa_sm_init(ctx);
  161. if (wpa->supp == NULL) {
  162. wpa_printf(MSG_DEBUG, "SUPP: wpa_sm_init() failed");
  163. return -1;
  164. }
  165. wpa_sm_set_own_addr(wpa->supp, wpa->supp_addr);
  166. wpa_sm_set_param(wpa->supp, WPA_PARAM_RSN_ENABLED, 1);
  167. wpa_sm_set_param(wpa->supp, WPA_PARAM_PROTO, WPA_PROTO_RSN);
  168. wpa_sm_set_param(wpa->supp, WPA_PARAM_PAIRWISE, WPA_CIPHER_CCMP);
  169. wpa_sm_set_param(wpa->supp, WPA_PARAM_GROUP, WPA_CIPHER_CCMP);
  170. wpa_sm_set_param(wpa->supp, WPA_PARAM_KEY_MGMT, WPA_KEY_MGMT_PSK);
  171. wpa_sm_set_pmk(wpa->supp, wpa->psk, PMK_LEN);
  172. wpa->supp_ie_len = sizeof(wpa->supp_ie);
  173. if (wpa_sm_set_assoc_wpa_ie_default(wpa->supp, wpa->supp_ie,
  174. &wpa->supp_ie_len) < 0) {
  175. wpa_printf(MSG_DEBUG, "SUPP: wpa_sm_set_assoc_wpa_ie_default()"
  176. " failed");
  177. return -1;
  178. }
  179. wpa_sm_notify_assoc(wpa->supp, wpa->auth_addr);
  180. return 0;
  181. }
  182. static void auth_logger(void *ctx, const u8 *addr, logger_level level,
  183. const char *txt)
  184. {
  185. if (addr)
  186. wpa_printf(MSG_DEBUG, "AUTH: " MACSTR " - %s",
  187. MAC2STR(addr), txt);
  188. else
  189. wpa_printf(MSG_DEBUG, "AUTH: %s", txt);
  190. }
  191. static void supp_eapol_rx(void *eloop_data, void *user_ctx)
  192. {
  193. struct wpa *wpa = eloop_data;
  194. wpa_printf(MSG_DEBUG, "SUPP: RX EAPOL frame");
  195. wpa_sm_rx_eapol(wpa->supp, wpa->auth_addr, wpa->auth_eapol,
  196. wpa->auth_eapol_len);
  197. }
  198. static int auth_send_eapol(void *ctx, const u8 *addr, const u8 *data,
  199. size_t data_len, int encrypt)
  200. {
  201. struct wpa *wpa = ctx;
  202. wpa_printf(MSG_DEBUG, "AUTH: %s(addr=" MACSTR " data_len=%lu "
  203. "encrypt=%d)",
  204. __func__, MAC2STR(addr), (unsigned long) data_len, encrypt);
  205. os_free(wpa->auth_eapol);
  206. wpa->auth_eapol = os_malloc(data_len);
  207. if (wpa->auth_eapol == NULL)
  208. return -1;
  209. os_memcpy(wpa->auth_eapol_dst, addr, ETH_ALEN);
  210. os_memcpy(wpa->auth_eapol, data, data_len);
  211. wpa->auth_eapol_len = data_len;
  212. eloop_register_timeout(0, 0, supp_eapol_rx, wpa, NULL);
  213. return 0;
  214. }
  215. static const u8 * auth_get_psk(void *ctx, const u8 *addr, const u8 *prev_psk)
  216. {
  217. struct wpa *wpa = ctx;
  218. wpa_printf(MSG_DEBUG, "AUTH: %s (addr=" MACSTR " prev_psk=%p)",
  219. __func__, MAC2STR(addr), prev_psk);
  220. if (prev_psk)
  221. return NULL;
  222. return wpa->psk;
  223. }
  224. static int auth_init_group(struct wpa *wpa)
  225. {
  226. struct wpa_auth_config conf;
  227. struct wpa_auth_callbacks cb;
  228. wpa_printf(MSG_DEBUG, "AUTH: Initializing group state machine");
  229. os_memset(&conf, 0, sizeof(conf));
  230. conf.wpa = 2;
  231. conf.wpa_key_mgmt = WPA_KEY_MGMT_PSK;
  232. conf.wpa_pairwise = WPA_CIPHER_CCMP;
  233. conf.rsn_pairwise = WPA_CIPHER_CCMP;
  234. conf.wpa_group = WPA_CIPHER_CCMP;
  235. conf.eapol_version = 2;
  236. os_memset(&cb, 0, sizeof(cb));
  237. cb.ctx = wpa;
  238. cb.logger = auth_logger;
  239. cb.send_eapol = auth_send_eapol;
  240. cb.get_psk = auth_get_psk;
  241. wpa->auth_group = wpa_init(wpa->auth_addr, &conf, &cb);
  242. if (wpa->auth_group == NULL) {
  243. wpa_printf(MSG_DEBUG, "AUTH: wpa_init() failed");
  244. return -1;
  245. }
  246. return 0;
  247. }
  248. static int auth_init(struct wpa *wpa)
  249. {
  250. wpa->auth = wpa_auth_sta_init(wpa->auth_group, wpa->supp_addr);
  251. if (wpa->auth == NULL) {
  252. wpa_printf(MSG_DEBUG, "AUTH: wpa_auth_sta_init() failed");
  253. return -1;
  254. }
  255. if (wpa_validate_wpa_ie(wpa->auth_group, wpa->auth, wpa->supp_ie,
  256. wpa->supp_ie_len, NULL, 0) != WPA_IE_OK) {
  257. wpa_printf(MSG_DEBUG, "AUTH: wpa_validate_wpa_ie() failed");
  258. return -1;
  259. }
  260. wpa_auth_sm_event(wpa->auth, WPA_ASSOC);
  261. wpa_auth_sta_associated(wpa->auth_group, wpa->auth);
  262. return 0;
  263. }
  264. static void deinit(struct wpa *wpa)
  265. {
  266. wpa_auth_sta_deinit(wpa->auth);
  267. wpa_sm_deinit(wpa->supp);
  268. wpa_deinit(wpa->auth_group);
  269. os_free(wpa->auth_eapol);
  270. wpa->auth_eapol = NULL;
  271. os_free(wpa->supp_eapol);
  272. wpa->supp_eapol = NULL;
  273. }
  274. int main(int argc, char *argv[])
  275. {
  276. struct wpa wpa;
  277. if (os_program_init())
  278. return -1;
  279. os_memset(&wpa, 0, sizeof(wpa));
  280. os_memset(wpa.auth_addr, 0x12, ETH_ALEN);
  281. os_memset(wpa.supp_addr, 0x32, ETH_ALEN);
  282. os_memset(wpa.psk, 0x44, PMK_LEN);
  283. wpa_debug_level = 0;
  284. wpa_debug_show_keys = 1;
  285. if (eloop_init(&wpa)) {
  286. wpa_printf(MSG_ERROR, "Failed to initialize event loop");
  287. return -1;
  288. }
  289. if (auth_init_group(&wpa) < 0)
  290. return -1;
  291. if (supp_init(&wpa) < 0)
  292. return -1;
  293. if (auth_init(&wpa) < 0)
  294. return -1;
  295. wpa_printf(MSG_DEBUG, "Starting eloop");
  296. eloop_run();
  297. wpa_printf(MSG_DEBUG, "eloop done");
  298. deinit(&wpa);
  299. eloop_destroy();
  300. os_program_deinit();
  301. return 0;
  302. }