wpa.c 121 KB

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