eap_server_ttls.c 36 KB

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