eapol_test.c 32 KB

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