eapol_test.c 35 KB

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