eap_peap.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302
  1. /*
  2. * EAP peer method: EAP-PEAP (draft-josefsson-pppext-eap-tls-eap-10.txt)
  3. * Copyright (c) 2004-2015, Jouni Malinen <j@w1.fi>
  4. *
  5. * This software may be distributed under the terms of the BSD license.
  6. * See README for more details.
  7. */
  8. #include "includes.h"
  9. #include "common.h"
  10. #include "crypto/sha1.h"
  11. #include "crypto/tls.h"
  12. #include "eap_common/eap_tlv_common.h"
  13. #include "eap_common/eap_peap_common.h"
  14. #include "eap_i.h"
  15. #include "eap_tls_common.h"
  16. #include "eap_config.h"
  17. #include "tncc.h"
  18. /* Maximum supported PEAP version
  19. * 0 = Microsoft's PEAP version 0; draft-kamath-pppext-peapv0-00.txt
  20. * 1 = draft-josefsson-ppext-eap-tls-eap-05.txt
  21. */
  22. #define EAP_PEAP_VERSION 1
  23. static void eap_peap_deinit(struct eap_sm *sm, void *priv);
  24. struct eap_peap_data {
  25. struct eap_ssl_data ssl;
  26. int peap_version, force_peap_version, force_new_label;
  27. const struct eap_method *phase2_method;
  28. void *phase2_priv;
  29. int phase2_success;
  30. int phase2_eap_success;
  31. int phase2_eap_started;
  32. struct eap_method_type phase2_type;
  33. struct eap_method_type *phase2_types;
  34. size_t num_phase2_types;
  35. int peap_outer_success; /* 0 = PEAP terminated on Phase 2 inner
  36. * EAP-Success
  37. * 1 = reply with tunneled EAP-Success to inner
  38. * EAP-Success and expect AS to send outer
  39. * (unencrypted) EAP-Success after this
  40. * 2 = reply with PEAP/TLS ACK to inner
  41. * EAP-Success and expect AS to send outer
  42. * (unencrypted) EAP-Success after this */
  43. int resuming; /* starting a resumed session */
  44. int reauth; /* reauthentication */
  45. u8 *key_data;
  46. u8 *session_id;
  47. size_t id_len;
  48. struct wpabuf *pending_phase2_req;
  49. struct wpabuf *pending_resp;
  50. enum { NO_BINDING, OPTIONAL_BINDING, REQUIRE_BINDING } crypto_binding;
  51. int crypto_binding_used;
  52. u8 binding_nonce[32];
  53. u8 ipmk[40];
  54. u8 cmk[20];
  55. int soh; /* Whether IF-TNCCS-SOH (Statement of Health; Microsoft NAP)
  56. * is enabled. */
  57. };
  58. static void eap_peap_parse_phase1(struct eap_peap_data *data,
  59. const char *phase1)
  60. {
  61. const char *pos;
  62. pos = os_strstr(phase1, "peapver=");
  63. if (pos) {
  64. data->force_peap_version = atoi(pos + 8);
  65. data->peap_version = data->force_peap_version;
  66. wpa_printf(MSG_DEBUG, "EAP-PEAP: Forced PEAP version %d",
  67. data->force_peap_version);
  68. }
  69. if (os_strstr(phase1, "peaplabel=1")) {
  70. data->force_new_label = 1;
  71. wpa_printf(MSG_DEBUG, "EAP-PEAP: Force new label for key "
  72. "derivation");
  73. }
  74. if (os_strstr(phase1, "peap_outer_success=0")) {
  75. data->peap_outer_success = 0;
  76. wpa_printf(MSG_DEBUG, "EAP-PEAP: terminate authentication on "
  77. "tunneled EAP-Success");
  78. } else if (os_strstr(phase1, "peap_outer_success=1")) {
  79. data->peap_outer_success = 1;
  80. wpa_printf(MSG_DEBUG, "EAP-PEAP: send tunneled EAP-Success "
  81. "after receiving tunneled EAP-Success");
  82. } else if (os_strstr(phase1, "peap_outer_success=2")) {
  83. data->peap_outer_success = 2;
  84. wpa_printf(MSG_DEBUG, "EAP-PEAP: send PEAP/TLS ACK after "
  85. "receiving tunneled EAP-Success");
  86. }
  87. if (os_strstr(phase1, "crypto_binding=0")) {
  88. data->crypto_binding = NO_BINDING;
  89. wpa_printf(MSG_DEBUG, "EAP-PEAP: Do not use cryptobinding");
  90. } else if (os_strstr(phase1, "crypto_binding=1")) {
  91. data->crypto_binding = OPTIONAL_BINDING;
  92. wpa_printf(MSG_DEBUG, "EAP-PEAP: Optional cryptobinding");
  93. } else if (os_strstr(phase1, "crypto_binding=2")) {
  94. data->crypto_binding = REQUIRE_BINDING;
  95. wpa_printf(MSG_DEBUG, "EAP-PEAP: Require cryptobinding");
  96. }
  97. #ifdef EAP_TNC
  98. if (os_strstr(phase1, "tnc=soh2")) {
  99. data->soh = 2;
  100. wpa_printf(MSG_DEBUG, "EAP-PEAP: SoH version 2 enabled");
  101. } else if (os_strstr(phase1, "tnc=soh1")) {
  102. data->soh = 1;
  103. wpa_printf(MSG_DEBUG, "EAP-PEAP: SoH version 1 enabled");
  104. } else if (os_strstr(phase1, "tnc=soh")) {
  105. data->soh = 2;
  106. wpa_printf(MSG_DEBUG, "EAP-PEAP: SoH version 2 enabled");
  107. }
  108. #endif /* EAP_TNC */
  109. }
  110. static void * eap_peap_init(struct eap_sm *sm)
  111. {
  112. struct eap_peap_data *data;
  113. struct eap_peer_config *config = eap_get_config(sm);
  114. data = os_zalloc(sizeof(*data));
  115. if (data == NULL)
  116. return NULL;
  117. sm->peap_done = FALSE;
  118. data->peap_version = EAP_PEAP_VERSION;
  119. data->force_peap_version = -1;
  120. data->peap_outer_success = 2;
  121. data->crypto_binding = OPTIONAL_BINDING;
  122. if (config && config->phase1)
  123. eap_peap_parse_phase1(data, config->phase1);
  124. if (eap_peer_select_phase2_methods(config, "auth=",
  125. &data->phase2_types,
  126. &data->num_phase2_types) < 0) {
  127. eap_peap_deinit(sm, data);
  128. return NULL;
  129. }
  130. data->phase2_type.vendor = EAP_VENDOR_IETF;
  131. data->phase2_type.method = EAP_TYPE_NONE;
  132. if (eap_peer_tls_ssl_init(sm, &data->ssl, config, EAP_TYPE_PEAP)) {
  133. wpa_printf(MSG_INFO, "EAP-PEAP: Failed to initialize SSL.");
  134. eap_peap_deinit(sm, data);
  135. return NULL;
  136. }
  137. return data;
  138. }
  139. static void eap_peap_free_key(struct eap_peap_data *data)
  140. {
  141. if (data->key_data) {
  142. bin_clear_free(data->key_data, EAP_TLS_KEY_LEN);
  143. data->key_data = NULL;
  144. }
  145. }
  146. static void eap_peap_deinit(struct eap_sm *sm, void *priv)
  147. {
  148. struct eap_peap_data *data = priv;
  149. if (data == NULL)
  150. return;
  151. if (data->phase2_priv && data->phase2_method)
  152. data->phase2_method->deinit(sm, data->phase2_priv);
  153. os_free(data->phase2_types);
  154. eap_peer_tls_ssl_deinit(sm, &data->ssl);
  155. eap_peap_free_key(data);
  156. os_free(data->session_id);
  157. wpabuf_free(data->pending_phase2_req);
  158. wpabuf_free(data->pending_resp);
  159. os_free(data);
  160. }
  161. /**
  162. * eap_tlv_build_nak - Build EAP-TLV NAK message
  163. * @id: EAP identifier for the header
  164. * @nak_type: TLV type (EAP_TLV_*)
  165. * Returns: Buffer to the allocated EAP-TLV NAK message or %NULL on failure
  166. *
  167. * This function builds an EAP-TLV NAK message. The caller is responsible for
  168. * freeing the returned buffer.
  169. */
  170. static struct wpabuf * eap_tlv_build_nak(int id, u16 nak_type)
  171. {
  172. struct wpabuf *msg;
  173. msg = eap_msg_alloc(EAP_VENDOR_IETF, EAP_TYPE_TLV, 10,
  174. EAP_CODE_RESPONSE, id);
  175. if (msg == NULL)
  176. return NULL;
  177. wpabuf_put_u8(msg, 0x80); /* Mandatory */
  178. wpabuf_put_u8(msg, EAP_TLV_NAK_TLV);
  179. wpabuf_put_be16(msg, 6); /* Length */
  180. wpabuf_put_be32(msg, 0); /* Vendor-Id */
  181. wpabuf_put_be16(msg, nak_type); /* NAK-Type */
  182. return msg;
  183. }
  184. static int eap_peap_get_isk(struct eap_sm *sm, struct eap_peap_data *data,
  185. u8 *isk, size_t isk_len)
  186. {
  187. u8 *key;
  188. size_t key_len;
  189. os_memset(isk, 0, isk_len);
  190. if (data->phase2_method == NULL || data->phase2_priv == NULL ||
  191. data->phase2_method->isKeyAvailable == NULL ||
  192. data->phase2_method->getKey == NULL)
  193. return 0;
  194. if (!data->phase2_method->isKeyAvailable(sm, data->phase2_priv) ||
  195. (key = data->phase2_method->getKey(sm, data->phase2_priv,
  196. &key_len)) == NULL) {
  197. wpa_printf(MSG_DEBUG, "EAP-PEAP: Could not get key material "
  198. "from Phase 2");
  199. return -1;
  200. }
  201. if (key_len > isk_len)
  202. key_len = isk_len;
  203. os_memcpy(isk, key, key_len);
  204. os_free(key);
  205. return 0;
  206. }
  207. static int eap_peap_derive_cmk(struct eap_sm *sm, struct eap_peap_data *data)
  208. {
  209. u8 *tk;
  210. u8 isk[32], imck[60];
  211. int resumed;
  212. /*
  213. * Tunnel key (TK) is the first 60 octets of the key generated by
  214. * phase 1 of PEAP (based on TLS).
  215. */
  216. tk = data->key_data;
  217. if (tk == NULL)
  218. return -1;
  219. wpa_hexdump_key(MSG_DEBUG, "EAP-PEAP: TK", tk, 60);
  220. resumed = tls_connection_resumed(sm->ssl_ctx, data->ssl.conn);
  221. wpa_printf(MSG_DEBUG,
  222. "EAP-PEAP: CMK derivation - reauth=%d resumed=%d phase2_eap_started=%d phase2_success=%d",
  223. data->reauth, resumed, data->phase2_eap_started,
  224. data->phase2_success);
  225. if (data->reauth && !data->phase2_eap_started && resumed) {
  226. /* Fast-connect: IPMK|CMK = TK */
  227. os_memcpy(data->ipmk, tk, 40);
  228. wpa_hexdump_key(MSG_DEBUG, "EAP-PEAP: IPMK from TK",
  229. data->ipmk, 40);
  230. os_memcpy(data->cmk, tk + 40, 20);
  231. wpa_hexdump_key(MSG_DEBUG, "EAP-PEAP: CMK from TK",
  232. data->cmk, 20);
  233. return 0;
  234. }
  235. if (eap_peap_get_isk(sm, data, isk, sizeof(isk)) < 0)
  236. return -1;
  237. wpa_hexdump_key(MSG_DEBUG, "EAP-PEAP: ISK", isk, sizeof(isk));
  238. /*
  239. * IPMK Seed = "Inner Methods Compound Keys" | ISK
  240. * TempKey = First 40 octets of TK
  241. * IPMK|CMK = PRF+(TempKey, IPMK Seed, 60)
  242. * (note: draft-josefsson-pppext-eap-tls-eap-10.txt includes a space
  243. * in the end of the label just before ISK; is that just a typo?)
  244. */
  245. wpa_hexdump_key(MSG_DEBUG, "EAP-PEAP: TempKey", tk, 40);
  246. if (peap_prfplus(data->peap_version, tk, 40,
  247. "Inner Methods Compound Keys",
  248. isk, sizeof(isk), imck, sizeof(imck)) < 0)
  249. return -1;
  250. wpa_hexdump_key(MSG_DEBUG, "EAP-PEAP: IMCK (IPMKj)",
  251. imck, sizeof(imck));
  252. os_memcpy(data->ipmk, imck, 40);
  253. wpa_hexdump_key(MSG_DEBUG, "EAP-PEAP: IPMK (S-IPMKj)", data->ipmk, 40);
  254. os_memcpy(data->cmk, imck + 40, 20);
  255. wpa_hexdump_key(MSG_DEBUG, "EAP-PEAP: CMK (CMKj)", data->cmk, 20);
  256. return 0;
  257. }
  258. static int eap_tlv_add_cryptobinding(struct eap_sm *sm,
  259. struct eap_peap_data *data,
  260. struct wpabuf *buf)
  261. {
  262. u8 *mac;
  263. u8 eap_type = EAP_TYPE_PEAP;
  264. const u8 *addr[2];
  265. size_t len[2];
  266. u16 tlv_type;
  267. /* Compound_MAC: HMAC-SHA1-160(cryptobinding TLV | EAP type) */
  268. addr[0] = wpabuf_put(buf, 0);
  269. len[0] = 60;
  270. addr[1] = &eap_type;
  271. len[1] = 1;
  272. tlv_type = EAP_TLV_CRYPTO_BINDING_TLV;
  273. wpabuf_put_be16(buf, tlv_type);
  274. wpabuf_put_be16(buf, 56);
  275. wpabuf_put_u8(buf, 0); /* Reserved */
  276. wpabuf_put_u8(buf, data->peap_version); /* Version */
  277. wpabuf_put_u8(buf, data->peap_version); /* RecvVersion */
  278. wpabuf_put_u8(buf, 1); /* SubType: 0 = Request, 1 = Response */
  279. wpabuf_put_data(buf, data->binding_nonce, 32); /* Nonce */
  280. mac = wpabuf_put(buf, 20); /* Compound_MAC */
  281. wpa_hexdump(MSG_MSGDUMP, "EAP-PEAP: Compound_MAC CMK", data->cmk, 20);
  282. wpa_hexdump(MSG_MSGDUMP, "EAP-PEAP: Compound_MAC data 1",
  283. addr[0], len[0]);
  284. wpa_hexdump(MSG_MSGDUMP, "EAP-PEAP: Compound_MAC data 2",
  285. addr[1], len[1]);
  286. if (hmac_sha1_vector(data->cmk, 20, 2, addr, len, mac) < 0)
  287. return -1;
  288. wpa_hexdump(MSG_MSGDUMP, "EAP-PEAP: Compound_MAC", mac, SHA1_MAC_LEN);
  289. data->crypto_binding_used = 1;
  290. return 0;
  291. }
  292. /**
  293. * eap_tlv_build_result - Build EAP-TLV Result message
  294. * @id: EAP identifier for the header
  295. * @status: Status (EAP_TLV_RESULT_SUCCESS or EAP_TLV_RESULT_FAILURE)
  296. * Returns: Buffer to the allocated EAP-TLV Result message or %NULL on failure
  297. *
  298. * This function builds an EAP-TLV Result message. The caller is responsible
  299. * for freeing the returned buffer.
  300. */
  301. static struct wpabuf * eap_tlv_build_result(struct eap_sm *sm,
  302. struct eap_peap_data *data,
  303. int crypto_tlv_used,
  304. int id, u16 status)
  305. {
  306. struct wpabuf *msg;
  307. size_t len;
  308. if (data->crypto_binding == NO_BINDING)
  309. crypto_tlv_used = 0;
  310. len = 6;
  311. if (crypto_tlv_used)
  312. len += 60; /* Cryptobinding TLV */
  313. msg = eap_msg_alloc(EAP_VENDOR_IETF, EAP_TYPE_TLV, len,
  314. EAP_CODE_RESPONSE, id);
  315. if (msg == NULL)
  316. return NULL;
  317. wpabuf_put_u8(msg, 0x80); /* Mandatory */
  318. wpabuf_put_u8(msg, EAP_TLV_RESULT_TLV);
  319. wpabuf_put_be16(msg, 2); /* Length */
  320. wpabuf_put_be16(msg, status); /* Status */
  321. if (crypto_tlv_used && eap_tlv_add_cryptobinding(sm, data, msg)) {
  322. wpabuf_free(msg);
  323. return NULL;
  324. }
  325. return msg;
  326. }
  327. static int eap_tlv_validate_cryptobinding(struct eap_sm *sm,
  328. struct eap_peap_data *data,
  329. const u8 *crypto_tlv,
  330. size_t crypto_tlv_len)
  331. {
  332. u8 buf[61], mac[SHA1_MAC_LEN];
  333. const u8 *pos;
  334. if (eap_peap_derive_cmk(sm, data) < 0) {
  335. wpa_printf(MSG_DEBUG, "EAP-PEAP: Could not derive CMK");
  336. return -1;
  337. }
  338. if (crypto_tlv_len != 4 + 56) {
  339. wpa_printf(MSG_DEBUG, "EAP-PEAP: Invalid cryptobinding TLV "
  340. "length %d", (int) crypto_tlv_len);
  341. return -1;
  342. }
  343. pos = crypto_tlv;
  344. pos += 4; /* TLV header */
  345. if (pos[1] != data->peap_version) {
  346. wpa_printf(MSG_DEBUG, "EAP-PEAP: Cryptobinding TLV Version "
  347. "mismatch (was %d; expected %d)",
  348. pos[1], data->peap_version);
  349. return -1;
  350. }
  351. if (pos[3] != 0) {
  352. wpa_printf(MSG_DEBUG, "EAP-PEAP: Unexpected Cryptobinding TLV "
  353. "SubType %d", pos[3]);
  354. return -1;
  355. }
  356. pos += 4;
  357. os_memcpy(data->binding_nonce, pos, 32);
  358. pos += 32; /* Nonce */
  359. /* Compound_MAC: HMAC-SHA1-160(cryptobinding TLV | EAP type) */
  360. os_memcpy(buf, crypto_tlv, 60);
  361. os_memset(buf + 4 + 4 + 32, 0, 20); /* Compound_MAC */
  362. buf[60] = EAP_TYPE_PEAP;
  363. wpa_hexdump(MSG_DEBUG, "EAP-PEAP: Compound_MAC data",
  364. buf, sizeof(buf));
  365. hmac_sha1(data->cmk, 20, buf, sizeof(buf), mac);
  366. if (os_memcmp_const(mac, pos, SHA1_MAC_LEN) != 0) {
  367. wpa_printf(MSG_DEBUG, "EAP-PEAP: Invalid Compound_MAC in "
  368. "cryptobinding TLV");
  369. wpa_hexdump(MSG_DEBUG, "EAP-PEAP: Received MAC",
  370. pos, SHA1_MAC_LEN);
  371. wpa_hexdump(MSG_DEBUG, "EAP-PEAP: Expected MAC",
  372. mac, SHA1_MAC_LEN);
  373. return -1;
  374. }
  375. wpa_printf(MSG_DEBUG, "EAP-PEAP: Valid cryptobinding TLV received");
  376. return 0;
  377. }
  378. /**
  379. * eap_tlv_process - Process a received EAP-TLV message and generate a response
  380. * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
  381. * @ret: Return values from EAP request validation and processing
  382. * @req: EAP-TLV request to be processed. The caller must have validated that
  383. * the buffer is large enough to contain full request (hdr->length bytes) and
  384. * that the EAP type is EAP_TYPE_TLV.
  385. * @resp: Buffer to return a pointer to the allocated response message. This
  386. * field should be initialized to %NULL before the call. The value will be
  387. * updated if a response message is generated. The caller is responsible for
  388. * freeing the allocated message.
  389. * @force_failure: Force negotiation to fail
  390. * Returns: 0 on success, -1 on failure
  391. */
  392. static int eap_tlv_process(struct eap_sm *sm, struct eap_peap_data *data,
  393. struct eap_method_ret *ret,
  394. const struct wpabuf *req, struct wpabuf **resp,
  395. int force_failure)
  396. {
  397. size_t left, tlv_len;
  398. const u8 *pos;
  399. const u8 *result_tlv = NULL, *crypto_tlv = NULL;
  400. size_t result_tlv_len = 0, crypto_tlv_len = 0;
  401. int tlv_type, mandatory;
  402. /* Parse TLVs */
  403. pos = eap_hdr_validate(EAP_VENDOR_IETF, EAP_TYPE_TLV, req, &left);
  404. if (pos == NULL)
  405. return -1;
  406. wpa_hexdump(MSG_DEBUG, "EAP-TLV: Received TLVs", pos, left);
  407. while (left >= 4) {
  408. mandatory = !!(pos[0] & 0x80);
  409. tlv_type = WPA_GET_BE16(pos) & 0x3fff;
  410. pos += 2;
  411. tlv_len = WPA_GET_BE16(pos);
  412. pos += 2;
  413. left -= 4;
  414. if (tlv_len > left) {
  415. wpa_printf(MSG_DEBUG, "EAP-TLV: TLV underrun "
  416. "(tlv_len=%lu left=%lu)",
  417. (unsigned long) tlv_len,
  418. (unsigned long) left);
  419. return -1;
  420. }
  421. switch (tlv_type) {
  422. case EAP_TLV_RESULT_TLV:
  423. result_tlv = pos;
  424. result_tlv_len = tlv_len;
  425. break;
  426. case EAP_TLV_CRYPTO_BINDING_TLV:
  427. crypto_tlv = pos;
  428. crypto_tlv_len = tlv_len;
  429. break;
  430. default:
  431. wpa_printf(MSG_DEBUG, "EAP-TLV: Unsupported TLV Type "
  432. "%d%s", tlv_type,
  433. mandatory ? " (mandatory)" : "");
  434. if (mandatory) {
  435. /* NAK TLV and ignore all TLVs in this packet.
  436. */
  437. *resp = eap_tlv_build_nak(eap_get_id(req),
  438. tlv_type);
  439. return *resp == NULL ? -1 : 0;
  440. }
  441. /* Ignore this TLV, but process other TLVs */
  442. break;
  443. }
  444. pos += tlv_len;
  445. left -= tlv_len;
  446. }
  447. if (left) {
  448. wpa_printf(MSG_DEBUG, "EAP-TLV: Last TLV too short in "
  449. "Request (left=%lu)", (unsigned long) left);
  450. return -1;
  451. }
  452. /* Process supported TLVs */
  453. if (crypto_tlv && data->crypto_binding != NO_BINDING) {
  454. wpa_hexdump(MSG_DEBUG, "EAP-PEAP: Cryptobinding TLV",
  455. crypto_tlv, crypto_tlv_len);
  456. if (eap_tlv_validate_cryptobinding(sm, data, crypto_tlv - 4,
  457. crypto_tlv_len + 4) < 0) {
  458. if (result_tlv == NULL)
  459. return -1;
  460. force_failure = 1;
  461. crypto_tlv = NULL; /* do not include Cryptobinding TLV
  462. * in response, if the received
  463. * cryptobinding was invalid. */
  464. }
  465. } else if (!crypto_tlv && data->crypto_binding == REQUIRE_BINDING) {
  466. wpa_printf(MSG_DEBUG, "EAP-PEAP: No cryptobinding TLV");
  467. return -1;
  468. }
  469. if (result_tlv) {
  470. int status, resp_status;
  471. wpa_hexdump(MSG_DEBUG, "EAP-TLV: Result TLV",
  472. result_tlv, result_tlv_len);
  473. if (result_tlv_len < 2) {
  474. wpa_printf(MSG_INFO, "EAP-TLV: Too short Result TLV "
  475. "(len=%lu)",
  476. (unsigned long) result_tlv_len);
  477. return -1;
  478. }
  479. status = WPA_GET_BE16(result_tlv);
  480. if (status == EAP_TLV_RESULT_SUCCESS) {
  481. wpa_printf(MSG_INFO, "EAP-TLV: TLV Result - Success "
  482. "- EAP-TLV/Phase2 Completed");
  483. if (force_failure) {
  484. wpa_printf(MSG_INFO, "EAP-TLV: Earlier failure"
  485. " - force failed Phase 2");
  486. resp_status = EAP_TLV_RESULT_FAILURE;
  487. ret->decision = DECISION_FAIL;
  488. } else {
  489. resp_status = EAP_TLV_RESULT_SUCCESS;
  490. ret->decision = DECISION_UNCOND_SUCC;
  491. }
  492. } else if (status == EAP_TLV_RESULT_FAILURE) {
  493. wpa_printf(MSG_INFO, "EAP-TLV: TLV Result - Failure");
  494. resp_status = EAP_TLV_RESULT_FAILURE;
  495. ret->decision = DECISION_FAIL;
  496. } else {
  497. wpa_printf(MSG_INFO, "EAP-TLV: Unknown TLV Result "
  498. "Status %d", status);
  499. resp_status = EAP_TLV_RESULT_FAILURE;
  500. ret->decision = DECISION_FAIL;
  501. }
  502. ret->methodState = METHOD_DONE;
  503. *resp = eap_tlv_build_result(sm, data, crypto_tlv != NULL,
  504. eap_get_id(req), resp_status);
  505. }
  506. return 0;
  507. }
  508. static int eap_peap_phase2_request(struct eap_sm *sm,
  509. struct eap_peap_data *data,
  510. struct eap_method_ret *ret,
  511. struct wpabuf *req,
  512. struct wpabuf **resp)
  513. {
  514. struct eap_hdr *hdr = wpabuf_mhead(req);
  515. size_t len = be_to_host16(hdr->length);
  516. u8 *pos;
  517. struct eap_method_ret iret;
  518. struct eap_peer_config *config = eap_get_config(sm);
  519. if (len <= sizeof(struct eap_hdr)) {
  520. wpa_printf(MSG_INFO, "EAP-PEAP: too short "
  521. "Phase 2 request (len=%lu)", (unsigned long) len);
  522. return -1;
  523. }
  524. pos = (u8 *) (hdr + 1);
  525. wpa_printf(MSG_DEBUG, "EAP-PEAP: Phase 2 Request: type=%d", *pos);
  526. switch (*pos) {
  527. case EAP_TYPE_IDENTITY:
  528. *resp = eap_sm_buildIdentity(sm, hdr->identifier, 1);
  529. break;
  530. case EAP_TYPE_TLV:
  531. os_memset(&iret, 0, sizeof(iret));
  532. if (eap_tlv_process(sm, data, &iret, req, resp,
  533. data->phase2_eap_started &&
  534. !data->phase2_eap_success)) {
  535. ret->methodState = METHOD_DONE;
  536. ret->decision = DECISION_FAIL;
  537. return -1;
  538. }
  539. if (iret.methodState == METHOD_DONE ||
  540. iret.methodState == METHOD_MAY_CONT) {
  541. ret->methodState = iret.methodState;
  542. ret->decision = iret.decision;
  543. data->phase2_success = 1;
  544. }
  545. break;
  546. case EAP_TYPE_EXPANDED:
  547. #ifdef EAP_TNC
  548. if (data->soh) {
  549. const u8 *epos;
  550. size_t eleft;
  551. epos = eap_hdr_validate(EAP_VENDOR_MICROSOFT, 0x21,
  552. req, &eleft);
  553. if (epos) {
  554. struct wpabuf *buf;
  555. wpa_printf(MSG_DEBUG,
  556. "EAP-PEAP: SoH EAP Extensions");
  557. buf = tncc_process_soh_request(data->soh,
  558. epos, eleft);
  559. if (buf) {
  560. *resp = eap_msg_alloc(
  561. EAP_VENDOR_MICROSOFT, 0x21,
  562. wpabuf_len(buf),
  563. EAP_CODE_RESPONSE,
  564. hdr->identifier);
  565. if (*resp == NULL) {
  566. ret->methodState = METHOD_DONE;
  567. ret->decision = DECISION_FAIL;
  568. wpabuf_free(buf);
  569. return -1;
  570. }
  571. wpabuf_put_buf(*resp, buf);
  572. wpabuf_free(buf);
  573. break;
  574. }
  575. }
  576. }
  577. #endif /* EAP_TNC */
  578. /* fall through */
  579. default:
  580. if (data->phase2_type.vendor == EAP_VENDOR_IETF &&
  581. data->phase2_type.method == EAP_TYPE_NONE) {
  582. size_t i;
  583. for (i = 0; i < data->num_phase2_types; i++) {
  584. if (data->phase2_types[i].vendor !=
  585. EAP_VENDOR_IETF ||
  586. data->phase2_types[i].method != *pos)
  587. continue;
  588. data->phase2_type.vendor =
  589. data->phase2_types[i].vendor;
  590. data->phase2_type.method =
  591. data->phase2_types[i].method;
  592. wpa_printf(MSG_DEBUG, "EAP-PEAP: Selected "
  593. "Phase 2 EAP vendor %d method %d",
  594. data->phase2_type.vendor,
  595. data->phase2_type.method);
  596. break;
  597. }
  598. }
  599. if (*pos != data->phase2_type.method ||
  600. *pos == EAP_TYPE_NONE) {
  601. if (eap_peer_tls_phase2_nak(data->phase2_types,
  602. data->num_phase2_types,
  603. hdr, resp))
  604. return -1;
  605. return 0;
  606. }
  607. if (data->phase2_priv == NULL) {
  608. data->phase2_method = eap_peer_get_eap_method(
  609. data->phase2_type.vendor,
  610. data->phase2_type.method);
  611. if (data->phase2_method) {
  612. sm->init_phase2 = 1;
  613. data->phase2_priv =
  614. data->phase2_method->init(sm);
  615. sm->init_phase2 = 0;
  616. }
  617. }
  618. if (data->phase2_priv == NULL || data->phase2_method == NULL) {
  619. wpa_printf(MSG_INFO, "EAP-PEAP: failed to initialize "
  620. "Phase 2 EAP method %d", *pos);
  621. ret->methodState = METHOD_DONE;
  622. ret->decision = DECISION_FAIL;
  623. return -1;
  624. }
  625. data->phase2_eap_started = 1;
  626. os_memset(&iret, 0, sizeof(iret));
  627. *resp = data->phase2_method->process(sm, data->phase2_priv,
  628. &iret, req);
  629. if ((iret.methodState == METHOD_DONE ||
  630. iret.methodState == METHOD_MAY_CONT) &&
  631. (iret.decision == DECISION_UNCOND_SUCC ||
  632. iret.decision == DECISION_COND_SUCC)) {
  633. data->phase2_eap_success = 1;
  634. data->phase2_success = 1;
  635. }
  636. break;
  637. }
  638. if (*resp == NULL &&
  639. (config->pending_req_identity || config->pending_req_password ||
  640. config->pending_req_otp || config->pending_req_new_password)) {
  641. wpabuf_free(data->pending_phase2_req);
  642. data->pending_phase2_req = wpabuf_alloc_copy(hdr, len);
  643. }
  644. return 0;
  645. }
  646. static int eap_peap_decrypt(struct eap_sm *sm, struct eap_peap_data *data,
  647. struct eap_method_ret *ret,
  648. const struct eap_hdr *req,
  649. const struct wpabuf *in_data,
  650. struct wpabuf **out_data)
  651. {
  652. struct wpabuf *in_decrypted = NULL;
  653. int res, skip_change = 0;
  654. struct eap_hdr *hdr, *rhdr;
  655. struct wpabuf *resp = NULL;
  656. size_t len;
  657. wpa_printf(MSG_DEBUG, "EAP-PEAP: received %lu bytes encrypted data for"
  658. " Phase 2", (unsigned long) wpabuf_len(in_data));
  659. if (data->pending_phase2_req) {
  660. wpa_printf(MSG_DEBUG, "EAP-PEAP: Pending Phase 2 request - "
  661. "skip decryption and use old data");
  662. /* Clear TLS reassembly state. */
  663. eap_peer_tls_reset_input(&data->ssl);
  664. in_decrypted = data->pending_phase2_req;
  665. data->pending_phase2_req = NULL;
  666. skip_change = 1;
  667. goto continue_req;
  668. }
  669. if (wpabuf_len(in_data) == 0 && sm->workaround &&
  670. data->phase2_success) {
  671. /*
  672. * Cisco ACS seems to be using TLS ACK to terminate
  673. * EAP-PEAPv0/GTC. Try to reply with TLS ACK.
  674. */
  675. wpa_printf(MSG_DEBUG, "EAP-PEAP: Received TLS ACK, but "
  676. "expected data - acknowledge with TLS ACK since "
  677. "Phase 2 has been completed");
  678. ret->decision = DECISION_COND_SUCC;
  679. ret->methodState = METHOD_DONE;
  680. return 1;
  681. } else if (wpabuf_len(in_data) == 0) {
  682. /* Received TLS ACK - requesting more fragments */
  683. return eap_peer_tls_encrypt(sm, &data->ssl, EAP_TYPE_PEAP,
  684. data->peap_version,
  685. req->identifier, NULL, out_data);
  686. }
  687. res = eap_peer_tls_decrypt(sm, &data->ssl, in_data, &in_decrypted);
  688. if (res)
  689. return res;
  690. continue_req:
  691. wpa_hexdump_buf(MSG_DEBUG, "EAP-PEAP: Decrypted Phase 2 EAP",
  692. in_decrypted);
  693. hdr = wpabuf_mhead(in_decrypted);
  694. if (wpabuf_len(in_decrypted) == 5 && hdr->code == EAP_CODE_REQUEST &&
  695. be_to_host16(hdr->length) == 5 &&
  696. eap_get_type(in_decrypted) == EAP_TYPE_IDENTITY) {
  697. /* At least FreeRADIUS seems to send full EAP header with
  698. * EAP Request Identity */
  699. skip_change = 1;
  700. }
  701. if (wpabuf_len(in_decrypted) >= 5 && hdr->code == EAP_CODE_REQUEST &&
  702. eap_get_type(in_decrypted) == EAP_TYPE_TLV) {
  703. skip_change = 1;
  704. }
  705. if (data->peap_version == 0 && !skip_change) {
  706. struct eap_hdr *nhdr;
  707. struct wpabuf *nmsg = wpabuf_alloc(sizeof(struct eap_hdr) +
  708. wpabuf_len(in_decrypted));
  709. if (nmsg == NULL) {
  710. wpabuf_free(in_decrypted);
  711. return 0;
  712. }
  713. nhdr = wpabuf_put(nmsg, sizeof(*nhdr));
  714. wpabuf_put_buf(nmsg, in_decrypted);
  715. nhdr->code = req->code;
  716. nhdr->identifier = req->identifier;
  717. nhdr->length = host_to_be16(sizeof(struct eap_hdr) +
  718. wpabuf_len(in_decrypted));
  719. wpabuf_free(in_decrypted);
  720. in_decrypted = nmsg;
  721. }
  722. hdr = wpabuf_mhead(in_decrypted);
  723. if (wpabuf_len(in_decrypted) < sizeof(*hdr)) {
  724. wpa_printf(MSG_INFO, "EAP-PEAP: Too short Phase 2 "
  725. "EAP frame (len=%lu)",
  726. (unsigned long) wpabuf_len(in_decrypted));
  727. wpabuf_free(in_decrypted);
  728. return 0;
  729. }
  730. len = be_to_host16(hdr->length);
  731. if (len > wpabuf_len(in_decrypted)) {
  732. wpa_printf(MSG_INFO, "EAP-PEAP: Length mismatch in "
  733. "Phase 2 EAP frame (len=%lu hdr->length=%lu)",
  734. (unsigned long) wpabuf_len(in_decrypted),
  735. (unsigned long) len);
  736. wpabuf_free(in_decrypted);
  737. return 0;
  738. }
  739. if (len < wpabuf_len(in_decrypted)) {
  740. wpa_printf(MSG_INFO, "EAP-PEAP: Odd.. Phase 2 EAP header has "
  741. "shorter length than full decrypted data "
  742. "(%lu < %lu)",
  743. (unsigned long) len,
  744. (unsigned long) wpabuf_len(in_decrypted));
  745. }
  746. wpa_printf(MSG_DEBUG, "EAP-PEAP: received Phase 2: code=%d "
  747. "identifier=%d length=%lu", hdr->code, hdr->identifier,
  748. (unsigned long) len);
  749. switch (hdr->code) {
  750. case EAP_CODE_REQUEST:
  751. if (eap_peap_phase2_request(sm, data, ret, in_decrypted,
  752. &resp)) {
  753. wpabuf_free(in_decrypted);
  754. wpa_printf(MSG_INFO, "EAP-PEAP: Phase2 Request "
  755. "processing failed");
  756. return 0;
  757. }
  758. break;
  759. case EAP_CODE_SUCCESS:
  760. wpa_printf(MSG_DEBUG, "EAP-PEAP: Phase 2 Success");
  761. if (data->peap_version == 1) {
  762. /* EAP-Success within TLS tunnel is used to indicate
  763. * shutdown of the TLS channel. The authentication has
  764. * been completed. */
  765. if (data->phase2_eap_started &&
  766. !data->phase2_eap_success) {
  767. wpa_printf(MSG_DEBUG, "EAP-PEAP: Phase 2 "
  768. "Success used to indicate success, "
  769. "but Phase 2 EAP was not yet "
  770. "completed successfully");
  771. ret->methodState = METHOD_DONE;
  772. ret->decision = DECISION_FAIL;
  773. wpabuf_free(in_decrypted);
  774. return 0;
  775. }
  776. wpa_printf(MSG_DEBUG, "EAP-PEAP: Version 1 - "
  777. "EAP-Success within TLS tunnel - "
  778. "authentication completed");
  779. ret->decision = DECISION_UNCOND_SUCC;
  780. ret->methodState = METHOD_DONE;
  781. data->phase2_success = 1;
  782. if (data->peap_outer_success == 2) {
  783. wpabuf_free(in_decrypted);
  784. wpa_printf(MSG_DEBUG, "EAP-PEAP: Use TLS ACK "
  785. "to finish authentication");
  786. return 1;
  787. } else if (data->peap_outer_success == 1) {
  788. /* Reply with EAP-Success within the TLS
  789. * channel to complete the authentication. */
  790. resp = wpabuf_alloc(sizeof(struct eap_hdr));
  791. if (resp) {
  792. rhdr = wpabuf_put(resp, sizeof(*rhdr));
  793. rhdr->code = EAP_CODE_SUCCESS;
  794. rhdr->identifier = hdr->identifier;
  795. rhdr->length =
  796. host_to_be16(sizeof(*rhdr));
  797. }
  798. } else {
  799. /* No EAP-Success expected for Phase 1 (outer,
  800. * unencrypted auth), so force EAP state
  801. * machine to SUCCESS state. */
  802. sm->peap_done = TRUE;
  803. }
  804. } else {
  805. /* FIX: ? */
  806. }
  807. break;
  808. case EAP_CODE_FAILURE:
  809. wpa_printf(MSG_DEBUG, "EAP-PEAP: Phase 2 Failure");
  810. ret->decision = DECISION_FAIL;
  811. ret->methodState = METHOD_MAY_CONT;
  812. ret->allowNotifications = FALSE;
  813. /* Reply with EAP-Failure within the TLS channel to complete
  814. * failure reporting. */
  815. resp = wpabuf_alloc(sizeof(struct eap_hdr));
  816. if (resp) {
  817. rhdr = wpabuf_put(resp, sizeof(*rhdr));
  818. rhdr->code = EAP_CODE_FAILURE;
  819. rhdr->identifier = hdr->identifier;
  820. rhdr->length = host_to_be16(sizeof(*rhdr));
  821. }
  822. break;
  823. default:
  824. wpa_printf(MSG_INFO, "EAP-PEAP: Unexpected code=%d in "
  825. "Phase 2 EAP header", hdr->code);
  826. break;
  827. }
  828. wpabuf_free(in_decrypted);
  829. if (resp) {
  830. int skip_change2 = 0;
  831. struct wpabuf *rmsg, buf;
  832. wpa_hexdump_buf_key(MSG_DEBUG,
  833. "EAP-PEAP: Encrypting Phase 2 data", resp);
  834. /* PEAP version changes */
  835. if (wpabuf_len(resp) >= 5 &&
  836. wpabuf_head_u8(resp)[0] == EAP_CODE_RESPONSE &&
  837. eap_get_type(resp) == EAP_TYPE_TLV)
  838. skip_change2 = 1;
  839. rmsg = resp;
  840. if (data->peap_version == 0 && !skip_change2) {
  841. wpabuf_set(&buf, wpabuf_head_u8(resp) +
  842. sizeof(struct eap_hdr),
  843. wpabuf_len(resp) - sizeof(struct eap_hdr));
  844. rmsg = &buf;
  845. }
  846. if (eap_peer_tls_encrypt(sm, &data->ssl, EAP_TYPE_PEAP,
  847. data->peap_version, req->identifier,
  848. rmsg, out_data)) {
  849. wpa_printf(MSG_INFO, "EAP-PEAP: Failed to encrypt "
  850. "a Phase 2 frame");
  851. }
  852. wpabuf_free(resp);
  853. }
  854. return 0;
  855. }
  856. static struct wpabuf * eap_peap_process(struct eap_sm *sm, void *priv,
  857. struct eap_method_ret *ret,
  858. const struct wpabuf *reqData)
  859. {
  860. const struct eap_hdr *req;
  861. size_t left;
  862. int res;
  863. u8 flags, id;
  864. struct wpabuf *resp;
  865. const u8 *pos;
  866. struct eap_peap_data *data = priv;
  867. struct wpabuf msg;
  868. pos = eap_peer_tls_process_init(sm, &data->ssl, EAP_TYPE_PEAP, ret,
  869. reqData, &left, &flags);
  870. if (pos == NULL)
  871. return NULL;
  872. req = wpabuf_head(reqData);
  873. id = req->identifier;
  874. if (flags & EAP_TLS_FLAGS_START) {
  875. wpa_printf(MSG_DEBUG, "EAP-PEAP: Start (server ver=%d, own "
  876. "ver=%d)", flags & EAP_TLS_VERSION_MASK,
  877. data->peap_version);
  878. if ((flags & EAP_TLS_VERSION_MASK) < data->peap_version)
  879. data->peap_version = flags & EAP_TLS_VERSION_MASK;
  880. if (data->force_peap_version >= 0 &&
  881. data->force_peap_version != data->peap_version) {
  882. wpa_printf(MSG_WARNING, "EAP-PEAP: Failed to select "
  883. "forced PEAP version %d",
  884. data->force_peap_version);
  885. ret->methodState = METHOD_DONE;
  886. ret->decision = DECISION_FAIL;
  887. ret->allowNotifications = FALSE;
  888. return NULL;
  889. }
  890. wpa_printf(MSG_DEBUG, "EAP-PEAP: Using PEAP version %d",
  891. data->peap_version);
  892. left = 0; /* make sure that this frame is empty, even though it
  893. * should always be, anyway */
  894. }
  895. wpabuf_set(&msg, pos, left);
  896. resp = NULL;
  897. if (tls_connection_established(sm->ssl_ctx, data->ssl.conn) &&
  898. !data->resuming) {
  899. res = eap_peap_decrypt(sm, data, ret, req, &msg, &resp);
  900. } else {
  901. if (sm->waiting_ext_cert_check && data->pending_resp) {
  902. struct eap_peer_config *config = eap_get_config(sm);
  903. if (config->pending_ext_cert_check ==
  904. EXT_CERT_CHECK_GOOD) {
  905. wpa_printf(MSG_DEBUG,
  906. "EAP-PEAP: External certificate check succeeded - continue handshake");
  907. resp = data->pending_resp;
  908. data->pending_resp = NULL;
  909. sm->waiting_ext_cert_check = 0;
  910. return resp;
  911. }
  912. if (config->pending_ext_cert_check ==
  913. EXT_CERT_CHECK_BAD) {
  914. wpa_printf(MSG_DEBUG,
  915. "EAP-PEAP: External certificate check failed - force authentication failure");
  916. ret->methodState = METHOD_DONE;
  917. ret->decision = DECISION_FAIL;
  918. sm->waiting_ext_cert_check = 0;
  919. return NULL;
  920. }
  921. wpa_printf(MSG_DEBUG,
  922. "EAP-PEAP: Continuing to wait external server certificate validation");
  923. return NULL;
  924. }
  925. res = eap_peer_tls_process_helper(sm, &data->ssl,
  926. EAP_TYPE_PEAP,
  927. data->peap_version, id, &msg,
  928. &resp);
  929. if (res < 0) {
  930. wpa_printf(MSG_DEBUG,
  931. "EAP-PEAP: TLS processing failed");
  932. ret->methodState = METHOD_DONE;
  933. ret->decision = DECISION_FAIL;
  934. return resp;
  935. }
  936. if (sm->waiting_ext_cert_check) {
  937. wpa_printf(MSG_DEBUG,
  938. "EAP-PEAP: Waiting external server certificate validation");
  939. wpabuf_free(data->pending_resp);
  940. data->pending_resp = resp;
  941. return NULL;
  942. }
  943. if (tls_connection_established(sm->ssl_ctx, data->ssl.conn)) {
  944. char *label;
  945. wpa_printf(MSG_DEBUG,
  946. "EAP-PEAP: TLS done, proceed to Phase 2");
  947. eap_peap_free_key(data);
  948. /* draft-josefsson-ppext-eap-tls-eap-05.txt
  949. * specifies that PEAPv1 would use "client PEAP
  950. * encryption" as the label. However, most existing
  951. * PEAPv1 implementations seem to be using the old
  952. * label, "client EAP encryption", instead. Use the old
  953. * label by default, but allow it to be configured with
  954. * phase1 parameter peaplabel=1. */
  955. if (data->force_new_label)
  956. label = "client PEAP encryption";
  957. else
  958. label = "client EAP encryption";
  959. wpa_printf(MSG_DEBUG, "EAP-PEAP: using label '%s' in "
  960. "key derivation", label);
  961. data->key_data =
  962. eap_peer_tls_derive_key(sm, &data->ssl, label,
  963. EAP_TLS_KEY_LEN);
  964. if (data->key_data) {
  965. wpa_hexdump_key(MSG_DEBUG,
  966. "EAP-PEAP: Derived key",
  967. data->key_data,
  968. EAP_TLS_KEY_LEN);
  969. } else {
  970. wpa_printf(MSG_DEBUG, "EAP-PEAP: Failed to "
  971. "derive key");
  972. }
  973. os_free(data->session_id);
  974. data->session_id =
  975. eap_peer_tls_derive_session_id(sm, &data->ssl,
  976. EAP_TYPE_PEAP,
  977. &data->id_len);
  978. if (data->session_id) {
  979. wpa_hexdump(MSG_DEBUG,
  980. "EAP-PEAP: Derived Session-Id",
  981. data->session_id, data->id_len);
  982. } else {
  983. wpa_printf(MSG_ERROR, "EAP-PEAP: Failed to "
  984. "derive Session-Id");
  985. }
  986. if (sm->workaround && data->resuming) {
  987. /*
  988. * At least few RADIUS servers (Aegis v1.1.6;
  989. * but not v1.1.4; and Cisco ACS) seem to be
  990. * terminating PEAPv1 (Aegis) or PEAPv0 (Cisco
  991. * ACS) session resumption with outer
  992. * EAP-Success. This does not seem to follow
  993. * draft-josefsson-pppext-eap-tls-eap-05.txt
  994. * section 4.2, so only allow this if EAP
  995. * workarounds are enabled.
  996. */
  997. wpa_printf(MSG_DEBUG, "EAP-PEAP: Workaround - "
  998. "allow outer EAP-Success to "
  999. "terminate PEAP resumption");
  1000. ret->decision = DECISION_COND_SUCC;
  1001. data->phase2_success = 1;
  1002. }
  1003. data->resuming = 0;
  1004. }
  1005. if (res == 2) {
  1006. /*
  1007. * Application data included in the handshake message.
  1008. */
  1009. wpabuf_free(data->pending_phase2_req);
  1010. data->pending_phase2_req = resp;
  1011. resp = NULL;
  1012. res = eap_peap_decrypt(sm, data, ret, req, &msg,
  1013. &resp);
  1014. }
  1015. }
  1016. if (ret->methodState == METHOD_DONE) {
  1017. ret->allowNotifications = FALSE;
  1018. }
  1019. if (res == 1) {
  1020. wpabuf_free(resp);
  1021. return eap_peer_tls_build_ack(id, EAP_TYPE_PEAP,
  1022. data->peap_version);
  1023. }
  1024. return resp;
  1025. }
  1026. static Boolean eap_peap_has_reauth_data(struct eap_sm *sm, void *priv)
  1027. {
  1028. struct eap_peap_data *data = priv;
  1029. return tls_connection_established(sm->ssl_ctx, data->ssl.conn) &&
  1030. data->phase2_success;
  1031. }
  1032. static void eap_peap_deinit_for_reauth(struct eap_sm *sm, void *priv)
  1033. {
  1034. struct eap_peap_data *data = priv;
  1035. wpabuf_free(data->pending_phase2_req);
  1036. data->pending_phase2_req = NULL;
  1037. wpabuf_free(data->pending_resp);
  1038. data->pending_resp = NULL;
  1039. data->crypto_binding_used = 0;
  1040. }
  1041. static void * eap_peap_init_for_reauth(struct eap_sm *sm, void *priv)
  1042. {
  1043. struct eap_peap_data *data = priv;
  1044. eap_peap_free_key(data);
  1045. os_free(data->session_id);
  1046. data->session_id = NULL;
  1047. if (eap_peer_tls_reauth_init(sm, &data->ssl)) {
  1048. os_free(data);
  1049. return NULL;
  1050. }
  1051. if (data->phase2_priv && data->phase2_method &&
  1052. data->phase2_method->init_for_reauth)
  1053. data->phase2_method->init_for_reauth(sm, data->phase2_priv);
  1054. data->phase2_success = 0;
  1055. data->phase2_eap_success = 0;
  1056. data->phase2_eap_started = 0;
  1057. data->resuming = 1;
  1058. data->reauth = 1;
  1059. sm->peap_done = FALSE;
  1060. return priv;
  1061. }
  1062. static int eap_peap_get_status(struct eap_sm *sm, void *priv, char *buf,
  1063. size_t buflen, int verbose)
  1064. {
  1065. struct eap_peap_data *data = priv;
  1066. int len, ret;
  1067. len = eap_peer_tls_status(sm, &data->ssl, buf, buflen, verbose);
  1068. if (data->phase2_method) {
  1069. ret = os_snprintf(buf + len, buflen - len,
  1070. "EAP-PEAPv%d Phase2 method=%s\n",
  1071. data->peap_version,
  1072. data->phase2_method->name);
  1073. if (os_snprintf_error(buflen - len, ret))
  1074. return len;
  1075. len += ret;
  1076. }
  1077. return len;
  1078. }
  1079. static Boolean eap_peap_isKeyAvailable(struct eap_sm *sm, void *priv)
  1080. {
  1081. struct eap_peap_data *data = priv;
  1082. return data->key_data != NULL && data->phase2_success;
  1083. }
  1084. static u8 * eap_peap_getKey(struct eap_sm *sm, void *priv, size_t *len)
  1085. {
  1086. struct eap_peap_data *data = priv;
  1087. u8 *key;
  1088. if (data->key_data == NULL || !data->phase2_success)
  1089. return NULL;
  1090. key = os_malloc(EAP_TLS_KEY_LEN);
  1091. if (key == NULL)
  1092. return NULL;
  1093. *len = EAP_TLS_KEY_LEN;
  1094. if (data->crypto_binding_used) {
  1095. u8 csk[128];
  1096. /*
  1097. * Note: It looks like Microsoft implementation requires null
  1098. * termination for this label while the one used for deriving
  1099. * IPMK|CMK did not use null termination.
  1100. */
  1101. if (peap_prfplus(data->peap_version, data->ipmk, 40,
  1102. "Session Key Generating Function",
  1103. (u8 *) "\00", 1, csk, sizeof(csk)) < 0) {
  1104. os_free(key);
  1105. return NULL;
  1106. }
  1107. wpa_hexdump_key(MSG_DEBUG, "EAP-PEAP: CSK", csk, sizeof(csk));
  1108. os_memcpy(key, csk, EAP_TLS_KEY_LEN);
  1109. wpa_hexdump(MSG_DEBUG, "EAP-PEAP: Derived key",
  1110. key, EAP_TLS_KEY_LEN);
  1111. } else
  1112. os_memcpy(key, data->key_data, EAP_TLS_KEY_LEN);
  1113. return key;
  1114. }
  1115. static u8 * eap_peap_get_session_id(struct eap_sm *sm, void *priv, size_t *len)
  1116. {
  1117. struct eap_peap_data *data = priv;
  1118. u8 *id;
  1119. if (data->session_id == NULL || !data->phase2_success)
  1120. return NULL;
  1121. id = os_malloc(data->id_len);
  1122. if (id == NULL)
  1123. return NULL;
  1124. *len = data->id_len;
  1125. os_memcpy(id, data->session_id, data->id_len);
  1126. return id;
  1127. }
  1128. int eap_peer_peap_register(void)
  1129. {
  1130. struct eap_method *eap;
  1131. eap = eap_peer_method_alloc(EAP_PEER_METHOD_INTERFACE_VERSION,
  1132. EAP_VENDOR_IETF, EAP_TYPE_PEAP, "PEAP");
  1133. if (eap == NULL)
  1134. return -1;
  1135. eap->init = eap_peap_init;
  1136. eap->deinit = eap_peap_deinit;
  1137. eap->process = eap_peap_process;
  1138. eap->isKeyAvailable = eap_peap_isKeyAvailable;
  1139. eap->getKey = eap_peap_getKey;
  1140. eap->get_status = eap_peap_get_status;
  1141. eap->has_reauth_data = eap_peap_has_reauth_data;
  1142. eap->deinit_for_reauth = eap_peap_deinit_for_reauth;
  1143. eap->init_for_reauth = eap_peap_init_for_reauth;
  1144. eap->getSessionId = eap_peap_get_session_id;
  1145. return eap_peer_method_register(eap);
  1146. }