eapol_test.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101
  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. wpa_s->eapol = eapol_sm_init(ctx);
  285. if (wpa_s->eapol == NULL) {
  286. os_free(ctx);
  287. printf("Failed to initialize EAPOL state machines.\n");
  288. return -1;
  289. }
  290. wpa_s->current_ssid = ssid;
  291. os_memset(&eapol_conf, 0, sizeof(eapol_conf));
  292. eapol_conf.accept_802_1x_keys = 1;
  293. eapol_conf.required_keys = 0;
  294. eapol_conf.fast_reauth = wpa_s->conf->fast_reauth;
  295. eapol_conf.workaround = ssid->eap_workaround;
  296. eapol_sm_notify_config(wpa_s->eapol, &ssid->eap, &eapol_conf);
  297. eapol_sm_register_scard_ctx(wpa_s->eapol, wpa_s->scard);
  298. eapol_sm_notify_portValid(wpa_s->eapol, FALSE);
  299. /* 802.1X::portControl = Auto */
  300. eapol_sm_notify_portEnabled(wpa_s->eapol, TRUE);
  301. return 0;
  302. }
  303. static void test_eapol_clean(struct eapol_test_data *e,
  304. struct wpa_supplicant *wpa_s)
  305. {
  306. radius_client_deinit(e->radius);
  307. os_free(e->last_eap_radius);
  308. if (e->last_recv_radius) {
  309. radius_msg_free(e->last_recv_radius);
  310. os_free(e->last_recv_radius);
  311. }
  312. os_free(e->eap_identity);
  313. e->eap_identity = NULL;
  314. eapol_sm_deinit(wpa_s->eapol);
  315. wpa_s->eapol = NULL;
  316. if (e->radius_conf && e->radius_conf->auth_server) {
  317. os_free(e->radius_conf->auth_server->shared_secret);
  318. os_free(e->radius_conf->auth_server);
  319. }
  320. os_free(e->radius_conf);
  321. e->radius_conf = NULL;
  322. scard_deinit(wpa_s->scard);
  323. if (wpa_s->ctrl_iface) {
  324. wpa_supplicant_ctrl_iface_deinit(wpa_s->ctrl_iface);
  325. wpa_s->ctrl_iface = NULL;
  326. }
  327. wpa_config_free(wpa_s->conf);
  328. }
  329. static void send_eap_request_identity(void *eloop_ctx, void *timeout_ctx)
  330. {
  331. struct wpa_supplicant *wpa_s = eloop_ctx;
  332. u8 buf[100], *pos;
  333. struct ieee802_1x_hdr *hdr;
  334. struct eap_hdr *eap;
  335. hdr = (struct ieee802_1x_hdr *) buf;
  336. hdr->version = EAPOL_VERSION;
  337. hdr->type = IEEE802_1X_TYPE_EAP_PACKET;
  338. hdr->length = htons(5);
  339. eap = (struct eap_hdr *) (hdr + 1);
  340. eap->code = EAP_CODE_REQUEST;
  341. eap->identifier = 0;
  342. eap->length = htons(5);
  343. pos = (u8 *) (eap + 1);
  344. *pos = EAP_TYPE_IDENTITY;
  345. printf("Sending fake EAP-Request-Identity\n");
  346. eapol_sm_rx_eapol(wpa_s->eapol, wpa_s->bssid, buf,
  347. sizeof(*hdr) + 5);
  348. }
  349. static void eapol_test_timeout(void *eloop_ctx, void *timeout_ctx)
  350. {
  351. struct eapol_test_data *e = eloop_ctx;
  352. printf("EAPOL test timed out\n");
  353. e->auth_timed_out = 1;
  354. eloop_terminate();
  355. }
  356. static char *eap_type_text(u8 type)
  357. {
  358. switch (type) {
  359. case EAP_TYPE_IDENTITY: return "Identity";
  360. case EAP_TYPE_NOTIFICATION: return "Notification";
  361. case EAP_TYPE_NAK: return "Nak";
  362. case EAP_TYPE_TLS: return "TLS";
  363. case EAP_TYPE_TTLS: return "TTLS";
  364. case EAP_TYPE_PEAP: return "PEAP";
  365. case EAP_TYPE_SIM: return "SIM";
  366. case EAP_TYPE_GTC: return "GTC";
  367. case EAP_TYPE_MD5: return "MD5";
  368. case EAP_TYPE_OTP: return "OTP";
  369. case EAP_TYPE_FAST: return "FAST";
  370. case EAP_TYPE_SAKE: return "SAKE";
  371. case EAP_TYPE_PSK: return "PSK";
  372. default: return "Unknown";
  373. }
  374. }
  375. static void ieee802_1x_decapsulate_radius(struct eapol_test_data *e)
  376. {
  377. u8 *eap;
  378. size_t len;
  379. struct eap_hdr *hdr;
  380. int eap_type = -1;
  381. char buf[64];
  382. struct radius_msg *msg;
  383. if (e->last_recv_radius == NULL)
  384. return;
  385. msg = e->last_recv_radius;
  386. eap = radius_msg_get_eap(msg, &len);
  387. if (eap == NULL) {
  388. /* draft-aboba-radius-rfc2869bis-20.txt, Chap. 2.6.3:
  389. * RADIUS server SHOULD NOT send Access-Reject/no EAP-Message
  390. * attribute */
  391. wpa_printf(MSG_DEBUG, "could not extract "
  392. "EAP-Message from RADIUS message");
  393. os_free(e->last_eap_radius);
  394. e->last_eap_radius = NULL;
  395. e->last_eap_radius_len = 0;
  396. return;
  397. }
  398. if (len < sizeof(*hdr)) {
  399. wpa_printf(MSG_DEBUG, "too short EAP packet "
  400. "received from authentication server");
  401. os_free(eap);
  402. return;
  403. }
  404. if (len > sizeof(*hdr))
  405. eap_type = eap[sizeof(*hdr)];
  406. hdr = (struct eap_hdr *) eap;
  407. switch (hdr->code) {
  408. case EAP_CODE_REQUEST:
  409. os_snprintf(buf, sizeof(buf), "EAP-Request-%s (%d)",
  410. eap_type >= 0 ? eap_type_text(eap_type) : "??",
  411. eap_type);
  412. break;
  413. case EAP_CODE_RESPONSE:
  414. os_snprintf(buf, sizeof(buf), "EAP Response-%s (%d)",
  415. eap_type >= 0 ? eap_type_text(eap_type) : "??",
  416. eap_type);
  417. break;
  418. case EAP_CODE_SUCCESS:
  419. os_strlcpy(buf, "EAP Success", sizeof(buf));
  420. /* LEAP uses EAP Success within an authentication, so must not
  421. * stop here with eloop_terminate(); */
  422. break;
  423. case EAP_CODE_FAILURE:
  424. os_strlcpy(buf, "EAP Failure", sizeof(buf));
  425. eloop_terminate();
  426. break;
  427. default:
  428. os_strlcpy(buf, "unknown EAP code", sizeof(buf));
  429. wpa_hexdump(MSG_DEBUG, "Decapsulated EAP packet", eap, len);
  430. break;
  431. }
  432. wpa_printf(MSG_DEBUG, "decapsulated EAP packet (code=%d "
  433. "id=%d len=%d) from RADIUS server: %s",
  434. hdr->code, hdr->identifier, ntohs(hdr->length), buf);
  435. /* sta->eapol_sm->be_auth.idFromServer = hdr->identifier; */
  436. os_free(e->last_eap_radius);
  437. e->last_eap_radius = eap;
  438. e->last_eap_radius_len = len;
  439. {
  440. struct ieee802_1x_hdr *dot1x;
  441. dot1x = os_malloc(sizeof(*dot1x) + len);
  442. assert(dot1x != NULL);
  443. dot1x->version = EAPOL_VERSION;
  444. dot1x->type = IEEE802_1X_TYPE_EAP_PACKET;
  445. dot1x->length = htons(len);
  446. os_memcpy((u8 *) (dot1x + 1), eap, len);
  447. eapol_sm_rx_eapol(e->wpa_s->eapol, e->wpa_s->bssid,
  448. (u8 *) dot1x, sizeof(*dot1x) + len);
  449. os_free(dot1x);
  450. }
  451. }
  452. static void ieee802_1x_get_keys(struct eapol_test_data *e,
  453. struct radius_msg *msg, struct radius_msg *req,
  454. u8 *shared_secret, size_t shared_secret_len)
  455. {
  456. struct radius_ms_mppe_keys *keys;
  457. keys = radius_msg_get_ms_keys(msg, req, shared_secret,
  458. shared_secret_len);
  459. if (keys && keys->send == NULL && keys->recv == NULL) {
  460. os_free(keys);
  461. keys = radius_msg_get_cisco_keys(msg, req, shared_secret,
  462. shared_secret_len);
  463. }
  464. if (keys) {
  465. if (keys->send) {
  466. wpa_hexdump(MSG_DEBUG, "MS-MPPE-Send-Key (sign)",
  467. keys->send, keys->send_len);
  468. }
  469. if (keys->recv) {
  470. wpa_hexdump(MSG_DEBUG, "MS-MPPE-Recv-Key (crypt)",
  471. keys->recv, keys->recv_len);
  472. e->authenticator_pmk_len =
  473. keys->recv_len > PMK_LEN ? PMK_LEN :
  474. keys->recv_len;
  475. os_memcpy(e->authenticator_pmk, keys->recv,
  476. e->authenticator_pmk_len);
  477. if (e->authenticator_pmk_len == 16 && keys->send &&
  478. keys->send_len == 16) {
  479. /* MS-CHAP-v2 derives 16 octet keys */
  480. wpa_printf(MSG_DEBUG, "Use MS-MPPE-Send-Key "
  481. "to extend PMK to 32 octets");
  482. os_memcpy(e->authenticator_pmk +
  483. e->authenticator_pmk_len,
  484. keys->send, keys->send_len);
  485. e->authenticator_pmk_len += keys->send_len;
  486. }
  487. }
  488. os_free(keys->send);
  489. os_free(keys->recv);
  490. os_free(keys);
  491. }
  492. }
  493. /* Process the RADIUS frames from Authentication Server */
  494. static RadiusRxResult
  495. ieee802_1x_receive_auth(struct radius_msg *msg, struct radius_msg *req,
  496. u8 *shared_secret, size_t shared_secret_len,
  497. void *data)
  498. {
  499. struct eapol_test_data *e = data;
  500. /* RFC 2869, Ch. 5.13: valid Message-Authenticator attribute MUST be
  501. * present when packet contains an EAP-Message attribute */
  502. if (msg->hdr->code == RADIUS_CODE_ACCESS_REJECT &&
  503. radius_msg_get_attr(msg, RADIUS_ATTR_MESSAGE_AUTHENTICATOR, NULL,
  504. 0) < 0 &&
  505. radius_msg_get_attr(msg, RADIUS_ATTR_EAP_MESSAGE, NULL, 0) < 0) {
  506. wpa_printf(MSG_DEBUG, "Allowing RADIUS "
  507. "Access-Reject without Message-Authenticator "
  508. "since it does not include EAP-Message\n");
  509. } else if (radius_msg_verify(msg, shared_secret, shared_secret_len,
  510. req, 1)) {
  511. printf("Incoming RADIUS packet did not have correct "
  512. "Message-Authenticator - dropped\n");
  513. return RADIUS_RX_UNKNOWN;
  514. }
  515. if (msg->hdr->code != RADIUS_CODE_ACCESS_ACCEPT &&
  516. msg->hdr->code != RADIUS_CODE_ACCESS_REJECT &&
  517. msg->hdr->code != RADIUS_CODE_ACCESS_CHALLENGE) {
  518. printf("Unknown RADIUS message code\n");
  519. return RADIUS_RX_UNKNOWN;
  520. }
  521. e->radius_identifier = -1;
  522. wpa_printf(MSG_DEBUG, "RADIUS packet matching with station");
  523. if (e->last_recv_radius) {
  524. radius_msg_free(e->last_recv_radius);
  525. os_free(e->last_recv_radius);
  526. }
  527. e->last_recv_radius = msg;
  528. switch (msg->hdr->code) {
  529. case RADIUS_CODE_ACCESS_ACCEPT:
  530. e->radius_access_accept_received = 1;
  531. ieee802_1x_get_keys(e, msg, req, shared_secret,
  532. shared_secret_len);
  533. break;
  534. case RADIUS_CODE_ACCESS_REJECT:
  535. e->radius_access_reject_received = 1;
  536. break;
  537. }
  538. ieee802_1x_decapsulate_radius(e);
  539. if ((msg->hdr->code == RADIUS_CODE_ACCESS_ACCEPT &&
  540. e->eapol_test_num_reauths < 0) ||
  541. msg->hdr->code == RADIUS_CODE_ACCESS_REJECT) {
  542. eloop_terminate();
  543. }
  544. return RADIUS_RX_QUEUED;
  545. }
  546. static void wpa_init_conf(struct eapol_test_data *e,
  547. struct wpa_supplicant *wpa_s, const char *authsrv,
  548. int port, const char *secret,
  549. const char *cli_addr)
  550. {
  551. struct hostapd_radius_server *as;
  552. int res;
  553. wpa_s->bssid[5] = 1;
  554. os_memcpy(wpa_s->own_addr, e->own_addr, ETH_ALEN);
  555. e->own_ip_addr.s_addr = htonl((127 << 24) | 1);
  556. os_strlcpy(wpa_s->ifname, "test", sizeof(wpa_s->ifname));
  557. e->radius_conf = os_zalloc(sizeof(struct hostapd_radius_servers));
  558. assert(e->radius_conf != NULL);
  559. e->radius_conf->num_auth_servers = 1;
  560. as = os_zalloc(sizeof(struct hostapd_radius_server));
  561. assert(as != NULL);
  562. #if defined(CONFIG_NATIVE_WINDOWS) || defined(CONFIG_ANSI_C_EXTRA)
  563. {
  564. int a[4];
  565. u8 *pos;
  566. sscanf(authsrv, "%d.%d.%d.%d", &a[0], &a[1], &a[2], &a[3]);
  567. pos = (u8 *) &as->addr.u.v4;
  568. *pos++ = a[0];
  569. *pos++ = a[1];
  570. *pos++ = a[2];
  571. *pos++ = a[3];
  572. }
  573. #else /* CONFIG_NATIVE_WINDOWS or CONFIG_ANSI_C_EXTRA */
  574. inet_aton(authsrv, &as->addr.u.v4);
  575. #endif /* CONFIG_NATIVE_WINDOWS or CONFIG_ANSI_C_EXTRA */
  576. as->addr.af = AF_INET;
  577. as->port = port;
  578. as->shared_secret = (u8 *) os_strdup(secret);
  579. as->shared_secret_len = os_strlen(secret);
  580. e->radius_conf->auth_server = as;
  581. e->radius_conf->auth_servers = as;
  582. e->radius_conf->msg_dumps = 1;
  583. if (cli_addr) {
  584. if (hostapd_parse_ip_addr(cli_addr,
  585. &e->radius_conf->client_addr) == 0)
  586. e->radius_conf->force_client_addr = 1;
  587. else {
  588. wpa_printf(MSG_ERROR, "Invalid IP address '%s'",
  589. cli_addr);
  590. assert(0);
  591. }
  592. }
  593. e->radius = radius_client_init(wpa_s, e->radius_conf);
  594. assert(e->radius != NULL);
  595. res = radius_client_register(e->radius, RADIUS_AUTH,
  596. ieee802_1x_receive_auth, e);
  597. assert(res == 0);
  598. }
  599. static int scard_test(void)
  600. {
  601. struct scard_data *scard;
  602. size_t len;
  603. char imsi[20];
  604. unsigned char _rand[16];
  605. #ifdef PCSC_FUNCS
  606. unsigned char sres[4];
  607. unsigned char kc[8];
  608. #endif /* PCSC_FUNCS */
  609. #define num_triplets 5
  610. unsigned char rand_[num_triplets][16];
  611. unsigned char sres_[num_triplets][4];
  612. unsigned char kc_[num_triplets][8];
  613. int i, res;
  614. size_t j;
  615. #define AKA_RAND_LEN 16
  616. #define AKA_AUTN_LEN 16
  617. #define AKA_AUTS_LEN 14
  618. #define RES_MAX_LEN 16
  619. #define IK_LEN 16
  620. #define CK_LEN 16
  621. unsigned char aka_rand[AKA_RAND_LEN];
  622. unsigned char aka_autn[AKA_AUTN_LEN];
  623. unsigned char aka_auts[AKA_AUTS_LEN];
  624. unsigned char aka_res[RES_MAX_LEN];
  625. size_t aka_res_len;
  626. unsigned char aka_ik[IK_LEN];
  627. unsigned char aka_ck[CK_LEN];
  628. scard = scard_init(SCARD_TRY_BOTH);
  629. if (scard == NULL)
  630. return -1;
  631. if (scard_set_pin(scard, "1234")) {
  632. wpa_printf(MSG_WARNING, "PIN validation failed");
  633. scard_deinit(scard);
  634. return -1;
  635. }
  636. len = sizeof(imsi);
  637. if (scard_get_imsi(scard, imsi, &len))
  638. goto failed;
  639. wpa_hexdump_ascii(MSG_DEBUG, "SCARD: IMSI", (u8 *) imsi, len);
  640. /* NOTE: Permanent Username: 1 | IMSI */
  641. os_memset(_rand, 0, sizeof(_rand));
  642. if (scard_gsm_auth(scard, _rand, sres, kc))
  643. goto failed;
  644. os_memset(_rand, 0xff, sizeof(_rand));
  645. if (scard_gsm_auth(scard, _rand, sres, kc))
  646. goto failed;
  647. for (i = 0; i < num_triplets; i++) {
  648. os_memset(rand_[i], i, sizeof(rand_[i]));
  649. if (scard_gsm_auth(scard, rand_[i], sres_[i], kc_[i]))
  650. goto failed;
  651. }
  652. for (i = 0; i < num_triplets; i++) {
  653. printf("1");
  654. for (j = 0; j < len; j++)
  655. printf("%c", imsi[j]);
  656. printf(",");
  657. for (j = 0; j < 16; j++)
  658. printf("%02X", rand_[i][j]);
  659. printf(",");
  660. for (j = 0; j < 4; j++)
  661. printf("%02X", sres_[i][j]);
  662. printf(",");
  663. for (j = 0; j < 8; j++)
  664. printf("%02X", kc_[i][j]);
  665. printf("\n");
  666. }
  667. wpa_printf(MSG_DEBUG, "Trying to use UMTS authentication");
  668. /* seq 39 (0x28) */
  669. os_memset(aka_rand, 0xaa, 16);
  670. os_memcpy(aka_autn, "\x86\x71\x31\xcb\xa2\xfc\x61\xdf"
  671. "\xa3\xb3\x97\x9d\x07\x32\xa2\x12", 16);
  672. res = scard_umts_auth(scard, aka_rand, aka_autn, aka_res, &aka_res_len,
  673. aka_ik, aka_ck, aka_auts);
  674. if (res == 0) {
  675. wpa_printf(MSG_DEBUG, "UMTS auth completed successfully");
  676. wpa_hexdump(MSG_DEBUG, "RES", aka_res, aka_res_len);
  677. wpa_hexdump(MSG_DEBUG, "IK", aka_ik, IK_LEN);
  678. wpa_hexdump(MSG_DEBUG, "CK", aka_ck, CK_LEN);
  679. } else if (res == -2) {
  680. wpa_printf(MSG_DEBUG, "UMTS auth resulted in synchronization "
  681. "failure");
  682. wpa_hexdump(MSG_DEBUG, "AUTS", aka_auts, AKA_AUTS_LEN);
  683. } else {
  684. wpa_printf(MSG_DEBUG, "UMTS auth failed");
  685. }
  686. failed:
  687. scard_deinit(scard);
  688. return 0;
  689. #undef num_triplets
  690. }
  691. static int scard_get_triplets(int argc, char *argv[])
  692. {
  693. struct scard_data *scard;
  694. size_t len;
  695. char imsi[20];
  696. unsigned char _rand[16];
  697. unsigned char sres[4];
  698. unsigned char kc[8];
  699. int num_triplets;
  700. int i;
  701. size_t j;
  702. if (argc < 2 || ((num_triplets = atoi(argv[1])) <= 0)) {
  703. printf("invalid parameters for sim command\n");
  704. return -1;
  705. }
  706. if (argc <= 2 || os_strcmp(argv[2], "debug") != 0) {
  707. /* disable debug output */
  708. wpa_debug_level = 99;
  709. }
  710. scard = scard_init(SCARD_GSM_SIM_ONLY);
  711. if (scard == NULL) {
  712. printf("Failed to open smartcard connection\n");
  713. return -1;
  714. }
  715. if (scard_set_pin(scard, argv[0])) {
  716. wpa_printf(MSG_WARNING, "PIN validation failed");
  717. scard_deinit(scard);
  718. return -1;
  719. }
  720. len = sizeof(imsi);
  721. if (scard_get_imsi(scard, imsi, &len)) {
  722. scard_deinit(scard);
  723. return -1;
  724. }
  725. for (i = 0; i < num_triplets; i++) {
  726. os_memset(_rand, i, sizeof(_rand));
  727. if (scard_gsm_auth(scard, _rand, sres, kc))
  728. break;
  729. /* IMSI:Kc:SRES:RAND */
  730. for (j = 0; j < len; j++)
  731. printf("%c", imsi[j]);
  732. printf(":");
  733. for (j = 0; j < 8; j++)
  734. printf("%02X", kc[j]);
  735. printf(":");
  736. for (j = 0; j < 4; j++)
  737. printf("%02X", sres[j]);
  738. printf(":");
  739. for (j = 0; j < 16; j++)
  740. printf("%02X", _rand[j]);
  741. printf("\n");
  742. }
  743. scard_deinit(scard);
  744. return 0;
  745. }
  746. static void eapol_test_terminate(int sig, void *eloop_ctx,
  747. void *signal_ctx)
  748. {
  749. struct wpa_supplicant *wpa_s = eloop_ctx;
  750. wpa_msg(wpa_s, MSG_INFO, "Signal %d received - terminating", sig);
  751. eloop_terminate();
  752. }
  753. static void usage(void)
  754. {
  755. printf("usage:\n"
  756. "eapol_test [-nWS] -c<conf> [-a<AS IP>] [-p<AS port>] "
  757. "[-s<AS secret>]\\\n"
  758. " [-r<count>] [-t<timeout>] [-C<Connect-Info>] \\\n"
  759. " [-M<client MAC address>] \\\n"
  760. " [-I<CUI>] [-i] [-A<client IP>]\n"
  761. "eapol_test scard\n"
  762. "eapol_test sim <PIN> <num triplets> [debug]\n"
  763. "\n");
  764. printf("options:\n"
  765. " -c<conf> = configuration file\n"
  766. " -a<AS IP> = IP address of the authentication server, "
  767. "default 127.0.0.1\n"
  768. " -p<AS port> = UDP port of the authentication server, "
  769. "default 1812\n"
  770. " -s<AS secret> = shared secret with the authentication "
  771. "server, default 'radius'\n"
  772. " -A<client IP> = IP address of the client, default: select "
  773. "automatically\n"
  774. " -r<count> = number of re-authentications\n"
  775. " -W = wait for a control interface monitor before starting\n"
  776. " -S = save configuration after authentiation\n"
  777. " -n = no MPPE keys expected\n"
  778. " -t<timeout> = sets timeout in seconds (default: 30 s)\n"
  779. " -C<Connect-Info> = RADIUS Connect-Info (default: "
  780. "CONNECT 11Mbps 802.11b)\n"
  781. " -M<client MAC address> = Set own MAC address "
  782. "(Calling-Station-Id,\n"
  783. " default: 02:00:00:00:00:01)\n"
  784. " -I<CUI> = send Chargeable-User-Identity containing the "
  785. "value of CUI\n"
  786. " -i = send NUL value in Chargeable-User-Identity\n");
  787. }
  788. int main(int argc, char *argv[])
  789. {
  790. struct wpa_supplicant wpa_s;
  791. int c, ret = 1, wait_for_monitor = 0, save_config = 0;
  792. char *as_addr = "127.0.0.1";
  793. int as_port = 1812;
  794. char *as_secret = "radius";
  795. char *cli_addr = NULL;
  796. char *conf = NULL;
  797. int timeout = 30;
  798. if (os_program_init())
  799. return -1;
  800. hostapd_logger_register_cb(hostapd_logger_cb);
  801. os_memset(&eapol_test, 0, sizeof(eapol_test));
  802. eapol_test.connect_info = "CONNECT 11Mbps 802.11b";
  803. os_memcpy(eapol_test.own_addr, "\x02\x00\x00\x00\x00\x01", ETH_ALEN);
  804. wpa_debug_level = 0;
  805. wpa_debug_show_keys = 1;
  806. for (;;) {
  807. c = getopt(argc, argv, "a:A:c:C:iI:M:np:r:s:St:W");
  808. if (c < 0)
  809. break;
  810. switch (c) {
  811. case 'a':
  812. as_addr = optarg;
  813. break;
  814. case 'A':
  815. cli_addr = optarg;
  816. break;
  817. case 'c':
  818. conf = optarg;
  819. break;
  820. case 'C':
  821. eapol_test.connect_info = optarg;
  822. break;
  823. case 'i':
  824. eapol_test.cui_flag = 1;
  825. break;
  826. case 'I':
  827. eapol_test.cui_flag = 2;
  828. eapol_test.cui_str = optarg;
  829. break;
  830. case 'M':
  831. if (hwaddr_aton(optarg, eapol_test.own_addr)) {
  832. usage();
  833. return -1;
  834. }
  835. break;
  836. case 'n':
  837. eapol_test.no_mppe_keys++;
  838. break;
  839. case 'p':
  840. as_port = atoi(optarg);
  841. break;
  842. case 'r':
  843. eapol_test.eapol_test_num_reauths = atoi(optarg);
  844. break;
  845. case 's':
  846. as_secret = optarg;
  847. break;
  848. case 'S':
  849. save_config++;
  850. break;
  851. case 't':
  852. timeout = atoi(optarg);
  853. break;
  854. case 'W':
  855. wait_for_monitor++;
  856. break;
  857. default:
  858. usage();
  859. return -1;
  860. }
  861. }
  862. if (argc > optind && os_strcmp(argv[optind], "scard") == 0) {
  863. return scard_test();
  864. }
  865. if (argc > optind && os_strcmp(argv[optind], "sim") == 0) {
  866. return scard_get_triplets(argc - optind - 1,
  867. &argv[optind + 1]);
  868. }
  869. if (conf == NULL) {
  870. usage();
  871. printf("Configuration file is required.\n");
  872. return -1;
  873. }
  874. if (eap_peer_register_methods()) {
  875. wpa_printf(MSG_ERROR, "Failed to register EAP methods");
  876. return -1;
  877. }
  878. if (eloop_init(&wpa_s)) {
  879. wpa_printf(MSG_ERROR, "Failed to initialize event loop");
  880. return -1;
  881. }
  882. os_memset(&wpa_s, 0, sizeof(wpa_s));
  883. eapol_test.wpa_s = &wpa_s;
  884. wpa_s.conf = wpa_config_read(conf);
  885. if (wpa_s.conf == NULL) {
  886. printf("Failed to parse configuration file '%s'.\n", conf);
  887. return -1;
  888. }
  889. if (wpa_s.conf->ssid == NULL) {
  890. printf("No networks defined.\n");
  891. return -1;
  892. }
  893. wpa_init_conf(&eapol_test, &wpa_s, as_addr, as_port, as_secret,
  894. cli_addr);
  895. wpa_s.ctrl_iface = wpa_supplicant_ctrl_iface_init(&wpa_s);
  896. if (wpa_s.ctrl_iface == NULL) {
  897. printf("Failed to initialize control interface '%s'.\n"
  898. "You may have another eapol_test process already "
  899. "running or the file was\n"
  900. "left by an unclean termination of eapol_test in "
  901. "which case you will need\n"
  902. "to manually remove this file before starting "
  903. "eapol_test again.\n",
  904. wpa_s.conf->ctrl_interface);
  905. return -1;
  906. }
  907. if (wpa_supplicant_scard_init(&wpa_s, wpa_s.conf->ssid))
  908. return -1;
  909. if (test_eapol(&eapol_test, &wpa_s, wpa_s.conf->ssid))
  910. return -1;
  911. if (wait_for_monitor)
  912. wpa_supplicant_ctrl_iface_wait(wpa_s.ctrl_iface);
  913. eloop_register_timeout(timeout, 0, eapol_test_timeout, &eapol_test,
  914. NULL);
  915. eloop_register_timeout(0, 0, send_eap_request_identity, &wpa_s, NULL);
  916. eloop_register_signal_terminate(eapol_test_terminate, NULL);
  917. eloop_register_signal_reconfig(eapol_test_terminate, NULL);
  918. eloop_run();
  919. eloop_cancel_timeout(eapol_test_timeout, &eapol_test, NULL);
  920. eloop_cancel_timeout(eapol_sm_reauth, &eapol_test, NULL);
  921. if (eapol_test_compare_pmk(&eapol_test) == 0 ||
  922. eapol_test.no_mppe_keys)
  923. ret = 0;
  924. if (eapol_test.auth_timed_out)
  925. ret = -2;
  926. if (eapol_test.radius_access_reject_received)
  927. ret = -3;
  928. if (save_config)
  929. wpa_config_write(conf, wpa_s.conf);
  930. test_eapol_clean(&eapol_test, &wpa_s);
  931. eap_peer_unregister_methods();
  932. eloop_destroy();
  933. printf("MPPE keys OK: %d mismatch: %d\n",
  934. eapol_test.num_mppe_ok, eapol_test.num_mppe_mismatch);
  935. if (eapol_test.num_mppe_mismatch)
  936. ret = -4;
  937. if (ret)
  938. printf("FAILURE\n");
  939. else
  940. printf("SUCCESS\n");
  941. os_program_deinit();
  942. return ret;
  943. }