wpa.c 72 KB

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