eap_ttls.c 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477
  1. /*
  2. * hostapd / EAP-TTLS (RFC 5281)
  3. * Copyright (c) 2004-2008, Jouni Malinen <j@w1.fi>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. *
  9. * Alternatively, this software may be distributed under the terms of BSD
  10. * license.
  11. *
  12. * See README and COPYING for more details.
  13. */
  14. #include "includes.h"
  15. #include "common.h"
  16. #include "eap_server/eap_i.h"
  17. #include "eap_server/eap_tls_common.h"
  18. #include "ms_funcs.h"
  19. #include "sha1.h"
  20. #include "eap_common/chap.h"
  21. #include "tls.h"
  22. #include "eap_common/eap_ttls.h"
  23. /* Maximum supported TTLS version
  24. * 0 = RFC 5281
  25. * 1 = draft-funk-eap-ttls-v1-00.txt
  26. */
  27. #ifndef EAP_TTLS_VERSION
  28. #define EAP_TTLS_VERSION 0 /* TTLSv1 implementation is not yet complete */
  29. #endif /* EAP_TTLS_VERSION */
  30. #define MSCHAPV2_KEY_LEN 16
  31. static void eap_ttls_reset(struct eap_sm *sm, void *priv);
  32. struct eap_ttls_data {
  33. struct eap_ssl_data ssl;
  34. enum {
  35. START, PHASE1, PHASE2_START, PHASE2_METHOD,
  36. PHASE2_MSCHAPV2_RESP, PHASE_FINISHED, SUCCESS, FAILURE
  37. } state;
  38. int ttls_version;
  39. int force_version;
  40. const struct eap_method *phase2_method;
  41. void *phase2_priv;
  42. int mschapv2_resp_ok;
  43. u8 mschapv2_auth_response[20];
  44. u8 mschapv2_ident;
  45. int tls_ia_configured;
  46. struct wpabuf *pending_phase2_eap_resp;
  47. int tnc_started;
  48. };
  49. static const char * eap_ttls_state_txt(int state)
  50. {
  51. switch (state) {
  52. case START:
  53. return "START";
  54. case PHASE1:
  55. return "PHASE1";
  56. case PHASE2_START:
  57. return "PHASE2_START";
  58. case PHASE2_METHOD:
  59. return "PHASE2_METHOD";
  60. case PHASE2_MSCHAPV2_RESP:
  61. return "PHASE2_MSCHAPV2_RESP";
  62. case PHASE_FINISHED:
  63. return "PHASE_FINISHED";
  64. case SUCCESS:
  65. return "SUCCESS";
  66. case FAILURE:
  67. return "FAILURE";
  68. default:
  69. return "Unknown?!";
  70. }
  71. }
  72. static void eap_ttls_state(struct eap_ttls_data *data, int state)
  73. {
  74. wpa_printf(MSG_DEBUG, "EAP-TTLS: %s -> %s",
  75. eap_ttls_state_txt(data->state),
  76. eap_ttls_state_txt(state));
  77. data->state = state;
  78. }
  79. static u8 * eap_ttls_avp_hdr(u8 *avphdr, u32 avp_code, u32 vendor_id,
  80. int mandatory, size_t len)
  81. {
  82. struct ttls_avp_vendor *avp;
  83. u8 flags;
  84. size_t hdrlen;
  85. avp = (struct ttls_avp_vendor *) avphdr;
  86. flags = mandatory ? AVP_FLAGS_MANDATORY : 0;
  87. if (vendor_id) {
  88. flags |= AVP_FLAGS_VENDOR;
  89. hdrlen = sizeof(*avp);
  90. avp->vendor_id = host_to_be32(vendor_id);
  91. } else {
  92. hdrlen = sizeof(struct ttls_avp);
  93. }
  94. avp->avp_code = host_to_be32(avp_code);
  95. avp->avp_length = host_to_be32((flags << 24) | (hdrlen + len));
  96. return avphdr + hdrlen;
  97. }
  98. static struct wpabuf * eap_ttls_avp_encapsulate(struct wpabuf *resp,
  99. u32 avp_code, int mandatory)
  100. {
  101. struct wpabuf *avp;
  102. u8 *pos;
  103. avp = wpabuf_alloc(sizeof(struct ttls_avp) + wpabuf_len(resp) + 4);
  104. if (avp == NULL) {
  105. wpabuf_free(resp);
  106. return NULL;
  107. }
  108. pos = eap_ttls_avp_hdr(wpabuf_mhead(avp), avp_code, 0, mandatory,
  109. wpabuf_len(resp));
  110. os_memcpy(pos, wpabuf_head(resp), wpabuf_len(resp));
  111. pos += wpabuf_len(resp);
  112. AVP_PAD((const u8 *) wpabuf_head(avp), pos);
  113. wpabuf_free(resp);
  114. wpabuf_put(avp, pos - (u8 *) wpabuf_head(avp));
  115. return avp;
  116. }
  117. struct eap_ttls_avp {
  118. /* Note: eap is allocated memory; caller is responsible for freeing
  119. * it. All the other pointers are pointing to the packet data, i.e.,
  120. * they must not be freed separately. */
  121. u8 *eap;
  122. size_t eap_len;
  123. u8 *user_name;
  124. size_t user_name_len;
  125. u8 *user_password;
  126. size_t user_password_len;
  127. u8 *chap_challenge;
  128. size_t chap_challenge_len;
  129. u8 *chap_password;
  130. size_t chap_password_len;
  131. u8 *mschap_challenge;
  132. size_t mschap_challenge_len;
  133. u8 *mschap_response;
  134. size_t mschap_response_len;
  135. u8 *mschap2_response;
  136. size_t mschap2_response_len;
  137. };
  138. static int eap_ttls_avp_parse(u8 *buf, size_t len, struct eap_ttls_avp *parse)
  139. {
  140. struct ttls_avp *avp;
  141. u8 *pos;
  142. int left;
  143. pos = buf;
  144. left = len;
  145. os_memset(parse, 0, sizeof(*parse));
  146. while (left > 0) {
  147. u32 avp_code, avp_length, vendor_id = 0;
  148. u8 avp_flags, *dpos;
  149. size_t pad, dlen;
  150. avp = (struct ttls_avp *) pos;
  151. avp_code = be_to_host32(avp->avp_code);
  152. avp_length = be_to_host32(avp->avp_length);
  153. avp_flags = (avp_length >> 24) & 0xff;
  154. avp_length &= 0xffffff;
  155. wpa_printf(MSG_DEBUG, "EAP-TTLS: AVP: code=%d flags=0x%02x "
  156. "length=%d", (int) avp_code, avp_flags,
  157. (int) avp_length);
  158. if ((int) avp_length > left) {
  159. wpa_printf(MSG_WARNING, "EAP-TTLS: AVP overflow "
  160. "(len=%d, left=%d) - dropped",
  161. (int) avp_length, left);
  162. goto fail;
  163. }
  164. if (avp_length < sizeof(*avp)) {
  165. wpa_printf(MSG_WARNING, "EAP-TTLS: Invalid AVP length "
  166. "%d", avp_length);
  167. goto fail;
  168. }
  169. dpos = (u8 *) (avp + 1);
  170. dlen = avp_length - sizeof(*avp);
  171. if (avp_flags & AVP_FLAGS_VENDOR) {
  172. if (dlen < 4) {
  173. wpa_printf(MSG_WARNING, "EAP-TTLS: vendor AVP "
  174. "underflow");
  175. goto fail;
  176. }
  177. vendor_id = be_to_host32(* (be32 *) dpos);
  178. wpa_printf(MSG_DEBUG, "EAP-TTLS: AVP vendor_id %d",
  179. (int) vendor_id);
  180. dpos += 4;
  181. dlen -= 4;
  182. }
  183. wpa_hexdump(MSG_DEBUG, "EAP-TTLS: AVP data", dpos, dlen);
  184. if (vendor_id == 0 && avp_code == RADIUS_ATTR_EAP_MESSAGE) {
  185. wpa_printf(MSG_DEBUG, "EAP-TTLS: AVP - EAP Message");
  186. if (parse->eap == NULL) {
  187. parse->eap = os_malloc(dlen);
  188. if (parse->eap == NULL) {
  189. wpa_printf(MSG_WARNING, "EAP-TTLS: "
  190. "failed to allocate memory "
  191. "for Phase 2 EAP data");
  192. goto fail;
  193. }
  194. os_memcpy(parse->eap, dpos, dlen);
  195. parse->eap_len = dlen;
  196. } else {
  197. u8 *neweap = os_realloc(parse->eap,
  198. parse->eap_len + dlen);
  199. if (neweap == NULL) {
  200. wpa_printf(MSG_WARNING, "EAP-TTLS: "
  201. "failed to allocate memory "
  202. "for Phase 2 EAP data");
  203. goto fail;
  204. }
  205. os_memcpy(neweap + parse->eap_len, dpos, dlen);
  206. parse->eap = neweap;
  207. parse->eap_len += dlen;
  208. }
  209. } else if (vendor_id == 0 &&
  210. avp_code == RADIUS_ATTR_USER_NAME) {
  211. wpa_hexdump_ascii(MSG_DEBUG, "EAP-TTLS: User-Name",
  212. dpos, dlen);
  213. parse->user_name = dpos;
  214. parse->user_name_len = dlen;
  215. } else if (vendor_id == 0 &&
  216. avp_code == RADIUS_ATTR_USER_PASSWORD) {
  217. u8 *password = dpos;
  218. size_t password_len = dlen;
  219. while (password_len > 0 &&
  220. password[password_len - 1] == '\0') {
  221. password_len--;
  222. }
  223. wpa_hexdump_ascii_key(MSG_DEBUG, "EAP-TTLS: "
  224. "User-Password (PAP)",
  225. password, password_len);
  226. parse->user_password = password;
  227. parse->user_password_len = password_len;
  228. } else if (vendor_id == 0 &&
  229. avp_code == RADIUS_ATTR_CHAP_CHALLENGE) {
  230. wpa_hexdump(MSG_DEBUG,
  231. "EAP-TTLS: CHAP-Challenge (CHAP)",
  232. dpos, dlen);
  233. parse->chap_challenge = dpos;
  234. parse->chap_challenge_len = dlen;
  235. } else if (vendor_id == 0 &&
  236. avp_code == RADIUS_ATTR_CHAP_PASSWORD) {
  237. wpa_hexdump(MSG_DEBUG,
  238. "EAP-TTLS: CHAP-Password (CHAP)",
  239. dpos, dlen);
  240. parse->chap_password = dpos;
  241. parse->chap_password_len = dlen;
  242. } else if (vendor_id == RADIUS_VENDOR_ID_MICROSOFT &&
  243. avp_code == RADIUS_ATTR_MS_CHAP_CHALLENGE) {
  244. wpa_hexdump(MSG_DEBUG,
  245. "EAP-TTLS: MS-CHAP-Challenge",
  246. dpos, dlen);
  247. parse->mschap_challenge = dpos;
  248. parse->mschap_challenge_len = dlen;
  249. } else if (vendor_id == RADIUS_VENDOR_ID_MICROSOFT &&
  250. avp_code == RADIUS_ATTR_MS_CHAP_RESPONSE) {
  251. wpa_hexdump(MSG_DEBUG,
  252. "EAP-TTLS: MS-CHAP-Response (MSCHAP)",
  253. dpos, dlen);
  254. parse->mschap_response = dpos;
  255. parse->mschap_response_len = dlen;
  256. } else if (vendor_id == RADIUS_VENDOR_ID_MICROSOFT &&
  257. avp_code == RADIUS_ATTR_MS_CHAP2_RESPONSE) {
  258. wpa_hexdump(MSG_DEBUG,
  259. "EAP-TTLS: MS-CHAP2-Response (MSCHAPV2)",
  260. dpos, dlen);
  261. parse->mschap2_response = dpos;
  262. parse->mschap2_response_len = dlen;
  263. } else if (avp_flags & AVP_FLAGS_MANDATORY) {
  264. wpa_printf(MSG_WARNING, "EAP-TTLS: Unsupported "
  265. "mandatory AVP code %d vendor_id %d - "
  266. "dropped", (int) avp_code, (int) vendor_id);
  267. goto fail;
  268. } else {
  269. wpa_printf(MSG_DEBUG, "EAP-TTLS: Ignoring unsupported "
  270. "AVP code %d vendor_id %d",
  271. (int) avp_code, (int) vendor_id);
  272. }
  273. pad = (4 - (avp_length & 3)) & 3;
  274. pos += avp_length + pad;
  275. left -= avp_length + pad;
  276. }
  277. return 0;
  278. fail:
  279. os_free(parse->eap);
  280. parse->eap = NULL;
  281. return -1;
  282. }
  283. static u8 * eap_ttls_implicit_challenge(struct eap_sm *sm,
  284. struct eap_ttls_data *data, size_t len)
  285. {
  286. struct tls_keys keys;
  287. u8 *challenge, *rnd;
  288. if (data->ttls_version == 0) {
  289. return eap_server_tls_derive_key(sm, &data->ssl,
  290. "ttls challenge", len);
  291. }
  292. os_memset(&keys, 0, sizeof(keys));
  293. if (tls_connection_get_keys(sm->ssl_ctx, data->ssl.conn, &keys) ||
  294. keys.client_random == NULL || keys.server_random == NULL ||
  295. keys.inner_secret == NULL) {
  296. wpa_printf(MSG_INFO, "EAP-TTLS: Could not get inner secret, "
  297. "client random, or server random to derive "
  298. "implicit challenge");
  299. return NULL;
  300. }
  301. rnd = os_malloc(keys.client_random_len + keys.server_random_len);
  302. challenge = os_malloc(len);
  303. if (rnd == NULL || challenge == NULL) {
  304. wpa_printf(MSG_INFO, "EAP-TTLS: No memory for implicit "
  305. "challenge derivation");
  306. os_free(rnd);
  307. os_free(challenge);
  308. return NULL;
  309. }
  310. os_memcpy(rnd, keys.server_random, keys.server_random_len);
  311. os_memcpy(rnd + keys.server_random_len, keys.client_random,
  312. keys.client_random_len);
  313. if (tls_prf(keys.inner_secret, keys.inner_secret_len,
  314. "inner application challenge", rnd,
  315. keys.client_random_len + keys.server_random_len,
  316. challenge, len)) {
  317. wpa_printf(MSG_DEBUG, "EAP-TTLS: Failed to derive implicit "
  318. "challenge");
  319. os_free(rnd);
  320. os_free(challenge);
  321. return NULL;
  322. }
  323. os_free(rnd);
  324. wpa_hexdump_key(MSG_DEBUG, "EAP-TTLS: Derived implicit challenge",
  325. challenge, len);
  326. return challenge;
  327. }
  328. static void * eap_ttls_init(struct eap_sm *sm)
  329. {
  330. struct eap_ttls_data *data;
  331. data = os_zalloc(sizeof(*data));
  332. if (data == NULL)
  333. return NULL;
  334. data->ttls_version = EAP_TTLS_VERSION;
  335. data->force_version = -1;
  336. if (sm->user && sm->user->force_version >= 0) {
  337. data->force_version = sm->user->force_version;
  338. wpa_printf(MSG_DEBUG, "EAP-TTLS: forcing version %d",
  339. data->force_version);
  340. data->ttls_version = data->force_version;
  341. }
  342. data->state = START;
  343. if (!(tls_capabilities(sm->ssl_ctx) & TLS_CAPABILITY_IA) &&
  344. data->ttls_version > 0) {
  345. if (data->force_version > 0) {
  346. wpa_printf(MSG_INFO, "EAP-TTLS: Forced TTLSv%d and "
  347. "TLS library does not support TLS/IA.",
  348. data->force_version);
  349. eap_ttls_reset(sm, data);
  350. return NULL;
  351. }
  352. data->ttls_version = 0;
  353. }
  354. if (eap_server_tls_ssl_init(sm, &data->ssl, 0)) {
  355. wpa_printf(MSG_INFO, "EAP-TTLS: Failed to initialize SSL.");
  356. eap_ttls_reset(sm, data);
  357. return NULL;
  358. }
  359. return data;
  360. }
  361. static void eap_ttls_reset(struct eap_sm *sm, void *priv)
  362. {
  363. struct eap_ttls_data *data = priv;
  364. if (data == NULL)
  365. return;
  366. if (data->phase2_priv && data->phase2_method)
  367. data->phase2_method->reset(sm, data->phase2_priv);
  368. eap_server_tls_ssl_deinit(sm, &data->ssl);
  369. wpabuf_free(data->pending_phase2_eap_resp);
  370. os_free(data);
  371. }
  372. static struct wpabuf * eap_ttls_build_start(struct eap_sm *sm,
  373. struct eap_ttls_data *data, u8 id)
  374. {
  375. struct wpabuf *req;
  376. req = eap_msg_alloc(EAP_VENDOR_IETF, EAP_TYPE_TTLS, 1,
  377. EAP_CODE_REQUEST, id);
  378. if (req == NULL) {
  379. wpa_printf(MSG_ERROR, "EAP-TTLS: Failed to allocate memory for"
  380. " request");
  381. eap_ttls_state(data, FAILURE);
  382. return NULL;
  383. }
  384. wpabuf_put_u8(req, EAP_TLS_FLAGS_START | data->ttls_version);
  385. eap_ttls_state(data, PHASE1);
  386. return req;
  387. }
  388. static struct wpabuf * eap_ttls_build_phase2_eap_req(
  389. struct eap_sm *sm, struct eap_ttls_data *data, u8 id)
  390. {
  391. struct wpabuf *buf, *encr_req;
  392. u8 *req;
  393. size_t req_len;
  394. buf = data->phase2_method->buildReq(sm, data->phase2_priv, id);
  395. if (buf == NULL)
  396. return NULL;
  397. wpa_hexdump_buf_key(MSG_DEBUG,
  398. "EAP-TTLS/EAP: Encapsulate Phase 2 data", buf);
  399. buf = eap_ttls_avp_encapsulate(buf, RADIUS_ATTR_EAP_MESSAGE, 1);
  400. if (buf == NULL) {
  401. wpa_printf(MSG_DEBUG, "EAP-TTLS/EAP: Failed to encapsulate "
  402. "packet");
  403. return NULL;
  404. }
  405. req = wpabuf_mhead(buf);
  406. req_len = wpabuf_len(buf);
  407. wpa_hexdump_key(MSG_DEBUG, "EAP-TTLS/EAP: Encrypt encapsulated Phase "
  408. "2 data", req, req_len);
  409. encr_req = eap_server_tls_encrypt(sm, &data->ssl, req, req_len);
  410. wpabuf_free(buf);
  411. return encr_req;
  412. }
  413. static struct wpabuf * eap_ttls_build_phase2_mschapv2(
  414. struct eap_sm *sm, struct eap_ttls_data *data)
  415. {
  416. struct wpabuf *encr_req;
  417. u8 *req, *pos, *end;
  418. int ret;
  419. size_t req_len;
  420. pos = req = os_malloc(100);
  421. if (req == NULL)
  422. return NULL;
  423. end = req + 100;
  424. if (data->mschapv2_resp_ok) {
  425. pos = eap_ttls_avp_hdr(pos, RADIUS_ATTR_MS_CHAP2_SUCCESS,
  426. RADIUS_VENDOR_ID_MICROSOFT, 1, 43);
  427. *pos++ = data->mschapv2_ident;
  428. ret = os_snprintf((char *) pos, end - pos, "S=");
  429. if (ret >= 0 && ret < end - pos)
  430. pos += ret;
  431. pos += wpa_snprintf_hex_uppercase(
  432. (char *) pos, end - pos, data->mschapv2_auth_response,
  433. sizeof(data->mschapv2_auth_response));
  434. } else {
  435. pos = eap_ttls_avp_hdr(pos, RADIUS_ATTR_MS_CHAP_ERROR,
  436. RADIUS_VENDOR_ID_MICROSOFT, 1, 6);
  437. os_memcpy(pos, "Failed", 6);
  438. pos += 6;
  439. AVP_PAD(req, pos);
  440. }
  441. req_len = pos - req;
  442. wpa_hexdump_key(MSG_DEBUG, "EAP-TTLS/MSCHAPV2: Encrypting Phase 2 "
  443. "data", req, req_len);
  444. encr_req = eap_server_tls_encrypt(sm, &data->ssl, req, req_len);
  445. os_free(req);
  446. return encr_req;
  447. }
  448. static struct wpabuf * eap_ttls_build_phase_finished(
  449. struct eap_sm *sm, struct eap_ttls_data *data, int final)
  450. {
  451. int len;
  452. struct wpabuf *req;
  453. const int max_len = 300;
  454. req = wpabuf_alloc(max_len);
  455. if (req == NULL)
  456. return NULL;
  457. len = tls_connection_ia_send_phase_finished(sm->ssl_ctx,
  458. data->ssl.conn, final,
  459. wpabuf_mhead(req),
  460. max_len);
  461. if (len < 0) {
  462. wpabuf_free(req);
  463. return NULL;
  464. }
  465. wpabuf_put(req, len);
  466. return req;
  467. }
  468. static struct wpabuf * eap_ttls_buildReq(struct eap_sm *sm, void *priv, u8 id)
  469. {
  470. struct eap_ttls_data *data = priv;
  471. if (data->ssl.state == FRAG_ACK) {
  472. return eap_server_tls_build_ack(id, EAP_TYPE_TTLS,
  473. data->ttls_version);
  474. }
  475. if (data->ssl.state == WAIT_FRAG_ACK) {
  476. return eap_server_tls_build_msg(&data->ssl, EAP_TYPE_TTLS,
  477. data->ttls_version, id);
  478. }
  479. switch (data->state) {
  480. case START:
  481. return eap_ttls_build_start(sm, data, id);
  482. case PHASE1:
  483. if (tls_connection_established(sm->ssl_ctx, data->ssl.conn)) {
  484. wpa_printf(MSG_DEBUG, "EAP-TTLS: Phase1 done, "
  485. "starting Phase2");
  486. eap_ttls_state(data, PHASE2_START);
  487. }
  488. break;
  489. case PHASE2_METHOD:
  490. wpabuf_free(data->ssl.out_buf);
  491. data->ssl.out_used = 0;
  492. data->ssl.out_buf = eap_ttls_build_phase2_eap_req(sm, data,
  493. id);
  494. break;
  495. case PHASE2_MSCHAPV2_RESP:
  496. wpabuf_free(data->ssl.out_buf);
  497. data->ssl.out_used = 0;
  498. data->ssl.out_buf = eap_ttls_build_phase2_mschapv2(sm, data);
  499. break;
  500. case PHASE_FINISHED:
  501. wpabuf_free(data->ssl.out_buf);
  502. data->ssl.out_used = 0;
  503. data->ssl.out_buf = eap_ttls_build_phase_finished(sm, data, 1);
  504. break;
  505. default:
  506. wpa_printf(MSG_DEBUG, "EAP-TTLS: %s - unexpected state %d",
  507. __func__, data->state);
  508. return NULL;
  509. }
  510. return eap_server_tls_build_msg(&data->ssl, EAP_TYPE_TTLS,
  511. data->ttls_version, id);
  512. }
  513. static Boolean eap_ttls_check(struct eap_sm *sm, void *priv,
  514. struct wpabuf *respData)
  515. {
  516. const u8 *pos;
  517. size_t len;
  518. pos = eap_hdr_validate(EAP_VENDOR_IETF, EAP_TYPE_TTLS, respData, &len);
  519. if (pos == NULL || len < 1) {
  520. wpa_printf(MSG_INFO, "EAP-TTLS: Invalid frame");
  521. return TRUE;
  522. }
  523. return FALSE;
  524. }
  525. static int eap_ttls_ia_permute_inner_secret(struct eap_sm *sm,
  526. struct eap_ttls_data *data,
  527. const u8 *key, size_t key_len)
  528. {
  529. u8 *buf;
  530. size_t buf_len;
  531. int ret;
  532. if (key) {
  533. buf_len = 2 + key_len;
  534. buf = os_malloc(buf_len);
  535. if (buf == NULL)
  536. return -1;
  537. WPA_PUT_BE16(buf, key_len);
  538. os_memcpy(buf + 2, key, key_len);
  539. } else {
  540. buf = NULL;
  541. buf_len = 0;
  542. }
  543. wpa_hexdump_key(MSG_DEBUG, "EAP-TTLS: Session keys for TLS/IA inner "
  544. "secret permutation", buf, buf_len);
  545. ret = tls_connection_ia_permute_inner_secret(sm->ssl_ctx,
  546. data->ssl.conn,
  547. buf, buf_len);
  548. os_free(buf);
  549. return ret;
  550. }
  551. static void eap_ttls_process_phase2_pap(struct eap_sm *sm,
  552. struct eap_ttls_data *data,
  553. const u8 *user_password,
  554. size_t user_password_len)
  555. {
  556. if (!sm->user || !sm->user->password || sm->user->password_hash ||
  557. !(sm->user->ttls_auth & EAP_TTLS_AUTH_PAP)) {
  558. wpa_printf(MSG_DEBUG, "EAP-TTLS/PAP: No plaintext user "
  559. "password configured");
  560. eap_ttls_state(data, FAILURE);
  561. return;
  562. }
  563. if (sm->user->password_len != user_password_len ||
  564. os_memcmp(sm->user->password, user_password, user_password_len) !=
  565. 0) {
  566. wpa_printf(MSG_DEBUG, "EAP-TTLS/PAP: Invalid user password");
  567. eap_ttls_state(data, FAILURE);
  568. return;
  569. }
  570. wpa_printf(MSG_DEBUG, "EAP-TTLS/PAP: Correct user password");
  571. eap_ttls_state(data, data->ttls_version > 0 ? PHASE_FINISHED :
  572. SUCCESS);
  573. }
  574. static void eap_ttls_process_phase2_chap(struct eap_sm *sm,
  575. struct eap_ttls_data *data,
  576. const u8 *challenge,
  577. size_t challenge_len,
  578. const u8 *password,
  579. size_t password_len)
  580. {
  581. u8 *chal, hash[CHAP_MD5_LEN];
  582. if (challenge == NULL || password == NULL ||
  583. challenge_len != EAP_TTLS_CHAP_CHALLENGE_LEN ||
  584. password_len != 1 + EAP_TTLS_CHAP_PASSWORD_LEN) {
  585. wpa_printf(MSG_DEBUG, "EAP-TTLS/CHAP: Invalid CHAP attributes "
  586. "(challenge len %lu password len %lu)",
  587. (unsigned long) challenge_len,
  588. (unsigned long) password_len);
  589. eap_ttls_state(data, FAILURE);
  590. return;
  591. }
  592. if (!sm->user || !sm->user->password || sm->user->password_hash ||
  593. !(sm->user->ttls_auth & EAP_TTLS_AUTH_CHAP)) {
  594. wpa_printf(MSG_DEBUG, "EAP-TTLS/CHAP: No plaintext user "
  595. "password configured");
  596. eap_ttls_state(data, FAILURE);
  597. return;
  598. }
  599. chal = eap_ttls_implicit_challenge(sm, data,
  600. EAP_TTLS_CHAP_CHALLENGE_LEN + 1);
  601. if (chal == NULL) {
  602. wpa_printf(MSG_DEBUG, "EAP-TTLS/CHAP: Failed to generate "
  603. "challenge from TLS data");
  604. eap_ttls_state(data, FAILURE);
  605. return;
  606. }
  607. if (os_memcmp(challenge, chal, EAP_TTLS_CHAP_CHALLENGE_LEN) != 0 ||
  608. password[0] != chal[EAP_TTLS_CHAP_CHALLENGE_LEN]) {
  609. wpa_printf(MSG_DEBUG, "EAP-TTLS/CHAP: Challenge mismatch");
  610. os_free(chal);
  611. eap_ttls_state(data, FAILURE);
  612. return;
  613. }
  614. os_free(chal);
  615. /* MD5(Ident + Password + Challenge) */
  616. chap_md5(password[0], sm->user->password, sm->user->password_len,
  617. challenge, challenge_len, hash);
  618. if (os_memcmp(hash, password + 1, EAP_TTLS_CHAP_PASSWORD_LEN) == 0) {
  619. wpa_printf(MSG_DEBUG, "EAP-TTLS/CHAP: Correct user password");
  620. eap_ttls_state(data, data->ttls_version > 0 ? PHASE_FINISHED :
  621. SUCCESS);
  622. } else {
  623. wpa_printf(MSG_DEBUG, "EAP-TTLS/CHAP: Invalid user password");
  624. eap_ttls_state(data, FAILURE);
  625. }
  626. }
  627. static void eap_ttls_process_phase2_mschap(struct eap_sm *sm,
  628. struct eap_ttls_data *data,
  629. u8 *challenge, size_t challenge_len,
  630. u8 *response, size_t response_len)
  631. {
  632. u8 *chal, nt_response[24];
  633. if (challenge == NULL || response == NULL ||
  634. challenge_len != EAP_TTLS_MSCHAP_CHALLENGE_LEN ||
  635. response_len != EAP_TTLS_MSCHAP_RESPONSE_LEN) {
  636. wpa_printf(MSG_DEBUG, "EAP-TTLS/MSCHAP: Invalid MS-CHAP "
  637. "attributes (challenge len %lu response len %lu)",
  638. (unsigned long) challenge_len,
  639. (unsigned long) response_len);
  640. eap_ttls_state(data, FAILURE);
  641. return;
  642. }
  643. if (!sm->user || !sm->user->password ||
  644. !(sm->user->ttls_auth & EAP_TTLS_AUTH_MSCHAP)) {
  645. wpa_printf(MSG_DEBUG, "EAP-TTLS/MSCHAP: No user password "
  646. "configured");
  647. eap_ttls_state(data, FAILURE);
  648. return;
  649. }
  650. chal = eap_ttls_implicit_challenge(sm, data,
  651. EAP_TTLS_MSCHAP_CHALLENGE_LEN + 1);
  652. if (chal == NULL) {
  653. wpa_printf(MSG_DEBUG, "EAP-TTLS/MSCHAP: Failed to generate "
  654. "challenge from TLS data");
  655. eap_ttls_state(data, FAILURE);
  656. return;
  657. }
  658. if (os_memcmp(challenge, chal, EAP_TTLS_MSCHAP_CHALLENGE_LEN) != 0 ||
  659. response[0] != chal[EAP_TTLS_MSCHAP_CHALLENGE_LEN]) {
  660. wpa_printf(MSG_DEBUG, "EAP-TTLS/MSCHAP: Challenge mismatch");
  661. os_free(chal);
  662. eap_ttls_state(data, FAILURE);
  663. return;
  664. }
  665. os_free(chal);
  666. if (sm->user->password_hash)
  667. challenge_response(challenge, sm->user->password, nt_response);
  668. else
  669. nt_challenge_response(challenge, sm->user->password,
  670. sm->user->password_len, nt_response);
  671. if (os_memcmp(nt_response, response + 2 + 24, 24) == 0) {
  672. wpa_printf(MSG_DEBUG, "EAP-TTLS/MSCHAP: Correct response");
  673. eap_ttls_state(data, data->ttls_version > 0 ? PHASE_FINISHED :
  674. SUCCESS);
  675. } else {
  676. wpa_printf(MSG_DEBUG, "EAP-TTLS/MSCHAP: Invalid NT-Response");
  677. wpa_hexdump(MSG_MSGDUMP, "EAP-TTLS/MSCHAP: Received",
  678. response + 2 + 24, 24);
  679. wpa_hexdump(MSG_MSGDUMP, "EAP-TTLS/MSCHAP: Expected",
  680. nt_response, 24);
  681. eap_ttls_state(data, FAILURE);
  682. }
  683. }
  684. static void eap_ttls_process_phase2_mschapv2(struct eap_sm *sm,
  685. struct eap_ttls_data *data,
  686. u8 *challenge,
  687. size_t challenge_len,
  688. u8 *response, size_t response_len)
  689. {
  690. u8 *chal, *username, nt_response[24], *rx_resp, *peer_challenge,
  691. *auth_challenge;
  692. size_t username_len, i;
  693. if (challenge == NULL || response == NULL ||
  694. challenge_len != EAP_TTLS_MSCHAPV2_CHALLENGE_LEN ||
  695. response_len != EAP_TTLS_MSCHAPV2_RESPONSE_LEN) {
  696. wpa_printf(MSG_DEBUG, "EAP-TTLS/MSCHAPV2: Invalid MS-CHAP2 "
  697. "attributes (challenge len %lu response len %lu)",
  698. (unsigned long) challenge_len,
  699. (unsigned long) response_len);
  700. eap_ttls_state(data, FAILURE);
  701. return;
  702. }
  703. if (!sm->user || !sm->user->password ||
  704. !(sm->user->ttls_auth & EAP_TTLS_AUTH_MSCHAPV2)) {
  705. wpa_printf(MSG_DEBUG, "EAP-TTLS/MSCHAPV2: No user password "
  706. "configured");
  707. eap_ttls_state(data, FAILURE);
  708. return;
  709. }
  710. /* MSCHAPv2 does not include optional domain name in the
  711. * challenge-response calculation, so remove domain prefix
  712. * (if present). */
  713. username = sm->identity;
  714. username_len = sm->identity_len;
  715. for (i = 0; i < username_len; i++) {
  716. if (username[i] == '\\') {
  717. username_len -= i + 1;
  718. username += i + 1;
  719. break;
  720. }
  721. }
  722. chal = eap_ttls_implicit_challenge(
  723. sm, data, EAP_TTLS_MSCHAPV2_CHALLENGE_LEN + 1);
  724. if (chal == NULL) {
  725. wpa_printf(MSG_DEBUG, "EAP-TTLS/MSCHAPV2: Failed to generate "
  726. "challenge from TLS data");
  727. eap_ttls_state(data, FAILURE);
  728. return;
  729. }
  730. if (os_memcmp(challenge, chal, EAP_TTLS_MSCHAPV2_CHALLENGE_LEN) != 0 ||
  731. response[0] != chal[EAP_TTLS_MSCHAPV2_CHALLENGE_LEN]) {
  732. wpa_printf(MSG_DEBUG, "EAP-TTLS/MSCHAPV2: Challenge mismatch");
  733. os_free(chal);
  734. eap_ttls_state(data, FAILURE);
  735. return;
  736. }
  737. os_free(chal);
  738. auth_challenge = challenge;
  739. peer_challenge = response + 2;
  740. wpa_hexdump_ascii(MSG_MSGDUMP, "EAP-TTLS/MSCHAPV2: User",
  741. username, username_len);
  742. wpa_hexdump(MSG_MSGDUMP, "EAP-TTLS/MSCHAPV2: auth_challenge",
  743. auth_challenge, EAP_TTLS_MSCHAPV2_CHALLENGE_LEN);
  744. wpa_hexdump(MSG_MSGDUMP, "EAP-TTLS/MSCHAPV2: peer_challenge",
  745. peer_challenge, EAP_TTLS_MSCHAPV2_CHALLENGE_LEN);
  746. if (sm->user->password_hash) {
  747. generate_nt_response_pwhash(auth_challenge, peer_challenge,
  748. username, username_len,
  749. sm->user->password,
  750. nt_response);
  751. } else {
  752. generate_nt_response(auth_challenge, peer_challenge,
  753. username, username_len,
  754. sm->user->password,
  755. sm->user->password_len,
  756. nt_response);
  757. }
  758. rx_resp = response + 2 + EAP_TTLS_MSCHAPV2_CHALLENGE_LEN + 8;
  759. if (os_memcmp(nt_response, rx_resp, 24) == 0) {
  760. wpa_printf(MSG_DEBUG, "EAP-TTLS/MSCHAPV2: Correct "
  761. "NT-Response");
  762. data->mschapv2_resp_ok = 1;
  763. if (data->ttls_version > 0) {
  764. const u8 *pw_hash;
  765. u8 pw_hash_buf[16], pw_hash_hash[16], master_key[16];
  766. u8 session_key[2 * MSCHAPV2_KEY_LEN];
  767. if (sm->user->password_hash)
  768. pw_hash = sm->user->password;
  769. else {
  770. nt_password_hash(sm->user->password,
  771. sm->user->password_len,
  772. pw_hash_buf);
  773. pw_hash = pw_hash_buf;
  774. }
  775. hash_nt_password_hash(pw_hash, pw_hash_hash);
  776. get_master_key(pw_hash_hash, nt_response, master_key);
  777. get_asymetric_start_key(master_key, session_key,
  778. MSCHAPV2_KEY_LEN, 0, 0);
  779. get_asymetric_start_key(master_key,
  780. session_key + MSCHAPV2_KEY_LEN,
  781. MSCHAPV2_KEY_LEN, 1, 0);
  782. eap_ttls_ia_permute_inner_secret(sm, data,
  783. session_key,
  784. sizeof(session_key));
  785. }
  786. if (sm->user->password_hash) {
  787. generate_authenticator_response_pwhash(
  788. sm->user->password,
  789. peer_challenge, auth_challenge,
  790. username, username_len, nt_response,
  791. data->mschapv2_auth_response);
  792. } else {
  793. generate_authenticator_response(
  794. sm->user->password, sm->user->password_len,
  795. peer_challenge, auth_challenge,
  796. username, username_len, nt_response,
  797. data->mschapv2_auth_response);
  798. }
  799. } else {
  800. wpa_printf(MSG_DEBUG, "EAP-TTLS/MSCHAPV2: Invalid "
  801. "NT-Response");
  802. wpa_hexdump(MSG_MSGDUMP, "EAP-TTLS/MSCHAPV2: Received",
  803. rx_resp, 24);
  804. wpa_hexdump(MSG_MSGDUMP, "EAP-TTLS/MSCHAPV2: Expected",
  805. nt_response, 24);
  806. data->mschapv2_resp_ok = 0;
  807. }
  808. eap_ttls_state(data, PHASE2_MSCHAPV2_RESP);
  809. data->mschapv2_ident = response[0];
  810. }
  811. static int eap_ttls_phase2_eap_init(struct eap_sm *sm,
  812. struct eap_ttls_data *data,
  813. EapType eap_type)
  814. {
  815. if (data->phase2_priv && data->phase2_method) {
  816. data->phase2_method->reset(sm, data->phase2_priv);
  817. data->phase2_method = NULL;
  818. data->phase2_priv = NULL;
  819. }
  820. data->phase2_method = eap_server_get_eap_method(EAP_VENDOR_IETF,
  821. eap_type);
  822. if (!data->phase2_method)
  823. return -1;
  824. sm->init_phase2 = 1;
  825. data->phase2_priv = data->phase2_method->init(sm);
  826. sm->init_phase2 = 0;
  827. return data->phase2_priv == NULL ? -1 : 0;
  828. }
  829. static void eap_ttls_process_phase2_eap_response(struct eap_sm *sm,
  830. struct eap_ttls_data *data,
  831. u8 *in_data, size_t in_len)
  832. {
  833. u8 next_type = EAP_TYPE_NONE;
  834. struct eap_hdr *hdr;
  835. u8 *pos;
  836. size_t left;
  837. struct wpabuf buf;
  838. const struct eap_method *m = data->phase2_method;
  839. void *priv = data->phase2_priv;
  840. if (priv == NULL) {
  841. wpa_printf(MSG_DEBUG, "EAP-TTLS/EAP: %s - Phase2 not "
  842. "initialized?!", __func__);
  843. return;
  844. }
  845. hdr = (struct eap_hdr *) in_data;
  846. pos = (u8 *) (hdr + 1);
  847. if (in_len > sizeof(*hdr) && *pos == EAP_TYPE_NAK) {
  848. left = in_len - sizeof(*hdr);
  849. wpa_hexdump(MSG_DEBUG, "EAP-TTLS/EAP: Phase2 type Nak'ed; "
  850. "allowed types", pos + 1, left - 1);
  851. eap_sm_process_nak(sm, pos + 1, left - 1);
  852. if (sm->user && sm->user_eap_method_index < EAP_MAX_METHODS &&
  853. sm->user->methods[sm->user_eap_method_index].method !=
  854. EAP_TYPE_NONE) {
  855. next_type = sm->user->methods[
  856. sm->user_eap_method_index++].method;
  857. wpa_printf(MSG_DEBUG, "EAP-TTLS: try EAP type %d",
  858. next_type);
  859. if (eap_ttls_phase2_eap_init(sm, data, next_type)) {
  860. wpa_printf(MSG_DEBUG, "EAP-TTLS: Failed to "
  861. "initialize EAP type %d",
  862. next_type);
  863. eap_ttls_state(data, FAILURE);
  864. return;
  865. }
  866. } else {
  867. eap_ttls_state(data, FAILURE);
  868. }
  869. return;
  870. }
  871. wpabuf_set(&buf, in_data, in_len);
  872. if (m->check(sm, priv, &buf)) {
  873. wpa_printf(MSG_DEBUG, "EAP-TTLS/EAP: Phase2 check() asked to "
  874. "ignore the packet");
  875. return;
  876. }
  877. m->process(sm, priv, &buf);
  878. if (sm->method_pending == METHOD_PENDING_WAIT) {
  879. wpa_printf(MSG_DEBUG, "EAP-TTLS/EAP: Phase2 method is in "
  880. "pending wait state - save decrypted response");
  881. wpabuf_free(data->pending_phase2_eap_resp);
  882. data->pending_phase2_eap_resp = wpabuf_dup(&buf);
  883. }
  884. if (!m->isDone(sm, priv))
  885. return;
  886. if (!m->isSuccess(sm, priv)) {
  887. wpa_printf(MSG_DEBUG, "EAP-TTLS/EAP: Phase2 method failed");
  888. eap_ttls_state(data, FAILURE);
  889. return;
  890. }
  891. switch (data->state) {
  892. case PHASE2_START:
  893. if (eap_user_get(sm, sm->identity, sm->identity_len, 1) != 0) {
  894. wpa_hexdump_ascii(MSG_DEBUG, "EAP_TTLS: Phase2 "
  895. "Identity not found in the user "
  896. "database",
  897. sm->identity, sm->identity_len);
  898. eap_ttls_state(data, FAILURE);
  899. break;
  900. }
  901. eap_ttls_state(data, PHASE2_METHOD);
  902. next_type = sm->user->methods[0].method;
  903. sm->user_eap_method_index = 1;
  904. wpa_printf(MSG_DEBUG, "EAP-TTLS: try EAP type %d", next_type);
  905. if (eap_ttls_phase2_eap_init(sm, data, next_type)) {
  906. wpa_printf(MSG_DEBUG, "EAP-TTLS: Failed to initialize "
  907. "EAP type %d", next_type);
  908. eap_ttls_state(data, FAILURE);
  909. }
  910. break;
  911. case PHASE2_METHOD:
  912. if (data->ttls_version > 0) {
  913. if (m->getKey) {
  914. u8 *key;
  915. size_t key_len;
  916. key = m->getKey(sm, priv, &key_len);
  917. eap_ttls_ia_permute_inner_secret(sm, data,
  918. key, key_len);
  919. }
  920. eap_ttls_state(data, PHASE_FINISHED);
  921. } else
  922. eap_ttls_state(data, SUCCESS);
  923. break;
  924. case FAILURE:
  925. break;
  926. default:
  927. wpa_printf(MSG_DEBUG, "EAP-TTLS: %s - unexpected state %d",
  928. __func__, data->state);
  929. break;
  930. }
  931. }
  932. static void eap_ttls_process_phase2_eap(struct eap_sm *sm,
  933. struct eap_ttls_data *data,
  934. const u8 *eap, size_t eap_len)
  935. {
  936. struct eap_hdr *hdr;
  937. size_t len;
  938. if (data->state == PHASE2_START) {
  939. wpa_printf(MSG_DEBUG, "EAP-TTLS/EAP: initializing Phase 2");
  940. if (eap_ttls_phase2_eap_init(sm, data, EAP_TYPE_IDENTITY) < 0)
  941. {
  942. wpa_printf(MSG_DEBUG, "EAP-TTLS/EAP: failed to "
  943. "initialize EAP-Identity");
  944. return;
  945. }
  946. }
  947. if (eap_len < sizeof(*hdr)) {
  948. wpa_printf(MSG_DEBUG, "EAP-TTLS/EAP: too short Phase 2 EAP "
  949. "packet (len=%lu)", (unsigned long) eap_len);
  950. return;
  951. }
  952. hdr = (struct eap_hdr *) eap;
  953. len = be_to_host16(hdr->length);
  954. wpa_printf(MSG_DEBUG, "EAP-TTLS/EAP: received Phase 2 EAP: code=%d "
  955. "identifier=%d length=%lu", hdr->code, hdr->identifier,
  956. (unsigned long) len);
  957. if (len > eap_len) {
  958. wpa_printf(MSG_INFO, "EAP-TTLS/EAP: Length mismatch in Phase 2"
  959. " EAP frame (hdr len=%lu, data len in AVP=%lu)",
  960. (unsigned long) len, (unsigned long) eap_len);
  961. return;
  962. }
  963. switch (hdr->code) {
  964. case EAP_CODE_RESPONSE:
  965. eap_ttls_process_phase2_eap_response(sm, data, (u8 *) hdr,
  966. len);
  967. break;
  968. default:
  969. wpa_printf(MSG_INFO, "EAP-TTLS/EAP: Unexpected code=%d in "
  970. "Phase 2 EAP header", hdr->code);
  971. break;
  972. }
  973. }
  974. static void eap_ttls_process_phase2(struct eap_sm *sm,
  975. struct eap_ttls_data *data,
  976. struct wpabuf *in_buf)
  977. {
  978. u8 *in_decrypted;
  979. int len_decrypted;
  980. struct eap_ttls_avp parse;
  981. size_t buf_len;
  982. u8 *in_data;
  983. size_t in_len;
  984. in_data = wpabuf_mhead(in_buf);
  985. in_len = wpabuf_len(in_buf);
  986. wpa_printf(MSG_DEBUG, "EAP-TTLS: received %lu bytes encrypted data for"
  987. " Phase 2", (unsigned long) in_len);
  988. if (data->pending_phase2_eap_resp) {
  989. wpa_printf(MSG_DEBUG, "EAP-TTLS: Pending Phase 2 EAP response "
  990. "- skip decryption and use old data");
  991. eap_ttls_process_phase2_eap(
  992. sm, data, wpabuf_head(data->pending_phase2_eap_resp),
  993. wpabuf_len(data->pending_phase2_eap_resp));
  994. wpabuf_free(data->pending_phase2_eap_resp);
  995. data->pending_phase2_eap_resp = NULL;
  996. return;
  997. }
  998. buf_len = in_len;
  999. /*
  1000. * Even though we try to disable TLS compression, it is possible that
  1001. * this cannot be done with all TLS libraries. Add extra buffer space
  1002. * to handle the possibility of the decrypted data being longer than
  1003. * input data.
  1004. */
  1005. buf_len += 500;
  1006. buf_len *= 3;
  1007. in_decrypted = os_malloc(buf_len);
  1008. if (in_decrypted == NULL) {
  1009. wpa_printf(MSG_WARNING, "EAP-TTLS: failed to allocate memory "
  1010. "for decryption");
  1011. return;
  1012. }
  1013. len_decrypted = tls_connection_decrypt(sm->ssl_ctx, data->ssl.conn,
  1014. in_data, in_len,
  1015. in_decrypted, buf_len);
  1016. if (len_decrypted < 0) {
  1017. wpa_printf(MSG_INFO, "EAP-TTLS: Failed to decrypt Phase 2 "
  1018. "data");
  1019. os_free(in_decrypted);
  1020. eap_ttls_state(data, FAILURE);
  1021. return;
  1022. }
  1023. if (data->state == PHASE_FINISHED) {
  1024. if (len_decrypted == 0 &&
  1025. tls_connection_ia_final_phase_finished(sm->ssl_ctx,
  1026. data->ssl.conn)) {
  1027. wpa_printf(MSG_DEBUG, "EAP-TTLS: FinalPhaseFinished "
  1028. "received");
  1029. eap_ttls_state(data, SUCCESS);
  1030. } else {
  1031. wpa_printf(MSG_INFO, "EAP-TTLS: Did not receive valid "
  1032. "FinalPhaseFinished");
  1033. eap_ttls_state(data, FAILURE);
  1034. }
  1035. os_free(in_decrypted);
  1036. return;
  1037. }
  1038. wpa_hexdump_key(MSG_DEBUG, "EAP-TTLS: Decrypted Phase 2 EAP",
  1039. in_decrypted, len_decrypted);
  1040. if (eap_ttls_avp_parse(in_decrypted, len_decrypted, &parse) < 0) {
  1041. wpa_printf(MSG_DEBUG, "EAP-TTLS: Failed to parse AVPs");
  1042. os_free(in_decrypted);
  1043. eap_ttls_state(data, FAILURE);
  1044. return;
  1045. }
  1046. if (parse.user_name) {
  1047. os_free(sm->identity);
  1048. sm->identity = os_malloc(parse.user_name_len);
  1049. if (sm->identity) {
  1050. os_memcpy(sm->identity, parse.user_name,
  1051. parse.user_name_len);
  1052. sm->identity_len = parse.user_name_len;
  1053. }
  1054. if (eap_user_get(sm, parse.user_name, parse.user_name_len, 1)
  1055. != 0) {
  1056. wpa_printf(MSG_DEBUG, "EAP-TTLS: Phase2 Identity not "
  1057. "found in the user database");
  1058. eap_ttls_state(data, FAILURE);
  1059. goto done;
  1060. }
  1061. }
  1062. #ifdef EAP_SERVER_TNC
  1063. if (data->tnc_started && parse.eap == NULL) {
  1064. wpa_printf(MSG_DEBUG, "EAP-TTLS: TNC started but no EAP "
  1065. "response from peer");
  1066. eap_ttls_state(data, FAILURE);
  1067. goto done;
  1068. }
  1069. #endif /* EAP_SERVER_TNC */
  1070. if (parse.eap) {
  1071. eap_ttls_process_phase2_eap(sm, data, parse.eap,
  1072. parse.eap_len);
  1073. } else if (parse.user_password) {
  1074. eap_ttls_process_phase2_pap(sm, data, parse.user_password,
  1075. parse.user_password_len);
  1076. } else if (parse.chap_password) {
  1077. eap_ttls_process_phase2_chap(sm, data,
  1078. parse.chap_challenge,
  1079. parse.chap_challenge_len,
  1080. parse.chap_password,
  1081. parse.chap_password_len);
  1082. } else if (parse.mschap_response) {
  1083. eap_ttls_process_phase2_mschap(sm, data,
  1084. parse.mschap_challenge,
  1085. parse.mschap_challenge_len,
  1086. parse.mschap_response,
  1087. parse.mschap_response_len);
  1088. } else if (parse.mschap2_response) {
  1089. eap_ttls_process_phase2_mschapv2(sm, data,
  1090. parse.mschap_challenge,
  1091. parse.mschap_challenge_len,
  1092. parse.mschap2_response,
  1093. parse.mschap2_response_len);
  1094. }
  1095. done:
  1096. os_free(in_decrypted);
  1097. os_free(parse.eap);
  1098. }
  1099. static void eap_ttls_start_tnc(struct eap_sm *sm, struct eap_ttls_data *data)
  1100. {
  1101. #ifdef EAP_SERVER_TNC
  1102. if (!sm->tnc || data->state != SUCCESS || data->tnc_started)
  1103. return;
  1104. wpa_printf(MSG_DEBUG, "EAP-TTLS: Initialize TNC");
  1105. if (eap_ttls_phase2_eap_init(sm, data, EAP_TYPE_TNC)) {
  1106. wpa_printf(MSG_DEBUG, "EAP-TTLS: Failed to initialize TNC");
  1107. eap_ttls_state(data, FAILURE);
  1108. return;
  1109. }
  1110. data->tnc_started = 1;
  1111. eap_ttls_state(data, PHASE2_METHOD);
  1112. #endif /* EAP_SERVER_TNC */
  1113. }
  1114. static int eap_ttls_process_version(struct eap_sm *sm, void *priv,
  1115. int peer_version)
  1116. {
  1117. struct eap_ttls_data *data = priv;
  1118. if (peer_version < data->ttls_version) {
  1119. wpa_printf(MSG_DEBUG, "EAP-TTLS: peer ver=%d, own ver=%d; "
  1120. "use version %d",
  1121. peer_version, data->ttls_version, peer_version);
  1122. data->ttls_version = peer_version;
  1123. }
  1124. if (data->ttls_version > 0 && !data->tls_ia_configured) {
  1125. if (tls_connection_set_ia(sm->ssl_ctx, data->ssl.conn, 1)) {
  1126. wpa_printf(MSG_INFO, "EAP-TTLS: Failed to enable "
  1127. "TLS/IA");
  1128. return -1;
  1129. }
  1130. data->tls_ia_configured = 1;
  1131. }
  1132. return 0;
  1133. }
  1134. static void eap_ttls_process_msg(struct eap_sm *sm, void *priv,
  1135. const struct wpabuf *respData)
  1136. {
  1137. struct eap_ttls_data *data = priv;
  1138. switch (data->state) {
  1139. case PHASE1:
  1140. if (eap_server_tls_phase1(sm, &data->ssl) < 0)
  1141. eap_ttls_state(data, FAILURE);
  1142. break;
  1143. case PHASE2_START:
  1144. case PHASE2_METHOD:
  1145. case PHASE_FINISHED:
  1146. eap_ttls_process_phase2(sm, data, data->ssl.in_buf);
  1147. eap_ttls_start_tnc(sm, data);
  1148. break;
  1149. case PHASE2_MSCHAPV2_RESP:
  1150. if (data->mschapv2_resp_ok && wpabuf_len(data->ssl.in_buf) ==
  1151. 0) {
  1152. wpa_printf(MSG_DEBUG, "EAP-TTLS/MSCHAPV2: Peer "
  1153. "acknowledged response");
  1154. eap_ttls_state(data, data->ttls_version > 0 ?
  1155. PHASE_FINISHED : SUCCESS);
  1156. } else if (!data->mschapv2_resp_ok) {
  1157. wpa_printf(MSG_DEBUG, "EAP-TTLS/MSCHAPV2: Peer "
  1158. "acknowledged error");
  1159. eap_ttls_state(data, FAILURE);
  1160. } else {
  1161. wpa_printf(MSG_DEBUG, "EAP-TTLS/MSCHAPV2: Unexpected "
  1162. "frame from peer (payload len %lu, "
  1163. "expected empty frame)",
  1164. (unsigned long)
  1165. wpabuf_len(data->ssl.in_buf));
  1166. eap_ttls_state(data, FAILURE);
  1167. }
  1168. eap_ttls_start_tnc(sm, data);
  1169. break;
  1170. default:
  1171. wpa_printf(MSG_DEBUG, "EAP-TTLS: Unexpected state %d in %s",
  1172. data->state, __func__);
  1173. break;
  1174. }
  1175. }
  1176. static void eap_ttls_process(struct eap_sm *sm, void *priv,
  1177. struct wpabuf *respData)
  1178. {
  1179. struct eap_ttls_data *data = priv;
  1180. if (eap_server_tls_process(sm, &data->ssl, respData, data,
  1181. EAP_TYPE_TTLS, eap_ttls_process_version,
  1182. eap_ttls_process_msg) < 0)
  1183. eap_ttls_state(data, FAILURE);
  1184. }
  1185. static Boolean eap_ttls_isDone(struct eap_sm *sm, void *priv)
  1186. {
  1187. struct eap_ttls_data *data = priv;
  1188. return data->state == SUCCESS || data->state == FAILURE;
  1189. }
  1190. static u8 * eap_ttls_v1_derive_key(struct eap_sm *sm,
  1191. struct eap_ttls_data *data)
  1192. {
  1193. struct tls_keys keys;
  1194. u8 *rnd, *key;
  1195. os_memset(&keys, 0, sizeof(keys));
  1196. if (tls_connection_get_keys(sm->ssl_ctx, data->ssl.conn, &keys) ||
  1197. keys.client_random == NULL || keys.server_random == NULL ||
  1198. keys.inner_secret == NULL) {
  1199. wpa_printf(MSG_INFO, "EAP-TTLS: Could not get inner secret, "
  1200. "client random, or server random to derive keying "
  1201. "material");
  1202. return NULL;
  1203. }
  1204. rnd = os_malloc(keys.client_random_len + keys.server_random_len);
  1205. key = os_malloc(EAP_TLS_KEY_LEN);
  1206. if (rnd == NULL || key == NULL) {
  1207. wpa_printf(MSG_INFO, "EAP-TTLS: No memory for key derivation");
  1208. os_free(rnd);
  1209. os_free(key);
  1210. return NULL;
  1211. }
  1212. os_memcpy(rnd, keys.client_random, keys.client_random_len);
  1213. os_memcpy(rnd + keys.client_random_len, keys.server_random,
  1214. keys.server_random_len);
  1215. if (tls_prf(keys.inner_secret, keys.inner_secret_len,
  1216. "ttls v1 keying material", rnd, keys.client_random_len +
  1217. keys.server_random_len, key, EAP_TLS_KEY_LEN)) {
  1218. wpa_printf(MSG_DEBUG, "EAP-TTLS: Failed to derive key");
  1219. os_free(rnd);
  1220. os_free(key);
  1221. return NULL;
  1222. }
  1223. wpa_hexdump(MSG_DEBUG, "EAP-TTLS: client/server random",
  1224. rnd, keys.client_random_len + keys.server_random_len);
  1225. wpa_hexdump_key(MSG_DEBUG, "EAP-TTLS: TLS/IA inner secret",
  1226. keys.inner_secret, keys.inner_secret_len);
  1227. os_free(rnd);
  1228. return key;
  1229. }
  1230. static u8 * eap_ttls_getKey(struct eap_sm *sm, void *priv, size_t *len)
  1231. {
  1232. struct eap_ttls_data *data = priv;
  1233. u8 *eapKeyData;
  1234. if (data->state != SUCCESS)
  1235. return NULL;
  1236. if (data->ttls_version == 0) {
  1237. eapKeyData = eap_server_tls_derive_key(sm, &data->ssl,
  1238. "ttls keying material",
  1239. EAP_TLS_KEY_LEN);
  1240. } else {
  1241. eapKeyData = eap_ttls_v1_derive_key(sm, data);
  1242. }
  1243. if (eapKeyData) {
  1244. *len = EAP_TLS_KEY_LEN;
  1245. wpa_hexdump_key(MSG_DEBUG, "EAP-TTLS: Derived key",
  1246. eapKeyData, EAP_TLS_KEY_LEN);
  1247. } else {
  1248. wpa_printf(MSG_DEBUG, "EAP-TTLS: Failed to derive key");
  1249. }
  1250. return eapKeyData;
  1251. }
  1252. static Boolean eap_ttls_isSuccess(struct eap_sm *sm, void *priv)
  1253. {
  1254. struct eap_ttls_data *data = priv;
  1255. return data->state == SUCCESS;
  1256. }
  1257. int eap_server_ttls_register(void)
  1258. {
  1259. struct eap_method *eap;
  1260. int ret;
  1261. eap = eap_server_method_alloc(EAP_SERVER_METHOD_INTERFACE_VERSION,
  1262. EAP_VENDOR_IETF, EAP_TYPE_TTLS, "TTLS");
  1263. if (eap == NULL)
  1264. return -1;
  1265. eap->init = eap_ttls_init;
  1266. eap->reset = eap_ttls_reset;
  1267. eap->buildReq = eap_ttls_buildReq;
  1268. eap->check = eap_ttls_check;
  1269. eap->process = eap_ttls_process;
  1270. eap->isDone = eap_ttls_isDone;
  1271. eap->getKey = eap_ttls_getKey;
  1272. eap->isSuccess = eap_ttls_isSuccess;
  1273. ret = eap_server_method_register(eap);
  1274. if (ret)
  1275. eap_server_method_free(eap);
  1276. return ret;
  1277. }