wpa_auth.c 114 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270
  1. /*
  2. * IEEE 802.11 RSN / WPA Authenticator
  3. * Copyright (c) 2004-2015, Jouni Malinen <j@w1.fi>
  4. *
  5. * This software may be distributed under the terms of the BSD license.
  6. * See README for more details.
  7. */
  8. #include "utils/includes.h"
  9. #include "utils/common.h"
  10. #include "utils/eloop.h"
  11. #include "utils/state_machine.h"
  12. #include "utils/bitfield.h"
  13. #include "common/ieee802_11_defs.h"
  14. #include "crypto/aes.h"
  15. #include "crypto/aes_wrap.h"
  16. #include "crypto/aes_siv.h"
  17. #include "crypto/crypto.h"
  18. #include "crypto/sha1.h"
  19. #include "crypto/sha256.h"
  20. #include "crypto/random.h"
  21. #include "eapol_auth/eapol_auth_sm.h"
  22. #include "ap_config.h"
  23. #include "ieee802_11.h"
  24. #include "wpa_auth.h"
  25. #include "pmksa_cache_auth.h"
  26. #include "wpa_auth_i.h"
  27. #include "wpa_auth_ie.h"
  28. #define STATE_MACHINE_DATA struct wpa_state_machine
  29. #define STATE_MACHINE_DEBUG_PREFIX "WPA"
  30. #define STATE_MACHINE_ADDR sm->addr
  31. static void wpa_send_eapol_timeout(void *eloop_ctx, void *timeout_ctx);
  32. static int wpa_sm_step(struct wpa_state_machine *sm);
  33. static int wpa_verify_key_mic(int akmp, struct wpa_ptk *PTK, u8 *data,
  34. size_t data_len);
  35. #ifdef CONFIG_FILS
  36. static int wpa_aead_decrypt(struct wpa_state_machine *sm, struct wpa_ptk *ptk,
  37. u8 *buf, size_t buf_len, u16 *_key_data_len);
  38. #endif /* CONFIG_FILS */
  39. static void wpa_sm_call_step(void *eloop_ctx, void *timeout_ctx);
  40. static void wpa_group_sm_step(struct wpa_authenticator *wpa_auth,
  41. struct wpa_group *group);
  42. static void wpa_request_new_ptk(struct wpa_state_machine *sm);
  43. static int wpa_gtk_update(struct wpa_authenticator *wpa_auth,
  44. struct wpa_group *group);
  45. static int wpa_group_config_group_keys(struct wpa_authenticator *wpa_auth,
  46. struct wpa_group *group);
  47. static int wpa_derive_ptk(struct wpa_state_machine *sm, const u8 *snonce,
  48. const u8 *pmk, unsigned int pmk_len,
  49. struct wpa_ptk *ptk);
  50. static void wpa_group_free(struct wpa_authenticator *wpa_auth,
  51. struct wpa_group *group);
  52. static void wpa_group_get(struct wpa_authenticator *wpa_auth,
  53. struct wpa_group *group);
  54. static void wpa_group_put(struct wpa_authenticator *wpa_auth,
  55. struct wpa_group *group);
  56. static u8 * ieee80211w_kde_add(struct wpa_state_machine *sm, u8 *pos);
  57. static const u32 eapol_key_timeout_first = 100; /* ms */
  58. static const u32 eapol_key_timeout_subseq = 1000; /* ms */
  59. static const u32 eapol_key_timeout_first_group = 500; /* ms */
  60. /* TODO: make these configurable */
  61. static const int dot11RSNAConfigPMKLifetime = 43200;
  62. static const int dot11RSNAConfigPMKReauthThreshold = 70;
  63. static const int dot11RSNAConfigSATimeout = 60;
  64. static inline int wpa_auth_mic_failure_report(
  65. struct wpa_authenticator *wpa_auth, const u8 *addr)
  66. {
  67. if (wpa_auth->cb->mic_failure_report)
  68. return wpa_auth->cb->mic_failure_report(wpa_auth->cb_ctx, addr);
  69. return 0;
  70. }
  71. static inline void wpa_auth_psk_failure_report(
  72. struct wpa_authenticator *wpa_auth, const u8 *addr)
  73. {
  74. if (wpa_auth->cb->psk_failure_report)
  75. wpa_auth->cb->psk_failure_report(wpa_auth->cb_ctx, addr);
  76. }
  77. static inline void wpa_auth_set_eapol(struct wpa_authenticator *wpa_auth,
  78. const u8 *addr, wpa_eapol_variable var,
  79. int value)
  80. {
  81. if (wpa_auth->cb->set_eapol)
  82. wpa_auth->cb->set_eapol(wpa_auth->cb_ctx, addr, var, value);
  83. }
  84. static inline int wpa_auth_get_eapol(struct wpa_authenticator *wpa_auth,
  85. const u8 *addr, wpa_eapol_variable var)
  86. {
  87. if (wpa_auth->cb->get_eapol == NULL)
  88. return -1;
  89. return wpa_auth->cb->get_eapol(wpa_auth->cb_ctx, addr, var);
  90. }
  91. static inline const u8 * wpa_auth_get_psk(struct wpa_authenticator *wpa_auth,
  92. const u8 *addr,
  93. const u8 *p2p_dev_addr,
  94. const u8 *prev_psk)
  95. {
  96. if (wpa_auth->cb->get_psk == NULL)
  97. return NULL;
  98. return wpa_auth->cb->get_psk(wpa_auth->cb_ctx, addr, p2p_dev_addr,
  99. prev_psk);
  100. }
  101. static inline int wpa_auth_get_msk(struct wpa_authenticator *wpa_auth,
  102. const u8 *addr, u8 *msk, size_t *len)
  103. {
  104. if (wpa_auth->cb->get_msk == NULL)
  105. return -1;
  106. return wpa_auth->cb->get_msk(wpa_auth->cb_ctx, addr, msk, len);
  107. }
  108. static inline int wpa_auth_set_key(struct wpa_authenticator *wpa_auth,
  109. int vlan_id,
  110. enum wpa_alg alg, const u8 *addr, int idx,
  111. u8 *key, size_t key_len)
  112. {
  113. if (wpa_auth->cb->set_key == NULL)
  114. return -1;
  115. return wpa_auth->cb->set_key(wpa_auth->cb_ctx, vlan_id, alg, addr, idx,
  116. key, key_len);
  117. }
  118. static inline int wpa_auth_get_seqnum(struct wpa_authenticator *wpa_auth,
  119. const u8 *addr, int idx, u8 *seq)
  120. {
  121. if (wpa_auth->cb->get_seqnum == NULL)
  122. return -1;
  123. return wpa_auth->cb->get_seqnum(wpa_auth->cb_ctx, addr, idx, seq);
  124. }
  125. static inline int
  126. wpa_auth_send_eapol(struct wpa_authenticator *wpa_auth, const u8 *addr,
  127. const u8 *data, size_t data_len, int encrypt)
  128. {
  129. if (wpa_auth->cb->send_eapol == NULL)
  130. return -1;
  131. return wpa_auth->cb->send_eapol(wpa_auth->cb_ctx, addr, data, data_len,
  132. encrypt);
  133. }
  134. #ifdef CONFIG_MESH
  135. static inline int wpa_auth_start_ampe(struct wpa_authenticator *wpa_auth,
  136. const u8 *addr)
  137. {
  138. if (wpa_auth->cb->start_ampe == NULL)
  139. return -1;
  140. return wpa_auth->cb->start_ampe(wpa_auth->cb_ctx, addr);
  141. }
  142. #endif /* CONFIG_MESH */
  143. int wpa_auth_for_each_sta(struct wpa_authenticator *wpa_auth,
  144. int (*cb)(struct wpa_state_machine *sm, void *ctx),
  145. void *cb_ctx)
  146. {
  147. if (wpa_auth->cb->for_each_sta == NULL)
  148. return 0;
  149. return wpa_auth->cb->for_each_sta(wpa_auth->cb_ctx, cb, cb_ctx);
  150. }
  151. int wpa_auth_for_each_auth(struct wpa_authenticator *wpa_auth,
  152. int (*cb)(struct wpa_authenticator *a, void *ctx),
  153. void *cb_ctx)
  154. {
  155. if (wpa_auth->cb->for_each_auth == NULL)
  156. return 0;
  157. return wpa_auth->cb->for_each_auth(wpa_auth->cb_ctx, cb, cb_ctx);
  158. }
  159. void wpa_auth_logger(struct wpa_authenticator *wpa_auth, const u8 *addr,
  160. logger_level level, const char *txt)
  161. {
  162. if (wpa_auth->cb->logger == NULL)
  163. return;
  164. wpa_auth->cb->logger(wpa_auth->cb_ctx, addr, level, txt);
  165. }
  166. void wpa_auth_vlogger(struct wpa_authenticator *wpa_auth, const u8 *addr,
  167. logger_level level, const char *fmt, ...)
  168. {
  169. char *format;
  170. int maxlen;
  171. va_list ap;
  172. if (wpa_auth->cb->logger == NULL)
  173. return;
  174. maxlen = os_strlen(fmt) + 100;
  175. format = os_malloc(maxlen);
  176. if (!format)
  177. return;
  178. va_start(ap, fmt);
  179. vsnprintf(format, maxlen, fmt, ap);
  180. va_end(ap);
  181. wpa_auth_logger(wpa_auth, addr, level, format);
  182. os_free(format);
  183. }
  184. static void wpa_sta_disconnect(struct wpa_authenticator *wpa_auth,
  185. const u8 *addr)
  186. {
  187. if (wpa_auth->cb->disconnect == NULL)
  188. return;
  189. wpa_printf(MSG_DEBUG, "wpa_sta_disconnect STA " MACSTR, MAC2STR(addr));
  190. wpa_auth->cb->disconnect(wpa_auth->cb_ctx, addr,
  191. WLAN_REASON_PREV_AUTH_NOT_VALID);
  192. }
  193. static int wpa_use_aes_cmac(struct wpa_state_machine *sm)
  194. {
  195. int ret = 0;
  196. #ifdef CONFIG_IEEE80211R_AP
  197. if (wpa_key_mgmt_ft(sm->wpa_key_mgmt))
  198. ret = 1;
  199. #endif /* CONFIG_IEEE80211R_AP */
  200. #ifdef CONFIG_IEEE80211W
  201. if (wpa_key_mgmt_sha256(sm->wpa_key_mgmt))
  202. ret = 1;
  203. #endif /* CONFIG_IEEE80211W */
  204. if (sm->wpa_key_mgmt == WPA_KEY_MGMT_OSEN)
  205. ret = 1;
  206. return ret;
  207. }
  208. static void wpa_rekey_gmk(void *eloop_ctx, void *timeout_ctx)
  209. {
  210. struct wpa_authenticator *wpa_auth = eloop_ctx;
  211. if (random_get_bytes(wpa_auth->group->GMK, WPA_GMK_LEN)) {
  212. wpa_printf(MSG_ERROR, "Failed to get random data for WPA "
  213. "initialization.");
  214. } else {
  215. wpa_auth_logger(wpa_auth, NULL, LOGGER_DEBUG, "GMK rekeyd");
  216. wpa_hexdump_key(MSG_DEBUG, "GMK",
  217. wpa_auth->group->GMK, WPA_GMK_LEN);
  218. }
  219. if (wpa_auth->conf.wpa_gmk_rekey) {
  220. eloop_register_timeout(wpa_auth->conf.wpa_gmk_rekey, 0,
  221. wpa_rekey_gmk, wpa_auth, NULL);
  222. }
  223. }
  224. static void wpa_rekey_gtk(void *eloop_ctx, void *timeout_ctx)
  225. {
  226. struct wpa_authenticator *wpa_auth = eloop_ctx;
  227. struct wpa_group *group, *next;
  228. wpa_auth_logger(wpa_auth, NULL, LOGGER_DEBUG, "rekeying GTK");
  229. group = wpa_auth->group;
  230. while (group) {
  231. wpa_group_get(wpa_auth, group);
  232. group->GTKReKey = TRUE;
  233. do {
  234. group->changed = FALSE;
  235. wpa_group_sm_step(wpa_auth, group);
  236. } while (group->changed);
  237. next = group->next;
  238. wpa_group_put(wpa_auth, group);
  239. group = next;
  240. }
  241. if (wpa_auth->conf.wpa_group_rekey) {
  242. eloop_register_timeout(wpa_auth->conf.wpa_group_rekey,
  243. 0, wpa_rekey_gtk, wpa_auth, NULL);
  244. }
  245. }
  246. static void wpa_rekey_ptk(void *eloop_ctx, void *timeout_ctx)
  247. {
  248. struct wpa_authenticator *wpa_auth = eloop_ctx;
  249. struct wpa_state_machine *sm = timeout_ctx;
  250. wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG, "rekeying PTK");
  251. wpa_request_new_ptk(sm);
  252. wpa_sm_step(sm);
  253. }
  254. static int wpa_auth_pmksa_clear_cb(struct wpa_state_machine *sm, void *ctx)
  255. {
  256. if (sm->pmksa == ctx)
  257. sm->pmksa = NULL;
  258. return 0;
  259. }
  260. static void wpa_auth_pmksa_free_cb(struct rsn_pmksa_cache_entry *entry,
  261. void *ctx)
  262. {
  263. struct wpa_authenticator *wpa_auth = ctx;
  264. wpa_auth_for_each_sta(wpa_auth, wpa_auth_pmksa_clear_cb, entry);
  265. }
  266. static int wpa_group_init_gmk_and_counter(struct wpa_authenticator *wpa_auth,
  267. struct wpa_group *group)
  268. {
  269. u8 buf[ETH_ALEN + 8 + sizeof(unsigned long)];
  270. u8 rkey[32];
  271. unsigned long ptr;
  272. if (random_get_bytes(group->GMK, WPA_GMK_LEN) < 0)
  273. return -1;
  274. wpa_hexdump_key(MSG_DEBUG, "GMK", group->GMK, WPA_GMK_LEN);
  275. /*
  276. * Counter = PRF-256(Random number, "Init Counter",
  277. * Local MAC Address || Time)
  278. */
  279. os_memcpy(buf, wpa_auth->addr, ETH_ALEN);
  280. wpa_get_ntp_timestamp(buf + ETH_ALEN);
  281. ptr = (unsigned long) group;
  282. os_memcpy(buf + ETH_ALEN + 8, &ptr, sizeof(ptr));
  283. if (random_get_bytes(rkey, sizeof(rkey)) < 0)
  284. return -1;
  285. if (sha1_prf(rkey, sizeof(rkey), "Init Counter", buf, sizeof(buf),
  286. group->Counter, WPA_NONCE_LEN) < 0)
  287. return -1;
  288. wpa_hexdump_key(MSG_DEBUG, "Key Counter",
  289. group->Counter, WPA_NONCE_LEN);
  290. return 0;
  291. }
  292. static struct wpa_group * wpa_group_init(struct wpa_authenticator *wpa_auth,
  293. int vlan_id, int delay_init)
  294. {
  295. struct wpa_group *group;
  296. group = os_zalloc(sizeof(struct wpa_group));
  297. if (group == NULL)
  298. return NULL;
  299. group->GTKAuthenticator = TRUE;
  300. group->vlan_id = vlan_id;
  301. group->GTK_len = wpa_cipher_key_len(wpa_auth->conf.wpa_group);
  302. if (random_pool_ready() != 1) {
  303. wpa_printf(MSG_INFO, "WPA: Not enough entropy in random pool "
  304. "for secure operations - update keys later when "
  305. "the first station connects");
  306. }
  307. /*
  308. * Set initial GMK/Counter value here. The actual values that will be
  309. * used in negotiations will be set once the first station tries to
  310. * connect. This allows more time for collecting additional randomness
  311. * on embedded devices.
  312. */
  313. if (wpa_group_init_gmk_and_counter(wpa_auth, group) < 0) {
  314. wpa_printf(MSG_ERROR, "Failed to get random data for WPA "
  315. "initialization.");
  316. os_free(group);
  317. return NULL;
  318. }
  319. group->GInit = TRUE;
  320. if (delay_init) {
  321. wpa_printf(MSG_DEBUG, "WPA: Delay group state machine start "
  322. "until Beacon frames have been configured");
  323. /* Initialization is completed in wpa_init_keys(). */
  324. } else {
  325. wpa_group_sm_step(wpa_auth, group);
  326. group->GInit = FALSE;
  327. wpa_group_sm_step(wpa_auth, group);
  328. }
  329. return group;
  330. }
  331. /**
  332. * wpa_init - Initialize WPA authenticator
  333. * @addr: Authenticator address
  334. * @conf: Configuration for WPA authenticator
  335. * @cb: Callback functions for WPA authenticator
  336. * Returns: Pointer to WPA authenticator data or %NULL on failure
  337. */
  338. struct wpa_authenticator * wpa_init(const u8 *addr,
  339. struct wpa_auth_config *conf,
  340. const struct wpa_auth_callbacks *cb,
  341. void *cb_ctx)
  342. {
  343. struct wpa_authenticator *wpa_auth;
  344. wpa_auth = os_zalloc(sizeof(struct wpa_authenticator));
  345. if (wpa_auth == NULL)
  346. return NULL;
  347. os_memcpy(wpa_auth->addr, addr, ETH_ALEN);
  348. os_memcpy(&wpa_auth->conf, conf, sizeof(*conf));
  349. wpa_auth->cb = cb;
  350. wpa_auth->cb_ctx = cb_ctx;
  351. if (wpa_auth_gen_wpa_ie(wpa_auth)) {
  352. wpa_printf(MSG_ERROR, "Could not generate WPA IE.");
  353. os_free(wpa_auth);
  354. return NULL;
  355. }
  356. wpa_auth->group = wpa_group_init(wpa_auth, 0, 1);
  357. if (wpa_auth->group == NULL) {
  358. os_free(wpa_auth->wpa_ie);
  359. os_free(wpa_auth);
  360. return NULL;
  361. }
  362. wpa_auth->pmksa = pmksa_cache_auth_init(wpa_auth_pmksa_free_cb,
  363. wpa_auth);
  364. if (wpa_auth->pmksa == NULL) {
  365. wpa_printf(MSG_ERROR, "PMKSA cache initialization failed.");
  366. os_free(wpa_auth->group);
  367. os_free(wpa_auth->wpa_ie);
  368. os_free(wpa_auth);
  369. return NULL;
  370. }
  371. #ifdef CONFIG_IEEE80211R_AP
  372. wpa_auth->ft_pmk_cache = wpa_ft_pmk_cache_init();
  373. if (wpa_auth->ft_pmk_cache == NULL) {
  374. wpa_printf(MSG_ERROR, "FT PMK cache initialization failed.");
  375. os_free(wpa_auth->group);
  376. os_free(wpa_auth->wpa_ie);
  377. pmksa_cache_auth_deinit(wpa_auth->pmksa);
  378. os_free(wpa_auth);
  379. return NULL;
  380. }
  381. #endif /* CONFIG_IEEE80211R_AP */
  382. if (wpa_auth->conf.wpa_gmk_rekey) {
  383. eloop_register_timeout(wpa_auth->conf.wpa_gmk_rekey, 0,
  384. wpa_rekey_gmk, wpa_auth, NULL);
  385. }
  386. if (wpa_auth->conf.wpa_group_rekey) {
  387. eloop_register_timeout(wpa_auth->conf.wpa_group_rekey, 0,
  388. wpa_rekey_gtk, wpa_auth, NULL);
  389. }
  390. #ifdef CONFIG_P2P
  391. if (WPA_GET_BE32(conf->ip_addr_start)) {
  392. int count = WPA_GET_BE32(conf->ip_addr_end) -
  393. WPA_GET_BE32(conf->ip_addr_start) + 1;
  394. if (count > 1000)
  395. count = 1000;
  396. if (count > 0)
  397. wpa_auth->ip_pool = bitfield_alloc(count);
  398. }
  399. #endif /* CONFIG_P2P */
  400. return wpa_auth;
  401. }
  402. int wpa_init_keys(struct wpa_authenticator *wpa_auth)
  403. {
  404. struct wpa_group *group = wpa_auth->group;
  405. wpa_printf(MSG_DEBUG, "WPA: Start group state machine to set initial "
  406. "keys");
  407. wpa_group_sm_step(wpa_auth, group);
  408. group->GInit = FALSE;
  409. wpa_group_sm_step(wpa_auth, group);
  410. if (group->wpa_group_state == WPA_GROUP_FATAL_FAILURE)
  411. return -1;
  412. return 0;
  413. }
  414. /**
  415. * wpa_deinit - Deinitialize WPA authenticator
  416. * @wpa_auth: Pointer to WPA authenticator data from wpa_init()
  417. */
  418. void wpa_deinit(struct wpa_authenticator *wpa_auth)
  419. {
  420. struct wpa_group *group, *prev;
  421. eloop_cancel_timeout(wpa_rekey_gmk, wpa_auth, NULL);
  422. eloop_cancel_timeout(wpa_rekey_gtk, wpa_auth, NULL);
  423. pmksa_cache_auth_deinit(wpa_auth->pmksa);
  424. #ifdef CONFIG_IEEE80211R_AP
  425. wpa_ft_pmk_cache_deinit(wpa_auth->ft_pmk_cache);
  426. wpa_auth->ft_pmk_cache = NULL;
  427. #endif /* CONFIG_IEEE80211R_AP */
  428. #ifdef CONFIG_P2P
  429. bitfield_free(wpa_auth->ip_pool);
  430. #endif /* CONFIG_P2P */
  431. os_free(wpa_auth->wpa_ie);
  432. group = wpa_auth->group;
  433. while (group) {
  434. prev = group;
  435. group = group->next;
  436. os_free(prev);
  437. }
  438. os_free(wpa_auth);
  439. }
  440. /**
  441. * wpa_reconfig - Update WPA authenticator configuration
  442. * @wpa_auth: Pointer to WPA authenticator data from wpa_init()
  443. * @conf: Configuration for WPA authenticator
  444. */
  445. int wpa_reconfig(struct wpa_authenticator *wpa_auth,
  446. struct wpa_auth_config *conf)
  447. {
  448. struct wpa_group *group;
  449. if (wpa_auth == NULL)
  450. return 0;
  451. os_memcpy(&wpa_auth->conf, conf, sizeof(*conf));
  452. if (wpa_auth_gen_wpa_ie(wpa_auth)) {
  453. wpa_printf(MSG_ERROR, "Could not generate WPA IE.");
  454. return -1;
  455. }
  456. /*
  457. * Reinitialize GTK to make sure it is suitable for the new
  458. * configuration.
  459. */
  460. group = wpa_auth->group;
  461. group->GTK_len = wpa_cipher_key_len(wpa_auth->conf.wpa_group);
  462. group->GInit = TRUE;
  463. wpa_group_sm_step(wpa_auth, group);
  464. group->GInit = FALSE;
  465. wpa_group_sm_step(wpa_auth, group);
  466. return 0;
  467. }
  468. struct wpa_state_machine *
  469. wpa_auth_sta_init(struct wpa_authenticator *wpa_auth, const u8 *addr,
  470. const u8 *p2p_dev_addr)
  471. {
  472. struct wpa_state_machine *sm;
  473. if (wpa_auth->group->wpa_group_state == WPA_GROUP_FATAL_FAILURE)
  474. return NULL;
  475. sm = os_zalloc(sizeof(struct wpa_state_machine));
  476. if (sm == NULL)
  477. return NULL;
  478. os_memcpy(sm->addr, addr, ETH_ALEN);
  479. if (p2p_dev_addr)
  480. os_memcpy(sm->p2p_dev_addr, p2p_dev_addr, ETH_ALEN);
  481. sm->wpa_auth = wpa_auth;
  482. sm->group = wpa_auth->group;
  483. wpa_group_get(sm->wpa_auth, sm->group);
  484. return sm;
  485. }
  486. int wpa_auth_sta_associated(struct wpa_authenticator *wpa_auth,
  487. struct wpa_state_machine *sm)
  488. {
  489. if (wpa_auth == NULL || !wpa_auth->conf.wpa || sm == NULL)
  490. return -1;
  491. #ifdef CONFIG_IEEE80211R_AP
  492. if (sm->ft_completed) {
  493. wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG,
  494. "FT authentication already completed - do not "
  495. "start 4-way handshake");
  496. /* Go to PTKINITDONE state to allow GTK rekeying */
  497. sm->wpa_ptk_state = WPA_PTK_PTKINITDONE;
  498. sm->Pair = TRUE;
  499. return 0;
  500. }
  501. #endif /* CONFIG_IEEE80211R_AP */
  502. #ifdef CONFIG_FILS
  503. if (sm->fils_completed) {
  504. wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG,
  505. "FILS authentication already completed - do not start 4-way handshake");
  506. /* Go to PTKINITDONE state to allow GTK rekeying */
  507. sm->wpa_ptk_state = WPA_PTK_PTKINITDONE;
  508. sm->Pair = TRUE;
  509. return 0;
  510. }
  511. #endif /* CONFIG_FILS */
  512. if (sm->started) {
  513. os_memset(&sm->key_replay, 0, sizeof(sm->key_replay));
  514. sm->ReAuthenticationRequest = TRUE;
  515. return wpa_sm_step(sm);
  516. }
  517. wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG,
  518. "start authentication");
  519. sm->started = 1;
  520. sm->Init = TRUE;
  521. if (wpa_sm_step(sm) == 1)
  522. return 1; /* should not really happen */
  523. sm->Init = FALSE;
  524. sm->AuthenticationRequest = TRUE;
  525. return wpa_sm_step(sm);
  526. }
  527. void wpa_auth_sta_no_wpa(struct wpa_state_machine *sm)
  528. {
  529. /* WPA/RSN was not used - clear WPA state. This is needed if the STA
  530. * reassociates back to the same AP while the previous entry for the
  531. * STA has not yet been removed. */
  532. if (sm == NULL)
  533. return;
  534. sm->wpa_key_mgmt = 0;
  535. }
  536. static void wpa_free_sta_sm(struct wpa_state_machine *sm)
  537. {
  538. #ifdef CONFIG_P2P
  539. if (WPA_GET_BE32(sm->ip_addr)) {
  540. u32 start;
  541. wpa_printf(MSG_DEBUG, "P2P: Free assigned IP "
  542. "address %u.%u.%u.%u from " MACSTR,
  543. sm->ip_addr[0], sm->ip_addr[1],
  544. sm->ip_addr[2], sm->ip_addr[3],
  545. MAC2STR(sm->addr));
  546. start = WPA_GET_BE32(sm->wpa_auth->conf.ip_addr_start);
  547. bitfield_clear(sm->wpa_auth->ip_pool,
  548. WPA_GET_BE32(sm->ip_addr) - start);
  549. }
  550. #endif /* CONFIG_P2P */
  551. if (sm->GUpdateStationKeys) {
  552. sm->group->GKeyDoneStations--;
  553. sm->GUpdateStationKeys = FALSE;
  554. }
  555. #ifdef CONFIG_IEEE80211R_AP
  556. os_free(sm->assoc_resp_ftie);
  557. wpabuf_free(sm->ft_pending_req_ies);
  558. #endif /* CONFIG_IEEE80211R_AP */
  559. os_free(sm->last_rx_eapol_key);
  560. os_free(sm->wpa_ie);
  561. wpa_group_put(sm->wpa_auth, sm->group);
  562. os_free(sm);
  563. }
  564. void wpa_auth_sta_deinit(struct wpa_state_machine *sm)
  565. {
  566. if (sm == NULL)
  567. return;
  568. if (sm->wpa_auth->conf.wpa_strict_rekey && sm->has_GTK) {
  569. wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
  570. "strict rekeying - force GTK rekey since STA "
  571. "is leaving");
  572. eloop_cancel_timeout(wpa_rekey_gtk, sm->wpa_auth, NULL);
  573. eloop_register_timeout(0, 500000, wpa_rekey_gtk, sm->wpa_auth,
  574. NULL);
  575. }
  576. eloop_cancel_timeout(wpa_send_eapol_timeout, sm->wpa_auth, sm);
  577. sm->pending_1_of_4_timeout = 0;
  578. eloop_cancel_timeout(wpa_sm_call_step, sm, NULL);
  579. eloop_cancel_timeout(wpa_rekey_ptk, sm->wpa_auth, sm);
  580. if (sm->in_step_loop) {
  581. /* Must not free state machine while wpa_sm_step() is running.
  582. * Freeing will be completed in the end of wpa_sm_step(). */
  583. wpa_printf(MSG_DEBUG, "WPA: Registering pending STA state "
  584. "machine deinit for " MACSTR, MAC2STR(sm->addr));
  585. sm->pending_deinit = 1;
  586. } else
  587. wpa_free_sta_sm(sm);
  588. }
  589. static void wpa_request_new_ptk(struct wpa_state_machine *sm)
  590. {
  591. if (sm == NULL)
  592. return;
  593. sm->PTKRequest = TRUE;
  594. sm->PTK_valid = 0;
  595. }
  596. static int wpa_replay_counter_valid(struct wpa_key_replay_counter *ctr,
  597. const u8 *replay_counter)
  598. {
  599. int i;
  600. for (i = 0; i < RSNA_MAX_EAPOL_RETRIES; i++) {
  601. if (!ctr[i].valid)
  602. break;
  603. if (os_memcmp(replay_counter, ctr[i].counter,
  604. WPA_REPLAY_COUNTER_LEN) == 0)
  605. return 1;
  606. }
  607. return 0;
  608. }
  609. static void wpa_replay_counter_mark_invalid(struct wpa_key_replay_counter *ctr,
  610. const u8 *replay_counter)
  611. {
  612. int i;
  613. for (i = 0; i < RSNA_MAX_EAPOL_RETRIES; i++) {
  614. if (ctr[i].valid &&
  615. (replay_counter == NULL ||
  616. os_memcmp(replay_counter, ctr[i].counter,
  617. WPA_REPLAY_COUNTER_LEN) == 0))
  618. ctr[i].valid = FALSE;
  619. }
  620. }
  621. #ifdef CONFIG_IEEE80211R_AP
  622. static int ft_check_msg_2_of_4(struct wpa_authenticator *wpa_auth,
  623. struct wpa_state_machine *sm,
  624. struct wpa_eapol_ie_parse *kde)
  625. {
  626. struct wpa_ie_data ie;
  627. struct rsn_mdie *mdie;
  628. if (wpa_parse_wpa_ie_rsn(kde->rsn_ie, kde->rsn_ie_len, &ie) < 0 ||
  629. ie.num_pmkid != 1 || ie.pmkid == NULL) {
  630. wpa_printf(MSG_DEBUG, "FT: No PMKR1Name in "
  631. "FT 4-way handshake message 2/4");
  632. return -1;
  633. }
  634. os_memcpy(sm->sup_pmk_r1_name, ie.pmkid, PMKID_LEN);
  635. wpa_hexdump(MSG_DEBUG, "FT: PMKR1Name from Supplicant",
  636. sm->sup_pmk_r1_name, PMKID_LEN);
  637. if (!kde->mdie || !kde->ftie) {
  638. wpa_printf(MSG_DEBUG, "FT: No %s in FT 4-way handshake "
  639. "message 2/4", kde->mdie ? "FTIE" : "MDIE");
  640. return -1;
  641. }
  642. mdie = (struct rsn_mdie *) (kde->mdie + 2);
  643. if (kde->mdie[1] < sizeof(struct rsn_mdie) ||
  644. os_memcmp(wpa_auth->conf.mobility_domain, mdie->mobility_domain,
  645. MOBILITY_DOMAIN_ID_LEN) != 0) {
  646. wpa_printf(MSG_DEBUG, "FT: MDIE mismatch");
  647. return -1;
  648. }
  649. if (sm->assoc_resp_ftie &&
  650. (kde->ftie[1] != sm->assoc_resp_ftie[1] ||
  651. os_memcmp(kde->ftie, sm->assoc_resp_ftie,
  652. 2 + sm->assoc_resp_ftie[1]) != 0)) {
  653. wpa_printf(MSG_DEBUG, "FT: FTIE mismatch");
  654. wpa_hexdump(MSG_DEBUG, "FT: FTIE in EAPOL-Key msg 2/4",
  655. kde->ftie, kde->ftie_len);
  656. wpa_hexdump(MSG_DEBUG, "FT: FTIE in (Re)AssocResp",
  657. sm->assoc_resp_ftie, 2 + sm->assoc_resp_ftie[1]);
  658. return -1;
  659. }
  660. return 0;
  661. }
  662. #endif /* CONFIG_IEEE80211R_AP */
  663. static int wpa_receive_error_report(struct wpa_authenticator *wpa_auth,
  664. struct wpa_state_machine *sm, int group)
  665. {
  666. /* Supplicant reported a Michael MIC error */
  667. wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_INFO,
  668. "received EAPOL-Key Error Request "
  669. "(STA detected Michael MIC failure (group=%d))",
  670. group);
  671. if (group && wpa_auth->conf.wpa_group != WPA_CIPHER_TKIP) {
  672. wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
  673. "ignore Michael MIC failure report since "
  674. "group cipher is not TKIP");
  675. } else if (!group && sm->pairwise != WPA_CIPHER_TKIP) {
  676. wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
  677. "ignore Michael MIC failure report since "
  678. "pairwise cipher is not TKIP");
  679. } else {
  680. if (wpa_auth_mic_failure_report(wpa_auth, sm->addr) > 0)
  681. return 1; /* STA entry was removed */
  682. sm->dot11RSNAStatsTKIPRemoteMICFailures++;
  683. wpa_auth->dot11RSNAStatsTKIPRemoteMICFailures++;
  684. }
  685. /*
  686. * Error report is not a request for a new key handshake, but since
  687. * Authenticator may do it, let's change the keys now anyway.
  688. */
  689. wpa_request_new_ptk(sm);
  690. return 0;
  691. }
  692. static int wpa_try_alt_snonce(struct wpa_state_machine *sm, u8 *data,
  693. size_t data_len)
  694. {
  695. struct wpa_ptk PTK;
  696. int ok = 0;
  697. const u8 *pmk = NULL;
  698. unsigned int pmk_len;
  699. os_memset(&PTK, 0, sizeof(PTK));
  700. for (;;) {
  701. if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt)) {
  702. pmk = wpa_auth_get_psk(sm->wpa_auth, sm->addr,
  703. sm->p2p_dev_addr, pmk);
  704. if (pmk == NULL)
  705. break;
  706. pmk_len = PMK_LEN;
  707. } else {
  708. pmk = sm->PMK;
  709. pmk_len = sm->pmk_len;
  710. }
  711. if (wpa_derive_ptk(sm, sm->alt_SNonce, pmk, pmk_len, &PTK) < 0)
  712. break;
  713. if (wpa_verify_key_mic(sm->wpa_key_mgmt, &PTK, data, data_len)
  714. == 0) {
  715. ok = 1;
  716. break;
  717. }
  718. if (!wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt))
  719. break;
  720. }
  721. if (!ok) {
  722. wpa_printf(MSG_DEBUG,
  723. "WPA: Earlier SNonce did not result in matching MIC");
  724. return -1;
  725. }
  726. wpa_printf(MSG_DEBUG,
  727. "WPA: Earlier SNonce resulted in matching MIC");
  728. sm->alt_snonce_valid = 0;
  729. os_memcpy(sm->SNonce, sm->alt_SNonce, WPA_NONCE_LEN);
  730. os_memcpy(&sm->PTK, &PTK, sizeof(PTK));
  731. sm->PTK_valid = TRUE;
  732. return 0;
  733. }
  734. void wpa_receive(struct wpa_authenticator *wpa_auth,
  735. struct wpa_state_machine *sm,
  736. u8 *data, size_t data_len)
  737. {
  738. struct ieee802_1x_hdr *hdr;
  739. struct wpa_eapol_key *key;
  740. u16 key_info, key_data_length;
  741. enum { PAIRWISE_2, PAIRWISE_4, GROUP_2, REQUEST,
  742. SMK_M1, SMK_M3, SMK_ERROR } msg;
  743. char *msgtxt;
  744. struct wpa_eapol_ie_parse kde;
  745. const u8 *key_data;
  746. size_t keyhdrlen, mic_len;
  747. u8 *mic;
  748. if (wpa_auth == NULL || !wpa_auth->conf.wpa || sm == NULL)
  749. return;
  750. wpa_hexdump(MSG_MSGDUMP, "WPA: RX EAPOL data", data, data_len);
  751. mic_len = wpa_mic_len(sm->wpa_key_mgmt);
  752. keyhdrlen = sizeof(*key) + mic_len + 2;
  753. if (data_len < sizeof(*hdr) + keyhdrlen) {
  754. wpa_printf(MSG_DEBUG, "WPA: Ignore too short EAPOL-Key frame");
  755. return;
  756. }
  757. hdr = (struct ieee802_1x_hdr *) data;
  758. key = (struct wpa_eapol_key *) (hdr + 1);
  759. mic = (u8 *) (key + 1);
  760. key_info = WPA_GET_BE16(key->key_info);
  761. key_data = mic + mic_len + 2;
  762. key_data_length = WPA_GET_BE16(mic + mic_len);
  763. wpa_printf(MSG_DEBUG, "WPA: Received EAPOL-Key from " MACSTR
  764. " key_info=0x%x type=%u mic_len=%u key_data_length=%u",
  765. MAC2STR(sm->addr), key_info, key->type,
  766. (unsigned int) mic_len, key_data_length);
  767. wpa_hexdump(MSG_MSGDUMP,
  768. "WPA: EAPOL-Key header (ending before Key MIC)",
  769. key, sizeof(*key));
  770. wpa_hexdump(MSG_MSGDUMP, "WPA: EAPOL-Key Key MIC",
  771. mic, mic_len);
  772. if (key_data_length > data_len - sizeof(*hdr) - keyhdrlen) {
  773. wpa_printf(MSG_INFO, "WPA: Invalid EAPOL-Key frame - "
  774. "key_data overflow (%d > %lu)",
  775. key_data_length,
  776. (unsigned long) (data_len - sizeof(*hdr) -
  777. keyhdrlen));
  778. return;
  779. }
  780. if (sm->wpa == WPA_VERSION_WPA2) {
  781. if (key->type == EAPOL_KEY_TYPE_WPA) {
  782. /*
  783. * Some deployed station implementations seem to send
  784. * msg 4/4 with incorrect type value in WPA2 mode.
  785. */
  786. wpa_printf(MSG_DEBUG, "Workaround: Allow EAPOL-Key "
  787. "with unexpected WPA type in RSN mode");
  788. } else if (key->type != EAPOL_KEY_TYPE_RSN) {
  789. wpa_printf(MSG_DEBUG, "Ignore EAPOL-Key with "
  790. "unexpected type %d in RSN mode",
  791. key->type);
  792. return;
  793. }
  794. } else {
  795. if (key->type != EAPOL_KEY_TYPE_WPA) {
  796. wpa_printf(MSG_DEBUG, "Ignore EAPOL-Key with "
  797. "unexpected type %d in WPA mode",
  798. key->type);
  799. return;
  800. }
  801. }
  802. wpa_hexdump(MSG_DEBUG, "WPA: Received Key Nonce", key->key_nonce,
  803. WPA_NONCE_LEN);
  804. wpa_hexdump(MSG_DEBUG, "WPA: Received Replay Counter",
  805. key->replay_counter, WPA_REPLAY_COUNTER_LEN);
  806. /* FIX: verify that the EAPOL-Key frame was encrypted if pairwise keys
  807. * are set */
  808. if ((key_info & (WPA_KEY_INFO_SMK_MESSAGE | WPA_KEY_INFO_REQUEST)) ==
  809. (WPA_KEY_INFO_SMK_MESSAGE | WPA_KEY_INFO_REQUEST)) {
  810. if (key_info & WPA_KEY_INFO_ERROR) {
  811. msg = SMK_ERROR;
  812. msgtxt = "SMK Error";
  813. } else {
  814. msg = SMK_M1;
  815. msgtxt = "SMK M1";
  816. }
  817. } else if (key_info & WPA_KEY_INFO_SMK_MESSAGE) {
  818. msg = SMK_M3;
  819. msgtxt = "SMK M3";
  820. } else if (key_info & WPA_KEY_INFO_REQUEST) {
  821. msg = REQUEST;
  822. msgtxt = "Request";
  823. } else if (!(key_info & WPA_KEY_INFO_KEY_TYPE)) {
  824. msg = GROUP_2;
  825. msgtxt = "2/2 Group";
  826. } else if (key_data_length == 0 ||
  827. (mic_len == 0 && (key_info & WPA_KEY_INFO_ENCR_KEY_DATA) &&
  828. key_data_length == AES_BLOCK_SIZE)) {
  829. msg = PAIRWISE_4;
  830. msgtxt = "4/4 Pairwise";
  831. } else {
  832. msg = PAIRWISE_2;
  833. msgtxt = "2/4 Pairwise";
  834. }
  835. /* TODO: key_info type validation for PeerKey */
  836. if (msg == REQUEST || msg == PAIRWISE_2 || msg == PAIRWISE_4 ||
  837. msg == GROUP_2) {
  838. u16 ver = key_info & WPA_KEY_INFO_TYPE_MASK;
  839. if (sm->pairwise == WPA_CIPHER_CCMP ||
  840. sm->pairwise == WPA_CIPHER_GCMP) {
  841. if (wpa_use_aes_cmac(sm) &&
  842. sm->wpa_key_mgmt != WPA_KEY_MGMT_OSEN &&
  843. !wpa_key_mgmt_suite_b(sm->wpa_key_mgmt) &&
  844. !wpa_key_mgmt_fils(sm->wpa_key_mgmt) &&
  845. ver != WPA_KEY_INFO_TYPE_AES_128_CMAC) {
  846. wpa_auth_logger(wpa_auth, sm->addr,
  847. LOGGER_WARNING,
  848. "advertised support for "
  849. "AES-128-CMAC, but did not "
  850. "use it");
  851. return;
  852. }
  853. if (!wpa_use_aes_cmac(sm) &&
  854. !wpa_key_mgmt_fils(sm->wpa_key_mgmt) &&
  855. ver != WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
  856. wpa_auth_logger(wpa_auth, sm->addr,
  857. LOGGER_WARNING,
  858. "did not use HMAC-SHA1-AES "
  859. "with CCMP/GCMP");
  860. return;
  861. }
  862. }
  863. if ((wpa_key_mgmt_suite_b(sm->wpa_key_mgmt) ||
  864. wpa_key_mgmt_fils(sm->wpa_key_mgmt)) &&
  865. ver != WPA_KEY_INFO_TYPE_AKM_DEFINED) {
  866. wpa_auth_logger(wpa_auth, sm->addr, LOGGER_WARNING,
  867. "did not use EAPOL-Key descriptor version 0 as required for AKM-defined cases");
  868. return;
  869. }
  870. }
  871. if (key_info & WPA_KEY_INFO_REQUEST) {
  872. if (sm->req_replay_counter_used &&
  873. os_memcmp(key->replay_counter, sm->req_replay_counter,
  874. WPA_REPLAY_COUNTER_LEN) <= 0) {
  875. wpa_auth_logger(wpa_auth, sm->addr, LOGGER_WARNING,
  876. "received EAPOL-Key request with "
  877. "replayed counter");
  878. return;
  879. }
  880. }
  881. if (!(key_info & WPA_KEY_INFO_REQUEST) &&
  882. !wpa_replay_counter_valid(sm->key_replay, key->replay_counter)) {
  883. int i;
  884. if (msg == PAIRWISE_2 &&
  885. wpa_replay_counter_valid(sm->prev_key_replay,
  886. key->replay_counter) &&
  887. sm->wpa_ptk_state == WPA_PTK_PTKINITNEGOTIATING &&
  888. os_memcmp(sm->SNonce, key->key_nonce, WPA_NONCE_LEN) != 0)
  889. {
  890. /*
  891. * Some supplicant implementations (e.g., Windows XP
  892. * WZC) update SNonce for each EAPOL-Key 2/4. This
  893. * breaks the workaround on accepting any of the
  894. * pending requests, so allow the SNonce to be updated
  895. * even if we have already sent out EAPOL-Key 3/4.
  896. */
  897. wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_DEBUG,
  898. "Process SNonce update from STA "
  899. "based on retransmitted EAPOL-Key "
  900. "1/4");
  901. sm->update_snonce = 1;
  902. os_memcpy(sm->alt_SNonce, sm->SNonce, WPA_NONCE_LEN);
  903. sm->alt_snonce_valid = TRUE;
  904. os_memcpy(sm->alt_replay_counter,
  905. sm->key_replay[0].counter,
  906. WPA_REPLAY_COUNTER_LEN);
  907. goto continue_processing;
  908. }
  909. if (msg == PAIRWISE_4 && sm->alt_snonce_valid &&
  910. sm->wpa_ptk_state == WPA_PTK_PTKINITNEGOTIATING &&
  911. os_memcmp(key->replay_counter, sm->alt_replay_counter,
  912. WPA_REPLAY_COUNTER_LEN) == 0) {
  913. /*
  914. * Supplicant may still be using the old SNonce since
  915. * there was two EAPOL-Key 2/4 messages and they had
  916. * different SNonce values.
  917. */
  918. wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_DEBUG,
  919. "Try to process received EAPOL-Key 4/4 based on old Replay Counter and SNonce from an earlier EAPOL-Key 1/4");
  920. goto continue_processing;
  921. }
  922. if (msg == PAIRWISE_2 &&
  923. wpa_replay_counter_valid(sm->prev_key_replay,
  924. key->replay_counter) &&
  925. sm->wpa_ptk_state == WPA_PTK_PTKINITNEGOTIATING) {
  926. wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_DEBUG,
  927. "ignore retransmitted EAPOL-Key %s - "
  928. "SNonce did not change", msgtxt);
  929. } else {
  930. wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_DEBUG,
  931. "received EAPOL-Key %s with "
  932. "unexpected replay counter", msgtxt);
  933. }
  934. for (i = 0; i < RSNA_MAX_EAPOL_RETRIES; i++) {
  935. if (!sm->key_replay[i].valid)
  936. break;
  937. wpa_hexdump(MSG_DEBUG, "pending replay counter",
  938. sm->key_replay[i].counter,
  939. WPA_REPLAY_COUNTER_LEN);
  940. }
  941. wpa_hexdump(MSG_DEBUG, "received replay counter",
  942. key->replay_counter, WPA_REPLAY_COUNTER_LEN);
  943. return;
  944. }
  945. continue_processing:
  946. #ifdef CONFIG_FILS
  947. if (sm->wpa == WPA_VERSION_WPA2 && mic_len == 0 &&
  948. !(key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
  949. wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_DEBUG,
  950. "WPA: Encr Key Data bit not set even though AEAD cipher is supposed to be used - drop frame");
  951. return;
  952. }
  953. #endif /* CONFIG_FILS */
  954. switch (msg) {
  955. case PAIRWISE_2:
  956. if (sm->wpa_ptk_state != WPA_PTK_PTKSTART &&
  957. sm->wpa_ptk_state != WPA_PTK_PTKCALCNEGOTIATING &&
  958. (!sm->update_snonce ||
  959. sm->wpa_ptk_state != WPA_PTK_PTKINITNEGOTIATING)) {
  960. wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_INFO,
  961. "received EAPOL-Key msg 2/4 in "
  962. "invalid state (%d) - dropped",
  963. sm->wpa_ptk_state);
  964. return;
  965. }
  966. random_add_randomness(key->key_nonce, WPA_NONCE_LEN);
  967. if (sm->group->reject_4way_hs_for_entropy) {
  968. /*
  969. * The system did not have enough entropy to generate
  970. * strong random numbers. Reject the first 4-way
  971. * handshake(s) and collect some entropy based on the
  972. * information from it. Once enough entropy is
  973. * available, the next atempt will trigger GMK/Key
  974. * Counter update and the station will be allowed to
  975. * continue.
  976. */
  977. wpa_printf(MSG_DEBUG, "WPA: Reject 4-way handshake to "
  978. "collect more entropy for random number "
  979. "generation");
  980. random_mark_pool_ready();
  981. wpa_sta_disconnect(wpa_auth, sm->addr);
  982. return;
  983. }
  984. break;
  985. case PAIRWISE_4:
  986. if (sm->wpa_ptk_state != WPA_PTK_PTKINITNEGOTIATING ||
  987. !sm->PTK_valid) {
  988. wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_INFO,
  989. "received EAPOL-Key msg 4/4 in "
  990. "invalid state (%d) - dropped",
  991. sm->wpa_ptk_state);
  992. return;
  993. }
  994. break;
  995. case GROUP_2:
  996. if (sm->wpa_ptk_group_state != WPA_PTK_GROUP_REKEYNEGOTIATING
  997. || !sm->PTK_valid) {
  998. wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_INFO,
  999. "received EAPOL-Key msg 2/2 in "
  1000. "invalid state (%d) - dropped",
  1001. sm->wpa_ptk_group_state);
  1002. return;
  1003. }
  1004. break;
  1005. #ifdef CONFIG_PEERKEY
  1006. case SMK_M1:
  1007. case SMK_M3:
  1008. case SMK_ERROR:
  1009. if (!wpa_auth->conf.peerkey) {
  1010. wpa_printf(MSG_DEBUG, "RSN: SMK M1/M3/Error, but "
  1011. "PeerKey use disabled - ignoring message");
  1012. return;
  1013. }
  1014. if (!sm->PTK_valid) {
  1015. wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
  1016. "received EAPOL-Key msg SMK in "
  1017. "invalid state - dropped");
  1018. return;
  1019. }
  1020. break;
  1021. #else /* CONFIG_PEERKEY */
  1022. case SMK_M1:
  1023. case SMK_M3:
  1024. case SMK_ERROR:
  1025. return; /* STSL disabled - ignore SMK messages */
  1026. #endif /* CONFIG_PEERKEY */
  1027. case REQUEST:
  1028. break;
  1029. }
  1030. wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_DEBUG,
  1031. "received EAPOL-Key frame (%s)", msgtxt);
  1032. if (key_info & WPA_KEY_INFO_ACK) {
  1033. wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
  1034. "received invalid EAPOL-Key: Key Ack set");
  1035. return;
  1036. }
  1037. if (!wpa_key_mgmt_fils(sm->wpa_key_mgmt) &&
  1038. !(key_info & WPA_KEY_INFO_MIC)) {
  1039. wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
  1040. "received invalid EAPOL-Key: Key MIC not set");
  1041. return;
  1042. }
  1043. #ifdef CONFIG_FILS
  1044. if (wpa_key_mgmt_fils(sm->wpa_key_mgmt) &&
  1045. (key_info & WPA_KEY_INFO_MIC)) {
  1046. wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
  1047. "received invalid EAPOL-Key: Key MIC set");
  1048. return;
  1049. }
  1050. #endif /* CONFIG_FILS */
  1051. sm->MICVerified = FALSE;
  1052. if (sm->PTK_valid && !sm->update_snonce) {
  1053. if (mic_len &&
  1054. wpa_verify_key_mic(sm->wpa_key_mgmt, &sm->PTK, data,
  1055. data_len) &&
  1056. (msg != PAIRWISE_4 || !sm->alt_snonce_valid ||
  1057. wpa_try_alt_snonce(sm, data, data_len))) {
  1058. wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
  1059. "received EAPOL-Key with invalid MIC");
  1060. return;
  1061. }
  1062. #ifdef CONFIG_FILS
  1063. if (!mic_len &&
  1064. wpa_aead_decrypt(sm, &sm->PTK, data, data_len,
  1065. &key_data_length) < 0) {
  1066. wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
  1067. "received EAPOL-Key with invalid MIC");
  1068. return;
  1069. }
  1070. #endif /* CONFIG_FILS */
  1071. sm->MICVerified = TRUE;
  1072. eloop_cancel_timeout(wpa_send_eapol_timeout, wpa_auth, sm);
  1073. sm->pending_1_of_4_timeout = 0;
  1074. }
  1075. if (key_info & WPA_KEY_INFO_REQUEST) {
  1076. if (sm->MICVerified) {
  1077. sm->req_replay_counter_used = 1;
  1078. os_memcpy(sm->req_replay_counter, key->replay_counter,
  1079. WPA_REPLAY_COUNTER_LEN);
  1080. } else {
  1081. wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
  1082. "received EAPOL-Key request with "
  1083. "invalid MIC");
  1084. return;
  1085. }
  1086. /*
  1087. * TODO: should decrypt key data field if encryption was used;
  1088. * even though MAC address KDE is not normally encrypted,
  1089. * supplicant is allowed to encrypt it.
  1090. */
  1091. if (msg == SMK_ERROR) {
  1092. #ifdef CONFIG_PEERKEY
  1093. wpa_smk_error(wpa_auth, sm, key_data, key_data_length);
  1094. #endif /* CONFIG_PEERKEY */
  1095. return;
  1096. } else if (key_info & WPA_KEY_INFO_ERROR) {
  1097. if (wpa_receive_error_report(
  1098. wpa_auth, sm,
  1099. !(key_info & WPA_KEY_INFO_KEY_TYPE)) > 0)
  1100. return; /* STA entry was removed */
  1101. } else if (key_info & WPA_KEY_INFO_KEY_TYPE) {
  1102. wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
  1103. "received EAPOL-Key Request for new "
  1104. "4-Way Handshake");
  1105. wpa_request_new_ptk(sm);
  1106. #ifdef CONFIG_PEERKEY
  1107. } else if (msg == SMK_M1) {
  1108. wpa_smk_m1(wpa_auth, sm, key, key_data,
  1109. key_data_length);
  1110. #endif /* CONFIG_PEERKEY */
  1111. } else if (key_data_length > 0 &&
  1112. wpa_parse_kde_ies(key_data, key_data_length,
  1113. &kde) == 0 &&
  1114. kde.mac_addr) {
  1115. } else {
  1116. wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
  1117. "received EAPOL-Key Request for GTK "
  1118. "rekeying");
  1119. eloop_cancel_timeout(wpa_rekey_gtk, wpa_auth, NULL);
  1120. wpa_rekey_gtk(wpa_auth, NULL);
  1121. }
  1122. } else {
  1123. /* Do not allow the same key replay counter to be reused. */
  1124. wpa_replay_counter_mark_invalid(sm->key_replay,
  1125. key->replay_counter);
  1126. if (msg == PAIRWISE_2) {
  1127. /*
  1128. * Maintain a copy of the pending EAPOL-Key frames in
  1129. * case the EAPOL-Key frame was retransmitted. This is
  1130. * needed to allow EAPOL-Key msg 2/4 reply to another
  1131. * pending msg 1/4 to update the SNonce to work around
  1132. * unexpected supplicant behavior.
  1133. */
  1134. os_memcpy(sm->prev_key_replay, sm->key_replay,
  1135. sizeof(sm->key_replay));
  1136. } else {
  1137. os_memset(sm->prev_key_replay, 0,
  1138. sizeof(sm->prev_key_replay));
  1139. }
  1140. /*
  1141. * Make sure old valid counters are not accepted anymore and
  1142. * do not get copied again.
  1143. */
  1144. wpa_replay_counter_mark_invalid(sm->key_replay, NULL);
  1145. }
  1146. #ifdef CONFIG_PEERKEY
  1147. if (msg == SMK_M3) {
  1148. wpa_smk_m3(wpa_auth, sm, key, key_data, key_data_length);
  1149. return;
  1150. }
  1151. #endif /* CONFIG_PEERKEY */
  1152. os_free(sm->last_rx_eapol_key);
  1153. sm->last_rx_eapol_key = os_memdup(data, data_len);
  1154. if (sm->last_rx_eapol_key == NULL)
  1155. return;
  1156. sm->last_rx_eapol_key_len = data_len;
  1157. sm->rx_eapol_key_secure = !!(key_info & WPA_KEY_INFO_SECURE);
  1158. sm->EAPOLKeyReceived = TRUE;
  1159. sm->EAPOLKeyPairwise = !!(key_info & WPA_KEY_INFO_KEY_TYPE);
  1160. sm->EAPOLKeyRequest = !!(key_info & WPA_KEY_INFO_REQUEST);
  1161. os_memcpy(sm->SNonce, key->key_nonce, WPA_NONCE_LEN);
  1162. wpa_sm_step(sm);
  1163. }
  1164. static int wpa_gmk_to_gtk(const u8 *gmk, const char *label, const u8 *addr,
  1165. const u8 *gnonce, u8 *gtk, size_t gtk_len)
  1166. {
  1167. u8 data[ETH_ALEN + WPA_NONCE_LEN + 8 + 16];
  1168. u8 *pos;
  1169. int ret = 0;
  1170. /* GTK = PRF-X(GMK, "Group key expansion",
  1171. * AA || GNonce || Time || random data)
  1172. * The example described in the IEEE 802.11 standard uses only AA and
  1173. * GNonce as inputs here. Add some more entropy since this derivation
  1174. * is done only at the Authenticator and as such, does not need to be
  1175. * exactly same.
  1176. */
  1177. os_memcpy(data, addr, ETH_ALEN);
  1178. os_memcpy(data + ETH_ALEN, gnonce, WPA_NONCE_LEN);
  1179. pos = data + ETH_ALEN + WPA_NONCE_LEN;
  1180. wpa_get_ntp_timestamp(pos);
  1181. pos += 8;
  1182. if (random_get_bytes(pos, 16) < 0)
  1183. ret = -1;
  1184. #ifdef CONFIG_IEEE80211W
  1185. sha256_prf(gmk, WPA_GMK_LEN, label, data, sizeof(data), gtk, gtk_len);
  1186. #else /* CONFIG_IEEE80211W */
  1187. if (sha1_prf(gmk, WPA_GMK_LEN, label, data, sizeof(data), gtk, gtk_len)
  1188. < 0)
  1189. ret = -1;
  1190. #endif /* CONFIG_IEEE80211W */
  1191. return ret;
  1192. }
  1193. static void wpa_send_eapol_timeout(void *eloop_ctx, void *timeout_ctx)
  1194. {
  1195. struct wpa_authenticator *wpa_auth = eloop_ctx;
  1196. struct wpa_state_machine *sm = timeout_ctx;
  1197. sm->pending_1_of_4_timeout = 0;
  1198. wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG, "EAPOL-Key timeout");
  1199. sm->TimeoutEvt = TRUE;
  1200. wpa_sm_step(sm);
  1201. }
  1202. void __wpa_send_eapol(struct wpa_authenticator *wpa_auth,
  1203. struct wpa_state_machine *sm, int key_info,
  1204. const u8 *key_rsc, const u8 *nonce,
  1205. const u8 *kde, size_t kde_len,
  1206. int keyidx, int encr, int force_version)
  1207. {
  1208. struct ieee802_1x_hdr *hdr;
  1209. struct wpa_eapol_key *key;
  1210. size_t len, mic_len, keyhdrlen;
  1211. int alg;
  1212. int key_data_len, pad_len = 0;
  1213. u8 *buf, *pos;
  1214. int version, pairwise;
  1215. int i;
  1216. u8 *key_mic, *key_data;
  1217. mic_len = wpa_mic_len(sm->wpa_key_mgmt);
  1218. keyhdrlen = sizeof(*key) + mic_len + 2;
  1219. len = sizeof(struct ieee802_1x_hdr) + keyhdrlen;
  1220. if (force_version)
  1221. version = force_version;
  1222. else if (sm->wpa_key_mgmt == WPA_KEY_MGMT_OSEN ||
  1223. wpa_key_mgmt_suite_b(sm->wpa_key_mgmt) ||
  1224. wpa_key_mgmt_fils(sm->wpa_key_mgmt))
  1225. version = WPA_KEY_INFO_TYPE_AKM_DEFINED;
  1226. else if (wpa_use_aes_cmac(sm))
  1227. version = WPA_KEY_INFO_TYPE_AES_128_CMAC;
  1228. else if (sm->pairwise != WPA_CIPHER_TKIP)
  1229. version = WPA_KEY_INFO_TYPE_HMAC_SHA1_AES;
  1230. else
  1231. version = WPA_KEY_INFO_TYPE_HMAC_MD5_RC4;
  1232. pairwise = !!(key_info & WPA_KEY_INFO_KEY_TYPE);
  1233. wpa_printf(MSG_DEBUG, "WPA: Send EAPOL(version=%d secure=%d mic=%d "
  1234. "ack=%d install=%d pairwise=%d kde_len=%lu keyidx=%d "
  1235. "encr=%d)",
  1236. version,
  1237. (key_info & WPA_KEY_INFO_SECURE) ? 1 : 0,
  1238. (key_info & WPA_KEY_INFO_MIC) ? 1 : 0,
  1239. (key_info & WPA_KEY_INFO_ACK) ? 1 : 0,
  1240. (key_info & WPA_KEY_INFO_INSTALL) ? 1 : 0,
  1241. pairwise, (unsigned long) kde_len, keyidx, encr);
  1242. key_data_len = kde_len;
  1243. if ((version == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES ||
  1244. sm->wpa_key_mgmt == WPA_KEY_MGMT_OSEN ||
  1245. wpa_key_mgmt_suite_b(sm->wpa_key_mgmt) ||
  1246. version == WPA_KEY_INFO_TYPE_AES_128_CMAC) && encr) {
  1247. pad_len = key_data_len % 8;
  1248. if (pad_len)
  1249. pad_len = 8 - pad_len;
  1250. key_data_len += pad_len + 8;
  1251. }
  1252. len += key_data_len;
  1253. if (!mic_len && encr)
  1254. len += AES_BLOCK_SIZE;
  1255. hdr = os_zalloc(len);
  1256. if (hdr == NULL)
  1257. return;
  1258. hdr->version = wpa_auth->conf.eapol_version;
  1259. hdr->type = IEEE802_1X_TYPE_EAPOL_KEY;
  1260. hdr->length = host_to_be16(len - sizeof(*hdr));
  1261. key = (struct wpa_eapol_key *) (hdr + 1);
  1262. key_mic = (u8 *) (key + 1);
  1263. key_data = ((u8 *) (hdr + 1)) + keyhdrlen;
  1264. key->type = sm->wpa == WPA_VERSION_WPA2 ?
  1265. EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA;
  1266. key_info |= version;
  1267. if (encr && sm->wpa == WPA_VERSION_WPA2)
  1268. key_info |= WPA_KEY_INFO_ENCR_KEY_DATA;
  1269. if (sm->wpa != WPA_VERSION_WPA2)
  1270. key_info |= keyidx << WPA_KEY_INFO_KEY_INDEX_SHIFT;
  1271. WPA_PUT_BE16(key->key_info, key_info);
  1272. alg = pairwise ? sm->pairwise : wpa_auth->conf.wpa_group;
  1273. if ((key_info & WPA_KEY_INFO_SMK_MESSAGE) ||
  1274. (sm->wpa == WPA_VERSION_WPA2 && !pairwise))
  1275. WPA_PUT_BE16(key->key_length, 0);
  1276. else
  1277. WPA_PUT_BE16(key->key_length, wpa_cipher_key_len(alg));
  1278. /* FIX: STSL: what to use as key_replay_counter? */
  1279. for (i = RSNA_MAX_EAPOL_RETRIES - 1; i > 0; i--) {
  1280. sm->key_replay[i].valid = sm->key_replay[i - 1].valid;
  1281. os_memcpy(sm->key_replay[i].counter,
  1282. sm->key_replay[i - 1].counter,
  1283. WPA_REPLAY_COUNTER_LEN);
  1284. }
  1285. inc_byte_array(sm->key_replay[0].counter, WPA_REPLAY_COUNTER_LEN);
  1286. os_memcpy(key->replay_counter, sm->key_replay[0].counter,
  1287. WPA_REPLAY_COUNTER_LEN);
  1288. wpa_hexdump(MSG_DEBUG, "WPA: Replay Counter",
  1289. key->replay_counter, WPA_REPLAY_COUNTER_LEN);
  1290. sm->key_replay[0].valid = TRUE;
  1291. if (nonce)
  1292. os_memcpy(key->key_nonce, nonce, WPA_NONCE_LEN);
  1293. if (key_rsc)
  1294. os_memcpy(key->key_rsc, key_rsc, WPA_KEY_RSC_LEN);
  1295. if (kde && !encr) {
  1296. os_memcpy(key_data, kde, kde_len);
  1297. WPA_PUT_BE16(key_mic + mic_len, kde_len);
  1298. #ifdef CONFIG_FILS
  1299. } else if (!mic_len) {
  1300. const u8 *aad[1];
  1301. size_t aad_len[1];
  1302. WPA_PUT_BE16(key_mic, AES_BLOCK_SIZE + kde_len);
  1303. wpa_hexdump_key(MSG_DEBUG, "Plaintext EAPOL-Key Key Data",
  1304. kde, kde_len);
  1305. wpa_hexdump_key(MSG_DEBUG, "WPA: KEK",
  1306. sm->PTK.kek, sm->PTK.kek_len);
  1307. /* AES-SIV AAD from EAPOL protocol version field (inclusive) to
  1308. * to Key Data (exclusive). */
  1309. aad[0] = (u8 *) hdr;
  1310. aad_len[0] = key_mic + 2 - (u8 *) hdr;
  1311. if (aes_siv_encrypt(sm->PTK.kek, sm->PTK.kek_len, kde, kde_len,
  1312. 1, aad, aad_len, key_mic + 2) < 0) {
  1313. wpa_printf(MSG_DEBUG, "WPA: AES-SIV encryption failed");
  1314. return;
  1315. }
  1316. wpa_hexdump(MSG_DEBUG, "WPA: Encrypted Key Data from SIV",
  1317. key_mic + 2, AES_BLOCK_SIZE + kde_len);
  1318. #endif /* CONFIG_FILS */
  1319. } else if (encr && kde) {
  1320. buf = os_zalloc(key_data_len);
  1321. if (buf == NULL) {
  1322. os_free(hdr);
  1323. return;
  1324. }
  1325. pos = buf;
  1326. os_memcpy(pos, kde, kde_len);
  1327. pos += kde_len;
  1328. if (pad_len)
  1329. *pos++ = 0xdd;
  1330. wpa_hexdump_key(MSG_DEBUG, "Plaintext EAPOL-Key Key Data",
  1331. buf, key_data_len);
  1332. if (version == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES ||
  1333. sm->wpa_key_mgmt == WPA_KEY_MGMT_OSEN ||
  1334. wpa_key_mgmt_suite_b(sm->wpa_key_mgmt) ||
  1335. version == WPA_KEY_INFO_TYPE_AES_128_CMAC) {
  1336. wpa_printf(MSG_DEBUG,
  1337. "WPA: Encrypt Key Data using AES-WRAP (KEK length %u)",
  1338. (unsigned int) sm->PTK.kek_len);
  1339. if (aes_wrap(sm->PTK.kek, sm->PTK.kek_len,
  1340. (key_data_len - 8) / 8, buf, key_data)) {
  1341. os_free(hdr);
  1342. os_free(buf);
  1343. return;
  1344. }
  1345. WPA_PUT_BE16(key_mic + mic_len, key_data_len);
  1346. #ifndef CONFIG_NO_RC4
  1347. } else if (sm->PTK.kek_len == 16) {
  1348. u8 ek[32];
  1349. wpa_printf(MSG_DEBUG,
  1350. "WPA: Encrypt Key Data using RC4");
  1351. os_memcpy(key->key_iv,
  1352. sm->group->Counter + WPA_NONCE_LEN - 16, 16);
  1353. inc_byte_array(sm->group->Counter, WPA_NONCE_LEN);
  1354. os_memcpy(ek, key->key_iv, 16);
  1355. os_memcpy(ek + 16, sm->PTK.kek, sm->PTK.kek_len);
  1356. os_memcpy(key_data, buf, key_data_len);
  1357. rc4_skip(ek, 32, 256, key_data, key_data_len);
  1358. WPA_PUT_BE16(key_mic + mic_len, key_data_len);
  1359. #endif /* CONFIG_NO_RC4 */
  1360. } else {
  1361. os_free(hdr);
  1362. os_free(buf);
  1363. return;
  1364. }
  1365. os_free(buf);
  1366. }
  1367. if (key_info & WPA_KEY_INFO_MIC) {
  1368. if (!sm->PTK_valid || !mic_len) {
  1369. wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG,
  1370. "PTK not valid when sending EAPOL-Key "
  1371. "frame");
  1372. os_free(hdr);
  1373. return;
  1374. }
  1375. wpa_eapol_key_mic(sm->PTK.kck, sm->PTK.kck_len,
  1376. sm->wpa_key_mgmt, version,
  1377. (u8 *) hdr, len, key_mic);
  1378. #ifdef CONFIG_TESTING_OPTIONS
  1379. if (!pairwise &&
  1380. wpa_auth->conf.corrupt_gtk_rekey_mic_probability > 0.0 &&
  1381. drand48() <
  1382. wpa_auth->conf.corrupt_gtk_rekey_mic_probability) {
  1383. wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
  1384. "Corrupting group EAPOL-Key Key MIC");
  1385. key_mic[0]++;
  1386. }
  1387. #endif /* CONFIG_TESTING_OPTIONS */
  1388. }
  1389. wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_inc_EapolFramesTx,
  1390. 1);
  1391. wpa_auth_send_eapol(wpa_auth, sm->addr, (u8 *) hdr, len,
  1392. sm->pairwise_set);
  1393. os_free(hdr);
  1394. }
  1395. static void wpa_send_eapol(struct wpa_authenticator *wpa_auth,
  1396. struct wpa_state_machine *sm, int key_info,
  1397. const u8 *key_rsc, const u8 *nonce,
  1398. const u8 *kde, size_t kde_len,
  1399. int keyidx, int encr)
  1400. {
  1401. int timeout_ms;
  1402. int pairwise = key_info & WPA_KEY_INFO_KEY_TYPE;
  1403. u32 ctr;
  1404. if (sm == NULL)
  1405. return;
  1406. __wpa_send_eapol(wpa_auth, sm, key_info, key_rsc, nonce, kde, kde_len,
  1407. keyidx, encr, 0);
  1408. ctr = pairwise ? sm->TimeoutCtr : sm->GTimeoutCtr;
  1409. if (ctr == 1 && wpa_auth->conf.tx_status)
  1410. timeout_ms = pairwise ? eapol_key_timeout_first :
  1411. eapol_key_timeout_first_group;
  1412. else
  1413. timeout_ms = eapol_key_timeout_subseq;
  1414. if (pairwise && ctr == 1 && !(key_info & WPA_KEY_INFO_MIC))
  1415. sm->pending_1_of_4_timeout = 1;
  1416. wpa_printf(MSG_DEBUG, "WPA: Use EAPOL-Key timeout of %u ms (retry "
  1417. "counter %u)", timeout_ms, ctr);
  1418. eloop_register_timeout(timeout_ms / 1000, (timeout_ms % 1000) * 1000,
  1419. wpa_send_eapol_timeout, wpa_auth, sm);
  1420. }
  1421. static int wpa_verify_key_mic(int akmp, struct wpa_ptk *PTK, u8 *data,
  1422. size_t data_len)
  1423. {
  1424. struct ieee802_1x_hdr *hdr;
  1425. struct wpa_eapol_key *key;
  1426. u16 key_info;
  1427. int ret = 0;
  1428. u8 mic[WPA_EAPOL_KEY_MIC_MAX_LEN], *mic_pos;
  1429. size_t mic_len = wpa_mic_len(akmp);
  1430. if (data_len < sizeof(*hdr) + sizeof(*key))
  1431. return -1;
  1432. hdr = (struct ieee802_1x_hdr *) data;
  1433. key = (struct wpa_eapol_key *) (hdr + 1);
  1434. mic_pos = (u8 *) (key + 1);
  1435. key_info = WPA_GET_BE16(key->key_info);
  1436. os_memcpy(mic, mic_pos, mic_len);
  1437. os_memset(mic_pos, 0, mic_len);
  1438. if (wpa_eapol_key_mic(PTK->kck, PTK->kck_len, akmp,
  1439. key_info & WPA_KEY_INFO_TYPE_MASK,
  1440. data, data_len, mic_pos) ||
  1441. os_memcmp_const(mic, mic_pos, mic_len) != 0)
  1442. ret = -1;
  1443. os_memcpy(mic_pos, mic, mic_len);
  1444. return ret;
  1445. }
  1446. void wpa_remove_ptk(struct wpa_state_machine *sm)
  1447. {
  1448. sm->PTK_valid = FALSE;
  1449. os_memset(&sm->PTK, 0, sizeof(sm->PTK));
  1450. if (wpa_auth_set_key(sm->wpa_auth, 0, WPA_ALG_NONE, sm->addr, 0, NULL,
  1451. 0))
  1452. wpa_printf(MSG_DEBUG,
  1453. "RSN: PTK removal from the driver failed");
  1454. sm->pairwise_set = FALSE;
  1455. eloop_cancel_timeout(wpa_rekey_ptk, sm->wpa_auth, sm);
  1456. }
  1457. int wpa_auth_sm_event(struct wpa_state_machine *sm, enum wpa_event event)
  1458. {
  1459. int remove_ptk = 1;
  1460. if (sm == NULL)
  1461. return -1;
  1462. wpa_auth_vlogger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
  1463. "event %d notification", event);
  1464. switch (event) {
  1465. case WPA_AUTH:
  1466. #ifdef CONFIG_MESH
  1467. /* PTKs are derived through AMPE */
  1468. if (wpa_auth_start_ampe(sm->wpa_auth, sm->addr)) {
  1469. /* not mesh */
  1470. break;
  1471. }
  1472. return 0;
  1473. #endif /* CONFIG_MESH */
  1474. case WPA_ASSOC:
  1475. break;
  1476. case WPA_DEAUTH:
  1477. case WPA_DISASSOC:
  1478. sm->DeauthenticationRequest = TRUE;
  1479. break;
  1480. case WPA_REAUTH:
  1481. case WPA_REAUTH_EAPOL:
  1482. if (!sm->started) {
  1483. /*
  1484. * When using WPS, we may end up here if the STA
  1485. * manages to re-associate without the previous STA
  1486. * entry getting removed. Consequently, we need to make
  1487. * sure that the WPA state machines gets initialized
  1488. * properly at this point.
  1489. */
  1490. wpa_printf(MSG_DEBUG, "WPA state machine had not been "
  1491. "started - initialize now");
  1492. sm->started = 1;
  1493. sm->Init = TRUE;
  1494. if (wpa_sm_step(sm) == 1)
  1495. return 1; /* should not really happen */
  1496. sm->Init = FALSE;
  1497. sm->AuthenticationRequest = TRUE;
  1498. break;
  1499. }
  1500. if (sm->GUpdateStationKeys) {
  1501. /*
  1502. * Reauthentication cancels the pending group key
  1503. * update for this STA.
  1504. */
  1505. sm->group->GKeyDoneStations--;
  1506. sm->GUpdateStationKeys = FALSE;
  1507. sm->PtkGroupInit = TRUE;
  1508. }
  1509. sm->ReAuthenticationRequest = TRUE;
  1510. break;
  1511. case WPA_ASSOC_FT:
  1512. #ifdef CONFIG_IEEE80211R_AP
  1513. wpa_printf(MSG_DEBUG, "FT: Retry PTK configuration "
  1514. "after association");
  1515. wpa_ft_install_ptk(sm);
  1516. /* Using FT protocol, not WPA auth state machine */
  1517. sm->ft_completed = 1;
  1518. return 0;
  1519. #else /* CONFIG_IEEE80211R_AP */
  1520. break;
  1521. #endif /* CONFIG_IEEE80211R_AP */
  1522. }
  1523. #ifdef CONFIG_IEEE80211R_AP
  1524. sm->ft_completed = 0;
  1525. #endif /* CONFIG_IEEE80211R_AP */
  1526. #ifdef CONFIG_IEEE80211W
  1527. if (sm->mgmt_frame_prot && event == WPA_AUTH)
  1528. remove_ptk = 0;
  1529. #endif /* CONFIG_IEEE80211W */
  1530. #ifdef CONFIG_FILS
  1531. if (wpa_key_mgmt_fils(sm->wpa_key_mgmt) &&
  1532. (event == WPA_AUTH || event == WPA_ASSOC))
  1533. remove_ptk = 0;
  1534. #endif /* CONFIG_FILS */
  1535. if (remove_ptk) {
  1536. sm->PTK_valid = FALSE;
  1537. os_memset(&sm->PTK, 0, sizeof(sm->PTK));
  1538. if (event != WPA_REAUTH_EAPOL)
  1539. wpa_remove_ptk(sm);
  1540. }
  1541. if (sm->in_step_loop) {
  1542. /*
  1543. * wpa_sm_step() is already running - avoid recursive call to
  1544. * it by making the existing loop process the new update.
  1545. */
  1546. sm->changed = TRUE;
  1547. return 0;
  1548. }
  1549. return wpa_sm_step(sm);
  1550. }
  1551. SM_STATE(WPA_PTK, INITIALIZE)
  1552. {
  1553. SM_ENTRY_MA(WPA_PTK, INITIALIZE, wpa_ptk);
  1554. if (sm->Init) {
  1555. /* Init flag is not cleared here, so avoid busy
  1556. * loop by claiming nothing changed. */
  1557. sm->changed = FALSE;
  1558. }
  1559. sm->keycount = 0;
  1560. if (sm->GUpdateStationKeys)
  1561. sm->group->GKeyDoneStations--;
  1562. sm->GUpdateStationKeys = FALSE;
  1563. if (sm->wpa == WPA_VERSION_WPA)
  1564. sm->PInitAKeys = FALSE;
  1565. if (1 /* Unicast cipher supported AND (ESS OR ((IBSS or WDS) and
  1566. * Local AA > Remote AA)) */) {
  1567. sm->Pair = TRUE;
  1568. }
  1569. wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portEnabled, 0);
  1570. wpa_remove_ptk(sm);
  1571. wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portValid, 0);
  1572. sm->TimeoutCtr = 0;
  1573. if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt) ||
  1574. sm->wpa_key_mgmt == WPA_KEY_MGMT_OWE) {
  1575. wpa_auth_set_eapol(sm->wpa_auth, sm->addr,
  1576. WPA_EAPOL_authorized, 0);
  1577. }
  1578. }
  1579. SM_STATE(WPA_PTK, DISCONNECT)
  1580. {
  1581. SM_ENTRY_MA(WPA_PTK, DISCONNECT, wpa_ptk);
  1582. sm->Disconnect = FALSE;
  1583. wpa_sta_disconnect(sm->wpa_auth, sm->addr);
  1584. }
  1585. SM_STATE(WPA_PTK, DISCONNECTED)
  1586. {
  1587. SM_ENTRY_MA(WPA_PTK, DISCONNECTED, wpa_ptk);
  1588. sm->DeauthenticationRequest = FALSE;
  1589. }
  1590. SM_STATE(WPA_PTK, AUTHENTICATION)
  1591. {
  1592. SM_ENTRY_MA(WPA_PTK, AUTHENTICATION, wpa_ptk);
  1593. os_memset(&sm->PTK, 0, sizeof(sm->PTK));
  1594. sm->PTK_valid = FALSE;
  1595. wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portControl_Auto,
  1596. 1);
  1597. wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portEnabled, 1);
  1598. sm->AuthenticationRequest = FALSE;
  1599. }
  1600. static void wpa_group_ensure_init(struct wpa_authenticator *wpa_auth,
  1601. struct wpa_group *group)
  1602. {
  1603. if (group->first_sta_seen)
  1604. return;
  1605. /*
  1606. * System has run bit further than at the time hostapd was started
  1607. * potentially very early during boot up. This provides better chances
  1608. * of collecting more randomness on embedded systems. Re-initialize the
  1609. * GMK and Counter here to improve their strength if there was not
  1610. * enough entropy available immediately after system startup.
  1611. */
  1612. wpa_printf(MSG_DEBUG, "WPA: Re-initialize GMK/Counter on first "
  1613. "station");
  1614. if (random_pool_ready() != 1) {
  1615. wpa_printf(MSG_INFO, "WPA: Not enough entropy in random pool "
  1616. "to proceed - reject first 4-way handshake");
  1617. group->reject_4way_hs_for_entropy = TRUE;
  1618. } else {
  1619. group->first_sta_seen = TRUE;
  1620. group->reject_4way_hs_for_entropy = FALSE;
  1621. }
  1622. if (wpa_group_init_gmk_and_counter(wpa_auth, group) < 0 ||
  1623. wpa_gtk_update(wpa_auth, group) < 0 ||
  1624. wpa_group_config_group_keys(wpa_auth, group) < 0) {
  1625. wpa_printf(MSG_INFO, "WPA: GMK/GTK setup failed");
  1626. group->first_sta_seen = FALSE;
  1627. group->reject_4way_hs_for_entropy = TRUE;
  1628. }
  1629. }
  1630. SM_STATE(WPA_PTK, AUTHENTICATION2)
  1631. {
  1632. SM_ENTRY_MA(WPA_PTK, AUTHENTICATION2, wpa_ptk);
  1633. wpa_group_ensure_init(sm->wpa_auth, sm->group);
  1634. sm->ReAuthenticationRequest = FALSE;
  1635. /*
  1636. * Definition of ANonce selection in IEEE Std 802.11i-2004 is somewhat
  1637. * ambiguous. The Authenticator state machine uses a counter that is
  1638. * incremented by one for each 4-way handshake. However, the security
  1639. * analysis of 4-way handshake points out that unpredictable nonces
  1640. * help in preventing precomputation attacks. Instead of the state
  1641. * machine definition, use an unpredictable nonce value here to provide
  1642. * stronger protection against potential precomputation attacks.
  1643. */
  1644. if (random_get_bytes(sm->ANonce, WPA_NONCE_LEN)) {
  1645. wpa_printf(MSG_ERROR, "WPA: Failed to get random data for "
  1646. "ANonce.");
  1647. sm->Disconnect = TRUE;
  1648. return;
  1649. }
  1650. wpa_hexdump(MSG_DEBUG, "WPA: Assign ANonce", sm->ANonce,
  1651. WPA_NONCE_LEN);
  1652. /* IEEE 802.11i does not clear TimeoutCtr here, but this is more
  1653. * logical place than INITIALIZE since AUTHENTICATION2 can be
  1654. * re-entered on ReAuthenticationRequest without going through
  1655. * INITIALIZE. */
  1656. sm->TimeoutCtr = 0;
  1657. }
  1658. SM_STATE(WPA_PTK, INITPMK)
  1659. {
  1660. u8 msk[2 * PMK_LEN];
  1661. size_t len = 2 * PMK_LEN;
  1662. SM_ENTRY_MA(WPA_PTK, INITPMK, wpa_ptk);
  1663. #ifdef CONFIG_IEEE80211R_AP
  1664. sm->xxkey_len = 0;
  1665. #endif /* CONFIG_IEEE80211R_AP */
  1666. if (sm->pmksa) {
  1667. wpa_printf(MSG_DEBUG, "WPA: PMK from PMKSA cache");
  1668. os_memcpy(sm->PMK, sm->pmksa->pmk, sm->pmksa->pmk_len);
  1669. sm->pmk_len = sm->pmksa->pmk_len;
  1670. } else if (wpa_auth_get_msk(sm->wpa_auth, sm->addr, msk, &len) == 0) {
  1671. unsigned int pmk_len;
  1672. if (wpa_key_mgmt_sha384(sm->wpa_key_mgmt))
  1673. pmk_len = PMK_LEN_SUITE_B_192;
  1674. else
  1675. pmk_len = PMK_LEN;
  1676. wpa_printf(MSG_DEBUG, "WPA: PMK from EAPOL state machine "
  1677. "(MSK len=%lu PMK len=%u)", (unsigned long) len,
  1678. pmk_len);
  1679. if (len < pmk_len) {
  1680. wpa_printf(MSG_DEBUG,
  1681. "WPA: MSK not long enough (%u) to create PMK (%u)",
  1682. (unsigned int) len, (unsigned int) pmk_len);
  1683. sm->Disconnect = TRUE;
  1684. return;
  1685. }
  1686. os_memcpy(sm->PMK, msk, pmk_len);
  1687. sm->pmk_len = pmk_len;
  1688. #ifdef CONFIG_IEEE80211R_AP
  1689. if (len >= 2 * PMK_LEN) {
  1690. os_memcpy(sm->xxkey, msk + PMK_LEN, PMK_LEN);
  1691. sm->xxkey_len = PMK_LEN;
  1692. }
  1693. #endif /* CONFIG_IEEE80211R_AP */
  1694. } else {
  1695. wpa_printf(MSG_DEBUG, "WPA: Could not get PMK, get_msk: %p",
  1696. sm->wpa_auth->cb->get_msk);
  1697. sm->Disconnect = TRUE;
  1698. return;
  1699. }
  1700. os_memset(msk, 0, sizeof(msk));
  1701. sm->req_replay_counter_used = 0;
  1702. /* IEEE 802.11i does not set keyRun to FALSE, but not doing this
  1703. * will break reauthentication since EAPOL state machines may not be
  1704. * get into AUTHENTICATING state that clears keyRun before WPA state
  1705. * machine enters AUTHENTICATION2 state and goes immediately to INITPMK
  1706. * state and takes PMK from the previously used AAA Key. This will
  1707. * eventually fail in 4-Way Handshake because Supplicant uses PMK
  1708. * derived from the new AAA Key. Setting keyRun = FALSE here seems to
  1709. * be good workaround for this issue. */
  1710. wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_keyRun, 0);
  1711. }
  1712. SM_STATE(WPA_PTK, INITPSK)
  1713. {
  1714. const u8 *psk;
  1715. SM_ENTRY_MA(WPA_PTK, INITPSK, wpa_ptk);
  1716. psk = wpa_auth_get_psk(sm->wpa_auth, sm->addr, sm->p2p_dev_addr, NULL);
  1717. if (psk) {
  1718. os_memcpy(sm->PMK, psk, PMK_LEN);
  1719. sm->pmk_len = PMK_LEN;
  1720. #ifdef CONFIG_IEEE80211R_AP
  1721. os_memcpy(sm->xxkey, psk, PMK_LEN);
  1722. sm->xxkey_len = PMK_LEN;
  1723. #endif /* CONFIG_IEEE80211R_AP */
  1724. }
  1725. sm->req_replay_counter_used = 0;
  1726. }
  1727. SM_STATE(WPA_PTK, PTKSTART)
  1728. {
  1729. u8 buf[2 + RSN_SELECTOR_LEN + PMKID_LEN], *pmkid = NULL;
  1730. size_t pmkid_len = 0;
  1731. SM_ENTRY_MA(WPA_PTK, PTKSTART, wpa_ptk);
  1732. sm->PTKRequest = FALSE;
  1733. sm->TimeoutEvt = FALSE;
  1734. sm->alt_snonce_valid = FALSE;
  1735. sm->TimeoutCtr++;
  1736. if (sm->TimeoutCtr > sm->wpa_auth->conf.wpa_pairwise_update_count) {
  1737. /* No point in sending the EAPOL-Key - we will disconnect
  1738. * immediately following this. */
  1739. return;
  1740. }
  1741. wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
  1742. "sending 1/4 msg of 4-Way Handshake");
  1743. /*
  1744. * TODO: Could add PMKID even with WPA2-PSK, but only if there is only
  1745. * one possible PSK for this STA.
  1746. */
  1747. if (sm->wpa == WPA_VERSION_WPA2 &&
  1748. wpa_key_mgmt_wpa_ieee8021x(sm->wpa_key_mgmt) &&
  1749. sm->wpa_key_mgmt != WPA_KEY_MGMT_OSEN) {
  1750. pmkid = buf;
  1751. pmkid_len = 2 + RSN_SELECTOR_LEN + PMKID_LEN;
  1752. pmkid[0] = WLAN_EID_VENDOR_SPECIFIC;
  1753. pmkid[1] = RSN_SELECTOR_LEN + PMKID_LEN;
  1754. RSN_SELECTOR_PUT(&pmkid[2], RSN_KEY_DATA_PMKID);
  1755. if (sm->pmksa) {
  1756. os_memcpy(&pmkid[2 + RSN_SELECTOR_LEN],
  1757. sm->pmksa->pmkid, PMKID_LEN);
  1758. } else if (wpa_key_mgmt_suite_b(sm->wpa_key_mgmt)) {
  1759. /* No KCK available to derive PMKID */
  1760. pmkid = NULL;
  1761. } else {
  1762. /*
  1763. * Calculate PMKID since no PMKSA cache entry was
  1764. * available with pre-calculated PMKID.
  1765. */
  1766. rsn_pmkid(sm->PMK, sm->pmk_len, sm->wpa_auth->addr,
  1767. sm->addr, &pmkid[2 + RSN_SELECTOR_LEN],
  1768. wpa_key_mgmt_sha256(sm->wpa_key_mgmt));
  1769. }
  1770. }
  1771. wpa_send_eapol(sm->wpa_auth, sm,
  1772. WPA_KEY_INFO_ACK | WPA_KEY_INFO_KEY_TYPE, NULL,
  1773. sm->ANonce, pmkid, pmkid_len, 0, 0);
  1774. }
  1775. static int wpa_derive_ptk(struct wpa_state_machine *sm, const u8 *snonce,
  1776. const u8 *pmk, unsigned int pmk_len,
  1777. struct wpa_ptk *ptk)
  1778. {
  1779. #ifdef CONFIG_IEEE80211R_AP
  1780. if (wpa_key_mgmt_ft(sm->wpa_key_mgmt))
  1781. return wpa_auth_derive_ptk_ft(sm, pmk, ptk);
  1782. #endif /* CONFIG_IEEE80211R_AP */
  1783. return wpa_pmk_to_ptk(pmk, pmk_len, "Pairwise key expansion",
  1784. sm->wpa_auth->addr, sm->addr, sm->ANonce, snonce,
  1785. ptk, sm->wpa_key_mgmt, sm->pairwise);
  1786. }
  1787. #ifdef CONFIG_FILS
  1788. int fils_auth_pmk_to_ptk(struct wpa_state_machine *sm, const u8 *pmk,
  1789. size_t pmk_len, const u8 *snonce, const u8 *anonce)
  1790. {
  1791. u8 ick[FILS_ICK_MAX_LEN];
  1792. size_t ick_len;
  1793. int res;
  1794. res = fils_pmk_to_ptk(pmk, pmk_len, sm->addr, sm->wpa_auth->addr,
  1795. snonce, anonce, &sm->PTK, ick, &ick_len,
  1796. sm->wpa_key_mgmt, sm->pairwise);
  1797. if (res < 0)
  1798. return res;
  1799. sm->PTK_valid = TRUE;
  1800. res = fils_key_auth_sk(ick, ick_len, snonce, anonce,
  1801. sm->addr, sm->wpa_auth->addr,
  1802. NULL, 0, NULL, 0, /* TODO: SK+PFS */
  1803. sm->wpa_key_mgmt, sm->fils_key_auth_sta,
  1804. sm->fils_key_auth_ap,
  1805. &sm->fils_key_auth_len);
  1806. os_memset(ick, 0, sizeof(ick));
  1807. /* Store nonces for (Re)Association Request/Response frame processing */
  1808. os_memcpy(sm->SNonce, snonce, FILS_NONCE_LEN);
  1809. os_memcpy(sm->ANonce, anonce, FILS_NONCE_LEN);
  1810. return res;
  1811. }
  1812. static int wpa_aead_decrypt(struct wpa_state_machine *sm, struct wpa_ptk *ptk,
  1813. u8 *buf, size_t buf_len, u16 *_key_data_len)
  1814. {
  1815. struct ieee802_1x_hdr *hdr;
  1816. struct wpa_eapol_key *key;
  1817. u8 *pos;
  1818. u16 key_data_len;
  1819. u8 *tmp;
  1820. const u8 *aad[1];
  1821. size_t aad_len[1];
  1822. hdr = (struct ieee802_1x_hdr *) buf;
  1823. key = (struct wpa_eapol_key *) (hdr + 1);
  1824. pos = (u8 *) (key + 1);
  1825. key_data_len = WPA_GET_BE16(pos);
  1826. if (key_data_len < AES_BLOCK_SIZE ||
  1827. key_data_len > buf_len - sizeof(*hdr) - sizeof(*key) - 2) {
  1828. wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_INFO,
  1829. "No room for AES-SIV data in the frame");
  1830. return -1;
  1831. }
  1832. pos += 2; /* Pointing at the Encrypted Key Data field */
  1833. tmp = os_malloc(key_data_len);
  1834. if (!tmp)
  1835. return -1;
  1836. /* AES-SIV AAD from EAPOL protocol version field (inclusive) to
  1837. * to Key Data (exclusive). */
  1838. aad[0] = buf;
  1839. aad_len[0] = pos - buf;
  1840. if (aes_siv_decrypt(ptk->kek, ptk->kek_len, pos, key_data_len,
  1841. 1, aad, aad_len, tmp) < 0) {
  1842. wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_INFO,
  1843. "Invalid AES-SIV data in the frame");
  1844. bin_clear_free(tmp, key_data_len);
  1845. return -1;
  1846. }
  1847. /* AEAD decryption and validation completed successfully */
  1848. key_data_len -= AES_BLOCK_SIZE;
  1849. wpa_hexdump_key(MSG_DEBUG, "WPA: Decrypted Key Data",
  1850. tmp, key_data_len);
  1851. /* Replace Key Data field with the decrypted version */
  1852. os_memcpy(pos, tmp, key_data_len);
  1853. pos -= 2; /* Key Data Length field */
  1854. WPA_PUT_BE16(pos, key_data_len);
  1855. bin_clear_free(tmp, key_data_len);
  1856. if (_key_data_len)
  1857. *_key_data_len = key_data_len;
  1858. return 0;
  1859. }
  1860. int fils_decrypt_assoc(struct wpa_state_machine *sm, const u8 *fils_session,
  1861. const struct ieee80211_mgmt *mgmt, size_t frame_len,
  1862. u8 *pos, size_t left)
  1863. {
  1864. u16 fc, stype;
  1865. const u8 *end, *ie_start, *ie, *session, *crypt;
  1866. struct ieee802_11_elems elems;
  1867. const u8 *aad[5];
  1868. size_t aad_len[5];
  1869. if (!sm || !sm->PTK_valid) {
  1870. wpa_printf(MSG_DEBUG,
  1871. "FILS: No KEK to decrypt Assocication Request frame");
  1872. return -1;
  1873. }
  1874. if (!wpa_key_mgmt_fils(sm->wpa_key_mgmt)) {
  1875. wpa_printf(MSG_DEBUG,
  1876. "FILS: Not a FILS AKM - reject association");
  1877. return -1;
  1878. }
  1879. end = ((const u8 *) mgmt) + frame_len;
  1880. fc = le_to_host16(mgmt->frame_control);
  1881. stype = WLAN_FC_GET_STYPE(fc);
  1882. if (stype == WLAN_FC_STYPE_REASSOC_REQ)
  1883. ie_start = mgmt->u.reassoc_req.variable;
  1884. else
  1885. ie_start = mgmt->u.assoc_req.variable;
  1886. ie = ie_start;
  1887. /*
  1888. * Find FILS Session element which is the last unencrypted element in
  1889. * the frame.
  1890. */
  1891. session = NULL;
  1892. while (ie + 1 < end) {
  1893. if (ie + 2 + ie[1] > end)
  1894. break;
  1895. if (ie[0] == WLAN_EID_EXTENSION &&
  1896. ie[1] >= 1 + FILS_SESSION_LEN &&
  1897. ie[2] == WLAN_EID_EXT_FILS_SESSION) {
  1898. session = ie;
  1899. break;
  1900. }
  1901. ie += 2 + ie[1];
  1902. }
  1903. if (!session) {
  1904. wpa_printf(MSG_DEBUG,
  1905. "FILS: Could not find FILS Session element in Association Request frame - reject");
  1906. return -1;
  1907. }
  1908. if (os_memcmp(fils_session, session + 3, FILS_SESSION_LEN) != 0) {
  1909. wpa_printf(MSG_DEBUG, "FILS: Session mismatch");
  1910. wpa_hexdump(MSG_DEBUG, "FILS: Expected FILS Session",
  1911. fils_session, FILS_SESSION_LEN);
  1912. wpa_hexdump(MSG_DEBUG, "FILS: Received FILS Session",
  1913. session + 3, FILS_SESSION_LEN);
  1914. return -1;
  1915. }
  1916. crypt = session + 2 + session[1];
  1917. if (end - crypt < AES_BLOCK_SIZE) {
  1918. wpa_printf(MSG_DEBUG,
  1919. "FILS: Too short frame to include AES-SIV data");
  1920. return -1;
  1921. }
  1922. /* AES-SIV AAD vectors */
  1923. /* The STA's MAC address */
  1924. aad[0] = mgmt->sa;
  1925. aad_len[0] = ETH_ALEN;
  1926. /* The AP's BSSID */
  1927. aad[1] = mgmt->da;
  1928. aad_len[1] = ETH_ALEN;
  1929. /* The STA's nonce */
  1930. aad[2] = sm->SNonce;
  1931. aad_len[2] = FILS_NONCE_LEN;
  1932. /* The AP's nonce */
  1933. aad[3] = sm->ANonce;
  1934. aad_len[3] = FILS_NONCE_LEN;
  1935. /*
  1936. * The (Re)Association Request frame from the Capability Information
  1937. * field to the FILS Session element (both inclusive).
  1938. */
  1939. aad[4] = (const u8 *) &mgmt->u.assoc_req.capab_info;
  1940. aad_len[4] = crypt - aad[4];
  1941. if (aes_siv_decrypt(sm->PTK.kek, sm->PTK.kek_len, crypt, end - crypt,
  1942. 5, aad, aad_len, pos + (crypt - ie_start)) < 0) {
  1943. wpa_printf(MSG_DEBUG,
  1944. "FILS: Invalid AES-SIV data in the frame");
  1945. return -1;
  1946. }
  1947. wpa_hexdump(MSG_DEBUG, "FILS: Decrypted Association Request elements",
  1948. pos, left - AES_BLOCK_SIZE);
  1949. if (ieee802_11_parse_elems(pos, left - AES_BLOCK_SIZE, &elems, 1) ==
  1950. ParseFailed) {
  1951. wpa_printf(MSG_DEBUG,
  1952. "FILS: Failed to parse decrypted elements");
  1953. return -1;
  1954. }
  1955. if (!elems.fils_key_confirm) {
  1956. wpa_printf(MSG_DEBUG, "FILS: No FILS Key Confirm element");
  1957. return -1;
  1958. }
  1959. if (elems.fils_key_confirm_len != sm->fils_key_auth_len) {
  1960. wpa_printf(MSG_DEBUG,
  1961. "FILS: Unexpected Key-Auth length %d (expected %d)",
  1962. elems.fils_key_confirm_len,
  1963. (int) sm->fils_key_auth_len);
  1964. return -1;
  1965. }
  1966. if (os_memcmp(elems.fils_key_confirm, sm->fils_key_auth_sta,
  1967. sm->fils_key_auth_len) != 0) {
  1968. wpa_printf(MSG_DEBUG, "FILS: Key-Auth mismatch");
  1969. wpa_hexdump(MSG_DEBUG, "FILS: Received Key-Auth",
  1970. elems.fils_key_confirm,
  1971. elems.fils_key_confirm_len);
  1972. wpa_hexdump(MSG_DEBUG, "FILS: Expected Key-Auth",
  1973. sm->fils_key_auth_sta, sm->fils_key_auth_len);
  1974. return -1;
  1975. }
  1976. return left - AES_BLOCK_SIZE;
  1977. }
  1978. int fils_encrypt_assoc(struct wpa_state_machine *sm, u8 *buf,
  1979. size_t current_len, size_t max_len,
  1980. const struct wpabuf *hlp)
  1981. {
  1982. u8 *end = buf + max_len;
  1983. u8 *pos = buf + current_len;
  1984. struct ieee80211_mgmt *mgmt;
  1985. struct wpabuf *plain;
  1986. u8 *len, *tmp, *tmp2;
  1987. u8 hdr[2];
  1988. u8 *gtk, dummy_gtk[32];
  1989. size_t gtk_len;
  1990. struct wpa_group *gsm;
  1991. const u8 *aad[5];
  1992. size_t aad_len[5];
  1993. if (!sm || !sm->PTK_valid)
  1994. return -1;
  1995. wpa_hexdump(MSG_DEBUG,
  1996. "FILS: Association Response frame before FILS processing",
  1997. buf, current_len);
  1998. mgmt = (struct ieee80211_mgmt *) buf;
  1999. /* AES-SIV AAD vectors */
  2000. /* The AP's BSSID */
  2001. aad[0] = mgmt->sa;
  2002. aad_len[0] = ETH_ALEN;
  2003. /* The STA's MAC address */
  2004. aad[1] = mgmt->da;
  2005. aad_len[1] = ETH_ALEN;
  2006. /* The AP's nonce */
  2007. aad[2] = sm->ANonce;
  2008. aad_len[2] = FILS_NONCE_LEN;
  2009. /* The STA's nonce */
  2010. aad[3] = sm->SNonce;
  2011. aad_len[3] = FILS_NONCE_LEN;
  2012. /*
  2013. * The (Re)Association Response frame from the Capability Information
  2014. * field (the same offset in both Association and Reassociation
  2015. * Response frames) to the FILS Session element (both inclusive).
  2016. */
  2017. aad[4] = (const u8 *) &mgmt->u.assoc_resp.capab_info;
  2018. aad_len[4] = pos - aad[4];
  2019. /* The following elements will be encrypted with AES-SIV */
  2020. plain = wpabuf_alloc(1000);
  2021. if (!plain)
  2022. return -1;
  2023. /* TODO: FILS Public Key */
  2024. /* FILS Key Confirmation */
  2025. wpabuf_put_u8(plain, WLAN_EID_EXTENSION); /* Element ID */
  2026. wpabuf_put_u8(plain, 1 + sm->fils_key_auth_len); /* Length */
  2027. /* Element ID Extension */
  2028. wpabuf_put_u8(plain, WLAN_EID_EXT_FILS_KEY_CONFIRM);
  2029. wpabuf_put_data(plain, sm->fils_key_auth_ap, sm->fils_key_auth_len);
  2030. /* FILS HLP Container */
  2031. if (hlp)
  2032. wpabuf_put_buf(plain, hlp);
  2033. /* TODO: FILS IP Address Assignment */
  2034. /* Key Delivery */
  2035. gsm = sm->group;
  2036. wpabuf_put_u8(plain, WLAN_EID_EXTENSION); /* Element ID */
  2037. len = wpabuf_put(plain, 1);
  2038. wpabuf_put_u8(plain, WLAN_EID_EXT_KEY_DELIVERY);
  2039. wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN,
  2040. wpabuf_put(plain, WPA_KEY_RSC_LEN));
  2041. /* GTK KDE */
  2042. gtk = gsm->GTK[gsm->GN - 1];
  2043. gtk_len = gsm->GTK_len;
  2044. if (sm->wpa_auth->conf.disable_gtk) {
  2045. /*
  2046. * Provide unique random GTK to each STA to prevent use
  2047. * of GTK in the BSS.
  2048. */
  2049. if (random_get_bytes(dummy_gtk, gtk_len) < 0) {
  2050. wpabuf_free(plain);
  2051. return -1;
  2052. }
  2053. gtk = dummy_gtk;
  2054. }
  2055. hdr[0] = gsm->GN & 0x03;
  2056. hdr[1] = 0;
  2057. tmp = wpabuf_put(plain, 0);
  2058. tmp2 = wpa_add_kde(tmp, RSN_KEY_DATA_GROUPKEY, hdr, 2,
  2059. gtk, gtk_len);
  2060. wpabuf_put(plain, tmp2 - tmp);
  2061. /* IGTK KDE */
  2062. tmp = wpabuf_put(plain, 0);
  2063. tmp2 = ieee80211w_kde_add(sm, tmp);
  2064. wpabuf_put(plain, tmp2 - tmp);
  2065. *len = (u8 *) wpabuf_put(plain, 0) - len - 1;
  2066. if (pos + wpabuf_len(plain) + AES_BLOCK_SIZE > end) {
  2067. wpa_printf(MSG_DEBUG,
  2068. "FILS: Not enough room for FILS elements");
  2069. wpabuf_free(plain);
  2070. return -1;
  2071. }
  2072. wpa_hexdump_buf_key(MSG_DEBUG, "FILS: Association Response plaintext",
  2073. plain);
  2074. if (aes_siv_encrypt(sm->PTK.kek, sm->PTK.kek_len,
  2075. wpabuf_head(plain), wpabuf_len(plain),
  2076. 5, aad, aad_len, pos) < 0) {
  2077. wpabuf_free(plain);
  2078. return -1;
  2079. }
  2080. wpa_hexdump(MSG_DEBUG,
  2081. "FILS: Encrypted Association Response elements",
  2082. pos, AES_BLOCK_SIZE + wpabuf_len(plain));
  2083. current_len += wpabuf_len(plain) + AES_BLOCK_SIZE;
  2084. wpabuf_free(plain);
  2085. sm->fils_completed = 1;
  2086. return current_len;
  2087. }
  2088. int fils_set_tk(struct wpa_state_machine *sm)
  2089. {
  2090. enum wpa_alg alg;
  2091. int klen;
  2092. if (!sm || !sm->PTK_valid)
  2093. return -1;
  2094. alg = wpa_cipher_to_alg(sm->pairwise);
  2095. klen = wpa_cipher_key_len(sm->pairwise);
  2096. wpa_printf(MSG_DEBUG, "FILS: Configure TK to the driver");
  2097. if (wpa_auth_set_key(sm->wpa_auth, 0, alg, sm->addr, 0,
  2098. sm->PTK.tk, klen)) {
  2099. wpa_printf(MSG_DEBUG, "FILS: Failed to set TK to the driver");
  2100. return -1;
  2101. }
  2102. return 0;
  2103. }
  2104. #endif /* CONFIG_FILS */
  2105. SM_STATE(WPA_PTK, PTKCALCNEGOTIATING)
  2106. {
  2107. struct wpa_authenticator *wpa_auth = sm->wpa_auth;
  2108. struct wpa_ptk PTK;
  2109. int ok = 0, psk_found = 0;
  2110. const u8 *pmk = NULL;
  2111. unsigned int pmk_len;
  2112. int ft;
  2113. const u8 *eapol_key_ie, *key_data, *mic;
  2114. u16 key_data_length;
  2115. size_t mic_len, eapol_key_ie_len;
  2116. struct ieee802_1x_hdr *hdr;
  2117. struct wpa_eapol_key *key;
  2118. struct wpa_eapol_ie_parse kde;
  2119. SM_ENTRY_MA(WPA_PTK, PTKCALCNEGOTIATING, wpa_ptk);
  2120. sm->EAPOLKeyReceived = FALSE;
  2121. sm->update_snonce = FALSE;
  2122. os_memset(&PTK, 0, sizeof(PTK));
  2123. mic_len = wpa_mic_len(sm->wpa_key_mgmt);
  2124. /* WPA with IEEE 802.1X: use the derived PMK from EAP
  2125. * WPA-PSK: iterate through possible PSKs and select the one matching
  2126. * the packet */
  2127. for (;;) {
  2128. if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt)) {
  2129. pmk = wpa_auth_get_psk(sm->wpa_auth, sm->addr,
  2130. sm->p2p_dev_addr, pmk);
  2131. if (pmk == NULL)
  2132. break;
  2133. psk_found = 1;
  2134. pmk_len = PMK_LEN;
  2135. } else {
  2136. pmk = sm->PMK;
  2137. pmk_len = sm->pmk_len;
  2138. }
  2139. if (wpa_derive_ptk(sm, sm->SNonce, pmk, pmk_len, &PTK) < 0)
  2140. break;
  2141. if (mic_len &&
  2142. wpa_verify_key_mic(sm->wpa_key_mgmt, &PTK,
  2143. sm->last_rx_eapol_key,
  2144. sm->last_rx_eapol_key_len) == 0) {
  2145. ok = 1;
  2146. break;
  2147. }
  2148. #ifdef CONFIG_FILS
  2149. if (!mic_len &&
  2150. wpa_aead_decrypt(sm, &PTK, sm->last_rx_eapol_key,
  2151. sm->last_rx_eapol_key_len, NULL) == 0) {
  2152. ok = 1;
  2153. break;
  2154. }
  2155. #endif /* CONFIG_FILS */
  2156. if (!wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt))
  2157. break;
  2158. }
  2159. if (!ok) {
  2160. wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
  2161. "invalid MIC in msg 2/4 of 4-Way Handshake");
  2162. if (psk_found)
  2163. wpa_auth_psk_failure_report(sm->wpa_auth, sm->addr);
  2164. return;
  2165. }
  2166. /*
  2167. * Note: last_rx_eapol_key length fields have already been validated in
  2168. * wpa_receive().
  2169. */
  2170. hdr = (struct ieee802_1x_hdr *) sm->last_rx_eapol_key;
  2171. key = (struct wpa_eapol_key *) (hdr + 1);
  2172. mic = (u8 *) (key + 1);
  2173. key_data = mic + mic_len + 2;
  2174. key_data_length = WPA_GET_BE16(mic + mic_len);
  2175. if (key_data_length > sm->last_rx_eapol_key_len - sizeof(*hdr) -
  2176. sizeof(*key) - mic_len - 2)
  2177. return;
  2178. if (wpa_parse_kde_ies(key_data, key_data_length, &kde) < 0) {
  2179. wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_INFO,
  2180. "received EAPOL-Key msg 2/4 with invalid Key Data contents");
  2181. return;
  2182. }
  2183. if (kde.rsn_ie) {
  2184. eapol_key_ie = kde.rsn_ie;
  2185. eapol_key_ie_len = kde.rsn_ie_len;
  2186. } else if (kde.osen) {
  2187. eapol_key_ie = kde.osen;
  2188. eapol_key_ie_len = kde.osen_len;
  2189. } else {
  2190. eapol_key_ie = kde.wpa_ie;
  2191. eapol_key_ie_len = kde.wpa_ie_len;
  2192. }
  2193. ft = sm->wpa == WPA_VERSION_WPA2 && wpa_key_mgmt_ft(sm->wpa_key_mgmt);
  2194. if (sm->wpa_ie == NULL ||
  2195. wpa_compare_rsn_ie(ft, sm->wpa_ie, sm->wpa_ie_len,
  2196. eapol_key_ie, eapol_key_ie_len)) {
  2197. wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
  2198. "WPA IE from (Re)AssocReq did not match with msg 2/4");
  2199. if (sm->wpa_ie) {
  2200. wpa_hexdump(MSG_DEBUG, "WPA IE in AssocReq",
  2201. sm->wpa_ie, sm->wpa_ie_len);
  2202. }
  2203. wpa_hexdump(MSG_DEBUG, "WPA IE in msg 2/4",
  2204. eapol_key_ie, eapol_key_ie_len);
  2205. /* MLME-DEAUTHENTICATE.request */
  2206. wpa_sta_disconnect(wpa_auth, sm->addr);
  2207. return;
  2208. }
  2209. #ifdef CONFIG_IEEE80211R_AP
  2210. if (ft && ft_check_msg_2_of_4(wpa_auth, sm, &kde) < 0) {
  2211. wpa_sta_disconnect(wpa_auth, sm->addr);
  2212. return;
  2213. }
  2214. #endif /* CONFIG_IEEE80211R_AP */
  2215. #ifdef CONFIG_P2P
  2216. if (kde.ip_addr_req && kde.ip_addr_req[0] &&
  2217. wpa_auth->ip_pool && WPA_GET_BE32(sm->ip_addr) == 0) {
  2218. int idx;
  2219. wpa_printf(MSG_DEBUG,
  2220. "P2P: IP address requested in EAPOL-Key exchange");
  2221. idx = bitfield_get_first_zero(wpa_auth->ip_pool);
  2222. if (idx >= 0) {
  2223. u32 start = WPA_GET_BE32(wpa_auth->conf.ip_addr_start);
  2224. bitfield_set(wpa_auth->ip_pool, idx);
  2225. WPA_PUT_BE32(sm->ip_addr, start + idx);
  2226. wpa_printf(MSG_DEBUG,
  2227. "P2P: Assigned IP address %u.%u.%u.%u to "
  2228. MACSTR, sm->ip_addr[0], sm->ip_addr[1],
  2229. sm->ip_addr[2], sm->ip_addr[3],
  2230. MAC2STR(sm->addr));
  2231. }
  2232. }
  2233. #endif /* CONFIG_P2P */
  2234. #ifdef CONFIG_IEEE80211R_AP
  2235. if (sm->wpa == WPA_VERSION_WPA2 && wpa_key_mgmt_ft(sm->wpa_key_mgmt)) {
  2236. /*
  2237. * Verify that PMKR1Name from EAPOL-Key message 2/4 matches
  2238. * with the value we derived.
  2239. */
  2240. if (os_memcmp_const(sm->sup_pmk_r1_name, sm->pmk_r1_name,
  2241. WPA_PMK_NAME_LEN) != 0) {
  2242. wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
  2243. "PMKR1Name mismatch in FT 4-way "
  2244. "handshake");
  2245. wpa_hexdump(MSG_DEBUG, "FT: PMKR1Name from "
  2246. "Supplicant",
  2247. sm->sup_pmk_r1_name, WPA_PMK_NAME_LEN);
  2248. wpa_hexdump(MSG_DEBUG, "FT: Derived PMKR1Name",
  2249. sm->pmk_r1_name, WPA_PMK_NAME_LEN);
  2250. return;
  2251. }
  2252. }
  2253. #endif /* CONFIG_IEEE80211R_AP */
  2254. sm->pending_1_of_4_timeout = 0;
  2255. eloop_cancel_timeout(wpa_send_eapol_timeout, sm->wpa_auth, sm);
  2256. if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt)) {
  2257. /* PSK may have changed from the previous choice, so update
  2258. * state machine data based on whatever PSK was selected here.
  2259. */
  2260. os_memcpy(sm->PMK, pmk, PMK_LEN);
  2261. sm->pmk_len = PMK_LEN;
  2262. }
  2263. sm->MICVerified = TRUE;
  2264. os_memcpy(&sm->PTK, &PTK, sizeof(PTK));
  2265. sm->PTK_valid = TRUE;
  2266. }
  2267. SM_STATE(WPA_PTK, PTKCALCNEGOTIATING2)
  2268. {
  2269. SM_ENTRY_MA(WPA_PTK, PTKCALCNEGOTIATING2, wpa_ptk);
  2270. sm->TimeoutCtr = 0;
  2271. }
  2272. #ifdef CONFIG_IEEE80211W
  2273. static int ieee80211w_kde_len(struct wpa_state_machine *sm)
  2274. {
  2275. if (sm->mgmt_frame_prot) {
  2276. size_t len;
  2277. len = wpa_cipher_key_len(sm->wpa_auth->conf.group_mgmt_cipher);
  2278. return 2 + RSN_SELECTOR_LEN + WPA_IGTK_KDE_PREFIX_LEN + len;
  2279. }
  2280. return 0;
  2281. }
  2282. static u8 * ieee80211w_kde_add(struct wpa_state_machine *sm, u8 *pos)
  2283. {
  2284. struct wpa_igtk_kde igtk;
  2285. struct wpa_group *gsm = sm->group;
  2286. u8 rsc[WPA_KEY_RSC_LEN];
  2287. size_t len = wpa_cipher_key_len(sm->wpa_auth->conf.group_mgmt_cipher);
  2288. if (!sm->mgmt_frame_prot)
  2289. return pos;
  2290. igtk.keyid[0] = gsm->GN_igtk;
  2291. igtk.keyid[1] = 0;
  2292. if (gsm->wpa_group_state != WPA_GROUP_SETKEYSDONE ||
  2293. wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN_igtk, rsc) < 0)
  2294. os_memset(igtk.pn, 0, sizeof(igtk.pn));
  2295. else
  2296. os_memcpy(igtk.pn, rsc, sizeof(igtk.pn));
  2297. os_memcpy(igtk.igtk, gsm->IGTK[gsm->GN_igtk - 4], len);
  2298. if (sm->wpa_auth->conf.disable_gtk) {
  2299. /*
  2300. * Provide unique random IGTK to each STA to prevent use of
  2301. * IGTK in the BSS.
  2302. */
  2303. if (random_get_bytes(igtk.igtk, len) < 0)
  2304. return pos;
  2305. }
  2306. pos = wpa_add_kde(pos, RSN_KEY_DATA_IGTK,
  2307. (const u8 *) &igtk, WPA_IGTK_KDE_PREFIX_LEN + len,
  2308. NULL, 0);
  2309. return pos;
  2310. }
  2311. #else /* CONFIG_IEEE80211W */
  2312. static int ieee80211w_kde_len(struct wpa_state_machine *sm)
  2313. {
  2314. return 0;
  2315. }
  2316. static u8 * ieee80211w_kde_add(struct wpa_state_machine *sm, u8 *pos)
  2317. {
  2318. return pos;
  2319. }
  2320. #endif /* CONFIG_IEEE80211W */
  2321. SM_STATE(WPA_PTK, PTKINITNEGOTIATING)
  2322. {
  2323. u8 rsc[WPA_KEY_RSC_LEN], *_rsc, *gtk, *kde, *pos, dummy_gtk[32];
  2324. size_t gtk_len, kde_len;
  2325. struct wpa_group *gsm = sm->group;
  2326. u8 *wpa_ie;
  2327. int wpa_ie_len, secure, keyidx, encr = 0;
  2328. SM_ENTRY_MA(WPA_PTK, PTKINITNEGOTIATING, wpa_ptk);
  2329. sm->TimeoutEvt = FALSE;
  2330. sm->TimeoutCtr++;
  2331. if (sm->TimeoutCtr > sm->wpa_auth->conf.wpa_pairwise_update_count) {
  2332. /* No point in sending the EAPOL-Key - we will disconnect
  2333. * immediately following this. */
  2334. return;
  2335. }
  2336. /* Send EAPOL(1, 1, 1, Pair, P, RSC, ANonce, MIC(PTK), RSNIE, [MDIE],
  2337. GTK[GN], IGTK, [FTIE], [TIE * 2])
  2338. */
  2339. os_memset(rsc, 0, WPA_KEY_RSC_LEN);
  2340. wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN, rsc);
  2341. /* If FT is used, wpa_auth->wpa_ie includes both RSNIE and MDIE */
  2342. wpa_ie = sm->wpa_auth->wpa_ie;
  2343. wpa_ie_len = sm->wpa_auth->wpa_ie_len;
  2344. if (sm->wpa == WPA_VERSION_WPA &&
  2345. (sm->wpa_auth->conf.wpa & WPA_PROTO_RSN) &&
  2346. wpa_ie_len > wpa_ie[1] + 2 && wpa_ie[0] == WLAN_EID_RSN) {
  2347. /* WPA-only STA, remove RSN IE and possible MDIE */
  2348. wpa_ie = wpa_ie + wpa_ie[1] + 2;
  2349. if (wpa_ie[0] == WLAN_EID_MOBILITY_DOMAIN)
  2350. wpa_ie = wpa_ie + wpa_ie[1] + 2;
  2351. wpa_ie_len = wpa_ie[1] + 2;
  2352. }
  2353. wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
  2354. "sending 3/4 msg of 4-Way Handshake");
  2355. if (sm->wpa == WPA_VERSION_WPA2) {
  2356. /* WPA2 send GTK in the 4-way handshake */
  2357. secure = 1;
  2358. gtk = gsm->GTK[gsm->GN - 1];
  2359. gtk_len = gsm->GTK_len;
  2360. if (sm->wpa_auth->conf.disable_gtk) {
  2361. /*
  2362. * Provide unique random GTK to each STA to prevent use
  2363. * of GTK in the BSS.
  2364. */
  2365. if (random_get_bytes(dummy_gtk, gtk_len) < 0)
  2366. return;
  2367. gtk = dummy_gtk;
  2368. }
  2369. keyidx = gsm->GN;
  2370. _rsc = rsc;
  2371. encr = 1;
  2372. } else {
  2373. /* WPA does not include GTK in msg 3/4 */
  2374. secure = 0;
  2375. gtk = NULL;
  2376. gtk_len = 0;
  2377. keyidx = 0;
  2378. _rsc = NULL;
  2379. if (sm->rx_eapol_key_secure) {
  2380. /*
  2381. * It looks like Windows 7 supplicant tries to use
  2382. * Secure bit in msg 2/4 after having reported Michael
  2383. * MIC failure and it then rejects the 4-way handshake
  2384. * if msg 3/4 does not set Secure bit. Work around this
  2385. * by setting the Secure bit here even in the case of
  2386. * WPA if the supplicant used it first.
  2387. */
  2388. wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
  2389. "STA used Secure bit in WPA msg 2/4 - "
  2390. "set Secure for 3/4 as workaround");
  2391. secure = 1;
  2392. }
  2393. }
  2394. kde_len = wpa_ie_len + ieee80211w_kde_len(sm);
  2395. if (gtk)
  2396. kde_len += 2 + RSN_SELECTOR_LEN + 2 + gtk_len;
  2397. #ifdef CONFIG_IEEE80211R_AP
  2398. if (wpa_key_mgmt_ft(sm->wpa_key_mgmt)) {
  2399. kde_len += 2 + PMKID_LEN; /* PMKR1Name into RSN IE */
  2400. kde_len += 300; /* FTIE + 2 * TIE */
  2401. }
  2402. #endif /* CONFIG_IEEE80211R_AP */
  2403. #ifdef CONFIG_P2P
  2404. if (WPA_GET_BE32(sm->ip_addr) > 0)
  2405. kde_len += 2 + RSN_SELECTOR_LEN + 3 * 4;
  2406. #endif /* CONFIG_P2P */
  2407. kde = os_malloc(kde_len);
  2408. if (kde == NULL)
  2409. return;
  2410. pos = kde;
  2411. os_memcpy(pos, wpa_ie, wpa_ie_len);
  2412. pos += wpa_ie_len;
  2413. #ifdef CONFIG_IEEE80211R_AP
  2414. if (wpa_key_mgmt_ft(sm->wpa_key_mgmt)) {
  2415. int res;
  2416. size_t elen;
  2417. elen = pos - kde;
  2418. res = wpa_insert_pmkid(kde, &elen, sm->pmk_r1_name);
  2419. if (res < 0) {
  2420. wpa_printf(MSG_ERROR, "FT: Failed to insert "
  2421. "PMKR1Name into RSN IE in EAPOL-Key data");
  2422. os_free(kde);
  2423. return;
  2424. }
  2425. pos -= wpa_ie_len;
  2426. pos += elen;
  2427. }
  2428. #endif /* CONFIG_IEEE80211R_AP */
  2429. if (gtk) {
  2430. u8 hdr[2];
  2431. hdr[0] = keyidx & 0x03;
  2432. hdr[1] = 0;
  2433. pos = wpa_add_kde(pos, RSN_KEY_DATA_GROUPKEY, hdr, 2,
  2434. gtk, gtk_len);
  2435. }
  2436. pos = ieee80211w_kde_add(sm, pos);
  2437. #ifdef CONFIG_IEEE80211R_AP
  2438. if (wpa_key_mgmt_ft(sm->wpa_key_mgmt)) {
  2439. int res;
  2440. struct wpa_auth_config *conf;
  2441. conf = &sm->wpa_auth->conf;
  2442. if (sm->assoc_resp_ftie &&
  2443. kde + kde_len - pos >= 2 + sm->assoc_resp_ftie[1]) {
  2444. os_memcpy(pos, sm->assoc_resp_ftie,
  2445. 2 + sm->assoc_resp_ftie[1]);
  2446. res = 2 + sm->assoc_resp_ftie[1];
  2447. } else {
  2448. res = wpa_write_ftie(conf, conf->r0_key_holder,
  2449. conf->r0_key_holder_len,
  2450. NULL, NULL, pos,
  2451. kde + kde_len - pos,
  2452. NULL, 0);
  2453. }
  2454. if (res < 0) {
  2455. wpa_printf(MSG_ERROR, "FT: Failed to insert FTIE "
  2456. "into EAPOL-Key Key Data");
  2457. os_free(kde);
  2458. return;
  2459. }
  2460. pos += res;
  2461. /* TIE[ReassociationDeadline] (TU) */
  2462. *pos++ = WLAN_EID_TIMEOUT_INTERVAL;
  2463. *pos++ = 5;
  2464. *pos++ = WLAN_TIMEOUT_REASSOC_DEADLINE;
  2465. WPA_PUT_LE32(pos, conf->reassociation_deadline);
  2466. pos += 4;
  2467. /* TIE[KeyLifetime] (seconds) */
  2468. *pos++ = WLAN_EID_TIMEOUT_INTERVAL;
  2469. *pos++ = 5;
  2470. *pos++ = WLAN_TIMEOUT_KEY_LIFETIME;
  2471. WPA_PUT_LE32(pos, conf->r0_key_lifetime * 60);
  2472. pos += 4;
  2473. }
  2474. #endif /* CONFIG_IEEE80211R_AP */
  2475. #ifdef CONFIG_P2P
  2476. if (WPA_GET_BE32(sm->ip_addr) > 0) {
  2477. u8 addr[3 * 4];
  2478. os_memcpy(addr, sm->ip_addr, 4);
  2479. os_memcpy(addr + 4, sm->wpa_auth->conf.ip_addr_mask, 4);
  2480. os_memcpy(addr + 8, sm->wpa_auth->conf.ip_addr_go, 4);
  2481. pos = wpa_add_kde(pos, WFA_KEY_DATA_IP_ADDR_ALLOC,
  2482. addr, sizeof(addr), NULL, 0);
  2483. }
  2484. #endif /* CONFIG_P2P */
  2485. wpa_send_eapol(sm->wpa_auth, sm,
  2486. (secure ? WPA_KEY_INFO_SECURE : 0) |
  2487. (wpa_mic_len(sm->wpa_key_mgmt) ? WPA_KEY_INFO_MIC : 0) |
  2488. WPA_KEY_INFO_ACK | WPA_KEY_INFO_INSTALL |
  2489. WPA_KEY_INFO_KEY_TYPE,
  2490. _rsc, sm->ANonce, kde, pos - kde, keyidx, encr);
  2491. os_free(kde);
  2492. }
  2493. SM_STATE(WPA_PTK, PTKINITDONE)
  2494. {
  2495. SM_ENTRY_MA(WPA_PTK, PTKINITDONE, wpa_ptk);
  2496. sm->EAPOLKeyReceived = FALSE;
  2497. if (sm->Pair) {
  2498. enum wpa_alg alg = wpa_cipher_to_alg(sm->pairwise);
  2499. int klen = wpa_cipher_key_len(sm->pairwise);
  2500. if (wpa_auth_set_key(sm->wpa_auth, 0, alg, sm->addr, 0,
  2501. sm->PTK.tk, klen)) {
  2502. wpa_sta_disconnect(sm->wpa_auth, sm->addr);
  2503. return;
  2504. }
  2505. /* FIX: MLME-SetProtection.Request(TA, Tx_Rx) */
  2506. sm->pairwise_set = TRUE;
  2507. if (sm->wpa_auth->conf.wpa_ptk_rekey) {
  2508. eloop_cancel_timeout(wpa_rekey_ptk, sm->wpa_auth, sm);
  2509. eloop_register_timeout(sm->wpa_auth->conf.
  2510. wpa_ptk_rekey, 0, wpa_rekey_ptk,
  2511. sm->wpa_auth, sm);
  2512. }
  2513. if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt) ||
  2514. sm->wpa_key_mgmt == WPA_KEY_MGMT_OWE) {
  2515. wpa_auth_set_eapol(sm->wpa_auth, sm->addr,
  2516. WPA_EAPOL_authorized, 1);
  2517. }
  2518. }
  2519. if (0 /* IBSS == TRUE */) {
  2520. sm->keycount++;
  2521. if (sm->keycount == 2) {
  2522. wpa_auth_set_eapol(sm->wpa_auth, sm->addr,
  2523. WPA_EAPOL_portValid, 1);
  2524. }
  2525. } else {
  2526. wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portValid,
  2527. 1);
  2528. }
  2529. wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_keyAvailable, 0);
  2530. wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_keyDone, 1);
  2531. if (sm->wpa == WPA_VERSION_WPA)
  2532. sm->PInitAKeys = TRUE;
  2533. else
  2534. sm->has_GTK = TRUE;
  2535. wpa_auth_vlogger(sm->wpa_auth, sm->addr, LOGGER_INFO,
  2536. "pairwise key handshake completed (%s)",
  2537. sm->wpa == WPA_VERSION_WPA ? "WPA" : "RSN");
  2538. #ifdef CONFIG_IEEE80211R_AP
  2539. wpa_ft_push_pmk_r1(sm->wpa_auth, sm->addr);
  2540. #endif /* CONFIG_IEEE80211R_AP */
  2541. }
  2542. SM_STEP(WPA_PTK)
  2543. {
  2544. struct wpa_authenticator *wpa_auth = sm->wpa_auth;
  2545. if (sm->Init)
  2546. SM_ENTER(WPA_PTK, INITIALIZE);
  2547. else if (sm->Disconnect
  2548. /* || FIX: dot11RSNAConfigSALifetime timeout */) {
  2549. wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG,
  2550. "WPA_PTK: sm->Disconnect");
  2551. SM_ENTER(WPA_PTK, DISCONNECT);
  2552. }
  2553. else if (sm->DeauthenticationRequest)
  2554. SM_ENTER(WPA_PTK, DISCONNECTED);
  2555. else if (sm->AuthenticationRequest)
  2556. SM_ENTER(WPA_PTK, AUTHENTICATION);
  2557. else if (sm->ReAuthenticationRequest)
  2558. SM_ENTER(WPA_PTK, AUTHENTICATION2);
  2559. else if (sm->PTKRequest)
  2560. SM_ENTER(WPA_PTK, PTKSTART);
  2561. else switch (sm->wpa_ptk_state) {
  2562. case WPA_PTK_INITIALIZE:
  2563. break;
  2564. case WPA_PTK_DISCONNECT:
  2565. SM_ENTER(WPA_PTK, DISCONNECTED);
  2566. break;
  2567. case WPA_PTK_DISCONNECTED:
  2568. SM_ENTER(WPA_PTK, INITIALIZE);
  2569. break;
  2570. case WPA_PTK_AUTHENTICATION:
  2571. SM_ENTER(WPA_PTK, AUTHENTICATION2);
  2572. break;
  2573. case WPA_PTK_AUTHENTICATION2:
  2574. if (wpa_key_mgmt_wpa_ieee8021x(sm->wpa_key_mgmt) &&
  2575. wpa_auth_get_eapol(sm->wpa_auth, sm->addr,
  2576. WPA_EAPOL_keyRun) > 0)
  2577. SM_ENTER(WPA_PTK, INITPMK);
  2578. else if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt) ||
  2579. sm->wpa_key_mgmt == WPA_KEY_MGMT_OWE
  2580. /* FIX: && 802.1X::keyRun */)
  2581. SM_ENTER(WPA_PTK, INITPSK);
  2582. break;
  2583. case WPA_PTK_INITPMK:
  2584. if (wpa_auth_get_eapol(sm->wpa_auth, sm->addr,
  2585. WPA_EAPOL_keyAvailable) > 0)
  2586. SM_ENTER(WPA_PTK, PTKSTART);
  2587. else {
  2588. wpa_auth->dot11RSNA4WayHandshakeFailures++;
  2589. wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_INFO,
  2590. "INITPMK - keyAvailable = false");
  2591. SM_ENTER(WPA_PTK, DISCONNECT);
  2592. }
  2593. break;
  2594. case WPA_PTK_INITPSK:
  2595. if (wpa_auth_get_psk(sm->wpa_auth, sm->addr, sm->p2p_dev_addr,
  2596. NULL))
  2597. SM_ENTER(WPA_PTK, PTKSTART);
  2598. else {
  2599. wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_INFO,
  2600. "no PSK configured for the STA");
  2601. wpa_auth->dot11RSNA4WayHandshakeFailures++;
  2602. SM_ENTER(WPA_PTK, DISCONNECT);
  2603. }
  2604. break;
  2605. case WPA_PTK_PTKSTART:
  2606. if (sm->EAPOLKeyReceived && !sm->EAPOLKeyRequest &&
  2607. sm->EAPOLKeyPairwise)
  2608. SM_ENTER(WPA_PTK, PTKCALCNEGOTIATING);
  2609. else if (sm->TimeoutCtr >
  2610. sm->wpa_auth->conf.wpa_pairwise_update_count) {
  2611. wpa_auth->dot11RSNA4WayHandshakeFailures++;
  2612. wpa_auth_vlogger(
  2613. sm->wpa_auth, sm->addr, LOGGER_DEBUG,
  2614. "PTKSTART: Retry limit %u reached",
  2615. sm->wpa_auth->conf.wpa_pairwise_update_count);
  2616. SM_ENTER(WPA_PTK, DISCONNECT);
  2617. } else if (sm->TimeoutEvt)
  2618. SM_ENTER(WPA_PTK, PTKSTART);
  2619. break;
  2620. case WPA_PTK_PTKCALCNEGOTIATING:
  2621. if (sm->MICVerified)
  2622. SM_ENTER(WPA_PTK, PTKCALCNEGOTIATING2);
  2623. else if (sm->EAPOLKeyReceived && !sm->EAPOLKeyRequest &&
  2624. sm->EAPOLKeyPairwise)
  2625. SM_ENTER(WPA_PTK, PTKCALCNEGOTIATING);
  2626. else if (sm->TimeoutEvt)
  2627. SM_ENTER(WPA_PTK, PTKSTART);
  2628. break;
  2629. case WPA_PTK_PTKCALCNEGOTIATING2:
  2630. SM_ENTER(WPA_PTK, PTKINITNEGOTIATING);
  2631. break;
  2632. case WPA_PTK_PTKINITNEGOTIATING:
  2633. if (sm->update_snonce)
  2634. SM_ENTER(WPA_PTK, PTKCALCNEGOTIATING);
  2635. else if (sm->EAPOLKeyReceived && !sm->EAPOLKeyRequest &&
  2636. sm->EAPOLKeyPairwise && sm->MICVerified)
  2637. SM_ENTER(WPA_PTK, PTKINITDONE);
  2638. else if (sm->TimeoutCtr >
  2639. sm->wpa_auth->conf.wpa_pairwise_update_count) {
  2640. wpa_auth->dot11RSNA4WayHandshakeFailures++;
  2641. wpa_auth_vlogger(
  2642. sm->wpa_auth, sm->addr, LOGGER_DEBUG,
  2643. "PTKINITNEGOTIATING: Retry limit %u reached",
  2644. sm->wpa_auth->conf.wpa_pairwise_update_count);
  2645. SM_ENTER(WPA_PTK, DISCONNECT);
  2646. } else if (sm->TimeoutEvt)
  2647. SM_ENTER(WPA_PTK, PTKINITNEGOTIATING);
  2648. break;
  2649. case WPA_PTK_PTKINITDONE:
  2650. break;
  2651. }
  2652. }
  2653. SM_STATE(WPA_PTK_GROUP, IDLE)
  2654. {
  2655. SM_ENTRY_MA(WPA_PTK_GROUP, IDLE, wpa_ptk_group);
  2656. if (sm->Init) {
  2657. /* Init flag is not cleared here, so avoid busy
  2658. * loop by claiming nothing changed. */
  2659. sm->changed = FALSE;
  2660. }
  2661. sm->GTimeoutCtr = 0;
  2662. }
  2663. SM_STATE(WPA_PTK_GROUP, REKEYNEGOTIATING)
  2664. {
  2665. u8 rsc[WPA_KEY_RSC_LEN];
  2666. struct wpa_group *gsm = sm->group;
  2667. const u8 *kde;
  2668. u8 *kde_buf = NULL, *pos, hdr[2];
  2669. size_t kde_len;
  2670. u8 *gtk, dummy_gtk[32];
  2671. SM_ENTRY_MA(WPA_PTK_GROUP, REKEYNEGOTIATING, wpa_ptk_group);
  2672. sm->GTimeoutCtr++;
  2673. if (sm->GTimeoutCtr > sm->wpa_auth->conf.wpa_group_update_count) {
  2674. /* No point in sending the EAPOL-Key - we will disconnect
  2675. * immediately following this. */
  2676. return;
  2677. }
  2678. if (sm->wpa == WPA_VERSION_WPA)
  2679. sm->PInitAKeys = FALSE;
  2680. sm->TimeoutEvt = FALSE;
  2681. /* Send EAPOL(1, 1, 1, !Pair, G, RSC, GNonce, MIC(PTK), GTK[GN]) */
  2682. os_memset(rsc, 0, WPA_KEY_RSC_LEN);
  2683. if (gsm->wpa_group_state == WPA_GROUP_SETKEYSDONE)
  2684. wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN, rsc);
  2685. wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
  2686. "sending 1/2 msg of Group Key Handshake");
  2687. gtk = gsm->GTK[gsm->GN - 1];
  2688. if (sm->wpa_auth->conf.disable_gtk) {
  2689. /*
  2690. * Provide unique random GTK to each STA to prevent use
  2691. * of GTK in the BSS.
  2692. */
  2693. if (random_get_bytes(dummy_gtk, gsm->GTK_len) < 0)
  2694. return;
  2695. gtk = dummy_gtk;
  2696. }
  2697. if (sm->wpa == WPA_VERSION_WPA2) {
  2698. kde_len = 2 + RSN_SELECTOR_LEN + 2 + gsm->GTK_len +
  2699. ieee80211w_kde_len(sm);
  2700. kde_buf = os_malloc(kde_len);
  2701. if (kde_buf == NULL)
  2702. return;
  2703. kde = pos = kde_buf;
  2704. hdr[0] = gsm->GN & 0x03;
  2705. hdr[1] = 0;
  2706. pos = wpa_add_kde(pos, RSN_KEY_DATA_GROUPKEY, hdr, 2,
  2707. gtk, gsm->GTK_len);
  2708. pos = ieee80211w_kde_add(sm, pos);
  2709. kde_len = pos - kde;
  2710. } else {
  2711. kde = gtk;
  2712. kde_len = gsm->GTK_len;
  2713. }
  2714. wpa_send_eapol(sm->wpa_auth, sm,
  2715. WPA_KEY_INFO_SECURE |
  2716. (wpa_mic_len(sm->wpa_key_mgmt) ? WPA_KEY_INFO_MIC : 0) |
  2717. WPA_KEY_INFO_ACK |
  2718. (!sm->Pair ? WPA_KEY_INFO_INSTALL : 0),
  2719. rsc, NULL, kde, kde_len, gsm->GN, 1);
  2720. os_free(kde_buf);
  2721. }
  2722. SM_STATE(WPA_PTK_GROUP, REKEYESTABLISHED)
  2723. {
  2724. SM_ENTRY_MA(WPA_PTK_GROUP, REKEYESTABLISHED, wpa_ptk_group);
  2725. sm->EAPOLKeyReceived = FALSE;
  2726. if (sm->GUpdateStationKeys)
  2727. sm->group->GKeyDoneStations--;
  2728. sm->GUpdateStationKeys = FALSE;
  2729. sm->GTimeoutCtr = 0;
  2730. /* FIX: MLME.SetProtection.Request(TA, Tx_Rx) */
  2731. wpa_auth_vlogger(sm->wpa_auth, sm->addr, LOGGER_INFO,
  2732. "group key handshake completed (%s)",
  2733. sm->wpa == WPA_VERSION_WPA ? "WPA" : "RSN");
  2734. sm->has_GTK = TRUE;
  2735. }
  2736. SM_STATE(WPA_PTK_GROUP, KEYERROR)
  2737. {
  2738. SM_ENTRY_MA(WPA_PTK_GROUP, KEYERROR, wpa_ptk_group);
  2739. if (sm->GUpdateStationKeys)
  2740. sm->group->GKeyDoneStations--;
  2741. sm->GUpdateStationKeys = FALSE;
  2742. sm->Disconnect = TRUE;
  2743. wpa_auth_vlogger(sm->wpa_auth, sm->addr, LOGGER_INFO,
  2744. "group key handshake failed (%s) after %u tries",
  2745. sm->wpa == WPA_VERSION_WPA ? "WPA" : "RSN",
  2746. sm->wpa_auth->conf.wpa_group_update_count);
  2747. }
  2748. SM_STEP(WPA_PTK_GROUP)
  2749. {
  2750. if (sm->Init || sm->PtkGroupInit) {
  2751. SM_ENTER(WPA_PTK_GROUP, IDLE);
  2752. sm->PtkGroupInit = FALSE;
  2753. } else switch (sm->wpa_ptk_group_state) {
  2754. case WPA_PTK_GROUP_IDLE:
  2755. if (sm->GUpdateStationKeys ||
  2756. (sm->wpa == WPA_VERSION_WPA && sm->PInitAKeys))
  2757. SM_ENTER(WPA_PTK_GROUP, REKEYNEGOTIATING);
  2758. break;
  2759. case WPA_PTK_GROUP_REKEYNEGOTIATING:
  2760. if (sm->EAPOLKeyReceived && !sm->EAPOLKeyRequest &&
  2761. !sm->EAPOLKeyPairwise && sm->MICVerified)
  2762. SM_ENTER(WPA_PTK_GROUP, REKEYESTABLISHED);
  2763. else if (sm->GTimeoutCtr >
  2764. sm->wpa_auth->conf.wpa_group_update_count)
  2765. SM_ENTER(WPA_PTK_GROUP, KEYERROR);
  2766. else if (sm->TimeoutEvt)
  2767. SM_ENTER(WPA_PTK_GROUP, REKEYNEGOTIATING);
  2768. break;
  2769. case WPA_PTK_GROUP_KEYERROR:
  2770. SM_ENTER(WPA_PTK_GROUP, IDLE);
  2771. break;
  2772. case WPA_PTK_GROUP_REKEYESTABLISHED:
  2773. SM_ENTER(WPA_PTK_GROUP, IDLE);
  2774. break;
  2775. }
  2776. }
  2777. static int wpa_gtk_update(struct wpa_authenticator *wpa_auth,
  2778. struct wpa_group *group)
  2779. {
  2780. int ret = 0;
  2781. os_memcpy(group->GNonce, group->Counter, WPA_NONCE_LEN);
  2782. inc_byte_array(group->Counter, WPA_NONCE_LEN);
  2783. if (wpa_gmk_to_gtk(group->GMK, "Group key expansion",
  2784. wpa_auth->addr, group->GNonce,
  2785. group->GTK[group->GN - 1], group->GTK_len) < 0)
  2786. ret = -1;
  2787. wpa_hexdump_key(MSG_DEBUG, "GTK",
  2788. group->GTK[group->GN - 1], group->GTK_len);
  2789. #ifdef CONFIG_IEEE80211W
  2790. if (wpa_auth->conf.ieee80211w != NO_MGMT_FRAME_PROTECTION) {
  2791. size_t len;
  2792. len = wpa_cipher_key_len(wpa_auth->conf.group_mgmt_cipher);
  2793. os_memcpy(group->GNonce, group->Counter, WPA_NONCE_LEN);
  2794. inc_byte_array(group->Counter, WPA_NONCE_LEN);
  2795. if (wpa_gmk_to_gtk(group->GMK, "IGTK key expansion",
  2796. wpa_auth->addr, group->GNonce,
  2797. group->IGTK[group->GN_igtk - 4], len) < 0)
  2798. ret = -1;
  2799. wpa_hexdump_key(MSG_DEBUG, "IGTK",
  2800. group->IGTK[group->GN_igtk - 4], len);
  2801. }
  2802. #endif /* CONFIG_IEEE80211W */
  2803. return ret;
  2804. }
  2805. static void wpa_group_gtk_init(struct wpa_authenticator *wpa_auth,
  2806. struct wpa_group *group)
  2807. {
  2808. wpa_printf(MSG_DEBUG, "WPA: group state machine entering state "
  2809. "GTK_INIT (VLAN-ID %d)", group->vlan_id);
  2810. group->changed = FALSE; /* GInit is not cleared here; avoid loop */
  2811. group->wpa_group_state = WPA_GROUP_GTK_INIT;
  2812. /* GTK[0..N] = 0 */
  2813. os_memset(group->GTK, 0, sizeof(group->GTK));
  2814. group->GN = 1;
  2815. group->GM = 2;
  2816. #ifdef CONFIG_IEEE80211W
  2817. group->GN_igtk = 4;
  2818. group->GM_igtk = 5;
  2819. #endif /* CONFIG_IEEE80211W */
  2820. /* GTK[GN] = CalcGTK() */
  2821. wpa_gtk_update(wpa_auth, group);
  2822. }
  2823. static int wpa_group_update_sta(struct wpa_state_machine *sm, void *ctx)
  2824. {
  2825. if (ctx != NULL && ctx != sm->group)
  2826. return 0;
  2827. if (sm->wpa_ptk_state != WPA_PTK_PTKINITDONE) {
  2828. wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
  2829. "Not in PTKINITDONE; skip Group Key update");
  2830. sm->GUpdateStationKeys = FALSE;
  2831. return 0;
  2832. }
  2833. if (sm->GUpdateStationKeys) {
  2834. /*
  2835. * This should not really happen, so add a debug log entry.
  2836. * Since we clear the GKeyDoneStations before the loop, the
  2837. * station needs to be counted here anyway.
  2838. */
  2839. wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
  2840. "GUpdateStationKeys was already set when "
  2841. "marking station for GTK rekeying");
  2842. }
  2843. /* Do not rekey GTK/IGTK when STA is in WNM-Sleep Mode */
  2844. if (sm->is_wnmsleep)
  2845. return 0;
  2846. sm->group->GKeyDoneStations++;
  2847. sm->GUpdateStationKeys = TRUE;
  2848. wpa_sm_step(sm);
  2849. return 0;
  2850. }
  2851. #ifdef CONFIG_WNM
  2852. /* update GTK when exiting WNM-Sleep Mode */
  2853. void wpa_wnmsleep_rekey_gtk(struct wpa_state_machine *sm)
  2854. {
  2855. if (sm == NULL || sm->is_wnmsleep)
  2856. return;
  2857. wpa_group_update_sta(sm, NULL);
  2858. }
  2859. void wpa_set_wnmsleep(struct wpa_state_machine *sm, int flag)
  2860. {
  2861. if (sm)
  2862. sm->is_wnmsleep = !!flag;
  2863. }
  2864. int wpa_wnmsleep_gtk_subelem(struct wpa_state_machine *sm, u8 *pos)
  2865. {
  2866. struct wpa_group *gsm = sm->group;
  2867. u8 *start = pos;
  2868. /*
  2869. * GTK subelement:
  2870. * Sub-elem ID[1] | Length[1] | Key Info[2] | Key Length[1] | RSC[8] |
  2871. * Key[5..32]
  2872. */
  2873. *pos++ = WNM_SLEEP_SUBELEM_GTK;
  2874. *pos++ = 11 + gsm->GTK_len;
  2875. /* Key ID in B0-B1 of Key Info */
  2876. WPA_PUT_LE16(pos, gsm->GN & 0x03);
  2877. pos += 2;
  2878. *pos++ = gsm->GTK_len;
  2879. if (wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN, pos) != 0)
  2880. return 0;
  2881. pos += 8;
  2882. os_memcpy(pos, gsm->GTK[gsm->GN - 1], gsm->GTK_len);
  2883. pos += gsm->GTK_len;
  2884. wpa_printf(MSG_DEBUG, "WNM: GTK Key ID %u in WNM-Sleep Mode exit",
  2885. gsm->GN);
  2886. wpa_hexdump_key(MSG_DEBUG, "WNM: GTK in WNM-Sleep Mode exit",
  2887. gsm->GTK[gsm->GN - 1], gsm->GTK_len);
  2888. return pos - start;
  2889. }
  2890. #ifdef CONFIG_IEEE80211W
  2891. int wpa_wnmsleep_igtk_subelem(struct wpa_state_machine *sm, u8 *pos)
  2892. {
  2893. struct wpa_group *gsm = sm->group;
  2894. u8 *start = pos;
  2895. size_t len = wpa_cipher_key_len(sm->wpa_auth->conf.group_mgmt_cipher);
  2896. /*
  2897. * IGTK subelement:
  2898. * Sub-elem ID[1] | Length[1] | KeyID[2] | PN[6] | Key[16]
  2899. */
  2900. *pos++ = WNM_SLEEP_SUBELEM_IGTK;
  2901. *pos++ = 2 + 6 + len;
  2902. WPA_PUT_LE16(pos, gsm->GN_igtk);
  2903. pos += 2;
  2904. if (wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN_igtk, pos) != 0)
  2905. return 0;
  2906. pos += 6;
  2907. os_memcpy(pos, gsm->IGTK[gsm->GN_igtk - 4], len);
  2908. pos += len;
  2909. wpa_printf(MSG_DEBUG, "WNM: IGTK Key ID %u in WNM-Sleep Mode exit",
  2910. gsm->GN_igtk);
  2911. wpa_hexdump_key(MSG_DEBUG, "WNM: IGTK in WNM-Sleep Mode exit",
  2912. gsm->IGTK[gsm->GN_igtk - 4], len);
  2913. return pos - start;
  2914. }
  2915. #endif /* CONFIG_IEEE80211W */
  2916. #endif /* CONFIG_WNM */
  2917. static void wpa_group_setkeys(struct wpa_authenticator *wpa_auth,
  2918. struct wpa_group *group)
  2919. {
  2920. int tmp;
  2921. wpa_printf(MSG_DEBUG, "WPA: group state machine entering state "
  2922. "SETKEYS (VLAN-ID %d)", group->vlan_id);
  2923. group->changed = TRUE;
  2924. group->wpa_group_state = WPA_GROUP_SETKEYS;
  2925. group->GTKReKey = FALSE;
  2926. tmp = group->GM;
  2927. group->GM = group->GN;
  2928. group->GN = tmp;
  2929. #ifdef CONFIG_IEEE80211W
  2930. tmp = group->GM_igtk;
  2931. group->GM_igtk = group->GN_igtk;
  2932. group->GN_igtk = tmp;
  2933. #endif /* CONFIG_IEEE80211W */
  2934. /* "GKeyDoneStations = GNoStations" is done in more robust way by
  2935. * counting the STAs that are marked with GUpdateStationKeys instead of
  2936. * including all STAs that could be in not-yet-completed state. */
  2937. wpa_gtk_update(wpa_auth, group);
  2938. if (group->GKeyDoneStations) {
  2939. wpa_printf(MSG_DEBUG, "wpa_group_setkeys: Unexpected "
  2940. "GKeyDoneStations=%d when starting new GTK rekey",
  2941. group->GKeyDoneStations);
  2942. group->GKeyDoneStations = 0;
  2943. }
  2944. wpa_auth_for_each_sta(wpa_auth, wpa_group_update_sta, group);
  2945. wpa_printf(MSG_DEBUG, "wpa_group_setkeys: GKeyDoneStations=%d",
  2946. group->GKeyDoneStations);
  2947. }
  2948. static int wpa_group_config_group_keys(struct wpa_authenticator *wpa_auth,
  2949. struct wpa_group *group)
  2950. {
  2951. int ret = 0;
  2952. if (wpa_auth_set_key(wpa_auth, group->vlan_id,
  2953. wpa_cipher_to_alg(wpa_auth->conf.wpa_group),
  2954. broadcast_ether_addr, group->GN,
  2955. group->GTK[group->GN - 1], group->GTK_len) < 0)
  2956. ret = -1;
  2957. #ifdef CONFIG_IEEE80211W
  2958. if (wpa_auth->conf.ieee80211w != NO_MGMT_FRAME_PROTECTION) {
  2959. enum wpa_alg alg;
  2960. size_t len;
  2961. alg = wpa_cipher_to_alg(wpa_auth->conf.group_mgmt_cipher);
  2962. len = wpa_cipher_key_len(wpa_auth->conf.group_mgmt_cipher);
  2963. if (ret == 0 &&
  2964. wpa_auth_set_key(wpa_auth, group->vlan_id, alg,
  2965. broadcast_ether_addr, group->GN_igtk,
  2966. group->IGTK[group->GN_igtk - 4], len) < 0)
  2967. ret = -1;
  2968. }
  2969. #endif /* CONFIG_IEEE80211W */
  2970. return ret;
  2971. }
  2972. static int wpa_group_disconnect_cb(struct wpa_state_machine *sm, void *ctx)
  2973. {
  2974. if (sm->group == ctx) {
  2975. wpa_printf(MSG_DEBUG, "WPA: Mark STA " MACSTR
  2976. " for discconnection due to fatal failure",
  2977. MAC2STR(sm->addr));
  2978. sm->Disconnect = TRUE;
  2979. }
  2980. return 0;
  2981. }
  2982. static void wpa_group_fatal_failure(struct wpa_authenticator *wpa_auth,
  2983. struct wpa_group *group)
  2984. {
  2985. wpa_printf(MSG_DEBUG, "WPA: group state machine entering state FATAL_FAILURE");
  2986. group->changed = TRUE;
  2987. group->wpa_group_state = WPA_GROUP_FATAL_FAILURE;
  2988. wpa_auth_for_each_sta(wpa_auth, wpa_group_disconnect_cb, group);
  2989. }
  2990. static int wpa_group_setkeysdone(struct wpa_authenticator *wpa_auth,
  2991. struct wpa_group *group)
  2992. {
  2993. wpa_printf(MSG_DEBUG, "WPA: group state machine entering state "
  2994. "SETKEYSDONE (VLAN-ID %d)", group->vlan_id);
  2995. group->changed = TRUE;
  2996. group->wpa_group_state = WPA_GROUP_SETKEYSDONE;
  2997. if (wpa_group_config_group_keys(wpa_auth, group) < 0) {
  2998. wpa_group_fatal_failure(wpa_auth, group);
  2999. return -1;
  3000. }
  3001. return 0;
  3002. }
  3003. static void wpa_group_sm_step(struct wpa_authenticator *wpa_auth,
  3004. struct wpa_group *group)
  3005. {
  3006. if (group->GInit) {
  3007. wpa_group_gtk_init(wpa_auth, group);
  3008. } else if (group->wpa_group_state == WPA_GROUP_FATAL_FAILURE) {
  3009. /* Do not allow group operations */
  3010. } else if (group->wpa_group_state == WPA_GROUP_GTK_INIT &&
  3011. group->GTKAuthenticator) {
  3012. wpa_group_setkeysdone(wpa_auth, group);
  3013. } else if (group->wpa_group_state == WPA_GROUP_SETKEYSDONE &&
  3014. group->GTKReKey) {
  3015. wpa_group_setkeys(wpa_auth, group);
  3016. } else if (group->wpa_group_state == WPA_GROUP_SETKEYS) {
  3017. if (group->GKeyDoneStations == 0)
  3018. wpa_group_setkeysdone(wpa_auth, group);
  3019. else if (group->GTKReKey)
  3020. wpa_group_setkeys(wpa_auth, group);
  3021. }
  3022. }
  3023. static int wpa_sm_step(struct wpa_state_machine *sm)
  3024. {
  3025. if (sm == NULL)
  3026. return 0;
  3027. if (sm->in_step_loop) {
  3028. /* This should not happen, but if it does, make sure we do not
  3029. * end up freeing the state machine too early by exiting the
  3030. * recursive call. */
  3031. wpa_printf(MSG_ERROR, "WPA: wpa_sm_step() called recursively");
  3032. return 0;
  3033. }
  3034. sm->in_step_loop = 1;
  3035. do {
  3036. if (sm->pending_deinit)
  3037. break;
  3038. sm->changed = FALSE;
  3039. sm->wpa_auth->group->changed = FALSE;
  3040. SM_STEP_RUN(WPA_PTK);
  3041. if (sm->pending_deinit)
  3042. break;
  3043. SM_STEP_RUN(WPA_PTK_GROUP);
  3044. if (sm->pending_deinit)
  3045. break;
  3046. wpa_group_sm_step(sm->wpa_auth, sm->group);
  3047. } while (sm->changed || sm->wpa_auth->group->changed);
  3048. sm->in_step_loop = 0;
  3049. if (sm->pending_deinit) {
  3050. wpa_printf(MSG_DEBUG, "WPA: Completing pending STA state "
  3051. "machine deinit for " MACSTR, MAC2STR(sm->addr));
  3052. wpa_free_sta_sm(sm);
  3053. return 1;
  3054. }
  3055. return 0;
  3056. }
  3057. static void wpa_sm_call_step(void *eloop_ctx, void *timeout_ctx)
  3058. {
  3059. struct wpa_state_machine *sm = eloop_ctx;
  3060. wpa_sm_step(sm);
  3061. }
  3062. void wpa_auth_sm_notify(struct wpa_state_machine *sm)
  3063. {
  3064. if (sm == NULL)
  3065. return;
  3066. eloop_register_timeout(0, 0, wpa_sm_call_step, sm, NULL);
  3067. }
  3068. void wpa_gtk_rekey(struct wpa_authenticator *wpa_auth)
  3069. {
  3070. int tmp, i;
  3071. struct wpa_group *group;
  3072. if (wpa_auth == NULL)
  3073. return;
  3074. group = wpa_auth->group;
  3075. for (i = 0; i < 2; i++) {
  3076. tmp = group->GM;
  3077. group->GM = group->GN;
  3078. group->GN = tmp;
  3079. #ifdef CONFIG_IEEE80211W
  3080. tmp = group->GM_igtk;
  3081. group->GM_igtk = group->GN_igtk;
  3082. group->GN_igtk = tmp;
  3083. #endif /* CONFIG_IEEE80211W */
  3084. wpa_gtk_update(wpa_auth, group);
  3085. wpa_group_config_group_keys(wpa_auth, group);
  3086. }
  3087. }
  3088. static const char * wpa_bool_txt(int val)
  3089. {
  3090. return val ? "TRUE" : "FALSE";
  3091. }
  3092. #define RSN_SUITE "%02x-%02x-%02x-%d"
  3093. #define RSN_SUITE_ARG(s) \
  3094. ((s) >> 24) & 0xff, ((s) >> 16) & 0xff, ((s) >> 8) & 0xff, (s) & 0xff
  3095. int wpa_get_mib(struct wpa_authenticator *wpa_auth, char *buf, size_t buflen)
  3096. {
  3097. int len = 0, ret;
  3098. char pmkid_txt[PMKID_LEN * 2 + 1];
  3099. #ifdef CONFIG_RSN_PREAUTH
  3100. const int preauth = 1;
  3101. #else /* CONFIG_RSN_PREAUTH */
  3102. const int preauth = 0;
  3103. #endif /* CONFIG_RSN_PREAUTH */
  3104. if (wpa_auth == NULL)
  3105. return len;
  3106. ret = os_snprintf(buf + len, buflen - len,
  3107. "dot11RSNAOptionImplemented=TRUE\n"
  3108. "dot11RSNAPreauthenticationImplemented=%s\n"
  3109. "dot11RSNAEnabled=%s\n"
  3110. "dot11RSNAPreauthenticationEnabled=%s\n",
  3111. wpa_bool_txt(preauth),
  3112. wpa_bool_txt(wpa_auth->conf.wpa & WPA_PROTO_RSN),
  3113. wpa_bool_txt(wpa_auth->conf.rsn_preauth));
  3114. if (os_snprintf_error(buflen - len, ret))
  3115. return len;
  3116. len += ret;
  3117. wpa_snprintf_hex(pmkid_txt, sizeof(pmkid_txt),
  3118. wpa_auth->dot11RSNAPMKIDUsed, PMKID_LEN);
  3119. ret = os_snprintf(
  3120. buf + len, buflen - len,
  3121. "dot11RSNAConfigVersion=%u\n"
  3122. "dot11RSNAConfigPairwiseKeysSupported=9999\n"
  3123. /* FIX: dot11RSNAConfigGroupCipher */
  3124. /* FIX: dot11RSNAConfigGroupRekeyMethod */
  3125. /* FIX: dot11RSNAConfigGroupRekeyTime */
  3126. /* FIX: dot11RSNAConfigGroupRekeyPackets */
  3127. "dot11RSNAConfigGroupRekeyStrict=%u\n"
  3128. "dot11RSNAConfigGroupUpdateCount=%u\n"
  3129. "dot11RSNAConfigPairwiseUpdateCount=%u\n"
  3130. "dot11RSNAConfigGroupCipherSize=%u\n"
  3131. "dot11RSNAConfigPMKLifetime=%u\n"
  3132. "dot11RSNAConfigPMKReauthThreshold=%u\n"
  3133. "dot11RSNAConfigNumberOfPTKSAReplayCounters=0\n"
  3134. "dot11RSNAConfigSATimeout=%u\n"
  3135. "dot11RSNAAuthenticationSuiteSelected=" RSN_SUITE "\n"
  3136. "dot11RSNAPairwiseCipherSelected=" RSN_SUITE "\n"
  3137. "dot11RSNAGroupCipherSelected=" RSN_SUITE "\n"
  3138. "dot11RSNAPMKIDUsed=%s\n"
  3139. "dot11RSNAAuthenticationSuiteRequested=" RSN_SUITE "\n"
  3140. "dot11RSNAPairwiseCipherRequested=" RSN_SUITE "\n"
  3141. "dot11RSNAGroupCipherRequested=" RSN_SUITE "\n"
  3142. "dot11RSNATKIPCounterMeasuresInvoked=%u\n"
  3143. "dot11RSNA4WayHandshakeFailures=%u\n"
  3144. "dot11RSNAConfigNumberOfGTKSAReplayCounters=0\n",
  3145. RSN_VERSION,
  3146. !!wpa_auth->conf.wpa_strict_rekey,
  3147. wpa_auth->conf.wpa_group_update_count,
  3148. wpa_auth->conf.wpa_pairwise_update_count,
  3149. wpa_cipher_key_len(wpa_auth->conf.wpa_group) * 8,
  3150. dot11RSNAConfigPMKLifetime,
  3151. dot11RSNAConfigPMKReauthThreshold,
  3152. dot11RSNAConfigSATimeout,
  3153. RSN_SUITE_ARG(wpa_auth->dot11RSNAAuthenticationSuiteSelected),
  3154. RSN_SUITE_ARG(wpa_auth->dot11RSNAPairwiseCipherSelected),
  3155. RSN_SUITE_ARG(wpa_auth->dot11RSNAGroupCipherSelected),
  3156. pmkid_txt,
  3157. RSN_SUITE_ARG(wpa_auth->dot11RSNAAuthenticationSuiteRequested),
  3158. RSN_SUITE_ARG(wpa_auth->dot11RSNAPairwiseCipherRequested),
  3159. RSN_SUITE_ARG(wpa_auth->dot11RSNAGroupCipherRequested),
  3160. wpa_auth->dot11RSNATKIPCounterMeasuresInvoked,
  3161. wpa_auth->dot11RSNA4WayHandshakeFailures);
  3162. if (os_snprintf_error(buflen - len, ret))
  3163. return len;
  3164. len += ret;
  3165. /* TODO: dot11RSNAConfigPairwiseCiphersTable */
  3166. /* TODO: dot11RSNAConfigAuthenticationSuitesTable */
  3167. /* Private MIB */
  3168. ret = os_snprintf(buf + len, buflen - len, "hostapdWPAGroupState=%d\n",
  3169. wpa_auth->group->wpa_group_state);
  3170. if (os_snprintf_error(buflen - len, ret))
  3171. return len;
  3172. len += ret;
  3173. return len;
  3174. }
  3175. int wpa_get_mib_sta(struct wpa_state_machine *sm, char *buf, size_t buflen)
  3176. {
  3177. int len = 0, ret;
  3178. u32 pairwise = 0;
  3179. if (sm == NULL)
  3180. return 0;
  3181. /* TODO: FF-FF-FF-FF-FF-FF entry for broadcast/multicast stats */
  3182. /* dot11RSNAStatsEntry */
  3183. pairwise = wpa_cipher_to_suite(sm->wpa == WPA_VERSION_WPA2 ?
  3184. WPA_PROTO_RSN : WPA_PROTO_WPA,
  3185. sm->pairwise);
  3186. if (pairwise == 0)
  3187. return 0;
  3188. ret = os_snprintf(
  3189. buf + len, buflen - len,
  3190. /* TODO: dot11RSNAStatsIndex */
  3191. "dot11RSNAStatsSTAAddress=" MACSTR "\n"
  3192. "dot11RSNAStatsVersion=1\n"
  3193. "dot11RSNAStatsSelectedPairwiseCipher=" RSN_SUITE "\n"
  3194. /* TODO: dot11RSNAStatsTKIPICVErrors */
  3195. "dot11RSNAStatsTKIPLocalMICFailures=%u\n"
  3196. "dot11RSNAStatsTKIPRemoteMICFailures=%u\n"
  3197. /* TODO: dot11RSNAStatsCCMPReplays */
  3198. /* TODO: dot11RSNAStatsCCMPDecryptErrors */
  3199. /* TODO: dot11RSNAStatsTKIPReplays */,
  3200. MAC2STR(sm->addr),
  3201. RSN_SUITE_ARG(pairwise),
  3202. sm->dot11RSNAStatsTKIPLocalMICFailures,
  3203. sm->dot11RSNAStatsTKIPRemoteMICFailures);
  3204. if (os_snprintf_error(buflen - len, ret))
  3205. return len;
  3206. len += ret;
  3207. /* Private MIB */
  3208. ret = os_snprintf(buf + len, buflen - len,
  3209. "hostapdWPAPTKState=%d\n"
  3210. "hostapdWPAPTKGroupState=%d\n",
  3211. sm->wpa_ptk_state,
  3212. sm->wpa_ptk_group_state);
  3213. if (os_snprintf_error(buflen - len, ret))
  3214. return len;
  3215. len += ret;
  3216. return len;
  3217. }
  3218. void wpa_auth_countermeasures_start(struct wpa_authenticator *wpa_auth)
  3219. {
  3220. if (wpa_auth)
  3221. wpa_auth->dot11RSNATKIPCounterMeasuresInvoked++;
  3222. }
  3223. int wpa_auth_pairwise_set(struct wpa_state_machine *sm)
  3224. {
  3225. return sm && sm->pairwise_set;
  3226. }
  3227. int wpa_auth_get_pairwise(struct wpa_state_machine *sm)
  3228. {
  3229. return sm->pairwise;
  3230. }
  3231. int wpa_auth_sta_key_mgmt(struct wpa_state_machine *sm)
  3232. {
  3233. if (sm == NULL)
  3234. return -1;
  3235. return sm->wpa_key_mgmt;
  3236. }
  3237. int wpa_auth_sta_wpa_version(struct wpa_state_machine *sm)
  3238. {
  3239. if (sm == NULL)
  3240. return 0;
  3241. return sm->wpa;
  3242. }
  3243. int wpa_auth_sta_clear_pmksa(struct wpa_state_machine *sm,
  3244. struct rsn_pmksa_cache_entry *entry)
  3245. {
  3246. if (sm == NULL || sm->pmksa != entry)
  3247. return -1;
  3248. sm->pmksa = NULL;
  3249. return 0;
  3250. }
  3251. struct rsn_pmksa_cache_entry *
  3252. wpa_auth_sta_get_pmksa(struct wpa_state_machine *sm)
  3253. {
  3254. return sm ? sm->pmksa : NULL;
  3255. }
  3256. void wpa_auth_sta_local_mic_failure_report(struct wpa_state_machine *sm)
  3257. {
  3258. if (sm)
  3259. sm->dot11RSNAStatsTKIPLocalMICFailures++;
  3260. }
  3261. const u8 * wpa_auth_get_wpa_ie(struct wpa_authenticator *wpa_auth, size_t *len)
  3262. {
  3263. if (wpa_auth == NULL)
  3264. return NULL;
  3265. *len = wpa_auth->wpa_ie_len;
  3266. return wpa_auth->wpa_ie;
  3267. }
  3268. int wpa_auth_pmksa_add(struct wpa_state_machine *sm, const u8 *pmk,
  3269. unsigned int pmk_len,
  3270. int session_timeout, struct eapol_state_machine *eapol)
  3271. {
  3272. if (sm == NULL || sm->wpa != WPA_VERSION_WPA2 ||
  3273. sm->wpa_auth->conf.disable_pmksa_caching)
  3274. return -1;
  3275. if (wpa_key_mgmt_sha384(sm->wpa_key_mgmt)) {
  3276. if (pmk_len > PMK_LEN_SUITE_B_192)
  3277. pmk_len = PMK_LEN_SUITE_B_192;
  3278. } else if (pmk_len > PMK_LEN) {
  3279. pmk_len = PMK_LEN;
  3280. }
  3281. if (pmksa_cache_auth_add(sm->wpa_auth->pmksa, pmk, pmk_len, NULL,
  3282. sm->PTK.kck, sm->PTK.kck_len,
  3283. sm->wpa_auth->addr, sm->addr, session_timeout,
  3284. eapol, sm->wpa_key_mgmt))
  3285. return 0;
  3286. return -1;
  3287. }
  3288. int wpa_auth_pmksa_add_preauth(struct wpa_authenticator *wpa_auth,
  3289. const u8 *pmk, size_t len, const u8 *sta_addr,
  3290. int session_timeout,
  3291. struct eapol_state_machine *eapol)
  3292. {
  3293. if (wpa_auth == NULL)
  3294. return -1;
  3295. if (pmksa_cache_auth_add(wpa_auth->pmksa, pmk, len, NULL,
  3296. NULL, 0,
  3297. wpa_auth->addr,
  3298. sta_addr, session_timeout, eapol,
  3299. WPA_KEY_MGMT_IEEE8021X))
  3300. return 0;
  3301. return -1;
  3302. }
  3303. int wpa_auth_pmksa_add_sae(struct wpa_authenticator *wpa_auth, const u8 *addr,
  3304. const u8 *pmk, const u8 *pmkid)
  3305. {
  3306. if (wpa_auth->conf.disable_pmksa_caching)
  3307. return -1;
  3308. if (pmksa_cache_auth_add(wpa_auth->pmksa, pmk, PMK_LEN, pmkid,
  3309. NULL, 0,
  3310. wpa_auth->addr, addr, 0, NULL,
  3311. WPA_KEY_MGMT_SAE))
  3312. return 0;
  3313. return -1;
  3314. }
  3315. void wpa_auth_pmksa_remove(struct wpa_authenticator *wpa_auth,
  3316. const u8 *sta_addr)
  3317. {
  3318. struct rsn_pmksa_cache_entry *pmksa;
  3319. if (wpa_auth == NULL || wpa_auth->pmksa == NULL)
  3320. return;
  3321. pmksa = pmksa_cache_auth_get(wpa_auth->pmksa, sta_addr, NULL);
  3322. if (pmksa) {
  3323. wpa_printf(MSG_DEBUG, "WPA: Remove PMKSA cache entry for "
  3324. MACSTR " based on request", MAC2STR(sta_addr));
  3325. pmksa_cache_free_entry(wpa_auth->pmksa, pmksa);
  3326. }
  3327. }
  3328. int wpa_auth_pmksa_list(struct wpa_authenticator *wpa_auth, char *buf,
  3329. size_t len)
  3330. {
  3331. if (!wpa_auth || !wpa_auth->pmksa)
  3332. return 0;
  3333. return pmksa_cache_auth_list(wpa_auth->pmksa, buf, len);
  3334. }
  3335. void wpa_auth_pmksa_flush(struct wpa_authenticator *wpa_auth)
  3336. {
  3337. if (wpa_auth && wpa_auth->pmksa)
  3338. pmksa_cache_auth_flush(wpa_auth->pmksa);
  3339. }
  3340. #ifdef CONFIG_PMKSA_CACHE_EXTERNAL
  3341. #ifdef CONFIG_MESH
  3342. int wpa_auth_pmksa_list_mesh(struct wpa_authenticator *wpa_auth, const u8 *addr,
  3343. char *buf, size_t len)
  3344. {
  3345. if (!wpa_auth || !wpa_auth->pmksa)
  3346. return 0;
  3347. return pmksa_cache_auth_list_mesh(wpa_auth->pmksa, addr, buf, len);
  3348. }
  3349. struct rsn_pmksa_cache_entry *
  3350. wpa_auth_pmksa_create_entry(const u8 *aa, const u8 *spa, const u8 *pmk,
  3351. const u8 *pmkid, int expiration)
  3352. {
  3353. struct rsn_pmksa_cache_entry *entry;
  3354. struct os_reltime now;
  3355. entry = pmksa_cache_auth_create_entry(pmk, PMK_LEN, pmkid, NULL, 0, aa,
  3356. spa, 0, NULL, WPA_KEY_MGMT_SAE);
  3357. if (!entry)
  3358. return NULL;
  3359. os_get_reltime(&now);
  3360. entry->expiration = now.sec + expiration;
  3361. return entry;
  3362. }
  3363. int wpa_auth_pmksa_add_entry(struct wpa_authenticator *wpa_auth,
  3364. struct rsn_pmksa_cache_entry *entry)
  3365. {
  3366. int ret;
  3367. if (!wpa_auth || !wpa_auth->pmksa)
  3368. return -1;
  3369. ret = pmksa_cache_auth_add_entry(wpa_auth->pmksa, entry);
  3370. if (ret < 0)
  3371. wpa_printf(MSG_DEBUG,
  3372. "RSN: Failed to store external PMKSA cache for "
  3373. MACSTR, MAC2STR(entry->spa));
  3374. return ret;
  3375. }
  3376. #endif /* CONFIG_MESH */
  3377. #endif /* CONFIG_PMKSA_CACHE_EXTERNAL */
  3378. struct rsn_pmksa_cache_entry *
  3379. wpa_auth_pmksa_get(struct wpa_authenticator *wpa_auth, const u8 *sta_addr,
  3380. const u8 *pmkid)
  3381. {
  3382. if (!wpa_auth || !wpa_auth->pmksa)
  3383. return NULL;
  3384. return pmksa_cache_auth_get(wpa_auth->pmksa, sta_addr, pmkid);
  3385. }
  3386. void wpa_auth_pmksa_set_to_sm(struct rsn_pmksa_cache_entry *pmksa,
  3387. struct wpa_state_machine *sm,
  3388. struct wpa_authenticator *wpa_auth,
  3389. u8 *pmkid, u8 *pmk)
  3390. {
  3391. if (!sm)
  3392. return;
  3393. sm->pmksa = pmksa;
  3394. os_memcpy(pmk, pmksa->pmk, PMK_LEN);
  3395. os_memcpy(pmkid, pmksa->pmkid, PMKID_LEN);
  3396. os_memcpy(wpa_auth->dot11RSNAPMKIDUsed, pmksa->pmkid, PMKID_LEN);
  3397. }
  3398. /*
  3399. * Remove and free the group from wpa_authenticator. This is triggered by a
  3400. * callback to make sure nobody is currently iterating the group list while it
  3401. * gets modified.
  3402. */
  3403. static void wpa_group_free(struct wpa_authenticator *wpa_auth,
  3404. struct wpa_group *group)
  3405. {
  3406. struct wpa_group *prev = wpa_auth->group;
  3407. wpa_printf(MSG_DEBUG, "WPA: Remove group state machine for VLAN-ID %d",
  3408. group->vlan_id);
  3409. while (prev) {
  3410. if (prev->next == group) {
  3411. /* This never frees the special first group as needed */
  3412. prev->next = group->next;
  3413. os_free(group);
  3414. break;
  3415. }
  3416. prev = prev->next;
  3417. }
  3418. }
  3419. /* Increase the reference counter for group */
  3420. static void wpa_group_get(struct wpa_authenticator *wpa_auth,
  3421. struct wpa_group *group)
  3422. {
  3423. /* Skip the special first group */
  3424. if (wpa_auth->group == group)
  3425. return;
  3426. group->references++;
  3427. }
  3428. /* Decrease the reference counter and maybe free the group */
  3429. static void wpa_group_put(struct wpa_authenticator *wpa_auth,
  3430. struct wpa_group *group)
  3431. {
  3432. /* Skip the special first group */
  3433. if (wpa_auth->group == group)
  3434. return;
  3435. group->references--;
  3436. if (group->references)
  3437. return;
  3438. wpa_group_free(wpa_auth, group);
  3439. }
  3440. /*
  3441. * Add a group that has its references counter set to zero. Caller needs to
  3442. * call wpa_group_get() on the return value to mark the entry in use.
  3443. */
  3444. static struct wpa_group *
  3445. wpa_auth_add_group(struct wpa_authenticator *wpa_auth, int vlan_id)
  3446. {
  3447. struct wpa_group *group;
  3448. if (wpa_auth == NULL || wpa_auth->group == NULL)
  3449. return NULL;
  3450. wpa_printf(MSG_DEBUG, "WPA: Add group state machine for VLAN-ID %d",
  3451. vlan_id);
  3452. group = wpa_group_init(wpa_auth, vlan_id, 0);
  3453. if (group == NULL)
  3454. return NULL;
  3455. group->next = wpa_auth->group->next;
  3456. wpa_auth->group->next = group;
  3457. return group;
  3458. }
  3459. /*
  3460. * Enforce that the group state machine for the VLAN is running, increase
  3461. * reference counter as interface is up. References might have been increased
  3462. * even if a negative value is returned.
  3463. * Returns: -1 on error (group missing, group already failed); otherwise, 0
  3464. */
  3465. int wpa_auth_ensure_group(struct wpa_authenticator *wpa_auth, int vlan_id)
  3466. {
  3467. struct wpa_group *group;
  3468. if (wpa_auth == NULL)
  3469. return 0;
  3470. group = wpa_auth->group;
  3471. while (group) {
  3472. if (group->vlan_id == vlan_id)
  3473. break;
  3474. group = group->next;
  3475. }
  3476. if (group == NULL) {
  3477. group = wpa_auth_add_group(wpa_auth, vlan_id);
  3478. if (group == NULL)
  3479. return -1;
  3480. }
  3481. wpa_printf(MSG_DEBUG,
  3482. "WPA: Ensure group state machine running for VLAN ID %d",
  3483. vlan_id);
  3484. wpa_group_get(wpa_auth, group);
  3485. group->num_setup_iface++;
  3486. if (group->wpa_group_state == WPA_GROUP_FATAL_FAILURE)
  3487. return -1;
  3488. return 0;
  3489. }
  3490. /*
  3491. * Decrease reference counter, expected to be zero afterwards.
  3492. * returns: -1 on error (group not found, group in fail state)
  3493. * -2 if wpa_group is still referenced
  3494. * 0 else
  3495. */
  3496. int wpa_auth_release_group(struct wpa_authenticator *wpa_auth, int vlan_id)
  3497. {
  3498. struct wpa_group *group;
  3499. int ret = 0;
  3500. if (wpa_auth == NULL)
  3501. return 0;
  3502. group = wpa_auth->group;
  3503. while (group) {
  3504. if (group->vlan_id == vlan_id)
  3505. break;
  3506. group = group->next;
  3507. }
  3508. if (group == NULL)
  3509. return -1;
  3510. wpa_printf(MSG_DEBUG,
  3511. "WPA: Try stopping group state machine for VLAN ID %d",
  3512. vlan_id);
  3513. if (group->num_setup_iface <= 0) {
  3514. wpa_printf(MSG_ERROR,
  3515. "WPA: wpa_auth_release_group called more often than wpa_auth_ensure_group for VLAN ID %d, skipping.",
  3516. vlan_id);
  3517. return -1;
  3518. }
  3519. group->num_setup_iface--;
  3520. if (group->wpa_group_state == WPA_GROUP_FATAL_FAILURE)
  3521. ret = -1;
  3522. if (group->references > 1) {
  3523. wpa_printf(MSG_DEBUG,
  3524. "WPA: Cannot stop group state machine for VLAN ID %d as references are still hold",
  3525. vlan_id);
  3526. ret = -2;
  3527. }
  3528. wpa_group_put(wpa_auth, group);
  3529. return ret;
  3530. }
  3531. int wpa_auth_sta_set_vlan(struct wpa_state_machine *sm, int vlan_id)
  3532. {
  3533. struct wpa_group *group;
  3534. if (sm == NULL || sm->wpa_auth == NULL)
  3535. return 0;
  3536. group = sm->wpa_auth->group;
  3537. while (group) {
  3538. if (group->vlan_id == vlan_id)
  3539. break;
  3540. group = group->next;
  3541. }
  3542. if (group == NULL) {
  3543. group = wpa_auth_add_group(sm->wpa_auth, vlan_id);
  3544. if (group == NULL)
  3545. return -1;
  3546. }
  3547. if (sm->group == group)
  3548. return 0;
  3549. if (group->wpa_group_state == WPA_GROUP_FATAL_FAILURE)
  3550. return -1;
  3551. wpa_printf(MSG_DEBUG, "WPA: Moving STA " MACSTR " to use group state "
  3552. "machine for VLAN ID %d", MAC2STR(sm->addr), vlan_id);
  3553. wpa_group_get(sm->wpa_auth, group);
  3554. wpa_group_put(sm->wpa_auth, sm->group);
  3555. sm->group = group;
  3556. return 0;
  3557. }
  3558. void wpa_auth_eapol_key_tx_status(struct wpa_authenticator *wpa_auth,
  3559. struct wpa_state_machine *sm, int ack)
  3560. {
  3561. if (wpa_auth == NULL || sm == NULL)
  3562. return;
  3563. wpa_printf(MSG_DEBUG, "WPA: EAPOL-Key TX status for STA " MACSTR
  3564. " ack=%d", MAC2STR(sm->addr), ack);
  3565. if (sm->pending_1_of_4_timeout && ack) {
  3566. /*
  3567. * Some deployed supplicant implementations update their SNonce
  3568. * for each EAPOL-Key 2/4 message even within the same 4-way
  3569. * handshake and then fail to use the first SNonce when
  3570. * deriving the PTK. This results in unsuccessful 4-way
  3571. * handshake whenever the relatively short initial timeout is
  3572. * reached and EAPOL-Key 1/4 is retransmitted. Try to work
  3573. * around this by increasing the timeout now that we know that
  3574. * the station has received the frame.
  3575. */
  3576. int timeout_ms = eapol_key_timeout_subseq;
  3577. wpa_printf(MSG_DEBUG, "WPA: Increase initial EAPOL-Key 1/4 "
  3578. "timeout by %u ms because of acknowledged frame",
  3579. timeout_ms);
  3580. eloop_cancel_timeout(wpa_send_eapol_timeout, wpa_auth, sm);
  3581. eloop_register_timeout(timeout_ms / 1000,
  3582. (timeout_ms % 1000) * 1000,
  3583. wpa_send_eapol_timeout, wpa_auth, sm);
  3584. }
  3585. }
  3586. int wpa_auth_uses_sae(struct wpa_state_machine *sm)
  3587. {
  3588. if (sm == NULL)
  3589. return 0;
  3590. return wpa_key_mgmt_sae(sm->wpa_key_mgmt);
  3591. }
  3592. int wpa_auth_uses_ft_sae(struct wpa_state_machine *sm)
  3593. {
  3594. if (sm == NULL)
  3595. return 0;
  3596. return sm->wpa_key_mgmt == WPA_KEY_MGMT_FT_SAE;
  3597. }
  3598. #ifdef CONFIG_P2P
  3599. int wpa_auth_get_ip_addr(struct wpa_state_machine *sm, u8 *addr)
  3600. {
  3601. if (sm == NULL || WPA_GET_BE32(sm->ip_addr) == 0)
  3602. return -1;
  3603. os_memcpy(addr, sm->ip_addr, 4);
  3604. return 0;
  3605. }
  3606. #endif /* CONFIG_P2P */
  3607. int wpa_auth_radius_das_disconnect_pmksa(struct wpa_authenticator *wpa_auth,
  3608. struct radius_das_attrs *attr)
  3609. {
  3610. return pmksa_cache_auth_radius_das_disconnect(wpa_auth->pmksa, attr);
  3611. }
  3612. void wpa_auth_reconfig_group_keys(struct wpa_authenticator *wpa_auth)
  3613. {
  3614. struct wpa_group *group;
  3615. if (!wpa_auth)
  3616. return;
  3617. for (group = wpa_auth->group; group; group = group->next)
  3618. wpa_group_config_group_keys(wpa_auth, group);
  3619. }
  3620. #ifdef CONFIG_FILS
  3621. struct wpa_auth_fils_iter_data {
  3622. struct wpa_authenticator *auth;
  3623. const u8 *cache_id;
  3624. struct rsn_pmksa_cache_entry *pmksa;
  3625. const u8 *spa;
  3626. const u8 *pmkid;
  3627. };
  3628. static int wpa_auth_fils_iter(struct wpa_authenticator *a, void *ctx)
  3629. {
  3630. struct wpa_auth_fils_iter_data *data = ctx;
  3631. if (a == data->auth || !a->conf.fils_cache_id ||
  3632. os_memcmp(a->conf.fils_cache_id, data->cache_id,
  3633. FILS_CACHE_ID_LEN) != 0)
  3634. return 0;
  3635. data->pmksa = pmksa_cache_auth_get(a->pmksa, data->spa, data->pmkid);
  3636. return data->pmksa != NULL;
  3637. }
  3638. struct rsn_pmksa_cache_entry *
  3639. wpa_auth_pmksa_get_fils_cache_id(struct wpa_authenticator *wpa_auth,
  3640. const u8 *sta_addr, const u8 *pmkid)
  3641. {
  3642. struct wpa_auth_fils_iter_data idata;
  3643. if (!wpa_auth->conf.fils_cache_id_set)
  3644. return NULL;
  3645. idata.auth = wpa_auth;
  3646. idata.cache_id = wpa_auth->conf.fils_cache_id;
  3647. idata.pmksa = NULL;
  3648. idata.spa = sta_addr;
  3649. idata.pmkid = pmkid;
  3650. wpa_auth_for_each_auth(wpa_auth, wpa_auth_fils_iter, &idata);
  3651. return idata.pmksa;
  3652. }
  3653. #endif /* CONFIG_FILS */