eap_peap.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436
  1. /*
  2. * hostapd / 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 "sha1.h"
  17. #include "eap_i.h"
  18. #include "eap_tls_common.h"
  19. #include "eap_common/eap_tlv_common.h"
  20. #include "eap_common/eap_peap_common.h"
  21. #include "tls.h"
  22. #include "tncs.h"
  23. /* Maximum supported PEAP version
  24. * 0 = Microsoft's PEAP version 0; draft-kamath-pppext-peapv0-00.txt
  25. * 1 = draft-josefsson-ppext-eap-tls-eap-05.txt
  26. * 2 = draft-josefsson-ppext-eap-tls-eap-10.txt
  27. */
  28. #define EAP_PEAP_VERSION 1
  29. static void eap_peap_reset(struct eap_sm *sm, void *priv);
  30. struct eap_peap_data {
  31. struct eap_ssl_data ssl;
  32. enum {
  33. START, PHASE1, PHASE1_ID2, PHASE2_START, PHASE2_ID,
  34. PHASE2_METHOD, PHASE2_SOH,
  35. PHASE2_TLV, SUCCESS_REQ, FAILURE_REQ, SUCCESS, FAILURE
  36. } state;
  37. int peap_version;
  38. int recv_version;
  39. const struct eap_method *phase2_method;
  40. void *phase2_priv;
  41. int force_version;
  42. struct wpabuf *pending_phase2_resp;
  43. enum { TLV_REQ_NONE, TLV_REQ_SUCCESS, TLV_REQ_FAILURE } tlv_request;
  44. int crypto_binding_sent;
  45. int crypto_binding_used;
  46. enum { NO_BINDING, OPTIONAL_BINDING, REQUIRE_BINDING } crypto_binding;
  47. u8 binding_nonce[32];
  48. u8 ipmk[40];
  49. u8 cmk[20];
  50. u8 *phase2_key;
  51. size_t phase2_key_len;
  52. struct wpabuf *soh_response;
  53. };
  54. static const char * eap_peap_state_txt(int state)
  55. {
  56. switch (state) {
  57. case START:
  58. return "START";
  59. case PHASE1:
  60. return "PHASE1";
  61. case PHASE1_ID2:
  62. return "PHASE1_ID2";
  63. case PHASE2_START:
  64. return "PHASE2_START";
  65. case PHASE2_ID:
  66. return "PHASE2_ID";
  67. case PHASE2_METHOD:
  68. return "PHASE2_METHOD";
  69. case PHASE2_SOH:
  70. return "PHASE2_SOH";
  71. case PHASE2_TLV:
  72. return "PHASE2_TLV";
  73. case SUCCESS_REQ:
  74. return "SUCCESS_REQ";
  75. case FAILURE_REQ:
  76. return "FAILURE_REQ";
  77. case SUCCESS:
  78. return "SUCCESS";
  79. case FAILURE:
  80. return "FAILURE";
  81. default:
  82. return "Unknown?!";
  83. }
  84. }
  85. static void eap_peap_state(struct eap_peap_data *data, int state)
  86. {
  87. wpa_printf(MSG_DEBUG, "EAP-PEAP: %s -> %s",
  88. eap_peap_state_txt(data->state),
  89. eap_peap_state_txt(state));
  90. data->state = state;
  91. }
  92. static struct wpabuf * eap_peapv2_tlv_eap_payload(struct wpabuf *buf)
  93. {
  94. struct wpabuf *e;
  95. struct eap_tlv_hdr *tlv;
  96. if (buf == NULL)
  97. return NULL;
  98. /* Encapsulate EAP packet in EAP-Payload TLV */
  99. wpa_printf(MSG_DEBUG, "EAP-PEAPv2: Add EAP-Payload TLV");
  100. e = wpabuf_alloc(sizeof(*tlv) + wpabuf_len(buf));
  101. if (e == NULL) {
  102. wpa_printf(MSG_DEBUG, "EAP-PEAPv2: Failed to allocate memory "
  103. "for TLV encapsulation");
  104. wpabuf_free(buf);
  105. return NULL;
  106. }
  107. tlv = wpabuf_put(e, sizeof(*tlv));
  108. tlv->tlv_type = host_to_be16(EAP_TLV_TYPE_MANDATORY |
  109. EAP_TLV_EAP_PAYLOAD_TLV);
  110. tlv->length = host_to_be16(wpabuf_len(buf));
  111. wpabuf_put_buf(e, buf);
  112. wpabuf_free(buf);
  113. return e;
  114. }
  115. static void eap_peap_req_success(struct eap_sm *sm,
  116. struct eap_peap_data *data)
  117. {
  118. if (data->state == FAILURE || data->state == FAILURE_REQ) {
  119. eap_peap_state(data, FAILURE);
  120. return;
  121. }
  122. if (data->peap_version == 0) {
  123. data->tlv_request = TLV_REQ_SUCCESS;
  124. eap_peap_state(data, PHASE2_TLV);
  125. } else {
  126. eap_peap_state(data, SUCCESS_REQ);
  127. }
  128. }
  129. static void eap_peap_req_failure(struct eap_sm *sm,
  130. struct eap_peap_data *data)
  131. {
  132. if (data->state == FAILURE || data->state == FAILURE_REQ ||
  133. data->state == SUCCESS_REQ || data->tlv_request != TLV_REQ_NONE) {
  134. eap_peap_state(data, FAILURE);
  135. return;
  136. }
  137. if (data->peap_version == 0) {
  138. data->tlv_request = TLV_REQ_FAILURE;
  139. eap_peap_state(data, PHASE2_TLV);
  140. } else {
  141. eap_peap_state(data, FAILURE_REQ);
  142. }
  143. }
  144. static void * eap_peap_init(struct eap_sm *sm)
  145. {
  146. struct eap_peap_data *data;
  147. data = os_zalloc(sizeof(*data));
  148. if (data == NULL)
  149. return NULL;
  150. data->peap_version = EAP_PEAP_VERSION;
  151. data->force_version = -1;
  152. if (sm->user && sm->user->force_version >= 0) {
  153. data->force_version = sm->user->force_version;
  154. wpa_printf(MSG_DEBUG, "EAP-PEAP: forcing version %d",
  155. data->force_version);
  156. data->peap_version = data->force_version;
  157. }
  158. data->state = START;
  159. data->crypto_binding = OPTIONAL_BINDING;
  160. if (eap_server_tls_ssl_init(sm, &data->ssl, 0)) {
  161. wpa_printf(MSG_INFO, "EAP-PEAP: Failed to initialize SSL.");
  162. eap_peap_reset(sm, data);
  163. return NULL;
  164. }
  165. return data;
  166. }
  167. static void eap_peap_reset(struct eap_sm *sm, void *priv)
  168. {
  169. struct eap_peap_data *data = priv;
  170. if (data == NULL)
  171. return;
  172. if (data->phase2_priv && data->phase2_method)
  173. data->phase2_method->reset(sm, data->phase2_priv);
  174. eap_server_tls_ssl_deinit(sm, &data->ssl);
  175. wpabuf_free(data->pending_phase2_resp);
  176. os_free(data->phase2_key);
  177. wpabuf_free(data->soh_response);
  178. os_free(data);
  179. }
  180. static struct wpabuf * eap_peap_build_start(struct eap_sm *sm,
  181. struct eap_peap_data *data, u8 id)
  182. {
  183. struct wpabuf *req;
  184. req = eap_msg_alloc(EAP_VENDOR_IETF, EAP_TYPE_PEAP, 1,
  185. EAP_CODE_REQUEST, id);
  186. if (req == NULL) {
  187. wpa_printf(MSG_ERROR, "EAP-PEAP: Failed to allocate memory for"
  188. " request");
  189. eap_peap_state(data, FAILURE);
  190. return NULL;
  191. }
  192. wpabuf_put_u8(req, EAP_TLS_FLAGS_START | data->peap_version);
  193. eap_peap_state(data, PHASE1);
  194. return req;
  195. }
  196. static struct wpabuf * eap_peap_build_phase2_req(struct eap_sm *sm,
  197. struct eap_peap_data *data,
  198. u8 id)
  199. {
  200. struct wpabuf *buf, *encr_req;
  201. const u8 *req;
  202. size_t req_len;
  203. if (data->phase2_method == NULL || data->phase2_priv == NULL) {
  204. wpa_printf(MSG_DEBUG, "EAP-PEAP: Phase 2 method not ready");
  205. return NULL;
  206. }
  207. buf = data->phase2_method->buildReq(sm, data->phase2_priv, id);
  208. if (data->peap_version >= 2 && buf)
  209. buf = eap_peapv2_tlv_eap_payload(buf);
  210. if (buf == NULL)
  211. return NULL;
  212. req = wpabuf_head(buf);
  213. req_len = wpabuf_len(buf);
  214. wpa_hexdump_key(MSG_DEBUG, "EAP-PEAP: Encrypting Phase 2 data",
  215. req, req_len);
  216. if (data->peap_version == 0 &&
  217. data->phase2_method->method != EAP_TYPE_TLV) {
  218. req += sizeof(struct eap_hdr);
  219. req_len -= sizeof(struct eap_hdr);
  220. }
  221. encr_req = eap_server_tls_encrypt(sm, &data->ssl, req, req_len);
  222. wpabuf_free(buf);
  223. return encr_req;
  224. }
  225. #ifdef EAP_TNC
  226. static struct wpabuf * eap_peap_build_phase2_soh(struct eap_sm *sm,
  227. struct eap_peap_data *data,
  228. u8 id)
  229. {
  230. struct wpabuf *buf1, *buf, *encr_req;
  231. const u8 *req;
  232. size_t req_len;
  233. buf1 = tncs_build_soh_request();
  234. if (buf1 == NULL)
  235. return NULL;
  236. buf = eap_msg_alloc(EAP_VENDOR_MICROSOFT, 0x21, wpabuf_len(buf1),
  237. EAP_CODE_REQUEST, id);
  238. if (buf == NULL) {
  239. wpabuf_free(buf1);
  240. return NULL;
  241. }
  242. wpabuf_put_buf(buf, buf1);
  243. wpabuf_free(buf1);
  244. req = wpabuf_head(buf);
  245. req_len = wpabuf_len(buf);
  246. wpa_hexdump_key(MSG_DEBUG, "EAP-PEAP: Encrypting Phase 2 SOH data",
  247. req, req_len);
  248. req += sizeof(struct eap_hdr);
  249. req_len -= sizeof(struct eap_hdr);
  250. encr_req = eap_server_tls_encrypt(sm, &data->ssl, req, req_len);
  251. wpabuf_free(buf);
  252. return encr_req;
  253. }
  254. #endif /* EAP_TNC */
  255. static void eap_peap_get_isk(struct eap_peap_data *data,
  256. u8 *isk, size_t isk_len)
  257. {
  258. size_t key_len;
  259. os_memset(isk, 0, isk_len);
  260. if (data->phase2_key == NULL)
  261. return;
  262. key_len = data->phase2_key_len;
  263. if (key_len > isk_len)
  264. key_len = isk_len;
  265. os_memcpy(isk, data->phase2_key, key_len);
  266. }
  267. static int eap_peap_derive_cmk(struct eap_sm *sm, struct eap_peap_data *data)
  268. {
  269. u8 *tk;
  270. u8 isk[32], imck[60];
  271. /*
  272. * Tunnel key (TK) is the first 60 octets of the key generated by
  273. * phase 1 of PEAP (based on TLS).
  274. */
  275. tk = eap_server_tls_derive_key(sm, &data->ssl, "client EAP encryption",
  276. EAP_TLS_KEY_LEN);
  277. if (tk == NULL)
  278. return -1;
  279. wpa_hexdump_key(MSG_DEBUG, "EAP-PEAP: TK", tk, 60);
  280. eap_peap_get_isk(data, isk, sizeof(isk));
  281. wpa_hexdump_key(MSG_DEBUG, "EAP-PEAP: ISK", isk, sizeof(isk));
  282. /*
  283. * IPMK Seed = "Inner Methods Compound Keys" | ISK
  284. * TempKey = First 40 octets of TK
  285. * IPMK|CMK = PRF+(TempKey, IPMK Seed, 60)
  286. * (note: draft-josefsson-pppext-eap-tls-eap-10.txt includes a space
  287. * in the end of the label just before ISK; is that just a typo?)
  288. */
  289. wpa_hexdump_key(MSG_DEBUG, "EAP-PEAP: TempKey", tk, 40);
  290. peap_prfplus(data->peap_version, tk, 40, "Inner Methods Compound Keys",
  291. isk, sizeof(isk), imck, sizeof(imck));
  292. wpa_hexdump_key(MSG_DEBUG, "EAP-PEAP: IMCK (IPMKj)",
  293. imck, sizeof(imck));
  294. os_free(tk);
  295. /* TODO: fast-connect: IPMK|CMK = TK */
  296. os_memcpy(data->ipmk, imck, 40);
  297. wpa_hexdump_key(MSG_DEBUG, "EAP-PEAP: IPMK (S-IPMKj)", data->ipmk, 40);
  298. os_memcpy(data->cmk, imck + 40, 20);
  299. wpa_hexdump_key(MSG_DEBUG, "EAP-PEAP: CMK (CMKj)", data->cmk, 20);
  300. return 0;
  301. }
  302. static struct wpabuf * eap_peap_build_phase2_tlv(struct eap_sm *sm,
  303. struct eap_peap_data *data,
  304. u8 id)
  305. {
  306. struct wpabuf *buf, *encr_req;
  307. size_t len;
  308. len = 6; /* Result TLV */
  309. if (data->crypto_binding != NO_BINDING)
  310. len += 60; /* Cryptobinding TLV */
  311. #ifdef EAP_TNC
  312. if (data->soh_response)
  313. len += wpabuf_len(data->soh_response);
  314. #endif /* EAP_TNC */
  315. buf = eap_msg_alloc(EAP_VENDOR_IETF, EAP_TYPE_TLV, len,
  316. EAP_CODE_REQUEST, id);
  317. if (buf == NULL)
  318. return NULL;
  319. wpabuf_put_u8(buf, 0x80); /* Mandatory */
  320. wpabuf_put_u8(buf, EAP_TLV_RESULT_TLV);
  321. /* Length */
  322. wpabuf_put_be16(buf, 2);
  323. /* Status */
  324. wpabuf_put_be16(buf, data->tlv_request == TLV_REQ_SUCCESS ?
  325. EAP_TLV_RESULT_SUCCESS : EAP_TLV_RESULT_FAILURE);
  326. if (data->peap_version == 0 && data->tlv_request == TLV_REQ_SUCCESS &&
  327. data->crypto_binding != NO_BINDING) {
  328. u8 *mac;
  329. u8 eap_type = EAP_TYPE_PEAP;
  330. const u8 *addr[2];
  331. size_t len[2];
  332. u16 tlv_type;
  333. #ifdef EAP_TNC
  334. if (data->soh_response) {
  335. wpa_printf(MSG_DEBUG, "EAP-PEAP: Adding MS-SOH "
  336. "Response TLV");
  337. wpabuf_put_buf(buf, data->soh_response);
  338. wpabuf_free(data->soh_response);
  339. data->soh_response = NULL;
  340. }
  341. #endif /* EAP_TNC */
  342. if (eap_peap_derive_cmk(sm, data) < 0 ||
  343. os_get_random(data->binding_nonce, 32)) {
  344. wpabuf_free(buf);
  345. return NULL;
  346. }
  347. /* Compound_MAC: HMAC-SHA1-160(cryptobinding TLV | EAP type) */
  348. addr[0] = wpabuf_put(buf, 0);
  349. len[0] = 60;
  350. addr[1] = &eap_type;
  351. len[1] = 1;
  352. tlv_type = EAP_TLV_CRYPTO_BINDING_TLV;
  353. if (data->peap_version >= 2)
  354. tlv_type |= EAP_TLV_TYPE_MANDATORY;
  355. wpabuf_put_be16(buf, tlv_type);
  356. wpabuf_put_be16(buf, 56);
  357. wpabuf_put_u8(buf, 0); /* Reserved */
  358. wpabuf_put_u8(buf, data->peap_version); /* Version */
  359. wpabuf_put_u8(buf, data->recv_version); /* RecvVersion */
  360. wpabuf_put_u8(buf, 0); /* SubType: 0 = Request, 1 = Response */
  361. wpabuf_put_data(buf, data->binding_nonce, 32); /* Nonce */
  362. mac = wpabuf_put(buf, 20); /* Compound_MAC */
  363. wpa_hexdump(MSG_MSGDUMP, "EAP-PEAP: Compound_MAC CMK",
  364. data->cmk, 20);
  365. wpa_hexdump(MSG_MSGDUMP, "EAP-PEAP: Compound_MAC data 1",
  366. addr[0], len[0]);
  367. wpa_hexdump(MSG_MSGDUMP, "EAP-PEAP: Compound_MAC data 2",
  368. addr[1], len[1]);
  369. hmac_sha1_vector(data->cmk, 20, 2, addr, len, mac);
  370. wpa_hexdump(MSG_MSGDUMP, "EAP-PEAP: Compound_MAC",
  371. mac, SHA1_MAC_LEN);
  372. data->crypto_binding_sent = 1;
  373. }
  374. wpa_hexdump_buf_key(MSG_DEBUG, "EAP-PEAP: Encrypting Phase 2 TLV data",
  375. buf);
  376. encr_req = eap_server_tls_encrypt(sm, &data->ssl, wpabuf_head(buf),
  377. wpabuf_len(buf));
  378. wpabuf_free(buf);
  379. return encr_req;
  380. }
  381. static struct wpabuf * eap_peap_build_phase2_term(struct eap_sm *sm,
  382. struct eap_peap_data *data,
  383. u8 id, int success)
  384. {
  385. struct wpabuf *encr_req;
  386. size_t req_len;
  387. struct eap_hdr *hdr;
  388. req_len = sizeof(*hdr);
  389. hdr = os_zalloc(req_len);
  390. if (hdr == NULL)
  391. return NULL;
  392. hdr->code = success ? EAP_CODE_SUCCESS : EAP_CODE_FAILURE;
  393. hdr->identifier = id;
  394. hdr->length = host_to_be16(req_len);
  395. wpa_hexdump_key(MSG_DEBUG, "EAP-PEAP: Encrypting Phase 2 data",
  396. (u8 *) hdr, req_len);
  397. encr_req = eap_server_tls_encrypt(sm, &data->ssl, (u8 *) hdr, req_len);
  398. os_free(hdr);
  399. return encr_req;
  400. }
  401. static struct wpabuf * eap_peap_buildReq(struct eap_sm *sm, void *priv, u8 id)
  402. {
  403. struct eap_peap_data *data = priv;
  404. if (data->ssl.state == FRAG_ACK) {
  405. return eap_server_tls_build_ack(id, EAP_TYPE_PEAP,
  406. data->peap_version);
  407. }
  408. if (data->ssl.state == WAIT_FRAG_ACK) {
  409. return eap_server_tls_build_msg(&data->ssl, EAP_TYPE_PEAP,
  410. data->peap_version, id);
  411. }
  412. switch (data->state) {
  413. case START:
  414. return eap_peap_build_start(sm, data, id);
  415. case PHASE1:
  416. case PHASE1_ID2:
  417. if (data->peap_version < 2 &&
  418. tls_connection_established(sm->ssl_ctx, data->ssl.conn)) {
  419. wpa_printf(MSG_DEBUG, "EAP-PEAP: Phase1 done, "
  420. "starting Phase2");
  421. eap_peap_state(data, PHASE2_START);
  422. }
  423. break;
  424. case PHASE2_ID:
  425. case PHASE2_METHOD:
  426. wpabuf_free(data->ssl.out_buf);
  427. data->ssl.out_used = 0;
  428. data->ssl.out_buf = eap_peap_build_phase2_req(sm, data, id);
  429. break;
  430. #ifdef EAP_TNC
  431. case PHASE2_SOH:
  432. wpabuf_free(data->ssl.out_buf);
  433. data->ssl.out_used = 0;
  434. data->ssl.out_buf = eap_peap_build_phase2_soh(sm, data, id);
  435. break;
  436. #endif /* EAP_TNC */
  437. case PHASE2_TLV:
  438. wpabuf_free(data->ssl.out_buf);
  439. data->ssl.out_used = 0;
  440. data->ssl.out_buf = eap_peap_build_phase2_tlv(sm, data, id);
  441. break;
  442. case SUCCESS_REQ:
  443. wpabuf_free(data->ssl.out_buf);
  444. data->ssl.out_used = 0;
  445. data->ssl.out_buf = eap_peap_build_phase2_term(sm, data, id,
  446. 1);
  447. break;
  448. case FAILURE_REQ:
  449. wpabuf_free(data->ssl.out_buf);
  450. data->ssl.out_used = 0;
  451. data->ssl.out_buf = eap_peap_build_phase2_term(sm, data, id,
  452. 0);
  453. break;
  454. default:
  455. wpa_printf(MSG_DEBUG, "EAP-PEAP: %s - unexpected state %d",
  456. __func__, data->state);
  457. return NULL;
  458. }
  459. return eap_server_tls_build_msg(&data->ssl, EAP_TYPE_PEAP,
  460. data->peap_version, id);
  461. }
  462. static Boolean eap_peap_check(struct eap_sm *sm, void *priv,
  463. struct wpabuf *respData)
  464. {
  465. const u8 *pos;
  466. size_t len;
  467. pos = eap_hdr_validate(EAP_VENDOR_IETF, EAP_TYPE_PEAP, respData, &len);
  468. if (pos == NULL || len < 1) {
  469. wpa_printf(MSG_INFO, "EAP-PEAP: Invalid frame");
  470. return TRUE;
  471. }
  472. return FALSE;
  473. }
  474. static int eap_peap_phase2_init(struct eap_sm *sm, struct eap_peap_data *data,
  475. EapType eap_type)
  476. {
  477. if (data->phase2_priv && data->phase2_method) {
  478. data->phase2_method->reset(sm, data->phase2_priv);
  479. data->phase2_method = NULL;
  480. data->phase2_priv = NULL;
  481. }
  482. data->phase2_method = eap_server_get_eap_method(EAP_VENDOR_IETF,
  483. eap_type);
  484. if (!data->phase2_method)
  485. return -1;
  486. sm->init_phase2 = 1;
  487. data->phase2_priv = data->phase2_method->init(sm);
  488. sm->init_phase2 = 0;
  489. return 0;
  490. }
  491. static int eap_tlv_validate_cryptobinding(struct eap_sm *sm,
  492. struct eap_peap_data *data,
  493. const u8 *crypto_tlv,
  494. size_t crypto_tlv_len)
  495. {
  496. u8 buf[61], mac[SHA1_MAC_LEN];
  497. const u8 *pos;
  498. if (crypto_tlv_len != 4 + 56) {
  499. wpa_printf(MSG_DEBUG, "EAP-PEAP: Invalid cryptobinding TLV "
  500. "length %d", (int) crypto_tlv_len);
  501. return -1;
  502. }
  503. pos = crypto_tlv;
  504. pos += 4; /* TLV header */
  505. if (pos[1] != data->peap_version) {
  506. wpa_printf(MSG_DEBUG, "EAP-PEAP: Cryptobinding TLV Version "
  507. "mismatch (was %d; expected %d)",
  508. pos[1], data->peap_version);
  509. return -1;
  510. }
  511. if (pos[3] != 1) {
  512. wpa_printf(MSG_DEBUG, "EAP-PEAP: Unexpected Cryptobinding TLV "
  513. "SubType %d", pos[3]);
  514. return -1;
  515. }
  516. pos += 4;
  517. pos += 32; /* Nonce */
  518. /* Compound_MAC: HMAC-SHA1-160(cryptobinding TLV | EAP type) */
  519. os_memcpy(buf, crypto_tlv, 60);
  520. os_memset(buf + 4 + 4 + 32, 0, 20); /* Compound_MAC */
  521. buf[60] = EAP_TYPE_PEAP;
  522. hmac_sha1(data->cmk, 20, buf, sizeof(buf), mac);
  523. if (os_memcmp(mac, pos, SHA1_MAC_LEN) != 0) {
  524. wpa_printf(MSG_DEBUG, "EAP-PEAP: Invalid Compound_MAC in "
  525. "cryptobinding TLV");
  526. wpa_hexdump_key(MSG_DEBUG, "EAP-PEAP: CMK", data->cmk, 20);
  527. wpa_hexdump(MSG_DEBUG, "EAP-PEAP: Cryptobinding seed data",
  528. buf, 61);
  529. return -1;
  530. }
  531. wpa_printf(MSG_DEBUG, "EAP-PEAP: Valid cryptobinding TLV received");
  532. return 0;
  533. }
  534. static void eap_peap_process_phase2_tlv(struct eap_sm *sm,
  535. struct eap_peap_data *data,
  536. struct wpabuf *in_data)
  537. {
  538. const u8 *pos;
  539. size_t left;
  540. const u8 *result_tlv = NULL, *crypto_tlv = NULL;
  541. size_t result_tlv_len = 0, crypto_tlv_len = 0;
  542. int tlv_type, mandatory, tlv_len;
  543. pos = eap_hdr_validate(EAP_VENDOR_IETF, EAP_TYPE_TLV, in_data, &left);
  544. if (pos == NULL) {
  545. wpa_printf(MSG_DEBUG, "EAP-PEAP: Invalid EAP-TLV header");
  546. return;
  547. }
  548. /* Parse TLVs */
  549. wpa_hexdump(MSG_DEBUG, "EAP-PEAP: Received TLVs", pos, left);
  550. while (left >= 4) {
  551. mandatory = !!(pos[0] & 0x80);
  552. tlv_type = pos[0] & 0x3f;
  553. tlv_type = (tlv_type << 8) | pos[1];
  554. tlv_len = ((int) pos[2] << 8) | pos[3];
  555. pos += 4;
  556. left -= 4;
  557. if ((size_t) tlv_len > left) {
  558. wpa_printf(MSG_DEBUG, "EAP-PEAP: TLV underrun "
  559. "(tlv_len=%d left=%lu)", tlv_len,
  560. (unsigned long) left);
  561. eap_peap_state(data, FAILURE);
  562. return;
  563. }
  564. switch (tlv_type) {
  565. case EAP_TLV_RESULT_TLV:
  566. result_tlv = pos;
  567. result_tlv_len = tlv_len;
  568. break;
  569. case EAP_TLV_CRYPTO_BINDING_TLV:
  570. crypto_tlv = pos;
  571. crypto_tlv_len = tlv_len;
  572. break;
  573. default:
  574. wpa_printf(MSG_DEBUG, "EAP-PEAP: Unsupported TLV Type "
  575. "%d%s", tlv_type,
  576. mandatory ? " (mandatory)" : "");
  577. if (mandatory) {
  578. eap_peap_state(data, FAILURE);
  579. return;
  580. }
  581. /* Ignore this TLV, but process other TLVs */
  582. break;
  583. }
  584. pos += tlv_len;
  585. left -= tlv_len;
  586. }
  587. if (left) {
  588. wpa_printf(MSG_DEBUG, "EAP-PEAP: Last TLV too short in "
  589. "Request (left=%lu)", (unsigned long) left);
  590. eap_peap_state(data, FAILURE);
  591. return;
  592. }
  593. /* Process supported TLVs */
  594. if (crypto_tlv && data->crypto_binding_sent) {
  595. wpa_hexdump(MSG_DEBUG, "EAP-PEAP: Cryptobinding TLV",
  596. crypto_tlv, crypto_tlv_len);
  597. if (eap_tlv_validate_cryptobinding(sm, data, crypto_tlv - 4,
  598. crypto_tlv_len + 4) < 0) {
  599. eap_peap_state(data, FAILURE);
  600. return;
  601. }
  602. data->crypto_binding_used = 1;
  603. } else if (!crypto_tlv && data->crypto_binding_sent &&
  604. data->crypto_binding == REQUIRE_BINDING) {
  605. wpa_printf(MSG_DEBUG, "EAP-PEAP: No cryptobinding TLV");
  606. eap_peap_state(data, FAILURE);
  607. return;
  608. }
  609. if (result_tlv) {
  610. int status;
  611. const char *requested;
  612. wpa_hexdump(MSG_DEBUG, "EAP-PEAP: Result TLV",
  613. result_tlv, result_tlv_len);
  614. if (result_tlv_len < 2) {
  615. wpa_printf(MSG_INFO, "EAP-PEAP: Too short Result TLV "
  616. "(len=%lu)",
  617. (unsigned long) result_tlv_len);
  618. eap_peap_state(data, FAILURE);
  619. return;
  620. }
  621. requested = data->tlv_request == TLV_REQ_SUCCESS ? "Success" :
  622. "Failure";
  623. status = WPA_GET_BE16(result_tlv);
  624. if (status == EAP_TLV_RESULT_SUCCESS) {
  625. wpa_printf(MSG_INFO, "EAP-PEAP: TLV Result - Success "
  626. "- requested %s", requested);
  627. if (data->tlv_request == TLV_REQ_SUCCESS)
  628. eap_peap_state(data, SUCCESS);
  629. else
  630. eap_peap_state(data, FAILURE);
  631. } else if (status == EAP_TLV_RESULT_FAILURE) {
  632. wpa_printf(MSG_INFO, "EAP-PEAP: TLV Result - Failure "
  633. "- requested %s", requested);
  634. eap_peap_state(data, FAILURE);
  635. } else {
  636. wpa_printf(MSG_INFO, "EAP-PEAP: Unknown TLV Result "
  637. "Status %d", status);
  638. eap_peap_state(data, FAILURE);
  639. }
  640. }
  641. }
  642. #ifdef EAP_TNC
  643. static void eap_peap_process_phase2_soh(struct eap_sm *sm,
  644. struct eap_peap_data *data,
  645. struct wpabuf *in_data)
  646. {
  647. const u8 *pos, *vpos;
  648. size_t left;
  649. const u8 *soh_tlv = NULL;
  650. size_t soh_tlv_len = 0;
  651. int tlv_type, mandatory, tlv_len, vtlv_len;
  652. u8 next_type;
  653. u32 vendor_id;
  654. pos = eap_hdr_validate(EAP_VENDOR_MICROSOFT, 0x21, in_data, &left);
  655. if (pos == NULL) {
  656. wpa_printf(MSG_DEBUG, "EAP-PEAP: Not a valid SoH EAP "
  657. "Extensions Method header - skip TNC");
  658. goto auth_method;
  659. }
  660. /* Parse TLVs */
  661. wpa_hexdump(MSG_DEBUG, "EAP-PEAP: Received TLVs (SoH)", pos, left);
  662. while (left >= 4) {
  663. mandatory = !!(pos[0] & 0x80);
  664. tlv_type = pos[0] & 0x3f;
  665. tlv_type = (tlv_type << 8) | pos[1];
  666. tlv_len = ((int) pos[2] << 8) | pos[3];
  667. pos += 4;
  668. left -= 4;
  669. if ((size_t) tlv_len > left) {
  670. wpa_printf(MSG_DEBUG, "EAP-PEAP: TLV underrun "
  671. "(tlv_len=%d left=%lu)", tlv_len,
  672. (unsigned long) left);
  673. eap_peap_state(data, FAILURE);
  674. return;
  675. }
  676. switch (tlv_type) {
  677. case EAP_TLV_VENDOR_SPECIFIC_TLV:
  678. if (tlv_len < 4) {
  679. wpa_printf(MSG_DEBUG, "EAP-PEAP: Too short "
  680. "vendor specific TLV (len=%d)",
  681. (int) tlv_len);
  682. eap_peap_state(data, FAILURE);
  683. return;
  684. }
  685. vendor_id = WPA_GET_BE32(pos);
  686. if (vendor_id != EAP_VENDOR_MICROSOFT) {
  687. if (mandatory) {
  688. eap_peap_state(data, FAILURE);
  689. return;
  690. }
  691. break;
  692. }
  693. vpos = pos + 4;
  694. mandatory = !!(vpos[0] & 0x80);
  695. tlv_type = vpos[0] & 0x3f;
  696. tlv_type = (tlv_type << 8) | vpos[1];
  697. vtlv_len = ((int) vpos[2] << 8) | vpos[3];
  698. vpos += 4;
  699. if (vpos + vtlv_len > pos + left) {
  700. wpa_printf(MSG_DEBUG, "EAP-PEAP: Vendor TLV "
  701. "underrun");
  702. eap_peap_state(data, FAILURE);
  703. return;
  704. }
  705. if (tlv_type == 1) {
  706. soh_tlv = vpos;
  707. soh_tlv_len = vtlv_len;
  708. break;
  709. }
  710. wpa_printf(MSG_DEBUG, "EAP-PEAP: Unsupported MS-TLV "
  711. "Type %d%s", tlv_type,
  712. mandatory ? " (mandatory)" : "");
  713. if (mandatory) {
  714. eap_peap_state(data, FAILURE);
  715. return;
  716. }
  717. /* Ignore this TLV, but process other TLVs */
  718. break;
  719. default:
  720. wpa_printf(MSG_DEBUG, "EAP-PEAP: Unsupported TLV Type "
  721. "%d%s", tlv_type,
  722. mandatory ? " (mandatory)" : "");
  723. if (mandatory) {
  724. eap_peap_state(data, FAILURE);
  725. return;
  726. }
  727. /* Ignore this TLV, but process other TLVs */
  728. break;
  729. }
  730. pos += tlv_len;
  731. left -= tlv_len;
  732. }
  733. if (left) {
  734. wpa_printf(MSG_DEBUG, "EAP-PEAP: Last TLV too short in "
  735. "Request (left=%lu)", (unsigned long) left);
  736. eap_peap_state(data, FAILURE);
  737. return;
  738. }
  739. /* Process supported TLVs */
  740. if (soh_tlv) {
  741. int failure = 0;
  742. wpabuf_free(data->soh_response);
  743. data->soh_response = tncs_process_soh(soh_tlv, soh_tlv_len,
  744. &failure);
  745. if (failure) {
  746. eap_peap_state(data, FAILURE);
  747. return;
  748. }
  749. } else {
  750. wpa_printf(MSG_DEBUG, "EAP-PEAP: No SoH TLV received");
  751. eap_peap_state(data, FAILURE);
  752. return;
  753. }
  754. auth_method:
  755. eap_peap_state(data, PHASE2_METHOD);
  756. next_type = sm->user->methods[0].method;
  757. sm->user_eap_method_index = 1;
  758. wpa_printf(MSG_DEBUG, "EAP-PEAP: try EAP type %d", next_type);
  759. eap_peap_phase2_init(sm, data, next_type);
  760. }
  761. #endif /* EAP_TNC */
  762. static void eap_peap_process_phase2_response(struct eap_sm *sm,
  763. struct eap_peap_data *data,
  764. struct wpabuf *in_data)
  765. {
  766. u8 next_type = EAP_TYPE_NONE;
  767. const struct eap_hdr *hdr;
  768. const u8 *pos;
  769. size_t left;
  770. if (data->state == PHASE2_TLV) {
  771. eap_peap_process_phase2_tlv(sm, data, in_data);
  772. return;
  773. }
  774. #ifdef EAP_TNC
  775. if (data->state == PHASE2_SOH) {
  776. eap_peap_process_phase2_soh(sm, data, in_data);
  777. return;
  778. }
  779. #endif /* EAP_TNC */
  780. if (data->phase2_priv == NULL) {
  781. wpa_printf(MSG_DEBUG, "EAP-PEAP: %s - Phase2 not "
  782. "initialized?!", __func__);
  783. return;
  784. }
  785. hdr = wpabuf_head(in_data);
  786. pos = (const u8 *) (hdr + 1);
  787. if (wpabuf_len(in_data) > sizeof(*hdr) && *pos == EAP_TYPE_NAK) {
  788. left = wpabuf_len(in_data) - sizeof(*hdr);
  789. wpa_hexdump(MSG_DEBUG, "EAP-PEAP: Phase2 type Nak'ed; "
  790. "allowed types", pos + 1, left - 1);
  791. eap_sm_process_nak(sm, pos + 1, left - 1);
  792. if (sm->user && sm->user_eap_method_index < EAP_MAX_METHODS &&
  793. sm->user->methods[sm->user_eap_method_index].method !=
  794. EAP_TYPE_NONE) {
  795. next_type = sm->user->methods[
  796. sm->user_eap_method_index++].method;
  797. wpa_printf(MSG_DEBUG, "EAP-PEAP: try EAP type %d",
  798. next_type);
  799. } else {
  800. eap_peap_req_failure(sm, data);
  801. next_type = EAP_TYPE_NONE;
  802. }
  803. eap_peap_phase2_init(sm, data, next_type);
  804. return;
  805. }
  806. if (data->phase2_method->check(sm, data->phase2_priv, in_data)) {
  807. wpa_printf(MSG_DEBUG, "EAP-PEAP: Phase2 check() asked to "
  808. "ignore the packet");
  809. return;
  810. }
  811. data->phase2_method->process(sm, data->phase2_priv, in_data);
  812. if (sm->method_pending == METHOD_PENDING_WAIT) {
  813. wpa_printf(MSG_DEBUG, "EAP-PEAP: Phase2 method is in "
  814. "pending wait state - save decrypted response");
  815. wpabuf_free(data->pending_phase2_resp);
  816. data->pending_phase2_resp = wpabuf_dup(in_data);
  817. }
  818. if (!data->phase2_method->isDone(sm, data->phase2_priv))
  819. return;
  820. if (!data->phase2_method->isSuccess(sm, data->phase2_priv)) {
  821. wpa_printf(MSG_DEBUG, "EAP-PEAP: Phase2 method failed");
  822. eap_peap_req_failure(sm, data);
  823. next_type = EAP_TYPE_NONE;
  824. eap_peap_phase2_init(sm, data, next_type);
  825. return;
  826. }
  827. os_free(data->phase2_key);
  828. if (data->phase2_method->getKey) {
  829. data->phase2_key = data->phase2_method->getKey(
  830. sm, data->phase2_priv, &data->phase2_key_len);
  831. if (data->phase2_key == NULL) {
  832. wpa_printf(MSG_DEBUG, "EAP-PEAP: Phase2 getKey "
  833. "failed");
  834. eap_peap_req_failure(sm, data);
  835. eap_peap_phase2_init(sm, data, EAP_TYPE_NONE);
  836. return;
  837. }
  838. if (data->phase2_key_len == 32 &&
  839. data->phase2_method->vendor == EAP_VENDOR_IETF &&
  840. data->phase2_method->method == EAP_TYPE_MSCHAPV2) {
  841. /*
  842. * Microsoft uses reverse order for MS-MPPE keys in
  843. * EAP-PEAP when compared to EAP-FAST derivation of
  844. * ISK. Swap the keys here to get the correct ISK for
  845. * EAP-PEAPv0 cryptobinding.
  846. */
  847. u8 tmp[16];
  848. os_memcpy(tmp, data->phase2_key, 16);
  849. os_memcpy(data->phase2_key, data->phase2_key + 16, 16);
  850. os_memcpy(data->phase2_key + 16, tmp, 16);
  851. }
  852. }
  853. switch (data->state) {
  854. case PHASE1_ID2:
  855. case PHASE2_ID:
  856. case PHASE2_SOH:
  857. if (eap_user_get(sm, sm->identity, sm->identity_len, 1) != 0) {
  858. wpa_hexdump_ascii(MSG_DEBUG, "EAP_PEAP: Phase2 "
  859. "Identity not found in the user "
  860. "database",
  861. sm->identity, sm->identity_len);
  862. eap_peap_req_failure(sm, data);
  863. next_type = EAP_TYPE_NONE;
  864. break;
  865. }
  866. #ifdef EAP_TNC
  867. if (data->state != PHASE2_SOH && sm->tnc &&
  868. data->peap_version == 0) {
  869. eap_peap_state(data, PHASE2_SOH);
  870. wpa_printf(MSG_DEBUG, "EAP-PEAP: Try to initialize "
  871. "TNC (NAP SOH)");
  872. next_type = EAP_TYPE_NONE;
  873. break;
  874. }
  875. #endif /* EAP_TNC */
  876. eap_peap_state(data, PHASE2_METHOD);
  877. next_type = sm->user->methods[0].method;
  878. sm->user_eap_method_index = 1;
  879. wpa_printf(MSG_DEBUG, "EAP-PEAP: try EAP type %d", next_type);
  880. break;
  881. case PHASE2_METHOD:
  882. eap_peap_req_success(sm, data);
  883. next_type = EAP_TYPE_NONE;
  884. break;
  885. case FAILURE:
  886. break;
  887. default:
  888. wpa_printf(MSG_DEBUG, "EAP-PEAP: %s - unexpected state %d",
  889. __func__, data->state);
  890. break;
  891. }
  892. eap_peap_phase2_init(sm, data, next_type);
  893. }
  894. static void eap_peap_process_phase2(struct eap_sm *sm,
  895. struct eap_peap_data *data,
  896. const struct wpabuf *respData,
  897. struct wpabuf *in_buf)
  898. {
  899. struct wpabuf *in_decrypted;
  900. int len_decrypted;
  901. const struct eap_hdr *hdr;
  902. size_t buf_len, len;
  903. u8 *in_data;
  904. size_t in_len;
  905. in_data = wpabuf_mhead(in_buf);
  906. in_len = wpabuf_len(in_buf);
  907. wpa_printf(MSG_DEBUG, "EAP-PEAP: received %lu bytes encrypted data for"
  908. " Phase 2", (unsigned long) in_len);
  909. if (data->pending_phase2_resp) {
  910. wpa_printf(MSG_DEBUG, "EAP-PEAP: Pending Phase 2 response - "
  911. "skip decryption and use old data");
  912. eap_peap_process_phase2_response(sm, data,
  913. data->pending_phase2_resp);
  914. wpabuf_free(data->pending_phase2_resp);
  915. data->pending_phase2_resp = NULL;
  916. return;
  917. }
  918. buf_len = in_len;
  919. /*
  920. * Even though we try to disable TLS compression, it is possible that
  921. * this cannot be done with all TLS libraries. Add extra buffer space
  922. * to handle the possibility of the decrypted data being longer than
  923. * input data.
  924. */
  925. buf_len += 500;
  926. buf_len *= 3;
  927. in_decrypted = wpabuf_alloc(buf_len);
  928. if (in_decrypted == NULL) {
  929. wpa_printf(MSG_WARNING, "EAP-PEAP: failed to allocate memory "
  930. "for decryption");
  931. return;
  932. }
  933. len_decrypted = tls_connection_decrypt(sm->ssl_ctx, data->ssl.conn,
  934. in_data, in_len,
  935. wpabuf_mhead(in_decrypted),
  936. buf_len);
  937. if (len_decrypted < 0) {
  938. wpa_printf(MSG_INFO, "EAP-PEAP: Failed to decrypt Phase 2 "
  939. "data");
  940. wpabuf_free(in_decrypted);
  941. eap_peap_state(data, FAILURE);
  942. return;
  943. }
  944. wpabuf_put(in_decrypted, len_decrypted);
  945. wpa_hexdump_buf_key(MSG_DEBUG, "EAP-PEAP: Decrypted Phase 2 EAP",
  946. in_decrypted);
  947. hdr = wpabuf_head(in_decrypted);
  948. if (data->peap_version == 0 && data->state != PHASE2_TLV) {
  949. const struct eap_hdr *resp;
  950. struct eap_hdr *nhdr;
  951. struct wpabuf *nbuf =
  952. wpabuf_alloc(sizeof(struct eap_hdr) +
  953. wpabuf_len(in_decrypted));
  954. if (nbuf == NULL) {
  955. wpabuf_free(in_decrypted);
  956. return;
  957. }
  958. resp = wpabuf_head(respData);
  959. nhdr = wpabuf_put(nbuf, sizeof(*nhdr));
  960. nhdr->code = resp->code;
  961. nhdr->identifier = resp->identifier;
  962. nhdr->length = host_to_be16(sizeof(struct eap_hdr) +
  963. wpabuf_len(in_decrypted));
  964. wpabuf_put_buf(nbuf, in_decrypted);
  965. wpabuf_free(in_decrypted);
  966. in_decrypted = nbuf;
  967. } else if (data->peap_version >= 2) {
  968. struct eap_tlv_hdr *tlv;
  969. struct wpabuf *nmsg;
  970. if (wpabuf_len(in_decrypted) < sizeof(*tlv) + sizeof(*hdr)) {
  971. wpa_printf(MSG_INFO, "EAP-PEAPv2: Too short Phase 2 "
  972. "EAP TLV");
  973. wpabuf_free(in_decrypted);
  974. return;
  975. }
  976. tlv = wpabuf_mhead(in_decrypted);
  977. if ((be_to_host16(tlv->tlv_type) & EAP_TLV_TYPE_MASK) !=
  978. EAP_TLV_EAP_PAYLOAD_TLV) {
  979. wpa_printf(MSG_INFO, "EAP-PEAPv2: Not an EAP TLV");
  980. wpabuf_free(in_decrypted);
  981. return;
  982. }
  983. if (sizeof(*tlv) + be_to_host16(tlv->length) >
  984. wpabuf_len(in_decrypted)) {
  985. wpa_printf(MSG_INFO, "EAP-PEAPv2: Invalid EAP TLV "
  986. "length");
  987. wpabuf_free(in_decrypted);
  988. return;
  989. }
  990. hdr = (struct eap_hdr *) (tlv + 1);
  991. if (be_to_host16(hdr->length) > be_to_host16(tlv->length)) {
  992. wpa_printf(MSG_INFO, "EAP-PEAPv2: No room for full "
  993. "EAP packet in EAP TLV");
  994. wpabuf_free(in_decrypted);
  995. return;
  996. }
  997. nmsg = wpabuf_alloc(be_to_host16(hdr->length));
  998. if (nmsg == NULL) {
  999. wpabuf_free(in_decrypted);
  1000. return;
  1001. }
  1002. wpabuf_put_data(nmsg, hdr, be_to_host16(hdr->length));
  1003. wpabuf_free(in_decrypted);
  1004. in_decrypted = nmsg;
  1005. }
  1006. hdr = wpabuf_head(in_decrypted);
  1007. if (wpabuf_len(in_decrypted) < (int) sizeof(*hdr)) {
  1008. wpa_printf(MSG_INFO, "EAP-PEAP: Too short Phase 2 "
  1009. "EAP frame (len=%lu)",
  1010. (unsigned long) wpabuf_len(in_decrypted));
  1011. wpabuf_free(in_decrypted);
  1012. eap_peap_req_failure(sm, data);
  1013. return;
  1014. }
  1015. len = be_to_host16(hdr->length);
  1016. if (len > wpabuf_len(in_decrypted)) {
  1017. wpa_printf(MSG_INFO, "EAP-PEAP: Length mismatch in "
  1018. "Phase 2 EAP frame (len=%lu hdr->length=%lu)",
  1019. (unsigned long) wpabuf_len(in_decrypted),
  1020. (unsigned long) len);
  1021. wpabuf_free(in_decrypted);
  1022. eap_peap_req_failure(sm, data);
  1023. return;
  1024. }
  1025. wpa_printf(MSG_DEBUG, "EAP-PEAP: received Phase 2: code=%d "
  1026. "identifier=%d length=%lu", hdr->code, hdr->identifier,
  1027. (unsigned long) len);
  1028. switch (hdr->code) {
  1029. case EAP_CODE_RESPONSE:
  1030. eap_peap_process_phase2_response(sm, data, in_decrypted);
  1031. break;
  1032. case EAP_CODE_SUCCESS:
  1033. wpa_printf(MSG_DEBUG, "EAP-PEAP: Phase 2 Success");
  1034. if (data->state == SUCCESS_REQ) {
  1035. eap_peap_state(data, SUCCESS);
  1036. }
  1037. break;
  1038. case EAP_CODE_FAILURE:
  1039. wpa_printf(MSG_DEBUG, "EAP-PEAP: Phase 2 Failure");
  1040. eap_peap_state(data, FAILURE);
  1041. break;
  1042. default:
  1043. wpa_printf(MSG_INFO, "EAP-PEAP: Unexpected code=%d in "
  1044. "Phase 2 EAP header", hdr->code);
  1045. break;
  1046. }
  1047. os_free(in_decrypted);
  1048. }
  1049. static int eap_peapv2_start_phase2(struct eap_sm *sm,
  1050. struct eap_peap_data *data)
  1051. {
  1052. struct wpabuf *buf, *buf2;
  1053. int res;
  1054. wpa_printf(MSG_DEBUG, "EAP-PEAPv2: Phase1 done, include first Phase2 "
  1055. "payload in the same message");
  1056. eap_peap_state(data, PHASE1_ID2);
  1057. if (eap_peap_phase2_init(sm, data, EAP_TYPE_IDENTITY))
  1058. return -1;
  1059. /* TODO: which Id to use here? */
  1060. buf = data->phase2_method->buildReq(sm, data->phase2_priv, 6);
  1061. if (buf == NULL)
  1062. return -1;
  1063. buf2 = eap_peapv2_tlv_eap_payload(buf);
  1064. if (buf2 == NULL)
  1065. return -1;
  1066. wpa_hexdump_buf(MSG_DEBUG, "EAP-PEAPv2: Identity Request", buf2);
  1067. buf = wpabuf_alloc(data->ssl.tls_out_limit);
  1068. if (buf == NULL) {
  1069. wpabuf_free(buf2);
  1070. return -1;
  1071. }
  1072. res = tls_connection_encrypt(sm->ssl_ctx, data->ssl.conn,
  1073. wpabuf_head(buf2), wpabuf_len(buf2),
  1074. wpabuf_put(buf, 0),
  1075. data->ssl.tls_out_limit);
  1076. wpabuf_free(buf2);
  1077. if (res < 0) {
  1078. wpa_printf(MSG_INFO, "EAP-PEAPv2: Failed to encrypt Phase 2 "
  1079. "data");
  1080. wpabuf_free(buf);
  1081. return -1;
  1082. }
  1083. wpabuf_put(buf, res);
  1084. wpa_hexdump_buf(MSG_DEBUG, "EAP-PEAPv2: Encrypted Identity Request",
  1085. buf);
  1086. /* Append TLS data into the pending buffer after the Server Finished */
  1087. if (wpabuf_resize(&data->ssl.out_buf, wpabuf_len(buf)) < 0) {
  1088. wpabuf_free(buf);
  1089. return -1;
  1090. }
  1091. wpabuf_put_buf(data->ssl.out_buf, buf);
  1092. wpabuf_free(buf);
  1093. return 0;
  1094. }
  1095. static int eap_peap_process_version(struct eap_sm *sm, void *priv,
  1096. int peer_version)
  1097. {
  1098. struct eap_peap_data *data = priv;
  1099. data->recv_version = peer_version;
  1100. if (data->force_version >= 0 && peer_version != data->force_version) {
  1101. wpa_printf(MSG_INFO, "EAP-PEAP: peer did not select the forced"
  1102. " version (forced=%d peer=%d) - reject",
  1103. data->force_version, peer_version);
  1104. return -1;
  1105. }
  1106. if (peer_version < data->peap_version) {
  1107. wpa_printf(MSG_DEBUG, "EAP-PEAP: peer ver=%d, own ver=%d; "
  1108. "use version %d",
  1109. peer_version, data->peap_version, peer_version);
  1110. data->peap_version = peer_version;
  1111. }
  1112. return 0;
  1113. }
  1114. static void eap_peap_process_msg(struct eap_sm *sm, void *priv,
  1115. const struct wpabuf *respData)
  1116. {
  1117. struct eap_peap_data *data = priv;
  1118. switch (data->state) {
  1119. case PHASE1:
  1120. if (eap_server_tls_phase1(sm, &data->ssl) < 0) {
  1121. eap_peap_state(data, FAILURE);
  1122. break;
  1123. }
  1124. if (data->peap_version >= 2 &&
  1125. tls_connection_established(sm->ssl_ctx, data->ssl.conn)) {
  1126. if (eap_peapv2_start_phase2(sm, data)) {
  1127. eap_peap_state(data, FAILURE);
  1128. break;
  1129. }
  1130. }
  1131. break;
  1132. case PHASE2_START:
  1133. eap_peap_state(data, PHASE2_ID);
  1134. eap_peap_phase2_init(sm, data, EAP_TYPE_IDENTITY);
  1135. break;
  1136. case PHASE1_ID2:
  1137. case PHASE2_ID:
  1138. case PHASE2_METHOD:
  1139. case PHASE2_SOH:
  1140. case PHASE2_TLV:
  1141. eap_peap_process_phase2(sm, data, respData, data->ssl.in_buf);
  1142. break;
  1143. case SUCCESS_REQ:
  1144. eap_peap_state(data, SUCCESS);
  1145. break;
  1146. case FAILURE_REQ:
  1147. eap_peap_state(data, FAILURE);
  1148. break;
  1149. default:
  1150. wpa_printf(MSG_DEBUG, "EAP-PEAP: Unexpected state %d in %s",
  1151. data->state, __func__);
  1152. break;
  1153. }
  1154. }
  1155. static void eap_peap_process(struct eap_sm *sm, void *priv,
  1156. struct wpabuf *respData)
  1157. {
  1158. struct eap_peap_data *data = priv;
  1159. if (eap_server_tls_process(sm, &data->ssl, respData, data,
  1160. EAP_TYPE_PEAP, eap_peap_process_version,
  1161. eap_peap_process_msg) < 0)
  1162. eap_peap_state(data, FAILURE);
  1163. }
  1164. static Boolean eap_peap_isDone(struct eap_sm *sm, void *priv)
  1165. {
  1166. struct eap_peap_data *data = priv;
  1167. return data->state == SUCCESS || data->state == FAILURE;
  1168. }
  1169. static u8 * eap_peap_getKey(struct eap_sm *sm, void *priv, size_t *len)
  1170. {
  1171. struct eap_peap_data *data = priv;
  1172. u8 *eapKeyData;
  1173. if (data->state != SUCCESS)
  1174. return NULL;
  1175. if (data->crypto_binding_used) {
  1176. u8 csk[128];
  1177. /*
  1178. * Note: It looks like Microsoft implementation requires null
  1179. * termination for this label while the one used for deriving
  1180. * IPMK|CMK did not use null termination.
  1181. */
  1182. peap_prfplus(data->peap_version, data->ipmk, 40,
  1183. "Session Key Generating Function",
  1184. (u8 *) "\00", 1, csk, sizeof(csk));
  1185. wpa_hexdump_key(MSG_DEBUG, "EAP-PEAP: CSK", csk, sizeof(csk));
  1186. eapKeyData = os_malloc(EAP_TLS_KEY_LEN);
  1187. if (eapKeyData) {
  1188. os_memcpy(eapKeyData, csk, EAP_TLS_KEY_LEN);
  1189. *len = EAP_TLS_KEY_LEN;
  1190. wpa_hexdump(MSG_DEBUG, "EAP-PEAP: Derived key",
  1191. eapKeyData, EAP_TLS_KEY_LEN);
  1192. } else {
  1193. wpa_printf(MSG_DEBUG, "EAP-PEAP: Failed to derive "
  1194. "key");
  1195. }
  1196. return eapKeyData;
  1197. }
  1198. /* TODO: PEAPv1 - different label in some cases */
  1199. eapKeyData = eap_server_tls_derive_key(sm, &data->ssl,
  1200. "client EAP encryption",
  1201. EAP_TLS_KEY_LEN);
  1202. if (eapKeyData) {
  1203. *len = EAP_TLS_KEY_LEN;
  1204. wpa_hexdump(MSG_DEBUG, "EAP-PEAP: Derived key",
  1205. eapKeyData, EAP_TLS_KEY_LEN);
  1206. } else {
  1207. wpa_printf(MSG_DEBUG, "EAP-PEAP: Failed to derive key");
  1208. }
  1209. return eapKeyData;
  1210. }
  1211. static Boolean eap_peap_isSuccess(struct eap_sm *sm, void *priv)
  1212. {
  1213. struct eap_peap_data *data = priv;
  1214. return data->state == SUCCESS;
  1215. }
  1216. int eap_server_peap_register(void)
  1217. {
  1218. struct eap_method *eap;
  1219. int ret;
  1220. eap = eap_server_method_alloc(EAP_SERVER_METHOD_INTERFACE_VERSION,
  1221. EAP_VENDOR_IETF, EAP_TYPE_PEAP, "PEAP");
  1222. if (eap == NULL)
  1223. return -1;
  1224. eap->init = eap_peap_init;
  1225. eap->reset = eap_peap_reset;
  1226. eap->buildReq = eap_peap_buildReq;
  1227. eap->check = eap_peap_check;
  1228. eap->process = eap_peap_process;
  1229. eap->isDone = eap_peap_isDone;
  1230. eap->getKey = eap_peap_getKey;
  1231. eap->isSuccess = eap_peap_isSuccess;
  1232. ret = eap_server_method_register(eap);
  1233. if (ret)
  1234. eap_server_method_free(eap);
  1235. return ret;
  1236. }