rx_eapol.c 30 KB

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