dpp.c 164 KB

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