rx_eapol.c 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151
  1. /*
  2. * Received Data frame processing for EAPOL messages
  3. * Copyright (c) 2010-2015, Jouni Malinen <j@w1.fi>
  4. *
  5. * This software may be distributed under the terms of the BSD license.
  6. * See README for more details.
  7. */
  8. #include "utils/includes.h"
  9. #include "utils/common.h"
  10. #include "crypto/aes_wrap.h"
  11. #include "crypto/crypto.h"
  12. #include "common/defs.h"
  13. #include "common/ieee802_11_defs.h"
  14. #include "common/ieee802_11_common.h"
  15. #include "common/eapol_common.h"
  16. #include "common/wpa_common.h"
  17. #include "rsn_supp/wpa_ie.h"
  18. #include "wlantest.h"
  19. static int is_zero(const u8 *buf, size_t len)
  20. {
  21. size_t i;
  22. for (i = 0; i < len; i++) {
  23. if (buf[i])
  24. return 0;
  25. }
  26. return 1;
  27. }
  28. static int check_mic(const u8 *kck, size_t kck_len, int akmp, int ver,
  29. const u8 *data, size_t len)
  30. {
  31. u8 *buf;
  32. int ret = -1;
  33. struct ieee802_1x_hdr *hdr;
  34. struct wpa_eapol_key *key;
  35. u8 rx_mic[WPA_EAPOL_KEY_MIC_MAX_LEN];
  36. size_t mic_len = 16;
  37. buf = os_malloc(len);
  38. if (buf == NULL)
  39. return -1;
  40. os_memcpy(buf, data, len);
  41. hdr = (struct ieee802_1x_hdr *) buf;
  42. key = (struct wpa_eapol_key *) (hdr + 1);
  43. os_memcpy(rx_mic, key->key_mic, mic_len);
  44. os_memset(key->key_mic, 0, mic_len);
  45. if (wpa_eapol_key_mic(kck, kck_len, akmp, ver, buf, len,
  46. key->key_mic) == 0 &&
  47. os_memcmp(rx_mic, key->key_mic, mic_len) == 0)
  48. ret = 0;
  49. os_free(buf);
  50. return ret;
  51. }
  52. static void rx_data_eapol_key_1_of_4(struct wlantest *wt, const u8 *dst,
  53. const u8 *src, const u8 *data, size_t len)
  54. {
  55. struct wlantest_bss *bss;
  56. struct wlantest_sta *sta;
  57. const struct ieee802_1x_hdr *eapol;
  58. const struct wpa_eapol_key *hdr;
  59. wpa_printf(MSG_DEBUG, "EAPOL-Key 1/4 " MACSTR " -> " MACSTR,
  60. MAC2STR(src), MAC2STR(dst));
  61. bss = bss_get(wt, src);
  62. if (bss == NULL)
  63. return;
  64. sta = sta_get(bss, dst);
  65. if (sta == NULL)
  66. return;
  67. eapol = (const struct ieee802_1x_hdr *) data;
  68. hdr = (const struct wpa_eapol_key *) (eapol + 1);
  69. if (is_zero(hdr->key_nonce, WPA_NONCE_LEN)) {
  70. add_note(wt, MSG_INFO, "EAPOL-Key 1/4 from " MACSTR
  71. " used zero nonce", MAC2STR(src));
  72. }
  73. if (!is_zero(hdr->key_rsc, 8)) {
  74. add_note(wt, MSG_INFO, "EAPOL-Key 1/4 from " MACSTR
  75. " used non-zero Key RSC", MAC2STR(src));
  76. }
  77. os_memcpy(sta->anonce, hdr->key_nonce, WPA_NONCE_LEN);
  78. }
  79. static int try_pmk(struct wlantest *wt, struct wlantest_bss *bss,
  80. struct wlantest_sta *sta, u16 ver,
  81. const u8 *data, size_t len,
  82. struct wlantest_pmk *pmk)
  83. {
  84. struct wpa_ptk ptk;
  85. if (wpa_pmk_to_ptk(pmk->pmk, sizeof(pmk->pmk),
  86. "Pairwise key expansion",
  87. bss->bssid, sta->addr, sta->anonce, sta->snonce,
  88. &ptk, sta->key_mgmt, sta->pairwise_cipher) < 0 ||
  89. check_mic(ptk.kck, ptk.kck_len, sta->key_mgmt, 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",
  103. sta->tptk.kck, sta->tptk.kck_len);
  104. wpa_hexdump(MSG_DEBUG, "TPTK:KEK",
  105. sta->tptk.kek, sta->tptk.kek_len);
  106. wpa_hexdump(MSG_DEBUG, "TPTK:TK",
  107. sta->tptk.tk, sta->tptk.tk_len);
  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, sta->ptk.kck_len);
  114. wpa_hexdump(MSG_DEBUG, "PTK:KEK", sta->ptk.kek, sta->ptk.kek_len);
  115. wpa_hexdump(MSG_DEBUG, "PTK:TK", sta->ptk.tk, sta->ptk.tk_len);
  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, ptk->ptk.kck_len,
  144. sta->key_mgmt, ver, data, len) < 0)
  145. continue;
  146. wpa_printf(MSG_INFO, "Pre-set PTK matches for STA "
  147. MACSTR " BSSID " MACSTR,
  148. MAC2STR(sta->addr), MAC2STR(bss->bssid));
  149. add_note(wt, MSG_DEBUG, "Using pre-set PTK");
  150. ptk->ptk_len = 32 +
  151. wpa_cipher_key_len(sta->pairwise_cipher);
  152. os_memcpy(&sta->ptk, &ptk->ptk, sizeof(ptk->ptk));
  153. wpa_hexdump(MSG_DEBUG, "PTK:KCK",
  154. sta->ptk.kck, sta->ptk.kck_len);
  155. wpa_hexdump(MSG_DEBUG, "PTK:KEK",
  156. sta->ptk.kek, sta->ptk.kek_len);
  157. wpa_hexdump(MSG_DEBUG, "PTK:TK",
  158. sta->ptk.tk, sta->ptk.tk_len);
  159. sta->ptk_set = 1;
  160. os_memset(sta->rsc_tods, 0, sizeof(sta->rsc_tods));
  161. os_memset(sta->rsc_fromds, 0, sizeof(sta->rsc_fromds));
  162. }
  163. wpa_debug_level = prev_level;
  164. }
  165. add_note(wt, MSG_DEBUG, "No matching PMK found to derive PTK");
  166. }
  167. static void rx_data_eapol_key_2_of_4(struct wlantest *wt, const u8 *dst,
  168. const u8 *src, const u8 *data, size_t len)
  169. {
  170. struct wlantest_bss *bss;
  171. struct wlantest_sta *sta;
  172. const struct ieee802_1x_hdr *eapol;
  173. const struct wpa_eapol_key *hdr;
  174. const u8 *key_data, *kck;
  175. size_t kck_len;
  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. kck_len = sta->ptk.kck_len;
  207. if (sta->tptk_set) {
  208. add_note(wt, MSG_DEBUG,
  209. "Use TPTK for validation EAPOL-Key MIC");
  210. kck = sta->tptk.kck;
  211. kck_len = sta->tptk.kck_len;
  212. }
  213. if (check_mic(kck, kck_len, sta->key_mgmt,
  214. key_info & WPA_KEY_INFO_TYPE_MASK, 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,
  327. size_t kek_len, u16 ver,
  328. const struct wpa_eapol_key *hdr,
  329. size_t *len)
  330. {
  331. if (kek_len != 16)
  332. return NULL;
  333. switch (ver) {
  334. case WPA_KEY_INFO_TYPE_HMAC_MD5_RC4:
  335. return decrypt_eapol_key_data_rc4(wt, kek, hdr, len);
  336. case WPA_KEY_INFO_TYPE_HMAC_SHA1_AES:
  337. case WPA_KEY_INFO_TYPE_AES_128_CMAC:
  338. return decrypt_eapol_key_data_aes(wt, kek, hdr, len);
  339. case WPA_KEY_INFO_TYPE_AKM_DEFINED:
  340. /* For now, assume this is OSEN */
  341. return decrypt_eapol_key_data_aes(wt, kek, hdr, len);
  342. default:
  343. add_note(wt, MSG_INFO,
  344. "Unsupported EAPOL-Key Key Descriptor Version %u",
  345. ver);
  346. return NULL;
  347. }
  348. }
  349. static void learn_kde_keys(struct wlantest *wt, struct wlantest_bss *bss,
  350. struct wlantest_sta *sta,
  351. const u8 *buf, size_t len, const u8 *rsc)
  352. {
  353. struct wpa_eapol_ie_parse ie;
  354. if (wpa_supplicant_parse_ies(buf, len, &ie) < 0) {
  355. add_note(wt, MSG_INFO, "Failed to parse EAPOL-Key Key Data");
  356. return;
  357. }
  358. if (ie.wpa_ie) {
  359. wpa_hexdump(MSG_MSGDUMP, "EAPOL-Key Key Data - WPA IE",
  360. ie.wpa_ie, ie.wpa_ie_len);
  361. }
  362. if (ie.rsn_ie) {
  363. wpa_hexdump(MSG_MSGDUMP, "EAPOL-Key Key Data - RSN IE",
  364. ie.rsn_ie, ie.rsn_ie_len);
  365. }
  366. if (ie.gtk) {
  367. wpa_hexdump(MSG_MSGDUMP, "EAPOL-Key Key Data - GTK KDE",
  368. ie.gtk, ie.gtk_len);
  369. if (ie.gtk_len >= 2 && ie.gtk_len <= 2 + 32) {
  370. int id;
  371. id = ie.gtk[0] & 0x03;
  372. add_note(wt, MSG_DEBUG, "GTK KeyID=%u tx=%u",
  373. id, !!(ie.gtk[0] & 0x04));
  374. if ((ie.gtk[0] & 0xf8) || ie.gtk[1]) {
  375. add_note(wt, MSG_INFO,
  376. "GTK KDE: Reserved field set: "
  377. "%02x %02x", ie.gtk[0], ie.gtk[1]);
  378. }
  379. wpa_hexdump(MSG_DEBUG, "GTK", ie.gtk + 2,
  380. ie.gtk_len - 2);
  381. bss->gtk_len[id] = ie.gtk_len - 2;
  382. sta->gtk_len = ie.gtk_len - 2;
  383. os_memcpy(bss->gtk[id], ie.gtk + 2, ie.gtk_len - 2);
  384. os_memcpy(sta->gtk, ie.gtk + 2, ie.gtk_len - 2);
  385. bss->rsc[id][0] = rsc[5];
  386. bss->rsc[id][1] = rsc[4];
  387. bss->rsc[id][2] = rsc[3];
  388. bss->rsc[id][3] = rsc[2];
  389. bss->rsc[id][4] = rsc[1];
  390. bss->rsc[id][5] = rsc[0];
  391. bss->gtk_idx = id;
  392. sta->gtk_idx = id;
  393. wpa_hexdump(MSG_DEBUG, "RSC", bss->rsc[id], 6);
  394. } else {
  395. add_note(wt, MSG_INFO, "Invalid GTK KDE length %u",
  396. (unsigned) ie.gtk_len);
  397. }
  398. }
  399. if (ie.igtk) {
  400. wpa_hexdump(MSG_MSGDUMP, "EAPOL-Key Key Data - IGTK KDE",
  401. ie.igtk, ie.igtk_len);
  402. if (ie.igtk_len == 24) {
  403. u16 id;
  404. id = WPA_GET_LE16(ie.igtk);
  405. if (id > 5) {
  406. add_note(wt, MSG_INFO,
  407. "Unexpected IGTK KeyID %u", id);
  408. } else {
  409. const u8 *ipn;
  410. add_note(wt, MSG_DEBUG, "IGTK KeyID %u", id);
  411. wpa_hexdump(MSG_DEBUG, "IPN", ie.igtk + 2, 6);
  412. wpa_hexdump(MSG_DEBUG, "IGTK", ie.igtk + 8,
  413. 16);
  414. os_memcpy(bss->igtk[id], ie.igtk + 8, 16);
  415. bss->igtk_len[id] = 16;
  416. ipn = ie.igtk + 2;
  417. bss->ipn[id][0] = ipn[5];
  418. bss->ipn[id][1] = ipn[4];
  419. bss->ipn[id][2] = ipn[3];
  420. bss->ipn[id][3] = ipn[2];
  421. bss->ipn[id][4] = ipn[1];
  422. bss->ipn[id][5] = ipn[0];
  423. bss->igtk_idx = id;
  424. }
  425. } else if (ie.igtk_len == 40) {
  426. u16 id;
  427. id = WPA_GET_LE16(ie.igtk);
  428. if (id > 5) {
  429. add_note(wt, MSG_INFO,
  430. "Unexpected IGTK KeyID %u", id);
  431. } else {
  432. const u8 *ipn;
  433. add_note(wt, MSG_DEBUG, "IGTK KeyID %u", id);
  434. wpa_hexdump(MSG_DEBUG, "IPN", ie.igtk + 2, 6);
  435. wpa_hexdump(MSG_DEBUG, "IGTK", ie.igtk + 8,
  436. 32);
  437. os_memcpy(bss->igtk[id], ie.igtk + 8, 32);
  438. bss->igtk_len[id] = 32;
  439. ipn = ie.igtk + 2;
  440. bss->ipn[id][0] = ipn[5];
  441. bss->ipn[id][1] = ipn[4];
  442. bss->ipn[id][2] = ipn[3];
  443. bss->ipn[id][3] = ipn[2];
  444. bss->ipn[id][4] = ipn[1];
  445. bss->ipn[id][5] = ipn[0];
  446. bss->igtk_idx = id;
  447. }
  448. } else {
  449. add_note(wt, MSG_INFO, "Invalid IGTK KDE length %u",
  450. (unsigned) ie.igtk_len);
  451. }
  452. }
  453. }
  454. static void rx_data_eapol_key_3_of_4(struct wlantest *wt, const u8 *dst,
  455. const u8 *src, const u8 *data, size_t len)
  456. {
  457. struct wlantest_bss *bss;
  458. struct wlantest_sta *sta;
  459. const struct ieee802_1x_hdr *eapol;
  460. const struct wpa_eapol_key *hdr;
  461. const u8 *key_data, *kck, *kek;
  462. size_t kck_len, kek_len;
  463. int recalc = 0;
  464. u16 key_info, ver;
  465. u8 *decrypted_buf = NULL;
  466. const u8 *decrypted;
  467. size_t decrypted_len = 0;
  468. struct wpa_eapol_ie_parse ie;
  469. wpa_printf(MSG_DEBUG, "EAPOL-Key 3/4 " MACSTR " -> " MACSTR,
  470. MAC2STR(src), MAC2STR(dst));
  471. bss = bss_get(wt, src);
  472. if (bss == NULL)
  473. return;
  474. sta = sta_get(bss, dst);
  475. if (sta == NULL)
  476. return;
  477. eapol = (const struct ieee802_1x_hdr *) data;
  478. hdr = (const struct wpa_eapol_key *) (eapol + 1);
  479. key_info = WPA_GET_BE16(hdr->key_info);
  480. if (os_memcmp(sta->anonce, hdr->key_nonce, WPA_NONCE_LEN) != 0) {
  481. add_note(wt, MSG_INFO,
  482. "EAPOL-Key ANonce mismatch between 1/4 and 3/4");
  483. recalc = 1;
  484. }
  485. os_memcpy(sta->anonce, hdr->key_nonce, WPA_NONCE_LEN);
  486. if (recalc) {
  487. derive_ptk(wt, bss, sta, key_info & WPA_KEY_INFO_TYPE_MASK,
  488. data, len);
  489. }
  490. if (!sta->ptk_set && !sta->tptk_set) {
  491. add_note(wt, MSG_DEBUG,
  492. "No PTK known to process EAPOL-Key 3/4");
  493. return;
  494. }
  495. kek = sta->ptk.kek;
  496. kek_len = sta->ptk.kek_len;
  497. kck = sta->ptk.kck;
  498. kck_len = sta->ptk.kck_len;
  499. if (sta->tptk_set) {
  500. add_note(wt, MSG_DEBUG,
  501. "Use TPTK for validation EAPOL-Key MIC");
  502. kck = sta->tptk.kck;
  503. kck_len = sta->tptk.kck_len;
  504. kek = sta->tptk.kek;
  505. kek_len = sta->tptk.kek_len;
  506. }
  507. if (check_mic(kck, kck_len, sta->key_mgmt,
  508. key_info & WPA_KEY_INFO_TYPE_MASK, data, len) < 0) {
  509. add_note(wt, MSG_INFO, "Mismatch in EAPOL-Key 3/4 MIC");
  510. return;
  511. }
  512. add_note(wt, MSG_DEBUG, "Valid MIC found in EAPOL-Key 3/4");
  513. key_data = (const u8 *) (hdr + 1);
  514. if (!(key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
  515. if (sta->proto & WPA_PROTO_RSN)
  516. add_note(wt, MSG_INFO,
  517. "EAPOL-Key 3/4 without EncrKeyData bit");
  518. decrypted = key_data;
  519. decrypted_len = WPA_GET_BE16(hdr->key_data_length);
  520. } else {
  521. ver = key_info & WPA_KEY_INFO_TYPE_MASK;
  522. decrypted_buf = decrypt_eapol_key_data(wt, kek, kek_len, ver,
  523. hdr, &decrypted_len);
  524. if (decrypted_buf == NULL) {
  525. add_note(wt, MSG_INFO,
  526. "Failed to decrypt EAPOL-Key Key Data");
  527. return;
  528. }
  529. decrypted = decrypted_buf;
  530. wpa_hexdump(MSG_DEBUG, "Decrypted EAPOL-Key Key Data",
  531. decrypted, decrypted_len);
  532. }
  533. if (wt->write_pcap_dumper && decrypted != key_data) {
  534. /* Fill in a dummy Data frame header */
  535. u8 buf[24 + 8 + sizeof(*eapol) + sizeof(*hdr)];
  536. struct ieee80211_hdr *h;
  537. struct wpa_eapol_key *k;
  538. const u8 *p;
  539. u8 *pos;
  540. size_t plain_len;
  541. plain_len = decrypted_len;
  542. p = decrypted;
  543. while (p + 1 < decrypted + decrypted_len) {
  544. if (p[0] == 0xdd && p[1] == 0x00) {
  545. /* Remove padding */
  546. plain_len = p - decrypted;
  547. break;
  548. }
  549. p += 2 + p[1];
  550. }
  551. os_memset(buf, 0, sizeof(buf));
  552. h = (struct ieee80211_hdr *) buf;
  553. h->frame_control = host_to_le16(0x0208);
  554. os_memcpy(h->addr1, dst, ETH_ALEN);
  555. os_memcpy(h->addr2, src, ETH_ALEN);
  556. os_memcpy(h->addr3, src, ETH_ALEN);
  557. pos = (u8 *) (h + 1);
  558. os_memcpy(pos, "\xaa\xaa\x03\x00\x00\x00\x88\x8e", 8);
  559. pos += 8;
  560. os_memcpy(pos, eapol, sizeof(*eapol));
  561. pos += sizeof(*eapol);
  562. os_memcpy(pos, hdr, sizeof(*hdr));
  563. k = (struct wpa_eapol_key *) pos;
  564. WPA_PUT_BE16(k->key_info,
  565. key_info & ~WPA_KEY_INFO_ENCR_KEY_DATA);
  566. WPA_PUT_BE16(k->key_data_length, plain_len);
  567. write_pcap_decrypted(wt, buf, sizeof(buf),
  568. decrypted, plain_len);
  569. }
  570. if (wpa_supplicant_parse_ies(decrypted, decrypted_len, &ie) < 0) {
  571. add_note(wt, MSG_INFO, "Failed to parse EAPOL-Key Key Data");
  572. os_free(decrypted_buf);
  573. return;
  574. }
  575. if ((ie.wpa_ie &&
  576. os_memcmp(ie.wpa_ie, bss->wpaie, ie.wpa_ie_len) != 0) ||
  577. (ie.wpa_ie == NULL && bss->wpaie[0])) {
  578. add_note(wt, MSG_INFO,
  579. "Mismatch in WPA IE between EAPOL-Key 3/4 and "
  580. "Beacon/Probe Response from " MACSTR,
  581. MAC2STR(bss->bssid));
  582. wpa_hexdump(MSG_INFO, "WPA IE in EAPOL-Key",
  583. ie.wpa_ie, ie.wpa_ie_len);
  584. wpa_hexdump(MSG_INFO, "WPA IE in Beacon/Probe "
  585. "Response",
  586. bss->wpaie,
  587. bss->wpaie[0] ? 2 + bss->wpaie[1] : 0);
  588. }
  589. if ((ie.rsn_ie &&
  590. os_memcmp(ie.rsn_ie, bss->rsnie, ie.rsn_ie_len) != 0) ||
  591. (ie.rsn_ie == NULL && bss->rsnie[0])) {
  592. add_note(wt, MSG_INFO, "Mismatch in RSN IE between EAPOL-Key "
  593. "3/4 and Beacon/Probe Response from " MACSTR,
  594. MAC2STR(bss->bssid));
  595. wpa_hexdump(MSG_INFO, "RSN IE in EAPOL-Key",
  596. ie.rsn_ie, ie.rsn_ie_len);
  597. wpa_hexdump(MSG_INFO, "RSN IE in Beacon/Probe Response",
  598. bss->rsnie,
  599. bss->rsnie[0] ? 2 + bss->rsnie[1] : 0);
  600. }
  601. learn_kde_keys(wt, bss, sta, decrypted, decrypted_len, hdr->key_rsc);
  602. os_free(decrypted_buf);
  603. }
  604. static void rx_data_eapol_key_4_of_4(struct wlantest *wt, const u8 *dst,
  605. const u8 *src, const u8 *data, size_t len)
  606. {
  607. struct wlantest_bss *bss;
  608. struct wlantest_sta *sta;
  609. const struct ieee802_1x_hdr *eapol;
  610. const struct wpa_eapol_key *hdr;
  611. u16 key_info;
  612. const u8 *kck;
  613. size_t kck_len;
  614. wpa_printf(MSG_DEBUG, "EAPOL-Key 4/4 " MACSTR " -> " MACSTR,
  615. MAC2STR(src), MAC2STR(dst));
  616. bss = bss_get(wt, dst);
  617. if (bss == NULL)
  618. return;
  619. sta = sta_get(bss, src);
  620. if (sta == NULL)
  621. return;
  622. eapol = (const struct ieee802_1x_hdr *) data;
  623. hdr = (const struct wpa_eapol_key *) (eapol + 1);
  624. if (!is_zero(hdr->key_rsc, 8)) {
  625. add_note(wt, MSG_INFO, "EAPOL-Key 4/4 from " MACSTR " used "
  626. "non-zero Key RSC", MAC2STR(src));
  627. }
  628. key_info = WPA_GET_BE16(hdr->key_info);
  629. if (!sta->ptk_set && !sta->tptk_set) {
  630. add_note(wt, MSG_DEBUG,
  631. "No PTK known to process EAPOL-Key 4/4");
  632. return;
  633. }
  634. kck = sta->ptk.kck;
  635. kck_len = sta->ptk.kck_len;
  636. if (sta->tptk_set) {
  637. add_note(wt, MSG_DEBUG,
  638. "Use TPTK for validation EAPOL-Key MIC");
  639. kck = sta->tptk.kck;
  640. kck_len = sta->tptk.kck_len;
  641. }
  642. if (check_mic(kck, kck_len, sta->key_mgmt,
  643. key_info & WPA_KEY_INFO_TYPE_MASK, data, len) < 0) {
  644. add_note(wt, MSG_INFO, "Mismatch in EAPOL-Key 4/4 MIC");
  645. return;
  646. }
  647. add_note(wt, MSG_DEBUG, "Valid MIC found in EAPOL-Key 4/4");
  648. if (sta->tptk_set) {
  649. add_note(wt, MSG_DEBUG, "Update PTK (rekeying)");
  650. os_memcpy(&sta->ptk, &sta->tptk, sizeof(sta->ptk));
  651. sta->ptk_set = 1;
  652. sta->tptk_set = 0;
  653. os_memset(sta->rsc_tods, 0, sizeof(sta->rsc_tods));
  654. os_memset(sta->rsc_fromds, 0, sizeof(sta->rsc_fromds));
  655. }
  656. }
  657. static void rx_data_eapol_key_1_of_2(struct wlantest *wt, const u8 *dst,
  658. const u8 *src, const u8 *data, size_t len)
  659. {
  660. struct wlantest_bss *bss;
  661. struct wlantest_sta *sta;
  662. const struct ieee802_1x_hdr *eapol;
  663. const struct wpa_eapol_key *hdr;
  664. u16 key_info, ver;
  665. u8 *decrypted;
  666. size_t decrypted_len = 0;
  667. wpa_printf(MSG_DEBUG, "EAPOL-Key 1/2 " MACSTR " -> " MACSTR,
  668. MAC2STR(src), MAC2STR(dst));
  669. bss = bss_get(wt, src);
  670. if (bss == NULL)
  671. return;
  672. sta = sta_get(bss, dst);
  673. if (sta == NULL)
  674. return;
  675. eapol = (const struct ieee802_1x_hdr *) data;
  676. hdr = (const struct wpa_eapol_key *) (eapol + 1);
  677. key_info = WPA_GET_BE16(hdr->key_info);
  678. if (!sta->ptk_set) {
  679. add_note(wt, MSG_DEBUG,
  680. "No PTK known to process EAPOL-Key 1/2");
  681. return;
  682. }
  683. if (sta->ptk_set &&
  684. check_mic(sta->ptk.kck, sta->ptk.kck_len, sta->key_mgmt,
  685. key_info & WPA_KEY_INFO_TYPE_MASK,
  686. data, len) < 0) {
  687. add_note(wt, MSG_INFO, "Mismatch in EAPOL-Key 1/2 MIC");
  688. return;
  689. }
  690. add_note(wt, MSG_DEBUG, "Valid MIC found in EAPOL-Key 1/2");
  691. if (sta->proto & WPA_PROTO_RSN &&
  692. !(key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
  693. add_note(wt, MSG_INFO, "EAPOL-Key 1/2 without EncrKeyData bit");
  694. return;
  695. }
  696. ver = key_info & WPA_KEY_INFO_TYPE_MASK;
  697. decrypted = decrypt_eapol_key_data(wt, sta->ptk.kek, sta->ptk.kek_len,
  698. ver, hdr, &decrypted_len);
  699. if (decrypted == NULL) {
  700. add_note(wt, MSG_INFO, "Failed to decrypt EAPOL-Key Key Data");
  701. return;
  702. }
  703. wpa_hexdump(MSG_DEBUG, "Decrypted EAPOL-Key Key Data",
  704. decrypted, decrypted_len);
  705. if (wt->write_pcap_dumper) {
  706. /* Fill in a dummy Data frame header */
  707. u8 buf[24 + 8 + sizeof(*eapol) + sizeof(*hdr)];
  708. struct ieee80211_hdr *h;
  709. struct wpa_eapol_key *k;
  710. u8 *pos;
  711. size_t plain_len;
  712. plain_len = decrypted_len;
  713. pos = decrypted;
  714. while (pos + 1 < decrypted + decrypted_len) {
  715. if (pos[0] == 0xdd && pos[1] == 0x00) {
  716. /* Remove padding */
  717. plain_len = pos - decrypted;
  718. break;
  719. }
  720. pos += 2 + pos[1];
  721. }
  722. os_memset(buf, 0, sizeof(buf));
  723. h = (struct ieee80211_hdr *) buf;
  724. h->frame_control = host_to_le16(0x0208);
  725. os_memcpy(h->addr1, dst, ETH_ALEN);
  726. os_memcpy(h->addr2, src, ETH_ALEN);
  727. os_memcpy(h->addr3, src, ETH_ALEN);
  728. pos = (u8 *) (h + 1);
  729. os_memcpy(pos, "\xaa\xaa\x03\x00\x00\x00\x88\x8e", 8);
  730. pos += 8;
  731. os_memcpy(pos, eapol, sizeof(*eapol));
  732. pos += sizeof(*eapol);
  733. os_memcpy(pos, hdr, sizeof(*hdr));
  734. k = (struct wpa_eapol_key *) pos;
  735. WPA_PUT_BE16(k->key_info,
  736. key_info & ~WPA_KEY_INFO_ENCR_KEY_DATA);
  737. WPA_PUT_BE16(k->key_data_length, plain_len);
  738. write_pcap_decrypted(wt, buf, sizeof(buf),
  739. decrypted, plain_len);
  740. }
  741. if (sta->proto & WPA_PROTO_RSN)
  742. learn_kde_keys(wt, bss, sta, decrypted, decrypted_len,
  743. hdr->key_rsc);
  744. else {
  745. int klen = bss->group_cipher == WPA_CIPHER_TKIP ? 32 : 16;
  746. if (decrypted_len == klen) {
  747. const u8 *rsc = hdr->key_rsc;
  748. int id;
  749. id = (key_info & WPA_KEY_INFO_KEY_INDEX_MASK) >>
  750. WPA_KEY_INFO_KEY_INDEX_SHIFT;
  751. add_note(wt, MSG_DEBUG, "GTK key index %d", id);
  752. wpa_hexdump(MSG_DEBUG, "GTK", decrypted,
  753. decrypted_len);
  754. bss->gtk_len[id] = decrypted_len;
  755. os_memcpy(bss->gtk[id], decrypted, decrypted_len);
  756. bss->rsc[id][0] = rsc[5];
  757. bss->rsc[id][1] = rsc[4];
  758. bss->rsc[id][2] = rsc[3];
  759. bss->rsc[id][3] = rsc[2];
  760. bss->rsc[id][4] = rsc[1];
  761. bss->rsc[id][5] = rsc[0];
  762. wpa_hexdump(MSG_DEBUG, "RSC", bss->rsc[id], 6);
  763. } else {
  764. add_note(wt, MSG_INFO, "Unexpected WPA Key Data length "
  765. "in Group Key msg 1/2 from " MACSTR,
  766. MAC2STR(src));
  767. }
  768. }
  769. os_free(decrypted);
  770. }
  771. static void rx_data_eapol_key_2_of_2(struct wlantest *wt, const u8 *dst,
  772. const u8 *src, const u8 *data, size_t len)
  773. {
  774. struct wlantest_bss *bss;
  775. struct wlantest_sta *sta;
  776. const struct ieee802_1x_hdr *eapol;
  777. const struct wpa_eapol_key *hdr;
  778. u16 key_info;
  779. wpa_printf(MSG_DEBUG, "EAPOL-Key 2/2 " MACSTR " -> " MACSTR,
  780. MAC2STR(src), MAC2STR(dst));
  781. bss = bss_get(wt, dst);
  782. if (bss == NULL)
  783. return;
  784. sta = sta_get(bss, src);
  785. if (sta == NULL)
  786. return;
  787. eapol = (const struct ieee802_1x_hdr *) data;
  788. hdr = (const struct wpa_eapol_key *) (eapol + 1);
  789. if (!is_zero(hdr->key_rsc, 8)) {
  790. add_note(wt, MSG_INFO, "EAPOL-Key 2/2 from " MACSTR " used "
  791. "non-zero Key RSC", MAC2STR(src));
  792. }
  793. key_info = WPA_GET_BE16(hdr->key_info);
  794. if (!sta->ptk_set) {
  795. add_note(wt, MSG_DEBUG,
  796. "No PTK known to process EAPOL-Key 2/2");
  797. return;
  798. }
  799. if (sta->ptk_set &&
  800. check_mic(sta->ptk.kck, sta->ptk.kck_len, sta->key_mgmt,
  801. key_info & WPA_KEY_INFO_TYPE_MASK,
  802. data, len) < 0) {
  803. add_note(wt, MSG_INFO, "Mismatch in EAPOL-Key 2/2 MIC");
  804. return;
  805. }
  806. add_note(wt, MSG_DEBUG, "Valid MIC found in EAPOL-Key 2/2");
  807. }
  808. static void rx_data_eapol_key(struct wlantest *wt, const u8 *dst,
  809. const u8 *src, const u8 *data, size_t len,
  810. int prot)
  811. {
  812. const struct ieee802_1x_hdr *eapol;
  813. const struct wpa_eapol_key *hdr;
  814. const u8 *key_data;
  815. u16 key_info, key_length, ver, key_data_length;
  816. eapol = (const struct ieee802_1x_hdr *) data;
  817. hdr = (const struct wpa_eapol_key *) (eapol + 1);
  818. wpa_hexdump(MSG_MSGDUMP, "EAPOL-Key",
  819. (const u8 *) hdr, len - sizeof(*eapol));
  820. if (len < sizeof(*hdr)) {
  821. add_note(wt, MSG_INFO, "Too short EAPOL-Key frame from " MACSTR,
  822. MAC2STR(src));
  823. return;
  824. }
  825. if (hdr->type == EAPOL_KEY_TYPE_RC4) {
  826. /* TODO: EAPOL-Key RC4 for WEP */
  827. wpa_printf(MSG_INFO, "EAPOL-Key Descriptor Type RC4 from "
  828. MACSTR, MAC2STR(src));
  829. return;
  830. }
  831. if (hdr->type != EAPOL_KEY_TYPE_RSN &&
  832. hdr->type != EAPOL_KEY_TYPE_WPA) {
  833. wpa_printf(MSG_INFO, "Unsupported EAPOL-Key Descriptor Type "
  834. "%u from " MACSTR, hdr->type, MAC2STR(src));
  835. return;
  836. }
  837. key_info = WPA_GET_BE16(hdr->key_info);
  838. key_length = WPA_GET_BE16(hdr->key_length);
  839. key_data_length = WPA_GET_BE16(hdr->key_data_length);
  840. key_data = (const u8 *) (hdr + 1);
  841. if (key_data + key_data_length > data + len) {
  842. add_note(wt, MSG_INFO, "Truncated EAPOL-Key from " MACSTR,
  843. MAC2STR(src));
  844. return;
  845. }
  846. if (key_data + key_data_length < data + len) {
  847. wpa_hexdump(MSG_DEBUG, "Extra data after EAPOL-Key Key Data "
  848. "field", key_data + key_data_length,
  849. data + len - key_data - key_data_length);
  850. }
  851. ver = key_info & WPA_KEY_INFO_TYPE_MASK;
  852. wpa_printf(MSG_DEBUG, "EAPOL-Key ver=%u %c idx=%u%s%s%s%s%s%s%s%s "
  853. "datalen=%u",
  854. ver, key_info & WPA_KEY_INFO_KEY_TYPE ? 'P' : 'G',
  855. (key_info & WPA_KEY_INFO_KEY_INDEX_MASK) >>
  856. WPA_KEY_INFO_KEY_INDEX_SHIFT,
  857. (key_info & WPA_KEY_INFO_INSTALL) ? " Install" : "",
  858. (key_info & WPA_KEY_INFO_ACK) ? " ACK" : "",
  859. (key_info & WPA_KEY_INFO_MIC) ? " MIC" : "",
  860. (key_info & WPA_KEY_INFO_SECURE) ? " Secure" : "",
  861. (key_info & WPA_KEY_INFO_ERROR) ? " Error" : "",
  862. (key_info & WPA_KEY_INFO_REQUEST) ? " Request" : "",
  863. (key_info & WPA_KEY_INFO_ENCR_KEY_DATA) ? " Encr" : "",
  864. (key_info & WPA_KEY_INFO_SMK_MESSAGE) ? " SMK" : "",
  865. key_data_length);
  866. if (ver != WPA_KEY_INFO_TYPE_HMAC_MD5_RC4 &&
  867. ver != WPA_KEY_INFO_TYPE_HMAC_SHA1_AES &&
  868. ver != WPA_KEY_INFO_TYPE_AES_128_CMAC &&
  869. ver != WPA_KEY_INFO_TYPE_AKM_DEFINED) {
  870. wpa_printf(MSG_INFO, "Unsupported EAPOL-Key Key Descriptor "
  871. "Version %u from " MACSTR, ver, MAC2STR(src));
  872. return;
  873. }
  874. wpa_hexdump(MSG_MSGDUMP, "EAPOL-Key Replay Counter",
  875. hdr->replay_counter, WPA_REPLAY_COUNTER_LEN);
  876. wpa_hexdump(MSG_MSGDUMP, "EAPOL-Key Key Nonce",
  877. hdr->key_nonce, WPA_NONCE_LEN);
  878. wpa_hexdump(MSG_MSGDUMP, "EAPOL-Key Key IV",
  879. hdr->key_iv, 16);
  880. wpa_hexdump(MSG_MSGDUMP, "EAPOL-Key RSC",
  881. hdr->key_rsc, WPA_KEY_RSC_LEN);
  882. wpa_hexdump(MSG_MSGDUMP, "EAPOL-Key Key MIC",
  883. hdr->key_mic, 16);
  884. wpa_hexdump(MSG_MSGDUMP, "EAPOL-Key Key Data",
  885. key_data, key_data_length);
  886. if (hdr->type == EAPOL_KEY_TYPE_RSN &&
  887. (key_info & (WPA_KEY_INFO_KEY_INDEX_MASK | BIT(14) | BIT(15))) !=
  888. 0) {
  889. wpa_printf(MSG_INFO, "RSN EAPOL-Key with non-zero reserved "
  890. "Key Info bits 0x%x from " MACSTR,
  891. key_info, MAC2STR(src));
  892. }
  893. if (hdr->type == EAPOL_KEY_TYPE_WPA &&
  894. (key_info & (WPA_KEY_INFO_ENCR_KEY_DATA |
  895. WPA_KEY_INFO_SMK_MESSAGE |BIT(14) | BIT(15))) != 0) {
  896. wpa_printf(MSG_INFO, "WPA EAPOL-Key with non-zero reserved "
  897. "Key Info bits 0x%x from " MACSTR,
  898. key_info, MAC2STR(src));
  899. }
  900. if (key_length > 32) {
  901. wpa_printf(MSG_INFO, "EAPOL-Key with invalid Key Length %d "
  902. "from " MACSTR, key_length, MAC2STR(src));
  903. }
  904. if (ver != WPA_KEY_INFO_TYPE_HMAC_MD5_RC4 &&
  905. !is_zero(hdr->key_iv, 16)) {
  906. wpa_printf(MSG_INFO, "EAPOL-Key with non-zero Key IV "
  907. "(reserved with ver=%d) field from " MACSTR,
  908. ver, MAC2STR(src));
  909. wpa_hexdump(MSG_INFO, "EAPOL-Key Key IV (reserved)",
  910. hdr->key_iv, 16);
  911. }
  912. if (!is_zero(hdr->key_id, 8)) {
  913. wpa_printf(MSG_INFO, "EAPOL-Key with non-zero Key ID "
  914. "(reserved) field from " MACSTR, MAC2STR(src));
  915. wpa_hexdump(MSG_INFO, "EAPOL-Key Key ID (reserved)",
  916. hdr->key_id, 8);
  917. }
  918. if (hdr->key_rsc[6] || hdr->key_rsc[7]) {
  919. wpa_printf(MSG_INFO, "EAPOL-Key with non-zero Key RSC octets "
  920. "(last two are unused)" MACSTR, MAC2STR(src));
  921. }
  922. if (key_info & (WPA_KEY_INFO_ERROR | WPA_KEY_INFO_REQUEST))
  923. return;
  924. if (key_info & WPA_KEY_INFO_SMK_MESSAGE)
  925. return;
  926. if (key_info & WPA_KEY_INFO_KEY_TYPE) {
  927. /* 4-Way Handshake */
  928. switch (key_info & (WPA_KEY_INFO_SECURE |
  929. WPA_KEY_INFO_MIC |
  930. WPA_KEY_INFO_ACK |
  931. WPA_KEY_INFO_INSTALL)) {
  932. case WPA_KEY_INFO_ACK:
  933. rx_data_eapol_key_1_of_4(wt, dst, src, data, len);
  934. break;
  935. case WPA_KEY_INFO_MIC:
  936. if (key_data_length == 0)
  937. rx_data_eapol_key_4_of_4(wt, dst, src, data,
  938. len);
  939. else
  940. rx_data_eapol_key_2_of_4(wt, dst, src, data,
  941. len);
  942. break;
  943. case WPA_KEY_INFO_MIC | WPA_KEY_INFO_ACK |
  944. WPA_KEY_INFO_INSTALL:
  945. /* WPA does not include Secure bit in 3/4 */
  946. rx_data_eapol_key_3_of_4(wt, dst, src, data, len);
  947. break;
  948. case WPA_KEY_INFO_SECURE | WPA_KEY_INFO_MIC |
  949. WPA_KEY_INFO_ACK | WPA_KEY_INFO_INSTALL:
  950. rx_data_eapol_key_3_of_4(wt, dst, src, data, len);
  951. break;
  952. case WPA_KEY_INFO_SECURE | WPA_KEY_INFO_MIC:
  953. if (key_data_length == 0)
  954. rx_data_eapol_key_4_of_4(wt, dst, src, data,
  955. len);
  956. else
  957. rx_data_eapol_key_2_of_4(wt, dst, src, data,
  958. len);
  959. break;
  960. default:
  961. wpa_printf(MSG_DEBUG, "Unsupported EAPOL-Key frame");
  962. break;
  963. }
  964. } else {
  965. /* Group Key Handshake */
  966. switch (key_info & (WPA_KEY_INFO_SECURE |
  967. WPA_KEY_INFO_MIC |
  968. WPA_KEY_INFO_ACK)) {
  969. case WPA_KEY_INFO_SECURE | WPA_KEY_INFO_MIC |
  970. WPA_KEY_INFO_ACK:
  971. rx_data_eapol_key_1_of_2(wt, dst, src, data, len);
  972. break;
  973. case WPA_KEY_INFO_SECURE | WPA_KEY_INFO_MIC:
  974. rx_data_eapol_key_2_of_2(wt, dst, src, data, len);
  975. break;
  976. default:
  977. wpa_printf(MSG_DEBUG, "Unsupported EAPOL-Key frame");
  978. break;
  979. }
  980. }
  981. }
  982. void rx_data_eapol(struct wlantest *wt, const u8 *dst, const u8 *src,
  983. const u8 *data, size_t len, int prot)
  984. {
  985. const struct ieee802_1x_hdr *hdr;
  986. u16 length;
  987. const u8 *p;
  988. wpa_hexdump(MSG_EXCESSIVE, "EAPOL", data, len);
  989. if (len < sizeof(*hdr)) {
  990. wpa_printf(MSG_INFO, "Too short EAPOL frame from " MACSTR,
  991. MAC2STR(src));
  992. return;
  993. }
  994. hdr = (const struct ieee802_1x_hdr *) data;
  995. length = be_to_host16(hdr->length);
  996. wpa_printf(MSG_DEBUG, "RX EAPOL: " MACSTR " -> " MACSTR "%s ver=%u "
  997. "type=%u len=%u",
  998. MAC2STR(src), MAC2STR(dst), prot ? " Prot" : "",
  999. hdr->version, hdr->type, length);
  1000. if (hdr->version < 1 || hdr->version > 3) {
  1001. wpa_printf(MSG_INFO, "Unexpected EAPOL version %u from "
  1002. MACSTR, hdr->version, MAC2STR(src));
  1003. }
  1004. if (sizeof(*hdr) + length > len) {
  1005. wpa_printf(MSG_INFO, "Truncated EAPOL frame from " MACSTR,
  1006. MAC2STR(src));
  1007. return;
  1008. }
  1009. if (sizeof(*hdr) + length < len) {
  1010. wpa_printf(MSG_INFO, "EAPOL frame with %d extra bytes",
  1011. (int) (len - sizeof(*hdr) - length));
  1012. }
  1013. p = (const u8 *) (hdr + 1);
  1014. switch (hdr->type) {
  1015. case IEEE802_1X_TYPE_EAP_PACKET:
  1016. wpa_hexdump(MSG_MSGDUMP, "EAPOL - EAP packet", p, length);
  1017. break;
  1018. case IEEE802_1X_TYPE_EAPOL_START:
  1019. wpa_hexdump(MSG_MSGDUMP, "EAPOL-Start", p, length);
  1020. break;
  1021. case IEEE802_1X_TYPE_EAPOL_LOGOFF:
  1022. wpa_hexdump(MSG_MSGDUMP, "EAPOL-Logoff", p, length);
  1023. break;
  1024. case IEEE802_1X_TYPE_EAPOL_KEY:
  1025. rx_data_eapol_key(wt, dst, src, data, sizeof(*hdr) + length,
  1026. prot);
  1027. break;
  1028. case IEEE802_1X_TYPE_EAPOL_ENCAPSULATED_ASF_ALERT:
  1029. wpa_hexdump(MSG_MSGDUMP, "EAPOL - Encapsulated ASF alert",
  1030. p, length);
  1031. break;
  1032. default:
  1033. wpa_hexdump(MSG_MSGDUMP, "Unknown EAPOL payload", p, length);
  1034. break;
  1035. }
  1036. }