eap_ttls.c 44 KB

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