test_wpa.c 8.3 KB

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