tls.c 430 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914
  1. /* tls.c
  2. *
  3. * Copyright (C) 2006-2022 wolfSSL Inc.
  4. *
  5. * This file is part of wolfSSL.
  6. *
  7. * wolfSSL is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * wolfSSL is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
  20. */
  21. #ifdef HAVE_CONFIG_H
  22. #include <config.h>
  23. #endif
  24. #include <wolfssl/wolfcrypt/settings.h>
  25. #ifndef WOLFCRYPT_ONLY
  26. #include <wolfssl/ssl.h>
  27. #include <wolfssl/internal.h>
  28. #include <wolfssl/error-ssl.h>
  29. #include <wolfssl/wolfcrypt/hash.h>
  30. #include <wolfssl/wolfcrypt/hmac.h>
  31. #include <wolfssl/wolfcrypt/kdf.h>
  32. #ifdef NO_INLINE
  33. #include <wolfssl/wolfcrypt/misc.h>
  34. #else
  35. #define WOLFSSL_MISC_INCLUDED
  36. #include <wolfcrypt/src/misc.c>
  37. #endif
  38. #ifdef HAVE_CURVE25519
  39. #include <wolfssl/wolfcrypt/curve25519.h>
  40. #endif
  41. #ifdef HAVE_CURVE448
  42. #include <wolfssl/wolfcrypt/curve448.h>
  43. #endif
  44. #ifdef HAVE_PQC
  45. #ifdef WOLFSSL_HAVE_KYBER
  46. #include <wolfssl/wolfcrypt/kyber.h>
  47. #ifdef WOLFSSL_WC_KYBER
  48. #include <wolfssl/wolfcrypt/wc_kyber.h>
  49. #elif defined(HAVE_LIBOQS)
  50. #include <oqs/kem.h>
  51. #include <wolfssl/wolfcrypt/ext_kyber.h>
  52. #elif defined(HAVE_PQM4)
  53. #include "api_kyber.h"
  54. #define PQM4_PUBLIC_KEY_LENGTH CRYPTO_PUBLICKEYBYTES
  55. #define PQM4_PRIVATE_KEY_LENGTH CRYPTO_SECRETKEYBYTES
  56. #define PQM4_SHARED_SECRET_LENGTH CRYPTO_BYTES
  57. #define PQM4_CIPHERTEXT_LENGTH CRYPTO_CIPHERTEXTBYTES
  58. #include <wolfssl/wolfcrypt/ext_kyber.h>
  59. #endif
  60. #endif
  61. #endif
  62. #if defined(WOLFSSL_RENESAS_TSIP_TLS)
  63. #include <wolfssl/wolfcrypt/port/Renesas/renesas-tsip-crypt.h>
  64. #endif
  65. #ifndef NO_TLS
  66. #if defined(WOLFSSL_TLS13) && defined(HAVE_SUPPORTED_CURVES)
  67. static int TLSX_KeyShare_IsSupported(int namedGroup);
  68. static void TLSX_KeyShare_FreeAll(KeyShareEntry* list, void* heap);
  69. #endif
  70. #ifdef HAVE_SUPPORTED_CURVES
  71. static int TLSX_PopulateSupportedGroups(WOLFSSL* ssl, TLSX** extensions);
  72. #endif
  73. /* Digest enable checks */
  74. #ifdef NO_OLD_TLS /* TLS 1.2 only */
  75. #if defined(NO_SHA256) && !defined(WOLFSSL_SHA384) && \
  76. !defined(WOLFSSL_SHA512)
  77. #error Must have SHA256, SHA384 or SHA512 enabled for TLS 1.2
  78. #endif
  79. #else /* TLS 1.1 or older */
  80. #if defined(NO_MD5) && defined(NO_SHA)
  81. #error Must have SHA1 and MD5 enabled for old TLS
  82. #endif
  83. #endif
  84. #ifdef WOLFSSL_TLS13
  85. #if !defined(NO_DH) && \
  86. !defined(HAVE_FFDHE_2048) && !defined(HAVE_FFDHE_3072) && \
  87. !defined(HAVE_FFDHE_4096) && !defined(HAVE_FFDHE_6144) && \
  88. !defined(HAVE_FFDHE_8192)
  89. #error Please configure your TLS 1.3 DH key size using either: HAVE_FFDHE_2048, HAVE_FFDHE_3072, HAVE_FFDHE_4096, HAVE_FFDHE_6144 or HAVE_FFDHE_8192
  90. #endif
  91. #if !defined(NO_RSA) && !defined(WC_RSA_PSS)
  92. #error The build option WC_RSA_PSS is required for TLS 1.3 with RSA
  93. #endif
  94. #ifndef HAVE_TLS_EXTENSIONS
  95. #ifndef _MSC_VER
  96. #error "The build option HAVE_TLS_EXTENSIONS is required for TLS 1.3"
  97. #else
  98. #pragma message("Error: The build option HAVE_TLS_EXTENSIONS is required for TLS 1.3")
  99. #endif
  100. #endif
  101. #endif
  102. /* Warn if secrets logging is enabled */
  103. #if defined(SHOW_SECRETS) || defined(WOLFSSL_SSLKEYLOGFILE)
  104. #ifndef _MSC_VER
  105. #warning The SHOW_SECRETS and WOLFSSL_SSLKEYLOGFILE options should only be used for debugging and never in a production environment
  106. #else
  107. #pragma message("Warning: The SHOW_SECRETS and WOLFSSL_SSLKEYLOGFILE options should only be used for debugging and never in a production environment")
  108. #endif
  109. #endif
  110. /* Optional Pre-Master-Secret logging for Wireshark */
  111. #if !defined(NO_FILESYSTEM) && defined(WOLFSSL_SSLKEYLOGFILE)
  112. #ifndef WOLFSSL_SSLKEYLOGFILE_OUTPUT
  113. #define WOLFSSL_SSLKEYLOGFILE_OUTPUT "sslkeylog.log"
  114. #endif
  115. #endif
  116. #ifndef WOLFSSL_NO_TLS12
  117. #ifdef WOLFSSL_SHA384
  118. #define HSHASH_SZ WC_SHA384_DIGEST_SIZE
  119. #else
  120. #define HSHASH_SZ FINISHED_SZ
  121. #endif
  122. int BuildTlsHandshakeHash(WOLFSSL* ssl, byte* hash, word32* hashLen)
  123. {
  124. int ret = 0;
  125. word32 hashSz = FINISHED_SZ;
  126. if (ssl == NULL || hash == NULL || hashLen == NULL || *hashLen < HSHASH_SZ)
  127. return BAD_FUNC_ARG;
  128. /* for constant timing perform these even if error */
  129. #ifndef NO_OLD_TLS
  130. ret |= wc_Md5GetHash(&ssl->hsHashes->hashMd5, hash);
  131. ret |= wc_ShaGetHash(&ssl->hsHashes->hashSha, &hash[WC_MD5_DIGEST_SIZE]);
  132. #endif
  133. if (IsAtLeastTLSv1_2(ssl)) {
  134. #ifndef NO_SHA256
  135. if (ssl->specs.mac_algorithm <= sha256_mac ||
  136. ssl->specs.mac_algorithm == blake2b_mac) {
  137. ret |= wc_Sha256GetHash(&ssl->hsHashes->hashSha256, hash);
  138. hashSz = WC_SHA256_DIGEST_SIZE;
  139. }
  140. #endif
  141. #ifdef WOLFSSL_SHA384
  142. if (ssl->specs.mac_algorithm == sha384_mac) {
  143. ret |= wc_Sha384GetHash(&ssl->hsHashes->hashSha384, hash);
  144. hashSz = WC_SHA384_DIGEST_SIZE;
  145. }
  146. #endif
  147. }
  148. *hashLen = hashSz;
  149. #ifdef WOLFSSL_CHECK_MEM_ZERO
  150. wc_MemZero_Add("TLS handshake hash", hash, hashSz);
  151. #endif
  152. if (ret != 0) {
  153. ret = BUILD_MSG_ERROR;
  154. WOLFSSL_ERROR_VERBOSE(ret);
  155. }
  156. return ret;
  157. }
  158. int BuildTlsFinished(WOLFSSL* ssl, Hashes* hashes, const byte* sender)
  159. {
  160. int ret;
  161. const byte* side = NULL;
  162. word32 hashSz = HSHASH_SZ;
  163. #if !defined(WOLFSSL_ASYNC_CRYPT) || defined(WC_ASYNC_NO_HASH)
  164. byte handshake_hash[HSHASH_SZ];
  165. #else
  166. WC_DECLARE_VAR(handshake_hash, byte, HSHASH_SZ, ssl->heap);
  167. if (handshake_hash == NULL)
  168. return MEMORY_E;
  169. #endif
  170. ret = BuildTlsHandshakeHash(ssl, handshake_hash, &hashSz);
  171. if (ret == 0) {
  172. if (XSTRNCMP((const char*)sender, (const char*)kTlsClientStr,
  173. SIZEOF_SENDER) == 0) {
  174. side = kTlsClientFinStr;
  175. }
  176. else if (XSTRNCMP((const char*)sender, (const char*)kTlsServerStr,
  177. SIZEOF_SENDER) == 0) {
  178. side = kTlsServerFinStr;
  179. }
  180. else {
  181. ret = BAD_FUNC_ARG;
  182. WOLFSSL_MSG("Unexpected sender value");
  183. }
  184. }
  185. if (ret == 0) {
  186. #ifdef WOLFSSL_HAVE_PRF
  187. #if !defined(NO_CERTS) && defined(HAVE_PK_CALLBACKS)
  188. if (ssl->ctx->TlsFinishedCb) {
  189. void* ctx = wolfSSL_GetTlsFinishedCtx(ssl);
  190. ret = ssl->ctx->TlsFinishedCb(ssl, side, handshake_hash,
  191. (byte*)hashes, ctx);
  192. }
  193. if (!ssl->ctx->TlsFinishedCb || ret == PROTOCOLCB_UNAVAILABLE)
  194. #endif
  195. {
  196. PRIVATE_KEY_UNLOCK();
  197. ret = wc_PRF_TLS((byte*)hashes, TLS_FINISHED_SZ,
  198. ssl->arrays->masterSecret,
  199. SECRET_LEN, side, FINISHED_LABEL_SZ, handshake_hash, hashSz,
  200. IsAtLeastTLSv1_2(ssl), ssl->specs.mac_algorithm,
  201. ssl->heap, ssl->devId);
  202. PRIVATE_KEY_LOCK();
  203. }
  204. ForceZero(handshake_hash, hashSz);
  205. #else
  206. /* Pseudo random function must be enabled in the configuration. */
  207. ret = PRF_MISSING;
  208. WOLFSSL_ERROR_VERBOSE(ret);
  209. WOLFSSL_MSG("Pseudo-random function is not enabled");
  210. (void)side;
  211. (void)hashes;
  212. #endif
  213. }
  214. #if defined(WOLFSSL_ASYNC_CRYPT) && !defined(WC_ASYNC_NO_HASH)
  215. WC_FREE_VAR(handshake_hash, ssl->heap);
  216. #elif defined(WOLFSSL_CHECK_MEM_ZERO)
  217. wc_MemZero_Check(handshake_hash, HSHASH_SZ);
  218. #endif
  219. return ret;
  220. }
  221. #endif /* !WOLFSSL_NO_TLS12 */
  222. #ifndef NO_OLD_TLS
  223. #ifdef WOLFSSL_ALLOW_TLSV10
  224. ProtocolVersion MakeTLSv1(void)
  225. {
  226. ProtocolVersion pv;
  227. pv.major = SSLv3_MAJOR;
  228. pv.minor = TLSv1_MINOR;
  229. return pv;
  230. }
  231. #endif /* WOLFSSL_ALLOW_TLSV10 */
  232. ProtocolVersion MakeTLSv1_1(void)
  233. {
  234. ProtocolVersion pv;
  235. pv.major = SSLv3_MAJOR;
  236. pv.minor = TLSv1_1_MINOR;
  237. return pv;
  238. }
  239. #endif /* !NO_OLD_TLS */
  240. #ifndef WOLFSSL_NO_TLS12
  241. ProtocolVersion MakeTLSv1_2(void)
  242. {
  243. ProtocolVersion pv;
  244. pv.major = SSLv3_MAJOR;
  245. pv.minor = TLSv1_2_MINOR;
  246. return pv;
  247. }
  248. #endif /* !WOLFSSL_NO_TLS12 */
  249. #ifdef WOLFSSL_TLS13
  250. /* The TLS v1.3 protocol version.
  251. *
  252. * returns the protocol version data for TLS v1.3.
  253. */
  254. ProtocolVersion MakeTLSv1_3(void)
  255. {
  256. ProtocolVersion pv;
  257. pv.major = SSLv3_MAJOR;
  258. pv.minor = TLSv1_3_MINOR;
  259. return pv;
  260. }
  261. #endif
  262. #ifndef WOLFSSL_NO_TLS12
  263. #ifdef HAVE_EXTENDED_MASTER
  264. static const byte ext_master_label[EXT_MASTER_LABEL_SZ + 1] =
  265. "extended master secret";
  266. #endif
  267. static const byte master_label[MASTER_LABEL_SZ + 1] = "master secret";
  268. static const byte key_label [KEY_LABEL_SZ + 1] = "key expansion";
  269. static int _DeriveTlsKeys(byte* key_dig, word32 key_dig_len,
  270. const byte* ms, word32 msLen,
  271. const byte* sr, const byte* cr,
  272. int tls1_2, int hash_type,
  273. void* heap, int devId)
  274. {
  275. int ret;
  276. #if defined(WOLFSSL_ASYNC_CRYPT) && !defined(WC_ASYNC_NO_HASH)
  277. WC_DECLARE_VAR(seed, byte, SEED_LEN, heap);
  278. if (seed == NULL)
  279. return MEMORY_E;
  280. #else
  281. byte seed[SEED_LEN];
  282. #endif
  283. XMEMCPY(seed, sr, RAN_LEN);
  284. XMEMCPY(seed + RAN_LEN, cr, RAN_LEN);
  285. #ifdef WOLFSSL_HAVE_PRF
  286. PRIVATE_KEY_UNLOCK();
  287. ret = wc_PRF_TLS(key_dig, key_dig_len, ms, msLen, key_label, KEY_LABEL_SZ,
  288. seed, SEED_LEN, tls1_2, hash_type, heap, devId);
  289. PRIVATE_KEY_LOCK();
  290. #else
  291. /* Pseudo random function must be enabled in the configuration. */
  292. ret = PRF_MISSING;
  293. WOLFSSL_ERROR_VERBOSE(ret);
  294. WOLFSSL_MSG("Pseudo-random function is not enabled");
  295. (void)key_dig;
  296. (void)key_dig_len;
  297. (void)ms;
  298. (void)msLen;
  299. (void)tls1_2;
  300. (void)hash_type;
  301. (void)heap;
  302. (void)devId;
  303. (void)key_label;
  304. (void)master_label;
  305. #ifdef HAVE_EXTENDED_MASTER
  306. (void)ext_master_label;
  307. #endif
  308. #endif
  309. #if defined(WOLFSSL_ASYNC_CRYPT) && !defined(WC_ASYNC_NO_HASH)
  310. WC_FREE_VAR(seed, heap);
  311. #endif
  312. return ret;
  313. }
  314. /* External facing wrapper so user can call as well, 0 on success */
  315. int wolfSSL_DeriveTlsKeys(byte* key_dig, word32 key_dig_len,
  316. const byte* ms, word32 msLen,
  317. const byte* sr, const byte* cr,
  318. int tls1_2, int hash_type)
  319. {
  320. return _DeriveTlsKeys(key_dig, key_dig_len, ms, msLen, sr, cr, tls1_2,
  321. hash_type, NULL, INVALID_DEVID);
  322. }
  323. int DeriveTlsKeys(WOLFSSL* ssl)
  324. {
  325. int ret;
  326. int key_dig_len = 2 * ssl->specs.hash_size +
  327. 2 * ssl->specs.key_size +
  328. 2 * ssl->specs.iv_size;
  329. #ifdef WOLFSSL_SMALL_STACK
  330. byte* key_dig;
  331. #else
  332. byte key_dig[MAX_PRF_DIG];
  333. #endif
  334. #ifdef WOLFSSL_SMALL_STACK
  335. key_dig = (byte*)XMALLOC(MAX_PRF_DIG, ssl->heap, DYNAMIC_TYPE_DIGEST);
  336. if (key_dig == NULL) {
  337. return MEMORY_E;
  338. }
  339. #endif
  340. #if !defined(NO_CERTS) && defined(HAVE_PK_CALLBACKS)
  341. ret = PROTOCOLCB_UNAVAILABLE;
  342. if (ssl->ctx->GenSessionKeyCb) {
  343. void* ctx = wolfSSL_GetGenSessionKeyCtx(ssl);
  344. ret = ssl->ctx->GenSessionKeyCb(ssl, ctx);
  345. }
  346. if (!ssl->ctx->GenSessionKeyCb || ret == PROTOCOLCB_UNAVAILABLE)
  347. #endif
  348. ret = _DeriveTlsKeys(key_dig, key_dig_len,
  349. ssl->arrays->masterSecret, SECRET_LEN,
  350. ssl->arrays->serverRandom, ssl->arrays->clientRandom,
  351. IsAtLeastTLSv1_2(ssl), ssl->specs.mac_algorithm,
  352. ssl->heap, ssl->devId);
  353. if (ret == 0)
  354. ret = StoreKeys(ssl, key_dig, PROVISION_CLIENT_SERVER);
  355. #ifdef WOLFSSL_SMALL_STACK
  356. XFREE(key_dig, ssl->heap, DYNAMIC_TYPE_DIGEST);
  357. #endif
  358. return ret;
  359. }
  360. static int _MakeTlsMasterSecret(byte* ms, word32 msLen,
  361. const byte* pms, word32 pmsLen,
  362. const byte* cr, const byte* sr,
  363. int tls1_2, int hash_type,
  364. void* heap, int devId)
  365. {
  366. int ret;
  367. #if !defined(WOLFSSL_ASYNC_CRYPT) || defined(WC_ASYNC_NO_HASH)
  368. byte seed[SEED_LEN];
  369. #else
  370. WC_DECLARE_VAR(seed, byte, SEED_LEN, heap);
  371. if (seed == NULL)
  372. return MEMORY_E;
  373. #endif
  374. XMEMCPY(seed, cr, RAN_LEN);
  375. XMEMCPY(seed + RAN_LEN, sr, RAN_LEN);
  376. #ifdef WOLFSSL_HAVE_PRF
  377. PRIVATE_KEY_UNLOCK();
  378. ret = wc_PRF_TLS(ms, msLen, pms, pmsLen, master_label, MASTER_LABEL_SZ,
  379. seed, SEED_LEN, tls1_2, hash_type, heap, devId);
  380. PRIVATE_KEY_LOCK();
  381. #else
  382. /* Pseudo random function must be enabled in the configuration. */
  383. ret = PRF_MISSING;
  384. WOLFSSL_MSG("Pseudo-random function is not enabled");
  385. (void)ms;
  386. (void)msLen;
  387. (void)pms;
  388. (void)pmsLen;
  389. (void)tls1_2;
  390. (void)hash_type;
  391. (void)heap;
  392. (void)devId;
  393. #endif
  394. #if defined(WOLFSSL_ASYNC_CRYPT) && !defined(WC_ASYNC_NO_HASH)
  395. WC_FREE_VAR(seed, heap);
  396. #endif
  397. return ret;
  398. }
  399. /* External facing wrapper so user can call as well, 0 on success */
  400. int wolfSSL_MakeTlsMasterSecret(byte* ms, word32 msLen,
  401. const byte* pms, word32 pmsLen,
  402. const byte* cr, const byte* sr,
  403. int tls1_2, int hash_type)
  404. {
  405. return _MakeTlsMasterSecret(ms, msLen, pms, pmsLen, cr, sr, tls1_2,
  406. hash_type, NULL, INVALID_DEVID);
  407. }
  408. #ifdef HAVE_EXTENDED_MASTER
  409. static int _MakeTlsExtendedMasterSecret(byte* ms, word32 msLen,
  410. const byte* pms, word32 pmsLen,
  411. const byte* sHash, word32 sHashLen,
  412. int tls1_2, int hash_type,
  413. void* heap, int devId)
  414. {
  415. int ret;
  416. #ifdef WOLFSSL_HAVE_PRF
  417. PRIVATE_KEY_UNLOCK();
  418. ret = wc_PRF_TLS(ms, msLen, pms, pmsLen, ext_master_label, EXT_MASTER_LABEL_SZ,
  419. sHash, sHashLen, tls1_2, hash_type, heap, devId);
  420. PRIVATE_KEY_LOCK();
  421. #else
  422. /* Pseudo random function must be enabled in the configuration. */
  423. ret = PRF_MISSING;
  424. WOLFSSL_MSG("Pseudo-random function is not enabled");
  425. (void)ms;
  426. (void)msLen;
  427. (void)pms;
  428. (void)pmsLen;
  429. (void)sHash;
  430. (void)sHashLen;
  431. (void)tls1_2;
  432. (void)hash_type;
  433. (void)heap;
  434. (void)devId;
  435. #endif
  436. return ret;
  437. }
  438. /* External facing wrapper so user can call as well, 0 on success */
  439. int wolfSSL_MakeTlsExtendedMasterSecret(byte* ms, word32 msLen,
  440. const byte* pms, word32 pmsLen,
  441. const byte* sHash, word32 sHashLen,
  442. int tls1_2, int hash_type)
  443. {
  444. return _MakeTlsExtendedMasterSecret(ms, msLen, pms, pmsLen, sHash, sHashLen,
  445. tls1_2, hash_type, NULL, INVALID_DEVID);
  446. }
  447. #endif /* HAVE_EXTENDED_MASTER */
  448. int MakeTlsMasterSecret(WOLFSSL* ssl)
  449. {
  450. int ret;
  451. #ifdef HAVE_EXTENDED_MASTER
  452. if (ssl->options.haveEMS) {
  453. word32 hashSz = HSHASH_SZ;
  454. #ifdef WOLFSSL_SMALL_STACK
  455. byte* handshake_hash = (byte*)XMALLOC(HSHASH_SZ, ssl->heap,
  456. DYNAMIC_TYPE_DIGEST);
  457. if (handshake_hash == NULL)
  458. return MEMORY_E;
  459. #else
  460. byte handshake_hash[HSHASH_SZ];
  461. #endif
  462. ret = BuildTlsHandshakeHash(ssl, handshake_hash, &hashSz);
  463. if (ret == 0) {
  464. ret = _MakeTlsExtendedMasterSecret(
  465. ssl->arrays->masterSecret, SECRET_LEN,
  466. ssl->arrays->preMasterSecret, ssl->arrays->preMasterSz,
  467. handshake_hash, hashSz,
  468. IsAtLeastTLSv1_2(ssl), ssl->specs.mac_algorithm,
  469. ssl->heap, ssl->devId);
  470. ForceZero(handshake_hash, hashSz);
  471. }
  472. #ifdef WOLFSSL_SMALL_STACK
  473. XFREE(handshake_hash, ssl->heap, DYNAMIC_TYPE_DIGEST);
  474. #elif defined(WOLFSSL_CHECK_MEM_ZERO)
  475. wc_MemZero_Check(handshake_hash, HSHASH_SZ);
  476. #endif
  477. }
  478. else
  479. #endif /* HAVE_EXTENDED_MASTER */
  480. {
  481. #if !defined(NO_CERTS) && defined(HAVE_PK_CALLBACKS)
  482. ret = PROTOCOLCB_UNAVAILABLE;
  483. if (ssl->ctx->GenMasterCb) {
  484. void* ctx = wolfSSL_GetGenMasterSecretCtx(ssl);
  485. ret = ssl->ctx->GenMasterCb(ssl, ctx);
  486. }
  487. if (!ssl->ctx->GenMasterCb || ret == PROTOCOLCB_UNAVAILABLE)
  488. #endif
  489. ret = _MakeTlsMasterSecret(ssl->arrays->masterSecret, SECRET_LEN,
  490. ssl->arrays->preMasterSecret, ssl->arrays->preMasterSz,
  491. ssl->arrays->clientRandom, ssl->arrays->serverRandom,
  492. IsAtLeastTLSv1_2(ssl), ssl->specs.mac_algorithm,
  493. ssl->heap, ssl->devId);
  494. }
  495. if (ret == 0) {
  496. #ifdef SHOW_SECRETS
  497. /* Wireshark Pre-Master-Secret Format:
  498. * CLIENT_RANDOM <clientrandom> <mastersecret>
  499. */
  500. const char* CLIENT_RANDOM_LABEL = "CLIENT_RANDOM";
  501. int i, pmsPos = 0;
  502. char pmsBuf[13 + 1 + 64 + 1 + 96 + 1 + 1];
  503. XSNPRINTF(&pmsBuf[pmsPos], sizeof(pmsBuf) - pmsPos, "%s ",
  504. CLIENT_RANDOM_LABEL);
  505. pmsPos += XSTRLEN(CLIENT_RANDOM_LABEL) + 1;
  506. for (i = 0; i < RAN_LEN; i++) {
  507. XSNPRINTF(&pmsBuf[pmsPos], sizeof(pmsBuf) - pmsPos, "%02x",
  508. ssl->arrays->clientRandom[i]);
  509. pmsPos += 2;
  510. }
  511. XSNPRINTF(&pmsBuf[pmsPos], sizeof(pmsBuf) - pmsPos, " ");
  512. pmsPos += 1;
  513. for (i = 0; i < SECRET_LEN; i++) {
  514. XSNPRINTF(&pmsBuf[pmsPos], sizeof(pmsBuf) - pmsPos, "%02x",
  515. ssl->arrays->masterSecret[i]);
  516. pmsPos += 2;
  517. }
  518. XSNPRINTF(&pmsBuf[pmsPos], sizeof(pmsBuf) - pmsPos, "\n");
  519. pmsPos += 1;
  520. /* print master secret */
  521. puts(pmsBuf);
  522. #if !defined(NO_FILESYSTEM) && defined(WOLFSSL_SSLKEYLOGFILE)
  523. {
  524. FILE* f = XFOPEN(WOLFSSL_SSLKEYLOGFILE_OUTPUT, "a");
  525. if (f != XBADFILE) {
  526. XFWRITE(pmsBuf, 1, pmsPos, f);
  527. XFCLOSE(f);
  528. }
  529. }
  530. #endif
  531. #endif /* SHOW_SECRETS */
  532. ret = DeriveTlsKeys(ssl);
  533. }
  534. return ret;
  535. }
  536. /* Used by EAP-TLS and EAP-TTLS to derive keying material from
  537. * the master_secret. */
  538. int wolfSSL_make_eap_keys(WOLFSSL* ssl, void* msk, unsigned int len,
  539. const char* label)
  540. {
  541. int ret;
  542. #ifdef WOLFSSL_SMALL_STACK
  543. byte* seed;
  544. #else
  545. byte seed[SEED_LEN];
  546. #endif
  547. #ifdef WOLFSSL_SMALL_STACK
  548. seed = (byte*)XMALLOC(SEED_LEN, ssl->heap, DYNAMIC_TYPE_SEED);
  549. if (seed == NULL)
  550. return MEMORY_E;
  551. #endif
  552. /*
  553. * As per RFC-5281, the order of the client and server randoms is reversed
  554. * from that used by the TLS protocol to derive keys.
  555. */
  556. XMEMCPY(seed, ssl->arrays->clientRandom, RAN_LEN);
  557. XMEMCPY(seed + RAN_LEN, ssl->arrays->serverRandom, RAN_LEN);
  558. #ifdef WOLFSSL_HAVE_PRF
  559. PRIVATE_KEY_UNLOCK();
  560. ret = wc_PRF_TLS((byte*)msk, len, ssl->arrays->masterSecret, SECRET_LEN,
  561. (const byte *)label, (word32)XSTRLEN(label), seed, SEED_LEN,
  562. IsAtLeastTLSv1_2(ssl), ssl->specs.mac_algorithm,
  563. ssl->heap, ssl->devId);
  564. PRIVATE_KEY_LOCK();
  565. #else
  566. /* Pseudo random function must be enabled in the configuration. */
  567. ret = PRF_MISSING;
  568. WOLFSSL_MSG("Pseudo-random function is not enabled");
  569. (void)msk;
  570. (void)len;
  571. (void)label;
  572. #endif
  573. #ifdef WOLFSSL_SMALL_STACK
  574. XFREE(seed, ssl->heap, DYNAMIC_TYPE_SEED);
  575. #endif
  576. return ret;
  577. }
  578. /* return HMAC digest type in wolfSSL format */
  579. int wolfSSL_GetHmacType(WOLFSSL* ssl)
  580. {
  581. if (ssl == NULL)
  582. return BAD_FUNC_ARG;
  583. switch (ssl->specs.mac_algorithm) {
  584. #ifndef NO_MD5
  585. case md5_mac:
  586. {
  587. return WC_MD5;
  588. }
  589. #endif
  590. #ifndef NO_SHA256
  591. case sha256_mac:
  592. {
  593. return WC_SHA256;
  594. }
  595. #endif
  596. #ifdef WOLFSSL_SHA384
  597. case sha384_mac:
  598. {
  599. return WC_SHA384;
  600. }
  601. #endif
  602. #ifndef NO_SHA
  603. case sha_mac:
  604. {
  605. return WC_SHA;
  606. }
  607. #endif
  608. #ifdef HAVE_BLAKE2
  609. case blake2b_mac:
  610. {
  611. return BLAKE2B_ID;
  612. }
  613. #endif
  614. default:
  615. {
  616. return WOLFSSL_FATAL_ERROR;
  617. }
  618. }
  619. }
  620. int wolfSSL_SetTlsHmacInner(WOLFSSL* ssl, byte* inner, word32 sz, int content,
  621. int verify)
  622. {
  623. if (ssl == NULL || inner == NULL)
  624. return BAD_FUNC_ARG;
  625. XMEMSET(inner, 0, WOLFSSL_TLS_HMAC_INNER_SZ);
  626. WriteSEQ(ssl, verify, inner);
  627. inner[SEQ_SZ] = (byte)content;
  628. inner[SEQ_SZ + ENUM_LEN] = ssl->version.major;
  629. inner[SEQ_SZ + ENUM_LEN + ENUM_LEN] = ssl->version.minor;
  630. c16toa((word16)sz, inner + SEQ_SZ + ENUM_LEN + VERSION_SZ);
  631. return 0;
  632. }
  633. #ifndef WOLFSSL_AEAD_ONLY
  634. #if !defined(WOLFSSL_NO_HASH_RAW) && !defined(HAVE_FIPS) && \
  635. !defined(HAVE_SELFTEST)
  636. /* Update the hash in the HMAC.
  637. *
  638. * hmac HMAC object.
  639. * data Data to be hashed.
  640. * sz Size of data to hash.
  641. * returns 0 on success, otherwise failure.
  642. */
  643. static int Hmac_HashUpdate(Hmac* hmac, const byte* data, word32 sz)
  644. {
  645. int ret = BAD_FUNC_ARG;
  646. switch (hmac->macType) {
  647. #ifndef NO_SHA
  648. case WC_SHA:
  649. ret = wc_ShaUpdate(&hmac->hash.sha, data, sz);
  650. break;
  651. #endif /* !NO_SHA */
  652. #ifndef NO_SHA256
  653. case WC_SHA256:
  654. ret = wc_Sha256Update(&hmac->hash.sha256, data, sz);
  655. break;
  656. #endif /* !NO_SHA256 */
  657. #ifdef WOLFSSL_SHA384
  658. case WC_SHA384:
  659. ret = wc_Sha384Update(&hmac->hash.sha384, data, sz);
  660. break;
  661. #endif /* WOLFSSL_SHA384 */
  662. #ifdef WOLFSSL_SHA512
  663. case WC_SHA512:
  664. ret = wc_Sha512Update(&hmac->hash.sha512, data, sz);
  665. break;
  666. #endif /* WOLFSSL_SHA512 */
  667. default:
  668. break;
  669. }
  670. return ret;
  671. }
  672. /* Finalize the hash but don't put the EOC, padding or length in.
  673. *
  674. * hmac HMAC object.
  675. * hash Hash result.
  676. * returns 0 on success, otherwise failure.
  677. */
  678. static int Hmac_HashFinalRaw(Hmac* hmac, unsigned char* hash)
  679. {
  680. int ret = BAD_FUNC_ARG;
  681. switch (hmac->macType) {
  682. #ifndef NO_SHA
  683. case WC_SHA:
  684. ret = wc_ShaFinalRaw(&hmac->hash.sha, hash);
  685. break;
  686. #endif /* !NO_SHA */
  687. #ifndef NO_SHA256
  688. case WC_SHA256:
  689. ret = wc_Sha256FinalRaw(&hmac->hash.sha256, hash);
  690. break;
  691. #endif /* !NO_SHA256 */
  692. #ifdef WOLFSSL_SHA384
  693. case WC_SHA384:
  694. ret = wc_Sha384FinalRaw(&hmac->hash.sha384, hash);
  695. break;
  696. #endif /* WOLFSSL_SHA384 */
  697. #ifdef WOLFSSL_SHA512
  698. case WC_SHA512:
  699. ret = wc_Sha512FinalRaw(&hmac->hash.sha512, hash);
  700. break;
  701. #endif /* WOLFSSL_SHA512 */
  702. default:
  703. break;
  704. }
  705. return ret;
  706. }
  707. /* Finalize the HMAC by performing outer hash.
  708. *
  709. * hmac HMAC object.
  710. * mac MAC result.
  711. * returns 0 on success, otherwise failure.
  712. */
  713. static int Hmac_OuterHash(Hmac* hmac, unsigned char* mac)
  714. {
  715. int ret = BAD_FUNC_ARG;
  716. wc_HashAlg hash;
  717. enum wc_HashType hashType = (enum wc_HashType)hmac->macType;
  718. int digestSz = wc_HashGetDigestSize(hashType);
  719. int blockSz = wc_HashGetBlockSize(hashType);
  720. if ((digestSz >= 0) && (blockSz >= 0)) {
  721. ret = wc_HashInit(&hash, hashType);
  722. }
  723. if (ret == 0) {
  724. ret = wc_HashUpdate(&hash, hashType, (byte*)hmac->opad,
  725. blockSz);
  726. if (ret == 0)
  727. ret = wc_HashUpdate(&hash, hashType, (byte*)hmac->innerHash,
  728. digestSz);
  729. if (ret == 0)
  730. ret = wc_HashFinal(&hash, hashType, mac);
  731. wc_HashFree(&hash, hashType);
  732. }
  733. return ret;
  734. }
  735. /* Calculate the HMAC of the header + message data.
  736. * Constant time implementation using wc_Sha*FinalRaw().
  737. *
  738. * hmac HMAC object.
  739. * digest MAC result.
  740. * in Message data.
  741. * sz Size of the message data.
  742. * header Constructed record header with length of handshake data.
  743. * returns 0 on success, otherwise failure.
  744. */
  745. static int Hmac_UpdateFinal_CT(Hmac* hmac, byte* digest, const byte* in,
  746. word32 sz, int macLen, byte* header)
  747. {
  748. byte lenBytes[8];
  749. int i, j;
  750. unsigned int k;
  751. int blockBits, blockMask;
  752. int lastBlockLen, extraLen, eocIndex;
  753. int blocks, safeBlocks, lenBlock, eocBlock;
  754. unsigned int maxLen;
  755. int blockSz, padSz;
  756. int ret;
  757. word32 realLen;
  758. byte extraBlock;
  759. switch (hmac->macType) {
  760. #ifndef NO_SHA
  761. case WC_SHA:
  762. blockSz = WC_SHA_BLOCK_SIZE;
  763. blockBits = 6;
  764. padSz = WC_SHA_BLOCK_SIZE - WC_SHA_PAD_SIZE + 1;
  765. break;
  766. #endif /* !NO_SHA */
  767. #ifndef NO_SHA256
  768. case WC_SHA256:
  769. blockSz = WC_SHA256_BLOCK_SIZE;
  770. blockBits = 6;
  771. padSz = WC_SHA256_BLOCK_SIZE - WC_SHA256_PAD_SIZE + 1;
  772. break;
  773. #endif /* !NO_SHA256 */
  774. #ifdef WOLFSSL_SHA384
  775. case WC_SHA384:
  776. blockSz = WC_SHA384_BLOCK_SIZE;
  777. blockBits = 7;
  778. padSz = WC_SHA384_BLOCK_SIZE - WC_SHA384_PAD_SIZE + 1;
  779. break;
  780. #endif /* WOLFSSL_SHA384 */
  781. #ifdef WOLFSSL_SHA512
  782. case WC_SHA512:
  783. blockSz = WC_SHA512_BLOCK_SIZE;
  784. blockBits = 7;
  785. padSz = WC_SHA512_BLOCK_SIZE - WC_SHA512_PAD_SIZE + 1;
  786. break;
  787. #endif /* WOLFSSL_SHA512 */
  788. default:
  789. return BAD_FUNC_ARG;
  790. }
  791. blockMask = blockSz - 1;
  792. /* Size of data to HMAC if padding length byte is zero. */
  793. maxLen = WOLFSSL_TLS_HMAC_INNER_SZ + sz - 1 - macLen;
  794. /* Complete data (including padding) has block for EOC and/or length. */
  795. extraBlock = ctSetLTE((maxLen + padSz) & blockMask, padSz);
  796. /* Total number of blocks for data including padding. */
  797. blocks = ((maxLen + blockSz - 1) >> blockBits) + extraBlock;
  798. /* Up to last 6 blocks can be hashed safely. */
  799. safeBlocks = blocks - 6;
  800. /* Length of message data. */
  801. realLen = maxLen - in[sz - 1];
  802. /* Number of message bytes in last block. */
  803. lastBlockLen = realLen & blockMask;
  804. /* Number of padding bytes in last block. */
  805. extraLen = ((blockSz * 2 - padSz - lastBlockLen) & blockMask) + 1;
  806. /* Number of blocks to create for hash. */
  807. lenBlock = (realLen + extraLen) >> blockBits;
  808. /* Block containing EOC byte. */
  809. eocBlock = realLen >> blockBits;
  810. /* Index of EOC byte in block. */
  811. eocIndex = realLen & blockMask;
  812. /* Add length of hmac's ipad to total length. */
  813. realLen += blockSz;
  814. /* Length as bits - 8 bytes bigendian. */
  815. c32toa(realLen >> ((sizeof(word32) * 8) - 3), lenBytes);
  816. c32toa(realLen << 3, lenBytes + sizeof(word32));
  817. ret = Hmac_HashUpdate(hmac, (unsigned char*)hmac->ipad, blockSz);
  818. if (ret != 0)
  819. return ret;
  820. XMEMSET(hmac->innerHash, 0, macLen);
  821. if (safeBlocks > 0) {
  822. ret = Hmac_HashUpdate(hmac, header, WOLFSSL_TLS_HMAC_INNER_SZ);
  823. if (ret != 0)
  824. return ret;
  825. ret = Hmac_HashUpdate(hmac, in, safeBlocks * blockSz -
  826. WOLFSSL_TLS_HMAC_INNER_SZ);
  827. if (ret != 0)
  828. return ret;
  829. }
  830. else
  831. safeBlocks = 0;
  832. XMEMSET(digest, 0, macLen);
  833. k = safeBlocks * blockSz;
  834. for (i = safeBlocks; i < blocks; i++) {
  835. unsigned char hashBlock[WC_MAX_BLOCK_SIZE];
  836. unsigned char isEocBlock = ctMaskEq(i, eocBlock);
  837. unsigned char isOutBlock = ctMaskEq(i, lenBlock);
  838. for (j = 0; j < blockSz; j++) {
  839. unsigned char atEoc = ctMaskEq(j, eocIndex) & isEocBlock;
  840. unsigned char pastEoc = ctMaskGT(j, eocIndex) & isEocBlock;
  841. unsigned char b = 0;
  842. if (k < WOLFSSL_TLS_HMAC_INNER_SZ)
  843. b = header[k];
  844. else if (k < maxLen)
  845. b = in[k - WOLFSSL_TLS_HMAC_INNER_SZ];
  846. k++;
  847. b = ctMaskSel(atEoc, 0x80, b);
  848. b &= (unsigned char)~(word32)pastEoc;
  849. b &= ((unsigned char)~(word32)isOutBlock) | isEocBlock;
  850. if (j >= blockSz - 8) {
  851. b = ctMaskSel(isOutBlock, lenBytes[j - (blockSz - 8)], b);
  852. }
  853. hashBlock[j] = b;
  854. }
  855. ret = Hmac_HashUpdate(hmac, hashBlock, blockSz);
  856. if (ret != 0)
  857. return ret;
  858. ret = Hmac_HashFinalRaw(hmac, hashBlock);
  859. if (ret != 0)
  860. return ret;
  861. for (j = 0; j < macLen; j++)
  862. ((unsigned char*)hmac->innerHash)[j] |= hashBlock[j] & isOutBlock;
  863. }
  864. ret = Hmac_OuterHash(hmac, digest);
  865. return ret;
  866. }
  867. #endif
  868. #if defined(WOLFSSL_NO_HASH_RAW) || defined(HAVE_FIPS) || \
  869. defined(HAVE_SELFTEST) || defined(HAVE_BLAKE2)
  870. /* Calculate the HMAC of the header + message data.
  871. * Constant time implementation using normal hashing operations.
  872. * Update-Final need to be constant time.
  873. *
  874. * hmac HMAC object.
  875. * digest MAC result.
  876. * in Message data.
  877. * sz Size of the message data.
  878. * header Constructed record header with length of handshake data.
  879. * returns 0 on success, otherwise failure.
  880. */
  881. static int Hmac_UpdateFinal(Hmac* hmac, byte* digest, const byte* in,
  882. word32 sz, byte* header)
  883. {
  884. byte dummy[WC_MAX_BLOCK_SIZE] = {0};
  885. int ret;
  886. word32 msgSz, blockSz, macSz, padSz, maxSz, realSz;
  887. word32 currSz, offset = 0;
  888. int msgBlocks, blocks, blockBits;
  889. int i;
  890. switch (hmac->macType) {
  891. #ifndef NO_SHA
  892. case WC_SHA:
  893. blockSz = WC_SHA_BLOCK_SIZE;
  894. blockBits = 6;
  895. macSz = WC_SHA_DIGEST_SIZE;
  896. padSz = WC_SHA_BLOCK_SIZE - WC_SHA_PAD_SIZE + 1;
  897. break;
  898. #endif /* !NO_SHA */
  899. #ifndef NO_SHA256
  900. case WC_SHA256:
  901. blockSz = WC_SHA256_BLOCK_SIZE;
  902. blockBits = 6;
  903. macSz = WC_SHA256_DIGEST_SIZE;
  904. padSz = WC_SHA256_BLOCK_SIZE - WC_SHA256_PAD_SIZE + 1;
  905. break;
  906. #endif /* !NO_SHA256 */
  907. #ifdef WOLFSSL_SHA384
  908. case WC_SHA384:
  909. blockSz = WC_SHA384_BLOCK_SIZE;
  910. blockBits = 7;
  911. macSz = WC_SHA384_DIGEST_SIZE;
  912. padSz = WC_SHA384_BLOCK_SIZE - WC_SHA384_PAD_SIZE + 1;
  913. break;
  914. #endif /* WOLFSSL_SHA384 */
  915. #ifdef WOLFSSL_SHA512
  916. case WC_SHA512:
  917. blockSz = WC_SHA512_BLOCK_SIZE;
  918. blockBits = 7;
  919. macSz = WC_SHA512_DIGEST_SIZE;
  920. padSz = WC_SHA512_BLOCK_SIZE - WC_SHA512_PAD_SIZE + 1;
  921. break;
  922. #endif /* WOLFSSL_SHA512 */
  923. #ifdef HAVE_BLAKE2
  924. case WC_HASH_TYPE_BLAKE2B:
  925. blockSz = BLAKE2B_BLOCKBYTES;
  926. blockBits = 7;
  927. macSz = BLAKE2B_256;
  928. padSz = 0;
  929. break;
  930. #endif /* HAVE_BLAKE2 */
  931. default:
  932. return BAD_FUNC_ARG;
  933. }
  934. msgSz = sz - (1 + in[sz - 1] + macSz);
  935. /* Make negative result 0 */
  936. msgSz &= ~(0 - (msgSz >> 31));
  937. realSz = WOLFSSL_TLS_HMAC_INNER_SZ + msgSz;
  938. maxSz = WOLFSSL_TLS_HMAC_INNER_SZ + (sz - 1) - macSz;
  939. /* Calculate #blocks processed in HMAC for max and real data. */
  940. blocks = maxSz >> blockBits;
  941. blocks += ((maxSz + padSz) % blockSz) < padSz;
  942. msgBlocks = realSz >> blockBits;
  943. /* #Extra blocks to process. */
  944. blocks -= msgBlocks + ((((realSz + padSz) % blockSz) < padSz) ? 1 : 0);
  945. /* Calculate whole blocks. */
  946. msgBlocks--;
  947. ret = wc_HmacUpdate(hmac, header, WOLFSSL_TLS_HMAC_INNER_SZ);
  948. if (ret == 0) {
  949. /* Fill the rest of the block with any available data. */
  950. currSz = ctMaskLT(msgSz, blockSz) & msgSz;
  951. currSz |= ctMaskGTE(msgSz, blockSz) & blockSz;
  952. currSz -= WOLFSSL_TLS_HMAC_INNER_SZ;
  953. currSz &= ~(0 - (currSz >> 31));
  954. ret = wc_HmacUpdate(hmac, in, currSz);
  955. offset = currSz;
  956. }
  957. if (ret == 0) {
  958. /* Do the hash operations on a block basis. */
  959. for (i = 0; i < msgBlocks; i++, offset += blockSz) {
  960. ret = wc_HmacUpdate(hmac, in + offset, blockSz);
  961. if (ret != 0)
  962. break;
  963. }
  964. }
  965. if (ret == 0)
  966. ret = wc_HmacUpdate(hmac, in + offset, msgSz - offset);
  967. if (ret == 0)
  968. ret = wc_HmacFinal(hmac, digest);
  969. if (ret == 0) {
  970. /* Do the dummy hash operations. Do at least one. */
  971. for (i = 0; i < blocks + 1; i++) {
  972. ret = wc_HmacUpdate(hmac, dummy, blockSz);
  973. if (ret != 0)
  974. break;
  975. }
  976. }
  977. return ret;
  978. }
  979. #endif
  980. int TLS_hmac(WOLFSSL* ssl, byte* digest, const byte* in, word32 sz, int padSz,
  981. int content, int verify, int epochOrder)
  982. {
  983. Hmac hmac;
  984. byte myInner[WOLFSSL_TLS_HMAC_INNER_SZ];
  985. int ret = 0;
  986. const byte* macSecret = NULL;
  987. word32 hashSz = 0;
  988. if (ssl == NULL)
  989. return BAD_FUNC_ARG;
  990. #ifdef HAVE_TRUNCATED_HMAC
  991. hashSz = ssl->truncated_hmac ? (byte)TRUNCATED_HMAC_SZ
  992. : ssl->specs.hash_size;
  993. #else
  994. hashSz = ssl->specs.hash_size;
  995. #endif
  996. #ifdef HAVE_FUZZER
  997. /* Fuzz "in" buffer with sz to be used in HMAC algorithm */
  998. if (ssl->fuzzerCb) {
  999. if (verify && padSz >= 0) {
  1000. ssl->fuzzerCb(ssl, in, sz + hashSz + padSz + 1, FUZZ_HMAC,
  1001. ssl->fuzzerCtx);
  1002. }
  1003. else {
  1004. ssl->fuzzerCb(ssl, in, sz, FUZZ_HMAC, ssl->fuzzerCtx);
  1005. }
  1006. }
  1007. #endif
  1008. if (!ssl->options.dtls)
  1009. wolfSSL_SetTlsHmacInner(ssl, myInner, sz, content, verify);
  1010. else
  1011. wolfSSL_SetTlsHmacInner(ssl, myInner, sz, content, epochOrder);
  1012. ret = wc_HmacInit(&hmac, ssl->heap, ssl->devId);
  1013. if (ret != 0)
  1014. return ret;
  1015. #ifdef WOLFSSL_DTLS
  1016. if (ssl->options.dtls)
  1017. macSecret = wolfSSL_GetDtlsMacSecret(ssl, verify, epochOrder);
  1018. else
  1019. macSecret = wolfSSL_GetMacSecret(ssl, verify);
  1020. #else
  1021. macSecret = wolfSSL_GetMacSecret(ssl, verify);
  1022. #endif
  1023. ret = wc_HmacSetKey(&hmac, wolfSSL_GetHmacType(ssl),
  1024. macSecret,
  1025. ssl->specs.hash_size);
  1026. if (ret == 0) {
  1027. /* Constant time verification required. */
  1028. if (verify && padSz >= 0) {
  1029. #if !defined(WOLFSSL_NO_HASH_RAW) && !defined(HAVE_FIPS) && \
  1030. !defined(HAVE_SELFTEST)
  1031. #ifdef HAVE_BLAKE2
  1032. if (wolfSSL_GetHmacType(ssl) == WC_HASH_TYPE_BLAKE2B) {
  1033. ret = Hmac_UpdateFinal(&hmac, digest, in,
  1034. sz + hashSz + padSz + 1, myInner);
  1035. }
  1036. else
  1037. #endif
  1038. {
  1039. ret = Hmac_UpdateFinal_CT(&hmac, digest, in,
  1040. sz + hashSz + padSz + 1, hashSz, myInner);
  1041. }
  1042. #else
  1043. ret = Hmac_UpdateFinal(&hmac, digest, in, sz + hashSz + padSz + 1,
  1044. myInner);
  1045. #endif
  1046. }
  1047. else {
  1048. ret = wc_HmacUpdate(&hmac, myInner, sizeof(myInner));
  1049. if (ret == 0)
  1050. ret = wc_HmacUpdate(&hmac, in, sz); /* content */
  1051. if (ret == 0)
  1052. ret = wc_HmacFinal(&hmac, digest);
  1053. }
  1054. }
  1055. wc_HmacFree(&hmac);
  1056. return ret;
  1057. }
  1058. #endif /* WOLFSSL_AEAD_ONLY */
  1059. #endif /* !WOLFSSL_NO_TLS12 */
  1060. #ifdef HAVE_TLS_EXTENSIONS
  1061. /**
  1062. * The TLSX semaphore is used to calculate the size of the extensions to be sent
  1063. * from one peer to another.
  1064. */
  1065. /** Supports up to 72 flags. Increase as needed. */
  1066. #define SEMAPHORE_SIZE 9
  1067. /**
  1068. * Converts the extension type (id) to an index in the semaphore.
  1069. *
  1070. * Official reference for TLS extension types:
  1071. * http://www.iana.org/assignments/tls-extensiontype-values/tls-extensiontype-values.xml
  1072. *
  1073. * Motivation:
  1074. * Previously, we used the extension type itself as the index of that
  1075. * extension in the semaphore as the extension types were declared
  1076. * sequentially, but maintain a semaphore as big as the number of available
  1077. * extensions is no longer an option since the release of renegotiation_info.
  1078. *
  1079. * How to update:
  1080. * Assign extension types that extrapolate the number of available semaphores
  1081. * to the first available index going backwards in the semaphore array.
  1082. * When adding a new extension type that don't extrapolate the number of
  1083. * available semaphores, check for a possible collision with with a
  1084. * 'remapped' extension type.
  1085. */
  1086. static WC_INLINE word16 TLSX_ToSemaphore(word16 type)
  1087. {
  1088. switch (type) {
  1089. case TLSX_RENEGOTIATION_INFO: /* 0xFF01 */
  1090. return 63;
  1091. #ifdef WOLFSSL_QUIC
  1092. case TLSX_KEY_QUIC_TP_PARAMS_DRAFT: /* 0xffa5 */
  1093. return 64;
  1094. #endif
  1095. default:
  1096. if (type > 62) {
  1097. /* This message SHOULD only happens during the adding of
  1098. new TLS extensions in which its IANA number overflows
  1099. the current semaphore's range, or if its number already
  1100. is assigned to be used by another extension.
  1101. Use this check value for the new extension and decrement
  1102. the check value by one. */
  1103. WOLFSSL_MSG("### TLSX semaphore collision or overflow detected!");
  1104. }
  1105. }
  1106. return type;
  1107. }
  1108. /** Checks if a specific light (tls extension) is not set in the semaphore. */
  1109. #define IS_OFF(semaphore, light) \
  1110. (!(((semaphore)[(light) / 8] & (byte) (0x01 << ((light) % 8)))))
  1111. /** Turn on a specific light (tls extension) in the semaphore. */
  1112. /* the semaphore marks the extensions already written to the message */
  1113. #define TURN_ON(semaphore, light) \
  1114. ((semaphore)[(light) / 8] |= (byte) (0x01 << ((light) % 8)))
  1115. /** Turn off a specific light (tls extension) in the semaphore. */
  1116. #define TURN_OFF(semaphore, light) \
  1117. ((semaphore)[(light) / 8] &= (byte) ~(0x01 << ((light) % 8)))
  1118. /** Creates a new extension. */
  1119. static TLSX* TLSX_New(TLSX_Type type, const void* data, void* heap)
  1120. {
  1121. TLSX* extension = (TLSX*)XMALLOC(sizeof(TLSX), heap, DYNAMIC_TYPE_TLSX);
  1122. (void)heap;
  1123. if (extension) {
  1124. extension->type = type;
  1125. extension->data = (void*)data;
  1126. extension->resp = 0;
  1127. extension->next = NULL;
  1128. }
  1129. return extension;
  1130. }
  1131. /**
  1132. * Creates a new extension and pushes it to the provided list.
  1133. * Checks for duplicate extensions, keeps the newest.
  1134. */
  1135. int TLSX_Push(TLSX** list, TLSX_Type type, const void* data, void* heap)
  1136. {
  1137. TLSX* extension = TLSX_New(type, data, heap);
  1138. if (extension == NULL)
  1139. return MEMORY_E;
  1140. /* pushes the new extension on the list. */
  1141. extension->next = *list;
  1142. *list = extension;
  1143. /* remove duplicate extensions, there should be only one of each type. */
  1144. do {
  1145. if (extension->next && extension->next->type == type) {
  1146. TLSX *next = extension->next;
  1147. extension->next = next->next;
  1148. next->next = NULL;
  1149. TLSX_FreeAll(next, heap);
  1150. /* there is no way to occur more than
  1151. * two extensions of the same type.
  1152. */
  1153. break;
  1154. }
  1155. } while ((extension = extension->next));
  1156. return 0;
  1157. }
  1158. #ifdef WOLFSSL_TLS13
  1159. /**
  1160. * Creates a new extension and prepend it to the provided list.
  1161. * Checks for duplicate extensions, keeps the newest.
  1162. */
  1163. static int TLSX_Prepend(TLSX** list, TLSX_Type type, void* data, void* heap)
  1164. {
  1165. TLSX* extension = TLSX_New(type, data, heap);
  1166. TLSX* curr = *list;
  1167. if (extension == NULL)
  1168. return MEMORY_E;
  1169. /* remove duplicate extensions, there should be only one of each type. */
  1170. while (curr && curr->next) {
  1171. if (curr->next->type == type) {
  1172. TLSX *next = curr->next;
  1173. curr->next = next->next;
  1174. next->next = NULL;
  1175. TLSX_FreeAll(next, heap);
  1176. }
  1177. curr = curr->next;
  1178. }
  1179. if (curr)
  1180. curr->next = extension;
  1181. else
  1182. *list = extension;
  1183. return 0;
  1184. }
  1185. #endif /* WOLFSSL_TLS13 */
  1186. #ifndef NO_WOLFSSL_CLIENT
  1187. int TLSX_CheckUnsupportedExtension(WOLFSSL* ssl, TLSX_Type type);
  1188. int TLSX_CheckUnsupportedExtension(WOLFSSL* ssl, TLSX_Type type)
  1189. {
  1190. TLSX *extension = TLSX_Find(ssl->extensions, type);
  1191. if (!extension)
  1192. extension = TLSX_Find(ssl->ctx->extensions, type);
  1193. return extension == NULL;
  1194. }
  1195. int TLSX_HandleUnsupportedExtension(WOLFSSL* ssl);
  1196. int TLSX_HandleUnsupportedExtension(WOLFSSL* ssl)
  1197. {
  1198. SendAlert(ssl, alert_fatal, unsupported_extension);
  1199. WOLFSSL_ERROR_VERBOSE(UNSUPPORTED_EXTENSION);
  1200. return UNSUPPORTED_EXTENSION;
  1201. }
  1202. #else
  1203. #define TLSX_CheckUnsupportedExtension(ssl, type) 0
  1204. #define TLSX_HandleUnsupportedExtension(ssl) 0
  1205. #endif
  1206. #if !defined(NO_WOLFSSL_SERVER) || defined(WOLFSSL_TLS13)
  1207. /** Mark an extension to be sent back to the client. */
  1208. static void TLSX_SetResponse(WOLFSSL* ssl, TLSX_Type type)
  1209. {
  1210. TLSX *extension = TLSX_Find(ssl->extensions, type);
  1211. if (extension)
  1212. extension->resp = 1;
  1213. }
  1214. #endif
  1215. /******************************************************************************/
  1216. /* Application-Layer Protocol Negotiation */
  1217. /******************************************************************************/
  1218. #ifdef HAVE_ALPN
  1219. /** Creates a new ALPN object, providing protocol name to use. */
  1220. static ALPN* TLSX_ALPN_New(char *protocol_name, word16 protocol_nameSz,
  1221. void* heap)
  1222. {
  1223. ALPN *alpn;
  1224. WOLFSSL_ENTER("TLSX_ALPN_New");
  1225. if (protocol_name == NULL ||
  1226. protocol_nameSz > WOLFSSL_MAX_ALPN_PROTO_NAME_LEN) {
  1227. WOLFSSL_MSG("Invalid arguments");
  1228. return NULL;
  1229. }
  1230. alpn = (ALPN*)XMALLOC(sizeof(ALPN), heap, DYNAMIC_TYPE_TLSX);
  1231. if (alpn == NULL) {
  1232. WOLFSSL_MSG("Memory failure");
  1233. return NULL;
  1234. }
  1235. alpn->next = NULL;
  1236. alpn->negotiated = 0;
  1237. alpn->options = 0;
  1238. alpn->protocol_name = (char*)XMALLOC(protocol_nameSz + 1,
  1239. heap, DYNAMIC_TYPE_TLSX);
  1240. if (alpn->protocol_name == NULL) {
  1241. WOLFSSL_MSG("Memory failure");
  1242. XFREE(alpn, heap, DYNAMIC_TYPE_TLSX);
  1243. return NULL;
  1244. }
  1245. XMEMCPY(alpn->protocol_name, protocol_name, protocol_nameSz);
  1246. alpn->protocol_name[protocol_nameSz] = 0;
  1247. (void)heap;
  1248. return alpn;
  1249. }
  1250. /** Releases an ALPN object. */
  1251. static void TLSX_ALPN_Free(ALPN *alpn, void* heap)
  1252. {
  1253. (void)heap;
  1254. if (alpn == NULL)
  1255. return;
  1256. XFREE(alpn->protocol_name, heap, DYNAMIC_TYPE_TLSX);
  1257. XFREE(alpn, heap, DYNAMIC_TYPE_TLSX);
  1258. }
  1259. /** Releases all ALPN objects in the provided list. */
  1260. static void TLSX_ALPN_FreeAll(ALPN *list, void* heap)
  1261. {
  1262. ALPN* alpn;
  1263. while ((alpn = list)) {
  1264. list = alpn->next;
  1265. TLSX_ALPN_Free(alpn, heap);
  1266. }
  1267. }
  1268. /** Tells the buffered size of the ALPN objects in a list. */
  1269. static word16 TLSX_ALPN_GetSize(ALPN *list)
  1270. {
  1271. ALPN* alpn;
  1272. word16 length = OPAQUE16_LEN; /* list length */
  1273. while ((alpn = list)) {
  1274. list = alpn->next;
  1275. length++; /* protocol name length is on one byte */
  1276. length += (word16)XSTRLEN(alpn->protocol_name);
  1277. }
  1278. return length;
  1279. }
  1280. /** Writes the ALPN objects of a list in a buffer. */
  1281. static word16 TLSX_ALPN_Write(ALPN *list, byte *output)
  1282. {
  1283. ALPN* alpn;
  1284. word16 length = 0;
  1285. word16 offset = OPAQUE16_LEN; /* list length offset */
  1286. while ((alpn = list)) {
  1287. list = alpn->next;
  1288. length = (word16)XSTRLEN(alpn->protocol_name);
  1289. /* protocol name length */
  1290. output[offset++] = (byte)length;
  1291. /* protocol name value */
  1292. XMEMCPY(output + offset, alpn->protocol_name, length);
  1293. offset += length;
  1294. }
  1295. /* writing list length */
  1296. c16toa(offset - OPAQUE16_LEN, output);
  1297. return offset;
  1298. }
  1299. /** Finds a protocol name in the provided ALPN list */
  1300. static ALPN* TLSX_ALPN_Find(ALPN *list, char *protocol_name, word16 size)
  1301. {
  1302. ALPN *alpn;
  1303. if (list == NULL || protocol_name == NULL)
  1304. return NULL;
  1305. alpn = list;
  1306. while (alpn != NULL && (
  1307. (word16)XSTRLEN(alpn->protocol_name) != size ||
  1308. XSTRNCMP(alpn->protocol_name, protocol_name, size)))
  1309. alpn = alpn->next;
  1310. return alpn;
  1311. }
  1312. /** Set the ALPN matching client and server requirements */
  1313. static int TLSX_SetALPN(TLSX** extensions, const void* data, word16 size,
  1314. void* heap)
  1315. {
  1316. ALPN *alpn;
  1317. int ret;
  1318. if (extensions == NULL || data == NULL)
  1319. return BAD_FUNC_ARG;
  1320. alpn = TLSX_ALPN_New((char *)data, size, heap);
  1321. if (alpn == NULL) {
  1322. WOLFSSL_MSG("Memory failure");
  1323. return MEMORY_E;
  1324. }
  1325. alpn->negotiated = 1;
  1326. ret = TLSX_Push(extensions, TLSX_APPLICATION_LAYER_PROTOCOL, (void*)alpn,
  1327. heap);
  1328. if (ret != 0) {
  1329. TLSX_ALPN_Free(alpn, heap);
  1330. return ret;
  1331. }
  1332. return WOLFSSL_SUCCESS;
  1333. }
  1334. /** Parses a buffer of ALPN extensions and set the first one matching
  1335. * client and server requirements */
  1336. static int TLSX_ALPN_ParseAndSet(WOLFSSL *ssl, const byte *input, word16 length,
  1337. byte isRequest)
  1338. {
  1339. word16 size = 0, offset = 0, idx = 0;
  1340. int r = BUFFER_ERROR;
  1341. byte match = 0;
  1342. TLSX *extension;
  1343. ALPN *alpn = NULL, *list;
  1344. if (OPAQUE16_LEN > length)
  1345. return BUFFER_ERROR;
  1346. ato16(input, &size);
  1347. offset += OPAQUE16_LEN;
  1348. if (size == 0)
  1349. return BUFFER_ERROR;
  1350. extension = TLSX_Find(ssl->extensions, TLSX_APPLICATION_LAYER_PROTOCOL);
  1351. if (extension == NULL)
  1352. extension = TLSX_Find(ssl->ctx->extensions,
  1353. TLSX_APPLICATION_LAYER_PROTOCOL);
  1354. #if defined(OPENSSL_ALL) || defined(WOLFSSL_NGINX) || defined(WOLFSSL_HAPROXY)
  1355. if (ssl->alpnSelect != NULL && ssl->options.side == WOLFSSL_SERVER_END) {
  1356. const byte* out;
  1357. unsigned char outLen;
  1358. if (ssl->alpnSelect(ssl, &out, &outLen, input + offset, size,
  1359. ssl->alpnSelectArg) == 0) {
  1360. WOLFSSL_MSG("ALPN protocol match");
  1361. /* clears out all current ALPN extensions set */
  1362. TLSX_Remove(&ssl->extensions, TLSX_APPLICATION_LAYER_PROTOCOL, ssl->heap);
  1363. extension = NULL;
  1364. if (TLSX_UseALPN(&ssl->extensions, (char*)out, outLen, 0, ssl->heap)
  1365. == WOLFSSL_SUCCESS) {
  1366. extension = TLSX_Find(ssl->extensions,
  1367. TLSX_APPLICATION_LAYER_PROTOCOL);
  1368. }
  1369. }
  1370. }
  1371. #endif
  1372. if (extension == NULL || extension->data == NULL) {
  1373. return isRequest ? 0
  1374. : TLSX_HandleUnsupportedExtension(ssl);
  1375. }
  1376. /* validating alpn list length */
  1377. if (length != OPAQUE16_LEN + size)
  1378. return BUFFER_ERROR;
  1379. list = (ALPN*)extension->data;
  1380. /* keep the list sent by client */
  1381. if (isRequest) {
  1382. if (ssl->alpn_client_list != NULL)
  1383. XFREE(ssl->alpn_client_list, ssl->heap, DYNAMIC_TYPE_ALPN);
  1384. ssl->alpn_client_list = (char *)XMALLOC(size, ssl->heap,
  1385. DYNAMIC_TYPE_ALPN);
  1386. if (ssl->alpn_client_list == NULL)
  1387. return MEMORY_ERROR;
  1388. }
  1389. for (size = 0; offset < length; offset += size) {
  1390. size = input[offset++];
  1391. if (offset + size > length || size == 0)
  1392. return BUFFER_ERROR;
  1393. if (isRequest) {
  1394. XMEMCPY(ssl->alpn_client_list+idx, (char*)input + offset, size);
  1395. idx += size;
  1396. ssl->alpn_client_list[idx++] = ',';
  1397. }
  1398. if (!match) {
  1399. alpn = TLSX_ALPN_Find(list, (char*)input + offset, size);
  1400. if (alpn != NULL) {
  1401. WOLFSSL_MSG("ALPN protocol match");
  1402. match = 1;
  1403. /* skip reading other values if not required */
  1404. if (!isRequest)
  1405. break;
  1406. }
  1407. }
  1408. }
  1409. if (isRequest)
  1410. ssl->alpn_client_list[idx-1] = 0;
  1411. if (!match) {
  1412. WOLFSSL_MSG("No ALPN protocol match");
  1413. /* do nothing if no protocol match between client and server and option
  1414. is set to continue (like OpenSSL) */
  1415. if (list->options & WOLFSSL_ALPN_CONTINUE_ON_MISMATCH) {
  1416. WOLFSSL_MSG("Continue on mismatch");
  1417. return 0;
  1418. }
  1419. SendAlert(ssl, alert_fatal, no_application_protocol);
  1420. WOLFSSL_ERROR_VERBOSE(UNKNOWN_ALPN_PROTOCOL_NAME_E);
  1421. return UNKNOWN_ALPN_PROTOCOL_NAME_E;
  1422. }
  1423. /* set the matching negotiated protocol */
  1424. r = TLSX_SetALPN(&ssl->extensions,
  1425. alpn->protocol_name,
  1426. (word16)XSTRLEN(alpn->protocol_name),
  1427. ssl->heap);
  1428. if (r != WOLFSSL_SUCCESS) {
  1429. WOLFSSL_MSG("TLSX_SetALPN failed");
  1430. return BUFFER_ERROR;
  1431. }
  1432. /* reply to ALPN extension sent from client */
  1433. if (isRequest) {
  1434. #ifndef NO_WOLFSSL_SERVER
  1435. TLSX_SetResponse(ssl, TLSX_APPLICATION_LAYER_PROTOCOL);
  1436. #endif
  1437. }
  1438. return 0;
  1439. }
  1440. /** Add a protocol name to the list of accepted usable ones */
  1441. int TLSX_UseALPN(TLSX** extensions, const void* data, word16 size, byte options,
  1442. void* heap)
  1443. {
  1444. ALPN *alpn;
  1445. TLSX *extension;
  1446. int ret;
  1447. if (extensions == NULL || data == NULL)
  1448. return BAD_FUNC_ARG;
  1449. alpn = TLSX_ALPN_New((char *)data, size, heap);
  1450. if (alpn == NULL) {
  1451. WOLFSSL_MSG("Memory failure");
  1452. return MEMORY_E;
  1453. }
  1454. /* Set Options of ALPN */
  1455. alpn->options = options;
  1456. extension = TLSX_Find(*extensions, TLSX_APPLICATION_LAYER_PROTOCOL);
  1457. if (extension == NULL) {
  1458. ret = TLSX_Push(extensions, TLSX_APPLICATION_LAYER_PROTOCOL,
  1459. (void*)alpn, heap);
  1460. if (ret != 0) {
  1461. TLSX_ALPN_Free(alpn, heap);
  1462. return ret;
  1463. }
  1464. }
  1465. else {
  1466. /* push new ALPN object to extension data. */
  1467. alpn->next = (ALPN*)extension->data;
  1468. extension->data = (void*)alpn;
  1469. }
  1470. return WOLFSSL_SUCCESS;
  1471. }
  1472. /** Get the protocol name set by the server */
  1473. int TLSX_ALPN_GetRequest(TLSX* extensions, void** data, word16 *dataSz)
  1474. {
  1475. TLSX *extension;
  1476. ALPN *alpn;
  1477. if (extensions == NULL || data == NULL || dataSz == NULL)
  1478. return BAD_FUNC_ARG;
  1479. extension = TLSX_Find(extensions, TLSX_APPLICATION_LAYER_PROTOCOL);
  1480. if (extension == NULL) {
  1481. WOLFSSL_MSG("TLS extension not found");
  1482. WOLFSSL_ERROR_VERBOSE(WOLFSSL_ALPN_NOT_FOUND);
  1483. return WOLFSSL_ALPN_NOT_FOUND;
  1484. }
  1485. alpn = (ALPN *)extension->data;
  1486. if (alpn == NULL) {
  1487. WOLFSSL_MSG("ALPN extension not found");
  1488. *data = NULL;
  1489. *dataSz = 0;
  1490. WOLFSSL_ERROR_VERBOSE(WOLFSSL_FATAL_ERROR);
  1491. return WOLFSSL_FATAL_ERROR;
  1492. }
  1493. if (alpn->negotiated != 1) {
  1494. /* consider as an error */
  1495. if (alpn->options & WOLFSSL_ALPN_FAILED_ON_MISMATCH) {
  1496. WOLFSSL_MSG("No protocol match with peer -> Failed");
  1497. WOLFSSL_ERROR_VERBOSE(WOLFSSL_FATAL_ERROR);
  1498. return WOLFSSL_FATAL_ERROR;
  1499. }
  1500. /* continue without negotiated protocol */
  1501. WOLFSSL_MSG("No protocol match with peer -> Continue");
  1502. WOLFSSL_ERROR_VERBOSE(WOLFSSL_ALPN_NOT_FOUND);
  1503. return WOLFSSL_ALPN_NOT_FOUND;
  1504. }
  1505. if (alpn->next != NULL) {
  1506. WOLFSSL_MSG("Only one protocol name must be accepted");
  1507. WOLFSSL_ERROR_VERBOSE(WOLFSSL_FATAL_ERROR);
  1508. return WOLFSSL_FATAL_ERROR;
  1509. }
  1510. *data = alpn->protocol_name;
  1511. *dataSz = (word16)XSTRLEN((char*)*data);
  1512. return WOLFSSL_SUCCESS;
  1513. }
  1514. #define ALPN_FREE_ALL TLSX_ALPN_FreeAll
  1515. #define ALPN_GET_SIZE TLSX_ALPN_GetSize
  1516. #define ALPN_WRITE TLSX_ALPN_Write
  1517. #define ALPN_PARSE TLSX_ALPN_ParseAndSet
  1518. #else /* HAVE_ALPN */
  1519. #define ALPN_FREE_ALL(list, heap)
  1520. #define ALPN_GET_SIZE(list) 0
  1521. #define ALPN_WRITE(a, b) 0
  1522. #define ALPN_PARSE(a, b, c, d) 0
  1523. #endif /* HAVE_ALPN */
  1524. /******************************************************************************/
  1525. /* Server Name Indication */
  1526. /******************************************************************************/
  1527. #ifdef HAVE_SNI
  1528. /** Creates a new SNI object. */
  1529. static SNI* TLSX_SNI_New(byte type, const void* data, word16 size, void* heap)
  1530. {
  1531. SNI* sni = (SNI*)XMALLOC(sizeof(SNI), heap, DYNAMIC_TYPE_TLSX);
  1532. (void)heap;
  1533. if (sni) {
  1534. sni->type = type;
  1535. sni->next = NULL;
  1536. #ifndef NO_WOLFSSL_SERVER
  1537. sni->options = 0;
  1538. sni->status = WOLFSSL_SNI_NO_MATCH;
  1539. #endif
  1540. switch (sni->type) {
  1541. case WOLFSSL_SNI_HOST_NAME:
  1542. sni->data.host_name = (char*)XMALLOC(size + 1, heap,
  1543. DYNAMIC_TYPE_TLSX);
  1544. if (sni->data.host_name) {
  1545. XSTRNCPY(sni->data.host_name, (const char*)data, size);
  1546. sni->data.host_name[size] = '\0';
  1547. } else {
  1548. XFREE(sni, heap, DYNAMIC_TYPE_TLSX);
  1549. sni = NULL;
  1550. }
  1551. break;
  1552. default: /* invalid type */
  1553. XFREE(sni, heap, DYNAMIC_TYPE_TLSX);
  1554. sni = NULL;
  1555. }
  1556. }
  1557. return sni;
  1558. }
  1559. /** Releases a SNI object. */
  1560. static void TLSX_SNI_Free(SNI* sni, void* heap)
  1561. {
  1562. if (sni) {
  1563. switch (sni->type) {
  1564. case WOLFSSL_SNI_HOST_NAME:
  1565. XFREE(sni->data.host_name, heap, DYNAMIC_TYPE_TLSX);
  1566. break;
  1567. }
  1568. XFREE(sni, heap, DYNAMIC_TYPE_TLSX);
  1569. }
  1570. (void)heap;
  1571. }
  1572. /** Releases all SNI objects in the provided list. */
  1573. static void TLSX_SNI_FreeAll(SNI* list, void* heap)
  1574. {
  1575. SNI* sni;
  1576. while ((sni = list)) {
  1577. list = sni->next;
  1578. TLSX_SNI_Free(sni, heap);
  1579. }
  1580. }
  1581. /** Tells the buffered size of the SNI objects in a list. */
  1582. static word16 TLSX_SNI_GetSize(SNI* list)
  1583. {
  1584. SNI* sni;
  1585. word16 length = OPAQUE16_LEN; /* list length */
  1586. while ((sni = list)) {
  1587. list = sni->next;
  1588. length += ENUM_LEN + OPAQUE16_LEN; /* sni type + sni length */
  1589. switch (sni->type) {
  1590. case WOLFSSL_SNI_HOST_NAME:
  1591. length += (word16)XSTRLEN((char*)sni->data.host_name);
  1592. break;
  1593. }
  1594. }
  1595. return length;
  1596. }
  1597. /** Writes the SNI objects of a list in a buffer. */
  1598. static word16 TLSX_SNI_Write(SNI* list, byte* output)
  1599. {
  1600. SNI* sni;
  1601. word16 length = 0;
  1602. word16 offset = OPAQUE16_LEN; /* list length offset */
  1603. while ((sni = list)) {
  1604. list = sni->next;
  1605. output[offset++] = sni->type; /* sni type */
  1606. switch (sni->type) {
  1607. case WOLFSSL_SNI_HOST_NAME:
  1608. length = (word16)XSTRLEN((char*)sni->data.host_name);
  1609. c16toa(length, output + offset); /* sni length */
  1610. offset += OPAQUE16_LEN;
  1611. XMEMCPY(output + offset, sni->data.host_name, length);
  1612. offset += length;
  1613. break;
  1614. }
  1615. }
  1616. c16toa(offset - OPAQUE16_LEN, output); /* writing list length */
  1617. return offset;
  1618. }
  1619. /** Finds a SNI object in the provided list. */
  1620. static SNI* TLSX_SNI_Find(SNI *list, byte type)
  1621. {
  1622. SNI* sni = list;
  1623. while (sni && sni->type != type)
  1624. sni = sni->next;
  1625. return sni;
  1626. }
  1627. #if (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER))
  1628. /** Sets the status of a SNI object. */
  1629. static void TLSX_SNI_SetStatus(TLSX* extensions, byte type, byte status)
  1630. {
  1631. TLSX* extension = TLSX_Find(extensions, TLSX_SERVER_NAME);
  1632. SNI* sni = TLSX_SNI_Find(extension ? (SNI*)extension->data : NULL, type);
  1633. if (sni)
  1634. sni->status = status;
  1635. }
  1636. #endif
  1637. /** Gets the status of a SNI object. */
  1638. byte TLSX_SNI_Status(TLSX* extensions, byte type)
  1639. {
  1640. TLSX* extension = TLSX_Find(extensions, TLSX_SERVER_NAME);
  1641. SNI* sni = TLSX_SNI_Find(extension ? (SNI*)extension->data : NULL, type);
  1642. if (sni)
  1643. return sni->status;
  1644. return 0;
  1645. }
  1646. /** Parses a buffer of SNI extensions. */
  1647. static int TLSX_SNI_Parse(WOLFSSL* ssl, const byte* input, word16 length,
  1648. byte isRequest)
  1649. {
  1650. #ifndef NO_WOLFSSL_SERVER
  1651. word16 size = 0;
  1652. word16 offset = 0;
  1653. int cacheOnly = 0;
  1654. SNI *sni = NULL;
  1655. byte type;
  1656. int matchStat;
  1657. byte matched;
  1658. #endif
  1659. TLSX *extension = TLSX_Find(ssl->extensions, TLSX_SERVER_NAME);
  1660. if (!extension)
  1661. extension = TLSX_Find(ssl->ctx->extensions, TLSX_SERVER_NAME);
  1662. if (!isRequest) {
  1663. #ifndef NO_WOLFSSL_CLIENT
  1664. if (!extension || !extension->data)
  1665. return TLSX_HandleUnsupportedExtension(ssl);
  1666. if (length > 0)
  1667. return BUFFER_ERROR; /* SNI response MUST be empty. */
  1668. /* This call enables wolfSSL_SNI_GetRequest() to be called in the
  1669. * client side to fetch the used SNI. It will only work if the SNI
  1670. * was set at the SSL object level. Right now we only support one
  1671. * name type, WOLFSSL_SNI_HOST_NAME, but in the future, the
  1672. * inclusion of other name types will turn this method inaccurate,
  1673. * as the extension response doesn't contains information of which
  1674. * name was accepted.
  1675. */
  1676. TLSX_SNI_SetStatus(ssl->extensions, WOLFSSL_SNI_HOST_NAME,
  1677. WOLFSSL_SNI_REAL_MATCH);
  1678. return 0;
  1679. #endif
  1680. }
  1681. #ifndef NO_WOLFSSL_SERVER
  1682. if (!extension || !extension->data) {
  1683. /* This will keep SNI even though TLSX_UseSNI has not been called.
  1684. * Enable it so that the received sni is available to functions
  1685. * that use a custom callback when SNI is received.
  1686. */
  1687. #ifdef WOLFSSL_ALWAYS_KEEP_SNI
  1688. cacheOnly = 1;
  1689. #endif
  1690. if (ssl->ctx->sniRecvCb) {
  1691. cacheOnly = 1;
  1692. }
  1693. if (cacheOnly) {
  1694. WOLFSSL_MSG("Forcing SSL object to store SNI parameter");
  1695. }
  1696. else {
  1697. /* Skipping, SNI not enabled at server side. */
  1698. return 0;
  1699. }
  1700. }
  1701. if (OPAQUE16_LEN > length)
  1702. return BUFFER_ERROR;
  1703. ato16(input, &size);
  1704. offset += OPAQUE16_LEN;
  1705. /* validating sni list length */
  1706. if (length != OPAQUE16_LEN + size || size == 0)
  1707. return BUFFER_ERROR;
  1708. /* SNI was badly specified and only one type is now recognized and allowed.
  1709. * Only one SNI value per type (RFC6066), so, no loop. */
  1710. type = input[offset++];
  1711. if (type != WOLFSSL_SNI_HOST_NAME)
  1712. return BUFFER_ERROR;
  1713. if (offset + OPAQUE16_LEN > length)
  1714. return BUFFER_ERROR;
  1715. ato16(input + offset, &size);
  1716. offset += OPAQUE16_LEN;
  1717. if (offset + size != length || size == 0)
  1718. return BUFFER_ERROR;
  1719. if (!cacheOnly && !(sni = TLSX_SNI_Find((SNI*)extension->data, type)))
  1720. return 0; /* not using this type of SNI. */
  1721. #ifdef WOLFSSL_TLS13
  1722. /* Don't process the second ClientHello SNI extension if there
  1723. * was problems with the first.
  1724. */
  1725. if (!cacheOnly && sni->status != 0)
  1726. return 0;
  1727. #endif
  1728. matched = cacheOnly || (XSTRLEN(sni->data.host_name) == size &&
  1729. XSTRNCMP(sni->data.host_name, (const char*)input + offset, size) == 0);
  1730. if (matched || sni->options & WOLFSSL_SNI_ANSWER_ON_MISMATCH) {
  1731. int r = TLSX_UseSNI(&ssl->extensions, type, input + offset, size,
  1732. ssl->heap);
  1733. if (r != WOLFSSL_SUCCESS)
  1734. return r; /* throws error. */
  1735. if (cacheOnly) {
  1736. WOLFSSL_MSG("Forcing storage of SNI, Fake match");
  1737. matchStat = WOLFSSL_SNI_FORCE_KEEP;
  1738. }
  1739. else if (matched) {
  1740. WOLFSSL_MSG("SNI did match!");
  1741. matchStat = WOLFSSL_SNI_REAL_MATCH;
  1742. }
  1743. else {
  1744. WOLFSSL_MSG("fake SNI match from ANSWER_ON_MISMATCH");
  1745. matchStat = WOLFSSL_SNI_FAKE_MATCH;
  1746. }
  1747. TLSX_SNI_SetStatus(ssl->extensions, type, (byte)matchStat);
  1748. if (!cacheOnly)
  1749. TLSX_SetResponse(ssl, TLSX_SERVER_NAME);
  1750. }
  1751. else if (!(sni->options & WOLFSSL_SNI_CONTINUE_ON_MISMATCH)) {
  1752. SendAlert(ssl, alert_fatal, unrecognized_name);
  1753. WOLFSSL_ERROR_VERBOSE(UNKNOWN_SNI_HOST_NAME_E);
  1754. return UNKNOWN_SNI_HOST_NAME_E;
  1755. }
  1756. #else
  1757. (void)input;
  1758. #endif
  1759. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  1760. (void)length;
  1761. #endif
  1762. return 0;
  1763. }
  1764. static int TLSX_SNI_VerifyParse(WOLFSSL* ssl, byte isRequest)
  1765. {
  1766. (void)ssl;
  1767. if (isRequest) {
  1768. #ifndef NO_WOLFSSL_SERVER
  1769. TLSX* ctx_ext = TLSX_Find(ssl->ctx->extensions, TLSX_SERVER_NAME);
  1770. TLSX* ssl_ext = TLSX_Find(ssl->extensions, TLSX_SERVER_NAME);
  1771. SNI* ctx_sni = ctx_ext ? (SNI*)ctx_ext->data : NULL;
  1772. SNI* ssl_sni = ssl_ext ? (SNI*)ssl_ext->data : NULL;
  1773. SNI* sni = NULL;
  1774. for (; ctx_sni; ctx_sni = ctx_sni->next) {
  1775. if (ctx_sni->options & WOLFSSL_SNI_ABORT_ON_ABSENCE) {
  1776. sni = TLSX_SNI_Find(ssl_sni, ctx_sni->type);
  1777. if (sni) {
  1778. if (sni->status != WOLFSSL_SNI_NO_MATCH)
  1779. continue;
  1780. /* if ssl level overrides ctx level, it is ok. */
  1781. if ((sni->options & WOLFSSL_SNI_ABORT_ON_ABSENCE) == 0)
  1782. continue;
  1783. }
  1784. SendAlert(ssl, alert_fatal, handshake_failure);
  1785. WOLFSSL_ERROR_VERBOSE(SNI_ABSENT_ERROR);
  1786. return SNI_ABSENT_ERROR;
  1787. }
  1788. }
  1789. for (; ssl_sni; ssl_sni = ssl_sni->next) {
  1790. if (ssl_sni->options & WOLFSSL_SNI_ABORT_ON_ABSENCE) {
  1791. if (ssl_sni->status != WOLFSSL_SNI_NO_MATCH)
  1792. continue;
  1793. SendAlert(ssl, alert_fatal, handshake_failure);
  1794. WOLFSSL_ERROR_VERBOSE(SNI_ABSENT_ERROR);
  1795. return SNI_ABSENT_ERROR;
  1796. }
  1797. }
  1798. #endif /* NO_WOLFSSL_SERVER */
  1799. }
  1800. return 0;
  1801. }
  1802. int TLSX_UseSNI(TLSX** extensions, byte type, const void* data, word16 size,
  1803. void* heap)
  1804. {
  1805. TLSX* extension;
  1806. SNI* sni = NULL;
  1807. if (extensions == NULL || data == NULL)
  1808. return BAD_FUNC_ARG;
  1809. if ((sni = TLSX_SNI_New(type, data, size, heap)) == NULL)
  1810. return MEMORY_E;
  1811. extension = TLSX_Find(*extensions, TLSX_SERVER_NAME);
  1812. if (!extension) {
  1813. int ret = TLSX_Push(extensions, TLSX_SERVER_NAME, (void*)sni, heap);
  1814. if (ret != 0) {
  1815. TLSX_SNI_Free(sni, heap);
  1816. return ret;
  1817. }
  1818. }
  1819. else {
  1820. /* push new SNI object to extension data. */
  1821. sni->next = (SNI*)extension->data;
  1822. extension->data = (void*)sni;
  1823. /* remove duplicate SNI, there should be only one of each type. */
  1824. do {
  1825. if (sni->next && sni->next->type == type) {
  1826. SNI* next = sni->next;
  1827. sni->next = next->next;
  1828. TLSX_SNI_Free(next, heap);
  1829. /* there is no way to occur more than
  1830. * two SNIs of the same type.
  1831. */
  1832. break;
  1833. }
  1834. } while ((sni = sni->next));
  1835. }
  1836. return WOLFSSL_SUCCESS;
  1837. }
  1838. #ifndef NO_WOLFSSL_SERVER
  1839. /** Tells the SNI requested by the client. */
  1840. word16 TLSX_SNI_GetRequest(TLSX* extensions, byte type, void** data)
  1841. {
  1842. TLSX* extension = TLSX_Find(extensions, TLSX_SERVER_NAME);
  1843. SNI* sni = TLSX_SNI_Find(extension ? (SNI*)extension->data : NULL, type);
  1844. if (sni && sni->status != WOLFSSL_SNI_NO_MATCH) {
  1845. switch (sni->type) {
  1846. case WOLFSSL_SNI_HOST_NAME:
  1847. if (data) {
  1848. *data = sni->data.host_name;
  1849. return (word16)XSTRLEN((char*)*data);
  1850. }
  1851. }
  1852. }
  1853. return 0;
  1854. }
  1855. /** Sets the options for a SNI object. */
  1856. void TLSX_SNI_SetOptions(TLSX* extensions, byte type, byte options)
  1857. {
  1858. TLSX* extension = TLSX_Find(extensions, TLSX_SERVER_NAME);
  1859. SNI* sni = TLSX_SNI_Find(extension ? (SNI*)extension->data : NULL, type);
  1860. if (sni)
  1861. sni->options = options;
  1862. }
  1863. /** Retrieves a SNI request from a client hello buffer. */
  1864. int TLSX_SNI_GetFromBuffer(const byte* clientHello, word32 helloSz,
  1865. byte type, byte* sni, word32* inOutSz)
  1866. {
  1867. word32 offset = 0;
  1868. word32 len32 = 0;
  1869. word16 len16 = 0;
  1870. if (helloSz < RECORD_HEADER_SZ + HANDSHAKE_HEADER_SZ + CLIENT_HELLO_FIRST)
  1871. return INCOMPLETE_DATA;
  1872. /* TLS record header */
  1873. if ((enum ContentType) clientHello[offset++] != handshake) {
  1874. /* checking for SSLv2.0 client hello according to: */
  1875. /* http://tools.ietf.org/html/rfc4346#appendix-E.1 */
  1876. if ((enum HandShakeType) clientHello[++offset] == client_hello) {
  1877. offset += ENUM_LEN + VERSION_SZ; /* skip version */
  1878. ato16(clientHello + offset, &len16);
  1879. offset += OPAQUE16_LEN;
  1880. if (len16 % 3) /* cipher_spec_length must be multiple of 3 */
  1881. return BUFFER_ERROR;
  1882. ato16(clientHello + offset, &len16);
  1883. /* Returning SNI_UNSUPPORTED do not increment offset here */
  1884. if (len16 != 0) /* session_id_length must be 0 */
  1885. return BUFFER_ERROR;
  1886. WOLFSSL_ERROR_VERBOSE(SNI_UNSUPPORTED);
  1887. return SNI_UNSUPPORTED;
  1888. }
  1889. return BUFFER_ERROR;
  1890. }
  1891. if (clientHello[offset++] != SSLv3_MAJOR)
  1892. return BUFFER_ERROR;
  1893. if (clientHello[offset++] < TLSv1_MINOR) {
  1894. WOLFSSL_ERROR_VERBOSE(SNI_UNSUPPORTED);
  1895. return SNI_UNSUPPORTED;
  1896. }
  1897. ato16(clientHello + offset, &len16);
  1898. offset += OPAQUE16_LEN;
  1899. if (offset + len16 > helloSz)
  1900. return INCOMPLETE_DATA;
  1901. /* Handshake header */
  1902. if ((enum HandShakeType) clientHello[offset] != client_hello)
  1903. return BUFFER_ERROR;
  1904. c24to32(clientHello + offset + 1, &len32);
  1905. offset += HANDSHAKE_HEADER_SZ;
  1906. if (offset + len32 > helloSz)
  1907. return BUFFER_ERROR;
  1908. /* client hello */
  1909. offset += VERSION_SZ + RAN_LEN; /* version, random */
  1910. if (helloSz < offset + clientHello[offset])
  1911. return BUFFER_ERROR;
  1912. offset += ENUM_LEN + clientHello[offset]; /* skip session id */
  1913. /* cypher suites */
  1914. if (helloSz < offset + OPAQUE16_LEN)
  1915. return BUFFER_ERROR;
  1916. ato16(clientHello + offset, &len16);
  1917. offset += OPAQUE16_LEN;
  1918. if (helloSz < offset + len16)
  1919. return BUFFER_ERROR;
  1920. offset += len16; /* skip cypher suites */
  1921. /* compression methods */
  1922. if (helloSz < offset + 1)
  1923. return BUFFER_ERROR;
  1924. if (helloSz < offset + clientHello[offset])
  1925. return BUFFER_ERROR;
  1926. offset += ENUM_LEN + clientHello[offset]; /* skip compression methods */
  1927. /* extensions */
  1928. if (helloSz < offset + OPAQUE16_LEN)
  1929. return 0; /* no extensions in client hello. */
  1930. ato16(clientHello + offset, &len16);
  1931. offset += OPAQUE16_LEN;
  1932. if (helloSz < offset + len16)
  1933. return BUFFER_ERROR;
  1934. while (len16 >= OPAQUE16_LEN + OPAQUE16_LEN) {
  1935. word16 extType;
  1936. word16 extLen;
  1937. ato16(clientHello + offset, &extType);
  1938. offset += OPAQUE16_LEN;
  1939. ato16(clientHello + offset, &extLen);
  1940. offset += OPAQUE16_LEN;
  1941. if (helloSz < offset + extLen)
  1942. return BUFFER_ERROR;
  1943. if (extType != TLSX_SERVER_NAME) {
  1944. offset += extLen; /* skip extension */
  1945. } else {
  1946. word16 listLen;
  1947. ato16(clientHello + offset, &listLen);
  1948. offset += OPAQUE16_LEN;
  1949. if (helloSz < offset + listLen)
  1950. return BUFFER_ERROR;
  1951. while (listLen > ENUM_LEN + OPAQUE16_LEN) {
  1952. byte sniType = clientHello[offset++];
  1953. word16 sniLen;
  1954. ato16(clientHello + offset, &sniLen);
  1955. offset += OPAQUE16_LEN;
  1956. if (helloSz < offset + sniLen)
  1957. return BUFFER_ERROR;
  1958. if (sniType != type) {
  1959. offset += sniLen;
  1960. listLen -= min(ENUM_LEN + OPAQUE16_LEN + sniLen, listLen);
  1961. continue;
  1962. }
  1963. *inOutSz = min(sniLen, *inOutSz);
  1964. XMEMCPY(sni, clientHello + offset, *inOutSz);
  1965. return WOLFSSL_SUCCESS;
  1966. }
  1967. }
  1968. len16 -= min(2 * OPAQUE16_LEN + extLen, len16);
  1969. }
  1970. return len16 ? BUFFER_ERROR : 0;
  1971. }
  1972. #endif
  1973. #define SNI_FREE_ALL TLSX_SNI_FreeAll
  1974. #define SNI_GET_SIZE TLSX_SNI_GetSize
  1975. #define SNI_WRITE TLSX_SNI_Write
  1976. #define SNI_PARSE TLSX_SNI_Parse
  1977. #define SNI_VERIFY_PARSE TLSX_SNI_VerifyParse
  1978. #else
  1979. #define SNI_FREE_ALL(list, heap)
  1980. #define SNI_GET_SIZE(list) 0
  1981. #define SNI_WRITE(a, b) 0
  1982. #define SNI_PARSE(a, b, c, d) 0
  1983. #define SNI_VERIFY_PARSE(a, b) 0
  1984. #endif /* HAVE_SNI */
  1985. /******************************************************************************/
  1986. /* Trusted CA Key Indication */
  1987. /******************************************************************************/
  1988. #ifdef HAVE_TRUSTED_CA
  1989. /** Creates a new TCA object. */
  1990. static TCA* TLSX_TCA_New(byte type, const byte* id, word16 idSz, void* heap)
  1991. {
  1992. TCA* tca = (TCA*)XMALLOC(sizeof(TCA), heap, DYNAMIC_TYPE_TLSX);
  1993. if (tca) {
  1994. XMEMSET(tca, 0, sizeof(TCA));
  1995. tca->type = type;
  1996. switch (type) {
  1997. case WOLFSSL_TRUSTED_CA_PRE_AGREED:
  1998. break;
  1999. #ifndef NO_SHA
  2000. case WOLFSSL_TRUSTED_CA_KEY_SHA1:
  2001. case WOLFSSL_TRUSTED_CA_CERT_SHA1:
  2002. if (idSz == WC_SHA_DIGEST_SIZE &&
  2003. (tca->id =
  2004. (byte*)XMALLOC(idSz, heap, DYNAMIC_TYPE_TLSX))) {
  2005. XMEMCPY(tca->id, id, idSz);
  2006. tca->idSz = idSz;
  2007. }
  2008. else {
  2009. XFREE(tca, heap, DYNAMIC_TYPE_TLSX);
  2010. tca = NULL;
  2011. }
  2012. break;
  2013. #endif
  2014. case WOLFSSL_TRUSTED_CA_X509_NAME:
  2015. if (idSz > 0 &&
  2016. (tca->id =
  2017. (byte*)XMALLOC(idSz, heap, DYNAMIC_TYPE_TLSX))) {
  2018. XMEMCPY(tca->id, id, idSz);
  2019. tca->idSz = idSz;
  2020. }
  2021. else {
  2022. XFREE(tca, heap, DYNAMIC_TYPE_TLSX);
  2023. tca = NULL;
  2024. }
  2025. break;
  2026. default: /* invalid type */
  2027. XFREE(tca, heap, DYNAMIC_TYPE_TLSX);
  2028. tca = NULL;
  2029. }
  2030. }
  2031. (void)heap;
  2032. return tca;
  2033. }
  2034. /** Releases a TCA object. */
  2035. static void TLSX_TCA_Free(TCA* tca, void* heap)
  2036. {
  2037. (void)heap;
  2038. if (tca) {
  2039. if (tca->id)
  2040. XFREE(tca->id, heap, DYNAMIC_TYPE_TLSX);
  2041. XFREE(tca, heap, DYNAMIC_TYPE_TLSX);
  2042. }
  2043. }
  2044. /** Releases all TCA objects in the provided list. */
  2045. static void TLSX_TCA_FreeAll(TCA* list, void* heap)
  2046. {
  2047. TCA* tca;
  2048. while ((tca = list)) {
  2049. list = tca->next;
  2050. TLSX_TCA_Free(tca, heap);
  2051. }
  2052. }
  2053. /** Tells the buffered size of the TCA objects in a list. */
  2054. static word16 TLSX_TCA_GetSize(TCA* list)
  2055. {
  2056. TCA* tca;
  2057. word16 length = OPAQUE16_LEN; /* list length */
  2058. while ((tca = list)) {
  2059. list = tca->next;
  2060. length += ENUM_LEN; /* tca type */
  2061. switch (tca->type) {
  2062. case WOLFSSL_TRUSTED_CA_PRE_AGREED:
  2063. break;
  2064. case WOLFSSL_TRUSTED_CA_KEY_SHA1:
  2065. case WOLFSSL_TRUSTED_CA_CERT_SHA1:
  2066. length += tca->idSz;
  2067. break;
  2068. case WOLFSSL_TRUSTED_CA_X509_NAME:
  2069. length += OPAQUE16_LEN + tca->idSz;
  2070. break;
  2071. }
  2072. }
  2073. return length;
  2074. }
  2075. /** Writes the TCA objects of a list in a buffer. */
  2076. static word16 TLSX_TCA_Write(TCA* list, byte* output)
  2077. {
  2078. TCA* tca;
  2079. word16 offset = OPAQUE16_LEN; /* list length offset */
  2080. while ((tca = list)) {
  2081. list = tca->next;
  2082. output[offset++] = tca->type; /* tca type */
  2083. switch (tca->type) {
  2084. case WOLFSSL_TRUSTED_CA_PRE_AGREED:
  2085. break;
  2086. #ifndef NO_SHA
  2087. case WOLFSSL_TRUSTED_CA_KEY_SHA1:
  2088. case WOLFSSL_TRUSTED_CA_CERT_SHA1:
  2089. if (tca->id != NULL) {
  2090. XMEMCPY(output + offset, tca->id, tca->idSz);
  2091. offset += tca->idSz;
  2092. }
  2093. else {
  2094. /* ID missing. Set to an empty string. */
  2095. c16toa(0, output + offset);
  2096. offset += OPAQUE16_LEN;
  2097. }
  2098. break;
  2099. #endif
  2100. case WOLFSSL_TRUSTED_CA_X509_NAME:
  2101. if (tca->id != NULL) {
  2102. c16toa(tca->idSz, output + offset); /* tca length */
  2103. offset += OPAQUE16_LEN;
  2104. XMEMCPY(output + offset, tca->id, tca->idSz);
  2105. offset += tca->idSz;
  2106. }
  2107. else {
  2108. /* ID missing. Set to an empty string. */
  2109. c16toa(0, output + offset);
  2110. offset += OPAQUE16_LEN;
  2111. }
  2112. break;
  2113. default:
  2114. /* ID unknown. Set to an empty string. */
  2115. c16toa(0, output + offset);
  2116. offset += OPAQUE16_LEN;
  2117. }
  2118. }
  2119. c16toa(offset - OPAQUE16_LEN, output); /* writing list length */
  2120. return offset;
  2121. }
  2122. #ifndef NO_WOLFSSL_SERVER
  2123. static TCA* TLSX_TCA_Find(TCA *list, byte type, const byte* id, word16 idSz)
  2124. {
  2125. TCA* tca = list;
  2126. while (tca && tca->type != type && type != WOLFSSL_TRUSTED_CA_PRE_AGREED &&
  2127. idSz != tca->idSz && !XMEMCMP(id, tca->id, idSz))
  2128. tca = tca->next;
  2129. return tca;
  2130. }
  2131. #endif /* NO_WOLFSSL_SERVER */
  2132. /** Parses a buffer of TCA extensions. */
  2133. static int TLSX_TCA_Parse(WOLFSSL* ssl, const byte* input, word16 length,
  2134. byte isRequest)
  2135. {
  2136. #ifndef NO_WOLFSSL_SERVER
  2137. word16 size = 0;
  2138. word16 offset = 0;
  2139. #endif
  2140. TLSX *extension = TLSX_Find(ssl->extensions, TLSX_TRUSTED_CA_KEYS);
  2141. if (!extension)
  2142. extension = TLSX_Find(ssl->ctx->extensions, TLSX_TRUSTED_CA_KEYS);
  2143. if (!isRequest) {
  2144. #ifndef NO_WOLFSSL_CLIENT
  2145. if (!extension || !extension->data)
  2146. return TLSX_HandleUnsupportedExtension(ssl);
  2147. if (length > 0)
  2148. return BUFFER_ERROR; /* TCA response MUST be empty. */
  2149. /* Set the flag that we're good for keys */
  2150. TLSX_SetResponse(ssl, TLSX_TRUSTED_CA_KEYS);
  2151. return 0;
  2152. #endif
  2153. }
  2154. #ifndef NO_WOLFSSL_SERVER
  2155. if (!extension || !extension->data) {
  2156. /* Skipping, TCA not enabled at server side. */
  2157. return 0;
  2158. }
  2159. if (OPAQUE16_LEN > length)
  2160. return BUFFER_ERROR;
  2161. ato16(input, &size);
  2162. offset += OPAQUE16_LEN;
  2163. /* validating tca list length */
  2164. if (length != OPAQUE16_LEN + size)
  2165. return BUFFER_ERROR;
  2166. for (size = 0; offset < length; offset += size) {
  2167. TCA *tca = NULL;
  2168. byte type;
  2169. const byte* id = NULL;
  2170. word16 idSz = 0;
  2171. if (offset + ENUM_LEN > length)
  2172. return BUFFER_ERROR;
  2173. type = input[offset++];
  2174. switch (type) {
  2175. case WOLFSSL_TRUSTED_CA_PRE_AGREED:
  2176. break;
  2177. #ifndef NO_SHA
  2178. case WOLFSSL_TRUSTED_CA_KEY_SHA1:
  2179. case WOLFSSL_TRUSTED_CA_CERT_SHA1:
  2180. if (offset + WC_SHA_DIGEST_SIZE > length)
  2181. return BUFFER_ERROR;
  2182. idSz = WC_SHA_DIGEST_SIZE;
  2183. id = input + offset;
  2184. offset += idSz;
  2185. break;
  2186. #endif
  2187. case WOLFSSL_TRUSTED_CA_X509_NAME:
  2188. if (offset + OPAQUE16_LEN > length)
  2189. return BUFFER_ERROR;
  2190. ato16(input + offset, &idSz);
  2191. offset += OPAQUE16_LEN;
  2192. if ((offset > length) || (idSz > length - offset))
  2193. return BUFFER_ERROR;
  2194. id = input + offset;
  2195. offset += idSz;
  2196. break;
  2197. default:
  2198. WOLFSSL_ERROR_VERBOSE(TCA_INVALID_ID_TYPE);
  2199. return TCA_INVALID_ID_TYPE;
  2200. }
  2201. /* Find the type/ID in the TCA list. */
  2202. tca = TLSX_TCA_Find((TCA*)extension->data, type, id, idSz);
  2203. if (tca != NULL) {
  2204. /* Found it. Set the response flag and break out of the loop. */
  2205. TLSX_SetResponse(ssl, TLSX_TRUSTED_CA_KEYS);
  2206. break;
  2207. }
  2208. }
  2209. #else
  2210. (void)input;
  2211. #endif
  2212. return 0;
  2213. }
  2214. /* Checks to see if the server sent a response for the TCA. */
  2215. static int TLSX_TCA_VerifyParse(WOLFSSL* ssl, byte isRequest)
  2216. {
  2217. (void)ssl;
  2218. if (!isRequest) {
  2219. #ifndef NO_WOLFSSL_CLIENT
  2220. TLSX* extension = TLSX_Find(ssl->extensions, TLSX_TRUSTED_CA_KEYS);
  2221. if (extension && !extension->resp) {
  2222. SendAlert(ssl, alert_fatal, handshake_failure);
  2223. WOLFSSL_ERROR_VERBOSE(TCA_ABSENT_ERROR);
  2224. return TCA_ABSENT_ERROR;
  2225. }
  2226. #endif /* NO_WOLFSSL_CLIENT */
  2227. }
  2228. return 0;
  2229. }
  2230. int TLSX_UseTrustedCA(TLSX** extensions, byte type,
  2231. const byte* id, word16 idSz, void* heap)
  2232. {
  2233. TLSX* extension;
  2234. TCA* tca = NULL;
  2235. if (extensions == NULL)
  2236. return BAD_FUNC_ARG;
  2237. if ((tca = TLSX_TCA_New(type, id, idSz, heap)) == NULL)
  2238. return MEMORY_E;
  2239. extension = TLSX_Find(*extensions, TLSX_TRUSTED_CA_KEYS);
  2240. if (!extension) {
  2241. int ret = TLSX_Push(extensions, TLSX_TRUSTED_CA_KEYS, (void*)tca, heap);
  2242. if (ret != 0) {
  2243. TLSX_TCA_Free(tca, heap);
  2244. return ret;
  2245. }
  2246. }
  2247. else {
  2248. /* push new TCA object to extension data. */
  2249. tca->next = (TCA*)extension->data;
  2250. extension->data = (void*)tca;
  2251. }
  2252. return WOLFSSL_SUCCESS;
  2253. }
  2254. #define TCA_FREE_ALL TLSX_TCA_FreeAll
  2255. #define TCA_GET_SIZE TLSX_TCA_GetSize
  2256. #define TCA_WRITE TLSX_TCA_Write
  2257. #define TCA_PARSE TLSX_TCA_Parse
  2258. #define TCA_VERIFY_PARSE TLSX_TCA_VerifyParse
  2259. #else /* HAVE_TRUSTED_CA */
  2260. #define TCA_FREE_ALL(list, heap)
  2261. #define TCA_GET_SIZE(list) 0
  2262. #define TCA_WRITE(a, b) 0
  2263. #define TCA_PARSE(a, b, c, d) 0
  2264. #define TCA_VERIFY_PARSE(a, b) 0
  2265. #endif /* HAVE_TRUSTED_CA */
  2266. /******************************************************************************/
  2267. /* Max Fragment Length Negotiation */
  2268. /******************************************************************************/
  2269. #ifdef HAVE_MAX_FRAGMENT
  2270. static word16 TLSX_MFL_Write(byte* data, byte* output)
  2271. {
  2272. output[0] = data[0];
  2273. return ENUM_LEN;
  2274. }
  2275. static int TLSX_MFL_Parse(WOLFSSL* ssl, const byte* input, word16 length,
  2276. byte isRequest)
  2277. {
  2278. if (length != ENUM_LEN)
  2279. return BUFFER_ERROR;
  2280. #ifdef WOLFSSL_OLD_UNSUPPORTED_EXTENSION
  2281. (void) isRequest;
  2282. #else
  2283. if (!isRequest)
  2284. if (TLSX_CheckUnsupportedExtension(ssl, TLSX_MAX_FRAGMENT_LENGTH))
  2285. return TLSX_HandleUnsupportedExtension(ssl);
  2286. #endif
  2287. switch (*input) {
  2288. case WOLFSSL_MFL_2_8 : ssl->max_fragment = 256; break;
  2289. case WOLFSSL_MFL_2_9 : ssl->max_fragment = 512; break;
  2290. case WOLFSSL_MFL_2_10: ssl->max_fragment = 1024; break;
  2291. case WOLFSSL_MFL_2_11: ssl->max_fragment = 2048; break;
  2292. case WOLFSSL_MFL_2_12: ssl->max_fragment = 4096; break;
  2293. case WOLFSSL_MFL_2_13: ssl->max_fragment = 8192; break;
  2294. default:
  2295. SendAlert(ssl, alert_fatal, illegal_parameter);
  2296. WOLFSSL_ERROR_VERBOSE(UNKNOWN_MAX_FRAG_LEN_E);
  2297. return UNKNOWN_MAX_FRAG_LEN_E;
  2298. }
  2299. #ifndef NO_WOLFSSL_SERVER
  2300. if (isRequest) {
  2301. int ret = TLSX_UseMaxFragment(&ssl->extensions, *input, ssl->heap);
  2302. if (ret != WOLFSSL_SUCCESS)
  2303. return ret; /* throw error */
  2304. TLSX_SetResponse(ssl, TLSX_MAX_FRAGMENT_LENGTH);
  2305. }
  2306. #endif
  2307. return 0;
  2308. }
  2309. int TLSX_UseMaxFragment(TLSX** extensions, byte mfl, void* heap)
  2310. {
  2311. byte* data = NULL;
  2312. int ret = 0;
  2313. if (extensions == NULL || mfl < WOLFSSL_MFL_MIN || mfl > WOLFSSL_MFL_MAX)
  2314. return BAD_FUNC_ARG;
  2315. data = (byte*)XMALLOC(ENUM_LEN, heap, DYNAMIC_TYPE_TLSX);
  2316. if (data == NULL)
  2317. return MEMORY_E;
  2318. data[0] = mfl;
  2319. ret = TLSX_Push(extensions, TLSX_MAX_FRAGMENT_LENGTH, data, heap);
  2320. if (ret != 0) {
  2321. XFREE(data, heap, DYNAMIC_TYPE_TLSX);
  2322. return ret;
  2323. }
  2324. return WOLFSSL_SUCCESS;
  2325. }
  2326. #define MFL_FREE_ALL(data, heap) XFREE(data, (heap), DYNAMIC_TYPE_TLSX)
  2327. #define MFL_GET_SIZE(data) ENUM_LEN
  2328. #define MFL_WRITE TLSX_MFL_Write
  2329. #define MFL_PARSE TLSX_MFL_Parse
  2330. #else
  2331. #define MFL_FREE_ALL(a, b)
  2332. #define MFL_GET_SIZE(a) 0
  2333. #define MFL_WRITE(a, b) 0
  2334. #define MFL_PARSE(a, b, c, d) 0
  2335. #endif /* HAVE_MAX_FRAGMENT */
  2336. /******************************************************************************/
  2337. /* Truncated HMAC */
  2338. /******************************************************************************/
  2339. #ifdef HAVE_TRUNCATED_HMAC
  2340. static int TLSX_THM_Parse(WOLFSSL* ssl, const byte* input, word16 length,
  2341. byte isRequest)
  2342. {
  2343. if (length != 0 || input == NULL)
  2344. return BUFFER_ERROR;
  2345. if (!isRequest) {
  2346. #ifndef WOLFSSL_OLD_UNSUPPORTED_EXTENSION
  2347. if (TLSX_CheckUnsupportedExtension(ssl, TLSX_TRUNCATED_HMAC))
  2348. return TLSX_HandleUnsupportedExtension(ssl);
  2349. #endif
  2350. }
  2351. else {
  2352. #ifndef NO_WOLFSSL_SERVER
  2353. int ret = TLSX_UseTruncatedHMAC(&ssl->extensions, ssl->heap);
  2354. if (ret != WOLFSSL_SUCCESS)
  2355. return ret; /* throw error */
  2356. TLSX_SetResponse(ssl, TLSX_TRUNCATED_HMAC);
  2357. #endif
  2358. }
  2359. ssl->truncated_hmac = 1;
  2360. return 0;
  2361. }
  2362. int TLSX_UseTruncatedHMAC(TLSX** extensions, void* heap)
  2363. {
  2364. int ret = 0;
  2365. if (extensions == NULL)
  2366. return BAD_FUNC_ARG;
  2367. ret = TLSX_Push(extensions, TLSX_TRUNCATED_HMAC, NULL, heap);
  2368. if (ret != 0)
  2369. return ret;
  2370. return WOLFSSL_SUCCESS;
  2371. }
  2372. #define THM_PARSE TLSX_THM_Parse
  2373. #else
  2374. #define THM_PARSE(a, b, c, d) 0
  2375. #endif /* HAVE_TRUNCATED_HMAC */
  2376. /******************************************************************************/
  2377. /* Certificate Status Request */
  2378. /******************************************************************************/
  2379. #ifdef HAVE_CERTIFICATE_STATUS_REQUEST
  2380. static void TLSX_CSR_Free(CertificateStatusRequest* csr, void* heap)
  2381. {
  2382. switch (csr->status_type) {
  2383. case WOLFSSL_CSR_OCSP:
  2384. FreeOcspRequest(&csr->request.ocsp);
  2385. break;
  2386. }
  2387. XFREE(csr, heap, DYNAMIC_TYPE_TLSX);
  2388. (void)heap;
  2389. }
  2390. static word16 TLSX_CSR_GetSize(CertificateStatusRequest* csr, byte isRequest)
  2391. {
  2392. word16 size = 0;
  2393. /* shut up compiler warnings */
  2394. (void) csr; (void) isRequest;
  2395. #ifndef NO_WOLFSSL_CLIENT
  2396. if (isRequest) {
  2397. switch (csr->status_type) {
  2398. case WOLFSSL_CSR_OCSP:
  2399. size += ENUM_LEN + 2 * OPAQUE16_LEN;
  2400. if (csr->request.ocsp.nonceSz)
  2401. size += OCSP_NONCE_EXT_SZ;
  2402. break;
  2403. }
  2404. }
  2405. #endif
  2406. #if defined(WOLFSSL_TLS13) && !defined(NO_WOLFSSL_SERVER)
  2407. if (!isRequest && csr->ssl->options.tls1_3)
  2408. return OPAQUE8_LEN + OPAQUE24_LEN + csr->response.length;
  2409. #endif
  2410. return size;
  2411. }
  2412. static word16 TLSX_CSR_Write(CertificateStatusRequest* csr, byte* output,
  2413. byte isRequest)
  2414. {
  2415. /* shut up compiler warnings */
  2416. (void) csr; (void) output; (void) isRequest;
  2417. #ifndef NO_WOLFSSL_CLIENT
  2418. if (isRequest) {
  2419. word16 offset = 0;
  2420. word16 length = 0;
  2421. /* type */
  2422. output[offset++] = csr->status_type;
  2423. switch (csr->status_type) {
  2424. case WOLFSSL_CSR_OCSP:
  2425. /* responder id list */
  2426. c16toa(0, output + offset);
  2427. offset += OPAQUE16_LEN;
  2428. /* request extensions */
  2429. if (csr->request.ocsp.nonceSz)
  2430. length = (word16)EncodeOcspRequestExtensions(
  2431. &csr->request.ocsp,
  2432. output + offset + OPAQUE16_LEN,
  2433. OCSP_NONCE_EXT_SZ);
  2434. c16toa(length, output + offset);
  2435. offset += OPAQUE16_LEN + length;
  2436. break;
  2437. }
  2438. return offset;
  2439. }
  2440. #endif
  2441. #if defined(WOLFSSL_TLS13) && !defined(NO_WOLFSSL_SERVER)
  2442. if (!isRequest && csr->ssl->options.tls1_3) {
  2443. word16 offset = 0;
  2444. output[offset++] = csr->status_type;
  2445. c32to24(csr->response.length, output + offset);
  2446. offset += OPAQUE24_LEN;
  2447. XMEMCPY(output + offset, csr->response.buffer, csr->response.length);
  2448. offset += csr->response.length;
  2449. return offset;
  2450. }
  2451. #endif
  2452. return 0;
  2453. }
  2454. static int TLSX_CSR_Parse(WOLFSSL* ssl, const byte* input, word16 length,
  2455. byte isRequest)
  2456. {
  2457. int ret;
  2458. #if !defined(NO_WOLFSSL_SERVER)
  2459. byte status_type;
  2460. word16 size = 0;
  2461. #if defined(WOLFSSL_TLS13)
  2462. DecodedCert* cert;
  2463. #endif
  2464. #endif
  2465. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER) \
  2466. && defined(WOLFSSL_TLS13)
  2467. OcspRequest* request;
  2468. TLSX* extension;
  2469. CertificateStatusRequest* csr;
  2470. #endif
  2471. #if !defined(NO_WOLFSSL_CLIENT) && defined(WOLFSSL_TLS13) \
  2472. || !defined(NO_WOLFSSL_SERVER)
  2473. word32 offset = 0;
  2474. #endif
  2475. #if !defined(NO_WOLFSSL_CLIENT) && defined(WOLFSSL_TLS13)
  2476. word32 resp_length;
  2477. #endif
  2478. /* shut up compiler warnings */
  2479. (void) ssl; (void) input;
  2480. if (!isRequest) {
  2481. #ifndef NO_WOLFSSL_CLIENT
  2482. extension = TLSX_Find(ssl->extensions, TLSX_STATUS_REQUEST);
  2483. csr = extension ? (CertificateStatusRequest*)extension->data : NULL;
  2484. if (!csr) {
  2485. /* look at context level */
  2486. extension = TLSX_Find(ssl->ctx->extensions, TLSX_STATUS_REQUEST);
  2487. csr = extension ? (CertificateStatusRequest*)extension->data : NULL;
  2488. if (!csr) /* unexpected extension */
  2489. return TLSX_HandleUnsupportedExtension(ssl);
  2490. /* enable extension at ssl level */
  2491. ret = TLSX_UseCertificateStatusRequest(&ssl->extensions,
  2492. csr->status_type, csr->options, ssl,
  2493. ssl->heap, ssl->devId);
  2494. if (ret != WOLFSSL_SUCCESS)
  2495. return ret;
  2496. switch (csr->status_type) {
  2497. case WOLFSSL_CSR_OCSP:
  2498. /* propagate nonce */
  2499. if (csr->request.ocsp.nonceSz) {
  2500. request =
  2501. (OcspRequest*)TLSX_CSR_GetRequest(ssl->extensions);
  2502. if (request) {
  2503. XMEMCPY(request->nonce, csr->request.ocsp.nonce,
  2504. csr->request.ocsp.nonceSz);
  2505. request->nonceSz = csr->request.ocsp.nonceSz;
  2506. }
  2507. }
  2508. break;
  2509. }
  2510. }
  2511. ssl->status_request = 1;
  2512. #ifdef WOLFSSL_TLS13
  2513. if (ssl->options.tls1_3) {
  2514. /* Get the new extension potentially created above. */
  2515. extension = TLSX_Find(ssl->extensions, TLSX_STATUS_REQUEST);
  2516. csr = extension ? (CertificateStatusRequest*)extension->data : NULL;
  2517. if (csr == NULL)
  2518. return MEMORY_ERROR;
  2519. ret = 0;
  2520. if (OPAQUE8_LEN + OPAQUE24_LEN > length)
  2521. ret = BUFFER_ERROR;
  2522. if (ret == 0 && input[offset++] != WOLFSSL_CSR_OCSP) {
  2523. ret = BAD_CERTIFICATE_STATUS_ERROR;
  2524. WOLFSSL_ERROR_VERBOSE(ret);
  2525. }
  2526. if (ret == 0) {
  2527. c24to32(input + offset, &resp_length);
  2528. offset += OPAQUE24_LEN;
  2529. if (offset + resp_length != length)
  2530. ret = BUFFER_ERROR;
  2531. }
  2532. if (ret == 0) {
  2533. csr->response.buffer = (byte*)(input + offset);
  2534. csr->response.length = resp_length;
  2535. }
  2536. return ret;
  2537. }
  2538. else
  2539. #endif
  2540. {
  2541. /* extension_data MUST be empty. */
  2542. return length ? BUFFER_ERROR : 0;
  2543. }
  2544. #endif
  2545. }
  2546. else {
  2547. #ifndef NO_WOLFSSL_SERVER
  2548. if (length == 0)
  2549. return 0;
  2550. status_type = input[offset++];
  2551. switch (status_type) {
  2552. case WOLFSSL_CSR_OCSP: {
  2553. /* skip responder_id_list */
  2554. if ((int)(length - offset) < OPAQUE16_LEN)
  2555. return BUFFER_ERROR;
  2556. ato16(input + offset, &size);
  2557. offset += OPAQUE16_LEN + size;
  2558. /* skip request_extensions */
  2559. if ((int)(length - offset) < OPAQUE16_LEN)
  2560. return BUFFER_ERROR;
  2561. ato16(input + offset, &size);
  2562. offset += OPAQUE16_LEN + size;
  2563. if (offset > length)
  2564. return BUFFER_ERROR;
  2565. /* is able to send OCSP response? */
  2566. if (SSL_CM(ssl) == NULL || !SSL_CM(ssl)->ocspStaplingEnabled)
  2567. return 0;
  2568. }
  2569. break;
  2570. /* unknown status type */
  2571. default:
  2572. return 0;
  2573. }
  2574. /* if using status_request and already sending it, skip this one */
  2575. #ifdef HAVE_CERTIFICATE_STATUS_REQUEST_V2
  2576. if (ssl->status_request_v2)
  2577. return 0;
  2578. #endif
  2579. /* accept the first good status_type and return */
  2580. ret = TLSX_UseCertificateStatusRequest(&ssl->extensions, status_type,
  2581. 0, ssl, ssl->heap, ssl->devId);
  2582. if (ret != WOLFSSL_SUCCESS)
  2583. return ret; /* throw error */
  2584. #if defined(WOLFSSL_TLS13)
  2585. if (ssl->options.tls1_3) {
  2586. cert = (DecodedCert*)XMALLOC(sizeof(DecodedCert), ssl->heap,
  2587. DYNAMIC_TYPE_DCERT);
  2588. if (cert == NULL) {
  2589. return MEMORY_E;
  2590. }
  2591. InitDecodedCert(cert, ssl->buffers.certificate->buffer,
  2592. ssl->buffers.certificate->length, ssl->heap);
  2593. ret = ParseCert(cert, CERT_TYPE, 1, SSL_CM(ssl));
  2594. if (ret != 0 ) {
  2595. XFREE(cert, ssl->heap, DYNAMIC_TYPE_DCERT);
  2596. return ret;
  2597. }
  2598. ret = TLSX_CSR_InitRequest(ssl->extensions, cert, ssl->heap);
  2599. if (ret != 0 ) {
  2600. XFREE(cert, ssl->heap, DYNAMIC_TYPE_DCERT);
  2601. return ret;
  2602. }
  2603. XFREE(cert, ssl->heap, DYNAMIC_TYPE_DCERT);
  2604. extension = TLSX_Find(ssl->extensions, TLSX_STATUS_REQUEST);
  2605. csr = extension ?
  2606. (CertificateStatusRequest*)extension->data : NULL;
  2607. if (csr == NULL)
  2608. return MEMORY_ERROR;
  2609. request = &csr->request.ocsp;
  2610. ret = CreateOcspResponse(ssl, &request, &csr->response);
  2611. if (ret != 0)
  2612. return ret;
  2613. if (csr->response.buffer)
  2614. TLSX_SetResponse(ssl, TLSX_STATUS_REQUEST);
  2615. }
  2616. else
  2617. #endif
  2618. TLSX_SetResponse(ssl, TLSX_STATUS_REQUEST);
  2619. ssl->status_request = status_type;
  2620. #endif
  2621. }
  2622. return 0;
  2623. }
  2624. int TLSX_CSR_InitRequest(TLSX* extensions, DecodedCert* cert, void* heap)
  2625. {
  2626. TLSX* extension = TLSX_Find(extensions, TLSX_STATUS_REQUEST);
  2627. CertificateStatusRequest* csr = extension ?
  2628. (CertificateStatusRequest*)extension->data : NULL;
  2629. int ret = 0;
  2630. if (csr) {
  2631. switch (csr->status_type) {
  2632. case WOLFSSL_CSR_OCSP: {
  2633. byte nonce[MAX_OCSP_NONCE_SZ];
  2634. int nonceSz = csr->request.ocsp.nonceSz;
  2635. /* preserve nonce */
  2636. XMEMCPY(nonce, csr->request.ocsp.nonce, nonceSz);
  2637. if ((ret = InitOcspRequest(&csr->request.ocsp, cert, 0, heap))
  2638. != 0)
  2639. return ret;
  2640. /* restore nonce */
  2641. XMEMCPY(csr->request.ocsp.nonce, nonce, nonceSz);
  2642. csr->request.ocsp.nonceSz = nonceSz;
  2643. }
  2644. break;
  2645. }
  2646. }
  2647. return ret;
  2648. }
  2649. void* TLSX_CSR_GetRequest(TLSX* extensions)
  2650. {
  2651. TLSX* extension = TLSX_Find(extensions, TLSX_STATUS_REQUEST);
  2652. CertificateStatusRequest* csr = extension ?
  2653. (CertificateStatusRequest*)extension->data : NULL;
  2654. if (csr) {
  2655. switch (csr->status_type) {
  2656. case WOLFSSL_CSR_OCSP:
  2657. return &csr->request.ocsp;
  2658. }
  2659. }
  2660. return NULL;
  2661. }
  2662. int TLSX_CSR_ForceRequest(WOLFSSL* ssl)
  2663. {
  2664. TLSX* extension = TLSX_Find(ssl->extensions, TLSX_STATUS_REQUEST);
  2665. CertificateStatusRequest* csr = extension ?
  2666. (CertificateStatusRequest*)extension->data : NULL;
  2667. if (csr) {
  2668. switch (csr->status_type) {
  2669. case WOLFSSL_CSR_OCSP:
  2670. if (SSL_CM(ssl)->ocspEnabled) {
  2671. csr->request.ocsp.ssl = ssl;
  2672. return CheckOcspRequest(SSL_CM(ssl)->ocsp,
  2673. &csr->request.ocsp, NULL);
  2674. }
  2675. else {
  2676. WOLFSSL_ERROR_VERBOSE(OCSP_LOOKUP_FAIL);
  2677. return OCSP_LOOKUP_FAIL;
  2678. }
  2679. }
  2680. }
  2681. return 0;
  2682. }
  2683. int TLSX_UseCertificateStatusRequest(TLSX** extensions, byte status_type,
  2684. byte options, WOLFSSL* ssl, void* heap,
  2685. int devId)
  2686. {
  2687. CertificateStatusRequest* csr = NULL;
  2688. int ret = 0;
  2689. if (!extensions || status_type != WOLFSSL_CSR_OCSP)
  2690. return BAD_FUNC_ARG;
  2691. csr = (CertificateStatusRequest*)
  2692. XMALLOC(sizeof(CertificateStatusRequest), heap, DYNAMIC_TYPE_TLSX);
  2693. if (!csr)
  2694. return MEMORY_E;
  2695. ForceZero(csr, sizeof(CertificateStatusRequest));
  2696. csr->status_type = status_type;
  2697. csr->options = options;
  2698. csr->ssl = ssl;
  2699. switch (csr->status_type) {
  2700. case WOLFSSL_CSR_OCSP:
  2701. if (options & WOLFSSL_CSR_OCSP_USE_NONCE) {
  2702. WC_RNG rng;
  2703. #ifndef HAVE_FIPS
  2704. ret = wc_InitRng_ex(&rng, heap, devId);
  2705. #else
  2706. ret = wc_InitRng(&rng);
  2707. (void)devId;
  2708. #endif
  2709. if (ret == 0) {
  2710. if (wc_RNG_GenerateBlock(&rng, csr->request.ocsp.nonce,
  2711. MAX_OCSP_NONCE_SZ) == 0)
  2712. csr->request.ocsp.nonceSz = MAX_OCSP_NONCE_SZ;
  2713. wc_FreeRng(&rng);
  2714. }
  2715. }
  2716. break;
  2717. }
  2718. if ((ret = TLSX_Push(extensions, TLSX_STATUS_REQUEST, csr, heap)) != 0) {
  2719. XFREE(csr, heap, DYNAMIC_TYPE_TLSX);
  2720. return ret;
  2721. }
  2722. return WOLFSSL_SUCCESS;
  2723. }
  2724. #define CSR_FREE_ALL TLSX_CSR_Free
  2725. #define CSR_GET_SIZE TLSX_CSR_GetSize
  2726. #define CSR_WRITE TLSX_CSR_Write
  2727. #define CSR_PARSE TLSX_CSR_Parse
  2728. #else
  2729. #define CSR_FREE_ALL(data, heap)
  2730. #define CSR_GET_SIZE(a, b) 0
  2731. #define CSR_WRITE(a, b, c) 0
  2732. #define CSR_PARSE(a, b, c, d) 0
  2733. #endif /* HAVE_CERTIFICATE_STATUS_REQUEST */
  2734. /******************************************************************************/
  2735. /* Certificate Status Request v2 */
  2736. /******************************************************************************/
  2737. #ifdef HAVE_CERTIFICATE_STATUS_REQUEST_V2
  2738. static void TLSX_CSR2_FreeAll(CertificateStatusRequestItemV2* csr2, void* heap)
  2739. {
  2740. CertificateStatusRequestItemV2* next;
  2741. for (; csr2; csr2 = next) {
  2742. next = csr2->next;
  2743. switch (csr2->status_type) {
  2744. case WOLFSSL_CSR2_OCSP:
  2745. case WOLFSSL_CSR2_OCSP_MULTI:
  2746. while(csr2->requests--)
  2747. FreeOcspRequest(&csr2->request.ocsp[csr2->requests]);
  2748. break;
  2749. }
  2750. XFREE(csr2, heap, DYNAMIC_TYPE_TLSX);
  2751. }
  2752. (void)heap;
  2753. }
  2754. static word16 TLSX_CSR2_GetSize(CertificateStatusRequestItemV2* csr2,
  2755. byte isRequest)
  2756. {
  2757. word16 size = 0;
  2758. /* shut up compiler warnings */
  2759. (void) csr2; (void) isRequest;
  2760. #ifndef NO_WOLFSSL_CLIENT
  2761. if (isRequest) {
  2762. CertificateStatusRequestItemV2* next;
  2763. for (size = OPAQUE16_LEN; csr2; csr2 = next) {
  2764. next = csr2->next;
  2765. switch (csr2->status_type) {
  2766. case WOLFSSL_CSR2_OCSP:
  2767. case WOLFSSL_CSR2_OCSP_MULTI:
  2768. size += ENUM_LEN + 3 * OPAQUE16_LEN;
  2769. if (csr2->request.ocsp[0].nonceSz)
  2770. size += OCSP_NONCE_EXT_SZ;
  2771. break;
  2772. }
  2773. }
  2774. }
  2775. #endif
  2776. return size;
  2777. }
  2778. static word16 TLSX_CSR2_Write(CertificateStatusRequestItemV2* csr2,
  2779. byte* output, byte isRequest)
  2780. {
  2781. /* shut up compiler warnings */
  2782. (void) csr2; (void) output; (void) isRequest;
  2783. #ifndef NO_WOLFSSL_CLIENT
  2784. if (isRequest) {
  2785. word16 offset;
  2786. word16 length;
  2787. for (offset = OPAQUE16_LEN; csr2 != NULL; csr2 = csr2->next) {
  2788. /* status_type */
  2789. output[offset++] = csr2->status_type;
  2790. /* request */
  2791. switch (csr2->status_type) {
  2792. case WOLFSSL_CSR2_OCSP:
  2793. case WOLFSSL_CSR2_OCSP_MULTI:
  2794. /* request_length */
  2795. length = 2 * OPAQUE16_LEN;
  2796. if (csr2->request.ocsp[0].nonceSz)
  2797. length += OCSP_NONCE_EXT_SZ;
  2798. c16toa(length, output + offset);
  2799. offset += OPAQUE16_LEN;
  2800. /* responder id list */
  2801. c16toa(0, output + offset);
  2802. offset += OPAQUE16_LEN;
  2803. /* request extensions */
  2804. length = 0;
  2805. if (csr2->request.ocsp[0].nonceSz)
  2806. length = (word16)EncodeOcspRequestExtensions(
  2807. &csr2->request.ocsp[0],
  2808. output + offset + OPAQUE16_LEN,
  2809. OCSP_NONCE_EXT_SZ);
  2810. c16toa(length, output + offset);
  2811. offset += OPAQUE16_LEN + length;
  2812. break;
  2813. }
  2814. }
  2815. /* list size */
  2816. c16toa(offset - OPAQUE16_LEN, output);
  2817. return offset;
  2818. }
  2819. #endif
  2820. return 0;
  2821. }
  2822. static int TLSX_CSR2_Parse(WOLFSSL* ssl, const byte* input, word16 length,
  2823. byte isRequest)
  2824. {
  2825. int ret;
  2826. /* shut up compiler warnings */
  2827. (void) ssl; (void) input;
  2828. if (!isRequest) {
  2829. #ifndef NO_WOLFSSL_CLIENT
  2830. TLSX* extension = TLSX_Find(ssl->extensions, TLSX_STATUS_REQUEST_V2);
  2831. CertificateStatusRequestItemV2* csr2 = extension ?
  2832. (CertificateStatusRequestItemV2*)extension->data : NULL;
  2833. if (!csr2) {
  2834. /* look at context level */
  2835. extension = TLSX_Find(ssl->ctx->extensions, TLSX_STATUS_REQUEST_V2);
  2836. csr2 = extension ?
  2837. (CertificateStatusRequestItemV2*)extension->data : NULL;
  2838. if (!csr2) /* unexpected extension */
  2839. return TLSX_HandleUnsupportedExtension(ssl);
  2840. /* enable extension at ssl level */
  2841. for (; csr2; csr2 = csr2->next) {
  2842. ret = TLSX_UseCertificateStatusRequestV2(&ssl->extensions,
  2843. csr2->status_type, csr2->options, ssl->heap,
  2844. ssl->devId);
  2845. if (ret != WOLFSSL_SUCCESS)
  2846. return ret;
  2847. switch (csr2->status_type) {
  2848. case WOLFSSL_CSR2_OCSP:
  2849. /* followed by */
  2850. case WOLFSSL_CSR2_OCSP_MULTI:
  2851. /* propagate nonce */
  2852. if (csr2->request.ocsp[0].nonceSz) {
  2853. OcspRequest* request =
  2854. (OcspRequest*)TLSX_CSR2_GetRequest(ssl->extensions,
  2855. csr2->status_type, 0);
  2856. if (request) {
  2857. XMEMCPY(request->nonce,
  2858. csr2->request.ocsp[0].nonce,
  2859. csr2->request.ocsp[0].nonceSz);
  2860. request->nonceSz =
  2861. csr2->request.ocsp[0].nonceSz;
  2862. }
  2863. }
  2864. break;
  2865. }
  2866. }
  2867. }
  2868. ssl->status_request_v2 = 1;
  2869. return length ? BUFFER_ERROR : 0; /* extension_data MUST be empty. */
  2870. #endif
  2871. }
  2872. else {
  2873. #ifndef NO_WOLFSSL_SERVER
  2874. byte status_type;
  2875. word16 request_length;
  2876. word16 offset = 0;
  2877. word16 size = 0;
  2878. /* list size */
  2879. if (offset + OPAQUE16_LEN >= length) {
  2880. return BUFFER_E;
  2881. }
  2882. ato16(input + offset, &request_length);
  2883. offset += OPAQUE16_LEN;
  2884. if (length - OPAQUE16_LEN != request_length)
  2885. return BUFFER_ERROR;
  2886. while (length > offset) {
  2887. if ((int)(length - offset) < ENUM_LEN + OPAQUE16_LEN)
  2888. return BUFFER_ERROR;
  2889. status_type = input[offset++];
  2890. ato16(input + offset, &request_length);
  2891. offset += OPAQUE16_LEN;
  2892. if (length - offset < request_length)
  2893. return BUFFER_ERROR;
  2894. switch (status_type) {
  2895. case WOLFSSL_CSR2_OCSP:
  2896. case WOLFSSL_CSR2_OCSP_MULTI:
  2897. /* skip responder_id_list */
  2898. if ((int)(length - offset) < OPAQUE16_LEN)
  2899. return BUFFER_ERROR;
  2900. ato16(input + offset, &size);
  2901. if (length - offset < size)
  2902. return BUFFER_ERROR;
  2903. offset += OPAQUE16_LEN + size;
  2904. /* skip request_extensions */
  2905. if ((int)(length - offset) < OPAQUE16_LEN)
  2906. return BUFFER_ERROR;
  2907. ato16(input + offset, &size);
  2908. if (length - offset < size)
  2909. return BUFFER_ERROR;
  2910. offset += OPAQUE16_LEN + size;
  2911. if (offset > length)
  2912. return BUFFER_ERROR;
  2913. /* is able to send OCSP response? */
  2914. if (SSL_CM(ssl) == NULL
  2915. || !SSL_CM(ssl)->ocspStaplingEnabled)
  2916. continue;
  2917. break;
  2918. default:
  2919. /* unknown status type, skipping! */
  2920. offset += request_length;
  2921. continue;
  2922. }
  2923. /* if using status_request and already sending it, remove it
  2924. * and prefer to use the v2 version */
  2925. #ifdef HAVE_CERTIFICATE_STATUS_REQUEST
  2926. if (ssl->status_request) {
  2927. ssl->status_request = 0;
  2928. TLSX_Remove(&ssl->extensions, TLSX_STATUS_REQUEST, ssl->heap);
  2929. }
  2930. #endif
  2931. /* TLS 1.3 servers MUST NOT act upon presence or information in
  2932. * this extension (RFC 8448 Section 4.4.2.1).
  2933. */
  2934. if (!IsAtLeastTLSv1_3(ssl->version)) {
  2935. /* accept the first good status_type and return */
  2936. ret = TLSX_UseCertificateStatusRequestV2(&ssl->extensions,
  2937. status_type, 0, ssl->heap, ssl->devId);
  2938. if (ret != WOLFSSL_SUCCESS)
  2939. return ret; /* throw error */
  2940. TLSX_SetResponse(ssl, TLSX_STATUS_REQUEST_V2);
  2941. ssl->status_request_v2 = status_type;
  2942. }
  2943. return 0;
  2944. }
  2945. #endif
  2946. }
  2947. return 0;
  2948. }
  2949. int TLSX_CSR2_InitRequests(TLSX* extensions, DecodedCert* cert, byte isPeer,
  2950. void* heap)
  2951. {
  2952. TLSX* extension = TLSX_Find(extensions, TLSX_STATUS_REQUEST_V2);
  2953. CertificateStatusRequestItemV2* csr2 = extension ?
  2954. (CertificateStatusRequestItemV2*)extension->data : NULL;
  2955. int ret = 0;
  2956. for (; csr2; csr2 = csr2->next) {
  2957. switch (csr2->status_type) {
  2958. case WOLFSSL_CSR2_OCSP:
  2959. if (!isPeer || csr2->requests != 0)
  2960. break;
  2961. FALL_THROUGH; /* followed by */
  2962. case WOLFSSL_CSR2_OCSP_MULTI: {
  2963. if (csr2->requests < 1 + MAX_CHAIN_DEPTH) {
  2964. byte nonce[MAX_OCSP_NONCE_SZ];
  2965. int nonceSz = csr2->request.ocsp[0].nonceSz;
  2966. /* preserve nonce, replicating nonce of ocsp[0] */
  2967. XMEMCPY(nonce, csr2->request.ocsp[0].nonce, nonceSz);
  2968. if ((ret = InitOcspRequest(
  2969. &csr2->request.ocsp[csr2->requests], cert,
  2970. 0, heap)) != 0)
  2971. return ret;
  2972. /* restore nonce */
  2973. XMEMCPY(csr2->request.ocsp[csr2->requests].nonce,
  2974. nonce, nonceSz);
  2975. csr2->request.ocsp[csr2->requests].nonceSz = nonceSz;
  2976. csr2->requests++;
  2977. }
  2978. }
  2979. break;
  2980. }
  2981. }
  2982. (void)cert;
  2983. return ret;
  2984. }
  2985. void* TLSX_CSR2_GetRequest(TLSX* extensions, byte status_type, byte idx)
  2986. {
  2987. TLSX* extension = TLSX_Find(extensions, TLSX_STATUS_REQUEST_V2);
  2988. CertificateStatusRequestItemV2* csr2 = extension ?
  2989. (CertificateStatusRequestItemV2*)extension->data : NULL;
  2990. for (; csr2; csr2 = csr2->next) {
  2991. if (csr2->status_type == status_type) {
  2992. switch (csr2->status_type) {
  2993. case WOLFSSL_CSR2_OCSP:
  2994. /* followed by */
  2995. case WOLFSSL_CSR2_OCSP_MULTI:
  2996. /* requests are initialized in the reverse order */
  2997. return idx < csr2->requests
  2998. ? &csr2->request.ocsp[csr2->requests - idx - 1]
  2999. : NULL;
  3000. }
  3001. }
  3002. }
  3003. return NULL;
  3004. }
  3005. int TLSX_CSR2_ForceRequest(WOLFSSL* ssl)
  3006. {
  3007. TLSX* extension = TLSX_Find(ssl->extensions, TLSX_STATUS_REQUEST_V2);
  3008. CertificateStatusRequestItemV2* csr2 = extension ?
  3009. (CertificateStatusRequestItemV2*)extension->data : NULL;
  3010. /* forces only the first one */
  3011. if (csr2) {
  3012. switch (csr2->status_type) {
  3013. case WOLFSSL_CSR2_OCSP:
  3014. /* followed by */
  3015. case WOLFSSL_CSR2_OCSP_MULTI:
  3016. if (SSL_CM(ssl)->ocspEnabled) {
  3017. csr2->request.ocsp[0].ssl = ssl;
  3018. return CheckOcspRequest(SSL_CM(ssl)->ocsp,
  3019. &csr2->request.ocsp[0], NULL);
  3020. }
  3021. else {
  3022. WOLFSSL_ERROR_VERBOSE(OCSP_LOOKUP_FAIL);
  3023. return OCSP_LOOKUP_FAIL;
  3024. }
  3025. }
  3026. }
  3027. return 0;
  3028. }
  3029. int TLSX_UseCertificateStatusRequestV2(TLSX** extensions, byte status_type,
  3030. byte options, void* heap, int devId)
  3031. {
  3032. TLSX* extension = NULL;
  3033. CertificateStatusRequestItemV2* csr2 = NULL;
  3034. int ret = 0;
  3035. if (!extensions)
  3036. return BAD_FUNC_ARG;
  3037. if (status_type != WOLFSSL_CSR2_OCSP
  3038. && status_type != WOLFSSL_CSR2_OCSP_MULTI)
  3039. return BAD_FUNC_ARG;
  3040. csr2 = (CertificateStatusRequestItemV2*)
  3041. XMALLOC(sizeof(CertificateStatusRequestItemV2), heap, DYNAMIC_TYPE_TLSX);
  3042. if (!csr2)
  3043. return MEMORY_E;
  3044. ForceZero(csr2, sizeof(CertificateStatusRequestItemV2));
  3045. csr2->status_type = status_type;
  3046. csr2->options = options;
  3047. csr2->next = NULL;
  3048. switch (csr2->status_type) {
  3049. case WOLFSSL_CSR2_OCSP:
  3050. case WOLFSSL_CSR2_OCSP_MULTI:
  3051. if (options & WOLFSSL_CSR2_OCSP_USE_NONCE) {
  3052. WC_RNG rng;
  3053. #ifndef HAVE_FIPS
  3054. ret = wc_InitRng_ex(&rng, heap, devId);
  3055. #else
  3056. ret = wc_InitRng(&rng);
  3057. (void)devId;
  3058. #endif
  3059. if (ret == 0) {
  3060. if (wc_RNG_GenerateBlock(&rng, csr2->request.ocsp[0].nonce,
  3061. MAX_OCSP_NONCE_SZ) == 0)
  3062. csr2->request.ocsp[0].nonceSz = MAX_OCSP_NONCE_SZ;
  3063. wc_FreeRng(&rng);
  3064. }
  3065. }
  3066. break;
  3067. }
  3068. /* append new item */
  3069. if ((extension = TLSX_Find(*extensions, TLSX_STATUS_REQUEST_V2))) {
  3070. CertificateStatusRequestItemV2* last =
  3071. (CertificateStatusRequestItemV2*)extension->data;
  3072. for (; last->next; last = last->next);
  3073. last->next = csr2;
  3074. }
  3075. else if ((ret = TLSX_Push(extensions, TLSX_STATUS_REQUEST_V2, csr2,heap))) {
  3076. XFREE(csr2, heap, DYNAMIC_TYPE_TLSX);
  3077. return ret;
  3078. }
  3079. return WOLFSSL_SUCCESS;
  3080. }
  3081. #define CSR2_FREE_ALL TLSX_CSR2_FreeAll
  3082. #define CSR2_GET_SIZE TLSX_CSR2_GetSize
  3083. #define CSR2_WRITE TLSX_CSR2_Write
  3084. #define CSR2_PARSE TLSX_CSR2_Parse
  3085. #else
  3086. #define CSR2_FREE_ALL(data, heap)
  3087. #define CSR2_GET_SIZE(a, b) 0
  3088. #define CSR2_WRITE(a, b, c) 0
  3089. #define CSR2_PARSE(a, b, c, d) 0
  3090. #endif /* HAVE_CERTIFICATE_STATUS_REQUEST_V2 */
  3091. /******************************************************************************/
  3092. /* Supported Elliptic Curves */
  3093. /******************************************************************************/
  3094. #ifdef HAVE_SUPPORTED_CURVES
  3095. #if !defined(HAVE_ECC) && !defined(HAVE_CURVE25519) && !defined(HAVE_CURVE448) \
  3096. && !defined(HAVE_FFDHE) && !defined(HAVE_PQC)
  3097. #error Elliptic Curves Extension requires Elliptic Curve Cryptography or liboqs groups. \
  3098. Use --enable-ecc and/or --enable-liboqs in the configure script or \
  3099. define HAVE_ECC. Alternatively use FFDHE for DH ciphersuites.
  3100. #endif
  3101. static int TLSX_SupportedCurve_New(SupportedCurve** curve, word16 name,
  3102. void* heap)
  3103. {
  3104. if (curve == NULL)
  3105. return BAD_FUNC_ARG;
  3106. (void)heap;
  3107. *curve = (SupportedCurve*)XMALLOC(sizeof(SupportedCurve), heap,
  3108. DYNAMIC_TYPE_TLSX);
  3109. if (*curve == NULL)
  3110. return MEMORY_E;
  3111. (*curve)->name = name;
  3112. (*curve)->next = NULL;
  3113. return 0;
  3114. }
  3115. static int TLSX_PointFormat_New(PointFormat** point, byte format, void* heap)
  3116. {
  3117. if (point == NULL)
  3118. return BAD_FUNC_ARG;
  3119. (void)heap;
  3120. *point = (PointFormat*)XMALLOC(sizeof(PointFormat), heap,
  3121. DYNAMIC_TYPE_TLSX);
  3122. if (*point == NULL)
  3123. return MEMORY_E;
  3124. (*point)->format = format;
  3125. (*point)->next = NULL;
  3126. return 0;
  3127. }
  3128. static void TLSX_SupportedCurve_FreeAll(SupportedCurve* list, void* heap)
  3129. {
  3130. SupportedCurve* curve;
  3131. while ((curve = list)) {
  3132. list = curve->next;
  3133. XFREE(curve, heap, DYNAMIC_TYPE_TLSX);
  3134. }
  3135. (void)heap;
  3136. }
  3137. static void TLSX_PointFormat_FreeAll(PointFormat* list, void* heap)
  3138. {
  3139. PointFormat* point;
  3140. while ((point = list)) {
  3141. list = point->next;
  3142. XFREE(point, heap, DYNAMIC_TYPE_TLSX);
  3143. }
  3144. (void)heap;
  3145. }
  3146. static int TLSX_SupportedCurve_Append(SupportedCurve* list, word16 name,
  3147. void* heap)
  3148. {
  3149. int ret = BAD_FUNC_ARG;
  3150. while (list) {
  3151. if (list->name == name) {
  3152. ret = 0; /* curve already in use */
  3153. break;
  3154. }
  3155. if (list->next == NULL) {
  3156. ret = TLSX_SupportedCurve_New(&list->next, name, heap);
  3157. break;
  3158. }
  3159. list = list->next;
  3160. }
  3161. return ret;
  3162. }
  3163. static int TLSX_PointFormat_Append(PointFormat* list, byte format, void* heap)
  3164. {
  3165. int ret = BAD_FUNC_ARG;
  3166. while (list) {
  3167. if (list->format == format) {
  3168. ret = 0; /* format already in use */
  3169. break;
  3170. }
  3171. if (list->next == NULL) {
  3172. ret = TLSX_PointFormat_New(&list->next, format, heap);
  3173. break;
  3174. }
  3175. list = list->next;
  3176. }
  3177. return ret;
  3178. }
  3179. #if defined(WOLFSSL_TLS13) || !defined(NO_WOLFSSL_CLIENT)
  3180. #if defined(HAVE_FFDHE) && (defined(HAVE_ECC) || defined(HAVE_CURVE25519) || \
  3181. defined(HAVE_CURVE448))
  3182. static void TLSX_SupportedCurve_ValidateRequest(const WOLFSSL* ssl,
  3183. const byte* semaphore)
  3184. {
  3185. /* If all pre-defined parameter types for key exchange are supported then
  3186. * always send SupportedGroups extension.
  3187. */
  3188. (void)ssl;
  3189. (void)semaphore;
  3190. }
  3191. #else
  3192. static void TLSX_SupportedCurve_ValidateRequest(WOLFSSL* ssl, byte* semaphore)
  3193. {
  3194. word16 i;
  3195. for (i = 0; i < ssl->suites->suiteSz; i += 2) {
  3196. if (ssl->suites->suites[i] == TLS13_BYTE)
  3197. return;
  3198. if ((ssl->suites->suites[i] == ECC_BYTE) ||
  3199. (ssl->suites->suites[i] == ECDHE_PSK_BYTE) ||
  3200. (ssl->suites->suites[i] == CHACHA_BYTE)) {
  3201. #if defined(HAVE_ECC) || defined(HAVE_CURVE25519) || \
  3202. defined(HAVE_CURVE448)
  3203. return;
  3204. #endif
  3205. }
  3206. #ifdef HAVE_FFDHE
  3207. else {
  3208. return;
  3209. }
  3210. #endif
  3211. }
  3212. /* turns semaphore on to avoid sending this extension. */
  3213. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_SUPPORTED_GROUPS));
  3214. }
  3215. #endif
  3216. /* Only send PointFormats if TLSv13, ECC or CHACHA cipher suite present.
  3217. */
  3218. static void TLSX_PointFormat_ValidateRequest(WOLFSSL* ssl, byte* semaphore)
  3219. {
  3220. word16 i;
  3221. for (i = 0; i < ssl->suites->suiteSz; i += 2) {
  3222. if (ssl->suites->suites[i] == TLS13_BYTE)
  3223. return;
  3224. if ((ssl->suites->suites[i] == ECC_BYTE) ||
  3225. (ssl->suites->suites[i] == ECDHE_PSK_BYTE) ||
  3226. (ssl->suites->suites[i] == CHACHA_BYTE)) {
  3227. #if defined(HAVE_ECC) || defined(HAVE_CURVE25519) || \
  3228. defined(HAVE_CURVE448)
  3229. return;
  3230. #endif
  3231. }
  3232. }
  3233. #ifdef HAVE_FFDHE
  3234. (void)semaphore;
  3235. return;
  3236. #else
  3237. /* turns semaphore on to avoid sending this extension. */
  3238. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_EC_POINT_FORMATS));
  3239. #endif
  3240. }
  3241. #endif /* WOLFSSL_TLS13 || !NO_WOLFSSL_CLIENT */
  3242. #ifndef NO_WOLFSSL_SERVER
  3243. static void TLSX_PointFormat_ValidateResponse(WOLFSSL* ssl, byte* semaphore)
  3244. {
  3245. #if defined(HAVE_FFDHE) || defined(HAVE_ECC) || defined(HAVE_CURVE25519) || \
  3246. defined(HAVE_CURVE448)
  3247. (void)semaphore;
  3248. #endif
  3249. if (ssl->options.cipherSuite0 == TLS13_BYTE)
  3250. return;
  3251. #if defined(HAVE_ECC) || defined(HAVE_CURVE25519) || defined(HAVE_CURVE448)
  3252. if (ssl->options.cipherSuite0 == ECC_BYTE ||
  3253. ssl->options.cipherSuite0 == ECDHE_PSK_BYTE ||
  3254. ssl->options.cipherSuite0 == CHACHA_BYTE) {
  3255. return;
  3256. }
  3257. #endif
  3258. /* turns semaphore on to avoid sending this extension. */
  3259. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_EC_POINT_FORMATS));
  3260. }
  3261. #endif /* !NO_WOLFSSL_SERVER */
  3262. #ifndef NO_WOLFSSL_CLIENT
  3263. static word16 TLSX_SupportedCurve_GetSize(SupportedCurve* list)
  3264. {
  3265. SupportedCurve* curve;
  3266. word16 length = OPAQUE16_LEN; /* list length */
  3267. while ((curve = list)) {
  3268. list = curve->next;
  3269. length += OPAQUE16_LEN; /* curve length */
  3270. }
  3271. return length;
  3272. }
  3273. #endif
  3274. static word16 TLSX_PointFormat_GetSize(PointFormat* list)
  3275. {
  3276. PointFormat* point;
  3277. word16 length = ENUM_LEN; /* list length */
  3278. while ((point = list)) {
  3279. list = point->next;
  3280. length += ENUM_LEN; /* format length */
  3281. }
  3282. return length;
  3283. }
  3284. #ifndef NO_WOLFSSL_CLIENT
  3285. static word16 TLSX_SupportedCurve_Write(SupportedCurve* list, byte* output)
  3286. {
  3287. word16 offset = OPAQUE16_LEN;
  3288. while (list) {
  3289. c16toa(list->name, output + offset);
  3290. offset += OPAQUE16_LEN;
  3291. list = list->next;
  3292. }
  3293. c16toa(offset - OPAQUE16_LEN, output); /* writing list length */
  3294. return offset;
  3295. }
  3296. #endif
  3297. static word16 TLSX_PointFormat_Write(PointFormat* list, byte* output)
  3298. {
  3299. word16 offset = ENUM_LEN;
  3300. while (list) {
  3301. output[offset++] = list->format;
  3302. list = list->next;
  3303. }
  3304. output[0] = (byte)(offset - ENUM_LEN);
  3305. return offset;
  3306. }
  3307. #if !defined(NO_WOLFSSL_SERVER) || (defined(WOLFSSL_TLS13) && \
  3308. !defined(WOLFSSL_NO_SERVER_GROUPS_EXT))
  3309. static int TLSX_SupportedCurve_Parse(WOLFSSL* ssl, const byte* input,
  3310. word16 length, byte isRequest)
  3311. {
  3312. word16 offset;
  3313. word16 name;
  3314. int ret;
  3315. if(!isRequest && !IsAtLeastTLSv1_3(ssl->version)) {
  3316. #ifdef WOLFSSL_ALLOW_SERVER_SC_EXT
  3317. return 0;
  3318. #else
  3319. return BUFFER_ERROR; /* servers doesn't send this extension. */
  3320. #endif
  3321. }
  3322. if (OPAQUE16_LEN > length || length % OPAQUE16_LEN)
  3323. return BUFFER_ERROR;
  3324. ato16(input, &offset);
  3325. /* validating curve list length */
  3326. if (length != OPAQUE16_LEN + offset)
  3327. return BUFFER_ERROR;
  3328. offset = OPAQUE16_LEN;
  3329. if (offset == length)
  3330. return 0;
  3331. #if defined(WOLFSSL_TLS13) && !defined(WOLFSSL_NO_SERVER_GROUPS_EXT)
  3332. if (!isRequest) {
  3333. TLSX* extension;
  3334. SupportedCurve* curve;
  3335. extension = TLSX_Find(ssl->extensions, TLSX_SUPPORTED_GROUPS);
  3336. if (extension != NULL) {
  3337. /* Replace client list with server list of supported groups. */
  3338. curve = (SupportedCurve*)extension->data;
  3339. extension->data = NULL;
  3340. TLSX_SupportedCurve_FreeAll(curve, ssl->heap);
  3341. ato16(input + offset, &name);
  3342. offset += OPAQUE16_LEN;
  3343. ret = TLSX_SupportedCurve_New(&curve, name, ssl->heap);
  3344. if (ret != 0)
  3345. return ret; /* throw error */
  3346. extension->data = (void*)curve;
  3347. }
  3348. }
  3349. #endif
  3350. for (; offset < length; offset += OPAQUE16_LEN) {
  3351. ato16(input + offset, &name);
  3352. ret = TLSX_UseSupportedCurve(&ssl->extensions, name, ssl->heap);
  3353. /* If it is BAD_FUNC_ARG then it is a group we do not support, but
  3354. * that is fine. */
  3355. if (ret != WOLFSSL_SUCCESS && ret != BAD_FUNC_ARG) {
  3356. return ret;
  3357. }
  3358. }
  3359. return 0;
  3360. }
  3361. #endif
  3362. #if !defined(NO_WOLFSSL_SERVER)
  3363. #if defined(WOLFSSL_TLS13) && !defined(WOLFSSL_NO_SERVER_GROUPS_EXT)
  3364. /* Checks the priority of the groups on the server and set the supported groups
  3365. * response if there is a group not advertised by the client that is preferred.
  3366. *
  3367. * ssl SSL/TLS object.
  3368. * returns 0 on success, otherwise an error.
  3369. */
  3370. int TLSX_SupportedCurve_CheckPriority(WOLFSSL* ssl)
  3371. {
  3372. int ret;
  3373. TLSX* extension;
  3374. TLSX* priority = NULL;
  3375. TLSX* ext = NULL;
  3376. word16 name;
  3377. SupportedCurve* curve;
  3378. extension = TLSX_Find(ssl->extensions, TLSX_SUPPORTED_GROUPS);
  3379. /* May be doing PSK with no key exchange. */
  3380. if (extension == NULL)
  3381. return 0;
  3382. ret = TLSX_PopulateSupportedGroups(ssl, &priority);
  3383. if (ret != WOLFSSL_SUCCESS) {
  3384. TLSX_FreeAll(priority, ssl->heap);
  3385. return ret;
  3386. }
  3387. ext = TLSX_Find(priority, TLSX_SUPPORTED_GROUPS);
  3388. if (ext == NULL) {
  3389. WOLFSSL_MSG("Could not find supported groups extension");
  3390. TLSX_FreeAll(priority, ssl->heap);
  3391. return 0;
  3392. }
  3393. curve = (SupportedCurve*)ext->data;
  3394. name = curve->name;
  3395. curve = (SupportedCurve*)extension->data;
  3396. while (curve != NULL) {
  3397. if (curve->name == name)
  3398. break;
  3399. curve = curve->next;
  3400. }
  3401. if (curve == NULL) {
  3402. /* Couldn't find the preferred group in client list. */
  3403. extension->resp = 1;
  3404. /* Send server list back and free client list. */
  3405. curve = (SupportedCurve*)extension->data;
  3406. extension->data = ext->data;
  3407. ext->data = curve;
  3408. }
  3409. TLSX_FreeAll(priority, ssl->heap);
  3410. return 0;
  3411. }
  3412. #endif /* WOLFSSL_TLS13 && !WOLFSSL_NO_SERVER_GROUPS_EXT */
  3413. #if defined(HAVE_FFDHE) && !defined(WOLFSSL_NO_TLS12)
  3414. #ifdef HAVE_PUBLIC_FFDHE
  3415. static int tlsx_ffdhe_find_group(WOLFSSL* ssl, SupportedCurve* clientGroup,
  3416. SupportedCurve* serverGroup)
  3417. {
  3418. int ret = 0;
  3419. SupportedCurve* group;
  3420. const DhParams* params = NULL;
  3421. for (; serverGroup != NULL; serverGroup = serverGroup->next) {
  3422. if (!WOLFSSL_NAMED_GROUP_IS_FFHDE(serverGroup->name))
  3423. continue;
  3424. for (group = clientGroup; group != NULL; group = group->next) {
  3425. if (serverGroup->name != group->name)
  3426. continue;
  3427. switch (serverGroup->name) {
  3428. #ifdef HAVE_FFDHE_2048
  3429. case WOLFSSL_FFDHE_2048:
  3430. params = wc_Dh_ffdhe2048_Get();
  3431. break;
  3432. #endif
  3433. #ifdef HAVE_FFDHE_3072
  3434. case WOLFSSL_FFDHE_3072:
  3435. params = wc_Dh_ffdhe3072_Get();
  3436. break;
  3437. #endif
  3438. #ifdef HAVE_FFDHE_4096
  3439. case WOLFSSL_FFDHE_4096:
  3440. params = wc_Dh_ffdhe4096_Get();
  3441. break;
  3442. #endif
  3443. #ifdef HAVE_FFDHE_6144
  3444. case WOLFSSL_FFDHE_6144:
  3445. params = wc_Dh_ffdhe6144_Get();
  3446. break;
  3447. #endif
  3448. #ifdef HAVE_FFDHE_8192
  3449. case WOLFSSL_FFDHE_8192:
  3450. params = wc_Dh_ffdhe8192_Get();
  3451. break;
  3452. #endif
  3453. default:
  3454. break;
  3455. }
  3456. if (params == NULL) {
  3457. ret = BAD_FUNC_ARG;
  3458. break;
  3459. }
  3460. if (params->p_len >= ssl->options.minDhKeySz &&
  3461. params->p_len <= ssl->options.maxDhKeySz) {
  3462. break;
  3463. }
  3464. }
  3465. if (ret != 0)
  3466. break;
  3467. if ((group != NULL) && (serverGroup->name == group->name))
  3468. break;
  3469. }
  3470. if ((ret == 0) && (serverGroup != NULL) && (params != NULL)) {
  3471. ssl->buffers.serverDH_P.buffer = (unsigned char *)params->p;
  3472. ssl->buffers.serverDH_P.length = params->p_len;
  3473. ssl->buffers.serverDH_G.buffer = (unsigned char *)params->g;
  3474. ssl->buffers.serverDH_G.length = params->g_len;
  3475. ssl->namedGroup = serverGroup->name;
  3476. #if !defined(WOLFSSL_OLD_PRIME_CHECK) && \
  3477. !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  3478. ssl->options.dhDoKeyTest = 0;
  3479. #endif
  3480. ssl->options.haveDH = 1;
  3481. }
  3482. return ret;
  3483. }
  3484. #else
  3485. static int tlsx_ffdhe_find_group(WOLFSSL* ssl, SupportedCurve* clientGroup,
  3486. SupportedCurve* serverGroup)
  3487. {
  3488. int ret = 0;
  3489. SupportedCurve* group;
  3490. word32 p_len;
  3491. for (; serverGroup != NULL; serverGroup = serverGroup->next) {
  3492. if (!WOLFSSL_NAMED_GROUP_IS_FFHDE(serverGroup->name))
  3493. continue;
  3494. for (group = clientGroup; group != NULL; group = group->next) {
  3495. if (serverGroup->name != group->name)
  3496. continue;
  3497. wc_DhGetNamedKeyParamSize(serverGroup->name, &p_len, NULL, NULL);
  3498. if (p_len == 0) {
  3499. ret = BAD_FUNC_ARG;
  3500. break;
  3501. }
  3502. if (p_len >= ssl->options.minDhKeySz &&
  3503. p_len <= ssl->options.maxDhKeySz) {
  3504. break;
  3505. }
  3506. }
  3507. if (ret != 0)
  3508. break;
  3509. if ((group != NULL) && (serverGroup->name == group->name))
  3510. break;
  3511. }
  3512. if ((ret == 0) && (serverGroup != NULL)) {
  3513. word32 pSz, gSz;
  3514. ssl->buffers.serverDH_P.buffer = NULL;
  3515. ssl->buffers.serverDH_G.buffer = NULL;
  3516. ret = wc_DhGetNamedKeyParamSize(serverGroup->name, &pSz, &gSz, NULL);
  3517. if (ret == 0) {
  3518. ssl->buffers.serverDH_P.buffer =
  3519. (byte*)XMALLOC(pSz, ssl->heap, DYNAMIC_TYPE_PUBLIC_KEY);
  3520. if (ssl->buffers.serverDH_P.buffer == NULL)
  3521. ret = MEMORY_E;
  3522. else
  3523. ssl->buffers.serverDH_P.length = pSz;
  3524. }
  3525. if (ret == 0) {
  3526. ssl->buffers.serverDH_G.buffer =
  3527. (byte*)XMALLOC(gSz, ssl->heap, DYNAMIC_TYPE_PUBLIC_KEY);
  3528. if (ssl->buffers.serverDH_G.buffer == NULL) {
  3529. ret = MEMORY_E;
  3530. } else
  3531. ssl->buffers.serverDH_G.length = gSz;
  3532. }
  3533. if (ret == 0) {
  3534. ret = wc_DhCopyNamedKey(serverGroup->name,
  3535. ssl->buffers.serverDH_P.buffer, &pSz,
  3536. ssl->buffers.serverDH_G.buffer, &gSz,
  3537. NULL, NULL);
  3538. }
  3539. if (ret == 0) {
  3540. ssl->buffers.weOwnDH = 1;
  3541. ssl->namedGroup = serverGroup->name;
  3542. #if !defined(WOLFSSL_OLD_PRIME_CHECK) && \
  3543. !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  3544. ssl->options.dhDoKeyTest = 0;
  3545. #endif
  3546. ssl->options.haveDH = 1;
  3547. }
  3548. else {
  3549. if (ssl->buffers.serverDH_P.buffer != NULL) {
  3550. XFREE(ssl->buffers.serverDH_P.buffer, ssl->heap,
  3551. DYNAMIC_TYPE_PUBLIC_KEY);
  3552. ssl->buffers.serverDH_P.length = 0;
  3553. ssl->buffers.serverDH_P.buffer = NULL;
  3554. }
  3555. if (ssl->buffers.serverDH_G.buffer != NULL) {
  3556. XFREE(ssl->buffers.serverDH_G.buffer, ssl->heap,
  3557. DYNAMIC_TYPE_PUBLIC_KEY);
  3558. ssl->buffers.serverDH_G.length = 0;
  3559. ssl->buffers.serverDH_G.buffer = NULL;
  3560. }
  3561. }
  3562. }
  3563. return ret;
  3564. }
  3565. #endif
  3566. /* Set the highest priority common FFDHE group on the server as compared to
  3567. * client extensions.
  3568. *
  3569. * ssl SSL/TLS object.
  3570. * returns 0 on success, otherwise an error.
  3571. */
  3572. int TLSX_SupportedFFDHE_Set(WOLFSSL* ssl)
  3573. {
  3574. int ret;
  3575. TLSX* priority = NULL;
  3576. TLSX* ext = NULL;
  3577. TLSX* extension;
  3578. SupportedCurve* clientGroup;
  3579. SupportedCurve* serverGroup;
  3580. SupportedCurve* group;
  3581. int found = 0;
  3582. extension = TLSX_Find(ssl->extensions, TLSX_SUPPORTED_GROUPS);
  3583. /* May be doing PSK with no key exchange. */
  3584. if (extension == NULL)
  3585. return 0;
  3586. clientGroup = (SupportedCurve*)extension->data;
  3587. for (group = clientGroup; group != NULL; group = group->next) {
  3588. if (WOLFSSL_NAMED_GROUP_IS_FFHDE(group->name)) {
  3589. found = 1;
  3590. break;
  3591. }
  3592. }
  3593. if (!found)
  3594. return 0;
  3595. if (ssl->buffers.serverDH_P.buffer && ssl->buffers.weOwnDH) {
  3596. XFREE(ssl->buffers.serverDH_P.buffer, ssl->heap,
  3597. DYNAMIC_TYPE_PUBLIC_KEY);
  3598. }
  3599. if (ssl->buffers.serverDH_G.buffer && ssl->buffers.weOwnDH) {
  3600. XFREE(ssl->buffers.serverDH_G.buffer, ssl->heap,
  3601. DYNAMIC_TYPE_PUBLIC_KEY);
  3602. }
  3603. ssl->buffers.serverDH_P.buffer = NULL;
  3604. ssl->buffers.serverDH_G.buffer = NULL;
  3605. ssl->buffers.weOwnDH = 0;
  3606. ssl->options.haveDH = 0;
  3607. ret = TLSX_PopulateSupportedGroups(ssl, &priority);
  3608. if (ret == WOLFSSL_SUCCESS) {
  3609. ext = TLSX_Find(priority, TLSX_SUPPORTED_GROUPS);
  3610. serverGroup = (SupportedCurve*)ext->data;
  3611. ret = tlsx_ffdhe_find_group(ssl, clientGroup, serverGroup);
  3612. }
  3613. TLSX_FreeAll(priority, ssl->heap);
  3614. return ret;
  3615. }
  3616. #endif /* HAVE_FFDHE && !WOLFSSL_NO_TLS12 */
  3617. #endif /* !NO_WOLFSSL_SERVER */
  3618. #if defined(WOLFSSL_TLS13) && !defined(WOLFSSL_NO_SERVER_GROUPS_EXT)
  3619. /* Return the preferred group.
  3620. *
  3621. * ssl SSL/TLS object.
  3622. * checkSupported Whether to check for the first supported group.
  3623. * returns BAD_FUNC_ARG if no group found, otherwise the group.
  3624. */
  3625. int TLSX_SupportedCurve_Preferred(WOLFSSL* ssl, int checkSupported)
  3626. {
  3627. TLSX* extension;
  3628. SupportedCurve* curve;
  3629. extension = TLSX_Find(ssl->extensions, TLSX_SUPPORTED_GROUPS);
  3630. if (extension == NULL)
  3631. return BAD_FUNC_ARG;
  3632. curve = (SupportedCurve*)extension->data;
  3633. while (curve != NULL) {
  3634. if (!checkSupported || TLSX_KeyShare_IsSupported(curve->name))
  3635. return curve->name;
  3636. curve = curve->next;
  3637. }
  3638. return BAD_FUNC_ARG;
  3639. }
  3640. #endif /* HAVE_SUPPORTED_CURVES */
  3641. #ifndef NO_WOLFSSL_SERVER
  3642. static int TLSX_PointFormat_Parse(WOLFSSL* ssl, const byte* input,
  3643. word16 length, byte isRequest)
  3644. {
  3645. int ret;
  3646. /* validating formats list length */
  3647. if (ENUM_LEN > length || length != (word16)ENUM_LEN + input[0])
  3648. return BUFFER_ERROR;
  3649. if (isRequest) {
  3650. /* adding uncompressed point format to response */
  3651. ret = TLSX_UsePointFormat(&ssl->extensions, WOLFSSL_EC_PF_UNCOMPRESSED,
  3652. ssl->heap);
  3653. if (ret != WOLFSSL_SUCCESS)
  3654. return ret; /* throw error */
  3655. TLSX_SetResponse(ssl, TLSX_EC_POINT_FORMATS);
  3656. }
  3657. return 0;
  3658. }
  3659. #if defined(HAVE_ECC) || defined(HAVE_CURVE25519) || defined(HAVE_CURVE448)
  3660. int TLSX_ValidateSupportedCurves(WOLFSSL* ssl, byte first, byte second) {
  3661. TLSX* extension = NULL;
  3662. SupportedCurve* curve = NULL;
  3663. word32 oid = 0;
  3664. word32 defOid = 0;
  3665. word32 defSz = 80; /* Maximum known curve size is 66. */
  3666. word32 nextOid = 0;
  3667. word32 nextSz = 80; /* Maximum known curve size is 66. */
  3668. word32 currOid = ssl->ecdhCurveOID;
  3669. int ephmSuite = 0;
  3670. word16 octets = 0; /* according to 'ecc_set_type ecc_sets[];' */
  3671. int key = 0; /* validate key */
  3672. (void)oid;
  3673. if (first == CHACHA_BYTE) {
  3674. switch (second) {
  3675. case TLS_DHE_RSA_WITH_CHACHA20_POLY1305_SHA256:
  3676. case TLS_PSK_WITH_CHACHA20_POLY1305_SHA256:
  3677. case TLS_DHE_PSK_WITH_CHACHA20_POLY1305_SHA256:
  3678. case TLS_DHE_RSA_WITH_CHACHA20_OLD_POLY1305_SHA256:
  3679. return 1; /* no suite restriction */
  3680. case TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256:
  3681. case TLS_ECDHE_RSA_WITH_CHACHA20_OLD_POLY1305_SHA256:
  3682. case TLS_ECDHE_PSK_WITH_CHACHA20_POLY1305_SHA256:
  3683. break;
  3684. }
  3685. }
  3686. if (first == ECC_BYTE || first == ECDHE_PSK_BYTE || first == CHACHA_BYTE)
  3687. extension = TLSX_Find(ssl->extensions, TLSX_SUPPORTED_GROUPS);
  3688. if (!extension)
  3689. return 1; /* no suite restriction */
  3690. for (curve = (SupportedCurve*)extension->data;
  3691. curve && !key;
  3692. curve = curve->next) {
  3693. #ifdef OPENSSL_EXTRA
  3694. /* skip if name is not in supported ECC range
  3695. * or disabled by user */
  3696. if (curve->name > WOLFSSL_ECC_MAX ||
  3697. wolfSSL_curve_is_disabled(ssl, curve->name))
  3698. continue;
  3699. #endif
  3700. /* find supported curve */
  3701. switch (curve->name) {
  3702. #ifdef HAVE_ECC
  3703. #if (defined(HAVE_ECC160) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 160
  3704. #ifndef NO_ECC_SECP
  3705. case WOLFSSL_ECC_SECP160R1:
  3706. oid = ECC_SECP160R1_OID;
  3707. octets = 20;
  3708. break;
  3709. #endif /* !NO_ECC_SECP */
  3710. #ifdef HAVE_ECC_SECPR2
  3711. case WOLFSSL_ECC_SECP160R2:
  3712. oid = ECC_SECP160R2_OID;
  3713. octets = 20;
  3714. break;
  3715. #endif /* HAVE_ECC_SECPR2 */
  3716. #ifdef HAVE_ECC_KOBLITZ
  3717. case WOLFSSL_ECC_SECP160K1:
  3718. oid = ECC_SECP160K1_OID;
  3719. octets = 20;
  3720. break;
  3721. #endif /* HAVE_ECC_KOBLITZ */
  3722. #endif
  3723. #if (defined(HAVE_ECC192) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 192
  3724. #ifndef NO_ECC_SECP
  3725. case WOLFSSL_ECC_SECP192R1:
  3726. oid = ECC_SECP192R1_OID;
  3727. octets = 24;
  3728. break;
  3729. #endif /* !NO_ECC_SECP */
  3730. #ifdef HAVE_ECC_KOBLITZ
  3731. case WOLFSSL_ECC_SECP192K1:
  3732. oid = ECC_SECP192K1_OID;
  3733. octets = 24;
  3734. break;
  3735. #endif /* HAVE_ECC_KOBLITZ */
  3736. #endif
  3737. #if (defined(HAVE_ECC224) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 224
  3738. #ifndef NO_ECC_SECP
  3739. case WOLFSSL_ECC_SECP224R1:
  3740. oid = ECC_SECP224R1_OID;
  3741. octets = 28;
  3742. break;
  3743. #endif /* !NO_ECC_SECP */
  3744. #ifdef HAVE_ECC_KOBLITZ
  3745. case WOLFSSL_ECC_SECP224K1:
  3746. oid = ECC_SECP224K1_OID;
  3747. octets = 28;
  3748. break;
  3749. #endif /* HAVE_ECC_KOBLITZ */
  3750. #endif
  3751. #if (!defined(NO_ECC256) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 256
  3752. #ifndef NO_ECC_SECP
  3753. case WOLFSSL_ECC_SECP256R1:
  3754. oid = ECC_SECP256R1_OID;
  3755. octets = 32;
  3756. break;
  3757. #endif /* !NO_ECC_SECP */
  3758. #endif /* !NO_ECC256 || HAVE_ALL_CURVES */
  3759. #endif
  3760. #if (defined(HAVE_CURVE25519) || defined(HAVE_ED25519)) && ECC_MIN_KEY_SZ <= 256
  3761. case WOLFSSL_ECC_X25519:
  3762. oid = ECC_X25519_OID;
  3763. octets = 32;
  3764. break;
  3765. #endif /* HAVE_CURVE25519 */
  3766. #ifdef HAVE_ECC
  3767. #if (!defined(NO_ECC256) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 256
  3768. #ifdef HAVE_ECC_KOBLITZ
  3769. case WOLFSSL_ECC_SECP256K1:
  3770. oid = ECC_SECP256K1_OID;
  3771. octets = 32;
  3772. break;
  3773. #endif /* HAVE_ECC_KOBLITZ */
  3774. #ifdef HAVE_ECC_BRAINPOOL
  3775. case WOLFSSL_ECC_BRAINPOOLP256R1:
  3776. oid = ECC_BRAINPOOLP256R1_OID;
  3777. octets = 32;
  3778. break;
  3779. #endif /* HAVE_ECC_BRAINPOOL */
  3780. #endif
  3781. #if (defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 384
  3782. #ifndef NO_ECC_SECP
  3783. case WOLFSSL_ECC_SECP384R1:
  3784. oid = ECC_SECP384R1_OID;
  3785. octets = 48;
  3786. break;
  3787. #endif /* !NO_ECC_SECP */
  3788. #ifdef HAVE_ECC_BRAINPOOL
  3789. case WOLFSSL_ECC_BRAINPOOLP384R1:
  3790. oid = ECC_BRAINPOOLP384R1_OID;
  3791. octets = 48;
  3792. break;
  3793. #endif /* HAVE_ECC_BRAINPOOL */
  3794. #endif
  3795. #endif
  3796. #if (defined(HAVE_CURVE448) || defined(HAVE_ED448)) && ECC_MIN_KEY_SZ <= 448
  3797. case WOLFSSL_ECC_X448:
  3798. oid = ECC_X448_OID;
  3799. octets = 57;
  3800. break;
  3801. #endif /* HAVE_CURVE448 */
  3802. #ifdef HAVE_ECC
  3803. #if (defined(HAVE_ECC512) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 512
  3804. #ifdef HAVE_ECC_BRAINPOOL
  3805. case WOLFSSL_ECC_BRAINPOOLP512R1:
  3806. oid = ECC_BRAINPOOLP512R1_OID;
  3807. octets = 64;
  3808. break;
  3809. #endif /* HAVE_ECC_BRAINPOOL */
  3810. #endif
  3811. #if (defined(HAVE_ECC521) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 521
  3812. #ifndef NO_ECC_SECP
  3813. case WOLFSSL_ECC_SECP521R1:
  3814. oid = ECC_SECP521R1_OID;
  3815. octets = 66;
  3816. break;
  3817. #endif /* !NO_ECC_SECP */
  3818. #endif
  3819. #endif
  3820. default: continue; /* unsupported curve */
  3821. }
  3822. #ifdef HAVE_ECC
  3823. /* Set default Oid */
  3824. if (defOid == 0 && ssl->eccTempKeySz <= octets && defSz > octets) {
  3825. defOid = oid;
  3826. defSz = octets;
  3827. }
  3828. /* The eccTempKeySz is the preferred ephemeral key size */
  3829. if (currOid == 0 && ssl->eccTempKeySz == octets)
  3830. currOid = oid;
  3831. if ((nextOid == 0 || nextSz > octets) && ssl->eccTempKeySz <= octets) {
  3832. nextOid = oid;
  3833. nextSz = octets;
  3834. }
  3835. #else
  3836. if (defOid == 0 && defSz > octets) {
  3837. defOid = oid;
  3838. defSz = octets;
  3839. }
  3840. if (currOid == 0)
  3841. currOid = oid;
  3842. if (nextOid == 0 || nextSz > octets) {
  3843. nextOid = oid;
  3844. nextSz = octets;
  3845. }
  3846. #endif
  3847. if (first == ECC_BYTE) {
  3848. switch (second) {
  3849. #if defined(HAVE_ECC) || defined(HAVE_ED25519) || defined(HAVE_ED448)
  3850. /* ECDHE_ECDSA */
  3851. case TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA:
  3852. case TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA:
  3853. case TLS_ECDHE_ECDSA_WITH_RC4_128_SHA:
  3854. case TLS_ECDHE_ECDSA_WITH_3DES_EDE_CBC_SHA:
  3855. case TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256:
  3856. case TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384:
  3857. case TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256:
  3858. case TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384:
  3859. case TLS_ECDHE_ECDSA_WITH_AES_128_CCM_8:
  3860. case TLS_ECDHE_ECDSA_WITH_AES_256_CCM_8:
  3861. key |= ssl->ecdhCurveOID == oid;
  3862. ephmSuite = 1;
  3863. break;
  3864. #ifdef WOLFSSL_STATIC_DH
  3865. /* ECDH_ECDSA */
  3866. case TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA:
  3867. case TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA:
  3868. case TLS_ECDH_ECDSA_WITH_RC4_128_SHA:
  3869. case TLS_ECDH_ECDSA_WITH_3DES_EDE_CBC_SHA:
  3870. case TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA256:
  3871. case TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA384:
  3872. case TLS_ECDH_ECDSA_WITH_AES_128_GCM_SHA256:
  3873. case TLS_ECDH_ECDSA_WITH_AES_256_GCM_SHA384:
  3874. if (oid == ECC_X25519_OID && defOid == oid) {
  3875. defOid = 0;
  3876. defSz = 80;
  3877. }
  3878. if (oid == ECC_X448_OID && defOid == oid) {
  3879. defOid = 0;
  3880. defSz = 80;
  3881. }
  3882. key |= ssl->pkCurveOID == oid;
  3883. break;
  3884. #endif /* WOLFSSL_STATIC_DH */
  3885. #endif /* HAVE_ECC || HAVE_ED25519 || HAVE_ED448 */
  3886. #ifndef NO_RSA
  3887. /* ECDHE_RSA */
  3888. case TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA:
  3889. case TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA:
  3890. case TLS_ECDHE_RSA_WITH_RC4_128_SHA:
  3891. case TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA:
  3892. case TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256:
  3893. case TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384:
  3894. case TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256:
  3895. case TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384:
  3896. key |= ssl->ecdhCurveOID == oid;
  3897. ephmSuite = 1;
  3898. break;
  3899. #if defined(HAVE_ECC) && defined(WOLFSSL_STATIC_DH)
  3900. /* ECDH_RSA */
  3901. case TLS_ECDH_RSA_WITH_AES_256_CBC_SHA:
  3902. case TLS_ECDH_RSA_WITH_AES_128_CBC_SHA:
  3903. case TLS_ECDH_RSA_WITH_RC4_128_SHA:
  3904. case TLS_ECDH_RSA_WITH_3DES_EDE_CBC_SHA:
  3905. case TLS_ECDH_RSA_WITH_AES_128_CBC_SHA256:
  3906. case TLS_ECDH_RSA_WITH_AES_256_CBC_SHA384:
  3907. case TLS_ECDH_RSA_WITH_AES_128_GCM_SHA256:
  3908. case TLS_ECDH_RSA_WITH_AES_256_GCM_SHA384:
  3909. if (oid == ECC_X25519_OID && defOid == oid) {
  3910. defOid = 0;
  3911. defSz = 80;
  3912. }
  3913. if (oid == ECC_X448_OID && defOid == oid) {
  3914. defOid = 0;
  3915. defSz = 80;
  3916. }
  3917. key |= ssl->pkCurveOID == oid;
  3918. break;
  3919. #endif /* HAVE_ECC && WOLFSSL_STATIC_DH */
  3920. #endif
  3921. default:
  3922. if (oid == ECC_X25519_OID && defOid == oid) {
  3923. defOid = 0;
  3924. defSz = 80;
  3925. }
  3926. if (oid == ECC_X448_OID && defOid == oid) {
  3927. defOid = 0;
  3928. defSz = 80;
  3929. }
  3930. key = 1;
  3931. break;
  3932. }
  3933. }
  3934. /* ChaCha20-Poly1305 ECC cipher suites */
  3935. if (first == CHACHA_BYTE) {
  3936. switch (second) {
  3937. #if defined(HAVE_ECC) || defined(HAVE_ED25519) || defined(HAVE_ED448)
  3938. /* ECDHE_ECDSA */
  3939. case TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256 :
  3940. case TLS_ECDHE_ECDSA_WITH_CHACHA20_OLD_POLY1305_SHA256 :
  3941. key |= ssl->ecdhCurveOID == oid;
  3942. ephmSuite = 1;
  3943. break;
  3944. #endif /* HAVE_ECC || HAVE_ED25519 || HAVE_ED448 */
  3945. #ifndef NO_RSA
  3946. /* ECDHE_RSA */
  3947. case TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256 :
  3948. case TLS_ECDHE_RSA_WITH_CHACHA20_OLD_POLY1305_SHA256 :
  3949. key |= ssl->ecdhCurveOID == oid;
  3950. ephmSuite = 1;
  3951. break;
  3952. #endif
  3953. default:
  3954. key = 1;
  3955. break;
  3956. }
  3957. }
  3958. }
  3959. /* Choose the default if it is at the required strength. */
  3960. #ifdef HAVE_ECC
  3961. if (ssl->ecdhCurveOID == 0 && defSz == ssl->eccTempKeySz)
  3962. #else
  3963. if (ssl->ecdhCurveOID == 0)
  3964. #endif
  3965. {
  3966. key = 1;
  3967. ssl->ecdhCurveOID = defOid;
  3968. }
  3969. /* Choose any curve at the required strength. */
  3970. if (ssl->ecdhCurveOID == 0) {
  3971. key = 1;
  3972. ssl->ecdhCurveOID = currOid;
  3973. }
  3974. /* Choose the default if it is at the next highest strength. */
  3975. if (ssl->ecdhCurveOID == 0 && defSz == nextSz)
  3976. ssl->ecdhCurveOID = defOid;
  3977. /* Choose any curve at the next highest strength. */
  3978. if (ssl->ecdhCurveOID == 0)
  3979. ssl->ecdhCurveOID = nextOid;
  3980. /* No curve and ephemeral ECC suite requires a matching curve. */
  3981. if (ssl->ecdhCurveOID == 0 && ephmSuite)
  3982. key = 0;
  3983. return key;
  3984. }
  3985. #endif
  3986. #endif /* NO_WOLFSSL_SERVER */
  3987. int TLSX_UseSupportedCurve(TLSX** extensions, word16 name, void* heap)
  3988. {
  3989. TLSX* extension = NULL;
  3990. SupportedCurve* curve = NULL;
  3991. int ret;
  3992. if (extensions == NULL) {
  3993. return BAD_FUNC_ARG;
  3994. }
  3995. #ifdef WOLFSSL_TLS13
  3996. if (! TLSX_KeyShare_IsSupported(name)) {
  3997. return BAD_FUNC_ARG;
  3998. }
  3999. #endif
  4000. extension = TLSX_Find(*extensions, TLSX_SUPPORTED_GROUPS);
  4001. if (!extension) {
  4002. ret = TLSX_SupportedCurve_New(&curve, name, heap);
  4003. if (ret != 0)
  4004. return ret;
  4005. ret = TLSX_Push(extensions, TLSX_SUPPORTED_GROUPS, curve, heap);
  4006. if (ret != 0) {
  4007. XFREE(curve, heap, DYNAMIC_TYPE_TLSX);
  4008. return ret;
  4009. }
  4010. }
  4011. else {
  4012. ret = TLSX_SupportedCurve_Append((SupportedCurve*)extension->data, name,
  4013. heap);
  4014. if (ret != 0)
  4015. return ret;
  4016. }
  4017. return WOLFSSL_SUCCESS;
  4018. }
  4019. int TLSX_UsePointFormat(TLSX** extensions, byte format, void* heap)
  4020. {
  4021. TLSX* extension = NULL;
  4022. PointFormat* point = NULL;
  4023. int ret = 0;
  4024. if (extensions == NULL)
  4025. return BAD_FUNC_ARG;
  4026. extension = TLSX_Find(*extensions, TLSX_EC_POINT_FORMATS);
  4027. if (!extension) {
  4028. ret = TLSX_PointFormat_New(&point, format, heap);
  4029. if (ret != 0)
  4030. return ret;
  4031. ret = TLSX_Push(extensions, TLSX_EC_POINT_FORMATS, point, heap);
  4032. if (ret != 0) {
  4033. XFREE(point, heap, DYNAMIC_TYPE_TLSX);
  4034. return ret;
  4035. }
  4036. }
  4037. else {
  4038. ret = TLSX_PointFormat_Append((PointFormat*)extension->data, format,
  4039. heap);
  4040. if (ret != 0)
  4041. return ret;
  4042. }
  4043. return WOLFSSL_SUCCESS;
  4044. }
  4045. #define EC_FREE_ALL TLSX_SupportedCurve_FreeAll
  4046. #define EC_VALIDATE_REQUEST TLSX_SupportedCurve_ValidateRequest
  4047. #ifndef NO_WOLFSSL_CLIENT
  4048. #define EC_GET_SIZE TLSX_SupportedCurve_GetSize
  4049. #define EC_WRITE TLSX_SupportedCurve_Write
  4050. #else
  4051. #define EC_GET_SIZE(list) 0
  4052. #define EC_WRITE(a, b) 0
  4053. #endif
  4054. #if !defined(NO_WOLFSSL_SERVER) || (defined(WOLFSSL_TLS13) && \
  4055. !defined(WOLFSSL_NO_SERVER_GROUPS_EXT))
  4056. #define EC_PARSE TLSX_SupportedCurve_Parse
  4057. #else
  4058. #define EC_PARSE(a, b, c, d) 0
  4059. #endif
  4060. #define PF_FREE_ALL TLSX_PointFormat_FreeAll
  4061. #define PF_VALIDATE_REQUEST TLSX_PointFormat_ValidateRequest
  4062. #define PF_VALIDATE_RESPONSE TLSX_PointFormat_ValidateResponse
  4063. #define PF_GET_SIZE TLSX_PointFormat_GetSize
  4064. #define PF_WRITE TLSX_PointFormat_Write
  4065. #ifndef NO_WOLFSSL_SERVER
  4066. #define PF_PARSE TLSX_PointFormat_Parse
  4067. #else
  4068. #define PF_PARSE(a, b, c, d) 0
  4069. #endif
  4070. #else
  4071. #define EC_FREE_ALL(list, heap)
  4072. #define EC_GET_SIZE(list) 0
  4073. #define EC_WRITE(a, b) 0
  4074. #define EC_PARSE(a, b, c, d) 0
  4075. #define EC_VALIDATE_REQUEST(a, b)
  4076. #define PF_FREE_ALL(list, heap)
  4077. #define PF_GET_SIZE(list) 0
  4078. #define PF_WRITE(a, b) 0
  4079. #define PF_PARSE(a, b, c, d) 0
  4080. #define PF_VALIDATE_REQUEST(a, b)
  4081. #define PF_VALIDATE_RESPONSE(a, b)
  4082. #endif /* HAVE_SUPPORTED_CURVES */
  4083. /******************************************************************************/
  4084. /* Renegotiation Indication */
  4085. /******************************************************************************/
  4086. #if defined(HAVE_SECURE_RENEGOTIATION) \
  4087. || defined(HAVE_SERVER_RENEGOTIATION_INFO)
  4088. static byte TLSX_SecureRenegotiation_GetSize(SecureRenegotiation* data,
  4089. int isRequest)
  4090. {
  4091. byte length = OPAQUE8_LEN; /* empty info length */
  4092. /* data will be NULL for HAVE_SERVER_RENEGOTIATION_INFO only */
  4093. if (data && data->enabled && data->verifySet) {
  4094. /* client sends client_verify_data only */
  4095. length += TLS_FINISHED_SZ;
  4096. /* server also sends server_verify_data */
  4097. if (!isRequest)
  4098. length += TLS_FINISHED_SZ;
  4099. }
  4100. return length;
  4101. }
  4102. static word16 TLSX_SecureRenegotiation_Write(SecureRenegotiation* data,
  4103. byte* output, int isRequest)
  4104. {
  4105. word16 offset = OPAQUE8_LEN; /* RenegotiationInfo length */
  4106. if (data && data->enabled && data->verifySet) {
  4107. /* client sends client_verify_data only */
  4108. XMEMCPY(output + offset, data->client_verify_data, TLS_FINISHED_SZ);
  4109. offset += TLS_FINISHED_SZ;
  4110. /* server also sends server_verify_data */
  4111. if (!isRequest) {
  4112. XMEMCPY(output + offset, data->server_verify_data, TLS_FINISHED_SZ);
  4113. offset += TLS_FINISHED_SZ;
  4114. }
  4115. }
  4116. output[0] = (byte)(offset - 1); /* info length - self */
  4117. return offset;
  4118. }
  4119. static int TLSX_SecureRenegotiation_Parse(WOLFSSL* ssl, const byte* input,
  4120. word16 length, byte isRequest)
  4121. {
  4122. int ret = SECURE_RENEGOTIATION_E;
  4123. if (length >= OPAQUE8_LEN) {
  4124. if (isRequest) {
  4125. #ifndef NO_WOLFSSL_SERVER
  4126. if (ssl->secure_renegotiation == NULL) {
  4127. ret = wolfSSL_UseSecureRenegotiation(ssl);
  4128. if (ret == WOLFSSL_SUCCESS)
  4129. ret = 0;
  4130. }
  4131. if (ret != 0 && ret != SECURE_RENEGOTIATION_E) {
  4132. }
  4133. else if (ssl->secure_renegotiation == NULL) {
  4134. }
  4135. else if (!ssl->secure_renegotiation->enabled) {
  4136. if (*input == 0) {
  4137. input++; /* get past size */
  4138. ssl->secure_renegotiation->enabled = 1;
  4139. TLSX_SetResponse(ssl, TLSX_RENEGOTIATION_INFO);
  4140. ret = 0;
  4141. }
  4142. else {
  4143. /* already in error state */
  4144. WOLFSSL_MSG("SCR client verify data present");
  4145. }
  4146. }
  4147. else if (*input == TLS_FINISHED_SZ) {
  4148. if (length < TLS_FINISHED_SZ + 1) {
  4149. WOLFSSL_MSG("SCR malformed buffer");
  4150. ret = BUFFER_E;
  4151. }
  4152. else {
  4153. input++; /* get past size */
  4154. /* validate client verify data */
  4155. if (XMEMCMP(input,
  4156. ssl->secure_renegotiation->client_verify_data,
  4157. TLS_FINISHED_SZ) == 0) {
  4158. WOLFSSL_MSG("SCR client verify data match");
  4159. TLSX_SetResponse(ssl, TLSX_RENEGOTIATION_INFO);
  4160. ret = 0; /* verified */
  4161. }
  4162. else {
  4163. /* already in error state */
  4164. WOLFSSL_MSG("SCR client verify data Failure");
  4165. }
  4166. }
  4167. }
  4168. #endif
  4169. }
  4170. else if (ssl->secure_renegotiation != NULL) {
  4171. #ifndef NO_WOLFSSL_CLIENT
  4172. if (!ssl->secure_renegotiation->enabled) {
  4173. if (*input == 0) {
  4174. ssl->secure_renegotiation->enabled = 1;
  4175. ret = 0;
  4176. }
  4177. }
  4178. else if (*input == 2 * TLS_FINISHED_SZ &&
  4179. length == 2 * TLS_FINISHED_SZ + OPAQUE8_LEN) {
  4180. input++; /* get past size */
  4181. /* validate client and server verify data */
  4182. if (XMEMCMP(input,
  4183. ssl->secure_renegotiation->client_verify_data,
  4184. TLS_FINISHED_SZ) == 0 &&
  4185. XMEMCMP(input + TLS_FINISHED_SZ,
  4186. ssl->secure_renegotiation->server_verify_data,
  4187. TLS_FINISHED_SZ) == 0) {
  4188. WOLFSSL_MSG("SCR client and server verify data match");
  4189. ret = 0; /* verified */
  4190. }
  4191. else {
  4192. /* already in error state */
  4193. WOLFSSL_MSG("SCR client and server verify data Failure");
  4194. }
  4195. }
  4196. #endif
  4197. }
  4198. }
  4199. if (ret != 0) {
  4200. WOLFSSL_ERROR_VERBOSE(ret);
  4201. SendAlert(ssl, alert_fatal, handshake_failure);
  4202. }
  4203. return ret;
  4204. }
  4205. int TLSX_UseSecureRenegotiation(TLSX** extensions, void* heap)
  4206. {
  4207. int ret = 0;
  4208. SecureRenegotiation* data;
  4209. data = (SecureRenegotiation*)XMALLOC(sizeof(SecureRenegotiation), heap,
  4210. DYNAMIC_TYPE_TLSX);
  4211. if (data == NULL)
  4212. return MEMORY_E;
  4213. XMEMSET(data, 0, sizeof(SecureRenegotiation));
  4214. ret = TLSX_Push(extensions, TLSX_RENEGOTIATION_INFO, data, heap);
  4215. if (ret != 0) {
  4216. XFREE(data, heap, DYNAMIC_TYPE_TLSX);
  4217. return ret;
  4218. }
  4219. return WOLFSSL_SUCCESS;
  4220. }
  4221. #ifdef HAVE_SERVER_RENEGOTIATION_INFO
  4222. int TLSX_AddEmptyRenegotiationInfo(TLSX** extensions, void* heap)
  4223. {
  4224. int ret;
  4225. /* send empty renegotiation_info extension */
  4226. TLSX* ext = TLSX_Find(*extensions, TLSX_RENEGOTIATION_INFO);
  4227. if (ext == NULL) {
  4228. ret = TLSX_UseSecureRenegotiation(extensions, heap);
  4229. if (ret != WOLFSSL_SUCCESS)
  4230. return ret;
  4231. ext = TLSX_Find(*extensions, TLSX_RENEGOTIATION_INFO);
  4232. }
  4233. if (ext)
  4234. ext->resp = 1;
  4235. return WOLFSSL_SUCCESS;
  4236. }
  4237. #endif /* HAVE_SERVER_RENEGOTIATION_INFO */
  4238. #define SCR_FREE_ALL(data, heap) XFREE(data, (heap), DYNAMIC_TYPE_TLSX)
  4239. #define SCR_GET_SIZE TLSX_SecureRenegotiation_GetSize
  4240. #define SCR_WRITE TLSX_SecureRenegotiation_Write
  4241. #define SCR_PARSE TLSX_SecureRenegotiation_Parse
  4242. #else
  4243. #define SCR_FREE_ALL(a, heap)
  4244. #define SCR_GET_SIZE(a, b) 0
  4245. #define SCR_WRITE(a, b, c) 0
  4246. #define SCR_PARSE(a, b, c, d) 0
  4247. #endif /* HAVE_SECURE_RENEGOTIATION || HAVE_SERVER_RENEGOTIATION_INFO */
  4248. /******************************************************************************/
  4249. /* Session Tickets */
  4250. /******************************************************************************/
  4251. #ifdef HAVE_SESSION_TICKET
  4252. #if defined(WOLFSSL_TLS13) || !defined(NO_WOLFSSL_CLIENT)
  4253. static void TLSX_SessionTicket_ValidateRequest(WOLFSSL* ssl)
  4254. {
  4255. TLSX* extension = TLSX_Find(ssl->extensions, TLSX_SESSION_TICKET);
  4256. SessionTicket* ticket = extension ?
  4257. (SessionTicket*)extension->data : NULL;
  4258. if (ticket) {
  4259. /* TODO validate ticket timeout here! */
  4260. if (ticket->lifetime == 0xfffffff) {
  4261. /* send empty ticket on timeout */
  4262. TLSX_UseSessionTicket(&ssl->extensions, NULL, ssl->heap);
  4263. }
  4264. }
  4265. }
  4266. #endif /* WOLFSSL_TLS13 || !NO_WOLFSSL_CLIENT */
  4267. static word16 TLSX_SessionTicket_GetSize(SessionTicket* ticket, int isRequest)
  4268. {
  4269. (void)isRequest;
  4270. return ticket ? ticket->size : 0;
  4271. }
  4272. static word16 TLSX_SessionTicket_Write(SessionTicket* ticket, byte* output,
  4273. int isRequest)
  4274. {
  4275. word16 offset = 0; /* empty ticket */
  4276. if (isRequest && ticket) {
  4277. XMEMCPY(output + offset, ticket->data, ticket->size);
  4278. offset += ticket->size;
  4279. }
  4280. return offset;
  4281. }
  4282. static int TLSX_SessionTicket_Parse(WOLFSSL* ssl, const byte* input,
  4283. word16 length, byte isRequest)
  4284. {
  4285. int ret = 0;
  4286. (void) input; /* avoid unused parameter if NO_WOLFSSL_SERVER defined */
  4287. if (!isRequest) {
  4288. if (TLSX_CheckUnsupportedExtension(ssl, TLSX_SESSION_TICKET))
  4289. return TLSX_HandleUnsupportedExtension(ssl);
  4290. if (length != 0)
  4291. return BUFFER_ERROR;
  4292. #ifndef NO_WOLFSSL_CLIENT
  4293. ssl->expect_session_ticket = 1;
  4294. #endif
  4295. }
  4296. #ifndef NO_WOLFSSL_SERVER
  4297. else {
  4298. /* server side */
  4299. if (ssl->ctx->ticketEncCb == NULL) {
  4300. WOLFSSL_MSG("Client sent session ticket, server has no callback");
  4301. return 0;
  4302. }
  4303. if (length > SESSION_TICKET_LEN) {
  4304. ret = BAD_TICKET_MSG_SZ;
  4305. WOLFSSL_ERROR_VERBOSE(ret);
  4306. } else if (IsAtLeastTLSv1_3(ssl->version)) {
  4307. WOLFSSL_MSG("Process client ticket rejected, TLS 1.3 no support");
  4308. ssl->options.rejectTicket = 1;
  4309. ret = 0; /* not fatal */
  4310. } else if (ssl->options.noTicketTls12) {
  4311. /* ignore ticket request */
  4312. } else if (length == 0) {
  4313. /* blank ticket */
  4314. ret = TLSX_UseSessionTicket(&ssl->extensions, NULL, ssl->heap);
  4315. if (ret == WOLFSSL_SUCCESS) {
  4316. ret = 0;
  4317. /* send blank ticket */
  4318. TLSX_SetResponse(ssl, TLSX_SESSION_TICKET);
  4319. ssl->options.createTicket = 1; /* will send ticket msg */
  4320. ssl->options.useTicket = 1;
  4321. ssl->options.resuming = 0; /* no standard resumption */
  4322. ssl->arrays->sessionIDSz = 0; /* no echo on blank ticket */
  4323. }
  4324. } else {
  4325. /* got actual ticket from client */
  4326. ret = DoClientTicket(ssl, input, length);
  4327. if (ret == WOLFSSL_TICKET_RET_OK) { /* use ticket to resume */
  4328. WOLFSSL_MSG("Using existing client ticket");
  4329. ssl->options.useTicket = 1;
  4330. ssl->options.resuming = 1;
  4331. /* SERVER: ticket is peer auth. */
  4332. ssl->options.peerAuthGood = 1;
  4333. } else if (ret == WOLFSSL_TICKET_RET_CREATE) {
  4334. WOLFSSL_MSG("Using existing client ticket, creating new one");
  4335. ret = TLSX_UseSessionTicket(&ssl->extensions, NULL, ssl->heap);
  4336. if (ret == WOLFSSL_SUCCESS) {
  4337. ret = 0;
  4338. TLSX_SetResponse(ssl, TLSX_SESSION_TICKET);
  4339. /* send blank ticket */
  4340. ssl->options.createTicket = 1; /* will send ticket msg */
  4341. ssl->options.useTicket = 1;
  4342. ssl->options.resuming = 1;
  4343. /* SERVER: ticket is peer auth. */
  4344. ssl->options.peerAuthGood = 1;
  4345. }
  4346. } else if (ret == WOLFSSL_TICKET_RET_REJECT) {
  4347. WOLFSSL_MSG("Process client ticket rejected, not using");
  4348. ssl->options.rejectTicket = 1;
  4349. ret = 0; /* not fatal */
  4350. } else if (ret == VERSION_ERROR) {
  4351. WOLFSSL_MSG("Process client ticket rejected, bad TLS version");
  4352. ssl->options.rejectTicket = 1;
  4353. ret = 0; /* not fatal */
  4354. } else if (ret == WOLFSSL_TICKET_RET_FATAL) {
  4355. WOLFSSL_MSG("Process client ticket fatal error, not using");
  4356. } else if (ret < 0) {
  4357. WOLFSSL_MSG("Process client ticket unknown error, not using");
  4358. }
  4359. }
  4360. }
  4361. #endif /* NO_WOLFSSL_SERVER */
  4362. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  4363. (void)ssl;
  4364. #endif
  4365. return ret;
  4366. }
  4367. WOLFSSL_LOCAL SessionTicket* TLSX_SessionTicket_Create(word32 lifetime,
  4368. byte* data, word16 size, void* heap)
  4369. {
  4370. SessionTicket* ticket = (SessionTicket*)XMALLOC(sizeof(SessionTicket),
  4371. heap, DYNAMIC_TYPE_TLSX);
  4372. if (ticket) {
  4373. ticket->data = (byte*)XMALLOC(size, heap, DYNAMIC_TYPE_TLSX);
  4374. if (ticket->data == NULL) {
  4375. XFREE(ticket, heap, DYNAMIC_TYPE_TLSX);
  4376. return NULL;
  4377. }
  4378. XMEMCPY(ticket->data, data, size);
  4379. ticket->size = size;
  4380. ticket->lifetime = lifetime;
  4381. }
  4382. (void)heap;
  4383. return ticket;
  4384. }
  4385. WOLFSSL_LOCAL void TLSX_SessionTicket_Free(SessionTicket* ticket, void* heap)
  4386. {
  4387. if (ticket) {
  4388. XFREE(ticket->data, heap, DYNAMIC_TYPE_TLSX);
  4389. XFREE(ticket, heap, DYNAMIC_TYPE_TLSX);
  4390. }
  4391. (void)heap;
  4392. }
  4393. int TLSX_UseSessionTicket(TLSX** extensions, SessionTicket* ticket, void* heap)
  4394. {
  4395. int ret = 0;
  4396. if (extensions == NULL)
  4397. return BAD_FUNC_ARG;
  4398. /* If the ticket is NULL, the client will request a new ticket from the
  4399. server. Otherwise, the client will use it in the next client hello. */
  4400. if ((ret = TLSX_Push(extensions, TLSX_SESSION_TICKET, (void*)ticket, heap))
  4401. != 0)
  4402. return ret;
  4403. return WOLFSSL_SUCCESS;
  4404. }
  4405. #define WOLF_STK_VALIDATE_REQUEST TLSX_SessionTicket_ValidateRequest
  4406. #define WOLF_STK_GET_SIZE TLSX_SessionTicket_GetSize
  4407. #define WOLF_STK_WRITE TLSX_SessionTicket_Write
  4408. #define WOLF_STK_PARSE TLSX_SessionTicket_Parse
  4409. #define WOLF_STK_FREE(stk, heap) TLSX_SessionTicket_Free((SessionTicket*)(stk),(heap))
  4410. #else
  4411. #define WOLF_STK_FREE(a, b)
  4412. #define WOLF_STK_VALIDATE_REQUEST(a)
  4413. #define WOLF_STK_GET_SIZE(a, b) 0
  4414. #define WOLF_STK_WRITE(a, b, c) 0
  4415. #define WOLF_STK_PARSE(a, b, c, d) 0
  4416. #endif /* HAVE_SESSION_TICKET */
  4417. #if defined(HAVE_ENCRYPT_THEN_MAC) && !defined(WOLFSSL_AEAD_ONLY)
  4418. /******************************************************************************/
  4419. /* Encrypt-then-MAC */
  4420. /******************************************************************************/
  4421. #ifndef WOLFSSL_NO_TLS12
  4422. static int TLSX_EncryptThenMac_Use(WOLFSSL* ssl);
  4423. /**
  4424. * Get the size of the Encrypt-Then-MAC extension.
  4425. *
  4426. * msgType Type of message to put extension into.
  4427. * pSz Size of extension data.
  4428. * return SANITY_MSG_E when the message is not allowed to have extension and
  4429. * 0 otherwise.
  4430. */
  4431. static int TLSX_EncryptThenMac_GetSize(byte msgType, word16* pSz)
  4432. {
  4433. (void)pSz;
  4434. if (msgType != client_hello && msgType != server_hello) {
  4435. WOLFSSL_ERROR_VERBOSE(SANITY_MSG_E);
  4436. return SANITY_MSG_E;
  4437. }
  4438. /* Empty extension */
  4439. return 0;
  4440. }
  4441. /**
  4442. * Write the Encrypt-Then-MAC extension.
  4443. *
  4444. * data Unused
  4445. * output Extension data buffer. Unused.
  4446. * msgType Type of message to put extension into.
  4447. * pSz Size of extension data.
  4448. * return SANITY_MSG_E when the message is not allowed to have extension and
  4449. * 0 otherwise.
  4450. */
  4451. static int TLSX_EncryptThenMac_Write(void* data, byte* output, byte msgType,
  4452. word16* pSz)
  4453. {
  4454. (void)data;
  4455. (void)output;
  4456. (void)pSz;
  4457. if (msgType != client_hello && msgType != server_hello) {
  4458. WOLFSSL_ERROR_VERBOSE(SANITY_MSG_E);
  4459. return SANITY_MSG_E;
  4460. }
  4461. /* Empty extension */
  4462. return 0;
  4463. }
  4464. /**
  4465. * Parse the Encrypt-Then-MAC extension.
  4466. *
  4467. * ssl SSL object
  4468. * input Extension data buffer.
  4469. * length Length of this extension's data.
  4470. * msgType Type of message to extension appeared in.
  4471. * return SANITY_MSG_E when the message is not allowed to have extension,
  4472. * BUFFER_ERROR when the extension's data is invalid,
  4473. * MEMORY_E when unable to allocate memory and
  4474. * 0 otherwise.
  4475. */
  4476. static int TLSX_EncryptThenMac_Parse(WOLFSSL* ssl, const byte* input,
  4477. word16 length, byte msgType)
  4478. {
  4479. int ret;
  4480. (void)input;
  4481. if (msgType != client_hello && msgType != server_hello) {
  4482. WOLFSSL_ERROR_VERBOSE(SANITY_MSG_E);
  4483. return SANITY_MSG_E;
  4484. }
  4485. /* Empty extension */
  4486. if (length != 0)
  4487. return BUFFER_ERROR;
  4488. if (msgType == client_hello) {
  4489. /* Check the user hasn't disallowed use of Encrypt-Then-Mac. */
  4490. if (!ssl->options.disallowEncThenMac) {
  4491. ssl->options.encThenMac = 1;
  4492. /* Set the extension reply. */
  4493. ret = TLSX_EncryptThenMac_Use(ssl);
  4494. if (ret != 0)
  4495. return ret;
  4496. }
  4497. return 0;
  4498. }
  4499. /* Server Hello */
  4500. if (ssl->options.disallowEncThenMac) {
  4501. WOLFSSL_ERROR_VERBOSE(SANITY_MSG_E);
  4502. return SANITY_MSG_E;
  4503. }
  4504. ssl->options.encThenMac = 1;
  4505. return 0;
  4506. }
  4507. /**
  4508. * Add the Encrypt-Then-MAC extension to list.
  4509. *
  4510. * ssl SSL object
  4511. * return MEMORY_E when unable to allocate memory and 0 otherwise.
  4512. */
  4513. static int TLSX_EncryptThenMac_Use(WOLFSSL* ssl)
  4514. {
  4515. int ret = 0;
  4516. TLSX* extension;
  4517. /* Find the Encrypt-Then-Mac extension if it exists. */
  4518. extension = TLSX_Find(ssl->extensions, TLSX_ENCRYPT_THEN_MAC);
  4519. if (extension == NULL) {
  4520. /* Push new Encrypt-Then-Mac extension. */
  4521. ret = TLSX_Push(&ssl->extensions, TLSX_ENCRYPT_THEN_MAC, NULL,
  4522. ssl->heap);
  4523. if (ret != 0)
  4524. return ret;
  4525. }
  4526. return 0;
  4527. }
  4528. /**
  4529. * Set the Encrypt-Then-MAC extension as one to respond too.
  4530. *
  4531. * ssl SSL object
  4532. * return EXT_MISSING when EncryptThenMac extension not in list.
  4533. */
  4534. int TLSX_EncryptThenMac_Respond(WOLFSSL* ssl)
  4535. {
  4536. TLSX* extension;
  4537. extension = TLSX_Find(ssl->extensions, TLSX_ENCRYPT_THEN_MAC);
  4538. if (extension == NULL)
  4539. return EXT_MISSING;
  4540. extension->resp = 1;
  4541. return 0;
  4542. }
  4543. #define ETM_GET_SIZE TLSX_EncryptThenMac_GetSize
  4544. #define ETM_WRITE TLSX_EncryptThenMac_Write
  4545. #define ETM_PARSE TLSX_EncryptThenMac_Parse
  4546. #else
  4547. #define ETM_GET_SIZE(a, b) 0
  4548. #define ETM_WRITE(a, b, c, d) 0
  4549. #define ETM_PARSE(a, b, c, d) 0
  4550. #endif /* !WOLFSSL_NO_TLS12 */
  4551. #endif /* HAVE_ENCRYPT_THEN_MAC && !WOLFSSL_AEAD_ONLY */
  4552. #ifdef WOLFSSL_SRTP
  4553. /******************************************************************************/
  4554. /* DTLS SRTP (Secure Real-time Transport Protocol) */
  4555. /******************************************************************************/
  4556. /* Only support single SRTP profile */
  4557. typedef struct TlsxSrtp {
  4558. word16 profileCount;
  4559. word16 ids; /* selected bits */
  4560. } TlsxSrtp;
  4561. static int TLSX_UseSRTP_GetSize(TlsxSrtp *srtp)
  4562. {
  4563. /* SRTP Profile Len (2)
  4564. * SRTP Profiles (2)
  4565. * MKI (master key id) Length */
  4566. return (OPAQUE16_LEN + (srtp->profileCount * OPAQUE16_LEN) + 1);
  4567. }
  4568. static TlsxSrtp* TLSX_UseSRTP_New(word16 ids, void* heap)
  4569. {
  4570. TlsxSrtp* srtp;
  4571. int i;
  4572. srtp = (TlsxSrtp*)XMALLOC(sizeof(TlsxSrtp), heap, DYNAMIC_TYPE_TLSX);
  4573. if (srtp == NULL) {
  4574. WOLFSSL_MSG("TLSX SRTP Memory failure");
  4575. return NULL;
  4576. }
  4577. /* count and test each bit set */
  4578. srtp->profileCount = 0;
  4579. for (i=0; i<16; i++) {
  4580. if (ids & (1 << i)) {
  4581. srtp->profileCount++;
  4582. }
  4583. }
  4584. srtp->ids = ids;
  4585. return srtp;
  4586. }
  4587. static void TLSX_UseSRTP_Free(TlsxSrtp *srtp, void* heap)
  4588. {
  4589. if (srtp != NULL) {
  4590. XFREE(srtp, heap, DYNAMIC_TYPE_TLSX);
  4591. }
  4592. (void)heap;
  4593. }
  4594. static int TLSX_UseSRTP_Parse(WOLFSSL* ssl, const byte* input, word16 length,
  4595. byte isRequest)
  4596. {
  4597. int ret = BAD_FUNC_ARG;
  4598. word16 profile_len = 0;
  4599. word16 profile_value = 0;
  4600. word16 offset = 0;
  4601. #ifndef NO_WOLFSSL_SERVER
  4602. int i;
  4603. TlsxSrtp* srtp = NULL;
  4604. #endif
  4605. if (length < OPAQUE16_LEN) {
  4606. return BUFFER_ERROR;
  4607. }
  4608. /* reset selected DTLS SRTP profile ID */
  4609. ssl->dtlsSrtpId = 0;
  4610. /* total length, not include itself */
  4611. ato16(input, &profile_len);
  4612. offset += OPAQUE16_LEN;
  4613. if (!isRequest) {
  4614. #ifndef NO_WOLFSSL_CLIENT
  4615. if (length < offset + OPAQUE16_LEN)
  4616. return BUFFER_ERROR;
  4617. ato16(input + offset, &profile_value);
  4618. /* check that the profile received was in the ones we support */
  4619. if (profile_value < 16 &&
  4620. (ssl->dtlsSrtpProfiles & (1 << profile_value))) {
  4621. ssl->dtlsSrtpId = profile_value;
  4622. ret = 0; /* success */
  4623. }
  4624. #endif
  4625. }
  4626. #ifndef NO_WOLFSSL_SERVER
  4627. else {
  4628. /* parse remainder one profile at a time, looking for match in CTX */
  4629. ret = 0;
  4630. for (i=offset; i<length; i+=OPAQUE16_LEN) {
  4631. ato16(input+i, &profile_value);
  4632. /* find first match */
  4633. if (profile_value < 16 &&
  4634. ssl->dtlsSrtpProfiles & (1 << profile_value)) {
  4635. ssl->dtlsSrtpId = profile_value;
  4636. /* make sure we respond with selected SRTP id selected */
  4637. srtp = TLSX_UseSRTP_New((1 << profile_value), ssl->heap);
  4638. if (srtp != NULL) {
  4639. ret = TLSX_Push(&ssl->extensions, TLSX_USE_SRTP,
  4640. (void*)srtp, ssl->heap);
  4641. if (ret == 0) {
  4642. TLSX_SetResponse(ssl, TLSX_USE_SRTP);
  4643. /* successfully set extension */
  4644. }
  4645. }
  4646. else {
  4647. ret = MEMORY_E;
  4648. }
  4649. break;
  4650. }
  4651. }
  4652. }
  4653. if (ret == 0 && ssl->dtlsSrtpId == 0) {
  4654. WOLFSSL_MSG("TLSX_UseSRTP_Parse profile not found!");
  4655. /* not fatal */
  4656. }
  4657. else if (ret != 0) {
  4658. ssl->dtlsSrtpId = 0;
  4659. TLSX_UseSRTP_Free(srtp, ssl->heap);
  4660. }
  4661. #endif
  4662. (void)profile_len;
  4663. return ret;
  4664. }
  4665. static word16 TLSX_UseSRTP_Write(TlsxSrtp* srtp, byte* output)
  4666. {
  4667. word16 offset = 0;
  4668. int i, j;
  4669. c16toa(srtp->profileCount*2, output+offset);
  4670. offset += OPAQUE16_LEN;
  4671. for (i=0; i< srtp->profileCount; i+=2) {
  4672. for (j=0; j<16; j++) {
  4673. if (srtp->ids & (1 << j)) {
  4674. c16toa(j, output+offset);
  4675. offset += OPAQUE16_LEN;
  4676. }
  4677. }
  4678. }
  4679. output[offset++] = 0x00; /* MKI Length */
  4680. return offset;
  4681. }
  4682. static int TLSX_UseSRTP(TLSX** extensions, word16 profiles, void* heap)
  4683. {
  4684. int ret = 0;
  4685. TLSX* extension;
  4686. if (extensions == NULL) {
  4687. return BAD_FUNC_ARG;
  4688. }
  4689. extension = TLSX_Find(*extensions, TLSX_USE_SRTP);
  4690. if (extension == NULL) {
  4691. TlsxSrtp* srtp = TLSX_UseSRTP_New(profiles, heap);
  4692. if (srtp == NULL) {
  4693. return MEMORY_E;
  4694. }
  4695. ret = TLSX_Push(extensions, TLSX_USE_SRTP, (void*)srtp, heap);
  4696. if (ret != 0) {
  4697. TLSX_UseSRTP_Free(srtp, heap);
  4698. }
  4699. }
  4700. return ret;
  4701. }
  4702. #ifndef NO_WOLFSSL_SERVER
  4703. #define SRTP_FREE TLSX_UseSRTP_Free
  4704. #define SRTP_PARSE TLSX_UseSRTP_Parse
  4705. #define SRTP_WRITE TLSX_UseSRTP_Write
  4706. #define SRTP_GET_SIZE TLSX_UseSRTP_GetSize
  4707. #else
  4708. #define SRTP_FREE(a, b)
  4709. #define SRTP_PARSE(a, b, c, d) 0
  4710. #define SRTP_WRITE(a, b) 0
  4711. #define SRTP_GET_SIZE(a) 0
  4712. #endif
  4713. #endif /* WOLFSSL_SRTP */
  4714. /******************************************************************************/
  4715. /* Supported Versions */
  4716. /******************************************************************************/
  4717. #ifdef WOLFSSL_TLS13
  4718. static WC_INLINE int versionIsGreater(byte isDtls, byte a, byte b)
  4719. {
  4720. (void)isDtls;
  4721. #ifdef WOLFSSL_DTLS
  4722. /* DTLS version increases backwards (-1,-2,-3,etc) */
  4723. if (isDtls)
  4724. return a < b;
  4725. #endif /* WOLFSSL_DTLS */
  4726. return a > b;
  4727. }
  4728. static WC_INLINE int versionIsLesser(byte isDtls, byte a, byte b)
  4729. {
  4730. (void)isDtls;
  4731. #ifdef WOLFSSL_DTLS
  4732. /* DTLS version increases backwards (-1,-2,-3,etc) */
  4733. if (isDtls)
  4734. return a > b;
  4735. #endif /* WOLFSSL_DTLS */
  4736. return a < b;
  4737. }
  4738. static WC_INLINE int versionIsAtLeast(byte isDtls, byte a, byte b)
  4739. {
  4740. (void)isDtls;
  4741. #ifdef WOLFSSL_DTLS
  4742. /* DTLS version increases backwards (-1,-2,-3,etc) */
  4743. if (isDtls)
  4744. return a <= b;
  4745. #endif /* WOLFSSL_DTLS */
  4746. return a >= b;
  4747. }
  4748. static WC_INLINE int versionIsLessEqual(byte isDtls, byte a, byte b)
  4749. {
  4750. (void)isDtls;
  4751. #ifdef WOLFSSL_DTLS
  4752. /* DTLS version increases backwards (-1,-2,-3,etc) */
  4753. if (isDtls)
  4754. return a >= b;
  4755. #endif /* WOLFSSL_DTLS */
  4756. return a <= b;
  4757. }
  4758. /* Return the size of the SupportedVersions extension's data.
  4759. *
  4760. * data The SSL/TLS object.
  4761. * msgType The type of the message this extension is being written into.
  4762. * returns the length of data that will be in the extension.
  4763. */
  4764. static int TLSX_SupportedVersions_GetSize(void* data, byte msgType, word16* pSz)
  4765. {
  4766. WOLFSSL* ssl = (WOLFSSL*)data;
  4767. byte tls13Minor, tls12Minor, tls11Minor, isDtls;
  4768. isDtls = !!ssl->options.dtls;
  4769. tls13Minor = (byte)(isDtls ? DTLSv1_3_MINOR : TLSv1_3_MINOR);
  4770. tls12Minor = (byte)(isDtls ? DTLSv1_2_MINOR : TLSv1_2_MINOR);
  4771. tls11Minor = (byte)(isDtls ? DTLS_MINOR : TLSv1_1_MINOR);
  4772. /* unused on some configuration */
  4773. (void)tls12Minor;
  4774. (void)tls13Minor;
  4775. (void)tls11Minor;
  4776. if (msgType == client_hello) {
  4777. /* TLS v1.2 and TLS v1.3 */
  4778. int cnt = 0;
  4779. if (versionIsLessEqual(isDtls, ssl->options.minDowngrade, tls13Minor)
  4780. #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER) || \
  4781. defined(WOLFSSL_WPAS_SMALL)
  4782. && (ssl->options.mask & SSL_OP_NO_TLSv1_3) == 0
  4783. #endif
  4784. ) {
  4785. cnt++;
  4786. }
  4787. if (ssl->options.downgrade) {
  4788. #ifndef WOLFSSL_NO_TLS12
  4789. if (versionIsLessEqual(
  4790. isDtls, ssl->options.minDowngrade, tls12Minor)
  4791. #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER) || \
  4792. defined(WOLFSSL_WPAS_SMALL)
  4793. && (ssl->options.mask & SSL_OP_NO_TLSv1_2) == 0
  4794. #endif
  4795. ) {
  4796. cnt++;
  4797. }
  4798. #endif
  4799. #ifndef NO_OLD_TLS
  4800. if (versionIsLessEqual(
  4801. isDtls, ssl->options.minDowngrade, tls11Minor)
  4802. #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER) || \
  4803. defined(WOLFSSL_WPAS_SMALL)
  4804. && (ssl->options.mask & SSL_OP_NO_TLSv1_1) == 0
  4805. #endif
  4806. ) {
  4807. cnt++;
  4808. }
  4809. #ifdef WOLFSSL_ALLOW_TLSV10
  4810. if (!ssl->options.dtls && (ssl->options.minDowngrade <= TLSv1_MINOR)
  4811. #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER) || \
  4812. defined(WOLFSSL_WPAS_SMALL)
  4813. && (ssl->options.mask & SSL_OP_NO_TLSv1) == 0
  4814. #endif
  4815. ) {
  4816. cnt++;
  4817. }
  4818. #endif
  4819. #endif
  4820. }
  4821. *pSz += (word16)(OPAQUE8_LEN + cnt * OPAQUE16_LEN);
  4822. }
  4823. else if (msgType == server_hello || msgType == hello_retry_request) {
  4824. *pSz += OPAQUE16_LEN;
  4825. }
  4826. else {
  4827. WOLFSSL_ERROR_VERBOSE(SANITY_MSG_E);
  4828. return SANITY_MSG_E;
  4829. }
  4830. return 0;
  4831. }
  4832. /* Writes the SupportedVersions extension into the buffer.
  4833. *
  4834. * data The SSL/TLS object.
  4835. * output The buffer to write the extension into.
  4836. * msgType The type of the message this extension is being written into.
  4837. * returns the length of data that was written.
  4838. */
  4839. static int TLSX_SupportedVersions_Write(void* data, byte* output,
  4840. byte msgType, word16* pSz)
  4841. {
  4842. WOLFSSL* ssl = (WOLFSSL*)data;
  4843. byte major;
  4844. byte* cnt;
  4845. byte tls13minor, tls12minor, tls11minor, isDtls = 0;
  4846. tls13minor = (byte)TLSv1_3_MINOR;
  4847. tls12minor = (byte)TLSv1_2_MINOR;
  4848. tls11minor = (byte)TLSv1_1_MINOR;
  4849. /* unused in some configuration */
  4850. (void)tls11minor;
  4851. (void)tls12minor;
  4852. #ifdef WOLFSSL_DTLS13
  4853. if (ssl->options.dtls) {
  4854. tls13minor = (byte)DTLSv1_3_MINOR;
  4855. tls12minor = (byte)DTLSv1_2_MINOR;
  4856. tls11minor = (byte)DTLS_MINOR;
  4857. isDtls = 1;
  4858. }
  4859. #endif /* WOLFSSL_DTLS13 */
  4860. if (msgType == client_hello) {
  4861. major = ssl->ctx->method->version.major;
  4862. cnt = output++;
  4863. *cnt = 0;
  4864. if (versionIsLessEqual(isDtls, ssl->options.minDowngrade, tls13minor)
  4865. #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER) || \
  4866. defined(WOLFSSL_WPAS_SMALL)
  4867. && (ssl->options.mask & SSL_OP_NO_TLSv1_3) == 0
  4868. #endif
  4869. ) {
  4870. *cnt += OPAQUE16_LEN;
  4871. #ifdef WOLFSSL_TLS13_DRAFT
  4872. /* The TLS draft major number. */
  4873. *(output++) = TLS_DRAFT_MAJOR;
  4874. /* Version of draft supported. */
  4875. *(output++) = TLS_DRAFT_MINOR;
  4876. #else
  4877. *(output++) = major;
  4878. *(output++) = tls13minor;
  4879. #endif
  4880. }
  4881. if (ssl->options.downgrade) {
  4882. #ifndef WOLFSSL_NO_TLS12
  4883. if (versionIsLessEqual(isDtls, ssl->options.minDowngrade, tls12minor)
  4884. #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER) || \
  4885. defined(WOLFSSL_WPAS_SMALL)
  4886. && (ssl->options.mask & SSL_OP_NO_TLSv1_2) == 0
  4887. #endif
  4888. ) {
  4889. *cnt += OPAQUE16_LEN;
  4890. *(output++) = major;
  4891. *(output++) = tls12minor;
  4892. }
  4893. #endif
  4894. #ifndef NO_OLD_TLS
  4895. if (versionIsLessEqual(isDtls, ssl->options.minDowngrade, tls11minor)
  4896. #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER) || \
  4897. defined(WOLFSSL_WPAS_SMALL)
  4898. && (ssl->options.mask & SSL_OP_NO_TLSv1_1) == 0
  4899. #endif
  4900. ) {
  4901. *cnt += OPAQUE16_LEN;
  4902. *(output++) = major;
  4903. *(output++) = tls11minor;
  4904. }
  4905. #ifdef WOLFSSL_ALLOW_TLSV10
  4906. if (!ssl->options.dtls && (ssl->options.minDowngrade <= TLSv1_MINOR)
  4907. #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER) || \
  4908. defined(WOLFSSL_WPAS_SMALL)
  4909. && (ssl->options.mask & SSL_OP_NO_TLSv1) == 0
  4910. #endif
  4911. ) {
  4912. *cnt += OPAQUE16_LEN;
  4913. *(output++) = major;
  4914. *(output++) = (byte)TLSv1_MINOR;
  4915. }
  4916. #endif
  4917. #endif
  4918. }
  4919. *pSz += (word16)(OPAQUE8_LEN + *cnt);
  4920. }
  4921. else if (msgType == server_hello || msgType == hello_retry_request) {
  4922. output[0] = ssl->version.major;
  4923. output[1] = ssl->version.minor;
  4924. *pSz += OPAQUE16_LEN;
  4925. }
  4926. else {
  4927. WOLFSSL_ERROR_VERBOSE(SANITY_MSG_E);
  4928. return SANITY_MSG_E;
  4929. }
  4930. return 0;
  4931. }
  4932. /* Parse the SupportedVersions extension.
  4933. *
  4934. * ssl The SSL/TLS object.
  4935. * input The buffer with the extension data.
  4936. * length The length of the extension data.
  4937. * msgType The type of the message this extension is being parsed from.
  4938. * returns 0 on success, otherwise failure.
  4939. */
  4940. static int TLSX_SupportedVersions_Parse(WOLFSSL* ssl, const byte* input,
  4941. word16 length, byte msgType)
  4942. {
  4943. ProtocolVersion pv = ssl->ctx->method->version;
  4944. int i;
  4945. int len;
  4946. byte newMinor = 0;
  4947. int set = 0;
  4948. int ret;
  4949. byte major, minor;
  4950. byte tls13minor, tls12minor;
  4951. byte isDtls;
  4952. tls13minor = TLSv1_3_MINOR;
  4953. tls12minor = TLSv1_2_MINOR;
  4954. isDtls = ssl->options.dtls == 1;
  4955. #ifdef WOLFSSL_DTLS13
  4956. if (ssl->options.dtls) {
  4957. tls13minor = DTLSv1_3_MINOR;
  4958. tls12minor = DTLSv1_2_MINOR;
  4959. }
  4960. #endif /* WOLFSSL_DTLS13 */
  4961. if (msgType == client_hello) {
  4962. /* Must contain a length and at least one version. */
  4963. if (length < OPAQUE8_LEN + OPAQUE16_LEN || (length & 1) != 1)
  4964. return BUFFER_ERROR;
  4965. len = *input;
  4966. /* Protocol version array must fill rest of data. */
  4967. if (length != (word16)OPAQUE8_LEN + len)
  4968. return BUFFER_ERROR;
  4969. input++;
  4970. /* Find first match. */
  4971. for (i = 0; i < len; i += OPAQUE16_LEN) {
  4972. major = input[i];
  4973. minor = input[i + OPAQUE8_LEN];
  4974. #ifdef WOLFSSL_TLS13_DRAFT
  4975. if (major == TLS_DRAFT_MAJOR && minor == TLS_DRAFT_MINOR) {
  4976. major = SSLv3_MAJOR;
  4977. minor = TLSv1_3_MINOR;
  4978. }
  4979. #else
  4980. if (major == TLS_DRAFT_MAJOR)
  4981. continue;
  4982. #endif
  4983. if (major != pv.major)
  4984. continue;
  4985. /* No upgrade allowed. */
  4986. if (versionIsGreater(isDtls, minor, ssl->version.minor))
  4987. continue;
  4988. /* Check downgrade. */
  4989. if (versionIsLesser(isDtls, minor, ssl->version.minor)) {
  4990. if (!ssl->options.downgrade)
  4991. continue;
  4992. if (versionIsLesser(
  4993. isDtls, minor, ssl->options.minDowngrade))
  4994. continue;
  4995. if (newMinor == 0 &&
  4996. versionIsGreater(
  4997. isDtls, minor, ssl->options.oldMinor)) {
  4998. /* Downgrade the version. */
  4999. ssl->version.minor = minor;
  5000. }
  5001. }
  5002. if (versionIsAtLeast(isDtls, minor, tls13minor)) {
  5003. ssl->options.tls1_3 = 1;
  5004. /* TLS v1.3 requires supported version extension */
  5005. if (TLSX_Find(ssl->extensions, TLSX_SUPPORTED_VERSIONS) == NULL) {
  5006. ret = TLSX_Prepend(&ssl->extensions,
  5007. TLSX_SUPPORTED_VERSIONS, ssl, ssl->heap);
  5008. if (ret != 0) {
  5009. return ret;
  5010. }
  5011. TLSX_SetResponse(ssl, TLSX_SUPPORTED_VERSIONS);
  5012. }
  5013. if (versionIsGreater(isDtls, minor, newMinor)) {
  5014. ssl->version.minor = minor;
  5015. newMinor = minor;
  5016. }
  5017. }
  5018. else if (versionIsGreater(
  5019. isDtls, minor, ssl->options.oldMinor))
  5020. ssl->options.oldMinor = minor;
  5021. set = 1;
  5022. }
  5023. if (!set) {
  5024. #ifdef WOLFSSL_MYSQL_COMPATIBLE
  5025. SendAlert(ssl, alert_fatal, wc_protocol_version);
  5026. #else
  5027. SendAlert(ssl, alert_fatal, protocol_version);
  5028. #endif
  5029. WOLFSSL_ERROR_VERBOSE(VERSION_ERROR);
  5030. return VERSION_ERROR;
  5031. }
  5032. }
  5033. else if (msgType == server_hello || msgType == hello_retry_request) {
  5034. /* Must contain one version. */
  5035. if (length != OPAQUE16_LEN)
  5036. return BUFFER_ERROR;
  5037. major = input[0];
  5038. minor = input[OPAQUE8_LEN];
  5039. if (major != pv.major) {
  5040. WOLFSSL_ERROR_VERBOSE(VERSION_ERROR);
  5041. return VERSION_ERROR;
  5042. }
  5043. /* Can't downgrade with this extension below TLS v1.3. */
  5044. if (versionIsLesser(isDtls, minor, tls13minor)) {
  5045. WOLFSSL_ERROR_VERBOSE(VERSION_ERROR);
  5046. return VERSION_ERROR;
  5047. }
  5048. /* Version is TLS v1.2 to handle downgrading from TLS v1.3+. */
  5049. if (ssl->options.downgrade && ssl->version.minor == tls12minor) {
  5050. /* Set minor version back to TLS v1.3+ */
  5051. ssl->version.minor = ssl->ctx->method->version.minor;
  5052. }
  5053. /* No upgrade allowed. */
  5054. if (versionIsLesser(isDtls, ssl->version.minor, minor)) {
  5055. WOLFSSL_ERROR_VERBOSE(VERSION_ERROR);
  5056. return VERSION_ERROR;
  5057. }
  5058. /* Check downgrade. */
  5059. if (versionIsGreater(isDtls, ssl->version.minor, minor)) {
  5060. if (!ssl->options.downgrade) {
  5061. WOLFSSL_ERROR_VERBOSE(VERSION_ERROR);
  5062. return VERSION_ERROR;
  5063. }
  5064. if (versionIsLesser(
  5065. isDtls, minor, ssl->options.minDowngrade)) {
  5066. WOLFSSL_ERROR_VERBOSE(VERSION_ERROR);
  5067. return VERSION_ERROR;
  5068. }
  5069. /* Downgrade the version. */
  5070. ssl->version.minor = minor;
  5071. }
  5072. }
  5073. else {
  5074. WOLFSSL_ERROR_VERBOSE(SANITY_MSG_E);
  5075. return SANITY_MSG_E;
  5076. }
  5077. return 0;
  5078. }
  5079. /* Sets a new SupportedVersions extension into the extension list.
  5080. *
  5081. * extensions The list of extensions.
  5082. * data The extensions specific data.
  5083. * heap The heap used for allocation.
  5084. * returns 0 on success, otherwise failure.
  5085. */
  5086. static int TLSX_SetSupportedVersions(TLSX** extensions, const void* data,
  5087. void* heap)
  5088. {
  5089. if (extensions == NULL || data == NULL)
  5090. return BAD_FUNC_ARG;
  5091. return TLSX_Push(extensions, TLSX_SUPPORTED_VERSIONS, data, heap);
  5092. }
  5093. #define SV_GET_SIZE TLSX_SupportedVersions_GetSize
  5094. #define SV_WRITE TLSX_SupportedVersions_Write
  5095. #define SV_PARSE TLSX_SupportedVersions_Parse
  5096. #else
  5097. #define SV_GET_SIZE(a, b, c) 0
  5098. #define SV_WRITE(a, b, c, d) 0
  5099. #define SV_PARSE(a, b, c, d) 0
  5100. #endif /* WOLFSSL_TLS13 */
  5101. #if defined(WOLFSSL_TLS13) && defined(WOLFSSL_SEND_HRR_COOKIE)
  5102. /******************************************************************************/
  5103. /* Cookie */
  5104. /******************************************************************************/
  5105. /* Free the cookie data.
  5106. *
  5107. * cookie Cookie data.
  5108. * heap The heap used for allocation.
  5109. */
  5110. static void TLSX_Cookie_FreeAll(Cookie* cookie, void* heap)
  5111. {
  5112. (void)heap;
  5113. if (cookie != NULL)
  5114. XFREE(cookie, heap, DYNAMIC_TYPE_TLSX);
  5115. }
  5116. /* Get the size of the encoded Cookie extension.
  5117. * In messages: ClientHello and HelloRetryRequest.
  5118. *
  5119. * cookie The cookie to write.
  5120. * msgType The type of the message this extension is being written into.
  5121. * returns the number of bytes of the encoded Cookie extension.
  5122. */
  5123. static int TLSX_Cookie_GetSize(Cookie* cookie, byte msgType, word16* pSz)
  5124. {
  5125. if (msgType == client_hello || msgType == hello_retry_request) {
  5126. *pSz += OPAQUE16_LEN + cookie->len;
  5127. }
  5128. else {
  5129. WOLFSSL_ERROR_VERBOSE(SANITY_MSG_E);
  5130. return SANITY_MSG_E;
  5131. }
  5132. return 0;
  5133. }
  5134. /* Writes the Cookie extension into the output buffer.
  5135. * Assumes that the the output buffer is big enough to hold data.
  5136. * In messages: ClientHello and HelloRetryRequest.
  5137. *
  5138. * cookie The cookie to write.
  5139. * output The buffer to write into.
  5140. * msgType The type of the message this extension is being written into.
  5141. * returns the number of bytes written into the buffer.
  5142. */
  5143. static int TLSX_Cookie_Write(Cookie* cookie, byte* output, byte msgType,
  5144. word16* pSz)
  5145. {
  5146. if (msgType == client_hello || msgType == hello_retry_request) {
  5147. c16toa(cookie->len, output);
  5148. output += OPAQUE16_LEN;
  5149. XMEMCPY(output, &cookie->data, cookie->len);
  5150. *pSz += OPAQUE16_LEN + cookie->len;
  5151. }
  5152. else {
  5153. WOLFSSL_ERROR_VERBOSE(SANITY_MSG_E);
  5154. return SANITY_MSG_E;
  5155. }
  5156. return 0;
  5157. }
  5158. /* Parse the Cookie extension.
  5159. * In messages: ClientHello and HelloRetryRequest.
  5160. *
  5161. * ssl The SSL/TLS object.
  5162. * input The extension data.
  5163. * length The length of the extension data.
  5164. * msgType The type of the message this extension is being parsed from.
  5165. * returns 0 on success and other values indicate failure.
  5166. */
  5167. static int TLSX_Cookie_Parse(WOLFSSL* ssl, const byte* input, word16 length,
  5168. byte msgType)
  5169. {
  5170. word16 len;
  5171. word16 idx = 0;
  5172. TLSX* extension;
  5173. Cookie* cookie;
  5174. if (msgType != client_hello && msgType != hello_retry_request) {
  5175. WOLFSSL_ERROR_VERBOSE(SANITY_MSG_E);
  5176. return SANITY_MSG_E;
  5177. }
  5178. /* Message contains length and Cookie which must be at least one byte
  5179. * in length.
  5180. */
  5181. if (length < OPAQUE16_LEN + 1)
  5182. return BUFFER_E;
  5183. ato16(input + idx, &len);
  5184. idx += OPAQUE16_LEN;
  5185. if (length - idx != len)
  5186. return BUFFER_E;
  5187. if (msgType == hello_retry_request)
  5188. return TLSX_Cookie_Use(ssl, input + idx, len, NULL, 0, 0);
  5189. /* client_hello */
  5190. extension = TLSX_Find(ssl->extensions, TLSX_COOKIE);
  5191. if (extension == NULL) {
  5192. #ifdef WOLFSSL_DTLS13
  5193. if (ssl->options.dtls && IsAtLeastTLSv1_3(ssl->version))
  5194. /* Allow a cookie extension with DTLS 1.3 because it is possible
  5195. * that a different SSL instance sent the cookie but we are now
  5196. * receiving it. */
  5197. return TLSX_Cookie_Use(ssl, input + idx, len, NULL, 0, 0);
  5198. else
  5199. #endif
  5200. {
  5201. WOLFSSL_ERROR_VERBOSE(HRR_COOKIE_ERROR);
  5202. return HRR_COOKIE_ERROR;
  5203. }
  5204. }
  5205. cookie = (Cookie*)extension->data;
  5206. if (cookie->len != len || XMEMCMP(&cookie->data, input + idx, len) != 0) {
  5207. WOLFSSL_ERROR_VERBOSE(HRR_COOKIE_ERROR);
  5208. return HRR_COOKIE_ERROR;
  5209. }
  5210. /* Request seen. */
  5211. extension->resp = 0;
  5212. return 0;
  5213. }
  5214. /* Use the data to create a new Cookie object in the extensions.
  5215. *
  5216. * ssl SSL/TLS object.
  5217. * data Cookie data.
  5218. * len Length of cookie data in bytes.
  5219. * mac MAC data.
  5220. * macSz Length of MAC data in bytes.
  5221. * resp Indicates the extension will go into a response (HelloRetryRequest).
  5222. * returns 0 on success and other values indicate failure.
  5223. */
  5224. int TLSX_Cookie_Use(WOLFSSL* ssl, const byte* data, word16 len, byte* mac,
  5225. byte macSz, int resp)
  5226. {
  5227. int ret = 0;
  5228. TLSX* extension;
  5229. Cookie* cookie;
  5230. /* Find the cookie extension if it exists. */
  5231. extension = TLSX_Find(ssl->extensions, TLSX_COOKIE);
  5232. if (extension == NULL) {
  5233. /* Push new cookie extension. */
  5234. ret = TLSX_Push(&ssl->extensions, TLSX_COOKIE, NULL, ssl->heap);
  5235. if (ret != 0)
  5236. return ret;
  5237. extension = TLSX_Find(ssl->extensions, TLSX_COOKIE);
  5238. if (extension == NULL)
  5239. return MEMORY_E;
  5240. }
  5241. /* The Cookie structure has one byte for cookie data already. */
  5242. cookie = (Cookie*)XMALLOC(sizeof(Cookie) + len + macSz - 1, ssl->heap,
  5243. DYNAMIC_TYPE_TLSX);
  5244. if (cookie == NULL)
  5245. return MEMORY_E;
  5246. cookie->len = len + macSz;
  5247. XMEMCPY(&cookie->data, data, len);
  5248. if (mac != NULL)
  5249. XMEMCPY(&cookie->data + len, mac, macSz);
  5250. if (extension->data != NULL)
  5251. XFREE(extension->data, ssl->heap, DYNAMIC_TYPE_TLSX);
  5252. extension->data = (void*)cookie;
  5253. extension->resp = (byte)resp;
  5254. return 0;
  5255. }
  5256. #define CKE_FREE_ALL TLSX_Cookie_FreeAll
  5257. #define CKE_GET_SIZE TLSX_Cookie_GetSize
  5258. #define CKE_WRITE TLSX_Cookie_Write
  5259. #define CKE_PARSE TLSX_Cookie_Parse
  5260. #else
  5261. #define CKE_FREE_ALL(a, b) 0
  5262. #define CKE_GET_SIZE(a, b, c) 0
  5263. #define CKE_WRITE(a, b, c, d) 0
  5264. #define CKE_PARSE(a, b, c, d) 0
  5265. #endif
  5266. #if !defined(NO_CERTS) && !defined(WOLFSSL_NO_SIGALG)
  5267. /******************************************************************************/
  5268. /* Signature Algorithms */
  5269. /******************************************************************************/
  5270. /* Return the size of the SignatureAlgorithms extension's data.
  5271. *
  5272. * data Unused
  5273. * returns the length of data that will be in the extension.
  5274. */
  5275. static word16 TLSX_SignatureAlgorithms_GetSize(void* data)
  5276. {
  5277. WOLFSSL* ssl = (WOLFSSL*)data;
  5278. return OPAQUE16_LEN + ssl->suites->hashSigAlgoSz;
  5279. }
  5280. /* Creates a bit string of supported hash algorithms with RSA PSS.
  5281. * The bit string is used when determining which signature algorithm to use
  5282. * when creating the CertificateVerify message.
  5283. * Note: Valid data has an even length as each signature algorithm is two bytes.
  5284. *
  5285. * ssl The SSL/TLS object.
  5286. * input The buffer with the list of supported signature algorithms.
  5287. * length The length of the list in bytes.
  5288. * returns 0 on success, BUFFER_ERROR when the length is not even.
  5289. */
  5290. static int TLSX_SignatureAlgorithms_MapPss(WOLFSSL *ssl, const byte* input,
  5291. word16 length)
  5292. {
  5293. word16 i;
  5294. if ((length & 1) == 1)
  5295. return BUFFER_ERROR;
  5296. ssl->pssAlgo = 0;
  5297. for (i = 0; i < length; i += 2) {
  5298. if (input[i] == rsa_pss_sa_algo && input[i + 1] <= sha512_mac)
  5299. ssl->pssAlgo |= 1 << input[i + 1];
  5300. #ifdef WOLFSSL_TLS13
  5301. if (input[i] == rsa_pss_sa_algo && input[i + 1] >= pss_sha256 &&
  5302. input[i + 1] <= pss_sha512) {
  5303. ssl->pssAlgo |= 1 << input[i + 1];
  5304. }
  5305. #endif
  5306. }
  5307. return 0;
  5308. }
  5309. /* Writes the SignatureAlgorithms extension into the buffer.
  5310. *
  5311. * data Unused
  5312. * output The buffer to write the extension into.
  5313. * returns the length of data that was written.
  5314. */
  5315. static word16 TLSX_SignatureAlgorithms_Write(void* data, byte* output)
  5316. {
  5317. WOLFSSL* ssl = (WOLFSSL*)data;
  5318. c16toa(ssl->suites->hashSigAlgoSz, output);
  5319. XMEMCPY(output + OPAQUE16_LEN, ssl->suites->hashSigAlgo,
  5320. ssl->suites->hashSigAlgoSz);
  5321. TLSX_SignatureAlgorithms_MapPss(ssl, output + OPAQUE16_LEN,
  5322. ssl->suites->hashSigAlgoSz);
  5323. return OPAQUE16_LEN + ssl->suites->hashSigAlgoSz;
  5324. }
  5325. /* Parse the SignatureAlgorithms extension.
  5326. *
  5327. * ssl The SSL/TLS object.
  5328. * input The buffer with the extension data.
  5329. * length The length of the extension data.
  5330. * returns 0 on success, otherwise failure.
  5331. */
  5332. static int TLSX_SignatureAlgorithms_Parse(WOLFSSL *ssl, const byte* input,
  5333. word16 length, byte isRequest, Suites* suites)
  5334. {
  5335. word16 len;
  5336. if (!isRequest)
  5337. return BUFFER_ERROR;
  5338. /* Must contain a length and at least algorithm. */
  5339. if (length < OPAQUE16_LEN + OPAQUE16_LEN || (length & 1) != 0)
  5340. return BUFFER_ERROR;
  5341. ato16(input, &len);
  5342. input += OPAQUE16_LEN;
  5343. /* Algorithm array must fill rest of data. */
  5344. if (length != OPAQUE16_LEN + len)
  5345. return BUFFER_ERROR;
  5346. /* Sig Algo list size must be even. */
  5347. if (suites->hashSigAlgoSz % 2 != 0)
  5348. return BUFFER_ERROR;
  5349. /* truncate hashSigAlgo list if too long */
  5350. suites->hashSigAlgoSz = len;
  5351. if (suites->hashSigAlgoSz > WOLFSSL_MAX_SIGALGO) {
  5352. WOLFSSL_MSG("TLSX SigAlgo list exceeds max, truncating");
  5353. suites->hashSigAlgoSz = WOLFSSL_MAX_SIGALGO;
  5354. }
  5355. XMEMCPY(suites->hashSigAlgo, input, suites->hashSigAlgoSz);
  5356. return TLSX_SignatureAlgorithms_MapPss(ssl, input, len);
  5357. }
  5358. /* Sets a new SignatureAlgorithms extension into the extension list.
  5359. *
  5360. * extensions The list of extensions.
  5361. * data The extensions specific data.
  5362. * heap The heap used for allocation.
  5363. * returns 0 on success, otherwise failure.
  5364. */
  5365. static int TLSX_SetSignatureAlgorithms(TLSX** extensions, const void* data,
  5366. void* heap)
  5367. {
  5368. if (extensions == NULL)
  5369. return BAD_FUNC_ARG;
  5370. return TLSX_Push(extensions, TLSX_SIGNATURE_ALGORITHMS, data, heap);
  5371. }
  5372. #define SA_GET_SIZE TLSX_SignatureAlgorithms_GetSize
  5373. #define SA_WRITE TLSX_SignatureAlgorithms_Write
  5374. #define SA_PARSE TLSX_SignatureAlgorithms_Parse
  5375. #endif
  5376. /******************************************************************************/
  5377. /* Signature Algorithms Certificate */
  5378. /******************************************************************************/
  5379. #if defined(WOLFSSL_TLS13) && !defined(NO_CERTS) && !defined(WOLFSSL_NO_SIGALG)
  5380. /* Return the size of the SignatureAlgorithms extension's data.
  5381. *
  5382. * data Unused
  5383. * returns the length of data that will be in the extension.
  5384. */
  5385. static word16 TLSX_SignatureAlgorithmsCert_GetSize(void* data)
  5386. {
  5387. WOLFSSL* ssl = (WOLFSSL*)data;
  5388. return OPAQUE16_LEN + ssl->certHashSigAlgoSz;
  5389. }
  5390. /* Writes the SignatureAlgorithmsCert extension into the buffer.
  5391. *
  5392. * data Unused
  5393. * output The buffer to write the extension into.
  5394. * returns the length of data that was written.
  5395. */
  5396. static word16 TLSX_SignatureAlgorithmsCert_Write(void* data, byte* output)
  5397. {
  5398. WOLFSSL* ssl = (WOLFSSL*)data;
  5399. c16toa(ssl->certHashSigAlgoSz, output);
  5400. XMEMCPY(output + OPAQUE16_LEN, ssl->certHashSigAlgo,
  5401. ssl->certHashSigAlgoSz);
  5402. return OPAQUE16_LEN + ssl->certHashSigAlgoSz;
  5403. }
  5404. /* Parse the SignatureAlgorithmsCert extension.
  5405. *
  5406. * ssl The SSL/TLS object.
  5407. * input The buffer with the extension data.
  5408. * length The length of the extension data.
  5409. * returns 0 on success, otherwise failure.
  5410. */
  5411. static int TLSX_SignatureAlgorithmsCert_Parse(WOLFSSL *ssl, const byte* input,
  5412. word16 length, byte isRequest)
  5413. {
  5414. word16 len;
  5415. if (!isRequest)
  5416. return BUFFER_ERROR;
  5417. /* Must contain a length and at least algorithm. */
  5418. if (length < OPAQUE16_LEN + OPAQUE16_LEN || (length & 1) != 0)
  5419. return BUFFER_ERROR;
  5420. ato16(input, &len);
  5421. input += OPAQUE16_LEN;
  5422. /* Algorithm array must fill rest of data. */
  5423. if (length != OPAQUE16_LEN + len)
  5424. return BUFFER_ERROR;
  5425. /* truncate hashSigAlgo list if too long */
  5426. ssl->certHashSigAlgoSz = len;
  5427. if (ssl->certHashSigAlgoSz > WOLFSSL_MAX_SIGALGO) {
  5428. WOLFSSL_MSG("TLSX SigAlgo list exceeds max, truncating");
  5429. ssl->certHashSigAlgoSz = WOLFSSL_MAX_SIGALGO;
  5430. }
  5431. XMEMCPY(ssl->certHashSigAlgo, input, ssl->certHashSigAlgoSz);
  5432. return 0;
  5433. }
  5434. /* Sets a new SignatureAlgorithmsCert extension into the extension list.
  5435. *
  5436. * extensions The list of extensions.
  5437. * data The extensions specific data.
  5438. * heap The heap used for allocation.
  5439. * returns 0 on success, otherwise failure.
  5440. */
  5441. static int TLSX_SetSignatureAlgorithmsCert(TLSX** extensions, const void* data,
  5442. void* heap)
  5443. {
  5444. if (extensions == NULL)
  5445. return BAD_FUNC_ARG;
  5446. return TLSX_Push(extensions, TLSX_SIGNATURE_ALGORITHMS_CERT, data, heap);
  5447. }
  5448. #define SAC_GET_SIZE TLSX_SignatureAlgorithmsCert_GetSize
  5449. #define SAC_WRITE TLSX_SignatureAlgorithmsCert_Write
  5450. #define SAC_PARSE TLSX_SignatureAlgorithmsCert_Parse
  5451. #endif /* WOLFSSL_TLS13 */
  5452. /******************************************************************************/
  5453. /* Key Share */
  5454. /******************************************************************************/
  5455. #if defined(WOLFSSL_TLS13) && defined(HAVE_SUPPORTED_CURVES)
  5456. /* Create a key share entry using named Diffie-Hellman parameters group.
  5457. * Generates a key pair.
  5458. *
  5459. * ssl The SSL/TLS object.
  5460. * kse The key share entry object.
  5461. * returns 0 on success, otherwise failure.
  5462. */
  5463. static int TLSX_KeyShare_GenDhKey(WOLFSSL *ssl, KeyShareEntry* kse)
  5464. {
  5465. int ret = 0;
  5466. #if !defined(NO_DH) && (!defined(NO_CERTS) || !defined(NO_PSK))
  5467. word32 pSz = 0, pvtSz = 0;
  5468. DhKey* dhKey = (DhKey*)kse->key;
  5469. /* Pick the parameters from the named group. */
  5470. #ifdef HAVE_PUBLIC_FFDHE
  5471. const DhParams* params = NULL;
  5472. switch (kse->group) {
  5473. #ifdef HAVE_FFDHE_2048
  5474. case WOLFSSL_FFDHE_2048:
  5475. params = wc_Dh_ffdhe2048_Get();
  5476. kse->keyLen = 29;
  5477. break;
  5478. #endif
  5479. #ifdef HAVE_FFDHE_3072
  5480. case WOLFSSL_FFDHE_3072:
  5481. params = wc_Dh_ffdhe3072_Get();
  5482. kse->keyLen = 34;
  5483. break;
  5484. #endif
  5485. #ifdef HAVE_FFDHE_4096
  5486. case WOLFSSL_FFDHE_4096:
  5487. params = wc_Dh_ffdhe4096_Get();
  5488. kse->keyLen = 39;
  5489. break;
  5490. #endif
  5491. #ifdef HAVE_FFDHE_6144
  5492. case WOLFSSL_FFDHE_6144:
  5493. params = wc_Dh_ffdhe6144_Get();
  5494. kse->keyLen = 46;
  5495. break;
  5496. #endif
  5497. #ifdef HAVE_FFDHE_8192
  5498. case WOLFSSL_FFDHE_8192:
  5499. params = wc_Dh_ffdhe8192_Get();
  5500. kse->keyLen = 52;
  5501. break;
  5502. #endif
  5503. default:
  5504. break;
  5505. }
  5506. if (params == NULL)
  5507. return BAD_FUNC_ARG;
  5508. pSz = params->p_len;
  5509. pvtSz = kse->keyLen;
  5510. #else
  5511. kse->keyLen = wc_DhGetNamedKeyMinSize(kse->group);
  5512. if (kse->keyLen == 0) {
  5513. return BAD_FUNC_ARG;
  5514. }
  5515. ret = wc_DhGetNamedKeyParamSize(kse->group, &pSz, NULL, NULL);
  5516. if (ret != 0) {
  5517. return BAD_FUNC_ARG;
  5518. }
  5519. pvtSz = kse->keyLen;
  5520. #endif
  5521. kse->pubKeyLen = pSz;
  5522. /* Trigger Key Generation */
  5523. if (kse->pubKey == NULL || kse->privKey == NULL) {
  5524. if (kse->key == NULL) {
  5525. kse->key = (DhKey*)XMALLOC(sizeof(DhKey), ssl->heap,
  5526. DYNAMIC_TYPE_DH);
  5527. if (kse->key == NULL)
  5528. return MEMORY_E;
  5529. /* Setup Key */
  5530. ret = wc_InitDhKey_ex((DhKey*)kse->key, ssl->heap, ssl->devId);
  5531. if (ret == 0) {
  5532. dhKey = (DhKey*)kse->key;
  5533. #ifdef HAVE_PUBLIC_FFDHE
  5534. ret = wc_DhSetKey(dhKey, params->p, params->p_len, params->g,
  5535. params->g_len);
  5536. #else
  5537. ret = wc_DhSetNamedKey(dhKey, kse->group);
  5538. #endif
  5539. }
  5540. }
  5541. /* Allocate space for the private and public key */
  5542. if (ret == 0 && kse->pubKey == NULL) {
  5543. kse->pubKey = (byte*)XMALLOC(kse->pubKeyLen, ssl->heap,
  5544. DYNAMIC_TYPE_PUBLIC_KEY);
  5545. if (kse->pubKey == NULL)
  5546. ret = MEMORY_E;
  5547. }
  5548. if (ret == 0 && kse->privKey == NULL) {
  5549. kse->privKey = (byte*)XMALLOC(kse->keyLen, ssl->heap,
  5550. DYNAMIC_TYPE_PRIVATE_KEY);
  5551. if (kse->privKey == NULL)
  5552. ret = MEMORY_E;
  5553. }
  5554. if (ret == 0) {
  5555. #if defined(WOLFSSL_STATIC_EPHEMERAL) && defined(WOLFSSL_DH_EXTRA)
  5556. ret = wolfSSL_StaticEphemeralKeyLoad(ssl, WC_PK_TYPE_DH, kse->key);
  5557. if (ret == 0) {
  5558. ret = wc_DhExportKeyPair(dhKey,
  5559. (byte*)kse->privKey, &kse->keyLen, /* private */
  5560. kse->pubKey, &kse->pubKeyLen /* public */
  5561. );
  5562. }
  5563. else
  5564. #endif
  5565. {
  5566. /* Generate a new key pair */
  5567. /* For async this is called once and when event is done, the
  5568. * provided buffers will be populated.
  5569. * Final processing is zero pad below. */
  5570. ret = DhGenKeyPair(ssl, dhKey,
  5571. (byte*)kse->privKey, &kse->keyLen, /* private */
  5572. kse->pubKey, &kse->pubKeyLen /* public */
  5573. );
  5574. #ifdef WOLFSSL_ASYNC_CRYPT
  5575. if (ret == WC_PENDING_E) {
  5576. return ret;
  5577. }
  5578. #endif
  5579. }
  5580. }
  5581. }
  5582. if (ret == 0) {
  5583. if (pSz != kse->pubKeyLen) {
  5584. /* Zero pad the front of the public key to match prime "p" size */
  5585. XMEMMOVE(kse->pubKey + pSz - kse->pubKeyLen, kse->pubKey,
  5586. kse->pubKeyLen);
  5587. XMEMSET(kse->pubKey, 0, pSz - kse->pubKeyLen);
  5588. kse->pubKeyLen = pSz;
  5589. }
  5590. if (pvtSz != kse->keyLen) {
  5591. /* Zero pad the front of the private key */
  5592. XMEMMOVE(kse->privKey + pvtSz - kse->keyLen, kse->privKey,
  5593. kse->keyLen);
  5594. XMEMSET(kse->privKey, 0, pvtSz - kse->keyLen);
  5595. kse->keyLen = pvtSz;
  5596. }
  5597. #ifdef WOLFSSL_DEBUG_TLS
  5598. WOLFSSL_MSG("Public DH Key");
  5599. WOLFSSL_BUFFER(kse->pubKey, kse->pubKeyLen);
  5600. #endif
  5601. }
  5602. /* Always release the DH key to free up memory.
  5603. * The DhKey will be setup again in TLSX_KeyShare_ProcessDh */
  5604. if (dhKey != NULL)
  5605. wc_FreeDhKey(dhKey);
  5606. if (kse->key != NULL) {
  5607. XFREE(kse->key, ssl->heap, DYNAMIC_TYPE_DH);
  5608. kse->key = NULL;
  5609. }
  5610. if (ret != 0) {
  5611. /* Cleanup on error, otherwise data owned by key share entry */
  5612. if (kse->privKey != NULL) {
  5613. XFREE(kse->privKey, ssl->heap, DYNAMIC_TYPE_PRIVATE_KEY);
  5614. kse->privKey = NULL;
  5615. }
  5616. if (kse->pubKey != NULL) {
  5617. XFREE(kse->pubKey, ssl->heap, DYNAMIC_TYPE_PUBLIC_KEY);
  5618. kse->pubKey = NULL;
  5619. }
  5620. }
  5621. #else
  5622. (void)ssl;
  5623. (void)kse;
  5624. ret = NOT_COMPILED_IN;
  5625. WOLFSSL_ERROR_VERBOSE(ret);
  5626. #endif
  5627. return ret;
  5628. }
  5629. /* Create a key share entry using X25519 parameters group.
  5630. * Generates a key pair.
  5631. *
  5632. * ssl The SSL/TLS object.
  5633. * kse The key share entry object.
  5634. * returns 0 on success, otherwise failure.
  5635. */
  5636. static int TLSX_KeyShare_GenX25519Key(WOLFSSL *ssl, KeyShareEntry* kse)
  5637. {
  5638. int ret = 0;
  5639. #ifdef HAVE_CURVE25519
  5640. curve25519_key* key = (curve25519_key*)kse->key;
  5641. if (kse->key == NULL) {
  5642. /* Allocate a Curve25519 key to hold private key. */
  5643. kse->key = (curve25519_key*)XMALLOC(sizeof(curve25519_key), ssl->heap,
  5644. DYNAMIC_TYPE_PRIVATE_KEY);
  5645. if (kse->key == NULL) {
  5646. WOLFSSL_MSG("GenX25519Key memory error");
  5647. return MEMORY_E;
  5648. }
  5649. /* Make an Curve25519 key. */
  5650. ret = wc_curve25519_init_ex((curve25519_key*)kse->key, ssl->heap,
  5651. INVALID_DEVID);
  5652. if (ret == 0) {
  5653. /* setting "key" means okay to call wc_curve25519_free */
  5654. key = (curve25519_key*)kse->key;
  5655. #ifdef WOLFSSL_STATIC_EPHEMERAL
  5656. ret = wolfSSL_StaticEphemeralKeyLoad(ssl, WC_PK_TYPE_CURVE25519, kse->key);
  5657. if (ret != 0)
  5658. #endif
  5659. {
  5660. ret = wc_curve25519_make_key(ssl->rng, CURVE25519_KEYSIZE, key);
  5661. }
  5662. }
  5663. }
  5664. if (ret == 0 && kse->pubKey == NULL) {
  5665. /* Allocate space for the public key. */
  5666. kse->pubKey = (byte*)XMALLOC(CURVE25519_KEYSIZE, ssl->heap,
  5667. DYNAMIC_TYPE_PUBLIC_KEY);
  5668. if (kse->pubKey == NULL) {
  5669. WOLFSSL_MSG("GenX25519Key pub memory error");
  5670. ret = MEMORY_E;
  5671. }
  5672. }
  5673. if (ret == 0) {
  5674. /* Export Curve25519 public key. */
  5675. kse->pubKeyLen = CURVE25519_KEYSIZE;
  5676. if (wc_curve25519_export_public_ex(key, kse->pubKey, &kse->pubKeyLen,
  5677. EC25519_LITTLE_ENDIAN) != 0) {
  5678. ret = ECC_EXPORT_ERROR;
  5679. WOLFSSL_ERROR_VERBOSE(ret);
  5680. }
  5681. kse->pubKeyLen = CURVE25519_KEYSIZE; /* always CURVE25519_KEYSIZE */
  5682. }
  5683. #ifdef WOLFSSL_DEBUG_TLS
  5684. if (ret == 0) {
  5685. WOLFSSL_MSG("Public Curve25519 Key");
  5686. WOLFSSL_BUFFER(kse->pubKey, kse->pubKeyLen);
  5687. }
  5688. #endif
  5689. if (ret != 0) {
  5690. /* Data owned by key share entry otherwise. */
  5691. if (kse->pubKey != NULL) {
  5692. XFREE(kse->pubKey, ssl->heap, DYNAMIC_TYPE_PUBLIC_KEY);
  5693. kse->pubKey = NULL;
  5694. }
  5695. if (key != NULL)
  5696. wc_curve25519_free(key);
  5697. if (kse->key != NULL) {
  5698. XFREE(kse->key, ssl->heap, DYNAMIC_TYPE_PRIVATE_KEY);
  5699. kse->key = NULL;
  5700. }
  5701. }
  5702. #else
  5703. (void)ssl;
  5704. (void)kse;
  5705. ret = NOT_COMPILED_IN;
  5706. WOLFSSL_ERROR_VERBOSE(ret);
  5707. #endif /* HAVE_CURVE25519 */
  5708. return ret;
  5709. }
  5710. /* Create a key share entry using X448 parameters group.
  5711. * Generates a key pair.
  5712. *
  5713. * ssl The SSL/TLS object.
  5714. * kse The key share entry object.
  5715. * returns 0 on success, otherwise failure.
  5716. */
  5717. static int TLSX_KeyShare_GenX448Key(WOLFSSL *ssl, KeyShareEntry* kse)
  5718. {
  5719. int ret = 0;
  5720. #ifdef HAVE_CURVE448
  5721. curve448_key* key = (curve448_key*)kse->key;
  5722. if (kse->key == NULL) {
  5723. /* Allocate a Curve448 key to hold private key. */
  5724. kse->key = (curve448_key*)XMALLOC(sizeof(curve448_key), ssl->heap,
  5725. DYNAMIC_TYPE_PRIVATE_KEY);
  5726. if (kse->key == NULL) {
  5727. WOLFSSL_MSG("GenX448Key memory error");
  5728. return MEMORY_E;
  5729. }
  5730. /* Make an Curve448 key. */
  5731. ret = wc_curve448_init((curve448_key*)kse->key);
  5732. if (ret == 0) {
  5733. key = (curve448_key*)kse->key;
  5734. #ifdef WOLFSSL_STATIC_EPHEMERAL
  5735. ret = wolfSSL_StaticEphemeralKeyLoad(ssl, WC_PK_TYPE_CURVE448, kse->key);
  5736. if (ret != 0)
  5737. #endif
  5738. {
  5739. ret = wc_curve448_make_key(ssl->rng, CURVE448_KEY_SIZE, key);
  5740. }
  5741. }
  5742. }
  5743. if (ret == 0 && kse->pubKey == NULL) {
  5744. /* Allocate space for the public key. */
  5745. kse->pubKey = (byte*)XMALLOC(CURVE448_KEY_SIZE, ssl->heap,
  5746. DYNAMIC_TYPE_PUBLIC_KEY);
  5747. if (kse->pubKey == NULL) {
  5748. WOLFSSL_MSG("GenX448Key pub memory error");
  5749. ret = MEMORY_E;
  5750. }
  5751. }
  5752. if (ret == 0) {
  5753. /* Export Curve448 public key. */
  5754. kse->pubKeyLen = CURVE448_KEY_SIZE;
  5755. if (wc_curve448_export_public_ex(key, kse->pubKey, &kse->pubKeyLen,
  5756. EC448_LITTLE_ENDIAN) != 0) {
  5757. ret = ECC_EXPORT_ERROR;
  5758. }
  5759. kse->pubKeyLen = CURVE448_KEY_SIZE; /* always CURVE448_KEY_SIZE */
  5760. }
  5761. #ifdef WOLFSSL_DEBUG_TLS
  5762. if (ret == 0) {
  5763. WOLFSSL_MSG("Public Curve448 Key");
  5764. WOLFSSL_BUFFER(kse->pubKey, kse->pubKeyLen);
  5765. }
  5766. #endif
  5767. if (ret != 0) {
  5768. /* Data owned by key share entry otherwise. */
  5769. if (kse->pubKey != NULL) {
  5770. XFREE(kse->pubKey, ssl->heap, DYNAMIC_TYPE_PUBLIC_KEY);
  5771. kse->pubKey = NULL;
  5772. }
  5773. if (key != NULL)
  5774. wc_curve448_free(key);
  5775. if (kse->key != NULL) {
  5776. XFREE(kse->key, ssl->heap, DYNAMIC_TYPE_PRIVATE_KEY);
  5777. kse->key = NULL;
  5778. }
  5779. }
  5780. #else
  5781. (void)ssl;
  5782. (void)kse;
  5783. ret = NOT_COMPILED_IN;
  5784. WOLFSSL_ERROR_VERBOSE(ret);
  5785. #endif /* HAVE_CURVE448 */
  5786. return ret;
  5787. }
  5788. /* Create a key share entry using named elliptic curve parameters group.
  5789. * Generates a key pair.
  5790. *
  5791. * ssl The SSL/TLS object.
  5792. * kse The key share entry object.
  5793. * returns 0 on success, otherwise failure.
  5794. */
  5795. static int TLSX_KeyShare_GenEccKey(WOLFSSL *ssl, KeyShareEntry* kse)
  5796. {
  5797. int ret = 0;
  5798. #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_EXPORT)
  5799. word32 keySize = 0;
  5800. word16 curveId = (word16) ECC_CURVE_INVALID;
  5801. ecc_key* eccKey = (ecc_key*)kse->key;
  5802. /* TODO: [TLS13] The key sizes should come from wolfcrypt. */
  5803. /* Translate named group to a curve id. */
  5804. switch (kse->group) {
  5805. #if (!defined(NO_ECC256) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 256
  5806. #ifndef NO_ECC_SECP
  5807. case WOLFSSL_ECC_SECP256R1:
  5808. curveId = ECC_SECP256R1;
  5809. keySize = 32;
  5810. break;
  5811. #endif /* !NO_ECC_SECP */
  5812. #endif
  5813. #if (defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 384
  5814. #ifndef NO_ECC_SECP
  5815. case WOLFSSL_ECC_SECP384R1:
  5816. curveId = ECC_SECP384R1;
  5817. keySize = 48;
  5818. break;
  5819. #endif /* !NO_ECC_SECP */
  5820. #endif
  5821. #if (defined(HAVE_ECC521) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 521
  5822. #ifndef NO_ECC_SECP
  5823. case WOLFSSL_ECC_SECP521R1:
  5824. curveId = ECC_SECP521R1;
  5825. keySize = 66;
  5826. break;
  5827. #endif /* !NO_ECC_SECP */
  5828. #endif
  5829. default:
  5830. WOLFSSL_ERROR_VERBOSE(BAD_FUNC_ARG);
  5831. return BAD_FUNC_ARG;
  5832. }
  5833. if (kse->key == NULL) {
  5834. kse->keyLen = keySize;
  5835. kse->pubKeyLen = keySize * 2 + 1;
  5836. #if defined(WOLFSSL_RENESAS_TSIP_TLS) && (WOLFSSL_RENESAS_TSIP_VER >= 115)
  5837. ret = tsip_Tls13GenEccKeyPair(ssl, kse);
  5838. if (ret != CRYPTOCB_UNAVAILABLE) {
  5839. return ret;
  5840. }
  5841. #endif
  5842. /* Allocate an ECC key to hold private key. */
  5843. kse->key = (byte*)XMALLOC(sizeof(ecc_key), ssl->heap, DYNAMIC_TYPE_ECC);
  5844. if (kse->key == NULL) {
  5845. WOLFSSL_MSG("EccTempKey Memory error");
  5846. return MEMORY_E;
  5847. }
  5848. /* Make an ECC key */
  5849. ret = wc_ecc_init_ex((ecc_key*)kse->key, ssl->heap, ssl->devId);
  5850. if (ret == 0) {
  5851. /* setting eccKey means okay to call wc_ecc_free */
  5852. eccKey = (ecc_key*)kse->key;
  5853. #ifdef WOLFSSL_STATIC_EPHEMERAL
  5854. ret = wolfSSL_StaticEphemeralKeyLoad(ssl, WC_PK_TYPE_ECDH, kse->key);
  5855. if (ret != 0)
  5856. #endif
  5857. {
  5858. /* set curve info for EccMakeKey "peer" info */
  5859. ret = wc_ecc_set_curve(eccKey, kse->keyLen, curveId);
  5860. if (ret == 0) {
  5861. /* Generate ephemeral ECC key */
  5862. /* For async this is called once and when event is done, the
  5863. * provided buffers in key be populated.
  5864. * Final processing is x963 key export below. */
  5865. ret = EccMakeKey(ssl, eccKey, eccKey);
  5866. }
  5867. #ifdef WOLFSSL_ASYNC_CRYPT
  5868. if (ret == WC_PENDING_E)
  5869. return ret;
  5870. #endif
  5871. }
  5872. }
  5873. }
  5874. if (ret == 0 && kse->pubKey == NULL) {
  5875. /* Allocate space for the public key */
  5876. kse->pubKey = (byte*)XMALLOC(kse->pubKeyLen, ssl->heap,
  5877. DYNAMIC_TYPE_PUBLIC_KEY);
  5878. if (kse->pubKey == NULL) {
  5879. WOLFSSL_MSG("Key data Memory error");
  5880. ret = MEMORY_E;
  5881. }
  5882. }
  5883. if (ret == 0) {
  5884. XMEMSET(kse->pubKey, 0, kse->pubKeyLen);
  5885. /* Export public key. */
  5886. PRIVATE_KEY_UNLOCK();
  5887. if (wc_ecc_export_x963(eccKey, kse->pubKey, &kse->pubKeyLen) != 0) {
  5888. ret = ECC_EXPORT_ERROR;
  5889. WOLFSSL_ERROR_VERBOSE(ret);
  5890. }
  5891. PRIVATE_KEY_LOCK();
  5892. }
  5893. #ifdef WOLFSSL_DEBUG_TLS
  5894. if (ret == 0) {
  5895. WOLFSSL_MSG("Public ECC Key");
  5896. WOLFSSL_BUFFER(kse->pubKey, kse->pubKeyLen);
  5897. }
  5898. #endif
  5899. if (ret != 0) {
  5900. /* Cleanup on error, otherwise data owned by key share entry */
  5901. if (kse->pubKey != NULL) {
  5902. XFREE(kse->pubKey, ssl->heap, DYNAMIC_TYPE_PUBLIC_KEY);
  5903. kse->pubKey = NULL;
  5904. }
  5905. if (eccKey != NULL)
  5906. wc_ecc_free(eccKey);
  5907. if (kse->key != NULL) {
  5908. XFREE(kse->key, ssl->heap, DYNAMIC_TYPE_PRIVATE_KEY);
  5909. kse->key = NULL;
  5910. }
  5911. }
  5912. #else
  5913. (void)ssl;
  5914. (void)kse;
  5915. ret = NOT_COMPILED_IN;
  5916. WOLFSSL_ERROR_VERBOSE(ret);
  5917. #endif /* HAVE_ECC && HAVE_ECC_KEY_EXPORT */
  5918. return ret;
  5919. }
  5920. #ifdef HAVE_PQC
  5921. #ifdef WOLFSSL_WC_KYBER
  5922. static int kyber_id2type(int id, int *type)
  5923. {
  5924. int ret = 0;
  5925. switch (id) {
  5926. #ifdef WOLFSSL_KYBER512
  5927. case WOLFSSL_KYBER_LEVEL1:
  5928. *type = KYBER512;
  5929. break;
  5930. #endif
  5931. #ifdef WOLFSSL_KYBER768
  5932. case WOLFSSL_KYBER_LEVEL3:
  5933. *type = KYBER768;
  5934. break;
  5935. #endif
  5936. #ifdef WOLFSSL_KYBER1024
  5937. case WOLFSSL_KYBER_LEVEL5:
  5938. *type = KYBER1024;
  5939. break;
  5940. #endif
  5941. default:
  5942. ret = NOT_COMPILED_IN;
  5943. break;
  5944. }
  5945. return ret;
  5946. }
  5947. #elif defined(HAVE_LIBOQS)
  5948. /* Transform a group ID into an OQS Algorithm name as a string. */
  5949. static const char* OQS_ID2name(int id)
  5950. {
  5951. switch (id) {
  5952. case WOLFSSL_KYBER_LEVEL1: return OQS_KEM_alg_kyber_512;
  5953. case WOLFSSL_KYBER_LEVEL3: return OQS_KEM_alg_kyber_768;
  5954. case WOLFSSL_KYBER_LEVEL5: return OQS_KEM_alg_kyber_1024;
  5955. case WOLFSSL_NTRU_HPS_LEVEL1: return OQS_KEM_alg_ntru_hps2048509;
  5956. case WOLFSSL_NTRU_HPS_LEVEL3: return OQS_KEM_alg_ntru_hps2048677;
  5957. case WOLFSSL_NTRU_HPS_LEVEL5: return OQS_KEM_alg_ntru_hps4096821;
  5958. case WOLFSSL_NTRU_HRSS_LEVEL3: return OQS_KEM_alg_ntru_hrss701;
  5959. case WOLFSSL_SABER_LEVEL1: return OQS_KEM_alg_saber_lightsaber;
  5960. case WOLFSSL_SABER_LEVEL3: return OQS_KEM_alg_saber_saber;
  5961. case WOLFSSL_SABER_LEVEL5: return OQS_KEM_alg_saber_firesaber;
  5962. case WOLFSSL_KYBER_90S_LEVEL1: return OQS_KEM_alg_kyber_512_90s;
  5963. case WOLFSSL_KYBER_90S_LEVEL3: return OQS_KEM_alg_kyber_768_90s;
  5964. case WOLFSSL_KYBER_90S_LEVEL5: return OQS_KEM_alg_kyber_1024_90s;
  5965. default: break;
  5966. }
  5967. return NULL;
  5968. }
  5969. #endif /* HAVE_LIBOQS */
  5970. typedef struct PqcHybridMapping {
  5971. int hybrid;
  5972. int ecc;
  5973. int pqc;
  5974. } PqcHybridMapping;
  5975. static const PqcHybridMapping pqc_hybrid_mapping[] = {
  5976. {.hybrid = WOLFSSL_P256_NTRU_HPS_LEVEL1, .ecc = WOLFSSL_ECC_SECP256R1,
  5977. .pqc = WOLFSSL_NTRU_HPS_LEVEL1},
  5978. {.hybrid = WOLFSSL_P384_NTRU_HPS_LEVEL3, .ecc = WOLFSSL_ECC_SECP384R1,
  5979. .pqc = WOLFSSL_NTRU_HPS_LEVEL3},
  5980. {.hybrid = WOLFSSL_P521_NTRU_HPS_LEVEL5, .ecc = WOLFSSL_ECC_SECP521R1,
  5981. .pqc = WOLFSSL_NTRU_HPS_LEVEL5},
  5982. {.hybrid = WOLFSSL_P384_NTRU_HRSS_LEVEL3, .ecc = WOLFSSL_ECC_SECP384R1,
  5983. .pqc = WOLFSSL_NTRU_HRSS_LEVEL3},
  5984. {.hybrid = WOLFSSL_P256_SABER_LEVEL1, .ecc = WOLFSSL_ECC_SECP256R1,
  5985. .pqc = WOLFSSL_SABER_LEVEL1},
  5986. {.hybrid = WOLFSSL_P384_SABER_LEVEL3, .ecc = WOLFSSL_ECC_SECP384R1,
  5987. .pqc = WOLFSSL_SABER_LEVEL3},
  5988. {.hybrid = WOLFSSL_P521_SABER_LEVEL5, .ecc = WOLFSSL_ECC_SECP521R1,
  5989. .pqc = WOLFSSL_SABER_LEVEL5},
  5990. {.hybrid = WOLFSSL_P256_KYBER_LEVEL1, .ecc = WOLFSSL_ECC_SECP256R1,
  5991. .pqc = WOLFSSL_KYBER_LEVEL1},
  5992. {.hybrid = WOLFSSL_P384_KYBER_LEVEL3, .ecc = WOLFSSL_ECC_SECP384R1,
  5993. .pqc = WOLFSSL_KYBER_LEVEL3},
  5994. {.hybrid = WOLFSSL_P521_KYBER_LEVEL5, .ecc = WOLFSSL_ECC_SECP521R1,
  5995. .pqc = WOLFSSL_KYBER_LEVEL5},
  5996. {.hybrid = WOLFSSL_P256_KYBER_90S_LEVEL1, .ecc = WOLFSSL_ECC_SECP256R1,
  5997. .pqc = WOLFSSL_KYBER_90S_LEVEL1},
  5998. {.hybrid = WOLFSSL_P384_KYBER_90S_LEVEL3, .ecc = WOLFSSL_ECC_SECP384R1,
  5999. .pqc = WOLFSSL_KYBER_90S_LEVEL3},
  6000. {.hybrid = WOLFSSL_P521_KYBER_90S_LEVEL5, .ecc = WOLFSSL_ECC_SECP521R1,
  6001. .pqc = WOLFSSL_KYBER_90S_LEVEL5},
  6002. {.hybrid = 0, .ecc = 0, .pqc = 0}
  6003. };
  6004. /* This will map an ecc-pqs hybrid group into its ecc group and pqc kem group.
  6005. * If it cannot find a mapping then *pqc is set to group. ecc is optional. */
  6006. static void findEccPqc(int *ecc, int *pqc, int group)
  6007. {
  6008. int i;
  6009. if (pqc == NULL) {
  6010. return;
  6011. }
  6012. *pqc = 0;
  6013. if (ecc != NULL) {
  6014. *ecc = 0;
  6015. }
  6016. for (i = 0; pqc_hybrid_mapping[i].hybrid != 0; i++) {
  6017. if (pqc_hybrid_mapping[i].hybrid == group) {
  6018. *pqc = pqc_hybrid_mapping[i].pqc;
  6019. if (ecc != NULL) {
  6020. *ecc = pqc_hybrid_mapping[i].ecc;
  6021. }
  6022. break;
  6023. }
  6024. }
  6025. if (*pqc == 0) {
  6026. /* It is not a hybrid, so maybe its simple. */
  6027. *pqc = group;
  6028. }
  6029. }
  6030. /* Create a key share entry using liboqs parameters group.
  6031. * Generates a key pair.
  6032. *
  6033. * ssl The SSL/TLS object.
  6034. * kse The key share entry object.
  6035. * returns 0 on success, otherwise failure.
  6036. */
  6037. #ifdef WOLFSSL_WC_KYBER
  6038. static int TLSX_KeyShare_GenPqcKey(WOLFSSL *ssl, KeyShareEntry* kse)
  6039. {
  6040. int ret = 0;
  6041. int type = 0;
  6042. KyberKey kem[1];
  6043. byte* pubKey = NULL;
  6044. byte* privKey = NULL;
  6045. KeyShareEntry *ecc_kse = NULL;
  6046. int oqs_group = 0;
  6047. int ecc_group = 0;
  6048. word32 privSz = 0;
  6049. word32 pubSz = 0;
  6050. findEccPqc(&ecc_group, &oqs_group, kse->group);
  6051. ret = kyber_id2type(oqs_group, &type);
  6052. if (ret == NOT_COMPILED_IN) {
  6053. WOLFSSL_MSG("Invalid Kyber algorithm specified.");
  6054. ret = BAD_FUNC_ARG;
  6055. }
  6056. if (ret == 0) {
  6057. ret = wc_KyberKey_Init(type, kem, ssl->heap, ssl->devId);
  6058. if (ret != 0) {
  6059. WOLFSSL_MSG("Failed to intialize Kyber Key.");
  6060. }
  6061. }
  6062. if (ret == 0) {
  6063. ecc_kse = (KeyShareEntry*)XMALLOC(sizeof(*ecc_kse), ssl->heap,
  6064. DYNAMIC_TYPE_TLSX);
  6065. if (ecc_kse == NULL) {
  6066. WOLFSSL_MSG("ecc_kse memory allocation failure");
  6067. ret = MEMORY_ERROR;
  6068. }
  6069. }
  6070. if (ret == 0) {
  6071. XMEMSET(ecc_kse, 0, sizeof(*ecc_kse));
  6072. ret = wc_KyberKey_PrivateKeySize(kem, &privSz);
  6073. }
  6074. if (ret == 0) {
  6075. ret = wc_KyberKey_PublicKeySize(kem, &pubSz);
  6076. }
  6077. if (ret == 0 && ecc_group != 0) {
  6078. ecc_kse->group = ecc_group;
  6079. ret = TLSX_KeyShare_GenEccKey(ssl, ecc_kse);
  6080. /* If fail, no error message, TLSX_KeyShare_GenEccKey will do it. */
  6081. }
  6082. if (ret == 0) {
  6083. pubKey = (byte*)XMALLOC(ecc_kse->pubKeyLen + pubSz, ssl->heap,
  6084. DYNAMIC_TYPE_PUBLIC_KEY);
  6085. if (pubKey == NULL) {
  6086. WOLFSSL_MSG("pubkey memory allocation failure");
  6087. ret = MEMORY_ERROR;
  6088. }
  6089. }
  6090. if (ret == 0) {
  6091. privKey = (byte*)XMALLOC(privSz, ssl->heap, DYNAMIC_TYPE_PRIVATE_KEY);
  6092. if (privKey == NULL) {
  6093. WOLFSSL_MSG("privkey memory allocation failure");
  6094. ret = MEMORY_ERROR;
  6095. }
  6096. }
  6097. if (ret == 0) {
  6098. ret = wc_KyberKey_MakeKey(kem, ssl->rng);
  6099. if (ret != 0) {
  6100. WOLFSSL_MSG("lKyber keygen failure");
  6101. }
  6102. }
  6103. if (ret == 0) {
  6104. ret = wc_KyberKey_EncodePublicKey(kem, pubKey + ecc_kse->pubKeyLen,
  6105. pubSz);
  6106. }
  6107. if (ret == 0) {
  6108. ret = wc_KyberKey_EncodePrivateKey(kem, privKey, privSz);
  6109. }
  6110. if (ret == 0) {
  6111. XMEMCPY(pubKey, ecc_kse->pubKey, ecc_kse->pubKeyLen);
  6112. kse->pubKey = pubKey;
  6113. kse->pubKeyLen = ecc_kse->pubKeyLen + pubSz;
  6114. pubKey = NULL;
  6115. /* Note we are saving the OQS private key and ECC private key
  6116. * separately. That's because the ECC private key is not simply a
  6117. * buffer. Its is an ecc_key struct.
  6118. */
  6119. kse->privKey = privKey;
  6120. privKey = NULL;
  6121. kse->key = ecc_kse->key;
  6122. ecc_kse->key = NULL;
  6123. }
  6124. #ifdef WOLFSSL_DEBUG_TLS
  6125. WOLFSSL_MSG("Public Kyber Key");
  6126. WOLFSSL_BUFFER(kse->pubKey, kse->pubKeyLen );
  6127. #endif
  6128. wc_KyberKey_Free(kem);
  6129. TLSX_KeyShare_FreeAll(ecc_kse, ssl->heap);
  6130. if (pubKey != NULL)
  6131. XFREE(pubKey, ssl->heap, DYNAMIC_TYPE_PUBLIC_KEY);
  6132. if (privKey != NULL)
  6133. XFREE(privKey, ssl->heap, DYNAMIC_TYPE_PRIVATE_KEY);
  6134. return ret;
  6135. }
  6136. #elif defined(HAVE_LIBOQS)
  6137. static int TLSX_KeyShare_GenPqcKey(WOLFSSL *ssl, KeyShareEntry* kse)
  6138. {
  6139. int ret = 0;
  6140. const char* algName = NULL;
  6141. OQS_KEM* kem = NULL;
  6142. byte* pubKey = NULL;
  6143. byte* privKey = NULL;
  6144. KeyShareEntry *ecc_kse = NULL;
  6145. int oqs_group = 0;
  6146. int ecc_group = 0;
  6147. findEccPqc(&ecc_group, &oqs_group, kse->group);
  6148. algName = OQS_ID2name(oqs_group);
  6149. if (algName == NULL) {
  6150. WOLFSSL_MSG("Invalid OQS algorithm specified.");
  6151. return BAD_FUNC_ARG;
  6152. }
  6153. kem = OQS_KEM_new(algName);
  6154. if (kem == NULL) {
  6155. WOLFSSL_MSG("Error creating OQS KEM, ensure algorithm support"
  6156. "was enabled in liboqs.");
  6157. return BAD_FUNC_ARG;
  6158. }
  6159. ecc_kse = (KeyShareEntry*)XMALLOC(sizeof(*ecc_kse), ssl->heap,
  6160. DYNAMIC_TYPE_TLSX);
  6161. if (ecc_kse == NULL) {
  6162. WOLFSSL_MSG("ecc_kse memory allocation failure");
  6163. ret = MEMORY_ERROR;
  6164. }
  6165. if (ret == 0) {
  6166. XMEMSET(ecc_kse, 0, sizeof(*ecc_kse));
  6167. }
  6168. if (ret == 0 && ecc_group != 0) {
  6169. ecc_kse->group = ecc_group;
  6170. ret = TLSX_KeyShare_GenEccKey(ssl, ecc_kse);
  6171. /* If fail, no error message, TLSX_KeyShare_GenEccKey will do it. */
  6172. }
  6173. if (ret == 0) {
  6174. pubKey = (byte*)XMALLOC(ecc_kse->pubKeyLen + kem->length_public_key,
  6175. ssl->heap, DYNAMIC_TYPE_PUBLIC_KEY);
  6176. if (pubKey == NULL) {
  6177. WOLFSSL_MSG("pubkey memory allocation failure");
  6178. ret = MEMORY_ERROR;
  6179. }
  6180. }
  6181. if (ret == 0) {
  6182. privKey = (byte*)XMALLOC(kem->length_secret_key,
  6183. ssl->heap, DYNAMIC_TYPE_PRIVATE_KEY);
  6184. if (privKey == NULL) {
  6185. WOLFSSL_MSG("privkey memory allocation failure");
  6186. ret = MEMORY_ERROR;
  6187. }
  6188. }
  6189. if (ret == 0) {
  6190. if (OQS_KEM_keypair(kem, pubKey + ecc_kse->pubKeyLen, privKey) ==
  6191. OQS_SUCCESS) {
  6192. XMEMCPY(pubKey, ecc_kse->pubKey, ecc_kse->pubKeyLen);
  6193. kse->pubKey = pubKey;
  6194. kse->pubKeyLen = ecc_kse->pubKeyLen +
  6195. (word32) kem->length_public_key;
  6196. pubKey = NULL;
  6197. /* Note we are saving the OQS private key and ECC private key
  6198. * separately. That's because the ECC private key is not simply a
  6199. * buffer. Its is an ecc_key struct.
  6200. */
  6201. kse->privKey = privKey;
  6202. privKey = NULL;
  6203. kse->key = ecc_kse->key;
  6204. ecc_kse->key = NULL;
  6205. ret = 0;
  6206. }
  6207. else {
  6208. WOLFSSL_MSG("liboqs keygen failure");
  6209. ret = BAD_FUNC_ARG;
  6210. WOLFSSL_ERROR_VERBOSE(ret);
  6211. }
  6212. }
  6213. #ifdef WOLFSSL_DEBUG_TLS
  6214. WOLFSSL_MSG("Public liboqs Key");
  6215. WOLFSSL_BUFFER(kse->pubKey, kse->pubKeyLen);
  6216. #endif
  6217. OQS_KEM_free(kem);
  6218. TLSX_KeyShare_FreeAll(ecc_kse, ssl->heap);
  6219. if (pubKey != NULL)
  6220. XFREE(pubKey, ssl->heap, DYNAMIC_TYPE_PUBLIC_KEY);
  6221. if (privKey != NULL)
  6222. XFREE(privKey, ssl->heap, DYNAMIC_TYPE_PRIVATE_KEY);
  6223. return ret;
  6224. }
  6225. #elif defined(HAVE_PQM4)
  6226. static int TLSX_KeyShare_GenPqcKey(WOLFSSL *ssl, KeyShareEntry* kse)
  6227. {
  6228. /* This assumes KYBER LEVEL 1 (512) implementation is compiled in. */
  6229. int ret = 0;
  6230. byte* pubKey = NULL;
  6231. byte* privKey = NULL;
  6232. KeyShareEntry *ecc_kse = NULL;
  6233. int oqs_group = 0;
  6234. int ecc_group = 0;
  6235. findEccPqc(&ecc_group, &oqs_group, kse->group);
  6236. ecc_kse = (KeyShareEntry*)XMALLOC(sizeof(*ecc_kse), ssl->heap,
  6237. DYNAMIC_TYPE_TLSX);
  6238. if (ecc_kse == NULL) {
  6239. WOLFSSL_MSG("ecc_kse memory allocation failure");
  6240. ret = MEMORY_ERROR;
  6241. }
  6242. if (ret == 0) {
  6243. XMEMSET(ecc_kse, 0, sizeof(*ecc_kse));
  6244. }
  6245. if (ret == 0 && ecc_group != 0) {
  6246. ecc_kse->group = ecc_group;
  6247. ret = TLSX_KeyShare_GenEccKey(ssl, ecc_kse);
  6248. /* If fail, no error message, TLSX_KeyShare_GenEccKey will do it. */
  6249. }
  6250. if (ret == 0) {
  6251. pubKey = (byte*)XMALLOC(ecc_kse->pubKeyLen + PQM4_PUBLIC_KEY_LENGTH,
  6252. ssl->heap, DYNAMIC_TYPE_PUBLIC_KEY);
  6253. if (pubKey == NULL) {
  6254. WOLFSSL_MSG("pubkey memory allocation failure");
  6255. ret = MEMORY_ERROR;
  6256. }
  6257. }
  6258. if (ret == 0) {
  6259. privKey = (byte*)XMALLOC(PQM4_PRIVATE_KEY_LENGTH,
  6260. ssl->heap, DYNAMIC_TYPE_PRIVATE_KEY);
  6261. if (privKey == NULL) {
  6262. WOLFSSL_MSG("privkey memory allocation failure");
  6263. ret = MEMORY_ERROR;
  6264. }
  6265. }
  6266. if (ret == 0) {
  6267. if (crypto_kem_keypair(pubKey + ecc_kse->pubKeyLen, privKey) == 0) {
  6268. XMEMCPY(pubKey, ecc_kse->pubKey, ecc_kse->pubKeyLen);
  6269. kse->pubKey = pubKey;
  6270. kse->pubKeyLen = ecc_kse->pubKeyLen +
  6271. (word32) PQM4_PUBLIC_KEY_LENGTH;
  6272. pubKey = NULL;
  6273. /* Note we are saving the PQ private key and ECC private key
  6274. * separately. That's because the ECC private key is not simply a
  6275. * buffer. Its is an ecc_key struct.
  6276. */
  6277. kse->privKey = privKey;
  6278. privKey = NULL;
  6279. kse->key = ecc_kse->key;
  6280. ecc_kse->key = NULL;
  6281. ret = 0;
  6282. }
  6283. else {
  6284. WOLFSSL_MSG("liboqs keygen failure");
  6285. ret = BAD_FUNC_ARG;
  6286. WOLFSSL_ERROR_VERBOSE(ret);
  6287. }
  6288. }
  6289. #ifdef WOLFSSL_DEBUG_TLS
  6290. WOLFSSL_MSG("Public PQM4 Key");
  6291. WOLFSSL_BUFFER(kse->pubKey, kse->pubKeyLen );
  6292. #endif
  6293. TLSX_KeyShare_FreeAll(ecc_kse, ssl->heap);
  6294. if (pubKey != NULL)
  6295. XFREE(pubKey, ssl->heap, DYNAMIC_TYPE_PUBLIC_KEY);
  6296. if (privKey != NULL)
  6297. XFREE(privKey, ssl->heap, DYNAMIC_TYPE_PRIVATE_KEY);
  6298. return ret;
  6299. }
  6300. #endif /* HAVE_PQM4 */
  6301. #endif /* HAVE_PQC */
  6302. /* Generate a secret/key using the key share entry.
  6303. *
  6304. * ssl The SSL/TLS object.
  6305. * kse The key share entry holding peer data.
  6306. */
  6307. static int TLSX_KeyShare_GenKey(WOLFSSL *ssl, KeyShareEntry *kse)
  6308. {
  6309. int ret;
  6310. /* Named FFDHE groups have a bit set to identify them. */
  6311. if (WOLFSSL_NAMED_GROUP_IS_FFHDE(kse->group))
  6312. ret = TLSX_KeyShare_GenDhKey(ssl, kse);
  6313. else if (kse->group == WOLFSSL_ECC_X25519)
  6314. ret = TLSX_KeyShare_GenX25519Key(ssl, kse);
  6315. else if (kse->group == WOLFSSL_ECC_X448)
  6316. ret = TLSX_KeyShare_GenX448Key(ssl, kse);
  6317. #ifdef HAVE_PQC
  6318. else if (WOLFSSL_NAMED_GROUP_IS_PQC(kse->group))
  6319. ret = TLSX_KeyShare_GenPqcKey(ssl, kse);
  6320. #endif
  6321. else
  6322. ret = TLSX_KeyShare_GenEccKey(ssl, kse);
  6323. #ifdef WOLFSSL_ASYNC_CRYPT
  6324. kse->lastRet = ret;
  6325. #endif
  6326. return ret;
  6327. }
  6328. /* Free the key share dynamic data.
  6329. *
  6330. * list The linked list of key share entry objects.
  6331. * heap The heap used for allocation.
  6332. */
  6333. static void TLSX_KeyShare_FreeAll(KeyShareEntry* list, void* heap)
  6334. {
  6335. KeyShareEntry* current;
  6336. while ((current = list) != NULL) {
  6337. list = current->next;
  6338. if (WOLFSSL_NAMED_GROUP_IS_FFHDE(current->group)) {
  6339. #ifndef NO_DH
  6340. wc_FreeDhKey((DhKey*)current->key);
  6341. #endif
  6342. }
  6343. else if (current->group == WOLFSSL_ECC_X25519) {
  6344. #ifdef HAVE_CURVE25519
  6345. wc_curve25519_free((curve25519_key*)current->key);
  6346. #endif
  6347. }
  6348. else if (current->group == WOLFSSL_ECC_X448) {
  6349. #ifdef HAVE_CURVE448
  6350. wc_curve448_free((curve448_key*)current->key);
  6351. #endif
  6352. }
  6353. #ifdef HAVE_PQC
  6354. else if (WOLFSSL_NAMED_GROUP_IS_PQC(current->group) &&
  6355. current->key != NULL) {
  6356. ForceZero((byte*)current->key, current->keyLen);
  6357. }
  6358. #endif
  6359. else {
  6360. #ifdef HAVE_ECC
  6361. wc_ecc_free((ecc_key*)current->key);
  6362. #endif
  6363. }
  6364. XFREE(current->key, heap, DYNAMIC_TYPE_PRIVATE_KEY);
  6365. #if !defined(NO_DH) && (!defined(NO_CERTS) || !defined(NO_PSK))
  6366. XFREE(current->privKey, heap, DYNAMIC_TYPE_PRIVATE_KEY);
  6367. #endif
  6368. XFREE(current->pubKey, heap, DYNAMIC_TYPE_PUBLIC_KEY);
  6369. XFREE(current->ke, heap, DYNAMIC_TYPE_PUBLIC_KEY);
  6370. XFREE(current, heap, DYNAMIC_TYPE_TLSX);
  6371. }
  6372. (void)heap;
  6373. }
  6374. /* Get the size of the encoded key share extension.
  6375. *
  6376. * list The linked list of key share extensions.
  6377. * msgType The type of the message this extension is being written into.
  6378. * returns the number of bytes of the encoded key share extension.
  6379. */
  6380. static word16 TLSX_KeyShare_GetSize(KeyShareEntry* list, byte msgType)
  6381. {
  6382. word16 len = 0;
  6383. byte isRequest = (msgType == client_hello);
  6384. KeyShareEntry* current;
  6385. /* The named group the server wants to use. */
  6386. if (msgType == hello_retry_request)
  6387. return OPAQUE16_LEN;
  6388. /* List of key exchange groups. */
  6389. if (isRequest)
  6390. len += OPAQUE16_LEN;
  6391. while ((current = list) != NULL) {
  6392. list = current->next;
  6393. if (!isRequest && current->pubKey == NULL)
  6394. continue;
  6395. len += (word16)(KE_GROUP_LEN + OPAQUE16_LEN + current->pubKeyLen);
  6396. }
  6397. return len;
  6398. }
  6399. /* Writes the key share extension into the output buffer.
  6400. * Assumes that the the output buffer is big enough to hold data.
  6401. *
  6402. * list The linked list of key share entries.
  6403. * output The buffer to write into.
  6404. * msgType The type of the message this extension is being written into.
  6405. * returns the number of bytes written into the buffer.
  6406. */
  6407. static word16 TLSX_KeyShare_Write(KeyShareEntry* list, byte* output,
  6408. byte msgType)
  6409. {
  6410. word16 i = 0;
  6411. byte isRequest = (msgType == client_hello);
  6412. KeyShareEntry* current;
  6413. if (msgType == hello_retry_request) {
  6414. c16toa(list->group, output);
  6415. return OPAQUE16_LEN;
  6416. }
  6417. /* ClientHello has a list but ServerHello is only the chosen. */
  6418. if (isRequest)
  6419. i += OPAQUE16_LEN;
  6420. /* Write out all in the list. */
  6421. while ((current = list) != NULL) {
  6422. list = current->next;
  6423. if (!isRequest && current->pubKey == NULL)
  6424. continue;
  6425. c16toa(current->group, &output[i]);
  6426. i += KE_GROUP_LEN;
  6427. c16toa((word16)(current->pubKeyLen), &output[i]);
  6428. i += OPAQUE16_LEN;
  6429. XMEMCPY(&output[i], current->pubKey, current->pubKeyLen);
  6430. i += (word16)current->pubKeyLen;
  6431. }
  6432. /* Write the length of the list if required. */
  6433. if (isRequest)
  6434. c16toa(i - OPAQUE16_LEN, output);
  6435. return i;
  6436. }
  6437. /* Process the DH key share extension on the client side.
  6438. *
  6439. * ssl The SSL/TLS object.
  6440. * keyShareEntry The key share entry object to use to calculate shared secret.
  6441. * returns 0 on success and other values indicate failure.
  6442. */
  6443. static int TLSX_KeyShare_ProcessDh(WOLFSSL* ssl, KeyShareEntry* keyShareEntry)
  6444. {
  6445. int ret = 0;
  6446. #if !defined(NO_DH) && (!defined(NO_CERTS) || !defined(NO_PSK))
  6447. word32 pSz = 0;
  6448. DhKey* dhKey = (DhKey*)keyShareEntry->key;
  6449. #ifdef HAVE_PUBLIC_FFDHE
  6450. const DhParams* params = NULL;
  6451. switch (keyShareEntry->group) {
  6452. #ifdef HAVE_FFDHE_2048
  6453. case WOLFSSL_FFDHE_2048:
  6454. params = wc_Dh_ffdhe2048_Get();
  6455. break;
  6456. #endif
  6457. #ifdef HAVE_FFDHE_3072
  6458. case WOLFSSL_FFDHE_3072:
  6459. params = wc_Dh_ffdhe3072_Get();
  6460. break;
  6461. #endif
  6462. #ifdef HAVE_FFDHE_4096
  6463. case WOLFSSL_FFDHE_4096:
  6464. params = wc_Dh_ffdhe4096_Get();
  6465. break;
  6466. #endif
  6467. #ifdef HAVE_FFDHE_6144
  6468. case WOLFSSL_FFDHE_6144:
  6469. params = wc_Dh_ffdhe6144_Get();
  6470. break;
  6471. #endif
  6472. #ifdef HAVE_FFDHE_8192
  6473. case WOLFSSL_FFDHE_8192:
  6474. params = wc_Dh_ffdhe8192_Get();
  6475. break;
  6476. #endif
  6477. default:
  6478. break;
  6479. }
  6480. if (params == NULL) {
  6481. WOLFSSL_ERROR_VERBOSE(PEER_KEY_ERROR);
  6482. return PEER_KEY_ERROR;
  6483. }
  6484. pSz = params->p_len;
  6485. #else
  6486. ret = wc_DhGetNamedKeyParamSize(keyShareEntry->group, &pSz, NULL, NULL);
  6487. if (ret != 0 || pSz == 0) {
  6488. WOLFSSL_ERROR_VERBOSE(PEER_KEY_ERROR);
  6489. return PEER_KEY_ERROR;
  6490. }
  6491. #endif
  6492. /* if DhKey is not setup, do it now */
  6493. if (keyShareEntry->key == NULL) {
  6494. keyShareEntry->key = (DhKey*)XMALLOC(sizeof(DhKey), ssl->heap,
  6495. DYNAMIC_TYPE_DH);
  6496. if (keyShareEntry->key == NULL)
  6497. return MEMORY_E;
  6498. /* Setup Key */
  6499. ret = wc_InitDhKey_ex((DhKey*)keyShareEntry->key, ssl->heap, ssl->devId);
  6500. if (ret == 0) {
  6501. dhKey = (DhKey*)keyShareEntry->key;
  6502. /* Set key */
  6503. #ifdef HAVE_PUBLIC_FFDHE
  6504. ret = wc_DhSetKey(dhKey, params->p, params->p_len, params->g,
  6505. params->g_len);
  6506. #else
  6507. ret = wc_DhSetNamedKey(dhKey, keyShareEntry->group);
  6508. #endif
  6509. }
  6510. }
  6511. if (ret == 0
  6512. #ifdef WOLFSSL_ASYNC_CRYPT
  6513. && keyShareEntry->lastRet == 0 /* don't enter here if WC_PENDING_E */
  6514. #endif
  6515. ) {
  6516. #ifdef WOLFSSL_DEBUG_TLS
  6517. WOLFSSL_MSG("Peer DH Key");
  6518. WOLFSSL_BUFFER(keyShareEntry->ke, keyShareEntry->keLen);
  6519. #endif
  6520. ssl->options.dhKeySz = (word16)pSz;
  6521. /* Derive secret from private key and peer's public key. */
  6522. ret = DhAgree(ssl, dhKey,
  6523. (const byte*)keyShareEntry->privKey, keyShareEntry->keyLen, /* our private */
  6524. keyShareEntry->ke, keyShareEntry->keLen, /* peer's public key */
  6525. ssl->arrays->preMasterSecret, &ssl->arrays->preMasterSz, /* secret */
  6526. NULL, 0
  6527. );
  6528. #ifdef WOLFSSL_ASYNC_CRYPT
  6529. if (ret == WC_PENDING_E) {
  6530. return ret;
  6531. }
  6532. #endif
  6533. }
  6534. /* RFC 8446 Section 7.4.1:
  6535. * ... left-padded with zeros up to the size of the prime. ...
  6536. */
  6537. if (ret == 0 && (word32)ssl->options.dhKeySz > ssl->arrays->preMasterSz) {
  6538. word32 diff = (word32)ssl->options.dhKeySz - ssl->arrays->preMasterSz;
  6539. XMEMMOVE(ssl->arrays->preMasterSecret + diff,
  6540. ssl->arrays->preMasterSecret, ssl->arrays->preMasterSz);
  6541. XMEMSET(ssl->arrays->preMasterSecret, 0, diff);
  6542. ssl->arrays->preMasterSz = ssl->options.dhKeySz;
  6543. }
  6544. /* done with key share, release resources */
  6545. if (dhKey)
  6546. wc_FreeDhKey(dhKey);
  6547. if (keyShareEntry->key) {
  6548. XFREE(keyShareEntry->key, ssl->heap, DYNAMIC_TYPE_DH);
  6549. keyShareEntry->key = NULL;
  6550. }
  6551. if (keyShareEntry->privKey != NULL) {
  6552. XFREE(keyShareEntry->privKey, ssl->heap, DYNAMIC_TYPE_PRIVATE_KEY);
  6553. keyShareEntry->privKey = NULL;
  6554. }
  6555. if (keyShareEntry->pubKey != NULL) {
  6556. XFREE(keyShareEntry->pubKey, ssl->heap, DYNAMIC_TYPE_PUBLIC_KEY);
  6557. keyShareEntry->pubKey = NULL;
  6558. }
  6559. XFREE(keyShareEntry->ke, ssl->heap, DYNAMIC_TYPE_PUBLIC_KEY);
  6560. keyShareEntry->ke = NULL;
  6561. #else
  6562. (void)ssl;
  6563. (void)keyShareEntry;
  6564. ret = PEER_KEY_ERROR;
  6565. WOLFSSL_ERROR_VERBOSE(ret);
  6566. #endif
  6567. return ret;
  6568. }
  6569. /* Process the X25519 key share extension on the client side.
  6570. *
  6571. * ssl The SSL/TLS object.
  6572. * keyShareEntry The key share entry object to use to calculate shared secret.
  6573. * returns 0 on success and other values indicate failure.
  6574. */
  6575. static int TLSX_KeyShare_ProcessX25519(WOLFSSL* ssl,
  6576. KeyShareEntry* keyShareEntry)
  6577. {
  6578. int ret;
  6579. #ifdef HAVE_CURVE25519
  6580. curve25519_key* key = (curve25519_key*)keyShareEntry->key;
  6581. curve25519_key* peerX25519Key;
  6582. #ifdef HAVE_ECC
  6583. if (ssl->peerEccKey != NULL) {
  6584. wc_ecc_free(ssl->peerEccKey);
  6585. ssl->peerEccKey = NULL;
  6586. ssl->peerEccKeyPresent = 0;
  6587. }
  6588. #endif
  6589. peerX25519Key = (curve25519_key*)XMALLOC(sizeof(curve25519_key), ssl->heap,
  6590. DYNAMIC_TYPE_TLSX);
  6591. if (peerX25519Key == NULL) {
  6592. WOLFSSL_MSG("PeerEccKey Memory error");
  6593. return MEMORY_ERROR;
  6594. }
  6595. ret = wc_curve25519_init(peerX25519Key);
  6596. if (ret != 0) {
  6597. XFREE(peerX25519Key, ssl->heap, DYNAMIC_TYPE_TLSX);
  6598. return ret;
  6599. }
  6600. #ifdef WOLFSSL_DEBUG_TLS
  6601. WOLFSSL_MSG("Peer Curve25519 Key");
  6602. WOLFSSL_BUFFER(keyShareEntry->ke, keyShareEntry->keLen);
  6603. #endif
  6604. if (wc_curve25519_check_public(keyShareEntry->ke, keyShareEntry->keLen,
  6605. EC25519_LITTLE_ENDIAN) != 0) {
  6606. ret = ECC_PEERKEY_ERROR;
  6607. WOLFSSL_ERROR_VERBOSE(ret);
  6608. }
  6609. if (ret == 0) {
  6610. if (wc_curve25519_import_public_ex(keyShareEntry->ke,
  6611. keyShareEntry->keLen, peerX25519Key,
  6612. EC25519_LITTLE_ENDIAN) != 0) {
  6613. ret = ECC_PEERKEY_ERROR;
  6614. WOLFSSL_ERROR_VERBOSE(ret);
  6615. }
  6616. }
  6617. if (ret == 0) {
  6618. ssl->ecdhCurveOID = ECC_X25519_OID;
  6619. ret = wc_curve25519_shared_secret_ex(key, peerX25519Key,
  6620. ssl->arrays->preMasterSecret,
  6621. &ssl->arrays->preMasterSz,
  6622. EC25519_LITTLE_ENDIAN);
  6623. }
  6624. wc_curve25519_free(peerX25519Key);
  6625. XFREE(peerX25519Key, ssl->heap, DYNAMIC_TYPE_TLSX);
  6626. wc_curve25519_free((curve25519_key*)keyShareEntry->key);
  6627. if (keyShareEntry->key != NULL) {
  6628. XFREE(keyShareEntry->key, ssl->heap, DYNAMIC_TYPE_PRIVATE_KEY);
  6629. keyShareEntry->key = NULL;
  6630. }
  6631. #else
  6632. (void)ssl;
  6633. (void)keyShareEntry;
  6634. ret = PEER_KEY_ERROR;
  6635. WOLFSSL_ERROR_VERBOSE(ret);
  6636. #endif /* HAVE_CURVE25519 */
  6637. return ret;
  6638. }
  6639. /* Process the X448 key share extension on the client side.
  6640. *
  6641. * ssl The SSL/TLS object.
  6642. * keyShareEntry The key share entry object to use to calculate shared secret.
  6643. * returns 0 on success and other values indicate failure.
  6644. */
  6645. static int TLSX_KeyShare_ProcessX448(WOLFSSL* ssl, KeyShareEntry* keyShareEntry)
  6646. {
  6647. int ret;
  6648. #ifdef HAVE_CURVE448
  6649. curve448_key* key = (curve448_key*)keyShareEntry->key;
  6650. curve448_key* peerX448Key;
  6651. #ifdef HAVE_ECC
  6652. if (ssl->peerEccKey != NULL) {
  6653. wc_ecc_free(ssl->peerEccKey);
  6654. ssl->peerEccKey = NULL;
  6655. ssl->peerEccKeyPresent = 0;
  6656. }
  6657. #endif
  6658. peerX448Key = (curve448_key*)XMALLOC(sizeof(curve448_key), ssl->heap,
  6659. DYNAMIC_TYPE_TLSX);
  6660. if (peerX448Key == NULL) {
  6661. WOLFSSL_MSG("PeerEccKey Memory error");
  6662. return MEMORY_ERROR;
  6663. }
  6664. ret = wc_curve448_init(peerX448Key);
  6665. if (ret != 0) {
  6666. XFREE(peerX448Key, ssl->heap, DYNAMIC_TYPE_TLSX);
  6667. return ret;
  6668. }
  6669. #ifdef WOLFSSL_DEBUG_TLS
  6670. WOLFSSL_MSG("Peer Curve448 Key");
  6671. WOLFSSL_BUFFER(keyShareEntry->ke, keyShareEntry->keLen);
  6672. #endif
  6673. if (wc_curve448_check_public(keyShareEntry->ke, keyShareEntry->keLen,
  6674. EC448_LITTLE_ENDIAN) != 0) {
  6675. ret = ECC_PEERKEY_ERROR;
  6676. WOLFSSL_ERROR_VERBOSE(ret);
  6677. }
  6678. if (ret == 0) {
  6679. if (wc_curve448_import_public_ex(keyShareEntry->ke,
  6680. keyShareEntry->keLen, peerX448Key,
  6681. EC448_LITTLE_ENDIAN) != 0) {
  6682. ret = ECC_PEERKEY_ERROR;
  6683. WOLFSSL_ERROR_VERBOSE(ret);
  6684. }
  6685. }
  6686. if (ret == 0) {
  6687. ssl->ecdhCurveOID = ECC_X448_OID;
  6688. ret = wc_curve448_shared_secret_ex(key, peerX448Key,
  6689. ssl->arrays->preMasterSecret,
  6690. &ssl->arrays->preMasterSz,
  6691. EC448_LITTLE_ENDIAN);
  6692. }
  6693. wc_curve448_free(peerX448Key);
  6694. XFREE(peerX448Key, ssl->heap, DYNAMIC_TYPE_TLSX);
  6695. wc_curve448_free((curve448_key*)keyShareEntry->key);
  6696. if (keyShareEntry->key != NULL) {
  6697. XFREE(keyShareEntry->key, ssl->heap, DYNAMIC_TYPE_PRIVATE_KEY);
  6698. keyShareEntry->key = NULL;
  6699. }
  6700. #else
  6701. (void)ssl;
  6702. (void)keyShareEntry;
  6703. ret = PEER_KEY_ERROR;
  6704. WOLFSSL_ERROR_VERBOSE(ret);
  6705. #endif /* HAVE_CURVE448 */
  6706. return ret;
  6707. }
  6708. /* Process the ECC key share extension on the client side.
  6709. *
  6710. * ssl The SSL/TLS object.
  6711. * keyShareEntry The key share entry object to use to calculate shared secret.
  6712. * returns 0 on success and other values indicate failure.
  6713. */
  6714. static int TLSX_KeyShare_ProcessEcc(WOLFSSL* ssl, KeyShareEntry* keyShareEntry)
  6715. {
  6716. int ret = 0;
  6717. #ifdef HAVE_ECC
  6718. int curveId = ECC_CURVE_INVALID;
  6719. ecc_key* eccKey = (ecc_key*)keyShareEntry->key;
  6720. /* find supported curve */
  6721. switch (keyShareEntry->group) {
  6722. #if (!defined(NO_ECC256) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 256
  6723. #ifndef NO_ECC_SECP
  6724. case WOLFSSL_ECC_SECP256R1:
  6725. curveId = ECC_SECP256R1;
  6726. break;
  6727. #endif /* !NO_ECC_SECP */
  6728. #endif
  6729. #if (defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 384
  6730. #ifndef NO_ECC_SECP
  6731. case WOLFSSL_ECC_SECP384R1:
  6732. curveId = ECC_SECP384R1;
  6733. break;
  6734. #endif /* !NO_ECC_SECP */
  6735. #endif
  6736. #if (defined(HAVE_ECC521) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 521
  6737. #ifndef NO_ECC_SECP
  6738. case WOLFSSL_ECC_SECP521R1:
  6739. curveId = ECC_SECP521R1;
  6740. break;
  6741. #endif /* !NO_ECC_SECP */
  6742. #endif
  6743. #if defined(HAVE_X448) && ECC_MIN_KEY_SZ <= 448
  6744. case WOLFSSL_ECC_X448:
  6745. curveId = ECC_X448;
  6746. break;
  6747. #endif
  6748. default:
  6749. /* unsupported curve */
  6750. WOLFSSL_ERROR_VERBOSE(ECC_PEERKEY_ERROR);
  6751. return ECC_PEERKEY_ERROR;
  6752. }
  6753. #ifdef WOLFSSL_ASYNC_CRYPT
  6754. if (keyShareEntry->lastRet == 0) /* don't enter here if WC_PENDING_E */
  6755. #endif
  6756. {
  6757. #ifdef WOLFSSL_DEBUG_TLS
  6758. WOLFSSL_MSG("Peer ECC Key");
  6759. WOLFSSL_BUFFER(keyShareEntry->ke, keyShareEntry->keLen);
  6760. #endif
  6761. if (ssl->peerEccKey != NULL) {
  6762. wc_ecc_free(ssl->peerEccKey);
  6763. XFREE(ssl->peerEccKey, ssl->heap, DYNAMIC_TYPE_ECC);
  6764. ssl->peerEccKeyPresent = 0;
  6765. }
  6766. #if defined(WOLFSSL_RENESAS_TSIP_TLS) && (WOLFSSL_RENESAS_TSIP_VER >= 115)
  6767. ret = tsip_Tls13GenSharedSecret(ssl, keyShareEntry);
  6768. if (ret != CRYPTOCB_UNAVAILABLE) {
  6769. return ret;
  6770. }
  6771. ret = 0;
  6772. #endif
  6773. ssl->peerEccKey = (ecc_key*)XMALLOC(sizeof(ecc_key), ssl->heap,
  6774. DYNAMIC_TYPE_ECC);
  6775. if (ssl->peerEccKey == NULL) {
  6776. WOLFSSL_MSG("PeerEccKey Memory error");
  6777. ret = MEMORY_ERROR;
  6778. }
  6779. if (ret == 0) {
  6780. ret = wc_ecc_init_ex(ssl->peerEccKey, ssl->heap, ssl->devId);
  6781. }
  6782. /* Point is validated by import function. */
  6783. if (ret == 0) {
  6784. ret = wc_ecc_import_x963_ex(keyShareEntry->ke, keyShareEntry->keLen,
  6785. ssl->peerEccKey, curveId);
  6786. if (ret != 0) {
  6787. ret = ECC_PEERKEY_ERROR;
  6788. WOLFSSL_ERROR_VERBOSE(ret);
  6789. }
  6790. }
  6791. if (ret == 0) {
  6792. ssl->ecdhCurveOID = ssl->peerEccKey->dp->oidSum;
  6793. ssl->peerEccKeyPresent = 1;
  6794. }
  6795. }
  6796. if (ret == 0 && eccKey == NULL)
  6797. ret = BAD_FUNC_ARG;
  6798. if (ret == 0) {
  6799. ret = EccSharedSecret(ssl, eccKey, ssl->peerEccKey,
  6800. keyShareEntry->ke, &keyShareEntry->keLen,
  6801. ssl->arrays->preMasterSecret, &ssl->arrays->preMasterSz,
  6802. ssl->options.side
  6803. );
  6804. #ifdef WOLFSSL_ASYNC_CRYPT
  6805. if (ret == WC_PENDING_E)
  6806. return ret;
  6807. #endif
  6808. }
  6809. /* done with key share, release resources */
  6810. if (ssl->peerEccKey != NULL
  6811. #ifdef HAVE_PK_CALLBACKS
  6812. && ssl->ctx->EccSharedSecretCb == NULL
  6813. #endif
  6814. ) {
  6815. wc_ecc_free(ssl->peerEccKey);
  6816. XFREE(ssl->peerEccKey, ssl->heap, DYNAMIC_TYPE_ECC);
  6817. ssl->peerEccKey = NULL;
  6818. ssl->peerEccKeyPresent = 0;
  6819. }
  6820. if (keyShareEntry->key) {
  6821. wc_ecc_free((ecc_key*)keyShareEntry->key);
  6822. XFREE(keyShareEntry->key, ssl->heap, DYNAMIC_TYPE_ECC);
  6823. keyShareEntry->key = NULL;
  6824. }
  6825. XFREE(keyShareEntry->ke, ssl->heap, DYNAMIC_TYPE_PUBLIC_KEY);
  6826. keyShareEntry->ke = NULL;
  6827. #else
  6828. (void)ssl;
  6829. (void)keyShareEntry;
  6830. ret = PEER_KEY_ERROR;
  6831. WOLFSSL_ERROR_VERBOSE(ret);
  6832. #endif /* HAVE_ECC */
  6833. return ret;
  6834. }
  6835. #ifdef HAVE_PQC
  6836. #ifdef WOLFSSL_WC_KYBER
  6837. /* Process the Kyber key share extension on the client side.
  6838. *
  6839. * ssl The SSL/TLS object.
  6840. * keyShareEntry The key share entry object to use to calculate shared secret.
  6841. * returns 0 on success and other values indicate failure.
  6842. */
  6843. static int TLSX_KeyShare_ProcessPqc(WOLFSSL* ssl, KeyShareEntry* keyShareEntry)
  6844. {
  6845. int ret = 0;
  6846. int type;
  6847. KyberKey kem[1];
  6848. byte* sharedSecret = NULL;
  6849. word32 sharedSecretLen = 0;
  6850. int oqs_group = 0;
  6851. int ecc_group = 0;
  6852. ecc_key eccpubkey;
  6853. word32 outlen = 0;
  6854. word32 privSz = 0;
  6855. word32 ctSz = 0;
  6856. word32 ssSz = 0;
  6857. if (keyShareEntry->ke == NULL) {
  6858. WOLFSSL_MSG("Invalid OQS algorithm specified.");
  6859. return BAD_FUNC_ARG;
  6860. }
  6861. if (ssl->options.side == WOLFSSL_SERVER_END) {
  6862. /* I am the server, the shared secret has already been generated and
  6863. * is in keyShareEntry->ke; copy it to the pre-master secret
  6864. * pre-allocated buffer. */
  6865. if (keyShareEntry->keLen > ENCRYPT_LEN) {
  6866. WOLFSSL_MSG("shared secret is too long.");
  6867. return LENGTH_ERROR;
  6868. }
  6869. XMEMCPY(ssl->arrays->preMasterSecret, keyShareEntry->ke,
  6870. keyShareEntry->keLen);
  6871. ssl->arrays->preMasterSz = keyShareEntry->keLen;
  6872. XFREE(keyShareEntry->ke, sl->heap, DYNAMIC_TYPE_SECRET)
  6873. keyShareEntry->ke = NULL;
  6874. keyShareEntry->keLen = 0;
  6875. return 0;
  6876. }
  6877. /* I am the client, the ciphertext is in keyShareEntry->ke */
  6878. findEccPqc(&ecc_group, &oqs_group, keyShareEntry->group);
  6879. ret = kyber_id2type(oqs_group, &type);
  6880. if (ret != 0) {
  6881. WOLFSSL_MSG("Invalid OQS algorithm specified.");
  6882. ret = BAD_FUNC_ARG;
  6883. }
  6884. if (ret == 0) {
  6885. ret = wc_KyberKey_Init(type, kem, ssl->heap, INVALID_DEVID);
  6886. if (ret != 0) {
  6887. WOLFSSL_MSG("Error creating Kyber KEM");
  6888. }
  6889. }
  6890. if (ret == 0) {
  6891. ret = wc_KyberKey_SharedSecretSize(kem, &ssSz);
  6892. }
  6893. if (ret == 0) {
  6894. sharedSecretLen = ssSz;
  6895. switch (ecc_group) {
  6896. case WOLFSSL_ECC_SECP256R1:
  6897. sharedSecretLen += 32;
  6898. outlen = 32;
  6899. break;
  6900. case WOLFSSL_ECC_SECP384R1:
  6901. sharedSecretLen += 48;
  6902. outlen = 48;
  6903. break;
  6904. case WOLFSSL_ECC_SECP521R1:
  6905. sharedSecretLen += 66;
  6906. outlen = 66;
  6907. break;
  6908. default:
  6909. break;
  6910. }
  6911. ret = wc_ecc_init_ex(&eccpubkey, ssl->heap, ssl->devId);
  6912. if (ret != 0) {
  6913. WOLFSSL_MSG("Memory allocation error.");
  6914. ret = MEMORY_E;
  6915. }
  6916. }
  6917. if (ret == 0) {
  6918. sharedSecret = (byte*)XMALLOC(sharedSecretLen, ssl->heap,
  6919. DYNAMIC_TYPE_TLSX);
  6920. if (sharedSecret == NULL) {
  6921. WOLFSSL_MSG("Memory allocation error.");
  6922. ret = MEMORY_E;
  6923. }
  6924. }
  6925. if (ret == 0) {
  6926. ret = wc_KyberKey_CipherTextSize(kem, &ctSz);
  6927. }
  6928. if (ret == 0) {
  6929. ret = wc_KyberKey_PrivateKeySize(kem, &privSz);
  6930. }
  6931. if (ret == 0) {
  6932. ret = wc_KyberKey_DecodePrivateKey(kem, keyShareEntry->privKey, privSz);
  6933. }
  6934. if (ret == 0) {
  6935. ret = wc_KyberKey_Decapsulate(kem, sharedSecret + outlen,
  6936. keyShareEntry->ke + keyShareEntry->keLen - ctSz, ctSz);
  6937. if (ret != 0) {
  6938. WOLFSSL_MSG("wc_KyberKey decapsulation failure.");
  6939. ret = BAD_FUNC_ARG;
  6940. }
  6941. }
  6942. if (ecc_group != 0) {
  6943. if (ret == 0) {
  6944. /* Point is validated by import function. */
  6945. ret = wc_ecc_import_x963(keyShareEntry->ke,
  6946. keyShareEntry->keLen - ctSz,
  6947. &eccpubkey);
  6948. if (ret != 0) {
  6949. WOLFSSL_MSG("ECC Public key import error.");
  6950. }
  6951. }
  6952. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  6953. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  6954. !defined(HAVE_SELFTEST)
  6955. if (ret == 0) {
  6956. ret = wc_ecc_set_rng(keyShareEntry->key, ssl->rng);
  6957. if (ret != 0) {
  6958. WOLFSSL_MSG("Failure to set the ECC private key RNG.");
  6959. }
  6960. }
  6961. #endif
  6962. if (ret == 0) {
  6963. PRIVATE_KEY_UNLOCK();
  6964. ret = wc_ecc_shared_secret(keyShareEntry->key, &eccpubkey,
  6965. sharedSecret, &outlen);
  6966. PRIVATE_KEY_LOCK();
  6967. if (outlen != sharedSecretLen - ssSz) {
  6968. WOLFSSL_MSG("ECC shared secret derivation error.");
  6969. ret = BAD_FUNC_ARG;
  6970. }
  6971. }
  6972. }
  6973. if ((ret == 0) && (sharedSecretLen > ENCRYPT_LEN)) {
  6974. WOLFSSL_MSG("shared secret is too long.");
  6975. ret = LENGTH_ERROR;
  6976. }
  6977. if (ret == 0) {
  6978. /* Copy the shared secret to the pre-master secret pre-allocated
  6979. * buffer. */
  6980. XMEMCPY(ssl->arrays->preMasterSecret, sharedSecret, sharedSecretLen);
  6981. ssl->arrays->preMasterSz = (word32) sharedSecretLen;
  6982. }
  6983. if (sharedSecret != NULL) {
  6984. XFREE(sharedSecret, ssl->heap, DYNAMIC_TYPE_SECRET);
  6985. }
  6986. wc_ecc_free(&eccpubkey);
  6987. wc_KyberKey_Free(kem);
  6988. return ret;
  6989. }
  6990. #elif defined(HAVE_LIBOQS)
  6991. /* Process the liboqs key share extension on the client side.
  6992. *
  6993. * ssl The SSL/TLS object.
  6994. * keyShareEntry The key share entry object to use to calculate shared secret.
  6995. * returns 0 on success and other values indicate failure.
  6996. */
  6997. static int TLSX_KeyShare_ProcessPqc(WOLFSSL* ssl, KeyShareEntry* keyShareEntry)
  6998. {
  6999. int ret = 0;
  7000. const char* algName = NULL;
  7001. OQS_KEM* kem = NULL;
  7002. byte* sharedSecret = NULL;
  7003. word32 sharedSecretLen = 0;
  7004. int oqs_group = 0;
  7005. int ecc_group = 0;
  7006. ecc_key eccpubkey;
  7007. word32 outlen = 0;
  7008. if (keyShareEntry->ke == NULL) {
  7009. WOLFSSL_MSG("Invalid OQS algorithm specified.");
  7010. return BAD_FUNC_ARG;
  7011. }
  7012. if (ssl->options.side == WOLFSSL_SERVER_END) {
  7013. /* I am the server, the shared secret has already been generated and
  7014. * is in keyShareEntry->ke; copy it to the pre-master secret
  7015. * pre-allocated buffer. */
  7016. if (keyShareEntry->keLen > ENCRYPT_LEN) {
  7017. WOLFSSL_MSG("shared secret is too long.");
  7018. WOLFSSL_ERROR_VERBOSE(LENGTH_ERROR);
  7019. return LENGTH_ERROR;
  7020. }
  7021. XMEMCPY(ssl->arrays->preMasterSecret, keyShareEntry->ke, keyShareEntry->keLen);
  7022. ssl->arrays->preMasterSz = keyShareEntry->keLen;
  7023. XFREE(keyShareEntry->ke, sl->heap, DYNAMIC_TYPE_SECRET)
  7024. keyShareEntry->ke = NULL;
  7025. keyShareEntry->keLen = 0;
  7026. return 0;
  7027. }
  7028. /* I am the client, the ciphertext is in keyShareEntry->ke */
  7029. findEccPqc(&ecc_group, &oqs_group, keyShareEntry->group);
  7030. algName = OQS_ID2name(oqs_group);
  7031. if (algName == NULL) {
  7032. WOLFSSL_MSG("Invalid OQS algorithm specified.");
  7033. WOLFSSL_ERROR_VERBOSE(BAD_FUNC_ARG);
  7034. return BAD_FUNC_ARG;
  7035. }
  7036. kem = OQS_KEM_new(algName);
  7037. if (kem == NULL) {
  7038. WOLFSSL_MSG("Error creating OQS KEM, ensure algorithm support"
  7039. "was enabled in liboqs.");
  7040. return MEMORY_E;
  7041. }
  7042. sharedSecretLen = (word32)kem->length_shared_secret;
  7043. switch (ecc_group) {
  7044. case WOLFSSL_ECC_SECP256R1:
  7045. sharedSecretLen += 32;
  7046. outlen = 32;
  7047. break;
  7048. case WOLFSSL_ECC_SECP384R1:
  7049. sharedSecretLen += 48;
  7050. outlen = 48;
  7051. break;
  7052. case WOLFSSL_ECC_SECP521R1:
  7053. sharedSecretLen += 66;
  7054. outlen = 66;
  7055. break;
  7056. default:
  7057. break;
  7058. }
  7059. ret = wc_ecc_init_ex(&eccpubkey, ssl->heap, ssl->devId);
  7060. if (ret != 0) {
  7061. WOLFSSL_MSG("Memory allocation error.");
  7062. return MEMORY_E;
  7063. }
  7064. sharedSecret = (byte*)XMALLOC(sharedSecretLen, ssl->heap,
  7065. DYNAMIC_TYPE_TLSX);
  7066. if (sharedSecret == NULL) {
  7067. WOLFSSL_MSG("Memory allocation error.");
  7068. ret = MEMORY_E;
  7069. }
  7070. if (ret == 0 && OQS_KEM_decaps(kem, sharedSecret + outlen,
  7071. keyShareEntry->ke + keyShareEntry->keLen -
  7072. kem->length_ciphertext,
  7073. keyShareEntry->privKey) != OQS_SUCCESS) {
  7074. WOLFSSL_MSG("Liboqs decapsulation failure.");
  7075. ret = BAD_FUNC_ARG;
  7076. WOLFSSL_ERROR_VERBOSE(ret);
  7077. }
  7078. if (ecc_group != 0) {
  7079. if (ret == 0) {
  7080. /* Point is validated by import function. */
  7081. ret = wc_ecc_import_x963(keyShareEntry->ke,
  7082. keyShareEntry->keLen -
  7083. (word32)kem->length_ciphertext,
  7084. &eccpubkey);
  7085. if (ret != 0) {
  7086. WOLFSSL_ERROR_VERBOSE(ret);
  7087. WOLFSSL_MSG("ECC Public key import error.");
  7088. }
  7089. }
  7090. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  7091. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  7092. !defined(HAVE_SELFTEST)
  7093. if (ret == 0) {
  7094. ret = wc_ecc_set_rng(keyShareEntry->key, ssl->rng);
  7095. if (ret != 0) {
  7096. WOLFSSL_MSG("Failure to set the ECC private key RNG.");
  7097. }
  7098. }
  7099. #endif
  7100. if (ret == 0) {
  7101. PRIVATE_KEY_UNLOCK();
  7102. ret = wc_ecc_shared_secret(keyShareEntry->key, &eccpubkey, sharedSecret, &outlen);
  7103. PRIVATE_KEY_LOCK();
  7104. if (outlen != sharedSecretLen - kem->length_shared_secret) {
  7105. WOLFSSL_MSG("ECC shared secret derivation error.");
  7106. ret = BAD_FUNC_ARG;
  7107. WOLFSSL_ERROR_VERBOSE(ret);
  7108. }
  7109. }
  7110. }
  7111. if (sharedSecretLen > ENCRYPT_LEN) {
  7112. WOLFSSL_MSG("shared secret is too long.");
  7113. ret = LENGTH_ERROR;
  7114. WOLFSSL_ERROR_VERBOSE(ret);
  7115. }
  7116. if (ret == 0) {
  7117. /* Copy the shared secret to the pre-master secret pre-allocated
  7118. * buffer. */
  7119. XMEMCPY(ssl->arrays->preMasterSecret, sharedSecret, sharedSecretLen);
  7120. ssl->arrays->preMasterSz = (word32) sharedSecretLen;
  7121. }
  7122. if (sharedSecret != NULL) {
  7123. XFREE(sharedSecret, ssl->heap, DYNAMIC_TYPE_SECRET);
  7124. }
  7125. wc_ecc_free(&eccpubkey);
  7126. OQS_KEM_free(kem);
  7127. return ret;
  7128. }
  7129. #elif defined(HAVE_PQM4)
  7130. static int TLSX_KeyShare_ProcessPqc(WOLFSSL* ssl, KeyShareEntry* keyShareEntry)
  7131. {
  7132. int ret = 0;
  7133. byte* sharedSecret = NULL;
  7134. word32 sharedSecretLen = 0;
  7135. int oqs_group = 0;
  7136. int ecc_group = 0;
  7137. ecc_key eccpubkey;
  7138. word32 outlen = 0;
  7139. if (keyShareEntry->ke == NULL) {
  7140. WOLFSSL_MSG("Invalid OQS algorithm specified.");
  7141. return BAD_FUNC_ARG;
  7142. }
  7143. if (ssl->options.side == WOLFSSL_SERVER_END) {
  7144. /* I am the server, the shared secret has already been generated and
  7145. * is in keyShareEntry->ke; copy it to the pre-master secret
  7146. * pre-allocated buffer. */
  7147. if (keyShareEntry->keLen > ENCRYPT_LEN) {
  7148. WOLFSSL_MSG("shared secret is too long.");
  7149. WOLFSSL_ERROR_VERBOSE(LENGTH_ERROR);
  7150. return LENGTH_ERROR;
  7151. }
  7152. XMEMCPY(ssl->arrays->preMasterSecret, keyShareEntry->ke, keyShareEntry->keLen);
  7153. ssl->arrays->preMasterSz = keyShareEntry->keLen;
  7154. XFREE(keyShareEntry->ke, sl->heap, DYNAMIC_TYPE_SECRET);
  7155. keyShareEntry->ke = NULL;
  7156. keyShareEntry->keLen = 0;
  7157. return 0;
  7158. }
  7159. /* I am the client, the ciphertext is in keyShareEntry->ke */
  7160. findEccPqc(&ecc_group, &oqs_group, keyShareEntry->group);
  7161. sharedSecretLen = (word32)PQM4_SHARED_SECRET_LENGTH;
  7162. switch (ecc_group) {
  7163. case WOLFSSL_ECC_SECP256R1:
  7164. sharedSecretLen += 32;
  7165. outlen = 32;
  7166. break;
  7167. case WOLFSSL_ECC_SECP384R1:
  7168. sharedSecretLen += 48;
  7169. outlen = 48;
  7170. break;
  7171. case WOLFSSL_ECC_SECP521R1:
  7172. sharedSecretLen += 66;
  7173. outlen = 66;
  7174. break;
  7175. default:
  7176. break;
  7177. }
  7178. ret = wc_ecc_init_ex(&eccpubkey, ssl->heap, ssl->devId);
  7179. if (ret != 0) {
  7180. WOLFSSL_MSG("Memory allocation error.");
  7181. return MEMORY_E;
  7182. }
  7183. sharedSecret = (byte*)XMALLOC(sharedSecretLen, ssl->heap,
  7184. DYNAMIC_TYPE_TLSX);
  7185. if (sharedSecret == NULL) {
  7186. WOLFSSL_MSG("Memory allocation error.");
  7187. ret = MEMORY_E;
  7188. }
  7189. if (ret == 0 && crypto_kem_dec(sharedSecret + outlen,
  7190. keyShareEntry->ke + keyShareEntry->keLen -
  7191. PQM4_CIPHERTEXT_LENGTH,
  7192. keyShareEntry->privKey) != 0) {
  7193. WOLFSSL_MSG("PQM4 decapsulation failure.");
  7194. ret = BAD_FUNC_ARG;
  7195. } else {
  7196. WOLFSSL_MSG("PQM4 decapsulation SUCCESS!!!!!");
  7197. }
  7198. if (ecc_group != 0) {
  7199. if (ret == 0) {
  7200. /* Point is validated by import function. */
  7201. ret = wc_ecc_import_x963(keyShareEntry->ke,
  7202. keyShareEntry->keLen -
  7203. (word32)PQM4_CIPHERTEXT_LENGTH,
  7204. &eccpubkey);
  7205. if (ret != 0) {
  7206. WOLFSSL_MSG("ECC Public key import error.");
  7207. }
  7208. }
  7209. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  7210. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  7211. !defined(HAVE_SELFTEST)
  7212. if (ret == 0) {
  7213. ret = wc_ecc_set_rng(keyShareEntry->key, ssl->rng);
  7214. if (ret != 0) {
  7215. WOLFSSL_MSG("Failure to set the ECC private key RNG.");
  7216. }
  7217. }
  7218. #endif
  7219. if (ret == 0) {
  7220. PRIVATE_KEY_UNLOCK();
  7221. ret = wc_ecc_shared_secret(keyShareEntry->key, &eccpubkey, sharedSecret, &outlen);
  7222. PRIVATE_KEY_LOCK();
  7223. if (outlen != sharedSecretLen - PQM4_SHARED_SECRET_LENGTH) {
  7224. WOLFSSL_MSG("ECC shared secret derivation error.");
  7225. ret = BAD_FUNC_ARG;
  7226. }
  7227. }
  7228. }
  7229. if (sharedSecretLen > ENCRYPT_LEN) {
  7230. WOLFSSL_MSG("shared secret is too long.\n");
  7231. ret = LENGTH_ERROR;
  7232. }
  7233. if (ret == 0) {
  7234. /* Copy the shared secret to the pre-master secret pre-allocated
  7235. * buffer. */
  7236. XMEMCPY(ssl->arrays->preMasterSecret, sharedSecret, sharedSecretLen);
  7237. ssl->arrays->preMasterSz = (word32) sharedSecretLen;
  7238. }
  7239. if (sharedSecret != NULL) {
  7240. XFREE(sharedSecret, ssl->heap, DYNAMIC_TYPE_SECRET);
  7241. }
  7242. wc_ecc_free(&eccpubkey);
  7243. return ret;
  7244. }
  7245. #endif /* HAVE_PQM4 */
  7246. #endif /* HAVE_PQC */
  7247. /* Process the key share extension on the client side.
  7248. *
  7249. * ssl The SSL/TLS object.
  7250. * keyShareEntry The key share entry object to use to calculate shared secret.
  7251. * returns 0 on success and other values indicate failure.
  7252. */
  7253. static int TLSX_KeyShare_Process(WOLFSSL* ssl, KeyShareEntry* keyShareEntry)
  7254. {
  7255. int ret;
  7256. #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
  7257. ssl->session->namedGroup = (byte)keyShareEntry->group;
  7258. #endif
  7259. /* reset the pre master secret size */
  7260. if (ssl->arrays->preMasterSz == 0)
  7261. ssl->arrays->preMasterSz = ENCRYPT_LEN;
  7262. /* Use Key Share Data from server. */
  7263. if (WOLFSSL_NAMED_GROUP_IS_FFHDE(keyShareEntry->group))
  7264. ret = TLSX_KeyShare_ProcessDh(ssl, keyShareEntry);
  7265. else if (keyShareEntry->group == WOLFSSL_ECC_X25519)
  7266. ret = TLSX_KeyShare_ProcessX25519(ssl, keyShareEntry);
  7267. else if (keyShareEntry->group == WOLFSSL_ECC_X448)
  7268. ret = TLSX_KeyShare_ProcessX448(ssl, keyShareEntry);
  7269. #ifdef HAVE_PQC
  7270. else if (WOLFSSL_NAMED_GROUP_IS_PQC(keyShareEntry->group))
  7271. ret = TLSX_KeyShare_ProcessPqc(ssl, keyShareEntry);
  7272. #endif
  7273. else
  7274. ret = TLSX_KeyShare_ProcessEcc(ssl, keyShareEntry);
  7275. #ifdef WOLFSSL_DEBUG_TLS
  7276. if (ret == 0) {
  7277. WOLFSSL_MSG("KE Secret");
  7278. WOLFSSL_BUFFER(ssl->arrays->preMasterSecret, ssl->arrays->preMasterSz);
  7279. }
  7280. #endif
  7281. #ifdef WOLFSSL_ASYNC_CRYPT
  7282. keyShareEntry->lastRet = ret;
  7283. #endif
  7284. return ret;
  7285. }
  7286. /* Parse an entry of the KeyShare extension.
  7287. *
  7288. * ssl The SSL/TLS object.
  7289. * input The extension data.
  7290. * length The length of the extension data.
  7291. * kse The new key share entry object.
  7292. * returns a positive number to indicate amount of data parsed and a negative
  7293. * number on error.
  7294. */
  7295. static int TLSX_KeyShareEntry_Parse(WOLFSSL* ssl, const byte* input,
  7296. word16 length, KeyShareEntry **kse)
  7297. {
  7298. int ret;
  7299. word16 group;
  7300. word16 keLen;
  7301. int offset = 0;
  7302. byte* ke;
  7303. if (length < OPAQUE16_LEN + OPAQUE16_LEN)
  7304. return BUFFER_ERROR;
  7305. /* Named group */
  7306. ato16(&input[offset], &group);
  7307. offset += OPAQUE16_LEN;
  7308. /* Key exchange data - public key. */
  7309. ato16(&input[offset], &keLen);
  7310. offset += OPAQUE16_LEN;
  7311. if (keLen == 0)
  7312. return INVALID_PARAMETER;
  7313. if (keLen > length - offset)
  7314. return BUFFER_ERROR;
  7315. #ifdef HAVE_PQC
  7316. if (WOLFSSL_NAMED_GROUP_IS_PQC(group) &&
  7317. ssl->options.side == WOLFSSL_SERVER_END) {
  7318. /* For KEMs, the public key is not stored. Casting away const because
  7319. * we know for KEMs, it will be read-only.*/
  7320. ke = (byte *)&input[offset];
  7321. } else
  7322. #endif
  7323. {
  7324. /* Store a copy in the key share object. */
  7325. ke = (byte*)XMALLOC(keLen, ssl->heap, DYNAMIC_TYPE_PUBLIC_KEY);
  7326. if (ke == NULL)
  7327. return MEMORY_E;
  7328. XMEMCPY(ke, &input[offset], keLen);
  7329. }
  7330. /* Populate a key share object in the extension. */
  7331. ret = TLSX_KeyShare_Use(ssl, group, keLen, ke, kse);
  7332. if (ret != 0) {
  7333. if (ke != &input[offset]) {
  7334. XFREE(ke, ssl->heap, DYNAMIC_TYPE_PUBLIC_KEY);
  7335. }
  7336. return ret;
  7337. }
  7338. /* Total length of the parsed data. */
  7339. return offset + keLen;
  7340. }
  7341. /* Searches the groups sent for the specified named group.
  7342. *
  7343. * ssl SSL/TLS object.
  7344. * name Group name to match.
  7345. * returns 1 when the extension has the group name and 0 otherwise.
  7346. */
  7347. static int TLSX_KeyShare_Find(WOLFSSL* ssl, word16 group)
  7348. {
  7349. TLSX* extension;
  7350. KeyShareEntry* list;
  7351. extension = TLSX_Find(ssl->extensions, TLSX_KEY_SHARE);
  7352. if (extension == NULL) {
  7353. extension = TLSX_Find(ssl->ctx->extensions, TLSX_KEY_SHARE);
  7354. if (extension == NULL)
  7355. return 0;
  7356. }
  7357. list = (KeyShareEntry*)extension->data;
  7358. while (list != NULL) {
  7359. if (list->group == group)
  7360. return 1;
  7361. list = list->next;
  7362. }
  7363. return 0;
  7364. }
  7365. /* Searches the supported groups extension for the specified named group.
  7366. *
  7367. * ssl The SSL/TLS object.
  7368. * name The group name to match.
  7369. * returns 1 when the extension has the group name and 0 otherwise.
  7370. */
  7371. static int TLSX_SupportedGroups_Find(WOLFSSL* ssl, word16 name)
  7372. {
  7373. #ifdef HAVE_SUPPORTED_CURVES
  7374. TLSX* extension;
  7375. SupportedCurve* curve = NULL;
  7376. if ((extension = TLSX_Find(ssl->extensions,
  7377. TLSX_SUPPORTED_GROUPS)) == NULL) {
  7378. if ((extension = TLSX_Find(ssl->ctx->extensions,
  7379. TLSX_SUPPORTED_GROUPS)) == NULL) {
  7380. return 0;
  7381. }
  7382. }
  7383. for (curve = (SupportedCurve*)extension->data; curve; curve = curve->next) {
  7384. if (curve->name == name)
  7385. return 1;
  7386. }
  7387. #endif
  7388. (void)ssl;
  7389. (void)name;
  7390. return 0;
  7391. }
  7392. /* Parse the KeyShare extension.
  7393. * Different formats in different messages.
  7394. *
  7395. * ssl The SSL/TLS object.
  7396. * input The extension data.
  7397. * length The length of the extension data.
  7398. * msgType The type of the message this extension is being parsed from.
  7399. * returns 0 on success and other values indicate failure.
  7400. */
  7401. static int TLSX_KeyShare_Parse(WOLFSSL* ssl, const byte* input, word16 length,
  7402. byte msgType)
  7403. {
  7404. int ret;
  7405. KeyShareEntry *keyShareEntry = NULL;
  7406. word16 group;
  7407. if (msgType == client_hello) {
  7408. int offset = 0;
  7409. word16 len;
  7410. TLSX* extension;
  7411. /* Add a KeyShare extension if it doesn't exist. */
  7412. extension = TLSX_Find(ssl->extensions, TLSX_KEY_SHARE);
  7413. if (extension == NULL) {
  7414. /* Push new KeyShare extension. */
  7415. ret = TLSX_Push(&ssl->extensions, TLSX_KEY_SHARE, NULL, ssl->heap);
  7416. if (ret != 0)
  7417. return ret;
  7418. }
  7419. if (length < OPAQUE16_LEN)
  7420. return BUFFER_ERROR;
  7421. /* ClientHello contains zero or more key share entries. */
  7422. ato16(input, &len);
  7423. if (len != length - OPAQUE16_LEN)
  7424. return BUFFER_ERROR;
  7425. offset += OPAQUE16_LEN;
  7426. while (offset < (int)length) {
  7427. ret = TLSX_KeyShareEntry_Parse(ssl, &input[offset],
  7428. length - (word16)offset,
  7429. &keyShareEntry);
  7430. if (ret < 0)
  7431. return ret;
  7432. offset += ret;
  7433. }
  7434. ret = 0;
  7435. }
  7436. else if (msgType == server_hello) {
  7437. int len;
  7438. if (length < OPAQUE16_LEN)
  7439. return BUFFER_ERROR;
  7440. /* The data is the named group the server wants to use. */
  7441. ato16(input, &group);
  7442. /* Check the selected group was supported by ClientHello extensions. */
  7443. if (!TLSX_SupportedGroups_Find(ssl, group)) {
  7444. WOLFSSL_ERROR_VERBOSE(BAD_KEY_SHARE_DATA);
  7445. return BAD_KEY_SHARE_DATA;
  7446. }
  7447. /* Check if the group was sent. */
  7448. if (!TLSX_KeyShare_Find(ssl, group)) {
  7449. WOLFSSL_ERROR_VERBOSE(BAD_KEY_SHARE_DATA);
  7450. return BAD_KEY_SHARE_DATA;
  7451. }
  7452. /* ServerHello contains one key share entry. */
  7453. len = TLSX_KeyShareEntry_Parse(ssl, input, length, &keyShareEntry);
  7454. if (len != (int)length)
  7455. return BUFFER_ERROR;
  7456. /* Not in list sent if there isn't a private key. */
  7457. if (keyShareEntry == NULL || (keyShareEntry->key == NULL
  7458. #if !defined(NO_DH) || defined(HAVE_PQC)
  7459. && keyShareEntry->privKey == NULL
  7460. #endif
  7461. )) {
  7462. WOLFSSL_ERROR_VERBOSE(BAD_KEY_SHARE_DATA);
  7463. return BAD_KEY_SHARE_DATA;
  7464. }
  7465. /* Process the entry to calculate the secret. */
  7466. ret = TLSX_KeyShare_Process(ssl, keyShareEntry);
  7467. if (ret == 0)
  7468. ssl->session->namedGroup = ssl->namedGroup = group;
  7469. }
  7470. else if (msgType == hello_retry_request) {
  7471. if (length != OPAQUE16_LEN)
  7472. return BUFFER_ERROR;
  7473. /* The data is the named group the server wants to use. */
  7474. ato16(input, &group);
  7475. #ifdef WOLFSSL_ASYNC_CRYPT
  7476. /* only perform find and clear TLSX if not returning from async */
  7477. if (ssl->error != WC_PENDING_E)
  7478. #endif
  7479. {
  7480. /* Check the selected group was supported by ClientHello extensions. */
  7481. if (!TLSX_SupportedGroups_Find(ssl, group)) {
  7482. WOLFSSL_ERROR_VERBOSE(BAD_KEY_SHARE_DATA);
  7483. return BAD_KEY_SHARE_DATA;
  7484. }
  7485. /* Check if the group was sent. */
  7486. if (TLSX_KeyShare_Find(ssl, group)) {
  7487. WOLFSSL_ERROR_VERBOSE(BAD_KEY_SHARE_DATA);
  7488. return BAD_KEY_SHARE_DATA;
  7489. }
  7490. /* Clear out unusable key shares. */
  7491. ret = TLSX_KeyShare_Empty(ssl);
  7492. if (ret != 0)
  7493. return ret;
  7494. }
  7495. #ifdef HAVE_PQC
  7496. /* For post-quantum groups, do this in TLSX_PopulateExtensions(). */
  7497. if (!WOLFSSL_NAMED_GROUP_IS_PQC(group))
  7498. #endif
  7499. ret = TLSX_KeyShare_Use(ssl, group, 0, NULL, NULL);
  7500. }
  7501. else {
  7502. /* Not a message type that is allowed to have this extension. */
  7503. WOLFSSL_ERROR_VERBOSE(SANITY_MSG_E);
  7504. return SANITY_MSG_E;
  7505. }
  7506. return ret;
  7507. }
  7508. /* Create a new key share entry and put it into the list.
  7509. *
  7510. * list The linked list of key share entries.
  7511. * group The named group.
  7512. * heap The memory to allocate with.
  7513. * keyShareEntry The new key share entry object.
  7514. * returns 0 on success and other values indicate failure.
  7515. */
  7516. static int TLSX_KeyShare_New(KeyShareEntry** list, int group, void *heap,
  7517. KeyShareEntry** keyShareEntry)
  7518. {
  7519. KeyShareEntry* kse;
  7520. KeyShareEntry** next;
  7521. kse = (KeyShareEntry*)XMALLOC(sizeof(KeyShareEntry), heap,
  7522. DYNAMIC_TYPE_TLSX);
  7523. if (kse == NULL)
  7524. return MEMORY_E;
  7525. XMEMSET(kse, 0, sizeof(*kse));
  7526. kse->group = (word16)group;
  7527. /* Add it to the back and maintain the links. */
  7528. while (*list != NULL) {
  7529. /* Assign to temporary to work around compiler bug found by customer. */
  7530. next = &((*list)->next);
  7531. list = next;
  7532. }
  7533. *list = kse;
  7534. *keyShareEntry = kse;
  7535. (void)heap;
  7536. return 0;
  7537. }
  7538. #ifdef HAVE_PQC
  7539. #ifdef WOLFSSL_WC_KYBER
  7540. static int server_generate_pqc_ciphertext(WOLFSSL* ssl,
  7541. KeyShareEntry* keyShareEntry, byte* data, word16 len)
  7542. {
  7543. /* I am the server. The data parameter is the client's public key. I need
  7544. * to generate the public information (AKA ciphertext) and shared secret
  7545. * here. Note the "public information" is equivalent to a the public key in
  7546. * key exchange parlance. That's why it is being assigned to pubKey.
  7547. */
  7548. int type;
  7549. KyberKey kem[1];
  7550. byte* sharedSecret = NULL;
  7551. byte* ciphertext = NULL;
  7552. int ret = 0;
  7553. int oqs_group = 0;
  7554. int ecc_group = 0;
  7555. KeyShareEntry *ecc_kse = NULL;
  7556. ecc_key eccpubkey;
  7557. word32 outlen = 0;
  7558. word32 pubSz = 0;
  7559. word32 ctSz = 0;
  7560. word32 ssSz = 0;
  7561. findEccPqc(&ecc_group, &oqs_group, keyShareEntry->group);
  7562. ret = kyber_id2type(oqs_group, &type);
  7563. if (ret != 0) {
  7564. WOLFSSL_MSG("Invalid Kyber algorithm specified.");
  7565. ret = BAD_FUNC_ARG;
  7566. }
  7567. if (ret == 0) {
  7568. ret = wc_ecc_init_ex(&eccpubkey, ssl->heap, ssl->devId);
  7569. if (ret != 0) {
  7570. WOLFSSL_MSG("Could not do ECC public key initialization.");
  7571. ret = MEMORY_E;
  7572. }
  7573. }
  7574. if (ret == 0) {
  7575. ecc_kse = (KeyShareEntry*)XMALLOC(sizeof(*ecc_kse), ssl->heap,
  7576. DYNAMIC_TYPE_TLSX);
  7577. if (ecc_kse == NULL) {
  7578. WOLFSSL_MSG("ecc_kse memory allocation failure");
  7579. ret = MEMORY_ERROR;
  7580. }
  7581. }
  7582. if (ret == 0) {
  7583. XMEMSET(ecc_kse, 0, sizeof(*ecc_kse));
  7584. }
  7585. if (ret == 0 && ecc_group != 0) {
  7586. ecc_kse->group = ecc_group;
  7587. ret = TLSX_KeyShare_GenEccKey(ssl, ecc_kse);
  7588. if (ret != 0) {
  7589. /* No message, TLSX_KeyShare_GenEccKey() will do it. */
  7590. return ret;
  7591. }
  7592. ret = 0;
  7593. }
  7594. if (ret == 0) {
  7595. ret = wc_KyberKey_Init(type, kem, ssl->heap, INVALID_DEVID);
  7596. if (ret == 0) {
  7597. WOLFSSL_MSG("Error creating Kyber KEM");
  7598. }
  7599. }
  7600. if (ret == 0) {
  7601. ret = wc_KyberKey_PublicKeySize(kem, &pubSz);
  7602. }
  7603. if (ret == 0) {
  7604. ret = wc_KyberKey_CipherTextSize(kem, &ctSz);
  7605. }
  7606. if (ret == 0) {
  7607. ret = wc_KyberKey_SharedSecretSize(kem, &ssSz);
  7608. }
  7609. if (ret == 0 && len != pubSz + ecc_kse->pubKeyLen) {
  7610. WOLFSSL_MSG("Invalid public key.");
  7611. ret = BAD_FUNC_ARG;
  7612. }
  7613. if (ret == 0) {
  7614. sharedSecret = (byte*)XMALLOC(ecc_kse->keyLen + ssSz, ssl->heap,
  7615. DYNAMIC_TYPE_TLSX);
  7616. ciphertext = (byte*)XMALLOC(ecc_kse->pubKeyLen + ctSz, ssl->heap,
  7617. DYNAMIC_TYPE_TLSX);
  7618. if (sharedSecret == NULL || ciphertext == NULL) {
  7619. WOLFSSL_MSG("Ciphertext/shared secret memory allocation failure.");
  7620. ret = MEMORY_E;
  7621. }
  7622. }
  7623. if (ecc_group != 0) {
  7624. if (ret == 0) {
  7625. /* Point is validated by import function. */
  7626. ret = wc_ecc_import_x963(data, len - pubSz, &eccpubkey);
  7627. if (ret != 0) {
  7628. WOLFSSL_MSG("Bad ECC public key.");
  7629. }
  7630. }
  7631. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  7632. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  7633. !defined(HAVE_SELFTEST)
  7634. if (ret == 0) {
  7635. ret = wc_ecc_set_rng(ecc_kse->key, ssl->rng);
  7636. }
  7637. #endif
  7638. if (ret == 0) {
  7639. outlen = ecc_kse->keyLen;
  7640. PRIVATE_KEY_UNLOCK();
  7641. ret = wc_ecc_shared_secret(ecc_kse->key, &eccpubkey,
  7642. sharedSecret,
  7643. &outlen);
  7644. PRIVATE_KEY_LOCK();
  7645. if (outlen != ecc_kse->keyLen) {
  7646. WOLFSSL_MSG("Data length mismatch.");
  7647. ret = BAD_FUNC_ARG;
  7648. }
  7649. }
  7650. }
  7651. if (ret == 0) {
  7652. ret = wc_KyberKey_DecodePublicKey(kem, data + ecc_kse->pubKeyLen,
  7653. pubSz);
  7654. }
  7655. if (ret == 0) {
  7656. ret = wc_KyberKey_Encapsulate(kem, ciphertext + ecc_kse->pubKeyLen,
  7657. sharedSecret + outlen, ssl->rng);
  7658. if (ret != 0) {
  7659. WOLFSSL_MSG("wc_KyberKey encapsulation failure.");
  7660. }
  7661. }
  7662. if (ret == 0) {
  7663. if (keyShareEntry->ke != NULL) {
  7664. XFREE(keyShareEntry->ke, ssl->heap, DYNAMIC_TYPE_PUBLIC_KEY);
  7665. }
  7666. keyShareEntry->ke = sharedSecret;
  7667. keyShareEntry->keLen = outlen + ssSz;
  7668. sharedSecret = NULL;
  7669. XMEMCPY(ciphertext, ecc_kse->pubKey, ecc_kse->pubKeyLen);
  7670. keyShareEntry->pubKey = ciphertext;
  7671. keyShareEntry->pubKeyLen = (word32)(ecc_kse->pubKeyLen + ctSz);
  7672. ciphertext = NULL;
  7673. }
  7674. TLSX_KeyShare_FreeAll(ecc_kse, ssl->heap);
  7675. if (sharedSecret != NULL)
  7676. XFREE(sharedSecret, ssl->heap, DYNAMIC_TYPE_TLSX);
  7677. if (ciphertext != NULL)
  7678. XFREE(ciphertext, ssl->heap, DYNAMIC_TYPE_TLSX);
  7679. wc_ecc_free(&eccpubkey);
  7680. wc_KyberKey_Free(kem);
  7681. return ret;
  7682. }
  7683. #elif defined(HAVE_LIBOQS)
  7684. static int server_generate_pqc_ciphertext(WOLFSSL* ssl,
  7685. KeyShareEntry* keyShareEntry,
  7686. byte* data, word16 len)
  7687. {
  7688. /* I am the server. The data parameter is the client's public key. I need
  7689. * to generate the public information (AKA ciphertext) and shared secret
  7690. * here. Note the "public information" is equivalent to a the public key in
  7691. * key exchange parlance. That's why it is being assigned to pubKey.
  7692. */
  7693. const char* algName = NULL;
  7694. OQS_KEM* kem = NULL;
  7695. byte* sharedSecret = NULL;
  7696. byte* ciphertext = NULL;
  7697. int ret = 0;
  7698. int oqs_group = 0;
  7699. int ecc_group = 0;
  7700. KeyShareEntry *ecc_kse = NULL;
  7701. ecc_key eccpubkey;
  7702. word32 outlen = 0;
  7703. findEccPqc(&ecc_group, &oqs_group, keyShareEntry->group);
  7704. algName = OQS_ID2name(oqs_group);
  7705. if (algName == NULL) {
  7706. WOLFSSL_MSG("Invalid OQS algorithm specified.");
  7707. return BAD_FUNC_ARG;
  7708. }
  7709. ret = wc_ecc_init_ex(&eccpubkey, ssl->heap, ssl->devId);
  7710. if (ret != 0) {
  7711. WOLFSSL_MSG("Could not do ECC public key initialization.");
  7712. return MEMORY_E;
  7713. }
  7714. ecc_kse = (KeyShareEntry*)XMALLOC(sizeof(*ecc_kse), ssl->heap, DYNAMIC_TYPE_TLSX);
  7715. if (ecc_kse == NULL) {
  7716. WOLFSSL_MSG("ecc_kse memory allocation failure");
  7717. ret = MEMORY_ERROR;
  7718. }
  7719. if (ret == 0) {
  7720. XMEMSET(ecc_kse, 0, sizeof(*ecc_kse));
  7721. }
  7722. if (ret == 0 && ecc_group != 0) {
  7723. ecc_kse->group = ecc_group;
  7724. ret = TLSX_KeyShare_GenEccKey(ssl, ecc_kse);
  7725. if (ret != 0) {
  7726. /* No message, TLSX_KeyShare_GenEccKey() will do it. */
  7727. return ret;
  7728. }
  7729. ret = 0;
  7730. }
  7731. if (ret == 0) {
  7732. kem = OQS_KEM_new(algName);
  7733. if (kem == NULL) {
  7734. WOLFSSL_MSG("Error creating OQS KEM, ensure algorithm support "
  7735. "was enabled in liboqs.");
  7736. ret = MEMORY_E;
  7737. }
  7738. }
  7739. if (ret == 0 && len != kem->length_public_key + ecc_kse->pubKeyLen) {
  7740. WOLFSSL_MSG("Invalid public key.");
  7741. WOLFSSL_ERROR_VERBOSE(BAD_FUNC_ARG);
  7742. ret = BAD_FUNC_ARG;
  7743. }
  7744. if (ret == 0) {
  7745. sharedSecret = (byte*)XMALLOC(ecc_kse->keyLen +
  7746. kem->length_shared_secret,
  7747. ssl->heap, DYNAMIC_TYPE_TLSX);
  7748. ciphertext = (byte*)XMALLOC(ecc_kse->pubKeyLen + kem->length_ciphertext,
  7749. ssl->heap, DYNAMIC_TYPE_TLSX);
  7750. if (sharedSecret == NULL || ciphertext == NULL) {
  7751. WOLFSSL_MSG("Ciphertext/shared secret memory allocation failure.");
  7752. ret = MEMORY_E;
  7753. }
  7754. }
  7755. if (ecc_group != 0) {
  7756. if (ret == 0) {
  7757. /* Point is validated by import function. */
  7758. ret = wc_ecc_import_x963(data, len - (word32)kem->length_public_key,
  7759. &eccpubkey);
  7760. if (ret != 0) {
  7761. WOLFSSL_MSG("Bad ECC public key.");
  7762. }
  7763. }
  7764. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  7765. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  7766. !defined(HAVE_SELFTEST)
  7767. if (ret == 0) {
  7768. ret = wc_ecc_set_rng(ecc_kse->key, ssl->rng);
  7769. }
  7770. #endif
  7771. if (ret == 0) {
  7772. outlen = ecc_kse->keyLen;
  7773. PRIVATE_KEY_UNLOCK();
  7774. ret = wc_ecc_shared_secret(ecc_kse->key, &eccpubkey,
  7775. sharedSecret,
  7776. &outlen);
  7777. PRIVATE_KEY_LOCK();
  7778. if (outlen != ecc_kse->keyLen) {
  7779. WOLFSSL_MSG("Data length mismatch.");
  7780. ret = BAD_FUNC_ARG;
  7781. }
  7782. }
  7783. }
  7784. if (ret == 0 &&
  7785. OQS_KEM_encaps(kem, ciphertext + ecc_kse->pubKeyLen,
  7786. sharedSecret + outlen,
  7787. data + ecc_kse->pubKeyLen) != OQS_SUCCESS) {
  7788. WOLFSSL_MSG("OQS Encapsulation failure.");
  7789. ret = BAD_FUNC_ARG;
  7790. }
  7791. if (ret == 0) {
  7792. if (keyShareEntry->ke != NULL) {
  7793. XFREE(keyShareEntry->ke, ssl->heap, DYNAMIC_TYPE_PUBLIC_KEY);
  7794. }
  7795. keyShareEntry->ke = sharedSecret;
  7796. keyShareEntry->keLen = outlen + (word32)kem->length_shared_secret;
  7797. sharedSecret = NULL;
  7798. XMEMCPY(ciphertext, ecc_kse->pubKey, ecc_kse->pubKeyLen);
  7799. keyShareEntry->pubKey = ciphertext;
  7800. keyShareEntry->pubKeyLen = (word32)(ecc_kse->pubKeyLen +
  7801. kem->length_ciphertext);
  7802. ciphertext = NULL;
  7803. /* Set namedGroup so wolfSSL_get_curve_name() can function properly on
  7804. * the server side. */
  7805. ssl->namedGroup = keyShareEntry->group;
  7806. }
  7807. TLSX_KeyShare_FreeAll(ecc_kse, ssl->heap);
  7808. if (sharedSecret != NULL)
  7809. XFREE(sharedSecret, ssl->heap, DYNAMIC_TYPE_TLSX);
  7810. if (ciphertext != NULL)
  7811. XFREE(ciphertext, ssl->heap, DYNAMIC_TYPE_TLSX);
  7812. wc_ecc_free(&eccpubkey);
  7813. OQS_KEM_free(kem);
  7814. return ret;
  7815. }
  7816. #elif defined(HAVE_PQM4)
  7817. static int server_generate_pqc_ciphertext(WOLFSSL* ssl,
  7818. KeyShareEntry* keyShareEntry,
  7819. byte* data, word16 len)
  7820. {
  7821. /* I am the server. The data parameter is the client's public key. I need
  7822. * to generate the public information (AKA ciphertext) and shared secret
  7823. * here. Note the "public information" is equivalent to a the public key in
  7824. * key exchange parlance. That's why it is being assigned to pubKey.
  7825. */
  7826. byte* sharedSecret = NULL;
  7827. byte* ciphertext = NULL;
  7828. int ret = 0;
  7829. int oqs_group = 0;
  7830. int ecc_group = 0;
  7831. KeyShareEntry *ecc_kse = NULL;
  7832. ecc_key eccpubkey;
  7833. word32 outlen = 0;
  7834. findEccPqc(&ecc_group, &oqs_group, keyShareEntry->group);
  7835. ret = wc_ecc_init_ex(&eccpubkey, ssl->heap, ssl->devId);
  7836. if (ret != 0) {
  7837. WOLFSSL_MSG("Could not do ECC public key initialization.");
  7838. return MEMORY_E;
  7839. }
  7840. ecc_kse = (KeyShareEntry*)XMALLOC(sizeof(*ecc_kse), ssl->heap, DYNAMIC_TYPE_TLSX);
  7841. if (ecc_kse == NULL) {
  7842. WOLFSSL_MSG("ecc_kse memory allocation failure");
  7843. ret = MEMORY_ERROR;
  7844. }
  7845. if (ret == 0) {
  7846. XMEMSET(ecc_kse, 0, sizeof(*ecc_kse));
  7847. }
  7848. if (ret == 0 && ecc_group != 0) {
  7849. ecc_kse->group = ecc_group;
  7850. ret = TLSX_KeyShare_GenEccKey(ssl, ecc_kse);
  7851. if (ret != 0) {
  7852. /* No message, TLSX_KeyShare_GenEccKey() will do it. */
  7853. return ret;
  7854. }
  7855. ret = 0;
  7856. }
  7857. if (ret == 0 && len != PQM4_PUBLIC_KEY_LENGTH + ecc_kse->pubKeyLen) {
  7858. WOLFSSL_MSG("Invalid public key.");
  7859. ret = BAD_FUNC_ARG;
  7860. }
  7861. if (ret == 0) {
  7862. sharedSecret = (byte*)XMALLOC(ecc_kse->keyLen + PQM4_SHARED_SECRET_LENGTH,
  7863. ssl->heap, DYNAMIC_TYPE_TLSX);
  7864. ciphertext = (byte*)XMALLOC(ecc_kse->pubKeyLen + PQM4_CIPHERTEXT_LENGTH,
  7865. ssl->heap, DYNAMIC_TYPE_TLSX);
  7866. if (sharedSecret == NULL || ciphertext == NULL) {
  7867. WOLFSSL_MSG("Ciphertext/shared secret memory allocation failure.");
  7868. ret = MEMORY_E;
  7869. }
  7870. }
  7871. if (ecc_group != 0) {
  7872. if (ret == 0) {
  7873. /* Point is validated by import function. */
  7874. ret = wc_ecc_import_x963(data, len - PQM4_PUBLIC_KEY_LENGTH,
  7875. &eccpubkey);
  7876. if (ret != 0) {
  7877. WOLFSSL_MSG("Bad ECC public key.");
  7878. }
  7879. }
  7880. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  7881. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  7882. !defined(HAVE_SELFTEST)
  7883. if (ret == 0) {
  7884. ret = wc_ecc_set_rng(ecc_kse->key, ssl->rng);
  7885. }
  7886. #endif
  7887. if (ret == 0) {
  7888. outlen = ecc_kse->keyLen;
  7889. PRIVATE_KEY_UNLOCK();
  7890. ret = wc_ecc_shared_secret(ecc_kse->key, &eccpubkey,
  7891. sharedSecret,
  7892. &outlen);
  7893. PRIVATE_KEY_LOCK();
  7894. if (outlen != ecc_kse->keyLen) {
  7895. WOLFSSL_MSG("Data length mismatch.");
  7896. ret = BAD_FUNC_ARG;
  7897. }
  7898. }
  7899. }
  7900. if (ret == 0 &&
  7901. crypto_kem_enc(ciphertext + ecc_kse->pubKeyLen,
  7902. sharedSecret + outlen,
  7903. data + ecc_kse->pubKeyLen) != 0) {
  7904. WOLFSSL_MSG("PQM4 Encapsulation failure.");
  7905. ret = BAD_FUNC_ARG;
  7906. }
  7907. if (ret == 0) {
  7908. if (keyShareEntry->ke != NULL) {
  7909. XFREE(keyShareEntry->ke, ssl->heap, DYNAMIC_TYPE_PUBLIC_KEY);
  7910. }
  7911. keyShareEntry->ke = sharedSecret;
  7912. keyShareEntry->keLen = outlen + (word32)PQM4_SHARED_SECRET_LENGTH;
  7913. sharedSecret = NULL;
  7914. XMEMCPY(ciphertext, ecc_kse->pubKey, ecc_kse->pubKeyLen);
  7915. keyShareEntry->pubKey = ciphertext;
  7916. keyShareEntry->pubKeyLen = (word32)(ecc_kse->pubKeyLen +
  7917. PQM4_CIPHERTEXT_LENGTH);
  7918. ciphertext = NULL;
  7919. /* Set namedGroup so wolfSSL_get_curve_name() can function properly on
  7920. * the server side. */
  7921. ssl->namedGroup = keyShareEntry->group;
  7922. }
  7923. TLSX_KeyShare_FreeAll(ecc_kse, ssl->heap);
  7924. if (sharedSecret != NULL)
  7925. XFREE(sharedSecret, ssl->heap, DYNAMIC_TYPE_TLSX);
  7926. if (ciphertext != NULL)
  7927. XFREE(ciphertext, ssl->heap, DYNAMIC_TYPE_TLSX);
  7928. wc_ecc_free(&eccpubkey);
  7929. return ret;
  7930. }
  7931. #endif /* HAVE_PQM4 */
  7932. #endif /* HAVE_PQC */
  7933. /* Use the data to create a new key share object in the extensions.
  7934. *
  7935. * ssl The SSL/TLS object.
  7936. * group The named group.
  7937. * len The length of the public key data.
  7938. * data The public key data.
  7939. * kse The new key share entry object.
  7940. * returns 0 on success and other values indicate failure.
  7941. */
  7942. int TLSX_KeyShare_Use(WOLFSSL* ssl, word16 group, word16 len, byte* data,
  7943. KeyShareEntry **kse)
  7944. {
  7945. int ret = 0;
  7946. TLSX* extension;
  7947. KeyShareEntry* keyShareEntry = NULL;
  7948. /* Find the KeyShare extension if it exists. */
  7949. extension = TLSX_Find(ssl->extensions, TLSX_KEY_SHARE);
  7950. if (extension == NULL) {
  7951. /* Push new KeyShare extension. */
  7952. ret = TLSX_Push(&ssl->extensions, TLSX_KEY_SHARE, NULL, ssl->heap);
  7953. if (ret != 0)
  7954. return ret;
  7955. extension = TLSX_Find(ssl->extensions, TLSX_KEY_SHARE);
  7956. if (extension == NULL)
  7957. return MEMORY_E;
  7958. }
  7959. extension->resp = 0;
  7960. /* Try to find the key share entry with this group. */
  7961. keyShareEntry = (KeyShareEntry*)extension->data;
  7962. while (keyShareEntry != NULL) {
  7963. if (keyShareEntry->group == group)
  7964. break;
  7965. keyShareEntry = keyShareEntry->next;
  7966. }
  7967. /* Create a new key share entry if not found. */
  7968. if (keyShareEntry == NULL) {
  7969. ret = TLSX_KeyShare_New((KeyShareEntry**)&extension->data, group,
  7970. ssl->heap, &keyShareEntry);
  7971. if (ret != 0)
  7972. return ret;
  7973. }
  7974. #ifdef HAVE_PQC
  7975. if (WOLFSSL_NAMED_GROUP_IS_PQC(group) &&
  7976. ssl->options.side == WOLFSSL_SERVER_END) {
  7977. ret = server_generate_pqc_ciphertext(ssl, keyShareEntry, data,
  7978. len);
  7979. if (ret != 0)
  7980. return ret;
  7981. }
  7982. else
  7983. #endif
  7984. if (data != NULL) {
  7985. if (keyShareEntry->ke != NULL) {
  7986. XFREE(keyShareEntry->ke, ssl->heap, DYNAMIC_TYPE_PUBLIC_KEY);
  7987. }
  7988. keyShareEntry->ke = data;
  7989. keyShareEntry->keLen = len;
  7990. }
  7991. else {
  7992. /* Generate a key pair. */
  7993. ret = TLSX_KeyShare_GenKey(ssl, keyShareEntry);
  7994. if (ret != 0)
  7995. return ret;
  7996. }
  7997. if (kse != NULL)
  7998. *kse = keyShareEntry;
  7999. return 0;
  8000. }
  8001. /* Set an empty Key Share extension.
  8002. *
  8003. * ssl The SSL/TLS object.
  8004. * returns 0 on success and other values indicate failure.
  8005. */
  8006. int TLSX_KeyShare_Empty(WOLFSSL* ssl)
  8007. {
  8008. int ret = 0;
  8009. TLSX* extension;
  8010. /* Find the KeyShare extension if it exists. */
  8011. extension = TLSX_Find(ssl->extensions, TLSX_KEY_SHARE);
  8012. if (extension == NULL) {
  8013. /* Push new KeyShare extension. */
  8014. ret = TLSX_Push(&ssl->extensions, TLSX_KEY_SHARE, NULL, ssl->heap);
  8015. }
  8016. else if (extension->data != NULL) {
  8017. TLSX_KeyShare_FreeAll((KeyShareEntry*)extension->data, ssl->heap);
  8018. extension->data = NULL;
  8019. }
  8020. return ret;
  8021. }
  8022. /* Returns whether this group is supported.
  8023. *
  8024. * namedGroup The named group to check.
  8025. * returns 1 when supported or 0 otherwise.
  8026. */
  8027. static int TLSX_KeyShare_IsSupported(int namedGroup)
  8028. {
  8029. switch (namedGroup) {
  8030. #ifdef HAVE_FFDHE_2048
  8031. case WOLFSSL_FFDHE_2048:
  8032. break;
  8033. #endif
  8034. #ifdef HAVE_FFDHE_3072
  8035. case WOLFSSL_FFDHE_3072:
  8036. break;
  8037. #endif
  8038. #ifdef HAVE_FFDHE_4096
  8039. case WOLFSSL_FFDHE_4096:
  8040. break;
  8041. #endif
  8042. #ifdef HAVE_FFDHE_6144
  8043. case WOLFSSL_FFDHE_6144:
  8044. break;
  8045. #endif
  8046. #ifdef HAVE_FFDHE_8192
  8047. case WOLFSSL_FFDHE_8192:
  8048. break;
  8049. #endif
  8050. #if (!defined(NO_ECC256) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 256
  8051. #ifdef HAVE_ECC_KOBLITZ
  8052. case WOLFSSL_ECC_SECP256K1:
  8053. break;
  8054. #endif
  8055. #ifndef NO_ECC_SECP
  8056. case WOLFSSL_ECC_SECP256R1:
  8057. break;
  8058. #endif /* !NO_ECC_SECP */
  8059. #ifdef HAVE_ECC_BRAINPOOL
  8060. case WOLFSSL_ECC_BRAINPOOLP256R1:
  8061. break;
  8062. #endif
  8063. #endif
  8064. #if defined(HAVE_CURVE25519) && ECC_MIN_KEY_SZ <= 256
  8065. case WOLFSSL_ECC_X25519:
  8066. break;
  8067. #endif
  8068. #if defined(HAVE_CURVE448) && ECC_MIN_KEY_SZ <= 448
  8069. case WOLFSSL_ECC_X448:
  8070. break;
  8071. #endif
  8072. #if (defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 384
  8073. #ifndef NO_ECC_SECP
  8074. case WOLFSSL_ECC_SECP384R1:
  8075. break;
  8076. #endif /* !NO_ECC_SECP */
  8077. #ifdef HAVE_ECC_BRAINPOOL
  8078. case WOLFSSL_ECC_BRAINPOOLP384R1:
  8079. break;
  8080. #endif
  8081. #endif
  8082. #if (defined(HAVE_ECC521) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 521
  8083. #ifndef NO_ECC_SECP
  8084. case WOLFSSL_ECC_SECP521R1:
  8085. break;
  8086. #endif /* !NO_ECC_SECP */
  8087. #endif
  8088. #if (defined(HAVE_ECC160) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 160
  8089. #ifdef HAVE_ECC_KOBLITZ
  8090. case WOLFSSL_ECC_SECP160K1:
  8091. break;
  8092. #endif
  8093. #ifndef NO_ECC_SECP
  8094. case WOLFSSL_ECC_SECP160R1:
  8095. break;
  8096. #endif
  8097. #ifdef HAVE_ECC_SECPR2
  8098. case WOLFSSL_ECC_SECP160R2:
  8099. break;
  8100. #endif
  8101. #endif
  8102. #if (defined(HAVE_ECC192) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 192
  8103. #ifdef HAVE_ECC_KOBLITZ
  8104. case WOLFSSL_ECC_SECP192K1:
  8105. break;
  8106. #endif
  8107. #ifndef NO_ECC_SECP
  8108. case WOLFSSL_ECC_SECP192R1:
  8109. break;
  8110. #endif
  8111. #endif
  8112. #if (defined(HAVE_ECC224) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 224
  8113. #ifdef HAVE_ECC_KOBLITZ
  8114. case WOLFSSL_ECC_SECP224K1:
  8115. break;
  8116. #endif
  8117. #ifndef NO_ECC_SECP
  8118. case WOLFSSL_ECC_SECP224R1:
  8119. break;
  8120. #endif
  8121. #endif
  8122. #if (defined(HAVE_ECC512) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 512
  8123. #ifdef HAVE_ECC_BRAINPOOL
  8124. case WOLFSSL_ECC_BRAINPOOLP512R1:
  8125. break;
  8126. #endif
  8127. #endif
  8128. #ifdef HAVE_PQC
  8129. #ifdef WOLFSSL_WC_KYBER
  8130. #ifdef WOLFSSL_KYBER512
  8131. case WOLFSSL_KYBER_LEVEL1:
  8132. #endif
  8133. #ifdef WOLFSSL_KYBER768
  8134. case WOLFSSL_KYBER_LEVEL3:
  8135. #endif
  8136. #ifdef WOLFSSL_KYBER1024
  8137. case WOLFSSL_KYBER_LEVEL5:
  8138. #endif
  8139. break;
  8140. #elif defined(HAVE_LIBOQS)
  8141. case WOLFSSL_KYBER_LEVEL1:
  8142. case WOLFSSL_KYBER_LEVEL3:
  8143. case WOLFSSL_KYBER_LEVEL5:
  8144. case WOLFSSL_NTRU_HPS_LEVEL1:
  8145. case WOLFSSL_NTRU_HPS_LEVEL3:
  8146. case WOLFSSL_NTRU_HPS_LEVEL5:
  8147. case WOLFSSL_NTRU_HRSS_LEVEL3:
  8148. case WOLFSSL_SABER_LEVEL1:
  8149. case WOLFSSL_SABER_LEVEL3:
  8150. case WOLFSSL_SABER_LEVEL5:
  8151. case WOLFSSL_KYBER_90S_LEVEL1:
  8152. case WOLFSSL_KYBER_90S_LEVEL3:
  8153. case WOLFSSL_KYBER_90S_LEVEL5:
  8154. case WOLFSSL_P256_NTRU_HPS_LEVEL1:
  8155. case WOLFSSL_P384_NTRU_HPS_LEVEL3:
  8156. case WOLFSSL_P521_NTRU_HPS_LEVEL5:
  8157. case WOLFSSL_P384_NTRU_HRSS_LEVEL3:
  8158. case WOLFSSL_P256_SABER_LEVEL1:
  8159. case WOLFSSL_P384_SABER_LEVEL3:
  8160. case WOLFSSL_P521_SABER_LEVEL5:
  8161. case WOLFSSL_P256_KYBER_LEVEL1:
  8162. case WOLFSSL_P384_KYBER_LEVEL3:
  8163. case WOLFSSL_P521_KYBER_LEVEL5:
  8164. case WOLFSSL_P256_KYBER_90S_LEVEL1:
  8165. case WOLFSSL_P384_KYBER_90S_LEVEL3:
  8166. case WOLFSSL_P521_KYBER_90S_LEVEL5:
  8167. findEccPqc(NULL, &namedGroup, namedGroup);
  8168. if (! OQS_KEM_alg_is_enabled(OQS_ID2name(namedGroup))) {
  8169. return 0;
  8170. }
  8171. break;
  8172. #elif defined(HAVE_PQM4)
  8173. case WOLFSSL_KYBER_LEVEL1:
  8174. break;
  8175. #endif
  8176. #endif /* HAVE_PQC */
  8177. default:
  8178. return 0;
  8179. }
  8180. return 1;
  8181. }
  8182. /* Examines the application specified group ranking and returns the rank of the
  8183. * group.
  8184. * If no group ranking set then all groups are rank 0 (highest).
  8185. *
  8186. * ssl The SSL/TLS object.
  8187. * group The group to check ranking for.
  8188. * returns ranking from 0 to MAX_GROUP_COUNT-1 or -1 when group not in list.
  8189. */
  8190. static int TLSX_KeyShare_GroupRank(WOLFSSL* ssl, int group)
  8191. {
  8192. byte i;
  8193. if (ssl->numGroups == 0) {
  8194. #if defined(HAVE_ECC) && defined(HAVE_SUPPORTED_CURVES)
  8195. #if (!defined(NO_ECC256) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 256
  8196. #ifndef NO_ECC_SECP
  8197. ssl->group[ssl->numGroups++] = WOLFSSL_ECC_SECP256R1;
  8198. #endif
  8199. #endif
  8200. #endif
  8201. #ifndef HAVE_FIPS
  8202. #if defined(HAVE_CURVE25519) && ECC_MIN_KEY_SZ <= 256
  8203. ssl->group[ssl->numGroups++] = WOLFSSL_ECC_X25519;
  8204. #endif
  8205. #endif
  8206. #ifndef HAVE_FIPS
  8207. #if defined(HAVE_CURVE448) && ECC_MIN_KEY_SZ <= 448
  8208. ssl->group[ssl->numGroups++] = WOLFSSL_ECC_X448;
  8209. #endif
  8210. #endif
  8211. #if defined(HAVE_ECC) && defined(HAVE_SUPPORTED_CURVES)
  8212. #if (defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 384
  8213. #ifndef NO_ECC_SECP
  8214. ssl->group[ssl->numGroups++] = WOLFSSL_ECC_SECP384R1;
  8215. #endif
  8216. #endif
  8217. #if (defined(HAVE_ECC521) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 521
  8218. #ifndef NO_ECC_SECP
  8219. ssl->group[ssl->numGroups++] = WOLFSSL_ECC_SECP521R1;
  8220. #endif
  8221. #endif
  8222. #endif
  8223. /* Add FFDHE supported groups. */
  8224. #ifdef HAVE_FFDHE_2048
  8225. ssl->group[ssl->numGroups++] = WOLFSSL_FFDHE_2048;
  8226. #endif
  8227. #ifdef HAVE_FFDHE_3072
  8228. ssl->group[ssl->numGroups++] = WOLFSSL_FFDHE_3072;
  8229. #endif
  8230. #ifdef HAVE_FFDHE_4096
  8231. ssl->group[ssl->numGroups++] = WOLFSSL_FFDHE_4096;
  8232. #endif
  8233. #ifdef HAVE_FFDHE_6144
  8234. ssl->group[ssl->numGroups++] = WOLFSSL_FFDHE_6144;
  8235. #endif
  8236. #ifdef HAVE_FFDHE_8192
  8237. ssl->group[ssl->numGroups++] = WOLFSSL_FFDHE_8192;
  8238. #endif
  8239. #ifdef HAVE_PQC
  8240. /* For the liboqs groups we need to do a runtime check because
  8241. * liboqs could be compiled to make an algorithm unavailable.
  8242. */
  8243. #ifdef WOLFSSL_WC_KYBER
  8244. #ifdef WOLFSSL_KYBER512
  8245. if (TLSX_KeyShare_IsSupported(WOLFSSL_KYBER_LEVEL1))
  8246. ssl->group[ssl->numGroups++] = WOLFSSL_KYBER_LEVEL1;
  8247. #endif
  8248. #ifdef WOLFSSL_KYBER768
  8249. if (TLSX_KeyShare_IsSupported(WOLFSSL_KYBER_LEVEL3))
  8250. ssl->group[ssl->numGroups++] = WOLFSSL_KYBER_LEVEL3;
  8251. #endif
  8252. #ifdef WOLFSSL_KYBER1024
  8253. if (TLSX_KeyShare_IsSupported(WOLFSSL_KYBER_LEVEL5))
  8254. ssl->group[ssl->numGroups++] = WOLFSSL_KYBER_LEVEL5;
  8255. #endif
  8256. #elif defined(HAVE_LIBOQS)
  8257. if (TLSX_KeyShare_IsSupported(WOLFSSL_KYBER_LEVEL1))
  8258. ssl->group[ssl->numGroups++] = WOLFSSL_KYBER_LEVEL1;
  8259. if (TLSX_KeyShare_IsSupported(WOLFSSL_KYBER_LEVEL3))
  8260. ssl->group[ssl->numGroups++] = WOLFSSL_KYBER_LEVEL3;
  8261. if (TLSX_KeyShare_IsSupported(WOLFSSL_KYBER_LEVEL5))
  8262. ssl->group[ssl->numGroups++] = WOLFSSL_KYBER_LEVEL5;
  8263. if (TLSX_KeyShare_IsSupported(WOLFSSL_NTRU_HPS_LEVEL1))
  8264. ssl->group[ssl->numGroups++] = WOLFSSL_NTRU_HPS_LEVEL1;
  8265. if (TLSX_KeyShare_IsSupported(WOLFSSL_NTRU_HPS_LEVEL3))
  8266. ssl->group[ssl->numGroups++] = WOLFSSL_NTRU_HPS_LEVEL3;
  8267. if (TLSX_KeyShare_IsSupported(WOLFSSL_NTRU_HPS_LEVEL5))
  8268. ssl->group[ssl->numGroups++] = WOLFSSL_NTRU_HPS_LEVEL5;
  8269. if (TLSX_KeyShare_IsSupported(WOLFSSL_NTRU_HRSS_LEVEL3))
  8270. ssl->group[ssl->numGroups++] = WOLFSSL_NTRU_HRSS_LEVEL3;
  8271. if (TLSX_KeyShare_IsSupported(WOLFSSL_SABER_LEVEL1))
  8272. ssl->group[ssl->numGroups++] = WOLFSSL_SABER_LEVEL1;
  8273. if (TLSX_KeyShare_IsSupported(WOLFSSL_SABER_LEVEL3))
  8274. ssl->group[ssl->numGroups++] = WOLFSSL_SABER_LEVEL3;
  8275. if (TLSX_KeyShare_IsSupported(WOLFSSL_SABER_LEVEL5))
  8276. ssl->group[ssl->numGroups++] = WOLFSSL_SABER_LEVEL5;
  8277. if (TLSX_KeyShare_IsSupported(WOLFSSL_KYBER_90S_LEVEL1))
  8278. ssl->group[ssl->numGroups++] = WOLFSSL_KYBER_90S_LEVEL1;
  8279. if (TLSX_KeyShare_IsSupported(WOLFSSL_KYBER_90S_LEVEL3))
  8280. ssl->group[ssl->numGroups++] = WOLFSSL_KYBER_90S_LEVEL3;
  8281. if (TLSX_KeyShare_IsSupported(WOLFSSL_KYBER_90S_LEVEL5))
  8282. ssl->group[ssl->numGroups++] = WOLFSSL_KYBER_90S_LEVEL5;
  8283. if (TLSX_KeyShare_IsSupported(WOLFSSL_P256_NTRU_HPS_LEVEL1))
  8284. ssl->group[ssl->numGroups++] = WOLFSSL_P256_NTRU_HPS_LEVEL1;
  8285. if (TLSX_KeyShare_IsSupported(WOLFSSL_P384_NTRU_HPS_LEVEL3))
  8286. ssl->group[ssl->numGroups++] = WOLFSSL_P384_NTRU_HPS_LEVEL3;
  8287. if (TLSX_KeyShare_IsSupported(WOLFSSL_P521_NTRU_HPS_LEVEL5))
  8288. ssl->group[ssl->numGroups++] = WOLFSSL_P521_NTRU_HPS_LEVEL5;
  8289. if (TLSX_KeyShare_IsSupported(WOLFSSL_P384_NTRU_HRSS_LEVEL3))
  8290. ssl->group[ssl->numGroups++] = WOLFSSL_P384_NTRU_HRSS_LEVEL3;
  8291. if (TLSX_KeyShare_IsSupported(WOLFSSL_P256_SABER_LEVEL1))
  8292. ssl->group[ssl->numGroups++] = WOLFSSL_P256_SABER_LEVEL1;
  8293. if (TLSX_KeyShare_IsSupported(WOLFSSL_P384_SABER_LEVEL3))
  8294. ssl->group[ssl->numGroups++] = WOLFSSL_P384_SABER_LEVEL3;
  8295. if (TLSX_KeyShare_IsSupported(WOLFSSL_P521_SABER_LEVEL5))
  8296. ssl->group[ssl->numGroups++] = WOLFSSL_P521_SABER_LEVEL5;
  8297. if (TLSX_KeyShare_IsSupported(WOLFSSL_P256_KYBER_LEVEL1))
  8298. ssl->group[ssl->numGroups++] = WOLFSSL_P256_KYBER_LEVEL1;
  8299. if (TLSX_KeyShare_IsSupported(WOLFSSL_P384_KYBER_LEVEL3))
  8300. ssl->group[ssl->numGroups++] = WOLFSSL_P384_KYBER_LEVEL3;
  8301. if (TLSX_KeyShare_IsSupported(WOLFSSL_P521_KYBER_LEVEL5))
  8302. ssl->group[ssl->numGroups++] = WOLFSSL_P521_KYBER_LEVEL5;
  8303. if (TLSX_KeyShare_IsSupported(WOLFSSL_P256_KYBER_90S_LEVEL1))
  8304. ssl->group[ssl->numGroups++] = WOLFSSL_P256_KYBER_90S_LEVEL1;
  8305. if (TLSX_KeyShare_IsSupported(WOLFSSL_P384_KYBER_90S_LEVEL3))
  8306. ssl->group[ssl->numGroups++] = WOLFSSL_P384_KYBER_90S_LEVEL3;
  8307. if (TLSX_KeyShare_IsSupported(WOLFSSL_P521_KYBER_90S_LEVEL5))
  8308. ssl->group[ssl->numGroups++] = WOLFSSL_P521_KYBER_90S_LEVEL5;
  8309. #elif defined(HAVE_PQM4)
  8310. if (TLSX_KeyShare_IsSupported(WOLFSSL_KYBER_LEVEL1))
  8311. ssl->group[ssl->numGroups++] = WOLFSSL_KYBER_LEVEL1;
  8312. #endif /* HAVE_LIBOQS */
  8313. #endif /* HAVE_PQC */
  8314. }
  8315. for (i = 0; i < ssl->numGroups; i++)
  8316. if (ssl->group[i] == (word16)group)
  8317. return i;
  8318. return -1;
  8319. }
  8320. /* Set a key share that is supported by the client into extensions.
  8321. *
  8322. * ssl The SSL/TLS object.
  8323. * returns BAD_KEY_SHARE_DATA if no supported group has a key share,
  8324. * 0 if a supported group has a key share and other values indicate an error.
  8325. */
  8326. static int TLSX_KeyShare_SetSupported(WOLFSSL* ssl)
  8327. {
  8328. int ret;
  8329. #ifdef HAVE_SUPPORTED_CURVES
  8330. TLSX* extension;
  8331. SupportedCurve* curve = NULL;
  8332. SupportedCurve* preferredCurve = NULL;
  8333. int preferredRank = WOLFSSL_MAX_GROUP_COUNT;
  8334. int rank;
  8335. extension = TLSX_Find(ssl->extensions, TLSX_SUPPORTED_GROUPS);
  8336. if (extension != NULL)
  8337. curve = (SupportedCurve*)extension->data;
  8338. /* Use server's preference order. */
  8339. for (; curve != NULL; curve = curve->next) {
  8340. if (!TLSX_KeyShare_IsSupported(curve->name))
  8341. continue;
  8342. if (wolfSSL_curve_is_disabled(ssl, curve->name))
  8343. continue;
  8344. rank = TLSX_KeyShare_GroupRank(ssl, curve->name);
  8345. if (rank == -1)
  8346. continue;
  8347. if (rank < preferredRank) {
  8348. preferredCurve = curve;
  8349. preferredRank = rank;
  8350. }
  8351. }
  8352. curve = preferredCurve;
  8353. if (curve == NULL) {
  8354. WOLFSSL_ERROR_VERBOSE(BAD_KEY_SHARE_DATA);
  8355. return BAD_KEY_SHARE_DATA;
  8356. }
  8357. /* Delete the old key share data list. */
  8358. extension = TLSX_Find(ssl->extensions, TLSX_KEY_SHARE);
  8359. if (extension != NULL) {
  8360. KeyShareEntry* kse = (KeyShareEntry*)extension->data;
  8361. #ifdef WOLFSSL_ASYNC_CRYPT
  8362. /* for async don't free, call `TLSX_KeyShare_Use` again */
  8363. if (kse && kse->lastRet != WC_PENDING_E)
  8364. #endif
  8365. {
  8366. TLSX_KeyShare_FreeAll(kse, ssl->heap);
  8367. extension->data = NULL;
  8368. }
  8369. }
  8370. /* Add in the chosen group. */
  8371. ret = TLSX_KeyShare_Use(ssl, curve->name, 0, NULL, NULL);
  8372. if (ret != 0 && ret != WC_PENDING_E)
  8373. return ret;
  8374. /* Set extension to be in response. */
  8375. extension = TLSX_Find(ssl->extensions, TLSX_KEY_SHARE);
  8376. extension->resp = 1;
  8377. #else
  8378. (void)ssl;
  8379. WOLFSSL_ERROR_VERBOSE(NOT_COMPILED_IN);
  8380. ret = NOT_COMPILED_IN;
  8381. #endif
  8382. return ret;
  8383. }
  8384. /* Ensure there is a key pair that can be used for key exchange.
  8385. *
  8386. * ssl The SSL/TLS object.
  8387. * doHelloRetry If set to non-zero will do hello_retry
  8388. * returns 0 on success and other values indicate failure.
  8389. */
  8390. int TLSX_KeyShare_Establish(WOLFSSL *ssl, int* doHelloRetry)
  8391. {
  8392. int ret;
  8393. TLSX* extension;
  8394. KeyShareEntry* clientKSE = NULL;
  8395. KeyShareEntry* serverKSE;
  8396. KeyShareEntry* list = NULL;
  8397. KeyShareEntry* preferredKSE = NULL;
  8398. int preferredRank = WOLFSSL_MAX_GROUP_COUNT;
  8399. int rank;
  8400. /* Find the KeyShare extension if it exists. */
  8401. extension = TLSX_Find(ssl->extensions, TLSX_KEY_SHARE);
  8402. if (extension != NULL)
  8403. list = (KeyShareEntry*)extension->data;
  8404. if (extension && extension->resp == 1) {
  8405. ret = 0;
  8406. #ifdef WOLFSSL_ASYNC_CRYPT
  8407. /* in async case make sure key generation is finalized */
  8408. serverKSE = (KeyShareEntry*)extension->data;
  8409. if (serverKSE->lastRet == WC_PENDING_E) {
  8410. if (ssl->options.serverState == SERVER_HELLO_RETRY_REQUEST_COMPLETE)
  8411. *doHelloRetry = 1;
  8412. ret = TLSX_KeyShare_GenKey(ssl, serverKSE);
  8413. }
  8414. #endif
  8415. return ret;
  8416. }
  8417. /* Use server's preference order. */
  8418. for (clientKSE = list; clientKSE != NULL; clientKSE = clientKSE->next) {
  8419. if (clientKSE->ke == NULL)
  8420. continue;
  8421. /* Check consistency now - extensions in any order. */
  8422. if (!TLSX_SupportedGroups_Find(ssl, clientKSE->group))
  8423. continue;
  8424. if (!WOLFSSL_NAMED_GROUP_IS_FFHDE(clientKSE->group)) {
  8425. /* Check max value supported. */
  8426. if (clientKSE->group > WOLFSSL_ECC_MAX) {
  8427. #ifdef HAVE_PQC
  8428. if (!WOLFSSL_NAMED_GROUP_IS_PQC(clientKSE->group))
  8429. #endif
  8430. continue;
  8431. }
  8432. if (wolfSSL_curve_is_disabled(ssl, clientKSE->group))
  8433. continue;
  8434. }
  8435. if (!TLSX_KeyShare_IsSupported(clientKSE->group))
  8436. continue;
  8437. rank = TLSX_KeyShare_GroupRank(ssl, clientKSE->group);
  8438. if (rank == -1)
  8439. continue;
  8440. if (rank < preferredRank) {
  8441. preferredKSE = clientKSE;
  8442. preferredRank = rank;
  8443. }
  8444. }
  8445. clientKSE = preferredKSE;
  8446. /* No supported group found - send HelloRetryRequest. */
  8447. if (clientKSE == NULL) {
  8448. /* Set KEY_SHARE_ERROR to indicate HelloRetryRequest required. */
  8449. *doHelloRetry = 1;
  8450. return TLSX_KeyShare_SetSupported(ssl);
  8451. }
  8452. list = NULL;
  8453. /* Generate a new key pair except in the case of OQS KEM because we
  8454. * are going to encapsulate and that does not require us to generate a
  8455. * key pair.
  8456. */
  8457. ret = TLSX_KeyShare_New(&list, clientKSE->group, ssl->heap, &serverKSE);
  8458. if (ret != 0)
  8459. return ret;
  8460. if (clientKSE->key == NULL) {
  8461. #ifdef HAVE_PQC
  8462. if (WOLFSSL_NAMED_GROUP_IS_PQC(clientKSE->group)) {
  8463. /* Going to need the public key (AKA ciphertext). */
  8464. serverKSE->pubKey = clientKSE->pubKey;
  8465. clientKSE->pubKey = NULL;
  8466. serverKSE->pubKeyLen = clientKSE->pubKeyLen;
  8467. clientKSE->pubKeyLen = 0;
  8468. }
  8469. else
  8470. #endif
  8471. {
  8472. ret = TLSX_KeyShare_GenKey(ssl, serverKSE);
  8473. }
  8474. /* for async do setup of serverKSE below, but return WC_PENDING_E */
  8475. if (ret != 0
  8476. #ifdef WOLFSSL_ASYNC_CRYPT
  8477. && ret != WC_PENDING_E
  8478. #endif
  8479. ) {
  8480. return ret;
  8481. }
  8482. }
  8483. else {
  8484. /* transfer buffers to serverKSE */
  8485. serverKSE->key = clientKSE->key;
  8486. clientKSE->key = NULL;
  8487. serverKSE->keyLen = clientKSE->keyLen;
  8488. serverKSE->pubKey = clientKSE->pubKey;
  8489. clientKSE->pubKey = NULL;
  8490. serverKSE->pubKeyLen = clientKSE->pubKeyLen;
  8491. #ifndef NO_DH
  8492. serverKSE->privKey = clientKSE->privKey;
  8493. clientKSE->privKey = NULL;
  8494. #endif
  8495. }
  8496. serverKSE->ke = clientKSE->ke;
  8497. serverKSE->keLen = clientKSE->keLen;
  8498. clientKSE->ke = NULL;
  8499. clientKSE->keLen = 0;
  8500. TLSX_KeyShare_FreeAll((KeyShareEntry*)extension->data, ssl->heap);
  8501. extension->data = (void *)serverKSE;
  8502. extension->resp = 1;
  8503. return ret;
  8504. }
  8505. /* Derive the shared secret of the key exchange.
  8506. *
  8507. * ssl The SSL/TLS object.
  8508. * returns 0 on success and other values indicate failure.
  8509. */
  8510. int TLSX_KeyShare_DeriveSecret(WOLFSSL *ssl)
  8511. {
  8512. int ret;
  8513. TLSX* extension;
  8514. KeyShareEntry* list = NULL;
  8515. #ifdef WOLFSSL_ASYNC_CRYPT
  8516. ret = wolfSSL_AsyncPop(ssl, NULL);
  8517. /* Check for error */
  8518. if (ret != WC_NOT_PENDING_E && ret < 0) {
  8519. return ret;
  8520. }
  8521. #endif
  8522. /* Find the KeyShare extension if it exists. */
  8523. extension = TLSX_Find(ssl->extensions, TLSX_KEY_SHARE);
  8524. if (extension != NULL)
  8525. list = (KeyShareEntry*)extension->data;
  8526. if (list == NULL)
  8527. return KEY_SHARE_ERROR;
  8528. /* Calculate secret. */
  8529. ret = TLSX_KeyShare_Process(ssl, list);
  8530. return ret;
  8531. }
  8532. #define KS_FREE_ALL TLSX_KeyShare_FreeAll
  8533. #define KS_GET_SIZE TLSX_KeyShare_GetSize
  8534. #define KS_WRITE TLSX_KeyShare_Write
  8535. #define KS_PARSE TLSX_KeyShare_Parse
  8536. #else
  8537. #define KS_FREE_ALL(a, b)
  8538. #define KS_GET_SIZE(a, b) 0
  8539. #define KS_WRITE(a, b, c) 0
  8540. #define KS_PARSE(a, b, c, d) 0
  8541. #endif /* WOLFSSL_TLS13 */
  8542. /******************************************************************************/
  8543. /* Pre-Shared Key */
  8544. /******************************************************************************/
  8545. #if defined(WOLFSSL_TLS13) && (defined(HAVE_SESSION_TICKET) || !defined(NO_PSK))
  8546. /* Free the pre-shared key dynamic data.
  8547. *
  8548. * list The linked list of key share entry objects.
  8549. * heap The heap used for allocation.
  8550. */
  8551. static void TLSX_PreSharedKey_FreeAll(PreSharedKey* list, void* heap)
  8552. {
  8553. PreSharedKey* current;
  8554. while ((current = list) != NULL) {
  8555. list = current->next;
  8556. XFREE(current->identity, heap, DYNAMIC_TYPE_TLSX);
  8557. XFREE(current, heap, DYNAMIC_TYPE_TLSX);
  8558. }
  8559. (void)heap;
  8560. }
  8561. /* Get the size of the encoded pre shared key extension.
  8562. *
  8563. * list The linked list of pre-shared key extensions.
  8564. * msgType The type of the message this extension is being written into.
  8565. * returns the number of bytes of the encoded pre-shared key extension or
  8566. * SANITY_MSG_E to indicate invalid message type.
  8567. */
  8568. static int TLSX_PreSharedKey_GetSize(PreSharedKey* list, byte msgType,
  8569. word16* pSz)
  8570. {
  8571. if (msgType == client_hello) {
  8572. /* Length of identities + Length of binders. */
  8573. word16 len = OPAQUE16_LEN + OPAQUE16_LEN;
  8574. while (list != NULL) {
  8575. /* Each entry has: identity, ticket age and binder. */
  8576. len += OPAQUE16_LEN + list->identityLen + OPAQUE32_LEN +
  8577. OPAQUE8_LEN + (word16)list->binderLen;
  8578. list = list->next;
  8579. }
  8580. *pSz += len;
  8581. return 0;
  8582. }
  8583. if (msgType == server_hello) {
  8584. *pSz += OPAQUE16_LEN;
  8585. return 0;
  8586. }
  8587. WOLFSSL_ERROR_VERBOSE(SANITY_MSG_E);
  8588. return SANITY_MSG_E;
  8589. }
  8590. /* The number of bytes to be written for the binders.
  8591. *
  8592. * list The linked list of pre-shared key extensions.
  8593. * msgType The type of the message this extension is being written into.
  8594. * returns the number of bytes of the encoded pre-shared key extension or
  8595. * SANITY_MSG_E to indicate invalid message type.
  8596. */
  8597. int TLSX_PreSharedKey_GetSizeBinders(PreSharedKey* list, byte msgType,
  8598. word16* pSz)
  8599. {
  8600. word16 len;
  8601. if (msgType != client_hello) {
  8602. WOLFSSL_ERROR_VERBOSE(SANITY_MSG_E);
  8603. return SANITY_MSG_E;
  8604. }
  8605. /* Length of all binders. */
  8606. len = OPAQUE16_LEN;
  8607. while (list != NULL) {
  8608. len += OPAQUE8_LEN + (word16)list->binderLen;
  8609. list = list->next;
  8610. }
  8611. *pSz = len;
  8612. return 0;
  8613. }
  8614. /* Writes the pre-shared key extension into the output buffer - binders only.
  8615. * Assumes that the the output buffer is big enough to hold data.
  8616. *
  8617. * list The linked list of key share entries.
  8618. * output The buffer to write into.
  8619. * msgType The type of the message this extension is being written into.
  8620. * returns the number of bytes written into the buffer.
  8621. */
  8622. int TLSX_PreSharedKey_WriteBinders(PreSharedKey* list, byte* output,
  8623. byte msgType, word16* pSz)
  8624. {
  8625. PreSharedKey* current = list;
  8626. word16 idx = 0;
  8627. word16 lenIdx;
  8628. word16 len;
  8629. if (msgType != client_hello) {
  8630. WOLFSSL_ERROR_VERBOSE(SANITY_MSG_E);
  8631. return SANITY_MSG_E;
  8632. }
  8633. /* Skip length of all binders. */
  8634. lenIdx = idx;
  8635. idx += OPAQUE16_LEN;
  8636. while (current != NULL) {
  8637. /* Binder data length. */
  8638. output[idx++] = (byte)current->binderLen;
  8639. /* Binder data. */
  8640. XMEMCPY(output + idx, current->binder, current->binderLen);
  8641. idx += (word16)current->binderLen;
  8642. current = current->next;
  8643. }
  8644. /* Length of the binders. */
  8645. len = idx - lenIdx - OPAQUE16_LEN;
  8646. c16toa(len, output + lenIdx);
  8647. *pSz = idx;
  8648. return 0;
  8649. }
  8650. /* Writes the pre-shared key extension into the output buffer.
  8651. * Assumes that the the output buffer is big enough to hold data.
  8652. *
  8653. * list The linked list of key share entries.
  8654. * output The buffer to write into.
  8655. * msgType The type of the message this extension is being written into.
  8656. * returns the number of bytes written into the buffer.
  8657. */
  8658. static int TLSX_PreSharedKey_Write(PreSharedKey* list, byte* output,
  8659. byte msgType, word16* pSz)
  8660. {
  8661. if (msgType == client_hello) {
  8662. PreSharedKey* current = list;
  8663. word16 idx = 0;
  8664. word16 lenIdx;
  8665. word16 len;
  8666. int ret;
  8667. /* Write identites only. Binders after HMACing over this. */
  8668. lenIdx = idx;
  8669. idx += OPAQUE16_LEN;
  8670. while (current != NULL) {
  8671. /* Identity length */
  8672. c16toa(current->identityLen, output + idx);
  8673. idx += OPAQUE16_LEN;
  8674. /* Identity data */
  8675. XMEMCPY(output + idx, current->identity, current->identityLen);
  8676. idx += current->identityLen;
  8677. /* Obfuscated ticket age. */
  8678. c32toa(current->ticketAge, output + idx);
  8679. idx += OPAQUE32_LEN;
  8680. current = current->next;
  8681. }
  8682. /* Length of the identites. */
  8683. len = idx - lenIdx - OPAQUE16_LEN;
  8684. c16toa(len, output + lenIdx);
  8685. /* Don't include binders here.
  8686. * The binders are based on the hash of all the ClientHello data up to
  8687. * and include the identities written above.
  8688. */
  8689. ret = TLSX_PreSharedKey_GetSizeBinders(list, msgType, &len);
  8690. if (ret < 0)
  8691. return ret;
  8692. *pSz += idx + len;
  8693. }
  8694. else if (msgType == server_hello) {
  8695. word16 i;
  8696. /* Find the index of the chosen identity. */
  8697. for (i=0; list != NULL && !list->chosen; i++)
  8698. list = list->next;
  8699. if (list == NULL) {
  8700. WOLFSSL_ERROR_VERBOSE(BUILD_MSG_ERROR);
  8701. return BUILD_MSG_ERROR;
  8702. }
  8703. /* The index of the identity chosen by the server from the list supplied
  8704. * by the client.
  8705. */
  8706. c16toa(i, output);
  8707. *pSz += OPAQUE16_LEN;
  8708. }
  8709. else {
  8710. WOLFSSL_ERROR_VERBOSE(SANITY_MSG_E);
  8711. return SANITY_MSG_E;
  8712. }
  8713. return 0;
  8714. }
  8715. /* Parse the pre-shared key extension.
  8716. * Different formats in different messages.
  8717. *
  8718. * ssl The SSL/TLS object.
  8719. * input The extension data.
  8720. * length The length of the extension data.
  8721. * msgType The type of the message this extension is being parsed from.
  8722. * returns 0 on success and other values indicate failure.
  8723. */
  8724. static int TLSX_PreSharedKey_Parse(WOLFSSL* ssl, const byte* input,
  8725. word16 length, byte msgType)
  8726. {
  8727. TLSX* extension;
  8728. PreSharedKey* list;
  8729. if (msgType == client_hello) {
  8730. int ret;
  8731. word16 len;
  8732. word16 idx = 0;
  8733. TLSX_Remove(&ssl->extensions, TLSX_PRE_SHARED_KEY, ssl->heap);
  8734. /* Length of identities and of binders. */
  8735. if ((int)(length - idx) < OPAQUE16_LEN + OPAQUE16_LEN)
  8736. return BUFFER_E;
  8737. /* Length of identities. */
  8738. ato16(input + idx, &len);
  8739. idx += OPAQUE16_LEN;
  8740. if (len < MIN_PSK_ID_LEN || length - idx < len)
  8741. return BUFFER_E;
  8742. /* Create a pre-shared key object for each identity. */
  8743. while (len > 0) {
  8744. const byte* identity;
  8745. word16 identityLen;
  8746. word32 age;
  8747. if (len < OPAQUE16_LEN)
  8748. return BUFFER_E;
  8749. /* Length of identity. */
  8750. ato16(input + idx, &identityLen);
  8751. idx += OPAQUE16_LEN;
  8752. if (len < OPAQUE16_LEN + identityLen + OPAQUE32_LEN ||
  8753. identityLen > MAX_PSK_ID_LEN)
  8754. return BUFFER_E;
  8755. /* Cache identity pointer. */
  8756. identity = input + idx;
  8757. idx += identityLen;
  8758. /* Ticket age. */
  8759. ato32(input + idx, &age);
  8760. idx += OPAQUE32_LEN;
  8761. ret = TLSX_PreSharedKey_Use(ssl, identity, identityLen, age, no_mac,
  8762. 0, 0, 1, NULL);
  8763. if (ret != 0)
  8764. return ret;
  8765. /* Done with this identity. */
  8766. len -= OPAQUE16_LEN + identityLen + OPAQUE32_LEN;
  8767. }
  8768. /* Find the list of identities sent to server. */
  8769. extension = TLSX_Find(ssl->extensions, TLSX_PRE_SHARED_KEY);
  8770. if (extension == NULL)
  8771. return PSK_KEY_ERROR;
  8772. list = (PreSharedKey*)extension->data;
  8773. /* Length of binders. */
  8774. if (idx + OPAQUE16_LEN > length)
  8775. return BUFFER_E;
  8776. ato16(input + idx, &len);
  8777. idx += OPAQUE16_LEN;
  8778. if (len < MIN_PSK_BINDERS_LEN || length - idx < len)
  8779. return BUFFER_E;
  8780. /* Set binder for each identity. */
  8781. while (list != NULL && len > 0) {
  8782. /* Length of binder */
  8783. list->binderLen = input[idx++];
  8784. if (list->binderLen < WC_SHA256_DIGEST_SIZE ||
  8785. list->binderLen > WC_MAX_DIGEST_SIZE)
  8786. return BUFFER_E;
  8787. if (len < OPAQUE8_LEN + list->binderLen)
  8788. return BUFFER_E;
  8789. /* Copy binder into static buffer. */
  8790. XMEMCPY(list->binder, input + idx, list->binderLen);
  8791. idx += (word16)list->binderLen;
  8792. /* Done with binder entry. */
  8793. len -= OPAQUE8_LEN + (word16)list->binderLen;
  8794. /* Next identity. */
  8795. list = list->next;
  8796. }
  8797. if (list != NULL || len != 0)
  8798. return BUFFER_E;
  8799. return 0;
  8800. }
  8801. if (msgType == server_hello) {
  8802. word16 idx;
  8803. /* Index of identity chosen by server. */
  8804. if (length != OPAQUE16_LEN)
  8805. return BUFFER_E;
  8806. ato16(input, &idx);
  8807. #ifdef WOLFSSL_EARLY_DATA
  8808. ssl->options.pskIdIndex = idx + 1;
  8809. #endif
  8810. /* Find the list of identities sent to server. */
  8811. extension = TLSX_Find(ssl->extensions, TLSX_PRE_SHARED_KEY);
  8812. if (extension == NULL)
  8813. return PSK_KEY_ERROR;
  8814. list = (PreSharedKey*)extension->data;
  8815. /* Mark the identity as chosen. */
  8816. for (; list != NULL && idx > 0; idx--)
  8817. list = list->next;
  8818. if (list == NULL) {
  8819. WOLFSSL_ERROR_VERBOSE(PSK_KEY_ERROR);
  8820. return PSK_KEY_ERROR;
  8821. }
  8822. list->chosen = 1;
  8823. #ifdef HAVE_SESSION_TICKET
  8824. if (list->resumption) {
  8825. /* Check that the session's details are the same as the server's. */
  8826. if (ssl->options.cipherSuite0 != ssl->session->cipherSuite0 ||
  8827. ssl->options.cipherSuite != ssl->session->cipherSuite ||
  8828. ssl->session->version.major != ssl->ctx->method->version.major ||
  8829. ssl->session->version.minor != ssl->ctx->method->version.minor) {
  8830. WOLFSSL_ERROR_VERBOSE(PSK_KEY_ERROR);
  8831. return PSK_KEY_ERROR;
  8832. }
  8833. }
  8834. #endif
  8835. return 0;
  8836. }
  8837. WOLFSSL_ERROR_VERBOSE(SANITY_MSG_E);
  8838. return SANITY_MSG_E;
  8839. }
  8840. /* Create a new pre-shared key and put it into the list.
  8841. *
  8842. * list The linked list of pre-shared key.
  8843. * identity The identity.
  8844. * len The length of the identity data.
  8845. * heap The memory to allocate with.
  8846. * preSharedKey The new pre-shared key object.
  8847. * returns 0 on success and other values indicate failure.
  8848. */
  8849. static int TLSX_PreSharedKey_New(PreSharedKey** list, const byte* identity,
  8850. word16 len, void *heap,
  8851. PreSharedKey** preSharedKey)
  8852. {
  8853. PreSharedKey* psk;
  8854. PreSharedKey** next;
  8855. psk = (PreSharedKey*)XMALLOC(sizeof(PreSharedKey), heap, DYNAMIC_TYPE_TLSX);
  8856. if (psk == NULL)
  8857. return MEMORY_E;
  8858. XMEMSET(psk, 0, sizeof(*psk));
  8859. /* Make a copy of the identity data. */
  8860. psk->identity = (byte*)XMALLOC(len, heap, DYNAMIC_TYPE_TLSX);
  8861. if (psk->identity == NULL) {
  8862. XFREE(psk, heap, DYNAMIC_TYPE_TLSX);
  8863. return MEMORY_E;
  8864. }
  8865. XMEMCPY(psk->identity, identity, len);
  8866. psk->identityLen = len;
  8867. /* Add it to the end and maintain the links. */
  8868. while (*list != NULL) {
  8869. /* Assign to temporary to work around compiler bug found by customer. */
  8870. next = &((*list)->next);
  8871. list = next;
  8872. }
  8873. *list = psk;
  8874. *preSharedKey = psk;
  8875. (void)heap;
  8876. return 0;
  8877. }
  8878. static WC_INLINE byte GetHmacLength(int hmac)
  8879. {
  8880. switch (hmac) {
  8881. #ifndef NO_SHA256
  8882. case sha256_mac:
  8883. return WC_SHA256_DIGEST_SIZE;
  8884. #endif
  8885. #ifdef WOLFSSL_SHA384
  8886. case sha384_mac:
  8887. return WC_SHA384_DIGEST_SIZE;
  8888. #endif
  8889. #ifdef WOLFSSL_SHA512
  8890. case sha512_mac:
  8891. return WC_SHA512_DIGEST_SIZE;
  8892. #endif
  8893. }
  8894. return 0;
  8895. }
  8896. /* Use the data to create a new pre-shared key object in the extensions.
  8897. *
  8898. * ssl The SSL/TLS object.
  8899. * identity The identity.
  8900. * len The length of the identity data.
  8901. * age The age of the identity.
  8902. * hmac The HMAC algorithm.
  8903. * ciphersuite0 The first byte of the ciphersuite to use.
  8904. * ciphersuite The second byte of the ciphersuite to use.
  8905. * resumption The PSK is for resumption of a session.
  8906. * preSharedKey The new pre-shared key object.
  8907. * returns 0 on success and other values indicate failure.
  8908. */
  8909. int TLSX_PreSharedKey_Use(WOLFSSL* ssl, const byte* identity, word16 len,
  8910. word32 age, byte hmac, byte cipherSuite0,
  8911. byte cipherSuite, byte resumption,
  8912. PreSharedKey **preSharedKey)
  8913. {
  8914. int ret = 0;
  8915. TLSX* extension;
  8916. PreSharedKey* psk = NULL;
  8917. /* Find the pre-shared key extension if it exists. */
  8918. extension = TLSX_Find(ssl->extensions, TLSX_PRE_SHARED_KEY);
  8919. if (extension == NULL) {
  8920. /* Push new pre-shared key extension. */
  8921. ret = TLSX_Push(&ssl->extensions, TLSX_PRE_SHARED_KEY, NULL, ssl->heap);
  8922. if (ret != 0)
  8923. return ret;
  8924. extension = TLSX_Find(ssl->extensions, TLSX_PRE_SHARED_KEY);
  8925. if (extension == NULL)
  8926. return MEMORY_E;
  8927. }
  8928. /* Try to find the pre-shared key with this identity. */
  8929. psk = (PreSharedKey*)extension->data;
  8930. while (psk != NULL) {
  8931. if ((psk->identityLen == len) &&
  8932. (XMEMCMP(psk->identity, identity, len) == 0)) {
  8933. break;
  8934. }
  8935. psk = psk->next;
  8936. }
  8937. /* Create a new pre-shared key object if not found. */
  8938. if (psk == NULL) {
  8939. ret = TLSX_PreSharedKey_New((PreSharedKey**)&extension->data, identity,
  8940. len, ssl->heap, &psk);
  8941. if (ret != 0)
  8942. return ret;
  8943. }
  8944. /* Update/set age and HMAC algorithm. */
  8945. psk->ticketAge = age;
  8946. psk->hmac = hmac;
  8947. psk->cipherSuite0 = cipherSuite0;
  8948. psk->cipherSuite = cipherSuite;
  8949. psk->resumption = resumption;
  8950. psk->binderLen = GetHmacLength(psk->hmac);
  8951. if (preSharedKey != NULL)
  8952. *preSharedKey = psk;
  8953. return 0;
  8954. }
  8955. #define PSK_FREE_ALL TLSX_PreSharedKey_FreeAll
  8956. #define PSK_GET_SIZE TLSX_PreSharedKey_GetSize
  8957. #define PSK_WRITE TLSX_PreSharedKey_Write
  8958. #define PSK_PARSE TLSX_PreSharedKey_Parse
  8959. #else
  8960. #define PSK_FREE_ALL(a, b)
  8961. #define PSK_GET_SIZE(a, b, c) 0
  8962. #define PSK_WRITE(a, b, c, d) 0
  8963. #define PSK_PARSE(a, b, c, d) 0
  8964. #endif
  8965. /******************************************************************************/
  8966. /* PSK Key Exchange Modes */
  8967. /******************************************************************************/
  8968. #if defined(WOLFSSL_TLS13) && (defined(HAVE_SESSION_TICKET) || !defined(NO_PSK))
  8969. /* Get the size of the encoded PSK KE modes extension.
  8970. * Only in ClientHello.
  8971. *
  8972. * modes The PSK KE mode bit string.
  8973. * msgType The type of the message this extension is being written into.
  8974. * returns the number of bytes of the encoded PSK KE mode extension.
  8975. */
  8976. static int TLSX_PskKeModes_GetSize(byte modes, byte msgType, word16* pSz)
  8977. {
  8978. if (msgType == client_hello) {
  8979. /* Format: Len | Modes* */
  8980. word16 len = OPAQUE8_LEN;
  8981. /* Check whether each possible mode is to be written. */
  8982. if (modes & (1 << PSK_KE))
  8983. len += OPAQUE8_LEN;
  8984. if (modes & (1 << PSK_DHE_KE))
  8985. len += OPAQUE8_LEN;
  8986. *pSz += len;
  8987. return 0;
  8988. }
  8989. WOLFSSL_ERROR_VERBOSE(SANITY_MSG_E);
  8990. return SANITY_MSG_E;
  8991. }
  8992. /* Writes the PSK KE modes extension into the output buffer.
  8993. * Assumes that the the output buffer is big enough to hold data.
  8994. * Only in ClientHello.
  8995. *
  8996. * modes The PSK KE mode bit string.
  8997. * output The buffer to write into.
  8998. * msgType The type of the message this extension is being written into.
  8999. * returns the number of bytes written into the buffer.
  9000. */
  9001. static int TLSX_PskKeModes_Write(byte modes, byte* output, byte msgType,
  9002. word16* pSz)
  9003. {
  9004. if (msgType == client_hello) {
  9005. /* Format: Len | Modes* */
  9006. word16 idx = OPAQUE8_LEN;
  9007. /* Write out each possible mode. */
  9008. if (modes & (1 << PSK_KE))
  9009. output[idx++] = PSK_KE;
  9010. if (modes & (1 << PSK_DHE_KE))
  9011. output[idx++] = PSK_DHE_KE;
  9012. /* Write out length of mode list. */
  9013. output[0] = (byte)(idx - OPAQUE8_LEN);
  9014. *pSz += idx;
  9015. return 0;
  9016. }
  9017. WOLFSSL_ERROR_VERBOSE(SANITY_MSG_E);
  9018. return SANITY_MSG_E;
  9019. }
  9020. /* Parse the PSK KE modes extension.
  9021. * Only in ClientHello.
  9022. *
  9023. * ssl The SSL/TLS object.
  9024. * input The extension data.
  9025. * length The length of the extension data.
  9026. * msgType The type of the message this extension is being parsed from.
  9027. * returns 0 on success and other values indicate failure.
  9028. */
  9029. static int TLSX_PskKeModes_Parse(WOLFSSL* ssl, const byte* input, word16 length,
  9030. byte msgType)
  9031. {
  9032. int ret;
  9033. if (msgType == client_hello) {
  9034. /* Format: Len | Modes* */
  9035. int idx = 0;
  9036. word16 len;
  9037. byte modes = 0;
  9038. /* Ensure length byte exists. */
  9039. if (length < OPAQUE8_LEN)
  9040. return BUFFER_E;
  9041. /* Get length of mode list and ensure that is the only data. */
  9042. len = input[0];
  9043. if (length - OPAQUE8_LEN != len)
  9044. return BUFFER_E;
  9045. idx = OPAQUE8_LEN;
  9046. /* Set a bit for each recognized modes. */
  9047. while (len > 0) {
  9048. /* Ignore unrecognized modes. */
  9049. if (input[idx] <= PSK_DHE_KE)
  9050. modes |= 1 << input[idx];
  9051. idx++;
  9052. len--;
  9053. }
  9054. ret = TLSX_PskKeModes_Use(ssl, modes);
  9055. if (ret != 0)
  9056. return ret;
  9057. return 0;
  9058. }
  9059. WOLFSSL_ERROR_VERBOSE(SANITY_MSG_E);
  9060. return SANITY_MSG_E;
  9061. }
  9062. /* Use the data to create a new PSK Key Exchange Modes object in the extensions.
  9063. *
  9064. * ssl The SSL/TLS object.
  9065. * modes The PSK key exchange modes.
  9066. * returns 0 on success and other values indicate failure.
  9067. */
  9068. int TLSX_PskKeModes_Use(WOLFSSL* ssl, byte modes)
  9069. {
  9070. int ret = 0;
  9071. TLSX* extension;
  9072. /* Find the PSK key exchange modes extension if it exists. */
  9073. extension = TLSX_Find(ssl->extensions, TLSX_PSK_KEY_EXCHANGE_MODES);
  9074. if (extension == NULL) {
  9075. /* Push new PSK key exchange modes extension. */
  9076. ret = TLSX_Push(&ssl->extensions, TLSX_PSK_KEY_EXCHANGE_MODES, NULL,
  9077. ssl->heap);
  9078. if (ret != 0)
  9079. return ret;
  9080. extension = TLSX_Find(ssl->extensions, TLSX_PSK_KEY_EXCHANGE_MODES);
  9081. if (extension == NULL)
  9082. return MEMORY_E;
  9083. }
  9084. extension->val = modes;
  9085. return 0;
  9086. }
  9087. #define PKM_GET_SIZE TLSX_PskKeModes_GetSize
  9088. #define PKM_WRITE TLSX_PskKeModes_Write
  9089. #define PKM_PARSE TLSX_PskKeModes_Parse
  9090. #else
  9091. #define PKM_GET_SIZE(a, b, c) 0
  9092. #define PKM_WRITE(a, b, c, d) 0
  9093. #define PKM_PARSE(a, b, c, d) 0
  9094. #endif
  9095. /******************************************************************************/
  9096. /* Post-Handshake Authentication */
  9097. /******************************************************************************/
  9098. #if defined(WOLFSSL_TLS13) && defined(WOLFSSL_POST_HANDSHAKE_AUTH)
  9099. /* Get the size of the encoded Post-Handshake Authentication extension.
  9100. * Only in ClientHello.
  9101. *
  9102. * msgType The type of the message this extension is being written into.
  9103. * returns the number of bytes of the encoded Post-Handshake Authentication
  9104. * extension.
  9105. */
  9106. static int TLSX_PostHandAuth_GetSize(byte msgType, word16* pSz)
  9107. {
  9108. if (msgType == client_hello) {
  9109. *pSz += 0;
  9110. return 0;
  9111. }
  9112. WOLFSSL_ERROR_VERBOSE(SANITY_MSG_E);
  9113. return SANITY_MSG_E;
  9114. }
  9115. /* Writes the Post-Handshake Authentication extension into the output buffer.
  9116. * Assumes that the the output buffer is big enough to hold data.
  9117. * Only in ClientHello.
  9118. *
  9119. * output The buffer to write into.
  9120. * msgType The type of the message this extension is being written into.
  9121. * returns the number of bytes written into the buffer.
  9122. */
  9123. static int TLSX_PostHandAuth_Write(byte* output, byte msgType, word16* pSz)
  9124. {
  9125. (void)output;
  9126. if (msgType == client_hello) {
  9127. *pSz += 0;
  9128. return 0;
  9129. }
  9130. WOLFSSL_ERROR_VERBOSE(SANITY_MSG_E);
  9131. return SANITY_MSG_E;
  9132. }
  9133. /* Parse the Post-Handshake Authentication extension.
  9134. * Only in ClientHello.
  9135. *
  9136. * ssl The SSL/TLS object.
  9137. * input The extension data.
  9138. * length The length of the extension data.
  9139. * msgType The type of the message this extension is being parsed from.
  9140. * returns 0 on success and other values indicate failure.
  9141. */
  9142. static int TLSX_PostHandAuth_Parse(WOLFSSL* ssl, const byte* input,
  9143. word16 length, byte msgType)
  9144. {
  9145. (void)input;
  9146. if (msgType == client_hello) {
  9147. /* Ensure extension is empty. */
  9148. if (length != 0)
  9149. return BUFFER_E;
  9150. ssl->options.postHandshakeAuth = 1;
  9151. return 0;
  9152. }
  9153. WOLFSSL_ERROR_VERBOSE(SANITY_MSG_E);
  9154. return SANITY_MSG_E;
  9155. }
  9156. /* Create a new Post-handshake authentication object in the extensions.
  9157. *
  9158. * ssl The SSL/TLS object.
  9159. * returns 0 on success and other values indicate failure.
  9160. */
  9161. static int TLSX_PostHandAuth_Use(WOLFSSL* ssl)
  9162. {
  9163. int ret = 0;
  9164. TLSX* extension;
  9165. /* Find the PSK key exchange modes extension if it exists. */
  9166. extension = TLSX_Find(ssl->extensions, TLSX_POST_HANDSHAKE_AUTH);
  9167. if (extension == NULL) {
  9168. /* Push new Post-handshake Authentication extension. */
  9169. ret = TLSX_Push(&ssl->extensions, TLSX_POST_HANDSHAKE_AUTH, NULL,
  9170. ssl->heap);
  9171. if (ret != 0)
  9172. return ret;
  9173. }
  9174. return 0;
  9175. }
  9176. #define PHA_GET_SIZE TLSX_PostHandAuth_GetSize
  9177. #define PHA_WRITE TLSX_PostHandAuth_Write
  9178. #define PHA_PARSE TLSX_PostHandAuth_Parse
  9179. #else
  9180. #define PHA_GET_SIZE(a, b) 0
  9181. #define PHA_WRITE(a, b, c) 0
  9182. #define PHA_PARSE(a, b, c, d) 0
  9183. #endif
  9184. /******************************************************************************/
  9185. /* Early Data Indication */
  9186. /******************************************************************************/
  9187. #ifdef WOLFSSL_EARLY_DATA
  9188. /* Get the size of the encoded Early Data Indication extension.
  9189. * In messages: ClientHello, EncryptedExtensions and NewSessionTicket.
  9190. *
  9191. * msgType The type of the message this extension is being written into.
  9192. * returns the number of bytes of the encoded Early Data Indication extension.
  9193. */
  9194. static int TLSX_EarlyData_GetSize(byte msgType, word16* pSz)
  9195. {
  9196. int ret = 0;
  9197. if (msgType == client_hello || msgType == encrypted_extensions)
  9198. *pSz += 0;
  9199. else if (msgType == session_ticket)
  9200. *pSz += OPAQUE32_LEN;
  9201. else {
  9202. ret = SANITY_MSG_E;
  9203. WOLFSSL_ERROR_VERBOSE(ret);
  9204. }
  9205. return ret;
  9206. }
  9207. /* Writes the Early Data Indicator extension into the output buffer.
  9208. * Assumes that the the output buffer is big enough to hold data.
  9209. * In messages: ClientHello, EncryptedExtensions and NewSessionTicket.
  9210. *
  9211. * maxSz The maximum early data size.
  9212. * output The buffer to write into.
  9213. * msgType The type of the message this extension is being written into.
  9214. * returns the number of bytes written into the buffer.
  9215. */
  9216. static int TLSX_EarlyData_Write(word32 maxSz, byte* output, byte msgType,
  9217. word16* pSz)
  9218. {
  9219. if (msgType == client_hello || msgType == encrypted_extensions)
  9220. return 0;
  9221. else if (msgType == session_ticket) {
  9222. c32toa(maxSz, output);
  9223. *pSz += OPAQUE32_LEN;
  9224. return 0;
  9225. }
  9226. WOLFSSL_ERROR_VERBOSE(SANITY_MSG_E);
  9227. return SANITY_MSG_E;
  9228. }
  9229. /* Parse the Early Data Indicator extension.
  9230. * In messages: ClientHello, EncryptedExtensions and NewSessionTicket.
  9231. *
  9232. * ssl The SSL/TLS object.
  9233. * input The extension data.
  9234. * length The length of the extension data.
  9235. * msgType The type of the message this extension is being parsed from.
  9236. * returns 0 on success and other values indicate failure.
  9237. */
  9238. static int TLSX_EarlyData_Parse(WOLFSSL* ssl, const byte* input, word16 length,
  9239. byte msgType)
  9240. {
  9241. if (msgType == client_hello) {
  9242. if (length != 0)
  9243. return BUFFER_E;
  9244. if (ssl->earlyData == expecting_early_data) {
  9245. if (ssl->options.maxEarlyDataSz != 0)
  9246. ssl->earlyDataStatus = WOLFSSL_EARLY_DATA_ACCEPTED;
  9247. else
  9248. ssl->earlyDataStatus = WOLFSSL_EARLY_DATA_REJECTED;
  9249. return TLSX_EarlyData_Use(ssl, 0, 0);
  9250. }
  9251. ssl->earlyData = early_data_ext;
  9252. return 0;
  9253. }
  9254. if (msgType == encrypted_extensions) {
  9255. if (length != 0)
  9256. return BUFFER_E;
  9257. /* Ensure the index of PSK identity chosen by server is 0.
  9258. * Index is plus one to handle 'not set' value of 0.
  9259. */
  9260. if (ssl->options.pskIdIndex != 1) {
  9261. WOLFSSL_ERROR_VERBOSE(PSK_KEY_ERROR);
  9262. return PSK_KEY_ERROR;
  9263. }
  9264. if (ssl->options.side == WOLFSSL_CLIENT_END) {
  9265. /* the extension from server comes in */
  9266. ssl->earlyDataStatus = WOLFSSL_EARLY_DATA_ACCEPTED;
  9267. }
  9268. return TLSX_EarlyData_Use(ssl, 1, 1);
  9269. }
  9270. if (msgType == session_ticket) {
  9271. word32 maxSz;
  9272. if (length != OPAQUE32_LEN)
  9273. return BUFFER_E;
  9274. ato32(input, &maxSz);
  9275. ssl->session->maxEarlyDataSz = maxSz;
  9276. return 0;
  9277. }
  9278. WOLFSSL_ERROR_VERBOSE(SANITY_MSG_E);
  9279. return SANITY_MSG_E;
  9280. }
  9281. /* Use the data to create a new Early Data object in the extensions.
  9282. *
  9283. * ssl The SSL/TLS object.
  9284. * maxSz The maximum early data size.
  9285. * is_response if this extension is part of a response
  9286. * returns 0 on success and other values indicate failure.
  9287. */
  9288. int TLSX_EarlyData_Use(WOLFSSL* ssl, word32 maxSz, int is_response)
  9289. {
  9290. int ret = 0;
  9291. TLSX* extension;
  9292. /* Find the early data extension if it exists. */
  9293. extension = TLSX_Find(ssl->extensions, TLSX_EARLY_DATA);
  9294. if (extension == NULL) {
  9295. /* Push new early data extension. */
  9296. ret = TLSX_Push(&ssl->extensions, TLSX_EARLY_DATA, NULL, ssl->heap);
  9297. if (ret != 0)
  9298. return ret;
  9299. extension = TLSX_Find(ssl->extensions, TLSX_EARLY_DATA);
  9300. if (extension == NULL)
  9301. return MEMORY_E;
  9302. }
  9303. extension->resp = is_response;
  9304. extension->val = maxSz;
  9305. return 0;
  9306. }
  9307. #define EDI_GET_SIZE TLSX_EarlyData_GetSize
  9308. #define EDI_WRITE TLSX_EarlyData_Write
  9309. #define EDI_PARSE TLSX_EarlyData_Parse
  9310. #else
  9311. #define EDI_GET_SIZE(a, b) 0
  9312. #define EDI_WRITE(a, b, c, d) 0
  9313. #define EDI_PARSE(a, b, c, d) 0
  9314. #endif
  9315. /******************************************************************************/
  9316. /* QUIC transport parameter extension */
  9317. /******************************************************************************/
  9318. #ifdef WOLFSSL_QUIC
  9319. static word16 TLSX_QuicTP_GetSize(TLSX* extension)
  9320. {
  9321. const QuicTransportParam *tp = (QuicTransportParam*)extension->data;
  9322. return tp ? tp->len : 0;
  9323. }
  9324. int TLSX_QuicTP_Use(WOLFSSL* ssl, TLSX_Type ext_type, int is_response)
  9325. {
  9326. int ret = 0;
  9327. TLSX* extension;
  9328. WOLFSSL_ENTER("TLSX_QuicTP_Use");
  9329. if (ssl->quic.transport_local == NULL) {
  9330. /* RFC9000, ch 7.3: "An endpoint MUST treat the absence of [...]
  9331. * from either endpoint [...] as a connection error of type
  9332. * TRANSPORT_PARAMETER_ERROR."
  9333. */
  9334. ret = QUIC_TP_MISSING_E;
  9335. goto cleanup;
  9336. }
  9337. extension = TLSX_Find(ssl->extensions, ext_type);
  9338. if (extension == NULL) {
  9339. ret = TLSX_Push(&ssl->extensions, ext_type, NULL, ssl->heap);
  9340. if (ret != 0)
  9341. goto cleanup;
  9342. extension = TLSX_Find(ssl->extensions, ext_type);
  9343. if (extension == NULL) {
  9344. ret = MEMORY_E;
  9345. goto cleanup;
  9346. }
  9347. }
  9348. if (extension->data) {
  9349. QuicTransportParam_free((QuicTransportParam*)extension->data, ssl->heap);
  9350. extension->data = NULL;
  9351. }
  9352. extension->resp = is_response;
  9353. extension->data = (void*)QuicTransportParam_dup(ssl->quic.transport_local, ssl->heap);
  9354. if (!extension->data) {
  9355. ret = MEMORY_E;
  9356. goto cleanup;
  9357. }
  9358. cleanup:
  9359. WOLFSSL_LEAVE("TLSX_QuicTP_Use", ret);
  9360. return ret;
  9361. }
  9362. static word16 TLSX_QuicTP_Write(QuicTransportParam *tp, byte* output)
  9363. {
  9364. word16 len = 0;
  9365. WOLFSSL_ENTER("TLSX_QuicTP_Write");
  9366. if (tp && tp->len) {
  9367. XMEMCPY(output, tp->data, tp->len);
  9368. len = tp->len;
  9369. }
  9370. WOLFSSL_LEAVE("TLSX_QuicTP_Write", len);
  9371. return len;
  9372. }
  9373. static int TLSX_QuicTP_Parse(WOLFSSL *ssl, const byte *input, size_t len, int ext_type, int msgType)
  9374. {
  9375. const QuicTransportParam *tp, **ptp;
  9376. (void)msgType;
  9377. tp = QuicTransportParam_new(input, len, ssl->heap);
  9378. if (!tp) {
  9379. return MEMORY_E;
  9380. }
  9381. ptp = (ext_type == TLSX_KEY_QUIC_TP_PARAMS_DRAFT) ?
  9382. &ssl->quic.transport_peer_draft : &ssl->quic.transport_peer;
  9383. if (*ptp) {
  9384. QTP_FREE(*ptp, ssl->heap);
  9385. }
  9386. *ptp = tp;
  9387. return 0;
  9388. }
  9389. #define QTP_GET_SIZE TLSX_QuicTP_GetSize
  9390. #define QTP_USE TLSX_QuicTP_Use
  9391. #define QTP_WRITE TLSX_QuicTP_Write
  9392. #define QTP_PARSE TLSX_QuicTP_Parse
  9393. #endif /* WOLFSSL_QUIC */
  9394. #if defined(WOLFSSL_DTLS_CID)
  9395. #define CID_GET_SIZE TLSX_ConnectionID_GetSize
  9396. #define CID_WRITE TLSX_ConnectionID_Write
  9397. #define CID_PARSE TLSX_ConnectionID_Parse
  9398. #define CID_FREE TLSX_ConnectionID_Free
  9399. #else
  9400. #define CID_GET_SIZE(a) 0
  9401. #define CID_WRITE(a, b) 0
  9402. #define CID_PARSE(a, b, c, d) 0
  9403. #define CID_FREE(a, b) 0
  9404. #endif /* defined(WOLFSSL_DTLS_CID) */
  9405. /******************************************************************************/
  9406. /* TLS Extensions Framework */
  9407. /******************************************************************************/
  9408. /** Finds an extension in the provided list. */
  9409. TLSX* TLSX_Find(TLSX* list, TLSX_Type type)
  9410. {
  9411. TLSX* extension = list;
  9412. while (extension && extension->type != type)
  9413. extension = extension->next;
  9414. return extension;
  9415. }
  9416. /** Remove an extension. */
  9417. void TLSX_Remove(TLSX** list, TLSX_Type type, void* heap)
  9418. {
  9419. TLSX* extension = *list;
  9420. TLSX** next = list;
  9421. while (extension && extension->type != type) {
  9422. next = &extension->next;
  9423. extension = extension->next;
  9424. }
  9425. if (extension) {
  9426. *next = extension->next;
  9427. extension->next = NULL;
  9428. TLSX_FreeAll(extension, heap);
  9429. }
  9430. }
  9431. /** Releases all extensions in the provided list. */
  9432. void TLSX_FreeAll(TLSX* list, void* heap)
  9433. {
  9434. TLSX* extension;
  9435. while ((extension = list)) {
  9436. list = extension->next;
  9437. switch (extension->type) {
  9438. #ifdef HAVE_SNI
  9439. case TLSX_SERVER_NAME:
  9440. SNI_FREE_ALL((SNI*)extension->data, heap);
  9441. break;
  9442. #endif
  9443. case TLSX_TRUSTED_CA_KEYS:
  9444. TCA_FREE_ALL((TCA*)extension->data, heap);
  9445. break;
  9446. case TLSX_MAX_FRAGMENT_LENGTH:
  9447. MFL_FREE_ALL(extension->data, heap);
  9448. break;
  9449. case TLSX_EXTENDED_MASTER_SECRET:
  9450. case TLSX_TRUNCATED_HMAC:
  9451. /* Nothing to do. */
  9452. break;
  9453. case TLSX_SUPPORTED_GROUPS:
  9454. EC_FREE_ALL((SupportedCurve*)extension->data, heap);
  9455. break;
  9456. case TLSX_EC_POINT_FORMATS:
  9457. PF_FREE_ALL((PointFormat*)extension->data, heap);
  9458. break;
  9459. case TLSX_STATUS_REQUEST:
  9460. CSR_FREE_ALL((CertificateStatusRequest*)extension->data, heap);
  9461. break;
  9462. case TLSX_STATUS_REQUEST_V2:
  9463. CSR2_FREE_ALL((CertificateStatusRequestItemV2*)extension->data,
  9464. heap);
  9465. break;
  9466. case TLSX_RENEGOTIATION_INFO:
  9467. SCR_FREE_ALL(extension->data, heap);
  9468. break;
  9469. case TLSX_SESSION_TICKET:
  9470. WOLF_STK_FREE(extension->data, heap);
  9471. break;
  9472. case TLSX_APPLICATION_LAYER_PROTOCOL:
  9473. ALPN_FREE_ALL((ALPN*)extension->data, heap);
  9474. break;
  9475. #if !defined(NO_CERTS) && !defined(WOLFSSL_NO_SIGALG)
  9476. case TLSX_SIGNATURE_ALGORITHMS:
  9477. break;
  9478. #endif
  9479. #if defined(HAVE_ENCRYPT_THEN_MAC) && !defined(WOLFSSL_AEAD_ONLY)
  9480. case TLSX_ENCRYPT_THEN_MAC:
  9481. break;
  9482. #endif
  9483. #ifdef WOLFSSL_TLS13
  9484. case TLSX_SUPPORTED_VERSIONS:
  9485. break;
  9486. #ifdef WOLFSSL_SEND_HRR_COOKIE
  9487. case TLSX_COOKIE:
  9488. CKE_FREE_ALL((Cookie*)extension->data, heap);
  9489. break;
  9490. #endif
  9491. #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
  9492. case TLSX_PRE_SHARED_KEY:
  9493. PSK_FREE_ALL((PreSharedKey*)extension->data, heap);
  9494. break;
  9495. case TLSX_PSK_KEY_EXCHANGE_MODES:
  9496. break;
  9497. #endif
  9498. #ifdef WOLFSSL_EARLY_DATA
  9499. case TLSX_EARLY_DATA:
  9500. break;
  9501. #endif
  9502. #ifdef WOLFSSL_POST_HANDSHAKE_AUTH
  9503. case TLSX_POST_HANDSHAKE_AUTH:
  9504. break;
  9505. #endif
  9506. #if !defined(NO_CERTS) && !defined(WOLFSSL_NO_SIGALG)
  9507. case TLSX_SIGNATURE_ALGORITHMS_CERT:
  9508. break;
  9509. #endif
  9510. case TLSX_KEY_SHARE:
  9511. KS_FREE_ALL((KeyShareEntry*)extension->data, heap);
  9512. break;
  9513. #endif
  9514. #ifdef WOLFSSL_SRTP
  9515. case TLSX_USE_SRTP:
  9516. SRTP_FREE((TlsxSrtp*)extension->data, heap);
  9517. break;
  9518. #endif
  9519. #ifdef WOLFSSL_QUIC
  9520. case TLSX_KEY_QUIC_TP_PARAMS:
  9521. FALL_THROUGH;
  9522. case TLSX_KEY_QUIC_TP_PARAMS_DRAFT:
  9523. QTP_FREE((QuicTransportParam*)extension->data, heap);
  9524. break;
  9525. #endif
  9526. #ifdef WOLFSSL_DTLS_CID
  9527. case TLSX_CONNECTION_ID:
  9528. CID_FREE((byte*)extension->data, heap);
  9529. break;
  9530. #endif /* WOLFSSL_DTLS_CID */
  9531. default:
  9532. break;
  9533. }
  9534. XFREE(extension, heap, DYNAMIC_TYPE_TLSX);
  9535. }
  9536. (void)heap;
  9537. }
  9538. /** Checks if the tls extensions are supported based on the protocol version. */
  9539. int TLSX_SupportExtensions(WOLFSSL* ssl) {
  9540. return ssl && (IsTLS(ssl) || ssl->version.major == DTLS_MAJOR);
  9541. }
  9542. /** Tells the buffered size of the extensions in a list. */
  9543. static int TLSX_GetSize(TLSX* list, byte* semaphore, byte msgType,
  9544. word16* pLength)
  9545. {
  9546. int ret = 0;
  9547. TLSX* extension;
  9548. word16 length = 0;
  9549. byte isRequest = (msgType == client_hello ||
  9550. msgType == certificate_request);
  9551. while ((extension = list)) {
  9552. list = extension->next;
  9553. /* only extensions marked as response are sent back to the client. */
  9554. if (!isRequest && !extension->resp)
  9555. continue; /* skip! */
  9556. /* ssl level extensions are expected to override ctx level ones. */
  9557. if (!IS_OFF(semaphore, TLSX_ToSemaphore(extension->type)))
  9558. continue; /* skip! */
  9559. /* extension type + extension data length. */
  9560. length += HELLO_EXT_TYPE_SZ + OPAQUE16_LEN;
  9561. switch (extension->type) {
  9562. #ifdef HAVE_SNI
  9563. case TLSX_SERVER_NAME:
  9564. /* SNI only sends the name on the request. */
  9565. if (isRequest)
  9566. length += SNI_GET_SIZE((SNI*)extension->data);
  9567. break;
  9568. #endif
  9569. case TLSX_TRUSTED_CA_KEYS:
  9570. /* TCA only sends the list on the request. */
  9571. if (isRequest)
  9572. length += TCA_GET_SIZE((TCA*)extension->data);
  9573. break;
  9574. case TLSX_MAX_FRAGMENT_LENGTH:
  9575. length += MFL_GET_SIZE(extension->data);
  9576. break;
  9577. case TLSX_EXTENDED_MASTER_SECRET:
  9578. case TLSX_TRUNCATED_HMAC:
  9579. /* always empty. */
  9580. break;
  9581. case TLSX_SUPPORTED_GROUPS:
  9582. length += EC_GET_SIZE((SupportedCurve*)extension->data);
  9583. break;
  9584. case TLSX_EC_POINT_FORMATS:
  9585. length += PF_GET_SIZE((PointFormat*)extension->data);
  9586. break;
  9587. case TLSX_STATUS_REQUEST:
  9588. length += CSR_GET_SIZE(
  9589. (CertificateStatusRequest*)extension->data, isRequest);
  9590. break;
  9591. case TLSX_STATUS_REQUEST_V2:
  9592. length += CSR2_GET_SIZE(
  9593. (CertificateStatusRequestItemV2*)extension->data,
  9594. isRequest);
  9595. break;
  9596. case TLSX_RENEGOTIATION_INFO:
  9597. length += SCR_GET_SIZE((SecureRenegotiation*)extension->data,
  9598. isRequest);
  9599. break;
  9600. case TLSX_SESSION_TICKET:
  9601. length += WOLF_STK_GET_SIZE((SessionTicket*)extension->data,
  9602. isRequest);
  9603. break;
  9604. case TLSX_APPLICATION_LAYER_PROTOCOL:
  9605. length += ALPN_GET_SIZE((ALPN*)extension->data);
  9606. break;
  9607. #if !defined(NO_CERTS) && !defined(WOLFSSL_NO_SIGALG)
  9608. case TLSX_SIGNATURE_ALGORITHMS:
  9609. length += SA_GET_SIZE(extension->data);
  9610. break;
  9611. #endif
  9612. #if defined(HAVE_ENCRYPT_THEN_MAC) && !defined(WOLFSSL_AEAD_ONLY)
  9613. case TLSX_ENCRYPT_THEN_MAC:
  9614. ret = ETM_GET_SIZE(msgType, &length);
  9615. break;
  9616. #endif /* HAVE_ENCRYPT_THEN_MAC */
  9617. #ifdef WOLFSSL_TLS13
  9618. case TLSX_SUPPORTED_VERSIONS:
  9619. ret = SV_GET_SIZE(extension->data, msgType, &length);
  9620. break;
  9621. #ifdef WOLFSSL_SEND_HRR_COOKIE
  9622. case TLSX_COOKIE:
  9623. ret = CKE_GET_SIZE((Cookie*)extension->data, msgType, &length);
  9624. break;
  9625. #endif
  9626. #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
  9627. case TLSX_PRE_SHARED_KEY:
  9628. ret = PSK_GET_SIZE((PreSharedKey*)extension->data, msgType,
  9629. &length);
  9630. break;
  9631. case TLSX_PSK_KEY_EXCHANGE_MODES:
  9632. ret = PKM_GET_SIZE((byte)extension->val, msgType, &length);
  9633. break;
  9634. #endif
  9635. #ifdef WOLFSSL_EARLY_DATA
  9636. case TLSX_EARLY_DATA:
  9637. ret = EDI_GET_SIZE(msgType, &length);
  9638. break;
  9639. #endif
  9640. #ifdef WOLFSSL_POST_HANDSHAKE_AUTH
  9641. case TLSX_POST_HANDSHAKE_AUTH:
  9642. ret = PHA_GET_SIZE(msgType, &length);
  9643. break;
  9644. #endif
  9645. #if !defined(NO_CERTS) && !defined(WOLFSSL_NO_SIGALG)
  9646. case TLSX_SIGNATURE_ALGORITHMS_CERT:
  9647. length += SAC_GET_SIZE(extension->data);
  9648. break;
  9649. #endif
  9650. case TLSX_KEY_SHARE:
  9651. length += KS_GET_SIZE((KeyShareEntry*)extension->data, msgType);
  9652. break;
  9653. #endif
  9654. #ifdef WOLFSSL_SRTP
  9655. case TLSX_USE_SRTP:
  9656. length += SRTP_GET_SIZE((TlsxSrtp*)extension->data);
  9657. break;
  9658. #endif
  9659. #ifdef WOLFSSL_QUIC
  9660. case TLSX_KEY_QUIC_TP_PARAMS:
  9661. FALL_THROUGH; /* followed by */
  9662. case TLSX_KEY_QUIC_TP_PARAMS_DRAFT:
  9663. length += QTP_GET_SIZE(extension);
  9664. break;
  9665. #endif
  9666. #ifdef WOLFSSL_DTLS_CID
  9667. case TLSX_CONNECTION_ID:
  9668. length += CID_GET_SIZE((byte*)extension->data);
  9669. break;
  9670. #endif /* WOLFSSL_DTLS_CID */
  9671. default:
  9672. break;
  9673. }
  9674. /* marks the extension as processed so ctx level */
  9675. /* extensions don't overlap with ssl level ones. */
  9676. TURN_ON(semaphore, TLSX_ToSemaphore(extension->type));
  9677. }
  9678. *pLength += length;
  9679. return ret;
  9680. }
  9681. /** Writes the extensions of a list in a buffer. */
  9682. static int TLSX_Write(TLSX* list, byte* output, byte* semaphore,
  9683. byte msgType, word16* pOffset)
  9684. {
  9685. int ret = 0;
  9686. TLSX* extension;
  9687. word16 offset = 0;
  9688. word16 length_offset = 0;
  9689. byte isRequest = (msgType == client_hello ||
  9690. msgType == certificate_request);
  9691. while ((extension = list)) {
  9692. list = extension->next;
  9693. /* only extensions marked as response are written in a response. */
  9694. if (!isRequest && !extension->resp)
  9695. continue; /* skip! */
  9696. /* ssl level extensions are expected to override ctx level ones. */
  9697. if (!IS_OFF(semaphore, TLSX_ToSemaphore(extension->type)))
  9698. continue; /* skip! */
  9699. /* writes extension type. */
  9700. c16toa(extension->type, output + offset);
  9701. offset += HELLO_EXT_TYPE_SZ + OPAQUE16_LEN;
  9702. length_offset = offset;
  9703. /* extension data should be written internally. */
  9704. switch (extension->type) {
  9705. #ifdef HAVE_SNI
  9706. case TLSX_SERVER_NAME:
  9707. if (isRequest) {
  9708. WOLFSSL_MSG("SNI extension to write");
  9709. offset += SNI_WRITE((SNI*)extension->data, output + offset);
  9710. }
  9711. break;
  9712. #endif
  9713. case TLSX_TRUSTED_CA_KEYS:
  9714. WOLFSSL_MSG("Trusted CA Indication extension to write");
  9715. if (isRequest) {
  9716. offset += TCA_WRITE((TCA*)extension->data, output + offset);
  9717. }
  9718. break;
  9719. case TLSX_MAX_FRAGMENT_LENGTH:
  9720. WOLFSSL_MSG("Max Fragment Length extension to write");
  9721. offset += MFL_WRITE((byte*)extension->data, output + offset);
  9722. break;
  9723. case TLSX_EXTENDED_MASTER_SECRET:
  9724. WOLFSSL_MSG("Extended Master Secret");
  9725. /* always empty. */
  9726. break;
  9727. case TLSX_TRUNCATED_HMAC:
  9728. WOLFSSL_MSG("Truncated HMAC extension to write");
  9729. /* always empty. */
  9730. break;
  9731. case TLSX_SUPPORTED_GROUPS:
  9732. WOLFSSL_MSG("Supported Groups extension to write");
  9733. offset += EC_WRITE((SupportedCurve*)extension->data,
  9734. output + offset);
  9735. break;
  9736. case TLSX_EC_POINT_FORMATS:
  9737. WOLFSSL_MSG("Point Formats extension to write");
  9738. offset += PF_WRITE((PointFormat*)extension->data,
  9739. output + offset);
  9740. break;
  9741. case TLSX_STATUS_REQUEST:
  9742. WOLFSSL_MSG("Certificate Status Request extension to write");
  9743. offset += CSR_WRITE((CertificateStatusRequest*)extension->data,
  9744. output + offset, isRequest);
  9745. break;
  9746. case TLSX_STATUS_REQUEST_V2:
  9747. WOLFSSL_MSG("Certificate Status Request v2 extension to write");
  9748. offset += CSR2_WRITE(
  9749. (CertificateStatusRequestItemV2*)extension->data,
  9750. output + offset, isRequest);
  9751. break;
  9752. case TLSX_RENEGOTIATION_INFO:
  9753. WOLFSSL_MSG("Secure Renegotiation extension to write");
  9754. offset += SCR_WRITE((SecureRenegotiation*)extension->data,
  9755. output + offset, isRequest);
  9756. break;
  9757. case TLSX_SESSION_TICKET:
  9758. WOLFSSL_MSG("Session Ticket extension to write");
  9759. offset += WOLF_STK_WRITE((SessionTicket*)extension->data,
  9760. output + offset, isRequest);
  9761. break;
  9762. case TLSX_APPLICATION_LAYER_PROTOCOL:
  9763. WOLFSSL_MSG("ALPN extension to write");
  9764. offset += ALPN_WRITE((ALPN*)extension->data, output + offset);
  9765. break;
  9766. #if !defined(NO_CERTS) && !defined(WOLFSSL_NO_SIGALG)
  9767. case TLSX_SIGNATURE_ALGORITHMS:
  9768. WOLFSSL_MSG("Signature Algorithms extension to write");
  9769. offset += SA_WRITE(extension->data, output + offset);
  9770. break;
  9771. #endif
  9772. #if defined(HAVE_ENCRYPT_THEN_MAC) && !defined(WOLFSSL_AEAD_ONLY)
  9773. case TLSX_ENCRYPT_THEN_MAC:
  9774. WOLFSSL_MSG("Encrypt-Then-Mac extension to write");
  9775. ret = ETM_WRITE(extension->data, output, msgType, &offset);
  9776. break;
  9777. #endif /* HAVE_ENCRYPT_THEN_MAC */
  9778. #ifdef WOLFSSL_TLS13
  9779. case TLSX_SUPPORTED_VERSIONS:
  9780. WOLFSSL_MSG("Supported Versions extension to write");
  9781. ret = SV_WRITE(extension->data, output + offset, msgType, &offset);
  9782. break;
  9783. #ifdef WOLFSSL_SEND_HRR_COOKIE
  9784. case TLSX_COOKIE:
  9785. WOLFSSL_MSG("Cookie extension to write");
  9786. ret = CKE_WRITE((Cookie*)extension->data, output + offset,
  9787. msgType, &offset);
  9788. break;
  9789. #endif
  9790. #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
  9791. case TLSX_PRE_SHARED_KEY:
  9792. WOLFSSL_MSG("Pre-Shared Key extension to write");
  9793. ret = PSK_WRITE((PreSharedKey*)extension->data, output + offset,
  9794. msgType, &offset);
  9795. break;
  9796. case TLSX_PSK_KEY_EXCHANGE_MODES:
  9797. WOLFSSL_MSG("PSK Key Exchange Modes extension to write");
  9798. ret = PKM_WRITE((byte)extension->val, output + offset, msgType,
  9799. &offset);
  9800. break;
  9801. #endif
  9802. #ifdef WOLFSSL_EARLY_DATA
  9803. case TLSX_EARLY_DATA:
  9804. WOLFSSL_MSG("Early Data extension to write");
  9805. ret = EDI_WRITE(extension->val, output + offset, msgType,
  9806. &offset);
  9807. break;
  9808. #endif
  9809. #ifdef WOLFSSL_POST_HANDSHAKE_AUTH
  9810. case TLSX_POST_HANDSHAKE_AUTH:
  9811. WOLFSSL_MSG("Post-Handshake Authentication extension to write");
  9812. ret = PHA_WRITE(output + offset, msgType, &offset);
  9813. break;
  9814. #endif
  9815. #if !defined(NO_CERTS) && !defined(WOLFSSL_NO_SIGALG)
  9816. case TLSX_SIGNATURE_ALGORITHMS_CERT:
  9817. WOLFSSL_MSG("Signature Algorithms extension to write");
  9818. offset += SAC_WRITE(extension->data, output + offset);
  9819. break;
  9820. #endif
  9821. case TLSX_KEY_SHARE:
  9822. WOLFSSL_MSG("Key Share extension to write");
  9823. offset += KS_WRITE((KeyShareEntry*)extension->data,
  9824. output + offset, msgType);
  9825. break;
  9826. #endif
  9827. #ifdef WOLFSSL_SRTP
  9828. case TLSX_USE_SRTP:
  9829. offset += SRTP_WRITE((TlsxSrtp*)extension->data, output+offset);
  9830. break;
  9831. #endif
  9832. #ifdef WOLFSSL_QUIC
  9833. case TLSX_KEY_QUIC_TP_PARAMS:
  9834. FALL_THROUGH;
  9835. case TLSX_KEY_QUIC_TP_PARAMS_DRAFT:
  9836. WOLFSSL_MSG("QUIC transport parameter to write");
  9837. offset += QTP_WRITE((QuicTransportParam*)extension->data,
  9838. output + offset);
  9839. break;
  9840. #endif
  9841. #ifdef WOLFSSL_DTLS_CID
  9842. case TLSX_CONNECTION_ID:
  9843. offset += CID_WRITE((byte*)extension->data, output+offset);
  9844. break;
  9845. #endif /* WOLFSSL_DTLS_CID */
  9846. default:
  9847. break;
  9848. }
  9849. /* writes extension data length. */
  9850. c16toa(offset - length_offset, output + length_offset - OPAQUE16_LEN);
  9851. /* marks the extension as processed so ctx level */
  9852. /* extensions don't overlap with ssl level ones. */
  9853. TURN_ON(semaphore, TLSX_ToSemaphore(extension->type));
  9854. }
  9855. *pOffset += offset;
  9856. return ret;
  9857. }
  9858. #ifdef HAVE_SUPPORTED_CURVES
  9859. /* Populates the default supported groups / curves */
  9860. static int TLSX_PopulateSupportedGroups(WOLFSSL* ssl, TLSX** extensions)
  9861. {
  9862. int ret = WOLFSSL_SUCCESS;
  9863. #ifdef WOLFSSL_TLS13
  9864. #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
  9865. if (ssl->options.resuming && ssl->session->namedGroup != 0) {
  9866. return TLSX_UseSupportedCurve(extensions, ssl->session->namedGroup,
  9867. ssl->heap);
  9868. }
  9869. #endif
  9870. if (ssl->numGroups != 0) {
  9871. int i;
  9872. for (i = 0; i < ssl->numGroups; i++) {
  9873. ret = TLSX_UseSupportedCurve(extensions, ssl->group[i], ssl->heap);
  9874. if (ret != WOLFSSL_SUCCESS)
  9875. return ret;
  9876. }
  9877. return WOLFSSL_SUCCESS;
  9878. }
  9879. #endif /* WOLFSSL_TLS13 */
  9880. #if defined(HAVE_ECC)
  9881. /* list in order by strength, since not all servers choose by strength */
  9882. #if (defined(HAVE_ECC521) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 521
  9883. #ifndef NO_ECC_SECP
  9884. ret = TLSX_UseSupportedCurve(extensions,
  9885. WOLFSSL_ECC_SECP521R1, ssl->heap);
  9886. if (ret != WOLFSSL_SUCCESS) return ret;
  9887. #endif
  9888. #endif
  9889. #if (defined(HAVE_ECC512) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 512
  9890. #ifdef HAVE_ECC_BRAINPOOL
  9891. ret = TLSX_UseSupportedCurve(extensions,
  9892. WOLFSSL_ECC_BRAINPOOLP512R1, ssl->heap);
  9893. if (ret != WOLFSSL_SUCCESS) return ret;
  9894. #endif
  9895. #endif
  9896. #if (defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 384
  9897. #ifndef NO_ECC_SECP
  9898. ret = TLSX_UseSupportedCurve(extensions,
  9899. WOLFSSL_ECC_SECP384R1, ssl->heap);
  9900. if (ret != WOLFSSL_SUCCESS) return ret;
  9901. #endif
  9902. #ifdef HAVE_ECC_BRAINPOOL
  9903. ret = TLSX_UseSupportedCurve(extensions,
  9904. WOLFSSL_ECC_BRAINPOOLP384R1, ssl->heap);
  9905. if (ret != WOLFSSL_SUCCESS) return ret;
  9906. #endif
  9907. #endif
  9908. #endif /* HAVE_ECC */
  9909. #ifndef HAVE_FIPS
  9910. #if defined(HAVE_CURVE448) && ECC_MIN_KEY_SZ <= 448
  9911. ret = TLSX_UseSupportedCurve(extensions,
  9912. WOLFSSL_ECC_X448, ssl->heap);
  9913. if (ret != WOLFSSL_SUCCESS) return ret;
  9914. #endif
  9915. #endif /* HAVE_FIPS */
  9916. #if defined(HAVE_ECC) && defined(HAVE_SUPPORTED_CURVES)
  9917. #if (!defined(NO_ECC256) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 256
  9918. #ifndef NO_ECC_SECP
  9919. ret = TLSX_UseSupportedCurve(extensions,
  9920. WOLFSSL_ECC_SECP256R1, ssl->heap);
  9921. if (ret != WOLFSSL_SUCCESS) return ret;
  9922. #endif
  9923. #ifdef HAVE_ECC_KOBLITZ
  9924. ret = TLSX_UseSupportedCurve(extensions,
  9925. WOLFSSL_ECC_SECP256K1, ssl->heap);
  9926. if (ret != WOLFSSL_SUCCESS) return ret;
  9927. #endif
  9928. #ifdef HAVE_ECC_BRAINPOOL
  9929. ret = TLSX_UseSupportedCurve(extensions,
  9930. WOLFSSL_ECC_BRAINPOOLP256R1, ssl->heap);
  9931. if (ret != WOLFSSL_SUCCESS) return ret;
  9932. #endif
  9933. #endif
  9934. #endif /* HAVE_ECC */
  9935. #ifndef HAVE_FIPS
  9936. #if defined(HAVE_CURVE25519) && ECC_MIN_KEY_SZ <= 256
  9937. ret = TLSX_UseSupportedCurve(extensions,
  9938. WOLFSSL_ECC_X25519, ssl->heap);
  9939. if (ret != WOLFSSL_SUCCESS) return ret;
  9940. #endif
  9941. #endif /* HAVE_FIPS */
  9942. #if defined(HAVE_ECC) && defined(HAVE_SUPPORTED_CURVES)
  9943. #if (defined(HAVE_ECC224) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 224
  9944. #ifndef NO_ECC_SECP
  9945. ret = TLSX_UseSupportedCurve(extensions,
  9946. WOLFSSL_ECC_SECP224R1, ssl->heap);
  9947. if (ret != WOLFSSL_SUCCESS) return ret;
  9948. #endif
  9949. #ifdef HAVE_ECC_KOBLITZ
  9950. ret = TLSX_UseSupportedCurve(extensions,
  9951. WOLFSSL_ECC_SECP224K1, ssl->heap);
  9952. if (ret != WOLFSSL_SUCCESS) return ret;
  9953. #endif
  9954. #endif
  9955. #ifndef HAVE_FIPS
  9956. #if (defined(HAVE_ECC192) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 192
  9957. #ifndef NO_ECC_SECP
  9958. ret = TLSX_UseSupportedCurve(extensions,
  9959. WOLFSSL_ECC_SECP192R1, ssl->heap);
  9960. if (ret != WOLFSSL_SUCCESS) return ret;
  9961. #endif
  9962. #ifdef HAVE_ECC_KOBLITZ
  9963. ret = TLSX_UseSupportedCurve(extensions,
  9964. WOLFSSL_ECC_SECP192K1, ssl->heap);
  9965. if (ret != WOLFSSL_SUCCESS) return ret;
  9966. #endif
  9967. #endif
  9968. #if (defined(HAVE_ECC160) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 160
  9969. #ifndef NO_ECC_SECP
  9970. ret = TLSX_UseSupportedCurve(extensions,
  9971. WOLFSSL_ECC_SECP160R1, ssl->heap);
  9972. if (ret != WOLFSSL_SUCCESS) return ret;
  9973. #endif
  9974. #ifdef HAVE_ECC_SECPR2
  9975. ret = TLSX_UseSupportedCurve(extensions,
  9976. WOLFSSL_ECC_SECP160R2, ssl->heap);
  9977. if (ret != WOLFSSL_SUCCESS) return ret;
  9978. #endif
  9979. #ifdef HAVE_ECC_KOBLITZ
  9980. ret = TLSX_UseSupportedCurve(extensions,
  9981. WOLFSSL_ECC_SECP160K1, ssl->heap);
  9982. if (ret != WOLFSSL_SUCCESS) return ret;
  9983. #endif
  9984. #endif
  9985. #endif /* HAVE_FIPS */
  9986. #endif /* HAVE_ECC */
  9987. #ifndef NO_DH
  9988. /* Add FFDHE supported groups. */
  9989. #ifdef HAVE_FFDHE_8192
  9990. if (8192/8 >= ssl->options.minDhKeySz &&
  9991. 8192/8 <= ssl->options.maxDhKeySz) {
  9992. ret = TLSX_UseSupportedCurve(extensions,
  9993. WOLFSSL_FFDHE_8192, ssl->heap);
  9994. if (ret != WOLFSSL_SUCCESS)
  9995. return ret;
  9996. }
  9997. #endif
  9998. #ifdef HAVE_FFDHE_6144
  9999. if (6144/8 >= ssl->options.minDhKeySz &&
  10000. 6144/8 <= ssl->options.maxDhKeySz) {
  10001. ret = TLSX_UseSupportedCurve(extensions,
  10002. WOLFSSL_FFDHE_6144, ssl->heap);
  10003. if (ret != WOLFSSL_SUCCESS)
  10004. return ret;
  10005. }
  10006. #endif
  10007. #ifdef HAVE_FFDHE_4096
  10008. if (4096/8 >= ssl->options.minDhKeySz &&
  10009. 4096/8 <= ssl->options.maxDhKeySz) {
  10010. ret = TLSX_UseSupportedCurve(extensions,
  10011. WOLFSSL_FFDHE_4096, ssl->heap);
  10012. if (ret != WOLFSSL_SUCCESS)
  10013. return ret;
  10014. }
  10015. #endif
  10016. #ifdef HAVE_FFDHE_3072
  10017. if (3072/8 >= ssl->options.minDhKeySz &&
  10018. 3072/8 <= ssl->options.maxDhKeySz) {
  10019. ret = TLSX_UseSupportedCurve(extensions,
  10020. WOLFSSL_FFDHE_3072, ssl->heap);
  10021. if (ret != WOLFSSL_SUCCESS)
  10022. return ret;
  10023. }
  10024. #endif
  10025. #ifdef HAVE_FFDHE_2048
  10026. if (2048/8 >= ssl->options.minDhKeySz &&
  10027. 2048/8 <= ssl->options.maxDhKeySz) {
  10028. ret = TLSX_UseSupportedCurve(extensions,
  10029. WOLFSSL_FFDHE_2048, ssl->heap);
  10030. if (ret != WOLFSSL_SUCCESS)
  10031. return ret;
  10032. }
  10033. #endif
  10034. #endif
  10035. #ifdef HAVE_PQC
  10036. #ifdef WOLFSSL_WC_KYBER
  10037. #ifdef WOLFSSL_KYBER512
  10038. if (ret == WOLFSSL_SUCCESS)
  10039. ret = TLSX_UseSupportedCurve(extensions, WOLFSSL_KYBER_LEVEL1,
  10040. ssl->heap);
  10041. #endif
  10042. #ifdef WOLFSSL_KYBER768
  10043. if (ret == WOLFSSL_SUCCESS)
  10044. ret = TLSX_UseSupportedCurve(extensions, WOLFSSL_KYBER_LEVEL3,
  10045. ssl->heap);
  10046. #endif
  10047. #ifdef WOLFSSL_KYBER768
  10048. if (ret == WOLFSSL_SUCCESS)
  10049. ret = TLSX_UseSupportedCurve(extensions, WOLFSSL_KYBER_LEVEL5,
  10050. ssl->heap);
  10051. #endif
  10052. #elif defined(HAVE_LIBOQS)
  10053. ret = TLSX_UseSupportedCurve(extensions, WOLFSSL_KYBER_LEVEL1, ssl->heap);
  10054. if (ret == WOLFSSL_SUCCESS)
  10055. ret = TLSX_UseSupportedCurve(extensions, WOLFSSL_KYBER_LEVEL3,
  10056. ssl->heap);
  10057. if (ret == WOLFSSL_SUCCESS)
  10058. ret = TLSX_UseSupportedCurve(extensions, WOLFSSL_KYBER_LEVEL5,
  10059. ssl->heap);
  10060. if (ret == WOLFSSL_SUCCESS)
  10061. ret = TLSX_UseSupportedCurve(extensions, WOLFSSL_NTRU_HPS_LEVEL1,
  10062. ssl->heap);
  10063. if (ret == WOLFSSL_SUCCESS)
  10064. ret = TLSX_UseSupportedCurve(extensions, WOLFSSL_NTRU_HPS_LEVEL3,
  10065. ssl->heap);
  10066. if (ret == WOLFSSL_SUCCESS)
  10067. ret = TLSX_UseSupportedCurve(extensions, WOLFSSL_NTRU_HPS_LEVEL5,
  10068. ssl->heap);
  10069. if (ret == WOLFSSL_SUCCESS)
  10070. ret = TLSX_UseSupportedCurve(extensions, WOLFSSL_NTRU_HRSS_LEVEL3,
  10071. ssl->heap);
  10072. if (ret == WOLFSSL_SUCCESS)
  10073. ret = TLSX_UseSupportedCurve(extensions, WOLFSSL_SABER_LEVEL1,
  10074. ssl->heap);
  10075. if (ret == WOLFSSL_SUCCESS)
  10076. ret = TLSX_UseSupportedCurve(extensions, WOLFSSL_SABER_LEVEL3,
  10077. ssl->heap);
  10078. if (ret == WOLFSSL_SUCCESS)
  10079. ret = TLSX_UseSupportedCurve(extensions, WOLFSSL_SABER_LEVEL5,
  10080. ssl->heap);
  10081. if (ret == WOLFSSL_SUCCESS)
  10082. ret = TLSX_UseSupportedCurve(extensions, WOLFSSL_KYBER_90S_LEVEL1,
  10083. ssl->heap);
  10084. if (ret == WOLFSSL_SUCCESS)
  10085. ret = TLSX_UseSupportedCurve(extensions, WOLFSSL_KYBER_90S_LEVEL3,
  10086. ssl->heap);
  10087. if (ret == WOLFSSL_SUCCESS)
  10088. ret = TLSX_UseSupportedCurve(extensions, WOLFSSL_KYBER_90S_LEVEL5,
  10089. ssl->heap);
  10090. if (ret == WOLFSSL_SUCCESS)
  10091. ret = TLSX_UseSupportedCurve(extensions, WOLFSSL_P256_NTRU_HPS_LEVEL1,
  10092. ssl->heap);
  10093. if (ret == WOLFSSL_SUCCESS)
  10094. ret = TLSX_UseSupportedCurve(extensions, WOLFSSL_P384_NTRU_HPS_LEVEL3,
  10095. ssl->heap);
  10096. if (ret == WOLFSSL_SUCCESS)
  10097. ret = TLSX_UseSupportedCurve(extensions, WOLFSSL_P521_NTRU_HPS_LEVEL5,
  10098. ssl->heap);
  10099. if (ret == WOLFSSL_SUCCESS)
  10100. ret = TLSX_UseSupportedCurve(extensions, WOLFSSL_P384_NTRU_HRSS_LEVEL3,
  10101. ssl->heap);
  10102. if (ret == WOLFSSL_SUCCESS)
  10103. ret = TLSX_UseSupportedCurve(extensions, WOLFSSL_P256_SABER_LEVEL1,
  10104. ssl->heap);
  10105. if (ret == WOLFSSL_SUCCESS)
  10106. ret = TLSX_UseSupportedCurve(extensions, WOLFSSL_P384_SABER_LEVEL3,
  10107. ssl->heap);
  10108. if (ret == WOLFSSL_SUCCESS)
  10109. ret = TLSX_UseSupportedCurve(extensions, WOLFSSL_P521_SABER_LEVEL5,
  10110. ssl->heap);
  10111. if (ret == WOLFSSL_SUCCESS)
  10112. ret = TLSX_UseSupportedCurve(extensions, WOLFSSL_P256_KYBER_LEVEL1,
  10113. ssl->heap);
  10114. if (ret == WOLFSSL_SUCCESS)
  10115. ret = TLSX_UseSupportedCurve(extensions, WOLFSSL_P384_KYBER_LEVEL3,
  10116. ssl->heap);
  10117. if (ret == WOLFSSL_SUCCESS)
  10118. ret = TLSX_UseSupportedCurve(extensions, WOLFSSL_P521_KYBER_LEVEL5,
  10119. ssl->heap);
  10120. if (ret == WOLFSSL_SUCCESS)
  10121. ret = TLSX_UseSupportedCurve(extensions, WOLFSSL_P256_KYBER_90S_LEVEL1,
  10122. ssl->heap);
  10123. if (ret == WOLFSSL_SUCCESS)
  10124. ret = TLSX_UseSupportedCurve(extensions, WOLFSSL_P384_KYBER_90S_LEVEL3,
  10125. ssl->heap);
  10126. if (ret == WOLFSSL_SUCCESS)
  10127. ret = TLSX_UseSupportedCurve(extensions, WOLFSSL_P521_KYBER_90S_LEVEL5,
  10128. ssl->heap);
  10129. #elif defined(HAVE_PQM4)
  10130. ret = TLSX_UseSupportedCurve(extensions, WOLFSSL_KYBER_LEVEL1, ssl->heap);
  10131. #endif /* HAVE_LIBOQS */
  10132. #endif /* HAVE_PQC */
  10133. (void)ssl;
  10134. (void)extensions;
  10135. return ret;
  10136. }
  10137. #endif /* HAVE_SUPPORTED_CURVES */
  10138. #if defined(WOLFSSL_TLS13) && defined(HAVE_SUPPORTED_CURVES)
  10139. static const word16 preferredGroup[] = {
  10140. #if defined(HAVE_ECC) && (!defined(NO_ECC256) || \
  10141. defined(HAVE_ALL_CURVES)) && !defined(NO_ECC_SECP) && ECC_MIN_KEY_SZ <= 256
  10142. WOLFSSL_ECC_SECP256R1,
  10143. #endif
  10144. #if defined(HAVE_CURVE25519) && ECC_MIN_KEY_SZ <= 256
  10145. WOLFSSL_ECC_X25519,
  10146. #endif
  10147. #if defined(HAVE_CURVE448) && ECC_MIN_KEY_SZ <= 448
  10148. WOLFSSL_ECC_X448,
  10149. #endif
  10150. #if defined(HAVE_ECC) && (!defined(NO_ECC384) || \
  10151. defined(HAVE_ALL_CURVES)) && !defined(NO_ECC_SECP) && ECC_MIN_KEY_SZ <= 384
  10152. WOLFSSL_ECC_SECP384R1,
  10153. #endif
  10154. #if defined(HAVE_ECC) && (!defined(NO_ECC521) || \
  10155. defined(HAVE_ALL_CURVES)) && !defined(NO_ECC_SECP) && ECC_MIN_KEY_SZ <= 521
  10156. WOLFSSL_ECC_SECP521R1,
  10157. #endif
  10158. #if defined(HAVE_FFDHE_2048)
  10159. WOLFSSL_FFDHE_2048,
  10160. #endif
  10161. #if defined(HAVE_FFDHE_3072)
  10162. WOLFSSL_FFDHE_3072,
  10163. #endif
  10164. #if defined(HAVE_FFDHE_4096)
  10165. WOLFSSL_FFDHE_4096,
  10166. #endif
  10167. #if defined(HAVE_FFDHE_6144)
  10168. WOLFSSL_FFDHE_6144,
  10169. #endif
  10170. #if defined(HAVE_FFDHE_8192)
  10171. WOLFSSL_FFDHE_8192,
  10172. #endif
  10173. WOLFSSL_NAMED_GROUP_INVALID
  10174. };
  10175. #endif /* WOLFSSL_TLS13 && HAVE_SUPPORTED_CURVES */
  10176. int TLSX_PopulateExtensions(WOLFSSL* ssl, byte isServer)
  10177. {
  10178. int ret = 0;
  10179. byte* public_key = NULL;
  10180. word16 public_key_len = 0;
  10181. #if defined(WOLFSSL_TLS13) && (defined(HAVE_SESSION_TICKET) || !defined(NO_PSK))
  10182. int usingPSK = 0;
  10183. #endif
  10184. #if defined(HAVE_SUPPORTED_CURVES) && defined(WOLFSSL_TLS13)
  10185. TLSX* extension = NULL;
  10186. word16 namedGroup = WOLFSSL_NAMED_GROUP_INVALID;
  10187. #endif
  10188. /* server will add extension depending on what is parsed from client */
  10189. if (!isServer) {
  10190. #if defined(HAVE_ENCRYPT_THEN_MAC) && !defined(WOLFSSL_AEAD_ONLY)
  10191. if (!ssl->options.disallowEncThenMac) {
  10192. ret = TLSX_EncryptThenMac_Use(ssl);
  10193. if (ret != 0)
  10194. return ret;
  10195. }
  10196. #endif
  10197. #if (defined(HAVE_ECC) || defined(HAVE_CURVE25519) || \
  10198. defined(HAVE_CURVE448)) && defined(HAVE_SUPPORTED_CURVES)
  10199. if (!ssl->options.userCurves && !ssl->ctx->userCurves) {
  10200. if (TLSX_Find(ssl->ctx->extensions,
  10201. TLSX_SUPPORTED_GROUPS) == NULL) {
  10202. ret = TLSX_PopulateSupportedGroups(ssl, &ssl->extensions);
  10203. if (ret != WOLFSSL_SUCCESS)
  10204. return ret;
  10205. }
  10206. }
  10207. if ((!IsAtLeastTLSv1_3(ssl->version) || ssl->options.downgrade) &&
  10208. TLSX_Find(ssl->ctx->extensions, TLSX_EC_POINT_FORMATS) == NULL &&
  10209. TLSX_Find(ssl->extensions, TLSX_EC_POINT_FORMATS) == NULL) {
  10210. ret = TLSX_UsePointFormat(&ssl->extensions,
  10211. WOLFSSL_EC_PF_UNCOMPRESSED, ssl->heap);
  10212. if (ret != WOLFSSL_SUCCESS)
  10213. return ret;
  10214. }
  10215. #endif /* (HAVE_ECC || CURVE25519 || CURVE448) && HAVE_SUPPORTED_CURVES */
  10216. #ifdef WOLFSSL_SRTP
  10217. if (ssl->options.dtls && ssl->dtlsSrtpProfiles != 0) {
  10218. WOLFSSL_MSG("Adding DTLS SRTP extension");
  10219. if ((ret = TLSX_UseSRTP(&ssl->extensions, ssl->dtlsSrtpProfiles,
  10220. ssl->heap)) != 0) {
  10221. return ret;
  10222. }
  10223. }
  10224. #endif
  10225. } /* is not server */
  10226. #if !defined(NO_CERTS) && !defined(WOLFSSL_NO_SIGALG)
  10227. WOLFSSL_MSG("Adding signature algorithms extension");
  10228. if ((ret = TLSX_SetSignatureAlgorithms(&ssl->extensions, ssl, ssl->heap))
  10229. != 0) {
  10230. return ret;
  10231. }
  10232. #else
  10233. ret = 0;
  10234. #endif
  10235. #ifdef WOLFSSL_TLS13
  10236. if (!isServer && IsAtLeastTLSv1_3(ssl->version)) {
  10237. /* Add mandatory TLS v1.3 extension: supported version */
  10238. WOLFSSL_MSG("Adding supported versions extension");
  10239. if ((ret = TLSX_SetSupportedVersions(&ssl->extensions, ssl,
  10240. ssl->heap)) != 0) {
  10241. return ret;
  10242. }
  10243. #if !defined(HAVE_ECC) && !defined(HAVE_CURVE25519) && \
  10244. !defined(HAVE_CURVE448) && defined(HAVE_SUPPORTED_CURVES)
  10245. if (TLSX_Find(ssl->ctx->extensions, TLSX_SUPPORTED_GROUPS) == NULL) {
  10246. /* Put in DH groups for TLS 1.3 only. */
  10247. ret = TLSX_PopulateSupportedGroups(ssl, &ssl->extensions);
  10248. if (ret != WOLFSSL_SUCCESS)
  10249. return ret;
  10250. ret = 0;
  10251. }
  10252. #endif /* !(HAVE_ECC || CURVE25519 || CURVE448) && HAVE_SUPPORTED_CURVES */
  10253. #if !defined(NO_CERTS) && !defined(WOLFSSL_NO_SIGALG)
  10254. if (ssl->certHashSigAlgoSz > 0) {
  10255. WOLFSSL_MSG("Adding signature algorithms cert extension");
  10256. if ((ret = TLSX_SetSignatureAlgorithmsCert(&ssl->extensions,
  10257. ssl, ssl->heap)) != 0) {
  10258. return ret;
  10259. }
  10260. }
  10261. #endif
  10262. #if defined(HAVE_SUPPORTED_CURVES)
  10263. extension = TLSX_Find(ssl->extensions, TLSX_KEY_SHARE);
  10264. if (extension == NULL) {
  10265. #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
  10266. if (ssl->options.resuming && ssl->session->namedGroup != 0)
  10267. namedGroup = ssl->session->namedGroup;
  10268. else
  10269. #endif
  10270. if (ssl->numGroups > 0) {
  10271. int set = 0;
  10272. int i, j;
  10273. /* try to find the highest element in ssl->group[]
  10274. * that is contained in preferredGroup[].
  10275. */
  10276. namedGroup = preferredGroup[0];
  10277. for (i = 0; i < ssl->numGroups && !set; i++) {
  10278. for (j = 0; preferredGroup[j] != WOLFSSL_NAMED_GROUP_INVALID; j++) {
  10279. if (preferredGroup[j] == ssl->group[i]) {
  10280. namedGroup = ssl->group[i];
  10281. set = 1;
  10282. break;
  10283. }
  10284. }
  10285. }
  10286. }
  10287. else {
  10288. /* Choose the most preferred group. */
  10289. namedGroup = preferredGroup[0];
  10290. }
  10291. }
  10292. else {
  10293. KeyShareEntry* kse = (KeyShareEntry*)extension->data;
  10294. if (kse)
  10295. namedGroup = kse->group;
  10296. }
  10297. if (namedGroup > 0) {
  10298. #ifdef HAVE_PQC
  10299. /* For KEMs, the key share has already been generated. */
  10300. if (!WOLFSSL_NAMED_GROUP_IS_PQC(namedGroup))
  10301. #endif
  10302. ret = TLSX_KeyShare_Use(ssl, namedGroup, 0, NULL, NULL);
  10303. if (ret != 0)
  10304. return ret;
  10305. }
  10306. #endif /* HAVE_SUPPORTED_CURVES */
  10307. #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
  10308. TLSX_Remove(&ssl->extensions, TLSX_PRE_SHARED_KEY, ssl->heap);
  10309. #endif
  10310. #if defined(HAVE_SESSION_TICKET)
  10311. if (ssl->options.resuming && ssl->session->ticketLen > 0) {
  10312. WOLFSSL_SESSION* sess = ssl->session;
  10313. #ifdef WOLFSSL_32BIT_MILLI_TIME
  10314. word32 now, milli;
  10315. #else
  10316. word64 now, milli;
  10317. #endif
  10318. if (sess->ticketLen > MAX_PSK_ID_LEN) {
  10319. WOLFSSL_MSG("Session ticket length for PSK ext is too large");
  10320. return BUFFER_ERROR;
  10321. }
  10322. /* Determine the MAC algorithm for the cipher suite used. */
  10323. ssl->options.cipherSuite0 = sess->cipherSuite0;
  10324. ssl->options.cipherSuite = sess->cipherSuite;
  10325. ret = SetCipherSpecs(ssl);
  10326. if (ret != 0)
  10327. return ret;
  10328. now = TimeNowInMilliseconds();
  10329. if (now == 0)
  10330. return GETTIME_ERROR;
  10331. #ifdef WOLFSSL_32BIT_MILLI_TIME
  10332. if (now < sess->ticketSeen)
  10333. milli = (0xFFFFFFFFU - sess->ticketSeen) + 1 + now;
  10334. else
  10335. milli = now - sess->ticketSeen;
  10336. milli += sess->ticketAdd;
  10337. /* Pre-shared key is mandatory extension for resumption. */
  10338. ret = TLSX_PreSharedKey_Use(ssl, sess->ticket, sess->ticketLen,
  10339. milli, ssl->specs.mac_algorithm, ssl->options.cipherSuite0,
  10340. ssl->options.cipherSuite, 1, NULL);
  10341. #else
  10342. milli = now - sess->ticketSeen + sess->ticketAdd;
  10343. /* Pre-shared key is mandatory extension for resumption. */
  10344. ret = TLSX_PreSharedKey_Use(ssl, sess->ticket, sess->ticketLen,
  10345. (word32)milli, ssl->specs.mac_algorithm,
  10346. ssl->options.cipherSuite0, ssl->options.cipherSuite, 1,
  10347. NULL);
  10348. #endif
  10349. if (ret != 0)
  10350. return ret;
  10351. usingPSK = 1;
  10352. }
  10353. #endif
  10354. #ifndef NO_PSK
  10355. #ifndef WOLFSSL_PSK_ONE_ID
  10356. if (ssl->options.client_psk_cs_cb != NULL) {
  10357. int i;
  10358. for (i = 0; i < ssl->suites->suiteSz; i += 2) {
  10359. byte cipherSuite0 = ssl->suites->suites[i + 0];
  10360. byte cipherSuite = ssl->suites->suites[i + 1];
  10361. unsigned int keySz;
  10362. #ifdef WOLFSSL_PSK_MULTI_ID_PER_CS
  10363. int cnt = 0;
  10364. #endif
  10365. #ifdef HAVE_NULL_CIPHER
  10366. if (cipherSuite0 == ECC_BYTE ||
  10367. cipherSuite0 == ECDHE_PSK_BYTE) {
  10368. if (cipherSuite != TLS_SHA256_SHA256 &&
  10369. cipherSuite != TLS_SHA384_SHA384) {
  10370. continue;
  10371. }
  10372. }
  10373. else
  10374. #endif
  10375. if (cipherSuite0 != TLS13_BYTE)
  10376. continue;
  10377. #ifdef WOLFSSL_PSK_MULTI_ID_PER_CS
  10378. do {
  10379. ssl->arrays->client_identity[0] = cnt;
  10380. #endif
  10381. ssl->arrays->client_identity[MAX_PSK_ID_LEN] = '\0';
  10382. keySz = ssl->options.client_psk_cs_cb(
  10383. ssl, ssl->arrays->server_hint,
  10384. ssl->arrays->client_identity, MAX_PSK_ID_LEN,
  10385. ssl->arrays->psk_key, MAX_PSK_KEY_LEN,
  10386. GetCipherNameInternal(cipherSuite0, cipherSuite));
  10387. if (keySz > 0) {
  10388. ssl->arrays->psk_keySz = keySz;
  10389. ret = TLSX_PreSharedKey_Use(ssl,
  10390. (byte*)ssl->arrays->client_identity,
  10391. (word16)XSTRLEN(ssl->arrays->client_identity),
  10392. 0, SuiteMac(ssl->suites->suites + i),
  10393. cipherSuite0, cipherSuite, 0, NULL);
  10394. if (ret != 0)
  10395. return ret;
  10396. #ifdef WOLFSSL_PSK_MULTI_ID_PER_CS
  10397. cnt++;
  10398. #endif
  10399. }
  10400. #ifdef WOLFSSL_PSK_MULTI_ID_PER_CS
  10401. }
  10402. while (keySz > 0);
  10403. #endif
  10404. }
  10405. usingPSK = 1;
  10406. }
  10407. else
  10408. #endif
  10409. if (ssl->options.client_psk_cb != NULL ||
  10410. ssl->options.client_psk_tls13_cb != NULL) {
  10411. /* Default ciphersuite. */
  10412. byte cipherSuite0 = TLS13_BYTE;
  10413. byte cipherSuite = WOLFSSL_DEF_PSK_CIPHER;
  10414. int cipherSuiteFlags = WOLFSSL_CIPHER_SUITE_FLAG_NONE;
  10415. const char* cipherName = NULL;
  10416. if (ssl->options.client_psk_tls13_cb != NULL) {
  10417. ssl->arrays->psk_keySz = ssl->options.client_psk_tls13_cb(
  10418. ssl, ssl->arrays->server_hint,
  10419. ssl->arrays->client_identity, MAX_PSK_ID_LEN,
  10420. ssl->arrays->psk_key, MAX_PSK_KEY_LEN, &cipherName);
  10421. if (GetCipherSuiteFromName(cipherName, &cipherSuite0,
  10422. &cipherSuite, &cipherSuiteFlags) != 0) {
  10423. return PSK_KEY_ERROR;
  10424. }
  10425. }
  10426. else {
  10427. ssl->arrays->psk_keySz = ssl->options.client_psk_cb(ssl,
  10428. ssl->arrays->server_hint, ssl->arrays->client_identity,
  10429. MAX_PSK_ID_LEN, ssl->arrays->psk_key, MAX_PSK_KEY_LEN);
  10430. }
  10431. #if defined(OPENSSL_EXTRA)
  10432. /* OpenSSL treats 0 as a PSK key length of 0
  10433. * and meaning no PSK available.
  10434. */
  10435. if (ssl->arrays->psk_keySz > MAX_PSK_KEY_LEN) {
  10436. return PSK_KEY_ERROR;
  10437. }
  10438. if (ssl->arrays->psk_keySz > 0) {
  10439. #else
  10440. if (ssl->arrays->psk_keySz == 0 ||
  10441. ssl->arrays->psk_keySz > MAX_PSK_KEY_LEN) {
  10442. return PSK_KEY_ERROR;
  10443. }
  10444. #endif
  10445. ssl->arrays->client_identity[MAX_PSK_ID_LEN] = '\0';
  10446. ssl->options.cipherSuite0 = cipherSuite0;
  10447. ssl->options.cipherSuite = cipherSuite;
  10448. (void)cipherSuiteFlags;
  10449. ret = SetCipherSpecs(ssl);
  10450. if (ret != 0)
  10451. return ret;
  10452. ret = TLSX_PreSharedKey_Use(ssl,
  10453. (byte*)ssl->arrays->client_identity,
  10454. (word16)XSTRLEN(ssl->arrays->client_identity),
  10455. 0, ssl->specs.mac_algorithm,
  10456. cipherSuite0, cipherSuite, 0,
  10457. NULL);
  10458. if (ret != 0)
  10459. return ret;
  10460. usingPSK = 1;
  10461. #if defined(OPENSSL_EXTRA)
  10462. }
  10463. #endif
  10464. }
  10465. #endif /* !NO_PSK */
  10466. #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
  10467. /* Some servers do not generate session tickets unless
  10468. * the extension is seen in a non-resume client hello.
  10469. * We used to send it only if we were otherwise using PSK.
  10470. * Now always send it. Define NO_TLSX_PSKKEM_PLAIN_ANNOUNCE
  10471. * to revert to the old behaviour. */
  10472. #ifdef NO_TLSX_PSKKEM_PLAIN_ANNOUNCE
  10473. if (usingPSK)
  10474. #endif
  10475. {
  10476. byte modes;
  10477. (void)usingPSK;
  10478. /* Pre-shared key modes: mandatory extension for resumption. */
  10479. modes = 1 << PSK_KE;
  10480. #if !defined(NO_DH) || defined(HAVE_ECC) || \
  10481. defined(HAVE_CURVE25519) || defined(HAVE_CURVE448)
  10482. if (!ssl->options.noPskDheKe)
  10483. modes |= 1 << PSK_DHE_KE;
  10484. #endif
  10485. ret = TLSX_PskKeModes_Use(ssl, modes);
  10486. if (ret != 0)
  10487. return ret;
  10488. }
  10489. #endif
  10490. #if defined(WOLFSSL_POST_HANDSHAKE_AUTH)
  10491. if (!isServer && ssl->options.postHandshakeAuth) {
  10492. ret = TLSX_PostHandAuth_Use(ssl);
  10493. if (ret != 0)
  10494. return ret;
  10495. }
  10496. #endif
  10497. }
  10498. #endif
  10499. (void)isServer;
  10500. (void)public_key;
  10501. (void)public_key_len;
  10502. (void)ssl;
  10503. return ret;
  10504. }
  10505. #if defined(WOLFSSL_TLS13) || !defined(NO_WOLFSSL_CLIENT)
  10506. /** Tells the buffered size of extensions to be sent into the client hello. */
  10507. int TLSX_GetRequestSize(WOLFSSL* ssl, byte msgType, word16* pLength)
  10508. {
  10509. int ret = 0;
  10510. word16 length = 0;
  10511. byte semaphore[SEMAPHORE_SIZE] = {0};
  10512. if (!TLSX_SupportExtensions(ssl))
  10513. return 0;
  10514. if (msgType == client_hello) {
  10515. EC_VALIDATE_REQUEST(ssl, semaphore);
  10516. PF_VALIDATE_REQUEST(ssl, semaphore);
  10517. WOLF_STK_VALIDATE_REQUEST(ssl);
  10518. #if !defined(NO_CERTS) && !defined(WOLFSSL_NO_SIGALG)
  10519. if (ssl->suites->hashSigAlgoSz == 0)
  10520. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_SIGNATURE_ALGORITHMS));
  10521. #endif
  10522. #if defined(WOLFSSL_TLS13)
  10523. if (!IsAtLeastTLSv1_2(ssl))
  10524. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_SUPPORTED_VERSIONS));
  10525. #if !defined(WOLFSSL_NO_TLS12) || !defined(NO_OLD_TLS)
  10526. if (!IsAtLeastTLSv1_3(ssl->version)) {
  10527. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_KEY_SHARE));
  10528. #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
  10529. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_PRE_SHARED_KEY));
  10530. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_PSK_KEY_EXCHANGE_MODES));
  10531. #endif
  10532. #ifdef WOLFSSL_EARLY_DATA
  10533. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_EARLY_DATA));
  10534. #endif
  10535. #ifdef WOLFSSL_SEND_HRR_COOKIE
  10536. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_COOKIE));
  10537. #endif
  10538. #ifdef WOLFSSL_POST_HANDSHAKE_AUTH
  10539. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_POST_HANDSHAKE_AUTH));
  10540. #endif
  10541. }
  10542. #endif
  10543. #endif
  10544. #if defined(HAVE_CERTIFICATE_STATUS_REQUEST) \
  10545. || defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2)
  10546. if (!SSL_CM(ssl)->ocspStaplingEnabled) {
  10547. /* mark already sent, so it won't send it */
  10548. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_STATUS_REQUEST));
  10549. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_STATUS_REQUEST_V2));
  10550. }
  10551. #endif
  10552. }
  10553. #ifdef WOLFSSL_TLS13
  10554. #ifndef NO_CERTS
  10555. else if (msgType == certificate_request) {
  10556. /* Don't send out any extension except those that are turned off. */
  10557. XMEMSET(semaphore, 0xff, SEMAPHORE_SIZE);
  10558. #if !defined(NO_CERTS) && !defined(WOLFSSL_NO_SIGALG)
  10559. TURN_OFF(semaphore, TLSX_ToSemaphore(TLSX_SIGNATURE_ALGORITHMS));
  10560. #endif
  10561. /* TODO: TLSX_SIGNED_CERTIFICATE_TIMESTAMP,
  10562. * TLSX_CERTIFICATE_AUTHORITIES, OID_FILTERS
  10563. * TLSX_STATUS_REQUEST
  10564. */
  10565. }
  10566. #endif
  10567. #endif
  10568. if (ssl->extensions) {
  10569. ret = TLSX_GetSize(ssl->extensions, semaphore, msgType, &length);
  10570. if (ret != 0)
  10571. return ret;
  10572. }
  10573. if (ssl->ctx && ssl->ctx->extensions) {
  10574. ret = TLSX_GetSize(ssl->ctx->extensions, semaphore, msgType, &length);
  10575. if (ret != 0)
  10576. return ret;
  10577. }
  10578. #ifdef HAVE_EXTENDED_MASTER
  10579. if (msgType == client_hello && ssl->options.haveEMS &&
  10580. (!IsAtLeastTLSv1_3(ssl->version) || ssl->options.downgrade)) {
  10581. length += HELLO_EXT_SZ;
  10582. }
  10583. #endif
  10584. if (length)
  10585. length += OPAQUE16_LEN; /* for total length storage. */
  10586. *pLength += length;
  10587. return ret;
  10588. }
  10589. /** Writes the extensions to be sent into the client hello. */
  10590. int TLSX_WriteRequest(WOLFSSL* ssl, byte* output, byte msgType, word16* pOffset)
  10591. {
  10592. int ret = 0;
  10593. word16 offset = 0;
  10594. byte semaphore[SEMAPHORE_SIZE] = {0};
  10595. if (!TLSX_SupportExtensions(ssl) || output == NULL)
  10596. return 0;
  10597. offset += OPAQUE16_LEN; /* extensions length */
  10598. if (msgType == client_hello) {
  10599. EC_VALIDATE_REQUEST(ssl, semaphore);
  10600. PF_VALIDATE_REQUEST(ssl, semaphore);
  10601. WOLF_STK_VALIDATE_REQUEST(ssl);
  10602. #if !defined(NO_CERTS) && !defined(WOLFSSL_NO_SIGALG)
  10603. if (ssl->suites->hashSigAlgoSz == 0)
  10604. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_SIGNATURE_ALGORITHMS));
  10605. #endif
  10606. #ifdef WOLFSSL_TLS13
  10607. if (!IsAtLeastTLSv1_2(ssl))
  10608. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_SUPPORTED_VERSIONS));
  10609. #if !defined(WOLFSSL_NO_TLS12) || !defined(NO_OLD_TLS)
  10610. if (!IsAtLeastTLSv1_3(ssl->version)) {
  10611. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_KEY_SHARE));
  10612. #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
  10613. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_PSK_KEY_EXCHANGE_MODES));
  10614. #endif
  10615. #ifdef WOLFSSL_EARLY_DATA
  10616. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_EARLY_DATA));
  10617. #endif
  10618. #ifdef WOLFSSL_SEND_HRR_COOKIE
  10619. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_COOKIE));
  10620. #endif
  10621. #ifdef WOLFSSL_POST_HANDSHAKE_AUTH
  10622. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_POST_HANDSHAKE_AUTH));
  10623. #endif
  10624. }
  10625. #endif
  10626. #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
  10627. /* Must write Pre-shared Key extension at the end in TLS v1.3.
  10628. * Must not write out Pre-shared Key extension in earlier versions of
  10629. * protocol.
  10630. */
  10631. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_PRE_SHARED_KEY));
  10632. #endif
  10633. #endif
  10634. #if defined(HAVE_CERTIFICATE_STATUS_REQUEST) \
  10635. || defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2)
  10636. /* mark already sent, so it won't send it */
  10637. if (!SSL_CM(ssl)->ocspStaplingEnabled) {
  10638. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_STATUS_REQUEST));
  10639. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_STATUS_REQUEST_V2));
  10640. }
  10641. #endif
  10642. }
  10643. #ifdef WOLFSSL_TLS13
  10644. #ifndef NO_CERTS
  10645. else if (msgType == certificate_request) {
  10646. /* Don't send out any extension except those that are turned off. */
  10647. XMEMSET(semaphore, 0xff, SEMAPHORE_SIZE);
  10648. #if !defined(NO_CERTS) && !defined(WOLFSSL_NO_SIGALG)
  10649. TURN_OFF(semaphore, TLSX_ToSemaphore(TLSX_SIGNATURE_ALGORITHMS));
  10650. #endif
  10651. /* TODO: TLSX_SIGNED_CERTIFICATE_TIMESTAMP,
  10652. * TLSX_CERTIFICATE_AUTHORITIES, TLSX_OID_FILTERS
  10653. * TLSX_STATUS_REQUEST
  10654. */
  10655. }
  10656. #endif
  10657. #endif
  10658. if (ssl->extensions) {
  10659. ret = TLSX_Write(ssl->extensions, output + offset, semaphore,
  10660. msgType, &offset);
  10661. if (ret != 0)
  10662. return ret;
  10663. }
  10664. if (ssl->ctx && ssl->ctx->extensions) {
  10665. ret = TLSX_Write(ssl->ctx->extensions, output + offset, semaphore,
  10666. msgType, &offset);
  10667. if (ret != 0)
  10668. return ret;
  10669. }
  10670. #ifdef HAVE_EXTENDED_MASTER
  10671. if (msgType == client_hello && ssl->options.haveEMS &&
  10672. (!IsAtLeastTLSv1_3(ssl->version) || ssl->options.downgrade)) {
  10673. WOLFSSL_MSG("EMS extension to write");
  10674. c16toa(HELLO_EXT_EXTMS, output + offset);
  10675. offset += HELLO_EXT_TYPE_SZ;
  10676. c16toa(0, output + offset);
  10677. offset += HELLO_EXT_SZ_SZ;
  10678. }
  10679. #endif
  10680. #ifdef WOLFSSL_TLS13
  10681. #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
  10682. if (msgType == client_hello && IsAtLeastTLSv1_3(ssl->version)) {
  10683. /* Write out what we can of Pre-shared key extension. */
  10684. TURN_OFF(semaphore, TLSX_ToSemaphore(TLSX_PRE_SHARED_KEY));
  10685. ret = TLSX_Write(ssl->extensions, output + offset, semaphore,
  10686. client_hello, &offset);
  10687. if (ret != 0)
  10688. return ret;
  10689. }
  10690. #endif
  10691. #endif
  10692. if (offset > OPAQUE16_LEN || msgType != client_hello)
  10693. c16toa(offset - OPAQUE16_LEN, output); /* extensions length */
  10694. *pOffset += offset;
  10695. return ret;
  10696. }
  10697. #endif /* WOLFSSL_TLS13 || !NO_WOLFSSL_CLIENT */
  10698. #if defined(WOLFSSL_TLS13) || !defined(NO_WOLFSSL_SERVER)
  10699. /** Tells the buffered size of extensions to be sent into the server hello. */
  10700. int TLSX_GetResponseSize(WOLFSSL* ssl, byte msgType, word16* pLength)
  10701. {
  10702. int ret = 0;
  10703. word16 length = 0;
  10704. byte semaphore[SEMAPHORE_SIZE] = {0};
  10705. switch (msgType) {
  10706. #ifndef NO_WOLFSSL_SERVER
  10707. case server_hello:
  10708. PF_VALIDATE_RESPONSE(ssl, semaphore);
  10709. #ifdef WOLFSSL_TLS13
  10710. if (IsAtLeastTLSv1_3(ssl->version)) {
  10711. XMEMSET(semaphore, 0xff, SEMAPHORE_SIZE);
  10712. TURN_OFF(semaphore,
  10713. TLSX_ToSemaphore(TLSX_SUPPORTED_VERSIONS));
  10714. #ifdef HAVE_SUPPORTED_CURVES
  10715. if (!ssl->options.noPskDheKe)
  10716. TURN_OFF(semaphore, TLSX_ToSemaphore(TLSX_KEY_SHARE));
  10717. #endif
  10718. #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
  10719. TURN_OFF(semaphore, TLSX_ToSemaphore(TLSX_PRE_SHARED_KEY));
  10720. #endif
  10721. #ifdef WOLFSSL_DTLS_CID
  10722. TURN_OFF(semaphore, TLSX_ToSemaphore(TLSX_CONNECTION_ID));
  10723. #endif /* WOLFSSL_DTLS_CID */
  10724. }
  10725. #if !defined(WOLFSSL_NO_TLS12) || !defined(NO_OLD_TLS)
  10726. else {
  10727. #ifdef HAVE_SUPPORTED_CURVES
  10728. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_KEY_SHARE));
  10729. #endif
  10730. #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
  10731. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_PRE_SHARED_KEY));
  10732. #endif
  10733. }
  10734. #endif
  10735. #endif
  10736. break;
  10737. #ifdef WOLFSSL_TLS13
  10738. case hello_retry_request:
  10739. XMEMSET(semaphore, 0xff, SEMAPHORE_SIZE);
  10740. TURN_OFF(semaphore, TLSX_ToSemaphore(TLSX_SUPPORTED_VERSIONS));
  10741. #ifdef HAVE_SUPPORTED_CURVES
  10742. if (!ssl->options.noPskDheKe)
  10743. TURN_OFF(semaphore, TLSX_ToSemaphore(TLSX_KEY_SHARE));
  10744. #endif
  10745. #ifdef WOLFSSL_SEND_HRR_COOKIE
  10746. TURN_OFF(semaphore, TLSX_ToSemaphore(TLSX_COOKIE));
  10747. #endif
  10748. break;
  10749. #endif
  10750. #ifdef WOLFSSL_TLS13
  10751. case encrypted_extensions:
  10752. /* Send out all extension except those that are turned on. */
  10753. #ifdef HAVE_ECC
  10754. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_EC_POINT_FORMATS));
  10755. #endif
  10756. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_SUPPORTED_VERSIONS));
  10757. #ifdef HAVE_SESSION_TICKET
  10758. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_SESSION_TICKET));
  10759. #endif
  10760. #ifdef HAVE_SUPPORTED_CURVES
  10761. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_KEY_SHARE));
  10762. #endif
  10763. #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
  10764. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_PRE_SHARED_KEY));
  10765. #endif
  10766. #ifdef HAVE_CERTIFICATE_STATUS_REQUEST
  10767. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_STATUS_REQUEST));
  10768. #endif
  10769. #ifdef HAVE_CERTIFICATE_STATUS_REQUEST_V2
  10770. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_STATUS_REQUEST_V2));
  10771. #endif
  10772. #if defined(HAVE_SERVER_RENEGOTIATION_INFO)
  10773. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_RENEGOTIATION_INFO));
  10774. #endif
  10775. #ifdef WOLFSSL_DTLS_CID
  10776. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_CONNECTION_ID));
  10777. #endif /* WOLFSSL_DTLS_CID */
  10778. break;
  10779. #ifdef WOLFSSL_EARLY_DATA
  10780. case session_ticket:
  10781. if (ssl->options.tls1_3) {
  10782. XMEMSET(semaphore, 0xff, SEMAPHORE_SIZE);
  10783. TURN_OFF(semaphore, TLSX_ToSemaphore(TLSX_EARLY_DATA));
  10784. }
  10785. break;
  10786. #endif
  10787. #endif
  10788. #endif
  10789. #ifdef WOLFSSL_TLS13
  10790. #ifndef NO_CERTS
  10791. case certificate:
  10792. /* Don't send out any extension except those that are turned off. */
  10793. XMEMSET(semaphore, 0xff, SEMAPHORE_SIZE);
  10794. TURN_OFF(semaphore, TLSX_ToSemaphore(TLSX_STATUS_REQUEST));
  10795. /* TODO: TLSX_SIGNED_CERTIFICATE_TIMESTAMP,
  10796. * TLSX_SERVER_CERTIFICATE_TYPE
  10797. */
  10798. break;
  10799. #endif
  10800. #endif
  10801. }
  10802. #ifdef HAVE_EXTENDED_MASTER
  10803. if (ssl->options.haveEMS && msgType == server_hello &&
  10804. !IsAtLeastTLSv1_3(ssl->version)) {
  10805. length += HELLO_EXT_SZ;
  10806. }
  10807. #endif
  10808. if (TLSX_SupportExtensions(ssl)) {
  10809. ret = TLSX_GetSize(ssl->extensions, semaphore, msgType, &length);
  10810. if (ret != 0)
  10811. return ret;
  10812. }
  10813. /* All the response data is set at the ssl object only, so no ctx here. */
  10814. if (length || msgType != server_hello)
  10815. length += OPAQUE16_LEN; /* for total length storage. */
  10816. *pLength += length;
  10817. return ret;
  10818. }
  10819. /** Writes the server hello extensions into a buffer. */
  10820. int TLSX_WriteResponse(WOLFSSL *ssl, byte* output, byte msgType, word16* pOffset)
  10821. {
  10822. int ret = 0;
  10823. word16 offset = 0;
  10824. if (TLSX_SupportExtensions(ssl) && output) {
  10825. byte semaphore[SEMAPHORE_SIZE] = {0};
  10826. switch (msgType) {
  10827. #ifndef NO_WOLFSSL_SERVER
  10828. case server_hello:
  10829. PF_VALIDATE_RESPONSE(ssl, semaphore);
  10830. #ifdef WOLFSSL_TLS13
  10831. if (IsAtLeastTLSv1_3(ssl->version)) {
  10832. XMEMSET(semaphore, 0xff, SEMAPHORE_SIZE);
  10833. TURN_OFF(semaphore,
  10834. TLSX_ToSemaphore(TLSX_SUPPORTED_VERSIONS));
  10835. #ifdef HAVE_SUPPORTED_CURVES
  10836. if (!ssl->options.noPskDheKe)
  10837. TURN_OFF(semaphore, TLSX_ToSemaphore(TLSX_KEY_SHARE));
  10838. #endif
  10839. #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
  10840. TURN_OFF(semaphore, TLSX_ToSemaphore(TLSX_PRE_SHARED_KEY));
  10841. #endif
  10842. #ifdef WOLFSSL_DTLS_CID
  10843. TURN_OFF(semaphore, TLSX_ToSemaphore(TLSX_CONNECTION_ID));
  10844. #endif /* WOLFSSL_DTLS_CID */
  10845. }
  10846. #if !defined(WOLFSSL_NO_TLS12) || !defined(NO_OLD_TLS)
  10847. else {
  10848. #ifdef HAVE_SUPPORTED_CURVES
  10849. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_KEY_SHARE));
  10850. #endif
  10851. #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
  10852. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_PRE_SHARED_KEY));
  10853. #endif
  10854. }
  10855. #endif
  10856. #endif
  10857. break;
  10858. #ifdef WOLFSSL_TLS13
  10859. case hello_retry_request:
  10860. XMEMSET(semaphore, 0xff, SEMAPHORE_SIZE);
  10861. TURN_OFF(semaphore, TLSX_ToSemaphore(TLSX_SUPPORTED_VERSIONS));
  10862. #ifdef HAVE_SUPPORTED_CURVES
  10863. if (!ssl->options.noPskDheKe)
  10864. TURN_OFF(semaphore, TLSX_ToSemaphore(TLSX_KEY_SHARE));
  10865. #endif
  10866. /* Cookie is written below as last extension. */
  10867. break;
  10868. #endif
  10869. #ifdef WOLFSSL_TLS13
  10870. case encrypted_extensions:
  10871. /* Send out all extension except those that are turned on. */
  10872. #ifdef HAVE_ECC
  10873. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_EC_POINT_FORMATS));
  10874. #endif
  10875. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_SUPPORTED_VERSIONS));
  10876. #ifdef HAVE_SESSION_TICKET
  10877. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_SESSION_TICKET));
  10878. #endif
  10879. #ifdef HAVE_SUPPORTED_CURVES
  10880. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_KEY_SHARE));
  10881. #endif
  10882. #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
  10883. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_PRE_SHARED_KEY));
  10884. #endif
  10885. #ifdef HAVE_CERTIFICATE_STATUS_REQUEST
  10886. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_STATUS_REQUEST));
  10887. #endif
  10888. #ifdef HAVE_CERTIFICATE_STATUS_REQUEST_V2
  10889. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_STATUS_REQUEST_V2));
  10890. #endif
  10891. #if defined(HAVE_SERVER_RENEGOTIATION_INFO)
  10892. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_RENEGOTIATION_INFO));
  10893. #endif
  10894. #ifdef WOLFSSL_DTLS_CID
  10895. TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_CONNECTION_ID));
  10896. #endif /* WOLFSSL_DTLS_CID */
  10897. break;
  10898. #ifdef WOLFSSL_EARLY_DATA
  10899. case session_ticket:
  10900. if (ssl->options.tls1_3) {
  10901. XMEMSET(semaphore, 0xff, SEMAPHORE_SIZE);
  10902. TURN_OFF(semaphore, TLSX_ToSemaphore(TLSX_EARLY_DATA));
  10903. }
  10904. break;
  10905. #endif
  10906. #endif
  10907. #endif
  10908. #ifdef WOLFSSL_TLS13
  10909. #ifndef NO_CERTS
  10910. case certificate:
  10911. /* Don't send out any extension except those that are turned
  10912. * off. */
  10913. XMEMSET(semaphore, 0xff, SEMAPHORE_SIZE);
  10914. TURN_OFF(semaphore, TLSX_ToSemaphore(TLSX_STATUS_REQUEST));
  10915. /* TODO: TLSX_SIGNED_CERTIFICATE_TIMESTAMP,
  10916. * TLSX_SERVER_CERTIFICATE_TYPE
  10917. */
  10918. break;
  10919. #endif
  10920. #endif
  10921. default:
  10922. break;
  10923. }
  10924. offset += OPAQUE16_LEN; /* extensions length */
  10925. ret = TLSX_Write(ssl->extensions, output + offset, semaphore,
  10926. msgType, &offset);
  10927. if (ret != 0)
  10928. return ret;
  10929. #if defined(WOLFSSL_TLS13) && defined(WOLFSSL_SEND_HRR_COOKIE)
  10930. if (msgType == hello_retry_request) {
  10931. XMEMSET(semaphore, 0xff, SEMAPHORE_SIZE);
  10932. TURN_OFF(semaphore, TLSX_ToSemaphore(TLSX_COOKIE));
  10933. ret = TLSX_Write(ssl->extensions, output + offset, semaphore,
  10934. msgType, &offset);
  10935. if (ret != 0)
  10936. return ret;
  10937. }
  10938. #endif
  10939. #ifdef HAVE_EXTENDED_MASTER
  10940. if (ssl->options.haveEMS && msgType == server_hello &&
  10941. !IsAtLeastTLSv1_3(ssl->version)) {
  10942. WOLFSSL_MSG("EMS extension to write");
  10943. c16toa(HELLO_EXT_EXTMS, output + offset);
  10944. offset += HELLO_EXT_TYPE_SZ;
  10945. c16toa(0, output + offset);
  10946. offset += HELLO_EXT_SZ_SZ;
  10947. }
  10948. #endif
  10949. if (offset > OPAQUE16_LEN || msgType != server_hello)
  10950. c16toa(offset - OPAQUE16_LEN, output); /* extensions length */
  10951. }
  10952. if (pOffset)
  10953. *pOffset += offset;
  10954. return ret;
  10955. }
  10956. #endif /* WOLFSSL_TLS13 || !NO_WOLFSSL_SERVER */
  10957. #ifdef WOLFSSL_TLS13
  10958. int TLSX_ParseVersion(WOLFSSL* ssl, const byte* input, word16 length,
  10959. byte msgType, int* found)
  10960. {
  10961. int ret = 0;
  10962. int offset = 0;
  10963. *found = 0;
  10964. while (offset < (int)length) {
  10965. word16 type;
  10966. word16 size;
  10967. if (offset + (2 * OPAQUE16_LEN) > length) {
  10968. ret = BUFFER_ERROR;
  10969. break;
  10970. }
  10971. ato16(input + offset, &type);
  10972. offset += HELLO_EXT_TYPE_SZ;
  10973. ato16(input + offset, &size);
  10974. offset += OPAQUE16_LEN;
  10975. if (offset + size > length) {
  10976. ret = BUFFER_ERROR;
  10977. break;
  10978. }
  10979. if (type == TLSX_SUPPORTED_VERSIONS) {
  10980. *found = 1;
  10981. WOLFSSL_MSG("Supported Versions extension received");
  10982. ret = SV_PARSE(ssl, input + offset, size, msgType);
  10983. break;
  10984. }
  10985. offset += size;
  10986. }
  10987. return ret;
  10988. }
  10989. #endif
  10990. /** Parses a buffer of TLS extensions. */
  10991. int TLSX_Parse(WOLFSSL* ssl, const byte* input, word16 length, byte msgType,
  10992. Suites *suites)
  10993. {
  10994. int ret = 0;
  10995. word16 offset = 0;
  10996. byte isRequest = (msgType == client_hello ||
  10997. msgType == certificate_request);
  10998. #ifdef HAVE_EXTENDED_MASTER
  10999. byte pendingEMS = 0;
  11000. #endif
  11001. #if defined(WOLFSSL_TLS13) && (defined(HAVE_SESSION_TICKET) || !defined(NO_PSK))
  11002. int pskDone = 0;
  11003. #endif
  11004. if (!ssl || !input || (isRequest && !suites))
  11005. return BAD_FUNC_ARG;
  11006. while (ret == 0 && offset < length) {
  11007. word16 type;
  11008. word16 size;
  11009. #if defined(WOLFSSL_TLS13) && (defined(HAVE_SESSION_TICKET) || !defined(NO_PSK))
  11010. if (msgType == client_hello && pskDone) {
  11011. WOLFSSL_ERROR_VERBOSE(PSK_KEY_ERROR);
  11012. return PSK_KEY_ERROR;
  11013. }
  11014. #endif
  11015. if (length - offset < HELLO_EXT_TYPE_SZ + OPAQUE16_LEN)
  11016. return BUFFER_ERROR;
  11017. ato16(input + offset, &type);
  11018. offset += HELLO_EXT_TYPE_SZ;
  11019. ato16(input + offset, &size);
  11020. offset += OPAQUE16_LEN;
  11021. if (length - offset < size)
  11022. return BUFFER_ERROR;
  11023. switch (type) {
  11024. #ifdef HAVE_SNI
  11025. case TLSX_SERVER_NAME:
  11026. WOLFSSL_MSG("SNI extension received");
  11027. #ifdef WOLFSSL_DEBUG_TLS
  11028. WOLFSSL_BUFFER(input + offset, size);
  11029. #endif
  11030. #ifdef WOLFSSL_TLS13
  11031. if (IsAtLeastTLSv1_3(ssl->version)) {
  11032. if (msgType != client_hello &&
  11033. msgType != encrypted_extensions)
  11034. return EXT_NOT_ALLOWED;
  11035. }
  11036. else
  11037. #endif
  11038. {
  11039. if (msgType != client_hello &&
  11040. msgType != server_hello)
  11041. return EXT_NOT_ALLOWED;
  11042. }
  11043. ret = SNI_PARSE(ssl, input + offset, size, isRequest);
  11044. break;
  11045. #endif
  11046. case TLSX_TRUSTED_CA_KEYS:
  11047. WOLFSSL_MSG("Trusted CA extension received");
  11048. #ifdef WOLFSSL_DEBUG_TLS
  11049. WOLFSSL_BUFFER(input + offset, size);
  11050. #endif
  11051. #ifdef WOLFSSL_TLS13
  11052. if (IsAtLeastTLSv1_3(ssl->version)) {
  11053. if (msgType != client_hello &&
  11054. msgType != encrypted_extensions)
  11055. return EXT_NOT_ALLOWED;
  11056. }
  11057. else
  11058. #endif
  11059. {
  11060. if (msgType != client_hello)
  11061. return EXT_NOT_ALLOWED;
  11062. }
  11063. ret = TCA_PARSE(ssl, input + offset, size, isRequest);
  11064. break;
  11065. case TLSX_MAX_FRAGMENT_LENGTH:
  11066. WOLFSSL_MSG("Max Fragment Length extension received");
  11067. #ifdef WOLFSSL_DEBUG_TLS
  11068. WOLFSSL_BUFFER(input + offset, size);
  11069. #endif
  11070. #ifdef WOLFSSL_TLS13
  11071. if (IsAtLeastTLSv1_3(ssl->version)) {
  11072. if (msgType != client_hello &&
  11073. msgType != encrypted_extensions) {
  11074. WOLFSSL_ERROR_VERBOSE(EXT_NOT_ALLOWED);
  11075. return EXT_NOT_ALLOWED;
  11076. }
  11077. }
  11078. else
  11079. #endif
  11080. {
  11081. if (msgType != client_hello &&
  11082. msgType != server_hello) {
  11083. WOLFSSL_ERROR_VERBOSE(EXT_NOT_ALLOWED);
  11084. return EXT_NOT_ALLOWED;
  11085. }
  11086. }
  11087. ret = MFL_PARSE(ssl, input + offset, size, isRequest);
  11088. break;
  11089. case TLSX_TRUNCATED_HMAC:
  11090. WOLFSSL_MSG("Truncated HMAC extension received");
  11091. #ifdef WOLFSSL_DEBUG_TLS
  11092. WOLFSSL_BUFFER(input + offset, size);
  11093. #endif
  11094. #ifdef WOLFSSL_TLS13
  11095. if (IsAtLeastTLSv1_3(ssl->version))
  11096. break;
  11097. #endif
  11098. if (msgType != client_hello)
  11099. return EXT_NOT_ALLOWED;
  11100. ret = THM_PARSE(ssl, input + offset, size, isRequest);
  11101. break;
  11102. case TLSX_SUPPORTED_GROUPS:
  11103. WOLFSSL_MSG("Supported Groups extension received");
  11104. #ifdef WOLFSSL_DEBUG_TLS
  11105. WOLFSSL_BUFFER(input + offset, size);
  11106. #endif
  11107. #ifdef WOLFSSL_TLS13
  11108. if (IsAtLeastTLSv1_3(ssl->version)) {
  11109. if (msgType != client_hello &&
  11110. msgType != encrypted_extensions) {
  11111. WOLFSSL_ERROR_VERBOSE(EXT_NOT_ALLOWED);
  11112. return EXT_NOT_ALLOWED;
  11113. }
  11114. }
  11115. else
  11116. #endif
  11117. {
  11118. if (msgType != client_hello) {
  11119. WOLFSSL_ERROR_VERBOSE(EXT_NOT_ALLOWED);
  11120. return EXT_NOT_ALLOWED;
  11121. }
  11122. }
  11123. ret = EC_PARSE(ssl, input + offset, size, isRequest);
  11124. break;
  11125. case TLSX_EC_POINT_FORMATS:
  11126. WOLFSSL_MSG("Point Formats extension received");
  11127. #ifdef WOLFSSL_DEBUG_TLS
  11128. WOLFSSL_BUFFER(input + offset, size);
  11129. #endif
  11130. #ifdef WOLFSSL_TLS13
  11131. if (IsAtLeastTLSv1_3(ssl->version))
  11132. break;
  11133. #endif
  11134. if (msgType != client_hello &&
  11135. msgType != server_hello) {
  11136. WOLFSSL_ERROR_VERBOSE(EXT_NOT_ALLOWED);
  11137. return EXT_NOT_ALLOWED;
  11138. }
  11139. ret = PF_PARSE(ssl, input + offset, size, isRequest);
  11140. break;
  11141. case TLSX_STATUS_REQUEST:
  11142. WOLFSSL_MSG("Certificate Status Request extension received");
  11143. #ifdef WOLFSSL_DEBUG_TLS
  11144. WOLFSSL_BUFFER(input + offset, size);
  11145. #endif
  11146. #ifdef WOLFSSL_TLS13
  11147. if (IsAtLeastTLSv1_3(ssl->version)) {
  11148. if (msgType != client_hello &&
  11149. msgType != certificate_request &&
  11150. msgType != certificate)
  11151. return EXT_NOT_ALLOWED;
  11152. }
  11153. else
  11154. #endif
  11155. {
  11156. if (msgType != client_hello &&
  11157. msgType != server_hello)
  11158. return EXT_NOT_ALLOWED;
  11159. }
  11160. ret = CSR_PARSE(ssl, input + offset, size, isRequest);
  11161. break;
  11162. case TLSX_STATUS_REQUEST_V2:
  11163. WOLFSSL_MSG("Certificate Status Request v2 extension received");
  11164. #ifdef WOLFSSL_DEBUG_TLS
  11165. WOLFSSL_BUFFER(input + offset, size);
  11166. #endif
  11167. #if defined(WOLFSSL_TLS13) && defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2)
  11168. if (IsAtLeastTLSv1_3(ssl->version)) {
  11169. if (msgType != client_hello &&
  11170. msgType != certificate_request &&
  11171. msgType != certificate)
  11172. return EXT_NOT_ALLOWED;
  11173. }
  11174. else
  11175. #endif
  11176. {
  11177. if (msgType != client_hello &&
  11178. msgType != server_hello)
  11179. return EXT_NOT_ALLOWED;
  11180. }
  11181. ret = CSR2_PARSE(ssl, input + offset, size, isRequest);
  11182. break;
  11183. #ifdef HAVE_EXTENDED_MASTER
  11184. case HELLO_EXT_EXTMS:
  11185. WOLFSSL_MSG("Extended Master Secret extension received");
  11186. #ifdef WOLFSSL_DEBUG_TLS
  11187. WOLFSSL_BUFFER(input + offset, size);
  11188. #endif
  11189. #if defined(WOLFSSL_TLS13)
  11190. if (IsAtLeastTLSv1_3(ssl->version))
  11191. break;
  11192. #endif
  11193. if (msgType != client_hello &&
  11194. msgType != server_hello)
  11195. return EXT_NOT_ALLOWED;
  11196. if (size != 0)
  11197. return BUFFER_ERROR;
  11198. #ifndef NO_WOLFSSL_SERVER
  11199. if (isRequest)
  11200. ssl->options.haveEMS = 1;
  11201. #endif
  11202. pendingEMS = 1;
  11203. break;
  11204. #endif
  11205. case TLSX_RENEGOTIATION_INFO:
  11206. WOLFSSL_MSG("Secure Renegotiation extension received");
  11207. #ifdef WOLFSSL_DEBUG_TLS
  11208. WOLFSSL_BUFFER(input + offset, size);
  11209. #endif
  11210. #ifdef WOLFSSL_TLS13
  11211. if (IsAtLeastTLSv1_3(ssl->version))
  11212. break;
  11213. #endif
  11214. if (msgType != client_hello &&
  11215. msgType != server_hello)
  11216. return EXT_NOT_ALLOWED;
  11217. ret = SCR_PARSE(ssl, input + offset, size, isRequest);
  11218. break;
  11219. case TLSX_SESSION_TICKET:
  11220. WOLFSSL_MSG("Session Ticket extension received");
  11221. #ifdef WOLFSSL_DEBUG_TLS
  11222. WOLFSSL_BUFFER(input + offset, size);
  11223. #endif
  11224. #if defined(WOLFSSL_TLS13) && defined(HAVE_SESSION_TICKET)
  11225. if (IsAtLeastTLSv1_3(ssl->version)) {
  11226. if (msgType != client_hello)
  11227. return EXT_NOT_ALLOWED;
  11228. }
  11229. else
  11230. #endif
  11231. {
  11232. if (msgType != client_hello &&
  11233. msgType != server_hello)
  11234. return EXT_NOT_ALLOWED;
  11235. }
  11236. ret = WOLF_STK_PARSE(ssl, input + offset, size, isRequest);
  11237. break;
  11238. case TLSX_APPLICATION_LAYER_PROTOCOL:
  11239. WOLFSSL_MSG("ALPN extension received");
  11240. #ifdef WOLFSSL_DEBUG_TLS
  11241. WOLFSSL_BUFFER(input + offset, size);
  11242. #endif
  11243. #if defined(WOLFSSL_TLS13) && defined(HAVE_ALPN)
  11244. if (IsAtLeastTLSv1_3(ssl->version)) {
  11245. if (msgType != client_hello &&
  11246. msgType != encrypted_extensions)
  11247. return EXT_NOT_ALLOWED;
  11248. }
  11249. else
  11250. #endif
  11251. {
  11252. if (msgType != client_hello &&
  11253. msgType != server_hello)
  11254. return EXT_NOT_ALLOWED;
  11255. }
  11256. ret = ALPN_PARSE(ssl, input + offset, size, isRequest);
  11257. break;
  11258. #if !defined(NO_CERTS) && !defined(WOLFSSL_NO_SIGALG)
  11259. case TLSX_SIGNATURE_ALGORITHMS:
  11260. WOLFSSL_MSG("Signature Algorithms extension received");
  11261. #ifdef WOLFSSL_DEBUG_TLS
  11262. WOLFSSL_BUFFER(input + offset, size);
  11263. #endif
  11264. if (!IsAtLeastTLSv1_2(ssl))
  11265. break;
  11266. #ifdef WOLFSSL_TLS13
  11267. if (IsAtLeastTLSv1_3(ssl->version)) {
  11268. if (msgType != client_hello &&
  11269. msgType != certificate_request)
  11270. return EXT_NOT_ALLOWED;
  11271. }
  11272. else
  11273. #endif
  11274. {
  11275. if (msgType != client_hello)
  11276. return EXT_NOT_ALLOWED;
  11277. }
  11278. ret = SA_PARSE(ssl, input + offset, size, isRequest, suites);
  11279. break;
  11280. #endif
  11281. #if defined(HAVE_ENCRYPT_THEN_MAC) && !defined(WOLFSSL_AEAD_ONLY)
  11282. case TLSX_ENCRYPT_THEN_MAC:
  11283. WOLFSSL_MSG("Encrypt-Then-Mac extension received");
  11284. /* Ignore for TLS 1.3+ */
  11285. if (IsAtLeastTLSv1_3(ssl->version))
  11286. break;
  11287. if (msgType != client_hello &&
  11288. msgType != server_hello)
  11289. return EXT_NOT_ALLOWED;
  11290. ret = ETM_PARSE(ssl, input + offset, size, msgType);
  11291. break;
  11292. #endif /* HAVE_ENCRYPT_THEN_MAC */
  11293. #ifdef WOLFSSL_TLS13
  11294. case TLSX_SUPPORTED_VERSIONS:
  11295. WOLFSSL_MSG("Skipping Supported Versions - already processed");
  11296. #ifdef WOLFSSL_DEBUG_TLS
  11297. WOLFSSL_BUFFER(input + offset, size);
  11298. #endif
  11299. if (msgType != client_hello &&
  11300. msgType != server_hello &&
  11301. msgType != hello_retry_request)
  11302. return EXT_NOT_ALLOWED;
  11303. break;
  11304. #ifdef WOLFSSL_SEND_HRR_COOKIE
  11305. case TLSX_COOKIE:
  11306. WOLFSSL_MSG("Cookie extension received");
  11307. #ifdef WOLFSSL_DEBUG_TLS
  11308. WOLFSSL_BUFFER(input + offset, size);
  11309. #endif
  11310. if (!IsAtLeastTLSv1_3(ssl->version))
  11311. break;
  11312. if (msgType != client_hello &&
  11313. msgType != hello_retry_request) {
  11314. return EXT_NOT_ALLOWED;
  11315. }
  11316. ret = CKE_PARSE(ssl, input + offset, size, msgType);
  11317. break;
  11318. #endif
  11319. #if defined(HAVE_SESSION_TICKET) || !defined(NO_PSK)
  11320. case TLSX_PRE_SHARED_KEY:
  11321. WOLFSSL_MSG("Pre-Shared Key extension received");
  11322. #ifdef WOLFSSL_DEBUG_TLS
  11323. WOLFSSL_BUFFER(input + offset, size);
  11324. #endif
  11325. if (!IsAtLeastTLSv1_3(ssl->version))
  11326. break;
  11327. if (msgType != client_hello &&
  11328. msgType != server_hello) {
  11329. WOLFSSL_ERROR_VERBOSE(EXT_NOT_ALLOWED);
  11330. return EXT_NOT_ALLOWED;
  11331. }
  11332. ret = PSK_PARSE(ssl, input + offset, size, msgType);
  11333. pskDone = 1;
  11334. break;
  11335. case TLSX_PSK_KEY_EXCHANGE_MODES:
  11336. WOLFSSL_MSG("PSK Key Exchange Modes extension received");
  11337. #ifdef WOLFSSL_DEBUG_TLS
  11338. WOLFSSL_BUFFER(input + offset, size);
  11339. #endif
  11340. if (!IsAtLeastTLSv1_3(ssl->version))
  11341. break;
  11342. if (msgType != client_hello) {
  11343. WOLFSSL_ERROR_VERBOSE(EXT_NOT_ALLOWED);
  11344. return EXT_NOT_ALLOWED;
  11345. }
  11346. ret = PKM_PARSE(ssl, input + offset, size, msgType);
  11347. break;
  11348. #endif
  11349. #ifdef WOLFSSL_EARLY_DATA
  11350. case TLSX_EARLY_DATA:
  11351. WOLFSSL_MSG("Early Data extension received");
  11352. #ifdef WOLFSSL_DEBUG_TLS
  11353. WOLFSSL_BUFFER(input + offset, size);
  11354. #endif
  11355. if (!IsAtLeastTLSv1_3(ssl->version))
  11356. break;
  11357. if (msgType != client_hello && msgType != session_ticket &&
  11358. msgType != encrypted_extensions) {
  11359. WOLFSSL_ERROR_VERBOSE(EXT_NOT_ALLOWED);
  11360. return EXT_NOT_ALLOWED;
  11361. }
  11362. ret = EDI_PARSE(ssl, input + offset, size, msgType);
  11363. break;
  11364. #endif
  11365. #ifdef WOLFSSL_POST_HANDSHAKE_AUTH
  11366. case TLSX_POST_HANDSHAKE_AUTH:
  11367. WOLFSSL_MSG("Post Handshake Authentication extension received");
  11368. #ifdef WOLFSSL_DEBUG_TLS
  11369. WOLFSSL_BUFFER(input + offset, size);
  11370. #endif
  11371. if (!IsAtLeastTLSv1_3(ssl->version))
  11372. break;
  11373. if (msgType != client_hello) {
  11374. WOLFSSL_ERROR_VERBOSE(EXT_NOT_ALLOWED);
  11375. return EXT_NOT_ALLOWED;
  11376. }
  11377. ret = PHA_PARSE(ssl, input + offset, size, msgType);
  11378. break;
  11379. #endif
  11380. #if !defined(NO_CERTS) && !defined(WOLFSSL_NO_SIGALG)
  11381. case TLSX_SIGNATURE_ALGORITHMS_CERT:
  11382. WOLFSSL_MSG("Signature Algorithms extension received");
  11383. #ifdef WOLFSSL_DEBUG_TLS
  11384. WOLFSSL_BUFFER(input + offset, size);
  11385. #endif
  11386. if (!IsAtLeastTLSv1_3(ssl->version))
  11387. break;
  11388. if (msgType != client_hello &&
  11389. msgType != certificate_request) {
  11390. WOLFSSL_ERROR_VERBOSE(EXT_NOT_ALLOWED);
  11391. return EXT_NOT_ALLOWED;
  11392. }
  11393. ret = SAC_PARSE(ssl, input + offset, size, isRequest);
  11394. break;
  11395. #endif
  11396. case TLSX_KEY_SHARE:
  11397. WOLFSSL_MSG("Key Share extension received");
  11398. #ifdef WOLFSSL_DEBUG_TLS
  11399. WOLFSSL_BUFFER(input + offset, size);
  11400. #endif
  11401. #ifdef HAVE_SUPPORTED_CURVES
  11402. if (!IsAtLeastTLSv1_3(ssl->version))
  11403. break;
  11404. if (msgType != client_hello && msgType != server_hello &&
  11405. msgType != hello_retry_request) {
  11406. WOLFSSL_ERROR_VERBOSE(EXT_NOT_ALLOWED);
  11407. return EXT_NOT_ALLOWED;
  11408. }
  11409. #endif
  11410. ret = KS_PARSE(ssl, input + offset, size, msgType);
  11411. break;
  11412. #endif
  11413. #ifdef WOLFSSL_SRTP
  11414. case TLSX_USE_SRTP:
  11415. WOLFSSL_MSG("Use SRTP extension received");
  11416. ret = SRTP_PARSE(ssl, input + offset, size, isRequest);
  11417. break;
  11418. #endif
  11419. #ifdef WOLFSSL_QUIC
  11420. case TLSX_KEY_QUIC_TP_PARAMS:
  11421. FALL_THROUGH;
  11422. case TLSX_KEY_QUIC_TP_PARAMS_DRAFT:
  11423. WOLFSSL_MSG("QUIC transport parameter received");
  11424. #ifdef WOLFSSL_DEBUG_TLS
  11425. WOLFSSL_BUFFER(input + offset, size);
  11426. #endif
  11427. if (IsAtLeastTLSv1_3(ssl->version) &&
  11428. msgType != client_hello &&
  11429. msgType != server_hello &&
  11430. msgType != encrypted_extensions) {
  11431. return EXT_NOT_ALLOWED;
  11432. }
  11433. else if (!IsAtLeastTLSv1_3(ssl->version) &&
  11434. msgType == encrypted_extensions) {
  11435. return EXT_NOT_ALLOWED;
  11436. }
  11437. else if (WOLFSSL_IS_QUIC(ssl)) {
  11438. ret = QTP_PARSE(ssl, input + offset, size, type, msgType);
  11439. }
  11440. else {
  11441. WOLFSSL_MSG("QUIC transport param TLS extension type, but no QUIC");
  11442. return EXT_NOT_ALLOWED; /* be safe, this should not happen */
  11443. }
  11444. break;
  11445. #endif /* WOLFSSL_QUIC */
  11446. #if defined(WOLFSSL_DTLS_CID)
  11447. case TLSX_CONNECTION_ID:
  11448. /* connection ID not supported in DTLSv1.2 */
  11449. if (!IsAtLeastTLSv1_3(ssl->version))
  11450. break;
  11451. if (msgType != client_hello && msgType != server_hello)
  11452. return EXT_NOT_ALLOWED;
  11453. WOLFSSL_MSG("ConnectionID extension received");
  11454. ret = CID_PARSE(ssl, input + offset, size, isRequest);
  11455. break;
  11456. #endif /* defined(WOLFSSL_DTLS_CID) */
  11457. default:
  11458. WOLFSSL_MSG("Unknown TLS extension type");
  11459. }
  11460. /* offset should be updated here! */
  11461. offset += size;
  11462. }
  11463. #ifdef HAVE_EXTENDED_MASTER
  11464. if (IsAtLeastTLSv1_3(ssl->version) && msgType == hello_retry_request) {
  11465. /* Don't change EMS status until server_hello received.
  11466. * Second ClientHello must have same extensions.
  11467. */
  11468. }
  11469. else if (!isRequest && ssl->options.haveEMS && !pendingEMS)
  11470. ssl->options.haveEMS = 0;
  11471. #endif
  11472. if (ret == 0)
  11473. ret = SNI_VERIFY_PARSE(ssl, isRequest);
  11474. if (ret == 0)
  11475. ret = TCA_VERIFY_PARSE(ssl, isRequest);
  11476. return ret;
  11477. }
  11478. /* undefining semaphore macros */
  11479. #undef IS_OFF
  11480. #undef TURN_ON
  11481. #undef SEMAPHORE_SIZE
  11482. #endif /* HAVE_TLS_EXTENSIONS */
  11483. #ifndef NO_WOLFSSL_CLIENT
  11484. WOLFSSL_METHOD* wolfTLS_client_method(void)
  11485. {
  11486. return wolfTLS_client_method_ex(NULL);
  11487. }
  11488. WOLFSSL_METHOD* wolfTLS_client_method_ex(void* heap)
  11489. {
  11490. WOLFSSL_METHOD* method =
  11491. (WOLFSSL_METHOD*) XMALLOC(sizeof(WOLFSSL_METHOD),
  11492. heap, DYNAMIC_TYPE_METHOD);
  11493. (void)heap;
  11494. WOLFSSL_ENTER("TLS_client_method_ex");
  11495. if (method) {
  11496. #if defined(WOLFSSL_TLS13)
  11497. InitSSL_Method(method, MakeTLSv1_3());
  11498. #elif !defined(WOLFSSL_NO_TLS12)
  11499. InitSSL_Method(method, MakeTLSv1_2());
  11500. #elif !defined(NO_OLD_TLS)
  11501. InitSSL_Method(method, MakeTLSv1_1());
  11502. #elif defined(WOLFSSL_ALLOW_TLSV10)
  11503. InitSSL_Method(method, MakeTLSv1());
  11504. #else
  11505. #error No TLS version enabled!
  11506. #endif
  11507. method->downgrade = 1;
  11508. method->side = WOLFSSL_CLIENT_END;
  11509. }
  11510. return method;
  11511. }
  11512. #ifndef NO_OLD_TLS
  11513. #ifdef WOLFSSL_ALLOW_TLSV10
  11514. WOLFSSL_METHOD* wolfTLSv1_client_method(void)
  11515. {
  11516. return wolfTLSv1_client_method_ex(NULL);
  11517. }
  11518. WOLFSSL_METHOD* wolfTLSv1_client_method_ex(void* heap)
  11519. {
  11520. WOLFSSL_METHOD* method =
  11521. (WOLFSSL_METHOD*) XMALLOC(sizeof(WOLFSSL_METHOD),
  11522. heap, DYNAMIC_TYPE_METHOD);
  11523. (void)heap;
  11524. WOLFSSL_ENTER("TLSv1_client_method_ex");
  11525. if (method)
  11526. InitSSL_Method(method, MakeTLSv1());
  11527. return method;
  11528. }
  11529. #endif /* WOLFSSL_ALLOW_TLSV10 */
  11530. WOLFSSL_METHOD* wolfTLSv1_1_client_method(void)
  11531. {
  11532. return wolfTLSv1_1_client_method_ex(NULL);
  11533. }
  11534. WOLFSSL_METHOD* wolfTLSv1_1_client_method_ex(void* heap)
  11535. {
  11536. WOLFSSL_METHOD* method =
  11537. (WOLFSSL_METHOD*) XMALLOC(sizeof(WOLFSSL_METHOD),
  11538. heap, DYNAMIC_TYPE_METHOD);
  11539. (void)heap;
  11540. WOLFSSL_ENTER("TLSv1_1_client_method_ex");
  11541. if (method)
  11542. InitSSL_Method(method, MakeTLSv1_1());
  11543. return method;
  11544. }
  11545. #endif /* !NO_OLD_TLS */
  11546. #ifndef WOLFSSL_NO_TLS12
  11547. WOLFSSL_ABI
  11548. WOLFSSL_METHOD* wolfTLSv1_2_client_method(void)
  11549. {
  11550. return wolfTLSv1_2_client_method_ex(NULL);
  11551. }
  11552. WOLFSSL_METHOD* wolfTLSv1_2_client_method_ex(void* heap)
  11553. {
  11554. WOLFSSL_METHOD* method =
  11555. (WOLFSSL_METHOD*) XMALLOC(sizeof(WOLFSSL_METHOD),
  11556. heap, DYNAMIC_TYPE_METHOD);
  11557. (void)heap;
  11558. WOLFSSL_ENTER("TLSv1_2_client_method_ex");
  11559. if (method)
  11560. InitSSL_Method(method, MakeTLSv1_2());
  11561. return method;
  11562. }
  11563. #endif /* WOLFSSL_NO_TLS12 */
  11564. #ifdef WOLFSSL_TLS13
  11565. /* The TLS v1.3 client method data.
  11566. *
  11567. * returns the method data for a TLS v1.3 client.
  11568. */
  11569. WOLFSSL_ABI
  11570. WOLFSSL_METHOD* wolfTLSv1_3_client_method(void)
  11571. {
  11572. return wolfTLSv1_3_client_method_ex(NULL);
  11573. }
  11574. /* The TLS v1.3 client method data.
  11575. *
  11576. * heap The heap used for allocation.
  11577. * returns the method data for a TLS v1.3 client.
  11578. */
  11579. WOLFSSL_METHOD* wolfTLSv1_3_client_method_ex(void* heap)
  11580. {
  11581. WOLFSSL_METHOD* method = (WOLFSSL_METHOD*)
  11582. XMALLOC(sizeof(WOLFSSL_METHOD), heap,
  11583. DYNAMIC_TYPE_METHOD);
  11584. (void)heap;
  11585. WOLFSSL_ENTER("TLSv1_3_client_method_ex");
  11586. if (method)
  11587. InitSSL_Method(method, MakeTLSv1_3());
  11588. return method;
  11589. }
  11590. #endif /* WOLFSSL_TLS13 */
  11591. #ifdef WOLFSSL_DTLS
  11592. WOLFSSL_METHOD* wolfDTLS_client_method(void)
  11593. {
  11594. return wolfDTLS_client_method_ex(NULL);
  11595. }
  11596. WOLFSSL_METHOD* wolfDTLS_client_method_ex(void* heap)
  11597. {
  11598. WOLFSSL_METHOD* method =
  11599. (WOLFSSL_METHOD*) XMALLOC(sizeof(WOLFSSL_METHOD),
  11600. heap, DYNAMIC_TYPE_METHOD);
  11601. (void)heap;
  11602. WOLFSSL_ENTER("DTLS_client_method_ex");
  11603. if (method) {
  11604. #if defined(WOLFSSL_DTLS13)
  11605. InitSSL_Method(method, MakeDTLSv1_3());
  11606. #elif !defined(WOLFSSL_NO_TLS12)
  11607. InitSSL_Method(method, MakeDTLSv1_2());
  11608. #elif !defined(NO_OLD_TLS)
  11609. InitSSL_Method(method, MakeDTLSv1());
  11610. #else
  11611. #error No DTLS version enabled!
  11612. #endif
  11613. method->downgrade = 1;
  11614. method->side = WOLFSSL_CLIENT_END;
  11615. }
  11616. return method;
  11617. }
  11618. #ifndef NO_OLD_TLS
  11619. WOLFSSL_METHOD* wolfDTLSv1_client_method(void)
  11620. {
  11621. return wolfDTLSv1_client_method_ex(NULL);
  11622. }
  11623. WOLFSSL_METHOD* wolfDTLSv1_client_method_ex(void* heap)
  11624. {
  11625. WOLFSSL_METHOD* method =
  11626. (WOLFSSL_METHOD*) XMALLOC(sizeof(WOLFSSL_METHOD),
  11627. heap, DYNAMIC_TYPE_METHOD);
  11628. (void)heap;
  11629. WOLFSSL_ENTER("DTLSv1_client_method_ex");
  11630. if (method)
  11631. InitSSL_Method(method, MakeDTLSv1());
  11632. return method;
  11633. }
  11634. #endif /* NO_OLD_TLS */
  11635. #ifndef WOLFSSL_NO_TLS12
  11636. WOLFSSL_METHOD* wolfDTLSv1_2_client_method(void)
  11637. {
  11638. return wolfDTLSv1_2_client_method_ex(NULL);
  11639. }
  11640. WOLFSSL_METHOD* wolfDTLSv1_2_client_method_ex(void* heap)
  11641. {
  11642. WOLFSSL_METHOD* method =
  11643. (WOLFSSL_METHOD*) XMALLOC(sizeof(WOLFSSL_METHOD),
  11644. heap, DYNAMIC_TYPE_METHOD);
  11645. (void)heap;
  11646. WOLFSSL_ENTER("DTLSv1_2_client_method_ex");
  11647. if (method)
  11648. InitSSL_Method(method, MakeDTLSv1_2());
  11649. (void)heap;
  11650. return method;
  11651. }
  11652. #endif /* !WOLFSSL_NO_TLS12 */
  11653. #endif /* WOLFSSL_DTLS */
  11654. #endif /* NO_WOLFSSL_CLIENT */
  11655. /* EITHER SIDE METHODS */
  11656. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  11657. #ifndef NO_OLD_TLS
  11658. #ifdef WOLFSSL_ALLOW_TLSV10
  11659. /* Gets a WOLFSSL_METHOD type that is not set as client or server
  11660. *
  11661. * Returns a pointer to a WOLFSSL_METHOD struct
  11662. */
  11663. WOLFSSL_METHOD* wolfTLSv1_method(void)
  11664. {
  11665. return wolfTLSv1_method_ex(NULL);
  11666. }
  11667. WOLFSSL_METHOD* wolfTLSv1_method_ex(void* heap)
  11668. {
  11669. WOLFSSL_METHOD* m;
  11670. WOLFSSL_ENTER("TLSv1_method");
  11671. #ifndef NO_WOLFSSL_CLIENT
  11672. m = wolfTLSv1_client_method_ex(heap);
  11673. #else
  11674. m = wolfTLSv1_server_method_ex(heap);
  11675. #endif
  11676. if (m != NULL) {
  11677. m->side = WOLFSSL_NEITHER_END;
  11678. }
  11679. return m;
  11680. }
  11681. #endif /* WOLFSSL_ALLOW_TLSV10 */
  11682. /* Gets a WOLFSSL_METHOD type that is not set as client or server
  11683. *
  11684. * Returns a pointer to a WOLFSSL_METHOD struct
  11685. */
  11686. WOLFSSL_METHOD* wolfTLSv1_1_method(void)
  11687. {
  11688. return wolfTLSv1_1_method_ex(NULL);
  11689. }
  11690. WOLFSSL_METHOD* wolfTLSv1_1_method_ex(void* heap)
  11691. {
  11692. WOLFSSL_METHOD* m;
  11693. WOLFSSL_ENTER("TLSv1_1_method");
  11694. #ifndef NO_WOLFSSL_CLIENT
  11695. m = wolfTLSv1_1_client_method_ex(heap);
  11696. #else
  11697. m = wolfTLSv1_1_server_method_ex(heap);
  11698. #endif
  11699. if (m != NULL) {
  11700. m->side = WOLFSSL_NEITHER_END;
  11701. }
  11702. return m;
  11703. }
  11704. #endif /* !NO_OLD_TLS */
  11705. #ifndef WOLFSSL_NO_TLS12
  11706. /* Gets a WOLFSSL_METHOD type that is not set as client or server
  11707. *
  11708. * Returns a pointer to a WOLFSSL_METHOD struct
  11709. */
  11710. WOLFSSL_METHOD* wolfTLSv1_2_method(void)
  11711. {
  11712. return wolfTLSv1_2_method_ex(NULL);
  11713. }
  11714. WOLFSSL_METHOD* wolfTLSv1_2_method_ex(void* heap)
  11715. {
  11716. WOLFSSL_METHOD* m;
  11717. WOLFSSL_ENTER("TLSv1_2_method");
  11718. #ifndef NO_WOLFSSL_CLIENT
  11719. m = wolfTLSv1_2_client_method_ex(heap);
  11720. #else
  11721. m = wolfTLSv1_2_server_method_ex(heap);
  11722. #endif
  11723. if (m != NULL) {
  11724. m->side = WOLFSSL_NEITHER_END;
  11725. }
  11726. return m;
  11727. }
  11728. #endif /* !WOLFSSL_NO_TLS12 */
  11729. #ifdef WOLFSSL_TLS13
  11730. /* Gets a WOLFSSL_METHOD type that is not set as client or server
  11731. *
  11732. * Returns a pointer to a WOLFSSL_METHOD struct
  11733. */
  11734. WOLFSSL_METHOD* wolfTLSv1_3_method(void)
  11735. {
  11736. return wolfTLSv1_3_method_ex(NULL);
  11737. }
  11738. WOLFSSL_METHOD* wolfTLSv1_3_method_ex(void* heap)
  11739. {
  11740. WOLFSSL_METHOD* m;
  11741. WOLFSSL_ENTER("TLSv1_3_method");
  11742. #ifndef NO_WOLFSSL_CLIENT
  11743. m = wolfTLSv1_3_client_method_ex(heap);
  11744. #else
  11745. m = wolfTLSv1_3_server_method_ex(heap);
  11746. #endif
  11747. if (m != NULL) {
  11748. m->side = WOLFSSL_NEITHER_END;
  11749. }
  11750. return m;
  11751. }
  11752. #endif /* WOLFSSL_TLS13 */
  11753. #ifdef WOLFSSL_DTLS
  11754. WOLFSSL_METHOD* wolfDTLS_method(void)
  11755. {
  11756. return wolfDTLS_method_ex(NULL);
  11757. }
  11758. WOLFSSL_METHOD* wolfDTLS_method_ex(void* heap)
  11759. {
  11760. WOLFSSL_METHOD* m;
  11761. WOLFSSL_ENTER("DTLS_method_ex");
  11762. #ifndef NO_WOLFSSL_CLIENT
  11763. m = wolfDTLS_client_method_ex(heap);
  11764. #else
  11765. m = wolfDTLS_server_method_ex(heap);
  11766. #endif
  11767. if (m != NULL) {
  11768. m->side = WOLFSSL_NEITHER_END;
  11769. }
  11770. return m;
  11771. }
  11772. #ifndef NO_OLD_TLS
  11773. WOLFSSL_METHOD* wolfDTLSv1_method(void)
  11774. {
  11775. return wolfDTLSv1_method_ex(NULL);
  11776. }
  11777. WOLFSSL_METHOD* wolfDTLSv1_method_ex(void* heap)
  11778. {
  11779. WOLFSSL_METHOD* m;
  11780. WOLFSSL_ENTER("DTLSv1_method_ex");
  11781. #ifndef NO_WOLFSSL_CLIENT
  11782. m = wolfDTLSv1_client_method_ex(heap);
  11783. #else
  11784. m = wolfDTLSv1_server_method_ex(heap);
  11785. #endif
  11786. if (m != NULL) {
  11787. m->side = WOLFSSL_NEITHER_END;
  11788. }
  11789. return m;
  11790. }
  11791. #endif /* !NO_OLD_TLS */
  11792. #ifndef WOLFSSL_NO_TLS12
  11793. WOLFSSL_METHOD* wolfDTLSv1_2_method(void)
  11794. {
  11795. return wolfDTLSv1_2_method_ex(NULL);
  11796. }
  11797. WOLFSSL_METHOD* wolfDTLSv1_2_method_ex(void* heap)
  11798. {
  11799. WOLFSSL_METHOD* m;
  11800. WOLFSSL_ENTER("DTLSv1_2_method");
  11801. #ifndef NO_WOLFSSL_CLIENT
  11802. m = wolfDTLSv1_2_client_method_ex(heap);
  11803. #else
  11804. m = wolfDTLSv1_2_server_method_ex(heap);
  11805. #endif
  11806. if (m != NULL) {
  11807. m->side = WOLFSSL_NEITHER_END;
  11808. }
  11809. return m;
  11810. }
  11811. #endif /* !WOLFSSL_NO_TLS12 */
  11812. #endif /* WOLFSSL_DTLS */
  11813. #endif /* OPENSSL_EXTRA || WOLFSSL_EITHER_SIDE */
  11814. #ifndef NO_WOLFSSL_SERVER
  11815. WOLFSSL_METHOD* wolfTLS_server_method(void)
  11816. {
  11817. return wolfTLS_server_method_ex(NULL);
  11818. }
  11819. WOLFSSL_METHOD* wolfTLS_server_method_ex(void* heap)
  11820. {
  11821. WOLFSSL_METHOD* method =
  11822. (WOLFSSL_METHOD*) XMALLOC(sizeof(WOLFSSL_METHOD),
  11823. heap, DYNAMIC_TYPE_METHOD);
  11824. (void)heap;
  11825. WOLFSSL_ENTER("TLS_server_method_ex");
  11826. if (method) {
  11827. #if defined(WOLFSSL_TLS13)
  11828. InitSSL_Method(method, MakeTLSv1_3());
  11829. #elif !defined(WOLFSSL_NO_TLS12)
  11830. InitSSL_Method(method, MakeTLSv1_2());
  11831. #elif !defined(NO_OLD_TLS)
  11832. InitSSL_Method(method, MakeTLSv1_1());
  11833. #elif defined(WOLFSSL_ALLOW_TLSV10)
  11834. InitSSL_Method(method, MakeTLSv1());
  11835. #else
  11836. #error No TLS version enabled!
  11837. #endif
  11838. method->downgrade = 1;
  11839. method->side = WOLFSSL_SERVER_END;
  11840. }
  11841. return method;
  11842. }
  11843. #ifndef NO_OLD_TLS
  11844. #ifdef WOLFSSL_ALLOW_TLSV10
  11845. WOLFSSL_METHOD* wolfTLSv1_server_method(void)
  11846. {
  11847. return wolfTLSv1_server_method_ex(NULL);
  11848. }
  11849. WOLFSSL_METHOD* wolfTLSv1_server_method_ex(void* heap)
  11850. {
  11851. WOLFSSL_METHOD* method =
  11852. (WOLFSSL_METHOD*) XMALLOC(sizeof(WOLFSSL_METHOD),
  11853. heap, DYNAMIC_TYPE_METHOD);
  11854. (void)heap;
  11855. WOLFSSL_ENTER("TLSv1_server_method_ex");
  11856. if (method) {
  11857. InitSSL_Method(method, MakeTLSv1());
  11858. method->side = WOLFSSL_SERVER_END;
  11859. }
  11860. return method;
  11861. }
  11862. #endif /* WOLFSSL_ALLOW_TLSV10 */
  11863. WOLFSSL_METHOD* wolfTLSv1_1_server_method(void)
  11864. {
  11865. return wolfTLSv1_1_server_method_ex(NULL);
  11866. }
  11867. WOLFSSL_METHOD* wolfTLSv1_1_server_method_ex(void* heap)
  11868. {
  11869. WOLFSSL_METHOD* method =
  11870. (WOLFSSL_METHOD*) XMALLOC(sizeof(WOLFSSL_METHOD),
  11871. heap, DYNAMIC_TYPE_METHOD);
  11872. (void)heap;
  11873. WOLFSSL_ENTER("TLSv1_1_server_method_ex");
  11874. if (method) {
  11875. InitSSL_Method(method, MakeTLSv1_1());
  11876. method->side = WOLFSSL_SERVER_END;
  11877. }
  11878. return method;
  11879. }
  11880. #endif /* !NO_OLD_TLS */
  11881. #ifndef WOLFSSL_NO_TLS12
  11882. WOLFSSL_ABI
  11883. WOLFSSL_METHOD* wolfTLSv1_2_server_method(void)
  11884. {
  11885. return wolfTLSv1_2_server_method_ex(NULL);
  11886. }
  11887. WOLFSSL_METHOD* wolfTLSv1_2_server_method_ex(void* heap)
  11888. {
  11889. WOLFSSL_METHOD* method =
  11890. (WOLFSSL_METHOD*) XMALLOC(sizeof(WOLFSSL_METHOD),
  11891. heap, DYNAMIC_TYPE_METHOD);
  11892. (void)heap;
  11893. WOLFSSL_ENTER("TLSv1_2_server_method_ex");
  11894. if (method) {
  11895. InitSSL_Method(method, MakeTLSv1_2());
  11896. method->side = WOLFSSL_SERVER_END;
  11897. }
  11898. return method;
  11899. }
  11900. #endif /* !WOLFSSL_NO_TLS12 */
  11901. #ifdef WOLFSSL_TLS13
  11902. /* The TLS v1.3 server method data.
  11903. *
  11904. * returns the method data for a TLS v1.3 server.
  11905. */
  11906. WOLFSSL_ABI
  11907. WOLFSSL_METHOD* wolfTLSv1_3_server_method(void)
  11908. {
  11909. return wolfTLSv1_3_server_method_ex(NULL);
  11910. }
  11911. /* The TLS v1.3 server method data.
  11912. *
  11913. * heap The heap used for allocation.
  11914. * returns the method data for a TLS v1.3 server.
  11915. */
  11916. WOLFSSL_METHOD* wolfTLSv1_3_server_method_ex(void* heap)
  11917. {
  11918. WOLFSSL_METHOD* method =
  11919. (WOLFSSL_METHOD*) XMALLOC(sizeof(WOLFSSL_METHOD),
  11920. heap, DYNAMIC_TYPE_METHOD);
  11921. (void)heap;
  11922. WOLFSSL_ENTER("TLSv1_3_server_method_ex");
  11923. if (method) {
  11924. InitSSL_Method(method, MakeTLSv1_3());
  11925. method->side = WOLFSSL_SERVER_END;
  11926. }
  11927. return method;
  11928. }
  11929. #endif /* WOLFSSL_TLS13 */
  11930. #ifdef WOLFSSL_DTLS
  11931. WOLFSSL_METHOD* wolfDTLS_server_method(void)
  11932. {
  11933. return wolfDTLS_server_method_ex(NULL);
  11934. }
  11935. WOLFSSL_METHOD* wolfDTLS_server_method_ex(void* heap)
  11936. {
  11937. WOLFSSL_METHOD* method =
  11938. (WOLFSSL_METHOD*) XMALLOC(sizeof(WOLFSSL_METHOD),
  11939. heap, DYNAMIC_TYPE_METHOD);
  11940. (void)heap;
  11941. WOLFSSL_ENTER("DTLS_server_method_ex");
  11942. if (method) {
  11943. #if defined(WOLFSSL_DTLS13)
  11944. InitSSL_Method(method, MakeDTLSv1_3());
  11945. #elif !defined(WOLFSSL_NO_TLS12)
  11946. InitSSL_Method(method, MakeDTLSv1_2());
  11947. #elif !defined(NO_OLD_TLS)
  11948. InitSSL_Method(method, MakeDTLSv1());
  11949. #else
  11950. #error No DTLS version enabled!
  11951. #endif
  11952. method->downgrade = 1;
  11953. method->side = WOLFSSL_SERVER_END;
  11954. }
  11955. return method;
  11956. }
  11957. #ifndef NO_OLD_TLS
  11958. WOLFSSL_METHOD* wolfDTLSv1_server_method(void)
  11959. {
  11960. return wolfDTLSv1_server_method_ex(NULL);
  11961. }
  11962. WOLFSSL_METHOD* wolfDTLSv1_server_method_ex(void* heap)
  11963. {
  11964. WOLFSSL_METHOD* method =
  11965. (WOLFSSL_METHOD*) XMALLOC(sizeof(WOLFSSL_METHOD),
  11966. heap, DYNAMIC_TYPE_METHOD);
  11967. (void)heap;
  11968. WOLFSSL_ENTER("DTLSv1_server_method_ex");
  11969. if (method) {
  11970. InitSSL_Method(method, MakeDTLSv1());
  11971. method->side = WOLFSSL_SERVER_END;
  11972. }
  11973. return method;
  11974. }
  11975. #endif /* !NO_OLD_TLS */
  11976. #ifndef WOLFSSL_NO_TLS12
  11977. WOLFSSL_METHOD* wolfDTLSv1_2_server_method(void)
  11978. {
  11979. return wolfDTLSv1_2_server_method_ex(NULL);
  11980. }
  11981. WOLFSSL_METHOD* wolfDTLSv1_2_server_method_ex(void* heap)
  11982. {
  11983. WOLFSSL_METHOD* method =
  11984. (WOLFSSL_METHOD*) XMALLOC(sizeof(WOLFSSL_METHOD),
  11985. heap, DYNAMIC_TYPE_METHOD);
  11986. WOLFSSL_ENTER("DTLSv1_2_server_method_ex");
  11987. (void)heap;
  11988. if (method) {
  11989. InitSSL_Method(method, MakeDTLSv1_2());
  11990. method->side = WOLFSSL_SERVER_END;
  11991. }
  11992. (void)heap;
  11993. return method;
  11994. }
  11995. #endif /* !WOLFSSL_NO_TLS12 */
  11996. #endif /* WOLFSSL_DTLS */
  11997. #endif /* NO_WOLFSSL_SERVER */
  11998. #endif /* NO_TLS */
  11999. #endif /* WOLFCRYPT_ONLY */