mesh_rsn.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762
  1. /*
  2. * WPA Supplicant - Mesh RSN routines
  3. * Copyright (c) 2013-2014, cozybit, Inc. All rights reserved.
  4. *
  5. * This software may be distributed under the terms of the BSD license.
  6. * See README for more details.
  7. */
  8. #include "utils/includes.h"
  9. #include "utils/common.h"
  10. #include "utils/eloop.h"
  11. #include "crypto/sha256.h"
  12. #include "crypto/random.h"
  13. #include "crypto/aes.h"
  14. #include "crypto/aes_siv.h"
  15. #include "rsn_supp/wpa.h"
  16. #include "ap/hostapd.h"
  17. #include "ap/wpa_auth.h"
  18. #include "ap/sta_info.h"
  19. #include "ap/ieee802_11.h"
  20. #include "wpa_supplicant_i.h"
  21. #include "driver_i.h"
  22. #include "wpas_glue.h"
  23. #include "mesh_mpm.h"
  24. #include "mesh_rsn.h"
  25. #define MESH_AUTH_TIMEOUT 10
  26. #define MESH_AUTH_RETRY 3
  27. void mesh_auth_timer(void *eloop_ctx, void *user_data)
  28. {
  29. struct wpa_supplicant *wpa_s = eloop_ctx;
  30. struct sta_info *sta = user_data;
  31. struct hostapd_data *hapd;
  32. if (sta->sae->state != SAE_ACCEPTED) {
  33. wpa_printf(MSG_DEBUG, "AUTH: Re-authenticate with " MACSTR
  34. " (attempt %d) ",
  35. MAC2STR(sta->addr), sta->sae_auth_retry);
  36. wpa_msg(wpa_s, MSG_INFO, MESH_SAE_AUTH_FAILURE "addr=" MACSTR,
  37. MAC2STR(sta->addr));
  38. if (sta->sae_auth_retry < MESH_AUTH_RETRY) {
  39. mesh_rsn_auth_sae_sta(wpa_s, sta);
  40. } else {
  41. hapd = wpa_s->ifmsh->bss[0];
  42. if (sta->sae_auth_retry > MESH_AUTH_RETRY) {
  43. ap_free_sta(hapd, sta);
  44. return;
  45. }
  46. /* block the STA if exceeded the number of attempts */
  47. wpa_mesh_set_plink_state(wpa_s, sta, PLINK_BLOCKED);
  48. sta->sae->state = SAE_NOTHING;
  49. wpa_msg(wpa_s, MSG_INFO, MESH_SAE_AUTH_BLOCKED "addr="
  50. MACSTR " duration=%d",
  51. MAC2STR(sta->addr),
  52. hapd->conf->ap_max_inactivity);
  53. }
  54. sta->sae_auth_retry++;
  55. }
  56. }
  57. static void auth_logger(void *ctx, const u8 *addr, logger_level level,
  58. const char *txt)
  59. {
  60. if (addr)
  61. wpa_printf(MSG_DEBUG, "AUTH: " MACSTR " - %s",
  62. MAC2STR(addr), txt);
  63. else
  64. wpa_printf(MSG_DEBUG, "AUTH: %s", txt);
  65. }
  66. static const u8 *auth_get_psk(void *ctx, const u8 *addr,
  67. const u8 *p2p_dev_addr, const u8 *prev_psk)
  68. {
  69. struct mesh_rsn *mesh_rsn = ctx;
  70. struct hostapd_data *hapd = mesh_rsn->wpa_s->ifmsh->bss[0];
  71. struct sta_info *sta = ap_get_sta(hapd, addr);
  72. wpa_printf(MSG_DEBUG, "AUTH: %s (addr=" MACSTR " prev_psk=%p)",
  73. __func__, MAC2STR(addr), prev_psk);
  74. if (sta && sta->auth_alg == WLAN_AUTH_SAE) {
  75. if (!sta->sae || prev_psk)
  76. return NULL;
  77. return sta->sae->pmk;
  78. }
  79. return NULL;
  80. }
  81. static int auth_set_key(void *ctx, int vlan_id, enum wpa_alg alg,
  82. const u8 *addr, int idx, u8 *key, size_t key_len)
  83. {
  84. struct mesh_rsn *mesh_rsn = ctx;
  85. u8 seq[6];
  86. os_memset(seq, 0, sizeof(seq));
  87. if (addr) {
  88. wpa_printf(MSG_DEBUG, "AUTH: %s(alg=%d addr=" MACSTR
  89. " key_idx=%d)",
  90. __func__, alg, MAC2STR(addr), idx);
  91. } else {
  92. wpa_printf(MSG_DEBUG, "AUTH: %s(alg=%d key_idx=%d)",
  93. __func__, alg, idx);
  94. }
  95. wpa_hexdump_key(MSG_DEBUG, "AUTH: set_key - key", key, key_len);
  96. return wpa_drv_set_key(mesh_rsn->wpa_s, alg, addr, idx,
  97. 1, seq, 6, key, key_len);
  98. }
  99. static int auth_start_ampe(void *ctx, const u8 *addr)
  100. {
  101. struct mesh_rsn *mesh_rsn = ctx;
  102. struct hostapd_data *hapd;
  103. struct sta_info *sta;
  104. if (mesh_rsn->wpa_s->current_ssid->mode != WPAS_MODE_MESH)
  105. return -1;
  106. hapd = mesh_rsn->wpa_s->ifmsh->bss[0];
  107. sta = ap_get_sta(hapd, addr);
  108. if (sta)
  109. eloop_cancel_timeout(mesh_auth_timer, mesh_rsn->wpa_s, sta);
  110. mesh_mpm_auth_peer(mesh_rsn->wpa_s, addr);
  111. return 0;
  112. }
  113. static int __mesh_rsn_auth_init(struct mesh_rsn *rsn, const u8 *addr,
  114. enum mfp_options ieee80211w)
  115. {
  116. struct wpa_auth_config conf;
  117. struct wpa_auth_callbacks cb;
  118. u8 seq[6] = {};
  119. wpa_printf(MSG_DEBUG, "AUTH: Initializing group state machine");
  120. os_memset(&conf, 0, sizeof(conf));
  121. conf.wpa = WPA_PROTO_RSN;
  122. conf.wpa_key_mgmt = WPA_KEY_MGMT_SAE;
  123. conf.wpa_pairwise = rsn->pairwise_cipher;
  124. conf.rsn_pairwise = rsn->pairwise_cipher;
  125. conf.wpa_group = rsn->group_cipher;
  126. conf.eapol_version = 0;
  127. conf.wpa_group_rekey = -1;
  128. #ifdef CONFIG_IEEE80211W
  129. conf.ieee80211w = ieee80211w;
  130. if (ieee80211w != NO_MGMT_FRAME_PROTECTION)
  131. conf.group_mgmt_cipher = rsn->mgmt_group_cipher;
  132. #endif /* CONFIG_IEEE80211W */
  133. os_memset(&cb, 0, sizeof(cb));
  134. cb.ctx = rsn;
  135. cb.logger = auth_logger;
  136. cb.get_psk = auth_get_psk;
  137. cb.set_key = auth_set_key;
  138. cb.start_ampe = auth_start_ampe;
  139. rsn->auth = wpa_init(addr, &conf, &cb);
  140. if (rsn->auth == NULL) {
  141. wpa_printf(MSG_DEBUG, "AUTH: wpa_init() failed");
  142. return -1;
  143. }
  144. /* TODO: support rekeying */
  145. rsn->mgtk_len = wpa_cipher_key_len(conf.wpa_group);
  146. if (random_get_bytes(rsn->mgtk, rsn->mgtk_len) < 0)
  147. return -1;
  148. rsn->mgtk_key_id = 1;
  149. #ifdef CONFIG_IEEE80211W
  150. if (ieee80211w != NO_MGMT_FRAME_PROTECTION) {
  151. rsn->igtk_len = wpa_cipher_key_len(conf.group_mgmt_cipher);
  152. if (random_get_bytes(rsn->igtk, rsn->igtk_len) < 0)
  153. return -1;
  154. rsn->igtk_key_id = 4;
  155. /* group mgmt */
  156. wpa_hexdump_key(MSG_DEBUG, "mesh: Own TX IGTK",
  157. rsn->igtk, rsn->igtk_len);
  158. wpa_drv_set_key(rsn->wpa_s,
  159. wpa_cipher_to_alg(rsn->mgmt_group_cipher), NULL,
  160. rsn->igtk_key_id, 1,
  161. seq, sizeof(seq), rsn->igtk, rsn->igtk_len);
  162. }
  163. #endif /* CONFIG_IEEE80211W */
  164. /* group privacy / data frames */
  165. wpa_hexdump_key(MSG_DEBUG, "mesh: Own TX MGTK",
  166. rsn->mgtk, rsn->mgtk_len);
  167. wpa_drv_set_key(rsn->wpa_s, wpa_cipher_to_alg(rsn->group_cipher), NULL,
  168. rsn->mgtk_key_id, 1, seq, sizeof(seq),
  169. rsn->mgtk, rsn->mgtk_len);
  170. return 0;
  171. }
  172. static void mesh_rsn_deinit(struct mesh_rsn *rsn)
  173. {
  174. os_memset(rsn->mgtk, 0, sizeof(rsn->mgtk));
  175. rsn->mgtk_len = 0;
  176. os_memset(rsn->igtk, 0, sizeof(rsn->igtk));
  177. rsn->igtk_len = 0;
  178. if (rsn->auth)
  179. wpa_deinit(rsn->auth);
  180. }
  181. struct mesh_rsn *mesh_rsn_auth_init(struct wpa_supplicant *wpa_s,
  182. struct mesh_conf *conf)
  183. {
  184. struct mesh_rsn *mesh_rsn;
  185. struct hostapd_data *bss = wpa_s->ifmsh->bss[0];
  186. const u8 *ie;
  187. size_t ie_len;
  188. mesh_rsn = os_zalloc(sizeof(*mesh_rsn));
  189. if (mesh_rsn == NULL)
  190. return NULL;
  191. mesh_rsn->wpa_s = wpa_s;
  192. mesh_rsn->pairwise_cipher = conf->pairwise_cipher;
  193. mesh_rsn->group_cipher = conf->group_cipher;
  194. mesh_rsn->mgmt_group_cipher = conf->mgmt_group_cipher;
  195. if (__mesh_rsn_auth_init(mesh_rsn, wpa_s->own_addr,
  196. conf->ieee80211w) < 0) {
  197. mesh_rsn_deinit(mesh_rsn);
  198. os_free(mesh_rsn);
  199. return NULL;
  200. }
  201. bss->wpa_auth = mesh_rsn->auth;
  202. ie = wpa_auth_get_wpa_ie(mesh_rsn->auth, &ie_len);
  203. conf->rsn_ie = (u8 *) ie;
  204. conf->rsn_ie_len = ie_len;
  205. wpa_supplicant_rsn_supp_set_config(wpa_s, wpa_s->current_ssid);
  206. return mesh_rsn;
  207. }
  208. static int index_within_array(const int *array, int idx)
  209. {
  210. int i;
  211. for (i = 0; i < idx; i++) {
  212. if (array[i] == -1)
  213. return 0;
  214. }
  215. return 1;
  216. }
  217. static int mesh_rsn_sae_group(struct wpa_supplicant *wpa_s,
  218. struct sae_data *sae)
  219. {
  220. int *groups = wpa_s->ifmsh->bss[0]->conf->sae_groups;
  221. /* Configuration may have changed, so validate current index */
  222. if (!index_within_array(groups, wpa_s->mesh_rsn->sae_group_index))
  223. return -1;
  224. for (;;) {
  225. int group = groups[wpa_s->mesh_rsn->sae_group_index];
  226. if (group <= 0)
  227. break;
  228. if (sae_set_group(sae, group) == 0) {
  229. wpa_dbg(wpa_s, MSG_DEBUG, "SME: Selected SAE group %d",
  230. sae->group);
  231. return 0;
  232. }
  233. wpa_s->mesh_rsn->sae_group_index++;
  234. }
  235. return -1;
  236. }
  237. static int mesh_rsn_build_sae_commit(struct wpa_supplicant *wpa_s,
  238. struct wpa_ssid *ssid,
  239. struct sta_info *sta)
  240. {
  241. if (ssid->passphrase == NULL) {
  242. wpa_msg(wpa_s, MSG_DEBUG, "SAE: No password available");
  243. return -1;
  244. }
  245. if (mesh_rsn_sae_group(wpa_s, sta->sae) < 0) {
  246. wpa_msg(wpa_s, MSG_DEBUG, "SAE: Failed to select group");
  247. return -1;
  248. }
  249. return sae_prepare_commit(wpa_s->own_addr, sta->addr,
  250. (u8 *) ssid->passphrase,
  251. os_strlen(ssid->passphrase), sta->sae);
  252. }
  253. /* initiate new SAE authentication with sta */
  254. int mesh_rsn_auth_sae_sta(struct wpa_supplicant *wpa_s,
  255. struct sta_info *sta)
  256. {
  257. struct hostapd_data *hapd = wpa_s->ifmsh->bss[0];
  258. struct wpa_ssid *ssid = wpa_s->current_ssid;
  259. struct rsn_pmksa_cache_entry *pmksa;
  260. unsigned int rnd;
  261. int ret;
  262. if (!ssid) {
  263. wpa_msg(wpa_s, MSG_DEBUG,
  264. "AUTH: No current_ssid known to initiate new SAE");
  265. return -1;
  266. }
  267. if (!sta->sae) {
  268. sta->sae = os_zalloc(sizeof(*sta->sae));
  269. if (sta->sae == NULL)
  270. return -1;
  271. }
  272. pmksa = wpa_auth_pmksa_get(hapd->wpa_auth, sta->addr, NULL);
  273. if (pmksa) {
  274. if (!sta->wpa_sm)
  275. sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth,
  276. sta->addr, NULL);
  277. if (!sta->wpa_sm) {
  278. wpa_printf(MSG_ERROR,
  279. "mesh: Failed to initialize RSN state machine");
  280. return -1;
  281. }
  282. wpa_printf(MSG_DEBUG,
  283. "AUTH: Mesh PMKSA cache entry found for " MACSTR
  284. " - try to use PMKSA caching instead of new SAE authentication",
  285. MAC2STR(sta->addr));
  286. wpa_auth_pmksa_set_to_sm(pmksa, sta->wpa_sm, hapd->wpa_auth,
  287. sta->sae->pmkid, sta->sae->pmk);
  288. sae_accept_sta(hapd, sta);
  289. sta->mesh_sae_pmksa_caching = 1;
  290. return 0;
  291. }
  292. sta->mesh_sae_pmksa_caching = 0;
  293. if (mesh_rsn_build_sae_commit(wpa_s, ssid, sta))
  294. return -1;
  295. wpa_msg(wpa_s, MSG_DEBUG,
  296. "AUTH: started authentication with SAE peer: " MACSTR,
  297. MAC2STR(sta->addr));
  298. ret = auth_sae_init_committed(hapd, sta);
  299. if (ret)
  300. return ret;
  301. eloop_cancel_timeout(mesh_auth_timer, wpa_s, sta);
  302. rnd = rand() % MESH_AUTH_TIMEOUT;
  303. eloop_register_timeout(MESH_AUTH_TIMEOUT + rnd, 0, mesh_auth_timer,
  304. wpa_s, sta);
  305. return 0;
  306. }
  307. void mesh_rsn_get_pmkid(struct mesh_rsn *rsn, struct sta_info *sta, u8 *pmkid)
  308. {
  309. os_memcpy(pmkid, sta->sae->pmkid, SAE_PMKID_LEN);
  310. }
  311. static void
  312. mesh_rsn_derive_aek(struct mesh_rsn *rsn, struct sta_info *sta)
  313. {
  314. u8 *myaddr = rsn->wpa_s->own_addr;
  315. u8 *peer = sta->addr;
  316. u8 *addr1, *addr2;
  317. u8 context[RSN_SELECTOR_LEN + 2 * ETH_ALEN], *ptr = context;
  318. /*
  319. * AEK = KDF-Hash-256(PMK, "AEK Derivation", Selected AKM Suite ||
  320. * min(localMAC, peerMAC) || max(localMAC, peerMAC))
  321. */
  322. /* Selected AKM Suite: SAE */
  323. RSN_SELECTOR_PUT(ptr, RSN_AUTH_KEY_MGMT_SAE);
  324. ptr += RSN_SELECTOR_LEN;
  325. if (os_memcmp(myaddr, peer, ETH_ALEN) < 0) {
  326. addr1 = myaddr;
  327. addr2 = peer;
  328. } else {
  329. addr1 = peer;
  330. addr2 = myaddr;
  331. }
  332. os_memcpy(ptr, addr1, ETH_ALEN);
  333. ptr += ETH_ALEN;
  334. os_memcpy(ptr, addr2, ETH_ALEN);
  335. sha256_prf(sta->sae->pmk, sizeof(sta->sae->pmk), "AEK Derivation",
  336. context, sizeof(context), sta->aek, sizeof(sta->aek));
  337. }
  338. /* derive mesh temporal key from pmk */
  339. int mesh_rsn_derive_mtk(struct wpa_supplicant *wpa_s, struct sta_info *sta)
  340. {
  341. u8 *ptr;
  342. u8 *min, *max;
  343. u8 *myaddr = wpa_s->own_addr;
  344. u8 *peer = sta->addr;
  345. u8 context[2 * WPA_NONCE_LEN + 2 * 2 + RSN_SELECTOR_LEN + 2 * ETH_ALEN];
  346. /*
  347. * MTK = KDF-Hash-Length(PMK, "Temporal Key Derivation", min(localNonce,
  348. * peerNonce) || max(localNonce, peerNonce) || min(localLinkID,
  349. * peerLinkID) || max(localLinkID, peerLinkID) || Selected AKM Suite ||
  350. * min(localMAC, peerMAC) || max(localMAC, peerMAC))
  351. */
  352. ptr = context;
  353. if (os_memcmp(sta->my_nonce, sta->peer_nonce, WPA_NONCE_LEN) < 0) {
  354. min = sta->my_nonce;
  355. max = sta->peer_nonce;
  356. } else {
  357. min = sta->peer_nonce;
  358. max = sta->my_nonce;
  359. }
  360. os_memcpy(ptr, min, WPA_NONCE_LEN);
  361. ptr += WPA_NONCE_LEN;
  362. os_memcpy(ptr, max, WPA_NONCE_LEN);
  363. ptr += WPA_NONCE_LEN;
  364. if (sta->my_lid < sta->peer_lid) {
  365. WPA_PUT_LE16(ptr, sta->my_lid);
  366. ptr += 2;
  367. WPA_PUT_LE16(ptr, sta->peer_lid);
  368. ptr += 2;
  369. } else {
  370. WPA_PUT_LE16(ptr, sta->peer_lid);
  371. ptr += 2;
  372. WPA_PUT_LE16(ptr, sta->my_lid);
  373. ptr += 2;
  374. }
  375. /* Selected AKM Suite: SAE */
  376. RSN_SELECTOR_PUT(ptr, RSN_AUTH_KEY_MGMT_SAE);
  377. ptr += RSN_SELECTOR_LEN;
  378. if (os_memcmp(myaddr, peer, ETH_ALEN) < 0) {
  379. min = myaddr;
  380. max = peer;
  381. } else {
  382. min = peer;
  383. max = myaddr;
  384. }
  385. os_memcpy(ptr, min, ETH_ALEN);
  386. ptr += ETH_ALEN;
  387. os_memcpy(ptr, max, ETH_ALEN);
  388. sta->mtk_len = wpa_cipher_key_len(wpa_s->mesh_rsn->pairwise_cipher);
  389. sha256_prf(sta->sae->pmk, SAE_PMK_LEN,
  390. "Temporal Key Derivation", context, sizeof(context),
  391. sta->mtk, sta->mtk_len);
  392. return 0;
  393. }
  394. void mesh_rsn_init_ampe_sta(struct wpa_supplicant *wpa_s, struct sta_info *sta)
  395. {
  396. if (random_get_bytes(sta->my_nonce, WPA_NONCE_LEN) < 0) {
  397. wpa_printf(MSG_INFO, "mesh: Failed to derive random nonce");
  398. /* TODO: How to handle this more cleanly? */
  399. }
  400. os_memset(sta->peer_nonce, 0, WPA_NONCE_LEN);
  401. mesh_rsn_derive_aek(wpa_s->mesh_rsn, sta);
  402. }
  403. /* insert AMPE and encrypted MIC at @ie.
  404. * @mesh_rsn: mesh RSN context
  405. * @sta: STA we're sending to
  406. * @cat: pointer to category code in frame header.
  407. * @buf: wpabuf to add encrypted AMPE and MIC to.
  408. * */
  409. int mesh_rsn_protect_frame(struct mesh_rsn *rsn, struct sta_info *sta,
  410. const u8 *cat, struct wpabuf *buf)
  411. {
  412. struct ieee80211_ampe_ie *ampe;
  413. u8 const *ie = wpabuf_head_u8(buf) + wpabuf_len(buf);
  414. u8 *ampe_ie, *pos, *mic_payload;
  415. const u8 *aad[] = { rsn->wpa_s->own_addr, sta->addr, cat };
  416. const size_t aad_len[] = { ETH_ALEN, ETH_ALEN, ie - cat };
  417. int ret = 0;
  418. size_t len;
  419. len = sizeof(*ampe);
  420. if (cat[1] == PLINK_OPEN)
  421. len += rsn->mgtk_len + WPA_KEY_RSC_LEN + 4;
  422. #ifdef CONFIG_IEEE80211W
  423. if (cat[1] == PLINK_OPEN && rsn->igtk_len)
  424. len += 2 + 6 + rsn->igtk_len;
  425. #endif /* CONFIG_IEEE80211W */
  426. if (2 + AES_BLOCK_SIZE + 2 + len > wpabuf_tailroom(buf)) {
  427. wpa_printf(MSG_ERROR, "protect frame: buffer too small");
  428. return -EINVAL;
  429. }
  430. ampe_ie = os_zalloc(2 + len);
  431. if (!ampe_ie) {
  432. wpa_printf(MSG_ERROR, "protect frame: out of memory");
  433. return -ENOMEM;
  434. }
  435. /* IE: AMPE */
  436. ampe_ie[0] = WLAN_EID_AMPE;
  437. ampe_ie[1] = len;
  438. ampe = (struct ieee80211_ampe_ie *) (ampe_ie + 2);
  439. RSN_SELECTOR_PUT(ampe->selected_pairwise_suite,
  440. RSN_CIPHER_SUITE_CCMP);
  441. os_memcpy(ampe->local_nonce, sta->my_nonce, WPA_NONCE_LEN);
  442. os_memcpy(ampe->peer_nonce, sta->peer_nonce, WPA_NONCE_LEN);
  443. pos = (u8 *) (ampe + 1);
  444. if (cat[1] != PLINK_OPEN)
  445. goto skip_keys;
  446. /* TODO: Key Replay Counter[8] optionally for
  447. * Mesh Group Key Inform/Acknowledge frames */
  448. /* TODO: static mgtk for now since we don't support rekeying! */
  449. /*
  450. * GTKdata[variable]:
  451. * MGTK[variable] || Key RSC[8] || GTKExpirationTime[4]
  452. */
  453. os_memcpy(pos, rsn->mgtk, rsn->mgtk_len);
  454. pos += rsn->mgtk_len;
  455. wpa_drv_get_seqnum(rsn->wpa_s, NULL, rsn->mgtk_key_id, pos);
  456. pos += WPA_KEY_RSC_LEN;
  457. /* Use fixed GTKExpirationTime for now */
  458. WPA_PUT_LE32(pos, 0xffffffff);
  459. pos += 4;
  460. #ifdef CONFIG_IEEE80211W
  461. /*
  462. * IGTKdata[variable]:
  463. * Key ID[2], IPN[6], IGTK[variable]
  464. */
  465. if (rsn->igtk_len) {
  466. WPA_PUT_LE16(pos, rsn->igtk_key_id);
  467. pos += 2;
  468. wpa_drv_get_seqnum(rsn->wpa_s, NULL, rsn->igtk_key_id, pos);
  469. pos += 6;
  470. os_memcpy(pos, rsn->igtk, rsn->igtk_len);
  471. }
  472. #endif /* CONFIG_IEEE80211W */
  473. skip_keys:
  474. wpa_hexdump_key(MSG_DEBUG, "mesh: Plaintext AMPE element",
  475. ampe_ie, 2 + len);
  476. /* IE: MIC */
  477. wpabuf_put_u8(buf, WLAN_EID_MIC);
  478. wpabuf_put_u8(buf, AES_BLOCK_SIZE);
  479. /* MIC field is output ciphertext */
  480. /* encrypt after MIC */
  481. mic_payload = wpabuf_put(buf, 2 + len + AES_BLOCK_SIZE);
  482. if (aes_siv_encrypt(sta->aek, sizeof(sta->aek), ampe_ie, 2 + len, 3,
  483. aad, aad_len, mic_payload)) {
  484. wpa_printf(MSG_ERROR, "protect frame: failed to encrypt");
  485. ret = -ENOMEM;
  486. }
  487. os_free(ampe_ie);
  488. return ret;
  489. }
  490. int mesh_rsn_process_ampe(struct wpa_supplicant *wpa_s, struct sta_info *sta,
  491. struct ieee802_11_elems *elems, const u8 *cat,
  492. const u8 *chosen_pmk,
  493. const u8 *start, size_t elems_len)
  494. {
  495. int ret = 0;
  496. struct ieee80211_ampe_ie *ampe;
  497. u8 null_nonce[WPA_NONCE_LEN] = {};
  498. u8 ampe_eid;
  499. u8 ampe_ie_len;
  500. u8 *ampe_buf, *crypt = NULL, *pos, *end;
  501. size_t crypt_len;
  502. const u8 *aad[] = { sta->addr, wpa_s->own_addr, cat };
  503. const size_t aad_len[] = { ETH_ALEN, ETH_ALEN,
  504. (elems->mic - 2) - cat };
  505. size_t key_len;
  506. if (!sta->sae) {
  507. struct hostapd_data *hapd = wpa_s->ifmsh->bss[0];
  508. if (!wpa_auth_pmksa_get(hapd->wpa_auth, sta->addr, NULL)) {
  509. wpa_printf(MSG_INFO,
  510. "Mesh RSN: SAE is not prepared yet");
  511. return -1;
  512. }
  513. mesh_rsn_auth_sae_sta(wpa_s, sta);
  514. }
  515. if (chosen_pmk && os_memcmp(chosen_pmk, sta->sae->pmkid, PMKID_LEN)) {
  516. wpa_msg(wpa_s, MSG_DEBUG,
  517. "Mesh RSN: Invalid PMKID (Chosen PMK did not match calculated PMKID)");
  518. return -1;
  519. }
  520. if (!elems->mic || elems->mic_len < AES_BLOCK_SIZE) {
  521. wpa_msg(wpa_s, MSG_DEBUG, "Mesh RSN: missing mic ie");
  522. return -1;
  523. }
  524. ampe_buf = (u8 *) elems->mic + elems->mic_len;
  525. if ((int) elems_len < ampe_buf - start)
  526. return -1;
  527. crypt_len = elems_len - (elems->mic - start);
  528. if (crypt_len < 2 + AES_BLOCK_SIZE) {
  529. wpa_msg(wpa_s, MSG_DEBUG, "Mesh RSN: missing ampe ie");
  530. return -1;
  531. }
  532. /* crypt is modified by siv_decrypt */
  533. crypt = os_zalloc(crypt_len);
  534. if (!crypt) {
  535. wpa_printf(MSG_ERROR, "Mesh RSN: out of memory");
  536. ret = -ENOMEM;
  537. goto free;
  538. }
  539. os_memcpy(crypt, elems->mic, crypt_len);
  540. if (aes_siv_decrypt(sta->aek, sizeof(sta->aek), crypt, crypt_len, 3,
  541. aad, aad_len, ampe_buf)) {
  542. wpa_printf(MSG_ERROR, "Mesh RSN: frame verification failed!");
  543. ret = -2;
  544. goto free;
  545. }
  546. crypt_len -= AES_BLOCK_SIZE;
  547. wpa_hexdump_key(MSG_DEBUG, "mesh: Decrypted AMPE element",
  548. ampe_buf, crypt_len);
  549. ampe_eid = *ampe_buf++;
  550. ampe_ie_len = *ampe_buf++;
  551. if (ampe_eid != WLAN_EID_AMPE ||
  552. (size_t) 2 + ampe_ie_len > crypt_len ||
  553. ampe_ie_len < sizeof(struct ieee80211_ampe_ie)) {
  554. wpa_msg(wpa_s, MSG_DEBUG, "Mesh RSN: invalid ampe ie");
  555. ret = -1;
  556. goto free;
  557. }
  558. ampe = (struct ieee80211_ampe_ie *) ampe_buf;
  559. pos = (u8 *) (ampe + 1);
  560. end = ampe_buf + ampe_ie_len;
  561. if (os_memcmp(ampe->peer_nonce, null_nonce, WPA_NONCE_LEN) != 0 &&
  562. os_memcmp(ampe->peer_nonce, sta->my_nonce, WPA_NONCE_LEN) != 0) {
  563. wpa_msg(wpa_s, MSG_DEBUG, "Mesh RSN: invalid peer nonce");
  564. ret = -1;
  565. goto free;
  566. }
  567. os_memcpy(sta->peer_nonce, ampe->local_nonce,
  568. sizeof(ampe->local_nonce));
  569. /* TODO: Key Replay Counter[8] in Mesh Group Key Inform/Acknowledge
  570. * frames */
  571. /*
  572. * GTKdata shall not be included in Mesh Peering Confirm. While the
  573. * standard does not state the same about IGTKdata, that same constraint
  574. * needs to apply for it. It makes no sense to include the keys in Mesh
  575. * Peering Close frames either, so while the standard does not seem to
  576. * have a shall statement for these, they are described without
  577. * mentioning GTKdata.
  578. *
  579. * An earlier implementation used to add GTKdata to both Mesh Peering
  580. * Open and Mesh Peering Confirm frames, so ignore the possibly present
  581. * GTKdata frame without rejecting the frame as a backwards
  582. * compatibility mechanism.
  583. */
  584. if (cat[1] != PLINK_OPEN) {
  585. if (end > pos) {
  586. wpa_hexdump_key(MSG_DEBUG,
  587. "mesh: Ignore unexpected GTKdata(etc.) fields in the end of AMPE element in Mesh Peering Confirm/Close",
  588. pos, end - pos);
  589. }
  590. goto free;
  591. }
  592. /*
  593. * GTKdata[variable]:
  594. * MGTK[variable] || Key RSC[8] || GTKExpirationTime[4]
  595. */
  596. sta->mgtk_key_id = 1; /* FIX: Where to get Key ID? */
  597. key_len = wpa_cipher_key_len(wpa_s->mesh_rsn->group_cipher);
  598. if ((int) key_len + WPA_KEY_RSC_LEN + 4 > end - pos) {
  599. wpa_dbg(wpa_s, MSG_DEBUG, "mesh: Truncated AMPE element");
  600. ret = -1;
  601. goto free;
  602. }
  603. sta->mgtk_len = key_len;
  604. os_memcpy(sta->mgtk, pos, sta->mgtk_len);
  605. wpa_hexdump_key(MSG_DEBUG, "mesh: GTKdata - MGTK",
  606. sta->mgtk, sta->mgtk_len);
  607. pos += sta->mgtk_len;
  608. wpa_hexdump(MSG_DEBUG, "mesh: GTKdata - MGTK - Key RSC",
  609. pos, WPA_KEY_RSC_LEN);
  610. os_memcpy(sta->mgtk_rsc, pos, sizeof(sta->mgtk_rsc));
  611. pos += WPA_KEY_RSC_LEN;
  612. wpa_printf(MSG_DEBUG,
  613. "mesh: GTKdata - MGTK - GTKExpirationTime: %u seconds",
  614. WPA_GET_LE32(pos));
  615. pos += 4;
  616. #ifdef CONFIG_IEEE80211W
  617. /*
  618. * IGTKdata[variable]:
  619. * Key ID[2], IPN[6], IGTK[variable]
  620. */
  621. key_len = wpa_cipher_key_len(wpa_s->mesh_rsn->mgmt_group_cipher);
  622. if (end - pos >= (int) (2 + 6 + key_len)) {
  623. sta->igtk_key_id = WPA_GET_LE16(pos);
  624. wpa_printf(MSG_DEBUG, "mesh: IGTKdata - Key ID %u",
  625. sta->igtk_key_id);
  626. pos += 2;
  627. os_memcpy(sta->igtk_rsc, pos, sizeof(sta->igtk_rsc));
  628. wpa_hexdump(MSG_DEBUG, "mesh: IGTKdata - IPN",
  629. sta->igtk_rsc, sizeof(sta->igtk_rsc));
  630. pos += 6;
  631. os_memcpy(sta->igtk, pos, key_len);
  632. sta->igtk_len = key_len;
  633. wpa_hexdump_key(MSG_DEBUG, "mesh: IGTKdata - IGTK",
  634. sta->igtk, sta->igtk_len);
  635. }
  636. #endif /* CONFIG_IEEE80211W */
  637. free:
  638. os_free(crypt);
  639. return ret;
  640. }