eapol_test.c 30 KB

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