dpp.c 190 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074
  1. /*
  2. * DPP functionality shared between hostapd and wpa_supplicant
  3. * Copyright (c) 2017, Qualcomm Atheros, Inc.
  4. *
  5. * This software may be distributed under the terms of the BSD license.
  6. * See README for more details.
  7. */
  8. #include "utils/includes.h"
  9. #include <openssl/opensslv.h>
  10. #include <openssl/err.h>
  11. #include "utils/common.h"
  12. #include "utils/base64.h"
  13. #include "utils/json.h"
  14. #include "common/ieee802_11_common.h"
  15. #include "common/ieee802_11_defs.h"
  16. #include "common/wpa_ctrl.h"
  17. #include "crypto/crypto.h"
  18. #include "crypto/random.h"
  19. #include "crypto/aes.h"
  20. #include "crypto/aes_siv.h"
  21. #include "crypto/sha384.h"
  22. #include "crypto/sha512.h"
  23. #include "drivers/driver.h"
  24. #include "dpp.h"
  25. #ifdef CONFIG_TESTING_OPTIONS
  26. enum dpp_test_behavior dpp_test = DPP_TEST_DISABLED;
  27. #endif /* CONFIG_TESTING_OPTIONS */
  28. #if OPENSSL_VERSION_NUMBER < 0x10100000L
  29. /* Compatibility wrappers for older versions. */
  30. static int ECDSA_SIG_set0(ECDSA_SIG *sig, BIGNUM *r, BIGNUM *s)
  31. {
  32. sig->r = r;
  33. sig->s = s;
  34. return 1;
  35. }
  36. static void ECDSA_SIG_get0(const ECDSA_SIG *sig, const BIGNUM **pr,
  37. const BIGNUM **ps)
  38. {
  39. if (pr)
  40. *pr = sig->r;
  41. if (ps)
  42. *ps = sig->s;
  43. }
  44. #endif
  45. static const struct dpp_curve_params dpp_curves[] = {
  46. /* The mandatory to support and the default NIST P-256 curve needs to
  47. * be the first entry on this list. */
  48. { "prime256v1", 32, 32, 16, 32, "P-256", 19, "ES256" },
  49. { "secp384r1", 48, 48, 24, 48, "P-384", 20, "ES384" },
  50. { "secp521r1", 64, 64, 32, 66, "P-521", 21, "ES512" },
  51. { "brainpoolP256r1", 32, 32, 16, 32, "BP-256", 28, "BS256" },
  52. { "brainpoolP384r1", 48, 48, 24, 48, "BP-384", 29, "BS384" },
  53. { "brainpoolP512r1", 64, 64, 32, 64, "BP-512", 30, "BS512" },
  54. { NULL, 0, 0, 0, 0, NULL, 0, NULL }
  55. };
  56. /* Role-specific elements for PKEX */
  57. /* NIST P-256 */
  58. static const u8 pkex_init_x_p256[32] = {
  59. 0x56, 0x26, 0x12, 0xcf, 0x36, 0x48, 0xfe, 0x0b,
  60. 0x07, 0x04, 0xbb, 0x12, 0x22, 0x50, 0xb2, 0x54,
  61. 0xb1, 0x94, 0x64, 0x7e, 0x54, 0xce, 0x08, 0x07,
  62. 0x2e, 0xec, 0xca, 0x74, 0x5b, 0x61, 0x2d, 0x25
  63. };
  64. static const u8 pkex_init_y_p256[32] = {
  65. 0x3e, 0x44, 0xc7, 0xc9, 0x8c, 0x1c, 0xa1, 0x0b,
  66. 0x20, 0x09, 0x93, 0xb2, 0xfd, 0xe5, 0x69, 0xdc,
  67. 0x75, 0xbc, 0xad, 0x33, 0xc1, 0xe7, 0xc6, 0x45,
  68. 0x4d, 0x10, 0x1e, 0x6a, 0x3d, 0x84, 0x3c, 0xa4
  69. };
  70. static const u8 pkex_resp_x_p256[32] = {
  71. 0x1e, 0xa4, 0x8a, 0xb1, 0xa4, 0xe8, 0x42, 0x39,
  72. 0xad, 0x73, 0x07, 0xf2, 0x34, 0xdf, 0x57, 0x4f,
  73. 0xc0, 0x9d, 0x54, 0xbe, 0x36, 0x1b, 0x31, 0x0f,
  74. 0x59, 0x91, 0x52, 0x33, 0xac, 0x19, 0x9d, 0x76
  75. };
  76. static const u8 pkex_resp_y_p256[32] = {
  77. 0x26, 0x04, 0x09, 0x45, 0x0a, 0x05, 0x20, 0xe7,
  78. 0xa7, 0x27, 0xc1, 0x36, 0x76, 0x85, 0xca, 0x3e,
  79. 0x42, 0x16, 0xf4, 0x89, 0x85, 0x34, 0x6e, 0xd5,
  80. 0x17, 0xde, 0xc0, 0xb8, 0xad, 0xfd, 0xb2, 0x98
  81. };
  82. /* NIST P-384 */
  83. static const u8 pkex_init_x_p384[48] = {
  84. 0x95, 0x3f, 0x42, 0x9e, 0x50, 0x7f, 0xf9, 0xaa,
  85. 0xac, 0x1a, 0xf2, 0x85, 0x2e, 0x64, 0x91, 0x68,
  86. 0x64, 0xc4, 0x3c, 0xb7, 0x5c, 0xf8, 0xc9, 0x53,
  87. 0x6e, 0x58, 0x4c, 0x7f, 0xc4, 0x64, 0x61, 0xac,
  88. 0x51, 0x8a, 0x6f, 0xfe, 0xab, 0x74, 0xe6, 0x12,
  89. 0x81, 0xac, 0x38, 0x5d, 0x41, 0xe6, 0xb9, 0xa3
  90. };
  91. static const u8 pkex_init_y_p384[48] = {
  92. 0x89, 0xd0, 0x97, 0x7b, 0x59, 0x4f, 0xa6, 0xd6,
  93. 0x7c, 0x5d, 0x93, 0x5b, 0x93, 0xc4, 0x07, 0xa9,
  94. 0x89, 0xee, 0xd5, 0xcd, 0x6f, 0x42, 0xf8, 0x38,
  95. 0xc8, 0xc6, 0x62, 0x24, 0x69, 0x0c, 0xd4, 0x48,
  96. 0xd8, 0x44, 0xd6, 0xc2, 0xe8, 0xcc, 0x62, 0x6b,
  97. 0x3c, 0x25, 0x53, 0xba, 0x4f, 0x71, 0xf8, 0xe7
  98. };
  99. static const u8 pkex_resp_x_p384[48] = {
  100. 0xad, 0xbe, 0xd7, 0x1d, 0x3a, 0x71, 0x64, 0x98,
  101. 0x5f, 0xb4, 0xd6, 0x4b, 0x50, 0xd0, 0x84, 0x97,
  102. 0x4b, 0x7e, 0x57, 0x70, 0xd2, 0xd9, 0xf4, 0x92,
  103. 0x2a, 0x3f, 0xce, 0x99, 0xc5, 0x77, 0x33, 0x44,
  104. 0x14, 0x56, 0x92, 0xcb, 0xae, 0x46, 0x64, 0xdf,
  105. 0xe0, 0xbb, 0xd7, 0xb1, 0x29, 0x20, 0x72, 0xdf
  106. };
  107. static const u8 pkex_resp_y_p384[48] = {
  108. 0x54, 0x58, 0x20, 0xad, 0x55, 0x1d, 0xca, 0xf3,
  109. 0x1c, 0x8a, 0xcd, 0x19, 0x40, 0xf9, 0x37, 0x83,
  110. 0xc7, 0xd6, 0xb3, 0x13, 0x7d, 0x53, 0x28, 0x5c,
  111. 0xf6, 0x2d, 0xf1, 0xdd, 0xa5, 0x8b, 0xad, 0x5d,
  112. 0x81, 0xab, 0xb1, 0x00, 0x39, 0xd6, 0xcc, 0x9c,
  113. 0xea, 0x1e, 0x84, 0x1d, 0xbf, 0xe3, 0x35, 0xf9
  114. };
  115. /* NIST P-521 */
  116. static const u8 pkex_init_x_p521[66] = {
  117. 0x00, 0x16, 0x20, 0x45, 0x19, 0x50, 0x95, 0x23,
  118. 0x0d, 0x24, 0xbe, 0x00, 0x87, 0xdc, 0xfa, 0xf0,
  119. 0x58, 0x9a, 0x01, 0x60, 0x07, 0x7a, 0xca, 0x76,
  120. 0x01, 0xab, 0x2d, 0x5a, 0x46, 0xcd, 0x2c, 0xb5,
  121. 0x11, 0x9a, 0xff, 0xaa, 0x48, 0x04, 0x91, 0x38,
  122. 0xcf, 0x86, 0xfc, 0xa4, 0xa5, 0x0f, 0x47, 0x01,
  123. 0x80, 0x1b, 0x30, 0xa3, 0xae, 0xe8, 0x1c, 0x2e,
  124. 0xea, 0xcc, 0xf0, 0x03, 0x9f, 0x77, 0x4c, 0x8d,
  125. 0x97, 0x76
  126. };
  127. static const u8 pkex_init_y_p521[66] = {
  128. 0x01, 0x4c, 0x71, 0xfd, 0x1b, 0xd5, 0x9c, 0xa6,
  129. 0xed, 0x39, 0xef, 0x45, 0xc5, 0x06, 0xfd, 0x66,
  130. 0xc0, 0xeb, 0x0f, 0xbf, 0x21, 0xa3, 0x36, 0x74,
  131. 0xfd, 0xaa, 0x05, 0x6e, 0x4e, 0x33, 0x95, 0x42,
  132. 0x1a, 0x9d, 0x3f, 0x3a, 0x1c, 0x5e, 0xa8, 0x60,
  133. 0xf7, 0xe5, 0x59, 0x1d, 0x07, 0xaa, 0x6f, 0x40,
  134. 0x0a, 0x59, 0x3c, 0x27, 0xad, 0xe0, 0x48, 0xfd,
  135. 0xd1, 0x83, 0x37, 0x4c, 0xdf, 0xe1, 0x86, 0x72,
  136. 0xfc, 0x57
  137. };
  138. static const u8 pkex_resp_x_p521[66] = {
  139. 0x00, 0x79, 0xe4, 0x4d, 0x6b, 0x5e, 0x12, 0x0a,
  140. 0x18, 0x2c, 0xb3, 0x05, 0x77, 0x0f, 0xc3, 0x44,
  141. 0x1a, 0xcd, 0x78, 0x46, 0x14, 0xee, 0x46, 0x3f,
  142. 0xab, 0xc9, 0x59, 0x7c, 0x85, 0xa0, 0xc2, 0xfb,
  143. 0x02, 0x32, 0x99, 0xde, 0x5d, 0xe1, 0x0d, 0x48,
  144. 0x2d, 0x71, 0x7d, 0x8d, 0x3f, 0x61, 0x67, 0x9e,
  145. 0x2b, 0x8b, 0x12, 0xde, 0x10, 0x21, 0x55, 0x0a,
  146. 0x5b, 0x2d, 0xe8, 0x05, 0x09, 0xf6, 0x20, 0x97,
  147. 0x84, 0xb4
  148. };
  149. static const u8 pkex_resp_y_p521[66] = {
  150. 0x01, 0xb9, 0x9c, 0xc6, 0x41, 0x32, 0x5b, 0xd2,
  151. 0x35, 0xd8, 0x8b, 0x2b, 0xe4, 0x6e, 0xcc, 0xdf,
  152. 0x7c, 0x38, 0xc4, 0x5b, 0xf6, 0x74, 0x71, 0x5c,
  153. 0x77, 0x16, 0x8a, 0x80, 0xa9, 0x84, 0xc7, 0x7b,
  154. 0x9d, 0xfd, 0x83, 0x6f, 0xae, 0xf8, 0x24, 0x16,
  155. 0x2f, 0x21, 0x25, 0x65, 0xa2, 0x1a, 0x6b, 0x2d,
  156. 0x30, 0x62, 0xb3, 0xcc, 0x6e, 0x59, 0x3c, 0x7f,
  157. 0x58, 0x91, 0x81, 0x72, 0x07, 0x8c, 0x91, 0xac,
  158. 0x31, 0x1e
  159. };
  160. /* Brainpool P-256r1 */
  161. static const u8 pkex_init_x_bp_p256r1[32] = {
  162. 0x46, 0x98, 0x18, 0x6c, 0x27, 0xcd, 0x4b, 0x10,
  163. 0x7d, 0x55, 0xa3, 0xdd, 0x89, 0x1f, 0x9f, 0xca,
  164. 0xc7, 0x42, 0x5b, 0x8a, 0x23, 0xed, 0xf8, 0x75,
  165. 0xac, 0xc7, 0xe9, 0x8d, 0xc2, 0x6f, 0xec, 0xd8
  166. };
  167. static const u8 pkex_init_y_bp_p256r1[32] = {
  168. 0x16, 0x30, 0x68, 0x32, 0x3b, 0xb0, 0x21, 0xee,
  169. 0xeb, 0xf7, 0xb6, 0x7c, 0xae, 0x52, 0x26, 0x42,
  170. 0x59, 0x28, 0x58, 0xb6, 0x14, 0x90, 0xed, 0x69,
  171. 0xd0, 0x67, 0xea, 0x25, 0x60, 0x0f, 0xa9, 0x6c
  172. };
  173. static const u8 pkex_resp_x_bp_p256r1[32] = {
  174. 0x90, 0x18, 0x84, 0xc9, 0xdc, 0xcc, 0xb5, 0x2f,
  175. 0x4a, 0x3f, 0x4f, 0x18, 0x0a, 0x22, 0x56, 0x6a,
  176. 0xa9, 0xef, 0xd4, 0xe6, 0xc3, 0x53, 0xc2, 0x1a,
  177. 0x23, 0x54, 0xdd, 0x08, 0x7e, 0x10, 0xd8, 0xe3
  178. };
  179. static const u8 pkex_resp_y_bp_p256r1[32] = {
  180. 0x2a, 0xfa, 0x98, 0x9b, 0xe3, 0xda, 0x30, 0xfd,
  181. 0x32, 0x28, 0xcb, 0x66, 0xfb, 0x40, 0x7f, 0xf2,
  182. 0xb2, 0x25, 0x80, 0x82, 0x44, 0x85, 0x13, 0x7e,
  183. 0x4b, 0xb5, 0x06, 0xc0, 0x03, 0x69, 0x23, 0x64
  184. };
  185. /* Brainpool P-384r1 */
  186. static const u8 pkex_init_x_bp_p384r1[48] = {
  187. 0x0a, 0x2c, 0xeb, 0x49, 0x5e, 0xb7, 0x23, 0xbd,
  188. 0x20, 0x5b, 0xe0, 0x49, 0xdf, 0xcf, 0xcf, 0x19,
  189. 0x37, 0x36, 0xe1, 0x2f, 0x59, 0xdb, 0x07, 0x06,
  190. 0xb5, 0xeb, 0x2d, 0xae, 0xc2, 0xb2, 0x38, 0x62,
  191. 0xa6, 0x73, 0x09, 0xa0, 0x6c, 0x0a, 0xa2, 0x30,
  192. 0x99, 0xeb, 0xf7, 0x1e, 0x47, 0xb9, 0x5e, 0xbe
  193. };
  194. static const u8 pkex_init_y_bp_p384r1[48] = {
  195. 0x54, 0x76, 0x61, 0x65, 0x75, 0x5a, 0x2f, 0x99,
  196. 0x39, 0x73, 0xca, 0x6c, 0xf9, 0xf7, 0x12, 0x86,
  197. 0x54, 0xd5, 0xd4, 0xad, 0x45, 0x7b, 0xbf, 0x32,
  198. 0xee, 0x62, 0x8b, 0x9f, 0x52, 0xe8, 0xa0, 0xc9,
  199. 0xb7, 0x9d, 0xd1, 0x09, 0xb4, 0x79, 0x1c, 0x3e,
  200. 0x1a, 0xbf, 0x21, 0x45, 0x66, 0x6b, 0x02, 0x52
  201. };
  202. static const u8 pkex_resp_x_bp_p384r1[48] = {
  203. 0x03, 0xa2, 0x57, 0xef, 0xe8, 0x51, 0x21, 0xa0,
  204. 0xc8, 0x9e, 0x21, 0x02, 0xb5, 0x9a, 0x36, 0x25,
  205. 0x74, 0x22, 0xd1, 0xf2, 0x1b, 0xa8, 0x9a, 0x9b,
  206. 0x97, 0xbc, 0x5a, 0xeb, 0x26, 0x15, 0x09, 0x71,
  207. 0x77, 0x59, 0xec, 0x8b, 0xb7, 0xe1, 0xe8, 0xce,
  208. 0x65, 0xb8, 0xaf, 0xf8, 0x80, 0xae, 0x74, 0x6c
  209. };
  210. static const u8 pkex_resp_y_bp_p384r1[48] = {
  211. 0x2f, 0xd9, 0x6a, 0xc7, 0x3e, 0xec, 0x76, 0x65,
  212. 0x2d, 0x38, 0x7f, 0xec, 0x63, 0x26, 0x3f, 0x04,
  213. 0xd8, 0x4e, 0xff, 0xe1, 0x0a, 0x51, 0x74, 0x70,
  214. 0xe5, 0x46, 0x63, 0x7f, 0x5c, 0xc0, 0xd1, 0x7c,
  215. 0xfb, 0x2f, 0xea, 0xe2, 0xd8, 0x0f, 0x84, 0xcb,
  216. 0xe9, 0x39, 0x5c, 0x64, 0xfe, 0xcb, 0x2f, 0xf1
  217. };
  218. /* Brainpool P-512r1 */
  219. static const u8 pkex_init_x_bp_p512r1[64] = {
  220. 0x4c, 0xe9, 0xb6, 0x1c, 0xe2, 0x00, 0x3c, 0x9c,
  221. 0xa9, 0xc8, 0x56, 0x52, 0xaf, 0x87, 0x3e, 0x51,
  222. 0x9c, 0xbb, 0x15, 0x31, 0x1e, 0xc1, 0x05, 0xfc,
  223. 0x7c, 0x77, 0xd7, 0x37, 0x61, 0x27, 0xd0, 0x95,
  224. 0x98, 0xee, 0x5d, 0xa4, 0x3d, 0x09, 0xdb, 0x3d,
  225. 0xfa, 0x89, 0x9e, 0x7f, 0xa6, 0xa6, 0x9c, 0xff,
  226. 0x83, 0x5c, 0x21, 0x6c, 0x3e, 0xf2, 0xfe, 0xdc,
  227. 0x63, 0xe4, 0xd1, 0x0e, 0x75, 0x45, 0x69, 0x0f
  228. };
  229. static const u8 pkex_init_y_bp_p512r1[64] = {
  230. 0x5a, 0x28, 0x01, 0xbe, 0x96, 0x82, 0x4e, 0xf6,
  231. 0xfa, 0xed, 0x7d, 0xfd, 0x48, 0x8b, 0x48, 0x4e,
  232. 0xd1, 0x97, 0x87, 0xc4, 0x05, 0x5d, 0x15, 0x2a,
  233. 0xf4, 0x91, 0x4b, 0x75, 0x90, 0xd9, 0x34, 0x2c,
  234. 0x3c, 0x12, 0xf2, 0xf5, 0x25, 0x94, 0x24, 0x34,
  235. 0xa7, 0x6d, 0x66, 0xbc, 0x27, 0xa4, 0xa0, 0x8d,
  236. 0xd5, 0xe1, 0x54, 0xa3, 0x55, 0x26, 0xd4, 0x14,
  237. 0x17, 0x0f, 0xc1, 0xc7, 0x3d, 0x68, 0x7f, 0x5a
  238. };
  239. static const u8 pkex_resp_x_bp_p512r1[64] = {
  240. 0x2a, 0x60, 0x32, 0x27, 0xa1, 0xe6, 0x94, 0x72,
  241. 0x1c, 0x48, 0xbe, 0xc5, 0x77, 0x14, 0x30, 0x76,
  242. 0xe4, 0xbf, 0xf7, 0x7b, 0xc5, 0xfd, 0xdf, 0x19,
  243. 0x1e, 0x0f, 0xdf, 0x1c, 0x40, 0xfa, 0x34, 0x9e,
  244. 0x1f, 0x42, 0x24, 0xa3, 0x2c, 0xd5, 0xc7, 0xc9,
  245. 0x7b, 0x47, 0x78, 0x96, 0xf1, 0x37, 0x0e, 0x88,
  246. 0xcb, 0xa6, 0x52, 0x29, 0xd7, 0xa8, 0x38, 0x29,
  247. 0x8e, 0x6e, 0x23, 0x47, 0xd4, 0x4b, 0x70, 0x3e
  248. };
  249. static const u8 pkex_resp_y_bp_p512r1[64] = {
  250. 0x2a, 0xbe, 0x59, 0xe6, 0xc4, 0xb3, 0xd8, 0x09,
  251. 0x66, 0x89, 0x0a, 0x2d, 0x19, 0xf0, 0x9c, 0x9f,
  252. 0xb4, 0xab, 0x8f, 0x50, 0x68, 0x3c, 0x74, 0x64,
  253. 0x4e, 0x19, 0x55, 0x81, 0x9b, 0x48, 0x5c, 0xf4,
  254. 0x12, 0x8d, 0xb9, 0xd8, 0x02, 0x5b, 0xe1, 0x26,
  255. 0x7e, 0x19, 0x5c, 0xfd, 0x70, 0xf7, 0x4b, 0xdc,
  256. 0xb5, 0x5d, 0xc1, 0x7a, 0xe9, 0xd1, 0x05, 0x2e,
  257. 0xd1, 0xfd, 0x2f, 0xce, 0x63, 0x77, 0x48, 0x2c
  258. };
  259. static int dpp_hash_vector(const struct dpp_curve_params *curve,
  260. size_t num_elem, const u8 *addr[], const size_t *len,
  261. u8 *mac)
  262. {
  263. if (curve->hash_len == 32)
  264. return sha256_vector(num_elem, addr, len, mac);
  265. if (curve->hash_len == 48)
  266. return sha384_vector(num_elem, addr, len, mac);
  267. if (curve->hash_len == 64)
  268. return sha512_vector(num_elem, addr, len, mac);
  269. return -1;
  270. }
  271. static int dpp_hkdf_expand(size_t hash_len, const u8 *secret, size_t secret_len,
  272. const char *label, u8 *out, size_t outlen)
  273. {
  274. if (hash_len == 32)
  275. return hmac_sha256_kdf(secret, secret_len, NULL,
  276. (const u8 *) label, os_strlen(label),
  277. out, outlen);
  278. if (hash_len == 48)
  279. return hmac_sha384_kdf(secret, secret_len, NULL,
  280. (const u8 *) label, os_strlen(label),
  281. out, outlen);
  282. if (hash_len == 64)
  283. return hmac_sha512_kdf(secret, secret_len, NULL,
  284. (const u8 *) label, os_strlen(label),
  285. out, outlen);
  286. return -1;
  287. }
  288. static int dpp_hmac_vector(size_t hash_len, const u8 *key, size_t key_len,
  289. size_t num_elem, const u8 *addr[],
  290. const size_t *len, u8 *mac)
  291. {
  292. if (hash_len == 32)
  293. return hmac_sha256_vector(key, key_len, num_elem, addr, len,
  294. mac);
  295. if (hash_len == 48)
  296. return hmac_sha384_vector(key, key_len, num_elem, addr, len,
  297. mac);
  298. if (hash_len == 64)
  299. return hmac_sha512_vector(key, key_len, num_elem, addr, len,
  300. mac);
  301. return -1;
  302. }
  303. static int dpp_hmac(size_t hash_len, const u8 *key, size_t key_len,
  304. const u8 *data, size_t data_len, u8 *mac)
  305. {
  306. if (hash_len == 32)
  307. return hmac_sha256(key, key_len, data, data_len, mac);
  308. if (hash_len == 48)
  309. return hmac_sha384(key, key_len, data, data_len, mac);
  310. if (hash_len == 64)
  311. return hmac_sha512(key, key_len, data, data_len, mac);
  312. return -1;
  313. }
  314. static int dpp_bn2bin_pad(const BIGNUM *bn, u8 *pos, size_t len)
  315. {
  316. int num_bytes, offset;
  317. num_bytes = BN_num_bytes(bn);
  318. if ((size_t) num_bytes > len)
  319. return -1;
  320. offset = len - num_bytes;
  321. os_memset(pos, 0, offset);
  322. BN_bn2bin(bn, pos + offset);
  323. return 0;
  324. }
  325. static struct wpabuf * dpp_get_pubkey_point(EVP_PKEY *pkey, int prefix)
  326. {
  327. int len, res;
  328. EC_KEY *eckey;
  329. struct wpabuf *buf;
  330. unsigned char *pos;
  331. eckey = EVP_PKEY_get1_EC_KEY(pkey);
  332. if (!eckey)
  333. return NULL;
  334. EC_KEY_set_conv_form(eckey, POINT_CONVERSION_UNCOMPRESSED);
  335. len = i2o_ECPublicKey(eckey, NULL);
  336. if (len <= 0) {
  337. wpa_printf(MSG_ERROR,
  338. "DDP: Failed to determine public key encoding length");
  339. EC_KEY_free(eckey);
  340. return NULL;
  341. }
  342. buf = wpabuf_alloc(len);
  343. if (!buf) {
  344. EC_KEY_free(eckey);
  345. return NULL;
  346. }
  347. pos = wpabuf_put(buf, len);
  348. res = i2o_ECPublicKey(eckey, &pos);
  349. EC_KEY_free(eckey);
  350. if (res != len) {
  351. wpa_printf(MSG_ERROR,
  352. "DDP: Failed to encode public key (res=%d/%d)",
  353. res, len);
  354. wpabuf_free(buf);
  355. return NULL;
  356. }
  357. if (!prefix) {
  358. /* Remove 0x04 prefix to match DPP definition */
  359. pos = wpabuf_mhead(buf);
  360. os_memmove(pos, pos + 1, len - 1);
  361. buf->used--;
  362. }
  363. return buf;
  364. }
  365. static EVP_PKEY * dpp_set_pubkey_point_group(const EC_GROUP *group,
  366. const u8 *buf_x, const u8 *buf_y,
  367. size_t len)
  368. {
  369. EC_KEY *eckey = NULL;
  370. BN_CTX *ctx;
  371. EC_POINT *point = NULL;
  372. BIGNUM *x = NULL, *y = NULL;
  373. EVP_PKEY *pkey = NULL;
  374. ctx = BN_CTX_new();
  375. if (!ctx) {
  376. wpa_printf(MSG_ERROR, "DPP: Out of memory");
  377. return NULL;
  378. }
  379. point = EC_POINT_new(group);
  380. x = BN_bin2bn(buf_x, len, NULL);
  381. y = BN_bin2bn(buf_y, len, NULL);
  382. if (!point || !x || !y) {
  383. wpa_printf(MSG_ERROR, "DPP: Out of memory");
  384. goto fail;
  385. }
  386. if (!EC_POINT_set_affine_coordinates_GFp(group, point, x, y, ctx)) {
  387. wpa_printf(MSG_ERROR,
  388. "DPP: OpenSSL: EC_POINT_set_affine_coordinates_GFp failed: %s",
  389. ERR_error_string(ERR_get_error(), NULL));
  390. goto fail;
  391. }
  392. if (!EC_POINT_is_on_curve(group, point, ctx) ||
  393. EC_POINT_is_at_infinity(group, point)) {
  394. wpa_printf(MSG_ERROR, "DPP: Invalid point");
  395. goto fail;
  396. }
  397. eckey = EC_KEY_new();
  398. if (!eckey ||
  399. EC_KEY_set_group(eckey, group) != 1 ||
  400. EC_KEY_set_public_key(eckey, point) != 1) {
  401. wpa_printf(MSG_ERROR,
  402. "DPP: Failed to set EC_KEY: %s",
  403. ERR_error_string(ERR_get_error(), NULL));
  404. goto fail;
  405. }
  406. EC_KEY_set_asn1_flag(eckey, OPENSSL_EC_NAMED_CURVE);
  407. pkey = EVP_PKEY_new();
  408. if (!pkey || EVP_PKEY_set1_EC_KEY(pkey, eckey) != 1) {
  409. wpa_printf(MSG_ERROR, "DPP: Could not create EVP_PKEY");
  410. goto fail;
  411. }
  412. out:
  413. BN_free(x);
  414. BN_free(y);
  415. EC_KEY_free(eckey);
  416. EC_POINT_free(point);
  417. BN_CTX_free(ctx);
  418. return pkey;
  419. fail:
  420. EVP_PKEY_free(pkey);
  421. pkey = NULL;
  422. goto out;
  423. }
  424. static EVP_PKEY * dpp_set_pubkey_point(EVP_PKEY *group_key,
  425. const u8 *buf, size_t len)
  426. {
  427. EC_KEY *eckey;
  428. const EC_GROUP *group;
  429. EVP_PKEY *pkey = NULL;
  430. if (len & 1)
  431. return NULL;
  432. eckey = EVP_PKEY_get1_EC_KEY(group_key);
  433. if (!eckey) {
  434. wpa_printf(MSG_ERROR,
  435. "DPP: Could not get EC_KEY from group_key");
  436. return NULL;
  437. }
  438. group = EC_KEY_get0_group(eckey);
  439. if (group)
  440. pkey = dpp_set_pubkey_point_group(group, buf, buf + len / 2,
  441. len / 2);
  442. else
  443. wpa_printf(MSG_ERROR, "DPP: Could not get EC group");
  444. EC_KEY_free(eckey);
  445. return pkey;
  446. }
  447. static void dpp_auth_fail(struct dpp_authentication *auth, const char *txt)
  448. {
  449. wpa_msg(auth->msg_ctx, MSG_INFO, DPP_EVENT_FAIL "%s", txt);
  450. }
  451. struct wpabuf * dpp_alloc_msg(enum dpp_public_action_frame_type type,
  452. size_t len)
  453. {
  454. struct wpabuf *msg;
  455. msg = wpabuf_alloc(8 + len);
  456. if (!msg)
  457. return NULL;
  458. wpabuf_put_u8(msg, WLAN_ACTION_PUBLIC);
  459. wpabuf_put_u8(msg, WLAN_PA_VENDOR_SPECIFIC);
  460. wpabuf_put_be24(msg, OUI_WFA);
  461. wpabuf_put_u8(msg, DPP_OUI_TYPE);
  462. wpabuf_put_u8(msg, 1); /* Crypto Suite */
  463. wpabuf_put_u8(msg, type);
  464. return msg;
  465. }
  466. const u8 * dpp_get_attr(const u8 *buf, size_t len, u16 req_id, u16 *ret_len)
  467. {
  468. u16 id, alen;
  469. const u8 *pos = buf, *end = buf + len;
  470. while (end - pos >= 4) {
  471. id = WPA_GET_LE16(pos);
  472. pos += 2;
  473. alen = WPA_GET_LE16(pos);
  474. pos += 2;
  475. if (alen > end - pos)
  476. return NULL;
  477. if (id == req_id) {
  478. *ret_len = alen;
  479. return pos;
  480. }
  481. pos += alen;
  482. }
  483. return NULL;
  484. }
  485. int dpp_check_attrs(const u8 *buf, size_t len)
  486. {
  487. const u8 *pos, *end;
  488. int wrapped_data = 0;
  489. pos = buf;
  490. end = buf + len;
  491. while (end - pos >= 4) {
  492. u16 id, alen;
  493. id = WPA_GET_LE16(pos);
  494. pos += 2;
  495. alen = WPA_GET_LE16(pos);
  496. pos += 2;
  497. wpa_printf(MSG_MSGDUMP, "DPP: Attribute ID %04x len %u",
  498. id, alen);
  499. if (alen > end - pos) {
  500. wpa_printf(MSG_DEBUG,
  501. "DPP: Truncated message - not enough room for the attribute - dropped");
  502. return -1;
  503. }
  504. if (wrapped_data) {
  505. wpa_printf(MSG_DEBUG,
  506. "DPP: An unexpected attribute included after the Wrapped Data attribute");
  507. return -1;
  508. }
  509. if (id == DPP_ATTR_WRAPPED_DATA)
  510. wrapped_data = 1;
  511. pos += alen;
  512. }
  513. if (end != pos) {
  514. wpa_printf(MSG_DEBUG,
  515. "DPP: Unexpected octets (%d) after the last attribute",
  516. (int) (end - pos));
  517. return -1;
  518. }
  519. return 0;
  520. }
  521. void dpp_bootstrap_info_free(struct dpp_bootstrap_info *info)
  522. {
  523. if (!info)
  524. return;
  525. os_free(info->uri);
  526. os_free(info->info);
  527. EVP_PKEY_free(info->pubkey);
  528. os_free(info);
  529. }
  530. const char * dpp_bootstrap_type_txt(enum dpp_bootstrap_type type)
  531. {
  532. switch (type) {
  533. case DPP_BOOTSTRAP_QR_CODE:
  534. return "QRCODE";
  535. case DPP_BOOTSTRAP_PKEX:
  536. return "PKEX";
  537. }
  538. return "??";
  539. }
  540. static int dpp_uri_valid_info(const char *info)
  541. {
  542. while (*info) {
  543. unsigned char val = *info++;
  544. if (val < 0x20 || val > 0x7e || val == 0x3b)
  545. return 0;
  546. }
  547. return 1;
  548. }
  549. static int dpp_clone_uri(struct dpp_bootstrap_info *bi, const char *uri)
  550. {
  551. bi->uri = os_strdup(uri);
  552. return bi->uri ? 0 : -1;
  553. }
  554. int dpp_parse_uri_chan_list(struct dpp_bootstrap_info *bi,
  555. const char *chan_list)
  556. {
  557. const char *pos = chan_list;
  558. int opclass, channel, freq;
  559. while (pos && *pos && *pos != ';') {
  560. opclass = atoi(pos);
  561. if (opclass <= 0)
  562. goto fail;
  563. pos = os_strchr(pos, '/');
  564. if (!pos)
  565. goto fail;
  566. pos++;
  567. channel = atoi(pos);
  568. if (channel <= 0)
  569. goto fail;
  570. while (*pos >= '0' && *pos <= '9')
  571. pos++;
  572. freq = ieee80211_chan_to_freq(NULL, opclass, channel);
  573. wpa_printf(MSG_DEBUG,
  574. "DPP: URI channel-list: opclass=%d channel=%d ==> freq=%d",
  575. opclass, channel, freq);
  576. if (freq < 0) {
  577. wpa_printf(MSG_DEBUG,
  578. "DPP: Ignore unknown URI channel-list channel (opclass=%d channel=%d)",
  579. opclass, channel);
  580. } else if (bi->num_freq == DPP_BOOTSTRAP_MAX_FREQ) {
  581. wpa_printf(MSG_DEBUG,
  582. "DPP: Too many channels in URI channel-list - ignore list");
  583. bi->num_freq = 0;
  584. break;
  585. } else {
  586. bi->freq[bi->num_freq++] = freq;
  587. }
  588. if (*pos == ';' || *pos == '\0')
  589. break;
  590. if (*pos != ',')
  591. goto fail;
  592. pos++;
  593. }
  594. return 0;
  595. fail:
  596. wpa_printf(MSG_DEBUG, "DPP: Invalid URI channel-list");
  597. return -1;
  598. }
  599. int dpp_parse_uri_mac(struct dpp_bootstrap_info *bi, const char *mac)
  600. {
  601. if (!mac)
  602. return 0;
  603. if (hwaddr_aton2(mac, bi->mac_addr) < 0) {
  604. wpa_printf(MSG_DEBUG, "DPP: Invalid URI mac");
  605. return -1;
  606. }
  607. wpa_printf(MSG_DEBUG, "DPP: URI mac: " MACSTR, MAC2STR(bi->mac_addr));
  608. return 0;
  609. }
  610. int dpp_parse_uri_info(struct dpp_bootstrap_info *bi, const char *info)
  611. {
  612. const char *end;
  613. if (!info)
  614. return 0;
  615. end = os_strchr(info, ';');
  616. if (!end)
  617. end = info + os_strlen(info);
  618. bi->info = os_malloc(end - info + 1);
  619. if (!bi->info)
  620. return -1;
  621. os_memcpy(bi->info, info, end - info);
  622. bi->info[end - info] = '\0';
  623. wpa_printf(MSG_DEBUG, "DPP: URI(information): %s", bi->info);
  624. if (!dpp_uri_valid_info(bi->info)) {
  625. wpa_printf(MSG_DEBUG, "DPP: Invalid URI information payload");
  626. return -1;
  627. }
  628. return 0;
  629. }
  630. static const struct dpp_curve_params *
  631. dpp_get_curve_oid(const ASN1_OBJECT *poid)
  632. {
  633. ASN1_OBJECT *oid;
  634. int i;
  635. for (i = 0; dpp_curves[i].name; i++) {
  636. oid = OBJ_txt2obj(dpp_curves[i].name, 0);
  637. if (oid && OBJ_cmp(poid, oid) == 0)
  638. return &dpp_curves[i];
  639. }
  640. return NULL;
  641. }
  642. static const struct dpp_curve_params * dpp_get_curve_nid(int nid)
  643. {
  644. int i, tmp;
  645. if (!nid)
  646. return NULL;
  647. for (i = 0; dpp_curves[i].name; i++) {
  648. tmp = OBJ_txt2nid(dpp_curves[i].name);
  649. if (tmp == nid)
  650. return &dpp_curves[i];
  651. }
  652. return NULL;
  653. }
  654. static int dpp_parse_uri_pk(struct dpp_bootstrap_info *bi, const char *info)
  655. {
  656. const char *end;
  657. u8 *data;
  658. size_t data_len;
  659. EVP_PKEY *pkey;
  660. const unsigned char *p;
  661. int res;
  662. X509_PUBKEY *pub = NULL;
  663. ASN1_OBJECT *ppkalg;
  664. const unsigned char *pk;
  665. int ppklen;
  666. X509_ALGOR *pa;
  667. #if OPENSSL_VERSION_NUMBER < 0x10100000L
  668. ASN1_OBJECT *pa_oid;
  669. #else
  670. const ASN1_OBJECT *pa_oid;
  671. #endif
  672. const void *pval;
  673. int ptype;
  674. const ASN1_OBJECT *poid;
  675. char buf[100];
  676. end = os_strchr(info, ';');
  677. if (!end)
  678. return -1;
  679. data = base64_decode((const unsigned char *) info, end - info,
  680. &data_len);
  681. if (!data) {
  682. wpa_printf(MSG_DEBUG,
  683. "DPP: Invalid base64 encoding on URI public-key");
  684. return -1;
  685. }
  686. wpa_hexdump(MSG_DEBUG, "DPP: Base64 decoded URI public-key",
  687. data, data_len);
  688. if (sha256_vector(1, (const u8 **) &data, &data_len,
  689. bi->pubkey_hash) < 0) {
  690. wpa_printf(MSG_DEBUG, "DPP: Failed to hash public key");
  691. return -1;
  692. }
  693. wpa_hexdump(MSG_DEBUG, "DPP: Public key hash",
  694. bi->pubkey_hash, SHA256_MAC_LEN);
  695. /* DER encoded ASN.1 SubjectPublicKeyInfo
  696. *
  697. * SubjectPublicKeyInfo ::= SEQUENCE {
  698. * algorithm AlgorithmIdentifier,
  699. * subjectPublicKey BIT STRING }
  700. *
  701. * AlgorithmIdentifier ::= SEQUENCE {
  702. * algorithm OBJECT IDENTIFIER,
  703. * parameters ANY DEFINED BY algorithm OPTIONAL }
  704. *
  705. * subjectPublicKey = compressed format public key per ANSI X9.63
  706. * algorithm = ecPublicKey (1.2.840.10045.2.1)
  707. * parameters = shall be present and shall be OBJECT IDENTIFIER; e.g.,
  708. * prime256v1 (1.2.840.10045.3.1.7)
  709. */
  710. p = data;
  711. pkey = d2i_PUBKEY(NULL, &p, data_len);
  712. os_free(data);
  713. if (!pkey) {
  714. wpa_printf(MSG_DEBUG,
  715. "DPP: Could not parse URI public-key SubjectPublicKeyInfo");
  716. return -1;
  717. }
  718. if (EVP_PKEY_type(EVP_PKEY_id(pkey)) != EVP_PKEY_EC) {
  719. wpa_printf(MSG_DEBUG,
  720. "DPP: SubjectPublicKeyInfo does not describe an EC key");
  721. EVP_PKEY_free(pkey);
  722. return -1;
  723. }
  724. res = X509_PUBKEY_set(&pub, pkey);
  725. if (res != 1) {
  726. wpa_printf(MSG_DEBUG, "DPP: Could not set pubkey");
  727. goto fail;
  728. }
  729. res = X509_PUBKEY_get0_param(&ppkalg, &pk, &ppklen, &pa, pub);
  730. if (res != 1) {
  731. wpa_printf(MSG_DEBUG,
  732. "DPP: Could not extract SubjectPublicKeyInfo parameters");
  733. goto fail;
  734. }
  735. res = OBJ_obj2txt(buf, sizeof(buf), ppkalg, 0);
  736. if (res < 0 || (size_t) res >= sizeof(buf)) {
  737. wpa_printf(MSG_DEBUG,
  738. "DPP: Could not extract SubjectPublicKeyInfo algorithm");
  739. goto fail;
  740. }
  741. wpa_printf(MSG_DEBUG, "DPP: URI subjectPublicKey algorithm: %s", buf);
  742. if (os_strcmp(buf, "id-ecPublicKey") != 0) {
  743. wpa_printf(MSG_DEBUG,
  744. "DPP: Unsupported SubjectPublicKeyInfo algorithm");
  745. goto fail;
  746. }
  747. X509_ALGOR_get0(&pa_oid, &ptype, (void *) &pval, pa);
  748. if (ptype != V_ASN1_OBJECT) {
  749. wpa_printf(MSG_DEBUG,
  750. "DPP: SubjectPublicKeyInfo parameters did not contain an OID");
  751. goto fail;
  752. }
  753. poid = pval;
  754. res = OBJ_obj2txt(buf, sizeof(buf), poid, 0);
  755. if (res < 0 || (size_t) res >= sizeof(buf)) {
  756. wpa_printf(MSG_DEBUG,
  757. "DPP: Could not extract SubjectPublicKeyInfo parameters OID");
  758. goto fail;
  759. }
  760. wpa_printf(MSG_DEBUG, "DPP: URI subjectPublicKey parameters: %s", buf);
  761. bi->curve = dpp_get_curve_oid(poid);
  762. if (!bi->curve) {
  763. wpa_printf(MSG_DEBUG,
  764. "DPP: Unsupported SubjectPublicKeyInfo curve: %s",
  765. buf);
  766. goto fail;
  767. }
  768. wpa_hexdump(MSG_DEBUG, "DPP: URI subjectPublicKey", pk, ppklen);
  769. X509_PUBKEY_free(pub);
  770. bi->pubkey = pkey;
  771. return 0;
  772. fail:
  773. X509_PUBKEY_free(pub);
  774. EVP_PKEY_free(pkey);
  775. return -1;
  776. }
  777. static struct dpp_bootstrap_info * dpp_parse_uri(const char *uri)
  778. {
  779. const char *pos = uri;
  780. const char *end;
  781. const char *chan_list = NULL, *mac = NULL, *info = NULL, *pk = NULL;
  782. struct dpp_bootstrap_info *bi;
  783. wpa_hexdump_ascii(MSG_DEBUG, "DPP: URI", uri, os_strlen(uri));
  784. if (os_strncmp(pos, "DPP:", 4) != 0) {
  785. wpa_printf(MSG_INFO, "DPP: Not a DPP URI");
  786. return NULL;
  787. }
  788. pos += 4;
  789. for (;;) {
  790. end = os_strchr(pos, ';');
  791. if (!end)
  792. break;
  793. if (end == pos) {
  794. /* Handle terminating ";;" and ignore unexpected ";"
  795. * for parsing robustness. */
  796. pos++;
  797. continue;
  798. }
  799. if (pos[0] == 'C' && pos[1] == ':' && !chan_list)
  800. chan_list = pos + 2;
  801. else if (pos[0] == 'M' && pos[1] == ':' && !mac)
  802. mac = pos + 2;
  803. else if (pos[0] == 'I' && pos[1] == ':' && !info)
  804. info = pos + 2;
  805. else if (pos[0] == 'K' && pos[1] == ':' && !pk)
  806. pk = pos + 2;
  807. else
  808. wpa_hexdump_ascii(MSG_DEBUG,
  809. "DPP: Ignore unrecognized URI parameter",
  810. pos, end - pos);
  811. pos = end + 1;
  812. }
  813. if (!pk) {
  814. wpa_printf(MSG_INFO, "DPP: URI missing public-key");
  815. return NULL;
  816. }
  817. bi = os_zalloc(sizeof(*bi));
  818. if (!bi)
  819. return NULL;
  820. if (dpp_clone_uri(bi, uri) < 0 ||
  821. dpp_parse_uri_chan_list(bi, chan_list) < 0 ||
  822. dpp_parse_uri_mac(bi, mac) < 0 ||
  823. dpp_parse_uri_info(bi, info) < 0 ||
  824. dpp_parse_uri_pk(bi, pk) < 0) {
  825. dpp_bootstrap_info_free(bi);
  826. bi = NULL;
  827. }
  828. return bi;
  829. }
  830. struct dpp_bootstrap_info * dpp_parse_qr_code(const char *uri)
  831. {
  832. struct dpp_bootstrap_info *bi;
  833. bi = dpp_parse_uri(uri);
  834. if (bi)
  835. bi->type = DPP_BOOTSTRAP_QR_CODE;
  836. return bi;
  837. }
  838. static void dpp_debug_print_key(const char *title, EVP_PKEY *key)
  839. {
  840. EC_KEY *eckey;
  841. BIO *out;
  842. size_t rlen;
  843. char *txt;
  844. int res;
  845. unsigned char *der = NULL;
  846. int der_len;
  847. out = BIO_new(BIO_s_mem());
  848. if (!out)
  849. return;
  850. EVP_PKEY_print_private(out, key, 0, NULL);
  851. rlen = BIO_ctrl_pending(out);
  852. txt = os_malloc(rlen + 1);
  853. if (txt) {
  854. res = BIO_read(out, txt, rlen);
  855. if (res > 0) {
  856. txt[res] = '\0';
  857. wpa_printf(MSG_DEBUG, "%s: %s", title, txt);
  858. }
  859. os_free(txt);
  860. }
  861. BIO_free(out);
  862. eckey = EVP_PKEY_get1_EC_KEY(key);
  863. if (!eckey)
  864. return;
  865. der_len = i2d_ECPrivateKey(eckey, &der);
  866. if (der_len > 0)
  867. wpa_hexdump_key(MSG_DEBUG, "DPP: ECPrivateKey", der, der_len);
  868. OPENSSL_free(der);
  869. if (der_len <= 0) {
  870. der = NULL;
  871. der_len = i2d_EC_PUBKEY(eckey, &der);
  872. if (der_len > 0)
  873. wpa_hexdump(MSG_DEBUG, "DPP: EC_PUBKEY", der, der_len);
  874. OPENSSL_free(der);
  875. }
  876. EC_KEY_free(eckey);
  877. }
  878. static EVP_PKEY * dpp_gen_keypair(const struct dpp_curve_params *curve)
  879. {
  880. #ifdef OPENSSL_IS_BORINGSSL
  881. EVP_PKEY_CTX *kctx = NULL;
  882. const EC_GROUP *group;
  883. EC_KEY *ec_params;
  884. #else
  885. EVP_PKEY_CTX *pctx, *kctx = NULL;
  886. #endif
  887. EVP_PKEY *params = NULL, *key = NULL;
  888. int nid;
  889. wpa_printf(MSG_DEBUG, "DPP: Generating a keypair");
  890. nid = OBJ_txt2nid(curve->name);
  891. if (nid == NID_undef) {
  892. wpa_printf(MSG_INFO, "DPP: Unsupported curve %s", curve->name);
  893. return NULL;
  894. }
  895. #ifdef OPENSSL_IS_BORINGSSL
  896. group = EC_GROUP_new_by_curve_name(nid);
  897. ec_params = EC_KEY_new();
  898. if (!ec_params || EC_KEY_set_group(ec_params, group) != 1) {
  899. wpa_printf(MSG_ERROR,
  900. "DPP: Failed to generate EC_KEY parameters");
  901. goto fail;
  902. }
  903. EC_KEY_set_asn1_flag(ec_params, OPENSSL_EC_NAMED_CURVE);
  904. params = EVP_PKEY_new();
  905. if (!params || EVP_PKEY_set1_EC_KEY(params, ec_params) != 1) {
  906. wpa_printf(MSG_ERROR,
  907. "DPP: Failed to generate EVP_PKEY parameters");
  908. goto fail;
  909. }
  910. #else
  911. pctx = EVP_PKEY_CTX_new_id(EVP_PKEY_EC, NULL);
  912. if (!pctx ||
  913. EVP_PKEY_paramgen_init(pctx) != 1 ||
  914. EVP_PKEY_CTX_set_ec_paramgen_curve_nid(pctx, nid) != 1 ||
  915. EVP_PKEY_CTX_set_ec_param_enc(pctx, OPENSSL_EC_NAMED_CURVE) != 1 ||
  916. EVP_PKEY_paramgen(pctx, &params) != 1) {
  917. wpa_printf(MSG_ERROR,
  918. "DPP: Failed to generate EVP_PKEY parameters");
  919. EVP_PKEY_CTX_free(pctx);
  920. goto fail;
  921. }
  922. EVP_PKEY_CTX_free(pctx);
  923. #endif
  924. kctx = EVP_PKEY_CTX_new(params, NULL);
  925. if (!kctx ||
  926. EVP_PKEY_keygen_init(kctx) != 1 ||
  927. EVP_PKEY_keygen(kctx, &key) != 1) {
  928. wpa_printf(MSG_ERROR, "DPP: Failed to generate EC key");
  929. goto fail;
  930. }
  931. if (wpa_debug_show_keys)
  932. dpp_debug_print_key("Own generated key", key);
  933. EVP_PKEY_free(params);
  934. EVP_PKEY_CTX_free(kctx);
  935. return key;
  936. fail:
  937. EVP_PKEY_CTX_free(kctx);
  938. EVP_PKEY_free(params);
  939. return NULL;
  940. }
  941. static const struct dpp_curve_params *
  942. dpp_get_curve_name(const char *name)
  943. {
  944. int i;
  945. for (i = 0; dpp_curves[i].name; i++) {
  946. if (os_strcmp(name, dpp_curves[i].name) == 0 ||
  947. (dpp_curves[i].jwk_crv &&
  948. os_strcmp(name, dpp_curves[i].jwk_crv) == 0))
  949. return &dpp_curves[i];
  950. }
  951. return NULL;
  952. }
  953. static const struct dpp_curve_params *
  954. dpp_get_curve_jwk_crv(const char *name)
  955. {
  956. int i;
  957. for (i = 0; dpp_curves[i].name; i++) {
  958. if (dpp_curves[i].jwk_crv &&
  959. os_strcmp(name, dpp_curves[i].jwk_crv) == 0)
  960. return &dpp_curves[i];
  961. }
  962. return NULL;
  963. }
  964. static EVP_PKEY * dpp_set_keypair(const struct dpp_curve_params **curve,
  965. const u8 *privkey, size_t privkey_len)
  966. {
  967. EVP_PKEY *pkey;
  968. EC_KEY *eckey;
  969. const EC_GROUP *group;
  970. int nid;
  971. pkey = EVP_PKEY_new();
  972. if (!pkey)
  973. return NULL;
  974. eckey = d2i_ECPrivateKey(NULL, &privkey, privkey_len);
  975. if (!eckey) {
  976. wpa_printf(MSG_INFO,
  977. "DPP: OpenSSL: d2i_ECPrivateKey() failed: %s",
  978. ERR_error_string(ERR_get_error(), NULL));
  979. EVP_PKEY_free(pkey);
  980. return NULL;
  981. }
  982. group = EC_KEY_get0_group(eckey);
  983. if (!group) {
  984. EC_KEY_free(eckey);
  985. EVP_PKEY_free(pkey);
  986. return NULL;
  987. }
  988. nid = EC_GROUP_get_curve_name(group);
  989. *curve = dpp_get_curve_nid(nid);
  990. if (!*curve) {
  991. wpa_printf(MSG_INFO,
  992. "DPP: Unsupported curve (nid=%d) in pre-assigned key",
  993. nid);
  994. EC_KEY_free(eckey);
  995. EVP_PKEY_free(pkey);
  996. return NULL;
  997. }
  998. if (EVP_PKEY_assign_EC_KEY(pkey, eckey) != 1) {
  999. EC_KEY_free(eckey);
  1000. EVP_PKEY_free(pkey);
  1001. return NULL;
  1002. }
  1003. return pkey;
  1004. }
  1005. int dpp_bootstrap_key_hash(struct dpp_bootstrap_info *bi)
  1006. {
  1007. unsigned char *der = NULL;
  1008. int der_len;
  1009. EC_KEY *eckey;
  1010. int res;
  1011. size_t len;
  1012. /* Need to get the compressed form of the public key through EC_KEY, so
  1013. * cannot use the simpler i2d_PUBKEY() here. */
  1014. eckey = EVP_PKEY_get1_EC_KEY(bi->pubkey);
  1015. if (!eckey)
  1016. return -1;
  1017. EC_KEY_set_conv_form(eckey, POINT_CONVERSION_COMPRESSED);
  1018. der_len = i2d_EC_PUBKEY(eckey, &der);
  1019. EC_KEY_free(eckey);
  1020. if (der_len <= 0) {
  1021. wpa_printf(MSG_ERROR,
  1022. "DDP: Failed to build DER encoded public key");
  1023. OPENSSL_free(der);
  1024. return -1;
  1025. }
  1026. len = der_len;
  1027. res = sha256_vector(1, (const u8 **) &der, &len, bi->pubkey_hash);
  1028. OPENSSL_free(der);
  1029. if (res < 0)
  1030. wpa_printf(MSG_DEBUG, "DPP: Failed to hash public key");
  1031. return res;
  1032. }
  1033. char * dpp_keygen(struct dpp_bootstrap_info *bi, const char *curve,
  1034. const u8 *privkey, size_t privkey_len)
  1035. {
  1036. unsigned char *base64 = NULL;
  1037. char *pos, *end;
  1038. size_t len;
  1039. unsigned char *der = NULL;
  1040. int der_len;
  1041. EC_KEY *eckey;
  1042. if (!curve) {
  1043. bi->curve = &dpp_curves[0];
  1044. } else {
  1045. bi->curve = dpp_get_curve_name(curve);
  1046. if (!bi->curve) {
  1047. wpa_printf(MSG_INFO, "DPP: Unsupported curve: %s",
  1048. curve);
  1049. return NULL;
  1050. }
  1051. }
  1052. if (privkey)
  1053. bi->pubkey = dpp_set_keypair(&bi->curve, privkey, privkey_len);
  1054. else
  1055. bi->pubkey = dpp_gen_keypair(bi->curve);
  1056. if (!bi->pubkey)
  1057. goto fail;
  1058. bi->own = 1;
  1059. /* Need to get the compressed form of the public key through EC_KEY, so
  1060. * cannot use the simpler i2d_PUBKEY() here. */
  1061. eckey = EVP_PKEY_get1_EC_KEY(bi->pubkey);
  1062. if (!eckey)
  1063. goto fail;
  1064. EC_KEY_set_conv_form(eckey, POINT_CONVERSION_COMPRESSED);
  1065. der_len = i2d_EC_PUBKEY(eckey, &der);
  1066. EC_KEY_free(eckey);
  1067. if (der_len <= 0) {
  1068. wpa_printf(MSG_ERROR,
  1069. "DDP: Failed to build DER encoded public key");
  1070. goto fail;
  1071. }
  1072. len = der_len;
  1073. if (sha256_vector(1, (const u8 **) &der, &len, bi->pubkey_hash) < 0) {
  1074. wpa_printf(MSG_DEBUG, "DPP: Failed to hash public key");
  1075. goto fail;
  1076. }
  1077. base64 = base64_encode(der, der_len, &len);
  1078. OPENSSL_free(der);
  1079. der = NULL;
  1080. if (!base64)
  1081. goto fail;
  1082. pos = (char *) base64;
  1083. end = pos + len;
  1084. for (;;) {
  1085. pos = os_strchr(pos, '\n');
  1086. if (!pos)
  1087. break;
  1088. os_memmove(pos, pos + 1, end - pos);
  1089. }
  1090. return (char *) base64;
  1091. fail:
  1092. os_free(base64);
  1093. OPENSSL_free(der);
  1094. return NULL;
  1095. }
  1096. static int dpp_derive_k1(const u8 *Mx, size_t Mx_len, u8 *k1,
  1097. unsigned int hash_len)
  1098. {
  1099. u8 salt[DPP_MAX_HASH_LEN], prk[DPP_MAX_HASH_LEN];
  1100. const char *info = "first intermediate key";
  1101. int res;
  1102. /* k1 = HKDF(<>, "first intermediate key", M.x) */
  1103. /* HKDF-Extract(<>, M.x) */
  1104. os_memset(salt, 0, hash_len);
  1105. if (dpp_hmac(hash_len, salt, hash_len, Mx, Mx_len, prk) < 0)
  1106. return -1;
  1107. wpa_hexdump_key(MSG_DEBUG, "DPP: PRK = HKDF-Extract(<>, IKM=M.x)",
  1108. prk, hash_len);
  1109. /* HKDF-Expand(PRK, info, L) */
  1110. res = dpp_hkdf_expand(hash_len, prk, hash_len, info, k1, hash_len);
  1111. os_memset(prk, 0, hash_len);
  1112. if (res < 0)
  1113. return -1;
  1114. wpa_hexdump_key(MSG_DEBUG, "DPP: k1 = HKDF-Expand(PRK, info, L)",
  1115. k1, hash_len);
  1116. return 0;
  1117. }
  1118. static int dpp_derive_k2(const u8 *Nx, size_t Nx_len, u8 *k2,
  1119. unsigned int hash_len)
  1120. {
  1121. u8 salt[DPP_MAX_HASH_LEN], prk[DPP_MAX_HASH_LEN];
  1122. const char *info = "second intermediate key";
  1123. int res;
  1124. /* k2 = HKDF(<>, "second intermediate key", N.x) */
  1125. /* HKDF-Extract(<>, N.x) */
  1126. os_memset(salt, 0, hash_len);
  1127. res = dpp_hmac(hash_len, salt, hash_len, Nx, Nx_len, prk);
  1128. if (res < 0)
  1129. return -1;
  1130. wpa_hexdump_key(MSG_DEBUG, "DPP: PRK = HKDF-Extract(<>, IKM=N.x)",
  1131. prk, hash_len);
  1132. /* HKDF-Expand(PRK, info, L) */
  1133. res = dpp_hkdf_expand(hash_len, prk, hash_len, info, k2, hash_len);
  1134. os_memset(prk, 0, hash_len);
  1135. if (res < 0)
  1136. return -1;
  1137. wpa_hexdump_key(MSG_DEBUG, "DPP: k2 = HKDF-Expand(PRK, info, L)",
  1138. k2, hash_len);
  1139. return 0;
  1140. }
  1141. static int dpp_derive_ke(struct dpp_authentication *auth, u8 *ke,
  1142. unsigned int hash_len)
  1143. {
  1144. size_t nonce_len;
  1145. u8 nonces[2 * DPP_MAX_NONCE_LEN];
  1146. const char *info_ke = "DPP Key";
  1147. u8 prk[DPP_MAX_HASH_LEN];
  1148. int res;
  1149. const u8 *addr[3];
  1150. size_t len[3];
  1151. size_t num_elem = 0;
  1152. /* ke = HKDF(I-nonce | R-nonce, "DPP Key", M.x | N.x [| L.x]) */
  1153. /* HKDF-Extract(I-nonce | R-nonce, M.x | N.x [| L.x]) */
  1154. nonce_len = auth->curve->nonce_len;
  1155. os_memcpy(nonces, auth->i_nonce, nonce_len);
  1156. os_memcpy(&nonces[nonce_len], auth->r_nonce, nonce_len);
  1157. addr[num_elem] = auth->Mx;
  1158. len[num_elem] = auth->secret_len;
  1159. num_elem++;
  1160. addr[num_elem] = auth->Nx;
  1161. len[num_elem] = auth->secret_len;
  1162. num_elem++;
  1163. if (auth->peer_bi && auth->own_bi) {
  1164. addr[num_elem] = auth->Lx;
  1165. len[num_elem] = auth->secret_len;
  1166. num_elem++;
  1167. }
  1168. res = dpp_hmac_vector(hash_len, nonces, 2 * nonce_len,
  1169. num_elem, addr, len, prk);
  1170. if (res < 0)
  1171. return -1;
  1172. wpa_hexdump_key(MSG_DEBUG, "DPP: PRK = HKDF-Extract(<>, IKM)",
  1173. prk, hash_len);
  1174. /* HKDF-Expand(PRK, info, L) */
  1175. res = dpp_hkdf_expand(hash_len, prk, hash_len, info_ke, ke, hash_len);
  1176. os_memset(prk, 0, hash_len);
  1177. if (res < 0)
  1178. return -1;
  1179. wpa_hexdump_key(MSG_DEBUG, "DPP: ke = HKDF-Expand(PRK, info, L)",
  1180. ke, hash_len);
  1181. return 0;
  1182. }
  1183. static struct wpabuf * dpp_auth_build_req(struct dpp_authentication *auth,
  1184. const struct wpabuf *pi,
  1185. size_t nonce_len,
  1186. const u8 *r_pubkey_hash,
  1187. const u8 *i_pubkey_hash,
  1188. unsigned int neg_freq)
  1189. {
  1190. struct wpabuf *msg;
  1191. u8 clear[4 + DPP_MAX_NONCE_LEN + 4 + 1];
  1192. u8 wrapped_data[4 + DPP_MAX_NONCE_LEN + 4 + 1 + AES_BLOCK_SIZE];
  1193. u8 *pos;
  1194. const u8 *addr[2];
  1195. size_t len[2], siv_len, attr_len;
  1196. u8 *attr_start, *attr_end;
  1197. /* Build DPP Authentication Request frame attributes */
  1198. attr_len = 2 * (4 + SHA256_MAC_LEN) + 4 + (pi ? wpabuf_len(pi) : 0) +
  1199. 4 + sizeof(wrapped_data);
  1200. if (neg_freq > 0)
  1201. attr_len += 4 + 2;
  1202. #ifdef CONFIG_TESTING_OPTIONS
  1203. if (dpp_test == DPP_TEST_AFTER_WRAPPED_DATA_AUTH_REQ)
  1204. attr_len += 4;
  1205. #endif /* CONFIG_TESTING_OPTIONS */
  1206. msg = dpp_alloc_msg(DPP_PA_AUTHENTICATION_REQ, attr_len);
  1207. if (!msg)
  1208. return NULL;
  1209. attr_start = wpabuf_put(msg, 0);
  1210. /* Responder Bootstrapping Key Hash */
  1211. if (r_pubkey_hash) {
  1212. wpabuf_put_le16(msg, DPP_ATTR_R_BOOTSTRAP_KEY_HASH);
  1213. wpabuf_put_le16(msg, SHA256_MAC_LEN);
  1214. wpabuf_put_data(msg, r_pubkey_hash, SHA256_MAC_LEN);
  1215. }
  1216. /* Initiator Bootstrapping Key Hash */
  1217. if (i_pubkey_hash) {
  1218. wpabuf_put_le16(msg, DPP_ATTR_I_BOOTSTRAP_KEY_HASH);
  1219. wpabuf_put_le16(msg, SHA256_MAC_LEN);
  1220. wpabuf_put_data(msg, i_pubkey_hash, SHA256_MAC_LEN);
  1221. }
  1222. /* Initiator Protocol Key */
  1223. if (pi) {
  1224. wpabuf_put_le16(msg, DPP_ATTR_I_PROTOCOL_KEY);
  1225. wpabuf_put_le16(msg, wpabuf_len(pi));
  1226. wpabuf_put_buf(msg, pi);
  1227. }
  1228. /* Channel */
  1229. if (neg_freq > 0) {
  1230. u8 op_class, channel;
  1231. if (ieee80211_freq_to_channel_ext(neg_freq, 0, 0, &op_class,
  1232. &channel) ==
  1233. NUM_HOSTAPD_MODES) {
  1234. wpa_printf(MSG_INFO,
  1235. "DPP: Unsupported negotiation frequency request: %d",
  1236. neg_freq);
  1237. wpabuf_free(msg);
  1238. return NULL;
  1239. }
  1240. wpabuf_put_le16(msg, DPP_ATTR_CHANNEL);
  1241. wpabuf_put_le16(msg, 2);
  1242. wpabuf_put_u8(msg, op_class);
  1243. wpabuf_put_u8(msg, channel);
  1244. }
  1245. #ifdef CONFIG_TESTING_OPTIONS
  1246. if (dpp_test == DPP_TEST_NO_WRAPPED_DATA_AUTH_REQ) {
  1247. wpa_printf(MSG_INFO, "DPP: TESTING - no Wrapped Data");
  1248. goto skip_wrapped_data;
  1249. }
  1250. #endif /* CONFIG_TESTING_OPTIONS */
  1251. /* Wrapped data ({I-nonce, I-capabilities}k1) */
  1252. pos = clear;
  1253. #ifdef CONFIG_TESTING_OPTIONS
  1254. if (dpp_test == DPP_TEST_NO_I_NONCE_AUTH_REQ) {
  1255. wpa_printf(MSG_INFO, "DPP: TESTING - no I-nonce");
  1256. goto skip_i_nonce;
  1257. }
  1258. #endif /* CONFIG_TESTING_OPTIONS */
  1259. /* I-nonce */
  1260. WPA_PUT_LE16(pos, DPP_ATTR_I_NONCE);
  1261. pos += 2;
  1262. WPA_PUT_LE16(pos, nonce_len);
  1263. pos += 2;
  1264. os_memcpy(pos, auth->i_nonce, nonce_len);
  1265. pos += nonce_len;
  1266. #ifdef CONFIG_TESTING_OPTIONS
  1267. skip_i_nonce:
  1268. if (dpp_test == DPP_TEST_NO_I_CAPAB_AUTH_REQ) {
  1269. wpa_printf(MSG_INFO, "DPP: TESTING - no I-capab");
  1270. goto skip_i_capab;
  1271. }
  1272. #endif /* CONFIG_TESTING_OPTIONS */
  1273. /* I-capabilities */
  1274. WPA_PUT_LE16(pos, DPP_ATTR_I_CAPABILITIES);
  1275. pos += 2;
  1276. WPA_PUT_LE16(pos, 1);
  1277. pos += 2;
  1278. auth->i_capab = auth->allowed_roles;
  1279. *pos++ = auth->i_capab;
  1280. #ifdef CONFIG_TESTING_OPTIONS
  1281. if (dpp_test == DPP_TEST_ZERO_I_CAPAB) {
  1282. wpa_printf(MSG_INFO, "DPP: TESTING - zero I-capabilities");
  1283. pos[-1] = 0;
  1284. }
  1285. skip_i_capab:
  1286. #endif /* CONFIG_TESTING_OPTIONS */
  1287. attr_end = wpabuf_put(msg, 0);
  1288. /* OUI, OUI type, Crypto Suite, DPP frame type */
  1289. addr[0] = wpabuf_head_u8(msg) + 2;
  1290. len[0] = 3 + 1 + 1 + 1;
  1291. wpa_hexdump(MSG_DEBUG, "DDP: AES-SIV AD[0]", addr[0], len[0]);
  1292. /* Attributes before Wrapped Data */
  1293. addr[1] = attr_start;
  1294. len[1] = attr_end - attr_start;
  1295. wpa_hexdump(MSG_DEBUG, "DDP: AES-SIV AD[1]", addr[1], len[1]);
  1296. siv_len = pos - clear;
  1297. wpa_hexdump(MSG_DEBUG, "DPP: AES-SIV cleartext", clear, siv_len);
  1298. if (aes_siv_encrypt(auth->k1, auth->curve->hash_len, clear, siv_len,
  1299. 2, addr, len, wrapped_data) < 0) {
  1300. wpabuf_free(msg);
  1301. return NULL;
  1302. }
  1303. siv_len += AES_BLOCK_SIZE;
  1304. wpa_hexdump(MSG_DEBUG, "DPP: AES-SIV ciphertext",
  1305. wrapped_data, siv_len);
  1306. wpabuf_put_le16(msg, DPP_ATTR_WRAPPED_DATA);
  1307. wpabuf_put_le16(msg, siv_len);
  1308. wpabuf_put_data(msg, wrapped_data, siv_len);
  1309. #ifdef CONFIG_TESTING_OPTIONS
  1310. if (dpp_test == DPP_TEST_AFTER_WRAPPED_DATA_AUTH_REQ) {
  1311. wpa_printf(MSG_INFO, "DPP: TESTING - attr after Wrapped Data");
  1312. wpabuf_put_le16(msg, DPP_ATTR_TESTING);
  1313. wpabuf_put_le16(msg, 0);
  1314. }
  1315. skip_wrapped_data:
  1316. #endif /* CONFIG_TESTING_OPTIONS */
  1317. wpa_hexdump_buf(MSG_DEBUG,
  1318. "DPP: Authentication Request frame attributes", msg);
  1319. return msg;
  1320. }
  1321. static struct wpabuf * dpp_auth_build_resp(struct dpp_authentication *auth,
  1322. enum dpp_status_error status,
  1323. const struct wpabuf *pr,
  1324. size_t nonce_len,
  1325. const u8 *r_pubkey_hash,
  1326. const u8 *i_pubkey_hash,
  1327. const u8 *r_nonce, const u8 *i_nonce,
  1328. const u8 *wrapped_r_auth,
  1329. size_t wrapped_r_auth_len,
  1330. const u8 *siv_key)
  1331. {
  1332. struct wpabuf *msg;
  1333. #define DPP_AUTH_RESP_CLEAR_LEN 2 * (4 + DPP_MAX_NONCE_LEN) + 4 + 1 + \
  1334. 4 + 4 + DPP_MAX_HASH_LEN + AES_BLOCK_SIZE
  1335. u8 clear[DPP_AUTH_RESP_CLEAR_LEN];
  1336. u8 wrapped_data[DPP_AUTH_RESP_CLEAR_LEN + AES_BLOCK_SIZE];
  1337. const u8 *addr[2];
  1338. size_t len[2], siv_len, attr_len;
  1339. u8 *attr_start, *attr_end, *pos;
  1340. auth->waiting_auth_conf = 1;
  1341. auth->auth_resp_tries = 0;
  1342. /* Build DPP Authentication Response frame attributes */
  1343. attr_len = 4 + 1 + 2 * (4 + SHA256_MAC_LEN) +
  1344. 4 + (pr ? wpabuf_len(pr) : 0) + 4 + sizeof(wrapped_data);
  1345. #ifdef CONFIG_TESTING_OPTIONS
  1346. if (dpp_test == DPP_TEST_AFTER_WRAPPED_DATA_AUTH_RESP)
  1347. attr_len += 4;
  1348. #endif /* CONFIG_TESTING_OPTIONS */
  1349. msg = dpp_alloc_msg(DPP_PA_AUTHENTICATION_RESP, attr_len);
  1350. if (!msg)
  1351. return NULL;
  1352. attr_start = wpabuf_put(msg, 0);
  1353. /* DPP Status */
  1354. if (status != 255) {
  1355. wpa_printf(MSG_DEBUG, "DPP: Status %d", status);
  1356. wpabuf_put_le16(msg, DPP_ATTR_STATUS);
  1357. wpabuf_put_le16(msg, 1);
  1358. wpabuf_put_u8(msg, status);
  1359. }
  1360. /* Responder Bootstrapping Key Hash */
  1361. if (r_pubkey_hash) {
  1362. wpabuf_put_le16(msg, DPP_ATTR_R_BOOTSTRAP_KEY_HASH);
  1363. wpabuf_put_le16(msg, SHA256_MAC_LEN);
  1364. wpabuf_put_data(msg, r_pubkey_hash, SHA256_MAC_LEN);
  1365. }
  1366. /* Initiator Bootstrapping Key Hash */
  1367. if (i_pubkey_hash) {
  1368. /* Mutual authentication */
  1369. wpabuf_put_le16(msg, DPP_ATTR_I_BOOTSTRAP_KEY_HASH);
  1370. wpabuf_put_le16(msg, SHA256_MAC_LEN);
  1371. wpabuf_put_data(msg, i_pubkey_hash, SHA256_MAC_LEN);
  1372. }
  1373. /* Responder Protocol Key */
  1374. if (pr) {
  1375. wpabuf_put_le16(msg, DPP_ATTR_R_PROTOCOL_KEY);
  1376. wpabuf_put_le16(msg, wpabuf_len(pr));
  1377. wpabuf_put_buf(msg, pr);
  1378. }
  1379. attr_end = wpabuf_put(msg, 0);
  1380. #ifdef CONFIG_TESTING_OPTIONS
  1381. if (dpp_test == DPP_TEST_NO_WRAPPED_DATA_AUTH_RESP) {
  1382. wpa_printf(MSG_INFO, "DPP: TESTING - no Wrapped Data");
  1383. goto skip_wrapped_data;
  1384. }
  1385. #endif /* CONFIG_TESTING_OPTIONS */
  1386. /* Wrapped data ({R-nonce, I-nonce, R-capabilities, {R-auth}ke}k2) */
  1387. pos = clear;
  1388. if (r_nonce) {
  1389. /* R-nonce */
  1390. WPA_PUT_LE16(pos, DPP_ATTR_R_NONCE);
  1391. pos += 2;
  1392. WPA_PUT_LE16(pos, nonce_len);
  1393. pos += 2;
  1394. os_memcpy(pos, r_nonce, nonce_len);
  1395. pos += nonce_len;
  1396. }
  1397. if (i_nonce) {
  1398. /* I-nonce */
  1399. WPA_PUT_LE16(pos, DPP_ATTR_I_NONCE);
  1400. pos += 2;
  1401. WPA_PUT_LE16(pos, nonce_len);
  1402. pos += 2;
  1403. os_memcpy(pos, i_nonce, nonce_len);
  1404. #ifdef CONFIG_TESTING_OPTIONS
  1405. if (dpp_test == DPP_TEST_I_NONCE_MISMATCH_AUTH_RESP) {
  1406. wpa_printf(MSG_INFO, "DPP: TESTING - I-nonce mismatch");
  1407. pos[nonce_len / 2] ^= 0x01;
  1408. }
  1409. #endif /* CONFIG_TESTING_OPTIONS */
  1410. pos += nonce_len;
  1411. }
  1412. #ifdef CONFIG_TESTING_OPTIONS
  1413. if (dpp_test == DPP_TEST_NO_R_CAPAB_AUTH_RESP) {
  1414. wpa_printf(MSG_INFO, "DPP: TESTING - no R-capab");
  1415. goto skip_r_capab;
  1416. }
  1417. #endif /* CONFIG_TESTING_OPTIONS */
  1418. /* R-capabilities */
  1419. WPA_PUT_LE16(pos, DPP_ATTR_R_CAPABILITIES);
  1420. pos += 2;
  1421. WPA_PUT_LE16(pos, 1);
  1422. pos += 2;
  1423. auth->r_capab = auth->configurator ? DPP_CAPAB_CONFIGURATOR :
  1424. DPP_CAPAB_ENROLLEE;
  1425. *pos++ = auth->r_capab;
  1426. #ifdef CONFIG_TESTING_OPTIONS
  1427. if (dpp_test == DPP_TEST_ZERO_R_CAPAB) {
  1428. wpa_printf(MSG_INFO, "DPP: TESTING - zero R-capabilities");
  1429. pos[-1] = 0;
  1430. } else if (dpp_test == DPP_TEST_INCOMPATIBLE_R_CAPAB_AUTH_RESP) {
  1431. wpa_printf(MSG_INFO,
  1432. "DPP: TESTING - incompatible R-capabilities");
  1433. if ((auth->i_capab & DPP_CAPAB_ROLE_MASK) ==
  1434. (DPP_CAPAB_CONFIGURATOR | DPP_CAPAB_ENROLLEE))
  1435. pos[-1] = 0;
  1436. else
  1437. pos[-1] = auth->configurator ? DPP_CAPAB_ENROLLEE :
  1438. DPP_CAPAB_CONFIGURATOR;
  1439. }
  1440. skip_r_capab:
  1441. #endif /* CONFIG_TESTING_OPTIONS */
  1442. if (wrapped_r_auth) {
  1443. /* {R-auth}ke */
  1444. WPA_PUT_LE16(pos, DPP_ATTR_WRAPPED_DATA);
  1445. pos += 2;
  1446. WPA_PUT_LE16(pos, wrapped_r_auth_len);
  1447. pos += 2;
  1448. os_memcpy(pos, wrapped_r_auth, wrapped_r_auth_len);
  1449. pos += wrapped_r_auth_len;
  1450. }
  1451. /* OUI, OUI type, Crypto Suite, DPP frame type */
  1452. addr[0] = wpabuf_head_u8(msg) + 2;
  1453. len[0] = 3 + 1 + 1 + 1;
  1454. wpa_hexdump(MSG_DEBUG, "DDP: AES-SIV AD[0]", addr[0], len[0]);
  1455. /* Attributes before Wrapped Data */
  1456. addr[1] = attr_start;
  1457. len[1] = attr_end - attr_start;
  1458. wpa_hexdump(MSG_DEBUG, "DDP: AES-SIV AD[1]", addr[1], len[1]);
  1459. siv_len = pos - clear;
  1460. wpa_hexdump(MSG_DEBUG, "DPP: AES-SIV cleartext", clear, siv_len);
  1461. if (aes_siv_encrypt(siv_key, auth->curve->hash_len, clear, siv_len,
  1462. 2, addr, len, wrapped_data) < 0) {
  1463. wpabuf_free(msg);
  1464. return NULL;
  1465. }
  1466. siv_len += AES_BLOCK_SIZE;
  1467. wpa_hexdump(MSG_DEBUG, "DPP: AES-SIV ciphertext",
  1468. wrapped_data, siv_len);
  1469. wpabuf_put_le16(msg, DPP_ATTR_WRAPPED_DATA);
  1470. wpabuf_put_le16(msg, siv_len);
  1471. wpabuf_put_data(msg, wrapped_data, siv_len);
  1472. #ifdef CONFIG_TESTING_OPTIONS
  1473. if (dpp_test == DPP_TEST_AFTER_WRAPPED_DATA_AUTH_RESP) {
  1474. wpa_printf(MSG_INFO, "DPP: TESTING - attr after Wrapped Data");
  1475. wpabuf_put_le16(msg, DPP_ATTR_TESTING);
  1476. wpabuf_put_le16(msg, 0);
  1477. }
  1478. skip_wrapped_data:
  1479. #endif /* CONFIG_TESTING_OPTIONS */
  1480. wpa_hexdump_buf(MSG_DEBUG,
  1481. "DPP: Authentication Response frame attributes", msg);
  1482. return msg;
  1483. }
  1484. static int dpp_channel_ok_init(struct hostapd_hw_modes *own_modes,
  1485. u16 num_modes, unsigned int freq)
  1486. {
  1487. u16 m;
  1488. int c, flag;
  1489. if (!own_modes || !num_modes)
  1490. return 1;
  1491. for (m = 0; m < num_modes; m++) {
  1492. for (c = 0; c < own_modes[m].num_channels; c++) {
  1493. if ((unsigned int) own_modes[m].channels[c].freq !=
  1494. freq)
  1495. continue;
  1496. flag = own_modes[m].channels[c].flag;
  1497. if (!(flag & (HOSTAPD_CHAN_DISABLED |
  1498. HOSTAPD_CHAN_NO_IR |
  1499. HOSTAPD_CHAN_RADAR)))
  1500. return 1;
  1501. }
  1502. }
  1503. wpa_printf(MSG_DEBUG, "DPP: Peer channel %u MHz not supported", freq);
  1504. return 0;
  1505. }
  1506. static int freq_included(const unsigned int freqs[], unsigned int num,
  1507. unsigned int freq)
  1508. {
  1509. while (num > 0) {
  1510. if (freqs[--num] == freq)
  1511. return 1;
  1512. }
  1513. return 0;
  1514. }
  1515. static void freq_to_start(unsigned int freqs[], unsigned int num,
  1516. unsigned int freq)
  1517. {
  1518. unsigned int i;
  1519. for (i = 0; i < num; i++) {
  1520. if (freqs[i] == freq)
  1521. break;
  1522. }
  1523. if (i == 0 || i >= num)
  1524. return;
  1525. os_memmove(&freqs[1], &freqs[0], i * sizeof(freqs[0]));
  1526. freqs[0] = freq;
  1527. }
  1528. static int dpp_channel_intersect(struct dpp_authentication *auth,
  1529. struct hostapd_hw_modes *own_modes,
  1530. u16 num_modes)
  1531. {
  1532. struct dpp_bootstrap_info *peer_bi = auth->peer_bi;
  1533. unsigned int i, freq;
  1534. for (i = 0; i < peer_bi->num_freq; i++) {
  1535. freq = peer_bi->freq[i];
  1536. if (freq_included(auth->freq, auth->num_freq, freq))
  1537. continue;
  1538. if (dpp_channel_ok_init(own_modes, num_modes, freq))
  1539. auth->freq[auth->num_freq++] = freq;
  1540. }
  1541. if (!auth->num_freq) {
  1542. wpa_printf(MSG_INFO,
  1543. "DPP: No available channels for initiating DPP Authentication");
  1544. return -1;
  1545. }
  1546. auth->curr_freq = auth->freq[0];
  1547. return 0;
  1548. }
  1549. static int dpp_channel_local_list(struct dpp_authentication *auth,
  1550. struct hostapd_hw_modes *own_modes,
  1551. u16 num_modes)
  1552. {
  1553. u16 m;
  1554. int c, flag;
  1555. unsigned int freq;
  1556. auth->num_freq = 0;
  1557. if (!own_modes || !num_modes) {
  1558. auth->freq[0] = 2412;
  1559. auth->freq[1] = 2437;
  1560. auth->freq[2] = 2462;
  1561. auth->num_freq = 3;
  1562. return 0;
  1563. }
  1564. for (m = 0; m < num_modes; m++) {
  1565. for (c = 0; c < own_modes[m].num_channels; c++) {
  1566. freq = own_modes[m].channels[c].freq;
  1567. flag = own_modes[m].channels[c].flag;
  1568. if (flag & (HOSTAPD_CHAN_DISABLED |
  1569. HOSTAPD_CHAN_NO_IR |
  1570. HOSTAPD_CHAN_RADAR))
  1571. continue;
  1572. if (freq_included(auth->freq, auth->num_freq, freq))
  1573. continue;
  1574. auth->freq[auth->num_freq++] = freq;
  1575. if (auth->num_freq == DPP_BOOTSTRAP_MAX_FREQ) {
  1576. m = num_modes;
  1577. break;
  1578. }
  1579. }
  1580. }
  1581. return auth->num_freq == 0 ? -1 : 0;
  1582. }
  1583. static int dpp_prepare_channel_list(struct dpp_authentication *auth,
  1584. struct hostapd_hw_modes *own_modes,
  1585. u16 num_modes)
  1586. {
  1587. int res;
  1588. char freqs[DPP_BOOTSTRAP_MAX_FREQ * 6 + 10], *pos, *end;
  1589. unsigned int i;
  1590. if (auth->peer_bi->num_freq > 0)
  1591. res = dpp_channel_intersect(auth, own_modes, num_modes);
  1592. else
  1593. res = dpp_channel_local_list(auth, own_modes, num_modes);
  1594. if (res < 0)
  1595. return res;
  1596. /* Prioritize 2.4 GHz channels 6, 1, 11 (in this order) to hit the most
  1597. * likely channels first. */
  1598. freq_to_start(auth->freq, auth->num_freq, 2462);
  1599. freq_to_start(auth->freq, auth->num_freq, 2412);
  1600. freq_to_start(auth->freq, auth->num_freq, 2437);
  1601. auth->freq_idx = 0;
  1602. auth->curr_freq = auth->freq[0];
  1603. pos = freqs;
  1604. end = pos + sizeof(freqs);
  1605. for (i = 0; i < auth->num_freq; i++) {
  1606. res = os_snprintf(pos, end - pos, " %u", auth->freq[i]);
  1607. if (os_snprintf_error(end - pos, res))
  1608. break;
  1609. pos += res;
  1610. }
  1611. *pos = '\0';
  1612. wpa_printf(MSG_DEBUG, "DPP: Possible frequencies for initiating:%s",
  1613. freqs);
  1614. return 0;
  1615. }
  1616. struct dpp_authentication * dpp_auth_init(void *msg_ctx,
  1617. struct dpp_bootstrap_info *peer_bi,
  1618. struct dpp_bootstrap_info *own_bi,
  1619. u8 dpp_allowed_roles,
  1620. unsigned int neg_freq,
  1621. struct hostapd_hw_modes *own_modes,
  1622. u16 num_modes)
  1623. {
  1624. struct dpp_authentication *auth;
  1625. size_t nonce_len;
  1626. EVP_PKEY_CTX *ctx = NULL;
  1627. size_t secret_len;
  1628. struct wpabuf *pi = NULL;
  1629. u8 zero[SHA256_MAC_LEN];
  1630. const u8 *r_pubkey_hash, *i_pubkey_hash;
  1631. auth = os_zalloc(sizeof(*auth));
  1632. if (!auth)
  1633. return NULL;
  1634. auth->msg_ctx = msg_ctx;
  1635. auth->initiator = 1;
  1636. auth->waiting_auth_resp = 1;
  1637. auth->allowed_roles = dpp_allowed_roles;
  1638. auth->configurator = !!(dpp_allowed_roles & DPP_CAPAB_CONFIGURATOR);
  1639. auth->peer_bi = peer_bi;
  1640. auth->own_bi = own_bi;
  1641. auth->curve = peer_bi->curve;
  1642. if (dpp_prepare_channel_list(auth, own_modes, num_modes) < 0)
  1643. goto fail;
  1644. nonce_len = auth->curve->nonce_len;
  1645. if (random_get_bytes(auth->i_nonce, nonce_len)) {
  1646. wpa_printf(MSG_ERROR, "DPP: Failed to generate I-nonce");
  1647. goto fail;
  1648. }
  1649. wpa_hexdump(MSG_DEBUG, "DPP: I-nonce", auth->i_nonce, nonce_len);
  1650. auth->own_protocol_key = dpp_gen_keypair(auth->curve);
  1651. if (!auth->own_protocol_key)
  1652. goto fail;
  1653. pi = dpp_get_pubkey_point(auth->own_protocol_key, 0);
  1654. if (!pi)
  1655. goto fail;
  1656. /* ECDH: M = pI * BR */
  1657. ctx = EVP_PKEY_CTX_new(auth->own_protocol_key, NULL);
  1658. if (!ctx ||
  1659. EVP_PKEY_derive_init(ctx) != 1 ||
  1660. EVP_PKEY_derive_set_peer(ctx, auth->peer_bi->pubkey) != 1 ||
  1661. EVP_PKEY_derive(ctx, NULL, &secret_len) != 1 ||
  1662. secret_len > DPP_MAX_SHARED_SECRET_LEN ||
  1663. EVP_PKEY_derive(ctx, auth->Mx, &secret_len) != 1) {
  1664. wpa_printf(MSG_ERROR,
  1665. "DPP: Failed to derive ECDH shared secret: %s",
  1666. ERR_error_string(ERR_get_error(), NULL));
  1667. goto fail;
  1668. }
  1669. auth->secret_len = secret_len;
  1670. EVP_PKEY_CTX_free(ctx);
  1671. ctx = NULL;
  1672. wpa_hexdump_key(MSG_DEBUG, "DPP: ECDH shared secret (M.x)",
  1673. auth->Mx, auth->secret_len);
  1674. if (dpp_derive_k1(auth->Mx, auth->secret_len, auth->k1,
  1675. auth->curve->hash_len) < 0)
  1676. goto fail;
  1677. r_pubkey_hash = auth->peer_bi->pubkey_hash;
  1678. if (auth->own_bi) {
  1679. i_pubkey_hash = auth->own_bi->pubkey_hash;
  1680. } else {
  1681. os_memset(zero, 0, SHA256_MAC_LEN);
  1682. i_pubkey_hash = zero;
  1683. }
  1684. #ifdef CONFIG_TESTING_OPTIONS
  1685. if (dpp_test == DPP_TEST_NO_R_BOOTSTRAP_KEY_HASH_AUTH_REQ) {
  1686. wpa_printf(MSG_INFO, "DPP: TESTING - no R-Bootstrap Key Hash");
  1687. r_pubkey_hash = NULL;
  1688. } else if (dpp_test == DPP_TEST_NO_I_BOOTSTRAP_KEY_HASH_AUTH_REQ) {
  1689. wpa_printf(MSG_INFO, "DPP: TESTING - no I-Bootstrap Key Hash");
  1690. i_pubkey_hash = NULL;
  1691. } else if (dpp_test == DPP_TEST_NO_I_PROTO_KEY_AUTH_REQ) {
  1692. wpa_printf(MSG_INFO, "DPP: TESTING - no I-Proto Key");
  1693. wpabuf_free(pi);
  1694. pi = NULL;
  1695. }
  1696. #endif /* CONFIG_TESTING_OPTIONS */
  1697. auth->req_msg = dpp_auth_build_req(auth, pi, nonce_len, r_pubkey_hash,
  1698. i_pubkey_hash, neg_freq);
  1699. if (!auth->req_msg)
  1700. goto fail;
  1701. out:
  1702. wpabuf_free(pi);
  1703. EVP_PKEY_CTX_free(ctx);
  1704. return auth;
  1705. fail:
  1706. dpp_auth_deinit(auth);
  1707. auth = NULL;
  1708. goto out;
  1709. }
  1710. struct wpabuf * dpp_build_conf_req(struct dpp_authentication *auth,
  1711. const char *json)
  1712. {
  1713. size_t nonce_len;
  1714. size_t json_len, clear_len;
  1715. struct wpabuf *clear = NULL, *msg = NULL;
  1716. u8 *wrapped;
  1717. size_t attr_len;
  1718. wpa_printf(MSG_DEBUG, "DPP: Build configuration request");
  1719. nonce_len = auth->curve->nonce_len;
  1720. if (random_get_bytes(auth->e_nonce, nonce_len)) {
  1721. wpa_printf(MSG_ERROR, "DPP: Failed to generate E-nonce");
  1722. goto fail;
  1723. }
  1724. wpa_hexdump(MSG_DEBUG, "DPP: E-nonce", auth->e_nonce, nonce_len);
  1725. json_len = os_strlen(json);
  1726. wpa_hexdump_ascii(MSG_DEBUG, "DPP: configAttr JSON", json, json_len);
  1727. /* { E-nonce, configAttrib }ke */
  1728. clear_len = 4 + nonce_len + 4 + json_len;
  1729. clear = wpabuf_alloc(clear_len);
  1730. attr_len = 4 + clear_len + AES_BLOCK_SIZE;
  1731. #ifdef CONFIG_TESTING_OPTIONS
  1732. if (dpp_test == DPP_TEST_AFTER_WRAPPED_DATA_CONF_REQ)
  1733. attr_len += 4;
  1734. #endif /* CONFIG_TESTING_OPTIONS */
  1735. msg = wpabuf_alloc(attr_len);
  1736. if (!clear || !msg)
  1737. goto fail;
  1738. #ifdef CONFIG_TESTING_OPTIONS
  1739. if (dpp_test == DPP_TEST_NO_E_NONCE_CONF_REQ) {
  1740. wpa_printf(MSG_INFO, "DPP: TESTING - no E-nonce");
  1741. goto skip_e_nonce;
  1742. }
  1743. if (dpp_test == DPP_TEST_NO_WRAPPED_DATA_CONF_REQ) {
  1744. wpa_printf(MSG_INFO, "DPP: TESTING - no Wrapped Data");
  1745. goto skip_wrapped_data;
  1746. }
  1747. #endif /* CONFIG_TESTING_OPTIONS */
  1748. /* E-nonce */
  1749. wpabuf_put_le16(clear, DPP_ATTR_ENROLLEE_NONCE);
  1750. wpabuf_put_le16(clear, nonce_len);
  1751. wpabuf_put_data(clear, auth->e_nonce, nonce_len);
  1752. #ifdef CONFIG_TESTING_OPTIONS
  1753. skip_e_nonce:
  1754. if (dpp_test == DPP_TEST_NO_CONFIG_ATTR_OBJ_CONF_REQ) {
  1755. wpa_printf(MSG_INFO, "DPP: TESTING - no configAttrib");
  1756. goto skip_conf_attr_obj;
  1757. }
  1758. #endif /* CONFIG_TESTING_OPTIONS */
  1759. /* configAttrib */
  1760. wpabuf_put_le16(clear, DPP_ATTR_CONFIG_ATTR_OBJ);
  1761. wpabuf_put_le16(clear, json_len);
  1762. wpabuf_put_data(clear, json, json_len);
  1763. #ifdef CONFIG_TESTING_OPTIONS
  1764. skip_conf_attr_obj:
  1765. #endif /* CONFIG_TESTING_OPTIONS */
  1766. wpabuf_put_le16(msg, DPP_ATTR_WRAPPED_DATA);
  1767. wpabuf_put_le16(msg, wpabuf_len(clear) + AES_BLOCK_SIZE);
  1768. wrapped = wpabuf_put(msg, wpabuf_len(clear) + AES_BLOCK_SIZE);
  1769. /* No AES-SIV AD */
  1770. wpa_hexdump_buf(MSG_DEBUG, "DPP: AES-SIV cleartext", clear);
  1771. if (aes_siv_encrypt(auth->ke, auth->curve->hash_len,
  1772. wpabuf_head(clear), wpabuf_len(clear),
  1773. 0, NULL, NULL, wrapped) < 0)
  1774. goto fail;
  1775. wpa_hexdump(MSG_DEBUG, "DPP: AES-SIV ciphertext",
  1776. wrapped, wpabuf_len(clear) + AES_BLOCK_SIZE);
  1777. #ifdef CONFIG_TESTING_OPTIONS
  1778. if (dpp_test == DPP_TEST_AFTER_WRAPPED_DATA_CONF_REQ) {
  1779. wpa_printf(MSG_INFO, "DPP: TESTING - attr after Wrapped Data");
  1780. wpabuf_put_le16(msg, DPP_ATTR_TESTING);
  1781. wpabuf_put_le16(msg, 0);
  1782. }
  1783. skip_wrapped_data:
  1784. #endif /* CONFIG_TESTING_OPTIONS */
  1785. wpa_hexdump_buf(MSG_DEBUG,
  1786. "DPP: Configuration Request frame attributes", msg);
  1787. wpabuf_free(clear);
  1788. return msg;
  1789. fail:
  1790. wpabuf_free(clear);
  1791. wpabuf_free(msg);
  1792. return NULL;
  1793. }
  1794. static void dpp_auth_success(struct dpp_authentication *auth)
  1795. {
  1796. wpa_printf(MSG_DEBUG,
  1797. "DPP: Authentication success - clear temporary keys");
  1798. os_memset(auth->Mx, 0, sizeof(auth->Mx));
  1799. os_memset(auth->Nx, 0, sizeof(auth->Nx));
  1800. os_memset(auth->Lx, 0, sizeof(auth->Lx));
  1801. os_memset(auth->k1, 0, sizeof(auth->k1));
  1802. os_memset(auth->k2, 0, sizeof(auth->k2));
  1803. auth->auth_success = 1;
  1804. }
  1805. static int dpp_gen_r_auth(struct dpp_authentication *auth, u8 *r_auth)
  1806. {
  1807. struct wpabuf *pix, *prx, *bix, *brx;
  1808. const u8 *addr[7];
  1809. size_t len[7];
  1810. size_t i, num_elem = 0;
  1811. size_t nonce_len;
  1812. u8 zero = 0;
  1813. int res = -1;
  1814. /* R-auth = H(I-nonce | R-nonce | PI.x | PR.x | [BI.x |] BR.x | 0) */
  1815. nonce_len = auth->curve->nonce_len;
  1816. if (auth->initiator) {
  1817. pix = dpp_get_pubkey_point(auth->own_protocol_key, 0);
  1818. prx = dpp_get_pubkey_point(auth->peer_protocol_key, 0);
  1819. if (auth->own_bi)
  1820. bix = dpp_get_pubkey_point(auth->own_bi->pubkey, 0);
  1821. else
  1822. bix = NULL;
  1823. brx = dpp_get_pubkey_point(auth->peer_bi->pubkey, 0);
  1824. } else {
  1825. pix = dpp_get_pubkey_point(auth->peer_protocol_key, 0);
  1826. prx = dpp_get_pubkey_point(auth->own_protocol_key, 0);
  1827. if (auth->peer_bi)
  1828. bix = dpp_get_pubkey_point(auth->peer_bi->pubkey, 0);
  1829. else
  1830. bix = NULL;
  1831. brx = dpp_get_pubkey_point(auth->own_bi->pubkey, 0);
  1832. }
  1833. if (!pix || !prx || !brx)
  1834. goto fail;
  1835. addr[num_elem] = auth->i_nonce;
  1836. len[num_elem] = nonce_len;
  1837. num_elem++;
  1838. addr[num_elem] = auth->r_nonce;
  1839. len[num_elem] = nonce_len;
  1840. num_elem++;
  1841. addr[num_elem] = wpabuf_head(pix);
  1842. len[num_elem] = wpabuf_len(pix) / 2;
  1843. num_elem++;
  1844. addr[num_elem] = wpabuf_head(prx);
  1845. len[num_elem] = wpabuf_len(prx) / 2;
  1846. num_elem++;
  1847. if (bix) {
  1848. addr[num_elem] = wpabuf_head(bix);
  1849. len[num_elem] = wpabuf_len(bix) / 2;
  1850. num_elem++;
  1851. }
  1852. addr[num_elem] = wpabuf_head(brx);
  1853. len[num_elem] = wpabuf_len(brx) / 2;
  1854. num_elem++;
  1855. addr[num_elem] = &zero;
  1856. len[num_elem] = 1;
  1857. num_elem++;
  1858. wpa_printf(MSG_DEBUG, "DPP: R-auth hash components");
  1859. for (i = 0; i < num_elem; i++)
  1860. wpa_hexdump(MSG_DEBUG, "DPP: hash component", addr[i], len[i]);
  1861. res = dpp_hash_vector(auth->curve, num_elem, addr, len, r_auth);
  1862. if (res == 0)
  1863. wpa_hexdump(MSG_DEBUG, "DPP: R-auth", r_auth,
  1864. auth->curve->hash_len);
  1865. fail:
  1866. wpabuf_free(pix);
  1867. wpabuf_free(prx);
  1868. wpabuf_free(bix);
  1869. wpabuf_free(brx);
  1870. return res;
  1871. }
  1872. static int dpp_gen_i_auth(struct dpp_authentication *auth, u8 *i_auth)
  1873. {
  1874. struct wpabuf *pix = NULL, *prx = NULL, *bix = NULL, *brx = NULL;
  1875. const u8 *addr[7];
  1876. size_t len[7];
  1877. size_t i, num_elem = 0;
  1878. size_t nonce_len;
  1879. u8 one = 1;
  1880. int res = -1;
  1881. /* I-auth = H(R-nonce | I-nonce | PR.x | PI.x | BR.x | [BI.x |] 1) */
  1882. nonce_len = auth->curve->nonce_len;
  1883. if (auth->initiator) {
  1884. pix = dpp_get_pubkey_point(auth->own_protocol_key, 0);
  1885. prx = dpp_get_pubkey_point(auth->peer_protocol_key, 0);
  1886. if (auth->own_bi)
  1887. bix = dpp_get_pubkey_point(auth->own_bi->pubkey, 0);
  1888. else
  1889. bix = NULL;
  1890. if (!auth->peer_bi)
  1891. goto fail;
  1892. brx = dpp_get_pubkey_point(auth->peer_bi->pubkey, 0);
  1893. } else {
  1894. pix = dpp_get_pubkey_point(auth->peer_protocol_key, 0);
  1895. prx = dpp_get_pubkey_point(auth->own_protocol_key, 0);
  1896. if (auth->peer_bi)
  1897. bix = dpp_get_pubkey_point(auth->peer_bi->pubkey, 0);
  1898. else
  1899. bix = NULL;
  1900. if (!auth->own_bi)
  1901. goto fail;
  1902. brx = dpp_get_pubkey_point(auth->own_bi->pubkey, 0);
  1903. }
  1904. if (!pix || !prx || !brx)
  1905. goto fail;
  1906. addr[num_elem] = auth->r_nonce;
  1907. len[num_elem] = nonce_len;
  1908. num_elem++;
  1909. addr[num_elem] = auth->i_nonce;
  1910. len[num_elem] = nonce_len;
  1911. num_elem++;
  1912. addr[num_elem] = wpabuf_head(prx);
  1913. len[num_elem] = wpabuf_len(prx) / 2;
  1914. num_elem++;
  1915. addr[num_elem] = wpabuf_head(pix);
  1916. len[num_elem] = wpabuf_len(pix) / 2;
  1917. num_elem++;
  1918. addr[num_elem] = wpabuf_head(brx);
  1919. len[num_elem] = wpabuf_len(brx) / 2;
  1920. num_elem++;
  1921. if (bix) {
  1922. addr[num_elem] = wpabuf_head(bix);
  1923. len[num_elem] = wpabuf_len(bix) / 2;
  1924. num_elem++;
  1925. }
  1926. addr[num_elem] = &one;
  1927. len[num_elem] = 1;
  1928. num_elem++;
  1929. wpa_printf(MSG_DEBUG, "DPP: I-auth hash components");
  1930. for (i = 0; i < num_elem; i++)
  1931. wpa_hexdump(MSG_DEBUG, "DPP: hash component", addr[i], len[i]);
  1932. res = dpp_hash_vector(auth->curve, num_elem, addr, len, i_auth);
  1933. if (res == 0)
  1934. wpa_hexdump(MSG_DEBUG, "DPP: I-auth", i_auth,
  1935. auth->curve->hash_len);
  1936. fail:
  1937. wpabuf_free(pix);
  1938. wpabuf_free(prx);
  1939. wpabuf_free(bix);
  1940. wpabuf_free(brx);
  1941. return res;
  1942. }
  1943. static int dpp_auth_derive_l_responder(struct dpp_authentication *auth)
  1944. {
  1945. const EC_GROUP *group;
  1946. EC_POINT *l = NULL;
  1947. EC_KEY *BI = NULL, *bR = NULL, *pR = NULL;
  1948. const EC_POINT *BI_point;
  1949. BN_CTX *bnctx;
  1950. BIGNUM *lx, *sum, *q;
  1951. const BIGNUM *bR_bn, *pR_bn;
  1952. int ret = -1;
  1953. /* L = ((bR + pR) modulo q) * BI */
  1954. bnctx = BN_CTX_new();
  1955. sum = BN_new();
  1956. q = BN_new();
  1957. lx = BN_new();
  1958. if (!bnctx || !sum || !q || !lx)
  1959. goto fail;
  1960. BI = EVP_PKEY_get1_EC_KEY(auth->peer_bi->pubkey);
  1961. if (!BI)
  1962. goto fail;
  1963. BI_point = EC_KEY_get0_public_key(BI);
  1964. group = EC_KEY_get0_group(BI);
  1965. if (!group)
  1966. goto fail;
  1967. bR = EVP_PKEY_get1_EC_KEY(auth->own_bi->pubkey);
  1968. pR = EVP_PKEY_get1_EC_KEY(auth->own_protocol_key);
  1969. if (!bR || !pR)
  1970. goto fail;
  1971. bR_bn = EC_KEY_get0_private_key(bR);
  1972. pR_bn = EC_KEY_get0_private_key(pR);
  1973. if (!bR_bn || !pR_bn)
  1974. goto fail;
  1975. if (EC_GROUP_get_order(group, q, bnctx) != 1 ||
  1976. BN_mod_add(sum, bR_bn, pR_bn, q, bnctx) != 1)
  1977. goto fail;
  1978. l = EC_POINT_new(group);
  1979. if (!l ||
  1980. EC_POINT_mul(group, l, NULL, BI_point, sum, bnctx) != 1 ||
  1981. EC_POINT_get_affine_coordinates_GFp(group, l, lx, NULL,
  1982. bnctx) != 1) {
  1983. wpa_printf(MSG_ERROR,
  1984. "OpenSSL: failed: %s",
  1985. ERR_error_string(ERR_get_error(), NULL));
  1986. goto fail;
  1987. }
  1988. if (dpp_bn2bin_pad(lx, auth->Lx, auth->secret_len) < 0)
  1989. goto fail;
  1990. wpa_hexdump_key(MSG_DEBUG, "DPP: L.x", auth->Lx, auth->secret_len);
  1991. ret = 0;
  1992. fail:
  1993. EC_POINT_clear_free(l);
  1994. EC_KEY_free(BI);
  1995. EC_KEY_free(bR);
  1996. EC_KEY_free(pR);
  1997. BN_clear_free(lx);
  1998. BN_clear_free(sum);
  1999. BN_free(q);
  2000. BN_CTX_free(bnctx);
  2001. return ret;
  2002. }
  2003. static int dpp_auth_derive_l_initiator(struct dpp_authentication *auth)
  2004. {
  2005. const EC_GROUP *group;
  2006. EC_POINT *l = NULL, *sum = NULL;
  2007. EC_KEY *bI = NULL, *BR = NULL, *PR = NULL;
  2008. const EC_POINT *BR_point, *PR_point;
  2009. BN_CTX *bnctx;
  2010. BIGNUM *lx;
  2011. const BIGNUM *bI_bn;
  2012. int ret = -1;
  2013. /* L = bI * (BR + PR) */
  2014. bnctx = BN_CTX_new();
  2015. lx = BN_new();
  2016. if (!bnctx || !lx)
  2017. goto fail;
  2018. BR = EVP_PKEY_get1_EC_KEY(auth->peer_bi->pubkey);
  2019. PR = EVP_PKEY_get1_EC_KEY(auth->peer_protocol_key);
  2020. if (!BR || !PR)
  2021. goto fail;
  2022. BR_point = EC_KEY_get0_public_key(BR);
  2023. PR_point = EC_KEY_get0_public_key(PR);
  2024. bI = EVP_PKEY_get1_EC_KEY(auth->own_bi->pubkey);
  2025. if (!bI)
  2026. goto fail;
  2027. group = EC_KEY_get0_group(bI);
  2028. bI_bn = EC_KEY_get0_private_key(bI);
  2029. if (!group || !bI_bn)
  2030. goto fail;
  2031. sum = EC_POINT_new(group);
  2032. l = EC_POINT_new(group);
  2033. if (!sum || !l ||
  2034. EC_POINT_add(group, sum, BR_point, PR_point, bnctx) != 1 ||
  2035. EC_POINT_mul(group, l, NULL, sum, bI_bn, bnctx) != 1 ||
  2036. EC_POINT_get_affine_coordinates_GFp(group, l, lx, NULL,
  2037. bnctx) != 1) {
  2038. wpa_printf(MSG_ERROR,
  2039. "OpenSSL: failed: %s",
  2040. ERR_error_string(ERR_get_error(), NULL));
  2041. goto fail;
  2042. }
  2043. if (dpp_bn2bin_pad(lx, auth->Lx, auth->secret_len) < 0)
  2044. goto fail;
  2045. wpa_hexdump_key(MSG_DEBUG, "DPP: L.x", auth->Lx, auth->secret_len);
  2046. ret = 0;
  2047. fail:
  2048. EC_POINT_clear_free(l);
  2049. EC_KEY_free(bI);
  2050. EC_KEY_free(BR);
  2051. EC_KEY_free(PR);
  2052. BN_clear_free(lx);
  2053. BN_CTX_free(bnctx);
  2054. return ret;
  2055. }
  2056. static int dpp_auth_build_resp_ok(struct dpp_authentication *auth)
  2057. {
  2058. size_t nonce_len;
  2059. EVP_PKEY_CTX *ctx = NULL;
  2060. size_t secret_len;
  2061. struct wpabuf *msg, *pr = NULL;
  2062. u8 r_auth[4 + DPP_MAX_HASH_LEN];
  2063. u8 wrapped_r_auth[4 + DPP_MAX_HASH_LEN + AES_BLOCK_SIZE], *w_r_auth;
  2064. size_t wrapped_r_auth_len;
  2065. int ret = -1;
  2066. const u8 *r_pubkey_hash, *i_pubkey_hash, *r_nonce, *i_nonce;
  2067. enum dpp_status_error status = DPP_STATUS_OK;
  2068. wpa_printf(MSG_DEBUG, "DPP: Build Authentication Response");
  2069. if (!auth->own_bi)
  2070. return -1;
  2071. nonce_len = auth->curve->nonce_len;
  2072. if (random_get_bytes(auth->r_nonce, nonce_len)) {
  2073. wpa_printf(MSG_ERROR, "DPP: Failed to generate R-nonce");
  2074. goto fail;
  2075. }
  2076. wpa_hexdump(MSG_DEBUG, "DPP: R-nonce", auth->r_nonce, nonce_len);
  2077. auth->own_protocol_key = dpp_gen_keypair(auth->curve);
  2078. if (!auth->own_protocol_key)
  2079. goto fail;
  2080. pr = dpp_get_pubkey_point(auth->own_protocol_key, 0);
  2081. if (!pr)
  2082. goto fail;
  2083. /* ECDH: N = pR * PI */
  2084. ctx = EVP_PKEY_CTX_new(auth->own_protocol_key, NULL);
  2085. if (!ctx ||
  2086. EVP_PKEY_derive_init(ctx) != 1 ||
  2087. EVP_PKEY_derive_set_peer(ctx, auth->peer_protocol_key) != 1 ||
  2088. EVP_PKEY_derive(ctx, NULL, &secret_len) != 1 ||
  2089. secret_len > DPP_MAX_SHARED_SECRET_LEN ||
  2090. EVP_PKEY_derive(ctx, auth->Nx, &secret_len) != 1) {
  2091. wpa_printf(MSG_ERROR,
  2092. "DPP: Failed to derive ECDH shared secret: %s",
  2093. ERR_error_string(ERR_get_error(), NULL));
  2094. goto fail;
  2095. }
  2096. EVP_PKEY_CTX_free(ctx);
  2097. ctx = NULL;
  2098. wpa_hexdump_key(MSG_DEBUG, "DPP: ECDH shared secret (N.x)",
  2099. auth->Nx, auth->secret_len);
  2100. if (dpp_derive_k2(auth->Nx, auth->secret_len, auth->k2,
  2101. auth->curve->hash_len) < 0)
  2102. goto fail;
  2103. if (auth->own_bi && auth->peer_bi) {
  2104. /* Mutual authentication */
  2105. if (dpp_auth_derive_l_responder(auth) < 0)
  2106. goto fail;
  2107. }
  2108. if (dpp_derive_ke(auth, auth->ke, auth->curve->hash_len) < 0)
  2109. goto fail;
  2110. /* R-auth = H(I-nonce | R-nonce | PI.x | PR.x | [BI.x |] BR.x | 0) */
  2111. WPA_PUT_LE16(r_auth, DPP_ATTR_R_AUTH_TAG);
  2112. WPA_PUT_LE16(&r_auth[2], auth->curve->hash_len);
  2113. if (dpp_gen_r_auth(auth, r_auth + 4) < 0)
  2114. goto fail;
  2115. #ifdef CONFIG_TESTING_OPTIONS
  2116. if (dpp_test == DPP_TEST_R_AUTH_MISMATCH_AUTH_RESP) {
  2117. wpa_printf(MSG_INFO, "DPP: TESTING - R-auth mismatch");
  2118. r_auth[4 + auth->curve->hash_len / 2] ^= 0x01;
  2119. }
  2120. #endif /* CONFIG_TESTING_OPTIONS */
  2121. if (aes_siv_encrypt(auth->ke, auth->curve->hash_len,
  2122. r_auth, 4 + auth->curve->hash_len,
  2123. 0, NULL, NULL, wrapped_r_auth) < 0)
  2124. goto fail;
  2125. wrapped_r_auth_len = 4 + auth->curve->hash_len + AES_BLOCK_SIZE;
  2126. wpa_hexdump(MSG_DEBUG, "DPP: {R-auth}ke",
  2127. wrapped_r_auth, wrapped_r_auth_len);
  2128. w_r_auth = wrapped_r_auth;
  2129. r_pubkey_hash = auth->own_bi->pubkey_hash;
  2130. if (auth->peer_bi)
  2131. i_pubkey_hash = auth->peer_bi->pubkey_hash;
  2132. else
  2133. i_pubkey_hash = NULL;
  2134. i_nonce = auth->i_nonce;
  2135. r_nonce = auth->r_nonce;
  2136. #ifdef CONFIG_TESTING_OPTIONS
  2137. if (dpp_test == DPP_TEST_NO_R_BOOTSTRAP_KEY_HASH_AUTH_RESP) {
  2138. wpa_printf(MSG_INFO, "DPP: TESTING - no R-Bootstrap Key Hash");
  2139. r_pubkey_hash = NULL;
  2140. } else if (dpp_test == DPP_TEST_NO_I_BOOTSTRAP_KEY_HASH_AUTH_RESP) {
  2141. wpa_printf(MSG_INFO, "DPP: TESTING - no I-Bootstrap Key Hash");
  2142. i_pubkey_hash = NULL;
  2143. } else if (dpp_test == DPP_TEST_NO_R_PROTO_KEY_AUTH_RESP) {
  2144. wpa_printf(MSG_INFO, "DPP: TESTING - no R-Proto Key");
  2145. wpabuf_free(pr);
  2146. pr = NULL;
  2147. } else if (dpp_test == DPP_TEST_NO_R_AUTH_AUTH_RESP) {
  2148. wpa_printf(MSG_INFO, "DPP: TESTING - no R-Auth");
  2149. w_r_auth = NULL;
  2150. wrapped_r_auth_len = 0;
  2151. } else if (dpp_test == DPP_TEST_NO_STATUS_AUTH_RESP) {
  2152. wpa_printf(MSG_INFO, "DPP: TESTING - no Status");
  2153. status = 255;
  2154. } else if (dpp_test == DPP_TEST_NO_R_NONCE_AUTH_RESP) {
  2155. wpa_printf(MSG_INFO, "DPP: TESTING - no R-nonce");
  2156. r_nonce = NULL;
  2157. } else if (dpp_test == DPP_TEST_NO_I_NONCE_AUTH_RESP) {
  2158. wpa_printf(MSG_INFO, "DPP: TESTING - no I-nonce");
  2159. i_nonce = NULL;
  2160. }
  2161. #endif /* CONFIG_TESTING_OPTIONS */
  2162. msg = dpp_auth_build_resp(auth, status, pr, nonce_len,
  2163. r_pubkey_hash, i_pubkey_hash,
  2164. r_nonce, i_nonce,
  2165. w_r_auth, wrapped_r_auth_len,
  2166. auth->k2);
  2167. if (!msg)
  2168. goto fail;
  2169. wpabuf_free(auth->resp_msg);
  2170. auth->resp_msg = msg;
  2171. ret = 0;
  2172. fail:
  2173. wpabuf_free(pr);
  2174. return ret;
  2175. }
  2176. static int dpp_auth_build_resp_status(struct dpp_authentication *auth,
  2177. enum dpp_status_error status)
  2178. {
  2179. struct wpabuf *msg;
  2180. const u8 *r_pubkey_hash, *i_pubkey_hash, *i_nonce;
  2181. if (!auth->own_bi)
  2182. return -1;
  2183. wpa_printf(MSG_DEBUG, "DPP: Build Authentication Response");
  2184. r_pubkey_hash = auth->own_bi->pubkey_hash;
  2185. if (auth->peer_bi)
  2186. i_pubkey_hash = auth->peer_bi->pubkey_hash;
  2187. else
  2188. i_pubkey_hash = NULL;
  2189. i_nonce = auth->i_nonce;
  2190. #ifdef CONFIG_TESTING_OPTIONS
  2191. if (dpp_test == DPP_TEST_NO_R_BOOTSTRAP_KEY_HASH_AUTH_RESP) {
  2192. wpa_printf(MSG_INFO, "DPP: TESTING - no R-Bootstrap Key Hash");
  2193. r_pubkey_hash = NULL;
  2194. } else if (dpp_test == DPP_TEST_NO_I_BOOTSTRAP_KEY_HASH_AUTH_RESP) {
  2195. wpa_printf(MSG_INFO, "DPP: TESTING - no I-Bootstrap Key Hash");
  2196. i_pubkey_hash = NULL;
  2197. } else if (dpp_test == DPP_TEST_NO_STATUS_AUTH_RESP) {
  2198. wpa_printf(MSG_INFO, "DPP: TESTING - no Status");
  2199. status = -1;
  2200. } else if (dpp_test == DPP_TEST_NO_I_NONCE_AUTH_RESP) {
  2201. wpa_printf(MSG_INFO, "DPP: TESTING - no I-nonce");
  2202. i_nonce = NULL;
  2203. }
  2204. #endif /* CONFIG_TESTING_OPTIONS */
  2205. msg = dpp_auth_build_resp(auth, status, NULL, auth->curve->nonce_len,
  2206. r_pubkey_hash, i_pubkey_hash,
  2207. NULL, i_nonce, NULL, 0, auth->k1);
  2208. if (!msg)
  2209. return -1;
  2210. wpabuf_free(auth->resp_msg);
  2211. auth->resp_msg = msg;
  2212. return 0;
  2213. }
  2214. struct dpp_authentication *
  2215. dpp_auth_req_rx(void *msg_ctx, u8 dpp_allowed_roles, int qr_mutual,
  2216. struct dpp_bootstrap_info *peer_bi,
  2217. struct dpp_bootstrap_info *own_bi,
  2218. unsigned int freq, const u8 *hdr, const u8 *attr_start,
  2219. size_t attr_len)
  2220. {
  2221. EVP_PKEY *pi = NULL;
  2222. EVP_PKEY_CTX *ctx = NULL;
  2223. size_t secret_len;
  2224. const u8 *addr[2];
  2225. size_t len[2];
  2226. u8 *unwrapped = NULL;
  2227. size_t unwrapped_len = 0;
  2228. const u8 *wrapped_data, *i_proto, *i_nonce, *i_capab, *i_bootstrap,
  2229. *channel;
  2230. u16 wrapped_data_len, i_proto_len, i_nonce_len, i_capab_len,
  2231. i_bootstrap_len, channel_len;
  2232. struct dpp_authentication *auth = NULL;
  2233. wrapped_data = dpp_get_attr(attr_start, attr_len, DPP_ATTR_WRAPPED_DATA,
  2234. &wrapped_data_len);
  2235. if (!wrapped_data || wrapped_data_len < AES_BLOCK_SIZE) {
  2236. wpa_msg(msg_ctx, MSG_INFO, DPP_EVENT_FAIL
  2237. "Missing or invalid required Wrapped Data attribute");
  2238. return NULL;
  2239. }
  2240. wpa_hexdump(MSG_MSGDUMP, "DPP: Wrapped Data",
  2241. wrapped_data, wrapped_data_len);
  2242. attr_len = wrapped_data - 4 - attr_start;
  2243. auth = os_zalloc(sizeof(*auth));
  2244. if (!auth)
  2245. goto fail;
  2246. auth->msg_ctx = msg_ctx;
  2247. auth->peer_bi = peer_bi;
  2248. auth->own_bi = own_bi;
  2249. auth->curve = own_bi->curve;
  2250. auth->curr_freq = freq;
  2251. channel = dpp_get_attr(attr_start, attr_len, DPP_ATTR_CHANNEL,
  2252. &channel_len);
  2253. if (channel) {
  2254. int neg_freq;
  2255. if (channel_len < 2) {
  2256. dpp_auth_fail(auth, "Too short Channel attribute");
  2257. goto fail;
  2258. }
  2259. neg_freq = ieee80211_chan_to_freq(NULL, channel[0], channel[1]);
  2260. wpa_printf(MSG_DEBUG,
  2261. "DPP: Initiator requested different channel for negotiation: op_class=%u channel=%u --> freq=%d",
  2262. channel[0], channel[1], neg_freq);
  2263. if (neg_freq < 0) {
  2264. dpp_auth_fail(auth,
  2265. "Unsupported Channel attribute value");
  2266. goto fail;
  2267. }
  2268. if (auth->curr_freq != (unsigned int) neg_freq) {
  2269. wpa_printf(MSG_DEBUG,
  2270. "DPP: Changing negotiation channel from %u MHz to %u MHz",
  2271. freq, neg_freq);
  2272. auth->curr_freq = neg_freq;
  2273. }
  2274. }
  2275. i_proto = dpp_get_attr(attr_start, attr_len, DPP_ATTR_I_PROTOCOL_KEY,
  2276. &i_proto_len);
  2277. if (!i_proto) {
  2278. dpp_auth_fail(auth,
  2279. "Missing required Initiator Protocol Key attribute");
  2280. goto fail;
  2281. }
  2282. wpa_hexdump(MSG_MSGDUMP, "DPP: Initiator Protocol Key",
  2283. i_proto, i_proto_len);
  2284. /* M = bR * PI */
  2285. pi = dpp_set_pubkey_point(own_bi->pubkey, i_proto, i_proto_len);
  2286. if (!pi) {
  2287. dpp_auth_fail(auth, "Invalid Initiator Protocol Key");
  2288. goto fail;
  2289. }
  2290. dpp_debug_print_key("Peer (Initiator) Protocol Key", pi);
  2291. ctx = EVP_PKEY_CTX_new(own_bi->pubkey, NULL);
  2292. if (!ctx ||
  2293. EVP_PKEY_derive_init(ctx) != 1 ||
  2294. EVP_PKEY_derive_set_peer(ctx, pi) != 1 ||
  2295. EVP_PKEY_derive(ctx, NULL, &secret_len) != 1 ||
  2296. secret_len > DPP_MAX_SHARED_SECRET_LEN ||
  2297. EVP_PKEY_derive(ctx, auth->Mx, &secret_len) != 1) {
  2298. wpa_printf(MSG_ERROR,
  2299. "DPP: Failed to derive ECDH shared secret: %s",
  2300. ERR_error_string(ERR_get_error(), NULL));
  2301. dpp_auth_fail(auth, "Failed to derive ECDH shared secret");
  2302. goto fail;
  2303. }
  2304. auth->secret_len = secret_len;
  2305. EVP_PKEY_CTX_free(ctx);
  2306. ctx = NULL;
  2307. wpa_hexdump_key(MSG_DEBUG, "DPP: ECDH shared secret (M.x)",
  2308. auth->Mx, auth->secret_len);
  2309. if (dpp_derive_k1(auth->Mx, auth->secret_len, auth->k1,
  2310. auth->curve->hash_len) < 0)
  2311. goto fail;
  2312. addr[0] = hdr;
  2313. len[0] = DPP_HDR_LEN;
  2314. addr[1] = attr_start;
  2315. len[1] = attr_len;
  2316. wpa_hexdump(MSG_DEBUG, "DDP: AES-SIV AD[0]", addr[0], len[0]);
  2317. wpa_hexdump(MSG_DEBUG, "DDP: AES-SIV AD[1]", addr[1], len[1]);
  2318. wpa_hexdump(MSG_DEBUG, "DPP: AES-SIV ciphertext",
  2319. wrapped_data, wrapped_data_len);
  2320. unwrapped_len = wrapped_data_len - AES_BLOCK_SIZE;
  2321. unwrapped = os_malloc(unwrapped_len);
  2322. if (!unwrapped)
  2323. goto fail;
  2324. if (aes_siv_decrypt(auth->k1, auth->curve->hash_len,
  2325. wrapped_data, wrapped_data_len,
  2326. 2, addr, len, unwrapped) < 0) {
  2327. dpp_auth_fail(auth, "AES-SIV decryption failed");
  2328. goto fail;
  2329. }
  2330. wpa_hexdump(MSG_DEBUG, "DPP: AES-SIV cleartext",
  2331. unwrapped, unwrapped_len);
  2332. if (dpp_check_attrs(unwrapped, unwrapped_len) < 0) {
  2333. dpp_auth_fail(auth, "Invalid attribute in unwrapped data");
  2334. goto fail;
  2335. }
  2336. i_nonce = dpp_get_attr(unwrapped, unwrapped_len, DPP_ATTR_I_NONCE,
  2337. &i_nonce_len);
  2338. if (!i_nonce || i_nonce_len != auth->curve->nonce_len) {
  2339. dpp_auth_fail(auth, "Missing or invalid I-nonce");
  2340. goto fail;
  2341. }
  2342. wpa_hexdump(MSG_DEBUG, "DPP: I-nonce", i_nonce, i_nonce_len);
  2343. os_memcpy(auth->i_nonce, i_nonce, i_nonce_len);
  2344. i_capab = dpp_get_attr(unwrapped, unwrapped_len,
  2345. DPP_ATTR_I_CAPABILITIES,
  2346. &i_capab_len);
  2347. if (!i_capab || i_capab_len < 1) {
  2348. dpp_auth_fail(auth, "Missing or invalid I-capabilities");
  2349. goto fail;
  2350. }
  2351. auth->i_capab = i_capab[0];
  2352. wpa_printf(MSG_DEBUG, "DPP: I-capabilities: 0x%02x", auth->i_capab);
  2353. bin_clear_free(unwrapped, unwrapped_len);
  2354. unwrapped = NULL;
  2355. switch (auth->i_capab & DPP_CAPAB_ROLE_MASK) {
  2356. case DPP_CAPAB_ENROLLEE:
  2357. if (!(dpp_allowed_roles & DPP_CAPAB_CONFIGURATOR)) {
  2358. wpa_printf(MSG_DEBUG,
  2359. "DPP: Local policy does not allow Configurator role");
  2360. goto not_compatible;
  2361. }
  2362. wpa_printf(MSG_DEBUG, "DPP: Acting as Configurator");
  2363. auth->configurator = 1;
  2364. break;
  2365. case DPP_CAPAB_CONFIGURATOR:
  2366. if (!(dpp_allowed_roles & DPP_CAPAB_ENROLLEE)) {
  2367. wpa_printf(MSG_DEBUG,
  2368. "DPP: Local policy does not allow Enrollee role");
  2369. goto not_compatible;
  2370. }
  2371. wpa_printf(MSG_DEBUG, "DPP: Acting as Enrollee");
  2372. auth->configurator = 0;
  2373. break;
  2374. case DPP_CAPAB_CONFIGURATOR | DPP_CAPAB_ENROLLEE:
  2375. if (dpp_allowed_roles & DPP_CAPAB_ENROLLEE) {
  2376. wpa_printf(MSG_DEBUG, "DPP: Acting as Enrollee");
  2377. auth->configurator = 0;
  2378. } else if (dpp_allowed_roles & DPP_CAPAB_CONFIGURATOR) {
  2379. wpa_printf(MSG_DEBUG, "DPP: Acting as Configurator");
  2380. auth->configurator = 1;
  2381. } else {
  2382. wpa_printf(MSG_DEBUG,
  2383. "DPP: Local policy does not allow Configurator/Enrollee role");
  2384. goto not_compatible;
  2385. }
  2386. break;
  2387. default:
  2388. wpa_printf(MSG_DEBUG, "DPP: Unexpected role in I-capabilities");
  2389. wpa_msg(auth->msg_ctx, MSG_INFO,
  2390. DPP_EVENT_FAIL "Invalid role in I-capabilities 0x%02x",
  2391. auth->i_capab & DPP_CAPAB_ROLE_MASK);
  2392. goto fail;
  2393. }
  2394. auth->peer_protocol_key = pi;
  2395. pi = NULL;
  2396. if (qr_mutual && !peer_bi && own_bi->type == DPP_BOOTSTRAP_QR_CODE) {
  2397. char hex[SHA256_MAC_LEN * 2 + 1];
  2398. wpa_printf(MSG_DEBUG,
  2399. "DPP: Mutual authentication required with QR Codes, but peer info is not yet available - request more time");
  2400. if (dpp_auth_build_resp_status(auth,
  2401. DPP_STATUS_RESPONSE_PENDING) < 0)
  2402. goto fail;
  2403. i_bootstrap = dpp_get_attr(attr_start, attr_len,
  2404. DPP_ATTR_I_BOOTSTRAP_KEY_HASH,
  2405. &i_bootstrap_len);
  2406. if (i_bootstrap && i_bootstrap_len == SHA256_MAC_LEN) {
  2407. auth->response_pending = 1;
  2408. os_memcpy(auth->waiting_pubkey_hash,
  2409. i_bootstrap, i_bootstrap_len);
  2410. wpa_snprintf_hex(hex, sizeof(hex), i_bootstrap,
  2411. i_bootstrap_len);
  2412. } else {
  2413. hex[0] = '\0';
  2414. }
  2415. wpa_msg(auth->msg_ctx, MSG_INFO, DPP_EVENT_SCAN_PEER_QR_CODE
  2416. "%s", hex);
  2417. return auth;
  2418. }
  2419. if (dpp_auth_build_resp_ok(auth) < 0)
  2420. goto fail;
  2421. return auth;
  2422. not_compatible:
  2423. wpa_msg(auth->msg_ctx, MSG_INFO, DPP_EVENT_NOT_COMPATIBLE
  2424. "i-capab=0x%02x", auth->i_capab);
  2425. if (dpp_allowed_roles & DPP_CAPAB_CONFIGURATOR)
  2426. auth->configurator = 1;
  2427. else
  2428. auth->configurator = 0;
  2429. auth->peer_protocol_key = pi;
  2430. pi = NULL;
  2431. if (dpp_auth_build_resp_status(auth, DPP_STATUS_NOT_COMPATIBLE) < 0)
  2432. goto fail;
  2433. auth->remove_on_tx_status = 1;
  2434. return auth;
  2435. fail:
  2436. bin_clear_free(unwrapped, unwrapped_len);
  2437. EVP_PKEY_free(pi);
  2438. EVP_PKEY_CTX_free(ctx);
  2439. dpp_auth_deinit(auth);
  2440. return NULL;
  2441. }
  2442. int dpp_notify_new_qr_code(struct dpp_authentication *auth,
  2443. struct dpp_bootstrap_info *peer_bi)
  2444. {
  2445. if (!auth || !auth->response_pending ||
  2446. os_memcmp(auth->waiting_pubkey_hash, peer_bi->pubkey_hash,
  2447. SHA256_MAC_LEN) != 0)
  2448. return 0;
  2449. wpa_printf(MSG_DEBUG,
  2450. "DPP: New scanned QR Code has matching public key that was needed to continue DPP Authentication exchange with "
  2451. MACSTR, MAC2STR(auth->peer_mac_addr));
  2452. auth->peer_bi = peer_bi;
  2453. if (dpp_auth_build_resp_ok(auth) < 0)
  2454. return -1;
  2455. return 1;
  2456. }
  2457. static struct wpabuf * dpp_auth_build_conf(struct dpp_authentication *auth,
  2458. enum dpp_status_error status)
  2459. {
  2460. struct wpabuf *msg;
  2461. u8 i_auth[4 + DPP_MAX_HASH_LEN];
  2462. size_t i_auth_len;
  2463. u8 r_nonce[4 + DPP_MAX_NONCE_LEN];
  2464. size_t r_nonce_len;
  2465. const u8 *addr[2];
  2466. size_t len[2], attr_len;
  2467. u8 *wrapped_i_auth;
  2468. u8 *wrapped_r_nonce;
  2469. u8 *attr_start, *attr_end;
  2470. wpa_printf(MSG_DEBUG, "DPP: Build Authentication Confirmation");
  2471. i_auth_len = 4 + auth->curve->hash_len;
  2472. r_nonce_len = 4 + auth->curve->nonce_len;
  2473. /* Build DPP Authentication Confirmation frame attributes */
  2474. attr_len = 4 + 1 + 2 * (4 + SHA256_MAC_LEN) +
  2475. 4 + i_auth_len + r_nonce_len + AES_BLOCK_SIZE;
  2476. #ifdef CONFIG_TESTING_OPTIONS
  2477. if (dpp_test == DPP_TEST_AFTER_WRAPPED_DATA_AUTH_CONF)
  2478. attr_len += 4;
  2479. #endif /* CONFIG_TESTING_OPTIONS */
  2480. msg = dpp_alloc_msg(DPP_PA_AUTHENTICATION_CONF, attr_len);
  2481. if (!msg)
  2482. goto fail;
  2483. attr_start = wpabuf_put(msg, 0);
  2484. #ifdef CONFIG_TESTING_OPTIONS
  2485. if (dpp_test == DPP_TEST_NO_STATUS_AUTH_CONF)
  2486. goto skip_status;
  2487. #endif /* CONFIG_TESTING_OPTIONS */
  2488. /* DPP Status */
  2489. wpabuf_put_le16(msg, DPP_ATTR_STATUS);
  2490. wpabuf_put_le16(msg, 1);
  2491. wpabuf_put_u8(msg, status);
  2492. #ifdef CONFIG_TESTING_OPTIONS
  2493. skip_status:
  2494. if (dpp_test == DPP_TEST_NO_R_BOOTSTRAP_KEY_HASH_AUTH_CONF)
  2495. goto skip_r_bootstrap_key;
  2496. #endif /* CONFIG_TESTING_OPTIONS */
  2497. /* Responder Bootstrapping Key Hash */
  2498. wpabuf_put_le16(msg, DPP_ATTR_R_BOOTSTRAP_KEY_HASH);
  2499. wpabuf_put_le16(msg, SHA256_MAC_LEN);
  2500. wpabuf_put_data(msg, auth->peer_bi->pubkey_hash, SHA256_MAC_LEN);
  2501. #ifdef CONFIG_TESTING_OPTIONS
  2502. skip_r_bootstrap_key:
  2503. if (dpp_test == DPP_TEST_NO_I_BOOTSTRAP_KEY_HASH_AUTH_CONF)
  2504. goto skip_i_bootstrap_key;
  2505. #endif /* CONFIG_TESTING_OPTIONS */
  2506. if (auth->own_bi) {
  2507. /* Mutual authentication */
  2508. /* Initiator Bootstrapping Key Hash */
  2509. wpabuf_put_le16(msg, DPP_ATTR_I_BOOTSTRAP_KEY_HASH);
  2510. wpabuf_put_le16(msg, SHA256_MAC_LEN);
  2511. wpabuf_put_data(msg, auth->own_bi->pubkey_hash, SHA256_MAC_LEN);
  2512. }
  2513. #ifdef CONFIG_TESTING_OPTIONS
  2514. skip_i_bootstrap_key:
  2515. if (dpp_test == DPP_TEST_NO_WRAPPED_DATA_AUTH_CONF)
  2516. goto skip_wrapped_data;
  2517. if (dpp_test == DPP_TEST_NO_I_AUTH_AUTH_CONF)
  2518. i_auth_len = 0;
  2519. #endif /* CONFIG_TESTING_OPTIONS */
  2520. attr_end = wpabuf_put(msg, 0);
  2521. /* OUI, OUI type, Crypto Suite, DPP frame type */
  2522. addr[0] = wpabuf_head_u8(msg) + 2;
  2523. len[0] = 3 + 1 + 1 + 1;
  2524. wpa_hexdump(MSG_DEBUG, "DDP: AES-SIV AD[0]", addr[0], len[0]);
  2525. /* Attributes before Wrapped Data */
  2526. addr[1] = attr_start;
  2527. len[1] = attr_end - attr_start;
  2528. wpa_hexdump(MSG_DEBUG, "DDP: AES-SIV AD[1]", addr[1], len[1]);
  2529. if (status == DPP_STATUS_OK) {
  2530. /* I-auth wrapped with ke */
  2531. wpabuf_put_le16(msg, DPP_ATTR_WRAPPED_DATA);
  2532. wpabuf_put_le16(msg, i_auth_len + AES_BLOCK_SIZE);
  2533. wrapped_i_auth = wpabuf_put(msg, i_auth_len + AES_BLOCK_SIZE);
  2534. #ifdef CONFIG_TESTING_OPTIONS
  2535. if (dpp_test == DPP_TEST_NO_I_AUTH_AUTH_CONF)
  2536. goto skip_i_auth;
  2537. #endif /* CONFIG_TESTING_OPTIONS */
  2538. /* I-auth = H(R-nonce | I-nonce | PR.x | PI.x | BR.x | [BI.x |]
  2539. * 1) */
  2540. WPA_PUT_LE16(i_auth, DPP_ATTR_I_AUTH_TAG);
  2541. WPA_PUT_LE16(&i_auth[2], auth->curve->hash_len);
  2542. if (dpp_gen_i_auth(auth, i_auth + 4) < 0)
  2543. goto fail;
  2544. #ifdef CONFIG_TESTING_OPTIONS
  2545. if (dpp_test == DPP_TEST_I_AUTH_MISMATCH_AUTH_CONF) {
  2546. wpa_printf(MSG_INFO, "DPP: TESTING - I-auth mismatch");
  2547. i_auth[4 + auth->curve->hash_len / 2] ^= 0x01;
  2548. }
  2549. skip_i_auth:
  2550. #endif /* CONFIG_TESTING_OPTIONS */
  2551. if (aes_siv_encrypt(auth->ke, auth->curve->hash_len,
  2552. i_auth, i_auth_len,
  2553. 2, addr, len, wrapped_i_auth) < 0)
  2554. goto fail;
  2555. wpa_hexdump(MSG_DEBUG, "DPP: {I-auth}ke",
  2556. wrapped_i_auth, i_auth_len + AES_BLOCK_SIZE);
  2557. } else {
  2558. /* R-nonce wrapped with k2 */
  2559. wpabuf_put_le16(msg, DPP_ATTR_WRAPPED_DATA);
  2560. wpabuf_put_le16(msg, r_nonce_len + AES_BLOCK_SIZE);
  2561. wrapped_r_nonce = wpabuf_put(msg, r_nonce_len + AES_BLOCK_SIZE);
  2562. WPA_PUT_LE16(r_nonce, DPP_ATTR_R_NONCE);
  2563. WPA_PUT_LE16(&r_nonce[2], auth->curve->nonce_len);
  2564. os_memcpy(r_nonce + 4, auth->r_nonce, auth->curve->nonce_len);
  2565. if (aes_siv_encrypt(auth->k2, auth->curve->hash_len,
  2566. r_nonce, r_nonce_len,
  2567. 2, addr, len, wrapped_r_nonce) < 0)
  2568. goto fail;
  2569. wpa_hexdump(MSG_DEBUG, "DPP: {R-nonce}k2",
  2570. wrapped_r_nonce, r_nonce_len + AES_BLOCK_SIZE);
  2571. }
  2572. #ifdef CONFIG_TESTING_OPTIONS
  2573. if (dpp_test == DPP_TEST_AFTER_WRAPPED_DATA_AUTH_CONF) {
  2574. wpa_printf(MSG_INFO, "DPP: TESTING - attr after Wrapped Data");
  2575. wpabuf_put_le16(msg, DPP_ATTR_TESTING);
  2576. wpabuf_put_le16(msg, 0);
  2577. }
  2578. skip_wrapped_data:
  2579. #endif /* CONFIG_TESTING_OPTIONS */
  2580. wpa_hexdump_buf(MSG_DEBUG,
  2581. "DPP: Authentication Confirmation frame attributes",
  2582. msg);
  2583. if (status == DPP_STATUS_OK)
  2584. dpp_auth_success(auth);
  2585. return msg;
  2586. fail:
  2587. return NULL;
  2588. }
  2589. static void
  2590. dpp_auth_resp_rx_status(struct dpp_authentication *auth, const u8 *hdr,
  2591. const u8 *attr_start, size_t attr_len,
  2592. const u8 *wrapped_data, u16 wrapped_data_len,
  2593. enum dpp_status_error status)
  2594. {
  2595. const u8 *addr[2];
  2596. size_t len[2];
  2597. u8 *unwrapped = NULL;
  2598. size_t unwrapped_len = 0;
  2599. const u8 *i_nonce, *r_capab;
  2600. u16 i_nonce_len, r_capab_len;
  2601. if (status == DPP_STATUS_NOT_COMPATIBLE) {
  2602. wpa_printf(MSG_DEBUG,
  2603. "DPP: Responder reported incompatible roles");
  2604. } else if (status == DPP_STATUS_RESPONSE_PENDING) {
  2605. wpa_printf(MSG_DEBUG,
  2606. "DPP: Responder reported more time needed");
  2607. } else {
  2608. wpa_printf(MSG_DEBUG,
  2609. "DPP: Responder reported failure (status %d)",
  2610. status);
  2611. dpp_auth_fail(auth, "Responder reported failure");
  2612. return;
  2613. }
  2614. addr[0] = hdr;
  2615. len[0] = DPP_HDR_LEN;
  2616. addr[1] = attr_start;
  2617. len[1] = attr_len;
  2618. wpa_hexdump(MSG_DEBUG, "DDP: AES-SIV AD[0]", addr[0], len[0]);
  2619. wpa_hexdump(MSG_DEBUG, "DDP: AES-SIV AD[1]", addr[1], len[1]);
  2620. wpa_hexdump(MSG_DEBUG, "DPP: AES-SIV ciphertext",
  2621. wrapped_data, wrapped_data_len);
  2622. unwrapped_len = wrapped_data_len - AES_BLOCK_SIZE;
  2623. unwrapped = os_malloc(unwrapped_len);
  2624. if (!unwrapped)
  2625. goto fail;
  2626. if (aes_siv_decrypt(auth->k1, auth->curve->hash_len,
  2627. wrapped_data, wrapped_data_len,
  2628. 2, addr, len, unwrapped) < 0) {
  2629. dpp_auth_fail(auth, "AES-SIV decryption failed");
  2630. goto fail;
  2631. }
  2632. wpa_hexdump(MSG_DEBUG, "DPP: AES-SIV cleartext",
  2633. unwrapped, unwrapped_len);
  2634. if (dpp_check_attrs(unwrapped, unwrapped_len) < 0) {
  2635. dpp_auth_fail(auth, "Invalid attribute in unwrapped data");
  2636. goto fail;
  2637. }
  2638. i_nonce = dpp_get_attr(unwrapped, unwrapped_len, DPP_ATTR_I_NONCE,
  2639. &i_nonce_len);
  2640. if (!i_nonce || i_nonce_len != auth->curve->nonce_len) {
  2641. dpp_auth_fail(auth, "Missing or invalid I-nonce");
  2642. goto fail;
  2643. }
  2644. wpa_hexdump(MSG_DEBUG, "DPP: I-nonce", i_nonce, i_nonce_len);
  2645. if (os_memcmp(auth->i_nonce, i_nonce, i_nonce_len) != 0) {
  2646. dpp_auth_fail(auth, "I-nonce mismatch");
  2647. goto fail;
  2648. }
  2649. r_capab = dpp_get_attr(unwrapped, unwrapped_len,
  2650. DPP_ATTR_R_CAPABILITIES,
  2651. &r_capab_len);
  2652. if (!r_capab || r_capab_len < 1) {
  2653. dpp_auth_fail(auth, "Missing or invalid R-capabilities");
  2654. goto fail;
  2655. }
  2656. auth->r_capab = r_capab[0];
  2657. wpa_printf(MSG_DEBUG, "DPP: R-capabilities: 0x%02x", auth->r_capab);
  2658. if (status == DPP_STATUS_NOT_COMPATIBLE) {
  2659. wpa_msg(auth->msg_ctx, MSG_INFO, DPP_EVENT_NOT_COMPATIBLE
  2660. "r-capab=0x%02x", auth->r_capab);
  2661. } else if (status == DPP_STATUS_RESPONSE_PENDING) {
  2662. u8 role = auth->r_capab & DPP_CAPAB_ROLE_MASK;
  2663. if ((auth->configurator && role != DPP_CAPAB_ENROLLEE) ||
  2664. (!auth->configurator && role != DPP_CAPAB_CONFIGURATOR)) {
  2665. wpa_msg(auth->msg_ctx, MSG_INFO,
  2666. DPP_EVENT_FAIL "Unexpected role in R-capabilities 0x%02x",
  2667. role);
  2668. } else {
  2669. wpa_printf(MSG_DEBUG,
  2670. "DPP: Continue waiting for full DPP Authentication Response");
  2671. wpa_msg(auth->msg_ctx, MSG_INFO, DPP_EVENT_RESPONSE_PENDING);
  2672. }
  2673. }
  2674. fail:
  2675. bin_clear_free(unwrapped, unwrapped_len);
  2676. }
  2677. struct wpabuf *
  2678. dpp_auth_resp_rx(struct dpp_authentication *auth, const u8 *hdr,
  2679. const u8 *attr_start, size_t attr_len)
  2680. {
  2681. EVP_PKEY *pr;
  2682. EVP_PKEY_CTX *ctx = NULL;
  2683. size_t secret_len;
  2684. const u8 *addr[2];
  2685. size_t len[2];
  2686. u8 *unwrapped = NULL, *unwrapped2 = NULL;
  2687. size_t unwrapped_len = 0, unwrapped2_len = 0;
  2688. const u8 *r_bootstrap, *i_bootstrap, *wrapped_data, *status, *r_proto,
  2689. *r_nonce, *i_nonce, *r_capab, *wrapped2, *r_auth;
  2690. u16 r_bootstrap_len, i_bootstrap_len, wrapped_data_len, status_len,
  2691. r_proto_len, r_nonce_len, i_nonce_len, r_capab_len,
  2692. wrapped2_len, r_auth_len;
  2693. u8 r_auth2[DPP_MAX_HASH_LEN];
  2694. u8 role;
  2695. if (!auth->initiator) {
  2696. dpp_auth_fail(auth, "Unexpected Authentication Response");
  2697. return NULL;
  2698. }
  2699. auth->waiting_auth_resp = 0;
  2700. wrapped_data = dpp_get_attr(attr_start, attr_len, DPP_ATTR_WRAPPED_DATA,
  2701. &wrapped_data_len);
  2702. if (!wrapped_data || wrapped_data_len < AES_BLOCK_SIZE) {
  2703. dpp_auth_fail(auth,
  2704. "Missing or invalid required Wrapped Data attribute");
  2705. return NULL;
  2706. }
  2707. wpa_hexdump(MSG_DEBUG, "DPP: Wrapped data",
  2708. wrapped_data, wrapped_data_len);
  2709. attr_len = wrapped_data - 4 - attr_start;
  2710. r_bootstrap = dpp_get_attr(attr_start, attr_len,
  2711. DPP_ATTR_R_BOOTSTRAP_KEY_HASH,
  2712. &r_bootstrap_len);
  2713. if (!r_bootstrap || r_bootstrap_len != SHA256_MAC_LEN) {
  2714. dpp_auth_fail(auth,
  2715. "Missing or invalid required Responder Bootstrapping Key Hash attribute");
  2716. return NULL;
  2717. }
  2718. wpa_hexdump(MSG_DEBUG, "DPP: Responder Bootstrapping Key Hash",
  2719. r_bootstrap, r_bootstrap_len);
  2720. if (os_memcmp(r_bootstrap, auth->peer_bi->pubkey_hash,
  2721. SHA256_MAC_LEN) != 0) {
  2722. dpp_auth_fail(auth,
  2723. "Unexpected Responder Bootstrapping Key Hash value");
  2724. wpa_hexdump(MSG_DEBUG,
  2725. "DPP: Expected Responder Bootstrapping Key Hash",
  2726. auth->peer_bi->pubkey_hash, SHA256_MAC_LEN);
  2727. return NULL;
  2728. }
  2729. i_bootstrap = dpp_get_attr(attr_start, attr_len,
  2730. DPP_ATTR_I_BOOTSTRAP_KEY_HASH,
  2731. &i_bootstrap_len);
  2732. if (i_bootstrap) {
  2733. if (i_bootstrap_len != SHA256_MAC_LEN) {
  2734. dpp_auth_fail(auth,
  2735. "Invalid Initiator Bootstrapping Key Hash attribute");
  2736. return NULL;
  2737. }
  2738. wpa_hexdump(MSG_MSGDUMP,
  2739. "DPP: Initiator Bootstrapping Key Hash",
  2740. i_bootstrap, i_bootstrap_len);
  2741. if (!auth->own_bi ||
  2742. os_memcmp(i_bootstrap, auth->own_bi->pubkey_hash,
  2743. SHA256_MAC_LEN) != 0) {
  2744. dpp_auth_fail(auth,
  2745. "Initiator Bootstrapping Key Hash attribute did not match");
  2746. return NULL;
  2747. }
  2748. } else if (auth->own_bi && auth->own_bi->type == DPP_BOOTSTRAP_PKEX) {
  2749. /* PKEX bootstrapping mandates use of mutual authentication */
  2750. dpp_auth_fail(auth,
  2751. "Missing Initiator Bootstrapping Key Hash attribute");
  2752. return NULL;
  2753. }
  2754. status = dpp_get_attr(attr_start, attr_len, DPP_ATTR_STATUS,
  2755. &status_len);
  2756. if (!status || status_len < 1) {
  2757. dpp_auth_fail(auth,
  2758. "Missing or invalid required DPP Status attribute");
  2759. return NULL;
  2760. }
  2761. wpa_printf(MSG_DEBUG, "DPP: Status %u", status[0]);
  2762. auth->auth_resp_status = status[0];
  2763. if (status[0] != DPP_STATUS_OK) {
  2764. dpp_auth_resp_rx_status(auth, hdr, attr_start,
  2765. attr_len, wrapped_data,
  2766. wrapped_data_len, status[0]);
  2767. return NULL;
  2768. }
  2769. if (!i_bootstrap && auth->own_bi) {
  2770. wpa_printf(MSG_DEBUG,
  2771. "DPP: Responder decided not to use mutual authentication");
  2772. auth->own_bi = NULL;
  2773. }
  2774. r_proto = dpp_get_attr(attr_start, attr_len, DPP_ATTR_R_PROTOCOL_KEY,
  2775. &r_proto_len);
  2776. if (!r_proto) {
  2777. dpp_auth_fail(auth,
  2778. "Missing required Responder Protocol Key attribute");
  2779. return NULL;
  2780. }
  2781. wpa_hexdump(MSG_MSGDUMP, "DPP: Responder Protocol Key",
  2782. r_proto, r_proto_len);
  2783. /* N = pI * PR */
  2784. pr = dpp_set_pubkey_point(auth->own_protocol_key, r_proto, r_proto_len);
  2785. if (!pr) {
  2786. dpp_auth_fail(auth, "Invalid Responder Protocol Key");
  2787. return NULL;
  2788. }
  2789. dpp_debug_print_key("Peer (Responder) Protocol Key", pr);
  2790. ctx = EVP_PKEY_CTX_new(auth->own_protocol_key, NULL);
  2791. if (!ctx ||
  2792. EVP_PKEY_derive_init(ctx) != 1 ||
  2793. EVP_PKEY_derive_set_peer(ctx, pr) != 1 ||
  2794. EVP_PKEY_derive(ctx, NULL, &secret_len) != 1 ||
  2795. secret_len > DPP_MAX_SHARED_SECRET_LEN ||
  2796. EVP_PKEY_derive(ctx, auth->Nx, &secret_len) != 1) {
  2797. wpa_printf(MSG_ERROR,
  2798. "DPP: Failed to derive ECDH shared secret: %s",
  2799. ERR_error_string(ERR_get_error(), NULL));
  2800. dpp_auth_fail(auth, "Failed to derive ECDH shared secret");
  2801. goto fail;
  2802. }
  2803. EVP_PKEY_CTX_free(ctx);
  2804. ctx = NULL;
  2805. auth->peer_protocol_key = pr;
  2806. pr = NULL;
  2807. wpa_hexdump_key(MSG_DEBUG, "DPP: ECDH shared secret (N.x)",
  2808. auth->Nx, auth->secret_len);
  2809. if (dpp_derive_k2(auth->Nx, auth->secret_len, auth->k2,
  2810. auth->curve->hash_len) < 0)
  2811. goto fail;
  2812. addr[0] = hdr;
  2813. len[0] = DPP_HDR_LEN;
  2814. addr[1] = attr_start;
  2815. len[1] = attr_len;
  2816. wpa_hexdump(MSG_DEBUG, "DDP: AES-SIV AD[0]", addr[0], len[0]);
  2817. wpa_hexdump(MSG_DEBUG, "DDP: AES-SIV AD[1]", addr[1], len[1]);
  2818. wpa_hexdump(MSG_DEBUG, "DPP: AES-SIV ciphertext",
  2819. wrapped_data, wrapped_data_len);
  2820. unwrapped_len = wrapped_data_len - AES_BLOCK_SIZE;
  2821. unwrapped = os_malloc(unwrapped_len);
  2822. if (!unwrapped)
  2823. goto fail;
  2824. if (aes_siv_decrypt(auth->k2, auth->curve->hash_len,
  2825. wrapped_data, wrapped_data_len,
  2826. 2, addr, len, unwrapped) < 0) {
  2827. dpp_auth_fail(auth, "AES-SIV decryption failed");
  2828. goto fail;
  2829. }
  2830. wpa_hexdump(MSG_DEBUG, "DPP: AES-SIV cleartext",
  2831. unwrapped, unwrapped_len);
  2832. if (dpp_check_attrs(unwrapped, unwrapped_len) < 0) {
  2833. dpp_auth_fail(auth, "Invalid attribute in unwrapped data");
  2834. goto fail;
  2835. }
  2836. r_nonce = dpp_get_attr(unwrapped, unwrapped_len, DPP_ATTR_R_NONCE,
  2837. &r_nonce_len);
  2838. if (!r_nonce || r_nonce_len != auth->curve->nonce_len) {
  2839. dpp_auth_fail(auth, "DPP: Missing or invalid R-nonce");
  2840. goto fail;
  2841. }
  2842. wpa_hexdump(MSG_DEBUG, "DPP: R-nonce", r_nonce, r_nonce_len);
  2843. os_memcpy(auth->r_nonce, r_nonce, r_nonce_len);
  2844. i_nonce = dpp_get_attr(unwrapped, unwrapped_len, DPP_ATTR_I_NONCE,
  2845. &i_nonce_len);
  2846. if (!i_nonce || i_nonce_len != auth->curve->nonce_len) {
  2847. dpp_auth_fail(auth, "Missing or invalid I-nonce");
  2848. goto fail;
  2849. }
  2850. wpa_hexdump(MSG_DEBUG, "DPP: I-nonce", i_nonce, i_nonce_len);
  2851. if (os_memcmp(auth->i_nonce, i_nonce, i_nonce_len) != 0) {
  2852. dpp_auth_fail(auth, "I-nonce mismatch");
  2853. goto fail;
  2854. }
  2855. if (auth->own_bi && auth->peer_bi) {
  2856. /* Mutual authentication */
  2857. if (dpp_auth_derive_l_initiator(auth) < 0)
  2858. goto fail;
  2859. }
  2860. r_capab = dpp_get_attr(unwrapped, unwrapped_len,
  2861. DPP_ATTR_R_CAPABILITIES,
  2862. &r_capab_len);
  2863. if (!r_capab || r_capab_len < 1) {
  2864. dpp_auth_fail(auth, "Missing or invalid R-capabilities");
  2865. goto fail;
  2866. }
  2867. auth->r_capab = r_capab[0];
  2868. wpa_printf(MSG_DEBUG, "DPP: R-capabilities: 0x%02x", auth->r_capab);
  2869. role = auth->r_capab & DPP_CAPAB_ROLE_MASK;
  2870. if ((auth->allowed_roles ==
  2871. (DPP_CAPAB_CONFIGURATOR | DPP_CAPAB_ENROLLEE)) &&
  2872. (role == DPP_CAPAB_CONFIGURATOR || role == DPP_CAPAB_ENROLLEE)) {
  2873. /* Peer selected its role, so move from "either role" to the
  2874. * role that is compatible with peer's selection. */
  2875. auth->configurator = role == DPP_CAPAB_ENROLLEE;
  2876. wpa_printf(MSG_DEBUG, "DPP: Acting as %s",
  2877. auth->configurator ? "Configurator" : "Enrollee");
  2878. } else if ((auth->configurator && role != DPP_CAPAB_ENROLLEE) ||
  2879. (!auth->configurator && role != DPP_CAPAB_CONFIGURATOR)) {
  2880. wpa_printf(MSG_DEBUG, "DPP: Incompatible role selection");
  2881. wpa_msg(auth->msg_ctx, MSG_INFO, DPP_EVENT_FAIL
  2882. "Unexpected role in R-capabilities 0x%02x",
  2883. role);
  2884. if (role != DPP_CAPAB_ENROLLEE &&
  2885. role != DPP_CAPAB_CONFIGURATOR)
  2886. goto fail;
  2887. bin_clear_free(unwrapped, unwrapped_len);
  2888. auth->remove_on_tx_status = 1;
  2889. return dpp_auth_build_conf(auth, DPP_STATUS_NOT_COMPATIBLE);
  2890. }
  2891. wrapped2 = dpp_get_attr(unwrapped, unwrapped_len,
  2892. DPP_ATTR_WRAPPED_DATA, &wrapped2_len);
  2893. if (!wrapped2 || wrapped2_len < AES_BLOCK_SIZE) {
  2894. dpp_auth_fail(auth,
  2895. "Missing or invalid Secondary Wrapped Data");
  2896. goto fail;
  2897. }
  2898. wpa_hexdump(MSG_DEBUG, "DPP: AES-SIV ciphertext",
  2899. wrapped2, wrapped2_len);
  2900. if (dpp_derive_ke(auth, auth->ke, auth->curve->hash_len) < 0)
  2901. goto fail;
  2902. unwrapped2_len = wrapped2_len - AES_BLOCK_SIZE;
  2903. unwrapped2 = os_malloc(unwrapped2_len);
  2904. if (!unwrapped2)
  2905. goto fail;
  2906. if (aes_siv_decrypt(auth->ke, auth->curve->hash_len,
  2907. wrapped2, wrapped2_len,
  2908. 0, NULL, NULL, unwrapped2) < 0) {
  2909. dpp_auth_fail(auth, "AES-SIV decryption failed");
  2910. goto fail;
  2911. }
  2912. wpa_hexdump(MSG_DEBUG, "DPP: AES-SIV cleartext",
  2913. unwrapped2, unwrapped2_len);
  2914. if (dpp_check_attrs(unwrapped2, unwrapped2_len) < 0) {
  2915. dpp_auth_fail(auth,
  2916. "Invalid attribute in secondary unwrapped data");
  2917. goto fail;
  2918. }
  2919. r_auth = dpp_get_attr(unwrapped2, unwrapped2_len, DPP_ATTR_R_AUTH_TAG,
  2920. &r_auth_len);
  2921. if (!r_auth || r_auth_len != auth->curve->hash_len) {
  2922. dpp_auth_fail(auth,
  2923. "Missing or invalid Responder Authenticating Tag");
  2924. goto fail;
  2925. }
  2926. wpa_hexdump(MSG_DEBUG, "DPP: Received Responder Authenticating Tag",
  2927. r_auth, r_auth_len);
  2928. /* R-auth' = H(I-nonce | R-nonce | PI.x | PR.x | [BI.x |] BR.x | 0) */
  2929. if (dpp_gen_r_auth(auth, r_auth2) < 0)
  2930. goto fail;
  2931. wpa_hexdump(MSG_DEBUG, "DPP: Calculated Responder Authenticating Tag",
  2932. r_auth2, r_auth_len);
  2933. if (os_memcmp(r_auth, r_auth2, r_auth_len) != 0) {
  2934. dpp_auth_fail(auth, "Mismatching Responder Authenticating Tag");
  2935. bin_clear_free(unwrapped, unwrapped_len);
  2936. bin_clear_free(unwrapped2, unwrapped2_len);
  2937. auth->remove_on_tx_status = 1;
  2938. return dpp_auth_build_conf(auth, DPP_STATUS_AUTH_FAILURE);
  2939. }
  2940. bin_clear_free(unwrapped, unwrapped_len);
  2941. bin_clear_free(unwrapped2, unwrapped2_len);
  2942. #ifdef CONFIG_TESTING_OPTIONS
  2943. if (dpp_test == DPP_TEST_AUTH_RESP_IN_PLACE_OF_CONF) {
  2944. wpa_printf(MSG_INFO,
  2945. "DPP: TESTING - Authentication Response in place of Confirm");
  2946. if (dpp_auth_build_resp_ok(auth) < 0)
  2947. return NULL;
  2948. return wpabuf_dup(auth->resp_msg);
  2949. }
  2950. #endif /* CONFIG_TESTING_OPTIONS */
  2951. return dpp_auth_build_conf(auth, DPP_STATUS_OK);
  2952. fail:
  2953. bin_clear_free(unwrapped, unwrapped_len);
  2954. bin_clear_free(unwrapped2, unwrapped2_len);
  2955. EVP_PKEY_free(pr);
  2956. EVP_PKEY_CTX_free(ctx);
  2957. return NULL;
  2958. }
  2959. static int dpp_auth_conf_rx_failure(struct dpp_authentication *auth,
  2960. const u8 *hdr,
  2961. const u8 *attr_start, size_t attr_len,
  2962. const u8 *wrapped_data,
  2963. u16 wrapped_data_len,
  2964. enum dpp_status_error status)
  2965. {
  2966. const u8 *addr[2];
  2967. size_t len[2];
  2968. u8 *unwrapped = NULL;
  2969. size_t unwrapped_len = 0;
  2970. const u8 *r_nonce;
  2971. u16 r_nonce_len;
  2972. /* Authentication Confirm failure cases are expected to include
  2973. * {R-nonce}k2 in the Wrapped Data attribute. */
  2974. addr[0] = hdr;
  2975. len[0] = DPP_HDR_LEN;
  2976. addr[1] = attr_start;
  2977. len[1] = attr_len;
  2978. wpa_hexdump(MSG_DEBUG, "DDP: AES-SIV AD[0]", addr[0], len[0]);
  2979. wpa_hexdump(MSG_DEBUG, "DDP: AES-SIV AD[1]", addr[1], len[1]);
  2980. wpa_hexdump(MSG_DEBUG, "DPP: AES-SIV ciphertext",
  2981. wrapped_data, wrapped_data_len);
  2982. unwrapped_len = wrapped_data_len - AES_BLOCK_SIZE;
  2983. unwrapped = os_malloc(unwrapped_len);
  2984. if (!unwrapped) {
  2985. dpp_auth_fail(auth, "Authentication failed");
  2986. goto fail;
  2987. }
  2988. if (aes_siv_decrypt(auth->k2, auth->curve->hash_len,
  2989. wrapped_data, wrapped_data_len,
  2990. 2, addr, len, unwrapped) < 0) {
  2991. dpp_auth_fail(auth, "AES-SIV decryption failed");
  2992. goto fail;
  2993. }
  2994. wpa_hexdump(MSG_DEBUG, "DPP: AES-SIV cleartext",
  2995. unwrapped, unwrapped_len);
  2996. if (dpp_check_attrs(unwrapped, unwrapped_len) < 0) {
  2997. dpp_auth_fail(auth, "Invalid attribute in unwrapped data");
  2998. goto fail;
  2999. }
  3000. r_nonce = dpp_get_attr(unwrapped, unwrapped_len, DPP_ATTR_R_NONCE,
  3001. &r_nonce_len);
  3002. if (!r_nonce || r_nonce_len != auth->curve->nonce_len) {
  3003. dpp_auth_fail(auth, "DPP: Missing or invalid R-nonce");
  3004. goto fail;
  3005. }
  3006. if (os_memcmp(r_nonce, auth->r_nonce, r_nonce_len) != 0) {
  3007. wpa_hexdump(MSG_DEBUG, "DPP: Received R-nonce",
  3008. r_nonce, r_nonce_len);
  3009. wpa_hexdump(MSG_DEBUG, "DPP: Expected R-nonce",
  3010. auth->r_nonce, r_nonce_len);
  3011. dpp_auth_fail(auth, "R-nonce mismatch");
  3012. goto fail;
  3013. }
  3014. if (status == DPP_STATUS_NOT_COMPATIBLE)
  3015. dpp_auth_fail(auth, "Peer reported incompatible R-capab role");
  3016. else if (status == DPP_STATUS_AUTH_FAILURE)
  3017. dpp_auth_fail(auth, "Peer reported authentication failure)");
  3018. fail:
  3019. bin_clear_free(unwrapped, unwrapped_len);
  3020. return -1;
  3021. }
  3022. int dpp_auth_conf_rx(struct dpp_authentication *auth, const u8 *hdr,
  3023. const u8 *attr_start, size_t attr_len)
  3024. {
  3025. const u8 *r_bootstrap, *i_bootstrap, *wrapped_data, *status, *i_auth;
  3026. u16 r_bootstrap_len, i_bootstrap_len, wrapped_data_len, status_len,
  3027. i_auth_len;
  3028. const u8 *addr[2];
  3029. size_t len[2];
  3030. u8 *unwrapped = NULL;
  3031. size_t unwrapped_len = 0;
  3032. u8 i_auth2[DPP_MAX_HASH_LEN];
  3033. if (auth->initiator) {
  3034. dpp_auth_fail(auth, "Unexpected Authentication Confirm");
  3035. return NULL;
  3036. }
  3037. auth->waiting_auth_conf = 0;
  3038. wrapped_data = dpp_get_attr(attr_start, attr_len, DPP_ATTR_WRAPPED_DATA,
  3039. &wrapped_data_len);
  3040. if (!wrapped_data || wrapped_data_len < AES_BLOCK_SIZE) {
  3041. dpp_auth_fail(auth,
  3042. "Missing or invalid required Wrapped Data attribute");
  3043. return -1;
  3044. }
  3045. wpa_hexdump(MSG_DEBUG, "DPP: Wrapped data",
  3046. wrapped_data, wrapped_data_len);
  3047. attr_len = wrapped_data - 4 - attr_start;
  3048. r_bootstrap = dpp_get_attr(attr_start, attr_len,
  3049. DPP_ATTR_R_BOOTSTRAP_KEY_HASH,
  3050. &r_bootstrap_len);
  3051. if (!r_bootstrap || r_bootstrap_len != SHA256_MAC_LEN) {
  3052. dpp_auth_fail(auth,
  3053. "Missing or invalid required Responder Bootstrapping Key Hash attribute");
  3054. return -1;
  3055. }
  3056. wpa_hexdump(MSG_DEBUG, "DPP: Responder Bootstrapping Key Hash",
  3057. r_bootstrap, r_bootstrap_len);
  3058. if (os_memcmp(r_bootstrap, auth->own_bi->pubkey_hash,
  3059. SHA256_MAC_LEN) != 0) {
  3060. wpa_hexdump(MSG_DEBUG,
  3061. "DPP: Expected Responder Bootstrapping Key Hash",
  3062. auth->peer_bi->pubkey_hash, SHA256_MAC_LEN);
  3063. dpp_auth_fail(auth,
  3064. "Responder Bootstrapping Key Hash mismatch");
  3065. return -1;
  3066. }
  3067. i_bootstrap = dpp_get_attr(attr_start, attr_len,
  3068. DPP_ATTR_I_BOOTSTRAP_KEY_HASH,
  3069. &i_bootstrap_len);
  3070. if (i_bootstrap) {
  3071. if (i_bootstrap_len != SHA256_MAC_LEN) {
  3072. dpp_auth_fail(auth,
  3073. "Invalid Initiator Bootstrapping Key Hash attribute");
  3074. return -1;
  3075. }
  3076. wpa_hexdump(MSG_MSGDUMP,
  3077. "DPP: Initiator Bootstrapping Key Hash",
  3078. i_bootstrap, i_bootstrap_len);
  3079. if (!auth->peer_bi ||
  3080. os_memcmp(i_bootstrap, auth->peer_bi->pubkey_hash,
  3081. SHA256_MAC_LEN) != 0) {
  3082. dpp_auth_fail(auth,
  3083. "Initiator Bootstrapping Key Hash mismatch");
  3084. return -1;
  3085. }
  3086. } else if (auth->own_bi && auth->peer_bi) {
  3087. /* Mutual authentication and peer did not include its
  3088. * Bootstrapping Key Hash attribute. */
  3089. dpp_auth_fail(auth,
  3090. "Missing Initiator Bootstrapping Key Hash attribute");
  3091. return -1;
  3092. }
  3093. status = dpp_get_attr(attr_start, attr_len, DPP_ATTR_STATUS,
  3094. &status_len);
  3095. if (!status || status_len < 1) {
  3096. dpp_auth_fail(auth,
  3097. "Missing or invalid required DPP Status attribute");
  3098. return -1;
  3099. }
  3100. wpa_printf(MSG_DEBUG, "DPP: Status %u", status[0]);
  3101. if (status[0] == DPP_STATUS_NOT_COMPATIBLE ||
  3102. status[0] == DPP_STATUS_AUTH_FAILURE)
  3103. return dpp_auth_conf_rx_failure(auth, hdr, attr_start,
  3104. attr_len, wrapped_data,
  3105. wrapped_data_len, status[0]);
  3106. if (status[0] != DPP_STATUS_OK) {
  3107. dpp_auth_fail(auth, "Authentication failed");
  3108. return -1;
  3109. }
  3110. addr[0] = hdr;
  3111. len[0] = DPP_HDR_LEN;
  3112. addr[1] = attr_start;
  3113. len[1] = attr_len;
  3114. wpa_hexdump(MSG_DEBUG, "DDP: AES-SIV AD[0]", addr[0], len[0]);
  3115. wpa_hexdump(MSG_DEBUG, "DDP: AES-SIV AD[1]", addr[1], len[1]);
  3116. wpa_hexdump(MSG_DEBUG, "DPP: AES-SIV ciphertext",
  3117. wrapped_data, wrapped_data_len);
  3118. unwrapped_len = wrapped_data_len - AES_BLOCK_SIZE;
  3119. unwrapped = os_malloc(unwrapped_len);
  3120. if (!unwrapped)
  3121. return -1;
  3122. if (aes_siv_decrypt(auth->ke, auth->curve->hash_len,
  3123. wrapped_data, wrapped_data_len,
  3124. 2, addr, len, unwrapped) < 0) {
  3125. dpp_auth_fail(auth, "AES-SIV decryption failed");
  3126. goto fail;
  3127. }
  3128. wpa_hexdump(MSG_DEBUG, "DPP: AES-SIV cleartext",
  3129. unwrapped, unwrapped_len);
  3130. if (dpp_check_attrs(unwrapped, unwrapped_len) < 0) {
  3131. dpp_auth_fail(auth, "Invalid attribute in unwrapped data");
  3132. goto fail;
  3133. }
  3134. i_auth = dpp_get_attr(unwrapped, unwrapped_len, DPP_ATTR_I_AUTH_TAG,
  3135. &i_auth_len);
  3136. if (!i_auth || i_auth_len != auth->curve->hash_len) {
  3137. dpp_auth_fail(auth,
  3138. "Missing or invalid Initiator Authenticating Tag");
  3139. goto fail;
  3140. }
  3141. wpa_hexdump(MSG_DEBUG, "DPP: Received Initiator Authenticating Tag",
  3142. i_auth, i_auth_len);
  3143. /* I-auth' = H(R-nonce | I-nonce | PR.x | PI.x | BR.x | [BI.x |] 1) */
  3144. if (dpp_gen_i_auth(auth, i_auth2) < 0)
  3145. goto fail;
  3146. wpa_hexdump(MSG_DEBUG, "DPP: Calculated Initiator Authenticating Tag",
  3147. i_auth2, i_auth_len);
  3148. if (os_memcmp(i_auth, i_auth2, i_auth_len) != 0) {
  3149. dpp_auth_fail(auth, "Mismatching Initiator Authenticating Tag");
  3150. goto fail;
  3151. }
  3152. bin_clear_free(unwrapped, unwrapped_len);
  3153. dpp_auth_success(auth);
  3154. return 0;
  3155. fail:
  3156. bin_clear_free(unwrapped, unwrapped_len);
  3157. return -1;
  3158. }
  3159. void dpp_configuration_free(struct dpp_configuration *conf)
  3160. {
  3161. if (!conf)
  3162. return;
  3163. str_clear_free(conf->passphrase);
  3164. bin_clear_free(conf, sizeof(*conf));
  3165. }
  3166. void dpp_auth_deinit(struct dpp_authentication *auth)
  3167. {
  3168. if (!auth)
  3169. return;
  3170. dpp_configuration_free(auth->conf_ap);
  3171. dpp_configuration_free(auth->conf_sta);
  3172. EVP_PKEY_free(auth->own_protocol_key);
  3173. EVP_PKEY_free(auth->peer_protocol_key);
  3174. wpabuf_free(auth->req_msg);
  3175. wpabuf_free(auth->resp_msg);
  3176. wpabuf_free(auth->conf_req);
  3177. os_free(auth->connector);
  3178. wpabuf_free(auth->net_access_key);
  3179. wpabuf_free(auth->c_sign_key);
  3180. #ifdef CONFIG_TESTING_OPTIONS
  3181. os_free(auth->config_obj_override);
  3182. os_free(auth->discovery_override);
  3183. os_free(auth->groups_override);
  3184. #endif /* CONFIG_TESTING_OPTIONS */
  3185. bin_clear_free(auth, sizeof(*auth));
  3186. }
  3187. static struct wpabuf *
  3188. dpp_build_conf_start(struct dpp_authentication *auth,
  3189. struct dpp_configuration *conf, size_t tailroom)
  3190. {
  3191. struct wpabuf *buf;
  3192. char ssid[6 * sizeof(conf->ssid) + 1];
  3193. #ifdef CONFIG_TESTING_OPTIONS
  3194. if (auth->discovery_override)
  3195. tailroom += os_strlen(auth->discovery_override);
  3196. #endif /* CONFIG_TESTING_OPTIONS */
  3197. buf = wpabuf_alloc(200 + tailroom);
  3198. if (!buf)
  3199. return NULL;
  3200. wpabuf_put_str(buf, "{\"wi-fi_tech\":\"infra\",\"discovery\":");
  3201. #ifdef CONFIG_TESTING_OPTIONS
  3202. if (auth->discovery_override) {
  3203. wpa_printf(MSG_DEBUG, "DPP: TESTING - discovery override: '%s'",
  3204. auth->discovery_override);
  3205. wpabuf_put_str(buf, auth->discovery_override);
  3206. wpabuf_put_u8(buf, ',');
  3207. return buf;
  3208. }
  3209. #endif /* CONFIG_TESTING_OPTIONS */
  3210. wpabuf_put_str(buf, "{\"ssid\":\"");
  3211. json_escape_string(ssid, sizeof(ssid),
  3212. (const char *) conf->ssid, conf->ssid_len);
  3213. wpabuf_put_str(buf, ssid);
  3214. wpabuf_put_str(buf, "\"},");
  3215. return buf;
  3216. }
  3217. static int dpp_build_jwk(struct wpabuf *buf, const char *name, EVP_PKEY *key,
  3218. const char *kid, const struct dpp_curve_params *curve)
  3219. {
  3220. struct wpabuf *pub;
  3221. const u8 *pos;
  3222. char *x = NULL, *y = NULL;
  3223. int ret = -1;
  3224. pub = dpp_get_pubkey_point(key, 0);
  3225. if (!pub)
  3226. goto fail;
  3227. pos = wpabuf_head(pub);
  3228. x = (char *) base64_url_encode(pos, curve->prime_len, NULL, 0);
  3229. pos += curve->prime_len;
  3230. y = (char *) base64_url_encode(pos, curve->prime_len, NULL, 0);
  3231. if (!x || !y)
  3232. goto fail;
  3233. wpabuf_put_str(buf, "\"");
  3234. wpabuf_put_str(buf, name);
  3235. wpabuf_put_str(buf, "\":{\"kty\":\"EC\",\"crv\":\"");
  3236. wpabuf_put_str(buf, curve->jwk_crv);
  3237. wpabuf_put_str(buf, "\",\"x\":\"");
  3238. wpabuf_put_str(buf, x);
  3239. wpabuf_put_str(buf, "\",\"y\":\"");
  3240. wpabuf_put_str(buf, y);
  3241. if (kid) {
  3242. wpabuf_put_str(buf, "\",\"kid\":\"");
  3243. wpabuf_put_str(buf, kid);
  3244. }
  3245. wpabuf_put_str(buf, "\"}");
  3246. ret = 0;
  3247. fail:
  3248. wpabuf_free(pub);
  3249. os_free(x);
  3250. os_free(y);
  3251. return ret;
  3252. }
  3253. static struct wpabuf *
  3254. dpp_build_conf_obj_dpp(struct dpp_authentication *auth, int ap,
  3255. struct dpp_configuration *conf)
  3256. {
  3257. struct wpabuf *buf = NULL;
  3258. char *signed1 = NULL, *signed2 = NULL, *signed3 = NULL;
  3259. size_t tailroom;
  3260. const struct dpp_curve_params *curve;
  3261. char jws_prot_hdr[100];
  3262. size_t signed1_len, signed2_len, signed3_len;
  3263. struct wpabuf *dppcon = NULL;
  3264. unsigned char *signature = NULL;
  3265. const unsigned char *p;
  3266. size_t signature_len;
  3267. EVP_MD_CTX *md_ctx = NULL;
  3268. ECDSA_SIG *sig = NULL;
  3269. char *dot = ".";
  3270. const EVP_MD *sign_md;
  3271. const BIGNUM *r, *s;
  3272. size_t extra_len = 1000;
  3273. if (!auth->conf) {
  3274. wpa_printf(MSG_INFO,
  3275. "DPP: No configurator specified - cannot generate DPP config object");
  3276. goto fail;
  3277. }
  3278. curve = auth->conf->curve;
  3279. if (curve->hash_len == SHA256_MAC_LEN) {
  3280. sign_md = EVP_sha256();
  3281. } else if (curve->hash_len == SHA384_MAC_LEN) {
  3282. sign_md = EVP_sha384();
  3283. } else if (curve->hash_len == SHA512_MAC_LEN) {
  3284. sign_md = EVP_sha512();
  3285. } else {
  3286. wpa_printf(MSG_DEBUG, "DPP: Unknown signature algorithm");
  3287. goto fail;
  3288. }
  3289. #ifdef CONFIG_TESTING_OPTIONS
  3290. if (auth->groups_override)
  3291. extra_len += os_strlen(auth->groups_override);
  3292. #endif /* CONFIG_TESTING_OPTIONS */
  3293. /* Connector (JSON dppCon object) */
  3294. dppcon = wpabuf_alloc(extra_len + 2 * auth->curve->prime_len * 4 / 3);
  3295. if (!dppcon)
  3296. goto fail;
  3297. #ifdef CONFIG_TESTING_OPTIONS
  3298. if (auth->groups_override) {
  3299. wpabuf_put_u8(dppcon, '{');
  3300. if (auth->groups_override) {
  3301. wpa_printf(MSG_DEBUG,
  3302. "DPP: TESTING - groups override: '%s'",
  3303. auth->groups_override);
  3304. wpabuf_put_str(dppcon, "\"groups\":");
  3305. wpabuf_put_str(dppcon, auth->groups_override);
  3306. wpabuf_put_u8(dppcon, ',');
  3307. }
  3308. goto skip_groups;
  3309. }
  3310. #endif /* CONFIG_TESTING_OPTIONS */
  3311. wpabuf_put_str(dppcon, "{\"groups\":[{\"groupId\":\"*\",");
  3312. wpabuf_printf(dppcon, "\"netRole\":\"%s\"}],", ap ? "ap" : "sta");
  3313. #ifdef CONFIG_TESTING_OPTIONS
  3314. skip_groups:
  3315. #endif /* CONFIG_TESTING_OPTIONS */
  3316. if (dpp_build_jwk(dppcon, "netAccessKey", auth->peer_protocol_key, NULL,
  3317. auth->curve) < 0) {
  3318. wpa_printf(MSG_DEBUG, "DPP: Failed to build netAccessKey JWK");
  3319. goto fail;
  3320. }
  3321. if (conf->netaccesskey_expiry) {
  3322. struct os_tm tm;
  3323. if (os_gmtime(conf->netaccesskey_expiry, &tm) < 0) {
  3324. wpa_printf(MSG_DEBUG,
  3325. "DPP: Failed to generate expiry string");
  3326. goto fail;
  3327. }
  3328. wpabuf_printf(dppcon,
  3329. ",\"expiry\":\"%04u-%02u-%02uT%02u:%02u:%02uZ\"",
  3330. tm.year, tm.month, tm.day,
  3331. tm.hour, tm.min, tm.sec);
  3332. }
  3333. wpabuf_put_u8(dppcon, '}');
  3334. wpa_printf(MSG_DEBUG, "DPP: dppCon: %s",
  3335. (const char *) wpabuf_head(dppcon));
  3336. os_snprintf(jws_prot_hdr, sizeof(jws_prot_hdr),
  3337. "{\"typ\":\"dppCon\",\"kid\":\"%s\",\"alg\":\"%s\"}",
  3338. auth->conf->kid, curve->jws_alg);
  3339. signed1 = (char *) base64_url_encode((unsigned char *) jws_prot_hdr,
  3340. os_strlen(jws_prot_hdr),
  3341. &signed1_len, 0);
  3342. signed2 = (char *) base64_url_encode(wpabuf_head(dppcon),
  3343. wpabuf_len(dppcon),
  3344. &signed2_len, 0);
  3345. if (!signed1 || !signed2)
  3346. goto fail;
  3347. md_ctx = EVP_MD_CTX_create();
  3348. if (!md_ctx)
  3349. goto fail;
  3350. ERR_clear_error();
  3351. if (EVP_DigestSignInit(md_ctx, NULL, sign_md, NULL,
  3352. auth->conf->csign) != 1) {
  3353. wpa_printf(MSG_DEBUG, "DPP: EVP_DigestSignInit failed: %s",
  3354. ERR_error_string(ERR_get_error(), NULL));
  3355. goto fail;
  3356. }
  3357. if (EVP_DigestSignUpdate(md_ctx, signed1, signed1_len) != 1 ||
  3358. EVP_DigestSignUpdate(md_ctx, dot, 1) != 1 ||
  3359. EVP_DigestSignUpdate(md_ctx, signed2, signed2_len) != 1) {
  3360. wpa_printf(MSG_DEBUG, "DPP: EVP_DigestSignUpdate failed: %s",
  3361. ERR_error_string(ERR_get_error(), NULL));
  3362. goto fail;
  3363. }
  3364. if (EVP_DigestSignFinal(md_ctx, NULL, &signature_len) != 1) {
  3365. wpa_printf(MSG_DEBUG, "DPP: EVP_DigestSignFinal failed: %s",
  3366. ERR_error_string(ERR_get_error(), NULL));
  3367. goto fail;
  3368. }
  3369. signature = os_malloc(signature_len);
  3370. if (!signature)
  3371. goto fail;
  3372. if (EVP_DigestSignFinal(md_ctx, signature, &signature_len) != 1) {
  3373. wpa_printf(MSG_DEBUG, "DPP: EVP_DigestSignFinal failed: %s",
  3374. ERR_error_string(ERR_get_error(), NULL));
  3375. goto fail;
  3376. }
  3377. wpa_hexdump(MSG_DEBUG, "DPP: signedConnector ECDSA signature (DER)",
  3378. signature, signature_len);
  3379. /* Convert to raw coordinates r,s */
  3380. p = signature;
  3381. sig = d2i_ECDSA_SIG(NULL, &p, signature_len);
  3382. if (!sig)
  3383. goto fail;
  3384. ECDSA_SIG_get0(sig, &r, &s);
  3385. if (dpp_bn2bin_pad(r, signature, curve->prime_len) < 0 ||
  3386. dpp_bn2bin_pad(s, signature + curve->prime_len,
  3387. curve->prime_len) < 0)
  3388. goto fail;
  3389. signature_len = 2 * curve->prime_len;
  3390. wpa_hexdump(MSG_DEBUG, "DPP: signedConnector ECDSA signature (raw r,s)",
  3391. signature, signature_len);
  3392. signed3 = (char *) base64_url_encode(signature, signature_len,
  3393. &signed3_len, 0);
  3394. if (!signed3)
  3395. goto fail;
  3396. tailroom = 1000;
  3397. tailroom += 2 * curve->prime_len * 4 / 3 + os_strlen(auth->conf->kid);
  3398. tailroom += signed1_len + signed2_len + signed3_len;
  3399. buf = dpp_build_conf_start(auth, conf, tailroom);
  3400. if (!buf)
  3401. return NULL;
  3402. wpabuf_put_str(buf, "\"cred\":{\"akm\":\"dpp\",\"signedConnector\":\"");
  3403. wpabuf_put_str(buf, signed1);
  3404. wpabuf_put_u8(buf, '.');
  3405. wpabuf_put_str(buf, signed2);
  3406. wpabuf_put_u8(buf, '.');
  3407. wpabuf_put_str(buf, signed3);
  3408. wpabuf_put_str(buf, "\",");
  3409. if (dpp_build_jwk(buf, "csign", auth->conf->csign, auth->conf->kid,
  3410. curve) < 0) {
  3411. wpa_printf(MSG_DEBUG, "DPP: Failed to build csign JWK");
  3412. goto fail;
  3413. }
  3414. wpabuf_put_str(buf, "}}");
  3415. wpa_hexdump_ascii_key(MSG_DEBUG, "DPP: Configuration Object",
  3416. wpabuf_head(buf), wpabuf_len(buf));
  3417. out:
  3418. EVP_MD_CTX_destroy(md_ctx);
  3419. ECDSA_SIG_free(sig);
  3420. os_free(signed1);
  3421. os_free(signed2);
  3422. os_free(signed3);
  3423. os_free(signature);
  3424. wpabuf_free(dppcon);
  3425. return buf;
  3426. fail:
  3427. wpa_printf(MSG_DEBUG, "DPP: Failed to build configuration object");
  3428. wpabuf_free(buf);
  3429. buf = NULL;
  3430. goto out;
  3431. }
  3432. static struct wpabuf *
  3433. dpp_build_conf_obj_legacy(struct dpp_authentication *auth, int ap,
  3434. struct dpp_configuration *conf)
  3435. {
  3436. struct wpabuf *buf;
  3437. buf = dpp_build_conf_start(auth, conf, 1000);
  3438. if (!buf)
  3439. return NULL;
  3440. wpabuf_put_str(buf, "\"cred\":{\"akm\":\"psk\",");
  3441. if (conf->passphrase) {
  3442. char pass[63 * 6 + 1];
  3443. if (os_strlen(conf->passphrase) > 63) {
  3444. wpabuf_free(buf);
  3445. return NULL;
  3446. }
  3447. json_escape_string(pass, sizeof(pass), conf->passphrase,
  3448. os_strlen(conf->passphrase));
  3449. wpabuf_put_str(buf, "\"pass\":\"");
  3450. wpabuf_put_str(buf, pass);
  3451. wpabuf_put_str(buf, "\"");
  3452. } else {
  3453. char psk[2 * sizeof(conf->psk) + 1];
  3454. wpa_snprintf_hex(psk, sizeof(psk),
  3455. conf->psk, sizeof(conf->psk));
  3456. wpabuf_put_str(buf, "\"psk_hex\":\"");
  3457. wpabuf_put_str(buf, psk);
  3458. wpabuf_put_str(buf, "\"");
  3459. }
  3460. wpabuf_put_str(buf, "}}");
  3461. wpa_hexdump_ascii_key(MSG_DEBUG, "DPP: Configuration Object (legacy)",
  3462. wpabuf_head(buf), wpabuf_len(buf));
  3463. return buf;
  3464. }
  3465. static struct wpabuf *
  3466. dpp_build_conf_obj(struct dpp_authentication *auth, int ap)
  3467. {
  3468. struct dpp_configuration *conf;
  3469. #ifdef CONFIG_TESTING_OPTIONS
  3470. if (auth->config_obj_override) {
  3471. wpa_printf(MSG_DEBUG, "DPP: Testing - Config Object override");
  3472. return wpabuf_alloc_copy(auth->config_obj_override,
  3473. os_strlen(auth->config_obj_override));
  3474. }
  3475. #endif /* CONFIG_TESTING_OPTIONS */
  3476. conf = ap ? auth->conf_ap : auth->conf_sta;
  3477. if (!conf) {
  3478. wpa_printf(MSG_DEBUG,
  3479. "DPP: No configuration available for Enrollee(%s) - reject configuration request",
  3480. ap ? "ap" : "sta");
  3481. return NULL;
  3482. }
  3483. if (conf->dpp)
  3484. return dpp_build_conf_obj_dpp(auth, ap, conf);
  3485. return dpp_build_conf_obj_legacy(auth, ap, conf);
  3486. }
  3487. static struct wpabuf *
  3488. dpp_build_conf_resp(struct dpp_authentication *auth, const u8 *e_nonce,
  3489. u16 e_nonce_len, int ap)
  3490. {
  3491. struct wpabuf *conf;
  3492. size_t clear_len, attr_len;
  3493. struct wpabuf *clear = NULL, *msg = NULL;
  3494. u8 *wrapped;
  3495. const u8 *addr[1];
  3496. size_t len[1];
  3497. enum dpp_status_error status;
  3498. conf = dpp_build_conf_obj(auth, ap);
  3499. if (conf) {
  3500. wpa_hexdump_ascii(MSG_DEBUG, "DPP: configurationObject JSON",
  3501. wpabuf_head(conf), wpabuf_len(conf));
  3502. }
  3503. status = conf ? DPP_STATUS_OK : DPP_STATUS_CONFIGURE_FAILURE;
  3504. /* { E-nonce, configurationObject}ke */
  3505. clear_len = 4 + e_nonce_len;
  3506. if (conf)
  3507. clear_len += 4 + wpabuf_len(conf);
  3508. clear = wpabuf_alloc(clear_len);
  3509. attr_len = 4 + 1 + 4 + clear_len + AES_BLOCK_SIZE;
  3510. #ifdef CONFIG_TESTING_OPTIONS
  3511. if (dpp_test == DPP_TEST_AFTER_WRAPPED_DATA_CONF_RESP)
  3512. attr_len += 4;
  3513. #endif /* CONFIG_TESTING_OPTIONS */
  3514. msg = wpabuf_alloc(attr_len);
  3515. if (!clear || !msg)
  3516. goto fail;
  3517. #ifdef CONFIG_TESTING_OPTIONS
  3518. if (dpp_test == DPP_TEST_NO_E_NONCE_CONF_RESP) {
  3519. wpa_printf(MSG_INFO, "DPP: TESTING - no E-nonce");
  3520. goto skip_e_nonce;
  3521. }
  3522. if (dpp_test == DPP_TEST_E_NONCE_MISMATCH_CONF_RESP) {
  3523. wpa_printf(MSG_INFO, "DPP: TESTING - E-nonce mismatch");
  3524. wpabuf_put_le16(clear, DPP_ATTR_ENROLLEE_NONCE);
  3525. wpabuf_put_le16(clear, e_nonce_len);
  3526. wpabuf_put_data(clear, e_nonce, e_nonce_len - 1);
  3527. wpabuf_put_u8(clear, e_nonce[e_nonce_len - 1] ^ 0x01);
  3528. goto skip_e_nonce;
  3529. }
  3530. if (dpp_test == DPP_TEST_NO_WRAPPED_DATA_CONF_RESP) {
  3531. wpa_printf(MSG_INFO, "DPP: TESTING - no Wrapped Data");
  3532. goto skip_wrapped_data;
  3533. }
  3534. #endif /* CONFIG_TESTING_OPTIONS */
  3535. /* E-nonce */
  3536. wpabuf_put_le16(clear, DPP_ATTR_ENROLLEE_NONCE);
  3537. wpabuf_put_le16(clear, e_nonce_len);
  3538. wpabuf_put_data(clear, e_nonce, e_nonce_len);
  3539. #ifdef CONFIG_TESTING_OPTIONS
  3540. skip_e_nonce:
  3541. if (dpp_test == DPP_TEST_NO_CONFIG_OBJ_CONF_RESP) {
  3542. wpa_printf(MSG_INFO, "DPP: TESTING - Config Object");
  3543. goto skip_config_obj;
  3544. }
  3545. #endif /* CONFIG_TESTING_OPTIONS */
  3546. if (conf) {
  3547. wpabuf_put_le16(clear, DPP_ATTR_CONFIG_OBJ);
  3548. wpabuf_put_le16(clear, wpabuf_len(conf));
  3549. wpabuf_put_buf(clear, conf);
  3550. }
  3551. #ifdef CONFIG_TESTING_OPTIONS
  3552. skip_config_obj:
  3553. if (dpp_test == DPP_TEST_NO_STATUS_CONF_RESP) {
  3554. wpa_printf(MSG_INFO, "DPP: TESTING - Status");
  3555. goto skip_status;
  3556. }
  3557. if (dpp_test == DPP_TEST_INVALID_STATUS_CONF_RESP) {
  3558. wpa_printf(MSG_INFO, "DPP: TESTING - invalid Status");
  3559. status = 255;
  3560. }
  3561. #endif /* CONFIG_TESTING_OPTIONS */
  3562. /* DPP Status */
  3563. wpabuf_put_le16(msg, DPP_ATTR_STATUS);
  3564. wpabuf_put_le16(msg, 1);
  3565. wpabuf_put_u8(msg, status);
  3566. #ifdef CONFIG_TESTING_OPTIONS
  3567. skip_status:
  3568. #endif /* CONFIG_TESTING_OPTIONS */
  3569. addr[0] = wpabuf_head(msg);
  3570. len[0] = wpabuf_len(msg);
  3571. wpa_hexdump(MSG_DEBUG, "DDP: AES-SIV AD", addr[0], len[0]);
  3572. wpabuf_put_le16(msg, DPP_ATTR_WRAPPED_DATA);
  3573. wpabuf_put_le16(msg, wpabuf_len(clear) + AES_BLOCK_SIZE);
  3574. wrapped = wpabuf_put(msg, wpabuf_len(clear) + AES_BLOCK_SIZE);
  3575. wpa_hexdump_buf(MSG_DEBUG, "DPP: AES-SIV cleartext", clear);
  3576. if (aes_siv_encrypt(auth->ke, auth->curve->hash_len,
  3577. wpabuf_head(clear), wpabuf_len(clear),
  3578. 1, addr, len, wrapped) < 0)
  3579. goto fail;
  3580. wpa_hexdump(MSG_DEBUG, "DPP: AES-SIV ciphertext",
  3581. wrapped, wpabuf_len(clear) + AES_BLOCK_SIZE);
  3582. #ifdef CONFIG_TESTING_OPTIONS
  3583. if (dpp_test == DPP_TEST_AFTER_WRAPPED_DATA_CONF_RESP) {
  3584. wpa_printf(MSG_INFO, "DPP: TESTING - attr after Wrapped Data");
  3585. wpabuf_put_le16(msg, DPP_ATTR_TESTING);
  3586. wpabuf_put_le16(msg, 0);
  3587. }
  3588. skip_wrapped_data:
  3589. #endif /* CONFIG_TESTING_OPTIONS */
  3590. wpa_hexdump_buf(MSG_DEBUG,
  3591. "DPP: Configuration Response attributes", msg);
  3592. out:
  3593. wpabuf_free(conf);
  3594. wpabuf_free(clear);
  3595. return msg;
  3596. fail:
  3597. wpabuf_free(msg);
  3598. msg = NULL;
  3599. goto out;
  3600. }
  3601. struct wpabuf *
  3602. dpp_conf_req_rx(struct dpp_authentication *auth, const u8 *attr_start,
  3603. size_t attr_len)
  3604. {
  3605. const u8 *wrapped_data, *e_nonce, *config_attr;
  3606. u16 wrapped_data_len, e_nonce_len, config_attr_len;
  3607. u8 *unwrapped = NULL;
  3608. size_t unwrapped_len = 0;
  3609. struct wpabuf *resp = NULL;
  3610. struct json_token *root = NULL, *token;
  3611. int ap;
  3612. if (dpp_check_attrs(attr_start, attr_len) < 0) {
  3613. dpp_auth_fail(auth, "Invalid attribute in config request");
  3614. return NULL;
  3615. }
  3616. wrapped_data = dpp_get_attr(attr_start, attr_len, DPP_ATTR_WRAPPED_DATA,
  3617. &wrapped_data_len);
  3618. if (!wrapped_data || wrapped_data_len < AES_BLOCK_SIZE) {
  3619. dpp_auth_fail(auth,
  3620. "Missing or invalid required Wrapped Data attribute");
  3621. return NULL;
  3622. }
  3623. wpa_hexdump(MSG_DEBUG, "DPP: AES-SIV ciphertext",
  3624. wrapped_data, wrapped_data_len);
  3625. unwrapped_len = wrapped_data_len - AES_BLOCK_SIZE;
  3626. unwrapped = os_malloc(unwrapped_len);
  3627. if (!unwrapped)
  3628. return NULL;
  3629. if (aes_siv_decrypt(auth->ke, auth->curve->hash_len,
  3630. wrapped_data, wrapped_data_len,
  3631. 0, NULL, NULL, unwrapped) < 0) {
  3632. dpp_auth_fail(auth, "AES-SIV decryption failed");
  3633. goto fail;
  3634. }
  3635. wpa_hexdump(MSG_DEBUG, "DPP: AES-SIV cleartext",
  3636. unwrapped, unwrapped_len);
  3637. if (dpp_check_attrs(unwrapped, unwrapped_len) < 0) {
  3638. dpp_auth_fail(auth, "Invalid attribute in unwrapped data");
  3639. goto fail;
  3640. }
  3641. e_nonce = dpp_get_attr(unwrapped, unwrapped_len,
  3642. DPP_ATTR_ENROLLEE_NONCE,
  3643. &e_nonce_len);
  3644. if (!e_nonce || e_nonce_len != auth->curve->nonce_len) {
  3645. dpp_auth_fail(auth,
  3646. "Missing or invalid Enrollee Nonce attribute");
  3647. goto fail;
  3648. }
  3649. wpa_hexdump(MSG_DEBUG, "DPP: Enrollee Nonce", e_nonce, e_nonce_len);
  3650. config_attr = dpp_get_attr(unwrapped, unwrapped_len,
  3651. DPP_ATTR_CONFIG_ATTR_OBJ,
  3652. &config_attr_len);
  3653. if (!config_attr) {
  3654. dpp_auth_fail(auth,
  3655. "Missing or invalid Config Attributes attribute");
  3656. goto fail;
  3657. }
  3658. wpa_hexdump_ascii(MSG_DEBUG, "DPP: Config Attributes",
  3659. config_attr, config_attr_len);
  3660. root = json_parse((const char *) config_attr, config_attr_len);
  3661. if (!root) {
  3662. dpp_auth_fail(auth, "Could not parse Config Attributes");
  3663. goto fail;
  3664. }
  3665. token = json_get_member(root, "name");
  3666. if (!token || token->type != JSON_STRING) {
  3667. dpp_auth_fail(auth, "No Config Attributes - name");
  3668. goto fail;
  3669. }
  3670. wpa_printf(MSG_DEBUG, "DPP: Enrollee name = '%s'", token->string);
  3671. token = json_get_member(root, "wi-fi_tech");
  3672. if (!token || token->type != JSON_STRING) {
  3673. dpp_auth_fail(auth, "No Config Attributes - wi-fi_tech");
  3674. goto fail;
  3675. }
  3676. wpa_printf(MSG_DEBUG, "DPP: wi-fi_tech = '%s'", token->string);
  3677. if (os_strcmp(token->string, "infra") != 0) {
  3678. wpa_printf(MSG_DEBUG, "DPP: Unsupported wi-fi_tech '%s'",
  3679. token->string);
  3680. dpp_auth_fail(auth, "Unsupported wi-fi_tech");
  3681. goto fail;
  3682. }
  3683. token = json_get_member(root, "netRole");
  3684. if (!token || token->type != JSON_STRING) {
  3685. dpp_auth_fail(auth, "No Config Attributes - netRole");
  3686. goto fail;
  3687. }
  3688. wpa_printf(MSG_DEBUG, "DPP: netRole = '%s'", token->string);
  3689. if (os_strcmp(token->string, "sta") == 0) {
  3690. ap = 0;
  3691. } else if (os_strcmp(token->string, "ap") == 0) {
  3692. ap = 1;
  3693. } else {
  3694. wpa_printf(MSG_DEBUG, "DPP: Unsupported netRole '%s'",
  3695. token->string);
  3696. dpp_auth_fail(auth, "Unsupported netRole");
  3697. goto fail;
  3698. }
  3699. resp = dpp_build_conf_resp(auth, e_nonce, e_nonce_len, ap);
  3700. fail:
  3701. json_free(root);
  3702. os_free(unwrapped);
  3703. return resp;
  3704. }
  3705. static struct wpabuf *
  3706. dpp_parse_jws_prot_hdr(const struct dpp_curve_params *curve,
  3707. const u8 *prot_hdr, u16 prot_hdr_len,
  3708. const EVP_MD **ret_md)
  3709. {
  3710. struct json_token *root, *token;
  3711. struct wpabuf *kid = NULL;
  3712. root = json_parse((const char *) prot_hdr, prot_hdr_len);
  3713. if (!root) {
  3714. wpa_printf(MSG_DEBUG,
  3715. "DPP: JSON parsing failed for JWS Protected Header");
  3716. goto fail;
  3717. }
  3718. if (root->type != JSON_OBJECT) {
  3719. wpa_printf(MSG_DEBUG,
  3720. "DPP: JWS Protected Header root is not an object");
  3721. goto fail;
  3722. }
  3723. token = json_get_member(root, "typ");
  3724. if (!token || token->type != JSON_STRING) {
  3725. wpa_printf(MSG_DEBUG, "DPP: No typ string value found");
  3726. goto fail;
  3727. }
  3728. wpa_printf(MSG_DEBUG, "DPP: JWS Protected Header typ=%s",
  3729. token->string);
  3730. if (os_strcmp(token->string, "dppCon") != 0) {
  3731. wpa_printf(MSG_DEBUG,
  3732. "DPP: Unsupported JWS Protected Header typ=%s",
  3733. token->string);
  3734. goto fail;
  3735. }
  3736. token = json_get_member(root, "alg");
  3737. if (!token || token->type != JSON_STRING) {
  3738. wpa_printf(MSG_DEBUG, "DPP: No alg string value found");
  3739. goto fail;
  3740. }
  3741. wpa_printf(MSG_DEBUG, "DPP: JWS Protected Header alg=%s",
  3742. token->string);
  3743. if (os_strcmp(token->string, curve->jws_alg) != 0) {
  3744. wpa_printf(MSG_DEBUG,
  3745. "DPP: Unexpected JWS Protected Header alg=%s (expected %s based on C-sign-key)",
  3746. token->string, curve->jws_alg);
  3747. goto fail;
  3748. }
  3749. if (os_strcmp(token->string, "ES256") == 0 ||
  3750. os_strcmp(token->string, "BS256") == 0)
  3751. *ret_md = EVP_sha256();
  3752. else if (os_strcmp(token->string, "ES384") == 0 ||
  3753. os_strcmp(token->string, "BS384") == 0)
  3754. *ret_md = EVP_sha384();
  3755. else if (os_strcmp(token->string, "ES512") == 0 ||
  3756. os_strcmp(token->string, "BS512") == 0)
  3757. *ret_md = EVP_sha512();
  3758. else
  3759. *ret_md = NULL;
  3760. if (!*ret_md) {
  3761. wpa_printf(MSG_DEBUG,
  3762. "DPP: Unsupported JWS Protected Header alg=%s",
  3763. token->string);
  3764. goto fail;
  3765. }
  3766. kid = json_get_member_base64url(root, "kid");
  3767. if (!kid) {
  3768. wpa_printf(MSG_DEBUG, "DPP: No kid string value found");
  3769. goto fail;
  3770. }
  3771. wpa_hexdump_buf(MSG_DEBUG, "DPP: JWS Protected Header kid (decoded)",
  3772. kid);
  3773. fail:
  3774. json_free(root);
  3775. return kid;
  3776. }
  3777. static int dpp_parse_cred_legacy(struct dpp_authentication *auth,
  3778. struct json_token *cred)
  3779. {
  3780. struct json_token *pass, *psk_hex;
  3781. wpa_printf(MSG_DEBUG, "DPP: Legacy akm=psk credential");
  3782. pass = json_get_member(cred, "pass");
  3783. psk_hex = json_get_member(cred, "psk_hex");
  3784. if (pass && pass->type == JSON_STRING) {
  3785. size_t len = os_strlen(pass->string);
  3786. wpa_hexdump_ascii_key(MSG_DEBUG, "DPP: Legacy passphrase",
  3787. pass->string, len);
  3788. if (len < 8 || len > 63)
  3789. return -1;
  3790. os_strlcpy(auth->passphrase, pass->string,
  3791. sizeof(auth->passphrase));
  3792. } else if (psk_hex && psk_hex->type == JSON_STRING) {
  3793. if (os_strlen(psk_hex->string) != PMK_LEN * 2 ||
  3794. hexstr2bin(psk_hex->string, auth->psk, PMK_LEN) < 0) {
  3795. wpa_printf(MSG_DEBUG, "DPP: Invalid psk_hex encoding");
  3796. return -1;
  3797. }
  3798. wpa_hexdump_key(MSG_DEBUG, "DPP: Legacy PSK",
  3799. auth->psk, PMK_LEN);
  3800. auth->psk_set = 1;
  3801. } else {
  3802. wpa_printf(MSG_DEBUG, "DPP: No pass or psk_hex strings found");
  3803. return -1;
  3804. }
  3805. return 0;
  3806. }
  3807. static EVP_PKEY * dpp_parse_jwk(struct json_token *jwk,
  3808. const struct dpp_curve_params **key_curve)
  3809. {
  3810. struct json_token *token;
  3811. const struct dpp_curve_params *curve;
  3812. struct wpabuf *x = NULL, *y = NULL;
  3813. EC_GROUP *group;
  3814. EVP_PKEY *pkey = NULL;
  3815. token = json_get_member(jwk, "kty");
  3816. if (!token || token->type != JSON_STRING) {
  3817. wpa_printf(MSG_DEBUG, "DPP: No kty in JWK");
  3818. goto fail;
  3819. }
  3820. if (os_strcmp(token->string, "EC") != 0) {
  3821. wpa_printf(MSG_DEBUG, "DPP: Unexpected JWK kty '%s",
  3822. token->string);
  3823. goto fail;
  3824. }
  3825. token = json_get_member(jwk, "crv");
  3826. if (!token || token->type != JSON_STRING) {
  3827. wpa_printf(MSG_DEBUG, "DPP: No crv in JWK");
  3828. goto fail;
  3829. }
  3830. curve = dpp_get_curve_jwk_crv(token->string);
  3831. if (!curve) {
  3832. wpa_printf(MSG_DEBUG, "DPP: Unsupported JWK crv '%s'",
  3833. token->string);
  3834. goto fail;
  3835. }
  3836. x = json_get_member_base64url(jwk, "x");
  3837. if (!x) {
  3838. wpa_printf(MSG_DEBUG, "DPP: No x in JWK");
  3839. goto fail;
  3840. }
  3841. wpa_hexdump_buf(MSG_DEBUG, "DPP: JWK x", x);
  3842. if (wpabuf_len(x) != curve->prime_len) {
  3843. wpa_printf(MSG_DEBUG,
  3844. "DPP: Unexpected JWK x length %u (expected %u for curve %s)",
  3845. (unsigned int) wpabuf_len(x),
  3846. (unsigned int) curve->prime_len, curve->name);
  3847. goto fail;
  3848. }
  3849. y = json_get_member_base64url(jwk, "y");
  3850. if (!y) {
  3851. wpa_printf(MSG_DEBUG, "DPP: No y in JWK");
  3852. goto fail;
  3853. }
  3854. wpa_hexdump_buf(MSG_DEBUG, "DPP: JWK y", y);
  3855. if (wpabuf_len(y) != curve->prime_len) {
  3856. wpa_printf(MSG_DEBUG,
  3857. "DPP: Unexpected JWK y length %u (expected %u for curve %s)",
  3858. (unsigned int) wpabuf_len(y),
  3859. (unsigned int) curve->prime_len, curve->name);
  3860. goto fail;
  3861. }
  3862. group = EC_GROUP_new_by_curve_name(OBJ_txt2nid(curve->name));
  3863. if (!group) {
  3864. wpa_printf(MSG_DEBUG, "DPP: Could not prepare group for JWK");
  3865. goto fail;
  3866. }
  3867. pkey = dpp_set_pubkey_point_group(group, wpabuf_head(x), wpabuf_head(y),
  3868. wpabuf_len(x));
  3869. *key_curve = curve;
  3870. fail:
  3871. wpabuf_free(x);
  3872. wpabuf_free(y);
  3873. return pkey;
  3874. }
  3875. int dpp_key_expired(const char *timestamp, os_time_t *expiry)
  3876. {
  3877. struct os_time now;
  3878. unsigned int year, month, day, hour, min, sec;
  3879. os_time_t utime;
  3880. const char *pos;
  3881. /* ISO 8601 date and time:
  3882. * <date>T<time>
  3883. * YYYY-MM-DDTHH:MM:SSZ
  3884. * YYYY-MM-DDTHH:MM:SS+03:00
  3885. */
  3886. if (os_strlen(timestamp) < 19) {
  3887. wpa_printf(MSG_DEBUG,
  3888. "DPP: Too short timestamp - assume expired key");
  3889. return 1;
  3890. }
  3891. if (sscanf(timestamp, "%04u-%02u-%02uT%02u:%02u:%02u",
  3892. &year, &month, &day, &hour, &min, &sec) != 6) {
  3893. wpa_printf(MSG_DEBUG,
  3894. "DPP: Failed to parse expiration day - assume expired key");
  3895. return 1;
  3896. }
  3897. if (os_mktime(year, month, day, hour, min, sec, &utime) < 0) {
  3898. wpa_printf(MSG_DEBUG,
  3899. "DPP: Invalid date/time information - assume expired key");
  3900. return 1;
  3901. }
  3902. pos = timestamp + 19;
  3903. if (*pos == 'Z' || *pos == '\0') {
  3904. /* In UTC - no need to adjust */
  3905. } else if (*pos == '-' || *pos == '+') {
  3906. int items;
  3907. /* Adjust local time to UTC */
  3908. items = sscanf(pos + 1, "%02u:%02u", &hour, &min);
  3909. if (items < 1) {
  3910. wpa_printf(MSG_DEBUG,
  3911. "DPP: Invalid time zone designator (%s) - assume expired key",
  3912. pos);
  3913. return 1;
  3914. }
  3915. if (*pos == '-')
  3916. utime += 3600 * hour;
  3917. if (*pos == '+')
  3918. utime -= 3600 * hour;
  3919. if (items > 1) {
  3920. if (*pos == '-')
  3921. utime += 60 * min;
  3922. if (*pos == '+')
  3923. utime -= 60 * min;
  3924. }
  3925. } else {
  3926. wpa_printf(MSG_DEBUG,
  3927. "DPP: Invalid time zone designator (%s) - assume expired key",
  3928. pos);
  3929. return 1;
  3930. }
  3931. if (expiry)
  3932. *expiry = utime;
  3933. if (os_get_time(&now) < 0) {
  3934. wpa_printf(MSG_DEBUG,
  3935. "DPP: Cannot get current time - assume expired key");
  3936. return 1;
  3937. }
  3938. if (now.sec > utime) {
  3939. wpa_printf(MSG_DEBUG, "DPP: Key has expired (%lu < %lu)",
  3940. utime, now.sec);
  3941. return 1;
  3942. }
  3943. return 0;
  3944. }
  3945. static int dpp_parse_connector(struct dpp_authentication *auth,
  3946. const unsigned char *payload,
  3947. u16 payload_len)
  3948. {
  3949. struct json_token *root, *groups, *netkey, *token;
  3950. int ret = -1;
  3951. EVP_PKEY *key = NULL;
  3952. const struct dpp_curve_params *curve;
  3953. unsigned int rules = 0;
  3954. root = json_parse((const char *) payload, payload_len);
  3955. if (!root) {
  3956. wpa_printf(MSG_DEBUG, "DPP: JSON parsing of connector failed");
  3957. goto fail;
  3958. }
  3959. groups = json_get_member(root, "groups");
  3960. if (!groups || groups->type != JSON_ARRAY) {
  3961. wpa_printf(MSG_DEBUG, "DPP: No groups array found");
  3962. goto skip_groups;
  3963. }
  3964. for (token = groups->child; token; token = token->sibling) {
  3965. struct json_token *id, *role;
  3966. id = json_get_member(token, "groupId");
  3967. if (!id || id->type != JSON_STRING) {
  3968. wpa_printf(MSG_DEBUG, "DPP: Missing groupId string");
  3969. goto fail;
  3970. }
  3971. role = json_get_member(token, "netRole");
  3972. if (!role || role->type != JSON_STRING) {
  3973. wpa_printf(MSG_DEBUG, "DPP: Missing netRole string");
  3974. goto fail;
  3975. }
  3976. wpa_printf(MSG_DEBUG,
  3977. "DPP: connector group: groupId='%s' netRole='%s'",
  3978. id->string, role->string);
  3979. rules++;
  3980. }
  3981. skip_groups:
  3982. if (!rules) {
  3983. wpa_printf(MSG_DEBUG,
  3984. "DPP: Connector includes no groups");
  3985. goto fail;
  3986. }
  3987. token = json_get_member(root, "expiry");
  3988. if (!token || token->type != JSON_STRING) {
  3989. wpa_printf(MSG_DEBUG,
  3990. "DPP: No expiry string found - connector does not expire");
  3991. } else {
  3992. wpa_printf(MSG_DEBUG, "DPP: expiry = %s", token->string);
  3993. if (dpp_key_expired(token->string,
  3994. &auth->net_access_key_expiry)) {
  3995. wpa_printf(MSG_DEBUG,
  3996. "DPP: Connector (netAccessKey) has expired");
  3997. goto fail;
  3998. }
  3999. }
  4000. netkey = json_get_member(root, "netAccessKey");
  4001. if (!netkey || netkey->type != JSON_OBJECT) {
  4002. wpa_printf(MSG_DEBUG, "DPP: No netAccessKey object found");
  4003. goto fail;
  4004. }
  4005. key = dpp_parse_jwk(netkey, &curve);
  4006. if (!key)
  4007. goto fail;
  4008. dpp_debug_print_key("DPP: Received netAccessKey", key);
  4009. if (EVP_PKEY_cmp(key, auth->own_protocol_key) != 1) {
  4010. wpa_printf(MSG_DEBUG,
  4011. "DPP: netAccessKey in connector does not match own protocol key");
  4012. #ifdef CONFIG_TESTING_OPTIONS
  4013. if (auth->ignore_netaccesskey_mismatch) {
  4014. wpa_printf(MSG_DEBUG,
  4015. "DPP: TESTING - skip netAccessKey mismatch");
  4016. } else {
  4017. goto fail;
  4018. }
  4019. #else /* CONFIG_TESTING_OPTIONS */
  4020. goto fail;
  4021. #endif /* CONFIG_TESTING_OPTIONS */
  4022. }
  4023. ret = 0;
  4024. fail:
  4025. EVP_PKEY_free(key);
  4026. json_free(root);
  4027. return ret;
  4028. }
  4029. static int dpp_check_pubkey_match(EVP_PKEY *pub, struct wpabuf *r_hash)
  4030. {
  4031. struct wpabuf *uncomp;
  4032. int res;
  4033. u8 hash[SHA256_MAC_LEN];
  4034. const u8 *addr[1];
  4035. size_t len[1];
  4036. if (wpabuf_len(r_hash) != SHA256_MAC_LEN)
  4037. return -1;
  4038. uncomp = dpp_get_pubkey_point(pub, 1);
  4039. if (!uncomp)
  4040. return -1;
  4041. addr[0] = wpabuf_head(uncomp);
  4042. len[0] = wpabuf_len(uncomp);
  4043. wpa_hexdump(MSG_DEBUG, "DPP: Uncompressed public key",
  4044. addr[0], len[0]);
  4045. res = sha256_vector(1, addr, len, hash);
  4046. wpabuf_free(uncomp);
  4047. if (res < 0)
  4048. return -1;
  4049. if (os_memcmp(hash, wpabuf_head(r_hash), SHA256_MAC_LEN) != 0) {
  4050. wpa_printf(MSG_DEBUG,
  4051. "DPP: Received hash value does not match calculated public key hash value");
  4052. wpa_hexdump(MSG_DEBUG, "DPP: Calculated hash",
  4053. hash, SHA256_MAC_LEN);
  4054. return -1;
  4055. }
  4056. return 0;
  4057. }
  4058. static void dpp_copy_csign(struct dpp_authentication *auth, EVP_PKEY *csign)
  4059. {
  4060. unsigned char *der = NULL;
  4061. int der_len;
  4062. der_len = i2d_PUBKEY(csign, &der);
  4063. if (der_len <= 0)
  4064. return;
  4065. wpabuf_free(auth->c_sign_key);
  4066. auth->c_sign_key = wpabuf_alloc_copy(der, der_len);
  4067. OPENSSL_free(der);
  4068. }
  4069. static void dpp_copy_netaccesskey(struct dpp_authentication *auth)
  4070. {
  4071. unsigned char *der = NULL;
  4072. int der_len;
  4073. EC_KEY *eckey;
  4074. eckey = EVP_PKEY_get1_EC_KEY(auth->own_protocol_key);
  4075. if (!eckey)
  4076. return;
  4077. der_len = i2d_ECPrivateKey(eckey, &der);
  4078. if (der_len <= 0) {
  4079. EC_KEY_free(eckey);
  4080. return;
  4081. }
  4082. wpabuf_free(auth->net_access_key);
  4083. auth->net_access_key = wpabuf_alloc_copy(der, der_len);
  4084. OPENSSL_free(der);
  4085. EC_KEY_free(eckey);
  4086. }
  4087. struct dpp_signed_connector_info {
  4088. unsigned char *payload;
  4089. size_t payload_len;
  4090. };
  4091. static enum dpp_status_error
  4092. dpp_process_signed_connector(struct dpp_signed_connector_info *info,
  4093. EVP_PKEY *csign_pub, const char *connector)
  4094. {
  4095. enum dpp_status_error ret = 255;
  4096. const char *pos, *end, *signed_start, *signed_end;
  4097. struct wpabuf *kid = NULL;
  4098. unsigned char *prot_hdr = NULL, *signature = NULL;
  4099. size_t prot_hdr_len = 0, signature_len = 0;
  4100. const EVP_MD *sign_md = NULL;
  4101. unsigned char *der = NULL;
  4102. int der_len;
  4103. int res;
  4104. EVP_MD_CTX *md_ctx = NULL;
  4105. ECDSA_SIG *sig = NULL;
  4106. BIGNUM *r = NULL, *s = NULL;
  4107. const struct dpp_curve_params *curve;
  4108. EC_KEY *eckey;
  4109. const EC_GROUP *group;
  4110. int nid;
  4111. eckey = EVP_PKEY_get1_EC_KEY(csign_pub);
  4112. if (!eckey)
  4113. goto fail;
  4114. group = EC_KEY_get0_group(eckey);
  4115. if (!group)
  4116. goto fail;
  4117. nid = EC_GROUP_get_curve_name(group);
  4118. curve = dpp_get_curve_nid(nid);
  4119. if (!curve)
  4120. goto fail;
  4121. wpa_printf(MSG_DEBUG, "DPP: C-sign-key group: %s", curve->jwk_crv);
  4122. os_memset(info, 0, sizeof(*info));
  4123. signed_start = pos = connector;
  4124. end = os_strchr(pos, '.');
  4125. if (!end) {
  4126. wpa_printf(MSG_DEBUG, "DPP: Missing dot(1) in signedConnector");
  4127. ret = DPP_STATUS_INVALID_CONNECTOR;
  4128. goto fail;
  4129. }
  4130. prot_hdr = base64_url_decode((const unsigned char *) pos,
  4131. end - pos, &prot_hdr_len);
  4132. if (!prot_hdr) {
  4133. wpa_printf(MSG_DEBUG,
  4134. "DPP: Failed to base64url decode signedConnector JWS Protected Header");
  4135. ret = DPP_STATUS_INVALID_CONNECTOR;
  4136. goto fail;
  4137. }
  4138. wpa_hexdump_ascii(MSG_DEBUG,
  4139. "DPP: signedConnector - JWS Protected Header",
  4140. prot_hdr, prot_hdr_len);
  4141. kid = dpp_parse_jws_prot_hdr(curve, prot_hdr, prot_hdr_len, &sign_md);
  4142. if (!kid) {
  4143. ret = DPP_STATUS_INVALID_CONNECTOR;
  4144. goto fail;
  4145. }
  4146. if (wpabuf_len(kid) != SHA256_MAC_LEN) {
  4147. wpa_printf(MSG_DEBUG,
  4148. "DPP: Unexpected signedConnector JWS Protected Header kid length: %u (expected %u)",
  4149. (unsigned int) wpabuf_len(kid), SHA256_MAC_LEN);
  4150. ret = DPP_STATUS_INVALID_CONNECTOR;
  4151. goto fail;
  4152. }
  4153. pos = end + 1;
  4154. end = os_strchr(pos, '.');
  4155. if (!end) {
  4156. wpa_printf(MSG_DEBUG,
  4157. "DPP: Missing dot(2) in signedConnector");
  4158. ret = DPP_STATUS_INVALID_CONNECTOR;
  4159. goto fail;
  4160. }
  4161. signed_end = end - 1;
  4162. info->payload = base64_url_decode((const unsigned char *) pos,
  4163. end - pos, &info->payload_len);
  4164. if (!info->payload) {
  4165. wpa_printf(MSG_DEBUG,
  4166. "DPP: Failed to base64url decode signedConnector JWS Payload");
  4167. ret = DPP_STATUS_INVALID_CONNECTOR;
  4168. goto fail;
  4169. }
  4170. wpa_hexdump_ascii(MSG_DEBUG,
  4171. "DPP: signedConnector - JWS Payload",
  4172. info->payload, info->payload_len);
  4173. pos = end + 1;
  4174. signature = base64_url_decode((const unsigned char *) pos,
  4175. os_strlen(pos), &signature_len);
  4176. if (!signature) {
  4177. wpa_printf(MSG_DEBUG,
  4178. "DPP: Failed to base64url decode signedConnector signature");
  4179. ret = DPP_STATUS_INVALID_CONNECTOR;
  4180. goto fail;
  4181. }
  4182. wpa_hexdump(MSG_DEBUG, "DPP: signedConnector - signature",
  4183. signature, signature_len);
  4184. if (dpp_check_pubkey_match(csign_pub, kid) < 0) {
  4185. ret = DPP_STATUS_NO_MATCH;
  4186. goto fail;
  4187. }
  4188. if (signature_len & 0x01) {
  4189. wpa_printf(MSG_DEBUG,
  4190. "DPP: Unexpected signedConnector signature length (%d)",
  4191. (int) signature_len);
  4192. ret = DPP_STATUS_INVALID_CONNECTOR;
  4193. goto fail;
  4194. }
  4195. /* JWS Signature encodes the signature (r,s) as two octet strings. Need
  4196. * to convert that to DER encoded ECDSA_SIG for OpenSSL EVP routines. */
  4197. r = BN_bin2bn(signature, signature_len / 2, NULL);
  4198. s = BN_bin2bn(signature + signature_len / 2, signature_len / 2, NULL);
  4199. sig = ECDSA_SIG_new();
  4200. if (!r || !s || !sig || ECDSA_SIG_set0(sig, r, s) != 1)
  4201. goto fail;
  4202. r = NULL;
  4203. s = NULL;
  4204. der_len = i2d_ECDSA_SIG(sig, &der);
  4205. if (der_len <= 0) {
  4206. wpa_printf(MSG_DEBUG, "DPP: Could not DER encode signature");
  4207. goto fail;
  4208. }
  4209. wpa_hexdump(MSG_DEBUG, "DPP: DER encoded signature", der, der_len);
  4210. md_ctx = EVP_MD_CTX_create();
  4211. if (!md_ctx)
  4212. goto fail;
  4213. ERR_clear_error();
  4214. if (EVP_DigestVerifyInit(md_ctx, NULL, sign_md, NULL, csign_pub) != 1) {
  4215. wpa_printf(MSG_DEBUG, "DPP: EVP_DigestVerifyInit failed: %s",
  4216. ERR_error_string(ERR_get_error(), NULL));
  4217. goto fail;
  4218. }
  4219. if (EVP_DigestVerifyUpdate(md_ctx, signed_start,
  4220. signed_end - signed_start + 1) != 1) {
  4221. wpa_printf(MSG_DEBUG, "DPP: EVP_DigestVerifyUpdate failed: %s",
  4222. ERR_error_string(ERR_get_error(), NULL));
  4223. goto fail;
  4224. }
  4225. res = EVP_DigestVerifyFinal(md_ctx, der, der_len);
  4226. if (res != 1) {
  4227. wpa_printf(MSG_DEBUG,
  4228. "DPP: EVP_DigestVerifyFinal failed (res=%d): %s",
  4229. res, ERR_error_string(ERR_get_error(), NULL));
  4230. ret = DPP_STATUS_INVALID_CONNECTOR;
  4231. goto fail;
  4232. }
  4233. ret = DPP_STATUS_OK;
  4234. fail:
  4235. EC_KEY_free(eckey);
  4236. EVP_MD_CTX_destroy(md_ctx);
  4237. os_free(prot_hdr);
  4238. wpabuf_free(kid);
  4239. os_free(signature);
  4240. ECDSA_SIG_free(sig);
  4241. BN_free(r);
  4242. BN_free(s);
  4243. OPENSSL_free(der);
  4244. return ret;
  4245. }
  4246. static int dpp_parse_cred_dpp(struct dpp_authentication *auth,
  4247. struct json_token *cred)
  4248. {
  4249. struct dpp_signed_connector_info info;
  4250. struct json_token *token, *csign;
  4251. int ret = -1;
  4252. EVP_PKEY *csign_pub = NULL;
  4253. const struct dpp_curve_params *key_curve = NULL;
  4254. const char *signed_connector;
  4255. os_memset(&info, 0, sizeof(info));
  4256. wpa_printf(MSG_DEBUG, "DPP: Connector credential");
  4257. csign = json_get_member(cred, "csign");
  4258. if (!csign || csign->type != JSON_OBJECT) {
  4259. wpa_printf(MSG_DEBUG, "DPP: No csign JWK in JSON");
  4260. goto fail;
  4261. }
  4262. csign_pub = dpp_parse_jwk(csign, &key_curve);
  4263. if (!csign_pub) {
  4264. wpa_printf(MSG_DEBUG, "DPP: Failed to parse csign JWK");
  4265. goto fail;
  4266. }
  4267. dpp_debug_print_key("DPP: Received C-sign-key", csign_pub);
  4268. token = json_get_member(cred, "signedConnector");
  4269. if (!token || token->type != JSON_STRING) {
  4270. wpa_printf(MSG_DEBUG, "DPP: No signedConnector string found");
  4271. goto fail;
  4272. }
  4273. wpa_hexdump_ascii(MSG_DEBUG, "DPP: signedConnector",
  4274. token->string, os_strlen(token->string));
  4275. signed_connector = token->string;
  4276. if (os_strchr(signed_connector, '"') ||
  4277. os_strchr(signed_connector, '\n')) {
  4278. wpa_printf(MSG_DEBUG,
  4279. "DPP: Unexpected character in signedConnector");
  4280. goto fail;
  4281. }
  4282. if (dpp_process_signed_connector(&info, csign_pub,
  4283. signed_connector) != DPP_STATUS_OK)
  4284. goto fail;
  4285. if (dpp_parse_connector(auth, info.payload, info.payload_len) < 0) {
  4286. wpa_printf(MSG_DEBUG, "DPP: Failed to parse connector");
  4287. goto fail;
  4288. }
  4289. os_free(auth->connector);
  4290. auth->connector = os_strdup(signed_connector);
  4291. dpp_copy_csign(auth, csign_pub);
  4292. dpp_copy_netaccesskey(auth);
  4293. ret = 0;
  4294. fail:
  4295. EVP_PKEY_free(csign_pub);
  4296. os_free(info.payload);
  4297. return ret;
  4298. }
  4299. static int dpp_parse_conf_obj(struct dpp_authentication *auth,
  4300. const u8 *conf_obj, u16 conf_obj_len)
  4301. {
  4302. int ret = -1;
  4303. struct json_token *root, *token, *discovery, *cred;
  4304. root = json_parse((const char *) conf_obj, conf_obj_len);
  4305. if (!root)
  4306. return -1;
  4307. if (root->type != JSON_OBJECT) {
  4308. dpp_auth_fail(auth, "JSON root is not an object");
  4309. goto fail;
  4310. }
  4311. token = json_get_member(root, "wi-fi_tech");
  4312. if (!token || token->type != JSON_STRING) {
  4313. dpp_auth_fail(auth, "No wi-fi_tech string value found");
  4314. goto fail;
  4315. }
  4316. if (os_strcmp(token->string, "infra") != 0) {
  4317. wpa_printf(MSG_DEBUG, "DPP: Unsupported wi-fi_tech value: '%s'",
  4318. token->string);
  4319. dpp_auth_fail(auth, "Unsupported wi-fi_tech value");
  4320. goto fail;
  4321. }
  4322. discovery = json_get_member(root, "discovery");
  4323. if (!discovery || discovery->type != JSON_OBJECT) {
  4324. dpp_auth_fail(auth, "No discovery object in JSON");
  4325. goto fail;
  4326. }
  4327. token = json_get_member(discovery, "ssid");
  4328. if (!token || token->type != JSON_STRING) {
  4329. dpp_auth_fail(auth, "No discovery::ssid string value found");
  4330. goto fail;
  4331. }
  4332. wpa_hexdump_ascii(MSG_DEBUG, "DPP: discovery::ssid",
  4333. token->string, os_strlen(token->string));
  4334. if (os_strlen(token->string) > SSID_MAX_LEN) {
  4335. dpp_auth_fail(auth, "Too long discovery::ssid string value");
  4336. goto fail;
  4337. }
  4338. auth->ssid_len = os_strlen(token->string);
  4339. os_memcpy(auth->ssid, token->string, auth->ssid_len);
  4340. cred = json_get_member(root, "cred");
  4341. if (!cred || cred->type != JSON_OBJECT) {
  4342. dpp_auth_fail(auth, "No cred object in JSON");
  4343. goto fail;
  4344. }
  4345. token = json_get_member(cred, "akm");
  4346. if (!token || token->type != JSON_STRING) {
  4347. dpp_auth_fail(auth, "No cred::akm string value found");
  4348. goto fail;
  4349. }
  4350. if (os_strcmp(token->string, "psk") == 0) {
  4351. if (dpp_parse_cred_legacy(auth, cred) < 0)
  4352. goto fail;
  4353. } else if (os_strcmp(token->string, "dpp") == 0) {
  4354. if (dpp_parse_cred_dpp(auth, cred) < 0)
  4355. goto fail;
  4356. } else {
  4357. wpa_printf(MSG_DEBUG, "DPP: Unsupported akm: %s",
  4358. token->string);
  4359. dpp_auth_fail(auth, "Unsupported akm");
  4360. goto fail;
  4361. }
  4362. wpa_printf(MSG_DEBUG, "DPP: JSON parsing completed successfully");
  4363. ret = 0;
  4364. fail:
  4365. json_free(root);
  4366. return ret;
  4367. }
  4368. int dpp_conf_resp_rx(struct dpp_authentication *auth,
  4369. const struct wpabuf *resp)
  4370. {
  4371. const u8 *wrapped_data, *e_nonce, *status, *conf_obj;
  4372. u16 wrapped_data_len, e_nonce_len, status_len, conf_obj_len;
  4373. const u8 *addr[1];
  4374. size_t len[1];
  4375. u8 *unwrapped = NULL;
  4376. size_t unwrapped_len = 0;
  4377. int ret = -1;
  4378. if (dpp_check_attrs(wpabuf_head(resp), wpabuf_len(resp)) < 0) {
  4379. dpp_auth_fail(auth, "Invalid attribute in config response");
  4380. return -1;
  4381. }
  4382. wrapped_data = dpp_get_attr(wpabuf_head(resp), wpabuf_len(resp),
  4383. DPP_ATTR_WRAPPED_DATA,
  4384. &wrapped_data_len);
  4385. if (!wrapped_data || wrapped_data_len < AES_BLOCK_SIZE) {
  4386. dpp_auth_fail(auth,
  4387. "Missing or invalid required Wrapped Data attribute");
  4388. return -1;
  4389. }
  4390. wpa_hexdump(MSG_DEBUG, "DPP: AES-SIV ciphertext",
  4391. wrapped_data, wrapped_data_len);
  4392. unwrapped_len = wrapped_data_len - AES_BLOCK_SIZE;
  4393. unwrapped = os_malloc(unwrapped_len);
  4394. if (!unwrapped)
  4395. return -1;
  4396. addr[0] = wpabuf_head(resp);
  4397. len[0] = wrapped_data - 4 - (const u8 *) wpabuf_head(resp);
  4398. wpa_hexdump(MSG_DEBUG, "DDP: AES-SIV AD", addr[0], len[0]);
  4399. if (aes_siv_decrypt(auth->ke, auth->curve->hash_len,
  4400. wrapped_data, wrapped_data_len,
  4401. 1, addr, len, unwrapped) < 0) {
  4402. dpp_auth_fail(auth, "AES-SIV decryption failed");
  4403. goto fail;
  4404. }
  4405. wpa_hexdump(MSG_DEBUG, "DPP: AES-SIV cleartext",
  4406. unwrapped, unwrapped_len);
  4407. if (dpp_check_attrs(unwrapped, unwrapped_len) < 0) {
  4408. dpp_auth_fail(auth, "Invalid attribute in unwrapped data");
  4409. goto fail;
  4410. }
  4411. e_nonce = dpp_get_attr(unwrapped, unwrapped_len,
  4412. DPP_ATTR_ENROLLEE_NONCE,
  4413. &e_nonce_len);
  4414. if (!e_nonce || e_nonce_len != auth->curve->nonce_len) {
  4415. dpp_auth_fail(auth,
  4416. "Missing or invalid Enrollee Nonce attribute");
  4417. goto fail;
  4418. }
  4419. wpa_hexdump(MSG_DEBUG, "DPP: Enrollee Nonce", e_nonce, e_nonce_len);
  4420. if (os_memcmp(e_nonce, auth->e_nonce, e_nonce_len) != 0) {
  4421. dpp_auth_fail(auth, "Enrollee Nonce mismatch");
  4422. goto fail;
  4423. }
  4424. status = dpp_get_attr(wpabuf_head(resp), wpabuf_len(resp),
  4425. DPP_ATTR_STATUS, &status_len);
  4426. if (!status || status_len < 1) {
  4427. dpp_auth_fail(auth,
  4428. "Missing or invalid required DPP Status attribute");
  4429. goto fail;
  4430. }
  4431. wpa_printf(MSG_DEBUG, "DPP: Status %u", status[0]);
  4432. if (status[0] != DPP_STATUS_OK) {
  4433. dpp_auth_fail(auth, "Configurator rejected configuration");
  4434. goto fail;
  4435. }
  4436. conf_obj = dpp_get_attr(unwrapped, unwrapped_len,
  4437. DPP_ATTR_CONFIG_OBJ, &conf_obj_len);
  4438. if (!conf_obj) {
  4439. dpp_auth_fail(auth,
  4440. "Missing required Configuration Object attribute");
  4441. goto fail;
  4442. }
  4443. wpa_hexdump_ascii(MSG_DEBUG, "DPP: configurationObject JSON",
  4444. conf_obj, conf_obj_len);
  4445. if (dpp_parse_conf_obj(auth, conf_obj, conf_obj_len) < 0)
  4446. goto fail;
  4447. ret = 0;
  4448. fail:
  4449. os_free(unwrapped);
  4450. return ret;
  4451. }
  4452. void dpp_configurator_free(struct dpp_configurator *conf)
  4453. {
  4454. if (!conf)
  4455. return;
  4456. EVP_PKEY_free(conf->csign);
  4457. os_free(conf->kid);
  4458. os_free(conf);
  4459. }
  4460. struct dpp_configurator *
  4461. dpp_keygen_configurator(const char *curve, const u8 *privkey,
  4462. size_t privkey_len)
  4463. {
  4464. struct dpp_configurator *conf;
  4465. struct wpabuf *csign_pub = NULL;
  4466. u8 kid_hash[SHA256_MAC_LEN];
  4467. const u8 *addr[1];
  4468. size_t len[1];
  4469. conf = os_zalloc(sizeof(*conf));
  4470. if (!conf)
  4471. return NULL;
  4472. if (!curve) {
  4473. conf->curve = &dpp_curves[0];
  4474. } else {
  4475. conf->curve = dpp_get_curve_name(curve);
  4476. if (!conf->curve) {
  4477. wpa_printf(MSG_INFO, "DPP: Unsupported curve: %s",
  4478. curve);
  4479. return NULL;
  4480. }
  4481. }
  4482. if (privkey)
  4483. conf->csign = dpp_set_keypair(&conf->curve, privkey,
  4484. privkey_len);
  4485. else
  4486. conf->csign = dpp_gen_keypair(conf->curve);
  4487. if (!conf->csign)
  4488. goto fail;
  4489. conf->own = 1;
  4490. csign_pub = dpp_get_pubkey_point(conf->csign, 1);
  4491. if (!csign_pub) {
  4492. wpa_printf(MSG_INFO, "DPP: Failed to extract C-sign-key");
  4493. goto fail;
  4494. }
  4495. /* kid = SHA256(ANSI X9.63 uncompressed C-sign-key) */
  4496. addr[0] = wpabuf_head(csign_pub);
  4497. len[0] = wpabuf_len(csign_pub);
  4498. if (sha256_vector(1, addr, len, kid_hash) < 0) {
  4499. wpa_printf(MSG_DEBUG,
  4500. "DPP: Failed to derive kid for C-sign-key");
  4501. goto fail;
  4502. }
  4503. conf->kid = (char *) base64_url_encode(kid_hash, sizeof(kid_hash),
  4504. NULL, 0);
  4505. if (!conf->kid)
  4506. goto fail;
  4507. out:
  4508. wpabuf_free(csign_pub);
  4509. return conf;
  4510. fail:
  4511. dpp_configurator_free(conf);
  4512. conf = NULL;
  4513. goto out;
  4514. }
  4515. int dpp_configurator_own_config(struct dpp_authentication *auth,
  4516. const char *curve)
  4517. {
  4518. struct wpabuf *conf_obj;
  4519. int ret = -1;
  4520. if (!auth->conf) {
  4521. wpa_printf(MSG_DEBUG, "DPP: No configurator specified");
  4522. return -1;
  4523. }
  4524. if (!curve) {
  4525. auth->curve = &dpp_curves[0];
  4526. } else {
  4527. auth->curve = dpp_get_curve_name(curve);
  4528. if (!auth->curve) {
  4529. wpa_printf(MSG_INFO, "DPP: Unsupported curve: %s",
  4530. curve);
  4531. return -1;
  4532. }
  4533. }
  4534. wpa_printf(MSG_DEBUG,
  4535. "DPP: Building own configuration/connector with curve %s",
  4536. auth->curve->name);
  4537. auth->own_protocol_key = dpp_gen_keypair(auth->curve);
  4538. if (!auth->own_protocol_key)
  4539. return -1;
  4540. dpp_copy_netaccesskey(auth);
  4541. auth->peer_protocol_key = auth->own_protocol_key;
  4542. dpp_copy_csign(auth, auth->conf->csign);
  4543. conf_obj = dpp_build_conf_obj(auth, 0);
  4544. if (!conf_obj)
  4545. goto fail;
  4546. ret = dpp_parse_conf_obj(auth, wpabuf_head(conf_obj),
  4547. wpabuf_len(conf_obj));
  4548. fail:
  4549. wpabuf_free(conf_obj);
  4550. auth->peer_protocol_key = NULL;
  4551. return ret;
  4552. }
  4553. static int dpp_compatible_netrole(const char *role1, const char *role2)
  4554. {
  4555. return (os_strcmp(role1, "sta") == 0 && os_strcmp(role2, "ap") == 0) ||
  4556. (os_strcmp(role1, "ap") == 0 && os_strcmp(role2, "sta") == 0);
  4557. }
  4558. static int dpp_connector_compatible_group(struct json_token *root,
  4559. const char *group_id,
  4560. const char *net_role)
  4561. {
  4562. struct json_token *groups, *token;
  4563. groups = json_get_member(root, "groups");
  4564. if (!groups || groups->type != JSON_ARRAY)
  4565. return 0;
  4566. for (token = groups->child; token; token = token->sibling) {
  4567. struct json_token *id, *role;
  4568. id = json_get_member(token, "groupId");
  4569. if (!id || id->type != JSON_STRING)
  4570. continue;
  4571. role = json_get_member(token, "netRole");
  4572. if (!role || role->type != JSON_STRING)
  4573. continue;
  4574. if (os_strcmp(id->string, "*") != 0 &&
  4575. os_strcmp(group_id, "*") != 0 &&
  4576. os_strcmp(id->string, group_id) != 0)
  4577. continue;
  4578. if (dpp_compatible_netrole(role->string, net_role))
  4579. return 1;
  4580. }
  4581. return 0;
  4582. }
  4583. static int dpp_connector_match_groups(struct json_token *own_root,
  4584. struct json_token *peer_root)
  4585. {
  4586. struct json_token *groups, *token;
  4587. groups = json_get_member(peer_root, "groups");
  4588. if (!groups || groups->type != JSON_ARRAY) {
  4589. wpa_printf(MSG_DEBUG, "DPP: No peer groups array found");
  4590. return 0;
  4591. }
  4592. for (token = groups->child; token; token = token->sibling) {
  4593. struct json_token *id, *role;
  4594. id = json_get_member(token, "groupId");
  4595. if (!id || id->type != JSON_STRING) {
  4596. wpa_printf(MSG_DEBUG,
  4597. "DPP: Missing peer groupId string");
  4598. continue;
  4599. }
  4600. role = json_get_member(token, "netRole");
  4601. if (!role || role->type != JSON_STRING) {
  4602. wpa_printf(MSG_DEBUG,
  4603. "DPP: Missing peer groups::netRole string");
  4604. continue;
  4605. }
  4606. wpa_printf(MSG_DEBUG,
  4607. "DPP: peer connector group: groupId='%s' netRole='%s'",
  4608. id->string, role->string);
  4609. if (dpp_connector_compatible_group(own_root, id->string,
  4610. role->string)) {
  4611. wpa_printf(MSG_DEBUG,
  4612. "DPP: Compatible group/netRole in own connector");
  4613. return 1;
  4614. }
  4615. }
  4616. return 0;
  4617. }
  4618. static int dpp_derive_pmk(const u8 *Nx, size_t Nx_len, u8 *pmk,
  4619. unsigned int hash_len)
  4620. {
  4621. u8 salt[DPP_MAX_HASH_LEN], prk[DPP_MAX_HASH_LEN];
  4622. const char *info = "DPP PMK";
  4623. int res;
  4624. /* PMK = HKDF(<>, "DPP PMK", N.x) */
  4625. /* HKDF-Extract(<>, N.x) */
  4626. os_memset(salt, 0, hash_len);
  4627. if (dpp_hmac(hash_len, salt, hash_len, Nx, Nx_len, prk) < 0)
  4628. return -1;
  4629. wpa_hexdump_key(MSG_DEBUG, "DPP: PRK = HKDF-Extract(<>, IKM=N.x)",
  4630. prk, hash_len);
  4631. /* HKDF-Expand(PRK, info, L) */
  4632. res = dpp_hkdf_expand(hash_len, prk, hash_len, info, pmk, hash_len);
  4633. os_memset(prk, 0, hash_len);
  4634. if (res < 0)
  4635. return -1;
  4636. wpa_hexdump_key(MSG_DEBUG, "DPP: PMK = HKDF-Expand(PRK, info, L)",
  4637. pmk, hash_len);
  4638. return 0;
  4639. }
  4640. static int dpp_derive_pmkid(const struct dpp_curve_params *curve,
  4641. EVP_PKEY *own_key, EVP_PKEY *peer_key, u8 *pmkid)
  4642. {
  4643. struct wpabuf *nkx, *pkx;
  4644. int ret = -1, res;
  4645. const u8 *addr[2];
  4646. size_t len[2];
  4647. u8 hash[SHA256_MAC_LEN];
  4648. /* PMKID = Truncate-128(H(min(NK.x, PK.x) | max(NK.x, PK.x))) */
  4649. nkx = dpp_get_pubkey_point(own_key, 0);
  4650. pkx = dpp_get_pubkey_point(peer_key, 0);
  4651. if (!nkx || !pkx)
  4652. goto fail;
  4653. addr[0] = wpabuf_head(nkx);
  4654. len[0] = wpabuf_len(nkx) / 2;
  4655. addr[1] = wpabuf_head(pkx);
  4656. len[1] = wpabuf_len(pkx) / 2;
  4657. if (len[0] != len[1])
  4658. goto fail;
  4659. if (os_memcmp(addr[0], addr[1], len[0]) > 0) {
  4660. addr[0] = wpabuf_head(pkx);
  4661. addr[1] = wpabuf_head(nkx);
  4662. }
  4663. wpa_hexdump(MSG_DEBUG, "DPP: PMKID hash payload 1", addr[0], len[0]);
  4664. wpa_hexdump(MSG_DEBUG, "DPP: PMKID hash payload 2", addr[1], len[1]);
  4665. res = sha256_vector(2, addr, len, hash);
  4666. if (res < 0)
  4667. goto fail;
  4668. wpa_hexdump(MSG_DEBUG, "DPP: PMKID hash output", hash, SHA256_MAC_LEN);
  4669. os_memcpy(pmkid, hash, PMKID_LEN);
  4670. wpa_hexdump(MSG_DEBUG, "DPP: PMKID", pmkid, PMKID_LEN);
  4671. ret = 0;
  4672. fail:
  4673. wpabuf_free(nkx);
  4674. wpabuf_free(pkx);
  4675. return ret;
  4676. }
  4677. enum dpp_status_error
  4678. dpp_peer_intro(struct dpp_introduction *intro, const char *own_connector,
  4679. const u8 *net_access_key, size_t net_access_key_len,
  4680. const u8 *csign_key, size_t csign_key_len,
  4681. const u8 *peer_connector, size_t peer_connector_len,
  4682. os_time_t *expiry)
  4683. {
  4684. struct json_token *root = NULL, *netkey, *token;
  4685. struct json_token *own_root = NULL;
  4686. enum dpp_status_error ret = 255, res;
  4687. EVP_PKEY *own_key = NULL, *peer_key = NULL;
  4688. struct wpabuf *own_key_pub = NULL;
  4689. const struct dpp_curve_params *curve, *own_curve;
  4690. struct dpp_signed_connector_info info;
  4691. const unsigned char *p;
  4692. EVP_PKEY *csign = NULL;
  4693. char *signed_connector = NULL;
  4694. const char *pos, *end;
  4695. unsigned char *own_conn = NULL;
  4696. size_t own_conn_len;
  4697. EVP_PKEY_CTX *ctx = NULL;
  4698. size_t Nx_len;
  4699. u8 Nx[DPP_MAX_SHARED_SECRET_LEN];
  4700. os_memset(intro, 0, sizeof(*intro));
  4701. os_memset(&info, 0, sizeof(info));
  4702. if (expiry)
  4703. *expiry = 0;
  4704. p = csign_key;
  4705. csign = d2i_PUBKEY(NULL, &p, csign_key_len);
  4706. if (!csign) {
  4707. wpa_printf(MSG_ERROR,
  4708. "DPP: Failed to parse local C-sign-key information");
  4709. goto fail;
  4710. }
  4711. own_key = dpp_set_keypair(&own_curve, net_access_key,
  4712. net_access_key_len);
  4713. if (!own_key) {
  4714. wpa_printf(MSG_ERROR, "DPP: Failed to parse own netAccessKey");
  4715. goto fail;
  4716. }
  4717. pos = os_strchr(own_connector, '.');
  4718. if (!pos) {
  4719. wpa_printf(MSG_DEBUG, "DPP: Own connector is missing the first dot (.)");
  4720. goto fail;
  4721. }
  4722. pos++;
  4723. end = os_strchr(pos, '.');
  4724. if (!end) {
  4725. wpa_printf(MSG_DEBUG, "DPP: Own connector is missing the second dot (.)");
  4726. goto fail;
  4727. }
  4728. own_conn = base64_url_decode((const unsigned char *) pos,
  4729. end - pos, &own_conn_len);
  4730. if (!own_conn) {
  4731. wpa_printf(MSG_DEBUG,
  4732. "DPP: Failed to base64url decode own signedConnector JWS Payload");
  4733. goto fail;
  4734. }
  4735. own_root = json_parse((const char *) own_conn, own_conn_len);
  4736. if (!own_root) {
  4737. wpa_printf(MSG_DEBUG, "DPP: Failed to parse local connector");
  4738. goto fail;
  4739. }
  4740. wpa_hexdump_ascii(MSG_DEBUG, "DPP: Peer signedConnector",
  4741. peer_connector, peer_connector_len);
  4742. signed_connector = os_malloc(peer_connector_len + 1);
  4743. if (!signed_connector)
  4744. goto fail;
  4745. os_memcpy(signed_connector, peer_connector, peer_connector_len);
  4746. signed_connector[peer_connector_len] = '\0';
  4747. res = dpp_process_signed_connector(&info, csign, signed_connector);
  4748. if (res != DPP_STATUS_OK) {
  4749. ret = res;
  4750. goto fail;
  4751. }
  4752. root = json_parse((const char *) info.payload, info.payload_len);
  4753. if (!root) {
  4754. wpa_printf(MSG_DEBUG, "DPP: JSON parsing of connector failed");
  4755. ret = DPP_STATUS_INVALID_CONNECTOR;
  4756. goto fail;
  4757. }
  4758. if (!dpp_connector_match_groups(own_root, root)) {
  4759. wpa_printf(MSG_DEBUG,
  4760. "DPP: Peer connector does not include compatible group netrole with own connector");
  4761. ret = DPP_STATUS_NO_MATCH;
  4762. goto fail;
  4763. }
  4764. token = json_get_member(root, "expiry");
  4765. if (!token || token->type != JSON_STRING) {
  4766. wpa_printf(MSG_DEBUG,
  4767. "DPP: No expiry string found - connector does not expire");
  4768. } else {
  4769. wpa_printf(MSG_DEBUG, "DPP: expiry = %s", token->string);
  4770. if (dpp_key_expired(token->string, expiry)) {
  4771. wpa_printf(MSG_DEBUG,
  4772. "DPP: Connector (netAccessKey) has expired");
  4773. ret = DPP_STATUS_INVALID_CONNECTOR;
  4774. goto fail;
  4775. }
  4776. }
  4777. netkey = json_get_member(root, "netAccessKey");
  4778. if (!netkey || netkey->type != JSON_OBJECT) {
  4779. wpa_printf(MSG_DEBUG, "DPP: No netAccessKey object found");
  4780. ret = DPP_STATUS_INVALID_CONNECTOR;
  4781. goto fail;
  4782. }
  4783. peer_key = dpp_parse_jwk(netkey, &curve);
  4784. if (!peer_key) {
  4785. ret = DPP_STATUS_INVALID_CONNECTOR;
  4786. goto fail;
  4787. }
  4788. dpp_debug_print_key("DPP: Received netAccessKey", peer_key);
  4789. if (own_curve != curve) {
  4790. wpa_printf(MSG_DEBUG,
  4791. "DPP: Mismatching netAccessKey curves (%s != %s)",
  4792. own_curve->name, curve->name);
  4793. ret = DPP_STATUS_INVALID_CONNECTOR;
  4794. goto fail;
  4795. }
  4796. /* ECDH: N = nk * PK */
  4797. ctx = EVP_PKEY_CTX_new(own_key, NULL);
  4798. if (!ctx ||
  4799. EVP_PKEY_derive_init(ctx) != 1 ||
  4800. EVP_PKEY_derive_set_peer(ctx, peer_key) != 1 ||
  4801. EVP_PKEY_derive(ctx, NULL, &Nx_len) != 1 ||
  4802. Nx_len > DPP_MAX_SHARED_SECRET_LEN ||
  4803. EVP_PKEY_derive(ctx, Nx, &Nx_len) != 1) {
  4804. wpa_printf(MSG_ERROR,
  4805. "DPP: Failed to derive ECDH shared secret: %s",
  4806. ERR_error_string(ERR_get_error(), NULL));
  4807. goto fail;
  4808. }
  4809. wpa_hexdump_key(MSG_DEBUG, "DPP: ECDH shared secret (N.x)",
  4810. Nx, Nx_len);
  4811. /* PMK = HKDF(<>, "DPP PMK", N.x) */
  4812. if (dpp_derive_pmk(Nx, Nx_len, intro->pmk, curve->hash_len) < 0) {
  4813. wpa_printf(MSG_ERROR, "DPP: Failed to derive PMK");
  4814. goto fail;
  4815. }
  4816. intro->pmk_len = curve->hash_len;
  4817. /* PMKID = Truncate-128(H(min(NK.x, PK.x) | max(NK.x, PK.x))) */
  4818. if (dpp_derive_pmkid(curve, own_key, peer_key, intro->pmkid) < 0) {
  4819. wpa_printf(MSG_ERROR, "DPP: Failed to derive PMKID");
  4820. goto fail;
  4821. }
  4822. ret = DPP_STATUS_OK;
  4823. fail:
  4824. if (ret != DPP_STATUS_OK)
  4825. os_memset(intro, 0, sizeof(*intro));
  4826. os_memset(Nx, 0, sizeof(Nx));
  4827. EVP_PKEY_CTX_free(ctx);
  4828. os_free(own_conn);
  4829. os_free(signed_connector);
  4830. os_free(info.payload);
  4831. EVP_PKEY_free(own_key);
  4832. wpabuf_free(own_key_pub);
  4833. EVP_PKEY_free(peer_key);
  4834. EVP_PKEY_free(csign);
  4835. json_free(root);
  4836. json_free(own_root);
  4837. return ret;
  4838. }
  4839. static EVP_PKEY * dpp_pkex_get_role_elem(const struct dpp_curve_params *curve,
  4840. int init)
  4841. {
  4842. EC_GROUP *group;
  4843. size_t len = curve->prime_len;
  4844. const u8 *x, *y;
  4845. switch (curve->ike_group) {
  4846. case 19:
  4847. x = init ? pkex_init_x_p256 : pkex_resp_x_p256;
  4848. y = init ? pkex_init_y_p256 : pkex_resp_y_p256;
  4849. break;
  4850. case 20:
  4851. x = init ? pkex_init_x_p384 : pkex_resp_x_p384;
  4852. y = init ? pkex_init_y_p384 : pkex_resp_y_p384;
  4853. break;
  4854. case 21:
  4855. x = init ? pkex_init_x_p521 : pkex_resp_x_p521;
  4856. y = init ? pkex_init_y_p521 : pkex_resp_y_p521;
  4857. break;
  4858. case 28:
  4859. x = init ? pkex_init_x_bp_p256r1 : pkex_resp_x_bp_p256r1;
  4860. y = init ? pkex_init_y_bp_p256r1 : pkex_resp_y_bp_p256r1;
  4861. break;
  4862. case 29:
  4863. x = init ? pkex_init_x_bp_p384r1 : pkex_resp_x_bp_p384r1;
  4864. y = init ? pkex_init_y_bp_p384r1 : pkex_resp_y_bp_p384r1;
  4865. break;
  4866. case 30:
  4867. x = init ? pkex_init_x_bp_p512r1 : pkex_resp_x_bp_p512r1;
  4868. y = init ? pkex_init_y_bp_p512r1 : pkex_resp_y_bp_p512r1;
  4869. break;
  4870. default:
  4871. return NULL;
  4872. }
  4873. group = EC_GROUP_new_by_curve_name(OBJ_txt2nid(curve->name));
  4874. if (!group)
  4875. return NULL;
  4876. return dpp_set_pubkey_point_group(group, x, y, len);
  4877. }
  4878. static EC_POINT * dpp_pkex_derive_Qi(const struct dpp_curve_params *curve,
  4879. const u8 *mac_init, const char *code,
  4880. const char *identifier, BN_CTX *bnctx,
  4881. const EC_GROUP **ret_group)
  4882. {
  4883. u8 hash[DPP_MAX_HASH_LEN];
  4884. const u8 *addr[3];
  4885. size_t len[3];
  4886. unsigned int num_elem = 0;
  4887. EC_POINT *Qi = NULL;
  4888. EVP_PKEY *Pi = NULL;
  4889. EC_KEY *Pi_ec = NULL;
  4890. const EC_POINT *Pi_point;
  4891. BIGNUM *hash_bn = NULL;
  4892. const EC_GROUP *group = NULL;
  4893. EC_GROUP *group2 = NULL;
  4894. /* Qi = H(MAC-Initiator | [identifier |] code) * Pi */
  4895. wpa_printf(MSG_DEBUG, "DPP: MAC-Initiator: " MACSTR, MAC2STR(mac_init));
  4896. addr[num_elem] = mac_init;
  4897. len[num_elem] = ETH_ALEN;
  4898. num_elem++;
  4899. if (identifier) {
  4900. wpa_printf(MSG_DEBUG, "DPP: code identifier: %s",
  4901. identifier);
  4902. addr[num_elem] = (const u8 *) identifier;
  4903. len[num_elem] = os_strlen(identifier);
  4904. num_elem++;
  4905. }
  4906. wpa_hexdump_ascii_key(MSG_DEBUG, "DPP: code", code, os_strlen(code));
  4907. addr[num_elem] = (const u8 *) code;
  4908. len[num_elem] = os_strlen(code);
  4909. num_elem++;
  4910. if (dpp_hash_vector(curve, num_elem, addr, len, hash) < 0)
  4911. goto fail;
  4912. wpa_hexdump_key(MSG_DEBUG,
  4913. "DPP: H(MAC-Initiator | [identifier |] code)",
  4914. hash, curve->hash_len);
  4915. Pi = dpp_pkex_get_role_elem(curve, 1);
  4916. if (!Pi)
  4917. goto fail;
  4918. dpp_debug_print_key("DPP: Pi", Pi);
  4919. Pi_ec = EVP_PKEY_get1_EC_KEY(Pi);
  4920. if (!Pi_ec)
  4921. goto fail;
  4922. Pi_point = EC_KEY_get0_public_key(Pi_ec);
  4923. group = EC_KEY_get0_group(Pi_ec);
  4924. if (!group)
  4925. goto fail;
  4926. group2 = EC_GROUP_dup(group);
  4927. if (!group2)
  4928. goto fail;
  4929. Qi = EC_POINT_new(group2);
  4930. if (!Qi) {
  4931. EC_GROUP_free(group2);
  4932. goto fail;
  4933. }
  4934. hash_bn = BN_bin2bn(hash, curve->hash_len, NULL);
  4935. if (!hash_bn ||
  4936. EC_POINT_mul(group2, Qi, NULL, Pi_point, hash_bn, bnctx) != 1)
  4937. goto fail;
  4938. if (EC_POINT_is_at_infinity(group, Qi)) {
  4939. wpa_printf(MSG_INFO, "DPP: Qi is the point-at-infinity");
  4940. goto fail;
  4941. }
  4942. out:
  4943. EC_KEY_free(Pi_ec);
  4944. EVP_PKEY_free(Pi);
  4945. BN_clear_free(hash_bn);
  4946. if (ret_group)
  4947. *ret_group = group2;
  4948. return Qi;
  4949. fail:
  4950. EC_POINT_free(Qi);
  4951. Qi = NULL;
  4952. goto out;
  4953. }
  4954. static EC_POINT * dpp_pkex_derive_Qr(const struct dpp_curve_params *curve,
  4955. const u8 *mac_resp, const char *code,
  4956. const char *identifier, BN_CTX *bnctx,
  4957. const EC_GROUP **ret_group)
  4958. {
  4959. u8 hash[DPP_MAX_HASH_LEN];
  4960. const u8 *addr[3];
  4961. size_t len[3];
  4962. unsigned int num_elem = 0;
  4963. EC_POINT *Qr = NULL;
  4964. EVP_PKEY *Pr = NULL;
  4965. EC_KEY *Pr_ec = NULL;
  4966. const EC_POINT *Pr_point;
  4967. BIGNUM *hash_bn = NULL;
  4968. const EC_GROUP *group = NULL;
  4969. EC_GROUP *group2 = NULL;
  4970. /* Qr = H(MAC-Responder | | [identifier | ] code) * Pr */
  4971. wpa_printf(MSG_DEBUG, "DPP: MAC-Responder: " MACSTR, MAC2STR(mac_resp));
  4972. addr[num_elem] = mac_resp;
  4973. len[num_elem] = ETH_ALEN;
  4974. num_elem++;
  4975. if (identifier) {
  4976. wpa_printf(MSG_DEBUG, "DPP: code identifier: %s",
  4977. identifier);
  4978. addr[num_elem] = (const u8 *) identifier;
  4979. len[num_elem] = os_strlen(identifier);
  4980. num_elem++;
  4981. }
  4982. wpa_hexdump_ascii_key(MSG_DEBUG, "DPP: code", code, os_strlen(code));
  4983. addr[num_elem] = (const u8 *) code;
  4984. len[num_elem] = os_strlen(code);
  4985. num_elem++;
  4986. if (dpp_hash_vector(curve, num_elem, addr, len, hash) < 0)
  4987. goto fail;
  4988. wpa_hexdump_key(MSG_DEBUG,
  4989. "DPP: H(MAC-Responder | [identifier |] code)",
  4990. hash, curve->hash_len);
  4991. Pr = dpp_pkex_get_role_elem(curve, 0);
  4992. if (!Pr)
  4993. goto fail;
  4994. dpp_debug_print_key("DPP: Pr", Pr);
  4995. Pr_ec = EVP_PKEY_get1_EC_KEY(Pr);
  4996. if (!Pr_ec)
  4997. goto fail;
  4998. Pr_point = EC_KEY_get0_public_key(Pr_ec);
  4999. group = EC_KEY_get0_group(Pr_ec);
  5000. if (!group)
  5001. goto fail;
  5002. group2 = EC_GROUP_dup(group);
  5003. if (!group2)
  5004. goto fail;
  5005. Qr = EC_POINT_new(group2);
  5006. if (!Qr) {
  5007. EC_GROUP_free(group2);
  5008. goto fail;
  5009. }
  5010. hash_bn = BN_bin2bn(hash, curve->hash_len, NULL);
  5011. if (!hash_bn ||
  5012. EC_POINT_mul(group2, Qr, NULL, Pr_point, hash_bn, bnctx) != 1)
  5013. goto fail;
  5014. if (EC_POINT_is_at_infinity(group, Qr)) {
  5015. wpa_printf(MSG_INFO, "DPP: Qr is the point-at-infinity");
  5016. goto fail;
  5017. }
  5018. out:
  5019. EC_KEY_free(Pr_ec);
  5020. EVP_PKEY_free(Pr);
  5021. BN_clear_free(hash_bn);
  5022. if (ret_group)
  5023. *ret_group = group2;
  5024. return Qr;
  5025. fail:
  5026. EC_POINT_free(Qr);
  5027. Qr = NULL;
  5028. goto out;
  5029. }
  5030. #ifdef CONFIG_TESTING_OPTIONS
  5031. static int dpp_test_gen_invalid_key(struct wpabuf *msg,
  5032. const struct dpp_curve_params *curve)
  5033. {
  5034. BN_CTX *ctx;
  5035. BIGNUM *x, *y;
  5036. int ret = -1;
  5037. EC_GROUP *group;
  5038. EC_POINT *point;
  5039. group = EC_GROUP_new_by_curve_name(OBJ_txt2nid(curve->name));
  5040. if (!group)
  5041. return NULL;
  5042. ctx = BN_CTX_new();
  5043. point = EC_POINT_new(group);
  5044. x = BN_new();
  5045. y = BN_new();
  5046. if (!ctx || !point || !x || !y)
  5047. goto fail;
  5048. if (BN_rand(x, curve->prime_len * 8, 0, 0) != 1)
  5049. goto fail;
  5050. /* Generate a random y coordinate that results in a point that is not
  5051. * on the curve. */
  5052. for (;;) {
  5053. if (BN_rand(y, curve->prime_len * 8, 0, 0) != 1)
  5054. goto fail;
  5055. if (EC_POINT_set_affine_coordinates_GFp(group, point, x, y,
  5056. ctx) != 1)
  5057. goto fail;
  5058. if (!EC_POINT_is_on_curve(group, point, ctx))
  5059. break;
  5060. }
  5061. if (dpp_bn2bin_pad(x, wpabuf_put(msg, curve->prime_len),
  5062. curve->prime_len) < 0 ||
  5063. dpp_bn2bin_pad(y, wpabuf_put(msg, curve->prime_len),
  5064. curve->prime_len) < 0)
  5065. goto fail;
  5066. ret = 0;
  5067. fail:
  5068. BN_free(x);
  5069. BN_free(y);
  5070. EC_POINT_free(point);
  5071. BN_CTX_free(ctx);
  5072. return ret;
  5073. }
  5074. #endif /* CONFIG_TESTING_OPTIONS */
  5075. static struct wpabuf * dpp_pkex_build_exchange_req(struct dpp_pkex *pkex)
  5076. {
  5077. EC_KEY *X_ec = NULL;
  5078. const EC_POINT *X_point;
  5079. BN_CTX *bnctx = NULL;
  5080. const EC_GROUP *group;
  5081. EC_POINT *Qi = NULL, *M = NULL;
  5082. struct wpabuf *M_buf = NULL;
  5083. BIGNUM *Mx = NULL, *My = NULL;
  5084. struct wpabuf *msg = NULL;
  5085. size_t attr_len;
  5086. const struct dpp_curve_params *curve = pkex->own_bi->curve;
  5087. wpa_printf(MSG_DEBUG, "DPP: Build PKEX Exchange Request");
  5088. /* Qi = H(MAC-Initiator | [identifier |] code) * Pi */
  5089. bnctx = BN_CTX_new();
  5090. if (!bnctx)
  5091. goto fail;
  5092. Qi = dpp_pkex_derive_Qi(curve, pkex->own_mac, pkex->code,
  5093. pkex->identifier, bnctx, &group);
  5094. if (!Qi)
  5095. goto fail;
  5096. /* Generate a random ephemeral keypair x/X */
  5097. pkex->x = dpp_gen_keypair(curve);
  5098. if (!pkex->x)
  5099. goto fail;
  5100. /* M = X + Qi */
  5101. X_ec = EVP_PKEY_get1_EC_KEY(pkex->x);
  5102. if (!X_ec)
  5103. goto fail;
  5104. X_point = EC_KEY_get0_public_key(X_ec);
  5105. if (!X_point)
  5106. goto fail;
  5107. M = EC_POINT_new(group);
  5108. Mx = BN_new();
  5109. My = BN_new();
  5110. if (!M || !Mx || !My ||
  5111. EC_POINT_add(group, M, X_point, Qi, bnctx) != 1 ||
  5112. EC_POINT_get_affine_coordinates_GFp(group, M, Mx, My, bnctx) != 1)
  5113. goto fail;
  5114. /* Initiator -> Responder: group, [identifier,] M */
  5115. attr_len = 4 + 2;
  5116. if (pkex->identifier)
  5117. attr_len += 4 + os_strlen(pkex->identifier);
  5118. attr_len += 4 + 2 * curve->prime_len;
  5119. msg = dpp_alloc_msg(DPP_PA_PKEX_EXCHANGE_REQ, attr_len);
  5120. if (!msg)
  5121. goto fail;
  5122. #ifdef CONFIG_TESTING_OPTIONS
  5123. if (dpp_test == DPP_TEST_NO_FINITE_CYCLIC_GROUP_PKEX_EXCHANGE_REQ) {
  5124. wpa_printf(MSG_INFO, "DPP: TESTING - no Finite Cyclic Group");
  5125. goto skip_finite_cyclic_group;
  5126. }
  5127. #endif /* CONFIG_TESTING_OPTIONS */
  5128. /* Finite Cyclic Group attribute */
  5129. wpabuf_put_le16(msg, DPP_ATTR_FINITE_CYCLIC_GROUP);
  5130. wpabuf_put_le16(msg, 2);
  5131. wpabuf_put_le16(msg, curve->ike_group);
  5132. #ifdef CONFIG_TESTING_OPTIONS
  5133. skip_finite_cyclic_group:
  5134. #endif /* CONFIG_TESTING_OPTIONS */
  5135. /* Code Identifier attribute */
  5136. if (pkex->identifier) {
  5137. wpabuf_put_le16(msg, DPP_ATTR_CODE_IDENTIFIER);
  5138. wpabuf_put_le16(msg, os_strlen(pkex->identifier));
  5139. wpabuf_put_str(msg, pkex->identifier);
  5140. }
  5141. #ifdef CONFIG_TESTING_OPTIONS
  5142. if (dpp_test == DPP_TEST_NO_ENCRYPTED_KEY_PKEX_EXCHANGE_REQ) {
  5143. wpa_printf(MSG_INFO, "DPP: TESTING - no Encrypted Key");
  5144. goto out;
  5145. }
  5146. #endif /* CONFIG_TESTING_OPTIONS */
  5147. /* M in Encrypted Key attribute */
  5148. wpabuf_put_le16(msg, DPP_ATTR_ENCRYPTED_KEY);
  5149. wpabuf_put_le16(msg, 2 * curve->prime_len);
  5150. #ifdef CONFIG_TESTING_OPTIONS
  5151. if (dpp_test == DPP_TEST_INVALID_ENCRYPTED_KEY_PKEX_EXCHANGE_REQ) {
  5152. wpa_printf(MSG_INFO, "DPP: TESTING - invalid Encrypted Key");
  5153. if (dpp_test_gen_invalid_key(msg, curve) < 0)
  5154. goto fail;
  5155. goto out;
  5156. }
  5157. #endif /* CONFIG_TESTING_OPTIONS */
  5158. if (dpp_bn2bin_pad(Mx, wpabuf_put(msg, curve->prime_len),
  5159. curve->prime_len) < 0 ||
  5160. dpp_bn2bin_pad(Mx, pkex->Mx, curve->prime_len) < 0 ||
  5161. dpp_bn2bin_pad(My, wpabuf_put(msg, curve->prime_len),
  5162. curve->prime_len) < 0)
  5163. goto fail;
  5164. out:
  5165. wpabuf_free(M_buf);
  5166. EC_KEY_free(X_ec);
  5167. EC_POINT_free(M);
  5168. EC_POINT_free(Qi);
  5169. BN_clear_free(Mx);
  5170. BN_clear_free(My);
  5171. BN_CTX_free(bnctx);
  5172. return msg;
  5173. fail:
  5174. wpa_printf(MSG_INFO, "DPP: Failed to build PKEX Exchange Request");
  5175. wpabuf_free(msg);
  5176. msg = NULL;
  5177. goto out;
  5178. }
  5179. static void dpp_pkex_fail(struct dpp_pkex *pkex, const char *txt)
  5180. {
  5181. wpa_msg(pkex->msg_ctx, MSG_INFO, DPP_EVENT_FAIL "%s", txt);
  5182. }
  5183. struct dpp_pkex * dpp_pkex_init(void *msg_ctx, struct dpp_bootstrap_info *bi,
  5184. const u8 *own_mac,
  5185. const char *identifier,
  5186. const char *code)
  5187. {
  5188. struct dpp_pkex *pkex;
  5189. pkex = os_zalloc(sizeof(*pkex));
  5190. if (!pkex)
  5191. return NULL;
  5192. pkex->msg_ctx = msg_ctx;
  5193. pkex->initiator = 1;
  5194. pkex->own_bi = bi;
  5195. os_memcpy(pkex->own_mac, own_mac, ETH_ALEN);
  5196. if (identifier) {
  5197. pkex->identifier = os_strdup(identifier);
  5198. if (!pkex->identifier)
  5199. goto fail;
  5200. }
  5201. pkex->code = os_strdup(code);
  5202. if (!pkex->code)
  5203. goto fail;
  5204. pkex->exchange_req = dpp_pkex_build_exchange_req(pkex);
  5205. if (!pkex->exchange_req)
  5206. goto fail;
  5207. return pkex;
  5208. fail:
  5209. dpp_pkex_free(pkex);
  5210. return NULL;
  5211. }
  5212. static struct wpabuf *
  5213. dpp_pkex_build_exchange_resp(struct dpp_pkex *pkex,
  5214. enum dpp_status_error status,
  5215. const BIGNUM *Nx, const BIGNUM *Ny)
  5216. {
  5217. struct wpabuf *msg = NULL;
  5218. size_t attr_len;
  5219. const struct dpp_curve_params *curve = pkex->own_bi->curve;
  5220. /* Initiator -> Responder: DPP Status, [identifier,] N */
  5221. attr_len = 4 + 1;
  5222. if (pkex->identifier)
  5223. attr_len += 4 + os_strlen(pkex->identifier);
  5224. attr_len += 4 + 2 * curve->prime_len;
  5225. msg = dpp_alloc_msg(DPP_PA_PKEX_EXCHANGE_RESP, attr_len);
  5226. if (!msg)
  5227. goto fail;
  5228. #ifdef CONFIG_TESTING_OPTIONS
  5229. if (dpp_test == DPP_TEST_NO_STATUS_PKEX_EXCHANGE_RESP) {
  5230. wpa_printf(MSG_INFO, "DPP: TESTING - no Status");
  5231. goto skip_status;
  5232. }
  5233. if (dpp_test == DPP_TEST_INVALID_STATUS_PKEX_EXCHANGE_RESP) {
  5234. wpa_printf(MSG_INFO, "DPP: TESTING - invalid Status");
  5235. status = 255;
  5236. }
  5237. #endif /* CONFIG_TESTING_OPTIONS */
  5238. /* DPP Status */
  5239. wpabuf_put_le16(msg, DPP_ATTR_STATUS);
  5240. wpabuf_put_le16(msg, 1);
  5241. wpabuf_put_u8(msg, status);
  5242. #ifdef CONFIG_TESTING_OPTIONS
  5243. skip_status:
  5244. #endif /* CONFIG_TESTING_OPTIONS */
  5245. /* Code Identifier attribute */
  5246. if (pkex->identifier) {
  5247. wpabuf_put_le16(msg, DPP_ATTR_CODE_IDENTIFIER);
  5248. wpabuf_put_le16(msg, os_strlen(pkex->identifier));
  5249. wpabuf_put_str(msg, pkex->identifier);
  5250. }
  5251. if (status != DPP_STATUS_OK)
  5252. goto skip_encrypted_key;
  5253. #ifdef CONFIG_TESTING_OPTIONS
  5254. if (dpp_test == DPP_TEST_NO_ENCRYPTED_KEY_PKEX_EXCHANGE_RESP) {
  5255. wpa_printf(MSG_INFO, "DPP: TESTING - no Encrypted Key");
  5256. goto skip_encrypted_key;
  5257. }
  5258. #endif /* CONFIG_TESTING_OPTIONS */
  5259. /* N in Encrypted Key attribute */
  5260. wpabuf_put_le16(msg, DPP_ATTR_ENCRYPTED_KEY);
  5261. wpabuf_put_le16(msg, 2 * curve->prime_len);
  5262. #ifdef CONFIG_TESTING_OPTIONS
  5263. if (dpp_test == DPP_TEST_INVALID_ENCRYPTED_KEY_PKEX_EXCHANGE_RESP) {
  5264. wpa_printf(MSG_INFO, "DPP: TESTING - invalid Encrypted Key");
  5265. if (dpp_test_gen_invalid_key(msg, curve) < 0)
  5266. goto fail;
  5267. goto skip_encrypted_key;
  5268. }
  5269. #endif /* CONFIG_TESTING_OPTIONS */
  5270. if (dpp_bn2bin_pad(Nx, wpabuf_put(msg, curve->prime_len),
  5271. curve->prime_len) < 0 ||
  5272. dpp_bn2bin_pad(Nx, pkex->Nx, curve->prime_len) < 0 ||
  5273. dpp_bn2bin_pad(Ny, wpabuf_put(msg, curve->prime_len),
  5274. curve->prime_len) < 0)
  5275. goto fail;
  5276. skip_encrypted_key:
  5277. if (status == DPP_STATUS_BAD_GROUP) {
  5278. /* Finite Cyclic Group attribute */
  5279. wpabuf_put_le16(msg, DPP_ATTR_FINITE_CYCLIC_GROUP);
  5280. wpabuf_put_le16(msg, 2);
  5281. wpabuf_put_le16(msg, curve->ike_group);
  5282. }
  5283. return msg;
  5284. fail:
  5285. wpabuf_free(msg);
  5286. return NULL;
  5287. }
  5288. static int dpp_pkex_derive_z(const u8 *mac_init, const u8 *mac_resp,
  5289. const u8 *Mx, size_t Mx_len,
  5290. const u8 *Nx, size_t Nx_len,
  5291. const char *code,
  5292. const u8 *Kx, size_t Kx_len,
  5293. u8 *z, unsigned int hash_len)
  5294. {
  5295. u8 salt[DPP_MAX_HASH_LEN], prk[DPP_MAX_HASH_LEN];
  5296. int res;
  5297. u8 *info, *pos;
  5298. size_t info_len;
  5299. /* z = HKDF(<>, MAC-Initiator | MAC-Responder | M.x | N.x | code, K.x)
  5300. */
  5301. /* HKDF-Extract(<>, IKM=K.x) */
  5302. os_memset(salt, 0, hash_len);
  5303. if (dpp_hmac(hash_len, salt, hash_len, Kx, Kx_len, prk) < 0)
  5304. return -1;
  5305. wpa_hexdump_key(MSG_DEBUG, "DPP: PRK = HKDF-Extract(<>, IKM)",
  5306. prk, hash_len);
  5307. info_len = 2 * ETH_ALEN + Mx_len + Nx_len + os_strlen(code);
  5308. info = os_malloc(info_len);
  5309. if (!info)
  5310. return -1;
  5311. pos = info;
  5312. os_memcpy(pos, mac_init, ETH_ALEN);
  5313. pos += ETH_ALEN;
  5314. os_memcpy(pos, mac_resp, ETH_ALEN);
  5315. pos += ETH_ALEN;
  5316. os_memcpy(pos, Mx, Mx_len);
  5317. pos += Mx_len;
  5318. os_memcpy(pos, Nx, Nx_len);
  5319. pos += Nx_len;
  5320. os_memcpy(pos, code, os_strlen(code));
  5321. /* HKDF-Expand(PRK, info, L) */
  5322. if (hash_len == 32)
  5323. res = hmac_sha256_kdf(prk, hash_len, NULL, info, info_len,
  5324. z, hash_len);
  5325. else if (hash_len == 48)
  5326. res = hmac_sha384_kdf(prk, hash_len, NULL, info, info_len,
  5327. z, hash_len);
  5328. else if (hash_len == 64)
  5329. res = hmac_sha512_kdf(prk, hash_len, NULL, info, info_len,
  5330. z, hash_len);
  5331. else
  5332. res = -1;
  5333. os_free(info);
  5334. os_memset(prk, 0, hash_len);
  5335. if (res < 0)
  5336. return -1;
  5337. wpa_hexdump_key(MSG_DEBUG, "DPP: z = HKDF-Expand(PRK, info, L)",
  5338. z, hash_len);
  5339. return 0;
  5340. }
  5341. struct dpp_pkex * dpp_pkex_rx_exchange_req(void *msg_ctx,
  5342. struct dpp_bootstrap_info *bi,
  5343. const u8 *own_mac,
  5344. const u8 *peer_mac,
  5345. const char *identifier,
  5346. const char *code,
  5347. const u8 *buf, size_t len)
  5348. {
  5349. const u8 *attr_group, *attr_id, *attr_key;
  5350. u16 attr_group_len, attr_id_len, attr_key_len;
  5351. const struct dpp_curve_params *curve = bi->curve;
  5352. u16 ike_group;
  5353. struct dpp_pkex *pkex = NULL;
  5354. EC_POINT *Qi = NULL, *Qr = NULL, *M = NULL, *X = NULL, *N = NULL;
  5355. BN_CTX *bnctx = NULL;
  5356. const EC_GROUP *group;
  5357. BIGNUM *Mx = NULL, *My = NULL;
  5358. EC_KEY *Y_ec = NULL, *X_ec = NULL;;
  5359. const EC_POINT *Y_point;
  5360. BIGNUM *Nx = NULL, *Ny = NULL;
  5361. u8 Kx[DPP_MAX_SHARED_SECRET_LEN];
  5362. size_t Kx_len;
  5363. int res;
  5364. EVP_PKEY_CTX *ctx = NULL;
  5365. if (bi->pkex_t >= PKEX_COUNTER_T_LIMIT) {
  5366. wpa_msg(msg_ctx, MSG_INFO, DPP_EVENT_FAIL
  5367. "PKEX counter t limit reached - ignore message");
  5368. return NULL;
  5369. }
  5370. attr_id = dpp_get_attr(buf, len, DPP_ATTR_CODE_IDENTIFIER,
  5371. &attr_id_len);
  5372. if (!attr_id && identifier) {
  5373. wpa_printf(MSG_DEBUG,
  5374. "DPP: No PKEX code identifier received, but expected one");
  5375. return NULL;
  5376. }
  5377. if (attr_id && identifier &&
  5378. (os_strlen(identifier) != attr_id_len ||
  5379. os_memcmp(identifier, attr_id, attr_id_len) != 0)) {
  5380. wpa_printf(MSG_DEBUG, "DPP: PKEX code identifier mismatch");
  5381. return NULL;
  5382. }
  5383. attr_group = dpp_get_attr(buf, len, DPP_ATTR_FINITE_CYCLIC_GROUP,
  5384. &attr_group_len);
  5385. if (!attr_group || attr_group_len != 2) {
  5386. wpa_msg(msg_ctx, MSG_INFO, DPP_EVENT_FAIL
  5387. "Missing or invalid Finite Cyclic Group attribute");
  5388. return NULL;
  5389. }
  5390. ike_group = WPA_GET_LE16(attr_group);
  5391. if (ike_group != curve->ike_group) {
  5392. wpa_msg(msg_ctx, MSG_INFO, DPP_EVENT_FAIL
  5393. "Mismatching PKEX curve: peer=%u own=%u",
  5394. ike_group, curve->ike_group);
  5395. pkex = os_zalloc(sizeof(*pkex));
  5396. if (!pkex)
  5397. goto fail;
  5398. pkex->own_bi = bi;
  5399. pkex->failed = 1;
  5400. pkex->exchange_resp = dpp_pkex_build_exchange_resp(
  5401. pkex, DPP_STATUS_BAD_GROUP, NULL, NULL);
  5402. if (!pkex->exchange_resp)
  5403. goto fail;
  5404. return pkex;
  5405. }
  5406. /* M in Encrypted Key attribute */
  5407. attr_key = dpp_get_attr(buf, len, DPP_ATTR_ENCRYPTED_KEY,
  5408. &attr_key_len);
  5409. if (!attr_key || attr_key_len & 0x01 || attr_key_len < 2 ||
  5410. attr_key_len / 2 > DPP_MAX_SHARED_SECRET_LEN) {
  5411. wpa_msg(msg_ctx, MSG_INFO, DPP_EVENT_FAIL
  5412. "Missing Encrypted Key attribute");
  5413. return NULL;
  5414. }
  5415. /* Qi = H(MAC-Initiator | [identifier |] code) * Pi */
  5416. bnctx = BN_CTX_new();
  5417. if (!bnctx)
  5418. goto fail;
  5419. Qi = dpp_pkex_derive_Qi(curve, peer_mac, code, identifier, bnctx,
  5420. &group);
  5421. if (!Qi)
  5422. goto fail;
  5423. /* X' = M - Qi */
  5424. X = EC_POINT_new(group);
  5425. M = EC_POINT_new(group);
  5426. Mx = BN_bin2bn(attr_key, attr_key_len / 2, NULL);
  5427. My = BN_bin2bn(attr_key + attr_key_len / 2, attr_key_len / 2, NULL);
  5428. if (!X || !M || !Mx || !My ||
  5429. EC_POINT_set_affine_coordinates_GFp(group, M, Mx, My, bnctx) != 1 ||
  5430. EC_POINT_is_at_infinity(group, M) ||
  5431. !EC_POINT_is_on_curve(group, M, bnctx) ||
  5432. EC_POINT_invert(group, Qi, bnctx) != 1 ||
  5433. EC_POINT_add(group, X, M, Qi, bnctx) != 1 ||
  5434. EC_POINT_is_at_infinity(group, X) ||
  5435. !EC_POINT_is_on_curve(group, X, bnctx)) {
  5436. wpa_msg(msg_ctx, MSG_INFO, DPP_EVENT_FAIL
  5437. "Invalid Encrypted Key value");
  5438. bi->pkex_t++;
  5439. goto fail;
  5440. }
  5441. pkex = os_zalloc(sizeof(*pkex));
  5442. if (!pkex)
  5443. goto fail;
  5444. pkex->t = bi->pkex_t;
  5445. pkex->msg_ctx = msg_ctx;
  5446. pkex->own_bi = bi;
  5447. os_memcpy(pkex->own_mac, own_mac, ETH_ALEN);
  5448. os_memcpy(pkex->peer_mac, peer_mac, ETH_ALEN);
  5449. if (identifier) {
  5450. pkex->identifier = os_strdup(identifier);
  5451. if (!pkex->identifier)
  5452. goto fail;
  5453. }
  5454. pkex->code = os_strdup(code);
  5455. if (!pkex->code)
  5456. goto fail;
  5457. os_memcpy(pkex->Mx, attr_key, attr_key_len / 2);
  5458. X_ec = EC_KEY_new();
  5459. if (!X_ec ||
  5460. EC_KEY_set_group(X_ec, group) != 1 ||
  5461. EC_KEY_set_public_key(X_ec, X) != 1)
  5462. goto fail;
  5463. pkex->x = EVP_PKEY_new();
  5464. if (!pkex->x ||
  5465. EVP_PKEY_set1_EC_KEY(pkex->x, X_ec) != 1)
  5466. goto fail;
  5467. /* Qr = H(MAC-Responder | | [identifier | ] code) * Pr */
  5468. Qr = dpp_pkex_derive_Qr(curve, own_mac, code, identifier, bnctx, NULL);
  5469. if (!Qr)
  5470. goto fail;
  5471. /* Generate a random ephemeral keypair y/Y */
  5472. pkex->y = dpp_gen_keypair(curve);
  5473. if (!pkex->y)
  5474. goto fail;
  5475. /* N = Y + Qr */
  5476. Y_ec = EVP_PKEY_get1_EC_KEY(pkex->y);
  5477. if (!Y_ec)
  5478. goto fail;
  5479. Y_point = EC_KEY_get0_public_key(Y_ec);
  5480. if (!Y_point)
  5481. goto fail;
  5482. N = EC_POINT_new(group);
  5483. Nx = BN_new();
  5484. Ny = BN_new();
  5485. if (!N || !Nx || !Ny ||
  5486. EC_POINT_add(group, N, Y_point, Qr, bnctx) != 1 ||
  5487. EC_POINT_get_affine_coordinates_GFp(group, N, Nx, Ny, bnctx) != 1)
  5488. goto fail;
  5489. pkex->exchange_resp = dpp_pkex_build_exchange_resp(pkex, DPP_STATUS_OK,
  5490. Nx, Ny);
  5491. if (!pkex->exchange_resp)
  5492. goto fail;
  5493. /* K = y * X' */
  5494. ctx = EVP_PKEY_CTX_new(pkex->y, NULL);
  5495. if (!ctx ||
  5496. EVP_PKEY_derive_init(ctx) != 1 ||
  5497. EVP_PKEY_derive_set_peer(ctx, pkex->x) != 1 ||
  5498. EVP_PKEY_derive(ctx, NULL, &Kx_len) != 1 ||
  5499. Kx_len > DPP_MAX_SHARED_SECRET_LEN ||
  5500. EVP_PKEY_derive(ctx, Kx, &Kx_len) != 1) {
  5501. wpa_printf(MSG_ERROR,
  5502. "DPP: Failed to derive ECDH shared secret: %s",
  5503. ERR_error_string(ERR_get_error(), NULL));
  5504. goto fail;
  5505. }
  5506. wpa_hexdump_key(MSG_DEBUG, "DPP: ECDH shared secret (K.x)",
  5507. Kx, Kx_len);
  5508. /* z = HKDF(<>, MAC-Initiator | MAC-Responder | M.x | N.x | code, K.x)
  5509. */
  5510. res = dpp_pkex_derive_z(pkex->peer_mac, pkex->own_mac,
  5511. pkex->Mx, curve->prime_len,
  5512. pkex->Nx, curve->prime_len, pkex->code,
  5513. Kx, Kx_len, pkex->z, curve->hash_len);
  5514. os_memset(Kx, 0, Kx_len);
  5515. if (res < 0)
  5516. goto fail;
  5517. pkex->exchange_done = 1;
  5518. out:
  5519. EVP_PKEY_CTX_free(ctx);
  5520. BN_CTX_free(bnctx);
  5521. EC_POINT_free(Qi);
  5522. EC_POINT_free(Qr);
  5523. BN_free(Mx);
  5524. BN_free(My);
  5525. BN_free(Nx);
  5526. BN_free(Ny);
  5527. EC_POINT_free(M);
  5528. EC_POINT_free(N);
  5529. EC_POINT_free(X);
  5530. EC_KEY_free(X_ec);
  5531. EC_KEY_free(Y_ec);
  5532. return pkex;
  5533. fail:
  5534. wpa_printf(MSG_DEBUG, "DPP: PKEX Exchange Request processing failed");
  5535. dpp_pkex_free(pkex);
  5536. pkex = NULL;
  5537. goto out;
  5538. }
  5539. static struct wpabuf *
  5540. dpp_pkex_build_commit_reveal_req(struct dpp_pkex *pkex,
  5541. const struct wpabuf *A_pub, const u8 *u)
  5542. {
  5543. const struct dpp_curve_params *curve = pkex->own_bi->curve;
  5544. struct wpabuf *msg = NULL;
  5545. size_t clear_len, attr_len;
  5546. struct wpabuf *clear = NULL;
  5547. u8 *wrapped;
  5548. u8 octet;
  5549. const u8 *addr[2];
  5550. size_t len[2];
  5551. /* {A, u, [bootstrapping info]}z */
  5552. clear_len = 4 + 2 * curve->prime_len + 4 + curve->hash_len;
  5553. clear = wpabuf_alloc(clear_len);
  5554. attr_len = 4 + clear_len + AES_BLOCK_SIZE;
  5555. #ifdef CONFIG_TESTING_OPTIONS
  5556. if (dpp_test == DPP_TEST_AFTER_WRAPPED_DATA_PKEX_CR_REQ)
  5557. attr_len += 4;
  5558. #endif /* CONFIG_TESTING_OPTIONS */
  5559. msg = dpp_alloc_msg(DPP_PA_PKEX_COMMIT_REVEAL_REQ, attr_len);
  5560. if (!clear || !msg)
  5561. goto fail;
  5562. #ifdef CONFIG_TESTING_OPTIONS
  5563. if (dpp_test == DPP_TEST_NO_BOOTSTRAP_KEY_PKEX_CR_REQ) {
  5564. wpa_printf(MSG_INFO, "DPP: TESTING - no Bootstrap Key");
  5565. goto skip_bootstrap_key;
  5566. }
  5567. if (dpp_test == DPP_TEST_INVALID_BOOTSTRAP_KEY_PKEX_CR_REQ) {
  5568. wpa_printf(MSG_INFO, "DPP: TESTING - invalid Bootstrap Key");
  5569. wpabuf_put_le16(clear, DPP_ATTR_BOOTSTRAP_KEY);
  5570. wpabuf_put_le16(clear, 2 * curve->prime_len);
  5571. if (dpp_test_gen_invalid_key(clear, curve) < 0)
  5572. goto fail;
  5573. goto skip_bootstrap_key;
  5574. }
  5575. #endif /* CONFIG_TESTING_OPTIONS */
  5576. /* A in Bootstrap Key attribute */
  5577. wpabuf_put_le16(clear, DPP_ATTR_BOOTSTRAP_KEY);
  5578. wpabuf_put_le16(clear, wpabuf_len(A_pub));
  5579. wpabuf_put_buf(clear, A_pub);
  5580. #ifdef CONFIG_TESTING_OPTIONS
  5581. skip_bootstrap_key:
  5582. if (dpp_test == DPP_TEST_NO_I_AUTH_TAG_PKEX_CR_REQ) {
  5583. wpa_printf(MSG_INFO, "DPP: TESTING - no I-Auth tag");
  5584. goto skip_i_auth_tag;
  5585. }
  5586. if (dpp_test == DPP_TEST_I_AUTH_TAG_MISMATCH_PKEX_CR_REQ) {
  5587. wpa_printf(MSG_INFO, "DPP: TESTING - I-Auth tag mismatch");
  5588. wpabuf_put_le16(clear, DPP_ATTR_I_AUTH_TAG);
  5589. wpabuf_put_le16(clear, curve->hash_len);
  5590. wpabuf_put_data(clear, u, curve->hash_len - 1);
  5591. wpabuf_put_u8(clear, u[curve->hash_len - 1] ^ 0x01);
  5592. goto skip_i_auth_tag;
  5593. }
  5594. #endif /* CONFIG_TESTING_OPTIONS */
  5595. /* u in I-Auth tag attribute */
  5596. wpabuf_put_le16(clear, DPP_ATTR_I_AUTH_TAG);
  5597. wpabuf_put_le16(clear, curve->hash_len);
  5598. wpabuf_put_data(clear, u, curve->hash_len);
  5599. #ifdef CONFIG_TESTING_OPTIONS
  5600. skip_i_auth_tag:
  5601. if (dpp_test == DPP_TEST_NO_WRAPPED_DATA_PKEX_CR_REQ) {
  5602. wpa_printf(MSG_INFO, "DPP: TESTING - no Wrapped Data");
  5603. goto skip_wrapped_data;
  5604. }
  5605. #endif /* CONFIG_TESTING_OPTIONS */
  5606. addr[0] = wpabuf_head_u8(msg) + 2;
  5607. len[0] = DPP_HDR_LEN;
  5608. octet = 0;
  5609. addr[1] = &octet;
  5610. len[1] = sizeof(octet);
  5611. wpa_hexdump(MSG_DEBUG, "DDP: AES-SIV AD[0]", addr[0], len[0]);
  5612. wpa_hexdump(MSG_DEBUG, "DDP: AES-SIV AD[1]", addr[1], len[1]);
  5613. wpabuf_put_le16(msg, DPP_ATTR_WRAPPED_DATA);
  5614. wpabuf_put_le16(msg, wpabuf_len(clear) + AES_BLOCK_SIZE);
  5615. wrapped = wpabuf_put(msg, wpabuf_len(clear) + AES_BLOCK_SIZE);
  5616. wpa_hexdump_buf(MSG_DEBUG, "DPP: AES-SIV cleartext", clear);
  5617. if (aes_siv_encrypt(pkex->z, curve->hash_len,
  5618. wpabuf_head(clear), wpabuf_len(clear),
  5619. 2, addr, len, wrapped) < 0)
  5620. goto fail;
  5621. wpa_hexdump(MSG_DEBUG, "DPP: AES-SIV ciphertext",
  5622. wrapped, wpabuf_len(clear) + AES_BLOCK_SIZE);
  5623. #ifdef CONFIG_TESTING_OPTIONS
  5624. if (dpp_test == DPP_TEST_AFTER_WRAPPED_DATA_PKEX_CR_REQ) {
  5625. wpa_printf(MSG_INFO, "DPP: TESTING - attr after Wrapped Data");
  5626. wpabuf_put_le16(msg, DPP_ATTR_TESTING);
  5627. wpabuf_put_le16(msg, 0);
  5628. }
  5629. skip_wrapped_data:
  5630. #endif /* CONFIG_TESTING_OPTIONS */
  5631. out:
  5632. wpabuf_free(clear);
  5633. return msg;
  5634. fail:
  5635. wpabuf_free(msg);
  5636. msg = NULL;
  5637. goto out;
  5638. }
  5639. struct wpabuf * dpp_pkex_rx_exchange_resp(struct dpp_pkex *pkex,
  5640. const u8 *buf, size_t buflen)
  5641. {
  5642. const u8 *attr_status, *attr_id, *attr_key, *attr_group;
  5643. u16 attr_status_len, attr_id_len, attr_key_len, attr_group_len;
  5644. const EC_GROUP *group;
  5645. BN_CTX *bnctx = NULL;
  5646. struct wpabuf *msg = NULL, *A_pub = NULL, *X_pub = NULL, *Y_pub = NULL;
  5647. const struct dpp_curve_params *curve = pkex->own_bi->curve;
  5648. EC_POINT *Qr = NULL, *Y = NULL, *N = NULL;
  5649. BIGNUM *Nx = NULL, *Ny = NULL;
  5650. EVP_PKEY_CTX *ctx = NULL;
  5651. EC_KEY *Y_ec = NULL;
  5652. size_t Jx_len, Kx_len;
  5653. u8 Jx[DPP_MAX_SHARED_SECRET_LEN], Kx[DPP_MAX_SHARED_SECRET_LEN];
  5654. const u8 *addr[4];
  5655. size_t len[4];
  5656. u8 u[DPP_MAX_HASH_LEN];
  5657. int res;
  5658. if (pkex->failed || pkex->t >= PKEX_COUNTER_T_LIMIT || !pkex->initiator)
  5659. return NULL;
  5660. attr_status = dpp_get_attr(buf, buflen, DPP_ATTR_STATUS,
  5661. &attr_status_len);
  5662. if (!attr_status || attr_status_len != 1) {
  5663. dpp_pkex_fail(pkex, "No DPP Status attribute");
  5664. return NULL;
  5665. }
  5666. wpa_printf(MSG_DEBUG, "DPP: Status %u", attr_status[0]);
  5667. if (attr_status[0] == DPP_STATUS_BAD_GROUP) {
  5668. attr_group = dpp_get_attr(buf, buflen,
  5669. DPP_ATTR_FINITE_CYCLIC_GROUP,
  5670. &attr_group_len);
  5671. if (attr_group && attr_group_len == 2) {
  5672. wpa_msg(pkex->msg_ctx, MSG_INFO, DPP_EVENT_FAIL
  5673. "Peer indicated mismatching PKEX group - proposed %u",
  5674. WPA_GET_LE16(attr_group));
  5675. return NULL;
  5676. }
  5677. }
  5678. if (attr_status[0] != DPP_STATUS_OK) {
  5679. dpp_pkex_fail(pkex, "PKEX failed (peer indicated failure)");
  5680. return NULL;
  5681. }
  5682. attr_id = dpp_get_attr(buf, buflen, DPP_ATTR_CODE_IDENTIFIER,
  5683. &attr_id_len);
  5684. if (!attr_id && pkex->identifier) {
  5685. wpa_printf(MSG_DEBUG,
  5686. "DPP: No PKEX code identifier received, but expected one");
  5687. return NULL;
  5688. }
  5689. if (attr_id && pkex->identifier &&
  5690. (os_strlen(pkex->identifier) != attr_id_len ||
  5691. os_memcmp(pkex->identifier, attr_id, attr_id_len) != 0)) {
  5692. dpp_pkex_fail(pkex, "PKEX code identifier mismatch");
  5693. return NULL;
  5694. }
  5695. /* N in Encrypted Key attribute */
  5696. attr_key = dpp_get_attr(buf, buflen, DPP_ATTR_ENCRYPTED_KEY,
  5697. &attr_key_len);
  5698. if (!attr_key || attr_key_len & 0x01 || attr_key_len < 2) {
  5699. dpp_pkex_fail(pkex, "Missing Encrypted Key attribute");
  5700. return NULL;
  5701. }
  5702. /* Qr = H(MAC-Responder | [identifier |] code) * Pr */
  5703. bnctx = BN_CTX_new();
  5704. if (!bnctx)
  5705. goto fail;
  5706. Qr = dpp_pkex_derive_Qr(curve, pkex->peer_mac, pkex->code,
  5707. pkex->identifier, bnctx, &group);
  5708. if (!Qr)
  5709. goto fail;
  5710. /* Y' = N - Qr */
  5711. Y = EC_POINT_new(group);
  5712. N = EC_POINT_new(group);
  5713. Nx = BN_bin2bn(attr_key, attr_key_len / 2, NULL);
  5714. Ny = BN_bin2bn(attr_key + attr_key_len / 2, attr_key_len / 2, NULL);
  5715. if (!Y || !N || !Nx || !Ny ||
  5716. EC_POINT_set_affine_coordinates_GFp(group, N, Nx, Ny, bnctx) != 1 ||
  5717. EC_POINT_is_at_infinity(group, N) ||
  5718. !EC_POINT_is_on_curve(group, N, bnctx) ||
  5719. EC_POINT_invert(group, Qr, bnctx) != 1 ||
  5720. EC_POINT_add(group, Y, N, Qr, bnctx) != 1 ||
  5721. EC_POINT_is_at_infinity(group, Y) ||
  5722. !EC_POINT_is_on_curve(group, Y, bnctx)) {
  5723. dpp_pkex_fail(pkex, "Invalid Encrypted Key value");
  5724. pkex->t++;
  5725. goto fail;
  5726. }
  5727. pkex->exchange_done = 1;
  5728. /* ECDH: J = a * Y’ */
  5729. Y_ec = EC_KEY_new();
  5730. if (!Y_ec ||
  5731. EC_KEY_set_group(Y_ec, group) != 1 ||
  5732. EC_KEY_set_public_key(Y_ec, Y) != 1)
  5733. goto fail;
  5734. pkex->y = EVP_PKEY_new();
  5735. if (!pkex->y ||
  5736. EVP_PKEY_set1_EC_KEY(pkex->y, Y_ec) != 1)
  5737. goto fail;
  5738. ctx = EVP_PKEY_CTX_new(pkex->own_bi->pubkey, NULL);
  5739. if (!ctx ||
  5740. EVP_PKEY_derive_init(ctx) != 1 ||
  5741. EVP_PKEY_derive_set_peer(ctx, pkex->y) != 1 ||
  5742. EVP_PKEY_derive(ctx, NULL, &Jx_len) != 1 ||
  5743. Jx_len > DPP_MAX_SHARED_SECRET_LEN ||
  5744. EVP_PKEY_derive(ctx, Jx, &Jx_len) != 1) {
  5745. wpa_printf(MSG_ERROR,
  5746. "DPP: Failed to derive ECDH shared secret: %s",
  5747. ERR_error_string(ERR_get_error(), NULL));
  5748. goto fail;
  5749. }
  5750. wpa_hexdump_key(MSG_DEBUG, "DPP: ECDH shared secret (J.x)",
  5751. Jx, Jx_len);
  5752. /* u = HMAC(J.x, MAC-Initiator | A.x | Y’.x | X.x ) */
  5753. A_pub = dpp_get_pubkey_point(pkex->own_bi->pubkey, 0);
  5754. Y_pub = dpp_get_pubkey_point(pkex->y, 0);
  5755. X_pub = dpp_get_pubkey_point(pkex->x, 0);
  5756. if (!A_pub || !Y_pub || !X_pub)
  5757. goto fail;
  5758. addr[0] = pkex->own_mac;
  5759. len[0] = ETH_ALEN;
  5760. addr[1] = wpabuf_head(A_pub);
  5761. len[1] = wpabuf_len(A_pub) / 2;
  5762. addr[2] = wpabuf_head(Y_pub);
  5763. len[2] = wpabuf_len(Y_pub) / 2;
  5764. addr[3] = wpabuf_head(X_pub);
  5765. len[3] = wpabuf_len(X_pub) / 2;
  5766. if (dpp_hmac_vector(curve->hash_len, Jx, Jx_len, 4, addr, len, u) < 0)
  5767. goto fail;
  5768. wpa_hexdump(MSG_DEBUG, "DPP: u", u, curve->hash_len);
  5769. /* K = x * Y’ */
  5770. EVP_PKEY_CTX_free(ctx);
  5771. ctx = EVP_PKEY_CTX_new(pkex->x, NULL);
  5772. if (!ctx ||
  5773. EVP_PKEY_derive_init(ctx) != 1 ||
  5774. EVP_PKEY_derive_set_peer(ctx, pkex->y) != 1 ||
  5775. EVP_PKEY_derive(ctx, NULL, &Kx_len) != 1 ||
  5776. Kx_len > DPP_MAX_SHARED_SECRET_LEN ||
  5777. EVP_PKEY_derive(ctx, Kx, &Kx_len) != 1) {
  5778. wpa_printf(MSG_ERROR,
  5779. "DPP: Failed to derive ECDH shared secret: %s",
  5780. ERR_error_string(ERR_get_error(), NULL));
  5781. goto fail;
  5782. }
  5783. wpa_hexdump_key(MSG_DEBUG, "DPP: ECDH shared secret (K.x)",
  5784. Kx, Kx_len);
  5785. /* z = HKDF(<>, MAC-Initiator | MAC-Responder | M.x | N.x | code, K.x)
  5786. */
  5787. res = dpp_pkex_derive_z(pkex->own_mac, pkex->peer_mac,
  5788. pkex->Mx, curve->prime_len,
  5789. attr_key /* N.x */, attr_key_len / 2,
  5790. pkex->code, Kx, Kx_len,
  5791. pkex->z, curve->hash_len);
  5792. os_memset(Kx, 0, Kx_len);
  5793. if (res < 0)
  5794. goto fail;
  5795. msg = dpp_pkex_build_commit_reveal_req(pkex, A_pub, u);
  5796. if (!msg)
  5797. goto fail;
  5798. out:
  5799. wpabuf_free(A_pub);
  5800. wpabuf_free(X_pub);
  5801. wpabuf_free(Y_pub);
  5802. EC_POINT_free(Qr);
  5803. EC_POINT_free(Y);
  5804. EC_POINT_free(N);
  5805. BN_free(Nx);
  5806. BN_free(Ny);
  5807. EC_KEY_free(Y_ec);
  5808. EVP_PKEY_CTX_free(ctx);
  5809. BN_CTX_free(bnctx);
  5810. return msg;
  5811. fail:
  5812. wpa_printf(MSG_DEBUG, "DPP: PKEX Exchange Response processing failed");
  5813. goto out;
  5814. }
  5815. static struct wpabuf *
  5816. dpp_pkex_build_commit_reveal_resp(struct dpp_pkex *pkex,
  5817. const struct wpabuf *B_pub, const u8 *v)
  5818. {
  5819. const struct dpp_curve_params *curve = pkex->own_bi->curve;
  5820. struct wpabuf *msg = NULL;
  5821. const u8 *addr[2];
  5822. size_t len[2];
  5823. u8 octet;
  5824. u8 *wrapped;
  5825. struct wpabuf *clear = NULL;
  5826. size_t clear_len, attr_len;
  5827. /* {B, v [bootstrapping info]}z */
  5828. clear_len = 4 + 2 * curve->prime_len + 4 + curve->hash_len;
  5829. clear = wpabuf_alloc(clear_len);
  5830. attr_len = 4 + clear_len + AES_BLOCK_SIZE;
  5831. #ifdef CONFIG_TESTING_OPTIONS
  5832. if (dpp_test == DPP_TEST_AFTER_WRAPPED_DATA_PKEX_CR_RESP)
  5833. attr_len += 4;
  5834. #endif /* CONFIG_TESTING_OPTIONS */
  5835. msg = dpp_alloc_msg(DPP_PA_PKEX_COMMIT_REVEAL_RESP, attr_len);
  5836. if (!clear || !msg)
  5837. goto fail;
  5838. #ifdef CONFIG_TESTING_OPTIONS
  5839. if (dpp_test == DPP_TEST_NO_BOOTSTRAP_KEY_PKEX_CR_RESP) {
  5840. wpa_printf(MSG_INFO, "DPP: TESTING - no Bootstrap Key");
  5841. goto skip_bootstrap_key;
  5842. }
  5843. if (dpp_test == DPP_TEST_INVALID_BOOTSTRAP_KEY_PKEX_CR_RESP) {
  5844. wpa_printf(MSG_INFO, "DPP: TESTING - invalid Bootstrap Key");
  5845. wpabuf_put_le16(clear, DPP_ATTR_BOOTSTRAP_KEY);
  5846. wpabuf_put_le16(clear, 2 * curve->prime_len);
  5847. if (dpp_test_gen_invalid_key(clear, curve) < 0)
  5848. goto fail;
  5849. goto skip_bootstrap_key;
  5850. }
  5851. #endif /* CONFIG_TESTING_OPTIONS */
  5852. /* B in Bootstrap Key attribute */
  5853. wpabuf_put_le16(clear, DPP_ATTR_BOOTSTRAP_KEY);
  5854. wpabuf_put_le16(clear, wpabuf_len(B_pub));
  5855. wpabuf_put_buf(clear, B_pub);
  5856. #ifdef CONFIG_TESTING_OPTIONS
  5857. skip_bootstrap_key:
  5858. if (dpp_test == DPP_TEST_NO_R_AUTH_TAG_PKEX_CR_RESP) {
  5859. wpa_printf(MSG_INFO, "DPP: TESTING - no R-Auth tag");
  5860. goto skip_r_auth_tag;
  5861. }
  5862. if (dpp_test == DPP_TEST_R_AUTH_TAG_MISMATCH_PKEX_CR_RESP) {
  5863. wpa_printf(MSG_INFO, "DPP: TESTING - R-Auth tag mismatch");
  5864. wpabuf_put_le16(clear, DPP_ATTR_R_AUTH_TAG);
  5865. wpabuf_put_le16(clear, curve->hash_len);
  5866. wpabuf_put_data(clear, v, curve->hash_len - 1);
  5867. wpabuf_put_u8(clear, v[curve->hash_len - 1] ^ 0x01);
  5868. goto skip_r_auth_tag;
  5869. }
  5870. #endif /* CONFIG_TESTING_OPTIONS */
  5871. /* v in R-Auth tag attribute */
  5872. wpabuf_put_le16(clear, DPP_ATTR_R_AUTH_TAG);
  5873. wpabuf_put_le16(clear, curve->hash_len);
  5874. wpabuf_put_data(clear, v, curve->hash_len);
  5875. #ifdef CONFIG_TESTING_OPTIONS
  5876. skip_r_auth_tag:
  5877. if (dpp_test == DPP_TEST_NO_WRAPPED_DATA_PKEX_CR_RESP) {
  5878. wpa_printf(MSG_INFO, "DPP: TESTING - no Wrapped Data");
  5879. goto skip_wrapped_data;
  5880. }
  5881. #endif /* CONFIG_TESTING_OPTIONS */
  5882. addr[0] = wpabuf_head_u8(msg) + 2;
  5883. len[0] = DPP_HDR_LEN;
  5884. octet = 1;
  5885. addr[1] = &octet;
  5886. len[1] = sizeof(octet);
  5887. wpa_hexdump(MSG_DEBUG, "DDP: AES-SIV AD[0]", addr[0], len[0]);
  5888. wpa_hexdump(MSG_DEBUG, "DDP: AES-SIV AD[1]", addr[1], len[1]);
  5889. wpabuf_put_le16(msg, DPP_ATTR_WRAPPED_DATA);
  5890. wpabuf_put_le16(msg, wpabuf_len(clear) + AES_BLOCK_SIZE);
  5891. wrapped = wpabuf_put(msg, wpabuf_len(clear) + AES_BLOCK_SIZE);
  5892. wpa_hexdump_buf(MSG_DEBUG, "DPP: AES-SIV cleartext", clear);
  5893. if (aes_siv_encrypt(pkex->z, curve->hash_len,
  5894. wpabuf_head(clear), wpabuf_len(clear),
  5895. 2, addr, len, wrapped) < 0)
  5896. goto fail;
  5897. wpa_hexdump(MSG_DEBUG, "DPP: AES-SIV ciphertext",
  5898. wrapped, wpabuf_len(clear) + AES_BLOCK_SIZE);
  5899. #ifdef CONFIG_TESTING_OPTIONS
  5900. if (dpp_test == DPP_TEST_AFTER_WRAPPED_DATA_PKEX_CR_RESP) {
  5901. wpa_printf(MSG_INFO, "DPP: TESTING - attr after Wrapped Data");
  5902. wpabuf_put_le16(msg, DPP_ATTR_TESTING);
  5903. wpabuf_put_le16(msg, 0);
  5904. }
  5905. skip_wrapped_data:
  5906. #endif /* CONFIG_TESTING_OPTIONS */
  5907. out:
  5908. wpabuf_free(clear);
  5909. return msg;
  5910. fail:
  5911. wpabuf_free(msg);
  5912. msg = NULL;
  5913. goto out;
  5914. }
  5915. struct wpabuf * dpp_pkex_rx_commit_reveal_req(struct dpp_pkex *pkex,
  5916. const u8 *hdr,
  5917. const u8 *buf, size_t buflen)
  5918. {
  5919. const struct dpp_curve_params *curve = pkex->own_bi->curve;
  5920. EVP_PKEY_CTX *ctx = NULL;
  5921. size_t Jx_len, Lx_len;
  5922. u8 Jx[DPP_MAX_SHARED_SECRET_LEN];
  5923. u8 Lx[DPP_MAX_SHARED_SECRET_LEN];
  5924. const u8 *wrapped_data, *b_key, *peer_u;
  5925. u16 wrapped_data_len, b_key_len, peer_u_len = 0;
  5926. const u8 *addr[4];
  5927. size_t len[4];
  5928. u8 octet;
  5929. u8 *unwrapped = NULL;
  5930. size_t unwrapped_len = 0;
  5931. struct wpabuf *msg = NULL, *A_pub = NULL, *X_pub = NULL, *Y_pub = NULL;
  5932. struct wpabuf *B_pub = NULL;
  5933. u8 u[DPP_MAX_HASH_LEN], v[DPP_MAX_HASH_LEN];
  5934. if (!pkex->exchange_done || pkex->failed ||
  5935. pkex->t >= PKEX_COUNTER_T_LIMIT || pkex->initiator)
  5936. goto fail;
  5937. wrapped_data = dpp_get_attr(buf, buflen, DPP_ATTR_WRAPPED_DATA,
  5938. &wrapped_data_len);
  5939. if (!wrapped_data || wrapped_data_len < AES_BLOCK_SIZE) {
  5940. dpp_pkex_fail(pkex,
  5941. "Missing or invalid required Wrapped Data attribute");
  5942. goto fail;
  5943. }
  5944. wpa_hexdump(MSG_DEBUG, "DPP: AES-SIV ciphertext",
  5945. wrapped_data, wrapped_data_len);
  5946. unwrapped_len = wrapped_data_len - AES_BLOCK_SIZE;
  5947. unwrapped = os_malloc(unwrapped_len);
  5948. if (!unwrapped)
  5949. goto fail;
  5950. addr[0] = hdr;
  5951. len[0] = DPP_HDR_LEN;
  5952. octet = 0;
  5953. addr[1] = &octet;
  5954. len[1] = sizeof(octet);
  5955. wpa_hexdump(MSG_DEBUG, "DDP: AES-SIV AD[0]", addr[0], len[0]);
  5956. wpa_hexdump(MSG_DEBUG, "DDP: AES-SIV AD[1]", addr[1], len[1]);
  5957. if (aes_siv_decrypt(pkex->z, curve->hash_len,
  5958. wrapped_data, wrapped_data_len,
  5959. 2, addr, len, unwrapped) < 0) {
  5960. dpp_pkex_fail(pkex,
  5961. "AES-SIV decryption failed - possible PKEX code mismatch");
  5962. pkex->failed = 1;
  5963. pkex->t++;
  5964. goto fail;
  5965. }
  5966. wpa_hexdump(MSG_DEBUG, "DPP: AES-SIV cleartext",
  5967. unwrapped, unwrapped_len);
  5968. if (dpp_check_attrs(unwrapped, unwrapped_len) < 0) {
  5969. dpp_pkex_fail(pkex, "Invalid attribute in unwrapped data");
  5970. goto fail;
  5971. }
  5972. b_key = dpp_get_attr(unwrapped, unwrapped_len, DPP_ATTR_BOOTSTRAP_KEY,
  5973. &b_key_len);
  5974. if (!b_key || b_key_len != 2 * curve->prime_len) {
  5975. dpp_pkex_fail(pkex, "No valid peer bootstrapping key found");
  5976. goto fail;
  5977. }
  5978. pkex->peer_bootstrap_key = dpp_set_pubkey_point(pkex->x, b_key,
  5979. b_key_len);
  5980. if (!pkex->peer_bootstrap_key) {
  5981. dpp_pkex_fail(pkex, "Peer bootstrapping key is invalid");
  5982. goto fail;
  5983. }
  5984. dpp_debug_print_key("DPP: Peer bootstrap public key",
  5985. pkex->peer_bootstrap_key);
  5986. /* ECDH: J' = y * A' */
  5987. ctx = EVP_PKEY_CTX_new(pkex->y, NULL);
  5988. if (!ctx ||
  5989. EVP_PKEY_derive_init(ctx) != 1 ||
  5990. EVP_PKEY_derive_set_peer(ctx, pkex->peer_bootstrap_key) != 1 ||
  5991. EVP_PKEY_derive(ctx, NULL, &Jx_len) != 1 ||
  5992. Jx_len > DPP_MAX_SHARED_SECRET_LEN ||
  5993. EVP_PKEY_derive(ctx, Jx, &Jx_len) != 1) {
  5994. wpa_printf(MSG_ERROR,
  5995. "DPP: Failed to derive ECDH shared secret: %s",
  5996. ERR_error_string(ERR_get_error(), NULL));
  5997. goto fail;
  5998. }
  5999. wpa_hexdump_key(MSG_DEBUG, "DPP: ECDH shared secret (J.x)",
  6000. Jx, Jx_len);
  6001. /* u' = HMAC(J'.x, MAC-Initiator | A'.x | Y.x | X'.x) */
  6002. A_pub = dpp_get_pubkey_point(pkex->peer_bootstrap_key, 0);
  6003. Y_pub = dpp_get_pubkey_point(pkex->y, 0);
  6004. X_pub = dpp_get_pubkey_point(pkex->x, 0);
  6005. if (!A_pub || !Y_pub || !X_pub)
  6006. goto fail;
  6007. addr[0] = pkex->peer_mac;
  6008. len[0] = ETH_ALEN;
  6009. addr[1] = wpabuf_head(A_pub);
  6010. len[1] = wpabuf_len(A_pub) / 2;
  6011. addr[2] = wpabuf_head(Y_pub);
  6012. len[2] = wpabuf_len(Y_pub) / 2;
  6013. addr[3] = wpabuf_head(X_pub);
  6014. len[3] = wpabuf_len(X_pub) / 2;
  6015. if (dpp_hmac_vector(curve->hash_len, Jx, Jx_len, 4, addr, len, u) < 0)
  6016. goto fail;
  6017. peer_u = dpp_get_attr(unwrapped, unwrapped_len, DPP_ATTR_I_AUTH_TAG,
  6018. &peer_u_len);
  6019. if (!peer_u || peer_u_len != curve->hash_len ||
  6020. os_memcmp(peer_u, u, curve->hash_len) != 0) {
  6021. dpp_pkex_fail(pkex, "No valid u (I-Auth tag) found");
  6022. wpa_hexdump(MSG_DEBUG, "DPP: Calculated u'",
  6023. u, curve->hash_len);
  6024. wpa_hexdump(MSG_DEBUG, "DPP: Received u", peer_u, peer_u_len);
  6025. pkex->t++;
  6026. goto fail;
  6027. }
  6028. wpa_printf(MSG_DEBUG, "DPP: Valid u (I-Auth tag) received");
  6029. /* ECDH: L = b * X' */
  6030. EVP_PKEY_CTX_free(ctx);
  6031. ctx = EVP_PKEY_CTX_new(pkex->own_bi->pubkey, NULL);
  6032. if (!ctx ||
  6033. EVP_PKEY_derive_init(ctx) != 1 ||
  6034. EVP_PKEY_derive_set_peer(ctx, pkex->x) != 1 ||
  6035. EVP_PKEY_derive(ctx, NULL, &Lx_len) != 1 ||
  6036. Lx_len > DPP_MAX_SHARED_SECRET_LEN ||
  6037. EVP_PKEY_derive(ctx, Lx, &Lx_len) != 1) {
  6038. wpa_printf(MSG_ERROR,
  6039. "DPP: Failed to derive ECDH shared secret: %s",
  6040. ERR_error_string(ERR_get_error(), NULL));
  6041. goto fail;
  6042. }
  6043. wpa_hexdump_key(MSG_DEBUG, "DPP: ECDH shared secret (L.x)",
  6044. Lx, Lx_len);
  6045. /* v = HMAC(L.x, MAC-Responder | B.x | X'.x | Y.x) */
  6046. B_pub = dpp_get_pubkey_point(pkex->own_bi->pubkey, 0);
  6047. if (!B_pub)
  6048. goto fail;
  6049. addr[0] = pkex->own_mac;
  6050. len[0] = ETH_ALEN;
  6051. addr[1] = wpabuf_head(B_pub);
  6052. len[1] = wpabuf_len(B_pub) / 2;
  6053. addr[2] = wpabuf_head(X_pub);
  6054. len[2] = wpabuf_len(X_pub) / 2;
  6055. addr[3] = wpabuf_head(Y_pub);
  6056. len[3] = wpabuf_len(Y_pub) / 2;
  6057. if (dpp_hmac_vector(curve->hash_len, Lx, Lx_len, 4, addr, len, v) < 0)
  6058. goto fail;
  6059. wpa_hexdump(MSG_DEBUG, "DPP: v", v, curve->hash_len);
  6060. msg = dpp_pkex_build_commit_reveal_resp(pkex, B_pub, v);
  6061. if (!msg)
  6062. goto fail;
  6063. out:
  6064. EVP_PKEY_CTX_free(ctx);
  6065. os_free(unwrapped);
  6066. wpabuf_free(A_pub);
  6067. wpabuf_free(B_pub);
  6068. wpabuf_free(X_pub);
  6069. wpabuf_free(Y_pub);
  6070. return msg;
  6071. fail:
  6072. wpa_printf(MSG_DEBUG,
  6073. "DPP: PKEX Commit-Reveal Request processing failed");
  6074. goto out;
  6075. }
  6076. int dpp_pkex_rx_commit_reveal_resp(struct dpp_pkex *pkex, const u8 *hdr,
  6077. const u8 *buf, size_t buflen)
  6078. {
  6079. const struct dpp_curve_params *curve = pkex->own_bi->curve;
  6080. const u8 *wrapped_data, *b_key, *peer_v;
  6081. u16 wrapped_data_len, b_key_len, peer_v_len = 0;
  6082. const u8 *addr[4];
  6083. size_t len[4];
  6084. u8 octet;
  6085. u8 *unwrapped = NULL;
  6086. size_t unwrapped_len = 0;
  6087. int ret = -1;
  6088. u8 v[DPP_MAX_HASH_LEN];
  6089. size_t Lx_len;
  6090. u8 Lx[DPP_MAX_SHARED_SECRET_LEN];
  6091. EVP_PKEY_CTX *ctx = NULL;
  6092. struct wpabuf *B_pub = NULL, *X_pub = NULL, *Y_pub = NULL;
  6093. if (!pkex->exchange_done || pkex->failed ||
  6094. pkex->t >= PKEX_COUNTER_T_LIMIT || !pkex->initiator)
  6095. goto fail;
  6096. wrapped_data = dpp_get_attr(buf, buflen, DPP_ATTR_WRAPPED_DATA,
  6097. &wrapped_data_len);
  6098. if (!wrapped_data || wrapped_data_len < AES_BLOCK_SIZE) {
  6099. dpp_pkex_fail(pkex,
  6100. "Missing or invalid required Wrapped Data attribute");
  6101. goto fail;
  6102. }
  6103. wpa_hexdump(MSG_DEBUG, "DPP: AES-SIV ciphertext",
  6104. wrapped_data, wrapped_data_len);
  6105. unwrapped_len = wrapped_data_len - AES_BLOCK_SIZE;
  6106. unwrapped = os_malloc(unwrapped_len);
  6107. if (!unwrapped)
  6108. goto fail;
  6109. addr[0] = hdr;
  6110. len[0] = DPP_HDR_LEN;
  6111. octet = 1;
  6112. addr[1] = &octet;
  6113. len[1] = sizeof(octet);
  6114. wpa_hexdump(MSG_DEBUG, "DDP: AES-SIV AD[0]", addr[0], len[0]);
  6115. wpa_hexdump(MSG_DEBUG, "DDP: AES-SIV AD[1]", addr[1], len[1]);
  6116. if (aes_siv_decrypt(pkex->z, curve->hash_len,
  6117. wrapped_data, wrapped_data_len,
  6118. 2, addr, len, unwrapped) < 0) {
  6119. dpp_pkex_fail(pkex,
  6120. "AES-SIV decryption failed - possible PKEX code mismatch");
  6121. pkex->t++;
  6122. goto fail;
  6123. }
  6124. wpa_hexdump(MSG_DEBUG, "DPP: AES-SIV cleartext",
  6125. unwrapped, unwrapped_len);
  6126. if (dpp_check_attrs(unwrapped, unwrapped_len) < 0) {
  6127. dpp_pkex_fail(pkex, "Invalid attribute in unwrapped data");
  6128. goto fail;
  6129. }
  6130. b_key = dpp_get_attr(unwrapped, unwrapped_len, DPP_ATTR_BOOTSTRAP_KEY,
  6131. &b_key_len);
  6132. if (!b_key || b_key_len != 2 * curve->prime_len) {
  6133. dpp_pkex_fail(pkex, "No valid peer bootstrapping key found");
  6134. goto fail;
  6135. }
  6136. pkex->peer_bootstrap_key = dpp_set_pubkey_point(pkex->x, b_key,
  6137. b_key_len);
  6138. if (!pkex->peer_bootstrap_key) {
  6139. dpp_pkex_fail(pkex, "Peer bootstrapping key is invalid");
  6140. goto fail;
  6141. }
  6142. dpp_debug_print_key("DPP: Peer bootstrap public key",
  6143. pkex->peer_bootstrap_key);
  6144. /* ECDH: L' = x * B' */
  6145. ctx = EVP_PKEY_CTX_new(pkex->x, NULL);
  6146. if (!ctx ||
  6147. EVP_PKEY_derive_init(ctx) != 1 ||
  6148. EVP_PKEY_derive_set_peer(ctx, pkex->peer_bootstrap_key) != 1 ||
  6149. EVP_PKEY_derive(ctx, NULL, &Lx_len) != 1 ||
  6150. Lx_len > DPP_MAX_SHARED_SECRET_LEN ||
  6151. EVP_PKEY_derive(ctx, Lx, &Lx_len) != 1) {
  6152. wpa_printf(MSG_ERROR,
  6153. "DPP: Failed to derive ECDH shared secret: %s",
  6154. ERR_error_string(ERR_get_error(), NULL));
  6155. goto fail;
  6156. }
  6157. wpa_hexdump_key(MSG_DEBUG, "DPP: ECDH shared secret (L.x)",
  6158. Lx, Lx_len);
  6159. /* v' = HMAC(L.x, MAC-Responder | B'.x | X.x | Y'.x) */
  6160. B_pub = dpp_get_pubkey_point(pkex->peer_bootstrap_key, 0);
  6161. X_pub = dpp_get_pubkey_point(pkex->x, 0);
  6162. Y_pub = dpp_get_pubkey_point(pkex->y, 0);
  6163. if (!B_pub || !X_pub || !Y_pub)
  6164. goto fail;
  6165. addr[0] = pkex->peer_mac;
  6166. len[0] = ETH_ALEN;
  6167. addr[1] = wpabuf_head(B_pub);
  6168. len[1] = wpabuf_len(B_pub) / 2;
  6169. addr[2] = wpabuf_head(X_pub);
  6170. len[2] = wpabuf_len(X_pub) / 2;
  6171. addr[3] = wpabuf_head(Y_pub);
  6172. len[3] = wpabuf_len(Y_pub) / 2;
  6173. if (dpp_hmac_vector(curve->hash_len, Lx, Lx_len, 4, addr, len, v) < 0)
  6174. goto fail;
  6175. peer_v = dpp_get_attr(unwrapped, unwrapped_len, DPP_ATTR_R_AUTH_TAG,
  6176. &peer_v_len);
  6177. if (!peer_v || peer_v_len != curve->hash_len ||
  6178. os_memcmp(peer_v, v, curve->hash_len) != 0) {
  6179. dpp_pkex_fail(pkex, "No valid v (R-Auth tag) found");
  6180. wpa_hexdump(MSG_DEBUG, "DPP: Calculated v'",
  6181. v, curve->hash_len);
  6182. wpa_hexdump(MSG_DEBUG, "DPP: Received v", peer_v, peer_v_len);
  6183. pkex->t++;
  6184. goto fail;
  6185. }
  6186. wpa_printf(MSG_DEBUG, "DPP: Valid v (R-Auth tag) received");
  6187. ret = 0;
  6188. out:
  6189. wpabuf_free(B_pub);
  6190. wpabuf_free(X_pub);
  6191. wpabuf_free(Y_pub);
  6192. EVP_PKEY_CTX_free(ctx);
  6193. os_free(unwrapped);
  6194. return ret;
  6195. fail:
  6196. goto out;
  6197. }
  6198. void dpp_pkex_free(struct dpp_pkex *pkex)
  6199. {
  6200. if (!pkex)
  6201. return;
  6202. os_free(pkex->identifier);
  6203. os_free(pkex->code);
  6204. EVP_PKEY_free(pkex->x);
  6205. EVP_PKEY_free(pkex->y);
  6206. EVP_PKEY_free(pkex->peer_bootstrap_key);
  6207. wpabuf_free(pkex->exchange_req);
  6208. wpabuf_free(pkex->exchange_resp);
  6209. os_free(pkex);
  6210. }