eap_peap.c 36 KB

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