wpa.c 120 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375
  1. /*
  2. * WPA Supplicant - WPA state machine and EAPOL-Key processing
  3. * Copyright (c) 2003-2017, 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 "crypto/sha256.h"
  17. #include "crypto/sha384.h"
  18. #include "crypto/sha512.h"
  19. #include "common/ieee802_11_defs.h"
  20. #include "common/ieee802_11_common.h"
  21. #include "eap_common/eap_defs.h"
  22. #include "eapol_supp/eapol_supp_sm.h"
  23. #include "wpa.h"
  24. #include "eloop.h"
  25. #include "preauth.h"
  26. #include "pmksa_cache.h"
  27. #include "wpa_i.h"
  28. #include "wpa_ie.h"
  29. static const u8 null_rsc[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
  30. /**
  31. * wpa_eapol_key_send - Send WPA/RSN EAPOL-Key message
  32. * @sm: Pointer to WPA state machine data from wpa_sm_init()
  33. * @ptk: PTK for Key Confirmation/Encryption Key
  34. * @ver: Version field from Key Info
  35. * @dest: Destination address for the frame
  36. * @proto: Ethertype (usually ETH_P_EAPOL)
  37. * @msg: EAPOL-Key message
  38. * @msg_len: Length of message
  39. * @key_mic: Pointer to the buffer to which the EAPOL-Key MIC is written
  40. * Returns: >= 0 on success, < 0 on failure
  41. */
  42. int wpa_eapol_key_send(struct wpa_sm *sm, struct wpa_ptk *ptk,
  43. int ver, const u8 *dest, u16 proto,
  44. u8 *msg, size_t msg_len, u8 *key_mic)
  45. {
  46. int ret = -1;
  47. size_t mic_len = wpa_mic_len(sm->key_mgmt, sm->pmk_len);
  48. wpa_printf(MSG_DEBUG, "WPA: Send EAPOL-Key frame to " MACSTR
  49. " ver=%d mic_len=%d key_mgmt=0x%x",
  50. MAC2STR(dest), ver, (int) mic_len, sm->key_mgmt);
  51. if (is_zero_ether_addr(dest) && is_zero_ether_addr(sm->bssid)) {
  52. /*
  53. * Association event was not yet received; try to fetch
  54. * BSSID from the driver.
  55. */
  56. if (wpa_sm_get_bssid(sm, sm->bssid) < 0) {
  57. wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
  58. "WPA: Failed to read BSSID for "
  59. "EAPOL-Key destination address");
  60. } else {
  61. dest = sm->bssid;
  62. wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
  63. "WPA: Use BSSID (" MACSTR
  64. ") as the destination for EAPOL-Key",
  65. MAC2STR(dest));
  66. }
  67. }
  68. if (mic_len) {
  69. if (key_mic && (!ptk || !ptk->kck_len))
  70. goto out;
  71. if (key_mic &&
  72. wpa_eapol_key_mic(ptk->kck, ptk->kck_len, sm->key_mgmt, ver,
  73. msg, msg_len, key_mic)) {
  74. wpa_msg(sm->ctx->msg_ctx, MSG_ERROR,
  75. "WPA: Failed to generate EAPOL-Key version %d key_mgmt 0x%x MIC",
  76. ver, sm->key_mgmt);
  77. goto out;
  78. }
  79. if (ptk)
  80. wpa_hexdump_key(MSG_DEBUG, "WPA: KCK",
  81. ptk->kck, ptk->kck_len);
  82. wpa_hexdump(MSG_DEBUG, "WPA: Derived Key MIC",
  83. key_mic, mic_len);
  84. } else {
  85. #ifdef CONFIG_FILS
  86. /* AEAD cipher - Key MIC field not used */
  87. struct ieee802_1x_hdr *s_hdr, *hdr;
  88. struct wpa_eapol_key *s_key, *key;
  89. u8 *buf, *s_key_data, *key_data;
  90. size_t buf_len = msg_len + AES_BLOCK_SIZE;
  91. size_t key_data_len;
  92. u16 eapol_len;
  93. const u8 *aad[1];
  94. size_t aad_len[1];
  95. if (!ptk || !ptk->kek_len)
  96. goto out;
  97. key_data_len = msg_len - sizeof(struct ieee802_1x_hdr) -
  98. sizeof(struct wpa_eapol_key) - 2;
  99. buf = os_malloc(buf_len);
  100. if (!buf)
  101. goto out;
  102. os_memcpy(buf, msg, msg_len);
  103. hdr = (struct ieee802_1x_hdr *) buf;
  104. key = (struct wpa_eapol_key *) (hdr + 1);
  105. key_data = ((u8 *) (key + 1)) + 2;
  106. /* Update EAPOL header to include AES-SIV overhead */
  107. eapol_len = be_to_host16(hdr->length);
  108. eapol_len += AES_BLOCK_SIZE;
  109. hdr->length = host_to_be16(eapol_len);
  110. /* Update Key Data Length field to include AES-SIV overhead */
  111. WPA_PUT_BE16((u8 *) (key + 1), AES_BLOCK_SIZE + key_data_len);
  112. s_hdr = (struct ieee802_1x_hdr *) msg;
  113. s_key = (struct wpa_eapol_key *) (s_hdr + 1);
  114. s_key_data = ((u8 *) (s_key + 1)) + 2;
  115. wpa_hexdump_key(MSG_DEBUG, "WPA: Plaintext Key Data",
  116. s_key_data, key_data_len);
  117. wpa_hexdump_key(MSG_DEBUG, "WPA: KEK", ptk->kek, ptk->kek_len);
  118. /* AES-SIV AAD from EAPOL protocol version field (inclusive) to
  119. * to Key Data (exclusive). */
  120. aad[0] = buf;
  121. aad_len[0] = key_data - buf;
  122. if (aes_siv_encrypt(ptk->kek, ptk->kek_len,
  123. s_key_data, key_data_len,
  124. 1, aad, aad_len, key_data) < 0) {
  125. os_free(buf);
  126. goto out;
  127. }
  128. wpa_hexdump(MSG_DEBUG, "WPA: Encrypted Key Data from SIV",
  129. key_data, AES_BLOCK_SIZE + key_data_len);
  130. os_free(msg);
  131. msg = buf;
  132. msg_len = buf_len;
  133. #else /* CONFIG_FILS */
  134. goto out;
  135. #endif /* CONFIG_FILS */
  136. }
  137. wpa_hexdump(MSG_MSGDUMP, "WPA: TX EAPOL-Key", msg, msg_len);
  138. ret = wpa_sm_ether_send(sm, dest, proto, msg, msg_len);
  139. eapol_sm_notify_tx_eapol_key(sm->eapol);
  140. out:
  141. os_free(msg);
  142. return ret;
  143. }
  144. /**
  145. * wpa_sm_key_request - Send EAPOL-Key Request
  146. * @sm: Pointer to WPA state machine data from wpa_sm_init()
  147. * @error: Indicate whether this is an Michael MIC error report
  148. * @pairwise: 1 = error report for pairwise packet, 0 = for group packet
  149. *
  150. * Send an EAPOL-Key Request to the current authenticator. This function is
  151. * used to request rekeying and it is usually called when a local Michael MIC
  152. * failure is detected.
  153. */
  154. void wpa_sm_key_request(struct wpa_sm *sm, int error, int pairwise)
  155. {
  156. size_t mic_len, hdrlen, rlen;
  157. struct wpa_eapol_key *reply;
  158. int key_info, ver;
  159. u8 bssid[ETH_ALEN], *rbuf, *key_mic, *mic;
  160. if (sm->key_mgmt == WPA_KEY_MGMT_OSEN ||
  161. wpa_key_mgmt_suite_b(sm->key_mgmt))
  162. ver = WPA_KEY_INFO_TYPE_AKM_DEFINED;
  163. else if (wpa_key_mgmt_ft(sm->key_mgmt) ||
  164. wpa_key_mgmt_sha256(sm->key_mgmt))
  165. ver = WPA_KEY_INFO_TYPE_AES_128_CMAC;
  166. else if (sm->pairwise_cipher != WPA_CIPHER_TKIP)
  167. ver = WPA_KEY_INFO_TYPE_HMAC_SHA1_AES;
  168. else
  169. ver = WPA_KEY_INFO_TYPE_HMAC_MD5_RC4;
  170. if (wpa_sm_get_bssid(sm, bssid) < 0) {
  171. wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
  172. "Failed to read BSSID for EAPOL-Key request");
  173. return;
  174. }
  175. mic_len = wpa_mic_len(sm->key_mgmt, sm->pmk_len);
  176. hdrlen = sizeof(*reply) + mic_len + 2;
  177. rbuf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_KEY, NULL,
  178. hdrlen, &rlen, (void *) &reply);
  179. if (rbuf == NULL)
  180. return;
  181. reply->type = (sm->proto == WPA_PROTO_RSN ||
  182. sm->proto == WPA_PROTO_OSEN) ?
  183. EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA;
  184. key_info = WPA_KEY_INFO_REQUEST | ver;
  185. if (sm->ptk_set)
  186. key_info |= WPA_KEY_INFO_SECURE;
  187. if (sm->ptk_set && mic_len)
  188. key_info |= WPA_KEY_INFO_MIC;
  189. if (error)
  190. key_info |= WPA_KEY_INFO_ERROR;
  191. if (pairwise)
  192. key_info |= WPA_KEY_INFO_KEY_TYPE;
  193. WPA_PUT_BE16(reply->key_info, key_info);
  194. WPA_PUT_BE16(reply->key_length, 0);
  195. os_memcpy(reply->replay_counter, sm->request_counter,
  196. WPA_REPLAY_COUNTER_LEN);
  197. inc_byte_array(sm->request_counter, WPA_REPLAY_COUNTER_LEN);
  198. mic = (u8 *) (reply + 1);
  199. WPA_PUT_BE16(mic + mic_len, 0);
  200. if (!(key_info & WPA_KEY_INFO_MIC))
  201. key_mic = NULL;
  202. else
  203. key_mic = mic;
  204. wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
  205. "WPA: Sending EAPOL-Key Request (error=%d "
  206. "pairwise=%d ptk_set=%d len=%lu)",
  207. error, pairwise, sm->ptk_set, (unsigned long) rlen);
  208. wpa_eapol_key_send(sm, &sm->ptk, ver, bssid, ETH_P_EAPOL, rbuf, rlen,
  209. key_mic);
  210. }
  211. static void wpa_supplicant_key_mgmt_set_pmk(struct wpa_sm *sm)
  212. {
  213. #ifdef CONFIG_IEEE80211R
  214. if (sm->key_mgmt == WPA_KEY_MGMT_FT_IEEE8021X) {
  215. if (wpa_sm_key_mgmt_set_pmk(sm, sm->xxkey, sm->xxkey_len))
  216. wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
  217. "RSN: Cannot set low order 256 bits of MSK for key management offload");
  218. } else {
  219. #endif /* CONFIG_IEEE80211R */
  220. if (wpa_sm_key_mgmt_set_pmk(sm, sm->pmk, sm->pmk_len))
  221. wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
  222. "RSN: Cannot set PMK for key management offload");
  223. #ifdef CONFIG_IEEE80211R
  224. }
  225. #endif /* CONFIG_IEEE80211R */
  226. }
  227. static int wpa_supplicant_get_pmk(struct wpa_sm *sm,
  228. const unsigned char *src_addr,
  229. const u8 *pmkid)
  230. {
  231. int abort_cached = 0;
  232. if (pmkid && !sm->cur_pmksa) {
  233. /* When using drivers that generate RSN IE, wpa_supplicant may
  234. * not have enough time to get the association information
  235. * event before receiving this 1/4 message, so try to find a
  236. * matching PMKSA cache entry here. */
  237. sm->cur_pmksa = pmksa_cache_get(sm->pmksa, src_addr, pmkid,
  238. NULL);
  239. if (sm->cur_pmksa) {
  240. wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
  241. "RSN: found matching PMKID from PMKSA cache");
  242. } else {
  243. wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
  244. "RSN: no matching PMKID found");
  245. abort_cached = 1;
  246. }
  247. }
  248. if (pmkid && sm->cur_pmksa &&
  249. os_memcmp_const(pmkid, sm->cur_pmksa->pmkid, PMKID_LEN) == 0) {
  250. wpa_hexdump(MSG_DEBUG, "RSN: matched PMKID", pmkid, PMKID_LEN);
  251. wpa_sm_set_pmk_from_pmksa(sm);
  252. wpa_hexdump_key(MSG_DEBUG, "RSN: PMK from PMKSA cache",
  253. sm->pmk, sm->pmk_len);
  254. eapol_sm_notify_cached(sm->eapol);
  255. #ifdef CONFIG_IEEE80211R
  256. sm->xxkey_len = 0;
  257. #endif /* CONFIG_IEEE80211R */
  258. } else if (wpa_key_mgmt_wpa_ieee8021x(sm->key_mgmt) && sm->eapol) {
  259. int res, pmk_len;
  260. if (wpa_key_mgmt_sha384(sm->key_mgmt))
  261. pmk_len = PMK_LEN_SUITE_B_192;
  262. else
  263. pmk_len = PMK_LEN;
  264. res = eapol_sm_get_key(sm->eapol, sm->pmk, pmk_len);
  265. if (res) {
  266. if (pmk_len == PMK_LEN) {
  267. /*
  268. * EAP-LEAP is an exception from other EAP
  269. * methods: it uses only 16-byte PMK.
  270. */
  271. res = eapol_sm_get_key(sm->eapol, sm->pmk, 16);
  272. pmk_len = 16;
  273. }
  274. } else {
  275. #ifdef CONFIG_IEEE80211R
  276. u8 buf[2 * PMK_LEN];
  277. if (eapol_sm_get_key(sm->eapol, buf, 2 * PMK_LEN) == 0)
  278. {
  279. os_memcpy(sm->xxkey, buf + PMK_LEN, PMK_LEN);
  280. sm->xxkey_len = PMK_LEN;
  281. os_memset(buf, 0, sizeof(buf));
  282. }
  283. #endif /* CONFIG_IEEE80211R */
  284. }
  285. if (res == 0) {
  286. struct rsn_pmksa_cache_entry *sa = NULL;
  287. const u8 *fils_cache_id = NULL;
  288. #ifdef CONFIG_FILS
  289. if (sm->fils_cache_id_set)
  290. fils_cache_id = sm->fils_cache_id;
  291. #endif /* CONFIG_FILS */
  292. wpa_hexdump_key(MSG_DEBUG, "WPA: PMK from EAPOL state "
  293. "machines", sm->pmk, pmk_len);
  294. sm->pmk_len = pmk_len;
  295. wpa_supplicant_key_mgmt_set_pmk(sm);
  296. if (sm->proto == WPA_PROTO_RSN &&
  297. !wpa_key_mgmt_suite_b(sm->key_mgmt) &&
  298. !wpa_key_mgmt_ft(sm->key_mgmt)) {
  299. sa = pmksa_cache_add(sm->pmksa,
  300. sm->pmk, pmk_len, NULL,
  301. NULL, 0,
  302. src_addr, sm->own_addr,
  303. sm->network_ctx,
  304. sm->key_mgmt,
  305. fils_cache_id);
  306. }
  307. if (!sm->cur_pmksa && pmkid &&
  308. pmksa_cache_get(sm->pmksa, src_addr, pmkid, NULL))
  309. {
  310. wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
  311. "RSN: the new PMK matches with the "
  312. "PMKID");
  313. abort_cached = 0;
  314. } else if (sa && !sm->cur_pmksa && pmkid) {
  315. /*
  316. * It looks like the authentication server
  317. * derived mismatching MSK. This should not
  318. * really happen, but bugs happen.. There is not
  319. * much we can do here without knowing what
  320. * exactly caused the server to misbehave.
  321. */
  322. wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
  323. "RSN: PMKID mismatch - authentication server may have derived different MSK?!");
  324. return -1;
  325. }
  326. if (!sm->cur_pmksa)
  327. sm->cur_pmksa = sa;
  328. } else {
  329. wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
  330. "WPA: Failed to get master session key from "
  331. "EAPOL state machines - key handshake "
  332. "aborted");
  333. if (sm->cur_pmksa) {
  334. wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
  335. "RSN: Cancelled PMKSA caching "
  336. "attempt");
  337. sm->cur_pmksa = NULL;
  338. abort_cached = 1;
  339. } else if (!abort_cached) {
  340. return -1;
  341. }
  342. }
  343. }
  344. if (abort_cached && wpa_key_mgmt_wpa_ieee8021x(sm->key_mgmt) &&
  345. !wpa_key_mgmt_suite_b(sm->key_mgmt) &&
  346. !wpa_key_mgmt_ft(sm->key_mgmt) && sm->key_mgmt != WPA_KEY_MGMT_OSEN)
  347. {
  348. /* Send EAPOL-Start to trigger full EAP authentication. */
  349. u8 *buf;
  350. size_t buflen;
  351. wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
  352. "RSN: no PMKSA entry found - trigger "
  353. "full EAP authentication");
  354. buf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_START,
  355. NULL, 0, &buflen, NULL);
  356. if (buf) {
  357. wpa_sm_ether_send(sm, sm->bssid, ETH_P_EAPOL,
  358. buf, buflen);
  359. os_free(buf);
  360. return -2;
  361. }
  362. return -1;
  363. }
  364. return 0;
  365. }
  366. /**
  367. * wpa_supplicant_send_2_of_4 - Send message 2 of WPA/RSN 4-Way Handshake
  368. * @sm: Pointer to WPA state machine data from wpa_sm_init()
  369. * @dst: Destination address for the frame
  370. * @key: Pointer to the EAPOL-Key frame header
  371. * @ver: Version bits from EAPOL-Key Key Info
  372. * @nonce: Nonce value for the EAPOL-Key frame
  373. * @wpa_ie: WPA/RSN IE
  374. * @wpa_ie_len: Length of the WPA/RSN IE
  375. * @ptk: PTK to use for keyed hash and encryption
  376. * Returns: >= 0 on success, < 0 on failure
  377. */
  378. int wpa_supplicant_send_2_of_4(struct wpa_sm *sm, const unsigned char *dst,
  379. const struct wpa_eapol_key *key,
  380. int ver, const u8 *nonce,
  381. const u8 *wpa_ie, size_t wpa_ie_len,
  382. struct wpa_ptk *ptk)
  383. {
  384. size_t mic_len, hdrlen, rlen;
  385. struct wpa_eapol_key *reply;
  386. u8 *rbuf, *key_mic;
  387. u8 *rsn_ie_buf = NULL;
  388. u16 key_info;
  389. if (wpa_ie == NULL) {
  390. wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, "WPA: No wpa_ie set - "
  391. "cannot generate msg 2/4");
  392. return -1;
  393. }
  394. #ifdef CONFIG_IEEE80211R
  395. if (wpa_key_mgmt_ft(sm->key_mgmt)) {
  396. int res;
  397. /*
  398. * Add PMKR1Name into RSN IE (PMKID-List) and add MDIE and
  399. * FTIE from (Re)Association Response.
  400. */
  401. rsn_ie_buf = os_malloc(wpa_ie_len + 2 + 2 + PMKID_LEN +
  402. sm->assoc_resp_ies_len);
  403. if (rsn_ie_buf == NULL)
  404. return -1;
  405. os_memcpy(rsn_ie_buf, wpa_ie, wpa_ie_len);
  406. res = wpa_insert_pmkid(rsn_ie_buf, &wpa_ie_len,
  407. sm->pmk_r1_name);
  408. if (res < 0) {
  409. os_free(rsn_ie_buf);
  410. return -1;
  411. }
  412. if (sm->assoc_resp_ies) {
  413. os_memcpy(rsn_ie_buf + wpa_ie_len, sm->assoc_resp_ies,
  414. sm->assoc_resp_ies_len);
  415. wpa_ie_len += sm->assoc_resp_ies_len;
  416. }
  417. wpa_ie = rsn_ie_buf;
  418. }
  419. #endif /* CONFIG_IEEE80211R */
  420. wpa_hexdump(MSG_DEBUG, "WPA: WPA IE for msg 2/4", wpa_ie, wpa_ie_len);
  421. mic_len = wpa_mic_len(sm->key_mgmt, sm->pmk_len);
  422. hdrlen = sizeof(*reply) + mic_len + 2;
  423. rbuf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_KEY,
  424. NULL, hdrlen + wpa_ie_len,
  425. &rlen, (void *) &reply);
  426. if (rbuf == NULL) {
  427. os_free(rsn_ie_buf);
  428. return -1;
  429. }
  430. reply->type = (sm->proto == WPA_PROTO_RSN ||
  431. sm->proto == WPA_PROTO_OSEN) ?
  432. EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA;
  433. key_info = ver | WPA_KEY_INFO_KEY_TYPE;
  434. if (mic_len)
  435. key_info |= WPA_KEY_INFO_MIC;
  436. else
  437. key_info |= WPA_KEY_INFO_ENCR_KEY_DATA;
  438. WPA_PUT_BE16(reply->key_info, key_info);
  439. if (sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN)
  440. WPA_PUT_BE16(reply->key_length, 0);
  441. else
  442. os_memcpy(reply->key_length, key->key_length, 2);
  443. os_memcpy(reply->replay_counter, key->replay_counter,
  444. WPA_REPLAY_COUNTER_LEN);
  445. wpa_hexdump(MSG_DEBUG, "WPA: Replay Counter", reply->replay_counter,
  446. WPA_REPLAY_COUNTER_LEN);
  447. key_mic = (u8 *) (reply + 1);
  448. WPA_PUT_BE16(key_mic + mic_len, wpa_ie_len); /* Key Data Length */
  449. os_memcpy(key_mic + mic_len + 2, wpa_ie, wpa_ie_len); /* Key Data */
  450. os_free(rsn_ie_buf);
  451. os_memcpy(reply->key_nonce, nonce, WPA_NONCE_LEN);
  452. wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Sending EAPOL-Key 2/4");
  453. return wpa_eapol_key_send(sm, ptk, ver, dst, ETH_P_EAPOL, rbuf, rlen,
  454. key_mic);
  455. }
  456. static int wpa_derive_ptk(struct wpa_sm *sm, const unsigned char *src_addr,
  457. const struct wpa_eapol_key *key, struct wpa_ptk *ptk)
  458. {
  459. #ifdef CONFIG_IEEE80211R
  460. if (wpa_key_mgmt_ft(sm->key_mgmt))
  461. return wpa_derive_ptk_ft(sm, src_addr, key, ptk);
  462. #endif /* CONFIG_IEEE80211R */
  463. return wpa_pmk_to_ptk(sm->pmk, sm->pmk_len, "Pairwise key expansion",
  464. sm->own_addr, sm->bssid, sm->snonce,
  465. key->key_nonce, ptk, sm->key_mgmt,
  466. sm->pairwise_cipher);
  467. }
  468. static void wpa_supplicant_process_1_of_4(struct wpa_sm *sm,
  469. const unsigned char *src_addr,
  470. const struct wpa_eapol_key *key,
  471. u16 ver, const u8 *key_data,
  472. size_t key_data_len)
  473. {
  474. struct wpa_eapol_ie_parse ie;
  475. struct wpa_ptk *ptk;
  476. int res;
  477. u8 *kde, *kde_buf = NULL;
  478. size_t kde_len;
  479. if (wpa_sm_get_network_ctx(sm) == NULL) {
  480. wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, "WPA: No SSID info "
  481. "found (msg 1 of 4)");
  482. return;
  483. }
  484. wpa_sm_set_state(sm, WPA_4WAY_HANDSHAKE);
  485. wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: RX message 1 of 4-Way "
  486. "Handshake from " MACSTR " (ver=%d)", MAC2STR(src_addr), ver);
  487. os_memset(&ie, 0, sizeof(ie));
  488. if (sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN) {
  489. /* RSN: msg 1/4 should contain PMKID for the selected PMK */
  490. wpa_hexdump(MSG_DEBUG, "RSN: msg 1/4 key data",
  491. key_data, key_data_len);
  492. if (wpa_supplicant_parse_ies(key_data, key_data_len, &ie) < 0)
  493. goto failed;
  494. if (ie.pmkid) {
  495. wpa_hexdump(MSG_DEBUG, "RSN: PMKID from "
  496. "Authenticator", ie.pmkid, PMKID_LEN);
  497. }
  498. }
  499. res = wpa_supplicant_get_pmk(sm, src_addr, ie.pmkid);
  500. if (res == -2) {
  501. wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "RSN: Do not reply to "
  502. "msg 1/4 - requesting full EAP authentication");
  503. return;
  504. }
  505. if (res)
  506. goto failed;
  507. if (sm->renew_snonce) {
  508. if (random_get_bytes(sm->snonce, WPA_NONCE_LEN)) {
  509. wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
  510. "WPA: Failed to get random data for SNonce");
  511. goto failed;
  512. }
  513. sm->renew_snonce = 0;
  514. wpa_hexdump(MSG_DEBUG, "WPA: Renewed SNonce",
  515. sm->snonce, WPA_NONCE_LEN);
  516. }
  517. /* Calculate PTK which will be stored as a temporary PTK until it has
  518. * been verified when processing message 3/4. */
  519. ptk = &sm->tptk;
  520. wpa_derive_ptk(sm, src_addr, key, ptk);
  521. if (sm->pairwise_cipher == WPA_CIPHER_TKIP) {
  522. u8 buf[8];
  523. /* Supplicant: swap tx/rx Mic keys */
  524. os_memcpy(buf, &ptk->tk[16], 8);
  525. os_memcpy(&ptk->tk[16], &ptk->tk[24], 8);
  526. os_memcpy(&ptk->tk[24], buf, 8);
  527. os_memset(buf, 0, sizeof(buf));
  528. }
  529. sm->tptk_set = 1;
  530. kde = sm->assoc_wpa_ie;
  531. kde_len = sm->assoc_wpa_ie_len;
  532. #ifdef CONFIG_P2P
  533. if (sm->p2p) {
  534. kde_buf = os_malloc(kde_len + 2 + RSN_SELECTOR_LEN + 1);
  535. if (kde_buf) {
  536. u8 *pos;
  537. wpa_printf(MSG_DEBUG, "P2P: Add IP Address Request KDE "
  538. "into EAPOL-Key 2/4");
  539. os_memcpy(kde_buf, kde, kde_len);
  540. kde = kde_buf;
  541. pos = kde + kde_len;
  542. *pos++ = WLAN_EID_VENDOR_SPECIFIC;
  543. *pos++ = RSN_SELECTOR_LEN + 1;
  544. RSN_SELECTOR_PUT(pos, WFA_KEY_DATA_IP_ADDR_REQ);
  545. pos += RSN_SELECTOR_LEN;
  546. *pos++ = 0x01;
  547. kde_len = pos - kde;
  548. }
  549. }
  550. #endif /* CONFIG_P2P */
  551. if (wpa_supplicant_send_2_of_4(sm, sm->bssid, key, ver, sm->snonce,
  552. kde, kde_len, ptk) < 0)
  553. goto failed;
  554. os_free(kde_buf);
  555. os_memcpy(sm->anonce, key->key_nonce, WPA_NONCE_LEN);
  556. return;
  557. failed:
  558. os_free(kde_buf);
  559. wpa_sm_deauthenticate(sm, WLAN_REASON_UNSPECIFIED);
  560. }
  561. static void wpa_sm_start_preauth(void *eloop_ctx, void *timeout_ctx)
  562. {
  563. struct wpa_sm *sm = eloop_ctx;
  564. rsn_preauth_candidate_process(sm);
  565. }
  566. static void wpa_supplicant_key_neg_complete(struct wpa_sm *sm,
  567. const u8 *addr, int secure)
  568. {
  569. wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
  570. "WPA: Key negotiation completed with "
  571. MACSTR " [PTK=%s GTK=%s]", MAC2STR(addr),
  572. wpa_cipher_txt(sm->pairwise_cipher),
  573. wpa_cipher_txt(sm->group_cipher));
  574. wpa_sm_cancel_auth_timeout(sm);
  575. wpa_sm_set_state(sm, WPA_COMPLETED);
  576. if (secure) {
  577. wpa_sm_mlme_setprotection(
  578. sm, addr, MLME_SETPROTECTION_PROTECT_TYPE_RX_TX,
  579. MLME_SETPROTECTION_KEY_TYPE_PAIRWISE);
  580. eapol_sm_notify_portValid(sm->eapol, TRUE);
  581. if (wpa_key_mgmt_wpa_psk(sm->key_mgmt) ||
  582. sm->key_mgmt == WPA_KEY_MGMT_DPP ||
  583. sm->key_mgmt == WPA_KEY_MGMT_OWE)
  584. eapol_sm_notify_eap_success(sm->eapol, TRUE);
  585. /*
  586. * Start preauthentication after a short wait to avoid a
  587. * possible race condition between the data receive and key
  588. * configuration after the 4-Way Handshake. This increases the
  589. * likelihood of the first preauth EAPOL-Start frame getting to
  590. * the target AP.
  591. */
  592. eloop_register_timeout(1, 0, wpa_sm_start_preauth, sm, NULL);
  593. }
  594. if (sm->cur_pmksa && sm->cur_pmksa->opportunistic) {
  595. wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
  596. "RSN: Authenticator accepted "
  597. "opportunistic PMKSA entry - marking it valid");
  598. sm->cur_pmksa->opportunistic = 0;
  599. }
  600. #ifdef CONFIG_IEEE80211R
  601. if (wpa_key_mgmt_ft(sm->key_mgmt)) {
  602. /* Prepare for the next transition */
  603. wpa_ft_prepare_auth_request(sm, NULL);
  604. }
  605. #endif /* CONFIG_IEEE80211R */
  606. }
  607. static void wpa_sm_rekey_ptk(void *eloop_ctx, void *timeout_ctx)
  608. {
  609. struct wpa_sm *sm = eloop_ctx;
  610. wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Request PTK rekeying");
  611. wpa_sm_key_request(sm, 0, 1);
  612. }
  613. static int wpa_supplicant_install_ptk(struct wpa_sm *sm,
  614. const struct wpa_eapol_key *key)
  615. {
  616. int keylen, rsclen;
  617. enum wpa_alg alg;
  618. const u8 *key_rsc;
  619. if (sm->ptk.installed) {
  620. wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
  621. "WPA: Do not re-install same PTK to the driver");
  622. return 0;
  623. }
  624. wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
  625. "WPA: Installing PTK to the driver");
  626. if (sm->pairwise_cipher == WPA_CIPHER_NONE) {
  627. wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Pairwise Cipher "
  628. "Suite: NONE - do not use pairwise keys");
  629. return 0;
  630. }
  631. if (!wpa_cipher_valid_pairwise(sm->pairwise_cipher)) {
  632. wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
  633. "WPA: Unsupported pairwise cipher %d",
  634. sm->pairwise_cipher);
  635. return -1;
  636. }
  637. alg = wpa_cipher_to_alg(sm->pairwise_cipher);
  638. keylen = wpa_cipher_key_len(sm->pairwise_cipher);
  639. rsclen = wpa_cipher_rsc_len(sm->pairwise_cipher);
  640. if (sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN) {
  641. key_rsc = null_rsc;
  642. } else {
  643. key_rsc = key->key_rsc;
  644. wpa_hexdump(MSG_DEBUG, "WPA: RSC", key_rsc, rsclen);
  645. }
  646. if (wpa_sm_set_key(sm, alg, sm->bssid, 0, 1, key_rsc, rsclen,
  647. sm->ptk.tk, keylen) < 0) {
  648. wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
  649. "WPA: Failed to set PTK to the "
  650. "driver (alg=%d keylen=%d bssid=" MACSTR ")",
  651. alg, keylen, MAC2STR(sm->bssid));
  652. return -1;
  653. }
  654. /* TK is not needed anymore in supplicant */
  655. os_memset(sm->ptk.tk, 0, WPA_TK_MAX_LEN);
  656. sm->ptk.installed = 1;
  657. if (sm->wpa_ptk_rekey) {
  658. eloop_cancel_timeout(wpa_sm_rekey_ptk, sm, NULL);
  659. eloop_register_timeout(sm->wpa_ptk_rekey, 0, wpa_sm_rekey_ptk,
  660. sm, NULL);
  661. }
  662. return 0;
  663. }
  664. static int wpa_supplicant_check_group_cipher(struct wpa_sm *sm,
  665. int group_cipher,
  666. int keylen, int maxkeylen,
  667. int *key_rsc_len,
  668. enum wpa_alg *alg)
  669. {
  670. int klen;
  671. *alg = wpa_cipher_to_alg(group_cipher);
  672. if (*alg == WPA_ALG_NONE) {
  673. wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
  674. "WPA: Unsupported Group Cipher %d",
  675. group_cipher);
  676. return -1;
  677. }
  678. *key_rsc_len = wpa_cipher_rsc_len(group_cipher);
  679. klen = wpa_cipher_key_len(group_cipher);
  680. if (keylen != klen || maxkeylen < klen) {
  681. wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
  682. "WPA: Unsupported %s Group Cipher key length %d (%d)",
  683. wpa_cipher_txt(group_cipher), keylen, maxkeylen);
  684. return -1;
  685. }
  686. return 0;
  687. }
  688. struct wpa_gtk_data {
  689. enum wpa_alg alg;
  690. int tx, key_rsc_len, keyidx;
  691. u8 gtk[32];
  692. int gtk_len;
  693. };
  694. static int wpa_supplicant_install_gtk(struct wpa_sm *sm,
  695. const struct wpa_gtk_data *gd,
  696. const u8 *key_rsc, int wnm_sleep)
  697. {
  698. const u8 *_gtk = gd->gtk;
  699. u8 gtk_buf[32];
  700. /* Detect possible key reinstallation */
  701. if ((sm->gtk.gtk_len == (size_t) gd->gtk_len &&
  702. os_memcmp(sm->gtk.gtk, gd->gtk, sm->gtk.gtk_len) == 0) ||
  703. (sm->gtk_wnm_sleep.gtk_len == (size_t) gd->gtk_len &&
  704. os_memcmp(sm->gtk_wnm_sleep.gtk, gd->gtk,
  705. sm->gtk_wnm_sleep.gtk_len) == 0)) {
  706. wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
  707. "WPA: Not reinstalling already in-use GTK to the driver (keyidx=%d tx=%d len=%d)",
  708. gd->keyidx, gd->tx, gd->gtk_len);
  709. return 0;
  710. }
  711. wpa_hexdump_key(MSG_DEBUG, "WPA: Group Key", gd->gtk, gd->gtk_len);
  712. wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
  713. "WPA: Installing GTK to the driver (keyidx=%d tx=%d len=%d)",
  714. gd->keyidx, gd->tx, gd->gtk_len);
  715. wpa_hexdump(MSG_DEBUG, "WPA: RSC", key_rsc, gd->key_rsc_len);
  716. if (sm->group_cipher == WPA_CIPHER_TKIP) {
  717. /* Swap Tx/Rx keys for Michael MIC */
  718. os_memcpy(gtk_buf, gd->gtk, 16);
  719. os_memcpy(gtk_buf + 16, gd->gtk + 24, 8);
  720. os_memcpy(gtk_buf + 24, gd->gtk + 16, 8);
  721. _gtk = gtk_buf;
  722. }
  723. if (sm->pairwise_cipher == WPA_CIPHER_NONE) {
  724. if (wpa_sm_set_key(sm, gd->alg, NULL,
  725. gd->keyidx, 1, key_rsc, gd->key_rsc_len,
  726. _gtk, gd->gtk_len) < 0) {
  727. wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
  728. "WPA: Failed to set GTK to the driver "
  729. "(Group only)");
  730. os_memset(gtk_buf, 0, sizeof(gtk_buf));
  731. return -1;
  732. }
  733. } else if (wpa_sm_set_key(sm, gd->alg, broadcast_ether_addr,
  734. gd->keyidx, gd->tx, key_rsc, gd->key_rsc_len,
  735. _gtk, gd->gtk_len) < 0) {
  736. wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
  737. "WPA: Failed to set GTK to "
  738. "the driver (alg=%d keylen=%d keyidx=%d)",
  739. gd->alg, gd->gtk_len, gd->keyidx);
  740. os_memset(gtk_buf, 0, sizeof(gtk_buf));
  741. return -1;
  742. }
  743. os_memset(gtk_buf, 0, sizeof(gtk_buf));
  744. if (wnm_sleep) {
  745. sm->gtk_wnm_sleep.gtk_len = gd->gtk_len;
  746. os_memcpy(sm->gtk_wnm_sleep.gtk, gd->gtk,
  747. sm->gtk_wnm_sleep.gtk_len);
  748. } else {
  749. sm->gtk.gtk_len = gd->gtk_len;
  750. os_memcpy(sm->gtk.gtk, gd->gtk, sm->gtk.gtk_len);
  751. }
  752. return 0;
  753. }
  754. static int wpa_supplicant_gtk_tx_bit_workaround(const struct wpa_sm *sm,
  755. int tx)
  756. {
  757. if (tx && sm->pairwise_cipher != WPA_CIPHER_NONE) {
  758. /* Ignore Tx bit for GTK if a pairwise key is used. One AP
  759. * seemed to set this bit (incorrectly, since Tx is only when
  760. * doing Group Key only APs) and without this workaround, the
  761. * data connection does not work because wpa_supplicant
  762. * configured non-zero keyidx to be used for unicast. */
  763. wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
  764. "WPA: Tx bit set for GTK, but pairwise "
  765. "keys are used - ignore Tx bit");
  766. return 0;
  767. }
  768. return tx;
  769. }
  770. static int wpa_supplicant_rsc_relaxation(const struct wpa_sm *sm,
  771. const u8 *rsc)
  772. {
  773. int rsclen;
  774. if (!sm->wpa_rsc_relaxation)
  775. return 0;
  776. rsclen = wpa_cipher_rsc_len(sm->group_cipher);
  777. /*
  778. * Try to detect RSC (endian) corruption issue where the AP sends
  779. * the RSC bytes in EAPOL-Key message in the wrong order, both if
  780. * it's actually a 6-byte field (as it should be) and if it treats
  781. * it as an 8-byte field.
  782. * An AP model known to have this bug is the Sapido RB-1632.
  783. */
  784. if (rsclen == 6 && ((rsc[5] && !rsc[0]) || rsc[6] || rsc[7])) {
  785. wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
  786. "RSC %02x%02x%02x%02x%02x%02x%02x%02x is likely bogus, using 0",
  787. rsc[0], rsc[1], rsc[2], rsc[3],
  788. rsc[4], rsc[5], rsc[6], rsc[7]);
  789. return 1;
  790. }
  791. return 0;
  792. }
  793. static int wpa_supplicant_pairwise_gtk(struct wpa_sm *sm,
  794. const struct wpa_eapol_key *key,
  795. const u8 *gtk, size_t gtk_len,
  796. int key_info)
  797. {
  798. struct wpa_gtk_data gd;
  799. const u8 *key_rsc;
  800. /*
  801. * IEEE Std 802.11i-2004 - 8.5.2 EAPOL-Key frames - Figure 43x
  802. * GTK KDE format:
  803. * KeyID[bits 0-1], Tx [bit 2], Reserved [bits 3-7]
  804. * Reserved [bits 0-7]
  805. * GTK
  806. */
  807. os_memset(&gd, 0, sizeof(gd));
  808. wpa_hexdump_key(MSG_DEBUG, "RSN: received GTK in pairwise handshake",
  809. gtk, gtk_len);
  810. if (gtk_len < 2 || gtk_len - 2 > sizeof(gd.gtk))
  811. return -1;
  812. gd.keyidx = gtk[0] & 0x3;
  813. gd.tx = wpa_supplicant_gtk_tx_bit_workaround(sm,
  814. !!(gtk[0] & BIT(2)));
  815. gtk += 2;
  816. gtk_len -= 2;
  817. os_memcpy(gd.gtk, gtk, gtk_len);
  818. gd.gtk_len = gtk_len;
  819. key_rsc = key->key_rsc;
  820. if (wpa_supplicant_rsc_relaxation(sm, key->key_rsc))
  821. key_rsc = null_rsc;
  822. if (sm->group_cipher != WPA_CIPHER_GTK_NOT_USED &&
  823. (wpa_supplicant_check_group_cipher(sm, sm->group_cipher,
  824. gtk_len, gtk_len,
  825. &gd.key_rsc_len, &gd.alg) ||
  826. wpa_supplicant_install_gtk(sm, &gd, key_rsc, 0))) {
  827. wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
  828. "RSN: Failed to install GTK");
  829. os_memset(&gd, 0, sizeof(gd));
  830. return -1;
  831. }
  832. os_memset(&gd, 0, sizeof(gd));
  833. wpa_supplicant_key_neg_complete(sm, sm->bssid,
  834. key_info & WPA_KEY_INFO_SECURE);
  835. return 0;
  836. }
  837. #ifdef CONFIG_IEEE80211W
  838. static int wpa_supplicant_install_igtk(struct wpa_sm *sm,
  839. const struct wpa_igtk_kde *igtk,
  840. int wnm_sleep)
  841. {
  842. size_t len = wpa_cipher_key_len(sm->mgmt_group_cipher);
  843. u16 keyidx = WPA_GET_LE16(igtk->keyid);
  844. /* Detect possible key reinstallation */
  845. if ((sm->igtk.igtk_len == len &&
  846. os_memcmp(sm->igtk.igtk, igtk->igtk, sm->igtk.igtk_len) == 0) ||
  847. (sm->igtk_wnm_sleep.igtk_len == len &&
  848. os_memcmp(sm->igtk_wnm_sleep.igtk, igtk->igtk,
  849. sm->igtk_wnm_sleep.igtk_len) == 0)) {
  850. wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
  851. "WPA: Not reinstalling already in-use IGTK to the driver (keyidx=%d)",
  852. keyidx);
  853. return 0;
  854. }
  855. wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
  856. "WPA: IGTK keyid %d pn %02x%02x%02x%02x%02x%02x",
  857. keyidx, MAC2STR(igtk->pn));
  858. wpa_hexdump_key(MSG_DEBUG, "WPA: IGTK", igtk->igtk, len);
  859. if (keyidx > 4095) {
  860. wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
  861. "WPA: Invalid IGTK KeyID %d", keyidx);
  862. return -1;
  863. }
  864. if (wpa_sm_set_key(sm, wpa_cipher_to_alg(sm->mgmt_group_cipher),
  865. broadcast_ether_addr,
  866. keyidx, 0, igtk->pn, sizeof(igtk->pn),
  867. igtk->igtk, len) < 0) {
  868. wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
  869. "WPA: Failed to configure IGTK to the driver");
  870. return -1;
  871. }
  872. if (wnm_sleep) {
  873. sm->igtk_wnm_sleep.igtk_len = len;
  874. os_memcpy(sm->igtk_wnm_sleep.igtk, igtk->igtk,
  875. sm->igtk_wnm_sleep.igtk_len);
  876. } else {
  877. sm->igtk.igtk_len = len;
  878. os_memcpy(sm->igtk.igtk, igtk->igtk, sm->igtk.igtk_len);
  879. }
  880. return 0;
  881. }
  882. #endif /* CONFIG_IEEE80211W */
  883. static int ieee80211w_set_keys(struct wpa_sm *sm,
  884. struct wpa_eapol_ie_parse *ie)
  885. {
  886. #ifdef CONFIG_IEEE80211W
  887. if (!wpa_cipher_valid_mgmt_group(sm->mgmt_group_cipher))
  888. return 0;
  889. if (ie->igtk) {
  890. size_t len;
  891. const struct wpa_igtk_kde *igtk;
  892. len = wpa_cipher_key_len(sm->mgmt_group_cipher);
  893. if (ie->igtk_len != WPA_IGTK_KDE_PREFIX_LEN + len)
  894. return -1;
  895. igtk = (const struct wpa_igtk_kde *) ie->igtk;
  896. if (wpa_supplicant_install_igtk(sm, igtk, 0) < 0)
  897. return -1;
  898. }
  899. return 0;
  900. #else /* CONFIG_IEEE80211W */
  901. return 0;
  902. #endif /* CONFIG_IEEE80211W */
  903. }
  904. static void wpa_report_ie_mismatch(struct wpa_sm *sm,
  905. const char *reason, const u8 *src_addr,
  906. const u8 *wpa_ie, size_t wpa_ie_len,
  907. const u8 *rsn_ie, size_t rsn_ie_len)
  908. {
  909. wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, "WPA: %s (src=" MACSTR ")",
  910. reason, MAC2STR(src_addr));
  911. if (sm->ap_wpa_ie) {
  912. wpa_hexdump(MSG_INFO, "WPA: WPA IE in Beacon/ProbeResp",
  913. sm->ap_wpa_ie, sm->ap_wpa_ie_len);
  914. }
  915. if (wpa_ie) {
  916. if (!sm->ap_wpa_ie) {
  917. wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
  918. "WPA: No WPA IE in Beacon/ProbeResp");
  919. }
  920. wpa_hexdump(MSG_INFO, "WPA: WPA IE in 3/4 msg",
  921. wpa_ie, wpa_ie_len);
  922. }
  923. if (sm->ap_rsn_ie) {
  924. wpa_hexdump(MSG_INFO, "WPA: RSN IE in Beacon/ProbeResp",
  925. sm->ap_rsn_ie, sm->ap_rsn_ie_len);
  926. }
  927. if (rsn_ie) {
  928. if (!sm->ap_rsn_ie) {
  929. wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
  930. "WPA: No RSN IE in Beacon/ProbeResp");
  931. }
  932. wpa_hexdump(MSG_INFO, "WPA: RSN IE in 3/4 msg",
  933. rsn_ie, rsn_ie_len);
  934. }
  935. wpa_sm_deauthenticate(sm, WLAN_REASON_IE_IN_4WAY_DIFFERS);
  936. }
  937. #ifdef CONFIG_IEEE80211R
  938. static int ft_validate_mdie(struct wpa_sm *sm,
  939. const unsigned char *src_addr,
  940. struct wpa_eapol_ie_parse *ie,
  941. const u8 *assoc_resp_mdie)
  942. {
  943. struct rsn_mdie *mdie;
  944. mdie = (struct rsn_mdie *) (ie->mdie + 2);
  945. if (ie->mdie == NULL || ie->mdie_len < 2 + sizeof(*mdie) ||
  946. os_memcmp(mdie->mobility_domain, sm->mobility_domain,
  947. MOBILITY_DOMAIN_ID_LEN) != 0) {
  948. wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "FT: MDIE in msg 3/4 did "
  949. "not match with the current mobility domain");
  950. return -1;
  951. }
  952. if (assoc_resp_mdie &&
  953. (assoc_resp_mdie[1] != ie->mdie[1] ||
  954. os_memcmp(assoc_resp_mdie, ie->mdie, 2 + ie->mdie[1]) != 0)) {
  955. wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "FT: MDIE mismatch");
  956. wpa_hexdump(MSG_DEBUG, "FT: MDIE in EAPOL-Key msg 3/4",
  957. ie->mdie, 2 + ie->mdie[1]);
  958. wpa_hexdump(MSG_DEBUG, "FT: MDIE in (Re)Association Response",
  959. assoc_resp_mdie, 2 + assoc_resp_mdie[1]);
  960. return -1;
  961. }
  962. return 0;
  963. }
  964. static int ft_validate_ftie(struct wpa_sm *sm,
  965. const unsigned char *src_addr,
  966. struct wpa_eapol_ie_parse *ie,
  967. const u8 *assoc_resp_ftie)
  968. {
  969. if (ie->ftie == NULL) {
  970. wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
  971. "FT: No FTIE in EAPOL-Key msg 3/4");
  972. return -1;
  973. }
  974. if (assoc_resp_ftie == NULL)
  975. return 0;
  976. if (assoc_resp_ftie[1] != ie->ftie[1] ||
  977. os_memcmp(assoc_resp_ftie, ie->ftie, 2 + ie->ftie[1]) != 0) {
  978. wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "FT: FTIE mismatch");
  979. wpa_hexdump(MSG_DEBUG, "FT: FTIE in EAPOL-Key msg 3/4",
  980. ie->ftie, 2 + ie->ftie[1]);
  981. wpa_hexdump(MSG_DEBUG, "FT: FTIE in (Re)Association Response",
  982. assoc_resp_ftie, 2 + assoc_resp_ftie[1]);
  983. return -1;
  984. }
  985. return 0;
  986. }
  987. static int ft_validate_rsnie(struct wpa_sm *sm,
  988. const unsigned char *src_addr,
  989. struct wpa_eapol_ie_parse *ie)
  990. {
  991. struct wpa_ie_data rsn;
  992. if (!ie->rsn_ie)
  993. return 0;
  994. /*
  995. * Verify that PMKR1Name from EAPOL-Key message 3/4
  996. * matches with the value we derived.
  997. */
  998. if (wpa_parse_wpa_ie_rsn(ie->rsn_ie, ie->rsn_ie_len, &rsn) < 0 ||
  999. rsn.num_pmkid != 1 || rsn.pmkid == NULL) {
  1000. wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "FT: No PMKR1Name in "
  1001. "FT 4-way handshake message 3/4");
  1002. return -1;
  1003. }
  1004. if (os_memcmp_const(rsn.pmkid, sm->pmk_r1_name, WPA_PMK_NAME_LEN) != 0)
  1005. {
  1006. wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
  1007. "FT: PMKR1Name mismatch in "
  1008. "FT 4-way handshake message 3/4");
  1009. wpa_hexdump(MSG_DEBUG, "FT: PMKR1Name from Authenticator",
  1010. rsn.pmkid, WPA_PMK_NAME_LEN);
  1011. wpa_hexdump(MSG_DEBUG, "FT: Derived PMKR1Name",
  1012. sm->pmk_r1_name, WPA_PMK_NAME_LEN);
  1013. return -1;
  1014. }
  1015. return 0;
  1016. }
  1017. static int wpa_supplicant_validate_ie_ft(struct wpa_sm *sm,
  1018. const unsigned char *src_addr,
  1019. struct wpa_eapol_ie_parse *ie)
  1020. {
  1021. const u8 *pos, *end, *mdie = NULL, *ftie = NULL;
  1022. if (sm->assoc_resp_ies) {
  1023. pos = sm->assoc_resp_ies;
  1024. end = pos + sm->assoc_resp_ies_len;
  1025. while (end - pos > 2) {
  1026. if (2 + pos[1] > end - pos)
  1027. break;
  1028. switch (*pos) {
  1029. case WLAN_EID_MOBILITY_DOMAIN:
  1030. mdie = pos;
  1031. break;
  1032. case WLAN_EID_FAST_BSS_TRANSITION:
  1033. ftie = pos;
  1034. break;
  1035. }
  1036. pos += 2 + pos[1];
  1037. }
  1038. }
  1039. if (ft_validate_mdie(sm, src_addr, ie, mdie) < 0 ||
  1040. ft_validate_ftie(sm, src_addr, ie, ftie) < 0 ||
  1041. ft_validate_rsnie(sm, src_addr, ie) < 0)
  1042. return -1;
  1043. return 0;
  1044. }
  1045. #endif /* CONFIG_IEEE80211R */
  1046. static int wpa_supplicant_validate_ie(struct wpa_sm *sm,
  1047. const unsigned char *src_addr,
  1048. struct wpa_eapol_ie_parse *ie)
  1049. {
  1050. if (sm->ap_wpa_ie == NULL && sm->ap_rsn_ie == NULL) {
  1051. wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
  1052. "WPA: No WPA/RSN IE for this AP known. "
  1053. "Trying to get from scan results");
  1054. if (wpa_sm_get_beacon_ie(sm) < 0) {
  1055. wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
  1056. "WPA: Could not find AP from "
  1057. "the scan results");
  1058. } else {
  1059. wpa_msg(sm->ctx->msg_ctx, MSG_DEBUG,
  1060. "WPA: Found the current AP from "
  1061. "updated scan results");
  1062. }
  1063. }
  1064. if (ie->wpa_ie == NULL && ie->rsn_ie == NULL &&
  1065. (sm->ap_wpa_ie || sm->ap_rsn_ie)) {
  1066. wpa_report_ie_mismatch(sm, "IE in 3/4 msg does not match "
  1067. "with IE in Beacon/ProbeResp (no IE?)",
  1068. src_addr, ie->wpa_ie, ie->wpa_ie_len,
  1069. ie->rsn_ie, ie->rsn_ie_len);
  1070. return -1;
  1071. }
  1072. if ((ie->wpa_ie && sm->ap_wpa_ie &&
  1073. (ie->wpa_ie_len != sm->ap_wpa_ie_len ||
  1074. os_memcmp(ie->wpa_ie, sm->ap_wpa_ie, ie->wpa_ie_len) != 0)) ||
  1075. (ie->rsn_ie && sm->ap_rsn_ie &&
  1076. wpa_compare_rsn_ie(wpa_key_mgmt_ft(sm->key_mgmt),
  1077. sm->ap_rsn_ie, sm->ap_rsn_ie_len,
  1078. ie->rsn_ie, ie->rsn_ie_len))) {
  1079. wpa_report_ie_mismatch(sm, "IE in 3/4 msg does not match "
  1080. "with IE in Beacon/ProbeResp",
  1081. src_addr, ie->wpa_ie, ie->wpa_ie_len,
  1082. ie->rsn_ie, ie->rsn_ie_len);
  1083. return -1;
  1084. }
  1085. if (sm->proto == WPA_PROTO_WPA &&
  1086. ie->rsn_ie && sm->ap_rsn_ie == NULL && sm->rsn_enabled) {
  1087. wpa_report_ie_mismatch(sm, "Possible downgrade attack "
  1088. "detected - RSN was enabled and RSN IE "
  1089. "was in msg 3/4, but not in "
  1090. "Beacon/ProbeResp",
  1091. src_addr, ie->wpa_ie, ie->wpa_ie_len,
  1092. ie->rsn_ie, ie->rsn_ie_len);
  1093. return -1;
  1094. }
  1095. #ifdef CONFIG_IEEE80211R
  1096. if (wpa_key_mgmt_ft(sm->key_mgmt) &&
  1097. wpa_supplicant_validate_ie_ft(sm, src_addr, ie) < 0)
  1098. return -1;
  1099. #endif /* CONFIG_IEEE80211R */
  1100. return 0;
  1101. }
  1102. /**
  1103. * wpa_supplicant_send_4_of_4 - Send message 4 of WPA/RSN 4-Way Handshake
  1104. * @sm: Pointer to WPA state machine data from wpa_sm_init()
  1105. * @dst: Destination address for the frame
  1106. * @key: Pointer to the EAPOL-Key frame header
  1107. * @ver: Version bits from EAPOL-Key Key Info
  1108. * @key_info: Key Info
  1109. * @ptk: PTK to use for keyed hash and encryption
  1110. * Returns: >= 0 on success, < 0 on failure
  1111. */
  1112. int wpa_supplicant_send_4_of_4(struct wpa_sm *sm, const unsigned char *dst,
  1113. const struct wpa_eapol_key *key,
  1114. u16 ver, u16 key_info,
  1115. struct wpa_ptk *ptk)
  1116. {
  1117. size_t mic_len, hdrlen, rlen;
  1118. struct wpa_eapol_key *reply;
  1119. u8 *rbuf, *key_mic;
  1120. mic_len = wpa_mic_len(sm->key_mgmt, sm->pmk_len);
  1121. hdrlen = sizeof(*reply) + mic_len + 2;
  1122. rbuf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_KEY, NULL,
  1123. hdrlen, &rlen, (void *) &reply);
  1124. if (rbuf == NULL)
  1125. return -1;
  1126. reply->type = (sm->proto == WPA_PROTO_RSN ||
  1127. sm->proto == WPA_PROTO_OSEN) ?
  1128. EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA;
  1129. key_info &= WPA_KEY_INFO_SECURE;
  1130. key_info |= ver | WPA_KEY_INFO_KEY_TYPE;
  1131. if (mic_len)
  1132. key_info |= WPA_KEY_INFO_MIC;
  1133. else
  1134. key_info |= WPA_KEY_INFO_ENCR_KEY_DATA;
  1135. WPA_PUT_BE16(reply->key_info, key_info);
  1136. if (sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN)
  1137. WPA_PUT_BE16(reply->key_length, 0);
  1138. else
  1139. os_memcpy(reply->key_length, key->key_length, 2);
  1140. os_memcpy(reply->replay_counter, key->replay_counter,
  1141. WPA_REPLAY_COUNTER_LEN);
  1142. key_mic = (u8 *) (reply + 1);
  1143. WPA_PUT_BE16(key_mic + mic_len, 0);
  1144. wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Sending EAPOL-Key 4/4");
  1145. return wpa_eapol_key_send(sm, ptk, ver, dst, ETH_P_EAPOL, rbuf, rlen,
  1146. key_mic);
  1147. }
  1148. static void wpa_supplicant_process_3_of_4(struct wpa_sm *sm,
  1149. const struct wpa_eapol_key *key,
  1150. u16 ver, const u8 *key_data,
  1151. size_t key_data_len)
  1152. {
  1153. u16 key_info, keylen;
  1154. struct wpa_eapol_ie_parse ie;
  1155. wpa_sm_set_state(sm, WPA_4WAY_HANDSHAKE);
  1156. wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: RX message 3 of 4-Way "
  1157. "Handshake from " MACSTR " (ver=%d)", MAC2STR(sm->bssid), ver);
  1158. key_info = WPA_GET_BE16(key->key_info);
  1159. wpa_hexdump(MSG_DEBUG, "WPA: IE KeyData", key_data, key_data_len);
  1160. if (wpa_supplicant_parse_ies(key_data, key_data_len, &ie) < 0)
  1161. goto failed;
  1162. if (ie.gtk && !(key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
  1163. wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
  1164. "WPA: GTK IE in unencrypted key data");
  1165. goto failed;
  1166. }
  1167. #ifdef CONFIG_IEEE80211W
  1168. if (ie.igtk && !(key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
  1169. wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
  1170. "WPA: IGTK KDE in unencrypted key data");
  1171. goto failed;
  1172. }
  1173. if (ie.igtk &&
  1174. wpa_cipher_valid_mgmt_group(sm->mgmt_group_cipher) &&
  1175. ie.igtk_len != WPA_IGTK_KDE_PREFIX_LEN +
  1176. (unsigned int) wpa_cipher_key_len(sm->mgmt_group_cipher)) {
  1177. wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
  1178. "WPA: Invalid IGTK KDE length %lu",
  1179. (unsigned long) ie.igtk_len);
  1180. goto failed;
  1181. }
  1182. #endif /* CONFIG_IEEE80211W */
  1183. if (wpa_supplicant_validate_ie(sm, sm->bssid, &ie) < 0)
  1184. goto failed;
  1185. if (os_memcmp(sm->anonce, key->key_nonce, WPA_NONCE_LEN) != 0) {
  1186. wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
  1187. "WPA: ANonce from message 1 of 4-Way Handshake "
  1188. "differs from 3 of 4-Way Handshake - drop packet (src="
  1189. MACSTR ")", MAC2STR(sm->bssid));
  1190. goto failed;
  1191. }
  1192. keylen = WPA_GET_BE16(key->key_length);
  1193. if (keylen != wpa_cipher_key_len(sm->pairwise_cipher)) {
  1194. wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
  1195. "WPA: Invalid %s key length %d (src=" MACSTR
  1196. ")", wpa_cipher_txt(sm->pairwise_cipher), keylen,
  1197. MAC2STR(sm->bssid));
  1198. goto failed;
  1199. }
  1200. #ifdef CONFIG_P2P
  1201. if (ie.ip_addr_alloc) {
  1202. os_memcpy(sm->p2p_ip_addr, ie.ip_addr_alloc, 3 * 4);
  1203. wpa_hexdump(MSG_DEBUG, "P2P: IP address info",
  1204. sm->p2p_ip_addr, sizeof(sm->p2p_ip_addr));
  1205. }
  1206. #endif /* CONFIG_P2P */
  1207. if (wpa_supplicant_send_4_of_4(sm, sm->bssid, key, ver, key_info,
  1208. &sm->ptk) < 0) {
  1209. goto failed;
  1210. }
  1211. /* SNonce was successfully used in msg 3/4, so mark it to be renewed
  1212. * for the next 4-Way Handshake. If msg 3 is received again, the old
  1213. * SNonce will still be used to avoid changing PTK. */
  1214. sm->renew_snonce = 1;
  1215. if (key_info & WPA_KEY_INFO_INSTALL) {
  1216. if (wpa_supplicant_install_ptk(sm, key))
  1217. goto failed;
  1218. }
  1219. if (key_info & WPA_KEY_INFO_SECURE) {
  1220. wpa_sm_mlme_setprotection(
  1221. sm, sm->bssid, MLME_SETPROTECTION_PROTECT_TYPE_RX,
  1222. MLME_SETPROTECTION_KEY_TYPE_PAIRWISE);
  1223. eapol_sm_notify_portValid(sm->eapol, TRUE);
  1224. }
  1225. wpa_sm_set_state(sm, WPA_GROUP_HANDSHAKE);
  1226. if (sm->group_cipher == WPA_CIPHER_GTK_NOT_USED) {
  1227. wpa_supplicant_key_neg_complete(sm, sm->bssid,
  1228. key_info & WPA_KEY_INFO_SECURE);
  1229. } else if (ie.gtk &&
  1230. wpa_supplicant_pairwise_gtk(sm, key,
  1231. ie.gtk, ie.gtk_len, key_info) < 0) {
  1232. wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
  1233. "RSN: Failed to configure GTK");
  1234. goto failed;
  1235. }
  1236. if (ieee80211w_set_keys(sm, &ie) < 0) {
  1237. wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
  1238. "RSN: Failed to configure IGTK");
  1239. goto failed;
  1240. }
  1241. if (ie.gtk)
  1242. wpa_sm_set_rekey_offload(sm);
  1243. if (sm->proto == WPA_PROTO_RSN && wpa_key_mgmt_suite_b(sm->key_mgmt)) {
  1244. struct rsn_pmksa_cache_entry *sa;
  1245. sa = pmksa_cache_add(sm->pmksa, sm->pmk, sm->pmk_len, NULL,
  1246. sm->ptk.kck, sm->ptk.kck_len,
  1247. sm->bssid, sm->own_addr,
  1248. sm->network_ctx, sm->key_mgmt, NULL);
  1249. if (!sm->cur_pmksa)
  1250. sm->cur_pmksa = sa;
  1251. }
  1252. sm->msg_3_of_4_ok = 1;
  1253. return;
  1254. failed:
  1255. wpa_sm_deauthenticate(sm, WLAN_REASON_UNSPECIFIED);
  1256. }
  1257. static int wpa_supplicant_process_1_of_2_rsn(struct wpa_sm *sm,
  1258. const u8 *keydata,
  1259. size_t keydatalen,
  1260. u16 key_info,
  1261. struct wpa_gtk_data *gd)
  1262. {
  1263. int maxkeylen;
  1264. struct wpa_eapol_ie_parse ie;
  1265. wpa_hexdump_key(MSG_DEBUG, "RSN: msg 1/2 key data",
  1266. keydata, keydatalen);
  1267. if (wpa_supplicant_parse_ies(keydata, keydatalen, &ie) < 0)
  1268. return -1;
  1269. if (ie.gtk && !(key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
  1270. wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
  1271. "WPA: GTK IE in unencrypted key data");
  1272. return -1;
  1273. }
  1274. if (ie.gtk == NULL) {
  1275. wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
  1276. "WPA: No GTK IE in Group Key msg 1/2");
  1277. return -1;
  1278. }
  1279. maxkeylen = gd->gtk_len = ie.gtk_len - 2;
  1280. if (wpa_supplicant_check_group_cipher(sm, sm->group_cipher,
  1281. gd->gtk_len, maxkeylen,
  1282. &gd->key_rsc_len, &gd->alg))
  1283. return -1;
  1284. wpa_hexdump_key(MSG_DEBUG, "RSN: received GTK in group key handshake",
  1285. ie.gtk, ie.gtk_len);
  1286. gd->keyidx = ie.gtk[0] & 0x3;
  1287. gd->tx = wpa_supplicant_gtk_tx_bit_workaround(sm,
  1288. !!(ie.gtk[0] & BIT(2)));
  1289. if (ie.gtk_len - 2 > sizeof(gd->gtk)) {
  1290. wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
  1291. "RSN: Too long GTK in GTK IE (len=%lu)",
  1292. (unsigned long) ie.gtk_len - 2);
  1293. return -1;
  1294. }
  1295. os_memcpy(gd->gtk, ie.gtk + 2, ie.gtk_len - 2);
  1296. if (ieee80211w_set_keys(sm, &ie) < 0)
  1297. wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
  1298. "RSN: Failed to configure IGTK");
  1299. return 0;
  1300. }
  1301. static int wpa_supplicant_process_1_of_2_wpa(struct wpa_sm *sm,
  1302. const struct wpa_eapol_key *key,
  1303. const u8 *key_data,
  1304. size_t key_data_len, u16 key_info,
  1305. u16 ver, struct wpa_gtk_data *gd)
  1306. {
  1307. size_t maxkeylen;
  1308. u16 gtk_len;
  1309. gtk_len = WPA_GET_BE16(key->key_length);
  1310. maxkeylen = key_data_len;
  1311. if (ver == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
  1312. if (maxkeylen < 8) {
  1313. wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
  1314. "WPA: Too short maxkeylen (%lu)",
  1315. (unsigned long) maxkeylen);
  1316. return -1;
  1317. }
  1318. maxkeylen -= 8;
  1319. }
  1320. if (gtk_len > maxkeylen ||
  1321. wpa_supplicant_check_group_cipher(sm, sm->group_cipher,
  1322. gtk_len, maxkeylen,
  1323. &gd->key_rsc_len, &gd->alg))
  1324. return -1;
  1325. gd->gtk_len = gtk_len;
  1326. gd->keyidx = (key_info & WPA_KEY_INFO_KEY_INDEX_MASK) >>
  1327. WPA_KEY_INFO_KEY_INDEX_SHIFT;
  1328. if (ver == WPA_KEY_INFO_TYPE_HMAC_MD5_RC4 && sm->ptk.kek_len == 16) {
  1329. #ifdef CONFIG_NO_RC4
  1330. wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
  1331. "WPA: RC4 not supported in the build");
  1332. return -1;
  1333. #else /* CONFIG_NO_RC4 */
  1334. u8 ek[32];
  1335. if (key_data_len > sizeof(gd->gtk)) {
  1336. wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
  1337. "WPA: RC4 key data too long (%lu)",
  1338. (unsigned long) key_data_len);
  1339. return -1;
  1340. }
  1341. os_memcpy(ek, key->key_iv, 16);
  1342. os_memcpy(ek + 16, sm->ptk.kek, sm->ptk.kek_len);
  1343. os_memcpy(gd->gtk, key_data, key_data_len);
  1344. if (rc4_skip(ek, 32, 256, gd->gtk, key_data_len)) {
  1345. os_memset(ek, 0, sizeof(ek));
  1346. wpa_msg(sm->ctx->msg_ctx, MSG_ERROR,
  1347. "WPA: RC4 failed");
  1348. return -1;
  1349. }
  1350. os_memset(ek, 0, sizeof(ek));
  1351. #endif /* CONFIG_NO_RC4 */
  1352. } else if (ver == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
  1353. if (maxkeylen % 8) {
  1354. wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
  1355. "WPA: Unsupported AES-WRAP len %lu",
  1356. (unsigned long) maxkeylen);
  1357. return -1;
  1358. }
  1359. if (maxkeylen > sizeof(gd->gtk)) {
  1360. wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
  1361. "WPA: AES-WRAP key data "
  1362. "too long (keydatalen=%lu maxkeylen=%lu)",
  1363. (unsigned long) key_data_len,
  1364. (unsigned long) maxkeylen);
  1365. return -1;
  1366. }
  1367. if (aes_unwrap(sm->ptk.kek, sm->ptk.kek_len, maxkeylen / 8,
  1368. key_data, gd->gtk)) {
  1369. wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
  1370. "WPA: AES unwrap failed - could not decrypt "
  1371. "GTK");
  1372. return -1;
  1373. }
  1374. } else {
  1375. wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
  1376. "WPA: Unsupported key_info type %d", ver);
  1377. return -1;
  1378. }
  1379. gd->tx = wpa_supplicant_gtk_tx_bit_workaround(
  1380. sm, !!(key_info & WPA_KEY_INFO_TXRX));
  1381. return 0;
  1382. }
  1383. static int wpa_supplicant_send_2_of_2(struct wpa_sm *sm,
  1384. const struct wpa_eapol_key *key,
  1385. int ver, u16 key_info)
  1386. {
  1387. size_t mic_len, hdrlen, rlen;
  1388. struct wpa_eapol_key *reply;
  1389. u8 *rbuf, *key_mic;
  1390. mic_len = wpa_mic_len(sm->key_mgmt, sm->pmk_len);
  1391. hdrlen = sizeof(*reply) + mic_len + 2;
  1392. rbuf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_KEY, NULL,
  1393. hdrlen, &rlen, (void *) &reply);
  1394. if (rbuf == NULL)
  1395. return -1;
  1396. reply->type = (sm->proto == WPA_PROTO_RSN ||
  1397. sm->proto == WPA_PROTO_OSEN) ?
  1398. EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA;
  1399. key_info &= WPA_KEY_INFO_KEY_INDEX_MASK;
  1400. key_info |= ver | WPA_KEY_INFO_SECURE;
  1401. if (mic_len)
  1402. key_info |= WPA_KEY_INFO_MIC;
  1403. else
  1404. key_info |= WPA_KEY_INFO_ENCR_KEY_DATA;
  1405. WPA_PUT_BE16(reply->key_info, key_info);
  1406. if (sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN)
  1407. WPA_PUT_BE16(reply->key_length, 0);
  1408. else
  1409. os_memcpy(reply->key_length, key->key_length, 2);
  1410. os_memcpy(reply->replay_counter, key->replay_counter,
  1411. WPA_REPLAY_COUNTER_LEN);
  1412. key_mic = (u8 *) (reply + 1);
  1413. WPA_PUT_BE16(key_mic + mic_len, 0);
  1414. wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Sending EAPOL-Key 2/2");
  1415. return wpa_eapol_key_send(sm, &sm->ptk, ver, sm->bssid, ETH_P_EAPOL,
  1416. rbuf, rlen, key_mic);
  1417. }
  1418. static void wpa_supplicant_process_1_of_2(struct wpa_sm *sm,
  1419. const unsigned char *src_addr,
  1420. const struct wpa_eapol_key *key,
  1421. const u8 *key_data,
  1422. size_t key_data_len, u16 ver)
  1423. {
  1424. u16 key_info;
  1425. int rekey, ret;
  1426. struct wpa_gtk_data gd;
  1427. const u8 *key_rsc;
  1428. if (!sm->msg_3_of_4_ok && !wpa_fils_is_completed(sm)) {
  1429. wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
  1430. "WPA: Group Key Handshake started prior to completion of 4-way handshake");
  1431. goto failed;
  1432. }
  1433. os_memset(&gd, 0, sizeof(gd));
  1434. rekey = wpa_sm_get_state(sm) == WPA_COMPLETED;
  1435. wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: RX message 1 of Group Key "
  1436. "Handshake from " MACSTR " (ver=%d)", MAC2STR(src_addr), ver);
  1437. key_info = WPA_GET_BE16(key->key_info);
  1438. if (sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN) {
  1439. ret = wpa_supplicant_process_1_of_2_rsn(sm, key_data,
  1440. key_data_len, key_info,
  1441. &gd);
  1442. } else {
  1443. ret = wpa_supplicant_process_1_of_2_wpa(sm, key, key_data,
  1444. key_data_len,
  1445. key_info, ver, &gd);
  1446. }
  1447. wpa_sm_set_state(sm, WPA_GROUP_HANDSHAKE);
  1448. if (ret)
  1449. goto failed;
  1450. key_rsc = key->key_rsc;
  1451. if (wpa_supplicant_rsc_relaxation(sm, key->key_rsc))
  1452. key_rsc = null_rsc;
  1453. if (wpa_supplicant_install_gtk(sm, &gd, key_rsc, 0) ||
  1454. wpa_supplicant_send_2_of_2(sm, key, ver, key_info) < 0)
  1455. goto failed;
  1456. os_memset(&gd, 0, sizeof(gd));
  1457. if (rekey) {
  1458. wpa_msg(sm->ctx->msg_ctx, MSG_INFO, "WPA: Group rekeying "
  1459. "completed with " MACSTR " [GTK=%s]",
  1460. MAC2STR(sm->bssid), wpa_cipher_txt(sm->group_cipher));
  1461. wpa_sm_cancel_auth_timeout(sm);
  1462. wpa_sm_set_state(sm, WPA_COMPLETED);
  1463. } else {
  1464. wpa_supplicant_key_neg_complete(sm, sm->bssid,
  1465. key_info &
  1466. WPA_KEY_INFO_SECURE);
  1467. }
  1468. wpa_sm_set_rekey_offload(sm);
  1469. return;
  1470. failed:
  1471. os_memset(&gd, 0, sizeof(gd));
  1472. wpa_sm_deauthenticate(sm, WLAN_REASON_UNSPECIFIED);
  1473. }
  1474. static int wpa_supplicant_verify_eapol_key_mic(struct wpa_sm *sm,
  1475. struct wpa_eapol_key *key,
  1476. u16 ver,
  1477. const u8 *buf, size_t len)
  1478. {
  1479. u8 mic[WPA_EAPOL_KEY_MIC_MAX_LEN];
  1480. int ok = 0;
  1481. size_t mic_len = wpa_mic_len(sm->key_mgmt, sm->pmk_len);
  1482. os_memcpy(mic, key + 1, mic_len);
  1483. if (sm->tptk_set) {
  1484. os_memset(key + 1, 0, mic_len);
  1485. wpa_eapol_key_mic(sm->tptk.kck, sm->tptk.kck_len, sm->key_mgmt,
  1486. ver, buf, len, (u8 *) (key + 1));
  1487. if (os_memcmp_const(mic, key + 1, mic_len) != 0) {
  1488. wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
  1489. "WPA: Invalid EAPOL-Key MIC "
  1490. "when using TPTK - ignoring TPTK");
  1491. } else {
  1492. ok = 1;
  1493. sm->tptk_set = 0;
  1494. sm->ptk_set = 1;
  1495. os_memcpy(&sm->ptk, &sm->tptk, sizeof(sm->ptk));
  1496. os_memset(&sm->tptk, 0, sizeof(sm->tptk));
  1497. /*
  1498. * This assures the same TPTK in sm->tptk can never be
  1499. * copied twice to sm->pkt as the new PTK. In
  1500. * combination with the installed flag in the wpa_ptk
  1501. * struct, this assures the same PTK is only installed
  1502. * once.
  1503. */
  1504. sm->renew_snonce = 1;
  1505. }
  1506. }
  1507. if (!ok && sm->ptk_set) {
  1508. os_memset(key + 1, 0, mic_len);
  1509. wpa_eapol_key_mic(sm->ptk.kck, sm->ptk.kck_len, sm->key_mgmt,
  1510. ver, buf, len, (u8 *) (key + 1));
  1511. if (os_memcmp_const(mic, key + 1, mic_len) != 0) {
  1512. wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
  1513. "WPA: Invalid EAPOL-Key MIC - "
  1514. "dropping packet");
  1515. return -1;
  1516. }
  1517. ok = 1;
  1518. }
  1519. if (!ok) {
  1520. wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
  1521. "WPA: Could not verify EAPOL-Key MIC - "
  1522. "dropping packet");
  1523. return -1;
  1524. }
  1525. os_memcpy(sm->rx_replay_counter, key->replay_counter,
  1526. WPA_REPLAY_COUNTER_LEN);
  1527. sm->rx_replay_counter_set = 1;
  1528. return 0;
  1529. }
  1530. /* Decrypt RSN EAPOL-Key key data (RC4 or AES-WRAP) */
  1531. static int wpa_supplicant_decrypt_key_data(struct wpa_sm *sm,
  1532. struct wpa_eapol_key *key,
  1533. size_t mic_len, u16 ver,
  1534. u8 *key_data, size_t *key_data_len)
  1535. {
  1536. wpa_hexdump(MSG_DEBUG, "RSN: encrypted key data",
  1537. key_data, *key_data_len);
  1538. if (!sm->ptk_set) {
  1539. wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
  1540. "WPA: PTK not available, cannot decrypt EAPOL-Key Key "
  1541. "Data");
  1542. return -1;
  1543. }
  1544. /* Decrypt key data here so that this operation does not need
  1545. * to be implemented separately for each message type. */
  1546. if (ver == WPA_KEY_INFO_TYPE_HMAC_MD5_RC4 && sm->ptk.kek_len == 16) {
  1547. #ifdef CONFIG_NO_RC4
  1548. wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
  1549. "WPA: RC4 not supported in the build");
  1550. return -1;
  1551. #else /* CONFIG_NO_RC4 */
  1552. u8 ek[32];
  1553. wpa_printf(MSG_DEBUG, "WPA: Decrypt Key Data using RC4");
  1554. os_memcpy(ek, key->key_iv, 16);
  1555. os_memcpy(ek + 16, sm->ptk.kek, sm->ptk.kek_len);
  1556. if (rc4_skip(ek, 32, 256, key_data, *key_data_len)) {
  1557. os_memset(ek, 0, sizeof(ek));
  1558. wpa_msg(sm->ctx->msg_ctx, MSG_ERROR,
  1559. "WPA: RC4 failed");
  1560. return -1;
  1561. }
  1562. os_memset(ek, 0, sizeof(ek));
  1563. #endif /* CONFIG_NO_RC4 */
  1564. } else if (ver == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES ||
  1565. ver == WPA_KEY_INFO_TYPE_AES_128_CMAC ||
  1566. sm->key_mgmt == WPA_KEY_MGMT_OWE ||
  1567. sm->key_mgmt == WPA_KEY_MGMT_DPP ||
  1568. sm->key_mgmt == WPA_KEY_MGMT_OSEN ||
  1569. wpa_key_mgmt_suite_b(sm->key_mgmt)) {
  1570. u8 *buf;
  1571. wpa_printf(MSG_DEBUG,
  1572. "WPA: Decrypt Key Data using AES-UNWRAP (KEK length %u)",
  1573. (unsigned int) sm->ptk.kek_len);
  1574. if (*key_data_len < 8 || *key_data_len % 8) {
  1575. wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
  1576. "WPA: Unsupported AES-WRAP len %u",
  1577. (unsigned int) *key_data_len);
  1578. return -1;
  1579. }
  1580. *key_data_len -= 8; /* AES-WRAP adds 8 bytes */
  1581. buf = os_malloc(*key_data_len);
  1582. if (buf == NULL) {
  1583. wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
  1584. "WPA: No memory for AES-UNWRAP buffer");
  1585. return -1;
  1586. }
  1587. if (aes_unwrap(sm->ptk.kek, sm->ptk.kek_len, *key_data_len / 8,
  1588. key_data, buf)) {
  1589. bin_clear_free(buf, *key_data_len);
  1590. wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
  1591. "WPA: AES unwrap failed - "
  1592. "could not decrypt EAPOL-Key key data");
  1593. return -1;
  1594. }
  1595. os_memcpy(key_data, buf, *key_data_len);
  1596. bin_clear_free(buf, *key_data_len);
  1597. WPA_PUT_BE16(((u8 *) (key + 1)) + mic_len, *key_data_len);
  1598. } else {
  1599. wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
  1600. "WPA: Unsupported key_info type %d", ver);
  1601. return -1;
  1602. }
  1603. wpa_hexdump_key(MSG_DEBUG, "WPA: decrypted EAPOL-Key key data",
  1604. key_data, *key_data_len);
  1605. return 0;
  1606. }
  1607. /**
  1608. * wpa_sm_aborted_cached - Notify WPA that PMKSA caching was aborted
  1609. * @sm: Pointer to WPA state machine data from wpa_sm_init()
  1610. */
  1611. void wpa_sm_aborted_cached(struct wpa_sm *sm)
  1612. {
  1613. if (sm && sm->cur_pmksa) {
  1614. wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
  1615. "RSN: Cancelling PMKSA caching attempt");
  1616. sm->cur_pmksa = NULL;
  1617. }
  1618. }
  1619. static void wpa_eapol_key_dump(struct wpa_sm *sm,
  1620. const struct wpa_eapol_key *key,
  1621. unsigned int key_data_len,
  1622. const u8 *mic, unsigned int mic_len)
  1623. {
  1624. #ifndef CONFIG_NO_STDOUT_DEBUG
  1625. u16 key_info = WPA_GET_BE16(key->key_info);
  1626. wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, " EAPOL-Key type=%d", key->type);
  1627. wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
  1628. " key_info 0x%x (ver=%d keyidx=%d rsvd=%d %s%s%s%s%s%s%s%s)",
  1629. key_info, key_info & WPA_KEY_INFO_TYPE_MASK,
  1630. (key_info & WPA_KEY_INFO_KEY_INDEX_MASK) >>
  1631. WPA_KEY_INFO_KEY_INDEX_SHIFT,
  1632. (key_info & (BIT(13) | BIT(14) | BIT(15))) >> 13,
  1633. key_info & WPA_KEY_INFO_KEY_TYPE ? "Pairwise" : "Group",
  1634. key_info & WPA_KEY_INFO_INSTALL ? " Install" : "",
  1635. key_info & WPA_KEY_INFO_ACK ? " Ack" : "",
  1636. key_info & WPA_KEY_INFO_MIC ? " MIC" : "",
  1637. key_info & WPA_KEY_INFO_SECURE ? " Secure" : "",
  1638. key_info & WPA_KEY_INFO_ERROR ? " Error" : "",
  1639. key_info & WPA_KEY_INFO_REQUEST ? " Request" : "",
  1640. key_info & WPA_KEY_INFO_ENCR_KEY_DATA ? " Encr" : "");
  1641. wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
  1642. " key_length=%u key_data_length=%u",
  1643. WPA_GET_BE16(key->key_length), key_data_len);
  1644. wpa_hexdump(MSG_DEBUG, " replay_counter",
  1645. key->replay_counter, WPA_REPLAY_COUNTER_LEN);
  1646. wpa_hexdump(MSG_DEBUG, " key_nonce", key->key_nonce, WPA_NONCE_LEN);
  1647. wpa_hexdump(MSG_DEBUG, " key_iv", key->key_iv, 16);
  1648. wpa_hexdump(MSG_DEBUG, " key_rsc", key->key_rsc, 8);
  1649. wpa_hexdump(MSG_DEBUG, " key_id (reserved)", key->key_id, 8);
  1650. wpa_hexdump(MSG_DEBUG, " key_mic", mic, mic_len);
  1651. #endif /* CONFIG_NO_STDOUT_DEBUG */
  1652. }
  1653. #ifdef CONFIG_FILS
  1654. static int wpa_supp_aead_decrypt(struct wpa_sm *sm, u8 *buf, size_t buf_len,
  1655. size_t *key_data_len)
  1656. {
  1657. struct wpa_ptk *ptk;
  1658. struct ieee802_1x_hdr *hdr;
  1659. struct wpa_eapol_key *key;
  1660. u8 *pos, *tmp;
  1661. const u8 *aad[1];
  1662. size_t aad_len[1];
  1663. if (*key_data_len < AES_BLOCK_SIZE) {
  1664. wpa_printf(MSG_INFO, "No room for AES-SIV data in the frame");
  1665. return -1;
  1666. }
  1667. if (sm->tptk_set)
  1668. ptk = &sm->tptk;
  1669. else if (sm->ptk_set)
  1670. ptk = &sm->ptk;
  1671. else
  1672. return -1;
  1673. hdr = (struct ieee802_1x_hdr *) buf;
  1674. key = (struct wpa_eapol_key *) (hdr + 1);
  1675. pos = (u8 *) (key + 1);
  1676. pos += 2; /* Pointing at the Encrypted Key Data field */
  1677. tmp = os_malloc(*key_data_len);
  1678. if (!tmp)
  1679. return -1;
  1680. /* AES-SIV AAD from EAPOL protocol version field (inclusive) to
  1681. * to Key Data (exclusive). */
  1682. aad[0] = buf;
  1683. aad_len[0] = pos - buf;
  1684. if (aes_siv_decrypt(ptk->kek, ptk->kek_len, pos, *key_data_len,
  1685. 1, aad, aad_len, tmp) < 0) {
  1686. wpa_printf(MSG_INFO, "Invalid AES-SIV data in the frame");
  1687. bin_clear_free(tmp, *key_data_len);
  1688. return -1;
  1689. }
  1690. /* AEAD decryption and validation completed successfully */
  1691. (*key_data_len) -= AES_BLOCK_SIZE;
  1692. wpa_hexdump_key(MSG_DEBUG, "WPA: Decrypted Key Data",
  1693. tmp, *key_data_len);
  1694. /* Replace Key Data field with the decrypted version */
  1695. os_memcpy(pos, tmp, *key_data_len);
  1696. pos -= 2; /* Key Data Length field */
  1697. WPA_PUT_BE16(pos, *key_data_len);
  1698. bin_clear_free(tmp, *key_data_len);
  1699. if (sm->tptk_set) {
  1700. sm->tptk_set = 0;
  1701. sm->ptk_set = 1;
  1702. os_memcpy(&sm->ptk, &sm->tptk, sizeof(sm->ptk));
  1703. os_memset(&sm->tptk, 0, sizeof(sm->tptk));
  1704. }
  1705. os_memcpy(sm->rx_replay_counter, key->replay_counter,
  1706. WPA_REPLAY_COUNTER_LEN);
  1707. sm->rx_replay_counter_set = 1;
  1708. return 0;
  1709. }
  1710. #endif /* CONFIG_FILS */
  1711. /**
  1712. * wpa_sm_rx_eapol - Process received WPA EAPOL frames
  1713. * @sm: Pointer to WPA state machine data from wpa_sm_init()
  1714. * @src_addr: Source MAC address of the EAPOL packet
  1715. * @buf: Pointer to the beginning of the EAPOL data (EAPOL header)
  1716. * @len: Length of the EAPOL frame
  1717. * Returns: 1 = WPA EAPOL-Key processed, 0 = not a WPA EAPOL-Key, -1 failure
  1718. *
  1719. * This function is called for each received EAPOL frame. Other than EAPOL-Key
  1720. * frames can be skipped if filtering is done elsewhere. wpa_sm_rx_eapol() is
  1721. * only processing WPA and WPA2 EAPOL-Key frames.
  1722. *
  1723. * The received EAPOL-Key packets are validated and valid packets are replied
  1724. * to. In addition, key material (PTK, GTK) is configured at the end of a
  1725. * successful key handshake.
  1726. */
  1727. int wpa_sm_rx_eapol(struct wpa_sm *sm, const u8 *src_addr,
  1728. const u8 *buf, size_t len)
  1729. {
  1730. size_t plen, data_len, key_data_len;
  1731. const struct ieee802_1x_hdr *hdr;
  1732. struct wpa_eapol_key *key;
  1733. u16 key_info, ver;
  1734. u8 *tmp = NULL;
  1735. int ret = -1;
  1736. u8 *mic, *key_data;
  1737. size_t mic_len, keyhdrlen;
  1738. #ifdef CONFIG_IEEE80211R
  1739. sm->ft_completed = 0;
  1740. #endif /* CONFIG_IEEE80211R */
  1741. mic_len = wpa_mic_len(sm->key_mgmt, sm->pmk_len);
  1742. keyhdrlen = sizeof(*key) + mic_len + 2;
  1743. if (len < sizeof(*hdr) + keyhdrlen) {
  1744. wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
  1745. "WPA: EAPOL frame too short to be a WPA "
  1746. "EAPOL-Key (len %lu, expecting at least %lu)",
  1747. (unsigned long) len,
  1748. (unsigned long) sizeof(*hdr) + keyhdrlen);
  1749. return 0;
  1750. }
  1751. hdr = (const struct ieee802_1x_hdr *) buf;
  1752. plen = be_to_host16(hdr->length);
  1753. data_len = plen + sizeof(*hdr);
  1754. wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
  1755. "IEEE 802.1X RX: version=%d type=%d length=%lu",
  1756. hdr->version, hdr->type, (unsigned long) plen);
  1757. if (hdr->version < EAPOL_VERSION) {
  1758. /* TODO: backwards compatibility */
  1759. }
  1760. if (hdr->type != IEEE802_1X_TYPE_EAPOL_KEY) {
  1761. wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
  1762. "WPA: EAPOL frame (type %u) discarded, "
  1763. "not a Key frame", hdr->type);
  1764. ret = 0;
  1765. goto out;
  1766. }
  1767. wpa_hexdump(MSG_MSGDUMP, "WPA: RX EAPOL-Key", buf, len);
  1768. if (plen > len - sizeof(*hdr) || plen < keyhdrlen) {
  1769. wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
  1770. "WPA: EAPOL frame payload size %lu "
  1771. "invalid (frame size %lu)",
  1772. (unsigned long) plen, (unsigned long) len);
  1773. ret = 0;
  1774. goto out;
  1775. }
  1776. if (data_len < len) {
  1777. wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
  1778. "WPA: ignoring %lu bytes after the IEEE 802.1X data",
  1779. (unsigned long) len - data_len);
  1780. }
  1781. /*
  1782. * Make a copy of the frame since we need to modify the buffer during
  1783. * MAC validation and Key Data decryption.
  1784. */
  1785. tmp = os_memdup(buf, data_len);
  1786. if (tmp == NULL)
  1787. goto out;
  1788. key = (struct wpa_eapol_key *) (tmp + sizeof(struct ieee802_1x_hdr));
  1789. mic = (u8 *) (key + 1);
  1790. key_data = mic + mic_len + 2;
  1791. if (key->type != EAPOL_KEY_TYPE_WPA && key->type != EAPOL_KEY_TYPE_RSN)
  1792. {
  1793. wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
  1794. "WPA: EAPOL-Key type (%d) unknown, discarded",
  1795. key->type);
  1796. ret = 0;
  1797. goto out;
  1798. }
  1799. key_data_len = WPA_GET_BE16(mic + mic_len);
  1800. wpa_eapol_key_dump(sm, key, key_data_len, mic, mic_len);
  1801. if (key_data_len > plen - keyhdrlen) {
  1802. wpa_msg(sm->ctx->msg_ctx, MSG_INFO, "WPA: Invalid EAPOL-Key "
  1803. "frame - key_data overflow (%u > %u)",
  1804. (unsigned int) key_data_len,
  1805. (unsigned int) (plen - keyhdrlen));
  1806. goto out;
  1807. }
  1808. eapol_sm_notify_lower_layer_success(sm->eapol, 0);
  1809. key_info = WPA_GET_BE16(key->key_info);
  1810. ver = key_info & WPA_KEY_INFO_TYPE_MASK;
  1811. if (ver != WPA_KEY_INFO_TYPE_HMAC_MD5_RC4 &&
  1812. #if defined(CONFIG_IEEE80211R) || defined(CONFIG_IEEE80211W)
  1813. ver != WPA_KEY_INFO_TYPE_AES_128_CMAC &&
  1814. #endif /* CONFIG_IEEE80211R || CONFIG_IEEE80211W */
  1815. ver != WPA_KEY_INFO_TYPE_HMAC_SHA1_AES &&
  1816. !wpa_key_mgmt_suite_b(sm->key_mgmt) &&
  1817. !wpa_key_mgmt_fils(sm->key_mgmt) &&
  1818. sm->key_mgmt != WPA_KEY_MGMT_OWE &&
  1819. sm->key_mgmt != WPA_KEY_MGMT_DPP &&
  1820. sm->key_mgmt != WPA_KEY_MGMT_OSEN) {
  1821. wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
  1822. "WPA: Unsupported EAPOL-Key descriptor version %d",
  1823. ver);
  1824. goto out;
  1825. }
  1826. if (sm->key_mgmt == WPA_KEY_MGMT_OSEN &&
  1827. ver != WPA_KEY_INFO_TYPE_AKM_DEFINED) {
  1828. wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
  1829. "OSEN: Unsupported EAPOL-Key descriptor version %d",
  1830. ver);
  1831. goto out;
  1832. }
  1833. if ((wpa_key_mgmt_suite_b(sm->key_mgmt) ||
  1834. wpa_key_mgmt_fils(sm->key_mgmt) ||
  1835. sm->key_mgmt == WPA_KEY_MGMT_DPP ||
  1836. sm->key_mgmt == WPA_KEY_MGMT_OWE) &&
  1837. ver != WPA_KEY_INFO_TYPE_AKM_DEFINED) {
  1838. wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
  1839. "RSN: Unsupported EAPOL-Key descriptor version %d (expected AKM defined = 0)",
  1840. ver);
  1841. goto out;
  1842. }
  1843. #ifdef CONFIG_IEEE80211R
  1844. if (wpa_key_mgmt_ft(sm->key_mgmt)) {
  1845. /* IEEE 802.11r uses a new key_info type (AES-128-CMAC). */
  1846. if (ver != WPA_KEY_INFO_TYPE_AES_128_CMAC) {
  1847. wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
  1848. "FT: AP did not use AES-128-CMAC");
  1849. goto out;
  1850. }
  1851. } else
  1852. #endif /* CONFIG_IEEE80211R */
  1853. #ifdef CONFIG_IEEE80211W
  1854. if (wpa_key_mgmt_sha256(sm->key_mgmt)) {
  1855. if (ver != WPA_KEY_INFO_TYPE_AES_128_CMAC &&
  1856. sm->key_mgmt != WPA_KEY_MGMT_OSEN &&
  1857. !wpa_key_mgmt_fils(sm->key_mgmt) &&
  1858. !wpa_key_mgmt_suite_b(sm->key_mgmt)) {
  1859. wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
  1860. "WPA: AP did not use the "
  1861. "negotiated AES-128-CMAC");
  1862. goto out;
  1863. }
  1864. } else
  1865. #endif /* CONFIG_IEEE80211W */
  1866. if (sm->pairwise_cipher == WPA_CIPHER_CCMP &&
  1867. !wpa_key_mgmt_suite_b(sm->key_mgmt) &&
  1868. !wpa_key_mgmt_fils(sm->key_mgmt) &&
  1869. sm->key_mgmt != WPA_KEY_MGMT_OWE &&
  1870. sm->key_mgmt != WPA_KEY_MGMT_DPP &&
  1871. ver != WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
  1872. wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
  1873. "WPA: CCMP is used, but EAPOL-Key "
  1874. "descriptor version (%d) is not 2", ver);
  1875. if (sm->group_cipher != WPA_CIPHER_CCMP &&
  1876. !(key_info & WPA_KEY_INFO_KEY_TYPE)) {
  1877. /* Earlier versions of IEEE 802.11i did not explicitly
  1878. * require version 2 descriptor for all EAPOL-Key
  1879. * packets, so allow group keys to use version 1 if
  1880. * CCMP is not used for them. */
  1881. wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
  1882. "WPA: Backwards compatibility: allow invalid "
  1883. "version for non-CCMP group keys");
  1884. } else if (ver == WPA_KEY_INFO_TYPE_AES_128_CMAC) {
  1885. wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
  1886. "WPA: Interoperability workaround: allow incorrect (should have been HMAC-SHA1), but stronger (is AES-128-CMAC), descriptor version to be used");
  1887. } else
  1888. goto out;
  1889. } else if (sm->pairwise_cipher == WPA_CIPHER_GCMP &&
  1890. !wpa_key_mgmt_suite_b(sm->key_mgmt) &&
  1891. ver != WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
  1892. wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
  1893. "WPA: GCMP is used, but EAPOL-Key "
  1894. "descriptor version (%d) is not 2", ver);
  1895. goto out;
  1896. }
  1897. if (sm->rx_replay_counter_set &&
  1898. os_memcmp(key->replay_counter, sm->rx_replay_counter,
  1899. WPA_REPLAY_COUNTER_LEN) <= 0) {
  1900. wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
  1901. "WPA: EAPOL-Key Replay Counter did not increase - "
  1902. "dropping packet");
  1903. goto out;
  1904. }
  1905. if (key_info & WPA_KEY_INFO_SMK_MESSAGE) {
  1906. wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
  1907. "WPA: Unsupported SMK bit in key_info");
  1908. goto out;
  1909. }
  1910. if (!(key_info & WPA_KEY_INFO_ACK)) {
  1911. wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
  1912. "WPA: No Ack bit in key_info");
  1913. goto out;
  1914. }
  1915. if (key_info & WPA_KEY_INFO_REQUEST) {
  1916. wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
  1917. "WPA: EAPOL-Key with Request bit - dropped");
  1918. goto out;
  1919. }
  1920. if ((key_info & WPA_KEY_INFO_MIC) &&
  1921. wpa_supplicant_verify_eapol_key_mic(sm, key, ver, tmp, data_len))
  1922. goto out;
  1923. #ifdef CONFIG_FILS
  1924. if (!mic_len && (key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
  1925. if (wpa_supp_aead_decrypt(sm, tmp, data_len, &key_data_len))
  1926. goto out;
  1927. }
  1928. #endif /* CONFIG_FILS */
  1929. if ((sm->proto == WPA_PROTO_RSN || sm->proto == WPA_PROTO_OSEN) &&
  1930. (key_info & WPA_KEY_INFO_ENCR_KEY_DATA) && mic_len) {
  1931. if (wpa_supplicant_decrypt_key_data(sm, key, mic_len,
  1932. ver, key_data,
  1933. &key_data_len))
  1934. goto out;
  1935. }
  1936. if (key_info & WPA_KEY_INFO_KEY_TYPE) {
  1937. if (key_info & WPA_KEY_INFO_KEY_INDEX_MASK) {
  1938. wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
  1939. "WPA: Ignored EAPOL-Key (Pairwise) with "
  1940. "non-zero key index");
  1941. goto out;
  1942. }
  1943. if (key_info & (WPA_KEY_INFO_MIC |
  1944. WPA_KEY_INFO_ENCR_KEY_DATA)) {
  1945. /* 3/4 4-Way Handshake */
  1946. wpa_supplicant_process_3_of_4(sm, key, ver, key_data,
  1947. key_data_len);
  1948. } else {
  1949. /* 1/4 4-Way Handshake */
  1950. wpa_supplicant_process_1_of_4(sm, src_addr, key,
  1951. ver, key_data,
  1952. key_data_len);
  1953. }
  1954. } else {
  1955. if ((mic_len && (key_info & WPA_KEY_INFO_MIC)) ||
  1956. (!mic_len && (key_info & WPA_KEY_INFO_ENCR_KEY_DATA))) {
  1957. /* 1/2 Group Key Handshake */
  1958. wpa_supplicant_process_1_of_2(sm, src_addr, key,
  1959. key_data, key_data_len,
  1960. ver);
  1961. } else {
  1962. wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
  1963. "WPA: EAPOL-Key (Group) without Mic/Encr bit - "
  1964. "dropped");
  1965. }
  1966. }
  1967. ret = 1;
  1968. out:
  1969. bin_clear_free(tmp, data_len);
  1970. return ret;
  1971. }
  1972. #ifdef CONFIG_CTRL_IFACE
  1973. static u32 wpa_key_mgmt_suite(struct wpa_sm *sm)
  1974. {
  1975. switch (sm->key_mgmt) {
  1976. case WPA_KEY_MGMT_IEEE8021X:
  1977. return ((sm->proto == WPA_PROTO_RSN ||
  1978. sm->proto == WPA_PROTO_OSEN) ?
  1979. RSN_AUTH_KEY_MGMT_UNSPEC_802_1X :
  1980. WPA_AUTH_KEY_MGMT_UNSPEC_802_1X);
  1981. case WPA_KEY_MGMT_PSK:
  1982. return (sm->proto == WPA_PROTO_RSN ?
  1983. RSN_AUTH_KEY_MGMT_PSK_OVER_802_1X :
  1984. WPA_AUTH_KEY_MGMT_PSK_OVER_802_1X);
  1985. #ifdef CONFIG_IEEE80211R
  1986. case WPA_KEY_MGMT_FT_IEEE8021X:
  1987. return RSN_AUTH_KEY_MGMT_FT_802_1X;
  1988. case WPA_KEY_MGMT_FT_PSK:
  1989. return RSN_AUTH_KEY_MGMT_FT_PSK;
  1990. #endif /* CONFIG_IEEE80211R */
  1991. #ifdef CONFIG_IEEE80211W
  1992. case WPA_KEY_MGMT_IEEE8021X_SHA256:
  1993. return RSN_AUTH_KEY_MGMT_802_1X_SHA256;
  1994. case WPA_KEY_MGMT_PSK_SHA256:
  1995. return RSN_AUTH_KEY_MGMT_PSK_SHA256;
  1996. #endif /* CONFIG_IEEE80211W */
  1997. case WPA_KEY_MGMT_CCKM:
  1998. return (sm->proto == WPA_PROTO_RSN ?
  1999. RSN_AUTH_KEY_MGMT_CCKM:
  2000. WPA_AUTH_KEY_MGMT_CCKM);
  2001. case WPA_KEY_MGMT_WPA_NONE:
  2002. return WPA_AUTH_KEY_MGMT_NONE;
  2003. case WPA_KEY_MGMT_IEEE8021X_SUITE_B:
  2004. return RSN_AUTH_KEY_MGMT_802_1X_SUITE_B;
  2005. case WPA_KEY_MGMT_IEEE8021X_SUITE_B_192:
  2006. return RSN_AUTH_KEY_MGMT_802_1X_SUITE_B_192;
  2007. default:
  2008. return 0;
  2009. }
  2010. }
  2011. #define RSN_SUITE "%02x-%02x-%02x-%d"
  2012. #define RSN_SUITE_ARG(s) \
  2013. ((s) >> 24) & 0xff, ((s) >> 16) & 0xff, ((s) >> 8) & 0xff, (s) & 0xff
  2014. /**
  2015. * wpa_sm_get_mib - Dump text list of MIB entries
  2016. * @sm: Pointer to WPA state machine data from wpa_sm_init()
  2017. * @buf: Buffer for the list
  2018. * @buflen: Length of the buffer
  2019. * Returns: Number of bytes written to buffer
  2020. *
  2021. * This function is used fetch dot11 MIB variables.
  2022. */
  2023. int wpa_sm_get_mib(struct wpa_sm *sm, char *buf, size_t buflen)
  2024. {
  2025. char pmkid_txt[PMKID_LEN * 2 + 1];
  2026. int rsna, ret;
  2027. size_t len;
  2028. if (sm->cur_pmksa) {
  2029. wpa_snprintf_hex(pmkid_txt, sizeof(pmkid_txt),
  2030. sm->cur_pmksa->pmkid, PMKID_LEN);
  2031. } else
  2032. pmkid_txt[0] = '\0';
  2033. if ((wpa_key_mgmt_wpa_psk(sm->key_mgmt) ||
  2034. wpa_key_mgmt_wpa_ieee8021x(sm->key_mgmt)) &&
  2035. sm->proto == WPA_PROTO_RSN)
  2036. rsna = 1;
  2037. else
  2038. rsna = 0;
  2039. ret = os_snprintf(buf, buflen,
  2040. "dot11RSNAOptionImplemented=TRUE\n"
  2041. "dot11RSNAPreauthenticationImplemented=TRUE\n"
  2042. "dot11RSNAEnabled=%s\n"
  2043. "dot11RSNAPreauthenticationEnabled=%s\n"
  2044. "dot11RSNAConfigVersion=%d\n"
  2045. "dot11RSNAConfigPairwiseKeysSupported=5\n"
  2046. "dot11RSNAConfigGroupCipherSize=%d\n"
  2047. "dot11RSNAConfigPMKLifetime=%d\n"
  2048. "dot11RSNAConfigPMKReauthThreshold=%d\n"
  2049. "dot11RSNAConfigNumberOfPTKSAReplayCounters=1\n"
  2050. "dot11RSNAConfigSATimeout=%d\n",
  2051. rsna ? "TRUE" : "FALSE",
  2052. rsna ? "TRUE" : "FALSE",
  2053. RSN_VERSION,
  2054. wpa_cipher_key_len(sm->group_cipher) * 8,
  2055. sm->dot11RSNAConfigPMKLifetime,
  2056. sm->dot11RSNAConfigPMKReauthThreshold,
  2057. sm->dot11RSNAConfigSATimeout);
  2058. if (os_snprintf_error(buflen, ret))
  2059. return 0;
  2060. len = ret;
  2061. ret = os_snprintf(
  2062. buf + len, buflen - len,
  2063. "dot11RSNAAuthenticationSuiteSelected=" RSN_SUITE "\n"
  2064. "dot11RSNAPairwiseCipherSelected=" RSN_SUITE "\n"
  2065. "dot11RSNAGroupCipherSelected=" RSN_SUITE "\n"
  2066. "dot11RSNAPMKIDUsed=%s\n"
  2067. "dot11RSNAAuthenticationSuiteRequested=" RSN_SUITE "\n"
  2068. "dot11RSNAPairwiseCipherRequested=" RSN_SUITE "\n"
  2069. "dot11RSNAGroupCipherRequested=" RSN_SUITE "\n"
  2070. "dot11RSNAConfigNumberOfGTKSAReplayCounters=0\n"
  2071. "dot11RSNA4WayHandshakeFailures=%u\n",
  2072. RSN_SUITE_ARG(wpa_key_mgmt_suite(sm)),
  2073. RSN_SUITE_ARG(wpa_cipher_to_suite(sm->proto,
  2074. sm->pairwise_cipher)),
  2075. RSN_SUITE_ARG(wpa_cipher_to_suite(sm->proto,
  2076. sm->group_cipher)),
  2077. pmkid_txt,
  2078. RSN_SUITE_ARG(wpa_key_mgmt_suite(sm)),
  2079. RSN_SUITE_ARG(wpa_cipher_to_suite(sm->proto,
  2080. sm->pairwise_cipher)),
  2081. RSN_SUITE_ARG(wpa_cipher_to_suite(sm->proto,
  2082. sm->group_cipher)),
  2083. sm->dot11RSNA4WayHandshakeFailures);
  2084. if (!os_snprintf_error(buflen - len, ret))
  2085. len += ret;
  2086. return (int) len;
  2087. }
  2088. #endif /* CONFIG_CTRL_IFACE */
  2089. static void wpa_sm_pmksa_free_cb(struct rsn_pmksa_cache_entry *entry,
  2090. void *ctx, enum pmksa_free_reason reason)
  2091. {
  2092. struct wpa_sm *sm = ctx;
  2093. int deauth = 0;
  2094. wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "RSN: PMKSA cache entry free_cb: "
  2095. MACSTR " reason=%d", MAC2STR(entry->aa), reason);
  2096. if (sm->cur_pmksa == entry) {
  2097. wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
  2098. "RSN: %s current PMKSA entry",
  2099. reason == PMKSA_REPLACE ? "replaced" : "removed");
  2100. pmksa_cache_clear_current(sm);
  2101. /*
  2102. * If an entry is simply being replaced, there's no need to
  2103. * deauthenticate because it will be immediately re-added.
  2104. * This happens when EAP authentication is completed again
  2105. * (reauth or failed PMKSA caching attempt).
  2106. */
  2107. if (reason != PMKSA_REPLACE)
  2108. deauth = 1;
  2109. }
  2110. if (reason == PMKSA_EXPIRE &&
  2111. (sm->pmk_len == entry->pmk_len &&
  2112. os_memcmp(sm->pmk, entry->pmk, sm->pmk_len) == 0)) {
  2113. wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
  2114. "RSN: deauthenticating due to expired PMK");
  2115. pmksa_cache_clear_current(sm);
  2116. deauth = 1;
  2117. }
  2118. if (deauth) {
  2119. os_memset(sm->pmk, 0, sizeof(sm->pmk));
  2120. wpa_sm_deauthenticate(sm, WLAN_REASON_UNSPECIFIED);
  2121. }
  2122. }
  2123. /**
  2124. * wpa_sm_init - Initialize WPA state machine
  2125. * @ctx: Context pointer for callbacks; this needs to be an allocated buffer
  2126. * Returns: Pointer to the allocated WPA state machine data
  2127. *
  2128. * This function is used to allocate a new WPA state machine and the returned
  2129. * value is passed to all WPA state machine calls.
  2130. */
  2131. struct wpa_sm * wpa_sm_init(struct wpa_sm_ctx *ctx)
  2132. {
  2133. struct wpa_sm *sm;
  2134. sm = os_zalloc(sizeof(*sm));
  2135. if (sm == NULL)
  2136. return NULL;
  2137. dl_list_init(&sm->pmksa_candidates);
  2138. sm->renew_snonce = 1;
  2139. sm->ctx = ctx;
  2140. sm->dot11RSNAConfigPMKLifetime = 43200;
  2141. sm->dot11RSNAConfigPMKReauthThreshold = 70;
  2142. sm->dot11RSNAConfigSATimeout = 60;
  2143. sm->pmksa = pmksa_cache_init(wpa_sm_pmksa_free_cb, sm, sm);
  2144. if (sm->pmksa == NULL) {
  2145. wpa_msg(sm->ctx->msg_ctx, MSG_ERROR,
  2146. "RSN: PMKSA cache initialization failed");
  2147. os_free(sm);
  2148. return NULL;
  2149. }
  2150. return sm;
  2151. }
  2152. /**
  2153. * wpa_sm_deinit - Deinitialize WPA state machine
  2154. * @sm: Pointer to WPA state machine data from wpa_sm_init()
  2155. */
  2156. void wpa_sm_deinit(struct wpa_sm *sm)
  2157. {
  2158. if (sm == NULL)
  2159. return;
  2160. pmksa_cache_deinit(sm->pmksa);
  2161. eloop_cancel_timeout(wpa_sm_start_preauth, sm, NULL);
  2162. eloop_cancel_timeout(wpa_sm_rekey_ptk, sm, NULL);
  2163. os_free(sm->assoc_wpa_ie);
  2164. os_free(sm->ap_wpa_ie);
  2165. os_free(sm->ap_rsn_ie);
  2166. wpa_sm_drop_sa(sm);
  2167. os_free(sm->ctx);
  2168. #ifdef CONFIG_IEEE80211R
  2169. os_free(sm->assoc_resp_ies);
  2170. #endif /* CONFIG_IEEE80211R */
  2171. #ifdef CONFIG_TESTING_OPTIONS
  2172. wpabuf_free(sm->test_assoc_ie);
  2173. #endif /* CONFIG_TESTING_OPTIONS */
  2174. #ifdef CONFIG_FILS_SK_PFS
  2175. crypto_ecdh_deinit(sm->fils_ecdh);
  2176. #endif /* CONFIG_FILS_SK_PFS */
  2177. #ifdef CONFIG_FILS
  2178. wpabuf_free(sm->fils_ft_ies);
  2179. #endif /* CONFIG_FILS */
  2180. #ifdef CONFIG_OWE
  2181. crypto_ecdh_deinit(sm->owe_ecdh);
  2182. #endif /* CONFIG_OWE */
  2183. os_free(sm);
  2184. }
  2185. /**
  2186. * wpa_sm_notify_assoc - Notify WPA state machine about association
  2187. * @sm: Pointer to WPA state machine data from wpa_sm_init()
  2188. * @bssid: The BSSID of the new association
  2189. *
  2190. * This function is called to let WPA state machine know that the connection
  2191. * was established.
  2192. */
  2193. void wpa_sm_notify_assoc(struct wpa_sm *sm, const u8 *bssid)
  2194. {
  2195. int clear_keys = 1;
  2196. if (sm == NULL)
  2197. return;
  2198. wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
  2199. "WPA: Association event - clear replay counter");
  2200. os_memcpy(sm->bssid, bssid, ETH_ALEN);
  2201. os_memset(sm->rx_replay_counter, 0, WPA_REPLAY_COUNTER_LEN);
  2202. sm->rx_replay_counter_set = 0;
  2203. sm->renew_snonce = 1;
  2204. if (os_memcmp(sm->preauth_bssid, bssid, ETH_ALEN) == 0)
  2205. rsn_preauth_deinit(sm);
  2206. #ifdef CONFIG_IEEE80211R
  2207. if (wpa_ft_is_completed(sm)) {
  2208. /*
  2209. * Clear portValid to kick EAPOL state machine to re-enter
  2210. * AUTHENTICATED state to get the EAPOL port Authorized.
  2211. */
  2212. eapol_sm_notify_portValid(sm->eapol, FALSE);
  2213. wpa_supplicant_key_neg_complete(sm, sm->bssid, 1);
  2214. /* Prepare for the next transition */
  2215. wpa_ft_prepare_auth_request(sm, NULL);
  2216. clear_keys = 0;
  2217. }
  2218. #endif /* CONFIG_IEEE80211R */
  2219. #ifdef CONFIG_FILS
  2220. if (sm->fils_completed) {
  2221. /*
  2222. * Clear portValid to kick EAPOL state machine to re-enter
  2223. * AUTHENTICATED state to get the EAPOL port Authorized.
  2224. */
  2225. wpa_supplicant_key_neg_complete(sm, sm->bssid, 1);
  2226. clear_keys = 0;
  2227. }
  2228. #endif /* CONFIG_FILS */
  2229. if (clear_keys) {
  2230. /*
  2231. * IEEE 802.11, 8.4.10: Delete PTK SA on (re)association if
  2232. * this is not part of a Fast BSS Transition.
  2233. */
  2234. wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Clear old PTK");
  2235. sm->ptk_set = 0;
  2236. os_memset(&sm->ptk, 0, sizeof(sm->ptk));
  2237. sm->tptk_set = 0;
  2238. os_memset(&sm->tptk, 0, sizeof(sm->tptk));
  2239. os_memset(&sm->gtk, 0, sizeof(sm->gtk));
  2240. os_memset(&sm->gtk_wnm_sleep, 0, sizeof(sm->gtk_wnm_sleep));
  2241. #ifdef CONFIG_IEEE80211W
  2242. os_memset(&sm->igtk, 0, sizeof(sm->igtk));
  2243. os_memset(&sm->igtk_wnm_sleep, 0, sizeof(sm->igtk_wnm_sleep));
  2244. #endif /* CONFIG_IEEE80211W */
  2245. }
  2246. #ifdef CONFIG_TDLS
  2247. wpa_tdls_assoc(sm);
  2248. #endif /* CONFIG_TDLS */
  2249. #ifdef CONFIG_P2P
  2250. os_memset(sm->p2p_ip_addr, 0, sizeof(sm->p2p_ip_addr));
  2251. #endif /* CONFIG_P2P */
  2252. }
  2253. /**
  2254. * wpa_sm_notify_disassoc - Notify WPA state machine about disassociation
  2255. * @sm: Pointer to WPA state machine data from wpa_sm_init()
  2256. *
  2257. * This function is called to let WPA state machine know that the connection
  2258. * was lost. This will abort any existing pre-authentication session.
  2259. */
  2260. void wpa_sm_notify_disassoc(struct wpa_sm *sm)
  2261. {
  2262. eloop_cancel_timeout(wpa_sm_start_preauth, sm, NULL);
  2263. eloop_cancel_timeout(wpa_sm_rekey_ptk, sm, NULL);
  2264. rsn_preauth_deinit(sm);
  2265. pmksa_cache_clear_current(sm);
  2266. if (wpa_sm_get_state(sm) == WPA_4WAY_HANDSHAKE)
  2267. sm->dot11RSNA4WayHandshakeFailures++;
  2268. #ifdef CONFIG_TDLS
  2269. wpa_tdls_disassoc(sm);
  2270. #endif /* CONFIG_TDLS */
  2271. #ifdef CONFIG_FILS
  2272. sm->fils_completed = 0;
  2273. #endif /* CONFIG_FILS */
  2274. #ifdef CONFIG_IEEE80211R
  2275. sm->ft_reassoc_completed = 0;
  2276. #endif /* CONFIG_IEEE80211R */
  2277. /* Keys are not needed in the WPA state machine anymore */
  2278. wpa_sm_drop_sa(sm);
  2279. sm->msg_3_of_4_ok = 0;
  2280. }
  2281. /**
  2282. * wpa_sm_set_pmk - Set PMK
  2283. * @sm: Pointer to WPA state machine data from wpa_sm_init()
  2284. * @pmk: The new PMK
  2285. * @pmk_len: The length of the new PMK in bytes
  2286. * @pmkid: Calculated PMKID
  2287. * @bssid: AA to add into PMKSA cache or %NULL to not cache the PMK
  2288. *
  2289. * Configure the PMK for WPA state machine.
  2290. */
  2291. void wpa_sm_set_pmk(struct wpa_sm *sm, const u8 *pmk, size_t pmk_len,
  2292. const u8 *pmkid, const u8 *bssid)
  2293. {
  2294. if (sm == NULL)
  2295. return;
  2296. sm->pmk_len = pmk_len;
  2297. os_memcpy(sm->pmk, pmk, pmk_len);
  2298. #ifdef CONFIG_IEEE80211R
  2299. /* Set XXKey to be PSK for FT key derivation */
  2300. sm->xxkey_len = pmk_len;
  2301. os_memcpy(sm->xxkey, pmk, pmk_len);
  2302. #endif /* CONFIG_IEEE80211R */
  2303. if (bssid) {
  2304. pmksa_cache_add(sm->pmksa, pmk, pmk_len, pmkid, NULL, 0,
  2305. bssid, sm->own_addr,
  2306. sm->network_ctx, sm->key_mgmt, NULL);
  2307. }
  2308. }
  2309. /**
  2310. * wpa_sm_set_pmk_from_pmksa - Set PMK based on the current PMKSA
  2311. * @sm: Pointer to WPA state machine data from wpa_sm_init()
  2312. *
  2313. * Take the PMK from the current PMKSA into use. If no PMKSA is active, the PMK
  2314. * will be cleared.
  2315. */
  2316. void wpa_sm_set_pmk_from_pmksa(struct wpa_sm *sm)
  2317. {
  2318. if (sm == NULL)
  2319. return;
  2320. if (sm->cur_pmksa) {
  2321. sm->pmk_len = sm->cur_pmksa->pmk_len;
  2322. os_memcpy(sm->pmk, sm->cur_pmksa->pmk, sm->pmk_len);
  2323. } else {
  2324. sm->pmk_len = PMK_LEN;
  2325. os_memset(sm->pmk, 0, PMK_LEN);
  2326. }
  2327. }
  2328. /**
  2329. * wpa_sm_set_fast_reauth - Set fast reauthentication (EAP) enabled/disabled
  2330. * @sm: Pointer to WPA state machine data from wpa_sm_init()
  2331. * @fast_reauth: Whether fast reauthentication (EAP) is allowed
  2332. */
  2333. void wpa_sm_set_fast_reauth(struct wpa_sm *sm, int fast_reauth)
  2334. {
  2335. if (sm)
  2336. sm->fast_reauth = fast_reauth;
  2337. }
  2338. /**
  2339. * wpa_sm_set_scard_ctx - Set context pointer for smartcard callbacks
  2340. * @sm: Pointer to WPA state machine data from wpa_sm_init()
  2341. * @scard_ctx: Context pointer for smartcard related callback functions
  2342. */
  2343. void wpa_sm_set_scard_ctx(struct wpa_sm *sm, void *scard_ctx)
  2344. {
  2345. if (sm == NULL)
  2346. return;
  2347. sm->scard_ctx = scard_ctx;
  2348. if (sm->preauth_eapol)
  2349. eapol_sm_register_scard_ctx(sm->preauth_eapol, scard_ctx);
  2350. }
  2351. /**
  2352. * wpa_sm_set_config - Notification of current configration change
  2353. * @sm: Pointer to WPA state machine data from wpa_sm_init()
  2354. * @config: Pointer to current network configuration
  2355. *
  2356. * Notify WPA state machine that configuration has changed. config will be
  2357. * stored as a backpointer to network configuration. This can be %NULL to clear
  2358. * the stored pointed.
  2359. */
  2360. void wpa_sm_set_config(struct wpa_sm *sm, struct rsn_supp_config *config)
  2361. {
  2362. if (!sm)
  2363. return;
  2364. if (config) {
  2365. sm->network_ctx = config->network_ctx;
  2366. sm->allowed_pairwise_cipher = config->allowed_pairwise_cipher;
  2367. sm->proactive_key_caching = config->proactive_key_caching;
  2368. sm->eap_workaround = config->eap_workaround;
  2369. sm->eap_conf_ctx = config->eap_conf_ctx;
  2370. if (config->ssid) {
  2371. os_memcpy(sm->ssid, config->ssid, config->ssid_len);
  2372. sm->ssid_len = config->ssid_len;
  2373. } else
  2374. sm->ssid_len = 0;
  2375. sm->wpa_ptk_rekey = config->wpa_ptk_rekey;
  2376. sm->p2p = config->p2p;
  2377. sm->wpa_rsc_relaxation = config->wpa_rsc_relaxation;
  2378. #ifdef CONFIG_FILS
  2379. if (config->fils_cache_id) {
  2380. sm->fils_cache_id_set = 1;
  2381. os_memcpy(sm->fils_cache_id, config->fils_cache_id,
  2382. FILS_CACHE_ID_LEN);
  2383. } else {
  2384. sm->fils_cache_id_set = 0;
  2385. }
  2386. #endif /* CONFIG_FILS */
  2387. } else {
  2388. sm->network_ctx = NULL;
  2389. sm->allowed_pairwise_cipher = 0;
  2390. sm->proactive_key_caching = 0;
  2391. sm->eap_workaround = 0;
  2392. sm->eap_conf_ctx = NULL;
  2393. sm->ssid_len = 0;
  2394. sm->wpa_ptk_rekey = 0;
  2395. sm->p2p = 0;
  2396. sm->wpa_rsc_relaxation = 0;
  2397. }
  2398. }
  2399. /**
  2400. * wpa_sm_set_own_addr - Set own MAC address
  2401. * @sm: Pointer to WPA state machine data from wpa_sm_init()
  2402. * @addr: Own MAC address
  2403. */
  2404. void wpa_sm_set_own_addr(struct wpa_sm *sm, const u8 *addr)
  2405. {
  2406. if (sm)
  2407. os_memcpy(sm->own_addr, addr, ETH_ALEN);
  2408. }
  2409. /**
  2410. * wpa_sm_set_ifname - Set network interface name
  2411. * @sm: Pointer to WPA state machine data from wpa_sm_init()
  2412. * @ifname: Interface name
  2413. * @bridge_ifname: Optional bridge interface name (for pre-auth)
  2414. */
  2415. void wpa_sm_set_ifname(struct wpa_sm *sm, const char *ifname,
  2416. const char *bridge_ifname)
  2417. {
  2418. if (sm) {
  2419. sm->ifname = ifname;
  2420. sm->bridge_ifname = bridge_ifname;
  2421. }
  2422. }
  2423. /**
  2424. * wpa_sm_set_eapol - Set EAPOL state machine pointer
  2425. * @sm: Pointer to WPA state machine data from wpa_sm_init()
  2426. * @eapol: Pointer to EAPOL state machine allocated with eapol_sm_init()
  2427. */
  2428. void wpa_sm_set_eapol(struct wpa_sm *sm, struct eapol_sm *eapol)
  2429. {
  2430. if (sm)
  2431. sm->eapol = eapol;
  2432. }
  2433. /**
  2434. * wpa_sm_set_param - Set WPA state machine parameters
  2435. * @sm: Pointer to WPA state machine data from wpa_sm_init()
  2436. * @param: Parameter field
  2437. * @value: Parameter value
  2438. * Returns: 0 on success, -1 on failure
  2439. */
  2440. int wpa_sm_set_param(struct wpa_sm *sm, enum wpa_sm_conf_params param,
  2441. unsigned int value)
  2442. {
  2443. int ret = 0;
  2444. if (sm == NULL)
  2445. return -1;
  2446. switch (param) {
  2447. case RSNA_PMK_LIFETIME:
  2448. if (value > 0)
  2449. sm->dot11RSNAConfigPMKLifetime = value;
  2450. else
  2451. ret = -1;
  2452. break;
  2453. case RSNA_PMK_REAUTH_THRESHOLD:
  2454. if (value > 0 && value <= 100)
  2455. sm->dot11RSNAConfigPMKReauthThreshold = value;
  2456. else
  2457. ret = -1;
  2458. break;
  2459. case RSNA_SA_TIMEOUT:
  2460. if (value > 0)
  2461. sm->dot11RSNAConfigSATimeout = value;
  2462. else
  2463. ret = -1;
  2464. break;
  2465. case WPA_PARAM_PROTO:
  2466. sm->proto = value;
  2467. break;
  2468. case WPA_PARAM_PAIRWISE:
  2469. sm->pairwise_cipher = value;
  2470. break;
  2471. case WPA_PARAM_GROUP:
  2472. sm->group_cipher = value;
  2473. break;
  2474. case WPA_PARAM_KEY_MGMT:
  2475. sm->key_mgmt = value;
  2476. break;
  2477. #ifdef CONFIG_IEEE80211W
  2478. case WPA_PARAM_MGMT_GROUP:
  2479. sm->mgmt_group_cipher = value;
  2480. break;
  2481. #endif /* CONFIG_IEEE80211W */
  2482. case WPA_PARAM_RSN_ENABLED:
  2483. sm->rsn_enabled = value;
  2484. break;
  2485. case WPA_PARAM_MFP:
  2486. sm->mfp = value;
  2487. break;
  2488. default:
  2489. break;
  2490. }
  2491. return ret;
  2492. }
  2493. /**
  2494. * wpa_sm_get_status - Get WPA state machine
  2495. * @sm: Pointer to WPA state machine data from wpa_sm_init()
  2496. * @buf: Buffer for status information
  2497. * @buflen: Maximum buffer length
  2498. * @verbose: Whether to include verbose status information
  2499. * Returns: Number of bytes written to buf.
  2500. *
  2501. * Query WPA state machine for status information. This function fills in
  2502. * a text area with current status information. If the buffer (buf) is not
  2503. * large enough, status information will be truncated to fit the buffer.
  2504. */
  2505. int wpa_sm_get_status(struct wpa_sm *sm, char *buf, size_t buflen,
  2506. int verbose)
  2507. {
  2508. char *pos = buf, *end = buf + buflen;
  2509. int ret;
  2510. ret = os_snprintf(pos, end - pos,
  2511. "pairwise_cipher=%s\n"
  2512. "group_cipher=%s\n"
  2513. "key_mgmt=%s\n",
  2514. wpa_cipher_txt(sm->pairwise_cipher),
  2515. wpa_cipher_txt(sm->group_cipher),
  2516. wpa_key_mgmt_txt(sm->key_mgmt, sm->proto));
  2517. if (os_snprintf_error(end - pos, ret))
  2518. return pos - buf;
  2519. pos += ret;
  2520. if (sm->mfp != NO_MGMT_FRAME_PROTECTION && sm->ap_rsn_ie) {
  2521. struct wpa_ie_data rsn;
  2522. if (wpa_parse_wpa_ie_rsn(sm->ap_rsn_ie, sm->ap_rsn_ie_len, &rsn)
  2523. >= 0 &&
  2524. rsn.capabilities & (WPA_CAPABILITY_MFPR |
  2525. WPA_CAPABILITY_MFPC)) {
  2526. ret = os_snprintf(pos, end - pos, "pmf=%d\n"
  2527. "mgmt_group_cipher=%s\n",
  2528. (rsn.capabilities &
  2529. WPA_CAPABILITY_MFPR) ? 2 : 1,
  2530. wpa_cipher_txt(
  2531. sm->mgmt_group_cipher));
  2532. if (os_snprintf_error(end - pos, ret))
  2533. return pos - buf;
  2534. pos += ret;
  2535. }
  2536. }
  2537. return pos - buf;
  2538. }
  2539. int wpa_sm_pmf_enabled(struct wpa_sm *sm)
  2540. {
  2541. struct wpa_ie_data rsn;
  2542. if (sm->mfp == NO_MGMT_FRAME_PROTECTION || !sm->ap_rsn_ie)
  2543. return 0;
  2544. if (wpa_parse_wpa_ie_rsn(sm->ap_rsn_ie, sm->ap_rsn_ie_len, &rsn) >= 0 &&
  2545. rsn.capabilities & (WPA_CAPABILITY_MFPR | WPA_CAPABILITY_MFPC))
  2546. return 1;
  2547. return 0;
  2548. }
  2549. /**
  2550. * wpa_sm_set_assoc_wpa_ie_default - Generate own WPA/RSN IE from configuration
  2551. * @sm: Pointer to WPA state machine data from wpa_sm_init()
  2552. * @wpa_ie: Pointer to buffer for WPA/RSN IE
  2553. * @wpa_ie_len: Pointer to the length of the wpa_ie buffer
  2554. * Returns: 0 on success, -1 on failure
  2555. */
  2556. int wpa_sm_set_assoc_wpa_ie_default(struct wpa_sm *sm, u8 *wpa_ie,
  2557. size_t *wpa_ie_len)
  2558. {
  2559. int res;
  2560. if (sm == NULL)
  2561. return -1;
  2562. #ifdef CONFIG_TESTING_OPTIONS
  2563. if (sm->test_assoc_ie) {
  2564. wpa_printf(MSG_DEBUG,
  2565. "TESTING: Replace association WPA/RSN IE");
  2566. if (*wpa_ie_len < wpabuf_len(sm->test_assoc_ie))
  2567. return -1;
  2568. os_memcpy(wpa_ie, wpabuf_head(sm->test_assoc_ie),
  2569. wpabuf_len(sm->test_assoc_ie));
  2570. res = wpabuf_len(sm->test_assoc_ie);
  2571. } else
  2572. #endif /* CONFIG_TESTING_OPTIONS */
  2573. res = wpa_gen_wpa_ie(sm, wpa_ie, *wpa_ie_len);
  2574. if (res < 0)
  2575. return -1;
  2576. *wpa_ie_len = res;
  2577. wpa_hexdump(MSG_DEBUG, "WPA: Set own WPA IE default",
  2578. wpa_ie, *wpa_ie_len);
  2579. if (sm->assoc_wpa_ie == NULL) {
  2580. /*
  2581. * Make a copy of the WPA/RSN IE so that 4-Way Handshake gets
  2582. * the correct version of the IE even if PMKSA caching is
  2583. * aborted (which would remove PMKID from IE generation).
  2584. */
  2585. sm->assoc_wpa_ie = os_memdup(wpa_ie, *wpa_ie_len);
  2586. if (sm->assoc_wpa_ie == NULL)
  2587. return -1;
  2588. sm->assoc_wpa_ie_len = *wpa_ie_len;
  2589. } else {
  2590. wpa_hexdump(MSG_DEBUG,
  2591. "WPA: Leave previously set WPA IE default",
  2592. sm->assoc_wpa_ie, sm->assoc_wpa_ie_len);
  2593. }
  2594. return 0;
  2595. }
  2596. /**
  2597. * wpa_sm_set_assoc_wpa_ie - Set own WPA/RSN IE from (Re)AssocReq
  2598. * @sm: Pointer to WPA state machine data from wpa_sm_init()
  2599. * @ie: Pointer to IE data (starting from id)
  2600. * @len: IE length
  2601. * Returns: 0 on success, -1 on failure
  2602. *
  2603. * Inform WPA state machine about the WPA/RSN IE used in (Re)Association
  2604. * Request frame. The IE will be used to override the default value generated
  2605. * with wpa_sm_set_assoc_wpa_ie_default().
  2606. */
  2607. int wpa_sm_set_assoc_wpa_ie(struct wpa_sm *sm, const u8 *ie, size_t len)
  2608. {
  2609. if (sm == NULL)
  2610. return -1;
  2611. os_free(sm->assoc_wpa_ie);
  2612. if (ie == NULL || len == 0) {
  2613. wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
  2614. "WPA: clearing own WPA/RSN IE");
  2615. sm->assoc_wpa_ie = NULL;
  2616. sm->assoc_wpa_ie_len = 0;
  2617. } else {
  2618. wpa_hexdump(MSG_DEBUG, "WPA: set own WPA/RSN IE", ie, len);
  2619. sm->assoc_wpa_ie = os_memdup(ie, len);
  2620. if (sm->assoc_wpa_ie == NULL)
  2621. return -1;
  2622. sm->assoc_wpa_ie_len = len;
  2623. }
  2624. return 0;
  2625. }
  2626. /**
  2627. * wpa_sm_set_ap_wpa_ie - Set AP WPA IE from Beacon/ProbeResp
  2628. * @sm: Pointer to WPA state machine data from wpa_sm_init()
  2629. * @ie: Pointer to IE data (starting from id)
  2630. * @len: IE length
  2631. * Returns: 0 on success, -1 on failure
  2632. *
  2633. * Inform WPA state machine about the WPA IE used in Beacon / Probe Response
  2634. * frame.
  2635. */
  2636. int wpa_sm_set_ap_wpa_ie(struct wpa_sm *sm, const u8 *ie, size_t len)
  2637. {
  2638. if (sm == NULL)
  2639. return -1;
  2640. os_free(sm->ap_wpa_ie);
  2641. if (ie == NULL || len == 0) {
  2642. wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
  2643. "WPA: clearing AP WPA IE");
  2644. sm->ap_wpa_ie = NULL;
  2645. sm->ap_wpa_ie_len = 0;
  2646. } else {
  2647. wpa_hexdump(MSG_DEBUG, "WPA: set AP WPA IE", ie, len);
  2648. sm->ap_wpa_ie = os_memdup(ie, len);
  2649. if (sm->ap_wpa_ie == NULL)
  2650. return -1;
  2651. sm->ap_wpa_ie_len = len;
  2652. }
  2653. return 0;
  2654. }
  2655. /**
  2656. * wpa_sm_set_ap_rsn_ie - Set AP RSN IE from Beacon/ProbeResp
  2657. * @sm: Pointer to WPA state machine data from wpa_sm_init()
  2658. * @ie: Pointer to IE data (starting from id)
  2659. * @len: IE length
  2660. * Returns: 0 on success, -1 on failure
  2661. *
  2662. * Inform WPA state machine about the RSN IE used in Beacon / Probe Response
  2663. * frame.
  2664. */
  2665. int wpa_sm_set_ap_rsn_ie(struct wpa_sm *sm, const u8 *ie, size_t len)
  2666. {
  2667. if (sm == NULL)
  2668. return -1;
  2669. os_free(sm->ap_rsn_ie);
  2670. if (ie == NULL || len == 0) {
  2671. wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
  2672. "WPA: clearing AP RSN IE");
  2673. sm->ap_rsn_ie = NULL;
  2674. sm->ap_rsn_ie_len = 0;
  2675. } else {
  2676. wpa_hexdump(MSG_DEBUG, "WPA: set AP RSN IE", ie, len);
  2677. sm->ap_rsn_ie = os_memdup(ie, len);
  2678. if (sm->ap_rsn_ie == NULL)
  2679. return -1;
  2680. sm->ap_rsn_ie_len = len;
  2681. }
  2682. return 0;
  2683. }
  2684. /**
  2685. * wpa_sm_parse_own_wpa_ie - Parse own WPA/RSN IE
  2686. * @sm: Pointer to WPA state machine data from wpa_sm_init()
  2687. * @data: Pointer to data area for parsing results
  2688. * Returns: 0 on success, -1 if IE is not known, or -2 on parsing failure
  2689. *
  2690. * Parse the contents of the own WPA or RSN IE from (Re)AssocReq and write the
  2691. * parsed data into data.
  2692. */
  2693. int wpa_sm_parse_own_wpa_ie(struct wpa_sm *sm, struct wpa_ie_data *data)
  2694. {
  2695. if (sm == NULL)
  2696. return -1;
  2697. if (sm->assoc_wpa_ie == NULL) {
  2698. wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG,
  2699. "WPA: No WPA/RSN IE available from association info");
  2700. return -1;
  2701. }
  2702. if (wpa_parse_wpa_ie(sm->assoc_wpa_ie, sm->assoc_wpa_ie_len, data))
  2703. return -2;
  2704. return 0;
  2705. }
  2706. int wpa_sm_pmksa_cache_list(struct wpa_sm *sm, char *buf, size_t len)
  2707. {
  2708. return pmksa_cache_list(sm->pmksa, buf, len);
  2709. }
  2710. struct rsn_pmksa_cache_entry * wpa_sm_pmksa_cache_head(struct wpa_sm *sm)
  2711. {
  2712. return pmksa_cache_head(sm->pmksa);
  2713. }
  2714. struct rsn_pmksa_cache_entry *
  2715. wpa_sm_pmksa_cache_add_entry(struct wpa_sm *sm,
  2716. struct rsn_pmksa_cache_entry * entry)
  2717. {
  2718. return pmksa_cache_add_entry(sm->pmksa, entry);
  2719. }
  2720. void wpa_sm_pmksa_cache_add(struct wpa_sm *sm, const u8 *pmk, size_t pmk_len,
  2721. const u8 *pmkid, const u8 *bssid,
  2722. const u8 *fils_cache_id)
  2723. {
  2724. sm->cur_pmksa = pmksa_cache_add(sm->pmksa, pmk, pmk_len, pmkid, NULL, 0,
  2725. bssid, sm->own_addr, sm->network_ctx,
  2726. sm->key_mgmt, fils_cache_id);
  2727. }
  2728. int wpa_sm_pmksa_exists(struct wpa_sm *sm, const u8 *bssid,
  2729. const void *network_ctx)
  2730. {
  2731. return pmksa_cache_get(sm->pmksa, bssid, NULL, network_ctx) != NULL;
  2732. }
  2733. void wpa_sm_drop_sa(struct wpa_sm *sm)
  2734. {
  2735. wpa_dbg(sm->ctx->msg_ctx, MSG_DEBUG, "WPA: Clear old PMK and PTK");
  2736. sm->ptk_set = 0;
  2737. sm->tptk_set = 0;
  2738. os_memset(sm->pmk, 0, sizeof(sm->pmk));
  2739. os_memset(&sm->ptk, 0, sizeof(sm->ptk));
  2740. os_memset(&sm->tptk, 0, sizeof(sm->tptk));
  2741. os_memset(&sm->gtk, 0, sizeof(sm->gtk));
  2742. os_memset(&sm->gtk_wnm_sleep, 0, sizeof(sm->gtk_wnm_sleep));
  2743. #ifdef CONFIG_IEEE80211W
  2744. os_memset(&sm->igtk, 0, sizeof(sm->igtk));
  2745. os_memset(&sm->igtk_wnm_sleep, 0, sizeof(sm->igtk_wnm_sleep));
  2746. #endif /* CONFIG_IEEE80211W */
  2747. #ifdef CONFIG_IEEE80211R
  2748. os_memset(sm->xxkey, 0, sizeof(sm->xxkey));
  2749. os_memset(sm->pmk_r0, 0, sizeof(sm->pmk_r0));
  2750. os_memset(sm->pmk_r1, 0, sizeof(sm->pmk_r1));
  2751. #endif /* CONFIG_IEEE80211R */
  2752. }
  2753. int wpa_sm_has_ptk(struct wpa_sm *sm)
  2754. {
  2755. if (sm == NULL)
  2756. return 0;
  2757. return sm->ptk_set;
  2758. }
  2759. void wpa_sm_update_replay_ctr(struct wpa_sm *sm, const u8 *replay_ctr)
  2760. {
  2761. os_memcpy(sm->rx_replay_counter, replay_ctr, WPA_REPLAY_COUNTER_LEN);
  2762. }
  2763. void wpa_sm_pmksa_cache_flush(struct wpa_sm *sm, void *network_ctx)
  2764. {
  2765. pmksa_cache_flush(sm->pmksa, network_ctx, NULL, 0);
  2766. }
  2767. #ifdef CONFIG_WNM
  2768. int wpa_wnmsleep_install_key(struct wpa_sm *sm, u8 subelem_id, u8 *buf)
  2769. {
  2770. u16 keyinfo;
  2771. u8 keylen; /* plaintext key len */
  2772. u8 *key_rsc;
  2773. if (subelem_id == WNM_SLEEP_SUBELEM_GTK) {
  2774. struct wpa_gtk_data gd;
  2775. os_memset(&gd, 0, sizeof(gd));
  2776. keylen = wpa_cipher_key_len(sm->group_cipher);
  2777. gd.key_rsc_len = wpa_cipher_rsc_len(sm->group_cipher);
  2778. gd.alg = wpa_cipher_to_alg(sm->group_cipher);
  2779. if (gd.alg == WPA_ALG_NONE) {
  2780. wpa_printf(MSG_DEBUG, "Unsupported group cipher suite");
  2781. return -1;
  2782. }
  2783. key_rsc = buf + 5;
  2784. keyinfo = WPA_GET_LE16(buf + 2);
  2785. gd.gtk_len = keylen;
  2786. if (gd.gtk_len != buf[4]) {
  2787. wpa_printf(MSG_DEBUG, "GTK len mismatch len %d vs %d",
  2788. gd.gtk_len, buf[4]);
  2789. return -1;
  2790. }
  2791. gd.keyidx = keyinfo & 0x03; /* B0 - B1 */
  2792. gd.tx = wpa_supplicant_gtk_tx_bit_workaround(
  2793. sm, !!(keyinfo & WPA_KEY_INFO_TXRX));
  2794. os_memcpy(gd.gtk, buf + 13, gd.gtk_len);
  2795. wpa_hexdump_key(MSG_DEBUG, "Install GTK (WNM SLEEP)",
  2796. gd.gtk, gd.gtk_len);
  2797. if (wpa_supplicant_install_gtk(sm, &gd, key_rsc, 1)) {
  2798. os_memset(&gd, 0, sizeof(gd));
  2799. wpa_printf(MSG_DEBUG, "Failed to install the GTK in "
  2800. "WNM mode");
  2801. return -1;
  2802. }
  2803. os_memset(&gd, 0, sizeof(gd));
  2804. #ifdef CONFIG_IEEE80211W
  2805. } else if (subelem_id == WNM_SLEEP_SUBELEM_IGTK) {
  2806. const struct wpa_igtk_kde *igtk;
  2807. igtk = (const struct wpa_igtk_kde *) (buf + 2);
  2808. if (wpa_supplicant_install_igtk(sm, igtk, 1) < 0)
  2809. return -1;
  2810. #endif /* CONFIG_IEEE80211W */
  2811. } else {
  2812. wpa_printf(MSG_DEBUG, "Unknown element id");
  2813. return -1;
  2814. }
  2815. return 0;
  2816. }
  2817. #endif /* CONFIG_WNM */
  2818. #ifdef CONFIG_P2P
  2819. int wpa_sm_get_p2p_ip_addr(struct wpa_sm *sm, u8 *buf)
  2820. {
  2821. if (sm == NULL || WPA_GET_BE32(sm->p2p_ip_addr) == 0)
  2822. return -1;
  2823. os_memcpy(buf, sm->p2p_ip_addr, 3 * 4);
  2824. return 0;
  2825. }
  2826. #endif /* CONFIG_P2P */
  2827. void wpa_sm_set_rx_replay_ctr(struct wpa_sm *sm, const u8 *rx_replay_counter)
  2828. {
  2829. if (rx_replay_counter == NULL)
  2830. return;
  2831. os_memcpy(sm->rx_replay_counter, rx_replay_counter,
  2832. WPA_REPLAY_COUNTER_LEN);
  2833. sm->rx_replay_counter_set = 1;
  2834. wpa_printf(MSG_DEBUG, "Updated key replay counter");
  2835. }
  2836. void wpa_sm_set_ptk_kck_kek(struct wpa_sm *sm,
  2837. const u8 *ptk_kck, size_t ptk_kck_len,
  2838. const u8 *ptk_kek, size_t ptk_kek_len)
  2839. {
  2840. if (ptk_kck && ptk_kck_len <= WPA_KCK_MAX_LEN) {
  2841. os_memcpy(sm->ptk.kck, ptk_kck, ptk_kck_len);
  2842. sm->ptk.kck_len = ptk_kck_len;
  2843. wpa_printf(MSG_DEBUG, "Updated PTK KCK");
  2844. }
  2845. if (ptk_kek && ptk_kek_len <= WPA_KEK_MAX_LEN) {
  2846. os_memcpy(sm->ptk.kek, ptk_kek, ptk_kek_len);
  2847. sm->ptk.kek_len = ptk_kek_len;
  2848. wpa_printf(MSG_DEBUG, "Updated PTK KEK");
  2849. }
  2850. sm->ptk_set = 1;
  2851. }
  2852. #ifdef CONFIG_TESTING_OPTIONS
  2853. void wpa_sm_set_test_assoc_ie(struct wpa_sm *sm, struct wpabuf *buf)
  2854. {
  2855. wpabuf_free(sm->test_assoc_ie);
  2856. sm->test_assoc_ie = buf;
  2857. }
  2858. #endif /* CONFIG_TESTING_OPTIONS */
  2859. #ifdef CONFIG_FILS
  2860. struct wpabuf * fils_build_auth(struct wpa_sm *sm, int dh_group, const u8 *md)
  2861. {
  2862. struct wpabuf *buf = NULL;
  2863. struct wpabuf *erp_msg;
  2864. struct wpabuf *pub = NULL;
  2865. erp_msg = eapol_sm_build_erp_reauth_start(sm->eapol);
  2866. if (!erp_msg && !sm->cur_pmksa) {
  2867. wpa_printf(MSG_DEBUG,
  2868. "FILS: Neither ERP EAP-Initiate/Re-auth nor PMKSA cache entry is available - skip FILS");
  2869. goto fail;
  2870. }
  2871. wpa_printf(MSG_DEBUG, "FILS: Try to use FILS (erp=%d pmksa_cache=%d)",
  2872. erp_msg != NULL, sm->cur_pmksa != NULL);
  2873. sm->fils_completed = 0;
  2874. if (!sm->assoc_wpa_ie) {
  2875. wpa_printf(MSG_INFO, "FILS: No own RSN IE set for FILS");
  2876. goto fail;
  2877. }
  2878. if (random_get_bytes(sm->fils_nonce, FILS_NONCE_LEN) < 0 ||
  2879. random_get_bytes(sm->fils_session, FILS_SESSION_LEN) < 0)
  2880. goto fail;
  2881. wpa_hexdump(MSG_DEBUG, "FILS: Generated FILS Nonce",
  2882. sm->fils_nonce, FILS_NONCE_LEN);
  2883. wpa_hexdump(MSG_DEBUG, "FILS: Generated FILS Session",
  2884. sm->fils_session, FILS_SESSION_LEN);
  2885. #ifdef CONFIG_FILS_SK_PFS
  2886. sm->fils_dh_group = dh_group;
  2887. if (dh_group) {
  2888. crypto_ecdh_deinit(sm->fils_ecdh);
  2889. sm->fils_ecdh = crypto_ecdh_init(dh_group);
  2890. if (!sm->fils_ecdh) {
  2891. wpa_printf(MSG_INFO,
  2892. "FILS: Could not initialize ECDH with group %d",
  2893. dh_group);
  2894. goto fail;
  2895. }
  2896. pub = crypto_ecdh_get_pubkey(sm->fils_ecdh, 1);
  2897. if (!pub)
  2898. goto fail;
  2899. wpa_hexdump_buf(MSG_DEBUG, "FILS: Element (DH public key)",
  2900. pub);
  2901. sm->fils_dh_elem_len = wpabuf_len(pub);
  2902. }
  2903. #endif /* CONFIG_FILS_SK_PFS */
  2904. buf = wpabuf_alloc(1000 + sm->assoc_wpa_ie_len +
  2905. (pub ? wpabuf_len(pub) : 0));
  2906. if (!buf)
  2907. goto fail;
  2908. /* Fields following the Authentication algorithm number field */
  2909. /* Authentication Transaction seq# */
  2910. wpabuf_put_le16(buf, 1);
  2911. /* Status Code */
  2912. wpabuf_put_le16(buf, WLAN_STATUS_SUCCESS);
  2913. /* TODO: FILS PK */
  2914. #ifdef CONFIG_FILS_SK_PFS
  2915. if (dh_group) {
  2916. /* Finite Cyclic Group */
  2917. wpabuf_put_le16(buf, dh_group);
  2918. /* Element */
  2919. wpabuf_put_buf(buf, pub);
  2920. }
  2921. #endif /* CONFIG_FILS_SK_PFS */
  2922. /* RSNE */
  2923. wpa_hexdump(MSG_DEBUG, "FILS: RSNE in FILS Authentication frame",
  2924. sm->assoc_wpa_ie, sm->assoc_wpa_ie_len);
  2925. wpabuf_put_data(buf, sm->assoc_wpa_ie, sm->assoc_wpa_ie_len);
  2926. if (md) {
  2927. /* MDE when using FILS for FT initial association */
  2928. struct rsn_mdie *mdie;
  2929. wpabuf_put_u8(buf, WLAN_EID_MOBILITY_DOMAIN);
  2930. wpabuf_put_u8(buf, sizeof(*mdie));
  2931. mdie = wpabuf_put(buf, sizeof(*mdie));
  2932. os_memcpy(mdie->mobility_domain, md, MOBILITY_DOMAIN_ID_LEN);
  2933. mdie->ft_capab = 0;
  2934. }
  2935. /* FILS Nonce */
  2936. wpabuf_put_u8(buf, WLAN_EID_EXTENSION); /* Element ID */
  2937. wpabuf_put_u8(buf, 1 + FILS_NONCE_LEN); /* Length */
  2938. /* Element ID Extension */
  2939. wpabuf_put_u8(buf, WLAN_EID_EXT_FILS_NONCE);
  2940. wpabuf_put_data(buf, sm->fils_nonce, FILS_NONCE_LEN);
  2941. /* FILS Session */
  2942. wpabuf_put_u8(buf, WLAN_EID_EXTENSION); /* Element ID */
  2943. wpabuf_put_u8(buf, 1 + FILS_SESSION_LEN); /* Length */
  2944. /* Element ID Extension */
  2945. wpabuf_put_u8(buf, WLAN_EID_EXT_FILS_SESSION);
  2946. wpabuf_put_data(buf, sm->fils_session, FILS_SESSION_LEN);
  2947. /* FILS Wrapped Data */
  2948. sm->fils_erp_pmkid_set = 0;
  2949. if (erp_msg) {
  2950. wpabuf_put_u8(buf, WLAN_EID_EXTENSION); /* Element ID */
  2951. wpabuf_put_u8(buf, 1 + wpabuf_len(erp_msg)); /* Length */
  2952. /* Element ID Extension */
  2953. wpabuf_put_u8(buf, WLAN_EID_EXT_FILS_WRAPPED_DATA);
  2954. wpabuf_put_buf(buf, erp_msg);
  2955. /* Calculate pending PMKID here so that we do not need to
  2956. * maintain a copy of the EAP-Initiate/Reauth message. */
  2957. if (fils_pmkid_erp(sm->key_mgmt, wpabuf_head(erp_msg),
  2958. wpabuf_len(erp_msg),
  2959. sm->fils_erp_pmkid) == 0)
  2960. sm->fils_erp_pmkid_set = 1;
  2961. }
  2962. wpa_hexdump_buf(MSG_DEBUG, "RSN: FILS fields for Authentication frame",
  2963. buf);
  2964. fail:
  2965. wpabuf_free(erp_msg);
  2966. wpabuf_free(pub);
  2967. return buf;
  2968. }
  2969. int fils_process_auth(struct wpa_sm *sm, const u8 *bssid, const u8 *data,
  2970. size_t len)
  2971. {
  2972. const u8 *pos, *end;
  2973. struct ieee802_11_elems elems;
  2974. struct wpa_ie_data rsn;
  2975. int pmkid_match = 0;
  2976. u8 ick[FILS_ICK_MAX_LEN];
  2977. size_t ick_len;
  2978. int res;
  2979. struct wpabuf *dh_ss = NULL;
  2980. const u8 *g_sta = NULL;
  2981. size_t g_sta_len = 0;
  2982. const u8 *g_ap = NULL;
  2983. size_t g_ap_len = 0;
  2984. struct wpabuf *pub = NULL;
  2985. os_memcpy(sm->bssid, bssid, ETH_ALEN);
  2986. wpa_hexdump(MSG_DEBUG, "FILS: Authentication frame fields",
  2987. data, len);
  2988. pos = data;
  2989. end = data + len;
  2990. /* TODO: FILS PK */
  2991. #ifdef CONFIG_FILS_SK_PFS
  2992. if (sm->fils_dh_group) {
  2993. u16 group;
  2994. /* Using FILS PFS */
  2995. /* Finite Cyclic Group */
  2996. if (end - pos < 2) {
  2997. wpa_printf(MSG_DEBUG,
  2998. "FILS: No room for Finite Cyclic Group");
  2999. goto fail;
  3000. }
  3001. group = WPA_GET_LE16(pos);
  3002. pos += 2;
  3003. if (group != sm->fils_dh_group) {
  3004. wpa_printf(MSG_DEBUG,
  3005. "FILS: Unexpected change in Finite Cyclic Group: %u (expected %u)",
  3006. group, sm->fils_dh_group);
  3007. goto fail;
  3008. }
  3009. /* Element */
  3010. if ((size_t) (end - pos) < sm->fils_dh_elem_len) {
  3011. wpa_printf(MSG_DEBUG, "FILS: No room for Element");
  3012. goto fail;
  3013. }
  3014. if (!sm->fils_ecdh) {
  3015. wpa_printf(MSG_DEBUG, "FILS: No ECDH state available");
  3016. goto fail;
  3017. }
  3018. dh_ss = crypto_ecdh_set_peerkey(sm->fils_ecdh, 1, pos,
  3019. sm->fils_dh_elem_len);
  3020. if (!dh_ss) {
  3021. wpa_printf(MSG_DEBUG, "FILS: ECDH operation failed");
  3022. goto fail;
  3023. }
  3024. wpa_hexdump_buf_key(MSG_DEBUG, "FILS: DH_SS", dh_ss);
  3025. g_ap = pos;
  3026. g_ap_len = sm->fils_dh_elem_len;
  3027. pos += sm->fils_dh_elem_len;
  3028. }
  3029. #endif /* CONFIG_FILS_SK_PFS */
  3030. wpa_hexdump(MSG_DEBUG, "FILS: Remaining IEs", pos, end - pos);
  3031. if (ieee802_11_parse_elems(pos, end - pos, &elems, 1) == ParseFailed) {
  3032. wpa_printf(MSG_DEBUG, "FILS: Could not parse elements");
  3033. goto fail;
  3034. }
  3035. /* RSNE */
  3036. wpa_hexdump(MSG_DEBUG, "FILS: RSN element", elems.rsn_ie,
  3037. elems.rsn_ie_len);
  3038. if (!elems.rsn_ie ||
  3039. wpa_parse_wpa_ie_rsn(elems.rsn_ie - 2, elems.rsn_ie_len + 2,
  3040. &rsn) < 0) {
  3041. wpa_printf(MSG_DEBUG, "FILS: No RSN element");
  3042. goto fail;
  3043. }
  3044. if (!elems.fils_nonce) {
  3045. wpa_printf(MSG_DEBUG, "FILS: No FILS Nonce field");
  3046. goto fail;
  3047. }
  3048. os_memcpy(sm->fils_anonce, elems.fils_nonce, FILS_NONCE_LEN);
  3049. wpa_hexdump(MSG_DEBUG, "FILS: ANonce", sm->fils_anonce, FILS_NONCE_LEN);
  3050. if (wpa_key_mgmt_ft(sm->key_mgmt)) {
  3051. struct wpa_ft_ies parse;
  3052. if (!elems.mdie || !elems.ftie) {
  3053. wpa_printf(MSG_DEBUG, "FILS+FT: No MDE or FTE");
  3054. goto fail;
  3055. }
  3056. if (wpa_ft_parse_ies(pos, end - pos, &parse) < 0) {
  3057. wpa_printf(MSG_DEBUG, "FILS+FT: Failed to parse IEs");
  3058. goto fail;
  3059. }
  3060. if (!parse.r0kh_id) {
  3061. wpa_printf(MSG_DEBUG,
  3062. "FILS+FT: No R0KH-ID subelem in FTE");
  3063. goto fail;
  3064. }
  3065. os_memcpy(sm->r0kh_id, parse.r0kh_id, parse.r0kh_id_len);
  3066. sm->r0kh_id_len = parse.r0kh_id_len;
  3067. wpa_hexdump_ascii(MSG_DEBUG, "FILS+FT: R0KH-ID",
  3068. sm->r0kh_id, sm->r0kh_id_len);
  3069. if (!parse.r1kh_id) {
  3070. wpa_printf(MSG_DEBUG,
  3071. "FILS+FT: No R1KH-ID subelem in FTE");
  3072. goto fail;
  3073. }
  3074. os_memcpy(sm->r1kh_id, parse.r1kh_id, FT_R1KH_ID_LEN);
  3075. wpa_hexdump(MSG_DEBUG, "FILS+FT: R1KH-ID",
  3076. sm->r1kh_id, FT_R1KH_ID_LEN);
  3077. /* TODO: Check MDE and FTE payload */
  3078. wpabuf_free(sm->fils_ft_ies);
  3079. sm->fils_ft_ies = wpabuf_alloc(2 + elems.mdie_len +
  3080. 2 + elems.ftie_len);
  3081. if (!sm->fils_ft_ies)
  3082. goto fail;
  3083. wpabuf_put_data(sm->fils_ft_ies, elems.mdie - 2,
  3084. 2 + elems.mdie_len);
  3085. wpabuf_put_data(sm->fils_ft_ies, elems.ftie - 2,
  3086. 2 + elems.ftie_len);
  3087. } else {
  3088. wpabuf_free(sm->fils_ft_ies);
  3089. sm->fils_ft_ies = NULL;
  3090. }
  3091. /* PMKID List */
  3092. if (rsn.pmkid && rsn.num_pmkid > 0) {
  3093. wpa_hexdump(MSG_DEBUG, "FILS: PMKID List",
  3094. rsn.pmkid, rsn.num_pmkid * PMKID_LEN);
  3095. if (rsn.num_pmkid != 1) {
  3096. wpa_printf(MSG_DEBUG, "FILS: Invalid PMKID selection");
  3097. goto fail;
  3098. }
  3099. wpa_hexdump(MSG_DEBUG, "FILS: PMKID", rsn.pmkid, PMKID_LEN);
  3100. if (os_memcmp(sm->cur_pmksa->pmkid, rsn.pmkid, PMKID_LEN) != 0)
  3101. {
  3102. wpa_printf(MSG_DEBUG, "FILS: PMKID mismatch");
  3103. wpa_hexdump(MSG_DEBUG, "FILS: Expected PMKID",
  3104. sm->cur_pmksa->pmkid, PMKID_LEN);
  3105. goto fail;
  3106. }
  3107. wpa_printf(MSG_DEBUG,
  3108. "FILS: Matching PMKID - continue using PMKSA caching");
  3109. pmkid_match = 1;
  3110. }
  3111. if (!pmkid_match && sm->cur_pmksa) {
  3112. wpa_printf(MSG_DEBUG,
  3113. "FILS: No PMKID match - cannot use cached PMKSA entry");
  3114. sm->cur_pmksa = NULL;
  3115. }
  3116. /* FILS Session */
  3117. if (!elems.fils_session) {
  3118. wpa_printf(MSG_DEBUG, "FILS: No FILS Session element");
  3119. goto fail;
  3120. }
  3121. wpa_hexdump(MSG_DEBUG, "FILS: FILS Session", elems.fils_session,
  3122. FILS_SESSION_LEN);
  3123. if (os_memcmp(sm->fils_session, elems.fils_session, FILS_SESSION_LEN)
  3124. != 0) {
  3125. wpa_printf(MSG_DEBUG, "FILS: Session mismatch");
  3126. wpa_hexdump(MSG_DEBUG, "FILS: Expected FILS Session",
  3127. sm->fils_session, FILS_SESSION_LEN);
  3128. goto fail;
  3129. }
  3130. /* FILS Wrapped Data */
  3131. if (!sm->cur_pmksa && elems.fils_wrapped_data) {
  3132. u8 rmsk[ERP_MAX_KEY_LEN];
  3133. size_t rmsk_len;
  3134. wpa_hexdump(MSG_DEBUG, "FILS: Wrapped Data",
  3135. elems.fils_wrapped_data,
  3136. elems.fils_wrapped_data_len);
  3137. eapol_sm_process_erp_finish(sm->eapol, elems.fils_wrapped_data,
  3138. elems.fils_wrapped_data_len);
  3139. if (eapol_sm_failed(sm->eapol))
  3140. goto fail;
  3141. rmsk_len = ERP_MAX_KEY_LEN;
  3142. res = eapol_sm_get_key(sm->eapol, rmsk, rmsk_len);
  3143. if (res == PMK_LEN) {
  3144. rmsk_len = PMK_LEN;
  3145. res = eapol_sm_get_key(sm->eapol, rmsk, rmsk_len);
  3146. }
  3147. if (res)
  3148. goto fail;
  3149. res = fils_rmsk_to_pmk(sm->key_mgmt, rmsk, rmsk_len,
  3150. sm->fils_nonce, sm->fils_anonce,
  3151. dh_ss ? wpabuf_head(dh_ss) : NULL,
  3152. dh_ss ? wpabuf_len(dh_ss) : 0,
  3153. sm->pmk, &sm->pmk_len);
  3154. os_memset(rmsk, 0, sizeof(rmsk));
  3155. /* Don't use DHss in PTK derivation if PMKSA caching is not
  3156. * used. */
  3157. wpabuf_clear_free(dh_ss);
  3158. dh_ss = NULL;
  3159. if (res)
  3160. goto fail;
  3161. if (!sm->fils_erp_pmkid_set) {
  3162. wpa_printf(MSG_DEBUG, "FILS: PMKID not available");
  3163. goto fail;
  3164. }
  3165. wpa_hexdump(MSG_DEBUG, "FILS: PMKID", sm->fils_erp_pmkid,
  3166. PMKID_LEN);
  3167. wpa_printf(MSG_DEBUG, "FILS: ERP processing succeeded - add PMKSA cache entry for the result");
  3168. sm->cur_pmksa = pmksa_cache_add(sm->pmksa, sm->pmk, sm->pmk_len,
  3169. sm->fils_erp_pmkid, NULL, 0,
  3170. sm->bssid, sm->own_addr,
  3171. sm->network_ctx, sm->key_mgmt,
  3172. NULL);
  3173. }
  3174. if (!sm->cur_pmksa) {
  3175. wpa_printf(MSG_DEBUG,
  3176. "FILS: No remaining options to continue FILS authentication");
  3177. goto fail;
  3178. }
  3179. if (fils_pmk_to_ptk(sm->pmk, sm->pmk_len, sm->own_addr, sm->bssid,
  3180. sm->fils_nonce, sm->fils_anonce,
  3181. dh_ss ? wpabuf_head(dh_ss) : NULL,
  3182. dh_ss ? wpabuf_len(dh_ss) : 0,
  3183. &sm->ptk, ick, &ick_len,
  3184. sm->key_mgmt, sm->pairwise_cipher,
  3185. sm->fils_ft, &sm->fils_ft_len) < 0) {
  3186. wpa_printf(MSG_DEBUG, "FILS: Failed to derive PTK");
  3187. goto fail;
  3188. }
  3189. wpabuf_clear_free(dh_ss);
  3190. dh_ss = NULL;
  3191. sm->ptk_set = 1;
  3192. sm->tptk_set = 0;
  3193. os_memset(&sm->tptk, 0, sizeof(sm->tptk));
  3194. #ifdef CONFIG_FILS_SK_PFS
  3195. if (sm->fils_dh_group) {
  3196. if (!sm->fils_ecdh) {
  3197. wpa_printf(MSG_INFO, "FILS: ECDH not initialized");
  3198. goto fail;
  3199. }
  3200. pub = crypto_ecdh_get_pubkey(sm->fils_ecdh, 1);
  3201. if (!pub)
  3202. goto fail;
  3203. wpa_hexdump_buf(MSG_DEBUG, "FILS: gSTA", pub);
  3204. g_sta = wpabuf_head(pub);
  3205. g_sta_len = wpabuf_len(pub);
  3206. if (!g_ap) {
  3207. wpa_printf(MSG_INFO, "FILS: gAP not available");
  3208. goto fail;
  3209. }
  3210. wpa_hexdump(MSG_DEBUG, "FILS: gAP", g_ap, g_ap_len);
  3211. }
  3212. #endif /* CONFIG_FILS_SK_PFS */
  3213. res = fils_key_auth_sk(ick, ick_len, sm->fils_nonce,
  3214. sm->fils_anonce, sm->own_addr, sm->bssid,
  3215. g_sta, g_sta_len, g_ap, g_ap_len,
  3216. sm->key_mgmt, sm->fils_key_auth_sta,
  3217. sm->fils_key_auth_ap,
  3218. &sm->fils_key_auth_len);
  3219. wpabuf_free(pub);
  3220. os_memset(ick, 0, sizeof(ick));
  3221. return res;
  3222. fail:
  3223. wpabuf_free(pub);
  3224. wpabuf_clear_free(dh_ss);
  3225. return -1;
  3226. }
  3227. #ifdef CONFIG_IEEE80211R
  3228. static int fils_ft_build_assoc_req_rsne(struct wpa_sm *sm, struct wpabuf *buf)
  3229. {
  3230. struct rsn_ie_hdr *rsnie;
  3231. u16 capab;
  3232. u8 *pos;
  3233. /* RSNIE[PMKR0Name/PMKR1Name] */
  3234. rsnie = wpabuf_put(buf, sizeof(*rsnie));
  3235. rsnie->elem_id = WLAN_EID_RSN;
  3236. WPA_PUT_LE16(rsnie->version, RSN_VERSION);
  3237. /* Group Suite Selector */
  3238. if (!wpa_cipher_valid_group(sm->group_cipher)) {
  3239. wpa_printf(MSG_WARNING, "FT: Invalid group cipher (%d)",
  3240. sm->group_cipher);
  3241. return -1;
  3242. }
  3243. pos = wpabuf_put(buf, RSN_SELECTOR_LEN);
  3244. RSN_SELECTOR_PUT(pos, wpa_cipher_to_suite(WPA_PROTO_RSN,
  3245. sm->group_cipher));
  3246. /* Pairwise Suite Count */
  3247. wpabuf_put_le16(buf, 1);
  3248. /* Pairwise Suite List */
  3249. if (!wpa_cipher_valid_pairwise(sm->pairwise_cipher)) {
  3250. wpa_printf(MSG_WARNING, "FT: Invalid pairwise cipher (%d)",
  3251. sm->pairwise_cipher);
  3252. return -1;
  3253. }
  3254. pos = wpabuf_put(buf, RSN_SELECTOR_LEN);
  3255. RSN_SELECTOR_PUT(pos, wpa_cipher_to_suite(WPA_PROTO_RSN,
  3256. sm->pairwise_cipher));
  3257. /* Authenticated Key Management Suite Count */
  3258. wpabuf_put_le16(buf, 1);
  3259. /* Authenticated Key Management Suite List */
  3260. pos = wpabuf_put(buf, RSN_SELECTOR_LEN);
  3261. if (sm->key_mgmt == WPA_KEY_MGMT_FT_FILS_SHA256)
  3262. RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_FT_FILS_SHA256);
  3263. else if (sm->key_mgmt == WPA_KEY_MGMT_FT_FILS_SHA384)
  3264. RSN_SELECTOR_PUT(pos, RSN_AUTH_KEY_MGMT_FT_FILS_SHA384);
  3265. else {
  3266. wpa_printf(MSG_WARNING,
  3267. "FILS+FT: Invalid key management type (%d)",
  3268. sm->key_mgmt);
  3269. return -1;
  3270. }
  3271. /* RSN Capabilities */
  3272. capab = 0;
  3273. #ifdef CONFIG_IEEE80211W
  3274. if (sm->mgmt_group_cipher == WPA_CIPHER_AES_128_CMAC)
  3275. capab |= WPA_CAPABILITY_MFPC;
  3276. #endif /* CONFIG_IEEE80211W */
  3277. wpabuf_put_le16(buf, capab);
  3278. /* PMKID Count */
  3279. wpabuf_put_le16(buf, 1);
  3280. /* PMKID List [PMKR1Name] */
  3281. wpa_hexdump_key(MSG_DEBUG, "FILS+FT: XXKey (FILS-FT)",
  3282. sm->fils_ft, sm->fils_ft_len);
  3283. wpa_hexdump_ascii(MSG_DEBUG, "FILS+FT: SSID", sm->ssid, sm->ssid_len);
  3284. wpa_hexdump(MSG_DEBUG, "FILS+FT: MDID",
  3285. sm->mobility_domain, MOBILITY_DOMAIN_ID_LEN);
  3286. wpa_hexdump_ascii(MSG_DEBUG, "FILS+FT: R0KH-ID",
  3287. sm->r0kh_id, sm->r0kh_id_len);
  3288. if (wpa_derive_pmk_r0(sm->fils_ft, sm->fils_ft_len, sm->ssid,
  3289. sm->ssid_len, sm->mobility_domain,
  3290. sm->r0kh_id, sm->r0kh_id_len, sm->own_addr,
  3291. sm->pmk_r0, sm->pmk_r0_name) < 0) {
  3292. wpa_printf(MSG_WARNING, "FILS+FT: Could not derive PMK-R0");
  3293. return -1;
  3294. }
  3295. wpa_hexdump_key(MSG_DEBUG, "FILS+FT: PMK-R0", sm->pmk_r0, PMK_LEN);
  3296. wpa_hexdump(MSG_DEBUG, "FILS+FT: PMKR0Name",
  3297. sm->pmk_r0_name, WPA_PMK_NAME_LEN);
  3298. wpa_printf(MSG_DEBUG, "FILS+FT: R1KH-ID: " MACSTR,
  3299. MAC2STR(sm->r1kh_id));
  3300. pos = wpabuf_put(buf, WPA_PMK_NAME_LEN);
  3301. if (wpa_derive_pmk_r1_name(sm->pmk_r0_name, sm->r1kh_id, sm->own_addr,
  3302. pos) < 0) {
  3303. wpa_printf(MSG_WARNING, "FILS+FT: Could not derive PMKR1Name");
  3304. return -1;
  3305. }
  3306. wpa_hexdump(MSG_DEBUG, "FILS+FT: PMKR1Name", pos, WPA_PMK_NAME_LEN);
  3307. #ifdef CONFIG_IEEE80211W
  3308. if (sm->mgmt_group_cipher == WPA_CIPHER_AES_128_CMAC) {
  3309. /* Management Group Cipher Suite */
  3310. pos = wpabuf_put(buf, RSN_SELECTOR_LEN);
  3311. RSN_SELECTOR_PUT(pos, RSN_CIPHER_SUITE_AES_128_CMAC);
  3312. }
  3313. #endif /* CONFIG_IEEE80211W */
  3314. rsnie->len = ((u8 *) wpabuf_put(buf, 0) - (u8 *) rsnie) - 2;
  3315. return 0;
  3316. }
  3317. #endif /* CONFIG_IEEE80211R */
  3318. struct wpabuf * fils_build_assoc_req(struct wpa_sm *sm, const u8 **kek,
  3319. size_t *kek_len, const u8 **snonce,
  3320. const u8 **anonce,
  3321. const struct wpabuf **hlp,
  3322. unsigned int num_hlp)
  3323. {
  3324. struct wpabuf *buf;
  3325. size_t len;
  3326. unsigned int i;
  3327. len = 1000;
  3328. #ifdef CONFIG_IEEE80211R
  3329. if (sm->fils_ft_ies)
  3330. len += wpabuf_len(sm->fils_ft_ies);
  3331. if (wpa_key_mgmt_ft(sm->key_mgmt))
  3332. len += 256;
  3333. #endif /* CONFIG_IEEE80211R */
  3334. for (i = 0; hlp && i < num_hlp; i++)
  3335. len += 10 + wpabuf_len(hlp[i]);
  3336. buf = wpabuf_alloc(len);
  3337. if (!buf)
  3338. return NULL;
  3339. #ifdef CONFIG_IEEE80211R
  3340. if (wpa_key_mgmt_ft(sm->key_mgmt) && sm->fils_ft_ies) {
  3341. /* MDE and FTE when using FILS+FT */
  3342. wpabuf_put_buf(buf, sm->fils_ft_ies);
  3343. /* RSNE with PMKR1Name in PMKID field */
  3344. if (fils_ft_build_assoc_req_rsne(sm, buf) < 0) {
  3345. wpabuf_free(buf);
  3346. return NULL;
  3347. }
  3348. }
  3349. #endif /* CONFIG_IEEE80211R */
  3350. /* FILS Session */
  3351. wpabuf_put_u8(buf, WLAN_EID_EXTENSION); /* Element ID */
  3352. wpabuf_put_u8(buf, 1 + FILS_SESSION_LEN); /* Length */
  3353. /* Element ID Extension */
  3354. wpabuf_put_u8(buf, WLAN_EID_EXT_FILS_SESSION);
  3355. wpabuf_put_data(buf, sm->fils_session, FILS_SESSION_LEN);
  3356. /* Everything after FILS Session element gets encrypted in the driver
  3357. * with KEK. The buffer returned from here is the plaintext version. */
  3358. /* TODO: FILS Public Key */
  3359. /* FILS Key Confirm */
  3360. wpabuf_put_u8(buf, WLAN_EID_EXTENSION); /* Element ID */
  3361. wpabuf_put_u8(buf, 1 + sm->fils_key_auth_len); /* Length */
  3362. /* Element ID Extension */
  3363. wpabuf_put_u8(buf, WLAN_EID_EXT_FILS_KEY_CONFIRM);
  3364. wpabuf_put_data(buf, sm->fils_key_auth_sta, sm->fils_key_auth_len);
  3365. /* FILS HLP Container */
  3366. for (i = 0; hlp && i < num_hlp; i++) {
  3367. const u8 *pos = wpabuf_head(hlp[i]);
  3368. size_t left = wpabuf_len(hlp[i]);
  3369. wpabuf_put_u8(buf, WLAN_EID_EXTENSION); /* Element ID */
  3370. if (left <= 254)
  3371. len = 1 + left;
  3372. else
  3373. len = 255;
  3374. wpabuf_put_u8(buf, len); /* Length */
  3375. /* Element ID Extension */
  3376. wpabuf_put_u8(buf, WLAN_EID_EXT_FILS_HLP_CONTAINER);
  3377. /* Destination MAC Address, Source MAC Address, HLP Packet.
  3378. * HLP Packet is in MSDU format (i.e., included the LLC/SNAP
  3379. * header when LPD is used). */
  3380. wpabuf_put_data(buf, pos, len - 1);
  3381. pos += len - 1;
  3382. left -= len - 1;
  3383. while (left) {
  3384. wpabuf_put_u8(buf, WLAN_EID_FRAGMENT);
  3385. len = left > 255 ? 255 : left;
  3386. wpabuf_put_u8(buf, len);
  3387. wpabuf_put_data(buf, pos, len);
  3388. pos += len;
  3389. left -= len;
  3390. }
  3391. }
  3392. /* TODO: FILS IP Address Assignment */
  3393. wpa_hexdump_buf(MSG_DEBUG, "FILS: Association Request plaintext", buf);
  3394. *kek = sm->ptk.kek;
  3395. *kek_len = sm->ptk.kek_len;
  3396. wpa_hexdump_key(MSG_DEBUG, "FILS: KEK for AEAD", *kek, *kek_len);
  3397. *snonce = sm->fils_nonce;
  3398. wpa_hexdump(MSG_DEBUG, "FILS: SNonce for AEAD AAD",
  3399. *snonce, FILS_NONCE_LEN);
  3400. *anonce = sm->fils_anonce;
  3401. wpa_hexdump(MSG_DEBUG, "FILS: ANonce for AEAD AAD",
  3402. *anonce, FILS_NONCE_LEN);
  3403. return buf;
  3404. }
  3405. static void fils_process_hlp_resp(struct wpa_sm *sm, const u8 *resp, size_t len)
  3406. {
  3407. const u8 *pos, *end;
  3408. wpa_hexdump(MSG_MSGDUMP, "FILS: HLP response", resp, len);
  3409. if (len < 2 * ETH_ALEN)
  3410. return;
  3411. pos = resp + 2 * ETH_ALEN;
  3412. end = resp + len;
  3413. if (end - pos >= 6 &&
  3414. os_memcmp(pos, "\xaa\xaa\x03\x00\x00\x00", 6) == 0)
  3415. pos += 6; /* Remove SNAP/LLC header */
  3416. wpa_sm_fils_hlp_rx(sm, resp, resp + ETH_ALEN, pos, end - pos);
  3417. }
  3418. static void fils_process_hlp_container(struct wpa_sm *sm, const u8 *pos,
  3419. size_t len)
  3420. {
  3421. const u8 *end = pos + len;
  3422. u8 *tmp, *tmp_pos;
  3423. /* Check if there are any FILS HLP Container elements */
  3424. while (end - pos >= 2) {
  3425. if (2 + pos[1] > end - pos)
  3426. return;
  3427. if (pos[0] == WLAN_EID_EXTENSION &&
  3428. pos[1] >= 1 + 2 * ETH_ALEN &&
  3429. pos[2] == WLAN_EID_EXT_FILS_HLP_CONTAINER)
  3430. break;
  3431. pos += 2 + pos[1];
  3432. }
  3433. if (end - pos < 2)
  3434. return; /* No FILS HLP Container elements */
  3435. tmp = os_malloc(end - pos);
  3436. if (!tmp)
  3437. return;
  3438. while (end - pos >= 2) {
  3439. if (2 + pos[1] > end - pos ||
  3440. pos[0] != WLAN_EID_EXTENSION ||
  3441. pos[1] < 1 + 2 * ETH_ALEN ||
  3442. pos[2] != WLAN_EID_EXT_FILS_HLP_CONTAINER)
  3443. break;
  3444. tmp_pos = tmp;
  3445. os_memcpy(tmp_pos, pos + 3, pos[1] - 1);
  3446. tmp_pos += pos[1] - 1;
  3447. pos += 2 + pos[1];
  3448. /* Add possible fragments */
  3449. while (end - pos >= 2 && pos[0] == WLAN_EID_FRAGMENT &&
  3450. 2 + pos[1] <= end - pos) {
  3451. os_memcpy(tmp_pos, pos + 2, pos[1]);
  3452. tmp_pos += pos[1];
  3453. pos += 2 + pos[1];
  3454. }
  3455. fils_process_hlp_resp(sm, tmp, tmp_pos - tmp);
  3456. }
  3457. os_free(tmp);
  3458. }
  3459. int fils_process_assoc_resp(struct wpa_sm *sm, const u8 *resp, size_t len)
  3460. {
  3461. const struct ieee80211_mgmt *mgmt;
  3462. const u8 *end, *ie_start;
  3463. struct ieee802_11_elems elems;
  3464. int keylen, rsclen;
  3465. enum wpa_alg alg;
  3466. struct wpa_gtk_data gd;
  3467. int maxkeylen;
  3468. struct wpa_eapol_ie_parse kde;
  3469. if (!sm || !sm->ptk_set) {
  3470. wpa_printf(MSG_DEBUG, "FILS: No KEK available");
  3471. return -1;
  3472. }
  3473. if (!wpa_key_mgmt_fils(sm->key_mgmt)) {
  3474. wpa_printf(MSG_DEBUG, "FILS: Not a FILS AKM");
  3475. return -1;
  3476. }
  3477. if (sm->fils_completed) {
  3478. wpa_printf(MSG_DEBUG,
  3479. "FILS: Association has already been completed for this FILS authentication - ignore unexpected retransmission");
  3480. return -1;
  3481. }
  3482. wpa_hexdump(MSG_DEBUG, "FILS: (Re)Association Response frame",
  3483. resp, len);
  3484. mgmt = (const struct ieee80211_mgmt *) resp;
  3485. if (len < IEEE80211_HDRLEN + sizeof(mgmt->u.assoc_resp))
  3486. return -1;
  3487. end = resp + len;
  3488. /* Same offset for Association Response and Reassociation Response */
  3489. ie_start = mgmt->u.assoc_resp.variable;
  3490. if (ieee802_11_parse_elems(ie_start, end - ie_start, &elems, 1) ==
  3491. ParseFailed) {
  3492. wpa_printf(MSG_DEBUG,
  3493. "FILS: Failed to parse decrypted elements");
  3494. goto fail;
  3495. }
  3496. if (!elems.fils_session) {
  3497. wpa_printf(MSG_DEBUG, "FILS: No FILS Session element");
  3498. return -1;
  3499. }
  3500. if (os_memcmp(elems.fils_session, sm->fils_session,
  3501. FILS_SESSION_LEN) != 0) {
  3502. wpa_printf(MSG_DEBUG, "FILS: FILS Session mismatch");
  3503. wpa_hexdump(MSG_DEBUG, "FILS: Received FILS Session",
  3504. elems.fils_session, FILS_SESSION_LEN);
  3505. wpa_hexdump(MSG_DEBUG, "FILS: Expected FILS Session",
  3506. sm->fils_session, FILS_SESSION_LEN);
  3507. }
  3508. /* TODO: FILS Public Key */
  3509. if (!elems.fils_key_confirm) {
  3510. wpa_printf(MSG_DEBUG, "FILS: No FILS Key Confirm element");
  3511. goto fail;
  3512. }
  3513. if (elems.fils_key_confirm_len != sm->fils_key_auth_len) {
  3514. wpa_printf(MSG_DEBUG,
  3515. "FILS: Unexpected Key-Auth length %d (expected %d)",
  3516. elems.fils_key_confirm_len,
  3517. (int) sm->fils_key_auth_len);
  3518. goto fail;
  3519. }
  3520. if (os_memcmp(elems.fils_key_confirm, sm->fils_key_auth_ap,
  3521. sm->fils_key_auth_len) != 0) {
  3522. wpa_printf(MSG_DEBUG, "FILS: Key-Auth mismatch");
  3523. wpa_hexdump(MSG_DEBUG, "FILS: Received Key-Auth",
  3524. elems.fils_key_confirm,
  3525. elems.fils_key_confirm_len);
  3526. wpa_hexdump(MSG_DEBUG, "FILS: Expected Key-Auth",
  3527. sm->fils_key_auth_ap, sm->fils_key_auth_len);
  3528. goto fail;
  3529. }
  3530. /* Key Delivery */
  3531. if (!elems.key_delivery) {
  3532. wpa_printf(MSG_DEBUG, "FILS: No Key Delivery element");
  3533. goto fail;
  3534. }
  3535. /* Parse GTK and set the key to the driver */
  3536. os_memset(&gd, 0, sizeof(gd));
  3537. if (wpa_supplicant_parse_ies(elems.key_delivery + WPA_KEY_RSC_LEN,
  3538. elems.key_delivery_len - WPA_KEY_RSC_LEN,
  3539. &kde) < 0) {
  3540. wpa_printf(MSG_DEBUG, "FILS: Failed to parse KDEs");
  3541. goto fail;
  3542. }
  3543. if (!kde.gtk) {
  3544. wpa_printf(MSG_DEBUG, "FILS: No GTK KDE");
  3545. goto fail;
  3546. }
  3547. maxkeylen = gd.gtk_len = kde.gtk_len - 2;
  3548. if (wpa_supplicant_check_group_cipher(sm, sm->group_cipher,
  3549. gd.gtk_len, maxkeylen,
  3550. &gd.key_rsc_len, &gd.alg))
  3551. goto fail;
  3552. wpa_hexdump_key(MSG_DEBUG, "FILS: Received GTK", kde.gtk, kde.gtk_len);
  3553. gd.keyidx = kde.gtk[0] & 0x3;
  3554. gd.tx = wpa_supplicant_gtk_tx_bit_workaround(sm,
  3555. !!(kde.gtk[0] & BIT(2)));
  3556. if (kde.gtk_len - 2 > sizeof(gd.gtk)) {
  3557. wpa_printf(MSG_DEBUG, "FILS: Too long GTK in GTK KDE (len=%lu)",
  3558. (unsigned long) kde.gtk_len - 2);
  3559. goto fail;
  3560. }
  3561. os_memcpy(gd.gtk, kde.gtk + 2, kde.gtk_len - 2);
  3562. wpa_printf(MSG_DEBUG, "FILS: Set GTK to driver");
  3563. if (wpa_supplicant_install_gtk(sm, &gd, elems.key_delivery, 0) < 0) {
  3564. wpa_printf(MSG_DEBUG, "FILS: Failed to set GTK");
  3565. goto fail;
  3566. }
  3567. if (ieee80211w_set_keys(sm, &kde) < 0) {
  3568. wpa_printf(MSG_DEBUG, "FILS: Failed to set IGTK");
  3569. goto fail;
  3570. }
  3571. alg = wpa_cipher_to_alg(sm->pairwise_cipher);
  3572. keylen = wpa_cipher_key_len(sm->pairwise_cipher);
  3573. rsclen = wpa_cipher_rsc_len(sm->pairwise_cipher);
  3574. wpa_hexdump_key(MSG_DEBUG, "FILS: Set TK to driver",
  3575. sm->ptk.tk, keylen);
  3576. if (wpa_sm_set_key(sm, alg, sm->bssid, 0, 1, null_rsc, rsclen,
  3577. sm->ptk.tk, keylen) < 0) {
  3578. wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
  3579. "FILS: Failed to set PTK to the driver (alg=%d keylen=%d bssid="
  3580. MACSTR ")",
  3581. alg, keylen, MAC2STR(sm->bssid));
  3582. goto fail;
  3583. }
  3584. /* TODO: TK could be cleared after auth frame exchange now that driver
  3585. * takes care of association frame encryption/decryption. */
  3586. /* TK is not needed anymore in supplicant */
  3587. os_memset(sm->ptk.tk, 0, WPA_TK_MAX_LEN);
  3588. sm->ptk.installed = 1;
  3589. /* FILS HLP Container */
  3590. fils_process_hlp_container(sm, ie_start, end - ie_start);
  3591. /* TODO: FILS IP Address Assignment */
  3592. wpa_printf(MSG_DEBUG, "FILS: Auth+Assoc completed successfully");
  3593. sm->fils_completed = 1;
  3594. return 0;
  3595. fail:
  3596. return -1;
  3597. }
  3598. void wpa_sm_set_reset_fils_completed(struct wpa_sm *sm, int set)
  3599. {
  3600. if (sm)
  3601. sm->fils_completed = !!set;
  3602. }
  3603. #endif /* CONFIG_FILS */
  3604. int wpa_fils_is_completed(struct wpa_sm *sm)
  3605. {
  3606. #ifdef CONFIG_FILS
  3607. return sm && sm->fils_completed;
  3608. #else /* CONFIG_FILS */
  3609. return 0;
  3610. #endif /* CONFIG_FILS */
  3611. }
  3612. #ifdef CONFIG_OWE
  3613. struct wpabuf * owe_build_assoc_req(struct wpa_sm *sm, u16 group)
  3614. {
  3615. struct wpabuf *ie = NULL, *pub = NULL;
  3616. size_t prime_len;
  3617. if (group == 19)
  3618. prime_len = 32;
  3619. else if (group == 20)
  3620. prime_len = 48;
  3621. else if (group == 21)
  3622. prime_len = 66;
  3623. else
  3624. return NULL;
  3625. crypto_ecdh_deinit(sm->owe_ecdh);
  3626. sm->owe_ecdh = crypto_ecdh_init(group);
  3627. if (!sm->owe_ecdh)
  3628. goto fail;
  3629. sm->owe_group = group;
  3630. pub = crypto_ecdh_get_pubkey(sm->owe_ecdh, 0);
  3631. pub = wpabuf_zeropad(pub, prime_len);
  3632. if (!pub)
  3633. goto fail;
  3634. ie = wpabuf_alloc(5 + wpabuf_len(pub));
  3635. if (!ie)
  3636. goto fail;
  3637. wpabuf_put_u8(ie, WLAN_EID_EXTENSION);
  3638. wpabuf_put_u8(ie, 1 + 2 + wpabuf_len(pub));
  3639. wpabuf_put_u8(ie, WLAN_EID_EXT_OWE_DH_PARAM);
  3640. wpabuf_put_le16(ie, group);
  3641. wpabuf_put_buf(ie, pub);
  3642. wpabuf_free(pub);
  3643. wpa_hexdump_buf(MSG_DEBUG, "OWE: Diffie-Hellman Parameter element",
  3644. ie);
  3645. return ie;
  3646. fail:
  3647. wpabuf_free(pub);
  3648. crypto_ecdh_deinit(sm->owe_ecdh);
  3649. sm->owe_ecdh = NULL;
  3650. return NULL;
  3651. }
  3652. int owe_process_assoc_resp(struct wpa_sm *sm, const u8 *bssid,
  3653. const u8 *resp_ies, size_t resp_ies_len)
  3654. {
  3655. struct ieee802_11_elems elems;
  3656. u16 group;
  3657. struct wpabuf *secret, *pub, *hkey;
  3658. int res;
  3659. u8 prk[SHA512_MAC_LEN], pmkid[SHA512_MAC_LEN];
  3660. const char *info = "OWE Key Generation";
  3661. const u8 *addr[2];
  3662. size_t len[2];
  3663. size_t hash_len, prime_len;
  3664. struct wpa_ie_data data;
  3665. if (!resp_ies ||
  3666. ieee802_11_parse_elems(resp_ies, resp_ies_len, &elems, 1) ==
  3667. ParseFailed) {
  3668. wpa_printf(MSG_INFO,
  3669. "OWE: Could not parse Association Response frame elements");
  3670. return -1;
  3671. }
  3672. if (sm->cur_pmksa && elems.rsn_ie &&
  3673. wpa_parse_wpa_ie_rsn(elems.rsn_ie - 2, 2 + elems.rsn_ie_len,
  3674. &data) == 0 &&
  3675. data.num_pmkid == 1 && data.pmkid &&
  3676. os_memcmp(sm->cur_pmksa->pmkid, data.pmkid, PMKID_LEN) == 0) {
  3677. wpa_printf(MSG_DEBUG, "OWE: Use PMKSA caching");
  3678. wpa_sm_set_pmk_from_pmksa(sm);
  3679. return 0;
  3680. }
  3681. if (!elems.owe_dh) {
  3682. wpa_printf(MSG_INFO,
  3683. "OWE: No Diffie-Hellman Parameter element found in Association Response frame");
  3684. return -1;
  3685. }
  3686. group = WPA_GET_LE16(elems.owe_dh);
  3687. if (group != sm->owe_group) {
  3688. wpa_printf(MSG_INFO,
  3689. "OWE: Unexpected Diffie-Hellman group in response: %u",
  3690. group);
  3691. return -1;
  3692. }
  3693. if (!sm->owe_ecdh) {
  3694. wpa_printf(MSG_INFO, "OWE: No ECDH state available");
  3695. return -1;
  3696. }
  3697. if (group == 19)
  3698. prime_len = 32;
  3699. else if (group == 20)
  3700. prime_len = 48;
  3701. else if (group == 21)
  3702. prime_len = 66;
  3703. else
  3704. return -1;
  3705. secret = crypto_ecdh_set_peerkey(sm->owe_ecdh, 0,
  3706. elems.owe_dh + 2,
  3707. elems.owe_dh_len - 2);
  3708. secret = wpabuf_zeropad(secret, prime_len);
  3709. if (!secret) {
  3710. wpa_printf(MSG_DEBUG, "OWE: Invalid peer DH public key");
  3711. return -1;
  3712. }
  3713. wpa_hexdump_buf_key(MSG_DEBUG, "OWE: DH shared secret", secret);
  3714. /* prk = HKDF-extract(C | A | group, z) */
  3715. pub = crypto_ecdh_get_pubkey(sm->owe_ecdh, 0);
  3716. if (!pub) {
  3717. wpabuf_clear_free(secret);
  3718. return -1;
  3719. }
  3720. /* PMKID = Truncate-128(Hash(C | A)) */
  3721. addr[0] = wpabuf_head(pub);
  3722. len[0] = wpabuf_len(pub);
  3723. addr[1] = elems.owe_dh + 2;
  3724. len[1] = elems.owe_dh_len - 2;
  3725. if (group == 19) {
  3726. res = sha256_vector(2, addr, len, pmkid);
  3727. hash_len = SHA256_MAC_LEN;
  3728. } else if (group == 20) {
  3729. res = sha384_vector(2, addr, len, pmkid);
  3730. hash_len = SHA384_MAC_LEN;
  3731. } else if (group == 21) {
  3732. res = sha512_vector(2, addr, len, pmkid);
  3733. hash_len = SHA512_MAC_LEN;
  3734. } else {
  3735. res = -1;
  3736. hash_len = 0;
  3737. }
  3738. pub = wpabuf_zeropad(pub, prime_len);
  3739. if (res < 0 || !pub) {
  3740. wpabuf_free(pub);
  3741. wpabuf_clear_free(secret);
  3742. return -1;
  3743. }
  3744. hkey = wpabuf_alloc(wpabuf_len(pub) + elems.owe_dh_len - 2 + 2);
  3745. if (!hkey) {
  3746. wpabuf_free(pub);
  3747. wpabuf_clear_free(secret);
  3748. return -1;
  3749. }
  3750. wpabuf_put_buf(hkey, pub); /* C */
  3751. wpabuf_free(pub);
  3752. wpabuf_put_data(hkey, elems.owe_dh + 2, elems.owe_dh_len - 2); /* A */
  3753. wpabuf_put_le16(hkey, sm->owe_group); /* group */
  3754. if (group == 19)
  3755. res = hmac_sha256(wpabuf_head(hkey), wpabuf_len(hkey),
  3756. wpabuf_head(secret), wpabuf_len(secret), prk);
  3757. else if (group == 20)
  3758. res = hmac_sha384(wpabuf_head(hkey), wpabuf_len(hkey),
  3759. wpabuf_head(secret), wpabuf_len(secret), prk);
  3760. else if (group == 21)
  3761. res = hmac_sha512(wpabuf_head(hkey), wpabuf_len(hkey),
  3762. wpabuf_head(secret), wpabuf_len(secret), prk);
  3763. wpabuf_clear_free(hkey);
  3764. wpabuf_clear_free(secret);
  3765. if (res < 0)
  3766. return -1;
  3767. wpa_hexdump_key(MSG_DEBUG, "OWE: prk", prk, hash_len);
  3768. /* PMK = HKDF-expand(prk, "OWE Key Generation", n) */
  3769. if (group == 19)
  3770. res = hmac_sha256_kdf(prk, hash_len, NULL, (const u8 *) info,
  3771. os_strlen(info), sm->pmk, hash_len);
  3772. else if (group == 20)
  3773. res = hmac_sha384_kdf(prk, hash_len, NULL, (const u8 *) info,
  3774. os_strlen(info), sm->pmk, hash_len);
  3775. else if (group == 21)
  3776. res = hmac_sha512_kdf(prk, hash_len, NULL, (const u8 *) info,
  3777. os_strlen(info), sm->pmk, hash_len);
  3778. os_memset(prk, 0, SHA512_MAC_LEN);
  3779. if (res < 0)
  3780. return -1;
  3781. sm->pmk_len = hash_len;
  3782. wpa_hexdump_key(MSG_DEBUG, "OWE: PMK", sm->pmk, sm->pmk_len);
  3783. wpa_hexdump(MSG_DEBUG, "OWE: PMKID", pmkid, PMKID_LEN);
  3784. pmksa_cache_add(sm->pmksa, sm->pmk, sm->pmk_len, pmkid, NULL, 0,
  3785. bssid, sm->own_addr, sm->network_ctx, sm->key_mgmt,
  3786. NULL);
  3787. return 0;
  3788. }
  3789. #endif /* CONFIG_OWE */
  3790. void wpa_sm_set_fils_cache_id(struct wpa_sm *sm, const u8 *fils_cache_id)
  3791. {
  3792. #ifdef CONFIG_FILS
  3793. if (sm && fils_cache_id) {
  3794. sm->fils_cache_id_set = 1;
  3795. os_memcpy(sm->fils_cache_id, fils_cache_id, FILS_CACHE_ID_LEN);
  3796. }
  3797. #endif /* CONFIG_FILS */
  3798. }