rx_eapol.c 32 KB

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