eapol_test.c 37 KB

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