eapol_test.c 39 KB

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