eapol_test.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199
  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. const u8 *shared_secret,
  509. size_t shared_secret_len)
  510. {
  511. struct radius_ms_mppe_keys *keys;
  512. keys = radius_msg_get_ms_keys(msg, req, shared_secret,
  513. shared_secret_len);
  514. if (keys && keys->send == NULL && keys->recv == NULL) {
  515. os_free(keys);
  516. keys = radius_msg_get_cisco_keys(msg, req, shared_secret,
  517. shared_secret_len);
  518. }
  519. if (keys) {
  520. if (keys->send) {
  521. wpa_hexdump(MSG_DEBUG, "MS-MPPE-Send-Key (sign)",
  522. keys->send, keys->send_len);
  523. }
  524. if (keys->recv) {
  525. wpa_hexdump(MSG_DEBUG, "MS-MPPE-Recv-Key (crypt)",
  526. keys->recv, keys->recv_len);
  527. e->authenticator_pmk_len =
  528. keys->recv_len > PMK_LEN ? PMK_LEN :
  529. keys->recv_len;
  530. os_memcpy(e->authenticator_pmk, keys->recv,
  531. e->authenticator_pmk_len);
  532. if (e->authenticator_pmk_len == 16 && keys->send &&
  533. keys->send_len == 16) {
  534. /* MS-CHAP-v2 derives 16 octet keys */
  535. wpa_printf(MSG_DEBUG, "Use MS-MPPE-Send-Key "
  536. "to extend PMK to 32 octets");
  537. os_memcpy(e->authenticator_pmk +
  538. e->authenticator_pmk_len,
  539. keys->send, keys->send_len);
  540. e->authenticator_pmk_len += keys->send_len;
  541. }
  542. }
  543. os_free(keys->send);
  544. os_free(keys->recv);
  545. os_free(keys);
  546. }
  547. }
  548. /* Process the RADIUS frames from Authentication Server */
  549. static RadiusRxResult
  550. ieee802_1x_receive_auth(struct radius_msg *msg, struct radius_msg *req,
  551. const u8 *shared_secret, size_t shared_secret_len,
  552. void *data)
  553. {
  554. struct eapol_test_data *e = data;
  555. /* RFC 2869, Ch. 5.13: valid Message-Authenticator attribute MUST be
  556. * present when packet contains an EAP-Message attribute */
  557. if (msg->hdr->code == RADIUS_CODE_ACCESS_REJECT &&
  558. radius_msg_get_attr(msg, RADIUS_ATTR_MESSAGE_AUTHENTICATOR, NULL,
  559. 0) < 0 &&
  560. radius_msg_get_attr(msg, RADIUS_ATTR_EAP_MESSAGE, NULL, 0) < 0) {
  561. wpa_printf(MSG_DEBUG, "Allowing RADIUS "
  562. "Access-Reject without Message-Authenticator "
  563. "since it does not include EAP-Message\n");
  564. } else if (radius_msg_verify(msg, shared_secret, shared_secret_len,
  565. req, 1)) {
  566. printf("Incoming RADIUS packet did not have correct "
  567. "Message-Authenticator - dropped\n");
  568. return RADIUS_RX_UNKNOWN;
  569. }
  570. if (msg->hdr->code != RADIUS_CODE_ACCESS_ACCEPT &&
  571. msg->hdr->code != RADIUS_CODE_ACCESS_REJECT &&
  572. msg->hdr->code != RADIUS_CODE_ACCESS_CHALLENGE) {
  573. printf("Unknown RADIUS message code\n");
  574. return RADIUS_RX_UNKNOWN;
  575. }
  576. e->radius_identifier = -1;
  577. wpa_printf(MSG_DEBUG, "RADIUS packet matching with station");
  578. if (e->last_recv_radius) {
  579. radius_msg_free(e->last_recv_radius);
  580. os_free(e->last_recv_radius);
  581. }
  582. e->last_recv_radius = msg;
  583. switch (msg->hdr->code) {
  584. case RADIUS_CODE_ACCESS_ACCEPT:
  585. e->radius_access_accept_received = 1;
  586. ieee802_1x_get_keys(e, msg, req, shared_secret,
  587. shared_secret_len);
  588. break;
  589. case RADIUS_CODE_ACCESS_REJECT:
  590. e->radius_access_reject_received = 1;
  591. break;
  592. }
  593. ieee802_1x_decapsulate_radius(e);
  594. if ((msg->hdr->code == RADIUS_CODE_ACCESS_ACCEPT &&
  595. e->eapol_test_num_reauths < 0) ||
  596. msg->hdr->code == RADIUS_CODE_ACCESS_REJECT) {
  597. eloop_terminate();
  598. }
  599. return RADIUS_RX_QUEUED;
  600. }
  601. static void wpa_init_conf(struct eapol_test_data *e,
  602. struct wpa_supplicant *wpa_s, const char *authsrv,
  603. int port, const char *secret,
  604. const char *cli_addr)
  605. {
  606. struct hostapd_radius_server *as;
  607. int res;
  608. wpa_s->bssid[5] = 1;
  609. os_memcpy(wpa_s->own_addr, e->own_addr, ETH_ALEN);
  610. e->own_ip_addr.s_addr = htonl((127 << 24) | 1);
  611. os_strlcpy(wpa_s->ifname, "test", sizeof(wpa_s->ifname));
  612. e->radius_conf = os_zalloc(sizeof(struct hostapd_radius_servers));
  613. assert(e->radius_conf != NULL);
  614. e->radius_conf->num_auth_servers = 1;
  615. as = os_zalloc(sizeof(struct hostapd_radius_server));
  616. assert(as != NULL);
  617. #if defined(CONFIG_NATIVE_WINDOWS) || defined(CONFIG_ANSI_C_EXTRA)
  618. {
  619. int a[4];
  620. u8 *pos;
  621. sscanf(authsrv, "%d.%d.%d.%d", &a[0], &a[1], &a[2], &a[3]);
  622. pos = (u8 *) &as->addr.u.v4;
  623. *pos++ = a[0];
  624. *pos++ = a[1];
  625. *pos++ = a[2];
  626. *pos++ = a[3];
  627. }
  628. #else /* CONFIG_NATIVE_WINDOWS or CONFIG_ANSI_C_EXTRA */
  629. inet_aton(authsrv, &as->addr.u.v4);
  630. #endif /* CONFIG_NATIVE_WINDOWS or CONFIG_ANSI_C_EXTRA */
  631. as->addr.af = AF_INET;
  632. as->port = port;
  633. as->shared_secret = (u8 *) os_strdup(secret);
  634. as->shared_secret_len = os_strlen(secret);
  635. e->radius_conf->auth_server = as;
  636. e->radius_conf->auth_servers = as;
  637. e->radius_conf->msg_dumps = 1;
  638. if (cli_addr) {
  639. if (hostapd_parse_ip_addr(cli_addr,
  640. &e->radius_conf->client_addr) == 0)
  641. e->radius_conf->force_client_addr = 1;
  642. else {
  643. wpa_printf(MSG_ERROR, "Invalid IP address '%s'",
  644. cli_addr);
  645. assert(0);
  646. }
  647. }
  648. e->radius = radius_client_init(wpa_s, e->radius_conf);
  649. assert(e->radius != NULL);
  650. res = radius_client_register(e->radius, RADIUS_AUTH,
  651. ieee802_1x_receive_auth, e);
  652. assert(res == 0);
  653. }
  654. static int scard_test(void)
  655. {
  656. struct scard_data *scard;
  657. size_t len;
  658. char imsi[20];
  659. unsigned char _rand[16];
  660. #ifdef PCSC_FUNCS
  661. unsigned char sres[4];
  662. unsigned char kc[8];
  663. #endif /* PCSC_FUNCS */
  664. #define num_triplets 5
  665. unsigned char rand_[num_triplets][16];
  666. unsigned char sres_[num_triplets][4];
  667. unsigned char kc_[num_triplets][8];
  668. int i, res;
  669. size_t j;
  670. #define AKA_RAND_LEN 16
  671. #define AKA_AUTN_LEN 16
  672. #define AKA_AUTS_LEN 14
  673. #define RES_MAX_LEN 16
  674. #define IK_LEN 16
  675. #define CK_LEN 16
  676. unsigned char aka_rand[AKA_RAND_LEN];
  677. unsigned char aka_autn[AKA_AUTN_LEN];
  678. unsigned char aka_auts[AKA_AUTS_LEN];
  679. unsigned char aka_res[RES_MAX_LEN];
  680. size_t aka_res_len;
  681. unsigned char aka_ik[IK_LEN];
  682. unsigned char aka_ck[CK_LEN];
  683. scard = scard_init(SCARD_TRY_BOTH);
  684. if (scard == NULL)
  685. return -1;
  686. if (scard_set_pin(scard, "1234")) {
  687. wpa_printf(MSG_WARNING, "PIN validation failed");
  688. scard_deinit(scard);
  689. return -1;
  690. }
  691. len = sizeof(imsi);
  692. if (scard_get_imsi(scard, imsi, &len))
  693. goto failed;
  694. wpa_hexdump_ascii(MSG_DEBUG, "SCARD: IMSI", (u8 *) imsi, len);
  695. /* NOTE: Permanent Username: 1 | IMSI */
  696. os_memset(_rand, 0, sizeof(_rand));
  697. if (scard_gsm_auth(scard, _rand, sres, kc))
  698. goto failed;
  699. os_memset(_rand, 0xff, sizeof(_rand));
  700. if (scard_gsm_auth(scard, _rand, sres, kc))
  701. goto failed;
  702. for (i = 0; i < num_triplets; i++) {
  703. os_memset(rand_[i], i, sizeof(rand_[i]));
  704. if (scard_gsm_auth(scard, rand_[i], sres_[i], kc_[i]))
  705. goto failed;
  706. }
  707. for (i = 0; i < num_triplets; i++) {
  708. printf("1");
  709. for (j = 0; j < len; j++)
  710. printf("%c", imsi[j]);
  711. printf(",");
  712. for (j = 0; j < 16; j++)
  713. printf("%02X", rand_[i][j]);
  714. printf(",");
  715. for (j = 0; j < 4; j++)
  716. printf("%02X", sres_[i][j]);
  717. printf(",");
  718. for (j = 0; j < 8; j++)
  719. printf("%02X", kc_[i][j]);
  720. printf("\n");
  721. }
  722. wpa_printf(MSG_DEBUG, "Trying to use UMTS authentication");
  723. /* seq 39 (0x28) */
  724. os_memset(aka_rand, 0xaa, 16);
  725. os_memcpy(aka_autn, "\x86\x71\x31\xcb\xa2\xfc\x61\xdf"
  726. "\xa3\xb3\x97\x9d\x07\x32\xa2\x12", 16);
  727. res = scard_umts_auth(scard, aka_rand, aka_autn, aka_res, &aka_res_len,
  728. aka_ik, aka_ck, aka_auts);
  729. if (res == 0) {
  730. wpa_printf(MSG_DEBUG, "UMTS auth completed successfully");
  731. wpa_hexdump(MSG_DEBUG, "RES", aka_res, aka_res_len);
  732. wpa_hexdump(MSG_DEBUG, "IK", aka_ik, IK_LEN);
  733. wpa_hexdump(MSG_DEBUG, "CK", aka_ck, CK_LEN);
  734. } else if (res == -2) {
  735. wpa_printf(MSG_DEBUG, "UMTS auth resulted in synchronization "
  736. "failure");
  737. wpa_hexdump(MSG_DEBUG, "AUTS", aka_auts, AKA_AUTS_LEN);
  738. } else {
  739. wpa_printf(MSG_DEBUG, "UMTS auth failed");
  740. }
  741. failed:
  742. scard_deinit(scard);
  743. return 0;
  744. #undef num_triplets
  745. }
  746. static int scard_get_triplets(int argc, char *argv[])
  747. {
  748. struct scard_data *scard;
  749. size_t len;
  750. char imsi[20];
  751. unsigned char _rand[16];
  752. unsigned char sres[4];
  753. unsigned char kc[8];
  754. int num_triplets;
  755. int i;
  756. size_t j;
  757. if (argc < 2 || ((num_triplets = atoi(argv[1])) <= 0)) {
  758. printf("invalid parameters for sim command\n");
  759. return -1;
  760. }
  761. if (argc <= 2 || os_strcmp(argv[2], "debug") != 0) {
  762. /* disable debug output */
  763. wpa_debug_level = 99;
  764. }
  765. scard = scard_init(SCARD_GSM_SIM_ONLY);
  766. if (scard == NULL) {
  767. printf("Failed to open smartcard connection\n");
  768. return -1;
  769. }
  770. if (scard_set_pin(scard, argv[0])) {
  771. wpa_printf(MSG_WARNING, "PIN validation failed");
  772. scard_deinit(scard);
  773. return -1;
  774. }
  775. len = sizeof(imsi);
  776. if (scard_get_imsi(scard, imsi, &len)) {
  777. scard_deinit(scard);
  778. return -1;
  779. }
  780. for (i = 0; i < num_triplets; i++) {
  781. os_memset(_rand, i, sizeof(_rand));
  782. if (scard_gsm_auth(scard, _rand, sres, kc))
  783. break;
  784. /* IMSI:Kc:SRES:RAND */
  785. for (j = 0; j < len; j++)
  786. printf("%c", imsi[j]);
  787. printf(":");
  788. for (j = 0; j < 8; j++)
  789. printf("%02X", kc[j]);
  790. printf(":");
  791. for (j = 0; j < 4; j++)
  792. printf("%02X", sres[j]);
  793. printf(":");
  794. for (j = 0; j < 16; j++)
  795. printf("%02X", _rand[j]);
  796. printf("\n");
  797. }
  798. scard_deinit(scard);
  799. return 0;
  800. }
  801. static void eapol_test_terminate(int sig, void *eloop_ctx,
  802. void *signal_ctx)
  803. {
  804. struct wpa_supplicant *wpa_s = eloop_ctx;
  805. wpa_msg(wpa_s, MSG_INFO, "Signal %d received - terminating", sig);
  806. eloop_terminate();
  807. }
  808. static void usage(void)
  809. {
  810. printf("usage:\n"
  811. "eapol_test [-nWS] -c<conf> [-a<AS IP>] [-p<AS port>] "
  812. "[-s<AS secret>]\\\n"
  813. " [-r<count>] [-t<timeout>] [-C<Connect-Info>] \\\n"
  814. " [-M<client MAC address>] \\\n"
  815. " [-N<attr spec>] \\\n"
  816. " [-A<client IP>]\n"
  817. "eapol_test scard\n"
  818. "eapol_test sim <PIN> <num triplets> [debug]\n"
  819. "\n");
  820. printf("options:\n"
  821. " -c<conf> = configuration file\n"
  822. " -a<AS IP> = IP address of the authentication server, "
  823. "default 127.0.0.1\n"
  824. " -p<AS port> = UDP port of the authentication server, "
  825. "default 1812\n"
  826. " -s<AS secret> = shared secret with the authentication "
  827. "server, default 'radius'\n"
  828. " -A<client IP> = IP address of the client, default: select "
  829. "automatically\n"
  830. " -r<count> = number of re-authentications\n"
  831. " -W = wait for a control interface monitor before starting\n"
  832. " -S = save configuration after authentication\n"
  833. " -n = no MPPE keys expected\n"
  834. " -t<timeout> = sets timeout in seconds (default: 30 s)\n"
  835. " -C<Connect-Info> = RADIUS Connect-Info (default: "
  836. "CONNECT 11Mbps 802.11b)\n"
  837. " -M<client MAC address> = Set own MAC address "
  838. "(Calling-Station-Id,\n"
  839. " default: 02:00:00:00:00:01)\n"
  840. " -N<attr spec> = send arbitrary attribute specified by:\n"
  841. " attr_id:syntax:value or attr_id\n"
  842. " attr_id - number id of the attribute\n"
  843. " syntax - one of: s, d, x\n"
  844. " s = string\n"
  845. " d = integer\n"
  846. " x = octet string\n"
  847. " value - attribute value.\n"
  848. " When only attr_id is specified, NULL will be used as "
  849. "value.\n"
  850. " Multiple attributes can be specified by using the "
  851. "option several times.\n");
  852. }
  853. int main(int argc, char *argv[])
  854. {
  855. struct wpa_supplicant wpa_s;
  856. int c, ret = 1, wait_for_monitor = 0, save_config = 0;
  857. char *as_addr = "127.0.0.1";
  858. int as_port = 1812;
  859. char *as_secret = "radius";
  860. char *cli_addr = NULL;
  861. char *conf = NULL;
  862. int timeout = 30;
  863. char *pos;
  864. struct extra_radius_attr *p = NULL, *p1;
  865. if (os_program_init())
  866. return -1;
  867. hostapd_logger_register_cb(hostapd_logger_cb);
  868. os_memset(&eapol_test, 0, sizeof(eapol_test));
  869. eapol_test.connect_info = "CONNECT 11Mbps 802.11b";
  870. os_memcpy(eapol_test.own_addr, "\x02\x00\x00\x00\x00\x01", ETH_ALEN);
  871. wpa_debug_level = 0;
  872. wpa_debug_show_keys = 1;
  873. for (;;) {
  874. c = getopt(argc, argv, "a:A:c:C:M:nN:p:r:s:St:W");
  875. if (c < 0)
  876. break;
  877. switch (c) {
  878. case 'a':
  879. as_addr = optarg;
  880. break;
  881. case 'A':
  882. cli_addr = optarg;
  883. break;
  884. case 'c':
  885. conf = optarg;
  886. break;
  887. case 'C':
  888. eapol_test.connect_info = optarg;
  889. break;
  890. case 'M':
  891. if (hwaddr_aton(optarg, eapol_test.own_addr)) {
  892. usage();
  893. return -1;
  894. }
  895. break;
  896. case 'n':
  897. eapol_test.no_mppe_keys++;
  898. break;
  899. case 'p':
  900. as_port = atoi(optarg);
  901. break;
  902. case 'r':
  903. eapol_test.eapol_test_num_reauths = atoi(optarg);
  904. break;
  905. case 's':
  906. as_secret = optarg;
  907. break;
  908. case 'S':
  909. save_config++;
  910. break;
  911. case 't':
  912. timeout = atoi(optarg);
  913. break;
  914. case 'W':
  915. wait_for_monitor++;
  916. break;
  917. case 'N':
  918. p1 = os_zalloc(sizeof(p1));
  919. if (p1 == NULL)
  920. break;
  921. if (!p)
  922. eapol_test.extra_attrs = p1;
  923. else
  924. p->next = p1;
  925. p = p1;
  926. p->type = atoi(optarg);
  927. pos = os_strchr(optarg, ':');
  928. if (pos == NULL) {
  929. p->syntax = 'n';
  930. p->data = NULL;
  931. break;
  932. }
  933. pos++;
  934. if (pos[0] == '\0' || pos[1] != ':') {
  935. printf("Incorrect format of attribute "
  936. "specification\n");
  937. break;
  938. }
  939. p->syntax = pos[0];
  940. p->data = pos + 2;
  941. break;
  942. default:
  943. usage();
  944. return -1;
  945. }
  946. }
  947. if (argc > optind && os_strcmp(argv[optind], "scard") == 0) {
  948. return scard_test();
  949. }
  950. if (argc > optind && os_strcmp(argv[optind], "sim") == 0) {
  951. return scard_get_triplets(argc - optind - 1,
  952. &argv[optind + 1]);
  953. }
  954. if (conf == NULL) {
  955. usage();
  956. printf("Configuration file is required.\n");
  957. return -1;
  958. }
  959. if (eap_peer_register_methods()) {
  960. wpa_printf(MSG_ERROR, "Failed to register EAP methods");
  961. return -1;
  962. }
  963. if (eloop_init(&wpa_s)) {
  964. wpa_printf(MSG_ERROR, "Failed to initialize event loop");
  965. return -1;
  966. }
  967. os_memset(&wpa_s, 0, sizeof(wpa_s));
  968. eapol_test.wpa_s = &wpa_s;
  969. wpa_s.conf = wpa_config_read(conf);
  970. if (wpa_s.conf == NULL) {
  971. printf("Failed to parse configuration file '%s'.\n", conf);
  972. return -1;
  973. }
  974. if (wpa_s.conf->ssid == NULL) {
  975. printf("No networks defined.\n");
  976. return -1;
  977. }
  978. wpa_init_conf(&eapol_test, &wpa_s, as_addr, as_port, as_secret,
  979. cli_addr);
  980. wpa_s.ctrl_iface = wpa_supplicant_ctrl_iface_init(&wpa_s);
  981. if (wpa_s.ctrl_iface == NULL) {
  982. printf("Failed to initialize control interface '%s'.\n"
  983. "You may have another eapol_test process already "
  984. "running or the file was\n"
  985. "left by an unclean termination of eapol_test in "
  986. "which case you will need\n"
  987. "to manually remove this file before starting "
  988. "eapol_test again.\n",
  989. wpa_s.conf->ctrl_interface);
  990. return -1;
  991. }
  992. if (wpa_supplicant_scard_init(&wpa_s, wpa_s.conf->ssid))
  993. return -1;
  994. if (test_eapol(&eapol_test, &wpa_s, wpa_s.conf->ssid))
  995. return -1;
  996. if (wait_for_monitor)
  997. wpa_supplicant_ctrl_iface_wait(wpa_s.ctrl_iface);
  998. eloop_register_timeout(timeout, 0, eapol_test_timeout, &eapol_test,
  999. NULL);
  1000. eloop_register_timeout(0, 0, send_eap_request_identity, &wpa_s, NULL);
  1001. eloop_register_signal_terminate(eapol_test_terminate, NULL);
  1002. eloop_register_signal_reconfig(eapol_test_terminate, NULL);
  1003. eloop_run();
  1004. eloop_cancel_timeout(eapol_test_timeout, &eapol_test, NULL);
  1005. eloop_cancel_timeout(eapol_sm_reauth, &eapol_test, NULL);
  1006. if (eapol_test_compare_pmk(&eapol_test) == 0 ||
  1007. eapol_test.no_mppe_keys)
  1008. ret = 0;
  1009. if (eapol_test.auth_timed_out)
  1010. ret = -2;
  1011. if (eapol_test.radius_access_reject_received)
  1012. ret = -3;
  1013. if (save_config)
  1014. wpa_config_write(conf, wpa_s.conf);
  1015. test_eapol_clean(&eapol_test, &wpa_s);
  1016. eap_peer_unregister_methods();
  1017. eloop_destroy();
  1018. printf("MPPE keys OK: %d mismatch: %d\n",
  1019. eapol_test.num_mppe_ok, eapol_test.num_mppe_mismatch);
  1020. if (eapol_test.num_mppe_mismatch)
  1021. ret = -4;
  1022. if (ret)
  1023. printf("FAILURE\n");
  1024. else
  1025. printf("SUCCESS\n");
  1026. os_program_deinit();
  1027. return ret;
  1028. }