eapol_test.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092
  1. /*
  2. * WPA Supplicant - test code
  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 "eap_peer/eap.h"
  23. #include "eloop.h"
  24. #include "wpa.h"
  25. #include "eap_peer/eap_i.h"
  26. #include "wpa_supplicant_i.h"
  27. #include "radius/radius.h"
  28. #include "radius/radius_client.h"
  29. #include "ctrl_iface.h"
  30. #include "pcsc_funcs.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 eapol_test_data {
  35. struct wpa_supplicant *wpa_s;
  36. int eapol_test_num_reauths;
  37. int no_mppe_keys;
  38. int num_mppe_ok, num_mppe_mismatch;
  39. u8 radius_identifier;
  40. struct radius_msg *last_recv_radius;
  41. struct in_addr own_ip_addr;
  42. struct radius_client_data *radius;
  43. struct hostapd_radius_servers *radius_conf;
  44. u8 *last_eap_radius; /* last received EAP Response from Authentication
  45. * Server */
  46. size_t last_eap_radius_len;
  47. u8 authenticator_pmk[PMK_LEN];
  48. size_t authenticator_pmk_len;
  49. int radius_access_accept_received;
  50. int radius_access_reject_received;
  51. int auth_timed_out;
  52. u8 *eap_identity;
  53. size_t eap_identity_len;
  54. char *connect_info;
  55. u8 own_addr[ETH_ALEN];
  56. int cui_flag;
  57. char *cui_str;
  58. };
  59. static struct eapol_test_data eapol_test;
  60. static void send_eap_request_identity(void *eloop_ctx, void *timeout_ctx);
  61. static void hostapd_logger_cb(void *ctx, const u8 *addr, unsigned int module,
  62. int level, const char *txt, size_t len)
  63. {
  64. if (addr)
  65. wpa_printf(MSG_DEBUG, "STA " MACSTR ": %s\n",
  66. MAC2STR(addr), txt);
  67. else
  68. wpa_printf(MSG_DEBUG, "%s", txt);
  69. }
  70. static void ieee802_1x_encapsulate_radius(struct eapol_test_data *e,
  71. const u8 *eap, size_t len)
  72. {
  73. struct radius_msg *msg;
  74. char buf[128];
  75. const struct eap_hdr *hdr;
  76. const u8 *pos;
  77. wpa_printf(MSG_DEBUG, "Encapsulating EAP message into a RADIUS "
  78. "packet");
  79. e->radius_identifier = radius_client_get_id(e->radius);
  80. msg = radius_msg_new(RADIUS_CODE_ACCESS_REQUEST,
  81. e->radius_identifier);
  82. if (msg == NULL) {
  83. printf("Could not create net RADIUS packet\n");
  84. return;
  85. }
  86. radius_msg_make_authenticator(msg, (u8 *) e, sizeof(*e));
  87. hdr = (const struct eap_hdr *) eap;
  88. pos = (const u8 *) (hdr + 1);
  89. if (len > sizeof(*hdr) && hdr->code == EAP_CODE_RESPONSE &&
  90. pos[0] == EAP_TYPE_IDENTITY) {
  91. pos++;
  92. os_free(e->eap_identity);
  93. e->eap_identity_len = len - sizeof(*hdr) - 1;
  94. e->eap_identity = os_malloc(e->eap_identity_len);
  95. if (e->eap_identity) {
  96. os_memcpy(e->eap_identity, pos, e->eap_identity_len);
  97. wpa_hexdump(MSG_DEBUG, "Learned identity from "
  98. "EAP-Response-Identity",
  99. e->eap_identity, e->eap_identity_len);
  100. }
  101. }
  102. if (e->eap_identity &&
  103. !radius_msg_add_attr(msg, RADIUS_ATTR_USER_NAME,
  104. e->eap_identity, e->eap_identity_len)) {
  105. printf("Could not add User-Name\n");
  106. goto fail;
  107. }
  108. if (!radius_msg_add_attr(msg, RADIUS_ATTR_NAS_IP_ADDRESS,
  109. (u8 *) &e->own_ip_addr, 4)) {
  110. printf("Could not add NAS-IP-Address\n");
  111. goto fail;
  112. }
  113. os_snprintf(buf, sizeof(buf), RADIUS_802_1X_ADDR_FORMAT,
  114. MAC2STR(e->wpa_s->own_addr));
  115. if (!radius_msg_add_attr(msg, RADIUS_ATTR_CALLING_STATION_ID,
  116. (u8 *) buf, os_strlen(buf))) {
  117. printf("Could not add Calling-Station-Id\n");
  118. goto fail;
  119. }
  120. /* TODO: should probably check MTU from driver config; 2304 is max for
  121. * IEEE 802.11, but use 1400 to avoid problems with too large packets
  122. */
  123. if (!radius_msg_add_attr_int32(msg, RADIUS_ATTR_FRAMED_MTU, 1400)) {
  124. printf("Could not add Framed-MTU\n");
  125. goto fail;
  126. }
  127. if (!radius_msg_add_attr_int32(msg, RADIUS_ATTR_NAS_PORT_TYPE,
  128. RADIUS_NAS_PORT_TYPE_IEEE_802_11)) {
  129. printf("Could not add NAS-Port-Type\n");
  130. goto fail;
  131. }
  132. os_snprintf(buf, sizeof(buf), "%s", e->connect_info);
  133. if (!radius_msg_add_attr(msg, RADIUS_ATTR_CONNECT_INFO,
  134. (u8 *) buf, os_strlen(buf))) {
  135. printf("Could not add Connect-Info\n");
  136. goto fail;
  137. }
  138. if (e->cui_flag) {
  139. int l = 0;
  140. if (e->cui_flag == 1) {
  141. l = 1;
  142. buf[0] = '\0';
  143. } else if (e->cui_flag == 2) {
  144. os_snprintf(buf, sizeof(buf), "%s", e->cui_str);
  145. l = os_strlen(buf);
  146. }
  147. if (!radius_msg_add_attr(msg,
  148. RADIUS_ATTR_CHARGEABLE_USER_IDENTITY,
  149. (u8 *) buf, l)) {
  150. printf("Could not add Chargeable-User-Identity\n");
  151. goto fail;
  152. }
  153. }
  154. if (eap && !radius_msg_add_eap(msg, eap, len)) {
  155. printf("Could not add EAP-Message\n");
  156. goto fail;
  157. }
  158. /* State attribute must be copied if and only if this packet is
  159. * Access-Request reply to the previous Access-Challenge */
  160. if (e->last_recv_radius && e->last_recv_radius->hdr->code ==
  161. RADIUS_CODE_ACCESS_CHALLENGE) {
  162. int res = radius_msg_copy_attr(msg, e->last_recv_radius,
  163. RADIUS_ATTR_STATE);
  164. if (res < 0) {
  165. printf("Could not copy State attribute from previous "
  166. "Access-Challenge\n");
  167. goto fail;
  168. }
  169. if (res > 0) {
  170. wpa_printf(MSG_DEBUG, " Copied RADIUS State "
  171. "Attribute");
  172. }
  173. }
  174. radius_client_send(e->radius, msg, RADIUS_AUTH, e->wpa_s->own_addr);
  175. return;
  176. fail:
  177. radius_msg_free(msg);
  178. os_free(msg);
  179. }
  180. static int eapol_test_eapol_send(void *ctx, int type, const u8 *buf,
  181. size_t len)
  182. {
  183. /* struct wpa_supplicant *wpa_s = ctx; */
  184. printf("WPA: eapol_test_eapol_send(type=%d len=%lu)\n",
  185. type, (unsigned long) len);
  186. if (type == IEEE802_1X_TYPE_EAP_PACKET) {
  187. wpa_hexdump(MSG_DEBUG, "TX EAP -> RADIUS", buf, len);
  188. ieee802_1x_encapsulate_radius(&eapol_test, buf, len);
  189. }
  190. return 0;
  191. }
  192. static void eapol_test_set_config_blob(void *ctx,
  193. struct wpa_config_blob *blob)
  194. {
  195. struct wpa_supplicant *wpa_s = ctx;
  196. wpa_config_set_blob(wpa_s->conf, blob);
  197. }
  198. static const struct wpa_config_blob *
  199. eapol_test_get_config_blob(void *ctx, const char *name)
  200. {
  201. struct wpa_supplicant *wpa_s = ctx;
  202. return wpa_config_get_blob(wpa_s->conf, name);
  203. }
  204. static void eapol_test_eapol_done_cb(void *ctx)
  205. {
  206. printf("WPA: EAPOL processing complete\n");
  207. }
  208. static void eapol_sm_reauth(void *eloop_ctx, void *timeout_ctx)
  209. {
  210. struct eapol_test_data *e = eloop_ctx;
  211. printf("\n\n\n\n\neapol_test: Triggering EAP reauthentication\n\n");
  212. e->radius_access_accept_received = 0;
  213. send_eap_request_identity(e->wpa_s, NULL);
  214. }
  215. static int eapol_test_compare_pmk(struct eapol_test_data *e)
  216. {
  217. u8 pmk[PMK_LEN];
  218. int ret = 1;
  219. if (eapol_sm_get_key(e->wpa_s->eapol, pmk, PMK_LEN) == 0) {
  220. wpa_hexdump(MSG_DEBUG, "PMK from EAPOL", pmk, PMK_LEN);
  221. if (os_memcmp(pmk, e->authenticator_pmk, PMK_LEN) != 0) {
  222. printf("WARNING: PMK mismatch\n");
  223. wpa_hexdump(MSG_DEBUG, "PMK from AS",
  224. e->authenticator_pmk, PMK_LEN);
  225. } else if (e->radius_access_accept_received)
  226. ret = 0;
  227. } else if (e->authenticator_pmk_len == 16 &&
  228. eapol_sm_get_key(e->wpa_s->eapol, pmk, 16) == 0) {
  229. wpa_hexdump(MSG_DEBUG, "LEAP PMK from EAPOL", pmk, 16);
  230. if (os_memcmp(pmk, e->authenticator_pmk, 16) != 0) {
  231. printf("WARNING: PMK mismatch\n");
  232. wpa_hexdump(MSG_DEBUG, "PMK from AS",
  233. e->authenticator_pmk, 16);
  234. } else if (e->radius_access_accept_received)
  235. ret = 0;
  236. } else if (e->radius_access_accept_received && e->no_mppe_keys) {
  237. /* No keying material expected */
  238. ret = 0;
  239. }
  240. if (ret && !e->no_mppe_keys)
  241. e->num_mppe_mismatch++;
  242. else if (!e->no_mppe_keys)
  243. e->num_mppe_ok++;
  244. return ret;
  245. }
  246. static void eapol_sm_cb(struct eapol_sm *eapol, int success, void *ctx)
  247. {
  248. struct eapol_test_data *e = ctx;
  249. printf("eapol_sm_cb: success=%d\n", success);
  250. e->eapol_test_num_reauths--;
  251. if (e->eapol_test_num_reauths < 0)
  252. eloop_terminate();
  253. else {
  254. eapol_test_compare_pmk(e);
  255. eloop_register_timeout(0, 100000, eapol_sm_reauth, e, NULL);
  256. }
  257. }
  258. static int test_eapol(struct eapol_test_data *e, struct wpa_supplicant *wpa_s,
  259. struct wpa_ssid *ssid)
  260. {
  261. struct eapol_config eapol_conf;
  262. struct eapol_ctx *ctx;
  263. ctx = os_zalloc(sizeof(*ctx));
  264. if (ctx == NULL) {
  265. printf("Failed to allocate EAPOL context.\n");
  266. return -1;
  267. }
  268. ctx->ctx = wpa_s;
  269. ctx->msg_ctx = wpa_s;
  270. ctx->scard_ctx = wpa_s->scard;
  271. ctx->cb = eapol_sm_cb;
  272. ctx->cb_ctx = e;
  273. ctx->eapol_send_ctx = wpa_s;
  274. ctx->preauth = 0;
  275. ctx->eapol_done_cb = eapol_test_eapol_done_cb;
  276. ctx->eapol_send = eapol_test_eapol_send;
  277. ctx->set_config_blob = eapol_test_set_config_blob;
  278. ctx->get_config_blob = eapol_test_get_config_blob;
  279. #ifdef EAP_TLS_OPENSSL
  280. ctx->opensc_engine_path = wpa_s->conf->opensc_engine_path;
  281. ctx->pkcs11_engine_path = wpa_s->conf->pkcs11_engine_path;
  282. ctx->pkcs11_module_path = wpa_s->conf->pkcs11_module_path;
  283. #endif /* EAP_TLS_OPENSSL */
  284. ctx->mac_addr = wpa_s->own_addr;
  285. wpa_s->eapol = eapol_sm_init(ctx);
  286. if (wpa_s->eapol == NULL) {
  287. os_free(ctx);
  288. printf("Failed to initialize EAPOL state machines.\n");
  289. return -1;
  290. }
  291. wpa_s->current_ssid = ssid;
  292. os_memset(&eapol_conf, 0, sizeof(eapol_conf));
  293. eapol_conf.accept_802_1x_keys = 1;
  294. eapol_conf.required_keys = 0;
  295. eapol_conf.fast_reauth = wpa_s->conf->fast_reauth;
  296. eapol_conf.workaround = ssid->eap_workaround;
  297. eapol_sm_notify_config(wpa_s->eapol, &ssid->eap, &eapol_conf);
  298. eapol_sm_register_scard_ctx(wpa_s->eapol, wpa_s->scard);
  299. eapol_sm_notify_portValid(wpa_s->eapol, FALSE);
  300. /* 802.1X::portControl = Auto */
  301. eapol_sm_notify_portEnabled(wpa_s->eapol, TRUE);
  302. return 0;
  303. }
  304. static void test_eapol_clean(struct eapol_test_data *e,
  305. struct wpa_supplicant *wpa_s)
  306. {
  307. radius_client_deinit(e->radius);
  308. os_free(e->last_eap_radius);
  309. if (e->last_recv_radius) {
  310. radius_msg_free(e->last_recv_radius);
  311. os_free(e->last_recv_radius);
  312. }
  313. os_free(e->eap_identity);
  314. e->eap_identity = NULL;
  315. eapol_sm_deinit(wpa_s->eapol);
  316. wpa_s->eapol = NULL;
  317. if (e->radius_conf && e->radius_conf->auth_server) {
  318. os_free(e->radius_conf->auth_server->shared_secret);
  319. os_free(e->radius_conf->auth_server);
  320. }
  321. os_free(e->radius_conf);
  322. e->radius_conf = NULL;
  323. scard_deinit(wpa_s->scard);
  324. if (wpa_s->ctrl_iface) {
  325. wpa_supplicant_ctrl_iface_deinit(wpa_s->ctrl_iface);
  326. wpa_s->ctrl_iface = NULL;
  327. }
  328. wpa_config_free(wpa_s->conf);
  329. }
  330. static void send_eap_request_identity(void *eloop_ctx, void *timeout_ctx)
  331. {
  332. struct wpa_supplicant *wpa_s = eloop_ctx;
  333. u8 buf[100], *pos;
  334. struct ieee802_1x_hdr *hdr;
  335. struct eap_hdr *eap;
  336. hdr = (struct ieee802_1x_hdr *) buf;
  337. hdr->version = EAPOL_VERSION;
  338. hdr->type = IEEE802_1X_TYPE_EAP_PACKET;
  339. hdr->length = htons(5);
  340. eap = (struct eap_hdr *) (hdr + 1);
  341. eap->code = EAP_CODE_REQUEST;
  342. eap->identifier = 0;
  343. eap->length = htons(5);
  344. pos = (u8 *) (eap + 1);
  345. *pos = EAP_TYPE_IDENTITY;
  346. printf("Sending fake EAP-Request-Identity\n");
  347. eapol_sm_rx_eapol(wpa_s->eapol, wpa_s->bssid, buf,
  348. sizeof(*hdr) + 5);
  349. }
  350. static void eapol_test_timeout(void *eloop_ctx, void *timeout_ctx)
  351. {
  352. struct eapol_test_data *e = eloop_ctx;
  353. printf("EAPOL test timed out\n");
  354. e->auth_timed_out = 1;
  355. eloop_terminate();
  356. }
  357. static char *eap_type_text(u8 type)
  358. {
  359. switch (type) {
  360. case EAP_TYPE_IDENTITY: return "Identity";
  361. case EAP_TYPE_NOTIFICATION: return "Notification";
  362. case EAP_TYPE_NAK: return "Nak";
  363. case EAP_TYPE_TLS: return "TLS";
  364. case EAP_TYPE_TTLS: return "TTLS";
  365. case EAP_TYPE_PEAP: return "PEAP";
  366. case EAP_TYPE_SIM: return "SIM";
  367. case EAP_TYPE_GTC: return "GTC";
  368. case EAP_TYPE_MD5: return "MD5";
  369. case EAP_TYPE_OTP: return "OTP";
  370. case EAP_TYPE_FAST: return "FAST";
  371. case EAP_TYPE_SAKE: return "SAKE";
  372. case EAP_TYPE_PSK: return "PSK";
  373. default: return "Unknown";
  374. }
  375. }
  376. static void ieee802_1x_decapsulate_radius(struct eapol_test_data *e)
  377. {
  378. u8 *eap;
  379. size_t len;
  380. struct eap_hdr *hdr;
  381. int eap_type = -1;
  382. char buf[64];
  383. struct radius_msg *msg;
  384. if (e->last_recv_radius == NULL)
  385. return;
  386. msg = e->last_recv_radius;
  387. eap = radius_msg_get_eap(msg, &len);
  388. if (eap == NULL) {
  389. /* draft-aboba-radius-rfc2869bis-20.txt, Chap. 2.6.3:
  390. * RADIUS server SHOULD NOT send Access-Reject/no EAP-Message
  391. * attribute */
  392. wpa_printf(MSG_DEBUG, "could not extract "
  393. "EAP-Message from RADIUS message");
  394. os_free(e->last_eap_radius);
  395. e->last_eap_radius = NULL;
  396. e->last_eap_radius_len = 0;
  397. return;
  398. }
  399. if (len < sizeof(*hdr)) {
  400. wpa_printf(MSG_DEBUG, "too short EAP packet "
  401. "received from authentication server");
  402. os_free(eap);
  403. return;
  404. }
  405. if (len > sizeof(*hdr))
  406. eap_type = eap[sizeof(*hdr)];
  407. hdr = (struct eap_hdr *) eap;
  408. switch (hdr->code) {
  409. case EAP_CODE_REQUEST:
  410. os_snprintf(buf, sizeof(buf), "EAP-Request-%s (%d)",
  411. eap_type >= 0 ? eap_type_text(eap_type) : "??",
  412. eap_type);
  413. break;
  414. case EAP_CODE_RESPONSE:
  415. os_snprintf(buf, sizeof(buf), "EAP Response-%s (%d)",
  416. eap_type >= 0 ? eap_type_text(eap_type) : "??",
  417. eap_type);
  418. break;
  419. case EAP_CODE_SUCCESS:
  420. os_strlcpy(buf, "EAP Success", sizeof(buf));
  421. /* LEAP uses EAP Success within an authentication, so must not
  422. * stop here with eloop_terminate(); */
  423. break;
  424. case EAP_CODE_FAILURE:
  425. os_strlcpy(buf, "EAP Failure", sizeof(buf));
  426. eloop_terminate();
  427. break;
  428. default:
  429. os_strlcpy(buf, "unknown EAP code", sizeof(buf));
  430. wpa_hexdump(MSG_DEBUG, "Decapsulated EAP packet", eap, len);
  431. break;
  432. }
  433. wpa_printf(MSG_DEBUG, "decapsulated EAP packet (code=%d "
  434. "id=%d len=%d) from RADIUS server: %s",
  435. hdr->code, hdr->identifier, ntohs(hdr->length), buf);
  436. /* sta->eapol_sm->be_auth.idFromServer = hdr->identifier; */
  437. os_free(e->last_eap_radius);
  438. e->last_eap_radius = eap;
  439. e->last_eap_radius_len = len;
  440. {
  441. struct ieee802_1x_hdr *dot1x;
  442. dot1x = os_malloc(sizeof(*dot1x) + len);
  443. assert(dot1x != NULL);
  444. dot1x->version = EAPOL_VERSION;
  445. dot1x->type = IEEE802_1X_TYPE_EAP_PACKET;
  446. dot1x->length = htons(len);
  447. os_memcpy((u8 *) (dot1x + 1), eap, len);
  448. eapol_sm_rx_eapol(e->wpa_s->eapol, e->wpa_s->bssid,
  449. (u8 *) dot1x, sizeof(*dot1x) + len);
  450. os_free(dot1x);
  451. }
  452. }
  453. static void ieee802_1x_get_keys(struct eapol_test_data *e,
  454. struct radius_msg *msg, struct radius_msg *req,
  455. u8 *shared_secret, size_t shared_secret_len)
  456. {
  457. struct radius_ms_mppe_keys *keys;
  458. keys = radius_msg_get_ms_keys(msg, req, shared_secret,
  459. shared_secret_len);
  460. if (keys && keys->send == NULL && keys->recv == NULL) {
  461. os_free(keys);
  462. keys = radius_msg_get_cisco_keys(msg, req, shared_secret,
  463. shared_secret_len);
  464. }
  465. if (keys) {
  466. if (keys->send) {
  467. wpa_hexdump(MSG_DEBUG, "MS-MPPE-Send-Key (sign)",
  468. keys->send, keys->send_len);
  469. }
  470. if (keys->recv) {
  471. wpa_hexdump(MSG_DEBUG, "MS-MPPE-Recv-Key (crypt)",
  472. keys->recv, keys->recv_len);
  473. e->authenticator_pmk_len =
  474. keys->recv_len > PMK_LEN ? PMK_LEN :
  475. keys->recv_len;
  476. os_memcpy(e->authenticator_pmk, keys->recv,
  477. e->authenticator_pmk_len);
  478. }
  479. os_free(keys->send);
  480. os_free(keys->recv);
  481. os_free(keys);
  482. }
  483. }
  484. /* Process the RADIUS frames from Authentication Server */
  485. static RadiusRxResult
  486. ieee802_1x_receive_auth(struct radius_msg *msg, struct radius_msg *req,
  487. u8 *shared_secret, size_t shared_secret_len,
  488. void *data)
  489. {
  490. struct eapol_test_data *e = data;
  491. /* RFC 2869, Ch. 5.13: valid Message-Authenticator attribute MUST be
  492. * present when packet contains an EAP-Message attribute */
  493. if (msg->hdr->code == RADIUS_CODE_ACCESS_REJECT &&
  494. radius_msg_get_attr(msg, RADIUS_ATTR_MESSAGE_AUTHENTICATOR, NULL,
  495. 0) < 0 &&
  496. radius_msg_get_attr(msg, RADIUS_ATTR_EAP_MESSAGE, NULL, 0) < 0) {
  497. wpa_printf(MSG_DEBUG, "Allowing RADIUS "
  498. "Access-Reject without Message-Authenticator "
  499. "since it does not include EAP-Message\n");
  500. } else if (radius_msg_verify(msg, shared_secret, shared_secret_len,
  501. req, 1)) {
  502. printf("Incoming RADIUS packet did not have correct "
  503. "Message-Authenticator - dropped\n");
  504. return RADIUS_RX_UNKNOWN;
  505. }
  506. if (msg->hdr->code != RADIUS_CODE_ACCESS_ACCEPT &&
  507. msg->hdr->code != RADIUS_CODE_ACCESS_REJECT &&
  508. msg->hdr->code != RADIUS_CODE_ACCESS_CHALLENGE) {
  509. printf("Unknown RADIUS message code\n");
  510. return RADIUS_RX_UNKNOWN;
  511. }
  512. e->radius_identifier = -1;
  513. wpa_printf(MSG_DEBUG, "RADIUS packet matching with station");
  514. if (e->last_recv_radius) {
  515. radius_msg_free(e->last_recv_radius);
  516. os_free(e->last_recv_radius);
  517. }
  518. e->last_recv_radius = msg;
  519. switch (msg->hdr->code) {
  520. case RADIUS_CODE_ACCESS_ACCEPT:
  521. e->radius_access_accept_received = 1;
  522. ieee802_1x_get_keys(e, msg, req, shared_secret,
  523. shared_secret_len);
  524. break;
  525. case RADIUS_CODE_ACCESS_REJECT:
  526. e->radius_access_reject_received = 1;
  527. break;
  528. }
  529. ieee802_1x_decapsulate_radius(e);
  530. if ((msg->hdr->code == RADIUS_CODE_ACCESS_ACCEPT &&
  531. e->eapol_test_num_reauths < 0) ||
  532. msg->hdr->code == RADIUS_CODE_ACCESS_REJECT) {
  533. eloop_terminate();
  534. }
  535. return RADIUS_RX_QUEUED;
  536. }
  537. static void wpa_init_conf(struct eapol_test_data *e,
  538. struct wpa_supplicant *wpa_s, const char *authsrv,
  539. int port, const char *secret,
  540. const char *cli_addr)
  541. {
  542. struct hostapd_radius_server *as;
  543. int res;
  544. wpa_s->bssid[5] = 1;
  545. os_memcpy(wpa_s->own_addr, e->own_addr, ETH_ALEN);
  546. e->own_ip_addr.s_addr = htonl((127 << 24) | 1);
  547. os_strlcpy(wpa_s->ifname, "test", sizeof(wpa_s->ifname));
  548. e->radius_conf = os_zalloc(sizeof(struct hostapd_radius_servers));
  549. assert(e->radius_conf != NULL);
  550. e->radius_conf->num_auth_servers = 1;
  551. as = os_zalloc(sizeof(struct hostapd_radius_server));
  552. assert(as != NULL);
  553. #if defined(CONFIG_NATIVE_WINDOWS) || defined(CONFIG_ANSI_C_EXTRA)
  554. {
  555. int a[4];
  556. u8 *pos;
  557. sscanf(authsrv, "%d.%d.%d.%d", &a[0], &a[1], &a[2], &a[3]);
  558. pos = (u8 *) &as->addr.u.v4;
  559. *pos++ = a[0];
  560. *pos++ = a[1];
  561. *pos++ = a[2];
  562. *pos++ = a[3];
  563. }
  564. #else /* CONFIG_NATIVE_WINDOWS or CONFIG_ANSI_C_EXTRA */
  565. inet_aton(authsrv, &as->addr.u.v4);
  566. #endif /* CONFIG_NATIVE_WINDOWS or CONFIG_ANSI_C_EXTRA */
  567. as->addr.af = AF_INET;
  568. as->port = port;
  569. as->shared_secret = (u8 *) os_strdup(secret);
  570. as->shared_secret_len = os_strlen(secret);
  571. e->radius_conf->auth_server = as;
  572. e->radius_conf->auth_servers = as;
  573. e->radius_conf->msg_dumps = 1;
  574. if (cli_addr) {
  575. if (hostapd_parse_ip_addr(cli_addr,
  576. &e->radius_conf->client_addr) == 0)
  577. e->radius_conf->force_client_addr = 1;
  578. else {
  579. wpa_printf(MSG_ERROR, "Invalid IP address '%s'",
  580. cli_addr);
  581. assert(0);
  582. }
  583. }
  584. e->radius = radius_client_init(wpa_s, e->radius_conf);
  585. assert(e->radius != NULL);
  586. res = radius_client_register(e->radius, RADIUS_AUTH,
  587. ieee802_1x_receive_auth, e);
  588. assert(res == 0);
  589. }
  590. static int scard_test(void)
  591. {
  592. struct scard_data *scard;
  593. size_t len;
  594. char imsi[20];
  595. unsigned char _rand[16];
  596. #ifdef PCSC_FUNCS
  597. unsigned char sres[4];
  598. unsigned char kc[8];
  599. #endif /* PCSC_FUNCS */
  600. #define num_triplets 5
  601. unsigned char rand_[num_triplets][16];
  602. unsigned char sres_[num_triplets][4];
  603. unsigned char kc_[num_triplets][8];
  604. int i, res;
  605. size_t j;
  606. #define AKA_RAND_LEN 16
  607. #define AKA_AUTN_LEN 16
  608. #define AKA_AUTS_LEN 14
  609. #define RES_MAX_LEN 16
  610. #define IK_LEN 16
  611. #define CK_LEN 16
  612. unsigned char aka_rand[AKA_RAND_LEN];
  613. unsigned char aka_autn[AKA_AUTN_LEN];
  614. unsigned char aka_auts[AKA_AUTS_LEN];
  615. unsigned char aka_res[RES_MAX_LEN];
  616. size_t aka_res_len;
  617. unsigned char aka_ik[IK_LEN];
  618. unsigned char aka_ck[CK_LEN];
  619. scard = scard_init(SCARD_TRY_BOTH);
  620. if (scard == NULL)
  621. return -1;
  622. if (scard_set_pin(scard, "1234")) {
  623. wpa_printf(MSG_WARNING, "PIN validation failed");
  624. scard_deinit(scard);
  625. return -1;
  626. }
  627. len = sizeof(imsi);
  628. if (scard_get_imsi(scard, imsi, &len))
  629. goto failed;
  630. wpa_hexdump_ascii(MSG_DEBUG, "SCARD: IMSI", (u8 *) imsi, len);
  631. /* NOTE: Permanent Username: 1 | IMSI */
  632. os_memset(_rand, 0, sizeof(_rand));
  633. if (scard_gsm_auth(scard, _rand, sres, kc))
  634. goto failed;
  635. os_memset(_rand, 0xff, sizeof(_rand));
  636. if (scard_gsm_auth(scard, _rand, sres, kc))
  637. goto failed;
  638. for (i = 0; i < num_triplets; i++) {
  639. os_memset(rand_[i], i, sizeof(rand_[i]));
  640. if (scard_gsm_auth(scard, rand_[i], sres_[i], kc_[i]))
  641. goto failed;
  642. }
  643. for (i = 0; i < num_triplets; i++) {
  644. printf("1");
  645. for (j = 0; j < len; j++)
  646. printf("%c", imsi[j]);
  647. printf(",");
  648. for (j = 0; j < 16; j++)
  649. printf("%02X", rand_[i][j]);
  650. printf(",");
  651. for (j = 0; j < 4; j++)
  652. printf("%02X", sres_[i][j]);
  653. printf(",");
  654. for (j = 0; j < 8; j++)
  655. printf("%02X", kc_[i][j]);
  656. printf("\n");
  657. }
  658. wpa_printf(MSG_DEBUG, "Trying to use UMTS authentication");
  659. /* seq 39 (0x28) */
  660. os_memset(aka_rand, 0xaa, 16);
  661. os_memcpy(aka_autn, "\x86\x71\x31\xcb\xa2\xfc\x61\xdf"
  662. "\xa3\xb3\x97\x9d\x07\x32\xa2\x12", 16);
  663. res = scard_umts_auth(scard, aka_rand, aka_autn, aka_res, &aka_res_len,
  664. aka_ik, aka_ck, aka_auts);
  665. if (res == 0) {
  666. wpa_printf(MSG_DEBUG, "UMTS auth completed successfully");
  667. wpa_hexdump(MSG_DEBUG, "RES", aka_res, aka_res_len);
  668. wpa_hexdump(MSG_DEBUG, "IK", aka_ik, IK_LEN);
  669. wpa_hexdump(MSG_DEBUG, "CK", aka_ck, CK_LEN);
  670. } else if (res == -2) {
  671. wpa_printf(MSG_DEBUG, "UMTS auth resulted in synchronization "
  672. "failure");
  673. wpa_hexdump(MSG_DEBUG, "AUTS", aka_auts, AKA_AUTS_LEN);
  674. } else {
  675. wpa_printf(MSG_DEBUG, "UMTS auth failed");
  676. }
  677. failed:
  678. scard_deinit(scard);
  679. return 0;
  680. #undef num_triplets
  681. }
  682. static int scard_get_triplets(int argc, char *argv[])
  683. {
  684. struct scard_data *scard;
  685. size_t len;
  686. char imsi[20];
  687. unsigned char _rand[16];
  688. unsigned char sres[4];
  689. unsigned char kc[8];
  690. int num_triplets;
  691. int i;
  692. size_t j;
  693. if (argc < 2 || ((num_triplets = atoi(argv[1])) <= 0)) {
  694. printf("invalid parameters for sim command\n");
  695. return -1;
  696. }
  697. if (argc <= 2 || os_strcmp(argv[2], "debug") != 0) {
  698. /* disable debug output */
  699. wpa_debug_level = 99;
  700. }
  701. scard = scard_init(SCARD_GSM_SIM_ONLY);
  702. if (scard == NULL) {
  703. printf("Failed to open smartcard connection\n");
  704. return -1;
  705. }
  706. if (scard_set_pin(scard, argv[0])) {
  707. wpa_printf(MSG_WARNING, "PIN validation failed");
  708. scard_deinit(scard);
  709. return -1;
  710. }
  711. len = sizeof(imsi);
  712. if (scard_get_imsi(scard, imsi, &len)) {
  713. scard_deinit(scard);
  714. return -1;
  715. }
  716. for (i = 0; i < num_triplets; i++) {
  717. os_memset(_rand, i, sizeof(_rand));
  718. if (scard_gsm_auth(scard, _rand, sres, kc))
  719. break;
  720. /* IMSI:Kc:SRES:RAND */
  721. for (j = 0; j < len; j++)
  722. printf("%c", imsi[j]);
  723. printf(":");
  724. for (j = 0; j < 8; j++)
  725. printf("%02X", kc[j]);
  726. printf(":");
  727. for (j = 0; j < 4; j++)
  728. printf("%02X", sres[j]);
  729. printf(":");
  730. for (j = 0; j < 16; j++)
  731. printf("%02X", _rand[j]);
  732. printf("\n");
  733. }
  734. scard_deinit(scard);
  735. return 0;
  736. }
  737. static void eapol_test_terminate(int sig, void *eloop_ctx,
  738. void *signal_ctx)
  739. {
  740. struct wpa_supplicant *wpa_s = eloop_ctx;
  741. wpa_msg(wpa_s, MSG_INFO, "Signal %d received - terminating", sig);
  742. eloop_terminate();
  743. }
  744. static void usage(void)
  745. {
  746. printf("usage:\n"
  747. "eapol_test [-nWS] -c<conf> [-a<AS IP>] [-p<AS port>] "
  748. "[-s<AS secret>]\\\n"
  749. " [-r<count>] [-t<timeout>] [-C<Connect-Info>] \\\n"
  750. " [-M<client MAC address>] \\\n"
  751. " [-I<CUI>] [-i] [-A<client IP>]\n"
  752. "eapol_test scard\n"
  753. "eapol_test sim <PIN> <num triplets> [debug]\n"
  754. "\n");
  755. printf("options:\n"
  756. " -c<conf> = configuration file\n"
  757. " -a<AS IP> = IP address of the authentication server, "
  758. "default 127.0.0.1\n"
  759. " -p<AS port> = UDP port of the authentication server, "
  760. "default 1812\n"
  761. " -s<AS secret> = shared secret with the authentication "
  762. "server, default 'radius'\n"
  763. " -A<client IP> = IP address of the client, default: select "
  764. "automatically\n"
  765. " -r<count> = number of re-authentications\n"
  766. " -W = wait for a control interface monitor before starting\n"
  767. " -S = save configuration after authentiation\n"
  768. " -n = no MPPE keys expected\n"
  769. " -t<timeout> = sets timeout in seconds (default: 30 s)\n"
  770. " -C<Connect-Info> = RADIUS Connect-Info (default: "
  771. "CONNECT 11Mbps 802.11b)\n"
  772. " -M<client MAC address> = Set own MAC address "
  773. "(Calling-Station-Id,\n"
  774. " default: 02:00:00:00:00:01)\n"
  775. " -I<CUI> = send Chargeable-User-Identity containing the "
  776. "value of CUI\n"
  777. " -i = send NUL value in Chargeable-User-Identity\n");
  778. }
  779. int main(int argc, char *argv[])
  780. {
  781. struct wpa_supplicant wpa_s;
  782. int c, ret = 1, wait_for_monitor = 0, save_config = 0;
  783. char *as_addr = "127.0.0.1";
  784. int as_port = 1812;
  785. char *as_secret = "radius";
  786. char *cli_addr = NULL;
  787. char *conf = NULL;
  788. int timeout = 30;
  789. if (os_program_init())
  790. return -1;
  791. hostapd_logger_register_cb(hostapd_logger_cb);
  792. os_memset(&eapol_test, 0, sizeof(eapol_test));
  793. eapol_test.connect_info = "CONNECT 11Mbps 802.11b";
  794. os_memcpy(eapol_test.own_addr, "\x02\x00\x00\x00\x00\x01", ETH_ALEN);
  795. wpa_debug_level = 0;
  796. wpa_debug_show_keys = 1;
  797. for (;;) {
  798. c = getopt(argc, argv, "a:A:c:C:iI:M:np:r:s:St:W");
  799. if (c < 0)
  800. break;
  801. switch (c) {
  802. case 'a':
  803. as_addr = optarg;
  804. break;
  805. case 'A':
  806. cli_addr = optarg;
  807. break;
  808. case 'c':
  809. conf = optarg;
  810. break;
  811. case 'C':
  812. eapol_test.connect_info = optarg;
  813. break;
  814. case 'i':
  815. eapol_test.cui_flag = 1;
  816. break;
  817. case 'I':
  818. eapol_test.cui_flag = 2;
  819. eapol_test.cui_str = optarg;
  820. break;
  821. case 'M':
  822. if (hwaddr_aton(optarg, eapol_test.own_addr)) {
  823. usage();
  824. return -1;
  825. }
  826. break;
  827. case 'n':
  828. eapol_test.no_mppe_keys++;
  829. break;
  830. case 'p':
  831. as_port = atoi(optarg);
  832. break;
  833. case 'r':
  834. eapol_test.eapol_test_num_reauths = atoi(optarg);
  835. break;
  836. case 's':
  837. as_secret = optarg;
  838. break;
  839. case 'S':
  840. save_config++;
  841. break;
  842. case 't':
  843. timeout = atoi(optarg);
  844. break;
  845. case 'W':
  846. wait_for_monitor++;
  847. break;
  848. default:
  849. usage();
  850. return -1;
  851. }
  852. }
  853. if (argc > optind && os_strcmp(argv[optind], "scard") == 0) {
  854. return scard_test();
  855. }
  856. if (argc > optind && os_strcmp(argv[optind], "sim") == 0) {
  857. return scard_get_triplets(argc - optind - 1,
  858. &argv[optind + 1]);
  859. }
  860. if (conf == NULL) {
  861. usage();
  862. printf("Configuration file is required.\n");
  863. return -1;
  864. }
  865. if (eap_peer_register_methods()) {
  866. wpa_printf(MSG_ERROR, "Failed to register EAP methods");
  867. return -1;
  868. }
  869. if (eloop_init(&wpa_s)) {
  870. wpa_printf(MSG_ERROR, "Failed to initialize event loop");
  871. return -1;
  872. }
  873. os_memset(&wpa_s, 0, sizeof(wpa_s));
  874. eapol_test.wpa_s = &wpa_s;
  875. wpa_s.conf = wpa_config_read(conf);
  876. if (wpa_s.conf == NULL) {
  877. printf("Failed to parse configuration file '%s'.\n", conf);
  878. return -1;
  879. }
  880. if (wpa_s.conf->ssid == NULL) {
  881. printf("No networks defined.\n");
  882. return -1;
  883. }
  884. wpa_init_conf(&eapol_test, &wpa_s, as_addr, as_port, as_secret,
  885. cli_addr);
  886. wpa_s.ctrl_iface = wpa_supplicant_ctrl_iface_init(&wpa_s);
  887. if (wpa_s.ctrl_iface == NULL) {
  888. printf("Failed to initialize control interface '%s'.\n"
  889. "You may have another eapol_test process already "
  890. "running or the file was\n"
  891. "left by an unclean termination of eapol_test in "
  892. "which case you will need\n"
  893. "to manually remove this file before starting "
  894. "eapol_test again.\n",
  895. wpa_s.conf->ctrl_interface);
  896. return -1;
  897. }
  898. if (wpa_supplicant_scard_init(&wpa_s, wpa_s.conf->ssid))
  899. return -1;
  900. if (test_eapol(&eapol_test, &wpa_s, wpa_s.conf->ssid))
  901. return -1;
  902. if (wait_for_monitor)
  903. wpa_supplicant_ctrl_iface_wait(wpa_s.ctrl_iface);
  904. eloop_register_timeout(timeout, 0, eapol_test_timeout, &eapol_test,
  905. NULL);
  906. eloop_register_timeout(0, 0, send_eap_request_identity, &wpa_s, NULL);
  907. eloop_register_signal_terminate(eapol_test_terminate, NULL);
  908. eloop_register_signal_reconfig(eapol_test_terminate, NULL);
  909. eloop_run();
  910. eloop_cancel_timeout(eapol_test_timeout, &eapol_test, NULL);
  911. eloop_cancel_timeout(eapol_sm_reauth, &eapol_test, NULL);
  912. if (eapol_test_compare_pmk(&eapol_test) == 0 ||
  913. eapol_test.no_mppe_keys)
  914. ret = 0;
  915. if (eapol_test.auth_timed_out)
  916. ret = -2;
  917. if (eapol_test.radius_access_reject_received)
  918. ret = -3;
  919. if (save_config)
  920. wpa_config_write(conf, wpa_s.conf);
  921. test_eapol_clean(&eapol_test, &wpa_s);
  922. eap_peer_unregister_methods();
  923. eloop_destroy();
  924. printf("MPPE keys OK: %d mismatch: %d\n",
  925. eapol_test.num_mppe_ok, eapol_test.num_mppe_mismatch);
  926. if (eapol_test.num_mppe_mismatch)
  927. ret = -4;
  928. if (ret)
  929. printf("FAILURE\n");
  930. else
  931. printf("SUCCESS\n");
  932. os_program_deinit();
  933. return ret;
  934. }