wpa.c 82 KB

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