rx_eapol.c 35 KB

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