dpp.c 182 KB

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