eap_ttls.c 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831
  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 || config->pending_req_sim)) {
  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. /* NT-Response */
  532. if (challenge_response(challenge, password, pos)) {
  533. wpa_printf(MSG_ERROR,
  534. "EAP-TTLS/MSCHAP: Failed derive password hash");
  535. wpabuf_free(msg);
  536. os_free(challenge);
  537. return -1;
  538. }
  539. wpa_hexdump_key(MSG_DEBUG, "EAP-TTLS: MSCHAP password hash",
  540. password, 16);
  541. } else {
  542. /* NT-Response */
  543. if (nt_challenge_response(challenge, password, password_len,
  544. pos)) {
  545. wpa_printf(MSG_ERROR,
  546. "EAP-TTLS/MSCHAP: Failed derive password");
  547. wpabuf_free(msg);
  548. os_free(challenge);
  549. return -1;
  550. }
  551. wpa_hexdump_ascii_key(MSG_DEBUG, "EAP-TTLS: MSCHAP password",
  552. password, password_len);
  553. }
  554. wpa_hexdump(MSG_DEBUG, "EAP-TTLS: MSCHAP implicit challenge",
  555. challenge, EAP_TTLS_MSCHAP_CHALLENGE_LEN);
  556. wpa_hexdump(MSG_DEBUG, "EAP-TTLS: MSCHAP response", pos, 24);
  557. pos += 24;
  558. os_free(challenge);
  559. AVP_PAD(buf, pos);
  560. wpabuf_put(msg, pos - buf);
  561. *resp = msg;
  562. /* EAP-TTLS/MSCHAP does not provide tunneled success
  563. * notification, so assume that Phase2 succeeds. */
  564. ret->methodState = METHOD_DONE;
  565. ret->decision = DECISION_COND_SUCC;
  566. return 0;
  567. #endif /* CONFIG_FIPS */
  568. }
  569. static int eap_ttls_phase2_request_pap(struct eap_sm *sm,
  570. struct eap_ttls_data *data,
  571. struct eap_method_ret *ret,
  572. struct wpabuf **resp)
  573. {
  574. struct wpabuf *msg;
  575. u8 *buf, *pos;
  576. size_t pad;
  577. const u8 *identity, *password;
  578. size_t identity_len, password_len;
  579. wpa_printf(MSG_DEBUG, "EAP-TTLS: Phase 2 PAP Request");
  580. identity = eap_get_config_identity(sm, &identity_len);
  581. password = eap_get_config_password(sm, &password_len);
  582. if (identity == NULL || password == NULL)
  583. return -1;
  584. msg = wpabuf_alloc(identity_len + password_len + 100);
  585. if (msg == NULL) {
  586. wpa_printf(MSG_ERROR,
  587. "EAP-TTLS/PAP: Failed to allocate memory");
  588. return -1;
  589. }
  590. pos = buf = wpabuf_mhead(msg);
  591. /* User-Name */
  592. pos = eap_ttls_avp_add(buf, pos, RADIUS_ATTR_USER_NAME, 0, 1,
  593. identity, identity_len);
  594. /* User-Password; in RADIUS, this is encrypted, but EAP-TTLS encrypts
  595. * the data, so no separate encryption is used in the AVP itself.
  596. * However, the password is padded to obfuscate its length. */
  597. pad = password_len == 0 ? 16 : (16 - (password_len & 15)) & 15;
  598. pos = eap_ttls_avp_hdr(pos, RADIUS_ATTR_USER_PASSWORD, 0, 1,
  599. password_len + pad);
  600. os_memcpy(pos, password, password_len);
  601. pos += password_len;
  602. os_memset(pos, 0, pad);
  603. pos += pad;
  604. AVP_PAD(buf, pos);
  605. wpabuf_put(msg, pos - buf);
  606. *resp = msg;
  607. /* EAP-TTLS/PAP does not provide tunneled success notification,
  608. * so assume that Phase2 succeeds. */
  609. ret->methodState = METHOD_DONE;
  610. ret->decision = DECISION_COND_SUCC;
  611. return 0;
  612. }
  613. static int eap_ttls_phase2_request_chap(struct eap_sm *sm,
  614. struct eap_ttls_data *data,
  615. struct eap_method_ret *ret,
  616. struct wpabuf **resp)
  617. {
  618. #ifdef CONFIG_FIPS
  619. wpa_printf(MSG_ERROR, "EAP-TTLS: CHAP not supported in FIPS build");
  620. return -1;
  621. #else /* CONFIG_FIPS */
  622. struct wpabuf *msg;
  623. u8 *buf, *pos, *challenge;
  624. const u8 *identity, *password;
  625. size_t identity_len, password_len;
  626. wpa_printf(MSG_DEBUG, "EAP-TTLS: Phase 2 CHAP Request");
  627. identity = eap_get_config_identity(sm, &identity_len);
  628. password = eap_get_config_password(sm, &password_len);
  629. if (identity == NULL || password == NULL)
  630. return -1;
  631. msg = wpabuf_alloc(identity_len + 1000);
  632. if (msg == NULL) {
  633. wpa_printf(MSG_ERROR,
  634. "EAP-TTLS/CHAP: Failed to allocate memory");
  635. return -1;
  636. }
  637. pos = buf = wpabuf_mhead(msg);
  638. /* User-Name */
  639. pos = eap_ttls_avp_add(buf, pos, RADIUS_ATTR_USER_NAME, 0, 1,
  640. identity, identity_len);
  641. /* CHAP-Challenge */
  642. challenge = eap_ttls_implicit_challenge(
  643. sm, data, EAP_TTLS_CHAP_CHALLENGE_LEN + 1);
  644. if (challenge == NULL) {
  645. wpabuf_free(msg);
  646. wpa_printf(MSG_ERROR, "EAP-TTLS/CHAP: Failed to derive "
  647. "implicit challenge");
  648. return -1;
  649. }
  650. pos = eap_ttls_avp_add(buf, pos, RADIUS_ATTR_CHAP_CHALLENGE, 0, 1,
  651. challenge, EAP_TTLS_CHAP_CHALLENGE_LEN);
  652. /* CHAP-Password */
  653. pos = eap_ttls_avp_hdr(pos, RADIUS_ATTR_CHAP_PASSWORD, 0, 1,
  654. 1 + EAP_TTLS_CHAP_PASSWORD_LEN);
  655. data->ident = challenge[EAP_TTLS_CHAP_CHALLENGE_LEN];
  656. *pos++ = data->ident;
  657. /* MD5(Ident + Password + Challenge) */
  658. chap_md5(data->ident, password, password_len, challenge,
  659. EAP_TTLS_CHAP_CHALLENGE_LEN, pos);
  660. wpa_hexdump_ascii(MSG_DEBUG, "EAP-TTLS: CHAP username",
  661. identity, identity_len);
  662. wpa_hexdump_ascii_key(MSG_DEBUG, "EAP-TTLS: CHAP password",
  663. password, password_len);
  664. wpa_hexdump(MSG_DEBUG, "EAP-TTLS: CHAP implicit challenge",
  665. challenge, EAP_TTLS_CHAP_CHALLENGE_LEN);
  666. wpa_hexdump(MSG_DEBUG, "EAP-TTLS: CHAP password",
  667. pos, EAP_TTLS_CHAP_PASSWORD_LEN);
  668. pos += EAP_TTLS_CHAP_PASSWORD_LEN;
  669. os_free(challenge);
  670. AVP_PAD(buf, pos);
  671. wpabuf_put(msg, pos - buf);
  672. *resp = msg;
  673. /* EAP-TTLS/CHAP does not provide tunneled success
  674. * notification, so assume that Phase2 succeeds. */
  675. ret->methodState = METHOD_DONE;
  676. ret->decision = DECISION_COND_SUCC;
  677. return 0;
  678. #endif /* CONFIG_FIPS */
  679. }
  680. static int eap_ttls_phase2_request(struct eap_sm *sm,
  681. struct eap_ttls_data *data,
  682. struct eap_method_ret *ret,
  683. struct eap_hdr *hdr,
  684. struct wpabuf **resp)
  685. {
  686. int res = 0;
  687. size_t len;
  688. enum phase2_types phase2_type = data->phase2_type;
  689. #ifdef EAP_TNC
  690. if (data->tnc_started) {
  691. wpa_printf(MSG_DEBUG, "EAP-TTLS: Processing TNC");
  692. phase2_type = EAP_TTLS_PHASE2_EAP;
  693. }
  694. #endif /* EAP_TNC */
  695. if (phase2_type == EAP_TTLS_PHASE2_MSCHAPV2 ||
  696. phase2_type == EAP_TTLS_PHASE2_MSCHAP ||
  697. phase2_type == EAP_TTLS_PHASE2_PAP ||
  698. phase2_type == EAP_TTLS_PHASE2_CHAP) {
  699. if (eap_get_config_identity(sm, &len) == NULL) {
  700. wpa_printf(MSG_INFO,
  701. "EAP-TTLS: Identity not configured");
  702. eap_sm_request_identity(sm);
  703. if (eap_get_config_password(sm, &len) == NULL)
  704. eap_sm_request_password(sm);
  705. return 0;
  706. }
  707. if (eap_get_config_password(sm, &len) == NULL) {
  708. wpa_printf(MSG_INFO,
  709. "EAP-TTLS: Password not configured");
  710. eap_sm_request_password(sm);
  711. return 0;
  712. }
  713. }
  714. switch (phase2_type) {
  715. case EAP_TTLS_PHASE2_EAP:
  716. res = eap_ttls_phase2_request_eap(sm, data, ret, hdr, resp);
  717. break;
  718. case EAP_TTLS_PHASE2_MSCHAPV2:
  719. res = eap_ttls_phase2_request_mschapv2(sm, data, ret, resp);
  720. break;
  721. case EAP_TTLS_PHASE2_MSCHAP:
  722. res = eap_ttls_phase2_request_mschap(sm, data, ret, resp);
  723. break;
  724. case EAP_TTLS_PHASE2_PAP:
  725. res = eap_ttls_phase2_request_pap(sm, data, ret, resp);
  726. break;
  727. case EAP_TTLS_PHASE2_CHAP:
  728. res = eap_ttls_phase2_request_chap(sm, data, ret, resp);
  729. break;
  730. default:
  731. wpa_printf(MSG_ERROR, "EAP-TTLS: Phase 2 - Unknown");
  732. res = -1;
  733. break;
  734. }
  735. if (res < 0) {
  736. ret->methodState = METHOD_DONE;
  737. ret->decision = DECISION_FAIL;
  738. }
  739. return res;
  740. }
  741. struct ttls_parse_avp {
  742. u8 *mschapv2;
  743. u8 *eapdata;
  744. size_t eap_len;
  745. int mschapv2_error;
  746. };
  747. static int eap_ttls_parse_attr_eap(const u8 *dpos, size_t dlen,
  748. struct ttls_parse_avp *parse)
  749. {
  750. wpa_printf(MSG_DEBUG, "EAP-TTLS: AVP - EAP Message");
  751. if (parse->eapdata == NULL) {
  752. parse->eapdata = os_memdup(dpos, dlen);
  753. if (parse->eapdata == NULL) {
  754. wpa_printf(MSG_WARNING, "EAP-TTLS: Failed to allocate "
  755. "memory for Phase 2 EAP data");
  756. return -1;
  757. }
  758. parse->eap_len = dlen;
  759. } else {
  760. u8 *neweap = os_realloc(parse->eapdata, parse->eap_len + dlen);
  761. if (neweap == NULL) {
  762. wpa_printf(MSG_WARNING, "EAP-TTLS: Failed to allocate "
  763. "memory for Phase 2 EAP data");
  764. return -1;
  765. }
  766. os_memcpy(neweap + parse->eap_len, dpos, dlen);
  767. parse->eapdata = neweap;
  768. parse->eap_len += dlen;
  769. }
  770. return 0;
  771. }
  772. static int eap_ttls_parse_avp(u8 *pos, size_t left,
  773. struct ttls_parse_avp *parse)
  774. {
  775. struct ttls_avp *avp;
  776. u32 avp_code, avp_length, vendor_id = 0;
  777. u8 avp_flags, *dpos;
  778. size_t dlen;
  779. avp = (struct ttls_avp *) pos;
  780. avp_code = be_to_host32(avp->avp_code);
  781. avp_length = be_to_host32(avp->avp_length);
  782. avp_flags = (avp_length >> 24) & 0xff;
  783. avp_length &= 0xffffff;
  784. wpa_printf(MSG_DEBUG, "EAP-TTLS: AVP: code=%d flags=0x%02x "
  785. "length=%d", (int) avp_code, avp_flags,
  786. (int) avp_length);
  787. if (avp_length > left) {
  788. wpa_printf(MSG_WARNING, "EAP-TTLS: AVP overflow "
  789. "(len=%d, left=%lu) - dropped",
  790. (int) avp_length, (unsigned long) left);
  791. return -1;
  792. }
  793. if (avp_length < sizeof(*avp)) {
  794. wpa_printf(MSG_WARNING, "EAP-TTLS: Invalid AVP length %d",
  795. avp_length);
  796. return -1;
  797. }
  798. dpos = (u8 *) (avp + 1);
  799. dlen = avp_length - sizeof(*avp);
  800. if (avp_flags & AVP_FLAGS_VENDOR) {
  801. if (dlen < 4) {
  802. wpa_printf(MSG_WARNING, "EAP-TTLS: Vendor AVP "
  803. "underflow");
  804. return -1;
  805. }
  806. vendor_id = WPA_GET_BE32(dpos);
  807. wpa_printf(MSG_DEBUG, "EAP-TTLS: AVP vendor_id %d",
  808. (int) vendor_id);
  809. dpos += 4;
  810. dlen -= 4;
  811. }
  812. wpa_hexdump(MSG_DEBUG, "EAP-TTLS: AVP data", dpos, dlen);
  813. if (vendor_id == 0 && avp_code == RADIUS_ATTR_EAP_MESSAGE) {
  814. if (eap_ttls_parse_attr_eap(dpos, dlen, parse) < 0)
  815. return -1;
  816. } else if (vendor_id == 0 && avp_code == RADIUS_ATTR_REPLY_MESSAGE) {
  817. /* This is an optional message that can be displayed to
  818. * the user. */
  819. wpa_hexdump_ascii(MSG_DEBUG, "EAP-TTLS: AVP - Reply-Message",
  820. dpos, dlen);
  821. } else if (vendor_id == RADIUS_VENDOR_ID_MICROSOFT &&
  822. avp_code == RADIUS_ATTR_MS_CHAP2_SUCCESS) {
  823. wpa_hexdump_ascii(MSG_DEBUG, "EAP-TTLS: MS-CHAP2-Success",
  824. dpos, dlen);
  825. if (dlen != 43) {
  826. wpa_printf(MSG_WARNING, "EAP-TTLS: Unexpected "
  827. "MS-CHAP2-Success length "
  828. "(len=%lu, expected 43)",
  829. (unsigned long) dlen);
  830. return -1;
  831. }
  832. parse->mschapv2 = dpos;
  833. } else if (vendor_id == RADIUS_VENDOR_ID_MICROSOFT &&
  834. avp_code == RADIUS_ATTR_MS_CHAP_ERROR) {
  835. wpa_hexdump_ascii(MSG_DEBUG, "EAP-TTLS: MS-CHAP-Error",
  836. dpos, dlen);
  837. parse->mschapv2_error = 1;
  838. } else if (avp_flags & AVP_FLAGS_MANDATORY) {
  839. wpa_printf(MSG_WARNING, "EAP-TTLS: Unsupported mandatory AVP "
  840. "code %d vendor_id %d - dropped",
  841. (int) avp_code, (int) vendor_id);
  842. return -1;
  843. } else {
  844. wpa_printf(MSG_DEBUG, "EAP-TTLS: Ignoring unsupported AVP "
  845. "code %d vendor_id %d",
  846. (int) avp_code, (int) vendor_id);
  847. }
  848. return avp_length;
  849. }
  850. static int eap_ttls_parse_avps(struct wpabuf *in_decrypted,
  851. struct ttls_parse_avp *parse)
  852. {
  853. u8 *pos;
  854. size_t left, pad;
  855. int avp_length;
  856. pos = wpabuf_mhead(in_decrypted);
  857. left = wpabuf_len(in_decrypted);
  858. wpa_hexdump(MSG_DEBUG, "EAP-TTLS: Decrypted Phase 2 AVPs", pos, left);
  859. if (left < sizeof(struct ttls_avp)) {
  860. wpa_printf(MSG_WARNING, "EAP-TTLS: Too short Phase 2 AVP frame"
  861. " len=%lu expected %lu or more - dropped",
  862. (unsigned long) left,
  863. (unsigned long) sizeof(struct ttls_avp));
  864. return -1;
  865. }
  866. /* Parse AVPs */
  867. os_memset(parse, 0, sizeof(*parse));
  868. while (left > 0) {
  869. avp_length = eap_ttls_parse_avp(pos, left, parse);
  870. if (avp_length < 0)
  871. return -1;
  872. pad = (4 - (avp_length & 3)) & 3;
  873. pos += avp_length + pad;
  874. if (left < avp_length + pad)
  875. left = 0;
  876. else
  877. left -= avp_length + pad;
  878. }
  879. return 0;
  880. }
  881. static u8 * eap_ttls_fake_identity_request(void)
  882. {
  883. struct eap_hdr *hdr;
  884. u8 *buf;
  885. wpa_printf(MSG_DEBUG, "EAP-TTLS: empty data in beginning of "
  886. "Phase 2 - use fake EAP-Request Identity");
  887. buf = os_malloc(sizeof(*hdr) + 1);
  888. if (buf == NULL) {
  889. wpa_printf(MSG_WARNING, "EAP-TTLS: failed to allocate "
  890. "memory for fake EAP-Identity Request");
  891. return NULL;
  892. }
  893. hdr = (struct eap_hdr *) buf;
  894. hdr->code = EAP_CODE_REQUEST;
  895. hdr->identifier = 0;
  896. hdr->length = host_to_be16(sizeof(*hdr) + 1);
  897. buf[sizeof(*hdr)] = EAP_TYPE_IDENTITY;
  898. return buf;
  899. }
  900. static int eap_ttls_encrypt_response(struct eap_sm *sm,
  901. struct eap_ttls_data *data,
  902. struct wpabuf *resp, u8 identifier,
  903. struct wpabuf **out_data)
  904. {
  905. if (resp == NULL)
  906. return 0;
  907. wpa_hexdump_buf_key(MSG_DEBUG, "EAP-TTLS: Encrypting Phase 2 data",
  908. resp);
  909. if (eap_peer_tls_encrypt(sm, &data->ssl, EAP_TYPE_TTLS,
  910. data->ttls_version, identifier,
  911. resp, out_data)) {
  912. wpa_printf(MSG_INFO, "EAP-TTLS: Failed to encrypt a Phase 2 "
  913. "frame");
  914. wpabuf_free(resp);
  915. return -1;
  916. }
  917. wpabuf_free(resp);
  918. return 0;
  919. }
  920. static int eap_ttls_process_phase2_eap(struct eap_sm *sm,
  921. struct eap_ttls_data *data,
  922. struct eap_method_ret *ret,
  923. struct ttls_parse_avp *parse,
  924. struct wpabuf **resp)
  925. {
  926. struct eap_hdr *hdr;
  927. size_t len;
  928. if (parse->eapdata == NULL) {
  929. wpa_printf(MSG_WARNING, "EAP-TTLS: No EAP Message in the "
  930. "packet - dropped");
  931. return -1;
  932. }
  933. wpa_hexdump(MSG_DEBUG, "EAP-TTLS: Phase 2 EAP",
  934. parse->eapdata, parse->eap_len);
  935. hdr = (struct eap_hdr *) parse->eapdata;
  936. if (parse->eap_len < sizeof(*hdr)) {
  937. wpa_printf(MSG_WARNING, "EAP-TTLS: Too short Phase 2 EAP "
  938. "frame (len=%lu, expected %lu or more) - dropped",
  939. (unsigned long) parse->eap_len,
  940. (unsigned long) sizeof(*hdr));
  941. return -1;
  942. }
  943. len = be_to_host16(hdr->length);
  944. if (len > parse->eap_len) {
  945. wpa_printf(MSG_INFO, "EAP-TTLS: Length mismatch in Phase 2 "
  946. "EAP frame (EAP hdr len=%lu, EAP data len in "
  947. "AVP=%lu)",
  948. (unsigned long) len,
  949. (unsigned long) parse->eap_len);
  950. return -1;
  951. }
  952. wpa_printf(MSG_DEBUG, "EAP-TTLS: received Phase 2: code=%d "
  953. "identifier=%d length=%lu",
  954. hdr->code, hdr->identifier, (unsigned long) len);
  955. switch (hdr->code) {
  956. case EAP_CODE_REQUEST:
  957. if (eap_ttls_phase2_request(sm, data, ret, hdr, resp)) {
  958. wpa_printf(MSG_INFO, "EAP-TTLS: Phase2 Request "
  959. "processing failed");
  960. return -1;
  961. }
  962. break;
  963. default:
  964. wpa_printf(MSG_INFO, "EAP-TTLS: Unexpected code=%d in "
  965. "Phase 2 EAP header", hdr->code);
  966. return -1;
  967. }
  968. return 0;
  969. }
  970. static int eap_ttls_process_phase2_mschapv2(struct eap_sm *sm,
  971. struct eap_ttls_data *data,
  972. struct eap_method_ret *ret,
  973. struct ttls_parse_avp *parse)
  974. {
  975. #ifdef EAP_MSCHAPv2
  976. if (parse->mschapv2_error) {
  977. wpa_printf(MSG_DEBUG, "EAP-TTLS/MSCHAPV2: Received "
  978. "MS-CHAP-Error - failed");
  979. ret->methodState = METHOD_DONE;
  980. ret->decision = DECISION_FAIL;
  981. /* Reply with empty data to ACK error */
  982. return 1;
  983. }
  984. if (parse->mschapv2 == NULL) {
  985. #ifdef EAP_TNC
  986. if (data->phase2_success && parse->eapdata) {
  987. /*
  988. * Allow EAP-TNC to be started after successfully
  989. * completed MSCHAPV2.
  990. */
  991. return 1;
  992. }
  993. #endif /* EAP_TNC */
  994. wpa_printf(MSG_WARNING, "EAP-TTLS: no MS-CHAP2-Success AVP "
  995. "received for Phase2 MSCHAPV2");
  996. return -1;
  997. }
  998. if (parse->mschapv2[0] != data->ident) {
  999. wpa_printf(MSG_WARNING, "EAP-TTLS: Ident mismatch for Phase 2 "
  1000. "MSCHAPV2 (received Ident 0x%02x, expected 0x%02x)",
  1001. parse->mschapv2[0], data->ident);
  1002. return -1;
  1003. }
  1004. if (!data->auth_response_valid ||
  1005. mschapv2_verify_auth_response(data->auth_response,
  1006. parse->mschapv2 + 1, 42)) {
  1007. wpa_printf(MSG_WARNING, "EAP-TTLS: Invalid authenticator "
  1008. "response in Phase 2 MSCHAPV2 success request");
  1009. return -1;
  1010. }
  1011. wpa_printf(MSG_INFO, "EAP-TTLS: Phase 2 MSCHAPV2 "
  1012. "authentication succeeded");
  1013. ret->methodState = METHOD_DONE;
  1014. ret->decision = DECISION_UNCOND_SUCC;
  1015. data->phase2_success = 1;
  1016. /*
  1017. * Reply with empty data; authentication server will reply
  1018. * with EAP-Success after this.
  1019. */
  1020. return 1;
  1021. #else /* EAP_MSCHAPv2 */
  1022. wpa_printf(MSG_ERROR, "EAP-TTLS: MSCHAPv2 not included in the build");
  1023. return -1;
  1024. #endif /* EAP_MSCHAPv2 */
  1025. }
  1026. #ifdef EAP_TNC
  1027. static int eap_ttls_process_tnc_start(struct eap_sm *sm,
  1028. struct eap_ttls_data *data,
  1029. struct eap_method_ret *ret,
  1030. struct ttls_parse_avp *parse,
  1031. struct wpabuf **resp)
  1032. {
  1033. /* TNC uses inner EAP method after non-EAP TTLS phase 2. */
  1034. if (parse->eapdata == NULL) {
  1035. wpa_printf(MSG_INFO, "EAP-TTLS: Phase 2 received "
  1036. "unexpected tunneled data (no EAP)");
  1037. return -1;
  1038. }
  1039. if (!data->ready_for_tnc) {
  1040. wpa_printf(MSG_INFO, "EAP-TTLS: Phase 2 received "
  1041. "EAP after non-EAP, but not ready for TNC");
  1042. return -1;
  1043. }
  1044. wpa_printf(MSG_DEBUG, "EAP-TTLS: Start TNC after completed "
  1045. "non-EAP method");
  1046. data->tnc_started = 1;
  1047. if (eap_ttls_process_phase2_eap(sm, data, ret, parse, resp) < 0)
  1048. return -1;
  1049. return 0;
  1050. }
  1051. #endif /* EAP_TNC */
  1052. static int eap_ttls_process_decrypted(struct eap_sm *sm,
  1053. struct eap_ttls_data *data,
  1054. struct eap_method_ret *ret,
  1055. u8 identifier,
  1056. struct ttls_parse_avp *parse,
  1057. struct wpabuf *in_decrypted,
  1058. struct wpabuf **out_data)
  1059. {
  1060. struct wpabuf *resp = NULL;
  1061. struct eap_peer_config *config = eap_get_config(sm);
  1062. int res;
  1063. enum phase2_types phase2_type = data->phase2_type;
  1064. #ifdef EAP_TNC
  1065. if (data->tnc_started)
  1066. phase2_type = EAP_TTLS_PHASE2_EAP;
  1067. #endif /* EAP_TNC */
  1068. switch (phase2_type) {
  1069. case EAP_TTLS_PHASE2_EAP:
  1070. if (eap_ttls_process_phase2_eap(sm, data, ret, parse, &resp) <
  1071. 0)
  1072. return -1;
  1073. break;
  1074. case EAP_TTLS_PHASE2_MSCHAPV2:
  1075. res = eap_ttls_process_phase2_mschapv2(sm, data, ret, parse);
  1076. #ifdef EAP_TNC
  1077. if (res == 1 && parse->eapdata && data->phase2_success) {
  1078. /*
  1079. * TNC may be required as the next
  1080. * authentication method within the tunnel.
  1081. */
  1082. ret->methodState = METHOD_MAY_CONT;
  1083. data->ready_for_tnc = 1;
  1084. if (eap_ttls_process_tnc_start(sm, data, ret, parse,
  1085. &resp) == 0)
  1086. break;
  1087. }
  1088. #endif /* EAP_TNC */
  1089. return res;
  1090. case EAP_TTLS_PHASE2_MSCHAP:
  1091. case EAP_TTLS_PHASE2_PAP:
  1092. case EAP_TTLS_PHASE2_CHAP:
  1093. #ifdef EAP_TNC
  1094. if (eap_ttls_process_tnc_start(sm, data, ret, parse, &resp) <
  1095. 0)
  1096. return -1;
  1097. break;
  1098. #else /* EAP_TNC */
  1099. /* EAP-TTLS/{MSCHAP,PAP,CHAP} should not send any TLS tunneled
  1100. * requests to the supplicant */
  1101. wpa_printf(MSG_INFO, "EAP-TTLS: Phase 2 received unexpected "
  1102. "tunneled data");
  1103. return -1;
  1104. #endif /* EAP_TNC */
  1105. }
  1106. if (resp) {
  1107. if (eap_ttls_encrypt_response(sm, data, resp, identifier,
  1108. out_data) < 0)
  1109. return -1;
  1110. } else if (config->pending_req_identity ||
  1111. config->pending_req_password ||
  1112. config->pending_req_otp ||
  1113. config->pending_req_new_password ||
  1114. config->pending_req_sim) {
  1115. wpabuf_free(data->pending_phase2_req);
  1116. data->pending_phase2_req = wpabuf_dup(in_decrypted);
  1117. }
  1118. return 0;
  1119. }
  1120. static int eap_ttls_implicit_identity_request(struct eap_sm *sm,
  1121. struct eap_ttls_data *data,
  1122. struct eap_method_ret *ret,
  1123. u8 identifier,
  1124. struct wpabuf **out_data)
  1125. {
  1126. int retval = 0;
  1127. struct eap_hdr *hdr;
  1128. struct wpabuf *resp;
  1129. hdr = (struct eap_hdr *) eap_ttls_fake_identity_request();
  1130. if (hdr == NULL) {
  1131. ret->methodState = METHOD_DONE;
  1132. ret->decision = DECISION_FAIL;
  1133. return -1;
  1134. }
  1135. resp = NULL;
  1136. if (eap_ttls_phase2_request(sm, data, ret, hdr, &resp)) {
  1137. wpa_printf(MSG_INFO, "EAP-TTLS: Phase2 Request "
  1138. "processing failed");
  1139. retval = -1;
  1140. } else {
  1141. struct eap_peer_config *config = eap_get_config(sm);
  1142. if (resp == NULL &&
  1143. (config->pending_req_identity ||
  1144. config->pending_req_password ||
  1145. config->pending_req_otp ||
  1146. config->pending_req_new_password ||
  1147. config->pending_req_sim)) {
  1148. /*
  1149. * Use empty buffer to force implicit request
  1150. * processing when EAP request is re-processed after
  1151. * user input.
  1152. */
  1153. wpabuf_free(data->pending_phase2_req);
  1154. data->pending_phase2_req = wpabuf_alloc(0);
  1155. }
  1156. retval = eap_ttls_encrypt_response(sm, data, resp, identifier,
  1157. out_data);
  1158. }
  1159. os_free(hdr);
  1160. if (retval < 0) {
  1161. ret->methodState = METHOD_DONE;
  1162. ret->decision = DECISION_FAIL;
  1163. }
  1164. return retval;
  1165. }
  1166. static int eap_ttls_phase2_start(struct eap_sm *sm, struct eap_ttls_data *data,
  1167. struct eap_method_ret *ret, u8 identifier,
  1168. struct wpabuf **out_data)
  1169. {
  1170. data->phase2_start = 0;
  1171. /*
  1172. * EAP-TTLS does not use Phase2 on fast re-auth; this must be done only
  1173. * if TLS part was indeed resuming a previous session. Most
  1174. * Authentication Servers terminate EAP-TTLS before reaching this
  1175. * point, but some do not. Make wpa_supplicant stop phase 2 here, if
  1176. * needed.
  1177. */
  1178. if (data->reauth &&
  1179. tls_connection_resumed(sm->ssl_ctx, data->ssl.conn)) {
  1180. wpa_printf(MSG_DEBUG, "EAP-TTLS: Session resumption - "
  1181. "skip phase 2");
  1182. *out_data = eap_peer_tls_build_ack(identifier, EAP_TYPE_TTLS,
  1183. data->ttls_version);
  1184. ret->methodState = METHOD_DONE;
  1185. ret->decision = DECISION_UNCOND_SUCC;
  1186. data->phase2_success = 1;
  1187. return 0;
  1188. }
  1189. return eap_ttls_implicit_identity_request(sm, data, ret, identifier,
  1190. out_data);
  1191. }
  1192. static int eap_ttls_decrypt(struct eap_sm *sm, struct eap_ttls_data *data,
  1193. struct eap_method_ret *ret, u8 identifier,
  1194. const struct wpabuf *in_data,
  1195. struct wpabuf **out_data)
  1196. {
  1197. struct wpabuf *in_decrypted = NULL;
  1198. int retval = 0;
  1199. struct ttls_parse_avp parse;
  1200. os_memset(&parse, 0, sizeof(parse));
  1201. wpa_printf(MSG_DEBUG, "EAP-TTLS: received %lu bytes encrypted data for"
  1202. " Phase 2",
  1203. in_data ? (unsigned long) wpabuf_len(in_data) : 0);
  1204. if (data->pending_phase2_req) {
  1205. wpa_printf(MSG_DEBUG, "EAP-TTLS: Pending Phase 2 request - "
  1206. "skip decryption and use old data");
  1207. /* Clear TLS reassembly state. */
  1208. eap_peer_tls_reset_input(&data->ssl);
  1209. in_decrypted = data->pending_phase2_req;
  1210. data->pending_phase2_req = NULL;
  1211. if (wpabuf_len(in_decrypted) == 0) {
  1212. wpabuf_free(in_decrypted);
  1213. return eap_ttls_implicit_identity_request(
  1214. sm, data, ret, identifier, out_data);
  1215. }
  1216. goto continue_req;
  1217. }
  1218. if ((in_data == NULL || wpabuf_len(in_data) == 0) &&
  1219. data->phase2_start) {
  1220. return eap_ttls_phase2_start(sm, data, ret, identifier,
  1221. out_data);
  1222. }
  1223. if (in_data == NULL || wpabuf_len(in_data) == 0) {
  1224. /* Received TLS ACK - requesting more fragments */
  1225. return eap_peer_tls_encrypt(sm, &data->ssl, EAP_TYPE_TTLS,
  1226. data->ttls_version,
  1227. identifier, NULL, out_data);
  1228. }
  1229. retval = eap_peer_tls_decrypt(sm, &data->ssl, in_data, &in_decrypted);
  1230. if (retval)
  1231. goto done;
  1232. continue_req:
  1233. data->phase2_start = 0;
  1234. if (eap_ttls_parse_avps(in_decrypted, &parse) < 0) {
  1235. retval = -1;
  1236. goto done;
  1237. }
  1238. retval = eap_ttls_process_decrypted(sm, data, ret, identifier,
  1239. &parse, in_decrypted, out_data);
  1240. done:
  1241. wpabuf_free(in_decrypted);
  1242. os_free(parse.eapdata);
  1243. if (retval < 0) {
  1244. ret->methodState = METHOD_DONE;
  1245. ret->decision = DECISION_FAIL;
  1246. }
  1247. return retval;
  1248. }
  1249. static int eap_ttls_process_handshake(struct eap_sm *sm,
  1250. struct eap_ttls_data *data,
  1251. struct eap_method_ret *ret,
  1252. u8 identifier,
  1253. const struct wpabuf *in_data,
  1254. struct wpabuf **out_data)
  1255. {
  1256. int res;
  1257. if (sm->waiting_ext_cert_check && data->pending_resp) {
  1258. struct eap_peer_config *config = eap_get_config(sm);
  1259. if (config->pending_ext_cert_check == EXT_CERT_CHECK_GOOD) {
  1260. wpa_printf(MSG_DEBUG,
  1261. "EAP-TTLS: External certificate check succeeded - continue handshake");
  1262. *out_data = data->pending_resp;
  1263. data->pending_resp = NULL;
  1264. sm->waiting_ext_cert_check = 0;
  1265. return 0;
  1266. }
  1267. if (config->pending_ext_cert_check == EXT_CERT_CHECK_BAD) {
  1268. wpa_printf(MSG_DEBUG,
  1269. "EAP-TTLS: External certificate check failed - force authentication failure");
  1270. ret->methodState = METHOD_DONE;
  1271. ret->decision = DECISION_FAIL;
  1272. sm->waiting_ext_cert_check = 0;
  1273. return 0;
  1274. }
  1275. wpa_printf(MSG_DEBUG,
  1276. "EAP-TTLS: Continuing to wait external server certificate validation");
  1277. return 0;
  1278. }
  1279. res = eap_peer_tls_process_helper(sm, &data->ssl, EAP_TYPE_TTLS,
  1280. data->ttls_version, identifier,
  1281. in_data, out_data);
  1282. if (res < 0) {
  1283. wpa_printf(MSG_DEBUG, "EAP-TTLS: TLS processing failed");
  1284. ret->methodState = METHOD_DONE;
  1285. ret->decision = DECISION_FAIL;
  1286. return -1;
  1287. }
  1288. if (sm->waiting_ext_cert_check) {
  1289. wpa_printf(MSG_DEBUG,
  1290. "EAP-TTLS: Waiting external server certificate validation");
  1291. wpabuf_free(data->pending_resp);
  1292. data->pending_resp = *out_data;
  1293. *out_data = NULL;
  1294. return 0;
  1295. }
  1296. if (tls_connection_established(sm->ssl_ctx, data->ssl.conn)) {
  1297. wpa_printf(MSG_DEBUG, "EAP-TTLS: TLS done, proceed to "
  1298. "Phase 2");
  1299. if (data->resuming) {
  1300. wpa_printf(MSG_DEBUG, "EAP-TTLS: fast reauth - may "
  1301. "skip Phase 2");
  1302. ret->decision = DECISION_COND_SUCC;
  1303. ret->methodState = METHOD_MAY_CONT;
  1304. }
  1305. data->phase2_start = 1;
  1306. eap_ttls_v0_derive_key(sm, data);
  1307. if (*out_data == NULL || wpabuf_len(*out_data) == 0) {
  1308. if (eap_ttls_decrypt(sm, data, ret, identifier,
  1309. NULL, out_data)) {
  1310. wpa_printf(MSG_WARNING, "EAP-TTLS: "
  1311. "failed to process early "
  1312. "start for Phase 2");
  1313. }
  1314. res = 0;
  1315. }
  1316. data->resuming = 0;
  1317. }
  1318. if (res == 2) {
  1319. /*
  1320. * Application data included in the handshake message.
  1321. */
  1322. wpabuf_free(data->pending_phase2_req);
  1323. data->pending_phase2_req = *out_data;
  1324. *out_data = NULL;
  1325. res = eap_ttls_decrypt(sm, data, ret, identifier, in_data,
  1326. out_data);
  1327. }
  1328. return res;
  1329. }
  1330. static void eap_ttls_check_auth_status(struct eap_sm *sm,
  1331. struct eap_ttls_data *data,
  1332. struct eap_method_ret *ret)
  1333. {
  1334. if (ret->methodState == METHOD_DONE) {
  1335. ret->allowNotifications = FALSE;
  1336. if (ret->decision == DECISION_UNCOND_SUCC ||
  1337. ret->decision == DECISION_COND_SUCC) {
  1338. wpa_printf(MSG_DEBUG, "EAP-TTLS: Authentication "
  1339. "completed successfully");
  1340. data->phase2_success = 1;
  1341. data->decision_succ = ret->decision;
  1342. #ifdef EAP_TNC
  1343. if (!data->ready_for_tnc && !data->tnc_started) {
  1344. /*
  1345. * TNC may be required as the next
  1346. * authentication method within the tunnel.
  1347. */
  1348. ret->methodState = METHOD_MAY_CONT;
  1349. data->ready_for_tnc = 1;
  1350. }
  1351. #endif /* EAP_TNC */
  1352. }
  1353. } else if (ret->methodState == METHOD_MAY_CONT &&
  1354. (ret->decision == DECISION_UNCOND_SUCC ||
  1355. ret->decision == DECISION_COND_SUCC)) {
  1356. wpa_printf(MSG_DEBUG, "EAP-TTLS: Authentication "
  1357. "completed successfully (MAY_CONT)");
  1358. data->phase2_success = 1;
  1359. data->decision_succ = ret->decision;
  1360. } else if (data->decision_succ != DECISION_FAIL &&
  1361. data->phase2_success &&
  1362. !data->ssl.tls_out) {
  1363. /*
  1364. * This is needed to cover the case where the final Phase 2
  1365. * message gets fragmented since fragmentation clears
  1366. * decision back to FAIL.
  1367. */
  1368. wpa_printf(MSG_DEBUG,
  1369. "EAP-TTLS: Restore success decision after fragmented frame sent completely");
  1370. ret->decision = data->decision_succ;
  1371. }
  1372. }
  1373. static struct wpabuf * eap_ttls_process(struct eap_sm *sm, void *priv,
  1374. struct eap_method_ret *ret,
  1375. const struct wpabuf *reqData)
  1376. {
  1377. size_t left;
  1378. int res;
  1379. u8 flags, id;
  1380. struct wpabuf *resp;
  1381. const u8 *pos;
  1382. struct eap_ttls_data *data = priv;
  1383. struct wpabuf msg;
  1384. pos = eap_peer_tls_process_init(sm, &data->ssl, EAP_TYPE_TTLS, ret,
  1385. reqData, &left, &flags);
  1386. if (pos == NULL)
  1387. return NULL;
  1388. id = eap_get_id(reqData);
  1389. if (flags & EAP_TLS_FLAGS_START) {
  1390. wpa_printf(MSG_DEBUG, "EAP-TTLS: Start (server ver=%d, own "
  1391. "ver=%d)", flags & EAP_TLS_VERSION_MASK,
  1392. data->ttls_version);
  1393. /* RFC 5281, Ch. 9.2:
  1394. * "This packet MAY contain additional information in the form
  1395. * of AVPs, which may provide useful hints to the client"
  1396. * For now, ignore any potential extra data.
  1397. */
  1398. left = 0;
  1399. }
  1400. wpabuf_set(&msg, pos, left);
  1401. resp = NULL;
  1402. if (tls_connection_established(sm->ssl_ctx, data->ssl.conn) &&
  1403. !data->resuming) {
  1404. res = eap_ttls_decrypt(sm, data, ret, id, &msg, &resp);
  1405. } else {
  1406. res = eap_ttls_process_handshake(sm, data, ret, id,
  1407. &msg, &resp);
  1408. }
  1409. eap_ttls_check_auth_status(sm, data, ret);
  1410. /* FIX: what about res == -1? Could just move all error processing into
  1411. * the other functions and get rid of this res==1 case here. */
  1412. if (res == 1) {
  1413. wpabuf_free(resp);
  1414. return eap_peer_tls_build_ack(id, EAP_TYPE_TTLS,
  1415. data->ttls_version);
  1416. }
  1417. return resp;
  1418. }
  1419. static Boolean eap_ttls_has_reauth_data(struct eap_sm *sm, void *priv)
  1420. {
  1421. struct eap_ttls_data *data = priv;
  1422. return tls_connection_established(sm->ssl_ctx, data->ssl.conn) &&
  1423. data->phase2_success;
  1424. }
  1425. static void eap_ttls_deinit_for_reauth(struct eap_sm *sm, void *priv)
  1426. {
  1427. struct eap_ttls_data *data = priv;
  1428. if (data->phase2_priv && data->phase2_method &&
  1429. data->phase2_method->deinit_for_reauth)
  1430. data->phase2_method->deinit_for_reauth(sm, data->phase2_priv);
  1431. wpabuf_free(data->pending_phase2_req);
  1432. data->pending_phase2_req = NULL;
  1433. wpabuf_free(data->pending_resp);
  1434. data->pending_resp = NULL;
  1435. data->decision_succ = DECISION_FAIL;
  1436. #ifdef EAP_TNC
  1437. data->ready_for_tnc = 0;
  1438. data->tnc_started = 0;
  1439. #endif /* EAP_TNC */
  1440. }
  1441. static void * eap_ttls_init_for_reauth(struct eap_sm *sm, void *priv)
  1442. {
  1443. struct eap_ttls_data *data = priv;
  1444. eap_ttls_free_key(data);
  1445. os_free(data->session_id);
  1446. data->session_id = NULL;
  1447. if (eap_peer_tls_reauth_init(sm, &data->ssl)) {
  1448. os_free(data);
  1449. return NULL;
  1450. }
  1451. if (data->phase2_priv && data->phase2_method &&
  1452. data->phase2_method->init_for_reauth)
  1453. data->phase2_method->init_for_reauth(sm, data->phase2_priv);
  1454. data->phase2_start = 0;
  1455. data->phase2_success = 0;
  1456. data->resuming = 1;
  1457. data->reauth = 1;
  1458. return priv;
  1459. }
  1460. static int eap_ttls_get_status(struct eap_sm *sm, void *priv, char *buf,
  1461. size_t buflen, int verbose)
  1462. {
  1463. struct eap_ttls_data *data = priv;
  1464. int len, ret;
  1465. len = eap_peer_tls_status(sm, &data->ssl, buf, buflen, verbose);
  1466. ret = os_snprintf(buf + len, buflen - len,
  1467. "EAP-TTLSv%d Phase2 method=",
  1468. data->ttls_version);
  1469. if (os_snprintf_error(buflen - len, ret))
  1470. return len;
  1471. len += ret;
  1472. switch (data->phase2_type) {
  1473. case EAP_TTLS_PHASE2_EAP:
  1474. ret = os_snprintf(buf + len, buflen - len, "EAP-%s\n",
  1475. data->phase2_method ?
  1476. data->phase2_method->name : "?");
  1477. break;
  1478. case EAP_TTLS_PHASE2_MSCHAPV2:
  1479. ret = os_snprintf(buf + len, buflen - len, "MSCHAPV2\n");
  1480. break;
  1481. case EAP_TTLS_PHASE2_MSCHAP:
  1482. ret = os_snprintf(buf + len, buflen - len, "MSCHAP\n");
  1483. break;
  1484. case EAP_TTLS_PHASE2_PAP:
  1485. ret = os_snprintf(buf + len, buflen - len, "PAP\n");
  1486. break;
  1487. case EAP_TTLS_PHASE2_CHAP:
  1488. ret = os_snprintf(buf + len, buflen - len, "CHAP\n");
  1489. break;
  1490. default:
  1491. ret = 0;
  1492. break;
  1493. }
  1494. if (os_snprintf_error(buflen - len, ret))
  1495. return len;
  1496. len += ret;
  1497. return len;
  1498. }
  1499. static Boolean eap_ttls_isKeyAvailable(struct eap_sm *sm, void *priv)
  1500. {
  1501. struct eap_ttls_data *data = priv;
  1502. return data->key_data != NULL && data->phase2_success;
  1503. }
  1504. static u8 * eap_ttls_getKey(struct eap_sm *sm, void *priv, size_t *len)
  1505. {
  1506. struct eap_ttls_data *data = priv;
  1507. u8 *key;
  1508. if (data->key_data == NULL || !data->phase2_success)
  1509. return NULL;
  1510. key = os_memdup(data->key_data, EAP_TLS_KEY_LEN);
  1511. if (key == NULL)
  1512. return NULL;
  1513. *len = EAP_TLS_KEY_LEN;
  1514. return key;
  1515. }
  1516. static u8 * eap_ttls_get_session_id(struct eap_sm *sm, void *priv, size_t *len)
  1517. {
  1518. struct eap_ttls_data *data = priv;
  1519. u8 *id;
  1520. if (data->session_id == NULL || !data->phase2_success)
  1521. return NULL;
  1522. id = os_memdup(data->session_id, data->id_len);
  1523. if (id == NULL)
  1524. return NULL;
  1525. *len = data->id_len;
  1526. return id;
  1527. }
  1528. static u8 * eap_ttls_get_emsk(struct eap_sm *sm, void *priv, size_t *len)
  1529. {
  1530. struct eap_ttls_data *data = priv;
  1531. u8 *key;
  1532. if (data->key_data == NULL)
  1533. return NULL;
  1534. key = os_memdup(data->key_data + EAP_TLS_KEY_LEN, EAP_EMSK_LEN);
  1535. if (key == NULL)
  1536. return NULL;
  1537. *len = EAP_EMSK_LEN;
  1538. return key;
  1539. }
  1540. int eap_peer_ttls_register(void)
  1541. {
  1542. struct eap_method *eap;
  1543. eap = eap_peer_method_alloc(EAP_PEER_METHOD_INTERFACE_VERSION,
  1544. EAP_VENDOR_IETF, EAP_TYPE_TTLS, "TTLS");
  1545. if (eap == NULL)
  1546. return -1;
  1547. eap->init = eap_ttls_init;
  1548. eap->deinit = eap_ttls_deinit;
  1549. eap->process = eap_ttls_process;
  1550. eap->isKeyAvailable = eap_ttls_isKeyAvailable;
  1551. eap->getKey = eap_ttls_getKey;
  1552. eap->getSessionId = eap_ttls_get_session_id;
  1553. eap->get_status = eap_ttls_get_status;
  1554. eap->has_reauth_data = eap_ttls_has_reauth_data;
  1555. eap->deinit_for_reauth = eap_ttls_deinit_for_reauth;
  1556. eap->init_for_reauth = eap_ttls_init_for_reauth;
  1557. eap->get_emsk = eap_ttls_get_emsk;
  1558. return eap_peer_method_register(eap);
  1559. }