eapol_test.c 39 KB

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