eapol_test.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279
  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 "config.h"
  15. #include "eapol_supp/eapol_supp_sm.h"
  16. #include "eap_peer/eap.h"
  17. #include "eap_server/eap_methods.h"
  18. #include "eloop.h"
  19. #include "utils/base64.h"
  20. #include "rsn_supp/wpa.h"
  21. #include "eap_peer/eap_i.h"
  22. #include "wpa_supplicant_i.h"
  23. #include "radius/radius.h"
  24. #include "radius/radius_client.h"
  25. #include "common/wpa_ctrl.h"
  26. #include "ctrl_iface.h"
  27. #include "pcsc_funcs.h"
  28. extern int wpa_debug_level;
  29. extern int wpa_debug_show_keys;
  30. struct wpa_driver_ops *wpa_drivers[] = { NULL };
  31. struct extra_radius_attr {
  32. u8 type;
  33. char syntax;
  34. char *data;
  35. struct extra_radius_attr *next;
  36. };
  37. struct eapol_test_data {
  38. struct wpa_supplicant *wpa_s;
  39. int eapol_test_num_reauths;
  40. int no_mppe_keys;
  41. int num_mppe_ok, num_mppe_mismatch;
  42. u8 radius_identifier;
  43. struct radius_msg *last_recv_radius;
  44. struct in_addr own_ip_addr;
  45. struct radius_client_data *radius;
  46. struct hostapd_radius_servers *radius_conf;
  47. u8 *last_eap_radius; /* last received EAP Response from Authentication
  48. * Server */
  49. size_t last_eap_radius_len;
  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. os_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. wpa_config_free(wpa_s->conf);
  428. p = e->extra_attrs;
  429. while (p) {
  430. prev = p;
  431. p = p->next;
  432. os_free(prev);
  433. }
  434. }
  435. static void send_eap_request_identity(void *eloop_ctx, void *timeout_ctx)
  436. {
  437. struct wpa_supplicant *wpa_s = eloop_ctx;
  438. u8 buf[100], *pos;
  439. struct ieee802_1x_hdr *hdr;
  440. struct eap_hdr *eap;
  441. hdr = (struct ieee802_1x_hdr *) buf;
  442. hdr->version = EAPOL_VERSION;
  443. hdr->type = IEEE802_1X_TYPE_EAP_PACKET;
  444. hdr->length = htons(5);
  445. eap = (struct eap_hdr *) (hdr + 1);
  446. eap->code = EAP_CODE_REQUEST;
  447. eap->identifier = 0;
  448. eap->length = htons(5);
  449. pos = (u8 *) (eap + 1);
  450. *pos = EAP_TYPE_IDENTITY;
  451. printf("Sending fake EAP-Request-Identity\n");
  452. eapol_sm_rx_eapol(wpa_s->eapol, wpa_s->bssid, buf,
  453. sizeof(*hdr) + 5);
  454. }
  455. static void eapol_test_timeout(void *eloop_ctx, void *timeout_ctx)
  456. {
  457. struct eapol_test_data *e = eloop_ctx;
  458. printf("EAPOL test timed out\n");
  459. e->auth_timed_out = 1;
  460. eloop_terminate();
  461. }
  462. static char *eap_type_text(u8 type)
  463. {
  464. switch (type) {
  465. case EAP_TYPE_IDENTITY: return "Identity";
  466. case EAP_TYPE_NOTIFICATION: return "Notification";
  467. case EAP_TYPE_NAK: return "Nak";
  468. case EAP_TYPE_TLS: return "TLS";
  469. case EAP_TYPE_TTLS: return "TTLS";
  470. case EAP_TYPE_PEAP: return "PEAP";
  471. case EAP_TYPE_SIM: return "SIM";
  472. case EAP_TYPE_GTC: return "GTC";
  473. case EAP_TYPE_MD5: return "MD5";
  474. case EAP_TYPE_OTP: return "OTP";
  475. case EAP_TYPE_FAST: return "FAST";
  476. case EAP_TYPE_SAKE: return "SAKE";
  477. case EAP_TYPE_PSK: return "PSK";
  478. default: return "Unknown";
  479. }
  480. }
  481. static void ieee802_1x_decapsulate_radius(struct eapol_test_data *e)
  482. {
  483. u8 *eap;
  484. size_t len;
  485. struct eap_hdr *hdr;
  486. int eap_type = -1;
  487. char buf[64];
  488. struct radius_msg *msg;
  489. if (e->last_recv_radius == NULL)
  490. return;
  491. msg = e->last_recv_radius;
  492. eap = radius_msg_get_eap(msg, &len);
  493. if (eap == NULL) {
  494. /* draft-aboba-radius-rfc2869bis-20.txt, Chap. 2.6.3:
  495. * RADIUS server SHOULD NOT send Access-Reject/no EAP-Message
  496. * attribute */
  497. wpa_printf(MSG_DEBUG, "could not extract "
  498. "EAP-Message from RADIUS message");
  499. os_free(e->last_eap_radius);
  500. e->last_eap_radius = NULL;
  501. e->last_eap_radius_len = 0;
  502. return;
  503. }
  504. if (len < sizeof(*hdr)) {
  505. wpa_printf(MSG_DEBUG, "too short EAP packet "
  506. "received from authentication server");
  507. os_free(eap);
  508. return;
  509. }
  510. if (len > sizeof(*hdr))
  511. eap_type = eap[sizeof(*hdr)];
  512. hdr = (struct eap_hdr *) 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(MSG_DEBUG, "Decapsulated EAP packet", eap, len);
  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. os_free(e->last_eap_radius);
  543. e->last_eap_radius = eap;
  544. e->last_eap_radius_len = len;
  545. {
  546. struct ieee802_1x_hdr *dot1x;
  547. dot1x = os_malloc(sizeof(*dot1x) + len);
  548. assert(dot1x != NULL);
  549. dot1x->version = EAPOL_VERSION;
  550. dot1x->type = IEEE802_1X_TYPE_EAP_PACKET;
  551. dot1x->length = htons(len);
  552. os_memcpy((u8 *) (dot1x + 1), eap, len);
  553. eapol_sm_rx_eapol(e->wpa_s->eapol, e->wpa_s->bssid,
  554. (u8 *) dot1x, sizeof(*dot1x) + len);
  555. os_free(dot1x);
  556. }
  557. }
  558. static void ieee802_1x_get_keys(struct eapol_test_data *e,
  559. struct radius_msg *msg, struct radius_msg *req,
  560. const u8 *shared_secret,
  561. size_t shared_secret_len)
  562. {
  563. struct radius_ms_mppe_keys *keys;
  564. keys = radius_msg_get_ms_keys(msg, req, shared_secret,
  565. shared_secret_len);
  566. if (keys && keys->send == NULL && keys->recv == NULL) {
  567. os_free(keys);
  568. keys = radius_msg_get_cisco_keys(msg, req, shared_secret,
  569. shared_secret_len);
  570. }
  571. if (keys) {
  572. if (keys->send) {
  573. wpa_hexdump(MSG_DEBUG, "MS-MPPE-Send-Key (sign)",
  574. keys->send, keys->send_len);
  575. }
  576. if (keys->recv) {
  577. wpa_hexdump(MSG_DEBUG, "MS-MPPE-Recv-Key (crypt)",
  578. keys->recv, keys->recv_len);
  579. e->authenticator_pmk_len =
  580. keys->recv_len > PMK_LEN ? PMK_LEN :
  581. keys->recv_len;
  582. os_memcpy(e->authenticator_pmk, keys->recv,
  583. e->authenticator_pmk_len);
  584. if (e->authenticator_pmk_len == 16 && keys->send &&
  585. keys->send_len == 16) {
  586. /* MS-CHAP-v2 derives 16 octet keys */
  587. wpa_printf(MSG_DEBUG, "Use MS-MPPE-Send-Key "
  588. "to extend PMK to 32 octets");
  589. os_memcpy(e->authenticator_pmk +
  590. e->authenticator_pmk_len,
  591. keys->send, keys->send_len);
  592. e->authenticator_pmk_len += keys->send_len;
  593. }
  594. }
  595. os_free(keys->send);
  596. os_free(keys->recv);
  597. os_free(keys);
  598. }
  599. }
  600. /* Process the RADIUS frames from Authentication Server */
  601. static RadiusRxResult
  602. ieee802_1x_receive_auth(struct radius_msg *msg, struct radius_msg *req,
  603. const u8 *shared_secret, size_t shared_secret_len,
  604. void *data)
  605. {
  606. struct eapol_test_data *e = data;
  607. struct radius_hdr *hdr = radius_msg_get_hdr(msg);
  608. /* RFC 2869, Ch. 5.13: valid Message-Authenticator attribute MUST be
  609. * present when packet contains an EAP-Message attribute */
  610. if (hdr->code == RADIUS_CODE_ACCESS_REJECT &&
  611. radius_msg_get_attr(msg, RADIUS_ATTR_MESSAGE_AUTHENTICATOR, NULL,
  612. 0) < 0 &&
  613. radius_msg_get_attr(msg, RADIUS_ATTR_EAP_MESSAGE, NULL, 0) < 0) {
  614. wpa_printf(MSG_DEBUG, "Allowing RADIUS "
  615. "Access-Reject without Message-Authenticator "
  616. "since it does not include EAP-Message\n");
  617. } else if (radius_msg_verify(msg, shared_secret, shared_secret_len,
  618. req, 1)) {
  619. printf("Incoming RADIUS packet did not have correct "
  620. "Message-Authenticator - dropped\n");
  621. return RADIUS_RX_UNKNOWN;
  622. }
  623. if (hdr->code != RADIUS_CODE_ACCESS_ACCEPT &&
  624. hdr->code != RADIUS_CODE_ACCESS_REJECT &&
  625. hdr->code != RADIUS_CODE_ACCESS_CHALLENGE) {
  626. printf("Unknown RADIUS message code\n");
  627. return RADIUS_RX_UNKNOWN;
  628. }
  629. e->radius_identifier = -1;
  630. wpa_printf(MSG_DEBUG, "RADIUS packet matching with station");
  631. radius_msg_free(e->last_recv_radius);
  632. e->last_recv_radius = msg;
  633. switch (hdr->code) {
  634. case RADIUS_CODE_ACCESS_ACCEPT:
  635. e->radius_access_accept_received = 1;
  636. ieee802_1x_get_keys(e, msg, req, shared_secret,
  637. shared_secret_len);
  638. break;
  639. case RADIUS_CODE_ACCESS_REJECT:
  640. e->radius_access_reject_received = 1;
  641. break;
  642. }
  643. ieee802_1x_decapsulate_radius(e);
  644. if ((hdr->code == RADIUS_CODE_ACCESS_ACCEPT &&
  645. e->eapol_test_num_reauths < 0) ||
  646. hdr->code == RADIUS_CODE_ACCESS_REJECT) {
  647. eloop_terminate();
  648. }
  649. return RADIUS_RX_QUEUED;
  650. }
  651. static void wpa_init_conf(struct eapol_test_data *e,
  652. struct wpa_supplicant *wpa_s, const char *authsrv,
  653. int port, const char *secret,
  654. const char *cli_addr)
  655. {
  656. struct hostapd_radius_server *as;
  657. int res;
  658. wpa_s->bssid[5] = 1;
  659. os_memcpy(wpa_s->own_addr, e->own_addr, ETH_ALEN);
  660. e->own_ip_addr.s_addr = htonl((127 << 24) | 1);
  661. os_strlcpy(wpa_s->ifname, "test", sizeof(wpa_s->ifname));
  662. e->radius_conf = os_zalloc(sizeof(struct hostapd_radius_servers));
  663. assert(e->radius_conf != NULL);
  664. e->radius_conf->num_auth_servers = 1;
  665. as = os_zalloc(sizeof(struct hostapd_radius_server));
  666. assert(as != NULL);
  667. #if defined(CONFIG_NATIVE_WINDOWS) || defined(CONFIG_ANSI_C_EXTRA)
  668. {
  669. int a[4];
  670. u8 *pos;
  671. sscanf(authsrv, "%d.%d.%d.%d", &a[0], &a[1], &a[2], &a[3]);
  672. pos = (u8 *) &as->addr.u.v4;
  673. *pos++ = a[0];
  674. *pos++ = a[1];
  675. *pos++ = a[2];
  676. *pos++ = a[3];
  677. }
  678. #else /* CONFIG_NATIVE_WINDOWS or CONFIG_ANSI_C_EXTRA */
  679. inet_aton(authsrv, &as->addr.u.v4);
  680. #endif /* CONFIG_NATIVE_WINDOWS or CONFIG_ANSI_C_EXTRA */
  681. as->addr.af = AF_INET;
  682. as->port = port;
  683. as->shared_secret = (u8 *) os_strdup(secret);
  684. as->shared_secret_len = os_strlen(secret);
  685. e->radius_conf->auth_server = as;
  686. e->radius_conf->auth_servers = as;
  687. e->radius_conf->msg_dumps = 1;
  688. if (cli_addr) {
  689. if (hostapd_parse_ip_addr(cli_addr,
  690. &e->radius_conf->client_addr) == 0)
  691. e->radius_conf->force_client_addr = 1;
  692. else {
  693. wpa_printf(MSG_ERROR, "Invalid IP address '%s'",
  694. cli_addr);
  695. assert(0);
  696. }
  697. }
  698. e->radius = radius_client_init(wpa_s, e->radius_conf);
  699. assert(e->radius != NULL);
  700. res = radius_client_register(e->radius, RADIUS_AUTH,
  701. ieee802_1x_receive_auth, e);
  702. assert(res == 0);
  703. }
  704. static int scard_test(void)
  705. {
  706. struct scard_data *scard;
  707. size_t len;
  708. char imsi[20];
  709. unsigned char _rand[16];
  710. #ifdef PCSC_FUNCS
  711. unsigned char sres[4];
  712. unsigned char kc[8];
  713. #endif /* PCSC_FUNCS */
  714. #define num_triplets 5
  715. unsigned char rand_[num_triplets][16];
  716. unsigned char sres_[num_triplets][4];
  717. unsigned char kc_[num_triplets][8];
  718. int i, res;
  719. size_t j;
  720. #define AKA_RAND_LEN 16
  721. #define AKA_AUTN_LEN 16
  722. #define AKA_AUTS_LEN 14
  723. #define RES_MAX_LEN 16
  724. #define IK_LEN 16
  725. #define CK_LEN 16
  726. unsigned char aka_rand[AKA_RAND_LEN];
  727. unsigned char aka_autn[AKA_AUTN_LEN];
  728. unsigned char aka_auts[AKA_AUTS_LEN];
  729. unsigned char aka_res[RES_MAX_LEN];
  730. size_t aka_res_len;
  731. unsigned char aka_ik[IK_LEN];
  732. unsigned char aka_ck[CK_LEN];
  733. scard = scard_init(SCARD_TRY_BOTH, NULL);
  734. if (scard == NULL)
  735. return -1;
  736. if (scard_set_pin(scard, "1234")) {
  737. wpa_printf(MSG_WARNING, "PIN validation failed");
  738. scard_deinit(scard);
  739. return -1;
  740. }
  741. len = sizeof(imsi);
  742. if (scard_get_imsi(scard, imsi, &len))
  743. goto failed;
  744. wpa_hexdump_ascii(MSG_DEBUG, "SCARD: IMSI", (u8 *) imsi, len);
  745. /* NOTE: Permanent Username: 1 | IMSI */
  746. wpa_printf(MSG_DEBUG, "SCARD: MNC length %d",
  747. scard_get_mnc_len(scard));
  748. os_memset(_rand, 0, sizeof(_rand));
  749. if (scard_gsm_auth(scard, _rand, sres, kc))
  750. goto failed;
  751. os_memset(_rand, 0xff, sizeof(_rand));
  752. if (scard_gsm_auth(scard, _rand, sres, kc))
  753. goto failed;
  754. for (i = 0; i < num_triplets; i++) {
  755. os_memset(rand_[i], i, sizeof(rand_[i]));
  756. if (scard_gsm_auth(scard, rand_[i], sres_[i], kc_[i]))
  757. goto failed;
  758. }
  759. for (i = 0; i < num_triplets; i++) {
  760. printf("1");
  761. for (j = 0; j < len; j++)
  762. printf("%c", imsi[j]);
  763. printf(",");
  764. for (j = 0; j < 16; j++)
  765. printf("%02X", rand_[i][j]);
  766. printf(",");
  767. for (j = 0; j < 4; j++)
  768. printf("%02X", sres_[i][j]);
  769. printf(",");
  770. for (j = 0; j < 8; j++)
  771. printf("%02X", kc_[i][j]);
  772. printf("\n");
  773. }
  774. wpa_printf(MSG_DEBUG, "Trying to use UMTS authentication");
  775. /* seq 39 (0x28) */
  776. os_memset(aka_rand, 0xaa, 16);
  777. os_memcpy(aka_autn, "\x86\x71\x31\xcb\xa2\xfc\x61\xdf"
  778. "\xa3\xb3\x97\x9d\x07\x32\xa2\x12", 16);
  779. res = scard_umts_auth(scard, aka_rand, aka_autn, aka_res, &aka_res_len,
  780. aka_ik, aka_ck, aka_auts);
  781. if (res == 0) {
  782. wpa_printf(MSG_DEBUG, "UMTS auth completed successfully");
  783. wpa_hexdump(MSG_DEBUG, "RES", aka_res, aka_res_len);
  784. wpa_hexdump(MSG_DEBUG, "IK", aka_ik, IK_LEN);
  785. wpa_hexdump(MSG_DEBUG, "CK", aka_ck, CK_LEN);
  786. } else if (res == -2) {
  787. wpa_printf(MSG_DEBUG, "UMTS auth resulted in synchronization "
  788. "failure");
  789. wpa_hexdump(MSG_DEBUG, "AUTS", aka_auts, AKA_AUTS_LEN);
  790. } else {
  791. wpa_printf(MSG_DEBUG, "UMTS auth failed");
  792. }
  793. failed:
  794. scard_deinit(scard);
  795. return 0;
  796. #undef num_triplets
  797. }
  798. static int scard_get_triplets(int argc, char *argv[])
  799. {
  800. struct scard_data *scard;
  801. size_t len;
  802. char imsi[20];
  803. unsigned char _rand[16];
  804. unsigned char sres[4];
  805. unsigned char kc[8];
  806. int num_triplets;
  807. int i;
  808. size_t j;
  809. if (argc < 2 || ((num_triplets = atoi(argv[1])) <= 0)) {
  810. printf("invalid parameters for sim command\n");
  811. return -1;
  812. }
  813. if (argc <= 2 || os_strcmp(argv[2], "debug") != 0) {
  814. /* disable debug output */
  815. wpa_debug_level = 99;
  816. }
  817. scard = scard_init(SCARD_GSM_SIM_ONLY, NULL);
  818. if (scard == NULL) {
  819. printf("Failed to open smartcard connection\n");
  820. return -1;
  821. }
  822. if (scard_set_pin(scard, argv[0])) {
  823. wpa_printf(MSG_WARNING, "PIN validation failed");
  824. scard_deinit(scard);
  825. return -1;
  826. }
  827. len = sizeof(imsi);
  828. if (scard_get_imsi(scard, imsi, &len)) {
  829. scard_deinit(scard);
  830. return -1;
  831. }
  832. for (i = 0; i < num_triplets; i++) {
  833. os_memset(_rand, i, sizeof(_rand));
  834. if (scard_gsm_auth(scard, _rand, sres, kc))
  835. break;
  836. /* IMSI:Kc:SRES:RAND */
  837. for (j = 0; j < len; j++)
  838. printf("%c", imsi[j]);
  839. printf(":");
  840. for (j = 0; j < 8; j++)
  841. printf("%02X", kc[j]);
  842. printf(":");
  843. for (j = 0; j < 4; j++)
  844. printf("%02X", sres[j]);
  845. printf(":");
  846. for (j = 0; j < 16; j++)
  847. printf("%02X", _rand[j]);
  848. printf("\n");
  849. }
  850. scard_deinit(scard);
  851. return 0;
  852. }
  853. static void eapol_test_terminate(int sig, void *signal_ctx)
  854. {
  855. struct wpa_supplicant *wpa_s = signal_ctx;
  856. wpa_msg(wpa_s, MSG_INFO, "Signal %d received - terminating", sig);
  857. eloop_terminate();
  858. }
  859. static void usage(void)
  860. {
  861. printf("usage:\n"
  862. "eapol_test [-nWS] -c<conf> [-a<AS IP>] [-p<AS port>] "
  863. "[-s<AS secret>]\\\n"
  864. " [-r<count>] [-t<timeout>] [-C<Connect-Info>] \\\n"
  865. " [-M<client MAC address>] [-o<server cert file] \\\n"
  866. " [-N<attr spec>] \\\n"
  867. " [-A<client IP>]\n"
  868. "eapol_test scard\n"
  869. "eapol_test sim <PIN> <num triplets> [debug]\n"
  870. "\n");
  871. printf("options:\n"
  872. " -c<conf> = configuration file\n"
  873. " -a<AS IP> = IP address of the authentication server, "
  874. "default 127.0.0.1\n"
  875. " -p<AS port> = UDP port of the authentication server, "
  876. "default 1812\n"
  877. " -s<AS secret> = shared secret with the authentication "
  878. "server, default 'radius'\n"
  879. " -A<client IP> = IP address of the client, default: select "
  880. "automatically\n"
  881. " -r<count> = number of re-authentications\n"
  882. " -W = wait for a control interface monitor before starting\n"
  883. " -S = save configuration after authentication\n"
  884. " -n = no MPPE keys expected\n"
  885. " -t<timeout> = sets timeout in seconds (default: 30 s)\n"
  886. " -C<Connect-Info> = RADIUS Connect-Info (default: "
  887. "CONNECT 11Mbps 802.11b)\n"
  888. " -M<client MAC address> = Set own MAC address "
  889. "(Calling-Station-Id,\n"
  890. " default: 02:00:00:00:00:01)\n"
  891. " -o<server cert file> = Write received server certificate\n"
  892. " chain to the specified file\n"
  893. " -N<attr spec> = send arbitrary attribute specified by:\n"
  894. " attr_id:syntax:value or attr_id\n"
  895. " attr_id - number id of the attribute\n"
  896. " syntax - one of: s, d, x\n"
  897. " s = string\n"
  898. " d = integer\n"
  899. " x = octet string\n"
  900. " value - attribute value.\n"
  901. " When only attr_id is specified, NULL will be used as "
  902. "value.\n"
  903. " Multiple attributes can be specified by using the "
  904. "option several times.\n");
  905. }
  906. int main(int argc, char *argv[])
  907. {
  908. struct wpa_supplicant wpa_s;
  909. int c, ret = 1, wait_for_monitor = 0, save_config = 0;
  910. char *as_addr = "127.0.0.1";
  911. int as_port = 1812;
  912. char *as_secret = "radius";
  913. char *cli_addr = NULL;
  914. char *conf = NULL;
  915. int timeout = 30;
  916. char *pos;
  917. struct extra_radius_attr *p = NULL, *p1;
  918. if (os_program_init())
  919. return -1;
  920. hostapd_logger_register_cb(hostapd_logger_cb);
  921. os_memset(&eapol_test, 0, sizeof(eapol_test));
  922. eapol_test.connect_info = "CONNECT 11Mbps 802.11b";
  923. os_memcpy(eapol_test.own_addr, "\x02\x00\x00\x00\x00\x01", ETH_ALEN);
  924. wpa_debug_level = 0;
  925. wpa_debug_show_keys = 1;
  926. for (;;) {
  927. c = getopt(argc, argv, "a:A:c:C:M:nN:o:p:r:s:St:W");
  928. if (c < 0)
  929. break;
  930. switch (c) {
  931. case 'a':
  932. as_addr = optarg;
  933. break;
  934. case 'A':
  935. cli_addr = optarg;
  936. break;
  937. case 'c':
  938. conf = optarg;
  939. break;
  940. case 'C':
  941. eapol_test.connect_info = optarg;
  942. break;
  943. case 'M':
  944. if (hwaddr_aton(optarg, eapol_test.own_addr)) {
  945. usage();
  946. return -1;
  947. }
  948. break;
  949. case 'n':
  950. eapol_test.no_mppe_keys++;
  951. break;
  952. case 'o':
  953. if (eapol_test.server_cert_file)
  954. fclose(eapol_test.server_cert_file);
  955. eapol_test.server_cert_file = fopen(optarg, "w");
  956. if (eapol_test.server_cert_file == NULL) {
  957. printf("Could not open '%s' for writing\n",
  958. optarg);
  959. return -1;
  960. }
  961. break;
  962. case 'p':
  963. as_port = atoi(optarg);
  964. break;
  965. case 'r':
  966. eapol_test.eapol_test_num_reauths = atoi(optarg);
  967. break;
  968. case 's':
  969. as_secret = optarg;
  970. break;
  971. case 'S':
  972. save_config++;
  973. break;
  974. case 't':
  975. timeout = atoi(optarg);
  976. break;
  977. case 'W':
  978. wait_for_monitor++;
  979. break;
  980. case 'N':
  981. p1 = os_zalloc(sizeof(p1));
  982. if (p1 == NULL)
  983. break;
  984. if (!p)
  985. eapol_test.extra_attrs = p1;
  986. else
  987. p->next = p1;
  988. p = p1;
  989. p->type = atoi(optarg);
  990. pos = os_strchr(optarg, ':');
  991. if (pos == NULL) {
  992. p->syntax = 'n';
  993. p->data = NULL;
  994. break;
  995. }
  996. pos++;
  997. if (pos[0] == '\0' || pos[1] != ':') {
  998. printf("Incorrect format of attribute "
  999. "specification\n");
  1000. break;
  1001. }
  1002. p->syntax = pos[0];
  1003. p->data = pos + 2;
  1004. break;
  1005. default:
  1006. usage();
  1007. return -1;
  1008. }
  1009. }
  1010. if (argc > optind && os_strcmp(argv[optind], "scard") == 0) {
  1011. return scard_test();
  1012. }
  1013. if (argc > optind && os_strcmp(argv[optind], "sim") == 0) {
  1014. return scard_get_triplets(argc - optind - 1,
  1015. &argv[optind + 1]);
  1016. }
  1017. if (conf == NULL) {
  1018. usage();
  1019. printf("Configuration file is required.\n");
  1020. return -1;
  1021. }
  1022. if (eap_register_methods()) {
  1023. wpa_printf(MSG_ERROR, "Failed to register EAP methods");
  1024. return -1;
  1025. }
  1026. if (eloop_init()) {
  1027. wpa_printf(MSG_ERROR, "Failed to initialize event loop");
  1028. return -1;
  1029. }
  1030. os_memset(&wpa_s, 0, sizeof(wpa_s));
  1031. eapol_test.wpa_s = &wpa_s;
  1032. wpa_s.conf = wpa_config_read(conf);
  1033. if (wpa_s.conf == NULL) {
  1034. printf("Failed to parse configuration file '%s'.\n", conf);
  1035. return -1;
  1036. }
  1037. if (wpa_s.conf->ssid == NULL) {
  1038. printf("No networks defined.\n");
  1039. return -1;
  1040. }
  1041. wpa_init_conf(&eapol_test, &wpa_s, as_addr, as_port, as_secret,
  1042. cli_addr);
  1043. wpa_s.ctrl_iface = wpa_supplicant_ctrl_iface_init(&wpa_s);
  1044. if (wpa_s.ctrl_iface == NULL) {
  1045. printf("Failed to initialize control interface '%s'.\n"
  1046. "You may have another eapol_test process already "
  1047. "running or the file was\n"
  1048. "left by an unclean termination of eapol_test in "
  1049. "which case you will need\n"
  1050. "to manually remove this file before starting "
  1051. "eapol_test again.\n",
  1052. wpa_s.conf->ctrl_interface);
  1053. return -1;
  1054. }
  1055. if (wpa_supplicant_scard_init(&wpa_s, wpa_s.conf->ssid))
  1056. return -1;
  1057. if (test_eapol(&eapol_test, &wpa_s, wpa_s.conf->ssid))
  1058. return -1;
  1059. if (wait_for_monitor)
  1060. wpa_supplicant_ctrl_iface_wait(wpa_s.ctrl_iface);
  1061. eloop_register_timeout(timeout, 0, eapol_test_timeout, &eapol_test,
  1062. NULL);
  1063. eloop_register_timeout(0, 0, send_eap_request_identity, &wpa_s, NULL);
  1064. eloop_register_signal_terminate(eapol_test_terminate, &wpa_s);
  1065. eloop_register_signal_reconfig(eapol_test_terminate, &wpa_s);
  1066. eloop_run();
  1067. eloop_cancel_timeout(eapol_test_timeout, &eapol_test, NULL);
  1068. eloop_cancel_timeout(eapol_sm_reauth, &eapol_test, NULL);
  1069. if (eapol_test_compare_pmk(&eapol_test) == 0 ||
  1070. eapol_test.no_mppe_keys)
  1071. ret = 0;
  1072. if (eapol_test.auth_timed_out)
  1073. ret = -2;
  1074. if (eapol_test.radius_access_reject_received)
  1075. ret = -3;
  1076. if (save_config)
  1077. wpa_config_write(conf, wpa_s.conf);
  1078. test_eapol_clean(&eapol_test, &wpa_s);
  1079. eap_peer_unregister_methods();
  1080. #ifdef CONFIG_AP
  1081. eap_server_unregister_methods();
  1082. #endif /* CONFIG_AP */
  1083. eloop_destroy();
  1084. if (eapol_test.server_cert_file)
  1085. fclose(eapol_test.server_cert_file);
  1086. printf("MPPE keys OK: %d mismatch: %d\n",
  1087. eapol_test.num_mppe_ok, eapol_test.num_mppe_mismatch);
  1088. if (eapol_test.num_mppe_mismatch)
  1089. ret = -4;
  1090. if (ret)
  1091. printf("FAILURE\n");
  1092. else
  1093. printf("SUCCESS\n");
  1094. os_program_deinit();
  1095. return ret;
  1096. }