rx_eapol.c 32 KB

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