eap_ttls.c 42 KB

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