wpa_ft.c 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432
  1. /*
  2. * hostapd - IEEE 802.11r - Fast BSS Transition
  3. * Copyright (c) 2004-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 "config.h"
  17. #include "wpa.h"
  18. #include "aes_wrap.h"
  19. #include "ieee802_11.h"
  20. #include "defs.h"
  21. #include "wpa_auth_i.h"
  22. #include "wpa_auth_ie.h"
  23. #ifdef CONFIG_IEEE80211R
  24. static int wpa_ft_rrb_send(struct wpa_authenticator *wpa_auth, const u8 *dst,
  25. const u8 *data, size_t data_len)
  26. {
  27. if (wpa_auth->cb.send_ether == NULL)
  28. return -1;
  29. return wpa_auth->cb.send_ether(wpa_auth->cb.ctx, dst, ETH_P_RRB,
  30. data, data_len);
  31. }
  32. static int wpa_ft_action_send(struct wpa_authenticator *wpa_auth,
  33. const u8 *dst, const u8 *data, size_t data_len)
  34. {
  35. if (wpa_auth->cb.send_ft_action == NULL)
  36. return -1;
  37. return wpa_auth->cb.send_ft_action(wpa_auth->cb.ctx, dst,
  38. data, data_len);
  39. }
  40. static struct wpa_state_machine *
  41. wpa_ft_add_sta(struct wpa_authenticator *wpa_auth, const u8 *sta_addr)
  42. {
  43. if (wpa_auth->cb.add_sta == NULL)
  44. return NULL;
  45. return wpa_auth->cb.add_sta(wpa_auth->cb.ctx, sta_addr);
  46. }
  47. int wpa_write_mdie(struct wpa_auth_config *conf, u8 *buf, size_t len)
  48. {
  49. u8 *pos = buf;
  50. u8 capab;
  51. if (len < 2 + sizeof(struct rsn_mdie))
  52. return -1;
  53. *pos++ = WLAN_EID_MOBILITY_DOMAIN;
  54. *pos++ = MOBILITY_DOMAIN_ID_LEN + 1;
  55. os_memcpy(pos, conf->mobility_domain, MOBILITY_DOMAIN_ID_LEN);
  56. pos += MOBILITY_DOMAIN_ID_LEN;
  57. capab = RSN_FT_CAPAB_FT_OVER_DS;
  58. *pos++ = capab;
  59. return pos - buf;
  60. }
  61. static int wpa_write_ftie(struct wpa_auth_config *conf, const u8 *r0kh_id,
  62. size_t r0kh_id_len,
  63. const u8 *anonce, const u8 *snonce,
  64. u8 *buf, size_t len, const u8 *subelem,
  65. size_t subelem_len)
  66. {
  67. u8 *pos = buf, *ielen;
  68. struct rsn_ftie *hdr;
  69. if (len < 2 + sizeof(*hdr) + 2 + FT_R1KH_ID_LEN + 2 + r0kh_id_len +
  70. subelem_len)
  71. return -1;
  72. *pos++ = WLAN_EID_FAST_BSS_TRANSITION;
  73. ielen = pos++;
  74. hdr = (struct rsn_ftie *) pos;
  75. os_memset(hdr, 0, sizeof(*hdr));
  76. pos += sizeof(*hdr);
  77. WPA_PUT_LE16(hdr->mic_control, 0);
  78. if (anonce)
  79. os_memcpy(hdr->anonce, anonce, WPA_NONCE_LEN);
  80. if (snonce)
  81. os_memcpy(hdr->snonce, snonce, WPA_NONCE_LEN);
  82. /* Optional Parameters */
  83. *pos++ = FTIE_SUBELEM_R1KH_ID;
  84. *pos++ = FT_R1KH_ID_LEN;
  85. os_memcpy(pos, conf->r1_key_holder, FT_R1KH_ID_LEN);
  86. pos += FT_R1KH_ID_LEN;
  87. if (r0kh_id) {
  88. *pos++ = FTIE_SUBELEM_R0KH_ID;
  89. *pos++ = r0kh_id_len;
  90. os_memcpy(pos, r0kh_id, r0kh_id_len);
  91. pos += r0kh_id_len;
  92. }
  93. if (subelem) {
  94. os_memcpy(pos, subelem, subelem_len);
  95. pos += subelem_len;
  96. }
  97. *ielen = pos - buf - 2;
  98. return pos - buf;
  99. }
  100. struct wpa_ft_pmk_r0_sa {
  101. struct wpa_ft_pmk_r0_sa *next;
  102. u8 pmk_r0[PMK_LEN];
  103. u8 pmk_r0_name[WPA_PMK_NAME_LEN];
  104. u8 spa[ETH_ALEN];
  105. /* TODO: expiration, identity, radius_class, EAP type, VLAN ID */
  106. int pmk_r1_pushed;
  107. };
  108. struct wpa_ft_pmk_r1_sa {
  109. struct wpa_ft_pmk_r1_sa *next;
  110. u8 pmk_r1[PMK_LEN];
  111. u8 pmk_r1_name[WPA_PMK_NAME_LEN];
  112. u8 spa[ETH_ALEN];
  113. /* TODO: expiration, identity, radius_class, EAP type, VLAN ID */
  114. };
  115. struct wpa_ft_pmk_cache {
  116. struct wpa_ft_pmk_r0_sa *pmk_r0;
  117. struct wpa_ft_pmk_r1_sa *pmk_r1;
  118. };
  119. struct wpa_ft_pmk_cache * wpa_ft_pmk_cache_init(void)
  120. {
  121. struct wpa_ft_pmk_cache *cache;
  122. cache = os_zalloc(sizeof(*cache));
  123. return cache;
  124. }
  125. void wpa_ft_pmk_cache_deinit(struct wpa_ft_pmk_cache *cache)
  126. {
  127. struct wpa_ft_pmk_r0_sa *r0, *r0prev;
  128. struct wpa_ft_pmk_r1_sa *r1, *r1prev;
  129. r0 = cache->pmk_r0;
  130. while (r0) {
  131. r0prev = r0;
  132. r0 = r0->next;
  133. os_memset(r0prev->pmk_r0, 0, PMK_LEN);
  134. os_free(r0prev);
  135. }
  136. r1 = cache->pmk_r1;
  137. while (r1) {
  138. r1prev = r1;
  139. r1 = r1->next;
  140. os_memset(r1prev->pmk_r1, 0, PMK_LEN);
  141. os_free(r1prev);
  142. }
  143. os_free(cache);
  144. }
  145. static int wpa_ft_store_pmk_r0(struct wpa_authenticator *wpa_auth,
  146. const u8 *spa, const u8 *pmk_r0,
  147. const u8 *pmk_r0_name)
  148. {
  149. struct wpa_ft_pmk_cache *cache = wpa_auth->ft_pmk_cache;
  150. struct wpa_ft_pmk_r0_sa *r0;
  151. /* TODO: add expiration and limit on number of entries in cache */
  152. r0 = os_zalloc(sizeof(*r0));
  153. if (r0 == NULL)
  154. return -1;
  155. os_memcpy(r0->pmk_r0, pmk_r0, PMK_LEN);
  156. os_memcpy(r0->pmk_r0_name, pmk_r0_name, WPA_PMK_NAME_LEN);
  157. os_memcpy(r0->spa, spa, ETH_ALEN);
  158. r0->next = cache->pmk_r0;
  159. cache->pmk_r0 = r0;
  160. return 0;
  161. }
  162. static int wpa_ft_fetch_pmk_r0(struct wpa_authenticator *wpa_auth,
  163. const u8 *spa, const u8 *pmk_r0_name,
  164. u8 *pmk_r0)
  165. {
  166. struct wpa_ft_pmk_cache *cache = wpa_auth->ft_pmk_cache;
  167. struct wpa_ft_pmk_r0_sa *r0;
  168. r0 = cache->pmk_r0;
  169. while (r0) {
  170. if (os_memcmp(r0->spa, spa, ETH_ALEN) == 0 &&
  171. os_memcmp(r0->pmk_r0_name, pmk_r0_name, WPA_PMK_NAME_LEN)
  172. == 0) {
  173. os_memcpy(pmk_r0, r0->pmk_r0, PMK_LEN);
  174. return 0;
  175. }
  176. r0 = r0->next;
  177. }
  178. return -1;
  179. }
  180. static int wpa_ft_store_pmk_r1(struct wpa_authenticator *wpa_auth,
  181. const u8 *spa, const u8 *pmk_r1,
  182. const u8 *pmk_r1_name)
  183. {
  184. struct wpa_ft_pmk_cache *cache = wpa_auth->ft_pmk_cache;
  185. struct wpa_ft_pmk_r1_sa *r1;
  186. /* TODO: add expiration and limit on number of entries in cache */
  187. r1 = os_zalloc(sizeof(*r1));
  188. if (r1 == NULL)
  189. return -1;
  190. os_memcpy(r1->pmk_r1, pmk_r1, PMK_LEN);
  191. os_memcpy(r1->pmk_r1_name, pmk_r1_name, WPA_PMK_NAME_LEN);
  192. os_memcpy(r1->spa, spa, ETH_ALEN);
  193. r1->next = cache->pmk_r1;
  194. cache->pmk_r1 = r1;
  195. return 0;
  196. }
  197. static int wpa_ft_fetch_pmk_r1(struct wpa_authenticator *wpa_auth,
  198. const u8 *spa, const u8 *pmk_r1_name,
  199. u8 *pmk_r1)
  200. {
  201. struct wpa_ft_pmk_cache *cache = wpa_auth->ft_pmk_cache;
  202. struct wpa_ft_pmk_r1_sa *r1;
  203. r1 = cache->pmk_r1;
  204. while (r1) {
  205. if (os_memcmp(r1->spa, spa, ETH_ALEN) == 0 &&
  206. os_memcmp(r1->pmk_r1_name, pmk_r1_name, WPA_PMK_NAME_LEN)
  207. == 0) {
  208. os_memcpy(pmk_r1, r1->pmk_r1, PMK_LEN);
  209. return 0;
  210. }
  211. r1 = r1->next;
  212. }
  213. return -1;
  214. }
  215. static int wpa_ft_pull_pmk_r1(struct wpa_authenticator *wpa_auth,
  216. const u8 *s1kh_id, const u8 *r0kh_id,
  217. size_t r0kh_id_len, const u8 *pmk_r0_name)
  218. {
  219. struct ft_remote_r0kh *r0kh;
  220. struct ft_r0kh_r1kh_pull_frame frame, f;
  221. r0kh = wpa_auth->conf.r0kh_list;
  222. while (r0kh) {
  223. if (r0kh->id_len == r0kh_id_len &&
  224. os_memcmp(r0kh->id, r0kh_id, r0kh_id_len) == 0)
  225. break;
  226. r0kh = r0kh->next;
  227. }
  228. if (r0kh == NULL)
  229. return -1;
  230. wpa_printf(MSG_DEBUG, "FT: Send PMK-R1 pull request to remote R0KH "
  231. "address " MACSTR, MAC2STR(r0kh->addr));
  232. os_memset(&frame, 0, sizeof(frame));
  233. frame.frame_type = RSN_REMOTE_FRAME_TYPE_FT_RRB;
  234. frame.packet_type = FT_PACKET_R0KH_R1KH_PULL;
  235. frame.data_length = host_to_le16(FT_R0KH_R1KH_PULL_DATA_LEN);
  236. os_memcpy(frame.ap_address, wpa_auth->addr, ETH_ALEN);
  237. /* aes_wrap() does not support inplace encryption, so use a temporary
  238. * buffer for the data. */
  239. if (os_get_random(f.nonce, sizeof(f.nonce))) {
  240. wpa_printf(MSG_DEBUG, "FT: Failed to get random data for "
  241. "nonce");
  242. return -1;
  243. }
  244. os_memcpy(f.pmk_r0_name, pmk_r0_name, WPA_PMK_NAME_LEN);
  245. os_memcpy(f.r1kh_id, wpa_auth->conf.r1_key_holder, FT_R1KH_ID_LEN);
  246. os_memcpy(f.s1kh_id, s1kh_id, ETH_ALEN);
  247. if (aes_wrap(r0kh->key, (FT_R0KH_R1KH_PULL_DATA_LEN + 7) / 8,
  248. f.nonce, frame.nonce) < 0)
  249. return -1;
  250. wpa_ft_rrb_send(wpa_auth, r0kh->addr, (u8 *) &frame, sizeof(frame));
  251. return 0;
  252. }
  253. int wpa_auth_derive_ptk_ft(struct wpa_state_machine *sm, const u8 *pmk,
  254. struct wpa_ptk *ptk)
  255. {
  256. u8 pmk_r0[PMK_LEN], pmk_r0_name[WPA_PMK_NAME_LEN];
  257. u8 pmk_r1[PMK_LEN], pmk_r1_name[WPA_PMK_NAME_LEN];
  258. u8 ptk_name[WPA_PMK_NAME_LEN];
  259. const u8 *mdid = sm->wpa_auth->conf.mobility_domain;
  260. const u8 *r0kh = sm->wpa_auth->conf.r0_key_holder;
  261. size_t r0kh_len = sm->wpa_auth->conf.r0_key_holder_len;
  262. const u8 *r1kh = sm->wpa_auth->conf.r1_key_holder;
  263. const u8 *ssid = sm->wpa_auth->conf.ssid;
  264. size_t ssid_len = sm->wpa_auth->conf.ssid_len;
  265. if (sm->xxkey_len == 0) {
  266. wpa_printf(MSG_DEBUG, "FT: XXKey not available for key "
  267. "derivation");
  268. return -1;
  269. }
  270. wpa_derive_pmk_r0(sm->xxkey, sm->xxkey_len, ssid, ssid_len, mdid,
  271. r0kh, r0kh_len, sm->addr, pmk_r0, pmk_r0_name);
  272. wpa_hexdump_key(MSG_DEBUG, "FT: PMK-R0", pmk_r0, PMK_LEN);
  273. wpa_hexdump(MSG_DEBUG, "FT: PMKR0Name", pmk_r0_name, WPA_PMK_NAME_LEN);
  274. wpa_ft_store_pmk_r0(sm->wpa_auth, sm->addr, pmk_r0, pmk_r0_name);
  275. wpa_derive_pmk_r1(pmk_r0, pmk_r0_name, r1kh, sm->addr,
  276. pmk_r1, pmk_r1_name);
  277. wpa_hexdump_key(MSG_DEBUG, "FT: PMK-R1", pmk_r1, PMK_LEN);
  278. wpa_hexdump(MSG_DEBUG, "FT: PMKR1Name", pmk_r1_name, WPA_PMK_NAME_LEN);
  279. wpa_ft_store_pmk_r1(sm->wpa_auth, sm->addr, pmk_r1, pmk_r1_name);
  280. wpa_pmk_r1_to_ptk(pmk_r1, sm->SNonce, sm->ANonce, sm->addr,
  281. sm->wpa_auth->addr, pmk_r1_name,
  282. (u8 *) ptk, sizeof(*ptk), ptk_name);
  283. wpa_hexdump_key(MSG_DEBUG, "FT: PTK", (u8 *) ptk, sizeof(*ptk));
  284. wpa_hexdump(MSG_DEBUG, "FT: PTKName", ptk_name, WPA_PMK_NAME_LEN);
  285. return 0;
  286. }
  287. static inline int wpa_auth_get_seqnum(struct wpa_authenticator *wpa_auth,
  288. const u8 *addr, int idx, u8 *seq)
  289. {
  290. if (wpa_auth->cb.get_seqnum == NULL)
  291. return -1;
  292. return wpa_auth->cb.get_seqnum(wpa_auth->cb.ctx, addr, idx, seq);
  293. }
  294. static u8 * wpa_ft_gtk_subelem(struct wpa_state_machine *sm, size_t *len)
  295. {
  296. u8 *subelem;
  297. struct wpa_group *gsm = sm->group;
  298. size_t subelem_len, pad_len;
  299. const u8 *key;
  300. size_t key_len;
  301. u8 keybuf[32];
  302. key_len = gsm->GTK_len;
  303. if (key_len > sizeof(keybuf))
  304. return NULL;
  305. /*
  306. * Pad key for AES Key Wrap if it is not multiple of 8 bytes or is less
  307. * than 16 bytes.
  308. */
  309. pad_len = key_len % 8;
  310. if (pad_len)
  311. pad_len = 8 - pad_len;
  312. if (key_len + pad_len < 16)
  313. pad_len += 8;
  314. if (pad_len) {
  315. os_memcpy(keybuf, gsm->GTK[gsm->GN - 1], key_len);
  316. os_memset(keybuf + key_len, 0, pad_len);
  317. keybuf[key_len] = 0xdd;
  318. key_len += pad_len;
  319. key = keybuf;
  320. } else
  321. key = gsm->GTK[gsm->GN - 1];
  322. /*
  323. * Sub-elem ID[1] | Length[1] | Key Info[1] | Key Length[1] | RSC[8] |
  324. * Key[5..32].
  325. */
  326. subelem_len = 12 + key_len + 8;
  327. subelem = os_zalloc(subelem_len);
  328. if (subelem == NULL)
  329. return NULL;
  330. subelem[0] = FTIE_SUBELEM_GTK;
  331. subelem[1] = 10 + key_len + 8;
  332. subelem[2] = gsm->GN & 0x03; /* Key ID in B0-B1 of Key Info */
  333. subelem[3] = gsm->GTK_len;
  334. wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN, subelem + 4);
  335. if (aes_wrap(sm->PTK.kek, key_len / 8, key, subelem + 12)) {
  336. os_free(subelem);
  337. return NULL;
  338. }
  339. *len = subelem_len;
  340. return subelem;
  341. }
  342. u8 * wpa_sm_write_assoc_resp_ies(struct wpa_state_machine *sm, u8 *pos,
  343. size_t max_len, int auth_alg)
  344. {
  345. u8 *end, *mdie, *ftie, *rsnie, *r0kh_id, *subelem = NULL;
  346. size_t mdie_len, ftie_len, rsnie_len, r0kh_id_len, subelem_len = 0;
  347. int res;
  348. struct wpa_auth_config *conf;
  349. struct rsn_ftie *_ftie;
  350. if (sm == NULL)
  351. return pos;
  352. conf = &sm->wpa_auth->conf;
  353. if (sm->wpa_key_mgmt != WPA_KEY_MGMT_FT_IEEE8021X &&
  354. sm->wpa_key_mgmt != WPA_KEY_MGMT_FT_PSK)
  355. return pos;
  356. end = pos + max_len;
  357. /* RSN */
  358. res = wpa_write_rsn_ie(conf, pos, end - pos, sm->pmk_r1_name);
  359. if (res < 0)
  360. return pos;
  361. rsnie = pos;
  362. rsnie_len = res;
  363. pos += res;
  364. /* Mobility Domain Information */
  365. res = wpa_write_mdie(conf, pos, end - pos);
  366. if (res < 0)
  367. return pos;
  368. mdie = pos;
  369. mdie_len = res;
  370. pos += res;
  371. /* Fast BSS Transition Information */
  372. if (auth_alg == WLAN_AUTH_FT) {
  373. subelem = wpa_ft_gtk_subelem(sm, &subelem_len);
  374. r0kh_id = sm->r0kh_id;
  375. r0kh_id_len = sm->r0kh_id_len;
  376. } else {
  377. r0kh_id = conf->r0_key_holder;
  378. r0kh_id_len = conf->r0_key_holder_len;
  379. }
  380. res = wpa_write_ftie(conf, r0kh_id, r0kh_id_len, NULL, NULL, pos,
  381. end - pos, subelem, subelem_len);
  382. os_free(subelem);
  383. if (res < 0)
  384. return pos;
  385. ftie = pos;
  386. ftie_len = res;
  387. pos += res;
  388. _ftie = (struct rsn_ftie *) (ftie + 2);
  389. _ftie->mic_control[1] = 3; /* Information element count */
  390. if (wpa_ft_mic(sm->PTK.kck, sm->addr, sm->wpa_auth->addr, 6,
  391. mdie, mdie_len, ftie, ftie_len,
  392. rsnie, rsnie_len, NULL, 0, _ftie->mic) < 0)
  393. wpa_printf(MSG_DEBUG, "FT: Failed to calculate MIC");
  394. return pos;
  395. }
  396. struct wpa_ft_ies {
  397. const u8 *mdie;
  398. size_t mdie_len;
  399. const u8 *ftie;
  400. size_t ftie_len;
  401. const u8 *r1kh_id;
  402. const u8 *gtk;
  403. size_t gtk_len;
  404. const u8 *r0kh_id;
  405. size_t r0kh_id_len;
  406. const u8 *rsn;
  407. size_t rsn_len;
  408. const u8 *rsn_pmkid;
  409. };
  410. static int wpa_ft_parse_ftie(const u8 *ie, size_t ie_len,
  411. struct wpa_ft_ies *parse)
  412. {
  413. const u8 *end, *pos;
  414. parse->ftie = ie;
  415. parse->ftie_len = ie_len;
  416. pos = ie + sizeof(struct rsn_ftie);
  417. end = ie + ie_len;
  418. while (pos + 2 <= end && pos + 2 + pos[1] <= end) {
  419. switch (pos[0]) {
  420. case FTIE_SUBELEM_R1KH_ID:
  421. if (pos[1] != FT_R1KH_ID_LEN) {
  422. wpa_printf(MSG_DEBUG, "FT: Invalid R1KH-ID "
  423. "length in FTIE: %d", pos[1]);
  424. return -1;
  425. }
  426. parse->r1kh_id = pos + 2;
  427. break;
  428. case FTIE_SUBELEM_GTK:
  429. parse->gtk = pos + 2;
  430. parse->gtk_len = pos[1];
  431. break;
  432. case FTIE_SUBELEM_R0KH_ID:
  433. if (pos[1] < 1 || pos[1] > FT_R0KH_ID_MAX_LEN) {
  434. wpa_printf(MSG_DEBUG, "FT: Invalid R0KH-ID "
  435. "length in FTIE: %d", pos[1]);
  436. return -1;
  437. }
  438. parse->r0kh_id = pos + 2;
  439. parse->r0kh_id_len = pos[1];
  440. break;
  441. }
  442. pos += 2 + pos[1];
  443. }
  444. return 0;
  445. }
  446. static int wpa_ft_parse_ies(const u8 *ies, size_t ies_len,
  447. struct wpa_ft_ies *parse)
  448. {
  449. const u8 *end, *pos;
  450. struct wpa_ie_data data;
  451. int ret;
  452. os_memset(parse, 0, sizeof(*parse));
  453. if (ies == NULL)
  454. return 0;
  455. pos = ies;
  456. end = ies + ies_len;
  457. while (pos + 2 <= end && pos + 2 + pos[1] <= end) {
  458. switch (pos[0]) {
  459. case WLAN_EID_RSN:
  460. parse->rsn = pos + 2;
  461. parse->rsn_len = pos[1];
  462. ret = wpa_parse_wpa_ie_rsn(parse->rsn - 2,
  463. parse->rsn_len + 2,
  464. &data);
  465. if (ret < 0) {
  466. wpa_printf(MSG_DEBUG, "FT: Failed to parse "
  467. "RSN IE: %d", ret);
  468. return -1;
  469. }
  470. if (data.num_pmkid == 1 && data.pmkid)
  471. parse->rsn_pmkid = data.pmkid;
  472. break;
  473. case WLAN_EID_MOBILITY_DOMAIN:
  474. parse->mdie = pos + 2;
  475. parse->mdie_len = pos[1];
  476. break;
  477. case WLAN_EID_FAST_BSS_TRANSITION:
  478. if (wpa_ft_parse_ftie(pos + 2, pos[1], parse) < 0)
  479. return -1;
  480. break;
  481. }
  482. pos += 2 + pos[1];
  483. }
  484. return 0;
  485. }
  486. static inline int wpa_auth_set_key(struct wpa_authenticator *wpa_auth,
  487. int vlan_id,
  488. const char *alg, const u8 *addr, int idx,
  489. u8 *key, size_t key_len)
  490. {
  491. if (wpa_auth->cb.set_key == NULL)
  492. return -1;
  493. return wpa_auth->cb.set_key(wpa_auth->cb.ctx, vlan_id, alg, addr, idx,
  494. key, key_len);
  495. }
  496. static void wpa_ft_install_ptk(struct wpa_state_machine *sm)
  497. {
  498. char *alg;
  499. int klen;
  500. /* MLME-SETKEYS.request(PTK) */
  501. if (sm->pairwise == WPA_CIPHER_TKIP) {
  502. alg = "TKIP";
  503. klen = 32;
  504. } else if (sm->pairwise == WPA_CIPHER_CCMP) {
  505. alg = "CCMP";
  506. klen = 16;
  507. } else
  508. return;
  509. /* FIX: add STA entry to kernel/driver here? The set_key will fail
  510. * most likely without this.. At the moment, STA entry is added only
  511. * after association has been completed. Alternatively, could
  512. * re-configure PTK at that point(?).
  513. */
  514. if (wpa_auth_set_key(sm->wpa_auth, 0, alg, sm->addr, 0,
  515. sm->PTK.tk1, klen))
  516. return;
  517. /* FIX: MLME-SetProtection.Request(TA, Tx_Rx) */
  518. sm->pairwise_set = TRUE;
  519. }
  520. static u16 wpa_ft_process_auth_req(struct wpa_state_machine *sm,
  521. const u8 *ies, size_t ies_len,
  522. u8 **resp_ies, size_t *resp_ies_len)
  523. {
  524. struct rsn_mdie *mdie;
  525. struct rsn_ftie *ftie;
  526. u8 pmk_r1[PMK_LEN], pmk_r1_name[WPA_PMK_NAME_LEN];
  527. u8 ptk_name[WPA_PMK_NAME_LEN];
  528. struct wpa_auth_config *conf;
  529. struct wpa_ft_ies parse;
  530. size_t buflen;
  531. int ret;
  532. u8 *pos, *end;
  533. *resp_ies = NULL;
  534. *resp_ies_len = 0;
  535. sm->pmk_r1_name_valid = 0;
  536. conf = &sm->wpa_auth->conf;
  537. wpa_hexdump(MSG_DEBUG, "FT: Received authentication frame IEs",
  538. ies, ies_len);
  539. if (wpa_ft_parse_ies(ies, ies_len, &parse) < 0) {
  540. wpa_printf(MSG_DEBUG, "FT: Failed to parse FT IEs");
  541. return WLAN_STATUS_UNSPECIFIED_FAILURE;
  542. }
  543. mdie = (struct rsn_mdie *) parse.mdie;
  544. if (mdie == NULL || parse.mdie_len < sizeof(*mdie) ||
  545. os_memcmp(mdie->mobility_domain,
  546. sm->wpa_auth->conf.mobility_domain,
  547. MOBILITY_DOMAIN_ID_LEN) != 0) {
  548. wpa_printf(MSG_DEBUG, "FT: Invalid MDIE");
  549. return WLAN_STATUS_INVALID_MDIE;
  550. }
  551. ftie = (struct rsn_ftie *) parse.ftie;
  552. if (ftie == NULL || parse.ftie_len < sizeof(*ftie)) {
  553. wpa_printf(MSG_DEBUG, "FT: Invalid FTIE");
  554. return WLAN_STATUS_INVALID_FTIE;
  555. }
  556. os_memcpy(sm->SNonce, ftie->snonce, WPA_NONCE_LEN);
  557. if (parse.r0kh_id == NULL) {
  558. wpa_printf(MSG_DEBUG, "FT: Invalid FTIE - no R0KH-ID");
  559. return WLAN_STATUS_INVALID_FTIE;
  560. }
  561. wpa_hexdump(MSG_DEBUG, "FT: STA R0KH-ID",
  562. parse.r0kh_id, parse.r0kh_id_len);
  563. os_memcpy(sm->r0kh_id, parse.r0kh_id, parse.r0kh_id_len);
  564. sm->r0kh_id_len = parse.r0kh_id_len;
  565. if (parse.rsn_pmkid == NULL) {
  566. wpa_printf(MSG_DEBUG, "FT: No PMKID in RSNIE");
  567. return WLAN_STATUS_INVALID_PMKID;
  568. }
  569. wpa_hexdump(MSG_DEBUG, "FT: Requested PMKR0Name",
  570. parse.rsn_pmkid, WPA_PMK_NAME_LEN);
  571. wpa_derive_pmk_r1_name(parse.rsn_pmkid,
  572. sm->wpa_auth->conf.r1_key_holder, sm->addr,
  573. pmk_r1_name);
  574. wpa_hexdump(MSG_DEBUG, "FT: Derived requested PMKR1Name",
  575. pmk_r1_name, WPA_PMK_NAME_LEN);
  576. if (wpa_ft_fetch_pmk_r1(sm->wpa_auth, sm->addr, pmk_r1_name, pmk_r1) <
  577. 0) {
  578. if (wpa_ft_pull_pmk_r1(sm->wpa_auth, sm->addr, sm->r0kh_id,
  579. sm->r0kh_id_len, parse.rsn_pmkid) < 0) {
  580. wpa_printf(MSG_DEBUG, "FT: Did not have matching "
  581. "PMK-R1 and unknown R0KH-ID");
  582. return WLAN_STATUS_INVALID_PMKID;
  583. }
  584. /*
  585. * TODO: Should return "status pending" (and the caller should
  586. * not send out response now). The real response will be sent
  587. * once the response from R0KH is received.
  588. */
  589. return WLAN_STATUS_INVALID_PMKID;
  590. }
  591. wpa_hexdump_key(MSG_DEBUG, "FT: Selected PMK-R1", pmk_r1, PMK_LEN);
  592. sm->pmk_r1_name_valid = 1;
  593. os_memcpy(sm->pmk_r1_name, pmk_r1_name, WPA_PMK_NAME_LEN);
  594. if (os_get_random(sm->ANonce, WPA_NONCE_LEN)) {
  595. wpa_printf(MSG_DEBUG, "FT: Failed to get random data for "
  596. "ANonce");
  597. return WLAN_STATUS_UNSPECIFIED_FAILURE;
  598. }
  599. wpa_hexdump(MSG_DEBUG, "FT: Received SNonce",
  600. sm->SNonce, WPA_NONCE_LEN);
  601. wpa_hexdump(MSG_DEBUG, "FT: Generated ANonce",
  602. sm->ANonce, WPA_NONCE_LEN);
  603. wpa_pmk_r1_to_ptk(pmk_r1, sm->SNonce, sm->ANonce, sm->addr,
  604. sm->wpa_auth->addr, pmk_r1_name,
  605. (u8 *) &sm->PTK, sizeof(sm->PTK), ptk_name);
  606. wpa_hexdump_key(MSG_DEBUG, "FT: PTK",
  607. (u8 *) &sm->PTK, sizeof(sm->PTK));
  608. wpa_hexdump(MSG_DEBUG, "FT: PTKName", ptk_name, WPA_PMK_NAME_LEN);
  609. wpa_ft_install_ptk(sm);
  610. buflen = 2 + sizeof(struct rsn_mdie) + 2 + sizeof(struct rsn_ftie) +
  611. 2 + FT_R1KH_ID_LEN + 200;
  612. *resp_ies = os_zalloc(buflen);
  613. if (*resp_ies == NULL) {
  614. return WLAN_STATUS_UNSPECIFIED_FAILURE;
  615. }
  616. pos = *resp_ies;
  617. end = *resp_ies + buflen;
  618. ret = wpa_write_rsn_ie(conf, pos, end - pos, parse.rsn_pmkid);
  619. if (ret < 0) {
  620. os_free(*resp_ies);
  621. *resp_ies = NULL;
  622. return WLAN_STATUS_UNSPECIFIED_FAILURE;
  623. }
  624. pos += ret;
  625. ret = wpa_write_mdie(conf, pos, end - pos);
  626. if (ret < 0) {
  627. os_free(*resp_ies);
  628. *resp_ies = NULL;
  629. return WLAN_STATUS_UNSPECIFIED_FAILURE;
  630. }
  631. pos += ret;
  632. ret = wpa_write_ftie(conf, parse.r0kh_id, parse.r0kh_id_len,
  633. sm->ANonce, sm->SNonce, pos, end - pos, NULL, 0);
  634. if (ret < 0) {
  635. os_free(*resp_ies);
  636. *resp_ies = NULL;
  637. return WLAN_STATUS_UNSPECIFIED_FAILURE;
  638. }
  639. pos += ret;
  640. *resp_ies_len = pos - *resp_ies;
  641. return WLAN_STATUS_SUCCESS;
  642. }
  643. void wpa_ft_process_auth(struct wpa_state_machine *sm, const u8 *bssid,
  644. u16 auth_transaction, const u8 *ies, size_t ies_len,
  645. void (*cb)(void *ctx, const u8 *dst, const u8 *bssid,
  646. u16 auth_transaction, u16 status,
  647. const u8 *ies, size_t ies_len),
  648. void *ctx)
  649. {
  650. u16 status;
  651. u8 *resp_ies;
  652. size_t resp_ies_len;
  653. if (sm == NULL) {
  654. wpa_printf(MSG_DEBUG, "FT: Received authentication frame, but "
  655. "WPA SM not available");
  656. return;
  657. }
  658. wpa_printf(MSG_DEBUG, "FT: Received authentication frame: STA=" MACSTR
  659. " BSSID=" MACSTR " transaction=%d",
  660. MAC2STR(sm->addr), MAC2STR(bssid), auth_transaction);
  661. status = wpa_ft_process_auth_req(sm, ies, ies_len, &resp_ies,
  662. &resp_ies_len);
  663. wpa_printf(MSG_DEBUG, "FT: FT authentication response: dst=" MACSTR
  664. " auth_transaction=%d status=%d",
  665. MAC2STR(sm->addr), auth_transaction + 1, status);
  666. wpa_hexdump(MSG_DEBUG, "FT: Response IEs", resp_ies, resp_ies_len);
  667. cb(ctx, sm->addr, bssid, auth_transaction + 1, status,
  668. resp_ies, resp_ies_len);
  669. os_free(resp_ies);
  670. }
  671. u16 wpa_ft_validate_reassoc(struct wpa_state_machine *sm, const u8 *ies,
  672. size_t ies_len)
  673. {
  674. struct wpa_ft_ies parse;
  675. struct rsn_mdie *mdie;
  676. struct rsn_ftie *ftie;
  677. u8 mic[16];
  678. if (sm == NULL)
  679. return WLAN_STATUS_UNSPECIFIED_FAILURE;
  680. wpa_hexdump(MSG_DEBUG, "FT: Reassoc Req IEs", ies, ies_len);
  681. if (wpa_ft_parse_ies(ies, ies_len, &parse) < 0) {
  682. wpa_printf(MSG_DEBUG, "FT: Failed to parse FT IEs");
  683. return WLAN_STATUS_UNSPECIFIED_FAILURE;
  684. }
  685. if (parse.rsn == NULL) {
  686. wpa_printf(MSG_DEBUG, "FT: No RSNIE in Reassoc Req");
  687. return WLAN_STATUS_UNSPECIFIED_FAILURE;
  688. }
  689. if (parse.rsn_pmkid == NULL) {
  690. wpa_printf(MSG_DEBUG, "FT: No PMKID in RSNIE");
  691. return WLAN_STATUS_INVALID_PMKID;
  692. }
  693. if (os_memcmp(parse.rsn_pmkid, sm->pmk_r1_name, WPA_PMK_NAME_LEN) != 0)
  694. {
  695. wpa_printf(MSG_DEBUG, "FT: PMKID in Reassoc Req did not match "
  696. "with the PMKR1Name derived from auth request");
  697. return WLAN_STATUS_INVALID_PMKID;
  698. }
  699. mdie = (struct rsn_mdie *) parse.mdie;
  700. if (mdie == NULL || parse.mdie_len < sizeof(*mdie) ||
  701. os_memcmp(mdie->mobility_domain,
  702. sm->wpa_auth->conf.mobility_domain,
  703. MOBILITY_DOMAIN_ID_LEN) != 0) {
  704. wpa_printf(MSG_DEBUG, "FT: Invalid MDIE");
  705. return WLAN_STATUS_INVALID_MDIE;
  706. }
  707. ftie = (struct rsn_ftie *) parse.ftie;
  708. if (ftie == NULL || parse.ftie_len < sizeof(*ftie)) {
  709. wpa_printf(MSG_DEBUG, "FT: Invalid FTIE");
  710. return WLAN_STATUS_INVALID_FTIE;
  711. }
  712. /*
  713. * Assume that MDIE, FTIE, and RSN IE are protected and that there is
  714. * no RIC, so total of 3 protected IEs.
  715. */
  716. if (ftie->mic_control[1] != 3) {
  717. wpa_printf(MSG_DEBUG, "FT: Unexpected IE count in FTIE (%d)",
  718. ftie->mic_control[1]);
  719. return WLAN_STATUS_INVALID_FTIE;
  720. }
  721. if (wpa_ft_mic(sm->PTK.kck, sm->addr, sm->wpa_auth->addr, 5,
  722. parse.mdie - 2, parse.mdie_len + 2,
  723. parse.ftie - 2, parse.ftie_len + 2,
  724. parse.rsn - 2, parse.rsn_len + 2, NULL, 0,
  725. mic) < 0) {
  726. wpa_printf(MSG_DEBUG, "FT: Failed to calculate MIC");
  727. return WLAN_STATUS_UNSPECIFIED_FAILURE;
  728. }
  729. if (os_memcmp(mic, ftie->mic, 16) != 0) {
  730. wpa_printf(MSG_DEBUG, "FT: Invalid MIC in FTIE");
  731. wpa_hexdump(MSG_MSGDUMP, "FT: Received MIC", ftie->mic, 16);
  732. wpa_hexdump(MSG_MSGDUMP, "FT: Calculated MIC", mic, 16);
  733. return WLAN_STATUS_INVALID_FTIE;
  734. }
  735. return WLAN_STATUS_SUCCESS;
  736. }
  737. int wpa_ft_action_rx(struct wpa_state_machine *sm, const u8 *data, size_t len)
  738. {
  739. const u8 *sta_addr, *target_ap;
  740. const u8 *ies;
  741. size_t ies_len;
  742. u8 action;
  743. struct ft_rrb_frame *frame;
  744. if (sm == NULL)
  745. return -1;
  746. /*
  747. * data: Category[1] Action[1] STA_Address[6] Target_AP_Address[6]
  748. * FT Request action frame body[variable]
  749. */
  750. if (len < 14) {
  751. wpa_printf(MSG_DEBUG, "FT: Too short FT Action frame "
  752. "(len=%lu)", (unsigned long) len);
  753. return -1;
  754. }
  755. action = data[1];
  756. sta_addr = data + 2;
  757. target_ap = data + 8;
  758. ies = data + 14;
  759. ies_len = len - 14;
  760. wpa_printf(MSG_DEBUG, "FT: Received FT Action frame (STA=" MACSTR
  761. " Target AP=" MACSTR " Action=%d)",
  762. MAC2STR(sta_addr), MAC2STR(target_ap), action);
  763. if (os_memcmp(sta_addr, sm->addr, ETH_ALEN) != 0) {
  764. wpa_printf(MSG_DEBUG, "FT: Mismatch in FT Action STA address: "
  765. "STA=" MACSTR " STA-Address=" MACSTR,
  766. MAC2STR(sm->addr), MAC2STR(sta_addr));
  767. return -1;
  768. }
  769. /*
  770. * Do some sanity checking on the target AP address (not own and not
  771. * broadcast. This could be extended to filter based on a list of known
  772. * APs in the MD (if such a list were configured).
  773. */
  774. if ((target_ap[0] & 0x01) ||
  775. os_memcmp(target_ap, sm->wpa_auth->addr, ETH_ALEN) == 0) {
  776. wpa_printf(MSG_DEBUG, "FT: Invalid Target AP in FT Action "
  777. "frame");
  778. return -1;
  779. }
  780. wpa_hexdump(MSG_MSGDUMP, "FT: Action frame body", ies, ies_len);
  781. /* RRB - Forward action frame to the target AP */
  782. frame = os_malloc(sizeof(*frame) + len);
  783. frame->frame_type = RSN_REMOTE_FRAME_TYPE_FT_RRB;
  784. frame->packet_type = FT_PACKET_REQUEST;
  785. frame->action_length = host_to_le16(len);
  786. os_memcpy(frame->ap_address, sm->wpa_auth->addr, ETH_ALEN);
  787. os_memcpy(frame + 1, data, len);
  788. wpa_ft_rrb_send(sm->wpa_auth, target_ap, (u8 *) frame,
  789. sizeof(*frame) + len);
  790. os_free(frame);
  791. return 0;
  792. }
  793. static int wpa_ft_rrb_rx_request(struct wpa_authenticator *wpa_auth,
  794. const u8 *current_ap, const u8 *sta_addr,
  795. const u8 *body, size_t len)
  796. {
  797. struct wpa_state_machine *sm;
  798. u16 status;
  799. u8 *resp_ies, *pos;
  800. size_t resp_ies_len, rlen;
  801. struct ft_rrb_frame *frame;
  802. sm = wpa_ft_add_sta(wpa_auth, sta_addr);
  803. if (sm == NULL) {
  804. wpa_printf(MSG_DEBUG, "FT: Failed to add new STA based on "
  805. "RRB Request");
  806. return -1;
  807. }
  808. wpa_hexdump(MSG_MSGDUMP, "FT: RRB Request Frame body", body, len);
  809. status = wpa_ft_process_auth_req(sm, body, len, &resp_ies,
  810. &resp_ies_len);
  811. wpa_printf(MSG_DEBUG, "FT: RRB authentication response: STA=" MACSTR
  812. " CurrentAP=" MACSTR " status=%d",
  813. MAC2STR(sm->addr), MAC2STR(current_ap), status);
  814. wpa_hexdump(MSG_DEBUG, "FT: Response IEs", resp_ies, resp_ies_len);
  815. /* RRB - Forward action frame response to the Current AP */
  816. /*
  817. * data: Category[1] Action[1] STA_Address[6] Target_AP_Address[6]
  818. * Status_Code[2] FT Request action frame body[variable]
  819. */
  820. rlen = 2 + 2 * ETH_ALEN + 2 + resp_ies_len;
  821. frame = os_malloc(sizeof(*frame) + rlen);
  822. frame->frame_type = RSN_REMOTE_FRAME_TYPE_FT_RRB;
  823. frame->packet_type = FT_PACKET_RESPONSE;
  824. frame->action_length = host_to_le16(rlen);
  825. os_memcpy(frame->ap_address, wpa_auth->addr, ETH_ALEN);
  826. pos = (u8 *) (frame + 1);
  827. *pos++ = WLAN_ACTION_FT;
  828. *pos++ = 2; /* Action: Response */
  829. os_memcpy(pos, sta_addr, ETH_ALEN);
  830. pos += ETH_ALEN;
  831. os_memcpy(pos, wpa_auth->addr, ETH_ALEN);
  832. pos += ETH_ALEN;
  833. WPA_PUT_LE16(pos, status);
  834. pos += 2;
  835. if (resp_ies) {
  836. os_memcpy(pos, resp_ies, resp_ies_len);
  837. os_free(resp_ies);
  838. }
  839. wpa_ft_rrb_send(wpa_auth, current_ap, (u8 *) frame,
  840. sizeof(*frame) + rlen);
  841. os_free(frame);
  842. return 0;
  843. }
  844. static int wpa_ft_rrb_rx_pull(struct wpa_authenticator *wpa_auth,
  845. const u8 *src_addr,
  846. const u8 *data, size_t data_len)
  847. {
  848. struct ft_r0kh_r1kh_pull_frame *frame, f;
  849. struct ft_remote_r1kh *r1kh;
  850. struct ft_r0kh_r1kh_resp_frame resp, r;
  851. u8 pmk_r0[PMK_LEN];
  852. wpa_printf(MSG_DEBUG, "FT: Received PMK-R1 pull");
  853. if (data_len < sizeof(*frame))
  854. return -1;
  855. r1kh = wpa_auth->conf.r1kh_list;
  856. while (r1kh) {
  857. if (os_memcmp(r1kh->addr, src_addr, ETH_ALEN) == 0)
  858. break;
  859. r1kh = r1kh->next;
  860. }
  861. if (r1kh == NULL) {
  862. wpa_printf(MSG_DEBUG, "FT: No matching R1KH address found for "
  863. "PMK-R1 pull source address " MACSTR,
  864. MAC2STR(src_addr));
  865. return -1;
  866. }
  867. frame = (struct ft_r0kh_r1kh_pull_frame *) data;
  868. /* aes_unwrap() does not support inplace decryption, so use a temporary
  869. * buffer for the data. */
  870. if (aes_unwrap(r1kh->key, (FT_R0KH_R1KH_PULL_DATA_LEN + 7) / 8,
  871. frame->nonce, f.nonce) < 0) {
  872. wpa_printf(MSG_DEBUG, "FT: Failed to decrypt PMK-R1 pull "
  873. "request from " MACSTR, MAC2STR(src_addr));
  874. return -1;
  875. }
  876. wpa_hexdump(MSG_DEBUG, "FT: PMK-R1 pull - nonce",
  877. f.nonce, sizeof(f.nonce));
  878. wpa_hexdump(MSG_DEBUG, "FT: PMK-R1 pull - PMKR0Name",
  879. f.pmk_r0_name, WPA_PMK_NAME_LEN);
  880. wpa_printf(MSG_DEBUG, "FT: PMK-R1 pull - R1KH-ID=" MACSTR "S1KH-ID="
  881. MACSTR, MAC2STR(f.r1kh_id), MAC2STR(f.s1kh_id));
  882. os_memset(&resp, 0, sizeof(resp));
  883. resp.frame_type = RSN_REMOTE_FRAME_TYPE_FT_RRB;
  884. resp.packet_type = FT_PACKET_R0KH_R1KH_RESP;
  885. resp.data_length = host_to_le16(FT_R0KH_R1KH_RESP_DATA_LEN);
  886. os_memcpy(resp.ap_address, wpa_auth->addr, ETH_ALEN);
  887. /* aes_wrap() does not support inplace encryption, so use a temporary
  888. * buffer for the data. */
  889. os_memcpy(r.nonce, f.nonce, sizeof(f.nonce));
  890. os_memcpy(r.r1kh_id, f.r1kh_id, FT_R1KH_ID_LEN);
  891. os_memcpy(r.s1kh_id, f.s1kh_id, ETH_ALEN);
  892. if (wpa_ft_fetch_pmk_r0(wpa_auth, f.s1kh_id, f.pmk_r0_name, pmk_r0) <
  893. 0) {
  894. wpa_printf(MSG_DEBUG, "FT: No matching PMKR0Name found for "
  895. "PMK-R1 pull");
  896. return -1;
  897. }
  898. wpa_derive_pmk_r1(pmk_r0, f.pmk_r0_name, f.r1kh_id, f.s1kh_id,
  899. r.pmk_r1, r.pmk_r1_name);
  900. wpa_hexdump_key(MSG_DEBUG, "FT: PMK-R1", r.pmk_r1, PMK_LEN);
  901. wpa_hexdump(MSG_DEBUG, "FT: PMKR1Name", r.pmk_r1_name,
  902. WPA_PMK_NAME_LEN);
  903. if (aes_wrap(r1kh->key, (FT_R0KH_R1KH_RESP_DATA_LEN + 7) / 8,
  904. r.nonce, resp.nonce) < 0) {
  905. os_memset(pmk_r0, 0, PMK_LEN);
  906. return -1;
  907. }
  908. os_memset(pmk_r0, 0, PMK_LEN);
  909. wpa_ft_rrb_send(wpa_auth, src_addr, (u8 *) &resp, sizeof(resp));
  910. return 0;
  911. }
  912. static int wpa_ft_rrb_rx_resp(struct wpa_authenticator *wpa_auth,
  913. const u8 *src_addr,
  914. const u8 *data, size_t data_len)
  915. {
  916. struct ft_r0kh_r1kh_resp_frame *frame, f;
  917. struct ft_remote_r0kh *r0kh;
  918. wpa_printf(MSG_DEBUG, "FT: Received PMK-R1 pull response");
  919. if (data_len < sizeof(*frame))
  920. return -1;
  921. r0kh = wpa_auth->conf.r0kh_list;
  922. while (r0kh) {
  923. if (os_memcmp(r0kh->addr, src_addr, ETH_ALEN) == 0)
  924. break;
  925. r0kh = r0kh->next;
  926. }
  927. if (r0kh == NULL) {
  928. wpa_printf(MSG_DEBUG, "FT: No matching R0KH address found for "
  929. "PMK-R0 pull response source address " MACSTR,
  930. MAC2STR(src_addr));
  931. return -1;
  932. }
  933. frame = (struct ft_r0kh_r1kh_resp_frame *) data;
  934. /* aes_unwrap() does not support inplace decryption, so use a temporary
  935. * buffer for the data. */
  936. if (aes_unwrap(r0kh->key, (FT_R0KH_R1KH_RESP_DATA_LEN + 7) / 8,
  937. frame->nonce, f.nonce) < 0) {
  938. wpa_printf(MSG_DEBUG, "FT: Failed to decrypt PMK-R1 pull "
  939. "response from " MACSTR, MAC2STR(src_addr));
  940. return -1;
  941. }
  942. if (os_memcmp(f.r1kh_id, wpa_auth->conf.r1_key_holder, FT_R1KH_ID_LEN)
  943. != 0) {
  944. wpa_printf(MSG_DEBUG, "FT: PMK-R1 pull response did not use a "
  945. "matching R1KH-ID");
  946. return -1;
  947. }
  948. /* TODO: verify that <nonce,s1kh_id> matches with a pending request
  949. * and call this requests callback function to finish request
  950. * processing */
  951. wpa_hexdump(MSG_DEBUG, "FT: PMK-R1 pull - nonce",
  952. f.nonce, sizeof(f.nonce));
  953. wpa_printf(MSG_DEBUG, "FT: PMK-R1 pull - R1KH-ID=" MACSTR "S1KH-ID="
  954. MACSTR, MAC2STR(f.r1kh_id), MAC2STR(f.s1kh_id));
  955. wpa_hexdump_key(MSG_DEBUG, "FT: PMK-R1 pull - PMK-R1",
  956. f.pmk_r1, PMK_LEN);
  957. wpa_hexdump(MSG_DEBUG, "FT: PMK-R1 pull - PMKR1Name",
  958. f.pmk_r1_name, WPA_PMK_NAME_LEN);
  959. wpa_ft_store_pmk_r1(wpa_auth, f.s1kh_id, f.pmk_r1, f.pmk_r1_name);
  960. os_memset(f.pmk_r1, 0, PMK_LEN);
  961. return 0;
  962. }
  963. static int wpa_ft_rrb_rx_push(struct wpa_authenticator *wpa_auth,
  964. const u8 *src_addr,
  965. const u8 *data, size_t data_len)
  966. {
  967. struct ft_r0kh_r1kh_push_frame *frame, f;
  968. struct ft_remote_r0kh *r0kh;
  969. struct os_time now;
  970. os_time_t tsend;
  971. wpa_printf(MSG_DEBUG, "FT: Received PMK-R1 push");
  972. if (data_len < sizeof(*frame))
  973. return -1;
  974. r0kh = wpa_auth->conf.r0kh_list;
  975. while (r0kh) {
  976. if (os_memcmp(r0kh->addr, src_addr, ETH_ALEN) == 0)
  977. break;
  978. r0kh = r0kh->next;
  979. }
  980. if (r0kh == NULL) {
  981. wpa_printf(MSG_DEBUG, "FT: No matching R0KH address found for "
  982. "PMK-R0 push source address " MACSTR,
  983. MAC2STR(src_addr));
  984. return -1;
  985. }
  986. frame = (struct ft_r0kh_r1kh_push_frame *) data;
  987. /* aes_unwrap() does not support inplace decryption, so use a temporary
  988. * buffer for the data. */
  989. if (aes_unwrap(r0kh->key, (FT_R0KH_R1KH_PUSH_DATA_LEN + 7) / 8,
  990. frame->timestamp, f.timestamp) < 0) {
  991. wpa_printf(MSG_DEBUG, "FT: Failed to decrypt PMK-R1 push from "
  992. MACSTR, MAC2STR(src_addr));
  993. return -1;
  994. }
  995. os_get_time(&now);
  996. tsend = WPA_GET_LE32(f.timestamp);
  997. if ((now.sec > tsend && now.sec - tsend > 60) ||
  998. (now.sec < tsend && tsend - now.sec > 60)) {
  999. wpa_printf(MSG_DEBUG, "FT: PMK-R1 push did not have a valid "
  1000. "timestamp: sender time %d own time %d\n",
  1001. (int) tsend, (int) now.sec);
  1002. return -1;
  1003. }
  1004. if (os_memcmp(f.r1kh_id, wpa_auth->conf.r1_key_holder, FT_R1KH_ID_LEN)
  1005. != 0) {
  1006. wpa_printf(MSG_DEBUG, "FT: PMK-R1 push did not use a matching "
  1007. "R1KH-ID (received " MACSTR " own " MACSTR ")",
  1008. MAC2STR(f.r1kh_id),
  1009. MAC2STR(wpa_auth->conf.r1_key_holder));
  1010. return -1;
  1011. }
  1012. wpa_printf(MSG_DEBUG, "FT: PMK-R1 push - R1KH-ID=" MACSTR " S1KH-ID="
  1013. MACSTR, MAC2STR(f.r1kh_id), MAC2STR(f.s1kh_id));
  1014. wpa_hexdump_key(MSG_DEBUG, "FT: PMK-R1 push - PMK-R1",
  1015. f.pmk_r1, PMK_LEN);
  1016. wpa_hexdump(MSG_DEBUG, "FT: PMK-R1 push - PMKR1Name",
  1017. f.pmk_r1_name, WPA_PMK_NAME_LEN);
  1018. wpa_ft_store_pmk_r1(wpa_auth, f.s1kh_id, f.pmk_r1, f.pmk_r1_name);
  1019. os_memset(f.pmk_r1, 0, PMK_LEN);
  1020. return 0;
  1021. }
  1022. int wpa_ft_rrb_rx(struct wpa_authenticator *wpa_auth, const u8 *src_addr,
  1023. const u8 *data, size_t data_len)
  1024. {
  1025. struct ft_rrb_frame *frame;
  1026. u16 alen;
  1027. const u8 *pos, *end, *start;
  1028. u8 action;
  1029. const u8 *sta_addr, *target_ap_addr;
  1030. wpa_printf(MSG_DEBUG, "FT: RRB received frame from remote AP " MACSTR,
  1031. MAC2STR(src_addr));
  1032. if (data_len < sizeof(*frame)) {
  1033. wpa_printf(MSG_DEBUG, "FT: Too short RRB frame (data_len=%lu)",
  1034. (unsigned long) data_len);
  1035. return -1;
  1036. }
  1037. pos = data;
  1038. frame = (struct ft_rrb_frame *) pos;
  1039. pos += sizeof(*frame);
  1040. alen = le_to_host16(frame->action_length);
  1041. wpa_printf(MSG_DEBUG, "FT: RRB frame - frame_type=%d packet_type=%d "
  1042. "action_length=%d ap_address=" MACSTR,
  1043. frame->frame_type, frame->packet_type, alen,
  1044. MAC2STR(frame->ap_address));
  1045. if (frame->frame_type != RSN_REMOTE_FRAME_TYPE_FT_RRB) {
  1046. /* Discard frame per IEEE 802.11r/D8.0, 10A.10.3 */
  1047. wpa_printf(MSG_DEBUG, "FT: RRB discarded frame with "
  1048. "unrecognized type %d", frame->frame_type);
  1049. return -1;
  1050. }
  1051. if (alen > data_len - sizeof(*frame)) {
  1052. wpa_printf(MSG_DEBUG, "FT: RRB frame too short for action "
  1053. "frame");
  1054. return -1;
  1055. }
  1056. if (frame->packet_type == FT_PACKET_R0KH_R1KH_PULL)
  1057. return wpa_ft_rrb_rx_pull(wpa_auth, src_addr, data, data_len);
  1058. if (frame->packet_type == FT_PACKET_R0KH_R1KH_RESP)
  1059. return wpa_ft_rrb_rx_resp(wpa_auth, src_addr, data, data_len);
  1060. if (frame->packet_type == FT_PACKET_R0KH_R1KH_PUSH)
  1061. return wpa_ft_rrb_rx_push(wpa_auth, src_addr, data, data_len);
  1062. wpa_hexdump(MSG_MSGDUMP, "FT: RRB - FT Action frame", pos, alen);
  1063. if (alen < 1 + 1 + 2 * ETH_ALEN) {
  1064. wpa_printf(MSG_DEBUG, "FT: Too short RRB frame (not enough "
  1065. "room for Action Frame body); alen=%lu",
  1066. (unsigned long) alen);
  1067. return -1;
  1068. }
  1069. start = pos;
  1070. end = pos + alen;
  1071. if (*pos != WLAN_ACTION_FT) {
  1072. wpa_printf(MSG_DEBUG, "FT: Unexpected Action frame category "
  1073. "%d", *pos);
  1074. return -1;
  1075. }
  1076. pos++;
  1077. action = *pos++;
  1078. sta_addr = pos;
  1079. pos += ETH_ALEN;
  1080. target_ap_addr = pos;
  1081. pos += ETH_ALEN;
  1082. wpa_printf(MSG_DEBUG, "FT: RRB Action Frame: action=%d sta_addr="
  1083. MACSTR " target_ap_addr=" MACSTR,
  1084. action, MAC2STR(sta_addr), MAC2STR(target_ap_addr));
  1085. if (frame->packet_type == FT_PACKET_REQUEST) {
  1086. wpa_printf(MSG_DEBUG, "FT: FT Packet Type - Request");
  1087. if (action != 1) {
  1088. wpa_printf(MSG_DEBUG, "FT: Unexpected Action %d in "
  1089. "RRB Request", action);
  1090. return -1;
  1091. }
  1092. if (os_memcmp(target_ap_addr, wpa_auth->addr, ETH_ALEN) != 0) {
  1093. wpa_printf(MSG_DEBUG, "FT: Target AP address in the "
  1094. "RRB Request does not match with own "
  1095. "address");
  1096. return -1;
  1097. }
  1098. if (wpa_ft_rrb_rx_request(wpa_auth, frame->ap_address,
  1099. sta_addr, pos, end - pos) < 0)
  1100. return -1;
  1101. } else if (frame->packet_type == FT_PACKET_RESPONSE) {
  1102. u16 status_code;
  1103. if (end - pos < 2) {
  1104. wpa_printf(MSG_DEBUG, "FT: Not enough room for status "
  1105. "code in RRB Response");
  1106. return -1;
  1107. }
  1108. status_code = WPA_GET_LE16(pos);
  1109. pos += 2;
  1110. wpa_printf(MSG_DEBUG, "FT: FT Packet Type - Response "
  1111. "(status_code=%d)", status_code);
  1112. if (wpa_ft_action_send(wpa_auth, sta_addr, start, alen) < 0)
  1113. return -1;
  1114. } else {
  1115. wpa_printf(MSG_DEBUG, "FT: RRB discarded frame with unknown "
  1116. "packet_type %d", frame->packet_type);
  1117. return -1;
  1118. }
  1119. return 0;
  1120. }
  1121. static void wpa_ft_generate_pmk_r1(struct wpa_authenticator *wpa_auth,
  1122. struct wpa_ft_pmk_r0_sa *pmk_r0,
  1123. struct ft_remote_r1kh *r1kh,
  1124. const u8 *s1kh_id)
  1125. {
  1126. struct ft_r0kh_r1kh_push_frame frame, f;
  1127. struct os_time now;
  1128. os_memset(&frame, 0, sizeof(frame));
  1129. frame.frame_type = RSN_REMOTE_FRAME_TYPE_FT_RRB;
  1130. frame.packet_type = FT_PACKET_R0KH_R1KH_PUSH;
  1131. frame.data_length = host_to_le16(FT_R0KH_R1KH_PUSH_DATA_LEN);
  1132. os_memcpy(frame.ap_address, wpa_auth->addr, ETH_ALEN);
  1133. /* aes_wrap() does not support inplace encryption, so use a temporary
  1134. * buffer for the data. */
  1135. os_memcpy(f.r1kh_id, r1kh->id, FT_R1KH_ID_LEN);
  1136. os_memcpy(f.s1kh_id, s1kh_id, ETH_ALEN);
  1137. os_memcpy(f.pmk_r0_name, pmk_r0->pmk_r0_name, WPA_PMK_NAME_LEN);
  1138. wpa_derive_pmk_r1(pmk_r0->pmk_r0, pmk_r0->pmk_r0_name, r1kh->id,
  1139. s1kh_id, f.pmk_r1, f.pmk_r1_name);
  1140. wpa_printf(MSG_DEBUG, "FT: R1KH-ID " MACSTR, MAC2STR(r1kh->id));
  1141. wpa_hexdump_key(MSG_DEBUG, "FT: PMK-R1", f.pmk_r1, PMK_LEN);
  1142. wpa_hexdump(MSG_DEBUG, "FT: PMKR1Name", f.pmk_r1_name,
  1143. WPA_PMK_NAME_LEN);
  1144. os_get_time(&now);
  1145. WPA_PUT_LE32(f.timestamp, now.sec);
  1146. if (aes_wrap(r1kh->key, (FT_R0KH_R1KH_PUSH_DATA_LEN + 7) / 8,
  1147. f.timestamp, frame.timestamp) < 0)
  1148. return;
  1149. wpa_ft_rrb_send(wpa_auth, r1kh->addr, (u8 *) &frame, sizeof(frame));
  1150. }
  1151. void wpa_ft_push_pmk_r1(struct wpa_authenticator *wpa_auth, const u8 *addr)
  1152. {
  1153. struct wpa_ft_pmk_r0_sa *r0;
  1154. struct ft_remote_r1kh *r1kh;
  1155. if (!wpa_auth->conf.pmk_r1_push)
  1156. return;
  1157. r0 = wpa_auth->ft_pmk_cache->pmk_r0;
  1158. while (r0) {
  1159. if (os_memcmp(r0->spa, addr, ETH_ALEN) == 0)
  1160. break;
  1161. r0 = r0->next;
  1162. }
  1163. if (r0 == NULL || r0->pmk_r1_pushed)
  1164. return;
  1165. r0->pmk_r1_pushed = 1;
  1166. wpa_printf(MSG_DEBUG, "FT: Deriving and pushing PMK-R1 keys to R1KHs "
  1167. "for STA " MACSTR, MAC2STR(addr));
  1168. r1kh = wpa_auth->conf.r1kh_list;
  1169. while (r1kh) {
  1170. wpa_ft_generate_pmk_r1(wpa_auth, r0, r1kh, addr);
  1171. r1kh = r1kh->next;
  1172. }
  1173. }
  1174. #endif /* CONFIG_IEEE80211R */