eap_ttls.c 52 KB

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