dpp.c 191 KB

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