dpp.c 167 KB

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