rx_eapol.c 31 KB

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