wpa.c 76 KB

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