eap_fast.c 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716
  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 "crypto/tls.h"
  17. #include "crypto/sha1.h"
  18. #include "eap_common/eap_tlv_common.h"
  19. #include "eap_i.h"
  20. #include "eap_tls_common.h"
  21. #include "eap_config.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. /* RFC 5422, Section 4.2.6 - PAC-Type TLV */
  775. if (len != 2) {
  776. wpa_printf(MSG_INFO, "EAP-FAST: Invalid PAC-Type "
  777. "length %lu (expected 2)",
  778. (unsigned long) len);
  779. wpa_hexdump_ascii(MSG_DEBUG,
  780. "EAP-FAST: PAC-Info - PAC-Type",
  781. pos, len);
  782. return -1;
  783. }
  784. pac_type = WPA_GET_BE16(pos);
  785. if (pac_type != PAC_TYPE_TUNNEL_PAC &&
  786. pac_type != PAC_TYPE_USER_AUTHORIZATION &&
  787. pac_type != PAC_TYPE_MACHINE_AUTHENTICATION) {
  788. wpa_printf(MSG_INFO, "EAP-FAST: Unsupported PAC Type "
  789. "%d", pac_type);
  790. return -1;
  791. }
  792. wpa_printf(MSG_DEBUG, "EAP-FAST: PAC-Info - PAC-Type %d",
  793. pac_type);
  794. entry->pac_type = pac_type;
  795. break;
  796. default:
  797. wpa_printf(MSG_DEBUG, "EAP-FAST: Ignored unknown PAC-Info "
  798. "type %d", type);
  799. break;
  800. }
  801. return 0;
  802. }
  803. static int eap_fast_process_pac_info(struct eap_fast_pac *entry)
  804. {
  805. struct pac_tlv_hdr *hdr;
  806. u8 *pos;
  807. size_t left, len;
  808. int type;
  809. /* RFC 5422, Section 4.2.4 */
  810. /* PAC-Type defaults to Tunnel PAC (Type 1) */
  811. entry->pac_type = PAC_TYPE_TUNNEL_PAC;
  812. pos = entry->pac_info;
  813. left = entry->pac_info_len;
  814. while (left > sizeof(*hdr)) {
  815. hdr = (struct pac_tlv_hdr *) pos;
  816. type = be_to_host16(hdr->type);
  817. len = be_to_host16(hdr->len);
  818. pos += sizeof(*hdr);
  819. left -= sizeof(*hdr);
  820. if (len > left) {
  821. wpa_printf(MSG_DEBUG, "EAP-FAST: PAC-Info overrun "
  822. "(type=%d len=%lu left=%lu)",
  823. type, (unsigned long) len,
  824. (unsigned long) left);
  825. return -1;
  826. }
  827. if (eap_fast_parse_pac_info(entry, type, pos, len) < 0)
  828. return -1;
  829. pos += len;
  830. left -= len;
  831. }
  832. if (entry->a_id == NULL || entry->a_id_info == NULL) {
  833. wpa_printf(MSG_DEBUG, "EAP-FAST: PAC-Info does not include "
  834. "all the required fields");
  835. return -1;
  836. }
  837. return 0;
  838. }
  839. static struct wpabuf * eap_fast_process_pac(struct eap_sm *sm,
  840. struct eap_fast_data *data,
  841. struct eap_method_ret *ret,
  842. u8 *pac, size_t pac_len)
  843. {
  844. struct eap_peer_config *config = eap_get_config(sm);
  845. struct eap_fast_pac entry;
  846. os_memset(&entry, 0, sizeof(entry));
  847. if (eap_fast_process_pac_tlv(&entry, pac, pac_len) ||
  848. eap_fast_process_pac_info(&entry))
  849. return NULL;
  850. eap_fast_add_pac(&data->pac, &data->current_pac, &entry);
  851. eap_fast_pac_list_truncate(data->pac, data->max_pac_list_len);
  852. if (data->use_pac_binary_format)
  853. eap_fast_save_pac_bin(sm, data->pac, config->pac_file);
  854. else
  855. eap_fast_save_pac(sm, data->pac, config->pac_file);
  856. if (data->provisioning) {
  857. if (data->anon_provisioning) {
  858. /*
  859. * Unauthenticated provisioning does not provide keying
  860. * material and must end with an EAP-Failure.
  861. * Authentication will be done separately after this.
  862. */
  863. data->success = 0;
  864. ret->decision = DECISION_FAIL;
  865. } else {
  866. /*
  867. * Server may or may not allow authenticated
  868. * provisioning also for key generation.
  869. */
  870. ret->decision = DECISION_COND_SUCC;
  871. }
  872. wpa_printf(MSG_DEBUG, "EAP-FAST: Send PAC-Acknowledgement TLV "
  873. "- Provisioning completed successfully");
  874. } else {
  875. /*
  876. * This is PAC refreshing, i.e., normal authentication that is
  877. * expected to be completed with an EAP-Success. However,
  878. * RFC 5422, Section 3.5 allows EAP-Failure to be sent even
  879. * after protected success exchange in case of EAP-Fast
  880. * provisioning, so we better use DECISION_COND_SUCC here
  881. * instead of DECISION_UNCOND_SUCC.
  882. */
  883. wpa_printf(MSG_DEBUG, "EAP-FAST: Send PAC-Acknowledgement TLV "
  884. "- PAC refreshing completed successfully");
  885. ret->decision = DECISION_COND_SUCC;
  886. }
  887. ret->methodState = METHOD_DONE;
  888. return eap_fast_tlv_pac_ack();
  889. }
  890. static int eap_fast_parse_decrypted(struct wpabuf *decrypted,
  891. struct eap_fast_tlv_parse *tlv,
  892. struct wpabuf **resp)
  893. {
  894. int mandatory, tlv_type, len, res;
  895. u8 *pos, *end;
  896. os_memset(tlv, 0, sizeof(*tlv));
  897. /* Parse TLVs from the decrypted Phase 2 data */
  898. pos = wpabuf_mhead(decrypted);
  899. end = pos + wpabuf_len(decrypted);
  900. while (pos + 4 < end) {
  901. mandatory = pos[0] & 0x80;
  902. tlv_type = WPA_GET_BE16(pos) & 0x3fff;
  903. pos += 2;
  904. len = WPA_GET_BE16(pos);
  905. pos += 2;
  906. if (pos + len > end) {
  907. wpa_printf(MSG_INFO, "EAP-FAST: TLV overflow");
  908. return -1;
  909. }
  910. wpa_printf(MSG_DEBUG, "EAP-FAST: Received Phase 2: "
  911. "TLV type %d length %d%s",
  912. tlv_type, len, mandatory ? " (mandatory)" : "");
  913. res = eap_fast_parse_tlv(tlv, tlv_type, pos, len);
  914. if (res == -2)
  915. break;
  916. if (res < 0) {
  917. if (mandatory) {
  918. wpa_printf(MSG_DEBUG, "EAP-FAST: Nak unknown "
  919. "mandatory TLV type %d", tlv_type);
  920. *resp = eap_fast_tlv_nak(0, tlv_type);
  921. break;
  922. } else {
  923. wpa_printf(MSG_DEBUG, "EAP-FAST: ignored "
  924. "unknown optional TLV type %d",
  925. tlv_type);
  926. }
  927. }
  928. pos += len;
  929. }
  930. return 0;
  931. }
  932. static int eap_fast_encrypt_response(struct eap_sm *sm,
  933. struct eap_fast_data *data,
  934. struct wpabuf *resp,
  935. u8 identifier, struct wpabuf **out_data)
  936. {
  937. if (resp == NULL)
  938. return 0;
  939. wpa_hexdump_buf(MSG_DEBUG, "EAP-FAST: Encrypting Phase 2 data",
  940. resp);
  941. if (eap_peer_tls_encrypt(sm, &data->ssl, EAP_TYPE_FAST,
  942. data->fast_version, identifier,
  943. resp, out_data)) {
  944. wpa_printf(MSG_INFO, "EAP-FAST: Failed to encrypt a Phase 2 "
  945. "frame");
  946. }
  947. wpabuf_free(resp);
  948. return 0;
  949. }
  950. static struct wpabuf * eap_fast_pac_request(void)
  951. {
  952. struct wpabuf *tmp;
  953. u8 *pos, *pos2;
  954. tmp = wpabuf_alloc(sizeof(struct eap_tlv_hdr) +
  955. sizeof(struct eap_tlv_request_action_tlv) +
  956. sizeof(struct eap_tlv_pac_type_tlv));
  957. if (tmp == NULL)
  958. return NULL;
  959. pos = wpabuf_put(tmp, 0);
  960. pos2 = eap_fast_write_pac_request(pos, PAC_TYPE_TUNNEL_PAC);
  961. wpabuf_put(tmp, pos2 - pos);
  962. return tmp;
  963. }
  964. static int eap_fast_process_decrypted(struct eap_sm *sm,
  965. struct eap_fast_data *data,
  966. struct eap_method_ret *ret,
  967. const struct eap_hdr *req,
  968. struct wpabuf *decrypted,
  969. struct wpabuf **out_data)
  970. {
  971. struct wpabuf *resp = NULL, *tmp;
  972. struct eap_fast_tlv_parse tlv;
  973. int failed = 0;
  974. if (eap_fast_parse_decrypted(decrypted, &tlv, &resp) < 0)
  975. return 0;
  976. if (resp)
  977. return eap_fast_encrypt_response(sm, data, resp,
  978. req->identifier, out_data);
  979. if (tlv.result == EAP_TLV_RESULT_FAILURE) {
  980. resp = eap_fast_tlv_result(EAP_TLV_RESULT_FAILURE, 0);
  981. return eap_fast_encrypt_response(sm, data, resp,
  982. req->identifier, out_data);
  983. }
  984. if (tlv.iresult == EAP_TLV_RESULT_FAILURE) {
  985. resp = eap_fast_tlv_result(EAP_TLV_RESULT_FAILURE, 1);
  986. return eap_fast_encrypt_response(sm, data, resp,
  987. req->identifier, out_data);
  988. }
  989. if (tlv.crypto_binding) {
  990. tmp = eap_fast_process_crypto_binding(sm, data, ret,
  991. tlv.crypto_binding,
  992. tlv.crypto_binding_len);
  993. if (tmp == NULL)
  994. failed = 1;
  995. else
  996. resp = wpabuf_concat(resp, tmp);
  997. }
  998. if (tlv.iresult == EAP_TLV_RESULT_SUCCESS) {
  999. tmp = eap_fast_tlv_result(failed ? EAP_TLV_RESULT_FAILURE :
  1000. EAP_TLV_RESULT_SUCCESS, 1);
  1001. resp = wpabuf_concat(resp, tmp);
  1002. }
  1003. if (tlv.eap_payload_tlv) {
  1004. tmp = eap_fast_process_eap_payload_tlv(
  1005. sm, data, ret, req, tlv.eap_payload_tlv,
  1006. tlv.eap_payload_tlv_len);
  1007. resp = wpabuf_concat(resp, tmp);
  1008. }
  1009. if (tlv.pac && tlv.result != EAP_TLV_RESULT_SUCCESS) {
  1010. wpa_printf(MSG_DEBUG, "EAP-FAST: PAC TLV without Result TLV "
  1011. "acknowledging success");
  1012. failed = 1;
  1013. } else if (tlv.pac && tlv.result == EAP_TLV_RESULT_SUCCESS) {
  1014. tmp = eap_fast_process_pac(sm, data, ret, tlv.pac,
  1015. tlv.pac_len);
  1016. resp = wpabuf_concat(resp, tmp);
  1017. }
  1018. if (data->current_pac == NULL && data->provisioning &&
  1019. !data->anon_provisioning && !tlv.pac &&
  1020. (tlv.iresult == EAP_TLV_RESULT_SUCCESS ||
  1021. tlv.result == EAP_TLV_RESULT_SUCCESS)) {
  1022. /*
  1023. * Need to request Tunnel PAC when using authenticated
  1024. * provisioning.
  1025. */
  1026. wpa_printf(MSG_DEBUG, "EAP-FAST: Request Tunnel PAC");
  1027. tmp = eap_fast_pac_request();
  1028. resp = wpabuf_concat(resp, tmp);
  1029. }
  1030. if (tlv.result == EAP_TLV_RESULT_SUCCESS && !failed) {
  1031. tmp = eap_fast_tlv_result(EAP_TLV_RESULT_SUCCESS, 0);
  1032. resp = wpabuf_concat(tmp, resp);
  1033. } else if (failed) {
  1034. tmp = eap_fast_tlv_result(EAP_TLV_RESULT_FAILURE, 0);
  1035. resp = wpabuf_concat(tmp, resp);
  1036. }
  1037. if (resp && tlv.result == EAP_TLV_RESULT_SUCCESS && !failed &&
  1038. tlv.crypto_binding && data->phase2_success) {
  1039. if (data->anon_provisioning) {
  1040. wpa_printf(MSG_DEBUG, "EAP-FAST: Unauthenticated "
  1041. "provisioning completed successfully.");
  1042. ret->methodState = METHOD_DONE;
  1043. ret->decision = DECISION_FAIL;
  1044. } else {
  1045. wpa_printf(MSG_DEBUG, "EAP-FAST: Authentication "
  1046. "completed successfully.");
  1047. if (data->provisioning)
  1048. ret->methodState = METHOD_MAY_CONT;
  1049. else
  1050. ret->methodState = METHOD_DONE;
  1051. ret->decision = DECISION_UNCOND_SUCC;
  1052. }
  1053. }
  1054. if (resp == NULL) {
  1055. wpa_printf(MSG_DEBUG, "EAP-FAST: No recognized TLVs - send "
  1056. "empty response packet");
  1057. resp = wpabuf_alloc(1);
  1058. }
  1059. return eap_fast_encrypt_response(sm, data, resp, req->identifier,
  1060. out_data);
  1061. }
  1062. static int eap_fast_decrypt(struct eap_sm *sm, struct eap_fast_data *data,
  1063. struct eap_method_ret *ret,
  1064. const struct eap_hdr *req,
  1065. const struct wpabuf *in_data,
  1066. struct wpabuf **out_data)
  1067. {
  1068. struct wpabuf *in_decrypted;
  1069. int res;
  1070. wpa_printf(MSG_DEBUG, "EAP-FAST: Received %lu bytes encrypted data for"
  1071. " Phase 2", (unsigned long) wpabuf_len(in_data));
  1072. if (data->pending_phase2_req) {
  1073. wpa_printf(MSG_DEBUG, "EAP-FAST: Pending Phase 2 request - "
  1074. "skip decryption and use old data");
  1075. /* Clear TLS reassembly state. */
  1076. eap_peer_tls_reset_input(&data->ssl);
  1077. in_decrypted = data->pending_phase2_req;
  1078. data->pending_phase2_req = NULL;
  1079. goto continue_req;
  1080. }
  1081. if (wpabuf_len(in_data) == 0) {
  1082. /* Received TLS ACK - requesting more fragments */
  1083. return eap_peer_tls_encrypt(sm, &data->ssl, EAP_TYPE_FAST,
  1084. data->fast_version,
  1085. req->identifier, NULL, out_data);
  1086. }
  1087. res = eap_peer_tls_decrypt(sm, &data->ssl, in_data, &in_decrypted);
  1088. if (res)
  1089. return res;
  1090. continue_req:
  1091. wpa_hexdump_buf(MSG_MSGDUMP, "EAP-FAST: Decrypted Phase 2 TLV(s)",
  1092. in_decrypted);
  1093. if (wpabuf_len(in_decrypted) < 4) {
  1094. wpa_printf(MSG_INFO, "EAP-FAST: Too short Phase 2 "
  1095. "TLV frame (len=%lu)",
  1096. (unsigned long) wpabuf_len(in_decrypted));
  1097. wpabuf_free(in_decrypted);
  1098. return -1;
  1099. }
  1100. res = eap_fast_process_decrypted(sm, data, ret, req,
  1101. in_decrypted, out_data);
  1102. wpabuf_free(in_decrypted);
  1103. return res;
  1104. }
  1105. static const u8 * eap_fast_get_a_id(const u8 *buf, size_t len, size_t *id_len)
  1106. {
  1107. const u8 *a_id;
  1108. struct pac_tlv_hdr *hdr;
  1109. /*
  1110. * Parse authority identity (A-ID) from the EAP-FAST/Start. This
  1111. * supports both raw A-ID and one inside an A-ID TLV.
  1112. */
  1113. a_id = buf;
  1114. *id_len = len;
  1115. if (len > sizeof(*hdr)) {
  1116. int tlen;
  1117. hdr = (struct pac_tlv_hdr *) buf;
  1118. tlen = be_to_host16(hdr->len);
  1119. if (be_to_host16(hdr->type) == PAC_TYPE_A_ID &&
  1120. sizeof(*hdr) + tlen <= len) {
  1121. wpa_printf(MSG_DEBUG, "EAP-FAST: A-ID was in TLV "
  1122. "(Start)");
  1123. a_id = (u8 *) (hdr + 1);
  1124. *id_len = tlen;
  1125. }
  1126. }
  1127. wpa_hexdump_ascii(MSG_DEBUG, "EAP-FAST: A-ID", a_id, *id_len);
  1128. return a_id;
  1129. }
  1130. static void eap_fast_select_pac(struct eap_fast_data *data,
  1131. const u8 *a_id, size_t a_id_len)
  1132. {
  1133. data->current_pac = eap_fast_get_pac(data->pac, a_id, a_id_len,
  1134. PAC_TYPE_TUNNEL_PAC);
  1135. if (data->current_pac == NULL) {
  1136. /*
  1137. * Tunnel PAC was not available for this A-ID. Try to use
  1138. * Machine Authentication PAC, if one is available.
  1139. */
  1140. data->current_pac = eap_fast_get_pac(
  1141. data->pac, a_id, a_id_len,
  1142. PAC_TYPE_MACHINE_AUTHENTICATION);
  1143. }
  1144. if (data->current_pac) {
  1145. wpa_printf(MSG_DEBUG, "EAP-FAST: PAC found for this A-ID "
  1146. "(PAC-Type %d)", data->current_pac->pac_type);
  1147. wpa_hexdump_ascii(MSG_MSGDUMP, "EAP-FAST: A-ID-Info",
  1148. data->current_pac->a_id_info,
  1149. data->current_pac->a_id_info_len);
  1150. }
  1151. }
  1152. static int eap_fast_use_pac_opaque(struct eap_sm *sm,
  1153. struct eap_fast_data *data,
  1154. struct eap_fast_pac *pac)
  1155. {
  1156. u8 *tlv;
  1157. size_t tlv_len, olen;
  1158. struct eap_tlv_hdr *ehdr;
  1159. olen = pac->pac_opaque_len;
  1160. tlv_len = sizeof(*ehdr) + olen;
  1161. tlv = os_malloc(tlv_len);
  1162. if (tlv) {
  1163. ehdr = (struct eap_tlv_hdr *) tlv;
  1164. ehdr->tlv_type = host_to_be16(PAC_TYPE_PAC_OPAQUE);
  1165. ehdr->length = host_to_be16(olen);
  1166. os_memcpy(ehdr + 1, pac->pac_opaque, olen);
  1167. }
  1168. if (tlv == NULL ||
  1169. tls_connection_client_hello_ext(sm->ssl_ctx, data->ssl.conn,
  1170. TLS_EXT_PAC_OPAQUE,
  1171. tlv, tlv_len) < 0) {
  1172. wpa_printf(MSG_DEBUG, "EAP-FAST: Failed to add PAC-Opaque TLS "
  1173. "extension");
  1174. os_free(tlv);
  1175. return -1;
  1176. }
  1177. os_free(tlv);
  1178. return 0;
  1179. }
  1180. static int eap_fast_clear_pac_opaque_ext(struct eap_sm *sm,
  1181. struct eap_fast_data *data)
  1182. {
  1183. if (tls_connection_client_hello_ext(sm->ssl_ctx, data->ssl.conn,
  1184. TLS_EXT_PAC_OPAQUE, NULL, 0) < 0) {
  1185. wpa_printf(MSG_DEBUG, "EAP-FAST: Failed to remove PAC-Opaque "
  1186. "TLS extension");
  1187. return -1;
  1188. }
  1189. return 0;
  1190. }
  1191. static int eap_fast_set_provisioning_ciphers(struct eap_sm *sm,
  1192. struct eap_fast_data *data)
  1193. {
  1194. u8 ciphers[5];
  1195. int count = 0;
  1196. if (data->provisioning_allowed & EAP_FAST_PROV_UNAUTH) {
  1197. wpa_printf(MSG_DEBUG, "EAP-FAST: Enabling unauthenticated "
  1198. "provisioning TLS cipher suites");
  1199. ciphers[count++] = TLS_CIPHER_ANON_DH_AES128_SHA;
  1200. }
  1201. if (data->provisioning_allowed & EAP_FAST_PROV_AUTH) {
  1202. wpa_printf(MSG_DEBUG, "EAP-FAST: Enabling authenticated "
  1203. "provisioning TLS cipher suites");
  1204. ciphers[count++] = TLS_CIPHER_RSA_DHE_AES128_SHA;
  1205. ciphers[count++] = TLS_CIPHER_AES128_SHA;
  1206. ciphers[count++] = TLS_CIPHER_RC4_SHA;
  1207. }
  1208. ciphers[count++] = TLS_CIPHER_NONE;
  1209. if (tls_connection_set_cipher_list(sm->ssl_ctx, data->ssl.conn,
  1210. ciphers)) {
  1211. wpa_printf(MSG_INFO, "EAP-FAST: Could not configure TLS "
  1212. "cipher suites for provisioning");
  1213. return -1;
  1214. }
  1215. return 0;
  1216. }
  1217. static int eap_fast_process_start(struct eap_sm *sm,
  1218. struct eap_fast_data *data, u8 flags,
  1219. const u8 *pos, size_t left)
  1220. {
  1221. const u8 *a_id;
  1222. size_t a_id_len;
  1223. /* EAP-FAST Version negotiation (section 3.1) */
  1224. wpa_printf(MSG_DEBUG, "EAP-FAST: Start (server ver=%d, own ver=%d)",
  1225. flags & EAP_TLS_VERSION_MASK, data->fast_version);
  1226. if ((flags & EAP_TLS_VERSION_MASK) < data->fast_version)
  1227. data->fast_version = flags & EAP_TLS_VERSION_MASK;
  1228. wpa_printf(MSG_DEBUG, "EAP-FAST: Using FAST version %d",
  1229. data->fast_version);
  1230. a_id = eap_fast_get_a_id(pos, left, &a_id_len);
  1231. eap_fast_select_pac(data, a_id, a_id_len);
  1232. if (data->resuming && data->current_pac) {
  1233. wpa_printf(MSG_DEBUG, "EAP-FAST: Trying to resume session - "
  1234. "do not add PAC-Opaque to TLS ClientHello");
  1235. if (eap_fast_clear_pac_opaque_ext(sm, data) < 0)
  1236. return -1;
  1237. } else if (data->current_pac) {
  1238. /*
  1239. * PAC found for the A-ID and we are not resuming an old
  1240. * session, so add PAC-Opaque extension to ClientHello.
  1241. */
  1242. if (eap_fast_use_pac_opaque(sm, data, data->current_pac) < 0)
  1243. return -1;
  1244. } else {
  1245. /* No PAC found, so we must provision one. */
  1246. if (!data->provisioning_allowed) {
  1247. wpa_printf(MSG_DEBUG, "EAP-FAST: No PAC found and "
  1248. "provisioning disabled");
  1249. return -1;
  1250. }
  1251. wpa_printf(MSG_DEBUG, "EAP-FAST: No PAC found - "
  1252. "starting provisioning");
  1253. if (eap_fast_set_provisioning_ciphers(sm, data) < 0 ||
  1254. eap_fast_clear_pac_opaque_ext(sm, data) < 0)
  1255. return -1;
  1256. data->provisioning = 1;
  1257. }
  1258. return 0;
  1259. }
  1260. static struct wpabuf * eap_fast_process(struct eap_sm *sm, void *priv,
  1261. struct eap_method_ret *ret,
  1262. const struct wpabuf *reqData)
  1263. {
  1264. const struct eap_hdr *req;
  1265. size_t left;
  1266. int res;
  1267. u8 flags, id;
  1268. struct wpabuf *resp;
  1269. const u8 *pos;
  1270. struct eap_fast_data *data = priv;
  1271. pos = eap_peer_tls_process_init(sm, &data->ssl, EAP_TYPE_FAST, ret,
  1272. reqData, &left, &flags);
  1273. if (pos == NULL)
  1274. return NULL;
  1275. req = wpabuf_head(reqData);
  1276. id = req->identifier;
  1277. if (flags & EAP_TLS_FLAGS_START) {
  1278. if (eap_fast_process_start(sm, data, flags, pos, left) < 0)
  1279. return NULL;
  1280. left = 0; /* A-ID is not used in further packet processing */
  1281. }
  1282. resp = NULL;
  1283. if (tls_connection_established(sm->ssl_ctx, data->ssl.conn) &&
  1284. !data->resuming) {
  1285. /* Process tunneled (encrypted) phase 2 data. */
  1286. struct wpabuf msg;
  1287. wpabuf_set(&msg, pos, left);
  1288. res = eap_fast_decrypt(sm, data, ret, req, &msg, &resp);
  1289. if (res < 0) {
  1290. ret->methodState = METHOD_DONE;
  1291. ret->decision = DECISION_FAIL;
  1292. /*
  1293. * Ack possible Alert that may have caused failure in
  1294. * decryption.
  1295. */
  1296. res = 1;
  1297. }
  1298. } else {
  1299. /* Continue processing TLS handshake (phase 1). */
  1300. res = eap_peer_tls_process_helper(sm, &data->ssl,
  1301. EAP_TYPE_FAST,
  1302. data->fast_version, id, pos,
  1303. left, &resp);
  1304. if (tls_connection_established(sm->ssl_ctx, data->ssl.conn)) {
  1305. char cipher[80];
  1306. wpa_printf(MSG_DEBUG,
  1307. "EAP-FAST: TLS done, proceed to Phase 2");
  1308. if (data->provisioning &&
  1309. (!(data->provisioning_allowed &
  1310. EAP_FAST_PROV_AUTH) ||
  1311. tls_get_cipher(sm->ssl_ctx, data->ssl.conn,
  1312. cipher, sizeof(cipher)) < 0 ||
  1313. os_strstr(cipher, "ADH-") ||
  1314. os_strstr(cipher, "anon"))) {
  1315. wpa_printf(MSG_DEBUG, "EAP-FAST: Using "
  1316. "anonymous (unauthenticated) "
  1317. "provisioning");
  1318. data->anon_provisioning = 1;
  1319. } else
  1320. data->anon_provisioning = 0;
  1321. data->resuming = 0;
  1322. eap_fast_derive_keys(sm, data);
  1323. }
  1324. if (res == 2) {
  1325. struct wpabuf msg;
  1326. /*
  1327. * Application data included in the handshake message.
  1328. */
  1329. wpabuf_free(data->pending_phase2_req);
  1330. data->pending_phase2_req = resp;
  1331. resp = NULL;
  1332. wpabuf_set(&msg, pos, left);
  1333. res = eap_fast_decrypt(sm, data, ret, req, &msg,
  1334. &resp);
  1335. }
  1336. }
  1337. if (res == 1) {
  1338. wpabuf_free(resp);
  1339. return eap_peer_tls_build_ack(id, EAP_TYPE_FAST,
  1340. data->fast_version);
  1341. }
  1342. return resp;
  1343. }
  1344. #if 0 /* FIX */
  1345. static Boolean eap_fast_has_reauth_data(struct eap_sm *sm, void *priv)
  1346. {
  1347. struct eap_fast_data *data = priv;
  1348. return tls_connection_established(sm->ssl_ctx, data->ssl.conn);
  1349. }
  1350. static void eap_fast_deinit_for_reauth(struct eap_sm *sm, void *priv)
  1351. {
  1352. struct eap_fast_data *data = priv;
  1353. os_free(data->key_block_p);
  1354. data->key_block_p = NULL;
  1355. wpabuf_free(data->pending_phase2_req);
  1356. data->pending_phase2_req = NULL;
  1357. }
  1358. static void * eap_fast_init_for_reauth(struct eap_sm *sm, void *priv)
  1359. {
  1360. struct eap_fast_data *data = priv;
  1361. if (eap_peer_tls_reauth_init(sm, &data->ssl)) {
  1362. os_free(data);
  1363. return NULL;
  1364. }
  1365. if (data->phase2_priv && data->phase2_method &&
  1366. data->phase2_method->init_for_reauth)
  1367. data->phase2_method->init_for_reauth(sm, data->phase2_priv);
  1368. data->phase2_success = 0;
  1369. data->resuming = 1;
  1370. data->provisioning = 0;
  1371. data->anon_provisioning = 0;
  1372. data->simck_idx = 0;
  1373. return priv;
  1374. }
  1375. #endif
  1376. static int eap_fast_get_status(struct eap_sm *sm, void *priv, char *buf,
  1377. size_t buflen, int verbose)
  1378. {
  1379. struct eap_fast_data *data = priv;
  1380. int len, ret;
  1381. len = eap_peer_tls_status(sm, &data->ssl, buf, buflen, verbose);
  1382. if (data->phase2_method) {
  1383. ret = os_snprintf(buf + len, buflen - len,
  1384. "EAP-FAST Phase2 method=%s\n",
  1385. data->phase2_method->name);
  1386. if (ret < 0 || (size_t) ret >= buflen - len)
  1387. return len;
  1388. len += ret;
  1389. }
  1390. return len;
  1391. }
  1392. static Boolean eap_fast_isKeyAvailable(struct eap_sm *sm, void *priv)
  1393. {
  1394. struct eap_fast_data *data = priv;
  1395. return data->success;
  1396. }
  1397. static u8 * eap_fast_getKey(struct eap_sm *sm, void *priv, size_t *len)
  1398. {
  1399. struct eap_fast_data *data = priv;
  1400. u8 *key;
  1401. if (!data->success)
  1402. return NULL;
  1403. key = os_malloc(EAP_FAST_KEY_LEN);
  1404. if (key == NULL)
  1405. return NULL;
  1406. *len = EAP_FAST_KEY_LEN;
  1407. os_memcpy(key, data->key_data, EAP_FAST_KEY_LEN);
  1408. return key;
  1409. }
  1410. static u8 * eap_fast_get_emsk(struct eap_sm *sm, void *priv, size_t *len)
  1411. {
  1412. struct eap_fast_data *data = priv;
  1413. u8 *key;
  1414. if (!data->success)
  1415. return NULL;
  1416. key = os_malloc(EAP_EMSK_LEN);
  1417. if (key == NULL)
  1418. return NULL;
  1419. *len = EAP_EMSK_LEN;
  1420. os_memcpy(key, data->emsk, EAP_EMSK_LEN);
  1421. return key;
  1422. }
  1423. int eap_peer_fast_register(void)
  1424. {
  1425. struct eap_method *eap;
  1426. int ret;
  1427. eap = eap_peer_method_alloc(EAP_PEER_METHOD_INTERFACE_VERSION,
  1428. EAP_VENDOR_IETF, EAP_TYPE_FAST, "FAST");
  1429. if (eap == NULL)
  1430. return -1;
  1431. eap->init = eap_fast_init;
  1432. eap->deinit = eap_fast_deinit;
  1433. eap->process = eap_fast_process;
  1434. eap->isKeyAvailable = eap_fast_isKeyAvailable;
  1435. eap->getKey = eap_fast_getKey;
  1436. eap->get_status = eap_fast_get_status;
  1437. #if 0
  1438. eap->has_reauth_data = eap_fast_has_reauth_data;
  1439. eap->deinit_for_reauth = eap_fast_deinit_for_reauth;
  1440. eap->init_for_reauth = eap_fast_init_for_reauth;
  1441. #endif
  1442. eap->get_emsk = eap_fast_get_emsk;
  1443. ret = eap_peer_method_register(eap);
  1444. if (ret)
  1445. eap_peer_method_free(eap);
  1446. return ret;
  1447. }