wpa_ft.c 25 KB

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