rx_eapol.c 30 KB

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