dpp.c 174 KB

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