wpa_ft.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886
  1. /*
  2. * WPA Supplicant - IEEE 802.11r - Fast BSS Transition
  3. * Copyright (c) 2006-2007, Jouni Malinen <j@w1.fi>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. *
  9. * Alternatively, this software may be distributed under the terms of BSD
  10. * license.
  11. *
  12. * See README and COPYING for more details.
  13. */
  14. #include "includes.h"
  15. #include "common.h"
  16. #include "wpa.h"
  17. #include "wpa_i.h"
  18. #include "wpa_ie.h"
  19. #include "aes_wrap.h"
  20. #include "ieee802_11_defs.h"
  21. #include "ieee802_11_common.h"
  22. #ifdef CONFIG_IEEE80211R
  23. int wpa_derive_ptk_ft(struct wpa_sm *sm, const unsigned char *src_addr,
  24. const struct wpa_eapol_key *key,
  25. struct wpa_ptk *ptk)
  26. {
  27. u8 pmk_r1_name[WPA_PMK_NAME_LEN];
  28. u8 ptk_name[WPA_PMK_NAME_LEN];
  29. const u8 *anonce = key->key_nonce;
  30. if (sm->xxkey_len == 0) {
  31. wpa_printf(MSG_DEBUG, "FT: XXKey not available for key "
  32. "derivation");
  33. return -1;
  34. }
  35. wpa_derive_pmk_r0(sm->xxkey, sm->xxkey_len, sm->ssid,
  36. sm->ssid_len, sm->mobility_domain,
  37. sm->r0kh_id, sm->r0kh_id_len, sm->own_addr,
  38. sm->pmk_r0, sm->pmk_r0_name);
  39. wpa_hexdump_key(MSG_DEBUG, "FT: PMK-R0", sm->pmk_r0, PMK_LEN);
  40. wpa_hexdump(MSG_DEBUG, "FT: PMKR0Name",
  41. sm->pmk_r0_name, WPA_PMK_NAME_LEN);
  42. wpa_derive_pmk_r1(sm->pmk_r0, sm->pmk_r0_name, sm->r1kh_id,
  43. sm->own_addr, sm->pmk_r1, pmk_r1_name);
  44. wpa_hexdump_key(MSG_DEBUG, "FT: PMK-R1", sm->pmk_r1, PMK_LEN);
  45. wpa_hexdump(MSG_DEBUG, "FT: PMKR1Name", pmk_r1_name, WPA_PMK_NAME_LEN);
  46. wpa_pmk_r1_to_ptk(sm->pmk_r1, sm->snonce, anonce, sm->own_addr,
  47. sm->bssid, pmk_r1_name,
  48. (u8 *) ptk, sizeof(*ptk), ptk_name);
  49. wpa_hexdump_key(MSG_DEBUG, "FT: PTK", (u8 *) ptk, sizeof(*ptk));
  50. wpa_hexdump(MSG_DEBUG, "FT: PTKName", ptk_name, WPA_PMK_NAME_LEN);
  51. return 0;
  52. }
  53. /**
  54. * wpa_sm_set_ft_params - Set FT (IEEE 802.11r) parameters
  55. * @sm: Pointer to WPA state machine data from wpa_sm_init()
  56. * @mobility_domain: Mobility domain identifier (2 octets)
  57. * @r0kh_id: PMK-R0 key holder identity (1-48 octets)
  58. * @r0kh_id_len: R0KH-ID length (1-48)
  59. * @r1kh_id: PMK-R1 key holder identity (16 octets)
  60. * Returns: 0 on success, -1 on failure
  61. */
  62. int wpa_sm_set_ft_params(struct wpa_sm *sm, const u8 *mobility_domain,
  63. const u8 *r0kh_id, size_t r0kh_id_len,
  64. const u8 *r1kh_id)
  65. {
  66. if (sm && mobility_domain) {
  67. wpa_hexdump(MSG_DEBUG, "FT: Mobility domain",
  68. mobility_domain, MOBILITY_DOMAIN_ID_LEN);
  69. os_memcpy(sm->mobility_domain, mobility_domain,
  70. MOBILITY_DOMAIN_ID_LEN);
  71. } else if (sm)
  72. os_memset(sm->mobility_domain, 0, MOBILITY_DOMAIN_ID_LEN);
  73. if (sm && r0kh_id) {
  74. if (r0kh_id_len > FT_R0KH_ID_MAX_LEN)
  75. return -1;
  76. wpa_hexdump(MSG_DEBUG, "FT: R0KH-ID", r0kh_id, r0kh_id_len);
  77. os_memcpy(sm->r0kh_id, r0kh_id, r0kh_id_len);
  78. sm->r0kh_id_len = r0kh_id_len;
  79. } else if (sm) {
  80. /* FIX: When should R0KH-ID be cleared? We need to keep the
  81. * old R0KH-ID in order to be able to use this during FT. */
  82. /*
  83. * os_memset(sm->r0kh_id, 0, FT_R0KH_ID_LEN);
  84. * sm->r0kh_id_len = 0;
  85. */
  86. }
  87. if (sm && r1kh_id) {
  88. wpa_hexdump(MSG_DEBUG, "FT: R1KH-ID", r1kh_id, FT_R1KH_ID_LEN);
  89. os_memcpy(sm->r1kh_id, r1kh_id, FT_R1KH_ID_LEN);
  90. } else if (sm)
  91. os_memset(sm->r1kh_id, 0, FT_R1KH_ID_LEN);
  92. return 0;
  93. }
  94. /**
  95. * wpa_ft_gen_req_ies - Generate FT (IEEE 802.11r) IEs for Auth/ReAssoc Request
  96. * @sm: Pointer to WPA state machine data from wpa_sm_init()
  97. * @len: Buffer for returning the length of the IEs
  98. * @anonce: ANonce or %NULL if not yet available
  99. * @pmk_name: PMKR0Name or PMKR1Name to be added into the RSN IE PMKID List
  100. * @kck: 128-bit KCK for MIC or %NULL if no MIC is used
  101. * @target_ap: Target AP address
  102. * @ric_ies: Optional IE(s), e.g., WMM TSPEC(s), for RIC-Request or %NULL
  103. * @ric_ies_len: Length of ric_ies buffer in octets
  104. * Returns: Pointer to buffer with IEs or %NULL on failure
  105. *
  106. * Caller is responsible for freeing the returned buffer with os_free();
  107. */
  108. static u8 * wpa_ft_gen_req_ies(struct wpa_sm *sm, size_t *len,
  109. const u8 *anonce, const u8 *pmk_name,
  110. const u8 *kck, const u8 *target_ap,
  111. const u8 *ric_ies, size_t ric_ies_len)
  112. {
  113. size_t buf_len;
  114. u8 *buf, *pos, *ftie_len, *ftie_pos;
  115. struct rsn_mdie *mdie;
  116. struct rsn_ftie *ftie;
  117. struct rsn_ie_hdr *rsnie;
  118. u16 capab;
  119. sm->ft_completed = 0;
  120. buf_len = 2 + sizeof(struct rsn_mdie) + 2 + sizeof(struct rsn_ftie) +
  121. 2 + sm->r0kh_id_len + ric_ies_len + 100;
  122. buf = os_zalloc(buf_len);
  123. if (buf == NULL)
  124. return NULL;
  125. pos = buf;
  126. /* RSNIE[PMKR0Name/PMKR1Name] */
  127. rsnie = (struct rsn_ie_hdr *) pos;
  128. rsnie->elem_id = WLAN_EID_RSN;
  129. WPA_PUT_LE16(rsnie->version, RSN_VERSION);
  130. pos = (u8 *) (rsnie + 1);
  131. /* Group Suite Selector */
  132. if (sm->group_cipher == WPA_CIPHER_CCMP)
  133. RSN_SELECTOR_PUT(pos, RSN_CIPHER_SUITE_CCMP);
  134. else if (sm->group_cipher == WPA_CIPHER_TKIP)
  135. RSN_SELECTOR_PUT(pos, RSN_CIPHER_SUITE_TKIP);
  136. else {
  137. wpa_printf(MSG_WARNING, "FT: Invalid group cipher (%d)",
  138. sm->group_cipher);
  139. os_free(buf);
  140. return NULL;
  141. }
  142. pos += RSN_SELECTOR_LEN;
  143. /* Pairwise Suite Count */
  144. WPA_PUT_LE16(pos, 1);
  145. pos += 2;
  146. /* Pairwise Suite List */
  147. if (sm->pairwise_cipher == WPA_CIPHER_CCMP)
  148. RSN_SELECTOR_PUT(pos, RSN_CIPHER_SUITE_CCMP);
  149. else if (sm->pairwise_cipher == WPA_CIPHER_TKIP)
  150. RSN_SELECTOR_PUT(pos, RSN_CIPHER_SUITE_TKIP);
  151. else {
  152. wpa_printf(MSG_WARNING, "FT: Invalid pairwise cipher (%d)",
  153. sm->pairwise_cipher);
  154. os_free(buf);
  155. return NULL;
  156. }
  157. pos += RSN_SELECTOR_LEN;
  158. /* Authenticated Key Management Suite Count */
  159. WPA_PUT_LE16(pos, 1);
  160. pos += 2;
  161. /* Authenticated Key Management Suite List */
  162. if (sm->key_mgmt == WPA_KEY_MGMT_FT_IEEE8021X)
  163. RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_FT_802_1X);
  164. else if (sm->key_mgmt == WPA_KEY_MGMT_FT_PSK)
  165. RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_FT_PSK);
  166. else {
  167. wpa_printf(MSG_WARNING, "FT: Invalid key management type (%d)",
  168. sm->key_mgmt);
  169. os_free(buf);
  170. return NULL;
  171. }
  172. pos += RSN_SELECTOR_LEN;
  173. /* RSN Capabilities */
  174. capab = 0;
  175. #ifdef CONFIG_IEEE80211W
  176. if (sm->mgmt_group_cipher == WPA_CIPHER_AES_128_CMAC)
  177. capab |= WPA_CAPABILITY_MFPC;
  178. #endif /* CONFIG_IEEE80211W */
  179. WPA_PUT_LE16(pos, capab);
  180. pos += 2;
  181. /* PMKID Count */
  182. WPA_PUT_LE16(pos, 1);
  183. pos += 2;
  184. /* PMKID List [PMKR0Name/PMKR1Name] */
  185. os_memcpy(pos, pmk_name, WPA_PMK_NAME_LEN);
  186. pos += WPA_PMK_NAME_LEN;
  187. #ifdef CONFIG_IEEE80211W
  188. if (sm->mgmt_group_cipher == WPA_CIPHER_AES_128_CMAC) {
  189. /* Management Group Cipher Suite */
  190. RSN_SELECTOR_PUT(pos, RSN_CIPHER_SUITE_AES_128_CMAC);
  191. pos += RSN_SELECTOR_LEN;
  192. }
  193. #endif /* CONFIG_IEEE80211W */
  194. rsnie->len = (pos - (u8 *) rsnie) - 2;
  195. /* MDIE */
  196. *pos++ = WLAN_EID_MOBILITY_DOMAIN;
  197. *pos++ = sizeof(*mdie);
  198. mdie = (struct rsn_mdie *) pos;
  199. pos += sizeof(*mdie);
  200. os_memcpy(mdie->mobility_domain, sm->mobility_domain,
  201. MOBILITY_DOMAIN_ID_LEN);
  202. mdie->ft_capab = 0; /* FIX: copy from the target AP's MDIE */
  203. /* FTIE[SNonce, R0KH-ID] */
  204. ftie_pos = pos;
  205. *pos++ = WLAN_EID_FAST_BSS_TRANSITION;
  206. ftie_len = pos++;
  207. ftie = (struct rsn_ftie *) pos;
  208. pos += sizeof(*ftie);
  209. os_memcpy(ftie->snonce, sm->snonce, WPA_NONCE_LEN);
  210. if (anonce)
  211. os_memcpy(ftie->anonce, anonce, WPA_NONCE_LEN);
  212. /* R0KH-ID sub-element */
  213. *pos++ = FTIE_SUBELEM_R0KH_ID;
  214. *pos++ = sm->r0kh_id_len;
  215. os_memcpy(pos, sm->r0kh_id, sm->r0kh_id_len);
  216. pos += sm->r0kh_id_len;
  217. *ftie_len = pos - ftie_len - 1;
  218. if (ric_ies) {
  219. /* RIC Request */
  220. os_memcpy(pos, ric_ies, ric_ies_len);
  221. pos += ric_ies_len;
  222. }
  223. if (kck) {
  224. /*
  225. * IEEE Std 802.11r-2008, 11A.8.4
  226. * MIC shall be calculated over:
  227. * non-AP STA MAC address
  228. * Target AP MAC address
  229. * Transaction seq number (5 for ReassocReq, 3 otherwise)
  230. * RSN IE
  231. * MDIE
  232. * FTIE (with MIC field set to 0)
  233. * RIC-Request (if present)
  234. */
  235. /* Information element count */
  236. ftie->mic_control[1] = 3 + ieee802_11_ie_count(ric_ies,
  237. ric_ies_len);
  238. if (wpa_ft_mic(kck, sm->own_addr, target_ap, 5,
  239. ((u8 *) mdie) - 2, 2 + sizeof(*mdie),
  240. ftie_pos, 2 + *ftie_len,
  241. (u8 *) rsnie, 2 + rsnie->len, ric_ies,
  242. ric_ies_len, ftie->mic) < 0) {
  243. wpa_printf(MSG_INFO, "FT: Failed to calculate MIC");
  244. os_free(buf);
  245. return NULL;
  246. }
  247. }
  248. *len = pos - buf;
  249. return buf;
  250. }
  251. struct wpa_ft_ies {
  252. const u8 *mdie;
  253. size_t mdie_len;
  254. const u8 *ftie;
  255. size_t ftie_len;
  256. const u8 *r1kh_id;
  257. const u8 *gtk;
  258. size_t gtk_len;
  259. const u8 *r0kh_id;
  260. size_t r0kh_id_len;
  261. const u8 *rsn;
  262. size_t rsn_len;
  263. const u8 *rsn_pmkid;
  264. const u8 *tie;
  265. size_t tie_len;
  266. const u8 *igtk;
  267. size_t igtk_len;
  268. };
  269. static int wpa_ft_parse_ftie(const u8 *ie, size_t ie_len,
  270. struct wpa_ft_ies *parse)
  271. {
  272. const u8 *end, *pos;
  273. parse->ftie = ie;
  274. parse->ftie_len = ie_len;
  275. pos = ie + sizeof(struct rsn_ftie);
  276. end = ie + ie_len;
  277. while (pos + 2 <= end && pos + 2 + pos[1] <= end) {
  278. switch (pos[0]) {
  279. case FTIE_SUBELEM_R1KH_ID:
  280. if (pos[1] != FT_R1KH_ID_LEN) {
  281. wpa_printf(MSG_DEBUG, "FT: Invalid R1KH-ID "
  282. "length in FTIE: %d", pos[1]);
  283. return -1;
  284. }
  285. parse->r1kh_id = pos + 2;
  286. break;
  287. case FTIE_SUBELEM_GTK:
  288. parse->gtk = pos + 2;
  289. parse->gtk_len = pos[1];
  290. break;
  291. case FTIE_SUBELEM_R0KH_ID:
  292. if (pos[1] < 1 || pos[1] > FT_R0KH_ID_MAX_LEN) {
  293. wpa_printf(MSG_DEBUG, "FT: Invalid R0KH-ID "
  294. "length in FTIE: %d", pos[1]);
  295. return -1;
  296. }
  297. parse->r0kh_id = pos + 2;
  298. parse->r0kh_id_len = pos[1];
  299. break;
  300. #ifdef CONFIG_IEEE80211W
  301. case FTIE_SUBELEM_IGTK:
  302. parse->igtk = pos + 2;
  303. parse->igtk_len = pos[1];
  304. break;
  305. #endif /* CONFIG_IEEE80211W */
  306. }
  307. pos += 2 + pos[1];
  308. }
  309. return 0;
  310. }
  311. static int wpa_ft_parse_ies(const u8 *ies, size_t ies_len,
  312. struct wpa_ft_ies *parse)
  313. {
  314. const u8 *end, *pos;
  315. struct wpa_ie_data data;
  316. int ret;
  317. os_memset(parse, 0, sizeof(*parse));
  318. if (ies == NULL)
  319. return 0;
  320. pos = ies;
  321. end = ies + ies_len;
  322. while (pos + 2 <= end && pos + 2 + pos[1] <= end) {
  323. switch (pos[0]) {
  324. case WLAN_EID_RSN:
  325. parse->rsn = pos + 2;
  326. parse->rsn_len = pos[1];
  327. ret = wpa_parse_wpa_ie_rsn(parse->rsn - 2,
  328. parse->rsn_len + 2,
  329. &data);
  330. if (ret < 0) {
  331. wpa_printf(MSG_DEBUG, "FT: Failed to parse "
  332. "RSN IE: %d", ret);
  333. return -1;
  334. }
  335. if (data.num_pmkid == 1 && data.pmkid)
  336. parse->rsn_pmkid = data.pmkid;
  337. break;
  338. case WLAN_EID_MOBILITY_DOMAIN:
  339. parse->mdie = pos + 2;
  340. parse->mdie_len = pos[1];
  341. break;
  342. case WLAN_EID_FAST_BSS_TRANSITION:
  343. if (wpa_ft_parse_ftie(pos + 2, pos[1], parse) < 0)
  344. return -1;
  345. break;
  346. case WLAN_EID_TIMEOUT_INTERVAL:
  347. parse->tie = pos + 2;
  348. parse->tie_len = pos[1];
  349. break;
  350. }
  351. pos += 2 + pos[1];
  352. }
  353. return 0;
  354. }
  355. static int wpa_ft_install_ptk(struct wpa_sm *sm, const u8 *bssid)
  356. {
  357. int keylen;
  358. wpa_alg alg;
  359. u8 null_rsc[6] = { 0, 0, 0, 0, 0, 0 };
  360. wpa_printf(MSG_DEBUG, "FT: Installing PTK to the driver.");
  361. switch (sm->pairwise_cipher) {
  362. case WPA_CIPHER_CCMP:
  363. alg = WPA_ALG_CCMP;
  364. keylen = 16;
  365. break;
  366. case WPA_CIPHER_TKIP:
  367. alg = WPA_ALG_TKIP;
  368. keylen = 32;
  369. break;
  370. default:
  371. wpa_printf(MSG_WARNING, "FT: Unsupported pairwise cipher %d",
  372. sm->pairwise_cipher);
  373. return -1;
  374. }
  375. if (wpa_sm_set_key(sm, alg, bssid, 0, 1, null_rsc,
  376. sizeof(null_rsc), (u8 *) sm->ptk.tk1, keylen) < 0) {
  377. wpa_printf(MSG_WARNING, "FT: Failed to set PTK to the driver");
  378. return -1;
  379. }
  380. return 0;
  381. }
  382. /**
  383. * wpa_ft_prepare_auth_request - Generate over-the-air auth request
  384. * @sm: Pointer to WPA state machine data from wpa_sm_init()
  385. * Returns: 0 on success, -1 on failure
  386. */
  387. int wpa_ft_prepare_auth_request(struct wpa_sm *sm)
  388. {
  389. u8 *ft_ies;
  390. size_t ft_ies_len;
  391. /* Generate a new SNonce */
  392. if (os_get_random(sm->snonce, WPA_NONCE_LEN)) {
  393. wpa_printf(MSG_INFO, "FT: Failed to generate a new SNonce");
  394. return -1;
  395. }
  396. ft_ies = wpa_ft_gen_req_ies(sm, &ft_ies_len, NULL, sm->pmk_r0_name,
  397. NULL, sm->bssid, NULL, 0);
  398. if (ft_ies) {
  399. wpa_sm_update_ft_ies(sm, sm->mobility_domain,
  400. ft_ies, ft_ies_len);
  401. os_free(ft_ies);
  402. }
  403. return 0;
  404. }
  405. int wpa_ft_process_response(struct wpa_sm *sm, const u8 *ies, size_t ies_len,
  406. int ft_action, const u8 *target_ap,
  407. const u8 *ric_ies, size_t ric_ies_len)
  408. {
  409. u8 *ft_ies;
  410. size_t ft_ies_len;
  411. struct wpa_ft_ies parse;
  412. struct rsn_mdie *mdie;
  413. struct rsn_ftie *ftie;
  414. u8 ptk_name[WPA_PMK_NAME_LEN];
  415. int ret;
  416. const u8 *bssid;
  417. wpa_hexdump(MSG_DEBUG, "FT: Response IEs", ies, ies_len);
  418. wpa_hexdump(MSG_DEBUG, "FT: RIC IEs", ric_ies, ric_ies_len);
  419. if (ft_action) {
  420. if (!sm->over_the_ds_in_progress) {
  421. wpa_printf(MSG_DEBUG, "FT: No over-the-DS in progress "
  422. "- drop FT Action Response");
  423. return -1;
  424. }
  425. if (os_memcmp(target_ap, sm->target_ap, ETH_ALEN) != 0) {
  426. wpa_printf(MSG_DEBUG, "FT: No over-the-DS in progress "
  427. "with this Target AP - drop FT Action "
  428. "Response");
  429. return -1;
  430. }
  431. }
  432. if (sm->key_mgmt != WPA_KEY_MGMT_FT_IEEE8021X &&
  433. sm->key_mgmt != WPA_KEY_MGMT_FT_PSK) {
  434. wpa_printf(MSG_DEBUG, "FT: Reject FT IEs since FT is not "
  435. "enabled for this connection");
  436. return -1;
  437. }
  438. if (wpa_ft_parse_ies(ies, ies_len, &parse) < 0) {
  439. wpa_printf(MSG_DEBUG, "FT: Failed to parse IEs");
  440. return -1;
  441. }
  442. mdie = (struct rsn_mdie *) parse.mdie;
  443. if (mdie == NULL || parse.mdie_len < sizeof(*mdie) ||
  444. os_memcmp(mdie->mobility_domain, sm->mobility_domain,
  445. MOBILITY_DOMAIN_ID_LEN) != 0) {
  446. wpa_printf(MSG_DEBUG, "FT: Invalid MDIE");
  447. return -1;
  448. }
  449. ftie = (struct rsn_ftie *) parse.ftie;
  450. if (ftie == NULL || parse.ftie_len < sizeof(*ftie)) {
  451. wpa_printf(MSG_DEBUG, "FT: Invalid FTIE");
  452. return -1;
  453. }
  454. if (parse.r0kh_id == NULL) {
  455. wpa_printf(MSG_DEBUG, "FT: No R0KH-ID subelem in FTIE");
  456. return -1;
  457. }
  458. if (parse.r0kh_id_len != sm->r0kh_id_len ||
  459. os_memcmp(parse.r0kh_id, sm->r0kh_id, parse.r0kh_id_len) != 0) {
  460. wpa_printf(MSG_DEBUG, "FT: R0KH-ID in FTIE did not match with "
  461. "the current R0KH-ID");
  462. wpa_hexdump(MSG_DEBUG, "FT: R0KH-ID in FTIE",
  463. parse.r0kh_id, parse.r0kh_id_len);
  464. wpa_hexdump(MSG_DEBUG, "FT: The current R0KH-ID",
  465. sm->r0kh_id, sm->r0kh_id_len);
  466. return -1;
  467. }
  468. if (parse.r1kh_id == NULL) {
  469. wpa_printf(MSG_DEBUG, "FT: No R1KH-ID subelem in FTIE");
  470. return -1;
  471. }
  472. if (parse.rsn_pmkid == NULL ||
  473. os_memcmp(parse.rsn_pmkid, sm->pmk_r0_name, WPA_PMK_NAME_LEN)) {
  474. wpa_printf(MSG_DEBUG, "FT: No matching PMKR0Name (PMKID) in "
  475. "RSNIE");
  476. return -1;
  477. }
  478. os_memcpy(sm->r1kh_id, parse.r1kh_id, FT_R1KH_ID_LEN);
  479. wpa_hexdump(MSG_DEBUG, "FT: R1KH-ID", sm->r1kh_id, FT_R1KH_ID_LEN);
  480. wpa_hexdump(MSG_DEBUG, "FT: SNonce", sm->snonce, WPA_NONCE_LEN);
  481. wpa_hexdump(MSG_DEBUG, "FT: ANonce", ftie->anonce, WPA_NONCE_LEN);
  482. wpa_derive_pmk_r1(sm->pmk_r0, sm->pmk_r0_name, sm->r1kh_id,
  483. sm->own_addr, sm->pmk_r1, sm->pmk_r1_name);
  484. wpa_hexdump_key(MSG_DEBUG, "FT: PMK-R1", sm->pmk_r1, PMK_LEN);
  485. wpa_hexdump(MSG_DEBUG, "FT: PMKR1Name",
  486. sm->pmk_r1_name, WPA_PMK_NAME_LEN);
  487. bssid = target_ap;
  488. wpa_pmk_r1_to_ptk(sm->pmk_r1, sm->snonce, ftie->anonce, sm->own_addr,
  489. bssid, sm->pmk_r1_name,
  490. (u8 *) &sm->ptk, sizeof(sm->ptk), ptk_name);
  491. wpa_hexdump_key(MSG_DEBUG, "FT: PTK",
  492. (u8 *) &sm->ptk, sizeof(sm->ptk));
  493. wpa_hexdump(MSG_DEBUG, "FT: PTKName", ptk_name, WPA_PMK_NAME_LEN);
  494. ft_ies = wpa_ft_gen_req_ies(sm, &ft_ies_len, ftie->anonce,
  495. sm->pmk_r1_name, sm->ptk.kck, bssid,
  496. ric_ies, ric_ies_len);
  497. if (ft_ies) {
  498. wpa_sm_update_ft_ies(sm, sm->mobility_domain,
  499. ft_ies, ft_ies_len);
  500. os_free(ft_ies);
  501. }
  502. ret = wpa_ft_install_ptk(sm, bssid);
  503. if (ret == 0) {
  504. sm->ft_completed = 1;
  505. if (ft_action) {
  506. /* TODO: trigger re-association to the Target AP;
  507. * MLME is now doing this automatically, but it should
  508. * really be done only if we get here successfully. */
  509. os_memcpy(sm->bssid, target_ap, ETH_ALEN);
  510. }
  511. }
  512. return ret;
  513. }
  514. int wpa_ft_is_completed(struct wpa_sm *sm)
  515. {
  516. if (sm == NULL)
  517. return 0;
  518. if (sm->key_mgmt != WPA_KEY_MGMT_FT_IEEE8021X &&
  519. sm->key_mgmt != WPA_KEY_MGMT_FT_PSK)
  520. return 0;
  521. return sm->ft_completed;
  522. }
  523. static int wpa_ft_process_gtk_subelem(struct wpa_sm *sm, const u8 *gtk_elem,
  524. size_t gtk_elem_len)
  525. {
  526. u8 gtk[32];
  527. int keyidx;
  528. wpa_alg alg;
  529. size_t gtk_len, keylen, rsc_len;
  530. if (gtk_elem == NULL) {
  531. wpa_printf(MSG_DEBUG, "FT: No GTK included in FTIE");
  532. return 0;
  533. }
  534. wpa_hexdump_key(MSG_DEBUG, "FT: Received GTK in Reassoc Resp",
  535. gtk_elem, gtk_elem_len);
  536. if (gtk_elem_len < 10 + 24 || (gtk_elem_len - 10) % 8 ||
  537. gtk_elem_len - 18 > sizeof(gtk)) {
  538. wpa_printf(MSG_DEBUG, "FT: Invalid GTK sub-elem "
  539. "length %lu", (unsigned long) gtk_elem_len);
  540. return -1;
  541. }
  542. gtk_len = gtk_elem_len - 18;
  543. if (aes_unwrap(sm->ptk.kek, gtk_len / 8, gtk_elem + 10, gtk)) {
  544. wpa_printf(MSG_WARNING, "FT: AES unwrap failed - could not "
  545. "decrypt GTK");
  546. return -1;
  547. }
  548. switch (sm->group_cipher) {
  549. case WPA_CIPHER_CCMP:
  550. keylen = 16;
  551. rsc_len = 6;
  552. alg = WPA_ALG_CCMP;
  553. break;
  554. case WPA_CIPHER_TKIP:
  555. keylen = 32;
  556. rsc_len = 6;
  557. alg = WPA_ALG_TKIP;
  558. break;
  559. case WPA_CIPHER_WEP104:
  560. keylen = 13;
  561. rsc_len = 0;
  562. alg = WPA_ALG_WEP;
  563. break;
  564. case WPA_CIPHER_WEP40:
  565. keylen = 5;
  566. rsc_len = 0;
  567. alg = WPA_ALG_WEP;
  568. break;
  569. default:
  570. wpa_printf(MSG_WARNING, "WPA: Unsupported Group Cipher %d",
  571. sm->group_cipher);
  572. return -1;
  573. }
  574. if (gtk_len < keylen) {
  575. wpa_printf(MSG_DEBUG, "FT: Too short GTK in FTIE");
  576. return -1;
  577. }
  578. /* Key Info[1] | Key Length[1] | RSC[8] | Key[5..32]. */
  579. keyidx = gtk_elem[0] & 0x03;
  580. if (gtk_elem[1] != keylen) {
  581. wpa_printf(MSG_DEBUG, "FT: GTK length mismatch: received %d "
  582. "negotiated %lu",
  583. gtk_elem[1], (unsigned long) keylen);
  584. return -1;
  585. }
  586. wpa_hexdump_key(MSG_DEBUG, "FT: GTK from Reassoc Resp", gtk, keylen);
  587. if (wpa_sm_set_key(sm, alg, (u8 *) "\xff\xff\xff\xff\xff\xff",
  588. keyidx, 0, gtk_elem + 2, rsc_len, gtk, keylen) <
  589. 0) {
  590. wpa_printf(MSG_WARNING, "WPA: Failed to set GTK to the "
  591. "driver.");
  592. return -1;
  593. }
  594. return 0;
  595. }
  596. #ifdef CONFIG_IEEE80211W
  597. static int wpa_ft_process_igtk_subelem(struct wpa_sm *sm, const u8 *igtk_elem,
  598. size_t igtk_elem_len)
  599. {
  600. u8 igtk[WPA_IGTK_LEN];
  601. u16 keyidx;
  602. if (sm->mgmt_group_cipher != WPA_CIPHER_AES_128_CMAC)
  603. return 0;
  604. if (igtk_elem == NULL) {
  605. wpa_printf(MSG_DEBUG, "FT: No IGTK included in FTIE");
  606. return 0;
  607. }
  608. wpa_hexdump_key(MSG_DEBUG, "FT: Received IGTK in Reassoc Resp",
  609. igtk_elem, igtk_elem_len);
  610. if (igtk_elem_len != 2 + 6 + 1 + WPA_IGTK_LEN + 8) {
  611. wpa_printf(MSG_DEBUG, "FT: Invalid IGTK sub-elem "
  612. "length %lu", (unsigned long) igtk_elem_len);
  613. return -1;
  614. }
  615. if (igtk_elem[8] != WPA_IGTK_LEN) {
  616. wpa_printf(MSG_DEBUG, "FT: Invalid IGTK sub-elem Key Length "
  617. "%d", igtk_elem[8]);
  618. return -1;
  619. }
  620. if (aes_unwrap(sm->ptk.kek, WPA_IGTK_LEN / 8, igtk_elem + 9, igtk)) {
  621. wpa_printf(MSG_WARNING, "FT: AES unwrap failed - could not "
  622. "decrypt IGTK");
  623. return -1;
  624. }
  625. /* KeyID[2] | IPN[6] | Key Length[1] | Key[16+8] */
  626. keyidx = WPA_GET_LE16(igtk_elem);
  627. wpa_hexdump_key(MSG_DEBUG, "FT: IGTK from Reassoc Resp", igtk,
  628. WPA_IGTK_LEN);
  629. if (wpa_sm_set_key(sm, WPA_ALG_IGTK, (u8 *) "\xff\xff\xff\xff\xff\xff",
  630. keyidx, 0, igtk_elem + 2, 6, igtk, WPA_IGTK_LEN) <
  631. 0) {
  632. wpa_printf(MSG_WARNING, "WPA: Failed to set IGTK to the "
  633. "driver.");
  634. return -1;
  635. }
  636. return 0;
  637. }
  638. #endif /* CONFIG_IEEE80211W */
  639. int wpa_ft_validate_reassoc_resp(struct wpa_sm *sm, const u8 *ies,
  640. size_t ies_len, const u8 *src_addr)
  641. {
  642. struct wpa_ft_ies parse;
  643. struct rsn_mdie *mdie;
  644. struct rsn_ftie *ftie;
  645. size_t count;
  646. u8 mic[16];
  647. wpa_hexdump(MSG_DEBUG, "FT: Response IEs", ies, ies_len);
  648. if (sm->key_mgmt != WPA_KEY_MGMT_FT_IEEE8021X &&
  649. sm->key_mgmt != WPA_KEY_MGMT_FT_PSK) {
  650. wpa_printf(MSG_DEBUG, "FT: Reject FT IEs since FT is not "
  651. "enabled for this connection");
  652. return -1;
  653. }
  654. if (wpa_ft_parse_ies(ies, ies_len, &parse) < 0) {
  655. wpa_printf(MSG_DEBUG, "FT: Failed to parse IEs");
  656. return -1;
  657. }
  658. mdie = (struct rsn_mdie *) parse.mdie;
  659. if (mdie == NULL || parse.mdie_len < sizeof(*mdie) ||
  660. os_memcmp(mdie->mobility_domain, sm->mobility_domain,
  661. MOBILITY_DOMAIN_ID_LEN) != 0) {
  662. wpa_printf(MSG_DEBUG, "FT: Invalid MDIE");
  663. return -1;
  664. }
  665. ftie = (struct rsn_ftie *) parse.ftie;
  666. if (ftie == NULL || parse.ftie_len < sizeof(*ftie)) {
  667. wpa_printf(MSG_DEBUG, "FT: Invalid FTIE");
  668. return -1;
  669. }
  670. if (parse.r0kh_id == NULL) {
  671. wpa_printf(MSG_DEBUG, "FT: No R0KH-ID subelem in FTIE");
  672. return -1;
  673. }
  674. if (parse.r0kh_id_len != sm->r0kh_id_len ||
  675. os_memcmp(parse.r0kh_id, sm->r0kh_id, parse.r0kh_id_len) != 0) {
  676. wpa_printf(MSG_DEBUG, "FT: R0KH-ID in FTIE did not match with "
  677. "the current R0KH-ID");
  678. wpa_hexdump(MSG_DEBUG, "FT: R0KH-ID in FTIE",
  679. parse.r0kh_id, parse.r0kh_id_len);
  680. wpa_hexdump(MSG_DEBUG, "FT: The current R0KH-ID",
  681. sm->r0kh_id, sm->r0kh_id_len);
  682. return -1;
  683. }
  684. if (parse.r1kh_id == NULL) {
  685. wpa_printf(MSG_DEBUG, "FT: No R1KH-ID subelem in FTIE");
  686. return -1;
  687. }
  688. if (os_memcmp(parse.r1kh_id, sm->r1kh_id, FT_R1KH_ID_LEN) != 0) {
  689. wpa_printf(MSG_DEBUG, "FT: Unknown R1KH-ID used in "
  690. "ReassocResp");
  691. return -1;
  692. }
  693. if (parse.rsn_pmkid == NULL ||
  694. os_memcmp(parse.rsn_pmkid, sm->pmk_r1_name, WPA_PMK_NAME_LEN)) {
  695. wpa_printf(MSG_DEBUG, "FT: No matching PMKR1Name (PMKID) in "
  696. "RSNIE (pmkid=%d)", !!parse.rsn_pmkid);
  697. return -1;
  698. }
  699. count = 3;
  700. if (parse.tie)
  701. count++;
  702. if (ftie->mic_control[1] != count) {
  703. wpa_printf(MSG_DEBUG, "FT: Unexpected IE count in FTIE (%d)",
  704. ftie->mic_control[1]);
  705. return -1;
  706. }
  707. if (wpa_ft_mic(sm->ptk.kck, sm->own_addr, src_addr, 6,
  708. parse.mdie - 2, parse.mdie_len + 2,
  709. parse.ftie - 2, parse.ftie_len + 2,
  710. parse.rsn - 2, parse.rsn_len + 2, NULL, 0,
  711. mic) < 0) {
  712. wpa_printf(MSG_DEBUG, "FT: Failed to calculate MIC");
  713. return -1;
  714. }
  715. if (os_memcmp(mic, ftie->mic, 16) != 0) {
  716. wpa_printf(MSG_DEBUG, "FT: Invalid MIC in FTIE");
  717. wpa_hexdump(MSG_MSGDUMP, "FT: Received MIC", ftie->mic, 16);
  718. wpa_hexdump(MSG_MSGDUMP, "FT: Calculated MIC", mic, 16);
  719. return -1;
  720. }
  721. if (wpa_ft_process_gtk_subelem(sm, parse.gtk, parse.gtk_len) < 0)
  722. return -1;
  723. #ifdef CONFIG_IEEE80211W
  724. if (wpa_ft_process_igtk_subelem(sm, parse.igtk, parse.igtk_len) < 0)
  725. return -1;
  726. #endif /* CONFIG_IEEE80211W */
  727. return 0;
  728. }
  729. /**
  730. * wpa_ft_start_over_ds - Generate over-the-DS auth request
  731. * @sm: Pointer to WPA state machine data from wpa_sm_init()
  732. * Returns: 0 on success, -1 on failure
  733. */
  734. int wpa_ft_start_over_ds(struct wpa_sm *sm, const u8 *target_ap)
  735. {
  736. u8 *ft_ies;
  737. size_t ft_ies_len;
  738. wpa_printf(MSG_DEBUG, "FT: Request over-the-DS with " MACSTR,
  739. MAC2STR(target_ap));
  740. /* Generate a new SNonce */
  741. if (os_get_random(sm->snonce, WPA_NONCE_LEN)) {
  742. wpa_printf(MSG_INFO, "FT: Failed to generate a new SNonce");
  743. return -1;
  744. }
  745. ft_ies = wpa_ft_gen_req_ies(sm, &ft_ies_len, NULL, sm->pmk_r0_name,
  746. NULL, target_ap, NULL, 0);
  747. if (ft_ies) {
  748. sm->over_the_ds_in_progress = 1;
  749. os_memcpy(sm->target_ap, target_ap, ETH_ALEN);
  750. wpa_sm_send_ft_action(sm, 1, target_ap, ft_ies, ft_ies_len);
  751. os_free(ft_ies);
  752. }
  753. return 0;
  754. }
  755. #endif /* CONFIG_IEEE80211R */