eapol_test.c 37 KB

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