eap_fast.c 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715
  1. /*
  2. * EAP peer method: EAP-FAST (RFC 4851)
  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 "eap_i.h"
  17. #include "eap_tls_common.h"
  18. #include "eap_config.h"
  19. #include "tls.h"
  20. #include "eap_common/eap_tlv_common.h"
  21. #include "sha1.h"
  22. #include "eap_fast_pac.h"
  23. #ifdef EAP_FAST_DYNAMIC
  24. #include "eap_fast_pac.c"
  25. #endif /* EAP_FAST_DYNAMIC */
  26. /* TODO:
  27. * - test session resumption and enable it if it interoperates
  28. * - password change (pending mschapv2 packet; replay decrypted packet)
  29. */
  30. static void eap_fast_deinit(struct eap_sm *sm, void *priv);
  31. struct eap_fast_data {
  32. struct eap_ssl_data ssl;
  33. int fast_version;
  34. const struct eap_method *phase2_method;
  35. void *phase2_priv;
  36. int phase2_success;
  37. struct eap_method_type phase2_type;
  38. struct eap_method_type *phase2_types;
  39. size_t num_phase2_types;
  40. int resuming; /* starting a resumed session */
  41. struct eap_fast_key_block_provisioning *key_block_p;
  42. #define EAP_FAST_PROV_UNAUTH 1
  43. #define EAP_FAST_PROV_AUTH 2
  44. int provisioning_allowed; /* Allowed PAC provisioning modes */
  45. int provisioning; /* doing PAC provisioning (not the normal auth) */
  46. int anon_provisioning; /* doing anonymous (unauthenticated)
  47. * provisioning */
  48. int session_ticket_used;
  49. u8 key_data[EAP_FAST_KEY_LEN];
  50. u8 emsk[EAP_EMSK_LEN];
  51. int success;
  52. struct eap_fast_pac *pac;
  53. struct eap_fast_pac *current_pac;
  54. size_t max_pac_list_len;
  55. int use_pac_binary_format;
  56. u8 simck[EAP_FAST_SIMCK_LEN];
  57. int simck_idx;
  58. struct wpabuf *pending_phase2_req;
  59. };
  60. static int eap_fast_session_ticket_cb(void *ctx, const u8 *ticket, size_t len,
  61. const u8 *client_random,
  62. const u8 *server_random,
  63. u8 *master_secret)
  64. {
  65. struct eap_fast_data *data = ctx;
  66. wpa_printf(MSG_DEBUG, "EAP-FAST: SessionTicket callback");
  67. if (client_random == NULL || server_random == NULL ||
  68. master_secret == NULL) {
  69. wpa_printf(MSG_DEBUG, "EAP-FAST: SessionTicket failed - fall "
  70. "back to full TLS handshake");
  71. data->session_ticket_used = 0;
  72. if (data->provisioning_allowed) {
  73. wpa_printf(MSG_DEBUG, "EAP-FAST: Try to provision a "
  74. "new PAC-Key");
  75. data->provisioning = 1;
  76. data->current_pac = NULL;
  77. }
  78. return 0;
  79. }
  80. wpa_hexdump(MSG_DEBUG, "EAP-FAST: SessionTicket", ticket, len);
  81. if (data->current_pac == NULL) {
  82. wpa_printf(MSG_DEBUG, "EAP-FAST: No PAC-Key available for "
  83. "using SessionTicket");
  84. data->session_ticket_used = 0;
  85. return 0;
  86. }
  87. eap_fast_derive_master_secret(data->current_pac->pac_key,
  88. server_random, client_random,
  89. master_secret);
  90. data->session_ticket_used = 1;
  91. return 1;
  92. }
  93. static int eap_fast_parse_phase1(struct eap_fast_data *data,
  94. const char *phase1)
  95. {
  96. const char *pos;
  97. pos = os_strstr(phase1, "fast_provisioning=");
  98. if (pos) {
  99. data->provisioning_allowed = atoi(pos + 18);
  100. wpa_printf(MSG_DEBUG, "EAP-FAST: Automatic PAC provisioning "
  101. "mode: %d", data->provisioning_allowed);
  102. }
  103. pos = os_strstr(phase1, "fast_max_pac_list_len=");
  104. if (pos) {
  105. data->max_pac_list_len = atoi(pos + 22);
  106. if (data->max_pac_list_len == 0)
  107. data->max_pac_list_len = 1;
  108. wpa_printf(MSG_DEBUG, "EAP-FAST: Maximum PAC list length: %lu",
  109. (unsigned long) data->max_pac_list_len);
  110. }
  111. pos = os_strstr(phase1, "fast_pac_format=binary");
  112. if (pos) {
  113. data->use_pac_binary_format = 1;
  114. wpa_printf(MSG_DEBUG, "EAP-FAST: Using binary format for PAC "
  115. "list");
  116. }
  117. return 0;
  118. }
  119. static void * eap_fast_init(struct eap_sm *sm)
  120. {
  121. struct eap_fast_data *data;
  122. struct eap_peer_config *config = eap_get_config(sm);
  123. data = os_zalloc(sizeof(*data));
  124. if (data == NULL)
  125. return NULL;
  126. data->fast_version = EAP_FAST_VERSION;
  127. data->max_pac_list_len = 10;
  128. if (config && config->phase1 &&
  129. eap_fast_parse_phase1(data, config->phase1) < 0) {
  130. eap_fast_deinit(sm, data);
  131. return NULL;
  132. }
  133. if (eap_peer_select_phase2_methods(config, "auth=",
  134. &data->phase2_types,
  135. &data->num_phase2_types) < 0) {
  136. eap_fast_deinit(sm, data);
  137. return NULL;
  138. }
  139. data->phase2_type.vendor = EAP_VENDOR_IETF;
  140. data->phase2_type.method = EAP_TYPE_NONE;
  141. if (eap_peer_tls_ssl_init(sm, &data->ssl, config)) {
  142. wpa_printf(MSG_INFO, "EAP-FAST: Failed to initialize SSL.");
  143. eap_fast_deinit(sm, data);
  144. return NULL;
  145. }
  146. if (tls_connection_set_session_ticket_cb(sm->ssl_ctx, data->ssl.conn,
  147. eap_fast_session_ticket_cb,
  148. data) < 0) {
  149. wpa_printf(MSG_INFO, "EAP-FAST: Failed to set SessionTicket "
  150. "callback");
  151. eap_fast_deinit(sm, data);
  152. return NULL;
  153. }
  154. /*
  155. * The local RADIUS server in a Cisco AP does not seem to like empty
  156. * fragments before data, so disable that workaround for CBC.
  157. * TODO: consider making this configurable
  158. */
  159. if (tls_connection_enable_workaround(sm->ssl_ctx, data->ssl.conn)) {
  160. wpa_printf(MSG_DEBUG, "EAP-FAST: Failed to enable TLS "
  161. "workarounds");
  162. }
  163. if (data->use_pac_binary_format &&
  164. eap_fast_load_pac_bin(sm, &data->pac, config->pac_file) < 0) {
  165. eap_fast_deinit(sm, data);
  166. return NULL;
  167. }
  168. if (!data->use_pac_binary_format &&
  169. eap_fast_load_pac(sm, &data->pac, config->pac_file) < 0) {
  170. eap_fast_deinit(sm, data);
  171. return NULL;
  172. }
  173. eap_fast_pac_list_truncate(data->pac, data->max_pac_list_len);
  174. if (data->pac == NULL && !data->provisioning_allowed) {
  175. wpa_printf(MSG_INFO, "EAP-FAST: No PAC configured and "
  176. "provisioning disabled");
  177. eap_fast_deinit(sm, data);
  178. return NULL;
  179. }
  180. return data;
  181. }
  182. static void eap_fast_deinit(struct eap_sm *sm, void *priv)
  183. {
  184. struct eap_fast_data *data = priv;
  185. struct eap_fast_pac *pac, *prev;
  186. if (data == NULL)
  187. return;
  188. if (data->phase2_priv && data->phase2_method)
  189. data->phase2_method->deinit(sm, data->phase2_priv);
  190. os_free(data->phase2_types);
  191. os_free(data->key_block_p);
  192. eap_peer_tls_ssl_deinit(sm, &data->ssl);
  193. pac = data->pac;
  194. prev = NULL;
  195. while (pac) {
  196. prev = pac;
  197. pac = pac->next;
  198. eap_fast_free_pac(prev);
  199. }
  200. wpabuf_free(data->pending_phase2_req);
  201. os_free(data);
  202. }
  203. static int eap_fast_derive_msk(struct eap_fast_data *data)
  204. {
  205. eap_fast_derive_eap_msk(data->simck, data->key_data);
  206. eap_fast_derive_eap_emsk(data->simck, data->emsk);
  207. data->success = 1;
  208. return 0;
  209. }
  210. static void eap_fast_derive_key_auth(struct eap_sm *sm,
  211. struct eap_fast_data *data)
  212. {
  213. u8 *sks;
  214. /* RFC 4851, Section 5.1:
  215. * Extra key material after TLS key_block: session_key_seed[40]
  216. */
  217. sks = eap_fast_derive_key(sm->ssl_ctx, data->ssl.conn, "key expansion",
  218. EAP_FAST_SKS_LEN);
  219. if (sks == NULL) {
  220. wpa_printf(MSG_DEBUG, "EAP-FAST: Failed to derive "
  221. "session_key_seed");
  222. return;
  223. }
  224. /*
  225. * RFC 4851, Section 5.2:
  226. * S-IMCK[0] = session_key_seed
  227. */
  228. wpa_hexdump_key(MSG_DEBUG,
  229. "EAP-FAST: session_key_seed (SKS = S-IMCK[0])",
  230. sks, EAP_FAST_SKS_LEN);
  231. data->simck_idx = 0;
  232. os_memcpy(data->simck, sks, EAP_FAST_SIMCK_LEN);
  233. os_free(sks);
  234. }
  235. static void eap_fast_derive_key_provisioning(struct eap_sm *sm,
  236. struct eap_fast_data *data)
  237. {
  238. os_free(data->key_block_p);
  239. data->key_block_p = (struct eap_fast_key_block_provisioning *)
  240. eap_fast_derive_key(sm->ssl_ctx, data->ssl.conn,
  241. "key expansion",
  242. sizeof(*data->key_block_p));
  243. if (data->key_block_p == NULL) {
  244. wpa_printf(MSG_DEBUG, "EAP-FAST: Failed to derive key block");
  245. return;
  246. }
  247. /*
  248. * RFC 4851, Section 5.2:
  249. * S-IMCK[0] = session_key_seed
  250. */
  251. wpa_hexdump_key(MSG_DEBUG,
  252. "EAP-FAST: session_key_seed (SKS = S-IMCK[0])",
  253. data->key_block_p->session_key_seed,
  254. sizeof(data->key_block_p->session_key_seed));
  255. data->simck_idx = 0;
  256. os_memcpy(data->simck, data->key_block_p->session_key_seed,
  257. EAP_FAST_SIMCK_LEN);
  258. wpa_hexdump_key(MSG_DEBUG, "EAP-FAST: server_challenge",
  259. data->key_block_p->server_challenge,
  260. sizeof(data->key_block_p->server_challenge));
  261. wpa_hexdump_key(MSG_DEBUG, "EAP-FAST: client_challenge",
  262. data->key_block_p->client_challenge,
  263. sizeof(data->key_block_p->client_challenge));
  264. }
  265. static void eap_fast_derive_keys(struct eap_sm *sm, struct eap_fast_data *data)
  266. {
  267. if (data->anon_provisioning)
  268. eap_fast_derive_key_provisioning(sm, data);
  269. else
  270. eap_fast_derive_key_auth(sm, data);
  271. }
  272. static int eap_fast_init_phase2_method(struct eap_sm *sm,
  273. struct eap_fast_data *data)
  274. {
  275. data->phase2_method =
  276. eap_peer_get_eap_method(data->phase2_type.vendor,
  277. data->phase2_type.method);
  278. if (data->phase2_method == NULL)
  279. return -1;
  280. if (data->key_block_p) {
  281. sm->auth_challenge = data->key_block_p->server_challenge;
  282. sm->peer_challenge = data->key_block_p->client_challenge;
  283. }
  284. sm->init_phase2 = 1;
  285. data->phase2_priv = data->phase2_method->init(sm);
  286. sm->init_phase2 = 0;
  287. sm->auth_challenge = NULL;
  288. sm->peer_challenge = NULL;
  289. return data->phase2_priv == NULL ? -1 : 0;
  290. }
  291. static int eap_fast_select_phase2_method(struct eap_fast_data *data, u8 type)
  292. {
  293. size_t i;
  294. /* TODO: TNC with anonymous provisioning; need to require both
  295. * completed MSCHAPv2 and TNC */
  296. if (data->anon_provisioning && type != EAP_TYPE_MSCHAPV2) {
  297. wpa_printf(MSG_INFO, "EAP-FAST: Only EAP-MSCHAPv2 is allowed "
  298. "during unauthenticated provisioning; reject phase2"
  299. " type %d", type);
  300. return -1;
  301. }
  302. #ifdef EAP_TNC
  303. if (type == EAP_TYPE_TNC) {
  304. data->phase2_type.vendor = EAP_VENDOR_IETF;
  305. data->phase2_type.method = EAP_TYPE_TNC;
  306. wpa_printf(MSG_DEBUG, "EAP-FAST: Selected Phase 2 EAP "
  307. "vendor %d method %d for TNC",
  308. data->phase2_type.vendor,
  309. data->phase2_type.method);
  310. return 0;
  311. }
  312. #endif /* EAP_TNC */
  313. for (i = 0; i < data->num_phase2_types; i++) {
  314. if (data->phase2_types[i].vendor != EAP_VENDOR_IETF ||
  315. data->phase2_types[i].method != type)
  316. continue;
  317. data->phase2_type.vendor = data->phase2_types[i].vendor;
  318. data->phase2_type.method = data->phase2_types[i].method;
  319. wpa_printf(MSG_DEBUG, "EAP-FAST: Selected Phase 2 EAP "
  320. "vendor %d method %d",
  321. data->phase2_type.vendor,
  322. data->phase2_type.method);
  323. break;
  324. }
  325. if (type != data->phase2_type.method || type == EAP_TYPE_NONE)
  326. return -1;
  327. return 0;
  328. }
  329. static int eap_fast_phase2_request(struct eap_sm *sm,
  330. struct eap_fast_data *data,
  331. struct eap_method_ret *ret,
  332. struct eap_hdr *hdr,
  333. struct wpabuf **resp)
  334. {
  335. size_t len = be_to_host16(hdr->length);
  336. u8 *pos;
  337. struct eap_method_ret iret;
  338. struct eap_peer_config *config = eap_get_config(sm);
  339. struct wpabuf msg;
  340. if (len <= sizeof(struct eap_hdr)) {
  341. wpa_printf(MSG_INFO, "EAP-FAST: too short "
  342. "Phase 2 request (len=%lu)", (unsigned long) len);
  343. return -1;
  344. }
  345. pos = (u8 *) (hdr + 1);
  346. wpa_printf(MSG_DEBUG, "EAP-FAST: Phase 2 Request: type=%d", *pos);
  347. if (*pos == EAP_TYPE_IDENTITY) {
  348. *resp = eap_sm_buildIdentity(sm, hdr->identifier, 1);
  349. return 0;
  350. }
  351. if (data->phase2_priv && data->phase2_method &&
  352. *pos != data->phase2_type.method) {
  353. wpa_printf(MSG_DEBUG, "EAP-FAST: Phase 2 EAP sequence - "
  354. "deinitialize previous method");
  355. data->phase2_method->deinit(sm, data->phase2_priv);
  356. data->phase2_method = NULL;
  357. data->phase2_priv = NULL;
  358. data->phase2_type.vendor = EAP_VENDOR_IETF;
  359. data->phase2_type.method = EAP_TYPE_NONE;
  360. }
  361. if (data->phase2_type.vendor == EAP_VENDOR_IETF &&
  362. data->phase2_type.method == EAP_TYPE_NONE &&
  363. eap_fast_select_phase2_method(data, *pos) < 0) {
  364. if (eap_peer_tls_phase2_nak(data->phase2_types,
  365. data->num_phase2_types,
  366. hdr, resp))
  367. return -1;
  368. return 0;
  369. }
  370. if (data->phase2_priv == NULL &&
  371. eap_fast_init_phase2_method(sm, data) < 0) {
  372. wpa_printf(MSG_INFO, "EAP-FAST: Failed to initialize "
  373. "Phase 2 EAP method %d", *pos);
  374. ret->methodState = METHOD_DONE;
  375. ret->decision = DECISION_FAIL;
  376. return -1;
  377. }
  378. os_memset(&iret, 0, sizeof(iret));
  379. wpabuf_set(&msg, hdr, len);
  380. *resp = data->phase2_method->process(sm, data->phase2_priv, &iret,
  381. &msg);
  382. if (*resp == NULL ||
  383. (iret.methodState == METHOD_DONE &&
  384. iret.decision == DECISION_FAIL)) {
  385. ret->methodState = METHOD_DONE;
  386. ret->decision = DECISION_FAIL;
  387. } else if ((iret.methodState == METHOD_DONE ||
  388. iret.methodState == METHOD_MAY_CONT) &&
  389. (iret.decision == DECISION_UNCOND_SUCC ||
  390. iret.decision == DECISION_COND_SUCC)) {
  391. data->phase2_success = 1;
  392. }
  393. if (*resp == NULL && config &&
  394. (config->pending_req_identity || config->pending_req_password ||
  395. config->pending_req_otp || config->pending_req_new_password)) {
  396. wpabuf_free(data->pending_phase2_req);
  397. data->pending_phase2_req = wpabuf_alloc_copy(hdr, len);
  398. } else if (*resp == NULL)
  399. return -1;
  400. return 0;
  401. }
  402. static struct wpabuf * eap_fast_tlv_nak(int vendor_id, int tlv_type)
  403. {
  404. struct wpabuf *buf;
  405. struct eap_tlv_nak_tlv *nak;
  406. buf = wpabuf_alloc(sizeof(*nak));
  407. if (buf == NULL)
  408. return NULL;
  409. nak = wpabuf_put(buf, sizeof(*nak));
  410. nak->tlv_type = host_to_be16(EAP_TLV_TYPE_MANDATORY | EAP_TLV_NAK_TLV);
  411. nak->length = host_to_be16(6);
  412. nak->vendor_id = host_to_be32(vendor_id);
  413. nak->nak_type = host_to_be16(tlv_type);
  414. return buf;
  415. }
  416. static struct wpabuf * eap_fast_tlv_result(int status, int intermediate)
  417. {
  418. struct wpabuf *buf;
  419. struct eap_tlv_intermediate_result_tlv *result;
  420. buf = wpabuf_alloc(sizeof(*result));
  421. if (buf == NULL)
  422. return NULL;
  423. wpa_printf(MSG_DEBUG, "EAP-FAST: Add %sResult TLV(status=%d)",
  424. intermediate ? "Intermediate " : "", status);
  425. result = wpabuf_put(buf, sizeof(*result));
  426. result->tlv_type = host_to_be16(EAP_TLV_TYPE_MANDATORY |
  427. (intermediate ?
  428. EAP_TLV_INTERMEDIATE_RESULT_TLV :
  429. EAP_TLV_RESULT_TLV));
  430. result->length = host_to_be16(2);
  431. result->status = host_to_be16(status);
  432. return buf;
  433. }
  434. static struct wpabuf * eap_fast_tlv_pac_ack(void)
  435. {
  436. struct wpabuf *buf;
  437. struct eap_tlv_result_tlv *res;
  438. struct eap_tlv_pac_ack_tlv *ack;
  439. buf = wpabuf_alloc(sizeof(*res) + sizeof(*ack));
  440. if (buf == NULL)
  441. return NULL;
  442. wpa_printf(MSG_DEBUG, "EAP-FAST: Add PAC TLV (ack)");
  443. ack = wpabuf_put(buf, sizeof(*ack));
  444. ack->tlv_type = host_to_be16(EAP_TLV_PAC_TLV |
  445. EAP_TLV_TYPE_MANDATORY);
  446. ack->length = host_to_be16(sizeof(*ack) - sizeof(struct eap_tlv_hdr));
  447. ack->pac_type = host_to_be16(PAC_TYPE_PAC_ACKNOWLEDGEMENT);
  448. ack->pac_len = host_to_be16(2);
  449. ack->result = host_to_be16(EAP_TLV_RESULT_SUCCESS);
  450. return buf;
  451. }
  452. static struct wpabuf * eap_fast_process_eap_payload_tlv(
  453. struct eap_sm *sm, struct eap_fast_data *data,
  454. struct eap_method_ret *ret, const struct eap_hdr *req,
  455. u8 *eap_payload_tlv, size_t eap_payload_tlv_len)
  456. {
  457. struct eap_hdr *hdr;
  458. struct wpabuf *resp = NULL;
  459. if (eap_payload_tlv_len < sizeof(*hdr)) {
  460. wpa_printf(MSG_DEBUG, "EAP-FAST: too short EAP "
  461. "Payload TLV (len=%lu)",
  462. (unsigned long) eap_payload_tlv_len);
  463. return NULL;
  464. }
  465. hdr = (struct eap_hdr *) eap_payload_tlv;
  466. if (be_to_host16(hdr->length) > eap_payload_tlv_len) {
  467. wpa_printf(MSG_DEBUG, "EAP-FAST: EAP packet overflow in "
  468. "EAP Payload TLV");
  469. return NULL;
  470. }
  471. if (hdr->code != EAP_CODE_REQUEST) {
  472. wpa_printf(MSG_INFO, "EAP-FAST: Unexpected code=%d in "
  473. "Phase 2 EAP header", hdr->code);
  474. return NULL;
  475. }
  476. if (eap_fast_phase2_request(sm, data, ret, hdr, &resp)) {
  477. wpa_printf(MSG_INFO, "EAP-FAST: Phase2 Request processing "
  478. "failed");
  479. return NULL;
  480. }
  481. return eap_fast_tlv_eap_payload(resp);
  482. }
  483. static int eap_fast_validate_crypto_binding(
  484. struct eap_tlv_crypto_binding_tlv *_bind)
  485. {
  486. wpa_printf(MSG_DEBUG, "EAP-FAST: Crypto-Binding TLV: Version %d "
  487. "Received Version %d SubType %d",
  488. _bind->version, _bind->received_version, _bind->subtype);
  489. wpa_hexdump(MSG_MSGDUMP, "EAP-FAST: NONCE",
  490. _bind->nonce, sizeof(_bind->nonce));
  491. wpa_hexdump(MSG_MSGDUMP, "EAP-FAST: Compound MAC",
  492. _bind->compound_mac, sizeof(_bind->compound_mac));
  493. if (_bind->version != EAP_FAST_VERSION ||
  494. _bind->received_version != EAP_FAST_VERSION ||
  495. _bind->subtype != EAP_TLV_CRYPTO_BINDING_SUBTYPE_REQUEST) {
  496. wpa_printf(MSG_INFO, "EAP-FAST: Invalid version/subtype in "
  497. "Crypto-Binding TLV: Version %d "
  498. "Received Version %d SubType %d",
  499. _bind->version, _bind->received_version,
  500. _bind->subtype);
  501. return -1;
  502. }
  503. return 0;
  504. }
  505. static void eap_fast_write_crypto_binding(
  506. struct eap_tlv_crypto_binding_tlv *rbind,
  507. struct eap_tlv_crypto_binding_tlv *_bind, const u8 *cmk)
  508. {
  509. rbind->tlv_type = host_to_be16(EAP_TLV_TYPE_MANDATORY |
  510. EAP_TLV_CRYPTO_BINDING_TLV);
  511. rbind->length = host_to_be16(sizeof(*rbind) -
  512. sizeof(struct eap_tlv_hdr));
  513. rbind->version = EAP_FAST_VERSION;
  514. rbind->received_version = _bind->version;
  515. rbind->subtype = EAP_TLV_CRYPTO_BINDING_SUBTYPE_RESPONSE;
  516. os_memcpy(rbind->nonce, _bind->nonce, sizeof(_bind->nonce));
  517. inc_byte_array(rbind->nonce, sizeof(rbind->nonce));
  518. hmac_sha1(cmk, EAP_FAST_CMK_LEN, (u8 *) rbind, sizeof(*rbind),
  519. rbind->compound_mac);
  520. wpa_printf(MSG_DEBUG, "EAP-FAST: Reply Crypto-Binding TLV: Version %d "
  521. "Received Version %d SubType %d",
  522. rbind->version, rbind->received_version, rbind->subtype);
  523. wpa_hexdump(MSG_MSGDUMP, "EAP-FAST: NONCE",
  524. rbind->nonce, sizeof(rbind->nonce));
  525. wpa_hexdump(MSG_MSGDUMP, "EAP-FAST: Compound MAC",
  526. rbind->compound_mac, sizeof(rbind->compound_mac));
  527. }
  528. static int eap_fast_get_phase2_key(struct eap_sm *sm,
  529. struct eap_fast_data *data,
  530. u8 *isk, size_t isk_len)
  531. {
  532. u8 *key;
  533. size_t key_len;
  534. os_memset(isk, 0, isk_len);
  535. if (data->phase2_method == NULL || data->phase2_priv == NULL) {
  536. wpa_printf(MSG_DEBUG, "EAP-FAST: Phase 2 method not "
  537. "available");
  538. return -1;
  539. }
  540. if (data->phase2_method->isKeyAvailable == NULL ||
  541. data->phase2_method->getKey == NULL)
  542. return 0;
  543. if (!data->phase2_method->isKeyAvailable(sm, data->phase2_priv) ||
  544. (key = data->phase2_method->getKey(sm, data->phase2_priv,
  545. &key_len)) == NULL) {
  546. wpa_printf(MSG_DEBUG, "EAP-FAST: Could not get key material "
  547. "from Phase 2");
  548. return -1;
  549. }
  550. if (key_len > isk_len)
  551. key_len = isk_len;
  552. if (key_len == 32 &&
  553. data->phase2_method->vendor == EAP_VENDOR_IETF &&
  554. data->phase2_method->method == EAP_TYPE_MSCHAPV2) {
  555. /*
  556. * EAP-FAST uses reverse order for MS-MPPE keys when deriving
  557. * MSK from EAP-MSCHAPv2. Swap the keys here to get the correct
  558. * ISK for EAP-FAST cryptobinding.
  559. */
  560. os_memcpy(isk, key + 16, 16);
  561. os_memcpy(isk + 16, key, 16);
  562. } else
  563. os_memcpy(isk, key, key_len);
  564. os_free(key);
  565. return 0;
  566. }
  567. static int eap_fast_get_cmk(struct eap_sm *sm, struct eap_fast_data *data,
  568. u8 *cmk)
  569. {
  570. u8 isk[32], imck[60];
  571. wpa_printf(MSG_DEBUG, "EAP-FAST: Determining CMK[%d] for Compound MIC "
  572. "calculation", data->simck_idx + 1);
  573. /*
  574. * RFC 4851, Section 5.2:
  575. * IMCK[j] = T-PRF(S-IMCK[j-1], "Inner Methods Compound Keys",
  576. * MSK[j], 60)
  577. * S-IMCK[j] = first 40 octets of IMCK[j]
  578. * CMK[j] = last 20 octets of IMCK[j]
  579. */
  580. if (eap_fast_get_phase2_key(sm, data, isk, sizeof(isk)) < 0)
  581. return -1;
  582. wpa_hexdump_key(MSG_MSGDUMP, "EAP-FAST: ISK[j]", isk, sizeof(isk));
  583. sha1_t_prf(data->simck, EAP_FAST_SIMCK_LEN,
  584. "Inner Methods Compound Keys",
  585. isk, sizeof(isk), imck, sizeof(imck));
  586. data->simck_idx++;
  587. os_memcpy(data->simck, imck, EAP_FAST_SIMCK_LEN);
  588. wpa_hexdump_key(MSG_MSGDUMP, "EAP-FAST: S-IMCK[j]",
  589. data->simck, EAP_FAST_SIMCK_LEN);
  590. os_memcpy(cmk, imck + EAP_FAST_SIMCK_LEN, EAP_FAST_CMK_LEN);
  591. wpa_hexdump_key(MSG_MSGDUMP, "EAP-FAST: CMK[j]",
  592. cmk, EAP_FAST_CMK_LEN);
  593. return 0;
  594. }
  595. static u8 * eap_fast_write_pac_request(u8 *pos, u16 pac_type)
  596. {
  597. struct eap_tlv_hdr *pac;
  598. struct eap_tlv_request_action_tlv *act;
  599. struct eap_tlv_pac_type_tlv *type;
  600. act = (struct eap_tlv_request_action_tlv *) pos;
  601. act->tlv_type = host_to_be16(EAP_TLV_REQUEST_ACTION_TLV);
  602. act->length = host_to_be16(2);
  603. act->action = host_to_be16(EAP_TLV_ACTION_PROCESS_TLV);
  604. pac = (struct eap_tlv_hdr *) (act + 1);
  605. pac->tlv_type = host_to_be16(EAP_TLV_PAC_TLV);
  606. pac->length = host_to_be16(sizeof(*type));
  607. type = (struct eap_tlv_pac_type_tlv *) (pac + 1);
  608. type->tlv_type = host_to_be16(PAC_TYPE_PAC_TYPE);
  609. type->length = host_to_be16(2);
  610. type->pac_type = host_to_be16(pac_type);
  611. return (u8 *) (type + 1);
  612. }
  613. static struct wpabuf * eap_fast_process_crypto_binding(
  614. struct eap_sm *sm, struct eap_fast_data *data,
  615. struct eap_method_ret *ret,
  616. struct eap_tlv_crypto_binding_tlv *_bind, size_t bind_len)
  617. {
  618. struct wpabuf *resp;
  619. u8 *pos;
  620. u8 cmk[EAP_FAST_CMK_LEN], cmac[SHA1_MAC_LEN];
  621. int res;
  622. size_t len;
  623. if (eap_fast_validate_crypto_binding(_bind) < 0)
  624. return NULL;
  625. if (eap_fast_get_cmk(sm, data, cmk) < 0)
  626. return NULL;
  627. /* Validate received Compound MAC */
  628. os_memcpy(cmac, _bind->compound_mac, sizeof(cmac));
  629. os_memset(_bind->compound_mac, 0, sizeof(cmac));
  630. wpa_hexdump(MSG_MSGDUMP, "EAP-FAST: Crypto-Binding TLV for Compound "
  631. "MAC calculation", (u8 *) _bind, bind_len);
  632. hmac_sha1(cmk, EAP_FAST_CMK_LEN, (u8 *) _bind, bind_len,
  633. _bind->compound_mac);
  634. res = os_memcmp(cmac, _bind->compound_mac, sizeof(cmac));
  635. wpa_hexdump(MSG_MSGDUMP, "EAP-FAST: Received Compound MAC",
  636. cmac, sizeof(cmac));
  637. wpa_hexdump(MSG_MSGDUMP, "EAP-FAST: Calculated Compound MAC",
  638. _bind->compound_mac, sizeof(cmac));
  639. if (res != 0) {
  640. wpa_printf(MSG_INFO, "EAP-FAST: Compound MAC did not match");
  641. os_memcpy(_bind->compound_mac, cmac, sizeof(cmac));
  642. return NULL;
  643. }
  644. /*
  645. * Compound MAC was valid, so authentication succeeded. Reply with
  646. * crypto binding to allow server to complete authentication.
  647. */
  648. len = sizeof(struct eap_tlv_crypto_binding_tlv);
  649. resp = wpabuf_alloc(len);
  650. if (resp == NULL)
  651. return NULL;
  652. if (!data->anon_provisioning && data->phase2_success &&
  653. eap_fast_derive_msk(data) < 0) {
  654. wpa_printf(MSG_INFO, "EAP-FAST: Failed to generate MSK");
  655. ret->methodState = METHOD_DONE;
  656. ret->decision = DECISION_FAIL;
  657. data->phase2_success = 0;
  658. wpabuf_free(resp);
  659. return NULL;
  660. }
  661. pos = wpabuf_put(resp, sizeof(struct eap_tlv_crypto_binding_tlv));
  662. eap_fast_write_crypto_binding((struct eap_tlv_crypto_binding_tlv *)
  663. pos, _bind, cmk);
  664. return resp;
  665. }
  666. static void eap_fast_parse_pac_tlv(struct eap_fast_pac *entry, int type,
  667. u8 *pos, size_t len, int *pac_key_found)
  668. {
  669. switch (type & 0x7fff) {
  670. case PAC_TYPE_PAC_KEY:
  671. wpa_hexdump_key(MSG_DEBUG, "EAP-FAST: PAC-Key", pos, len);
  672. if (len != EAP_FAST_PAC_KEY_LEN) {
  673. wpa_printf(MSG_DEBUG, "EAP-FAST: Invalid PAC-Key "
  674. "length %lu", (unsigned long) len);
  675. break;
  676. }
  677. *pac_key_found = 1;
  678. os_memcpy(entry->pac_key, pos, len);
  679. break;
  680. case PAC_TYPE_PAC_OPAQUE:
  681. wpa_hexdump(MSG_DEBUG, "EAP-FAST: PAC-Opaque", pos, len);
  682. entry->pac_opaque = pos;
  683. entry->pac_opaque_len = len;
  684. break;
  685. case PAC_TYPE_PAC_INFO:
  686. wpa_hexdump(MSG_DEBUG, "EAP-FAST: PAC-Info", pos, len);
  687. entry->pac_info = pos;
  688. entry->pac_info_len = len;
  689. break;
  690. default:
  691. wpa_printf(MSG_DEBUG, "EAP-FAST: Ignored unknown PAC type %d",
  692. type);
  693. break;
  694. }
  695. }
  696. static int eap_fast_process_pac_tlv(struct eap_fast_pac *entry,
  697. u8 *pac, size_t pac_len)
  698. {
  699. struct pac_tlv_hdr *hdr;
  700. u8 *pos;
  701. size_t left, len;
  702. int type, pac_key_found = 0;
  703. pos = pac;
  704. left = pac_len;
  705. while (left > sizeof(*hdr)) {
  706. hdr = (struct pac_tlv_hdr *) pos;
  707. type = be_to_host16(hdr->type);
  708. len = be_to_host16(hdr->len);
  709. pos += sizeof(*hdr);
  710. left -= sizeof(*hdr);
  711. if (len > left) {
  712. wpa_printf(MSG_DEBUG, "EAP-FAST: PAC TLV overrun "
  713. "(type=%d len=%lu left=%lu)",
  714. type, (unsigned long) len,
  715. (unsigned long) left);
  716. return -1;
  717. }
  718. eap_fast_parse_pac_tlv(entry, type, pos, len, &pac_key_found);
  719. pos += len;
  720. left -= len;
  721. }
  722. if (!pac_key_found || !entry->pac_opaque || !entry->pac_info) {
  723. wpa_printf(MSG_DEBUG, "EAP-FAST: PAC TLV does not include "
  724. "all the required fields");
  725. return -1;
  726. }
  727. return 0;
  728. }
  729. static int eap_fast_parse_pac_info(struct eap_fast_pac *entry, int type,
  730. u8 *pos, size_t len)
  731. {
  732. u16 pac_type;
  733. u32 lifetime;
  734. struct os_time now;
  735. switch (type & 0x7fff) {
  736. case PAC_TYPE_CRED_LIFETIME:
  737. if (len != 4) {
  738. wpa_hexdump(MSG_DEBUG, "EAP-FAST: PAC-Info - "
  739. "Invalid CRED_LIFETIME length - ignored",
  740. pos, len);
  741. return 0;
  742. }
  743. /*
  744. * This is not currently saved separately in PAC files since
  745. * the server can automatically initiate PAC update when
  746. * needed. Anyway, the information is available from PAC-Info
  747. * dump if it is needed for something in the future.
  748. */
  749. lifetime = WPA_GET_BE32(pos);
  750. os_get_time(&now);
  751. wpa_printf(MSG_DEBUG, "EAP-FAST: PAC-Info - CRED_LIFETIME %d "
  752. "(%d days)",
  753. lifetime, (lifetime - (u32) now.sec) / 86400);
  754. break;
  755. case PAC_TYPE_A_ID:
  756. wpa_hexdump_ascii(MSG_DEBUG, "EAP-FAST: PAC-Info - A-ID",
  757. pos, len);
  758. entry->a_id = pos;
  759. entry->a_id_len = len;
  760. break;
  761. case PAC_TYPE_I_ID:
  762. wpa_hexdump_ascii(MSG_DEBUG, "EAP-FAST: PAC-Info - I-ID",
  763. pos, len);
  764. entry->i_id = pos;
  765. entry->i_id_len = len;
  766. break;
  767. case PAC_TYPE_A_ID_INFO:
  768. wpa_hexdump_ascii(MSG_DEBUG, "EAP-FAST: PAC-Info - A-ID-Info",
  769. pos, len);
  770. entry->a_id_info = pos;
  771. entry->a_id_info_len = len;
  772. break;
  773. case PAC_TYPE_PAC_TYPE:
  774. /*
  775. * draft-cam-winget-eap-fast-provisioning-04.txt,
  776. * Section 4.2.6 - PAC-Type TLV
  777. */
  778. if (len != 2) {
  779. wpa_printf(MSG_INFO, "EAP-FAST: Invalid PAC-Type "
  780. "length %lu (expected 2)",
  781. (unsigned long) len);
  782. wpa_hexdump_ascii(MSG_DEBUG,
  783. "EAP-FAST: PAC-Info - PAC-Type",
  784. pos, len);
  785. return -1;
  786. }
  787. pac_type = WPA_GET_BE16(pos);
  788. if (pac_type != PAC_TYPE_TUNNEL_PAC &&
  789. pac_type != PAC_TYPE_USER_AUTHORIZATION &&
  790. pac_type != PAC_TYPE_MACHINE_AUTHENTICATION) {
  791. wpa_printf(MSG_INFO, "EAP-FAST: Unsupported PAC Type "
  792. "%d", pac_type);
  793. return -1;
  794. }
  795. wpa_printf(MSG_DEBUG, "EAP-FAST: PAC-Info - PAC-Type %d",
  796. pac_type);
  797. entry->pac_type = pac_type;
  798. break;
  799. default:
  800. wpa_printf(MSG_DEBUG, "EAP-FAST: Ignored unknown PAC-Info "
  801. "type %d", type);
  802. break;
  803. }
  804. return 0;
  805. }
  806. static int eap_fast_process_pac_info(struct eap_fast_pac *entry)
  807. {
  808. struct pac_tlv_hdr *hdr;
  809. u8 *pos;
  810. size_t left, len;
  811. int type;
  812. /* draft-cam-winget-eap-fast-provisioning-04.txt, Section 4.2.4 */
  813. /* PAC-Type defaults to Tunnel PAC (Type 1) */
  814. entry->pac_type = PAC_TYPE_TUNNEL_PAC;
  815. pos = entry->pac_info;
  816. left = entry->pac_info_len;
  817. while (left > sizeof(*hdr)) {
  818. hdr = (struct pac_tlv_hdr *) pos;
  819. type = be_to_host16(hdr->type);
  820. len = be_to_host16(hdr->len);
  821. pos += sizeof(*hdr);
  822. left -= sizeof(*hdr);
  823. if (len > left) {
  824. wpa_printf(MSG_DEBUG, "EAP-FAST: PAC-Info overrun "
  825. "(type=%d len=%lu left=%lu)",
  826. type, (unsigned long) len,
  827. (unsigned long) left);
  828. return -1;
  829. }
  830. if (eap_fast_parse_pac_info(entry, type, pos, len) < 0)
  831. return -1;
  832. pos += len;
  833. left -= len;
  834. }
  835. if (entry->a_id == NULL || entry->a_id_info == NULL) {
  836. wpa_printf(MSG_DEBUG, "EAP-FAST: PAC-Info does not include "
  837. "all the required fields");
  838. return -1;
  839. }
  840. return 0;
  841. }
  842. static struct wpabuf * eap_fast_process_pac(struct eap_sm *sm,
  843. struct eap_fast_data *data,
  844. struct eap_method_ret *ret,
  845. u8 *pac, size_t pac_len)
  846. {
  847. struct eap_peer_config *config = eap_get_config(sm);
  848. struct eap_fast_pac entry;
  849. os_memset(&entry, 0, sizeof(entry));
  850. if (eap_fast_process_pac_tlv(&entry, pac, pac_len) ||
  851. eap_fast_process_pac_info(&entry))
  852. return NULL;
  853. eap_fast_add_pac(&data->pac, &data->current_pac, &entry);
  854. eap_fast_pac_list_truncate(data->pac, data->max_pac_list_len);
  855. if (data->use_pac_binary_format)
  856. eap_fast_save_pac_bin(sm, data->pac, config->pac_file);
  857. else
  858. eap_fast_save_pac(sm, data->pac, config->pac_file);
  859. if (data->provisioning) {
  860. if (data->anon_provisioning) {
  861. /*
  862. * Unauthenticated provisioning does not provide keying
  863. * material and must end with an EAP-Failure.
  864. * Authentication will be done separately after this.
  865. */
  866. data->success = 0;
  867. ret->decision = DECISION_FAIL;
  868. } else {
  869. /*
  870. * Server may or may not allow authenticated
  871. * provisioning also for key generation.
  872. */
  873. ret->decision = DECISION_COND_SUCC;
  874. }
  875. wpa_printf(MSG_DEBUG, "EAP-FAST: Send PAC-Acknowledgement TLV "
  876. "- Provisioning completed successfully");
  877. } else {
  878. /*
  879. * This is PAC refreshing, i.e., normal authentication that is
  880. * expected to be completed with an EAP-Success.
  881. */
  882. wpa_printf(MSG_DEBUG, "EAP-FAST: Send PAC-Acknowledgement TLV "
  883. "- PAC refreshing completed successfully");
  884. ret->decision = DECISION_UNCOND_SUCC;
  885. }
  886. ret->methodState = METHOD_DONE;
  887. return eap_fast_tlv_pac_ack();
  888. }
  889. static int eap_fast_parse_decrypted(struct wpabuf *decrypted,
  890. struct eap_fast_tlv_parse *tlv,
  891. struct wpabuf **resp)
  892. {
  893. int mandatory, tlv_type, len, res;
  894. u8 *pos, *end;
  895. os_memset(tlv, 0, sizeof(*tlv));
  896. /* Parse TLVs from the decrypted Phase 2 data */
  897. pos = wpabuf_mhead(decrypted);
  898. end = pos + wpabuf_len(decrypted);
  899. while (pos + 4 < end) {
  900. mandatory = pos[0] & 0x80;
  901. tlv_type = WPA_GET_BE16(pos) & 0x3fff;
  902. pos += 2;
  903. len = WPA_GET_BE16(pos);
  904. pos += 2;
  905. if (pos + len > end) {
  906. wpa_printf(MSG_INFO, "EAP-FAST: TLV overflow");
  907. return -1;
  908. }
  909. wpa_printf(MSG_DEBUG, "EAP-FAST: Received Phase 2: "
  910. "TLV type %d length %d%s",
  911. tlv_type, len, mandatory ? " (mandatory)" : "");
  912. res = eap_fast_parse_tlv(tlv, tlv_type, pos, len);
  913. if (res == -2)
  914. break;
  915. if (res < 0) {
  916. if (mandatory) {
  917. wpa_printf(MSG_DEBUG, "EAP-FAST: Nak unknown "
  918. "mandatory TLV type %d", tlv_type);
  919. *resp = eap_fast_tlv_nak(0, tlv_type);
  920. break;
  921. } else {
  922. wpa_printf(MSG_DEBUG, "EAP-FAST: ignored "
  923. "unknown optional TLV type %d",
  924. tlv_type);
  925. }
  926. }
  927. pos += len;
  928. }
  929. return 0;
  930. }
  931. static int eap_fast_encrypt_response(struct eap_sm *sm,
  932. struct eap_fast_data *data,
  933. struct wpabuf *resp,
  934. u8 identifier, struct wpabuf **out_data)
  935. {
  936. if (resp == NULL)
  937. return 0;
  938. wpa_hexdump_buf(MSG_DEBUG, "EAP-FAST: Encrypting Phase 2 data",
  939. resp);
  940. if (eap_peer_tls_encrypt(sm, &data->ssl, EAP_TYPE_FAST,
  941. data->fast_version, identifier,
  942. resp, out_data)) {
  943. wpa_printf(MSG_INFO, "EAP-FAST: Failed to encrypt a Phase 2 "
  944. "frame");
  945. }
  946. wpabuf_free(resp);
  947. return 0;
  948. }
  949. static struct wpabuf * eap_fast_pac_request(void)
  950. {
  951. struct wpabuf *tmp;
  952. u8 *pos, *pos2;
  953. tmp = wpabuf_alloc(sizeof(struct eap_tlv_hdr) +
  954. sizeof(struct eap_tlv_request_action_tlv) +
  955. sizeof(struct eap_tlv_pac_type_tlv));
  956. if (tmp == NULL)
  957. return NULL;
  958. pos = wpabuf_put(tmp, 0);
  959. pos2 = eap_fast_write_pac_request(pos, PAC_TYPE_TUNNEL_PAC);
  960. wpabuf_put(tmp, pos2 - pos);
  961. return tmp;
  962. }
  963. static int eap_fast_process_decrypted(struct eap_sm *sm,
  964. struct eap_fast_data *data,
  965. struct eap_method_ret *ret,
  966. const struct eap_hdr *req,
  967. struct wpabuf *decrypted,
  968. struct wpabuf **out_data)
  969. {
  970. struct wpabuf *resp = NULL, *tmp;
  971. struct eap_fast_tlv_parse tlv;
  972. int failed = 0;
  973. if (eap_fast_parse_decrypted(decrypted, &tlv, &resp) < 0)
  974. return 0;
  975. if (resp)
  976. return eap_fast_encrypt_response(sm, data, resp,
  977. req->identifier, out_data);
  978. if (tlv.result == EAP_TLV_RESULT_FAILURE) {
  979. resp = eap_fast_tlv_result(EAP_TLV_RESULT_FAILURE, 0);
  980. return eap_fast_encrypt_response(sm, data, resp,
  981. req->identifier, out_data);
  982. }
  983. if (tlv.iresult == EAP_TLV_RESULT_FAILURE) {
  984. resp = eap_fast_tlv_result(EAP_TLV_RESULT_FAILURE, 1);
  985. return eap_fast_encrypt_response(sm, data, resp,
  986. req->identifier, out_data);
  987. }
  988. if (tlv.crypto_binding) {
  989. tmp = eap_fast_process_crypto_binding(sm, data, ret,
  990. tlv.crypto_binding,
  991. tlv.crypto_binding_len);
  992. if (tmp == NULL)
  993. failed = 1;
  994. else
  995. resp = wpabuf_concat(resp, tmp);
  996. }
  997. if (tlv.iresult == EAP_TLV_RESULT_SUCCESS) {
  998. tmp = eap_fast_tlv_result(failed ? EAP_TLV_RESULT_FAILURE :
  999. EAP_TLV_RESULT_SUCCESS, 1);
  1000. resp = wpabuf_concat(resp, tmp);
  1001. }
  1002. if (tlv.eap_payload_tlv) {
  1003. tmp = eap_fast_process_eap_payload_tlv(
  1004. sm, data, ret, req, tlv.eap_payload_tlv,
  1005. tlv.eap_payload_tlv_len);
  1006. resp = wpabuf_concat(resp, tmp);
  1007. }
  1008. if (tlv.pac && tlv.result != EAP_TLV_RESULT_SUCCESS) {
  1009. wpa_printf(MSG_DEBUG, "EAP-FAST: PAC TLV without Result TLV "
  1010. "acknowledging success");
  1011. failed = 1;
  1012. } else if (tlv.pac && tlv.result == EAP_TLV_RESULT_SUCCESS) {
  1013. tmp = eap_fast_process_pac(sm, data, ret, tlv.pac,
  1014. tlv.pac_len);
  1015. resp = wpabuf_concat(resp, tmp);
  1016. }
  1017. if (data->current_pac == NULL && data->provisioning &&
  1018. !data->anon_provisioning && !tlv.pac &&
  1019. (tlv.iresult == EAP_TLV_RESULT_SUCCESS ||
  1020. tlv.result == EAP_TLV_RESULT_SUCCESS)) {
  1021. /*
  1022. * Need to request Tunnel PAC when using authenticated
  1023. * provisioning.
  1024. */
  1025. wpa_printf(MSG_DEBUG, "EAP-FAST: Request Tunnel PAC");
  1026. tmp = eap_fast_pac_request();
  1027. resp = wpabuf_concat(resp, tmp);
  1028. }
  1029. if (tlv.result == EAP_TLV_RESULT_SUCCESS && !failed) {
  1030. tmp = eap_fast_tlv_result(EAP_TLV_RESULT_SUCCESS, 0);
  1031. resp = wpabuf_concat(tmp, resp);
  1032. } else if (failed) {
  1033. tmp = eap_fast_tlv_result(EAP_TLV_RESULT_FAILURE, 0);
  1034. resp = wpabuf_concat(tmp, resp);
  1035. }
  1036. if (resp && tlv.result == EAP_TLV_RESULT_SUCCESS && !failed &&
  1037. tlv.crypto_binding && data->phase2_success) {
  1038. if (data->anon_provisioning) {
  1039. wpa_printf(MSG_DEBUG, "EAP-FAST: Unauthenticated "
  1040. "provisioning completed successfully.");
  1041. ret->methodState = METHOD_DONE;
  1042. ret->decision = DECISION_FAIL;
  1043. } else {
  1044. wpa_printf(MSG_DEBUG, "EAP-FAST: Authentication "
  1045. "completed successfully.");
  1046. if (data->provisioning)
  1047. ret->methodState = METHOD_MAY_CONT;
  1048. else
  1049. ret->methodState = METHOD_DONE;
  1050. ret->decision = DECISION_UNCOND_SUCC;
  1051. }
  1052. }
  1053. if (resp == NULL) {
  1054. wpa_printf(MSG_DEBUG, "EAP-FAST: No recognized TLVs - send "
  1055. "empty response packet");
  1056. resp = wpabuf_alloc(1);
  1057. }
  1058. return eap_fast_encrypt_response(sm, data, resp, req->identifier,
  1059. out_data);
  1060. }
  1061. static int eap_fast_decrypt(struct eap_sm *sm, struct eap_fast_data *data,
  1062. struct eap_method_ret *ret,
  1063. const struct eap_hdr *req,
  1064. const struct wpabuf *in_data,
  1065. struct wpabuf **out_data)
  1066. {
  1067. struct wpabuf *in_decrypted;
  1068. int res;
  1069. wpa_printf(MSG_DEBUG, "EAP-FAST: Received %lu bytes encrypted data for"
  1070. " Phase 2", (unsigned long) wpabuf_len(in_data));
  1071. if (data->pending_phase2_req) {
  1072. wpa_printf(MSG_DEBUG, "EAP-FAST: Pending Phase 2 request - "
  1073. "skip decryption and use old data");
  1074. /* Clear TLS reassembly state. */
  1075. eap_peer_tls_reset_input(&data->ssl);
  1076. in_decrypted = data->pending_phase2_req;
  1077. data->pending_phase2_req = NULL;
  1078. goto continue_req;
  1079. }
  1080. if (wpabuf_len(in_data) == 0) {
  1081. /* Received TLS ACK - requesting more fragments */
  1082. return eap_peer_tls_encrypt(sm, &data->ssl, EAP_TYPE_FAST,
  1083. data->fast_version,
  1084. req->identifier, NULL, out_data);
  1085. }
  1086. res = eap_peer_tls_decrypt(sm, &data->ssl, in_data, &in_decrypted);
  1087. if (res)
  1088. return res;
  1089. continue_req:
  1090. wpa_hexdump_buf(MSG_MSGDUMP, "EAP-FAST: Decrypted Phase 2 TLV(s)",
  1091. in_decrypted);
  1092. if (wpabuf_len(in_decrypted) < 4) {
  1093. wpa_printf(MSG_INFO, "EAP-FAST: Too short Phase 2 "
  1094. "TLV frame (len=%lu)",
  1095. (unsigned long) wpabuf_len(in_decrypted));
  1096. wpabuf_free(in_decrypted);
  1097. return -1;
  1098. }
  1099. res = eap_fast_process_decrypted(sm, data, ret, req,
  1100. in_decrypted, out_data);
  1101. wpabuf_free(in_decrypted);
  1102. return res;
  1103. }
  1104. static const u8 * eap_fast_get_a_id(const u8 *buf, size_t len, size_t *id_len)
  1105. {
  1106. const u8 *a_id;
  1107. struct pac_tlv_hdr *hdr;
  1108. /*
  1109. * Parse authority identity (A-ID) from the EAP-FAST/Start. This
  1110. * supports both raw A-ID and one inside an A-ID TLV.
  1111. */
  1112. a_id = buf;
  1113. *id_len = len;
  1114. if (len > sizeof(*hdr)) {
  1115. int tlen;
  1116. hdr = (struct pac_tlv_hdr *) buf;
  1117. tlen = be_to_host16(hdr->len);
  1118. if (be_to_host16(hdr->type) == PAC_TYPE_A_ID &&
  1119. sizeof(*hdr) + tlen <= len) {
  1120. wpa_printf(MSG_DEBUG, "EAP-FAST: A-ID was in TLV "
  1121. "(Start)");
  1122. a_id = (u8 *) (hdr + 1);
  1123. *id_len = tlen;
  1124. }
  1125. }
  1126. wpa_hexdump_ascii(MSG_DEBUG, "EAP-FAST: A-ID", a_id, *id_len);
  1127. return a_id;
  1128. }
  1129. static void eap_fast_select_pac(struct eap_fast_data *data,
  1130. const u8 *a_id, size_t a_id_len)
  1131. {
  1132. data->current_pac = eap_fast_get_pac(data->pac, a_id, a_id_len,
  1133. PAC_TYPE_TUNNEL_PAC);
  1134. if (data->current_pac == NULL) {
  1135. /*
  1136. * Tunnel PAC was not available for this A-ID. Try to use
  1137. * Machine Authentication PAC, if one is available.
  1138. */
  1139. data->current_pac = eap_fast_get_pac(
  1140. data->pac, a_id, a_id_len,
  1141. PAC_TYPE_MACHINE_AUTHENTICATION);
  1142. }
  1143. if (data->current_pac) {
  1144. wpa_printf(MSG_DEBUG, "EAP-FAST: PAC found for this A-ID "
  1145. "(PAC-Type %d)", data->current_pac->pac_type);
  1146. wpa_hexdump_ascii(MSG_MSGDUMP, "EAP-FAST: A-ID-Info",
  1147. data->current_pac->a_id_info,
  1148. data->current_pac->a_id_info_len);
  1149. }
  1150. }
  1151. static int eap_fast_use_pac_opaque(struct eap_sm *sm,
  1152. struct eap_fast_data *data,
  1153. struct eap_fast_pac *pac)
  1154. {
  1155. u8 *tlv;
  1156. size_t tlv_len, olen;
  1157. struct eap_tlv_hdr *ehdr;
  1158. olen = pac->pac_opaque_len;
  1159. tlv_len = sizeof(*ehdr) + olen;
  1160. tlv = os_malloc(tlv_len);
  1161. if (tlv) {
  1162. ehdr = (struct eap_tlv_hdr *) tlv;
  1163. ehdr->tlv_type = host_to_be16(PAC_TYPE_PAC_OPAQUE);
  1164. ehdr->length = host_to_be16(olen);
  1165. os_memcpy(ehdr + 1, pac->pac_opaque, olen);
  1166. }
  1167. if (tlv == NULL ||
  1168. tls_connection_client_hello_ext(sm->ssl_ctx, data->ssl.conn,
  1169. TLS_EXT_PAC_OPAQUE,
  1170. tlv, tlv_len) < 0) {
  1171. wpa_printf(MSG_DEBUG, "EAP-FAST: Failed to add PAC-Opaque TLS "
  1172. "extension");
  1173. os_free(tlv);
  1174. return -1;
  1175. }
  1176. os_free(tlv);
  1177. return 0;
  1178. }
  1179. static int eap_fast_clear_pac_opaque_ext(struct eap_sm *sm,
  1180. struct eap_fast_data *data)
  1181. {
  1182. if (tls_connection_client_hello_ext(sm->ssl_ctx, data->ssl.conn,
  1183. TLS_EXT_PAC_OPAQUE, NULL, 0) < 0) {
  1184. wpa_printf(MSG_DEBUG, "EAP-FAST: Failed to remove PAC-Opaque "
  1185. "TLS extension");
  1186. return -1;
  1187. }
  1188. return 0;
  1189. }
  1190. static int eap_fast_set_provisioning_ciphers(struct eap_sm *sm,
  1191. struct eap_fast_data *data)
  1192. {
  1193. u8 ciphers[5];
  1194. int count = 0;
  1195. if (data->provisioning_allowed & EAP_FAST_PROV_UNAUTH) {
  1196. wpa_printf(MSG_DEBUG, "EAP-FAST: Enabling unauthenticated "
  1197. "provisioning TLS cipher suites");
  1198. ciphers[count++] = TLS_CIPHER_ANON_DH_AES128_SHA;
  1199. }
  1200. if (data->provisioning_allowed & EAP_FAST_PROV_AUTH) {
  1201. wpa_printf(MSG_DEBUG, "EAP-FAST: Enabling authenticated "
  1202. "provisioning TLS cipher suites");
  1203. ciphers[count++] = TLS_CIPHER_RSA_DHE_AES128_SHA;
  1204. ciphers[count++] = TLS_CIPHER_AES128_SHA;
  1205. ciphers[count++] = TLS_CIPHER_RC4_SHA;
  1206. }
  1207. ciphers[count++] = TLS_CIPHER_NONE;
  1208. if (tls_connection_set_cipher_list(sm->ssl_ctx, data->ssl.conn,
  1209. ciphers)) {
  1210. wpa_printf(MSG_INFO, "EAP-FAST: Could not configure TLS "
  1211. "cipher suites for provisioning");
  1212. return -1;
  1213. }
  1214. return 0;
  1215. }
  1216. static int eap_fast_process_start(struct eap_sm *sm,
  1217. struct eap_fast_data *data, u8 flags,
  1218. const u8 *pos, size_t left)
  1219. {
  1220. const u8 *a_id;
  1221. size_t a_id_len;
  1222. /* EAP-FAST Version negotiation (section 3.1) */
  1223. wpa_printf(MSG_DEBUG, "EAP-FAST: Start (server ver=%d, own ver=%d)",
  1224. flags & EAP_PEAP_VERSION_MASK, data->fast_version);
  1225. if ((flags & EAP_PEAP_VERSION_MASK) < data->fast_version)
  1226. data->fast_version = flags & EAP_PEAP_VERSION_MASK;
  1227. wpa_printf(MSG_DEBUG, "EAP-FAST: Using FAST version %d",
  1228. data->fast_version);
  1229. a_id = eap_fast_get_a_id(pos, left, &a_id_len);
  1230. eap_fast_select_pac(data, a_id, a_id_len);
  1231. if (data->resuming && data->current_pac) {
  1232. wpa_printf(MSG_DEBUG, "EAP-FAST: Trying to resume session - "
  1233. "do not add PAC-Opaque to TLS ClientHello");
  1234. if (eap_fast_clear_pac_opaque_ext(sm, data) < 0)
  1235. return -1;
  1236. } else if (data->current_pac) {
  1237. /*
  1238. * PAC found for the A-ID and we are not resuming an old
  1239. * session, so add PAC-Opaque extension to ClientHello.
  1240. */
  1241. if (eap_fast_use_pac_opaque(sm, data, data->current_pac) < 0)
  1242. return -1;
  1243. } else {
  1244. /* No PAC found, so we must provision one. */
  1245. if (!data->provisioning_allowed) {
  1246. wpa_printf(MSG_DEBUG, "EAP-FAST: No PAC found and "
  1247. "provisioning disabled");
  1248. return -1;
  1249. }
  1250. wpa_printf(MSG_DEBUG, "EAP-FAST: No PAC found - "
  1251. "starting provisioning");
  1252. if (eap_fast_set_provisioning_ciphers(sm, data) < 0 ||
  1253. eap_fast_clear_pac_opaque_ext(sm, data) < 0)
  1254. return -1;
  1255. data->provisioning = 1;
  1256. }
  1257. return 0;
  1258. }
  1259. static struct wpabuf * eap_fast_process(struct eap_sm *sm, void *priv,
  1260. struct eap_method_ret *ret,
  1261. const struct wpabuf *reqData)
  1262. {
  1263. const struct eap_hdr *req;
  1264. size_t left;
  1265. int res;
  1266. u8 flags, id;
  1267. struct wpabuf *resp;
  1268. const u8 *pos;
  1269. struct eap_fast_data *data = priv;
  1270. pos = eap_peer_tls_process_init(sm, &data->ssl, EAP_TYPE_FAST, ret,
  1271. reqData, &left, &flags);
  1272. if (pos == NULL)
  1273. return NULL;
  1274. req = wpabuf_head(reqData);
  1275. id = req->identifier;
  1276. if (flags & EAP_TLS_FLAGS_START) {
  1277. if (eap_fast_process_start(sm, data, flags, pos, left) < 0)
  1278. return NULL;
  1279. left = 0; /* A-ID is not used in further packet processing */
  1280. }
  1281. resp = NULL;
  1282. if (tls_connection_established(sm->ssl_ctx, data->ssl.conn) &&
  1283. !data->resuming) {
  1284. /* Process tunneled (encrypted) phase 2 data. */
  1285. struct wpabuf msg;
  1286. wpabuf_set(&msg, pos, left);
  1287. res = eap_fast_decrypt(sm, data, ret, req, &msg, &resp);
  1288. if (res < 0) {
  1289. ret->methodState = METHOD_DONE;
  1290. ret->decision = DECISION_FAIL;
  1291. /*
  1292. * Ack possible Alert that may have caused failure in
  1293. * decryption.
  1294. */
  1295. res = 1;
  1296. }
  1297. } else {
  1298. /* Continue processing TLS handshake (phase 1). */
  1299. res = eap_peer_tls_process_helper(sm, &data->ssl,
  1300. EAP_TYPE_FAST,
  1301. data->fast_version, id, pos,
  1302. left, &resp);
  1303. if (tls_connection_established(sm->ssl_ctx, data->ssl.conn)) {
  1304. char cipher[80];
  1305. wpa_printf(MSG_DEBUG,
  1306. "EAP-FAST: TLS done, proceed to Phase 2");
  1307. if (data->provisioning &&
  1308. (!(data->provisioning_allowed &
  1309. EAP_FAST_PROV_AUTH) ||
  1310. tls_get_cipher(sm->ssl_ctx, data->ssl.conn,
  1311. cipher, sizeof(cipher)) < 0 ||
  1312. os_strstr(cipher, "ADH-") ||
  1313. os_strstr(cipher, "anon"))) {
  1314. wpa_printf(MSG_DEBUG, "EAP-FAST: Using "
  1315. "anonymous (unauthenticated) "
  1316. "provisioning");
  1317. data->anon_provisioning = 1;
  1318. } else
  1319. data->anon_provisioning = 0;
  1320. data->resuming = 0;
  1321. eap_fast_derive_keys(sm, data);
  1322. }
  1323. if (res == 2) {
  1324. struct wpabuf msg;
  1325. /*
  1326. * Application data included in the handshake message.
  1327. */
  1328. wpabuf_free(data->pending_phase2_req);
  1329. data->pending_phase2_req = resp;
  1330. resp = NULL;
  1331. wpabuf_set(&msg, pos, left);
  1332. res = eap_fast_decrypt(sm, data, ret, req, &msg,
  1333. &resp);
  1334. }
  1335. }
  1336. if (res == 1) {
  1337. wpabuf_free(resp);
  1338. return eap_peer_tls_build_ack(id, EAP_TYPE_FAST,
  1339. data->fast_version);
  1340. }
  1341. return resp;
  1342. }
  1343. #if 0 /* FIX */
  1344. static Boolean eap_fast_has_reauth_data(struct eap_sm *sm, void *priv)
  1345. {
  1346. struct eap_fast_data *data = priv;
  1347. return tls_connection_established(sm->ssl_ctx, data->ssl.conn);
  1348. }
  1349. static void eap_fast_deinit_for_reauth(struct eap_sm *sm, void *priv)
  1350. {
  1351. struct eap_fast_data *data = priv;
  1352. os_free(data->key_block_p);
  1353. data->key_block_p = NULL;
  1354. wpabuf_free(data->pending_phase2_req);
  1355. data->pending_phase2_req = NULL;
  1356. }
  1357. static void * eap_fast_init_for_reauth(struct eap_sm *sm, void *priv)
  1358. {
  1359. struct eap_fast_data *data = priv;
  1360. if (eap_peer_tls_reauth_init(sm, &data->ssl)) {
  1361. os_free(data);
  1362. return NULL;
  1363. }
  1364. if (data->phase2_priv && data->phase2_method &&
  1365. data->phase2_method->init_for_reauth)
  1366. data->phase2_method->init_for_reauth(sm, data->phase2_priv);
  1367. data->phase2_success = 0;
  1368. data->resuming = 1;
  1369. data->provisioning = 0;
  1370. data->anon_provisioning = 0;
  1371. data->simck_idx = 0;
  1372. return priv;
  1373. }
  1374. #endif
  1375. static int eap_fast_get_status(struct eap_sm *sm, void *priv, char *buf,
  1376. size_t buflen, int verbose)
  1377. {
  1378. struct eap_fast_data *data = priv;
  1379. int len, ret;
  1380. len = eap_peer_tls_status(sm, &data->ssl, buf, buflen, verbose);
  1381. if (data->phase2_method) {
  1382. ret = os_snprintf(buf + len, buflen - len,
  1383. "EAP-FAST Phase2 method=%s\n",
  1384. data->phase2_method->name);
  1385. if (ret < 0 || (size_t) ret >= buflen - len)
  1386. return len;
  1387. len += ret;
  1388. }
  1389. return len;
  1390. }
  1391. static Boolean eap_fast_isKeyAvailable(struct eap_sm *sm, void *priv)
  1392. {
  1393. struct eap_fast_data *data = priv;
  1394. return data->success;
  1395. }
  1396. static u8 * eap_fast_getKey(struct eap_sm *sm, void *priv, size_t *len)
  1397. {
  1398. struct eap_fast_data *data = priv;
  1399. u8 *key;
  1400. if (!data->success)
  1401. return NULL;
  1402. key = os_malloc(EAP_FAST_KEY_LEN);
  1403. if (key == NULL)
  1404. return NULL;
  1405. *len = EAP_FAST_KEY_LEN;
  1406. os_memcpy(key, data->key_data, EAP_FAST_KEY_LEN);
  1407. return key;
  1408. }
  1409. static u8 * eap_fast_get_emsk(struct eap_sm *sm, void *priv, size_t *len)
  1410. {
  1411. struct eap_fast_data *data = priv;
  1412. u8 *key;
  1413. if (!data->success)
  1414. return NULL;
  1415. key = os_malloc(EAP_EMSK_LEN);
  1416. if (key == NULL)
  1417. return NULL;
  1418. *len = EAP_EMSK_LEN;
  1419. os_memcpy(key, data->emsk, EAP_EMSK_LEN);
  1420. return key;
  1421. }
  1422. int eap_peer_fast_register(void)
  1423. {
  1424. struct eap_method *eap;
  1425. int ret;
  1426. eap = eap_peer_method_alloc(EAP_PEER_METHOD_INTERFACE_VERSION,
  1427. EAP_VENDOR_IETF, EAP_TYPE_FAST, "FAST");
  1428. if (eap == NULL)
  1429. return -1;
  1430. eap->init = eap_fast_init;
  1431. eap->deinit = eap_fast_deinit;
  1432. eap->process = eap_fast_process;
  1433. eap->isKeyAvailable = eap_fast_isKeyAvailable;
  1434. eap->getKey = eap_fast_getKey;
  1435. eap->get_status = eap_fast_get_status;
  1436. #if 0
  1437. eap->has_reauth_data = eap_fast_has_reauth_data;
  1438. eap->deinit_for_reauth = eap_fast_deinit_for_reauth;
  1439. eap->init_for_reauth = eap_fast_init_for_reauth;
  1440. #endif
  1441. eap->get_emsk = eap_fast_get_emsk;
  1442. ret = eap_peer_method_register(eap);
  1443. if (ret)
  1444. eap_peer_method_free(eap);
  1445. return ret;
  1446. }