eap_server_fast.c 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639
  1. /*
  2. * EAP-FAST server (RFC 4851)
  3. * Copyright (c) 2004-2008, Jouni Malinen <j@w1.fi>
  4. *
  5. * This software may be distributed under the terms of the BSD license.
  6. * See README for more details.
  7. */
  8. #include "includes.h"
  9. #include "common.h"
  10. #include "crypto/aes_wrap.h"
  11. #include "crypto/sha1.h"
  12. #include "crypto/tls.h"
  13. #include "crypto/random.h"
  14. #include "eap_common/eap_tlv_common.h"
  15. #include "eap_common/eap_fast_common.h"
  16. #include "eap_i.h"
  17. #include "eap_tls_common.h"
  18. static void eap_fast_reset(struct eap_sm *sm, void *priv);
  19. /* Private PAC-Opaque TLV types */
  20. #define PAC_OPAQUE_TYPE_PAD 0
  21. #define PAC_OPAQUE_TYPE_KEY 1
  22. #define PAC_OPAQUE_TYPE_LIFETIME 2
  23. #define PAC_OPAQUE_TYPE_IDENTITY 3
  24. struct eap_fast_data {
  25. struct eap_ssl_data ssl;
  26. enum {
  27. START, PHASE1, PHASE2_START, PHASE2_ID, PHASE2_METHOD,
  28. CRYPTO_BINDING, REQUEST_PAC, SUCCESS, FAILURE
  29. } state;
  30. int fast_version;
  31. const struct eap_method *phase2_method;
  32. void *phase2_priv;
  33. int force_version;
  34. int peer_version;
  35. u8 crypto_binding_nonce[32];
  36. int final_result;
  37. struct eap_fast_key_block_provisioning *key_block_p;
  38. u8 simck[EAP_FAST_SIMCK_LEN];
  39. u8 cmk[EAP_FAST_CMK_LEN];
  40. int simck_idx;
  41. u8 pac_opaque_encr[16];
  42. u8 *srv_id;
  43. size_t srv_id_len;
  44. char *srv_id_info;
  45. int anon_provisioning;
  46. int send_new_pac; /* server triggered re-keying of Tunnel PAC */
  47. struct wpabuf *pending_phase2_resp;
  48. u8 *identity; /* from PAC-Opaque */
  49. size_t identity_len;
  50. int eap_seq;
  51. int tnc_started;
  52. int pac_key_lifetime;
  53. int pac_key_refresh_time;
  54. };
  55. static int eap_fast_process_phase2_start(struct eap_sm *sm,
  56. struct eap_fast_data *data);
  57. static const char * eap_fast_state_txt(int state)
  58. {
  59. switch (state) {
  60. case START:
  61. return "START";
  62. case PHASE1:
  63. return "PHASE1";
  64. case PHASE2_START:
  65. return "PHASE2_START";
  66. case PHASE2_ID:
  67. return "PHASE2_ID";
  68. case PHASE2_METHOD:
  69. return "PHASE2_METHOD";
  70. case CRYPTO_BINDING:
  71. return "CRYPTO_BINDING";
  72. case REQUEST_PAC:
  73. return "REQUEST_PAC";
  74. case SUCCESS:
  75. return "SUCCESS";
  76. case FAILURE:
  77. return "FAILURE";
  78. default:
  79. return "Unknown?!";
  80. }
  81. }
  82. static void eap_fast_state(struct eap_fast_data *data, int state)
  83. {
  84. wpa_printf(MSG_DEBUG, "EAP-FAST: %s -> %s",
  85. eap_fast_state_txt(data->state),
  86. eap_fast_state_txt(state));
  87. data->state = state;
  88. }
  89. static EapType eap_fast_req_failure(struct eap_sm *sm,
  90. struct eap_fast_data *data)
  91. {
  92. /* TODO: send Result TLV(FAILURE) */
  93. eap_fast_state(data, FAILURE);
  94. return EAP_TYPE_NONE;
  95. }
  96. static int eap_fast_session_ticket_cb(void *ctx, const u8 *ticket, size_t len,
  97. const u8 *client_random,
  98. const u8 *server_random,
  99. u8 *master_secret)
  100. {
  101. struct eap_fast_data *data = ctx;
  102. const u8 *pac_opaque;
  103. size_t pac_opaque_len;
  104. u8 *buf, *pos, *end, *pac_key = NULL;
  105. os_time_t lifetime = 0;
  106. struct os_time now;
  107. u8 *identity = NULL;
  108. size_t identity_len = 0;
  109. wpa_printf(MSG_DEBUG, "EAP-FAST: SessionTicket callback");
  110. wpa_hexdump(MSG_DEBUG, "EAP-FAST: SessionTicket (PAC-Opaque)",
  111. ticket, len);
  112. if (len < 4 || WPA_GET_BE16(ticket) != PAC_TYPE_PAC_OPAQUE) {
  113. wpa_printf(MSG_DEBUG, "EAP-FAST: Ignore invalid "
  114. "SessionTicket");
  115. return 0;
  116. }
  117. pac_opaque_len = WPA_GET_BE16(ticket + 2);
  118. pac_opaque = ticket + 4;
  119. if (pac_opaque_len < 8 || pac_opaque_len % 8 ||
  120. pac_opaque_len > len - 4) {
  121. wpa_printf(MSG_DEBUG, "EAP-FAST: Ignore invalid PAC-Opaque "
  122. "(len=%lu left=%lu)",
  123. (unsigned long) pac_opaque_len,
  124. (unsigned long) len);
  125. return 0;
  126. }
  127. wpa_hexdump(MSG_DEBUG, "EAP-FAST: Received PAC-Opaque",
  128. pac_opaque, pac_opaque_len);
  129. buf = os_malloc(pac_opaque_len - 8);
  130. if (buf == NULL) {
  131. wpa_printf(MSG_DEBUG, "EAP-FAST: Failed to allocate memory "
  132. "for decrypting PAC-Opaque");
  133. return 0;
  134. }
  135. if (aes_unwrap(data->pac_opaque_encr, sizeof(data->pac_opaque_encr),
  136. (pac_opaque_len - 8) / 8, pac_opaque, buf) < 0) {
  137. wpa_printf(MSG_DEBUG, "EAP-FAST: Failed to decrypt "
  138. "PAC-Opaque");
  139. os_free(buf);
  140. /*
  141. * This may have been caused by server changing the PAC-Opaque
  142. * encryption key, so just ignore this PAC-Opaque instead of
  143. * failing the authentication completely. Provisioning can now
  144. * be used to provision a new PAC.
  145. */
  146. return 0;
  147. }
  148. end = buf + pac_opaque_len - 8;
  149. wpa_hexdump_key(MSG_DEBUG, "EAP-FAST: Decrypted PAC-Opaque",
  150. buf, end - buf);
  151. pos = buf;
  152. while (end - pos > 1) {
  153. u8 id, elen;
  154. id = *pos++;
  155. elen = *pos++;
  156. if (elen > end - pos)
  157. break;
  158. switch (id) {
  159. case PAC_OPAQUE_TYPE_PAD:
  160. goto done;
  161. case PAC_OPAQUE_TYPE_KEY:
  162. if (elen != EAP_FAST_PAC_KEY_LEN) {
  163. wpa_printf(MSG_DEBUG,
  164. "EAP-FAST: Invalid PAC-Key length %d",
  165. elen);
  166. os_free(buf);
  167. return -1;
  168. }
  169. pac_key = pos;
  170. wpa_hexdump_key(MSG_DEBUG, "EAP-FAST: PAC-Key from "
  171. "decrypted PAC-Opaque",
  172. pac_key, EAP_FAST_PAC_KEY_LEN);
  173. break;
  174. case PAC_OPAQUE_TYPE_LIFETIME:
  175. if (elen != 4) {
  176. wpa_printf(MSG_DEBUG, "EAP-FAST: Invalid "
  177. "PAC-Key lifetime length %d",
  178. elen);
  179. os_free(buf);
  180. return -1;
  181. }
  182. lifetime = WPA_GET_BE32(pos);
  183. break;
  184. case PAC_OPAQUE_TYPE_IDENTITY:
  185. identity = pos;
  186. identity_len = elen;
  187. break;
  188. }
  189. pos += elen;
  190. }
  191. done:
  192. if (pac_key == NULL) {
  193. wpa_printf(MSG_DEBUG, "EAP-FAST: No PAC-Key included in "
  194. "PAC-Opaque");
  195. os_free(buf);
  196. return -1;
  197. }
  198. if (identity) {
  199. wpa_hexdump_ascii(MSG_DEBUG, "EAP-FAST: Identity from "
  200. "PAC-Opaque", identity, identity_len);
  201. os_free(data->identity);
  202. data->identity = os_malloc(identity_len);
  203. if (data->identity) {
  204. os_memcpy(data->identity, identity, identity_len);
  205. data->identity_len = identity_len;
  206. }
  207. }
  208. if (os_get_time(&now) < 0 || lifetime <= 0 || now.sec > lifetime) {
  209. wpa_printf(MSG_DEBUG, "EAP-FAST: PAC-Key not valid anymore "
  210. "(lifetime=%ld now=%ld)", lifetime, now.sec);
  211. data->send_new_pac = 2;
  212. /*
  213. * Allow PAC to be used to allow a PAC update with some level
  214. * of server authentication (i.e., do not fall back to full TLS
  215. * handshake since we cannot be sure that the peer would be
  216. * able to validate server certificate now). However, reject
  217. * the authentication since the PAC was not valid anymore. Peer
  218. * can connect again with the newly provisioned PAC after this.
  219. */
  220. } else if (lifetime - now.sec < data->pac_key_refresh_time) {
  221. wpa_printf(MSG_DEBUG, "EAP-FAST: PAC-Key soft timeout; send "
  222. "an update if authentication succeeds");
  223. data->send_new_pac = 1;
  224. }
  225. eap_fast_derive_master_secret(pac_key, server_random, client_random,
  226. master_secret);
  227. os_free(buf);
  228. return 1;
  229. }
  230. static void eap_fast_derive_key_auth(struct eap_sm *sm,
  231. struct eap_fast_data *data)
  232. {
  233. u8 *sks;
  234. /* RFC 4851, Section 5.1:
  235. * Extra key material after TLS key_block: session_key_seed[40]
  236. */
  237. sks = eap_fast_derive_key(sm->ssl_ctx, data->ssl.conn,
  238. EAP_FAST_SKS_LEN);
  239. if (sks == NULL) {
  240. wpa_printf(MSG_DEBUG, "EAP-FAST: Failed to derive "
  241. "session_key_seed");
  242. return;
  243. }
  244. /*
  245. * RFC 4851, Section 5.2:
  246. * S-IMCK[0] = session_key_seed
  247. */
  248. wpa_hexdump_key(MSG_DEBUG,
  249. "EAP-FAST: session_key_seed (SKS = S-IMCK[0])",
  250. sks, EAP_FAST_SKS_LEN);
  251. data->simck_idx = 0;
  252. os_memcpy(data->simck, sks, EAP_FAST_SIMCK_LEN);
  253. os_free(sks);
  254. }
  255. static void eap_fast_derive_key_provisioning(struct eap_sm *sm,
  256. struct eap_fast_data *data)
  257. {
  258. os_free(data->key_block_p);
  259. data->key_block_p = (struct eap_fast_key_block_provisioning *)
  260. eap_fast_derive_key(sm->ssl_ctx, data->ssl.conn,
  261. sizeof(*data->key_block_p));
  262. if (data->key_block_p == NULL) {
  263. wpa_printf(MSG_DEBUG, "EAP-FAST: Failed to derive key block");
  264. return;
  265. }
  266. /*
  267. * RFC 4851, Section 5.2:
  268. * S-IMCK[0] = session_key_seed
  269. */
  270. wpa_hexdump_key(MSG_DEBUG,
  271. "EAP-FAST: session_key_seed (SKS = S-IMCK[0])",
  272. data->key_block_p->session_key_seed,
  273. sizeof(data->key_block_p->session_key_seed));
  274. data->simck_idx = 0;
  275. os_memcpy(data->simck, data->key_block_p->session_key_seed,
  276. EAP_FAST_SIMCK_LEN);
  277. wpa_hexdump_key(MSG_DEBUG, "EAP-FAST: server_challenge",
  278. data->key_block_p->server_challenge,
  279. sizeof(data->key_block_p->server_challenge));
  280. wpa_hexdump_key(MSG_DEBUG, "EAP-FAST: client_challenge",
  281. data->key_block_p->client_challenge,
  282. sizeof(data->key_block_p->client_challenge));
  283. }
  284. static int eap_fast_get_phase2_key(struct eap_sm *sm,
  285. struct eap_fast_data *data,
  286. u8 *isk, size_t isk_len)
  287. {
  288. u8 *key;
  289. size_t key_len;
  290. os_memset(isk, 0, isk_len);
  291. if (data->phase2_method == NULL || data->phase2_priv == NULL) {
  292. wpa_printf(MSG_DEBUG, "EAP-FAST: Phase 2 method not "
  293. "available");
  294. return -1;
  295. }
  296. if (data->phase2_method->getKey == NULL)
  297. return 0;
  298. if ((key = data->phase2_method->getKey(sm, data->phase2_priv,
  299. &key_len)) == NULL) {
  300. wpa_printf(MSG_DEBUG, "EAP-FAST: Could not get key material "
  301. "from Phase 2");
  302. return -1;
  303. }
  304. if (key_len > isk_len)
  305. key_len = isk_len;
  306. if (key_len == 32 &&
  307. data->phase2_method->vendor == EAP_VENDOR_IETF &&
  308. data->phase2_method->method == EAP_TYPE_MSCHAPV2) {
  309. /*
  310. * EAP-FAST uses reverse order for MS-MPPE keys when deriving
  311. * MSK from EAP-MSCHAPv2. Swap the keys here to get the correct
  312. * ISK for EAP-FAST cryptobinding.
  313. */
  314. os_memcpy(isk, key + 16, 16);
  315. os_memcpy(isk + 16, key, 16);
  316. } else
  317. os_memcpy(isk, key, key_len);
  318. os_free(key);
  319. return 0;
  320. }
  321. static int eap_fast_update_icmk(struct eap_sm *sm, struct eap_fast_data *data)
  322. {
  323. u8 isk[32], imck[60];
  324. wpa_printf(MSG_DEBUG, "EAP-FAST: Deriving ICMK[%d] (S-IMCK and CMK)",
  325. data->simck_idx + 1);
  326. /*
  327. * RFC 4851, Section 5.2:
  328. * IMCK[j] = T-PRF(S-IMCK[j-1], "Inner Methods Compound Keys",
  329. * MSK[j], 60)
  330. * S-IMCK[j] = first 40 octets of IMCK[j]
  331. * CMK[j] = last 20 octets of IMCK[j]
  332. */
  333. if (eap_fast_get_phase2_key(sm, data, isk, sizeof(isk)) < 0)
  334. return -1;
  335. wpa_hexdump_key(MSG_MSGDUMP, "EAP-FAST: ISK[j]", isk, sizeof(isk));
  336. sha1_t_prf(data->simck, EAP_FAST_SIMCK_LEN,
  337. "Inner Methods Compound Keys",
  338. isk, sizeof(isk), imck, sizeof(imck));
  339. data->simck_idx++;
  340. os_memcpy(data->simck, imck, EAP_FAST_SIMCK_LEN);
  341. wpa_hexdump_key(MSG_MSGDUMP, "EAP-FAST: S-IMCK[j]",
  342. data->simck, EAP_FAST_SIMCK_LEN);
  343. os_memcpy(data->cmk, imck + EAP_FAST_SIMCK_LEN, EAP_FAST_CMK_LEN);
  344. wpa_hexdump_key(MSG_MSGDUMP, "EAP-FAST: CMK[j]",
  345. data->cmk, EAP_FAST_CMK_LEN);
  346. return 0;
  347. }
  348. static void * eap_fast_init(struct eap_sm *sm)
  349. {
  350. struct eap_fast_data *data;
  351. u8 ciphers[7] = {
  352. TLS_CIPHER_ANON_DH_AES128_SHA,
  353. TLS_CIPHER_AES128_SHA,
  354. TLS_CIPHER_RSA_DHE_AES128_SHA,
  355. TLS_CIPHER_RC4_SHA,
  356. TLS_CIPHER_RSA_DHE_AES256_SHA,
  357. TLS_CIPHER_AES256_SHA,
  358. TLS_CIPHER_NONE
  359. };
  360. data = os_zalloc(sizeof(*data));
  361. if (data == NULL)
  362. return NULL;
  363. data->fast_version = EAP_FAST_VERSION;
  364. data->force_version = -1;
  365. if (sm->user && sm->user->force_version >= 0) {
  366. data->force_version = sm->user->force_version;
  367. wpa_printf(MSG_DEBUG, "EAP-FAST: forcing version %d",
  368. data->force_version);
  369. data->fast_version = data->force_version;
  370. }
  371. data->state = START;
  372. if (eap_server_tls_ssl_init(sm, &data->ssl, 0, EAP_TYPE_FAST)) {
  373. wpa_printf(MSG_INFO, "EAP-FAST: Failed to initialize SSL.");
  374. eap_fast_reset(sm, data);
  375. return NULL;
  376. }
  377. if (tls_connection_set_cipher_list(sm->ssl_ctx, data->ssl.conn,
  378. ciphers) < 0) {
  379. wpa_printf(MSG_INFO, "EAP-FAST: Failed to set TLS cipher "
  380. "suites");
  381. eap_fast_reset(sm, data);
  382. return NULL;
  383. }
  384. if (tls_connection_set_session_ticket_cb(sm->ssl_ctx, data->ssl.conn,
  385. eap_fast_session_ticket_cb,
  386. data) < 0) {
  387. wpa_printf(MSG_INFO, "EAP-FAST: Failed to set SessionTicket "
  388. "callback");
  389. eap_fast_reset(sm, data);
  390. return NULL;
  391. }
  392. if (sm->pac_opaque_encr_key == NULL) {
  393. wpa_printf(MSG_INFO, "EAP-FAST: No PAC-Opaque encryption key "
  394. "configured");
  395. eap_fast_reset(sm, data);
  396. return NULL;
  397. }
  398. os_memcpy(data->pac_opaque_encr, sm->pac_opaque_encr_key,
  399. sizeof(data->pac_opaque_encr));
  400. if (sm->eap_fast_a_id == NULL) {
  401. wpa_printf(MSG_INFO, "EAP-FAST: No A-ID configured");
  402. eap_fast_reset(sm, data);
  403. return NULL;
  404. }
  405. data->srv_id = os_memdup(sm->eap_fast_a_id, sm->eap_fast_a_id_len);
  406. if (data->srv_id == NULL) {
  407. eap_fast_reset(sm, data);
  408. return NULL;
  409. }
  410. data->srv_id_len = sm->eap_fast_a_id_len;
  411. if (sm->eap_fast_a_id_info == NULL) {
  412. wpa_printf(MSG_INFO, "EAP-FAST: No A-ID-Info configured");
  413. eap_fast_reset(sm, data);
  414. return NULL;
  415. }
  416. data->srv_id_info = os_strdup(sm->eap_fast_a_id_info);
  417. if (data->srv_id_info == NULL) {
  418. eap_fast_reset(sm, data);
  419. return NULL;
  420. }
  421. /* PAC-Key lifetime in seconds (hard limit) */
  422. data->pac_key_lifetime = sm->pac_key_lifetime;
  423. /*
  424. * PAC-Key refresh time in seconds (soft limit on remaining hard
  425. * limit). The server will generate a new PAC-Key when this number of
  426. * seconds (or fewer) of the lifetime remains.
  427. */
  428. data->pac_key_refresh_time = sm->pac_key_refresh_time;
  429. return data;
  430. }
  431. static void eap_fast_reset(struct eap_sm *sm, void *priv)
  432. {
  433. struct eap_fast_data *data = priv;
  434. if (data == NULL)
  435. return;
  436. if (data->phase2_priv && data->phase2_method)
  437. data->phase2_method->reset(sm, data->phase2_priv);
  438. eap_server_tls_ssl_deinit(sm, &data->ssl);
  439. os_free(data->srv_id);
  440. os_free(data->srv_id_info);
  441. os_free(data->key_block_p);
  442. wpabuf_free(data->pending_phase2_resp);
  443. os_free(data->identity);
  444. bin_clear_free(data, sizeof(*data));
  445. }
  446. static struct wpabuf * eap_fast_build_start(struct eap_sm *sm,
  447. struct eap_fast_data *data, u8 id)
  448. {
  449. struct wpabuf *req;
  450. req = eap_msg_alloc(EAP_VENDOR_IETF, EAP_TYPE_FAST,
  451. 1 + sizeof(struct pac_tlv_hdr) + data->srv_id_len,
  452. EAP_CODE_REQUEST, id);
  453. if (req == NULL) {
  454. wpa_printf(MSG_ERROR, "EAP-FAST: Failed to allocate memory for"
  455. " request");
  456. eap_fast_state(data, FAILURE);
  457. return NULL;
  458. }
  459. wpabuf_put_u8(req, EAP_TLS_FLAGS_START | data->fast_version);
  460. /* RFC 4851, 4.1.1. Authority ID Data */
  461. eap_fast_put_tlv(req, PAC_TYPE_A_ID, data->srv_id, data->srv_id_len);
  462. eap_fast_state(data, PHASE1);
  463. return req;
  464. }
  465. static int eap_fast_phase1_done(struct eap_sm *sm, struct eap_fast_data *data)
  466. {
  467. char cipher[64];
  468. wpa_printf(MSG_DEBUG, "EAP-FAST: Phase1 done, starting Phase2");
  469. if (tls_get_cipher(sm->ssl_ctx, data->ssl.conn, cipher, sizeof(cipher))
  470. < 0) {
  471. wpa_printf(MSG_DEBUG, "EAP-FAST: Failed to get cipher "
  472. "information");
  473. eap_fast_state(data, FAILURE);
  474. return -1;
  475. }
  476. data->anon_provisioning = os_strstr(cipher, "ADH") != NULL;
  477. if (data->anon_provisioning) {
  478. wpa_printf(MSG_DEBUG, "EAP-FAST: Anonymous provisioning");
  479. eap_fast_derive_key_provisioning(sm, data);
  480. } else
  481. eap_fast_derive_key_auth(sm, data);
  482. eap_fast_state(data, PHASE2_START);
  483. return 0;
  484. }
  485. static struct wpabuf * eap_fast_build_phase2_req(struct eap_sm *sm,
  486. struct eap_fast_data *data,
  487. u8 id)
  488. {
  489. struct wpabuf *req;
  490. if (data->phase2_priv == NULL) {
  491. wpa_printf(MSG_DEBUG, "EAP-FAST: Phase 2 method not "
  492. "initialized");
  493. return NULL;
  494. }
  495. req = data->phase2_method->buildReq(sm, data->phase2_priv, id);
  496. if (req == NULL)
  497. return NULL;
  498. wpa_hexdump_buf_key(MSG_MSGDUMP, "EAP-FAST: Phase 2 EAP-Request", req);
  499. return eap_fast_tlv_eap_payload(req);
  500. }
  501. static struct wpabuf * eap_fast_build_crypto_binding(
  502. struct eap_sm *sm, struct eap_fast_data *data)
  503. {
  504. struct wpabuf *buf;
  505. struct eap_tlv_result_tlv *result;
  506. struct eap_tlv_crypto_binding_tlv *binding;
  507. buf = wpabuf_alloc(2 * sizeof(*result) + sizeof(*binding));
  508. if (buf == NULL)
  509. return NULL;
  510. if (data->send_new_pac || data->anon_provisioning ||
  511. data->phase2_method)
  512. data->final_result = 0;
  513. else
  514. data->final_result = 1;
  515. if (!data->final_result || data->eap_seq > 1) {
  516. /* Intermediate-Result */
  517. wpa_printf(MSG_DEBUG, "EAP-FAST: Add Intermediate-Result TLV "
  518. "(status=SUCCESS)");
  519. result = wpabuf_put(buf, sizeof(*result));
  520. result->tlv_type = host_to_be16(
  521. EAP_TLV_TYPE_MANDATORY |
  522. EAP_TLV_INTERMEDIATE_RESULT_TLV);
  523. result->length = host_to_be16(2);
  524. result->status = host_to_be16(EAP_TLV_RESULT_SUCCESS);
  525. }
  526. if (data->final_result) {
  527. /* Result TLV */
  528. wpa_printf(MSG_DEBUG, "EAP-FAST: Add Result TLV "
  529. "(status=SUCCESS)");
  530. result = wpabuf_put(buf, sizeof(*result));
  531. result->tlv_type = host_to_be16(EAP_TLV_TYPE_MANDATORY |
  532. EAP_TLV_RESULT_TLV);
  533. result->length = host_to_be16(2);
  534. result->status = host_to_be16(EAP_TLV_RESULT_SUCCESS);
  535. }
  536. /* Crypto-Binding TLV */
  537. binding = wpabuf_put(buf, sizeof(*binding));
  538. binding->tlv_type = host_to_be16(EAP_TLV_TYPE_MANDATORY |
  539. EAP_TLV_CRYPTO_BINDING_TLV);
  540. binding->length = host_to_be16(sizeof(*binding) -
  541. sizeof(struct eap_tlv_hdr));
  542. binding->version = EAP_FAST_VERSION;
  543. binding->received_version = data->peer_version;
  544. binding->subtype = EAP_TLV_CRYPTO_BINDING_SUBTYPE_REQUEST;
  545. if (random_get_bytes(binding->nonce, sizeof(binding->nonce)) < 0) {
  546. wpabuf_free(buf);
  547. return NULL;
  548. }
  549. /*
  550. * RFC 4851, Section 4.2.8:
  551. * The nonce in a request MUST have its least significant bit set to 0.
  552. */
  553. binding->nonce[sizeof(binding->nonce) - 1] &= ~0x01;
  554. os_memcpy(data->crypto_binding_nonce, binding->nonce,
  555. sizeof(binding->nonce));
  556. /*
  557. * RFC 4851, Section 5.3:
  558. * CMK = CMK[j]
  559. * Compound-MAC = HMAC-SHA1( CMK, Crypto-Binding TLV )
  560. */
  561. hmac_sha1(data->cmk, EAP_FAST_CMK_LEN,
  562. (u8 *) binding, sizeof(*binding),
  563. binding->compound_mac);
  564. wpa_printf(MSG_DEBUG, "EAP-FAST: Add Crypto-Binding TLV: Version %d "
  565. "Received Version %d SubType %d",
  566. binding->version, binding->received_version,
  567. binding->subtype);
  568. wpa_hexdump(MSG_MSGDUMP, "EAP-FAST: NONCE",
  569. binding->nonce, sizeof(binding->nonce));
  570. wpa_hexdump(MSG_MSGDUMP, "EAP-FAST: Compound MAC",
  571. binding->compound_mac, sizeof(binding->compound_mac));
  572. return buf;
  573. }
  574. static struct wpabuf * eap_fast_build_pac(struct eap_sm *sm,
  575. struct eap_fast_data *data)
  576. {
  577. u8 pac_key[EAP_FAST_PAC_KEY_LEN];
  578. u8 *pac_buf, *pac_opaque;
  579. struct wpabuf *buf;
  580. u8 *pos;
  581. size_t buf_len, srv_id_info_len, pac_len;
  582. struct eap_tlv_hdr *pac_tlv;
  583. struct pac_tlv_hdr *pac_info;
  584. struct eap_tlv_result_tlv *result;
  585. struct os_time now;
  586. if (random_get_bytes(pac_key, EAP_FAST_PAC_KEY_LEN) < 0 ||
  587. os_get_time(&now) < 0)
  588. return NULL;
  589. wpa_hexdump_key(MSG_DEBUG, "EAP-FAST: Generated PAC-Key",
  590. pac_key, EAP_FAST_PAC_KEY_LEN);
  591. pac_len = (2 + EAP_FAST_PAC_KEY_LEN) + (2 + 4) +
  592. (2 + sm->identity_len) + 8;
  593. pac_buf = os_malloc(pac_len);
  594. if (pac_buf == NULL)
  595. return NULL;
  596. srv_id_info_len = os_strlen(data->srv_id_info);
  597. pos = pac_buf;
  598. *pos++ = PAC_OPAQUE_TYPE_KEY;
  599. *pos++ = EAP_FAST_PAC_KEY_LEN;
  600. os_memcpy(pos, pac_key, EAP_FAST_PAC_KEY_LEN);
  601. pos += EAP_FAST_PAC_KEY_LEN;
  602. *pos++ = PAC_OPAQUE_TYPE_LIFETIME;
  603. *pos++ = 4;
  604. WPA_PUT_BE32(pos, now.sec + data->pac_key_lifetime);
  605. pos += 4;
  606. if (sm->identity) {
  607. *pos++ = PAC_OPAQUE_TYPE_IDENTITY;
  608. *pos++ = sm->identity_len;
  609. os_memcpy(pos, sm->identity, sm->identity_len);
  610. pos += sm->identity_len;
  611. }
  612. pac_len = pos - pac_buf;
  613. while (pac_len % 8) {
  614. *pos++ = PAC_OPAQUE_TYPE_PAD;
  615. pac_len++;
  616. }
  617. pac_opaque = os_malloc(pac_len + 8);
  618. if (pac_opaque == NULL) {
  619. os_free(pac_buf);
  620. return NULL;
  621. }
  622. if (aes_wrap(data->pac_opaque_encr, sizeof(data->pac_opaque_encr),
  623. pac_len / 8, pac_buf, pac_opaque) < 0) {
  624. os_free(pac_buf);
  625. os_free(pac_opaque);
  626. return NULL;
  627. }
  628. os_free(pac_buf);
  629. pac_len += 8;
  630. wpa_hexdump(MSG_DEBUG, "EAP-FAST: PAC-Opaque",
  631. pac_opaque, pac_len);
  632. buf_len = sizeof(*pac_tlv) +
  633. sizeof(struct pac_tlv_hdr) + EAP_FAST_PAC_KEY_LEN +
  634. sizeof(struct pac_tlv_hdr) + pac_len +
  635. data->srv_id_len + srv_id_info_len + 100 + sizeof(*result);
  636. buf = wpabuf_alloc(buf_len);
  637. if (buf == NULL) {
  638. os_free(pac_opaque);
  639. return NULL;
  640. }
  641. /* Result TLV */
  642. wpa_printf(MSG_DEBUG, "EAP-FAST: Add Result TLV (status=SUCCESS)");
  643. result = wpabuf_put(buf, sizeof(*result));
  644. WPA_PUT_BE16((u8 *) &result->tlv_type,
  645. EAP_TLV_TYPE_MANDATORY | EAP_TLV_RESULT_TLV);
  646. WPA_PUT_BE16((u8 *) &result->length, 2);
  647. WPA_PUT_BE16((u8 *) &result->status, EAP_TLV_RESULT_SUCCESS);
  648. /* PAC TLV */
  649. wpa_printf(MSG_DEBUG, "EAP-FAST: Add PAC TLV");
  650. pac_tlv = wpabuf_put(buf, sizeof(*pac_tlv));
  651. pac_tlv->tlv_type = host_to_be16(EAP_TLV_TYPE_MANDATORY |
  652. EAP_TLV_PAC_TLV);
  653. /* PAC-Key */
  654. eap_fast_put_tlv(buf, PAC_TYPE_PAC_KEY, pac_key, EAP_FAST_PAC_KEY_LEN);
  655. /* PAC-Opaque */
  656. eap_fast_put_tlv(buf, PAC_TYPE_PAC_OPAQUE, pac_opaque, pac_len);
  657. os_free(pac_opaque);
  658. /* PAC-Info */
  659. pac_info = wpabuf_put(buf, sizeof(*pac_info));
  660. pac_info->type = host_to_be16(PAC_TYPE_PAC_INFO);
  661. /* PAC-Lifetime (inside PAC-Info) */
  662. eap_fast_put_tlv_hdr(buf, PAC_TYPE_CRED_LIFETIME, 4);
  663. wpabuf_put_be32(buf, now.sec + data->pac_key_lifetime);
  664. /* A-ID (inside PAC-Info) */
  665. eap_fast_put_tlv(buf, PAC_TYPE_A_ID, data->srv_id, data->srv_id_len);
  666. /* Note: headers may be misaligned after A-ID */
  667. if (sm->identity) {
  668. eap_fast_put_tlv(buf, PAC_TYPE_I_ID, sm->identity,
  669. sm->identity_len);
  670. }
  671. /* A-ID-Info (inside PAC-Info) */
  672. eap_fast_put_tlv(buf, PAC_TYPE_A_ID_INFO, data->srv_id_info,
  673. srv_id_info_len);
  674. /* PAC-Type (inside PAC-Info) */
  675. eap_fast_put_tlv_hdr(buf, PAC_TYPE_PAC_TYPE, 2);
  676. wpabuf_put_be16(buf, PAC_TYPE_TUNNEL_PAC);
  677. /* Update PAC-Info and PAC TLV Length fields */
  678. pos = wpabuf_put(buf, 0);
  679. pac_info->len = host_to_be16(pos - (u8 *) (pac_info + 1));
  680. pac_tlv->length = host_to_be16(pos - (u8 *) (pac_tlv + 1));
  681. return buf;
  682. }
  683. static int eap_fast_encrypt_phase2(struct eap_sm *sm,
  684. struct eap_fast_data *data,
  685. struct wpabuf *plain, int piggyback)
  686. {
  687. struct wpabuf *encr;
  688. wpa_hexdump_buf_key(MSG_DEBUG, "EAP-FAST: Encrypting Phase 2 TLVs",
  689. plain);
  690. encr = eap_server_tls_encrypt(sm, &data->ssl, plain);
  691. wpabuf_free(plain);
  692. if (!encr)
  693. return -1;
  694. if (data->ssl.tls_out && piggyback) {
  695. wpa_printf(MSG_DEBUG, "EAP-FAST: Piggyback Phase 2 data "
  696. "(len=%d) with last Phase 1 Message (len=%d "
  697. "used=%d)",
  698. (int) wpabuf_len(encr),
  699. (int) wpabuf_len(data->ssl.tls_out),
  700. (int) data->ssl.tls_out_pos);
  701. if (wpabuf_resize(&data->ssl.tls_out, wpabuf_len(encr)) < 0) {
  702. wpa_printf(MSG_WARNING, "EAP-FAST: Failed to resize "
  703. "output buffer");
  704. wpabuf_free(encr);
  705. return -1;
  706. }
  707. wpabuf_put_buf(data->ssl.tls_out, encr);
  708. wpabuf_free(encr);
  709. } else {
  710. wpabuf_free(data->ssl.tls_out);
  711. data->ssl.tls_out_pos = 0;
  712. data->ssl.tls_out = encr;
  713. }
  714. return 0;
  715. }
  716. static struct wpabuf * eap_fast_buildReq(struct eap_sm *sm, void *priv, u8 id)
  717. {
  718. struct eap_fast_data *data = priv;
  719. struct wpabuf *req = NULL;
  720. int piggyback = 0;
  721. if (data->ssl.state == FRAG_ACK) {
  722. return eap_server_tls_build_ack(id, EAP_TYPE_FAST,
  723. data->fast_version);
  724. }
  725. if (data->ssl.state == WAIT_FRAG_ACK) {
  726. return eap_server_tls_build_msg(&data->ssl, EAP_TYPE_FAST,
  727. data->fast_version, id);
  728. }
  729. switch (data->state) {
  730. case START:
  731. return eap_fast_build_start(sm, data, id);
  732. case PHASE1:
  733. if (tls_connection_established(sm->ssl_ctx, data->ssl.conn)) {
  734. if (eap_fast_phase1_done(sm, data) < 0)
  735. return NULL;
  736. if (data->state == PHASE2_START) {
  737. /*
  738. * Try to generate Phase 2 data to piggyback
  739. * with the end of Phase 1 to avoid extra
  740. * roundtrip.
  741. */
  742. wpa_printf(MSG_DEBUG, "EAP-FAST: Try to start "
  743. "Phase 2");
  744. if (eap_fast_process_phase2_start(sm, data))
  745. break;
  746. req = eap_fast_build_phase2_req(sm, data, id);
  747. piggyback = 1;
  748. }
  749. }
  750. break;
  751. case PHASE2_ID:
  752. case PHASE2_METHOD:
  753. req = eap_fast_build_phase2_req(sm, data, id);
  754. break;
  755. case CRYPTO_BINDING:
  756. req = eap_fast_build_crypto_binding(sm, data);
  757. if (data->phase2_method) {
  758. /*
  759. * Include the start of the next EAP method in the
  760. * sequence in the same message with Crypto-Binding to
  761. * save a round-trip.
  762. */
  763. struct wpabuf *eap;
  764. eap = eap_fast_build_phase2_req(sm, data, id);
  765. req = wpabuf_concat(req, eap);
  766. eap_fast_state(data, PHASE2_METHOD);
  767. }
  768. break;
  769. case REQUEST_PAC:
  770. req = eap_fast_build_pac(sm, data);
  771. break;
  772. default:
  773. wpa_printf(MSG_DEBUG, "EAP-FAST: %s - unexpected state %d",
  774. __func__, data->state);
  775. return NULL;
  776. }
  777. if (req &&
  778. eap_fast_encrypt_phase2(sm, data, req, piggyback) < 0)
  779. return NULL;
  780. return eap_server_tls_build_msg(&data->ssl, EAP_TYPE_FAST,
  781. data->fast_version, id);
  782. }
  783. static Boolean eap_fast_check(struct eap_sm *sm, void *priv,
  784. struct wpabuf *respData)
  785. {
  786. const u8 *pos;
  787. size_t len;
  788. pos = eap_hdr_validate(EAP_VENDOR_IETF, EAP_TYPE_FAST, respData, &len);
  789. if (pos == NULL || len < 1) {
  790. wpa_printf(MSG_INFO, "EAP-FAST: Invalid frame");
  791. return TRUE;
  792. }
  793. return FALSE;
  794. }
  795. static int eap_fast_phase2_init(struct eap_sm *sm, struct eap_fast_data *data,
  796. EapType eap_type)
  797. {
  798. if (data->phase2_priv && data->phase2_method) {
  799. data->phase2_method->reset(sm, data->phase2_priv);
  800. data->phase2_method = NULL;
  801. data->phase2_priv = NULL;
  802. }
  803. data->phase2_method = eap_server_get_eap_method(EAP_VENDOR_IETF,
  804. eap_type);
  805. if (!data->phase2_method)
  806. return -1;
  807. if (data->key_block_p) {
  808. sm->auth_challenge = data->key_block_p->server_challenge;
  809. sm->peer_challenge = data->key_block_p->client_challenge;
  810. }
  811. sm->init_phase2 = 1;
  812. data->phase2_priv = data->phase2_method->init(sm);
  813. sm->init_phase2 = 0;
  814. sm->auth_challenge = NULL;
  815. sm->peer_challenge = NULL;
  816. return data->phase2_priv == NULL ? -1 : 0;
  817. }
  818. static void eap_fast_process_phase2_response(struct eap_sm *sm,
  819. struct eap_fast_data *data,
  820. u8 *in_data, size_t in_len)
  821. {
  822. u8 next_type = EAP_TYPE_NONE;
  823. struct eap_hdr *hdr;
  824. u8 *pos;
  825. size_t left;
  826. struct wpabuf buf;
  827. const struct eap_method *m = data->phase2_method;
  828. void *priv = data->phase2_priv;
  829. if (priv == NULL) {
  830. wpa_printf(MSG_DEBUG, "EAP-FAST: %s - Phase2 not "
  831. "initialized?!", __func__);
  832. return;
  833. }
  834. hdr = (struct eap_hdr *) in_data;
  835. pos = (u8 *) (hdr + 1);
  836. if (in_len > sizeof(*hdr) && *pos == EAP_TYPE_NAK) {
  837. left = in_len - sizeof(*hdr);
  838. wpa_hexdump(MSG_DEBUG, "EAP-FAST: Phase2 type Nak'ed; "
  839. "allowed types", pos + 1, left - 1);
  840. #ifdef EAP_SERVER_TNC
  841. if (m && m->vendor == EAP_VENDOR_IETF &&
  842. m->method == EAP_TYPE_TNC) {
  843. wpa_printf(MSG_DEBUG, "EAP-FAST: Peer Nak'ed required "
  844. "TNC negotiation");
  845. next_type = eap_fast_req_failure(sm, data);
  846. eap_fast_phase2_init(sm, data, next_type);
  847. return;
  848. }
  849. #endif /* EAP_SERVER_TNC */
  850. eap_sm_process_nak(sm, pos + 1, left - 1);
  851. if (sm->user && sm->user_eap_method_index < EAP_MAX_METHODS &&
  852. sm->user->methods[sm->user_eap_method_index].method !=
  853. EAP_TYPE_NONE) {
  854. next_type = sm->user->methods[
  855. sm->user_eap_method_index++].method;
  856. wpa_printf(MSG_DEBUG, "EAP-FAST: try EAP type %d",
  857. next_type);
  858. } else {
  859. next_type = eap_fast_req_failure(sm, data);
  860. }
  861. eap_fast_phase2_init(sm, data, next_type);
  862. return;
  863. }
  864. wpabuf_set(&buf, in_data, in_len);
  865. if (m->check(sm, priv, &buf)) {
  866. wpa_printf(MSG_DEBUG, "EAP-FAST: Phase2 check() asked to "
  867. "ignore the packet");
  868. eap_fast_req_failure(sm, data);
  869. return;
  870. }
  871. m->process(sm, priv, &buf);
  872. if (!m->isDone(sm, priv))
  873. return;
  874. if (!m->isSuccess(sm, priv)) {
  875. wpa_printf(MSG_DEBUG, "EAP-FAST: Phase2 method failed");
  876. next_type = eap_fast_req_failure(sm, data);
  877. eap_fast_phase2_init(sm, data, next_type);
  878. return;
  879. }
  880. switch (data->state) {
  881. case PHASE2_ID:
  882. if (eap_user_get(sm, sm->identity, sm->identity_len, 1) != 0) {
  883. wpa_hexdump_ascii(MSG_DEBUG, "EAP-FAST: Phase2 "
  884. "Identity not found in the user "
  885. "database",
  886. sm->identity, sm->identity_len);
  887. next_type = eap_fast_req_failure(sm, data);
  888. break;
  889. }
  890. eap_fast_state(data, PHASE2_METHOD);
  891. if (data->anon_provisioning) {
  892. /*
  893. * Only EAP-MSCHAPv2 is allowed for anonymous
  894. * provisioning.
  895. */
  896. next_type = EAP_TYPE_MSCHAPV2;
  897. sm->user_eap_method_index = 0;
  898. } else {
  899. next_type = sm->user->methods[0].method;
  900. sm->user_eap_method_index = 1;
  901. }
  902. wpa_printf(MSG_DEBUG, "EAP-FAST: try EAP type %d", next_type);
  903. break;
  904. case PHASE2_METHOD:
  905. case CRYPTO_BINDING:
  906. eap_fast_update_icmk(sm, data);
  907. eap_fast_state(data, CRYPTO_BINDING);
  908. data->eap_seq++;
  909. next_type = EAP_TYPE_NONE;
  910. #ifdef EAP_SERVER_TNC
  911. if (sm->tnc && !data->tnc_started) {
  912. wpa_printf(MSG_DEBUG, "EAP-FAST: Initialize TNC");
  913. next_type = EAP_TYPE_TNC;
  914. data->tnc_started = 1;
  915. }
  916. #endif /* EAP_SERVER_TNC */
  917. break;
  918. case FAILURE:
  919. break;
  920. default:
  921. wpa_printf(MSG_DEBUG, "EAP-FAST: %s - unexpected state %d",
  922. __func__, data->state);
  923. break;
  924. }
  925. eap_fast_phase2_init(sm, data, next_type);
  926. }
  927. static void eap_fast_process_phase2_eap(struct eap_sm *sm,
  928. struct eap_fast_data *data,
  929. u8 *in_data, size_t in_len)
  930. {
  931. struct eap_hdr *hdr;
  932. size_t len;
  933. hdr = (struct eap_hdr *) in_data;
  934. if (in_len < (int) sizeof(*hdr)) {
  935. wpa_printf(MSG_INFO, "EAP-FAST: Too short Phase 2 "
  936. "EAP frame (len=%lu)", (unsigned long) in_len);
  937. eap_fast_req_failure(sm, data);
  938. return;
  939. }
  940. len = be_to_host16(hdr->length);
  941. if (len > in_len) {
  942. wpa_printf(MSG_INFO, "EAP-FAST: Length mismatch in "
  943. "Phase 2 EAP frame (len=%lu hdr->length=%lu)",
  944. (unsigned long) in_len, (unsigned long) len);
  945. eap_fast_req_failure(sm, data);
  946. return;
  947. }
  948. wpa_printf(MSG_DEBUG, "EAP-FAST: Received Phase 2: code=%d "
  949. "identifier=%d length=%lu", hdr->code, hdr->identifier,
  950. (unsigned long) len);
  951. switch (hdr->code) {
  952. case EAP_CODE_RESPONSE:
  953. eap_fast_process_phase2_response(sm, data, (u8 *) hdr, len);
  954. break;
  955. default:
  956. wpa_printf(MSG_INFO, "EAP-FAST: Unexpected code=%d in "
  957. "Phase 2 EAP header", hdr->code);
  958. break;
  959. }
  960. }
  961. static int eap_fast_parse_tlvs(struct wpabuf *data,
  962. struct eap_fast_tlv_parse *tlv)
  963. {
  964. int mandatory, tlv_type, res;
  965. size_t len;
  966. u8 *pos, *end;
  967. os_memset(tlv, 0, sizeof(*tlv));
  968. pos = wpabuf_mhead(data);
  969. end = pos + wpabuf_len(data);
  970. while (end - pos > 4) {
  971. mandatory = pos[0] & 0x80;
  972. tlv_type = WPA_GET_BE16(pos) & 0x3fff;
  973. pos += 2;
  974. len = WPA_GET_BE16(pos);
  975. pos += 2;
  976. if (len > (size_t) (end - pos)) {
  977. wpa_printf(MSG_INFO, "EAP-FAST: TLV overflow");
  978. return -1;
  979. }
  980. wpa_printf(MSG_DEBUG, "EAP-FAST: Received Phase 2: "
  981. "TLV type %d length %u%s",
  982. tlv_type, (unsigned int) len,
  983. mandatory ? " (mandatory)" : "");
  984. res = eap_fast_parse_tlv(tlv, tlv_type, pos, len);
  985. if (res == -2)
  986. break;
  987. if (res < 0) {
  988. if (mandatory) {
  989. wpa_printf(MSG_DEBUG, "EAP-FAST: Nak unknown "
  990. "mandatory TLV type %d", tlv_type);
  991. /* TODO: generate Nak TLV */
  992. break;
  993. } else {
  994. wpa_printf(MSG_DEBUG, "EAP-FAST: Ignored "
  995. "unknown optional TLV type %d",
  996. tlv_type);
  997. }
  998. }
  999. pos += len;
  1000. }
  1001. return 0;
  1002. }
  1003. static int eap_fast_validate_crypto_binding(
  1004. struct eap_fast_data *data, struct eap_tlv_crypto_binding_tlv *b,
  1005. size_t bind_len)
  1006. {
  1007. u8 cmac[SHA1_MAC_LEN];
  1008. wpa_printf(MSG_DEBUG, "EAP-FAST: Reply Crypto-Binding TLV: "
  1009. "Version %d Received Version %d SubType %d",
  1010. b->version, b->received_version, b->subtype);
  1011. wpa_hexdump(MSG_MSGDUMP, "EAP-FAST: NONCE",
  1012. b->nonce, sizeof(b->nonce));
  1013. wpa_hexdump(MSG_MSGDUMP, "EAP-FAST: Compound MAC",
  1014. b->compound_mac, sizeof(b->compound_mac));
  1015. if (b->version != EAP_FAST_VERSION ||
  1016. b->received_version != EAP_FAST_VERSION) {
  1017. wpa_printf(MSG_DEBUG, "EAP-FAST: Unexpected version "
  1018. "in Crypto-Binding: version %d "
  1019. "received_version %d", b->version,
  1020. b->received_version);
  1021. return -1;
  1022. }
  1023. if (b->subtype != EAP_TLV_CRYPTO_BINDING_SUBTYPE_RESPONSE) {
  1024. wpa_printf(MSG_DEBUG, "EAP-FAST: Unexpected subtype in "
  1025. "Crypto-Binding: %d", b->subtype);
  1026. return -1;
  1027. }
  1028. if (os_memcmp_const(data->crypto_binding_nonce, b->nonce, 31) != 0 ||
  1029. (data->crypto_binding_nonce[31] | 1) != b->nonce[31]) {
  1030. wpa_printf(MSG_DEBUG, "EAP-FAST: Invalid nonce in "
  1031. "Crypto-Binding");
  1032. return -1;
  1033. }
  1034. os_memcpy(cmac, b->compound_mac, sizeof(cmac));
  1035. os_memset(b->compound_mac, 0, sizeof(cmac));
  1036. wpa_hexdump(MSG_MSGDUMP, "EAP-FAST: Crypto-Binding TLV for "
  1037. "Compound MAC calculation",
  1038. (u8 *) b, bind_len);
  1039. hmac_sha1(data->cmk, EAP_FAST_CMK_LEN, (u8 *) b, bind_len,
  1040. b->compound_mac);
  1041. if (os_memcmp_const(cmac, b->compound_mac, sizeof(cmac)) != 0) {
  1042. wpa_hexdump(MSG_MSGDUMP,
  1043. "EAP-FAST: Calculated Compound MAC",
  1044. b->compound_mac, sizeof(cmac));
  1045. wpa_printf(MSG_INFO, "EAP-FAST: Compound MAC did not "
  1046. "match");
  1047. return -1;
  1048. }
  1049. return 0;
  1050. }
  1051. static int eap_fast_pac_type(u8 *pac, size_t len, u16 type)
  1052. {
  1053. struct eap_tlv_pac_type_tlv *tlv;
  1054. if (pac == NULL || len != sizeof(*tlv))
  1055. return 0;
  1056. tlv = (struct eap_tlv_pac_type_tlv *) pac;
  1057. return be_to_host16(tlv->tlv_type) == PAC_TYPE_PAC_TYPE &&
  1058. be_to_host16(tlv->length) == 2 &&
  1059. be_to_host16(tlv->pac_type) == type;
  1060. }
  1061. static void eap_fast_process_phase2_tlvs(struct eap_sm *sm,
  1062. struct eap_fast_data *data,
  1063. struct wpabuf *in_data)
  1064. {
  1065. struct eap_fast_tlv_parse tlv;
  1066. int check_crypto_binding = data->state == CRYPTO_BINDING;
  1067. if (eap_fast_parse_tlvs(in_data, &tlv) < 0) {
  1068. wpa_printf(MSG_DEBUG, "EAP-FAST: Failed to parse received "
  1069. "Phase 2 TLVs");
  1070. return;
  1071. }
  1072. if (tlv.result == EAP_TLV_RESULT_FAILURE) {
  1073. wpa_printf(MSG_DEBUG, "EAP-FAST: Result TLV indicated "
  1074. "failure");
  1075. eap_fast_state(data, FAILURE);
  1076. return;
  1077. }
  1078. if (data->state == REQUEST_PAC) {
  1079. u16 type, len, res;
  1080. if (tlv.pac == NULL || tlv.pac_len < 6) {
  1081. wpa_printf(MSG_DEBUG, "EAP-FAST: No PAC "
  1082. "Acknowledgement received");
  1083. eap_fast_state(data, FAILURE);
  1084. return;
  1085. }
  1086. type = WPA_GET_BE16(tlv.pac);
  1087. len = WPA_GET_BE16(tlv.pac + 2);
  1088. res = WPA_GET_BE16(tlv.pac + 4);
  1089. if (type != PAC_TYPE_PAC_ACKNOWLEDGEMENT || len != 2 ||
  1090. res != EAP_TLV_RESULT_SUCCESS) {
  1091. wpa_printf(MSG_DEBUG, "EAP-FAST: PAC TLV did not "
  1092. "contain acknowledgement");
  1093. eap_fast_state(data, FAILURE);
  1094. return;
  1095. }
  1096. wpa_printf(MSG_DEBUG, "EAP-FAST: PAC-Acknowledgement received "
  1097. "- PAC provisioning succeeded");
  1098. eap_fast_state(data, (data->anon_provisioning ||
  1099. data->send_new_pac == 2) ?
  1100. FAILURE : SUCCESS);
  1101. return;
  1102. }
  1103. if (check_crypto_binding) {
  1104. if (tlv.crypto_binding == NULL) {
  1105. wpa_printf(MSG_DEBUG, "EAP-FAST: No Crypto-Binding "
  1106. "TLV received");
  1107. eap_fast_state(data, FAILURE);
  1108. return;
  1109. }
  1110. if (data->final_result &&
  1111. tlv.result != EAP_TLV_RESULT_SUCCESS) {
  1112. wpa_printf(MSG_DEBUG, "EAP-FAST: Crypto-Binding TLV "
  1113. "without Success Result");
  1114. eap_fast_state(data, FAILURE);
  1115. return;
  1116. }
  1117. if (!data->final_result &&
  1118. tlv.iresult != EAP_TLV_RESULT_SUCCESS) {
  1119. wpa_printf(MSG_DEBUG, "EAP-FAST: Crypto-Binding TLV "
  1120. "without intermediate Success Result");
  1121. eap_fast_state(data, FAILURE);
  1122. return;
  1123. }
  1124. if (eap_fast_validate_crypto_binding(data, tlv.crypto_binding,
  1125. tlv.crypto_binding_len)) {
  1126. eap_fast_state(data, FAILURE);
  1127. return;
  1128. }
  1129. wpa_printf(MSG_DEBUG, "EAP-FAST: Valid Crypto-Binding TLV "
  1130. "received");
  1131. if (data->final_result) {
  1132. wpa_printf(MSG_DEBUG, "EAP-FAST: Authentication "
  1133. "completed successfully");
  1134. }
  1135. if (data->anon_provisioning &&
  1136. sm->eap_fast_prov != ANON_PROV &&
  1137. sm->eap_fast_prov != BOTH_PROV) {
  1138. wpa_printf(MSG_DEBUG, "EAP-FAST: Client is trying to "
  1139. "use unauthenticated provisioning which is "
  1140. "disabled");
  1141. eap_fast_state(data, FAILURE);
  1142. return;
  1143. }
  1144. if (sm->eap_fast_prov != AUTH_PROV &&
  1145. sm->eap_fast_prov != BOTH_PROV &&
  1146. tlv.request_action == EAP_TLV_ACTION_PROCESS_TLV &&
  1147. eap_fast_pac_type(tlv.pac, tlv.pac_len,
  1148. PAC_TYPE_TUNNEL_PAC)) {
  1149. wpa_printf(MSG_DEBUG, "EAP-FAST: Client is trying to "
  1150. "use authenticated provisioning which is "
  1151. "disabled");
  1152. eap_fast_state(data, FAILURE);
  1153. return;
  1154. }
  1155. if (data->anon_provisioning ||
  1156. (tlv.request_action == EAP_TLV_ACTION_PROCESS_TLV &&
  1157. eap_fast_pac_type(tlv.pac, tlv.pac_len,
  1158. PAC_TYPE_TUNNEL_PAC))) {
  1159. wpa_printf(MSG_DEBUG, "EAP-FAST: Requested a new "
  1160. "Tunnel PAC");
  1161. eap_fast_state(data, REQUEST_PAC);
  1162. } else if (data->send_new_pac) {
  1163. wpa_printf(MSG_DEBUG, "EAP-FAST: Server triggered "
  1164. "re-keying of Tunnel PAC");
  1165. eap_fast_state(data, REQUEST_PAC);
  1166. } else if (data->final_result)
  1167. eap_fast_state(data, SUCCESS);
  1168. }
  1169. if (tlv.eap_payload_tlv) {
  1170. eap_fast_process_phase2_eap(sm, data, tlv.eap_payload_tlv,
  1171. tlv.eap_payload_tlv_len);
  1172. }
  1173. }
  1174. static void eap_fast_process_phase2(struct eap_sm *sm,
  1175. struct eap_fast_data *data,
  1176. struct wpabuf *in_buf)
  1177. {
  1178. struct wpabuf *in_decrypted;
  1179. wpa_printf(MSG_DEBUG, "EAP-FAST: Received %lu bytes encrypted data for"
  1180. " Phase 2", (unsigned long) wpabuf_len(in_buf));
  1181. if (data->pending_phase2_resp) {
  1182. wpa_printf(MSG_DEBUG, "EAP-PEAP: Pending Phase 2 response - "
  1183. "skip decryption and use old data");
  1184. eap_fast_process_phase2_tlvs(sm, data,
  1185. data->pending_phase2_resp);
  1186. wpabuf_free(data->pending_phase2_resp);
  1187. data->pending_phase2_resp = NULL;
  1188. return;
  1189. }
  1190. in_decrypted = tls_connection_decrypt(sm->ssl_ctx, data->ssl.conn,
  1191. in_buf);
  1192. if (in_decrypted == NULL) {
  1193. wpa_printf(MSG_INFO, "EAP-FAST: Failed to decrypt Phase 2 "
  1194. "data");
  1195. eap_fast_state(data, FAILURE);
  1196. return;
  1197. }
  1198. wpa_hexdump_buf_key(MSG_DEBUG, "EAP-FAST: Decrypted Phase 2 TLVs",
  1199. in_decrypted);
  1200. eap_fast_process_phase2_tlvs(sm, data, in_decrypted);
  1201. if (sm->method_pending == METHOD_PENDING_WAIT) {
  1202. wpa_printf(MSG_DEBUG, "EAP-FAST: Phase2 method is in "
  1203. "pending wait state - save decrypted response");
  1204. wpabuf_free(data->pending_phase2_resp);
  1205. data->pending_phase2_resp = in_decrypted;
  1206. return;
  1207. }
  1208. wpabuf_free(in_decrypted);
  1209. }
  1210. static int eap_fast_process_version(struct eap_sm *sm, void *priv,
  1211. int peer_version)
  1212. {
  1213. struct eap_fast_data *data = priv;
  1214. data->peer_version = peer_version;
  1215. if (data->force_version >= 0 && peer_version != data->force_version) {
  1216. wpa_printf(MSG_INFO, "EAP-FAST: peer did not select the forced"
  1217. " version (forced=%d peer=%d) - reject",
  1218. data->force_version, peer_version);
  1219. return -1;
  1220. }
  1221. if (peer_version < data->fast_version) {
  1222. wpa_printf(MSG_DEBUG, "EAP-FAST: peer ver=%d, own ver=%d; "
  1223. "use version %d",
  1224. peer_version, data->fast_version, peer_version);
  1225. data->fast_version = peer_version;
  1226. }
  1227. return 0;
  1228. }
  1229. static int eap_fast_process_phase1(struct eap_sm *sm,
  1230. struct eap_fast_data *data)
  1231. {
  1232. if (eap_server_tls_phase1(sm, &data->ssl) < 0) {
  1233. wpa_printf(MSG_INFO, "EAP-FAST: TLS processing failed");
  1234. eap_fast_state(data, FAILURE);
  1235. return -1;
  1236. }
  1237. if (!tls_connection_established(sm->ssl_ctx, data->ssl.conn) ||
  1238. wpabuf_len(data->ssl.tls_out) > 0)
  1239. return 1;
  1240. /*
  1241. * Phase 1 was completed with the received message (e.g., when using
  1242. * abbreviated handshake), so Phase 2 can be started immediately
  1243. * without having to send through an empty message to the peer.
  1244. */
  1245. return eap_fast_phase1_done(sm, data);
  1246. }
  1247. static int eap_fast_process_phase2_start(struct eap_sm *sm,
  1248. struct eap_fast_data *data)
  1249. {
  1250. u8 next_type;
  1251. if (data->identity) {
  1252. os_free(sm->identity);
  1253. sm->identity = data->identity;
  1254. data->identity = NULL;
  1255. sm->identity_len = data->identity_len;
  1256. data->identity_len = 0;
  1257. sm->require_identity_match = 1;
  1258. if (eap_user_get(sm, sm->identity, sm->identity_len, 1) != 0) {
  1259. wpa_hexdump_ascii(MSG_DEBUG, "EAP-FAST: "
  1260. "Phase2 Identity not found "
  1261. "in the user database",
  1262. sm->identity, sm->identity_len);
  1263. next_type = eap_fast_req_failure(sm, data);
  1264. } else {
  1265. wpa_printf(MSG_DEBUG, "EAP-FAST: Identity already "
  1266. "known - skip Phase 2 Identity Request");
  1267. next_type = sm->user->methods[0].method;
  1268. sm->user_eap_method_index = 1;
  1269. }
  1270. eap_fast_state(data, PHASE2_METHOD);
  1271. } else {
  1272. eap_fast_state(data, PHASE2_ID);
  1273. next_type = EAP_TYPE_IDENTITY;
  1274. }
  1275. return eap_fast_phase2_init(sm, data, next_type);
  1276. }
  1277. static void eap_fast_process_msg(struct eap_sm *sm, void *priv,
  1278. const struct wpabuf *respData)
  1279. {
  1280. struct eap_fast_data *data = priv;
  1281. switch (data->state) {
  1282. case PHASE1:
  1283. if (eap_fast_process_phase1(sm, data))
  1284. break;
  1285. /* fall through to PHASE2_START */
  1286. case PHASE2_START:
  1287. eap_fast_process_phase2_start(sm, data);
  1288. break;
  1289. case PHASE2_ID:
  1290. case PHASE2_METHOD:
  1291. case CRYPTO_BINDING:
  1292. case REQUEST_PAC:
  1293. eap_fast_process_phase2(sm, data, data->ssl.tls_in);
  1294. break;
  1295. default:
  1296. wpa_printf(MSG_DEBUG, "EAP-FAST: Unexpected state %d in %s",
  1297. data->state, __func__);
  1298. break;
  1299. }
  1300. }
  1301. static void eap_fast_process(struct eap_sm *sm, void *priv,
  1302. struct wpabuf *respData)
  1303. {
  1304. struct eap_fast_data *data = priv;
  1305. if (eap_server_tls_process(sm, &data->ssl, respData, data,
  1306. EAP_TYPE_FAST, eap_fast_process_version,
  1307. eap_fast_process_msg) < 0)
  1308. eap_fast_state(data, FAILURE);
  1309. }
  1310. static Boolean eap_fast_isDone(struct eap_sm *sm, void *priv)
  1311. {
  1312. struct eap_fast_data *data = priv;
  1313. return data->state == SUCCESS || data->state == FAILURE;
  1314. }
  1315. static u8 * eap_fast_getKey(struct eap_sm *sm, void *priv, size_t *len)
  1316. {
  1317. struct eap_fast_data *data = priv;
  1318. u8 *eapKeyData;
  1319. if (data->state != SUCCESS)
  1320. return NULL;
  1321. eapKeyData = os_malloc(EAP_FAST_KEY_LEN);
  1322. if (eapKeyData == NULL)
  1323. return NULL;
  1324. if (eap_fast_derive_eap_msk(data->simck, eapKeyData) < 0) {
  1325. os_free(eapKeyData);
  1326. return NULL;
  1327. }
  1328. *len = EAP_FAST_KEY_LEN;
  1329. return eapKeyData;
  1330. }
  1331. static u8 * eap_fast_get_emsk(struct eap_sm *sm, void *priv, size_t *len)
  1332. {
  1333. struct eap_fast_data *data = priv;
  1334. u8 *eapKeyData;
  1335. if (data->state != SUCCESS)
  1336. return NULL;
  1337. eapKeyData = os_malloc(EAP_EMSK_LEN);
  1338. if (eapKeyData == NULL)
  1339. return NULL;
  1340. if (eap_fast_derive_eap_emsk(data->simck, eapKeyData) < 0) {
  1341. os_free(eapKeyData);
  1342. return NULL;
  1343. }
  1344. *len = EAP_EMSK_LEN;
  1345. return eapKeyData;
  1346. }
  1347. static Boolean eap_fast_isSuccess(struct eap_sm *sm, void *priv)
  1348. {
  1349. struct eap_fast_data *data = priv;
  1350. return data->state == SUCCESS;
  1351. }
  1352. static u8 * eap_fast_get_session_id(struct eap_sm *sm, void *priv, size_t *len)
  1353. {
  1354. struct eap_fast_data *data = priv;
  1355. if (data->state != SUCCESS)
  1356. return NULL;
  1357. return eap_server_tls_derive_session_id(sm, &data->ssl, EAP_TYPE_FAST,
  1358. len);
  1359. }
  1360. int eap_server_fast_register(void)
  1361. {
  1362. struct eap_method *eap;
  1363. eap = eap_server_method_alloc(EAP_SERVER_METHOD_INTERFACE_VERSION,
  1364. EAP_VENDOR_IETF, EAP_TYPE_FAST, "FAST");
  1365. if (eap == NULL)
  1366. return -1;
  1367. eap->init = eap_fast_init;
  1368. eap->reset = eap_fast_reset;
  1369. eap->buildReq = eap_fast_buildReq;
  1370. eap->check = eap_fast_check;
  1371. eap->process = eap_fast_process;
  1372. eap->isDone = eap_fast_isDone;
  1373. eap->getKey = eap_fast_getKey;
  1374. eap->get_emsk = eap_fast_get_emsk;
  1375. eap->isSuccess = eap_fast_isSuccess;
  1376. eap->getSessionId = eap_fast_get_session_id;
  1377. return eap_server_method_register(eap);
  1378. }