rx_eapol.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230
  1. /*
  2. * Received Data frame processing for EAPOL messages
  3. * Copyright (c) 2010-2015, Jouni Malinen <j@w1.fi>
  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 "crypto/aes_wrap.h"
  11. #include "crypto/crypto.h"
  12. #include "common/defs.h"
  13. #include "common/ieee802_11_defs.h"
  14. #include "common/ieee802_11_common.h"
  15. #include "common/eapol_common.h"
  16. #include "common/wpa_common.h"
  17. #include "rsn_supp/wpa_ie.h"
  18. #include "wlantest.h"
  19. static int is_zero(const u8 *buf, size_t len)
  20. {
  21. size_t i;
  22. for (i = 0; i < len; i++) {
  23. if (buf[i])
  24. return 0;
  25. }
  26. return 1;
  27. }
  28. static int check_mic(const u8 *kck, size_t kck_len, int akmp, int ver,
  29. const u8 *data, size_t len)
  30. {
  31. u8 *buf;
  32. int ret = -1;
  33. struct ieee802_1x_hdr *hdr;
  34. struct wpa_eapol_key *key;
  35. u8 rx_mic[WPA_EAPOL_KEY_MIC_MAX_LEN];
  36. size_t mic_len = wpa_mic_len(akmp, PMK_LEN);
  37. buf = os_memdup(data, len);
  38. if (buf == NULL)
  39. return -1;
  40. hdr = (struct ieee802_1x_hdr *) buf;
  41. key = (struct wpa_eapol_key *) (hdr + 1);
  42. os_memcpy(rx_mic, key + 1, mic_len);
  43. os_memset(key + 1, 0, mic_len);
  44. if (wpa_eapol_key_mic(kck, kck_len, akmp, ver, buf, len,
  45. (u8 *) (key + 1)) == 0 &&
  46. os_memcmp(rx_mic, key + 1, mic_len) == 0)
  47. ret = 0;
  48. os_free(buf);
  49. return ret;
  50. }
  51. static void rx_data_eapol_key_1_of_4(struct wlantest *wt, const u8 *dst,
  52. const u8 *src, const u8 *data, size_t len)
  53. {
  54. struct wlantest_bss *bss;
  55. struct wlantest_sta *sta;
  56. const struct ieee802_1x_hdr *eapol;
  57. const struct wpa_eapol_key *hdr;
  58. wpa_printf(MSG_DEBUG, "EAPOL-Key 1/4 " MACSTR " -> " MACSTR,
  59. MAC2STR(src), MAC2STR(dst));
  60. bss = bss_get(wt, src);
  61. if (bss == NULL)
  62. return;
  63. sta = sta_get(bss, dst);
  64. if (sta == NULL)
  65. return;
  66. eapol = (const struct ieee802_1x_hdr *) data;
  67. hdr = (const struct wpa_eapol_key *) (eapol + 1);
  68. if (is_zero(hdr->key_nonce, WPA_NONCE_LEN)) {
  69. add_note(wt, MSG_INFO, "EAPOL-Key 1/4 from " MACSTR
  70. " used zero nonce", MAC2STR(src));
  71. }
  72. if (!is_zero(hdr->key_rsc, 8)) {
  73. add_note(wt, MSG_INFO, "EAPOL-Key 1/4 from " MACSTR
  74. " used non-zero Key RSC", MAC2STR(src));
  75. }
  76. os_memcpy(sta->anonce, hdr->key_nonce, WPA_NONCE_LEN);
  77. }
  78. static int try_pmk(struct wlantest *wt, struct wlantest_bss *bss,
  79. struct wlantest_sta *sta, u16 ver,
  80. const u8 *data, size_t len,
  81. struct wlantest_pmk *pmk)
  82. {
  83. struct wpa_ptk ptk;
  84. if (wpa_key_mgmt_ft(sta->key_mgmt)) {
  85. u8 pmk_r0[PMK_LEN];
  86. u8 pmk_r0_name[WPA_PMK_NAME_LEN];
  87. u8 pmk_r1[PMK_LEN];
  88. u8 pmk_r1_name[WPA_PMK_NAME_LEN];
  89. u8 ptk_name[WPA_PMK_NAME_LEN];
  90. wpa_derive_pmk_r0(pmk->pmk, sizeof(pmk->pmk),
  91. bss->ssid, bss->ssid_len, bss->mdid,
  92. bss->r0kh_id, bss->r0kh_id_len,
  93. sta->addr, pmk_r0, pmk_r0_name);
  94. wpa_hexdump(MSG_DEBUG, "FT: PMK-R0", pmk_r0, PMK_LEN);
  95. wpa_hexdump(MSG_DEBUG, "FT: PMKR0Name", pmk_r0_name,
  96. WPA_PMK_NAME_LEN);
  97. wpa_derive_pmk_r1(pmk_r0, pmk_r0_name, bss->r1kh_id,
  98. sta->addr, pmk_r1, pmk_r1_name);
  99. wpa_hexdump_key(MSG_DEBUG, "FT: PMK-R1", pmk_r1, PMK_LEN);
  100. wpa_hexdump(MSG_DEBUG, "FT: PMKR1Name", pmk_r1_name,
  101. WPA_PMK_NAME_LEN);
  102. if (wpa_pmk_r1_to_ptk(pmk_r1, sta->snonce, sta->anonce,
  103. sta->addr,
  104. bss->bssid, pmk_r1_name, &ptk, ptk_name,
  105. sta->key_mgmt,
  106. sta->pairwise_cipher) < 0 ||
  107. check_mic(ptk.kck, ptk.kck_len, sta->key_mgmt, ver, data,
  108. len) < 0)
  109. return -1;
  110. } else if (wpa_pmk_to_ptk(pmk->pmk, sizeof(pmk->pmk),
  111. "Pairwise key expansion",
  112. bss->bssid, sta->addr, sta->anonce,
  113. sta->snonce, &ptk, sta->key_mgmt,
  114. sta->pairwise_cipher) < 0 ||
  115. check_mic(ptk.kck, ptk.kck_len, sta->key_mgmt, ver, data,
  116. len) < 0) {
  117. return -1;
  118. }
  119. sta->tk_len = wpa_cipher_key_len(sta->pairwise_cipher);
  120. wpa_printf(MSG_INFO, "Derived PTK for STA " MACSTR " BSSID " MACSTR,
  121. MAC2STR(sta->addr), MAC2STR(bss->bssid));
  122. sta->counters[WLANTEST_STA_COUNTER_PTK_LEARNED]++;
  123. if (sta->ptk_set) {
  124. /*
  125. * Rekeying - use new PTK for EAPOL-Key frames, but continue
  126. * using the old PTK for frame decryption.
  127. */
  128. add_note(wt, MSG_DEBUG, "Derived PTK during rekeying");
  129. os_memcpy(&sta->tptk, &ptk, sizeof(ptk));
  130. wpa_hexdump(MSG_DEBUG, "TPTK:KCK",
  131. sta->tptk.kck, sta->tptk.kck_len);
  132. wpa_hexdump(MSG_DEBUG, "TPTK:KEK",
  133. sta->tptk.kek, sta->tptk.kek_len);
  134. wpa_hexdump(MSG_DEBUG, "TPTK:TK",
  135. sta->tptk.tk, sta->tptk.tk_len);
  136. sta->tptk_set = 1;
  137. return 0;
  138. }
  139. add_note(wt, MSG_DEBUG, "Derived new PTK");
  140. os_memcpy(&sta->ptk, &ptk, sizeof(ptk));
  141. wpa_hexdump(MSG_DEBUG, "PTK:KCK", sta->ptk.kck, sta->ptk.kck_len);
  142. wpa_hexdump(MSG_DEBUG, "PTK:KEK", sta->ptk.kek, sta->ptk.kek_len);
  143. wpa_hexdump(MSG_DEBUG, "PTK:TK", sta->ptk.tk, sta->ptk.tk_len);
  144. sta->ptk_set = 1;
  145. os_memset(sta->rsc_tods, 0, sizeof(sta->rsc_tods));
  146. os_memset(sta->rsc_fromds, 0, sizeof(sta->rsc_fromds));
  147. return 0;
  148. }
  149. static void derive_ptk(struct wlantest *wt, struct wlantest_bss *bss,
  150. struct wlantest_sta *sta, u16 ver,
  151. const u8 *data, size_t len)
  152. {
  153. struct wlantest_pmk *pmk;
  154. wpa_printf(MSG_DEBUG, "Trying to derive PTK for " MACSTR " (ver %u)",
  155. MAC2STR(sta->addr), ver);
  156. dl_list_for_each(pmk, &bss->pmk, struct wlantest_pmk, list) {
  157. wpa_printf(MSG_DEBUG, "Try per-BSS PMK");
  158. if (try_pmk(wt, bss, sta, ver, data, len, pmk) == 0)
  159. return;
  160. }
  161. dl_list_for_each(pmk, &wt->pmk, struct wlantest_pmk, list) {
  162. wpa_printf(MSG_DEBUG, "Try global PMK");
  163. if (try_pmk(wt, bss, sta, ver, data, len, pmk) == 0)
  164. return;
  165. }
  166. if (!sta->ptk_set) {
  167. struct wlantest_ptk *ptk;
  168. int prev_level = wpa_debug_level;
  169. wpa_debug_level = MSG_WARNING;
  170. dl_list_for_each(ptk, &wt->ptk, struct wlantest_ptk, list) {
  171. if (check_mic(ptk->ptk.kck, ptk->ptk.kck_len,
  172. sta->key_mgmt, ver, data, len) < 0)
  173. continue;
  174. wpa_printf(MSG_INFO, "Pre-set PTK matches for STA "
  175. MACSTR " BSSID " MACSTR,
  176. MAC2STR(sta->addr), MAC2STR(bss->bssid));
  177. add_note(wt, MSG_DEBUG, "Using pre-set PTK");
  178. ptk->ptk_len = 32 +
  179. wpa_cipher_key_len(sta->pairwise_cipher);
  180. os_memcpy(&sta->ptk, &ptk->ptk, sizeof(ptk->ptk));
  181. wpa_hexdump(MSG_DEBUG, "PTK:KCK",
  182. sta->ptk.kck, sta->ptk.kck_len);
  183. wpa_hexdump(MSG_DEBUG, "PTK:KEK",
  184. sta->ptk.kek, sta->ptk.kek_len);
  185. wpa_hexdump(MSG_DEBUG, "PTK:TK",
  186. sta->ptk.tk, sta->ptk.tk_len);
  187. sta->ptk_set = 1;
  188. os_memset(sta->rsc_tods, 0, sizeof(sta->rsc_tods));
  189. os_memset(sta->rsc_fromds, 0, sizeof(sta->rsc_fromds));
  190. }
  191. wpa_debug_level = prev_level;
  192. }
  193. add_note(wt, MSG_DEBUG, "No matching PMK found to derive PTK");
  194. }
  195. static void rx_data_eapol_key_2_of_4(struct wlantest *wt, const u8 *dst,
  196. const u8 *src, const u8 *data, size_t len)
  197. {
  198. struct wlantest_bss *bss;
  199. struct wlantest_sta *sta;
  200. const struct ieee802_1x_hdr *eapol;
  201. const struct wpa_eapol_key *hdr;
  202. const u8 *key_data, *kck, *mic;
  203. size_t kck_len, mic_len;
  204. u16 key_info, key_data_len;
  205. struct wpa_eapol_ie_parse ie;
  206. wpa_printf(MSG_DEBUG, "EAPOL-Key 2/4 " MACSTR " -> " MACSTR,
  207. MAC2STR(src), MAC2STR(dst));
  208. bss = bss_get(wt, dst);
  209. if (bss == NULL)
  210. return;
  211. sta = sta_get(bss, src);
  212. if (sta == NULL)
  213. return;
  214. eapol = (const struct ieee802_1x_hdr *) data;
  215. hdr = (const struct wpa_eapol_key *) (eapol + 1);
  216. mic_len = wpa_mic_len(sta->key_mgmt, PMK_LEN);
  217. mic = (const u8 *) (hdr + 1);
  218. if (is_zero(hdr->key_nonce, WPA_NONCE_LEN)) {
  219. add_note(wt, MSG_INFO, "EAPOL-Key 2/4 from " MACSTR
  220. " used zero nonce", MAC2STR(src));
  221. }
  222. if (!is_zero(hdr->key_rsc, 8)) {
  223. add_note(wt, MSG_INFO, "EAPOL-Key 2/4 from " MACSTR
  224. " used non-zero Key RSC", MAC2STR(src));
  225. }
  226. os_memcpy(sta->snonce, hdr->key_nonce, WPA_NONCE_LEN);
  227. key_info = WPA_GET_BE16(hdr->key_info);
  228. key_data_len = WPA_GET_BE16(mic + mic_len);
  229. derive_ptk(wt, bss, sta, key_info & WPA_KEY_INFO_TYPE_MASK, data, len);
  230. if (!sta->ptk_set && !sta->tptk_set) {
  231. add_note(wt, MSG_DEBUG,
  232. "No PTK known to process EAPOL-Key 2/4");
  233. return;
  234. }
  235. kck = sta->ptk.kck;
  236. kck_len = sta->ptk.kck_len;
  237. if (sta->tptk_set) {
  238. add_note(wt, MSG_DEBUG,
  239. "Use TPTK for validation EAPOL-Key MIC");
  240. kck = sta->tptk.kck;
  241. kck_len = sta->tptk.kck_len;
  242. }
  243. if (check_mic(kck, kck_len, sta->key_mgmt,
  244. key_info & WPA_KEY_INFO_TYPE_MASK, data, len) < 0) {
  245. add_note(wt, MSG_INFO, "Mismatch in EAPOL-Key 2/4 MIC");
  246. return;
  247. }
  248. add_note(wt, MSG_DEBUG, "Valid MIC found in EAPOL-Key 2/4");
  249. key_data = mic + mic_len + 2;
  250. if (wpa_supplicant_parse_ies(key_data, key_data_len, &ie) < 0) {
  251. add_note(wt, MSG_INFO, "Failed to parse EAPOL-Key Key Data");
  252. return;
  253. }
  254. if (ie.wpa_ie) {
  255. wpa_hexdump(MSG_MSGDUMP, "EAPOL-Key Key Data - WPA IE",
  256. ie.wpa_ie, ie.wpa_ie_len);
  257. if (os_memcmp(ie.wpa_ie, sta->rsnie, ie.wpa_ie_len) != 0) {
  258. struct ieee802_11_elems elems;
  259. add_note(wt, MSG_INFO,
  260. "Mismatch in WPA IE between EAPOL-Key 2/4 "
  261. "and (Re)Association Request from " MACSTR,
  262. MAC2STR(sta->addr));
  263. wpa_hexdump(MSG_INFO, "WPA IE in EAPOL-Key",
  264. ie.wpa_ie, ie.wpa_ie_len);
  265. wpa_hexdump(MSG_INFO, "WPA IE in (Re)Association "
  266. "Request",
  267. sta->rsnie,
  268. sta->rsnie[0] ? 2 + sta->rsnie[1] : 0);
  269. /*
  270. * The sniffer may have missed (Re)Association
  271. * Request, so try to survive with the information from
  272. * EAPOL-Key.
  273. */
  274. os_memset(&elems, 0, sizeof(elems));
  275. elems.wpa_ie = ie.wpa_ie + 2;
  276. elems.wpa_ie_len = ie.wpa_ie_len - 2;
  277. wpa_printf(MSG_DEBUG, "Update STA data based on WPA "
  278. "IE in EAPOL-Key 2/4");
  279. sta_update_assoc(sta, &elems);
  280. }
  281. }
  282. if (ie.rsn_ie) {
  283. wpa_hexdump(MSG_MSGDUMP, "EAPOL-Key Key Data - RSN IE",
  284. ie.rsn_ie, ie.rsn_ie_len);
  285. if (os_memcmp(ie.rsn_ie, sta->rsnie, ie.rsn_ie_len) != 0) {
  286. struct ieee802_11_elems elems;
  287. add_note(wt, MSG_INFO,
  288. "Mismatch in RSN IE between EAPOL-Key 2/4 "
  289. "and (Re)Association Request from " MACSTR,
  290. MAC2STR(sta->addr));
  291. wpa_hexdump(MSG_INFO, "RSN IE in EAPOL-Key",
  292. ie.rsn_ie, ie.rsn_ie_len);
  293. wpa_hexdump(MSG_INFO, "RSN IE in (Re)Association "
  294. "Request",
  295. sta->rsnie,
  296. sta->rsnie[0] ? 2 + sta->rsnie[1] : 0);
  297. /*
  298. * The sniffer may have missed (Re)Association
  299. * Request, so try to survive with the information from
  300. * EAPOL-Key.
  301. */
  302. os_memset(&elems, 0, sizeof(elems));
  303. elems.rsn_ie = ie.rsn_ie + 2;
  304. elems.rsn_ie_len = ie.rsn_ie_len - 2;
  305. wpa_printf(MSG_DEBUG, "Update STA data based on RSN "
  306. "IE in EAPOL-Key 2/4");
  307. sta_update_assoc(sta, &elems);
  308. }
  309. }
  310. }
  311. static u8 * decrypt_eapol_key_data_rc4(struct wlantest *wt, const u8 *kek,
  312. const struct wpa_eapol_key *hdr,
  313. const u8 *keydata, u16 keydatalen,
  314. size_t *len)
  315. {
  316. u8 ek[32], *buf;
  317. buf = os_memdup(keydata, keydatalen);
  318. if (buf == NULL)
  319. return NULL;
  320. os_memcpy(ek, hdr->key_iv, 16);
  321. os_memcpy(ek + 16, kek, 16);
  322. if (rc4_skip(ek, 32, 256, buf, keydatalen)) {
  323. add_note(wt, MSG_INFO, "RC4 failed");
  324. os_free(buf);
  325. return NULL;
  326. }
  327. *len = keydatalen;
  328. return buf;
  329. }
  330. static u8 * decrypt_eapol_key_data_aes(struct wlantest *wt, const u8 *kek,
  331. const struct wpa_eapol_key *hdr,
  332. const u8 *keydata, u16 keydatalen,
  333. size_t *len)
  334. {
  335. u8 *buf;
  336. if (keydatalen % 8) {
  337. add_note(wt, MSG_INFO, "Unsupported AES-WRAP len %d",
  338. keydatalen);
  339. return NULL;
  340. }
  341. keydatalen -= 8; /* AES-WRAP adds 8 bytes */
  342. buf = os_malloc(keydatalen);
  343. if (buf == NULL)
  344. return NULL;
  345. if (aes_unwrap(kek, 16, keydatalen / 8, keydata, buf)) {
  346. os_free(buf);
  347. add_note(wt, MSG_INFO,
  348. "AES unwrap failed - could not decrypt EAPOL-Key "
  349. "key data");
  350. return NULL;
  351. }
  352. *len = keydatalen;
  353. return buf;
  354. }
  355. static u8 * decrypt_eapol_key_data(struct wlantest *wt, int akmp, const u8 *kek,
  356. size_t kek_len, u16 ver,
  357. const struct wpa_eapol_key *hdr,
  358. size_t *len)
  359. {
  360. size_t mic_len;
  361. u16 keydatalen;
  362. const u8 *mic, *keydata;
  363. if (kek_len != 16)
  364. return NULL;
  365. mic = (const u8 *) (hdr + 1);
  366. mic_len = wpa_mic_len(akmp, PMK_LEN);
  367. keydata = mic + mic_len + 2;
  368. keydatalen = WPA_GET_BE16(mic + mic_len);
  369. switch (ver) {
  370. case WPA_KEY_INFO_TYPE_HMAC_MD5_RC4:
  371. return decrypt_eapol_key_data_rc4(wt, kek, hdr, keydata,
  372. keydatalen, len);
  373. case WPA_KEY_INFO_TYPE_HMAC_SHA1_AES:
  374. case WPA_KEY_INFO_TYPE_AES_128_CMAC:
  375. return decrypt_eapol_key_data_aes(wt, kek, hdr, keydata,
  376. keydatalen, len);
  377. case WPA_KEY_INFO_TYPE_AKM_DEFINED:
  378. /* For now, assume this is OSEN */
  379. return decrypt_eapol_key_data_aes(wt, kek, hdr, keydata,
  380. keydatalen, len);
  381. default:
  382. add_note(wt, MSG_INFO,
  383. "Unsupported EAPOL-Key Key Descriptor Version %u",
  384. ver);
  385. return NULL;
  386. }
  387. }
  388. static void learn_kde_keys(struct wlantest *wt, struct wlantest_bss *bss,
  389. struct wlantest_sta *sta,
  390. const u8 *buf, size_t len, const u8 *rsc)
  391. {
  392. struct wpa_eapol_ie_parse ie;
  393. if (wpa_supplicant_parse_ies(buf, len, &ie) < 0) {
  394. add_note(wt, MSG_INFO, "Failed to parse EAPOL-Key Key Data");
  395. return;
  396. }
  397. if (ie.wpa_ie) {
  398. wpa_hexdump(MSG_MSGDUMP, "EAPOL-Key Key Data - WPA IE",
  399. ie.wpa_ie, ie.wpa_ie_len);
  400. }
  401. if (ie.rsn_ie) {
  402. wpa_hexdump(MSG_MSGDUMP, "EAPOL-Key Key Data - RSN IE",
  403. ie.rsn_ie, ie.rsn_ie_len);
  404. }
  405. if (ie.gtk) {
  406. wpa_hexdump(MSG_MSGDUMP, "EAPOL-Key Key Data - GTK KDE",
  407. ie.gtk, ie.gtk_len);
  408. if (ie.gtk_len >= 2 && ie.gtk_len <= 2 + 32) {
  409. int id;
  410. id = ie.gtk[0] & 0x03;
  411. add_note(wt, MSG_DEBUG, "GTK KeyID=%u tx=%u",
  412. id, !!(ie.gtk[0] & 0x04));
  413. if ((ie.gtk[0] & 0xf8) || ie.gtk[1]) {
  414. add_note(wt, MSG_INFO,
  415. "GTK KDE: Reserved field set: "
  416. "%02x %02x", ie.gtk[0], ie.gtk[1]);
  417. }
  418. wpa_hexdump(MSG_DEBUG, "GTK", ie.gtk + 2,
  419. ie.gtk_len - 2);
  420. bss->gtk_len[id] = ie.gtk_len - 2;
  421. sta->gtk_len = ie.gtk_len - 2;
  422. os_memcpy(bss->gtk[id], ie.gtk + 2, ie.gtk_len - 2);
  423. os_memcpy(sta->gtk, ie.gtk + 2, ie.gtk_len - 2);
  424. bss->rsc[id][0] = rsc[5];
  425. bss->rsc[id][1] = rsc[4];
  426. bss->rsc[id][2] = rsc[3];
  427. bss->rsc[id][3] = rsc[2];
  428. bss->rsc[id][4] = rsc[1];
  429. bss->rsc[id][5] = rsc[0];
  430. bss->gtk_idx = id;
  431. sta->gtk_idx = id;
  432. wpa_hexdump(MSG_DEBUG, "RSC", bss->rsc[id], 6);
  433. } else {
  434. add_note(wt, MSG_INFO, "Invalid GTK KDE length %u",
  435. (unsigned) ie.gtk_len);
  436. }
  437. }
  438. if (ie.igtk) {
  439. wpa_hexdump(MSG_MSGDUMP, "EAPOL-Key Key Data - IGTK KDE",
  440. ie.igtk, ie.igtk_len);
  441. if (ie.igtk_len == 24) {
  442. u16 id;
  443. id = WPA_GET_LE16(ie.igtk);
  444. if (id > 5) {
  445. add_note(wt, MSG_INFO,
  446. "Unexpected IGTK KeyID %u", id);
  447. } else {
  448. const u8 *ipn;
  449. add_note(wt, MSG_DEBUG, "IGTK KeyID %u", id);
  450. wpa_hexdump(MSG_DEBUG, "IPN", ie.igtk + 2, 6);
  451. wpa_hexdump(MSG_DEBUG, "IGTK", ie.igtk + 8,
  452. 16);
  453. os_memcpy(bss->igtk[id], ie.igtk + 8, 16);
  454. bss->igtk_len[id] = 16;
  455. ipn = ie.igtk + 2;
  456. bss->ipn[id][0] = ipn[5];
  457. bss->ipn[id][1] = ipn[4];
  458. bss->ipn[id][2] = ipn[3];
  459. bss->ipn[id][3] = ipn[2];
  460. bss->ipn[id][4] = ipn[1];
  461. bss->ipn[id][5] = ipn[0];
  462. bss->igtk_idx = id;
  463. }
  464. } else if (ie.igtk_len == 40) {
  465. u16 id;
  466. id = WPA_GET_LE16(ie.igtk);
  467. if (id > 5) {
  468. add_note(wt, MSG_INFO,
  469. "Unexpected IGTK KeyID %u", id);
  470. } else {
  471. const u8 *ipn;
  472. add_note(wt, MSG_DEBUG, "IGTK KeyID %u", id);
  473. wpa_hexdump(MSG_DEBUG, "IPN", ie.igtk + 2, 6);
  474. wpa_hexdump(MSG_DEBUG, "IGTK", ie.igtk + 8,
  475. 32);
  476. os_memcpy(bss->igtk[id], ie.igtk + 8, 32);
  477. bss->igtk_len[id] = 32;
  478. ipn = ie.igtk + 2;
  479. bss->ipn[id][0] = ipn[5];
  480. bss->ipn[id][1] = ipn[4];
  481. bss->ipn[id][2] = ipn[3];
  482. bss->ipn[id][3] = ipn[2];
  483. bss->ipn[id][4] = ipn[1];
  484. bss->ipn[id][5] = ipn[0];
  485. bss->igtk_idx = id;
  486. }
  487. } else {
  488. add_note(wt, MSG_INFO, "Invalid IGTK KDE length %u",
  489. (unsigned) ie.igtk_len);
  490. }
  491. }
  492. }
  493. static void rx_data_eapol_key_3_of_4(struct wlantest *wt, const u8 *dst,
  494. const u8 *src, const u8 *data, size_t len)
  495. {
  496. struct wlantest_bss *bss;
  497. struct wlantest_sta *sta;
  498. const struct ieee802_1x_hdr *eapol;
  499. const struct wpa_eapol_key *hdr;
  500. const u8 *key_data, *kck, *kek, *mic;
  501. size_t kck_len, kek_len, mic_len;
  502. int recalc = 0;
  503. u16 key_info, ver;
  504. u8 *decrypted_buf = NULL;
  505. const u8 *decrypted;
  506. size_t decrypted_len = 0;
  507. struct wpa_eapol_ie_parse ie;
  508. wpa_printf(MSG_DEBUG, "EAPOL-Key 3/4 " MACSTR " -> " MACSTR,
  509. MAC2STR(src), MAC2STR(dst));
  510. bss = bss_get(wt, src);
  511. if (bss == NULL)
  512. return;
  513. sta = sta_get(bss, dst);
  514. if (sta == NULL)
  515. return;
  516. mic_len = wpa_mic_len(sta->key_mgmt, PMK_LEN);
  517. eapol = (const struct ieee802_1x_hdr *) data;
  518. hdr = (const struct wpa_eapol_key *) (eapol + 1);
  519. mic = (const u8 *) (hdr + 1);
  520. key_info = WPA_GET_BE16(hdr->key_info);
  521. if (os_memcmp(sta->anonce, hdr->key_nonce, WPA_NONCE_LEN) != 0) {
  522. add_note(wt, MSG_INFO,
  523. "EAPOL-Key ANonce mismatch between 1/4 and 3/4");
  524. recalc = 1;
  525. }
  526. os_memcpy(sta->anonce, hdr->key_nonce, WPA_NONCE_LEN);
  527. if (recalc) {
  528. derive_ptk(wt, bss, sta, key_info & WPA_KEY_INFO_TYPE_MASK,
  529. data, len);
  530. }
  531. if (!sta->ptk_set && !sta->tptk_set) {
  532. add_note(wt, MSG_DEBUG,
  533. "No PTK known to process EAPOL-Key 3/4");
  534. return;
  535. }
  536. kek = sta->ptk.kek;
  537. kek_len = sta->ptk.kek_len;
  538. kck = sta->ptk.kck;
  539. kck_len = sta->ptk.kck_len;
  540. if (sta->tptk_set) {
  541. add_note(wt, MSG_DEBUG,
  542. "Use TPTK for validation EAPOL-Key MIC");
  543. kck = sta->tptk.kck;
  544. kck_len = sta->tptk.kck_len;
  545. kek = sta->tptk.kek;
  546. kek_len = sta->tptk.kek_len;
  547. }
  548. if (check_mic(kck, kck_len, sta->key_mgmt,
  549. key_info & WPA_KEY_INFO_TYPE_MASK, data, len) < 0) {
  550. add_note(wt, MSG_INFO, "Mismatch in EAPOL-Key 3/4 MIC");
  551. return;
  552. }
  553. add_note(wt, MSG_DEBUG, "Valid MIC found in EAPOL-Key 3/4");
  554. key_data = mic + mic_len + 2;
  555. if (!(key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
  556. if (sta->proto & WPA_PROTO_RSN)
  557. add_note(wt, MSG_INFO,
  558. "EAPOL-Key 3/4 without EncrKeyData bit");
  559. decrypted = key_data;
  560. decrypted_len = WPA_GET_BE16(mic + mic_len);
  561. } else {
  562. ver = key_info & WPA_KEY_INFO_TYPE_MASK;
  563. decrypted_buf = decrypt_eapol_key_data(wt, sta->key_mgmt,
  564. kek, kek_len, ver,
  565. hdr, &decrypted_len);
  566. if (decrypted_buf == NULL) {
  567. add_note(wt, MSG_INFO,
  568. "Failed to decrypt EAPOL-Key Key Data");
  569. return;
  570. }
  571. decrypted = decrypted_buf;
  572. wpa_hexdump(MSG_DEBUG, "Decrypted EAPOL-Key Key Data",
  573. decrypted, decrypted_len);
  574. }
  575. if ((wt->write_pcap_dumper || wt->pcapng) && decrypted != key_data) {
  576. /* Fill in a dummy Data frame header */
  577. u8 buf[24 + 8 + sizeof(*eapol) + sizeof(*hdr) + 64];
  578. struct ieee80211_hdr *h;
  579. struct wpa_eapol_key *k;
  580. const u8 *p;
  581. u8 *pos;
  582. size_t plain_len;
  583. plain_len = decrypted_len;
  584. p = decrypted;
  585. while (p + 1 < decrypted + decrypted_len) {
  586. if (p[0] == 0xdd && p[1] == 0x00) {
  587. /* Remove padding */
  588. plain_len = p - decrypted;
  589. p = NULL;
  590. break;
  591. }
  592. p += 2 + p[1];
  593. }
  594. if (p && p > decrypted && *p == 0xdd &&
  595. p + 1 == decrypted + decrypted_len) {
  596. /* Remove padding */
  597. p--;
  598. plain_len = p - decrypted;
  599. }
  600. os_memset(buf, 0, sizeof(buf));
  601. h = (struct ieee80211_hdr *) buf;
  602. h->frame_control = host_to_le16(0x0208);
  603. os_memcpy(h->addr1, dst, ETH_ALEN);
  604. os_memcpy(h->addr2, src, ETH_ALEN);
  605. os_memcpy(h->addr3, src, ETH_ALEN);
  606. pos = (u8 *) (h + 1);
  607. os_memcpy(pos, "\xaa\xaa\x03\x00\x00\x00\x88\x8e", 8);
  608. pos += 8;
  609. os_memcpy(pos, eapol, sizeof(*eapol));
  610. pos += sizeof(*eapol);
  611. os_memcpy(pos, hdr, sizeof(*hdr) + mic_len);
  612. k = (struct wpa_eapol_key *) pos;
  613. pos += sizeof(struct wpa_eapol_key) + mic_len;
  614. WPA_PUT_BE16(k->key_info,
  615. key_info & ~WPA_KEY_INFO_ENCR_KEY_DATA);
  616. WPA_PUT_BE16(pos, plain_len);
  617. write_pcap_decrypted(wt, buf, 24 + 8 + sizeof(*eapol) +
  618. sizeof(*hdr) + mic_len + 2,
  619. decrypted, plain_len);
  620. }
  621. if (wpa_supplicant_parse_ies(decrypted, decrypted_len, &ie) < 0) {
  622. add_note(wt, MSG_INFO, "Failed to parse EAPOL-Key Key Data");
  623. os_free(decrypted_buf);
  624. return;
  625. }
  626. if ((ie.wpa_ie &&
  627. os_memcmp(ie.wpa_ie, bss->wpaie, ie.wpa_ie_len) != 0) ||
  628. (ie.wpa_ie == NULL && bss->wpaie[0])) {
  629. add_note(wt, MSG_INFO,
  630. "Mismatch in WPA IE between EAPOL-Key 3/4 and "
  631. "Beacon/Probe Response from " MACSTR,
  632. MAC2STR(bss->bssid));
  633. wpa_hexdump(MSG_INFO, "WPA IE in EAPOL-Key",
  634. ie.wpa_ie, ie.wpa_ie_len);
  635. wpa_hexdump(MSG_INFO, "WPA IE in Beacon/Probe "
  636. "Response",
  637. bss->wpaie,
  638. bss->wpaie[0] ? 2 + bss->wpaie[1] : 0);
  639. }
  640. if ((ie.rsn_ie &&
  641. os_memcmp(ie.rsn_ie, bss->rsnie, ie.rsn_ie_len) != 0) ||
  642. (ie.rsn_ie == NULL && bss->rsnie[0])) {
  643. add_note(wt, MSG_INFO, "Mismatch in RSN IE between EAPOL-Key "
  644. "3/4 and Beacon/Probe Response from " MACSTR,
  645. MAC2STR(bss->bssid));
  646. wpa_hexdump(MSG_INFO, "RSN IE in EAPOL-Key",
  647. ie.rsn_ie, ie.rsn_ie_len);
  648. wpa_hexdump(MSG_INFO, "RSN IE in Beacon/Probe Response",
  649. bss->rsnie,
  650. bss->rsnie[0] ? 2 + bss->rsnie[1] : 0);
  651. }
  652. learn_kde_keys(wt, bss, sta, decrypted, decrypted_len, hdr->key_rsc);
  653. os_free(decrypted_buf);
  654. }
  655. static void rx_data_eapol_key_4_of_4(struct wlantest *wt, const u8 *dst,
  656. const u8 *src, const u8 *data, size_t len)
  657. {
  658. struct wlantest_bss *bss;
  659. struct wlantest_sta *sta;
  660. const struct ieee802_1x_hdr *eapol;
  661. const struct wpa_eapol_key *hdr;
  662. u16 key_info;
  663. const u8 *kck;
  664. size_t kck_len;
  665. wpa_printf(MSG_DEBUG, "EAPOL-Key 4/4 " MACSTR " -> " MACSTR,
  666. MAC2STR(src), MAC2STR(dst));
  667. bss = bss_get(wt, dst);
  668. if (bss == NULL)
  669. return;
  670. sta = sta_get(bss, src);
  671. if (sta == NULL)
  672. return;
  673. eapol = (const struct ieee802_1x_hdr *) data;
  674. hdr = (const struct wpa_eapol_key *) (eapol + 1);
  675. if (!is_zero(hdr->key_rsc, 8)) {
  676. add_note(wt, MSG_INFO, "EAPOL-Key 4/4 from " MACSTR " used "
  677. "non-zero Key RSC", MAC2STR(src));
  678. }
  679. key_info = WPA_GET_BE16(hdr->key_info);
  680. if (!sta->ptk_set && !sta->tptk_set) {
  681. add_note(wt, MSG_DEBUG,
  682. "No PTK known to process EAPOL-Key 4/4");
  683. return;
  684. }
  685. kck = sta->ptk.kck;
  686. kck_len = sta->ptk.kck_len;
  687. if (sta->tptk_set) {
  688. add_note(wt, MSG_DEBUG,
  689. "Use TPTK for validation EAPOL-Key MIC");
  690. kck = sta->tptk.kck;
  691. kck_len = sta->tptk.kck_len;
  692. }
  693. if (check_mic(kck, kck_len, sta->key_mgmt,
  694. key_info & WPA_KEY_INFO_TYPE_MASK, data, len) < 0) {
  695. add_note(wt, MSG_INFO, "Mismatch in EAPOL-Key 4/4 MIC");
  696. return;
  697. }
  698. add_note(wt, MSG_DEBUG, "Valid MIC found in EAPOL-Key 4/4");
  699. if (sta->tptk_set) {
  700. add_note(wt, MSG_DEBUG, "Update PTK (rekeying)");
  701. os_memcpy(&sta->ptk, &sta->tptk, sizeof(sta->ptk));
  702. sta->ptk_set = 1;
  703. sta->tptk_set = 0;
  704. os_memset(sta->rsc_tods, 0, sizeof(sta->rsc_tods));
  705. os_memset(sta->rsc_fromds, 0, sizeof(sta->rsc_fromds));
  706. }
  707. }
  708. static void rx_data_eapol_key_1_of_2(struct wlantest *wt, const u8 *dst,
  709. const u8 *src, const u8 *data, size_t len)
  710. {
  711. struct wlantest_bss *bss;
  712. struct wlantest_sta *sta;
  713. const struct ieee802_1x_hdr *eapol;
  714. const struct wpa_eapol_key *hdr;
  715. u16 key_info, ver;
  716. u8 *decrypted;
  717. size_t decrypted_len = 0;
  718. size_t mic_len;
  719. wpa_printf(MSG_DEBUG, "EAPOL-Key 1/2 " MACSTR " -> " MACSTR,
  720. MAC2STR(src), MAC2STR(dst));
  721. bss = bss_get(wt, src);
  722. if (bss == NULL)
  723. return;
  724. sta = sta_get(bss, dst);
  725. if (sta == NULL)
  726. return;
  727. mic_len = wpa_mic_len(sta->key_mgmt, PMK_LEN);
  728. eapol = (const struct ieee802_1x_hdr *) data;
  729. hdr = (const struct wpa_eapol_key *) (eapol + 1);
  730. key_info = WPA_GET_BE16(hdr->key_info);
  731. if (!sta->ptk_set) {
  732. add_note(wt, MSG_DEBUG,
  733. "No PTK known to process EAPOL-Key 1/2");
  734. return;
  735. }
  736. if (sta->ptk_set &&
  737. check_mic(sta->ptk.kck, sta->ptk.kck_len, sta->key_mgmt,
  738. key_info & WPA_KEY_INFO_TYPE_MASK,
  739. data, len) < 0) {
  740. add_note(wt, MSG_INFO, "Mismatch in EAPOL-Key 1/2 MIC");
  741. return;
  742. }
  743. add_note(wt, MSG_DEBUG, "Valid MIC found in EAPOL-Key 1/2");
  744. if (sta->proto & WPA_PROTO_RSN &&
  745. !(key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
  746. add_note(wt, MSG_INFO, "EAPOL-Key 1/2 without EncrKeyData bit");
  747. return;
  748. }
  749. ver = key_info & WPA_KEY_INFO_TYPE_MASK;
  750. decrypted = decrypt_eapol_key_data(wt, sta->key_mgmt,
  751. sta->ptk.kek, sta->ptk.kek_len,
  752. ver, hdr, &decrypted_len);
  753. if (decrypted == NULL) {
  754. add_note(wt, MSG_INFO, "Failed to decrypt EAPOL-Key Key Data");
  755. return;
  756. }
  757. wpa_hexdump(MSG_DEBUG, "Decrypted EAPOL-Key Key Data",
  758. decrypted, decrypted_len);
  759. if (wt->write_pcap_dumper || wt->pcapng) {
  760. /* Fill in a dummy Data frame header */
  761. u8 buf[24 + 8 + sizeof(*eapol) + sizeof(*hdr) + 64];
  762. struct ieee80211_hdr *h;
  763. struct wpa_eapol_key *k;
  764. u8 *pos;
  765. size_t plain_len;
  766. plain_len = decrypted_len;
  767. pos = decrypted;
  768. while (pos + 1 < decrypted + decrypted_len) {
  769. if (pos[0] == 0xdd && pos[1] == 0x00) {
  770. /* Remove padding */
  771. plain_len = pos - decrypted;
  772. break;
  773. }
  774. pos += 2 + pos[1];
  775. }
  776. os_memset(buf, 0, sizeof(buf));
  777. h = (struct ieee80211_hdr *) buf;
  778. h->frame_control = host_to_le16(0x0208);
  779. os_memcpy(h->addr1, dst, ETH_ALEN);
  780. os_memcpy(h->addr2, src, ETH_ALEN);
  781. os_memcpy(h->addr3, src, ETH_ALEN);
  782. pos = (u8 *) (h + 1);
  783. os_memcpy(pos, "\xaa\xaa\x03\x00\x00\x00\x88\x8e", 8);
  784. pos += 8;
  785. os_memcpy(pos, eapol, sizeof(*eapol));
  786. pos += sizeof(*eapol);
  787. os_memcpy(pos, hdr, sizeof(*hdr) + mic_len);
  788. k = (struct wpa_eapol_key *) pos;
  789. pos += sizeof(struct wpa_eapol_key) + mic_len;
  790. WPA_PUT_BE16(k->key_info,
  791. key_info & ~WPA_KEY_INFO_ENCR_KEY_DATA);
  792. WPA_PUT_BE16(pos, plain_len);
  793. write_pcap_decrypted(wt, buf, 24 + 8 + sizeof(*eapol) +
  794. sizeof(*hdr) + mic_len + 2,
  795. decrypted, plain_len);
  796. }
  797. if (sta->proto & WPA_PROTO_RSN)
  798. learn_kde_keys(wt, bss, sta, decrypted, decrypted_len,
  799. hdr->key_rsc);
  800. else {
  801. int klen = bss->group_cipher == WPA_CIPHER_TKIP ? 32 : 16;
  802. if (decrypted_len == klen) {
  803. const u8 *rsc = hdr->key_rsc;
  804. int id;
  805. id = (key_info & WPA_KEY_INFO_KEY_INDEX_MASK) >>
  806. WPA_KEY_INFO_KEY_INDEX_SHIFT;
  807. add_note(wt, MSG_DEBUG, "GTK key index %d", id);
  808. wpa_hexdump(MSG_DEBUG, "GTK", decrypted,
  809. decrypted_len);
  810. bss->gtk_len[id] = decrypted_len;
  811. os_memcpy(bss->gtk[id], decrypted, decrypted_len);
  812. bss->rsc[id][0] = rsc[5];
  813. bss->rsc[id][1] = rsc[4];
  814. bss->rsc[id][2] = rsc[3];
  815. bss->rsc[id][3] = rsc[2];
  816. bss->rsc[id][4] = rsc[1];
  817. bss->rsc[id][5] = rsc[0];
  818. wpa_hexdump(MSG_DEBUG, "RSC", bss->rsc[id], 6);
  819. } else {
  820. add_note(wt, MSG_INFO, "Unexpected WPA Key Data length "
  821. "in Group Key msg 1/2 from " MACSTR,
  822. MAC2STR(src));
  823. }
  824. }
  825. os_free(decrypted);
  826. }
  827. static void rx_data_eapol_key_2_of_2(struct wlantest *wt, const u8 *dst,
  828. const u8 *src, const u8 *data, size_t len)
  829. {
  830. struct wlantest_bss *bss;
  831. struct wlantest_sta *sta;
  832. const struct ieee802_1x_hdr *eapol;
  833. const struct wpa_eapol_key *hdr;
  834. u16 key_info;
  835. wpa_printf(MSG_DEBUG, "EAPOL-Key 2/2 " MACSTR " -> " MACSTR,
  836. MAC2STR(src), MAC2STR(dst));
  837. bss = bss_get(wt, dst);
  838. if (bss == NULL)
  839. return;
  840. sta = sta_get(bss, src);
  841. if (sta == NULL)
  842. return;
  843. eapol = (const struct ieee802_1x_hdr *) data;
  844. hdr = (const struct wpa_eapol_key *) (eapol + 1);
  845. if (!is_zero(hdr->key_rsc, 8)) {
  846. add_note(wt, MSG_INFO, "EAPOL-Key 2/2 from " MACSTR " used "
  847. "non-zero Key RSC", MAC2STR(src));
  848. }
  849. key_info = WPA_GET_BE16(hdr->key_info);
  850. if (!sta->ptk_set) {
  851. add_note(wt, MSG_DEBUG,
  852. "No PTK known to process EAPOL-Key 2/2");
  853. return;
  854. }
  855. if (sta->ptk_set &&
  856. check_mic(sta->ptk.kck, sta->ptk.kck_len, sta->key_mgmt,
  857. key_info & WPA_KEY_INFO_TYPE_MASK,
  858. data, len) < 0) {
  859. add_note(wt, MSG_INFO, "Mismatch in EAPOL-Key 2/2 MIC");
  860. return;
  861. }
  862. add_note(wt, MSG_DEBUG, "Valid MIC found in EAPOL-Key 2/2");
  863. }
  864. static void rx_data_eapol_key(struct wlantest *wt, const u8 *bssid,
  865. const u8 *sta_addr, const u8 *dst,
  866. const u8 *src, const u8 *data, size_t len,
  867. int prot)
  868. {
  869. const struct ieee802_1x_hdr *eapol;
  870. const struct wpa_eapol_key *hdr;
  871. const u8 *key_data;
  872. u16 key_info, key_length, ver, key_data_length;
  873. size_t mic_len = 16;
  874. const u8 *mic;
  875. struct wlantest_bss *bss;
  876. struct wlantest_sta *sta;
  877. bss = bss_get(wt, bssid);
  878. if (bss) {
  879. sta = sta_get(bss, sta_addr);
  880. if (sta)
  881. mic_len = wpa_mic_len(sta->key_mgmt, PMK_LEN);
  882. }
  883. eapol = (const struct ieee802_1x_hdr *) data;
  884. hdr = (const struct wpa_eapol_key *) (eapol + 1);
  885. wpa_hexdump(MSG_MSGDUMP, "EAPOL-Key",
  886. (const u8 *) hdr, len - sizeof(*eapol));
  887. if (len < sizeof(*hdr) + mic_len + 2) {
  888. add_note(wt, MSG_INFO, "Too short EAPOL-Key frame from " MACSTR,
  889. MAC2STR(src));
  890. return;
  891. }
  892. mic = (const u8 *) (hdr + 1);
  893. if (hdr->type == EAPOL_KEY_TYPE_RC4) {
  894. /* TODO: EAPOL-Key RC4 for WEP */
  895. wpa_printf(MSG_INFO, "EAPOL-Key Descriptor Type RC4 from "
  896. MACSTR, MAC2STR(src));
  897. return;
  898. }
  899. if (hdr->type != EAPOL_KEY_TYPE_RSN &&
  900. hdr->type != EAPOL_KEY_TYPE_WPA) {
  901. wpa_printf(MSG_INFO, "Unsupported EAPOL-Key Descriptor Type "
  902. "%u from " MACSTR, hdr->type, MAC2STR(src));
  903. return;
  904. }
  905. key_info = WPA_GET_BE16(hdr->key_info);
  906. key_length = WPA_GET_BE16(hdr->key_length);
  907. key_data_length = WPA_GET_BE16(mic + mic_len);
  908. key_data = mic + mic_len + 2;
  909. if (key_data + key_data_length > data + len) {
  910. add_note(wt, MSG_INFO, "Truncated EAPOL-Key from " MACSTR,
  911. MAC2STR(src));
  912. return;
  913. }
  914. if (key_data + key_data_length < data + len) {
  915. wpa_hexdump(MSG_DEBUG, "Extra data after EAPOL-Key Key Data "
  916. "field", key_data + key_data_length,
  917. data + len - key_data - key_data_length);
  918. }
  919. ver = key_info & WPA_KEY_INFO_TYPE_MASK;
  920. wpa_printf(MSG_DEBUG, "EAPOL-Key ver=%u %c idx=%u%s%s%s%s%s%s%s%s "
  921. "datalen=%u",
  922. ver, key_info & WPA_KEY_INFO_KEY_TYPE ? 'P' : 'G',
  923. (key_info & WPA_KEY_INFO_KEY_INDEX_MASK) >>
  924. WPA_KEY_INFO_KEY_INDEX_SHIFT,
  925. (key_info & WPA_KEY_INFO_INSTALL) ? " Install" : "",
  926. (key_info & WPA_KEY_INFO_ACK) ? " ACK" : "",
  927. (key_info & WPA_KEY_INFO_MIC) ? " MIC" : "",
  928. (key_info & WPA_KEY_INFO_SECURE) ? " Secure" : "",
  929. (key_info & WPA_KEY_INFO_ERROR) ? " Error" : "",
  930. (key_info & WPA_KEY_INFO_REQUEST) ? " Request" : "",
  931. (key_info & WPA_KEY_INFO_ENCR_KEY_DATA) ? " Encr" : "",
  932. (key_info & WPA_KEY_INFO_SMK_MESSAGE) ? " SMK" : "",
  933. key_data_length);
  934. if (ver != WPA_KEY_INFO_TYPE_HMAC_MD5_RC4 &&
  935. ver != WPA_KEY_INFO_TYPE_HMAC_SHA1_AES &&
  936. ver != WPA_KEY_INFO_TYPE_AES_128_CMAC &&
  937. ver != WPA_KEY_INFO_TYPE_AKM_DEFINED) {
  938. wpa_printf(MSG_INFO, "Unsupported EAPOL-Key Key Descriptor "
  939. "Version %u from " MACSTR, ver, MAC2STR(src));
  940. return;
  941. }
  942. wpa_hexdump(MSG_MSGDUMP, "EAPOL-Key Replay Counter",
  943. hdr->replay_counter, WPA_REPLAY_COUNTER_LEN);
  944. wpa_hexdump(MSG_MSGDUMP, "EAPOL-Key Key Nonce",
  945. hdr->key_nonce, WPA_NONCE_LEN);
  946. wpa_hexdump(MSG_MSGDUMP, "EAPOL-Key Key IV",
  947. hdr->key_iv, 16);
  948. wpa_hexdump(MSG_MSGDUMP, "EAPOL-Key RSC",
  949. hdr->key_rsc, WPA_KEY_RSC_LEN);
  950. wpa_hexdump(MSG_MSGDUMP, "EAPOL-Key Key MIC",
  951. mic, mic_len);
  952. wpa_hexdump(MSG_MSGDUMP, "EAPOL-Key Key Data",
  953. key_data, key_data_length);
  954. if (hdr->type == EAPOL_KEY_TYPE_RSN &&
  955. (key_info & (WPA_KEY_INFO_KEY_INDEX_MASK | BIT(14) | BIT(15))) !=
  956. 0) {
  957. wpa_printf(MSG_INFO, "RSN EAPOL-Key with non-zero reserved "
  958. "Key Info bits 0x%x from " MACSTR,
  959. key_info, MAC2STR(src));
  960. }
  961. if (hdr->type == EAPOL_KEY_TYPE_WPA &&
  962. (key_info & (WPA_KEY_INFO_ENCR_KEY_DATA |
  963. WPA_KEY_INFO_SMK_MESSAGE |BIT(14) | BIT(15))) != 0) {
  964. wpa_printf(MSG_INFO, "WPA EAPOL-Key with non-zero reserved "
  965. "Key Info bits 0x%x from " MACSTR,
  966. key_info, MAC2STR(src));
  967. }
  968. if (key_length > 32) {
  969. wpa_printf(MSG_INFO, "EAPOL-Key with invalid Key Length %d "
  970. "from " MACSTR, key_length, MAC2STR(src));
  971. }
  972. if (ver != WPA_KEY_INFO_TYPE_HMAC_MD5_RC4 &&
  973. !is_zero(hdr->key_iv, 16)) {
  974. wpa_printf(MSG_INFO, "EAPOL-Key with non-zero Key IV "
  975. "(reserved with ver=%d) field from " MACSTR,
  976. ver, MAC2STR(src));
  977. wpa_hexdump(MSG_INFO, "EAPOL-Key Key IV (reserved)",
  978. hdr->key_iv, 16);
  979. }
  980. if (!is_zero(hdr->key_id, 8)) {
  981. wpa_printf(MSG_INFO, "EAPOL-Key with non-zero Key ID "
  982. "(reserved) field from " MACSTR, MAC2STR(src));
  983. wpa_hexdump(MSG_INFO, "EAPOL-Key Key ID (reserved)",
  984. hdr->key_id, 8);
  985. }
  986. if (hdr->key_rsc[6] || hdr->key_rsc[7]) {
  987. wpa_printf(MSG_INFO, "EAPOL-Key with non-zero Key RSC octets "
  988. "(last two are unused)" MACSTR, MAC2STR(src));
  989. }
  990. if (key_info & (WPA_KEY_INFO_ERROR | WPA_KEY_INFO_REQUEST))
  991. return;
  992. if (key_info & WPA_KEY_INFO_SMK_MESSAGE)
  993. return;
  994. if (key_info & WPA_KEY_INFO_KEY_TYPE) {
  995. /* 4-Way Handshake */
  996. switch (key_info & (WPA_KEY_INFO_SECURE |
  997. WPA_KEY_INFO_MIC |
  998. WPA_KEY_INFO_ACK |
  999. WPA_KEY_INFO_INSTALL)) {
  1000. case WPA_KEY_INFO_ACK:
  1001. rx_data_eapol_key_1_of_4(wt, dst, src, data, len);
  1002. break;
  1003. case WPA_KEY_INFO_MIC:
  1004. if (key_data_length == 0)
  1005. rx_data_eapol_key_4_of_4(wt, dst, src, data,
  1006. len);
  1007. else
  1008. rx_data_eapol_key_2_of_4(wt, dst, src, data,
  1009. len);
  1010. break;
  1011. case WPA_KEY_INFO_MIC | WPA_KEY_INFO_ACK |
  1012. WPA_KEY_INFO_INSTALL:
  1013. /* WPA does not include Secure bit in 3/4 */
  1014. rx_data_eapol_key_3_of_4(wt, dst, src, data, len);
  1015. break;
  1016. case WPA_KEY_INFO_SECURE | WPA_KEY_INFO_MIC |
  1017. WPA_KEY_INFO_ACK | WPA_KEY_INFO_INSTALL:
  1018. case WPA_KEY_INFO_SECURE |
  1019. WPA_KEY_INFO_ACK | WPA_KEY_INFO_INSTALL:
  1020. rx_data_eapol_key_3_of_4(wt, dst, src, data, len);
  1021. break;
  1022. case WPA_KEY_INFO_SECURE | WPA_KEY_INFO_MIC:
  1023. case WPA_KEY_INFO_SECURE:
  1024. if (key_data_length == 0)
  1025. rx_data_eapol_key_4_of_4(wt, dst, src, data,
  1026. len);
  1027. else
  1028. rx_data_eapol_key_2_of_4(wt, dst, src, data,
  1029. len);
  1030. break;
  1031. default:
  1032. wpa_printf(MSG_DEBUG, "Unsupported EAPOL-Key frame");
  1033. break;
  1034. }
  1035. } else {
  1036. /* Group Key Handshake */
  1037. switch (key_info & (WPA_KEY_INFO_SECURE |
  1038. WPA_KEY_INFO_MIC |
  1039. WPA_KEY_INFO_ACK)) {
  1040. case WPA_KEY_INFO_SECURE | WPA_KEY_INFO_MIC |
  1041. WPA_KEY_INFO_ACK:
  1042. case WPA_KEY_INFO_SECURE | WPA_KEY_INFO_ACK:
  1043. rx_data_eapol_key_1_of_2(wt, dst, src, data, len);
  1044. break;
  1045. case WPA_KEY_INFO_SECURE | WPA_KEY_INFO_MIC:
  1046. case WPA_KEY_INFO_SECURE:
  1047. rx_data_eapol_key_2_of_2(wt, dst, src, data, len);
  1048. break;
  1049. default:
  1050. wpa_printf(MSG_DEBUG, "Unsupported EAPOL-Key frame");
  1051. break;
  1052. }
  1053. }
  1054. }
  1055. void rx_data_eapol(struct wlantest *wt, const u8 *bssid, const u8 *sta_addr,
  1056. const u8 *dst, const u8 *src,
  1057. const u8 *data, size_t len, int prot)
  1058. {
  1059. const struct ieee802_1x_hdr *hdr;
  1060. u16 length;
  1061. const u8 *p;
  1062. wpa_hexdump(MSG_EXCESSIVE, "EAPOL", data, len);
  1063. if (len < sizeof(*hdr)) {
  1064. wpa_printf(MSG_INFO, "Too short EAPOL frame from " MACSTR,
  1065. MAC2STR(src));
  1066. return;
  1067. }
  1068. hdr = (const struct ieee802_1x_hdr *) data;
  1069. length = be_to_host16(hdr->length);
  1070. wpa_printf(MSG_DEBUG, "RX EAPOL: " MACSTR " -> " MACSTR "%s ver=%u "
  1071. "type=%u len=%u",
  1072. MAC2STR(src), MAC2STR(dst), prot ? " Prot" : "",
  1073. hdr->version, hdr->type, length);
  1074. if (hdr->version < 1 || hdr->version > 3) {
  1075. wpa_printf(MSG_INFO, "Unexpected EAPOL version %u from "
  1076. MACSTR, hdr->version, MAC2STR(src));
  1077. }
  1078. if (sizeof(*hdr) + length > len) {
  1079. wpa_printf(MSG_INFO, "Truncated EAPOL frame from " MACSTR,
  1080. MAC2STR(src));
  1081. return;
  1082. }
  1083. if (sizeof(*hdr) + length < len) {
  1084. wpa_printf(MSG_INFO, "EAPOL frame with %d extra bytes",
  1085. (int) (len - sizeof(*hdr) - length));
  1086. }
  1087. p = (const u8 *) (hdr + 1);
  1088. switch (hdr->type) {
  1089. case IEEE802_1X_TYPE_EAP_PACKET:
  1090. wpa_hexdump(MSG_MSGDUMP, "EAPOL - EAP packet", p, length);
  1091. break;
  1092. case IEEE802_1X_TYPE_EAPOL_START:
  1093. wpa_hexdump(MSG_MSGDUMP, "EAPOL-Start", p, length);
  1094. break;
  1095. case IEEE802_1X_TYPE_EAPOL_LOGOFF:
  1096. wpa_hexdump(MSG_MSGDUMP, "EAPOL-Logoff", p, length);
  1097. break;
  1098. case IEEE802_1X_TYPE_EAPOL_KEY:
  1099. rx_data_eapol_key(wt, bssid, sta_addr, dst, src, data,
  1100. sizeof(*hdr) + length, prot);
  1101. break;
  1102. case IEEE802_1X_TYPE_EAPOL_ENCAPSULATED_ASF_ALERT:
  1103. wpa_hexdump(MSG_MSGDUMP, "EAPOL - Encapsulated ASF alert",
  1104. p, length);
  1105. break;
  1106. default:
  1107. wpa_hexdump(MSG_MSGDUMP, "Unknown EAPOL payload", p, length);
  1108. break;
  1109. }
  1110. }