wpa_auth_ft.c 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776
  1. /*
  2. * hostapd - IEEE 802.11r - Fast BSS Transition
  3. * Copyright (c) 2004-2009, 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 "utils/includes.h"
  15. #include "utils/common.h"
  16. #include "common/ieee802_11_defs.h"
  17. #include "common/ieee802_11_common.h"
  18. #include "crypto/aes_wrap.h"
  19. #include "ap_config.h"
  20. #include "ieee802_11.h"
  21. #include "wmm.h"
  22. #include "wpa_auth.h"
  23. #include "wpa_auth_i.h"
  24. #include "wpa_auth_ie.h"
  25. #ifdef CONFIG_IEEE80211R
  26. struct wpa_ft_ies {
  27. const u8 *mdie;
  28. size_t mdie_len;
  29. const u8 *ftie;
  30. size_t ftie_len;
  31. const u8 *r1kh_id;
  32. const u8 *gtk;
  33. size_t gtk_len;
  34. const u8 *r0kh_id;
  35. size_t r0kh_id_len;
  36. const u8 *rsn;
  37. size_t rsn_len;
  38. const u8 *rsn_pmkid;
  39. const u8 *ric;
  40. size_t ric_len;
  41. };
  42. static int wpa_ft_parse_ies(const u8 *ies, size_t ies_len,
  43. struct wpa_ft_ies *parse);
  44. static int wpa_ft_rrb_send(struct wpa_authenticator *wpa_auth, const u8 *dst,
  45. const u8 *data, size_t data_len)
  46. {
  47. if (wpa_auth->cb.send_ether == NULL)
  48. return -1;
  49. wpa_printf(MSG_DEBUG, "FT: RRB send to " MACSTR, MAC2STR(dst));
  50. return wpa_auth->cb.send_ether(wpa_auth->cb.ctx, dst, ETH_P_RRB,
  51. data, data_len);
  52. }
  53. static int wpa_ft_action_send(struct wpa_authenticator *wpa_auth,
  54. const u8 *dst, const u8 *data, size_t data_len)
  55. {
  56. if (wpa_auth->cb.send_ft_action == NULL)
  57. return -1;
  58. return wpa_auth->cb.send_ft_action(wpa_auth->cb.ctx, dst,
  59. data, data_len);
  60. }
  61. static struct wpa_state_machine *
  62. wpa_ft_add_sta(struct wpa_authenticator *wpa_auth, const u8 *sta_addr)
  63. {
  64. if (wpa_auth->cb.add_sta == NULL)
  65. return NULL;
  66. return wpa_auth->cb.add_sta(wpa_auth->cb.ctx, sta_addr);
  67. }
  68. int wpa_write_mdie(struct wpa_auth_config *conf, u8 *buf, size_t len)
  69. {
  70. u8 *pos = buf;
  71. u8 capab;
  72. if (len < 2 + sizeof(struct rsn_mdie))
  73. return -1;
  74. *pos++ = WLAN_EID_MOBILITY_DOMAIN;
  75. *pos++ = MOBILITY_DOMAIN_ID_LEN + 1;
  76. os_memcpy(pos, conf->mobility_domain, MOBILITY_DOMAIN_ID_LEN);
  77. pos += MOBILITY_DOMAIN_ID_LEN;
  78. capab = RSN_FT_CAPAB_FT_OVER_DS;
  79. *pos++ = capab;
  80. return pos - buf;
  81. }
  82. int wpa_write_ftie(struct wpa_auth_config *conf, const u8 *r0kh_id,
  83. size_t r0kh_id_len,
  84. const u8 *anonce, const u8 *snonce,
  85. u8 *buf, size_t len, const u8 *subelem,
  86. size_t subelem_len)
  87. {
  88. u8 *pos = buf, *ielen;
  89. struct rsn_ftie *hdr;
  90. if (len < 2 + sizeof(*hdr) + 2 + FT_R1KH_ID_LEN + 2 + r0kh_id_len +
  91. subelem_len)
  92. return -1;
  93. *pos++ = WLAN_EID_FAST_BSS_TRANSITION;
  94. ielen = pos++;
  95. hdr = (struct rsn_ftie *) pos;
  96. os_memset(hdr, 0, sizeof(*hdr));
  97. pos += sizeof(*hdr);
  98. WPA_PUT_LE16(hdr->mic_control, 0);
  99. if (anonce)
  100. os_memcpy(hdr->anonce, anonce, WPA_NONCE_LEN);
  101. if (snonce)
  102. os_memcpy(hdr->snonce, snonce, WPA_NONCE_LEN);
  103. /* Optional Parameters */
  104. *pos++ = FTIE_SUBELEM_R1KH_ID;
  105. *pos++ = FT_R1KH_ID_LEN;
  106. os_memcpy(pos, conf->r1_key_holder, FT_R1KH_ID_LEN);
  107. pos += FT_R1KH_ID_LEN;
  108. if (r0kh_id) {
  109. *pos++ = FTIE_SUBELEM_R0KH_ID;
  110. *pos++ = r0kh_id_len;
  111. os_memcpy(pos, r0kh_id, r0kh_id_len);
  112. pos += r0kh_id_len;
  113. }
  114. if (subelem) {
  115. os_memcpy(pos, subelem, subelem_len);
  116. pos += subelem_len;
  117. }
  118. *ielen = pos - buf - 2;
  119. return pos - buf;
  120. }
  121. struct wpa_ft_pmk_r0_sa {
  122. struct wpa_ft_pmk_r0_sa *next;
  123. u8 pmk_r0[PMK_LEN];
  124. u8 pmk_r0_name[WPA_PMK_NAME_LEN];
  125. u8 spa[ETH_ALEN];
  126. int pairwise; /* Pairwise cipher suite, WPA_CIPHER_* */
  127. /* TODO: expiration, identity, radius_class, EAP type, VLAN ID */
  128. int pmk_r1_pushed;
  129. };
  130. struct wpa_ft_pmk_r1_sa {
  131. struct wpa_ft_pmk_r1_sa *next;
  132. u8 pmk_r1[PMK_LEN];
  133. u8 pmk_r1_name[WPA_PMK_NAME_LEN];
  134. u8 spa[ETH_ALEN];
  135. int pairwise; /* Pairwise cipher suite, WPA_CIPHER_* */
  136. /* TODO: expiration, identity, radius_class, EAP type, VLAN ID */
  137. };
  138. struct wpa_ft_pmk_cache {
  139. struct wpa_ft_pmk_r0_sa *pmk_r0;
  140. struct wpa_ft_pmk_r1_sa *pmk_r1;
  141. };
  142. struct wpa_ft_pmk_cache * wpa_ft_pmk_cache_init(void)
  143. {
  144. struct wpa_ft_pmk_cache *cache;
  145. cache = os_zalloc(sizeof(*cache));
  146. return cache;
  147. }
  148. void wpa_ft_pmk_cache_deinit(struct wpa_ft_pmk_cache *cache)
  149. {
  150. struct wpa_ft_pmk_r0_sa *r0, *r0prev;
  151. struct wpa_ft_pmk_r1_sa *r1, *r1prev;
  152. r0 = cache->pmk_r0;
  153. while (r0) {
  154. r0prev = r0;
  155. r0 = r0->next;
  156. os_memset(r0prev->pmk_r0, 0, PMK_LEN);
  157. os_free(r0prev);
  158. }
  159. r1 = cache->pmk_r1;
  160. while (r1) {
  161. r1prev = r1;
  162. r1 = r1->next;
  163. os_memset(r1prev->pmk_r1, 0, PMK_LEN);
  164. os_free(r1prev);
  165. }
  166. os_free(cache);
  167. }
  168. static int wpa_ft_store_pmk_r0(struct wpa_authenticator *wpa_auth,
  169. const u8 *spa, const u8 *pmk_r0,
  170. const u8 *pmk_r0_name, int pairwise)
  171. {
  172. struct wpa_ft_pmk_cache *cache = wpa_auth->ft_pmk_cache;
  173. struct wpa_ft_pmk_r0_sa *r0;
  174. /* TODO: add expiration and limit on number of entries in cache */
  175. r0 = os_zalloc(sizeof(*r0));
  176. if (r0 == NULL)
  177. return -1;
  178. os_memcpy(r0->pmk_r0, pmk_r0, PMK_LEN);
  179. os_memcpy(r0->pmk_r0_name, pmk_r0_name, WPA_PMK_NAME_LEN);
  180. os_memcpy(r0->spa, spa, ETH_ALEN);
  181. r0->pairwise = pairwise;
  182. r0->next = cache->pmk_r0;
  183. cache->pmk_r0 = r0;
  184. return 0;
  185. }
  186. static int wpa_ft_fetch_pmk_r0(struct wpa_authenticator *wpa_auth,
  187. const u8 *spa, const u8 *pmk_r0_name,
  188. u8 *pmk_r0, int *pairwise)
  189. {
  190. struct wpa_ft_pmk_cache *cache = wpa_auth->ft_pmk_cache;
  191. struct wpa_ft_pmk_r0_sa *r0;
  192. r0 = cache->pmk_r0;
  193. while (r0) {
  194. if (os_memcmp(r0->spa, spa, ETH_ALEN) == 0 &&
  195. os_memcmp(r0->pmk_r0_name, pmk_r0_name, WPA_PMK_NAME_LEN)
  196. == 0) {
  197. os_memcpy(pmk_r0, r0->pmk_r0, PMK_LEN);
  198. if (pairwise)
  199. *pairwise = r0->pairwise;
  200. return 0;
  201. }
  202. r0 = r0->next;
  203. }
  204. return -1;
  205. }
  206. static int wpa_ft_store_pmk_r1(struct wpa_authenticator *wpa_auth,
  207. const u8 *spa, const u8 *pmk_r1,
  208. const u8 *pmk_r1_name, int pairwise)
  209. {
  210. struct wpa_ft_pmk_cache *cache = wpa_auth->ft_pmk_cache;
  211. struct wpa_ft_pmk_r1_sa *r1;
  212. /* TODO: add expiration and limit on number of entries in cache */
  213. r1 = os_zalloc(sizeof(*r1));
  214. if (r1 == NULL)
  215. return -1;
  216. os_memcpy(r1->pmk_r1, pmk_r1, PMK_LEN);
  217. os_memcpy(r1->pmk_r1_name, pmk_r1_name, WPA_PMK_NAME_LEN);
  218. os_memcpy(r1->spa, spa, ETH_ALEN);
  219. r1->pairwise = pairwise;
  220. r1->next = cache->pmk_r1;
  221. cache->pmk_r1 = r1;
  222. return 0;
  223. }
  224. static int wpa_ft_fetch_pmk_r1(struct wpa_authenticator *wpa_auth,
  225. const u8 *spa, const u8 *pmk_r1_name,
  226. u8 *pmk_r1, int *pairwise)
  227. {
  228. struct wpa_ft_pmk_cache *cache = wpa_auth->ft_pmk_cache;
  229. struct wpa_ft_pmk_r1_sa *r1;
  230. r1 = cache->pmk_r1;
  231. while (r1) {
  232. if (os_memcmp(r1->spa, spa, ETH_ALEN) == 0 &&
  233. os_memcmp(r1->pmk_r1_name, pmk_r1_name, WPA_PMK_NAME_LEN)
  234. == 0) {
  235. os_memcpy(pmk_r1, r1->pmk_r1, PMK_LEN);
  236. if (pairwise)
  237. *pairwise = r1->pairwise;
  238. return 0;
  239. }
  240. r1 = r1->next;
  241. }
  242. return -1;
  243. }
  244. static int wpa_ft_pull_pmk_r1(struct wpa_authenticator *wpa_auth,
  245. const u8 *s1kh_id, const u8 *r0kh_id,
  246. size_t r0kh_id_len, const u8 *pmk_r0_name)
  247. {
  248. struct ft_remote_r0kh *r0kh;
  249. struct ft_r0kh_r1kh_pull_frame frame, f;
  250. r0kh = wpa_auth->conf.r0kh_list;
  251. while (r0kh) {
  252. if (r0kh->id_len == r0kh_id_len &&
  253. os_memcmp(r0kh->id, r0kh_id, r0kh_id_len) == 0)
  254. break;
  255. r0kh = r0kh->next;
  256. }
  257. if (r0kh == NULL)
  258. return -1;
  259. wpa_printf(MSG_DEBUG, "FT: Send PMK-R1 pull request to remote R0KH "
  260. "address " MACSTR, MAC2STR(r0kh->addr));
  261. os_memset(&frame, 0, sizeof(frame));
  262. frame.frame_type = RSN_REMOTE_FRAME_TYPE_FT_RRB;
  263. frame.packet_type = FT_PACKET_R0KH_R1KH_PULL;
  264. frame.data_length = host_to_le16(FT_R0KH_R1KH_PULL_DATA_LEN);
  265. os_memcpy(frame.ap_address, wpa_auth->addr, ETH_ALEN);
  266. /* aes_wrap() does not support inplace encryption, so use a temporary
  267. * buffer for the data. */
  268. if (os_get_random(f.nonce, sizeof(f.nonce))) {
  269. wpa_printf(MSG_DEBUG, "FT: Failed to get random data for "
  270. "nonce");
  271. return -1;
  272. }
  273. os_memcpy(f.pmk_r0_name, pmk_r0_name, WPA_PMK_NAME_LEN);
  274. os_memcpy(f.r1kh_id, wpa_auth->conf.r1_key_holder, FT_R1KH_ID_LEN);
  275. os_memcpy(f.s1kh_id, s1kh_id, ETH_ALEN);
  276. if (aes_wrap(r0kh->key, (FT_R0KH_R1KH_PULL_DATA_LEN + 7) / 8,
  277. f.nonce, frame.nonce) < 0)
  278. return -1;
  279. wpa_ft_rrb_send(wpa_auth, r0kh->addr, (u8 *) &frame, sizeof(frame));
  280. return 0;
  281. }
  282. int wpa_auth_derive_ptk_ft(struct wpa_state_machine *sm, const u8 *pmk,
  283. struct wpa_ptk *ptk, size_t ptk_len)
  284. {
  285. u8 pmk_r0[PMK_LEN], pmk_r0_name[WPA_PMK_NAME_LEN];
  286. u8 pmk_r1[PMK_LEN];
  287. u8 ptk_name[WPA_PMK_NAME_LEN];
  288. const u8 *mdid = sm->wpa_auth->conf.mobility_domain;
  289. const u8 *r0kh = sm->wpa_auth->conf.r0_key_holder;
  290. size_t r0kh_len = sm->wpa_auth->conf.r0_key_holder_len;
  291. const u8 *r1kh = sm->wpa_auth->conf.r1_key_holder;
  292. const u8 *ssid = sm->wpa_auth->conf.ssid;
  293. size_t ssid_len = sm->wpa_auth->conf.ssid_len;
  294. if (sm->xxkey_len == 0) {
  295. wpa_printf(MSG_DEBUG, "FT: XXKey not available for key "
  296. "derivation");
  297. return -1;
  298. }
  299. wpa_derive_pmk_r0(sm->xxkey, sm->xxkey_len, ssid, ssid_len, mdid,
  300. r0kh, r0kh_len, sm->addr, pmk_r0, pmk_r0_name);
  301. wpa_hexdump_key(MSG_DEBUG, "FT: PMK-R0", pmk_r0, PMK_LEN);
  302. wpa_hexdump(MSG_DEBUG, "FT: PMKR0Name", pmk_r0_name, WPA_PMK_NAME_LEN);
  303. wpa_ft_store_pmk_r0(sm->wpa_auth, sm->addr, pmk_r0, pmk_r0_name,
  304. sm->pairwise);
  305. wpa_derive_pmk_r1(pmk_r0, pmk_r0_name, r1kh, sm->addr,
  306. pmk_r1, sm->pmk_r1_name);
  307. wpa_hexdump_key(MSG_DEBUG, "FT: PMK-R1", pmk_r1, PMK_LEN);
  308. wpa_hexdump(MSG_DEBUG, "FT: PMKR1Name", sm->pmk_r1_name,
  309. WPA_PMK_NAME_LEN);
  310. wpa_ft_store_pmk_r1(sm->wpa_auth, sm->addr, pmk_r1, sm->pmk_r1_name,
  311. sm->pairwise);
  312. wpa_pmk_r1_to_ptk(pmk_r1, sm->SNonce, sm->ANonce, sm->addr,
  313. sm->wpa_auth->addr, sm->pmk_r1_name,
  314. (u8 *) ptk, ptk_len, ptk_name);
  315. wpa_hexdump_key(MSG_DEBUG, "FT: PTK", (u8 *) ptk, ptk_len);
  316. wpa_hexdump(MSG_DEBUG, "FT: PTKName", ptk_name, WPA_PMK_NAME_LEN);
  317. return 0;
  318. }
  319. static inline int wpa_auth_get_seqnum(struct wpa_authenticator *wpa_auth,
  320. const u8 *addr, int idx, u8 *seq)
  321. {
  322. if (wpa_auth->cb.get_seqnum == NULL)
  323. return -1;
  324. return wpa_auth->cb.get_seqnum(wpa_auth->cb.ctx, addr, idx, seq);
  325. }
  326. static u8 * wpa_ft_gtk_subelem(struct wpa_state_machine *sm, size_t *len)
  327. {
  328. u8 *subelem;
  329. struct wpa_group *gsm = sm->group;
  330. size_t subelem_len, pad_len;
  331. const u8 *key;
  332. size_t key_len;
  333. u8 keybuf[32];
  334. key_len = gsm->GTK_len;
  335. if (key_len > sizeof(keybuf))
  336. return NULL;
  337. /*
  338. * Pad key for AES Key Wrap if it is not multiple of 8 bytes or is less
  339. * than 16 bytes.
  340. */
  341. pad_len = key_len % 8;
  342. if (pad_len)
  343. pad_len = 8 - pad_len;
  344. if (key_len + pad_len < 16)
  345. pad_len += 8;
  346. if (pad_len) {
  347. os_memcpy(keybuf, gsm->GTK[gsm->GN - 1], key_len);
  348. os_memset(keybuf + key_len, 0, pad_len);
  349. keybuf[key_len] = 0xdd;
  350. key_len += pad_len;
  351. key = keybuf;
  352. } else
  353. key = gsm->GTK[gsm->GN - 1];
  354. /*
  355. * Sub-elem ID[1] | Length[1] | Key Info[2] | Key Length[1] | RSC[8] |
  356. * Key[5..32].
  357. */
  358. subelem_len = 13 + key_len + 8;
  359. subelem = os_zalloc(subelem_len);
  360. if (subelem == NULL)
  361. return NULL;
  362. subelem[0] = FTIE_SUBELEM_GTK;
  363. subelem[1] = 11 + key_len + 8;
  364. /* Key ID in B0-B1 of Key Info */
  365. WPA_PUT_LE16(&subelem[2], gsm->GN & 0x03);
  366. subelem[4] = gsm->GTK_len;
  367. wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN, subelem + 5);
  368. if (aes_wrap(sm->PTK.kek, key_len / 8, key, subelem + 13)) {
  369. os_free(subelem);
  370. return NULL;
  371. }
  372. *len = subelem_len;
  373. return subelem;
  374. }
  375. #ifdef CONFIG_IEEE80211W
  376. static u8 * wpa_ft_igtk_subelem(struct wpa_state_machine *sm, size_t *len)
  377. {
  378. u8 *subelem, *pos;
  379. struct wpa_group *gsm = sm->group;
  380. size_t subelem_len;
  381. /* Sub-elem ID[1] | Length[1] | KeyID[2] | IPN[6] | Key Length[1] |
  382. * Key[16+8] */
  383. subelem_len = 1 + 1 + 2 + 6 + 1 + WPA_IGTK_LEN + 8;
  384. subelem = os_zalloc(subelem_len);
  385. if (subelem == NULL)
  386. return NULL;
  387. pos = subelem;
  388. *pos++ = FTIE_SUBELEM_IGTK;
  389. *pos++ = subelem_len - 2;
  390. WPA_PUT_LE16(pos, gsm->GN_igtk);
  391. pos += 2;
  392. wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN_igtk, pos);
  393. pos += 6;
  394. *pos++ = WPA_IGTK_LEN;
  395. if (aes_wrap(sm->PTK.kek, WPA_IGTK_LEN / 8,
  396. gsm->IGTK[gsm->GN_igtk - 4], pos)) {
  397. os_free(subelem);
  398. return NULL;
  399. }
  400. *len = subelem_len;
  401. return subelem;
  402. }
  403. #endif /* CONFIG_IEEE80211W */
  404. static u8 * wpa_ft_process_rdie(u8 *pos, u8 *end, u8 id, u8 descr_count,
  405. const u8 *ies, size_t ies_len)
  406. {
  407. struct ieee802_11_elems parse;
  408. struct rsn_rdie *rdie;
  409. wpa_printf(MSG_DEBUG, "FT: Resource Request: id=%d descr_count=%d",
  410. id, descr_count);
  411. wpa_hexdump(MSG_MSGDUMP, "FT: Resource descriptor IE(s)",
  412. ies, ies_len);
  413. if (end - pos < (int) sizeof(*rdie)) {
  414. wpa_printf(MSG_ERROR, "FT: Not enough room for response RDIE");
  415. return pos;
  416. }
  417. *pos++ = WLAN_EID_RIC_DATA;
  418. *pos++ = sizeof(*rdie);
  419. rdie = (struct rsn_rdie *) pos;
  420. rdie->id = id;
  421. rdie->descr_count = 0;
  422. rdie->status_code = host_to_le16(WLAN_STATUS_SUCCESS);
  423. pos += sizeof(*rdie);
  424. if (ieee802_11_parse_elems((u8 *) ies, ies_len, &parse, 1) ==
  425. ParseFailed) {
  426. wpa_printf(MSG_DEBUG, "FT: Failed to parse request IEs");
  427. rdie->status_code =
  428. host_to_le16(WLAN_STATUS_UNSPECIFIED_FAILURE);
  429. return pos;
  430. }
  431. #ifdef NEED_AP_MLME
  432. if (parse.wmm_tspec) {
  433. struct wmm_tspec_element *tspec;
  434. int res;
  435. if (parse.wmm_tspec_len + 2 < (int) sizeof(*tspec)) {
  436. wpa_printf(MSG_DEBUG, "FT: Too short WMM TSPEC IE "
  437. "(%d)", (int) parse.wmm_tspec_len);
  438. rdie->status_code =
  439. host_to_le16(WLAN_STATUS_UNSPECIFIED_FAILURE);
  440. return pos;
  441. }
  442. if (end - pos < (int) sizeof(*tspec)) {
  443. wpa_printf(MSG_ERROR, "FT: Not enough room for "
  444. "response TSPEC");
  445. rdie->status_code =
  446. host_to_le16(WLAN_STATUS_UNSPECIFIED_FAILURE);
  447. return pos;
  448. }
  449. tspec = (struct wmm_tspec_element *) pos;
  450. os_memcpy(tspec, parse.wmm_tspec - 2, sizeof(*tspec));
  451. res = wmm_process_tspec(tspec);
  452. wpa_printf(MSG_DEBUG, "FT: ADDTS processing result: %d", res);
  453. if (res == WMM_ADDTS_STATUS_INVALID_PARAMETERS)
  454. rdie->status_code =
  455. host_to_le16(WLAN_STATUS_INVALID_PARAMETERS);
  456. else if (res == WMM_ADDTS_STATUS_REFUSED)
  457. rdie->status_code =
  458. host_to_le16(WLAN_STATUS_REQUEST_DECLINED);
  459. else {
  460. /* TSPEC accepted; include updated TSPEC in response */
  461. rdie->descr_count = 1;
  462. pos += sizeof(*tspec);
  463. }
  464. return pos;
  465. }
  466. #endif /* NEED_AP_MLME */
  467. wpa_printf(MSG_DEBUG, "FT: No supported resource requested");
  468. rdie->status_code = host_to_le16(WLAN_STATUS_UNSPECIFIED_FAILURE);
  469. return pos;
  470. }
  471. static u8 * wpa_ft_process_ric(u8 *pos, u8 *end, const u8 *ric, size_t ric_len)
  472. {
  473. const u8 *rpos, *start;
  474. const struct rsn_rdie *rdie;
  475. wpa_hexdump(MSG_MSGDUMP, "FT: RIC Request", ric, ric_len);
  476. rpos = ric;
  477. while (rpos + sizeof(*rdie) < ric + ric_len) {
  478. if (rpos[0] != WLAN_EID_RIC_DATA || rpos[1] < sizeof(*rdie) ||
  479. rpos + 2 + rpos[1] > ric + ric_len)
  480. break;
  481. rdie = (const struct rsn_rdie *) (rpos + 2);
  482. rpos += 2 + rpos[1];
  483. start = rpos;
  484. while (rpos + 2 <= ric + ric_len &&
  485. rpos + 2 + rpos[1] <= ric + ric_len) {
  486. if (rpos[0] == WLAN_EID_RIC_DATA)
  487. break;
  488. rpos += 2 + rpos[1];
  489. }
  490. pos = wpa_ft_process_rdie(pos, end, rdie->id,
  491. rdie->descr_count,
  492. start, rpos - start);
  493. }
  494. return pos;
  495. }
  496. u8 * wpa_sm_write_assoc_resp_ies(struct wpa_state_machine *sm, u8 *pos,
  497. size_t max_len, int auth_alg,
  498. const u8 *req_ies, size_t req_ies_len)
  499. {
  500. u8 *end, *mdie, *ftie, *rsnie = NULL, *r0kh_id, *subelem = NULL;
  501. size_t mdie_len, ftie_len, rsnie_len = 0, r0kh_id_len, subelem_len = 0;
  502. int res;
  503. struct wpa_auth_config *conf;
  504. struct rsn_ftie *_ftie;
  505. struct wpa_ft_ies parse;
  506. u8 *ric_start;
  507. u8 *anonce, *snonce;
  508. if (sm == NULL)
  509. return pos;
  510. conf = &sm->wpa_auth->conf;
  511. if (sm->wpa_key_mgmt != WPA_KEY_MGMT_FT_IEEE8021X &&
  512. sm->wpa_key_mgmt != WPA_KEY_MGMT_FT_PSK)
  513. return pos;
  514. end = pos + max_len;
  515. if (auth_alg == WLAN_AUTH_FT) {
  516. /*
  517. * RSN (only present if this is a Reassociation Response and
  518. * part of a fast BSS transition)
  519. */
  520. res = wpa_write_rsn_ie(conf, pos, end - pos, sm->pmk_r1_name);
  521. if (res < 0)
  522. return pos;
  523. rsnie = pos;
  524. rsnie_len = res;
  525. pos += res;
  526. }
  527. /* Mobility Domain Information */
  528. res = wpa_write_mdie(conf, pos, end - pos);
  529. if (res < 0)
  530. return pos;
  531. mdie = pos;
  532. mdie_len = res;
  533. pos += res;
  534. /* Fast BSS Transition Information */
  535. if (auth_alg == WLAN_AUTH_FT) {
  536. subelem = wpa_ft_gtk_subelem(sm, &subelem_len);
  537. r0kh_id = sm->r0kh_id;
  538. r0kh_id_len = sm->r0kh_id_len;
  539. anonce = sm->ANonce;
  540. snonce = sm->SNonce;
  541. #ifdef CONFIG_IEEE80211W
  542. if (sm->mgmt_frame_prot) {
  543. u8 *igtk;
  544. size_t igtk_len;
  545. u8 *nbuf;
  546. igtk = wpa_ft_igtk_subelem(sm, &igtk_len);
  547. if (igtk == NULL) {
  548. os_free(subelem);
  549. return pos;
  550. }
  551. nbuf = os_realloc(subelem, subelem_len + igtk_len);
  552. if (nbuf == NULL) {
  553. os_free(subelem);
  554. os_free(igtk);
  555. return pos;
  556. }
  557. subelem = nbuf;
  558. os_memcpy(subelem + subelem_len, igtk, igtk_len);
  559. subelem_len += igtk_len;
  560. os_free(igtk);
  561. }
  562. #endif /* CONFIG_IEEE80211W */
  563. } else {
  564. r0kh_id = conf->r0_key_holder;
  565. r0kh_id_len = conf->r0_key_holder_len;
  566. anonce = NULL;
  567. snonce = NULL;
  568. }
  569. res = wpa_write_ftie(conf, r0kh_id, r0kh_id_len, anonce, snonce, pos,
  570. end - pos, subelem, subelem_len);
  571. os_free(subelem);
  572. if (res < 0)
  573. return pos;
  574. ftie = pos;
  575. ftie_len = res;
  576. pos += res;
  577. os_free(sm->assoc_resp_ftie);
  578. sm->assoc_resp_ftie = os_malloc(ftie_len);
  579. if (sm->assoc_resp_ftie)
  580. os_memcpy(sm->assoc_resp_ftie, ftie, ftie_len);
  581. _ftie = (struct rsn_ftie *) (ftie + 2);
  582. if (auth_alg == WLAN_AUTH_FT)
  583. _ftie->mic_control[1] = 3; /* Information element count */
  584. ric_start = pos;
  585. if (wpa_ft_parse_ies(req_ies, req_ies_len, &parse) == 0 && parse.ric) {
  586. pos = wpa_ft_process_ric(pos, end, parse.ric, parse.ric_len);
  587. if (auth_alg == WLAN_AUTH_FT)
  588. _ftie->mic_control[1] +=
  589. ieee802_11_ie_count(ric_start,
  590. pos - ric_start);
  591. }
  592. if (ric_start == pos)
  593. ric_start = NULL;
  594. if (auth_alg == WLAN_AUTH_FT &&
  595. wpa_ft_mic(sm->PTK.kck, sm->addr, sm->wpa_auth->addr, 6,
  596. mdie, mdie_len, ftie, ftie_len,
  597. rsnie, rsnie_len,
  598. ric_start, ric_start ? pos - ric_start : 0,
  599. _ftie->mic) < 0)
  600. wpa_printf(MSG_DEBUG, "FT: Failed to calculate MIC");
  601. return pos;
  602. }
  603. static int wpa_ft_parse_ftie(const u8 *ie, size_t ie_len,
  604. struct wpa_ft_ies *parse)
  605. {
  606. const u8 *end, *pos;
  607. parse->ftie = ie;
  608. parse->ftie_len = ie_len;
  609. pos = ie + sizeof(struct rsn_ftie);
  610. end = ie + ie_len;
  611. while (pos + 2 <= end && pos + 2 + pos[1] <= end) {
  612. switch (pos[0]) {
  613. case FTIE_SUBELEM_R1KH_ID:
  614. if (pos[1] != FT_R1KH_ID_LEN) {
  615. wpa_printf(MSG_DEBUG, "FT: Invalid R1KH-ID "
  616. "length in FTIE: %d", pos[1]);
  617. return -1;
  618. }
  619. parse->r1kh_id = pos + 2;
  620. break;
  621. case FTIE_SUBELEM_GTK:
  622. parse->gtk = pos + 2;
  623. parse->gtk_len = pos[1];
  624. break;
  625. case FTIE_SUBELEM_R0KH_ID:
  626. if (pos[1] < 1 || pos[1] > FT_R0KH_ID_MAX_LEN) {
  627. wpa_printf(MSG_DEBUG, "FT: Invalid R0KH-ID "
  628. "length in FTIE: %d", pos[1]);
  629. return -1;
  630. }
  631. parse->r0kh_id = pos + 2;
  632. parse->r0kh_id_len = pos[1];
  633. break;
  634. }
  635. pos += 2 + pos[1];
  636. }
  637. return 0;
  638. }
  639. static int wpa_ft_parse_ies(const u8 *ies, size_t ies_len,
  640. struct wpa_ft_ies *parse)
  641. {
  642. const u8 *end, *pos;
  643. struct wpa_ie_data data;
  644. int ret;
  645. const struct rsn_ftie *ftie;
  646. int prot_ie_count = 0;
  647. os_memset(parse, 0, sizeof(*parse));
  648. if (ies == NULL)
  649. return 0;
  650. pos = ies;
  651. end = ies + ies_len;
  652. while (pos + 2 <= end && pos + 2 + pos[1] <= end) {
  653. switch (pos[0]) {
  654. case WLAN_EID_RSN:
  655. parse->rsn = pos + 2;
  656. parse->rsn_len = pos[1];
  657. ret = wpa_parse_wpa_ie_rsn(parse->rsn - 2,
  658. parse->rsn_len + 2,
  659. &data);
  660. if (ret < 0) {
  661. wpa_printf(MSG_DEBUG, "FT: Failed to parse "
  662. "RSN IE: %d", ret);
  663. return -1;
  664. }
  665. if (data.num_pmkid == 1 && data.pmkid)
  666. parse->rsn_pmkid = data.pmkid;
  667. break;
  668. case WLAN_EID_MOBILITY_DOMAIN:
  669. parse->mdie = pos + 2;
  670. parse->mdie_len = pos[1];
  671. break;
  672. case WLAN_EID_FAST_BSS_TRANSITION:
  673. if (pos[1] < sizeof(*ftie))
  674. return -1;
  675. ftie = (const struct rsn_ftie *) (pos + 2);
  676. prot_ie_count = ftie->mic_control[1];
  677. if (wpa_ft_parse_ftie(pos + 2, pos[1], parse) < 0)
  678. return -1;
  679. break;
  680. case WLAN_EID_RIC_DATA:
  681. if (parse->ric == NULL)
  682. parse->ric = pos;
  683. }
  684. pos += 2 + pos[1];
  685. }
  686. if (prot_ie_count == 0)
  687. return 0; /* no MIC */
  688. /*
  689. * Check that the protected IE count matches with IEs included in the
  690. * frame.
  691. */
  692. if (parse->rsn)
  693. prot_ie_count--;
  694. if (parse->mdie)
  695. prot_ie_count--;
  696. if (parse->ftie)
  697. prot_ie_count--;
  698. if (prot_ie_count < 0) {
  699. wpa_printf(MSG_DEBUG, "FT: Some required IEs not included in "
  700. "the protected IE count");
  701. return -1;
  702. }
  703. if (prot_ie_count == 0 && parse->ric) {
  704. wpa_printf(MSG_DEBUG, "FT: RIC IE(s) in the frame, but not "
  705. "included in protected IE count");
  706. return -1;
  707. }
  708. /* Determine the end of the RIC IE(s) */
  709. pos = parse->ric;
  710. while (pos && pos + 2 <= end && pos + 2 + pos[1] <= end &&
  711. prot_ie_count) {
  712. prot_ie_count--;
  713. pos += 2 + pos[1];
  714. }
  715. parse->ric_len = pos - parse->ric;
  716. if (prot_ie_count) {
  717. wpa_printf(MSG_DEBUG, "FT: %d protected IEs missing from "
  718. "frame", (int) prot_ie_count);
  719. return -1;
  720. }
  721. return 0;
  722. }
  723. static inline int wpa_auth_set_key(struct wpa_authenticator *wpa_auth,
  724. int vlan_id,
  725. enum wpa_alg alg, const u8 *addr, int idx,
  726. u8 *key, size_t key_len)
  727. {
  728. if (wpa_auth->cb.set_key == NULL)
  729. return -1;
  730. return wpa_auth->cb.set_key(wpa_auth->cb.ctx, vlan_id, alg, addr, idx,
  731. key, key_len);
  732. }
  733. void wpa_ft_install_ptk(struct wpa_state_machine *sm)
  734. {
  735. enum wpa_alg alg;
  736. int klen;
  737. /* MLME-SETKEYS.request(PTK) */
  738. if (sm->pairwise == WPA_CIPHER_TKIP) {
  739. alg = WPA_ALG_TKIP;
  740. klen = 32;
  741. } else if (sm->pairwise == WPA_CIPHER_CCMP) {
  742. alg = WPA_ALG_CCMP;
  743. klen = 16;
  744. } else {
  745. wpa_printf(MSG_DEBUG, "FT: Unknown pairwise alg 0x%x - skip "
  746. "PTK configuration", sm->pairwise);
  747. return;
  748. }
  749. /* FIX: add STA entry to kernel/driver here? The set_key will fail
  750. * most likely without this.. At the moment, STA entry is added only
  751. * after association has been completed. This function will be called
  752. * again after association to get the PTK configured, but that could be
  753. * optimized by adding the STA entry earlier.
  754. */
  755. if (wpa_auth_set_key(sm->wpa_auth, 0, alg, sm->addr, 0,
  756. sm->PTK.tk1, klen))
  757. return;
  758. /* FIX: MLME-SetProtection.Request(TA, Tx_Rx) */
  759. sm->pairwise_set = TRUE;
  760. }
  761. static u16 wpa_ft_process_auth_req(struct wpa_state_machine *sm,
  762. const u8 *ies, size_t ies_len,
  763. u8 **resp_ies, size_t *resp_ies_len)
  764. {
  765. struct rsn_mdie *mdie;
  766. struct rsn_ftie *ftie;
  767. u8 pmk_r1[PMK_LEN], pmk_r1_name[WPA_PMK_NAME_LEN];
  768. u8 ptk_name[WPA_PMK_NAME_LEN];
  769. struct wpa_auth_config *conf;
  770. struct wpa_ft_ies parse;
  771. size_t buflen, ptk_len;
  772. int ret;
  773. u8 *pos, *end;
  774. int pairwise;
  775. *resp_ies = NULL;
  776. *resp_ies_len = 0;
  777. sm->pmk_r1_name_valid = 0;
  778. conf = &sm->wpa_auth->conf;
  779. wpa_hexdump(MSG_DEBUG, "FT: Received authentication frame IEs",
  780. ies, ies_len);
  781. if (wpa_ft_parse_ies(ies, ies_len, &parse) < 0) {
  782. wpa_printf(MSG_DEBUG, "FT: Failed to parse FT IEs");
  783. return WLAN_STATUS_UNSPECIFIED_FAILURE;
  784. }
  785. mdie = (struct rsn_mdie *) parse.mdie;
  786. if (mdie == NULL || parse.mdie_len < sizeof(*mdie) ||
  787. os_memcmp(mdie->mobility_domain,
  788. sm->wpa_auth->conf.mobility_domain,
  789. MOBILITY_DOMAIN_ID_LEN) != 0) {
  790. wpa_printf(MSG_DEBUG, "FT: Invalid MDIE");
  791. return WLAN_STATUS_INVALID_MDIE;
  792. }
  793. ftie = (struct rsn_ftie *) parse.ftie;
  794. if (ftie == NULL || parse.ftie_len < sizeof(*ftie)) {
  795. wpa_printf(MSG_DEBUG, "FT: Invalid FTIE");
  796. return WLAN_STATUS_INVALID_FTIE;
  797. }
  798. os_memcpy(sm->SNonce, ftie->snonce, WPA_NONCE_LEN);
  799. if (parse.r0kh_id == NULL) {
  800. wpa_printf(MSG_DEBUG, "FT: Invalid FTIE - no R0KH-ID");
  801. return WLAN_STATUS_INVALID_FTIE;
  802. }
  803. wpa_hexdump(MSG_DEBUG, "FT: STA R0KH-ID",
  804. parse.r0kh_id, parse.r0kh_id_len);
  805. os_memcpy(sm->r0kh_id, parse.r0kh_id, parse.r0kh_id_len);
  806. sm->r0kh_id_len = parse.r0kh_id_len;
  807. if (parse.rsn_pmkid == NULL) {
  808. wpa_printf(MSG_DEBUG, "FT: No PMKID in RSNIE");
  809. return WLAN_STATUS_INVALID_PMKID;
  810. }
  811. wpa_hexdump(MSG_DEBUG, "FT: Requested PMKR0Name",
  812. parse.rsn_pmkid, WPA_PMK_NAME_LEN);
  813. wpa_derive_pmk_r1_name(parse.rsn_pmkid,
  814. sm->wpa_auth->conf.r1_key_holder, sm->addr,
  815. pmk_r1_name);
  816. wpa_hexdump(MSG_DEBUG, "FT: Derived requested PMKR1Name",
  817. pmk_r1_name, WPA_PMK_NAME_LEN);
  818. if (wpa_ft_fetch_pmk_r1(sm->wpa_auth, sm->addr, pmk_r1_name, pmk_r1,
  819. &pairwise) < 0) {
  820. if (wpa_ft_pull_pmk_r1(sm->wpa_auth, sm->addr, sm->r0kh_id,
  821. sm->r0kh_id_len, parse.rsn_pmkid) < 0) {
  822. wpa_printf(MSG_DEBUG, "FT: Did not have matching "
  823. "PMK-R1 and unknown R0KH-ID");
  824. return WLAN_STATUS_INVALID_PMKID;
  825. }
  826. /*
  827. * TODO: Should return "status pending" (and the caller should
  828. * not send out response now). The real response will be sent
  829. * once the response from R0KH is received.
  830. */
  831. return WLAN_STATUS_INVALID_PMKID;
  832. }
  833. wpa_hexdump_key(MSG_DEBUG, "FT: Selected PMK-R1", pmk_r1, PMK_LEN);
  834. sm->pmk_r1_name_valid = 1;
  835. os_memcpy(sm->pmk_r1_name, pmk_r1_name, WPA_PMK_NAME_LEN);
  836. if (os_get_random(sm->ANonce, WPA_NONCE_LEN)) {
  837. wpa_printf(MSG_DEBUG, "FT: Failed to get random data for "
  838. "ANonce");
  839. return WLAN_STATUS_UNSPECIFIED_FAILURE;
  840. }
  841. wpa_hexdump(MSG_DEBUG, "FT: Received SNonce",
  842. sm->SNonce, WPA_NONCE_LEN);
  843. wpa_hexdump(MSG_DEBUG, "FT: Generated ANonce",
  844. sm->ANonce, WPA_NONCE_LEN);
  845. ptk_len = pairwise != WPA_CIPHER_CCMP ? 64 : 48;
  846. wpa_pmk_r1_to_ptk(pmk_r1, sm->SNonce, sm->ANonce, sm->addr,
  847. sm->wpa_auth->addr, pmk_r1_name,
  848. (u8 *) &sm->PTK, ptk_len, ptk_name);
  849. wpa_hexdump_key(MSG_DEBUG, "FT: PTK",
  850. (u8 *) &sm->PTK, ptk_len);
  851. wpa_hexdump(MSG_DEBUG, "FT: PTKName", ptk_name, WPA_PMK_NAME_LEN);
  852. sm->pairwise = pairwise;
  853. wpa_ft_install_ptk(sm);
  854. buflen = 2 + sizeof(struct rsn_mdie) + 2 + sizeof(struct rsn_ftie) +
  855. 2 + FT_R1KH_ID_LEN + 200;
  856. *resp_ies = os_zalloc(buflen);
  857. if (*resp_ies == NULL) {
  858. return WLAN_STATUS_UNSPECIFIED_FAILURE;
  859. }
  860. pos = *resp_ies;
  861. end = *resp_ies + buflen;
  862. ret = wpa_write_rsn_ie(conf, pos, end - pos, parse.rsn_pmkid);
  863. if (ret < 0) {
  864. os_free(*resp_ies);
  865. *resp_ies = NULL;
  866. return WLAN_STATUS_UNSPECIFIED_FAILURE;
  867. }
  868. pos += ret;
  869. ret = wpa_write_mdie(conf, pos, end - pos);
  870. if (ret < 0) {
  871. os_free(*resp_ies);
  872. *resp_ies = NULL;
  873. return WLAN_STATUS_UNSPECIFIED_FAILURE;
  874. }
  875. pos += ret;
  876. ret = wpa_write_ftie(conf, parse.r0kh_id, parse.r0kh_id_len,
  877. sm->ANonce, sm->SNonce, pos, end - pos, NULL, 0);
  878. if (ret < 0) {
  879. os_free(*resp_ies);
  880. *resp_ies = NULL;
  881. return WLAN_STATUS_UNSPECIFIED_FAILURE;
  882. }
  883. pos += ret;
  884. *resp_ies_len = pos - *resp_ies;
  885. return WLAN_STATUS_SUCCESS;
  886. }
  887. void wpa_ft_process_auth(struct wpa_state_machine *sm, const u8 *bssid,
  888. u16 auth_transaction, const u8 *ies, size_t ies_len,
  889. void (*cb)(void *ctx, const u8 *dst, const u8 *bssid,
  890. u16 auth_transaction, u16 status,
  891. const u8 *ies, size_t ies_len),
  892. void *ctx)
  893. {
  894. u16 status;
  895. u8 *resp_ies;
  896. size_t resp_ies_len;
  897. if (sm == NULL) {
  898. wpa_printf(MSG_DEBUG, "FT: Received authentication frame, but "
  899. "WPA SM not available");
  900. return;
  901. }
  902. wpa_printf(MSG_DEBUG, "FT: Received authentication frame: STA=" MACSTR
  903. " BSSID=" MACSTR " transaction=%d",
  904. MAC2STR(sm->addr), MAC2STR(bssid), auth_transaction);
  905. status = wpa_ft_process_auth_req(sm, ies, ies_len, &resp_ies,
  906. &resp_ies_len);
  907. wpa_printf(MSG_DEBUG, "FT: FT authentication response: dst=" MACSTR
  908. " auth_transaction=%d status=%d",
  909. MAC2STR(sm->addr), auth_transaction + 1, status);
  910. wpa_hexdump(MSG_DEBUG, "FT: Response IEs", resp_ies, resp_ies_len);
  911. cb(ctx, sm->addr, bssid, auth_transaction + 1, status,
  912. resp_ies, resp_ies_len);
  913. os_free(resp_ies);
  914. }
  915. u16 wpa_ft_validate_reassoc(struct wpa_state_machine *sm, const u8 *ies,
  916. size_t ies_len)
  917. {
  918. struct wpa_ft_ies parse;
  919. struct rsn_mdie *mdie;
  920. struct rsn_ftie *ftie;
  921. u8 mic[16];
  922. unsigned int count;
  923. if (sm == NULL)
  924. return WLAN_STATUS_UNSPECIFIED_FAILURE;
  925. wpa_hexdump(MSG_DEBUG, "FT: Reassoc Req IEs", ies, ies_len);
  926. if (wpa_ft_parse_ies(ies, ies_len, &parse) < 0) {
  927. wpa_printf(MSG_DEBUG, "FT: Failed to parse FT IEs");
  928. return WLAN_STATUS_UNSPECIFIED_FAILURE;
  929. }
  930. if (parse.rsn == NULL) {
  931. wpa_printf(MSG_DEBUG, "FT: No RSNIE in Reassoc Req");
  932. return WLAN_STATUS_UNSPECIFIED_FAILURE;
  933. }
  934. if (parse.rsn_pmkid == NULL) {
  935. wpa_printf(MSG_DEBUG, "FT: No PMKID in RSNIE");
  936. return WLAN_STATUS_INVALID_PMKID;
  937. }
  938. if (os_memcmp(parse.rsn_pmkid, sm->pmk_r1_name, WPA_PMK_NAME_LEN) != 0)
  939. {
  940. wpa_printf(MSG_DEBUG, "FT: PMKID in Reassoc Req did not match "
  941. "with the PMKR1Name derived from auth request");
  942. return WLAN_STATUS_INVALID_PMKID;
  943. }
  944. mdie = (struct rsn_mdie *) parse.mdie;
  945. if (mdie == NULL || parse.mdie_len < sizeof(*mdie) ||
  946. os_memcmp(mdie->mobility_domain,
  947. sm->wpa_auth->conf.mobility_domain,
  948. MOBILITY_DOMAIN_ID_LEN) != 0) {
  949. wpa_printf(MSG_DEBUG, "FT: Invalid MDIE");
  950. return WLAN_STATUS_INVALID_MDIE;
  951. }
  952. ftie = (struct rsn_ftie *) parse.ftie;
  953. if (ftie == NULL || parse.ftie_len < sizeof(*ftie)) {
  954. wpa_printf(MSG_DEBUG, "FT: Invalid FTIE");
  955. return WLAN_STATUS_INVALID_FTIE;
  956. }
  957. if (os_memcmp(ftie->snonce, sm->SNonce, WPA_NONCE_LEN) != 0) {
  958. wpa_printf(MSG_DEBUG, "FT: SNonce mismatch in FTIE");
  959. wpa_hexdump(MSG_DEBUG, "FT: Received SNonce",
  960. ftie->snonce, WPA_NONCE_LEN);
  961. wpa_hexdump(MSG_DEBUG, "FT: Expected SNonce",
  962. sm->SNonce, WPA_NONCE_LEN);
  963. return -1;
  964. }
  965. if (os_memcmp(ftie->anonce, sm->ANonce, WPA_NONCE_LEN) != 0) {
  966. wpa_printf(MSG_DEBUG, "FT: ANonce mismatch in FTIE");
  967. wpa_hexdump(MSG_DEBUG, "FT: Received ANonce",
  968. ftie->anonce, WPA_NONCE_LEN);
  969. wpa_hexdump(MSG_DEBUG, "FT: Expected ANonce",
  970. sm->ANonce, WPA_NONCE_LEN);
  971. return -1;
  972. }
  973. if (parse.r0kh_id == NULL) {
  974. wpa_printf(MSG_DEBUG, "FT: No R0KH-ID subelem in FTIE");
  975. return -1;
  976. }
  977. if (parse.r0kh_id_len != sm->r0kh_id_len ||
  978. os_memcmp(parse.r0kh_id, sm->r0kh_id, parse.r0kh_id_len) != 0) {
  979. wpa_printf(MSG_DEBUG, "FT: R0KH-ID in FTIE did not match with "
  980. "the current R0KH-ID");
  981. wpa_hexdump(MSG_DEBUG, "FT: R0KH-ID in FTIE",
  982. parse.r0kh_id, parse.r0kh_id_len);
  983. wpa_hexdump(MSG_DEBUG, "FT: The current R0KH-ID",
  984. sm->r0kh_id, sm->r0kh_id_len);
  985. return -1;
  986. }
  987. if (parse.r1kh_id == NULL) {
  988. wpa_printf(MSG_DEBUG, "FT: No R1KH-ID subelem in FTIE");
  989. return -1;
  990. }
  991. if (os_memcmp(parse.r1kh_id, sm->wpa_auth->conf.r1_key_holder,
  992. FT_R1KH_ID_LEN) != 0) {
  993. wpa_printf(MSG_DEBUG, "FT: Unknown R1KH-ID used in "
  994. "ReassocReq");
  995. wpa_hexdump(MSG_DEBUG, "FT: R1KH-ID in FTIE",
  996. parse.r1kh_id, FT_R1KH_ID_LEN);
  997. wpa_hexdump(MSG_DEBUG, "FT: Expected R1KH-ID",
  998. sm->wpa_auth->conf.r1_key_holder, FT_R1KH_ID_LEN);
  999. return -1;
  1000. }
  1001. if (parse.rsn_pmkid == NULL ||
  1002. os_memcmp(parse.rsn_pmkid, sm->pmk_r1_name, WPA_PMK_NAME_LEN)) {
  1003. wpa_printf(MSG_DEBUG, "FT: No matching PMKR1Name (PMKID) in "
  1004. "RSNIE (pmkid=%d)", !!parse.rsn_pmkid);
  1005. return -1;
  1006. }
  1007. count = 3;
  1008. if (parse.ric)
  1009. count++;
  1010. if (ftie->mic_control[1] != count) {
  1011. wpa_printf(MSG_DEBUG, "FT: Unexpected IE count in MIC "
  1012. "Control: received %u expected %u",
  1013. ftie->mic_control[1], count);
  1014. return -1;
  1015. }
  1016. if (wpa_ft_mic(sm->PTK.kck, sm->addr, sm->wpa_auth->addr, 5,
  1017. parse.mdie - 2, parse.mdie_len + 2,
  1018. parse.ftie - 2, parse.ftie_len + 2,
  1019. parse.rsn - 2, parse.rsn_len + 2,
  1020. parse.ric, parse.ric_len,
  1021. mic) < 0) {
  1022. wpa_printf(MSG_DEBUG, "FT: Failed to calculate MIC");
  1023. return WLAN_STATUS_UNSPECIFIED_FAILURE;
  1024. }
  1025. if (os_memcmp(mic, ftie->mic, 16) != 0) {
  1026. wpa_printf(MSG_DEBUG, "FT: Invalid MIC in FTIE");
  1027. wpa_hexdump(MSG_MSGDUMP, "FT: Received MIC", ftie->mic, 16);
  1028. wpa_hexdump(MSG_MSGDUMP, "FT: Calculated MIC", mic, 16);
  1029. return WLAN_STATUS_INVALID_FTIE;
  1030. }
  1031. return WLAN_STATUS_SUCCESS;
  1032. }
  1033. int wpa_ft_action_rx(struct wpa_state_machine *sm, const u8 *data, size_t len)
  1034. {
  1035. const u8 *sta_addr, *target_ap;
  1036. const u8 *ies;
  1037. size_t ies_len;
  1038. u8 action;
  1039. struct ft_rrb_frame *frame;
  1040. if (sm == NULL)
  1041. return -1;
  1042. /*
  1043. * data: Category[1] Action[1] STA_Address[6] Target_AP_Address[6]
  1044. * FT Request action frame body[variable]
  1045. */
  1046. if (len < 14) {
  1047. wpa_printf(MSG_DEBUG, "FT: Too short FT Action frame "
  1048. "(len=%lu)", (unsigned long) len);
  1049. return -1;
  1050. }
  1051. action = data[1];
  1052. sta_addr = data + 2;
  1053. target_ap = data + 8;
  1054. ies = data + 14;
  1055. ies_len = len - 14;
  1056. wpa_printf(MSG_DEBUG, "FT: Received FT Action frame (STA=" MACSTR
  1057. " Target AP=" MACSTR " Action=%d)",
  1058. MAC2STR(sta_addr), MAC2STR(target_ap), action);
  1059. if (os_memcmp(sta_addr, sm->addr, ETH_ALEN) != 0) {
  1060. wpa_printf(MSG_DEBUG, "FT: Mismatch in FT Action STA address: "
  1061. "STA=" MACSTR " STA-Address=" MACSTR,
  1062. MAC2STR(sm->addr), MAC2STR(sta_addr));
  1063. return -1;
  1064. }
  1065. /*
  1066. * Do some sanity checking on the target AP address (not own and not
  1067. * broadcast. This could be extended to filter based on a list of known
  1068. * APs in the MD (if such a list were configured).
  1069. */
  1070. if ((target_ap[0] & 0x01) ||
  1071. os_memcmp(target_ap, sm->wpa_auth->addr, ETH_ALEN) == 0) {
  1072. wpa_printf(MSG_DEBUG, "FT: Invalid Target AP in FT Action "
  1073. "frame");
  1074. return -1;
  1075. }
  1076. wpa_hexdump(MSG_MSGDUMP, "FT: Action frame body", ies, ies_len);
  1077. /* RRB - Forward action frame to the target AP */
  1078. frame = os_malloc(sizeof(*frame) + len);
  1079. frame->frame_type = RSN_REMOTE_FRAME_TYPE_FT_RRB;
  1080. frame->packet_type = FT_PACKET_REQUEST;
  1081. frame->action_length = host_to_le16(len);
  1082. os_memcpy(frame->ap_address, sm->wpa_auth->addr, ETH_ALEN);
  1083. os_memcpy(frame + 1, data, len);
  1084. wpa_ft_rrb_send(sm->wpa_auth, target_ap, (u8 *) frame,
  1085. sizeof(*frame) + len);
  1086. os_free(frame);
  1087. return 0;
  1088. }
  1089. static int wpa_ft_rrb_rx_request(struct wpa_authenticator *wpa_auth,
  1090. const u8 *current_ap, const u8 *sta_addr,
  1091. const u8 *body, size_t len)
  1092. {
  1093. struct wpa_state_machine *sm;
  1094. u16 status;
  1095. u8 *resp_ies, *pos;
  1096. size_t resp_ies_len, rlen;
  1097. struct ft_rrb_frame *frame;
  1098. sm = wpa_ft_add_sta(wpa_auth, sta_addr);
  1099. if (sm == NULL) {
  1100. wpa_printf(MSG_DEBUG, "FT: Failed to add new STA based on "
  1101. "RRB Request");
  1102. return -1;
  1103. }
  1104. wpa_hexdump(MSG_MSGDUMP, "FT: RRB Request Frame body", body, len);
  1105. status = wpa_ft_process_auth_req(sm, body, len, &resp_ies,
  1106. &resp_ies_len);
  1107. wpa_printf(MSG_DEBUG, "FT: RRB authentication response: STA=" MACSTR
  1108. " CurrentAP=" MACSTR " status=%d",
  1109. MAC2STR(sm->addr), MAC2STR(current_ap), status);
  1110. wpa_hexdump(MSG_DEBUG, "FT: Response IEs", resp_ies, resp_ies_len);
  1111. /* RRB - Forward action frame response to the Current AP */
  1112. /*
  1113. * data: Category[1] Action[1] STA_Address[6] Target_AP_Address[6]
  1114. * Status_Code[2] FT Request action frame body[variable]
  1115. */
  1116. rlen = 2 + 2 * ETH_ALEN + 2 + resp_ies_len;
  1117. frame = os_malloc(sizeof(*frame) + rlen);
  1118. frame->frame_type = RSN_REMOTE_FRAME_TYPE_FT_RRB;
  1119. frame->packet_type = FT_PACKET_RESPONSE;
  1120. frame->action_length = host_to_le16(rlen);
  1121. os_memcpy(frame->ap_address, wpa_auth->addr, ETH_ALEN);
  1122. pos = (u8 *) (frame + 1);
  1123. *pos++ = WLAN_ACTION_FT;
  1124. *pos++ = 2; /* Action: Response */
  1125. os_memcpy(pos, sta_addr, ETH_ALEN);
  1126. pos += ETH_ALEN;
  1127. os_memcpy(pos, wpa_auth->addr, ETH_ALEN);
  1128. pos += ETH_ALEN;
  1129. WPA_PUT_LE16(pos, status);
  1130. pos += 2;
  1131. if (resp_ies) {
  1132. os_memcpy(pos, resp_ies, resp_ies_len);
  1133. os_free(resp_ies);
  1134. }
  1135. wpa_ft_rrb_send(wpa_auth, current_ap, (u8 *) frame,
  1136. sizeof(*frame) + rlen);
  1137. os_free(frame);
  1138. return 0;
  1139. }
  1140. static int wpa_ft_rrb_rx_pull(struct wpa_authenticator *wpa_auth,
  1141. const u8 *src_addr,
  1142. const u8 *data, size_t data_len)
  1143. {
  1144. struct ft_r0kh_r1kh_pull_frame *frame, f;
  1145. struct ft_remote_r1kh *r1kh;
  1146. struct ft_r0kh_r1kh_resp_frame resp, r;
  1147. u8 pmk_r0[PMK_LEN];
  1148. int pairwise;
  1149. wpa_printf(MSG_DEBUG, "FT: Received PMK-R1 pull");
  1150. if (data_len < sizeof(*frame))
  1151. return -1;
  1152. r1kh = wpa_auth->conf.r1kh_list;
  1153. while (r1kh) {
  1154. if (os_memcmp(r1kh->addr, src_addr, ETH_ALEN) == 0)
  1155. break;
  1156. r1kh = r1kh->next;
  1157. }
  1158. if (r1kh == NULL) {
  1159. wpa_printf(MSG_DEBUG, "FT: No matching R1KH address found for "
  1160. "PMK-R1 pull source address " MACSTR,
  1161. MAC2STR(src_addr));
  1162. return -1;
  1163. }
  1164. frame = (struct ft_r0kh_r1kh_pull_frame *) data;
  1165. /* aes_unwrap() does not support inplace decryption, so use a temporary
  1166. * buffer for the data. */
  1167. if (aes_unwrap(r1kh->key, (FT_R0KH_R1KH_PULL_DATA_LEN + 7) / 8,
  1168. frame->nonce, f.nonce) < 0) {
  1169. wpa_printf(MSG_DEBUG, "FT: Failed to decrypt PMK-R1 pull "
  1170. "request from " MACSTR, MAC2STR(src_addr));
  1171. return -1;
  1172. }
  1173. wpa_hexdump(MSG_DEBUG, "FT: PMK-R1 pull - nonce",
  1174. f.nonce, sizeof(f.nonce));
  1175. wpa_hexdump(MSG_DEBUG, "FT: PMK-R1 pull - PMKR0Name",
  1176. f.pmk_r0_name, WPA_PMK_NAME_LEN);
  1177. wpa_printf(MSG_DEBUG, "FT: PMK-R1 pull - R1KH-ID=" MACSTR "S1KH-ID="
  1178. MACSTR, MAC2STR(f.r1kh_id), MAC2STR(f.s1kh_id));
  1179. os_memset(&resp, 0, sizeof(resp));
  1180. resp.frame_type = RSN_REMOTE_FRAME_TYPE_FT_RRB;
  1181. resp.packet_type = FT_PACKET_R0KH_R1KH_RESP;
  1182. resp.data_length = host_to_le16(FT_R0KH_R1KH_RESP_DATA_LEN);
  1183. os_memcpy(resp.ap_address, wpa_auth->addr, ETH_ALEN);
  1184. /* aes_wrap() does not support inplace encryption, so use a temporary
  1185. * buffer for the data. */
  1186. os_memcpy(r.nonce, f.nonce, sizeof(f.nonce));
  1187. os_memcpy(r.r1kh_id, f.r1kh_id, FT_R1KH_ID_LEN);
  1188. os_memcpy(r.s1kh_id, f.s1kh_id, ETH_ALEN);
  1189. if (wpa_ft_fetch_pmk_r0(wpa_auth, f.s1kh_id, f.pmk_r0_name, pmk_r0,
  1190. &pairwise) < 0) {
  1191. wpa_printf(MSG_DEBUG, "FT: No matching PMKR0Name found for "
  1192. "PMK-R1 pull");
  1193. return -1;
  1194. }
  1195. wpa_derive_pmk_r1(pmk_r0, f.pmk_r0_name, f.r1kh_id, f.s1kh_id,
  1196. r.pmk_r1, r.pmk_r1_name);
  1197. wpa_hexdump_key(MSG_DEBUG, "FT: PMK-R1", r.pmk_r1, PMK_LEN);
  1198. wpa_hexdump(MSG_DEBUG, "FT: PMKR1Name", r.pmk_r1_name,
  1199. WPA_PMK_NAME_LEN);
  1200. r.pairwise = host_to_le16(pairwise);
  1201. if (aes_wrap(r1kh->key, (FT_R0KH_R1KH_RESP_DATA_LEN + 7) / 8,
  1202. r.nonce, resp.nonce) < 0) {
  1203. os_memset(pmk_r0, 0, PMK_LEN);
  1204. return -1;
  1205. }
  1206. os_memset(pmk_r0, 0, PMK_LEN);
  1207. wpa_ft_rrb_send(wpa_auth, src_addr, (u8 *) &resp, sizeof(resp));
  1208. return 0;
  1209. }
  1210. static int wpa_ft_rrb_rx_resp(struct wpa_authenticator *wpa_auth,
  1211. const u8 *src_addr,
  1212. const u8 *data, size_t data_len)
  1213. {
  1214. struct ft_r0kh_r1kh_resp_frame *frame, f;
  1215. struct ft_remote_r0kh *r0kh;
  1216. int pairwise;
  1217. wpa_printf(MSG_DEBUG, "FT: Received PMK-R1 pull response");
  1218. if (data_len < sizeof(*frame))
  1219. return -1;
  1220. r0kh = wpa_auth->conf.r0kh_list;
  1221. while (r0kh) {
  1222. if (os_memcmp(r0kh->addr, src_addr, ETH_ALEN) == 0)
  1223. break;
  1224. r0kh = r0kh->next;
  1225. }
  1226. if (r0kh == NULL) {
  1227. wpa_printf(MSG_DEBUG, "FT: No matching R0KH address found for "
  1228. "PMK-R0 pull response source address " MACSTR,
  1229. MAC2STR(src_addr));
  1230. return -1;
  1231. }
  1232. frame = (struct ft_r0kh_r1kh_resp_frame *) data;
  1233. /* aes_unwrap() does not support inplace decryption, so use a temporary
  1234. * buffer for the data. */
  1235. if (aes_unwrap(r0kh->key, (FT_R0KH_R1KH_RESP_DATA_LEN + 7) / 8,
  1236. frame->nonce, f.nonce) < 0) {
  1237. wpa_printf(MSG_DEBUG, "FT: Failed to decrypt PMK-R1 pull "
  1238. "response from " MACSTR, MAC2STR(src_addr));
  1239. return -1;
  1240. }
  1241. if (os_memcmp(f.r1kh_id, wpa_auth->conf.r1_key_holder, FT_R1KH_ID_LEN)
  1242. != 0) {
  1243. wpa_printf(MSG_DEBUG, "FT: PMK-R1 pull response did not use a "
  1244. "matching R1KH-ID");
  1245. return -1;
  1246. }
  1247. /* TODO: verify that <nonce,s1kh_id> matches with a pending request
  1248. * and call this requests callback function to finish request
  1249. * processing */
  1250. pairwise = le_to_host16(f.pairwise);
  1251. wpa_hexdump(MSG_DEBUG, "FT: PMK-R1 pull - nonce",
  1252. f.nonce, sizeof(f.nonce));
  1253. wpa_printf(MSG_DEBUG, "FT: PMK-R1 pull - R1KH-ID=" MACSTR "S1KH-ID="
  1254. MACSTR " pairwise=0x%x",
  1255. MAC2STR(f.r1kh_id), MAC2STR(f.s1kh_id), pairwise);
  1256. wpa_hexdump_key(MSG_DEBUG, "FT: PMK-R1 pull - PMK-R1",
  1257. f.pmk_r1, PMK_LEN);
  1258. wpa_hexdump(MSG_DEBUG, "FT: PMK-R1 pull - PMKR1Name",
  1259. f.pmk_r1_name, WPA_PMK_NAME_LEN);
  1260. wpa_ft_store_pmk_r1(wpa_auth, f.s1kh_id, f.pmk_r1, f.pmk_r1_name,
  1261. pairwise);
  1262. os_memset(f.pmk_r1, 0, PMK_LEN);
  1263. return 0;
  1264. }
  1265. static int wpa_ft_rrb_rx_push(struct wpa_authenticator *wpa_auth,
  1266. const u8 *src_addr,
  1267. const u8 *data, size_t data_len)
  1268. {
  1269. struct ft_r0kh_r1kh_push_frame *frame, f;
  1270. struct ft_remote_r0kh *r0kh;
  1271. struct os_time now;
  1272. os_time_t tsend;
  1273. int pairwise;
  1274. wpa_printf(MSG_DEBUG, "FT: Received PMK-R1 push");
  1275. if (data_len < sizeof(*frame))
  1276. return -1;
  1277. r0kh = wpa_auth->conf.r0kh_list;
  1278. while (r0kh) {
  1279. if (os_memcmp(r0kh->addr, src_addr, ETH_ALEN) == 0)
  1280. break;
  1281. r0kh = r0kh->next;
  1282. }
  1283. if (r0kh == NULL) {
  1284. wpa_printf(MSG_DEBUG, "FT: No matching R0KH address found for "
  1285. "PMK-R0 push source address " MACSTR,
  1286. MAC2STR(src_addr));
  1287. return -1;
  1288. }
  1289. frame = (struct ft_r0kh_r1kh_push_frame *) data;
  1290. /* aes_unwrap() does not support inplace decryption, so use a temporary
  1291. * buffer for the data. */
  1292. if (aes_unwrap(r0kh->key, (FT_R0KH_R1KH_PUSH_DATA_LEN + 7) / 8,
  1293. frame->timestamp, f.timestamp) < 0) {
  1294. wpa_printf(MSG_DEBUG, "FT: Failed to decrypt PMK-R1 push from "
  1295. MACSTR, MAC2STR(src_addr));
  1296. return -1;
  1297. }
  1298. os_get_time(&now);
  1299. tsend = WPA_GET_LE32(f.timestamp);
  1300. if ((now.sec > tsend && now.sec - tsend > 60) ||
  1301. (now.sec < tsend && tsend - now.sec > 60)) {
  1302. wpa_printf(MSG_DEBUG, "FT: PMK-R1 push did not have a valid "
  1303. "timestamp: sender time %d own time %d\n",
  1304. (int) tsend, (int) now.sec);
  1305. return -1;
  1306. }
  1307. if (os_memcmp(f.r1kh_id, wpa_auth->conf.r1_key_holder, FT_R1KH_ID_LEN)
  1308. != 0) {
  1309. wpa_printf(MSG_DEBUG, "FT: PMK-R1 push did not use a matching "
  1310. "R1KH-ID (received " MACSTR " own " MACSTR ")",
  1311. MAC2STR(f.r1kh_id),
  1312. MAC2STR(wpa_auth->conf.r1_key_holder));
  1313. return -1;
  1314. }
  1315. pairwise = le_to_host16(f.pairwise);
  1316. wpa_printf(MSG_DEBUG, "FT: PMK-R1 push - R1KH-ID=" MACSTR " S1KH-ID="
  1317. MACSTR " pairwise=0x%x",
  1318. MAC2STR(f.r1kh_id), MAC2STR(f.s1kh_id), pairwise);
  1319. wpa_hexdump_key(MSG_DEBUG, "FT: PMK-R1 push - PMK-R1",
  1320. f.pmk_r1, PMK_LEN);
  1321. wpa_hexdump(MSG_DEBUG, "FT: PMK-R1 push - PMKR1Name",
  1322. f.pmk_r1_name, WPA_PMK_NAME_LEN);
  1323. wpa_ft_store_pmk_r1(wpa_auth, f.s1kh_id, f.pmk_r1, f.pmk_r1_name,
  1324. pairwise);
  1325. os_memset(f.pmk_r1, 0, PMK_LEN);
  1326. return 0;
  1327. }
  1328. int wpa_ft_rrb_rx(struct wpa_authenticator *wpa_auth, const u8 *src_addr,
  1329. const u8 *data, size_t data_len)
  1330. {
  1331. struct ft_rrb_frame *frame;
  1332. u16 alen;
  1333. const u8 *pos, *end, *start;
  1334. u8 action;
  1335. const u8 *sta_addr, *target_ap_addr;
  1336. wpa_printf(MSG_DEBUG, "FT: RRB received frame from remote AP " MACSTR,
  1337. MAC2STR(src_addr));
  1338. if (data_len < sizeof(*frame)) {
  1339. wpa_printf(MSG_DEBUG, "FT: Too short RRB frame (data_len=%lu)",
  1340. (unsigned long) data_len);
  1341. return -1;
  1342. }
  1343. pos = data;
  1344. frame = (struct ft_rrb_frame *) pos;
  1345. pos += sizeof(*frame);
  1346. alen = le_to_host16(frame->action_length);
  1347. wpa_printf(MSG_DEBUG, "FT: RRB frame - frame_type=%d packet_type=%d "
  1348. "action_length=%d ap_address=" MACSTR,
  1349. frame->frame_type, frame->packet_type, alen,
  1350. MAC2STR(frame->ap_address));
  1351. if (frame->frame_type != RSN_REMOTE_FRAME_TYPE_FT_RRB) {
  1352. /* Discard frame per IEEE Std 802.11r-2008, 11A.10.3 */
  1353. wpa_printf(MSG_DEBUG, "FT: RRB discarded frame with "
  1354. "unrecognized type %d", frame->frame_type);
  1355. return -1;
  1356. }
  1357. if (alen > data_len - sizeof(*frame)) {
  1358. wpa_printf(MSG_DEBUG, "FT: RRB frame too short for action "
  1359. "frame");
  1360. return -1;
  1361. }
  1362. if (frame->packet_type == FT_PACKET_R0KH_R1KH_PULL)
  1363. return wpa_ft_rrb_rx_pull(wpa_auth, src_addr, data, data_len);
  1364. if (frame->packet_type == FT_PACKET_R0KH_R1KH_RESP)
  1365. return wpa_ft_rrb_rx_resp(wpa_auth, src_addr, data, data_len);
  1366. if (frame->packet_type == FT_PACKET_R0KH_R1KH_PUSH)
  1367. return wpa_ft_rrb_rx_push(wpa_auth, src_addr, data, data_len);
  1368. wpa_hexdump(MSG_MSGDUMP, "FT: RRB - FT Action frame", pos, alen);
  1369. if (alen < 1 + 1 + 2 * ETH_ALEN) {
  1370. wpa_printf(MSG_DEBUG, "FT: Too short RRB frame (not enough "
  1371. "room for Action Frame body); alen=%lu",
  1372. (unsigned long) alen);
  1373. return -1;
  1374. }
  1375. start = pos;
  1376. end = pos + alen;
  1377. if (*pos != WLAN_ACTION_FT) {
  1378. wpa_printf(MSG_DEBUG, "FT: Unexpected Action frame category "
  1379. "%d", *pos);
  1380. return -1;
  1381. }
  1382. pos++;
  1383. action = *pos++;
  1384. sta_addr = pos;
  1385. pos += ETH_ALEN;
  1386. target_ap_addr = pos;
  1387. pos += ETH_ALEN;
  1388. wpa_printf(MSG_DEBUG, "FT: RRB Action Frame: action=%d sta_addr="
  1389. MACSTR " target_ap_addr=" MACSTR,
  1390. action, MAC2STR(sta_addr), MAC2STR(target_ap_addr));
  1391. if (frame->packet_type == FT_PACKET_REQUEST) {
  1392. wpa_printf(MSG_DEBUG, "FT: FT Packet Type - Request");
  1393. if (action != 1) {
  1394. wpa_printf(MSG_DEBUG, "FT: Unexpected Action %d in "
  1395. "RRB Request", action);
  1396. return -1;
  1397. }
  1398. if (os_memcmp(target_ap_addr, wpa_auth->addr, ETH_ALEN) != 0) {
  1399. wpa_printf(MSG_DEBUG, "FT: Target AP address in the "
  1400. "RRB Request does not match with own "
  1401. "address");
  1402. return -1;
  1403. }
  1404. if (wpa_ft_rrb_rx_request(wpa_auth, frame->ap_address,
  1405. sta_addr, pos, end - pos) < 0)
  1406. return -1;
  1407. } else if (frame->packet_type == FT_PACKET_RESPONSE) {
  1408. u16 status_code;
  1409. if (end - pos < 2) {
  1410. wpa_printf(MSG_DEBUG, "FT: Not enough room for status "
  1411. "code in RRB Response");
  1412. return -1;
  1413. }
  1414. status_code = WPA_GET_LE16(pos);
  1415. pos += 2;
  1416. wpa_printf(MSG_DEBUG, "FT: FT Packet Type - Response "
  1417. "(status_code=%d)", status_code);
  1418. if (wpa_ft_action_send(wpa_auth, sta_addr, start, alen) < 0)
  1419. return -1;
  1420. } else {
  1421. wpa_printf(MSG_DEBUG, "FT: RRB discarded frame with unknown "
  1422. "packet_type %d", frame->packet_type);
  1423. return -1;
  1424. }
  1425. return 0;
  1426. }
  1427. static void wpa_ft_generate_pmk_r1(struct wpa_authenticator *wpa_auth,
  1428. struct wpa_ft_pmk_r0_sa *pmk_r0,
  1429. struct ft_remote_r1kh *r1kh,
  1430. const u8 *s1kh_id, int pairwise)
  1431. {
  1432. struct ft_r0kh_r1kh_push_frame frame, f;
  1433. struct os_time now;
  1434. os_memset(&frame, 0, sizeof(frame));
  1435. frame.frame_type = RSN_REMOTE_FRAME_TYPE_FT_RRB;
  1436. frame.packet_type = FT_PACKET_R0KH_R1KH_PUSH;
  1437. frame.data_length = host_to_le16(FT_R0KH_R1KH_PUSH_DATA_LEN);
  1438. os_memcpy(frame.ap_address, wpa_auth->addr, ETH_ALEN);
  1439. /* aes_wrap() does not support inplace encryption, so use a temporary
  1440. * buffer for the data. */
  1441. os_memcpy(f.r1kh_id, r1kh->id, FT_R1KH_ID_LEN);
  1442. os_memcpy(f.s1kh_id, s1kh_id, ETH_ALEN);
  1443. os_memcpy(f.pmk_r0_name, pmk_r0->pmk_r0_name, WPA_PMK_NAME_LEN);
  1444. wpa_derive_pmk_r1(pmk_r0->pmk_r0, pmk_r0->pmk_r0_name, r1kh->id,
  1445. s1kh_id, f.pmk_r1, f.pmk_r1_name);
  1446. wpa_printf(MSG_DEBUG, "FT: R1KH-ID " MACSTR, MAC2STR(r1kh->id));
  1447. wpa_hexdump_key(MSG_DEBUG, "FT: PMK-R1", f.pmk_r1, PMK_LEN);
  1448. wpa_hexdump(MSG_DEBUG, "FT: PMKR1Name", f.pmk_r1_name,
  1449. WPA_PMK_NAME_LEN);
  1450. os_get_time(&now);
  1451. WPA_PUT_LE32(f.timestamp, now.sec);
  1452. f.pairwise = host_to_le16(pairwise);
  1453. if (aes_wrap(r1kh->key, (FT_R0KH_R1KH_PUSH_DATA_LEN + 7) / 8,
  1454. f.timestamp, frame.timestamp) < 0)
  1455. return;
  1456. wpa_ft_rrb_send(wpa_auth, r1kh->addr, (u8 *) &frame, sizeof(frame));
  1457. }
  1458. void wpa_ft_push_pmk_r1(struct wpa_authenticator *wpa_auth, const u8 *addr)
  1459. {
  1460. struct wpa_ft_pmk_r0_sa *r0;
  1461. struct ft_remote_r1kh *r1kh;
  1462. if (!wpa_auth->conf.pmk_r1_push)
  1463. return;
  1464. r0 = wpa_auth->ft_pmk_cache->pmk_r0;
  1465. while (r0) {
  1466. if (os_memcmp(r0->spa, addr, ETH_ALEN) == 0)
  1467. break;
  1468. r0 = r0->next;
  1469. }
  1470. if (r0 == NULL || r0->pmk_r1_pushed)
  1471. return;
  1472. r0->pmk_r1_pushed = 1;
  1473. wpa_printf(MSG_DEBUG, "FT: Deriving and pushing PMK-R1 keys to R1KHs "
  1474. "for STA " MACSTR, MAC2STR(addr));
  1475. r1kh = wpa_auth->conf.r1kh_list;
  1476. while (r1kh) {
  1477. wpa_ft_generate_pmk_r1(wpa_auth, r0, r1kh, addr, r0->pairwise);
  1478. r1kh = r1kh->next;
  1479. }
  1480. }
  1481. #endif /* CONFIG_IEEE80211R */