dpp.c 177 KB

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