eapol_test.c 37 KB

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