eapol_test.c 35 KB

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