dpp.c 159 KB

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