wpa.c 66 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401
  1. /*
  2. * WPA Supplicant - WPA state machine and EAPOL-Key processing
  3. * Copyright (c) 2003-2008, Jouni Malinen <j@w1.fi>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. *
  9. * Alternatively, this software may be distributed under the terms of BSD
  10. * license.
  11. *
  12. * See README and COPYING for more details.
  13. */
  14. #include "includes.h"
  15. #include "common.h"
  16. #include "rc4.h"
  17. #include "aes_wrap.h"
  18. #include "wpa.h"
  19. #include "eloop.h"
  20. #include "eapol_supp/eapol_supp_sm.h"
  21. #include "preauth.h"
  22. #include "pmksa_cache.h"
  23. #include "wpa_i.h"
  24. #include "wpa_ie.h"
  25. #include "peerkey.h"
  26. #include "ieee802_11_defs.h"
  27. /**
  28. * wpa_cipher_txt - Convert cipher suite to a text string
  29. * @cipher: Cipher suite (WPA_CIPHER_* enum)
  30. * Returns: Pointer to a text string of the cipher suite name
  31. */
  32. static const char * wpa_cipher_txt(int cipher)
  33. {
  34. switch (cipher) {
  35. case WPA_CIPHER_NONE:
  36. return "NONE";
  37. case WPA_CIPHER_WEP40:
  38. return "WEP-40";
  39. case WPA_CIPHER_WEP104:
  40. return "WEP-104";
  41. case WPA_CIPHER_TKIP:
  42. return "TKIP";
  43. case WPA_CIPHER_CCMP:
  44. return "CCMP";
  45. default:
  46. return "UNKNOWN";
  47. }
  48. }
  49. /**
  50. * wpa_key_mgmt_txt - Convert key management suite to a text string
  51. * @key_mgmt: Key management suite (WPA_KEY_MGMT_* enum)
  52. * @proto: WPA/WPA2 version (WPA_PROTO_*)
  53. * Returns: Pointer to a text string of the key management suite name
  54. */
  55. static const char * wpa_key_mgmt_txt(int key_mgmt, int proto)
  56. {
  57. switch (key_mgmt) {
  58. case WPA_KEY_MGMT_IEEE8021X:
  59. return proto == WPA_PROTO_RSN ?
  60. "WPA2/IEEE 802.1X/EAP" : "WPA/IEEE 802.1X/EAP";
  61. case WPA_KEY_MGMT_PSK:
  62. return proto == WPA_PROTO_RSN ?
  63. "WPA2-PSK" : "WPA-PSK";
  64. case WPA_KEY_MGMT_NONE:
  65. return "NONE";
  66. case WPA_KEY_MGMT_IEEE8021X_NO_WPA:
  67. return "IEEE 802.1X (no WPA)";
  68. #ifdef CONFIG_IEEE80211R
  69. case WPA_KEY_MGMT_FT_IEEE8021X:
  70. return "FT-EAP";
  71. case WPA_KEY_MGMT_FT_PSK:
  72. return "FT-PSK";
  73. #endif /* CONFIG_IEEE80211R */
  74. #ifdef CONFIG_IEEE80211W
  75. case WPA_KEY_MGMT_IEEE8021X_SHA256:
  76. return "WPA2-EAP-SHA256";
  77. case WPA_KEY_MGMT_PSK_SHA256:
  78. return "WPA2-PSK-SHA256";
  79. #endif /* CONFIG_IEEE80211W */
  80. default:
  81. return "UNKNOWN";
  82. }
  83. }
  84. /**
  85. * wpa_eapol_key_send - Send WPA/RSN EAPOL-Key message
  86. * @sm: Pointer to WPA state machine data from wpa_sm_init()
  87. * @kck: Key Confirmation Key (KCK, part of PTK)
  88. * @ver: Version field from Key Info
  89. * @dest: Destination address for the frame
  90. * @proto: Ethertype (usually ETH_P_EAPOL)
  91. * @msg: EAPOL-Key message
  92. * @msg_len: Length of message
  93. * @key_mic: Pointer to the buffer to which the EAPOL-Key MIC is written
  94. */
  95. void wpa_eapol_key_send(struct wpa_sm *sm, const u8 *kck,
  96. int ver, const u8 *dest, u16 proto,
  97. u8 *msg, size_t msg_len, u8 *key_mic)
  98. {
  99. if (is_zero_ether_addr(dest) && is_zero_ether_addr(sm->bssid)) {
  100. /*
  101. * Association event was not yet received; try to fetch
  102. * BSSID from the driver.
  103. */
  104. if (wpa_sm_get_bssid(sm, sm->bssid) < 0) {
  105. wpa_printf(MSG_DEBUG, "WPA: Failed to read BSSID for "
  106. "EAPOL-Key destination address");
  107. } else {
  108. dest = sm->bssid;
  109. wpa_printf(MSG_DEBUG, "WPA: Use BSSID (" MACSTR
  110. ") as the destination for EAPOL-Key",
  111. MAC2STR(dest));
  112. }
  113. }
  114. if (key_mic)
  115. wpa_eapol_key_mic(kck, ver, msg, msg_len, key_mic);
  116. wpa_hexdump(MSG_MSGDUMP, "WPA: TX EAPOL-Key", msg, msg_len);
  117. wpa_sm_ether_send(sm, dest, proto, msg, msg_len);
  118. eapol_sm_notify_tx_eapol_key(sm->eapol);
  119. os_free(msg);
  120. }
  121. /**
  122. * wpa_sm_key_request - Send EAPOL-Key Request
  123. * @sm: Pointer to WPA state machine data from wpa_sm_init()
  124. * @error: Indicate whether this is an Michael MIC error report
  125. * @pairwise: 1 = error report for pairwise packet, 0 = for group packet
  126. *
  127. * Send an EAPOL-Key Request to the current authenticator. This function is
  128. * used to request rekeying and it is usually called when a local Michael MIC
  129. * failure is detected.
  130. */
  131. void wpa_sm_key_request(struct wpa_sm *sm, int error, int pairwise)
  132. {
  133. size_t rlen;
  134. struct wpa_eapol_key *reply;
  135. int key_info, ver;
  136. u8 bssid[ETH_ALEN], *rbuf;
  137. if (wpa_key_mgmt_ft(sm->key_mgmt) || wpa_key_mgmt_sha256(sm->key_mgmt))
  138. ver = WPA_KEY_INFO_TYPE_AES_128_CMAC;
  139. else if (sm->pairwise_cipher == WPA_CIPHER_CCMP)
  140. ver = WPA_KEY_INFO_TYPE_HMAC_SHA1_AES;
  141. else
  142. ver = WPA_KEY_INFO_TYPE_HMAC_MD5_RC4;
  143. if (wpa_sm_get_bssid(sm, bssid) < 0) {
  144. wpa_printf(MSG_WARNING, "Failed to read BSSID for EAPOL-Key "
  145. "request");
  146. return;
  147. }
  148. rbuf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_KEY, NULL,
  149. sizeof(*reply), &rlen, (void *) &reply);
  150. if (rbuf == NULL)
  151. return;
  152. reply->type = sm->proto == WPA_PROTO_RSN ?
  153. EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA;
  154. key_info = WPA_KEY_INFO_REQUEST | ver;
  155. if (sm->ptk_set)
  156. key_info |= WPA_KEY_INFO_MIC;
  157. if (error)
  158. key_info |= WPA_KEY_INFO_ERROR;
  159. if (pairwise)
  160. key_info |= WPA_KEY_INFO_KEY_TYPE;
  161. WPA_PUT_BE16(reply->key_info, key_info);
  162. WPA_PUT_BE16(reply->key_length, 0);
  163. os_memcpy(reply->replay_counter, sm->request_counter,
  164. WPA_REPLAY_COUNTER_LEN);
  165. inc_byte_array(sm->request_counter, WPA_REPLAY_COUNTER_LEN);
  166. WPA_PUT_BE16(reply->key_data_length, 0);
  167. wpa_printf(MSG_INFO, "WPA: Sending EAPOL-Key Request (error=%d "
  168. "pairwise=%d ptk_set=%d len=%lu)",
  169. error, pairwise, sm->ptk_set, (unsigned long) rlen);
  170. wpa_eapol_key_send(sm, sm->ptk.kck, ver, bssid, ETH_P_EAPOL,
  171. rbuf, rlen, key_info & WPA_KEY_INFO_MIC ?
  172. reply->key_mic : NULL);
  173. }
  174. static int wpa_supplicant_get_pmk(struct wpa_sm *sm,
  175. const unsigned char *src_addr,
  176. const u8 *pmkid)
  177. {
  178. int abort_cached = 0;
  179. if (pmkid && !sm->cur_pmksa) {
  180. /* When using drivers that generate RSN IE, wpa_supplicant may
  181. * not have enough time to get the association information
  182. * event before receiving this 1/4 message, so try to find a
  183. * matching PMKSA cache entry here. */
  184. sm->cur_pmksa = pmksa_cache_get(sm->pmksa, src_addr, pmkid);
  185. if (sm->cur_pmksa) {
  186. wpa_printf(MSG_DEBUG, "RSN: found matching PMKID from "
  187. "PMKSA cache");
  188. } else {
  189. wpa_printf(MSG_DEBUG, "RSN: no matching PMKID found");
  190. abort_cached = 1;
  191. }
  192. }
  193. if (pmkid && sm->cur_pmksa &&
  194. os_memcmp(pmkid, sm->cur_pmksa->pmkid, PMKID_LEN) == 0) {
  195. wpa_hexdump(MSG_DEBUG, "RSN: matched PMKID", pmkid, PMKID_LEN);
  196. wpa_sm_set_pmk_from_pmksa(sm);
  197. wpa_hexdump_key(MSG_DEBUG, "RSN: PMK from PMKSA cache",
  198. sm->pmk, sm->pmk_len);
  199. eapol_sm_notify_cached(sm->eapol);
  200. #ifdef CONFIG_IEEE80211R
  201. sm->xxkey_len = 0;
  202. #endif /* CONFIG_IEEE80211R */
  203. } else if (wpa_key_mgmt_wpa_ieee8021x(sm->key_mgmt) && sm->eapol) {
  204. int res, pmk_len;
  205. pmk_len = PMK_LEN;
  206. res = eapol_sm_get_key(sm->eapol, sm->pmk, PMK_LEN);
  207. if (res) {
  208. /*
  209. * EAP-LEAP is an exception from other EAP methods: it
  210. * uses only 16-byte PMK.
  211. */
  212. res = eapol_sm_get_key(sm->eapol, sm->pmk, 16);
  213. pmk_len = 16;
  214. } else {
  215. #ifdef CONFIG_IEEE80211R
  216. u8 buf[2 * PMK_LEN];
  217. if (eapol_sm_get_key(sm->eapol, buf, 2 * PMK_LEN) == 0)
  218. {
  219. os_memcpy(sm->xxkey, buf + PMK_LEN, PMK_LEN);
  220. sm->xxkey_len = PMK_LEN;
  221. os_memset(buf, 0, sizeof(buf));
  222. }
  223. #endif /* CONFIG_IEEE80211R */
  224. }
  225. if (res == 0) {
  226. wpa_hexdump_key(MSG_DEBUG, "WPA: PMK from EAPOL state "
  227. "machines", sm->pmk, pmk_len);
  228. sm->pmk_len = pmk_len;
  229. if (sm->proto == WPA_PROTO_RSN) {
  230. pmksa_cache_add(sm->pmksa, sm->pmk, pmk_len,
  231. src_addr, sm->own_addr,
  232. sm->network_ctx, sm->key_mgmt);
  233. }
  234. if (!sm->cur_pmksa && pmkid &&
  235. pmksa_cache_get(sm->pmksa, src_addr, pmkid)) {
  236. wpa_printf(MSG_DEBUG, "RSN: the new PMK "
  237. "matches with the PMKID");
  238. abort_cached = 0;
  239. }
  240. } else {
  241. wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
  242. "WPA: Failed to get master session key from "
  243. "EAPOL state machines");
  244. wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
  245. "WPA: Key handshake aborted");
  246. if (sm->cur_pmksa) {
  247. wpa_printf(MSG_DEBUG, "RSN: Cancelled PMKSA "
  248. "caching attempt");
  249. sm->cur_pmksa = NULL;
  250. abort_cached = 1;
  251. } else if (!abort_cached) {
  252. return -1;
  253. }
  254. }
  255. }
  256. if (abort_cached && wpa_key_mgmt_wpa_ieee8021x(sm->key_mgmt)) {
  257. /* Send EAPOL-Start to trigger full EAP authentication. */
  258. u8 *buf;
  259. size_t buflen;
  260. wpa_printf(MSG_DEBUG, "RSN: no PMKSA entry found - trigger "
  261. "full EAP authentication");
  262. buf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_START,
  263. NULL, 0, &buflen, NULL);
  264. if (buf) {
  265. wpa_sm_ether_send(sm, sm->bssid, ETH_P_EAPOL,
  266. buf, buflen);
  267. os_free(buf);
  268. }
  269. return -1;
  270. }
  271. return 0;
  272. }
  273. /**
  274. * wpa_supplicant_send_2_of_4 - Send message 2 of WPA/RSN 4-Way Handshake
  275. * @sm: Pointer to WPA state machine data from wpa_sm_init()
  276. * @dst: Destination address for the frame
  277. * @key: Pointer to the EAPOL-Key frame header
  278. * @ver: Version bits from EAPOL-Key Key Info
  279. * @nonce: Nonce value for the EAPOL-Key frame
  280. * @wpa_ie: WPA/RSN IE
  281. * @wpa_ie_len: Length of the WPA/RSN IE
  282. * @ptk: PTK to use for keyed hash and encryption
  283. * Returns: 0 on success, -1 on failure
  284. */
  285. int wpa_supplicant_send_2_of_4(struct wpa_sm *sm, const unsigned char *dst,
  286. const struct wpa_eapol_key *key,
  287. int ver, const u8 *nonce,
  288. const u8 *wpa_ie, size_t wpa_ie_len,
  289. struct wpa_ptk *ptk)
  290. {
  291. size_t rlen;
  292. struct wpa_eapol_key *reply;
  293. u8 *rbuf;
  294. if (wpa_ie == NULL) {
  295. wpa_printf(MSG_WARNING, "WPA: No wpa_ie set - cannot "
  296. "generate msg 2/4");
  297. return -1;
  298. }
  299. wpa_hexdump(MSG_DEBUG, "WPA: WPA IE for msg 2/4", wpa_ie, wpa_ie_len);
  300. rbuf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_KEY,
  301. NULL, sizeof(*reply) + wpa_ie_len,
  302. &rlen, (void *) &reply);
  303. if (rbuf == NULL)
  304. return -1;
  305. reply->type = sm->proto == WPA_PROTO_RSN ?
  306. EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA;
  307. WPA_PUT_BE16(reply->key_info,
  308. ver | WPA_KEY_INFO_KEY_TYPE | WPA_KEY_INFO_MIC);
  309. if (sm->proto == WPA_PROTO_RSN)
  310. WPA_PUT_BE16(reply->key_length, 0);
  311. else
  312. os_memcpy(reply->key_length, key->key_length, 2);
  313. os_memcpy(reply->replay_counter, key->replay_counter,
  314. WPA_REPLAY_COUNTER_LEN);
  315. WPA_PUT_BE16(reply->key_data_length, wpa_ie_len);
  316. os_memcpy(reply + 1, wpa_ie, wpa_ie_len);
  317. os_memcpy(reply->key_nonce, nonce, WPA_NONCE_LEN);
  318. wpa_printf(MSG_DEBUG, "WPA: Sending EAPOL-Key 2/4");
  319. wpa_eapol_key_send(sm, ptk->kck, ver, dst, ETH_P_EAPOL,
  320. rbuf, rlen, reply->key_mic);
  321. return 0;
  322. }
  323. static int wpa_derive_ptk(struct wpa_sm *sm, const unsigned char *src_addr,
  324. const struct wpa_eapol_key *key,
  325. struct wpa_ptk *ptk)
  326. {
  327. size_t ptk_len = sm->pairwise_cipher == WPA_CIPHER_CCMP ? 48 : 64;
  328. #ifdef CONFIG_IEEE80211R
  329. if (wpa_key_mgmt_ft(sm->key_mgmt))
  330. return wpa_derive_ptk_ft(sm, src_addr, key, ptk, ptk_len);
  331. #endif /* CONFIG_IEEE80211R */
  332. wpa_pmk_to_ptk(sm->pmk, sm->pmk_len, "Pairwise key expansion",
  333. sm->own_addr, sm->bssid, sm->snonce, key->key_nonce,
  334. (u8 *) ptk, ptk_len,
  335. wpa_key_mgmt_sha256(sm->key_mgmt));
  336. return 0;
  337. }
  338. static void wpa_supplicant_process_1_of_4(struct wpa_sm *sm,
  339. const unsigned char *src_addr,
  340. const struct wpa_eapol_key *key,
  341. u16 ver)
  342. {
  343. struct wpa_eapol_ie_parse ie;
  344. struct wpa_ptk *ptk;
  345. u8 buf[8];
  346. if (wpa_sm_get_network_ctx(sm) == NULL) {
  347. wpa_printf(MSG_WARNING, "WPA: No SSID info found (msg 1 of "
  348. "4).");
  349. return;
  350. }
  351. wpa_sm_set_state(sm, WPA_4WAY_HANDSHAKE);
  352. wpa_printf(MSG_DEBUG, "WPA: RX message 1 of 4-Way Handshake from "
  353. MACSTR " (ver=%d)", MAC2STR(src_addr), ver);
  354. os_memset(&ie, 0, sizeof(ie));
  355. #ifndef CONFIG_NO_WPA2
  356. if (sm->proto == WPA_PROTO_RSN) {
  357. /* RSN: msg 1/4 should contain PMKID for the selected PMK */
  358. const u8 *_buf = (const u8 *) (key + 1);
  359. size_t len = WPA_GET_BE16(key->key_data_length);
  360. wpa_hexdump(MSG_DEBUG, "RSN: msg 1/4 key data", _buf, len);
  361. wpa_supplicant_parse_ies(_buf, len, &ie);
  362. if (ie.pmkid) {
  363. wpa_hexdump(MSG_DEBUG, "RSN: PMKID from "
  364. "Authenticator", ie.pmkid, PMKID_LEN);
  365. }
  366. }
  367. #endif /* CONFIG_NO_WPA2 */
  368. if (wpa_supplicant_get_pmk(sm, src_addr, ie.pmkid))
  369. return;
  370. if (sm->renew_snonce) {
  371. if (os_get_random(sm->snonce, WPA_NONCE_LEN)) {
  372. wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
  373. "WPA: Failed to get random data for SNonce");
  374. return;
  375. }
  376. sm->renew_snonce = 0;
  377. wpa_hexdump(MSG_DEBUG, "WPA: Renewed SNonce",
  378. sm->snonce, WPA_NONCE_LEN);
  379. }
  380. /* Calculate PTK which will be stored as a temporary PTK until it has
  381. * been verified when processing message 3/4. */
  382. ptk = &sm->tptk;
  383. wpa_derive_ptk(sm, src_addr, key, ptk);
  384. /* Supplicant: swap tx/rx Mic keys */
  385. os_memcpy(buf, ptk->u.auth.tx_mic_key, 8);
  386. os_memcpy(ptk->u.auth.tx_mic_key, ptk->u.auth.rx_mic_key, 8);
  387. os_memcpy(ptk->u.auth.rx_mic_key, buf, 8);
  388. sm->tptk_set = 1;
  389. if (wpa_supplicant_send_2_of_4(sm, sm->bssid, key, ver, sm->snonce,
  390. sm->assoc_wpa_ie, sm->assoc_wpa_ie_len,
  391. ptk))
  392. return;
  393. os_memcpy(sm->anonce, key->key_nonce, WPA_NONCE_LEN);
  394. }
  395. static void wpa_sm_start_preauth(void *eloop_ctx, void *timeout_ctx)
  396. {
  397. struct wpa_sm *sm = eloop_ctx;
  398. rsn_preauth_candidate_process(sm);
  399. }
  400. static void wpa_supplicant_key_neg_complete(struct wpa_sm *sm,
  401. const u8 *addr, int secure)
  402. {
  403. wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
  404. "WPA: Key negotiation completed with "
  405. MACSTR " [PTK=%s GTK=%s]", MAC2STR(addr),
  406. wpa_cipher_txt(sm->pairwise_cipher),
  407. wpa_cipher_txt(sm->group_cipher));
  408. wpa_sm_cancel_auth_timeout(sm);
  409. wpa_sm_set_state(sm, WPA_COMPLETED);
  410. if (secure) {
  411. wpa_sm_mlme_setprotection(
  412. sm, addr, MLME_SETPROTECTION_PROTECT_TYPE_RX_TX,
  413. MLME_SETPROTECTION_KEY_TYPE_PAIRWISE);
  414. eapol_sm_notify_portValid(sm->eapol, TRUE);
  415. if (wpa_key_mgmt_wpa_psk(sm->key_mgmt))
  416. eapol_sm_notify_eap_success(sm->eapol, TRUE);
  417. /*
  418. * Start preauthentication after a short wait to avoid a
  419. * possible race condition between the data receive and key
  420. * configuration after the 4-Way Handshake. This increases the
  421. * likelyhood of the first preauth EAPOL-Start frame getting to
  422. * the target AP.
  423. */
  424. eloop_register_timeout(1, 0, wpa_sm_start_preauth, sm, NULL);
  425. }
  426. if (sm->cur_pmksa && sm->cur_pmksa->opportunistic) {
  427. wpa_printf(MSG_DEBUG, "RSN: Authenticator accepted "
  428. "opportunistic PMKSA entry - marking it valid");
  429. sm->cur_pmksa->opportunistic = 0;
  430. }
  431. #ifdef CONFIG_IEEE80211R
  432. if (wpa_key_mgmt_ft(sm->key_mgmt)) {
  433. /* Prepare for the next transition */
  434. wpa_ft_prepare_auth_request(sm);
  435. }
  436. #endif /* CONFIG_IEEE80211R */
  437. }
  438. static void wpa_sm_rekey_ptk(void *eloop_ctx, void *timeout_ctx)
  439. {
  440. struct wpa_sm *sm = eloop_ctx;
  441. wpa_printf(MSG_DEBUG, "WPA: Request PTK rekeying");
  442. wpa_sm_key_request(sm, 0, 1);
  443. }
  444. static int wpa_supplicant_install_ptk(struct wpa_sm *sm,
  445. const struct wpa_eapol_key *key)
  446. {
  447. int keylen, rsclen;
  448. wpa_alg alg;
  449. const u8 *key_rsc;
  450. u8 null_rsc[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
  451. wpa_printf(MSG_DEBUG, "WPA: Installing PTK to the driver.");
  452. switch (sm->pairwise_cipher) {
  453. case WPA_CIPHER_CCMP:
  454. alg = WPA_ALG_CCMP;
  455. keylen = 16;
  456. rsclen = 6;
  457. break;
  458. case WPA_CIPHER_TKIP:
  459. alg = WPA_ALG_TKIP;
  460. keylen = 32;
  461. rsclen = 6;
  462. break;
  463. case WPA_CIPHER_NONE:
  464. wpa_printf(MSG_DEBUG, "WPA: Pairwise Cipher Suite: "
  465. "NONE - do not use pairwise keys");
  466. return 0;
  467. default:
  468. wpa_printf(MSG_WARNING, "WPA: Unsupported pairwise cipher %d",
  469. sm->pairwise_cipher);
  470. return -1;
  471. }
  472. if (sm->proto == WPA_PROTO_RSN) {
  473. key_rsc = null_rsc;
  474. } else {
  475. key_rsc = key->key_rsc;
  476. wpa_hexdump(MSG_DEBUG, "WPA: RSC", key_rsc, rsclen);
  477. }
  478. if (wpa_sm_set_key(sm, alg, sm->bssid, 0, 1, key_rsc, rsclen,
  479. (u8 *) sm->ptk.tk1, keylen) < 0) {
  480. wpa_printf(MSG_WARNING, "WPA: Failed to set PTK to the "
  481. "driver.");
  482. return -1;
  483. }
  484. if (sm->wpa_ptk_rekey) {
  485. eloop_cancel_timeout(wpa_sm_rekey_ptk, sm, NULL);
  486. eloop_register_timeout(sm->wpa_ptk_rekey, 0, wpa_sm_rekey_ptk,
  487. sm, NULL);
  488. }
  489. return 0;
  490. }
  491. static int wpa_supplicant_check_group_cipher(int group_cipher,
  492. int keylen, int maxkeylen,
  493. int *key_rsc_len, wpa_alg *alg)
  494. {
  495. int ret = 0;
  496. switch (group_cipher) {
  497. case WPA_CIPHER_CCMP:
  498. if (keylen != 16 || maxkeylen < 16) {
  499. ret = -1;
  500. break;
  501. }
  502. *key_rsc_len = 6;
  503. *alg = WPA_ALG_CCMP;
  504. break;
  505. case WPA_CIPHER_TKIP:
  506. if (keylen != 32 || maxkeylen < 32) {
  507. ret = -1;
  508. break;
  509. }
  510. *key_rsc_len = 6;
  511. *alg = WPA_ALG_TKIP;
  512. break;
  513. case WPA_CIPHER_WEP104:
  514. if (keylen != 13 || maxkeylen < 13) {
  515. ret = -1;
  516. break;
  517. }
  518. *key_rsc_len = 0;
  519. *alg = WPA_ALG_WEP;
  520. break;
  521. case WPA_CIPHER_WEP40:
  522. if (keylen != 5 || maxkeylen < 5) {
  523. ret = -1;
  524. break;
  525. }
  526. *key_rsc_len = 0;
  527. *alg = WPA_ALG_WEP;
  528. break;
  529. default:
  530. wpa_printf(MSG_WARNING, "WPA: Unsupported Group Cipher %d",
  531. group_cipher);
  532. return -1;
  533. }
  534. if (ret < 0 ) {
  535. wpa_printf(MSG_WARNING, "WPA: Unsupported %s Group Cipher key "
  536. "length %d (%d).",
  537. wpa_cipher_txt(group_cipher), keylen, maxkeylen);
  538. }
  539. return ret;
  540. }
  541. struct wpa_gtk_data {
  542. wpa_alg alg;
  543. int tx, key_rsc_len, keyidx;
  544. u8 gtk[32];
  545. int gtk_len;
  546. };
  547. static int wpa_supplicant_install_gtk(struct wpa_sm *sm,
  548. const struct wpa_gtk_data *gd,
  549. const u8 *key_rsc)
  550. {
  551. const u8 *_gtk = gd->gtk;
  552. u8 gtk_buf[32];
  553. wpa_hexdump_key(MSG_DEBUG, "WPA: Group Key", gd->gtk, gd->gtk_len);
  554. wpa_printf(MSG_DEBUG, "WPA: Installing GTK to the driver "
  555. "(keyidx=%d tx=%d len=%d).", gd->keyidx, gd->tx,
  556. gd->gtk_len);
  557. wpa_hexdump(MSG_DEBUG, "WPA: RSC", key_rsc, gd->key_rsc_len);
  558. if (sm->group_cipher == WPA_CIPHER_TKIP) {
  559. /* Swap Tx/Rx keys for Michael MIC */
  560. os_memcpy(gtk_buf, gd->gtk, 16);
  561. os_memcpy(gtk_buf + 16, gd->gtk + 24, 8);
  562. os_memcpy(gtk_buf + 24, gd->gtk + 16, 8);
  563. _gtk = gtk_buf;
  564. }
  565. if (sm->pairwise_cipher == WPA_CIPHER_NONE) {
  566. if (wpa_sm_set_key(sm, gd->alg,
  567. (u8 *) "\xff\xff\xff\xff\xff\xff",
  568. gd->keyidx, 1, key_rsc, gd->key_rsc_len,
  569. _gtk, gd->gtk_len) < 0) {
  570. wpa_printf(MSG_WARNING, "WPA: Failed to set "
  571. "GTK to the driver (Group only).");
  572. return -1;
  573. }
  574. } else if (wpa_sm_set_key(sm, gd->alg,
  575. (u8 *) "\xff\xff\xff\xff\xff\xff",
  576. gd->keyidx, gd->tx, key_rsc, gd->key_rsc_len,
  577. _gtk, gd->gtk_len) < 0) {
  578. wpa_printf(MSG_WARNING, "WPA: Failed to set GTK to "
  579. "the driver.");
  580. return -1;
  581. }
  582. return 0;
  583. }
  584. static int wpa_supplicant_gtk_tx_bit_workaround(const struct wpa_sm *sm,
  585. int tx)
  586. {
  587. if (tx && sm->pairwise_cipher != WPA_CIPHER_NONE) {
  588. /* Ignore Tx bit for GTK if a pairwise key is used. One AP
  589. * seemed to set this bit (incorrectly, since Tx is only when
  590. * doing Group Key only APs) and without this workaround, the
  591. * data connection does not work because wpa_supplicant
  592. * configured non-zero keyidx to be used for unicast. */
  593. wpa_printf(MSG_INFO, "WPA: Tx bit set for GTK, but pairwise "
  594. "keys are used - ignore Tx bit");
  595. return 0;
  596. }
  597. return tx;
  598. }
  599. static int wpa_supplicant_pairwise_gtk(struct wpa_sm *sm,
  600. const struct wpa_eapol_key *key,
  601. const u8 *gtk, size_t gtk_len,
  602. int key_info)
  603. {
  604. #ifndef CONFIG_NO_WPA2
  605. struct wpa_gtk_data gd;
  606. /*
  607. * IEEE Std 802.11i-2004 - 8.5.2 EAPOL-Key frames - Figure 43x
  608. * GTK KDE format:
  609. * KeyID[bits 0-1], Tx [bit 2], Reserved [bits 3-7]
  610. * Reserved [bits 0-7]
  611. * GTK
  612. */
  613. os_memset(&gd, 0, sizeof(gd));
  614. wpa_hexdump_key(MSG_DEBUG, "RSN: received GTK in pairwise handshake",
  615. gtk, gtk_len);
  616. if (gtk_len < 2 || gtk_len - 2 > sizeof(gd.gtk))
  617. return -1;
  618. gd.keyidx = gtk[0] & 0x3;
  619. gd.tx = wpa_supplicant_gtk_tx_bit_workaround(sm,
  620. !!(gtk[0] & BIT(2)));
  621. gtk += 2;
  622. gtk_len -= 2;
  623. os_memcpy(gd.gtk, gtk, gtk_len);
  624. gd.gtk_len = gtk_len;
  625. if (wpa_supplicant_check_group_cipher(sm->group_cipher,
  626. gtk_len, gtk_len,
  627. &gd.key_rsc_len, &gd.alg) ||
  628. wpa_supplicant_install_gtk(sm, &gd, key->key_rsc)) {
  629. wpa_printf(MSG_DEBUG, "RSN: Failed to install GTK");
  630. return -1;
  631. }
  632. wpa_supplicant_key_neg_complete(sm, sm->bssid,
  633. key_info & WPA_KEY_INFO_SECURE);
  634. return 0;
  635. #else /* CONFIG_NO_WPA2 */
  636. return -1;
  637. #endif /* CONFIG_NO_WPA2 */
  638. }
  639. static int ieee80211w_set_keys(struct wpa_sm *sm,
  640. struct wpa_eapol_ie_parse *ie)
  641. {
  642. #ifdef CONFIG_IEEE80211W
  643. if (sm->mgmt_group_cipher != WPA_CIPHER_AES_128_CMAC)
  644. return 0;
  645. if (ie->igtk) {
  646. const struct wpa_igtk_kde *igtk;
  647. u16 keyidx;
  648. if (ie->igtk_len != sizeof(*igtk))
  649. return -1;
  650. igtk = (const struct wpa_igtk_kde *) ie->igtk;
  651. keyidx = WPA_GET_LE16(igtk->keyid);
  652. wpa_printf(MSG_DEBUG, "WPA: IGTK keyid %d "
  653. "pn %02x%02x%02x%02x%02x%02x",
  654. keyidx, MAC2STR(igtk->pn));
  655. wpa_hexdump_key(MSG_DEBUG, "WPA: IGTK",
  656. igtk->igtk, WPA_IGTK_LEN);
  657. if (keyidx > 4095) {
  658. wpa_printf(MSG_WARNING, "WPA: Invalid IGTK KeyID %d",
  659. keyidx);
  660. return -1;
  661. }
  662. if (wpa_sm_set_key(sm, WPA_ALG_IGTK,
  663. (u8 *) "\xff\xff\xff\xff\xff\xff",
  664. keyidx, 0, igtk->pn, sizeof(igtk->pn),
  665. igtk->igtk, WPA_IGTK_LEN) < 0) {
  666. wpa_printf(MSG_WARNING, "WPA: Failed to configure IGTK"
  667. " to the driver");
  668. return -1;
  669. }
  670. }
  671. return 0;
  672. #else /* CONFIG_IEEE80211W */
  673. return 0;
  674. #endif /* CONFIG_IEEE80211W */
  675. }
  676. static void wpa_report_ie_mismatch(struct wpa_sm *sm,
  677. const char *reason, const u8 *src_addr,
  678. const u8 *wpa_ie, size_t wpa_ie_len,
  679. const u8 *rsn_ie, size_t rsn_ie_len)
  680. {
  681. wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, "WPA: %s (src=" MACSTR ")",
  682. reason, MAC2STR(src_addr));
  683. if (sm->ap_wpa_ie) {
  684. wpa_hexdump(MSG_INFO, "WPA: WPA IE in Beacon/ProbeResp",
  685. sm->ap_wpa_ie, sm->ap_wpa_ie_len);
  686. }
  687. if (wpa_ie) {
  688. if (!sm->ap_wpa_ie) {
  689. wpa_printf(MSG_INFO, "WPA: No WPA IE in "
  690. "Beacon/ProbeResp");
  691. }
  692. wpa_hexdump(MSG_INFO, "WPA: WPA IE in 3/4 msg",
  693. wpa_ie, wpa_ie_len);
  694. }
  695. if (sm->ap_rsn_ie) {
  696. wpa_hexdump(MSG_INFO, "WPA: RSN IE in Beacon/ProbeResp",
  697. sm->ap_rsn_ie, sm->ap_rsn_ie_len);
  698. }
  699. if (rsn_ie) {
  700. if (!sm->ap_rsn_ie) {
  701. wpa_printf(MSG_INFO, "WPA: No RSN IE in "
  702. "Beacon/ProbeResp");
  703. }
  704. wpa_hexdump(MSG_INFO, "WPA: RSN IE in 3/4 msg",
  705. rsn_ie, rsn_ie_len);
  706. }
  707. wpa_sm_disassociate(sm, WLAN_REASON_IE_IN_4WAY_DIFFERS);
  708. }
  709. static int wpa_supplicant_validate_ie(struct wpa_sm *sm,
  710. const unsigned char *src_addr,
  711. struct wpa_eapol_ie_parse *ie)
  712. {
  713. if (sm->ap_wpa_ie == NULL && sm->ap_rsn_ie == NULL) {
  714. wpa_printf(MSG_DEBUG, "WPA: No WPA/RSN IE for this AP known. "
  715. "Trying to get from scan results");
  716. if (wpa_sm_get_beacon_ie(sm) < 0) {
  717. wpa_printf(MSG_WARNING, "WPA: Could not find AP from "
  718. "the scan results");
  719. } else {
  720. wpa_printf(MSG_DEBUG, "WPA: Found the current AP from "
  721. "updated scan results");
  722. }
  723. }
  724. if (ie->wpa_ie == NULL && ie->rsn_ie == NULL &&
  725. (sm->ap_wpa_ie || sm->ap_rsn_ie)) {
  726. wpa_report_ie_mismatch(sm, "IE in 3/4 msg does not match "
  727. "with IE in Beacon/ProbeResp (no IE?)",
  728. src_addr, ie->wpa_ie, ie->wpa_ie_len,
  729. ie->rsn_ie, ie->rsn_ie_len);
  730. return -1;
  731. }
  732. if ((ie->wpa_ie && sm->ap_wpa_ie &&
  733. (ie->wpa_ie_len != sm->ap_wpa_ie_len ||
  734. os_memcmp(ie->wpa_ie, sm->ap_wpa_ie, ie->wpa_ie_len) != 0)) ||
  735. (ie->rsn_ie && sm->ap_rsn_ie &&
  736. (ie->rsn_ie_len != sm->ap_rsn_ie_len ||
  737. os_memcmp(ie->rsn_ie, sm->ap_rsn_ie, ie->rsn_ie_len) != 0))) {
  738. wpa_report_ie_mismatch(sm, "IE in 3/4 msg does not match "
  739. "with IE in Beacon/ProbeResp",
  740. src_addr, ie->wpa_ie, ie->wpa_ie_len,
  741. ie->rsn_ie, ie->rsn_ie_len);
  742. return -1;
  743. }
  744. if (sm->proto == WPA_PROTO_WPA &&
  745. ie->rsn_ie && sm->ap_rsn_ie == NULL && sm->rsn_enabled) {
  746. wpa_report_ie_mismatch(sm, "Possible downgrade attack "
  747. "detected - RSN was enabled and RSN IE "
  748. "was in msg 3/4, but not in "
  749. "Beacon/ProbeResp",
  750. src_addr, ie->wpa_ie, ie->wpa_ie_len,
  751. ie->rsn_ie, ie->rsn_ie_len);
  752. return -1;
  753. }
  754. #ifdef CONFIG_IEEE80211R
  755. if (wpa_key_mgmt_ft(sm->key_mgmt)) {
  756. struct rsn_mdie *mdie;
  757. /* TODO: verify that full MDIE matches with the one from scan
  758. * results, not only mobility domain */
  759. mdie = (struct rsn_mdie *) (ie->mdie + 2);
  760. if (ie->mdie == NULL || ie->mdie_len < 2 + sizeof(*mdie) ||
  761. os_memcmp(mdie->mobility_domain, sm->mobility_domain,
  762. MOBILITY_DOMAIN_ID_LEN) != 0) {
  763. wpa_printf(MSG_DEBUG, "FT: MDIE in msg 3/4 did not "
  764. "match with the current mobility domain");
  765. return -1;
  766. }
  767. }
  768. #endif /* CONFIG_IEEE80211R */
  769. return 0;
  770. }
  771. /**
  772. * wpa_supplicant_send_4_of_4 - Send message 4 of WPA/RSN 4-Way Handshake
  773. * @sm: Pointer to WPA state machine data from wpa_sm_init()
  774. * @dst: Destination address for the frame
  775. * @key: Pointer to the EAPOL-Key frame header
  776. * @ver: Version bits from EAPOL-Key Key Info
  777. * @key_info: Key Info
  778. * @kde: KDEs to include the EAPOL-Key frame
  779. * @kde_len: Length of KDEs
  780. * @ptk: PTK to use for keyed hash and encryption
  781. * Returns: 0 on success, -1 on failure
  782. */
  783. int wpa_supplicant_send_4_of_4(struct wpa_sm *sm, const unsigned char *dst,
  784. const struct wpa_eapol_key *key,
  785. u16 ver, u16 key_info,
  786. const u8 *kde, size_t kde_len,
  787. struct wpa_ptk *ptk)
  788. {
  789. size_t rlen;
  790. struct wpa_eapol_key *reply;
  791. u8 *rbuf;
  792. if (kde)
  793. wpa_hexdump(MSG_DEBUG, "WPA: KDE for msg 4/4", kde, kde_len);
  794. rbuf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_KEY, NULL,
  795. sizeof(*reply) + kde_len,
  796. &rlen, (void *) &reply);
  797. if (rbuf == NULL)
  798. return -1;
  799. reply->type = sm->proto == WPA_PROTO_RSN ?
  800. EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA;
  801. key_info &= WPA_KEY_INFO_SECURE;
  802. key_info |= ver | WPA_KEY_INFO_KEY_TYPE | WPA_KEY_INFO_MIC;
  803. WPA_PUT_BE16(reply->key_info, key_info);
  804. if (sm->proto == WPA_PROTO_RSN)
  805. WPA_PUT_BE16(reply->key_length, 0);
  806. else
  807. os_memcpy(reply->key_length, key->key_length, 2);
  808. os_memcpy(reply->replay_counter, key->replay_counter,
  809. WPA_REPLAY_COUNTER_LEN);
  810. WPA_PUT_BE16(reply->key_data_length, kde_len);
  811. if (kde)
  812. os_memcpy(reply + 1, kde, kde_len);
  813. wpa_printf(MSG_DEBUG, "WPA: Sending EAPOL-Key 4/4");
  814. wpa_eapol_key_send(sm, ptk->kck, ver, dst, ETH_P_EAPOL,
  815. rbuf, rlen, reply->key_mic);
  816. return 0;
  817. }
  818. static void wpa_supplicant_process_3_of_4(struct wpa_sm *sm,
  819. const struct wpa_eapol_key *key,
  820. u16 ver)
  821. {
  822. u16 key_info, keylen, len;
  823. const u8 *pos;
  824. struct wpa_eapol_ie_parse ie;
  825. wpa_sm_set_state(sm, WPA_4WAY_HANDSHAKE);
  826. wpa_printf(MSG_DEBUG, "WPA: RX message 3 of 4-Way Handshake from "
  827. MACSTR " (ver=%d)", MAC2STR(sm->bssid), ver);
  828. key_info = WPA_GET_BE16(key->key_info);
  829. pos = (const u8 *) (key + 1);
  830. len = WPA_GET_BE16(key->key_data_length);
  831. wpa_hexdump(MSG_DEBUG, "WPA: IE KeyData", pos, len);
  832. wpa_supplicant_parse_ies(pos, len, &ie);
  833. if (ie.gtk && !(key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
  834. wpa_printf(MSG_WARNING, "WPA: GTK IE in unencrypted key data");
  835. return;
  836. }
  837. #ifdef CONFIG_IEEE80211W
  838. if (ie.igtk && !(key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
  839. wpa_printf(MSG_WARNING, "WPA: IGTK KDE in unencrypted key "
  840. "data");
  841. return;
  842. }
  843. if (ie.igtk && ie.igtk_len != sizeof(struct wpa_igtk_kde)) {
  844. wpa_printf(MSG_WARNING, "WPA: Invalid IGTK KDE length %lu",
  845. (unsigned long) ie.igtk_len);
  846. return;
  847. }
  848. #endif /* CONFIG_IEEE80211W */
  849. if (wpa_supplicant_validate_ie(sm, sm->bssid, &ie) < 0)
  850. return;
  851. if (os_memcmp(sm->anonce, key->key_nonce, WPA_NONCE_LEN) != 0) {
  852. wpa_printf(MSG_WARNING, "WPA: ANonce from message 1 of 4-Way "
  853. "Handshake differs from 3 of 4-Way Handshake - drop"
  854. " packet (src=" MACSTR ")", MAC2STR(sm->bssid));
  855. return;
  856. }
  857. keylen = WPA_GET_BE16(key->key_length);
  858. switch (sm->pairwise_cipher) {
  859. case WPA_CIPHER_CCMP:
  860. if (keylen != 16) {
  861. wpa_printf(MSG_WARNING, "WPA: Invalid CCMP key length "
  862. "%d (src=" MACSTR ")",
  863. keylen, MAC2STR(sm->bssid));
  864. return;
  865. }
  866. break;
  867. case WPA_CIPHER_TKIP:
  868. if (keylen != 32) {
  869. wpa_printf(MSG_WARNING, "WPA: Invalid TKIP key length "
  870. "%d (src=" MACSTR ")",
  871. keylen, MAC2STR(sm->bssid));
  872. return;
  873. }
  874. break;
  875. }
  876. if (wpa_supplicant_send_4_of_4(sm, sm->bssid, key, ver, key_info,
  877. NULL, 0, &sm->ptk))
  878. return;
  879. /* SNonce was successfully used in msg 3/4, so mark it to be renewed
  880. * for the next 4-Way Handshake. If msg 3 is received again, the old
  881. * SNonce will still be used to avoid changing PTK. */
  882. sm->renew_snonce = 1;
  883. if (key_info & WPA_KEY_INFO_INSTALL) {
  884. wpa_supplicant_install_ptk(sm, key);
  885. }
  886. if (key_info & WPA_KEY_INFO_SECURE) {
  887. wpa_sm_mlme_setprotection(
  888. sm, sm->bssid, MLME_SETPROTECTION_PROTECT_TYPE_RX,
  889. MLME_SETPROTECTION_KEY_TYPE_PAIRWISE);
  890. eapol_sm_notify_portValid(sm->eapol, TRUE);
  891. }
  892. wpa_sm_set_state(sm, WPA_GROUP_HANDSHAKE);
  893. if (ie.gtk &&
  894. wpa_supplicant_pairwise_gtk(sm, key,
  895. ie.gtk, ie.gtk_len, key_info) < 0) {
  896. wpa_printf(MSG_INFO, "RSN: Failed to configure GTK");
  897. }
  898. if (ieee80211w_set_keys(sm, &ie) < 0)
  899. wpa_printf(MSG_INFO, "RSN: Failed to configure IGTK");
  900. }
  901. static int wpa_supplicant_process_1_of_2_rsn(struct wpa_sm *sm,
  902. const u8 *keydata,
  903. size_t keydatalen,
  904. u16 key_info,
  905. struct wpa_gtk_data *gd)
  906. {
  907. int maxkeylen;
  908. struct wpa_eapol_ie_parse ie;
  909. wpa_hexdump(MSG_DEBUG, "RSN: msg 1/2 key data", keydata, keydatalen);
  910. wpa_supplicant_parse_ies(keydata, keydatalen, &ie);
  911. if (ie.gtk && !(key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
  912. wpa_printf(MSG_WARNING, "WPA: GTK IE in unencrypted key data");
  913. return -1;
  914. }
  915. if (ie.gtk == NULL) {
  916. wpa_printf(MSG_INFO, "WPA: No GTK IE in Group Key msg 1/2");
  917. return -1;
  918. }
  919. maxkeylen = gd->gtk_len = ie.gtk_len - 2;
  920. if (wpa_supplicant_check_group_cipher(sm->group_cipher,
  921. gd->gtk_len, maxkeylen,
  922. &gd->key_rsc_len, &gd->alg))
  923. return -1;
  924. wpa_hexdump(MSG_DEBUG, "RSN: received GTK in group key handshake",
  925. ie.gtk, ie.gtk_len);
  926. gd->keyidx = ie.gtk[0] & 0x3;
  927. gd->tx = wpa_supplicant_gtk_tx_bit_workaround(sm,
  928. !!(ie.gtk[0] & BIT(2)));
  929. if (ie.gtk_len - 2 > sizeof(gd->gtk)) {
  930. wpa_printf(MSG_INFO, "RSN: Too long GTK in GTK IE "
  931. "(len=%lu)", (unsigned long) ie.gtk_len - 2);
  932. return -1;
  933. }
  934. os_memcpy(gd->gtk, ie.gtk + 2, ie.gtk_len - 2);
  935. if (ieee80211w_set_keys(sm, &ie) < 0)
  936. wpa_printf(MSG_INFO, "RSN: Failed to configure IGTK");
  937. return 0;
  938. }
  939. static int wpa_supplicant_process_1_of_2_wpa(struct wpa_sm *sm,
  940. const struct wpa_eapol_key *key,
  941. size_t keydatalen, int key_info,
  942. size_t extra_len, u16 ver,
  943. struct wpa_gtk_data *gd)
  944. {
  945. size_t maxkeylen;
  946. u8 ek[32];
  947. gd->gtk_len = WPA_GET_BE16(key->key_length);
  948. maxkeylen = keydatalen;
  949. if (keydatalen > extra_len) {
  950. wpa_printf(MSG_INFO, "WPA: Truncated EAPOL-Key packet:"
  951. " key_data_length=%lu > extra_len=%lu",
  952. (unsigned long) keydatalen,
  953. (unsigned long) extra_len);
  954. return -1;
  955. }
  956. if (ver == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
  957. if (maxkeylen < 8) {
  958. wpa_printf(MSG_INFO, "WPA: Too short maxkeylen (%lu)",
  959. (unsigned long) maxkeylen);
  960. return -1;
  961. }
  962. maxkeylen -= 8;
  963. }
  964. if (wpa_supplicant_check_group_cipher(sm->group_cipher,
  965. gd->gtk_len, maxkeylen,
  966. &gd->key_rsc_len, &gd->alg))
  967. return -1;
  968. gd->keyidx = (key_info & WPA_KEY_INFO_KEY_INDEX_MASK) >>
  969. WPA_KEY_INFO_KEY_INDEX_SHIFT;
  970. if (ver == WPA_KEY_INFO_TYPE_HMAC_MD5_RC4) {
  971. os_memcpy(ek, key->key_iv, 16);
  972. os_memcpy(ek + 16, sm->ptk.kek, 16);
  973. if (keydatalen > sizeof(gd->gtk)) {
  974. wpa_printf(MSG_WARNING, "WPA: RC4 key data "
  975. "too long (%lu)",
  976. (unsigned long) keydatalen);
  977. return -1;
  978. }
  979. os_memcpy(gd->gtk, key + 1, keydatalen);
  980. rc4_skip(ek, 32, 256, gd->gtk, keydatalen);
  981. } else if (ver == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
  982. if (keydatalen % 8) {
  983. wpa_printf(MSG_WARNING, "WPA: Unsupported AES-WRAP "
  984. "len %lu", (unsigned long) keydatalen);
  985. return -1;
  986. }
  987. if (maxkeylen > sizeof(gd->gtk)) {
  988. wpa_printf(MSG_WARNING, "WPA: AES-WRAP key data "
  989. "too long (keydatalen=%lu maxkeylen=%lu)",
  990. (unsigned long) keydatalen,
  991. (unsigned long) maxkeylen);
  992. return -1;
  993. }
  994. if (aes_unwrap(sm->ptk.kek, maxkeylen / 8,
  995. (const u8 *) (key + 1), gd->gtk)) {
  996. wpa_printf(MSG_WARNING, "WPA: AES unwrap "
  997. "failed - could not decrypt GTK");
  998. return -1;
  999. }
  1000. } else {
  1001. wpa_printf(MSG_WARNING, "WPA: Unsupported key_info type %d",
  1002. ver);
  1003. return -1;
  1004. }
  1005. gd->tx = wpa_supplicant_gtk_tx_bit_workaround(
  1006. sm, !!(key_info & WPA_KEY_INFO_TXRX));
  1007. return 0;
  1008. }
  1009. static int wpa_supplicant_send_2_of_2(struct wpa_sm *sm,
  1010. const struct wpa_eapol_key *key,
  1011. int ver, u16 key_info)
  1012. {
  1013. size_t rlen;
  1014. struct wpa_eapol_key *reply;
  1015. u8 *rbuf;
  1016. rbuf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_KEY, NULL,
  1017. sizeof(*reply), &rlen, (void *) &reply);
  1018. if (rbuf == NULL)
  1019. return -1;
  1020. reply->type = sm->proto == WPA_PROTO_RSN ?
  1021. EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA;
  1022. key_info &= WPA_KEY_INFO_KEY_INDEX_MASK;
  1023. key_info |= ver | WPA_KEY_INFO_MIC | WPA_KEY_INFO_SECURE;
  1024. WPA_PUT_BE16(reply->key_info, key_info);
  1025. if (sm->proto == WPA_PROTO_RSN)
  1026. WPA_PUT_BE16(reply->key_length, 0);
  1027. else
  1028. os_memcpy(reply->key_length, key->key_length, 2);
  1029. os_memcpy(reply->replay_counter, key->replay_counter,
  1030. WPA_REPLAY_COUNTER_LEN);
  1031. WPA_PUT_BE16(reply->key_data_length, 0);
  1032. wpa_printf(MSG_DEBUG, "WPA: Sending EAPOL-Key 2/2");
  1033. wpa_eapol_key_send(sm, sm->ptk.kck, ver, sm->bssid, ETH_P_EAPOL,
  1034. rbuf, rlen, reply->key_mic);
  1035. return 0;
  1036. }
  1037. static void wpa_supplicant_process_1_of_2(struct wpa_sm *sm,
  1038. const unsigned char *src_addr,
  1039. const struct wpa_eapol_key *key,
  1040. int extra_len, u16 ver)
  1041. {
  1042. u16 key_info, keydatalen;
  1043. int rekey, ret;
  1044. struct wpa_gtk_data gd;
  1045. os_memset(&gd, 0, sizeof(gd));
  1046. rekey = wpa_sm_get_state(sm) == WPA_COMPLETED;
  1047. wpa_printf(MSG_DEBUG, "WPA: RX message 1 of Group Key Handshake from "
  1048. MACSTR " (ver=%d)", MAC2STR(src_addr), ver);
  1049. key_info = WPA_GET_BE16(key->key_info);
  1050. keydatalen = WPA_GET_BE16(key->key_data_length);
  1051. if (sm->proto == WPA_PROTO_RSN) {
  1052. ret = wpa_supplicant_process_1_of_2_rsn(sm,
  1053. (const u8 *) (key + 1),
  1054. keydatalen, key_info,
  1055. &gd);
  1056. } else {
  1057. ret = wpa_supplicant_process_1_of_2_wpa(sm, key, keydatalen,
  1058. key_info, extra_len,
  1059. ver, &gd);
  1060. }
  1061. wpa_sm_set_state(sm, WPA_GROUP_HANDSHAKE);
  1062. if (ret)
  1063. return;
  1064. if (wpa_supplicant_install_gtk(sm, &gd, key->key_rsc) ||
  1065. wpa_supplicant_send_2_of_2(sm, key, ver, key_info))
  1066. return;
  1067. if (rekey) {
  1068. wpa_msg(sm->ctx->msg_ctx, MSG_INFO, "WPA: Group rekeying "
  1069. "completed with " MACSTR " [GTK=%s]",
  1070. MAC2STR(sm->bssid), wpa_cipher_txt(sm->group_cipher));
  1071. wpa_sm_cancel_auth_timeout(sm);
  1072. wpa_sm_set_state(sm, WPA_COMPLETED);
  1073. } else {
  1074. wpa_supplicant_key_neg_complete(sm, sm->bssid,
  1075. key_info &
  1076. WPA_KEY_INFO_SECURE);
  1077. }
  1078. }
  1079. static int wpa_supplicant_verify_eapol_key_mic(struct wpa_sm *sm,
  1080. struct wpa_eapol_key *key,
  1081. u16 ver,
  1082. const u8 *buf, size_t len)
  1083. {
  1084. u8 mic[16];
  1085. int ok = 0;
  1086. os_memcpy(mic, key->key_mic, 16);
  1087. if (sm->tptk_set) {
  1088. os_memset(key->key_mic, 0, 16);
  1089. wpa_eapol_key_mic(sm->tptk.kck, ver, buf, len,
  1090. key->key_mic);
  1091. if (os_memcmp(mic, key->key_mic, 16) != 0) {
  1092. wpa_printf(MSG_WARNING, "WPA: Invalid EAPOL-Key MIC "
  1093. "when using TPTK - ignoring TPTK");
  1094. } else {
  1095. ok = 1;
  1096. sm->tptk_set = 0;
  1097. sm->ptk_set = 1;
  1098. os_memcpy(&sm->ptk, &sm->tptk, sizeof(sm->ptk));
  1099. }
  1100. }
  1101. if (!ok && sm->ptk_set) {
  1102. os_memset(key->key_mic, 0, 16);
  1103. wpa_eapol_key_mic(sm->ptk.kck, ver, buf, len,
  1104. key->key_mic);
  1105. if (os_memcmp(mic, key->key_mic, 16) != 0) {
  1106. wpa_printf(MSG_WARNING, "WPA: Invalid EAPOL-Key MIC "
  1107. "- dropping packet");
  1108. return -1;
  1109. }
  1110. ok = 1;
  1111. }
  1112. if (!ok) {
  1113. wpa_printf(MSG_WARNING, "WPA: Could not verify EAPOL-Key MIC "
  1114. "- dropping packet");
  1115. return -1;
  1116. }
  1117. os_memcpy(sm->rx_replay_counter, key->replay_counter,
  1118. WPA_REPLAY_COUNTER_LEN);
  1119. sm->rx_replay_counter_set = 1;
  1120. return 0;
  1121. }
  1122. /* Decrypt RSN EAPOL-Key key data (RC4 or AES-WRAP) */
  1123. static int wpa_supplicant_decrypt_key_data(struct wpa_sm *sm,
  1124. struct wpa_eapol_key *key, u16 ver)
  1125. {
  1126. u16 keydatalen = WPA_GET_BE16(key->key_data_length);
  1127. wpa_hexdump(MSG_DEBUG, "RSN: encrypted key data",
  1128. (u8 *) (key + 1), keydatalen);
  1129. if (!sm->ptk_set) {
  1130. wpa_printf(MSG_WARNING, "WPA: PTK not available, "
  1131. "cannot decrypt EAPOL-Key key data.");
  1132. return -1;
  1133. }
  1134. /* Decrypt key data here so that this operation does not need
  1135. * to be implemented separately for each message type. */
  1136. if (ver == WPA_KEY_INFO_TYPE_HMAC_MD5_RC4) {
  1137. u8 ek[32];
  1138. os_memcpy(ek, key->key_iv, 16);
  1139. os_memcpy(ek + 16, sm->ptk.kek, 16);
  1140. rc4_skip(ek, 32, 256, (u8 *) (key + 1), keydatalen);
  1141. } else if (ver == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES ||
  1142. ver == WPA_KEY_INFO_TYPE_AES_128_CMAC) {
  1143. u8 *buf;
  1144. if (keydatalen % 8) {
  1145. wpa_printf(MSG_WARNING, "WPA: Unsupported "
  1146. "AES-WRAP len %d", keydatalen);
  1147. return -1;
  1148. }
  1149. keydatalen -= 8; /* AES-WRAP adds 8 bytes */
  1150. buf = os_malloc(keydatalen);
  1151. if (buf == NULL) {
  1152. wpa_printf(MSG_WARNING, "WPA: No memory for "
  1153. "AES-UNWRAP buffer");
  1154. return -1;
  1155. }
  1156. if (aes_unwrap(sm->ptk.kek, keydatalen / 8,
  1157. (u8 *) (key + 1), buf)) {
  1158. os_free(buf);
  1159. wpa_printf(MSG_WARNING, "WPA: AES unwrap failed - "
  1160. "could not decrypt EAPOL-Key key data");
  1161. return -1;
  1162. }
  1163. os_memcpy(key + 1, buf, keydatalen);
  1164. os_free(buf);
  1165. WPA_PUT_BE16(key->key_data_length, keydatalen);
  1166. } else {
  1167. wpa_printf(MSG_WARNING, "WPA: Unsupported key_info type %d",
  1168. ver);
  1169. return -1;
  1170. }
  1171. wpa_hexdump_key(MSG_DEBUG, "WPA: decrypted EAPOL-Key key data",
  1172. (u8 *) (key + 1), keydatalen);
  1173. return 0;
  1174. }
  1175. /**
  1176. * wpa_sm_aborted_cached - Notify WPA that PMKSA caching was aborted
  1177. * @sm: Pointer to WPA state machine data from wpa_sm_init()
  1178. */
  1179. void wpa_sm_aborted_cached(struct wpa_sm *sm)
  1180. {
  1181. if (sm && sm->cur_pmksa) {
  1182. wpa_printf(MSG_DEBUG, "RSN: Cancelling PMKSA caching attempt");
  1183. sm->cur_pmksa = NULL;
  1184. }
  1185. }
  1186. static void wpa_eapol_key_dump(const struct wpa_eapol_key *key)
  1187. {
  1188. #ifndef CONFIG_NO_STDOUT_DEBUG
  1189. u16 key_info = WPA_GET_BE16(key->key_info);
  1190. wpa_printf(MSG_DEBUG, " EAPOL-Key type=%d", key->type);
  1191. wpa_printf(MSG_DEBUG, " key_info 0x%x (ver=%d keyidx=%d rsvd=%d %s"
  1192. "%s%s%s%s%s%s%s)",
  1193. key_info, key_info & WPA_KEY_INFO_TYPE_MASK,
  1194. (key_info & WPA_KEY_INFO_KEY_INDEX_MASK) >>
  1195. WPA_KEY_INFO_KEY_INDEX_SHIFT,
  1196. (key_info & (BIT(13) | BIT(14) | BIT(15))) >> 13,
  1197. key_info & WPA_KEY_INFO_KEY_TYPE ? "Pairwise" : "Group",
  1198. key_info & WPA_KEY_INFO_INSTALL ? " Install" : "",
  1199. key_info & WPA_KEY_INFO_ACK ? " Ack" : "",
  1200. key_info & WPA_KEY_INFO_MIC ? " MIC" : "",
  1201. key_info & WPA_KEY_INFO_SECURE ? " Secure" : "",
  1202. key_info & WPA_KEY_INFO_ERROR ? " Error" : "",
  1203. key_info & WPA_KEY_INFO_REQUEST ? " Request" : "",
  1204. key_info & WPA_KEY_INFO_ENCR_KEY_DATA ? " Encr" : "");
  1205. wpa_printf(MSG_DEBUG, " key_length=%u key_data_length=%u",
  1206. WPA_GET_BE16(key->key_length),
  1207. WPA_GET_BE16(key->key_data_length));
  1208. wpa_hexdump(MSG_DEBUG, " replay_counter",
  1209. key->replay_counter, WPA_REPLAY_COUNTER_LEN);
  1210. wpa_hexdump(MSG_DEBUG, " key_nonce", key->key_nonce, WPA_NONCE_LEN);
  1211. wpa_hexdump(MSG_DEBUG, " key_iv", key->key_iv, 16);
  1212. wpa_hexdump(MSG_DEBUG, " key_rsc", key->key_rsc, 8);
  1213. wpa_hexdump(MSG_DEBUG, " key_id (reserved)", key->key_id, 8);
  1214. wpa_hexdump(MSG_DEBUG, " key_mic", key->key_mic, 16);
  1215. #endif /* CONFIG_NO_STDOUT_DEBUG */
  1216. }
  1217. /**
  1218. * wpa_sm_rx_eapol - Process received WPA EAPOL frames
  1219. * @sm: Pointer to WPA state machine data from wpa_sm_init()
  1220. * @src_addr: Source MAC address of the EAPOL packet
  1221. * @buf: Pointer to the beginning of the EAPOL data (EAPOL header)
  1222. * @len: Length of the EAPOL frame
  1223. * Returns: 1 = WPA EAPOL-Key processed, 0 = not a WPA EAPOL-Key, -1 failure
  1224. *
  1225. * This function is called for each received EAPOL frame. Other than EAPOL-Key
  1226. * frames can be skipped if filtering is done elsewhere. wpa_sm_rx_eapol() is
  1227. * only processing WPA and WPA2 EAPOL-Key frames.
  1228. *
  1229. * The received EAPOL-Key packets are validated and valid packets are replied
  1230. * to. In addition, key material (PTK, GTK) is configured at the end of a
  1231. * successful key handshake.
  1232. */
  1233. int wpa_sm_rx_eapol(struct wpa_sm *sm, const u8 *src_addr,
  1234. const u8 *buf, size_t len)
  1235. {
  1236. size_t plen, data_len, extra_len;
  1237. struct ieee802_1x_hdr *hdr;
  1238. struct wpa_eapol_key *key;
  1239. u16 key_info, ver;
  1240. u8 *tmp;
  1241. int ret = -1;
  1242. struct wpa_peerkey *peerkey = NULL;
  1243. #ifdef CONFIG_IEEE80211R
  1244. sm->ft_completed = 0;
  1245. #endif /* CONFIG_IEEE80211R */
  1246. if (len < sizeof(*hdr) + sizeof(*key)) {
  1247. wpa_printf(MSG_DEBUG, "WPA: EAPOL frame too short to be a WPA "
  1248. "EAPOL-Key (len %lu, expecting at least %lu)",
  1249. (unsigned long) len,
  1250. (unsigned long) sizeof(*hdr) + sizeof(*key));
  1251. return 0;
  1252. }
  1253. tmp = os_malloc(len);
  1254. if (tmp == NULL)
  1255. return -1;
  1256. os_memcpy(tmp, buf, len);
  1257. hdr = (struct ieee802_1x_hdr *) tmp;
  1258. key = (struct wpa_eapol_key *) (hdr + 1);
  1259. plen = be_to_host16(hdr->length);
  1260. data_len = plen + sizeof(*hdr);
  1261. wpa_printf(MSG_DEBUG, "IEEE 802.1X RX: version=%d type=%d length=%lu",
  1262. hdr->version, hdr->type, (unsigned long) plen);
  1263. if (hdr->version < EAPOL_VERSION) {
  1264. /* TODO: backwards compatibility */
  1265. }
  1266. if (hdr->type != IEEE802_1X_TYPE_EAPOL_KEY) {
  1267. wpa_printf(MSG_DEBUG, "WPA: EAPOL frame (type %u) discarded, "
  1268. "not a Key frame", hdr->type);
  1269. ret = 0;
  1270. goto out;
  1271. }
  1272. if (plen > len - sizeof(*hdr) || plen < sizeof(*key)) {
  1273. wpa_printf(MSG_DEBUG, "WPA: EAPOL frame payload size %lu "
  1274. "invalid (frame size %lu)",
  1275. (unsigned long) plen, (unsigned long) len);
  1276. ret = 0;
  1277. goto out;
  1278. }
  1279. if (key->type != EAPOL_KEY_TYPE_WPA && key->type != EAPOL_KEY_TYPE_RSN)
  1280. {
  1281. wpa_printf(MSG_DEBUG, "WPA: EAPOL-Key type (%d) unknown, "
  1282. "discarded", key->type);
  1283. ret = 0;
  1284. goto out;
  1285. }
  1286. wpa_eapol_key_dump(key);
  1287. eapol_sm_notify_lower_layer_success(sm->eapol, 0);
  1288. wpa_hexdump(MSG_MSGDUMP, "WPA: RX EAPOL-Key", tmp, len);
  1289. if (data_len < len) {
  1290. wpa_printf(MSG_DEBUG, "WPA: ignoring %lu bytes after the IEEE "
  1291. "802.1X data", (unsigned long) len - data_len);
  1292. }
  1293. key_info = WPA_GET_BE16(key->key_info);
  1294. ver = key_info & WPA_KEY_INFO_TYPE_MASK;
  1295. if (ver != WPA_KEY_INFO_TYPE_HMAC_MD5_RC4 &&
  1296. #if defined(CONFIG_IEEE80211R) || defined(CONFIG_IEEE80211W)
  1297. ver != WPA_KEY_INFO_TYPE_AES_128_CMAC &&
  1298. #endif /* CONFIG_IEEE80211R || CONFIG_IEEE80211W */
  1299. ver != WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
  1300. wpa_printf(MSG_INFO, "WPA: Unsupported EAPOL-Key descriptor "
  1301. "version %d.", ver);
  1302. goto out;
  1303. }
  1304. #ifdef CONFIG_IEEE80211R
  1305. if (wpa_key_mgmt_ft(sm->key_mgmt)) {
  1306. /* IEEE 802.11r uses a new key_info type (AES-128-CMAC). */
  1307. if (ver != WPA_KEY_INFO_TYPE_AES_128_CMAC) {
  1308. wpa_printf(MSG_INFO, "FT: AP did not use "
  1309. "AES-128-CMAC.");
  1310. goto out;
  1311. }
  1312. } else
  1313. #endif /* CONFIG_IEEE80211R */
  1314. #ifdef CONFIG_IEEE80211W
  1315. if (wpa_key_mgmt_sha256(sm->key_mgmt)) {
  1316. if (ver != WPA_KEY_INFO_TYPE_AES_128_CMAC) {
  1317. wpa_printf(MSG_INFO, "WPA: AP did not use the "
  1318. "negotiated AES-128-CMAC.");
  1319. goto out;
  1320. }
  1321. } else
  1322. #endif /* CONFIG_IEEE80211W */
  1323. if (sm->pairwise_cipher == WPA_CIPHER_CCMP &&
  1324. ver != WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
  1325. wpa_printf(MSG_INFO, "WPA: CCMP is used, but EAPOL-Key "
  1326. "descriptor version (%d) is not 2.", ver);
  1327. if (sm->group_cipher != WPA_CIPHER_CCMP &&
  1328. !(key_info & WPA_KEY_INFO_KEY_TYPE)) {
  1329. /* Earlier versions of IEEE 802.11i did not explicitly
  1330. * require version 2 descriptor for all EAPOL-Key
  1331. * packets, so allow group keys to use version 1 if
  1332. * CCMP is not used for them. */
  1333. wpa_printf(MSG_INFO, "WPA: Backwards compatibility: "
  1334. "allow invalid version for non-CCMP group "
  1335. "keys");
  1336. } else
  1337. goto out;
  1338. }
  1339. #ifdef CONFIG_PEERKEY
  1340. for (peerkey = sm->peerkey; peerkey; peerkey = peerkey->next) {
  1341. if (os_memcmp(peerkey->addr, src_addr, ETH_ALEN) == 0)
  1342. break;
  1343. }
  1344. if (!(key_info & WPA_KEY_INFO_SMK_MESSAGE) && peerkey) {
  1345. if (!peerkey->initiator && peerkey->replay_counter_set &&
  1346. os_memcmp(key->replay_counter, peerkey->replay_counter,
  1347. WPA_REPLAY_COUNTER_LEN) <= 0) {
  1348. wpa_printf(MSG_WARNING, "RSN: EAPOL-Key Replay "
  1349. "Counter did not increase (STK) - dropping "
  1350. "packet");
  1351. goto out;
  1352. } else if (peerkey->initiator) {
  1353. u8 _tmp[WPA_REPLAY_COUNTER_LEN];
  1354. os_memcpy(_tmp, key->replay_counter,
  1355. WPA_REPLAY_COUNTER_LEN);
  1356. inc_byte_array(_tmp, WPA_REPLAY_COUNTER_LEN);
  1357. if (os_memcmp(_tmp, peerkey->replay_counter,
  1358. WPA_REPLAY_COUNTER_LEN) != 0) {
  1359. wpa_printf(MSG_DEBUG, "RSN: EAPOL-Key Replay "
  1360. "Counter did not match (STK) - "
  1361. "dropping packet");
  1362. goto out;
  1363. }
  1364. }
  1365. }
  1366. if (peerkey && peerkey->initiator && (key_info & WPA_KEY_INFO_ACK)) {
  1367. wpa_printf(MSG_INFO, "RSN: Ack bit in key_info from STK peer");
  1368. goto out;
  1369. }
  1370. #endif /* CONFIG_PEERKEY */
  1371. if (!peerkey && sm->rx_replay_counter_set &&
  1372. os_memcmp(key->replay_counter, sm->rx_replay_counter,
  1373. WPA_REPLAY_COUNTER_LEN) <= 0) {
  1374. wpa_printf(MSG_WARNING, "WPA: EAPOL-Key Replay Counter did not"
  1375. " increase - dropping packet");
  1376. goto out;
  1377. }
  1378. if (!(key_info & (WPA_KEY_INFO_ACK | WPA_KEY_INFO_SMK_MESSAGE))
  1379. #ifdef CONFIG_PEERKEY
  1380. && (peerkey == NULL || !peerkey->initiator)
  1381. #endif /* CONFIG_PEERKEY */
  1382. ) {
  1383. wpa_printf(MSG_INFO, "WPA: No Ack bit in key_info");
  1384. goto out;
  1385. }
  1386. if (key_info & WPA_KEY_INFO_REQUEST) {
  1387. wpa_printf(MSG_INFO, "WPA: EAPOL-Key with Request bit - "
  1388. "dropped");
  1389. goto out;
  1390. }
  1391. if ((key_info & WPA_KEY_INFO_MIC) && !peerkey &&
  1392. wpa_supplicant_verify_eapol_key_mic(sm, key, ver, tmp, data_len))
  1393. goto out;
  1394. #ifdef CONFIG_PEERKEY
  1395. if ((key_info & WPA_KEY_INFO_MIC) && peerkey &&
  1396. peerkey_verify_eapol_key_mic(sm, peerkey, key, ver, tmp, data_len))
  1397. goto out;
  1398. #endif /* CONFIG_PEERKEY */
  1399. extra_len = data_len - sizeof(*hdr) - sizeof(*key);
  1400. if (WPA_GET_BE16(key->key_data_length) > extra_len) {
  1401. wpa_msg(sm->ctx->msg_ctx, MSG_INFO, "WPA: Invalid EAPOL-Key "
  1402. "frame - key_data overflow (%d > %lu)",
  1403. WPA_GET_BE16(key->key_data_length),
  1404. (unsigned long) extra_len);
  1405. goto out;
  1406. }
  1407. extra_len = WPA_GET_BE16(key->key_data_length);
  1408. if (sm->proto == WPA_PROTO_RSN &&
  1409. (key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
  1410. if (wpa_supplicant_decrypt_key_data(sm, key, ver))
  1411. goto out;
  1412. extra_len = WPA_GET_BE16(key->key_data_length);
  1413. }
  1414. if (key_info & WPA_KEY_INFO_KEY_TYPE) {
  1415. if (key_info & WPA_KEY_INFO_KEY_INDEX_MASK) {
  1416. wpa_printf(MSG_WARNING, "WPA: Ignored EAPOL-Key "
  1417. "(Pairwise) with non-zero key index");
  1418. goto out;
  1419. }
  1420. if (peerkey) {
  1421. /* PeerKey 4-Way Handshake */
  1422. peerkey_rx_eapol_4way(sm, peerkey, key, key_info, ver);
  1423. } else if (key_info & WPA_KEY_INFO_MIC) {
  1424. /* 3/4 4-Way Handshake */
  1425. wpa_supplicant_process_3_of_4(sm, key, ver);
  1426. } else {
  1427. /* 1/4 4-Way Handshake */
  1428. wpa_supplicant_process_1_of_4(sm, src_addr, key,
  1429. ver);
  1430. }
  1431. } else if (key_info & WPA_KEY_INFO_SMK_MESSAGE) {
  1432. /* PeerKey SMK Handshake */
  1433. peerkey_rx_eapol_smk(sm, src_addr, key, extra_len, key_info,
  1434. ver);
  1435. } else {
  1436. if (key_info & WPA_KEY_INFO_MIC) {
  1437. /* 1/2 Group Key Handshake */
  1438. wpa_supplicant_process_1_of_2(sm, src_addr, key,
  1439. extra_len, ver);
  1440. } else {
  1441. wpa_printf(MSG_WARNING, "WPA: EAPOL-Key (Group) "
  1442. "without Mic bit - dropped");
  1443. }
  1444. }
  1445. ret = 1;
  1446. out:
  1447. os_free(tmp);
  1448. return ret;
  1449. }
  1450. #ifdef CONFIG_CTRL_IFACE
  1451. static int wpa_cipher_bits(int cipher)
  1452. {
  1453. switch (cipher) {
  1454. case WPA_CIPHER_CCMP:
  1455. return 128;
  1456. case WPA_CIPHER_TKIP:
  1457. return 256;
  1458. case WPA_CIPHER_WEP104:
  1459. return 104;
  1460. case WPA_CIPHER_WEP40:
  1461. return 40;
  1462. default:
  1463. return 0;
  1464. }
  1465. }
  1466. static u32 wpa_key_mgmt_suite(struct wpa_sm *sm)
  1467. {
  1468. switch (sm->key_mgmt) {
  1469. case WPA_KEY_MGMT_IEEE8021X:
  1470. return (sm->proto == WPA_PROTO_RSN ?
  1471. RSN_AUTH_KEY_MGMT_UNSPEC_802_1X :
  1472. WPA_AUTH_KEY_MGMT_UNSPEC_802_1X);
  1473. case WPA_KEY_MGMT_PSK:
  1474. return (sm->proto == WPA_PROTO_RSN ?
  1475. RSN_AUTH_KEY_MGMT_PSK_OVER_802_1X :
  1476. WPA_AUTH_KEY_MGMT_PSK_OVER_802_1X);
  1477. #ifdef CONFIG_IEEE80211R
  1478. case WPA_KEY_MGMT_FT_IEEE8021X:
  1479. return RSN_AUTH_KEY_MGMT_FT_802_1X;
  1480. case WPA_KEY_MGMT_FT_PSK:
  1481. return RSN_AUTH_KEY_MGMT_FT_PSK;
  1482. #endif /* CONFIG_IEEE80211R */
  1483. #ifdef CONFIG_IEEE80211W
  1484. case WPA_KEY_MGMT_IEEE8021X_SHA256:
  1485. return RSN_AUTH_KEY_MGMT_802_1X_SHA256;
  1486. case WPA_KEY_MGMT_PSK_SHA256:
  1487. return RSN_AUTH_KEY_MGMT_PSK_SHA256;
  1488. #endif /* CONFIG_IEEE80211W */
  1489. case WPA_KEY_MGMT_WPA_NONE:
  1490. return WPA_AUTH_KEY_MGMT_NONE;
  1491. default:
  1492. return 0;
  1493. }
  1494. }
  1495. static u32 wpa_cipher_suite(struct wpa_sm *sm, int cipher)
  1496. {
  1497. switch (cipher) {
  1498. case WPA_CIPHER_CCMP:
  1499. return (sm->proto == WPA_PROTO_RSN ?
  1500. RSN_CIPHER_SUITE_CCMP : WPA_CIPHER_SUITE_CCMP);
  1501. case WPA_CIPHER_TKIP:
  1502. return (sm->proto == WPA_PROTO_RSN ?
  1503. RSN_CIPHER_SUITE_TKIP : WPA_CIPHER_SUITE_TKIP);
  1504. case WPA_CIPHER_WEP104:
  1505. return (sm->proto == WPA_PROTO_RSN ?
  1506. RSN_CIPHER_SUITE_WEP104 : WPA_CIPHER_SUITE_WEP104);
  1507. case WPA_CIPHER_WEP40:
  1508. return (sm->proto == WPA_PROTO_RSN ?
  1509. RSN_CIPHER_SUITE_WEP40 : WPA_CIPHER_SUITE_WEP40);
  1510. case WPA_CIPHER_NONE:
  1511. return (sm->proto == WPA_PROTO_RSN ?
  1512. RSN_CIPHER_SUITE_NONE : WPA_CIPHER_SUITE_NONE);
  1513. default:
  1514. return 0;
  1515. }
  1516. }
  1517. #define RSN_SUITE "%02x-%02x-%02x-%d"
  1518. #define RSN_SUITE_ARG(s) \
  1519. ((s) >> 24) & 0xff, ((s) >> 16) & 0xff, ((s) >> 8) & 0xff, (s) & 0xff
  1520. /**
  1521. * wpa_sm_get_mib - Dump text list of MIB entries
  1522. * @sm: Pointer to WPA state machine data from wpa_sm_init()
  1523. * @buf: Buffer for the list
  1524. * @buflen: Length of the buffer
  1525. * Returns: Number of bytes written to buffer
  1526. *
  1527. * This function is used fetch dot11 MIB variables.
  1528. */
  1529. int wpa_sm_get_mib(struct wpa_sm *sm, char *buf, size_t buflen)
  1530. {
  1531. char pmkid_txt[PMKID_LEN * 2 + 1];
  1532. int rsna, ret;
  1533. size_t len;
  1534. if (sm->cur_pmksa) {
  1535. wpa_snprintf_hex(pmkid_txt, sizeof(pmkid_txt),
  1536. sm->cur_pmksa->pmkid, PMKID_LEN);
  1537. } else
  1538. pmkid_txt[0] = '\0';
  1539. if ((wpa_key_mgmt_wpa_psk(sm->key_mgmt) ||
  1540. wpa_key_mgmt_wpa_ieee8021x(sm->key_mgmt)) &&
  1541. sm->proto == WPA_PROTO_RSN)
  1542. rsna = 1;
  1543. else
  1544. rsna = 0;
  1545. ret = os_snprintf(buf, buflen,
  1546. "dot11RSNAOptionImplemented=TRUE\n"
  1547. "dot11RSNAPreauthenticationImplemented=TRUE\n"
  1548. "dot11RSNAEnabled=%s\n"
  1549. "dot11RSNAPreauthenticationEnabled=%s\n"
  1550. "dot11RSNAConfigVersion=%d\n"
  1551. "dot11RSNAConfigPairwiseKeysSupported=5\n"
  1552. "dot11RSNAConfigGroupCipherSize=%d\n"
  1553. "dot11RSNAConfigPMKLifetime=%d\n"
  1554. "dot11RSNAConfigPMKReauthThreshold=%d\n"
  1555. "dot11RSNAConfigNumberOfPTKSAReplayCounters=1\n"
  1556. "dot11RSNAConfigSATimeout=%d\n",
  1557. rsna ? "TRUE" : "FALSE",
  1558. rsna ? "TRUE" : "FALSE",
  1559. RSN_VERSION,
  1560. wpa_cipher_bits(sm->group_cipher),
  1561. sm->dot11RSNAConfigPMKLifetime,
  1562. sm->dot11RSNAConfigPMKReauthThreshold,
  1563. sm->dot11RSNAConfigSATimeout);
  1564. if (ret < 0 || (size_t) ret >= buflen)
  1565. return 0;
  1566. len = ret;
  1567. ret = os_snprintf(
  1568. buf + len, buflen - len,
  1569. "dot11RSNAAuthenticationSuiteSelected=" RSN_SUITE "\n"
  1570. "dot11RSNAPairwiseCipherSelected=" RSN_SUITE "\n"
  1571. "dot11RSNAGroupCipherSelected=" RSN_SUITE "\n"
  1572. "dot11RSNAPMKIDUsed=%s\n"
  1573. "dot11RSNAAuthenticationSuiteRequested=" RSN_SUITE "\n"
  1574. "dot11RSNAPairwiseCipherRequested=" RSN_SUITE "\n"
  1575. "dot11RSNAGroupCipherRequested=" RSN_SUITE "\n"
  1576. "dot11RSNAConfigNumberOfGTKSAReplayCounters=0\n"
  1577. "dot11RSNA4WayHandshakeFailures=%u\n",
  1578. RSN_SUITE_ARG(wpa_key_mgmt_suite(sm)),
  1579. RSN_SUITE_ARG(wpa_cipher_suite(sm, sm->pairwise_cipher)),
  1580. RSN_SUITE_ARG(wpa_cipher_suite(sm, sm->group_cipher)),
  1581. pmkid_txt,
  1582. RSN_SUITE_ARG(wpa_key_mgmt_suite(sm)),
  1583. RSN_SUITE_ARG(wpa_cipher_suite(sm, sm->pairwise_cipher)),
  1584. RSN_SUITE_ARG(wpa_cipher_suite(sm, sm->group_cipher)),
  1585. sm->dot11RSNA4WayHandshakeFailures);
  1586. if (ret >= 0 && (size_t) ret < buflen)
  1587. len += ret;
  1588. return (int) len;
  1589. }
  1590. #endif /* CONFIG_CTRL_IFACE */
  1591. static void wpa_sm_pmksa_free_cb(struct rsn_pmksa_cache_entry *entry,
  1592. void *ctx, int replace)
  1593. {
  1594. struct wpa_sm *sm = ctx;
  1595. if (sm->cur_pmksa == entry ||
  1596. (sm->pmk_len == entry->pmk_len &&
  1597. os_memcmp(sm->pmk, entry->pmk, sm->pmk_len) == 0)) {
  1598. wpa_printf(MSG_DEBUG, "RSN: removed current PMKSA entry");
  1599. sm->cur_pmksa = NULL;
  1600. if (replace) {
  1601. /* A new entry is being added, so no need to
  1602. * deauthenticate in this case. This happens when EAP
  1603. * authentication is completed again (reauth or failed
  1604. * PMKSA caching attempt). */
  1605. return;
  1606. }
  1607. os_memset(sm->pmk, 0, sizeof(sm->pmk));
  1608. wpa_sm_deauthenticate(sm, WLAN_REASON_UNSPECIFIED);
  1609. }
  1610. }
  1611. /**
  1612. * wpa_sm_init - Initialize WPA state machine
  1613. * @ctx: Context pointer for callbacks; this needs to be an allocated buffer
  1614. * Returns: Pointer to the allocated WPA state machine data
  1615. *
  1616. * This function is used to allocate a new WPA state machine and the returned
  1617. * value is passed to all WPA state machine calls.
  1618. */
  1619. struct wpa_sm * wpa_sm_init(struct wpa_sm_ctx *ctx)
  1620. {
  1621. struct wpa_sm *sm;
  1622. sm = os_zalloc(sizeof(*sm));
  1623. if (sm == NULL)
  1624. return NULL;
  1625. sm->renew_snonce = 1;
  1626. sm->ctx = ctx;
  1627. sm->dot11RSNAConfigPMKLifetime = 43200;
  1628. sm->dot11RSNAConfigPMKReauthThreshold = 70;
  1629. sm->dot11RSNAConfigSATimeout = 60;
  1630. sm->pmksa = pmksa_cache_init(wpa_sm_pmksa_free_cb, sm, sm);
  1631. if (sm->pmksa == NULL) {
  1632. wpa_printf(MSG_ERROR, "RSN: PMKSA cache initialization "
  1633. "failed");
  1634. os_free(sm);
  1635. return NULL;
  1636. }
  1637. return sm;
  1638. }
  1639. /**
  1640. * wpa_sm_deinit - Deinitialize WPA state machine
  1641. * @sm: Pointer to WPA state machine data from wpa_sm_init()
  1642. */
  1643. void wpa_sm_deinit(struct wpa_sm *sm)
  1644. {
  1645. if (sm == NULL)
  1646. return;
  1647. pmksa_cache_deinit(sm->pmksa);
  1648. eloop_cancel_timeout(wpa_sm_start_preauth, sm, NULL);
  1649. eloop_cancel_timeout(wpa_sm_rekey_ptk, sm, NULL);
  1650. os_free(sm->assoc_wpa_ie);
  1651. os_free(sm->ap_wpa_ie);
  1652. os_free(sm->ap_rsn_ie);
  1653. os_free(sm->ctx);
  1654. peerkey_deinit(sm);
  1655. os_free(sm);
  1656. }
  1657. /**
  1658. * wpa_sm_notify_assoc - Notify WPA state machine about association
  1659. * @sm: Pointer to WPA state machine data from wpa_sm_init()
  1660. * @bssid: The BSSID of the new association
  1661. *
  1662. * This function is called to let WPA state machine know that the connection
  1663. * was established.
  1664. */
  1665. void wpa_sm_notify_assoc(struct wpa_sm *sm, const u8 *bssid)
  1666. {
  1667. int clear_ptk = 1;
  1668. if (sm == NULL)
  1669. return;
  1670. wpa_printf(MSG_DEBUG, "WPA: Association event - clear replay counter");
  1671. os_memcpy(sm->bssid, bssid, ETH_ALEN);
  1672. os_memset(sm->rx_replay_counter, 0, WPA_REPLAY_COUNTER_LEN);
  1673. sm->rx_replay_counter_set = 0;
  1674. sm->renew_snonce = 1;
  1675. if (os_memcmp(sm->preauth_bssid, bssid, ETH_ALEN) == 0)
  1676. rsn_preauth_deinit(sm);
  1677. #ifdef CONFIG_IEEE80211R
  1678. if (wpa_ft_is_completed(sm)) {
  1679. wpa_supplicant_key_neg_complete(sm, sm->bssid, 1);
  1680. /* Prepare for the next transition */
  1681. wpa_ft_prepare_auth_request(sm);
  1682. clear_ptk = 0;
  1683. }
  1684. #endif /* CONFIG_IEEE80211R */
  1685. if (clear_ptk) {
  1686. /*
  1687. * IEEE 802.11, 8.4.10: Delete PTK SA on (re)association if
  1688. * this is not part of a Fast BSS Transition.
  1689. */
  1690. wpa_printf(MSG_DEBUG, "WPA: Clear old PTK");
  1691. sm->ptk_set = 0;
  1692. sm->tptk_set = 0;
  1693. }
  1694. }
  1695. /**
  1696. * wpa_sm_notify_disassoc - Notify WPA state machine about disassociation
  1697. * @sm: Pointer to WPA state machine data from wpa_sm_init()
  1698. *
  1699. * This function is called to let WPA state machine know that the connection
  1700. * was lost. This will abort any existing pre-authentication session.
  1701. */
  1702. void wpa_sm_notify_disassoc(struct wpa_sm *sm)
  1703. {
  1704. rsn_preauth_deinit(sm);
  1705. if (wpa_sm_get_state(sm) == WPA_4WAY_HANDSHAKE)
  1706. sm->dot11RSNA4WayHandshakeFailures++;
  1707. }
  1708. /**
  1709. * wpa_sm_set_pmk - Set PMK
  1710. * @sm: Pointer to WPA state machine data from wpa_sm_init()
  1711. * @pmk: The new PMK
  1712. * @pmk_len: The length of the new PMK in bytes
  1713. *
  1714. * Configure the PMK for WPA state machine.
  1715. */
  1716. void wpa_sm_set_pmk(struct wpa_sm *sm, const u8 *pmk, size_t pmk_len)
  1717. {
  1718. if (sm == NULL)
  1719. return;
  1720. sm->pmk_len = pmk_len;
  1721. os_memcpy(sm->pmk, pmk, pmk_len);
  1722. #ifdef CONFIG_IEEE80211R
  1723. /* Set XXKey to be PSK for FT key derivation */
  1724. sm->xxkey_len = pmk_len;
  1725. os_memcpy(sm->xxkey, pmk, pmk_len);
  1726. #endif /* CONFIG_IEEE80211R */
  1727. }
  1728. /**
  1729. * wpa_sm_set_pmk_from_pmksa - Set PMK based on the current PMKSA
  1730. * @sm: Pointer to WPA state machine data from wpa_sm_init()
  1731. *
  1732. * Take the PMK from the current PMKSA into use. If no PMKSA is active, the PMK
  1733. * will be cleared.
  1734. */
  1735. void wpa_sm_set_pmk_from_pmksa(struct wpa_sm *sm)
  1736. {
  1737. if (sm == NULL)
  1738. return;
  1739. if (sm->cur_pmksa) {
  1740. sm->pmk_len = sm->cur_pmksa->pmk_len;
  1741. os_memcpy(sm->pmk, sm->cur_pmksa->pmk, sm->pmk_len);
  1742. } else {
  1743. sm->pmk_len = PMK_LEN;
  1744. os_memset(sm->pmk, 0, PMK_LEN);
  1745. }
  1746. }
  1747. /**
  1748. * wpa_sm_set_fast_reauth - Set fast reauthentication (EAP) enabled/disabled
  1749. * @sm: Pointer to WPA state machine data from wpa_sm_init()
  1750. * @fast_reauth: Whether fast reauthentication (EAP) is allowed
  1751. */
  1752. void wpa_sm_set_fast_reauth(struct wpa_sm *sm, int fast_reauth)
  1753. {
  1754. if (sm)
  1755. sm->fast_reauth = fast_reauth;
  1756. }
  1757. /**
  1758. * wpa_sm_set_scard_ctx - Set context pointer for smartcard callbacks
  1759. * @sm: Pointer to WPA state machine data from wpa_sm_init()
  1760. * @scard_ctx: Context pointer for smartcard related callback functions
  1761. */
  1762. void wpa_sm_set_scard_ctx(struct wpa_sm *sm, void *scard_ctx)
  1763. {
  1764. if (sm == NULL)
  1765. return;
  1766. sm->scard_ctx = scard_ctx;
  1767. if (sm->preauth_eapol)
  1768. eapol_sm_register_scard_ctx(sm->preauth_eapol, scard_ctx);
  1769. }
  1770. /**
  1771. * wpa_sm_set_config - Notification of current configration change
  1772. * @sm: Pointer to WPA state machine data from wpa_sm_init()
  1773. * @config: Pointer to current network configuration
  1774. *
  1775. * Notify WPA state machine that configuration has changed. config will be
  1776. * stored as a backpointer to network configuration. This can be %NULL to clear
  1777. * the stored pointed.
  1778. */
  1779. void wpa_sm_set_config(struct wpa_sm *sm, struct rsn_supp_config *config)
  1780. {
  1781. if (!sm)
  1782. return;
  1783. if (config) {
  1784. sm->network_ctx = config->network_ctx;
  1785. sm->peerkey_enabled = config->peerkey_enabled;
  1786. sm->allowed_pairwise_cipher = config->allowed_pairwise_cipher;
  1787. sm->proactive_key_caching = config->proactive_key_caching;
  1788. sm->eap_workaround = config->eap_workaround;
  1789. sm->eap_conf_ctx = config->eap_conf_ctx;
  1790. if (config->ssid) {
  1791. os_memcpy(sm->ssid, config->ssid, config->ssid_len);
  1792. sm->ssid_len = config->ssid_len;
  1793. } else
  1794. sm->ssid_len = 0;
  1795. sm->wpa_ptk_rekey = config->wpa_ptk_rekey;
  1796. } else {
  1797. sm->network_ctx = NULL;
  1798. sm->peerkey_enabled = 0;
  1799. sm->allowed_pairwise_cipher = 0;
  1800. sm->proactive_key_caching = 0;
  1801. sm->eap_workaround = 0;
  1802. sm->eap_conf_ctx = NULL;
  1803. sm->ssid_len = 0;
  1804. sm->wpa_ptk_rekey = 0;
  1805. }
  1806. if (config == NULL || config->network_ctx != sm->network_ctx)
  1807. pmksa_cache_notify_reconfig(sm->pmksa);
  1808. }
  1809. /**
  1810. * wpa_sm_set_own_addr - Set own MAC address
  1811. * @sm: Pointer to WPA state machine data from wpa_sm_init()
  1812. * @addr: Own MAC address
  1813. */
  1814. void wpa_sm_set_own_addr(struct wpa_sm *sm, const u8 *addr)
  1815. {
  1816. if (sm)
  1817. os_memcpy(sm->own_addr, addr, ETH_ALEN);
  1818. }
  1819. /**
  1820. * wpa_sm_set_ifname - Set network interface name
  1821. * @sm: Pointer to WPA state machine data from wpa_sm_init()
  1822. * @ifname: Interface name
  1823. * @bridge_ifname: Optional bridge interface name (for pre-auth)
  1824. */
  1825. void wpa_sm_set_ifname(struct wpa_sm *sm, const char *ifname,
  1826. const char *bridge_ifname)
  1827. {
  1828. if (sm) {
  1829. sm->ifname = ifname;
  1830. sm->bridge_ifname = bridge_ifname;
  1831. }
  1832. }
  1833. /**
  1834. * wpa_sm_set_eapol - Set EAPOL state machine pointer
  1835. * @sm: Pointer to WPA state machine data from wpa_sm_init()
  1836. * @eapol: Pointer to EAPOL state machine allocated with eapol_sm_init()
  1837. */
  1838. void wpa_sm_set_eapol(struct wpa_sm *sm, struct eapol_sm *eapol)
  1839. {
  1840. if (sm)
  1841. sm->eapol = eapol;
  1842. }
  1843. /**
  1844. * wpa_sm_set_param - Set WPA state machine parameters
  1845. * @sm: Pointer to WPA state machine data from wpa_sm_init()
  1846. * @param: Parameter field
  1847. * @value: Parameter value
  1848. * Returns: 0 on success, -1 on failure
  1849. */
  1850. int wpa_sm_set_param(struct wpa_sm *sm, enum wpa_sm_conf_params param,
  1851. unsigned int value)
  1852. {
  1853. int ret = 0;
  1854. if (sm == NULL)
  1855. return -1;
  1856. switch (param) {
  1857. case RSNA_PMK_LIFETIME:
  1858. if (value > 0)
  1859. sm->dot11RSNAConfigPMKLifetime = value;
  1860. else
  1861. ret = -1;
  1862. break;
  1863. case RSNA_PMK_REAUTH_THRESHOLD:
  1864. if (value > 0 && value <= 100)
  1865. sm->dot11RSNAConfigPMKReauthThreshold = value;
  1866. else
  1867. ret = -1;
  1868. break;
  1869. case RSNA_SA_TIMEOUT:
  1870. if (value > 0)
  1871. sm->dot11RSNAConfigSATimeout = value;
  1872. else
  1873. ret = -1;
  1874. break;
  1875. case WPA_PARAM_PROTO:
  1876. sm->proto = value;
  1877. break;
  1878. case WPA_PARAM_PAIRWISE:
  1879. sm->pairwise_cipher = value;
  1880. break;
  1881. case WPA_PARAM_GROUP:
  1882. sm->group_cipher = value;
  1883. break;
  1884. case WPA_PARAM_KEY_MGMT:
  1885. sm->key_mgmt = value;
  1886. break;
  1887. #ifdef CONFIG_IEEE80211W
  1888. case WPA_PARAM_MGMT_GROUP:
  1889. sm->mgmt_group_cipher = value;
  1890. break;
  1891. #endif /* CONFIG_IEEE80211W */
  1892. case WPA_PARAM_RSN_ENABLED:
  1893. sm->rsn_enabled = value;
  1894. break;
  1895. default:
  1896. break;
  1897. }
  1898. return ret;
  1899. }
  1900. /**
  1901. * wpa_sm_get_param - Get WPA state machine parameters
  1902. * @sm: Pointer to WPA state machine data from wpa_sm_init()
  1903. * @param: Parameter field
  1904. * Returns: Parameter value
  1905. */
  1906. unsigned int wpa_sm_get_param(struct wpa_sm *sm, enum wpa_sm_conf_params param)
  1907. {
  1908. if (sm == NULL)
  1909. return 0;
  1910. switch (param) {
  1911. case RSNA_PMK_LIFETIME:
  1912. return sm->dot11RSNAConfigPMKLifetime;
  1913. case RSNA_PMK_REAUTH_THRESHOLD:
  1914. return sm->dot11RSNAConfigPMKReauthThreshold;
  1915. case RSNA_SA_TIMEOUT:
  1916. return sm->dot11RSNAConfigSATimeout;
  1917. case WPA_PARAM_PROTO:
  1918. return sm->proto;
  1919. case WPA_PARAM_PAIRWISE:
  1920. return sm->pairwise_cipher;
  1921. case WPA_PARAM_GROUP:
  1922. return sm->group_cipher;
  1923. case WPA_PARAM_KEY_MGMT:
  1924. return sm->key_mgmt;
  1925. #ifdef CONFIG_IEEE80211W
  1926. case WPA_PARAM_MGMT_GROUP:
  1927. return sm->mgmt_group_cipher;
  1928. #endif /* CONFIG_IEEE80211W */
  1929. case WPA_PARAM_RSN_ENABLED:
  1930. return sm->rsn_enabled;
  1931. default:
  1932. return 0;
  1933. }
  1934. }
  1935. /**
  1936. * wpa_sm_get_status - Get WPA state machine
  1937. * @sm: Pointer to WPA state machine data from wpa_sm_init()
  1938. * @buf: Buffer for status information
  1939. * @buflen: Maximum buffer length
  1940. * @verbose: Whether to include verbose status information
  1941. * Returns: Number of bytes written to buf.
  1942. *
  1943. * Query WPA state machine for status information. This function fills in
  1944. * a text area with current status information. If the buffer (buf) is not
  1945. * large enough, status information will be truncated to fit the buffer.
  1946. */
  1947. int wpa_sm_get_status(struct wpa_sm *sm, char *buf, size_t buflen,
  1948. int verbose)
  1949. {
  1950. char *pos = buf, *end = buf + buflen;
  1951. int ret;
  1952. ret = os_snprintf(pos, end - pos,
  1953. "pairwise_cipher=%s\n"
  1954. "group_cipher=%s\n"
  1955. "key_mgmt=%s\n",
  1956. wpa_cipher_txt(sm->pairwise_cipher),
  1957. wpa_cipher_txt(sm->group_cipher),
  1958. wpa_key_mgmt_txt(sm->key_mgmt, sm->proto));
  1959. if (ret < 0 || ret >= end - pos)
  1960. return pos - buf;
  1961. pos += ret;
  1962. return pos - buf;
  1963. }
  1964. /**
  1965. * wpa_sm_set_assoc_wpa_ie_default - Generate own WPA/RSN IE from configuration
  1966. * @sm: Pointer to WPA state machine data from wpa_sm_init()
  1967. * @wpa_ie: Pointer to buffer for WPA/RSN IE
  1968. * @wpa_ie_len: Pointer to the length of the wpa_ie buffer
  1969. * Returns: 0 on success, -1 on failure
  1970. */
  1971. int wpa_sm_set_assoc_wpa_ie_default(struct wpa_sm *sm, u8 *wpa_ie,
  1972. size_t *wpa_ie_len)
  1973. {
  1974. int res;
  1975. if (sm == NULL)
  1976. return -1;
  1977. res = wpa_gen_wpa_ie(sm, wpa_ie, *wpa_ie_len);
  1978. if (res < 0)
  1979. return -1;
  1980. *wpa_ie_len = res;
  1981. wpa_hexdump(MSG_DEBUG, "WPA: Set own WPA IE default",
  1982. wpa_ie, *wpa_ie_len);
  1983. if (sm->assoc_wpa_ie == NULL) {
  1984. /*
  1985. * Make a copy of the WPA/RSN IE so that 4-Way Handshake gets
  1986. * the correct version of the IE even if PMKSA caching is
  1987. * aborted (which would remove PMKID from IE generation).
  1988. */
  1989. sm->assoc_wpa_ie = os_malloc(*wpa_ie_len);
  1990. if (sm->assoc_wpa_ie == NULL)
  1991. return -1;
  1992. os_memcpy(sm->assoc_wpa_ie, wpa_ie, *wpa_ie_len);
  1993. sm->assoc_wpa_ie_len = *wpa_ie_len;
  1994. }
  1995. return 0;
  1996. }
  1997. /**
  1998. * wpa_sm_set_assoc_wpa_ie - Set own WPA/RSN IE from (Re)AssocReq
  1999. * @sm: Pointer to WPA state machine data from wpa_sm_init()
  2000. * @ie: Pointer to IE data (starting from id)
  2001. * @len: IE length
  2002. * Returns: 0 on success, -1 on failure
  2003. *
  2004. * Inform WPA state machine about the WPA/RSN IE used in (Re)Association
  2005. * Request frame. The IE will be used to override the default value generated
  2006. * with wpa_sm_set_assoc_wpa_ie_default().
  2007. */
  2008. int wpa_sm_set_assoc_wpa_ie(struct wpa_sm *sm, const u8 *ie, size_t len)
  2009. {
  2010. if (sm == NULL)
  2011. return -1;
  2012. os_free(sm->assoc_wpa_ie);
  2013. if (ie == NULL || len == 0) {
  2014. wpa_printf(MSG_DEBUG, "WPA: clearing own WPA/RSN IE");
  2015. sm->assoc_wpa_ie = NULL;
  2016. sm->assoc_wpa_ie_len = 0;
  2017. } else {
  2018. wpa_hexdump(MSG_DEBUG, "WPA: set own WPA/RSN IE", ie, len);
  2019. sm->assoc_wpa_ie = os_malloc(len);
  2020. if (sm->assoc_wpa_ie == NULL)
  2021. return -1;
  2022. os_memcpy(sm->assoc_wpa_ie, ie, len);
  2023. sm->assoc_wpa_ie_len = len;
  2024. }
  2025. return 0;
  2026. }
  2027. /**
  2028. * wpa_sm_set_ap_wpa_ie - Set AP WPA IE from Beacon/ProbeResp
  2029. * @sm: Pointer to WPA state machine data from wpa_sm_init()
  2030. * @ie: Pointer to IE data (starting from id)
  2031. * @len: IE length
  2032. * Returns: 0 on success, -1 on failure
  2033. *
  2034. * Inform WPA state machine about the WPA IE used in Beacon / Probe Response
  2035. * frame.
  2036. */
  2037. int wpa_sm_set_ap_wpa_ie(struct wpa_sm *sm, const u8 *ie, size_t len)
  2038. {
  2039. if (sm == NULL)
  2040. return -1;
  2041. os_free(sm->ap_wpa_ie);
  2042. if (ie == NULL || len == 0) {
  2043. wpa_printf(MSG_DEBUG, "WPA: clearing AP WPA IE");
  2044. sm->ap_wpa_ie = NULL;
  2045. sm->ap_wpa_ie_len = 0;
  2046. } else {
  2047. wpa_hexdump(MSG_DEBUG, "WPA: set AP WPA IE", ie, len);
  2048. sm->ap_wpa_ie = os_malloc(len);
  2049. if (sm->ap_wpa_ie == NULL)
  2050. return -1;
  2051. os_memcpy(sm->ap_wpa_ie, ie, len);
  2052. sm->ap_wpa_ie_len = len;
  2053. }
  2054. return 0;
  2055. }
  2056. /**
  2057. * wpa_sm_set_ap_rsn_ie - Set AP RSN IE from Beacon/ProbeResp
  2058. * @sm: Pointer to WPA state machine data from wpa_sm_init()
  2059. * @ie: Pointer to IE data (starting from id)
  2060. * @len: IE length
  2061. * Returns: 0 on success, -1 on failure
  2062. *
  2063. * Inform WPA state machine about the RSN IE used in Beacon / Probe Response
  2064. * frame.
  2065. */
  2066. int wpa_sm_set_ap_rsn_ie(struct wpa_sm *sm, const u8 *ie, size_t len)
  2067. {
  2068. if (sm == NULL)
  2069. return -1;
  2070. os_free(sm->ap_rsn_ie);
  2071. if (ie == NULL || len == 0) {
  2072. wpa_printf(MSG_DEBUG, "WPA: clearing AP RSN IE");
  2073. sm->ap_rsn_ie = NULL;
  2074. sm->ap_rsn_ie_len = 0;
  2075. } else {
  2076. wpa_hexdump(MSG_DEBUG, "WPA: set AP RSN IE", ie, len);
  2077. sm->ap_rsn_ie = os_malloc(len);
  2078. if (sm->ap_rsn_ie == NULL)
  2079. return -1;
  2080. os_memcpy(sm->ap_rsn_ie, ie, len);
  2081. sm->ap_rsn_ie_len = len;
  2082. }
  2083. return 0;
  2084. }
  2085. /**
  2086. * wpa_sm_parse_own_wpa_ie - Parse own WPA/RSN IE
  2087. * @sm: Pointer to WPA state machine data from wpa_sm_init()
  2088. * @data: Pointer to data area for parsing results
  2089. * Returns: 0 on success, -1 if IE is not known, or -2 on parsing failure
  2090. *
  2091. * Parse the contents of the own WPA or RSN IE from (Re)AssocReq and write the
  2092. * parsed data into data.
  2093. */
  2094. int wpa_sm_parse_own_wpa_ie(struct wpa_sm *sm, struct wpa_ie_data *data)
  2095. {
  2096. if (sm == NULL || sm->assoc_wpa_ie == NULL) {
  2097. wpa_printf(MSG_DEBUG, "WPA: No WPA/RSN IE available from "
  2098. "association info");
  2099. return -1;
  2100. }
  2101. if (wpa_parse_wpa_ie(sm->assoc_wpa_ie, sm->assoc_wpa_ie_len, data))
  2102. return -2;
  2103. return 0;
  2104. }
  2105. int wpa_sm_pmksa_cache_list(struct wpa_sm *sm, char *buf, size_t len)
  2106. {
  2107. #ifndef CONFIG_NO_WPA2
  2108. return pmksa_cache_list(sm->pmksa, buf, len);
  2109. #else /* CONFIG_NO_WPA2 */
  2110. return -1;
  2111. #endif /* CONFIG_NO_WPA2 */
  2112. }