eapol_test.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073
  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. {
  540. struct hostapd_radius_server *as;
  541. int res;
  542. wpa_s->bssid[5] = 1;
  543. os_memcpy(wpa_s->own_addr, e->own_addr, ETH_ALEN);
  544. e->own_ip_addr.s_addr = htonl((127 << 24) | 1);
  545. os_strlcpy(wpa_s->ifname, "test", sizeof(wpa_s->ifname));
  546. e->radius_conf = os_zalloc(sizeof(struct hostapd_radius_servers));
  547. assert(e->radius_conf != NULL);
  548. e->radius_conf->num_auth_servers = 1;
  549. as = os_zalloc(sizeof(struct hostapd_radius_server));
  550. assert(as != NULL);
  551. #if defined(CONFIG_NATIVE_WINDOWS) || defined(CONFIG_ANSI_C_EXTRA)
  552. {
  553. int a[4];
  554. u8 *pos;
  555. sscanf(authsrv, "%d.%d.%d.%d", &a[0], &a[1], &a[2], &a[3]);
  556. pos = (u8 *) &as->addr.u.v4;
  557. *pos++ = a[0];
  558. *pos++ = a[1];
  559. *pos++ = a[2];
  560. *pos++ = a[3];
  561. }
  562. #else /* CONFIG_NATIVE_WINDOWS or CONFIG_ANSI_C_EXTRA */
  563. inet_aton(authsrv, &as->addr.u.v4);
  564. #endif /* CONFIG_NATIVE_WINDOWS or CONFIG_ANSI_C_EXTRA */
  565. as->addr.af = AF_INET;
  566. as->port = port;
  567. as->shared_secret = (u8 *) os_strdup(secret);
  568. as->shared_secret_len = os_strlen(secret);
  569. e->radius_conf->auth_server = as;
  570. e->radius_conf->auth_servers = as;
  571. e->radius_conf->msg_dumps = 1;
  572. e->radius = radius_client_init(wpa_s, e->radius_conf);
  573. assert(e->radius != NULL);
  574. res = radius_client_register(e->radius, RADIUS_AUTH,
  575. ieee802_1x_receive_auth, e);
  576. assert(res == 0);
  577. }
  578. static int scard_test(void)
  579. {
  580. struct scard_data *scard;
  581. size_t len;
  582. char imsi[20];
  583. unsigned char _rand[16];
  584. #ifdef PCSC_FUNCS
  585. unsigned char sres[4];
  586. unsigned char kc[8];
  587. #endif /* PCSC_FUNCS */
  588. #define num_triplets 5
  589. unsigned char rand_[num_triplets][16];
  590. unsigned char sres_[num_triplets][4];
  591. unsigned char kc_[num_triplets][8];
  592. int i, res;
  593. size_t j;
  594. #define AKA_RAND_LEN 16
  595. #define AKA_AUTN_LEN 16
  596. #define AKA_AUTS_LEN 14
  597. #define RES_MAX_LEN 16
  598. #define IK_LEN 16
  599. #define CK_LEN 16
  600. unsigned char aka_rand[AKA_RAND_LEN];
  601. unsigned char aka_autn[AKA_AUTN_LEN];
  602. unsigned char aka_auts[AKA_AUTS_LEN];
  603. unsigned char aka_res[RES_MAX_LEN];
  604. size_t aka_res_len;
  605. unsigned char aka_ik[IK_LEN];
  606. unsigned char aka_ck[CK_LEN];
  607. scard = scard_init(SCARD_TRY_BOTH);
  608. if (scard == NULL)
  609. return -1;
  610. if (scard_set_pin(scard, "1234")) {
  611. wpa_printf(MSG_WARNING, "PIN validation failed");
  612. scard_deinit(scard);
  613. return -1;
  614. }
  615. len = sizeof(imsi);
  616. if (scard_get_imsi(scard, imsi, &len))
  617. goto failed;
  618. wpa_hexdump_ascii(MSG_DEBUG, "SCARD: IMSI", (u8 *) imsi, len);
  619. /* NOTE: Permanent Username: 1 | IMSI */
  620. os_memset(_rand, 0, sizeof(_rand));
  621. if (scard_gsm_auth(scard, _rand, sres, kc))
  622. goto failed;
  623. os_memset(_rand, 0xff, sizeof(_rand));
  624. if (scard_gsm_auth(scard, _rand, sres, kc))
  625. goto failed;
  626. for (i = 0; i < num_triplets; i++) {
  627. os_memset(rand_[i], i, sizeof(rand_[i]));
  628. if (scard_gsm_auth(scard, rand_[i], sres_[i], kc_[i]))
  629. goto failed;
  630. }
  631. for (i = 0; i < num_triplets; i++) {
  632. printf("1");
  633. for (j = 0; j < len; j++)
  634. printf("%c", imsi[j]);
  635. printf(",");
  636. for (j = 0; j < 16; j++)
  637. printf("%02X", rand_[i][j]);
  638. printf(",");
  639. for (j = 0; j < 4; j++)
  640. printf("%02X", sres_[i][j]);
  641. printf(",");
  642. for (j = 0; j < 8; j++)
  643. printf("%02X", kc_[i][j]);
  644. printf("\n");
  645. }
  646. wpa_printf(MSG_DEBUG, "Trying to use UMTS authentication");
  647. /* seq 39 (0x28) */
  648. os_memset(aka_rand, 0xaa, 16);
  649. os_memcpy(aka_autn, "\x86\x71\x31\xcb\xa2\xfc\x61\xdf"
  650. "\xa3\xb3\x97\x9d\x07\x32\xa2\x12", 16);
  651. res = scard_umts_auth(scard, aka_rand, aka_autn, aka_res, &aka_res_len,
  652. aka_ik, aka_ck, aka_auts);
  653. if (res == 0) {
  654. wpa_printf(MSG_DEBUG, "UMTS auth completed successfully");
  655. wpa_hexdump(MSG_DEBUG, "RES", aka_res, aka_res_len);
  656. wpa_hexdump(MSG_DEBUG, "IK", aka_ik, IK_LEN);
  657. wpa_hexdump(MSG_DEBUG, "CK", aka_ck, CK_LEN);
  658. } else if (res == -2) {
  659. wpa_printf(MSG_DEBUG, "UMTS auth resulted in synchronization "
  660. "failure");
  661. wpa_hexdump(MSG_DEBUG, "AUTS", aka_auts, AKA_AUTS_LEN);
  662. } else {
  663. wpa_printf(MSG_DEBUG, "UMTS auth failed");
  664. }
  665. failed:
  666. scard_deinit(scard);
  667. return 0;
  668. #undef num_triplets
  669. }
  670. static int scard_get_triplets(int argc, char *argv[])
  671. {
  672. struct scard_data *scard;
  673. size_t len;
  674. char imsi[20];
  675. unsigned char _rand[16];
  676. unsigned char sres[4];
  677. unsigned char kc[8];
  678. int num_triplets;
  679. int i;
  680. size_t j;
  681. if (argc < 2 || ((num_triplets = atoi(argv[1])) <= 0)) {
  682. printf("invalid parameters for sim command\n");
  683. return -1;
  684. }
  685. if (argc <= 2 || os_strcmp(argv[2], "debug") != 0) {
  686. /* disable debug output */
  687. wpa_debug_level = 99;
  688. }
  689. scard = scard_init(SCARD_GSM_SIM_ONLY);
  690. if (scard == NULL) {
  691. printf("Failed to open smartcard connection\n");
  692. return -1;
  693. }
  694. if (scard_set_pin(scard, argv[0])) {
  695. wpa_printf(MSG_WARNING, "PIN validation failed");
  696. scard_deinit(scard);
  697. return -1;
  698. }
  699. len = sizeof(imsi);
  700. if (scard_get_imsi(scard, imsi, &len)) {
  701. scard_deinit(scard);
  702. return -1;
  703. }
  704. for (i = 0; i < num_triplets; i++) {
  705. os_memset(_rand, i, sizeof(_rand));
  706. if (scard_gsm_auth(scard, _rand, sres, kc))
  707. break;
  708. /* IMSI:Kc:SRES:RAND */
  709. for (j = 0; j < len; j++)
  710. printf("%c", imsi[j]);
  711. printf(":");
  712. for (j = 0; j < 8; j++)
  713. printf("%02X", kc[j]);
  714. printf(":");
  715. for (j = 0; j < 4; j++)
  716. printf("%02X", sres[j]);
  717. printf(":");
  718. for (j = 0; j < 16; j++)
  719. printf("%02X", _rand[j]);
  720. printf("\n");
  721. }
  722. scard_deinit(scard);
  723. return 0;
  724. }
  725. static void eapol_test_terminate(int sig, void *eloop_ctx,
  726. void *signal_ctx)
  727. {
  728. struct wpa_supplicant *wpa_s = eloop_ctx;
  729. wpa_msg(wpa_s, MSG_INFO, "Signal %d received - terminating", sig);
  730. eloop_terminate();
  731. }
  732. static void usage(void)
  733. {
  734. printf("usage:\n"
  735. "eapol_test [-nWS] -c<conf> [-a<AS IP>] [-p<AS port>] "
  736. "[-s<AS secret>] \\\n"
  737. " [-r<count>] [-t<timeout>] [-C<Connect-Info>] \\\n"
  738. " [-M<client MAC address>] \\\n"
  739. " [-I<CUI>] [-i]\n"
  740. "eapol_test scard\n"
  741. "eapol_test sim <PIN> <num triplets> [debug]\n"
  742. "\n");
  743. printf("options:\n"
  744. " -c<conf> = configuration file\n"
  745. " -a<AS IP> = IP address of the authentication server, "
  746. "default 127.0.0.1\n"
  747. " -p<AS port> = UDP port of the authentication server, "
  748. "default 1812\n"
  749. " -s<AS secret> = shared secret with the authentication "
  750. "server, default 'radius'\n"
  751. " -r<count> = number of re-authentications\n"
  752. " -W = wait for a control interface monitor before starting\n"
  753. " -S = save configuration after authentiation\n"
  754. " -n = no MPPE keys expected\n"
  755. " -t<timeout> = sets timeout in seconds (default: 30 s)\n"
  756. " -C<Connect-Info> = RADIUS Connect-Info (default: "
  757. "CONNECT 11Mbps 802.11b)\n"
  758. " -M<client MAC address> = Set own MAC address "
  759. "(Calling-Station-Id,\n"
  760. " default: 02:00:00:00:00:01)\n"
  761. " -I<CUI> = send Chargeable-User-Identity containing the "
  762. "value of CUI\n"
  763. " -i = send NUL value in Chargeable-User-Identity\n");
  764. }
  765. int main(int argc, char *argv[])
  766. {
  767. struct wpa_supplicant wpa_s;
  768. int c, ret = 1, wait_for_monitor = 0, save_config = 0;
  769. char *as_addr = "127.0.0.1";
  770. int as_port = 1812;
  771. char *as_secret = "radius";
  772. char *conf = NULL;
  773. int timeout = 30;
  774. if (os_program_init())
  775. return -1;
  776. hostapd_logger_register_cb(hostapd_logger_cb);
  777. os_memset(&eapol_test, 0, sizeof(eapol_test));
  778. eapol_test.connect_info = "CONNECT 11Mbps 802.11b";
  779. os_memcpy(eapol_test.own_addr, "\x02\x00\x00\x00\x00\x01", ETH_ALEN);
  780. wpa_debug_level = 0;
  781. wpa_debug_show_keys = 1;
  782. for (;;) {
  783. c = getopt(argc, argv, "a:c:C:iI:M:np:r:s:St:W");
  784. if (c < 0)
  785. break;
  786. switch (c) {
  787. case 'a':
  788. as_addr = optarg;
  789. break;
  790. case 'c':
  791. conf = optarg;
  792. break;
  793. case 'C':
  794. eapol_test.connect_info = optarg;
  795. break;
  796. case 'i':
  797. eapol_test.cui_flag = 1;
  798. break;
  799. case 'I':
  800. eapol_test.cui_flag = 2;
  801. eapol_test.cui_str = optarg;
  802. break;
  803. case 'M':
  804. if (hwaddr_aton(optarg, eapol_test.own_addr)) {
  805. usage();
  806. return -1;
  807. }
  808. break;
  809. case 'n':
  810. eapol_test.no_mppe_keys++;
  811. break;
  812. case 'p':
  813. as_port = atoi(optarg);
  814. break;
  815. case 'r':
  816. eapol_test.eapol_test_num_reauths = atoi(optarg);
  817. break;
  818. case 's':
  819. as_secret = optarg;
  820. break;
  821. case 'S':
  822. save_config++;
  823. break;
  824. case 't':
  825. timeout = atoi(optarg);
  826. break;
  827. case 'W':
  828. wait_for_monitor++;
  829. break;
  830. default:
  831. usage();
  832. return -1;
  833. }
  834. }
  835. if (argc > optind && os_strcmp(argv[optind], "scard") == 0) {
  836. return scard_test();
  837. }
  838. if (argc > optind && os_strcmp(argv[optind], "sim") == 0) {
  839. return scard_get_triplets(argc - optind - 1,
  840. &argv[optind + 1]);
  841. }
  842. if (conf == NULL) {
  843. usage();
  844. printf("Configuration file is required.\n");
  845. return -1;
  846. }
  847. if (eap_peer_register_methods()) {
  848. wpa_printf(MSG_ERROR, "Failed to register EAP methods");
  849. return -1;
  850. }
  851. if (eloop_init(&wpa_s)) {
  852. wpa_printf(MSG_ERROR, "Failed to initialize event loop");
  853. return -1;
  854. }
  855. os_memset(&wpa_s, 0, sizeof(wpa_s));
  856. eapol_test.wpa_s = &wpa_s;
  857. wpa_s.conf = wpa_config_read(conf);
  858. if (wpa_s.conf == NULL) {
  859. printf("Failed to parse configuration file '%s'.\n", conf);
  860. return -1;
  861. }
  862. if (wpa_s.conf->ssid == NULL) {
  863. printf("No networks defined.\n");
  864. return -1;
  865. }
  866. wpa_init_conf(&eapol_test, &wpa_s, as_addr, as_port, as_secret);
  867. wpa_s.ctrl_iface = wpa_supplicant_ctrl_iface_init(&wpa_s);
  868. if (wpa_s.ctrl_iface == NULL) {
  869. printf("Failed to initialize control interface '%s'.\n"
  870. "You may have another eapol_test process already "
  871. "running or the file was\n"
  872. "left by an unclean termination of eapol_test in "
  873. "which case you will need\n"
  874. "to manually remove this file before starting "
  875. "eapol_test again.\n",
  876. wpa_s.conf->ctrl_interface);
  877. return -1;
  878. }
  879. if (wpa_supplicant_scard_init(&wpa_s, wpa_s.conf->ssid))
  880. return -1;
  881. if (test_eapol(&eapol_test, &wpa_s, wpa_s.conf->ssid))
  882. return -1;
  883. if (wait_for_monitor)
  884. wpa_supplicant_ctrl_iface_wait(wpa_s.ctrl_iface);
  885. eloop_register_timeout(timeout, 0, eapol_test_timeout, &eapol_test,
  886. NULL);
  887. eloop_register_timeout(0, 0, send_eap_request_identity, &wpa_s, NULL);
  888. eloop_register_signal_terminate(eapol_test_terminate, NULL);
  889. eloop_register_signal_reconfig(eapol_test_terminate, NULL);
  890. eloop_run();
  891. eloop_cancel_timeout(eapol_test_timeout, &eapol_test, NULL);
  892. eloop_cancel_timeout(eapol_sm_reauth, &eapol_test, NULL);
  893. if (eapol_test_compare_pmk(&eapol_test) == 0 ||
  894. eapol_test.no_mppe_keys)
  895. ret = 0;
  896. if (eapol_test.auth_timed_out)
  897. ret = -2;
  898. if (eapol_test.radius_access_reject_received)
  899. ret = -3;
  900. if (save_config)
  901. wpa_config_write(conf, wpa_s.conf);
  902. test_eapol_clean(&eapol_test, &wpa_s);
  903. eap_peer_unregister_methods();
  904. eloop_destroy();
  905. printf("MPPE keys OK: %d mismatch: %d\n",
  906. eapol_test.num_mppe_ok, eapol_test.num_mppe_mismatch);
  907. if (eapol_test.num_mppe_mismatch)
  908. ret = -4;
  909. if (ret)
  910. printf("FAILURE\n");
  911. else
  912. printf("SUCCESS\n");
  913. os_program_deinit();
  914. return ret;
  915. }