rx_eapol.c 34 KB

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