eapol_test.c 39 KB

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