test_wpa.c 8.3 KB

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