wpa.c 94 KB

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