rx_eapol.c 32 KB

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