eap_ttls.c 52 KB

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