eapol_test.c 35 KB

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