wpa_auth.c 132 KB

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