eapol_test.c 33 KB

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