wpa_ft.c 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497
  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. #ifdef CONFIG_IEEE80211W
  295. static inline int wpa_auth_get_seqnum_igtk(struct wpa_authenticator *wpa_auth,
  296. const u8 *addr, int idx, u8 *seq)
  297. {
  298. if (wpa_auth->cb.get_seqnum_igtk == NULL)
  299. return -1;
  300. return wpa_auth->cb.get_seqnum_igtk(wpa_auth->cb.ctx, addr, idx, seq);
  301. }
  302. #endif /* CONFIG_IEEE80211W */
  303. static u8 * wpa_ft_gtk_subelem(struct wpa_state_machine *sm, size_t *len)
  304. {
  305. u8 *subelem;
  306. struct wpa_group *gsm = sm->group;
  307. size_t subelem_len, pad_len;
  308. const u8 *key;
  309. size_t key_len;
  310. u8 keybuf[32];
  311. key_len = gsm->GTK_len;
  312. if (key_len > sizeof(keybuf))
  313. return NULL;
  314. /*
  315. * Pad key for AES Key Wrap if it is not multiple of 8 bytes or is less
  316. * than 16 bytes.
  317. */
  318. pad_len = key_len % 8;
  319. if (pad_len)
  320. pad_len = 8 - pad_len;
  321. if (key_len + pad_len < 16)
  322. pad_len += 8;
  323. if (pad_len) {
  324. os_memcpy(keybuf, gsm->GTK[gsm->GN - 1], key_len);
  325. os_memset(keybuf + key_len, 0, pad_len);
  326. keybuf[key_len] = 0xdd;
  327. key_len += pad_len;
  328. key = keybuf;
  329. } else
  330. key = gsm->GTK[gsm->GN - 1];
  331. /*
  332. * Sub-elem ID[1] | Length[1] | Key Info[1] | Key Length[1] | RSC[8] |
  333. * Key[5..32].
  334. */
  335. subelem_len = 12 + key_len + 8;
  336. subelem = os_zalloc(subelem_len);
  337. if (subelem == NULL)
  338. return NULL;
  339. subelem[0] = FTIE_SUBELEM_GTK;
  340. subelem[1] = 10 + key_len + 8;
  341. subelem[2] = gsm->GN & 0x03; /* Key ID in B0-B1 of Key Info */
  342. subelem[3] = gsm->GTK_len;
  343. wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN, subelem + 4);
  344. if (aes_wrap(sm->PTK.kek, key_len / 8, key, subelem + 12)) {
  345. os_free(subelem);
  346. return NULL;
  347. }
  348. *len = subelem_len;
  349. return subelem;
  350. }
  351. #ifdef CONFIG_IEEE80211W
  352. static u8 * wpa_ft_igtk_subelem(struct wpa_state_machine *sm, size_t *len)
  353. {
  354. u8 *subelem, *pos;
  355. struct wpa_group *gsm = sm->group;
  356. size_t subelem_len;
  357. /* Sub-elem ID[1] | Length[1] | KeyID[2] | PN[6] | Key[16+8] */
  358. subelem_len = 1 + 1 + 2 + 6 + WPA_IGTK_LEN + 8;
  359. subelem = os_zalloc(subelem_len);
  360. if (subelem == NULL)
  361. return NULL;
  362. pos = subelem;
  363. *pos++ = FTIE_SUBELEM_IGTK;
  364. *pos++ = subelem_len - 2;
  365. WPA_PUT_LE16(pos, gsm->GN_igtk);
  366. pos += 2;
  367. wpa_auth_get_seqnum_igtk(sm->wpa_auth, NULL, gsm->GN_igtk, pos);
  368. pos += 6;
  369. if (aes_wrap(sm->PTK.kek, WPA_IGTK_LEN / 8,
  370. gsm->IGTK[gsm->GN_igtk - 4], pos)) {
  371. os_free(subelem);
  372. return NULL;
  373. }
  374. *len = subelem_len;
  375. return subelem;
  376. }
  377. #endif /* CONFIG_IEEE80211W */
  378. u8 * wpa_sm_write_assoc_resp_ies(struct wpa_state_machine *sm, u8 *pos,
  379. size_t max_len, int auth_alg)
  380. {
  381. u8 *end, *mdie, *ftie, *rsnie, *r0kh_id, *subelem = NULL;
  382. size_t mdie_len, ftie_len, rsnie_len, r0kh_id_len, subelem_len = 0;
  383. int res;
  384. struct wpa_auth_config *conf;
  385. struct rsn_ftie *_ftie;
  386. if (sm == NULL)
  387. return pos;
  388. conf = &sm->wpa_auth->conf;
  389. if (sm->wpa_key_mgmt != WPA_KEY_MGMT_FT_IEEE8021X &&
  390. sm->wpa_key_mgmt != WPA_KEY_MGMT_FT_PSK)
  391. return pos;
  392. end = pos + max_len;
  393. /* RSN */
  394. res = wpa_write_rsn_ie(conf, pos, end - pos, sm->pmk_r1_name);
  395. if (res < 0)
  396. return pos;
  397. rsnie = pos;
  398. rsnie_len = res;
  399. pos += res;
  400. /* Mobility Domain Information */
  401. res = wpa_write_mdie(conf, pos, end - pos);
  402. if (res < 0)
  403. return pos;
  404. mdie = pos;
  405. mdie_len = res;
  406. pos += res;
  407. /* Fast BSS Transition Information */
  408. if (auth_alg == WLAN_AUTH_FT) {
  409. subelem = wpa_ft_gtk_subelem(sm, &subelem_len);
  410. r0kh_id = sm->r0kh_id;
  411. r0kh_id_len = sm->r0kh_id_len;
  412. #ifdef CONFIG_IEEE80211W
  413. if (sm->mgmt_frame_prot) {
  414. u8 *igtk;
  415. size_t igtk_len;
  416. u8 *nbuf;
  417. igtk = wpa_ft_igtk_subelem(sm, &igtk_len);
  418. if (igtk == NULL) {
  419. os_free(subelem);
  420. return pos;
  421. }
  422. nbuf = os_realloc(subelem, subelem_len + igtk_len);
  423. if (nbuf == NULL) {
  424. os_free(subelem);
  425. os_free(igtk);
  426. return pos;
  427. }
  428. subelem = nbuf;
  429. os_memcpy(subelem + subelem_len, igtk, igtk_len);
  430. subelem_len += igtk_len;
  431. os_free(igtk);
  432. }
  433. #endif /* CONFIG_IEEE80211W */
  434. } else {
  435. r0kh_id = conf->r0_key_holder;
  436. r0kh_id_len = conf->r0_key_holder_len;
  437. }
  438. res = wpa_write_ftie(conf, r0kh_id, r0kh_id_len, NULL, NULL, pos,
  439. end - pos, subelem, subelem_len);
  440. os_free(subelem);
  441. if (res < 0)
  442. return pos;
  443. ftie = pos;
  444. ftie_len = res;
  445. pos += res;
  446. _ftie = (struct rsn_ftie *) (ftie + 2);
  447. _ftie->mic_control[1] = 3; /* Information element count */
  448. if (wpa_ft_mic(sm->PTK.kck, sm->addr, sm->wpa_auth->addr, 6,
  449. mdie, mdie_len, ftie, ftie_len,
  450. rsnie, rsnie_len, NULL, 0, _ftie->mic) < 0)
  451. wpa_printf(MSG_DEBUG, "FT: Failed to calculate MIC");
  452. return pos;
  453. }
  454. struct wpa_ft_ies {
  455. const u8 *mdie;
  456. size_t mdie_len;
  457. const u8 *ftie;
  458. size_t ftie_len;
  459. const u8 *r1kh_id;
  460. const u8 *gtk;
  461. size_t gtk_len;
  462. const u8 *r0kh_id;
  463. size_t r0kh_id_len;
  464. const u8 *rsn;
  465. size_t rsn_len;
  466. const u8 *rsn_pmkid;
  467. };
  468. static int wpa_ft_parse_ftie(const u8 *ie, size_t ie_len,
  469. struct wpa_ft_ies *parse)
  470. {
  471. const u8 *end, *pos;
  472. parse->ftie = ie;
  473. parse->ftie_len = ie_len;
  474. pos = ie + sizeof(struct rsn_ftie);
  475. end = ie + ie_len;
  476. while (pos + 2 <= end && pos + 2 + pos[1] <= end) {
  477. switch (pos[0]) {
  478. case FTIE_SUBELEM_R1KH_ID:
  479. if (pos[1] != FT_R1KH_ID_LEN) {
  480. wpa_printf(MSG_DEBUG, "FT: Invalid R1KH-ID "
  481. "length in FTIE: %d", pos[1]);
  482. return -1;
  483. }
  484. parse->r1kh_id = pos + 2;
  485. break;
  486. case FTIE_SUBELEM_GTK:
  487. parse->gtk = pos + 2;
  488. parse->gtk_len = pos[1];
  489. break;
  490. case FTIE_SUBELEM_R0KH_ID:
  491. if (pos[1] < 1 || pos[1] > FT_R0KH_ID_MAX_LEN) {
  492. wpa_printf(MSG_DEBUG, "FT: Invalid R0KH-ID "
  493. "length in FTIE: %d", pos[1]);
  494. return -1;
  495. }
  496. parse->r0kh_id = pos + 2;
  497. parse->r0kh_id_len = pos[1];
  498. break;
  499. }
  500. pos += 2 + pos[1];
  501. }
  502. return 0;
  503. }
  504. static int wpa_ft_parse_ies(const u8 *ies, size_t ies_len,
  505. struct wpa_ft_ies *parse)
  506. {
  507. const u8 *end, *pos;
  508. struct wpa_ie_data data;
  509. int ret;
  510. os_memset(parse, 0, sizeof(*parse));
  511. if (ies == NULL)
  512. return 0;
  513. pos = ies;
  514. end = ies + ies_len;
  515. while (pos + 2 <= end && pos + 2 + pos[1] <= end) {
  516. switch (pos[0]) {
  517. case WLAN_EID_RSN:
  518. parse->rsn = pos + 2;
  519. parse->rsn_len = pos[1];
  520. ret = wpa_parse_wpa_ie_rsn(parse->rsn - 2,
  521. parse->rsn_len + 2,
  522. &data);
  523. if (ret < 0) {
  524. wpa_printf(MSG_DEBUG, "FT: Failed to parse "
  525. "RSN IE: %d", ret);
  526. return -1;
  527. }
  528. if (data.num_pmkid == 1 && data.pmkid)
  529. parse->rsn_pmkid = data.pmkid;
  530. break;
  531. case WLAN_EID_MOBILITY_DOMAIN:
  532. parse->mdie = pos + 2;
  533. parse->mdie_len = pos[1];
  534. break;
  535. case WLAN_EID_FAST_BSS_TRANSITION:
  536. if (wpa_ft_parse_ftie(pos + 2, pos[1], parse) < 0)
  537. return -1;
  538. break;
  539. }
  540. pos += 2 + pos[1];
  541. }
  542. return 0;
  543. }
  544. static inline int wpa_auth_set_key(struct wpa_authenticator *wpa_auth,
  545. int vlan_id,
  546. const char *alg, const u8 *addr, int idx,
  547. u8 *key, size_t key_len)
  548. {
  549. if (wpa_auth->cb.set_key == NULL)
  550. return -1;
  551. return wpa_auth->cb.set_key(wpa_auth->cb.ctx, vlan_id, alg, addr, idx,
  552. key, key_len);
  553. }
  554. static void wpa_ft_install_ptk(struct wpa_state_machine *sm)
  555. {
  556. char *alg;
  557. int klen;
  558. /* MLME-SETKEYS.request(PTK) */
  559. if (sm->pairwise == WPA_CIPHER_TKIP) {
  560. alg = "TKIP";
  561. klen = 32;
  562. } else if (sm->pairwise == WPA_CIPHER_CCMP) {
  563. alg = "CCMP";
  564. klen = 16;
  565. } else
  566. return;
  567. /* FIX: add STA entry to kernel/driver here? The set_key will fail
  568. * most likely without this.. At the moment, STA entry is added only
  569. * after association has been completed. Alternatively, could
  570. * re-configure PTK at that point(?).
  571. */
  572. if (wpa_auth_set_key(sm->wpa_auth, 0, alg, sm->addr, 0,
  573. sm->PTK.tk1, klen))
  574. return;
  575. /* FIX: MLME-SetProtection.Request(TA, Tx_Rx) */
  576. sm->pairwise_set = TRUE;
  577. }
  578. static u16 wpa_ft_process_auth_req(struct wpa_state_machine *sm,
  579. const u8 *ies, size_t ies_len,
  580. u8 **resp_ies, size_t *resp_ies_len)
  581. {
  582. struct rsn_mdie *mdie;
  583. struct rsn_ftie *ftie;
  584. u8 pmk_r1[PMK_LEN], pmk_r1_name[WPA_PMK_NAME_LEN];
  585. u8 ptk_name[WPA_PMK_NAME_LEN];
  586. struct wpa_auth_config *conf;
  587. struct wpa_ft_ies parse;
  588. size_t buflen;
  589. int ret;
  590. u8 *pos, *end;
  591. *resp_ies = NULL;
  592. *resp_ies_len = 0;
  593. sm->pmk_r1_name_valid = 0;
  594. conf = &sm->wpa_auth->conf;
  595. wpa_hexdump(MSG_DEBUG, "FT: Received authentication frame IEs",
  596. ies, ies_len);
  597. if (wpa_ft_parse_ies(ies, ies_len, &parse) < 0) {
  598. wpa_printf(MSG_DEBUG, "FT: Failed to parse FT IEs");
  599. return WLAN_STATUS_UNSPECIFIED_FAILURE;
  600. }
  601. mdie = (struct rsn_mdie *) parse.mdie;
  602. if (mdie == NULL || parse.mdie_len < sizeof(*mdie) ||
  603. os_memcmp(mdie->mobility_domain,
  604. sm->wpa_auth->conf.mobility_domain,
  605. MOBILITY_DOMAIN_ID_LEN) != 0) {
  606. wpa_printf(MSG_DEBUG, "FT: Invalid MDIE");
  607. return WLAN_STATUS_INVALID_MDIE;
  608. }
  609. ftie = (struct rsn_ftie *) parse.ftie;
  610. if (ftie == NULL || parse.ftie_len < sizeof(*ftie)) {
  611. wpa_printf(MSG_DEBUG, "FT: Invalid FTIE");
  612. return WLAN_STATUS_INVALID_FTIE;
  613. }
  614. os_memcpy(sm->SNonce, ftie->snonce, WPA_NONCE_LEN);
  615. if (parse.r0kh_id == NULL) {
  616. wpa_printf(MSG_DEBUG, "FT: Invalid FTIE - no R0KH-ID");
  617. return WLAN_STATUS_INVALID_FTIE;
  618. }
  619. wpa_hexdump(MSG_DEBUG, "FT: STA R0KH-ID",
  620. parse.r0kh_id, parse.r0kh_id_len);
  621. os_memcpy(sm->r0kh_id, parse.r0kh_id, parse.r0kh_id_len);
  622. sm->r0kh_id_len = parse.r0kh_id_len;
  623. if (parse.rsn_pmkid == NULL) {
  624. wpa_printf(MSG_DEBUG, "FT: No PMKID in RSNIE");
  625. return WLAN_STATUS_INVALID_PMKID;
  626. }
  627. wpa_hexdump(MSG_DEBUG, "FT: Requested PMKR0Name",
  628. parse.rsn_pmkid, WPA_PMK_NAME_LEN);
  629. wpa_derive_pmk_r1_name(parse.rsn_pmkid,
  630. sm->wpa_auth->conf.r1_key_holder, sm->addr,
  631. pmk_r1_name);
  632. wpa_hexdump(MSG_DEBUG, "FT: Derived requested PMKR1Name",
  633. pmk_r1_name, WPA_PMK_NAME_LEN);
  634. if (wpa_ft_fetch_pmk_r1(sm->wpa_auth, sm->addr, pmk_r1_name, pmk_r1) <
  635. 0) {
  636. if (wpa_ft_pull_pmk_r1(sm->wpa_auth, sm->addr, sm->r0kh_id,
  637. sm->r0kh_id_len, parse.rsn_pmkid) < 0) {
  638. wpa_printf(MSG_DEBUG, "FT: Did not have matching "
  639. "PMK-R1 and unknown R0KH-ID");
  640. return WLAN_STATUS_INVALID_PMKID;
  641. }
  642. /*
  643. * TODO: Should return "status pending" (and the caller should
  644. * not send out response now). The real response will be sent
  645. * once the response from R0KH is received.
  646. */
  647. return WLAN_STATUS_INVALID_PMKID;
  648. }
  649. wpa_hexdump_key(MSG_DEBUG, "FT: Selected PMK-R1", pmk_r1, PMK_LEN);
  650. sm->pmk_r1_name_valid = 1;
  651. os_memcpy(sm->pmk_r1_name, pmk_r1_name, WPA_PMK_NAME_LEN);
  652. if (os_get_random(sm->ANonce, WPA_NONCE_LEN)) {
  653. wpa_printf(MSG_DEBUG, "FT: Failed to get random data for "
  654. "ANonce");
  655. return WLAN_STATUS_UNSPECIFIED_FAILURE;
  656. }
  657. wpa_hexdump(MSG_DEBUG, "FT: Received SNonce",
  658. sm->SNonce, WPA_NONCE_LEN);
  659. wpa_hexdump(MSG_DEBUG, "FT: Generated ANonce",
  660. sm->ANonce, WPA_NONCE_LEN);
  661. wpa_pmk_r1_to_ptk(pmk_r1, sm->SNonce, sm->ANonce, sm->addr,
  662. sm->wpa_auth->addr, pmk_r1_name,
  663. (u8 *) &sm->PTK, sizeof(sm->PTK), ptk_name);
  664. wpa_hexdump_key(MSG_DEBUG, "FT: PTK",
  665. (u8 *) &sm->PTK, sizeof(sm->PTK));
  666. wpa_hexdump(MSG_DEBUG, "FT: PTKName", ptk_name, WPA_PMK_NAME_LEN);
  667. wpa_ft_install_ptk(sm);
  668. buflen = 2 + sizeof(struct rsn_mdie) + 2 + sizeof(struct rsn_ftie) +
  669. 2 + FT_R1KH_ID_LEN + 200;
  670. *resp_ies = os_zalloc(buflen);
  671. if (*resp_ies == NULL) {
  672. return WLAN_STATUS_UNSPECIFIED_FAILURE;
  673. }
  674. pos = *resp_ies;
  675. end = *resp_ies + buflen;
  676. ret = wpa_write_rsn_ie(conf, pos, end - pos, parse.rsn_pmkid);
  677. if (ret < 0) {
  678. os_free(*resp_ies);
  679. *resp_ies = NULL;
  680. return WLAN_STATUS_UNSPECIFIED_FAILURE;
  681. }
  682. pos += ret;
  683. ret = wpa_write_mdie(conf, pos, end - pos);
  684. if (ret < 0) {
  685. os_free(*resp_ies);
  686. *resp_ies = NULL;
  687. return WLAN_STATUS_UNSPECIFIED_FAILURE;
  688. }
  689. pos += ret;
  690. ret = wpa_write_ftie(conf, parse.r0kh_id, parse.r0kh_id_len,
  691. sm->ANonce, sm->SNonce, pos, end - pos, NULL, 0);
  692. if (ret < 0) {
  693. os_free(*resp_ies);
  694. *resp_ies = NULL;
  695. return WLAN_STATUS_UNSPECIFIED_FAILURE;
  696. }
  697. pos += ret;
  698. *resp_ies_len = pos - *resp_ies;
  699. return WLAN_STATUS_SUCCESS;
  700. }
  701. void wpa_ft_process_auth(struct wpa_state_machine *sm, const u8 *bssid,
  702. u16 auth_transaction, const u8 *ies, size_t ies_len,
  703. void (*cb)(void *ctx, const u8 *dst, const u8 *bssid,
  704. u16 auth_transaction, u16 status,
  705. const u8 *ies, size_t ies_len),
  706. void *ctx)
  707. {
  708. u16 status;
  709. u8 *resp_ies;
  710. size_t resp_ies_len;
  711. if (sm == NULL) {
  712. wpa_printf(MSG_DEBUG, "FT: Received authentication frame, but "
  713. "WPA SM not available");
  714. return;
  715. }
  716. wpa_printf(MSG_DEBUG, "FT: Received authentication frame: STA=" MACSTR
  717. " BSSID=" MACSTR " transaction=%d",
  718. MAC2STR(sm->addr), MAC2STR(bssid), auth_transaction);
  719. status = wpa_ft_process_auth_req(sm, ies, ies_len, &resp_ies,
  720. &resp_ies_len);
  721. wpa_printf(MSG_DEBUG, "FT: FT authentication response: dst=" MACSTR
  722. " auth_transaction=%d status=%d",
  723. MAC2STR(sm->addr), auth_transaction + 1, status);
  724. wpa_hexdump(MSG_DEBUG, "FT: Response IEs", resp_ies, resp_ies_len);
  725. cb(ctx, sm->addr, bssid, auth_transaction + 1, status,
  726. resp_ies, resp_ies_len);
  727. os_free(resp_ies);
  728. }
  729. u16 wpa_ft_validate_reassoc(struct wpa_state_machine *sm, const u8 *ies,
  730. size_t ies_len)
  731. {
  732. struct wpa_ft_ies parse;
  733. struct rsn_mdie *mdie;
  734. struct rsn_ftie *ftie;
  735. u8 mic[16];
  736. if (sm == NULL)
  737. return WLAN_STATUS_UNSPECIFIED_FAILURE;
  738. wpa_hexdump(MSG_DEBUG, "FT: Reassoc Req IEs", ies, ies_len);
  739. if (wpa_ft_parse_ies(ies, ies_len, &parse) < 0) {
  740. wpa_printf(MSG_DEBUG, "FT: Failed to parse FT IEs");
  741. return WLAN_STATUS_UNSPECIFIED_FAILURE;
  742. }
  743. if (parse.rsn == NULL) {
  744. wpa_printf(MSG_DEBUG, "FT: No RSNIE in Reassoc Req");
  745. return WLAN_STATUS_UNSPECIFIED_FAILURE;
  746. }
  747. if (parse.rsn_pmkid == NULL) {
  748. wpa_printf(MSG_DEBUG, "FT: No PMKID in RSNIE");
  749. return WLAN_STATUS_INVALID_PMKID;
  750. }
  751. if (os_memcmp(parse.rsn_pmkid, sm->pmk_r1_name, WPA_PMK_NAME_LEN) != 0)
  752. {
  753. wpa_printf(MSG_DEBUG, "FT: PMKID in Reassoc Req did not match "
  754. "with the PMKR1Name derived from auth request");
  755. return WLAN_STATUS_INVALID_PMKID;
  756. }
  757. mdie = (struct rsn_mdie *) parse.mdie;
  758. if (mdie == NULL || parse.mdie_len < sizeof(*mdie) ||
  759. os_memcmp(mdie->mobility_domain,
  760. sm->wpa_auth->conf.mobility_domain,
  761. MOBILITY_DOMAIN_ID_LEN) != 0) {
  762. wpa_printf(MSG_DEBUG, "FT: Invalid MDIE");
  763. return WLAN_STATUS_INVALID_MDIE;
  764. }
  765. ftie = (struct rsn_ftie *) parse.ftie;
  766. if (ftie == NULL || parse.ftie_len < sizeof(*ftie)) {
  767. wpa_printf(MSG_DEBUG, "FT: Invalid FTIE");
  768. return WLAN_STATUS_INVALID_FTIE;
  769. }
  770. /*
  771. * Assume that MDIE, FTIE, and RSN IE are protected and that there is
  772. * no RIC, so total of 3 protected IEs.
  773. */
  774. if (ftie->mic_control[1] != 3) {
  775. wpa_printf(MSG_DEBUG, "FT: Unexpected IE count in FTIE (%d)",
  776. ftie->mic_control[1]);
  777. return WLAN_STATUS_INVALID_FTIE;
  778. }
  779. if (wpa_ft_mic(sm->PTK.kck, sm->addr, sm->wpa_auth->addr, 5,
  780. parse.mdie - 2, parse.mdie_len + 2,
  781. parse.ftie - 2, parse.ftie_len + 2,
  782. parse.rsn - 2, parse.rsn_len + 2, NULL, 0,
  783. mic) < 0) {
  784. wpa_printf(MSG_DEBUG, "FT: Failed to calculate MIC");
  785. return WLAN_STATUS_UNSPECIFIED_FAILURE;
  786. }
  787. if (os_memcmp(mic, ftie->mic, 16) != 0) {
  788. wpa_printf(MSG_DEBUG, "FT: Invalid MIC in FTIE");
  789. wpa_hexdump(MSG_MSGDUMP, "FT: Received MIC", ftie->mic, 16);
  790. wpa_hexdump(MSG_MSGDUMP, "FT: Calculated MIC", mic, 16);
  791. return WLAN_STATUS_INVALID_FTIE;
  792. }
  793. return WLAN_STATUS_SUCCESS;
  794. }
  795. int wpa_ft_action_rx(struct wpa_state_machine *sm, const u8 *data, size_t len)
  796. {
  797. const u8 *sta_addr, *target_ap;
  798. const u8 *ies;
  799. size_t ies_len;
  800. u8 action;
  801. struct ft_rrb_frame *frame;
  802. if (sm == NULL)
  803. return -1;
  804. /*
  805. * data: Category[1] Action[1] STA_Address[6] Target_AP_Address[6]
  806. * FT Request action frame body[variable]
  807. */
  808. if (len < 14) {
  809. wpa_printf(MSG_DEBUG, "FT: Too short FT Action frame "
  810. "(len=%lu)", (unsigned long) len);
  811. return -1;
  812. }
  813. action = data[1];
  814. sta_addr = data + 2;
  815. target_ap = data + 8;
  816. ies = data + 14;
  817. ies_len = len - 14;
  818. wpa_printf(MSG_DEBUG, "FT: Received FT Action frame (STA=" MACSTR
  819. " Target AP=" MACSTR " Action=%d)",
  820. MAC2STR(sta_addr), MAC2STR(target_ap), action);
  821. if (os_memcmp(sta_addr, sm->addr, ETH_ALEN) != 0) {
  822. wpa_printf(MSG_DEBUG, "FT: Mismatch in FT Action STA address: "
  823. "STA=" MACSTR " STA-Address=" MACSTR,
  824. MAC2STR(sm->addr), MAC2STR(sta_addr));
  825. return -1;
  826. }
  827. /*
  828. * Do some sanity checking on the target AP address (not own and not
  829. * broadcast. This could be extended to filter based on a list of known
  830. * APs in the MD (if such a list were configured).
  831. */
  832. if ((target_ap[0] & 0x01) ||
  833. os_memcmp(target_ap, sm->wpa_auth->addr, ETH_ALEN) == 0) {
  834. wpa_printf(MSG_DEBUG, "FT: Invalid Target AP in FT Action "
  835. "frame");
  836. return -1;
  837. }
  838. wpa_hexdump(MSG_MSGDUMP, "FT: Action frame body", ies, ies_len);
  839. /* RRB - Forward action frame to the target AP */
  840. frame = os_malloc(sizeof(*frame) + len);
  841. frame->frame_type = RSN_REMOTE_FRAME_TYPE_FT_RRB;
  842. frame->packet_type = FT_PACKET_REQUEST;
  843. frame->action_length = host_to_le16(len);
  844. os_memcpy(frame->ap_address, sm->wpa_auth->addr, ETH_ALEN);
  845. os_memcpy(frame + 1, data, len);
  846. wpa_ft_rrb_send(sm->wpa_auth, target_ap, (u8 *) frame,
  847. sizeof(*frame) + len);
  848. os_free(frame);
  849. return 0;
  850. }
  851. static int wpa_ft_rrb_rx_request(struct wpa_authenticator *wpa_auth,
  852. const u8 *current_ap, const u8 *sta_addr,
  853. const u8 *body, size_t len)
  854. {
  855. struct wpa_state_machine *sm;
  856. u16 status;
  857. u8 *resp_ies, *pos;
  858. size_t resp_ies_len, rlen;
  859. struct ft_rrb_frame *frame;
  860. sm = wpa_ft_add_sta(wpa_auth, sta_addr);
  861. if (sm == NULL) {
  862. wpa_printf(MSG_DEBUG, "FT: Failed to add new STA based on "
  863. "RRB Request");
  864. return -1;
  865. }
  866. wpa_hexdump(MSG_MSGDUMP, "FT: RRB Request Frame body", body, len);
  867. status = wpa_ft_process_auth_req(sm, body, len, &resp_ies,
  868. &resp_ies_len);
  869. wpa_printf(MSG_DEBUG, "FT: RRB authentication response: STA=" MACSTR
  870. " CurrentAP=" MACSTR " status=%d",
  871. MAC2STR(sm->addr), MAC2STR(current_ap), status);
  872. wpa_hexdump(MSG_DEBUG, "FT: Response IEs", resp_ies, resp_ies_len);
  873. /* RRB - Forward action frame response to the Current AP */
  874. /*
  875. * data: Category[1] Action[1] STA_Address[6] Target_AP_Address[6]
  876. * Status_Code[2] FT Request action frame body[variable]
  877. */
  878. rlen = 2 + 2 * ETH_ALEN + 2 + resp_ies_len;
  879. frame = os_malloc(sizeof(*frame) + rlen);
  880. frame->frame_type = RSN_REMOTE_FRAME_TYPE_FT_RRB;
  881. frame->packet_type = FT_PACKET_RESPONSE;
  882. frame->action_length = host_to_le16(rlen);
  883. os_memcpy(frame->ap_address, wpa_auth->addr, ETH_ALEN);
  884. pos = (u8 *) (frame + 1);
  885. *pos++ = WLAN_ACTION_FT;
  886. *pos++ = 2; /* Action: Response */
  887. os_memcpy(pos, sta_addr, ETH_ALEN);
  888. pos += ETH_ALEN;
  889. os_memcpy(pos, wpa_auth->addr, ETH_ALEN);
  890. pos += ETH_ALEN;
  891. WPA_PUT_LE16(pos, status);
  892. pos += 2;
  893. if (resp_ies) {
  894. os_memcpy(pos, resp_ies, resp_ies_len);
  895. os_free(resp_ies);
  896. }
  897. wpa_ft_rrb_send(wpa_auth, current_ap, (u8 *) frame,
  898. sizeof(*frame) + rlen);
  899. os_free(frame);
  900. return 0;
  901. }
  902. static int wpa_ft_rrb_rx_pull(struct wpa_authenticator *wpa_auth,
  903. const u8 *src_addr,
  904. const u8 *data, size_t data_len)
  905. {
  906. struct ft_r0kh_r1kh_pull_frame *frame, f;
  907. struct ft_remote_r1kh *r1kh;
  908. struct ft_r0kh_r1kh_resp_frame resp, r;
  909. u8 pmk_r0[PMK_LEN];
  910. wpa_printf(MSG_DEBUG, "FT: Received PMK-R1 pull");
  911. if (data_len < sizeof(*frame))
  912. return -1;
  913. r1kh = wpa_auth->conf.r1kh_list;
  914. while (r1kh) {
  915. if (os_memcmp(r1kh->addr, src_addr, ETH_ALEN) == 0)
  916. break;
  917. r1kh = r1kh->next;
  918. }
  919. if (r1kh == NULL) {
  920. wpa_printf(MSG_DEBUG, "FT: No matching R1KH address found for "
  921. "PMK-R1 pull source address " MACSTR,
  922. MAC2STR(src_addr));
  923. return -1;
  924. }
  925. frame = (struct ft_r0kh_r1kh_pull_frame *) data;
  926. /* aes_unwrap() does not support inplace decryption, so use a temporary
  927. * buffer for the data. */
  928. if (aes_unwrap(r1kh->key, (FT_R0KH_R1KH_PULL_DATA_LEN + 7) / 8,
  929. frame->nonce, f.nonce) < 0) {
  930. wpa_printf(MSG_DEBUG, "FT: Failed to decrypt PMK-R1 pull "
  931. "request from " MACSTR, MAC2STR(src_addr));
  932. return -1;
  933. }
  934. wpa_hexdump(MSG_DEBUG, "FT: PMK-R1 pull - nonce",
  935. f.nonce, sizeof(f.nonce));
  936. wpa_hexdump(MSG_DEBUG, "FT: PMK-R1 pull - PMKR0Name",
  937. f.pmk_r0_name, WPA_PMK_NAME_LEN);
  938. wpa_printf(MSG_DEBUG, "FT: PMK-R1 pull - R1KH-ID=" MACSTR "S1KH-ID="
  939. MACSTR, MAC2STR(f.r1kh_id), MAC2STR(f.s1kh_id));
  940. os_memset(&resp, 0, sizeof(resp));
  941. resp.frame_type = RSN_REMOTE_FRAME_TYPE_FT_RRB;
  942. resp.packet_type = FT_PACKET_R0KH_R1KH_RESP;
  943. resp.data_length = host_to_le16(FT_R0KH_R1KH_RESP_DATA_LEN);
  944. os_memcpy(resp.ap_address, wpa_auth->addr, ETH_ALEN);
  945. /* aes_wrap() does not support inplace encryption, so use a temporary
  946. * buffer for the data. */
  947. os_memcpy(r.nonce, f.nonce, sizeof(f.nonce));
  948. os_memcpy(r.r1kh_id, f.r1kh_id, FT_R1KH_ID_LEN);
  949. os_memcpy(r.s1kh_id, f.s1kh_id, ETH_ALEN);
  950. if (wpa_ft_fetch_pmk_r0(wpa_auth, f.s1kh_id, f.pmk_r0_name, pmk_r0) <
  951. 0) {
  952. wpa_printf(MSG_DEBUG, "FT: No matching PMKR0Name found for "
  953. "PMK-R1 pull");
  954. return -1;
  955. }
  956. wpa_derive_pmk_r1(pmk_r0, f.pmk_r0_name, f.r1kh_id, f.s1kh_id,
  957. r.pmk_r1, r.pmk_r1_name);
  958. wpa_hexdump_key(MSG_DEBUG, "FT: PMK-R1", r.pmk_r1, PMK_LEN);
  959. wpa_hexdump(MSG_DEBUG, "FT: PMKR1Name", r.pmk_r1_name,
  960. WPA_PMK_NAME_LEN);
  961. if (aes_wrap(r1kh->key, (FT_R0KH_R1KH_RESP_DATA_LEN + 7) / 8,
  962. r.nonce, resp.nonce) < 0) {
  963. os_memset(pmk_r0, 0, PMK_LEN);
  964. return -1;
  965. }
  966. os_memset(pmk_r0, 0, PMK_LEN);
  967. wpa_ft_rrb_send(wpa_auth, src_addr, (u8 *) &resp, sizeof(resp));
  968. return 0;
  969. }
  970. static int wpa_ft_rrb_rx_resp(struct wpa_authenticator *wpa_auth,
  971. const u8 *src_addr,
  972. const u8 *data, size_t data_len)
  973. {
  974. struct ft_r0kh_r1kh_resp_frame *frame, f;
  975. struct ft_remote_r0kh *r0kh;
  976. wpa_printf(MSG_DEBUG, "FT: Received PMK-R1 pull response");
  977. if (data_len < sizeof(*frame))
  978. return -1;
  979. r0kh = wpa_auth->conf.r0kh_list;
  980. while (r0kh) {
  981. if (os_memcmp(r0kh->addr, src_addr, ETH_ALEN) == 0)
  982. break;
  983. r0kh = r0kh->next;
  984. }
  985. if (r0kh == NULL) {
  986. wpa_printf(MSG_DEBUG, "FT: No matching R0KH address found for "
  987. "PMK-R0 pull response source address " MACSTR,
  988. MAC2STR(src_addr));
  989. return -1;
  990. }
  991. frame = (struct ft_r0kh_r1kh_resp_frame *) data;
  992. /* aes_unwrap() does not support inplace decryption, so use a temporary
  993. * buffer for the data. */
  994. if (aes_unwrap(r0kh->key, (FT_R0KH_R1KH_RESP_DATA_LEN + 7) / 8,
  995. frame->nonce, f.nonce) < 0) {
  996. wpa_printf(MSG_DEBUG, "FT: Failed to decrypt PMK-R1 pull "
  997. "response from " MACSTR, MAC2STR(src_addr));
  998. return -1;
  999. }
  1000. if (os_memcmp(f.r1kh_id, wpa_auth->conf.r1_key_holder, FT_R1KH_ID_LEN)
  1001. != 0) {
  1002. wpa_printf(MSG_DEBUG, "FT: PMK-R1 pull response did not use a "
  1003. "matching R1KH-ID");
  1004. return -1;
  1005. }
  1006. /* TODO: verify that <nonce,s1kh_id> matches with a pending request
  1007. * and call this requests callback function to finish request
  1008. * processing */
  1009. wpa_hexdump(MSG_DEBUG, "FT: PMK-R1 pull - nonce",
  1010. f.nonce, sizeof(f.nonce));
  1011. wpa_printf(MSG_DEBUG, "FT: PMK-R1 pull - R1KH-ID=" MACSTR "S1KH-ID="
  1012. MACSTR, MAC2STR(f.r1kh_id), MAC2STR(f.s1kh_id));
  1013. wpa_hexdump_key(MSG_DEBUG, "FT: PMK-R1 pull - PMK-R1",
  1014. f.pmk_r1, PMK_LEN);
  1015. wpa_hexdump(MSG_DEBUG, "FT: PMK-R1 pull - PMKR1Name",
  1016. f.pmk_r1_name, WPA_PMK_NAME_LEN);
  1017. wpa_ft_store_pmk_r1(wpa_auth, f.s1kh_id, f.pmk_r1, f.pmk_r1_name);
  1018. os_memset(f.pmk_r1, 0, PMK_LEN);
  1019. return 0;
  1020. }
  1021. static int wpa_ft_rrb_rx_push(struct wpa_authenticator *wpa_auth,
  1022. const u8 *src_addr,
  1023. const u8 *data, size_t data_len)
  1024. {
  1025. struct ft_r0kh_r1kh_push_frame *frame, f;
  1026. struct ft_remote_r0kh *r0kh;
  1027. struct os_time now;
  1028. os_time_t tsend;
  1029. wpa_printf(MSG_DEBUG, "FT: Received PMK-R1 push");
  1030. if (data_len < sizeof(*frame))
  1031. return -1;
  1032. r0kh = wpa_auth->conf.r0kh_list;
  1033. while (r0kh) {
  1034. if (os_memcmp(r0kh->addr, src_addr, ETH_ALEN) == 0)
  1035. break;
  1036. r0kh = r0kh->next;
  1037. }
  1038. if (r0kh == NULL) {
  1039. wpa_printf(MSG_DEBUG, "FT: No matching R0KH address found for "
  1040. "PMK-R0 push source address " MACSTR,
  1041. MAC2STR(src_addr));
  1042. return -1;
  1043. }
  1044. frame = (struct ft_r0kh_r1kh_push_frame *) data;
  1045. /* aes_unwrap() does not support inplace decryption, so use a temporary
  1046. * buffer for the data. */
  1047. if (aes_unwrap(r0kh->key, (FT_R0KH_R1KH_PUSH_DATA_LEN + 7) / 8,
  1048. frame->timestamp, f.timestamp) < 0) {
  1049. wpa_printf(MSG_DEBUG, "FT: Failed to decrypt PMK-R1 push from "
  1050. MACSTR, MAC2STR(src_addr));
  1051. return -1;
  1052. }
  1053. os_get_time(&now);
  1054. tsend = WPA_GET_LE32(f.timestamp);
  1055. if ((now.sec > tsend && now.sec - tsend > 60) ||
  1056. (now.sec < tsend && tsend - now.sec > 60)) {
  1057. wpa_printf(MSG_DEBUG, "FT: PMK-R1 push did not have a valid "
  1058. "timestamp: sender time %d own time %d\n",
  1059. (int) tsend, (int) now.sec);
  1060. return -1;
  1061. }
  1062. if (os_memcmp(f.r1kh_id, wpa_auth->conf.r1_key_holder, FT_R1KH_ID_LEN)
  1063. != 0) {
  1064. wpa_printf(MSG_DEBUG, "FT: PMK-R1 push did not use a matching "
  1065. "R1KH-ID (received " MACSTR " own " MACSTR ")",
  1066. MAC2STR(f.r1kh_id),
  1067. MAC2STR(wpa_auth->conf.r1_key_holder));
  1068. return -1;
  1069. }
  1070. wpa_printf(MSG_DEBUG, "FT: PMK-R1 push - R1KH-ID=" MACSTR " S1KH-ID="
  1071. MACSTR, MAC2STR(f.r1kh_id), MAC2STR(f.s1kh_id));
  1072. wpa_hexdump_key(MSG_DEBUG, "FT: PMK-R1 push - PMK-R1",
  1073. f.pmk_r1, PMK_LEN);
  1074. wpa_hexdump(MSG_DEBUG, "FT: PMK-R1 push - PMKR1Name",
  1075. f.pmk_r1_name, WPA_PMK_NAME_LEN);
  1076. wpa_ft_store_pmk_r1(wpa_auth, f.s1kh_id, f.pmk_r1, f.pmk_r1_name);
  1077. os_memset(f.pmk_r1, 0, PMK_LEN);
  1078. return 0;
  1079. }
  1080. int wpa_ft_rrb_rx(struct wpa_authenticator *wpa_auth, const u8 *src_addr,
  1081. const u8 *data, size_t data_len)
  1082. {
  1083. struct ft_rrb_frame *frame;
  1084. u16 alen;
  1085. const u8 *pos, *end, *start;
  1086. u8 action;
  1087. const u8 *sta_addr, *target_ap_addr;
  1088. wpa_printf(MSG_DEBUG, "FT: RRB received frame from remote AP " MACSTR,
  1089. MAC2STR(src_addr));
  1090. if (data_len < sizeof(*frame)) {
  1091. wpa_printf(MSG_DEBUG, "FT: Too short RRB frame (data_len=%lu)",
  1092. (unsigned long) data_len);
  1093. return -1;
  1094. }
  1095. pos = data;
  1096. frame = (struct ft_rrb_frame *) pos;
  1097. pos += sizeof(*frame);
  1098. alen = le_to_host16(frame->action_length);
  1099. wpa_printf(MSG_DEBUG, "FT: RRB frame - frame_type=%d packet_type=%d "
  1100. "action_length=%d ap_address=" MACSTR,
  1101. frame->frame_type, frame->packet_type, alen,
  1102. MAC2STR(frame->ap_address));
  1103. if (frame->frame_type != RSN_REMOTE_FRAME_TYPE_FT_RRB) {
  1104. /* Discard frame per IEEE Std 802.11r-2008, 11A.10.3 */
  1105. wpa_printf(MSG_DEBUG, "FT: RRB discarded frame with "
  1106. "unrecognized type %d", frame->frame_type);
  1107. return -1;
  1108. }
  1109. if (alen > data_len - sizeof(*frame)) {
  1110. wpa_printf(MSG_DEBUG, "FT: RRB frame too short for action "
  1111. "frame");
  1112. return -1;
  1113. }
  1114. if (frame->packet_type == FT_PACKET_R0KH_R1KH_PULL)
  1115. return wpa_ft_rrb_rx_pull(wpa_auth, src_addr, data, data_len);
  1116. if (frame->packet_type == FT_PACKET_R0KH_R1KH_RESP)
  1117. return wpa_ft_rrb_rx_resp(wpa_auth, src_addr, data, data_len);
  1118. if (frame->packet_type == FT_PACKET_R0KH_R1KH_PUSH)
  1119. return wpa_ft_rrb_rx_push(wpa_auth, src_addr, data, data_len);
  1120. wpa_hexdump(MSG_MSGDUMP, "FT: RRB - FT Action frame", pos, alen);
  1121. if (alen < 1 + 1 + 2 * ETH_ALEN) {
  1122. wpa_printf(MSG_DEBUG, "FT: Too short RRB frame (not enough "
  1123. "room for Action Frame body); alen=%lu",
  1124. (unsigned long) alen);
  1125. return -1;
  1126. }
  1127. start = pos;
  1128. end = pos + alen;
  1129. if (*pos != WLAN_ACTION_FT) {
  1130. wpa_printf(MSG_DEBUG, "FT: Unexpected Action frame category "
  1131. "%d", *pos);
  1132. return -1;
  1133. }
  1134. pos++;
  1135. action = *pos++;
  1136. sta_addr = pos;
  1137. pos += ETH_ALEN;
  1138. target_ap_addr = pos;
  1139. pos += ETH_ALEN;
  1140. wpa_printf(MSG_DEBUG, "FT: RRB Action Frame: action=%d sta_addr="
  1141. MACSTR " target_ap_addr=" MACSTR,
  1142. action, MAC2STR(sta_addr), MAC2STR(target_ap_addr));
  1143. if (frame->packet_type == FT_PACKET_REQUEST) {
  1144. wpa_printf(MSG_DEBUG, "FT: FT Packet Type - Request");
  1145. if (action != 1) {
  1146. wpa_printf(MSG_DEBUG, "FT: Unexpected Action %d in "
  1147. "RRB Request", action);
  1148. return -1;
  1149. }
  1150. if (os_memcmp(target_ap_addr, wpa_auth->addr, ETH_ALEN) != 0) {
  1151. wpa_printf(MSG_DEBUG, "FT: Target AP address in the "
  1152. "RRB Request does not match with own "
  1153. "address");
  1154. return -1;
  1155. }
  1156. if (wpa_ft_rrb_rx_request(wpa_auth, frame->ap_address,
  1157. sta_addr, pos, end - pos) < 0)
  1158. return -1;
  1159. } else if (frame->packet_type == FT_PACKET_RESPONSE) {
  1160. u16 status_code;
  1161. if (end - pos < 2) {
  1162. wpa_printf(MSG_DEBUG, "FT: Not enough room for status "
  1163. "code in RRB Response");
  1164. return -1;
  1165. }
  1166. status_code = WPA_GET_LE16(pos);
  1167. pos += 2;
  1168. wpa_printf(MSG_DEBUG, "FT: FT Packet Type - Response "
  1169. "(status_code=%d)", status_code);
  1170. if (wpa_ft_action_send(wpa_auth, sta_addr, start, alen) < 0)
  1171. return -1;
  1172. } else {
  1173. wpa_printf(MSG_DEBUG, "FT: RRB discarded frame with unknown "
  1174. "packet_type %d", frame->packet_type);
  1175. return -1;
  1176. }
  1177. return 0;
  1178. }
  1179. static void wpa_ft_generate_pmk_r1(struct wpa_authenticator *wpa_auth,
  1180. struct wpa_ft_pmk_r0_sa *pmk_r0,
  1181. struct ft_remote_r1kh *r1kh,
  1182. const u8 *s1kh_id)
  1183. {
  1184. struct ft_r0kh_r1kh_push_frame frame, f;
  1185. struct os_time now;
  1186. os_memset(&frame, 0, sizeof(frame));
  1187. frame.frame_type = RSN_REMOTE_FRAME_TYPE_FT_RRB;
  1188. frame.packet_type = FT_PACKET_R0KH_R1KH_PUSH;
  1189. frame.data_length = host_to_le16(FT_R0KH_R1KH_PUSH_DATA_LEN);
  1190. os_memcpy(frame.ap_address, wpa_auth->addr, ETH_ALEN);
  1191. /* aes_wrap() does not support inplace encryption, so use a temporary
  1192. * buffer for the data. */
  1193. os_memcpy(f.r1kh_id, r1kh->id, FT_R1KH_ID_LEN);
  1194. os_memcpy(f.s1kh_id, s1kh_id, ETH_ALEN);
  1195. os_memcpy(f.pmk_r0_name, pmk_r0->pmk_r0_name, WPA_PMK_NAME_LEN);
  1196. wpa_derive_pmk_r1(pmk_r0->pmk_r0, pmk_r0->pmk_r0_name, r1kh->id,
  1197. s1kh_id, f.pmk_r1, f.pmk_r1_name);
  1198. wpa_printf(MSG_DEBUG, "FT: R1KH-ID " MACSTR, MAC2STR(r1kh->id));
  1199. wpa_hexdump_key(MSG_DEBUG, "FT: PMK-R1", f.pmk_r1, PMK_LEN);
  1200. wpa_hexdump(MSG_DEBUG, "FT: PMKR1Name", f.pmk_r1_name,
  1201. WPA_PMK_NAME_LEN);
  1202. os_get_time(&now);
  1203. WPA_PUT_LE32(f.timestamp, now.sec);
  1204. if (aes_wrap(r1kh->key, (FT_R0KH_R1KH_PUSH_DATA_LEN + 7) / 8,
  1205. f.timestamp, frame.timestamp) < 0)
  1206. return;
  1207. wpa_ft_rrb_send(wpa_auth, r1kh->addr, (u8 *) &frame, sizeof(frame));
  1208. }
  1209. void wpa_ft_push_pmk_r1(struct wpa_authenticator *wpa_auth, const u8 *addr)
  1210. {
  1211. struct wpa_ft_pmk_r0_sa *r0;
  1212. struct ft_remote_r1kh *r1kh;
  1213. if (!wpa_auth->conf.pmk_r1_push)
  1214. return;
  1215. r0 = wpa_auth->ft_pmk_cache->pmk_r0;
  1216. while (r0) {
  1217. if (os_memcmp(r0->spa, addr, ETH_ALEN) == 0)
  1218. break;
  1219. r0 = r0->next;
  1220. }
  1221. if (r0 == NULL || r0->pmk_r1_pushed)
  1222. return;
  1223. r0->pmk_r1_pushed = 1;
  1224. wpa_printf(MSG_DEBUG, "FT: Deriving and pushing PMK-R1 keys to R1KHs "
  1225. "for STA " MACSTR, MAC2STR(addr));
  1226. r1kh = wpa_auth->conf.r1kh_list;
  1227. while (r1kh) {
  1228. wpa_ft_generate_pmk_r1(wpa_auth, r0, r1kh, addr);
  1229. r1kh = r1kh->next;
  1230. }
  1231. }
  1232. #endif /* CONFIG_IEEE80211R */