test_wpa.c 8.5 KB

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