wpa_ft.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960
  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 "crypto/aes_wrap.h"
  17. #include "common/ieee802_11_defs.h"
  18. #include "common/ieee802_11_common.h"
  19. #include "wpa.h"
  20. #include "wpa_i.h"
  21. #include "wpa_ie.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, size_t ptk_len)
  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, ptk_len, ptk_name);
  49. wpa_hexdump_key(MSG_DEBUG, "FT: PTK", (u8 *) ptk, ptk_len);
  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. const u8 *ric;
  269. size_t ric_len;
  270. };
  271. static int wpa_ft_parse_ftie(const u8 *ie, size_t ie_len,
  272. struct wpa_ft_ies *parse)
  273. {
  274. const u8 *end, *pos;
  275. parse->ftie = ie;
  276. parse->ftie_len = ie_len;
  277. pos = ie + sizeof(struct rsn_ftie);
  278. end = ie + ie_len;
  279. while (pos + 2 <= end && pos + 2 + pos[1] <= end) {
  280. switch (pos[0]) {
  281. case FTIE_SUBELEM_R1KH_ID:
  282. if (pos[1] != FT_R1KH_ID_LEN) {
  283. wpa_printf(MSG_DEBUG, "FT: Invalid R1KH-ID "
  284. "length in FTIE: %d", pos[1]);
  285. return -1;
  286. }
  287. parse->r1kh_id = pos + 2;
  288. break;
  289. case FTIE_SUBELEM_GTK:
  290. parse->gtk = pos + 2;
  291. parse->gtk_len = pos[1];
  292. break;
  293. case FTIE_SUBELEM_R0KH_ID:
  294. if (pos[1] < 1 || pos[1] > FT_R0KH_ID_MAX_LEN) {
  295. wpa_printf(MSG_DEBUG, "FT: Invalid R0KH-ID "
  296. "length in FTIE: %d", pos[1]);
  297. return -1;
  298. }
  299. parse->r0kh_id = pos + 2;
  300. parse->r0kh_id_len = pos[1];
  301. break;
  302. #ifdef CONFIG_IEEE80211W
  303. case FTIE_SUBELEM_IGTK:
  304. parse->igtk = pos + 2;
  305. parse->igtk_len = pos[1];
  306. break;
  307. #endif /* CONFIG_IEEE80211W */
  308. }
  309. pos += 2 + pos[1];
  310. }
  311. return 0;
  312. }
  313. static int wpa_ft_parse_ies(const u8 *ies, size_t ies_len,
  314. struct wpa_ft_ies *parse)
  315. {
  316. const u8 *end, *pos;
  317. struct wpa_ie_data data;
  318. int ret;
  319. const struct rsn_ftie *ftie;
  320. int prot_ie_count = 0;
  321. os_memset(parse, 0, sizeof(*parse));
  322. if (ies == NULL)
  323. return 0;
  324. pos = ies;
  325. end = ies + ies_len;
  326. while (pos + 2 <= end && pos + 2 + pos[1] <= end) {
  327. switch (pos[0]) {
  328. case WLAN_EID_RSN:
  329. parse->rsn = pos + 2;
  330. parse->rsn_len = pos[1];
  331. ret = wpa_parse_wpa_ie_rsn(parse->rsn - 2,
  332. parse->rsn_len + 2,
  333. &data);
  334. if (ret < 0) {
  335. wpa_printf(MSG_DEBUG, "FT: Failed to parse "
  336. "RSN IE: %d", ret);
  337. return -1;
  338. }
  339. if (data.num_pmkid == 1 && data.pmkid)
  340. parse->rsn_pmkid = data.pmkid;
  341. break;
  342. case WLAN_EID_MOBILITY_DOMAIN:
  343. parse->mdie = pos + 2;
  344. parse->mdie_len = pos[1];
  345. break;
  346. case WLAN_EID_FAST_BSS_TRANSITION:
  347. if (pos[1] < sizeof(*ftie))
  348. return -1;
  349. ftie = (const struct rsn_ftie *) (pos + 2);
  350. prot_ie_count = ftie->mic_control[1];
  351. if (wpa_ft_parse_ftie(pos + 2, pos[1], parse) < 0)
  352. return -1;
  353. break;
  354. case WLAN_EID_TIMEOUT_INTERVAL:
  355. parse->tie = pos + 2;
  356. parse->tie_len = pos[1];
  357. break;
  358. case WLAN_EID_RIC_DATA:
  359. if (parse->ric == NULL)
  360. parse->ric = pos;
  361. }
  362. pos += 2 + pos[1];
  363. }
  364. if (prot_ie_count == 0)
  365. return 0; /* no MIC */
  366. /*
  367. * Check that the protected IE count matches with IEs included in the
  368. * frame.
  369. */
  370. if (parse->rsn)
  371. prot_ie_count--;
  372. if (parse->mdie)
  373. prot_ie_count--;
  374. if (parse->ftie)
  375. prot_ie_count--;
  376. if (parse->tie)
  377. prot_ie_count--;
  378. if (prot_ie_count < 0) {
  379. wpa_printf(MSG_DEBUG, "FT: Some required IEs not included in "
  380. "the protected IE count");
  381. return -1;
  382. }
  383. if (prot_ie_count == 0 && parse->ric) {
  384. wpa_printf(MSG_DEBUG, "FT: RIC IE(s) in the frame, but not "
  385. "included in protected IE count");
  386. return -1;
  387. }
  388. /* Determine the end of the RIC IE(s) */
  389. pos = parse->ric;
  390. while (pos && pos + 2 <= end && pos + 2 + pos[1] <= end &&
  391. prot_ie_count) {
  392. prot_ie_count--;
  393. pos += 2 + pos[1];
  394. }
  395. parse->ric_len = pos - parse->ric;
  396. if (prot_ie_count) {
  397. wpa_printf(MSG_DEBUG, "FT: %d protected IEs missing from "
  398. "frame", (int) prot_ie_count);
  399. return -1;
  400. }
  401. return 0;
  402. }
  403. static int wpa_ft_install_ptk(struct wpa_sm *sm, const u8 *bssid)
  404. {
  405. int keylen;
  406. enum wpa_alg alg;
  407. u8 null_rsc[6] = { 0, 0, 0, 0, 0, 0 };
  408. wpa_printf(MSG_DEBUG, "FT: Installing PTK to the driver.");
  409. switch (sm->pairwise_cipher) {
  410. case WPA_CIPHER_CCMP:
  411. alg = WPA_ALG_CCMP;
  412. keylen = 16;
  413. break;
  414. case WPA_CIPHER_TKIP:
  415. alg = WPA_ALG_TKIP;
  416. keylen = 32;
  417. break;
  418. default:
  419. wpa_printf(MSG_WARNING, "FT: Unsupported pairwise cipher %d",
  420. sm->pairwise_cipher);
  421. return -1;
  422. }
  423. if (wpa_sm_set_key(sm, alg, bssid, 0, 1, null_rsc,
  424. sizeof(null_rsc), (u8 *) sm->ptk.tk1, keylen) < 0) {
  425. wpa_printf(MSG_WARNING, "FT: Failed to set PTK to the driver");
  426. return -1;
  427. }
  428. return 0;
  429. }
  430. /**
  431. * wpa_ft_prepare_auth_request - Generate over-the-air auth request
  432. * @sm: Pointer to WPA state machine data from wpa_sm_init()
  433. * Returns: 0 on success, -1 on failure
  434. */
  435. int wpa_ft_prepare_auth_request(struct wpa_sm *sm)
  436. {
  437. u8 *ft_ies;
  438. size_t ft_ies_len;
  439. /* Generate a new SNonce */
  440. if (os_get_random(sm->snonce, WPA_NONCE_LEN)) {
  441. wpa_printf(MSG_INFO, "FT: Failed to generate a new SNonce");
  442. return -1;
  443. }
  444. ft_ies = wpa_ft_gen_req_ies(sm, &ft_ies_len, NULL, sm->pmk_r0_name,
  445. NULL, sm->bssid, NULL, 0);
  446. if (ft_ies) {
  447. wpa_sm_update_ft_ies(sm, sm->mobility_domain,
  448. ft_ies, ft_ies_len);
  449. os_free(ft_ies);
  450. }
  451. return 0;
  452. }
  453. int wpa_ft_process_response(struct wpa_sm *sm, const u8 *ies, size_t ies_len,
  454. int ft_action, const u8 *target_ap,
  455. const u8 *ric_ies, size_t ric_ies_len)
  456. {
  457. u8 *ft_ies;
  458. size_t ft_ies_len, ptk_len;
  459. struct wpa_ft_ies parse;
  460. struct rsn_mdie *mdie;
  461. struct rsn_ftie *ftie;
  462. u8 ptk_name[WPA_PMK_NAME_LEN];
  463. int ret;
  464. const u8 *bssid;
  465. wpa_hexdump(MSG_DEBUG, "FT: Response IEs", ies, ies_len);
  466. wpa_hexdump(MSG_DEBUG, "FT: RIC IEs", ric_ies, ric_ies_len);
  467. if (ft_action) {
  468. if (!sm->over_the_ds_in_progress) {
  469. wpa_printf(MSG_DEBUG, "FT: No over-the-DS in progress "
  470. "- drop FT Action Response");
  471. return -1;
  472. }
  473. if (os_memcmp(target_ap, sm->target_ap, ETH_ALEN) != 0) {
  474. wpa_printf(MSG_DEBUG, "FT: No over-the-DS in progress "
  475. "with this Target AP - drop FT Action "
  476. "Response");
  477. return -1;
  478. }
  479. }
  480. if (sm->key_mgmt != WPA_KEY_MGMT_FT_IEEE8021X &&
  481. sm->key_mgmt != WPA_KEY_MGMT_FT_PSK) {
  482. wpa_printf(MSG_DEBUG, "FT: Reject FT IEs since FT is not "
  483. "enabled for this connection");
  484. return -1;
  485. }
  486. if (wpa_ft_parse_ies(ies, ies_len, &parse) < 0) {
  487. wpa_printf(MSG_DEBUG, "FT: Failed to parse IEs");
  488. return -1;
  489. }
  490. mdie = (struct rsn_mdie *) parse.mdie;
  491. if (mdie == NULL || parse.mdie_len < sizeof(*mdie) ||
  492. os_memcmp(mdie->mobility_domain, sm->mobility_domain,
  493. MOBILITY_DOMAIN_ID_LEN) != 0) {
  494. wpa_printf(MSG_DEBUG, "FT: Invalid MDIE");
  495. return -1;
  496. }
  497. ftie = (struct rsn_ftie *) parse.ftie;
  498. if (ftie == NULL || parse.ftie_len < sizeof(*ftie)) {
  499. wpa_printf(MSG_DEBUG, "FT: Invalid FTIE");
  500. return -1;
  501. }
  502. if (parse.r0kh_id == NULL) {
  503. wpa_printf(MSG_DEBUG, "FT: No R0KH-ID subelem in FTIE");
  504. return -1;
  505. }
  506. if (parse.r0kh_id_len != sm->r0kh_id_len ||
  507. os_memcmp(parse.r0kh_id, sm->r0kh_id, parse.r0kh_id_len) != 0) {
  508. wpa_printf(MSG_DEBUG, "FT: R0KH-ID in FTIE did not match with "
  509. "the current R0KH-ID");
  510. wpa_hexdump(MSG_DEBUG, "FT: R0KH-ID in FTIE",
  511. parse.r0kh_id, parse.r0kh_id_len);
  512. wpa_hexdump(MSG_DEBUG, "FT: The current R0KH-ID",
  513. sm->r0kh_id, sm->r0kh_id_len);
  514. return -1;
  515. }
  516. if (parse.r1kh_id == NULL) {
  517. wpa_printf(MSG_DEBUG, "FT: No R1KH-ID subelem in FTIE");
  518. return -1;
  519. }
  520. if (parse.rsn_pmkid == NULL ||
  521. os_memcmp(parse.rsn_pmkid, sm->pmk_r0_name, WPA_PMK_NAME_LEN)) {
  522. wpa_printf(MSG_DEBUG, "FT: No matching PMKR0Name (PMKID) in "
  523. "RSNIE");
  524. return -1;
  525. }
  526. os_memcpy(sm->r1kh_id, parse.r1kh_id, FT_R1KH_ID_LEN);
  527. wpa_hexdump(MSG_DEBUG, "FT: R1KH-ID", sm->r1kh_id, FT_R1KH_ID_LEN);
  528. wpa_hexdump(MSG_DEBUG, "FT: SNonce", sm->snonce, WPA_NONCE_LEN);
  529. wpa_hexdump(MSG_DEBUG, "FT: ANonce", ftie->anonce, WPA_NONCE_LEN);
  530. wpa_derive_pmk_r1(sm->pmk_r0, sm->pmk_r0_name, sm->r1kh_id,
  531. sm->own_addr, sm->pmk_r1, sm->pmk_r1_name);
  532. wpa_hexdump_key(MSG_DEBUG, "FT: PMK-R1", sm->pmk_r1, PMK_LEN);
  533. wpa_hexdump(MSG_DEBUG, "FT: PMKR1Name",
  534. sm->pmk_r1_name, WPA_PMK_NAME_LEN);
  535. bssid = target_ap;
  536. ptk_len = sm->pairwise_cipher == WPA_CIPHER_CCMP ? 48 : 64;
  537. wpa_pmk_r1_to_ptk(sm->pmk_r1, sm->snonce, ftie->anonce, sm->own_addr,
  538. bssid, sm->pmk_r1_name,
  539. (u8 *) &sm->ptk, ptk_len, ptk_name);
  540. wpa_hexdump_key(MSG_DEBUG, "FT: PTK",
  541. (u8 *) &sm->ptk, ptk_len);
  542. wpa_hexdump(MSG_DEBUG, "FT: PTKName", ptk_name, WPA_PMK_NAME_LEN);
  543. ft_ies = wpa_ft_gen_req_ies(sm, &ft_ies_len, ftie->anonce,
  544. sm->pmk_r1_name, sm->ptk.kck, bssid,
  545. ric_ies, ric_ies_len);
  546. if (ft_ies) {
  547. wpa_sm_update_ft_ies(sm, sm->mobility_domain,
  548. ft_ies, ft_ies_len);
  549. os_free(ft_ies);
  550. }
  551. wpa_sm_mark_authenticated(sm, bssid);
  552. ret = wpa_ft_install_ptk(sm, bssid);
  553. if (ret) {
  554. /*
  555. * Some drivers do not support key configuration when we are
  556. * not associated with the target AP. Work around this by
  557. * trying again after the following reassociation gets
  558. * completed.
  559. */
  560. wpa_printf(MSG_DEBUG, "FT: Failed to set PTK prior to "
  561. "association - try again after reassociation");
  562. sm->set_ptk_after_assoc = 1;
  563. } else
  564. sm->set_ptk_after_assoc = 0;
  565. sm->ft_completed = 1;
  566. if (ft_action) {
  567. /*
  568. * The caller is expected trigger re-association with the
  569. * Target AP.
  570. */
  571. os_memcpy(sm->bssid, target_ap, ETH_ALEN);
  572. }
  573. return 0;
  574. }
  575. int wpa_ft_is_completed(struct wpa_sm *sm)
  576. {
  577. if (sm == NULL)
  578. return 0;
  579. if (sm->key_mgmt != WPA_KEY_MGMT_FT_IEEE8021X &&
  580. sm->key_mgmt != WPA_KEY_MGMT_FT_PSK)
  581. return 0;
  582. return sm->ft_completed;
  583. }
  584. static int wpa_ft_process_gtk_subelem(struct wpa_sm *sm, const u8 *gtk_elem,
  585. size_t gtk_elem_len)
  586. {
  587. u8 gtk[32];
  588. int keyidx;
  589. enum wpa_alg alg;
  590. size_t gtk_len, keylen, rsc_len;
  591. if (gtk_elem == NULL) {
  592. wpa_printf(MSG_DEBUG, "FT: No GTK included in FTIE");
  593. return 0;
  594. }
  595. wpa_hexdump_key(MSG_DEBUG, "FT: Received GTK in Reassoc Resp",
  596. gtk_elem, gtk_elem_len);
  597. if (gtk_elem_len < 10 + 24 || (gtk_elem_len - 10) % 8 ||
  598. gtk_elem_len - 18 > sizeof(gtk)) {
  599. wpa_printf(MSG_DEBUG, "FT: Invalid GTK sub-elem "
  600. "length %lu", (unsigned long) gtk_elem_len);
  601. return -1;
  602. }
  603. gtk_len = gtk_elem_len - 18;
  604. if (aes_unwrap(sm->ptk.kek, gtk_len / 8, gtk_elem + 10, gtk)) {
  605. wpa_printf(MSG_WARNING, "FT: AES unwrap failed - could not "
  606. "decrypt GTK");
  607. return -1;
  608. }
  609. switch (sm->group_cipher) {
  610. case WPA_CIPHER_CCMP:
  611. keylen = 16;
  612. rsc_len = 6;
  613. alg = WPA_ALG_CCMP;
  614. break;
  615. case WPA_CIPHER_TKIP:
  616. keylen = 32;
  617. rsc_len = 6;
  618. alg = WPA_ALG_TKIP;
  619. break;
  620. case WPA_CIPHER_WEP104:
  621. keylen = 13;
  622. rsc_len = 0;
  623. alg = WPA_ALG_WEP;
  624. break;
  625. case WPA_CIPHER_WEP40:
  626. keylen = 5;
  627. rsc_len = 0;
  628. alg = WPA_ALG_WEP;
  629. break;
  630. default:
  631. wpa_printf(MSG_WARNING, "WPA: Unsupported Group Cipher %d",
  632. sm->group_cipher);
  633. return -1;
  634. }
  635. if (gtk_len < keylen) {
  636. wpa_printf(MSG_DEBUG, "FT: Too short GTK in FTIE");
  637. return -1;
  638. }
  639. /* Key Info[1] | Key Length[1] | RSC[8] | Key[5..32]. */
  640. keyidx = gtk_elem[0] & 0x03;
  641. if (gtk_elem[1] != keylen) {
  642. wpa_printf(MSG_DEBUG, "FT: GTK length mismatch: received %d "
  643. "negotiated %lu",
  644. gtk_elem[1], (unsigned long) keylen);
  645. return -1;
  646. }
  647. wpa_hexdump_key(MSG_DEBUG, "FT: GTK from Reassoc Resp", gtk, keylen);
  648. if (wpa_sm_set_key(sm, alg, (u8 *) "\xff\xff\xff\xff\xff\xff",
  649. keyidx, 0, gtk_elem + 2, rsc_len, gtk, keylen) <
  650. 0) {
  651. wpa_printf(MSG_WARNING, "WPA: Failed to set GTK to the "
  652. "driver.");
  653. return -1;
  654. }
  655. return 0;
  656. }
  657. #ifdef CONFIG_IEEE80211W
  658. static int wpa_ft_process_igtk_subelem(struct wpa_sm *sm, const u8 *igtk_elem,
  659. size_t igtk_elem_len)
  660. {
  661. u8 igtk[WPA_IGTK_LEN];
  662. u16 keyidx;
  663. if (sm->mgmt_group_cipher != WPA_CIPHER_AES_128_CMAC)
  664. return 0;
  665. if (igtk_elem == NULL) {
  666. wpa_printf(MSG_DEBUG, "FT: No IGTK included in FTIE");
  667. return 0;
  668. }
  669. wpa_hexdump_key(MSG_DEBUG, "FT: Received IGTK in Reassoc Resp",
  670. igtk_elem, igtk_elem_len);
  671. if (igtk_elem_len != 2 + 6 + 1 + WPA_IGTK_LEN + 8) {
  672. wpa_printf(MSG_DEBUG, "FT: Invalid IGTK sub-elem "
  673. "length %lu", (unsigned long) igtk_elem_len);
  674. return -1;
  675. }
  676. if (igtk_elem[8] != WPA_IGTK_LEN) {
  677. wpa_printf(MSG_DEBUG, "FT: Invalid IGTK sub-elem Key Length "
  678. "%d", igtk_elem[8]);
  679. return -1;
  680. }
  681. if (aes_unwrap(sm->ptk.kek, WPA_IGTK_LEN / 8, igtk_elem + 9, igtk)) {
  682. wpa_printf(MSG_WARNING, "FT: AES unwrap failed - could not "
  683. "decrypt IGTK");
  684. return -1;
  685. }
  686. /* KeyID[2] | IPN[6] | Key Length[1] | Key[16+8] */
  687. keyidx = WPA_GET_LE16(igtk_elem);
  688. wpa_hexdump_key(MSG_DEBUG, "FT: IGTK from Reassoc Resp", igtk,
  689. WPA_IGTK_LEN);
  690. if (wpa_sm_set_key(sm, WPA_ALG_IGTK, (u8 *) "\xff\xff\xff\xff\xff\xff",
  691. keyidx, 0, igtk_elem + 2, 6, igtk, WPA_IGTK_LEN) <
  692. 0) {
  693. wpa_printf(MSG_WARNING, "WPA: Failed to set IGTK to the "
  694. "driver.");
  695. return -1;
  696. }
  697. return 0;
  698. }
  699. #endif /* CONFIG_IEEE80211W */
  700. int wpa_ft_validate_reassoc_resp(struct wpa_sm *sm, const u8 *ies,
  701. size_t ies_len, const u8 *src_addr)
  702. {
  703. struct wpa_ft_ies parse;
  704. struct rsn_mdie *mdie;
  705. struct rsn_ftie *ftie;
  706. size_t count;
  707. u8 mic[16];
  708. wpa_hexdump(MSG_DEBUG, "FT: Response IEs", ies, ies_len);
  709. if (sm->key_mgmt != WPA_KEY_MGMT_FT_IEEE8021X &&
  710. sm->key_mgmt != WPA_KEY_MGMT_FT_PSK) {
  711. wpa_printf(MSG_DEBUG, "FT: Reject FT IEs since FT is not "
  712. "enabled for this connection");
  713. return -1;
  714. }
  715. if (wpa_ft_parse_ies(ies, ies_len, &parse) < 0) {
  716. wpa_printf(MSG_DEBUG, "FT: Failed to parse IEs");
  717. return -1;
  718. }
  719. mdie = (struct rsn_mdie *) parse.mdie;
  720. if (mdie == NULL || parse.mdie_len < sizeof(*mdie) ||
  721. os_memcmp(mdie->mobility_domain, sm->mobility_domain,
  722. MOBILITY_DOMAIN_ID_LEN) != 0) {
  723. wpa_printf(MSG_DEBUG, "FT: Invalid MDIE");
  724. return -1;
  725. }
  726. ftie = (struct rsn_ftie *) parse.ftie;
  727. if (ftie == NULL || parse.ftie_len < sizeof(*ftie)) {
  728. wpa_printf(MSG_DEBUG, "FT: Invalid FTIE");
  729. return -1;
  730. }
  731. if (parse.r0kh_id == NULL) {
  732. wpa_printf(MSG_DEBUG, "FT: No R0KH-ID subelem in FTIE");
  733. return -1;
  734. }
  735. if (parse.r0kh_id_len != sm->r0kh_id_len ||
  736. os_memcmp(parse.r0kh_id, sm->r0kh_id, parse.r0kh_id_len) != 0) {
  737. wpa_printf(MSG_DEBUG, "FT: R0KH-ID in FTIE did not match with "
  738. "the current R0KH-ID");
  739. wpa_hexdump(MSG_DEBUG, "FT: R0KH-ID in FTIE",
  740. parse.r0kh_id, parse.r0kh_id_len);
  741. wpa_hexdump(MSG_DEBUG, "FT: The current R0KH-ID",
  742. sm->r0kh_id, sm->r0kh_id_len);
  743. return -1;
  744. }
  745. if (parse.r1kh_id == NULL) {
  746. wpa_printf(MSG_DEBUG, "FT: No R1KH-ID subelem in FTIE");
  747. return -1;
  748. }
  749. if (os_memcmp(parse.r1kh_id, sm->r1kh_id, FT_R1KH_ID_LEN) != 0) {
  750. wpa_printf(MSG_DEBUG, "FT: Unknown R1KH-ID used in "
  751. "ReassocResp");
  752. return -1;
  753. }
  754. if (parse.rsn_pmkid == NULL ||
  755. os_memcmp(parse.rsn_pmkid, sm->pmk_r1_name, WPA_PMK_NAME_LEN)) {
  756. wpa_printf(MSG_DEBUG, "FT: No matching PMKR1Name (PMKID) in "
  757. "RSNIE (pmkid=%d)", !!parse.rsn_pmkid);
  758. return -1;
  759. }
  760. count = 3;
  761. if (parse.tie)
  762. count++;
  763. if (wpa_ft_mic(sm->ptk.kck, sm->own_addr, src_addr, 6,
  764. parse.mdie - 2, parse.mdie_len + 2,
  765. parse.ftie - 2, parse.ftie_len + 2,
  766. parse.rsn - 2, parse.rsn_len + 2,
  767. parse.ric, parse.ric_len,
  768. mic) < 0) {
  769. wpa_printf(MSG_DEBUG, "FT: Failed to calculate MIC");
  770. return -1;
  771. }
  772. if (os_memcmp(mic, ftie->mic, 16) != 0) {
  773. wpa_printf(MSG_DEBUG, "FT: Invalid MIC in FTIE");
  774. wpa_hexdump(MSG_MSGDUMP, "FT: Received MIC", ftie->mic, 16);
  775. wpa_hexdump(MSG_MSGDUMP, "FT: Calculated MIC", mic, 16);
  776. return -1;
  777. }
  778. if (wpa_ft_process_gtk_subelem(sm, parse.gtk, parse.gtk_len) < 0)
  779. return -1;
  780. #ifdef CONFIG_IEEE80211W
  781. if (wpa_ft_process_igtk_subelem(sm, parse.igtk, parse.igtk_len) < 0)
  782. return -1;
  783. #endif /* CONFIG_IEEE80211W */
  784. if (sm->set_ptk_after_assoc) {
  785. wpa_printf(MSG_DEBUG, "FT: Try to set PTK again now that we "
  786. "are associated");
  787. if (wpa_ft_install_ptk(sm, src_addr) < 0)
  788. return -1;
  789. sm->set_ptk_after_assoc = 0;
  790. }
  791. if (parse.ric) {
  792. wpa_hexdump(MSG_MSGDUMP, "FT: RIC Response",
  793. parse.ric, parse.ric_len);
  794. /* TODO: parse response and inform driver about results */
  795. }
  796. return 0;
  797. }
  798. /**
  799. * wpa_ft_start_over_ds - Generate over-the-DS auth request
  800. * @sm: Pointer to WPA state machine data from wpa_sm_init()
  801. * Returns: 0 on success, -1 on failure
  802. */
  803. int wpa_ft_start_over_ds(struct wpa_sm *sm, const u8 *target_ap)
  804. {
  805. u8 *ft_ies;
  806. size_t ft_ies_len;
  807. wpa_printf(MSG_DEBUG, "FT: Request over-the-DS with " MACSTR,
  808. MAC2STR(target_ap));
  809. /* Generate a new SNonce */
  810. if (os_get_random(sm->snonce, WPA_NONCE_LEN)) {
  811. wpa_printf(MSG_INFO, "FT: Failed to generate a new SNonce");
  812. return -1;
  813. }
  814. ft_ies = wpa_ft_gen_req_ies(sm, &ft_ies_len, NULL, sm->pmk_r0_name,
  815. NULL, target_ap, NULL, 0);
  816. if (ft_ies) {
  817. sm->over_the_ds_in_progress = 1;
  818. os_memcpy(sm->target_ap, target_ap, ETH_ALEN);
  819. wpa_sm_send_ft_action(sm, 1, target_ap, ft_ies, ft_ies_len);
  820. os_free(ft_ies);
  821. }
  822. return 0;
  823. }
  824. #endif /* CONFIG_IEEE80211R */