eapol_test.c 33 KB

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