rx_eapol.c 35 KB

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