eapol_test.c 33 KB

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