eap_server_fast.c 42 KB

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