eapol_test.c 36 KB

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