eap_ttls.c 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813
  1. /*
  2. * EAP peer method: EAP-TTLS (RFC 5281)
  3. * Copyright (c) 2004-2015, 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_common/chap.h"
  14. #include "eap_common/eap_ttls.h"
  15. #include "mschapv2.h"
  16. #include "eap_i.h"
  17. #include "eap_tls_common.h"
  18. #include "eap_config.h"
  19. #define EAP_TTLS_VERSION 0
  20. static void eap_ttls_deinit(struct eap_sm *sm, void *priv);
  21. struct eap_ttls_data {
  22. struct eap_ssl_data ssl;
  23. int ttls_version;
  24. const struct eap_method *phase2_method;
  25. void *phase2_priv;
  26. int phase2_success;
  27. int phase2_start;
  28. EapDecision decision_succ;
  29. enum phase2_types {
  30. EAP_TTLS_PHASE2_EAP,
  31. EAP_TTLS_PHASE2_MSCHAPV2,
  32. EAP_TTLS_PHASE2_MSCHAP,
  33. EAP_TTLS_PHASE2_PAP,
  34. EAP_TTLS_PHASE2_CHAP
  35. } phase2_type;
  36. struct eap_method_type phase2_eap_type;
  37. struct eap_method_type *phase2_eap_types;
  38. size_t num_phase2_eap_types;
  39. u8 auth_response[MSCHAPV2_AUTH_RESPONSE_LEN];
  40. int auth_response_valid;
  41. u8 master_key[MSCHAPV2_MASTER_KEY_LEN]; /* MSCHAPv2 master key */
  42. u8 ident;
  43. int resuming; /* starting a resumed session */
  44. int reauth; /* reauthentication */
  45. u8 *key_data;
  46. u8 *session_id;
  47. size_t id_len;
  48. struct wpabuf *pending_phase2_req;
  49. struct wpabuf *pending_resp;
  50. #ifdef EAP_TNC
  51. int ready_for_tnc;
  52. int tnc_started;
  53. #endif /* EAP_TNC */
  54. };
  55. static void * eap_ttls_init(struct eap_sm *sm)
  56. {
  57. struct eap_ttls_data *data;
  58. struct eap_peer_config *config = eap_get_config(sm);
  59. int selected_non_eap;
  60. char *selected;
  61. data = os_zalloc(sizeof(*data));
  62. if (data == NULL)
  63. return NULL;
  64. data->ttls_version = EAP_TTLS_VERSION;
  65. selected = "EAP";
  66. selected_non_eap = 0;
  67. data->phase2_type = EAP_TTLS_PHASE2_EAP;
  68. /*
  69. * Either one auth= type or one or more autheap= methods can be
  70. * specified.
  71. */
  72. if (config && config->phase2) {
  73. const char *token, *last = NULL;
  74. while ((token = cstr_token(config->phase2, " \t", &last))) {
  75. if (os_strncmp(token, "auth=", 5) != 0)
  76. continue;
  77. token += 5;
  78. if (last - token == 8 &&
  79. os_strncmp(token, "MSCHAPV2", 8) == 0) {
  80. selected = "MSCHAPV2";
  81. data->phase2_type = EAP_TTLS_PHASE2_MSCHAPV2;
  82. } else if (last - token == 6 &&
  83. os_strncmp(token, "MSCHAP", 6) == 0) {
  84. selected = "MSCHAP";
  85. data->phase2_type = EAP_TTLS_PHASE2_MSCHAP;
  86. } else if (last - token == 3 &&
  87. os_strncmp(token, "PAP", 3) == 0) {
  88. selected = "PAP";
  89. data->phase2_type = EAP_TTLS_PHASE2_PAP;
  90. } else if (last - token == 4 &&
  91. os_strncmp(token, "CHAP", 4) == 0) {
  92. selected = "CHAP";
  93. data->phase2_type = EAP_TTLS_PHASE2_CHAP;
  94. } else {
  95. wpa_printf(MSG_ERROR,
  96. "EAP-TTLS: Unsupported Phase2 type '%s'",
  97. token);
  98. eap_ttls_deinit(sm, data);
  99. return NULL;
  100. }
  101. if (selected_non_eap) {
  102. wpa_printf(MSG_ERROR,
  103. "EAP-TTLS: Only one Phase2 type can be specified");
  104. eap_ttls_deinit(sm, data);
  105. return NULL;
  106. }
  107. selected_non_eap = 1;
  108. }
  109. if (os_strstr(config->phase2, "autheap=")) {
  110. if (selected_non_eap) {
  111. wpa_printf(MSG_ERROR,
  112. "EAP-TTLS: Both auth= and autheap= params cannot be specified");
  113. eap_ttls_deinit(sm, data);
  114. return NULL;
  115. }
  116. selected = "EAP";
  117. data->phase2_type = EAP_TTLS_PHASE2_EAP;
  118. }
  119. }
  120. wpa_printf(MSG_DEBUG, "EAP-TTLS: Phase2 type: %s", selected);
  121. if (data->phase2_type == EAP_TTLS_PHASE2_EAP) {
  122. if (eap_peer_select_phase2_methods(config, "autheap=",
  123. &data->phase2_eap_types,
  124. &data->num_phase2_eap_types)
  125. < 0) {
  126. eap_ttls_deinit(sm, data);
  127. return NULL;
  128. }
  129. data->phase2_eap_type.vendor = EAP_VENDOR_IETF;
  130. data->phase2_eap_type.method = EAP_TYPE_NONE;
  131. }
  132. if (eap_peer_tls_ssl_init(sm, &data->ssl, config, EAP_TYPE_TTLS)) {
  133. wpa_printf(MSG_INFO, "EAP-TTLS: Failed to initialize SSL.");
  134. eap_ttls_deinit(sm, data);
  135. return NULL;
  136. }
  137. return data;
  138. }
  139. static void eap_ttls_phase2_eap_deinit(struct eap_sm *sm,
  140. struct eap_ttls_data *data)
  141. {
  142. if (data->phase2_priv && data->phase2_method) {
  143. data->phase2_method->deinit(sm, data->phase2_priv);
  144. data->phase2_method = NULL;
  145. data->phase2_priv = NULL;
  146. }
  147. }
  148. static void eap_ttls_free_key(struct eap_ttls_data *data)
  149. {
  150. if (data->key_data) {
  151. bin_clear_free(data->key_data, EAP_TLS_KEY_LEN + EAP_EMSK_LEN);
  152. data->key_data = NULL;
  153. }
  154. }
  155. static void eap_ttls_deinit(struct eap_sm *sm, void *priv)
  156. {
  157. struct eap_ttls_data *data = priv;
  158. if (data == NULL)
  159. return;
  160. eap_ttls_phase2_eap_deinit(sm, data);
  161. os_free(data->phase2_eap_types);
  162. eap_peer_tls_ssl_deinit(sm, &data->ssl);
  163. eap_ttls_free_key(data);
  164. os_free(data->session_id);
  165. wpabuf_free(data->pending_phase2_req);
  166. wpabuf_free(data->pending_resp);
  167. os_free(data);
  168. }
  169. static u8 * eap_ttls_avp_hdr(u8 *avphdr, u32 avp_code, u32 vendor_id,
  170. int mandatory, size_t len)
  171. {
  172. struct ttls_avp_vendor *avp;
  173. u8 flags;
  174. size_t hdrlen;
  175. avp = (struct ttls_avp_vendor *) avphdr;
  176. flags = mandatory ? AVP_FLAGS_MANDATORY : 0;
  177. if (vendor_id) {
  178. flags |= AVP_FLAGS_VENDOR;
  179. hdrlen = sizeof(*avp);
  180. avp->vendor_id = host_to_be32(vendor_id);
  181. } else {
  182. hdrlen = sizeof(struct ttls_avp);
  183. }
  184. avp->avp_code = host_to_be32(avp_code);
  185. avp->avp_length = host_to_be32(((u32) flags << 24) |
  186. (u32) (hdrlen + len));
  187. return avphdr + hdrlen;
  188. }
  189. static u8 * eap_ttls_avp_add(u8 *start, u8 *avphdr, u32 avp_code,
  190. u32 vendor_id, int mandatory,
  191. const u8 *data, size_t len)
  192. {
  193. u8 *pos;
  194. pos = eap_ttls_avp_hdr(avphdr, avp_code, vendor_id, mandatory, len);
  195. os_memcpy(pos, data, len);
  196. pos += len;
  197. AVP_PAD(start, pos);
  198. return pos;
  199. }
  200. static int eap_ttls_avp_encapsulate(struct wpabuf **resp, u32 avp_code,
  201. int mandatory)
  202. {
  203. struct wpabuf *msg;
  204. u8 *avp, *pos;
  205. msg = wpabuf_alloc(sizeof(struct ttls_avp) + wpabuf_len(*resp) + 4);
  206. if (msg == NULL) {
  207. wpabuf_free(*resp);
  208. *resp = NULL;
  209. return -1;
  210. }
  211. avp = wpabuf_mhead(msg);
  212. pos = eap_ttls_avp_hdr(avp, avp_code, 0, mandatory, wpabuf_len(*resp));
  213. os_memcpy(pos, wpabuf_head(*resp), wpabuf_len(*resp));
  214. pos += wpabuf_len(*resp);
  215. AVP_PAD(avp, pos);
  216. wpabuf_free(*resp);
  217. wpabuf_put(msg, pos - avp);
  218. *resp = msg;
  219. return 0;
  220. }
  221. static int eap_ttls_v0_derive_key(struct eap_sm *sm,
  222. struct eap_ttls_data *data)
  223. {
  224. eap_ttls_free_key(data);
  225. data->key_data = eap_peer_tls_derive_key(sm, &data->ssl,
  226. "ttls keying material",
  227. EAP_TLS_KEY_LEN +
  228. EAP_EMSK_LEN);
  229. if (!data->key_data) {
  230. wpa_printf(MSG_INFO, "EAP-TTLS: Failed to derive key");
  231. return -1;
  232. }
  233. wpa_hexdump_key(MSG_DEBUG, "EAP-TTLS: Derived key",
  234. data->key_data, EAP_TLS_KEY_LEN);
  235. wpa_hexdump_key(MSG_DEBUG, "EAP-TTLS: Derived EMSK",
  236. data->key_data + EAP_TLS_KEY_LEN,
  237. EAP_EMSK_LEN);
  238. os_free(data->session_id);
  239. data->session_id = eap_peer_tls_derive_session_id(sm, &data->ssl,
  240. EAP_TYPE_TTLS,
  241. &data->id_len);
  242. if (data->session_id) {
  243. wpa_hexdump(MSG_DEBUG, "EAP-TTLS: Derived Session-Id",
  244. data->session_id, data->id_len);
  245. } else {
  246. wpa_printf(MSG_ERROR, "EAP-TTLS: Failed to derive Session-Id");
  247. }
  248. return 0;
  249. }
  250. #ifndef CONFIG_FIPS
  251. static u8 * eap_ttls_implicit_challenge(struct eap_sm *sm,
  252. struct eap_ttls_data *data, size_t len)
  253. {
  254. return eap_peer_tls_derive_key(sm, &data->ssl, "ttls challenge", len);
  255. }
  256. #endif /* CONFIG_FIPS */
  257. static void eap_ttls_phase2_select_eap_method(struct eap_ttls_data *data,
  258. u8 method)
  259. {
  260. size_t i;
  261. for (i = 0; i < data->num_phase2_eap_types; i++) {
  262. if (data->phase2_eap_types[i].vendor != EAP_VENDOR_IETF ||
  263. data->phase2_eap_types[i].method != method)
  264. continue;
  265. data->phase2_eap_type.vendor =
  266. data->phase2_eap_types[i].vendor;
  267. data->phase2_eap_type.method =
  268. data->phase2_eap_types[i].method;
  269. wpa_printf(MSG_DEBUG, "EAP-TTLS: Selected "
  270. "Phase 2 EAP vendor %d method %d",
  271. data->phase2_eap_type.vendor,
  272. data->phase2_eap_type.method);
  273. break;
  274. }
  275. }
  276. static int eap_ttls_phase2_eap_process(struct eap_sm *sm,
  277. struct eap_ttls_data *data,
  278. struct eap_method_ret *ret,
  279. struct eap_hdr *hdr, size_t len,
  280. struct wpabuf **resp)
  281. {
  282. struct wpabuf msg;
  283. struct eap_method_ret iret;
  284. os_memset(&iret, 0, sizeof(iret));
  285. wpabuf_set(&msg, hdr, len);
  286. *resp = data->phase2_method->process(sm, data->phase2_priv, &iret,
  287. &msg);
  288. if ((iret.methodState == METHOD_DONE ||
  289. iret.methodState == METHOD_MAY_CONT) &&
  290. (iret.decision == DECISION_UNCOND_SUCC ||
  291. iret.decision == DECISION_COND_SUCC ||
  292. iret.decision == DECISION_FAIL)) {
  293. ret->methodState = iret.methodState;
  294. ret->decision = iret.decision;
  295. }
  296. return 0;
  297. }
  298. static int eap_ttls_phase2_request_eap_method(struct eap_sm *sm,
  299. struct eap_ttls_data *data,
  300. struct eap_method_ret *ret,
  301. struct eap_hdr *hdr, size_t len,
  302. u8 method, struct wpabuf **resp)
  303. {
  304. #ifdef EAP_TNC
  305. if (data->tnc_started && data->phase2_method &&
  306. data->phase2_priv && method == EAP_TYPE_TNC &&
  307. data->phase2_eap_type.method == EAP_TYPE_TNC)
  308. return eap_ttls_phase2_eap_process(sm, data, ret, hdr, len,
  309. resp);
  310. if (data->ready_for_tnc && !data->tnc_started &&
  311. method == EAP_TYPE_TNC) {
  312. wpa_printf(MSG_DEBUG, "EAP-TTLS: Start TNC after completed "
  313. "EAP method");
  314. data->tnc_started = 1;
  315. }
  316. if (data->tnc_started) {
  317. if (data->phase2_eap_type.vendor != EAP_VENDOR_IETF ||
  318. data->phase2_eap_type.method == EAP_TYPE_TNC) {
  319. wpa_printf(MSG_DEBUG, "EAP-TTLS: Unexpected EAP "
  320. "type %d for TNC", method);
  321. return -1;
  322. }
  323. data->phase2_eap_type.vendor = EAP_VENDOR_IETF;
  324. data->phase2_eap_type.method = method;
  325. wpa_printf(MSG_DEBUG, "EAP-TTLS: Selected "
  326. "Phase 2 EAP vendor %d method %d (TNC)",
  327. data->phase2_eap_type.vendor,
  328. data->phase2_eap_type.method);
  329. if (data->phase2_type == EAP_TTLS_PHASE2_EAP)
  330. eap_ttls_phase2_eap_deinit(sm, data);
  331. }
  332. #endif /* EAP_TNC */
  333. if (data->phase2_eap_type.vendor == EAP_VENDOR_IETF &&
  334. data->phase2_eap_type.method == EAP_TYPE_NONE)
  335. eap_ttls_phase2_select_eap_method(data, method);
  336. if (method != data->phase2_eap_type.method || method == EAP_TYPE_NONE)
  337. {
  338. if (eap_peer_tls_phase2_nak(data->phase2_eap_types,
  339. data->num_phase2_eap_types,
  340. hdr, resp))
  341. return -1;
  342. return 0;
  343. }
  344. if (data->phase2_priv == NULL) {
  345. data->phase2_method = eap_peer_get_eap_method(
  346. EAP_VENDOR_IETF, method);
  347. if (data->phase2_method) {
  348. sm->init_phase2 = 1;
  349. data->phase2_priv = data->phase2_method->init(sm);
  350. sm->init_phase2 = 0;
  351. }
  352. }
  353. if (data->phase2_priv == NULL || data->phase2_method == NULL) {
  354. wpa_printf(MSG_INFO, "EAP-TTLS: failed to initialize "
  355. "Phase 2 EAP method %d", method);
  356. return -1;
  357. }
  358. return eap_ttls_phase2_eap_process(sm, data, ret, hdr, len, resp);
  359. }
  360. static int eap_ttls_phase2_request_eap(struct eap_sm *sm,
  361. struct eap_ttls_data *data,
  362. struct eap_method_ret *ret,
  363. struct eap_hdr *hdr,
  364. struct wpabuf **resp)
  365. {
  366. size_t len = be_to_host16(hdr->length);
  367. u8 *pos;
  368. struct eap_peer_config *config = eap_get_config(sm);
  369. if (len <= sizeof(struct eap_hdr)) {
  370. wpa_printf(MSG_INFO, "EAP-TTLS: too short "
  371. "Phase 2 request (len=%lu)", (unsigned long) len);
  372. return -1;
  373. }
  374. pos = (u8 *) (hdr + 1);
  375. wpa_printf(MSG_DEBUG, "EAP-TTLS: Phase 2 EAP Request: type=%d", *pos);
  376. switch (*pos) {
  377. case EAP_TYPE_IDENTITY:
  378. *resp = eap_sm_buildIdentity(sm, hdr->identifier, 1);
  379. break;
  380. default:
  381. if (eap_ttls_phase2_request_eap_method(sm, data, ret, hdr, len,
  382. *pos, resp) < 0)
  383. return -1;
  384. break;
  385. }
  386. if (*resp == NULL &&
  387. (config->pending_req_identity || config->pending_req_password ||
  388. config->pending_req_otp)) {
  389. return 0;
  390. }
  391. if (*resp == NULL)
  392. return -1;
  393. wpa_hexdump_buf(MSG_DEBUG, "EAP-TTLS: AVP encapsulate EAP Response",
  394. *resp);
  395. return eap_ttls_avp_encapsulate(resp, RADIUS_ATTR_EAP_MESSAGE, 1);
  396. }
  397. static int eap_ttls_phase2_request_mschapv2(struct eap_sm *sm,
  398. struct eap_ttls_data *data,
  399. struct eap_method_ret *ret,
  400. struct wpabuf **resp)
  401. {
  402. #ifdef CONFIG_FIPS
  403. wpa_printf(MSG_ERROR, "EAP-TTLS: MSCHAPV2 not supported in FIPS build");
  404. return -1;
  405. #else /* CONFIG_FIPS */
  406. #ifdef EAP_MSCHAPv2
  407. struct wpabuf *msg;
  408. u8 *buf, *pos, *challenge, *peer_challenge;
  409. const u8 *identity, *password;
  410. size_t identity_len, password_len;
  411. int pwhash;
  412. wpa_printf(MSG_DEBUG, "EAP-TTLS: Phase 2 MSCHAPV2 Request");
  413. identity = eap_get_config_identity(sm, &identity_len);
  414. password = eap_get_config_password2(sm, &password_len, &pwhash);
  415. if (identity == NULL || password == NULL)
  416. return -1;
  417. msg = wpabuf_alloc(identity_len + 1000);
  418. if (msg == NULL) {
  419. wpa_printf(MSG_ERROR,
  420. "EAP-TTLS/MSCHAPV2: Failed to allocate memory");
  421. return -1;
  422. }
  423. pos = buf = wpabuf_mhead(msg);
  424. /* User-Name */
  425. pos = eap_ttls_avp_add(buf, pos, RADIUS_ATTR_USER_NAME, 0, 1,
  426. identity, identity_len);
  427. /* MS-CHAP-Challenge */
  428. challenge = eap_ttls_implicit_challenge(
  429. sm, data, EAP_TTLS_MSCHAPV2_CHALLENGE_LEN + 1);
  430. if (challenge == NULL) {
  431. wpabuf_free(msg);
  432. wpa_printf(MSG_ERROR, "EAP-TTLS/MSCHAPV2: Failed to derive "
  433. "implicit challenge");
  434. return -1;
  435. }
  436. pos = eap_ttls_avp_add(buf, pos, RADIUS_ATTR_MS_CHAP_CHALLENGE,
  437. RADIUS_VENDOR_ID_MICROSOFT, 1,
  438. challenge, EAP_TTLS_MSCHAPV2_CHALLENGE_LEN);
  439. /* MS-CHAP2-Response */
  440. pos = eap_ttls_avp_hdr(pos, RADIUS_ATTR_MS_CHAP2_RESPONSE,
  441. RADIUS_VENDOR_ID_MICROSOFT, 1,
  442. EAP_TTLS_MSCHAPV2_RESPONSE_LEN);
  443. data->ident = challenge[EAP_TTLS_MSCHAPV2_CHALLENGE_LEN];
  444. *pos++ = data->ident;
  445. *pos++ = 0; /* Flags */
  446. if (os_get_random(pos, EAP_TTLS_MSCHAPV2_CHALLENGE_LEN) < 0) {
  447. os_free(challenge);
  448. wpabuf_free(msg);
  449. wpa_printf(MSG_ERROR, "EAP-TTLS/MSCHAPV2: Failed to get "
  450. "random data for peer challenge");
  451. return -1;
  452. }
  453. peer_challenge = pos;
  454. pos += EAP_TTLS_MSCHAPV2_CHALLENGE_LEN;
  455. os_memset(pos, 0, 8); /* Reserved, must be zero */
  456. pos += 8;
  457. if (mschapv2_derive_response(identity, identity_len, password,
  458. password_len, pwhash, challenge,
  459. peer_challenge, pos, data->auth_response,
  460. data->master_key)) {
  461. os_free(challenge);
  462. wpabuf_free(msg);
  463. wpa_printf(MSG_ERROR, "EAP-TTLS/MSCHAPV2: Failed to derive "
  464. "response");
  465. return -1;
  466. }
  467. data->auth_response_valid = 1;
  468. pos += 24;
  469. os_free(challenge);
  470. AVP_PAD(buf, pos);
  471. wpabuf_put(msg, pos - buf);
  472. *resp = msg;
  473. return 0;
  474. #else /* EAP_MSCHAPv2 */
  475. wpa_printf(MSG_ERROR, "EAP-TTLS: MSCHAPv2 not included in the build");
  476. return -1;
  477. #endif /* EAP_MSCHAPv2 */
  478. #endif /* CONFIG_FIPS */
  479. }
  480. static int eap_ttls_phase2_request_mschap(struct eap_sm *sm,
  481. struct eap_ttls_data *data,
  482. struct eap_method_ret *ret,
  483. struct wpabuf **resp)
  484. {
  485. #ifdef CONFIG_FIPS
  486. wpa_printf(MSG_ERROR, "EAP-TTLS: MSCHAP not supported in FIPS build");
  487. return -1;
  488. #else /* CONFIG_FIPS */
  489. struct wpabuf *msg;
  490. u8 *buf, *pos, *challenge;
  491. const u8 *identity, *password;
  492. size_t identity_len, password_len;
  493. int pwhash;
  494. wpa_printf(MSG_DEBUG, "EAP-TTLS: Phase 2 MSCHAP Request");
  495. identity = eap_get_config_identity(sm, &identity_len);
  496. password = eap_get_config_password2(sm, &password_len, &pwhash);
  497. if (identity == NULL || password == NULL)
  498. return -1;
  499. msg = wpabuf_alloc(identity_len + 1000);
  500. if (msg == NULL) {
  501. wpa_printf(MSG_ERROR,
  502. "EAP-TTLS/MSCHAP: Failed to allocate memory");
  503. return -1;
  504. }
  505. pos = buf = wpabuf_mhead(msg);
  506. /* User-Name */
  507. pos = eap_ttls_avp_add(buf, pos, RADIUS_ATTR_USER_NAME, 0, 1,
  508. identity, identity_len);
  509. /* MS-CHAP-Challenge */
  510. challenge = eap_ttls_implicit_challenge(
  511. sm, data, EAP_TTLS_MSCHAP_CHALLENGE_LEN + 1);
  512. if (challenge == NULL) {
  513. wpabuf_free(msg);
  514. wpa_printf(MSG_ERROR, "EAP-TTLS/MSCHAP: Failed to derive "
  515. "implicit challenge");
  516. return -1;
  517. }
  518. pos = eap_ttls_avp_add(buf, pos, RADIUS_ATTR_MS_CHAP_CHALLENGE,
  519. RADIUS_VENDOR_ID_MICROSOFT, 1,
  520. challenge, EAP_TTLS_MSCHAP_CHALLENGE_LEN);
  521. /* MS-CHAP-Response */
  522. pos = eap_ttls_avp_hdr(pos, RADIUS_ATTR_MS_CHAP_RESPONSE,
  523. RADIUS_VENDOR_ID_MICROSOFT, 1,
  524. EAP_TTLS_MSCHAP_RESPONSE_LEN);
  525. data->ident = challenge[EAP_TTLS_MSCHAP_CHALLENGE_LEN];
  526. *pos++ = data->ident;
  527. *pos++ = 1; /* Flags: Use NT style passwords */
  528. os_memset(pos, 0, 24); /* LM-Response */
  529. pos += 24;
  530. if (pwhash) {
  531. challenge_response(challenge, password, pos); /* NT-Response */
  532. wpa_hexdump_key(MSG_DEBUG, "EAP-TTLS: MSCHAP password hash",
  533. password, 16);
  534. } else {
  535. nt_challenge_response(challenge, password, password_len,
  536. pos); /* NT-Response */
  537. wpa_hexdump_ascii_key(MSG_DEBUG, "EAP-TTLS: MSCHAP password",
  538. password, password_len);
  539. }
  540. wpa_hexdump(MSG_DEBUG, "EAP-TTLS: MSCHAP implicit challenge",
  541. challenge, EAP_TTLS_MSCHAP_CHALLENGE_LEN);
  542. wpa_hexdump(MSG_DEBUG, "EAP-TTLS: MSCHAP response", pos, 24);
  543. pos += 24;
  544. os_free(challenge);
  545. AVP_PAD(buf, pos);
  546. wpabuf_put(msg, pos - buf);
  547. *resp = msg;
  548. /* EAP-TTLS/MSCHAP does not provide tunneled success
  549. * notification, so assume that Phase2 succeeds. */
  550. ret->methodState = METHOD_DONE;
  551. ret->decision = DECISION_COND_SUCC;
  552. return 0;
  553. #endif /* CONFIG_FIPS */
  554. }
  555. static int eap_ttls_phase2_request_pap(struct eap_sm *sm,
  556. struct eap_ttls_data *data,
  557. struct eap_method_ret *ret,
  558. struct wpabuf **resp)
  559. {
  560. struct wpabuf *msg;
  561. u8 *buf, *pos;
  562. size_t pad;
  563. const u8 *identity, *password;
  564. size_t identity_len, password_len;
  565. wpa_printf(MSG_DEBUG, "EAP-TTLS: Phase 2 PAP Request");
  566. identity = eap_get_config_identity(sm, &identity_len);
  567. password = eap_get_config_password(sm, &password_len);
  568. if (identity == NULL || password == NULL)
  569. return -1;
  570. msg = wpabuf_alloc(identity_len + password_len + 100);
  571. if (msg == NULL) {
  572. wpa_printf(MSG_ERROR,
  573. "EAP-TTLS/PAP: Failed to allocate memory");
  574. return -1;
  575. }
  576. pos = buf = wpabuf_mhead(msg);
  577. /* User-Name */
  578. pos = eap_ttls_avp_add(buf, pos, RADIUS_ATTR_USER_NAME, 0, 1,
  579. identity, identity_len);
  580. /* User-Password; in RADIUS, this is encrypted, but EAP-TTLS encrypts
  581. * the data, so no separate encryption is used in the AVP itself.
  582. * However, the password is padded to obfuscate its length. */
  583. pad = password_len == 0 ? 16 : (16 - (password_len & 15)) & 15;
  584. pos = eap_ttls_avp_hdr(pos, RADIUS_ATTR_USER_PASSWORD, 0, 1,
  585. password_len + pad);
  586. os_memcpy(pos, password, password_len);
  587. pos += password_len;
  588. os_memset(pos, 0, pad);
  589. pos += pad;
  590. AVP_PAD(buf, pos);
  591. wpabuf_put(msg, pos - buf);
  592. *resp = msg;
  593. /* EAP-TTLS/PAP does not provide tunneled success notification,
  594. * so assume that Phase2 succeeds. */
  595. ret->methodState = METHOD_DONE;
  596. ret->decision = DECISION_COND_SUCC;
  597. return 0;
  598. }
  599. static int eap_ttls_phase2_request_chap(struct eap_sm *sm,
  600. struct eap_ttls_data *data,
  601. struct eap_method_ret *ret,
  602. struct wpabuf **resp)
  603. {
  604. #ifdef CONFIG_FIPS
  605. wpa_printf(MSG_ERROR, "EAP-TTLS: CHAP not supported in FIPS build");
  606. return -1;
  607. #else /* CONFIG_FIPS */
  608. struct wpabuf *msg;
  609. u8 *buf, *pos, *challenge;
  610. const u8 *identity, *password;
  611. size_t identity_len, password_len;
  612. wpa_printf(MSG_DEBUG, "EAP-TTLS: Phase 2 CHAP Request");
  613. identity = eap_get_config_identity(sm, &identity_len);
  614. password = eap_get_config_password(sm, &password_len);
  615. if (identity == NULL || password == NULL)
  616. return -1;
  617. msg = wpabuf_alloc(identity_len + 1000);
  618. if (msg == NULL) {
  619. wpa_printf(MSG_ERROR,
  620. "EAP-TTLS/CHAP: Failed to allocate memory");
  621. return -1;
  622. }
  623. pos = buf = wpabuf_mhead(msg);
  624. /* User-Name */
  625. pos = eap_ttls_avp_add(buf, pos, RADIUS_ATTR_USER_NAME, 0, 1,
  626. identity, identity_len);
  627. /* CHAP-Challenge */
  628. challenge = eap_ttls_implicit_challenge(
  629. sm, data, EAP_TTLS_CHAP_CHALLENGE_LEN + 1);
  630. if (challenge == NULL) {
  631. wpabuf_free(msg);
  632. wpa_printf(MSG_ERROR, "EAP-TTLS/CHAP: Failed to derive "
  633. "implicit challenge");
  634. return -1;
  635. }
  636. pos = eap_ttls_avp_add(buf, pos, RADIUS_ATTR_CHAP_CHALLENGE, 0, 1,
  637. challenge, EAP_TTLS_CHAP_CHALLENGE_LEN);
  638. /* CHAP-Password */
  639. pos = eap_ttls_avp_hdr(pos, RADIUS_ATTR_CHAP_PASSWORD, 0, 1,
  640. 1 + EAP_TTLS_CHAP_PASSWORD_LEN);
  641. data->ident = challenge[EAP_TTLS_CHAP_CHALLENGE_LEN];
  642. *pos++ = data->ident;
  643. /* MD5(Ident + Password + Challenge) */
  644. chap_md5(data->ident, password, password_len, challenge,
  645. EAP_TTLS_CHAP_CHALLENGE_LEN, pos);
  646. wpa_hexdump_ascii(MSG_DEBUG, "EAP-TTLS: CHAP username",
  647. identity, identity_len);
  648. wpa_hexdump_ascii_key(MSG_DEBUG, "EAP-TTLS: CHAP password",
  649. password, password_len);
  650. wpa_hexdump(MSG_DEBUG, "EAP-TTLS: CHAP implicit challenge",
  651. challenge, EAP_TTLS_CHAP_CHALLENGE_LEN);
  652. wpa_hexdump(MSG_DEBUG, "EAP-TTLS: CHAP password",
  653. pos, EAP_TTLS_CHAP_PASSWORD_LEN);
  654. pos += EAP_TTLS_CHAP_PASSWORD_LEN;
  655. os_free(challenge);
  656. AVP_PAD(buf, pos);
  657. wpabuf_put(msg, pos - buf);
  658. *resp = msg;
  659. /* EAP-TTLS/CHAP does not provide tunneled success
  660. * notification, so assume that Phase2 succeeds. */
  661. ret->methodState = METHOD_DONE;
  662. ret->decision = DECISION_COND_SUCC;
  663. return 0;
  664. #endif /* CONFIG_FIPS */
  665. }
  666. static int eap_ttls_phase2_request(struct eap_sm *sm,
  667. struct eap_ttls_data *data,
  668. struct eap_method_ret *ret,
  669. struct eap_hdr *hdr,
  670. struct wpabuf **resp)
  671. {
  672. int res = 0;
  673. size_t len;
  674. enum phase2_types phase2_type = data->phase2_type;
  675. #ifdef EAP_TNC
  676. if (data->tnc_started) {
  677. wpa_printf(MSG_DEBUG, "EAP-TTLS: Processing TNC");
  678. phase2_type = EAP_TTLS_PHASE2_EAP;
  679. }
  680. #endif /* EAP_TNC */
  681. if (phase2_type == EAP_TTLS_PHASE2_MSCHAPV2 ||
  682. phase2_type == EAP_TTLS_PHASE2_MSCHAP ||
  683. phase2_type == EAP_TTLS_PHASE2_PAP ||
  684. phase2_type == EAP_TTLS_PHASE2_CHAP) {
  685. if (eap_get_config_identity(sm, &len) == NULL) {
  686. wpa_printf(MSG_INFO,
  687. "EAP-TTLS: Identity not configured");
  688. eap_sm_request_identity(sm);
  689. if (eap_get_config_password(sm, &len) == NULL)
  690. eap_sm_request_password(sm);
  691. return 0;
  692. }
  693. if (eap_get_config_password(sm, &len) == NULL) {
  694. wpa_printf(MSG_INFO,
  695. "EAP-TTLS: Password not configured");
  696. eap_sm_request_password(sm);
  697. return 0;
  698. }
  699. }
  700. switch (phase2_type) {
  701. case EAP_TTLS_PHASE2_EAP:
  702. res = eap_ttls_phase2_request_eap(sm, data, ret, hdr, resp);
  703. break;
  704. case EAP_TTLS_PHASE2_MSCHAPV2:
  705. res = eap_ttls_phase2_request_mschapv2(sm, data, ret, resp);
  706. break;
  707. case EAP_TTLS_PHASE2_MSCHAP:
  708. res = eap_ttls_phase2_request_mschap(sm, data, ret, resp);
  709. break;
  710. case EAP_TTLS_PHASE2_PAP:
  711. res = eap_ttls_phase2_request_pap(sm, data, ret, resp);
  712. break;
  713. case EAP_TTLS_PHASE2_CHAP:
  714. res = eap_ttls_phase2_request_chap(sm, data, ret, resp);
  715. break;
  716. default:
  717. wpa_printf(MSG_ERROR, "EAP-TTLS: Phase 2 - Unknown");
  718. res = -1;
  719. break;
  720. }
  721. if (res < 0) {
  722. ret->methodState = METHOD_DONE;
  723. ret->decision = DECISION_FAIL;
  724. }
  725. return res;
  726. }
  727. struct ttls_parse_avp {
  728. u8 *mschapv2;
  729. u8 *eapdata;
  730. size_t eap_len;
  731. int mschapv2_error;
  732. };
  733. static int eap_ttls_parse_attr_eap(const u8 *dpos, size_t dlen,
  734. struct ttls_parse_avp *parse)
  735. {
  736. wpa_printf(MSG_DEBUG, "EAP-TTLS: AVP - EAP Message");
  737. if (parse->eapdata == NULL) {
  738. parse->eapdata = os_malloc(dlen);
  739. if (parse->eapdata == NULL) {
  740. wpa_printf(MSG_WARNING, "EAP-TTLS: Failed to allocate "
  741. "memory for Phase 2 EAP data");
  742. return -1;
  743. }
  744. os_memcpy(parse->eapdata, dpos, dlen);
  745. parse->eap_len = dlen;
  746. } else {
  747. u8 *neweap = os_realloc(parse->eapdata, parse->eap_len + dlen);
  748. if (neweap == NULL) {
  749. wpa_printf(MSG_WARNING, "EAP-TTLS: Failed to allocate "
  750. "memory for Phase 2 EAP data");
  751. return -1;
  752. }
  753. os_memcpy(neweap + parse->eap_len, dpos, dlen);
  754. parse->eapdata = neweap;
  755. parse->eap_len += dlen;
  756. }
  757. return 0;
  758. }
  759. static int eap_ttls_parse_avp(u8 *pos, size_t left,
  760. struct ttls_parse_avp *parse)
  761. {
  762. struct ttls_avp *avp;
  763. u32 avp_code, avp_length, vendor_id = 0;
  764. u8 avp_flags, *dpos;
  765. size_t dlen;
  766. avp = (struct ttls_avp *) pos;
  767. avp_code = be_to_host32(avp->avp_code);
  768. avp_length = be_to_host32(avp->avp_length);
  769. avp_flags = (avp_length >> 24) & 0xff;
  770. avp_length &= 0xffffff;
  771. wpa_printf(MSG_DEBUG, "EAP-TTLS: AVP: code=%d flags=0x%02x "
  772. "length=%d", (int) avp_code, avp_flags,
  773. (int) avp_length);
  774. if (avp_length > left) {
  775. wpa_printf(MSG_WARNING, "EAP-TTLS: AVP overflow "
  776. "(len=%d, left=%lu) - dropped",
  777. (int) avp_length, (unsigned long) left);
  778. return -1;
  779. }
  780. if (avp_length < sizeof(*avp)) {
  781. wpa_printf(MSG_WARNING, "EAP-TTLS: Invalid AVP length %d",
  782. avp_length);
  783. return -1;
  784. }
  785. dpos = (u8 *) (avp + 1);
  786. dlen = avp_length - sizeof(*avp);
  787. if (avp_flags & AVP_FLAGS_VENDOR) {
  788. if (dlen < 4) {
  789. wpa_printf(MSG_WARNING, "EAP-TTLS: Vendor AVP "
  790. "underflow");
  791. return -1;
  792. }
  793. vendor_id = WPA_GET_BE32(dpos);
  794. wpa_printf(MSG_DEBUG, "EAP-TTLS: AVP vendor_id %d",
  795. (int) vendor_id);
  796. dpos += 4;
  797. dlen -= 4;
  798. }
  799. wpa_hexdump(MSG_DEBUG, "EAP-TTLS: AVP data", dpos, dlen);
  800. if (vendor_id == 0 && avp_code == RADIUS_ATTR_EAP_MESSAGE) {
  801. if (eap_ttls_parse_attr_eap(dpos, dlen, parse) < 0)
  802. return -1;
  803. } else if (vendor_id == 0 && avp_code == RADIUS_ATTR_REPLY_MESSAGE) {
  804. /* This is an optional message that can be displayed to
  805. * the user. */
  806. wpa_hexdump_ascii(MSG_DEBUG, "EAP-TTLS: AVP - Reply-Message",
  807. dpos, dlen);
  808. } else if (vendor_id == RADIUS_VENDOR_ID_MICROSOFT &&
  809. avp_code == RADIUS_ATTR_MS_CHAP2_SUCCESS) {
  810. wpa_hexdump_ascii(MSG_DEBUG, "EAP-TTLS: MS-CHAP2-Success",
  811. dpos, dlen);
  812. if (dlen != 43) {
  813. wpa_printf(MSG_WARNING, "EAP-TTLS: Unexpected "
  814. "MS-CHAP2-Success length "
  815. "(len=%lu, expected 43)",
  816. (unsigned long) dlen);
  817. return -1;
  818. }
  819. parse->mschapv2 = dpos;
  820. } else if (vendor_id == RADIUS_VENDOR_ID_MICROSOFT &&
  821. avp_code == RADIUS_ATTR_MS_CHAP_ERROR) {
  822. wpa_hexdump_ascii(MSG_DEBUG, "EAP-TTLS: MS-CHAP-Error",
  823. dpos, dlen);
  824. parse->mschapv2_error = 1;
  825. } else if (avp_flags & AVP_FLAGS_MANDATORY) {
  826. wpa_printf(MSG_WARNING, "EAP-TTLS: Unsupported mandatory AVP "
  827. "code %d vendor_id %d - dropped",
  828. (int) avp_code, (int) vendor_id);
  829. return -1;
  830. } else {
  831. wpa_printf(MSG_DEBUG, "EAP-TTLS: Ignoring unsupported AVP "
  832. "code %d vendor_id %d",
  833. (int) avp_code, (int) vendor_id);
  834. }
  835. return avp_length;
  836. }
  837. static int eap_ttls_parse_avps(struct wpabuf *in_decrypted,
  838. struct ttls_parse_avp *parse)
  839. {
  840. u8 *pos;
  841. size_t left, pad;
  842. int avp_length;
  843. pos = wpabuf_mhead(in_decrypted);
  844. left = wpabuf_len(in_decrypted);
  845. wpa_hexdump(MSG_DEBUG, "EAP-TTLS: Decrypted Phase 2 AVPs", pos, left);
  846. if (left < sizeof(struct ttls_avp)) {
  847. wpa_printf(MSG_WARNING, "EAP-TTLS: Too short Phase 2 AVP frame"
  848. " len=%lu expected %lu or more - dropped",
  849. (unsigned long) left,
  850. (unsigned long) sizeof(struct ttls_avp));
  851. return -1;
  852. }
  853. /* Parse AVPs */
  854. os_memset(parse, 0, sizeof(*parse));
  855. while (left > 0) {
  856. avp_length = eap_ttls_parse_avp(pos, left, parse);
  857. if (avp_length < 0)
  858. return -1;
  859. pad = (4 - (avp_length & 3)) & 3;
  860. pos += avp_length + pad;
  861. if (left < avp_length + pad)
  862. left = 0;
  863. else
  864. left -= avp_length + pad;
  865. }
  866. return 0;
  867. }
  868. static u8 * eap_ttls_fake_identity_request(void)
  869. {
  870. struct eap_hdr *hdr;
  871. u8 *buf;
  872. wpa_printf(MSG_DEBUG, "EAP-TTLS: empty data in beginning of "
  873. "Phase 2 - use fake EAP-Request Identity");
  874. buf = os_malloc(sizeof(*hdr) + 1);
  875. if (buf == NULL) {
  876. wpa_printf(MSG_WARNING, "EAP-TTLS: failed to allocate "
  877. "memory for fake EAP-Identity Request");
  878. return NULL;
  879. }
  880. hdr = (struct eap_hdr *) buf;
  881. hdr->code = EAP_CODE_REQUEST;
  882. hdr->identifier = 0;
  883. hdr->length = host_to_be16(sizeof(*hdr) + 1);
  884. buf[sizeof(*hdr)] = EAP_TYPE_IDENTITY;
  885. return buf;
  886. }
  887. static int eap_ttls_encrypt_response(struct eap_sm *sm,
  888. struct eap_ttls_data *data,
  889. struct wpabuf *resp, u8 identifier,
  890. struct wpabuf **out_data)
  891. {
  892. if (resp == NULL)
  893. return 0;
  894. wpa_hexdump_buf_key(MSG_DEBUG, "EAP-TTLS: Encrypting Phase 2 data",
  895. resp);
  896. if (eap_peer_tls_encrypt(sm, &data->ssl, EAP_TYPE_TTLS,
  897. data->ttls_version, identifier,
  898. resp, out_data)) {
  899. wpa_printf(MSG_INFO, "EAP-TTLS: Failed to encrypt a Phase 2 "
  900. "frame");
  901. wpabuf_free(resp);
  902. return -1;
  903. }
  904. wpabuf_free(resp);
  905. return 0;
  906. }
  907. static int eap_ttls_process_phase2_eap(struct eap_sm *sm,
  908. struct eap_ttls_data *data,
  909. struct eap_method_ret *ret,
  910. struct ttls_parse_avp *parse,
  911. struct wpabuf **resp)
  912. {
  913. struct eap_hdr *hdr;
  914. size_t len;
  915. if (parse->eapdata == NULL) {
  916. wpa_printf(MSG_WARNING, "EAP-TTLS: No EAP Message in the "
  917. "packet - dropped");
  918. return -1;
  919. }
  920. wpa_hexdump(MSG_DEBUG, "EAP-TTLS: Phase 2 EAP",
  921. parse->eapdata, parse->eap_len);
  922. hdr = (struct eap_hdr *) parse->eapdata;
  923. if (parse->eap_len < sizeof(*hdr)) {
  924. wpa_printf(MSG_WARNING, "EAP-TTLS: Too short Phase 2 EAP "
  925. "frame (len=%lu, expected %lu or more) - dropped",
  926. (unsigned long) parse->eap_len,
  927. (unsigned long) sizeof(*hdr));
  928. return -1;
  929. }
  930. len = be_to_host16(hdr->length);
  931. if (len > parse->eap_len) {
  932. wpa_printf(MSG_INFO, "EAP-TTLS: Length mismatch in Phase 2 "
  933. "EAP frame (EAP hdr len=%lu, EAP data len in "
  934. "AVP=%lu)",
  935. (unsigned long) len,
  936. (unsigned long) parse->eap_len);
  937. return -1;
  938. }
  939. wpa_printf(MSG_DEBUG, "EAP-TTLS: received Phase 2: code=%d "
  940. "identifier=%d length=%lu",
  941. hdr->code, hdr->identifier, (unsigned long) len);
  942. switch (hdr->code) {
  943. case EAP_CODE_REQUEST:
  944. if (eap_ttls_phase2_request(sm, data, ret, hdr, resp)) {
  945. wpa_printf(MSG_INFO, "EAP-TTLS: Phase2 Request "
  946. "processing failed");
  947. return -1;
  948. }
  949. break;
  950. default:
  951. wpa_printf(MSG_INFO, "EAP-TTLS: Unexpected code=%d in "
  952. "Phase 2 EAP header", hdr->code);
  953. return -1;
  954. }
  955. return 0;
  956. }
  957. static int eap_ttls_process_phase2_mschapv2(struct eap_sm *sm,
  958. struct eap_ttls_data *data,
  959. struct eap_method_ret *ret,
  960. struct ttls_parse_avp *parse)
  961. {
  962. #ifdef EAP_MSCHAPv2
  963. if (parse->mschapv2_error) {
  964. wpa_printf(MSG_DEBUG, "EAP-TTLS/MSCHAPV2: Received "
  965. "MS-CHAP-Error - failed");
  966. ret->methodState = METHOD_DONE;
  967. ret->decision = DECISION_FAIL;
  968. /* Reply with empty data to ACK error */
  969. return 1;
  970. }
  971. if (parse->mschapv2 == NULL) {
  972. #ifdef EAP_TNC
  973. if (data->phase2_success && parse->eapdata) {
  974. /*
  975. * Allow EAP-TNC to be started after successfully
  976. * completed MSCHAPV2.
  977. */
  978. return 1;
  979. }
  980. #endif /* EAP_TNC */
  981. wpa_printf(MSG_WARNING, "EAP-TTLS: no MS-CHAP2-Success AVP "
  982. "received for Phase2 MSCHAPV2");
  983. return -1;
  984. }
  985. if (parse->mschapv2[0] != data->ident) {
  986. wpa_printf(MSG_WARNING, "EAP-TTLS: Ident mismatch for Phase 2 "
  987. "MSCHAPV2 (received Ident 0x%02x, expected 0x%02x)",
  988. parse->mschapv2[0], data->ident);
  989. return -1;
  990. }
  991. if (!data->auth_response_valid ||
  992. mschapv2_verify_auth_response(data->auth_response,
  993. parse->mschapv2 + 1, 42)) {
  994. wpa_printf(MSG_WARNING, "EAP-TTLS: Invalid authenticator "
  995. "response in Phase 2 MSCHAPV2 success request");
  996. return -1;
  997. }
  998. wpa_printf(MSG_INFO, "EAP-TTLS: Phase 2 MSCHAPV2 "
  999. "authentication succeeded");
  1000. ret->methodState = METHOD_DONE;
  1001. ret->decision = DECISION_UNCOND_SUCC;
  1002. data->phase2_success = 1;
  1003. /*
  1004. * Reply with empty data; authentication server will reply
  1005. * with EAP-Success after this.
  1006. */
  1007. return 1;
  1008. #else /* EAP_MSCHAPv2 */
  1009. wpa_printf(MSG_ERROR, "EAP-TTLS: MSCHAPv2 not included in the build");
  1010. return -1;
  1011. #endif /* EAP_MSCHAPv2 */
  1012. }
  1013. #ifdef EAP_TNC
  1014. static int eap_ttls_process_tnc_start(struct eap_sm *sm,
  1015. struct eap_ttls_data *data,
  1016. struct eap_method_ret *ret,
  1017. struct ttls_parse_avp *parse,
  1018. struct wpabuf **resp)
  1019. {
  1020. /* TNC uses inner EAP method after non-EAP TTLS phase 2. */
  1021. if (parse->eapdata == NULL) {
  1022. wpa_printf(MSG_INFO, "EAP-TTLS: Phase 2 received "
  1023. "unexpected tunneled data (no EAP)");
  1024. return -1;
  1025. }
  1026. if (!data->ready_for_tnc) {
  1027. wpa_printf(MSG_INFO, "EAP-TTLS: Phase 2 received "
  1028. "EAP after non-EAP, but not ready for TNC");
  1029. return -1;
  1030. }
  1031. wpa_printf(MSG_DEBUG, "EAP-TTLS: Start TNC after completed "
  1032. "non-EAP method");
  1033. data->tnc_started = 1;
  1034. if (eap_ttls_process_phase2_eap(sm, data, ret, parse, resp) < 0)
  1035. return -1;
  1036. return 0;
  1037. }
  1038. #endif /* EAP_TNC */
  1039. static int eap_ttls_process_decrypted(struct eap_sm *sm,
  1040. struct eap_ttls_data *data,
  1041. struct eap_method_ret *ret,
  1042. u8 identifier,
  1043. struct ttls_parse_avp *parse,
  1044. struct wpabuf *in_decrypted,
  1045. struct wpabuf **out_data)
  1046. {
  1047. struct wpabuf *resp = NULL;
  1048. struct eap_peer_config *config = eap_get_config(sm);
  1049. int res;
  1050. enum phase2_types phase2_type = data->phase2_type;
  1051. #ifdef EAP_TNC
  1052. if (data->tnc_started)
  1053. phase2_type = EAP_TTLS_PHASE2_EAP;
  1054. #endif /* EAP_TNC */
  1055. switch (phase2_type) {
  1056. case EAP_TTLS_PHASE2_EAP:
  1057. if (eap_ttls_process_phase2_eap(sm, data, ret, parse, &resp) <
  1058. 0)
  1059. return -1;
  1060. break;
  1061. case EAP_TTLS_PHASE2_MSCHAPV2:
  1062. res = eap_ttls_process_phase2_mschapv2(sm, data, ret, parse);
  1063. #ifdef EAP_TNC
  1064. if (res == 1 && parse->eapdata && data->phase2_success) {
  1065. /*
  1066. * TNC may be required as the next
  1067. * authentication method within the tunnel.
  1068. */
  1069. ret->methodState = METHOD_MAY_CONT;
  1070. data->ready_for_tnc = 1;
  1071. if (eap_ttls_process_tnc_start(sm, data, ret, parse,
  1072. &resp) == 0)
  1073. break;
  1074. }
  1075. #endif /* EAP_TNC */
  1076. return res;
  1077. case EAP_TTLS_PHASE2_MSCHAP:
  1078. case EAP_TTLS_PHASE2_PAP:
  1079. case EAP_TTLS_PHASE2_CHAP:
  1080. #ifdef EAP_TNC
  1081. if (eap_ttls_process_tnc_start(sm, data, ret, parse, &resp) <
  1082. 0)
  1083. return -1;
  1084. break;
  1085. #else /* EAP_TNC */
  1086. /* EAP-TTLS/{MSCHAP,PAP,CHAP} should not send any TLS tunneled
  1087. * requests to the supplicant */
  1088. wpa_printf(MSG_INFO, "EAP-TTLS: Phase 2 received unexpected "
  1089. "tunneled data");
  1090. return -1;
  1091. #endif /* EAP_TNC */
  1092. }
  1093. if (resp) {
  1094. if (eap_ttls_encrypt_response(sm, data, resp, identifier,
  1095. out_data) < 0)
  1096. return -1;
  1097. } else if (config->pending_req_identity ||
  1098. config->pending_req_password ||
  1099. config->pending_req_otp ||
  1100. config->pending_req_new_password) {
  1101. wpabuf_free(data->pending_phase2_req);
  1102. data->pending_phase2_req = wpabuf_dup(in_decrypted);
  1103. }
  1104. return 0;
  1105. }
  1106. static int eap_ttls_implicit_identity_request(struct eap_sm *sm,
  1107. struct eap_ttls_data *data,
  1108. struct eap_method_ret *ret,
  1109. u8 identifier,
  1110. struct wpabuf **out_data)
  1111. {
  1112. int retval = 0;
  1113. struct eap_hdr *hdr;
  1114. struct wpabuf *resp;
  1115. hdr = (struct eap_hdr *) eap_ttls_fake_identity_request();
  1116. if (hdr == NULL) {
  1117. ret->methodState = METHOD_DONE;
  1118. ret->decision = DECISION_FAIL;
  1119. return -1;
  1120. }
  1121. resp = NULL;
  1122. if (eap_ttls_phase2_request(sm, data, ret, hdr, &resp)) {
  1123. wpa_printf(MSG_INFO, "EAP-TTLS: Phase2 Request "
  1124. "processing failed");
  1125. retval = -1;
  1126. } else {
  1127. struct eap_peer_config *config = eap_get_config(sm);
  1128. if (resp == NULL &&
  1129. (config->pending_req_identity ||
  1130. config->pending_req_password ||
  1131. config->pending_req_otp ||
  1132. config->pending_req_new_password)) {
  1133. /*
  1134. * Use empty buffer to force implicit request
  1135. * processing when EAP request is re-processed after
  1136. * user input.
  1137. */
  1138. wpabuf_free(data->pending_phase2_req);
  1139. data->pending_phase2_req = wpabuf_alloc(0);
  1140. }
  1141. retval = eap_ttls_encrypt_response(sm, data, resp, identifier,
  1142. out_data);
  1143. }
  1144. os_free(hdr);
  1145. if (retval < 0) {
  1146. ret->methodState = METHOD_DONE;
  1147. ret->decision = DECISION_FAIL;
  1148. }
  1149. return retval;
  1150. }
  1151. static int eap_ttls_phase2_start(struct eap_sm *sm, struct eap_ttls_data *data,
  1152. struct eap_method_ret *ret, u8 identifier,
  1153. struct wpabuf **out_data)
  1154. {
  1155. data->phase2_start = 0;
  1156. /*
  1157. * EAP-TTLS does not use Phase2 on fast re-auth; this must be done only
  1158. * if TLS part was indeed resuming a previous session. Most
  1159. * Authentication Servers terminate EAP-TTLS before reaching this
  1160. * point, but some do not. Make wpa_supplicant stop phase 2 here, if
  1161. * needed.
  1162. */
  1163. if (data->reauth &&
  1164. tls_connection_resumed(sm->ssl_ctx, data->ssl.conn)) {
  1165. wpa_printf(MSG_DEBUG, "EAP-TTLS: Session resumption - "
  1166. "skip phase 2");
  1167. *out_data = eap_peer_tls_build_ack(identifier, EAP_TYPE_TTLS,
  1168. data->ttls_version);
  1169. ret->methodState = METHOD_DONE;
  1170. ret->decision = DECISION_UNCOND_SUCC;
  1171. data->phase2_success = 1;
  1172. return 0;
  1173. }
  1174. return eap_ttls_implicit_identity_request(sm, data, ret, identifier,
  1175. out_data);
  1176. }
  1177. static int eap_ttls_decrypt(struct eap_sm *sm, struct eap_ttls_data *data,
  1178. struct eap_method_ret *ret, u8 identifier,
  1179. const struct wpabuf *in_data,
  1180. struct wpabuf **out_data)
  1181. {
  1182. struct wpabuf *in_decrypted = NULL;
  1183. int retval = 0;
  1184. struct ttls_parse_avp parse;
  1185. os_memset(&parse, 0, sizeof(parse));
  1186. wpa_printf(MSG_DEBUG, "EAP-TTLS: received %lu bytes encrypted data for"
  1187. " Phase 2",
  1188. in_data ? (unsigned long) wpabuf_len(in_data) : 0);
  1189. if (data->pending_phase2_req) {
  1190. wpa_printf(MSG_DEBUG, "EAP-TTLS: Pending Phase 2 request - "
  1191. "skip decryption and use old data");
  1192. /* Clear TLS reassembly state. */
  1193. eap_peer_tls_reset_input(&data->ssl);
  1194. in_decrypted = data->pending_phase2_req;
  1195. data->pending_phase2_req = NULL;
  1196. if (wpabuf_len(in_decrypted) == 0) {
  1197. wpabuf_free(in_decrypted);
  1198. return eap_ttls_implicit_identity_request(
  1199. sm, data, ret, identifier, out_data);
  1200. }
  1201. goto continue_req;
  1202. }
  1203. if ((in_data == NULL || wpabuf_len(in_data) == 0) &&
  1204. data->phase2_start) {
  1205. return eap_ttls_phase2_start(sm, data, ret, identifier,
  1206. out_data);
  1207. }
  1208. if (in_data == NULL || wpabuf_len(in_data) == 0) {
  1209. /* Received TLS ACK - requesting more fragments */
  1210. return eap_peer_tls_encrypt(sm, &data->ssl, EAP_TYPE_TTLS,
  1211. data->ttls_version,
  1212. identifier, NULL, out_data);
  1213. }
  1214. retval = eap_peer_tls_decrypt(sm, &data->ssl, in_data, &in_decrypted);
  1215. if (retval)
  1216. goto done;
  1217. continue_req:
  1218. data->phase2_start = 0;
  1219. if (eap_ttls_parse_avps(in_decrypted, &parse) < 0) {
  1220. retval = -1;
  1221. goto done;
  1222. }
  1223. retval = eap_ttls_process_decrypted(sm, data, ret, identifier,
  1224. &parse, in_decrypted, out_data);
  1225. done:
  1226. wpabuf_free(in_decrypted);
  1227. os_free(parse.eapdata);
  1228. if (retval < 0) {
  1229. ret->methodState = METHOD_DONE;
  1230. ret->decision = DECISION_FAIL;
  1231. }
  1232. return retval;
  1233. }
  1234. static int eap_ttls_process_handshake(struct eap_sm *sm,
  1235. struct eap_ttls_data *data,
  1236. struct eap_method_ret *ret,
  1237. u8 identifier,
  1238. const struct wpabuf *in_data,
  1239. struct wpabuf **out_data)
  1240. {
  1241. int res;
  1242. if (sm->waiting_ext_cert_check && data->pending_resp) {
  1243. struct eap_peer_config *config = eap_get_config(sm);
  1244. if (config->pending_ext_cert_check == EXT_CERT_CHECK_GOOD) {
  1245. wpa_printf(MSG_DEBUG,
  1246. "EAP-TTLS: External certificate check succeeded - continue handshake");
  1247. *out_data = data->pending_resp;
  1248. data->pending_resp = NULL;
  1249. sm->waiting_ext_cert_check = 0;
  1250. return 0;
  1251. }
  1252. if (config->pending_ext_cert_check == EXT_CERT_CHECK_BAD) {
  1253. wpa_printf(MSG_DEBUG,
  1254. "EAP-TTLS: External certificate check failed - force authentication failure");
  1255. ret->methodState = METHOD_DONE;
  1256. ret->decision = DECISION_FAIL;
  1257. sm->waiting_ext_cert_check = 0;
  1258. return 0;
  1259. }
  1260. wpa_printf(MSG_DEBUG,
  1261. "EAP-TTLS: Continuing to wait external server certificate validation");
  1262. return 0;
  1263. }
  1264. res = eap_peer_tls_process_helper(sm, &data->ssl, EAP_TYPE_TTLS,
  1265. data->ttls_version, identifier,
  1266. in_data, out_data);
  1267. if (res < 0) {
  1268. wpa_printf(MSG_DEBUG, "EAP-TTLS: TLS processing failed");
  1269. ret->methodState = METHOD_DONE;
  1270. ret->decision = DECISION_FAIL;
  1271. return -1;
  1272. }
  1273. if (sm->waiting_ext_cert_check) {
  1274. wpa_printf(MSG_DEBUG,
  1275. "EAP-TTLS: Waiting external server certificate validation");
  1276. wpabuf_free(data->pending_resp);
  1277. data->pending_resp = *out_data;
  1278. *out_data = NULL;
  1279. return 0;
  1280. }
  1281. if (tls_connection_established(sm->ssl_ctx, data->ssl.conn)) {
  1282. wpa_printf(MSG_DEBUG, "EAP-TTLS: TLS done, proceed to "
  1283. "Phase 2");
  1284. if (data->resuming) {
  1285. wpa_printf(MSG_DEBUG, "EAP-TTLS: fast reauth - may "
  1286. "skip Phase 2");
  1287. ret->decision = DECISION_COND_SUCC;
  1288. ret->methodState = METHOD_MAY_CONT;
  1289. }
  1290. data->phase2_start = 1;
  1291. eap_ttls_v0_derive_key(sm, data);
  1292. if (*out_data == NULL || wpabuf_len(*out_data) == 0) {
  1293. if (eap_ttls_decrypt(sm, data, ret, identifier,
  1294. NULL, out_data)) {
  1295. wpa_printf(MSG_WARNING, "EAP-TTLS: "
  1296. "failed to process early "
  1297. "start for Phase 2");
  1298. }
  1299. res = 0;
  1300. }
  1301. data->resuming = 0;
  1302. }
  1303. if (res == 2) {
  1304. /*
  1305. * Application data included in the handshake message.
  1306. */
  1307. wpabuf_free(data->pending_phase2_req);
  1308. data->pending_phase2_req = *out_data;
  1309. *out_data = NULL;
  1310. res = eap_ttls_decrypt(sm, data, ret, identifier, in_data,
  1311. out_data);
  1312. }
  1313. return res;
  1314. }
  1315. static void eap_ttls_check_auth_status(struct eap_sm *sm,
  1316. struct eap_ttls_data *data,
  1317. struct eap_method_ret *ret)
  1318. {
  1319. if (ret->methodState == METHOD_DONE) {
  1320. ret->allowNotifications = FALSE;
  1321. if (ret->decision == DECISION_UNCOND_SUCC ||
  1322. ret->decision == DECISION_COND_SUCC) {
  1323. wpa_printf(MSG_DEBUG, "EAP-TTLS: Authentication "
  1324. "completed successfully");
  1325. data->phase2_success = 1;
  1326. data->decision_succ = ret->decision;
  1327. #ifdef EAP_TNC
  1328. if (!data->ready_for_tnc && !data->tnc_started) {
  1329. /*
  1330. * TNC may be required as the next
  1331. * authentication method within the tunnel.
  1332. */
  1333. ret->methodState = METHOD_MAY_CONT;
  1334. data->ready_for_tnc = 1;
  1335. }
  1336. #endif /* EAP_TNC */
  1337. }
  1338. } else if (ret->methodState == METHOD_MAY_CONT &&
  1339. (ret->decision == DECISION_UNCOND_SUCC ||
  1340. ret->decision == DECISION_COND_SUCC)) {
  1341. wpa_printf(MSG_DEBUG, "EAP-TTLS: Authentication "
  1342. "completed successfully (MAY_CONT)");
  1343. data->phase2_success = 1;
  1344. data->decision_succ = ret->decision;
  1345. } else if (data->decision_succ != DECISION_FAIL &&
  1346. data->phase2_success &&
  1347. !data->ssl.tls_out) {
  1348. /*
  1349. * This is needed to cover the case where the final Phase 2
  1350. * message gets fragmented since fragmentation clears
  1351. * decision back to FAIL.
  1352. */
  1353. wpa_printf(MSG_DEBUG,
  1354. "EAP-TTLS: Restore success decision after fragmented frame sent completely");
  1355. ret->decision = data->decision_succ;
  1356. }
  1357. }
  1358. static struct wpabuf * eap_ttls_process(struct eap_sm *sm, void *priv,
  1359. struct eap_method_ret *ret,
  1360. const struct wpabuf *reqData)
  1361. {
  1362. size_t left;
  1363. int res;
  1364. u8 flags, id;
  1365. struct wpabuf *resp;
  1366. const u8 *pos;
  1367. struct eap_ttls_data *data = priv;
  1368. struct wpabuf msg;
  1369. pos = eap_peer_tls_process_init(sm, &data->ssl, EAP_TYPE_TTLS, ret,
  1370. reqData, &left, &flags);
  1371. if (pos == NULL)
  1372. return NULL;
  1373. id = eap_get_id(reqData);
  1374. if (flags & EAP_TLS_FLAGS_START) {
  1375. wpa_printf(MSG_DEBUG, "EAP-TTLS: Start (server ver=%d, own "
  1376. "ver=%d)", flags & EAP_TLS_VERSION_MASK,
  1377. data->ttls_version);
  1378. /* RFC 5281, Ch. 9.2:
  1379. * "This packet MAY contain additional information in the form
  1380. * of AVPs, which may provide useful hints to the client"
  1381. * For now, ignore any potential extra data.
  1382. */
  1383. left = 0;
  1384. }
  1385. wpabuf_set(&msg, pos, left);
  1386. resp = NULL;
  1387. if (tls_connection_established(sm->ssl_ctx, data->ssl.conn) &&
  1388. !data->resuming) {
  1389. res = eap_ttls_decrypt(sm, data, ret, id, &msg, &resp);
  1390. } else {
  1391. res = eap_ttls_process_handshake(sm, data, ret, id,
  1392. &msg, &resp);
  1393. }
  1394. eap_ttls_check_auth_status(sm, data, ret);
  1395. /* FIX: what about res == -1? Could just move all error processing into
  1396. * the other functions and get rid of this res==1 case here. */
  1397. if (res == 1) {
  1398. wpabuf_free(resp);
  1399. return eap_peer_tls_build_ack(id, EAP_TYPE_TTLS,
  1400. data->ttls_version);
  1401. }
  1402. return resp;
  1403. }
  1404. static Boolean eap_ttls_has_reauth_data(struct eap_sm *sm, void *priv)
  1405. {
  1406. struct eap_ttls_data *data = priv;
  1407. return tls_connection_established(sm->ssl_ctx, data->ssl.conn) &&
  1408. data->phase2_success;
  1409. }
  1410. static void eap_ttls_deinit_for_reauth(struct eap_sm *sm, void *priv)
  1411. {
  1412. struct eap_ttls_data *data = priv;
  1413. wpabuf_free(data->pending_phase2_req);
  1414. data->pending_phase2_req = NULL;
  1415. wpabuf_free(data->pending_resp);
  1416. data->pending_resp = NULL;
  1417. data->decision_succ = DECISION_FAIL;
  1418. #ifdef EAP_TNC
  1419. data->ready_for_tnc = 0;
  1420. data->tnc_started = 0;
  1421. #endif /* EAP_TNC */
  1422. }
  1423. static void * eap_ttls_init_for_reauth(struct eap_sm *sm, void *priv)
  1424. {
  1425. struct eap_ttls_data *data = priv;
  1426. eap_ttls_free_key(data);
  1427. os_free(data->session_id);
  1428. data->session_id = NULL;
  1429. if (eap_peer_tls_reauth_init(sm, &data->ssl)) {
  1430. os_free(data);
  1431. return NULL;
  1432. }
  1433. if (data->phase2_priv && data->phase2_method &&
  1434. data->phase2_method->init_for_reauth)
  1435. data->phase2_method->init_for_reauth(sm, data->phase2_priv);
  1436. data->phase2_start = 0;
  1437. data->phase2_success = 0;
  1438. data->resuming = 1;
  1439. data->reauth = 1;
  1440. return priv;
  1441. }
  1442. static int eap_ttls_get_status(struct eap_sm *sm, void *priv, char *buf,
  1443. size_t buflen, int verbose)
  1444. {
  1445. struct eap_ttls_data *data = priv;
  1446. int len, ret;
  1447. len = eap_peer_tls_status(sm, &data->ssl, buf, buflen, verbose);
  1448. ret = os_snprintf(buf + len, buflen - len,
  1449. "EAP-TTLSv%d Phase2 method=",
  1450. data->ttls_version);
  1451. if (os_snprintf_error(buflen - len, ret))
  1452. return len;
  1453. len += ret;
  1454. switch (data->phase2_type) {
  1455. case EAP_TTLS_PHASE2_EAP:
  1456. ret = os_snprintf(buf + len, buflen - len, "EAP-%s\n",
  1457. data->phase2_method ?
  1458. data->phase2_method->name : "?");
  1459. break;
  1460. case EAP_TTLS_PHASE2_MSCHAPV2:
  1461. ret = os_snprintf(buf + len, buflen - len, "MSCHAPV2\n");
  1462. break;
  1463. case EAP_TTLS_PHASE2_MSCHAP:
  1464. ret = os_snprintf(buf + len, buflen - len, "MSCHAP\n");
  1465. break;
  1466. case EAP_TTLS_PHASE2_PAP:
  1467. ret = os_snprintf(buf + len, buflen - len, "PAP\n");
  1468. break;
  1469. case EAP_TTLS_PHASE2_CHAP:
  1470. ret = os_snprintf(buf + len, buflen - len, "CHAP\n");
  1471. break;
  1472. default:
  1473. ret = 0;
  1474. break;
  1475. }
  1476. if (os_snprintf_error(buflen - len, ret))
  1477. return len;
  1478. len += ret;
  1479. return len;
  1480. }
  1481. static Boolean eap_ttls_isKeyAvailable(struct eap_sm *sm, void *priv)
  1482. {
  1483. struct eap_ttls_data *data = priv;
  1484. return data->key_data != NULL && data->phase2_success;
  1485. }
  1486. static u8 * eap_ttls_getKey(struct eap_sm *sm, void *priv, size_t *len)
  1487. {
  1488. struct eap_ttls_data *data = priv;
  1489. u8 *key;
  1490. if (data->key_data == NULL || !data->phase2_success)
  1491. return NULL;
  1492. key = os_malloc(EAP_TLS_KEY_LEN);
  1493. if (key == NULL)
  1494. return NULL;
  1495. *len = EAP_TLS_KEY_LEN;
  1496. os_memcpy(key, data->key_data, EAP_TLS_KEY_LEN);
  1497. return key;
  1498. }
  1499. static u8 * eap_ttls_get_session_id(struct eap_sm *sm, void *priv, size_t *len)
  1500. {
  1501. struct eap_ttls_data *data = priv;
  1502. u8 *id;
  1503. if (data->session_id == NULL || !data->phase2_success)
  1504. return NULL;
  1505. id = os_malloc(data->id_len);
  1506. if (id == NULL)
  1507. return NULL;
  1508. *len = data->id_len;
  1509. os_memcpy(id, data->session_id, data->id_len);
  1510. return id;
  1511. }
  1512. static u8 * eap_ttls_get_emsk(struct eap_sm *sm, void *priv, size_t *len)
  1513. {
  1514. struct eap_ttls_data *data = priv;
  1515. u8 *key;
  1516. if (data->key_data == NULL)
  1517. return NULL;
  1518. key = os_malloc(EAP_EMSK_LEN);
  1519. if (key == NULL)
  1520. return NULL;
  1521. *len = EAP_EMSK_LEN;
  1522. os_memcpy(key, data->key_data + EAP_TLS_KEY_LEN, EAP_EMSK_LEN);
  1523. return key;
  1524. }
  1525. int eap_peer_ttls_register(void)
  1526. {
  1527. struct eap_method *eap;
  1528. eap = eap_peer_method_alloc(EAP_PEER_METHOD_INTERFACE_VERSION,
  1529. EAP_VENDOR_IETF, EAP_TYPE_TTLS, "TTLS");
  1530. if (eap == NULL)
  1531. return -1;
  1532. eap->init = eap_ttls_init;
  1533. eap->deinit = eap_ttls_deinit;
  1534. eap->process = eap_ttls_process;
  1535. eap->isKeyAvailable = eap_ttls_isKeyAvailable;
  1536. eap->getKey = eap_ttls_getKey;
  1537. eap->getSessionId = eap_ttls_get_session_id;
  1538. eap->get_status = eap_ttls_get_status;
  1539. eap->has_reauth_data = eap_ttls_has_reauth_data;
  1540. eap->deinit_for_reauth = eap_ttls_deinit_for_reauth;
  1541. eap->init_for_reauth = eap_ttls_init_for_reauth;
  1542. eap->get_emsk = eap_ttls_get_emsk;
  1543. return eap_peer_method_register(eap);
  1544. }