eapol_test.c 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448
  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 *altsubject[], int num_altsubject,
  408. const char *cert_hash,
  409. const struct wpabuf *cert)
  410. {
  411. struct eapol_test_data *e = ctx;
  412. wpa_msg(e->wpa_s, MSG_INFO, WPA_EVENT_EAP_PEER_CERT
  413. "depth=%d subject='%s'%s%s",
  414. depth, subject,
  415. cert_hash ? " hash=" : "",
  416. cert_hash ? cert_hash : "");
  417. if (cert) {
  418. char *cert_hex;
  419. size_t len = wpabuf_len(cert) * 2 + 1;
  420. cert_hex = os_malloc(len);
  421. if (cert_hex) {
  422. wpa_snprintf_hex(cert_hex, len, wpabuf_head(cert),
  423. wpabuf_len(cert));
  424. wpa_msg_ctrl(e->wpa_s, MSG_INFO,
  425. WPA_EVENT_EAP_PEER_CERT
  426. "depth=%d subject='%s' cert=%s",
  427. depth, subject, cert_hex);
  428. os_free(cert_hex);
  429. }
  430. if (e->server_cert_file)
  431. eapol_test_write_cert(e->server_cert_file,
  432. subject, cert);
  433. }
  434. if (altsubject) {
  435. int i;
  436. for (i = 0; i < num_altsubject; i++)
  437. wpa_msg(e->wpa_s, MSG_INFO, WPA_EVENT_EAP_PEER_ALT
  438. "depth=%d %s", depth, altsubject[i]);
  439. }
  440. }
  441. static void eapol_test_set_anon_id(void *ctx, const u8 *id, size_t len)
  442. {
  443. struct eapol_test_data *e = ctx;
  444. struct wpa_supplicant *wpa_s = e->wpa_s;
  445. char *str;
  446. int res;
  447. wpa_hexdump_ascii(MSG_DEBUG, "EAP method updated anonymous_identity",
  448. id, len);
  449. if (wpa_s->current_ssid == NULL)
  450. return;
  451. if (id == NULL) {
  452. if (wpa_config_set(wpa_s->current_ssid, "anonymous_identity",
  453. "NULL", 0) < 0)
  454. return;
  455. } else {
  456. str = os_malloc(len * 2 + 1);
  457. if (str == NULL)
  458. return;
  459. wpa_snprintf_hex(str, len * 2 + 1, id, len);
  460. res = wpa_config_set(wpa_s->current_ssid, "anonymous_identity",
  461. str, 0);
  462. os_free(str);
  463. if (res < 0)
  464. return;
  465. }
  466. }
  467. static int test_eapol(struct eapol_test_data *e, struct wpa_supplicant *wpa_s,
  468. struct wpa_ssid *ssid)
  469. {
  470. struct eapol_config eapol_conf;
  471. struct eapol_ctx *ctx;
  472. ctx = os_zalloc(sizeof(*ctx));
  473. if (ctx == NULL) {
  474. printf("Failed to allocate EAPOL context.\n");
  475. return -1;
  476. }
  477. ctx->ctx = e;
  478. ctx->msg_ctx = wpa_s;
  479. ctx->scard_ctx = wpa_s->scard;
  480. ctx->cb = eapol_sm_cb;
  481. ctx->cb_ctx = e;
  482. ctx->eapol_send_ctx = wpa_s;
  483. ctx->preauth = 0;
  484. ctx->eapol_done_cb = eapol_test_eapol_done_cb;
  485. ctx->eapol_send = eapol_test_eapol_send;
  486. ctx->set_config_blob = eapol_test_set_config_blob;
  487. ctx->get_config_blob = eapol_test_get_config_blob;
  488. ctx->opensc_engine_path = wpa_s->conf->opensc_engine_path;
  489. ctx->pkcs11_engine_path = wpa_s->conf->pkcs11_engine_path;
  490. ctx->pkcs11_module_path = wpa_s->conf->pkcs11_module_path;
  491. ctx->openssl_ciphers = wpa_s->conf->openssl_ciphers;
  492. ctx->eap_param_needed = eapol_test_eap_param_needed;
  493. ctx->cert_cb = eapol_test_cert_cb;
  494. ctx->cert_in_cb = 1;
  495. ctx->set_anon_id = eapol_test_set_anon_id;
  496. wpa_s->eapol = eapol_sm_init(ctx);
  497. if (wpa_s->eapol == NULL) {
  498. os_free(ctx);
  499. printf("Failed to initialize EAPOL state machines.\n");
  500. return -1;
  501. }
  502. wpa_s->current_ssid = ssid;
  503. os_memset(&eapol_conf, 0, sizeof(eapol_conf));
  504. eapol_conf.accept_802_1x_keys = 1;
  505. eapol_conf.required_keys = 0;
  506. eapol_conf.fast_reauth = wpa_s->conf->fast_reauth;
  507. eapol_conf.workaround = ssid->eap_workaround;
  508. eapol_conf.external_sim = wpa_s->conf->external_sim;
  509. eapol_sm_notify_config(wpa_s->eapol, &ssid->eap, &eapol_conf);
  510. eapol_sm_register_scard_ctx(wpa_s->eapol, wpa_s->scard);
  511. eapol_sm_notify_portValid(wpa_s->eapol, FALSE);
  512. /* 802.1X::portControl = Auto */
  513. eapol_sm_notify_portEnabled(wpa_s->eapol, TRUE);
  514. return 0;
  515. }
  516. static void test_eapol_clean(struct eapol_test_data *e,
  517. struct wpa_supplicant *wpa_s)
  518. {
  519. struct extra_radius_attr *p, *prev;
  520. radius_client_deinit(e->radius);
  521. wpabuf_free(e->last_eap_radius);
  522. radius_msg_free(e->last_recv_radius);
  523. e->last_recv_radius = NULL;
  524. os_free(e->eap_identity);
  525. e->eap_identity = NULL;
  526. eapol_sm_deinit(wpa_s->eapol);
  527. wpa_s->eapol = NULL;
  528. if (e->radius_conf && e->radius_conf->auth_server) {
  529. os_free(e->radius_conf->auth_server->shared_secret);
  530. os_free(e->radius_conf->auth_server);
  531. }
  532. os_free(e->radius_conf);
  533. e->radius_conf = NULL;
  534. scard_deinit(wpa_s->scard);
  535. if (wpa_s->ctrl_iface) {
  536. wpa_supplicant_ctrl_iface_deinit(wpa_s->ctrl_iface);
  537. wpa_s->ctrl_iface = NULL;
  538. }
  539. ext_password_deinit(wpa_s->ext_pw);
  540. wpa_s->ext_pw = NULL;
  541. wpa_config_free(wpa_s->conf);
  542. p = e->extra_attrs;
  543. while (p) {
  544. prev = p;
  545. p = p->next;
  546. os_free(prev);
  547. }
  548. }
  549. static void send_eap_request_identity(void *eloop_ctx, void *timeout_ctx)
  550. {
  551. struct wpa_supplicant *wpa_s = eloop_ctx;
  552. u8 buf[100], *pos;
  553. struct ieee802_1x_hdr *hdr;
  554. struct eap_hdr *eap;
  555. hdr = (struct ieee802_1x_hdr *) buf;
  556. hdr->version = EAPOL_VERSION;
  557. hdr->type = IEEE802_1X_TYPE_EAP_PACKET;
  558. hdr->length = htons(5);
  559. eap = (struct eap_hdr *) (hdr + 1);
  560. eap->code = EAP_CODE_REQUEST;
  561. eap->identifier = 0;
  562. eap->length = htons(5);
  563. pos = (u8 *) (eap + 1);
  564. *pos = EAP_TYPE_IDENTITY;
  565. printf("Sending fake EAP-Request-Identity\n");
  566. eapol_sm_rx_eapol(wpa_s->eapol, wpa_s->bssid, buf,
  567. sizeof(*hdr) + 5);
  568. }
  569. static void eapol_test_timeout(void *eloop_ctx, void *timeout_ctx)
  570. {
  571. struct eapol_test_data *e = eloop_ctx;
  572. printf("EAPOL test timed out\n");
  573. e->auth_timed_out = 1;
  574. eloop_terminate();
  575. }
  576. static char *eap_type_text(u8 type)
  577. {
  578. switch (type) {
  579. case EAP_TYPE_IDENTITY: return "Identity";
  580. case EAP_TYPE_NOTIFICATION: return "Notification";
  581. case EAP_TYPE_NAK: return "Nak";
  582. case EAP_TYPE_TLS: return "TLS";
  583. case EAP_TYPE_TTLS: return "TTLS";
  584. case EAP_TYPE_PEAP: return "PEAP";
  585. case EAP_TYPE_SIM: return "SIM";
  586. case EAP_TYPE_GTC: return "GTC";
  587. case EAP_TYPE_MD5: return "MD5";
  588. case EAP_TYPE_OTP: return "OTP";
  589. case EAP_TYPE_FAST: return "FAST";
  590. case EAP_TYPE_SAKE: return "SAKE";
  591. case EAP_TYPE_PSK: return "PSK";
  592. default: return "Unknown";
  593. }
  594. }
  595. static void ieee802_1x_decapsulate_radius(struct eapol_test_data *e)
  596. {
  597. struct wpabuf *eap;
  598. const struct eap_hdr *hdr;
  599. int eap_type = -1;
  600. char buf[64];
  601. struct radius_msg *msg;
  602. if (e->last_recv_radius == NULL)
  603. return;
  604. msg = e->last_recv_radius;
  605. eap = radius_msg_get_eap(msg);
  606. if (eap == NULL) {
  607. /* draft-aboba-radius-rfc2869bis-20.txt, Chap. 2.6.3:
  608. * RADIUS server SHOULD NOT send Access-Reject/no EAP-Message
  609. * attribute */
  610. wpa_printf(MSG_DEBUG, "could not extract "
  611. "EAP-Message from RADIUS message");
  612. wpabuf_free(e->last_eap_radius);
  613. e->last_eap_radius = NULL;
  614. return;
  615. }
  616. if (wpabuf_len(eap) < sizeof(*hdr)) {
  617. wpa_printf(MSG_DEBUG, "too short EAP packet "
  618. "received from authentication server");
  619. wpabuf_free(eap);
  620. return;
  621. }
  622. if (wpabuf_len(eap) > sizeof(*hdr))
  623. eap_type = (wpabuf_head_u8(eap))[sizeof(*hdr)];
  624. hdr = wpabuf_head(eap);
  625. switch (hdr->code) {
  626. case EAP_CODE_REQUEST:
  627. os_snprintf(buf, sizeof(buf), "EAP-Request-%s (%d)",
  628. eap_type >= 0 ? eap_type_text(eap_type) : "??",
  629. eap_type);
  630. break;
  631. case EAP_CODE_RESPONSE:
  632. os_snprintf(buf, sizeof(buf), "EAP Response-%s (%d)",
  633. eap_type >= 0 ? eap_type_text(eap_type) : "??",
  634. eap_type);
  635. break;
  636. case EAP_CODE_SUCCESS:
  637. os_strlcpy(buf, "EAP Success", sizeof(buf));
  638. /* LEAP uses EAP Success within an authentication, so must not
  639. * stop here with eloop_terminate(); */
  640. break;
  641. case EAP_CODE_FAILURE:
  642. os_strlcpy(buf, "EAP Failure", sizeof(buf));
  643. eloop_terminate();
  644. break;
  645. default:
  646. os_strlcpy(buf, "unknown EAP code", sizeof(buf));
  647. wpa_hexdump_buf(MSG_DEBUG, "Decapsulated EAP packet", eap);
  648. break;
  649. }
  650. wpa_printf(MSG_DEBUG, "decapsulated EAP packet (code=%d "
  651. "id=%d len=%d) from RADIUS server: %s",
  652. hdr->code, hdr->identifier, ntohs(hdr->length), buf);
  653. /* sta->eapol_sm->be_auth.idFromServer = hdr->identifier; */
  654. wpabuf_free(e->last_eap_radius);
  655. e->last_eap_radius = eap;
  656. {
  657. struct ieee802_1x_hdr *dot1x;
  658. dot1x = os_malloc(sizeof(*dot1x) + wpabuf_len(eap));
  659. assert(dot1x != NULL);
  660. dot1x->version = EAPOL_VERSION;
  661. dot1x->type = IEEE802_1X_TYPE_EAP_PACKET;
  662. dot1x->length = htons(wpabuf_len(eap));
  663. os_memcpy((u8 *) (dot1x + 1), wpabuf_head(eap),
  664. wpabuf_len(eap));
  665. eapol_sm_rx_eapol(e->wpa_s->eapol, e->wpa_s->bssid,
  666. (u8 *) dot1x,
  667. sizeof(*dot1x) + wpabuf_len(eap));
  668. os_free(dot1x);
  669. }
  670. }
  671. static void ieee802_1x_get_keys(struct eapol_test_data *e,
  672. struct radius_msg *msg, struct radius_msg *req,
  673. const u8 *shared_secret,
  674. size_t shared_secret_len)
  675. {
  676. struct radius_ms_mppe_keys *keys;
  677. u8 *buf;
  678. size_t len;
  679. keys = radius_msg_get_ms_keys(msg, req, shared_secret,
  680. shared_secret_len);
  681. if (keys && keys->send == NULL && keys->recv == NULL) {
  682. os_free(keys);
  683. keys = radius_msg_get_cisco_keys(msg, req, shared_secret,
  684. shared_secret_len);
  685. }
  686. if (keys) {
  687. if (keys->send) {
  688. wpa_hexdump(MSG_DEBUG, "MS-MPPE-Send-Key (sign)",
  689. keys->send, keys->send_len);
  690. }
  691. if (keys->recv) {
  692. wpa_hexdump(MSG_DEBUG, "MS-MPPE-Recv-Key (crypt)",
  693. keys->recv, keys->recv_len);
  694. e->authenticator_pmk_len =
  695. keys->recv_len > PMK_LEN ? PMK_LEN :
  696. keys->recv_len;
  697. os_memcpy(e->authenticator_pmk, keys->recv,
  698. e->authenticator_pmk_len);
  699. if (e->authenticator_pmk_len == 16 && keys->send &&
  700. keys->send_len == 16) {
  701. /* MS-CHAP-v2 derives 16 octet keys */
  702. wpa_printf(MSG_DEBUG, "Use MS-MPPE-Send-Key "
  703. "to extend PMK to 32 octets");
  704. os_memcpy(e->authenticator_pmk +
  705. e->authenticator_pmk_len,
  706. keys->send, keys->send_len);
  707. e->authenticator_pmk_len += keys->send_len;
  708. }
  709. }
  710. os_free(keys->send);
  711. os_free(keys->recv);
  712. os_free(keys);
  713. }
  714. if (radius_msg_get_attr_ptr(msg, RADIUS_ATTR_EAP_KEY_NAME, &buf, &len,
  715. NULL) == 0) {
  716. os_memcpy(e->authenticator_eap_key_name, buf, len);
  717. e->authenticator_eap_key_name_len = len;
  718. } else {
  719. e->authenticator_eap_key_name_len = 0;
  720. }
  721. }
  722. /* Process the RADIUS frames from Authentication Server */
  723. static RadiusRxResult
  724. ieee802_1x_receive_auth(struct radius_msg *msg, struct radius_msg *req,
  725. const u8 *shared_secret, size_t shared_secret_len,
  726. void *data)
  727. {
  728. struct eapol_test_data *e = data;
  729. struct radius_hdr *hdr = radius_msg_get_hdr(msg);
  730. /* RFC 2869, Ch. 5.13: valid Message-Authenticator attribute MUST be
  731. * present when packet contains an EAP-Message attribute */
  732. if (hdr->code == RADIUS_CODE_ACCESS_REJECT &&
  733. radius_msg_get_attr(msg, RADIUS_ATTR_MESSAGE_AUTHENTICATOR, NULL,
  734. 0) < 0 &&
  735. radius_msg_get_attr(msg, RADIUS_ATTR_EAP_MESSAGE, NULL, 0) < 0) {
  736. wpa_printf(MSG_DEBUG, "Allowing RADIUS "
  737. "Access-Reject without Message-Authenticator "
  738. "since it does not include EAP-Message\n");
  739. } else if (radius_msg_verify(msg, shared_secret, shared_secret_len,
  740. req, 1)) {
  741. printf("Incoming RADIUS packet did not have correct "
  742. "Message-Authenticator - dropped\n");
  743. return RADIUS_RX_UNKNOWN;
  744. }
  745. if (hdr->code != RADIUS_CODE_ACCESS_ACCEPT &&
  746. hdr->code != RADIUS_CODE_ACCESS_REJECT &&
  747. hdr->code != RADIUS_CODE_ACCESS_CHALLENGE) {
  748. printf("Unknown RADIUS message code\n");
  749. return RADIUS_RX_UNKNOWN;
  750. }
  751. e->radius_identifier = -1;
  752. wpa_printf(MSG_DEBUG, "RADIUS packet matching with station");
  753. radius_msg_free(e->last_recv_radius);
  754. e->last_recv_radius = msg;
  755. switch (hdr->code) {
  756. case RADIUS_CODE_ACCESS_ACCEPT:
  757. e->radius_access_accept_received = 1;
  758. ieee802_1x_get_keys(e, msg, req, shared_secret,
  759. shared_secret_len);
  760. break;
  761. case RADIUS_CODE_ACCESS_REJECT:
  762. e->radius_access_reject_received = 1;
  763. break;
  764. }
  765. ieee802_1x_decapsulate_radius(e);
  766. if ((hdr->code == RADIUS_CODE_ACCESS_ACCEPT &&
  767. e->eapol_test_num_reauths < 0) ||
  768. hdr->code == RADIUS_CODE_ACCESS_REJECT) {
  769. eloop_terminate();
  770. }
  771. return RADIUS_RX_QUEUED;
  772. }
  773. static void wpa_init_conf(struct eapol_test_data *e,
  774. struct wpa_supplicant *wpa_s, const char *authsrv,
  775. int port, const char *secret,
  776. const char *cli_addr)
  777. {
  778. struct hostapd_radius_server *as;
  779. int res;
  780. wpa_s->bssid[5] = 1;
  781. os_memcpy(wpa_s->own_addr, e->own_addr, ETH_ALEN);
  782. e->own_ip_addr.s_addr = htonl((127 << 24) | 1);
  783. os_strlcpy(wpa_s->ifname, "test", sizeof(wpa_s->ifname));
  784. e->radius_conf = os_zalloc(sizeof(struct hostapd_radius_servers));
  785. assert(e->radius_conf != NULL);
  786. e->radius_conf->num_auth_servers = 1;
  787. as = os_zalloc(sizeof(struct hostapd_radius_server));
  788. assert(as != NULL);
  789. #if defined(CONFIG_NATIVE_WINDOWS) || defined(CONFIG_ANSI_C_EXTRA)
  790. {
  791. int a[4];
  792. u8 *pos;
  793. sscanf(authsrv, "%d.%d.%d.%d", &a[0], &a[1], &a[2], &a[3]);
  794. pos = (u8 *) &as->addr.u.v4;
  795. *pos++ = a[0];
  796. *pos++ = a[1];
  797. *pos++ = a[2];
  798. *pos++ = a[3];
  799. }
  800. #else /* CONFIG_NATIVE_WINDOWS or CONFIG_ANSI_C_EXTRA */
  801. if (inet_aton(authsrv, &as->addr.u.v4) < 0) {
  802. wpa_printf(MSG_ERROR, "Invalid IP address '%s'",
  803. authsrv);
  804. assert(0);
  805. }
  806. #endif /* CONFIG_NATIVE_WINDOWS or CONFIG_ANSI_C_EXTRA */
  807. as->addr.af = AF_INET;
  808. as->port = port;
  809. as->shared_secret = (u8 *) os_strdup(secret);
  810. as->shared_secret_len = os_strlen(secret);
  811. e->radius_conf->auth_server = as;
  812. e->radius_conf->auth_servers = as;
  813. e->radius_conf->msg_dumps = 1;
  814. if (cli_addr) {
  815. if (hostapd_parse_ip_addr(cli_addr,
  816. &e->radius_conf->client_addr) == 0)
  817. e->radius_conf->force_client_addr = 1;
  818. else {
  819. wpa_printf(MSG_ERROR, "Invalid IP address '%s'",
  820. cli_addr);
  821. assert(0);
  822. }
  823. }
  824. e->radius = radius_client_init(wpa_s, e->radius_conf);
  825. assert(e->radius != NULL);
  826. res = radius_client_register(e->radius, RADIUS_AUTH,
  827. ieee802_1x_receive_auth, e);
  828. assert(res == 0);
  829. }
  830. static int scard_test(struct eapol_test_data *e)
  831. {
  832. struct scard_data *scard;
  833. size_t len;
  834. char imsi[20];
  835. unsigned char _rand[16];
  836. #ifdef PCSC_FUNCS
  837. unsigned char sres[4];
  838. unsigned char kc[8];
  839. #endif /* PCSC_FUNCS */
  840. #define num_triplets 5
  841. unsigned char rand_[num_triplets][16];
  842. unsigned char sres_[num_triplets][4];
  843. unsigned char kc_[num_triplets][8];
  844. int i, res;
  845. size_t j;
  846. #define AKA_RAND_LEN 16
  847. #define AKA_AUTN_LEN 16
  848. #define AKA_AUTS_LEN 14
  849. #define RES_MAX_LEN 16
  850. #define IK_LEN 16
  851. #define CK_LEN 16
  852. unsigned char aka_rand[AKA_RAND_LEN];
  853. unsigned char aka_autn[AKA_AUTN_LEN];
  854. unsigned char aka_auts[AKA_AUTS_LEN];
  855. unsigned char aka_res[RES_MAX_LEN];
  856. size_t aka_res_len;
  857. unsigned char aka_ik[IK_LEN];
  858. unsigned char aka_ck[CK_LEN];
  859. scard = scard_init(e->pcsc_reader);
  860. if (scard == NULL)
  861. return -1;
  862. if (scard_set_pin(scard, e->pcsc_pin)) {
  863. wpa_printf(MSG_WARNING, "PIN validation failed");
  864. scard_deinit(scard);
  865. return -1;
  866. }
  867. len = sizeof(imsi);
  868. if (scard_get_imsi(scard, imsi, &len))
  869. goto failed;
  870. wpa_hexdump_ascii(MSG_DEBUG, "SCARD: IMSI", (u8 *) imsi, len);
  871. /* NOTE: Permanent Username: 1 | IMSI */
  872. wpa_printf(MSG_DEBUG, "SCARD: MNC length %d",
  873. scard_get_mnc_len(scard));
  874. os_memset(_rand, 0, sizeof(_rand));
  875. if (scard_gsm_auth(scard, _rand, sres, kc))
  876. goto failed;
  877. os_memset(_rand, 0xff, sizeof(_rand));
  878. if (scard_gsm_auth(scard, _rand, sres, kc))
  879. goto failed;
  880. for (i = 0; i < num_triplets; i++) {
  881. os_memset(rand_[i], i, sizeof(rand_[i]));
  882. if (scard_gsm_auth(scard, rand_[i], sres_[i], kc_[i]))
  883. goto failed;
  884. }
  885. for (i = 0; i < num_triplets; i++) {
  886. printf("1");
  887. for (j = 0; j < len; j++)
  888. printf("%c", imsi[j]);
  889. printf(",");
  890. for (j = 0; j < 16; j++)
  891. printf("%02X", rand_[i][j]);
  892. printf(",");
  893. for (j = 0; j < 4; j++)
  894. printf("%02X", sres_[i][j]);
  895. printf(",");
  896. for (j = 0; j < 8; j++)
  897. printf("%02X", kc_[i][j]);
  898. printf("\n");
  899. }
  900. wpa_printf(MSG_DEBUG, "Trying to use UMTS authentication");
  901. /* seq 39 (0x28) */
  902. os_memset(aka_rand, 0xaa, 16);
  903. os_memcpy(aka_autn, "\x86\x71\x31\xcb\xa2\xfc\x61\xdf"
  904. "\xa3\xb3\x97\x9d\x07\x32\xa2\x12", 16);
  905. res = scard_umts_auth(scard, aka_rand, aka_autn, aka_res, &aka_res_len,
  906. aka_ik, aka_ck, aka_auts);
  907. if (res == 0) {
  908. wpa_printf(MSG_DEBUG, "UMTS auth completed successfully");
  909. wpa_hexdump(MSG_DEBUG, "RES", aka_res, aka_res_len);
  910. wpa_hexdump(MSG_DEBUG, "IK", aka_ik, IK_LEN);
  911. wpa_hexdump(MSG_DEBUG, "CK", aka_ck, CK_LEN);
  912. } else if (res == -2) {
  913. wpa_printf(MSG_DEBUG, "UMTS auth resulted in synchronization "
  914. "failure");
  915. wpa_hexdump(MSG_DEBUG, "AUTS", aka_auts, AKA_AUTS_LEN);
  916. } else {
  917. wpa_printf(MSG_DEBUG, "UMTS auth failed");
  918. }
  919. failed:
  920. scard_deinit(scard);
  921. return 0;
  922. #undef num_triplets
  923. }
  924. static int scard_get_triplets(struct eapol_test_data *e, int argc, char *argv[])
  925. {
  926. struct scard_data *scard;
  927. size_t len;
  928. char imsi[20];
  929. unsigned char _rand[16];
  930. unsigned char sres[4];
  931. unsigned char kc[8];
  932. int num_triplets;
  933. int i;
  934. size_t j;
  935. if (argc < 2 || ((num_triplets = atoi(argv[1])) <= 0)) {
  936. printf("invalid parameters for sim command\n");
  937. return -1;
  938. }
  939. if (argc <= 2 || os_strcmp(argv[2], "debug") != 0) {
  940. /* disable debug output */
  941. wpa_debug_level = 99;
  942. }
  943. scard = scard_init(e->pcsc_reader);
  944. if (scard == NULL) {
  945. printf("Failed to open smartcard connection\n");
  946. return -1;
  947. }
  948. if (scard_set_pin(scard, argv[0])) {
  949. wpa_printf(MSG_WARNING, "PIN validation failed");
  950. scard_deinit(scard);
  951. return -1;
  952. }
  953. len = sizeof(imsi);
  954. if (scard_get_imsi(scard, imsi, &len)) {
  955. scard_deinit(scard);
  956. return -1;
  957. }
  958. for (i = 0; i < num_triplets; i++) {
  959. os_memset(_rand, i, sizeof(_rand));
  960. if (scard_gsm_auth(scard, _rand, sres, kc))
  961. break;
  962. /* IMSI:Kc:SRES:RAND */
  963. for (j = 0; j < len; j++)
  964. printf("%c", imsi[j]);
  965. printf(":");
  966. for (j = 0; j < 8; j++)
  967. printf("%02X", kc[j]);
  968. printf(":");
  969. for (j = 0; j < 4; j++)
  970. printf("%02X", sres[j]);
  971. printf(":");
  972. for (j = 0; j < 16; j++)
  973. printf("%02X", _rand[j]);
  974. printf("\n");
  975. }
  976. scard_deinit(scard);
  977. return 0;
  978. }
  979. static void eapol_test_terminate(int sig, void *signal_ctx)
  980. {
  981. struct wpa_supplicant *wpa_s = signal_ctx;
  982. wpa_msg(wpa_s, MSG_INFO, "Signal %d received - terminating", sig);
  983. eloop_terminate();
  984. }
  985. static void usage(void)
  986. {
  987. printf("usage:\n"
  988. "eapol_test [-enWS] -c<conf> [-a<AS IP>] [-p<AS port>] "
  989. "[-s<AS secret>]\\\n"
  990. " [-r<count>] [-t<timeout>] [-C<Connect-Info>] \\\n"
  991. " [-M<client MAC address>] [-o<server cert file] \\\n"
  992. " [-N<attr spec>] [-R<PC/SC reader>] "
  993. "[-P<PC/SC PIN>] \\\n"
  994. " [-A<client IP>]\n"
  995. "eapol_test scard\n"
  996. "eapol_test sim <PIN> <num triplets> [debug]\n"
  997. "\n");
  998. printf("options:\n"
  999. " -c<conf> = configuration file\n"
  1000. " -a<AS IP> = IP address of the authentication server, "
  1001. "default 127.0.0.1\n"
  1002. " -p<AS port> = UDP port of the authentication server, "
  1003. "default 1812\n"
  1004. " -s<AS secret> = shared secret with the authentication "
  1005. "server, default 'radius'\n"
  1006. " -A<client IP> = IP address of the client, default: select "
  1007. "automatically\n"
  1008. " -r<count> = number of re-authentications\n"
  1009. " -e = Request EAP-Key-Name\n"
  1010. " -W = wait for a control interface monitor before starting\n"
  1011. " -S = save configuration after authentication\n"
  1012. " -n = no MPPE keys expected\n"
  1013. " -t<timeout> = sets timeout in seconds (default: 30 s)\n"
  1014. " -C<Connect-Info> = RADIUS Connect-Info (default: "
  1015. "CONNECT 11Mbps 802.11b)\n"
  1016. " -M<client MAC address> = Set own MAC address "
  1017. "(Calling-Station-Id,\n"
  1018. " default: 02:00:00:00:00:01)\n"
  1019. " -o<server cert file> = Write received server certificate\n"
  1020. " chain to the specified file\n"
  1021. " -N<attr spec> = send arbitrary attribute specified by:\n"
  1022. " attr_id:syntax:value or attr_id\n"
  1023. " attr_id - number id of the attribute\n"
  1024. " syntax - one of: s, d, x\n"
  1025. " s = string\n"
  1026. " d = integer\n"
  1027. " x = octet string\n"
  1028. " value - attribute value.\n"
  1029. " When only attr_id is specified, NULL will be used as "
  1030. "value.\n"
  1031. " Multiple attributes can be specified by using the "
  1032. "option several times.\n");
  1033. }
  1034. int main(int argc, char *argv[])
  1035. {
  1036. struct wpa_global global;
  1037. struct wpa_supplicant wpa_s;
  1038. int c, ret = 1, wait_for_monitor = 0, save_config = 0;
  1039. char *as_addr = "127.0.0.1";
  1040. int as_port = 1812;
  1041. char *as_secret = "radius";
  1042. char *cli_addr = NULL;
  1043. char *conf = NULL;
  1044. int timeout = 30;
  1045. char *pos;
  1046. struct extra_radius_attr *p = NULL, *p1;
  1047. if (os_program_init())
  1048. return -1;
  1049. hostapd_logger_register_cb(hostapd_logger_cb);
  1050. os_memset(&eapol_test, 0, sizeof(eapol_test));
  1051. eapol_test.connect_info = "CONNECT 11Mbps 802.11b";
  1052. os_memcpy(eapol_test.own_addr, "\x02\x00\x00\x00\x00\x01", ETH_ALEN);
  1053. eapol_test.pcsc_pin = "1234";
  1054. wpa_debug_level = 0;
  1055. wpa_debug_show_keys = 1;
  1056. for (;;) {
  1057. c = getopt(argc, argv, "a:A:c:C:eM:nN:o:p:P:r:R:s:St:W");
  1058. if (c < 0)
  1059. break;
  1060. switch (c) {
  1061. case 'a':
  1062. as_addr = optarg;
  1063. break;
  1064. case 'A':
  1065. cli_addr = optarg;
  1066. break;
  1067. case 'c':
  1068. conf = optarg;
  1069. break;
  1070. case 'C':
  1071. eapol_test.connect_info = optarg;
  1072. break;
  1073. case 'e':
  1074. eapol_test.req_eap_key_name = 1;
  1075. break;
  1076. case 'M':
  1077. if (hwaddr_aton(optarg, eapol_test.own_addr)) {
  1078. usage();
  1079. return -1;
  1080. }
  1081. break;
  1082. case 'n':
  1083. eapol_test.no_mppe_keys++;
  1084. break;
  1085. case 'o':
  1086. if (eapol_test.server_cert_file)
  1087. fclose(eapol_test.server_cert_file);
  1088. eapol_test.server_cert_file = fopen(optarg, "w");
  1089. if (eapol_test.server_cert_file == NULL) {
  1090. printf("Could not open '%s' for writing\n",
  1091. optarg);
  1092. return -1;
  1093. }
  1094. break;
  1095. case 'p':
  1096. as_port = atoi(optarg);
  1097. break;
  1098. case 'P':
  1099. eapol_test.pcsc_pin = optarg;
  1100. break;
  1101. case 'r':
  1102. eapol_test.eapol_test_num_reauths = atoi(optarg);
  1103. break;
  1104. case 'R':
  1105. eapol_test.pcsc_reader = optarg;
  1106. break;
  1107. case 's':
  1108. as_secret = optarg;
  1109. break;
  1110. case 'S':
  1111. save_config++;
  1112. break;
  1113. case 't':
  1114. timeout = atoi(optarg);
  1115. break;
  1116. case 'W':
  1117. wait_for_monitor++;
  1118. break;
  1119. case 'N':
  1120. p1 = os_zalloc(sizeof(*p1));
  1121. if (p1 == NULL)
  1122. break;
  1123. if (!p)
  1124. eapol_test.extra_attrs = p1;
  1125. else
  1126. p->next = p1;
  1127. p = p1;
  1128. p->type = atoi(optarg);
  1129. pos = os_strchr(optarg, ':');
  1130. if (pos == NULL) {
  1131. p->syntax = 'n';
  1132. p->data = NULL;
  1133. break;
  1134. }
  1135. pos++;
  1136. if (pos[0] == '\0' || pos[1] != ':') {
  1137. printf("Incorrect format of attribute "
  1138. "specification\n");
  1139. break;
  1140. }
  1141. p->syntax = pos[0];
  1142. p->data = pos + 2;
  1143. break;
  1144. default:
  1145. usage();
  1146. return -1;
  1147. }
  1148. }
  1149. if (argc > optind && os_strcmp(argv[optind], "scard") == 0) {
  1150. return scard_test(&eapol_test);
  1151. }
  1152. if (argc > optind && os_strcmp(argv[optind], "sim") == 0) {
  1153. return scard_get_triplets(&eapol_test, argc - optind - 1,
  1154. &argv[optind + 1]);
  1155. }
  1156. if (conf == NULL) {
  1157. usage();
  1158. printf("Configuration file is required.\n");
  1159. return -1;
  1160. }
  1161. if (eap_register_methods()) {
  1162. wpa_printf(MSG_ERROR, "Failed to register EAP methods");
  1163. return -1;
  1164. }
  1165. if (eloop_init()) {
  1166. wpa_printf(MSG_ERROR, "Failed to initialize event loop");
  1167. return -1;
  1168. }
  1169. os_memset(&global, 0, sizeof(global));
  1170. os_memset(&wpa_s, 0, sizeof(wpa_s));
  1171. wpa_s.global = &global;
  1172. eapol_test.wpa_s = &wpa_s;
  1173. dl_list_init(&wpa_s.bss);
  1174. dl_list_init(&wpa_s.bss_id);
  1175. wpa_s.conf = wpa_config_read(conf, NULL);
  1176. if (wpa_s.conf == NULL) {
  1177. printf("Failed to parse configuration file '%s'.\n", conf);
  1178. return -1;
  1179. }
  1180. if (wpa_s.conf->ssid == NULL) {
  1181. printf("No networks defined.\n");
  1182. return -1;
  1183. }
  1184. if (eapol_test.pcsc_reader) {
  1185. os_free(wpa_s.conf->pcsc_reader);
  1186. wpa_s.conf->pcsc_reader = os_strdup(eapol_test.pcsc_reader);
  1187. }
  1188. wpa_init_conf(&eapol_test, &wpa_s, as_addr, as_port, as_secret,
  1189. cli_addr);
  1190. wpa_s.ctrl_iface = wpa_supplicant_ctrl_iface_init(&wpa_s);
  1191. if (wpa_s.ctrl_iface == NULL) {
  1192. printf("Failed to initialize control interface '%s'.\n"
  1193. "You may have another eapol_test process already "
  1194. "running or the file was\n"
  1195. "left by an unclean termination of eapol_test in "
  1196. "which case you will need\n"
  1197. "to manually remove this file before starting "
  1198. "eapol_test again.\n",
  1199. wpa_s.conf->ctrl_interface);
  1200. return -1;
  1201. }
  1202. if (wpa_supplicant_scard_init(&wpa_s, wpa_s.conf->ssid))
  1203. return -1;
  1204. if (test_eapol(&eapol_test, &wpa_s, wpa_s.conf->ssid))
  1205. return -1;
  1206. if (wpas_init_ext_pw(&wpa_s) < 0)
  1207. return -1;
  1208. if (wait_for_monitor)
  1209. wpa_supplicant_ctrl_iface_wait(wpa_s.ctrl_iface);
  1210. eloop_register_timeout(timeout, 0, eapol_test_timeout, &eapol_test,
  1211. NULL);
  1212. eloop_register_timeout(0, 0, send_eap_request_identity, &wpa_s, NULL);
  1213. eloop_register_signal_terminate(eapol_test_terminate, &wpa_s);
  1214. eloop_register_signal_reconfig(eapol_test_terminate, &wpa_s);
  1215. eloop_run();
  1216. eloop_cancel_timeout(eapol_test_timeout, &eapol_test, NULL);
  1217. eloop_cancel_timeout(eapol_sm_reauth, &eapol_test, NULL);
  1218. if (eapol_test_compare_pmk(&eapol_test) == 0 ||
  1219. eapol_test.no_mppe_keys)
  1220. ret = 0;
  1221. if (eapol_test.auth_timed_out)
  1222. ret = -2;
  1223. if (eapol_test.radius_access_reject_received)
  1224. ret = -3;
  1225. if (save_config)
  1226. wpa_config_write(conf, wpa_s.conf);
  1227. test_eapol_clean(&eapol_test, &wpa_s);
  1228. eap_peer_unregister_methods();
  1229. #ifdef CONFIG_AP
  1230. eap_server_unregister_methods();
  1231. #endif /* CONFIG_AP */
  1232. eloop_destroy();
  1233. if (eapol_test.server_cert_file)
  1234. fclose(eapol_test.server_cert_file);
  1235. printf("MPPE keys OK: %d mismatch: %d\n",
  1236. eapol_test.num_mppe_ok, eapol_test.num_mppe_mismatch);
  1237. if (eapol_test.num_mppe_mismatch)
  1238. ret = -4;
  1239. if (ret)
  1240. printf("FAILURE\n");
  1241. else
  1242. printf("SUCCESS\n");
  1243. os_program_deinit();
  1244. return ret;
  1245. }