eap_peap.c 35 KB

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