mesh_rsn.c 20 KB

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