eapol_test.c 37 KB

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