eapol_test.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091
  1. /*
  2. * WPA Supplicant - test code
  3. * Copyright (c) 2003-2007, Jouni Malinen <j@w1.fi>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. *
  9. * Alternatively, this software may be distributed under the terms of BSD
  10. * license.
  11. *
  12. * See README and COPYING for more details.
  13. *
  14. * IEEE 802.1X Supplicant test code (to be used in place of wpa_supplicant.c.
  15. * Not used in production version.
  16. */
  17. #include "includes.h"
  18. #include <assert.h>
  19. #include "common.h"
  20. #include "config.h"
  21. #include "eapol_supp/eapol_supp_sm.h"
  22. #include "eap_peer/eap.h"
  23. #include "eloop.h"
  24. #include "wpa.h"
  25. #include "eap_peer/eap_i.h"
  26. #include "wpa_supplicant_i.h"
  27. #include "radius/radius.h"
  28. #include "radius/radius_client.h"
  29. #include "ctrl_iface.h"
  30. #include "pcsc_funcs.h"
  31. extern int wpa_debug_level;
  32. extern int wpa_debug_show_keys;
  33. struct wpa_driver_ops *wpa_supplicant_drivers[] = { NULL };
  34. struct eapol_test_data {
  35. struct wpa_supplicant *wpa_s;
  36. int eapol_test_num_reauths;
  37. int no_mppe_keys;
  38. int num_mppe_ok, num_mppe_mismatch;
  39. u8 radius_identifier;
  40. struct radius_msg *last_recv_radius;
  41. struct in_addr own_ip_addr;
  42. struct radius_client_data *radius;
  43. struct hostapd_radius_servers *radius_conf;
  44. u8 *last_eap_radius; /* last received EAP Response from Authentication
  45. * Server */
  46. size_t last_eap_radius_len;
  47. u8 authenticator_pmk[PMK_LEN];
  48. size_t authenticator_pmk_len;
  49. int radius_access_accept_received;
  50. int radius_access_reject_received;
  51. int auth_timed_out;
  52. u8 *eap_identity;
  53. size_t eap_identity_len;
  54. char *connect_info;
  55. u8 own_addr[ETH_ALEN];
  56. int cui_flag;
  57. char *cui_str;
  58. };
  59. static struct eapol_test_data eapol_test;
  60. static void send_eap_request_identity(void *eloop_ctx, void *timeout_ctx);
  61. static void hostapd_logger_cb(void *ctx, const u8 *addr, unsigned int module,
  62. int level, const char *txt, size_t len)
  63. {
  64. if (addr)
  65. wpa_printf(MSG_DEBUG, "STA " MACSTR ": %s\n",
  66. MAC2STR(addr), txt);
  67. else
  68. wpa_printf(MSG_DEBUG, "%s", txt);
  69. }
  70. static void ieee802_1x_encapsulate_radius(struct eapol_test_data *e,
  71. const u8 *eap, size_t len)
  72. {
  73. struct radius_msg *msg;
  74. char buf[128];
  75. const struct eap_hdr *hdr;
  76. const u8 *pos;
  77. wpa_printf(MSG_DEBUG, "Encapsulating EAP message into a RADIUS "
  78. "packet");
  79. e->radius_identifier = radius_client_get_id(e->radius);
  80. msg = radius_msg_new(RADIUS_CODE_ACCESS_REQUEST,
  81. e->radius_identifier);
  82. if (msg == NULL) {
  83. printf("Could not create net RADIUS packet\n");
  84. return;
  85. }
  86. radius_msg_make_authenticator(msg, (u8 *) e, sizeof(*e));
  87. hdr = (const struct eap_hdr *) eap;
  88. pos = (const u8 *) (hdr + 1);
  89. if (len > sizeof(*hdr) && hdr->code == EAP_CODE_RESPONSE &&
  90. pos[0] == EAP_TYPE_IDENTITY) {
  91. pos++;
  92. os_free(e->eap_identity);
  93. e->eap_identity_len = len - sizeof(*hdr) - 1;
  94. e->eap_identity = os_malloc(e->eap_identity_len);
  95. if (e->eap_identity) {
  96. os_memcpy(e->eap_identity, pos, e->eap_identity_len);
  97. wpa_hexdump(MSG_DEBUG, "Learned identity from "
  98. "EAP-Response-Identity",
  99. e->eap_identity, e->eap_identity_len);
  100. }
  101. }
  102. if (e->eap_identity &&
  103. !radius_msg_add_attr(msg, RADIUS_ATTR_USER_NAME,
  104. e->eap_identity, e->eap_identity_len)) {
  105. printf("Could not add User-Name\n");
  106. goto fail;
  107. }
  108. if (!radius_msg_add_attr(msg, RADIUS_ATTR_NAS_IP_ADDRESS,
  109. (u8 *) &e->own_ip_addr, 4)) {
  110. printf("Could not add NAS-IP-Address\n");
  111. goto fail;
  112. }
  113. os_snprintf(buf, sizeof(buf), RADIUS_802_1X_ADDR_FORMAT,
  114. MAC2STR(e->wpa_s->own_addr));
  115. if (!radius_msg_add_attr(msg, RADIUS_ATTR_CALLING_STATION_ID,
  116. (u8 *) buf, os_strlen(buf))) {
  117. printf("Could not add Calling-Station-Id\n");
  118. goto fail;
  119. }
  120. /* TODO: should probably check MTU from driver config; 2304 is max for
  121. * IEEE 802.11, but use 1400 to avoid problems with too large packets
  122. */
  123. if (!radius_msg_add_attr_int32(msg, RADIUS_ATTR_FRAMED_MTU, 1400)) {
  124. printf("Could not add Framed-MTU\n");
  125. goto fail;
  126. }
  127. if (!radius_msg_add_attr_int32(msg, RADIUS_ATTR_NAS_PORT_TYPE,
  128. RADIUS_NAS_PORT_TYPE_IEEE_802_11)) {
  129. printf("Could not add NAS-Port-Type\n");
  130. goto fail;
  131. }
  132. os_snprintf(buf, sizeof(buf), "%s", e->connect_info);
  133. if (!radius_msg_add_attr(msg, RADIUS_ATTR_CONNECT_INFO,
  134. (u8 *) buf, os_strlen(buf))) {
  135. printf("Could not add Connect-Info\n");
  136. goto fail;
  137. }
  138. if (e->cui_flag) {
  139. int l = 0;
  140. if (e->cui_flag == 1) {
  141. l = 1;
  142. buf[0] = '\0';
  143. } else if (e->cui_flag == 2) {
  144. os_snprintf(buf, sizeof(buf), "%s", e->cui_str);
  145. l = os_strlen(buf);
  146. }
  147. if (!radius_msg_add_attr(msg,
  148. RADIUS_ATTR_CHARGEABLE_USER_IDENTITY,
  149. (u8 *) buf, l)) {
  150. printf("Could not add Chargeable-User-Identity\n");
  151. goto fail;
  152. }
  153. }
  154. if (eap && !radius_msg_add_eap(msg, eap, len)) {
  155. printf("Could not add EAP-Message\n");
  156. goto fail;
  157. }
  158. /* State attribute must be copied if and only if this packet is
  159. * Access-Request reply to the previous Access-Challenge */
  160. if (e->last_recv_radius && e->last_recv_radius->hdr->code ==
  161. RADIUS_CODE_ACCESS_CHALLENGE) {
  162. int res = radius_msg_copy_attr(msg, e->last_recv_radius,
  163. RADIUS_ATTR_STATE);
  164. if (res < 0) {
  165. printf("Could not copy State attribute from previous "
  166. "Access-Challenge\n");
  167. goto fail;
  168. }
  169. if (res > 0) {
  170. wpa_printf(MSG_DEBUG, " Copied RADIUS State "
  171. "Attribute");
  172. }
  173. }
  174. radius_client_send(e->radius, msg, RADIUS_AUTH, e->wpa_s->own_addr);
  175. return;
  176. fail:
  177. radius_msg_free(msg);
  178. os_free(msg);
  179. }
  180. static int eapol_test_eapol_send(void *ctx, int type, const u8 *buf,
  181. size_t len)
  182. {
  183. /* struct wpa_supplicant *wpa_s = ctx; */
  184. printf("WPA: eapol_test_eapol_send(type=%d len=%lu)\n",
  185. type, (unsigned long) len);
  186. if (type == IEEE802_1X_TYPE_EAP_PACKET) {
  187. wpa_hexdump(MSG_DEBUG, "TX EAP -> RADIUS", buf, len);
  188. ieee802_1x_encapsulate_radius(&eapol_test, buf, len);
  189. }
  190. return 0;
  191. }
  192. static void eapol_test_set_config_blob(void *ctx,
  193. struct wpa_config_blob *blob)
  194. {
  195. struct wpa_supplicant *wpa_s = ctx;
  196. wpa_config_set_blob(wpa_s->conf, blob);
  197. }
  198. static const struct wpa_config_blob *
  199. eapol_test_get_config_blob(void *ctx, const char *name)
  200. {
  201. struct wpa_supplicant *wpa_s = ctx;
  202. return wpa_config_get_blob(wpa_s->conf, name);
  203. }
  204. static void eapol_test_eapol_done_cb(void *ctx)
  205. {
  206. printf("WPA: EAPOL processing complete\n");
  207. }
  208. static void eapol_sm_reauth(void *eloop_ctx, void *timeout_ctx)
  209. {
  210. struct eapol_test_data *e = eloop_ctx;
  211. printf("\n\n\n\n\neapol_test: Triggering EAP reauthentication\n\n");
  212. e->radius_access_accept_received = 0;
  213. send_eap_request_identity(e->wpa_s, NULL);
  214. }
  215. static int eapol_test_compare_pmk(struct eapol_test_data *e)
  216. {
  217. u8 pmk[PMK_LEN];
  218. int ret = 1;
  219. if (eapol_sm_get_key(e->wpa_s->eapol, pmk, PMK_LEN) == 0) {
  220. wpa_hexdump(MSG_DEBUG, "PMK from EAPOL", pmk, PMK_LEN);
  221. if (os_memcmp(pmk, e->authenticator_pmk, PMK_LEN) != 0) {
  222. printf("WARNING: PMK mismatch\n");
  223. wpa_hexdump(MSG_DEBUG, "PMK from AS",
  224. e->authenticator_pmk, PMK_LEN);
  225. } else if (e->radius_access_accept_received)
  226. ret = 0;
  227. } else if (e->authenticator_pmk_len == 16 &&
  228. eapol_sm_get_key(e->wpa_s->eapol, pmk, 16) == 0) {
  229. wpa_hexdump(MSG_DEBUG, "LEAP PMK from EAPOL", pmk, 16);
  230. if (os_memcmp(pmk, e->authenticator_pmk, 16) != 0) {
  231. printf("WARNING: PMK mismatch\n");
  232. wpa_hexdump(MSG_DEBUG, "PMK from AS",
  233. e->authenticator_pmk, 16);
  234. } else if (e->radius_access_accept_received)
  235. ret = 0;
  236. } else if (e->radius_access_accept_received && e->no_mppe_keys) {
  237. /* No keying material expected */
  238. ret = 0;
  239. }
  240. if (ret && !e->no_mppe_keys)
  241. e->num_mppe_mismatch++;
  242. else if (!e->no_mppe_keys)
  243. e->num_mppe_ok++;
  244. return ret;
  245. }
  246. static void eapol_sm_cb(struct eapol_sm *eapol, int success, void *ctx)
  247. {
  248. struct eapol_test_data *e = ctx;
  249. printf("eapol_sm_cb: success=%d\n", success);
  250. e->eapol_test_num_reauths--;
  251. if (e->eapol_test_num_reauths < 0)
  252. eloop_terminate();
  253. else {
  254. eapol_test_compare_pmk(e);
  255. eloop_register_timeout(0, 100000, eapol_sm_reauth, e, NULL);
  256. }
  257. }
  258. static int test_eapol(struct eapol_test_data *e, struct wpa_supplicant *wpa_s,
  259. struct wpa_ssid *ssid)
  260. {
  261. struct eapol_config eapol_conf;
  262. struct eapol_ctx *ctx;
  263. ctx = os_zalloc(sizeof(*ctx));
  264. if (ctx == NULL) {
  265. printf("Failed to allocate EAPOL context.\n");
  266. return -1;
  267. }
  268. ctx->ctx = wpa_s;
  269. ctx->msg_ctx = wpa_s;
  270. ctx->scard_ctx = wpa_s->scard;
  271. ctx->cb = eapol_sm_cb;
  272. ctx->cb_ctx = e;
  273. ctx->eapol_send_ctx = wpa_s;
  274. ctx->preauth = 0;
  275. ctx->eapol_done_cb = eapol_test_eapol_done_cb;
  276. ctx->eapol_send = eapol_test_eapol_send;
  277. ctx->set_config_blob = eapol_test_set_config_blob;
  278. ctx->get_config_blob = eapol_test_get_config_blob;
  279. #ifdef EAP_TLS_OPENSSL
  280. ctx->opensc_engine_path = wpa_s->conf->opensc_engine_path;
  281. ctx->pkcs11_engine_path = wpa_s->conf->pkcs11_engine_path;
  282. ctx->pkcs11_module_path = wpa_s->conf->pkcs11_module_path;
  283. #endif /* EAP_TLS_OPENSSL */
  284. wpa_s->eapol = eapol_sm_init(ctx);
  285. if (wpa_s->eapol == NULL) {
  286. os_free(ctx);
  287. printf("Failed to initialize EAPOL state machines.\n");
  288. return -1;
  289. }
  290. wpa_s->current_ssid = ssid;
  291. os_memset(&eapol_conf, 0, sizeof(eapol_conf));
  292. eapol_conf.accept_802_1x_keys = 1;
  293. eapol_conf.required_keys = 0;
  294. eapol_conf.fast_reauth = wpa_s->conf->fast_reauth;
  295. eapol_conf.workaround = ssid->eap_workaround;
  296. eapol_sm_notify_config(wpa_s->eapol, &ssid->eap, &eapol_conf);
  297. eapol_sm_register_scard_ctx(wpa_s->eapol, wpa_s->scard);
  298. eapol_sm_notify_portValid(wpa_s->eapol, FALSE);
  299. /* 802.1X::portControl = Auto */
  300. eapol_sm_notify_portEnabled(wpa_s->eapol, TRUE);
  301. return 0;
  302. }
  303. static void test_eapol_clean(struct eapol_test_data *e,
  304. struct wpa_supplicant *wpa_s)
  305. {
  306. radius_client_deinit(e->radius);
  307. os_free(e->last_eap_radius);
  308. if (e->last_recv_radius) {
  309. radius_msg_free(e->last_recv_radius);
  310. os_free(e->last_recv_radius);
  311. }
  312. os_free(e->eap_identity);
  313. e->eap_identity = NULL;
  314. eapol_sm_deinit(wpa_s->eapol);
  315. wpa_s->eapol = NULL;
  316. if (e->radius_conf && e->radius_conf->auth_server) {
  317. os_free(e->radius_conf->auth_server->shared_secret);
  318. os_free(e->radius_conf->auth_server);
  319. }
  320. os_free(e->radius_conf);
  321. e->radius_conf = NULL;
  322. scard_deinit(wpa_s->scard);
  323. if (wpa_s->ctrl_iface) {
  324. wpa_supplicant_ctrl_iface_deinit(wpa_s->ctrl_iface);
  325. wpa_s->ctrl_iface = NULL;
  326. }
  327. wpa_config_free(wpa_s->conf);
  328. }
  329. static void send_eap_request_identity(void *eloop_ctx, void *timeout_ctx)
  330. {
  331. struct wpa_supplicant *wpa_s = eloop_ctx;
  332. u8 buf[100], *pos;
  333. struct ieee802_1x_hdr *hdr;
  334. struct eap_hdr *eap;
  335. hdr = (struct ieee802_1x_hdr *) buf;
  336. hdr->version = EAPOL_VERSION;
  337. hdr->type = IEEE802_1X_TYPE_EAP_PACKET;
  338. hdr->length = htons(5);
  339. eap = (struct eap_hdr *) (hdr + 1);
  340. eap->code = EAP_CODE_REQUEST;
  341. eap->identifier = 0;
  342. eap->length = htons(5);
  343. pos = (u8 *) (eap + 1);
  344. *pos = EAP_TYPE_IDENTITY;
  345. printf("Sending fake EAP-Request-Identity\n");
  346. eapol_sm_rx_eapol(wpa_s->eapol, wpa_s->bssid, buf,
  347. sizeof(*hdr) + 5);
  348. }
  349. static void eapol_test_timeout(void *eloop_ctx, void *timeout_ctx)
  350. {
  351. struct eapol_test_data *e = eloop_ctx;
  352. printf("EAPOL test timed out\n");
  353. e->auth_timed_out = 1;
  354. eloop_terminate();
  355. }
  356. static char *eap_type_text(u8 type)
  357. {
  358. switch (type) {
  359. case EAP_TYPE_IDENTITY: return "Identity";
  360. case EAP_TYPE_NOTIFICATION: return "Notification";
  361. case EAP_TYPE_NAK: return "Nak";
  362. case EAP_TYPE_TLS: return "TLS";
  363. case EAP_TYPE_TTLS: return "TTLS";
  364. case EAP_TYPE_PEAP: return "PEAP";
  365. case EAP_TYPE_SIM: return "SIM";
  366. case EAP_TYPE_GTC: return "GTC";
  367. case EAP_TYPE_MD5: return "MD5";
  368. case EAP_TYPE_OTP: return "OTP";
  369. case EAP_TYPE_FAST: return "FAST";
  370. case EAP_TYPE_SAKE: return "SAKE";
  371. case EAP_TYPE_PSK: return "PSK";
  372. default: return "Unknown";
  373. }
  374. }
  375. static void ieee802_1x_decapsulate_radius(struct eapol_test_data *e)
  376. {
  377. u8 *eap;
  378. size_t len;
  379. struct eap_hdr *hdr;
  380. int eap_type = -1;
  381. char buf[64];
  382. struct radius_msg *msg;
  383. if (e->last_recv_radius == NULL)
  384. return;
  385. msg = e->last_recv_radius;
  386. eap = radius_msg_get_eap(msg, &len);
  387. if (eap == NULL) {
  388. /* draft-aboba-radius-rfc2869bis-20.txt, Chap. 2.6.3:
  389. * RADIUS server SHOULD NOT send Access-Reject/no EAP-Message
  390. * attribute */
  391. wpa_printf(MSG_DEBUG, "could not extract "
  392. "EAP-Message from RADIUS message");
  393. os_free(e->last_eap_radius);
  394. e->last_eap_radius = NULL;
  395. e->last_eap_radius_len = 0;
  396. return;
  397. }
  398. if (len < sizeof(*hdr)) {
  399. wpa_printf(MSG_DEBUG, "too short EAP packet "
  400. "received from authentication server");
  401. os_free(eap);
  402. return;
  403. }
  404. if (len > sizeof(*hdr))
  405. eap_type = eap[sizeof(*hdr)];
  406. hdr = (struct eap_hdr *) eap;
  407. switch (hdr->code) {
  408. case EAP_CODE_REQUEST:
  409. os_snprintf(buf, sizeof(buf), "EAP-Request-%s (%d)",
  410. eap_type >= 0 ? eap_type_text(eap_type) : "??",
  411. eap_type);
  412. break;
  413. case EAP_CODE_RESPONSE:
  414. os_snprintf(buf, sizeof(buf), "EAP Response-%s (%d)",
  415. eap_type >= 0 ? eap_type_text(eap_type) : "??",
  416. eap_type);
  417. break;
  418. case EAP_CODE_SUCCESS:
  419. os_strlcpy(buf, "EAP Success", sizeof(buf));
  420. /* LEAP uses EAP Success within an authentication, so must not
  421. * stop here with eloop_terminate(); */
  422. break;
  423. case EAP_CODE_FAILURE:
  424. os_strlcpy(buf, "EAP Failure", sizeof(buf));
  425. eloop_terminate();
  426. break;
  427. default:
  428. os_strlcpy(buf, "unknown EAP code", sizeof(buf));
  429. wpa_hexdump(MSG_DEBUG, "Decapsulated EAP packet", eap, len);
  430. break;
  431. }
  432. wpa_printf(MSG_DEBUG, "decapsulated EAP packet (code=%d "
  433. "id=%d len=%d) from RADIUS server: %s",
  434. hdr->code, hdr->identifier, ntohs(hdr->length), buf);
  435. /* sta->eapol_sm->be_auth.idFromServer = hdr->identifier; */
  436. os_free(e->last_eap_radius);
  437. e->last_eap_radius = eap;
  438. e->last_eap_radius_len = len;
  439. {
  440. struct ieee802_1x_hdr *dot1x;
  441. dot1x = os_malloc(sizeof(*dot1x) + len);
  442. assert(dot1x != NULL);
  443. dot1x->version = EAPOL_VERSION;
  444. dot1x->type = IEEE802_1X_TYPE_EAP_PACKET;
  445. dot1x->length = htons(len);
  446. os_memcpy((u8 *) (dot1x + 1), eap, len);
  447. eapol_sm_rx_eapol(e->wpa_s->eapol, e->wpa_s->bssid,
  448. (u8 *) dot1x, sizeof(*dot1x) + len);
  449. os_free(dot1x);
  450. }
  451. }
  452. static void ieee802_1x_get_keys(struct eapol_test_data *e,
  453. struct radius_msg *msg, struct radius_msg *req,
  454. u8 *shared_secret, size_t shared_secret_len)
  455. {
  456. struct radius_ms_mppe_keys *keys;
  457. keys = radius_msg_get_ms_keys(msg, req, shared_secret,
  458. shared_secret_len);
  459. if (keys && keys->send == NULL && keys->recv == NULL) {
  460. os_free(keys);
  461. keys = radius_msg_get_cisco_keys(msg, req, shared_secret,
  462. shared_secret_len);
  463. }
  464. if (keys) {
  465. if (keys->send) {
  466. wpa_hexdump(MSG_DEBUG, "MS-MPPE-Send-Key (sign)",
  467. keys->send, keys->send_len);
  468. }
  469. if (keys->recv) {
  470. wpa_hexdump(MSG_DEBUG, "MS-MPPE-Recv-Key (crypt)",
  471. keys->recv, keys->recv_len);
  472. e->authenticator_pmk_len =
  473. keys->recv_len > PMK_LEN ? PMK_LEN :
  474. keys->recv_len;
  475. os_memcpy(e->authenticator_pmk, keys->recv,
  476. e->authenticator_pmk_len);
  477. }
  478. os_free(keys->send);
  479. os_free(keys->recv);
  480. os_free(keys);
  481. }
  482. }
  483. /* Process the RADIUS frames from Authentication Server */
  484. static RadiusRxResult
  485. ieee802_1x_receive_auth(struct radius_msg *msg, struct radius_msg *req,
  486. u8 *shared_secret, size_t shared_secret_len,
  487. void *data)
  488. {
  489. struct eapol_test_data *e = data;
  490. /* RFC 2869, Ch. 5.13: valid Message-Authenticator attribute MUST be
  491. * present when packet contains an EAP-Message attribute */
  492. if (msg->hdr->code == RADIUS_CODE_ACCESS_REJECT &&
  493. radius_msg_get_attr(msg, RADIUS_ATTR_MESSAGE_AUTHENTICATOR, NULL,
  494. 0) < 0 &&
  495. radius_msg_get_attr(msg, RADIUS_ATTR_EAP_MESSAGE, NULL, 0) < 0) {
  496. wpa_printf(MSG_DEBUG, "Allowing RADIUS "
  497. "Access-Reject without Message-Authenticator "
  498. "since it does not include EAP-Message\n");
  499. } else if (radius_msg_verify(msg, shared_secret, shared_secret_len,
  500. req, 1)) {
  501. printf("Incoming RADIUS packet did not have correct "
  502. "Message-Authenticator - dropped\n");
  503. return RADIUS_RX_UNKNOWN;
  504. }
  505. if (msg->hdr->code != RADIUS_CODE_ACCESS_ACCEPT &&
  506. msg->hdr->code != RADIUS_CODE_ACCESS_REJECT &&
  507. msg->hdr->code != RADIUS_CODE_ACCESS_CHALLENGE) {
  508. printf("Unknown RADIUS message code\n");
  509. return RADIUS_RX_UNKNOWN;
  510. }
  511. e->radius_identifier = -1;
  512. wpa_printf(MSG_DEBUG, "RADIUS packet matching with station");
  513. if (e->last_recv_radius) {
  514. radius_msg_free(e->last_recv_radius);
  515. os_free(e->last_recv_radius);
  516. }
  517. e->last_recv_radius = msg;
  518. switch (msg->hdr->code) {
  519. case RADIUS_CODE_ACCESS_ACCEPT:
  520. e->radius_access_accept_received = 1;
  521. ieee802_1x_get_keys(e, msg, req, shared_secret,
  522. shared_secret_len);
  523. break;
  524. case RADIUS_CODE_ACCESS_REJECT:
  525. e->radius_access_reject_received = 1;
  526. break;
  527. }
  528. ieee802_1x_decapsulate_radius(e);
  529. if ((msg->hdr->code == RADIUS_CODE_ACCESS_ACCEPT &&
  530. e->eapol_test_num_reauths < 0) ||
  531. msg->hdr->code == RADIUS_CODE_ACCESS_REJECT) {
  532. eloop_terminate();
  533. }
  534. return RADIUS_RX_QUEUED;
  535. }
  536. static void wpa_init_conf(struct eapol_test_data *e,
  537. struct wpa_supplicant *wpa_s, const char *authsrv,
  538. int port, const char *secret,
  539. const char *cli_addr)
  540. {
  541. struct hostapd_radius_server *as;
  542. int res;
  543. wpa_s->bssid[5] = 1;
  544. os_memcpy(wpa_s->own_addr, e->own_addr, ETH_ALEN);
  545. e->own_ip_addr.s_addr = htonl((127 << 24) | 1);
  546. os_strlcpy(wpa_s->ifname, "test", sizeof(wpa_s->ifname));
  547. e->radius_conf = os_zalloc(sizeof(struct hostapd_radius_servers));
  548. assert(e->radius_conf != NULL);
  549. e->radius_conf->num_auth_servers = 1;
  550. as = os_zalloc(sizeof(struct hostapd_radius_server));
  551. assert(as != NULL);
  552. #if defined(CONFIG_NATIVE_WINDOWS) || defined(CONFIG_ANSI_C_EXTRA)
  553. {
  554. int a[4];
  555. u8 *pos;
  556. sscanf(authsrv, "%d.%d.%d.%d", &a[0], &a[1], &a[2], &a[3]);
  557. pos = (u8 *) &as->addr.u.v4;
  558. *pos++ = a[0];
  559. *pos++ = a[1];
  560. *pos++ = a[2];
  561. *pos++ = a[3];
  562. }
  563. #else /* CONFIG_NATIVE_WINDOWS or CONFIG_ANSI_C_EXTRA */
  564. inet_aton(authsrv, &as->addr.u.v4);
  565. #endif /* CONFIG_NATIVE_WINDOWS or CONFIG_ANSI_C_EXTRA */
  566. as->addr.af = AF_INET;
  567. as->port = port;
  568. as->shared_secret = (u8 *) os_strdup(secret);
  569. as->shared_secret_len = os_strlen(secret);
  570. e->radius_conf->auth_server = as;
  571. e->radius_conf->auth_servers = as;
  572. e->radius_conf->msg_dumps = 1;
  573. if (cli_addr) {
  574. if (hostapd_parse_ip_addr(cli_addr,
  575. &e->radius_conf->client_addr) == 0)
  576. e->radius_conf->force_client_addr = 1;
  577. else {
  578. wpa_printf(MSG_ERROR, "Invalid IP address '%s'",
  579. cli_addr);
  580. assert(0);
  581. }
  582. }
  583. e->radius = radius_client_init(wpa_s, e->radius_conf);
  584. assert(e->radius != NULL);
  585. res = radius_client_register(e->radius, RADIUS_AUTH,
  586. ieee802_1x_receive_auth, e);
  587. assert(res == 0);
  588. }
  589. static int scard_test(void)
  590. {
  591. struct scard_data *scard;
  592. size_t len;
  593. char imsi[20];
  594. unsigned char _rand[16];
  595. #ifdef PCSC_FUNCS
  596. unsigned char sres[4];
  597. unsigned char kc[8];
  598. #endif /* PCSC_FUNCS */
  599. #define num_triplets 5
  600. unsigned char rand_[num_triplets][16];
  601. unsigned char sres_[num_triplets][4];
  602. unsigned char kc_[num_triplets][8];
  603. int i, res;
  604. size_t j;
  605. #define AKA_RAND_LEN 16
  606. #define AKA_AUTN_LEN 16
  607. #define AKA_AUTS_LEN 14
  608. #define RES_MAX_LEN 16
  609. #define IK_LEN 16
  610. #define CK_LEN 16
  611. unsigned char aka_rand[AKA_RAND_LEN];
  612. unsigned char aka_autn[AKA_AUTN_LEN];
  613. unsigned char aka_auts[AKA_AUTS_LEN];
  614. unsigned char aka_res[RES_MAX_LEN];
  615. size_t aka_res_len;
  616. unsigned char aka_ik[IK_LEN];
  617. unsigned char aka_ck[CK_LEN];
  618. scard = scard_init(SCARD_TRY_BOTH);
  619. if (scard == NULL)
  620. return -1;
  621. if (scard_set_pin(scard, "1234")) {
  622. wpa_printf(MSG_WARNING, "PIN validation failed");
  623. scard_deinit(scard);
  624. return -1;
  625. }
  626. len = sizeof(imsi);
  627. if (scard_get_imsi(scard, imsi, &len))
  628. goto failed;
  629. wpa_hexdump_ascii(MSG_DEBUG, "SCARD: IMSI", (u8 *) imsi, len);
  630. /* NOTE: Permanent Username: 1 | IMSI */
  631. os_memset(_rand, 0, sizeof(_rand));
  632. if (scard_gsm_auth(scard, _rand, sres, kc))
  633. goto failed;
  634. os_memset(_rand, 0xff, sizeof(_rand));
  635. if (scard_gsm_auth(scard, _rand, sres, kc))
  636. goto failed;
  637. for (i = 0; i < num_triplets; i++) {
  638. os_memset(rand_[i], i, sizeof(rand_[i]));
  639. if (scard_gsm_auth(scard, rand_[i], sres_[i], kc_[i]))
  640. goto failed;
  641. }
  642. for (i = 0; i < num_triplets; i++) {
  643. printf("1");
  644. for (j = 0; j < len; j++)
  645. printf("%c", imsi[j]);
  646. printf(",");
  647. for (j = 0; j < 16; j++)
  648. printf("%02X", rand_[i][j]);
  649. printf(",");
  650. for (j = 0; j < 4; j++)
  651. printf("%02X", sres_[i][j]);
  652. printf(",");
  653. for (j = 0; j < 8; j++)
  654. printf("%02X", kc_[i][j]);
  655. printf("\n");
  656. }
  657. wpa_printf(MSG_DEBUG, "Trying to use UMTS authentication");
  658. /* seq 39 (0x28) */
  659. os_memset(aka_rand, 0xaa, 16);
  660. os_memcpy(aka_autn, "\x86\x71\x31\xcb\xa2\xfc\x61\xdf"
  661. "\xa3\xb3\x97\x9d\x07\x32\xa2\x12", 16);
  662. res = scard_umts_auth(scard, aka_rand, aka_autn, aka_res, &aka_res_len,
  663. aka_ik, aka_ck, aka_auts);
  664. if (res == 0) {
  665. wpa_printf(MSG_DEBUG, "UMTS auth completed successfully");
  666. wpa_hexdump(MSG_DEBUG, "RES", aka_res, aka_res_len);
  667. wpa_hexdump(MSG_DEBUG, "IK", aka_ik, IK_LEN);
  668. wpa_hexdump(MSG_DEBUG, "CK", aka_ck, CK_LEN);
  669. } else if (res == -2) {
  670. wpa_printf(MSG_DEBUG, "UMTS auth resulted in synchronization "
  671. "failure");
  672. wpa_hexdump(MSG_DEBUG, "AUTS", aka_auts, AKA_AUTS_LEN);
  673. } else {
  674. wpa_printf(MSG_DEBUG, "UMTS auth failed");
  675. }
  676. failed:
  677. scard_deinit(scard);
  678. return 0;
  679. #undef num_triplets
  680. }
  681. static int scard_get_triplets(int argc, char *argv[])
  682. {
  683. struct scard_data *scard;
  684. size_t len;
  685. char imsi[20];
  686. unsigned char _rand[16];
  687. unsigned char sres[4];
  688. unsigned char kc[8];
  689. int num_triplets;
  690. int i;
  691. size_t j;
  692. if (argc < 2 || ((num_triplets = atoi(argv[1])) <= 0)) {
  693. printf("invalid parameters for sim command\n");
  694. return -1;
  695. }
  696. if (argc <= 2 || os_strcmp(argv[2], "debug") != 0) {
  697. /* disable debug output */
  698. wpa_debug_level = 99;
  699. }
  700. scard = scard_init(SCARD_GSM_SIM_ONLY);
  701. if (scard == NULL) {
  702. printf("Failed to open smartcard connection\n");
  703. return -1;
  704. }
  705. if (scard_set_pin(scard, argv[0])) {
  706. wpa_printf(MSG_WARNING, "PIN validation failed");
  707. scard_deinit(scard);
  708. return -1;
  709. }
  710. len = sizeof(imsi);
  711. if (scard_get_imsi(scard, imsi, &len)) {
  712. scard_deinit(scard);
  713. return -1;
  714. }
  715. for (i = 0; i < num_triplets; i++) {
  716. os_memset(_rand, i, sizeof(_rand));
  717. if (scard_gsm_auth(scard, _rand, sres, kc))
  718. break;
  719. /* IMSI:Kc:SRES:RAND */
  720. for (j = 0; j < len; j++)
  721. printf("%c", imsi[j]);
  722. printf(":");
  723. for (j = 0; j < 8; j++)
  724. printf("%02X", kc[j]);
  725. printf(":");
  726. for (j = 0; j < 4; j++)
  727. printf("%02X", sres[j]);
  728. printf(":");
  729. for (j = 0; j < 16; j++)
  730. printf("%02X", _rand[j]);
  731. printf("\n");
  732. }
  733. scard_deinit(scard);
  734. return 0;
  735. }
  736. static void eapol_test_terminate(int sig, void *eloop_ctx,
  737. void *signal_ctx)
  738. {
  739. struct wpa_supplicant *wpa_s = eloop_ctx;
  740. wpa_msg(wpa_s, MSG_INFO, "Signal %d received - terminating", sig);
  741. eloop_terminate();
  742. }
  743. static void usage(void)
  744. {
  745. printf("usage:\n"
  746. "eapol_test [-nWS] -c<conf> [-a<AS IP>] [-p<AS port>] "
  747. "[-s<AS secret>]\\\n"
  748. " [-r<count>] [-t<timeout>] [-C<Connect-Info>] \\\n"
  749. " [-M<client MAC address>] \\\n"
  750. " [-I<CUI>] [-i] [-A<client IP>]\n"
  751. "eapol_test scard\n"
  752. "eapol_test sim <PIN> <num triplets> [debug]\n"
  753. "\n");
  754. printf("options:\n"
  755. " -c<conf> = configuration file\n"
  756. " -a<AS IP> = IP address of the authentication server, "
  757. "default 127.0.0.1\n"
  758. " -p<AS port> = UDP port of the authentication server, "
  759. "default 1812\n"
  760. " -s<AS secret> = shared secret with the authentication "
  761. "server, default 'radius'\n"
  762. " -A<client IP> = IP address of the client, default: select "
  763. "automatically\n"
  764. " -r<count> = number of re-authentications\n"
  765. " -W = wait for a control interface monitor before starting\n"
  766. " -S = save configuration after authentiation\n"
  767. " -n = no MPPE keys expected\n"
  768. " -t<timeout> = sets timeout in seconds (default: 30 s)\n"
  769. " -C<Connect-Info> = RADIUS Connect-Info (default: "
  770. "CONNECT 11Mbps 802.11b)\n"
  771. " -M<client MAC address> = Set own MAC address "
  772. "(Calling-Station-Id,\n"
  773. " default: 02:00:00:00:00:01)\n"
  774. " -I<CUI> = send Chargeable-User-Identity containing the "
  775. "value of CUI\n"
  776. " -i = send NUL value in Chargeable-User-Identity\n");
  777. }
  778. int main(int argc, char *argv[])
  779. {
  780. struct wpa_supplicant wpa_s;
  781. int c, ret = 1, wait_for_monitor = 0, save_config = 0;
  782. char *as_addr = "127.0.0.1";
  783. int as_port = 1812;
  784. char *as_secret = "radius";
  785. char *cli_addr = NULL;
  786. char *conf = NULL;
  787. int timeout = 30;
  788. if (os_program_init())
  789. return -1;
  790. hostapd_logger_register_cb(hostapd_logger_cb);
  791. os_memset(&eapol_test, 0, sizeof(eapol_test));
  792. eapol_test.connect_info = "CONNECT 11Mbps 802.11b";
  793. os_memcpy(eapol_test.own_addr, "\x02\x00\x00\x00\x00\x01", ETH_ALEN);
  794. wpa_debug_level = 0;
  795. wpa_debug_show_keys = 1;
  796. for (;;) {
  797. c = getopt(argc, argv, "a:A:c:C:iI:M:np:r:s:St:W");
  798. if (c < 0)
  799. break;
  800. switch (c) {
  801. case 'a':
  802. as_addr = optarg;
  803. break;
  804. case 'A':
  805. cli_addr = optarg;
  806. break;
  807. case 'c':
  808. conf = optarg;
  809. break;
  810. case 'C':
  811. eapol_test.connect_info = optarg;
  812. break;
  813. case 'i':
  814. eapol_test.cui_flag = 1;
  815. break;
  816. case 'I':
  817. eapol_test.cui_flag = 2;
  818. eapol_test.cui_str = optarg;
  819. break;
  820. case 'M':
  821. if (hwaddr_aton(optarg, eapol_test.own_addr)) {
  822. usage();
  823. return -1;
  824. }
  825. break;
  826. case 'n':
  827. eapol_test.no_mppe_keys++;
  828. break;
  829. case 'p':
  830. as_port = atoi(optarg);
  831. break;
  832. case 'r':
  833. eapol_test.eapol_test_num_reauths = atoi(optarg);
  834. break;
  835. case 's':
  836. as_secret = optarg;
  837. break;
  838. case 'S':
  839. save_config++;
  840. break;
  841. case 't':
  842. timeout = atoi(optarg);
  843. break;
  844. case 'W':
  845. wait_for_monitor++;
  846. break;
  847. default:
  848. usage();
  849. return -1;
  850. }
  851. }
  852. if (argc > optind && os_strcmp(argv[optind], "scard") == 0) {
  853. return scard_test();
  854. }
  855. if (argc > optind && os_strcmp(argv[optind], "sim") == 0) {
  856. return scard_get_triplets(argc - optind - 1,
  857. &argv[optind + 1]);
  858. }
  859. if (conf == NULL) {
  860. usage();
  861. printf("Configuration file is required.\n");
  862. return -1;
  863. }
  864. if (eap_peer_register_methods()) {
  865. wpa_printf(MSG_ERROR, "Failed to register EAP methods");
  866. return -1;
  867. }
  868. if (eloop_init(&wpa_s)) {
  869. wpa_printf(MSG_ERROR, "Failed to initialize event loop");
  870. return -1;
  871. }
  872. os_memset(&wpa_s, 0, sizeof(wpa_s));
  873. eapol_test.wpa_s = &wpa_s;
  874. wpa_s.conf = wpa_config_read(conf);
  875. if (wpa_s.conf == NULL) {
  876. printf("Failed to parse configuration file '%s'.\n", conf);
  877. return -1;
  878. }
  879. if (wpa_s.conf->ssid == NULL) {
  880. printf("No networks defined.\n");
  881. return -1;
  882. }
  883. wpa_init_conf(&eapol_test, &wpa_s, as_addr, as_port, as_secret,
  884. cli_addr);
  885. wpa_s.ctrl_iface = wpa_supplicant_ctrl_iface_init(&wpa_s);
  886. if (wpa_s.ctrl_iface == NULL) {
  887. printf("Failed to initialize control interface '%s'.\n"
  888. "You may have another eapol_test process already "
  889. "running or the file was\n"
  890. "left by an unclean termination of eapol_test in "
  891. "which case you will need\n"
  892. "to manually remove this file before starting "
  893. "eapol_test again.\n",
  894. wpa_s.conf->ctrl_interface);
  895. return -1;
  896. }
  897. if (wpa_supplicant_scard_init(&wpa_s, wpa_s.conf->ssid))
  898. return -1;
  899. if (test_eapol(&eapol_test, &wpa_s, wpa_s.conf->ssid))
  900. return -1;
  901. if (wait_for_monitor)
  902. wpa_supplicant_ctrl_iface_wait(wpa_s.ctrl_iface);
  903. eloop_register_timeout(timeout, 0, eapol_test_timeout, &eapol_test,
  904. NULL);
  905. eloop_register_timeout(0, 0, send_eap_request_identity, &wpa_s, NULL);
  906. eloop_register_signal_terminate(eapol_test_terminate, NULL);
  907. eloop_register_signal_reconfig(eapol_test_terminate, NULL);
  908. eloop_run();
  909. eloop_cancel_timeout(eapol_test_timeout, &eapol_test, NULL);
  910. eloop_cancel_timeout(eapol_sm_reauth, &eapol_test, NULL);
  911. if (eapol_test_compare_pmk(&eapol_test) == 0 ||
  912. eapol_test.no_mppe_keys)
  913. ret = 0;
  914. if (eapol_test.auth_timed_out)
  915. ret = -2;
  916. if (eapol_test.radius_access_reject_received)
  917. ret = -3;
  918. if (save_config)
  919. wpa_config_write(conf, wpa_s.conf);
  920. test_eapol_clean(&eapol_test, &wpa_s);
  921. eap_peer_unregister_methods();
  922. eloop_destroy();
  923. printf("MPPE keys OK: %d mismatch: %d\n",
  924. eapol_test.num_mppe_ok, eapol_test.num_mppe_mismatch);
  925. if (eapol_test.num_mppe_mismatch)
  926. ret = -4;
  927. if (ret)
  928. printf("FAILURE\n");
  929. else
  930. printf("SUCCESS\n");
  931. os_program_deinit();
  932. return ret;
  933. }