eapol_test.c 27 KB

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