rx_eapol.c 33 KB

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