eap_server_fast.c 42 KB

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