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_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);
  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);
  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);
  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 && 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. break;
  590. }
  591. p += 2 + p[1];
  592. }
  593. os_memset(buf, 0, sizeof(buf));
  594. h = (struct ieee80211_hdr *) buf;
  595. h->frame_control = host_to_le16(0x0208);
  596. os_memcpy(h->addr1, dst, ETH_ALEN);
  597. os_memcpy(h->addr2, src, ETH_ALEN);
  598. os_memcpy(h->addr3, src, ETH_ALEN);
  599. pos = (u8 *) (h + 1);
  600. os_memcpy(pos, "\xaa\xaa\x03\x00\x00\x00\x88\x8e", 8);
  601. pos += 8;
  602. os_memcpy(pos, eapol, sizeof(*eapol));
  603. pos += sizeof(*eapol);
  604. os_memcpy(pos, hdr, sizeof(*hdr) + mic_len);
  605. k = (struct wpa_eapol_key *) pos;
  606. pos += sizeof(struct wpa_eapol_key) + mic_len;
  607. WPA_PUT_BE16(k->key_info,
  608. key_info & ~WPA_KEY_INFO_ENCR_KEY_DATA);
  609. WPA_PUT_BE16(pos, plain_len);
  610. write_pcap_decrypted(wt, buf, 24 + 8 + sizeof(*eapol) +
  611. sizeof(*hdr) + mic_len + 2,
  612. decrypted, plain_len);
  613. }
  614. if (wpa_supplicant_parse_ies(decrypted, decrypted_len, &ie) < 0) {
  615. add_note(wt, MSG_INFO, "Failed to parse EAPOL-Key Key Data");
  616. os_free(decrypted_buf);
  617. return;
  618. }
  619. if ((ie.wpa_ie &&
  620. os_memcmp(ie.wpa_ie, bss->wpaie, ie.wpa_ie_len) != 0) ||
  621. (ie.wpa_ie == NULL && bss->wpaie[0])) {
  622. add_note(wt, MSG_INFO,
  623. "Mismatch in WPA IE between EAPOL-Key 3/4 and "
  624. "Beacon/Probe Response from " MACSTR,
  625. MAC2STR(bss->bssid));
  626. wpa_hexdump(MSG_INFO, "WPA IE in EAPOL-Key",
  627. ie.wpa_ie, ie.wpa_ie_len);
  628. wpa_hexdump(MSG_INFO, "WPA IE in Beacon/Probe "
  629. "Response",
  630. bss->wpaie,
  631. bss->wpaie[0] ? 2 + bss->wpaie[1] : 0);
  632. }
  633. if ((ie.rsn_ie &&
  634. os_memcmp(ie.rsn_ie, bss->rsnie, ie.rsn_ie_len) != 0) ||
  635. (ie.rsn_ie == NULL && bss->rsnie[0])) {
  636. add_note(wt, MSG_INFO, "Mismatch in RSN IE between EAPOL-Key "
  637. "3/4 and Beacon/Probe Response from " MACSTR,
  638. MAC2STR(bss->bssid));
  639. wpa_hexdump(MSG_INFO, "RSN IE in EAPOL-Key",
  640. ie.rsn_ie, ie.rsn_ie_len);
  641. wpa_hexdump(MSG_INFO, "RSN IE in Beacon/Probe Response",
  642. bss->rsnie,
  643. bss->rsnie[0] ? 2 + bss->rsnie[1] : 0);
  644. }
  645. learn_kde_keys(wt, bss, sta, decrypted, decrypted_len, hdr->key_rsc);
  646. os_free(decrypted_buf);
  647. }
  648. static void rx_data_eapol_key_4_of_4(struct wlantest *wt, const u8 *dst,
  649. const u8 *src, const u8 *data, size_t len)
  650. {
  651. struct wlantest_bss *bss;
  652. struct wlantest_sta *sta;
  653. const struct ieee802_1x_hdr *eapol;
  654. const struct wpa_eapol_key *hdr;
  655. u16 key_info;
  656. const u8 *kck;
  657. size_t kck_len;
  658. wpa_printf(MSG_DEBUG, "EAPOL-Key 4/4 " MACSTR " -> " MACSTR,
  659. MAC2STR(src), MAC2STR(dst));
  660. bss = bss_get(wt, dst);
  661. if (bss == NULL)
  662. return;
  663. sta = sta_get(bss, src);
  664. if (sta == NULL)
  665. return;
  666. eapol = (const struct ieee802_1x_hdr *) data;
  667. hdr = (const struct wpa_eapol_key *) (eapol + 1);
  668. if (!is_zero(hdr->key_rsc, 8)) {
  669. add_note(wt, MSG_INFO, "EAPOL-Key 4/4 from " MACSTR " used "
  670. "non-zero Key RSC", MAC2STR(src));
  671. }
  672. key_info = WPA_GET_BE16(hdr->key_info);
  673. if (!sta->ptk_set && !sta->tptk_set) {
  674. add_note(wt, MSG_DEBUG,
  675. "No PTK known to process EAPOL-Key 4/4");
  676. return;
  677. }
  678. kck = sta->ptk.kck;
  679. kck_len = sta->ptk.kck_len;
  680. if (sta->tptk_set) {
  681. add_note(wt, MSG_DEBUG,
  682. "Use TPTK for validation EAPOL-Key MIC");
  683. kck = sta->tptk.kck;
  684. kck_len = sta->tptk.kck_len;
  685. }
  686. if (check_mic(kck, kck_len, sta->key_mgmt,
  687. key_info & WPA_KEY_INFO_TYPE_MASK, data, len) < 0) {
  688. add_note(wt, MSG_INFO, "Mismatch in EAPOL-Key 4/4 MIC");
  689. return;
  690. }
  691. add_note(wt, MSG_DEBUG, "Valid MIC found in EAPOL-Key 4/4");
  692. if (sta->tptk_set) {
  693. add_note(wt, MSG_DEBUG, "Update PTK (rekeying)");
  694. os_memcpy(&sta->ptk, &sta->tptk, sizeof(sta->ptk));
  695. sta->ptk_set = 1;
  696. sta->tptk_set = 0;
  697. os_memset(sta->rsc_tods, 0, sizeof(sta->rsc_tods));
  698. os_memset(sta->rsc_fromds, 0, sizeof(sta->rsc_fromds));
  699. }
  700. }
  701. static void rx_data_eapol_key_1_of_2(struct wlantest *wt, const u8 *dst,
  702. const u8 *src, const u8 *data, size_t len)
  703. {
  704. struct wlantest_bss *bss;
  705. struct wlantest_sta *sta;
  706. const struct ieee802_1x_hdr *eapol;
  707. const struct wpa_eapol_key *hdr;
  708. u16 key_info, ver;
  709. u8 *decrypted;
  710. size_t decrypted_len = 0;
  711. size_t mic_len;
  712. wpa_printf(MSG_DEBUG, "EAPOL-Key 1/2 " MACSTR " -> " MACSTR,
  713. MAC2STR(src), MAC2STR(dst));
  714. bss = bss_get(wt, src);
  715. if (bss == NULL)
  716. return;
  717. sta = sta_get(bss, dst);
  718. if (sta == NULL)
  719. return;
  720. mic_len = wpa_mic_len(sta->key_mgmt);
  721. eapol = (const struct ieee802_1x_hdr *) data;
  722. hdr = (const struct wpa_eapol_key *) (eapol + 1);
  723. key_info = WPA_GET_BE16(hdr->key_info);
  724. if (!sta->ptk_set) {
  725. add_note(wt, MSG_DEBUG,
  726. "No PTK known to process EAPOL-Key 1/2");
  727. return;
  728. }
  729. if (sta->ptk_set &&
  730. check_mic(sta->ptk.kck, sta->ptk.kck_len, sta->key_mgmt,
  731. key_info & WPA_KEY_INFO_TYPE_MASK,
  732. data, len) < 0) {
  733. add_note(wt, MSG_INFO, "Mismatch in EAPOL-Key 1/2 MIC");
  734. return;
  735. }
  736. add_note(wt, MSG_DEBUG, "Valid MIC found in EAPOL-Key 1/2");
  737. if (sta->proto & WPA_PROTO_RSN &&
  738. !(key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
  739. add_note(wt, MSG_INFO, "EAPOL-Key 1/2 without EncrKeyData bit");
  740. return;
  741. }
  742. ver = key_info & WPA_KEY_INFO_TYPE_MASK;
  743. decrypted = decrypt_eapol_key_data(wt, sta->key_mgmt,
  744. sta->ptk.kek, sta->ptk.kek_len,
  745. ver, hdr, &decrypted_len);
  746. if (decrypted == NULL) {
  747. add_note(wt, MSG_INFO, "Failed to decrypt EAPOL-Key Key Data");
  748. return;
  749. }
  750. wpa_hexdump(MSG_DEBUG, "Decrypted EAPOL-Key Key Data",
  751. decrypted, decrypted_len);
  752. if (wt->write_pcap_dumper) {
  753. /* Fill in a dummy Data frame header */
  754. u8 buf[24 + 8 + sizeof(*eapol) + sizeof(*hdr) + 64];
  755. struct ieee80211_hdr *h;
  756. struct wpa_eapol_key *k;
  757. u8 *pos;
  758. size_t plain_len;
  759. plain_len = decrypted_len;
  760. pos = decrypted;
  761. while (pos + 1 < decrypted + decrypted_len) {
  762. if (pos[0] == 0xdd && pos[1] == 0x00) {
  763. /* Remove padding */
  764. plain_len = pos - decrypted;
  765. break;
  766. }
  767. pos += 2 + pos[1];
  768. }
  769. os_memset(buf, 0, sizeof(buf));
  770. h = (struct ieee80211_hdr *) buf;
  771. h->frame_control = host_to_le16(0x0208);
  772. os_memcpy(h->addr1, dst, ETH_ALEN);
  773. os_memcpy(h->addr2, src, ETH_ALEN);
  774. os_memcpy(h->addr3, src, ETH_ALEN);
  775. pos = (u8 *) (h + 1);
  776. os_memcpy(pos, "\xaa\xaa\x03\x00\x00\x00\x88\x8e", 8);
  777. pos += 8;
  778. os_memcpy(pos, eapol, sizeof(*eapol));
  779. pos += sizeof(*eapol);
  780. os_memcpy(pos, hdr, sizeof(*hdr) + mic_len);
  781. k = (struct wpa_eapol_key *) pos;
  782. pos += sizeof(struct wpa_eapol_key) + mic_len;
  783. WPA_PUT_BE16(k->key_info,
  784. key_info & ~WPA_KEY_INFO_ENCR_KEY_DATA);
  785. WPA_PUT_BE16(pos, plain_len);
  786. write_pcap_decrypted(wt, buf, 24 + 8 + sizeof(*eapol) +
  787. sizeof(*hdr) + mic_len + 2,
  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. }