rx_eapol.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989
  1. /*
  2. * Received Data frame processing for EAPOL messages
  3. * Copyright (c) 2010, 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 "crypto/aes_wrap.h"
  17. #include "crypto/crypto.h"
  18. #include "common/defs.h"
  19. #include "common/ieee802_11_defs.h"
  20. #include "common/eapol_common.h"
  21. #include "common/wpa_common.h"
  22. #include "rsn_supp/wpa_ie.h"
  23. #include "wlantest.h"
  24. static int is_zero(const u8 *buf, size_t len)
  25. {
  26. size_t i;
  27. for (i = 0; i < len; i++) {
  28. if (buf[i])
  29. return 0;
  30. }
  31. return 1;
  32. }
  33. static int check_mic(const u8 *kck, int ver, const u8 *data, size_t len)
  34. {
  35. u8 *buf;
  36. int ret = -1;
  37. struct ieee802_1x_hdr *hdr;
  38. struct wpa_eapol_key *key;
  39. u8 rx_mic[16];
  40. buf = os_malloc(len);
  41. if (buf == NULL)
  42. return -1;
  43. os_memcpy(buf, data, len);
  44. hdr = (struct ieee802_1x_hdr *) buf;
  45. key = (struct wpa_eapol_key *) (hdr + 1);
  46. os_memcpy(rx_mic, key->key_mic, 16);
  47. os_memset(key->key_mic, 0, 16);
  48. if (wpa_eapol_key_mic(kck, ver, buf, len, key->key_mic) == 0 &&
  49. os_memcmp(rx_mic, key->key_mic, 16) == 0)
  50. ret = 0;
  51. os_free(buf);
  52. return ret;
  53. }
  54. static void rx_data_eapol_key_1_of_4(struct wlantest *wt, const u8 *dst,
  55. const u8 *src, const u8 *data, size_t len)
  56. {
  57. struct wlantest_bss *bss;
  58. struct wlantest_sta *sta;
  59. const struct ieee802_1x_hdr *eapol;
  60. const struct wpa_eapol_key *hdr;
  61. wpa_printf(MSG_DEBUG, "EAPOL-Key 1/4 " MACSTR " -> " MACSTR,
  62. MAC2STR(src), MAC2STR(dst));
  63. bss = bss_get(wt, src);
  64. if (bss == NULL)
  65. return;
  66. sta = sta_get(bss, dst);
  67. if (sta == NULL)
  68. return;
  69. eapol = (const struct ieee802_1x_hdr *) data;
  70. hdr = (const struct wpa_eapol_key *) (eapol + 1);
  71. if (is_zero(hdr->key_nonce, WPA_NONCE_LEN)) {
  72. wpa_printf(MSG_INFO, "EAPOL-Key 1/4 from " MACSTR " used "
  73. "zero nonce", MAC2STR(src));
  74. }
  75. if (!is_zero(hdr->key_rsc, 8)) {
  76. wpa_printf(MSG_INFO, "EAPOL-Key 1/4 from " MACSTR " used "
  77. "non-zero Key RSC", MAC2STR(src));
  78. }
  79. os_memcpy(sta->anonce, hdr->key_nonce, WPA_NONCE_LEN);
  80. }
  81. static int try_pmk(struct wlantest_bss *bss, struct wlantest_sta *sta,
  82. u16 ver, const u8 *data, size_t len,
  83. struct wlantest_pmk *pmk)
  84. {
  85. struct wpa_ptk ptk;
  86. size_t ptk_len = sta->pairwise_cipher == WPA_CIPHER_TKIP ? 64 : 48;
  87. wpa_pmk_to_ptk(pmk->pmk, sizeof(pmk->pmk),
  88. "Pairwise key expansion",
  89. bss->bssid, sta->addr, sta->anonce, sta->snonce,
  90. (u8 *) &ptk, ptk_len,
  91. wpa_key_mgmt_sha256(sta->key_mgmt));
  92. if (check_mic(ptk.kck, ver, data, len) < 0)
  93. return -1;
  94. wpa_printf(MSG_INFO, "Derived PTK for STA " MACSTR " BSSID " MACSTR,
  95. MAC2STR(sta->addr), MAC2STR(bss->bssid));
  96. sta->counters[WLANTEST_STA_COUNTER_PTK_LEARNED]++;
  97. os_memcpy(&sta->ptk, &ptk, sizeof(ptk));
  98. wpa_hexdump(MSG_DEBUG, "PTK:KCK", sta->ptk.kck, 16);
  99. wpa_hexdump(MSG_DEBUG, "PTK:KEK", sta->ptk.kek, 16);
  100. wpa_hexdump(MSG_DEBUG, "PTK:TK1", sta->ptk.tk1, 16);
  101. if (ptk_len > 48)
  102. wpa_hexdump(MSG_DEBUG, "PTK:TK2", sta->ptk.u.tk2, 16);
  103. sta->ptk_set = 1;
  104. os_memset(sta->rsc_tods, 0, sizeof(sta->rsc_tods));
  105. os_memset(sta->rsc_fromds, 0, sizeof(sta->rsc_fromds));
  106. return 0;
  107. }
  108. static void derive_ptk(struct wlantest *wt, struct wlantest_bss *bss,
  109. struct wlantest_sta *sta, u16 ver,
  110. const u8 *data, size_t len)
  111. {
  112. struct wlantest_pmk *pmk;
  113. dl_list_for_each(pmk, &bss->pmk, struct wlantest_pmk, list) {
  114. if (try_pmk(bss, sta, ver, data, len, pmk) == 0)
  115. return;
  116. }
  117. dl_list_for_each(pmk, &wt->pmk, struct wlantest_pmk, list) {
  118. if (try_pmk(bss, sta, ver, data, len, pmk) == 0)
  119. return;
  120. }
  121. }
  122. static void rx_data_eapol_key_2_of_4(struct wlantest *wt, const u8 *dst,
  123. const u8 *src, const u8 *data, size_t len)
  124. {
  125. struct wlantest_bss *bss;
  126. struct wlantest_sta *sta;
  127. const struct ieee802_1x_hdr *eapol;
  128. const struct wpa_eapol_key *hdr;
  129. const u8 *key_data;
  130. u16 key_info, key_data_len;
  131. struct wpa_eapol_ie_parse ie;
  132. wpa_printf(MSG_DEBUG, "EAPOL-Key 2/4 " MACSTR " -> " MACSTR,
  133. MAC2STR(src), MAC2STR(dst));
  134. bss = bss_get(wt, dst);
  135. if (bss == NULL)
  136. return;
  137. sta = sta_get(bss, src);
  138. if (sta == NULL)
  139. return;
  140. eapol = (const struct ieee802_1x_hdr *) data;
  141. hdr = (const struct wpa_eapol_key *) (eapol + 1);
  142. if (is_zero(hdr->key_nonce, WPA_NONCE_LEN)) {
  143. wpa_printf(MSG_INFO, "EAPOL-Key 2/4 from " MACSTR " used "
  144. "zero nonce", MAC2STR(src));
  145. }
  146. if (!is_zero(hdr->key_rsc, 8)) {
  147. wpa_printf(MSG_INFO, "EAPOL-Key 2/4 from " MACSTR " used "
  148. "non-zero Key RSC", MAC2STR(src));
  149. }
  150. os_memcpy(sta->snonce, hdr->key_nonce, WPA_NONCE_LEN);
  151. key_info = WPA_GET_BE16(hdr->key_info);
  152. key_data_len = WPA_GET_BE16(hdr->key_data_length);
  153. derive_ptk(wt, bss, sta, key_info & WPA_KEY_INFO_TYPE_MASK, data, len);
  154. if (!sta->ptk_set) {
  155. wpa_printf(MSG_DEBUG, "No PTK known to process EAPOL-Key 2/4");
  156. return;
  157. }
  158. if (check_mic(sta->ptk.kck, key_info & WPA_KEY_INFO_TYPE_MASK,
  159. data, len) < 0) {
  160. wpa_printf(MSG_INFO, "Mismatch in EAPOL-Key 2/4 MIC");
  161. return;
  162. }
  163. wpa_printf(MSG_DEBUG, "Valid MIC found in EAPOL-Key 2/4");
  164. key_data = (const u8 *) (hdr + 1);
  165. if (wpa_supplicant_parse_ies(key_data, key_data_len, &ie) < 0) {
  166. wpa_printf(MSG_INFO, "Failed to parse EAPOL-Key Key Data");
  167. return;
  168. }
  169. if (ie.wpa_ie) {
  170. wpa_hexdump(MSG_MSGDUMP, "EAPOL-Key Key Data - WPA IE",
  171. ie.wpa_ie, ie.wpa_ie_len);
  172. if (os_memcmp(ie.wpa_ie, sta->rsnie, ie.wpa_ie_len) != 0) {
  173. wpa_printf(MSG_INFO, "Mismatch in WPA IE between "
  174. "EAPOL-Key 2/4 and (Re)Association "
  175. "Request from " MACSTR, MAC2STR(sta->addr));
  176. wpa_hexdump(MSG_INFO, "WPA IE in EAPOL-Key",
  177. ie.wpa_ie, ie.wpa_ie_len);
  178. wpa_hexdump(MSG_INFO, "WPA IE in (Re)Association "
  179. "Request",
  180. sta->rsnie,
  181. sta->rsnie[0] ? 2 + sta->rsnie[1] : 0);
  182. }
  183. }
  184. if (ie.rsn_ie) {
  185. wpa_hexdump(MSG_MSGDUMP, "EAPOL-Key Key Data - RSN IE",
  186. ie.rsn_ie, ie.rsn_ie_len);
  187. if (os_memcmp(ie.rsn_ie, sta->rsnie, ie.rsn_ie_len) != 0) {
  188. wpa_printf(MSG_INFO, "Mismatch in RSN IE between "
  189. "EAPOL-Key 2/4 and (Re)Association "
  190. "Request from " MACSTR, MAC2STR(sta->addr));
  191. wpa_hexdump(MSG_INFO, "RSN IE in EAPOL-Key",
  192. ie.rsn_ie, ie.rsn_ie_len);
  193. wpa_hexdump(MSG_INFO, "RSN IE in (Re)Association "
  194. "Request",
  195. sta->rsnie,
  196. sta->rsnie[0] ? 2 + sta->rsnie[1] : 0);
  197. }
  198. }
  199. }
  200. static u8 * decrypt_eapol_key_data_rc4(const u8 *kek,
  201. const struct wpa_eapol_key *hdr,
  202. size_t *len)
  203. {
  204. u8 ek[32], *buf;
  205. u16 keydatalen = WPA_GET_BE16(hdr->key_data_length);
  206. buf = os_malloc(keydatalen);
  207. if (buf == NULL)
  208. return NULL;
  209. os_memcpy(ek, hdr->key_iv, 16);
  210. os_memcpy(ek + 16, kek, 16);
  211. os_memcpy(buf, hdr + 1, keydatalen);
  212. if (rc4_skip(ek, 32, 256, buf, keydatalen)) {
  213. wpa_printf(MSG_INFO, "RC4 failed");
  214. os_free(buf);
  215. return NULL;
  216. }
  217. *len = keydatalen;
  218. return buf;
  219. }
  220. static u8 * decrypt_eapol_key_data_aes(const u8 *kek,
  221. const struct wpa_eapol_key *hdr,
  222. size_t *len)
  223. {
  224. u8 *buf;
  225. u16 keydatalen = WPA_GET_BE16(hdr->key_data_length);
  226. if (keydatalen % 8) {
  227. wpa_printf(MSG_INFO, "Unsupported AES-WRAP len %d",
  228. keydatalen);
  229. return NULL;
  230. }
  231. keydatalen -= 8; /* AES-WRAP adds 8 bytes */
  232. buf = os_malloc(keydatalen);
  233. if (buf == NULL)
  234. return NULL;
  235. if (aes_unwrap(kek, keydatalen / 8, (u8 *) (hdr + 1), buf)) {
  236. os_free(buf);
  237. wpa_printf(MSG_INFO, "AES unwrap failed - "
  238. "could not decrypt EAPOL-Key key data");
  239. return NULL;
  240. }
  241. *len = keydatalen;
  242. return buf;
  243. }
  244. static u8 * decrypt_eapol_key_data(const u8 *kek, u16 ver,
  245. const struct wpa_eapol_key *hdr,
  246. size_t *len)
  247. {
  248. switch (ver) {
  249. case WPA_KEY_INFO_TYPE_HMAC_MD5_RC4:
  250. return decrypt_eapol_key_data_rc4(kek, hdr, len);
  251. case WPA_KEY_INFO_TYPE_HMAC_SHA1_AES:
  252. case WPA_KEY_INFO_TYPE_AES_128_CMAC:
  253. return decrypt_eapol_key_data_aes(kek, hdr, len);
  254. default:
  255. wpa_printf(MSG_INFO, "Unsupported EAPOL-Key Key Descriptor "
  256. "Version %u", ver);
  257. return NULL;
  258. }
  259. }
  260. static void learn_kde_keys(struct wlantest_bss *bss, const u8 *buf, size_t len,
  261. const u8 *rsc)
  262. {
  263. struct wpa_eapol_ie_parse ie;
  264. if (wpa_supplicant_parse_ies(buf, len, &ie) < 0) {
  265. wpa_printf(MSG_INFO, "Failed to parse EAPOL-Key Key Data");
  266. return;
  267. }
  268. if (ie.wpa_ie) {
  269. wpa_hexdump(MSG_MSGDUMP, "EAPOL-Key Key Data - WPA IE",
  270. ie.wpa_ie, ie.wpa_ie_len);
  271. }
  272. if (ie.rsn_ie) {
  273. wpa_hexdump(MSG_MSGDUMP, "EAPOL-Key Key Data - RSN IE",
  274. ie.rsn_ie, ie.rsn_ie_len);
  275. }
  276. if (ie.gtk) {
  277. wpa_hexdump(MSG_MSGDUMP, "EAPOL-Key Key Data - GTK KDE",
  278. ie.gtk, ie.gtk_len);
  279. if (ie.gtk_len >= 2 && ie.gtk_len <= 2 + 32) {
  280. int id;
  281. id = ie.gtk[0] & 0x03;
  282. wpa_printf(MSG_DEBUG, "GTK KeyID=%u tx=%u",
  283. id, !!(ie.gtk[0] & 0x04));
  284. if ((ie.gtk[0] & 0xf8) || ie.gtk[1])
  285. wpa_printf(MSG_INFO, "GTK KDE: Reserved field "
  286. "set: %02x %02x",
  287. ie.gtk[0], ie.gtk[1]);
  288. wpa_hexdump(MSG_DEBUG, "GTK", ie.gtk + 2,
  289. ie.gtk_len - 2);
  290. bss->gtk_len[id] = ie.gtk_len - 2;
  291. os_memcpy(bss->gtk[id], ie.gtk + 2, ie.gtk_len - 2);
  292. bss->rsc[id][0] = rsc[5];
  293. bss->rsc[id][1] = rsc[4];
  294. bss->rsc[id][2] = rsc[3];
  295. bss->rsc[id][3] = rsc[2];
  296. bss->rsc[id][4] = rsc[1];
  297. bss->rsc[id][5] = rsc[0];
  298. bss->gtk_idx = id;
  299. wpa_hexdump(MSG_DEBUG, "RSC", bss->rsc[id], 6);
  300. } else {
  301. wpa_printf(MSG_INFO, "Invalid GTK KDE length %u",
  302. (unsigned) ie.gtk_len);
  303. }
  304. }
  305. if (ie.igtk) {
  306. wpa_hexdump(MSG_MSGDUMP, "EAPOL-Key Key Data - IGTK KDE",
  307. ie.igtk, ie.igtk_len);
  308. if (ie.igtk_len == 24) {
  309. u16 id;
  310. id = WPA_GET_LE16(ie.igtk);
  311. if (id > 5) {
  312. wpa_printf(MSG_INFO, "Unexpected IGTK KeyID "
  313. "%u", id);
  314. } else {
  315. const u8 *ipn;
  316. wpa_printf(MSG_DEBUG, "IGTK KeyID %u", id);
  317. wpa_hexdump(MSG_DEBUG, "IPN", ie.igtk + 2, 6);
  318. wpa_hexdump(MSG_DEBUG, "IGTK", ie.igtk + 8,
  319. 16);
  320. os_memcpy(bss->igtk[id], ie.igtk + 8, 16);
  321. bss->igtk_set[id] = 1;
  322. ipn = ie.igtk + 2;
  323. bss->ipn[id][0] = ipn[5];
  324. bss->ipn[id][1] = ipn[4];
  325. bss->ipn[id][2] = ipn[3];
  326. bss->ipn[id][3] = ipn[2];
  327. bss->ipn[id][4] = ipn[1];
  328. bss->ipn[id][5] = ipn[0];
  329. bss->igtk_idx = id;
  330. }
  331. } else {
  332. wpa_printf(MSG_INFO, "Invalid IGTK KDE length %u",
  333. (unsigned) ie.igtk_len);
  334. }
  335. }
  336. }
  337. static void rx_data_eapol_key_3_of_4(struct wlantest *wt, const u8 *dst,
  338. const u8 *src, const u8 *data, size_t len)
  339. {
  340. struct wlantest_bss *bss;
  341. struct wlantest_sta *sta;
  342. const struct ieee802_1x_hdr *eapol;
  343. const struct wpa_eapol_key *hdr;
  344. const u8 *key_data;
  345. int recalc = 0;
  346. u16 key_info, ver;
  347. u8 *decrypted_buf = NULL;
  348. const u8 *decrypted;
  349. size_t decrypted_len = 0;
  350. struct wpa_eapol_ie_parse ie;
  351. wpa_printf(MSG_DEBUG, "EAPOL-Key 3/4 " MACSTR " -> " MACSTR,
  352. MAC2STR(src), MAC2STR(dst));
  353. bss = bss_get(wt, src);
  354. if (bss == NULL)
  355. return;
  356. sta = sta_get(bss, dst);
  357. if (sta == NULL)
  358. return;
  359. eapol = (const struct ieee802_1x_hdr *) data;
  360. hdr = (const struct wpa_eapol_key *) (eapol + 1);
  361. key_info = WPA_GET_BE16(hdr->key_info);
  362. if (os_memcmp(sta->anonce, hdr->key_nonce, WPA_NONCE_LEN) != 0) {
  363. wpa_printf(MSG_INFO, "EAPOL-Key ANonce mismatch between 1/4 "
  364. "and 3/4");
  365. recalc = 1;
  366. }
  367. os_memcpy(sta->anonce, hdr->key_nonce, WPA_NONCE_LEN);
  368. if (recalc) {
  369. derive_ptk(wt, bss, sta, key_info & WPA_KEY_INFO_TYPE_MASK,
  370. data, len);
  371. }
  372. if (!sta->ptk_set) {
  373. wpa_printf(MSG_DEBUG, "No PTK known to process EAPOL-Key 3/4");
  374. return;
  375. }
  376. if (check_mic(sta->ptk.kck, key_info & WPA_KEY_INFO_TYPE_MASK,
  377. data, len) < 0) {
  378. wpa_printf(MSG_INFO, "Mismatch in EAPOL-Key 3/4 MIC");
  379. return;
  380. }
  381. wpa_printf(MSG_DEBUG, "Valid MIC found in EAPOL-Key 3/4");
  382. key_data = (const u8 *) (hdr + 1);
  383. if (!(key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
  384. if (sta->proto & WPA_PROTO_RSN)
  385. wpa_printf(MSG_INFO, "EAPOL-Key 3/4 without "
  386. "EncrKeyData bit");
  387. decrypted = key_data;
  388. decrypted_len = WPA_GET_BE16(hdr->key_data_length);
  389. } else {
  390. ver = key_info & WPA_KEY_INFO_TYPE_MASK;
  391. decrypted_buf = decrypt_eapol_key_data(sta->ptk.kek, ver, hdr,
  392. &decrypted_len);
  393. if (decrypted_buf == NULL) {
  394. wpa_printf(MSG_INFO, "Failed to decrypt EAPOL-Key Key "
  395. "Data");
  396. return;
  397. }
  398. decrypted = decrypted_buf;
  399. wpa_hexdump(MSG_DEBUG, "Decrypted EAPOL-Key Key Data",
  400. decrypted, decrypted_len);
  401. }
  402. if (wt->write_pcap_dumper && decrypted != key_data) {
  403. /* Fill in a dummy Data frame header */
  404. u8 buf[24 + 8 + sizeof(*eapol) + sizeof(*hdr)];
  405. struct ieee80211_hdr *h;
  406. struct wpa_eapol_key *k;
  407. const u8 *p;
  408. u8 *pos;
  409. size_t plain_len;
  410. plain_len = decrypted_len;
  411. p = decrypted;
  412. while (p + 1 < decrypted + decrypted_len) {
  413. if (p[0] == 0xdd && p[1] == 0x00) {
  414. /* Remove padding */
  415. plain_len = p - decrypted;
  416. break;
  417. }
  418. p += 2 + p[1];
  419. }
  420. os_memset(buf, 0, sizeof(buf));
  421. h = (struct ieee80211_hdr *) buf;
  422. h->frame_control = host_to_le16(0x0208);
  423. os_memcpy(h->addr1, dst, ETH_ALEN);
  424. os_memcpy(h->addr2, src, ETH_ALEN);
  425. os_memcpy(h->addr3, src, ETH_ALEN);
  426. pos = (u8 *) (h + 1);
  427. os_memcpy(pos, "\xaa\xaa\x03\x00\x00\x00\x88\x8e", 8);
  428. pos += 8;
  429. os_memcpy(pos, eapol, sizeof(*eapol));
  430. pos += sizeof(*eapol);
  431. os_memcpy(pos, hdr, sizeof(*hdr));
  432. k = (struct wpa_eapol_key *) pos;
  433. WPA_PUT_BE16(k->key_info,
  434. key_info & ~WPA_KEY_INFO_ENCR_KEY_DATA);
  435. WPA_PUT_BE16(k->key_data_length, plain_len);
  436. write_pcap_decrypted(wt, buf, sizeof(buf),
  437. decrypted, plain_len);
  438. }
  439. if (wpa_supplicant_parse_ies(decrypted, decrypted_len, &ie) < 0) {
  440. wpa_printf(MSG_INFO, "Failed to parse EAPOL-Key Key Data");
  441. os_free(decrypted_buf);
  442. return;
  443. }
  444. if ((ie.wpa_ie &&
  445. os_memcmp(ie.wpa_ie, bss->wpaie, ie.wpa_ie_len) != 0) ||
  446. (ie.wpa_ie == NULL && bss->wpaie[0])) {
  447. wpa_printf(MSG_INFO, "Mismatch in WPA IE between "
  448. "EAPOL-Key 3/4 and Beacon/Probe Response "
  449. "from " MACSTR, MAC2STR(bss->bssid));
  450. wpa_hexdump(MSG_INFO, "WPA IE in EAPOL-Key",
  451. ie.wpa_ie, ie.wpa_ie_len);
  452. wpa_hexdump(MSG_INFO, "WPA IE in Beacon/Probe "
  453. "Response",
  454. bss->wpaie,
  455. bss->wpaie[0] ? 2 + bss->wpaie[1] : 0);
  456. }
  457. if ((ie.rsn_ie &&
  458. os_memcmp(ie.rsn_ie, bss->rsnie, ie.rsn_ie_len) != 0) ||
  459. (ie.rsn_ie == NULL && bss->rsnie[0])) {
  460. wpa_printf(MSG_INFO, "Mismatch in RSN IE between "
  461. "EAPOL-Key 3/4 and Beacon/Probe Response "
  462. "from " MACSTR, MAC2STR(bss->bssid));
  463. wpa_hexdump(MSG_INFO, "RSN IE in EAPOL-Key",
  464. ie.rsn_ie, ie.rsn_ie_len);
  465. wpa_hexdump(MSG_INFO, "RSN IE in (Re)Association "
  466. "Request",
  467. bss->rsnie,
  468. bss->rsnie[0] ? 2 + bss->rsnie[1] : 0);
  469. }
  470. learn_kde_keys(bss, decrypted, decrypted_len, hdr->key_rsc);
  471. os_free(decrypted_buf);
  472. }
  473. static void rx_data_eapol_key_4_of_4(struct wlantest *wt, const u8 *dst,
  474. const u8 *src, const u8 *data, size_t len)
  475. {
  476. struct wlantest_bss *bss;
  477. struct wlantest_sta *sta;
  478. const struct ieee802_1x_hdr *eapol;
  479. const struct wpa_eapol_key *hdr;
  480. u16 key_info;
  481. wpa_printf(MSG_DEBUG, "EAPOL-Key 4/4 " MACSTR " -> " MACSTR,
  482. MAC2STR(src), MAC2STR(dst));
  483. bss = bss_get(wt, dst);
  484. if (bss == NULL)
  485. return;
  486. sta = sta_get(bss, src);
  487. if (sta == NULL)
  488. return;
  489. eapol = (const struct ieee802_1x_hdr *) data;
  490. hdr = (const struct wpa_eapol_key *) (eapol + 1);
  491. if (!is_zero(hdr->key_rsc, 8)) {
  492. wpa_printf(MSG_INFO, "EAPOL-Key 4/4 from " MACSTR " used "
  493. "non-zero Key RSC", MAC2STR(src));
  494. }
  495. key_info = WPA_GET_BE16(hdr->key_info);
  496. if (!sta->ptk_set) {
  497. wpa_printf(MSG_DEBUG, "No PTK known to process EAPOL-Key 4/4");
  498. return;
  499. }
  500. if (sta->ptk_set &&
  501. check_mic(sta->ptk.kck, key_info & WPA_KEY_INFO_TYPE_MASK,
  502. data, len) < 0) {
  503. wpa_printf(MSG_INFO, "Mismatch in EAPOL-Key 4/4 MIC");
  504. return;
  505. }
  506. wpa_printf(MSG_DEBUG, "Valid MIC found in EAPOL-Key 4/4");
  507. }
  508. static void rx_data_eapol_key_1_of_2(struct wlantest *wt, const u8 *dst,
  509. const u8 *src, const u8 *data, size_t len)
  510. {
  511. struct wlantest_bss *bss;
  512. struct wlantest_sta *sta;
  513. const struct ieee802_1x_hdr *eapol;
  514. const struct wpa_eapol_key *hdr;
  515. const u8 *key_data;
  516. u16 key_info, ver;
  517. u8 *decrypted;
  518. size_t decrypted_len = 0;
  519. wpa_printf(MSG_DEBUG, "EAPOL-Key 1/2 " MACSTR " -> " MACSTR,
  520. MAC2STR(src), MAC2STR(dst));
  521. bss = bss_get(wt, src);
  522. if (bss == NULL)
  523. return;
  524. sta = sta_get(bss, dst);
  525. if (sta == NULL)
  526. return;
  527. eapol = (const struct ieee802_1x_hdr *) data;
  528. hdr = (const struct wpa_eapol_key *) (eapol + 1);
  529. key_info = WPA_GET_BE16(hdr->key_info);
  530. if (!sta->ptk_set) {
  531. wpa_printf(MSG_DEBUG, "No PTK known to process EAPOL-Key 1/2");
  532. return;
  533. }
  534. if (sta->ptk_set &&
  535. check_mic(sta->ptk.kck, key_info & WPA_KEY_INFO_TYPE_MASK,
  536. data, len) < 0) {
  537. wpa_printf(MSG_INFO, "Mismatch in EAPOL-Key 1/2 MIC");
  538. return;
  539. }
  540. wpa_printf(MSG_DEBUG, "Valid MIC found in EAPOL-Key 1/2");
  541. key_data = (const u8 *) (hdr + 1);
  542. if (sta->proto & WPA_PROTO_RSN &&
  543. !(key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
  544. wpa_printf(MSG_INFO, "EAPOL-Key 1/2 without EncrKeyData bit");
  545. return;
  546. }
  547. ver = key_info & WPA_KEY_INFO_TYPE_MASK;
  548. decrypted = decrypt_eapol_key_data(sta->ptk.kek, ver, hdr,
  549. &decrypted_len);
  550. if (decrypted == NULL) {
  551. wpa_printf(MSG_INFO, "Failed to decrypt EAPOL-Key Key Data");
  552. return;
  553. }
  554. wpa_hexdump(MSG_DEBUG, "Decrypted EAPOL-Key Key Data",
  555. decrypted, decrypted_len);
  556. if (wt->write_pcap_dumper) {
  557. /* Fill in a dummy Data frame header */
  558. u8 buf[24 + 8 + sizeof(*eapol) + sizeof(*hdr)];
  559. struct ieee80211_hdr *h;
  560. struct wpa_eapol_key *k;
  561. u8 *pos;
  562. size_t plain_len;
  563. plain_len = decrypted_len;
  564. pos = decrypted;
  565. while (pos + 1 < decrypted + decrypted_len) {
  566. if (pos[0] == 0xdd && pos[1] == 0x00) {
  567. /* Remove padding */
  568. plain_len = pos - decrypted;
  569. break;
  570. }
  571. pos += 2 + pos[1];
  572. }
  573. os_memset(buf, 0, sizeof(buf));
  574. h = (struct ieee80211_hdr *) buf;
  575. h->frame_control = host_to_le16(0x0208);
  576. os_memcpy(h->addr1, dst, ETH_ALEN);
  577. os_memcpy(h->addr2, src, ETH_ALEN);
  578. os_memcpy(h->addr3, src, ETH_ALEN);
  579. pos = (u8 *) (h + 1);
  580. os_memcpy(pos, "\xaa\xaa\x03\x00\x00\x00\x88\x8e", 8);
  581. pos += 8;
  582. os_memcpy(pos, eapol, sizeof(*eapol));
  583. pos += sizeof(*eapol);
  584. os_memcpy(pos, hdr, sizeof(*hdr));
  585. k = (struct wpa_eapol_key *) pos;
  586. WPA_PUT_BE16(k->key_info,
  587. key_info & ~WPA_KEY_INFO_ENCR_KEY_DATA);
  588. WPA_PUT_BE16(k->key_data_length, plain_len);
  589. write_pcap_decrypted(wt, buf, sizeof(buf),
  590. decrypted, plain_len);
  591. }
  592. if (sta->proto & WPA_PROTO_RSN)
  593. learn_kde_keys(bss, decrypted, decrypted_len, hdr->key_rsc);
  594. else {
  595. int len = bss->group_cipher == WPA_CIPHER_TKIP ? 32 : 16;
  596. if (decrypted_len == len) {
  597. const u8 *rsc = hdr->key_rsc;
  598. int id;
  599. id = (key_info & WPA_KEY_INFO_KEY_INDEX_MASK) >>
  600. WPA_KEY_INFO_KEY_INDEX_SHIFT;
  601. wpa_printf(MSG_DEBUG, "GTK key index %d", id);
  602. wpa_hexdump(MSG_DEBUG, "GTK", decrypted,
  603. decrypted_len);
  604. bss->gtk_len[id] = decrypted_len;
  605. os_memcpy(bss->gtk[id], decrypted, decrypted_len);
  606. bss->rsc[id][0] = rsc[5];
  607. bss->rsc[id][1] = rsc[4];
  608. bss->rsc[id][2] = rsc[3];
  609. bss->rsc[id][3] = rsc[2];
  610. bss->rsc[id][4] = rsc[1];
  611. bss->rsc[id][5] = rsc[0];
  612. wpa_hexdump(MSG_DEBUG, "RSC", bss->rsc[id], 6);
  613. } else {
  614. wpa_printf(MSG_INFO, "Unexpected WPA Key Data length "
  615. "in Group Key msg 1/2 from " MACSTR,
  616. MAC2STR(src));
  617. }
  618. }
  619. os_free(decrypted);
  620. }
  621. static void rx_data_eapol_key_2_of_2(struct wlantest *wt, const u8 *dst,
  622. const u8 *src, const u8 *data, size_t len)
  623. {
  624. struct wlantest_bss *bss;
  625. struct wlantest_sta *sta;
  626. const struct ieee802_1x_hdr *eapol;
  627. const struct wpa_eapol_key *hdr;
  628. u16 key_info;
  629. wpa_printf(MSG_DEBUG, "EAPOL-Key 2/2 " MACSTR " -> " MACSTR,
  630. MAC2STR(src), MAC2STR(dst));
  631. bss = bss_get(wt, dst);
  632. if (bss == NULL)
  633. return;
  634. sta = sta_get(bss, src);
  635. if (sta == NULL)
  636. return;
  637. eapol = (const struct ieee802_1x_hdr *) data;
  638. hdr = (const struct wpa_eapol_key *) (eapol + 1);
  639. if (!is_zero(hdr->key_rsc, 8)) {
  640. wpa_printf(MSG_INFO, "EAPOL-Key 2/2 from " MACSTR " used "
  641. "non-zero Key RSC", MAC2STR(src));
  642. }
  643. key_info = WPA_GET_BE16(hdr->key_info);
  644. if (!sta->ptk_set) {
  645. wpa_printf(MSG_DEBUG, "No PTK known to process EAPOL-Key 2/2");
  646. return;
  647. }
  648. if (sta->ptk_set &&
  649. check_mic(sta->ptk.kck, key_info & WPA_KEY_INFO_TYPE_MASK,
  650. data, len) < 0) {
  651. wpa_printf(MSG_INFO, "Mismatch in EAPOL-Key 2/2 MIC");
  652. return;
  653. }
  654. wpa_printf(MSG_DEBUG, "Valid MIC found in EAPOL-Key 2/2");
  655. }
  656. static void rx_data_eapol_key(struct wlantest *wt, const u8 *dst,
  657. const u8 *src, const u8 *data, size_t len,
  658. int prot)
  659. {
  660. const struct ieee802_1x_hdr *eapol;
  661. const struct wpa_eapol_key *hdr;
  662. const u8 *key_data;
  663. u16 key_info, key_length, ver, key_data_length;
  664. eapol = (const struct ieee802_1x_hdr *) data;
  665. hdr = (const struct wpa_eapol_key *) (eapol + 1);
  666. wpa_hexdump(MSG_MSGDUMP, "EAPOL-Key",
  667. (const u8 *) hdr, len - sizeof(*eapol));
  668. if (len < sizeof(*hdr)) {
  669. wpa_printf(MSG_INFO, "Too short EAPOL-Key frame from " MACSTR,
  670. MAC2STR(src));
  671. return;
  672. }
  673. if (hdr->type == EAPOL_KEY_TYPE_RC4) {
  674. /* TODO: EAPOL-Key RC4 for WEP */
  675. wpa_printf(MSG_INFO, "EAPOL-Key Descriptor Type RC4 from "
  676. MACSTR, MAC2STR(src));
  677. return;
  678. }
  679. if (hdr->type != EAPOL_KEY_TYPE_RSN &&
  680. hdr->type != EAPOL_KEY_TYPE_WPA) {
  681. wpa_printf(MSG_INFO, "Unsupported EAPOL-Key Descriptor Type "
  682. "%u from " MACSTR, hdr->type, MAC2STR(src));
  683. return;
  684. }
  685. key_info = WPA_GET_BE16(hdr->key_info);
  686. key_length = WPA_GET_BE16(hdr->key_length);
  687. key_data_length = WPA_GET_BE16(hdr->key_data_length);
  688. key_data = (const u8 *) (hdr + 1);
  689. if (key_data + key_data_length > data + len) {
  690. wpa_printf(MSG_INFO, "Truncated EAPOL-Key from " MACSTR,
  691. MAC2STR(src));
  692. return;
  693. }
  694. if (key_data + key_data_length < data + len) {
  695. wpa_hexdump(MSG_DEBUG, "Extra data after EAPOL-Key Key Data "
  696. "field", key_data + key_data_length,
  697. data + len - key_data - key_data_length);
  698. }
  699. ver = key_info & WPA_KEY_INFO_TYPE_MASK;
  700. wpa_printf(MSG_DEBUG, "EAPOL-Key ver=%u %c idx=%u%s%s%s%s%s%s%s%s "
  701. "datalen=%u",
  702. ver, key_info & WPA_KEY_INFO_KEY_TYPE ? 'P' : 'G',
  703. (key_info & WPA_KEY_INFO_KEY_INDEX_MASK) >>
  704. WPA_KEY_INFO_KEY_INDEX_SHIFT,
  705. (key_info & WPA_KEY_INFO_INSTALL) ? " Install" : "",
  706. (key_info & WPA_KEY_INFO_ACK) ? " ACK" : "",
  707. (key_info & WPA_KEY_INFO_MIC) ? " MIC" : "",
  708. (key_info & WPA_KEY_INFO_SECURE) ? " Secure" : "",
  709. (key_info & WPA_KEY_INFO_ERROR) ? " Error" : "",
  710. (key_info & WPA_KEY_INFO_REQUEST) ? " Request" : "",
  711. (key_info & WPA_KEY_INFO_ENCR_KEY_DATA) ? " Encr" : "",
  712. (key_info & WPA_KEY_INFO_SMK_MESSAGE) ? " SMK" : "",
  713. key_data_length);
  714. if (ver != WPA_KEY_INFO_TYPE_HMAC_MD5_RC4 &&
  715. ver != WPA_KEY_INFO_TYPE_HMAC_SHA1_AES &&
  716. ver != WPA_KEY_INFO_TYPE_AES_128_CMAC) {
  717. wpa_printf(MSG_INFO, "Unsupported EAPOL-Key Key Descriptor "
  718. "Version %u from " MACSTR, ver, MAC2STR(src));
  719. return;
  720. }
  721. wpa_hexdump(MSG_MSGDUMP, "EAPOL-Key Replay Counter",
  722. hdr->replay_counter, WPA_REPLAY_COUNTER_LEN);
  723. wpa_hexdump(MSG_MSGDUMP, "EAPOL-Key Key Nonce",
  724. hdr->key_nonce, WPA_NONCE_LEN);
  725. wpa_hexdump(MSG_MSGDUMP, "EAPOL-Key Key IV",
  726. hdr->key_iv, 16);
  727. wpa_hexdump(MSG_MSGDUMP, "EAPOL-Key RSC",
  728. hdr->key_rsc, WPA_KEY_RSC_LEN);
  729. wpa_hexdump(MSG_MSGDUMP, "EAPOL-Key Key MIC",
  730. hdr->key_mic, 16);
  731. wpa_hexdump(MSG_MSGDUMP, "EAPOL-Key Key Data",
  732. key_data, key_data_length);
  733. if (hdr->type == EAPOL_KEY_TYPE_RSN &&
  734. (key_info & (WPA_KEY_INFO_KEY_INDEX_MASK | BIT(14) | BIT(15))) !=
  735. 0) {
  736. wpa_printf(MSG_INFO, "RSN EAPOL-Key with non-zero reserved "
  737. "Key Info bits 0x%x from " MACSTR,
  738. key_info, MAC2STR(src));
  739. }
  740. if (hdr->type == EAPOL_KEY_TYPE_WPA &&
  741. (key_info & (WPA_KEY_INFO_ENCR_KEY_DATA |
  742. WPA_KEY_INFO_SMK_MESSAGE |BIT(14) | BIT(15))) != 0) {
  743. wpa_printf(MSG_INFO, "WPA EAPOL-Key with non-zero reserved "
  744. "Key Info bits 0x%x from " MACSTR,
  745. key_info, MAC2STR(src));
  746. }
  747. if (key_length > 32) {
  748. wpa_printf(MSG_INFO, "EAPOL-Key with invalid Key Length %d "
  749. "from " MACSTR, key_length, MAC2STR(src));
  750. }
  751. if (ver != WPA_KEY_INFO_TYPE_HMAC_MD5_RC4 &&
  752. !is_zero(hdr->key_iv, 16)) {
  753. wpa_printf(MSG_INFO, "EAPOL-Key with non-zero Key IV "
  754. "(reserved with ver=%d) field from " MACSTR,
  755. ver, MAC2STR(src));
  756. wpa_hexdump(MSG_INFO, "EAPOL-Key Key IV (reserved)",
  757. hdr->key_iv, 16);
  758. }
  759. if (!is_zero(hdr->key_id, 8)) {
  760. wpa_printf(MSG_INFO, "EAPOL-Key with non-zero Key ID "
  761. "(reserved) field from " MACSTR, MAC2STR(src));
  762. wpa_hexdump(MSG_INFO, "EAPOL-Key Key ID (reserved)",
  763. hdr->key_id, 8);
  764. }
  765. if (hdr->key_rsc[6] || hdr->key_rsc[7]) {
  766. wpa_printf(MSG_INFO, "EAPOL-Key with non-zero Key RSC octets "
  767. "(last two are unused)" MACSTR, MAC2STR(src));
  768. }
  769. if (key_info & (WPA_KEY_INFO_ERROR | WPA_KEY_INFO_REQUEST))
  770. return;
  771. if (key_info & WPA_KEY_INFO_SMK_MESSAGE)
  772. return;
  773. if (key_info & WPA_KEY_INFO_KEY_TYPE) {
  774. /* 4-Way Handshake */
  775. switch (key_info & (WPA_KEY_INFO_SECURE |
  776. WPA_KEY_INFO_MIC |
  777. WPA_KEY_INFO_ACK |
  778. WPA_KEY_INFO_INSTALL)) {
  779. case WPA_KEY_INFO_ACK:
  780. rx_data_eapol_key_1_of_4(wt, dst, src, data, len);
  781. break;
  782. case WPA_KEY_INFO_MIC:
  783. if (key_data_length == 0)
  784. rx_data_eapol_key_4_of_4(wt, dst, src, data,
  785. len);
  786. else
  787. rx_data_eapol_key_2_of_4(wt, dst, src, data,
  788. len);
  789. break;
  790. case WPA_KEY_INFO_MIC | WPA_KEY_INFO_ACK |
  791. WPA_KEY_INFO_INSTALL:
  792. /* WPA does not include Secure bit in 3/4 */
  793. rx_data_eapol_key_3_of_4(wt, dst, src, data, len);
  794. break;
  795. case WPA_KEY_INFO_SECURE | WPA_KEY_INFO_MIC |
  796. WPA_KEY_INFO_ACK | WPA_KEY_INFO_INSTALL:
  797. rx_data_eapol_key_3_of_4(wt, dst, src, data, len);
  798. break;
  799. case WPA_KEY_INFO_SECURE | WPA_KEY_INFO_MIC:
  800. rx_data_eapol_key_4_of_4(wt, dst, src, data, len);
  801. break;
  802. default:
  803. wpa_printf(MSG_DEBUG, "Unsupported EAPOL-Key frame");
  804. break;
  805. }
  806. } else {
  807. /* Group Key Handshake */
  808. switch (key_info & (WPA_KEY_INFO_SECURE |
  809. WPA_KEY_INFO_MIC |
  810. WPA_KEY_INFO_ACK)) {
  811. case WPA_KEY_INFO_SECURE | WPA_KEY_INFO_MIC |
  812. WPA_KEY_INFO_ACK:
  813. rx_data_eapol_key_1_of_2(wt, dst, src, data, len);
  814. break;
  815. case WPA_KEY_INFO_SECURE | WPA_KEY_INFO_MIC:
  816. rx_data_eapol_key_2_of_2(wt, dst, src, data, len);
  817. break;
  818. default:
  819. wpa_printf(MSG_DEBUG, "Unsupported EAPOL-Key frame");
  820. break;
  821. }
  822. }
  823. }
  824. void rx_data_eapol(struct wlantest *wt, const u8 *dst, const u8 *src,
  825. const u8 *data, size_t len, int prot)
  826. {
  827. const struct ieee802_1x_hdr *hdr;
  828. u16 length;
  829. const u8 *p;
  830. wpa_hexdump(MSG_EXCESSIVE, "EAPOL", data, len);
  831. if (len < sizeof(*hdr)) {
  832. wpa_printf(MSG_INFO, "Too short EAPOL frame from " MACSTR,
  833. MAC2STR(src));
  834. return;
  835. }
  836. hdr = (const struct ieee802_1x_hdr *) data;
  837. length = be_to_host16(hdr->length);
  838. wpa_printf(MSG_DEBUG, "RX EAPOL: " MACSTR " -> " MACSTR "%s ver=%u "
  839. "type=%u len=%u",
  840. MAC2STR(src), MAC2STR(dst), prot ? " Prot" : "",
  841. hdr->version, hdr->type, length);
  842. if (hdr->version < 1 || hdr->version > 3) {
  843. wpa_printf(MSG_INFO, "Unexpected EAPOL version %u from "
  844. MACSTR, hdr->version, MAC2STR(src));
  845. }
  846. if (sizeof(*hdr) + length > len) {
  847. wpa_printf(MSG_INFO, "Truncated EAPOL frame from " MACSTR,
  848. MAC2STR(src));
  849. return;
  850. }
  851. if (sizeof(*hdr) + length < len) {
  852. wpa_printf(MSG_INFO, "EAPOL frame with %d extra bytes",
  853. (int) (len - sizeof(*hdr) - length));
  854. }
  855. p = (const u8 *) (hdr + 1);
  856. switch (hdr->type) {
  857. case IEEE802_1X_TYPE_EAP_PACKET:
  858. wpa_hexdump(MSG_MSGDUMP, "EAPOL - EAP packet", p, length);
  859. break;
  860. case IEEE802_1X_TYPE_EAPOL_START:
  861. wpa_hexdump(MSG_MSGDUMP, "EAPOL-Start", p, length);
  862. break;
  863. case IEEE802_1X_TYPE_EAPOL_LOGOFF:
  864. wpa_hexdump(MSG_MSGDUMP, "EAPOL-Logoff", p, length);
  865. break;
  866. case IEEE802_1X_TYPE_EAPOL_KEY:
  867. rx_data_eapol_key(wt, dst, src, data, sizeof(*hdr) + length,
  868. prot);
  869. break;
  870. case IEEE802_1X_TYPE_EAPOL_ENCAPSULATED_ASF_ALERT:
  871. wpa_hexdump(MSG_MSGDUMP, "EAPOL - Encapsulated ASF alert",
  872. p, length);
  873. break;
  874. default:
  875. wpa_hexdump(MSG_MSGDUMP, "Unknown EAPOL payload", p, length);
  876. break;
  877. }
  878. }