eap_server_fast.c 42 KB

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