wpa.c 73 KB

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